Skip to contentSkip to conversation

mm/migrate_device: consolidate compound folio handling

PATCH Superseded Hui Su 2 participants 3 messages Sep 11 – Sep 12

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Hui Su

author
#0 lore raw

PATCH 1/1 mm/migrate_device: consolidate compound folio handling

Commit dc41e961a269 ("mm/migrate_device: avoid out-of-bounds writes for compound folios") added handling for compound folios that do not fit in the remaining PFN array.

migrate_device_range() and migrate_device_pfns() duplicate the logic for locking device PFNs, encoding compound folios, and handling this boundary condition.

A compound folio cannot be represented partially for migration. Warn when one does not fit in the remaining PFN array, while retaining the existing defensive handling: release any lock and reference acquired for the current folio, clear the remaining entries, and stop collecting.

Move the shared collection and encoding logic into a helper so both interfaces handle compound folios consistently. Also use memset() for the compound-folio tail entries instead of open-coding the clearing loop.

Document that an encountered compound folio must fit entirely in the remaining range or PFN array.

---
Tested on x86_64 with KASAN enabled:

- Built mm/migrate_device.o and the kernel successfully.
- Ran the HMM migrate_anon_huge_zero selftest; the private-device case passed.
- No KASAN report or truncated compound-folio WARN was observed.

 mm/migrate_device.c | 90 ++++++++++++++++++++++++---------------------
 1 file changed, 49 insertions(+), 41 deletions(-)
mm/migrate_device.c +49 -41
index 009bfa8b212d..c57eadea6565 100644
@@ -1392,6 +1392,39 @@ static unsigned long migrate_device_pfn_lock(unsigned long pfn)
 	return migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
 }
 
+/*
+ * Collect a device folio into the page-granular PFN array.
+ *
+ * Return the number of entries consumed, or 0 if the folio does not fit in
+ * the remaining array.
+ */
+static unsigned int migrate_device_collect_folio(unsigned long *src_pfn,
+						 unsigned long pfn,
+						 unsigned long remaining)
+{
+	struct folio *folio = page_folio(pfn_to_page(pfn));
+	unsigned int nr;
+
+	*src_pfn = migrate_device_pfn_lock(pfn);
+	nr = folio_nr_pages(folio);
+
+	if (WARN_ON_ONCE(nr > remaining)) {
+		if (*src_pfn & MIGRATE_PFN_MIGRATE) {
+			folio_unlock(folio);
+			folio_put(folio);
+		}
+		memset(src_pfn, 0, remaining * sizeof(*src_pfn));
+		return 0;
+	}
+
+	if (nr > 1) {
+		*src_pfn |= MIGRATE_PFN_COMPOUND;
+		memset(src_pfn + 1, 0, (nr - 1) * sizeof(*src_pfn));
+	}
+
+	return nr;
+}
+
 /**
  * migrate_device_range() - migrate device private pfns to normal memory.
  * @src_pfns: array large enough to hold migrating source device private pfns.
@@ -1410,35 +1443,22 @@ static unsigned long migrate_device_pfn_lock(unsigned long pfn)
  * migrating pages that aren't free before unmapping them. Drivers may then
  * allocate destination pages and start copying data from the device to CPU
  * memory before calling migrate_device_pages().
+ *
+ * A compound folio must fit entirely in the remaining range.
  */
 int migrate_device_range(unsigned long *src_pfns, unsigned long start,
 			unsigned long npages)
 {
-	unsigned long i, j, pfn;
+	unsigned long i, pfn;
 
 	for (pfn = start, i = 0; i < npages; pfn++, i++) {
-		struct page *page = pfn_to_page(pfn);
-		struct folio *folio = page_folio(page);
-		unsigned int nr = 1;
+		unsigned int nr;
 
-		src_pfns[i] = migrate_device_pfn_lock(pfn);
-		nr = folio_nr_pages(folio);
-		if (nr > npages - i) {
-			if (src_pfns[i] & MIGRATE_PFN_MIGRATE) {
-				folio_unlock(folio);
-				folio_put(folio);
-			}
-			memset(&src_pfns[i], 0,
-			       (npages - i) * sizeof(*src_pfns));
+		nr = migrate_device_collect_folio(&src_pfns[i], pfn, npages - i);
+		if (!nr)
 			break;
-		}
-		if (nr > 1) {
-			src_pfns[i] |= MIGRATE_PFN_COMPOUND;
-			for (j = 1; j < nr; j++)
-				src_pfns[i+j] = 0;
-			i += j - 1;
-			pfn += j - 1;
-		}
+		i += nr - 1;
+		pfn += nr - 1;
 	}
 
 	migrate_device_unmap(src_pfns, npages, NULL);
@@ -1454,33 +1474,21 @@ EXPORT_SYMBOL(migrate_device_range);
  *
  * Similar to migrate_device_range() but supports non-contiguous pre-populated
  * array of device pages to migrate.
+ *
+ * A compound folio must fit entirely in the remaining PFN array.
  */
 int migrate_device_pfns(unsigned long *src_pfns, unsigned long npages)
 {
-	unsigned long i, j;
+	unsigned long i;
 
 	for (i = 0; i < npages; i++) {
-		struct page *page = pfn_to_page(src_pfns[i]);
-		struct folio *folio = page_folio(page);
-		unsigned int nr = 1;
+		unsigned long pfn = src_pfns[i];
+		unsigned int nr;
 
-		src_pfns[i] = migrate_device_pfn_lock(src_pfns[i]);
-		nr = folio_nr_pages(folio);
-		if (nr > npages - i) {
-			if (src_pfns[i] & MIGRATE_PFN_MIGRATE) {
-				folio_unlock(folio);
-				folio_put(folio);
-			}
-			memset(&src_pfns[i], 0,
-			       (npages - i) * sizeof(*src_pfns));
+		nr = migrate_device_collect_folio(&src_pfns[i], pfn, npages - i);
+		if (!nr)
 			break;
-		}
-		if (nr > 1) {
-			src_pfns[i] |= MIGRATE_PFN_COMPOUND;
-			for (j = 1; j < nr; j++)
-				src_pfns[i+j] = 0;
-			i += j - 1;
-		}
+		i += nr - 1;
 	}
 
 	migrate_device_unmap(src_pfns, npages, NULL);
Signature · 1 line
2.55.0
#1 lore raw
Quoted 41 lines · Hui Su

Commit dc41e961a269 ("mm/migrate_device: avoid out-of-bounds writes for compound folios") added handling for compound folios that do not fit in the remaining PFN array.

migrate_device_range() and migrate_device_pfns() duplicate the logic for locking device PFNs, encoding compound folios, and handling this boundary condition.

A compound folio cannot be represented partially for migration. Warn when one does not fit in the remaining PFN array, while retaining the existing defensive handling: release any lock and reference acquired for the current folio, clear the remaining entries, and stop collecting.

Move the shared collection and encoding logic into a helper so both interfaces handle compound folios consistently. Also use memset() for the compound-folio tail entries instead of open-coding the clearing loop.

Document that an encountered compound folio must fit entirely in the remaining range or PFN array.

---
Tested on x86_64 with KASAN enabled:

- Built mm/migrate_device.o and the kernel successfully.
- Ran the HMM migrate_anon_huge_zero selftest; the private-device case passed.
- No KASAN report or truncated compound-folio WARN was observed.

 mm/migrate_device.c | 90 ++++++++++++++++++++++++---------------------
 1 file changed, 49 insertions(+), 41 deletions(-)
mm/migrate_device.c +0 -0
index 009bfa8b212d..c57eadea6565 100644
@@ -1392,6 +1392,39 @@ static unsigned long migrate_device_pfn_lock(unsigned long pfn)
 	return migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
 }

mm/migrate_device.c static unsigned long migrate_device_pfn_lock(unsigned long pfn) · line 1403

Quoted 10 lines
mm/migrate_device.c +9 -0
 
+/*
+ * Collect a device folio into the page-granular PFN array.
+ *
+ * Return the number of entries consumed, or 0 if the folio does not fit in
+ * the remaining array.
+ */
+static unsigned int migrate_device_collect_folio(unsigned long *src_pfn,
+						 unsigned long pfn,
+						 unsigned long remaining)

Two tab indent please.

mm/migrate_device.c static unsigned long migrate_device_pfn_lock(unsigned long pfn) · line 1411

Quoted 8 lines
mm/migrate_device.c +8 -0
+{
+	struct folio *folio = page_folio(pfn_to_page(pfn));
+	unsigned int nr;
+
+	*src_pfn = migrate_device_pfn_lock(pfn);
+	nr = folio_nr_pages(folio);
+
+	if (WARN_ON_ONCE(nr > remaining)) {

Just to be sure: there is no way we can currently validly trigger this, right?

Quoted 5 lines
mm/migrate_device.c +5 -0
+		if (*src_pfn & MIGRATE_PFN_MIGRATE) {
+			folio_unlock(folio);
+			folio_put(folio);
+		}
+		memset(src_pfn, 0, remaining * sizeof(*src_pfn));

mm/migrate_device.c static unsigned long migrate_device_pfn_lock(unsigned long pfn) · line 1426

Quoted 10 lines
mm/migrate_device.c +10 -0
+		return 0;
+	}
+
+	if (nr > 1) {
+		*src_pfn |= MIGRATE_PFN_COMPOUND;
+		memset(src_pfn + 1, 0, (nr - 1) * sizeof(*src_pfn));
+	}
+
+	return nr;
+}

Looks much cleaner.

Signature · 3 lines
Cheers,

David
author
#2 lore raw

Hi David,

Quoted 3 lines

Two tab indent please.

Just to be sure: there is no way we can currently validly trigger this, right?

Correct, as far as I can tell. I checked the current in-tree callers and do not see a valid path that can trigger this condition.

The migrate_device_range() callers evict complete device-memory chunks, and the migrate_device_pfns() caller expects large source folios to be represented whole. In these cases, the supplied range or PFN array is large enough to contain each encountered compound folio completely, which is required because a compound folio cannot be partially migrated.

The condition is still reachable with invalid, truncated input. For example, on a 4 KiB base-page system, passing only one entry for the head of a 2 MiB device compound folio would leave one entry remaining while folio_nr_pages() is 512. That would be caller misuse, and is the invariant checked by the WARN_ON_ONCE().

I reran the original HMM migrate_anon_huge_zero reproducer on current mainline. The private-device case passes, the coherent-device case is skipped because DEVICE_COHERENT is unavailable in my test configuration, and the test exits with status 0. I also instrumented the truncated compound-folio condition; it was not hit, and no KASAN report was observed.

I'll fix the helper parameter indentation and resend the patch.

Thanks,
Hui