Skip to contentSkip to conversation

mm: Move set_pxd_safe() helpers from generic to platform

PATCH Superseded Anshuman Khandual 3 participants 10 messages Sep 20, 2024 – Sep 26, 2024

Subsystems (5)
Memory management x86 RISC-V MM core +1 more subsystems
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: Move set_pxd_safe() helpers from generic to platform

set_pxd_safe() helpers that serve a specific purpose for both x86 and riscv platforms, do not need to be in the common memory code. Otherwise they just unnecessarily make the common API more complicated. This moves the helpers from common code to platform instead.

---
 arch/riscv/include/asm/pgtable.h | 19 ++++++++++++++++
 arch/x86/include/asm/pgtable.h   | 37 +++++++++++++++++++++++++++++++
 include/linux/pgtable.h          | 38 --------------------------------
 3 files changed, 56 insertions(+), 38 deletions(-)
arch/riscv/include/asm/pgtable.h +19 -0
index 089f3c9f56a3..39ca652c5ebe 100644
@@ -957,6 +957,25 @@ void misc_mem_init(void);
 extern unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)];
 #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
 
+/*
+ * Use set_p*_safe(), and elide TLB flushing, when confident that *no*
+ * TLB flush will be required as a result of the "set". For example, use
+ * in scenarios where it is known ahead of time that the routine is
+ * setting non-present entries, or re-setting an existing entry to the
+ * same value. Otherwise, use the typical "set" helpers and flush the
+ * TLB.
+ */
+#define set_p4d_safe(p4dp, p4d) \
+({ \
+	WARN_ON_ONCE(p4d_present(*p4dp) && !p4d_same(*p4dp, p4d)); \
+	set_p4d(p4dp, p4d); \
+})
+
+#define set_pgd_safe(pgdp, pgd) \
+({ \
+	WARN_ON_ONCE(pgd_present(*pgdp) && !pgd_same(*pgdp, pgd)); \
+	set_pgd(pgdp, pgd); \
+})
 #endif /* !__ASSEMBLY__ */
 
 #endif /* _ASM_RISCV_PGTABLE_H */
arch/x86/include/asm/pgtable.h +37 -0
index e39311a89bf4..fefb52bb6b4d 100644
@@ -1701,6 +1701,43 @@ bool arch_is_platform_page(u64 paddr);
 #define arch_is_platform_page arch_is_platform_page
 #endif
 
+/*
+ * Use set_p*_safe(), and elide TLB flushing, when confident that *no*
+ * TLB flush will be required as a result of the "set". For example, use
+ * in scenarios where it is known ahead of time that the routine is
+ * setting non-present entries, or re-setting an existing entry to the
+ * same value. Otherwise, use the typical "set" helpers and flush the
+ * TLB.
+ */
+#define set_pte_safe(ptep, pte) \
+({ \
+	WARN_ON_ONCE(pte_present(*ptep) && !pte_same(*ptep, pte)); \
+	set_pte(ptep, pte); \
+})
+
+#define set_pmd_safe(pmdp, pmd) \
+({ \
+	WARN_ON_ONCE(pmd_present(*pmdp) && !pmd_same(*pmdp, pmd)); \
+	set_pmd(pmdp, pmd); \
+})
+
+#define set_pud_safe(pudp, pud) \
+({ \
+	WARN_ON_ONCE(pud_present(*pudp) && !pud_same(*pudp, pud)); \
+	set_pud(pudp, pud); \
+})
+
+#define set_p4d_safe(p4dp, p4d) \
+({ \
+	WARN_ON_ONCE(p4d_present(*p4dp) && !p4d_same(*p4dp, p4d)); \
+	set_p4d(p4dp, p4d); \
+})
+
+#define set_pgd_safe(pgdp, pgd) \
+({ \
+	WARN_ON_ONCE(pgd_present(*pgdp) && !pgd_same(*pgdp, pgd)); \
+	set_pgd(pgdp, pgd); \
+})
 #endif	/* __ASSEMBLY__ */
 
 #endif /* _ASM_X86_PGTABLE_H */
include/linux/pgtable.h +0 -38
index 2a6a3cccfc36..0bf88e505aad 100644
@@ -1050,44 +1050,6 @@ static inline int pgd_same(pgd_t pgd_a, pgd_t pgd_b)
 }
 #endif
 
-/*
- * Use set_p*_safe(), and elide TLB flushing, when confident that *no*
- * TLB flush will be required as a result of the "set". For example, use
- * in scenarios where it is known ahead of time that the routine is
- * setting non-present entries, or re-setting an existing entry to the
- * same value. Otherwise, use the typical "set" helpers and flush the
- * TLB.
- */
-#define set_pte_safe(ptep, pte) \
-({ \
-	WARN_ON_ONCE(pte_present(*ptep) && !pte_same(*ptep, pte)); \
-	set_pte(ptep, pte); \
-})
-
-#define set_pmd_safe(pmdp, pmd) \
-({ \
-	WARN_ON_ONCE(pmd_present(*pmdp) && !pmd_same(*pmdp, pmd)); \
-	set_pmd(pmdp, pmd); \
-})
-
-#define set_pud_safe(pudp, pud) \
-({ \
-	WARN_ON_ONCE(pud_present(*pudp) && !pud_same(*pudp, pud)); \
-	set_pud(pudp, pud); \
-})
-
-#define set_p4d_safe(p4dp, p4d) \
-({ \
-	WARN_ON_ONCE(p4d_present(*p4dp) && !p4d_same(*p4dp, p4d)); \
-	set_p4d(p4dp, p4d); \
-})
-
-#define set_pgd_safe(pgdp, pgd) \
-({ \
-	WARN_ON_ONCE(pgd_present(*pgdp) && !pgd_same(*pgdp, pgd)); \
-	set_pgd(pgdp, pgd); \
-})
-
 #ifndef __HAVE_ARCH_DO_SWAP_PAGE
 static inline void arch_do_swap_page_nr(struct mm_struct *mm,
 				     struct vm_area_struct *vma,
Signature · 1 line
2.25.1
#1 lore raw
Quoted 4 lines · Anshuman Khandual

set_pxd_safe() helpers that serve a specific purpose for both x86 and riscv platforms, do not need to be in the common memory code. Otherwise they just unnecessarily make the common API more complicated. This moves the helpers from common code to platform instead.

I just did a quick grep and don't see any difference between the _safe and normal variants. A quick grep didn't turn up any actual users.

Did anyone actually double check that these are still needed on x86 in the first place?

#2 lore raw
Quoted 11 lines · Dave Hansen
Quoted 4 lines · Anshuman Khandual

set_pxd_safe() helpers that serve a specific purpose for both x86 and riscv platforms, do not need to be in the common memory code. Otherwise they just unnecessarily make the common API more complicated. This moves the helpers from common code to platform instead.

I just did a quick grep and don't see any difference between the _safe and normal variants. A quick grep didn't turn up any actual users.

Did anyone actually double check that these are still needed on x86 in the first place?

arch/x86/mm/init_64.c

#define DEFINE_ENTRY(type1, type2, init)                        \
static inline void set_##type1##_init(type1##_t *arg1,          \
                        type2##_t arg2, bool init)              \
{                                                               \
        if (init)                                               \
                set_##type1##_safe(arg1, arg2);                 \
        else                                                    \
                set_##type1(arg1, arg2);                        \

}

DEFINE_ENTRY(p4d, p4d, init)
DEFINE_ENTRY(pud, pud, init)
DEFINE_ENTRY(pmd, pmd, init)
DEFINE_ENTRY(pte, pte, init)

We had triggered a build problem after dropping off set_pte_safe() which seemed not to be used after normal grep.

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

#3 lore raw
Quoted 6 lines · Anshuman Khandual
Quoted 5 lines

I just did a quick grep and don't see any difference between the _safe and normal variants. A quick grep didn't turn up any actual users.

Did anyone actually double check that these are still needed on x86 in the first place?

arch/x86/mm/init_64.c

Ahh, the #defines make them immune to grep. :)

Long-term, we should make sure these are still necessary. Short term (in this patch), please just put the #defines in init_64.c if it is the only site that needs them.

#4 lore raw
Quoted 11 lines · Dave Hansen
Quoted 6 lines · Anshuman Khandual
Quoted 5 lines

I just did a quick grep and don't see any difference between the _safe and normal variants. A quick grep didn't turn up any actual users.

Did anyone actually double check that these are still needed on x86 in the first place?

arch/x86/mm/init_64.c

Ahh, the #defines make them immune to grep. :)

Long-term, we should make sure these are still necessary. Short term

Agreed, David H has also mentioned about that.

Quoted 2 lines

(in this patch), please just put the #defines in init_64.c if it is the only site that needs them.

That is not the only site where they get used. set_pmd/pud/p4d/pgd_safe() are also called from respective pmd/pud/p4d/pgd_populate_safe() helpers in the header arch/x86/include/asm/pgalloc.h.

Besides also hit a road block in converting these macros as static inline functions as suggested by David H earlier, because pmd/pud/p4d/pgd_same() macros are defined in include/linux/pgtable.h, way after <asm/pgtable.h> gets included.

I guess we will have just leave these macros is in arch/x86/include/asm/pgtable.h

#5 lore raw
In reply to Anshuman Khandual
Quoted 90 lines · Anshuman Khandual

set_pxd_safe() helpers that serve a specific purpose for both x86 and riscv platforms, do not need to be in the common memory code. Otherwise they just unnecessarily make the common API more complicated. This moves the helpers from common code to platform instead.

---
  arch/riscv/include/asm/pgtable.h | 19 ++++++++++++++++
  arch/x86/include/asm/pgtable.h   | 37 +++++++++++++++++++++++++++++++
  include/linux/pgtable.h          | 38 --------------------------------
  3 files changed, 56 insertions(+), 38 deletions(-)
arch/riscv/include/asm/pgtable.h +19 -0
index 089f3c9f56a3..39ca652c5ebe 100644
@@ -957,6 +957,25 @@ void misc_mem_init(void);
  extern unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)];
  #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
 
+/*
+ * Use set_p*_safe(), and elide TLB flushing, when confident that *no*
+ * TLB flush will be required as a result of the "set". For example, use
+ * in scenarios where it is known ahead of time that the routine is
+ * setting non-present entries, or re-setting an existing entry to the
+ * same value. Otherwise, use the typical "set" helpers and flush the
+ * TLB.
+ */
+#define set_p4d_safe(p4dp, p4d) \
+({ \
+	WARN_ON_ONCE(p4d_present(*p4dp) && !p4d_same(*p4dp, p4d)); \
+	set_p4d(p4dp, p4d); \
+})
+
+#define set_pgd_safe(pgdp, pgd) \
+({ \
+	WARN_ON_ONCE(pgd_present(*pgdp) && !pgd_same(*pgdp, pgd)); \
+	set_pgd(pgdp, pgd); \
+})
  #endif /* !__ASSEMBLY__ */
 
  #endif /* _ASM_RISCV_PGTABLE_H */
arch/x86/include/asm/pgtable.h +28 -0
index e39311a89bf4..fefb52bb6b4d 100644
@@ -1701,6 +1701,43 @@ bool arch_is_platform_page(u64 paddr);
  #define arch_is_platform_page arch_is_platform_page
  #endif
 
+/*
+ * Use set_p*_safe(), and elide TLB flushing, when confident that *no*
+ * TLB flush will be required as a result of the "set". For example, use
+ * in scenarios where it is known ahead of time that the routine is
+ * setting non-present entries, or re-setting an existing entry to the
+ * same value. Otherwise, use the typical "set" helpers and flush the
+ * TLB.
+ */
+#define set_pte_safe(ptep, pte) \
+({ \
+	WARN_ON_ONCE(pte_present(*ptep) && !pte_same(*ptep, pte)); \
+	set_pte(ptep, pte); \
+})
+
+#define set_pmd_safe(pmdp, pmd) \
+({ \
+	WARN_ON_ONCE(pmd_present(*pmdp) && !pmd_same(*pmdp, pmd)); \
+	set_pmd(pmdp, pmd); \
+})
+
+#define set_pud_safe(pudp, pud) \
+({ \
+	WARN_ON_ONCE(pud_present(*pudp) && !pud_same(*pudp, pud)); \
+	set_pud(pudp, pud); \
+})
+
+#define set_p4d_safe(p4dp, p4d) \
+({ \

arch/x86/include/asm/pgtable.h bool arch_is_platform_page(u64 paddr); · line 1741

Quoted 10 lines
arch/x86/include/asm/pgtable.h +9 -0
+	WARN_ON_ONCE(p4d_present(*p4dp) && !p4d_same(*p4dp, p4d)); \
+	set_p4d(p4dp, p4d); \
+})
+
+#define set_pgd_safe(pgdp, pgd) \
+({ \
+	WARN_ON_ONCE(pgd_present(*pgdp) && !pgd_same(*pgdp, pgd)); \
+	set_pgd(pgdp, pgd); \
+})
  #endif	/* __ASSEMBLY__ */

I'm wondering if we can completely get rid of these, for example via:

arch/x86/mm/init_64.c +2 -4
index d8dbeac8b206..bc71c25930bb 100644
@@ -79,10 +79,8 @@ DEFINE_POPULATE(pmd_populate_kernel, pmd, pte, init)
 static inline void set_##type1##_init(type1##_t *arg1,         \
                        type2##_t arg2, bool init)              \
 {                                                              \
-       if (init)                                               \
-               set_##type1##_safe(arg1, arg2);                 \
-       else                                                    \
-               set_##type1(arg1, arg2);                        \
+       WARN_ON_ONCE(init && ##type1##_present(*arg1) && !##type1##_same(*arg1, arg2)); \
+       set_##type1(arg1, arg2);                                \
 }
 
 

We might be able to handle the pgd_populate etc part similarly, possibly getting rid of the pgd_populate_safe etc as well.

Assuming I don't miss anything important :)

Ideally, we get rid of the macros here and just use inline functions ...

Signature · 3 lines
Cheers,

David / dhildenb
#6 lore raw
Quoted 118 lines · David Hildenbrand
Quoted 96 lines · Anshuman Khandual

set_pxd_safe() helpers that serve a specific purpose for both x86 and riscv platforms, do not need to be in the common memory code. Otherwise they just unnecessarily make the common API more complicated. This moves the helpers from common code to platform instead.

---
  arch/riscv/include/asm/pgtable.h | 19 ++++++++++++++++ arch/x86/include/asm/pgtable.h   | 37 +++++++++++++++++++++++++++++++ include/linux/pgtable.h          | 38 --------------------------------

  3 files changed, 56 insertions(+), 38 deletions(-)
arch/riscv/include/asm/pgtable.h +0 -0
index 089f3c9f56a3..39ca652c5ebe 100644
@@ -957,6 +957,25 @@ void misc_mem_init(void);
   extern unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)];
   #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))

  +/*
+ * Use set_p*_safe(), and elide TLB flushing, when confident that *no*
+ * TLB flush will be required as a result of the "set". For example, use
+ * in scenarios where it is known ahead of time that the routine is
+ * setting non-present entries, or re-setting an existing entry to the
+ * same value. Otherwise, use the typical "set" helpers and flush the
+ * TLB.
+ */

arch/riscv/include/asm/pgtable.h +11 -0
+#define set_p4d_safe(p4dp, p4d) \
+({ \
+    WARN_ON_ONCE(p4d_present(*p4dp) && !p4d_same(*p4dp, p4d)); \
+    set_p4d(p4dp, p4d); \
+})
+
+#define set_pgd_safe(pgdp, pgd) \
+({ \
+    WARN_ON_ONCE(pgd_present(*pgdp) && !pgd_same(*pgdp, pgd)); \
+    set_pgd(pgdp, pgd); \
+})

  #endif /* !__ASSEMBLY__ */
    #endif /* _ASM_RISCV_PGTABLE_H */

arch/x86/include/asm/pgtable.h +0 -0
index e39311a89bf4..fefb52bb6b4d 100644
@@ -1701,6 +1701,43 @@ bool arch_is_platform_page(u64 paddr);
   #define arch_is_platform_page arch_is_platform_page
   #endif

  +/*
+ * Use set_p*_safe(), and elide TLB flushing, when confident that *no*
+ * TLB flush will be required as a result of the "set". For example, use
+ * in scenarios where it is known ahead of time that the routine is
+ * setting non-present entries, or re-setting an existing entry to the
+ * same value. Otherwise, use the typical "set" helpers and flush the
+ * TLB.
+ */

arch/x86/include/asm/pgtable.h +29 -0
+#define set_pte_safe(ptep, pte) \
+({ \
+    WARN_ON_ONCE(pte_present(*ptep) && !pte_same(*ptep, pte)); \
+    set_pte(ptep, pte); \
+})
+
+#define set_pmd_safe(pmdp, pmd) \
+({ \
+    WARN_ON_ONCE(pmd_present(*pmdp) && !pmd_same(*pmdp, pmd)); \
+    set_pmd(pmdp, pmd); \
+})
+
+#define set_pud_safe(pudp, pud) \
+({ \
+    WARN_ON_ONCE(pud_present(*pudp) && !pud_same(*pudp, pud)); \
+    set_pud(pudp, pud); \
+})
+
+#define set_p4d_safe(p4dp, p4d) \
+({ \
+    WARN_ON_ONCE(p4d_present(*p4dp) && !p4d_same(*p4dp, p4d)); \
+    set_p4d(p4dp, p4d); \
+})
+
+#define set_pgd_safe(pgdp, pgd) \
+({ \
+    WARN_ON_ONCE(pgd_present(*pgdp) && !pgd_same(*pgdp, pgd)); \
+    set_pgd(pgdp, pgd); \
+})
Quoted 1 line

  #endif    /* __ASSEMBLY__ */

I'm wondering if we can completely get rid of these, for example via:

arch/x86/mm/init_64.c +2 -4
index d8dbeac8b206..bc71c25930bb 100644
@@ -79,10 +79,8 @@ DEFINE_POPULATE(pmd_populate_kernel, pmd, pte, init)
  static inline void set_##type1##_init(type1##_t *arg1,         \
                         type2##_t arg2, bool init)              \
  {                                                              \
-       if (init)                                               \
-               set_##type1##_safe(arg1, arg2);                 \
-       else                                                    \
-               set_##type1(arg1, arg2);                        \
+       WARN_ON_ONCE(init && ##type1##_present(*arg1) && !##type1##_same(*arg1, arg2)); \
+       set_##type1(arg1, arg2);                                \
  }

Quoted 4 lines

We might be able to handle the pgd_populate etc part similarly, possibly getting rid of the pgd_populate_safe etc as well.

Assuming I don't miss anything important :)

Sounds feasible but will just leave that upto the x86 platform folks to change later on, after this patch which just moves these helpers inside the platform code.

Quoted 3 lines

Ideally, we get rid of the macros here and just use inline functions ...

Sure, makes sense. Will change these as inline functions.

-#define set_pte_safe(ptep, pte) \
-({ \
- WARN_ON_ONCE(pte_present(*ptep) && !pte_same(*ptep, pte)); \
- set_pte(ptep, pte); \
-})
+static inline void set_pte_safe(pte_t *ptep, pte_t pte) +{
+ WARN_ON_ONCE(pte_present(*ptep) && !pte_same(*ptep, pte));
+ set_pte(ptep, pte);
+}

#7 lore raw
Quoted 137 lines · Anshuman Khandual
Quoted 118 lines · David Hildenbrand
Quoted 96 lines · Anshuman Khandual

set_pxd_safe() helpers that serve a specific purpose for both x86 and riscv platforms, do not need to be in the common memory code. Otherwise they just unnecessarily make the common API more complicated. This moves the helpers from common code to platform instead.

---
  arch/riscv/include/asm/pgtable.h | 19 ++++++++++++++++ arch/x86/include/asm/pgtable.h   | 37 +++++++++++++++++++++++++++++++ include/linux/pgtable.h          | 38 --------------------------------

  3 files changed, 56 insertions(+), 38 deletions(-)
arch/riscv/include/asm/pgtable.h +0 -0
index 089f3c9f56a3..39ca652c5ebe 100644
@@ -957,6 +957,25 @@ void misc_mem_init(void);
   extern unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)];
   #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))

  +/*
+ * Use set_p*_safe(), and elide TLB flushing, when confident that *no*
+ * TLB flush will be required as a result of the "set". For example, use
+ * in scenarios where it is known ahead of time that the routine is
+ * setting non-present entries, or re-setting an existing entry to the
+ * same value. Otherwise, use the typical "set" helpers and flush the
+ * TLB.
+ */

arch/riscv/include/asm/pgtable.h +11 -0
+#define set_p4d_safe(p4dp, p4d) \
+({ \
+    WARN_ON_ONCE(p4d_present(*p4dp) && !p4d_same(*p4dp, p4d)); \
+    set_p4d(p4dp, p4d); \
+})
+
+#define set_pgd_safe(pgdp, pgd) \
+({ \
+    WARN_ON_ONCE(pgd_present(*pgdp) && !pgd_same(*pgdp, pgd)); \
+    set_pgd(pgdp, pgd); \
+})

  #endif /* !__ASSEMBLY__ */
    #endif /* _ASM_RISCV_PGTABLE_H */

arch/x86/include/asm/pgtable.h +0 -0
index e39311a89bf4..fefb52bb6b4d 100644
@@ -1701,6 +1701,43 @@ bool arch_is_platform_page(u64 paddr);
   #define arch_is_platform_page arch_is_platform_page
   #endif

  +/*
+ * Use set_p*_safe(), and elide TLB flushing, when confident that *no*
+ * TLB flush will be required as a result of the "set". For example, use
+ * in scenarios where it is known ahead of time that the routine is
+ * setting non-present entries, or re-setting an existing entry to the
+ * same value. Otherwise, use the typical "set" helpers and flush the
+ * TLB.
+ */

arch/x86/include/asm/pgtable.h +29 -0
+#define set_pte_safe(ptep, pte) \
+({ \
+    WARN_ON_ONCE(pte_present(*ptep) && !pte_same(*ptep, pte)); \
+    set_pte(ptep, pte); \
+})
+
+#define set_pmd_safe(pmdp, pmd) \
+({ \
+    WARN_ON_ONCE(pmd_present(*pmdp) && !pmd_same(*pmdp, pmd)); \
+    set_pmd(pmdp, pmd); \
+})
+
+#define set_pud_safe(pudp, pud) \
+({ \
+    WARN_ON_ONCE(pud_present(*pudp) && !pud_same(*pudp, pud)); \
+    set_pud(pudp, pud); \
+})
+
+#define set_p4d_safe(p4dp, p4d) \
+({ \
+    WARN_ON_ONCE(p4d_present(*p4dp) && !p4d_same(*p4dp, p4d)); \
+    set_p4d(p4dp, p4d); \
+})
+
+#define set_pgd_safe(pgdp, pgd) \
+({ \
+    WARN_ON_ONCE(pgd_present(*pgdp) && !pgd_same(*pgdp, pgd)); \
+    set_pgd(pgdp, pgd); \
+})
Quoted 1 line

  #endif    /* __ASSEMBLY__ */

I'm wondering if we can completely get rid of these, for example via:

arch/x86/mm/init_64.c +2 -4
index d8dbeac8b206..bc71c25930bb 100644
@@ -79,10 +79,8 @@ DEFINE_POPULATE(pmd_populate_kernel, pmd, pte, init)
  static inline void set_##type1##_init(type1##_t *arg1,         \
                         type2##_t arg2, bool init)              \
  {                                                              \
-       if (init)                                               \
-               set_##type1##_safe(arg1, arg2);                 \
-       else                                                    \
-               set_##type1(arg1, arg2);                        \
+       WARN_ON_ONCE(init && ##type1##_present(*arg1) && !##type1##_same(*arg1, arg2)); \
+       set_##type1(arg1, arg2);                                \
  }

Quoted 4 lines

We might be able to handle the pgd_populate etc part similarly, possibly getting rid of the pgd_populate_safe etc as well.

Assuming I don't miss anything important :)

Sounds feasible but will just leave that upto the x86 platform folks to change later on, after this patch which just moves these helpers inside the platform code.

Quoted 3 lines

Ideally, we get rid of the macros here and just use inline functions ...

Sure, makes sense. Will change these as inline functions.

Review

Quoted 10 lines
(quoted diff) +5 -5
-#define set_pte_safe(ptep, pte) \
-({ \
-       WARN_ON_ONCE(pte_present(*ptep) && !pte_same(*ptep, pte)); \
-       set_pte(ptep, pte); \
-})
+static inline void set_pte_safe(pte_t *ptep, pte_t pte)
+{
+       WARN_ON_ONCE(pte_present(*ptep) && !pte_same(*ptep, pte));
+       set_pte(ptep, pte);
+}

This has hit a road block in converting these macros as static inline functions as suggested earlier, because pmd/pud/p4d/pgd_same() macros are defined in generic header include/linux/pgtable.h, but way after <asm/pgtable.h> gets included. I guess then the current patch should be left as it is.

#8 lore raw
Quoted 148 lines · Anshuman Khandual
Quoted 145 lines · Anshuman Khandual
Quoted 121 lines · David Hildenbrand
Quoted 87 lines · Anshuman Khandual

set_pxd_safe() helpers that serve a specific purpose for both x86 and riscv platforms, do not need to be in the common memory code. Otherwise they just unnecessarily make the common API more complicated. This moves the helpers from common code to platform instead.

---
   arch/riscv/include/asm/pgtable.h | 19 ++++++++++++++++
   arch/x86/include/asm/pgtable.h   | 37 +++++++++++++++++++++++++++++++
   include/linux/pgtable.h          | 38 --------------------------------
   3 files changed, 56 insertions(+), 38 deletions(-)
arch/riscv/include/asm/pgtable.h +18 -0
index 089f3c9f56a3..39ca652c5ebe 100644
@@ -957,6 +957,25 @@ void misc_mem_init(void);
   extern unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)];
   #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
   +/*
+ * Use set_p*_safe(), and elide TLB flushing, when confident that *no*
+ * TLB flush will be required as a result of the "set". For example, use
+ * in scenarios where it is known ahead of time that the routine is
+ * setting non-present entries, or re-setting an existing entry to the
+ * same value. Otherwise, use the typical "set" helpers and flush the
+ * TLB.
+ */
+#define set_p4d_safe(p4dp, p4d) \
+({ \
+    WARN_ON_ONCE(p4d_present(*p4dp) && !p4d_same(*p4dp, p4d)); \
+    set_p4d(p4dp, p4d); \
+})
+
+#define set_pgd_safe(pgdp, pgd) \
+({ \
+    WARN_ON_ONCE(pgd_present(*pgdp) && !pgd_same(*pgdp, pgd)); \
+    set_pgd(pgdp, pgd); \
+})
   #endif /* !__ASSEMBLY__ */
     #endif /* _ASM_RISCV_PGTABLE_H */
arch/x86/include/asm/pgtable.h +27 -0
index e39311a89bf4..fefb52bb6b4d 100644
@@ -1701,6 +1701,43 @@ bool arch_is_platform_page(u64 paddr);
   #define arch_is_platform_page arch_is_platform_page
   #endif
   +/*
+ * Use set_p*_safe(), and elide TLB flushing, when confident that *no*
+ * TLB flush will be required as a result of the "set". For example, use
+ * in scenarios where it is known ahead of time that the routine is
+ * setting non-present entries, or re-setting an existing entry to the
+ * same value. Otherwise, use the typical "set" helpers and flush the
+ * TLB.
+ */
+#define set_pte_safe(ptep, pte) \
+({ \
+    WARN_ON_ONCE(pte_present(*ptep) && !pte_same(*ptep, pte)); \
+    set_pte(ptep, pte); \
+})
+
+#define set_pmd_safe(pmdp, pmd) \
+({ \
+    WARN_ON_ONCE(pmd_present(*pmdp) && !pmd_same(*pmdp, pmd)); \
+    set_pmd(pmdp, pmd); \
+})
+
+#define set_pud_safe(pudp, pud) \
+({ \
+    WARN_ON_ONCE(pud_present(*pudp) && !pud_same(*pudp, pud)); \
+    set_pud(pudp, pud); \
+})
+
+#define set_p4d_safe(p4dp, p4d) \
+({ \

arch/x86/include/asm/pgtable.h bool arch_is_platform_page(u64 paddr); · line 1740

Quoted 10 lines
arch/x86/include/asm/pgtable.h +9 -0
+    WARN_ON_ONCE(p4d_present(*p4dp) && !p4d_same(*p4dp, p4d)); \
+    set_p4d(p4dp, p4d); \
+})
+
+#define set_pgd_safe(pgdp, pgd) \
+({ \
+    WARN_ON_ONCE(pgd_present(*pgdp) && !pgd_same(*pgdp, pgd)); \
+    set_pgd(pgdp, pgd); \
+})
   #endif    /* __ASSEMBLY__ */

I'm wondering if we can completely get rid of these, for example via:

arch/x86/mm/init_64.c +2 -4
index d8dbeac8b206..bc71c25930bb 100644
@@ -79,10 +79,8 @@ DEFINE_POPULATE(pmd_populate_kernel, pmd, pte, init)
  static inline void set_##type1##_init(type1##_t *arg1,         \
                         type2##_t arg2, bool init)              \
  {                                                              \
-       if (init)                                               \
-               set_##type1##_safe(arg1, arg2);                 \
-       else                                                    \
-               set_##type1(arg1, arg2);                        \
+       WARN_ON_ONCE(init && ##type1##_present(*arg1) && !##type1##_same(*arg1, arg2)); \
+       set_##type1(arg1, arg2);                                \
  }
 
 

We might be able to handle the pgd_populate etc part similarly, possibly getting rid of the pgd_populate_safe etc as well.

Quoted 1 line

Assuming I don't miss anything important :)

Sounds feasible but will just leave that upto the x86 platform folks to change later on, after this patch which just moves these helpers inside the platform code.

Quoted 3 lines

Ideally, we get rid of the macros here and just use inline functions ...

Sure, makes sense. Will change these as inline functions.

(quoted diff) +4 -5
-#define set_pte_safe(ptep, pte) \
-({ \
-       WARN_ON_ONCE(pte_present(*ptep) && !pte_same(*ptep, pte)); \
-       set_pte(ptep, pte); \
-})
+static inline void set_pte_safe(pte_t *ptep, pte_t pte)
+{
+       WARN_ON_ONCE(pte_present(*ptep) && !pte_same(*ptep, pte));
+       set_pte(ptep, pte);
Quoted 8 lines
Quoted 2 lines

+}

This has hit a road block in converting these macros as static inline functions as suggested earlier, because pmd/pud/p4d/pgd_same() macros are defined in generic header include/linux/pgtable.h, but way after <asm/pgtable.h> gets included. I guess then the current patch should be left as it is.

Just to clarify: My ideas was to get rid of set_pte_safe() *completely* in x86 code, and turn set_pte_init() etc. into inline functions.

But we can do that on top, if Dave is fine with this patch.

Signature · 3 lines
Cheers,

David / dhildenb
#9 lore raw
In reply to Anshuman Khandual
Quoted 141 lines · Anshuman Khandual

set_pxd_safe() helpers that serve a specific purpose for both x86 and riscv platforms, do not need to be in the common memory code. Otherwise they just unnecessarily make the common API more complicated. This moves the helpers from common code to platform instead.

---
 arch/riscv/include/asm/pgtable.h | 19 ++++++++++++++++
 arch/x86/include/asm/pgtable.h   | 37 +++++++++++++++++++++++++++++++
 include/linux/pgtable.h          | 38 --------------------------------
 3 files changed, 56 insertions(+), 38 deletions(-)
arch/riscv/include/asm/pgtable.h +19 -0
index 089f3c9f56a3..39ca652c5ebe 100644
@@ -957,6 +957,25 @@ void misc_mem_init(void);
 extern unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)];
 #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
 
+/*
+ * Use set_p*_safe(), and elide TLB flushing, when confident that *no*
+ * TLB flush will be required as a result of the "set". For example, use
+ * in scenarios where it is known ahead of time that the routine is
+ * setting non-present entries, or re-setting an existing entry to the
+ * same value. Otherwise, use the typical "set" helpers and flush the
+ * TLB.
+ */
+#define set_p4d_safe(p4dp, p4d) \
+({ \
+	WARN_ON_ONCE(p4d_present(*p4dp) && !p4d_same(*p4dp, p4d)); \
+	set_p4d(p4dp, p4d); \
+})
+
+#define set_pgd_safe(pgdp, pgd) \
+({ \
+	WARN_ON_ONCE(pgd_present(*pgdp) && !pgd_same(*pgdp, pgd)); \
+	set_pgd(pgdp, pgd); \
+})
 #endif /* !__ASSEMBLY__ */
 
 #endif /* _ASM_RISCV_PGTABLE_H */
arch/x86/include/asm/pgtable.h +37 -0
index e39311a89bf4..fefb52bb6b4d 100644
@@ -1701,6 +1701,43 @@ bool arch_is_platform_page(u64 paddr);
 #define arch_is_platform_page arch_is_platform_page
 #endif
 
+/*
+ * Use set_p*_safe(), and elide TLB flushing, when confident that *no*
+ * TLB flush will be required as a result of the "set". For example, use
+ * in scenarios where it is known ahead of time that the routine is
+ * setting non-present entries, or re-setting an existing entry to the
+ * same value. Otherwise, use the typical "set" helpers and flush the
+ * TLB.
+ */
+#define set_pte_safe(ptep, pte) \
+({ \
+	WARN_ON_ONCE(pte_present(*ptep) && !pte_same(*ptep, pte)); \
+	set_pte(ptep, pte); \
+})
+
+#define set_pmd_safe(pmdp, pmd) \
+({ \
+	WARN_ON_ONCE(pmd_present(*pmdp) && !pmd_same(*pmdp, pmd)); \
+	set_pmd(pmdp, pmd); \
+})
+
+#define set_pud_safe(pudp, pud) \
+({ \
+	WARN_ON_ONCE(pud_present(*pudp) && !pud_same(*pudp, pud)); \
+	set_pud(pudp, pud); \
+})
+
+#define set_p4d_safe(p4dp, p4d) \
+({ \
+	WARN_ON_ONCE(p4d_present(*p4dp) && !p4d_same(*p4dp, p4d)); \
+	set_p4d(p4dp, p4d); \
+})
+
+#define set_pgd_safe(pgdp, pgd) \
+({ \
+	WARN_ON_ONCE(pgd_present(*pgdp) && !pgd_same(*pgdp, pgd)); \
+	set_pgd(pgdp, pgd); \
+})
 #endif	/* __ASSEMBLY__ */
 
 #endif /* _ASM_X86_PGTABLE_H */
include/linux/pgtable.h +0 -31
index 2a6a3cccfc36..0bf88e505aad 100644
@@ -1050,44 +1050,6 @@ static inline int pgd_same(pgd_t pgd_a, pgd_t pgd_b)
 }
 #endif
 
-/*
- * Use set_p*_safe(), and elide TLB flushing, when confident that *no*
- * TLB flush will be required as a result of the "set". For example, use
- * in scenarios where it is known ahead of time that the routine is
- * setting non-present entries, or re-setting an existing entry to the
- * same value. Otherwise, use the typical "set" helpers and flush the
- * TLB.
- */
-#define set_pte_safe(ptep, pte) \
-({ \
-	WARN_ON_ONCE(pte_present(*ptep) && !pte_same(*ptep, pte)); \
-	set_pte(ptep, pte); \
-})
-
-#define set_pmd_safe(pmdp, pmd) \
-({ \
-	WARN_ON_ONCE(pmd_present(*pmdp) && !pmd_same(*pmdp, pmd)); \
-	set_pmd(pmdp, pmd); \
-})
-
-#define set_pud_safe(pudp, pud) \
-({ \
-	WARN_ON_ONCE(pud_present(*pudp) && !pud_same(*pudp, pud)); \
-	set_pud(pudp, pud); \
-})
-
-#define set_p4d_safe(p4dp, p4d) \
-({ \
-	WARN_ON_ONCE(p4d_present(*p4dp) && !p4d_same(*p4dp, p4d)); \
-	set_p4d(p4dp, p4d); \
-})

include/linux/pgtable.h static inline int pgd_same(pgd_t pgd_a, pgd_t pgd_b) · line 1055

Quoted 10 lines
include/linux/pgtable.h +0 -7
-
-#define set_pgd_safe(pgdp, pgd) \
-({ \
-	WARN_ON_ONCE(pgd_present(*pgdp) && !pgd_same(*pgdp, pgd)); \
-	set_pgd(pgdp, pgd); \
-})
-
 #ifndef __HAVE_ARCH_DO_SWAP_PAGE
 static inline void arch_do_swap_page_nr(struct mm_struct *mm,
 				     struct vm_area_struct *vma,

Hello Dave/Palmer,

Unless there is still any more objection from x86 or riscv, could this patch be pulled ?

- Anshuman