Skip to contentSkip to conversation

remove tlb_remove_page_ptdesc()

PATCH Applied Qi Zheng 5 participants 22 messages Feb 25, 2025 – Mar 4, 2025

Subsystems (16)
Memory management x86 MIPS RISC-V +12 more subsystems
lore
Share & tools
lore

Qi Zheng

author
#0 lore raw

v2 cover remove tlb_remove_page_ptdesc()

Changes in v2:
- add [PATCH v2 2/6] (Peter Zijlstra)
- remove [PATCH 4/5] and add [PATCH v2 5/6]
- rebase onto the next-20250224

Hi all,

As suggested by Peter Zijlstra below [1], this series aims to remove tlb_remove_page_ptdesc().

: Fundamentally tlb_remove_page() is about removing *pages* as from a PTE, : there should not be a page-table anywhere near here *ever*. :
: Yes, some architectures use tlb_remove_page() for page-tables too, but : that is more or less an implementation detail that can be fixed.

After this series, all architectures use tlb_remove_table() or tlb_remove_ptdesc() to remove the page table pages. In the future, once all architectures using tlb_remove_table() have also converted to using struct ptdesc (eg. powerpc), it may be possible to use only tlb_remove_ptdesc().

This series is based on next-20250224.

Comments and suggestions are welcome!

Thanks,
Qi

[1]. https://lore.kernel.org/linux-mm/[email protected]/

Qi Zheng (6):
mm: pgtable: make generic tlb_remove_table() use struct ptdesc
mm: pgtable: change pt parameter of tlb_remove_ptdesc() to struct
ptdesc *
mm: pgtable: convert some architectures to use tlb_remove_ptdesc()
riscv: pgtable: unconditionally use tlb_remove_ptdesc()
x86: pgtable: convert to use tlb_remove_ptdesc()
mm: pgtable: remove tlb_remove_page_ptdesc()

 arch/csky/include/asm/pgalloc.h      |  3 +--
 arch/hexagon/include/asm/pgalloc.h   |  3 +--
 arch/loongarch/include/asm/pgalloc.h |  3 +--
 arch/m68k/include/asm/sun3_pgalloc.h |  3 +--
 arch/mips/include/asm/pgalloc.h      |  3 +--
 arch/nios2/include/asm/pgalloc.h     |  9 ++++-----
 arch/openrisc/include/asm/pgalloc.h  |  3 +--
 arch/riscv/include/asm/pgalloc.h     | 26 ++++----------------------
 arch/sh/include/asm/pgalloc.h        |  3 +--
 arch/um/include/asm/pgalloc.h        |  9 +++------
 arch/x86/mm/pgtable.c                |  8 ++++----
 include/asm-generic/tlb.h            | 14 ++++----------
 12 files changed, 26 insertions(+), 61 deletions(-)
Signature · 1 line
2.20.1
author
#1 lore raw

PATCH v2 1/6 mm: pgtable: make generic tlb_remove_table() use struct ptdesc

Now only arm will call tlb_remove_ptdesc()/tlb_remove_table() when CONFIG_MMU_GATHER_TABLE_FREE is disabled. In this case, the type of the table parameter is actually struct ptdesc * instead of struct page *.

Since struct ptdesc still overlaps with struct page and has not been separated from it, forcing the table parameter to struct page * will not cause any problems at this time. But this is definitely incorrect and needs to be fixed. So just like the generic __tlb_remove_table(), let generic tlb_remove_table() use struct ptdesc by default when CONFIG_MMU_GATHER_TABLE_FREE is disabled.

---
 include/asm-generic/tlb.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
include/asm-generic/tlb.h +3 -3
index b35b36fa7aabf..54f579750c8e5 100644
@@ -232,10 +232,10 @@ static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page);
  */
 static inline void tlb_remove_table(struct mmu_gather *tlb, void *table)
 {
-	struct page *page = (struct page *)table;
+	struct ptdesc *ptdesc = (struct ptdesc *)table;
 
-	pagetable_dtor(page_ptdesc(page));
-	tlb_remove_page(tlb, page);
+	pagetable_dtor(ptdesc);
+	tlb_remove_page(tlb, ptdesc_page(ptdesc));
 }
 #endif /* CONFIG_MMU_GATHER_TABLE_FREE */
 
Signature · 1 line
2.20.1
Reviewed-by on v2 1/6
#2 lore raw

Re: [PATCH v2 1/6] mm: pgtable: make generic tlb_remove_table() use struct ptdesc

Quoted 4 lines · Qi Zheng

Now only arm will call tlb_remove_ptdesc()/tlb_remove_table() when CONFIG_MMU_GATHER_TABLE_FREE is disabled. In this case, the type of the table parameter is actually struct ptdesc * instead of struct page *.

Quoted 8 lines

Since struct ptdesc still overlaps with struct page and has not been separated from it, forcing the table parameter to struct page * will not cause any problems at this time. But this is definitely incorrect and needs to be fixed. So just like the generic __tlb_remove_table(), let generic tlb_remove_table() use struct ptdesc by default when CONFIG_MMU_GATHER_TABLE_FREE is disabled.

author
#3 lore raw

PATCH v2 2/6 mm: pgtable: change pt parameter of tlb_remove_ptdesc() to struct ptdesc *

In reply to Qi Zheng

All callers of tlb_remove_ptdesc() pass it a pointer of struct ptdesc, so let's change the pt parameter from void * to struct ptdesc * to perform a type safety check.

---
 include/asm-generic/tlb.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/asm-generic/tlb.h +1 -1
index 54f579750c8e5..18bf499ef8801 100644
@@ -506,7 +506,7 @@ static inline void tlb_remove_page(struct mmu_gather *tlb, struct page *page)
 	return tlb_remove_page_size(tlb, page, PAGE_SIZE);
 }
 
-static inline void tlb_remove_ptdesc(struct mmu_gather *tlb, void *pt)
+static inline void tlb_remove_ptdesc(struct mmu_gather *tlb, struct ptdesc *pt)
 {
 	tlb_remove_table(tlb, pt);
 }
Signature · 1 line
2.20.1
Reviewed-by on v2 2/6
#4 lore raw

Re: [PATCH v2 2/6] mm: pgtable: change pt parameter of tlb_remove_ptdesc() to struct ptdesc *

Quoted 6 lines · Qi Zheng

All callers of tlb_remove_ptdesc() pass it a pointer of struct ptdesc, so let's change the pt parameter from void * to struct ptdesc * to perform a type safety check.

author
#5 lore raw

PATCH v2 3/6 mm: pgtable: convert some architectures to use tlb_remove_ptdesc()

In reply to Qi Zheng

Now, the nine architectures of csky, hexagon, loongarch, m68k, mips, nios2, openrisc, sh and um do not select CONFIG_MMU_GATHER_RCU_TABLE_FREE, and just call pagetable_dtor() + tlb_remove_page_ptdesc() (the wrapper of tlb_remove_page()). This is the same as the implementation of tlb_remove_{ptdesc|table}() under !CONFIG_MMU_GATHER_TABLE_FREE, so convert these architectures to use tlb_remove_ptdesc().

The ultimate goal is to make the architecture only use tlb_remove_ptdesc() or tlb_remove_table() for page table pages.

---
 arch/csky/include/asm/pgalloc.h      | 3 +--
 arch/hexagon/include/asm/pgalloc.h   | 3 +--
 arch/loongarch/include/asm/pgalloc.h | 3 +--
 arch/m68k/include/asm/sun3_pgalloc.h | 3 +--
 arch/mips/include/asm/pgalloc.h      | 3 +--
 arch/nios2/include/asm/pgalloc.h     | 9 ++++-----
 arch/openrisc/include/asm/pgalloc.h  | 3 +--
 arch/sh/include/asm/pgalloc.h        | 3 +--
 arch/um/include/asm/pgalloc.h        | 9 +++------
 9 files changed, 14 insertions(+), 25 deletions(-)
arch/csky/include/asm/pgalloc.h +1 -2
index bf8400c28b5a3..9d2b50265a8d8 100644
@@ -63,8 +63,7 @@ static inline pgd_t *pgd_alloc(struct mm_struct *mm)
 
 #define __pte_free_tlb(tlb, pte, address)		\
 do {							\
-	pagetable_dtor(page_ptdesc(pte));		\
-	tlb_remove_page_ptdesc(tlb, page_ptdesc(pte));	\
+	tlb_remove_ptdesc((tlb), page_ptdesc(pte));	\
 } while (0)
 
 extern void pagetable_init(void);
arch/hexagon/include/asm/pgalloc.h +1 -2
index 1ee5f5f157ca7..3d35d2bc42534 100644
@@ -89,8 +89,7 @@ static inline void pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmd,
 
 #define __pte_free_tlb(tlb, pte, addr)				\
 do {								\
-	pagetable_dtor((page_ptdesc(pte)));			\
-	tlb_remove_page_ptdesc((tlb), (page_ptdesc(pte)));	\
+	tlb_remove_ptdesc((tlb), page_ptdesc(pte));		\
 } while (0)
 
 #endif
arch/loongarch/include/asm/pgalloc.h +1 -2
index 7211dff8c969e..ac026146e7e95 100644
@@ -57,8 +57,7 @@ static inline pte_t *pte_alloc_one_kernel(struct mm_struct *mm)
 
 #define __pte_free_tlb(tlb, pte, address)			\
 do {								\
-	pagetable_dtor(page_ptdesc(pte));			\
-	tlb_remove_page_ptdesc((tlb), page_ptdesc(pte));	\
+	tlb_remove_ptdesc((tlb), page_ptdesc(pte));		\
 } while (0)
 
 #ifndef __PAGETABLE_PMD_FOLDED
arch/m68k/include/asm/sun3_pgalloc.h +1 -2
index 80afc3a187249..ddc24812f1832 100644
@@ -19,8 +19,7 @@ extern const char bad_pmd_string[];
 
 #define __pte_free_tlb(tlb, pte, addr)				\
 do {								\
-	pagetable_dtor(page_ptdesc(pte));			\
-	tlb_remove_page_ptdesc((tlb), page_ptdesc(pte));	\
+	tlb_remove_ptdesc((tlb), page_ptdesc(pte));		\
 } while (0)
 
 static inline void pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmd, pte_t *pte)
arch/mips/include/asm/pgalloc.h +1 -2
index 26c7a6ede983c..7e73d2f913dd4 100644
@@ -50,8 +50,7 @@ extern pgd_t *pgd_alloc(struct mm_struct *mm);
 
 #define __pte_free_tlb(tlb, pte, address)			\
 do {								\
-	pagetable_dtor(page_ptdesc(pte));			\
-	tlb_remove_page_ptdesc((tlb), page_ptdesc(pte));	\
+	tlb_remove_ptdesc((tlb), page_ptdesc(pte));		\
 } while (0)
 
 #ifndef __PAGETABLE_PMD_FOLDED
arch/nios2/include/asm/pgalloc.h +4 -5
index 12a536b7bfbd4..4b4a1766e2cc7 100644
@@ -28,10 +28,9 @@ static inline void pmd_populate(struct mm_struct *mm, pmd_t *pmd,
 
 extern pgd_t *pgd_alloc(struct mm_struct *mm);
 
-#define __pte_free_tlb(tlb, pte, addr)					\
-	do {								\
-		pagetable_dtor(page_ptdesc(pte));			\
-		tlb_remove_page_ptdesc((tlb), (page_ptdesc(pte)));	\
-	} while (0)
+#define __pte_free_tlb(tlb, pte, addr)				\
+do {								\
+	tlb_remove_ptdesc((tlb), page_ptdesc(pte));		\
+} while (0)
 
 #endif /* _ASM_NIOS2_PGALLOC_H */
arch/openrisc/include/asm/pgalloc.h +1 -2
index 3372f4e6ab4b5..2964e26980a18 100644
@@ -66,8 +66,7 @@ extern pte_t *pte_alloc_one_kernel(struct mm_struct *mm);
 
 #define __pte_free_tlb(tlb, pte, addr)				\
 do {								\
-	pagetable_dtor(page_ptdesc(pte));			\
-	tlb_remove_page_ptdesc((tlb), (page_ptdesc(pte)));	\
+	tlb_remove_ptdesc((tlb), page_ptdesc(pte));		\
 } while (0)
 
 #endif
arch/sh/include/asm/pgalloc.h +1 -2
index 96d938fdf2244..c376d4b708fda 100644
@@ -34,8 +34,7 @@ static inline void pmd_populate(struct mm_struct *mm, pmd_t *pmd,
 
 #define __pte_free_tlb(tlb, pte, addr)				\
 do {								\
-	pagetable_dtor(page_ptdesc(pte));			\
-	tlb_remove_page_ptdesc((tlb), (page_ptdesc(pte)));	\
+	tlb_remove_ptdesc((tlb), page_ptdesc(pte));		\
 } while (0)
 
 #endif /* __ASM_SH_PGALLOC_H */
arch/um/include/asm/pgalloc.h +3 -6
index f0af23c3aeb2b..a70151cfd11c9 100644
@@ -27,24 +27,21 @@ extern pgd_t *pgd_alloc(struct mm_struct *);
 
 #define __pte_free_tlb(tlb, pte, address)			\
 do {								\
-	pagetable_dtor(page_ptdesc(pte));			\
-	tlb_remove_page_ptdesc((tlb), (page_ptdesc(pte)));	\
+	tlb_remove_ptdesc((tlb), page_ptdesc(pte));		\
 } while (0)
 
 #if CONFIG_PGTABLE_LEVELS > 2
 
 #define __pmd_free_tlb(tlb, pmd, address)			\
 do {								\
-	pagetable_dtor(virt_to_ptdesc(pmd));			\
-	tlb_remove_page_ptdesc((tlb), virt_to_ptdesc(pmd));	\
+	tlb_remove_ptdesc((tlb), virt_to_ptdesc(pmd));		\
 } while (0)
 
 #if CONFIG_PGTABLE_LEVELS > 3
 
 #define __pud_free_tlb(tlb, pud, address)			\
 do {								\
-	pagetable_dtor(virt_to_ptdesc(pud));		\
-	tlb_remove_page_ptdesc((tlb), virt_to_ptdesc(pud));	\
+	tlb_remove_ptdesc((tlb), virt_to_ptdesc(pud));		\
 } while (0)
 
 #endif
Signature · 1 line
2.20.1
Reviewed-by on v2 3/6
#6 lore raw

Re: [PATCH v2 3/6] mm: pgtable: convert some architectures to use tlb_remove_ptdesc()

Quoted 7 lines · Qi Zheng

Now, the nine architectures of csky, hexagon, loongarch, m68k, mips, nios2, openrisc, sh and um do not select CONFIG_MMU_GATHER_RCU_TABLE_FREE, and just call pagetable_dtor() + tlb_remove_page_ptdesc() (the wrapper of tlb_remove_page()). This is the same as the implementation of tlb_remove_{ptdesc|table}() under !CONFIG_MMU_GATHER_TABLE_FREE, so convert these architectures to use tlb_remove_ptdesc().

Quoted 5 lines

The ultimate goal is to make the architecture only use tlb_remove_ptdesc() or tlb_remove_table() for page table pages.

maintainer Acked-by on v2 3/6
#7 lore raw

Re: [PATCH v2 3/6] mm: pgtable: convert some architectures to use tlb_remove_ptdesc()

In reply to Qi Zheng

Hi Qi,

Quoted 7 lines · Qi Zheng

Now, the nine architectures of csky, hexagon, loongarch, m68k, mips, nios2, openrisc, sh and um do not select CONFIG_MMU_GATHER_RCU_TABLE_FREE, and just call pagetable_dtor() + tlb_remove_page_ptdesc() (the wrapper of tlb_remove_page()). This is the same as the implementation of tlb_remove_{ptdesc|table}() under !CONFIG_MMU_GATHER_TABLE_FREE, so convert these architectures to use tlb_remove_ptdesc().

Quoted 5 lines

The ultimate goal is to make the architecture only use tlb_remove_ptdesc() or tlb_remove_table() for page table pages.

Thanks for your patch!

Quoted 1 line
 arch/m68k/include/asm/sun3_pgalloc.h | 3 +--

arch/m68k/include/asm/sun3_pgalloc.h extern const char bad_pmd_string[]; · line 24

Quoted 11 lines
arch/m68k/include/asm/sun3_pgalloc.h +1 -2
@@ -19,8 +19,7 @@ extern const char bad_pmd_string[];
 
 #define __pte_free_tlb(tlb, pte, addr)                         \
 do {                                                           \
-       pagetable_dtor(page_ptdesc(pte));                       \
-       tlb_remove_page_ptdesc((tlb), page_ptdesc(pte));        \
+       tlb_remove_ptdesc((tlb), page_ptdesc(pte));             \
 } while (0)
 

With only a single statement remaining, you can remove the do { ... } while construct, too.

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
author
#8 lore raw

Re: [PATCH v2 3/6] mm: pgtable: convert some architectures to use tlb_remove_ptdesc()

Quoted 13 lines · Geert Uytterhoeven

Hi Qi,

Quoted 10 lines · Qi Zheng

Now, the nine architectures of csky, hexagon, loongarch, m68k, mips, nios2, openrisc, sh and um do not select CONFIG_MMU_GATHER_RCU_TABLE_FREE, and just call pagetable_dtor() + tlb_remove_page_ptdesc() (the wrapper of tlb_remove_page()). This is the same as the implementation of tlb_remove_{ptdesc|table}() under !CONFIG_MMU_GATHER_TABLE_FREE, so convert these architectures to use tlb_remove_ptdesc().

The ultimate goal is to make the architecture only use tlb_remove_ptdesc() or tlb_remove_table() for page table pages.

Quoted 8 lines
Quoted 2 lines

Thanks for your patch!

Quoted 1 line
  arch/m68k/include/asm/sun3_pgalloc.h | 3 +--

Thanks for your review.

Quoted 5 lines
Quoted 5 lines
arch/m68k/include/asm/sun3_pgalloc.h +0 -0
@@ -19,8 +19,7 @@ extern const char bad_pmd_string[];
 
Quoted 10 lines

arch/m68k/include/asm/sun3_pgalloc.h extern const char bad_pmd_string[]; · line 24

Quoted 7 lines
arch/m68k/include/asm/sun3_pgalloc.h +1 -2
  #define __pte_free_tlb(tlb, pte, addr)                         \
  do {                                                           \
-       pagetable_dtor(page_ptdesc(pte));                       \
-       tlb_remove_page_ptdesc((tlb), page_ptdesc(pte));        \
+       tlb_remove_ptdesc((tlb), page_ptdesc(pte));             \
  } while (0)
 

With only a single statement remaining, you can remove the do { ... } while construct, too.

Ah, right. Will do in the next version.

Thanks!

Quoted 5 lines

Gr{oetje,eeting}s,

Geert

author
#9 lore raw

PATCH v2 3/6 mm: pgtable: convert some architectures to use tlb_remove_ptdesc()

In reply to Qi Zheng

Now, the nine architectures of csky, hexagon, loongarch, m68k, mips, nios2, openrisc, sh and um do not select CONFIG_MMU_GATHER_RCU_TABLE_FREE, and just call pagetable_dtor() + tlb_remove_page_ptdesc() (the wrapper of tlb_remove_page()). This is the same as the implementation of tlb_remove_{ptdesc|table}() under !CONFIG_MMU_GATHER_TABLE_FREE, so convert these architectures to use tlb_remove_ptdesc().

The ultimate goal is to make the architecture only use tlb_remove_ptdesc() or tlb_remove_table() for page table pages.

---
Changes in v2 update:
- remove the do { ... } while construct. (Geert Uytterhoeven)
- collect the Reviewed-by and Acked-by.

 arch/csky/include/asm/pgalloc.h      |  7 ++-----
 arch/hexagon/include/asm/pgalloc.h   |  7 ++-----
 arch/loongarch/include/asm/pgalloc.h |  7 ++-----
 arch/m68k/include/asm/sun3_pgalloc.h |  7 ++-----
 arch/mips/include/asm/pgalloc.h      |  7 ++-----
 arch/nios2/include/asm/pgalloc.h     |  7 ++-----
 arch/openrisc/include/asm/pgalloc.h  |  7 ++-----
 arch/sh/include/asm/pgalloc.h        |  7 ++-----
 arch/um/include/asm/pgalloc.h        | 21 ++++++---------------
 9 files changed, 22 insertions(+), 55 deletions(-)
arch/csky/include/asm/pgalloc.h +2 -5
index bf8400c28b5a3..11055c5749686 100644
@@ -61,11 +61,8 @@ static inline pgd_t *pgd_alloc(struct mm_struct *mm)
 	return ret;
 }
 
-#define __pte_free_tlb(tlb, pte, address)		\
-do {							\
-	pagetable_dtor(page_ptdesc(pte));		\
-	tlb_remove_page_ptdesc(tlb, page_ptdesc(pte));	\
-} while (0)
+#define __pte_free_tlb(tlb, pte, address)	\
+	tlb_remove_ptdesc((tlb), page_ptdesc(pte))
 
 extern void pagetable_init(void);
 extern void mmu_init(unsigned long min_pfn, unsigned long max_pfn);
arch/hexagon/include/asm/pgalloc.h +2 -5
index 1ee5f5f157ca7..937a11ef4c33c 100644
@@ -87,10 +87,7 @@ static inline void pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmd,
 		max_kernel_seg = pmdindex;
 }
 
-#define __pte_free_tlb(tlb, pte, addr)				\
-do {								\
-	pagetable_dtor((page_ptdesc(pte)));			\
-	tlb_remove_page_ptdesc((tlb), (page_ptdesc(pte)));	\
-} while (0)
+#define __pte_free_tlb(tlb, pte, addr)	\
+	tlb_remove_ptdesc((tlb), page_ptdesc(pte))
 
 #endif
arch/loongarch/include/asm/pgalloc.h +2 -5
index 7211dff8c969e..73629c1b8328e 100644
@@ -55,11 +55,8 @@ static inline pte_t *pte_alloc_one_kernel(struct mm_struct *mm)
 	return pte;
 }
 
-#define __pte_free_tlb(tlb, pte, address)			\
-do {								\
-	pagetable_dtor(page_ptdesc(pte));			\
-	tlb_remove_page_ptdesc((tlb), page_ptdesc(pte));	\
-} while (0)
+#define __pte_free_tlb(tlb, pte, address)	\
+	tlb_remove_ptdesc((tlb), page_ptdesc(pte));
 
 #ifndef __PAGETABLE_PMD_FOLDED
 
arch/m68k/include/asm/sun3_pgalloc.h +2 -5
index 80afc3a187249..1e21c758b774e 100644
@@ -17,11 +17,8 @@
 
 extern const char bad_pmd_string[];
 
-#define __pte_free_tlb(tlb, pte, addr)				\
-do {								\
-	pagetable_dtor(page_ptdesc(pte));			\
-	tlb_remove_page_ptdesc((tlb), page_ptdesc(pte));	\
-} while (0)
+#define __pte_free_tlb(tlb, pte, addr)	\
+	tlb_remove_ptdesc((tlb), page_ptdesc(pte))
 
 static inline void pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmd, pte_t *pte)
 {
arch/mips/include/asm/pgalloc.h +2 -5
index 26c7a6ede983c..bbca420c96d3c 100644
@@ -48,11 +48,8 @@ static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd)
 extern void pgd_init(void *addr);
 extern pgd_t *pgd_alloc(struct mm_struct *mm);
 
-#define __pte_free_tlb(tlb, pte, address)			\
-do {								\
-	pagetable_dtor(page_ptdesc(pte));			\
-	tlb_remove_page_ptdesc((tlb), page_ptdesc(pte));	\
-} while (0)
+#define __pte_free_tlb(tlb, pte, address)	\
+	tlb_remove_ptdesc((tlb), page_ptdesc(pte))
 
 #ifndef __PAGETABLE_PMD_FOLDED
 
arch/nios2/include/asm/pgalloc.h +2 -5
index 12a536b7bfbd4..db122b093a8be 100644
@@ -28,10 +28,7 @@ static inline void pmd_populate(struct mm_struct *mm, pmd_t *pmd,
 
 extern pgd_t *pgd_alloc(struct mm_struct *mm);
 
-#define __pte_free_tlb(tlb, pte, addr)					\
-	do {								\
-		pagetable_dtor(page_ptdesc(pte));			\
-		tlb_remove_page_ptdesc((tlb), (page_ptdesc(pte)));	\
-	} while (0)
+#define __pte_free_tlb(tlb, pte, addr)	\
+	tlb_remove_ptdesc((tlb), page_ptdesc(pte))
 
 #endif /* _ASM_NIOS2_PGALLOC_H */
arch/openrisc/include/asm/pgalloc.h +2 -5
index 3372f4e6ab4b5..3f110931d8f6e 100644
@@ -64,10 +64,7 @@ extern inline pgd_t *pgd_alloc(struct mm_struct *mm)
 
 extern pte_t *pte_alloc_one_kernel(struct mm_struct *mm);
 
-#define __pte_free_tlb(tlb, pte, addr)				\
-do {								\
-	pagetable_dtor(page_ptdesc(pte));			\
-	tlb_remove_page_ptdesc((tlb), (page_ptdesc(pte)));	\
-} while (0)
+#define __pte_free_tlb(tlb, pte, addr)	\
+	tlb_remove_ptdesc((tlb), page_ptdesc(pte))
 
 #endif
arch/sh/include/asm/pgalloc.h +2 -5
index 96d938fdf2244..6fe7123d38fa9 100644
@@ -32,10 +32,7 @@ static inline void pmd_populate(struct mm_struct *mm, pmd_t *pmd,
 	set_pmd(pmd, __pmd((unsigned long)page_address(pte)));
 }
 
-#define __pte_free_tlb(tlb, pte, addr)				\
-do {								\
-	pagetable_dtor(page_ptdesc(pte));			\
-	tlb_remove_page_ptdesc((tlb), (page_ptdesc(pte)));	\
-} while (0)
+#define __pte_free_tlb(tlb, pte, addr)	\
+	tlb_remove_ptdesc((tlb), page_ptdesc(pte))
 
 #endif /* __ASM_SH_PGALLOC_H */
arch/um/include/asm/pgalloc.h +6 -15
index f0af23c3aeb2b..826ec44b58cdb 100644
@@ -25,27 +25,18 @@
  */
 extern pgd_t *pgd_alloc(struct mm_struct *);
 
-#define __pte_free_tlb(tlb, pte, address)			\
-do {								\
-	pagetable_dtor(page_ptdesc(pte));			\
-	tlb_remove_page_ptdesc((tlb), (page_ptdesc(pte)));	\
-} while (0)
+#define __pte_free_tlb(tlb, pte, address)	\
+	tlb_remove_ptdesc((tlb), page_ptdesc(pte))
 
 #if CONFIG_PGTABLE_LEVELS > 2
 
-#define __pmd_free_tlb(tlb, pmd, address)			\
-do {								\
-	pagetable_dtor(virt_to_ptdesc(pmd));			\
-	tlb_remove_page_ptdesc((tlb), virt_to_ptdesc(pmd));	\
-} while (0)
+#define __pmd_free_tlb(tlb, pmd, address)	\
+	tlb_remove_ptdesc((tlb), virt_to_ptdesc(pmd))
 
 #if CONFIG_PGTABLE_LEVELS > 3
 
-#define __pud_free_tlb(tlb, pud, address)			\
-do {								\
-	pagetable_dtor(virt_to_ptdesc(pud));		\
-	tlb_remove_page_ptdesc((tlb), virt_to_ptdesc(pud));	\
-} while (0)
+#define __pud_free_tlb(tlb, pud, address)	\
+	tlb_remove_ptdesc((tlb), virt_to_ptdesc(pud))
 
 #endif
 #endif
Signature · 1 line
2.20.1
maintainer
#10 lore raw

Re: [PATCH v2 3/6 update] mm: pgtable: convert some architectures to use tlb_remove_ptdesc()

Quoted 7 lines · On Mon, 3 Mar 2025 15:26:03 +0800 Qi Zheng <[email protected]> wrote:

Now, the nine architectures of csky, hexagon, loongarch, m68k, mips, nios2, openrisc, sh and um do not select CONFIG_MMU_GATHER_RCU_TABLE_FREE, and just call pagetable_dtor() + tlb_remove_page_ptdesc() (the wrapper of tlb_remove_page()). This is the same as the implementation of tlb_remove_{ptdesc|table}() under !CONFIG_MMU_GATHER_TABLE_FREE, so convert these architectures to use tlb_remove_ptdesc().

checkpatch warns.

Do these things have to be macros? Switching to static inline fixes the unused-arg warning in a nice fashion.

I'll fix the trailing-semicolon issue locally.

WARNING: Argument 'address' is not used in function-like macro #51: FILE: arch/csky/include/asm/pgalloc.h:64:
+#define __pte_free_tlb(tlb, pte, address) \
+ tlb_remove_ptdesc((tlb), page_ptdesc(pte))

WARNING: Argument 'addr' is not used in function-like macro #66: FILE: arch/hexagon/include/asm/pgalloc.h:90:
+#define __pte_free_tlb(tlb, pte, addr) \
+ tlb_remove_ptdesc((tlb), page_ptdesc(pte))

WARNING: Argument 'address' is not used in function-like macro #80: FILE: arch/loongarch/include/asm/pgalloc.h:58:
+#define __pte_free_tlb(tlb, pte, address) \
+ tlb_remove_ptdesc((tlb), page_ptdesc(pte));

WARNING: macros should not use a trailing semicolon #80: FILE: arch/loongarch/include/asm/pgalloc.h:58:
+#define __pte_free_tlb(tlb, pte, address) \
+ tlb_remove_ptdesc((tlb), page_ptdesc(pte));

WARNING: Argument 'addr' is not used in function-like macro #95: FILE: arch/m68k/include/asm/sun3_pgalloc.h:20:
+#define __pte_free_tlb(tlb, pte, addr) \
+ tlb_remove_ptdesc((tlb), page_ptdesc(pte))

WARNING: Argument 'address' is not used in function-like macro #110: FILE: arch/mips/include/asm/pgalloc.h:51:
+#define __pte_free_tlb(tlb, pte, address) \
+ tlb_remove_ptdesc((tlb), page_ptdesc(pte))

WARNING: Argument 'addr' is not used in function-like macro #125: FILE: arch/nios2/include/asm/pgalloc.h:31:
+#define __pte_free_tlb(tlb, pte, addr) \
+ tlb_remove_ptdesc((tlb), page_ptdesc(pte))

WARNING: Argument 'addr' is not used in function-like macro #139: FILE: arch/openrisc/include/asm/pgalloc.h:67:
+#define __pte_free_tlb(tlb, pte, addr) \
+ tlb_remove_ptdesc((tlb), page_ptdesc(pte))

WARNING: Argument 'addr' is not used in function-like macro #153: FILE: arch/sh/include/asm/pgalloc.h:35:
+#define __pte_free_tlb(tlb, pte, addr) \
+ tlb_remove_ptdesc((tlb), page_ptdesc(pte))

WARNING: Argument 'address' is not used in function-like macro #167: FILE: arch/um/include/asm/pgalloc.h:28:
+#define __pte_free_tlb(tlb, pte, address) \
+ tlb_remove_ptdesc((tlb), page_ptdesc(pte))

WARNING: Argument 'address' is not used in function-like macro #176: FILE: arch/um/include/asm/pgalloc.h:33:
+#define __pmd_free_tlb(tlb, pmd, address) \
+ tlb_remove_ptdesc((tlb), virt_to_ptdesc(pmd))

WARNING: Argument 'address' is not used in function-like macro #185: FILE: arch/um/include/asm/pgalloc.h:38:
+#define __pud_free_tlb(tlb, pud, address) \
+ tlb_remove_ptdesc((tlb), virt_to_ptdesc(pud))

total: 0 errors, 12 warnings, 122 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.

./patches/mm-pgtable-convert-some-architectures-to-use-tlb_remove_ptdesc-v2.patch has style problems, please review.

NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.

author
#11 lore raw

Re: [PATCH v2 3/6 update] mm: pgtable: convert some architectures to use tlb_remove_ptdesc()

Quoted 10 lines · Andrew Morton
Quoted 7 lines · On Mon, 3 Mar 2025 15:26:03 +0800 Qi Zheng <[email protected]> wrote:

Now, the nine architectures of csky, hexagon, loongarch, m68k, mips, nios2, openrisc, sh and um do not select CONFIG_MMU_GATHER_RCU_TABLE_FREE, and just call pagetable_dtor() + tlb_remove_page_ptdesc() (the wrapper of tlb_remove_page()). This is the same as the implementation of tlb_remove_{ptdesc|table}() under !CONFIG_MMU_GATHER_TABLE_FREE, so convert these architectures to use tlb_remove_ptdesc().

Quoted 4 lines

checkpatch warns.

Do these things have to be macros? Switching to static inline fixes the unused-arg warning in a nice fashion.

This can be switched to static inline. In addition, I found that alpha, arc, microblaze, parisc, sparc32 and xtensa also have the unused-arg issue. Do I need to add a new patch to fix all of them, or just fix the newly added 11 warnings?

Quoted 2 lines

I'll fix the trailing-semicolon issue locally.

Thanks!

Quoted 71 lines

WARNING: Argument 'address' is not used in function-like macro #51: FILE: arch/csky/include/asm/pgalloc.h:64:

(quoted diff) +2 -0
+#define __pte_free_tlb(tlb, pte, address)	\
+	tlb_remove_ptdesc((tlb), page_ptdesc(pte))

WARNING: Argument 'addr' is not used in function-like macro #66: FILE: arch/hexagon/include/asm/pgalloc.h:90:

(quoted diff) +2 -0
+#define __pte_free_tlb(tlb, pte, addr)	\
+	tlb_remove_ptdesc((tlb), page_ptdesc(pte))

WARNING: Argument 'address' is not used in function-like macro #80: FILE: arch/loongarch/include/asm/pgalloc.h:58:

arch/loongarch/include/asm/pgalloc.h +2 -0
+#define __pte_free_tlb(tlb, pte, address)	\
+	tlb_remove_ptdesc((tlb), page_ptdesc(pte));

WARNING: macros should not use a trailing semicolon #80: FILE: arch/loongarch/include/asm/pgalloc.h:58:

arch/loongarch/include/asm/pgalloc.h +2 -0
+#define __pte_free_tlb(tlb, pte, address)	\
+	tlb_remove_ptdesc((tlb), page_ptdesc(pte));

WARNING: Argument 'addr' is not used in function-like macro #95: FILE: arch/m68k/include/asm/sun3_pgalloc.h:20:

(quoted diff) +2 -0
+#define __pte_free_tlb(tlb, pte, addr)	\
+	tlb_remove_ptdesc((tlb), page_ptdesc(pte))

WARNING: Argument 'address' is not used in function-like macro #110: FILE: arch/mips/include/asm/pgalloc.h:51:

(quoted diff) +2 -0
+#define __pte_free_tlb(tlb, pte, address)	\
+	tlb_remove_ptdesc((tlb), page_ptdesc(pte))

WARNING: Argument 'addr' is not used in function-like macro #125: FILE: arch/nios2/include/asm/pgalloc.h:31:

(quoted diff) +2 -0
+#define __pte_free_tlb(tlb, pte, addr)	\
+	tlb_remove_ptdesc((tlb), page_ptdesc(pte))

WARNING: Argument 'addr' is not used in function-like macro #139: FILE: arch/openrisc/include/asm/pgalloc.h:67:

(quoted diff) +2 -0
+#define __pte_free_tlb(tlb, pte, addr)	\
+	tlb_remove_ptdesc((tlb), page_ptdesc(pte))

WARNING: Argument 'addr' is not used in function-like macro #153: FILE: arch/sh/include/asm/pgalloc.h:35:

(quoted diff) +2 -0
+#define __pte_free_tlb(tlb, pte, addr)	\
+	tlb_remove_ptdesc((tlb), page_ptdesc(pte))

WARNING: Argument 'address' is not used in function-like macro #167: FILE: arch/um/include/asm/pgalloc.h:28:

(quoted diff) +2 -0
+#define __pte_free_tlb(tlb, pte, address)	\
+	tlb_remove_ptdesc((tlb), page_ptdesc(pte))

WARNING: Argument 'address' is not used in function-like macro #176: FILE: arch/um/include/asm/pgalloc.h:33:

arch/um/include/asm/pgalloc.h +2 -0
+#define __pmd_free_tlb(tlb, pmd, address)	\
+	tlb_remove_ptdesc((tlb), virt_to_ptdesc(pmd))

WARNING: Argument 'address' is not used in function-like macro #185: FILE: arch/um/include/asm/pgalloc.h:38:

arch/um/include/asm/pgalloc.h +2 -0
+#define __pud_free_tlb(tlb, pud, address)	\
+	tlb_remove_ptdesc((tlb), virt_to_ptdesc(pud))

total: 0 errors, 12 warnings, 122 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.

./patches/mm-pgtable-convert-some-architectures-to-use-tlb_remove_ptdesc-v2.patch has style problems, please review.

NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.

maintainer
#12 lore raw

Re: [PATCH v2 3/6 update] mm: pgtable: convert some architectures to use tlb_remove_ptdesc()

Quoted 15 lines · On Tue, 4 Mar 2025 10:31:07 +0800 Qi Zheng <[email protected]> wrote:
Quoted 12 lines · Andrew Morton
Quoted 7 lines · On Mon, 3 Mar 2025 15:26:03 +0800 Qi Zheng <[email protected]> wrote:

Now, the nine architectures of csky, hexagon, loongarch, m68k, mips, nios2, openrisc, sh and um do not select CONFIG_MMU_GATHER_RCU_TABLE_FREE, and just call pagetable_dtor() + tlb_remove_page_ptdesc() (the wrapper of tlb_remove_page()). This is the same as the implementation of tlb_remove_{ptdesc|table}() under !CONFIG_MMU_GATHER_TABLE_FREE, so convert these architectures to use tlb_remove_ptdesc().

checkpatch warns.

Quoted 7 lines
Quoted 2 lines

Do these things have to be macros? Switching to static inline fixes the unused-arg warning in a nice fashion.

This can be switched to static inline. In addition, I found that alpha, arc, microblaze, parisc, sparc32 and xtensa also have the unused-arg issue. Do I need to add a new patch to fix all of them, or just fix the newly added 11 warnings?

I guess leave things as they are now. Switching all these to C functions can be addressed at a later time?

author
#13 lore raw

Re: [PATCH v2 3/6 update] mm: pgtable: convert some architectures to use tlb_remove_ptdesc()

Quoted 20 lines · Andrew Morton
Quoted 18 lines · On Tue, 4 Mar 2025 10:31:07 +0800 Qi Zheng <[email protected]> wrote:
Quoted 14 lines · Andrew Morton
Quoted 7 lines · On Mon, 3 Mar 2025 15:26:03 +0800 Qi Zheng <[email protected]> wrote:

Now, the nine architectures of csky, hexagon, loongarch, m68k, mips, nios2, openrisc, sh and um do not select CONFIG_MMU_GATHER_RCU_TABLE_FREE, and just call pagetable_dtor() + tlb_remove_page_ptdesc() (the wrapper of tlb_remove_page()). This is the same as the implementation of tlb_remove_{ptdesc|table}() under !CONFIG_MMU_GATHER_TABLE_FREE, so convert these architectures to use tlb_remove_ptdesc().

checkpatch warns.

Do these things have to be macros? Switching to static inline fixes the unused-arg warning in a nice fashion.

Quoted 7 lines
Quoted 4 lines

This can be switched to static inline. In addition, I found that alpha, arc, microblaze, parisc, sparc32 and xtensa also have the unused-arg issue. Do I need to add a new patch to fix all of them, or just fix the newly added 11 warnings?

I guess leave things as they are now. Switching all these to C functions can be addressed at a later time?

Okay, let's leave it for later.

author
#14 lore raw

PATCH v2 4/6 riscv: pgtable: unconditionally use tlb_remove_ptdesc()

In reply to Qi Zheng

To support fast gup, the commit 69be3fb111e7 ("riscv: enable MMU_GATHER_RCU_TABLE_FREE for SMP && MMU") did the following:

1) use tlb_remove_page_ptdesc() for those platforms which use IPI to perform TLB shootdown

2) use tlb_remove_ptdesc() for those platforms which use SBI to perform TLB shootdown

The tlb_remove_page_ptdesc() is the wrapper of the tlb_remove_page(). By design, the tlb_remove_page() should be used to remove a normal page from a page table entry, and should not be used for page table pages.

The tlb_remove_ptdesc() is the wrapper of the tlb_remove_table(), which is designed specifically for freeing page table pages. If the CONFIG_MMU_GATHER_TABLE_FREE is enabled, the tlb_remove_table() will use semi RCU to free page table pages, that is:

- batch table freeing: asynchronous free by RCU
- single table freeing: IPI + synchronous free

If the CONFIG_MMU_GATHER_TABLE_FREE is disabled, the tlb_remove_table() will fall back to pagetable_dtor() + tlb_remove_page().

For case 1), since we need to perform TLB shootdown before freeing the page table page, the local_irq_save() in fast gup can block the freeing and protect the fast gup page walker. Therefore we can ensure safety by just using tlb_remove_page_ptdesc(). In addition, we can also the tlb_remove_ptdesc()/tlb_remove_table() to achieve it, and it doesn't matter whether CONFIG_MMU_GATHER_RCU_TABLE_FREE is selected. And in theory, the performance of freeing pages asynchronously via RCU will not be lower than synchronous free.

For case 2), since local_irq_save() only disable S-privilege IPI irq but not M-privilege's, which is used by the SBI implementation to perform TLB shootdown, so we must select CONFIG_MMU_GATHER_RCU_TABLE_FREE and use tlb_remove_ptdesc() to ensure safety. The riscv selects this config for SMP && MMU, the CONFIG_RISCV_SBI is dependent on MMU. Therefore, only the UP system may have the situation where CONFIG_MMU_GATHER_RCU_TABLE_FREE is disabled but CONFIG_RISCV_SBI is enabled. But there is no freeing vs fast gup race in the UP system.

So, in summary, we can use tlb_remove_ptdesc() to support fast gup in all cases, and this interface is specifically designed for page table pages. So let's use it unconditionally.

---
 arch/riscv/include/asm/pgalloc.h | 26 ++++----------------------
 1 file changed, 4 insertions(+), 22 deletions(-)
arch/riscv/include/asm/pgalloc.h +4 -22
index 3e2aebea6312d..770ce18a7328b 100644
@@ -15,24 +15,6 @@
 #define __HAVE_ARCH_PUD_FREE
 #include <asm-generic/pgalloc.h>
 
-/*
- * While riscv platforms with riscv_ipi_for_rfence as true require an IPI to
- * perform TLB shootdown, some platforms with riscv_ipi_for_rfence as false use
- * SBI to perform TLB shootdown. To keep software pagetable walkers safe in this
- * case we switch to RCU based table free (MMU_GATHER_RCU_TABLE_FREE). See the
- * comment below 'ifdef CONFIG_MMU_GATHER_RCU_TABLE_FREE' in include/asm-generic/tlb.h
- * for more details.
- */
-static inline void riscv_tlb_remove_ptdesc(struct mmu_gather *tlb, void *pt)
-{
-	if (riscv_use_sbi_for_rfence()) {
-		tlb_remove_ptdesc(tlb, pt);
-	} else {
-		pagetable_dtor(pt);
-		tlb_remove_page_ptdesc(tlb, pt);
-	}
-}
-
 static inline void pmd_populate_kernel(struct mm_struct *mm,
 	pmd_t *pmd, pte_t *pte)
 {
@@ -108,14 +90,14 @@ static inline void __pud_free_tlb(struct mmu_gather *tlb, pud_t *pud,
 				  unsigned long addr)
 {
 	if (pgtable_l4_enabled)
-		riscv_tlb_remove_ptdesc(tlb, virt_to_ptdesc(pud));
+		tlb_remove_ptdesc(tlb, virt_to_ptdesc(pud));
 }
 
 static inline void __p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d,
 				  unsigned long addr)
 {
 	if (pgtable_l5_enabled)
-		riscv_tlb_remove_ptdesc(tlb, virt_to_ptdesc(p4d));
+		tlb_remove_ptdesc(tlb, virt_to_ptdesc(p4d));
 }
 #endif /* __PAGETABLE_PMD_FOLDED */
 
@@ -143,7 +125,7 @@ static inline pgd_t *pgd_alloc(struct mm_struct *mm)
 static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd,
 				  unsigned long addr)
 {
-	riscv_tlb_remove_ptdesc(tlb, virt_to_ptdesc(pmd));
+	tlb_remove_ptdesc(tlb, virt_to_ptdesc(pmd));
 }
 
 #endif /* __PAGETABLE_PMD_FOLDED */
@@ -151,7 +133,7 @@ static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd,
 static inline void __pte_free_tlb(struct mmu_gather *tlb, pgtable_t pte,
 				  unsigned long addr)
 {
-	riscv_tlb_remove_ptdesc(tlb, page_ptdesc(pte));
+	tlb_remove_ptdesc(tlb, page_ptdesc(pte));
 }
 #endif /* CONFIG_MMU */
 
Signature · 1 line
2.20.1
#15 lore raw

Re: [External] [PATCH v2 4/6] riscv: pgtable: unconditionally use tlb_remove_ptdesc()

Hi Qi,

Quoted 118 lines · Qi Zheng

To support fast gup, the commit 69be3fb111e7 ("riscv: enable MMU_GATHER_RCU_TABLE_FREE for SMP && MMU") did the following:

1) use tlb_remove_page_ptdesc() for those platforms which use IPI to perform TLB shootdown

2) use tlb_remove_ptdesc() for those platforms which use SBI to perform TLB shootdown

The tlb_remove_page_ptdesc() is the wrapper of the tlb_remove_page(). By design, the tlb_remove_page() should be used to remove a normal page from a page table entry, and should not be used for page table pages.

The tlb_remove_ptdesc() is the wrapper of the tlb_remove_table(), which is designed specifically for freeing page table pages. If the CONFIG_MMU_GATHER_TABLE_FREE is enabled, the tlb_remove_table() will use semi RCU to free page table pages, that is:

- batch table freeing: asynchronous free by RCU
- single table freeing: IPI + synchronous free

If the CONFIG_MMU_GATHER_TABLE_FREE is disabled, the tlb_remove_table() will fall back to pagetable_dtor() + tlb_remove_page().

For case 1), since we need to perform TLB shootdown before freeing the page table page, the local_irq_save() in fast gup can block the freeing and protect the fast gup page walker. Therefore we can ensure safety by just using tlb_remove_page_ptdesc(). In addition, we can also the tlb_remove_ptdesc()/tlb_remove_table() to achieve it, and it doesn't matter whether CONFIG_MMU_GATHER_RCU_TABLE_FREE is selected. And in theory, the performance of freeing pages asynchronously via RCU will not be lower than synchronous free.

For case 2), since local_irq_save() only disable S-privilege IPI irq but not M-privilege's, which is used by the SBI implementation to perform TLB shootdown, so we must select CONFIG_MMU_GATHER_RCU_TABLE_FREE and use tlb_remove_ptdesc() to ensure safety. The riscv selects this config for SMP && MMU, the CONFIG_RISCV_SBI is dependent on MMU. Therefore, only the UP system may have the situation where CONFIG_MMU_GATHER_RCU_TABLE_FREE is disabled but CONFIG_RISCV_SBI is enabled. But there is no freeing vs fast gup race in the UP system.

So, in summary, we can use tlb_remove_ptdesc() to support fast gup in all cases, and this interface is specifically designed for page table pages. So let's use it unconditionally.

---
 arch/riscv/include/asm/pgalloc.h | 26 ++++----------------------
 1 file changed, 4 insertions(+), 22 deletions(-)
arch/riscv/include/asm/pgalloc.h +4 -23
index 3e2aebea6312d..770ce18a7328b 100644
@@ -15,24 +15,6 @@
 #define __HAVE_ARCH_PUD_FREE
 #include <asm-generic/pgalloc.h>
 
-/*
- * While riscv platforms with riscv_ipi_for_rfence as true require an IPI to
- * perform TLB shootdown, some platforms with riscv_ipi_for_rfence as false use
- * SBI to perform TLB shootdown. To keep software pagetable walkers safe in this
- * case we switch to RCU based table free (MMU_GATHER_RCU_TABLE_FREE). See the
- * comment below 'ifdef CONFIG_MMU_GATHER_RCU_TABLE_FREE' in include/asm-generic/tlb.h
- * for more details.
- */
-static inline void riscv_tlb_remove_ptdesc(struct mmu_gather *tlb, void *pt)
-{
-       if (riscv_use_sbi_for_rfence()) {
-               tlb_remove_ptdesc(tlb, pt);
-       } else {
-               pagetable_dtor(pt);
-               tlb_remove_page_ptdesc(tlb, pt);
-       }
-}
-
 static inline void pmd_populate_kernel(struct mm_struct *mm,
        pmd_t *pmd, pte_t *pte)
 {
@@ -108,14 +90,14 @@ static inline void __pud_free_tlb(struct mmu_gather *tlb, pud_t *pud,
                                  unsigned long addr)
 {
        if (pgtable_l4_enabled)
-               riscv_tlb_remove_ptdesc(tlb, virt_to_ptdesc(pud));
+               tlb_remove_ptdesc(tlb, virt_to_ptdesc(pud));
 }
 
 static inline void __p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d,
                                  unsigned long addr)
 {
        if (pgtable_l5_enabled)
-               riscv_tlb_remove_ptdesc(tlb, virt_to_ptdesc(p4d));
+               tlb_remove_ptdesc(tlb, virt_to_ptdesc(p4d));
 }
 #endif /* __PAGETABLE_PMD_FOLDED */
 
@@ -143,7 +125,7 @@ static inline pgd_t *pgd_alloc(struct mm_struct *mm)
 static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd,
                                  unsigned long addr)
 {
-       riscv_tlb_remove_ptdesc(tlb, virt_to_ptdesc(pmd));
+       tlb_remove_ptdesc(tlb, virt_to_ptdesc(pmd));
 }
 
 #endif /* __PAGETABLE_PMD_FOLDED */
@@ -151,7 +133,7 @@ static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd,
 static inline void __pte_free_tlb(struct mmu_gather *tlb, pgtable_t pte,
                                  unsigned long addr)
 {
-       riscv_tlb_remove_ptdesc(tlb, page_ptdesc(pte));
+       tlb_remove_ptdesc(tlb, page_ptdesc(pte));
 }
 #endif /* CONFIG_MMU */
 
--
Quoted 3 lines

2.20.1

This set of patches mainly refactors the remove page table function interfaces. The comment "riscv_ipi_for_rfence" has been removed, as it was no longer needed, and this patch handles that.

Additionally, whether riscv_use_sbi_for_rfence is true or false, page tables can now be released using RCU. This patch changes the previous synchronous release logic (for !riscv_use_sbi_for_rfence) to an RCU-based release.

So, Reviewed-by: Yunhui Cui <[email protected]>

Based on qemu-system-riscv64, I tested this patch. The log is as follows:
./gup_test
TAP version 13
1..1
# GUP_FAST_BENCHMARK: Time: get:663365 put:117 us

# ok 1 ioctl status 0
# Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0

So, Tested-by: Yunhui Cui <[email protected]>

Thanks,
Yunhui

author
#16 lore raw

Re: [PATCH v2 4/6] riscv: pgtable: unconditionally use tlb_remove_ptdesc()

Quoted 2 lines · yunhui cui

Hi Qi,

[...]

Quoted 14 lines
Quoted 2 lines

This set of patches mainly refactors the remove page table function interfaces. The comment "riscv_ipi_for_rfence" has been removed, as it was no longer needed, and this patch handles that.

Additionally, whether riscv_use_sbi_for_rfence is true or false, page tables can now be released using RCU. This patch changes the previous synchronous release logic (for !riscv_use_sbi_for_rfence) to an RCU-based release.

So, Reviewed-by: Yunhui Cui <[email protected]>

Quoted 7 lines

Based on qemu-system-riscv64, I tested this patch. The log is as follows:
./gup_test
TAP version 13
1..1
# GUP_FAST_BENCHMARK: Time: get:663365 put:117 us

# ok 1 ioctl status 0
# Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0

Nice.

Quoted 2 lines

So, Tested-by: Yunhui Cui <[email protected]>

Thank you very much for your review and testing!

Quoted 3 lines

Thanks,
Yunhui

author
#17 lore raw

PATCH v2 5/6 x86: pgtable: convert to use tlb_remove_ptdesc()

In reply to Qi Zheng

The x86 has already been converted to use struct ptdesc, so convert it to use tlb_remove_ptdesc() instead of tlb_remove_table().

---
 arch/x86/mm/pgtable.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/mm/pgtable.c +4 -4
index b1c1f72c1fd1b..f28ddac0f734a 100644
@@ -45,7 +45,7 @@ early_param("userpte", setup_userpte);
 void ___pte_free_tlb(struct mmu_gather *tlb, struct page *pte)
 {
 	paravirt_release_pte(page_to_pfn(pte));
-	tlb_remove_table(tlb, page_ptdesc(pte));
+	tlb_remove_ptdesc(tlb, page_ptdesc(pte));
 }
 
 #if CONFIG_PGTABLE_LEVELS > 2
@@ -59,21 +59,21 @@ void ___pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd)
 #ifdef CONFIG_X86_PAE
 	tlb->need_flush_all = 1;
 #endif
-	tlb_remove_table(tlb, virt_to_ptdesc(pmd));
+	tlb_remove_ptdesc(tlb, virt_to_ptdesc(pmd));
 }
 
 #if CONFIG_PGTABLE_LEVELS > 3
 void ___pud_free_tlb(struct mmu_gather *tlb, pud_t *pud)
 {
 	paravirt_release_pud(__pa(pud) >> PAGE_SHIFT);
-	tlb_remove_table(tlb, virt_to_ptdesc(pud));
+	tlb_remove_ptdesc(tlb, virt_to_ptdesc(pud));
 }
 
 #if CONFIG_PGTABLE_LEVELS > 4
 void ___p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d)
 {
 	paravirt_release_p4d(__pa(p4d) >> PAGE_SHIFT);
-	tlb_remove_table(tlb, virt_to_ptdesc(p4d));
+	tlb_remove_ptdesc(tlb, virt_to_ptdesc(p4d));
 }
 #endif	/* CONFIG_PGTABLE_LEVELS > 4 */
 #endif	/* CONFIG_PGTABLE_LEVELS > 3 */
Signature · 1 line
2.20.1
maintainer
#18 lore raw

Re: [PATCH v2 5/6] x86: pgtable: convert to use tlb_remove_ptdesc()

Quoted 3 lines · On Tue, 25 Feb 2025 11:45:55 +0800 Qi Zheng <[email protected]> wrote:

The x86 has already been converted to use struct ptdesc, so convert it to use tlb_remove_ptdesc() instead of tlb_remove_table().

This is dependent upon Rik's a37259732a7dc ("x86/mm: Make MMU_GATHER_RCU_TABLE_FREE unconditional") from the x86 tree. I'll add Rik's patch to mm-unstable also and shall figure it out during the merge window.

author
#19 lore raw

Re: [PATCH v2 5/6] x86: pgtable: convert to use tlb_remove_ptdesc()

Quoted 8 lines · Andrew Morton
Quoted 3 lines · On Tue, 25 Feb 2025 11:45:55 +0800 Qi Zheng <[email protected]> wrote:

The x86 has already been converted to use struct ptdesc, so convert it to use tlb_remove_ptdesc() instead of tlb_remove_table().

This is dependent upon Rik's a37259732a7dc ("x86/mm: Make MMU_GATHER_RCU_TABLE_FREE unconditional") from the x86 tree. I'll add

Yes.

Quoted 2 lines

Rik's patch to mm-unstable also and shall figure it out during the merge window.

Thanks!

author
#20 lore raw

PATCH v2 6/6 mm: pgtable: remove tlb_remove_page_ptdesc()

In reply to Qi Zheng

The tlb_remove_ptdesc()/tlb_remove_table() is specially designed for page table pages, and now all architectures have been converted to use it to remove page table pages. So let's remove tlb_remove_page_ptdesc(), it currently has no users and should not be used for page table pages.

---
 include/asm-generic/tlb.h | 6 ------
 1 file changed, 6 deletions(-)
include/asm-generic/tlb.h +0 -6
index 18bf499ef8801..dd292c6d3ce88 100644
@@ -511,12 +511,6 @@ static inline void tlb_remove_ptdesc(struct mmu_gather *tlb, struct ptdesc *pt)
 	tlb_remove_table(tlb, pt);
 }
 
-/* Like tlb_remove_ptdesc, but for page-like page directories. */
-static inline void tlb_remove_page_ptdesc(struct mmu_gather *tlb, struct ptdesc *pt)
-{
-	tlb_remove_page(tlb, ptdesc_page(pt));
-}
-
 static inline void tlb_change_page_size(struct mmu_gather *tlb,
 						     unsigned int page_size)
 {
Signature · 1 line
2.20.1
Reviewed-by on v2 6/6
#21 lore raw

Re: [PATCH v2 6/6] mm: pgtable: remove tlb_remove_page_ptdesc()

Quoted 7 lines · Qi Zheng

The tlb_remove_ptdesc()/tlb_remove_table() is specially designed for page table pages, and now all architectures have been converted to use it to remove page table pages. So let's remove tlb_remove_page_ptdesc(), it currently has no users and should not be used for page table pages.