Skip to contentSkip to conversation

lib/test_hmm: don't allocate device memory chunks under mmap_lock

PATCH In review Andrey Mokrushin 3 participants 3 messages Dec 2 – Dec 3

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/test_hmm: don't allocate device memory chunks under mmap_lock

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")

---
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(-)
lib/test_hmm.c +87 -31
index 893caab93ba..8d9f51443d2 100644
@@ -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;
 
Signature · 1 line
2.43.0
#1 lore raw

On 2026-12-02 at 17:15 +1100, Andrey Mokrushin <[email protected]> wrote...

Quoted 32 lines

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).

Quoted 7 lines
Fixes b2ef9f5a5cb3 ("mm/hmm/test: add selftest driver for HMM")

---
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.

It will take me a while to review the fix but the deadlock sounds plausible. From memory Nouveau also copied a similar pattern from here, have you (or your agent) looked at that to see if it suffers the same issue?

Thanks.

- Alistair

Quoted 222 lines

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(-)
lib/test_hmm.c +87 -31
index 893caab93ba..8d9f51443d2 100644
@@ -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;
 
Signature · 1 line
2.43.0
#2 lore raw
In reply to Andrey Mokrushin
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] mm/hmm/test: broken base-page fallback in dmirror_migrate_alloc_and_copy() - [Medium] mm/hmm/test…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] mm/hmm/test: broken base-page fallback in dmirror_migrate_alloc_and_copy()
- [Medium] mm/hmm/test: partial split crash or loop in dmirror_migrate_alloc_and_copy()
- [Medium] mm/hmm/test: unbounded chunk allocation loop in dmirror_migrate_to_device()
--

commit acf453213f1810f4cf9d9256c95b4bd7dcb938c3
Author: Andrey Mokrushin <[email protected]>

lib/test_hmm: don't allocate device memory chunks under mmap_lock

This commit moves device memory chunk allocation out of dmirror_devmem_alloc_page() so that memremap_pages() is not called while holding mmap_lock, instead retrying the range in dmirror_migrate_to_device() after dropping mmap_lock and allocating the needed chunks.

Quoted 4 lines
lib/test_hmm.c +0 -0
index 893caab93bac2..8d9f51443d209 100644

[ ... ]

Quoted 18 lines
lib/test_hmm.c +0 -14
@@ -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++;

lib/test_hmm.c static int dmirror_allocate_chunk(struct dmirror_device *mdevice, · line 762

Quoted 10 lines
lib/test_hmm.c +1 -7
-		} else {
-			ret = -ENOMEM;
-			goto err_unlock;
-		}
-	}
-err_unlock:
 	spin_unlock(&mdevice->lock);
 
-	return ret;
+	return 0;

[Severity: Medium]
Can removing the !mdevice->free_folios check when is_large is true cause dmirror_migrate_to_device() to allocate 256 MB chunks in an unbounded loop?

On architectures where HPAGE_PMD_SIZE exceeds DEVMEM_CHUNK_SIZE (such as 64 KB base pages where HPAGE_PMD_SIZE is 512 MB and DEVMEM_CHUNK_SIZE is 256 MB), pfn + HPAGE_PMD_NR <= pfn_last is never true, so dmirror_allocate_chunk(mdevice, true) puts every page onto mdevice->free_pages, leaves mdevice->free_folios empty, and returns 0. dmirror_migrate_to_device() then sets next = addr and retries the same range until memory or iomem_resource is exhausted.

[ ... ]

Quoted 10 lines
lib/test_hmm.c +3 -2
@@ -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;

lib/test_hmm.c static void dmirror_migrate_alloc_and_copy(struct migrate_vma *args, · line 906

Quoted 10 lines
lib/test_hmm.c +10 -0
+
+			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;
+			}
+		}

[Severity: Medium]
Does jumping to next when is_large is true and mdevice->free_folios is empty break the fallback that splits a compound migration into base pages from mdevice->free_pages?

dmirror_device_init() allocates the initial chunk with is_large = false, leaving mdevice->free_folios empty. On HMM_DMIRROR_MEMORY_DEVICE_COHERENT devices, dmirror_allocate_chunk() uses a fixed physical address range that is already mapped by that initial chunk, so a later dmirror_allocate_chunk(mdevice, true) call always fails. Before this change, that failure returned NULL from dmirror_devmem_alloc_page() and fell through to the if (!dpage) block to allocate order-0 pages from mdevice->free_pages; now goto next skips the split, and when dmirror_allocate_chunks() fails in dmirror_migrate_to_device(), the compound page is left unmigrated and dmirror_do_read() fails with -ENOENT.

Quoted 14 lines
lib/test_hmm.c +7 -0
 		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;
+			}

lib/test_hmm.c static void dmirror_migrate_alloc_and_copy(struct migrate_vma *args, · line 939

Quoted 10 lines
lib/test_hmm.c +8 -1
+
 			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;

[Severity: Medium]
What happens if dmirror_devmem_alloc_page() fails partway through this split loop (either because another thread took pages from mdevice->free_pages after dmirror_has_free_pages() dropped mdevice->lock, or because alloc_pages() failed)?

If it fails at i > 0, dst[0 .. i-1] remain populated without MIGRATE_PFN_COMPOUND while dst[i .. nr-1] remain 0; when spage is NULL (an unbacked compound entry), __migrate_device_pages() then calls migrate_vma_insert_page() across all nr subpages and dereferences NULL in page_folio(migrate_pfn_to_page(*dst)) at entry i:

mm/migrate_device.c:__migrate_device_pages() {
    ...
			if ((src_pfns[i] & MIGRATE_PFN_COMPOUND) &&
				(!(dst_pfns[i] & MIGRATE_PFN_COMPOUND))) {
				nr = migrate_vma_nr_pages(&src_pfns[i]);
				src_pfns[i] &= ~MIGRATE_PFN_COMPOUND;
			} else {
				nr = 1;
			}

			for (j = 0; j < nr && i + j < npages; j++) {
				src_pfns[i+j] |= MIGRATE_PFN_MIGRATE;
				migrate_vma_insert_page(migrate,
					addr + j * PAGE_SIZE,
					&dst_pfns[i+j], &src_pfns[i+j]);
			}
    ...
}

If it fails at i == 0, break exits the inner loop before src, dst and addr are incremented, and the following continue re-runs the outer loop at the same address without setting *need_chunk.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1