A related problem that this patch does not fix, found with the same
reproducer (nv-mmu-fault.c, below my signature; libdrm only, Fermi
only).
If a channel sends a graphics method while it has no graphics object,
and no other channel has one either, the GPU is lost: no MMU fault is
reported, so recovery never starts, the channel cannot be idled, and
every later client hangs in its teardown. A reboot is needed.
nv-mmu-fault -e noctx-idle
sends one method to subchannel 0 after a SET_OBJECT of class 0x90b5
without the engine number, which routes it to GR. When another channel
does have a GR object ("-e noctx"), GR's context-switch unit faults
(READ at 0, HUB/CTXCTL) and recovery works with this patch.
This needs a working copy-engine channel: Ubuntu's 7.0 kernel lacks
a2b7b707d75e, so its TTM channel uses M2MF on GR, and GR always has an
object. With a2b7b707d75e (mainline), nothing keeps GR in use while no
client uses it.
Quadro 600 (GF108), Ubuntu 7.0.0-44 nouveau + a2b7b707d75e, with and
without this v2 (same result):
- While the channel is alive (40 s), the GR status (0x002640) reads
80041002 (chid 2 = the channel, bit 31 set), and the FIFO raises no
interrupt at all.
- When the process exits:
nouveau 0000:01:00.0: nv-mmu-fault[16835]: failed to idle channel 2
[nv-mmu-fault[16835]]
nouveau 0000:01:00.0: timeout
WARNING: nvkm/engine/fifo/runl.c:251 at nvkm_runl_preempt_wait+0xf7/0x110
[nouveau]
nvkm_chan_preempt_locked+0x4a/0x70 [nouveau]
nvkm_chan_cctx_bind+0xa7/0x170 [nouveau]
...
nouveau 0000:01:00.0: fifo: SCHED_ERROR 0d []
(same warning, from nvkm_chan_remove)
nouveau 0000:01:00.0: fifo: SCHED_ERROR 0d []
WARNING: nvkm/engine/fifo/nv50.c:228 at nv50_runl_wait+0xe8/0x100 [nouveau]
nouveau 0000:01:00.0: fifo: INTR 00000001: 0000000b
- A GL client started afterwards (piglit fbo-blit) times out in
nv50_runl_wait, and its channel cannot be idled either.
On an NVS 315 (GF119), with our test tree that also has a working CE
channel, the FIFO does report it, as SCHED_ERROR 0a [CTXSW_TIMEOUT] every
4.3 s, with the GR status at 80041002. gf100_fifo_intr_sched_ctxsw()
then finds no engine to recover: gf100_engn_chsw() only treats the status
as a switch in progress if bit 28 (busy) is set, so the timeout repeats
forever.
Things I tried on the GF119, as experiments only:
- Treating bit 31 as "switch in progress" makes the handler trigger a
recovery, but by then the status reads 01140002 and
nvkm_runl_rc_engn() skips it ("not on channel").
- Remembering the channel at the timeout, recovering it and resetting
GR led to a divide error in gf100_gr_init_zcull(), reached from
gf100_gr_reset() -> gf100_gr_init() on a GR that was never
initialised (tpc_total == 0). nvkm_engine_reset() does not check
whether the engine is in use.
- Doing only nvkm_mc_reset() for an engine that is not in use recovered
11 of 11 rounds, and the GPU was fine afterwards. But bit 31 is also
set during ordinary context switches, so channel teardown then polled
for about a second each time.
I don't know what bit 31 of 0x002640 means (envytools has no entry for
it), so I have no patch to propose. Userspace should not be able to
take the GPU down this way. So far I have only hit it with the
reproducer.
Testing done with the help of Claude (AI assistant).
Frédéric Mahé
---8<--- nv-mmu-fault.c ---8<---
// SPDX-License-Identifier: MIT
/*
* nv-mmu-fault: make a Fermi GPU take an MMU fault on demand, to test
* nouveau's FIFO recovery.
*
* Opens the nouveau render node, creates a channel, and submits one push
* buffer that makes an engine access an unmapped GPU virtual address
* (0 by default). The kernel is expected to report the fault, recover
* and kill the channel; later submissions on it fail with -ENODEV.
* Each round runs in a child process with a fresh channel; the child
* exits without libdrm teardown (the kernel frees the channel when the
* file is closed).
*
* nv-mmu-fault [-e MODE] [-n rounds] [-a addr] [-V] [-w ms] [-r node]
*
* Modes (-e):
* gr M2MF (class 0x9039, GR engine) inline write to addr.
* ce copy engine 0 (class 0x90b5) copy between two unmapped ranges.
* both ce copy, then gr write, on the same channel (on GF119 only the
* first fault is ever reported: the channel is stopped at once).
* noctx a GR method on a channel that has no GR object, so no GR
* context (SET_OBJECT 0x90b5 without the engine number routes
* the subchannel to GR). GR's context-switch unit then reads
* a null context: fault READ at 0, client HUB/CTXCTL. The tool
* keeps a second channel with a GR object open meanwhile, so
* that GR is powered.
* noctx-idle the same without that second channel: if nothing else
* uses GR (needs a working copy-engine channel, i.e. a kernel
* with a2b7b707d75e), no fault is raised: on GF119 the FIFO
* reports SCHED_ERROR CTXSW_TIMEOUT every 4.3 s, on GF108
* nothing at all until teardown; either way the channel can
* no longer be idled and the GPU is lost.
* -V control run: use a real VRAM buffer; nothing should fault and the
* channel should stay alive.
*
* Exit status: 0 if every round ended as expected (channel killed, or
* alive with -V), 1 otherwise.
*
* Fermi only (GF1xx): the classes, method offsets and the SET_OBJECT
* format (class | engine << 16) are Fermi's.
*
* Build: gcc -O2 -Wall -o nv-mmu-fault nv-mmu-fault.c \
* $(pkg-config --cflags --libs libdrm_nouveau)
*
* Written with help from Claude (Anthropic).
*/
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include <linux/types.h>
#include <xf86drm.h>
#include <nouveau_drm.h>
#include <nouveau.h>
#include <nvif/class.h>
#include <nvif/cl0080.h>
#define SUBC_M2MF 2
#define SUBC_CE 4
#define M2MF_CLASS 0x9039 /* GR engine (0) */
#define CE_CLASS 0x90b5 /* copy engine 0: FIFO engine 4 */
#define CE_ENGINE 4
/* Fermi FIFO packet header: incrementing methods. */
static uint32_t
pkhdr(int subc, int mthd, unsigned size)
{
return 0x20000000 | (size << 16) | (subc << 13) | (mthd >> 2);
}
/* Fermi FIFO packet header: non-incrementing methods. */
static uint32_t
pkhdr_ni(int subc, int mthd, unsigned size)
{
return 0x60000000 | (size << 16) | (subc << 13) | (mthd >> 2);
}
#define OUT(v) (*push->cur++ = (v))
static const char *node = "/dev/dri/renderD128";
static uint64_t addr;
static int use_gr, use_ce, noctx, valid_buf, wait_ms = 5000;
static double
now_ms(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1e3 + ts.tv_nsec / 1e6;
}
static void
usage(void)
{
fprintf(stderr, "usage: nv-mmu-fault [-e gr|ce|both|noctx|noctx-idle]
[-n rounds] "
"[-a addr] [-V] [-w max-wait-ms] [-r /dev/dri/renderD128]\n");
exit(2);
}
/* One round, in a child process. Exit status: 0 channel killed,
* 1 channel alive at the end of the wait, 2 setup error.
*/
static void
round_child(int r)
{
struct nv_device_v0 devarg = { .device = ~0ULL };
struct nvc0_fifo fifo = { };
struct nouveau_object *chan, *obj;
struct nouveau_client *client;
struct nouveau_pushbuf *push;
struct nouveau_device *dev;
struct nouveau_drm *drm;
double t0, t;
int fd, ret;
fd = open(node, O_RDWR | O_CLOEXEC);
if (fd < 0) {
perror(node);
_exit(2);
}
if (nouveau_drm_new(fd, &drm) ||
nouveau_device_new(&drm->client, NV_DEVICE, &devarg,
sizeof(devarg), &dev)) {
fprintf(stderr, "nouveau device open failed\n");
_exit(2);
}
if (dev->chipset < 0xc0 || dev->chipset >= 0xe0) {
fprintf(stderr, "chipset %02x: this tool is for Fermi (GF1xx)
only\n",
dev->chipset);
_exit(2);
}
if (nouveau_object_new(&dev->object, 0, NOUVEAU_FIFO_CHANNEL_CLASS,
&fifo, sizeof(fifo), &chan) ||
nouveau_client_new(dev, &client) ||
nouveau_pushbuf_new(client, chan, 4, 32 * 1024, true, &push)) {
fprintf(stderr, "round %d: channel setup failed\n", r);
_exit(2);
}
if (use_gr && nouveau_object_new(chan, 0xbeef9039, M2MF_CLASS,
NULL, 0, &obj)) {
fprintf(stderr, "M2MF class %04x not available\n", M2MF_CLASS);
_exit(2);
}
if ((use_ce || noctx) && nouveau_object_new(chan, 0xbeef90b5, CE_CLASS,
NULL, 0, &obj)) {
fprintf(stderr, "copy class %04x not available\n", CE_CLASS);
_exit(2);
}
if (valid_buf) {
/* Control run: target a real 4 MiB VRAM buffer instead. */
struct nouveau_pushbuf_refn ref;
struct nouveau_bo *bo;
if (nouveau_bo_new(dev, NOUVEAU_BO_VRAM, 0x1000, 4 << 20,
NULL, &bo))
_exit(2);
ref.bo = bo;
ref.flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_RDWR;
if (nouveau_pushbuf_refn(push, &ref, 1))
_exit(2);
addr = bo->offset;
}
if (nouveau_pushbuf_space(push, 64, 0, 0))
_exit(2);
if (use_gr) {
OUT(pkhdr(SUBC_M2MF, 0x0000, 1)); /* SET_OBJECT */
OUT(M2MF_CLASS);
}
if (use_ce) {
OUT(pkhdr(SUBC_CE, 0x0000, 1));
OUT(CE_CLASS | (CE_ENGINE << 16));
}
if (noctx) {
/* No engine number: the subchannel goes to engine 0 (GR),
* for which this channel has no context.
*/
OUT(pkhdr(SUBC_CE, 0x0000, 1));
OUT(CE_CLASS);
}
if (use_gr && use_ce) {
/* Submit the bindings first, so that GR has switched to this
* channel before the two faulting operations arrive together.
*/
if (nouveau_pushbuf_kick(push, chan))
_exit(2);
usleep(20000);
if (nouveau_pushbuf_space(push, 64, 0, 0))
_exit(2);
}
if (use_ce) {
/* 4 KiB linear copy from addr + 1 MiB to addr + 2 MiB.
Launched
* before the GR write: the copy engine runs on its own while
* the FIFO goes on to the GR methods, so in "both" mode the two
* engines can fault together.
*/
uint64_t src = addr + 0x100000, dst = addr + 0x200000;
OUT(pkhdr(SUBC_CE, 0x030c, 8)); /* src, dst, pitches, size */
OUT(src >> 32);
OUT((uint32_t)src);
OUT(dst >> 32);
OUT((uint32_t)dst);
OUT(4096); /* pitch in */
OUT(4096); /* pitch out */
OUT(4096); /* line length */
OUT(1); /* line count */
OUT(pkhdr(SUBC_CE, 0x0300, 1)); /* LAUNCH_DMA */
OUT(0x00000110); /* linear in/out */
}
if (use_gr) {
/* Inline M2MF write of 16 dwords to addr. */
OUT(pkhdr(SUBC_M2MF, 0x0238, 2)); /* OFFSET_OUT_HIGH */
OUT(addr >> 32);
OUT((uint32_t)addr);
OUT(pkhdr(SUBC_M2MF, 0x031c, 2)); /* LINE_LENGTH_IN */
OUT(16 * 4);
OUT(1); /* LINE_COUNT */
OUT(pkhdr(SUBC_M2MF, 0x0300, 1)); /* EXEC */
OUT(0x00100111); /* push, linear in/out */
OUT(pkhdr_ni(SUBC_M2MF, 0x0304, 16)); /* DATA */
for (int i = 0; i < 16; i++)
OUT(0xdead0000 | i);
}
t0 = now_ms();
ret = nouveau_pushbuf_kick(push, chan);
if (ret) {
fprintf(stderr, "round %d: first kick failed: %d\n", r, ret);
_exit(2);
}
/* Wait for the kernel to kill the channel. The pushbuf ioctl checks
* chan->killed before anything else, so an empty submission (no
* pushes) is a side-effect-free probe. (A real second kick would
* hit an assertion in libdrm once the kernel rejects it.)
*/
for (;;) {
struct drm_nouveau_gem_pushbuf req = {
.channel = ((struct nouveau_fifo *)chan->data)->channel,
};
usleep(2000);
ret = drmCommandWriteRead(drm->fd, DRM_NOUVEAU_GEM_PUSHBUF,
&req, sizeof(req));
t = now_ms() - t0;
if (ret) {
printf("round %d: channel killed after %.0f ms (probe:
%d)\n",
r, t, ret);
fflush(stdout);
_exit(0);
}
if (t > wait_ms) {
printf("round %d: channel still alive after %d ms\n",
r, wait_ms);
fflush(stdout);
_exit(1);
}
}
}
/* Keep GR powered for the whole run: nouveau only initialises an engine
* while some channel has an object on it. With GR up, a GR method on a
* channel without GR context makes GR's context-switch unit read a null
* context (MMU fault, HUB/CTXCTL); with GR down it only stalls the FIFO
* (SCHED_ERROR CTXSW_TIMEOUT).
*/
static void
gr_holder(void)
{
struct nv_device_v0 devarg = { .device = ~0ULL };
struct nvc0_fifo fifo = { };
struct nouveau_object *chan, *obj;
struct nouveau_device *dev;
struct nouveau_drm *drm;
int fd = open(node, O_RDWR | O_CLOEXEC);
if (fd < 0 || nouveau_drm_new(fd, &drm) ||
nouveau_device_new(&drm->client, NV_DEVICE, &devarg,
sizeof(devarg), &dev) ||
nouveau_object_new(&dev->object, 0, NOUVEAU_FIFO_CHANNEL_CLASS,
&fifo, sizeof(fifo), &chan) ||
nouveau_object_new(chan, 0xbeef9039, M2MF_CLASS, NULL, 0, &obj)) {
fprintf(stderr, "GR holder channel setup failed\n");
exit(2);
}
}
int
main(int argc, char **argv)
{
const char *eng = "gr";
int rounds = 1, opt, r, killed = 0, alive = 0, errors = 0;
while ((opt = getopt(argc, argv, "e:n:a:w:r:V")) != -1) {
switch (opt) {
case 'e': eng = optarg; break;
case 'n': rounds = atoi(optarg); break;
case 'a': addr = strtoull(optarg, NULL, 0); break;
case 'w': wait_ms = atoi(optarg); break;
case 'r': node = optarg; break;
case 'V': valid_buf = 1; break;
default: usage();
}
}
use_gr = !strcmp(eng, "gr") || !strcmp(eng, "both");
use_ce = !strcmp(eng, "ce") || !strcmp(eng, "both");
noctx = !strncmp(eng, "noctx", 5);
if (!use_gr && !use_ce && !noctx)
usage();
if (noctx && strcmp(eng, "noctx-idle"))
gr_holder();
for (r = 1; r <= rounds; r++) {
int status;
pid_t pid = fork();
if (pid < 0) {
perror("fork");
return 1;
}
if (pid == 0)
round_child(r);
if (waitpid(pid, &status, 0) < 0 || !WIFEXITED(status)) {
errors++;
continue;
}
switch (WEXITSTATUS(status)) {
case 0: killed++; break;
case 1: alive++; break;
default: errors++; break;
}
if (WEXITSTATUS(status) == 2)
break; /* setup problem: no point going on */
}
printf("nv-mmu-fault: %d rounds, %d channels killed, %d alive, %d
errors\n",
rounds, killed, alive, errors);
if (errors)
return 1;
return valid_buf ? (killed != 0) : (alive != 0);
}