mm: make minimum slab alignment a runtime property
Subsystems (12)
PATCH v2 1/1 mm: make minimum slab alignment a runtime property
When CONFIG_KASAN_HW_TAGS is enabled we currently increase the minimum slab alignment to 16. This happens even if MTE is not supported in hardware or disabled via kasan=off, which creates an unnecessary memory overhead in those cases. Eliminate this overhead by making the minimum slab alignment a runtime property and only aligning to 16 if KASAN is enabled at runtime.
On a DragonBoard 845c (non-MTE hardware) with a kernel built with CONFIG_KASAN_HW_TAGS, waiting for quiescence after a full Android boot I see the following Slab measurements in /proc/meminfo (median of 3 reboots):
Before: 169020 kB After: 167304 kB
---
v2:
- use max instead of max_t in flat_stack_align()
arch/arc/include/asm/cache.h | 4 ++-- arch/arm/include/asm/cache.h | 2 +- arch/arm64/include/asm/cache.h | 19 +++++++++++++------ arch/microblaze/include/asm/page.h | 2 +- arch/riscv/include/asm/cache.h | 2 +- arch/sparc/include/asm/cache.h | 2 +- arch/xtensa/include/asm/processor.h | 2 +- fs/binfmt_flat.c | 9 ++++++--- include/crypto/hash.h | 2 +- include/linux/slab.h | 22 +++++++++++++++++----- mm/slab.c | 7 +++---- mm/slab_common.c | 3 +-- mm/slob.c | 6 +++--- 13 files changed, 51 insertions(+), 31 deletions(-)
arch/arc/include/asm/cache.h +2 -2
@@ -55,11 +55,11 @@* Make sure slab-allocated buffers are 64-bit aligned when atomic64_t uses * ARCv2 64-bit atomics (LLOCKD/SCONDD). This guarantess runtime 64-bit * alignment for any atomic64_t embedded in buffer. -* Default ARCH_SLAB_MINALIGN is __alignof__(long long) which has a relaxed +* Default ARCH_SLAB_MIN_MINALIGN is __alignof__(long long) which has a relaxed * value of 4 (and not 8) in ARC ABI. */ #if defined(CONFIG_ARC_HAS_LL64 ) && defined(CONFIG_ARC_HAS_LLSC ) -#define ARCH_SLAB_MINALIGN 8 +#define ARCH_SLAB_MIN_MINALIGN 8#endif extern int ioc_enable;
arch/arm/include/asm/cache.h +1 -1
@@ -21,7 +21,7 @@* With EABI on ARMv5 and above we must have 64-bit aligned slab pointers. */ #if defined(CONFIG_AEABI ) && (__LINUX_ARM_ARCH__ >=5 ) -#define ARCH_SLAB_MINALIGN 8 +#define ARCH_SLAB_MIN_MINALIGN 8#endif #define __read_mostly __section(".data..read_mostly")
arch/arm64/include/asm/cache.h +13 -6
@@ -6,6 +6,7 @@#define __ASM_CACHE_H#include <asm/cputype.h> +#include <asm/mte-def.h> #define CTR_L1IP_SHIFT 14#define CTR_L1IP_MASK 3
@@ -49,15 +50,21 @@*/ #define ARCH_DMA_MINALIGN (128) -#ifdef CONFIG_KASAN_SW_TAGS -#define ARCH_SLAB_MINALIGN (1ULL << KASAN_SHADOW_SCALE_SHIFT) -#elif defined(CONFIG_KASAN_HW_TAGS ) -#define ARCH_SLAB_MINALIGN MTE_GRANULE_SIZE -#endif -#ifndef __ASSEMBLY__#include <linux/bitops.h> +#include <linux/kasan-enabled.h> + +#ifdef CONFIG_KASAN_SW_TAGS +#define ARCH_SLAB_MIN_MINALIGN (1ULL << KASAN_SHADOW_SCALE_SHIFT) +#elif defined(CONFIG_KASAN_HW_TAGS ) +static inline size_t arch_slab_minalign (void ) +{ +return kasan_hw_tags_enabled () ?MTE_GRANULE_SIZE : + __alignof__(unsigned long long ); +} +#define arch_slab_minalign () arch_slab_minalign() +#endif #define ICACHEF_ALIASING 0#define ICACHEF_VPIPT 1
arch/microblaze/include/asm/page.h +1 -1
@@ -33,7 +33,7 @@/* MS be sure that SLAB allocates aligned objects */ #define ARCH_DMA_MINALIGN L1_CACHE_BYTES -#define ARCH_SLAB_MINALIGN L1_CACHE_BYTES +#define ARCH_SLAB_MIN_MINALIGN L1_CACHE_BYTES/* * PAGE_OFFSET -- the first address of the first page of memory. With MMU
arch/riscv/include/asm/cache.h +1 -1
@@ -16,7 +16,7 @@* the flat loader aligns it accordingly. */ #ifndef CONFIG_MMU -#define ARCH_SLAB_MINALIGN 16 +#define ARCH_SLAB_MIN_MINALIGN 16#endif #endif /* _ASM_RISCV_CACHE_H */
arch/sparc/include/asm/cache.h +1 -1
@@ -8,7 +8,7 @@#ifndef _SPARC_CACHE_H#define _SPARC_CACHE_H -#define ARCH_SLAB_MINALIGN __alignof__(unsigned long long) +#define ARCH_SLAB_MIN_MINALIGN __alignof__(unsigned long long)#define L1_CACHE_SHIFT 5#define L1_CACHE_BYTES 32
arch/xtensa/include/asm/processor.h +1 -1
@@ -18,7 +18,7 @@#include <asm/types.h> #include <asm/regs.h> -#define ARCH_SLAB_MINALIGN XTENSA_STACK_ALIGNMENT +#define ARCH_SLAB_MIN_MINALIGN XTENSA_STACK_ALIGNMENT/* * User space process size: 1 GB.
fs/binfmt_flat.c +6 -3
@@ -64,7 +64,10 @@* Here we can be a bit looser than the data sections since this * needs to only meet arch ABI requirements. */ -#define FLAT_STACK_ALIGN max_t(unsigned long, sizeof(void *), ARCH_SLAB_MINALIGN) +static size_t flat_stack_align (void ) +{ +return max (sizeof (void *),arch_slab_minalign ()); +}#define RELOC_FAILED 0xff00ff01/* Relocation incorrect somewhere */ #define UNLOADED_LIB 0x7ff000ff/* Placeholder for unused library */
@@ -148,7 +151,7 @@ static int create_flat_tables(struct linux_binprm *bprm, unsigned long arg_start sp -=2 ;/* argvp + envp */ sp -=1 ;/* &argc */ - current->mm->start_stack = (unsigned long )sp & -FLAT_STACK_ALIGN ; + current->mm->start_stack = (unsigned long )sp & -flat_stack_align (); sp = (unsigned long __user *)current->mm->start_stack;if (put_user (bprm->argc, sp++))
@@ -966,7 +969,7 @@ static int load_flat_binary(struct linux_binprm *bprm)#endif stack_len += (bprm->argc +1 ) *sizeof (char *);/* the argv array */ stack_len += (bprm->envc +1 ) *sizeof (char *);/* the envp array */ - stack_len =ALIGN (stack_len,FLAT_STACK_ALIGN ); + stack_len =ALIGN (stack_len,flat_stack_align ()); res =load_flat_file (bprm, &libinfo,0 , &stack_len);if (res <0 )
include/crypto/hash.h +1 -1
@@ -149,7 +149,7 @@ struct ahash_alg {struct shash_desc {struct crypto_shash *tfm; -void *__ctx[]__aligned(ARCH_SLAB_MINALIGN) ; +void *__ctx[]__aligned(ARCH_SLAB_MIN_MINALIGN) ; };#define HASH_MAX_DIGESTSIZE 64
include/linux/slab.h +17 -5
@@ -201,21 +201,33 @@ void kmem_dump_obj(void *object);#endif /* -* Setting ARCH_SLAB_MINALIGN in arch headers allows a different alignment. +* Setting ARCH_SLAB_MIN_MINALIGN in arch headers allows a different alignment. * Intended for arches that get misalignment faults even for 64 bit integer * aligned buffers. */ -#ifndef ARCH_SLAB_MINALIGN -#define ARCH_SLAB_MINALIGN __alignof__(unsigned long long) +#ifndef ARCH_SLAB_MIN_MINALIGN +#define ARCH_SLAB_MIN_MINALIGN __alignof__(unsigned long long) +#endif + +/* +* Arches can define this function if they want to decide the minimum slab +* alignment at runtime. The value returned by the function must be +* >= ARCH_SLAB_MIN_MINALIGN. +*/ +#ifndef arch_slab_minalign +static inline size_t arch_slab_minalign (void ) +{ +return ARCH_SLAB_MIN_MINALIGN ; +}#endif /* * kmalloc and friends return ARCH_KMALLOC_MINALIGN aligned -* pointers. kmem_cache_alloc and friends return ARCH_SLAB_MINALIGN +* pointers. kmem_cache_alloc and friends return ARCH_SLAB_MIN_MINALIGN * aligned pointers. */ #define __assume_kmalloc_alignment __assume_aligned(ARCH_KMALLOC_MINALIGN) -#define __assume_slab_alignment __assume_aligned(ARCH_SLAB_MINALIGN) +#define __assume_slab_alignment __assume_aligned(ARCH_SLAB_MIN_MINALIGN)#define __assume_page_alignment __assume_aligned(PAGE_SIZE)/*
mm/slab.c +3 -4
@@ -3009,10 +3009,9 @@ static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep, objp +=obj_offset (cachep);if (cachep->ctor && cachep->flags &SLAB_POISON ) cachep->ctor (objp); -if (ARCH_SLAB_MINALIGN && - ((unsigned long )objp & (ARCH_SLAB_MINALIGN -1 ))) { -pr_err ("0x%px: not aligned to ARCH_SLAB_MINALIGN=%d\n" , - objp, (int )ARCH_SLAB_MINALIGN ); +if ((unsigned long )objp & (arch_slab_minalign () -1 )) { +pr_err ("0x%px: not aligned to arch_slab_minalign()=%d\n" , objp, + (int )arch_slab_minalign ()); }return objp; }
mm/slab_common.c +1 -2
@@ -154,8 +154,7 @@ static unsigned int calculate_alignment(slab_flags_t flags, align =max (align, ralign); } -if (align <ARCH_SLAB_MINALIGN ) - align =ARCH_SLAB_MINALIGN ; + align =max_t (size_t, align,arch_slab_minalign ());return ALIGN (align,sizeof (void *)); }
mm/slob.c +3 -3
@@ -478,7 +478,7 @@ static __always_inline void * __do_kmalloc_node(size_t size, gfp_t gfp, int node, unsigned long caller) { unsigned int *m; -int minalign =max_t (size_t,ARCH_KMALLOC_MINALIGN ,ARCH_SLAB_MINALIGN ); +int minalign =max_t (size_t,ARCH_KMALLOC_MINALIGN ,arch_slab_minalign ());void *ret; gfp &= gfp_allowed_mask;
@@ -555,7 +555,7 @@ void kfree(const void *block) sp =virt_to_folio (block);if (folio_test_slab (sp)) { -int align =max_t (size_t,ARCH_KMALLOC_MINALIGN ,ARCH_SLAB_MINALIGN ); +int align =max_t (size_t,ARCH_KMALLOC_MINALIGN ,arch_slab_minalign ());unsigned int *m = (unsigned int *)(block - align);slob_free (m, *m + align); }else {
@@ -584,7 +584,7 @@ size_t __ksize(const void *block)if (unlikely (!folio_test_slab (folio)))return folio_size (folio); - align =max_t (size_t,ARCH_KMALLOC_MINALIGN ,ARCH_SLAB_MINALIGN ); + align =max_t (size_t,ARCH_KMALLOC_MINALIGN ,arch_slab_minalign ()); m = (unsigned int *)(block - align);return SLOB_UNITS (*m) *SLOB_UNIT ; }
Signature · 1 line
2.36.0.rc2.479.g8af0fa9b8e-goog
Quoted 8 lines · Peter Collingbourne
When
CONFIG_KASAN_HW_TAGSis enabled we currently increase the minimum slab alignment to 16. This happens even if MTE is not supported in hardware or disabled via kasan=off, which creates an unnecessary memory overhead in those cases. Eliminate this overhead by making the minimum slab alignment a runtime property and only aligning to 16 if KASAN is enabled at runtime.
Quoted 7 lines
On a DragonBoard 845c (non-MTE hardware) with a kernel built with
CONFIG_KASAN_HW_TAGS, waiting for quiescence after a full Android boot I see the following Slab measurements in/proc/meminfo(median of 3 reboots):Before: 169020 kB After: 167304 kB
Thanks for the improvement, Peter!
Overall, the patch looks good to me:
While looking at the code, I noticed a couple of issues in the already existing comments. Not sure if they are worth fixing, but I'll point them out just in case.
Quoted 217 lines
Signed-off-by Peter Collingbourne <[email protected]>---
v2:
- use max instead ofmax_tinflat_stack_align()arch/arc/include/asm/cache.h | 4 ++-- arch/arm/include/asm/cache.h | 2 +- arch/arm64/include/asm/cache.h | 19 +++++++++++++------ arch/microblaze/include/asm/page.h | 2 +- arch/riscv/include/asm/cache.h | 2 +- arch/sparc/include/asm/cache.h | 2 +- arch/xtensa/include/asm/processor.h | 2 +- fs/binfmt_flat.c | 9 ++++++--- include/crypto/hash.h | 2 +- include/linux/slab.h | 22 +++++++++++++++++----- mm/slab.c | 7 +++---- mm/slab_common.c | 3 +-- mm/slob.c | 6 +++--- 13 files changed, 51 insertions(+), 31 deletions(-)arch/arc/include/asm/cache.h +2
-2@@ -55,11 +55,11 @@* Make sure slab-allocated buffers are 64-bit aligned when atomic64_t uses * ARCv2 64-bit atomics (LLOCKD/SCONDD). This guarantess runtime 64-bit * alignment for any atomic64_t embedded in buffer. -* Default ARCH_SLAB_MINALIGN is __alignof__(long long) which has a relaxed +* Default ARCH_SLAB_MIN_MINALIGN is __alignof__(long long) which has a relaxed * value of 4 (and not 8) in ARC ABI. */ #if defined(CONFIG_ARC_HAS_LL64 ) && defined(CONFIG_ARC_HAS_LLSC ) -#define ARCH_SLAB_MINALIGN 8 +#define ARCH_SLAB_MIN_MINALIGN 8#endif extern int ioc_enable;arch/arm/include/asm/cache.h +1
-1@@ -21,7 +21,7 @@* With EABI on ARMv5 and above we must have 64-bit aligned slab pointers. */ #if defined(CONFIG_AEABI ) && (__LINUX_ARM_ARCH__ >=5 ) -#define ARCH_SLAB_MINALIGN 8 +#define ARCH_SLAB_MIN_MINALIGN 8#endif #define __read_mostly __section(".data..read_mostly")arch/arm64/include/asm/cache.h +13
-6@@ -6,6 +6,7 @@#define __ASM_CACHE_H#include <asm/cputype.h> +#include <asm/mte-def.h> #define CTR_L1IP_SHIFT 14#define CTR_L1IP_MASK 3@@ -49,15 +50,21 @@*/ #define ARCH_DMA_MINALIGN (128) -#ifdef CONFIG_KASAN_SW_TAGS -#define ARCH_SLAB_MINALIGN (1ULL << KASAN_SHADOW_SCALE_SHIFT) -#elif defined(CONFIG_KASAN_HW_TAGS ) -#define ARCH_SLAB_MINALIGN MTE_GRANULE_SIZE -#endif -#ifndef __ASSEMBLY__#include <linux/bitops.h> +#include <linux/kasan-enabled.h> + +#ifdef CONFIG_KASAN_SW_TAGS +#define ARCH_SLAB_MIN_MINALIGN (1ULL << KASAN_SHADOW_SCALE_SHIFT) +#elif defined(CONFIG_KASAN_HW_TAGS ) +static inline size_t arch_slab_minalign (void ) +{ +return kasan_hw_tags_enabled () ?MTE_GRANULE_SIZE : + __alignof__(unsigned long long ); +} +#define arch_slab_minalign () arch_slab_minalign() +#endif #define ICACHEF_ALIASING 0#define ICACHEF_VPIPT 1arch/microblaze/include/asm/page.h +1
-1@@ -33,7 +33,7 @@/* MS be sure that SLAB allocates aligned objects */ #define ARCH_DMA_MINALIGN L1_CACHE_BYTES -#define ARCH_SLAB_MINALIGN L1_CACHE_BYTES +#define ARCH_SLAB_MIN_MINALIGN L1_CACHE_BYTES/* * PAGE_OFFSET -- the first address of the first page of memory. With MMU arch/riscv/include/asm/cache.h +1
-1@@ -16,7 +16,7 @@* the flat loader aligns it accordingly. */ #ifndef CONFIG_MMU -#define ARCH_SLAB_MINALIGN 16 +#define ARCH_SLAB_MIN_MINALIGN 16#endif #endif /* _ASM_RISCV_CACHE_H */ arch/sparc/include/asm/cache.h +1
-1@@ -8,7 +8,7 @@#ifndef _SPARC_CACHE_H#define _SPARC_CACHE_H -#define ARCH_SLAB_MINALIGN __alignof__(unsigned long long) +#define ARCH_SLAB_MIN_MINALIGN __alignof__(unsigned long long)#define L1_CACHE_SHIFT 5#define L1_CACHE_BYTES 32arch/xtensa/include/asm/processor.h +1
-1@@ -18,7 +18,7 @@#include <asm/types.h> #include <asm/regs.h> -#define ARCH_SLAB_MINALIGN XTENSA_STACK_ALIGNMENT +#define ARCH_SLAB_MIN_MINALIGN XTENSA_STACK_ALIGNMENT/* * User space process size: 1 GB. fs/binfmt_flat.c +6
-3@@ -64,7 +64,10 @@* Here we can be a bit looser than the data sections since this * needs to only meet arch ABI requirements. */ -#define FLAT_STACK_ALIGN max_t(unsigned long, sizeof(void *), ARCH_SLAB_MINALIGN) +static size_t flat_stack_align (void ) +{ +return max (sizeof (void *),arch_slab_minalign ()); +}#define RELOC_FAILED 0xff00ff01/* Relocation incorrect somewhere */ #define UNLOADED_LIB 0x7ff000ff/* Placeholder for unused library */ @@ -148,7 +151,7 @@ static int create_flat_tables(struct linux_binprm *bprm, unsigned long arg_start sp -=2 ;/* argvp + envp */ sp -=1 ;/* &argc */ - current->mm->start_stack = (unsigned long )sp & -FLAT_STACK_ALIGN ; + current->mm->start_stack = (unsigned long )sp & -flat_stack_align (); sp = (unsigned long __user *)current->mm->start_stack;if (put_user (bprm->argc, sp++))@@ -966,7 +969,7 @@ static int load_flat_binary(struct linux_binprm *bprm)#endif stack_len += (bprm->argc +1 ) *sizeof (char *);/* the argv array */ stack_len += (bprm->envc +1 ) *sizeof (char *);/* the envp array */ - stack_len =ALIGN (stack_len,FLAT_STACK_ALIGN ); + stack_len =ALIGN (stack_len,flat_stack_align ()); res =load_flat_file (bprm, &libinfo,0 , &stack_len);if (res <0 )include/crypto/hash.h +1
-1@@ -149,7 +149,7 @@ struct ahash_alg {struct shash_desc {struct crypto_shash *tfm; -void *__ctx[]__aligned(ARCH_SLAB_MINALIGN) ; +void *__ctx[]__aligned(ARCH_SLAB_MIN_MINALIGN) ; };#define HASH_MAX_DIGESTSIZE 64include/linux/slab.h +12
-3@@ -201,21 +201,33 @@ void kmem_dump_obj(void *object);#endif /* -* Setting ARCH_SLAB_MINALIGN in arch headers allows a different alignment. +* Setting ARCH_SLAB_MIN_MINALIGN in arch headers allows a different alignment. * Intended for arches that get misalignment faults even for 64 bit integer * aligned buffers. */ -#ifndef ARCH_SLAB_MINALIGN -#define ARCH_SLAB_MINALIGN __alignof__(unsigned long long) +#ifndef ARCH_SLAB_MIN_MINALIGN +#define ARCH_SLAB_MIN_MINALIGN __alignof__(unsigned long long) +#endif + +/* +* Arches can define this function if they want to decide the minimum slab +* alignment at runtime. The value returned by the function must be +* >= ARCH_SLAB_MIN_MINALIGN. +*/ +#ifndef arch_slab_minalign +static inline size_t arch_slab_minalign (void )
include/linux/slab.h void kmem_dump_obj(void *object); · line 227
Quoted 10 lines
include/linux/slab.h +4
-1+{ +return ARCH_SLAB_MIN_MINALIGN ; +}#endif /* * kmalloc and friends return ARCH_KMALLOC_MINALIGN aligned -* pointers. kmem_cache_alloc and friends return ARCH_SLAB_MINALIGN +* pointers. kmem_cache_alloc and friends return ARCH_SLAB_MIN_MINALIGN * aligned pointers.
This comment is not precise: kmalloc relies on kmem_cache_alloc, so kmalloc technically returns pointers aligned to max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MIN_MINALIGN). See create_kmalloc_cache()->create_boot_cache()->calculate_alignment() for SLAB and SLUB and __do_kmalloc_node() for SLOB. This alignment is stronger than the one is specified for __assume_kmalloc_alignment below, so the code should be fine. However, the comment is confusing.
Also, the comment next to the ARCH_KMALLOC_MINALIGN definition says "Setting ARCH_KMALLOC_MINALIGN in arch headers" while it should say "Setting ARCH_DMA_MINALIGN in arch headers".
Quoted 73 lines
include/linux/slab.h +1
-1*/ #define __assume_kmalloc_alignment __assume_aligned(ARCH_KMALLOC_MINALIGN) -#define __assume_slab_alignment __assume_aligned(ARCH_SLAB_MINALIGN) +#define __assume_slab_alignment __assume_aligned(ARCH_SLAB_MIN_MINALIGN)#define __assume_page_alignment __assume_aligned(PAGE_SIZE)/* mm/slab.c +3
-4@@ -3009,10 +3009,9 @@ static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep, objp +=obj_offset (cachep);if (cachep->ctor && cachep->flags &SLAB_POISON ) cachep->ctor (objp); -if (ARCH_SLAB_MINALIGN && - ((unsigned long )objp & (ARCH_SLAB_MINALIGN -1 ))) { -pr_err ("0x%px: not aligned to ARCH_SLAB_MINALIGN=%d\n" , - objp, (int )ARCH_SLAB_MINALIGN ); +if ((unsigned long )objp & (arch_slab_minalign () -1 )) { +pr_err ("0x%px: not aligned to arch_slab_minalign()=%d\n" , objp, + (int )arch_slab_minalign ()); }return objp; }mm/slab_common.c +1
-2@@ -154,8 +154,7 @@ static unsigned int calculate_alignment(slab_flags_t flags, align =max (align, ralign); } -if (align <ARCH_SLAB_MINALIGN ) - align =ARCH_SLAB_MINALIGN ; + align =max_t (size_t, align,arch_slab_minalign ());return ALIGN (align,sizeof (void *)); }mm/slob.c +3
-4@@ -478,7 +478,7 @@ static __always_inline void * __do_kmalloc_node(size_t size, gfp_t gfp, int node, unsigned long caller) { unsigned int *m; -int minalign =max_t (size_t,ARCH_KMALLOC_MINALIGN ,ARCH_SLAB_MINALIGN ); +int minalign =max_t (size_t,ARCH_KMALLOC_MINALIGN ,arch_slab_minalign ());void *ret; gfp &= gfp_allowed_mask;@@ -555,7 +555,7 @@ void kfree(const void *block) sp =virt_to_folio (block);if (folio_test_slab (sp)) { -int align =max_t (size_t,ARCH_KMALLOC_MINALIGN ,ARCH_SLAB_MINALIGN ); +int align =max_t (size_t,ARCH_KMALLOC_MINALIGN ,arch_slab_minalign ());unsigned int *m = (unsigned int *)(block - align);slob_free (m, *m + align); }else {@@ -584,7 +584,7 @@ size_t __ksize(const void *block)if (unlikely (!folio_test_slab (folio)))return folio_size (folio); - align =max_t (size_t,ARCH_KMALLOC_MINALIGN ,ARCH_SLAB_MINALIGN ); + align =max_t (size_t,ARCH_KMALLOC_MINALIGN ,arch_slab_minalign ()); m = (unsigned int *)(block - align);return SLOB_UNITS (*m) *SLOB_UNIT ; } --2.36.0.rc2.479.g8af0fa9b8e-goog
Catalin Marinas
maintainerQuoted 8 lines · Peter Collingbourne
include/linux/slab.h +0
-0@@ -201,21 +201,33 @@ void kmem_dump_obj(void *object);#endif /*
include/linux/slab.h void kmem_dump_obj(void *object); · line 210
Quoted 10 lines
include/linux/slab.h +4
-3-* Setting ARCH_SLAB_MINALIGN in arch headers allows a different alignment. +* Setting ARCH_SLAB_MIN_MINALIGN in arch headers allows a different alignment. * Intended for arches that get misalignment faults even for 64 bit integer * aligned buffers. */ -#ifndef ARCH_SLAB_MINALIGN -#define ARCH_SLAB_MINALIGN __alignof__(unsigned long long) +#ifndef ARCH_SLAB_MIN_MINALIGN +#define ARCH_SLAB_MIN_MINALIGN __alignof__(unsigned long long) +#endif
Sorry, only a drive-by comment, I'll look at the arm64 parts next week. I've seen it mentioned in the first version, what's the point of MIN_MIN and not just MIN?
Signature · 1 line
Catalin
Quoted 15 lines · Andrey Konovalov
Quoted 13 lines · Peter Collingbourne
When
CONFIG_KASAN_HW_TAGSis enabled we currently increase the minimum slab alignment to 16. This happens even if MTE is not supported in hardware or disabled via kasan=off, which creates an unnecessary memory overhead in those cases. Eliminate this overhead by making the minimum slab alignment a runtime property and only aligning to 16 if KASAN is enabled at runtime.On a DragonBoard 845c (non-MTE hardware) with a kernel built with
CONFIG_KASAN_HW_TAGS, waiting for quiescence after a full Android boot I see the following Slab measurements in/proc/meminfo(median of 3 reboots):
Quoted 8 lines
Quoted 2 lines
Before: 169020 kB After: 167304 kBThanks for the improvement, Peter!
Overall, the patch looks good to me:
Reviewed-by Andrey Konovalov <[email protected]>
Thanks!
Quoted 240 lines
While looking at the code, I noticed a couple of issues in the already existing comments. Not sure if they are worth fixing, but I'll point them out just in case.
Quoted 217 lines
Signed-off-by Peter Collingbourne <[email protected]>---
v2:
- use max instead ofmax_tinflat_stack_align()arch/arc/include/asm/cache.h | 4 ++-- arch/arm/include/asm/cache.h | 2 +- arch/arm64/include/asm/cache.h | 19 +++++++++++++------ arch/microblaze/include/asm/page.h | 2 +- arch/riscv/include/asm/cache.h | 2 +- arch/sparc/include/asm/cache.h | 2 +- arch/xtensa/include/asm/processor.h | 2 +- fs/binfmt_flat.c | 9 ++++++--- include/crypto/hash.h | 2 +- include/linux/slab.h | 22 +++++++++++++++++----- mm/slab.c | 7 +++---- mm/slab_common.c | 3 +-- mm/slob.c | 6 +++--- 13 files changed, 51 insertions(+), 31 deletions(-)arch/arc/include/asm/cache.h +2
-2@@ -55,11 +55,11 @@* Make sure slab-allocated buffers are 64-bit aligned when atomic64_t uses * ARCv2 64-bit atomics (LLOCKD/SCONDD). This guarantess runtime 64-bit * alignment for any atomic64_t embedded in buffer. -* Default ARCH_SLAB_MINALIGN is __alignof__(long long) which has a relaxed +* Default ARCH_SLAB_MIN_MINALIGN is __alignof__(long long) which has a relaxed * value of 4 (and not 8) in ARC ABI. */ #if defined(CONFIG_ARC_HAS_LL64 ) && defined(CONFIG_ARC_HAS_LLSC ) -#define ARCH_SLAB_MINALIGN 8 +#define ARCH_SLAB_MIN_MINALIGN 8#endif extern int ioc_enable;arch/arm/include/asm/cache.h +1
-1@@ -21,7 +21,7 @@* With EABI on ARMv5 and above we must have 64-bit aligned slab pointers. */ #if defined(CONFIG_AEABI ) && (__LINUX_ARM_ARCH__ >=5 ) -#define ARCH_SLAB_MINALIGN 8 +#define ARCH_SLAB_MIN_MINALIGN 8#endif #define __read_mostly __section(".data..read_mostly")arch/arm64/include/asm/cache.h +13
-6@@ -6,6 +6,7 @@#define __ASM_CACHE_H#include <asm/cputype.h> +#include <asm/mte-def.h> #define CTR_L1IP_SHIFT 14#define CTR_L1IP_MASK 3@@ -49,15 +50,21 @@*/ #define ARCH_DMA_MINALIGN (128) -#ifdef CONFIG_KASAN_SW_TAGS -#define ARCH_SLAB_MINALIGN (1ULL << KASAN_SHADOW_SCALE_SHIFT) -#elif defined(CONFIG_KASAN_HW_TAGS ) -#define ARCH_SLAB_MINALIGN MTE_GRANULE_SIZE -#endif -#ifndef __ASSEMBLY__#include <linux/bitops.h> +#include <linux/kasan-enabled.h> + +#ifdef CONFIG_KASAN_SW_TAGS +#define ARCH_SLAB_MIN_MINALIGN (1ULL << KASAN_SHADOW_SCALE_SHIFT) +#elif defined(CONFIG_KASAN_HW_TAGS ) +static inline size_t arch_slab_minalign (void ) +{ +return kasan_hw_tags_enabled () ?MTE_GRANULE_SIZE : + __alignof__(unsigned long long ); +} +#define arch_slab_minalign () arch_slab_minalign() +#endif #define ICACHEF_ALIASING 0#define ICACHEF_VPIPT 1arch/microblaze/include/asm/page.h +1
-1@@ -33,7 +33,7 @@/* MS be sure that SLAB allocates aligned objects */ #define ARCH_DMA_MINALIGN L1_CACHE_BYTES -#define ARCH_SLAB_MINALIGN L1_CACHE_BYTES +#define ARCH_SLAB_MIN_MINALIGN L1_CACHE_BYTES/* * PAGE_OFFSET -- the first address of the first page of memory. With MMU arch/riscv/include/asm/cache.h +1
-1@@ -16,7 +16,7 @@* the flat loader aligns it accordingly. */ #ifndef CONFIG_MMU -#define ARCH_SLAB_MINALIGN 16 +#define ARCH_SLAB_MIN_MINALIGN 16#endif #endif /* _ASM_RISCV_CACHE_H */ arch/sparc/include/asm/cache.h +1
-1@@ -8,7 +8,7 @@#ifndef _SPARC_CACHE_H#define _SPARC_CACHE_H -#define ARCH_SLAB_MINALIGN __alignof__(unsigned long long) +#define ARCH_SLAB_MIN_MINALIGN __alignof__(unsigned long long)#define L1_CACHE_SHIFT 5#define L1_CACHE_BYTES 32arch/xtensa/include/asm/processor.h +1
-1@@ -18,7 +18,7 @@#include <asm/types.h> #include <asm/regs.h> -#define ARCH_SLAB_MINALIGN XTENSA_STACK_ALIGNMENT +#define ARCH_SLAB_MIN_MINALIGN XTENSA_STACK_ALIGNMENT/* * User space process size: 1 GB. fs/binfmt_flat.c +6
-3@@ -64,7 +64,10 @@* Here we can be a bit looser than the data sections since this * needs to only meet arch ABI requirements. */ -#define FLAT_STACK_ALIGN max_t(unsigned long, sizeof(void *), ARCH_SLAB_MINALIGN) +static size_t flat_stack_align (void ) +{ +return max (sizeof (void *),arch_slab_minalign ()); +}#define RELOC_FAILED 0xff00ff01/* Relocation incorrect somewhere */ #define UNLOADED_LIB 0x7ff000ff/* Placeholder for unused library */ @@ -148,7 +151,7 @@ static int create_flat_tables(struct linux_binprm *bprm, unsigned long arg_start sp -=2 ;/* argvp + envp */ sp -=1 ;/* &argc */ - current->mm->start_stack = (unsigned long )sp & -FLAT_STACK_ALIGN ; + current->mm->start_stack = (unsigned long )sp & -flat_stack_align (); sp = (unsigned long __user *)current->mm->start_stack;if (put_user (bprm->argc, sp++))@@ -966,7 +969,7 @@ static int load_flat_binary(struct linux_binprm *bprm)#endif stack_len += (bprm->argc +1 ) *sizeof (char *);/* the argv array */ stack_len += (bprm->envc +1 ) *sizeof (char *);/* the envp array */ - stack_len =ALIGN (stack_len,FLAT_STACK_ALIGN ); + stack_len =ALIGN (stack_len,flat_stack_align ()); res =load_flat_file (bprm, &libinfo,0 , &stack_len);if (res <0 )include/crypto/hash.h +1
-1@@ -149,7 +149,7 @@ struct ahash_alg {struct shash_desc {struct crypto_shash *tfm; -void *__ctx[]__aligned(ARCH_SLAB_MINALIGN) ; +void *__ctx[]__aligned(ARCH_SLAB_MIN_MINALIGN) ; };#define HASH_MAX_DIGESTSIZE 64include/linux/slab.h +12
-3@@ -201,21 +201,33 @@ void kmem_dump_obj(void *object);#endif /* -* Setting ARCH_SLAB_MINALIGN in arch headers allows a different alignment. +* Setting ARCH_SLAB_MIN_MINALIGN in arch headers allows a different alignment. * Intended for arches that get misalignment faults even for 64 bit integer * aligned buffers. */ -#ifndef ARCH_SLAB_MINALIGN -#define ARCH_SLAB_MINALIGN __alignof__(unsigned long long) +#ifndef ARCH_SLAB_MIN_MINALIGN +#define ARCH_SLAB_MIN_MINALIGN __alignof__(unsigned long long) +#endif + +/* +* Arches can define this function if they want to decide the minimum slab +* alignment at runtime. The value returned by the function must be +* >= ARCH_SLAB_MIN_MINALIGN. +*/ +#ifndef arch_slab_minalign +static inline size_t arch_slab_minalign (void )include/linux/slab.h void kmem_dump_obj(void *object); · line 227
Quoted 10 lines
include/linux/slab.h +4
-1+{ +return ARCH_SLAB_MIN_MINALIGN ; +}#endif /* * kmalloc and friends return ARCH_KMALLOC_MINALIGN aligned -* pointers. kmem_cache_alloc and friends return ARCH_SLAB_MINALIGN +* pointers. kmem_cache_alloc and friends return ARCH_SLAB_MIN_MINALIGN * aligned pointers. This comment is not precise: kmalloc relies on
kmem_cache_alloc, so kmalloc technically returns pointers aligned tomax(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MIN_MINALIGN). See create_kmalloc_cache()->create_boot_cache()->calculate_alignment() for SLAB and SLUB and__do_kmalloc_node()for SLOB. This alignment is stronger than the one is specified for__assume_kmalloc_alignmentbelow, so the code should be fine. However, the comment is confusing.
Quoted 3 lines
Also, the comment next to the
ARCH_KMALLOC_MINALIGNdefinition says "SettingARCH_KMALLOC_MINALIGNin arch headers" while it should say "SettingARCH_DMA_MINALIGNin arch headers".
Good catches. Let's fix these issues separately (especially since I reverted the ARCH_SLAB_MIN_MINALIGN change so the patches shouldn't conflict). Would you like to send patches? I can if not.
Peter
Quoted 9 lines · Catalin Marinas
Quoted 7 lines · Peter Collingbourne
include/linux/slab.h +0
-0@@ -201,21 +201,33 @@ void kmem_dump_obj(void *object);#endif
Quoted 15 lines
include/linux/slab.h void kmem_dump_obj(void *object); · line 210
Quoted 11 lines
include/linux/slab.h +4
-3/* -* Setting ARCH_SLAB_MINALIGN in arch headers allows a different alignment. +* Setting ARCH_SLAB_MIN_MINALIGN in arch headers allows a different alignment. * Intended for arches that get misalignment faults even for 64 bit integer * aligned buffers. */ -#ifndef ARCH_SLAB_MINALIGN -#define ARCH_SLAB_MINALIGN __alignof__(unsigned long long) +#ifndef ARCH_SLAB_MIN_MINALIGN +#define ARCH_SLAB_MIN_MINALIGN __alignof__(unsigned long long) +#endif Sorry, only a drive-by comment, I'll look at the arm64 parts next week. I've seen it mentioned in the first version, what's the point of
MIN_MINand not just MIN?
I tried to explain it here:
https://lore.kernel.org/all/CAMn1gO5xHZvFSSsW5sTVaUBN_gS-cYYNMG3PnpgCmh7kk_Zx7Q@mail.gmail.com/
In the end I decided to go back to MIN so this is moot.
Peter