KASAN for powerpc64 radix
Subsystems (5)
v4 cover KASAN for powerpc64 radix
Building on the work of Christophe, Aneesh and Balbir, I've ported KASAN to 64-bit Book3S kernels running on the Radix MMU.
This provides full inline instrumentation on radix, but does require that you be able to specify the amount of physically contiguous memory on the system at compile time. More details in patch 4.
v4: More cleanups, split renaming out, clarify bits and bobs.
Drop the stack walk disablement, that isn't needed. No other functional change.
v3: Reduce the overly ambitious scope of the MAX_PTRS change.
Document more things, including around why some of the restrictions apply.
Clean up the code more, thanks Christophe.
v2: The big change is the introduction of tree-wide(ish)
MAX_PTRS_PER_{PTE,PMD,PUD} macros in preference to the previous approach, which was for the arch to override the page table array definitions with their own. (And I squashed the annoying intermittent crash!)
Apart from that there's just a lot of cleanup. Christophe, I've addressed most of what you asked for and I will reply to your v1 emails to clarify what remains unchanged.
Daniel Axtens (4):
kasan: define and use MAX_PTRS_PER_* for early shadow tables
kasan: Document support on 32-bit powerpc powerpc/mm/kasan: rename kasan_init_32.c to init_32.c
powerpc: Book3S 64-bit "heavyweight" KASAN support
Documentation/dev-tools/kasan.rst | 7 +-
Documentation/powerpc/kasan.txt | 122 ++++++++++++++++++
arch/powerpc/Kconfig | 2 +
arch/powerpc/Kconfig.debug | 21 +++
arch/powerpc/Makefile | 11 ++
arch/powerpc/include/asm/book3s/64/hash.h | 4 +
arch/powerpc/include/asm/book3s/64/pgtable.h | 7 +
arch/powerpc/include/asm/book3s/64/radix.h | 5 +
arch/powerpc/include/asm/kasan.h | 21 ++-
arch/powerpc/kernel/prom.c | 61 ++++++++-
arch/powerpc/mm/kasan/Makefile | 3 +-
.../mm/kasan/{kasan_init_32.c => init_32.c} | 0
arch/powerpc/mm/kasan/init_book3s_64.c | 70 ++++++++++
arch/powerpc/platforms/Kconfig.cputype | 1 +
include/linux/kasan.h | 18 ++-
mm/kasan/init.c | 6 +-
16 files changed, 346 insertions(+), 13 deletions(-)
create mode 100644 Documentation/powerpc/kasan.txt
rename arch/powerpc/mm/kasan/{kasan_init_32.c => init_32.c} (100%)
create mode 100644 arch/powerpc/mm/kasan/init_book3s_64.cSignature · 1 line
2.20.1
PATCH v4 1/4 kasan: define and use MAX_PTRS_PER_* for early shadow tables
powerpc has a variable number of PTRS_PER_*, set at runtime based on the MMU that the kernel is booted under.
This means the PTRS_PER_* are no longer constants, and therefore breaks the build.
Define default MAX_PTRS_PER_*s in the same style as MAX_PTRS_PER_P4D. As KASAN is the only user at the moment, just define them in the kasan header, and have them default to PTRS_PER_* unless overridden in arch code.
--- include/linux/kasan.h | 18 +++++++++++++++--- mm/kasan/init.c | 6 +++--- 2 files changed, 18 insertions(+), 6 deletions(-)
include/linux/kasan.h +15 -3
@@ -14,10 +14,22 @@ struct task_struct;#include <asm/kasan.h> #include <asm/pgtable.h> +#ifndef MAX_PTRS_PER_PTE +#define MAX_PTRS_PER_PTE PTRS_PER_PTE +#endif + +#ifndef MAX_PTRS_PER_PMD +#define MAX_PTRS_PER_PMD PTRS_PER_PMD +#endif + +#ifndef MAX_PTRS_PER_PUD +#define MAX_PTRS_PER_PUD PTRS_PER_PUD +#endif +extern unsigned char kasan_early_shadow_page[PAGE_SIZE ]; -extern pte_t kasan_early_shadow_pte[PTRS_PER_PTE ]; -extern pmd_t kasan_early_shadow_pmd[PTRS_PER_PMD ]; -extern pud_t kasan_early_shadow_pud[PTRS_PER_PUD ]; +extern pte_t kasan_early_shadow_pte[MAX_PTRS_PER_PTE ]; +extern pmd_t kasan_early_shadow_pmd[MAX_PTRS_PER_PMD ]; +extern pud_t kasan_early_shadow_pud[MAX_PTRS_PER_PUD ];extern p4d_t kasan_early_shadow_p4d[MAX_PTRS_PER_P4D ]; int kasan_populate_early_shadow(const void *shadow_start,
mm/kasan/init.c +3 -3
@@ -46,7 +46,7 @@ static inline bool kasan_p4d_table(pgd_t pgd) }#endif #if CONFIG_PGTABLE_LEVELS >3 -pud_t kasan_early_shadow_pud[PTRS_PER_PUD] __page_aligned_bss; +pud_t kasan_early_shadow_pud[MAX_PTRS_PER_PUD] __page_aligned_bss;static inline bool kasan_pud_table (p4d_t p4d) {return p4d_page (p4d) ==virt_to_page (lm_alias (kasan_early_shadow_pud));
@@ -58,7 +58,7 @@ static inline bool kasan_pud_table(p4d_t p4d) }#endif #if CONFIG_PGTABLE_LEVELS >2 -pmd_t kasan_early_shadow_pmd[PTRS_PER_PMD] __page_aligned_bss; +pmd_t kasan_early_shadow_pmd[MAX_PTRS_PER_PMD] __page_aligned_bss;static inline bool kasan_pmd_table (pud_t pud) {return pud_page (pud) ==virt_to_page (lm_alias (kasan_early_shadow_pmd));
@@ -69,7 +69,7 @@ static inline bool kasan_pmd_table(pud_t pud)return false; }#endif -pte_t kasan_early_shadow_pte[PTRS_PER_PTE] __page_aligned_bss; +pte_t kasan_early_shadow_pte[MAX_PTRS_PER_PTE] __page_aligned_bss;static inline bool kasan_pte_table (pmd_t pmd) {
Signature · 1 line
2.20.1
PATCH v4 2/4 kasan: Document support on 32-bit powerpc
In reply to Daniel AxtensKASAN is supported on 32-bit powerpc and the docs should reflect this.
--- Documentation/dev-tools/kasan.rst | 3 ++- Documentation/powerpc/kasan.txt | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 Documentation/powerpc/kasan.txt
Documentation/dev-tools/kasan.rst +2 -1
@@ -22,7 +22,8 @@ global variables yet. Tag-based KASAN is only supported in Clang and requires version 7.0.0 or later. Currently generic KASAN is supported for the x86_64, arm64, xtensa and s390 -architectures, and tag-based KASAN is supported only for arm64. +architectures. It is also supported on 32-bit powerpc kernels. Tag-based KASAN +is supported only on arm64. Usage -----
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 not currently supported, but modules +are.
Signature · 1 line
2.20.1
PATCH v4 3/4 powerpc/mm/kasan: rename kasan_init_32.c to init_32.c
In reply to Daniel Axtenskasan 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,4 +2,4 @@ KASAN_SANITIZE := n -obj-$(CONFIG_PPC32) += kasan_init_32.o +obj-$(CONFIG_PPC32) += init_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.20.1
PATCH v4 4/4 powerpc: Book3S 64-bit "heavyweight" KASAN support
In reply to Daniel AxtensKASAN 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 in real mode after booting. Most notably a lot of KVM runs in real mode, and it would be good to be able to instrument it.
- Because code runs in real mode after boot, the offset has to point to valid memory both in and out of real mode.
[ppc64 mm note: The kernel installs a linear mapping at effective address c000... onward. 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.]
One approach is just to give up on inline instrumentation. This way all checks can be delayed until after everything set is up correctly, and the address-to-shadow calculations can be overridden. However, the features and speed boost provided by inline instrumentation are worth trying to do better.
If _at compile time_ it is known how much contiguous physical memory a system has, the top 1/8th of the first block of physical memory can be set aside for the shadow. This is a big hammer and comes with 3 big consequences:
- there's no nice way to handle physically discontiguous memory, so only the first physical memory block can be used.
- kernels will simply fail to boot on machines with less memory than specified when compiling.
- kernels running on machines with more memory than specified when compiling will simply ignore the extra memory.
Implement and document KASAN this way. The current implementation is Radix only.
Despite the limitations, it can still find bugs,
e.g. http://patchwork.ozlabs.org/patch/1103775/
At the moment, this physical memory limit must be set _even for outline mode_. This may be changed in a later series - a different implementation could be added for outline mode that dynamically allocates shadow at a fixed offset. For example, see https://patchwork.ozlabs.org/patch/795211/
---
Changes since v3:
- Address further feedback from Christophe.
- Drop changes to stack walking, it looks like the issue I observed is related to that particular stack, not stack-walking generally.
Changes since v2:
- Address feedback from Christophe around cleanups and docs.
- Address feedback from Balbir: at this point I don't have a good solution for the issues you identify around the limitations of the inline implementation but I think that it's worth trying to get the stack instrumentation support. I'm happy to have an alternative and more flexible outline mode - I had envisoned this would be called 'lightweight' mode as it imposes fewer restrictions. I've linked to your implementation. I think it's best to add it in a follow-up series.
- Made the default PHYS_MEM_SIZE_FOR_KASAN value 1024MB. I think most people have guests with at least that much memory in the Radix 64s case so it's a much saner default - it means that if you just turn on KASAN without reading the docs you're much more likely to have a bootable kernel, which you will never have if the value is set to zero! I'm happy to bikeshed the value if we want.
Changes since v1:
- Landed kasan vmalloc support upstream
- Lots of feedback from Christophe.
Changes since the rfc:
- Boots real and virtual hardware, kvm works.
- disabled reporting when we're checking the stack for exception frames. The behaviour isn't wrong, just incompatible with KASAN.
- Documentation!
- Dropped old module stuff in favour of KASAN_VMALLOC.
The bugs with ftrace and kuap were due to kernel bloat pushing prom_init calls to be done via the plt. Because we did not have a relocatable kernel, and they are done very early, this caused everything to explode. Compile with CONFIG_RELOCATABLE!
--- Documentation/dev-tools/kasan.rst | 8 +- Documentation/powerpc/kasan.txt | 112 ++++++++++++++++++- arch/powerpc/Kconfig | 2 + arch/powerpc/Kconfig.debug | 21 ++++ arch/powerpc/Makefile | 11 ++ arch/powerpc/include/asm/book3s/64/hash.h | 4 + arch/powerpc/include/asm/book3s/64/pgtable.h | 7 ++ arch/powerpc/include/asm/book3s/64/radix.h | 5 + arch/powerpc/include/asm/kasan.h | 21 +++- arch/powerpc/kernel/prom.c | 61 +++++++++- arch/powerpc/mm/kasan/Makefile | 1 + arch/powerpc/mm/kasan/init_book3s_64.c | 70 ++++++++++++ arch/powerpc/platforms/Kconfig.cputype | 1 + 13 files changed, 316 insertions(+), 8 deletions(-) create mode 100644 arch/powerpc/mm/kasan/init_book3s_64.c
Documentation/dev-tools/kasan.rst +5 -3
@@ -22,8 +22,9 @@ global variables yet. Tag-based KASAN is only supported in Clang and requires version 7.0.0 or later. Currently generic KASAN is supported for the x86_64, arm64, xtensa and s390 -architectures. It is also supported on 32-bit powerpc kernels. Tag-based KASAN -is supported only on arm64. +architectures. It is also supported on powerpc, for 32-bit kernels, and for +64-bit kernels running under the Radix MMU. Tag-based KASAN is supported only +on arm64. Usage -----
@@ -256,7 +257,8 @@ CONFIG_KASAN_VMALLOC ~~~~~~~~~~~~~~~~~~~~ With ``CONFIG_KASAN_VMALLOC``, KASAN can cover vmalloc space at the -cost of greater memory usage. Currently this is only supported on x86. +cost of greater memory usage. Currently this is optional on x86, and +required on 64-bit powerpc. This works by hooking into vmalloc and vmap, and dynamically allocating real shadow memory to back the mappings.
Documentation/powerpc/kasan.txt +111 -1
118 lines not on this page · raw message
arch/powerpc/Kconfig +2 -0
@@ -173,6 +173,8 @@ config PPC select HAVE_ARCH_HUGE_VMAP if PPC_BOOK3S_64 && PPC_RADIX_MMU select HAVE_ARCH_JUMP_LABEL select HAVE_ARCH_KASAN if PPC32 + select HAVE_ARCH_KASAN if PPC_BOOK3S_64 && PPC_RADIX_MMU + select HAVE_ARCH_KASAN_VMALLOC if PPC_BOOK3S_64 && PPC_RADIX_MMU select HAVE_ARCH_KGDB select HAVE_ARCH_MMAP_RND_BITS select HAVE_ARCH_MMAP_RND_COMPAT_BITS if COMPAT
arch/powerpc/Kconfig.debug +21 -0
@@ -394,6 +394,27 @@ config PPC_FAST_ENDIAN_SWITCH help If you're unsure what this is, say N. +config PHYS_MEM_SIZE_FOR_KASAN + int "Contiguous physical memory size for KASAN (MB)" if KASAN && PPC_BOOK3S_64 + default 1024 + help + + To get inline instrumentation support for KASAN on 64-bit Book3S + machines, you need to know how much contiguous physical memory your + system has. A shadow offset will be calculated based on this figure, + which will be compiled in to the kernel. KASAN will use this offset + to access its shadow region, which is used to verify memory accesses. + + If you attempt to boot on a system with less memory than you specify + here, your system will fail to boot very early in the process. If you + boot on a system with more memory than you specify, the extra memory + will wasted - it will be reserved and not used. + + For systems with discontiguous blocks of physical memory, specify the + size of the block starting at 0x0. You can determine this by looking + at the memory layout info printed to dmesg by the radix MMU code + early in boot. See Documentation/powerpc/kasan.txt. + config KASAN_SHADOW_OFFSET hex depends on KASAN
arch/powerpc/Makefile +11 -0
@@ -230,6 +230,17 @@ ifdef CONFIG_476FPE_ERR46 -T $(srctree)/arch/powerpc/platforms/44x/ppc476_modules.lds endif +ifdef CONFIG_PPC_BOOK3S_64 +# The KASAN shadow offset is such that linear map (0xc000...) is shadowed by +# the last 8th of linearly mapped physical memory. This way, if the code uses +# 0xc addresses throughout, accesses work both in in real mode (where the top +# bits are ignored) and outside of real mode. +# +# 0xc000000000000000 >> 3 = 0xa800000000000000 = 12105675798371893248 +KASAN_SHADOW_OFFSET = $(shell echo 7 \* 1024 \* 1024 \* $(CONFIG_PHYS_MEM_SIZE_FOR_KASAN) / 8 + 12105675798371893248 | bc) +KBUILD_CFLAGS += -DKASAN_SHADOW_OFFSET=$(KASAN_SHADOW_OFFSET)UL +endif + # No AltiVec or VSX instructions when building kernel KBUILD_CFLAGS += $(call cc-option,-mno-altivec) KBUILD_CFLAGS += $(call cc-option,-mno-vsx)
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 +7 -0
@@ -231,6 +231,13 @@ 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) +/* PMD_SHIFT determines what a second-level page table entry can map */ #define PMD_SHIFT (PAGE_SHIFT + PTE_INDEX_SIZE)#define PMD_SIZE (1UL << PMD_SHIFT)
arch/powerpc/include/asm/book3s/64/radix.h +5 -0
@@ -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. */
arch/powerpc/include/asm/kasan.h +18 -3
@@ -2,6 +2,9 @@#ifndef __ASM_KASAN_H#define __ASM_KASAN_H +#include <asm/page.h> +#include <asm/pgtable.h> +#ifdef CONFIG_KASAN #define _GLOBAL_KASAN (fn) _GLOBAL(__##fn)#define _GLOBAL_TOC_KASAN (fn) _GLOBAL_TOC(__##fn)
@@ -14,13 +17,19 @@#ifndef __ASSEMBLY__ -#include <asm/page.h> +#ifdef CONFIG_KASAN +void kasan_init (void ); +#else +static inline void kasan_init (void ) { } +#endif #define KASAN_SHADOW_SCALE_SHIFT 3#define KASAN_SHADOW_START (KASAN_SHADOW_OFFSET + \ (PAGE_OFFSET >> KASAN_SHADOW_SCALE_SHIFT)) +#ifdef CONFIG_PPC32 +#define KASAN_SHADOW_OFFSET ASM_CONST(CONFIG_KASAN_SHADOW_OFFSET)#define KASAN_SHADOW_END 0UL
@@ -30,11 +39,17 @@#ifdef CONFIG_KASAN void kasan_early_init (void );void kasan_mmu_init (void ); -void kasan_init (void );#else -static inline void kasan_init (void ) { }static inline void kasan_mmu_init (void ) { }#endif +#endif + +#ifdef CONFIG_PPC_BOOK3S_64 + +#define KASAN_SHADOW_SIZE ((u64)CONFIG_PHYS_MEM_SIZE_FOR_KASAN * \ + 1024 * 1024 * 1 / 8) + +#endif /* CONFIG_PPC_BOOK3S_64 */ #endif /* __ASSEMBLY */ #endif
arch/powerpc/kernel/prom.c +60 -1
92 lines not on this page · raw message
arch/powerpc/mm/kasan/Makefile +1 -0
@@ -3,3 +3,4 @@ KASAN_SANITIZE := n obj-$(CONFIG_PPC32) += init_32.o +obj-$(CONFIG_PPC_BOOK3S_64) += init_book3s_64.o
arch/powerpc/mm/kasan/init_book3s_64.c new file +70 -0
70 lines not on this page · raw message
arch/powerpc/platforms/Kconfig.cputype +1 -0
@@ -91,6 +91,7 @@ config PPC_BOOK3S_64 select ARCH_SUPPORTS_NUMA_BALANCING select IRQ_WORK select PPC_MM_SLICES + select KASAN_VMALLOC if KASAN config PPC_BOOK3E_64 bool "Embedded processors"
Signature · 1 line
2.20.1
Re: [PATCH v4 4/4] powerpc: Book3S 64-bit "heavyweight" KASAN support
Quoted 122 lines · Daniel Axtens
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 in real mode after booting. Most notably a lot of KVM runs in real mode, and it would be good to be able to instrument it.
- Because code runs in real mode after boot, the offset has to point to valid memory both in and out of real mode.
[ppc64 mm note: The kernel installs a linear mapping at effective address c000... onward. 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.]
One approach is just to give up on inline instrumentation. This way all checks can be delayed until after everything set is up correctly, and the address-to-shadow calculations can be overridden. However, the features and speed boost provided by inline instrumentation are worth trying to do better.
If
_atcompiletime_it is known how much contiguous physical memory a system has, the top 1/8th of the first block of physical memory can be set aside for the shadow. This is a big hammer and comes with 3 big consequences:- there's no nice way to handle physically discontiguous memory, so only the first physical memory block can be used.
- kernels will simply fail to boot on machines with less memory than specified when compiling.
- kernels running on machines with more memory than specified when compiling will simply ignore the extra memory.
Implement and document KASAN this way. The current implementation is Radix only.
Despite the limitations, it can still find bugs,
e.g. http://patchwork.ozlabs.org/patch/1103775/At the moment, this physical memory limit must be set
_evenfor outlinemode_. This may be changed in a later series - a different implementation could be added for outline mode that dynamically allocates shadow at a fixed offset. For example, see https://patchwork.ozlabs.org/patch/795211/Suggested-by Michael Ellerman <[email protected]>Cc Balbir Singh <[email protected]> # ppc64 out-of-line radix versionCc Christophe Leroy <[email protected]> # ppc32 versionSigned-off-by Daniel Axtens <[email protected]>---
Changes since v3:
- Address further feedback from Christophe.
- Drop changes to stack walking, it looks like the issue I observed is related to that particular stack, not stack-walking generally.Changes since v2:
- Address feedback from Christophe around cleanups and docs.
- Address feedback from Balbir: at this point I don't have a good solution for the issues you identify around the limitations of the inline implementation but I think that it's worth trying to get the stack instrumentation support. I'm happy to have an alternative and more flexible outline mode - I had envisoned this would be called 'lightweight' mode as it imposes fewer restrictions. I've linked to your implementation. I think it's best to add it in a follow-up series.
- Made the defaultPHYS_MEM_SIZE_FOR_KASANvalue 1024MB. I think most people have guests with at least that much memory in the Radix 64s case so it's a much saner default - it means that if you just turn on KASAN without reading the docs you're much more likely to have a bootable kernel, which you will never have if the value is set to zero! I'm happy to bikeshed the value if we want.Changes since v1:
- Landed kasan vmalloc support upstream
- Lots of feedback from Christophe.Changes since the rfc:
- Boots real and virtual hardware, kvm works.
- disabled reporting when we're checking the stack for exception frames. The behaviour isn't wrong, just incompatible with KASAN.
- Documentation!
- Dropped old module stuff in favour of
KASAN_VMALLOC.The bugs with ftrace and kuap were due to kernel bloat pushing
prom_initcalls to be done via the plt. Because we did not have a relocatable kernel, and they are done very early, this caused everything to explode. Compile withCONFIG_RELOCATABLE!--- Documentation/dev-tools/kasan.rst | 8 +- Documentation/powerpc/kasan.txt | 112 ++++++++++++++++++- arch/powerpc/Kconfig | 2 + arch/powerpc/Kconfig.debug | 21 ++++ arch/powerpc/Makefile | 11 ++ arch/powerpc/include/asm/book3s/64/hash.h | 4 + arch/powerpc/include/asm/book3s/64/pgtable.h | 7 ++ arch/powerpc/include/asm/book3s/64/radix.h | 5 + arch/powerpc/include/asm/kasan.h | 21 +++- arch/powerpc/kernel/prom.c | 61 +++++++++- arch/powerpc/mm/kasan/Makefile | 1 + arch/powerpc/mm/kasan/init_book3s_64.c | 70 ++++++++++++ arch/powerpc/platforms/Kconfig.cputype | 1 + 13 files changed, 316 insertions(+), 8 deletions(-)create mode 100644
arch/powerpc/mm/kasan/init_book3s_64.carch/powerpc/include/asm/kasan.h +0
-0
arch/powerpc/include/asm/kasan.h line 6
Quoted 5 lines
arch/powerpc/include/asm/kasan.h +2
-0@@ -2,6 +2,9 @@#ifndef __ASM_KASAN_H#define __ASM_KASAN_H +#include <asm/page.h> +#include <asm/pgtable.h>
What do you need asm/pgtable.h for ?
Build failure due to circular inclusion of asm/pgtable.h:
CC arch/powerpc/kernel/asm-offsets.s
In file included from ./arch/powerpc/include/asm/nohash/32/pgtable.h:77:0,
from ./arch/powerpc/include/asm/nohash/pgtable.h:8,
from ./arch/powerpc/include/asm/pgtable.h:20,
from ./arch/powerpc/include/asm/kasan.h:6,
from ./include/linux/kasan.h:14,
from ./include/linux/slab.h:136,
from ./include/linux/crypto.h:19,
from ./include/crypto/hash.h:11,
from ./include/linux/uio.h:10,
from ./include/linux/socket.h:8,
from ./include/linux/compat.h:15,
from arch/powerpc/kernel/asm-offsets.c:14:
./include/asm-generic/fixmap.h: In function 'fix_to_virt':
./arch/powerpc/include/asm/fixmap.h:28:22: error: 'KASAN_SHADOW_START' undeclared (first use in this function)
#define FIXADDR_TOP (KASAN_SHADOW_START - PAGE_SIZE)
^
./include/asm-generic/fixmap.h:21:27: note: in expansion of macro 'FIXADDR_TOP'
#define __fix_to_virt(x) (FIXADDR_TOP - ((x) << PAGE_SHIFT))
^
./include/asm-generic/fixmap.h:33:9: note: in expansion of macro '__fix_to_virt'
return __fix_to_virt(idx);
^
./arch/powerpc/include/asm/fixmap.h:28:22: note: each undeclared identifier is reported only once for each function it appears in
#define FIXADDR_TOP (KASAN_SHADOW_START - PAGE_SIZE)
^
./include/asm-generic/fixmap.h:21:27: note: in expansion of macro 'FIXADDR_TOP'
#define __fix_to_virt(x) (FIXADDR_TOP - ((x) << PAGE_SHIFT))
^
./include/asm-generic/fixmap.h:33:9: note: in expansion of macro '__fix_to_virt'
return __fix_to_virt(idx);
^
In file included from ./include/linux/bug.h:5:0,
from ./include/linux/thread_info.h:12,
from ./include/asm-generic/preempt.h:5,
from ./arch/powerpc/include/generated/asm/preempt.h:1,
from ./include/linux/preempt.h:78,
from ./include/linux/spinlock.h:51,
from ./include/linux/seqlock.h:36,
from ./include/linux/time.h:6,
from ./include/linux/compat.h:10,
from arch/powerpc/kernel/asm-offsets.c:14:
./include/asm-generic/fixmap.h: In function 'virt_to_fix':
./arch/powerpc/include/asm/fixmap.h:28:22: error: 'KASAN_SHADOW_START' undeclared (first use in this function)
#define FIXADDR_TOP (KASAN_SHADOW_START - PAGE_SIZE)
^
./arch/powerpc/include/asm/bug.h:73:27: note: in definition of macro 'BUG_ON'
if (__builtin_constant_p(x)) { \
^
./include/asm-generic/fixmap.h:38:18: note: in expansion of macro 'FIXADDR_TOP'
BUG_ON(vaddr >= FIXADDR_TOP || vaddr < FIXADDR_START);
^
In file included from ./arch/powerpc/include/asm/nohash/32/pgtable.h:77:0,
from ./arch/powerpc/include/asm/nohash/pgtable.h:8,
from ./arch/powerpc/include/asm/pgtable.h:20,
from ./arch/powerpc/include/asm/kasan.h:6,
from ./include/linux/kasan.h:14,
from ./include/linux/slab.h:136,
from ./include/linux/crypto.h:19,
from ./include/crypto/hash.h:11,
from ./include/linux/uio.h:10,
from ./include/linux/socket.h:8,
from ./include/linux/compat.h:15,
from arch/powerpc/kernel/asm-offsets.c:14:
./arch/powerpc/include/asm/fixmap.h: In function '__set_fixmap':
./arch/powerpc/include/asm/fixmap.h:28:22: error: 'KASAN_SHADOW_START' undeclared (first use in this function)
#define FIXADDR_TOP (KASAN_SHADOW_START - PAGE_SIZE)
^
./include/asm-generic/fixmap.h:21:27: note: in expansion of macro 'FIXADDR_TOP'
#define __fix_to_virt(x) (FIXADDR_TOP - ((x) << PAGE_SHIFT))
^
./arch/powerpc/include/asm/fixmap.h:102:18: note: in expansion of macro '__fix_to_virt'
map_kernel_page(__fix_to_virt(idx), phys, flags);
^make[2]: *** [arch/powerpc/kernel/asm-offsets.s] Error 1 make[1]: *** [prepare0] Error 2
make: *** [sub-make] Error 2
Quoted 5 lines
arch/powerpc/include/asm/kasan.h +1
-0+#ifdef CONFIG_KASAN #define _GLOBAL_KASAN (fn) _GLOBAL(__##fn)#define _GLOBAL_TOC_KASAN (fn) _GLOBAL_TOC(__##fn)
arch/powerpc/include/asm/kasan.h line 24
Quoted 9 lines
arch/powerpc/include/asm/kasan.h +5
-1@@ -14,13 +17,19 @@#ifndef __ASSEMBLY__ -#include <asm/page.h> +#ifdef CONFIG_KASAN +void kasan_init (void ); +#else +static inline void kasan_init (void ) { } +#endif
I don't think it is worth moving this. Just keep everything out of the #ifdef CONFIG_PPC32. Having undefined/unused functions there shouldn't matter.
arch/powerpc/include/asm/kasan.h
Quoted 12 lines
arch/powerpc/include/asm/kasan.h +2
-0#define KASAN_SHADOW_SCALE_SHIFT 3#define KASAN_SHADOW_START (KASAN_SHADOW_OFFSET + \ (PAGE_OFFSET >> KASAN_SHADOW_SCALE_SHIFT)) +#ifdef CONFIG_PPC32 +#define KASAN_SHADOW_OFFSET ASM_CONST(CONFIG_KASAN_SHADOW_OFFSET)#define KASAN_SHADOW_END 0UL@@ -30,11 +39,17 @@
Keep the block below out of the CONFIG_PPC32 ifdef, don't need to move kasan_init()
Quoted 4 lines
arch/powerpc/include/asm/kasan.h +0
-1#ifdef CONFIG_KASAN void kasan_early_init (void );void kasan_mmu_init (void ); -void kasan_init (void );
arch/powerpc/include/asm/kasan.h line 50
Quoted 10 lines
arch/powerpc/include/asm/kasan.h +6
-1#else -static inline void kasan_init (void ) { }static inline void kasan_mmu_init (void ) { }#endif +#endif + +#ifdef CONFIG_PPC_BOOK3S_64 + +#define KASAN_SHADOW_SIZE ((u64)CONFIG_PHYS_MEM_SIZE_FOR_KASAN * \ + 1024 * 1024 * 1 / 8)
What about:
(ASM_CONST(CONFIG_PHYS_MEM_SIZE_FOR_KASAN) * SZ_1G) >> KASAN_SHADOW_SCALE_SHIFT
Quoted 45 lines
arch/powerpc/include/asm/kasan.h +2
-0+ +#endif /* CONFIG_PPC_BOOK3S_64 */ #endif /* __ASSEMBLY */ #endif arch/powerpc/kernel/prom.c +21
-0@@ -72,6 +72,7 @@ unsigned long tce_alloc_start, tce_alloc_end;u64 ppc64_rma_size;#endif static phys_addr_t first_memblock_size; +static phys_addr_t top_phys_addr;static int __initdata boot_cpu_count;static int __init early_parse_mem (char *p)@@ -449,6 +450,26 @@ static bool validate_mem_limit(u64 base, u64 *size) {u64 max_mem =1UL << (MAX_PHYSMEM_BITS ); +/* +* To handle the NUMA/discontiguous memory case, don't allow a block +* to be added if it falls completely beyond the configured physical +* memory. Print an informational message. +* +* Frustratingly we also see this with qemu - it seems to split the +* specified memory into a number of smaller blocks. If this happens +* under qemu, it probably represents misconfiguration. So we want +* the message to be noticeable, but not shouty. +* +* See Documentation/powerpc/kasan.txt +*/ +if (IS_ENABLED (CONFIG_KASAN ) && + (base >= ((u64 )CONFIG_PHYS_MEM_SIZE_FOR_KASAN <<20 ))) { +pr_warn ("KASAN: not adding memory block at %llx (size %llx)\n" +"This could be due to discontiguous memory or kernel misconfiguration." , + base, *size); +return false; + } +if (base >= max_mem)return false;if ((base + *size) > max_mem)
arch/powerpc/kernel/prom.c void __init early_init_dt_add_memory_arch(u64 base, u64 size) · line 598
Quoted 7 lines
arch/powerpc/kernel/prom.c +2
-1@@ -572,8 +593,10 @@ void __init early_init_dt_add_memory_arch(u64 base, u64 size)/* Add the chunk to the MEMBLOCK list */ if (add_mem_to_memblock) { -if (validate_mem_limit (base, &size)) +if (validate_mem_limit (base, &size)) {memblock_add (base, size); + top_phys_addr =max (top_phys_addr, base + size);
Build failure, you have to cast (base + size) to (phys_addr_t) as phys_addr_t is not always u64.
CC arch/powerpc/kernel/asm-offsets.s CALL scripts/checksyscalls.sh CALL scripts/atomic/check-atomics.sh CC arch/powerpc/kernel/prom.o
In file included from arch/powerpc/kernel/prom.c:15:0:arch/powerpc/kernel/prom.c: In function 'early_init_dt_add_memory_arch':./include/linux/kernel.h:844:29: error: comparison of distinct pointer types lacks a cast [-Werror]
(!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
^
./include/linux/kernel.h:858:4: note: in expansion of macro '__typecheck'
(__typecheck(x, y) && __no_side_effects(x, y))
^
./include/linux/kernel.h:868:24: note: in expansion of macro '__safe_cmp'
__builtin_choose_expr(__safe_cmp(x, y), \
^
./include/linux/kernel.h:884:19: note: in expansion of macro '__careful_cmp'
#define max(x, y) __careful_cmp(x, y, >)
^
arch/powerpc/kernel/prom.c:598:20: note: in expansion of macro 'max'
top_phys_addr = max(top_phys_addr, base + size);
^cc1: all warnings being treated as errors
make[3]: *** [arch/powerpc/kernel/prom.o] Error 1
make[2]: *** [arch/powerpc/kernel] Error 2
make[1]: *** [arch/powerpc] Error 2
make: *** [sub-make] Error 2
Quoted 44 lines
arch/powerpc/kernel/prom.c +30
-0+ } } }@@ -613,6 +636,8 @@ static void __init early_reserve_mem_dt(void)static void __init early_reserve_mem (void ) {__be64 *reserve_map; +phys_addr_t kasan_shadow_start; +phys_addr_t kasan_memory_size; reserve_map = (__be64 *)(((unsigned long )initial_boot_params) +fdt_off_mem_rsvmap (initial_boot_params));@@ -651,6 +676,40 @@ static void __init early_reserve_mem(void) return; }#endif + +if (IS_ENABLED (CONFIG_KASAN ) &&IS_ENABLED (CONFIG_PPC_BOOK3S_64 )) { + kasan_memory_size = + ((phys_addr_t )CONFIG_PHYS_MEM_SIZE_FOR_KASAN <<20 ); + +if (top_phys_addr < kasan_memory_size) { +/* +* We are doomed. We shouldn't even be able to get this +* far, but we do in qemu. If we continue and turn +* relocations on, we'll take fatal page faults for +* memory that's not physically present. Instead, +* panic() here: it will be saved to __log_buf even if +* it doesn't get printed to the console. +*/ +panic ("Tried to boot a KASAN kernel configured for %u MB with only %llu MB! Aborting." , +CONFIG_PHYS_MEM_SIZE_FOR_KASAN , + (u64)(top_phys_addr >>20 )); + }else if (top_phys_addr > kasan_memory_size) { +/* print a biiiig warning in hopes people notice */ +pr_err ("===========================================\n" +"Physical memory exceeds compiled-in maximum!\n" +"This kernel was compiled for KASAN with %u MB physical memory.\n" +"The physical memory detected is at least %llu MB.\n" +"Memory above the compiled limit will not be used!\n" +"===========================================\n" , +CONFIG_PHYS_MEM_SIZE_FOR_KASAN , + (u64)(top_phys_addr >>20 ));
arch/powerpc/kernel/prom.c static void __init early_reserve_mem(void) · line 715
Quoted 10 lines
arch/powerpc/kernel/prom.c +7
-0+ } + + kasan_shadow_start =_ALIGN_DOWN (kasan_memory_size *7 /8 ,PAGE_SIZE ); +DBG ("reserving %llx -> %llx for KASAN" , + kasan_shadow_start, top_phys_addr); +memblock_reserve (kasan_shadow_start, top_phys_addr - kasan_shadow_start); + } }#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Christophe
Re: [PATCH v4 4/4] powerpc: Book3S 64-bit "heavyweight" KASAN support
Quoted 126 lines · Christophe Leroy
Quoted 125 lines · Daniel Axtens
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 in real mode after booting. Most notably a lot of KVM runs in real mode, and it would be good to be able to instrument it.
- Because code runs in real mode after boot, the offset has to point to valid memory both in and out of real mode.
[ppc64 mm note: The kernel installs a linear mapping at effective address c000... onward. 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.]
One approach is just to give up on inline instrumentation. This way all checks can be delayed until after everything set is up correctly, and the address-to-shadow calculations can be overridden. However, the features and speed boost provided by inline instrumentation are worth trying to do better.
If
_atcompiletime_it is known how much contiguous physical memory a system has, the top 1/8th of the first block of physical memory can be set aside for the shadow. This is a big hammer and comes with 3 big consequences:- there's no nice way to handle physically discontiguous memory, so only the first physical memory block can be used.
- kernels will simply fail to boot on machines with less memory than specified when compiling.
- kernels running on machines with more memory than specified when compiling will simply ignore the extra memory.
Implement and document KASAN this way. The current implementation is Radix only.
Despite the limitations, it can still find bugs,
e.g. http://patchwork.ozlabs.org/patch/1103775/At the moment, this physical memory limit must be set
_evenfor outlinemode_. This may be changed in a later series - a different implementation could be added for outline mode that dynamically allocates shadow at a fixed offset. For example, see https://patchwork.ozlabs.org/patch/795211/Suggested-by Michael Ellerman <[email protected]>Cc Balbir Singh <[email protected]> # ppc64 out-of-line radix versionCc Christophe Leroy <[email protected]> # ppc32 versionSigned-off-by Daniel Axtens <[email protected]>---
Changes since v3:
- Address further feedback from Christophe.
- Drop changes to stack walking, it looks like the issue I observed is related to that particular stack, not stack-walking generally.Changes since v2:
- Address feedback from Christophe around cleanups and docs.
- Address feedback from Balbir: at this point I don't have a good solution for the issues you identify around the limitations of the inline implementation but I think that it's worth trying to get the stack instrumentation support. I'm happy to have an alternative and more flexible outline mode - I had envisoned this would be called 'lightweight' mode as it imposes fewer restrictions. I've linked to your implementation. I think it's best to add it in a follow-up series.
- Made the defaultPHYS_MEM_SIZE_FOR_KASANvalue 1024MB. I think most people have guests with at least that much memory in the Radix 64s case so it's a much saner default - it means that if you just turn on KASAN without reading the docs you're much more likely to have a bootable kernel, which you will never have if the value is set to zero! I'm happy to bikeshed the value if we want.Changes since v1:
- Landed kasan vmalloc support upstream
- Lots of feedback from Christophe.Changes since the rfc:
- Boots real and virtual hardware, kvm works.
- disabled reporting when we're checking the stack for exception frames. The behaviour isn't wrong, just incompatible with KASAN.
- Documentation!
- Dropped old module stuff in favour of
KASAN_VMALLOC.The bugs with ftrace and kuap were due to kernel bloat pushing
prom_initcalls to be done via the plt. Because we did not have a relocatable kernel, and they are done very early, this caused everything to explode. Compile withCONFIG_RELOCATABLE!--- Documentation/dev-tools/kasan.rst | 8 +- Documentation/powerpc/kasan.txt | 112 ++++++++++++++++++- arch/powerpc/Kconfig | 2 + arch/powerpc/Kconfig.debug | 21 ++++ arch/powerpc/Makefile | 11 ++ arch/powerpc/include/asm/book3s/64/hash.h | 4 + arch/powerpc/include/asm/book3s/64/pgtable.h | 7 ++ arch/powerpc/include/asm/book3s/64/radix.h | 5 + arch/powerpc/include/asm/kasan.h | 21 +++- arch/powerpc/kernel/prom.c | 61 +++++++++- arch/powerpc/mm/kasan/Makefile | 1 + arch/powerpc/mm/kasan/init_book3s_64.c | 70 ++++++++++++ arch/powerpc/platforms/Kconfig.cputype | 1 + 13 files changed, 316 insertions(+), 8 deletions(-)create mode 100644
arch/powerpc/mm/kasan/init_book3s_64.carch/powerpc/include/asm/kasan.h +0
-0@@ -2,6 +2,9 @@#ifndef __ASM_KASAN_H#define __ASM_KASAN_H
Quoted 6 lines
arch/powerpc/include/asm/kasan.h line 6
Quoted 2 lines
arch/powerpc/include/asm/kasan.h +2
-0+#include <asm/page.h> +#include <asm/pgtable.h> What do you need
asm/pgtable.hfor ?Build failure due to circular inclusion of
asm/pgtable.h:
I see there's a lot of ppc32 stuff, I clearly need to bite the bullet and get a ppc32 toolchain so I can squash these without chewing up any more of your time. I'll sort that out and send a new spin.
Regards,
Daniel
Quoted 288 lines
CC
arch/powerpc/kernel/asm-offsets.s
In file included from./arch/powerpc/include/asm/nohash/32/pgtable.h:77:0,from ./arch/powerpc/include/asm/nohash/pgtable.h:8, from ./arch/powerpc/include/asm/pgtable.h:20, from ./arch/powerpc/include/asm/kasan.h:6, from ./include/linux/kasan.h:14, from ./include/linux/slab.h:136, from ./include/linux/crypto.h:19, from ./include/crypto/hash.h:11, from ./include/linux/uio.h:10, from ./include/linux/socket.h:8, from ./include/linux/compat.h:15, from arch/powerpc/kernel/asm-offsets.c:14: ./include/asm-generic/fixmap.h: In function 'fix_to_virt': ./arch/powerpc/include/asm/fixmap.h:28:22: error: 'KASAN_SHADOW_START' undeclared (first use in this function) #define FIXADDR_TOP (KASAN_SHADOW_START - PAGE_SIZE) ^ ./include/asm-generic/fixmap.h:21:27: note: in expansion of macro 'FIXADDR_TOP' #define __fix_to_virt(x) (FIXADDR_TOP - ((x) << PAGE_SHIFT)) ^ ./include/asm-generic/fixmap.h:33:9: note: in expansion of macro '__fix_to_virt' return __fix_to_virt(idx); ^ ./arch/powerpc/include/asm/fixmap.h:28:22: note: each undeclared identifier is reported only once for each function it appears in #define FIXADDR_TOP (KASAN_SHADOW_START - PAGE_SIZE) ^ ./include/asm-generic/fixmap.h:21:27: note: in expansion of macro 'FIXADDR_TOP' #define __fix_to_virt(x) (FIXADDR_TOP - ((x) << PAGE_SHIFT)) ^ ./include/asm-generic/fixmap.h:33:9: note: in expansion of macro '__fix_to_virt' return __fix_to_virt(idx); ^ In file included from ./include/linux/bug.h:5:0, from ./include/linux/thread_info.h:12, from ./include/asm-generic/preempt.h:5, from ./arch/powerpc/include/generated/asm/preempt.h:1, from ./include/linux/preempt.h:78, from ./include/linux/spinlock.h:51, from ./include/linux/seqlock.h:36, from ./include/linux/time.h:6, from ./include/linux/compat.h:10, from arch/powerpc/kernel/asm-offsets.c:14: ./include/asm-generic/fixmap.h: In function 'virt_to_fix': ./arch/powerpc/include/asm/fixmap.h:28:22: error: 'KASAN_SHADOW_START' undeclared (first use in this function) #define FIXADDR_TOP (KASAN_SHADOW_START - PAGE_SIZE) ^ ./arch/powerpc/include/asm/bug.h:73:27: note: in definition of macro 'BUG_ON' if (__builtin_constant_p(x)) { \ ^ ./include/asm-generic/fixmap.h:38:18: note: in expansion of macro 'FIXADDR_TOP' BUG_ON(vaddr >= FIXADDR_TOP || vaddr < FIXADDR_START); ^ In file included from ./arch/powerpc/include/asm/nohash/32/pgtable.h:77:0, from ./arch/powerpc/include/asm/nohash/pgtable.h:8, from ./arch/powerpc/include/asm/pgtable.h:20, from ./arch/powerpc/include/asm/kasan.h:6, from ./include/linux/kasan.h:14, from ./include/linux/slab.h:136, from ./include/linux/crypto.h:19, from ./include/crypto/hash.h:11, from ./include/linux/uio.h:10, from ./include/linux/socket.h:8, from ./include/linux/compat.h:15, from arch/powerpc/kernel/asm-offsets.c:14: ./arch/powerpc/include/asm/fixmap.h: In function '__set_fixmap': ./arch/powerpc/include/asm/fixmap.h:28:22: error: 'KASAN_SHADOW_START' undeclared (first use in this function) #define FIXADDR_TOP (KASAN_SHADOW_START - PAGE_SIZE) ^ ./include/asm-generic/fixmap.h:21:27: note: in expansion of macro 'FIXADDR_TOP' #define __fix_to_virt(x) (FIXADDR_TOP - ((x) << PAGE_SHIFT)) ^ ./arch/powerpc/include/asm/fixmap.h:102:18: note: in expansion of macro '__fix_to_virt' map_kernel_page(__fix_to_virt(idx), phys, flags); ^make[2]: *** [
arch/powerpc/kernel/asm-offsets.s] Error 1 make[1]: *** [prepare0] Error 2
make: *** [sub-make] Error 2Quoted 5 lines
arch/powerpc/include/asm/kasan.h +1
-0+#ifdef CONFIG_KASAN #define _GLOBAL_KASAN (fn) _GLOBAL(__##fn)#define _GLOBAL_TOC_KASAN (fn) _GLOBAL_TOC(__##fn)arch/powerpc/include/asm/kasan.h line 24
Quoted 9 lines
arch/powerpc/include/asm/kasan.h +5
-1@@ -14,13 +17,19 @@#ifndef __ASSEMBLY__ -#include <asm/page.h> +#ifdef CONFIG_KASAN +void kasan_init (void ); +#else +static inline void kasan_init (void ) { } +#endif I don't think it is worth moving this. Just keep everything out of the #ifdef
CONFIG_PPC32. Having undefined/unused functions there shouldn't matter.arch/powerpc/include/asm/kasan.h
Quoted 12 lines
arch/powerpc/include/asm/kasan.h +2
-0#define KASAN_SHADOW_SCALE_SHIFT 3#define KASAN_SHADOW_START (KASAN_SHADOW_OFFSET + \ (PAGE_OFFSET >> KASAN_SHADOW_SCALE_SHIFT)) +#ifdef CONFIG_PPC32 +#define KASAN_SHADOW_OFFSET ASM_CONST(CONFIG_KASAN_SHADOW_OFFSET)#define KASAN_SHADOW_END 0UL@@ -30,11 +39,17 @@Keep the block below out of the
CONFIG_PPC32ifdef, don't need to movekasan_init()Quoted 4 lines
arch/powerpc/include/asm/kasan.h +0
-1#ifdef CONFIG_KASAN void kasan_early_init (void );void kasan_mmu_init (void ); -void kasan_init (void );arch/powerpc/include/asm/kasan.h line 50
Quoted 10 lines
arch/powerpc/include/asm/kasan.h +6
-1#else -static inline void kasan_init (void ) { }static inline void kasan_mmu_init (void ) { }#endif +#endif + +#ifdef CONFIG_PPC_BOOK3S_64 + +#define KASAN_SHADOW_SIZE ((u64)CONFIG_PHYS_MEM_SIZE_FOR_KASAN * \ + 1024 * 1024 * 1 / 8)What about:
(
ASM_CONST(CONFIG_PHYS_MEM_SIZE_FOR_KASAN)*SZ_1G) >>KASAN_SHADOW_SCALE_SHIFTQuoted 45 lines
arch/powerpc/include/asm/kasan.h +2
-0+ +#endif /* CONFIG_PPC_BOOK3S_64 */ #endif /* __ASSEMBLY */ #endif arch/powerpc/kernel/prom.c +21
-0@@ -72,6 +72,7 @@ unsigned long tce_alloc_start, tce_alloc_end;u64 ppc64_rma_size;#endif static phys_addr_t first_memblock_size; +static phys_addr_t top_phys_addr;static int __initdata boot_cpu_count;static int __init early_parse_mem (char *p)@@ -449,6 +450,26 @@ static bool validate_mem_limit(u64 base, u64 *size) {u64 max_mem =1UL << (MAX_PHYSMEM_BITS ); +/* +* To handle the NUMA/discontiguous memory case, don't allow a block +* to be added if it falls completely beyond the configured physical +* memory. Print an informational message. +* +* Frustratingly we also see this with qemu - it seems to split the +* specified memory into a number of smaller blocks. If this happens +* under qemu, it probably represents misconfiguration. So we want +* the message to be noticeable, but not shouty. +* +* See Documentation/powerpc/kasan.txt +*/ +if (IS_ENABLED (CONFIG_KASAN ) && + (base >= ((u64 )CONFIG_PHYS_MEM_SIZE_FOR_KASAN <<20 ))) { +pr_warn ("KASAN: not adding memory block at %llx (size %llx)\n" +"This could be due to discontiguous memory or kernel misconfiguration." , + base, *size); +return false; + } +if (base >= max_mem)return false;if ((base + *size) > max_mem)arch/powerpc/kernel/prom.c void __init early_init_dt_add_memory_arch(u64 base, u64 size) · line 598
Quoted 7 lines
arch/powerpc/kernel/prom.c +2
-1@@ -572,8 +593,10 @@ void __init early_init_dt_add_memory_arch(u64 base, u64 size)/* Add the chunk to the MEMBLOCK list */ if (add_mem_to_memblock) { -if (validate_mem_limit (base, &size)) +if (validate_mem_limit (base, &size)) {memblock_add (base, size); + top_phys_addr =max (top_phys_addr, base + size);Build failure, you have to cast (base + size) to (
phys_addr_t) asphys_addr_tis not always u64.CC arch/powerpc/kernel/asm-offsets.s CALL scripts/checksyscalls.sh CALL scripts/atomic/check-atomics.sh CC arch/powerpc/kernel/prom.oIn file included from
arch/powerpc/kernel/prom.c:15:0:arch/powerpc/kernel/prom.c: In function 'early_init_dt_add_memory_arch':./include/linux/kernel.h:844:29: error: comparison of distinct pointer types lacks a cast [-Werror](!!(sizeof((typeof(x) *)1 == (typeof(y) *)1))) ^ ./include/linux/kernel.h:858:4: note: in expansion of macro '__typecheck' (__typecheck(x, y) && __no_side_effects(x, y)) ^ ./include/linux/kernel.h:868:24: note: in expansion of macro '__safe_cmp' __builtin_choose_expr(__safe_cmp(x, y), \ ^ ./include/linux/kernel.h:884:19: note: in expansion of macro '__careful_cmp' #define max(x, y) __careful_cmp(x, y, >) ^ arch/powerpc/kernel/prom.c:598:20: note: in expansion of macro 'max' top_phys_addr = max(top_phys_addr, base + size); ^cc1: all warnings being treated as errors
make[3]: *** [arch/powerpc/kernel/prom.o] Error 1
make[2]: *** [arch/powerpc/kernel] Error 2
make[1]: *** [arch/powerpc] Error 2
make: *** [sub-make] Error 2Quoted 44 lines
arch/powerpc/kernel/prom.c +30
-0+ } } }@@ -613,6 +636,8 @@ static void __init early_reserve_mem_dt(void)static void __init early_reserve_mem (void ) {__be64 *reserve_map; +phys_addr_t kasan_shadow_start; +phys_addr_t kasan_memory_size; reserve_map = (__be64 *)(((unsigned long )initial_boot_params) +fdt_off_mem_rsvmap (initial_boot_params));@@ -651,6 +676,40 @@ static void __init early_reserve_mem(void) return; }#endif + +if (IS_ENABLED (CONFIG_KASAN ) &&IS_ENABLED (CONFIG_PPC_BOOK3S_64 )) { + kasan_memory_size = + ((phys_addr_t )CONFIG_PHYS_MEM_SIZE_FOR_KASAN <<20 ); + +if (top_phys_addr < kasan_memory_size) { +/* +* We are doomed. We shouldn't even be able to get this +* far, but we do in qemu. If we continue and turn +* relocations on, we'll take fatal page faults for +* memory that's not physically present. Instead, +* panic() here: it will be saved to __log_buf even if +* it doesn't get printed to the console. +*/ +panic ("Tried to boot a KASAN kernel configured for %u MB with only %llu MB! Aborting." , +CONFIG_PHYS_MEM_SIZE_FOR_KASAN , + (u64)(top_phys_addr >>20 )); + }else if (top_phys_addr > kasan_memory_size) { +/* print a biiiig warning in hopes people notice */ +pr_err ("===========================================\n" +"Physical memory exceeds compiled-in maximum!\n" +"This kernel was compiled for KASAN with %u MB physical memory.\n" +"The physical memory detected is at least %llu MB.\n" +"Memory above the compiled limit will not be used!\n" +"===========================================\n" , +CONFIG_PHYS_MEM_SIZE_FOR_KASAN , + (u64)(top_phys_addr >>20 ));arch/powerpc/kernel/prom.c static void __init early_reserve_mem(void) · line 715
Quoted 10 lines
arch/powerpc/kernel/prom.c +7
-0+ } + + kasan_shadow_start =_ALIGN_DOWN (kasan_memory_size *7 /8 ,PAGE_SIZE ); +DBG ("reserving %llx -> %llx for KASAN" , + kasan_shadow_start, top_phys_addr); +memblock_reserve (kasan_shadow_start, top_phys_addr - kasan_shadow_start); + } }#ifdef CONFIG_PPC_TRANSACTIONAL_MEM Christophe
Re: [PATCH v4 4/4] powerpc: Book3S 64-bit "heavyweight" KASAN support
Le 19/12/2019 à 10:50, Daniel Axtens a écrit :
Quoted 133 lines
Quoted 131 lines · Christophe Leroy
Quoted 122 lines · Daniel Axtens
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 in real mode after booting. Most notably a lot of KVM runs in real mode, and it would be good to be able to instrument it.
- Because code runs in real mode after boot, the offset has to point to valid memory both in and out of real mode.
[ppc64 mm note: The kernel installs a linear mapping at effective address c000... onward. 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.]
One approach is just to give up on inline instrumentation. This way all checks can be delayed until after everything set is up correctly, and the address-to-shadow calculations can be overridden. However, the features and speed boost provided by inline instrumentation are worth trying to do better.
If
_atcompiletime_it is known how much contiguous physical memory a system has, the top 1/8th of the first block of physical memory can be set aside for the shadow. This is a big hammer and comes with 3 big consequences:- there's no nice way to handle physically discontiguous memory, so only the first physical memory block can be used.
- kernels will simply fail to boot on machines with less memory than specified when compiling.
- kernels running on machines with more memory than specified when compiling will simply ignore the extra memory.
Implement and document KASAN this way. The current implementation is Radix only.
Despite the limitations, it can still find bugs,
e.g. http://patchwork.ozlabs.org/patch/1103775/At the moment, this physical memory limit must be set
_evenfor outlinemode_. This may be changed in a later series - a different implementation could be added for outline mode that dynamically allocates shadow at a fixed offset. For example, see https://patchwork.ozlabs.org/patch/795211/Suggested-by Michael Ellerman <[email protected]>Cc Balbir Singh <[email protected]> # ppc64 out-of-line radix versionCc Christophe Leroy <[email protected]> # ppc32 versionSigned-off-by Daniel Axtens <[email protected]>---
Changes since v3:
- Address further feedback from Christophe.
- Drop changes to stack walking, it looks like the issue I observed is related to that particular stack, not stack-walking generally.Changes since v2:
- Address feedback from Christophe around cleanups and docs.
- Address feedback from Balbir: at this point I don't have a good solution for the issues you identify around the limitations of the inline implementation but I think that it's worth trying to get the stack instrumentation support. I'm happy to have an alternative and more flexible outline mode - I had envisoned this would be called 'lightweight' mode as it imposes fewer restrictions. I've linked to your implementation. I think it's best to add it in a follow-up series.
- Made the defaultPHYS_MEM_SIZE_FOR_KASANvalue 1024MB. I think most people have guests with at least that much memory in the Radix 64s case so it's a much saner default - it means that if you just turn on KASAN without reading the docs you're much more likely to have a bootable kernel, which you will never have if the value is set to zero! I'm happy to bikeshed the value if we want.Changes since v1:
- Landed kasan vmalloc support upstream
- Lots of feedback from Christophe.Changes since the rfc:
- Boots real and virtual hardware, kvm works.
- disabled reporting when we're checking the stack for exception frames. The behaviour isn't wrong, just incompatible with KASAN.
- Documentation!
- Dropped old module stuff in favour of
KASAN_VMALLOC.The bugs with ftrace and kuap were due to kernel bloat pushing
prom_initcalls to be done via the plt. Because we did not have a relocatable kernel, and they are done very early, this caused everything to explode. Compile withCONFIG_RELOCATABLE!--- Documentation/dev-tools/kasan.rst | 8 +- Documentation/powerpc/kasan.txt | 112 ++++++++++++++++++- arch/powerpc/Kconfig | 2 + arch/powerpc/Kconfig.debug | 21 ++++ arch/powerpc/Makefile | 11 ++ arch/powerpc/include/asm/book3s/64/hash.h | 4 + arch/powerpc/include/asm/book3s/64/pgtable.h | 7 ++ arch/powerpc/include/asm/book3s/64/radix.h | 5 + arch/powerpc/include/asm/kasan.h | 21 +++- arch/powerpc/kernel/prom.c | 61 +++++++++- arch/powerpc/mm/kasan/Makefile | 1 + arch/powerpc/mm/kasan/init_book3s_64.c | 70 ++++++++++++ arch/powerpc/platforms/Kconfig.cputype | 1 + 13 files changed, 316 insertions(+), 8 deletions(-)create mode 100644
arch/powerpc/mm/kasan/init_book3s_64.carch/powerpc/include/asm/kasan.h +0
-0arch/powerpc/include/asm/kasan.h line 6
Quoted 5 lines
arch/powerpc/include/asm/kasan.h +2
-0@@ -2,6 +2,9 @@#ifndef __ASM_KASAN_H#define __ASM_KASAN_H +#include <asm/page.h> +#include <asm/pgtable.h> What do you need
asm/pgtable.hfor ?
Quoted 6 lines
Quoted 1 line
Build failure due to circular inclusion of
asm/pgtable.h:I see there's a lot of ppc32 stuff, I clearly need to bite the bullet and get a ppc32 toolchain so I can squash these without chewing up any more of your time. I'll sort that out and send a new spin.
I'm using a powerpc64 toolchain to build both ppc32 and ppc64 kernels (from https://mirrors.edge.kernel.org/pub/tools/crosstool/ )
Another thing, did you test PTDUMP stuff with KASAN ? It looks like KASAN address markers don't depend on PPC32, but are only initialised by populate_markers() for PPC32.
Regarding kasan.h, I think we should be able to end up with something where the definition of KASAN_SHADOW_OFFSET should only depend on the existence of CONFIG_KASAN_SHADOW_OFFSET, and where only KASAN_SHADOW_SIZE should depend on the target (ie PPC32 or BOOK3S64) Everything else should be common. KASAN_END should be START+SIZE.
It looks like what you have called KASAN_SHADOW_SIZE is not similar to what is called KASAN_SHADOW_SIZE for PPC32, as yours only covers the SHADOW_SIZE for linear mem while PPC32 one covers the full space.
Christophe
Re: [PATCH v4 4/4] powerpc: Book3S 64-bit "heavyweight" KASAN support
Quoted 147 lines · Christophe Leroy
Le 19/12/2019 à 10:50, Daniel Axtens a écrit :
Quoted 134 lines
Quoted 132 lines · Christophe Leroy
Quoted 124 lines · Daniel Axtens
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 in real mode after booting. Most notably a lot of KVM runs in real mode, and it would be good to be able to instrument it.
- Because code runs in real mode after boot, the offset has to point to valid memory both in and out of real mode.
[ppc64 mm note: The kernel installs a linear mapping at effective address c000... onward. 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.]
One approach is just to give up on inline instrumentation. This way all checks can be delayed until after everything set is up correctly, and the address-to-shadow calculations can be overridden. However, the features and speed boost provided by inline instrumentation are worth trying to do better.
If
_atcompiletime_it is known how much contiguous physical memory a system has, the top 1/8th of the first block of physical memory can be set aside for the shadow. This is a big hammer and comes with 3 big consequences:- there's no nice way to handle physically discontiguous memory, so only the first physical memory block can be used.
- kernels will simply fail to boot on machines with less memory than specified when compiling.
- kernels running on machines with more memory than specified when compiling will simply ignore the extra memory.
Implement and document KASAN this way. The current implementation is Radix only.
Despite the limitations, it can still find bugs,
e.g. http://patchwork.ozlabs.org/patch/1103775/At the moment, this physical memory limit must be set
_evenfor outlinemode_. This may be changed in a later series - a different implementation could be added for outline mode that dynamically allocates shadow at a fixed offset. For example, see https://patchwork.ozlabs.org/patch/795211/Suggested-by Michael Ellerman <[email protected]>Cc Balbir Singh <[email protected]> # ppc64 out-of-line radix versionCc Christophe Leroy <[email protected]> # ppc32 versionSigned-off-by Daniel Axtens <[email protected]>---
Changes since v3:
- Address further feedback from Christophe.
- Drop changes to stack walking, it looks like the issue I observed is related to that particular stack, not stack-walking generally.Changes since v2:
- Address feedback from Christophe around cleanups and docs.
- Address feedback from Balbir: at this point I don't have a good solution for the issues you identify around the limitations of the inline implementation but I think that it's worth trying to get the stack instrumentation support. I'm happy to have an alternative and more flexible outline mode
- I had
envisoned this would be called 'lightweight' mode as it imposes fewer restrictions. I've linked to your implementation. I think it's best to add it in a follow-up series.
- Made the defaultPHYS_MEM_SIZE_FOR_KASANvalue 1024MB. I think most people have guests with at least that much memory in the Radix 64s case so it's a much saner default - it means that if you just turn on KASAN without reading the docs you're much more likely to have a bootable kernel, which you will never have if the value is set to zero! I'm happy to bikeshed the value if we want.Changes since v1:
- Landed kasan vmalloc support upstream
- Lots of feedback from Christophe.Changes since the rfc:
- Boots real and virtual hardware, kvm works.
- disabled reporting when we're checking the stack for exception frames. The behaviour isn't wrong, just incompatible with KASAN.
- Documentation!
- Dropped old module stuff in favour of
KASAN_VMALLOC.The bugs with ftrace and kuap were due to kernel bloat pushing
prom_initcalls to be done via the plt. Because we did not have a relocatable kernel, and they are done very early, this caused everything to explode. Compile withCONFIG_RELOCATABLE!
---
Documentation/dev-tools/kasan.rst | 8 +- Documentation/powerpc/kasan.txt | 112
++++++++++++++++++-
arch/powerpc/Kconfig | 2 + arch/powerpc/Kconfig.debug | 21 ++++ arch/powerpc/Makefile | 11 ++ arch/powerpc/include/asm/book3s/64/hash.h | 4 +arch/powerpc/include/asm/book3s/64/pgtable.h| 7 ++ arch/powerpc/include/asm/book3s/64/radix.h | 5 + arch/powerpc/include/asm/kasan.h | 21 +++- arch/powerpc/kernel/prom.c | 61 +++++++++- arch/powerpc/mm/kasan/Makefile | 1 + arch/powerpc/mm/kasan/init_book3s_64.c | 70 ++++++++++++ arch/powerpc/platforms/Kconfig.cputype | 1 +13 files changed, 316 insertions(+), 8 deletions(-)create mode 100644
arch/powerpc/mm/kasan/init_book3s_64.carch/powerpc/include/asm/kasan.h +0
-0arch/powerpc/include/asm/kasan.h line 5
Quoted 4 lines
arch/powerpc/include/asm/kasan.h +2
-0@@ -2,6 +2,9 @@ #ifndef __ASM_KASAN_H #define __ASM_KASAN_H +#include <asm/page.h> +#include <asm/pgtable.h> What do you need
asm/pgtable.hfor ?Quoted 6 lines
Quoted 1 line
Build failure due to circular inclusion of
asm/pgtable.h:I see there's a lot of ppc32 stuff, I clearly need to bite the bullet and get a ppc32 toolchain so I can squash these without chewing up any more of your time. I'll sort that out and send a new spin.
I'm using a powerpc64 toolchain to build both ppc32 and ppc64 kernels (from https://mirrors.edge.kernel.org/pub/tools/crosstool/ )
Quoted 7 lines
Another thing, did you test PTDUMP stuff with KASAN ? It looks like KASAN address markers don't depend on PPC32, but are only initialised by
populate_markers()for PPC32.Regarding
kasan.h, I think we should be able to end up with something where the definition ofKASAN_SHADOW_OFFSETshould only depend on the existence ofCONFIG_KASAN_SHADOW_OFFSET, and where onlyKASAN_SHADOW_SIZEshould depend on the target (ie PPC32 or BOOK3S64) Everything else should be common.KASAN_ENDshould be START+SIZE.It looks like what you have called
KASAN_SHADOW_SIZEis not similar to what is calledKASAN_SHADOW_SIZEfor PPC32, as yours only covers theSHADOW_SIZEfor linear mem while PPC32 one covers the full space.
More or less something like that:
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __ASM_KASAN_H
#define __ASM_KASAN_H
#include <asm/page.h>
#ifdef CONFIG_KASAN
#define _GLOBAL_KASAN(fn) _GLOBAL(__##fn)
#define _GLOBAL_TOC_KASAN(fn) _GLOBAL_TOC(__##fn)
#define EXPORT_SYMBOL_KASAN(fn) EXPORT_SYMBOL(__##fn)
#else
#define _GLOBAL_KASAN(fn) _GLOBAL(fn)
#define _GLOBAL_TOC_KASAN(fn) _GLOBAL_TOC(fn)
#define EXPORT_SYMBOL_KASAN(fn)
#endif
#ifndef __ASSEMBLY__
#define KASAN_SHADOW_SCALE_SHIFT 3
#define KASAN_SHADOW_START (KASAN_SHADOW_OFFSET + \
(PAGE_OFFSET >> KASAN_SHADOW_SCALE_SHIFT))
#define KASAN_SHADOW_END (KASAN_SHADOW_START + KASAN_SHADOW_SIZE)
#ifdef CONFIG_KASAN_SHADOW_OFFSET
#define KASAN_SHADOW_OFFSET ASM_CONST(CONFIG_KASAN_SHADOW_OFFSET)
#endif
#ifdef CONFIG_PPC32
#define KASAN_SHADOW_SIZE ((-PAGE_OFFSET) >> KASAN_SHADOW_SCALE_SHIFT)
#endif
#ifdef CONFIG_PPC_BOOK3S_64
#define KASAN_SHADOW_SIZE (ASM_CONST(CONFIG_PHYS_MEM_SIZE_FOR_KASAN) * SZ_1G) >> \
KASAN_SHADOW_SCALE_SHIFT)
#endif
#ifdef CONFIG_KASAN
void kasan_early_init(void);
void kasan_mmu_init(void);
void kasan_init(void);
#else
static inline void kasan_init(void) { }
static inline void kasan_mmu_init(void) { }
#endif
#endif /* __ASSEMBLY */
#endif
Christophe
Re: [PATCH v4 4/4] powerpc: Book3S 64-bit "heavyweight" KASAN support
In reply to Christophe LeroyQuoted 136 lines · Christophe Leroy
Le 19/12/2019 à 10:50, Daniel Axtens a écrit :
Quoted 135 lines
Quoted 132 lines · Christophe Leroy
Quoted 122 lines · Daniel Axtens
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 in real mode after booting. Most notably a lot of KVM runs in real mode, and it would be good to be able to instrument it.
- Because code runs in real mode after boot, the offset has to point to valid memory both in and out of real mode.
[ppc64 mm note: The kernel installs a linear mapping at effective address c000... onward. 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.]
One approach is just to give up on inline instrumentation. This way all checks can be delayed until after everything set is up correctly, and the address-to-shadow calculations can be overridden. However, the features and speed boost provided by inline instrumentation are worth trying to do better.
If
_atcompiletime_it is known how much contiguous physical memory a system has, the top 1/8th of the first block of physical memory can be set aside for the shadow. This is a big hammer and comes with 3 big consequences:- there's no nice way to handle physically discontiguous memory, so only the first physical memory block can be used.
- kernels will simply fail to boot on machines with less memory than specified when compiling.
- kernels running on machines with more memory than specified when compiling will simply ignore the extra memory.
Implement and document KASAN this way. The current implementation is Radix only.
Despite the limitations, it can still find bugs,
e.g. http://patchwork.ozlabs.org/patch/1103775/At the moment, this physical memory limit must be set
_evenfor outlinemode_. This may be changed in a later series - a different implementation could be added for outline mode that dynamically allocates shadow at a fixed offset. For example, see https://patchwork.ozlabs.org/patch/795211/Suggested-by Michael Ellerman <[email protected]>Cc Balbir Singh <[email protected]> # ppc64 out-of-line radix versionCc Christophe Leroy <[email protected]> # ppc32 versionSigned-off-by Daniel Axtens <[email protected]>---
Changes since v3:
- Address further feedback from Christophe.
- Drop changes to stack walking, it looks like the issue I observed is related to that particular stack, not stack-walking generally.Changes since v2:
- Address feedback from Christophe around cleanups and docs.
- Address feedback from Balbir: at this point I don't have a good solution for the issues you identify around the limitations of the inline implementation but I think that it's worth trying to get the stack instrumentation support. I'm happy to have an alternative and more flexible outline mode - I had envisoned this would be called 'lightweight' mode as it imposes fewer restrictions. I've linked to your implementation. I think it's best to add it in a follow-up series.
- Made the defaultPHYS_MEM_SIZE_FOR_KASANvalue 1024MB. I think most people have guests with at least that much memory in the Radix 64s case so it's a much saner default - it means that if you just turn on KASAN without reading the docs you're much more likely to have a bootable kernel, which you will never have if the value is set to zero! I'm happy to bikeshed the value if we want.Changes since v1:
- Landed kasan vmalloc support upstream
- Lots of feedback from Christophe.Changes since the rfc:
- Boots real and virtual hardware, kvm works.
- disabled reporting when we're checking the stack for exception frames. The behaviour isn't wrong, just incompatible with KASAN.
- Documentation!
- Dropped old module stuff in favour of
KASAN_VMALLOC.The bugs with ftrace and kuap were due to kernel bloat pushing
prom_initcalls to be done via the plt. Because we did not have a relocatable kernel, and they are done very early, this caused everything to explode. Compile withCONFIG_RELOCATABLE!--- Documentation/dev-tools/kasan.rst | 8 +- Documentation/powerpc/kasan.txt | 112 ++++++++++++++++++- arch/powerpc/Kconfig | 2 + arch/powerpc/Kconfig.debug | 21 ++++ arch/powerpc/Makefile | 11 ++ arch/powerpc/include/asm/book3s/64/hash.h | 4 + arch/powerpc/include/asm/book3s/64/pgtable.h | 7 ++ arch/powerpc/include/asm/book3s/64/radix.h | 5 + arch/powerpc/include/asm/kasan.h | 21 +++- arch/powerpc/kernel/prom.c | 61 +++++++++- arch/powerpc/mm/kasan/Makefile | 1 + arch/powerpc/mm/kasan/init_book3s_64.c | 70 ++++++++++++ arch/powerpc/platforms/Kconfig.cputype | 1 + 13 files changed, 316 insertions(+), 8 deletions(-)create mode 100644
arch/powerpc/mm/kasan/init_book3s_64.carch/powerpc/include/asm/kasan.h +0
-0arch/powerpc/include/asm/kasan.h line 6
Quoted 5 lines
arch/powerpc/include/asm/kasan.h +2
-0@@ -2,6 +2,9 @@#ifndef __ASM_KASAN_H#define __ASM_KASAN_H +#include <asm/page.h> +#include <asm/pgtable.h> What do you need
asm/pgtable.hfor ?Build failure due to circular inclusion of
asm/pgtable.h:
Quoted 7 lines
Quoted 4 lines
I see there's a lot of ppc32 stuff, I clearly need to bite the bullet and get a ppc32 toolchain so I can squash these without chewing up any more of your time. I'll sort that out and send a new spin.
I'm using a powerpc64 toolchain to build both ppc32 and ppc64 kernels (from https://mirrors.edge.kernel.org/pub/tools/crosstool/ )
I am now using the distro toolchain that Ubuntu provides, and I've reproduced and fixed the 32bit issues you identifed.
Quoted 3 lines
Another thing, did you test PTDUMP stuff with KASAN ? It looks like KASAN address markers don't depend on PPC32, but are only initialised by
populate_markers()for PPC32.
Hmm, OK. This is my last workday for the year, so I will look at this and the simplifications to kasan.h early next year.
Thanks,
Daniel
Quoted 12 lines
Regarding
kasan.h, I think we should be able to end up with something where the definition ofKASAN_SHADOW_OFFSETshould only depend on the existence ofCONFIG_KASAN_SHADOW_OFFSET, and where onlyKASAN_SHADOW_SIZEshould depend on the target (ie PPC32 or BOOK3S64) Everything else should be common.KASAN_ENDshould be START+SIZE.It looks like what you have called
KASAN_SHADOW_SIZEis not similar to what is calledKASAN_SHADOW_SIZEfor PPC32, as yours only covers theSHADOW_SIZEfor linear mem while PPC32 one covers the full space.Christophe
Michael Ellerman
reviewerRe: [PATCH v4 4/4] powerpc: Book3S 64-bit "heavyweight" KASAN support
In reply to Daniel AxtensQuoted 3 lines · Daniel Axtens
Quoted 2 lines · Christophe Leroy
Quoted 1 line · Daniel Axtens
KASAN support on Book3S is a bit tricky to get right:
...
Quoted 13 lines
Quoted 13 lines
arch/powerpc/include/asm/kasan.h line 6
Quoted 10 lines
arch/powerpc/include/asm/kasan.h +2
-0@@ -2,6 +2,9 @@#ifndef __ASM_KASAN_H#define __ASM_KASAN_H +#include <asm/page.h> +#include <asm/pgtable.h> What do you need
asm/pgtable.hfor ?
Quoted 5 lines
Quoted 1 line
Build failure due to circular inclusion of
asm/pgtable.h:I see there's a lot of ppc32 stuff, I clearly need to bite the bullet and get a ppc32 toolchain so I can squash these without chewing up any more of your time. I'll sort that out and send a new spin.
I think you run Ubuntu, in which case it should just be:
$ apt install gcc-powerpc-linux-gnu
cheers