mm/page_vma_mapped: fix device-private PMD handling
Subsystems (3)
PATCH v4 1/1 mm/page_vma_mapped: fix device-private PMD handling
Commit 65edfda6f3f2 ("mm/rmap: extend rmap and migration support device-private entries") introduced the concept of device-private PMD entries, but did not correctly update the rmap walk code to account for them.
As a result, when page_vma_mapped_walk() encounters device-private PMD entries, it takes no action other than to acquire the PMD lock and exit.
However this is highly problematic for two reasons - firstly, device private entries possess a PFN so check_pmd() needs to be called to ensure an overlapping PFN range.
Secondly, and more importantly, if PVMW_MIGRATION is set the caller assumes the returned entry is a migration entry, resulting in memory corruption when the caller tries to interpret the device private entry as such.
In addition, commit 146287290023 ("mm/huge_memory: implement device-private THP splitting") allowed device private PMDs to be split like THP mappings, but again did not update this code path.
As a result, we might race a PMD split prior to acquiring the PMD lock.
This patch addresses all of these issues by invoking check_pmd(), ensuring PMVW_MIGRATION is not set and checks whether a split raced us we do for PMD THP and migration entries.
---
v4:
* refine subject and commit log based on Lorenzo's suggestion
* put pmd device-private entry handling in its own if branch, suggested by Lorenzo
v3:
* remove cleanup part, only fix the issue for device-private entry
* refine user effect description based on Lorenzo's suggestion
v2: https://lore.kernel.org/all/[email protected]/T/#u
* specify the possible error case of current code and user visible effect
* besides fix, cleanup the pmd entry handling based on David's suggestion
v1: https://lore.kernel.org/linux-mm/[email protected]/
--- mm/page_vma_mapped.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)
mm/page_vma_mapped.c +15 -5
@@ -269,14 +269,24 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)/* THP pmd was split under us: handle on pte level */ spin_unlock (pvmw->ptl); pvmw->ptl =NULL ; - }else if (!pmd_present (pmde)) { -const softleaf_t entry =softleaf_from_pmd (pmde); + }else if (pmd_is_device_private_entry (pmde)) { +softleaf_t entry; + + pvmw->ptl =pmd_lock (mm, pvmw->pmd); + pmde = *pvmw->pmd; + entry =softleaf_from_pmd (pmde); -if (softleaf_is_device_private (entry)) { - pvmw->ptl =pmd_lock (mm, pvmw->pmd); +if (likely (softleaf_is_device_private (entry))) { +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (!check_pmd (softleaf_to_pfn (entry), pvmw)) +return not_found (pvmw);return true; } - +/* device-private pmd was split under us: handle on pte level */ +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ; + }else if (!pmd_present (pmde)) { if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address, PMD_ORDER) &&
Signature · 1 line
2.34.1
Lance Yang
reviewerQuoted 25 lines · Wei Yang
Commit
65edfda6f3f2("mm/rmap: extend rmap and migration support device-private entries") introduced the concept of device-private PMD entries, but did not correctly update the rmap walk code to account for them.As a result, when
page_vma_mapped_walk()encounters device-private PMD entries, it takes no action other than to acquire the PMD lock and exit.However this is highly problematic for two reasons - firstly, device private entries possess a PFN so
check_pmd()needs to be called to ensure an overlapping PFN range.Secondly, and more importantly, if
PVMW_MIGRATIONis set the caller assumes the returned entry is a migration entry, resulting in memory corruption when the caller tries to interpret the device private entry as such.In addition, commit 146287290023 ("
mm/huge_memory: implement device-private THP splitting") allowed device private PMDs to be split like THP mappings, but again did not update this code path.As a result, we might race a PMD split prior to acquiring the PMD lock.
Quoted 8 lines
This patch addresses all of these issues by invoking
check_pmd(), ensuringPMVW_MIGRATIONis not set and checks whether a split raced us we do for PMD THP and migration entries.Fixes 65edfda6f3f2 ("mm/rmap: extend rmap and migration support device-private entries")Cc <[email protected]>Signed-off-by Wei Yang <[email protected]>Suggested-by David Hildenbrand <[email protected]>
Shouldn't we add
as well?
v4 mostly follows Lorenzo's comments, code bits included. Feels only fair.
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
Quoted 31 lines
Cc David Hildenbrand <[email protected]>Cc Balbir Singh <[email protected]>Cc SeongJae Park <[email protected]>Cc Zi Yan <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Lance Yang <[email protected]>---
v4:
* refine subject and commit log based on Lorenzo's suggestion
* put pmd device-private entry handling in its own if branch, suggested by Lorenzov3:
* remove cleanup part, only fix the issue for device-private entry
* refine user effect description based on Lorenzo's suggestionv2: https://lore.kernel.org/all/[email protected]/T/#u
* specify the possible error case of current code and user visible effect
* besides fix, cleanup the pmd entry handling based on David's suggestionv1: https://lore.kernel.org/linux-mm/[email protected]/
---mm/page_vma_mapped.c| 20 +++++++++++++++-----1 file changed, 15 insertions(+), 5 deletions(-)mm/page_vma_mapped.c +0
-0@@ -269,14 +269,24 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
Hmm ... looks like there may still be a race here ...
Current code picks the branch from the lockless PMD value:
pmde = pmdp_get_lockless(pvmw->pmd);
if (pmd_trans_huge(pmde) || pmd_is_migration_entry(pmde)) {
pvmw->ptl = pmd_lock(mm, pvmw->pmd);
pmde = *pvmw->pmd;
if (!pmd_present(pmde)) {
softleaf_t entry;
if (!thp_migration_supported() ||
!(pvmw->flags & PVMW_MIGRATION))
return not_found(pvmw);
entry = softleaf_from_pmd(pmde);
if (!softleaf_is_migration(entry) ||
!check_pmd(softleaf_to_pfn(entry), pvmw))
return not_found(pvmw);
return true;
}
}But after taking PTL, the PMD may already be a different non-present PMD type:
CPU0: pmde = pmdp_get_lockless(); // sees PMD migration entry
CPU1: remove_migration_ptes(src, dst /* device-private */)
... via rmap_walk(dst) ... page_vma_mapped_walk(&pvmw /* src, PVMW_MIGRATION */)
returns with PTL held for the PMD migration entry
remove_migration_pmd(new = dst page)
installs a device-private PMD
next page_vma_mapped_walk()
drops PTL via not_found()
CPU0: takes PTL
pmde = *pvmw->pmd; // now device-private PMD
So when PVMW_MIGRATION is not set, current code can return not_found() before we even decode the locked PMD as a device-private entry.
Commit 65edfda6f3f2 ("mm/rmap: extend rmap and migration support device-private entries") made the
device-private PMD <-> PMD migration
transition possible.
set_pmd_migration_entry() can replace a device-private PMD with a PMD migration entry, and remove_migration_pmd() can restore a PMD migration entry back to a device-private PMD when the new folio is device-private.
Maybe decode the locked softleaf entry first, before the migration-only checks? Something like this on top:
---8<---
mm/page_vma_mapped.c +13 -2
@@ -249,10 +249,18 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)if (!pmd_present (pmde)) {softleaf_t entry; + entry =softleaf_from_pmd (pmde); +if (softleaf_is_device_private (entry)) { +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (!check_pmd (softleaf_to_pfn (entry), pvmw)) +return not_found (pvmw); +return true; + } +if (!thp_migration_supported () || !(pvmw->flags &PVMW_MIGRATION ))return not_found (pvmw); - entry =softleaf_from_pmd (pmde);if (!softleaf_is_migration (entry) || !check_pmd (softleaf_to_pfn (entry), pvmw))
@@ -266,7 +274,10 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)return not_found (pvmw);return true; } -/* THP pmd was split under us: handle on pte level */ +/* +* THP pmd was split under us, or device-private PMD +* changed under us: handle on pte level. +*/ spin_unlock (pvmw->ptl); pvmw->ptl =NULL ; }else if (pmd_is_device_private_entry (pmde)) {
--
Anyway, that stuff is getting kinda messy now. Feels like it really needs a cleanup on top before it bites us again :)
Cheers, Lance
Quoted 33 lines
mm/page_vma_mapped.c +15
-5/* THP pmd was split under us: handle on pte level */ spin_unlock (pvmw->ptl); pvmw->ptl =NULL ; - }else if (!pmd_present (pmde)) { -const softleaf_t entry =softleaf_from_pmd (pmde); + }else if (pmd_is_device_private_entry (pmde)) { +softleaf_t entry; + + pvmw->ptl =pmd_lock (mm, pvmw->pmd); + pmde = *pvmw->pmd; + entry =softleaf_from_pmd (pmde); -if (softleaf_is_device_private (entry)) { - pvmw->ptl =pmd_lock (mm, pvmw->pmd); +if (likely (softleaf_is_device_private (entry))) { +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (!check_pmd (softleaf_to_pfn (entry), pvmw)) +return not_found (pvmw);return true; } - +/* device-private pmd was split under us: handle on pte level */ +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ; + }else if (!pmd_present (pmde)) { if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address, PMD_ORDER) &&Signature · 1 line
2.34.1
Quoted 36 lines · Lance Yang
Quoted 25 lines · Wei Yang
Commit
65edfda6f3f2("mm/rmap: extend rmap and migration support device-private entries") introduced the concept of device-private PMD entries, but did not correctly update the rmap walk code to account for them.As a result, when
page_vma_mapped_walk()encounters device-private PMD entries, it takes no action other than to acquire the PMD lock and exit.However this is highly problematic for two reasons - firstly, device private entries possess a PFN so
check_pmd()needs to be called to ensure an overlapping PFN range.Secondly, and more importantly, if
PVMW_MIGRATIONis set the caller assumes the returned entry is a migration entry, resulting in memory corruption when the caller tries to interpret the device private entry as such.In addition, commit 146287290023 ("
mm/huge_memory: implement device-private THP splitting") allowed device private PMDs to be split like THP mappings, but again did not update this code path.As a result, we might race a PMD split prior to acquiring the PMD lock.
Quoted 8 lines
This patch addresses all of these issues by invoking
check_pmd(), ensuringPMVW_MIGRATIONis not set and checks whether a split raced us we do for PMD THP and migration entries.Fixes 65edfda6f3f2 ("mm/rmap: extend rmap and migration support device-private entries")Cc <[email protected]>Signed-off-by Wei Yang <[email protected]>Suggested-by David Hildenbrand <[email protected]>
Quoted 7 lines
Shouldn't we add
Suggested-by Lorenzo Stoakes <[email protected]>as well?
v4 mostly follows Lorenzo's comments, code bits included. Feels only fair.
Fair enough, added.
Quoted 81 lines
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
Quoted 32 lines
Cc David Hildenbrand <[email protected]>Cc Balbir Singh <[email protected]>Cc SeongJae Park <[email protected]>Cc Zi Yan <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Lance Yang <[email protected]>---
v4:
* refine subject and commit log based on Lorenzo's suggestion
* put pmd device-private entry handling in its own if branch, suggested by Lorenzov3:
* remove cleanup part, only fix the issue for device-private entry
* refine user effect description based on Lorenzo's suggestionv2: https://lore.kernel.org/all/[email protected]/T/#u
* specify the possible error case of current code and user visible effect
* besides fix, cleanup the pmd entry handling based on David's suggestionv1: https://lore.kernel.org/linux-mm/[email protected]/
---mm/page_vma_mapped.c| 20 +++++++++++++++-----1 file changed, 15 insertions(+), 5 deletions(-)mm/page_vma_mapped.c +0
-0@@ -269,14 +269,24 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)Hmm ... looks like there may still be a race here ...
Current code picks the branch from the lockless PMD value:
pmde = pmdp_get_lockless(pvmw->pmd); if (pmd_trans_huge(pmde) || pmd_is_migration_entry(pmde)) { pvmw->ptl = pmd_lock(mm, pvmw->pmd); pmde = *pvmw->pmd; if (!pmd_present(pmde)) { softleaf_t entry; if (!thp_migration_supported() || !(pvmw->flags & PVMW_MIGRATION)) return not_found(pvmw); entry = softleaf_from_pmd(pmde); if (!softleaf_is_migration(entry) || !check_pmd(softleaf_to_pfn(entry), pvmw)) return not_found(pvmw); return true; } }But after taking PTL, the PMD may already be a different non-present PMD type:
CPU0: pmde =
pmdp_get_lockless(); // sees PMD migration entryCPU1:
remove_migration_ptes(src, dst /* device-private */)
... viarmap_walk(dst)...page_vma_mapped_walk(&pvmw /* src, PVMW_MIGRATION */)
returns with PTL held for the PMD migration entry
remove_migration_pmd(new = dst page)
installs a device-private PMD
nextpage_vma_mapped_walk()
drops PTL vianot_found()CPU0: takes PTL
pmde = *pvmw->pmd; // now device-private PMDSo when
PVMW_MIGRATIONis not set, current code can returnnot_found()before we even decode the locked PMD as a device-private entry.Commit
65edfda6f3f2("mm/rmap: extend rmap and migration support device-private entries") made the
Quoted 8 lines
device-private PMD <-> PMD migration
transition possible.
set_pmd_migration_entry()can replace a device-private PMD with a PMD migration entry, andremove_migration_pmd()can restore a PMD migration entry back to a device-private PMD when the new folio is device-private.
Nice catch.
But I think this matters if migration fail and restore the pmd to src folio. When we successfully migrate to new folio, check_pmd() could catch it and return not_found(). IIUC.
One more question: assume A unmap a folio, and B migrate the same one.
If B set_pmd_migration_entry() first, then A won't see this PMD from page_vma_mapped_walk(), IIUC. Then B failed to migrate, and restore the folio as this PMD migration entry is there. So A should check the status after unmap, right? Would it see unstable status?
I am a little lost what is the correct way to do here.
Quoted 11 lines
Maybe decode the locked softleaf entry first, before the migration-only checks? Something like this on top:
---8<---
mm/page_vma_mapped.c +0
-0@@ -249,10 +249,18 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)if (!pmd_present (pmde)) {softleaf_t entry;
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 260
Quoted 10 lines
mm/page_vma_mapped.c +9
-0+ entry =softleaf_from_pmd (pmde); +if (softleaf_is_device_private (entry)) { +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (!check_pmd (softleaf_to_pfn (entry), pvmw)) +return not_found (pvmw); +return true; + } +
If we have to do this, I am afraid we can put all three cases handling here...
Not necessary to put pmd_is_device_private_entry() handling in two places.
Quoted 21 lines
mm/page_vma_mapped.c +4
-3if (!thp_migration_supported () || !(pvmw->flags &PVMW_MIGRATION ))return not_found (pvmw); - entry =softleaf_from_pmd (pmde);if (!softleaf_is_migration (entry) || !check_pmd (softleaf_to_pfn (entry), pvmw))@@ -266,7 +274,10 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)return not_found (pvmw);return true; } -/* THP pmd was split under us: handle on pte level */ +/* +* THP pmd was split under us, or device-private PMD +* changed under us: handle on pte level. +*/ spin_unlock (pvmw->ptl); pvmw->ptl =NULL ; }else if (pmd_is_device_private_entry (pmde)) { --
Quoted 2 lines
Anyway, that stuff is getting kinda messy now. Feels like it really needs a cleanup on top before it bites us again :)
Agree.
I haven't imagined this would be more complicated than I thought :-)
Quoted 1 line
Cheers, Lance
Signature · 2 lines
Wei Yang Help you, Help me
David Hildenbrand (Arm)
maintainerQuoted 18 lines
Quoted 18 lines
CPU0: pmde =
pmdp_get_lockless(); // sees PMD migration entryCPU1:
remove_migration_ptes(src, dst /* device-private */)
... viarmap_walk(dst)...page_vma_mapped_walk(&pvmw /* src, PVMW_MIGRATION */)
returns with PTL held for the PMD migration entry
remove_migration_pmd(new = dst page)
installs a device-private PMD
nextpage_vma_mapped_walk()
drops PTL vianot_found()CPU0: takes PTL
pmde = *pvmw->pmd; // now device-private PMDSo when
PVMW_MIGRATIONis not set, current code can returnnot_found()before we even decode the locked PMD as a device-private entry.
Quoted 6 lines
Quoted 6 lines
Commit
65edfda6f3f2("mm/rmap: extend rmap and migration support device-private entries") made thedevice-private PMD <-> PMD migration
transition possible.
Doesn't the folio lock help here already?
Signature · 3 lines
Cheers, David
Lance Yang
reviewerQuoted 18 lines · David Hildenbrand (Arm)
Quoted 18 lines
Quoted 18 lines
CPU0: pmde =
pmdp_get_lockless(); // sees PMD migration entryCPU1:
remove_migration_ptes(src, dst /* device-private */)
... viarmap_walk(dst)...page_vma_mapped_walk(&pvmw /* src, PVMW_MIGRATION */)
returns with PTL held for the PMD migration entry
remove_migration_pmd(new = dst page)
installs a device-private PMD
nextpage_vma_mapped_walk()
drops PTL vianot_found()CPU0: takes PTL
pmde = *pvmw->pmd; // now device-private PMDSo when
PVMW_MIGRATIONis not set, current code can returnnot_found()before we even decode the locked PMD as a device-private entry.
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Commit
65edfda6f3f2("mm/rmap: extend rmap and migration support device-private entries") made thedevice-private PMD <-> PMD migration
transition possible.
Doesn't the folio lock help here already?
Ah, yeah, I was too focused on the PTL and missed the folio lock ... Don't have a caller like that :) Went over the fix again, nothing else jumped out.
Quoted 1 line · Lance Yang
[...]
Quoted 11 lines
Quoted 5 lines
Fixes 65edfda6f3f2 ("mm/rmap: extend rmap and migration support device-private entries")Cc <[email protected]>Signed-off-by Wei Yang <[email protected]>Suggested-by David Hildenbrand <[email protected]>Shouldn't we add
Suggested-by Lorenzo Stoakes <[email protected]>as well?
No need to resend. I think Andrew can add this when applying :)
Quoted 28 lines
v4 mostly follows Lorenzo's comments, code bits included. Feels only fair.
Quoted 26 lines
Cc David Hildenbrand <[email protected]>Cc Balbir Singh <[email protected]>Cc SeongJae Park <[email protected]>Cc Zi Yan <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Lance Yang <[email protected]>---
v4:
* refine subject and commit log based on Lorenzo's suggestion
* put pmd device-private entry handling in its own if branch, suggested by Lorenzov3:
* remove cleanup part, only fix the issue for device-private entry
* refine user effect description based on Lorenzo's suggestionv2: https://lore.kernel.org/all/[email protected]/T/#u
* specify the possible error case of current code and user visible effect
* besides fix, cleanup the pmd entry handling based on David's suggestionv1: https://lore.kernel.org/linux-mm/[email protected]/
---mm/page_vma_mapped.c| 20 +++++++++++++++-----1 file changed, 15 insertions(+), 5 deletions(-)
Quoted 6 lines
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
Quoted 5 lines
mm/page_vma_mapped.c +0
-0@@ -269,14 +269,24 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
Never mind my race comment below. Obviously missed folio lock there. My bad. Don't have a caller like that. Nothing else jumped out, so:
Cheers, Lance
Quoted 137 lines
Hmm ... looks like there may still be a race here ...
Current code picks the branch from the lockless PMD value:
pmde = pmdp_get_lockless(pvmw->pmd); if (pmd_trans_huge(pmde) || pmd_is_migration_entry(pmde)) { pvmw->ptl = pmd_lock(mm, pvmw->pmd); pmde = *pvmw->pmd; if (!pmd_present(pmde)) { softleaf_t entry; if (!thp_migration_supported() || !(pvmw->flags & PVMW_MIGRATION)) return not_found(pvmw); entry = softleaf_from_pmd(pmde); if (!softleaf_is_migration(entry) || !check_pmd(softleaf_to_pfn(entry), pvmw)) return not_found(pvmw); return true; } }But after taking PTL, the PMD may already be a different non-present PMD type:
CPU0: pmde =
pmdp_get_lockless(); // sees PMD migration entryCPU1:
remove_migration_ptes(src, dst /* device-private */)
... viarmap_walk(dst)...page_vma_mapped_walk(&pvmw /* src, PVMW_MIGRATION */)
returns with PTL held for the PMD migration entry
remove_migration_pmd(new = dst page)
installs a device-private PMD
nextpage_vma_mapped_walk()
drops PTL vianot_found()CPU0: takes PTL
pmde = *pvmw->pmd; // now device-private PMDSo when
PVMW_MIGRATIONis not set, current code can returnnot_found()before we even decode the locked PMD as a device-private entry.Commit
65edfda6f3f2("mm/rmap: extend rmap and migration support device-private entries") made thedevice-private PMD <-> PMD migration
transition possible.
set_pmd_migration_entry()can replace a device-private PMD with a PMD migration entry, andremove_migration_pmd()can restore a PMD migration entry back to a device-private PMD when the new folio is device-private.Maybe decode the locked softleaf entry first, before the migration-only checks? Something like this on top:
---8<---
mm/page_vma_mapped.c +13
-3@@ -249,10 +249,18 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)if (!pmd_present (pmde)) {softleaf_t entry; + entry =softleaf_from_pmd (pmde); +if (softleaf_is_device_private (entry)) { +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (!check_pmd (softleaf_to_pfn (entry), pvmw)) +return not_found (pvmw); +return true; + } +if (!thp_migration_supported () || !(pvmw->flags &PVMW_MIGRATION ))return not_found (pvmw); - entry =softleaf_from_pmd (pmde);if (!softleaf_is_migration (entry) || !check_pmd (softleaf_to_pfn (entry), pvmw))@@ -266,7 +274,10 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)return not_found (pvmw);return true; } -/* THP pmd was split under us: handle on pte level */ +/* +* THP pmd was split under us, or device-private PMD +* changed under us: handle on pte level. +*/ spin_unlock (pvmw->ptl); pvmw->ptl =NULL ; }else if (pmd_is_device_private_entry (pmde)) { --Anyway, that stuff is getting kinda messy now. Feels like it really needs a cleanup on top before it bites us again :)
Cheers, Lance
Quoted 33 lines
mm/page_vma_mapped.c +15
-5/* THP pmd was split under us: handle on pte level */ spin_unlock (pvmw->ptl); pvmw->ptl =NULL ; - }else if (!pmd_present (pmde)) { -const softleaf_t entry =softleaf_from_pmd (pmde); + }else if (pmd_is_device_private_entry (pmde)) { +softleaf_t entry; + + pvmw->ptl =pmd_lock (mm, pvmw->pmd); + pmde = *pvmw->pmd; + entry =softleaf_from_pmd (pmde); -if (softleaf_is_device_private (entry)) { - pvmw->ptl =pmd_lock (mm, pvmw->pmd); +if (likely (softleaf_is_device_private (entry))) { +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (!check_pmd (softleaf_to_pfn (entry), pvmw)) +return not_found (pvmw);return true; } - +/* device-private pmd was split under us: handle on pte level */ +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ; + }else if (!pmd_present (pmde)) { if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address, PMD_ORDER) &&Signature · 1 line
2.34.1
Andrew Morton
maintainerQuoted 5 lines · On Thu, 25 Jun 2026 19:42:35 +0800 Lance Yang <[email protected]> wrote:
Quoted 3 lines
Suggested-by Lorenzo Stoakes <[email protected]>as well?
No need to resend. I think Andrew can add this when applying :)
I managed.
Thanks, I'll queue this as a hotfix. Somewhat tentatively - it's unclear whether we'll be seeing a v2?
Lorenzo Stoakes
maintainerQuoted 36 lines · Lance Yang
Quoted 25 lines · Wei Yang
Commit
65edfda6f3f2("mm/rmap: extend rmap and migration support device-private entries") introduced the concept of device-private PMD entries, but did not correctly update the rmap walk code to account for them.As a result, when
page_vma_mapped_walk()encounters device-private PMD entries, it takes no action other than to acquire the PMD lock and exit.However this is highly problematic for two reasons - firstly, device private entries possess a PFN so
check_pmd()needs to be called to ensure an overlapping PFN range.Secondly, and more importantly, if
PVMW_MIGRATIONis set the caller assumes the returned entry is a migration entry, resulting in memory corruption when the caller tries to interpret the device private entry as such.In addition, commit 146287290023 ("
mm/huge_memory: implement device-private THP splitting") allowed device private PMDs to be split like THP mappings, but again did not update this code path.As a result, we might race a PMD split prior to acquiring the PMD lock.
Quoted 8 lines
This patch addresses all of these issues by invoking
check_pmd(), ensuringPMVW_MIGRATIONis not set and checks whether a split raced us we do for PMD THP and migration entries.Fixes 65edfda6f3f2 ("mm/rmap: extend rmap and migration support device-private entries")Cc <[email protected]>Signed-off-by Wei Yang <[email protected]>Suggested-by David Hildenbrand <[email protected]>
Quoted 7 lines
Shouldn't we add
Suggested-by Lorenzo Stoakes <[email protected]>as well?
v4 mostly follows Lorenzo's comments, code bits included. Feels only fair.
Thanks Lance :)
I'm kinda indifferent about it really, I'm really keen to ensure people sending patches get the credit for their work, so if I send a patch in reply as a shorthand for 'I think this might work better', I don't expect/require any credit at all, it's just sometimes a quicker way of responding!
But if Wei wants to add a S-b that's fine by me also! :)
Cheers, Lorenzo
Quoted 22 lines · Wei Yang
Commit
65edfda6f3f2("mm/rmap: extend rmap and migration support device-private entries") introduced the concept of device-private PMD entries, but did not correctly update the rmap walk code to account for them.As a result, when
page_vma_mapped_walk()encounters device-private PMD entries, it takes no action other than to acquire the PMD lock and exit.However this is highly problematic for two reasons - firstly, device private entries possess a PFN so
check_pmd()needs to be called to ensure an overlapping PFN range.Secondly, and more importantly, if
PVMW_MIGRATIONis set the caller assumes the returned entry is a migration entry, resulting in memory corruption when the caller tries to interpret the device private entry as such.In addition, commit 146287290023 ("
mm/huge_memory: implement device-private THP splitting") allowed device private PMDs to be split like THP mappings, but again did not update this code path.
Quoted 6 lines
As a result, we might race a PMD split prior to acquiring the PMD lock.
This patch addresses all of these issues by invoking
check_pmd(), ensuringPMVW_MIGRATIONis not set and checks whether a split raced us we do for PMD THP and migration entries.
Should be PVMW_MIGRATION and "us we do" -> "as we do"
Quoted 55 lines
Fixes 65edfda6f3f2 ("mm/rmap: extend rmap and migration support device-private entries")Cc <[email protected]>Signed-off-by Wei Yang <[email protected]>Suggested-by David Hildenbrand <[email protected]>Cc David Hildenbrand <[email protected]>Cc Balbir Singh <[email protected]>Cc SeongJae Park <[email protected]>Cc Zi Yan <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Lance Yang <[email protected]>---
v4:
* refine subject and commit log based on Lorenzo's suggestion
* put pmd device-private entry handling in its own if branch, suggested by Lorenzov3:
* remove cleanup part, only fix the issue for device-private entry
* refine user effect description based on Lorenzo's suggestionv2: https://lore.kernel.org/all/[email protected]/T/#u
* specify the possible error case of current code and user visible effect
* besides fix, cleanup the pmd entry handling based on David's suggestionv1: https://lore.kernel.org/linux-mm/[email protected]/
--- mm/page_vma_mapped.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)mm/page_vma_mapped.c +11
-4@@ -269,14 +269,24 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)/* THP pmd was split under us: handle on pte level */ spin_unlock (pvmw->ptl); pvmw->ptl =NULL ; - }else if (!pmd_present (pmde)) { -const softleaf_t entry =softleaf_from_pmd (pmde); + }else if (pmd_is_device_private_entry (pmde)) { +softleaf_t entry; + + pvmw->ptl =pmd_lock (mm, pvmw->pmd); + pmde = *pvmw->pmd; + entry =softleaf_from_pmd (pmde); -if (softleaf_is_device_private (entry)) { - pvmw->ptl =pmd_lock (mm, pvmw->pmd); +if (likely (softleaf_is_device_private (entry))) { +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (!check_pmd (softleaf_to_pfn (entry), pvmw)) +return not_found (pvmw);
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 292
Quoted 10 lines
mm/page_vma_mapped.c +4
-1return true; } - +/* device-private pmd was split under us: handle on pte level */ +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ; + }else if (!pmd_present (pmde)) { if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address, PMD_ORDER) &&
I looked at comments from Lance on "device-private PMD <-> PMD migration" and had the same comment as David
Balbir
Quoted 23 lines · Balbir Singh
Quoted 22 lines · Wei Yang
Commit
65edfda6f3f2("mm/rmap: extend rmap and migration support device-private entries") introduced the concept of device-private PMD entries, but did not correctly update the rmap walk code to account for them.As a result, when
page_vma_mapped_walk()encounters device-private PMD entries, it takes no action other than to acquire the PMD lock and exit.However this is highly problematic for two reasons - firstly, device private entries possess a PFN so
check_pmd()needs to be called to ensure an overlapping PFN range.Secondly, and more importantly, if
PVMW_MIGRATIONis set the caller assumes the returned entry is a migration entry, resulting in memory corruption when the caller tries to interpret the device private entry as such.In addition, commit 146287290023 ("
mm/huge_memory: implement device-private THP splitting") allowed device private PMDs to be split like THP mappings, but again did not update this code path.
Quoted 9 lines
Quoted 6 lines
As a result, we might race a PMD split prior to acquiring the PMD lock.
This patch addresses all of these issues by invoking
check_pmd(), ensuringPMVW_MIGRATIONis not set and checks whether a split raced us we do for PMD THP and migration entries.Should be
PVMW_MIGRATIONand "us we do" -> "as we do"
Hi, Balbir
Sorry for missing your comment.
Hmm... looks you are right.
Andrew,
Would you mind handling it or prefer a v2?
Signature · 2 lines
Wei Yang Help you, Help me
Andrew Morton
maintainerQuoted 5 lines · On Fri, 26 Jun 2026 00:44:16 +0000 Wei Yang <[email protected]> wrote:
Quoted 2 lines
Should be
PVMW_MIGRATIONand "us we do" -> "as we do"Hi, Balbir
Quoted 7 lines
Sorry for missing your comment.
Hmm... looks you are right.
Andrew,
Would you mind handling it or prefer a v2?
I have made those edits.
Quoted 50 lines · Wei Yang
Commit
65edfda6f3f2("mm/rmap: extend rmap and migration support device-private entries") introduced the concept of device-private PMD entries, but did not correctly update the rmap walk code to account for them.As a result, when
page_vma_mapped_walk()encounters device-private PMD entries, it takes no action other than to acquire the PMD lock and exit.However this is highly problematic for two reasons - firstly, device private entries possess a PFN so
check_pmd()needs to be called to ensure an overlapping PFN range.Secondly, and more importantly, if
PVMW_MIGRATIONis set the caller assumes the returned entry is a migration entry, resulting in memory corruption when the caller tries to interpret the device private entry as such.In addition, commit 146287290023 ("
mm/huge_memory: implement device-private THP splitting") allowed device private PMDs to be split like THP mappings, but again did not update this code path.As a result, we might race a PMD split prior to acquiring the PMD lock.
This patch addresses all of these issues by invoking
check_pmd(), ensuringPMVW_MIGRATIONis not set and checks whether a split raced us we do for PMD THP and migration entries.Fixes 65edfda6f3f2 ("mm/rmap: extend rmap and migration support device-private entries")Cc <[email protected]>Signed-off-by Wei Yang <[email protected]>Suggested-by David Hildenbrand <[email protected]>Cc David Hildenbrand <[email protected]>Cc Balbir Singh <[email protected]>Cc SeongJae Park <[email protected]>Cc Zi Yan <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Lance Yang <[email protected]>---
v4:
* refine subject and commit log based on Lorenzo's suggestion
* put pmd device-private entry handling in its own if branch, suggested by Lorenzov3:
* remove cleanup part, only fix the issue for device-private entry
* refine user effect description based on Lorenzo's suggestion
Quoted 9 lines
v2: https://lore.kernel.org/all/[email protected]/T/#u
* specify the possible error case of current code and user visible effect
* besides fix, cleanup the pmd entry handling based on David's suggestionv1: https://lore.kernel.org/linux-mm/[email protected]/
--- mm/page_vma_mapped.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)
LGTM. The discussion from the patch history is very valuable. Thanks.
Signature · 2 lines
Best Regards, Yan, Zi
David Hildenbrand (Arm)
maintainerQuoted 83 lines · Wei Yang
Commit
65edfda6f3f2("mm/rmap: extend rmap and migration support device-private entries") introduced the concept of device-private PMD entries, but did not correctly update the rmap walk code to account for them.As a result, when
page_vma_mapped_walk()encounters device-private PMD entries, it takes no action other than to acquire the PMD lock and exit.However this is highly problematic for two reasons - firstly, device private entries possess a PFN so
check_pmd()needs to be called to ensure an overlapping PFN range.Secondly, and more importantly, if
PVMW_MIGRATIONis set the caller assumes the returned entry is a migration entry, resulting in memory corruption when the caller tries to interpret the device private entry as such.In addition, commit 146287290023 ("
mm/huge_memory: implement device-private THP splitting") allowed device private PMDs to be split like THP mappings, but again did not update this code path.As a result, we might race a PMD split prior to acquiring the PMD lock.
This patch addresses all of these issues by invoking
check_pmd(), ensuringPMVW_MIGRATIONis not set and checks whether a split raced us we do for PMD THP and migration entries.Fixes 65edfda6f3f2 ("mm/rmap: extend rmap and migration support device-private entries")Cc <[email protected]>Signed-off-by Wei Yang <[email protected]>Suggested-by David Hildenbrand <[email protected]>Cc David Hildenbrand <[email protected]>Cc Balbir Singh <[email protected]>Cc SeongJae Park <[email protected]>Cc Zi Yan <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Lance Yang <[email protected]>---
v4:
* refine subject and commit log based on Lorenzo's suggestion
* put pmd device-private entry handling in its own if branch, suggested by Lorenzov3:
* remove cleanup part, only fix the issue for device-private entry
* refine user effect description based on Lorenzo's suggestionv2: https://lore.kernel.org/all/[email protected]/T/#u
* specify the possible error case of current code and user visible effect
* besides fix, cleanup the pmd entry handling based on David's suggestionv1: https://lore.kernel.org/linux-mm/[email protected]/
--- mm/page_vma_mapped.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)mm/page_vma_mapped.c +11
-4@@ -269,14 +269,24 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)/* THP pmd was split under us: handle on pte level */ spin_unlock (pvmw->ptl); pvmw->ptl =NULL ; - }else if (!pmd_present (pmde)) { -const softleaf_t entry =softleaf_from_pmd (pmde); + }else if (pmd_is_device_private_entry (pmde)) { +softleaf_t entry; + + pvmw->ptl =pmd_lock (mm, pvmw->pmd); + pmde = *pvmw->pmd; + entry =softleaf_from_pmd (pmde); -if (softleaf_is_device_private (entry)) { - pvmw->ptl =pmd_lock (mm, pvmw->pmd); +if (likely (softleaf_is_device_private (entry))) { +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (!check_pmd (softleaf_to_pfn (entry), pvmw)) +return not_found (pvmw);
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 292
Quoted 10 lines
mm/page_vma_mapped.c +4
-1return true; } - +/* device-private pmd was split under us: handle on pte level */ +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ; + }else if (!pmd_present (pmde)) { if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address, PMD_ORDER) &&
This is extremely hard to review given the existing crap handling here. I'm really sorry, but it makes my head hurt (I'm not kidding :) ).
It's completely unclear why we only have to check for a subset of the cases after taking the lock.
Could we simply extend the existing migration pmd handling and leave the !pmd_present() case for pmd_none()?
That leaves no question to "which transitions are actually allowed", including "could we accidentally assume something is a page table when really it isn't".
So what about something like the following?
The "thp_migration_supported()" is not required when checking for pmd_is_migration_entry(), as that defaults to "false" when not compiled in.
Untested:
From 048ecd33673ec649e168fbbb97749a7c0e344fcd Mon Sep 17 00:00:00 2001
From: "David Hildenbrand (Arm)" <[email protected]>
Date: Fri, 26 Jun 2026 12:03:40 +0200
Subject: [PATCH] tmp
--- mm/page_vma_mapped.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-)
mm/page_vma_mapped.c +15 -12
@@ -243,21 +243,31 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)*/ pmde =pmdp_get_lockless (pvmw->pmd); -if (pmd_trans_huge (pmde) ||pmd_is_migration_entry (pmde)) { +if (pmd_trans_huge (pmde) ||pmd_is_migration_entry (pmde) || +pmd_is_device_private_entry (pmde)) { pvmw->ptl =pmd_lock (mm, pvmw->pmd); pmde = *pvmw->pmd; -if (!pmd_present (pmde)) { +if (pmd_is_migration_entry (pmde)) {softleaf_t entry; -if (!thp_migration_supported () || - !(pvmw->flags &PVMW_MIGRATION )) +if (!(pvmw->flags &PVMW_MIGRATION ))return not_found (pvmw); entry =softleaf_from_pmd (pmde); +if (!check_pmd (softleaf_to_pfn (entry), pvmw)) +return not_found (pvmw); +return true; + }else if (pmd_is_device_private_entry (pmde)) { +softleaf_t entry; -if (!softleaf_is_migration (entry) || - !check_pmd (softleaf_to_pfn (entry), pvmw)) +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); + entry =softleaf_from_pmd (pmde); +if (!check_pmd (softleaf_to_pfn (entry), pvmw))return not_found (pvmw);return true; + }else if (!pmd_present (pmde) ){ +return not_found (pvmw); }if (likely (pmd_trans_huge (pmde))) {if (pvmw->flags &PVMW_MIGRATION )
@@ -270,12 +280,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)spin_unlock (pvmw->ptl); pvmw->ptl =NULL ; } else if (!pmd_present(pmde)) { - const softleaf_t entry = softleaf_from_pmd(pmde); - - if (softleaf_is_device_private(entry)) { - pvmw->ptl = pmd_lock(mm, pvmw->pmd); - return true; - } if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address,
Signature · 7 lines
2.43.0 -- Cheers, David
Lorenzo Stoakes
maintainerQuoted 134 lines · David Hildenbrand (Arm)
Quoted 83 lines · Wei Yang
Commit
65edfda6f3f2("mm/rmap: extend rmap and migration support device-private entries") introduced the concept of device-private PMD entries, but did not correctly update the rmap walk code to account for them.As a result, when
page_vma_mapped_walk()encounters device-private PMD entries, it takes no action other than to acquire the PMD lock and exit.However this is highly problematic for two reasons - firstly, device private entries possess a PFN so
check_pmd()needs to be called to ensure an overlapping PFN range.Secondly, and more importantly, if
PVMW_MIGRATIONis set the caller assumes the returned entry is a migration entry, resulting in memory corruption when the caller tries to interpret the device private entry as such.In addition, commit 146287290023 ("
mm/huge_memory: implement device-private THP splitting") allowed device private PMDs to be split like THP mappings, but again did not update this code path.As a result, we might race a PMD split prior to acquiring the PMD lock.
This patch addresses all of these issues by invoking
check_pmd(), ensuringPMVW_MIGRATIONis not set and checks whether a split raced us we do for PMD THP and migration entries.Fixes 65edfda6f3f2 ("mm/rmap: extend rmap and migration support device-private entries")Cc <[email protected]>Signed-off-by Wei Yang <[email protected]>Suggested-by David Hildenbrand <[email protected]>Cc David Hildenbrand <[email protected]>Cc Balbir Singh <[email protected]>Cc SeongJae Park <[email protected]>Cc Zi Yan <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Lance Yang <[email protected]>---
v4:
* refine subject and commit log based on Lorenzo's suggestion
* put pmd device-private entry handling in its own if branch, suggested by Lorenzov3:
* remove cleanup part, only fix the issue for device-private entry
* refine user effect description based on Lorenzo's suggestionv2: https://lore.kernel.org/all/[email protected]/T/#u
* specify the possible error case of current code and user visible effect
* besides fix, cleanup the pmd entry handling based on David's suggestionv1: https://lore.kernel.org/linux-mm/[email protected]/
--- mm/page_vma_mapped.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)mm/page_vma_mapped.c +11
-4@@ -269,14 +269,24 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)/* THP pmd was split under us: handle on pte level */ spin_unlock (pvmw->ptl); pvmw->ptl =NULL ; - }else if (!pmd_present (pmde)) { -const softleaf_t entry =softleaf_from_pmd (pmde); + }else if (pmd_is_device_private_entry (pmde)) { +softleaf_t entry; + + pvmw->ptl =pmd_lock (mm, pvmw->pmd); + pmde = *pvmw->pmd; + entry =softleaf_from_pmd (pmde); -if (softleaf_is_device_private (entry)) { - pvmw->ptl =pmd_lock (mm, pvmw->pmd); +if (likely (softleaf_is_device_private (entry))) { +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (!check_pmd (softleaf_to_pfn (entry), pvmw)) +return not_found (pvmw);mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 292
Quoted 10 lines
mm/page_vma_mapped.c +4
-1return true; } - +/* device-private pmd was split under us: handle on pte level */ +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ; + }else if (!pmd_present (pmde)) { if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address, PMD_ORDER) &&This is extremely hard to review given the existing crap handling here. I'm really sorry, but it makes my head hurt (I'm not kidding :) ).
It's completely unclear why we only have to check for a subset of the cases after taking the lock.
Could we simply extend the existing migration pmd handling and leave the !pmd_present() case for
pmd_none()?That leaves no question to "which transitions are actually allowed", including "could we accidentally assume something is a page table when really it isn't".
So what about something like the following?
The "
thp_migration_supported()" is not required when checking forpmd_is_migration_entry(), as that defaults to "false" when not compiled in.Untested:
From
048ecd33673ec649e168fbbb97749a7c0e344fcdMon Sep 17 00:00:00 2001
From: "David Hildenbrand (Arm)" <[email protected]>
Date: Fri, 26 Jun 2026 12:03:40 +0200
Subject: [PATCH] tmpSigned-off-by David Hildenbrand (Arm) <[email protected]>--- mm/page_vma_mapped.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-)mm/page_vma_mapped.c +0
-0@@ -243,21 +243,31 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)*/ pmde =pmdp_get_lockless (pvmw->pmd);
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 252
Quoted 10 lines
mm/page_vma_mapped.c +3
-3-if (pmd_trans_huge (pmde) ||pmd_is_migration_entry (pmde)) { +if (pmd_trans_huge (pmde) ||pmd_is_migration_entry (pmde) || +pmd_is_device_private_entry (pmde)) { pvmw->ptl =pmd_lock (mm, pvmw->pmd); pmde = *pvmw->pmd; -if (!pmd_present (pmde)) { +if (pmd_is_migration_entry (pmde)) {softleaf_t entry; - if (!thp_migration_supported() ||
Do we care about this? Or is !tmp_migration_supported() -> implies you wouldn't see a migration entry here anyway?
Maybe worth a VM_WARN_ON_ONCE()?
Quoted 26 lines
mm/page_vma_mapped.c +12
-3- !(pvmw->flags & PVMW_MIGRATION)) +if (!(pvmw->flags &PVMW_MIGRATION ))return not_found (pvmw); entry =softleaf_from_pmd (pmde); +if (!check_pmd (softleaf_to_pfn (entry), pvmw)) +return not_found (pvmw); +return true; + }else if (pmd_is_device_private_entry (pmde)) { +softleaf_t entry; -if (!softleaf_is_migration (entry) || - !check_pmd (softleaf_to_pfn (entry), pvmw)) +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); + entry =softleaf_from_pmd (pmde); +if (!check_pmd (softleaf_to_pfn (entry), pvmw))return not_found (pvmw);return true; + }else if (!pmd_present (pmde) ){ +return not_found (pvmw); }if (likely (pmd_trans_huge (pmde))) {if (pvmw->flags &PVMW_MIGRATION )@@ -270,12 +280,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)spin_unlock (pvmw->ptl); pvmw->ptl =NULL ;
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 285
Quoted 10 lines
mm/page_vma_mapped.c +0
-6} else if (!pmd_present(pmde)) { - const softleaf_t entry = softleaf_from_pmd(pmde); - - if (softleaf_is_device_private(entry)) { - pvmw->ptl = pmd_lock(mm, pvmw->pmd); - return true; - } if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address,
Overall though this seems fine to me.
Quoted 8 lines
--
2.43.0--
Cheers,David
Thanks, Lorenzo
David Hildenbrand (Arm)
maintainerQuoted 144 lines · Lorenzo Stoakes
Quoted 143 lines · David Hildenbrand (Arm)
Quoted 83 lines · Wei Yang
Commit
65edfda6f3f2("mm/rmap: extend rmap and migration support device-private entries") introduced the concept of device-private PMD entries, but did not correctly update the rmap walk code to account for them.As a result, when
page_vma_mapped_walk()encounters device-private PMD entries, it takes no action other than to acquire the PMD lock and exit.However this is highly problematic for two reasons - firstly, device private entries possess a PFN so
check_pmd()needs to be called to ensure an overlapping PFN range.Secondly, and more importantly, if
PVMW_MIGRATIONis set the caller assumes the returned entry is a migration entry, resulting in memory corruption when the caller tries to interpret the device private entry as such.In addition, commit 146287290023 ("
mm/huge_memory: implement device-private THP splitting") allowed device private PMDs to be split like THP mappings, but again did not update this code path.As a result, we might race a PMD split prior to acquiring the PMD lock.
This patch addresses all of these issues by invoking
check_pmd(), ensuringPMVW_MIGRATIONis not set and checks whether a split raced us we do for PMD THP and migration entries.Fixes 65edfda6f3f2 ("mm/rmap: extend rmap and migration support device-private entries")Cc <[email protected]>Signed-off-by Wei Yang <[email protected]>Suggested-by David Hildenbrand <[email protected]>Cc David Hildenbrand <[email protected]>Cc Balbir Singh <[email protected]>Cc SeongJae Park <[email protected]>Cc Zi Yan <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Lance Yang <[email protected]>---
v4:
* refine subject and commit log based on Lorenzo's suggestion
* put pmd device-private entry handling in its own if branch, suggested by Lorenzov3:
* remove cleanup part, only fix the issue for device-private entry
* refine user effect description based on Lorenzo's suggestionv2: https://lore.kernel.org/all/[email protected]/T/#u
* specify the possible error case of current code and user visible effect
* besides fix, cleanup the pmd entry handling based on David's suggestionv1: https://lore.kernel.org/linux-mm/[email protected]/
--- mm/page_vma_mapped.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)mm/page_vma_mapped.c +11
-4@@ -269,14 +269,24 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)/* THP pmd was split under us: handle on pte level */ spin_unlock (pvmw->ptl); pvmw->ptl =NULL ; - }else if (!pmd_present (pmde)) { -const softleaf_t entry =softleaf_from_pmd (pmde); + }else if (pmd_is_device_private_entry (pmde)) { +softleaf_t entry; + + pvmw->ptl =pmd_lock (mm, pvmw->pmd); + pmde = *pvmw->pmd; + entry =softleaf_from_pmd (pmde); -if (softleaf_is_device_private (entry)) { - pvmw->ptl =pmd_lock (mm, pvmw->pmd); +if (likely (softleaf_is_device_private (entry))) { +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (!check_pmd (softleaf_to_pfn (entry), pvmw)) +return not_found (pvmw);mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 292
Quoted 10 lines
mm/page_vma_mapped.c +4
-1return true; } - +/* device-private pmd was split under us: handle on pte level */ +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ; + }else if (!pmd_present (pmde)) { if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address, PMD_ORDER) &&This is extremely hard to review given the existing crap handling here. I'm really sorry, but it makes my head hurt (I'm not kidding :) ).
It's completely unclear why we only have to check for a subset of the cases after taking the lock.
Could we simply extend the existing migration pmd handling and leave the !pmd_present() case for
pmd_none()?That leaves no question to "which transitions are actually allowed", including "could we accidentally assume something is a page table when really it isn't".
So what about something like the following?
The "
thp_migration_supported()" is not required when checking forpmd_is_migration_entry(), as that defaults to "false" when not compiled in.Untested:
From
048ecd33673ec649e168fbbb97749a7c0e344fcdMon Sep 17 00:00:00 2001
From: "David Hildenbrand (Arm)" <[email protected]>
Date: Fri, 26 Jun 2026 12:03:40 +0200
Subject: [PATCH] tmpSigned-off-by David Hildenbrand (Arm) <[email protected]>--- mm/page_vma_mapped.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-)mm/page_vma_mapped.c +3
-2@@ -243,21 +243,31 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)*/ pmde =pmdp_get_lockless (pvmw->pmd); -if (pmd_trans_huge (pmde) ||pmd_is_migration_entry (pmde)) { +if (pmd_trans_huge (pmde) ||pmd_is_migration_entry (pmde) || +pmd_is_device_private_entry (pmde)) { pvmw->ptl =pmd_lock (mm, pvmw->pmd); pmde = *pvmw->pmd; -if (!pmd_present (pmde)) { +if (pmd_is_migration_entry (pmde)) {softleaf_t entry;
Quoted 4 lines
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 252
Quoted 1 line
mm/page_vma_mapped.c +0
-1- if (!thp_migration_supported() ||Do we care about this? Or is !tmp_migration_supported() -> implies you wouldn't see a migration entry here anyway?
Yeah, I noted above
"The "thp_migration_supported()" is not required when checking for pmd_is_migration_entry(), as that defaults to "false" when not compiled in."
Given that
tmp_migration_supported() -> IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION);$
And
pmd_is_migration_entry() -> softleaf_is_migration(softleaf_from_pmd(pmd));
whereby softleaf_from_pmd() only returns something non-none for CONFIG_ARCH_ENABLE_THP_MIGRATION.
Quoted 2 lines
Maybe worth a
VM_WARN_ON_ONCE()?
I think it was primarily a a hack to slightly optimize code generated for !CONFIG_ARCH_ENABLE_THP_MIGRATION, not really something for correctness as it seems.
So I think we can safely drop it. :)
Signature · 3 lines
Cheers, David
Quoted 167 lines · David Hildenbrand (Arm)
Quoted 144 lines · Lorenzo Stoakes
Quoted 143 lines · David Hildenbrand (Arm)
Quoted 83 lines · Wei Yang
Commit
65edfda6f3f2("mm/rmap: extend rmap and migration support device-private entries") introduced the concept of device-private PMD entries, but did not correctly update the rmap walk code to account for them.As a result, when
page_vma_mapped_walk()encounters device-private PMD entries, it takes no action other than to acquire the PMD lock and exit.However this is highly problematic for two reasons - firstly, device private entries possess a PFN so
check_pmd()needs to be called to ensure an overlapping PFN range.Secondly, and more importantly, if
PVMW_MIGRATIONis set the caller assumes the returned entry is a migration entry, resulting in memory corruption when the caller tries to interpret the device private entry as such.In addition, commit 146287290023 ("
mm/huge_memory: implement device-private THP splitting") allowed device private PMDs to be split like THP mappings, but again did not update this code path.As a result, we might race a PMD split prior to acquiring the PMD lock.
This patch addresses all of these issues by invoking
check_pmd(), ensuringPMVW_MIGRATIONis not set and checks whether a split raced us we do for PMD THP and migration entries.Fixes 65edfda6f3f2 ("mm/rmap: extend rmap and migration support device-private entries")Cc <[email protected]>Signed-off-by Wei Yang <[email protected]>Suggested-by David Hildenbrand <[email protected]>Cc David Hildenbrand <[email protected]>Cc Balbir Singh <[email protected]>Cc SeongJae Park <[email protected]>Cc Zi Yan <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Lance Yang <[email protected]>---
v4:
* refine subject and commit log based on Lorenzo's suggestion
* put pmd device-private entry handling in its own if branch, suggested by Lorenzov3:
* remove cleanup part, only fix the issue for device-private entry
* refine user effect description based on Lorenzo's suggestionv2: https://lore.kernel.org/all/[email protected]/T/#u
* specify the possible error case of current code and user visible effect
* besides fix, cleanup the pmd entry handling based on David's suggestionv1: https://lore.kernel.org/linux-mm/[email protected]/
--- mm/page_vma_mapped.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)mm/page_vma_mapped.c +11
-4@@ -269,14 +269,24 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)/* THP pmd was split under us: handle on pte level */ spin_unlock (pvmw->ptl); pvmw->ptl =NULL ; - }else if (!pmd_present (pmde)) { -const softleaf_t entry =softleaf_from_pmd (pmde); + }else if (pmd_is_device_private_entry (pmde)) { +softleaf_t entry; + + pvmw->ptl =pmd_lock (mm, pvmw->pmd); + pmde = *pvmw->pmd; + entry =softleaf_from_pmd (pmde); -if (softleaf_is_device_private (entry)) { - pvmw->ptl =pmd_lock (mm, pvmw->pmd); +if (likely (softleaf_is_device_private (entry))) { +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (!check_pmd (softleaf_to_pfn (entry), pvmw)) +return not_found (pvmw);mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 292
Quoted 10 lines
mm/page_vma_mapped.c +4
-1return true; } - +/* device-private pmd was split under us: handle on pte level */ +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ; + }else if (!pmd_present (pmde)) { if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address, PMD_ORDER) &&This is extremely hard to review given the existing crap handling here. I'm really sorry, but it makes my head hurt (I'm not kidding :) ).
It's completely unclear why we only have to check for a subset of the cases after taking the lock.
Could we simply extend the existing migration pmd handling and leave the !pmd_present() case for
pmd_none()?That leaves no question to "which transitions are actually allowed", including "could we accidentally assume something is a page table when really it isn't".
So what about something like the following?
The "
thp_migration_supported()" is not required when checking forpmd_is_migration_entry(), as that defaults to "false" when not compiled in.Untested:
From
048ecd33673ec649e168fbbb97749a7c0e344fcdMon Sep 17 00:00:00 2001
From: "David Hildenbrand (Arm)" <[email protected]>
Date: Fri, 26 Jun 2026 12:03:40 +0200
Subject: [PATCH] tmpSigned-off-by David Hildenbrand (Arm) <[email protected]>--- mm/page_vma_mapped.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-)mm/page_vma_mapped.c +3
-2@@ -243,21 +243,31 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)*/ pmde =pmdp_get_lockless (pvmw->pmd); -if (pmd_trans_huge (pmde) ||pmd_is_migration_entry (pmde)) { +if (pmd_trans_huge (pmde) ||pmd_is_migration_entry (pmde) || +pmd_is_device_private_entry (pmde)) { pvmw->ptl =pmd_lock (mm, pvmw->pmd); pmde = *pvmw->pmd; -if (!pmd_present (pmde)) { +if (pmd_is_migration_entry (pmde)) {softleaf_t entry;Quoted 4 lines
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 252
Quoted 1 line
mm/page_vma_mapped.c +0
-1- if (!thp_migration_supported() ||Do we care about this? Or is !tmp_migration_supported() -> implies you wouldn't see a migration entry here anyway?
Yeah, I noted above
"The "
thp_migration_supported()" is not required when checking forpmd_is_migration_entry(), as that defaults to "false" when not compiled in."Given that
tmp_migration_supported()-> IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION);$And
pmd_is_migration_entry()-> softleaf_is_migration(softleaf_from_pmd(pmd));whereby
softleaf_from_pmd()only returns something non-none forCONFIG_ARCH_ENABLE_THP_MIGRATION.Quoted 1 line
Quoted 6 lines
Quoted 1 line
Maybe worth a
VM_WARN_ON_ONCE()?I think it was primarily a a hack to slightly optimize code generated for !CONFIG_ARCH_ENABLE_THP_MIGRATION, not really something for correctness as it seems.
So I think we can safely drop it. :)
thp_migration_supported() here is legacy code[1] from v4.14 when I added the THP migration support. IIRC, the purpose was to avoid checking PMD migration entry if the support is not enabled, but looking at it again today, that thp_migration_supported() is unnecessary since is_migration_entry(pmd_to_swp_entry(*pvmw->pmd)) returns false if !CONFIG_ARCH_ENABLE_THP_MIGRATION.
[1] https://elixir.bootlin.com/linux/v4.14/source/mm/page_vma_mapped.c#L157
Best Regards,
Yan, Zi
Lorenzo Stoakes
maintainerQuoted 176 lines · Zi Yan
Quoted 167 lines · David Hildenbrand (Arm)
Quoted 144 lines · Lorenzo Stoakes
Quoted 143 lines · David Hildenbrand (Arm)
Quoted 83 lines · Wei Yang
Commit
65edfda6f3f2("mm/rmap: extend rmap and migration support device-private entries") introduced the concept of device-private PMD entries, but did not correctly update the rmap walk code to account for them.As a result, when
page_vma_mapped_walk()encounters device-private PMD entries, it takes no action other than to acquire the PMD lock and exit.However this is highly problematic for two reasons - firstly, device private entries possess a PFN so
check_pmd()needs to be called to ensure an overlapping PFN range.Secondly, and more importantly, if
PVMW_MIGRATIONis set the caller assumes the returned entry is a migration entry, resulting in memory corruption when the caller tries to interpret the device private entry as such.In addition, commit 146287290023 ("
mm/huge_memory: implement device-private THP splitting") allowed device private PMDs to be split like THP mappings, but again did not update this code path.As a result, we might race a PMD split prior to acquiring the PMD lock.
This patch addresses all of these issues by invoking
check_pmd(), ensuringPMVW_MIGRATIONis not set and checks whether a split raced us we do for PMD THP and migration entries.Fixes 65edfda6f3f2 ("mm/rmap: extend rmap and migration support device-private entries")Cc <[email protected]>Signed-off-by Wei Yang <[email protected]>Suggested-by David Hildenbrand <[email protected]>Cc David Hildenbrand <[email protected]>Cc Balbir Singh <[email protected]>Cc SeongJae Park <[email protected]>Cc Zi Yan <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Lance Yang <[email protected]>---
v4:
* refine subject and commit log based on Lorenzo's suggestion
* put pmd device-private entry handling in its own if branch, suggested by Lorenzov3:
* remove cleanup part, only fix the issue for device-private entry
* refine user effect description based on Lorenzo's suggestionv2: https://lore.kernel.org/all/[email protected]/T/#u
* specify the possible error case of current code and user visible effect
* besides fix, cleanup the pmd entry handling based on David's suggestionv1: https://lore.kernel.org/linux-mm/[email protected]/
--- mm/page_vma_mapped.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)mm/page_vma_mapped.c +11
-4@@ -269,14 +269,24 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)/* THP pmd was split under us: handle on pte level */ spin_unlock (pvmw->ptl); pvmw->ptl =NULL ; - }else if (!pmd_present (pmde)) { -const softleaf_t entry =softleaf_from_pmd (pmde); + }else if (pmd_is_device_private_entry (pmde)) { +softleaf_t entry; + + pvmw->ptl =pmd_lock (mm, pvmw->pmd); + pmde = *pvmw->pmd; + entry =softleaf_from_pmd (pmde); -if (softleaf_is_device_private (entry)) { - pvmw->ptl =pmd_lock (mm, pvmw->pmd); +if (likely (softleaf_is_device_private (entry))) { +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (!check_pmd (softleaf_to_pfn (entry), pvmw)) +return not_found (pvmw);mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 292
Quoted 10 lines
mm/page_vma_mapped.c +4
-1return true; } - +/* device-private pmd was split under us: handle on pte level */ +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ; + }else if (!pmd_present (pmde)) { if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address, PMD_ORDER) &&This is extremely hard to review given the existing crap handling here. I'm really sorry, but it makes my head hurt (I'm not kidding :) ).
It's completely unclear why we only have to check for a subset of the cases after taking the lock.
Could we simply extend the existing migration pmd handling and leave the !pmd_present() case for
pmd_none()?That leaves no question to "which transitions are actually allowed", including "could we accidentally assume something is a page table when really it isn't".
So what about something like the following?
The "
thp_migration_supported()" is not required when checking forpmd_is_migration_entry(), as that defaults to "false" when not compiled in.Untested:
From
048ecd33673ec649e168fbbb97749a7c0e344fcdMon Sep 17 00:00:00 2001
From: "David Hildenbrand (Arm)" <[email protected]>
Date: Fri, 26 Jun 2026 12:03:40 +0200
Subject: [PATCH] tmpSigned-off-by David Hildenbrand (Arm) <[email protected]>--- mm/page_vma_mapped.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-)mm/page_vma_mapped.c +3
-2@@ -243,21 +243,31 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)*/ pmde =pmdp_get_lockless (pvmw->pmd); -if (pmd_trans_huge (pmde) ||pmd_is_migration_entry (pmde)) { +if (pmd_trans_huge (pmde) ||pmd_is_migration_entry (pmde) || +pmd_is_device_private_entry (pmde)) { pvmw->ptl =pmd_lock (mm, pvmw->pmd); pmde = *pvmw->pmd; -if (!pmd_present (pmde)) { +if (pmd_is_migration_entry (pmde)) {softleaf_t entry;Quoted 4 lines
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 252
Quoted 1 line
mm/page_vma_mapped.c +0
-1- if (!thp_migration_supported() ||Do we care about this? Or is !tmp_migration_supported() -> implies you wouldn't see a migration entry here anyway?
Yeah, I noted above
"The "
thp_migration_supported()" is not required when checking forpmd_is_migration_entry(), as that defaults to "false" when not compiled in."Given that
tmp_migration_supported()-> IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION);$And
pmd_is_migration_entry()-> softleaf_is_migration(softleaf_from_pmd(pmd));whereby
softleaf_from_pmd()only returns something non-none forCONFIG_ARCH_ENABLE_THP_MIGRATION.Quoted 1 line
Quoted 6 lines
Quoted 1 line
Maybe worth a
VM_WARN_ON_ONCE()?I think it was primarily a a hack to slightly optimize code generated for !CONFIG_ARCH_ENABLE_THP_MIGRATION, not really something for correctness as it seems.
So I think we can safely drop it. :)
Quoted 8 lines
thp_migration_supported()here is legacy code[1] from v4.14 when I added the THP migration support. IIRC, the purpose was to avoid checking PMD migration entry if the support is not enabled, but looking at it again today, thatthp_migration_supported()is unnecessary since is_migration_entry(pmd_to_swp_entry(*pvmw->pmd)) returns false if !CONFIG_ARCH_ENABLE_THP_MIGRATION.[1] https://elixir.bootlin.com/linux/v4.14/source/mm/page_vma_mapped.c#L157
Thanks guys, let's drop it then!
Quoted 3 lines
Best Regards,
Yan, Zi
Cheers, Lorenzo
Lance Yang
reviewerQuoted 171 lines · David Hildenbrand (Arm)
Quoted 83 lines · Wei Yang
Commit
65edfda6f3f2("mm/rmap: extend rmap and migration support device-private entries") introduced the concept of device-private PMD entries, but did not correctly update the rmap walk code to account for them.As a result, when
page_vma_mapped_walk()encounters device-private PMD entries, it takes no action other than to acquire the PMD lock and exit.However this is highly problematic for two reasons - firstly, device private entries possess a PFN so
check_pmd()needs to be called to ensure an overlapping PFN range.Secondly, and more importantly, if
PVMW_MIGRATIONis set the caller assumes the returned entry is a migration entry, resulting in memory corruption when the caller tries to interpret the device private entry as such.In addition, commit 146287290023 ("
mm/huge_memory: implement device-private THP splitting") allowed device private PMDs to be split like THP mappings, but again did not update this code path.As a result, we might race a PMD split prior to acquiring the PMD lock.
This patch addresses all of these issues by invoking
check_pmd(), ensuringPMVW_MIGRATIONis not set and checks whether a split raced us we do for PMD THP and migration entries.Fixes 65edfda6f3f2 ("mm/rmap: extend rmap and migration support device-private entries")Cc <[email protected]>Signed-off-by Wei Yang <[email protected]>Suggested-by David Hildenbrand <[email protected]>Cc David Hildenbrand <[email protected]>Cc Balbir Singh <[email protected]>Cc SeongJae Park <[email protected]>Cc Zi Yan <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Lance Yang <[email protected]>---
v4:
* refine subject and commit log based on Lorenzo's suggestion
* put pmd device-private entry handling in its own if branch, suggested by Lorenzov3:
* remove cleanup part, only fix the issue for device-private entry
* refine user effect description based on Lorenzo's suggestionv2: https://lore.kernel.org/all/[email protected]/T/#u
* specify the possible error case of current code and user visible effect
* besides fix, cleanup the pmd entry handling based on David's suggestionv1: https://lore.kernel.org/linux-mm/[email protected]/
--- mm/page_vma_mapped.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)mm/page_vma_mapped.c +11
-4@@ -269,14 +269,24 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)/* THP pmd was split under us: handle on pte level */ spin_unlock (pvmw->ptl); pvmw->ptl =NULL ; - }else if (!pmd_present (pmde)) { -const softleaf_t entry =softleaf_from_pmd (pmde); + }else if (pmd_is_device_private_entry (pmde)) { +softleaf_t entry; + + pvmw->ptl =pmd_lock (mm, pvmw->pmd); + pmde = *pvmw->pmd; + entry =softleaf_from_pmd (pmde); -if (softleaf_is_device_private (entry)) { - pvmw->ptl =pmd_lock (mm, pvmw->pmd); +if (likely (softleaf_is_device_private (entry))) { +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (!check_pmd (softleaf_to_pfn (entry), pvmw)) +return not_found (pvmw);mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 292
Quoted 10 lines
mm/page_vma_mapped.c +4
-1return true; } - +/* device-private pmd was split under us: handle on pte level */ +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ; + }else if (!pmd_present (pmde)) { if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address, PMD_ORDER) &&This is extremely hard to review given the existing crap handling here. I'm really sorry, but it makes my head hurt (I'm not kidding :) ).
It's completely unclear why we only have to check for a subset of the cases after taking the lock.
Could we simply extend the existing migration pmd handling and leave the !pmd_present() case for
pmd_none()?That leaves no question to "which transitions are actually allowed", including "could we accidentally assume something is a page table when really it isn't".
So what about something like the following?
The "
thp_migration_supported()" is not required when checking forpmd_is_migration_entry(), as that defaults to "false" when not compiled in.Untested:
Quoted 1 line
From
048ecd33673ec649e168fbbb97749a7c0e344fcdMon Sep 17 00:00:00 2001From: "David Hildenbrand (Arm)" <[email protected]>
Date: Fri, 26 Jun 2026 12:03:40 +0200
Subject: [PATCH] tmpSigned-off-by David Hildenbrand (Arm) <[email protected]>---
mm/page_vma_mapped.c| 29 +++++++++++++++++------------1 file changed, 17 insertions(+), 12 deletions(-)mm/page_vma_mapped.c +15
-6@@ -243,21 +243,31 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)*/ pmde =pmdp_get_lockless (pvmw->pmd); -if (pmd_trans_huge (pmde) ||pmd_is_migration_entry (pmde)) { +if (pmd_trans_huge (pmde) ||pmd_is_migration_entry (pmde) || +pmd_is_device_private_entry (pmde)) { pvmw->ptl =pmd_lock (mm, pvmw->pmd); pmde = *pvmw->pmd; -if (!pmd_present (pmde)) { +if (pmd_is_migration_entry (pmde)) {softleaf_t entry; -if (!thp_migration_supported () || - !(pvmw->flags &PVMW_MIGRATION )) +if (!(pvmw->flags &PVMW_MIGRATION ))return not_found (pvmw); entry =softleaf_from_pmd (pmde); +if (!check_pmd (softleaf_to_pfn (entry), pvmw)) +return not_found (pvmw); +return true; + }else if (pmd_is_device_private_entry (pmde)) { +softleaf_t entry; -if (!softleaf_is_migration (entry) || - !check_pmd (softleaf_to_pfn (entry), pvmw)) +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); + entry =softleaf_from_pmd (pmde); +if (!check_pmd (softleaf_to_pfn (entry), pvmw))return not_found (pvmw);return true; + }else if (!pmd_present (pmde) ){ +return not_found (pvmw); }if (likely (pmd_trans_huge (pmde))) {if (pvmw->flags &PVMW_MIGRATION )@@ -270,12 +280,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)spin_unlock (pvmw->ptl); pvmw->ptl =NULL ;
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 285
Quoted 10 lines
mm/page_vma_mapped.c +0
-6} else if (!pmd_present(pmde)) { - const softleaf_t entry = softleaf_from_pmd(pmde); - - if (softleaf_is_device_private(entry)) { - pvmw->ptl = pmd_lock(mm, pvmw->pmd); - return true; - } if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address,
Might be good with this on top:
---8<---
mm/page_vma_mapped.c +1 -1
@@ -281,7 +281,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)return not_found (pvmw);return true; } -/* THP pmd was split under us: handle on pte level */ +/* THP/device-private pmd was split under us: handle on pte level */ spin_unlock (pvmw->ptl); pvmw->ptl =NULL ; }else if (!pmd_present (pmde)) {
--
Looks good to me as well, thanks!
David Hildenbrand (Arm)
maintainerQuoted 202 lines · Lance Yang
Quoted 171 lines · David Hildenbrand (Arm)
Quoted 83 lines · Wei Yang
Commit
65edfda6f3f2("mm/rmap: extend rmap and migration support device-private entries") introduced the concept of device-private PMD entries, but did not correctly update the rmap walk code to account for them.As a result, when
page_vma_mapped_walk()encounters device-private PMD entries, it takes no action other than to acquire the PMD lock and exit.However this is highly problematic for two reasons - firstly, device private entries possess a PFN so
check_pmd()needs to be called to ensure an overlapping PFN range.Secondly, and more importantly, if
PVMW_MIGRATIONis set the caller assumes the returned entry is a migration entry, resulting in memory corruption when the caller tries to interpret the device private entry as such.In addition, commit 146287290023 ("
mm/huge_memory: implement device-private THP splitting") allowed device private PMDs to be split like THP mappings, but again did not update this code path.As a result, we might race a PMD split prior to acquiring the PMD lock.
This patch addresses all of these issues by invoking
check_pmd(), ensuringPMVW_MIGRATIONis not set and checks whether a split raced us we do for PMD THP and migration entries.Fixes 65edfda6f3f2 ("mm/rmap: extend rmap and migration support device-private entries")Cc <[email protected]>Signed-off-by Wei Yang <[email protected]>Suggested-by David Hildenbrand <[email protected]>Cc David Hildenbrand <[email protected]>Cc Balbir Singh <[email protected]>Cc SeongJae Park <[email protected]>Cc Zi Yan <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Lance Yang <[email protected]>---
v4:
* refine subject and commit log based on Lorenzo's suggestion
* put pmd device-private entry handling in its own if branch, suggested by Lorenzov3:
* remove cleanup part, only fix the issue for device-private entry
* refine user effect description based on Lorenzo's suggestionv2: https://lore.kernel.org/all/[email protected]/T/#u
* specify the possible error case of current code and user visible effect
* besides fix, cleanup the pmd entry handling based on David's suggestionv1: https://lore.kernel.org/linux-mm/[email protected]/
--- mm/page_vma_mapped.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)mm/page_vma_mapped.c +11
-4@@ -269,14 +269,24 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)/* THP pmd was split under us: handle on pte level */ spin_unlock (pvmw->ptl); pvmw->ptl =NULL ; - }else if (!pmd_present (pmde)) { -const softleaf_t entry =softleaf_from_pmd (pmde); + }else if (pmd_is_device_private_entry (pmde)) { +softleaf_t entry; + + pvmw->ptl =pmd_lock (mm, pvmw->pmd); + pmde = *pvmw->pmd; + entry =softleaf_from_pmd (pmde); -if (softleaf_is_device_private (entry)) { - pvmw->ptl =pmd_lock (mm, pvmw->pmd); +if (likely (softleaf_is_device_private (entry))) { +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (!check_pmd (softleaf_to_pfn (entry), pvmw)) +return not_found (pvmw);mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 292
Quoted 10 lines
mm/page_vma_mapped.c +4
-1return true; } - +/* device-private pmd was split under us: handle on pte level */ +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ; + }else if (!pmd_present (pmde)) { if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address, PMD_ORDER) &&This is extremely hard to review given the existing crap handling here. I'm really sorry, but it makes my head hurt (I'm not kidding :) ).
It's completely unclear why we only have to check for a subset of the cases after taking the lock.
Could we simply extend the existing migration pmd handling and leave the !pmd_present() case for
pmd_none()?That leaves no question to "which transitions are actually allowed", including "could we accidentally assume something is a page table when really it isn't".
So what about something like the following?
The "
thp_migration_supported()" is not required when checking forpmd_is_migration_entry(), as that defaults to "false" when not compiled in.Untested:
Quoted 1 line
From
048ecd33673ec649e168fbbb97749a7c0e344fcdMon Sep 17 00:00:00 2001From: "David Hildenbrand (Arm)" <[email protected]>
Date: Fri, 26 Jun 2026 12:03:40 +0200
Subject: [PATCH] tmpSigned-off-by David Hildenbrand (Arm) <[email protected]>---
mm/page_vma_mapped.c| 29 +++++++++++++++++------------1 file changed, 17 insertions(+), 12 deletions(-)mm/page_vma_mapped.c +15
-6@@ -243,21 +243,31 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)*/ pmde =pmdp_get_lockless (pvmw->pmd); -if (pmd_trans_huge (pmde) ||pmd_is_migration_entry (pmde)) { +if (pmd_trans_huge (pmde) ||pmd_is_migration_entry (pmde) || +pmd_is_device_private_entry (pmde)) { pvmw->ptl =pmd_lock (mm, pvmw->pmd); pmde = *pvmw->pmd; -if (!pmd_present (pmde)) { +if (pmd_is_migration_entry (pmde)) {softleaf_t entry; -if (!thp_migration_supported () || - !(pvmw->flags &PVMW_MIGRATION )) +if (!(pvmw->flags &PVMW_MIGRATION ))return not_found (pvmw); entry =softleaf_from_pmd (pmde); +if (!check_pmd (softleaf_to_pfn (entry), pvmw)) +return not_found (pvmw); +return true; + }else if (pmd_is_device_private_entry (pmde)) { +softleaf_t entry; -if (!softleaf_is_migration (entry) || - !check_pmd (softleaf_to_pfn (entry), pvmw)) +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); + entry =softleaf_from_pmd (pmde); +if (!check_pmd (softleaf_to_pfn (entry), pvmw))return not_found (pvmw);return true; + }else if (!pmd_present (pmde) ){ +return not_found (pvmw); }if (likely (pmd_trans_huge (pmde))) {if (pvmw->flags &PVMW_MIGRATION )@@ -270,12 +280,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)spin_unlock (pvmw->ptl); pvmw->ptl =NULL ;mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 285
Quoted 10 lines
mm/page_vma_mapped.c +0
-6} else if (!pmd_present(pmde)) { - const softleaf_t entry = softleaf_from_pmd(pmde); - - if (softleaf_is_device_private(entry)) { - pvmw->ptl = pmd_lock(mm, pvmw->pmd); - return true; - } if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address,Might be good with this on top:
---8<---
mm/page_vma_mapped.c +1
-2@@ -281,7 +281,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)return not_found (pvmw);return true; } -/* THP pmd was split under us: handle on pte level */ +/* THP/device-private pmd was split under us: handle on pte level */ spin_unlock (pvmw->ptl); pvmw->ptl =NULL ; }else if (!pmd_present (pmde)) { --
Quoted 1 line
Looks good to me as well, thanks!
Makes sense, thanks!
Signature · 3 lines
Cheers, David
Quoted 192 lines · Lance Yang
Quoted 171 lines · David Hildenbrand (Arm)
Quoted 83 lines · Wei Yang
Commit
65edfda6f3f2("mm/rmap: extend rmap and migration support device-private entries") introduced the concept of device-private PMD entries, but did not correctly update the rmap walk code to account for them.As a result, when
page_vma_mapped_walk()encounters device-private PMD entries, it takes no action other than to acquire the PMD lock and exit.However this is highly problematic for two reasons - firstly, device private entries possess a PFN so
check_pmd()needs to be called to ensure an overlapping PFN range.Secondly, and more importantly, if
PVMW_MIGRATIONis set the caller assumes the returned entry is a migration entry, resulting in memory corruption when the caller tries to interpret the device private entry as such.In addition, commit 146287290023 ("
mm/huge_memory: implement device-private THP splitting") allowed device private PMDs to be split like THP mappings, but again did not update this code path.As a result, we might race a PMD split prior to acquiring the PMD lock.
This patch addresses all of these issues by invoking
check_pmd(), ensuringPMVW_MIGRATIONis not set and checks whether a split raced us we do for PMD THP and migration entries.Fixes 65edfda6f3f2 ("mm/rmap: extend rmap and migration support device-private entries")Cc <[email protected]>Signed-off-by Wei Yang <[email protected]>Suggested-by David Hildenbrand <[email protected]>Cc David Hildenbrand <[email protected]>Cc Balbir Singh <[email protected]>Cc SeongJae Park <[email protected]>Cc Zi Yan <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Lance Yang <[email protected]>---
v4:
* refine subject and commit log based on Lorenzo's suggestion
* put pmd device-private entry handling in its own if branch, suggested by Lorenzov3:
* remove cleanup part, only fix the issue for device-private entry
* refine user effect description based on Lorenzo's suggestionv2: https://lore.kernel.org/all/[email protected]/T/#u
* specify the possible error case of current code and user visible effect
* besides fix, cleanup the pmd entry handling based on David's suggestionv1: https://lore.kernel.org/linux-mm/[email protected]/
--- mm/page_vma_mapped.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)mm/page_vma_mapped.c +11
-4@@ -269,14 +269,24 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)/* THP pmd was split under us: handle on pte level */ spin_unlock (pvmw->ptl); pvmw->ptl =NULL ; - }else if (!pmd_present (pmde)) { -const softleaf_t entry =softleaf_from_pmd (pmde); + }else if (pmd_is_device_private_entry (pmde)) { +softleaf_t entry; + + pvmw->ptl =pmd_lock (mm, pvmw->pmd); + pmde = *pvmw->pmd; + entry =softleaf_from_pmd (pmde); -if (softleaf_is_device_private (entry)) { - pvmw->ptl =pmd_lock (mm, pvmw->pmd); +if (likely (softleaf_is_device_private (entry))) { +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (!check_pmd (softleaf_to_pfn (entry), pvmw)) +return not_found (pvmw);mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 292
Quoted 10 lines
mm/page_vma_mapped.c +4
-1return true; } - +/* device-private pmd was split under us: handle on pte level */ +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ; + }else if (!pmd_present (pmde)) { if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address, PMD_ORDER) &&This is extremely hard to review given the existing crap handling here. I'm really sorry, but it makes my head hurt (I'm not kidding :) ).
It's completely unclear why we only have to check for a subset of the cases after taking the lock.
Could we simply extend the existing migration pmd handling and leave the !pmd_present() case for
pmd_none()?That leaves no question to "which transitions are actually allowed", including "could we accidentally assume something is a page table when really it isn't".
So what about something like the following?
The "
thp_migration_supported()" is not required when checking forpmd_is_migration_entry(), as that defaults to "false" when not compiled in.Untested:
Quoted 1 line
From
048ecd33673ec649e168fbbb97749a7c0e344fcdMon Sep 17 00:00:00 2001From: "David Hildenbrand (Arm)" <[email protected]>
Date: Fri, 26 Jun 2026 12:03:40 +0200
Subject: [PATCH] tmpSigned-off-by David Hildenbrand (Arm) <[email protected]>---
mm/page_vma_mapped.c| 29 +++++++++++++++++------------1 file changed, 17 insertions(+), 12 deletions(-)mm/page_vma_mapped.c +15
-6@@ -243,21 +243,31 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)*/ pmde =pmdp_get_lockless (pvmw->pmd); -if (pmd_trans_huge (pmde) ||pmd_is_migration_entry (pmde)) { +if (pmd_trans_huge (pmde) ||pmd_is_migration_entry (pmde) || +pmd_is_device_private_entry (pmde)) { pvmw->ptl =pmd_lock (mm, pvmw->pmd); pmde = *pvmw->pmd; -if (!pmd_present (pmde)) { +if (pmd_is_migration_entry (pmde)) {softleaf_t entry; -if (!thp_migration_supported () || - !(pvmw->flags &PVMW_MIGRATION )) +if (!(pvmw->flags &PVMW_MIGRATION ))return not_found (pvmw); entry =softleaf_from_pmd (pmde); +if (!check_pmd (softleaf_to_pfn (entry), pvmw)) +return not_found (pvmw); +return true; + }else if (pmd_is_device_private_entry (pmde)) { +softleaf_t entry; -if (!softleaf_is_migration (entry) || - !check_pmd (softleaf_to_pfn (entry), pvmw)) +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); + entry =softleaf_from_pmd (pmde); +if (!check_pmd (softleaf_to_pfn (entry), pvmw))return not_found (pvmw);return true; + }else if (!pmd_present (pmde) ){ +return not_found (pvmw); }if (likely (pmd_trans_huge (pmde))) {if (pvmw->flags &PVMW_MIGRATION )@@ -270,12 +280,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)spin_unlock (pvmw->ptl); pvmw->ptl =NULL ;mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 285
Quoted 10 lines
mm/page_vma_mapped.c +0
-6} else if (!pmd_present(pmde)) { - const softleaf_t entry = softleaf_from_pmd(pmde); - - if (softleaf_is_device_private(entry)) { - pvmw->ptl = pmd_lock(mm, pvmw->pmd); - return true; - } if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address,Might be good with this on top:
---8<---
mm/page_vma_mapped.c +0
-0
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 284
Quoted 5 lines
mm/page_vma_mapped.c +1
-1@@ -281,7 +281,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)return not_found (pvmw);return true; } -/* THP pmd was split under us: handle on pte level */ +/* THP/device-private pmd was split under us: handle on pte level */
As the comment in commit 65edfda6f3f2 ("mm/rmap: extend rmap and migration support device-private entries") says:
Add device-private THP support...
Per my understanding, we first already setup mapping and "migrate" to device memory. This looks a kind of place holder.
Not familiar with this. Just want to clarify, we want to treat device-private pmd as some sort of THP or not?
Quoted 6 lines
mm/page_vma_mapped.c +0
-1spin_unlock (pvmw->ptl); pvmw->ptl =NULL ; }else if (!pmd_present (pmde)) { --Looks good to me as well, thanks!
Signature · 2 lines
Wei Yang Help you, Help me
Lance Yang
reviewerOn 2026/6/27 08:38, Wei Yang wrote:
[...]
Quoted 18 lines
Quoted 9 lines
Might be good with this on top:
---8<---
mm/page_vma_mapped.c +0
-0mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 284
Quoted 5 lines
mm/page_vma_mapped.c +1
-1@@ -281,7 +281,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)return not_found (pvmw);return true; } -/* THP pmd was split under us: handle on pte level */ +/* THP/device-private pmd was split under us: handle on pte level */ As the comment in commit
65edfda6f3f2("mm/rmap: extend rmap and migration support device-private entries") says:
Quoted 7 lines
Add device-private THP support...
Per my understanding, we first already setup mapping and "migrate" to device memory. This looks a kind of place holder.
Not familiar with this. Just want to clarify, we want to treat device-private pmd as some sort of THP or not?
Not a regular THP, obviously. Just the PMD-sized device-private entry case.
It can be split under us too; see commit 146287290023 ("mm/huge_memory:
implement device-private THP splitting").
Nothing deeper meant here. After taking PTL, if that PMD-sized entry is gone, just drop to the PTE walk.
Quoted 5 lines
Quoted 5 lines
mm/page_vma_mapped.c +0
-1spin_unlock (pvmw->ptl); pvmw->ptl =NULL ; }else if (!pmd_present (pmde)) { --
Quoted 98 lines · David Hildenbrand (Arm)
Quoted 83 lines · Wei Yang
Commit
65edfda6f3f2("mm/rmap: extend rmap and migration support device-private entries") introduced the concept of device-private PMD entries, but did not correctly update the rmap walk code to account for them.As a result, when
page_vma_mapped_walk()encounters device-private PMD entries, it takes no action other than to acquire the PMD lock and exit.However this is highly problematic for two reasons - firstly, device private entries possess a PFN so
check_pmd()needs to be called to ensure an overlapping PFN range.Secondly, and more importantly, if
PVMW_MIGRATIONis set the caller assumes the returned entry is a migration entry, resulting in memory corruption when the caller tries to interpret the device private entry as such.In addition, commit 146287290023 ("
mm/huge_memory: implement device-private THP splitting") allowed device private PMDs to be split like THP mappings, but again did not update this code path.As a result, we might race a PMD split prior to acquiring the PMD lock.
This patch addresses all of these issues by invoking
check_pmd(), ensuringPMVW_MIGRATIONis not set and checks whether a split raced us we do for PMD THP and migration entries.Fixes 65edfda6f3f2 ("mm/rmap: extend rmap and migration support device-private entries")Cc <[email protected]>Signed-off-by Wei Yang <[email protected]>Suggested-by David Hildenbrand <[email protected]>Cc David Hildenbrand <[email protected]>Cc Balbir Singh <[email protected]>Cc SeongJae Park <[email protected]>Cc Zi Yan <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Lance Yang <[email protected]>---
v4:
* refine subject and commit log based on Lorenzo's suggestion
* put pmd device-private entry handling in its own if branch, suggested by Lorenzov3:
* remove cleanup part, only fix the issue for device-private entry
* refine user effect description based on Lorenzo's suggestionv2: https://lore.kernel.org/all/[email protected]/T/#u
* specify the possible error case of current code and user visible effect
* besides fix, cleanup the pmd entry handling based on David's suggestionv1: https://lore.kernel.org/linux-mm/[email protected]/
--- mm/page_vma_mapped.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)mm/page_vma_mapped.c +11
-4@@ -269,14 +269,24 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)/* THP pmd was split under us: handle on pte level */ spin_unlock (pvmw->ptl); pvmw->ptl =NULL ; - }else if (!pmd_present (pmde)) { -const softleaf_t entry =softleaf_from_pmd (pmde); + }else if (pmd_is_device_private_entry (pmde)) { +softleaf_t entry; + + pvmw->ptl =pmd_lock (mm, pvmw->pmd); + pmde = *pvmw->pmd; + entry =softleaf_from_pmd (pmde); -if (softleaf_is_device_private (entry)) { - pvmw->ptl =pmd_lock (mm, pvmw->pmd); +if (likely (softleaf_is_device_private (entry))) { +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (!check_pmd (softleaf_to_pfn (entry), pvmw)) +return not_found (pvmw);mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 292
Quoted 10 lines
mm/page_vma_mapped.c +4
-1return true; } - +/* device-private pmd was split under us: handle on pte level */ +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ; + }else if (!pmd_present (pmde)) { if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address, PMD_ORDER) &&This is extremely hard to review given the existing crap handling here. I'm really sorry, but it makes my head hurt (I'm not kidding :) ).
Quoted 9 lines
It's completely unclear why we only have to check for a subset of the cases after taking the lock.
Could we simply extend the existing migration pmd handling and leave the !pmd_present() case for
pmd_none()?That leaves no question to "which transitions are actually allowed", including "could we accidentally assume something is a page table when really it isn't".
Consolidate all the cases in one place looks reasonable.
And make the logic clearer.
Quoted 76 lines
So what about something like the following?
The "
thp_migration_supported()" is not required when checking forpmd_is_migration_entry(), as that defaults to "false" when not compiled in.Untested:
Quoted 1 line
From
048ecd33673ec649e168fbbb97749a7c0e344fcdMon Sep 17 00:00:00 2001From: "David Hildenbrand (Arm)" <[email protected]>
Date: Fri, 26 Jun 2026 12:03:40 +0200
Subject: [PATCH] tmpSigned-off-by David Hildenbrand (Arm) <[email protected]>---
mm/page_vma_mapped.c| 29 +++++++++++++++++------------1 file changed, 17 insertions(+), 12 deletions(-)mm/page_vma_mapped.c +15
-12@@ -243,21 +243,31 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)*/ pmde =pmdp_get_lockless (pvmw->pmd); -if (pmd_trans_huge (pmde) ||pmd_is_migration_entry (pmde)) { +if (pmd_trans_huge (pmde) ||pmd_is_migration_entry (pmde) || +pmd_is_device_private_entry (pmde)) { pvmw->ptl =pmd_lock (mm, pvmw->pmd); pmde = *pvmw->pmd; -if (!pmd_present (pmde)) { +if (pmd_is_migration_entry (pmde)) {softleaf_t entry; -if (!thp_migration_supported () || - !(pvmw->flags &PVMW_MIGRATION )) +if (!(pvmw->flags &PVMW_MIGRATION ))return not_found (pvmw); entry =softleaf_from_pmd (pmde); +if (!check_pmd (softleaf_to_pfn (entry), pvmw)) +return not_found (pvmw); +return true; + }else if (pmd_is_device_private_entry (pmde)) { +softleaf_t entry; -if (!softleaf_is_migration (entry) || - !check_pmd (softleaf_to_pfn (entry), pvmw)) +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); + entry =softleaf_from_pmd (pmde); +if (!check_pmd (softleaf_to_pfn (entry), pvmw))return not_found (pvmw);return true; + }else if (!pmd_present (pmde) ){ +return not_found (pvmw); }if (likely (pmd_trans_huge (pmde))) {if (pvmw->flags &PVMW_MIGRATION )@@ -270,12 +280,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)spin_unlock (pvmw->ptl); pvmw->ptl =NULL ; } else if (!pmd_present(pmde)) { - const softleaf_t entry = softleaf_from_pmd(pmde); - - if (softleaf_is_device_private(entry)) { - pvmw->ptl = pmd_lock(mm, pvmw->pmd); - return true; - } if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address,Signature · 1 line
2.43.0
Will prepare v5 based one this.
Thanks.
Quoted 5 lines
Signature · 4 lines
-- Cheers, David
Signature · 2 lines
Wei Yang Help you, Help me
Quoted 1 line · David Hildenbrand (Arm)
On 6/24/26 08:53, Wei Yang wrote:
[...]
Quoted 14 lines
This is extremely hard to review given the existing crap handling here. I'm really sorry, but it makes my head hurt (I'm not kidding :) ).
It's completely unclear why we only have to check for a subset of the cases after taking the lock.
Could we simply extend the existing migration pmd handling and leave the !pmd_present() case for
pmd_none()?That leaves no question to "which transitions are actually allowed", including "could we accidentally assume something is a page table when really it isn't".
Quoted 7 lines
So what about something like the following?
The "
thp_migration_supported()" is not required when checking forpmd_is_migration_entry(), as that defaults to "false" when not compiled in.Untested:
Hi David
I did a little adjustment like below. Want to check with you at first.
Quoted 18 lines
Quoted 2 lines
From
048ecd33673ec649e168fbbb97749a7c0e344fcdMon Sep 17 00:00:00 2001From: "David Hildenbrand (Arm)" <[email protected]>
Date: Fri, 26 Jun 2026 12:03:40 +0200
Subject: [PATCH] tmpSigned-off-by David Hildenbrand (Arm) <[email protected]>---
mm/page_vma_mapped.c| 29 +++++++++++++++++------------1 file changed, 17 insertions(+), 12 deletions(-)mm/page_vma_mapped.c +0
-0@@ -243,21 +243,31 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)*/ pmde =pmdp_get_lockless (pvmw->pmd);
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 252
Quoted 10 lines
mm/page_vma_mapped.c +3
-2-if (pmd_trans_huge (pmde) ||pmd_is_migration_entry (pmde)) { +if (pmd_trans_huge (pmde) ||pmd_is_migration_entry (pmde) || +pmd_is_device_private_entry (pmde)) { pvmw->ptl =pmd_lock (mm, pvmw->pmd); pmde = *pvmw->pmd; -if (!pmd_present (pmde)) { +if (pmd_is_migration_entry (pmde)) {softleaf_t entry;
How about:
const softleaf_t entry = softleaf_from_pmd(pmde);
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 255
Quoted 5 lines
mm/page_vma_mapped.c +1
-2-if (!thp_migration_supported () || - !(pvmw->flags &PVMW_MIGRATION )) +if (!(pvmw->flags &PVMW_MIGRATION ))return not_found (pvmw); entry =softleaf_from_pmd (pmde);
could be removed.
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 260
Quoted 5 lines
mm/page_vma_mapped.c +5
-0+if (!check_pmd (softleaf_to_pfn (entry), pvmw)) +return not_found (pvmw); +return true; + }else if (pmd_is_device_private_entry (pmde)) { +softleaf_t entry;
The same.
Quoted 4 lines
mm/page_vma_mapped.c +1
-2-if (!softleaf_is_migration (entry) || - !check_pmd (softleaf_to_pfn (entry), pvmw)) +if (pvmw->flags &PVMW_MIGRATION )
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 272
Quoted 10 lines
mm/page_vma_mapped.c +5
-0+return not_found (pvmw); + entry =softleaf_from_pmd (pmde); +if (!check_pmd (softleaf_to_pfn (entry), pvmw))return not_found (pvmw);return true; + }else if (!pmd_present (pmde) ){ +return not_found (pvmw); }if (likely (pmd_trans_huge (pmde))) {if (pvmw->flags &PVMW_MIGRATION )
How about merge this with above? And put at the first case?
Below is what it looks like:
if (pmd_trans_huge(pmde) || pmd_is_migration_entry(pmde) ||
pmd_is_device_private_entry(pmde)) {
pvmw->ptl = pmd_lock(mm, pvmw->pmd);
pmde = *pvmw->pmd;
if (likely(pmd_trans_huge(pmde))) {
if (pvmw->flags & PVMW_MIGRATION)
return not_found(pvmw);
if (!check_pmd(pmd_pfn(pmde), pvmw))
return not_found(pvmw);
return true;
} else if (pmd_is_migration_entry(pmde)) {
const softleaf_t entry = softleaf_from_pmd(pmde);
if (!(pvmw->flags & PVMW_MIGRATION))
return not_found(pvmw);
if (!check_pmd(softleaf_to_pfn(entry), pvmw))
return not_found(pvmw);
return true;
} else if (pmd_is_device_private_entry(pmde)) {
const softleaf_t entry = softleaf_from_pmd(pmde);
if (pvmw->flags & PVMW_MIGRATION)
return not_found(pvmw);
if (!check_pmd(softleaf_to_pfn(entry), pvmw))
return not_found(pvmw);
return true;
} else if (!pmd_present(pmde)) {
return not_found(pvmw);
}
/* THP pmd was split under us: handle on pte level */
spin_unlock(pvmw->ptl);
pvmw->ptl = NULL;
} else if (!pmd_present(pmde)) {Test with split_huge_page_test/khugepaged/hmm-test/migration in selftets, looks good.
Signature · 2 lines
Wei Yang Help you, Help me
Lance Yang
reviewerOn 2026/6/27 10:07, Wei Yang wrote:
[...]
Quoted 70 lines
Hi David
I did a little adjustment like below. Want to check with you at first.
Quoted 18 lines
Quoted 2 lines
From
048ecd33673ec649e168fbbb97749a7c0e344fcdMon Sep 17 00:00:00 2001From: "David Hildenbrand (Arm)" <[email protected]>
Date: Fri, 26 Jun 2026 12:03:40 +0200
Subject: [PATCH] tmpSigned-off-by David Hildenbrand (Arm) <[email protected]>---
mm/page_vma_mapped.c| 29 +++++++++++++++++------------1 file changed, 17 insertions(+), 12 deletions(-)mm/page_vma_mapped.c +0
-0@@ -243,21 +243,31 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)*/ pmde =pmdp_get_lockless (pvmw->pmd);mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 252
Quoted 10 lines
mm/page_vma_mapped.c +3
-2-if (pmd_trans_huge (pmde) ||pmd_is_migration_entry (pmde)) { +if (pmd_trans_huge (pmde) ||pmd_is_migration_entry (pmde) || +pmd_is_device_private_entry (pmde)) { pvmw->ptl =pmd_lock (mm, pvmw->pmd); pmde = *pvmw->pmd; -if (!pmd_present (pmde)) { +if (pmd_is_migration_entry (pmde)) {softleaf_t entry;How about:
const softleaf_t entry = softleaf_from_pmd(pmde);mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 255
Quoted 5 lines
mm/page_vma_mapped.c +1
-2-if (!thp_migration_supported () || - !(pvmw->flags &PVMW_MIGRATION )) +if (!(pvmw->flags &PVMW_MIGRATION ))return not_found (pvmw); entry =softleaf_from_pmd (pmde);could be removed.
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 260
Quoted 5 lines
mm/page_vma_mapped.c +5
-0+if (!check_pmd (softleaf_to_pfn (entry), pvmw)) +return not_found (pvmw); +return true; + }else if (pmd_is_device_private_entry (pmde)) { +softleaf_t entry;The same.
Quoted 4 lines
mm/page_vma_mapped.c +1
-2-if (!softleaf_is_migration (entry) || - !check_pmd (softleaf_to_pfn (entry), pvmw)) +if (pvmw->flags &PVMW_MIGRATION )mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 272
Quoted 10 lines
mm/page_vma_mapped.c +5
-0+return not_found (pvmw); + entry =softleaf_from_pmd (pmde); +if (!check_pmd (softleaf_to_pfn (entry), pvmw))return not_found (pvmw);return true; + }else if (!pmd_present (pmde) ){ +return not_found (pvmw); }if (likely (pmd_trans_huge (pmde))) {if (pvmw->flags &PVMW_MIGRATION )How about merge this with above? And put at the first case?
Quoted 1 line
Below is what it looks like:
Why add more churn to a fix with a stable tag? Cleanup can come later no?
[...]
Quoted 74 lines · Lance Yang
On 2026/6/27 10:07, Wei Yang wrote:
[...]Quoted 70 lines
Hi David
I did a little adjustment like below. Want to check with you at first.
Quoted 18 lines
Quoted 2 lines
From
048ecd33673ec649e168fbbb97749a7c0e344fcdMon Sep 17 00:00:00 2001From: "David Hildenbrand (Arm)" <[email protected]>
Date: Fri, 26 Jun 2026 12:03:40 +0200
Subject: [PATCH] tmpSigned-off-by David Hildenbrand (Arm) <[email protected]>---
mm/page_vma_mapped.c| 29 +++++++++++++++++------------1 file changed, 17 insertions(+), 12 deletions(-)mm/page_vma_mapped.c +0
-0@@ -243,21 +243,31 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)*/ pmde =pmdp_get_lockless (pvmw->pmd);mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 252
Quoted 10 lines
mm/page_vma_mapped.c +3
-2-if (pmd_trans_huge (pmde) ||pmd_is_migration_entry (pmde)) { +if (pmd_trans_huge (pmde) ||pmd_is_migration_entry (pmde) || +pmd_is_device_private_entry (pmde)) { pvmw->ptl =pmd_lock (mm, pvmw->pmd); pmde = *pvmw->pmd; -if (!pmd_present (pmde)) { +if (pmd_is_migration_entry (pmde)) {softleaf_t entry;How about:
const softleaf_t entry = softleaf_from_pmd(pmde);mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 255
Quoted 5 lines
mm/page_vma_mapped.c +1
-2-if (!thp_migration_supported () || - !(pvmw->flags &PVMW_MIGRATION )) +if (!(pvmw->flags &PVMW_MIGRATION ))return not_found (pvmw); entry =softleaf_from_pmd (pmde);could be removed.
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 260
Quoted 5 lines
mm/page_vma_mapped.c +5
-0+if (!check_pmd (softleaf_to_pfn (entry), pvmw)) +return not_found (pvmw); +return true; + }else if (pmd_is_device_private_entry (pmde)) { +softleaf_t entry;The same.
Quoted 4 lines
mm/page_vma_mapped.c +1
-2-if (!softleaf_is_migration (entry) || - !check_pmd (softleaf_to_pfn (entry), pvmw)) +if (pvmw->flags &PVMW_MIGRATION )mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 272
Quoted 10 lines
mm/page_vma_mapped.c +5
-0+return not_found (pvmw); + entry =softleaf_from_pmd (pmde); +if (!check_pmd (softleaf_to_pfn (entry), pvmw))return not_found (pvmw);return true; + }else if (!pmd_present (pmde) ){ +return not_found (pvmw); }if (likely (pmd_trans_huge (pmde))) {if (pvmw->flags &PVMW_MIGRATION )How about merge this with above? And put at the first case?
Quoted 4 lines
Quoted 1 line
Below is what it looks like:
Why add more churn to a fix with a stable tag? Cleanup can come later no?
OK, will leave it for future cleanup.
Signature · 2 lines
Wei Yang Help you, Help me
David Hildenbrand (Arm)
maintainerQuoted 21 lines · Wei Yang
Quoted 20 lines · Lance Yang
On 2026/6/27 10:07, Wei Yang wrote:
[...]Quoted 16 lines
Hi David
I did a little adjustment like below. Want to check with you at first.
How about:
const softleaf_t entry = softleaf_from_pmd(pmde);could be removed.
The same.
Quoted 9 lines
Quoted 6 lines
Quoted 3 lines
How about merge this with above? And put at the first case?
Below is what it looks like:
Why add more churn to a fix with a stable tag? Cleanup can come later no?
OK, will leave it for future cleanup.
Right, I tried to make the diff minimal :)
Please send a v5, thanks.
Signature · 3 lines
Cheers, David