* [PATCH] mm/migrate_device: use locked folio in migrate_device_{range,pfns}()
@ 2026-11-25 22:27 Elkin Cruz
2026-11-25 22:41 ` David Hildenbrand (Arm)
0 siblings, 1 reply; 4+ messages in thread
From: Elkin Cruz @ 2026-11-25 22:27 UTC (permalink / raw)
To: David Hildenbrand, Andrew Morton
Cc: Zi Yan, Matthew Brost, Joshua Hahn, Byungchul Park, Gregory Price,
Ying Huang, Alistair Popple, linux-mm, Lorenzo Stoakes,
linux-kernel, llvm, irogers, Elkin Cruz, stable
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")
Cc: stable@vger.kernel.org
Signed-off-by: Elkin Cruz <elkin@google.com>
---
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(-)
diff --git a/mm/migrate_device.c b/mm/migrate_device.c
index a953e7acbea6..a599d5baa340 100644
--- a/mm/migrate_device.c
+++ b/mm/migrate_device.c
@@ -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;
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] mm/migrate_device: use locked folio in migrate_device_{range,pfns}()
2026-11-25 22:27 [PATCH] mm/migrate_device: use locked folio in migrate_device_{range,pfns}() Elkin Cruz
@ 2026-11-25 22:41 ` David Hildenbrand (Arm)
2026-11-25 22:58 ` Elkin Cruz
0 siblings, 1 reply; 4+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-25 22:41 UTC (permalink / raw)
To: Elkin Cruz, Andrew Morton
Cc: Zi Yan, Matthew Brost, Joshua Hahn, Byungchul Park, Gregory Price,
Ying Huang, Alistair Popple, linux-mm, Lorenzo Stoakes,
linux-kernel, llvm, irogers, stable
On 11/25/26 23:27, Elkin Cruz wrote:
> 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")
> Cc: stable@vger.kernel.org
> Signed-off-by: Elkin Cruz <elkin@google.com>
> ---
>
> 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.
--
Cheers,
David
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] mm/migrate_device: use locked folio in migrate_device_{range,pfns}()
2026-11-25 22:41 ` David Hildenbrand (Arm)
@ 2026-11-25 22:58 ` Elkin Cruz
2026-11-26 11:31 ` David Hildenbrand (Arm)
0 siblings, 1 reply; 4+ messages in thread
From: Elkin Cruz @ 2026-11-25 22:58 UTC (permalink / raw)
To: David Hildenbrand (Arm)
Cc: Andrew Morton, Zi Yan, Matthew Brost, Joshua Hahn, Byungchul Park,
Gregory Price, Ying Huang, Alistair Popple, linux-mm,
Lorenzo Stoakes, linux-kernel, llvm, irogers, stable
> 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
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] mm/migrate_device: use locked folio in migrate_device_{range,pfns}()
2026-11-25 22:58 ` Elkin Cruz
@ 2026-11-26 11:31 ` David Hildenbrand (Arm)
0 siblings, 0 replies; 4+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-26 11:31 UTC (permalink / raw)
To: Elkin Cruz
Cc: Andrew Morton, Zi Yan, Matthew Brost, Joshua Hahn, Byungchul Park,
Gregory Price, Ying Huang, Alistair Popple, linux-mm,
Lorenzo Stoakes, linux-kernel, llvm, irogers, stable
On 11/25/26 23:58, Elkin Cruz wrote:
>> 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/20260924041210.13d04b8be24495fc593665283d2e4453d449502f@163.com/
Can you review that one? it does more refactoring, but I think it covers this
change as well.
--
Cheers,
David
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-26 11:31 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 22:27 [PATCH] mm/migrate_device: use locked folio in migrate_device_{range,pfns}() Elkin Cruz
2026-11-25 22:41 ` David Hildenbrand (Arm)
2026-11-25 22:58 ` Elkin Cruz
2026-11-26 11:31 ` David Hildenbrand (Arm)
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox