mm/page_vma_mapped_walk: Use ptep_get_lockless() for lockless access
Subsystems (3)
Alexander Gordeev
maintainerPATCH v2 1/1 mm/page_vma_mapped_walk: Use ptep_get_lockless() for lockless access
Switch from ptep_get() to ptep_get_lockless() accessor for PTE reads when no lock is taken.
--- mm/page_vma_mapped.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
mm/page_vma_mapped.c +2 -2
@@ -41,7 +41,7 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp,if (!pvmw->pte)return false; - ptent =ptep_get (pvmw->pte); + ptent =ptep_get_lockless (pvmw->pte);if (pte_none (ptent)) {return false;
@@ -310,7 +310,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) goto restart; } pvmw->pte++; - }while (pte_none (ptep_get (pvmw->pte))); + }while (pte_none (ptep_get_lockless (pvmw->pte)));if (!pvmw->ptl) {spin_lock (ptl);
Signature · 1 line
2.51.0
Quoted 22 lines · Alexander Gordeev
Switch from
ptep_get()toptep_get_lockless()accessor for PTE reads when no lock is taken.Signed-off-by Alexander Gordeev <[email protected]>---
mm/page_vma_mapped.c| 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)mm/page_vma_mapped.c +1
-1@@ -41,7 +41,7 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp,if (!pvmw->pte)return false; - ptent =ptep_get (pvmw->pte); + ptent =ptep_get_lockless (pvmw->pte);if (pte_none (ptent)) {return false;
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 313
Quoted 5 lines
mm/page_vma_mapped.c +1
-1@@ -310,7 +310,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) goto restart; } pvmw->pte++; - }while (pte_none (ptep_get (pvmw->pte))); + }while (pte_none (ptep_get_lockless (pvmw->pte)));
As Oscar mentioned in lkml.org/lkml/2026/4/27/630, map_pte() may take the lock. So probably it is not right?
Quoted 6 lines
mm/page_vma_mapped.c +0
-0if (!pvmw->ptl) {spin_lock (ptl);Signature · 1 line
2.51.0
Signature · 2 lines
Wei Yang Help you, Help me
Alexander Gordeev
maintainerQuoted 9 lines · Wei Yang
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 313
Quoted 6 lines
mm/page_vma_mapped.c +1
-1@@ -310,7 +310,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) goto restart; } pvmw->pte++; - }while (pte_none (ptep_get (pvmw->pte))); + }while (pte_none (ptep_get_lockless (pvmw->pte)));As Oscar mentioned in lkml.org/lkml/2026/4/27/630,
map_pte()may take the lock. So probably it is not right?
If I read the code correctly map_pte() might take the lock, but also might not take it. If it took the lock and uses ptep_get_lockless(), then it is fine. But if it did not take the lock and uses ptep_get(), then it is an issue.
Quoted 9 lines
Quoted 6 lines
mm/page_vma_mapped.c +0
-0if (!pvmw->ptl) {spin_lock (ptl);Signature · 1 line
2.51.0Signature · 1 line
Wei Yang
Thanks!
Quoted 11 lines · Alexander Gordeev
Quoted 9 lines · Wei Yang
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 313
Quoted 6 lines
mm/page_vma_mapped.c +1
-1@@ -310,7 +310,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) goto restart; } pvmw->pte++; - }while (pte_none (ptep_get (pvmw->pte))); + }while (pte_none (ptep_get_lockless (pvmw->pte)));As Oscar mentioned in lkml.org/lkml/2026/4/27/630,
map_pte()may take the lock. So probably it is not right?
Quoted 5 lines
If I read the code correctly
map_pte()might take the lock, but also might not take it. If it took the lock and usesptep_get_lockless(), then it is fine. But if it did not take the lock and usesptep_get(), then it is an issue.
So the rule here is:
* ptep_get_lockless() could be used for locked and not locked
* ptep_get() only used when locked
Right?
Quoted 11 lines
Quoted 9 lines
Quoted 6 lines
mm/page_vma_mapped.c +0
-0if (!pvmw->ptl) {spin_lock (ptl);Signature · 1 line
2.51.0Signature · 1 line
Wei YangThanks!
Signature · 2 lines
Wei Yang Help you, Help me
Alexander Gordeev
maintainerQuoted 18 lines · Wei Yang
Quoted 11 lines · Alexander Gordeev
Quoted 9 lines · Wei Yang
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 313
Quoted 6 lines
mm/page_vma_mapped.c +1
-1@@ -310,7 +310,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) goto restart; } pvmw->pte++; - }while (pte_none (ptep_get (pvmw->pte))); + }while (pte_none (ptep_get_lockless (pvmw->pte)));As Oscar mentioned in lkml.org/lkml/2026/4/27/630,
map_pte()may take the lock. So probably it is not right?Quoted 5 lines
If I read the code correctly
map_pte()might take the lock, but also might not take it. If it took the lock and usesptep_get_lockless(), then it is fine. But if it did not take the lock and usesptep_get(), then it is an issue.
Quoted 6 lines
So the rule here is:
*
ptep_get_lockless()could be used for locked and not locked
*ptep_get()only used when lockedRight?
Yes, this is my assumption.
Quoted 15 lines
Quoted 11 lines
Quoted 9 lines
Quoted 6 lines
mm/page_vma_mapped.c +0
-0if (!pvmw->ptl) {spin_lock (ptl);Signature · 1 line
2.51.0Signature · 1 line
Wei YangThanks!
Signature · 2 lines
Wei Yang Help you, Help me
Quoted 19 lines · Alexander Gordeev
Quoted 18 lines · Wei Yang
Quoted 16 lines · Alexander Gordeev
Quoted 9 lines · Wei Yang
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 313
Quoted 6 lines
mm/page_vma_mapped.c +1
-1@@ -310,7 +310,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) goto restart; } pvmw->pte++; - }while (pte_none (ptep_get (pvmw->pte))); + }while (pte_none (ptep_get_lockless (pvmw->pte)));As Oscar mentioned in lkml.org/lkml/2026/4/27/630,
map_pte()may take the lock. So probably it is not right?If I read the code correctly
map_pte()might take the lock, but also might not take it. If it took the lock and usesptep_get_lockless(), then it is fine. But if it did not take the lock and usesptep_get(), then it is an issue.
Quoted 9 lines
Quoted 6 lines
So the rule here is:
*
ptep_get_lockless()could be used for locked and not locked
*ptep_get()only used when lockedRight?
Yes, this is my assumption.
Thanks, if so, it looks good.
Quoted 15 lines
Quoted 15 lines
Quoted 11 lines
Quoted 9 lines
Quoted 6 lines
mm/page_vma_mapped.c +0
-0if (!pvmw->ptl) {spin_lock (ptl);Signature · 1 line
2.51.0Signature · 1 line
Wei YangThanks!
Signature · 2 lines
Wei Yang Help you, Help me
Signature · 2 lines
Wei Yang Help you, Help me
David Hildenbrand (Arm)
maintainerQuoted 9 lines · Alexander Gordeev
Quoted 8 lines · Wei Yang
Quoted 6 lines · Alexander Gordeev
If I read the code correctly
map_pte()might take the lock, but also might not take it. If it took the lock and usesptep_get_lockless(), then it is fine. But if it did not take the lock and usesptep_get(), then it is an issue.
Quoted 8 lines
Quoted 6 lines
So the rule here is:
*
ptep_get_lockless()could be used for locked and not locked
*ptep_get()only used when lockedRight?
Yes, this is my assumption.
I agree, ptep_get_lockless() simply makes sense to return something sensible if there are concurrent modifications (which cannot happen when the PTL is held).
That's why only 32bit with 64bit PTEs and arm64 even has to special-case it.
We should clarify in the patch description that in the do-while loop, we might or might not hold the PTL, and that calling ptep_get_lockless() with the PTL held is OK.
I wonder if it's more efficient and clearer, to use the correct variant, though?
mm/page_vma_mapped.c +6 -1
@@ -187,6 +187,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)p4d_t *p4d;pud_t *pud;pmd_t pmde; +pte_t pteval;/* The only possible pmd mapping has been handled on last iteration */ if (pvmw->pmd && !pvmw->pte)
@@ -310,7 +311,11 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) goto restart; } pvmw->pte++; - }while (pte_none (ptep_get (pvmw->pte))); +if (!pvmw->ptl) + pteval =ptep_get_lockless (pvmw->pte); +else + pteval =ptep_get (pvmw->pte); + }while (pte_none (pteval));if (!pvmw->ptl) {spin_lock (ptl);
Signature · 3 lines
Cheers, David
Alexander Gordeev
maintainerQuoted 35 lines · David Hildenbrand (Arm)
Quoted 8 lines
Quoted 6 lines
So the rule here is:
*
ptep_get_lockless()could be used for locked and not locked
*ptep_get()only used when lockedRight?
Yes, this is my assumption.
I agree,
ptep_get_lockless()simply makes sense to return something sensible if there are concurrent modifications (which cannot happen when the PTL is held).That's why only 32bit with 64bit PTEs and arm64 even has to special-case it.
We should clarify in the patch description that in the do-while loop, we might or might not hold the PTL, and that calling
ptep_get_lockless()with the PTL held is OK.I wonder if it's more efficient and clearer, to use the correct variant, though?
mm/page_vma_mapped.c +1
-0@@ -187,6 +187,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)p4d_t *p4d;pud_t *pud;pmd_t pmde; +pte_t pteval;/* The only possible pmd mapping has been handled on last iteration */ if (pvmw->pmd && !pvmw->pte)
mm/page_vma_mapped.c bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) · line 318
Quoted 9 lines
mm/page_vma_mapped.c +5
-1@@ -310,7 +311,11 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) goto restart; } pvmw->pte++; - }while (pte_none (ptep_get (pvmw->pte))); +if (!pvmw->ptl) + pteval =ptep_get_lockless (pvmw->pte); +else + pteval =ptep_get (pvmw->pte); + }while (pte_none (pteval));
Looks fine to me. I will try and add it to the next version.
Quoted 8 lines
mm/page_vma_mapped.c +0
-0if (!pvmw->ptl) {spin_lock (ptl);Signature · 4 lines
-- Cheers, David
Thanks!