mm/slab: reduce slab accounting memory overhead by allocating slabobj_ext metadata within unsed slab space
Subsystems (6)
v4 cover mm/slab: reduce slab accounting memory overhead by allocating slabobj_ext metadata within unsed slab space
RFC V3: https://lore.kernel.org/linux-mm/[email protected]
I believe I addressed all comments in RFC V3 (except handling lazy allocation of slabobj_exts, which I would prefer to do as future work). Please let me know if I missed your comments.
If there is no major drawbacks or concerns coming up, I would like to push this forward for 7.0 merge window after some review & testing.
Have a wonderful end of the year!
RFC V3 -> V4:
- Rebased onto the latest slab/for-next, dropped RFC
- The metadata alignment (after orig_size) fix is now included as patch 1 of this series
- Patch 2: Document that use_freeptr_offset can be used for caches with constructor (Suren, Vlastimil)
- Patch 6: use get/put_slab_obj_exts() instead of metadata_access_enable/disable (Suren)
- Patch 7: Change !mem_cgroup_disabled() check to memcg_kmem_online() (Andrey Ryabinin)
- Added Reviewed-by, Suggested-by tags, thanks!
When CONFIG_MEMCG and CONFIG_MEM_ALLOC_PROFILING are enabled, the kernel allocates two pointers per object: one for the memory cgroup (obj_cgroup) to which it belongs, and another for the code location that requested the allocation.
In two special cases, this overhead can be eliminated by allocating slabobj_ext metadata from unused space within a slab:
Case 1. The "leftover" space after the last slab object is larger than
the size of an array of slabobj_ext.
Case 2. The per-object alignment padding is larger than
sizeof(struct slabobj_ext).
For these two cases, one or two pointers can be saved per slab object.
Examples: ext4 inode cache (case 1) and xfs inode cache (case 2). That's approximately 0.7-0.8% (memcg) or 1.5-1.6%% (memcg + mem profiling) of the total inode cache size.
Implementing case 2 is not straightforward, because the existing code assumes that slab->obj_exts is an array of slabobj_ext, while case 2 breaks the assumption.
As suggested by Vlastimil, abstract access to individual slabobj_ext metadata via a new helper named slab_obj_ext():
static inline struct slabobj_ext *slab_obj_ext(struct slab *slab,
unsigned long obj_exts, unsigned int index)
{
return (struct slabobj_ext *)(obj_exts + slab_get_stride(slab) * index);
} In the normal case (including case 1), slab->obj_exts points to an array of slabobj_ext, and the stride is sizeof(struct slabobj_ext).
In case 2, the stride is s->size andslab->obj_exts = slab_address(slab) + s->red_left_pad + (offset of slabobj_ext)
With this approach, the memcg charging fastpath doesn't need to care the storage method of slabobj_ext.
Harry Yoo (8):
mm/slab: use unsigned long for orig_size to ensure proper metadata
align
mm/slab: allow specifying free pointer offset when using constructor
ext4: specify the free pointer offset for ext4_inode_cache mm/slab: abstract slabobj_ext access via new slab_obj_ext() helper mm/slab: use stride to access slabobj_ext mm/memcontrol,alloc_tag: handle slabobj_ext access under KASAN poison mm/slab: save memory by allocating slabobj_ext array from leftover mm/slab: place slabobj_ext metadata in unused space within s->size
fs/ext4/super.c | 20 ++- include/linux/slab.h | 39 +++-- mm/memcontrol.c | 31 +++- mm/slab.h | 120 ++++++++++++++- mm/slab_common.c | 8 +- mm/slub.c | 345 +++++++++++++++++++++++++++++++++++-------- 6 files changed, 466 insertions(+), 97 deletions(-)
Signature · 1 line
2.43.0
PATCH v4 1/8 mm/slab: use unsigned long for orig_size to ensure proper metadata align
When both KASAN and SLAB_STORE_USER are enabled, accesses to struct kasan_alloc_meta fields can be misaligned on 64-bit architectures. This occurs because orig_size is currently defined as unsigned int, which only guarantees 4-byte alignment. When struct kasan_alloc_meta is placed after orig_size, it may end up at a 4-byte boundary rather than the required 8-byte boundary on 64-bit systems.
Note that 64-bit architectures without HAVE_EFFICIENT_UNALIGNED_ACCESS are assumed to require 64-bit accesses to be 64-bit aligned. See HAVE_64BIT_ALIGNED_ACCESS and commit adab66b71abf ("Revert:
"ring-buffer: Remove HAVE_64BIT_ALIGNED_ACCESS"") for more details.
Change orig_size from unsigned int to unsigned long to ensure proper alignment for any subsequent metadata. This should not waste additional memory because kmalloc objects are already aligned to at least ARCH_KMALLOC_MINALIGN.
--- mm/slub.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
mm/slub.c +7 -7
@@ -857,7 +857,7 @@ static inline bool slab_update_freelist(struct kmem_cache *s, struct slab *slab,* request size in the meta data area, for better debug and sanity check. */ static inline void set_orig_size (struct kmem_cache *s, -void *object,unsigned int orig_size) +void *object,unsigned long orig_size) {void *p =kasan_reset_tag (object);
@@ -867,10 +867,10 @@ static inline void set_orig_size(struct kmem_cache *s, p +=get_info_end (s); p +=sizeof (struct track ) *2 ; - *(unsigned int *)p = orig_size; + *(unsigned long *)p = orig_size; } -static inline unsigned int get_orig_size (struct kmem_cache *s,void *object) +static inline unsigned long get_orig_size (struct kmem_cache *s,void *object) {void *p =kasan_reset_tag (object);
@@ -883,7 +883,7 @@ static inline unsigned int get_orig_size(struct kmem_cache *s, void *object) p +=get_info_end (s); p +=sizeof (struct track ) *2 ; -return *(unsigned int *)p; +return *(unsigned long *)p; }#ifdef CONFIG_SLUB_DEBUG
@@ -1198,7 +1198,7 @@ static void print_trailer(struct kmem_cache *s, struct slab *slab, u8 *p) off +=2 *sizeof (struct track );if (slub_debug_orig_size (s)) - off +=sizeof (unsigned int ); + off +=sizeof (unsigned long ); off +=kasan_metadata_size (s, false);
@@ -1394,7 +1394,7 @@ static int check_pad_bytes(struct kmem_cache *s, struct slab *slab, u8 *p) off +=2 *sizeof (struct track );if (s->flags &SLAB_KMALLOC ) - off +=sizeof (unsigned int ); + off +=sizeof (unsigned long ); } off +=kasan_metadata_size (s, false);
@@ -7949,7 +7949,7 @@ static int calculate_sizes(struct kmem_cache_args *args, struct kmem_cache *s)/* Save the original kmalloc request size */ if (flags &SLAB_KMALLOC ) - size += sizeof(unsigned int); + size += sizeof(unsigned long); }#endif
Signature · 1 line
2.43.0
PATCH v4 2/8 mm/slab: allow specifying free pointer offset when using constructor
In reply to Harry YooWhen a slab cache has a constructor, the free pointer is placed after the object because certain fields must not be overwritten even after the object is freed.
However, some fields that the constructor does not initialize can safely be overwritten after free. Allow specifying the free pointer offset within the object, reducing the overall object size when some fields can be reused for the free pointer.
Adjust the document accordingly.
--- include/linux/slab.h | 30 ++++++++++++++++-------------- mm/slab_common.c | 2 +- mm/slub.c | 6 ++++-- 3 files changed, 21 insertions(+), 17 deletions(-)
include/linux/slab.h +16 -14
@@ -299,24 +299,26 @@ struct kmem_cache_args {unsigned int usersize;/** * @freeptr_offset: Custom offset for the free pointer -* in &SLAB_TYPESAFE_BY_RCU caches +* in caches with &SLAB_TYPESAFE_BY_RCU or @ctor * -* By default &SLAB_TYPESAFE_BY_RCU caches place the free pointer -* outside of the object. This might cause the object to grow in size. -* Cache creators that have a reason to avoid this can specify a custom -* free pointer offset in their struct where the free pointer will be -* placed. +* By default, &SLAB_TYPESAFE_BY_RCU and @ctor caches place the free +* pointer outside of the object. This might cause the object to grow +* in size. Cache creators that have a reason to avoid this can specify +* a custom free pointer offset in their data structure where the free +* pointer will be placed. * -* Note that placing the free pointer inside the object requires the -* caller to ensure that no fields are invalidated that are required to -* guard against object recycling (See &SLAB_TYPESAFE_BY_RCU for -* details). +* For caches with &SLAB_TYPESAFE_BY_RCU, the caller must ensure that +* the free pointer does not overlay fields required to guard against +* object recycling (See &SLAB_TYPESAFE_BY_RCU for details). * -* Using %0 as a value for @freeptr_offset is valid. If @freeptr_offset -* is specified, %use_freeptr_offset must be set %true. +* For caches with @ctor, the caller must ensure that the free pointer +* does not overlay fields initialized by the constructor. +* +* Currently, only caches with &SLAB_TYPESAFE_BY_RCU or @ctor +* may specify @freeptr_offset. * -* Note that @ctor currently isn't supported with custom free pointers -* as a @ctor requires an external free pointer. +* Using %0 as a value for @freeptr_offset is valid. If @freeptr_offset +* is specified, @use_freeptr_offset must be set %true. */ unsigned int freeptr_offset;/**
mm/slab_common.c +1 -1
@@ -231,7 +231,7 @@ static struct kmem_cache *create_cache(const char *name, err = -EINVAL ;if (args->use_freeptr_offset && (args->freeptr_offset >= object_size || - !(flags &SLAB_TYPESAFE_BY_RCU ) || + (!(flags &SLAB_TYPESAFE_BY_RCU ) && !args->ctor) || !IS_ALIGNED (args->freeptr_offset, __alignof__(freeptr_t )))) goto out;
mm/slub.c +4 -2
@@ -7907,7 +7907,8 @@ static int calculate_sizes(struct kmem_cache_args *args, struct kmem_cache *s) s->inuse = size;if (((flags &SLAB_TYPESAFE_BY_RCU ) && !args->use_freeptr_offset) || - (flags &SLAB_POISON ) || s->ctor || + (flags &SLAB_POISON ) || + (s->ctor && !args->use_freeptr_offset) || ((flags &SLAB_RED_ZONE ) && (s->object_size <sizeof (void *) ||slub_debug_orig_size (s)))) {/*
@@ -7928,7 +7929,8 @@ static int calculate_sizes(struct kmem_cache_args *args, struct kmem_cache *s)*/ s->offset = size; size +=sizeof (void *); - }else if ((flags &SLAB_TYPESAFE_BY_RCU ) && args->use_freeptr_offset) { + }else if (((flags &SLAB_TYPESAFE_BY_RCU ) || s->ctor) && + args->use_freeptr_offset) { s->offset = args->freeptr_offset; }else {/*
Signature · 1 line
2.43.0
PATCH v4 3/8 ext4: specify the free pointer offset for ext4_inode_cache
In reply to Harry YooConvert ext4_inode_cache to use the kmem_cache_args interface and specify a free pointer offset.
Since ext4_inode_cache uses a constructor, the free pointer would be placed after the object to overwriting fields used by the constructor. However, some fields such as ->i_flags are not used by the constructor and can safely be repurposed for the free pointer.
Specify the free pointer offset at i_flags to reduce the object size.
--- fs/ext4/super.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
fs/ext4/super.c +14 -6
@@ -1491,12 +1491,20 @@ static void init_once(void *foo)static int __init init_inodecache (void ) { - ext4_inode_cachep =kmem_cache_create_usercopy ("ext4_inode_cache" , -sizeof (struct ext4_inode_info ),0 , -SLAB_RECLAIM_ACCOUNT |SLAB_ACCOUNT , - offsetof(struct ext4_inode_info , i_data), - sizeof_field(struct ext4_inode_info, i_data), - init_once); +struct kmem_cache_args args = { + .align =0 , + .useroffset = offsetof(struct ext4_inode_info , i_data), + .usersize = sizeof_field(struct ext4_inode_info, i_data), + .use_freeptr_offset = true, + .freeptr_offset = offsetof(struct ext4_inode_info , i_flags), + .ctor = init_once, + }; + + ext4_inode_cachep =kmem_cache_create ("ext4_inode_cache" , +sizeof (struct ext4_inode_info ), + &args, +SLAB_RECLAIM_ACCOUNT |SLAB_ACCOUNT ); +if (ext4_inode_cachep ==NULL )return -ENOMEM ;return 0 ;
Signature · 1 line
2.43.0
PATCH v4 4/8 mm/slab: abstract slabobj_ext access via new slab_obj_ext() helper
In reply to Harry YooCurrently, the slab allocator assumes that slab->obj_exts is a pointer to an array of struct slabobj_ext objects. However, to support storage methods where struct slabobj_ext is embedded within objects, the slab allocator should not make this assumption. Instead of directly dereferencing the slabobj_exts array, abstract access to struct slabobj_ext via helper functions.
Introduce a new API slabobj_ext metadata access:
slab_obj_ext(slab, obj_exts, index) - returns the pointer to struct slabobj_ext element at the given index.
Directly dereferencing the return value of slab_obj_exts() is no longer allowed. Instead, slab_obj_ext() must always be used to access individual struct slabobj_ext objects.
Convert all users to use these APIs.
No functional changes intended.
--- mm/memcontrol.c | 23 +++++++++++++++------- mm/slab.h | 43 +++++++++++++++++++++++++++++++++++------ mm/slub.c | 51 ++++++++++++++++++++++++++++--------------------- 3 files changed, 82 insertions(+), 35 deletions(-)
mm/memcontrol.c +16 -7
@@ -2596,7 +2596,8 @@ struct mem_cgroup *mem_cgroup_from_obj_slab(struct slab *slab, void *p)* Memcg membership data for each individual object is saved in * slab->obj_exts. */ -struct slabobj_ext *obj_exts; +unsigned long obj_exts; +struct slabobj_ext *obj_ext;unsigned int off; obj_exts =slab_obj_exts (slab);
@@ -2604,8 +2605,9 @@ struct mem_cgroup *mem_cgroup_from_obj_slab(struct slab *slab, void *p)return NULL ; off =obj_to_index (slab->slab_cache, slab, p); -if (obj_exts[off].objcg) -return obj_cgroup_memcg (obj_exts[off].objcg); + obj_ext =slab_obj_ext (slab, obj_exts, off); +if (obj_ext->objcg) +return obj_cgroup_memcg (obj_ext->objcg);return NULL ; }
@@ -3191,6 +3193,9 @@ bool __memcg_slab_post_alloc_hook(struct kmem_cache *s, struct list_lru *lru, }for (i =0 ; i < size; i++) { +unsigned long obj_exts; +struct slabobj_ext *obj_ext; + slab =virt_to_slab (p[i]); if (!slab_obj_exts(slab) &&
@@ -3213,29 +3218,33 @@ bool __memcg_slab_post_alloc_hook(struct kmem_cache *s, struct list_lru *lru, slab_pgdat(slab), cache_vmstat_idx(s)))return false; + obj_exts =slab_obj_exts (slab); off =obj_to_index (s, slab, p[i]); + obj_ext =slab_obj_ext (slab, obj_exts, off);obj_cgroup_get (objcg); -slab_obj_exts (slab)[off].objcg = objcg; + obj_ext->objcg = objcg; }return true; }void __memcg_slab_free_hook (struct kmem_cache *s,struct slab *slab, -void **p,int objects,struct slabobj_ext *obj_exts) +void **p,int objects,unsigned long obj_exts) {size_t obj_size =obj_full_size (s);for (int i =0 ; i < objects; i++) {struct obj_cgroup *objcg; +struct slabobj_ext *obj_ext;unsigned int off; off =obj_to_index (s, slab, p[i]); - objcg = obj_exts[off].objcg; + obj_ext =slab_obj_ext (slab, obj_exts, off); + objcg = obj_ext->objcg;if (!objcg)continue ; - obj_exts[off].objcg =NULL ; + obj_ext->objcg =NULL ;refill_obj_stock (objcg, obj_size, true, -obj_size,slab_pgdat (slab),cache_vmstat_idx (s));obj_cgroup_put (objcg);
mm/slab.h +37 -6
68 lines not on this page · raw message
mm/slub.c +29 -22
146 lines not on this page · raw message
Signature · 1 line
2.43.0
kernel test robot
build botRe: [PATCH V4 4/8] mm/slab: abstract slabobj_ext access via new slab_obj_ext() helper
kernel test robot noticed the following build errors:
Hi Harry,
kernel test robot noticed the following build errors:
[auto build test ERROR on akpm-mm/mm-everything]
url: https://github.com/intel-lab-lkp/linux/commits/Harry-Yoo/mm-slab-use-unsigned-long-for-orig_size-to-ensure-proper-metadata-align/20251222-191144 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything patch link: https://lore.kernel.org/r/20251222110843.980347-5-harry.yoo%40oracle.com patch subject: [PATCH V4 4/8] mm/slab: abstract slabobj_ext access via new slab_obj_ext() helper config: x86_64-buildonly-randconfig-001-20251223 (https://download.01.org/0day-ci/archive/20251223/[email protected]/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251223/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> mm/slub.c:2056:33: error: passing 'union codetag_ref' to parameter of incompatible type 'union codetag_ref *'; take the address with &
2056 | if (unlikely(is_codetag_empty(ext->ref)))
| ^~~~~~~~
| &
include/linux/compiler.h:47:41: note: expanded from macro 'unlikely'
47 | # define unlikely(x) (__branch_check__(x, 0, __builtin_constant_p(x)))
| ^
include/linux/compiler.h:32:34: note: expanded from macro '__branch_check__'
32 | ______r = __builtin_expect(!!(x), expect); \
| ^
include/linux/alloc_tag.h:52:56: note: passing argument to parameter 'ref' here
52 | static inline bool is_codetag_empty(union codetag_ref *ref)
| ^
>> mm/slub.c:2056:33: error: passing 'union codetag_ref' to parameter of incompatible type 'union codetag_ref *'; take the address with &
2056 | if (unlikely(is_codetag_empty(ext->ref)))
| ^~~~~~~~
| &
include/linux/compiler.h:47:68: note: expanded from macro 'unlikely'
47 | # define unlikely(x) (__branch_check__(x, 0, __builtin_constant_p(x)))
| ^
include/linux/compiler.h:34:19: note: expanded from macro '__branch_check__'
34 | expect, is_constant); \
| ^~~~~~~~~~~ include/linux/alloc_tag.h:52:56: note: passing argument to parameter 'ref' here
52 | static inline bool is_codetag_empty(union codetag_ref *ref)
| ^
2 errors generated.
vim +2056 mm/slub.c
2042
2043 static inline void mark_objexts_empty(struct slabobj_ext *obj_exts)
2044 {
2045 unsigned long slab_exts;
2046 struct slab *obj_exts_slab;
2047
2048 obj_exts_slab = virt_to_slab(obj_exts);
2049 slab_exts = slab_obj_exts(obj_exts_slab);
2050 if (slab_exts) {
2051 unsigned int offs = obj_to_index(obj_exts_slab->slab_cache,
2052 obj_exts_slab, obj_exts);
2053 struct slabobj_ext *ext = slab_obj_ext(obj_exts_slab,
2054 slab_exts, offs);
2055
> 2056 if (unlikely(is_codetag_empty(ext->ref)))
2057 return;
2058
2059 /* codetag should be NULL here */
2060 WARN_ON(ext->ref.ct);
2061 set_codetag_empty(&ext->ref);
2062 }
2063 }
2064
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [PATCH V4 4/8] mm/slab: abstract slabobj_ext access via new slab_obj_ext() helper
In reply to Harry Yookernel test robot noticed the following build errors:
Hi Harry,
kernel test robot noticed the following build errors:
[auto build test ERROR on akpm-mm/mm-everything]
url: https://github.com/intel-lab-lkp/linux/commits/Harry-Yoo/mm-slab-use-unsigned-long-for-orig_size-to-ensure-proper-metadata-align/20251222-191144 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything patch link: https://lore.kernel.org/r/20251222110843.980347-5-harry.yoo%40oracle.com patch subject: [PATCH V4 4/8] mm/slab: abstract slabobj_ext access via new slab_obj_ext() helper config: parisc-randconfig-002-20251223 (https://download.01.org/0day-ci/archive/20251223/[email protected]/config) compiler: hppa-linux-gcc (GCC) 8.5.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251223/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
In file included from include/asm-generic/bug.h:5,
from arch/parisc/include/asm/bug.h:97,
from include/linux/bug.h:5,
from include/linux/mmdebug.h:5,
from include/linux/mm.h:6,
from mm/slub.c:13:
mm/slub.c: In function 'mark_objexts_empty':
>> mm/slub.c:2056:36: error: incompatible type for argument 1 of 'is_codetag_empty'
if (unlikely(is_codetag_empty(ext->ref)))
~~~^~~~~
include/linux/compiler.h:77:42: note: in definition of macro 'unlikely'
# define unlikely(x) __builtin_expect(!!(x), 0)
^
In file included from include/linux/workqueue.h:9,
from include/linux/mm_types.h:19,
from include/linux/mmzone.h:22,
from include/linux/gfp.h:7,
from include/linux/mm.h:7,
from mm/slub.c:13:
include/linux/alloc_tag.h:52:56: note: expected 'union codetag_ref *' but argument is of type 'union codetag_ref'
static inline bool is_codetag_empty(union codetag_ref *ref)
~~~~~~~~~~~~~~~~~~~^~~Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for CAN_DEV Depends on [n]: NETDEVICES [=n] && CAN [=y] Selected by [y]:
- CAN [=y] && NET [=y]
vim +/is_codetag_empty +2056 mm/slub.c
2042
2043 static inline void mark_objexts_empty(struct slabobj_ext *obj_exts)
2044 {
2045 unsigned long slab_exts;
2046 struct slab *obj_exts_slab;
2047
2048 obj_exts_slab = virt_to_slab(obj_exts);
2049 slab_exts = slab_obj_exts(obj_exts_slab);
2050 if (slab_exts) {
2051 unsigned int offs = obj_to_index(obj_exts_slab->slab_cache,
2052 obj_exts_slab, obj_exts);
2053 struct slabobj_ext *ext = slab_obj_ext(obj_exts_slab,
2054 slab_exts, offs);
2055
> 2056 if (unlikely(is_codetag_empty(ext->ref)))
2057 return;
2058
2059 /* codetag should be NULL here */
2060 WARN_ON(ext->ref.ct);
2061 set_codetag_empty(&ext->ref);
2062 }
2063 }
2064
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
PATCH v4 5/8 mm/slab: use stride to access slabobj_ext
In reply to Harry YooUse a configurable stride value when accessing slab object extension metadata instead of assuming a fixed sizeof(struct slabobj_ext).
Store stride value in free bits of slab->counters field. This allows for flexibility in cases where the extension is embedded within slab objects.
Since these free bits exist only on 64-bit, any future optimizations that need to change stride value cannot be enabled on 32-bit architectures.
--- mm/slab.h | 37 +++++++++++++++++++++++++++++++++---- mm/slub.c | 2 ++ 2 files changed, 35 insertions(+), 4 deletions(-)
mm/slab.h +33 -4
@@ -55,6 +55,14 @@ struct freelist_counters {* that the slab was corrupted */ unsigned frozen:1; +#ifdef CONFIG_64BIT +/* +* Some optimizations use free bits in 'counters' field +* to save memory. In case ->stride field is not available, +* such optimizations are disabled. +*/ +unsigned short stride; +#endif }; }; };
@@ -531,6 +539,26 @@ static inline unsigned long slab_obj_exts(struct slab *slab)return obj_exts & ~OBJEXTS_FLAGS_MASK ; } +#ifdef CONFIG_64BIT +static inline void slab_set_stride (struct slab *slab,unsigned short stride) +{ + slab->stride = stride; +} +static inline unsigned short slab_get_stride (struct slab *slab) +{ +return slab->stride; +} +#else +static inline void slab_set_stride (struct slab *slab,unsigned short stride) +{ +VM_WARN_ON_ONCE (stride !=sizeof (struct slabobj_ext )); +} +static inline unsigned short slab_get_stride (struct slab *slab) +{ +return sizeof (struct slabobj_ext ); +} +#endif +/* * slab_obj_ext - get the pointer to the slab object extension metadata * associated with an object in a slab.
@@ -544,13 +572,10 @@ static inline struct slabobj_ext *slab_obj_ext(struct slab *slab,unsigned long obj_exts, unsigned int index) { -struct slabobj_ext *obj_ext; - VM_WARN_ON_ONCE(!slab_obj_exts(slab));VM_WARN_ON_ONCE (obj_exts !=slab_obj_exts (slab)); - obj_ext = (struct slabobj_ext *)obj_exts; -return &obj_ext[index]; +return (struct slabobj_ext *)(obj_exts +slab_get_stride (slab) * index); } int alloc_slab_obj_exts(struct slab *slab, struct kmem_cache *s,
@@ -569,6 +594,10 @@ static inline struct slabobj_ext *slab_obj_ext(struct slab *slab, return NULL; } +static inline void slab_set_stride (struct slab *slab,unsigned int stride) { } +static inline unsigned int slab_get_stride (struct slab *slab) {return 0 ; } + +#endif /* CONFIG_SLAB_OBJ_EXT */ static inline enum node_stat_item cache_vmstat_idx (struct kmem_cache *s)
mm/slub.c +2 -0
@@ -2147,6 +2147,8 @@ int alloc_slab_obj_exts(struct slab *slab, struct kmem_cache *s, retry: old_exts =READ_ONCE (slab->obj_exts);handle_failed_objexts_alloc (old_exts, vec, objects); +slab_set_stride (slab,sizeof (struct slabobj_ext )); +if (new_slab) {/* * If the slab is brand new and nobody can yet access its
Signature · 1 line
2.43.0
PATCH v4 6/8 mm/memcontrol,alloc_tag: handle slabobj_ext access under KASAN poison
In reply to Harry YooIn the near future, slabobj_ext may reside outside the allocated slab object range within a slab, which could be reported as an out-of-bounds access by KASAN.
As suggested by Andrey Konovalov [1], explicitly disable KASAN and KMSAN checks when accessing slabobj_ext within slab allocator, memory profiling, and memory cgroup code. While an alternative approach could be to unpoison slabobj_ext, out-of-bounds accesses outside the slab allocator are generally more common.
Move metadata_access_enable()/disable() helpers to mm/slab.h so that it can be used outside mm/slub.c. However, as suggested by Suren Baghdasaryan [2], instead of calling them directly from mm code (which is more prone to errors), change users to access slabobj_ext via get/put APIs:
- Users should call get_slab_obj_exts() to access slabobj_metadata. From now on, accessing it outside the section covered by get_slab_obj_exts() ~ put_slab_obj_exts() is illegal. This ensures that accesses to slabobj_ext metadata won't be reported as access violations.
- If get_slab_obj_exts() returns zero, the caller should not call put_slab_obj_exts(). Otherwise it must be paired with put_slab_obj_exts().
Call kasan_reset_tag() in slab_obj_ext() before returning the address to prevent SW or HW tag-based KASAN from reporting false positives.
--- mm/memcontrol.c | 12 +++++++-- mm/slab.h | 54 +++++++++++++++++++++++++++++++++++--- mm/slub.c | 69 ++++++++++++++++++++++++------------------------- 3 files changed, 95 insertions(+), 40 deletions(-)
mm/memcontrol.c +10 -2
@@ -2604,10 +2604,16 @@ struct mem_cgroup *mem_cgroup_from_obj_slab(struct slab *slab, void *p)if (!obj_exts)return NULL ; +get_slab_obj_exts (obj_exts); off =obj_to_index (slab->slab_cache, slab, p); obj_ext =slab_obj_ext (slab, obj_exts, off); -if (obj_ext->objcg) -return obj_cgroup_memcg (obj_ext->objcg); +if (obj_ext->objcg) { +struct obj_cgroup *objcg = obj_ext->objcg; + +put_slab_obj_exts (obj_exts); +return obj_cgroup_memcg (objcg); + } +put_slab_obj_exts (obj_exts);return NULL ; }
@@ -3219,10 +3225,12 @@ bool __memcg_slab_post_alloc_hook(struct kmem_cache *s, struct list_lru *lru,return false; obj_exts =slab_obj_exts (slab); +get_slab_obj_exts (obj_exts); off =obj_to_index (s, slab, p[i]); obj_ext =slab_obj_ext (slab, obj_exts, off);obj_cgroup_get (objcg); obj_ext->objcg = objcg; +put_slab_obj_exts (obj_exts); }return true;
mm/slab.h +51 -3
86 lines not on this page · raw message
mm/slub.c +34 -35
155 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v4 7/8 mm/slab: save memory by allocating slabobj_ext array from leftover
In reply to Harry YooThe leftover space in a slab is always smaller than s->size, and kmem caches for large objects that are not power-of-two sizes tend to have a greater amount of leftover space per slab. In some cases, the leftover space is larger than the size of the slabobj_ext array for the slab.
An excellent example of such a cache is ext4_inode_cache. On my system, the object size is 1144, with a preferred order of 3, 28 objects per slab, and 736 bytes of leftover space per slab.
Since the size of the slabobj_ext array is only 224 bytes (w/o mem profiling) or 448 bytes (w/ mem profiling) per slab, the entire array fits within the leftover space.
Allocate the slabobj_exts array from this unused space instead of using kcalloc() when it is large enough. The array is allocated from unused space only when creating new slabs, and it doesn't try to utilize unused space if alloc_slab_obj_exts() is called after slab creation because implementing lazy allocation involves more expensive synchronization.
The implementation and evaluation of lazy allocation from unused space is left as future-work. As pointed by Vlastimil Babka [1], it could be beneficial when a slab cache without SLAB_ACCOUNT can be created, and some of the allocations from the cache use __GFP_ACCOUNT. For example, xarray does that.
To avoid unnecessary overhead when MEMCG (with SLAB_ACCOUNT) and MEM_ALLOC_PROFILING are not used for the cache, allocate the slabobj_ext array only when either of them is enabled.
[ MEMCG=y, MEM_ALLOC_PROFILING=n ]
Before patch (creating ~2.64M directories on ext4):
Slab: 4747880 kB
SReclaimable: 4169652 kB
SUnreclaim: 578228 kB
After patch (creating ~2.64M directories on ext4):
Slab: 4724020 kB
SReclaimable: 4169188 kB
SUnreclaim: 554832 kB (-22.84 MiB)
Enjoy the memory savings!
--- mm/slub.c | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 151 insertions(+), 5 deletions(-)
mm/slub.c +151 -5
@@ -886,6 +886,99 @@ static inline unsigned long get_orig_size(struct kmem_cache *s, void *object)return *(unsigned long *)p; } +#ifdef CONFIG_SLAB_OBJ_EXT + +/* +* Check if memory cgroup or memory allocation profiling is enabled. +* If enabled, SLUB tries to reduce memory overhead of accounting +* slab objects. If neither is enabled when this function is called, +* the optimization is simply skipped to avoid affecting caches that do not +* need slabobj_ext metadata. +* +* However, this may disable optimization when memory cgroup or memory +* allocation profiling is used, but slabs are created too early +* even before those subsystems are initialized. +*/ +static inline bool need_slab_obj_exts (struct kmem_cache *s) +{ +if (memcg_kmem_online () && (s->flags &SLAB_ACCOUNT )) +return true; + +if (mem_alloc_profiling_enabled ()) +return true; + +return false; +} + +static inline unsigned int obj_exts_size_in_slab (struct slab *slab) +{ +return sizeof (struct slabobj_ext ) * slab->objects; +} + +static inline unsigned long obj_exts_offset_in_slab (struct kmem_cache *s, +struct slab *slab) +{ +unsigned long objext_offset; + + objext_offset = s->red_left_pad + s->size * slab->objects; + objext_offset =ALIGN (objext_offset,sizeof (struct slabobj_ext )); +return objext_offset; +} + +static inline bool obj_exts_fit_within_slab_leftover (struct kmem_cache *s, +struct slab *slab) +{ +unsigned long objext_offset =obj_exts_offset_in_slab (s, slab); +unsigned long objext_size =obj_exts_size_in_slab (slab); + +return objext_offset + objext_size <=slab_size (slab); +} + +static inline bool obj_exts_in_slab (struct kmem_cache *s,struct slab *slab) +{ +unsigned long expected; +unsigned long obj_exts; + + obj_exts =slab_obj_exts (slab); +if (!obj_exts) +return false; + +if (!obj_exts_fit_within_slab_leftover (s, slab)) +return false; + + expected = (unsigned long )slab_address (slab); + expected +=obj_exts_offset_in_slab (s, slab); +return obj_exts == expected; +} +#else +static inline bool need_slab_obj_exts (struct kmem_cache *s) +{ +return false; +} + +static inline unsigned int obj_exts_size_in_slab (struct slab *slab) +{ +return 0 ; +} + +static inline unsigned long obj_exts_offset_in_slab (struct kmem_cache *s, +struct slab *slab) +{ +return 0 ; +} + +static inline bool obj_exts_fit_within_slab_leftover (struct kmem_cache *s, +struct slab *slab) +{ +return false; +} + +static inline bool obj_exts_in_slab (struct kmem_cache *s,struct slab *slab) +{ +return false; +} +#endif +#ifdef CONFIG_SLUB_DEBUG /*
@@ -1405,7 +1498,15 @@ slab_pad_check(struct kmem_cache *s, struct slab *slab) start =slab_address (slab); length =slab_size (slab); end = start + length; - remainder = length % s->size; + +if (obj_exts_in_slab (s, slab)) { + remainder = length; + remainder -=obj_exts_offset_in_slab (s, slab); + remainder -=obj_exts_size_in_slab (slab); + }else { + remainder = length % s->size; + } +if (!remainder)return ;
@@ -2179,6 +2280,11 @@ static inline void free_slab_obj_exts(struct slab *slab)return ; } +if (obj_exts_in_slab (slab->slab_cache, slab)) { + slab->obj_exts =0 ; +return ; + } +/* * obj_exts was created with __GFP_NO_OBJ_EXT flag, therefore its * corresponding extension will be NULL. alloc_tag_sub() will throw a
@@ -2194,6 +2300,35 @@ static inline void free_slab_obj_exts(struct slab *slab) slab->obj_exts = 0; } +/* +* Try to allocate slabobj_ext array from unused space. +* This function must be called on a freshly allocated slab to prevent +* concurrency problems. +*/ +static void alloc_slab_obj_exts_early (struct kmem_cache *s,struct slab *slab) +{ +void *addr; +unsigned long obj_exts; + +if (!need_slab_obj_exts (s)) +return ; + +if (obj_exts_fit_within_slab_leftover (s, slab)) { + addr =slab_address (slab) +obj_exts_offset_in_slab (s, slab); + addr =kasan_reset_tag (addr); + obj_exts = (unsigned long )addr; + +get_slab_obj_exts (obj_exts); +memset (addr,0 ,obj_exts_size_in_slab (slab)); +put_slab_obj_exts (obj_exts); + +if (IS_ENABLED (CONFIG_MEMCG )) + obj_exts |=MEMCG_DATA_OBJEXTS ; + slab->obj_exts = obj_exts; +slab_set_stride (slab,sizeof (struct slabobj_ext )); + } +} +#else /* CONFIG_SLAB_OBJ_EXT */ static inline void init_slab_obj_exts (struct slab *slab)
@@ -2210,6 +2345,11 @@ static inline void free_slab_obj_exts(struct slab *slab) { } +static inline void alloc_slab_obj_exts_early (struct kmem_cache *s, +struct slab *slab) +{ +} +#endif /* CONFIG_SLAB_OBJ_EXT */ #ifdef CONFIG_MEM_ALLOC_PROFILING
@@ -3206,7 +3346,9 @@ static inline bool shuffle_freelist(struct kmem_cache *s, struct slab *slab) static __always_inline void account_slab(struct slab *slab, int order,struct kmem_cache *s,gfp_t gfp) { -if (memcg_kmem_online () && (s->flags &SLAB_ACCOUNT )) +if (memcg_kmem_online () && + (s->flags &SLAB_ACCOUNT ) && + !slab_obj_exts (slab))alloc_slab_obj_exts (slab, s, gfp, true); mod_node_page_state(slab_pgdat(slab), cache_vmstat_idx(s),
@@ -3270,9 +3412,6 @@ static struct slab *allocate_slab(struct kmem_cache *s, gfp_t flags, int node) slab->objects =oo_objects (oo); slab->inuse =0 ; slab->frozen =0 ; -init_slab_obj_exts (slab); - -account_slab (slab,oo_order (oo), s, flags); slab->slab_cache = s;
@@ -3281,6 +3420,13 @@ static struct slab *allocate_slab(struct kmem_cache *s, gfp_t flags, int node) start =slab_address (slab);setup_slab_debug (s, slab, start); +init_slab_obj_exts (slab); +/* +* Poison the slab before initializing the slabobj_ext array +* to prevent the array from being overwritten. +*/ +alloc_slab_obj_exts_early (s, slab); +account_slab (slab,oo_order (oo), s, flags); shuffle =shuffle_freelist (s, slab);
Signature · 1 line
2.43.0
kernel test robot
build botRe: [PATCH V4 7/8] mm/slab: save memory by allocating slabobj_ext array from leftover
kernel test robot noticed the following build errors:
Hi Harry,
kernel test robot noticed the following build errors:
[auto build test ERROR on akpm-mm/mm-everything]
url: https://github.com/intel-lab-lkp/linux/commits/Harry-Yoo/mm-slab-use-unsigned-long-for-orig_size-to-ensure-proper-metadata-align/20251222-191144 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything patch link: https://lore.kernel.org/r/20251222110843.980347-8-harry.yoo%40oracle.com patch subject: [PATCH V4 7/8] mm/slab: save memory by allocating slabobj_ext array from leftover config: x86_64-buildonly-randconfig-001-20251223 (https://download.01.org/0day-ci/archive/20251223/[email protected]/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251223/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
mm/slub.c:2140:33: error: passing 'union codetag_ref' to parameter of incompatible type 'union codetag_ref *'; take the address with &
2140 | if (unlikely(is_codetag_empty(ext->ref))) {
| ^~~~~~~~
| &
include/linux/compiler.h:47:41: note: expanded from macro 'unlikely'
47 | # define unlikely(x) (__branch_check__(x, 0, __builtin_constant_p(x)))
| ^
include/linux/compiler.h:32:34: note: expanded from macro '__branch_check__'
32 | ______r = __builtin_expect(!!(x), expect); \
| ^
include/linux/alloc_tag.h:52:56: note: passing argument to parameter 'ref' here
52 | static inline bool is_codetag_empty(union codetag_ref *ref)
| ^
mm/slub.c:2140:33: error: passing 'union codetag_ref' to parameter of incompatible type 'union codetag_ref *'; take the address with &
2140 | if (unlikely(is_codetag_empty(ext->ref))) {
| ^~~~~~~~
| &
include/linux/compiler.h:47:68: note: expanded from macro 'unlikely'
47 | # define unlikely(x) (__branch_check__(x, 0, __builtin_constant_p(x)))
| ^
include/linux/compiler.h:34:19: note: expanded from macro '__branch_check__'
34 | expect, is_constant); \
| ^~~~~~~~~~~
include/linux/alloc_tag.h:52:56: note: passing argument to parameter 'ref' here
52 | static inline bool is_codetag_empty(union codetag_ref *ref)
| ^
>> mm/slub.c:2326:16: error: use of undeclared identifier 'MEMCG_DATA_OBJEXTS'
2326 | obj_exts |= MEMCG_DATA_OBJEXTS;
| ^3 errors generated.
vim +/MEMCG_DATA_OBJEXTS +2326 mm/slub.c
2302
2303 /*
2304 * Try to allocate slabobj_ext array from unused space.
2305 * This function must be called on a freshly allocated slab to prevent
2306 * concurrency problems.
2307 */
2308 static void alloc_slab_obj_exts_early(struct kmem_cache *s, struct slab *slab)
2309 {
2310 void *addr;
2311 unsigned long obj_exts;
2312
2313 if (!need_slab_obj_exts(s))
2314 return;
2315
2316 if (obj_exts_fit_within_slab_leftover(s, slab)) {
2317 addr = slab_address(slab) + obj_exts_offset_in_slab(s, slab);
2318 addr = kasan_reset_tag(addr);
2319 obj_exts = (unsigned long)addr;
2320
2321 get_slab_obj_exts(obj_exts);
2322 memset(addr, 0, obj_exts_size_in_slab(slab));
2323 put_slab_obj_exts(obj_exts);
2324
2325 if (IS_ENABLED(CONFIG_MEMCG))
> 2326 obj_exts |= MEMCG_DATA_OBJEXTS;
2327 slab->obj_exts = obj_exts;
2328 slab_set_stride(slab, sizeof(struct slabobj_ext));
2329 }
2330 }
2331
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Hao Li
reviewerRe: [PATCH V4 7/8] mm/slab: save memory by allocating slabobj_ext array from leftover
In reply to Harry YooQuoted 82 lines · Harry Yoo
The leftover space in a slab is always smaller than
s->size, and kmem caches for large objects that are not power-of-two sizes tend to have a greater amount of leftover space per slab. In some cases, the leftover space is larger than the size of theslabobj_extarray for the slab.An excellent example of such a cache is
ext4_inode_cache. On my system, the object size is 1144, with a preferred order of 3, 28 objects per slab, and 736 bytes of leftover space per slab.Since the size of the
slabobj_extarray is only 224 bytes (w/o mem profiling) or 448 bytes (w/ mem profiling) per slab, the entire array fits within the leftover space.Allocate the
slabobj_extsarray from this unused space instead of usingkcalloc()when it is large enough. The array is allocated from unused space only when creating new slabs, and it doesn't try to utilize unused space ifalloc_slab_obj_exts()is called after slab creation because implementing lazy allocation involves more expensive synchronization.The implementation and evaluation of lazy allocation from unused space is left as future-work. As pointed by Vlastimil Babka [1], it could be beneficial when a slab cache without
SLAB_ACCOUNTcan be created, and some of the allocations from the cache use__GFP_ACCOUNT. For example, xarray does that.To avoid unnecessary overhead when MEMCG (with
SLAB_ACCOUNT) andMEM_ALLOC_PROFILINGare not used for the cache, allocate theslabobj_extarray only when either of them is enabled.[ MEMCG=y, MEM_ALLOC_PROFILING=n ]
Before patch (creating ~2.64M directories on ext4):
Slab: 4747880 kB
SReclaimable: 4169652 kB
SUnreclaim: 578228 kBAfter patch (creating ~2.64M directories on ext4):
Slab: 4724020 kB
SReclaimable: 4169188 kB
SUnreclaim: 554832 kB (-22.84 MiB)Enjoy the memory savings!
Signed-off-by Harry Yoo <[email protected]>--- mm/slub.c | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 151 insertions(+), 5 deletions(-)mm/slub.c +25
-0@@ -886,6 +886,99 @@ static inline unsigned long get_orig_size(struct kmem_cache *s, void *object)return *(unsigned long *)p; } +#ifdef CONFIG_SLAB_OBJ_EXT + +/* +* Check if memory cgroup or memory allocation profiling is enabled. +* If enabled, SLUB tries to reduce memory overhead of accounting +* slab objects. If neither is enabled when this function is called, +* the optimization is simply skipped to avoid affecting caches that do not +* need slabobj_ext metadata. +* +* However, this may disable optimization when memory cgroup or memory +* allocation profiling is used, but slabs are created too early +* even before those subsystems are initialized. +*/ +static inline bool need_slab_obj_exts (struct kmem_cache *s) +{ +if (memcg_kmem_online () && (s->flags &SLAB_ACCOUNT )) +return true; + +if (mem_alloc_profiling_enabled ()) +return true; + +return false; +} + +static inline unsigned int obj_exts_size_in_slab (struct slab *slab)
mm/slub.c static inline unsigned long get_orig_size(struct kmem_cache *s, void *object) · line 923
Quoted 10 lines
mm/slub.c +10
-0+{ +return sizeof (struct slabobj_ext ) * slab->objects; +} + +static inline unsigned long obj_exts_offset_in_slab (struct kmem_cache *s, +struct slab *slab) +{ +unsigned long objext_offset; + + objext_offset = s->red_left_pad + s->size * slab->objects;
Hi Harry,
As s->size already includes s->red_left_pad, do we still need s->red_left_pad here?
Quoted 176 lines
mm/slub.c +116
-5+ objext_offset =ALIGN (objext_offset,sizeof (struct slabobj_ext )); +return objext_offset; +} + +static inline bool obj_exts_fit_within_slab_leftover (struct kmem_cache *s, +struct slab *slab) +{ +unsigned long objext_offset =obj_exts_offset_in_slab (s, slab); +unsigned long objext_size =obj_exts_size_in_slab (slab); + +return objext_offset + objext_size <=slab_size (slab); +} + +static inline bool obj_exts_in_slab (struct kmem_cache *s,struct slab *slab) +{ +unsigned long expected; +unsigned long obj_exts; + + obj_exts =slab_obj_exts (slab); +if (!obj_exts) +return false; + +if (!obj_exts_fit_within_slab_leftover (s, slab)) +return false; + + expected = (unsigned long )slab_address (slab); + expected +=obj_exts_offset_in_slab (s, slab); +return obj_exts == expected; +} +#else +static inline bool need_slab_obj_exts (struct kmem_cache *s) +{ +return false; +} + +static inline unsigned int obj_exts_size_in_slab (struct slab *slab) +{ +return 0 ; +} + +static inline unsigned long obj_exts_offset_in_slab (struct kmem_cache *s, +struct slab *slab) +{ +return 0 ; +} + +static inline bool obj_exts_fit_within_slab_leftover (struct kmem_cache *s, +struct slab *slab) +{ +return false; +} + +static inline bool obj_exts_in_slab (struct kmem_cache *s,struct slab *slab) +{ +return false; +} +#endif +#ifdef CONFIG_SLUB_DEBUG /* @@ -1405,7 +1498,15 @@ slab_pad_check(struct kmem_cache *s, struct slab *slab) start =slab_address (slab); length =slab_size (slab); end = start + length; - remainder = length % s->size; + +if (obj_exts_in_slab (s, slab)) { + remainder = length; + remainder -=obj_exts_offset_in_slab (s, slab); + remainder -=obj_exts_size_in_slab (slab); + }else { + remainder = length % s->size; + } +if (!remainder)return ;@@ -2179,6 +2280,11 @@ static inline void free_slab_obj_exts(struct slab *slab)return ; } +if (obj_exts_in_slab (slab->slab_cache, slab)) { + slab->obj_exts =0 ; +return ; + } +/* * obj_exts was created with __GFP_NO_OBJ_EXT flag, therefore its * corresponding extension will be NULL. alloc_tag_sub() will throw a @@ -2194,6 +2300,35 @@ static inline void free_slab_obj_exts(struct slab *slab) slab->obj_exts = 0; } +/* +* Try to allocate slabobj_ext array from unused space. +* This function must be called on a freshly allocated slab to prevent +* concurrency problems. +*/ +static void alloc_slab_obj_exts_early (struct kmem_cache *s,struct slab *slab) +{ +void *addr; +unsigned long obj_exts; + +if (!need_slab_obj_exts (s)) +return ; + +if (obj_exts_fit_within_slab_leftover (s, slab)) { + addr =slab_address (slab) +obj_exts_offset_in_slab (s, slab); + addr =kasan_reset_tag (addr); + obj_exts = (unsigned long )addr; + +get_slab_obj_exts (obj_exts); +memset (addr,0 ,obj_exts_size_in_slab (slab)); +put_slab_obj_exts (obj_exts); + +if (IS_ENABLED (CONFIG_MEMCG )) + obj_exts |=MEMCG_DATA_OBJEXTS ; + slab->obj_exts = obj_exts; +slab_set_stride (slab,sizeof (struct slabobj_ext )); + } +} +#else /* CONFIG_SLAB_OBJ_EXT */ static inline void init_slab_obj_exts (struct slab *slab)@@ -2210,6 +2345,11 @@ static inline void free_slab_obj_exts(struct slab *slab) { } +static inline void alloc_slab_obj_exts_early (struct kmem_cache *s, +struct slab *slab) +{ +} +#endif /* CONFIG_SLAB_OBJ_EXT */ #ifdef CONFIG_MEM_ALLOC_PROFILING @@ -3206,7 +3346,9 @@ static inline bool shuffle_freelist(struct kmem_cache *s, struct slab *slab) static __always_inline void account_slab(struct slab *slab, int order,struct kmem_cache *s,gfp_t gfp) { -if (memcg_kmem_online () && (s->flags &SLAB_ACCOUNT )) +if (memcg_kmem_online () && + (s->flags &SLAB_ACCOUNT ) && + !slab_obj_exts (slab))alloc_slab_obj_exts (slab, s, gfp, true); mod_node_page_state(slab_pgdat(slab), cache_vmstat_idx(s),@@ -3270,9 +3412,6 @@ static struct slab *allocate_slab(struct kmem_cache *s, gfp_t flags, int node) slab->objects =oo_objects (oo); slab->inuse =0 ; slab->frozen =0 ; -init_slab_obj_exts (slab); - -account_slab (slab,oo_order (oo), s, flags); slab->slab_cache = s;@@ -3281,6 +3420,13 @@ static struct slab *allocate_slab(struct kmem_cache *s, gfp_t flags, int node) start =slab_address (slab);setup_slab_debug (s, slab, start); +init_slab_obj_exts (slab); +/* +* Poison the slab before initializing the slabobj_ext array +* to prevent the array from being overwritten. +*/ +alloc_slab_obj_exts_early (s, slab); +account_slab (slab,oo_order (oo), s, flags); shuffle =shuffle_freelist (s, slab);Signature · 1 line
2.43.0
Re: [PATCH V4 7/8] mm/slab: save memory by allocating slabobj_ext array from leftover
Quoted 87 lines · Hao Li
Quoted 86 lines · Harry Yoo
The leftover space in a slab is always smaller than
s->size, and kmem caches for large objects that are not power-of-two sizes tend to have a greater amount of leftover space per slab. In some cases, the leftover space is larger than the size of theslabobj_extarray for the slab.An excellent example of such a cache is
ext4_inode_cache. On my system, the object size is 1144, with a preferred order of 3, 28 objects per slab, and 736 bytes of leftover space per slab.Since the size of the
slabobj_extarray is only 224 bytes (w/o mem profiling) or 448 bytes (w/ mem profiling) per slab, the entire array fits within the leftover space.Allocate the
slabobj_extsarray from this unused space instead of usingkcalloc()when it is large enough. The array is allocated from unused space only when creating new slabs, and it doesn't try to utilize unused space ifalloc_slab_obj_exts()is called after slab creation because implementing lazy allocation involves more expensive synchronization.The implementation and evaluation of lazy allocation from unused space is left as future-work. As pointed by Vlastimil Babka [1], it could be beneficial when a slab cache without
SLAB_ACCOUNTcan be created, and some of the allocations from the cache use__GFP_ACCOUNT. For example, xarray does that.To avoid unnecessary overhead when MEMCG (with
SLAB_ACCOUNT) andMEM_ALLOC_PROFILINGare not used for the cache, allocate theslabobj_extarray only when either of them is enabled.[ MEMCG=y, MEM_ALLOC_PROFILING=n ]
Before patch (creating ~2.64M directories on ext4):
Slab: 4747880 kB
SReclaimable: 4169652 kB
SUnreclaim: 578228 kBAfter patch (creating ~2.64M directories on ext4):
Slab: 4724020 kB
SReclaimable: 4169188 kB
SUnreclaim: 554832 kB (-22.84 MiB)Enjoy the memory savings!
Signed-off-by Harry Yoo <[email protected]>--- mm/slub.c | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 151 insertions(+), 5 deletions(-)mm/slub.c +29
-0@@ -886,6 +886,99 @@ static inline unsigned long get_orig_size(struct kmem_cache *s, void *object)return *(unsigned long *)p; } +#ifdef CONFIG_SLAB_OBJ_EXT + +/* +* Check if memory cgroup or memory allocation profiling is enabled. +* If enabled, SLUB tries to reduce memory overhead of accounting +* slab objects. If neither is enabled when this function is called, +* the optimization is simply skipped to avoid affecting caches that do not +* need slabobj_ext metadata. +* +* However, this may disable optimization when memory cgroup or memory +* allocation profiling is used, but slabs are created too early +* even before those subsystems are initialized. +*/ +static inline bool need_slab_obj_exts (struct kmem_cache *s) +{ +if (memcg_kmem_online () && (s->flags &SLAB_ACCOUNT )) +return true; + +if (mem_alloc_profiling_enabled ()) +return true; + +return false; +} + +static inline unsigned int obj_exts_size_in_slab (struct slab *slab) +{ +return sizeof (struct slabobj_ext ) * slab->objects; +} +
Quoted 8 lines
mm/slub.c static inline unsigned long get_orig_size(struct kmem_cache *s, void *object) · line 923
Quoted 6 lines
mm/slub.c +6
-0+static inline unsigned long obj_exts_offset_in_slab (struct kmem_cache *s, +struct slab *slab) +{ +unsigned long objext_offset; + + objext_offset = s->red_left_pad + s->size * slab->objects;Hi Harry,
Hi Hao, thanks for the review!
Hope you're doing well.
Quoted 1 line
As
s->sizealready includess->red_left_pad
Great question. It's true that s->size includes s->red_left_pad, but we have also a redzone right before the first object:
[ redzone ] [ obj 1 | redzone ] [ obj 2| redzone ] [ ... ]
So we have (slab->objects + 1) red zones and so
Quoted 1 line
do we still need >
s->red_left_padhere?
I think this is still needed.
Signature · 2 lines
Cheers, Harry / Hyeonggon
Hao Li
reviewerRe: [PATCH V4 7/8] mm/slab: save memory by allocating slabobj_ext array from leftover
Quoted 97 lines · Harry Yoo
Quoted 87 lines · Hao Li
Quoted 86 lines · Harry Yoo
The leftover space in a slab is always smaller than
s->size, and kmem caches for large objects that are not power-of-two sizes tend to have a greater amount of leftover space per slab. In some cases, the leftover space is larger than the size of theslabobj_extarray for the slab.An excellent example of such a cache is
ext4_inode_cache. On my system, the object size is 1144, with a preferred order of 3, 28 objects per slab, and 736 bytes of leftover space per slab.Since the size of the
slabobj_extarray is only 224 bytes (w/o mem profiling) or 448 bytes (w/ mem profiling) per slab, the entire array fits within the leftover space.Allocate the
slabobj_extsarray from this unused space instead of usingkcalloc()when it is large enough. The array is allocated from unused space only when creating new slabs, and it doesn't try to utilize unused space ifalloc_slab_obj_exts()is called after slab creation because implementing lazy allocation involves more expensive synchronization.The implementation and evaluation of lazy allocation from unused space is left as future-work. As pointed by Vlastimil Babka [1], it could be beneficial when a slab cache without
SLAB_ACCOUNTcan be created, and some of the allocations from the cache use__GFP_ACCOUNT. For example, xarray does that.To avoid unnecessary overhead when MEMCG (with
SLAB_ACCOUNT) andMEM_ALLOC_PROFILINGare not used for the cache, allocate theslabobj_extarray only when either of them is enabled.[ MEMCG=y, MEM_ALLOC_PROFILING=n ]
Before patch (creating ~2.64M directories on ext4):
Slab: 4747880 kB
SReclaimable: 4169652 kB
SUnreclaim: 578228 kBAfter patch (creating ~2.64M directories on ext4):
Slab: 4724020 kB
SReclaimable: 4169188 kB
SUnreclaim: 554832 kB (-22.84 MiB)Enjoy the memory savings!
Signed-off-by Harry Yoo <[email protected]>--- mm/slub.c | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 151 insertions(+), 5 deletions(-)mm/slub.c +29
-0@@ -886,6 +886,99 @@ static inline unsigned long get_orig_size(struct kmem_cache *s, void *object)return *(unsigned long *)p; } +#ifdef CONFIG_SLAB_OBJ_EXT + +/* +* Check if memory cgroup or memory allocation profiling is enabled. +* If enabled, SLUB tries to reduce memory overhead of accounting +* slab objects. If neither is enabled when this function is called, +* the optimization is simply skipped to avoid affecting caches that do not +* need slabobj_ext metadata. +* +* However, this may disable optimization when memory cgroup or memory +* allocation profiling is used, but slabs are created too early +* even before those subsystems are initialized. +*/ +static inline bool need_slab_obj_exts (struct kmem_cache *s) +{ +if (memcg_kmem_online () && (s->flags &SLAB_ACCOUNT )) +return true; + +if (mem_alloc_profiling_enabled ()) +return true; + +return false; +} + +static inline unsigned int obj_exts_size_in_slab (struct slab *slab) +{ +return sizeof (struct slabobj_ext ) * slab->objects; +} +Quoted 8 lines
mm/slub.c static inline unsigned long get_orig_size(struct kmem_cache *s, void *object) · line 923
Quoted 6 lines
mm/slub.c +6
-0+static inline unsigned long obj_exts_offset_in_slab (struct kmem_cache *s, +struct slab *slab) +{ +unsigned long objext_offset; + + objext_offset = s->red_left_pad + s->size * slab->objects;Hi Harry,
Quoted 2 lines
Hi Hao, thanks for the review!
Hope you're doing well.
Thanks Harry. Hope you are too!
Quoted 9 lines
Quoted 2 lines
As
s->sizealready includess->red_left_padGreat question. It's true that
s->sizeincludess->red_left_pad, but we have also a redzone right before the first object:[ redzone ] [ obj 1 | redzone ] [ obj 2| redzone ] [ ... ]
So we have (
slab->objects+ 1) red zones and so
I have a follow-up question regarding the redzones. Unless I'm missing some detail, it seems the left redzone should apply to each object as well. If so, I would expect the memory layout to be:
[left redzone | obj 1 | right redzone], [left redzone | obj 2 | right redzone], [ ... ]
In calculate_sizes(), I see:
if ((flags & SLAB_RED_ZONE) && size == s->object_size)
size += sizeof(void *);
...
...
if (flags & SLAB_RED_ZONE) {
size += s->red_left_pad;
}Could you please confirm whether my understanding is correct, or point out what I'm missing?
Quoted 8 lines
Quoted 2 lines
do we still need >
s->red_left_padhere?I think this is still needed.
Signature · 2 lines
Cheers, Harry / Hyeonggon
Re: [PATCH V4 7/8] mm/slab: save memory by allocating slabobj_ext array from leftover
Quoted 117 lines · Hao Li
Quoted 97 lines · Harry Yoo
Quoted 87 lines · Hao Li
Quoted 86 lines · Harry Yoo
The leftover space in a slab is always smaller than
s->size, and kmem caches for large objects that are not power-of-two sizes tend to have a greater amount of leftover space per slab. In some cases, the leftover space is larger than the size of theslabobj_extarray for the slab.An excellent example of such a cache is
ext4_inode_cache. On my system, the object size is 1144, with a preferred order of 3, 28 objects per slab, and 736 bytes of leftover space per slab.Since the size of the
slabobj_extarray is only 224 bytes (w/o mem profiling) or 448 bytes (w/ mem profiling) per slab, the entire array fits within the leftover space.Allocate the
slabobj_extsarray from this unused space instead of usingkcalloc()when it is large enough. The array is allocated from unused space only when creating new slabs, and it doesn't try to utilize unused space ifalloc_slab_obj_exts()is called after slab creation because implementing lazy allocation involves more expensive synchronization.The implementation and evaluation of lazy allocation from unused space is left as future-work. As pointed by Vlastimil Babka [1], it could be beneficial when a slab cache without
SLAB_ACCOUNTcan be created, and some of the allocations from the cache use__GFP_ACCOUNT. For example, xarray does that.To avoid unnecessary overhead when MEMCG (with
SLAB_ACCOUNT) andMEM_ALLOC_PROFILINGare not used for the cache, allocate theslabobj_extarray only when either of them is enabled.[ MEMCG=y, MEM_ALLOC_PROFILING=n ]
Before patch (creating ~2.64M directories on ext4):
Slab: 4747880 kB
SReclaimable: 4169652 kB
SUnreclaim: 578228 kBAfter patch (creating ~2.64M directories on ext4):
Slab: 4724020 kB
SReclaimable: 4169188 kB
SUnreclaim: 554832 kB (-22.84 MiB)Enjoy the memory savings!
Signed-off-by Harry Yoo <[email protected]>--- mm/slub.c | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 151 insertions(+), 5 deletions(-)mm/slub.c +29
-0@@ -886,6 +886,99 @@ static inline unsigned long get_orig_size(struct kmem_cache *s, void *object)return *(unsigned long *)p; } +#ifdef CONFIG_SLAB_OBJ_EXT + +/* +* Check if memory cgroup or memory allocation profiling is enabled. +* If enabled, SLUB tries to reduce memory overhead of accounting +* slab objects. If neither is enabled when this function is called, +* the optimization is simply skipped to avoid affecting caches that do not +* need slabobj_ext metadata. +* +* However, this may disable optimization when memory cgroup or memory +* allocation profiling is used, but slabs are created too early +* even before those subsystems are initialized. +*/ +static inline bool need_slab_obj_exts (struct kmem_cache *s) +{ +if (memcg_kmem_online () && (s->flags &SLAB_ACCOUNT )) +return true; + +if (mem_alloc_profiling_enabled ()) +return true; + +return false; +} + +static inline unsigned int obj_exts_size_in_slab (struct slab *slab) +{ +return sizeof (struct slabobj_ext ) * slab->objects; +} +Quoted 8 lines
mm/slub.c static inline unsigned long get_orig_size(struct kmem_cache *s, void *object) · line 923
Quoted 6 lines
mm/slub.c +6
-0+static inline unsigned long obj_exts_offset_in_slab (struct kmem_cache *s, +struct slab *slab) +{ +unsigned long objext_offset; + + objext_offset = s->red_left_pad + s->size * slab->objects;Hi Harry,
Quoted 2 lines
Hi Hao, thanks for the review!
Hope you're doing well.Thanks Harry. Hope you are too!
Quoted 9 lines
Quoted 2 lines
As
s->sizealready includess->red_left_padGreat question. It's true that
s->sizeincludess->red_left_pad, but we have also a redzone right before the first object:[ redzone ] [ obj 1 | redzone ] [ obj 2| redzone ] [ ... ]
So we have (
slab->objects+ 1) red zones and soI have a follow-up question regarding the redzones. Unless I'm missing some detail, it seems the left redzone should apply to each object as well. If so, I would expect the memory layout to be:
Quoted 6 lines
[left redzone | obj 1 | right redzone], [left redzone | obj 2 | right redzone], [ ... ]
In
calculate_sizes(), I see:if ((flags &
SLAB_RED_ZONE) && size ==s->object_size)size += sizeof(void *);
Yes, this is the right redzone,
Quoted 5 lines
...
...if (flags & SLAB_RED_ZONE) { size += s->red_left_pad; }
This is the left red zone.
Both of them are included in the size...
Oh god, I was confused, thanks for the correction!
Quoted 2 lines
Could you please confirm whether my understanding is correct, or point out what I'm missing?
I think your understanding is correct.
Hmm, perhaps we should update the "Object layout:" comment above check_pad_bytes() to avoid future confusion?
Quoted 7 lines
Quoted 7 lines
Quoted 1 line
do we still need >
s->red_left_padhere?I think this is still needed.
Signature · 2 lines
Cheers, Harry / Hyeonggon
Signature · 2 lines
Cheers, Harry / Hyeonggon
Hao Li
reviewerRe: [PATCH V4 7/8] mm/slab: save memory by allocating slabobj_ext array from leftover
Quoted 133 lines · Harry Yoo
Quoted 117 lines · Hao Li
Quoted 97 lines · Harry Yoo
Quoted 87 lines · Hao Li
Quoted 86 lines · Harry Yoo
The leftover space in a slab is always smaller than
s->size, and kmem caches for large objects that are not power-of-two sizes tend to have a greater amount of leftover space per slab. In some cases, the leftover space is larger than the size of theslabobj_extarray for the slab.An excellent example of such a cache is
ext4_inode_cache. On my system, the object size is 1144, with a preferred order of 3, 28 objects per slab, and 736 bytes of leftover space per slab.Since the size of the
slabobj_extarray is only 224 bytes (w/o mem profiling) or 448 bytes (w/ mem profiling) per slab, the entire array fits within the leftover space.Allocate the
slabobj_extsarray from this unused space instead of usingkcalloc()when it is large enough. The array is allocated from unused space only when creating new slabs, and it doesn't try to utilize unused space ifalloc_slab_obj_exts()is called after slab creation because implementing lazy allocation involves more expensive synchronization.The implementation and evaluation of lazy allocation from unused space is left as future-work. As pointed by Vlastimil Babka [1], it could be beneficial when a slab cache without
SLAB_ACCOUNTcan be created, and some of the allocations from the cache use__GFP_ACCOUNT. For example, xarray does that.To avoid unnecessary overhead when MEMCG (with
SLAB_ACCOUNT) andMEM_ALLOC_PROFILINGare not used for the cache, allocate theslabobj_extarray only when either of them is enabled.[ MEMCG=y, MEM_ALLOC_PROFILING=n ]
Before patch (creating ~2.64M directories on ext4):
Slab: 4747880 kB
SReclaimable: 4169652 kB
SUnreclaim: 578228 kBAfter patch (creating ~2.64M directories on ext4):
Slab: 4724020 kB
SReclaimable: 4169188 kB
SUnreclaim: 554832 kB (-22.84 MiB)Enjoy the memory savings!
Signed-off-by Harry Yoo <[email protected]>--- mm/slub.c | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 151 insertions(+), 5 deletions(-)mm/slub.c +29
-0@@ -886,6 +886,99 @@ static inline unsigned long get_orig_size(struct kmem_cache *s, void *object)return *(unsigned long *)p; } +#ifdef CONFIG_SLAB_OBJ_EXT + +/* +* Check if memory cgroup or memory allocation profiling is enabled. +* If enabled, SLUB tries to reduce memory overhead of accounting +* slab objects. If neither is enabled when this function is called, +* the optimization is simply skipped to avoid affecting caches that do not +* need slabobj_ext metadata. +* +* However, this may disable optimization when memory cgroup or memory +* allocation profiling is used, but slabs are created too early +* even before those subsystems are initialized. +*/ +static inline bool need_slab_obj_exts (struct kmem_cache *s) +{ +if (memcg_kmem_online () && (s->flags &SLAB_ACCOUNT )) +return true; + +if (mem_alloc_profiling_enabled ()) +return true; + +return false; +} + +static inline unsigned int obj_exts_size_in_slab (struct slab *slab) +{ +return sizeof (struct slabobj_ext ) * slab->objects; +} +Quoted 8 lines
mm/slub.c static inline unsigned long get_orig_size(struct kmem_cache *s, void *object) · line 923
Quoted 6 lines
mm/slub.c +6
-0+static inline unsigned long obj_exts_offset_in_slab (struct kmem_cache *s, +struct slab *slab) +{ +unsigned long objext_offset; + + objext_offset = s->red_left_pad + s->size * slab->objects;Hi Harry,
Quoted 2 lines
Hi Hao, thanks for the review!
Hope you're doing well.Thanks Harry. Hope you are too!
Quoted 9 lines
Quoted 2 lines
As
s->sizealready includess->red_left_padGreat question. It's true that
s->sizeincludess->red_left_pad, but we have also a redzone right before the first object:[ redzone ] [ obj 1 | redzone ] [ obj 2| redzone ] [ ... ]
So we have (
slab->objects+ 1) red zones and soI have a follow-up question regarding the redzones. Unless I'm missing some detail, it seems the left redzone should apply to each object as well. If so, I would expect the memory layout to be:
Quoted 6 lines
[left redzone | obj 1 | right redzone], [left redzone | obj 2 | right redzone], [ ... ]
In
calculate_sizes(), I see:if ((flags &
SLAB_RED_ZONE) && size ==s->object_size)size += sizeof(void *);Yes, this is the right redzone,
Quoted 5 lines
...
...if (flags & SLAB_RED_ZONE) { size += s->red_left_pad; }
Quoted 4 lines
This is the left red zone.
Both of them are included in the size...Oh god, I was confused, thanks for the correction!
Glad it helped!
Quoted 8 lines
Quoted 3 lines
Could you please confirm whether my understanding is correct, or point out what I'm missing?
I think your understanding is correct.
Hmm, perhaps we should update the "Object layout:" comment above
check_pad_bytes()to avoid future confusion?
Yes, exactly. That’s a good idea. Also, I feel the layout description in the check_pad_bytes() comment isn’t very intuitive and can be a bit hard to follow. I think it might be clearer if we explicitly list out each field. What do you think about that?
Quoted 12 lines
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines
do we still need >
s->red_left_padhere?I think this is still needed.
Signature · 2 lines
Cheers, Harry / HyeonggonSignature · 2 lines
Cheers, Harry / Hyeonggon
Re: [PATCH V4 7/8] mm/slab: save memory by allocating slabobj_ext array from leftover
Quoted 146 lines · Hao Li
Quoted 133 lines · Harry Yoo
Quoted 117 lines · Hao Li
Quoted 97 lines · Harry Yoo
Quoted 87 lines · Hao Li
Quoted 86 lines · Harry Yoo
The leftover space in a slab is always smaller than
s->size, and kmem caches for large objects that are not power-of-two sizes tend to have a greater amount of leftover space per slab. In some cases, the leftover space is larger than the size of theslabobj_extarray for the slab.An excellent example of such a cache is
ext4_inode_cache. On my system, the object size is 1144, with a preferred order of 3, 28 objects per slab, and 736 bytes of leftover space per slab.Since the size of the
slabobj_extarray is only 224 bytes (w/o mem profiling) or 448 bytes (w/ mem profiling) per slab, the entire array fits within the leftover space.Allocate the
slabobj_extsarray from this unused space instead of usingkcalloc()when it is large enough. The array is allocated from unused space only when creating new slabs, and it doesn't try to utilize unused space ifalloc_slab_obj_exts()is called after slab creation because implementing lazy allocation involves more expensive synchronization.The implementation and evaluation of lazy allocation from unused space is left as future-work. As pointed by Vlastimil Babka [1], it could be beneficial when a slab cache without
SLAB_ACCOUNTcan be created, and some of the allocations from the cache use__GFP_ACCOUNT. For example, xarray does that.To avoid unnecessary overhead when MEMCG (with
SLAB_ACCOUNT) andMEM_ALLOC_PROFILINGare not used for the cache, allocate theslabobj_extarray only when either of them is enabled.[ MEMCG=y, MEM_ALLOC_PROFILING=n ]
Before patch (creating ~2.64M directories on ext4):
Slab: 4747880 kB
SReclaimable: 4169652 kB
SUnreclaim: 578228 kBAfter patch (creating ~2.64M directories on ext4):
Slab: 4724020 kB
SReclaimable: 4169188 kB
SUnreclaim: 554832 kB (-22.84 MiB)Enjoy the memory savings!
Signed-off-by Harry Yoo <[email protected]>--- mm/slub.c | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 151 insertions(+), 5 deletions(-)mm/slub.c +29
-0@@ -886,6 +886,99 @@ static inline unsigned long get_orig_size(struct kmem_cache *s, void *object)return *(unsigned long *)p; } +#ifdef CONFIG_SLAB_OBJ_EXT + +/* +* Check if memory cgroup or memory allocation profiling is enabled. +* If enabled, SLUB tries to reduce memory overhead of accounting +* slab objects. If neither is enabled when this function is called, +* the optimization is simply skipped to avoid affecting caches that do not +* need slabobj_ext metadata. +* +* However, this may disable optimization when memory cgroup or memory +* allocation profiling is used, but slabs are created too early +* even before those subsystems are initialized. +*/ +static inline bool need_slab_obj_exts (struct kmem_cache *s) +{ +if (memcg_kmem_online () && (s->flags &SLAB_ACCOUNT )) +return true; + +if (mem_alloc_profiling_enabled ()) +return true; + +return false; +} + +static inline unsigned int obj_exts_size_in_slab (struct slab *slab) +{ +return sizeof (struct slabobj_ext ) * slab->objects; +} +Quoted 8 lines
mm/slub.c static inline unsigned long get_orig_size(struct kmem_cache *s, void *object) · line 923
Quoted 6 lines
mm/slub.c +6
-0+static inline unsigned long obj_exts_offset_in_slab (struct kmem_cache *s, +struct slab *slab) +{ +unsigned long objext_offset; + + objext_offset = s->red_left_pad + s->size * slab->objects;Hi Harry,
Quoted 2 lines
Hi Hao, thanks for the review!
Hope you're doing well.Thanks Harry. Hope you are too!
Quoted 9 lines
Quoted 2 lines
As
s->sizealready includess->red_left_padGreat question. It's true that
s->sizeincludess->red_left_pad, but we have also a redzone right before the first object:[ redzone ] [ obj 1 | redzone ] [ obj 2| redzone ] [ ... ]
So we have (
slab->objects+ 1) red zones and soI have a follow-up question regarding the redzones. Unless I'm missing some detail, it seems the left redzone should apply to each object as well. If so, I would expect the memory layout to be:
Quoted 6 lines
[left redzone | obj 1 | right redzone], [left redzone | obj 2 | right redzone], [ ... ]
In
calculate_sizes(), I see:if ((flags &
SLAB_RED_ZONE) && size ==s->object_size)size += sizeof(void *);Yes, this is the right redzone,
Quoted 5 lines
...
...if (flags & SLAB_RED_ZONE) { size += s->red_left_pad; }Quoted 4 lines
This is the left red zone.
Both of them are included in the size...Oh god, I was confused, thanks for the correction!
Glad it helped!
Quoted 5 lines
Quoted 2 lines
Could you please confirm whether my understanding is correct, or point out what I'm missing?
I think your understanding is correct.
Quoted 8 lines
Quoted 2 lines
Hmm, perhaps we should update the "Object layout:" comment above
check_pad_bytes()to avoid future confusion?Yes, exactly. That’s a good idea.
Also, I feel the layout description in the
check_pad_bytes()comment isn’t very intuitive and can be a bit hard to follow. I think it might be clearer if we explicitly list out each field. What do you think about that?
Yeah it's confusing, but from your description
I'm not sure what the end result would look like.
Could you please do a patch that does it? (and also adding left redzone to the object layout comment, if you are willing to!)
As long as it makes it more understandable/intuitive, it'd be nice to have!
Signature · 2 lines
Cheers, Harry / Hyeonggon
Hao Li
reviewerRe: [PATCH V4 7/8] mm/slab: save memory by allocating slabobj_ext array from leftover
Quoted 156 lines · Harry Yoo
Quoted 146 lines · Hao Li
Quoted 133 lines · Harry Yoo
Quoted 117 lines · Hao Li
Quoted 97 lines · Harry Yoo
Quoted 87 lines · Hao Li
Quoted 86 lines · Harry Yoo
The leftover space in a slab is always smaller than
s->size, and kmem caches for large objects that are not power-of-two sizes tend to have a greater amount of leftover space per slab. In some cases, the leftover space is larger than the size of theslabobj_extarray for the slab.An excellent example of such a cache is
ext4_inode_cache. On my system, the object size is 1144, with a preferred order of 3, 28 objects per slab, and 736 bytes of leftover space per slab.Since the size of the
slabobj_extarray is only 224 bytes (w/o mem profiling) or 448 bytes (w/ mem profiling) per slab, the entire array fits within the leftover space.Allocate the
slabobj_extsarray from this unused space instead of usingkcalloc()when it is large enough. The array is allocated from unused space only when creating new slabs, and it doesn't try to utilize unused space ifalloc_slab_obj_exts()is called after slab creation because implementing lazy allocation involves more expensive synchronization.The implementation and evaluation of lazy allocation from unused space is left as future-work. As pointed by Vlastimil Babka [1], it could be beneficial when a slab cache without
SLAB_ACCOUNTcan be created, and some of the allocations from the cache use__GFP_ACCOUNT. For example, xarray does that.To avoid unnecessary overhead when MEMCG (with
SLAB_ACCOUNT) andMEM_ALLOC_PROFILINGare not used for the cache, allocate theslabobj_extarray only when either of them is enabled.[ MEMCG=y, MEM_ALLOC_PROFILING=n ]
Before patch (creating ~2.64M directories on ext4):
Slab: 4747880 kB
SReclaimable: 4169652 kB
SUnreclaim: 578228 kBAfter patch (creating ~2.64M directories on ext4):
Slab: 4724020 kB
SReclaimable: 4169188 kB
SUnreclaim: 554832 kB (-22.84 MiB)Enjoy the memory savings!
Signed-off-by Harry Yoo <[email protected]>--- mm/slub.c | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 151 insertions(+), 5 deletions(-)mm/slub.c +29
-0@@ -886,6 +886,99 @@ static inline unsigned long get_orig_size(struct kmem_cache *s, void *object)return *(unsigned long *)p; } +#ifdef CONFIG_SLAB_OBJ_EXT + +/* +* Check if memory cgroup or memory allocation profiling is enabled. +* If enabled, SLUB tries to reduce memory overhead of accounting +* slab objects. If neither is enabled when this function is called, +* the optimization is simply skipped to avoid affecting caches that do not +* need slabobj_ext metadata. +* +* However, this may disable optimization when memory cgroup or memory +* allocation profiling is used, but slabs are created too early +* even before those subsystems are initialized. +*/ +static inline bool need_slab_obj_exts (struct kmem_cache *s) +{ +if (memcg_kmem_online () && (s->flags &SLAB_ACCOUNT )) +return true; + +if (mem_alloc_profiling_enabled ()) +return true; + +return false; +} + +static inline unsigned int obj_exts_size_in_slab (struct slab *slab) +{ +return sizeof (struct slabobj_ext ) * slab->objects; +} +Quoted 8 lines
mm/slub.c static inline unsigned long get_orig_size(struct kmem_cache *s, void *object) · line 923
Quoted 6 lines
mm/slub.c +6
-0+static inline unsigned long obj_exts_offset_in_slab (struct kmem_cache *s, +struct slab *slab) +{ +unsigned long objext_offset; + + objext_offset = s->red_left_pad + s->size * slab->objects;Hi Harry,
Quoted 2 lines
Hi Hao, thanks for the review!
Hope you're doing well.Thanks Harry. Hope you are too!
Quoted 9 lines
Quoted 2 lines
As
s->sizealready includess->red_left_padGreat question. It's true that
s->sizeincludess->red_left_pad, but we have also a redzone right before the first object:[ redzone ] [ obj 1 | redzone ] [ obj 2| redzone ] [ ... ]
So we have (
slab->objects+ 1) red zones and soI have a follow-up question regarding the redzones. Unless I'm missing some detail, it seems the left redzone should apply to each object as well. If so, I would expect the memory layout to be:
Quoted 6 lines
[left redzone | obj 1 | right redzone], [left redzone | obj 2 | right redzone], [ ... ]
In
calculate_sizes(), I see:if ((flags &
SLAB_RED_ZONE) && size ==s->object_size)size += sizeof(void *);Yes, this is the right redzone,
Quoted 5 lines
...
...if (flags & SLAB_RED_ZONE) { size += s->red_left_pad; }Quoted 4 lines
This is the left red zone.
Both of them are included in the size...Oh god, I was confused, thanks for the correction!
Glad it helped!
Quoted 5 lines
Quoted 2 lines
Could you please confirm whether my understanding is correct, or point out what I'm missing?
I think your understanding is correct.
Quoted 8 lines
Quoted 2 lines
Hmm, perhaps we should update the "Object layout:" comment above
check_pad_bytes()to avoid future confusion?Yes, exactly. That’s a good idea.
Also, I feel the layout description in the
check_pad_bytes()comment isn’t very intuitive and can be a bit hard to follow. I think it might be clearer if we explicitly list out each field. What do you think about that?
Quoted 5 lines
Yeah it's confusing, but from your description
I'm not sure what the end result would look like.Could you please do a patch that does it? (and also adding left redzone to the object layout comment, if you are willing to!)
Sure — I'd be happy to!
Quoted 3 lines
As long as it makes it more understandable/intuitive, it'd be nice to have!
I'll send a patch for review soon.
Signature · 2 lines
Thanks, Hao
Quoted 4 lines
Signature · 3 lines
-- Cheers, Harry / Hyeonggon
Hao Li
reviewerPATCH 1/1 slub: clarify object field layout comments
In reply to Harry YooThe comments above check_pad_bytes() document the field layout of a single object. Rewrite them to improve clarity and precision.
Also update an outdated comment in calculate_sizes().
---
Hi Harry, this patch adds more detailed object layout documentation. Let me know if you have any comments.
mm/slub.c | 92 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 53 insertions(+), 39 deletions(-)
mm/slub.c +53 -39
@@ -1211,44 +1211,58 @@ check_bytes_and_report(struct kmem_cache *s, struct slab *slab, }/* -* Object layout: -* -* object address -* Bytes of the object to be managed. -* If the freepointer may overlay the object then the free -* pointer is at the middle of the object. -* -* Poisoning uses 0x6b (POISON_FREE) and the last byte is -* 0xa5 (POISON_END) -* -* object + s->object_size -* Padding to reach word boundary. This is also used for Redzoning. -* Padding is extended by another word if Redzoning is enabled and -* object_size == inuse. -* -* We fill with 0xbb (SLUB_RED_INACTIVE) for inactive objects and with -* 0xcc (SLUB_RED_ACTIVE) for objects in use. -* -* object + s->inuse -* Meta data starts here. -* -* A. Free pointer (if we cannot overwrite object on free) -* B. Tracking data for SLAB_STORE_USER -* C. Original request size for kmalloc object (SLAB_STORE_USER enabled) -* D. Padding to reach required alignment boundary or at minimum -* one word if debugging is on to be able to detect writes -* before the word boundary. -* -* Padding is done using 0x5a (POISON_INUSE) -* -* object + s->size -* Nothing is used beyond s->size. -* -* If slabcaches are merged then the object_size and inuse boundaries are mostly -* ignored. And therefore no slab options that rely on these boundaries +* Object field layout: +* +* [Left redzone padding] (if SLAB_RED_ZONE) +* - Field size: s->red_left_pad +* - Filled with 0xbb (SLUB_RED_INACTIVE) for inactive objects and +* 0xcc (SLUB_RED_ACTIVE) for objects in use when SLAB_RED_ZONE. +* +* [Object bytes] +* - Field size: s->object_size +* - Object payload bytes. +* - If the freepointer may overlap the object, it is stored inside +* the object (typically near the middle). +* - Poisoning uses 0x6b (POISON_FREE) and the last byte is +* 0xa5 (POISON_END) when __OBJECT_POISON is enabled. +* +* [Word-align padding] (right redzone when SLAB_RED_ZONE is set) +* - Field size: s->inuse - s->object_size +* - If redzoning is enabled and ALIGN(size, sizeof(void *)) adds no +* padding, explicitly extend by one word so the right redzone is +* non-empty. +* - Filled with 0xbb (SLUB_RED_INACTIVE) for inactive objects and +* 0xcc (SLUB_RED_ACTIVE) for objects in use when SLAB_RED_ZONE. +* +* [Metadata starts at object + s->inuse] +* - A. freelist pointer (if freeptr_outside_object) +* - B. alloc tracking (SLAB_STORE_USER) +* - C. free tracking (SLAB_STORE_USER) +* - D. original request size (SLAB_KMALLOC && SLAB_STORE_USER) +* - E. KASAN metadata (if enabled) +* +* [Mandatory padding] (if CONFIG_SLUB_DEBUG && SLAB_RED_ZONE) +* - One mandatory debug word to guarantee a minimum poisoned gap +* between metadata and the next object, independent of alignment. +* - Filled with 0x5a (POISON_INUSE) when SLAB_POISON is set. +* [Final alignment padding] +* - Any bytes added by ALIGN(size, s->align) to reach s->size. +* - Filled with 0x5a (POISON_INUSE) when SLAB_POISON is set. +* +* Notes: +* - Redzones are filled by init_object() with SLUB_RED_ACTIVE/INACTIVE. +* - Object contents are poisoned with POISON_FREE/END when __OBJECT_POISON. +* - The trailing padding is pre-filled with POISON_INUSE by +* setup_slab_debug() when SLAB_POISON is set, and is validated by +* check_pad_bytes(). +* - The first object pointer is slab_address(slab) + +* (s->red_left_pad if redzoning); subsequent objects are reached by +* adding s->size each time. +* +* If slabcaches are merged then the object_size and inuse boundaries are +* mostly ignored. Therefore no slab options that rely on these boundaries * may be used with merged slabcaches. */ -static int check_pad_bytes (struct kmem_cache *s,struct slab *slab,u8 *p) {unsigned long off =get_info_end (s);/* The end of info */
@@ -7103,9 +7117,9 @@ static int calculate_sizes(struct kmem_cache_args *args, struct kmem_cache *s)/* -* If we are Redzoning then check if there is some space between the -* end of the object and the free pointer. If not then add an -* additional word to have some bytes to store Redzone information. +* If we are Redzoning and there is no space between the end of the +* object and the following fields, add one word so the right Redzone +* is non-empty. */ if ((flags &SLAB_RED_ZONE ) && size == s->object_size) size +=sizeof (void *);
Signature · 1 line
2.50.1
Re: [PATCH] slub: clarify object field layout comments
Quoted 10 lines · Hao Li
The comments above
check_pad_bytes()document the field layout of a single object. Rewrite them to improve clarity and precision.Also update an outdated comment in
calculate_sizes().Suggested-by Harry Yoo <[email protected]>Signed-off-by Hao Li <[email protected]>---
Hi Harry, this patch adds more detailed object layout documentation. Let me know if you have any comments.
Hi Hao, thanks for improving it!
It looks much clearer now.
few nits below.
Quoted 6 lines
+ * Object field layout:
+ *
+ * [Left redzone padding] (ifSLAB_RED_ZONE)
+ * - Field size:s->red_left_pad
+ * - Filled with0xbb(SLUB_RED_INACTIVE) for inactive objects and
+ *0xcc(SLUB_RED_ACTIVE) for objects in use whenSLAB_RED_ZONE.
nit: although it becomes clear after reading the Notes: section, I would like to make it clear that object address starts here (after the left redzone) and the left redzone is right before each object.
Quoted 37 lines
+ * [Object bytes]
+ * - Field size: s->object_size + * - Object payload bytes. + * - If the freepointer may overlap the object, it is stored inside+ * the object (typically near the middle).
+ * - Poisoning uses0x6b(POISON_FREE) and the last byte is
+ *0xa5(POISON_END) when__OBJECT_POISONis enabled.
+ *
+ * [Word-align padding] (right redzone whenSLAB_RED_ZONEis set)
+ * - Field size:s->inuse-s->object_size
+ * - If redzoning is enabled andALIGN(size, sizeof(void *))adds no
+ * padding, explicitly extend by one word so the right redzone is
+ * non-empty.
+ * - Filled with0xbb(SLUB_RED_INACTIVE) for inactive objects and
+ *0xcc(SLUB_RED_ACTIVE) for objects in use whenSLAB_RED_ZONE.
+ *
+ * [Metadata starts at object +s->inuse]+ * - A. freelist pointer (if freeptr_outside_object) + * - B. alloc tracking (SLAB_STORE_USER) + * - C. free tracking (SLAB_STORE_USER) + * - D. original request size (SLAB_KMALLOC && SLAB_STORE_USER) + * - E. KASAN metadata (if enabled)+ *
+ * [Mandatory padding] (ifCONFIG_SLUB_DEBUG&&SLAB_RED_ZONE)
+ * - One mandatory debug word to guarantee a minimum poisoned gap
+ * between metadata and the next object, independent of alignment.
+ * - Filled with0x5a(POISON_INUSE) whenSLAB_POISONis set.+ * [Final alignment padding]
+ * - Any bytes added byALIGN(size, s->align)to reachs->size.
+ * - Filled with0x5a(POISON_INUSE) whenSLAB_POISONis set.
+ *
+ * Notes:
+ * - Redzones are filled byinit_object()with SLUB_RED_ACTIVE/INACTIVE.
+ * - Object contents are poisoned with POISON_FREE/END when__OBJECT_POISON.
+ * - The trailing padding is pre-filled withPOISON_INUSEby
+ *setup_slab_debug()whenSLAB_POISONis set, and is validated by
Quoted 8 lines
+ *
check_pad_bytes().
+ * - The first object pointer isslab_address(slab)+
+ * (s->red_left_padif redzoning); subsequent objects are reached by
+ * addings->sizeeach time.
+ *
+ * If slabcaches are merged then theobject_sizeand inuse boundaries are
+ * mostly ignored. Therefore no slab options that rely on these boundaries
* may be used with merged slabcaches.
For the last paragraph, perhaps it'll be clearer to say:
"If a slab cache flag relies on specific metadata to exist at a fixed
offset, the flag must be included in SLAB_NEVER_MERGE to prevent merging. Otherwise, the cache would misbehave as s->object_size and s->inuse are adjusted during cache merging"
Otherwise looks great to me, so please feel free to add:
Signature · 2 lines
Cheers, Harry / Hyeonggon
Hao Li
reviewerRe: [PATCH] slub: clarify object field layout comments
Quoted 6 lines · Harry Yoo
Quoted 5 lines · Hao Li
The comments above
check_pad_bytes()document the field layout of a single object. Rewrite them to improve clarity and precision.Also update an outdated comment in
calculate_sizes().
Quoted 8 lines
Quoted 5 lines
Suggested-by Harry Yoo <[email protected]>Signed-off-by Hao Li <[email protected]>---
Hi Harry, this patch adds more detailed object layout documentation. Let me know if you have any comments.Hi Hao, thanks for improving it!
It looks much clearer now.
Hi Harry,
Thanks for the review and the Acked-by!
Quoted 10 lines
few nits below.
Quoted 6 lines
+ * Object field layout:
+ *
+ * [Left redzone padding] (ifSLAB_RED_ZONE)
+ * - Field size:s->red_left_pad
+ * - Filled with0xbb(SLUB_RED_INACTIVE) for inactive objects and
+ *0xcc(SLUB_RED_ACTIVE) for objects in use whenSLAB_RED_ZONE.
Quoted 3 lines
nit: although it becomes clear after reading the Notes: section, I would like to make it clear that object address starts here (after the left redzone) and the left redzone is right before each object.
Good point. I’ll make this explicit in v2.
Quoted 47 lines
Quoted 38 lines
+ * [Object bytes]
+ * - Field size: s->object_size + * - Object payload bytes. + * - If the freepointer may overlap the object, it is stored inside+ * the object (typically near the middle).
+ * - Poisoning uses0x6b(POISON_FREE) and the last byte is
+ *0xa5(POISON_END) when__OBJECT_POISONis enabled.
+ *
+ * [Word-align padding] (right redzone whenSLAB_RED_ZONEis set)
+ * - Field size:s->inuse-s->object_size
+ * - If redzoning is enabled andALIGN(size, sizeof(void *))adds no
+ * padding, explicitly extend by one word so the right redzone is
+ * non-empty.
+ * - Filled with0xbb(SLUB_RED_INACTIVE) for inactive objects and
+ *0xcc(SLUB_RED_ACTIVE) for objects in use whenSLAB_RED_ZONE.
+ *
+ * [Metadata starts at object +s->inuse]+ * - A. freelist pointer (if freeptr_outside_object) + * - B. alloc tracking (SLAB_STORE_USER) + * - C. free tracking (SLAB_STORE_USER) + * - D. original request size (SLAB_KMALLOC && SLAB_STORE_USER) + * - E. KASAN metadata (if enabled)+ *
+ * [Mandatory padding] (ifCONFIG_SLUB_DEBUG&&SLAB_RED_ZONE)
+ * - One mandatory debug word to guarantee a minimum poisoned gap
+ * between metadata and the next object, independent of alignment.
+ * - Filled with0x5a(POISON_INUSE) whenSLAB_POISONis set.+ * [Final alignment padding]
+ * - Any bytes added byALIGN(size, s->align)to reachs->size.
+ * - Filled with0x5a(POISON_INUSE) whenSLAB_POISONis set.
+ *
+ * Notes:
+ * - Redzones are filled byinit_object()with SLUB_RED_ACTIVE/INACTIVE.
+ * - Object contents are poisoned with POISON_FREE/END when__OBJECT_POISON.
+ * - The trailing padding is pre-filled withPOISON_INUSEby
+ *setup_slab_debug()whenSLAB_POISONis set, and is validated byQuoted 8 lines
+ *
check_pad_bytes().
+ * - The first object pointer isslab_address(slab)+
+ * (s->red_left_padif redzoning); subsequent objects are reached by
+ * addings->sizeeach time.
+ *
+ * If slabcaches are merged then theobject_sizeand inuse boundaries are
+ * mostly ignored. Therefore no slab options that rely on these boundaries
* may be used with merged slabcaches.
Quoted 6 lines
For the last paragraph, perhaps it'll be clearer to say:
"If a slab cache flag relies on specific metadata to exist at a fixed
offset, the flag must be included inSLAB_NEVER_MERGEto prevent merging. Otherwise, the cache would misbehave ass->object_sizeands->inuseare adjusted during cache merging"
Agreed. I’ll reword that paragraph along your suggestion to emphasize the fixed-offset metadata requirement.
Quoted 3 lines
Otherwise looks great to me, so please feel free to add:
Acked-by Harry Yoo <[email protected]>
I'll include this Acked-by in v2. Thanks!
Signature · 2 lines
Thanks Hao
Quoted 4 lines
Signature · 3 lines
-- Cheers, Harry / Hyeonggon
PATCH v4 8/8 mm/slab: place slabobj_ext metadata in unused space within s->size
In reply to Harry YooWhen a cache has high s->align value and s->object_size is not aligned to it, each object ends up with some unused space because of alignment. If this wasted space is big enough, we can use it to store the slabobj_ext metadata instead of wasting it.
On my system, this happens with caches like kmem_cache, mm_struct, pid, task_struct, sighand_cache, xfs_inode, and others.
To place the slabobj_ext metadata within each object, the existing slab_obj_ext() logic can still be used by setting:
- slab->obj_exts = slab_address(slab) + s->red_left_zone + (slabobj_ext offset)
- stride = s->size
slab_obj_ext() doesn't need know where the metadata is stored, so this method works without adding extra overhead to slab_obj_ext().
A good example benefiting from this optimization is xfs_inode (object_size: 992, align: 64). To measure memory savings, 2 millions of files were created on XFS.
[ MEMCG=y, MEM_ALLOC_PROFILING=n ]
Before patch (creating ~2.64M directories on xfs):
Slab: 5175976 kB SReclaimable: 3837524 kB SUnreclaim: 1338452 kB
After patch (creating ~2.64M directories on xfs):
Slab: 5152912 kB SReclaimable: 3838568 kB SUnreclaim: 1314344 kB (-23.54 MiB)
Enjoy the memory savings!
--- include/linux/slab.h | 9 ++++++ mm/slab_common.c | 6 ++-- mm/slub.c | 73 ++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 83 insertions(+), 5 deletions(-)
include/linux/slab.h +9 -0
@@ -59,6 +59,9 @@ enum _slab_flag_bits { _SLAB_CMPXCHG_DOUBLE,#ifdef CONFIG_SLAB_OBJ_EXT _SLAB_NO_OBJ_EXT, +#endif +#if defined(CONFIG_SLAB_OBJ_EXT ) && defined(CONFIG_64BIT ) + _SLAB_OBJ_EXT_IN_OBJ, #endif _SLAB_FLAGS_LAST_BIT };
@@ -244,6 +247,12 @@ enum _slab_flag_bits {#define SLAB_NO_OBJ_EXT __SLAB_FLAG_UNUSED#endif +#if defined(CONFIG_SLAB_OBJ_EXT ) && defined(CONFIG_64BIT ) +#define SLAB_OBJ_EXT_IN_OBJ __SLAB_FLAG_BIT(_SLAB_OBJ_EXT_IN_OBJ) +#else +#define SLAB_OBJ_EXT_IN_OBJ __SLAB_FLAG_UNUSED +#endif +/* * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests. *
mm/slab_common.c +4 -2
@@ -43,11 +43,13 @@ DEFINE_MUTEX(slab_mutex);struct kmem_cache *kmem_cache;/* -* Set of flags that will prevent slab merging +* Set of flags that will prevent slab merging. +* Any flag that adds per-object metadata should be included, +* since slab merging can update s->inuse that affects the metadata layout. */ #define SLAB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \ SLAB_TRACE | SLAB_TYPESAFE_BY_RCU | SLAB_NOLEAKTRACE | \ - SLAB_FAILSLAB | SLAB_NO_MERGE) + SLAB_FAILSLAB | SLAB_NO_MERGE | SLAB_OBJ_EXT_IN_OBJ)#define SLAB_MERGE_SAME (SLAB_RECLAIM_ACCOUNT | SLAB_CACHE_DMA | \ SLAB_CACHE_DMA32 | SLAB_ACCOUNT)
mm/slub.c +70 -3
121 lines not on this page · raw message
Signature · 1 line
2.43.0
Hao Li
reviewerRe: [PATCH V4 8/8] mm/slab: place slabobj_ext metadata in unused space within s->size
Quoted 178 lines · Harry Yoo
When a cache has high
s->alignvalue ands->object_sizeis not aligned to it, each object ends up with some unused space because of alignment. If this wasted space is big enough, we can use it to store theslabobj_extmetadata instead of wasting it.On my system, this happens with caches like
kmem_cache,mm_struct, pid,task_struct,sighand_cache,xfs_inode, and others.To place the
slabobj_extmetadata within each object, the existingslab_obj_ext()logic can still be used by setting:-
slab->obj_exts=slab_address(slab)+s->red_left_zone+ (slabobj_extoffset)
- stride =s->size
slab_obj_ext()doesn't need know where the metadata is stored, so this method works without adding extra overhead toslab_obj_ext().A good example benefiting from this optimization is
xfs_inode(object_size: 992, align: 64). To measure memory savings, 2 millions of files were created on XFS.[ MEMCG=y, MEM_ALLOC_PROFILING=n ]
Before patch (creating ~2.64M directories on xfs):
Slab: 5175976 kB SReclaimable: 3837524 kB SUnreclaim: 1338452 kBAfter patch (creating ~2.64M directories on xfs):
Slab: 5152912 kB SReclaimable: 3838568 kB SUnreclaim: 1314344 kB (-23.54 MiB)Enjoy the memory savings!
Suggested-by Vlastimil Babka <[email protected]>Signed-off-by Harry Yoo <[email protected]>--- include/linux/slab.h | 9 ++++++ mm/slab_common.c | 6 ++-- mm/slub.c | 73 ++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 83 insertions(+), 5 deletions(-)include/linux/slab.h +9
-0@@ -59,6 +59,9 @@ enum _slab_flag_bits { _SLAB_CMPXCHG_DOUBLE,#ifdef CONFIG_SLAB_OBJ_EXT _SLAB_NO_OBJ_EXT, +#endif +#if defined(CONFIG_SLAB_OBJ_EXT ) && defined(CONFIG_64BIT ) + _SLAB_OBJ_EXT_IN_OBJ, #endif _SLAB_FLAGS_LAST_BIT };@@ -244,6 +247,12 @@ enum _slab_flag_bits {#define SLAB_NO_OBJ_EXT __SLAB_FLAG_UNUSED#endif +#if defined(CONFIG_SLAB_OBJ_EXT ) && defined(CONFIG_64BIT ) +#define SLAB_OBJ_EXT_IN_OBJ __SLAB_FLAG_BIT(_SLAB_OBJ_EXT_IN_OBJ) +#else +#define SLAB_OBJ_EXT_IN_OBJ __SLAB_FLAG_UNUSED +#endif +/* * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests. * mm/slab_common.c +4
-2@@ -43,11 +43,13 @@ DEFINE_MUTEX(slab_mutex);struct kmem_cache *kmem_cache;/* -* Set of flags that will prevent slab merging +* Set of flags that will prevent slab merging. +* Any flag that adds per-object metadata should be included, +* since slab merging can update s->inuse that affects the metadata layout. */ #define SLAB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \ SLAB_TRACE | SLAB_TYPESAFE_BY_RCU | SLAB_NOLEAKTRACE | \ - SLAB_FAILSLAB | SLAB_NO_MERGE) + SLAB_FAILSLAB | SLAB_NO_MERGE | SLAB_OBJ_EXT_IN_OBJ)#define SLAB_MERGE_SAME (SLAB_RECLAIM_ACCOUNT | SLAB_CACHE_DMA | \ SLAB_CACHE_DMA32 | SLAB_ACCOUNT)mm/slub.c +45
-2@@ -977,6 +977,39 @@ static inline bool obj_exts_in_slab(struct kmem_cache *s, struct slab *slab) {return false; } + +#endif + +#if defined(CONFIG_SLAB_OBJ_EXT ) && defined(CONFIG_64BIT ) +static bool obj_exts_in_object (struct kmem_cache *s) +{ +return s->flags &SLAB_OBJ_EXT_IN_OBJ ; +} + +static unsigned int obj_exts_offset_in_object (struct kmem_cache *s) +{ +unsigned int offset =get_info_end (s); + +if (kmem_cache_debug_flags (s,SLAB_STORE_USER )) + offset +=sizeof (struct track ) *2 ; + +if (slub_debug_orig_size (s)) + offset +=sizeof (unsigned long ); + + offset +=kasan_metadata_size (s, false); + +return offset; +} +#else +static inline bool obj_exts_in_object (struct kmem_cache *s) +{ +return false; +} + +static inline unsigned int obj_exts_offset_in_object (struct kmem_cache *s) +{ +return 0 ; +}#endif #ifdef CONFIG_SLUB_DEBUG @@ -1277,6 +1310,9 @@ static void print_trailer(struct kmem_cache *s, struct slab *slab, u8 *p) off +=kasan_metadata_size (s, false); +if (obj_exts_in_object (s)) + off +=sizeof (struct slabobj_ext ); +if (off !=size_from_object (s))/* Beginning of the filler is the free pointer */ print_section(KERN_ERR, "Padding ", p + off,@@ -1446,7 +1482,10 @@ check_bytes_and_report(struct kmem_cache *s, struct slab *slab,* A. Free pointer (if we cannot overwrite object on free) * B. Tracking data for SLAB_STORE_USER * C. Original request size for kmalloc object (SLAB_STORE_USER enabled) -* D. Padding to reach required alignment boundary or at minimum +* D. KASAN alloc metadata (KASAN enabled) +* E. struct slabobj_ext to store accounting metadata +* (SLAB_OBJ_EXT_IN_OBJ enabled) +* F. Padding to reach required alignment boundary or at minimum * one word if debugging is on to be able to detect writes * before the word boundary. * @@ -1474,6 +1513,9 @@ static int check_pad_bytes(struct kmem_cache *s, struct slab *slab, u8 *p) off +=kasan_metadata_size (s, false); +if (obj_exts_in_object (s)) + off +=sizeof (struct slabobj_ext ); +if (size_from_object (s) == off)return 1 ;@@ -2280,7 +2322,8 @@ static inline void free_slab_obj_exts(struct slab *slab)return ; } -if (obj_exts_in_slab (slab->slab_cache, slab)) { +if (obj_exts_in_slab (slab->slab_cache, slab) || +obj_exts_in_object (slab->slab_cache)) { slab->obj_exts =0 ;return ; }
mm/slub.c static void alloc_slab_obj_exts_early(struct kmem_cache *s, struct slab *slab) · line 2377
Quoted 9 lines
mm/slub.c +6
-0@@ -2326,6 +2369,23 @@ static void alloc_slab_obj_exts_early(struct kmem_cache *s, struct slab *slab) obj_exts |=MEMCG_DATA_OBJEXTS ; slab->obj_exts = obj_exts;slab_set_stride (slab,sizeof (struct slabobj_ext )); + }else if (obj_exts_in_object (s)) { +unsigned int offset =obj_exts_offset_in_object (s); + + obj_exts = (unsigned long )slab_address (slab); + obj_exts += s->red_left_pad; + obj_exts +=obj_exts_offset_in_object (s);
Hi, Harry
It looks like this could just be simplified to obj_exts += offset, right?
Quoted 24 lines
mm/slub.c +12
-0+ +get_slab_obj_exts (obj_exts); +for_each_object (addr, s,slab_address (slab), slab->objects) +memset (kasan_reset_tag (addr) + offset,0 , +sizeof (struct slabobj_ext )); +put_slab_obj_exts (obj_exts); + +if (IS_ENABLED (CONFIG_MEMCG )) + obj_exts |=MEMCG_DATA_OBJEXTS ; + slab->obj_exts = obj_exts; +slab_set_stride (slab, s->size); } }@@ -8023,6 +8083,7 @@ static int calculate_sizes(struct kmem_cache_args *args, struct kmem_cache *s) {slab_flags_t flags = s->flags;unsigned int size = s->object_size; +unsigned int aligned_size;unsigned int order;/* @@ -8132,7 +8193,13 @@ static int calculate_sizes(struct kmem_cache_args *args, struct kmem_cache *s)* offset 0. In order to align the objects we have to simply size
mm/slub.c static int calculate_sizes(struct kmem_cache_args *args, struct kmem_cache *s) · line 8202
Quoted 10 lines
mm/slub.c +7
-1* each object to conform to the alignment. */ - size =ALIGN (size, s->align); + aligned_size =ALIGN (size, s->align); +#if defined(CONFIG_SLAB_OBJ_EXT ) && defined(CONFIG_64BIT ) +if (aligned_size - size >=sizeof (struct slabobj_ext )) + s->flags |=SLAB_OBJ_EXT_IN_OBJ ; +#endif + size = aligned_size; +
One more thought: in calculate_sizes() we add some extra padding when SLAB_RED_ZONE is enabled:
if (flags & SLAB_RED_ZONE) {
/*
* Add some empty padding so that we can catch
* overwrites from earlier objects rather than let
* tracking information or the free pointer be
* corrupted if a user writes before the start
* of the object.
*/
size += sizeof(void *);
...
}
From what I understand, this additional padding ends up being placed after the KASAN allocation metadata.
Since it’s only "extra" padding (i.e., it doesn’t seem strictly required for the layout), and your patch would reuse this area — together with the final padding introduced by size = ALIGN(size, s->align); — for objext, it seems like this padding may no longer provide much benefit.
Do you think it would make sense to remove this extra padding altogether?
Signature · 2 lines
Thanks, Hao
Quoted 6 lines
mm/slub.c +0
-0s->size = size; s->reciprocal_size =reciprocal_value (size); order =calculate_order (size);Signature · 1 line
2.43.0
Re: [PATCH V4 8/8] mm/slab: place slabobj_ext metadata in unused space within s->size
Quoted 191 lines · Hao Li
Quoted 178 lines · Harry Yoo
When a cache has high
s->alignvalue ands->object_sizeis not aligned to it, each object ends up with some unused space because of alignment. If this wasted space is big enough, we can use it to store theslabobj_extmetadata instead of wasting it.On my system, this happens with caches like
kmem_cache,mm_struct, pid,task_struct,sighand_cache,xfs_inode, and others.To place the
slabobj_extmetadata within each object, the existingslab_obj_ext()logic can still be used by setting:-
slab->obj_exts=slab_address(slab)+s->red_left_zone+ (slabobj_extoffset)
- stride =s->size
slab_obj_ext()doesn't need know where the metadata is stored, so this method works without adding extra overhead toslab_obj_ext().A good example benefiting from this optimization is
xfs_inode(object_size: 992, align: 64). To measure memory savings, 2 millions of files were created on XFS.[ MEMCG=y, MEM_ALLOC_PROFILING=n ]
Before patch (creating ~2.64M directories on xfs):
Slab: 5175976 kB SReclaimable: 3837524 kB SUnreclaim: 1338452 kBAfter patch (creating ~2.64M directories on xfs):
Slab: 5152912 kB SReclaimable: 3838568 kB SUnreclaim: 1314344 kB (-23.54 MiB)Enjoy the memory savings!
Suggested-by Vlastimil Babka <[email protected]>Signed-off-by Harry Yoo <[email protected]>--- include/linux/slab.h | 9 ++++++ mm/slab_common.c | 6 ++-- mm/slub.c | 73 ++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 83 insertions(+), 5 deletions(-)include/linux/slab.h +9
-0@@ -59,6 +59,9 @@ enum _slab_flag_bits { _SLAB_CMPXCHG_DOUBLE,#ifdef CONFIG_SLAB_OBJ_EXT _SLAB_NO_OBJ_EXT, +#endif +#if defined(CONFIG_SLAB_OBJ_EXT ) && defined(CONFIG_64BIT ) + _SLAB_OBJ_EXT_IN_OBJ, #endif _SLAB_FLAGS_LAST_BIT };@@ -244,6 +247,12 @@ enum _slab_flag_bits {#define SLAB_NO_OBJ_EXT __SLAB_FLAG_UNUSED#endif +#if defined(CONFIG_SLAB_OBJ_EXT ) && defined(CONFIG_64BIT ) +#define SLAB_OBJ_EXT_IN_OBJ __SLAB_FLAG_BIT(_SLAB_OBJ_EXT_IN_OBJ) +#else +#define SLAB_OBJ_EXT_IN_OBJ __SLAB_FLAG_UNUSED +#endif +/* * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests. * mm/slab_common.c +4
-2@@ -43,11 +43,13 @@ DEFINE_MUTEX(slab_mutex);struct kmem_cache *kmem_cache;/* -* Set of flags that will prevent slab merging +* Set of flags that will prevent slab merging. +* Any flag that adds per-object metadata should be included, +* since slab merging can update s->inuse that affects the metadata layout. */ #define SLAB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \ SLAB_TRACE | SLAB_TYPESAFE_BY_RCU | SLAB_NOLEAKTRACE | \ - SLAB_FAILSLAB | SLAB_NO_MERGE) + SLAB_FAILSLAB | SLAB_NO_MERGE | SLAB_OBJ_EXT_IN_OBJ)#define SLAB_MERGE_SAME (SLAB_RECLAIM_ACCOUNT | SLAB_CACHE_DMA | \ SLAB_CACHE_DMA32 | SLAB_ACCOUNT)mm/slub.c +45
-2@@ -977,6 +977,39 @@ static inline bool obj_exts_in_slab(struct kmem_cache *s, struct slab *slab) {return false; } + +#endif + +#if defined(CONFIG_SLAB_OBJ_EXT ) && defined(CONFIG_64BIT ) +static bool obj_exts_in_object (struct kmem_cache *s) +{ +return s->flags &SLAB_OBJ_EXT_IN_OBJ ; +} + +static unsigned int obj_exts_offset_in_object (struct kmem_cache *s) +{ +unsigned int offset =get_info_end (s); + +if (kmem_cache_debug_flags (s,SLAB_STORE_USER )) + offset +=sizeof (struct track ) *2 ; + +if (slub_debug_orig_size (s)) + offset +=sizeof (unsigned long ); + + offset +=kasan_metadata_size (s, false); + +return offset; +} +#else +static inline bool obj_exts_in_object (struct kmem_cache *s) +{ +return false; +} + +static inline unsigned int obj_exts_offset_in_object (struct kmem_cache *s) +{ +return 0 ; +}#endif #ifdef CONFIG_SLUB_DEBUG @@ -1277,6 +1310,9 @@ static void print_trailer(struct kmem_cache *s, struct slab *slab, u8 *p) off +=kasan_metadata_size (s, false); +if (obj_exts_in_object (s)) + off +=sizeof (struct slabobj_ext ); +if (off !=size_from_object (s))/* Beginning of the filler is the free pointer */ print_section(KERN_ERR, "Padding ", p + off,@@ -1446,7 +1482,10 @@ check_bytes_and_report(struct kmem_cache *s, struct slab *slab,* A. Free pointer (if we cannot overwrite object on free) * B. Tracking data for SLAB_STORE_USER * C. Original request size for kmalloc object (SLAB_STORE_USER enabled) -* D. Padding to reach required alignment boundary or at minimum +* D. KASAN alloc metadata (KASAN enabled) +* E. struct slabobj_ext to store accounting metadata +* (SLAB_OBJ_EXT_IN_OBJ enabled) +* F. Padding to reach required alignment boundary or at minimum * one word if debugging is on to be able to detect writes * before the word boundary. * @@ -1474,6 +1513,9 @@ static int check_pad_bytes(struct kmem_cache *s, struct slab *slab, u8 *p) off +=kasan_metadata_size (s, false); +if (obj_exts_in_object (s)) + off +=sizeof (struct slabobj_ext ); +if (size_from_object (s) == off)return 1 ;@@ -2280,7 +2322,8 @@ static inline void free_slab_obj_exts(struct slab *slab)return ; } -if (obj_exts_in_slab (slab->slab_cache, slab)) { +if (obj_exts_in_slab (slab->slab_cache, slab) || +obj_exts_in_object (slab->slab_cache)) { slab->obj_exts =0 ;return ; }mm/slub.c static void alloc_slab_obj_exts_early(struct kmem_cache *s, struct slab *slab) · line 2377
Quoted 9 lines
mm/slub.c +6
-0@@ -2326,6 +2369,23 @@ static void alloc_slab_obj_exts_early(struct kmem_cache *s, struct slab *slab) obj_exts |=MEMCG_DATA_OBJEXTS ; slab->obj_exts = obj_exts;slab_set_stride (slab,sizeof (struct slabobj_ext )); + }else if (obj_exts_in_object (s)) { +unsigned int offset =obj_exts_offset_in_object (s); + + obj_exts = (unsigned long )slab_address (slab); + obj_exts += s->red_left_pad; + obj_exts +=obj_exts_offset_in_object (s);Hi, Harry
Quoted 1 line
It looks like this could just be simplified to
obj_exts+= offset, right?
Right! Will do in v5.
Quoted 51 lines
Quoted 24 lines
mm/slub.c +12
-0+ +get_slab_obj_exts (obj_exts); +for_each_object (addr, s,slab_address (slab), slab->objects) +memset (kasan_reset_tag (addr) + offset,0 , +sizeof (struct slabobj_ext )); +put_slab_obj_exts (obj_exts); + +if (IS_ENABLED (CONFIG_MEMCG )) + obj_exts |=MEMCG_DATA_OBJEXTS ; + slab->obj_exts = obj_exts; +slab_set_stride (slab, s->size); } }@@ -8023,6 +8083,7 @@ static int calculate_sizes(struct kmem_cache_args *args, struct kmem_cache *s) {slab_flags_t flags = s->flags;unsigned int size = s->object_size; +unsigned int aligned_size;unsigned int order;/* @@ -8132,7 +8193,13 @@ static int calculate_sizes(struct kmem_cache_args *args, struct kmem_cache *s)* offset 0. In order to align the objects we have to simply size mm/slub.c static int calculate_sizes(struct kmem_cache_args *args, struct kmem_cache *s) · line 8202
Quoted 10 lines
mm/slub.c +7
-1* each object to conform to the alignment. */ - size =ALIGN (size, s->align); + aligned_size =ALIGN (size, s->align); +#if defined(CONFIG_SLAB_OBJ_EXT ) && defined(CONFIG_64BIT ) +if (aligned_size - size >=sizeof (struct slabobj_ext )) + s->flags |=SLAB_OBJ_EXT_IN_OBJ ; +#endif + size = aligned_size; +One more thought: in
calculate_sizes()we add some extra padding whenSLAB_RED_ZONEis enabled:if (flags &
SLAB_RED_ZONE) {
/*
* Add some empty padding so that we can catch
* overwrites from earlier objects rather than let
* tracking information or the free pointer be
* corrupted if a user writes before the start
* of the object.
*/
size +=sizeof(void *);
...
}
Quoted 2 lines
From what I understand, this additional padding ends up being placed after the KASAN allocation metadata.
Right.
Quoted 3 lines
Since it’s only "extra" padding (i.e., it doesn’t seem strictly required for the layout), and your patch would reuse this area — together with the final padding introduced by
size = ALIGN(size, s->align);
Very good point!
Nah, it wasn't intentional to reuse the extra padding.
Quoted 3 lines
for objext, it seems like this padding may no longer provide much benefit. Do you think it would make sense to remove this extra padding altogether?
I think when debugging flags are enabled it'd still be useful to have, I'll try to keep the padding area after obj_ext (so that overwrites from the previous object won't overwrite the metadata).
Thanks a lot!
Signature · 2 lines
Cheers, Harry / Hyeonggon
Hao Li
reviewerRe: [PATCH V4 8/8] mm/slab: place slabobj_ext metadata in unused space within s->size
Quoted 256 lines · Harry Yoo
Quoted 191 lines · Hao Li
Quoted 178 lines · Harry Yoo
When a cache has high
s->alignvalue ands->object_sizeis not aligned to it, each object ends up with some unused space because of alignment. If this wasted space is big enough, we can use it to store theslabobj_extmetadata instead of wasting it.On my system, this happens with caches like
kmem_cache,mm_struct, pid,task_struct,sighand_cache,xfs_inode, and others.To place the
slabobj_extmetadata within each object, the existingslab_obj_ext()logic can still be used by setting:-
slab->obj_exts=slab_address(slab)+s->red_left_zone+ (slabobj_extoffset)
- stride =s->size
slab_obj_ext()doesn't need know where the metadata is stored, so this method works without adding extra overhead toslab_obj_ext().A good example benefiting from this optimization is
xfs_inode(object_size: 992, align: 64). To measure memory savings, 2 millions of files were created on XFS.[ MEMCG=y, MEM_ALLOC_PROFILING=n ]
Before patch (creating ~2.64M directories on xfs):
Slab: 5175976 kB SReclaimable: 3837524 kB SUnreclaim: 1338452 kBAfter patch (creating ~2.64M directories on xfs):
Slab: 5152912 kB SReclaimable: 3838568 kB SUnreclaim: 1314344 kB (-23.54 MiB)Enjoy the memory savings!
Suggested-by Vlastimil Babka <[email protected]>Signed-off-by Harry Yoo <[email protected]>--- include/linux/slab.h | 9 ++++++ mm/slab_common.c | 6 ++-- mm/slub.c | 73 ++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 83 insertions(+), 5 deletions(-)include/linux/slab.h +9
-0@@ -59,6 +59,9 @@ enum _slab_flag_bits { _SLAB_CMPXCHG_DOUBLE,#ifdef CONFIG_SLAB_OBJ_EXT _SLAB_NO_OBJ_EXT, +#endif +#if defined(CONFIG_SLAB_OBJ_EXT ) && defined(CONFIG_64BIT ) + _SLAB_OBJ_EXT_IN_OBJ, #endif _SLAB_FLAGS_LAST_BIT };@@ -244,6 +247,12 @@ enum _slab_flag_bits {#define SLAB_NO_OBJ_EXT __SLAB_FLAG_UNUSED#endif +#if defined(CONFIG_SLAB_OBJ_EXT ) && defined(CONFIG_64BIT ) +#define SLAB_OBJ_EXT_IN_OBJ __SLAB_FLAG_BIT(_SLAB_OBJ_EXT_IN_OBJ) +#else +#define SLAB_OBJ_EXT_IN_OBJ __SLAB_FLAG_UNUSED +#endif +/* * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests. * mm/slab_common.c +4
-2@@ -43,11 +43,13 @@ DEFINE_MUTEX(slab_mutex);struct kmem_cache *kmem_cache;/* -* Set of flags that will prevent slab merging +* Set of flags that will prevent slab merging. +* Any flag that adds per-object metadata should be included, +* since slab merging can update s->inuse that affects the metadata layout. */ #define SLAB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \ SLAB_TRACE | SLAB_TYPESAFE_BY_RCU | SLAB_NOLEAKTRACE | \ - SLAB_FAILSLAB | SLAB_NO_MERGE) + SLAB_FAILSLAB | SLAB_NO_MERGE | SLAB_OBJ_EXT_IN_OBJ)#define SLAB_MERGE_SAME (SLAB_RECLAIM_ACCOUNT | SLAB_CACHE_DMA | \ SLAB_CACHE_DMA32 | SLAB_ACCOUNT)mm/slub.c +45
-2@@ -977,6 +977,39 @@ static inline bool obj_exts_in_slab(struct kmem_cache *s, struct slab *slab) {return false; } + +#endif + +#if defined(CONFIG_SLAB_OBJ_EXT ) && defined(CONFIG_64BIT ) +static bool obj_exts_in_object (struct kmem_cache *s) +{ +return s->flags &SLAB_OBJ_EXT_IN_OBJ ; +} + +static unsigned int obj_exts_offset_in_object (struct kmem_cache *s) +{ +unsigned int offset =get_info_end (s); + +if (kmem_cache_debug_flags (s,SLAB_STORE_USER )) + offset +=sizeof (struct track ) *2 ; + +if (slub_debug_orig_size (s)) + offset +=sizeof (unsigned long ); + + offset +=kasan_metadata_size (s, false); + +return offset; +} +#else +static inline bool obj_exts_in_object (struct kmem_cache *s) +{ +return false; +} + +static inline unsigned int obj_exts_offset_in_object (struct kmem_cache *s) +{ +return 0 ; +}#endif #ifdef CONFIG_SLUB_DEBUG @@ -1277,6 +1310,9 @@ static void print_trailer(struct kmem_cache *s, struct slab *slab, u8 *p) off +=kasan_metadata_size (s, false); +if (obj_exts_in_object (s)) + off +=sizeof (struct slabobj_ext ); +if (off !=size_from_object (s))/* Beginning of the filler is the free pointer */ print_section(KERN_ERR, "Padding ", p + off,@@ -1446,7 +1482,10 @@ check_bytes_and_report(struct kmem_cache *s, struct slab *slab,* A. Free pointer (if we cannot overwrite object on free) * B. Tracking data for SLAB_STORE_USER * C. Original request size for kmalloc object (SLAB_STORE_USER enabled) -* D. Padding to reach required alignment boundary or at minimum +* D. KASAN alloc metadata (KASAN enabled) +* E. struct slabobj_ext to store accounting metadata +* (SLAB_OBJ_EXT_IN_OBJ enabled) +* F. Padding to reach required alignment boundary or at minimum * one word if debugging is on to be able to detect writes * before the word boundary. * @@ -1474,6 +1513,9 @@ static int check_pad_bytes(struct kmem_cache *s, struct slab *slab, u8 *p) off +=kasan_metadata_size (s, false); +if (obj_exts_in_object (s)) + off +=sizeof (struct slabobj_ext ); +if (size_from_object (s) == off)return 1 ;@@ -2280,7 +2322,8 @@ static inline void free_slab_obj_exts(struct slab *slab)return ; } -if (obj_exts_in_slab (slab->slab_cache, slab)) { +if (obj_exts_in_slab (slab->slab_cache, slab) || +obj_exts_in_object (slab->slab_cache)) { slab->obj_exts =0 ;return ; }mm/slub.c static void alloc_slab_obj_exts_early(struct kmem_cache *s, struct slab *slab) · line 2377
Quoted 9 lines
mm/slub.c +6
-0@@ -2326,6 +2369,23 @@ static void alloc_slab_obj_exts_early(struct kmem_cache *s, struct slab *slab) obj_exts |=MEMCG_DATA_OBJEXTS ; slab->obj_exts = obj_exts;slab_set_stride (slab,sizeof (struct slabobj_ext )); + }else if (obj_exts_in_object (s)) { +unsigned int offset =obj_exts_offset_in_object (s); + + obj_exts = (unsigned long )slab_address (slab); + obj_exts += s->red_left_pad; + obj_exts +=obj_exts_offset_in_object (s);Hi, Harry
Quoted 1 line
It looks like this could just be simplified to
obj_exts+= offset, right?Right! Will do in v5.
Quoted 51 lines
Quoted 24 lines
mm/slub.c +12
-0+ +get_slab_obj_exts (obj_exts); +for_each_object (addr, s,slab_address (slab), slab->objects) +memset (kasan_reset_tag (addr) + offset,0 , +sizeof (struct slabobj_ext )); +put_slab_obj_exts (obj_exts); + +if (IS_ENABLED (CONFIG_MEMCG )) + obj_exts |=MEMCG_DATA_OBJEXTS ; + slab->obj_exts = obj_exts; +slab_set_stride (slab, s->size); } }@@ -8023,6 +8083,7 @@ static int calculate_sizes(struct kmem_cache_args *args, struct kmem_cache *s) {slab_flags_t flags = s->flags;unsigned int size = s->object_size; +unsigned int aligned_size;unsigned int order;/* @@ -8132,7 +8193,13 @@ static int calculate_sizes(struct kmem_cache_args *args, struct kmem_cache *s)* offset 0. In order to align the objects we have to simply size mm/slub.c static int calculate_sizes(struct kmem_cache_args *args, struct kmem_cache *s) · line 8202
Quoted 10 lines
mm/slub.c +7
-1* each object to conform to the alignment. */ - size =ALIGN (size, s->align); + aligned_size =ALIGN (size, s->align); +#if defined(CONFIG_SLAB_OBJ_EXT ) && defined(CONFIG_64BIT ) +if (aligned_size - size >=sizeof (struct slabobj_ext )) + s->flags |=SLAB_OBJ_EXT_IN_OBJ ; +#endif + size = aligned_size; +One more thought: in
calculate_sizes()we add some extra padding whenSLAB_RED_ZONEis enabled:if (flags &
SLAB_RED_ZONE) {
/*
* Add some empty padding so that we can catch
* overwrites from earlier objects rather than let
* tracking information or the free pointer be
* corrupted if a user writes before the start
* of the object.
*/
size +=sizeof(void *);
...
}Quoted 2 lines
From what I understand, this additional padding ends up being placed after the KASAN allocation metadata.
Right.
Quoted 3 lines
Since it’s only "extra" padding (i.e., it doesn’t seem strictly required for the layout), and your patch would reuse this area — together with the final padding introduced by
size = ALIGN(size, s->align);
Quoted 8 lines
Very good point!
Nah, it wasn't intentional to reuse the extra padding.Quoted 3 lines
for objext, it seems like this padding may no longer provide much benefit. Do you think it would make sense to remove this extra padding altogether?
I think when debugging flags are enabled it'd still be useful to have,
Absolutely — I’m with you on this.
After thinking about it again, I agree it’s better to keep it.
Without that mandatory extra word, we could end up with "no trailing padding at all" in cases where ALIGN(size, s->align) doesn’t actually add any bytes.
Quoted 2 lines
I'll try to keep the padding area after
obj_ext(so that overwrites from the previous object won't overwrite the metadata).
Agree — we should make sure there is at least sizeof(void *) of extra space after obj_exts when SLAB_RED_ZONE is enabled, so POISON_INUSE has somewhere to go.
Quoted 2 lines
Thanks a lot!
Happy to help.
Signature · 2 lines
Thanks, Hao
Quoted 4 lines
Signature · 3 lines
-- Cheers, Harry / Hyeonggon
Re: [PATCH V4 8/8] mm/slab: place slabobj_ext metadata in unused space within s->size
Quoted 21 lines · Hao Li
Quoted 20 lines · Harry Yoo
Quoted 18 lines · Hao Li
One more thought: in
calculate_sizes()we add some extra padding whenSLAB_RED_ZONEis enabled:if (flags &
SLAB_RED_ZONE) {
/*
* Add some empty padding so that we can catch
* overwrites from earlier objects rather than let
* tracking information or the free pointer be
* corrupted if a user writes before the start
* of the object.
*/
size +=sizeof(void *);
...
}From what I understand, this additional padding ends up being placed after the KASAN allocation metadata.
Quoted 8 lines
Quoted 8 lines
Right.
Quoted 3 lines
Since it’s only "extra" padding (i.e., it doesn’t seem strictly required for the layout), and your patch would reuse this area — together with the final padding introduced by
size = ALIGN(size, s->align);Very good point!
Nah, it wasn't intentional to reuse the extra padding.
Waaaait, now I'm looking into it again to write V5...
It may reduce (or remove) the space for the final padding but not the mandatory padding because the mandatory padding is already included in the size before aligned_size = ALIGN(size, s->align)
Quoted 14 lines
Quoted 5 lines
Quoted 3 lines
for objext, it seems like this padding may no longer provide much benefit. Do you think it would make sense to remove this extra padding altogether?
I think when debugging flags are enabled it'd still be useful to have,
Absolutely — I’m with you on this.
After thinking about it again, I agree it’s better to keep it.
Without that mandatory extra word, we could end up with "no trailing padding at all" in cases where
ALIGN(size, s->align)doesn’t actually add any bytes.
Quoted 6 lines
Quoted 2 lines
I'll try to keep the padding area after
obj_ext(so that overwrites from the previous object won't overwrite the metadata).Agree — we should make sure there is at least
sizeof(void *)of extra space afterobj_extswhenSLAB_RED_ZONEis enabled, soPOISON_INUSEhas somewhere to go.
I think V4 of the patchset is already doing that, no?
The mandatory padding exists after obj_ext if SLAB_RED_ZONE is enabled and the final padding may or may not exist. check_pad_bytes() already knows that the padding(s) exist after obj_ext.
By the way, thanks for fixing the comment once again, it's easier to think about the layout now.
Signature · 2 lines
Cheers, Harry / Hyeonggon
Hao Li
reviewerRe: [PATCH V4 8/8] mm/slab: place slabobj_ext metadata in unused space within s->size
Quoted 28 lines · Harry Yoo
Quoted 27 lines · Hao Li
Quoted 26 lines · Harry Yoo
Quoted 16 lines · Hao Li
One more thought: in
calculate_sizes()we add some extra padding whenSLAB_RED_ZONEis enabled:if (flags &
SLAB_RED_ZONE) {
/*
* Add some empty padding so that we can catch
* overwrites from earlier objects rather than let
* tracking information or the free pointer be
* corrupted if a user writes before the start
* of the object.
*/
size +=sizeof(void *);
...
}Quoted 2 lines
From what I understand, this additional padding ends up being placed after the KASAN allocation metadata.
Right.
Quoted 3 lines
Since it’s only "extra" padding (i.e., it doesn’t seem strictly required for the layout), and your patch would reuse this area — together with the final padding introduced by
size = ALIGN(size, s->align);
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
Very good point!
Nah, it wasn't intentional to reuse the extra padding.Waaaait, now I'm looking into it again to write V5...
It may reduce (or remove) the space for the final padding but not the mandatory padding because the mandatory padding is already included in the size before
aligned_size = ALIGN(size, s->align)
Ah, right - I double-checked as well. aligned_size - size is exactly the space reserved for the final padding, so slabobj_ext won't eat into the mandatory padding.
Quoted 22 lines
Quoted 15 lines
Quoted 6 lines
Quoted 4 lines
for objext, it seems like this padding may no longer provide much benefit. Do you think it would make sense to remove this extra padding altogether?
I think when debugging flags are enabled it'd still be useful to have,
Absolutely — I’m with you on this.
After thinking about it again, I agree it’s better to keep it.
Without that mandatory extra word, we could end up with "no trailing padding at all" in cases where
ALIGN(size, s->align)doesn’t actually add any bytes.Quoted 6 lines
Quoted 2 lines
I'll try to keep the padding area after
obj_ext(so that overwrites from the previous object won't overwrite the metadata).Agree — we should make sure there is at least
sizeof(void *)of extra space afterobj_extswhenSLAB_RED_ZONEis enabled, soPOISON_INUSEhas somewhere to go.
Quoted 5 lines
I think V4 of the patchset is already doing that, no?
The mandatory padding exists after
obj_extifSLAB_RED_ZONEis enabled and the final padding may or may not exist.check_pad_bytes()already knows that the padding(s) exist afterobj_ext.
Yes, you are right, V4 already does this — I just hadn't noticed it earlier...
Quoted 3 lines
By the way, thanks for fixing the comment once again, it's easier to think about the layout now.
Glad it helped. The object layout is really subtle — missing even a small detail was enough to throw us off. Glad we finally got it all straightened out.
Signature · 2 lines
Thanks, Hao