* [PATCH v2 1/3] mm/huge_memory: skip zone device folios in madvise_free_huge_pmd()
2026-08-17 22:08 [PATCH v2 0/3] mm: reject zone device folios in more folio walkers Gregory Price
@ 2026-08-17 22:08 ` Gregory Price
2026-08-17 22:08 ` [PATCH v2 2/3] mm/madvise: skip zone device folios in cold/pageout PMD range Gregory Price
` (2 subsequent siblings)
3 siblings, 0 replies; 6+ messages in thread
From: Gregory Price @ 2026-08-17 22:08 UTC (permalink / raw)
To: linux-mm
Cc: linux-kernel, kernel-team, akpm, david, ljs, ziy, baolin.wang,
liam, nico.pache, ryan.roberts, dev.jain, baohua, lance.yang,
usama.arif, vbabka, jannh, matthew.brost, joshua.hahnjy,
rakie.kim, byungchul, gourry, ying.huang, apopple, balbirs,
stable
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")
Cc: stable@vger.kernel.org
Signed-off-by: Gregory Price (Meta) <gourry@gourry.net>
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
---
mm/huge_memory.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index ced400f72d43a..714773695bbff 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -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.
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH v2 2/3] mm/madvise: skip zone device folios in cold/pageout PMD range
2026-08-17 22:08 [PATCH v2 0/3] mm: reject zone device folios in more folio walkers Gregory Price
2026-08-17 22:08 ` [PATCH v2 1/3] mm/huge_memory: skip zone device folios in madvise_free_huge_pmd() Gregory Price
@ 2026-08-17 22:08 ` Gregory Price
2026-08-17 22:08 ` [PATCH v2 3/3] mm/mempolicy: skip zone device folios when queueing folios Gregory Price
2026-08-18 4:31 ` [PATCH v2 0/3] mm: reject zone device folios in more folio walkers Lance Yang
3 siblings, 0 replies; 6+ messages in thread
From: Gregory Price @ 2026-08-17 22:08 UTC (permalink / raw)
To: linux-mm
Cc: linux-kernel, kernel-team, akpm, david, ljs, ziy, baolin.wang,
liam, nico.pache, ryan.roberts, dev.jain, baohua, lance.yang,
usama.arif, vbabka, jannh, matthew.brost, joshua.hahnjy,
rakie.kim, byungchul, gourry, ying.huang, apopple, balbirs,
stable
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")
Cc: stable@vger.kernel.org
Signed-off-by: Gregory Price (Meta) <gourry@gourry.net>
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
---
mm/madvise.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/mm/madvise.c b/mm/madvise.c
index c179938097bf0..2151bb9468594 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -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;
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH v2 3/3] mm/mempolicy: skip zone device folios when queueing folios
2026-08-17 22:08 [PATCH v2 0/3] mm: reject zone device folios in more folio walkers Gregory Price
2026-08-17 22:08 ` [PATCH v2 1/3] mm/huge_memory: skip zone device folios in madvise_free_huge_pmd() Gregory Price
2026-08-17 22:08 ` [PATCH v2 2/3] mm/madvise: skip zone device folios in cold/pageout PMD range Gregory Price
@ 2026-08-17 22:08 ` Gregory Price
2026-08-17 23:34 ` Balbir Singh
2026-08-18 4:31 ` [PATCH v2 0/3] mm: reject zone device folios in more folio walkers Lance Yang
3 siblings, 1 reply; 6+ messages in thread
From: Gregory Price @ 2026-08-17 22:08 UTC (permalink / raw)
To: linux-mm
Cc: linux-kernel, kernel-team, akpm, david, ljs, ziy, baolin.wang,
liam, nico.pache, ryan.roberts, dev.jain, baohua, lance.yang,
usama.arif, vbabka, jannh, matthew.brost, joshua.hahnjy,
rakie.kim, byungchul, gourry, ying.huang, apopple, balbirs,
stable
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")
Cc: stable@vger.kernel.org
Signed-off-by: Gregory Price (Meta) <gourry@gourry.net>
---
mm/mempolicy.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 3498a5651d50f..3418b5664dcf8 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -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;
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 6+ messages in thread* Re: [PATCH v2 3/3] mm/mempolicy: skip zone device folios when queueing folios
2026-08-17 22:08 ` [PATCH v2 3/3] mm/mempolicy: skip zone device folios when queueing folios Gregory Price
@ 2026-08-17 23:34 ` Balbir Singh
0 siblings, 0 replies; 6+ messages in thread
From: Balbir Singh @ 2026-08-17 23:34 UTC (permalink / raw)
To: Gregory Price
Cc: linux-mm, linux-kernel, kernel-team, akpm, david, ljs, ziy,
baolin.wang, liam, nico.pache, ryan.roberts, dev.jain, baohua,
lance.yang, usama.arif, vbabka, jannh, matthew.brost,
joshua.hahnjy, rakie.kim, byungchul, ying.huang, apopple, stable
On Mon, Aug 17, 2026 at 06:08:10PM -0400, Gregory Price wrote:
> 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")
> Cc: stable@vger.kernel.org
> Signed-off-by: Gregory Price (Meta) <gourry@gourry.net>
> ---
> mm/mempolicy.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/mm/mempolicy.c b/mm/mempolicy.c
> index 3498a5651d50f..3418b5664dcf8 100644
> --- a/mm/mempolicy.c
> +++ b/mm/mempolicy.c
> @@ -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;
> --
> 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
Reviewed-by: Balbir Singh <balbirs@nvidia.com>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2 0/3] mm: reject zone device folios in more folio walkers
2026-08-17 22:08 [PATCH v2 0/3] mm: reject zone device folios in more folio walkers Gregory Price
` (2 preceding siblings ...)
2026-08-17 22:08 ` [PATCH v2 3/3] mm/mempolicy: skip zone device folios when queueing folios Gregory Price
@ 2026-08-18 4:31 ` Lance Yang
3 siblings, 0 replies; 6+ messages in thread
From: Lance Yang @ 2026-08-18 4:31 UTC (permalink / raw)
To: gourry
Cc: linux-mm, linux-kernel, kernel-team, akpm, david, ljs, ziy,
baolin.wang, liam, nico.pache, ryan.roberts, dev.jain, baohua,
usama.arif, vbabka, jannh, matthew.brost, joshua.hahnjy,
rakie.kim, byungchul, ying.huang, apopple, balbirs, Lance Yang
On Mon, Aug 17, 2026 at 06:08:07PM -0400, Gregory Price wrote:
>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.
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.
Tested-by: Lance Yang <lance.yang@linux.dev>
^ permalink raw reply [flat|nested] 6+ messages in thread