drm/nouveau/fifo/gf100-: reset the faulting engine even if it switched channels
Subsystems (4)
PATCH v2 1/1 drm/nouveau/fifo/gf100-: reset the faulting engine even if it switched channels
When an engine reports an MMU fault, recovery flags the faulting channel group and nvkm_runl_rc() resets only the engines whose current context (cxid) belongs to a flagged group. On Fermi the fault can be reported after GR has started switching to another channel: the engine status then reads e.g. 0x01140003 (not busy, chid 3) while channel 4 faulted, so GR is not reset, stays stalled on the fault, and the runlist update never completes:
fifo: fault 01 [WRITE] at 0 engine 00 [PGRAPH] client 0f [GPC0/PROP] reason 02 [PAGE_NOT_PRESENT] on channel 4 object-namespac[1768]: channel 4 killed!
fifo:000000:00[ gr]: 01140003: busy 0 save 1 unk0 0 unk1 0 chid 3
fifo:000000:00[ gr]: cxid not valid
WARNING: drivers/gpu/drm/nouveau/nvkm/engine/fifo/nv50.c:228 at nv50_runl_wait+0xe8/0x100 [nouveau] fifo: SCHED_ERROR 0d [] WARNING: drivers/gpu/drm/nouveau/nvkm/engine/fifo/runl.c:251 at nvkm_runl_preempt_wait+0xf7/0x110 [nouveau]
After that no channel makes progress: the whole GPU is dead until a reboot, and processes using it hang in D state.
Record in the channel group which engines reported a fault for it. The recovery pass that takes the group from pending to running collects that mask, and resets those engines regardless of what their status says. Resetting GR may discard the context being switched in, but that is far better than losing the GPU.
Faults reported for a group whose recovery is already running are recorded but never acted on: as before, such a group is not recovered again, and keeping the mask with the group means a later recovery of another group cannot reset an engine on its behalf.
Tested on a Quadro 600 (GF108): a program that faults (piglit "object-namespace-pollution glClearTexSubImage texture") while three other processes keep GR busy hangs the GPU within one to three faults without this patch. With it, 20 of 20 faults recovered (17 of them hit the state above), with no runlist timeouts, and the GPU kept working. On an NVS 315 (GF119) the same test hangs the GPU at the first fault without this patch; with it, 20 of 20 rounds completed, every fault was recovered, and the GPU kept working.
Written with help from Claude (Anthropic); reviewed and tested on hardware.
---
v2: keep the faulting engines in the channel group (cgrp->rc_engm) instead of a per-engine flag, and consume them only in the recovery pass that takes that group into recovery. v1 could leave the flag set when a second fault arrived for a group already in recovery, so that a later recovery of another group would reset the engine; it could also let a pass already in progress reset an engine for a group whose channels it had not removed yet. Both reported by the Sashiko review of v1. rc_engm is initialised explicitly, as nvkm_cgrp_new() uses kmalloc. v2 was tested again on the Quadro 600 (GF108): the piglit test above recovered 20 of 20 faults (17 in the state above); "nv-mmu-fault -e noctx" (below) recovered 60 of 60 runs, plus 20 of 20 with fifo debug output, where GR was reset every time through the new mask (its status held no valid context); "-e gr" under load recovered 2 x 40 of 40. No runlist timeouts. The NVS 315 numbers are from v1.
v1: https://lore.kernel.org/nouveau/[email protected]/
Tested on GF108 and GF119 only. gf100_fifo_mmu_fault_recover() is also used by gk104, gm107, gv100 and tu102 (non-GSP), so the change applies to those too; feedback from such hardware is welcome.
A small libdrm program reproduces this without Mesa or piglit (nv-mmu-fault.c, available on request):
nv-mmu-fault -e noctx
sends one graphics method on a channel that has no graphics object. PGRAPH's context-switch unit then faults (READ at 0, HUB/CTXCTL) while the engine status no longer names the channel. On an NVS 315 (GF119), without this patch the GPU is dead after the first run; with it, 60 of 60 runs recovered. "nv-mmu-fault -e gr" in a loop with three processes keeping PGRAPH busy gives the WRITE fault of the log above: dead at the 4th fault without the patch, 40 of 40 recovered with it.
drivers/gpu/drm/nouveau/nvkm/engine/fifo/cgrp.c | 1 + drivers/gpu/drm/nouveau/nvkm/engine/fifo/cgrp.h | 1 + drivers/gpu/drm/nouveau/nvkm/engine/fifo/gf100.c | 11 +++++++++-- drivers/gpu/drm/nouveau/nvkm/engine/fifo/runl.c | 15 ++++++++++++--- 4 files changed, 23 insertions(+), 5 deletions(-)
drivers/gpu/drm/nouveau/nvkm/engine/fifo/cgrp.c +1 -0
@@ -241,6 +241,7 @@ nvkm_cgrp_new(struct nvkm_runl *runl, const char *name, struct nvkm_vmm *vmm, boINIT_LIST_HEAD (&cgrp->vctxs);mutex_init (&cgrp->mutex);atomic_set (&cgrp->rc,NVKM_CGRP_RC_NONE ); +atomic_set (&cgrp->rc_engm,0 );if (runl->cgid) { cgrp->id =nvkm_chid_get (runl->cgid, cgrp);
drivers/gpu/drm/nouveau/nvkm/engine/fifo/cgrp.h +1 -0
@@ -50,6 +50,7 @@ struct nvkm_cgrp {#define NVKM_CGRP_RC_PENDING 1#define NVKM_CGRP_RC_RUNNING 2atomic_t rc; +atomic_t rc_engm;/* engines that faulted on the group, reset during its RC */ struct list_head head; };
drivers/gpu/drm/nouveau/nvkm/engine/fifo/gf100.c +9 -2
@@ -597,9 +597,16 @@ gf100_fifo_mmu_fault_recover(struct nvkm_fifo *fifo, struct nvkm_fault_data *inf info->reason, er ? er->name :"" , chan ? chan->id : -1, info->inst, chan ? chan->name : "unknown"); -/* Handle host/engine faults. */ -if (chan) +/* Handle host/engine faults. The engine that reported the fault must +* be reset even if it has already switched to another channel (its +* status then no longer points at the faulting channel), otherwise it +* stays stalled and the runlist update never completes. +*/ +if (chan) { +if (engine && engn->runl == chan->cgrp->runl && !WARN_ON (engn->id >=32 )) +atomic_or (BIT (engn->id), &chan->cgrp->rc_engm);nvkm_runl_rc_cgrp (chan->cgrp); + }nvkm_chan_put (&chan, flags); }
drivers/gpu/drm/nouveau/nvkm/engine/fifo/runl.c +12 -3
@@ -63,6 +63,7 @@ nvkm_runl_rc(struct nvkm_runl *runl)struct nvkm_engn *engn;unsigned long flags;int rc, state, i; +u32 engm =0 ;bool reset;/* Runlist is blocked before scheduling recovery - fetch count. */
@@ -75,6 +76,9 @@ nvkm_runl_rc(struct nvkm_runl *runl) nvkm_runl_foreach_cgrp_safe(cgrp, gtmp, runl) { state =atomic_cmpxchg (&cgrp->rc,NVKM_CGRP_RC_PENDING ,NVKM_CGRP_RC_RUNNING );if (state ==NVKM_CGRP_RC_PENDING ) { +/* Collect the engines that reported a fault for the group. */ + engm |=atomic_xchg (&cgrp->rc_engm,0 ); +/* Disable all channels in them, and remove from runlist. */ nvkm_cgrp_foreach_chan_safe(chan, ctmp, cgrp) {nvkm_chan_error (chan, false);
@@ -99,14 +103,19 @@ nvkm_runl_rc(struct nvkm_runl *runl)/* Look for engines that are still on flagged channel groups - reset them. */ nvkm_runl_foreach_engn_cond(engn, runl, engn->func->cxid) { +bool faulted = engn->id <32 && (engm &BIT (engn->id)); + cgrp =nvkm_engn_cgrp_get (engn, &flags); -if (!cgrp) { +if (!cgrp && !faulted) {ENGN_DEBUG (engn,"cxid not valid" );continue ; } - reset =atomic_read (&cgrp->rc) ==NVKM_CGRP_RC_RUNNING ; -nvkm_cgrp_put (&cgrp, flags); + reset = faulted; +if (cgrp) { + reset |=atomic_read (&cgrp->rc) ==NVKM_CGRP_RC_RUNNING ; +nvkm_cgrp_put (&cgrp, flags); + }if (!reset) {ENGN_DEBUG (engn,"cxid not in recovery" );continue ;
Signature · 1 line
2.53.0
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_tpkhdr(int subc, int mthd, unsigned size)
{
return 0x20000000 | (size << 16) | (subc << 13) | (mthd >> 2);
}/* Fermi FIFO packet header: non-incrementing methods. */ static uint32_tpkhdr_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 doublenow_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 voidround_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 voidgr_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);
}
}intmain(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);
}