Skip to contentSkip to conversation

mm: reject zone device folios in more folio walkers

PATCH Applied Gregory Price 6 participants 15 messages Aug 17 – Aug 31

Subsystems (5)
lore
Share & tools
lore

Gregory Price

author maintainer
#0 lore raw

v2 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
author maintainer
#1 lore raw

PATCH 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.

Fixes a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")
---
 mm/huge_memory.c | 4 ++++
 1 file changed, 4 insertions(+)
mm/huge_memory.c +4 -0
index ced400f72d43a..714773695bbff 100644
@@ -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
author maintainer
#2 lore raw

PATCH v2 2/3 mm/madvise: skip zone device folios in cold/pageout PMD range

In reply to Gregory Price

madvise_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.

Fixes a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")
---
 mm/madvise.c | 3 +++
 1 file changed, 3 insertions(+)
mm/madvise.c +3 -0
index c179938097bf0..2151bb9468594 100644
@@ -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
Reviewed-by on v2 2/3
#3 lore raw

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 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.

Fixes a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")
---
 mm/madvise.c | 3 +++
 1 file changed, 3 insertions(+)
mm/madvise.c +0 -0
index c179938097bf0..2151bb9468594 100644

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

author maintainer
#4 lore raw

PATCH v2 3/3 mm/mempolicy: skip zone device folios when queueing folios

In reply to Gregory Price

queue_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).

Fixes a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")
---
 mm/mempolicy.c | 2 ++
 1 file changed, 2 insertions(+)
mm/mempolicy.c +2 -0
index 3498a5651d50f..3418b5664dcf8 100644
@@ -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
Reviewed-by on v2 3/3
#5 lore raw

Re: [PATCH v2 3/3] mm/mempolicy: skip zone device folios when queueing folios

Quoted 45 lines · Gregory Price

queue_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).

Fixes a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")
---
 mm/mempolicy.c | 2 ++
 1 file changed, 2 insertions(+)
mm/mempolicy.c +2 -0
index 3498a5651d50f..3418b5664dcf8 100644
@@ -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

author maintainer
#6 lore raw

Re: [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
index 3498a5651d50f..3418b5664dcf8 100644
@@ -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-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

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

maintainer Acked-by on v2 3/3
#7 lore raw

Re: [PATCH v2 3/3] mm/mempolicy: skip zone device folios when queueing folios

In reply to Gregory Price
Quoted 18 lines · Gregory Price

queue_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.

Quoted 7 lines

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).

Fixes a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")

Seems sensible so:

Quoted 18 lines
---
 mm/mempolicy.c | 2 ++
 1 file changed, 2 insertions(+)
mm/mempolicy.c +2 -1
index 3498a5651d50f..3418b5664dcf8 100644
@@ -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

maintainer Acked-by on v2 3/3
#8 lore raw

Re: [PATCH v2 3/3] mm/mempolicy: skip zone device folios when queueing folios

In reply to Gregory Price
Quoted 18 lines · Gregory Price

queue_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.

Quoted 8 lines

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).

Fixes a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")

---

Signature · 3 lines
Cheers,

David
reviewer Tested-by on the v2 series
#9 lore raw
In reply to Gregory Price
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_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.

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.

maintainer
#10 lore raw
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_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.

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.

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.

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]
reviewer
#11 lore raw
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_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.

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.

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.

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]
reviewer
#12 lore raw
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_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.

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.

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.

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_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.

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().

Fixes a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")
---
 mm/huge_memory.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
mm/huge_memory.c +11 -3
index 54494c3fa983..a5dd38e9a8de 100644
@@ -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

author maintainer
#13 lore raw
Quoted 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

#14 lore raw
In reply to Lance Yang
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_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.

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.

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.

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_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.

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().

Fixes a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")
---
 mm/huge_memory.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
mm/huge_memory.c +8 -3
index 54494c3fa983..a5dd38e9a8de 100644
@@ -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