From: Mika Penttilä nouveau_dmem_migrate_to_ram() ignores the nouveau_fence_new() result, commits the migration with migrate_vma_pages() before the copy has completed, and then uses the lazy, interrupt driven fence wait, whose completion interrupt can be missed. A failed fence allocation leaves the fence uninitialized, and a copy that doesn't complete in time leaves the process with a page that wasn't fully copied. Check the fence, poll it a jiffy at a time with a deadline (a tighter interval was found to regress to the timeout on hardware), and commit the migration only once the copy is confirmed: a fence signaled with an error, as a channel kill does, doesn't count. Otherwise abort it: finalize restores the device private pte with the data intact, and the fault returns SIGBUS (VM_FAULT_OOM if the fence couldn't be allocated). Letting the CPU fault again instead would, with a wedged copy engine, queue another copy and leak another page on every retry. The destination page and its DMA mapping may still be in use by the copy engine, so they are leaked rather than freed. That is rare, as the copy didn't complete within the deadline. The early returns after allocating the src/dst arrays now free them too. A GPU access to the page while the copy runs faults, and the GPU fault worker waits for this migration before it can replay that fault. A copy was seen on hardware not to progress until the faulting channel was replayed, so this can hold up the copy. The deadline covers that case as well. Fixes: c32287471077 ("gpu/drm/nouveau: enable THP support for GPU memory migration") Fixes: bfe69ef94aee ("nouveau: simplify nouveau_dmem_migrate_to_ram") Signed-off-by: Mika Penttilä --- drivers/gpu/drm/nouveau/nouveau_dmem.c | 109 +++++++++++++++++++++++-- 1 file changed, 100 insertions(+), 9 deletions(-) diff --git a/drivers/gpu/drm/nouveau/nouveau_dmem.c b/drivers/gpu/drm/nouveau/nouveau_dmem.c index 6824f5401d00..36c44d3a3c8f 100644 --- a/drivers/gpu/drm/nouveau/nouveau_dmem.c +++ b/drivers/gpu/drm/nouveau/nouveau_dmem.c @@ -40,6 +40,7 @@ #include #include #include +#include /* * FIXME: this is ugly right now we are using TTM to allocate vram and we pin @@ -213,6 +214,70 @@ static void nouveau_dmem_fence_done(struct nouveau_fence **fence) } } +/* + * Poll the copy fence instead of the lazy (interrupt-driven) wait since + * completion IRQ is randomly missed, hitting the timeout. Poll gently, a + * jiffy at a time: a tighter interval (usleep_range()) polls the fence far + * more often and was found on hardware to regress to the timeout. A CPU + * fault on device memory thus takes at least a jiffy. The deadline is + * well beyond any copy time and only catches a copy that won't complete. + * + * A fence signaled with an error, as a channel kill does to its pending + * fences, doesn't confirm the copy. + * + * Returns 0 on completion, -EBUSY on timeout, -EIO if the fence signaled + * with an error. Consumes the fence reference. + */ +static int nouveau_dmem_fence_poll(struct nouveau_fence **fence) +{ + unsigned long deadline = jiffies + 30 * HZ; + int ret = 0; + + while (!nouveau_fence_done(*fence)) { + if (time_after(jiffies, deadline)) { + ret = -EBUSY; + break; + } + schedule_timeout_uninterruptible(msecs_to_jiffies(1)); + } + if (!ret && dma_fence_get_status(&(*fence)->base) < 0) + ret = -EIO; + nouveau_fence_unref(fence); + + return ret; +} + +/* + * The copy could not be confirmed (no fence, timeout, or a fence signaled with + * an error): don't migrate, so finalize restores the original ptes and the + * source stays valid. The copy engine may still be writing the destination, + * and there is no telling when it stops, so keep the destination folios: take + * them out of dst[], so that migrate_vma_pages() doesn't migrate those entries + * and finalize doesn't free them, and keep their allocation reference. The + * caller skips the DMA unmap. Only the destination is written by the copy; + * the source is restored and stays in use. + * + * This is rare: the copy didn't complete within the deadline, because the + * engine is wedged or held up. Leaking is the safe choice then. The leaked + * destination stays allocated with its DMA mapping; a VRAM destination keeps + * its chunk in use, so device teardown can't complete afterwards. + */ +static void nouveau_dmem_migrate_abort(struct nouveau_drm *drm, + struct migrate_vma *args) +{ + unsigned long i; + + for (i = 0; i < args->npages; i++) { + struct page *dpage = migrate_pfn_to_page(args->dst[i]); + + if (dpage) { + folio_unlock(page_folio(dpage)); + args->dst[i] = 0; + } + } + NV_ERROR(drm, "svm: copy not confirmed, migration aborted, pages leaked\n"); +} + static int nouveau_dmem_copy_folio(struct nouveau_drm *drm, struct folio *sfolio, struct folio *dfolio, struct nouveau_dmem_dma_info *dma_info) @@ -244,11 +309,11 @@ static int nouveau_dmem_copy_folio(struct nouveau_drm *drm, static vm_fault_t nouveau_dmem_migrate_to_ram(struct vm_fault *vmf) { struct nouveau_drm *drm = page_to_drm(vmf->page); - struct nouveau_fence *fence; + struct nouveau_fence *fence = NULL; struct nouveau_svmm *svmm; struct page *dpage; vm_fault_t ret = 0; - int err; + int err, fret = 0; struct migrate_vma args = { .vma = vmf->vma, .pgmap_owner = drm->dev, @@ -293,10 +358,12 @@ static vm_fault_t nouveau_dmem_migrate_to_ram(struct vm_fault *vmf) * than just one page on CPU fault. When such fault happens it is very * likely that more surrounding page will CPU fault too. */ - if (migrate_vma_setup(&args) < 0) - return VM_FAULT_SIGBUS; + if (migrate_vma_setup(&args) < 0) { + ret = VM_FAULT_SIGBUS; + goto err; + } if (!args.cpages) - return 0; + goto done; if (order) dpage = folio_page(vma_alloc_folio(GFP_HIGHUSER | __GFP_ZERO, @@ -324,11 +391,35 @@ static vm_fault_t nouveau_dmem_migrate_to_ram(struct vm_fault *vmf) goto done; } - nouveau_dmem_fence_new(drm, &fence); + /* + * Commit the migration only once the copy is confirmed. Without a + * fence the copy can't be confirmed: treat it as failed. + */ + fret = nouveau_dmem_fence_new(drm, &fence); + /* + * A GPU access to this page while the copy runs faults, and the GPU + * fault worker waits for this migration before it can replay that + * fault. A copy was seen on hardware not to progress until the + * faulting channel was replayed, so this can hold up the copy. The + * deadline then aborts the migration instead of committing an + * unconfirmed copy. + */ + if (!fret) + fret = nouveau_dmem_fence_poll(&fence); + if (fret) { + nouveau_dmem_migrate_abort(drm, &args); + /* + * finalize restores the device private pte and the data is + * intact, but don't let the CPU fault again: with the copy + * engine wedged, every refault would queue another copy and + * leak another destination page. + */ + ret = fret == -ENOMEM ? VM_FAULT_OOM : VM_FAULT_SIGBUS; + } else { + dma_unmap_page(drm->dev->dev, dma_info.dma_addr, dma_info.size, + DMA_BIDIRECTIONAL); + } migrate_vma_pages(&args); - nouveau_dmem_fence_done(&fence); - dma_unmap_page(drm->dev->dev, dma_info.dma_addr, dma_info.size, - DMA_BIDIRECTIONAL); done: migrate_vma_finalize(&args); err: -- 2.55.0