Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] lib/test_hmm: don't allocate device memory chunks under mmap_lock
@ 2026-12-02  6:15 Andrey Mokrushin
  2026-12-02 23:07 ` Alistair Popple
  0 siblings, 1 reply; 2+ messages in thread
From: Andrey Mokrushin @ 2026-12-02  6:15 UTC (permalink / raw)
  To: Andrew Morton, Jason Gunthorpe, Leon Romanovsky
  Cc: linux-mm, linux-kernel, lvc-project

dmirror_migrate_to_device() holds mmap_read_lock() of the mirrored mm
and, when the device runs out of free pages, dmirror_devmem_alloc_page()
allocates a new chunk with memremap_pages(), which takes
cpu_hotplug_lock via mem_hotplug_begin(). This nests cpu_hotplug_lock
inside mmap_lock, while cpuset nests mmap_lock inside cpu_hotplug_lock
and cpuset_mutex (cgroup_attach_lock() -> cpuset_attach() ->
mpol_rebind_mm(), also cpuset_handle_hotplug() -> mpol_rebind_mm()).
Lockdep reports it while running hmm-tests after a task has been moved
into a cpuset with a different set of memory nodes:

  WARNING: possible circular locking dependency detected
  hmm-tests is trying to acquire lock:
   (&mdevice->devmem_lock), at: dmirror_allocate_chunk+0xc7/0x520 [test_hmm]
  but task is already holding lock:
   (&mm->mmap_lock), at: mmap_read_lock+0x36/0xc0 [test_hmm]

  Chain exists of:
    &mdevice->devmem_lock --> cpuset_mutex --> &mm->mmap_lock

The deadlock is real if a CPU hotplug writer is waiting on
cpu_hotplug_lock at the same time.

Don't allocate chunks from dmirror_devmem_alloc_page(). Report an empty
free list to dmirror_migrate_to_device() instead, which allocates the
missing chunk after dropping mmap_lock and retries the current range.
Pages that were already migrated are skipped on the retry, as they are
no longer system memory. When a huge page has to be split into base
pages, do it only if enough base pages are free, so the split is not
left half done.

Found by Linux Verification Center (linuxtesting.org).

Fixes: b2ef9f5a5cb3 ("mm/hmm/test: add selftest driver for HMM")
Signed-off-by: Andrey Mokrushin <a.mokrushin@ideco.ru>
---
The analysis of the lockdep report, the fix and the changelog were drafted
with an AI coding assistant (Claude Code); it was also used to run the
tests below. I reviewed the change, checked the results and take
responsibility for the patch.

Tested on mm-unstable 890dc639d8a in QEMU with 2 NUMA nodes and lockdep:
cpuset.mems of a cgroup holding a task is changed, then hmm-tests is run.
  without the patch: the circular locking report above, 2 of 2 runs;
  with the patch: no lockdep reports, 2 of 2 runs.
hmm-tests results are the same with and without the patch:
39 passed, 0 failed, 39 skipped.

 lib/test_hmm.c | 118 ++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 87 insertions(+), 31 deletions(-)

diff --git a/lib/test_hmm.c b/lib/test_hmm.c
index 893caab93ba..8d9f51443d2 100644
--- a/lib/test_hmm.c
+++ b/lib/test_hmm.c
@@ -661,7 +661,7 @@ static int dmirror_write(struct dmirror *dmirror, struct hmm_dmirror_cmd *cmd)
 }
 
 static int dmirror_allocate_chunk(struct dmirror_device *mdevice,
-				  struct page **ppage, bool is_large)
+				  bool is_large)
 {
 	struct dmirror_chunk *devmem;
 	struct resource *res = NULL;
@@ -757,29 +757,9 @@ static int dmirror_allocate_chunk(struct dmirror_device *mdevice,
 		pfn++;
 	}
 
-	ret = 0;
-	if (ppage) {
-		if (is_large) {
-			if (!mdevice->free_folios) {
-				ret = -ENOMEM;
-				goto err_unlock;
-			}
-			*ppage = folio_page(mdevice->free_folios, 0);
-			mdevice->free_folios = (*ppage)->zone_device_data;
-			mdevice->calloc += HPAGE_PMD_NR;
-		} else if (mdevice->free_pages) {
-			*ppage = mdevice->free_pages;
-			mdevice->free_pages = (*ppage)->zone_device_data;
-			mdevice->calloc++;
-		} else {
-			ret = -ENOMEM;
-			goto err_unlock;
-		}
-	}
-err_unlock:
 	spin_unlock(&mdevice->lock);
 
-	return ret;
+	return 0;
 
 err_release:
 	mutex_unlock(&mdevice->devmem_lock);
@@ -792,8 +772,42 @@ static int dmirror_allocate_chunk(struct dmirror_device *mdevice,
 	return ret;
 }
 
+#define DMIRROR_NEED_PAGES	BIT(0)
+#define DMIRROR_NEED_FOLIOS	BIT(1)
+
+static int dmirror_allocate_chunks(struct dmirror_device *mdevice,
+				   unsigned int need)
+{
+	int ret = 0;
+
+	if (need & DMIRROR_NEED_PAGES)
+		ret = dmirror_allocate_chunk(mdevice, false);
+	if (!ret && (need & DMIRROR_NEED_FOLIOS))
+		ret = dmirror_allocate_chunk(mdevice, true);
+	return ret;
+}
+
+static bool dmirror_has_free_pages(struct dmirror_device *mdevice,
+				   unsigned long nr)
+{
+	struct page *page;
+
+	spin_lock(&mdevice->lock);
+	for (page = mdevice->free_pages; page && nr; page = page->zone_device_data)
+		nr--;
+	spin_unlock(&mdevice->lock);
+	return !nr;
+}
+
+/*
+ * Called with the mmap_lock of the mirrored mm held, so it must not allocate
+ * a new chunk: memremap_pages() takes cpu_hotplug_lock, which nests outside
+ * of mmap_lock (e.g. cpuset_attach() -> mpol_rebind_mm()). If the free list
+ * is empty, *empty is set and the caller allocates a chunk after dropping
+ * mmap_lock.
+ */
 static struct page *dmirror_devmem_alloc_page(struct dmirror *dmirror,
-					      bool is_large)
+					      bool is_large, bool *empty)
 {
 	struct page *dpage = NULL;
 	struct page *rpage = NULL;
@@ -825,8 +839,8 @@ static struct page *dmirror_devmem_alloc_page(struct dmirror *dmirror,
 		spin_unlock(&mdevice->lock);
 	} else {
 		spin_unlock(&mdevice->lock);
-		if (dmirror_allocate_chunk(mdevice, &dpage, is_large))
-			goto error;
+		*empty = true;
+		goto error;
 	}
 
 	if (rpage && order)
@@ -845,7 +859,8 @@ static struct page *dmirror_devmem_alloc_page(struct dmirror *dmirror,
 }
 
 static void dmirror_migrate_alloc_and_copy(struct migrate_vma *args,
-					   struct dmirror *dmirror)
+					   struct dmirror *dmirror,
+					   unsigned int *need_chunk)
 {
 	const unsigned long *src = args->src;
 	unsigned long *dst = args->dst;
@@ -858,6 +873,7 @@ static void dmirror_migrate_alloc_and_copy(struct migrate_vma *args,
 		bool is_large = *src & MIGRATE_PFN_COMPOUND;
 		int write = (*src & MIGRATE_PFN_WRITE) ? MIGRATE_PFN_WRITE : 0;
 		unsigned long nr = 1;
+		bool fail_alloc = false;
 
 		if (!(*src & MIGRATE_PFN_MIGRATE))
 			goto next;
@@ -874,9 +890,20 @@ static void dmirror_migrate_alloc_and_copy(struct migrate_vma *args,
 
 		if (dmirror->flags & HMM_DMIRROR_FLAG_FAIL_ALLOC) {
 			dmirror->flags &= ~HMM_DMIRROR_FLAG_FAIL_ALLOC;
+			fail_alloc = true;
 			dpage = NULL;
-		} else
-			dpage = dmirror_devmem_alloc_page(dmirror, is_large);
+		} else {
+			bool empty = false;
+
+			dpage = dmirror_devmem_alloc_page(dmirror, is_large,
+							  &empty);
+			if (empty) {
+				/* Retried after a new chunk is allocated */
+				*need_chunk |= is_large ? DMIRROR_NEED_FOLIOS :
+							  DMIRROR_NEED_PAGES;
+				goto next;
+			}
+		}
 
 		if (!dpage) {
 			struct folio *folio;
@@ -894,8 +921,22 @@ static void dmirror_migrate_alloc_and_copy(struct migrate_vma *args,
 				nr = folio_nr_pages(folio);
 			}
 
+			/* Split into base pages only if all of them are free */
+			if (!dmirror_has_free_pages(dmirror->mdevice, nr)) {
+				*need_chunk |= DMIRROR_NEED_PAGES;
+				if (fail_alloc)
+					dmirror->flags |= HMM_DMIRROR_FLAG_FAIL_ALLOC;
+				goto next;
+			}
+
 			for (i = 0; i < nr && addr < args->end; i++) {
-				dpage = dmirror_devmem_alloc_page(dmirror, false);
+				bool empty = false;
+
+				dpage = dmirror_devmem_alloc_page(dmirror, false,
+								  &empty);
+				/* Only if another thread raced for the pages */
+				if (!dpage)
+					break;
 				rpage = BACKING_PAGE(dpage);
 				rpage->zone_device_data = dmirror;
 
@@ -1307,6 +1348,7 @@ static int dmirror_migrate_to_device(struct dmirror *dmirror,
 	int ret;
 	unsigned long *src_pfns = NULL;
 	unsigned long *dst_pfns = NULL;
+	unsigned int need_chunk;
 
 	start = cmd->addr;
 	end = start + size;
@@ -1347,10 +1389,24 @@ static int dmirror_migrate_to_device(struct dmirror *dmirror,
 			goto out;
 
 		pr_debug("Migrating from sys mem to device mem\n");
-		dmirror_migrate_alloc_and_copy(&args, dmirror);
+		need_chunk = 0;
+		dmirror_migrate_alloc_and_copy(&args, dmirror, &need_chunk);
 		migrate_vma_pages(&args);
 		dmirror_migrate_finalize_and_map(&args, dmirror);
 		migrate_vma_finalize(&args);
+
+		/*
+		 * Out of free device pages: allocate a chunk without holding
+		 * mmap_lock and retry the rest of this range.
+		 */
+		if (need_chunk) {
+			mmap_read_unlock(mm);
+			if (dmirror_allocate_chunks(dmirror->mdevice, need_chunk))
+				need_chunk = 0;
+			mmap_read_lock(mm);
+			if (need_chunk)
+				next = addr;
+		}
 	}
 	mmap_read_unlock(mm);
 	mmput(mm);
@@ -1891,7 +1947,7 @@ static int dmirror_device_init(struct dmirror_device *mdevice, int id)
 		goto put_device;
 
 	/* Build a list of free ZONE_DEVICE struct pages */
-	ret = dmirror_allocate_chunk(mdevice, NULL, false);
+	ret = dmirror_allocate_chunk(mdevice, false);
 	if (ret)
 		goto put_device;
 
-- 
2.43.0




^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-02 23:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02  6:15 [PATCH] lib/test_hmm: don't allocate device memory chunks under mmap_lock Andrey Mokrushin
2026-12-02 23:07 ` Alistair Popple

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox