Skip to contentSkip to conversation

KASAN core changes for ppc64 radix KASAN

PATCH Superseded Daniel Axtens 3 participants 11 messages Jun 17, 2021 – Jun 23, 2021

Subsystems (7)

v15 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#0 lore raw

v15 cover KASAN core changes for ppc64 radix KASAN

Building on the work of Christophe, Aneesh and Balbir, I've ported KASAN to 64-bit Book3S kernels running on the Radix MMU. I've been trying this for a while, but we keep having collisions between the kasan code in the mm tree and the code I want to put in to the ppc tree.

This series just contains the kasan core changes that we need. These can go in via the mm tree. I will then propose the powerpc changes for a later cycle. (The most recent RFC for the powerpc changes is in the v12 series at
https://lore.kernel.org/linux-mm/[email protected]/
)

v15 applies to next-20210611. There should be no noticeable changes to other platforms.

Changes since v14: Included a bunch of Reviewed-by:s, thanks Christophe and Marco. Cleaned up the build time error #ifdefs, thanks Christophe.

Changes since v13: move the MAX_PTR_PER_* definitions out of kasan and into pgtable.h. Add a build time error to hopefully prevent any confusion about when the new hook is applicable. Thanks Marco and Christophe.

Changes since v12: respond to Marco's review comments - clean up the help for ARCH_DISABLE_KASAN_INLINE, and add an arch readiness check to the new granule poisioning function. Thanks Marco.

Daniel Axtens (4):
kasan: allow an architecture to disable inline instrumentation
kasan: allow architectures to provide an outline readiness check
mm: define default MAX_PTRS_PER_* in include/pgtable.h
kasan: use MAX_PTRS_PER_* for early shadow tables

 arch/s390/include/asm/pgtable.h     |  2 --
 include/asm-generic/pgtable-nop4d.h |  1 -
 include/linux/kasan.h               |  6 +++---
 include/linux/pgtable.h             | 22 ++++++++++++++++++++++
 lib/Kconfig.kasan                   | 14 ++++++++++++++
 mm/kasan/common.c                   |  4 ++++
 mm/kasan/generic.c                  |  3 +++
 mm/kasan/init.c                     |  6 +++---
 mm/kasan/kasan.h                    |  6 ++++++
 mm/kasan/shadow.c                   |  8 ++++++++
 10 files changed, 63 insertions(+), 9 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH v15 1/4 kasan: allow an architecture to disable inline instrumentation

For annoying architectural reasons, it's very difficult to support inline instrumentation on powerpc64.*

Add a Kconfig flag to allow an arch to disable inline. (It's a bit annoying to be 'backwards', but I'm not aware of any way to have an arch force a symbol to be 'n', rather than 'y'.)

We also disable stack instrumentation in this case as it does things that are functionally equivalent to inline instrumentation, namely adding code that touches the shadow directly without going through a C helper.

* on ppc64 atm, the shadow lives in virtual memory and isn't accessible in real mode. However, before we turn on virtual memory, we parse the device tree to determine which platform and MMU we're running under. That calls generic DT code, which is instrumented. Inline instrumentation in DT would unconditionally attempt to touch the shadow region, which we won't have set up yet, and would crash. We can make outline mode wait for the arch to be ready, but we can't change what the compiler inserts for inline mode.

---
 lib/Kconfig.kasan | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
lib/Kconfig.kasan +14 -0
index cffc2ebbf185..cb5e02d09e11 100644
@@ -12,6 +12,15 @@ config HAVE_ARCH_KASAN_HW_TAGS
 config HAVE_ARCH_KASAN_VMALLOC
 	bool
 
+config ARCH_DISABLE_KASAN_INLINE
+	bool
+	help
+	  Sometimes an architecture might not be able to support inline
+	  instrumentation but might be able to support outline instrumentation.
+	  This option allows an architecture to prevent inline and stack
+	  instrumentation from being enabled.
+
+
 config CC_HAS_KASAN_GENERIC
 	def_bool $(cc-option, -fsanitize=kernel-address)
 
@@ -130,6 +139,7 @@ config KASAN_OUTLINE
 
 config KASAN_INLINE
 	bool "Inline instrumentation"
+	depends on !ARCH_DISABLE_KASAN_INLINE
 	help
 	  Compiler directly inserts code checking shadow memory before
 	  memory accesses. This is faster than outline (in some workloads
@@ -141,6 +151,7 @@ endchoice
 config KASAN_STACK
 	bool "Enable stack instrumentation (unsafe)" if CC_IS_CLANG && !COMPILE_TEST
 	depends on KASAN_GENERIC || KASAN_SW_TAGS
+	depends on !ARCH_DISABLE_KASAN_INLINE
 	default y if CC_IS_GCC
 	help
 	  The LLVM stack address sanitizer has a know problem that
@@ -154,6 +165,9 @@ config KASAN_STACK
 	  but clang users can still enable it for builds without
 	  CONFIG_COMPILE_TEST.	On gcc it is assumed to always be safe
 	  to use and enabled by default.
+	  If the architecture disables inline instrumentation, this is
+	  also disabled as it adds inline-style instrumentation that
+	  is run unconditionally.
 
 config KASAN_SW_TAGS_IDENTIFY
 	bool "Enable memory corruption identification"
Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH v15 1/4] kasan: allow an architecture to disable inline instrumentation

Quoted 31 lines · Daniel Axtens

For annoying architectural reasons, it's very difficult to support inline instrumentation on powerpc64.*

Add a Kconfig flag to allow an arch to disable inline. (It's a bit annoying to be 'backwards', but I'm not aware of any way to have an arch force a symbol to be 'n', rather than 'y'.)

We also disable stack instrumentation in this case as it does things that are functionally equivalent to inline instrumentation, namely adding code that touches the shadow directly without going through a C helper.

* on ppc64 atm, the shadow lives in virtual memory and isn't accessible in real mode. However, before we turn on virtual memory, we parse the device tree to determine which platform and MMU we're running under. That calls generic DT code, which is instrumented. Inline instrumentation in DT would unconditionally attempt to touch the shadow region, which we won't have set up yet, and would crash. We can make outline mode wait for the arch to be ready, but we can't change what the compiler inserts for inline mode.

---
 lib/Kconfig.kasan | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
lib/Kconfig.kasan +0 -0
index cffc2ebbf185..cb5e02d09e11 100644

lib/Kconfig.kasan config HAVE_ARCH_KASAN_HW_TAGS · line 21

Quoted 10 lines
lib/Kconfig.kasan +7 -0
@@ -12,6 +12,15 @@ config HAVE_ARCH_KASAN_HW_TAGS
 config HAVE_ARCH_KASAN_VMALLOC
        bool
 
+config ARCH_DISABLE_KASAN_INLINE
+       bool
+       help
+         Sometimes an architecture might not be able to support inline
+         instrumentation but might be able to support outline instrumentation.
+         This option allows an architecture to prevent inline and stack
+         instrumentation from being enabled.

This seems too wordy.

How about: "An architecture might not support inline instrumentation. When this option is selected, inline and stack instrumentation are disabled."

lib/Kconfig.kasan config HAVE_ARCH_KASAN_HW_TAGS · line 23

Quoted 2 lines
lib/Kconfig.kasan +2 -0
+
+

Drop the extra empty line.

Quoted 20 lines
lib/Kconfig.kasan +2 -0
 config CC_HAS_KASAN_GENERIC
        def_bool $(cc-option, -fsanitize=kernel-address)
 
@@ -130,6 +139,7 @@ config KASAN_OUTLINE
 
 config KASAN_INLINE
        bool "Inline instrumentation"
+       depends on !ARCH_DISABLE_KASAN_INLINE
        help
          Compiler directly inserts code checking shadow memory before
          memory accesses. This is faster than outline (in some workloads
@@ -141,6 +151,7 @@ endchoice
 config KASAN_STACK
        bool "Enable stack instrumentation (unsafe)" if CC_IS_CLANG && !COMPILE_TEST
        depends on KASAN_GENERIC || KASAN_SW_TAGS
+       depends on !ARCH_DISABLE_KASAN_INLINE
        default y if CC_IS_GCC
        help
          The LLVM stack address sanitizer has a know problem that

lib/Kconfig.kasan config KASAN_STACK · line 168

Quoted 4 lines
lib/Kconfig.kasan +1 -0
@@ -154,6 +165,9 @@ config KASAN_STACK
          but clang users can still enable it for builds without
          CONFIG_COMPILE_TEST.  On gcc it is assumed to always be safe
          to use and enabled by default.
+         If the architecture disables inline instrumentation, this is

this => stack instrumentation

Quoted 8 lines
lib/Kconfig.kasan +2 -1
+         also disabled as it adds inline-style instrumentation that
+         is run unconditionally.
 
 config KASAN_SW_TAGS_IDENTIFY
        bool "Enable memory corruption identification"
--

2.30.2

#3 lore raw

PATCH v15 2/4 kasan: allow architectures to provide an outline readiness check

In reply to Daniel Axtens

Allow architectures to define a kasan_arch_is_ready() hook that bails out of any function that's about to touch the shadow unless the arch says that it is ready for the memory to be accessed. This is fairly uninvasive and should have a negligible performance penalty.

This will only work in outline mode, so an arch must specify ARCH_DISABLE_KASAN_INLINE if it requires this.

--

Both previous RFCs for ppc64 - by 2 different people - have needed this trick! See:
- https://lore.kernel.org/patchwork/patch/592820/ # ppc64 hash series
- https://patchwork.ozlabs.org/patch/795211/ # ppc radix series

Build tested on arm64 with SW_TAGS and x86 with INLINE: the error fires if I add a kasan_arch_is_ready define.

---
 mm/kasan/common.c  | 4 ++++
 mm/kasan/generic.c | 3 +++
 mm/kasan/kasan.h   | 6 ++++++
 mm/kasan/shadow.c  | 8 ++++++++
 4 files changed, 21 insertions(+)
mm/kasan/common.c +4 -0
index 10177cc26d06..0ad615f3801d 100644
@@ -331,6 +331,10 @@ static inline bool ____kasan_slab_free(struct kmem_cache *cache, void *object,
 	u8 tag;
 	void *tagged_object;
 
+	/* Bail if the arch isn't ready */
+	if (!kasan_arch_is_ready())
+		return false;
+
 	tag = get_tag(object);
 	tagged_object = object;
 	object = kasan_reset_tag(object);
mm/kasan/generic.c +3 -0
index 53cbf28859b5..c3f5ba7a294a 100644
@@ -163,6 +163,9 @@ static __always_inline bool check_region_inline(unsigned long addr,
 						size_t size, bool write,
 						unsigned long ret_ip)
 {
+	if (!kasan_arch_is_ready())
+		return true;
+
 	if (unlikely(size == 0))
 		return true;
 
mm/kasan/kasan.h +6 -0
index 8f450bc28045..4dbc8def64f4 100644
@@ -449,6 +449,12 @@ static inline void kasan_poison_last_granule(const void *address, size_t size) {
 
 #endif /* CONFIG_KASAN_GENERIC */
 
+#ifndef kasan_arch_is_ready
+static inline bool kasan_arch_is_ready(void)	{ return true; }
+#elif !defined(CONFIG_KASAN_GENERIC) || !defined(CONFIG_KASAN_OUTLINE)
+#error kasan_arch_is_ready only works in KASAN generic outline mode!
+#endif
+
 /*
  * Exported functions for interfaces called from assembly or from generated
  * code. Declarations here to avoid warning about missing declarations.
mm/kasan/shadow.c +8 -0
index 082ee5b6d9a1..3c7f7efe6f68 100644
@@ -73,6 +73,10 @@ void kasan_poison(const void *addr, size_t size, u8 value, bool init)
 {
 	void *shadow_start, *shadow_end;
 
+	/* Don't touch the shadow memory if arch isn't ready */
+	if (!kasan_arch_is_ready())
+		return;
+
 	/*
 	 * Perform shadow offset calculation based on untagged address, as
 	 * some of the callers (e.g. kasan_poison_object_data) pass tagged
@@ -99,6 +103,10 @@ EXPORT_SYMBOL(kasan_poison);
 #ifdef CONFIG_KASAN_GENERIC
 void kasan_poison_last_granule(const void *addr, size_t size)
 {
+	/* Don't touch the shadow memory if arch isn't ready */
+	if (!kasan_arch_is_ready())
+		return;
+
 	if (size & KASAN_GRANULE_MASK) {
 		u8 *shadow = (u8 *)kasan_mem_to_shadow(addr + size);
 		*shadow = size & KASAN_GRANULE_MASK;
Signature · 1 line
2.30.2
#4 lore raw

Re: [PATCH v15 2/4] kasan: allow architectures to provide an outline readiness check

Quoted 36 lines · Daniel Axtens

Allow architectures to define a kasan_arch_is_ready() hook that bails out of any function that's about to touch the shadow unless the arch says that it is ready for the memory to be accessed. This is fairly uninvasive and should have a negligible performance penalty.

This will only work in outline mode, so an arch must specify ARCH_DISABLE_KASAN_INLINE if it requires this.

--

Both previous RFCs for ppc64 - by 2 different people - have needed this trick! See:
- https://lore.kernel.org/patchwork/patch/592820/ # ppc64 hash series
- https://patchwork.ozlabs.org/patch/795211/ # ppc radix series

Build tested on arm64 with SW_TAGS and x86 with INLINE: the error fires if I add a kasan_arch_is_ready define.

---
 mm/kasan/common.c  | 4 ++++
 mm/kasan/generic.c | 3 +++
 mm/kasan/kasan.h   | 6 ++++++
 mm/kasan/shadow.c  | 8 ++++++++
 4 files changed, 21 insertions(+)
mm/kasan/common.c +0 -0
index 10177cc26d06..0ad615f3801d 100644

mm/kasan/common.c static inline bool ____kasan_slab_free(struct kmem_cache *cache, void *object, · line 334

Quoted 4 lines
mm/kasan/common.c +1 -0
@@ -331,6 +331,10 @@ static inline bool ____kasan_slab_free(struct kmem_cache *cache, void *object,
        u8 tag;
        void *tagged_object;
 
+       /* Bail if the arch isn't ready */

This comment brings no value. The fact that we bail is clear from the following line. The comment should explain why we bail.

mm/kasan/common.c static inline bool ____kasan_slab_free(struct kmem_cache *cache, void *object, · line 336

Quoted 2 lines
mm/kasan/common.c +2 -0
+       if (!kasan_arch_is_ready())
+               return false;

Have you considered including these checks into the high-level wrappers in include/linux/kasan.h? Would that work?

Quoted 64 lines
mm/kasan/common.c +1 -0
+
        tag = get_tag(object);
        tagged_object = object;
        object = kasan_reset_tag(object);
mm/kasan/generic.c +3 -0
index 53cbf28859b5..c3f5ba7a294a 100644
@@ -163,6 +163,9 @@ static __always_inline bool check_region_inline(unsigned long addr,
                                                size_t size, bool write,
                                                unsigned long ret_ip)
 {
+       if (!kasan_arch_is_ready())
+               return true;
+
        if (unlikely(size == 0))
                return true;
 
mm/kasan/kasan.h +6 -0
index 8f450bc28045..4dbc8def64f4 100644
@@ -449,6 +449,12 @@ static inline void kasan_poison_last_granule(const void *address, size_t size) {
 
 #endif /* CONFIG_KASAN_GENERIC */
 
+#ifndef kasan_arch_is_ready
+static inline bool kasan_arch_is_ready(void)   { return true; }
+#elif !defined(CONFIG_KASAN_GENERIC) || !defined(CONFIG_KASAN_OUTLINE)
+#error kasan_arch_is_ready only works in KASAN generic outline mode!
+#endif
+
 /*
  * Exported functions for interfaces called from assembly or from generated
  * code. Declarations here to avoid warning about missing declarations.
mm/kasan/shadow.c +8 -1
index 082ee5b6d9a1..3c7f7efe6f68 100644
@@ -73,6 +73,10 @@ void kasan_poison(const void *addr, size_t size, u8 value, bool init)
 {
        void *shadow_start, *shadow_end;
 
+       /* Don't touch the shadow memory if arch isn't ready */
+       if (!kasan_arch_is_ready())
+               return;
+
        /*
         * Perform shadow offset calculation based on untagged address, as
         * some of the callers (e.g. kasan_poison_object_data) pass tagged
@@ -99,6 +103,10 @@ EXPORT_SYMBOL(kasan_poison);
 #ifdef CONFIG_KASAN_GENERIC
 void kasan_poison_last_granule(const void *addr, size_t size)
 {
+       /* Don't touch the shadow memory if arch isn't ready */
+       if (!kasan_arch_is_ready())
+               return;
+
        if (size & KASAN_GRANULE_MASK) {
                u8 *shadow = (u8 *)kasan_mem_to_shadow(addr + size);
                *shadow = size & KASAN_GRANULE_MASK;
--

2.30.2

#5 lore raw

Re: [PATCH v15 2/4] kasan: allow architectures to provide an outline readiness check

Quoted 8 lines
Quoted 8 lines
mm/kasan/common.c +0 -0
index 10177cc26d06..0ad615f3801d 100644
@@ -331,6 +331,10 @@ static inline bool ____kasan_slab_free(struct kmem_cache *cache, void *object,
        u8 tag;
        void *tagged_object;
 
Quoted 7 lines

mm/kasan/common.c static inline bool ____kasan_slab_free(struct kmem_cache *cache, void *object, · line 334

Quoted 1 line
mm/kasan/common.c +1 -0
+       /* Bail if the arch isn't ready */

This comment brings no value. The fact that we bail is clear from the following line. The comment should explain why we bail.

Quoted 2 lines
mm/kasan/common.c +2 -0
+       if (!kasan_arch_is_ready())
+               return false;

Fair enough, I've just dropped the comments as I don't think there's really a lot of scope for the generic/core comment to explain why a particular architecture might not be ready.

Quoted 2 lines

Have you considered including these checks into the high-level wrappers in include/linux/kasan.h? Would that work?

I don't think those wrappers will catch the outline check functions like __asan_load*, which also need guarding.

Kind regards,
Daniel

#6 lore raw

PATCH v15 3/4 mm: define default MAX_PTRS_PER_* in include/pgtable.h

In reply to Daniel Axtens

Commit c65e774fb3f6 ("x86/mm: Make PGDIR_SHIFT and PTRS_PER_P4D variable") made PTRS_PER_P4D variable on x86 and introduced MAX_PTRS_PER_P4D as a constant for cases which need a compile-time constant (e.g. fixed-size arrays).

powerpc likewise has boot-time selectable MMU features which can cause other mm "constants" to vary. For KASAN, we have some static PTE/PMD/PUD/P4D arrays so we need compile-time maximums for all these constants. Extend the MAX_PTRS_PER_ idiom, and place default definitions in include/pgtable.h. These define MAX_PTRS_PER_x to be PTRS_PER_x unless an architecture has defined MAX_PTRS_PER_x in its arch headers.

Clean up pgtable-nop4d.h and s390's MAX_PTRS_PER_P4D definitions while we're at it: both can just pick up the default now.

---

s390 was compile tested only.

---
 arch/s390/include/asm/pgtable.h     |  2 --
 include/asm-generic/pgtable-nop4d.h |  1 -
 include/linux/pgtable.h             | 22 ++++++++++++++++++++++
 3 files changed, 22 insertions(+), 3 deletions(-)
arch/s390/include/asm/pgtable.h +0 -2
index 7c66ae5d7e32..cf05954ce013 100644
@@ -342,8 +342,6 @@ static inline int is_module_addr(void *addr)
 #define PTRS_PER_P4D	_CRST_ENTRIES
 #define PTRS_PER_PGD	_CRST_ENTRIES
 
-#define MAX_PTRS_PER_P4D	PTRS_PER_P4D
-
 /*
  * Segment table and region3 table entry encoding
  * (R = read-only, I = invalid, y = young bit):
include/asm-generic/pgtable-nop4d.h +0 -1
index ce2cbb3c380f..2f6b1befb129 100644
@@ -9,7 +9,6 @@
 typedef struct { pgd_t pgd; } p4d_t;
 
 #define P4D_SHIFT		PGDIR_SHIFT
-#define MAX_PTRS_PER_P4D	1
 #define PTRS_PER_P4D		1
 #define P4D_SIZE		(1UL << P4D_SHIFT)
 #define P4D_MASK		(~(P4D_SIZE-1))
include/linux/pgtable.h +22 -0
index 9e6f71265f72..69700e3e615f 100644
@@ -1625,4 +1625,26 @@ typedef unsigned int pgtbl_mod_mask;
 #define pte_leaf_size(x) PAGE_SIZE
 #endif
 
+/*
+ * Some architectures have MMUs that are configurable or selectable at boot
+ * time. These lead to variable PTRS_PER_x. For statically allocated arrays it
+ * helps to have a static maximum value.
+ */
+
+#ifndef MAX_PTRS_PER_PTE
+#define MAX_PTRS_PER_PTE PTRS_PER_PTE
+#endif
+
+#ifndef MAX_PTRS_PER_PMD
+#define MAX_PTRS_PER_PMD PTRS_PER_PMD
+#endif
+
+#ifndef MAX_PTRS_PER_PUD
+#define MAX_PTRS_PER_PUD PTRS_PER_PUD
+#endif
+
+#ifndef MAX_PTRS_PER_P4D
+#define MAX_PTRS_PER_P4D PTRS_PER_P4D
+#endif
+
 #endif /* _LINUX_PGTABLE_H */
Signature · 1 line
2.30.2
reviewer Acked-by on v15 3/4
#7 lore raw

Re: [PATCH v15 3/4] mm: define default MAX_PTRS_PER_* in include/pgtable.h

Quoted 86 lines · Daniel Axtens

Commit c65e774fb3f6 ("x86/mm: Make PGDIR_SHIFT and PTRS_PER_P4D variable") made PTRS_PER_P4D variable on x86 and introduced MAX_PTRS_PER_P4D as a constant for cases which need a compile-time constant (e.g. fixed-size arrays).

powerpc likewise has boot-time selectable MMU features which can cause other mm "constants" to vary. For KASAN, we have some static PTE/PMD/PUD/P4D arrays so we need compile-time maximums for all these constants. Extend the MAX_PTRS_PER_ idiom, and place default definitions in include/pgtable.h. These define MAX_PTRS_PER_x to be PTRS_PER_x unless an architecture has defined MAX_PTRS_PER_x in its arch headers.

Clean up pgtable-nop4d.h and s390's MAX_PTRS_PER_P4D definitions while we're at it: both can just pick up the default now.

---

s390 was compile tested only.

---
 arch/s390/include/asm/pgtable.h     |  2 --
 include/asm-generic/pgtable-nop4d.h |  1 -
 include/linux/pgtable.h             | 22 ++++++++++++++++++++++
 3 files changed, 22 insertions(+), 3 deletions(-)
arch/s390/include/asm/pgtable.h +0 -2
index 7c66ae5d7e32..cf05954ce013 100644
@@ -342,8 +342,6 @@ static inline int is_module_addr(void *addr)
 #define PTRS_PER_P4D   _CRST_ENTRIES
 #define PTRS_PER_PGD   _CRST_ENTRIES
 
-#define MAX_PTRS_PER_P4D       PTRS_PER_P4D
-
 /*
  * Segment table and region3 table entry encoding
  * (R = read-only, I = invalid, y = young bit):
include/asm-generic/pgtable-nop4d.h +0 -1
index ce2cbb3c380f..2f6b1befb129 100644
@@ -9,7 +9,6 @@
 typedef struct { pgd_t pgd; } p4d_t;
 
 #define P4D_SHIFT              PGDIR_SHIFT
-#define MAX_PTRS_PER_P4D       1
 #define PTRS_PER_P4D           1
 #define P4D_SIZE               (1UL << P4D_SHIFT)
 #define P4D_MASK               (~(P4D_SIZE-1))
include/linux/pgtable.h +22 -1
index 9e6f71265f72..69700e3e615f 100644
@@ -1625,4 +1625,26 @@ typedef unsigned int pgtbl_mod_mask;
 #define pte_leaf_size(x) PAGE_SIZE
 #endif
 
+/*
+ * Some architectures have MMUs that are configurable or selectable at boot
+ * time. These lead to variable PTRS_PER_x. For statically allocated arrays it
+ * helps to have a static maximum value.
+ */
+
+#ifndef MAX_PTRS_PER_PTE
+#define MAX_PTRS_PER_PTE PTRS_PER_PTE
+#endif
+
+#ifndef MAX_PTRS_PER_PMD
+#define MAX_PTRS_PER_PMD PTRS_PER_PMD
+#endif
+
+#ifndef MAX_PTRS_PER_PUD
+#define MAX_PTRS_PER_PUD PTRS_PER_PUD
+#endif
+
+#ifndef MAX_PTRS_PER_P4D
+#define MAX_PTRS_PER_P4D PTRS_PER_P4D
+#endif
+
 #endif /* _LINUX_PGTABLE_H */
--
Quoted 2 lines

2.30.2

#8 lore raw

PATCH v15 4/4 kasan: use MAX_PTRS_PER_* for early shadow tables

In reply to Daniel Axtens

powerpc has a variable number of PTRS_PER_*, set at runtime based on the MMU that the kernel is booted under.

This means the PTRS_PER_* are no longer constants, and therefore breaks the build. Switch to using MAX_PTRS_PER_*, which are constant.

---
 include/linux/kasan.h | 6 +++---
 mm/kasan/init.c       | 6 +++---
 2 files changed, 6 insertions(+), 6 deletions(-)
include/linux/kasan.h +3 -3
index 768d7d342757..5310e217bd74 100644
@@ -41,9 +41,9 @@ struct kunit_kasan_expectation {
 #endif
 
 extern unsigned char kasan_early_shadow_page[PAGE_SIZE];
-extern pte_t kasan_early_shadow_pte[PTRS_PER_PTE + PTE_HWTABLE_PTRS];
-extern pmd_t kasan_early_shadow_pmd[PTRS_PER_PMD];
-extern pud_t kasan_early_shadow_pud[PTRS_PER_PUD];
+extern pte_t kasan_early_shadow_pte[MAX_PTRS_PER_PTE + PTE_HWTABLE_PTRS];
+extern pmd_t kasan_early_shadow_pmd[MAX_PTRS_PER_PMD];
+extern pud_t kasan_early_shadow_pud[MAX_PTRS_PER_PUD];
 extern p4d_t kasan_early_shadow_p4d[MAX_PTRS_PER_P4D];
 
 int kasan_populate_early_shadow(const void *shadow_start,
mm/kasan/init.c +3 -3
index 348f31d15a97..cc64ed6858c6 100644
@@ -41,7 +41,7 @@ static inline bool kasan_p4d_table(pgd_t pgd)
 }
 #endif
 #if CONFIG_PGTABLE_LEVELS > 3
-pud_t kasan_early_shadow_pud[PTRS_PER_PUD] __page_aligned_bss;
+pud_t kasan_early_shadow_pud[MAX_PTRS_PER_PUD] __page_aligned_bss;
 static inline bool kasan_pud_table(p4d_t p4d)
 {
 	return p4d_page(p4d) == virt_to_page(lm_alias(kasan_early_shadow_pud));
@@ -53,7 +53,7 @@ static inline bool kasan_pud_table(p4d_t p4d)
 }
 #endif
 #if CONFIG_PGTABLE_LEVELS > 2
-pmd_t kasan_early_shadow_pmd[PTRS_PER_PMD] __page_aligned_bss;
+pmd_t kasan_early_shadow_pmd[MAX_PTRS_PER_PMD] __page_aligned_bss;
 static inline bool kasan_pmd_table(pud_t pud)
 {
 	return pud_page(pud) == virt_to_page(lm_alias(kasan_early_shadow_pmd));
@@ -64,7 +64,7 @@ static inline bool kasan_pmd_table(pud_t pud)
 	return false;
 }
 #endif
-pte_t kasan_early_shadow_pte[PTRS_PER_PTE + PTE_HWTABLE_PTRS]
+pte_t kasan_early_shadow_pte[MAX_PTRS_PER_PTE + PTE_HWTABLE_PTRS]
 	__page_aligned_bss;
 
 static inline bool kasan_pte_table(pmd_t pmd)
Signature · 1 line
2.30.2
reviewer Reviewed-by on v15 4/4
#9 lore raw

Re: [PATCH v15 4/4] kasan: use MAX_PTRS_PER_* for early shadow tables

Quoted 67 lines · Daniel Axtens

powerpc has a variable number of PTRS_PER_*, set at runtime based on the MMU that the kernel is booted under.

This means the PTRS_PER_* are no longer constants, and therefore breaks the build. Switch to using MAX_PTRS_PER_*, which are constant.

---
 include/linux/kasan.h | 6 +++---
 mm/kasan/init.c       | 6 +++---
 2 files changed, 6 insertions(+), 6 deletions(-)
include/linux/kasan.h +3 -3
index 768d7d342757..5310e217bd74 100644
@@ -41,9 +41,9 @@ struct kunit_kasan_expectation {
 #endif
 
 extern unsigned char kasan_early_shadow_page[PAGE_SIZE];
-extern pte_t kasan_early_shadow_pte[PTRS_PER_PTE + PTE_HWTABLE_PTRS];
-extern pmd_t kasan_early_shadow_pmd[PTRS_PER_PMD];
-extern pud_t kasan_early_shadow_pud[PTRS_PER_PUD];
+extern pte_t kasan_early_shadow_pte[MAX_PTRS_PER_PTE + PTE_HWTABLE_PTRS];
+extern pmd_t kasan_early_shadow_pmd[MAX_PTRS_PER_PMD];
+extern pud_t kasan_early_shadow_pud[MAX_PTRS_PER_PUD];
 extern p4d_t kasan_early_shadow_p4d[MAX_PTRS_PER_P4D];
 
 int kasan_populate_early_shadow(const void *shadow_start,
mm/kasan/init.c +3 -4
index 348f31d15a97..cc64ed6858c6 100644
@@ -41,7 +41,7 @@ static inline bool kasan_p4d_table(pgd_t pgd)
 }
 #endif
 #if CONFIG_PGTABLE_LEVELS > 3
-pud_t kasan_early_shadow_pud[PTRS_PER_PUD] __page_aligned_bss;
+pud_t kasan_early_shadow_pud[MAX_PTRS_PER_PUD] __page_aligned_bss;
 static inline bool kasan_pud_table(p4d_t p4d)
 {
        return p4d_page(p4d) == virt_to_page(lm_alias(kasan_early_shadow_pud));
@@ -53,7 +53,7 @@ static inline bool kasan_pud_table(p4d_t p4d)
 }
 #endif
 #if CONFIG_PGTABLE_LEVELS > 2
-pmd_t kasan_early_shadow_pmd[PTRS_PER_PMD] __page_aligned_bss;
+pmd_t kasan_early_shadow_pmd[MAX_PTRS_PER_PMD] __page_aligned_bss;
 static inline bool kasan_pmd_table(pud_t pud)
 {
        return pud_page(pud) == virt_to_page(lm_alias(kasan_early_shadow_pmd));
@@ -64,7 +64,7 @@ static inline bool kasan_pmd_table(pud_t pud)
        return false;
 }
 #endif
-pte_t kasan_early_shadow_pte[PTRS_PER_PTE + PTE_HWTABLE_PTRS]
+pte_t kasan_early_shadow_pte[MAX_PTRS_PER_PTE + PTE_HWTABLE_PTRS]
        __page_aligned_bss;
 
 static inline bool kasan_pte_table(pmd_t pmd)
--
Quoted 2 lines

2.30.2

Reviewed-by on the v15 series
#10 lore raw
In reply to Daniel Axtens
Quoted 29 lines · Daniel Axtens

Building on the work of Christophe, Aneesh and Balbir, I've ported KASAN to 64-bit Book3S kernels running on the Radix MMU. I've been trying this for a while, but we keep having collisions between the kasan code in the mm tree and the code I want to put in to the ppc tree.

This series just contains the kasan core changes that we need. These can go in via the mm tree. I will then propose the powerpc changes for a later cycle. (The most recent RFC for the powerpc changes is in the v12 series at
https://lore.kernel.org/linux-mm/[email protected]/
)

v15 applies to next-20210611. There should be no noticeable changes to other platforms.

Changes since v14: Included a bunch of Reviewed-by:s, thanks Christophe and Marco. Cleaned up the build time error #ifdefs, thanks Christophe.

Changes since v13: move the MAX_PTR_PER_* definitions out of kasan and into pgtable.h. Add a build time error to hopefully prevent any confusion about when the new hook is applicable. Thanks Marco and Christophe.

Changes since v12: respond to Marco's review comments - clean up the help for ARCH_DISABLE_KASAN_INLINE, and add an arch readiness check to the new granule poisioning function. Thanks Marco.

Quoted 6 lines

Daniel Axtens (4):
kasan: allow an architecture to disable inline instrumentation
kasan: allow architectures to provide an outline readiness check
mm: define default MAX_PTRS_PER_* in include/pgtable.h
kasan: use MAX_PTRS_PER_* for early shadow tables

The series seems reasonable