Skip to contentSkip to conversation

riscv: mm: clean up and optimize some page table get-and-clear functions

PATCH Applied Paul Walmsley 2 participants 5 messages Nov 20, 2025 RISC-V

lore
Share & tools
lore

Paul Walmsley

author maintainer
#0 lore raw

cover riscv: mm: clean up and optimize some page table get-and-clear functions

This series implements some minor cleanups on the RISC-V page table get-and-clear functions. We continue the work from commit 546e42c8c6d94 ("riscv: Use an atomic xchg in pudp_huge_get_and_clear()"). The two themes are:

- avoid atomic operations on non-SMP kernels

- use xchg() rather than atomic_long_xchg() on variables that aren't atomic_long_t

Tested on QEMU 10.0.6 on RV32 and RV64, SMP and !SMP, running kselftests.

- Paul

Paul Walmsley (3):
riscv: mm: pmdp_huge_get_and_clear(): avoid atomic ops when
!CONFIG_SMP
riscv: mm: ptep_get_and_clear(): avoid atomic ops when !CONFIG_SMP
riscv: mm: use xchg() on non-atomic_long_t variables, not
atomic_long_xchg()

 arch/riscv/include/asm/pgtable.h | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

base-commit: dcb6fa37fd7bc9c3d2b066329b0d27dedf8becaa

Signature · 7 lines
2.48.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author maintainer
#1 lore raw

PATCH 1/3 riscv: mm: pmdp_huge_get_and_clear(): avoid atomic ops when !CONFIG_SMP

When !CONFIG_SMP, there's no need for atomic operations in pmdp_huge_get_and_clear(), so, similar to what x86 does, let's not use atomics in this case. See also commit 546e42c8c6d94 ("riscv: Use an atomic xchg in pudp_huge_get_and_clear()").

Cc Alexandre Ghiti <[email protected]>
---
 arch/riscv/include/asm/pgtable.h | 6 ++++++
 1 file changed, 6 insertions(+)
arch/riscv/include/asm/pgtable.h +6 -0
index 5a08eb5fe99f..64a2197e6d97 100644
@@ -861,7 +861,13 @@ static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma,
 static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm,
 					unsigned long address, pmd_t *pmdp)
 {
+#ifdef CONFIG_SMP
 	pmd_t pmd = __pmd(atomic_long_xchg((atomic_long_t *)pmdp, 0));
+#else
+	pmd_t pmd = *pmdp;
+
+	pmd_clear(pmdp);
+#endif
 
 	page_table_check_pmd_clear(mm, pmd);
 
Signature · 7 lines
2.48.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author maintainer
#2 lore raw

PATCH 2/3 riscv: mm: ptep_get_and_clear(): avoid atomic ops when !CONFIG_SMP

In reply to Paul Walmsley

When !CONFIG_SMP, there's no need for atomic operations in ptep_get_and_clear(), so, similar to x86, let's not use atomics in this case.

Cc Alexandre Ghiti <[email protected]>
---
 arch/riscv/include/asm/pgtable.h | 6 ++++++
 1 file changed, 6 insertions(+)
arch/riscv/include/asm/pgtable.h +6 -0
index 64a2197e6d97..e1610d031199 100644
@@ -590,7 +590,13 @@ extern int ptep_test_and_clear_young(struct vm_area_struct *vma, unsigned long a
 static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
 				       unsigned long address, pte_t *ptep)
 {
+#ifdef CONFIG_SMP
 	pte_t pte = __pte(atomic_long_xchg((atomic_long_t *)ptep, 0));
+#else
+	pte_t pte = *ptep;
+
+	set_pte(ptep, __pte(0));
+#endif
 
 	page_table_check_pte_clear(mm, pte);
 
Signature · 7 lines
2.48.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author maintainer
#3 lore raw

PATCH 3/3 riscv: mm: use xchg() on non-atomic_long_t variables, not atomic_long_xchg()

In reply to Paul Walmsley

Let's not call atomic_long_xchg() on something that's not an atomic_long_t, and just use xchg() instead. Continues the cleanup from commit 546e42c8c6d94 ("riscv: Use an atomic xchg in pudp_huge_get_and_clear()"),

Cc Alexandre Ghiti <[email protected]>
---
 arch/riscv/include/asm/pgtable.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/riscv/include/asm/pgtable.h +2 -2
index e1610d031199..a1ffa502739b 100644
@@ -591,7 +591,7 @@ static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
 				       unsigned long address, pte_t *ptep)
 {
 #ifdef CONFIG_SMP
-	pte_t pte = __pte(atomic_long_xchg((atomic_long_t *)ptep, 0));
+	pte_t pte = __pte(xchg(&ptep->pte, 0));
 #else
 	pte_t pte = *ptep;
 
@@ -868,7 +868,7 @@ static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm,
 					unsigned long address, pmd_t *pmdp)
 {
 #ifdef CONFIG_SMP
-	pmd_t pmd = __pmd(atomic_long_xchg((atomic_long_t *)pmdp, 0));
+	pmd_t pmd = __pmd(xchg(&pmdp->pmd, 0));
 #else
 	pmd_t pmd = *pmdp;
 
Signature · 7 lines
2.48.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#4 lore raw
In reply to Paul Walmsley
This series was applied to riscv/linux.git (for-next) by Paul Walmsley <[email protected]>:

Hello:

This series was applied to riscv/linux.git (for-next) by Paul Walmsley <[email protected]>:

Quoted 8 lines · On Wed, 19 Nov 2025 21:18:27 -0700 you wrote:

This series implements some minor cleanups on the RISC-V page table get-and-clear functions. We continue the work from commit 546e42c8c6d94 ("riscv: Use an atomic xchg in pudp_huge_get_and_clear()"). The two themes are:

- avoid atomic operations on non-SMP kernels

[...]

Here is the summary with links:
- [1/3] riscv: mm: pmdp_huge_get_and_clear(): avoid atomic ops when !CONFIG_SMP

    https://git.kernel.org/riscv/c/44d98d7575d0
  - [2/3] riscv: mm: ptep_get_and_clear(): avoid atomic ops when !CONFIG_SMP
    https://git.kernel.org/riscv/c/ad6a7d4bcee7
  - [3/3] riscv: mm: use xchg() on non-atomic_long_t variables, not atomic_long_xchg()
    https://git.kernel.org/riscv/c/36313cea1273

You are awesome, thank you!

Signature · 9 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv