Skip to contentSkip to conversation

KASAN for powerpc/32

PATCH Superseded Christophe Leroy 2 participants 17 messages Mar 1, 2019 – Mar 8, 2019

Subsystems (4)

v9 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v9 cover KASAN for powerpc/32

This series adds KASAN support to powerpc/32

Tested on nohash/32 (8xx) and book3s/32 (mpc832x ie 603). Boot tested on qemu mac99

Changes in v9:
- Fixed fixmap IMMR alignment issue on 8xx with KASAN enabled.
- Set up final shadow page tables before switching to the final hash table on hash32
- Using PAGE_READONLY instead of PAGE_KERNEL_RO on hash32
- Use flash_tlb_kernel_range() instead of flash_tlb_mm() which doesn't work for kernel on some subarches.
- use __set_pte_at() instead of pte_update() to install final page tables

Changes in v8:
- Fixed circular issue between pgtable.h and fixmap.h
- Added missing includes in ppc64 string files
- Fixed kasan string related macro names for ppc64.
- Fixed most checkpatch messages
- build tested on kisskb (http://kisskb.ellerman.id.au/kisskb/head/6e65827de2fe71d21682dafd9084ed2cc6e06d4f/)
- moved CONFIG_KASAN_SHADOW_OFFSET in Kconfig.debug

Changes in v7:
- split in several smaller patches
- prom_init now has its own string functions
- full deactivation of powerpc-optimised string functions when KASAN is active
- shadow area now at a fixed place on very top of kernel virtual space.
- Early static hash table for hash book3s/32.
- Full support of both inline and outline instrumentation for both hash and nohash ppc32
- Earlier full activation of kasan.

Changes in v6:
- Fixed oops on module loading (due to access to RO shadow zero area).
- Added support for hash book3s/32, thanks to Daniel's patch to differ KASAN activation.
- Reworked handling of optimised string functions (dedicated patch for it)
- Reordered some files to ease adding of book3e/64 support.

Changes in v5:
- Added KASAN_SHADOW_OFFSET in Makefile, otherwise we fallback to KASAN_MINIMAL
and some stuff like stack instrumentation is not performed
- Moved calls to kasan_early_init() in head.S because stack instrumentation
in machine_init was performed before the call to kasan_early_init()
- Mapping kasan_early_shadow_page RW in kasan_early_init() and
remaping RO later in kasan_init()
- Allocating a big memblock() for shadow area, falling back to PAGE_SIZE blocks in case of failure.

Changes in v4:
- Comments from Andrey (DISABLE_BRANCH_PROFILING, Activation of reports)
- Proper initialisation of shadow area in kasan_init()
- Panic in case Hash table is required.
- Added comments in patch one to explain why *t = *s becomes memcpy(t, s, ...)
- Call of kasan_init_tags()

Changes in v3:
- Removed the printk() in kasan_early_init() to avoid build failure (see https://github.com/linuxppc/issues/issues/218)
- Added necessary changes in asm/book3s/32/pgtable.h to get it work on powerpc 603 family
- Added a few KASAN_SANITIZE_xxx.o := n to successfully boot on powerpc 603 family

Changes in v2:
- Rebased.
- Using __set_pte_at() to build the early table.
- Worked around and got rid of the patch adding asm/page.h in asm/pgtable-types.h

    ==> might be fixed independently but not needed for this serie.

Christophe Leroy (11):
powerpc/32: Move early_init() in a separate file
powerpc: prepare string/mem functions for KASAN powerpc/prom_init: don't use string functions from lib/ powerpc/mm: don't use direct assignation during early boot. powerpc/32: use memset() instead of memset_io() to zero BSS powerpc/32: make KVIRT_TOP dependent on FIXMAP_START powerpc/32: prepare shadow area for KASAN
powerpc: disable KASAN instrumentation on early/critical files. powerpc/32: Add KASAN support
powerpc/32s: move hash code patching out of MMU_init_hw() powerpc/32s: set up an early static hash table for KASAN.

 arch/powerpc/Kconfig                         |   1 +
 arch/powerpc/Kconfig.debug                   |   5 +
 arch/powerpc/include/asm/book3s/32/pgtable.h |  13 +-
 arch/powerpc/include/asm/fixmap.h            |   5 +
 arch/powerpc/include/asm/kasan.h             |  39 +++++
 arch/powerpc/include/asm/nohash/32/pgtable.h |  13 +-
 arch/powerpc/include/asm/string.h            |  32 +++-
 arch/powerpc/kernel/Makefile                 |  14 +-
 arch/powerpc/kernel/cputable.c               |  13 +-
 arch/powerpc/kernel/early_32.c               |  36 +++++
 arch/powerpc/kernel/head_32.S                |  49 ++++--
 arch/powerpc/kernel/head_40x.S               |   3 +
 arch/powerpc/kernel/head_44x.S               |   3 +
 arch/powerpc/kernel/head_8xx.S               |   3 +
 arch/powerpc/kernel/head_fsl_booke.S         |   3 +
 arch/powerpc/kernel/prom_init.c              | 213 +++++++++++++++++++++------
 arch/powerpc/kernel/prom_init_check.sh       |  12 +-
 arch/powerpc/kernel/setup-common.c           |   3 +
 arch/powerpc/kernel/setup_32.c               |  28 ----
 arch/powerpc/lib/Makefile                    |  19 ++-
 arch/powerpc/lib/copy_32.S                   |  15 +-
 arch/powerpc/lib/mem_64.S                    |  11 +-
 arch/powerpc/lib/memcpy_64.S                 |   5 +-
 arch/powerpc/mm/Makefile                     |   7 +
 arch/powerpc/mm/kasan/Makefile               |   5 +
 arch/powerpc/mm/kasan/kasan_init_32.c        | 189 ++++++++++++++++++++++++
 arch/powerpc/mm/mem.c                        |   4 +
 arch/powerpc/mm/mmu_decl.h                   |   2 +
 arch/powerpc/mm/ppc_mmu_32.c                 |  36 +++--
 arch/powerpc/mm/ptdump/ptdump.c              |   8 +
 arch/powerpc/platforms/powermac/Makefile     |   6 +
 arch/powerpc/purgatory/Makefile              |   3 +
 arch/powerpc/xmon/Makefile                   |   1 +
 33 files changed, 676 insertions(+), 123 deletions(-)
 create mode 100644 arch/powerpc/include/asm/kasan.h
 create mode 100644 arch/powerpc/kernel/early_32.c
 create mode 100644 arch/powerpc/mm/kasan/Makefile
 create mode 100644 arch/powerpc/mm/kasan/kasan_init_32.c
Signature · 1 line
2.13.3
#1 lore raw

PATCH v9 1/11 powerpc/32: Move early_init() in a separate file

In preparation of KASAN, move early_init() into a separate file in order to allow deactivation of KASAN for that function.

---
 arch/powerpc/kernel/Makefile   |  2 +-
 arch/powerpc/kernel/early_32.c | 36 ++++++++++++++++++++++++++++++++++++
 arch/powerpc/kernel/setup_32.c | 28 ----------------------------
 3 files changed, 37 insertions(+), 29 deletions(-)
 create mode 100644 arch/powerpc/kernel/early_32.c
arch/powerpc/kernel/Makefile +1 -1
index cddadccf551d..45e47752b692 100644
@@ -93,7 +93,7 @@ extra-y				+= vmlinux.lds
 
 obj-$(CONFIG_RELOCATABLE)	+= reloc_$(BITS).o
 
-obj-$(CONFIG_PPC32)		+= entry_32.o setup_32.o
+obj-$(CONFIG_PPC32)		+= entry_32.o setup_32.o early_32.o
 obj-$(CONFIG_PPC64)		+= dma-iommu.o iommu.o
 obj-$(CONFIG_KGDB)		+= kgdb.o
 obj-$(CONFIG_BOOTX_TEXT)	+= btext.o
arch/powerpc/kernel/early_32.c new file +36 -0
new file mode 100644
index 000000000000..cf3cdd81dc47
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Early init before relocation
+ */
+
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <asm/setup.h>
+#include <asm/sections.h>
+#include <asm/asm-prototypes.h>
+
+/*
+ * We're called here very early in the boot.
+ *
+ * Note that the kernel may be running at an address which is different
+ * from the address that it was linked at, so we must use RELOC/PTRRELOC
+ * to access static data (including strings).  -- paulus
+ */
+notrace unsigned long __init early_init(unsigned long dt_ptr)
+{
+	unsigned long offset = reloc_offset();
+
+	/* First zero the BSS -- use memset_io, some platforms don't have caches on yet */
+	memset_io((void __iomem *)PTRRELOC(&__bss_start), 0, __bss_stop - __bss_start);
+
+	/*
+	 * Identify the CPU type and fix up code sections
+	 * that depend on which cpu we have.
+	 */
+	identify_cpu(offset, mfspr(SPRN_PVR));
+
+	apply_feature_fixups();
+
+	return KERNELBASE + offset;
+}
arch/powerpc/kernel/setup_32.c +0 -28
index 1f0b7629c1a6..3f0d51b4b2f5 100644
@@ -63,34 +63,6 @@ EXPORT_SYMBOL(DMA_MODE_READ);
 EXPORT_SYMBOL(DMA_MODE_WRITE);
 
 /*
- * We're called here very early in the boot.
- *
- * Note that the kernel may be running at an address which is different
- * from the address that it was linked at, so we must use RELOC/PTRRELOC
- * to access static data (including strings).  -- paulus
- */
-notrace unsigned long __init early_init(unsigned long dt_ptr)
-{
-	unsigned long offset = reloc_offset();
-
-	/* First zero the BSS -- use memset_io, some platforms don't have
-	 * caches on yet */
-	memset_io((void __iomem *)PTRRELOC(&__bss_start), 0,
-			__bss_stop - __bss_start);
-
-	/*
-	 * Identify the CPU type and fix up code sections
-	 * that depend on which cpu we have.
-	 */
-	identify_cpu(offset, mfspr(SPRN_PVR));
-
-	apply_feature_fixups();
-
-	return KERNELBASE + offset;
-}
-
-
-/*
  * This is run before start_kernel(), the kernel has been relocated
  * and we are running with enough of the MMU enabled to have our
  * proper kernel virtual addresses
Signature · 1 line
2.13.3
#2 lore raw

PATCH v9 2/11 powerpc: prepare string/mem functions for KASAN

In reply to Christophe Leroy

CONFIG_KASAN implements wrappers for memcpy() memmove() and memset() Those wrappers are doing the verification then call respectively __memcpy() __memmove() and __memset(). The arches are therefore expected to rename their optimised functions that way.

For files on which KASAN is inhibited, #defines are used to allow them to directly call optimised versions of the functions without going through the KASAN wrappers.

See commit 393f203f5fd5 ("x86_64: kasan: add interceptors for memset/memmove/memcpy functions") for details.

Other string / mem functions do not (yet) have kasan wrappers, we therefore have to fallback to the generic versions when KASAN is active, otherwise KASAN checks will be skipped.

---
 arch/powerpc/include/asm/kasan.h       | 15 +++++++++++++++
 arch/powerpc/include/asm/string.h      | 32 +++++++++++++++++++++++++++++---
 arch/powerpc/kernel/prom_init_check.sh | 10 +++++++++-
 arch/powerpc/lib/Makefile              | 11 ++++++++---
 arch/powerpc/lib/copy_32.S             | 15 +++++++++------
 arch/powerpc/lib/mem_64.S              | 11 +++++++----
 arch/powerpc/lib/memcpy_64.S           |  5 +++--
 7 files changed, 80 insertions(+), 19 deletions(-)
 create mode 100644 arch/powerpc/include/asm/kasan.h
arch/powerpc/include/asm/kasan.h new file +15 -0
new file mode 100644
index 000000000000..c3161b8fc017
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ASM_KASAN_H
+#define __ASM_KASAN_H
+
+#ifdef CONFIG_KASAN
+#define _GLOBAL_KASAN(fn)	.weak fn ; _GLOBAL(__##fn) ; _GLOBAL(fn)
+#define _GLOBAL_TOC_KASAN(fn)	.weak fn ; _GLOBAL_TOC(__##fn) ; _GLOBAL_TOC(fn)
+#define EXPORT_SYMBOL_KASAN(fn)	EXPORT_SYMBOL(__##fn) ; EXPORT_SYMBOL(fn)
+#else
+#define _GLOBAL_KASAN(fn)	_GLOBAL(fn)
+#define _GLOBAL_TOC_KASAN(fn)	_GLOBAL_TOC(fn)
+#define EXPORT_SYMBOL_KASAN(fn)	EXPORT_SYMBOL(fn)
+#endif
+
+#endif
arch/powerpc/include/asm/string.h +29 -3
index 1647de15a31e..9bf6dffb4090 100644
@@ -4,14 +4,17 @@
 
 #ifdef __KERNEL__
 
+#ifndef CONFIG_KASAN
 #define __HAVE_ARCH_STRNCPY
 #define __HAVE_ARCH_STRNCMP
+#define __HAVE_ARCH_MEMCHR
+#define __HAVE_ARCH_MEMCMP
+#define __HAVE_ARCH_MEMSET16
+#endif
+
 #define __HAVE_ARCH_MEMSET
 #define __HAVE_ARCH_MEMCPY
 #define __HAVE_ARCH_MEMMOVE
-#define __HAVE_ARCH_MEMCMP
-#define __HAVE_ARCH_MEMCHR
-#define __HAVE_ARCH_MEMSET16
 #define __HAVE_ARCH_MEMCPY_FLUSHCACHE
 
 extern char * strcpy(char *,const char *);
@@ -27,7 +30,27 @@ extern int memcmp(const void *,const void *,__kernel_size_t);
 extern void * memchr(const void *,int,__kernel_size_t);
 extern void * memcpy_flushcache(void *,const void *,__kernel_size_t);
 
+void *__memset(void *s, int c, __kernel_size_t count);
+void *__memcpy(void *to, const void *from, __kernel_size_t n);
+void *__memmove(void *to, const void *from, __kernel_size_t n);
+
+#if defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__)
+/*
+ * For files that are not instrumented (e.g. mm/slub.c) we
+ * should use not instrumented version of mem* functions.
+ */
+#define memcpy(dst, src, len) __memcpy(dst, src, len)
+#define memmove(dst, src, len) __memmove(dst, src, len)
+#define memset(s, c, n) __memset(s, c, n)
+
+#ifndef __NO_FORTIFY
+#define __NO_FORTIFY /* FORTIFY_SOURCE uses __builtin_memcpy, etc. */
+#endif
+
+#endif
+
 #ifdef CONFIG_PPC64
+#ifndef CONFIG_KASAN
 #define __HAVE_ARCH_MEMSET32
 #define __HAVE_ARCH_MEMSET64
 
@@ -49,8 +72,11 @@ static inline void *memset64(uint64_t *p, uint64_t v, __kernel_size_t n)
 {
 	return __memset64(p, v, n * 8);
 }
+#endif
 #else
+#ifndef CONFIG_KASAN
 #define __HAVE_ARCH_STRLEN
+#endif
 
 extern void *memset16(uint16_t *, uint16_t, __kernel_size_t);
 #endif
arch/powerpc/kernel/prom_init_check.sh +9 -1
index 667df97d2595..181fd10008ef 100644
@@ -16,8 +16,16 @@
 # If you really need to reference something from prom_init.o add
 # it to the list below:
 
+grep "^CONFIG_KASAN=y$" .config >/dev/null
+if [ $? -eq 0 ]
+then
+	MEM_FUNCS="__memcpy __memset"
+else
+	MEM_FUNCS="memcpy memset"
+fi
+
 WHITELIST="add_reloc_offset __bss_start __bss_stop copy_and_flush
-_end enter_prom memcpy memset reloc_offset __secondary_hold
+_end enter_prom $MEM_FUNCS reloc_offset __secondary_hold
 __secondary_hold_acknowledge __secondary_hold_spinloop __start
 strcmp strcpy strlcpy strlen strncmp strstr kstrtobool logo_linux_clut224
 reloc_got2 kernstart_addr memstart_addr linux_banner _stext
arch/powerpc/lib/Makefile +8 -3
index 79396e184bca..47a4de434c22 100644
@@ -8,9 +8,14 @@ ccflags-$(CONFIG_PPC64)	:= $(NO_MINIMAL_TOC)
 CFLAGS_REMOVE_code-patching.o = $(CC_FLAGS_FTRACE)
 CFLAGS_REMOVE_feature-fixups.o = $(CC_FLAGS_FTRACE)
 
-obj-y += string.o alloc.o code-patching.o feature-fixups.o
+obj-y += alloc.o code-patching.o feature-fixups.o
 
-obj-$(CONFIG_PPC32)	+= div64.o copy_32.o crtsavres.o strlen_32.o
+ifndef CONFIG_KASAN
+obj-y	+=	string.o memcmp_$(BITS).o
+obj-$(CONFIG_PPC32)	+= strlen_32.o
+endif
+
+obj-$(CONFIG_PPC32)	+= div64.o copy_32.o crtsavres.o
 
 obj-$(CONFIG_FUNCTION_ERROR_INJECTION)	+= error-inject.o
 
@@ -34,7 +39,7 @@ obj64-$(CONFIG_KPROBES_SANITY_TEST)	+= test_emulate_step.o \
 					   test_emulate_step_exec_instr.o
 
 obj-y			+= checksum_$(BITS).o checksum_wrappers.o \
-			   string_$(BITS).o memcmp_$(BITS).o
+			   string_$(BITS).o
 
 obj-y			+= sstep.o ldstfp.o quad.o
 obj64-y			+= quad.o
arch/powerpc/lib/copy_32.S +9 -6
index ba66846fe973..fc4fa7246200 100644
@@ -14,6 +14,7 @@
 #include <asm/ppc_asm.h>
 #include <asm/export.h>
 #include <asm/code-patching-asm.h>
+#include <asm/kasan.h>
 
 #define COPY_16_BYTES		\
 	lwz	r7,4(r4);	\
@@ -68,6 +69,7 @@ CACHELINE_BYTES = L1_CACHE_BYTES
 LG_CACHELINE_BYTES = L1_CACHE_SHIFT
 CACHELINE_MASK = (L1_CACHE_BYTES-1)
 
+#ifndef CONFIG_KASAN
 _GLOBAL(memset16)
 	rlwinm.	r0 ,r5, 31, 1, 31
 	addi	r6, r3, -4
@@ -81,6 +83,7 @@ _GLOBAL(memset16)
 	sth	r4, 4(r6)
 	blr
 EXPORT_SYMBOL(memset16)
+#endif
 
 /*
  * Use dcbz on the complete cache lines in the destination
@@ -91,7 +94,7 @@ EXPORT_SYMBOL(memset16)
  * We therefore skip the optimised bloc that uses dcbz. This jump is
  * replaced by a nop once cache is active. This is done in machine_init()
  */
-_GLOBAL(memset)
+_GLOBAL_KASAN(memset)
 	cmplwi	0,r5,4
 	blt	7f
 
@@ -150,7 +153,7 @@ _GLOBAL(memset)
 9:	stbu	r4,1(r6)
 	bdnz	9b
 	blr
-EXPORT_SYMBOL(memset)
+EXPORT_SYMBOL_KASAN(memset)
 
 /*
  * This version uses dcbz on the complete cache lines in the
@@ -163,12 +166,12 @@ EXPORT_SYMBOL(memset)
  * We therefore jump to generic_memcpy which doesn't use dcbz. This jump is
  * replaced by a nop once cache is active. This is done in machine_init()
  */
-_GLOBAL(memmove)
+_GLOBAL_KASAN(memmove)
 	cmplw	0,r3,r4
 	bgt	backwards_memcpy
 	/* fall through */
 
-_GLOBAL(memcpy)
+_GLOBAL_KASAN(memcpy)
 1:	b	generic_memcpy
 	patch_site	1b, patch__memcpy_nocache
 
@@ -242,8 +245,8 @@ _GLOBAL(memcpy)
 	stbu	r0,1(r6)
 	bdnz	40b
 65:	blr
-EXPORT_SYMBOL(memcpy)
-EXPORT_SYMBOL(memmove)
+EXPORT_SYMBOL_KASAN(memcpy)
+EXPORT_SYMBOL_KASAN(memmove)
 
 generic_memcpy:
 	srwi.	r7,r5,3
arch/powerpc/lib/mem_64.S +7 -4
index 3c3be02f33b7..7cd6cf6822a2 100644
@@ -12,7 +12,9 @@
 #include <asm/errno.h>
 #include <asm/ppc_asm.h>
 #include <asm/export.h>
+#include <asm/kasan.h>
 
+#ifndef CONFIG_KASAN
 _GLOBAL(__memset16)
 	rlwimi	r4,r4,16,0,15
 	/* fall through */
@@ -29,8 +31,9 @@ _GLOBAL(__memset64)
 EXPORT_SYMBOL(__memset16)
 EXPORT_SYMBOL(__memset32)
 EXPORT_SYMBOL(__memset64)
+#endif
 
-_GLOBAL(memset)
+_GLOBAL_KASAN(memset)
 	neg	r0,r3
 	rlwimi	r4,r4,8,16,23
 	andi.	r0,r0,7			/* # bytes to be 8-byte aligned */
@@ -95,9 +98,9 @@ _GLOBAL(memset)
 10:	bflr	31
 	stb	r4,0(r6)
 	blr
-EXPORT_SYMBOL(memset)
+EXPORT_SYMBOL_KASAN(memset)
 
-_GLOBAL_TOC(memmove)
+_GLOBAL_TOC_KASAN(memmove)
 	cmplw	0,r3,r4
 	bgt	backwards_memcpy
 	b	memcpy
@@ -138,4 +141,4 @@ _GLOBAL(backwards_memcpy)
 	beq	2b
 	mtctr	r7
 	b	1b
-EXPORT_SYMBOL(memmove)
+EXPORT_SYMBOL_KASAN(memmove)
arch/powerpc/lib/memcpy_64.S +3 -2
index 273ea67e60a1..862b515b8868 100644
@@ -11,6 +11,7 @@
 #include <asm/export.h>
 #include <asm/asm-compat.h>
 #include <asm/feature-fixups.h>
+#include <asm/kasan.h>
 
 #ifndef SELFTEST_CASE
 /* For big-endian, 0 == most CPUs, 1 == POWER6, 2 == Cell */
@@ -18,7 +19,7 @@
 #endif
 
 	.align	7
-_GLOBAL_TOC(memcpy)
+_GLOBAL_TOC_KASAN(memcpy)
 BEGIN_FTR_SECTION
 #ifdef __LITTLE_ENDIAN__
 	cmpdi	cr7,r5,0
@@ -229,4 +230,4 @@ END_FTR_SECTION_IFCLR(CPU_FTR_UNALIGNED_LD_STD)
 4:	ld	r3,-STACKFRAMESIZE+STK_REG(R31)(r1)	/* return dest pointer */
 	blr
 #endif
-EXPORT_SYMBOL(memcpy)
+EXPORT_SYMBOL_KASAN(memcpy)
Signature · 1 line
2.13.3
#3 lore raw

Re: [PATCH v9 02/11] powerpc: prepare string/mem functions for KASAN

Hi Christophe,

Quoted 6 lines
arch/powerpc/include/asm/kasan.h new file +0 -0
new file mode 100644
index 000000000000..c3161b8fc017

arch/powerpc/include/asm/kasan.h line 8

Quoted 8 lines
arch/powerpc/include/asm/kasan.h new file +8 -0
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ASM_KASAN_H
+#define __ASM_KASAN_H
+
+#ifdef CONFIG_KASAN
+#define _GLOBAL_KASAN(fn)	.weak fn ; _GLOBAL(__##fn) ; _GLOBAL(fn)
+#define _GLOBAL_TOC_KASAN(fn)	.weak fn ; _GLOBAL_TOC(__##fn) ; _GLOBAL_TOC(fn)
+#define EXPORT_SYMBOL_KASAN(fn)	EXPORT_SYMBOL(__##fn) ; EXPORT_SYMBOL(fn)

I'm having some trouble with this. I get warnings like this:

WARNING: EXPORT symbol "__memcpy" [vmlinux] version generation failed, symbol will not be versioned.

It seems to be related to the export line, as if I swap the exports to do fn before __##fn I get:

WARNING: EXPORT symbol "memset" [vmlinux] version generation failed, symbol will not be versioned.

I have narrowed this down to combining 2 EXPORT_SYMBOL()s on one line. This works - no warning:

EXPORT_SYMBOL(memset)
EXPORT_SYMBOL(__memset)

This throws a warning:

EXPORT_SYMBOL(memset) ; EXPORT_SYMBOL(__memset)

I notice in looking at the diff of preprocessed source we end up invoking an asm macro that doesn't seem to have a full final argument, I wonder if that's relevant...

-___EXPORT_SYMBOL __memset, __memset, ; ___EXPORT_SYMBOL memset, memset, +___EXPORT_SYMBOL __memset, __memset,
+___EXPORT_SYMBOL memset, memset,

I also notice that nowhere else in the source do people have multiple EXPORT_SYMBOLs on the same line, and other arches seem to just unconditionally export both symbols on multiple lines.

I have no idea how this works for you - maybe it's affected by something 32bit.

How would you feel about this approach instead? I'm not tied to any of the names or anything.

arch/powerpc/include/asm/ppc_asm.h +10 -0
index e0637730a8e7..7b6a91b448dd 100644
@@ -214,6 +214,9 @@ name: \
 
 #define DOTSYM(a)      a
 
+#define PROVIDE_WEAK_ALIAS(strongname, weakname) \
+       .weak weakname ; .set weakname, strongname ;
+
 #else
 
 #define XGLUE(a,b) a##b
@@ -236,6 +239,10 @@ GLUE(.,name):
 
 #define DOTSYM(a)      GLUE(.,a)
 
+#define PROVIDE_WEAK_ALIAS(strongname, weakname) \
+       .weak weakname ; .set weakname, strongname ; \
+       .weak DOTSYM(weakname) ; .set DOTSYM(weakname), DOTSYM(strongname) ;
+
 #endif
 
 #else /* 32-bit */
@@ -251,6 +258,9 @@ GLUE(.,name):
 
 #define _GLOBAL_TOC(name) _GLOBAL(name)
 
+#define PROVIDE_WEAK_ALIAS(strongname, weakname) \
+       .weak weakname ; .set weakname, strongname ;
+
 #endif
 
 /*
arch/powerpc/lib/mem_64.S +8 -4
@@ -33,7 +33,8 @@ EXPORT_SYMBOL(__memset32)
 EXPORT_SYMBOL(__memset64)
 #endif
 
-_GLOBAL_KASAN(memset)
+PROVIDE_WEAK_ALIAS(__memset,memset)
+_GLOBAL(__memset)
        neg     r0,r3
        rlwimi  r4,r4,8,16,23
        andi.   r0,r0,7                 /* # bytes to be 8-byte aligned */
@@ -98,9 +99,11 @@ _GLOBAL_KASAN(memset)
 10:    bflr    31
        stb     r4,0(r6)
        blr
-EXPORT_SYMBOL_KASAN(memset)
+EXPORT_SYMBOL(memset)
+EXPORT_SYMBOL(__memset)
 
-_GLOBAL_TOC_KASAN(memmove)
+PROVIDE_WEAK_ALIAS(__memmove,memove)
+_GLOBAL_TOC(__memmove)
        cmplw   0,r3,r4
        bgt     backwards_memcpy
        b       memcpy
@@ -141,4 +144,5 @@ _GLOBAL(backwards_memcpy)
        beq     2b
        mtctr   r7
        b       1b
-EXPORT_SYMBOL_KASAN(memmove)
+EXPORT_SYMBOL(memmove)
+EXPORT_SYMBOL(__memmove)
arch/powerpc/lib/memcpy_64.S +4 -2
index 862b515b8868..7c1b09556cad 100644
@@ -19,7 +19,8 @@
 #endif
 
        .align  7
-_GLOBAL_TOC_KASAN(memcpy)
+PROVIDE_WEAK_ALIAS(__memcpy,memcpy)
+_GLOBAL_TOC(__memcpy)
 BEGIN_FTR_SECTION
 #ifdef __LITTLE_ENDIAN__
        cmpdi   cr7,r5,0
@@ -230,4 +231,5 @@ END_FTR_SECTION_IFCLR(CPU_FTR_UNALIGNED_LD_STD)
 4:     ld      r3,-STACKFRAMESIZE+STK_REG(R31)(r1)     /* return dest pointer */
        blr
 #endif
-EXPORT_SYMBOL_KASAN(memcpy)
+EXPORT_SYMBOL(__memcpy)
+EXPORT_SYMBOL(memcpy)

Regards,
Daniel

Quoted 268 lines
arch/powerpc/include/asm/kasan.h new file +7 -0
+#else
+#define _GLOBAL_KASAN(fn)	_GLOBAL(fn)
+#define _GLOBAL_TOC_KASAN(fn)	_GLOBAL_TOC(fn)
+#define EXPORT_SYMBOL_KASAN(fn)	EXPORT_SYMBOL(fn)
+#endif
+
+#endif
arch/powerpc/include/asm/string.h +29 -3
index 1647de15a31e..9bf6dffb4090 100644
@@ -4,14 +4,17 @@
 
 #ifdef __KERNEL__
 
+#ifndef CONFIG_KASAN
 #define __HAVE_ARCH_STRNCPY
 #define __HAVE_ARCH_STRNCMP
+#define __HAVE_ARCH_MEMCHR
+#define __HAVE_ARCH_MEMCMP
+#define __HAVE_ARCH_MEMSET16
+#endif
+
 #define __HAVE_ARCH_MEMSET
 #define __HAVE_ARCH_MEMCPY
 #define __HAVE_ARCH_MEMMOVE
-#define __HAVE_ARCH_MEMCMP
-#define __HAVE_ARCH_MEMCHR
-#define __HAVE_ARCH_MEMSET16
 #define __HAVE_ARCH_MEMCPY_FLUSHCACHE
 
 extern char * strcpy(char *,const char *);
@@ -27,7 +30,27 @@ extern int memcmp(const void *,const void *,__kernel_size_t);
 extern void * memchr(const void *,int,__kernel_size_t);
 extern void * memcpy_flushcache(void *,const void *,__kernel_size_t);
 
+void *__memset(void *s, int c, __kernel_size_t count);
+void *__memcpy(void *to, const void *from, __kernel_size_t n);
+void *__memmove(void *to, const void *from, __kernel_size_t n);
+
+#if defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__)
+/*
+ * For files that are not instrumented (e.g. mm/slub.c) we
+ * should use not instrumented version of mem* functions.
+ */
+#define memcpy(dst, src, len) __memcpy(dst, src, len)
+#define memmove(dst, src, len) __memmove(dst, src, len)
+#define memset(s, c, n) __memset(s, c, n)
+
+#ifndef __NO_FORTIFY
+#define __NO_FORTIFY /* FORTIFY_SOURCE uses __builtin_memcpy, etc. */
+#endif
+
+#endif
+
 #ifdef CONFIG_PPC64
+#ifndef CONFIG_KASAN
 #define __HAVE_ARCH_MEMSET32
 #define __HAVE_ARCH_MEMSET64
 
@@ -49,8 +72,11 @@ static inline void *memset64(uint64_t *p, uint64_t v, __kernel_size_t n)
 {
 	return __memset64(p, v, n * 8);
 }
+#endif
 #else
+#ifndef CONFIG_KASAN
 #define __HAVE_ARCH_STRLEN
+#endif
 
 extern void *memset16(uint16_t *, uint16_t, __kernel_size_t);
 #endif
arch/powerpc/kernel/prom_init_check.sh +9 -1
index 667df97d2595..181fd10008ef 100644
@@ -16,8 +16,16 @@
 # If you really need to reference something from prom_init.o add
 # it to the list below:
 
+grep "^CONFIG_KASAN=y$" .config >/dev/null
+if [ $? -eq 0 ]
+then
+	MEM_FUNCS="__memcpy __memset"
+else
+	MEM_FUNCS="memcpy memset"
+fi
+
 WHITELIST="add_reloc_offset __bss_start __bss_stop copy_and_flush
-_end enter_prom memcpy memset reloc_offset __secondary_hold
+_end enter_prom $MEM_FUNCS reloc_offset __secondary_hold
 __secondary_hold_acknowledge __secondary_hold_spinloop __start
 strcmp strcpy strlcpy strlen strncmp strstr kstrtobool logo_linux_clut224
 reloc_got2 kernstart_addr memstart_addr linux_banner _stext
arch/powerpc/lib/Makefile +8 -3
index 79396e184bca..47a4de434c22 100644
@@ -8,9 +8,14 @@ ccflags-$(CONFIG_PPC64)	:= $(NO_MINIMAL_TOC)
 CFLAGS_REMOVE_code-patching.o = $(CC_FLAGS_FTRACE)
 CFLAGS_REMOVE_feature-fixups.o = $(CC_FLAGS_FTRACE)
 
-obj-y += string.o alloc.o code-patching.o feature-fixups.o
+obj-y += alloc.o code-patching.o feature-fixups.o
 
-obj-$(CONFIG_PPC32)	+= div64.o copy_32.o crtsavres.o strlen_32.o
+ifndef CONFIG_KASAN
+obj-y	+=	string.o memcmp_$(BITS).o
+obj-$(CONFIG_PPC32)	+= strlen_32.o
+endif
+
+obj-$(CONFIG_PPC32)	+= div64.o copy_32.o crtsavres.o
 
 obj-$(CONFIG_FUNCTION_ERROR_INJECTION)	+= error-inject.o
 
@@ -34,7 +39,7 @@ obj64-$(CONFIG_KPROBES_SANITY_TEST)	+= test_emulate_step.o \
 					   test_emulate_step_exec_instr.o
 
 obj-y			+= checksum_$(BITS).o checksum_wrappers.o \
-			   string_$(BITS).o memcmp_$(BITS).o
+			   string_$(BITS).o
 
 obj-y			+= sstep.o ldstfp.o quad.o
 obj64-y			+= quad.o
arch/powerpc/lib/copy_32.S +9 -6
index ba66846fe973..fc4fa7246200 100644
@@ -14,6 +14,7 @@
 #include <asm/ppc_asm.h>
 #include <asm/export.h>
 #include <asm/code-patching-asm.h>
+#include <asm/kasan.h>
 
 #define COPY_16_BYTES		\
 	lwz	r7,4(r4);	\
@@ -68,6 +69,7 @@ CACHELINE_BYTES = L1_CACHE_BYTES
 LG_CACHELINE_BYTES = L1_CACHE_SHIFT
 CACHELINE_MASK = (L1_CACHE_BYTES-1)
 
+#ifndef CONFIG_KASAN
 _GLOBAL(memset16)
 	rlwinm.	r0 ,r5, 31, 1, 31
 	addi	r6, r3, -4
@@ -81,6 +83,7 @@ _GLOBAL(memset16)
 	sth	r4, 4(r6)
 	blr
 EXPORT_SYMBOL(memset16)
+#endif
 
 /*
  * Use dcbz on the complete cache lines in the destination
@@ -91,7 +94,7 @@ EXPORT_SYMBOL(memset16)
  * We therefore skip the optimised bloc that uses dcbz. This jump is
  * replaced by a nop once cache is active. This is done in machine_init()
  */
-_GLOBAL(memset)
+_GLOBAL_KASAN(memset)
 	cmplwi	0,r5,4
 	blt	7f
 
@@ -150,7 +153,7 @@ _GLOBAL(memset)
 9:	stbu	r4,1(r6)
 	bdnz	9b
 	blr
-EXPORT_SYMBOL(memset)
+EXPORT_SYMBOL_KASAN(memset)
 
 /*
  * This version uses dcbz on the complete cache lines in the
@@ -163,12 +166,12 @@ EXPORT_SYMBOL(memset)
  * We therefore jump to generic_memcpy which doesn't use dcbz. This jump is
  * replaced by a nop once cache is active. This is done in machine_init()
  */
-_GLOBAL(memmove)
+_GLOBAL_KASAN(memmove)
 	cmplw	0,r3,r4
 	bgt	backwards_memcpy
 	/* fall through */
 
-_GLOBAL(memcpy)
+_GLOBAL_KASAN(memcpy)
 1:	b	generic_memcpy
 	patch_site	1b, patch__memcpy_nocache
 
@@ -242,8 +245,8 @@ _GLOBAL(memcpy)
 	stbu	r0,1(r6)
 	bdnz	40b
 65:	blr
-EXPORT_SYMBOL(memcpy)
-EXPORT_SYMBOL(memmove)
+EXPORT_SYMBOL_KASAN(memcpy)
+EXPORT_SYMBOL_KASAN(memmove)
 
 generic_memcpy:
 	srwi.	r7,r5,3
arch/powerpc/lib/mem_64.S +7 -4
index 3c3be02f33b7..7cd6cf6822a2 100644
@@ -12,7 +12,9 @@
 #include <asm/errno.h>
 #include <asm/ppc_asm.h>
 #include <asm/export.h>
+#include <asm/kasan.h>
 
+#ifndef CONFIG_KASAN
 _GLOBAL(__memset16)
 	rlwimi	r4,r4,16,0,15
 	/* fall through */
@@ -29,8 +31,9 @@ _GLOBAL(__memset64)
 EXPORT_SYMBOL(__memset16)
 EXPORT_SYMBOL(__memset32)
 EXPORT_SYMBOL(__memset64)
+#endif
 
-_GLOBAL(memset)
+_GLOBAL_KASAN(memset)
 	neg	r0,r3
 	rlwimi	r4,r4,8,16,23
 	andi.	r0,r0,7			/* # bytes to be 8-byte aligned */
@@ -95,9 +98,9 @@ _GLOBAL(memset)
 10:	bflr	31
 	stb	r4,0(r6)
 	blr
-EXPORT_SYMBOL(memset)
+EXPORT_SYMBOL_KASAN(memset)
 
-_GLOBAL_TOC(memmove)
+_GLOBAL_TOC_KASAN(memmove)
 	cmplw	0,r3,r4
 	bgt	backwards_memcpy
 	b	memcpy
@@ -138,4 +141,4 @@ _GLOBAL(backwards_memcpy)
 	beq	2b
 	mtctr	r7
 	b	1b
-EXPORT_SYMBOL(memmove)
+EXPORT_SYMBOL_KASAN(memmove)
arch/powerpc/lib/memcpy_64.S +3 -2
index 273ea67e60a1..862b515b8868 100644
@@ -11,6 +11,7 @@
 #include <asm/export.h>
 #include <asm/asm-compat.h>
 #include <asm/feature-fixups.h>
+#include <asm/kasan.h>
 
 #ifndef SELFTEST_CASE
 /* For big-endian, 0 == most CPUs, 1 == POWER6, 2 == Cell */
@@ -18,7 +19,7 @@
 #endif
 
 	.align	7
-_GLOBAL_TOC(memcpy)
+_GLOBAL_TOC_KASAN(memcpy)
 BEGIN_FTR_SECTION
 #ifdef __LITTLE_ENDIAN__
 	cmpdi	cr7,r5,0
@@ -229,4 +230,4 @@ END_FTR_SECTION_IFCLR(CPU_FTR_UNALIGNED_LD_STD)
 4:	ld	r3,-STACKFRAMESIZE+STK_REG(R31)(r1)	/* return dest pointer */
 	blr
 #endif
-EXPORT_SYMBOL(memcpy)
+EXPORT_SYMBOL_KASAN(memcpy)
Signature · 1 line
2.13.3
#4 lore raw

Re: [PATCH v9 02/11] powerpc: prepare string/mem functions for KASAN

Le 04/03/2019 à 06:26, Daniel Axtens a écrit :

Quoted 17 lines

Hi Christophe,

Quoted 6 lines
arch/powerpc/include/asm/kasan.h new file +0 -0
new file mode 100644
index 000000000000..c3161b8fc017

arch/powerpc/include/asm/kasan.h line 8

Quoted 8 lines
arch/powerpc/include/asm/kasan.h new file +8 -0
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ASM_KASAN_H
+#define __ASM_KASAN_H
+
+#ifdef CONFIG_KASAN
+#define _GLOBAL_KASAN(fn)	.weak fn ; _GLOBAL(__##fn) ; _GLOBAL(fn)
+#define _GLOBAL_TOC_KASAN(fn)	.weak fn ; _GLOBAL_TOC(__##fn) ; _GLOBAL_TOC(fn)
+#define EXPORT_SYMBOL_KASAN(fn)	EXPORT_SYMBOL(__##fn) ; EXPORT_SYMBOL(fn)

I'm having some trouble with this. I get warnings like this:

I don't have such problem, neither with ppc32 nor with ppc64e_defconfig.

What config are you using ?

Another (unrelated) question I have:
On the initial arm64 implementation (39d114ddc682 arm64: add KASAN support) they made KASAN implementation depend on SPARSEMEM_VMEMMAP (allthough they later removed that dependency with commit e17d8025f07e arm64/mm/kasan: don't use vmemmap_populate() to initialize shadow)

So I'm wondering why on your side, KASAN depends on !SPARSEMEM_VMEMMAP

Thanks
Christophe

Quoted 398 lines

WARNING: EXPORT symbol "__memcpy" [vmlinux] version generation failed, symbol will not be versioned.

It seems to be related to the export line, as if I swap the exports to do fn before __##fn I get:

WARNING: EXPORT symbol "memset" [vmlinux] version generation failed, symbol will not be versioned.

I have narrowed this down to combining 2 EXPORT_SYMBOL()s on one line. This works - no warning:

EXPORT_SYMBOL(memset)
EXPORT_SYMBOL(__memset)

This throws a warning:

EXPORT_SYMBOL(memset) ; EXPORT_SYMBOL(__memset)

I notice in looking at the diff of preprocessed source we end up invoking an asm macro that doesn't seem to have a full final argument, I wonder if that's relevant...

(quoted diff) +2 -1
-___EXPORT_SYMBOL __memset, __memset, ; ___EXPORT_SYMBOL memset, memset,
+___EXPORT_SYMBOL __memset, __memset,
+___EXPORT_SYMBOL memset, memset,

I also notice that nowhere else in the source do people have multiple EXPORT_SYMBOLs on the same line, and other arches seem to just unconditionally export both symbols on multiple lines.

I have no idea how this works for you - maybe it's affected by something 32bit.

How would you feel about this approach instead? I'm not tied to any of the names or anything.

arch/powerpc/include/asm/ppc_asm.h +10 -0
index e0637730a8e7..7b6a91b448dd 100644
@@ -214,6 +214,9 @@ name: \
 
  #define DOTSYM(a)      a
 
+#define PROVIDE_WEAK_ALIAS(strongname, weakname) \
+       .weak weakname ; .set weakname, strongname ;
+
  #else
 
  #define XGLUE(a,b) a##b
@@ -236,6 +239,10 @@ GLUE(.,name):
 
  #define DOTSYM(a)      GLUE(.,a)
 
+#define PROVIDE_WEAK_ALIAS(strongname, weakname) \
+       .weak weakname ; .set weakname, strongname ; \
+       .weak DOTSYM(weakname) ; .set DOTSYM(weakname), DOTSYM(strongname) ;
+
  #endif
 
  #else /* 32-bit */
@@ -251,6 +258,9 @@ GLUE(.,name):
 
  #define _GLOBAL_TOC(name) _GLOBAL(name)
 
+#define PROVIDE_WEAK_ALIAS(strongname, weakname) \
+       .weak weakname ; .set weakname, strongname ;
+
  #endif
 
  /*
arch/powerpc/lib/mem_64.S +8 -4
@@ -33,7 +33,8 @@ EXPORT_SYMBOL(__memset32)
  EXPORT_SYMBOL(__memset64)
  #endif
 
-_GLOBAL_KASAN(memset)
+PROVIDE_WEAK_ALIAS(__memset,memset)
+_GLOBAL(__memset)
         neg     r0,r3
         rlwimi  r4,r4,8,16,23
         andi.   r0,r0,7                 /* # bytes to be 8-byte aligned */
@@ -98,9 +99,11 @@ _GLOBAL_KASAN(memset)
  10:    bflr    31
         stb     r4,0(r6)
         blr
-EXPORT_SYMBOL_KASAN(memset)
+EXPORT_SYMBOL(memset)
+EXPORT_SYMBOL(__memset)
 
-_GLOBAL_TOC_KASAN(memmove)
+PROVIDE_WEAK_ALIAS(__memmove,memove)
+_GLOBAL_TOC(__memmove)
         cmplw   0,r3,r4
         bgt     backwards_memcpy
         b       memcpy
@@ -141,4 +144,5 @@ _GLOBAL(backwards_memcpy)
         beq     2b
         mtctr   r7
         b       1b
-EXPORT_SYMBOL_KASAN(memmove)
+EXPORT_SYMBOL(memmove)
+EXPORT_SYMBOL(__memmove)
arch/powerpc/lib/memcpy_64.S +4 -2
index 862b515b8868..7c1b09556cad 100644
@@ -19,7 +19,8 @@
  #endif
 
         .align  7
-_GLOBAL_TOC_KASAN(memcpy)
+PROVIDE_WEAK_ALIAS(__memcpy,memcpy)
+_GLOBAL_TOC(__memcpy)
  BEGIN_FTR_SECTION
  #ifdef __LITTLE_ENDIAN__
         cmpdi   cr7,r5,0
@@ -230,4 +231,5 @@ END_FTR_SECTION_IFCLR(CPU_FTR_UNALIGNED_LD_STD)
  4:     ld      r3,-STACKFRAMESIZE+STK_REG(R31)(r1)     /* return dest pointer */
         blr
  #endif
-EXPORT_SYMBOL_KASAN(memcpy)
+EXPORT_SYMBOL(__memcpy)
+EXPORT_SYMBOL(memcpy)

Regards,
Daniel

Quoted 268 lines
arch/powerpc/include/asm/kasan.h new file +7 -0
+#else
+#define _GLOBAL_KASAN(fn)	_GLOBAL(fn)
+#define _GLOBAL_TOC_KASAN(fn)	_GLOBAL_TOC(fn)
+#define EXPORT_SYMBOL_KASAN(fn)	EXPORT_SYMBOL(fn)
+#endif
+
+#endif
arch/powerpc/include/asm/string.h +29 -3
index 1647de15a31e..9bf6dffb4090 100644
@@ -4,14 +4,17 @@
 
  #ifdef __KERNEL__
 
+#ifndef CONFIG_KASAN
  #define __HAVE_ARCH_STRNCPY
  #define __HAVE_ARCH_STRNCMP
+#define __HAVE_ARCH_MEMCHR
+#define __HAVE_ARCH_MEMCMP
+#define __HAVE_ARCH_MEMSET16
+#endif
+
  #define __HAVE_ARCH_MEMSET
  #define __HAVE_ARCH_MEMCPY
  #define __HAVE_ARCH_MEMMOVE
-#define __HAVE_ARCH_MEMCMP
-#define __HAVE_ARCH_MEMCHR
-#define __HAVE_ARCH_MEMSET16
  #define __HAVE_ARCH_MEMCPY_FLUSHCACHE
 
  extern char * strcpy(char *,const char *);
@@ -27,7 +30,27 @@ extern int memcmp(const void *,const void *,__kernel_size_t);
  extern void * memchr(const void *,int,__kernel_size_t);
  extern void * memcpy_flushcache(void *,const void *,__kernel_size_t);
 
+void *__memset(void *s, int c, __kernel_size_t count);
+void *__memcpy(void *to, const void *from, __kernel_size_t n);
+void *__memmove(void *to, const void *from, __kernel_size_t n);
+
+#if defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__)
+/*
+ * For files that are not instrumented (e.g. mm/slub.c) we
+ * should use not instrumented version of mem* functions.
+ */
+#define memcpy(dst, src, len) __memcpy(dst, src, len)
+#define memmove(dst, src, len) __memmove(dst, src, len)
+#define memset(s, c, n) __memset(s, c, n)
+
+#ifndef __NO_FORTIFY
+#define __NO_FORTIFY /* FORTIFY_SOURCE uses __builtin_memcpy, etc. */
+#endif
+
+#endif
+
  #ifdef CONFIG_PPC64
+#ifndef CONFIG_KASAN
  #define __HAVE_ARCH_MEMSET32
  #define __HAVE_ARCH_MEMSET64
 
@@ -49,8 +72,11 @@ static inline void *memset64(uint64_t *p, uint64_t v, __kernel_size_t n)
  {
  	return __memset64(p, v, n * 8);
  }
+#endif
  #else
+#ifndef CONFIG_KASAN
  #define __HAVE_ARCH_STRLEN
+#endif
 
  extern void *memset16(uint16_t *, uint16_t, __kernel_size_t);
  #endif
arch/powerpc/kernel/prom_init_check.sh +9 -1
index 667df97d2595..181fd10008ef 100644
@@ -16,8 +16,16 @@
  # If you really need to reference something from prom_init.o add
  # it to the list below:
 
+grep "^CONFIG_KASAN=y$" .config >/dev/null
+if [ $? -eq 0 ]
+then
+	MEM_FUNCS="__memcpy __memset"
+else
+	MEM_FUNCS="memcpy memset"
+fi
+
  WHITELIST="add_reloc_offset __bss_start __bss_stop copy_and_flush
-_end enter_prom memcpy memset reloc_offset __secondary_hold
+_end enter_prom $MEM_FUNCS reloc_offset __secondary_hold
  __secondary_hold_acknowledge __secondary_hold_spinloop __start
  strcmp strcpy strlcpy strlen strncmp strstr kstrtobool logo_linux_clut224
  reloc_got2 kernstart_addr memstart_addr linux_banner _stext
arch/powerpc/lib/Makefile +8 -3
index 79396e184bca..47a4de434c22 100644
@@ -8,9 +8,14 @@ ccflags-$(CONFIG_PPC64)	:= $(NO_MINIMAL_TOC)
  CFLAGS_REMOVE_code-patching.o = $(CC_FLAGS_FTRACE)
  CFLAGS_REMOVE_feature-fixups.o = $(CC_FLAGS_FTRACE)
 
-obj-y += string.o alloc.o code-patching.o feature-fixups.o
+obj-y += alloc.o code-patching.o feature-fixups.o
 
-obj-$(CONFIG_PPC32)	+= div64.o copy_32.o crtsavres.o strlen_32.o
+ifndef CONFIG_KASAN
+obj-y	+=	string.o memcmp_$(BITS).o
+obj-$(CONFIG_PPC32)	+= strlen_32.o
+endif
+
+obj-$(CONFIG_PPC32)	+= div64.o copy_32.o crtsavres.o
 
  obj-$(CONFIG_FUNCTION_ERROR_INJECTION)	+= error-inject.o
 
@@ -34,7 +39,7 @@ obj64-$(CONFIG_KPROBES_SANITY_TEST)	+= test_emulate_step.o \
  					   test_emulate_step_exec_instr.o
 
  obj-y			+= checksum_$(BITS).o checksum_wrappers.o \
-			   string_$(BITS).o memcmp_$(BITS).o
+			   string_$(BITS).o
 
  obj-y			+= sstep.o ldstfp.o quad.o
  obj64-y			+= quad.o
arch/powerpc/lib/copy_32.S +9 -6
index ba66846fe973..fc4fa7246200 100644
@@ -14,6 +14,7 @@
  #include <asm/ppc_asm.h>
  #include <asm/export.h>
  #include <asm/code-patching-asm.h>
+#include <asm/kasan.h>
 
  #define COPY_16_BYTES		\
  	lwz	r7,4(r4);	\
@@ -68,6 +69,7 @@ CACHELINE_BYTES = L1_CACHE_BYTES
  LG_CACHELINE_BYTES = L1_CACHE_SHIFT
  CACHELINE_MASK = (L1_CACHE_BYTES-1)
 
+#ifndef CONFIG_KASAN
  _GLOBAL(memset16)
  	rlwinm.	r0 ,r5, 31, 1, 31
  	addi	r6, r3, -4
@@ -81,6 +83,7 @@ _GLOBAL(memset16)
  	sth	r4, 4(r6)
  	blr
  EXPORT_SYMBOL(memset16)
+#endif
 
  /*
   * Use dcbz on the complete cache lines in the destination
@@ -91,7 +94,7 @@ EXPORT_SYMBOL(memset16)
   * We therefore skip the optimised bloc that uses dcbz. This jump is
   * replaced by a nop once cache is active. This is done in machine_init()
   */
-_GLOBAL(memset)
+_GLOBAL_KASAN(memset)
  	cmplwi	0,r5,4
  	blt	7f
 
@@ -150,7 +153,7 @@ _GLOBAL(memset)
  9:	stbu	r4,1(r6)
  	bdnz	9b
  	blr
-EXPORT_SYMBOL(memset)
+EXPORT_SYMBOL_KASAN(memset)
 
  /*
   * This version uses dcbz on the complete cache lines in the
@@ -163,12 +166,12 @@ EXPORT_SYMBOL(memset)
   * We therefore jump to generic_memcpy which doesn't use dcbz. This jump is
   * replaced by a nop once cache is active. This is done in machine_init()
   */
-_GLOBAL(memmove)
+_GLOBAL_KASAN(memmove)
  	cmplw	0,r3,r4
  	bgt	backwards_memcpy
  	/* fall through */
 
-_GLOBAL(memcpy)
+_GLOBAL_KASAN(memcpy)
  1:	b	generic_memcpy
  	patch_site	1b, patch__memcpy_nocache
 
@@ -242,8 +245,8 @@ _GLOBAL(memcpy)
  	stbu	r0,1(r6)
  	bdnz	40b
  65:	blr
-EXPORT_SYMBOL(memcpy)
-EXPORT_SYMBOL(memmove)
+EXPORT_SYMBOL_KASAN(memcpy)
+EXPORT_SYMBOL_KASAN(memmove)
 
  generic_memcpy:
  	srwi.	r7,r5,3
arch/powerpc/lib/mem_64.S +7 -4
index 3c3be02f33b7..7cd6cf6822a2 100644
@@ -12,7 +12,9 @@
  #include <asm/errno.h>
  #include <asm/ppc_asm.h>
  #include <asm/export.h>
+#include <asm/kasan.h>
 
+#ifndef CONFIG_KASAN
  _GLOBAL(__memset16)
  	rlwimi	r4,r4,16,0,15
  	/* fall through */
@@ -29,8 +31,9 @@ _GLOBAL(__memset64)
  EXPORT_SYMBOL(__memset16)
  EXPORT_SYMBOL(__memset32)
  EXPORT_SYMBOL(__memset64)
+#endif
 
-_GLOBAL(memset)
+_GLOBAL_KASAN(memset)
  	neg	r0,r3
  	rlwimi	r4,r4,8,16,23
  	andi.	r0,r0,7			/* # bytes to be 8-byte aligned */
@@ -95,9 +98,9 @@ _GLOBAL(memset)
  10:	bflr	31
  	stb	r4,0(r6)
  	blr
-EXPORT_SYMBOL(memset)
+EXPORT_SYMBOL_KASAN(memset)
 
-_GLOBAL_TOC(memmove)
+_GLOBAL_TOC_KASAN(memmove)
  	cmplw	0,r3,r4
  	bgt	backwards_memcpy
  	b	memcpy
@@ -138,4 +141,4 @@ _GLOBAL(backwards_memcpy)
  	beq	2b
  	mtctr	r7
  	b	1b
-EXPORT_SYMBOL(memmove)
+EXPORT_SYMBOL_KASAN(memmove)
arch/powerpc/lib/memcpy_64.S +3 -2
index 273ea67e60a1..862b515b8868 100644
@@ -11,6 +11,7 @@
  #include <asm/export.h>
  #include <asm/asm-compat.h>
  #include <asm/feature-fixups.h>
+#include <asm/kasan.h>
 
  #ifndef SELFTEST_CASE
  /* For big-endian, 0 == most CPUs, 1 == POWER6, 2 == Cell */
@@ -18,7 +19,7 @@
  #endif
 
  	.align	7
-_GLOBAL_TOC(memcpy)
+_GLOBAL_TOC_KASAN(memcpy)
  BEGIN_FTR_SECTION
  #ifdef __LITTLE_ENDIAN__
  	cmpdi	cr7,r5,0
@@ -229,4 +230,4 @@ END_FTR_SECTION_IFCLR(CPU_FTR_UNALIGNED_LD_STD)
  4:	ld	r3,-STACKFRAMESIZE+STK_REG(R31)(r1)	/* return dest pointer */
  	blr
  #endif
-EXPORT_SYMBOL(memcpy)
+EXPORT_SYMBOL_KASAN(memcpy)
Signature · 1 line
2.13.3
#5 lore raw

Re: [PATCH v9 02/11] powerpc: prepare string/mem functions for KASAN

Quoted 19 lines · Christophe Leroy

Le 04/03/2019 à 06:26, Daniel Axtens a écrit :

Quoted 17 lines

Hi Christophe,

Quoted 6 lines
arch/powerpc/include/asm/kasan.h new file +0 -0
new file mode 100644
index 000000000000..c3161b8fc017

arch/powerpc/include/asm/kasan.h line 8

Quoted 8 lines
arch/powerpc/include/asm/kasan.h new file +8 -0
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ASM_KASAN_H
+#define __ASM_KASAN_H
+
+#ifdef CONFIG_KASAN
+#define _GLOBAL_KASAN(fn)	.weak fn ; _GLOBAL(__##fn) ; _GLOBAL(fn)
+#define _GLOBAL_TOC_KASAN(fn)	.weak fn ; _GLOBAL_TOC(__##fn) ; _GLOBAL_TOC(fn)
+#define EXPORT_SYMBOL_KASAN(fn)	EXPORT_SYMBOL(__##fn) ; EXPORT_SYMBOL(fn)

I'm having some trouble with this. I get warnings like this:

Quoted 2 lines

I don't have such problem, neither with ppc32 nor with ppc64e_defconfig.

Fascinating, I'll dig into that more.

Quoted 1 line

What config are you using ?

Attached, it's based on the one provided with the SDK for the T4240RDB.

Quoted 7 lines

Another (unrelated) question I have:
On the initial arm64 implementation (39d114ddc682 arm64: add KASAN support) they made KASAN implementation depend on SPARSEMEM_VMEMMAP (allthough they later removed that dependency with commit e17d8025f07e arm64/mm/kasan: don't use vmemmap_populate() to initialize shadow)

So I'm wondering why on your side, KASAN depends on !SPARSEMEM_VMEMMAP

I use the vmemmap area as the shadow region for kasan, in a way that takes absolutely no account of any other use. It's very possible that I could instead do something similar to what arm64 used to do - I think one of the previous ppc64 approaches did something similar too.

Regards,
Daniel

#6 lore raw

Re: [PATCH v9 02/11] powerpc: prepare string/mem functions for KASAN

In reply to Daniel Axtens

Hi Daniel,

Quoted 18 lines · Daniel Axtens

Hi Christophe,

Quoted 6 lines
arch/powerpc/include/asm/kasan.h new file +0 -0
new file mode 100644
index 000000000000..c3161b8fc017

arch/powerpc/include/asm/kasan.h line 8

Quoted 8 lines
arch/powerpc/include/asm/kasan.h new file +8 -0
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ASM_KASAN_H
+#define __ASM_KASAN_H
+
+#ifdef CONFIG_KASAN
+#define _GLOBAL_KASAN(fn)	.weak fn ; _GLOBAL(__##fn) ; _GLOBAL(fn)
+#define _GLOBAL_TOC_KASAN(fn)	.weak fn ; _GLOBAL_TOC(__##fn) ; _GLOBAL_TOC(fn)
+#define EXPORT_SYMBOL_KASAN(fn)	EXPORT_SYMBOL(__##fn) ; EXPORT_SYMBOL(fn)

I'm having some trouble with this. I get warnings like this:

Quoted 1 line

WARNING: EXPORT symbol "__memcpy" [vmlinux] version generation failed, symbol will not be versioned.

I don't have this problem, neither with my PPC32 defconfigs nor with ppc64e_defconfig - SPARSEMEM_VMEMMAP + KASAN Using GCC 8.1

I've been looking into it in more details and can't understand the need for a weak symbol. A weak symbol is to allow it's optional replacement by other code. But here KASAN replaces it inconditionally, so I see no point for a weak symbol here.

Regarding the export of the functions, I believe that when the functions are defined in KASAN, they should be exported by KASAN and not by the arch. But such a change is out of scope for now. So lets have a double export for now, one day we will drop it.

Regarding export of __memcpy() etc..., there is at least the LKDTM module which inhibits KASAN, so it really needs to be exported.

What about the patch below ?

Christophe

arch/powerpc/include/asm/kasan.h new file +15 -0
new file mode 100644
index 000000000000..2c179a39d4ba
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ASM_KASAN_H
+#define __ASM_KASAN_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
+
+#endif
arch/powerpc/include/asm/string.h +29 -3
index 1647de15a31e..9bf6dffb4090 100644
@@ -4,14 +4,17 @@
 
 #ifdef __KERNEL__
 
+#ifndef CONFIG_KASAN
 #define __HAVE_ARCH_STRNCPY
 #define __HAVE_ARCH_STRNCMP
+#define __HAVE_ARCH_MEMCHR
+#define __HAVE_ARCH_MEMCMP
+#define __HAVE_ARCH_MEMSET16
+#endif
+
 #define __HAVE_ARCH_MEMSET
 #define __HAVE_ARCH_MEMCPY
 #define __HAVE_ARCH_MEMMOVE
-#define __HAVE_ARCH_MEMCMP
-#define __HAVE_ARCH_MEMCHR
-#define __HAVE_ARCH_MEMSET16
 #define __HAVE_ARCH_MEMCPY_FLUSHCACHE
 
 extern char * strcpy(char *,const char *);
@@ -27,7 +30,27 @@ extern int memcmp(const void *,const void *,__kernel_size_t);
 extern void * memchr(const void *,int,__kernel_size_t);
 extern void * memcpy_flushcache(void *,const void *,__kernel_size_t);
 
+void *__memset(void *s, int c, __kernel_size_t count);
+void *__memcpy(void *to, const void *from, __kernel_size_t n);
+void *__memmove(void *to, const void *from, __kernel_size_t n);
+
+#if defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__)
+/*
+ * For files that are not instrumented (e.g. mm/slub.c) we
+ * should use not instrumented version of mem* functions.
+ */
+#define memcpy(dst, src, len) __memcpy(dst, src, len)
+#define memmove(dst, src, len) __memmove(dst, src, len)
+#define memset(s, c, n) __memset(s, c, n)
+
+#ifndef __NO_FORTIFY
+#define __NO_FORTIFY /* FORTIFY_SOURCE uses __builtin_memcpy, etc. */
+#endif
+
+#endif
+
 #ifdef CONFIG_PPC64
+#ifndef CONFIG_KASAN
 #define __HAVE_ARCH_MEMSET32
 #define __HAVE_ARCH_MEMSET64
 
@@ -49,8 +72,11 @@ static inline void *memset64(uint64_t *p, uint64_t v, __kernel_size_t n)
 {
 	return __memset64(p, v, n * 8);
 }
+#endif
 #else
+#ifndef CONFIG_KASAN
 #define __HAVE_ARCH_STRLEN
+#endif
 
 extern void *memset16(uint16_t *, uint16_t, __kernel_size_t);
 #endif
arch/powerpc/kernel/prom_init_check.sh +9 -1
index 667df97d2595..181fd10008ef 100644
@@ -16,8 +16,16 @@
 # If you really need to reference something from prom_init.o add
 # it to the list below:
 
+grep "^CONFIG_KASAN=y$" .config >/dev/null
+if [ $? -eq 0 ]
+then
+	MEM_FUNCS="__memcpy __memset"
+else
+	MEM_FUNCS="memcpy memset"
+fi
+
 WHITELIST="add_reloc_offset __bss_start __bss_stop copy_and_flush
-_end enter_prom memcpy memset reloc_offset __secondary_hold
+_end enter_prom $MEM_FUNCS reloc_offset __secondary_hold
 __secondary_hold_acknowledge __secondary_hold_spinloop __start
 strcmp strcpy strlcpy strlen strncmp strstr kstrtobool logo_linux_clut224
 reloc_got2 kernstart_addr memstart_addr linux_banner _stext
arch/powerpc/lib/Makefile +8 -3
index 79396e184bca..47a4de434c22 100644
@@ -8,9 +8,14 @@ ccflags-$(CONFIG_PPC64)	:= $(NO_MINIMAL_TOC)
 CFLAGS_REMOVE_code-patching.o = $(CC_FLAGS_FTRACE)
 CFLAGS_REMOVE_feature-fixups.o = $(CC_FLAGS_FTRACE)
 
-obj-y += string.o alloc.o code-patching.o feature-fixups.o
+obj-y += alloc.o code-patching.o feature-fixups.o
 
-obj-$(CONFIG_PPC32)	+= div64.o copy_32.o crtsavres.o strlen_32.o
+ifndef CONFIG_KASAN
+obj-y	+=	string.o memcmp_$(BITS).o
+obj-$(CONFIG_PPC32)	+= strlen_32.o
+endif
+
+obj-$(CONFIG_PPC32)	+= div64.o copy_32.o crtsavres.o
 
 obj-$(CONFIG_FUNCTION_ERROR_INJECTION)	+= error-inject.o
 
@@ -34,7 +39,7 @@ obj64-$(CONFIG_KPROBES_SANITY_TEST)	+= test_emulate_step.o \
 					   test_emulate_step_exec_instr.o
 
 obj-y			+= checksum_$(BITS).o checksum_wrappers.o \
-			   string_$(BITS).o memcmp_$(BITS).o
+			   string_$(BITS).o
 
 obj-y			+= sstep.o ldstfp.o quad.o
 obj64-y			+= quad.o
arch/powerpc/lib/copy_32.S +5 -1
index ba66846fe973..d5642481fb98 100644
@@ -14,6 +14,7 @@
 #include <asm/ppc_asm.h>
 #include <asm/export.h>
 #include <asm/code-patching-asm.h>
+#include <asm/kasan.h>
 
 #define COPY_16_BYTES		\
 	lwz	r7,4(r4);	\
@@ -68,6 +69,7 @@ CACHELINE_BYTES = L1_CACHE_BYTES
 LG_CACHELINE_BYTES = L1_CACHE_SHIFT
 CACHELINE_MASK = (L1_CACHE_BYTES-1)
 
+#ifndef CONFIG_KASAN
 _GLOBAL(memset16)
 	rlwinm.	r0 ,r5, 31, 1, 31
 	addi	r6, r3, -4
@@ -81,6 +83,7 @@ _GLOBAL(memset16)
 	sth	r4, 4(r6)
 	blr
 EXPORT_SYMBOL(memset16)
+#endif
 
 /*
  * Use dcbz on the complete cache lines in the destination
@@ -91,7 +94,7 @@ EXPORT_SYMBOL(memset16)
  * We therefore skip the optimised bloc that uses dcbz. This jump is
  * replaced by a nop once cache is active. This is done in machine_init()
  */
-_GLOBAL(memset)
+_GLOBAL_KASAN(memset)
 	cmplwi	0,r5,4
 	blt	7f
 
@@ -151,6 +154,7 @@ _GLOBAL(memset)
 	bdnz	9b
 	blr
 EXPORT_SYMBOL(memset)
+EXPORT_SYMBOL_KASAN(memset)
 
 /*
  * This version uses dcbz on the complete cache lines in the
@@ -163,12 +167,12 @@ EXPORT_SYMBOL(memset)
  * We therefore jump to generic_memcpy which doesn't use dcbz. This 

jump is
* replaced by a nop once cache is active. This is done in machine_init() */
-_GLOBAL(memmove)
+_GLOBAL_KASAN(memmove)
cmplw 0,r3,r4
bgt backwards_memcpy
/* fall through */

-_GLOBAL(memcpy)
+_GLOBAL_KASAN(memcpy)
1: b generic_memcpy
patch_site 1b, patch__memcpy_nocache

@@ -244,6 +248,8 @@ _GLOBAL(memcpy)
65: blr
EXPORT_SYMBOL(memcpy)
EXPORT_SYMBOL(memmove)
+EXPORT_SYMBOL_KASAN(memcpy)
+EXPORT_SYMBOL_KASAN(memmove)

generic_memcpy:
srwi. r7,r5,3

arch/powerpc/lib/mem_64.S +7 -2
index 3c3be02f33b7..7f6bd031c306 100644
@@ -12,7 +12,9 @@
 #include <asm/errno.h>
 #include <asm/ppc_asm.h>
 #include <asm/export.h>
+#include <asm/kasan.h>
 
+#ifndef CONFIG_KASAN
 _GLOBAL(__memset16)
 	rlwimi	r4,r4,16,0,15
 	/* fall through */
@@ -29,8 +31,9 @@ _GLOBAL(__memset64)
 EXPORT_SYMBOL(__memset16)
 EXPORT_SYMBOL(__memset32)
 EXPORT_SYMBOL(__memset64)
+#endif
 
-_GLOBAL(memset)
+_GLOBAL_KASAN(memset)
 	neg	r0,r3
 	rlwimi	r4,r4,8,16,23
 	andi.	r0,r0,7			/* # bytes to be 8-byte aligned */
@@ -96,8 +99,9 @@ _GLOBAL(memset)
 	stb	r4,0(r6)
 	blr
 EXPORT_SYMBOL(memset)
+EXPORT_SYMBOL_KASAN(memset)
 
-_GLOBAL_TOC(memmove)
+_GLOBAL_TOC_KASAN(memmove)
 	cmplw	0,r3,r4
 	bgt	backwards_memcpy
 	b	memcpy
@@ -139,3 +143,4 @@ _GLOBAL(backwards_memcpy)
 	mtctr	r7
 	b	1b
 EXPORT_SYMBOL(memmove)
+EXPORT_SYMBOL_KASAN(memmove)
arch/powerpc/lib/memcpy_64.S +3 -1
index 273ea67e60a1..25c3772c1dfb 100644
@@ -11,6 +11,7 @@
 #include <asm/export.h>
 #include <asm/asm-compat.h>
 #include <asm/feature-fixups.h>
+#include <asm/kasan.h>
 
 #ifndef SELFTEST_CASE
 /* For big-endian, 0 == most CPUs, 1 == POWER6, 2 == Cell */
@@ -18,7 +19,7 @@
 #endif
 
 	.align	7
-_GLOBAL_TOC(memcpy)
+_GLOBAL_TOC_KASAN(memcpy)
 BEGIN_FTR_SECTION
 #ifdef __LITTLE_ENDIAN__
 	cmpdi	cr7,r5,0
@@ -230,3 +231,4 @@ END_FTR_SECTION_IFCLR(CPU_FTR_UNALIGNED_LD_STD)
 	blr
 #endif
 EXPORT_SYMBOL(memcpy)
+EXPORT_SYMBOL_KASAN(memcpy)
Quoted 396 lines

It seems to be related to the export line, as if I swap the exports to do fn before __##fn I get:

WARNING: EXPORT symbol "memset" [vmlinux] version generation failed, symbol will not be versioned.

I have narrowed this down to combining 2 EXPORT_SYMBOL()s on one line. This works - no warning:

EXPORT_SYMBOL(memset)
EXPORT_SYMBOL(__memset)

This throws a warning:

EXPORT_SYMBOL(memset) ; EXPORT_SYMBOL(__memset)

I notice in looking at the diff of preprocessed source we end up invoking an asm macro that doesn't seem to have a full final argument, I wonder if that's relevant...

(quoted diff) +2 -1
-___EXPORT_SYMBOL __memset, __memset, ; ___EXPORT_SYMBOL memset, memset,
+___EXPORT_SYMBOL __memset, __memset,
+___EXPORT_SYMBOL memset, memset,

I also notice that nowhere else in the source do people have multiple EXPORT_SYMBOLs on the same line, and other arches seem to just unconditionally export both symbols on multiple lines.

I have no idea how this works for you - maybe it's affected by something 32bit.

How would you feel about this approach instead? I'm not tied to any of the names or anything.

arch/powerpc/include/asm/ppc_asm.h +10 -0
index e0637730a8e7..7b6a91b448dd 100644
@@ -214,6 +214,9 @@ name: \
 
  #define DOTSYM(a)      a
 
+#define PROVIDE_WEAK_ALIAS(strongname, weakname) \
+       .weak weakname ; .set weakname, strongname ;
+
  #else
 
  #define XGLUE(a,b) a##b
@@ -236,6 +239,10 @@ GLUE(.,name):
 
  #define DOTSYM(a)      GLUE(.,a)
 
+#define PROVIDE_WEAK_ALIAS(strongname, weakname) \
+       .weak weakname ; .set weakname, strongname ; \
+       .weak DOTSYM(weakname) ; .set DOTSYM(weakname), DOTSYM(strongname) ;
+
  #endif
 
  #else /* 32-bit */
@@ -251,6 +258,9 @@ GLUE(.,name):
 
  #define _GLOBAL_TOC(name) _GLOBAL(name)
 
+#define PROVIDE_WEAK_ALIAS(strongname, weakname) \
+       .weak weakname ; .set weakname, strongname ;
+
  #endif
 
  /*
arch/powerpc/lib/mem_64.S +8 -4
@@ -33,7 +33,8 @@ EXPORT_SYMBOL(__memset32)
  EXPORT_SYMBOL(__memset64)
  #endif
 
-_GLOBAL_KASAN(memset)
+PROVIDE_WEAK_ALIAS(__memset,memset)
+_GLOBAL(__memset)
         neg     r0,r3
         rlwimi  r4,r4,8,16,23
         andi.   r0,r0,7                 /* # bytes to be 8-byte aligned */
@@ -98,9 +99,11 @@ _GLOBAL_KASAN(memset)
  10:    bflr    31
         stb     r4,0(r6)
         blr
-EXPORT_SYMBOL_KASAN(memset)
+EXPORT_SYMBOL(memset)
+EXPORT_SYMBOL(__memset)
 
-_GLOBAL_TOC_KASAN(memmove)
+PROVIDE_WEAK_ALIAS(__memmove,memove)
+_GLOBAL_TOC(__memmove)
         cmplw   0,r3,r4
         bgt     backwards_memcpy
         b       memcpy
@@ -141,4 +144,5 @@ _GLOBAL(backwards_memcpy)
         beq     2b
         mtctr   r7
         b       1b
-EXPORT_SYMBOL_KASAN(memmove)
+EXPORT_SYMBOL(memmove)
+EXPORT_SYMBOL(__memmove)
arch/powerpc/lib/memcpy_64.S +4 -2
index 862b515b8868..7c1b09556cad 100644
@@ -19,7 +19,8 @@
  #endif
 
         .align  7
-_GLOBAL_TOC_KASAN(memcpy)
+PROVIDE_WEAK_ALIAS(__memcpy,memcpy)
+_GLOBAL_TOC(__memcpy)
  BEGIN_FTR_SECTION
  #ifdef __LITTLE_ENDIAN__
         cmpdi   cr7,r5,0
@@ -230,4 +231,5 @@ END_FTR_SECTION_IFCLR(CPU_FTR_UNALIGNED_LD_STD)
  4:     ld      r3,-STACKFRAMESIZE+STK_REG(R31)(r1)     /* return dest pointer */
         blr
  #endif
-EXPORT_SYMBOL_KASAN(memcpy)
+EXPORT_SYMBOL(__memcpy)
+EXPORT_SYMBOL(memcpy)

Regards,
Daniel

Quoted 268 lines
arch/powerpc/include/asm/kasan.h new file +7 -0
+#else
+#define _GLOBAL_KASAN(fn)	_GLOBAL(fn)
+#define _GLOBAL_TOC_KASAN(fn)	_GLOBAL_TOC(fn)
+#define EXPORT_SYMBOL_KASAN(fn)	EXPORT_SYMBOL(fn)
+#endif
+
+#endif
arch/powerpc/include/asm/string.h +29 -3
index 1647de15a31e..9bf6dffb4090 100644
@@ -4,14 +4,17 @@
 
  #ifdef __KERNEL__
 
+#ifndef CONFIG_KASAN
  #define __HAVE_ARCH_STRNCPY
  #define __HAVE_ARCH_STRNCMP
+#define __HAVE_ARCH_MEMCHR
+#define __HAVE_ARCH_MEMCMP
+#define __HAVE_ARCH_MEMSET16
+#endif
+
  #define __HAVE_ARCH_MEMSET
  #define __HAVE_ARCH_MEMCPY
  #define __HAVE_ARCH_MEMMOVE
-#define __HAVE_ARCH_MEMCMP
-#define __HAVE_ARCH_MEMCHR
-#define __HAVE_ARCH_MEMSET16
  #define __HAVE_ARCH_MEMCPY_FLUSHCACHE
 
  extern char * strcpy(char *,const char *);
@@ -27,7 +30,27 @@ extern int memcmp(const void *,const void *,__kernel_size_t);
  extern void * memchr(const void *,int,__kernel_size_t);
  extern void * memcpy_flushcache(void *,const void *,__kernel_size_t);
 
+void *__memset(void *s, int c, __kernel_size_t count);
+void *__memcpy(void *to, const void *from, __kernel_size_t n);
+void *__memmove(void *to, const void *from, __kernel_size_t n);
+
+#if defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__)
+/*
+ * For files that are not instrumented (e.g. mm/slub.c) we
+ * should use not instrumented version of mem* functions.
+ */
+#define memcpy(dst, src, len) __memcpy(dst, src, len)
+#define memmove(dst, src, len) __memmove(dst, src, len)
+#define memset(s, c, n) __memset(s, c, n)
+
+#ifndef __NO_FORTIFY
+#define __NO_FORTIFY /* FORTIFY_SOURCE uses __builtin_memcpy, etc. */
+#endif
+
+#endif
+
  #ifdef CONFIG_PPC64
+#ifndef CONFIG_KASAN
  #define __HAVE_ARCH_MEMSET32
  #define __HAVE_ARCH_MEMSET64
 
@@ -49,8 +72,11 @@ static inline void *memset64(uint64_t *p, uint64_t v, __kernel_size_t n)
  {
  	return __memset64(p, v, n * 8);
  }
+#endif
  #else
+#ifndef CONFIG_KASAN
  #define __HAVE_ARCH_STRLEN
+#endif
 
  extern void *memset16(uint16_t *, uint16_t, __kernel_size_t);
  #endif
arch/powerpc/kernel/prom_init_check.sh +9 -1
index 667df97d2595..181fd10008ef 100644
@@ -16,8 +16,16 @@
  # If you really need to reference something from prom_init.o add
  # it to the list below:
 
+grep "^CONFIG_KASAN=y$" .config >/dev/null
+if [ $? -eq 0 ]
+then
+	MEM_FUNCS="__memcpy __memset"
+else
+	MEM_FUNCS="memcpy memset"
+fi
+
  WHITELIST="add_reloc_offset __bss_start __bss_stop copy_and_flush
-_end enter_prom memcpy memset reloc_offset __secondary_hold
+_end enter_prom $MEM_FUNCS reloc_offset __secondary_hold
  __secondary_hold_acknowledge __secondary_hold_spinloop __start
  strcmp strcpy strlcpy strlen strncmp strstr kstrtobool logo_linux_clut224
  reloc_got2 kernstart_addr memstart_addr linux_banner _stext
arch/powerpc/lib/Makefile +8 -3
index 79396e184bca..47a4de434c22 100644
@@ -8,9 +8,14 @@ ccflags-$(CONFIG_PPC64)	:= $(NO_MINIMAL_TOC)
  CFLAGS_REMOVE_code-patching.o = $(CC_FLAGS_FTRACE)
  CFLAGS_REMOVE_feature-fixups.o = $(CC_FLAGS_FTRACE)
 
-obj-y += string.o alloc.o code-patching.o feature-fixups.o
+obj-y += alloc.o code-patching.o feature-fixups.o
 
-obj-$(CONFIG_PPC32)	+= div64.o copy_32.o crtsavres.o strlen_32.o
+ifndef CONFIG_KASAN
+obj-y	+=	string.o memcmp_$(BITS).o
+obj-$(CONFIG_PPC32)	+= strlen_32.o
+endif
+
+obj-$(CONFIG_PPC32)	+= div64.o copy_32.o crtsavres.o
 
  obj-$(CONFIG_FUNCTION_ERROR_INJECTION)	+= error-inject.o
 
@@ -34,7 +39,7 @@ obj64-$(CONFIG_KPROBES_SANITY_TEST)	+= test_emulate_step.o \
  					   test_emulate_step_exec_instr.o
 
  obj-y			+= checksum_$(BITS).o checksum_wrappers.o \
-			   string_$(BITS).o memcmp_$(BITS).o
+			   string_$(BITS).o
 
  obj-y			+= sstep.o ldstfp.o quad.o
  obj64-y			+= quad.o
arch/powerpc/lib/copy_32.S +9 -6
index ba66846fe973..fc4fa7246200 100644
@@ -14,6 +14,7 @@
  #include <asm/ppc_asm.h>
  #include <asm/export.h>
  #include <asm/code-patching-asm.h>
+#include <asm/kasan.h>
 
  #define COPY_16_BYTES		\
  	lwz	r7,4(r4);	\
@@ -68,6 +69,7 @@ CACHELINE_BYTES = L1_CACHE_BYTES
  LG_CACHELINE_BYTES = L1_CACHE_SHIFT
  CACHELINE_MASK = (L1_CACHE_BYTES-1)
 
+#ifndef CONFIG_KASAN
  _GLOBAL(memset16)
  	rlwinm.	r0 ,r5, 31, 1, 31
  	addi	r6, r3, -4
@@ -81,6 +83,7 @@ _GLOBAL(memset16)
  	sth	r4, 4(r6)
  	blr
  EXPORT_SYMBOL(memset16)
+#endif
 
  /*
   * Use dcbz on the complete cache lines in the destination
@@ -91,7 +94,7 @@ EXPORT_SYMBOL(memset16)
   * We therefore skip the optimised bloc that uses dcbz. This jump is
   * replaced by a nop once cache is active. This is done in machine_init()
   */
-_GLOBAL(memset)
+_GLOBAL_KASAN(memset)
  	cmplwi	0,r5,4
  	blt	7f
 
@@ -150,7 +153,7 @@ _GLOBAL(memset)
  9:	stbu	r4,1(r6)
  	bdnz	9b
  	blr
-EXPORT_SYMBOL(memset)
+EXPORT_SYMBOL_KASAN(memset)
 
  /*
   * This version uses dcbz on the complete cache lines in the
@@ -163,12 +166,12 @@ EXPORT_SYMBOL(memset)
   * We therefore jump to generic_memcpy which doesn't use dcbz. This jump is
   * replaced by a nop once cache is active. This is done in machine_init()
   */
-_GLOBAL(memmove)
+_GLOBAL_KASAN(memmove)
  	cmplw	0,r3,r4
  	bgt	backwards_memcpy
  	/* fall through */
 
-_GLOBAL(memcpy)
+_GLOBAL_KASAN(memcpy)
  1:	b	generic_memcpy
  	patch_site	1b, patch__memcpy_nocache
 
@@ -242,8 +245,8 @@ _GLOBAL(memcpy)
  	stbu	r0,1(r6)
  	bdnz	40b
  65:	blr
-EXPORT_SYMBOL(memcpy)
-EXPORT_SYMBOL(memmove)
+EXPORT_SYMBOL_KASAN(memcpy)
+EXPORT_SYMBOL_KASAN(memmove)
 
  generic_memcpy:
  	srwi.	r7,r5,3
arch/powerpc/lib/mem_64.S +7 -4
index 3c3be02f33b7..7cd6cf6822a2 100644
@@ -12,7 +12,9 @@
  #include <asm/errno.h>
  #include <asm/ppc_asm.h>
  #include <asm/export.h>
+#include <asm/kasan.h>
 
+#ifndef CONFIG_KASAN
  _GLOBAL(__memset16)
  	rlwimi	r4,r4,16,0,15
  	/* fall through */
@@ -29,8 +31,9 @@ _GLOBAL(__memset64)
  EXPORT_SYMBOL(__memset16)
  EXPORT_SYMBOL(__memset32)
  EXPORT_SYMBOL(__memset64)
+#endif
 
-_GLOBAL(memset)
+_GLOBAL_KASAN(memset)
  	neg	r0,r3
  	rlwimi	r4,r4,8,16,23
  	andi.	r0,r0,7			/* # bytes to be 8-byte aligned */
@@ -95,9 +98,9 @@ _GLOBAL(memset)
  10:	bflr	31
  	stb	r4,0(r6)
  	blr
-EXPORT_SYMBOL(memset)
+EXPORT_SYMBOL_KASAN(memset)
 
-_GLOBAL_TOC(memmove)
+_GLOBAL_TOC_KASAN(memmove)
  	cmplw	0,r3,r4
  	bgt	backwards_memcpy
  	b	memcpy
@@ -138,4 +141,4 @@ _GLOBAL(backwards_memcpy)
  	beq	2b
  	mtctr	r7
  	b	1b
-EXPORT_SYMBOL(memmove)
+EXPORT_SYMBOL_KASAN(memmove)
arch/powerpc/lib/memcpy_64.S +3 -2
index 273ea67e60a1..862b515b8868 100644
@@ -11,6 +11,7 @@
  #include <asm/export.h>
  #include <asm/asm-compat.h>
  #include <asm/feature-fixups.h>
+#include <asm/kasan.h>
 
  #ifndef SELFTEST_CASE
  /* For big-endian, 0 == most CPUs, 1 == POWER6, 2 == Cell */
@@ -18,7 +19,7 @@
  #endif
 
  	.align	7
-_GLOBAL_TOC(memcpy)
+_GLOBAL_TOC_KASAN(memcpy)
  BEGIN_FTR_SECTION
  #ifdef __LITTLE_ENDIAN__
  	cmpdi	cr7,r5,0
@@ -229,4 +230,4 @@ END_FTR_SECTION_IFCLR(CPU_FTR_UNALIGNED_LD_STD)
  4:	ld	r3,-STACKFRAMESIZE+STK_REG(R31)(r1)	/* return dest pointer */
  	blr
  #endif
-EXPORT_SYMBOL(memcpy)
+EXPORT_SYMBOL_KASAN(memcpy)
Signature · 1 line
2.13.3
#7 lore raw

Re: [PATCH v9 02/11] powerpc: prepare string/mem functions for KASAN

Quoted 9 lines
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+#ifdef CONFIG_KASAN
+#define _GLOBAL_KASAN(fn)	.weak fn ; _GLOBAL(__##fn) ; _GLOBAL(fn)
+#define _GLOBAL_TOC_KASAN(fn)	.weak fn ; _GLOBAL_TOC(__##fn) ; _GLOBAL_TOC(fn)
+#define EXPORT_SYMBOL_KASAN(fn)	EXPORT_SYMBOL(__##fn) ; EXPORT_SYMBOL(fn)

I'm having some trouble with this. I get warnings like this:

WARNING: EXPORT symbol "__memcpy" [vmlinux] version generation failed, symbol will not be versioned.

Quoted 8 lines

I don't have this problem, neither with my PPC32 defconfigs nor with ppc64e_defconfig - SPARSEMEM_VMEMMAP + KASAN
Using GCC 8.1

I've been looking into it in more details and can't understand the need for a weak symbol. A weak symbol is to allow it's optional replacement by other code. But here KASAN replaces it inconditionally, so I see no point for a weak symbol here.

If you don't make it weak, and kasan and arch code will export it as a regular symbol, and you will end up with a clash like this:

powerpc64-linux-gnu-ld: mm/kasan/common.o: in function `.memset':
(.text+0x210): multiple definition of `.memset'; arch/powerpc/lib/mem_64.o:(.text+0x0): first defined here
powerpc64-linux-gnu-ld: mm/kasan/common.o:(.opd+0x78): multiple definition of `memset'; arch/powerpc/lib/mem_64.o:(.opd+0x0): first defined here

But your patch doesn't do that, so don't worry :)

Quoted 9 lines

Regarding the export of the functions, I believe that when the functions are defined in KASAN, they should be exported by KASAN and not by the arch. But such a change is out of scope for now. So lets have a double export for now, one day we will drop it.

Regarding export of __memcpy() etc..., there is at least the LKDTM module which inhibits KASAN, so it really needs to be exported.

What about the patch below ?

Yes, that works for me. After de-mangling it I have used it to replace your patch 2 v9.

Thanks for bearing with me.

Regards,
Daniel

Quoted 682 lines

Christophe

arch/powerpc/include/asm/kasan.h new file +15 -0
new file mode 100644
index 000000000000..2c179a39d4ba
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ASM_KASAN_H
+#define __ASM_KASAN_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
+
+#endif
arch/powerpc/include/asm/string.h +29 -3
index 1647de15a31e..9bf6dffb4090 100644
@@ -4,14 +4,17 @@
 
  #ifdef __KERNEL__
 
+#ifndef CONFIG_KASAN
  #define __HAVE_ARCH_STRNCPY
  #define __HAVE_ARCH_STRNCMP
+#define __HAVE_ARCH_MEMCHR
+#define __HAVE_ARCH_MEMCMP
+#define __HAVE_ARCH_MEMSET16
+#endif
+
  #define __HAVE_ARCH_MEMSET
  #define __HAVE_ARCH_MEMCPY
  #define __HAVE_ARCH_MEMMOVE
-#define __HAVE_ARCH_MEMCMP
-#define __HAVE_ARCH_MEMCHR
-#define __HAVE_ARCH_MEMSET16
  #define __HAVE_ARCH_MEMCPY_FLUSHCACHE
 
  extern char * strcpy(char *,const char *);
@@ -27,7 +30,27 @@ extern int memcmp(const void *,const void 
 *,__kernel_size_t);
  extern void * memchr(const void *,int,__kernel_size_t);
  extern void * memcpy_flushcache(void *,const void *,__kernel_size_t);
 
+void *__memset(void *s, int c, __kernel_size_t count);
+void *__memcpy(void *to, const void *from, __kernel_size_t n);
+void *__memmove(void *to, const void *from, __kernel_size_t n);
+
+#if defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__)
+/*
+ * For files that are not instrumented (e.g. mm/slub.c) we
+ * should use not instrumented version of mem* functions.
+ */
+#define memcpy(dst, src, len) __memcpy(dst, src, len)
+#define memmove(dst, src, len) __memmove(dst, src, len)
+#define memset(s, c, n) __memset(s, c, n)
+
+#ifndef __NO_FORTIFY
+#define __NO_FORTIFY /* FORTIFY_SOURCE uses __builtin_memcpy, etc. */
+#endif
+
+#endif
+
  #ifdef CONFIG_PPC64
+#ifndef CONFIG_KASAN
  #define __HAVE_ARCH_MEMSET32
  #define __HAVE_ARCH_MEMSET64
@@ -49,8 +72,11 @@ static inline void *memset64(uint64_t *p, uint64_t v, 
 __kernel_size_t n)
  {
  	return __memset64(p, v, n * 8);
  }
+#endif
  #else
+#ifndef CONFIG_KASAN
  #define __HAVE_ARCH_STRLEN
+#endif
 
  extern void *memset16(uint16_t *, uint16_t, __kernel_size_t);
  #endif
arch/powerpc/kernel/prom_init_check.sh +9 -1
index 667df97d2595..181fd10008ef 100644
@@ -16,8 +16,16 @@
  # If you really need to reference something from prom_init.o add
  # it to the list below:
 
+grep "^CONFIG_KASAN=y$" .config >/dev/null
+if [ $? -eq 0 ]
+then
+	MEM_FUNCS="__memcpy __memset"
+else
+	MEM_FUNCS="memcpy memset"
+fi
+
  WHITELIST="add_reloc_offset __bss_start __bss_stop copy_and_flush
-_end enter_prom memcpy memset reloc_offset __secondary_hold
+_end enter_prom $MEM_FUNCS reloc_offset __secondary_hold
  __secondary_hold_acknowledge __secondary_hold_spinloop __start
  strcmp strcpy strlcpy strlen strncmp strstr kstrtobool logo_linux_clut224
  reloc_got2 kernstart_addr memstart_addr linux_banner _stext
arch/powerpc/lib/Makefile +8 -3
index 79396e184bca..47a4de434c22 100644
@@ -8,9 +8,14 @@ ccflags-$(CONFIG_PPC64)	:= $(NO_MINIMAL_TOC)
  CFLAGS_REMOVE_code-patching.o = $(CC_FLAGS_FTRACE)
  CFLAGS_REMOVE_feature-fixups.o = $(CC_FLAGS_FTRACE)
 
-obj-y += string.o alloc.o code-patching.o feature-fixups.o
+obj-y += alloc.o code-patching.o feature-fixups.o
 
-obj-$(CONFIG_PPC32)	+= div64.o copy_32.o crtsavres.o strlen_32.o
+ifndef CONFIG_KASAN
+obj-y	+=	string.o memcmp_$(BITS).o
+obj-$(CONFIG_PPC32)	+= strlen_32.o
+endif
+
+obj-$(CONFIG_PPC32)	+= div64.o copy_32.o crtsavres.o
 
  obj-$(CONFIG_FUNCTION_ERROR_INJECTION)	+= error-inject.o
 
@@ -34,7 +39,7 @@ obj64-$(CONFIG_KPROBES_SANITY_TEST)	+= 
 test_emulate_step.o \
  					   test_emulate_step_exec_instr.o
 
  obj-y			+= checksum_$(BITS).o checksum_wrappers.o \
-			   string_$(BITS).o memcmp_$(BITS).o
+			   string_$(BITS).o
 
  obj-y			+= sstep.o ldstfp.o quad.o

obj64-y += quad.o

arch/powerpc/lib/copy_32.S +5 -1
index ba66846fe973..d5642481fb98 100644
@@ -14,6 +14,7 @@
  #include <asm/ppc_asm.h>
  #include <asm/export.h>
  #include <asm/code-patching-asm.h>
+#include <asm/kasan.h>
 
  #define COPY_16_BYTES		\
  	lwz	r7,4(r4);	\
@@ -68,6 +69,7 @@ CACHELINE_BYTES = L1_CACHE_BYTES
  LG_CACHELINE_BYTES = L1_CACHE_SHIFT
  CACHELINE_MASK = (L1_CACHE_BYTES-1)
 
+#ifndef CONFIG_KASAN
  _GLOBAL(memset16)
  	rlwinm.	r0 ,r5, 31, 1, 31
  	addi	r6, r3, -4
@@ -81,6 +83,7 @@ _GLOBAL(memset16)
  	sth	r4, 4(r6)
  	blr
  EXPORT_SYMBOL(memset16)
+#endif
 
  /*
   * Use dcbz on the complete cache lines in the destination
@@ -91,7 +94,7 @@ EXPORT_SYMBOL(memset16)
   * We therefore skip the optimised bloc that uses dcbz. This jump is
   * replaced by a nop once cache is active. This is done in machine_init()
   */
-_GLOBAL(memset)
+_GLOBAL_KASAN(memset)
  	cmplwi	0,r5,4
  	blt	7f
 
@@ -151,6 +154,7 @@ _GLOBAL(memset)
  	bdnz	9b
  	blr
  EXPORT_SYMBOL(memset)
+EXPORT_SYMBOL_KASAN(memset)
 
  /*
   * This version uses dcbz on the complete cache lines in the
@@ -163,12 +167,12 @@ EXPORT_SYMBOL(memset)
   * We therefore jump to generic_memcpy which doesn't use dcbz. This 

jump is
* replaced by a nop once cache is active. This is done in machine_init() */

arch/powerpc/lib/copy_32.S +4 -2
-_GLOBAL(memmove)
+_GLOBAL_KASAN(memmove)
  	cmplw	0,r3,r4
  	bgt	backwards_memcpy
  	/* fall through */
 
-_GLOBAL(memcpy)
+_GLOBAL_KASAN(memcpy)
  1:	b	generic_memcpy
  	patch_site	1b, patch__memcpy_nocache
@@ -244,6 +248,8 @@ _GLOBAL(memcpy)
  65:	blr
  EXPORT_SYMBOL(memcpy)
  EXPORT_SYMBOL(memmove)
+EXPORT_SYMBOL_KASAN(memcpy)
+EXPORT_SYMBOL_KASAN(memmove)
 
  generic_memcpy:
  	srwi.	r7,r5,3
arch/powerpc/lib/mem_64.S +7 -2
index 3c3be02f33b7..7f6bd031c306 100644
@@ -12,7 +12,9 @@
  #include <asm/errno.h>
  #include <asm/ppc_asm.h>
  #include <asm/export.h>
+#include <asm/kasan.h>
 
+#ifndef CONFIG_KASAN
  _GLOBAL(__memset16)
  	rlwimi	r4,r4,16,0,15
  	/* fall through */
@@ -29,8 +31,9 @@ _GLOBAL(__memset64)
  EXPORT_SYMBOL(__memset16)
  EXPORT_SYMBOL(__memset32)
  EXPORT_SYMBOL(__memset64)
+#endif
 
-_GLOBAL(memset)
+_GLOBAL_KASAN(memset)
  	neg	r0,r3
  	rlwimi	r4,r4,8,16,23
  	andi.	r0,r0,7			/* # bytes to be 8-byte aligned */
@@ -96,8 +99,9 @@ _GLOBAL(memset)
  	stb	r4,0(r6)
  	blr
  EXPORT_SYMBOL(memset)
+EXPORT_SYMBOL_KASAN(memset)
 
-_GLOBAL_TOC(memmove)
+_GLOBAL_TOC_KASAN(memmove)
  	cmplw	0,r3,r4
  	bgt	backwards_memcpy
  	b	memcpy
@@ -139,3 +143,4 @@ _GLOBAL(backwards_memcpy)
  	mtctr	r7
  	b	1b
  EXPORT_SYMBOL(memmove)
+EXPORT_SYMBOL_KASAN(memmove)
arch/powerpc/lib/memcpy_64.S +3 -1
index 273ea67e60a1..25c3772c1dfb 100644
@@ -11,6 +11,7 @@
  #include <asm/export.h>
  #include <asm/asm-compat.h>
  #include <asm/feature-fixups.h>
+#include <asm/kasan.h>
 
  #ifndef SELFTEST_CASE
  /* For big-endian, 0 == most CPUs, 1 == POWER6, 2 == Cell */
@@ -18,7 +19,7 @@
  #endif
 
  	.align	7
-_GLOBAL_TOC(memcpy)
+_GLOBAL_TOC_KASAN(memcpy)
  BEGIN_FTR_SECTION
  #ifdef __LITTLE_ENDIAN__
  	cmpdi	cr7,r5,0
@@ -230,3 +231,4 @@ END_FTR_SECTION_IFCLR(CPU_FTR_UNALIGNED_LD_STD)
  	blr
  #endif
  EXPORT_SYMBOL(memcpy)
+EXPORT_SYMBOL_KASAN(memcpy)
Quoted 398 lines

It seems to be related to the export line, as if I swap the exports to do fn before __##fn I get:

WARNING: EXPORT symbol "memset" [vmlinux] version generation failed, symbol will not be versioned.

I have narrowed this down to combining 2 EXPORT_SYMBOL()s on one line. This works - no warning:

EXPORT_SYMBOL(memset)
EXPORT_SYMBOL(__memset)

This throws a warning:

EXPORT_SYMBOL(memset) ; EXPORT_SYMBOL(__memset)

I notice in looking at the diff of preprocessed source we end up invoking an asm macro that doesn't seem to have a full final argument, I wonder if that's relevant...

(quoted diff) +2 -1
-___EXPORT_SYMBOL __memset, __memset, ; ___EXPORT_SYMBOL memset, memset,
+___EXPORT_SYMBOL __memset, __memset,
+___EXPORT_SYMBOL memset, memset,

I also notice that nowhere else in the source do people have multiple EXPORT_SYMBOLs on the same line, and other arches seem to just unconditionally export both symbols on multiple lines.

I have no idea how this works for you - maybe it's affected by something 32bit.

How would you feel about this approach instead? I'm not tied to any of the names or anything.

arch/powerpc/include/asm/ppc_asm.h +10 -0
index e0637730a8e7..7b6a91b448dd 100644
@@ -214,6 +214,9 @@ name: \
 
  #define DOTSYM(a)      a
 
+#define PROVIDE_WEAK_ALIAS(strongname, weakname) \
+       .weak weakname ; .set weakname, strongname ;
+
  #else
 
  #define XGLUE(a,b) a##b
@@ -236,6 +239,10 @@ GLUE(.,name):
 
  #define DOTSYM(a)      GLUE(.,a)
 
+#define PROVIDE_WEAK_ALIAS(strongname, weakname) \
+       .weak weakname ; .set weakname, strongname ; \
+       .weak DOTSYM(weakname) ; .set DOTSYM(weakname), DOTSYM(strongname) ;
+
  #endif
 
  #else /* 32-bit */
@@ -251,6 +258,9 @@ GLUE(.,name):
 
  #define _GLOBAL_TOC(name) _GLOBAL(name)
 
+#define PROVIDE_WEAK_ALIAS(strongname, weakname) \
+       .weak weakname ; .set weakname, strongname ;
+
  #endif
 
  /*
arch/powerpc/lib/mem_64.S +8 -4
@@ -33,7 +33,8 @@ EXPORT_SYMBOL(__memset32)
  EXPORT_SYMBOL(__memset64)
  #endif
 
-_GLOBAL_KASAN(memset)
+PROVIDE_WEAK_ALIAS(__memset,memset)
+_GLOBAL(__memset)
         neg     r0,r3
         rlwimi  r4,r4,8,16,23
         andi.   r0,r0,7                 /* # bytes to be 8-byte aligned */
@@ -98,9 +99,11 @@ _GLOBAL_KASAN(memset)
  10:    bflr    31
         stb     r4,0(r6)
         blr
-EXPORT_SYMBOL_KASAN(memset)
+EXPORT_SYMBOL(memset)
+EXPORT_SYMBOL(__memset)
 
-_GLOBAL_TOC_KASAN(memmove)
+PROVIDE_WEAK_ALIAS(__memmove,memove)
+_GLOBAL_TOC(__memmove)
         cmplw   0,r3,r4
         bgt     backwards_memcpy
         b       memcpy
@@ -141,4 +144,5 @@ _GLOBAL(backwards_memcpy)
         beq     2b
         mtctr   r7
         b       1b
-EXPORT_SYMBOL_KASAN(memmove)
+EXPORT_SYMBOL(memmove)
+EXPORT_SYMBOL(__memmove)
arch/powerpc/lib/memcpy_64.S +4 -2
index 862b515b8868..7c1b09556cad 100644
@@ -19,7 +19,8 @@
  #endif
 
         .align  7
-_GLOBAL_TOC_KASAN(memcpy)
+PROVIDE_WEAK_ALIAS(__memcpy,memcpy)
+_GLOBAL_TOC(__memcpy)
  BEGIN_FTR_SECTION
  #ifdef __LITTLE_ENDIAN__
         cmpdi   cr7,r5,0
@@ -230,4 +231,5 @@ END_FTR_SECTION_IFCLR(CPU_FTR_UNALIGNED_LD_STD)
  4:     ld      r3,-STACKFRAMESIZE+STK_REG(R31)(r1)     /* return dest pointer */
         blr
  #endif
-EXPORT_SYMBOL_KASAN(memcpy)
+EXPORT_SYMBOL(__memcpy)
+EXPORT_SYMBOL(memcpy)

Regards,
Daniel

Quoted 268 lines
arch/powerpc/include/asm/kasan.h new file +7 -0
+#else
+#define _GLOBAL_KASAN(fn)	_GLOBAL(fn)
+#define _GLOBAL_TOC_KASAN(fn)	_GLOBAL_TOC(fn)
+#define EXPORT_SYMBOL_KASAN(fn)	EXPORT_SYMBOL(fn)
+#endif
+
+#endif
arch/powerpc/include/asm/string.h +29 -3
index 1647de15a31e..9bf6dffb4090 100644
@@ -4,14 +4,17 @@
 
  #ifdef __KERNEL__
 
+#ifndef CONFIG_KASAN
  #define __HAVE_ARCH_STRNCPY
  #define __HAVE_ARCH_STRNCMP
+#define __HAVE_ARCH_MEMCHR
+#define __HAVE_ARCH_MEMCMP
+#define __HAVE_ARCH_MEMSET16
+#endif
+
  #define __HAVE_ARCH_MEMSET
  #define __HAVE_ARCH_MEMCPY
  #define __HAVE_ARCH_MEMMOVE
-#define __HAVE_ARCH_MEMCMP
-#define __HAVE_ARCH_MEMCHR
-#define __HAVE_ARCH_MEMSET16
  #define __HAVE_ARCH_MEMCPY_FLUSHCACHE
 
  extern char * strcpy(char *,const char *);
@@ -27,7 +30,27 @@ extern int memcmp(const void *,const void *,__kernel_size_t);
  extern void * memchr(const void *,int,__kernel_size_t);
  extern void * memcpy_flushcache(void *,const void *,__kernel_size_t);
 
+void *__memset(void *s, int c, __kernel_size_t count);
+void *__memcpy(void *to, const void *from, __kernel_size_t n);
+void *__memmove(void *to, const void *from, __kernel_size_t n);
+
+#if defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__)
+/*
+ * For files that are not instrumented (e.g. mm/slub.c) we
+ * should use not instrumented version of mem* functions.
+ */
+#define memcpy(dst, src, len) __memcpy(dst, src, len)
+#define memmove(dst, src, len) __memmove(dst, src, len)
+#define memset(s, c, n) __memset(s, c, n)
+
+#ifndef __NO_FORTIFY
+#define __NO_FORTIFY /* FORTIFY_SOURCE uses __builtin_memcpy, etc. */
+#endif
+
+#endif
+
  #ifdef CONFIG_PPC64
+#ifndef CONFIG_KASAN
  #define __HAVE_ARCH_MEMSET32
  #define __HAVE_ARCH_MEMSET64
 
@@ -49,8 +72,11 @@ static inline void *memset64(uint64_t *p, uint64_t v, __kernel_size_t n)
  {
  	return __memset64(p, v, n * 8);
  }
+#endif
  #else
+#ifndef CONFIG_KASAN
  #define __HAVE_ARCH_STRLEN
+#endif
 
  extern void *memset16(uint16_t *, uint16_t, __kernel_size_t);
  #endif
arch/powerpc/kernel/prom_init_check.sh +9 -1
index 667df97d2595..181fd10008ef 100644
@@ -16,8 +16,16 @@
  # If you really need to reference something from prom_init.o add
  # it to the list below:
 
+grep "^CONFIG_KASAN=y$" .config >/dev/null
+if [ $? -eq 0 ]
+then
+	MEM_FUNCS="__memcpy __memset"
+else
+	MEM_FUNCS="memcpy memset"
+fi
+
  WHITELIST="add_reloc_offset __bss_start __bss_stop copy_and_flush
-_end enter_prom memcpy memset reloc_offset __secondary_hold
+_end enter_prom $MEM_FUNCS reloc_offset __secondary_hold
  __secondary_hold_acknowledge __secondary_hold_spinloop __start
  strcmp strcpy strlcpy strlen strncmp strstr kstrtobool logo_linux_clut224
  reloc_got2 kernstart_addr memstart_addr linux_banner _stext
arch/powerpc/lib/Makefile +8 -3
index 79396e184bca..47a4de434c22 100644
@@ -8,9 +8,14 @@ ccflags-$(CONFIG_PPC64)	:= $(NO_MINIMAL_TOC)
  CFLAGS_REMOVE_code-patching.o = $(CC_FLAGS_FTRACE)
  CFLAGS_REMOVE_feature-fixups.o = $(CC_FLAGS_FTRACE)
 
-obj-y += string.o alloc.o code-patching.o feature-fixups.o
+obj-y += alloc.o code-patching.o feature-fixups.o
 
-obj-$(CONFIG_PPC32)	+= div64.o copy_32.o crtsavres.o strlen_32.o
+ifndef CONFIG_KASAN
+obj-y	+=	string.o memcmp_$(BITS).o
+obj-$(CONFIG_PPC32)	+= strlen_32.o
+endif
+
+obj-$(CONFIG_PPC32)	+= div64.o copy_32.o crtsavres.o
 
  obj-$(CONFIG_FUNCTION_ERROR_INJECTION)	+= error-inject.o
 
@@ -34,7 +39,7 @@ obj64-$(CONFIG_KPROBES_SANITY_TEST)	+= test_emulate_step.o \
  					   test_emulate_step_exec_instr.o
 
  obj-y			+= checksum_$(BITS).o checksum_wrappers.o \
-			   string_$(BITS).o memcmp_$(BITS).o
+			   string_$(BITS).o
 
  obj-y			+= sstep.o ldstfp.o quad.o
  obj64-y			+= quad.o
arch/powerpc/lib/copy_32.S +9 -6
index ba66846fe973..fc4fa7246200 100644
@@ -14,6 +14,7 @@
  #include <asm/ppc_asm.h>
  #include <asm/export.h>
  #include <asm/code-patching-asm.h>
+#include <asm/kasan.h>
 
  #define COPY_16_BYTES		\
  	lwz	r7,4(r4);	\
@@ -68,6 +69,7 @@ CACHELINE_BYTES = L1_CACHE_BYTES
  LG_CACHELINE_BYTES = L1_CACHE_SHIFT
  CACHELINE_MASK = (L1_CACHE_BYTES-1)
 
+#ifndef CONFIG_KASAN
  _GLOBAL(memset16)
  	rlwinm.	r0 ,r5, 31, 1, 31
  	addi	r6, r3, -4
@@ -81,6 +83,7 @@ _GLOBAL(memset16)
  	sth	r4, 4(r6)
  	blr
  EXPORT_SYMBOL(memset16)
+#endif
 
  /*
   * Use dcbz on the complete cache lines in the destination
@@ -91,7 +94,7 @@ EXPORT_SYMBOL(memset16)
   * We therefore skip the optimised bloc that uses dcbz. This jump is
   * replaced by a nop once cache is active. This is done in machine_init()
   */
-_GLOBAL(memset)
+_GLOBAL_KASAN(memset)
  	cmplwi	0,r5,4
  	blt	7f
 
@@ -150,7 +153,7 @@ _GLOBAL(memset)
  9:	stbu	r4,1(r6)
  	bdnz	9b
  	blr
-EXPORT_SYMBOL(memset)
+EXPORT_SYMBOL_KASAN(memset)
 
  /*
   * This version uses dcbz on the complete cache lines in the
@@ -163,12 +166,12 @@ EXPORT_SYMBOL(memset)
   * We therefore jump to generic_memcpy which doesn't use dcbz. This jump is
   * replaced by a nop once cache is active. This is done in machine_init()
   */
-_GLOBAL(memmove)
+_GLOBAL_KASAN(memmove)
  	cmplw	0,r3,r4
  	bgt	backwards_memcpy
  	/* fall through */
 
-_GLOBAL(memcpy)
+_GLOBAL_KASAN(memcpy)
  1:	b	generic_memcpy
  	patch_site	1b, patch__memcpy_nocache
 
@@ -242,8 +245,8 @@ _GLOBAL(memcpy)
  	stbu	r0,1(r6)
  	bdnz	40b
  65:	blr
-EXPORT_SYMBOL(memcpy)
-EXPORT_SYMBOL(memmove)
+EXPORT_SYMBOL_KASAN(memcpy)
+EXPORT_SYMBOL_KASAN(memmove)
 
  generic_memcpy:
  	srwi.	r7,r5,3
arch/powerpc/lib/mem_64.S +7 -4
index 3c3be02f33b7..7cd6cf6822a2 100644
@@ -12,7 +12,9 @@
  #include <asm/errno.h>
  #include <asm/ppc_asm.h>
  #include <asm/export.h>
+#include <asm/kasan.h>
 
+#ifndef CONFIG_KASAN
  _GLOBAL(__memset16)
  	rlwimi	r4,r4,16,0,15
  	/* fall through */
@@ -29,8 +31,9 @@ _GLOBAL(__memset64)
  EXPORT_SYMBOL(__memset16)
  EXPORT_SYMBOL(__memset32)
  EXPORT_SYMBOL(__memset64)
+#endif
 
-_GLOBAL(memset)
+_GLOBAL_KASAN(memset)
  	neg	r0,r3
  	rlwimi	r4,r4,8,16,23
  	andi.	r0,r0,7			/* # bytes to be 8-byte aligned */
@@ -95,9 +98,9 @@ _GLOBAL(memset)
  10:	bflr	31
  	stb	r4,0(r6)
  	blr
-EXPORT_SYMBOL(memset)
+EXPORT_SYMBOL_KASAN(memset)
 
-_GLOBAL_TOC(memmove)
+_GLOBAL_TOC_KASAN(memmove)
  	cmplw	0,r3,r4
  	bgt	backwards_memcpy
  	b	memcpy
@@ -138,4 +141,4 @@ _GLOBAL(backwards_memcpy)
  	beq	2b
  	mtctr	r7
  	b	1b
-EXPORT_SYMBOL(memmove)
+EXPORT_SYMBOL_KASAN(memmove)
arch/powerpc/lib/memcpy_64.S +3 -2
index 273ea67e60a1..862b515b8868 100644
@@ -11,6 +11,7 @@
  #include <asm/export.h>
  #include <asm/asm-compat.h>
  #include <asm/feature-fixups.h>
+#include <asm/kasan.h>
 
  #ifndef SELFTEST_CASE
  /* For big-endian, 0 == most CPUs, 1 == POWER6, 2 == Cell */
@@ -18,7 +19,7 @@
  #endif
 
  	.align	7
-_GLOBAL_TOC(memcpy)
+_GLOBAL_TOC_KASAN(memcpy)
  BEGIN_FTR_SECTION
  #ifdef __LITTLE_ENDIAN__
  	cmpdi	cr7,r5,0
@@ -229,4 +230,4 @@ END_FTR_SECTION_IFCLR(CPU_FTR_UNALIGNED_LD_STD)
  4:	ld	r3,-STACKFRAMESIZE+STK_REG(R31)(r1)	/* return dest pointer */
  	blr
  #endif
-EXPORT_SYMBOL(memcpy)
+EXPORT_SYMBOL_KASAN(memcpy)
Signature · 1 line
2.13.3
#8 lore raw

PATCH v9 3/11 powerpc/prom_init: don't use string functions from lib/

In reply to Christophe Leroy

When KASAN is active, the string functions in lib/ are doing the KASAN checks. This is too early for prom_init.

This patch implements dedicated string functions for prom_init, which will be compiled in with KASAN disabled.

Size of prom_init before the patch:
text data bss dec hex filename
12060 488 6960 19508 4c34 arch/powerpc/kernel/prom_init.o

Size of prom_init after the patch:
text data bss dec hex filename
12460 488 6960 19908 4dc4 arch/powerpc/kernel/prom_init.o

This increases the size of prom_init a bit, but as prom_init is in __init section, it is freed after boot anyway.

---
 arch/powerpc/kernel/prom_init.c        | 213 ++++++++++++++++++++++++++-------
 arch/powerpc/kernel/prom_init_check.sh |   2 +-
 2 files changed, 173 insertions(+), 42 deletions(-)
arch/powerpc/kernel/prom_init.c +172 -41
index f33ff4163a51..56d5639ab082 100644
@@ -224,6 +224,137 @@ static bool  __prombss rtas_has_query_cpu_stopped;
 #define PHANDLE_VALID(p)	((p) != 0 && (p) != PROM_ERROR)
 #define IHANDLE_VALID(i)	((i) != 0 && (i) != PROM_ERROR)
 
+/* Copied from lib/string.c and lib/kstrtox.c */
+
+static int __init prom_strcmp(const char *cs, const char *ct)
+{
+	unsigned char c1, c2;
+
+	while (1) {
+		c1 = *cs++;
+		c2 = *ct++;
+		if (c1 != c2)
+			return c1 < c2 ? -1 : 1;
+		if (!c1)
+			break;
+	}
+	return 0;
+}
+
+static char __init *prom_strcpy(char *dest, const char *src)
+{
+	char *tmp = dest;
+
+	while ((*dest++ = *src++) != '\0')
+		/* nothing */;
+	return tmp;
+}
+
+static int __init prom_strncmp(const char *cs, const char *ct, size_t count)
+{
+	unsigned char c1, c2;
+
+	while (count) {
+		c1 = *cs++;
+		c2 = *ct++;
+		if (c1 != c2)
+			return c1 < c2 ? -1 : 1;
+		if (!c1)
+			break;
+		count--;
+	}
+	return 0;
+}
+
+static size_t __init prom_strlen(const char *s)
+{
+	const char *sc;
+
+	for (sc = s; *sc != '\0'; ++sc)
+		/* nothing */;
+	return sc - s;
+}
+
+static int __init prom_memcmp(const void *cs, const void *ct, size_t count)
+{
+	const unsigned char *su1, *su2;
+	int res = 0;
+
+	for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
+		if ((res = *su1 - *su2) != 0)
+			break;
+	return res;
+}
+
+static char __init *prom_strstr(const char *s1, const char *s2)
+{
+	size_t l1, l2;
+
+	l2 = prom_strlen(s2);
+	if (!l2)
+		return (char *)s1;
+	l1 = prom_strlen(s1);
+	while (l1 >= l2) {
+		l1--;
+		if (!prom_memcmp(s1, s2, l2))
+			return (char *)s1;
+		s1++;
+	}
+	return NULL;
+}
+
+#ifdef CONFIG_CMDLINE
+static size_t __init prom_strlcpy(char *dest, const char *src, size_t size)
+{
+	size_t ret = prom_strlen(src);
+
+	if (size) {
+		size_t len = (ret >= size) ? size - 1 : ret;
+		memcpy(dest, src, len);
+		dest[len] = '\0';
+	}
+	return ret;
+}
+#endif
+
+#ifdef CONFIG_PPC_PSERIES
+static int __init prom_strtobool(const char *s, bool *res)
+{
+	if (!s)
+		return -EINVAL;
+
+	switch (s[0]) {
+	case 'y':
+	case 'Y':
+	case '1':
+		*res = true;
+		return 0;
+	case 'n':
+	case 'N':
+	case '0':
+		*res = false;
+		return 0;
+	case 'o':
+	case 'O':
+		switch (s[1]) {
+		case 'n':
+		case 'N':
+			*res = true;
+			return 0;
+		case 'f':
+		case 'F':
+			*res = false;
+			return 0;
+		default:
+			break;
+		}
+	default:
+		break;
+	}
+
+	return -EINVAL;
+}
+#endif
 
 /* This is the one and *ONLY* place where we actually call open
  * firmware.
@@ -555,7 +686,7 @@ static int __init prom_setprop(phandle node, const char *nodename,
 	add_string(&p, tohex((u32)(unsigned long) value));
 	add_string(&p, tohex(valuelen));
 	add_string(&p, tohex(ADDR(pname)));
-	add_string(&p, tohex(strlen(pname)));
+	add_string(&p, tohex(prom_strlen(pname)));
 	add_string(&p, "property");
 	*p = 0;
 	return call_prom("interpret", 1, 1, (u32)(unsigned long) cmd);
@@ -639,25 +770,25 @@ static void __init early_cmdline_parse(void)
 		l = prom_getprop(prom.chosen, "bootargs", p, COMMAND_LINE_SIZE-1);
 #ifdef CONFIG_CMDLINE
 	if (l <= 0 || p[0] == '\0') /* dbl check */
-		strlcpy(prom_cmd_line,
+		prom_strlcpy(prom_cmd_line,
 			CONFIG_CMDLINE, sizeof(prom_cmd_line));
 #endif /* CONFIG_CMDLINE */
 	prom_printf("command line: %s\n", prom_cmd_line);
 
 #ifdef CONFIG_PPC64
-	opt = strstr(prom_cmd_line, "iommu=");
+	opt = prom_strstr(prom_cmd_line, "iommu=");
 	if (opt) {
 		prom_printf("iommu opt is: %s\n", opt);
 		opt += 6;
 		while (*opt && *opt == ' ')
 			opt++;
-		if (!strncmp(opt, "off", 3))
+		if (!prom_strncmp(opt, "off", 3))
 			prom_iommu_off = 1;
-		else if (!strncmp(opt, "force", 5))
+		else if (!prom_strncmp(opt, "force", 5))
 			prom_iommu_force_on = 1;
 	}
 #endif
-	opt = strstr(prom_cmd_line, "mem=");
+	opt = prom_strstr(prom_cmd_line, "mem=");
 	if (opt) {
 		opt += 4;
 		prom_memory_limit = prom_memparse(opt, (const char **)&opt);
@@ -669,13 +800,13 @@ static void __init early_cmdline_parse(void)
 
 #ifdef CONFIG_PPC_PSERIES
 	prom_radix_disable = !IS_ENABLED(CONFIG_PPC_RADIX_MMU_DEFAULT);
-	opt = strstr(prom_cmd_line, "disable_radix");
+	opt = prom_strstr(prom_cmd_line, "disable_radix");
 	if (opt) {
 		opt += 13;
 		if (*opt && *opt == '=') {
 			bool val;
 
-			if (kstrtobool(++opt, &val))
+			if (prom_strtobool(++opt, &val))
 				prom_radix_disable = false;
 			else
 				prom_radix_disable = val;
@@ -1028,7 +1159,7 @@ static int __init prom_count_smt_threads(void)
 		type[0] = 0;
 		prom_getprop(node, "device_type", type, sizeof(type));
 
-		if (strcmp(type, "cpu"))
+		if (prom_strcmp(type, "cpu"))
 			continue;
 		/*
 		 * There is an entry for each smt thread, each entry being
@@ -1475,7 +1606,7 @@ static void __init prom_init_mem(void)
 			 */
 			prom_getprop(node, "name", type, sizeof(type));
 		}
-		if (strcmp(type, "memory"))
+		if (prom_strcmp(type, "memory"))
 			continue;
 
 		plen = prom_getprop(node, "reg", regbuf, sizeof(regbuf));
@@ -1756,19 +1887,19 @@ static void __init prom_initialize_tce_table(void)
 		prom_getprop(node, "device_type", type, sizeof(type));
 		prom_getprop(node, "model", model, sizeof(model));
 
-		if ((type[0] == 0) || (strstr(type, "pci") == NULL))
+		if ((type[0] == 0) || (prom_strstr(type, "pci") == NULL))
 			continue;
 
 		/* Keep the old logic intact to avoid regression. */
 		if (compatible[0] != 0) {
-			if ((strstr(compatible, "python") == NULL) &&
-			    (strstr(compatible, "Speedwagon") == NULL) &&
-			    (strstr(compatible, "Winnipeg") == NULL))
+			if ((prom_strstr(compatible, "python") == NULL) &&
+			    (prom_strstr(compatible, "Speedwagon") == NULL) &&
+			    (prom_strstr(compatible, "Winnipeg") == NULL))
 				continue;
 		} else if (model[0] != 0) {
-			if ((strstr(model, "ython") == NULL) &&
-			    (strstr(model, "peedwagon") == NULL) &&
-			    (strstr(model, "innipeg") == NULL))
+			if ((prom_strstr(model, "ython") == NULL) &&
+			    (prom_strstr(model, "peedwagon") == NULL) &&
+			    (prom_strstr(model, "innipeg") == NULL))
 				continue;
 		}
 
@@ -1917,12 +2048,12 @@ static void __init prom_hold_cpus(void)
 
 		type[0] = 0;
 		prom_getprop(node, "device_type", type, sizeof(type));
-		if (strcmp(type, "cpu") != 0)
+		if (prom_strcmp(type, "cpu") != 0)
 			continue;
 
 		/* Skip non-configured cpus. */
 		if (prom_getprop(node, "status", type, sizeof(type)) > 0)
-			if (strcmp(type, "okay") != 0)
+			if (prom_strcmp(type, "okay") != 0)
 				continue;
 
 		reg = cpu_to_be32(-1); /* make sparse happy */
@@ -1998,9 +2129,9 @@ static void __init prom_find_mmu(void)
 		return;
 	version[sizeof(version) - 1] = 0;
 	/* XXX might need to add other versions here */
-	if (strcmp(version, "Open Firmware, 1.0.5") == 0)
+	if (prom_strcmp(version, "Open Firmware, 1.0.5") == 0)
 		of_workarounds = OF_WA_CLAIM;
-	else if (strncmp(version, "FirmWorks,3.", 12) == 0) {
+	else if (prom_strncmp(version, "FirmWorks,3.", 12) == 0) {
 		of_workarounds = OF_WA_CLAIM | OF_WA_LONGTRAIL;
 		call_prom("interpret", 1, 1, "dev /memory 0 to allow-reclaim");
 	} else
@@ -2033,7 +2164,7 @@ static void __init prom_init_stdout(void)
 	call_prom("instance-to-path", 3, 1, prom.stdout, path, 255);
 	prom_printf("OF stdout device is: %s\n", of_stdout_device);
 	prom_setprop(prom.chosen, "/chosen", "linux,stdout-path",
-		     path, strlen(path) + 1);
+		     path, prom_strlen(path) + 1);
 
 	/* instance-to-package fails on PA-Semi */
 	stdout_node = call_prom("instance-to-package", 1, 1, prom.stdout);
@@ -2043,7 +2174,7 @@ static void __init prom_init_stdout(void)
 		/* If it's a display, note it */
 		memset(type, 0, sizeof(type));
 		prom_getprop(stdout_node, "device_type", type, sizeof(type));
-		if (strcmp(type, "display") == 0)
+		if (prom_strcmp(type, "display") == 0)
 			prom_setprop(stdout_node, path, "linux,boot-display", NULL, 0);
 	}
 }
@@ -2064,19 +2195,19 @@ static int __init prom_find_machine_type(void)
 		compat[len] = 0;
 		while (i < len) {
 			char *p = &compat[i];
-			int sl = strlen(p);
+			int sl = prom_strlen(p);
 			if (sl == 0)
 				break;
-			if (strstr(p, "Power Macintosh") ||
-			    strstr(p, "MacRISC"))
+			if (prom_strstr(p, "Power Macintosh") ||
+			    prom_strstr(p, "MacRISC"))
 				return PLATFORM_POWERMAC;
 #ifdef CONFIG_PPC64
 			/* We must make sure we don't detect the IBM Cell
 			 * blades as pSeries due to some firmware issues,
 			 * so we do it here.
 			 */
-			if (strstr(p, "IBM,CBEA") ||
-			    strstr(p, "IBM,CPBW-1.0"))
+			if (prom_strstr(p, "IBM,CBEA") ||
+			    prom_strstr(p, "IBM,CPBW-1.0"))
 				return PLATFORM_GENERIC;
 #endif /* CONFIG_PPC64 */
 			i += sl + 1;
@@ -2093,7 +2224,7 @@ static int __init prom_find_machine_type(void)
 			   compat, sizeof(compat)-1);
 	if (len <= 0)
 		return PLATFORM_GENERIC;
-	if (strcmp(compat, "chrp"))
+	if (prom_strcmp(compat, "chrp"))
 		return PLATFORM_GENERIC;
 
 	/* Default to pSeries. We need to know if we are running LPAR */
@@ -2155,7 +2286,7 @@ static void __init prom_check_displays(void)
 	for (node = 0; prom_next_node(&node); ) {
 		memset(type, 0, sizeof(type));
 		prom_getprop(node, "device_type", type, sizeof(type));
-		if (strcmp(type, "display") != 0)
+		if (prom_strcmp(type, "display") != 0)
 			continue;
 
 		/* It seems OF doesn't null-terminate the path :-( */
@@ -2259,9 +2390,9 @@ static unsigned long __init dt_find_string(char *str)
 	s = os = (char *)dt_string_start;
 	s += 4;
 	while (s <  (char *)dt_string_end) {
-		if (strcmp(s, str) == 0)
+		if (prom_strcmp(s, str) == 0)
 			return s - os;
-		s += strlen(s) + 1;
+		s += prom_strlen(s) + 1;
 	}
 	return 0;
 }
@@ -2294,7 +2425,7 @@ static void __init scan_dt_build_strings(phandle node,
 		}
 
  		/* skip "name" */
- 		if (strcmp(namep, "name") == 0) {
+		if (prom_strcmp(namep, "name") == 0) {
  			*mem_start = (unsigned long)namep;
  			prev_name = "name";
  			continue;
@@ -2306,7 +2437,7 @@ static void __init scan_dt_build_strings(phandle node,
 			namep = sstart + soff;
 		} else {
 			/* Trim off some if we can */
-			*mem_start = (unsigned long)namep + strlen(namep) + 1;
+			*mem_start = (unsigned long)namep + prom_strlen(namep) + 1;
 			dt_string_end = *mem_start;
 		}
 		prev_name = namep;
@@ -2375,7 +2506,7 @@ static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
 			break;
 
  		/* skip "name" */
- 		if (strcmp(pname, "name") == 0) {
+		if (prom_strcmp(pname, "name") == 0) {
  			prev_name = "name";
  			continue;
  		}
@@ -2406,7 +2537,7 @@ static void __init scan_dt_build_struct(phandle node, unsigned long *mem_start,
 		call_prom("getprop", 4, 1, node, pname, valp, l);
 		*mem_start = _ALIGN(*mem_start, 4);
 
-		if (!strcmp(pname, "phandle"))
+		if (!prom_strcmp(pname, "phandle"))
 			has_phandle = 1;
 	}
 
@@ -2476,8 +2607,8 @@ static void __init flatten_device_tree(void)
 
 	/* Add "phandle" in there, we'll need it */
 	namep = make_room(&mem_start, &mem_end, 16, 1);
-	strcpy(namep, "phandle");
-	mem_start = (unsigned long)namep + strlen(namep) + 1;
+	prom_strcpy(namep, "phandle");
+	mem_start = (unsigned long)namep + prom_strlen(namep) + 1;
 
 	/* Build string array */
 	prom_printf("Building dt strings...\n"); 
@@ -2799,7 +2930,7 @@ static void __init fixup_device_tree_efika(void)
 	rv = prom_getprop(node, "model", prop, sizeof(prop));
 	if (rv == PROM_ERROR)
 		return;
-	if (strcmp(prop, "EFIKA5K2"))
+	if (prom_strcmp(prop, "EFIKA5K2"))
 		return;
 
 	prom_printf("Applying EFIKA device tree fixups\n");
@@ -2807,13 +2938,13 @@ static void __init fixup_device_tree_efika(void)
 	/* Claiming to be 'chrp' is death */
 	node = call_prom("finddevice", 1, 1, ADDR("/"));
 	rv = prom_getprop(node, "device_type", prop, sizeof(prop));
-	if (rv != PROM_ERROR && (strcmp(prop, "chrp") == 0))
+	if (rv != PROM_ERROR && (prom_strcmp(prop, "chrp") == 0))
 		prom_setprop(node, "/", "device_type", "efika", sizeof("efika"));
 
 	/* CODEGEN,description is exposed in /proc/cpuinfo so
 	   fix that too */
 	rv = prom_getprop(node, "CODEGEN,description", prop, sizeof(prop));
-	if (rv != PROM_ERROR && (strstr(prop, "CHRP")))
+	if (rv != PROM_ERROR && (prom_strstr(prop, "CHRP")))
 		prom_setprop(node, "/", "CODEGEN,description",
 			     "Efika 5200B PowerPC System",
 			     sizeof("Efika 5200B PowerPC System"));
arch/powerpc/kernel/prom_init_check.sh +1 -1
index 181fd10008ef..4cac45cb5de5 100644
@@ -27,7 +27,7 @@ fi
 WHITELIST="add_reloc_offset __bss_start __bss_stop copy_and_flush
 _end enter_prom $MEM_FUNCS reloc_offset __secondary_hold
 __secondary_hold_acknowledge __secondary_hold_spinloop __start
-strcmp strcpy strlcpy strlen strncmp strstr kstrtobool logo_linux_clut224
+logo_linux_clut224
 reloc_got2 kernstart_addr memstart_addr linux_banner _stext
 __prom_init_toc_start __prom_init_toc_end btext_setup_display TOC."
 
Signature · 1 line
2.13.3
#9 lore raw

PATCH v9 4/11 powerpc/mm: don't use direct assignation during early boot.

In reply to Christophe Leroy

In kernel/cputable.c, explicitly use memcpy() instead of *y = *x; This will allow GCC to replace it with __memcpy() when KASAN is selected.

---
 arch/powerpc/kernel/cputable.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
arch/powerpc/kernel/cputable.c +10 -3
index 1eab54bc6ee9..cd12f362b61f 100644
@@ -2147,7 +2147,11 @@ void __init set_cur_cpu_spec(struct cpu_spec *s)
 	struct cpu_spec *t = &the_cpu_spec;
 
 	t = PTRRELOC(t);
-	*t = *s;
+	/*
+	 * use memcpy() instead of *t = *s so that GCC replaces it
+	 * by __memcpy() when KASAN is active
+	 */
+	memcpy(t, s, sizeof(*t));
 
 	*PTRRELOC(&cur_cpu_spec) = &the_cpu_spec;
 }
@@ -2161,8 +2165,11 @@ static struct cpu_spec * __init setup_cpu_spec(unsigned long offset,
 	t = PTRRELOC(t);
 	old = *t;
 
-	/* Copy everything, then do fixups */
-	*t = *s;
+	/*
+	 * Copy everything, then do fixups. Use memcpy() instead of *t = *s
+	 * so that GCC replaces it by __memcpy() when KASAN is active
+	 */
+	memcpy(t, s, sizeof(*t));
 
 	/*
 	 * If we are overriding a previous value derived from the real
Signature · 1 line
2.13.3
#10 lore raw

PATCH v9 5/11 powerpc/32: use memset() instead of memset_io() to zero BSS

In reply to Christophe Leroy

Since commit 400c47d81ca38 ("powerpc32: memset: only use dcbz once cache is enabled"), memset() can be used before activation of the cache, so no need to use memset_io() for zeroing the BSS.

---
 arch/powerpc/kernel/early_32.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/powerpc/kernel/early_32.c +2 -2
index cf3cdd81dc47..3482118ffe76 100644
@@ -21,8 +21,8 @@ notrace unsigned long __init early_init(unsigned long dt_ptr)
 {
 	unsigned long offset = reloc_offset();
 
-	/* First zero the BSS -- use memset_io, some platforms don't have caches on yet */
-	memset_io((void __iomem *)PTRRELOC(&__bss_start), 0, __bss_stop - __bss_start);
+	/* First zero the BSS */
+	memset(PTRRELOC(&__bss_start), 0, __bss_stop - __bss_start);
 
 	/*
 	 * Identify the CPU type and fix up code sections
Signature · 1 line
2.13.3
#11 lore raw

PATCH v9 6/11 powerpc/32: make KVIRT_TOP dependent on FIXMAP_START

In reply to Christophe Leroy

When we add KASAN shadow area, KVIRT_TOP can't be anymore fixed at 0xfe000000.

This patch uses FIXADDR_START to define KVIRT_TOP.

---
 arch/powerpc/include/asm/book3s/32/pgtable.h | 13 ++++++++++---
 arch/powerpc/include/asm/nohash/32/pgtable.h | 13 ++++++++++---
 2 files changed, 20 insertions(+), 6 deletions(-)
arch/powerpc/include/asm/book3s/32/pgtable.h +10 -3
index aa8406b8f7ba..838de59f6754 100644
@@ -134,15 +134,24 @@ static inline bool pte_user(pte_t pte)
 #define PGDIR_MASK	(~(PGDIR_SIZE-1))
 
 #define USER_PTRS_PER_PGD	(TASK_SIZE / PGDIR_SIZE)
+
+#ifndef __ASSEMBLY__
+
+int map_kernel_page(unsigned long va, phys_addr_t pa, pgprot_t prot);
+
+#endif /* !__ASSEMBLY__ */
+
 /*
  * This is the bottom of the PKMAP area with HIGHMEM or an arbitrary
  * value (for now) on others, from where we can start layout kernel
  * virtual space that goes below PKMAP and FIXMAP
  */
+#include <asm/fixmap.h>
+
 #ifdef CONFIG_HIGHMEM
 #define KVIRT_TOP	PKMAP_BASE
 #else
-#define KVIRT_TOP	(0xfe000000UL)	/* for now, could be FIXMAP_BASE ? */
+#define KVIRT_TOP	FIXADDR_START
 #endif
 
 /*
@@ -373,8 +382,6 @@ static inline void __ptep_set_access_flags(struct vm_area_struct *vma,
 #define __pte_to_swp_entry(pte)		((swp_entry_t) { pte_val(pte) >> 3 })
 #define __swp_entry_to_pte(x)		((pte_t) { (x).val << 3 })
 
-int map_kernel_page(unsigned long va, phys_addr_t pa, pgprot_t prot);
-
 /* Generic accessors to PTE bits */
 static inline int pte_write(pte_t pte)		{ return !!(pte_val(pte) & _PAGE_RW);}
 static inline int pte_read(pte_t pte)		{ return 1; }
arch/powerpc/include/asm/nohash/32/pgtable.h +10 -3
index bed433358260..0284f8f5305f 100644
@@ -64,15 +64,24 @@ extern int icache_44x_need_flush;
 #define pgd_ERROR(e) \
 	pr_err("%s:%d: bad pgd %08lx.\n", __FILE__, __LINE__, pgd_val(e))
 
+#ifndef __ASSEMBLY__
+
+int map_kernel_page(unsigned long va, phys_addr_t pa, pgprot_t prot);
+
+#endif /* !__ASSEMBLY__ */
+
+
 /*
  * This is the bottom of the PKMAP area with HIGHMEM or an arbitrary
  * value (for now) on others, from where we can start layout kernel
  * virtual space that goes below PKMAP and FIXMAP
  */
+#include <asm/fixmap.h>
+
 #ifdef CONFIG_HIGHMEM
 #define KVIRT_TOP	PKMAP_BASE
 #else
-#define KVIRT_TOP	(0xfe000000UL)	/* for now, could be FIXMAP_BASE ? */
+#define KVIRT_TOP	FIXADDR_START
 #endif
 
 /*
@@ -379,8 +388,6 @@ static inline int pte_young(pte_t pte)
 #define __pte_to_swp_entry(pte)		((swp_entry_t) { pte_val(pte) >> 3 })
 #define __swp_entry_to_pte(x)		((pte_t) { (x).val << 3 })
 
-int map_kernel_page(unsigned long va, phys_addr_t pa, pgprot_t prot);
-
 #endif /* !__ASSEMBLY__ */
 
 #endif /* __ASM_POWERPC_NOHASH_32_PGTABLE_H */
Signature · 1 line
2.13.3
#12 lore raw

PATCH v9 7/11 powerpc/32: prepare shadow area for KASAN

In reply to Christophe Leroy

This patch prepares a shadow area for KASAN.

The shadow area will be at the top of the kernel virtual memory space above the fixmap area and will occupy one eighth of the total kernel virtual memory space.

---
 arch/powerpc/Kconfig.debug        |  5 +++++
 arch/powerpc/include/asm/fixmap.h |  5 +++++
 arch/powerpc/include/asm/kasan.h  | 16 ++++++++++++++++
 arch/powerpc/mm/mem.c             |  4 ++++
 arch/powerpc/mm/ptdump/ptdump.c   |  8 ++++++++
 5 files changed, 38 insertions(+)
arch/powerpc/Kconfig.debug +5 -0
index 4e00cb0a5464..61febbbdd02b 100644
@@ -366,3 +366,8 @@ config PPC_FAST_ENDIAN_SWITCH
         depends on DEBUG_KERNEL && PPC_BOOK3S_64
         help
 	  If you're unsure what this is, say N.
+
+config KASAN_SHADOW_OFFSET
+	hex
+	depends on KASAN
+	default 0xe0000000
arch/powerpc/include/asm/fixmap.h +5 -0
index b9fbed84ddca..0cfc365d814b 100644
@@ -22,7 +22,12 @@
 #include <asm/kmap_types.h>
 #endif
 
+#ifdef CONFIG_KASAN
+#include <asm/kasan.h>
+#define FIXADDR_TOP	(KASAN_SHADOW_START - PAGE_SIZE)
+#else
 #define FIXADDR_TOP	((unsigned long)(-PAGE_SIZE))
+#endif
 
 /*
  * Here we define all the compile-time 'special' virtual
arch/powerpc/include/asm/kasan.h +16 -0
index c3161b8fc017..8dc1e3819171 100644
@@ -12,4 +12,20 @@
 #define EXPORT_SYMBOL_KASAN(fn)	EXPORT_SYMBOL(fn)
 #endif
 
+#ifndef __ASSEMBLY__
+
+#include <asm/page.h>
+
+#define KASAN_SHADOW_SCALE_SHIFT	3
+
+#define KASAN_SHADOW_OFFSET	ASM_CONST(CONFIG_KASAN_SHADOW_OFFSET)
+
+#define KASAN_SHADOW_START	(KASAN_SHADOW_OFFSET + \
+				 (PAGE_OFFSET >> KASAN_SHADOW_SCALE_SHIFT))
+
+#define KASAN_SHADOW_END	0UL
+
+#define KASAN_SHADOW_SIZE	(KASAN_SHADOW_END - KASAN_SHADOW_START)
+
+#endif /* __ASSEMBLY */
 #endif
arch/powerpc/mm/mem.c +4 -0
index f6787f90e158..4e7fa4eb2dd3 100644
@@ -309,6 +309,10 @@ void __init mem_init(void)
 	mem_init_print_info(NULL);
 #ifdef CONFIG_PPC32
 	pr_info("Kernel virtual memory layout:\n");
+#ifdef CONFIG_KASAN
+	pr_info("  * 0x%08lx..0x%08lx  : kasan shadow mem\n",
+		KASAN_SHADOW_START, KASAN_SHADOW_END);
+#endif
 	pr_info("  * 0x%08lx..0x%08lx  : fixmap\n", FIXADDR_START, FIXADDR_TOP);
 #ifdef CONFIG_HIGHMEM
 	pr_info("  * 0x%08lx..0x%08lx  : highmem PTEs\n",
arch/powerpc/mm/ptdump/ptdump.c +8 -0
index 37138428ab55..812ed680024f 100644
@@ -101,6 +101,10 @@ static struct addr_marker address_markers[] = {
 	{ 0,	"Fixmap start" },
 	{ 0,	"Fixmap end" },
 #endif
+#ifdef CONFIG_KASAN
+	{ 0,	"kasan shadow mem start" },
+	{ 0,	"kasan shadow mem end" },
+#endif
 	{ -1,	NULL },
 };
 
@@ -322,6 +326,10 @@ static void populate_markers(void)
 #endif
 	address_markers[i++].start_address = FIXADDR_START;
 	address_markers[i++].start_address = FIXADDR_TOP;
+#ifdef CONFIG_KASAN
+	address_markers[i++].start_address = KASAN_SHADOW_START;
+	address_markers[i++].start_address = KASAN_SHADOW_END;
+#endif
 #endif /* CONFIG_PPC64 */
 }
 
Signature · 1 line
2.13.3
#13 lore raw

PATCH v9 8/11 powerpc: disable KASAN instrumentation on early/critical files.

In reply to Christophe Leroy

All files containing functions run before kasan_early_init() is called must have KASAN instrumentation disabled.

For those file, branch profiling also have to be disabled otherwise each if () generates a call to ftrace_likely_update().

---
 arch/powerpc/kernel/Makefile             | 12 ++++++++++++
 arch/powerpc/lib/Makefile                |  8 ++++++++
 arch/powerpc/mm/Makefile                 |  6 ++++++
 arch/powerpc/platforms/powermac/Makefile |  6 ++++++
 arch/powerpc/purgatory/Makefile          |  3 +++
 arch/powerpc/xmon/Makefile               |  1 +
 6 files changed, 36 insertions(+)
arch/powerpc/kernel/Makefile +12 -0
index 45e47752b692..0ea6c4aa3a20 100644
@@ -31,6 +31,18 @@ CFLAGS_REMOVE_btext.o = $(CC_FLAGS_FTRACE)
 CFLAGS_REMOVE_prom.o = $(CC_FLAGS_FTRACE)
 endif
 
+KASAN_SANITIZE_early_32.o := n
+KASAN_SANITIZE_cputable.o := n
+KASAN_SANITIZE_prom_init.o := n
+KASAN_SANITIZE_btext.o := n
+
+ifdef CONFIG_KASAN
+CFLAGS_early_32.o += -DDISABLE_BRANCH_PROFILING
+CFLAGS_cputable.o += -DDISABLE_BRANCH_PROFILING
+CFLAGS_prom_init.o += -DDISABLE_BRANCH_PROFILING
+CFLAGS_btext.o += -DDISABLE_BRANCH_PROFILING
+endif
+
 obj-y				:= cputable.o ptrace.o syscalls.o \
 				   irq.o align.o signal_32.o pmc.o vdso.o \
 				   process.o systbl.o idle.o \
arch/powerpc/lib/Makefile +8 -0
index 47a4de434c22..c55f9c27bf79 100644
@@ -8,6 +8,14 @@ ccflags-$(CONFIG_PPC64)	:= $(NO_MINIMAL_TOC)
 CFLAGS_REMOVE_code-patching.o = $(CC_FLAGS_FTRACE)
 CFLAGS_REMOVE_feature-fixups.o = $(CC_FLAGS_FTRACE)
 
+KASAN_SANITIZE_code-patching.o := n
+KASAN_SANITIZE_feature-fixups.o := n
+
+ifdef CONFIG_KASAN
+CFLAGS_code-patching.o += -DDISABLE_BRANCH_PROFILING
+CFLAGS_feature-fixups.o += -DDISABLE_BRANCH_PROFILING
+endif
+
 obj-y += alloc.o code-patching.o feature-fixups.o
 
 ifndef CONFIG_KASAN
arch/powerpc/mm/Makefile +6 -0
index d52ec118e09d..240d73dce6bb 100644
@@ -7,6 +7,12 @@ ccflags-$(CONFIG_PPC64)	:= $(NO_MINIMAL_TOC)
 
 CFLAGS_REMOVE_slb.o = $(CC_FLAGS_FTRACE)
 
+KASAN_SANITIZE_ppc_mmu_32.o := n
+
+ifdef CONFIG_KASAN
+CFLAGS_ppc_mmu_32.o  		+= -DDISABLE_BRANCH_PROFILING
+endif
+
 obj-y				:= fault.o mem.o pgtable.o mmap.o \
 				   init_$(BITS).o pgtable_$(BITS).o \
 				   init-common.o mmu_context.o drmem.o
arch/powerpc/platforms/powermac/Makefile +6 -0
index 923bfb340433..859efbacdf06 100644
@@ -2,6 +2,12 @@
 CFLAGS_bootx_init.o  		+= -fPIC
 CFLAGS_bootx_init.o  		+= $(call cc-option, -fno-stack-protector)
 
+KASAN_SANITIZE_bootx_init.o := n
+
+ifdef CONFIG_KASAN
+CFLAGS_bootx_init.o  		+= -DDISABLE_BRANCH_PROFILING
+endif
+
 ifdef CONFIG_FUNCTION_TRACER
 # Do not trace early boot code
 CFLAGS_REMOVE_bootx_init.o = $(CC_FLAGS_FTRACE)
arch/powerpc/purgatory/Makefile +3 -0
index 4314ba5baf43..7c6d8b14f440 100644
@@ -1,4 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
+
+KASAN_SANITIZE := n
+
 targets += trampoline.o purgatory.ro kexec-purgatory.c
 
 LDFLAGS_purgatory.ro := -e purgatory_start -r --no-undefined
arch/powerpc/xmon/Makefile +1 -0
index 3050f9323254..f142570ad860 100644
@@ -7,6 +7,7 @@ subdir-ccflags-y := $(call cc-disable-warning, builtin-requires-header)
 GCOV_PROFILE := n
 KCOV_INSTRUMENT := n
 UBSAN_SANITIZE := n
+KASAN_SANITIZE := n
 
 # Disable ftrace for the entire directory
 ORIG_CFLAGS := $(KBUILD_CFLAGS)
Signature · 1 line
2.13.3
#14 lore raw

PATCH v9 9/11 powerpc/32: Add KASAN support

In reply to Christophe Leroy

This patch adds KASAN support for PPC32. The following patch will add an early activation of hash table for book3s. Until then, a warning will be raised if trying to use KASAN on an hash 6xx.

To support KASAN, this patch initialises that MMU mapings for accessing to the KASAN shadow area defined in a previous patch.

An early mapping is set as soon as the kernel code has been relocated at its definitive place.

Then the definitive mapping is set once paging is initialised.

For modules, the shadow area is allocated at module_alloc().

---
 arch/powerpc/Kconfig                  |   1 +
 arch/powerpc/include/asm/kasan.h      |   7 ++
 arch/powerpc/kernel/head_32.S         |   3 +
 arch/powerpc/kernel/head_40x.S        |   3 +
 arch/powerpc/kernel/head_44x.S        |   3 +
 arch/powerpc/kernel/head_8xx.S        |   3 +
 arch/powerpc/kernel/head_fsl_booke.S  |   3 +
 arch/powerpc/kernel/setup-common.c    |   3 +
 arch/powerpc/mm/Makefile              |   1 +
 arch/powerpc/mm/kasan/Makefile        |   5 ++
 arch/powerpc/mm/kasan/kasan_init_32.c | 152 ++++++++++++++++++++++++++++++++++
 11 files changed, 184 insertions(+)
 create mode 100644 arch/powerpc/mm/kasan/Makefile
 create mode 100644 arch/powerpc/mm/kasan/kasan_init_32.c
arch/powerpc/Kconfig +1 -0
index 652c25260838..8d6108c83299 100644
@@ -172,6 +172,7 @@ config PPC
 	select GENERIC_TIME_VSYSCALL
 	select HAVE_ARCH_AUDITSYSCALL
 	select HAVE_ARCH_JUMP_LABEL
+	select HAVE_ARCH_KASAN			if PPC32
 	select HAVE_ARCH_KGDB
 	select HAVE_ARCH_MMAP_RND_BITS
 	select HAVE_ARCH_MMAP_RND_COMPAT_BITS	if COMPAT
arch/powerpc/include/asm/kasan.h +7 -0
index 8dc1e3819171..74a4ba9fb8a3 100644
@@ -27,5 +27,12 @@
 
 #define KASAN_SHADOW_SIZE	(KASAN_SHADOW_END - KASAN_SHADOW_START)
 
+#ifdef CONFIG_KASAN
+void kasan_early_init(void);
+void kasan_init(void);
+#else
+static inline void kasan_init(void) { }
+#endif
+
 #endif /* __ASSEMBLY */
 #endif
arch/powerpc/kernel/head_32.S +3 -0
index ce6a972f2584..02229c005853 100644
@@ -952,6 +952,9 @@ start_here:
  * Do early platform-specific initialization,
  * and set up the MMU.
  */
+#ifdef CONFIG_KASAN
+	bl	kasan_early_init
+#endif
 	li	r3,0
 	mr	r4,r31
 	bl	machine_init
arch/powerpc/kernel/head_40x.S +3 -0
index a9c934f2319b..efa219d2136e 100644
@@ -848,6 +848,9 @@ start_here:
 /*
  * Decide what sort of machine this is and initialize the MMU.
  */
+#ifdef CONFIG_KASAN
+	bl	kasan_early_init
+#endif
 	li	r3,0
 	mr	r4,r31
 	bl	machine_init
arch/powerpc/kernel/head_44x.S +3 -0
index 37117ab11584..34a5df827b38 100644
@@ -203,6 +203,9 @@ _ENTRY(_start);
 /*
  * Decide what sort of machine this is and initialize the MMU.
  */
+#ifdef CONFIG_KASAN
+	bl	kasan_early_init
+#endif
 	li	r3,0
 	mr	r4,r31
 	bl	machine_init
arch/powerpc/kernel/head_8xx.S +3 -0
index 03c73b4c6435..d25adb6ef235 100644
@@ -853,6 +853,9 @@ start_here:
 /*
  * Decide what sort of machine this is and initialize the MMU.
  */
+#ifdef CONFIG_KASAN
+	bl	kasan_early_init
+#endif
 	li	r3,0
 	mr	r4,r31
 	bl	machine_init
arch/powerpc/kernel/head_fsl_booke.S +3 -0
index 1881127682e9..0fc38eb957b7 100644
@@ -275,6 +275,9 @@ set_ivor:
 /*
  * Decide what sort of machine this is and initialize the MMU.
  */
+#ifdef CONFIG_KASAN
+	bl	kasan_early_init
+#endif
 	mr	r3,r30
 	mr	r4,r31
 	bl	machine_init
arch/powerpc/kernel/setup-common.c +3 -0
index e7534f306c8e..3c6c5a43901e 100644
@@ -67,6 +67,7 @@
 #include <asm/livepatch.h>
 #include <asm/mmu_context.h>
 #include <asm/cpu_has_feature.h>
+#include <asm/kasan.h>
 
 #include "setup.h"
 
@@ -865,6 +866,8 @@ static void smp_setup_pacas(void)
  */
 void __init setup_arch(char **cmdline_p)
 {
+	kasan_init();
+
 	*cmdline_p = boot_command_line;
 
 	/* Set a half-reasonable default so udelay does something sensible */
arch/powerpc/mm/Makefile +1 -0
index 240d73dce6bb..80382a2d169b 100644
@@ -53,6 +53,7 @@ obj-$(CONFIG_PPC_COPRO_BASE)	+= copro_fault.o
 obj-$(CONFIG_SPAPR_TCE_IOMMU)	+= mmu_context_iommu.o
 obj-$(CONFIG_PPC_PTDUMP)	+= ptdump/
 obj-$(CONFIG_PPC_MEM_KEYS)	+= pkeys.o
+obj-$(CONFIG_KASAN)		+= kasan/
 
 # Disable kcov instrumentation on sensitive code
 # This is necessary for booting with kcov enabled on book3e machines
arch/powerpc/mm/kasan/Makefile new file +5 -0
new file mode 100644
index 000000000000..6577897673dd
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0
+
+KASAN_SANITIZE := n
+
+obj-$(CONFIG_PPC32)           += kasan_init_32.o
arch/powerpc/mm/kasan/kasan_init_32.c new file +152 -0

152 lines not on this page · raw message

Signature · 1 line
2.13.3
#15 lore raw

PATCH v9 10/11 powerpc/32s: move hash code patching out of MMU_init_hw()

In reply to Christophe Leroy

For KASAN, hash table handling will be activated early for accessing to KASAN shadow areas.

In order to avoid any modification of the hash functions while they are still used with the early hash table, the code patching is moved out of MMU_init_hw() and put close to the big-bang switch to the final hash table.

---
 arch/powerpc/kernel/head_32.S |  3 +++
 arch/powerpc/mm/mmu_decl.h    |  1 +
 arch/powerpc/mm/ppc_mmu_32.c  | 36 ++++++++++++++++++++++--------------
 3 files changed, 26 insertions(+), 14 deletions(-)
arch/powerpc/kernel/head_32.S +3 -0
index 02229c005853..e644aab2cf5b 100644
@@ -960,6 +960,9 @@ start_here:
 	bl	machine_init
 	bl	__save_cpu_setup
 	bl	MMU_init
+BEGIN_MMU_FTR_SECTION
+	bl	MMU_init_hw_patch
+END_MMU_FTR_SECTION_IFSET(MMU_FTR_HPTE_TABLE)
 
 /*
  * Go back to running unmapped so we can load up new values
arch/powerpc/mm/mmu_decl.h +1 -0
index 74ff61dabcb1..d726ff776054 100644
@@ -130,6 +130,7 @@ extern void wii_memory_fixups(void);
  */
 #ifdef CONFIG_PPC32
 extern void MMU_init_hw(void);
+void MMU_init_hw_patch(void);
 unsigned long mmu_mapin_ram(unsigned long base, unsigned long top);
 #endif
 
arch/powerpc/mm/ppc_mmu_32.c +22 -14
index 2d5b0d50fb31..38c0e28c21e1 100644
@@ -39,6 +39,7 @@
 struct hash_pte *Hash, *Hash_end;
 unsigned long Hash_size, Hash_mask;
 unsigned long _SDR1;
+static unsigned int hash_mb, hash_mb2;
 
 struct ppc_bat BATS[8][2];	/* 8 pairs of IBAT, DBAT */
 
@@ -308,7 +309,6 @@ void hash_preload(struct mm_struct *mm, unsigned long ea,
  */
 void __init MMU_init_hw(void)
 {
-	unsigned int hmask, mb, mb2;
 	unsigned int n_hpteg, lg_n_hpteg;
 
 	if (!mmu_has_feature(MMU_FTR_HPTE_TABLE))
@@ -349,20 +349,30 @@ void __init MMU_init_hw(void)
 	       (unsigned long long)(total_memory >> 20), Hash_size >> 10, Hash);
 
 
-	/*
-	 * Patch up the instructions in hashtable.S:create_hpte
-	 */
-	if ( ppc_md.progress ) ppc_md.progress("hash:patch", 0x345);
 	Hash_mask = n_hpteg - 1;
-	hmask = Hash_mask >> (16 - LG_HPTEG_SIZE);
-	mb2 = mb = 32 - LG_HPTEG_SIZE - lg_n_hpteg;
+	hash_mb2 = hash_mb = 32 - LG_HPTEG_SIZE - lg_n_hpteg;
 	if (lg_n_hpteg > 16)
-		mb2 = 16 - LG_HPTEG_SIZE;
+		hash_mb2 = 16 - LG_HPTEG_SIZE;
+}
+
+void __init MMU_init_hw_patch(void)
+{
+	unsigned int hmask = Hash_mask >> (16 - LG_HPTEG_SIZE);
 
+	if (ppc_md.progress)
+		ppc_md.progress("hash:patch", 0x345);
+	if (ppc_md.progress)
+		ppc_md.progress("hash:done", 0x205);
+
+	/* WARNING: Make sure nothing can trigger a KASAN check past this point */
+
+	/*
+	 * Patch up the instructions in hashtable.S:create_hpte
+	 */
 	modify_instruction_site(&patch__hash_page_A0, 0xffff,
 				((unsigned int)Hash - PAGE_OFFSET) >> 16);
-	modify_instruction_site(&patch__hash_page_A1, 0x7c0, mb << 6);
-	modify_instruction_site(&patch__hash_page_A2, 0x7c0, mb2 << 6);
+	modify_instruction_site(&patch__hash_page_A1, 0x7c0, hash_mb << 6);
+	modify_instruction_site(&patch__hash_page_A2, 0x7c0, hash_mb2 << 6);
 	modify_instruction_site(&patch__hash_page_B, 0xffff, hmask);
 	modify_instruction_site(&patch__hash_page_C, 0xffff, hmask);
 
@@ -371,11 +381,9 @@ void __init MMU_init_hw(void)
 	 */
 	modify_instruction_site(&patch__flush_hash_A0, 0xffff,
 				((unsigned int)Hash - PAGE_OFFSET) >> 16);
-	modify_instruction_site(&patch__flush_hash_A1, 0x7c0, mb << 6);
-	modify_instruction_site(&patch__flush_hash_A2, 0x7c0, mb2 << 6);
+	modify_instruction_site(&patch__flush_hash_A1, 0x7c0, hash_mb << 6);
+	modify_instruction_site(&patch__flush_hash_A2, 0x7c0, hash_mb2 << 6);
 	modify_instruction_site(&patch__flush_hash_B, 0xffff, hmask);
-
-	if ( ppc_md.progress ) ppc_md.progress("hash:done", 0x205);
 }
 
 void setup_initial_memory_limit(phys_addr_t first_memblock_base,
Signature · 1 line
2.13.3
#16 lore raw

PATCH v9 11/11 powerpc/32s: set up an early static hash table for KASAN.

In reply to Christophe Leroy

KASAN requires early activation of hash table, before memblock() functions are available.

This patch implements an early hash_table statically defined in __initdata.

During early boot, a single page table is used.

For hash32, when doing the final init, one page table is allocated for each PGD entry because of the _PAGE_HASHPTE flag which can't be common to several virt pages. This is done after memblock get available but before switching to the final hash table, otherwise there are issues with TLB flushing due to the shared entries.

For hash32, the zero shadow page gets mapped with PAGE_READONLY instead of PAGE_KERNEL_RO, because the PP bits don't provide a RO kernel, so PAGE_KERNEL_RO is equivalent to PAGE_KERNEL. By using PAGE_READONLY, the page is RO for both kernel and user, but this is not a security issue as it contains only zeroes.

---
 arch/powerpc/include/asm/kasan.h      |  1 +
 arch/powerpc/kernel/head_32.S         | 43 ++++++++++++++++++++++-------
 arch/powerpc/mm/kasan/kasan_init_32.c | 51 ++++++++++++++++++++++++++++++-----
 arch/powerpc/mm/mmu_decl.h            |  1 +
 4 files changed, 79 insertions(+), 17 deletions(-)
arch/powerpc/include/asm/kasan.h +1 -0
index 74a4ba9fb8a3..c9fe0369a8fc 100644
@@ -29,6 +29,7 @@
 
 #ifdef CONFIG_KASAN
 void kasan_early_init(void);
+void kasan_mmu_init(void);
 void kasan_init(void);
 #else
 static inline void kasan_init(void) { }
arch/powerpc/kernel/head_32.S +33 -10

83 lines not on this page · raw message

arch/powerpc/mm/kasan/kasan_init_32.c +44 -7

93 lines not on this page · raw message

arch/powerpc/mm/mmu_decl.h +1 -0
index d726ff776054..31fce3914ddc 100644
@@ -106,6 +106,7 @@ extern unsigned int rtas_data, rtas_size;
 struct hash_pte;
 extern struct hash_pte *Hash, *Hash_end;
 extern unsigned long Hash_size, Hash_mask;
+extern u8 early_hash[];
 
 #endif /* CONFIG_PPC32 */
 
Signature · 1 line
2.13.3