Skip to contentSkip to conversation

mm/page_vma_mapped_walk: Use ptep_get_lockless() for lockless access

PATCH Superseded Alexander Gordeev 3 participants 8 messages May 4 – May 8

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Alexander Gordeev

author maintainer
#0 lore raw

PATCH 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
index a4d52fdb3056..6559e17f11c2 100644
@@ -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
#1 lore raw
Quoted 22 lines · Alexander Gordeev

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 +1 -1
index a4d52fdb3056..6559e17f11c2 100644
@@ -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 -0
 		if (!pvmw->ptl) {
 			spin_lock(ptl);
Signature · 1 line
2.51.0
Signature · 2 lines
Wei Yang
Help you, Help me
author maintainer
#2 lore raw
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 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 -0
 		if (!pvmw->ptl) {
 			spin_lock(ptl);
Signature · 1 line
2.51.0
Signature · 1 line
Wei Yang

Thanks!

#3 lore raw
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 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.

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 -0
 		if (!pvmw->ptl) {
 			spin_lock(ptl);
Signature · 1 line
2.51.0
Signature · 1 line
Wei Yang

Thanks!

Signature · 2 lines
Wei Yang
Help you, Help me
author maintainer
#4 lore raw
Quoted 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 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 6 lines

So the rule here is:

* ptep_get_lockless() could be used for locked and not locked
* ptep_get() only used when locked

Right?

Yes, this is my assumption.

Quoted 15 lines
Quoted 11 lines
Quoted 9 lines
Quoted 6 lines
mm/page_vma_mapped.c +0 -0
 		if (!pvmw->ptl) {
 			spin_lock(ptl);
Signature · 1 line
2.51.0
Signature · 1 line
Wei Yang

Thanks!

Signature · 2 lines
Wei Yang
Help you, Help me
#5 lore raw
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 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

So the rule here is:

* ptep_get_lockless() could be used for locked and not locked
* ptep_get() only used when locked

Right?

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 -0
 		if (!pvmw->ptl) {
 			spin_lock(ptl);
Signature · 1 line
2.51.0
Signature · 1 line
Wei Yang

Thanks!

Signature · 2 lines
Wei Yang
Help you, Help me
Signature · 2 lines
Wei Yang
Help you, Help me
#6 lore raw
In reply to Alexander Gordeev
Quoted 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 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 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 locked

Right?

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
index a4d52fdb3056..36d97661a4e5 100644
@@ -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
author maintainer
#7 lore raw
Quoted 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 locked

Right?

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
index a4d52fdb3056..36d97661a4e5 100644
@@ -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 -0
                if (!pvmw->ptl) {
                        spin_lock(ptl);
 
Signature · 4 lines
-- 
Cheers,

David

Thanks!