Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: mpenttil@redhat.com
To: linux-mm@kvack.org
Cc: dri-devel@lists.freedesktop.org, intel-xe@lists.freedesktop.org,
	linux-kernel@vger.kernel.org,
	"Mika Penttilä" <mpenttil@redhat.com>,
	"David Hildenbrand" <david@kernel.org>,
	"Jason Gunthorpe" <jgg@nvidia.com>,
	"Leon Romanovsky" <leonro@nvidia.com>,
	"Alistair Popple" <apopple@nvidia.com>,
	"Balbir Singh" <balbirs@nvidia.com>, "Zi Yan" <ziy@nvidia.com>,
	"Matthew Brost" <matthew.brost@intel.com>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Lorenzo Stoakes" <ljs@kernel.org>,
	"Liam R. Howlett" <liam@infradead.org>,
	"Vlastimil Babka" <vbabka@kernel.org>,
	"Mike Rapoport" <rppt@kernel.org>,
	"Michal Hocko" <mhocko@suse.com>, "Lyude Paul" <lyude@redhat.com>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Shuah Khan" <shuah@kernel.org>,
	"Madhavan Srinivasan" <maddy@linux.ibm.com>
Subject: [PATCH v18 13/14] drm/nouveau/dmem: commit migrate_to_ram() only after the copy is confirmed
Date: Tue,  1 Dec 2026 06:55:30 +0200	[thread overview]
Message-ID: <20261201045531.3622535-14-mpenttil@redhat.com> (raw)
In-Reply-To: <20261201045531.3622535-1-mpenttil@redhat.com>

From: Mika Penttilä <mpenttil@redhat.com>

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ä <mpenttil@redhat.com>
---
 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 <linux/hmm.h>
 #include <linux/memremap.h>
 #include <linux/migrate.h>
+#include <linux/jiffies.h>
 
 /*
  * 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



  parent reply	other threads:[~2026-12-01  4:57 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  4:55 [PATCH v18 00/14] migrate on fault for device pages mpenttil
2026-12-01  4:55 ` [PATCH v18 01/14] mm/Kconfig: changes for " mpenttil
2026-12-01  4:55 ` [PATCH v18 02/14] mm: add helper to convert HMM pfn to migrate pfn mpenttil
2026-12-01  4:55 ` [PATCH v18 03/14] mm/hmm: preparations for HMM to participate in migration mpenttil
2026-12-01  4:55 ` [PATCH v18 04/14] mm/hmm: do the plumbing " mpenttil
2026-12-01  4:55 ` [PATCH v18 05/14] mm/hmm: implement rollback for device page migration in HMM pagewalk mpenttil
2026-12-01  4:55 ` [PATCH v18 06/14] mm/hmm: migrate collection in HMM pagewalk - pte level mpenttil
2026-12-01  4:55 ` [PATCH v18 07/14] mm/hmm: migrate collection in HMM pagewalk - pmd level mpenttil
2026-12-01  4:55 ` [PATCH v18 08/14] mm/hmm: add lazy MMU mode support for migration in HMM pagewalk mpenttil
2026-12-01  4:55 ` [PATCH v18 09/14] mm: enable device page migration from " mpenttil
2026-12-01  4:55 ` [PATCH v18 10/14] lib/test_hmm: add a new testcase for the migrate on fault mpenttil
2026-12-01  4:55 ` [PATCH v18 11/14] Documentation/mm/hmm: document migration through hmm_range_fault() mpenttil
2026-12-01  4:55 ` [PATCH v18 12/14] drm/nouveau/dmem: serialize the copy channel users mpenttil
2026-12-01  4:55 ` mpenttil [this message]
2026-12-01  4:55 ` [PATCH v18 14/14] drm/nouveau/svm: add support for migrate on fault mpenttil

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201045531.3622535-14-mpenttil@redhat.com \
    --to=mpenttil@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=apopple@nvidia.com \
    --cc=balbirs@nvidia.com \
    --cc=corbet@lwn.net \
    --cc=dakr@kernel.org \
    --cc=david@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=jgg@nvidia.com \
    --cc=leonro@nvidia.com \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=lyude@redhat.com \
    --cc=maddy@linux.ibm.com \
    --cc=matthew.brost@intel.com \
    --cc=mhocko@suse.com \
    --cc=rppt@kernel.org \
    --cc=shuah@kernel.org \
    --cc=vbabka@kernel.org \
    --cc=ziy@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox