Skip to contentSkip to conversation

KASAN for powerpc64 radix

PATCH Superseded Daniel Axtens 2 participants 6 messages Jul 2, 2020 – Jul 6, 2020

Subsystems (5)

v8 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#0 lore raw

v8 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.

v8 is just a rebase of v7 on a more recent powerpc/merge and a fixup of a whitespace error.

Module globals still don't work, but that's due to some 'clever' renaming of a section that the powerpc module loading code does to avoid more complicated relocations/tramplines rather than anything to do with KASAN.

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             |   8 +-
 Documentation/powerpc/kasan.txt               | 122 ++++++++++++++++++
 arch/powerpc/Kconfig                          |   3 +-
 arch/powerpc/Kconfig.debug                    |  23 +++-
 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              |  11 +-
 arch/powerpc/kernel/Makefile                  |   2 +
 arch/powerpc/kernel/process.c                 |  16 ++-
 arch/powerpc/kernel/prom.c                    |  76 ++++++++++-
 arch/powerpc/mm/kasan/Makefile                |   3 +-
 .../mm/kasan/{kasan_init_32.c => init_32.c}   |   0
 arch/powerpc/mm/kasan/init_book3s_64.c        |  73 +++++++++++
 arch/powerpc/mm/ptdump/ptdump.c               |  10 +-
 arch/powerpc/platforms/Kconfig.cputype        |   1 +
 include/linux/kasan.h                         |  18 ++-
 mm/kasan/init.c                               |   6 +-
 19 files changed, 377 insertions(+), 22 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.c
Signature · 1 line
2.25.1
#1 lore raw

PATCH v8 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
index 82522e996c76..b6f94952333b 100644
@@ -14,10 +14,22 @@ struct task_struct;
 #include <linux/pgtable.h>
 #include <asm/kasan.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
index fe6be0be1f76..42bca3d27db8 100644
@@ -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.25.1
#2 lore raw

PATCH v8 2/4 kasan: Document support on 32-bit powerpc

In reply to Daniel Axtens

KASAN is supported on 32-bit powerpc and the docs should reflect this.

Document s390 support while we're at it.

---
 Documentation/dev-tools/kasan.rst |  7 +++++--
 Documentation/powerpc/kasan.txt   | 12 ++++++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/powerpc/kasan.txt
Documentation/dev-tools/kasan.rst +5 -2
index c652d740735d..554cbee1d240 100644
@@ -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, s390 and
-riscv architectures, and tag-based KASAN is supported only for arm64.
+riscv architectures. It is also supported on 32-bit powerpc kernels. Tag-based
+KASAN is supported only on arm64.
 
 Usage
 -----
@@ -255,7 +256,9 @@ 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 supported on x86, s390
+and 32-bit powerpc. It is optional, except 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.
Documentation/powerpc/kasan.txt new file +12 -0
new file mode 100644
index 000000000000..26bb0e8bb18c
@@ -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.25.1
#3 lore raw

PATCH v8 3/4 powerpc/mm/kasan: rename kasan_init_32.c to init_32.c

In reply to Daniel Axtens

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
index bb1a5408b86b..42fb628a44fd 100644
@@ -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
similarity index 100%
rename from arch/powerpc/mm/kasan/kasan_init_32.c
rename to arch/powerpc/mm/kasan/init_32.c
Signature · 1 line
2.25.1
#4 lore raw

PATCH v8 4/4 powerpc: Book3S 64-bit "heavyweight" KASAN support

In reply to Daniel Axtens

Implement a limited form of KASAN for Book3S 64-bit machines running under the Radix MMU:

- Set aside the last 1/8th of the first contiguous block of physical memory to provide writable shadow for the linear map. For annoying reasons documented below, the memory size must be specified at compile time.

- 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 back all kernel addresses with real memory - only linear map and vmalloc. On boot, set up a single page of read-only shadow that marks all these accesses as valid.

- Make our stack-walking code KASAN-safe by using READ_ONCE_NOCHECK - generic code, arm64, s390 and x86 all do this for similar sorts of
reasons: when unwinding a stack, we might touch memory that KASAN has marked as being out-of-bounds. In our case we often get this when checking for an exception frame because we're checking an arbitrary offset into the stack frame.

See commit 20955746320e ("s390/kasan: avoid false positives during stack unwind"), commit bcaf669b4bdb ("arm64: disable kasan when accessing frame->fp in unwind_frame"), commit 91e08ab0c851 ("x86/dumpstack:
Prevent KASAN false positive warnings") and commit 6e22c8366416 ("tracing, kasan: Silence Kasan warning in check_stack of stack_tracer")

- Document KASAN in both generic and 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 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.

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/

Cc Balbir Singh <[email protected]> # ppc64 out-of-line radix version
Cc Christophe Leroy <[email protected]> # ppc32 version

and things impacting PPC32

---
 Documentation/dev-tools/kasan.rst            |   9 +-
 Documentation/powerpc/kasan.txt              | 112 ++++++++++++++++++-
 arch/powerpc/Kconfig                         |   3 +-
 arch/powerpc/Kconfig.debug                   |  23 +++-
 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             |  11 +-
 arch/powerpc/kernel/Makefile                 |   2 +
 arch/powerpc/kernel/process.c                |  16 +--
 arch/powerpc/kernel/prom.c                   |  76 ++++++++++++-
 arch/powerpc/mm/kasan/Makefile               |   1 +
 arch/powerpc/mm/kasan/init_book3s_64.c       |  73 ++++++++++++
 arch/powerpc/mm/ptdump/ptdump.c              |  10 +-
 arch/powerpc/platforms/Kconfig.cputype       |   1 +
 16 files changed, 346 insertions(+), 18 deletions(-)
 create mode 100644 arch/powerpc/mm/kasan/init_book3s_64.c
Documentation/dev-tools/kasan.rst +5 -4
index 554cbee1d240..5722de91ccce 100644
@@ -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, s390 and
-riscv architectures. It is also supported on 32-bit powerpc kernels. Tag-based
-KASAN is supported only on arm64.
+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 is supported
+only on arm64.
 
 Usage
 -----
@@ -257,8 +258,8 @@ CONFIG_KASAN_VMALLOC
 
 With ``CONFIG_KASAN_VMALLOC``, KASAN can cover vmalloc space at the
 cost of greater memory usage. Currently this supported on x86, s390
-and 32-bit powerpc. It is optional, except on 32-bit powerpc kernels
-with module support, where it is required.
+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.
Documentation/powerpc/kasan.txt +111 -1

118 lines not on this page · raw message

arch/powerpc/Kconfig +2 -1
index 51abc59c3334..fe04704a05eb 100644
@@ -172,7 +172,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 && PPC_PAGE_SHIFT <= 14
-	select HAVE_ARCH_KASAN_VMALLOC		if PPC32 && PPC_PAGE_SHIFT <= 14
+	select HAVE_ARCH_KASAN			if PPC_BOOK3S_64 && PPC_RADIX_MMU
+	select HAVE_ARCH_KASAN_VMALLOC		if HAVE_ARCH_KASAN
 	select HAVE_ARCH_KGDB
 	select HAVE_ARCH_MMAP_RND_BITS
 	select HAVE_ARCH_MMAP_RND_COMPAT_BITS	if COMPAT
arch/powerpc/Kconfig.debug +22 -1
index b88900f4832f..22cc80c22ff9 100644
@@ -394,7 +394,28 @@ 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
+	depends on KASAN && PPC32
 	default 0xe0000000
arch/powerpc/Makefile +11 -0
index 3e8da9cf2eb9..7a8e4739bec4 100644
@@ -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
index 73ad038ed10b..105b90594a8a 100644
@@ -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
index 25c3cb8272c0..42edd172178a 100644
@@ -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
index 0cba794c4fb8..8d41f4e657c2 100644
@@ -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 +10 -1
index be85c7005fb1..3b57aee0d461 100644
@@ -21,9 +21,18 @@
 #define KASAN_SHADOW_START	(KASAN_SHADOW_OFFSET + \
 				 (PAGE_OFFSET >> KASAN_SHADOW_SCALE_SHIFT))
 
+#ifdef CONFIG_KASAN_SHADOW_OFFSET
 #define KASAN_SHADOW_OFFSET	ASM_CONST(CONFIG_KASAN_SHADOW_OFFSET)
+#endif
 
+#ifdef CONFIG_PPC32
 #define KASAN_SHADOW_END	(-(-KASAN_SHADOW_START >> KASAN_SHADOW_SCALE_SHIFT))
+#endif
+
+#ifdef CONFIG_PPC_BOOK3S_64
+#define KASAN_SHADOW_END	(KASAN_SHADOW_OFFSET + \
+				 (RADIX_VMEMMAP_END >> KASAN_SHADOW_SCALE_SHIFT))
+#endif
 
 #ifdef CONFIG_KASAN
 void kasan_early_init(void);
@@ -38,5 +47,5 @@ void kasan_update_early_region(unsigned long k_start, unsigned long k_end, pte_t
 int kasan_init_shadow_page_tables(unsigned long k_start, unsigned long k_end);
 int kasan_init_region(void *start, size_t size);
 
-#endif /* __ASSEMBLY */
+#endif /* !__ASSEMBLY__ */
 #endif
arch/powerpc/kernel/Makefile +2 -0
index 244542ae2a91..70a6e386c98d 100644
@@ -32,6 +32,8 @@ 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
 
 ifdef CONFIG_KASAN
 CFLAGS_early_32.o += -DDISABLE_BRANCH_PROFILING
arch/powerpc/kernel/process.c +9 -7
index 4650b9bb217f..a8b3690f41bf 100644
@@ -2097,8 +2097,8 @@ void show_stack(struct task_struct *tsk, unsigned long *stack,
 			break;
 
 		stack = (unsigned long *) sp;
-		newsp = stack[0];
-		ip = stack[STACK_FRAME_LR_SAVE];
+		newsp = READ_ONCE_NOCHECK(stack[0]);
+		ip = READ_ONCE_NOCHECK(stack[STACK_FRAME_LR_SAVE]);
 		if (!firstframe || ip != lr) {
 			printk("%s["REG"] ["REG"] %pS",
 				loglvl, sp, ip, (void *)ip);
@@ -2118,14 +2118,16 @@ void show_stack(struct task_struct *tsk, unsigned long *stack,
 		 * See if this is an exception frame.
 		 * We look for the "regshere" marker in the current frame.
 		 */
-		if (validate_sp(sp, tsk, STACK_INT_FRAME_SIZE)
-		    && stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
+		if (validate_sp(sp, tsk, STACK_INT_FRAME_SIZE) &&
+		    (READ_ONCE_NOCHECK(stack[STACK_FRAME_MARKER]) ==
+		     STACK_FRAME_REGS_MARKER)) {
 			struct pt_regs *regs = (struct pt_regs *)
 				(sp + STACK_FRAME_OVERHEAD);
-			lr = regs->link;
+			lr = READ_ONCE_NOCHECK(regs->link);
 			printk("%s--- interrupt: %lx at %pS\n    LR = %pS\n",
-			       loglvl, regs->trap,
-			       (void *)regs->nip, (void *)lr);
+			       loglvl, READ_ONCE_NOCHECK(regs->trap),
+			       (void *)READ_ONCE_NOCHECK(regs->nip),
+			       (void *)READ_ONCE_NOCHECK(lr));
 			firstframe = 1;
 		}
 
arch/powerpc/kernel/prom.c +74 -2

119 lines not on this page · raw message

arch/powerpc/mm/kasan/Makefile +1 -0
index 42fb628a44fd..33c92e7012af 100644
@@ -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 +73 -0

73 lines not on this page · raw message

arch/powerpc/mm/ptdump/ptdump.c +9 -1
index de6e05ef871c..60b8f61491b3 100644
@@ -74,6 +74,10 @@ struct addr_marker {
 
 static struct addr_marker address_markers[] = {
 	{ 0,	"Start of kernel VM" },
+#if defined(CONFIG_PPC64) && defined(CONFIG_KASAN)
+	{ 0,	"kasan shadow mem start" },
+	{ 0,	"kasan shadow mem end" },
+#endif
 	{ 0,	"vmalloc() Area" },
 	{ 0,	"vmalloc() End" },
 #ifdef CONFIG_PPC64
@@ -93,10 +97,10 @@ static struct addr_marker address_markers[] = {
 #endif
 	{ 0,	"Fixmap start" },
 	{ 0,	"Fixmap end" },
-#endif
 #ifdef CONFIG_KASAN
 	{ 0,	"kasan shadow mem start" },
 	{ 0,	"kasan shadow mem end" },
+#endif
 #endif
 	{ -1,	NULL },
 };
@@ -349,6 +353,10 @@ static void populate_markers(void)
 	int i = 0;
 
 	address_markers[i++].start_address = PAGE_OFFSET;
+#if defined(CONFIG_PPC64) && defined(CONFIG_KASAN)
+	address_markers[i++].start_address = KASAN_SHADOW_START;
+	address_markers[i++].start_address = KASAN_SHADOW_END;
+#endif
 	address_markers[i++].start_address = VMALLOC_START;
 	address_markers[i++].start_address = VMALLOC_END;
 #ifdef CONFIG_PPC64
arch/powerpc/platforms/Kconfig.cputype +1 -0
index 87737ec86d39..b430e4314b56 100644
@@ -99,6 +99,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.25.1
#5 lore raw

Re: [PATCH v8 4/4] powerpc: Book3S 64-bit "heavyweight" KASAN support

Thank you for the patch! Perhaps something to improve:

Hi Daniel,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on powerpc/next]
[also build test WARNING on linux/master linus/master v5.8-rc4 next-20200703] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Daniel-Axtens/KASAN-for-powerpc64-radix/20200702-105552
base:   https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git next
config: powerpc64-randconfig-s031-20200706 (attached as .config)
compiler: powerpc64le-linux-gcc (GCC) 9.3.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# apt-get install sparse
# sparse version: v0.6.2-14-g8fce3d7a-dirty
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' ARCH=powerpc64

If you fix the issue, kindly add following tag as appropriate

sparse warnings: (new ones prefixed by >>)

drivers/char/tpm/tpm_ibmvtpm.c:130:9: sparse: sparse: cast removes address space '__iomem' of expression

>> drivers/char/tpm/tpm_ibmvtpm.c:131:9: sparse: sparse: incorrect type in argument 1 (different address spaces) @@     expected void *s @@     got void [noderef] __iomem *rtce_buf @@
>> drivers/char/tpm/tpm_ibmvtpm.c:131:9: sparse:     expected void *s

drivers/char/tpm/tpm_ibmvtpm.c:131:9: sparse: got void [noderef] __iomem *rtce_buf
drivers/char/tpm/tpm_ibmvtpm.c:234:9: sparse: sparse: cast removes address space '__iomem' of expression
drivers/char/tpm/tpm_ibmvtpm.c:369:30: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected void const * @@ got void [noderef] __iomem *rtce_buf @@
drivers/char/tpm/tpm_ibmvtpm.c:369:30: sparse: expected void const *
drivers/char/tpm/tpm_ibmvtpm.c:369:30: sparse: got void [noderef] __iomem *rtce_buf
drivers/char/tpm/tpm_ibmvtpm.c:530:43: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected void [noderef] __iomem *rtce_buf @@ got void * @@
drivers/char/tpm/tpm_ibmvtpm.c:530:43: sparse: expected void [noderef] __iomem *rtce_buf
drivers/char/tpm/tpm_ibmvtpm.c:530:43: sparse: got void *
drivers/char/tpm/tpm_ibmvtpm.c:537:52: sparse: sparse: incorrect type in argument 2 (different address spaces) @@ expected void *ptr @@ got void [noderef] __iomem *rtce_buf @@
drivers/char/tpm/tpm_ibmvtpm.c:537:52: sparse: expected void *ptr
drivers/char/tpm/tpm_ibmvtpm.c:537:52: sparse: got void [noderef] __iomem *rtce_buf
drivers/char/tpm/tpm_ibmvtpm.c:543:46: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected void const * @@ got void [noderef] __iomem *rtce_buf @@
drivers/char/tpm/tpm_ibmvtpm.c:543:46: sparse: expected void const *
drivers/char/tpm/tpm_ibmvtpm.c:543:46: sparse: got void [noderef] __iomem *rtce_buf
--
drivers/tty/vt/vt.c:233:5: sparse: sparse: symbol 'console_blank_hook' was not declared. Should it be static? drivers/tty/vt/vt.c:2901:19: sparse: sparse: symbol 'console_driver' was not declared. Should it be static? arch/powerpc/include/asm/vga.h:34:16: sparse: sparse: cast to restricted __le16 arch/powerpc/include/asm/vga.h:34:16: sparse: sparse: cast to restricted __le16 arch/powerpc/include/asm/vga.h:34:16: sparse: sparse: cast to restricted __le16

>> arch/powerpc/include/asm/vga.h:40:21: sparse: sparse: incorrect type in argument 2 (different base types) @@     expected unsigned short [usertype] @@     got restricted __le16 [usertype] @@
>> arch/powerpc/include/asm/vga.h:40:21: sparse:     expected unsigned short [usertype]
   arch/powerpc/include/asm/vga.h:40:21: sparse:     got restricted __le16 [usertype]
   arch/powerpc/include/asm/vga.h:34:16: sparse: sparse: cast to restricted __le16
   arch/powerpc/include/asm/vga.h:34:16: sparse: sparse: cast to restricted __le16
   arch/powerpc/include/asm/vga.h:34:16: sparse: sparse: cast to restricted __le16
   arch/powerpc/include/asm/vga.h:34:16: sparse: sparse: cast to restricted __le16
   arch/powerpc/include/asm/vga.h:29:15: sparse: sparse: incorrect type in assignment (different base types) @@     expected unsigned short volatile [usertype] @@     got restricted __le16 [usertype] @@
   arch/powerpc/include/asm/vga.h:29:15: sparse:     expected unsigned short volatile [usertype]
   arch/powerpc/include/asm/vga.h:29:15: sparse:     got restricted __le16 [usertype]
   arch/powerpc/include/asm/vga.h:34:16: sparse: sparse: cast to restricted __le16
   arch/powerpc/include/asm/vga.h:29:15: sparse: sparse: incorrect type in assignment (different base types) @@     expected unsigned short volatile [usertype] @@     got restricted __le16 [usertype] @@
   arch/powerpc/include/asm/vga.h:29:15: sparse:     expected unsigned short volatile [usertype]
   arch/powerpc/include/asm/vga.h:29:15: sparse:     got restricted __le16 [usertype]
   arch/powerpc/include/asm/vga.h:34:16: sparse: sparse: cast to restricted __le16
   arch/powerpc/include/asm/vga.h:29:15: sparse: sparse: incorrect type in assignment (different base types) @@     expected unsigned short volatile [usertype] @@     got restricted __le16 [usertype] @@
   arch/powerpc/include/asm/vga.h:29:15: sparse:     expected unsigned short volatile [usertype]
   arch/powerpc/include/asm/vga.h:29:15: sparse:     got restricted __le16 [usertype]
   arch/powerpc/include/asm/vga.h:29:15: sparse: sparse: incorrect type in assignment (different base types) @@     expected unsigned short volatile [usertype] @@     got restricted __le16 [usertype] @@
   arch/powerpc/include/asm/vga.h:29:15: sparse:     expected unsigned short volatile [usertype]
   arch/powerpc/include/asm/vga.h:29:15: sparse:     got restricted __le16 [usertype]
   arch/powerpc/include/asm/vga.h:34:16: sparse: sparse: cast to restricted __le16
   arch/powerpc/include/asm/vga.h:29:15: sparse: sparse: incorrect type in assignment (different base types) @@     expected unsigned short volatile [usertype] @@     got restricted __le16 [usertype] @@
   arch/powerpc/include/asm/vga.h:29:15: sparse:     expected unsigned short volatile [usertype]
   arch/powerpc/include/asm/vga.h:29:15: sparse:     got restricted __le16 [usertype]
>> arch/powerpc/include/asm/vga.h:40:21: sparse: sparse: incorrect type in argument 2 (different base types) @@     expected unsigned short [usertype] @@     got restricted __le16 [usertype] @@
>> arch/powerpc/include/asm/vga.h:40:21: sparse:     expected unsigned short [usertype]
   arch/powerpc/include/asm/vga.h:40:21: sparse:     got restricted __le16 [usertype]
>> arch/powerpc/include/asm/vga.h:40:21: sparse: sparse: incorrect type in argument 2 (different base types) @@     expected unsigned short [usertype] @@     got restricted __le16 [usertype] @@
>> arch/powerpc/include/asm/vga.h:40:21: sparse:     expected unsigned short [usertype]
   arch/powerpc/include/asm/vga.h:40:21: sparse:     got restricted __le16 [usertype]
   arch/powerpc/include/asm/vga.h:34:16: sparse: sparse: cast to restricted __le16
   arch/powerpc/include/asm/vga.h:29:15: sparse: sparse: incorrect type in assignment (different base types) @@     expected unsigned short volatile [usertype] @@     got restricted __le16 [usertype] @@
   arch/powerpc/include/asm/vga.h:29:15: sparse:     expected unsigned short volatile [usertype]
   arch/powerpc/include/asm/vga.h:29:15: sparse:     got restricted __le16 [usertype]
   arch/powerpc/include/asm/vga.h:29:15: sparse: sparse: incorrect type in assignment (different base types) @@     expected unsigned short volatile [usertype] @@     got restricted __le16 [usertype] @@
   arch/powerpc/include/asm/vga.h:29:15: sparse:     expected unsigned short volatile [usertype]
   arch/powerpc/include/asm/vga.h:29:15: sparse:     got restricted __le16 [usertype]
   arch/powerpc/include/asm/vga.h:34:16: sparse: sparse: cast to restricted __le16
   arch/powerpc/include/asm/vga.h:29:15: sparse: sparse: incorrect type in assignment (different base types) @@     expected unsigned short volatile [usertype] @@     got restricted __le16 [usertype] @@
   arch/powerpc/include/asm/vga.h:29:15: sparse:     expected unsigned short volatile [usertype]
   arch/powerpc/include/asm/vga.h:29:15: sparse:     got restricted __le16 [usertype]
>> arch/powerpc/include/asm/vga.h:40:21: sparse: sparse: incorrect type in argument 2 (different base types) @@     expected unsigned short [usertype] @@     got restricted __le16 [usertype] @@
>> arch/powerpc/include/asm/vga.h:40:21: sparse:     expected unsigned short [usertype]
   arch/powerpc/include/asm/vga.h:40:21: sparse:     got restricted __le16 [usertype]
>> arch/powerpc/include/asm/vga.h:40:21: sparse: sparse: incorrect type in argument 2 (different base types) @@     expected unsigned short [usertype] @@     got restricted __le16 [usertype] @@
>> arch/powerpc/include/asm/vga.h:40:21: sparse:     expected unsigned short [usertype]
   arch/powerpc/include/asm/vga.h:40:21: sparse:     got restricted __le16 [usertype]
>> arch/powerpc/include/asm/vga.h:40:21: sparse: sparse: incorrect type in argument 2 (different base types) @@     expected unsigned short [usertype] @@     got restricted __le16 [usertype] @@
>> arch/powerpc/include/asm/vga.h:40:21: sparse:     expected unsigned short [usertype]
   arch/powerpc/include/asm/vga.h:40:21: sparse:     got restricted __le16 [usertype]
>> arch/powerpc/include/asm/vga.h:40:21: sparse: sparse: incorrect type in argument 2 (different base types) @@     expected unsigned short [usertype] @@     got restricted __le16 [usertype] @@
>> arch/powerpc/include/asm/vga.h:40:21: sparse:     expected unsigned short [usertype]
   arch/powerpc/include/asm/vga.h:40:21: sparse:     got restricted __le16 [usertype]
>> arch/powerpc/include/asm/vga.h:40:21: sparse: sparse: incorrect type in argument 2 (different base types) @@     expected unsigned short [usertype] @@     got restricted __le16 [usertype] @@
>> arch/powerpc/include/asm/vga.h:40:21: sparse:     expected unsigned short [usertype]

arch/powerpc/include/asm/vga.h:40:21: sparse: got restricted __le16 [usertype]
arch/powerpc/include/asm/vga.h:29:15: sparse: sparse: incorrect type in assignment (different base types) @@ expected unsigned short volatile [usertype] @@ got restricted __le16 [usertype] @@
arch/powerpc/include/asm/vga.h:29:15: sparse: expected unsigned short volatile [usertype]
arch/powerpc/include/asm/vga.h:29:15: sparse: got restricted __le16 [usertype]
arch/powerpc/include/asm/vga.h:29:15: sparse: sparse: incorrect type in assignment (different base types) @@ expected unsigned short volatile [usertype] @@ got restricted __le16 [usertype] @@
arch/powerpc/include/asm/vga.h:29:15: sparse: expected unsigned short volatile [usertype]
arch/powerpc/include/asm/vga.h:29:15: sparse: got restricted __le16 [usertype]
arch/powerpc/include/asm/vga.h:34:16: sparse: sparse: cast to restricted __le16 arch/powerpc/include/asm/vga.h:34:16: sparse: sparse: cast to restricted __le16
arch/powerpc/include/asm/vga.h:29:15: sparse: sparse: incorrect type in assignment (different base types) @@ expected unsigned short volatile [usertype] @@ got restricted __le16 [usertype] @@
arch/powerpc/include/asm/vga.h:29:15: sparse: expected unsigned short volatile [usertype]
arch/powerpc/include/asm/vga.h:29:15: sparse: got restricted __le16 [usertype]

vim +131 drivers/char/tpm/tpm_ibmvtpm.c

132f7629474424 Ashley Lai        2012-08-22   94  
132f7629474424 Ashley Lai        2012-08-22   95  /**
132f7629474424 Ashley Lai        2012-08-22   96   * tpm_ibmvtpm_recv - Receive data after send
93c12f293f8798 Winkler, Tomas    2016-11-23   97   *
132f7629474424 Ashley Lai        2012-08-22   98   * @chip:	tpm chip struct
132f7629474424 Ashley Lai        2012-08-22   99   * @buf:	buffer to read
93c12f293f8798 Winkler, Tomas    2016-11-23  100   * @count:	size of buffer
132f7629474424 Ashley Lai        2012-08-22  101   *
93c12f293f8798 Winkler, Tomas    2016-11-23  102   * Return:
132f7629474424 Ashley Lai        2012-08-22  103   *	Number of bytes read
132f7629474424 Ashley Lai        2012-08-22  104   */
132f7629474424 Ashley Lai        2012-08-22  105  static int tpm_ibmvtpm_recv(struct tpm_chip *chip, u8 *buf, size_t count)
132f7629474424 Ashley Lai        2012-08-22  106  {
9e0d39d8a6a0a8 Christophe Ricard 2016-03-31  107  	struct ibmvtpm_dev *ibmvtpm = dev_get_drvdata(&chip->dev);
132f7629474424 Ashley Lai        2012-08-22  108  	u16 len;
b5666502700855 Ashley Lai        2012-09-12  109  	int sig;
132f7629474424 Ashley Lai        2012-08-22  110  
132f7629474424 Ashley Lai        2012-08-22  111  	if (!ibmvtpm->rtce_buf) {
132f7629474424 Ashley Lai        2012-08-22  112  		dev_err(ibmvtpm->dev, "ibmvtpm device is not ready\n");
132f7629474424 Ashley Lai        2012-08-22  113  		return 0;
132f7629474424 Ashley Lai        2012-08-22  114  	}
132f7629474424 Ashley Lai        2012-08-22  115  
6674ff145eef1f Stefan Berger     2015-12-09  116  	sig = wait_event_interruptible(ibmvtpm->wq, !ibmvtpm->tpm_processing_cmd);
b5666502700855 Ashley Lai        2012-09-12  117  	if (sig)
b5666502700855 Ashley Lai        2012-09-12  118  		return -EINTR;
b5666502700855 Ashley Lai        2012-09-12  119  
b5666502700855 Ashley Lai        2012-09-12  120  	len = ibmvtpm->res_len;
132f7629474424 Ashley Lai        2012-08-22  121  
b5666502700855 Ashley Lai        2012-09-12  122  	if (count < len) {
132f7629474424 Ashley Lai        2012-08-22  123  		dev_err(ibmvtpm->dev,
37ab03414829e5 Jason Gunthorpe   2013-09-14  124  			"Invalid size in recv: count=%zd, crq_size=%d\n",
b5666502700855 Ashley Lai        2012-09-12  125  			count, len);
132f7629474424 Ashley Lai        2012-08-22  126  		return -EIO;
132f7629474424 Ashley Lai        2012-08-22  127  	}
132f7629474424 Ashley Lai        2012-08-22  128  
132f7629474424 Ashley Lai        2012-08-22  129  	spin_lock(&ibmvtpm->rtce_lock);
b5666502700855 Ashley Lai        2012-09-12  130  	memcpy((void *)buf, (void *)ibmvtpm->rtce_buf, len);
b5666502700855 Ashley Lai        2012-09-12 @131  	memset(ibmvtpm->rtce_buf, 0, len);
b5666502700855 Ashley Lai        2012-09-12  132  	ibmvtpm->res_len = 0;
132f7629474424 Ashley Lai        2012-08-22  133  	spin_unlock(&ibmvtpm->rtce_lock);
132f7629474424 Ashley Lai        2012-08-22  134  	return len;
132f7629474424 Ashley Lai        2012-08-22  135  }
132f7629474424 Ashley Lai        2012-08-22  136  

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org