mm/migrate_device: avoid out-of-bounds writes for compound folios
Subsystems (3)
PATCH v2 1/1 mm/migrate_device: avoid out-of-bounds writes for compound folios
migrate_device_range() and migrate_device_pfns() clear the entries following a compound folio so that the PFN arrays retain their page-granular representation.
If a compound folio extends beyond the end of the caller-provided range, the loops clear all following folio entries without limiting them to the number of slots remaining in the npages-sized array, causing an out-of-bounds write.
Do not proceed with a compound folio if its page-granular representation does not fit entirely in the remaining PFN array. If this happens, drop any reference and lock acquired for the folio, clear the remaining entries, and stop collecting.
Observed with a KASAN x86 QEMU kernel using the HMM migrate_anon_huge_zero selftest. Closing /dev/hmm_dmirror0 after migrating an anonymous huge page to device memory exercises:
dmirror_fops_release()
-> dmirror_device_evict_chunk()
-> migrate_device_range()---
Changes in v2:
- Do not partially represent a compound folio when it does not fit in the remaining PFN array.
- Drop any reference and lock acquired for that folio, clear the remaining entries, and stop collecting, as suggested by Balbir Singh.
v1: https://lore.kernel.org/lkml/[email protected]/
mm/migrate_device.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
mm/migrate_device.c +18 -0
@@ -1400,6 +1400,15 @@ int migrate_device_range(unsigned long *src_pfns, unsigned long start, 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)); +break ; + }if (nr >1 ) { src_pfns[i] |=MIGRATE_PFN_COMPOUND ;for (j =1 ; j < nr; j++)
@@ -1434,6 +1443,15 @@ int migrate_device_pfns(unsigned long *src_pfns, unsigned long npages) 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)); +break ; + }if (nr >1 ) { src_pfns[i] |=MIGRATE_PFN_COMPOUND ;for (j =1 ; j < nr; j++)
Signature · 1 line
2.54.0
Andrew Morton
maintainerQuoted 14 lines · On Mon, 17 Aug 2026 20:08:00 +0800 Hui Su <[email protected]> wrote:
migrate_device_range()andmigrate_device_pfns()clear the entries following a compound folio so that the PFN arrays retain their page-granular representation.If a compound folio extends beyond the end of the caller-provided range, the loops clear all following folio entries without limiting them to the number of slots remaining in the npages-sized array, causing an out-of-bounds write.
Do not proceed with a compound folio if its page-granular representation does not fit entirely in the remaining PFN array. If this happens, drop any reference and lock acquired for the folio, clear the remaining entries, and stop collecting.
Quoted 7 lines
Observed with a KASAN x86 QEMU kernel using the HMM
migrate_anon_huge_zeroselftest. Closing/dev/hmm_dmirror0after migrating an anonymous huge page to device memory exercises:
dmirror_fops_release()-> dmirror_device_evict_chunk() -> migrate_device_range()
It isn't clear (to me, at least) what "exercises:" means. Was there a WARN? Did the kernel crash? Did it erase all my cat videos?
IOW, in detail, what are the userspace-visible effects of the bug? Please add this info to the changelog and maintain it.
Sashiko might have found some things. Some pre-existing, some newly added:
https://sashiko.dev/#/patchset/[email protected]
Quoted 24 lines · Andrew Morton
Quoted 14 lines · On Mon, 17 Aug 2026 20:08:00 +0800 Hui Su <[email protected]> wrote:
migrate_device_range()andmigrate_device_pfns()clear the entries following a compound folio so that the PFN arrays retain their page-granular representation.If a compound folio extends beyond the end of the caller-provided range, the loops clear all following folio entries without limiting them to the number of slots remaining in the npages-sized array, causing an out-of-bounds write.
Do not proceed with a compound folio if its page-granular representation does not fit entirely in the remaining PFN array. If this happens, drop any reference and lock acquired for the folio, clear the remaining entries, and stop collecting.
Quoted 7 lines
Observed with a KASAN x86 QEMU kernel using the HMM
migrate_anon_huge_zeroselftest. Closing/dev/hmm_dmirror0after migrating an anonymous huge page to device memory exercises:
dmirror_fops_release()-> dmirror_device_evict_chunk() -> migrate_device_range()
Quoted 10 lines
It isn't clear (to me, at least) what "exercises:" means. Was there a WARN? Did the kernel crash? Did it erase all my cat videos?
IOW, in detail, what are the userspace-visible effects of the bug? Please add this info to the changelog and maintain it.
Sashiko might have found some things. Some pre-existing, some newly added:
https://sashiko.dev/#/patchset/[email protected]
userspace-visible effects are a function of the usage of the API by the device driver. If the device driver passes in npages that don't account for PMD pages, it would be an issue. In the case above, closing the device will evict pages from the device back to the CPU, but I am surprised that the size of npages was not properly split, we should fix that as well.
FYI: I've tried reading through Sashiko's report, but there is too much pre-existing content that it points to and it can be quite time consuming. I am working on adding new test cases to try and catch some of the issues found
Balbir
David Hildenbrand (Arm)
maintainerQuoted 63 lines · Hui Su
migrate_device_range()andmigrate_device_pfns()clear the entries following a compound folio so that the PFN arrays retain their page-granular representation.If a compound folio extends beyond the end of the caller-provided range, the loops clear all following folio entries without limiting them to the number of slots remaining in the npages-sized array, causing an out-of-bounds write.
Do not proceed with a compound folio if its page-granular representation does not fit entirely in the remaining PFN array. If this happens, drop any reference and lock acquired for the folio, clear the remaining entries, and stop collecting.
Observed with a KASAN x86 QEMU kernel using the HMM
migrate_anon_huge_zeroselftest. Closing/dev/hmm_dmirror0after migrating an anonymous huge page to device memory exercises:
dmirror_fops_release()-> dmirror_device_evict_chunk() -> migrate_device_range()Fixes a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")Signed-off-by Hui Su <[email protected]>---
Changes in v2:
- Do not partially represent a compound folio when it does not fit in the remaining PFN array.
- Drop any reference and lock acquired for that folio, clear the remaining entries, and stop collecting, as suggested by Balbir Singh.v1: https://lore.kernel.org/lkml/[email protected]/
mm/migrate_device.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)mm/migrate_device.c +11
-0@@ -1400,6 +1400,15 @@ int migrate_device_range(unsigned long *src_pfns, unsigned long start, 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)); +break ; + }if (nr >1 ) { src_pfns[i] |=MIGRATE_PFN_COMPOUND ;for (j =1 ; j < nr; j++)@@ -1434,6 +1443,15 @@ int migrate_device_pfns(unsigned long *src_pfns, unsigned long npages) 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 ) {
mm/migrate_device.c int migrate_device_pfns(unsigned long *src_pfns, unsigned long npages) · line 1457
Quoted 10 lines
mm/migrate_device.c +7
-0+folio_unlock (folio); +folio_put (folio); + } +memset (&src_pfns[i],0 , + (npages - i) *sizeof (*src_pfns)); +break ; + }if (nr >1 ) { src_pfns[i] |=MIGRATE_PFN_COMPOUND ;for (j =1 ; j < nr; j++)
The code duplication here makes me angry. :) And using a memset on one branch but not on the other is weird.
But is this the right fix or rather what
https://lore.kernel.org/r/[email protected]
tried? That fix would also need a cleanup but seems code-wise simpler. A cleanup could look like:
mm/migrate_device.c +2 -4
@@ -1419,10 +1419,9 @@ int migrate_device_range(unsigned long *src_pfns, unsigned long start,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 =min (folio_nr_pages (folio), npages - i); src_pfns[i] =migrate_device_pfn_lock (pfn); - nr =folio_nr_pages (folio);if (nr >1 ) { src_pfns[i] |=MIGRATE_PFN_COMPOUND ;for (j =1 ; j < nr; j++)
@@ -1453,10 +1452,9 @@ int migrate_device_pfns(unsigned long *src_pfns, unsigned long npages)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 int nr =min (folio_nr_pages (folio), npages - i); src_pfns[i] =migrate_device_pfn_lock (src_pfns[i]); - nr =folio_nr_pages (folio);if (nr >1 ) { src_pfns[i] |=MIGRATE_PFN_COMPOUND ;for (j =1 ; j < nr; j++)
And as a further cleanup, we'd better de-duplicate that code and possibly use a memset for clearing, getting rid of j entirely.
Signature · 3 lines
Cheers, David
Matthew Brost
reviewerQuoted 78 lines · David Hildenbrand (Arm)
Quoted 63 lines · Hui Su
migrate_device_range()andmigrate_device_pfns()clear the entries following a compound folio so that the PFN arrays retain their page-granular representation.If a compound folio extends beyond the end of the caller-provided range, the loops clear all following folio entries without limiting them to the number of slots remaining in the npages-sized array, causing an out-of-bounds write.
Do not proceed with a compound folio if its page-granular representation does not fit entirely in the remaining PFN array. If this happens, drop any reference and lock acquired for the folio, clear the remaining entries, and stop collecting.
Observed with a KASAN x86 QEMU kernel using the HMM
migrate_anon_huge_zeroselftest. Closing/dev/hmm_dmirror0after migrating an anonymous huge page to device memory exercises:
dmirror_fops_release()-> dmirror_device_evict_chunk() -> migrate_device_range()Fixes a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")Signed-off-by Hui Su <[email protected]>---
Changes in v2:
- Do not partially represent a compound folio when it does not fit in the remaining PFN array.
- Drop any reference and lock acquired for that folio, clear the remaining entries, and stop collecting, as suggested by Balbir Singh.v1: https://lore.kernel.org/lkml/[email protected]/
mm/migrate_device.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)mm/migrate_device.c +11
-0@@ -1400,6 +1400,15 @@ int migrate_device_range(unsigned long *src_pfns, unsigned long start, 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)); +break ; + }if (nr >1 ) { src_pfns[i] |=MIGRATE_PFN_COMPOUND ;for (j =1 ; j < nr; j++)@@ -1434,6 +1443,15 @@ int migrate_device_pfns(unsigned long *src_pfns, unsigned long npages) 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 ) {mm/migrate_device.c int migrate_device_pfns(unsigned long *src_pfns, unsigned long npages) · line 1457
Quoted 10 lines
mm/migrate_device.c +7
-0+folio_unlock (folio); +folio_put (folio); + } +memset (&src_pfns[i],0 , + (npages - i) *sizeof (*src_pfns)); +break ; + }if (nr >1 ) { src_pfns[i] |=MIGRATE_PFN_COMPOUND ;for (j =1 ; j < nr; j++)The code duplication here makes me angry. :) And using a memset on one branch but not on the other is weird.
Quoted 4 lines
But is this the right fix or rather what
https://lore.kernel.org/r/[email protected]
I'm not sure my patch is right now that I think about, or if this patch is right, or your suggestion either :).
Quoted 19 lines
tried? That fix would also need a cleanup but seems code-wise simpler. A cleanup could look like:
mm/migrate_device.c +1
-2@@ -1419,10 +1419,9 @@ int migrate_device_range(unsigned long *src_pfns, unsigned long start,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 =min (folio_nr_pages (folio), npages - i); src_pfns[i] =migrate_device_pfn_lock (pfn); - nr =folio_nr_pages (folio);if (nr >1 ) { src_pfns[i] |=MIGRATE_PFN_COMPOUND ;for (j =1 ; j < nr; j++)
mm/migrate_device.c int migrate_device_pfns(unsigned long *src_pfns, unsigned long npages) · line 1455
Quoted 5 lines
mm/migrate_device.c +1
-1@@ -1453,10 +1452,9 @@ int migrate_device_pfns(unsigned long *src_pfns, unsigned long npages)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 int nr =min (folio_nr_pages (folio), npages - i);
I think the semantics of both these function should be that if a user passes in a PFN array and one of the PFNs exceeds the npages limit, that is a misuse of this function and we should unwind and return an error. The caller is providing the PFNs and should therefore know the backing page alignment, so npages should always end exactly on a page boundary. The sole caller of migrate_device_pfns function is drm_gpusvm/pagemap, which certainly adheres to this requirement.
The two callers of migrate_device_range, also seemly adheres to this requirement.
Matt
Quoted 17 lines
mm/migrate_device.c +0
-1src_pfns[i] =migrate_device_pfn_lock (src_pfns[i]); - nr =folio_nr_pages (folio);if (nr >1 ) { src_pfns[i] |=MIGRATE_PFN_COMPOUND ;for (j =1 ; j < nr; j++)And as a further cleanup, we'd better de-duplicate that code and possibly use a memset for clearing, getting rid of j entirely.
Signature · 3 lines
Cheers, David
David Hildenbrand (Arm)
maintainerQuoted 120 lines · Matthew Brost
Quoted 78 lines · David Hildenbrand (Arm)
Quoted 63 lines · Hui Su
migrate_device_range()andmigrate_device_pfns()clear the entries following a compound folio so that the PFN arrays retain their page-granular representation.If a compound folio extends beyond the end of the caller-provided range, the loops clear all following folio entries without limiting them to the number of slots remaining in the npages-sized array, causing an out-of-bounds write.
Do not proceed with a compound folio if its page-granular representation does not fit entirely in the remaining PFN array. If this happens, drop any reference and lock acquired for the folio, clear the remaining entries, and stop collecting.
Observed with a KASAN x86 QEMU kernel using the HMM
migrate_anon_huge_zeroselftest. Closing/dev/hmm_dmirror0after migrating an anonymous huge page to device memory exercises:
dmirror_fops_release()-> dmirror_device_evict_chunk() -> migrate_device_range()Fixes a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")Signed-off-by Hui Su <[email protected]>---
Changes in v2:
- Do not partially represent a compound folio when it does not fit in the remaining PFN array.
- Drop any reference and lock acquired for that folio, clear the remaining entries, and stop collecting, as suggested by Balbir Singh.v1: https://lore.kernel.org/lkml/[email protected]/
mm/migrate_device.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)mm/migrate_device.c +11
-0@@ -1400,6 +1400,15 @@ int migrate_device_range(unsigned long *src_pfns, unsigned long start, 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)); +break ; + }if (nr >1 ) { src_pfns[i] |=MIGRATE_PFN_COMPOUND ;for (j =1 ; j < nr; j++)@@ -1434,6 +1443,15 @@ int migrate_device_pfns(unsigned long *src_pfns, unsigned long npages) 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 ) {mm/migrate_device.c int migrate_device_pfns(unsigned long *src_pfns, unsigned long npages) · line 1457
Quoted 10 lines
mm/migrate_device.c +7
-0+folio_unlock (folio); +folio_put (folio); + } +memset (&src_pfns[i],0 , + (npages - i) *sizeof (*src_pfns)); +break ; + }if (nr >1 ) { src_pfns[i] |=MIGRATE_PFN_COMPOUND ;for (j =1 ; j < nr; j++)The code duplication here makes me angry. :) And using a memset on one branch but not on the other is weird.
Quoted 4 lines
But is this the right fix or rather what
https://lore.kernel.org/r/[email protected]I'm not sure my patch is right now that I think about, or if this patch is right, or your suggestion either :).
Quoted 19 lines
tried? That fix would also need a cleanup but seems code-wise simpler. A cleanup could look like:
mm/migrate_device.c +1
-2@@ -1419,10 +1419,9 @@ int migrate_device_range(unsigned long *src_pfns, unsigned long start,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 =min (folio_nr_pages (folio), npages - i); src_pfns[i] =migrate_device_pfn_lock (pfn); - nr =folio_nr_pages (folio);if (nr >1 ) { src_pfns[i] |=MIGRATE_PFN_COMPOUND ;for (j =1 ; j < nr; j++)mm/migrate_device.c int migrate_device_pfns(unsigned long *src_pfns, unsigned long npages) · line 1455
Quoted 5 lines
mm/migrate_device.c +1
-1@@ -1453,10 +1452,9 @@ int migrate_device_pfns(unsigned long *src_pfns, unsigned long npages)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 int nr =min (folio_nr_pages (folio), npages - i);I think the semantics of both these function should be that if a user passes in a PFN array and one of the PFNs exceeds the
npageslimit, that is a misuse of this function and we should unwind and return an error. The caller is providing the PFNs and should therefore know the backing page alignment, sonpagesshould always end exactly on a page boundary. The sole caller ofmigrate_device_pfnsfunction isdrm_gpusvm/pagemap, which certainly adheres to this requirement.
Quoted 2 lines
The two callers of
migrate_device_range, also seemly adheres to this requirement.
That's what I was thinking: because how should we partially migrate folios? Doesn't make sense :)
If this cannot be triggered today, neither Fixes: nor CC: stable is appropriate.
Agreed that we should just warn.
Signature · 3 lines
Cheers, David
Quoted 6 lines · David Hildenbrand (Arm)
That's what I was thinking: because how should we partially migrate folios? Doesn't make sense :)
If this cannot be triggered today, neither Fixes: nor CC: stable is appropriate.
Agreed that we should just warn.
Hi David, Matthew,
Sorry, I missed this earlier.
This fix has already landed in mainline, but I agree that the API semantics should be clarified. Partially migrating a compound folio does not make sense. If the intended contract is that callers must not provide a range that cuts through a compound folio, then treating such input as caller misuse with an explicit WARN/error path sounds reasonable.
The reason I added the Fixes tag and Cc'd stable was that I observed an actual KASAN out-of-bounds write with the HMM migrate_anon_huge_zero selftest. The path was:
dmirror_fops_release()
-> dmirror_device_evict_chunk()
-> migrate_device_range()My intent was to prevent migrate_device_range() from writing past the caller-provided src_pfns array when such an input is observed. That said, silently stopping the collection is not necessarily the best API semantics if this should be treated as caller misuse.
I will rerun the reproducer on current mainline and report back with the result. If the preferred direction is to make this an explicit WARN/error path instead, I can send a follow-up patch on top of mainline.
Thanks,
Hui
David Hildenbrand (Arm)
maintainerQuoted 26 lines · Hui Su
Quoted 6 lines · David Hildenbrand (Arm)
That's what I was thinking: because how should we partially migrate folios? Doesn't make sense :)
If this cannot be triggered today, neither Fixes: nor CC: stable is appropriate.
Agreed that we should just warn.
Hi David, Matthew,
Sorry, I missed this earlier.
This fix has already landed in mainline, but I agree that the API semantics should be clarified. Partially migrating a compound folio does not make sense. If the intended contract is that callers must not provide a range that cuts through a compound folio, then treating such input as caller misuse with an explicit WARN/error path sounds reasonable.
The reason I added the Fixes tag and Cc'd stable was that I observed an actual KASAN out-of-bounds write with the HMM
migrate_anon_huge_zeroselftest. The path was:
dmirror_fops_release()-> dmirror_device_evict_chunk() -> migrate_device_range()
Quoted 8 lines
My intent was to prevent
migrate_device_range()from writing past the caller-providedsrc_pfnsarray when such an input is observed. That said, silently stopping the collection is not necessarily the best API semantics if this should be treated as caller misuse.I will rerun the reproducer on current mainline and report back with the result. If the preferred direction is to make this an explicit WARN/error path instead, I can send a follow-up patch on top of mainline.
Yes, and please clean up the code in any case; the duplication should be avoided.
Signature · 3 lines
Cheers, David
Quoted 1 line · David Hildenbrand (Arm)
Yes, and please clean up the code in any case; the duplication should be avoided.
Hi David, Matthew,
I reran the original HMM migrate_anon_huge_zero reproducer on current mainline.
The private-device case passes, and I could not reproduce the original KASAN out-of-bounds write. I also instrumented the truncated compound-folio case, and that condition was not hit. The coherent-device case was skipped on this setup.
So for the follow-up cleanup, I don't plan to add a Fixes tag or Cc stable. It only consolidates the duplicated compound-folio handling, uses memset() for the tail entries, and adds WARN_ON_ONCE() for the caller invariant discussed here.
The original stable Cc was based on the KASAN OOB I observed on the tree I tested at the time.
I'll send the cleanup as a separate patch.
Thanks,
Hui