mm: reject zone device folios in more folio walkers
Subsystems (5)
Gregory Price
maintainerv2 cover mm: reject zone device folios in more folio walkers
Several LRU-oriented mm walkers resolve the folio backing a PMD entry (or a physical pfn) and then reclaim, age, migrate, or lazyfree it without ever checking for ZONE_DEVICE memory.
This series adds missing folio_is_zone_device() rejections, matching the checks that comparable walkers already perform.
- mm/huge_memory, mm/madvise: the !pmd_present branch above these sites only filters device-private entries (which are non-present).
A present zone device PMD (e.g. device-coherent) would still reach the folio and be lazyfreed / aged / paged out. Add an explicit check.
- mm/mempolicy: queue_folios_pmd() can see a present zone device PMD (e.g. device-coherent) and queue it for migration.
No crash reproducer - this is a correctness/hardening cleanup found by inspection. All checks are placed after the folio is resolved and before it is acted upon, on paths that already hold the relevant page-table lock, so no locking or refcount changes are involved.
Gregory Price (3):
mm/huge_memory: skip zone device folios in madvise_free_huge_pmd() mm/madvise: skip zone device folios in cold/pageout PMD range mm/mempolicy: skip zone device folios when queueing folios
mm/huge_memory.c | 4 ++++ mm/madvise.c | 3 +++ mm/mempolicy.c | 2 ++ 3 files changed, 9 insertions(+)
---
v2 - drop hugetlb, move checks earlier in queue migration route
- fixes tags for all 3
Signature · 1 line
2.53.0-Meta
Gregory Price
maintainerPATCH v2 1/3 mm/huge_memory: skip zone device folios in madvise_free_huge_pmd()
madvise_free_huge_pmd() resolves the folio backing a PMD via pmd_folio() and marks it lazyfree without checking for zone device memory.
The surrounding guards do not cover every zone device case:
- MADV_FREE only operates on anonymous VMAs (DAX mappings are excluded)
- !pmd_present() branch rejects device-private and migration entries
- present zone device PMD (device coherent THP) is not filtered. Unlike vm_normal_page_pmd(), it performs no special/pfnmap check, and would be marked lazyfree here.
Bail out when the folio is a zone device folio.
--- mm/huge_memory.c | 4 ++++ 1 file changed, 4 insertions(+)
mm/huge_memory.c +4 -0
@@ -2381,6 +2381,10 @@ bool madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, } folio =pmd_folio (orig_pmd); + +if (folio_is_zone_device (folio)) + goto out; +/* * If other processes are mapping this folio, we couldn't discard * the folio unless they all do MADV_FREE so let's skip the folio.
Signature · 1 line
2.53.0-Meta
Gregory Price
maintainerPATCH v2 2/3 mm/madvise: skip zone device folios in cold/pageout PMD range
In reply to Gregory Pricemadvise_cold_or_pageout_pte_range() resolves the folio backing a PMD via pmd_folio() and ages or reclaims it without checking for zone device memory.
The surrounding guards do not cover every zone device case:
- can_madv_lru_vma() excludes VM_PFNMAP and VM_HUGETLB VMAs (so device DAX is filtered)
- !pmd_present() branch above rejects device-private and migration entries, which are non-present.
- A present zone device PMD - e.g. a device-coherent THP - is not filtered by any of these, nor by pmd_folio() (unlike vm_normal_page_pmd(), it performs no special/pfnmap check), and would be aged or paged out here.
Skip ZONE_DEVICE folios explicitly during MADV_COLD/PAGEOUT.
--- mm/madvise.c | 3 +++ 1 file changed, 3 insertions(+)
mm/madvise.c +3 -0
@@ -396,6 +396,9 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, folio =pmd_folio (orig_pmd); +if (folio_is_zone_device (folio)) + goto huge_unlock; +/* Do not interfere with other mappings of this folio */ if (folio_maybe_mapped_shared (folio)) goto huge_unlock;
Signature · 1 line
2.53.0-Meta
Re: [PATCH v2 2/3] mm/madvise: skip zone device folios in cold/pageout PMD range
Quoted 33 lines · Gregory Price
madvise_cold_or_pageout_pte_range()resolves the folio backing a PMD viapmd_folio()and ages or reclaims it without checking for zone device memory.The surrounding guards do not cover every zone device case:
-
can_madv_lru_vma()excludesVM_PFNMAPandVM_HUGETLBVMAs (so device DAX is filtered)- !pmd_present() branch above rejects device-private and migration entries, which are non-present.
- A present zone device PMD - e.g. a device-coherent THP - is not filtered by any of these, nor by
pmd_folio()(unlikevm_normal_page_pmd(), it performs no special/pfnmap check), and would be aged or paged out here.Skip
ZONE_DEVICEfolios explicitly during MADV_COLD/PAGEOUT.Fixes a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")Signed-off-by Gregory Price (Meta) <[email protected]>Reviewed-by Lorenzo Stoakes (ARM) <[email protected]>--- mm/madvise.c | 3 +++ 1 file changed, 3 insertions(+)mm/madvise.c +0
-0
mm/madvise.c static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, · line 404
Quoted 9 lines
mm/madvise.c +3
-0@@ -396,6 +396,9 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, folio =pmd_folio (orig_pmd); +if (folio_is_zone_device (folio)) + goto huge_unlock; +/* Do not interfere with other mappings of this folio */ if (folio_maybe_mapped_shared (folio)) goto huge_unlock;
Makes sense
Gregory Price
maintainerPATCH v2 3/3 mm/mempolicy: skip zone device folios when queueing folios
In reply to Gregory Pricequeue_folios_pte_range() already pairs vm_normal_folio() with an explicit folio_is_zone_device() check before adding folios to the migration pagelist.
vm_normal_folio() alone does not reject zone device memory (a present device-coherent page in a normal VMA is returned as "normal").
Mirror the explicit check in queue_folios_pmd() as well.
queue_folios_pmd() uses pmd_folio() directly and can encounter a present zone device PMD - e.g. a device-coherent THP.
This is not filtered by existing checks:
!pmd_present() - only rejects non-present device-private and
migration entries
vma_migratable() - excludes DAX and VM_PFNMAP.
The early return also means such a folio is no longer counted in qp->nr_failed under MPOL_MF_STRICT. This is the same pattern used by queue_folios_pte_range() (skipping zone device without failing).
--- mm/mempolicy.c | 2 ++ 1 file changed, 2 insertions(+)
mm/mempolicy.c +2 -0
@@ -662,6 +662,8 @@ static void queue_folios_pmd(pmd_t *pmd, struct mm_walk *walk)return ; } folio =pmd_folio (pmdval); +if (folio_is_zone_device (folio)) +return ;if (is_huge_zero_folio (folio)) { walk->action =ACTION_CONTINUE ;return ;
Signature · 1 line
2.53.0-Meta
Re: [PATCH v2 3/3] mm/mempolicy: skip zone device folios when queueing folios
Quoted 45 lines · Gregory Price
queue_folios_pte_range()already pairsvm_normal_folio()with an explicitfolio_is_zone_device()check before adding folios to the migration pagelist.
vm_normal_folio()alone does not reject zone device memory (a present device-coherent page in a normal VMA is returned as "normal").Mirror the explicit check in
queue_folios_pmd()as well.
queue_folios_pmd()usespmd_folio()directly and can encounter a present zone device PMD - e.g. a device-coherent THP.This is not filtered by existing checks:
!pmd_present() - only rejects non-present device-private and
migration entries
vma_migratable()- excludes DAX andVM_PFNMAP.The early return also means such a folio is no longer counted in
qp->nr_failedunderMPOL_MF_STRICT. This is the same pattern used byqueue_folios_pte_range()(skipping zone device without failing).Fixes a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")Signed-off-by Gregory Price (Meta) <[email protected]>--- mm/mempolicy.c | 2 ++ 1 file changed, 2 insertions(+)mm/mempolicy.c +2
-0@@ -662,6 +662,8 @@ static void queue_folios_pmd(pmd_t *pmd, struct mm_walk *walk)return ; } folio =pmd_folio (pmdval); +if (folio_is_zone_device (folio)) +return ;if (is_huge_zero_folio (folio)) { walk->action =ACTION_CONTINUE ;return ;Signature · 1 line
2.53.0-Meta
A similar patch was sent out earlier, but that was only for zone device private THP, we don't support migration of device coherent THP today (we should add support). The patch itself makes sense
Gregory Price
maintainerRe: [PATCH v2 3/3] mm/mempolicy: skip zone device folios when queueing folios
Quoted 15 lines · Balbir Singh
Quoted 14 lines · Gregory Price
mm/mempolicy.c +2
-0@@ -662,6 +662,8 @@ static void queue_folios_pmd(pmd_t *pmd, struct mm_walk *walk)return ; } folio =pmd_folio (pmdval); +if (folio_is_zone_device (folio)) +return ;if (is_huge_zero_folio (folio)) { walk->action =ACTION_CONTINUE ;return ;
Quoted 9 lines
Quoted 3 lines
Signature · 1 line
2.53.0-MetaA similar patch was sent out earlier, but that was only for zone device private THP, we don't support migration of device coherent THP today (we should add support). The patch itself makes sense
Yeah IIRC that was Usama's and I got confused thinking this had already been resolved - but i realized this still affects device coherent.
ty!
~Gregory
Re: [PATCH v2 3/3] mm/mempolicy: skip zone device folios when queueing folios
In reply to Gregory PriceQuoted 18 lines · Gregory Price
queue_folios_pte_range()already pairsvm_normal_folio()with an explicitfolio_is_zone_device()check before adding folios to the migration pagelist.
vm_normal_folio()alone does not reject zone device memory (a present device-coherent page in a normal VMA is returned as "normal").Mirror the explicit check in
queue_folios_pmd()as well.
queue_folios_pmd()usespmd_folio()directly and can encounter a present zone device PMD - e.g. a device-coherent THP.This is not filtered by existing checks:
!pmd_present() - only rejects non-present device-private and
migration entries
vma_migratable()- excludes DAX andVM_PFNMAP.
Quoted 7 lines
The early return also means such a folio is no longer counted in
qp->nr_failedunderMPOL_MF_STRICT. This is the same pattern used byqueue_folios_pte_range()(skipping zone device without failing).Fixes a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")Signed-off-by Gregory Price (Meta) <[email protected]>
Seems sensible so:
Quoted 18 lines
--- mm/mempolicy.c | 2 ++ 1 file changed, 2 insertions(+)mm/mempolicy.c +2
-1@@ -662,6 +662,8 @@ static void queue_folios_pmd(pmd_t *pmd, struct mm_walk *walk)return ; } folio =pmd_folio (pmdval); +if (folio_is_zone_device (folio)) +return ;if (is_huge_zero_folio (folio)) { walk->action =ACTION_CONTINUE ;return ; --
Quoted 2 lines
2.53.0-Meta
--
Cheers, Lorenzo
Re: [PATCH v2 3/3] mm/mempolicy: skip zone device folios when queueing folios
In reply to Gregory PriceQuoted 18 lines · Gregory Price
queue_folios_pte_range()already pairsvm_normal_folio()with an explicitfolio_is_zone_device()check before adding folios to the migration pagelist.
vm_normal_folio()alone does not reject zone device memory (a present device-coherent page in a normal VMA is returned as "normal").Mirror the explicit check in
queue_folios_pmd()as well.
queue_folios_pmd()usespmd_folio()directly and can encounter a present zone device PMD - e.g. a device-coherent THP.This is not filtered by existing checks:
!pmd_present() - only rejects non-present device-private and
migration entries
vma_migratable()- excludes DAX andVM_PFNMAP.
Quoted 8 lines
The early return also means such a folio is no longer counted in
qp->nr_failedunderMPOL_MF_STRICT. This is the same pattern used byqueue_folios_pte_range()(skipping zone device without failing).Fixes a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")Signed-off-by Gregory Price (Meta) <[email protected]>---
Signature · 3 lines
Cheers, David
Quoted 13 lines · Gregory Price
Several LRU-oriented mm walkers resolve the folio backing a PMD entry (or a physical pfn) and then reclaim, age, migrate, or lazyfree it without ever checking for
ZONE_DEVICEmemory.This series adds missing
folio_is_zone_device()rejections, matching the checks that comparable walkers already perform.-
mm/huge_memory, mm/madvise: the !pmd_present branch above these sites only filters device-private entries (which are non-present).A present zone device PMD (e.g. device-coherent) would still reach the folio and be lazyfreed / aged / paged out. Add an explicit check.
Quoted 7 lines
- mm/mempolicy:
queue_folios_pmd()can see a present zone device PMD (e.g. device-coherent) and queue it for migration.No crash reproducer - this is a correctness/hardening cleanup found by inspection. All checks are placed after the folio is resolved and before it is acted upon, on paths that already hold the relevant page-table lock, so no locking or refcount changes are involved.
Cool!
Gave the whole series a spin on x86_64 QEMU with a PMD-mapped device-coherent THP. Without these patches, partial MADV_FREE and MADV_COLD reliably hit a kernel panic in remove_migration_pte(), while mbind(MPOL_MF_MOVE | MPOL_MF_STRICT) returned -EIO.
With v2, all three worked fine, PMD mapping stayed intact, and data checked out :)
Note that both kernels used the same small change to the in-kernel HMM test driver, allowing its coherent device memory to be allocated as 2 MB folios so the PMD-mapped test case could be exercised.
Andrew Morton
maintainerQuoted 30 lines · On Tue, 18 Aug 2026 12:31:43 +0800 Lance Yang <[email protected]> wrote:
Quoted 13 lines · Gregory Price
Several LRU-oriented mm walkers resolve the folio backing a PMD entry (or a physical pfn) and then reclaim, age, migrate, or lazyfree it without ever checking for
ZONE_DEVICEmemory.This series adds missing
folio_is_zone_device()rejections, matching the checks that comparable walkers already perform.-
mm/huge_memory, mm/madvise: the !pmd_present branch above these sites only filters device-private entries (which are non-present).A present zone device PMD (e.g. device-coherent) would still reach the folio and be lazyfreed / aged / paged out. Add an explicit check.
Quoted 7 lines
- mm/mempolicy:
queue_folios_pmd()can see a present zone device PMD (e.g. device-coherent) and queue it for migration.No crash reproducer - this is a correctness/hardening cleanup found by inspection. All checks are placed after the folio is resolved and before it is acted upon, on paths that already hold the relevant page-table lock, so no locking or refcount changes are involved.
Cool!
Gave the whole series a spin on
x86_64QEMU with a PMD-mapped device-coherent THP. Without these patches, partialMADV_FREEandMADV_COLDreliably hit a kernel panic inremove_migration_pte(), whilembind(MPOL_MF_MOVE | MPOL_MF_STRICT)returned-EIO.
Quoted 8 lines
With v2, all three worked fine, PMD mapping stayed intact, and data checked out :)
Note that both kernels used the same small change to the in-kernel HMM test driver, allowing its coherent device memory to be allocated as 2 MB folios so the PMD-mapped test case could be exercised.
Tested-by Lance Yang <[email protected]>
Thanks Lance, you're so diligent.
I'm wondering what to do here. Gregory told us
: No crash reproducer - this is a correctness/hardening cleanup found by : inspection. All checks are placed after the folio is resolved and before : it is acted upon, on paths that already hold the relevant page-table lock, : so no locking or refcount changes are involved.
And you had to tweak the hmm-test driver to reproduce the bug(s).
So when do we push this series out to -stable? As a hair-on-fire hotfix, or as a leisurely next-merge-window thing?
And Sashiko was clearly having a bad day, able to find only nine pre-existing things to shout about:
https://sashiko.dev/#/patchset/[email protected]
Lance Yang
reviewerQuoted 50 lines · Andrew Morton
Quoted 30 lines · On Tue, 18 Aug 2026 12:31:43 +0800 Lance Yang <[email protected]> wrote:
Quoted 13 lines · Gregory Price
Several LRU-oriented mm walkers resolve the folio backing a PMD entry (or a physical pfn) and then reclaim, age, migrate, or lazyfree it without ever checking for
ZONE_DEVICEmemory.This series adds missing
folio_is_zone_device()rejections, matching the checks that comparable walkers already perform.-
mm/huge_memory, mm/madvise: the !pmd_present branch above these sites only filters device-private entries (which are non-present).A present zone device PMD (e.g. device-coherent) would still reach the folio and be lazyfreed / aged / paged out. Add an explicit check.
Quoted 7 lines
- mm/mempolicy:
queue_folios_pmd()can see a present zone device PMD (e.g. device-coherent) and queue it for migration.No crash reproducer - this is a correctness/hardening cleanup found by inspection. All checks are placed after the folio is resolved and before it is acted upon, on paths that already hold the relevant page-table lock, so no locking or refcount changes are involved.
Cool!
Gave the whole series a spin on
x86_64QEMU with a PMD-mapped device-coherent THP. Without these patches, partialMADV_FREEandMADV_COLDreliably hit a kernel panic inremove_migration_pte(), whilembind(MPOL_MF_MOVE | MPOL_MF_STRICT)returned-EIO.Quoted 8 lines
With v2, all three worked fine, PMD mapping stayed intact, and data checked out :)
Note that both kernels used the same small change to the in-kernel HMM test driver, allowing its coherent device memory to be allocated as 2 MB folios so the PMD-mapped test case could be exercised.
Tested-by Lance Yang <[email protected]>Thanks Lance, you're so diligent.
I'm wondering what to do here. Gregory told us
: No crash reproducer - this is a correctness/hardening cleanup found by : inspection. All checks are placed after the folio is resolved and before : it is acted upon, on paths that already hold the relevant page-table lock, : so no locking or refcount changes are involved.
Quoted 4 lines
And you had to tweak the hmm-test driver to reproduce the bug(s).
So when do we push this series out to -stable? As a hair-on-fire hotfix, or as a leisurely next-merge-window thing?
Thanks, Andrew :) Yeah, I'd say next merge window should be fine :)
The crash is real once the mapping exists, but I had to tweak test_hmm to create that PMD-mapped device-coherent folio, and I couldn't find any in-tree production driver doing that today.
So no need to rush this one, I guess.
Quoted 5 lines
And Sashiko was clearly having a bad day, able to find only nine pre-existing things to shout about:
https://sashiko.dev/#/patchset/[email protected]
Lance Yang
reviewerQuoted 58 lines · Lance Yang
Quoted 50 lines · Andrew Morton
Quoted 30 lines · On Tue, 18 Aug 2026 12:31:43 +0800 Lance Yang <[email protected]> wrote:
Quoted 13 lines · Gregory Price
Several LRU-oriented mm walkers resolve the folio backing a PMD entry (or a physical pfn) and then reclaim, age, migrate, or lazyfree it without ever checking for
ZONE_DEVICEmemory.This series adds missing
folio_is_zone_device()rejections, matching the checks that comparable walkers already perform.-
mm/huge_memory, mm/madvise: the !pmd_present branch above these sites only filters device-private entries (which are non-present).A present zone device PMD (e.g. device-coherent) would still reach the folio and be lazyfreed / aged / paged out. Add an explicit check.
Quoted 7 lines
- mm/mempolicy:
queue_folios_pmd()can see a present zone device PMD (e.g. device-coherent) and queue it for migration.No crash reproducer - this is a correctness/hardening cleanup found by inspection. All checks are placed after the folio is resolved and before it is acted upon, on paths that already hold the relevant page-table lock, so no locking or refcount changes are involved.
Cool!
Gave the whole series a spin on
x86_64QEMU with a PMD-mapped device-coherent THP. Without these patches, partialMADV_FREEandMADV_COLDreliably hit a kernel panic inremove_migration_pte(), whilembind(MPOL_MF_MOVE | MPOL_MF_STRICT)returned-EIO.Quoted 8 lines
With v2, all three worked fine, PMD mapping stayed intact, and data checked out :)
Note that both kernels used the same small change to the in-kernel HMM test driver, allowing its coherent device memory to be allocated as 2 MB folios so the PMD-mapped test case could be exercised.
Tested-by Lance Yang <[email protected]>Thanks Lance, you're so diligent.
I'm wondering what to do here. Gregory told us
: No crash reproducer - this is a correctness/hardening cleanup found by : inspection. All checks are placed after the folio is resolved and before : it is acted upon, on paths that already hold the relevant page-table lock, : so no locking or refcount changes are involved.
Quoted 4 lines
And you had to tweak the hmm-test driver to reproduce the bug(s).
So when do we push this series out to -stable? As a hair-on-fire hotfix, or as a leisurely next-merge-window thing?
Quoted 7 lines
Thanks, Andrew :) Yeah, I'd say next merge window should be fine :)
The crash is real once the mapping exists, but I had to tweak
test_hmmto create that PMD-mapped device-coherent folio, and I couldn't find any in-tree production driver doing that today.So no need to rush this one, I guess.
BTW, noticed that the ZONE_DEVICE split handling only covers device-private folios, so device-coherent folios aren't supported ...
The call chains are:
split_folio()
-> __folio_split()
-> folio_check_splittable()
-> __folio_freeze_and_split_unmapped()migrate_vma_pages()
-> __migrate_device_pages()
-> migrate_vma_split_unmapped_folio()
-> folio_split_unmapped()
-> __folio_freeze_and_split_unmapped()And I added the device-coherent check to folio_check_splittable() and folio_split_unmapped(). See below. They can go away once device-coherent folio splitting is supported :)
If folks think it's worth having, I can send it as a follow-up :)
---8<---
Subject: [PATCH] mm/huge_memory: don't split device-coherent folios
From: Lance Yang <[email protected]>
The ZONE_DEVICE split handling only covers device-private folios. Device-coherent folios are not supported.
The call chains are:
split_folio()
-> __folio_split()
-> folio_check_splittable()
-> __folio_freeze_and_split_unmapped()migrate_vma_pages()
-> __migrate_device_pages()
-> migrate_vma_split_unmapped_folio()
-> folio_split_unmapped()
-> __folio_freeze_and_split_unmapped()Reject device-coherent folios in folio_check_splittable() and folio_split_unmapped().
--- mm/huge_memory.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)
mm/huge_memory.c +11 -3
@@ -3937,6 +3937,10 @@ int folio_check_splittable(struct folio *folio, unsigned int new_order,if (!folio->mapping && !folio_test_anon (folio))return -EBUSY ; +/* TODO: Support splitting device-coherent folios. */ +if (folio_is_device_coherent (folio)) +return -EOPNOTSUPP ; +/* order-1 is not supported for anonymous THP. */ if (folio_test_anon (folio) && new_order ==1 )return -EINVAL ;
@@ -4354,15 +4358,16 @@ static int __folio_split(struct folio *folio, unsigned int new_order,* * anon_vma_lock is not required to be held, mmap_read_lock() or * mmap_write_lock() should be held. @folio is expected to be locked by the -* caller. device-private and non device-private folios are supported along +* caller. device-private and non-ZONE_DEVICE folios are supported along * with folios that are in the swapcache. @folio should also be unmapped and * isolated from LRU (if applicable) * * Upon return, the folio is not remapped, split folios are not added to LRU, * free_folio_and_swap_cache() is not called, and new folios remain locked. * -* Return: 0 on success, -EAGAIN if the folio cannot be split (e.g., due to -* insufficient reference count or extra pins). +* Return: 0 on success, -EOPNOTSUPP for device-coherent folios, or -EAGAIN if +* the folio cannot be split (e.g., due to insufficient reference +* count or extra pins). */ int folio_split_unmapped (struct folio *folio,unsigned int new_order) {
@@ -4373,6 +4378,9 @@ int folio_split_unmapped(struct folio *folio, unsigned int new_order)VM_WARN_ON_ONCE_FOLIO (!folio_test_large (folio), folio);VM_WARN_ON_ONCE_FOLIO (!folio_test_anon (folio), folio); +if (folio_is_device_coherent (folio)) +return -EOPNOTSUPP ; +if (folio_expected_ref_count (folio) !=folio_ref_count (folio) -1 )return -EAGAIN ;
--
Cheers, Lance
Gregory Price
maintainerQuoted 6 lines · Lance Yang
If folks think it's worth having, I can send it as a follow-up :)
---8<---
Subject: [PATCH]mm/huge_memory: don't split device-coherent folios
First - thank you for testing, I was struggling to figure out a way to test this and I somehow missed your email where you managed it.
Second - I think the split checks are worth it. As I've worked through my private node series, I've learned there are at least a handful of missing zone device hooks throughout mm - but there is a lot of nuance between the modes that is frustrating to keep straight.
Every zone device patch helps clarify how coherent-link accelerators might manage its memory as a numa node, so at the very least I appreciate it.
~Gregory
Quoted 157 lines · Lance Yang
Quoted 58 lines · Lance Yang
Quoted 50 lines · Andrew Morton
Quoted 30 lines · On Tue, 18 Aug 2026 12:31:43 +0800 Lance Yang <[email protected]> wrote:
Quoted 13 lines · Gregory Price
Several LRU-oriented mm walkers resolve the folio backing a PMD entry (or a physical pfn) and then reclaim, age, migrate, or lazyfree it without ever checking for
ZONE_DEVICEmemory.This series adds missing
folio_is_zone_device()rejections, matching the checks that comparable walkers already perform.-
mm/huge_memory, mm/madvise: the !pmd_present branch above these sites only filters device-private entries (which are non-present).A present zone device PMD (e.g. device-coherent) would still reach the folio and be lazyfreed / aged / paged out. Add an explicit check.
Quoted 7 lines
- mm/mempolicy:
queue_folios_pmd()can see a present zone device PMD (e.g. device-coherent) and queue it for migration.No crash reproducer - this is a correctness/hardening cleanup found by inspection. All checks are placed after the folio is resolved and before it is acted upon, on paths that already hold the relevant page-table lock, so no locking or refcount changes are involved.
Cool!
Gave the whole series a spin on
x86_64QEMU with a PMD-mapped device-coherent THP. Without these patches, partialMADV_FREEandMADV_COLDreliably hit a kernel panic inremove_migration_pte(), whilembind(MPOL_MF_MOVE | MPOL_MF_STRICT)returned-EIO.Quoted 8 lines
With v2, all three worked fine, PMD mapping stayed intact, and data checked out :)
Note that both kernels used the same small change to the in-kernel HMM test driver, allowing its coherent device memory to be allocated as 2 MB folios so the PMD-mapped test case could be exercised.
Tested-by Lance Yang <[email protected]>Thanks Lance, you're so diligent.
I'm wondering what to do here. Gregory told us
: No crash reproducer - this is a correctness/hardening cleanup found by : inspection. All checks are placed after the folio is resolved and before : it is acted upon, on paths that already hold the relevant page-table lock, : so no locking or refcount changes are involved.
Quoted 4 lines
And you had to tweak the hmm-test driver to reproduce the bug(s).
So when do we push this series out to -stable? As a hair-on-fire hotfix, or as a leisurely next-merge-window thing?
Quoted 7 lines
Thanks, Andrew :) Yeah, I'd say next merge window should be fine :)
The crash is real once the mapping exists, but I had to tweak
test_hmmto create that PMD-mapped device-coherent folio, and I couldn't find any in-tree production driver doing that today.So no need to rush this one, I guess.
BTW, noticed that the
ZONE_DEVICEsplit handling only covers device-private folios, so device-coherent folios aren't supported ...The call chains are:
split_folio()
->__folio_split()-> folio_check_splittable() -> __folio_freeze_and_split_unmapped()
migrate_vma_pages()
->__migrate_device_pages()-> migrate_vma_split_unmapped_folio() -> folio_split_unmapped() -> __folio_freeze_and_split_unmapped()And I added the device-coherent check to
folio_check_splittable()andfolio_split_unmapped(). See below. They can go away once device-coherent folio splitting is supported :)If folks think it's worth having, I can send it as a follow-up :)
---8<---
Subject: [PATCH]mm/huge_memory: don't split device-coherent foliosFrom: Lance Yang <[email protected]>
The
ZONE_DEVICEsplit handling only covers device-private folios. Device-coherent folios are not supported.The call chains are:
split_folio()
->__folio_split()-> folio_check_splittable() -> __folio_freeze_and_split_unmapped()
migrate_vma_pages()
->__migrate_device_pages()-> migrate_vma_split_unmapped_folio() -> folio_split_unmapped() -> __folio_freeze_and_split_unmapped()Reject device-coherent folios in
folio_check_splittable()andfolio_split_unmapped().Fixes a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")Signed-off-by Lance Yang <[email protected]>--- mm/huge_memory.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)mm/huge_memory.c +8
-3@@ -3937,6 +3937,10 @@ int folio_check_splittable(struct folio *folio, unsigned int new_order,if (!folio->mapping && !folio_test_anon (folio))return -EBUSY ; +/* TODO: Support splitting device-coherent folios. */ +if (folio_is_device_coherent (folio)) +return -EOPNOTSUPP ; +/* order-1 is not supported for anonymous THP. */ if (folio_test_anon (folio) && new_order ==1 )return -EINVAL ;@@ -4354,15 +4358,16 @@ static int __folio_split(struct folio *folio, unsigned int new_order,* * anon_vma_lock is not required to be held, mmap_read_lock() or * mmap_write_lock() should be held. @folio is expected to be locked by the -* caller. device-private and non device-private folios are supported along +* caller. device-private and non-ZONE_DEVICE folios are supported along * with folios that are in the swapcache. @folio should also be unmapped and * isolated from LRU (if applicable) * * Upon return, the folio is not remapped, split folios are not added to LRU, * free_folio_and_swap_cache() is not called, and new folios remain locked. * -* Return: 0 on success, -EAGAIN if the folio cannot be split (e.g., due to -* insufficient reference count or extra pins). +* Return: 0 on success, -EOPNOTSUPP for device-coherent folios, or -EAGAIN if +* the folio cannot be split (e.g., due to insufficient reference +* count or extra pins). */ int folio_split_unmapped (struct folio *folio,unsigned int new_order) {
mm/huge_memory.c int folio_split_unmapped(struct folio *folio, unsigned int new_order)
Quoted 10 lines
mm/huge_memory.c +3
-1@@ -4373,6 +4378,9 @@ int folio_split_unmapped(struct folio *folio, unsigned int new_order)VM_WARN_ON_ONCE_FOLIO (!folio_test_large (folio), folio);VM_WARN_ON_ONCE_FOLIO (!folio_test_anon (folio), folio); +if (folio_is_device_coherent (folio)) +return -EOPNOTSUPP ; +if (folio_expected_ref_count (folio) !=folio_ref_count (folio) -1 )return -EAGAIN ; --
FYI: Device Coherent THP is not yet supported, support should be easy to add. it is definitely desirable, we should get it working along with mTHP support as well (TODO).
Balbir