KASAN support for 64-bit Book 3S powerpc
Subsystems (5)
PATCH v2 5/6 powerpc/kasan: Disable address sanitization in kexec paths
In reply to Paul MackerrasFrom: Daniel Axtens <[email protected]>
The kexec code paths involve code that necessarily run in real mode, as CPUs are disabled and control is transferred to the new kernel. Disable address sanitization for the kexec code and the functions called in real mode on CPUs being disabled.
[[email protected]: combined a few work-in-progress commits of
Daniel's and wrote the commit message.]
--- arch/powerpc/kexec/Makefile | 2 ++ arch/powerpc/platforms/pseries/Makefile | 3 +++ arch/powerpc/sysdev/xics/xics-common.c | 4 ++-- arch/powerpc/sysdev/xive/common.c | 4 ++-- 4 files changed, 9 insertions(+), 4 deletions(-)
arch/powerpc/kexec/Makefile +2 -0
@@ -13,3 +13,5 @@ obj-$(CONFIG_KEXEC_FILE) += file_load.o ranges.o file_load_$(BITS).o elf_$(BITS) GCOV_PROFILE_core_$(BITS).o := n KCOV_INSTRUMENT_core_$(BITS).o := n UBSAN_SANITIZE_core_$(BITS).o := n +KASAN_SANITIZE_core.o := n +KASAN_SANITIZE_core_$(BITS) := n
arch/powerpc/platforms/pseries/Makefile +3 -0
@@ -35,3 +35,6 @@ obj-$(CONFIG_ARCH_HAS_CC_PLATFORM) += cc_platform.o # nothing that operates in real mode is safe for KASAN KASAN_SANITIZE_ras.o := n +KASAN_SANITIZE_kexec.o := n +#machine_kexec +KASAN_SANITIZE_setup.o := n
arch/powerpc/sysdev/xics/xics-common.c +2 -2
@@ -146,7 +146,7 @@ void __init xics_smp_probe(void)#endif /* CONFIG_SMP */ -void xics_teardown_cpu (void ) +noinstr void xics_teardown_cpu(void) {struct xics_cppr *os_cppr =this_cpu_ptr (&xics_cppr);
@@ -159,7 +159,7 @@ void xics_teardown_cpu(void) icp_ops->teardown_cpu (); } -void xics_kexec_teardown_cpu (int secondary) +noinstr void xics_kexec_teardown_cpu(int secondary) {xics_teardown_cpu ();
arch/powerpc/sysdev/xive/common.c +2 -2
@@ -1241,7 +1241,7 @@ static int xive_setup_cpu_ipi(unsigned int cpu)return 0 ; } -static void xive_cleanup_cpu_ipi (unsigned int cpu,struct xive_cpu *xc) +noinstr static void xive_cleanup_cpu_ipi(unsigned int cpu, struct xive_cpu *xc) {unsigned int xive_ipi_irq =xive_ipi_cpu_to_irq (cpu);
@@ -1634,7 +1634,7 @@ void xive_flush_interrupt(void)#endif /* CONFIG_SMP */ -void xive_teardown_cpu (void ) +noinstr void xive_teardown_cpu(void) {struct xive_cpu *xc =__this_cpu_read (xive_cpu);unsigned int cpu =smp_processor_id ();
Signature · 1 line
2.35.3
PATCH v2 4/6 powerpc/kasan: Don't instrument non-maskable or raw interrupts
In reply to Paul MackerrasFrom: Daniel Axtens <[email protected]>
Disable address sanitization for raw and non-maskable interrupt handlers, because they can run in real mode, where we cannot access the shadow memory. (Note that kasan_arch_is_ready() doesn't test for real mode, since it is a static branch for speed, and in any case not all the entry points to the generic KASAN code are protected by kasan_arch_is_ready guards.)
The changes to interrupt_nmi_enter/exit_prepare() look larger than they actually are. The changes are equivalent to adding !IS_ENABLED(CONFIG_KASAN) to the conditions for calling nmi_enter() or nmi_exit() in real mode. That is, the code is equivalent to using the following condition for calling nmi_enter/exit:
if (((!IS_ENABLED(CONFIG_PPC_BOOK3S_64) || !firmware_has_feature(FW_FEATURE_LPAR) || radix_enabled()) && !IS_ENABLED(CONFIG_KASAN) || (mfmsr() & MSR_DR))
That unwieldy condition has been split into several statements with comments, for easier reading.
The nmi_ipi_lock functions that call atomic functions (i.e., nmi_ipi_lock_start(), nmi_ipi_lock() and nmi_ipi_unlock()), besides being marked noinstr, now call arch_atomic_* functions instead of atomic_* functions because with KASAN enabled, the atomic_* functions are wrappers which explicitly do address sanitization on their arguments. Since we are trying to avoid address sanitization, we have to use the lower-level arch_atomic_* versions.
In hv_nmi_check_nonrecoverable(), the regs_set_unrecoverable() call has been open-coded so as to avoid having to either trust the inlining or mark regs_set_unrecoverable() as noinstr.
[[email protected]: combined a few work-in-progress commits of
Daniel's and wrote the commit message.]
--- arch/powerpc/include/asm/interrupt.h | 52 +++++++++++++++++++++------- arch/powerpc/kernel/smp.c | 22 ++++++------ arch/powerpc/kernel/traps.c | 6 ++-- arch/powerpc/lib/Makefile | 3 ++ arch/powerpc/platforms/powernv/smp.c | 2 +- 5 files changed, 59 insertions(+), 26 deletions(-)
arch/powerpc/include/asm/interrupt.h +40 -12
@@ -327,22 +327,46 @@ static inline void interrupt_nmi_enter_prepare(struct pt_regs *regs, struct inte }#endif +/* If data relocations are enabled, it's safe to use nmi_enter() */ +if (mfmsr () &MSR_DR ) { +nmi_enter (); +return ; + } +/* -* Do not use nmi_enter() for pseries hash guest taking a real-mode +* But do not use nmi_enter() for pseries hash guest taking a real-mode * NMI because not everything it touches is within the RMA limit. */ -if (!IS_ENABLED (CONFIG_PPC_BOOK3S_64 ) || - !firmware_has_feature (FW_FEATURE_LPAR ) || -radix_enabled () || (mfmsr () &MSR_DR )) -nmi_enter (); +if (IS_ENABLED (CONFIG_PPC_BOOK3S_64 ) && +firmware_has_feature (FW_FEATURE_LPAR ) && + !radix_enabled ()) +return ; + +/* +* Likewise, don't use it if we have some form of instrumentation (like +* KASAN shadow) that is not safe to access in real mode (even on radix) +*/ +if (IS_ENABLED (CONFIG_KASAN )) +return ; + +/* Otherwise, it should be safe to call it */ +nmi_enter (); }static inline void interrupt_nmi_exit_prepare (struct pt_regs *regs,struct interrupt_nmi_state *state) { -if (!IS_ENABLED (CONFIG_PPC_BOOK3S_64 ) || - !firmware_has_feature (FW_FEATURE_LPAR ) || -radix_enabled () || (mfmsr () &MSR_DR )) +if (mfmsr () &MSR_DR ) { +// nmi_exit if relocations are on nmi_exit (); + }else if (IS_ENABLED (CONFIG_PPC_BOOK3S_64 ) && +firmware_has_feature (FW_FEATURE_LPAR ) && + !radix_enabled ()) { +// no nmi_exit for a pseries hash guest taking a real mode exception + }else if (IS_ENABLED (CONFIG_KASAN )) { +// no nmi_exit for KASAN in real mode + }else { +nmi_exit (); + }/* * nmi does not call nap_adjust_return because nmi should not create
@@ -410,7 +434,8 @@ static inline void interrupt_nmi_exit_prepare(struct pt_regs *regs, struct inter* Specific handlers may have additional restrictions. */ #define DEFINE_INTERRUPT_HANDLER_RAW (func) \ -static __always_inline long ____##func(struct pt_regs *regs); \ +static __always_inline __no_sanitize_address __no_kcsan long \ +____##func(struct pt_regs *regs); \ \ interrupt_handler long func(struct pt_regs *regs) \ { \
@@ -424,7 +449,8 @@ interrupt_handler long func(struct pt_regs *regs) \ } \NOKPROBE_SYMBOL (func); \ \ -static __always_inline long ____##func(struct pt_regs *regs) +static __always_inline __no_sanitize_address __no_kcsan long \ +____##func(struct pt_regs *regs)/** * DECLARE_INTERRUPT_HANDLER - Declare synchronous interrupt handler function
@@ -549,7 +575,8 @@ static __always_inline void ____##func(struct pt_regs *regs)* body with a pair of curly brackets. */ #define DEFINE_INTERRUPT_HANDLER_NMI (func) \ -static __always_inline long ____##func(struct pt_regs *regs); \ +static __always_inline __no_sanitize_address __no_kcsan long \ +____##func(struct pt_regs *regs); \ \ interrupt_handler long func(struct pt_regs *regs) \ { \
@@ -566,7 +593,8 @@ interrupt_handler long func(struct pt_regs *regs) \ } \NOKPROBE_SYMBOL (func); \ \ -static __always_inline long ____##func(struct pt_regs *regs) +static __always_inline __no_sanitize_address __no_kcsan long \ +____##func(struct pt_regs *regs)/* Interrupt handlers */
arch/powerpc/kernel/smp.c +11 -11
@@ -413,32 +413,32 @@ static struct cpumask nmi_ipi_pending_mask;static bool nmi_ipi_busy = false;static void (*nmi_ipi_function)(struct pt_regs *) =NULL ; -static void nmi_ipi_lock_start (unsigned long *flags) +noinstr static void nmi_ipi_lock_start(unsigned long *flags) {raw_local_irq_save (*flags);hard_irq_disable (); -while (atomic_cmpxchg (&__nmi_ipi_lock,0 ,1 ) ==1 ) { +while (arch_atomic_cmpxchg (&__nmi_ipi_lock,0 ,1 ) ==1 ) {raw_local_irq_restore (*flags); -spin_until_cond (atomic_read (&__nmi_ipi_lock) ==0 ); +spin_until_cond (arch_atomic_read (&__nmi_ipi_lock) ==0 );raw_local_irq_save (*flags);hard_irq_disable (); } } -static void nmi_ipi_lock (void ) +noinstr static void nmi_ipi_lock(void) { -while (atomic_cmpxchg (&__nmi_ipi_lock,0 ,1 ) ==1 ) -spin_until_cond (atomic_read (&__nmi_ipi_lock) ==0 ); +while (arch_atomic_cmpxchg (&__nmi_ipi_lock,0 ,1 ) ==1 ) +spin_until_cond (arch_atomic_read (&__nmi_ipi_lock) ==0 ); } -static void nmi_ipi_unlock (void ) +noinstr static void nmi_ipi_unlock(void) {smp_mb (); -WARN_ON (atomic_read (&__nmi_ipi_lock) !=1 ); -atomic_set (&__nmi_ipi_lock,0 ); +WARN_ON (arch_atomic_read (&__nmi_ipi_lock) !=1 ); +arch_atomic_set (&__nmi_ipi_lock,0 ); } -static void nmi_ipi_unlock_end (unsigned long *flags) +noinstr static void nmi_ipi_unlock_end(unsigned long *flags) {nmi_ipi_unlock ();raw_local_irq_restore (*flags);
@@ -447,7 +447,7 @@ static void nmi_ipi_unlock_end(unsigned long *flags)/* * Platform NMI handler calls this to ack */ -int smp_handle_nmi_ipi (struct pt_regs *regs) +noinstr int smp_handle_nmi_ipi(struct pt_regs *regs) {void (*fn)(struct pt_regs *) =NULL ;unsigned long flags;
arch/powerpc/kernel/traps.c +4 -2
@@ -393,7 +393,7 @@ void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)* Builds that do not support KVM could take this second option to increase * the recoverability of NMIs. */ -void hv_nmi_check_nonrecoverable (struct pt_regs *regs) +noinstr void hv_nmi_check_nonrecoverable(struct pt_regs *regs) {#ifdef CONFIG_PPC_POWERNV unsigned long kbase = (unsigned long )_stext;
@@ -433,7 +433,9 @@ void hv_nmi_check_nonrecoverable(struct pt_regs *regs)return ; nonrecoverable: -regs_set_unrecoverable (regs); + regs->msr &= ~MSR_RI ; + local_paca->hsrr_valid =0 ; + local_paca->srr_valid =0 ;#endif }DEFINE_INTERRUPT_HANDLER_NMI (system_reset_exception)
arch/powerpc/lib/Makefile +3 -0
@@ -13,6 +13,9 @@ CFLAGS_REMOVE_feature-fixups.o = $(CC_FLAGS_FTRACE) KASAN_SANITIZE_code-patching.o := n KASAN_SANITIZE_feature-fixups.o := n +# restart_table.o contains functions called in the NMI interrupt path +# which can be in real mode. Disable KASAN. +KASAN_SANITIZE_restart_table.o := n ifdef CONFIG_KASAN CFLAGS_code-patching.o += -DDISABLE_BRANCH_PROFILING
arch/powerpc/platforms/powernv/smp.c +1 -1
@@ -345,7 +345,7 @@ static void __init pnv_smp_probe(void) } } -static int pnv_system_reset_exception (struct pt_regs *regs) +noinstr static int pnv_system_reset_exception(struct pt_regs *regs) {if (smp_handle_nmi_ipi (regs))return 1 ;
Signature · 1 line
2.35.3
v2 cover KASAN support for 64-bit Book 3S powerpc
This patch series implements KASAN on 64-bit POWER with radix MMU, such as POWER9 or POWER10. Daniel Axtens posted previous versions of these patches, but is no longer working on KASAN, and I have been asked to get them ready for inclusion.
Because of various technical difficulties, mostly around the need to allow for code that runs in real mode, we only support "outline" mode (as opposed to "inline" mode), where the compiler adds a call to a checking procedure before every store to memory.
This series has known deficiencies, specifically that the kernel will crash on boot on a HPT system, and that out-of-bounds accesses to module global data are not caught (which leads to one of the KASAN tests failing).
v2: Split the large patch 3/3 of the previous series into three patches and addressed review comments; put the generic documentation changes in a separate patch at the end of the series; removed the RFC tag.
Comments welcome.
Paul.
Documentation/dev-tools/kasan.rst | 7 +-
Documentation/powerpc/kasan.txt | 58 ++++++++++++
arch/powerpc/Kconfig | 5 +-
arch/powerpc/Kconfig.debug | 3 +-
arch/powerpc/include/asm/book3s/64/hash.h | 4 +
arch/powerpc/include/asm/book3s/64/pgtable.h | 3 +
arch/powerpc/include/asm/book3s/64/radix.h | 12 ++-
arch/powerpc/include/asm/interrupt.h | 52 ++++++++---
arch/powerpc/include/asm/kasan.h | 22 +++++
arch/powerpc/kernel/Makefile | 11 +++
arch/powerpc/kernel/smp.c | 22 ++---
arch/powerpc/kernel/traps.c | 6 +-
arch/powerpc/kexec/Makefile | 2 +
arch/powerpc/kvm/Makefile | 5 +
arch/powerpc/lib/Makefile | 3 +
arch/powerpc/mm/book3s64/Makefile | 9 ++
arch/powerpc/mm/kasan/Makefile | 3 +-
.../mm/kasan/{kasan_init_32.c => init_32.c} | 0
arch/powerpc/mm/kasan/init_book3s_64.c | 103 +++++++++++++++++++++
arch/powerpc/mm/ptdump/ptdump.c | 3 +-
arch/powerpc/platforms/Kconfig.cputype | 1 +
arch/powerpc/platforms/powernv/Makefile | 8 ++
arch/powerpc/platforms/powernv/smp.c | 2 +-
arch/powerpc/platforms/pseries/Makefile | 6 ++
arch/powerpc/sysdev/xics/xics-common.c | 4 +-
arch/powerpc/sysdev/xive/common.c | 4 +-
26 files changed, 320 insertions(+), 38 deletions(-)PATCH v2 6/6 Documentation/kasan: Update details of KASAN on powerpc
From: Daniel Axtens <[email protected]>
--- Documentation/dev-tools/kasan.rst | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
Documentation/dev-tools/kasan.rst +6 -1
@@ -36,7 +36,9 @@ Both software KASAN modes work with SLUB and SLAB memory allocators, while the hardware tag-based KASAN currently only supports SLUB. Currently, generic KASAN is supported for the x86_64, arm, arm64, xtensa, s390, -and riscv architectures, and tag-based KASAN modes are supported only for arm64. +and riscv architectures. It is also supported on powerpc for 32-bit kernels and +for 64-bit kernels running under the Radix MMU. Tag-based KASAN modes are +supported only for arm64. Usage -----
@@ -351,6 +353,9 @@ With ``CONFIG_KASAN_VMALLOC``, KASAN can cover vmalloc space at the cost of greater memory usage. Currently, this is supported on x86, riscv, s390, and powerpc. +It is optional, except on 64-bit powerpc kernels, and on 32-bit +powerpc kernels with module support, where it is required. + This works by hooking into vmalloc and vmap and dynamically allocating real shadow memory to back the mappings.
Signature · 1 line
2.35.3
PATCH v2 2/6 powerpc/mm/kasan: rename kasan_init_32.c to init_32.c
In reply to Paul MackerrasFrom: Daniel Axtens <[email protected]>
kasan is already implied by the directory name, we don't need to repeat it.
---
arch/powerpc/mm/kasan/Makefile | 2 +-
arch/powerpc/mm/kasan/{kasan_init_32.c => init_32.c} | 0
2 files changed, 1 insertion(+), 1 deletion(-)
rename arch/powerpc/mm/kasan/{kasan_init_32.c => init_32.c} (100%)arch/powerpc/mm/kasan/Makefile +1 -1
@@ -2,6 +2,6 @@ KASAN_SANITIZE := n -obj-$(CONFIG_PPC32) += kasan_init_32.o +obj-$(CONFIG_PPC32) += init_32.o obj-$(CONFIG_PPC_8xx) += 8xx.o obj-$(CONFIG_PPC_BOOK3S_32) += book3s_32.o
arch/powerpc/mm/kasan/init_32.c renamed from arch/powerpc/mm/kasan/kasan_init_32.c +0 -0
Signature · 1 line
2.35.3
PATCH v2 1/6 kasan: Document support on 32-bit powerpc
In reply to Paul MackerrasFrom: Daniel Axtens <[email protected]>
KASAN is supported on 32-bit powerpc and the docs should reflect this.
--- Documentation/powerpc/kasan.txt | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 Documentation/powerpc/kasan.txt
Documentation/powerpc/kasan.txt new file +12 -0
@@ -0,0 +1,12 @@ +KASAN is supported on powerpc on 32-bit only. + +32 bit support +============== + +KASAN is supported on both hash and nohash MMUs on 32-bit. + +The shadow area sits at the top of the kernel virtual memory space above the +fixmap area and occupies one eighth of the total kernel virtual memory space. + +Instrumentation of the vmalloc area is optional, unless built with modules, +in which case it is required.
Signature · 1 line
2.35.3
PATCH v2 3/6 powerpc: Book3S 64-bit outline-only KASAN support
In reply to Paul MackerrasFrom: Daniel Axtens <[email protected]>
Implement a limited form of KASAN for Book3S 64-bit machines running under the Radix MMU, supporting only outline mode.
- Enable the compiler instrumentation to check addresses and maintain the shadow region. (This is the guts of KASAN which we can easily reuse.)
- Require kasan-vmalloc support to handle modules and anything else in vmalloc space.
- KASAN needs to be able to validate all pointer accesses, but we can't instrument all kernel addresses - only linear map and vmalloc. On boot, set up a single page of read-only shadow that marks all iomap and vmemmap accesses as valid.
- Document KASAN in powerpc docs.
Background
----------
KASAN support on Book3S is a bit tricky to get right:
- It would be good to support inline instrumentation so as to be able to catch stack issues that cannot be caught with outline mode.
- Inline instrumentation requires a fixed offset.
- Book3S runs code with translations off ("real mode") during boot, including a lot of generic device-tree parsing code which is used to determine MMU features.
[ppc64 mm note: The kernel installs a linear mapping at effective address c000...-c008.... This is a one-to-one mapping with physical memory from 0000... onward. Because of how memory accesses work on powerpc 64-bit Book3S, a kernel pointer in the linear map accesses the same memory both with translations on (accessing as an 'effective address'), and with translations off (accessing as a 'real address'). This works in both guests and the hypervisor. For more details, see s5.7 of Book III of version 3 of the ISA, in particular the Storage Control Overview, s5.7.3, and s5.7.5 - noting that this KASAN implementation currently only supports Radix.]
- Some code - most notably a lot of KVM code - also runs with translations off after boot.
- Therefore any offset has to point to memory that is valid with translations on or off.
One approach is just to give up on inline instrumentation. This way boot-time checks can be delayed until after the MMU is set is up, and we can just not instrument any code that runs with translations off after booting. Take this approach for now and require outline instrumentation.
Previous attempts allowed inline instrumentation. However, they came with some unfortunate restrictions: only physically contiguous memory could be used and it had to be specified at compile time. Maybe we can do better in the future.
[[email protected] - Rebased onto 5.17. Note that a kernel with
CONFIG_KASAN=y will crash during boot on a machine using HPT translation because not all the entry points to the generic KASAN code are protected with a call to kasan_arch_is_ready().]
--- Documentation/powerpc/kasan.txt | 48 ++++++++- arch/powerpc/Kconfig | 5 +- arch/powerpc/Kconfig.debug | 3 +- arch/powerpc/include/asm/book3s/64/hash.h | 4 + arch/powerpc/include/asm/book3s/64/pgtable.h | 3 + arch/powerpc/include/asm/book3s/64/radix.h | 12 ++- arch/powerpc/include/asm/kasan.h | 22 ++++ arch/powerpc/kernel/Makefile | 11 ++ arch/powerpc/kvm/Makefile | 5 + arch/powerpc/mm/book3s64/Makefile | 9 ++ arch/powerpc/mm/kasan/Makefile | 1 + arch/powerpc/mm/kasan/init_book3s_64.c | 103 +++++++++++++++++++ arch/powerpc/mm/ptdump/ptdump.c | 3 +- arch/powerpc/platforms/Kconfig.cputype | 1 + arch/powerpc/platforms/powernv/Makefile | 8 ++ arch/powerpc/platforms/pseries/Makefile | 3 + 16 files changed, 234 insertions(+), 7 deletions(-) create mode 100644 arch/powerpc/mm/kasan/init_book3s_64.c
Documentation/powerpc/kasan.txt +47 -1
@@ -1,4 +1,4 @@ -KASAN is supported on powerpc on 32-bit only. +KASAN is supported on powerpc on 32-bit and Radix 64-bit only. 32 bit support ==============
@@ -10,3 +10,49 @@ fixmap area and occupies one eighth of the total kernel virtual memory space. Instrumentation of the vmalloc area is optional, unless built with modules, in which case it is required. + +64 bit support +============== + +Currently, only the radix MMU is supported. There have been versions for hash +and Book3E processors floating around on the mailing list, but nothing has been +merged. + +KASAN support on Book3S is a bit tricky to get right: + + - It would be good to support inline instrumentation so as to be able to catch + stack issues that cannot be caught with outline mode. + + - Inline instrumentation requires a fixed offset. + + - Book3S runs code with translations off ("real mode") during boot, including a + lot of generic device-tree parsing code which is used to determine MMU + features. + + - Some code - most notably a lot of KVM code - also runs with translations off + after boot. + + - Therefore any offset has to point to memory that is valid with + translations on or off. + +One approach is just to give up on inline instrumentation. This way boot-time +checks can be delayed until after the MMU is set is up, and we can just not +instrument any code that runs with translations off after booting. This is the +current approach. + +To avoid this limitiation, the KASAN shadow would have to be placed inside the +linear mapping, using the same high-bits trick we use for the rest of the linear +mapping. This is tricky: + + - We'd like to place it near the start of physical memory. In theory we can do + this at run-time based on how much physical memory we have, but this requires + being able to arbitrarily relocate the kernel, which is basically the tricky + part of KASLR. Not being game to implement both tricky things at once, this + is hopefully something we can revisit once we get KASLR for Book3S. + + - Alternatively, we can place the shadow at the _end_ of memory, but this + requires knowing how much contiguous physical memory a system has _at compile + time_. This is a big hammer, and has some unfortunate consequences: inablity + to handle discontiguous physical memory, total failure to boot on machines + with less memory than specified, and that machines with more memory than + specified can't use it. This was deemed unacceptable.
arch/powerpc/Kconfig +4 -1
@@ -105,6 +105,7 @@ config PPC # Please keep this list sorted alphabetically. # select ARCH_32BIT_OFF_T if PPC32 + select ARCH_DISABLE_KASAN_INLINE if PPC_RADIX_MMU select ARCH_ENABLE_MEMORY_HOTPLUG select ARCH_ENABLE_MEMORY_HOTREMOVE select ARCH_HAS_COPY_MC if PPC64
@@ -152,6 +153,7 @@ config PPC select ARCH_WANT_IPC_PARSE_VERSION select ARCH_WANT_IRQS_OFF_ACTIVATE_MM select ARCH_WANT_LD_ORPHAN_WARN + select ARCH_WANTS_NO_INSTR select ARCH_WEAK_RELEASE_ACQUIRE select BINFMT_ELF select BUILDTIME_TABLE_SORT
@@ -183,7 +185,8 @@ config PPC select HAVE_ARCH_JUMP_LABEL select HAVE_ARCH_JUMP_LABEL_RELATIVE select HAVE_ARCH_KASAN if PPC32 && PPC_PAGE_SHIFT <= 14 - select HAVE_ARCH_KASAN_VMALLOC if PPC32 && PPC_PAGE_SHIFT <= 14 + select HAVE_ARCH_KASAN if PPC_RADIX_MMU + select HAVE_ARCH_KASAN_VMALLOC if HAVE_ARCH_KASAN select HAVE_ARCH_KFENCE if PPC_BOOK3S_32 || PPC_8xx || 40x select HAVE_ARCH_KGDB select HAVE_ARCH_MMAP_RND_BITS
arch/powerpc/Kconfig.debug +2 -1
@@ -374,4 +374,5 @@ config PPC_FAST_ENDIAN_SWITCH config KASAN_SHADOW_OFFSET hex depends on KASAN - default 0xe0000000 + default 0xe0000000 if PPC32 + default 0xa80e000000000000 if PPC64
arch/powerpc/include/asm/book3s/64/hash.h +4 -0
@@ -18,6 +18,10 @@#include <asm/book3s/64/hash-4k.h> #endif +#define H_PTRS_PER_PTE (1 << H_PTE_INDEX_SIZE) +#define H_PTRS_PER_PMD (1 << H_PMD_INDEX_SIZE) +#define H_PTRS_PER_PUD (1 << H_PUD_INDEX_SIZE) +/* Bits to set in a PMD/PUD/PGD entry valid bit*/ #define HASH_PMD_VAL_BITS (0x8000000000000000UL)#define HASH_PUD_VAL_BITS (0x8000000000000000UL)
arch/powerpc/include/asm/book3s/64/pgtable.h +3 -0
@@ -232,6 +232,9 @@ extern unsigned long __pmd_frag_size_shift;#define PTRS_PER_PUD (1 << PUD_INDEX_SIZE)#define PTRS_PER_PGD (1 << PGD_INDEX_SIZE) +#define MAX_PTRS_PER_PTE ((H_PTRS_PER_PTE > R_PTRS_PER_PTE) ? H_PTRS_PER_PTE : R_PTRS_PER_PTE) +#define MAX_PTRS_PER_PMD ((H_PTRS_PER_PMD > R_PTRS_PER_PMD) ? H_PTRS_PER_PMD : R_PTRS_PER_PMD) +#define MAX_PTRS_PER_PUD ((H_PTRS_PER_PUD > R_PTRS_PER_PUD) ? H_PTRS_PER_PUD : R_PTRS_PER_PUD)#define MAX_PTRS_PER_PGD (1 << (H_PGD_INDEX_SIZE > RADIX_PGD_INDEX_SIZE ? \ H_PGD_INDEX_SIZE : RADIX_PGD_INDEX_SIZE))
arch/powerpc/include/asm/book3s/64/radix.h +9 -3
@@ -35,6 +35,11 @@#define RADIX_PMD_SHIFT (PAGE_SHIFT + RADIX_PTE_INDEX_SIZE)#define RADIX_PUD_SHIFT (RADIX_PMD_SHIFT + RADIX_PMD_INDEX_SIZE)#define RADIX_PGD_SHIFT (RADIX_PUD_SHIFT + RADIX_PUD_INDEX_SIZE) + +#define R_PTRS_PER_PTE (1 << RADIX_PTE_INDEX_SIZE) +#define R_PTRS_PER_PMD (1 << RADIX_PMD_INDEX_SIZE) +#define R_PTRS_PER_PUD (1 << RADIX_PUD_INDEX_SIZE) +/* * Size of EA range mapped by our pagetables. */
@@ -68,11 +73,11 @@* * * 3rd quadrant expanded: -* +------------------------------+ -* | | +* +------------------------------+ Highest address (0xc010000000000000) +* +------------------------------+ KASAN shadow end (0xc00fc00000000000) * | | * | | -* +------------------------------+ Kernel vmemmap end (0xc010000000000000) +* +------------------------------+ Kernel vmemmap end/shadow start (0xc00e000000000000) * | | * | 512TB | * | |
@@ -91,6 +96,7 @@* +------------------------------+ Kernel linear (0xc.....) */ +/* For the sizes of the shadow area, see kasan.h */ /* * If we store section details in page->flags we can't increase the MAX_PHYSMEM_BITS
arch/powerpc/include/asm/kasan.h +22 -0
@@ -30,9 +30,31 @@#define KASAN_SHADOW_OFFSET ASM_CONST(CONFIG_KASAN_SHADOW_OFFSET) +#ifdef CONFIG_PPC32 #define KASAN_SHADOW_END (-(-KASAN_SHADOW_START >> KASAN_SHADOW_SCALE_SHIFT)) +#elif defined(CONFIG_PPC_BOOK3S_64 ) +/* +* The shadow ends before the highest accessible address +* because we don't need a shadow for the shadow. Instead: +* c00e000000000000 << 3 + a80e000000000000 = c00fc00000000000 +*/ +#define KASAN_SHADOW_END 0xc00fc00000000000UL +#endif #ifdef CONFIG_KASAN +#ifdef CONFIG_PPC_BOOK3S_64 +DECLARE_STATIC_KEY_FALSE (powerpc_kasan_enabled_key); + +static __always_inline bool kasan_arch_is_ready(void) +{ +if (static_branch_likely (&powerpc_kasan_enabled_key)) +return true; +return false; +} + +#define kasan_arch_is_ready kasan_arch_is_ready +#endif +void kasan_early_init (void );void kasan_mmu_init (void );void kasan_init (void );
arch/powerpc/kernel/Makefile +11 -0
@@ -33,6 +33,17 @@ KASAN_SANITIZE_early_32.o := n KASAN_SANITIZE_cputable.o := n KASAN_SANITIZE_prom_init.o := n KASAN_SANITIZE_btext.o := n +KASAN_SANITIZE_paca.o := n +KASAN_SANITIZE_setup_64.o := n +KASAN_SANITIZE_mce.o := n +KASAN_SANITIZE_mce_power.o := n + +# we have to be particularly careful in ppc64 to exclude code that +# runs with translations off, as we cannot access the shadow with +# translations off. However, ppc32 can sanitize this. +ifdef CONFIG_PPC64 +KASAN_SANITIZE_traps.o := n +endif ifdef CONFIG_KASAN CFLAGS_early_32.o += -DDISABLE_BRANCH_PROFILING
arch/powerpc/kvm/Makefile +5 -0
@@ -134,3 +134,8 @@ obj-$(CONFIG_KVM_BOOK3S_64_PR) += kvm-pr.o obj-$(CONFIG_KVM_BOOK3S_64_HV) += kvm-hv.o obj-y += $(kvm-book3s_64-builtin-objs-y) + +# KVM does a lot in real-mode, and 64-bit Book3S KASAN doesn't support that +ifdef CONFIG_PPC_BOOK3S_64 +KASAN_SANITIZE := n +endif
arch/powerpc/mm/book3s64/Makefile +9 -0
@@ -24,3 +24,12 @@ obj-$(CONFIG_PPC_PKEY) += pkeys.o # Instrumenting the SLB fault path can lead to duplicate SLB entries KCOV_INSTRUMENT_slb.o := n + +# Parts of these can run in real mode and therefore are +# not safe with the current outline KASAN implementation +KASAN_SANITIZE_mmu_context.o := n +KASAN_SANITIZE_pgtable.o := n +KASAN_SANITIZE_radix_pgtable.o := n +KASAN_SANITIZE_radix_tlb.o := n +KASAN_SANITIZE_slb.o := n +KASAN_SANITIZE_pkeys.o := n
arch/powerpc/mm/kasan/Makefile +1 -0
@@ -5,3 +5,4 @@ KASAN_SANITIZE := n obj-$(CONFIG_PPC32) += init_32.o obj-$(CONFIG_PPC_8xx) += 8xx.o obj-$(CONFIG_PPC_BOOK3S_32) += book3s_32.o +obj-$(CONFIG_PPC_BOOK3S_64) += init_book3s_64.o
arch/powerpc/mm/kasan/init_book3s_64.c new file +103 -0
103 lines not on this page · raw message
arch/powerpc/mm/ptdump/ptdump.c +2 -1
@@ -21,6 +21,7 @@#include <linux/seq_file.h> #include <asm/fixmap.h> #include <linux/const.h> +#include <linux/kasan.h> #include <asm/page.h> #include <asm/hugetlb.h>
@@ -289,11 +290,11 @@ static void populate_markers(void)#endif address_markers[i++].start_address =FIXADDR_START ; address_markers[i++].start_address =FIXADDR_TOP ; +#endif /* CONFIG_PPC64 */ #ifdef CONFIG_KASAN address_markers[i++].start_address =KASAN_SHADOW_START ; address_markers[i++].start_address =KASAN_SHADOW_END ;#endif -#endif /* CONFIG_PPC64 */ }static int ptdump_show (struct seq_file *m,void *v)
arch/powerpc/platforms/Kconfig.cputype +1 -0
@@ -104,6 +104,7 @@ config PPC_BOOK3S_64 select HAVE_MOVE_PUD select IRQ_WORK select PPC_64S_HASH_MMU if !PPC_RADIX_MMU + select KASAN_VMALLOC if KASAN config PPC_BOOK3E_64 bool "Embedded processors"
arch/powerpc/platforms/powernv/Makefile +8 -0
@@ -1,4 +1,12 @@ # SPDX-License-Identifier: GPL-2.0 + +# nothing that deals with real mode is safe to KASAN +# in particular, idle code runs a bunch of things in real mode +KASAN_SANITIZE_idle.o := n +KASAN_SANITIZE_pci-ioda.o := n +# pnv_machine_check_early +KASAN_SANITIZE_setup.o := n + obj-y += setup.o opal-call.o opal-wrappers.o opal.o opal-async.o obj-y += idle.o opal-rtc.o opal-nvram.o opal-lpc.o opal-flash.o obj-y += rng.o opal-elog.o opal-dump.o opal-sysparam.o opal-sensor.o
arch/powerpc/platforms/pseries/Makefile +3 -0
@@ -32,3 +32,6 @@ obj-$(CONFIG_SUSPEND) += suspend.o obj-$(CONFIG_PPC_VAS) += vas.o obj-$(CONFIG_ARCH_HAS_CC_PLATFORM) += cc_platform.o + +# nothing that operates in real mode is safe for KASAN +KASAN_SANITIZE_ras.o := n
Signature · 1 line
2.35.3
Le 18/05/2022 à 12:03, Paul Mackerras a écrit :
Quoted 15 lines
This patch series implements KASAN on 64-bit POWER with radix MMU, such as POWER9 or POWER10. Daniel Axtens posted previous versions of these patches, but is no longer working on KASAN, and I have been asked to get them ready for inclusion.
Because of various technical difficulties, mostly around the need to allow for code that runs in real mode, we only support "outline" mode (as opposed to "inline" mode), where the compiler adds a call to a checking procedure before every store to memory.
This series has known deficiencies, specifically that the kernel will crash on boot on a HPT system, and that out-of-bounds accesses to module global data are not caught (which leads to one of the KASAN tests failing).
Quoted 6 lines
v2: Split the large patch 3/3 of the previous series into three patches and addressed review comments; put the generic documentation changes in a separate patch at the end of the series; removed the RFC tag.
Comments welcome.
The series looks good to me.
Maybe patch 3 should go after patches 4 and 5 which are preparation patches.
Christophe
Quoted 30 lines
Paul.
Documentation/dev-tools/kasan.rst | 7 +- Documentation/powerpc/kasan.txt | 58 ++++++++++++ arch/powerpc/Kconfig | 5 +- arch/powerpc/Kconfig.debug | 3 +- arch/powerpc/include/asm/book3s/64/hash.h | 4 + arch/powerpc/include/asm/book3s/64/pgtable.h | 3 + arch/powerpc/include/asm/book3s/64/radix.h | 12 ++- arch/powerpc/include/asm/interrupt.h | 52 ++++++++--- arch/powerpc/include/asm/kasan.h | 22 +++++ arch/powerpc/kernel/Makefile | 11 +++ arch/powerpc/kernel/smp.c | 22 ++--- arch/powerpc/kernel/traps.c | 6 +- arch/powerpc/kexec/Makefile | 2 + arch/powerpc/kvm/Makefile | 5 + arch/powerpc/lib/Makefile | 3 + arch/powerpc/mm/book3s64/Makefile | 9 ++ arch/powerpc/mm/kasan/Makefile | 3 +- .../mm/kasan/{kasan_init_32.c => init_32.c} | 0 arch/powerpc/mm/kasan/init_book3s_64.c | 103 +++++++++++++++++++++ arch/powerpc/mm/ptdump/ptdump.c | 3 +- arch/powerpc/platforms/Kconfig.cputype | 1 + arch/powerpc/platforms/powernv/Makefile | 8 ++ arch/powerpc/platforms/powernv/smp.c | 2 +- arch/powerpc/platforms/pseries/Makefile | 6 ++ arch/powerpc/sysdev/xics/xics-common.c | 4 +- arch/powerpc/sysdev/xive/common.c | 4 +- 26 files changed, 320 insertions(+), 38 deletions(-)
Quoted 11 lines · Paul Mackerras
This patch series implements KASAN on 64-bit POWER with radix MMU, such as POWER9 or POWER10. Daniel Axtens posted previous versions of these patches, but is no longer working on KASAN, and I have been asked to get them ready for inclusion.
Because of various technical difficulties, mostly around the need to allow for code that runs in real mode, we only support "outline" mode (as opposed to "inline" mode), where the compiler adds a call to a checking procedure before every store to memory.
[...]
Patches 1-5 applied to powerpc/next.
[1/6] kasan: Document support on 32-bit powerpc
https://git.kernel.org/powerpc/c/60e832def18de7a0753393034c6ae459b3bee70a [2/6] powerpc/mm/kasan: rename kasan_init_32.c to init_32.c https://git.kernel.org/powerpc/c/f08aed52412c860f68e30da148da58ad8e40a43b [3/6] powerpc: Book3S 64-bit outline-only KASAN support https://git.kernel.org/powerpc/c/41b7a347bf1491e7300563bb224432608b41f62a [4/6] powerpc/kasan: Don't instrument non-maskable or raw interrupts https://git.kernel.org/powerpc/c/5352090a999570c6e8a701bcb755fd91e8c5a2cd [5/6] powerpc/kasan: Disable address sanitization in kexec paths https://git.kernel.org/powerpc/c/2ab2d5794f14c08676690bf0859f16cc768bb3a4
cheers
Re: [PATCH v2 3/6] powerpc: Book3S 64-bit outline-only KASAN support
In reply to Paul MackerrasHi,
Quoted 11 lines · Paul Mackerras
From: Daniel Axtens <[email protected]>
Implement a limited form of KASAN for Book3S 64-bit machines running under the Radix MMU, supporting only outline mode.
- Enable the compiler instrumentation to check addresses and maintain the shadow region. (This is the guts of KASAN which we can easily reuse.)
- Require kasan-vmalloc support to handle modules and anything else in vmalloc space.
Quoted 7 lines
- KASAN needs to be able to validate all pointer accesses, but we can't instrument all kernel addresses - only linear map and vmalloc. On boot, set up a single page of read-only shadow that marks all iomap and vmemmap accesses as valid.
- Document KASAN in powerpc docs.
With this patch applied, powerpc:allmodconfig builds fail as follows.
Building powerpc:allmodconfig ... failed
--------------
Error log:
Error: External symbol 'memset' referenced from prom_init.c
make[3]: [arch/powerpc/kernel/Makefile:202: arch/powerpc/kernel/prom_init_check] Error 1 (ignored)
powerpc64-linux-ld: drivers/gpu/drm/amd/amdgpu/../display/dc/dml/display_mode_lib.o uses hard float, drivers/gpu/drm/amd/amdgpu/../display/dc/dcn31/dcn31_resource.o uses soft float
powerpc64-linux-ld: failed to merge target specific data of file drivers/gpu/drm/amd/amdgpu/../display/dc/dcn31/dcn31_resource.o
powerpc64-linux-ld: drivers/gpu/drm/amd/amdgpu/../display/dc/dml/display_mode_lib.o uses hard float, drivers/gpu/drm/amd/amdgpu/../display/dc/dcn315/dcn315_resource.o uses soft float
powerpc64-linux-ld: failed to merge target specific data of file drivers/gpu/drm/amd/amdgpu/../display/dc/dcn315/dcn315_resource.o
powerpc64-linux-ld: drivers/gpu/drm/amd/amdgpu/../display/dc/dml/display_mode_lib.o uses hard float, drivers/gpu/drm/amd/amdgpu/../display/dc/dcn316/dcn316_resource.o uses soft float
powerpc64-linux-ld: failed to merge target specific data of file drivers/gpu/drm/amd/amdgpu/../display/dc/dcn316/dcn316_resource.o
make[5]: [scripts/Makefile.build:435: drivers/gpu/drm/amd/amdgpu/amdgpu.o] Error 1 (ignored)
make[2]: *** No rule to make target 'drivers/gpu/drm/amd/amdgpu/amdgpu.o', needed by 'modules-only.symvers'. Stop.
make[1]: [Makefile:1753: modules] Error 2 (ignored)
This is with gcc 11.3 and binutils 2.38. I also tried with gcc 11.2 and binutils 2.36.1, with the same results.
Reverting this patch fixes the problem.
Guenter
Re: [PATCH v2 6/6] Documentation/kasan: Update details of KASAN on powerpc
In reply to Paul MackerrasLe 18/05/2022 à 12:07, Paul Mackerras a écrit :
Quoted 3 lines
From: Daniel Axtens <[email protected]>
Signed-off-by Paul Mackerras <[email protected]>
This change doesn't apply, can you rebase ?
Thanks
Christophe
Quoted 29 lines
--- Documentation/dev-tools/kasan.rst | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)Documentation/dev-tools/kasan.rst +6
-1@@ -36,7 +36,9 @@ Both software KASAN modes work with SLUB and SLAB memory allocators, while the hardware tag-based KASAN currently only supports SLUB. Currently, generic KASAN is supported for the x86_64, arm, arm64, xtensa, s390, -and riscv architectures, and tag-based KASAN modes are supported only for arm64. +and riscv architectures. It is also supported on powerpc for 32-bit kernels and +for 64-bit kernels running under the Radix MMU. Tag-based KASAN modes are +supported only for arm64. Usage -----@@ -351,6 +353,9 @@ With ``CONFIG_KASAN_VMALLOC``, KASAN can cover vmalloc space at the cost of greater memory usage. Currently, this is supported on x86, riscv, s390, and powerpc. +It is optional, except on 64-bit powerpc kernels, and on 32-bit +powerpc kernels with module support, where it is required. + This works by hooking into vmalloc and vmap and dynamically allocating real shadow memory to back the mappings.