arm64, powerpc, riscv, s390, x86: ptdump: refactor CONFIG_DEBUG_WX
authorChristophe Leroy <christophe.leroy@csgroup.eu>
Tue, 30 Jan 2024 10:34:33 +0000 (11:34 +0100)
committerAndrew Morton <akpm@linux-foundation.org>
Thu, 22 Feb 2024 18:24:47 +0000 (10:24 -0800)
All architectures using the core ptdump functionality also implement
CONFIG_DEBUG_WX, and they all do it more or less the same way, with a
function called debug_checkwx() that is called by mark_rodata_ro(), which
is a substitute to ptdump_check_wx() when CONFIG_DEBUG_WX is set and a
no-op otherwise.

Refactor by centrally defining debug_checkwx() in linux/ptdump.h and call
debug_checkwx() immediately after calling mark_rodata_ro() instead of
calling it at the end of every mark_rodata_ro().

On x86_32, mark_rodata_ro() first checks __supported_pte_mask has _PAGE_NX
before calling debug_checkwx().  Now the check is inside the callee
ptdump_walk_pgd_level_checkwx().

On powerpc_64, mark_rodata_ro() bails out early before calling
ptdump_check_wx() when the MMU doesn't have KERNEL_RO feature.  The check
is now also done in ptdump_check_wx() as it is called outside
mark_rodata_ro().

Link: https://lkml.kernel.org/r/a59b102d7964261d31ead0316a9f18628e4e7a8e.1706610398.git.christophe.leroy@csgroup.eu
Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>
Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Cc: Albert Ou <aou@eecs.berkeley.edu>
Cc: Alexander Gordeev <agordeev@linux.ibm.com>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: "Aneesh Kumar K.V (IBM)" <aneesh.kumar@kernel.org>
Cc: Borislav Petkov (AMD) <bp@alien8.de>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Christian Borntraeger <borntraeger@linux.ibm.com>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Gerald Schaefer <gerald.schaefer@linux.ibm.com>
Cc: Greg KH <greg@kroah.com>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: "Naveen N. Rao" <naveen.n.rao@linux.ibm.com>
Cc: Nicholas Piggin <npiggin@gmail.com>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Paul Walmsley <paul.walmsley@sifive.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Phong Tran <tranmanphong@gmail.com>
Cc: Russell King <linux@armlinux.org.uk>
Cc: Steven Price <steven.price@arm.com>
Cc: Sven Schnelle <svens@linux.ibm.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Will Deacon <will@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
18 files changed:
arch/arm64/include/asm/ptdump.h
arch/arm64/mm/mmu.c
arch/powerpc/mm/mmu_decl.h
arch/powerpc/mm/pgtable_32.c
arch/powerpc/mm/pgtable_64.c
arch/powerpc/mm/ptdump/ptdump.c
arch/riscv/include/asm/ptdump.h [deleted file]
arch/riscv/mm/init.c
arch/riscv/mm/ptdump.c
arch/s390/include/asm/ptdump.h [deleted file]
arch/s390/mm/dump_pagetables.c
arch/s390/mm/init.c
arch/x86/include/asm/pgtable.h
arch/x86/mm/dump_pagetables.c
arch/x86/mm/init_32.c
arch/x86/mm/init_64.c
include/linux/ptdump.h
init/main.c

index 581caac525b03affb45808198bce14be5d7a13fa..5b1701c76d1cec2b26f23b198d8528fb2aec8806 100644 (file)
@@ -29,13 +29,6 @@ void __init ptdump_debugfs_register(struct ptdump_info *info, const char *name);
 static inline void ptdump_debugfs_register(struct ptdump_info *info,
                                           const char *name) { }
 #endif
-void ptdump_check_wx(void);
 #endif /* CONFIG_PTDUMP_CORE */
 
-#ifdef CONFIG_DEBUG_WX
-#define debug_checkwx()        ptdump_check_wx()
-#else
-#define debug_checkwx()        do { } while (0)
-#endif
-
 #endif /* __ASM_PTDUMP_H */
index 1ac7467d34c9c333ace3df5259e40cc860bb4740..3a27d887f7dd71c0675af9140486f802ec64a281 100644 (file)
@@ -632,8 +632,6 @@ void mark_rodata_ro(void)
        section_size = (unsigned long)__init_begin - (unsigned long)__start_rodata;
        update_mapping_prot(__pa_symbol(__start_rodata), (unsigned long)__start_rodata,
                            section_size, PAGE_KERNEL_RO);
-
-       debug_checkwx();
 }
 
 static void __init map_kernel_segment(pgd_t *pgdp, void *va_start, void *va_end,
index 72341b9fb5521ff6032d6b3998bb972714e6a1d0..90dcc28440562954035808c3123f7b372fcf282d 100644 (file)
@@ -171,12 +171,6 @@ static inline void mmu_mark_rodata_ro(void) { }
 void __init mmu_mapin_immr(void);
 #endif
 
-#ifdef CONFIG_DEBUG_WX
-void ptdump_check_wx(void);
-#else
-static inline void ptdump_check_wx(void) { }
-#endif
-
 static inline bool debug_pagealloc_enabled_or_kfence(void)
 {
        return IS_ENABLED(CONFIG_KFENCE) || debug_pagealloc_enabled();
index 5c02fd08d61eff04253b5069a852f4da907348fe..12498017da8e43dba086334f491819ec922b264f 100644 (file)
@@ -153,7 +153,6 @@ void mark_rodata_ro(void)
 
        if (v_block_mapped((unsigned long)_stext + 1)) {
                mmu_mark_rodata_ro();
-               ptdump_check_wx();
                return;
        }
 
@@ -166,9 +165,6 @@ void mark_rodata_ro(void)
                   PFN_DOWN((unsigned long)_stext);
 
        set_memory_ro((unsigned long)_stext, numpages);
-
-       // mark_initmem_nx() should have already run by now
-       ptdump_check_wx();
 }
 #endif
 
index 5ac1fd30341bb29704bedabd63e43cc105e3f285..1b366526f4f21e58b81e6f89ec3cffa3a1715138 100644 (file)
@@ -150,9 +150,6 @@ void mark_rodata_ro(void)
                radix__mark_rodata_ro();
        else
                hash__mark_rodata_ro();
-
-       // mark_initmem_nx() should have already run by now
-       ptdump_check_wx();
 }
 
 void mark_initmem_nx(void)
index 2313053fe679ed34f12c75c738b809dc115529ab..620d4917ebe8a64687fbbc3fbad3290039201a31 100644 (file)
@@ -343,6 +343,9 @@ void ptdump_check_wx(void)
                }
        };
 
+       if (IS_ENABLED(CONFIG_PPC_BOOK3S_64) && !mmu_has_feature(MMU_FTR_KERNEL_RO))
+               return;
+
        ptdump_walk_pgd(&st.ptdump, &init_mm, NULL);
 
        if (st.wx_pages)
diff --git a/arch/riscv/include/asm/ptdump.h b/arch/riscv/include/asm/ptdump.h
deleted file mode 100644 (file)
index 3c9ea6d..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0 */
-/*
- * Copyright (C) 2019 SiFive
- */
-
-#ifndef _ASM_RISCV_PTDUMP_H
-#define _ASM_RISCV_PTDUMP_H
-
-void ptdump_check_wx(void);
-
-#ifdef CONFIG_DEBUG_WX
-static inline void debug_checkwx(void)
-{
-       ptdump_check_wx();
-}
-#else
-static inline void debug_checkwx(void)
-{
-}
-#endif
-
-#endif /* _ASM_RISCV_PTDUMP_H */
index fa34cf55037bd37ad0b8d3bb3b67f6f91d243f58..a4f218cfb845f66064aa110af883da31b9a3344c 100644 (file)
@@ -29,7 +29,6 @@
 #include <asm/io.h>
 #include <asm/numa.h>
 #include <asm/pgtable.h>
-#include <asm/ptdump.h>
 #include <asm/sections.h>
 #include <asm/soc.h>
 #include <asm/tlbflush.h>
@@ -723,8 +722,6 @@ void mark_rodata_ro(void)
        if (IS_ENABLED(CONFIG_64BIT))
                set_kernel_memory(lm_alias(__start_rodata), lm_alias(_data),
                                  set_memory_ro);
-
-       debug_checkwx();
 }
 #else
 static __init pgprot_t pgprot_from_va(uintptr_t va)
index 657c27bc07a7694edbb70795c4b2bd102b8780d0..0752656033136608a916be57246279de9a898a35 100644 (file)
@@ -9,7 +9,6 @@
 #include <linux/seq_file.h>
 #include <linux/ptdump.h>
 
-#include <asm/ptdump.h>
 #include <linux/pgtable.h>
 #include <asm/kasan.h>
 
diff --git a/arch/s390/include/asm/ptdump.h b/arch/s390/include/asm/ptdump.h
deleted file mode 100644 (file)
index f960b28..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0 */
-
-#ifndef _ASM_S390_PTDUMP_H
-#define _ASM_S390_PTDUMP_H
-
-void ptdump_check_wx(void);
-
-static inline void debug_checkwx(void)
-{
-       if (IS_ENABLED(CONFIG_DEBUG_WX))
-               ptdump_check_wx();
-}
-
-#endif /* _ASM_S390_PTDUMP_H */
index d37a8f607b71882557ca26e719b6cffa60700b97..8dcb4e0c71bde679d28126077611960742b70920 100644 (file)
@@ -6,7 +6,6 @@
 #include <linux/mm.h>
 #include <linux/kfence.h>
 #include <linux/kasan.h>
-#include <asm/ptdump.h>
 #include <asm/kasan.h>
 #include <asm/abs_lowcore.h>
 #include <asm/nospec-branch.h>
index 8d9a60ccb7771ae78dde34e54afba1dd661c1e2d..f6391442c0c2adba90d8e2ff0cdde30728dc5b19 100644 (file)
@@ -37,7 +37,6 @@
 #include <asm/pgalloc.h>
 #include <asm/ctlreg.h>
 #include <asm/kfence.h>
-#include <asm/ptdump.h>
 #include <asm/dma.h>
 #include <asm/abs_lowcore.h>
 #include <asm/tlb.h>
@@ -109,7 +108,6 @@ void mark_rodata_ro(void)
 
        __set_memory_ro(__start_ro_after_init, __end_ro_after_init);
        pr_info("Write protected read-only-after-init data: %luk\n", size >> 10);
-       debug_checkwx();
 }
 
 int set_memory_encrypted(unsigned long vaddr, int numpages)
index 9d077bca6a103ecfcbd02424483fd5971995bafd..6c979028e5212fdb49a0fb3f09946d7386789b0b 100644 (file)
@@ -32,6 +32,7 @@ void ptdump_walk_pgd_level(struct seq_file *m, struct mm_struct *mm);
 void ptdump_walk_pgd_level_debugfs(struct seq_file *m, struct mm_struct *mm,
                                   bool user);
 void ptdump_walk_pgd_level_checkwx(void);
+#define ptdump_check_wx ptdump_walk_pgd_level_checkwx
 void ptdump_walk_user_pgd_level_checkwx(void);
 
 /*
@@ -41,10 +42,8 @@ void ptdump_walk_user_pgd_level_checkwx(void);
 #define pgprot_decrypted(prot) __pgprot(cc_mkdec(pgprot_val(prot)))
 
 #ifdef CONFIG_DEBUG_WX
-#define debug_checkwx()                ptdump_walk_pgd_level_checkwx()
 #define debug_checkwx_user()   ptdump_walk_user_pgd_level_checkwx()
 #else
-#define debug_checkwx()                do { } while (0)
 #define debug_checkwx_user()   do { } while (0)
 #endif
 
index e1b599ecbbc26d02c2de09a589f899f0eebca01d..0008524eebe9af98552b1a2b4ee3f815bb4b1baf 100644 (file)
@@ -433,6 +433,9 @@ void ptdump_walk_user_pgd_level_checkwx(void)
 
 void ptdump_walk_pgd_level_checkwx(void)
 {
+       if (!(__supported_pte_mask & _PAGE_NX))
+               return;
+
        ptdump_walk_pgd_level_core(NULL, &init_mm, INIT_PGD, true, false);
 }
 
index b63403d7179df4b466d3c7b1b5ff36b654cc5fd1..5c736b707caea07bd41c1c904ee548b51399c1d2 100644 (file)
@@ -800,6 +800,4 @@ void mark_rodata_ro(void)
        set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
 #endif
        mark_nxdata_nx();
-       if (__supported_pte_mask & _PAGE_NX)
-               debug_checkwx();
 }
index a0dffaca6d2bfc2f1fdc47a8017f72fa74f98690..ebdbcae48011d4cfe6851906a675f7fa05d1892d 100644 (file)
@@ -1412,8 +1412,6 @@ void mark_rodata_ro(void)
                                (void *)text_end, (void *)rodata_start);
        free_kernel_image_pages("unused kernel image (rodata/data gap)",
                                (void *)rodata_end, (void *)_sdata);
-
-       debug_checkwx();
 }
 
 /*
index 2a3a955864259a53a95d988991e6fc2c502bb720..c10513739bf951182e099c6274364078fff6d411 100644 (file)
@@ -19,5 +19,12 @@ struct ptdump_state {
 };
 
 void ptdump_walk_pgd(struct ptdump_state *st, struct mm_struct *mm, pgd_t *pgd);
+void ptdump_check_wx(void);
+
+static inline void debug_checkwx(void)
+{
+       if (IS_ENABLED(CONFIG_DEBUG_WX))
+               ptdump_check_wx();
+}
 
 #endif /* _LINUX_PTDUMP_H */
index e24b0780fdff7a807bd027ab26e61fc303c624ef..749a9f8d2c9b0d78d0f7fe0a7f392c4c67ea161d 100644 (file)
@@ -99,6 +99,7 @@
 #include <linux/init_syscalls.h>
 #include <linux/stackdepot.h>
 #include <linux/randomize_kstack.h>
+#include <linux/ptdump.h>
 #include <net/net_namespace.h>
 
 #include <asm/io.h>
@@ -1408,6 +1409,7 @@ static void mark_readonly(void)
                 */
                rcu_barrier();
                mark_rodata_ro();
+               debug_checkwx();
                rodata_test();
        } else
                pr_info("Kernel memory protection disabled.\n");