Skip to contentSkip to conversation

mm/migrate_device: use locked folio in migrate_device_{range,pfns}()

PATCH In review Elkin Cruz 2 participants 4 messages Nov 25 – Nov 26

Subsystems (3)
lore
Share & tools
lore

Elkin Cruz

author
#0 lore raw

PATCH 1/1 mm/migrate_device: use locked folio in migrate_device_{range,pfns}()

migrate_device_range() and migrate_device_pfns() read a speculative folio pointer via page_folio() prior to taking a reference or lock in migrate_device_pfn_lock(). If the page splits or changes compound state before lock acquisition, folio_nr_pages(folio) inspects stale state and the nr > npages - i error path calls unpin_anon_rmap(), folio_unlock(), and folio_put() on the unowned speculative pointer while leaking the reference and lock on the folio actually locked by migrate_device_pfn_lock().

Return the referenced, locked folio directly from migrate_device_pfn_lock() and skip unlocked entries cleanly.

Fixes dc41e961a269 ("mm/migrate_device: avoid out-of-bounds writes for compound folios")
Fixes a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")

---

I am trying out new Clang static analysis checks for kernel bug classes, and they flagged this code as possibly buggy. The findings were triaged/fixed and commit descriptions were drafted with help of AI, then checked against linux-next (not runtime-tested on hardware).

Thanks,
Elkin

 mm/migrate_device.c | 41 ++++++++++++++++++++++-------------------
 1 file changed, 22 insertions(+), 19 deletions(-)
mm/migrate_device.c +22 -19
index a953e7acbea6..a599d5baa340 100644
@@ -1416,11 +1416,13 @@ void migrate_vma_finalize(struct migrate_vma *migrate)
 }
 EXPORT_SYMBOL(migrate_vma_finalize);
 
-static unsigned long migrate_device_pfn_lock(unsigned long pfn)
+static unsigned long migrate_device_pfn_lock(unsigned long pfn,
+					     struct folio **foliop)
 {
 	unsigned long src_pfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
 	struct folio *folio;
 
+	*foliop = NULL;
 	folio = folio_get_nontail_page(pfn_to_page(pfn));
 	if (!folio)
 		return 0;
@@ -1436,6 +1438,7 @@ static unsigned long migrate_device_pfn_lock(unsigned long pfn)
 		return 0;
 	}
 
+	*foliop = folio;
 	return src_pfn;
 }
 
@@ -1464,18 +1467,18 @@ int migrate_device_range(unsigned long *src_pfns, unsigned long start,
 	unsigned long i, j, 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;
+		struct folio *folio;
+		unsigned int nr;
+
+		src_pfns[i] = migrate_device_pfn_lock(pfn, &folio);
+		if (!folio)
+			continue;
 
-		src_pfns[i] = migrate_device_pfn_lock(pfn);
 		nr = folio_nr_pages(folio);
 		if (nr > npages - i) {
-			if (src_pfns[i] & MIGRATE_PFN_MIGRATE) {
-				unpin_anon_rmap(folio, src_pfns[i]);
-				folio_unlock(folio);
-				folio_put(folio);
-			}
+			unpin_anon_rmap(folio, src_pfns[i]);
+			folio_unlock(folio);
+			folio_put(folio);
 			memset(&src_pfns[i], 0,
 			       (npages - i) * sizeof(*src_pfns));
 			break;
@@ -1508,18 +1511,18 @@ int migrate_device_pfns(unsigned long *src_pfns, unsigned long npages)
 	unsigned long i, j;
 
 	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;
+		struct folio *folio;
+		unsigned int nr;
+
+		src_pfns[i] = migrate_device_pfn_lock(src_pfns[i], &folio);
+		if (!folio)
+			continue;
 
-		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) {
-				unpin_anon_rmap(folio, src_pfns[i]);
-				folio_unlock(folio);
-				folio_put(folio);
-			}
+			unpin_anon_rmap(folio, src_pfns[i]);
+			folio_unlock(folio);
+			folio_put(folio);
 			memset(&src_pfns[i], 0,
 			       (npages - i) * sizeof(*src_pfns));
 			break;
Signature · 1 line
2.56.1.839.g22a8290db9-goog
#1 lore raw
Quoted 16 lines · Elkin Cruz

migrate_device_range() and migrate_device_pfns() read a speculative folio pointer via page_folio() prior to taking a reference or lock in migrate_device_pfn_lock(). If the page splits or changes compound state before lock acquisition, folio_nr_pages(folio) inspects stale state and the nr > npages - i error path calls unpin_anon_rmap(), folio_unlock(), and folio_put() on the unowned speculative pointer while leaking the reference and lock on the folio actually locked by migrate_device_pfn_lock().

Return the referenced, locked folio directly from migrate_device_pfn_lock() and skip unlocked entries cleanly.

Fixes dc41e961a269 ("mm/migrate_device: avoid out-of-bounds writes for compound folios")
Fixes a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")
Quoted 6 lines

---

I am trying out new Clang static analysis checks for kernel bug classes, and they flagged this code as possibly buggy. The findings were triaged/fixed and commit descriptions were drafted with help of AI, then checked against linux-next (not runtime-tested on hardware).

Before you send any more: if you cannot reproduce it in the tree, don't report it.

So can this here be reproduced?

We're very interested in real bugs, but not in theoretical ones.

Signature · 3 lines
Cheers,

David
author
#2 lore raw
Quoted 1 line

Before you send any more: if you cannot reproduce it in the tree, don't report it.

As with the previous report, this one is just a hardening patch on a theoretical case.

Please ignore this patch.

Thanks again for the feedback!!
Elkin

#3 lore raw
Quoted 6 lines · Elkin Cruz
Quoted 1 line

Before you send any more: if you cannot reproduce it in the tree, don't report it.

As with the previous report, this one is just a hardening patch on a theoretical case.

Please ignore this patch.

All good, just don't use Fixes/stable when not appropriate :)

My memory came back, I asked for this exact change and we ended up with:

https://lore.kernel.org/linux-mm/[email protected]/

Can you review that one? it does more refactoring, but I think it covers this change as well.

Signature · 3 lines
Cheers,

David