mm/page_vma_mapped: revalidate and do proper check before return device-private pmd
Subsystems (3)
PATCH 1/1 mm/page_vma_mapped: revalidate and do proper check before return device-private pmd
For pmd_trans_huge() and pmd_is_migration_entry(), we does following before return the pmd entry:
* re-validate pmd entry
* check PVMW_MIGRATION
* check_pmd()
* handle on pte level if split under us
But for device-private pmd, we just return after pmd_lock(). This may lead to inproper situation.
This patch fixes commit 65edfda6f3f2 ("mm/rmap: extend rmap and migration support device-private entries") by following the same pattern as pmd_trans_huge() and pmd_is_migration_entry().
--- mm/page_vma_mapped.c | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-)
mm/page_vma_mapped.c +23 -11
@@ -269,21 +269,33 @@ 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); +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, -PMD_ORDER ) && - (pvmw->nr_pages >=HPAGE_PMD_NR )) -sync_with_folio_pmd_zap (mm, pvmw->pmd); + entry =softleaf_from_pmd (*pvmw->pmd); + +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; + } -step_forward (pvmw,PMD_SIZE ); -continue ; +/* THP pmd was split under us: handle on pte level */ +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ; + }else { +if ((pvmw->flags &PVMW_SYNC ) && +thp_vma_suitable_order (vma, pvmw->address, +PMD_ORDER ) && + (pvmw->nr_pages >=HPAGE_PMD_NR )) +sync_with_folio_pmd_zap (mm, pvmw->pmd); + +step_forward (pvmw,PMD_SIZE ); +continue ; + } }if (!map_pte (pvmw, &pmde, &ptl)) {if (!pvmw->pte)
Signature · 1 line
2.34.1
Andrew Morton
maintainerQuoted 10 lines · On Fri, 8 May 2026 01:37:28 +0000 Wei Yang <[email protected]> wrote:
For
pmd_trans_huge()andpmd_is_migration_entry(), we does following before return the pmd entry:* re-validate pmd entry
* checkPVMW_MIGRATION
*check_pmd()
* handle on pte level if split under usBut for device-private pmd, we just return after
pmd_lock(). This may lead to inproper situation.
What is "improper situation"?
Quoted 4 lines
This patch fixes commit
65edfda6f3f2("mm/rmap: extend rmap and migration support device-private entries") by following the same pattern aspmd_trans_huge()andpmd_is_migration_entry().
Quoted 8 lines
Fixes 65edfda6f3f2 ("mm/rmap: extend rmap and migration support device-private entries")Signed-off-by Wei Yang <[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 <[email protected]>
If we're to propose a fix for -stable backporting I believe we should fully explain to -stable maintainers *why* we're making that proposal.
IOW, and not for the first time(!), what are the worst-case userspace-visible effects of this bug?
Quoted 11 lines · Wei Yang
For
pmd_trans_huge()andpmd_is_migration_entry(), we does following before return the pmd entry:* re-validate pmd entry
* checkPVMW_MIGRATION
*check_pmd()
* handle on pte level if split under usBut for device-private pmd, we just return after
pmd_lock(). This may lead to inproper situation.
Could you elaborate a more on the improper situation?
Quoted 30 lines
This patch fixes commit
65edfda6f3f2("mm/rmap: extend rmap and migration support device-private entries") by following the same pattern aspmd_trans_huge()andpmd_is_migration_entry().Fixes 65edfda6f3f2 ("mm/rmap: extend rmap and migration support device-private entries")Signed-off-by Wei Yang <[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 <[email protected]>--- mm/page_vma_mapped.c | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-)mm/page_vma_mapped.c +1
-2@@ -269,21 +269,33 @@ 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); +softleaf_t entry =softleaf_from_pmd (pmde);if (softleaf_is_device_private (entry)) { pvmw->ptl =pmd_lock (mm, pvmw->pmd); -return true;
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 278
Quoted 10 lines
mm/page_vma_mapped.c +3
-7- } - -if ((pvmw->flags &PVMW_SYNC ) && -thp_vma_suitable_order (vma, pvmw->address, -PMD_ORDER ) && - (pvmw->nr_pages >=HPAGE_PMD_NR )) -sync_with_folio_pmd_zap (mm, pvmw->pmd); + entry =softleaf_from_pmd (*pvmw->pmd); + +if (softleaf_is_device_private (entry)) {
Do we need to check softleaf_is_device_private() twice, can't we hold the pmd lock and check once?
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 280
Quoted 2 lines
mm/page_vma_mapped.c +2
-0+if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw);
Double check, do we want to skip migration pte's (from remove_migration_pte)
Quoted 13 lines
mm/page_vma_mapped.c +10
-2+if (!check_pmd (softleaf_to_pfn (entry), pvmw)) +return not_found (pvmw); +return true; + } -step_forward (pvmw,PMD_SIZE ); -continue ; +/* THP pmd was split under us: handle on pte level */ +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ; + } else { + if ((pvmw->flags & PVMW_SYNC) && + thp_vma_suitable_order(vma, pvmw->address,
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 301
Quoted 10 lines
mm/page_vma_mapped.c +7
-0+ PMD_ORDER) && + (pvmw->nr_pages >= HPAGE_PMD_NR)) +sync_with_folio_pmd_zap (mm, pvmw->pmd); + +step_forward (pvmw,PMD_SIZE ); +continue ; + } }if (!map_pte (pvmw, &pmde, &ptl)) {if (!pvmw->pte)
How was this tested? Did you run hmm-tests? Is there a broken user space that caught the issue?
Balbir Singh
Quoted 9 lines · Balbir Singh
Quoted 8 lines · Wei Yang
For
pmd_trans_huge()andpmd_is_migration_entry(), we does following before return the pmd entry:* re-validate pmd entry
* checkPVMW_MIGRATION
*check_pmd()
* handle on pte level if split under us
Quoted 6 lines
Quoted 3 lines
But for device-private pmd, we just return after
pmd_lock(). This may lead to inproper situation.Could you elaborate a more on the improper situation?
For example, in remove_migration_pte() page_vma_mapped_walk() may return true on a device-private entry even it is not a migration entry.
Quoted 27 lines
Quoted 27 lines
This patch fixes commit
65edfda6f3f2("mm/rmap: extend rmap and migration support device-private entries") by following the same pattern aspmd_trans_huge()andpmd_is_migration_entry().Fixes 65edfda6f3f2 ("mm/rmap: extend rmap and migration support device-private entries")Signed-off-by Wei Yang <[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 <[email protected]>--- mm/page_vma_mapped.c | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-)mm/page_vma_mapped.c +1
-1@@ -269,21 +269,33 @@ 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); +softleaf_t entry =softleaf_from_pmd (pmde);
Quoted 17 lines
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 278
Quoted 13 lines
mm/page_vma_mapped.c +3
-8if (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, -PMD_ORDER ) && - (pvmw->nr_pages >=HPAGE_PMD_NR )) -sync_with_folio_pmd_zap (mm, pvmw->pmd); + entry =softleaf_from_pmd (*pvmw->pmd); + +if (softleaf_is_device_private (entry)) {Do we need to check
softleaf_is_device_private()twice, can't we hold the pmd lock and check once?
We discussed this code on [1], which spot the difference between device-private pmd and the other two pmd case which re-validation after pmd_lock().
Do check after pmd_lock() share the same pattern as the other two pmd entry case. Also lock is heavy, check & lock & re-validate seems more friendly to system. Otherwise we always need to grab lock.
And David suggest to use softleaf_is_device_private() again, [2].
Quoted 5 lines
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 280
Quoted 2 lines
mm/page_vma_mapped.c +2
-0+if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw);Double check, do we want to skip migration pte's (from
remove_migration_pte)
Do you mean skip device-private entry?
remove_migration_pte() looks for migration entry, and tries to replace it.
The semantics above is: if it looks for migration entry, return not_found() for device-private entry. Since device-private entry is not migration entry, IIUC.
Quoted 19 lines
Quoted 19 lines
mm/page_vma_mapped.c +16
-2+if (!check_pmd (softleaf_to_pfn (entry), pvmw)) +return not_found (pvmw); +return true; + } -step_forward (pvmw,PMD_SIZE ); -continue ; +/* THP pmd was split under us: handle on pte level */ +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ; + }else { +if ((pvmw->flags &PVMW_SYNC ) && +thp_vma_suitable_order (vma, pvmw->address, +PMD_ORDER ) && + (pvmw->nr_pages >=HPAGE_PMD_NR )) +sync_with_folio_pmd_zap (mm, pvmw->pmd); + +step_forward (pvmw,PMD_SIZE ); +continue ;
Quoted 8 lines
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 301
Quoted 4 lines
mm/page_vma_mapped.c +1
-0+ } }if (!map_pte (pvmw, &pmde, &ptl)) {if (!pvmw->pte)How was this tested? Did you run hmm-tests? Is there a broken user space that caught the issue?
I didn't do device-private memory related test.
IIUC, device-private memory is device related. I don't have such devices. Or we have other workaround to test it? Glad to know if so.
BTW, this fix is from pure code analysis and discussion. Maybe it would be better to include you in the discussion first, then I could get more background on it.
Quoted 2 lines
Balbir Singh
[1]: https://lore.kernel.org/all/[email protected]/
[2]: https://lore.kernel.org/all/[email protected]/
Signature · 2 lines
Wei Yang Help you, Help me
Quoted 10 lines · Andrew Morton
Quoted 8 lines · On Fri, 8 May 2026 01:37:28 +0000 Wei Yang <[email protected]> wrote:
For
pmd_trans_huge()andpmd_is_migration_entry(), we does following before return the pmd entry:* re-validate pmd entry
* checkPVMW_MIGRATION
*check_pmd()
* handle on pte level if split under us
Quoted 5 lines
Quoted 2 lines
But for device-private pmd, we just return after
pmd_lock(). This may lead to improper situation.What is "improper situation"?
For example, in remove_migration_pte(), page_vma_mapped_walk() may return device-private entry which is not a migration entry.
Quoted 13 lines
Quoted 4 lines
This patch fixes commit
65edfda6f3f2("mm/rmap: extend rmap and migration support device-private entries") by following the same pattern aspmd_trans_huge()andpmd_is_migration_entry().Quoted 8 lines
Fixes 65edfda6f3f2 ("mm/rmap: extend rmap and migration support device-private entries")Signed-off-by Wei Yang <[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 <[email protected]>
Quoted 3 lines
If we're to propose a fix for -stable backporting I believe we should fully explain to -stable maintainers *why* we're making that proposal.
IIUC, we may do migration on a wrong pmd entry, which may corrupt data.
Quoted 3 lines
IOW, and not for the first time(!), what are the worst-case userspace-visible effects of this bug?
Got it, will pay attention. Sorry for the trouble.
Signature · 2 lines
Wei Yang Help you, Help me
David Hildenbrand (Arm)
maintainerQuoted 42 lines · Balbir Singh
Quoted 11 lines · Wei Yang
For
pmd_trans_huge()andpmd_is_migration_entry(), we does following before return the pmd entry:* re-validate pmd entry
* checkPVMW_MIGRATION
*check_pmd()
* handle on pte level if split under usBut for device-private pmd, we just return after
pmd_lock(). This may lead to inproper situation.Could you elaborate a more on the improper situation?
Quoted 27 lines
This patch fixes commit
65edfda6f3f2("mm/rmap: extend rmap and migration support device-private entries") by following the same pattern aspmd_trans_huge()andpmd_is_migration_entry().Fixes 65edfda6f3f2 ("mm/rmap: extend rmap and migration support device-private entries")Signed-off-by Wei Yang <[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 <[email protected]>--- mm/page_vma_mapped.c | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-)mm/page_vma_mapped.c +1
-1@@ -269,21 +269,33 @@ 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); +softleaf_t entry =softleaf_from_pmd (pmde);
Quoted 16 lines
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 278
Quoted 13 lines
mm/page_vma_mapped.c +3
-8if (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, -PMD_ORDER ) && - (pvmw->nr_pages >=HPAGE_PMD_NR )) -sync_with_folio_pmd_zap (mm, pvmw->pmd); + entry =softleaf_from_pmd (*pvmw->pmd); + +if (softleaf_is_device_private (entry)) {Do we need to check
softleaf_is_device_private()twice, can't we hold the pmd lock and check once?
I think what we try to do here is, is to only grab the lock if we verified that there is something of interest in there.
I wonder if we should rewrite that whole thing to just do a pmd_same() check after grabbing the lock.
Something a lot cleaner like:
mm/page_vma_mapped.c +30 -33
@@ -242,40 +242,28 @@ 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)) { - 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; - } -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; - } -/* 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); - -if (softleaf_is_device_private (entry)) { - pvmw->ptl =pmd_lock (mm, pvmw->pmd); -return true; - } +if (pmd_present (pmde)) { +if (!pmd_leaf (pmde)) + goto pte_table; +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (!check_pmd (pmd_pfn (pmde), pvmw)) +return not_found (pvmw); + }else if (pmd_is_migration_entry (pmde)) { +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); + }else if (pmd_is_device_private_entry (pmde)) { +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); + }else { if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address, PMD_ORDER) &&
@@ -285,6 +273,15 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)step_forward (pvmw,PMD_SIZE );continue ; } + +/* Double-check under PTL that the PMD didn't change. */ + pvmw->ptl =pmd_lock (mm, pvmw->pmd); +if (pmd_same (pmde,pmdp_get (pvmw->pmd))) +return true; +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ; + goto restart; +pte_table:if (!map_pte (pvmw, &pmde, &ptl)) {if (!pvmw->pte)
There is likely room to clean this up / compress it further.
I'll note that this now also adds proper check_pmd() checks to pmd_is_device_private_entry().
The not_found(pvmw) if check_pmd() fails is rather weird ... but likely this works because THPs can really only be mapped through one PMD, and we always will look at the right spot ...
Signature · 3 lines
Cheers, David
Quoted 151 lines · David Hildenbrand (Arm)
Quoted 42 lines · Balbir Singh
Quoted 11 lines · Wei Yang
For
pmd_trans_huge()andpmd_is_migration_entry(), we does following before return the pmd entry:* re-validate pmd entry
* checkPVMW_MIGRATION
*check_pmd()
* handle on pte level if split under usBut for device-private pmd, we just return after
pmd_lock(). This may lead to inproper situation.Could you elaborate a more on the improper situation?
Quoted 27 lines
This patch fixes commit
65edfda6f3f2("mm/rmap: extend rmap and migration support device-private entries") by following the same pattern aspmd_trans_huge()andpmd_is_migration_entry().Fixes 65edfda6f3f2 ("mm/rmap: extend rmap and migration support device-private entries")Signed-off-by Wei Yang <[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 <[email protected]>--- mm/page_vma_mapped.c | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-)mm/page_vma_mapped.c +1
-1@@ -269,21 +269,33 @@ 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); +softleaf_t entry =softleaf_from_pmd (pmde);Quoted 16 lines
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 278
Quoted 13 lines
mm/page_vma_mapped.c +3
-8if (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, -PMD_ORDER ) && - (pvmw->nr_pages >=HPAGE_PMD_NR )) -sync_with_folio_pmd_zap (mm, pvmw->pmd); + entry =softleaf_from_pmd (*pvmw->pmd); + +if (softleaf_is_device_private (entry)) {Do we need to check
softleaf_is_device_private()twice, can't we hold the pmd lock and check once?I think what we try to do here is, is to only grab the lock if we verified that there is something of interest in there.
I wonder if we should rewrite that whole thing to just do a
pmd_same()check after grabbing the lock.Something a lot cleaner like:
mm/page_vma_mapped.c +30
-33@@ -242,40 +242,28 @@ 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)) { - 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; - } -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; - } -/* 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); - -if (softleaf_is_device_private (entry)) { - pvmw->ptl =pmd_lock (mm, pvmw->pmd); -return true; - } +if (pmd_present (pmde)) { +if (!pmd_leaf (pmde)) + goto pte_table; +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (!check_pmd (pmd_pfn (pmde), pvmw)) +return not_found (pvmw); + }else if (pmd_is_migration_entry (pmde)) { +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); + }else if (pmd_is_device_private_entry (pmde)) { +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); + }else { if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address, PMD_ORDER) &&@@ -285,6 +273,15 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)step_forward (pvmw,PMD_SIZE );continue ; } + +/* Double-check under PTL that the PMD didn't change. */ + pvmw->ptl =pmd_lock (mm, pvmw->pmd); +if (pmd_same (pmde,pmdp_get (pvmw->pmd))) +return true; +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ; + goto restart; +pte_table:if (!map_pte (pvmw, &pmde, &ptl)) {if (!pvmw->pte)
Quoted 1 line
There is likely room to clean this up / compress it further.
I tried to compress above logic like this, hope it could look cleaner.
if (pmd_trans_huge(pmde) || pmd_is_valid_softleaf(pmde)) {
unsigned long pfn;
bool is_migration = pmd_is_migration_entry(pmde);
bool for_migration = !!(pvmw->flags & PVMW_MIGRATION);
if (is_migration != for_migration)
return not_found(pvmw);
if (pmd_trans_huge(pmde))
pfn = pmd_pfn(pmde);
else
pfn = softleaf_to_pfn(softleaf_from_pmd(pmde));
if (!check_pmd(pfn, pvmw))
return not_found(pvmw);
} else if (!pmd_present(pmde)) {Quoted 9 lines
I'll note that this now also adds proper
check_pmd()checks topmd_is_device_private_entry().The
not_found(pvmw)ifcheck_pmd()fails is rather weird ... but likely this works because THPs can really only be mapped through one PMD, and we always will look at the right spot ...Signature · 3 lines
Cheers, David
Signature · 2 lines
Wei Yang Help you, Help me
David Hildenbrand (Arm)
maintainerQuoted 104 lines · Wei Yang
Quoted 99 lines · David Hildenbrand (Arm)
Quoted 6 lines · Balbir Singh
Could you elaborate a more on the improper situation?
Do we need to check
softleaf_is_device_private()twice, can't we hold the pmd lock and check once?I think what we try to do here is, is to only grab the lock if we verified that there is something of interest in there.
I wonder if we should rewrite that whole thing to just do a
pmd_same()check after grabbing the lock.Something a lot cleaner like:
mm/page_vma_mapped.c +30
-33@@ -242,40 +242,28 @@ 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)) { - 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; - } -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; - } -/* 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); - -if (softleaf_is_device_private (entry)) { - pvmw->ptl =pmd_lock (mm, pvmw->pmd); -return true; - } +if (pmd_present (pmde)) { +if (!pmd_leaf (pmde)) + goto pte_table; +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (!check_pmd (pmd_pfn (pmde), pvmw)) +return not_found (pvmw); + }else if (pmd_is_migration_entry (pmde)) { +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); + }else if (pmd_is_device_private_entry (pmde)) { +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); + }else { if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address, PMD_ORDER) &&@@ -285,6 +273,15 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)step_forward (pvmw,PMD_SIZE );continue ; } + +/* Double-check under PTL that the PMD didn't change. */ + pvmw->ptl =pmd_lock (mm, pvmw->pmd); +if (pmd_same (pmde,pmdp_get (pvmw->pmd))) +return true; +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ; + goto restart; +pte_table:if (!map_pte (pvmw, &pmde, &ptl)) {if (!pvmw->pte)Quoted 1 line
There is likely room to clean this up / compress it further.
I tried to compress above logic like this, hope it could look cleaner.
Quoted 16 lines
if (pmd_trans_huge(pmde) || pmd_is_valid_softleaf(pmde)) {
unsigned long pfn;
bool is_migration = pmd_is_migration_entry(pmde);
bool for_migration = !!(pvmw->flags & PVMW_MIGRATION);
if (is_migration != for_migration)
return not_found(pvmw);
if (pmd_trans_huge(pmde))
pfn = pmd_pfn(pmde);
else
pfn = softleaf_to_pfn(softleaf_from_pmd(pmde));
if (!check_pmd(pfn, pvmw))
return not_found(pvmw);
} else if (!pmd_present(pmde)) {It's more compact, but not necessarily cleaner. In particular, I detest pmd_trans_huge(), we should phase it out.
if (pmd_present(pmde) && !pmd_leaf(pmde)) {
goto pte_table;
} else if (pmd_present(pmde) || pmd_is_valid_softleaf(pmde))...
Might work as well. But once we add support for other softleaf types, we'll have to touch it again. So I'd rather just list what we actually expect.
Signature · 3 lines
Cheers, David
Quoted 105 lines · David Hildenbrand (Arm)
Quoted 104 lines · Wei Yang
Quoted 99 lines · David Hildenbrand (Arm)
Quoted 6 lines · Balbir Singh
Could you elaborate a more on the improper situation?
Do we need to check
softleaf_is_device_private()twice, can't we hold the pmd lock and check once?I think what we try to do here is, is to only grab the lock if we verified that there is something of interest in there.
I wonder if we should rewrite that whole thing to just do a
pmd_same()check after grabbing the lock.Something a lot cleaner like:
mm/page_vma_mapped.c +30
-33@@ -242,40 +242,28 @@ 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)) { - 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; - } -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; - } -/* 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); - -if (softleaf_is_device_private (entry)) { - pvmw->ptl =pmd_lock (mm, pvmw->pmd); -return true; - } +if (pmd_present (pmde)) { +if (!pmd_leaf (pmde)) + goto pte_table; +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (!check_pmd (pmd_pfn (pmde), pvmw)) +return not_found (pvmw); + }else if (pmd_is_migration_entry (pmde)) { +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); + }else if (pmd_is_device_private_entry (pmde)) { +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); + }else { if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address, PMD_ORDER) &&@@ -285,6 +273,15 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)step_forward (pvmw,PMD_SIZE );continue ; } + +/* Double-check under PTL that the PMD didn't change. */ + pvmw->ptl =pmd_lock (mm, pvmw->pmd); +if (pmd_same (pmde,pmdp_get (pvmw->pmd))) +return true; +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ; + goto restart; +pte_table:if (!map_pte (pvmw, &pmde, &ptl)) {if (!pvmw->pte)Quoted 1 line
There is likely room to clean this up / compress it further.
I tried to compress above logic like this, hope it could look cleaner.
Quoted 8 lines
Quoted 8 lines
if (pmd_trans_huge(pmde) || pmd_is_valid_softleaf(pmde)) { unsigned long pfn; bool is_migration = pmd_is_migration_entry(pmde); bool for_migration = !!(pvmw->flags & PVMW_MIGRATION); if (is_migration != for_migration) return not_found(pvmw);
I got some time to look at PVMW_MIGRATION, remove_migration_ptes is invoked for device private pages, would we want them to skip device private pmd pages?
Quoted 12 lines
Quoted 8 lines
if (pmd_trans_huge(pmde)) pfn = pmd_pfn(pmde); else pfn = softleaf_to_pfn(softleaf_from_pmd(pmde)); if (!check_pmd(pfn, pvmw)) return not_found(pvmw); } else if (!pmd_present(pmde)) {It's more compact, but not necessarily cleaner. In particular, I detest
pmd_trans_huge(), we should phase it out.
Quoted 9 lines
if (pmd_present(pmde) && !pmd_leaf(pmde)) { goto pte_table; } else if (pmd_present(pmde) || pmd_is_valid_softleaf(pmde))...
Might work as well. But once we add support for other softleaf types, we'll have to touch it again. So I'd rather just list what we actually expect.
Balbir
Quoted 111 lines · Balbir Singh
Quoted 110 lines · David Hildenbrand (Arm)
Quoted 109 lines · Wei Yang
Quoted 99 lines · David Hildenbrand (Arm)
Quoted 6 lines · Balbir Singh
Could you elaborate a more on the improper situation?
Do we need to check
softleaf_is_device_private()twice, can't we hold the pmd lock and check once?I think what we try to do here is, is to only grab the lock if we verified that there is something of interest in there.
I wonder if we should rewrite that whole thing to just do a
pmd_same()check after grabbing the lock.Something a lot cleaner like:
mm/page_vma_mapped.c +30
-33@@ -242,40 +242,28 @@ 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)) { - 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; - } -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; - } -/* 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); - -if (softleaf_is_device_private (entry)) { - pvmw->ptl =pmd_lock (mm, pvmw->pmd); -return true; - } +if (pmd_present (pmde)) { +if (!pmd_leaf (pmde)) + goto pte_table; +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (!check_pmd (pmd_pfn (pmde), pvmw)) +return not_found (pvmw); + }else if (pmd_is_migration_entry (pmde)) { +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); + }else if (pmd_is_device_private_entry (pmde)) { +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); + }else { if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address, PMD_ORDER) &&@@ -285,6 +273,15 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)step_forward (pvmw,PMD_SIZE );continue ; } + +/* Double-check under PTL that the PMD didn't change. */ + pvmw->ptl =pmd_lock (mm, pvmw->pmd); +if (pmd_same (pmde,pmdp_get (pvmw->pmd))) +return true; +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ; + goto restart; +pte_table:if (!map_pte (pvmw, &pmde, &ptl)) {if (!pvmw->pte)Quoted 1 line
There is likely room to clean this up / compress it further.
I tried to compress above logic like this, hope it could look cleaner.
if (pmd_trans_huge(pmde) || pmd_is_valid_softleaf(pmde)) { unsigned long pfn; bool is_migration = pmd_is_migration_entry(pmde); bool for_migration = !!(pvmw->flags & PVMW_MIGRATION);
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
if (is_migration != for_migration) return not_found(pvmw);I got some time to look at
PVMW_MIGRATION,remove_migration_ptesis invoked for device private pages, would we want them to skip device private pmd pages?
Not get you clearly.
You mean skip device-private pmd page in remove_migration_ptes()?
Quoted 23 lines
Quoted 12 lines
Quoted 8 lines
if (pmd_trans_huge(pmde)) pfn = pmd_pfn(pmde); else pfn = softleaf_to_pfn(softleaf_from_pmd(pmde)); if (!check_pmd(pfn, pvmw)) return not_found(pvmw); } else if (!pmd_present(pmde)) {It's more compact, but not necessarily cleaner. In particular, I detest
pmd_trans_huge(), we should phase it out.Quoted 9 lines
if (pmd_present(pmde) && !pmd_leaf(pmde)) { goto pte_table; } else if (pmd_present(pmde) || pmd_is_valid_softleaf(pmde))...
Might work as well. But once we add support for other softleaf types, we'll have to touch it again. So I'd rather just list what we actually expect.
Balbir
Signature · 2 lines
Wei Yang Help you, Help me
Quoted 116 lines · Wei Yang
Quoted 115 lines · Balbir Singh
Quoted 113 lines · David Hildenbrand (Arm)
Quoted 112 lines · Wei Yang
Quoted 99 lines · David Hildenbrand (Arm)
Quoted 6 lines · Balbir Singh
Could you elaborate a more on the improper situation?
Do we need to check
softleaf_is_device_private()twice, can't we hold the pmd lock and check once?I think what we try to do here is, is to only grab the lock if we verified that there is something of interest in there.
I wonder if we should rewrite that whole thing to just do a
pmd_same()check after grabbing the lock.Something a lot cleaner like:
mm/page_vma_mapped.c +30
-33@@ -242,40 +242,28 @@ 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)) { - 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; - } -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; - } -/* 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); - -if (softleaf_is_device_private (entry)) { - pvmw->ptl =pmd_lock (mm, pvmw->pmd); -return true; - } +if (pmd_present (pmde)) { +if (!pmd_leaf (pmde)) + goto pte_table; +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (!check_pmd (pmd_pfn (pmde), pvmw)) +return not_found (pvmw); + }else if (pmd_is_migration_entry (pmde)) { +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); + }else if (pmd_is_device_private_entry (pmde)) { +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); + }else { if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address, PMD_ORDER) &&@@ -285,6 +273,15 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)step_forward (pvmw,PMD_SIZE );continue ; } + +/* Double-check under PTL that the PMD didn't change. */ + pvmw->ptl =pmd_lock (mm, pvmw->pmd); +if (pmd_same (pmde,pmdp_get (pvmw->pmd))) +return true; +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ; + goto restart; +pte_table:if (!map_pte (pvmw, &pmde, &ptl)) {if (!pvmw->pte)Quoted 1 line
There is likely room to clean this up / compress it further.
I tried to compress above logic like this, hope it could look cleaner.
if (pmd_trans_huge(pmde) || pmd_is_valid_softleaf(pmde)) { unsigned long pfn; bool is_migration = pmd_is_migration_entry(pmde); bool for_migration = !!(pvmw->flags & PVMW_MIGRATION); if (is_migration != for_migration) return not_found(pvmw);
Quoted 9 lines
Quoted 4 lines
I got some time to look at
PVMW_MIGRATION,remove_migration_ptesis invoked for device private pages, would we want them to skip device private pmd pages?Not get you clearly.
You mean skip device-private pmd page in
remove_migration_ptes()?
Yes, could you elaborate on why it's OK to skip device-private pmd pages in remove_migration_ptes() and if so what is the impact?
Quoted 24 lines
Quoted 23 lines
Quoted 12 lines
Quoted 8 lines
if (pmd_trans_huge(pmde)) pfn = pmd_pfn(pmde); else pfn = softleaf_to_pfn(softleaf_from_pmd(pmde)); if (!check_pmd(pfn, pvmw)) return not_found(pvmw); } else if (!pmd_present(pmde)) {It's more compact, but not necessarily cleaner. In particular, I detest
pmd_trans_huge(), we should phase it out.Quoted 9 lines
if (pmd_present(pmde) && !pmd_leaf(pmde)) { goto pte_table; } else if (pmd_present(pmde) || pmd_is_valid_softleaf(pmde))...
Might work as well. But once we add support for other softleaf types, we'll have to touch it again. So I'd rather just list what we actually expect.
Balbir
Quoted 116 lines · Wei Yang
Quoted 115 lines · Balbir Singh
Quoted 113 lines · David Hildenbrand (Arm)
Quoted 112 lines · Wei Yang
Quoted 99 lines · David Hildenbrand (Arm)
Quoted 6 lines · Balbir Singh
Could you elaborate a more on the improper situation?
Do we need to check
softleaf_is_device_private()twice, can't we hold the pmd lock and check once?I think what we try to do here is, is to only grab the lock if we verified that there is something of interest in there.
I wonder if we should rewrite that whole thing to just do a
pmd_same()check after grabbing the lock.Something a lot cleaner like:
mm/page_vma_mapped.c +30
-33@@ -242,40 +242,28 @@ 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)) { - 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; - } -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; - } -/* 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); - -if (softleaf_is_device_private (entry)) { - pvmw->ptl =pmd_lock (mm, pvmw->pmd); -return true; - } +if (pmd_present (pmde)) { +if (!pmd_leaf (pmde)) + goto pte_table; +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (!check_pmd (pmd_pfn (pmde), pvmw)) +return not_found (pvmw); + }else if (pmd_is_migration_entry (pmde)) { +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); + }else if (pmd_is_device_private_entry (pmde)) { +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); + }else { if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address, PMD_ORDER) &&@@ -285,6 +273,15 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)step_forward (pvmw,PMD_SIZE );continue ; } + +/* Double-check under PTL that the PMD didn't change. */ + pvmw->ptl =pmd_lock (mm, pvmw->pmd); +if (pmd_same (pmde,pmdp_get (pvmw->pmd))) +return true; +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ; + goto restart; +pte_table:if (!map_pte (pvmw, &pmde, &ptl)) {if (!pvmw->pte)Quoted 1 line
There is likely room to clean this up / compress it further.
I tried to compress above logic like this, hope it could look cleaner.
if (pmd_trans_huge(pmde) || pmd_is_valid_softleaf(pmde)) { unsigned long pfn; bool is_migration = pmd_is_migration_entry(pmde); bool for_migration = !!(pvmw->flags & PVMW_MIGRATION); if (is_migration != for_migration) return not_found(pvmw);
Quoted 8 lines
Quoted 4 lines
I got some time to look at
PVMW_MIGRATION,remove_migration_ptesis invoked for device private pages, would we want them to skip device private pmd pages?Not get you clearly.
You mean skip device-private pmd page in
remove_migration_ptes()?
I had a look and Alistair pointed out, your proposed changes are correct.
Thanks,
Balbir
Quoted 125 lines · David Hildenbrand (Arm)
Quoted 104 lines · Wei Yang
Quoted 99 lines · David Hildenbrand (Arm)
Quoted 6 lines · Balbir Singh
Could you elaborate a more on the improper situation?
Do we need to check
softleaf_is_device_private()twice, can't we hold the pmd lock and check once?I think what we try to do here is, is to only grab the lock if we verified that there is something of interest in there.
I wonder if we should rewrite that whole thing to just do a
pmd_same()check after grabbing the lock.Something a lot cleaner like:
mm/page_vma_mapped.c +30
-33@@ -242,40 +242,28 @@ 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)) { - 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; - } -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; - } -/* 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); - -if (softleaf_is_device_private (entry)) { - pvmw->ptl =pmd_lock (mm, pvmw->pmd); -return true; - } +if (pmd_present (pmde)) { +if (!pmd_leaf (pmde)) + goto pte_table; +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (!check_pmd (pmd_pfn (pmde), pvmw)) +return not_found (pvmw); + }else if (pmd_is_migration_entry (pmde)) { +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); + }else if (pmd_is_device_private_entry (pmde)) { +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); + }else { if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address, PMD_ORDER) &&@@ -285,6 +273,15 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)step_forward (pvmw,PMD_SIZE );continue ; } + +/* Double-check under PTL that the PMD didn't change. */ + pvmw->ptl =pmd_lock (mm, pvmw->pmd); +if (pmd_same (pmde,pmdp_get (pvmw->pmd))) +return true; +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ; + goto restart; +pte_table:if (!map_pte (pvmw, &pmde, &ptl)) {if (!pvmw->pte)Quoted 1 line
There is likely room to clean this up / compress it further.
I tried to compress above logic like this, hope it could look cleaner.
Quoted 16 lines
if (pmd_trans_huge(pmde) || pmd_is_valid_softleaf(pmde)) { unsigned long pfn; bool is_migration = pmd_is_migration_entry(pmde); bool for_migration = !!(pvmw->flags & PVMW_MIGRATION); if (is_migration != for_migration) return not_found(pvmw); if (pmd_trans_huge(pmde)) pfn = pmd_pfn(pmde); else pfn = softleaf_to_pfn(softleaf_from_pmd(pmde)); if (!check_pmd(pfn, pvmw)) return not_found(pvmw); } else if (!pmd_present(pmde)) {It's more compact, but not necessarily cleaner. In particular, I detest
pmd_trans_huge(), we should phase it out.
Quoted 9 lines
if (pmd_present(pmde) && !pmd_leaf(pmde)) { goto pte_table; } else if (pmd_present(pmde) || pmd_is_valid_softleaf(pmde))...
Might work as well. But once we add support for other softleaf types, we'll have to touch it again. So I'd rather just list what we actually expect.
Hi, David
I may not follow you. Just want to confirm whether you prefer this goes as a fix first, or you prefer it goes as what you suggested here as a cleanup?
Quoted 4 lines
Signature · 3 lines
Cheers, David
Signature · 2 lines
Wei Yang Help you, Help me
David Hildenbrand (Arm)
maintainerQuoted 29 lines · Wei Yang
Quoted 28 lines · David Hildenbrand (Arm)
Quoted 19 lines · Wei Yang
I tried to compress above logic like this, hope it could look cleaner.
if (pmd_trans_huge(pmde) || pmd_is_valid_softleaf(pmde)) { unsigned long pfn; bool is_migration = pmd_is_migration_entry(pmde); bool for_migration = !!(pvmw->flags & PVMW_MIGRATION); if (is_migration != for_migration) return not_found(pvmw); if (pmd_trans_huge(pmde)) pfn = pmd_pfn(pmde); else pfn = softleaf_to_pfn(softleaf_from_pmd(pmde)); if (!check_pmd(pfn, pvmw)) return not_found(pvmw); } else if (!pmd_present(pmde)) {It's more compact, but not necessarily cleaner. In particular, I detest
pmd_trans_huge(), we should phase it out.if (pmd_present(pmde) && !pmd_leaf(pmde)) { goto pte_table; } else if (pmd_present(pmde) || pmd_is_valid_softleaf(pmde))
Quoted 7 lines
Quoted 5 lines
...
Might work as well. But once we add support for other softleaf types, we'll have to touch it again. So I'd rather just list what we actually expect.
Hi, David
Hi,
Quoted 3 lines
I may not follow you. Just want to confirm whether you prefer this goes as a fix first, or you prefer it goes as what you suggested here as a cleanup?
I guess we should just do it properly when we're touching the code already.
Does that answer your question? Will you send a proper patch?
Signature · 3 lines
Cheers, David
Quoted 38 lines · David Hildenbrand (Arm)
Quoted 29 lines · Wei Yang
Quoted 28 lines · David Hildenbrand (Arm)
Quoted 19 lines · Wei Yang
I tried to compress above logic like this, hope it could look cleaner.
if (pmd_trans_huge(pmde) || pmd_is_valid_softleaf(pmde)) { unsigned long pfn; bool is_migration = pmd_is_migration_entry(pmde); bool for_migration = !!(pvmw->flags & PVMW_MIGRATION); if (is_migration != for_migration) return not_found(pvmw); if (pmd_trans_huge(pmde)) pfn = pmd_pfn(pmde); else pfn = softleaf_to_pfn(softleaf_from_pmd(pmde)); if (!check_pmd(pfn, pvmw)) return not_found(pvmw); } else if (!pmd_present(pmde)) {It's more compact, but not necessarily cleaner. In particular, I detest
pmd_trans_huge(), we should phase it out.if (pmd_present(pmde) && !pmd_leaf(pmde)) { goto pte_table; } else if (pmd_present(pmde) || pmd_is_valid_softleaf(pmde))Quoted 7 lines
Quoted 5 lines
...
Might work as well. But once we add support for other softleaf types, we'll have to touch it again. So I'd rather just list what we actually expect.
Hi, David
Quoted 7 lines
Hi,
Quoted 3 lines
I may not follow you. Just want to confirm whether you prefer this goes as a fix first, or you prefer it goes as what you suggested here as a cleanup?
I guess we should just do it properly when we're touching the code already.
Ok, IIUC, I will prepare the fix as you mentioned above.
Quoted 3 lines
Does that answer your question? Will you send a proper patch?
Sure.
Quoted 4 lines
Signature · 3 lines
Cheers, David
Signature · 2 lines
Wei Yang Help you, Help me
Lance Yang
reviewerQuoted 5 lines · David Hildenbrand (Arm)
Quoted 4 lines · Wei Yang
Quoted 3 lines · David Hildenbrand (Arm)
Quoted 2 lines · Wei Yang
I tried to compress above logic like this, hope it could look cleaner.
Emm ... spelling out the present/migration/device-private cases makes this easier to review, and avoids hiding future softleaf types behind pmd_is_valid_softleaf(), IMHO.
So I'd prefer David's explicit version[1].
Quoted 33 lines
Quoted 25 lines
Quoted 25 lines
Quoted 17 lines
if (pmd_trans_huge(pmde) || pmd_is_valid_softleaf(pmde)) { unsigned long pfn; bool is_migration = pmd_is_migration_entry(pmde); bool for_migration = !!(pvmw->flags & PVMW_MIGRATION); if (is_migration != for_migration) return not_found(pvmw); if (pmd_trans_huge(pmde)) pfn = pmd_pfn(pmde); else pfn = softleaf_to_pfn(softleaf_from_pmd(pmde)); if (!check_pmd(pfn, pvmw)) return not_found(pvmw); } else if (!pmd_present(pmde)) {It's more compact, but not necessarily cleaner. In particular, I detest
pmd_trans_huge(), we should phase it out.if (pmd_present(pmde) && !pmd_leaf(pmde)) { goto pte_table; } else if (pmd_present(pmde) || pmd_is_valid_softleaf(pmde))Quoted 7 lines
Quoted 5 lines
...
Might work as well. But once we add support for other softleaf types, we'll have to touch it again. So I'd rather just list what we actually expect.
Hi, David
Quoted 7 lines
Hi,
Quoted 3 lines
I may not follow you. Just want to confirm whether you prefer this goes as a fix first, or you prefer it goes as what you suggested here as a cleanup?
I guess we should just do it properly when we're touching the code already.
+1 to that ;)
Quoted 1 line
Does that answer your question? Will you send a proper patch?
Copied the diff from [1] below, with a couple of tiny nits inline. Feel free to grab any of this if it looks sane :)
---8<---
mm/page_vma_mapped.c +9 -33
@@ -242,40 +242,28 @@ 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)) { - 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; - } -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; - } -/* 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); - -if (softleaf_is_device_private (entry)) { - pvmw->ptl =pmd_lock (mm, pvmw->pmd); -return true; - } +if (pmd_present (pmde)) { +if (!pmd_leaf (pmde)) + goto pte_table; +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (!check_pmd (pmd_pfn (pmde), pvmw)) +return not_found (pvmw); + }else if (pmd_is_migration_entry (pmde)) { +softleaf_t entry =softleaf_from_pmd (pmde);
Could be const.
+
+ if (!(pvmw->flags & PVMW_MIGRATION))
+ return not_found(pvmw);
+ if (!check_pmd(softleaf_to_pfn(entry), pvmw))
+ return not_found(pvmw);
+ } else if (pmd_is_device_private_entry(pmde)) {
+ softleaf_t entry = softleaf_from_pmd(pmde);
Ditto.
+ if (pvmw->flags & PVMW_MIGRATION)
+ return not_found(pvmw);
+ if (!check_pmd(softleaf_to_pfn(entry), pvmw))
+ return not_found(pvmw);
+ } else {
if ((pvmw->flags & PVMW_SYNC) &&
thp_vma_suitable_order(vma, pvmw->address,
PMD_ORDER) &&
@@ -285,6 +273,15 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
step_forward(pvmw, PMD_SIZE);
continue;
}
+
+ /* Double-check under PTL that the PMD didn't change. */
+ pvmw->ptl = pmd_lock(mm, pvmw->pmd);
+ if (pmd_same(pmde, pmdp_get(pvmw->pmd)))Maybe worth a likely() here? The PMD normally shouldn't change under us.
+ return true;
+ spin_unlock(pvmw->ptl);
+ pvmw->ptl = NULL;
+ goto restart;
+pte_table:
if (!map_pte(pvmw, &pmde, &ptl)) {
if (!pvmw->pte)---
[1] https://lore.kernel.org/linux-mm/[email protected]/
Cheers, Lance
Quoted 53 lines · Lance Yang
Quoted 5 lines · David Hildenbrand (Arm)
Quoted 4 lines · Wei Yang
Quoted 3 lines · David Hildenbrand (Arm)
Quoted 2 lines · Wei Yang
I tried to compress above logic like this, hope it could look cleaner.
Emm ... spelling out the present/migration/device-private cases makes this easier to review, and avoids hiding future softleaf types behind
pmd_is_valid_softleaf(), IMHO.So I'd prefer David's explicit version[1].
Quoted 39 lines
Quoted 25 lines
Quoted 25 lines
Quoted 17 lines
if (pmd_trans_huge(pmde) || pmd_is_valid_softleaf(pmde)) { unsigned long pfn; bool is_migration = pmd_is_migration_entry(pmde); bool for_migration = !!(pvmw->flags & PVMW_MIGRATION); if (is_migration != for_migration) return not_found(pvmw); if (pmd_trans_huge(pmde)) pfn = pmd_pfn(pmde); else pfn = softleaf_to_pfn(softleaf_from_pmd(pmde)); if (!check_pmd(pfn, pvmw)) return not_found(pvmw); } else if (!pmd_present(pmde)) {It's more compact, but not necessarily cleaner. In particular, I detest
pmd_trans_huge(), we should phase it out.if (pmd_present(pmde) && !pmd_leaf(pmde)) { goto pte_table; } else if (pmd_present(pmde) || pmd_is_valid_softleaf(pmde))Quoted 7 lines
Quoted 5 lines
...
Might work as well. But once we add support for other softleaf types, we'll have to touch it again. So I'd rather just list what we actually expect.
Hi, David
Hi,
Quoted 3 lines
I may not follow you. Just want to confirm whether you prefer this goes as a fix first, or you prefer it goes as what you suggested here as a cleanup?
Quoted 8 lines
Quoted 1 line
I guess we should just do it properly when we're touching the code already.
+1 to that ;)
Quoted 1 line
Does that answer your question? Will you send a proper patch?
Copied the diff from [1] below, with a couple of tiny nits inline. Feel free to grab any of this if it looks sane :)
Hi, Lance
Thanks for taking a look.
I just sent v2 [2] with the suggestion form David from [1]. So didn't include your suggestion yet.
If it doesn't bother too much, would you mind reply there so that we could have the same base line?
Thanks
[2]: https://lore.kernel.org/all/[email protected]/T/#u
Quoted 96 lines
---8<---
mm/page_vma_mapped.c +9
-33@@ -242,40 +242,28 @@ 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)) { - 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; - } -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; - } -/* 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); - -if (softleaf_is_device_private (entry)) { - pvmw->ptl =pmd_lock (mm, pvmw->pmd); -return true; - } +if (pmd_present (pmde)) { +if (!pmd_leaf (pmde)) + goto pte_table; +if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (!check_pmd (pmd_pfn (pmde), pvmw)) +return not_found (pvmw); + }else if (pmd_is_migration_entry (pmde)) { +softleaf_t entry =softleaf_from_pmd (pmde);Could be const.
mm/page_vma_mapped.c +7
-0+ +if (!(pvmw->flags &PVMW_MIGRATION )) +return not_found (pvmw); +if (!check_pmd (softleaf_to_pfn (entry), pvmw)) +return not_found (pvmw); + }else if (pmd_is_device_private_entry (pmde)) { +softleaf_t entry =softleaf_from_pmd (pmde);Ditto.
mm/page_vma_mapped.c +9
-0+if (pvmw->flags &PVMW_MIGRATION ) +return not_found (pvmw); +if (!check_pmd (softleaf_to_pfn (entry), pvmw)) +return not_found (pvmw); + }else { if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address, PMD_ORDER) &&@@ -285,6 +273,15 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)step_forward (pvmw,PMD_SIZE );continue ; } + +/* Double-check under PTL that the PMD didn't change. */ + pvmw->ptl =pmd_lock (mm, pvmw->pmd); +if (pmd_same (pmde,pmdp_get (pvmw->pmd)))Maybe worth a
likely()here? The PMD normally shouldn't change under us.mm/page_vma_mapped.c +5
-1+return true; +spin_unlock (pvmw->ptl); + pvmw->ptl =NULL ; + goto restart; +pte_table:if (!map_pte (pvmw, &pmde, &ptl)) {if (!pvmw->pte) ---[1] https://lore.kernel.org/linux-mm/[email protected]/
Cheers, Lance
Signature · 2 lines
Wei Yang Help you, Help me