Skip to contentSkip to conversation

mm/slub: enable debugging memory wasting of kmalloc

PATCH Superseded Feng Tang 4 participants 15 messages Jul 1, 2022 – Jul 19, 2022

Subsystems (2)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Feng Tang

author
#0 lore raw

PATCH 1/1 mm/slub: enable debugging memory wasting of kmalloc

kmalloc's API family is critical for mm, with one shortcoming that its object size is fixed to be power of 2. When user requests memory for '2^n + 1' bytes, actually 2^(n+1) bytes will be allocated, so in worst case, there is around 50% memory space waste.

We've met a kernel boot OOM panic (v5.10), and from the dumped slab info:

    [   26.062145] kmalloc-2k            814056KB     814056KB

From debug we found there are huge number of 'struct iova_magazine', whose size is 1032 bytes (1024 + 8), so each allocation will waste 1016 bytes. Though the issue was solved by giving the right (bigger) size of RAM, it is still nice to optimize the size (either use a kmalloc friendly size or create a dedicated slab for it).

And from lkml archive, there was another crash kernel OOM case [1] back in 2019, which seems to be related with the similar slab waste situation, as the log is similar:

    [    4.332648] iommu: Adding device 0000:20:02.0 to group 16
    [    4.338946] swapper/0 invoked oom-killer: gfp_mask=0x6040c0(GFP_KERNEL|__GFP_COMP), nodemask=(null), order=0, oom_score_adj=0
    ...
    [    4.857565] kmalloc-2048           59164KB      59164KB

The crash kernel only has 256M memory, and 59M is pretty big here. (Note: the related code has been changed and optimised in recent kernel [2], these logs are picked just to demo the problem)

So add an way to track each kmalloc's memory waste info, and leverage the existing SLUB debug framework to show its call stack info, so that user can evaluate the waste situation, identify some hot spots and optimize accordingly, for a better utilization of memory.

The waste info is integrated into existing interface:
/sys/kernel/debug/slab/kmalloc-xx/alloc_traces, one example of 'kmalloc-4k' after boot is:

126 ixgbe_alloc_q_vector+0xa5/0x4a0 [ixgbe] waste=233856/1856 age=1493302/1493830/1494358 pid=1284 cpus=32 nodes=1

        __slab_alloc.isra.86+0x52/0x80
        __kmalloc_node+0x143/0x350
        ixgbe_alloc_q_vector+0xa5/0x4a0 [ixgbe]
        ixgbe_init_interrupt_scheme+0x1a6/0x730 [ixgbe]
        ixgbe_probe+0xc8e/0x10d0 [ixgbe]
        local_pci_probe+0x42/0x80
        work_for_cpu_fn+0x13/0x20
        process_one_work+0x1c5/0x390

which means in 'kmalloc-4k' slab, there are 126 requests of 2240 bytes which got a 4KB space (wasting 1856 bytes each and 233856 bytes in total). And when system starts some real workload like multiple docker instances, there are more severe waste.

[1]. https://lkml.org/lkml/2019/8/12/266
[2]. https://lore.kernel.org/lkml/[email protected]/

---
Changelog:

since RFC
* fix problems in kmem_cache_alloc_bulk() and records sorting, improve the print format (Hyeonggon Yoo)
* fix a compiling issue found by 0Day bot
* update the commit log based info from iova developers

 mm/slub.c | 52 +++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 35 insertions(+), 17 deletions(-)
mm/slub.c +35 -17
index b1281b8654bd3..97304ea1e6aa5 100644
@@ -271,6 +271,7 @@ struct track {
 #endif
 	int cpu;		/* Was running on cpu */
 	int pid;		/* Pid context */
+	unsigned long waste;	/* memory waste for a kmalloc-ed object */
 	unsigned long when;	/* When did the operation occur */
 };
 
@@ -747,6 +748,7 @@ static inline depot_stack_handle_t set_track_prepare(void)
 
 static void set_track_update(struct kmem_cache *s, void *object,
 			     enum track_item alloc, unsigned long addr,
+			     unsigned long waste,
 			     depot_stack_handle_t handle)
 {
 	struct track *p = get_track(s, object, alloc);
@@ -758,14 +760,16 @@ static void set_track_update(struct kmem_cache *s, void *object,
 	p->cpu = smp_processor_id();
 	p->pid = current->pid;
 	p->when = jiffies;
+	p->waste = waste;
 }
 
 static __always_inline void set_track(struct kmem_cache *s, void *object,
-				      enum track_item alloc, unsigned long addr)
+				      enum track_item alloc, unsigned long addr,
+				      unsigned long waste)
 {
 	depot_stack_handle_t handle = set_track_prepare();
 
-	set_track_update(s, object, alloc, addr, handle);
+	set_track_update(s, object, alloc, addr, waste, handle);
 }
 
 static void init_tracking(struct kmem_cache *s, void *object)
@@ -1325,7 +1329,9 @@ static inline int alloc_consistency_checks(struct kmem_cache *s,
 
 static noinline int alloc_debug_processing(struct kmem_cache *s,
 					struct slab *slab,
-					void *object, unsigned long addr)
+					void *object, unsigned long addr,
+					unsigned long waste
+					)
 {
 	if (s->flags & SLAB_CONSISTENCY_CHECKS) {
 		if (!alloc_consistency_checks(s, slab, object))
@@ -1334,7 +1340,7 @@ static noinline int alloc_debug_processing(struct kmem_cache *s,
 
 	/* Success perform special debug activities for allocs */
 	if (s->flags & SLAB_STORE_USER)
-		set_track(s, object, TRACK_ALLOC, addr);
+		set_track(s, object, TRACK_ALLOC, addr, waste);
 	trace(s, slab, object, 1);
 	init_object(s, object, SLUB_RED_ACTIVE);
 	return 1;
@@ -1418,7 +1424,7 @@ static noinline int free_debug_processing(
 	}
 
 	if (s->flags & SLAB_STORE_USER)
-		set_track_update(s, object, TRACK_FREE, addr, handle);
+		set_track_update(s, object, TRACK_FREE, addr, 0, handle);
 	trace(s, slab, object, 0);
 	/* Freepointer not overwritten by init_object(), SLAB_POISON moved it */
 	init_object(s, object, SLUB_RED_INACTIVE);
@@ -1661,7 +1667,8 @@ static inline
 void setup_slab_debug(struct kmem_cache *s, struct slab *slab, void *addr) {}
 
 static inline int alloc_debug_processing(struct kmem_cache *s,
-	struct slab *slab, void *object, unsigned long addr) { return 0; }
+	struct slab *slab, void *object, unsigned long addr,
+	unsigned long waste) { return 0; }
 
 static inline int free_debug_processing(
 	struct kmem_cache *s, struct slab *slab,
@@ -2905,7 +2912,7 @@ static inline void *get_freelist(struct kmem_cache *s, struct slab *slab)
  * already disabled (which is the case for bulk allocation).
  */
 static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
-			  unsigned long addr, struct kmem_cache_cpu *c)
+			  unsigned long addr, struct kmem_cache_cpu *c, unsigned int orig_size)
 {
 	void *freelist;
 	struct slab *slab;
@@ -3048,7 +3055,7 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 check_new_slab:
 
 	if (kmem_cache_debug(s)) {
-		if (!alloc_debug_processing(s, slab, freelist, addr)) {
+		if (!alloc_debug_processing(s, slab, freelist, addr, s->object_size - orig_size)) {
 			/* Slab failed checks. Next slab needed */
 			goto new_slab;
 		} else {
@@ -3102,7 +3109,7 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  * pointer.
  */
 static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
-			  unsigned long addr, struct kmem_cache_cpu *c)
+			  unsigned long addr, struct kmem_cache_cpu *c, unsigned int orig_size)
 {
 	void *p;
 
@@ -3115,7 +3122,7 @@ static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 	c = slub_get_cpu_ptr(s->cpu_slab);
 #endif
 
-	p = ___slab_alloc(s, gfpflags, node, addr, c);
+	p = ___slab_alloc(s, gfpflags, node, addr, c, orig_size);
 #ifdef CONFIG_PREEMPT_COUNT
 	slub_put_cpu_ptr(s->cpu_slab);
 #endif
@@ -3206,7 +3213,7 @@ static __always_inline void *slab_alloc_node(struct kmem_cache *s, struct list_l
 	 */
 	if (IS_ENABLED(CONFIG_PREEMPT_RT) ||
 	    unlikely(!object || !slab || !node_match(slab, node))) {
-		object = __slab_alloc(s, gfpflags, node, addr, c);
+		object = __slab_alloc(s, gfpflags, node, addr, c, orig_size);
 	} else {
 		void *next_object = get_freepointer_safe(s, object);
 
@@ -3731,7 +3738,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
 			 * of re-populating per CPU c->freelist
 			 */
 			p[i] = ___slab_alloc(s, flags, NUMA_NO_NODE,
-					    _RET_IP_, c);
+					    _RET_IP_, c, s->object_size);
 			if (unlikely(!p[i]))
 				goto error;
 
@@ -5092,6 +5099,7 @@ struct location {
 	depot_stack_handle_t handle;
 	unsigned long count;
 	unsigned long addr;
+	unsigned long waste;
 	long long sum_time;
 	long min_time;
 	long max_time;
@@ -5142,7 +5150,7 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
 {
 	long start, end, pos;
 	struct location *l;
-	unsigned long caddr, chandle;
+	unsigned long caddr, chandle, cwaste;
 	unsigned long age = jiffies - track->when;
 	depot_stack_handle_t handle = 0;
 
@@ -5162,11 +5170,13 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
 		if (pos == end)
 			break;
 
-		caddr = t->loc[pos].addr;
-		chandle = t->loc[pos].handle;
-		if ((track->addr == caddr) && (handle == chandle)) {
+		l = &t->loc[pos];
+		caddr = l->addr;
+		chandle = l->handle;
+		cwaste = l->waste;
+		if ((track->addr == caddr) && (handle == chandle) &&
+			(track->waste == cwaste)) {
 
-			l = &t->loc[pos];
 			l->count++;
 			if (track->when) {
 				l->sum_time += age;
@@ -5191,6 +5201,9 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
 			end = pos;
 		else if (track->addr == caddr && handle < chandle)
 			end = pos;
+		else if (track->addr == caddr && handle == chandle &&
+				track->waste < cwaste)
+			end = pos;
 		else
 			start = pos;
 	}
@@ -5214,6 +5227,7 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
 	l->min_pid = track->pid;
 	l->max_pid = track->pid;
 	l->handle = handle;
+	l->waste = track->waste;
 	cpumask_clear(to_cpumask(l->cpus));
 	cpumask_set_cpu(track->cpu, to_cpumask(l->cpus));
 	nodes_clear(l->nodes);
@@ -6102,6 +6116,10 @@ static int slab_debugfs_show(struct seq_file *seq, void *v)
 		else
 			seq_puts(seq, "<not-available>");
 
+		if (l->waste)
+			seq_printf(seq, " waste=%lu/%lu",
+				l->count * l->waste, l->waste);
+
 		if (l->sum_time != l->min_time) {
 			seq_printf(seq, " age=%ld/%llu/%ld",
 				l->min_time, div_u64(l->sum_time, l->count),
Signature · 1 line
2.27.0
#1 lore raw

mm/slub.c static inline void *get_freelist(struct kmem_cache *s, struct slab *slab) · line 2916

Quoted 4 lines · Feng Tang

static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,

mm/slub.c +1 -1
-			  unsigned long addr, struct kmem_cache_cpu *c)
+			  unsigned long addr, struct kmem_cache_cpu *c, unsigned int orig_size)
 {

It would be good to avoid expanding the basic slab handling functions for kmalloc. Can we restrict the mods to the kmalloc related functions?

author
#2 lore raw

Hi Christoph,

Quoted 9 lines · Christoph Lameter

mm/slub.c static inline void *get_freelist(struct kmem_cache *s, struct slab *slab) · line 2916

Quoted 4 lines · Feng Tang

static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,

mm/slub.c +1 -1
-			  unsigned long addr, struct kmem_cache_cpu *c)
+			  unsigned long addr, struct kmem_cache_cpu *c, unsigned int orig_size)
 {

It would be good to avoid expanding the basic slab handling functions for kmalloc. Can we restrict the mods to the kmalloc related functions?

Yes, this is the part that concerned me. I tried but haven't figured a way.

I started implemting it several month ago, and stuck with several kmalloc APIs in a hacky way like dump_stack() when there is a waste over 1/4 of the object_size of the kmalloc_caches[][].

Then I found one central API which has all the needed info (object_size & orig_size) that we can yell about the waste :

static __always_inline void *slab_alloc_node(struct kmem_cache *s, struct list_lru *lru,
gfp_t gfpflags, int node, unsigned long addr, size_t orig_size)

which I thought could be still hacky, as the existing 'alloc_traces' can't be resued which already has the count/call-stack info. Current solution leverage it at the cost of adding 'orig_size' parameters, but I don't know how to pass the 'waste' info through as track/location is in the lowest level.

Thanks,
Feng

#3 lore raw
Quoted 23 lines · Feng Tang

Hi Christoph,

Quoted 9 lines · Christoph Lameter

mm/slub.c static inline void *get_freelist(struct kmem_cache *s, struct slab *slab) · line 2916

Quoted 4 lines · Feng Tang

static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,

mm/slub.c +1 -1
-			  unsigned long addr, struct kmem_cache_cpu *c)
+			  unsigned long addr, struct kmem_cache_cpu *c, unsigned int orig_size)
 {

It would be good to avoid expanding the basic slab handling functions for kmalloc. Can we restrict the mods to the kmalloc related functions?

Yes, this is the part that concerned me. I tried but haven't figured a way.

I started implemting it several month ago, and stuck with several kmalloc APIs in a hacky way like dump_stack() when there is a waste over 1/4 of the object_size of the kmalloc_caches[][].

Then I found one central API which has all the needed info (object_size & orig_size) that we can yell about the waste :

Quoted 8 lines

static __always_inline void *slab_alloc_node(struct kmem_cache *s, struct list_lru *lru,
gfp_t gfpflags, int node, unsigned long addr, size_t orig_size)

which I thought could be still hacky, as the existing 'alloc_traces' can't be resued which already has the count/call-stack info. Current solution leverage it at the cost of adding 'orig_size' parameters, but I don't know how to pass the 'waste' info through as track/location is in the lowest level.

If adding cost of orig_size parameter for non-debugging case is concern, what about doing this in userspace script that makes use of kmalloc tracepoints?

	kmalloc: call_site=tty_buffer_alloc+0x43/0x90 ptr=00000000b78761e1
	bytes_req=1056 bytes_alloc=2048 gfp_flags=GFP_ATOMIC|__GFP_NOWARN
	accounted=false

calculating sum of (bytes_alloc - bytes_req) for each call_site may be an alternative solution.

Thanks,
Hyeonggon

Quoted 5 lines

Thanks,
Feng

author
#4 lore raw
Quoted 37 lines · Hyeonggon Yoo
Quoted 23 lines · Feng Tang

Hi Christoph,

Quoted 9 lines · Christoph Lameter

Review

Quoted 4 lines · Feng Tang

static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,

(quoted diff) +1 -1
-			  unsigned long addr, struct kmem_cache_cpu *c)
+			  unsigned long addr, struct kmem_cache_cpu *c, unsigned int orig_size)
 {

It would be good to avoid expanding the basic slab handling functions for kmalloc. Can we restrict the mods to the kmalloc related functions?

Yes, this is the part that concerned me. I tried but haven't figured a way.

I started implemting it several month ago, and stuck with several kmalloc APIs in a hacky way like dump_stack() when there is a waste over 1/4 of the object_size of the kmalloc_caches[][].

Then I found one central API which has all the needed info (object_size & orig_size) that we can yell about the waste :

Quoted 8 lines

static __always_inline void *slab_alloc_node(struct kmem_cache *s, struct list_lru *lru,
gfp_t gfpflags, int node, unsigned long addr, size_t orig_size)

which I thought could be still hacky, as the existing 'alloc_traces' can't be resued which already has the count/call-stack info. Current solution leverage it at the cost of adding 'orig_size' parameters, but I don't know how to pass the 'waste' info through as track/location is in the lowest level.

If adding cost of orig_size parameter for non-debugging case is concern, what about doing this in userspace script that makes use of kmalloc tracepoints?

Quoted 6 lines
	kmalloc: call_site=tty_buffer_alloc+0x43/0x90 ptr=00000000b78761e1
	bytes_req=1056 bytes_alloc=2048 gfp_flags=GFP_ATOMIC|__GFP_NOWARN
	accounted=false

calculating sum of (bytes_alloc - bytes_req) for each call_site may be an alternative solution.

Yes, this is doable, and it will met some of the problems I met before, one is there are currently 2 alloc path: kmalloc and kmalloc_node, also we need to consider the free problem to calculate the real waste, and the free trace point doesn't have size info (Yes, we could compare the pointer with alloc path, and the user script may need to be more complexer). That's why I love the current 'alloc_traces' interface, which has the count (slove the free counting problem) and full call stack info.

And for the extra parameter cost issue, I rethink about it, and we can leverage the 'slab_alloc_node()' to solve it, and the patch is much simpler now without adding a new parameter:

---

mm/slub.c +27 -5
index b1281b8654bd3..ce4568dbb0f2d 100644
@@ -271,6 +271,7 @@ struct track {
 #endif
 	int cpu;		/* Was running on cpu */
 	int pid;		/* Pid context */
+	unsigned long waste;	/* memory waste for a kmalloc-ed object */
 	unsigned long when;	/* When did the operation occur */
 };
 
@@ -3240,6 +3241,16 @@ static __always_inline void *slab_alloc_node(struct kmem_cache *s, struct list_l
 	init = slab_want_init_on_alloc(gfpflags, s);
 
 out:
+
+#ifdef CONFIG_SLUB_DEBUG
+	if (object && s->object_size != orig_size) {
+		struct track *track;
+
+		track = get_track(s, object, TRACK_ALLOC);
+		track->waste = s->object_size - orig_size;
+	}
+#endif
+
 	slab_post_alloc_hook(s, objcg, gfpflags, 1, &object, init);
 
 	return object;
@@ -5092,6 +5103,7 @@ struct location {
 	depot_stack_handle_t handle;
 	unsigned long count;
 	unsigned long addr;
+	unsigned long waste;
 	long long sum_time;
 	long min_time;
 	long max_time;
@@ -5142,7 +5154,7 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
 {
 	long start, end, pos;
 	struct location *l;
-	unsigned long caddr, chandle;
+	unsigned long caddr, chandle, cwaste;
 	unsigned long age = jiffies - track->when;
 	depot_stack_handle_t handle = 0;
 
@@ -5162,11 +5174,13 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
 		if (pos == end)
 			break;
 
-		caddr = t->loc[pos].addr;
-		chandle = t->loc[pos].handle;
-		if ((track->addr == caddr) && (handle == chandle)) {
+		l = &t->loc[pos];
+		caddr = l->addr;
+		chandle = l->handle;
+		cwaste = l->waste;
+		if ((track->addr == caddr) && (handle == chandle) &&
+			(track->waste == cwaste)) {
 
-			l = &t->loc[pos];
 			l->count++;
 			if (track->when) {
 				l->sum_time += age;
@@ -5191,6 +5205,9 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
 			end = pos;
 		else if (track->addr == caddr && handle < chandle)
 			end = pos;
+		else if (track->addr == caddr && handle == chandle &&
+				track->waste < cwaste)
+			end = pos;
 		else
 			start = pos;
 	}
@@ -5214,6 +5231,7 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
 	l->min_pid = track->pid;
 	l->max_pid = track->pid;
 	l->handle = handle;
+	l->waste = track->waste;
 	cpumask_clear(to_cpumask(l->cpus));
 	cpumask_set_cpu(track->cpu, to_cpumask(l->cpus));
 	nodes_clear(l->nodes);
@@ -6102,6 +6120,10 @@ static int slab_debugfs_show(struct seq_file *seq, void *v)
 		else
 			seq_puts(seq, "<not-available>");
 
+		if (l->waste)
+			seq_printf(seq, " waste=%lu/%lu",
+				l->count * l->waste, l->waste);
+
 		if (l->sum_time != l->min_time) {
 			seq_printf(seq, " age=%ld/%llu/%ld",
 				l->min_time, div_u64(l->sum_time, l->count),

Thanks,
Feng

Quoted 8 lines

Thanks,
Hyeonggon

Quoted 5 lines

Thanks,
Feng

#5 lore raw
Quoted 45 lines · Feng Tang
Quoted 37 lines · Hyeonggon Yoo
Quoted 23 lines · Feng Tang

Hi Christoph,

Quoted 9 lines · Christoph Lameter

Review

Quoted 4 lines · Feng Tang

static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,

(quoted diff) +1 -1
-			  unsigned long addr, struct kmem_cache_cpu *c)
+			  unsigned long addr, struct kmem_cache_cpu *c, unsigned int orig_size)
 {

It would be good to avoid expanding the basic slab handling functions for kmalloc. Can we restrict the mods to the kmalloc related functions?

Yes, this is the part that concerned me. I tried but haven't figured a way.

I started implemting it several month ago, and stuck with several kmalloc APIs in a hacky way like dump_stack() when there is a waste over 1/4 of the object_size of the kmalloc_caches[][].

Then I found one central API which has all the needed info (object_size & orig_size) that we can yell about the waste :

Quoted 8 lines

static __always_inline void *slab_alloc_node(struct kmem_cache *s, struct list_lru *lru,
gfp_t gfpflags, int node, unsigned long addr, size_t orig_size)

which I thought could be still hacky, as the existing 'alloc_traces' can't be resued which already has the count/call-stack info. Current solution leverage it at the cost of adding 'orig_size' parameters, but I don't know how to pass the 'waste' info through as track/location is in the lowest level.

If adding cost of orig_size parameter for non-debugging case is concern, what about doing this in userspace script that makes use of kmalloc tracepoints?

Quoted 6 lines
	kmalloc: call_site=tty_buffer_alloc+0x43/0x90 ptr=00000000b78761e1
	bytes_req=1056 bytes_alloc=2048 gfp_flags=GFP_ATOMIC|__GFP_NOWARN
	accounted=false

calculating sum of (bytes_alloc - bytes_req) for each call_site may be an alternative solution.

Quoted 8 lines

Yes, this is doable, and it will met some of the problems I met before, one is there are currently 2 alloc path: kmalloc and kmalloc_node, also we need to consider the free problem to calculate the real waste, and the free trace point doesn't have size info (Yes, we could compare the pointer with alloc path, and the user script may need to be more complexer). That's why I love the current 'alloc_traces' interface, which has the count (slove the free counting problem) and full call stack info.

Understood.

Quoted 21 lines

And for the extra parameter cost issue, I rethink about it, and we can leverage the 'slab_alloc_node()' to solve it, and the patch is much simpler now without adding a new parameter:

---

mm/slub.c +1 -0
index b1281b8654bd3..ce4568dbb0f2d 100644
@@ -271,6 +271,7 @@ struct track {
 #endif
 	int cpu;		/* Was running on cpu */
 	int pid;		/* Pid context */
+	unsigned long waste;	/* memory waste for a kmalloc-ed object */
 	unsigned long when;	/* When did the operation occur */
 };
 
@@ -3240,6 +3241,16 @@ static __always_inline void *slab_alloc_node(struct kmem_cache *s, struct list_l
 	init = slab_want_init_on_alloc(gfpflags, s);
 
 out:

mm/slub.c static __always_inline void *slab_alloc_node(struct kmem_cache *s, struct list_l · line 3253

Quoted 10 lines
mm/slub.c +10 -0
+
+#ifdef CONFIG_SLUB_DEBUG
+	if (object && s->object_size != orig_size) {
+		struct track *track;
+
+		track = get_track(s, object, TRACK_ALLOC);
+		track->waste = s->object_size - orig_size;
+	}
+#endif
+

This scares me. It does not check if the cache has SLAB_STORE_USER flag.

Also CONFIG_SLUB_DEBUG is enabled by default, which means that it is still against not affecting non-debugging case.

I like v1 more than modified version.

Thanks,
Hyeonggon

Quoted 79 lines
mm/slub.c +16 -5
 	slab_post_alloc_hook(s, objcg, gfpflags, 1, &object, init);
 
 	return object;
@@ -5092,6 +5103,7 @@ struct location {
 	depot_stack_handle_t handle;
 	unsigned long count;
 	unsigned long addr;
+	unsigned long waste;
 	long long sum_time;
 	long min_time;
 	long max_time;
@@ -5142,7 +5154,7 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
 {
 	long start, end, pos;
 	struct location *l;
-	unsigned long caddr, chandle;
+	unsigned long caddr, chandle, cwaste;
 	unsigned long age = jiffies - track->when;
 	depot_stack_handle_t handle = 0;
 
@@ -5162,11 +5174,13 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
 		if (pos == end)
 			break;
 
-		caddr = t->loc[pos].addr;
-		chandle = t->loc[pos].handle;
-		if ((track->addr == caddr) && (handle == chandle)) {
+		l = &t->loc[pos];
+		caddr = l->addr;
+		chandle = l->handle;
+		cwaste = l->waste;
+		if ((track->addr == caddr) && (handle == chandle) &&
+			(track->waste == cwaste)) {
 
-			l = &t->loc[pos];
 			l->count++;
 			if (track->when) {
 				l->sum_time += age;
@@ -5191,6 +5205,9 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
 			end = pos;
 		else if (track->addr == caddr && handle < chandle)
 			end = pos;
+		else if (track->addr == caddr && handle == chandle &&
+				track->waste < cwaste)
+			end = pos;
 		else
 			start = pos;
 	}
@@ -5214,6 +5231,7 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
 	l->min_pid = track->pid;
 	l->max_pid = track->pid;
 	l->handle = handle;
+	l->waste = track->waste;
 	cpumask_clear(to_cpumask(l->cpus));
 	cpumask_set_cpu(track->cpu, to_cpumask(l->cpus));
 	nodes_clear(l->nodes);
@@ -6102,6 +6120,10 @@ static int slab_debugfs_show(struct seq_file *seq, void *v)
 		else
 			seq_puts(seq, "<not-available>");
 
+		if (l->waste)
+			seq_printf(seq, " waste=%lu/%lu",
+				l->count * l->waste, l->waste);
+
 		if (l->sum_time != l->min_time) {
 			seq_printf(seq, " age=%ld/%llu/%ld",
 				l->min_time, div_u64(l->sum_time, l->count),

Thanks,
Feng

Quoted 8 lines

Thanks,
Hyeonggon

Quoted 5 lines

Thanks,
Feng

author
#6 lore raw
Quoted 77 lines · Hyeonggon Yoo
Quoted 45 lines · Feng Tang
Quoted 37 lines · Hyeonggon Yoo
Quoted 23 lines · Feng Tang

Hi Christoph,

Quoted 9 lines · Christoph Lameter

Review

Quoted 4 lines · Feng Tang

static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,

(quoted diff) +1 -1
-			  unsigned long addr, struct kmem_cache_cpu *c)
+			  unsigned long addr, struct kmem_cache_cpu *c, unsigned int orig_size)
 {

It would be good to avoid expanding the basic slab handling functions for kmalloc. Can we restrict the mods to the kmalloc related functions?

Yes, this is the part that concerned me. I tried but haven't figured a way.

I started implemting it several month ago, and stuck with several kmalloc APIs in a hacky way like dump_stack() when there is a waste over 1/4 of the object_size of the kmalloc_caches[][].

Then I found one central API which has all the needed info (object_size & orig_size) that we can yell about the waste :

Quoted 8 lines

static __always_inline void *slab_alloc_node(struct kmem_cache *s, struct list_lru *lru,
gfp_t gfpflags, int node, unsigned long addr, size_t orig_size)

which I thought could be still hacky, as the existing 'alloc_traces' can't be resued which already has the count/call-stack info. Current solution leverage it at the cost of adding 'orig_size' parameters, but I don't know how to pass the 'waste' info through as track/location is in the lowest level.

If adding cost of orig_size parameter for non-debugging case is concern, what about doing this in userspace script that makes use of kmalloc tracepoints?

Quoted 6 lines
	kmalloc: call_site=tty_buffer_alloc+0x43/0x90 ptr=00000000b78761e1
	bytes_req=1056 bytes_alloc=2048 gfp_flags=GFP_ATOMIC|__GFP_NOWARN
	accounted=false

calculating sum of (bytes_alloc - bytes_req) for each call_site may be an alternative solution.

Quoted 8 lines

Yes, this is doable, and it will met some of the problems I met before, one is there are currently 2 alloc path: kmalloc and kmalloc_node, also we need to consider the free problem to calculate the real waste, and the free trace point doesn't have size info (Yes, we could compare the pointer with alloc path, and the user script may need to be more complexer). That's why I love the current 'alloc_traces' interface, which has the count (slove the free counting problem) and full call stack info.

Understood.

Quoted 20 lines

And for the extra parameter cost issue, I rethink about it, and we can leverage the 'slab_alloc_node()' to solve it, and the patch is much simpler now without adding a new parameter:

---

mm/slub.c +1 -0
index b1281b8654bd3..ce4568dbb0f2d 100644
@@ -271,6 +271,7 @@ struct track {
 #endif
 	int cpu;		/* Was running on cpu */
 	int pid;		/* Pid context */
+	unsigned long waste;	/* memory waste for a kmalloc-ed object */
 	unsigned long when;	/* When did the operation occur */
 };
 
@@ -3240,6 +3241,16 @@ static __always_inline void *slab_alloc_node(struct kmem_cache *s, struct list_l
 	init = slab_want_init_on_alloc(gfpflags, s);
 
Quoted 14 lines

mm/slub.c static __always_inline void *slab_alloc_node(struct kmem_cache *s, struct list_l · line 3253

Quoted 11 lines
mm/slub.c +10 -0
 out:
+
+#ifdef CONFIG_SLUB_DEBUG
+	if (object && s->object_size != orig_size) {
+		struct track *track;
+
+		track = get_track(s, object, TRACK_ALLOC);
+		track->waste = s->object_size - orig_size;
+	}
+#endif
+

This scares me. It does not check if the cache has SLAB_STORE_USER flag.

Yes, I missed that.

Quoted 2 lines

Also CONFIG_SLUB_DEBUG is enabled by default, which means that it is still against not affecting non-debugging case.

Yes, logically these debug stuff can be put together in low-level function.

Quoted 1 line

I like v1 more than modified version.

I see, thanks

- Feng

Quoted 82 lines

Thanks,
Hyeonggon

Quoted 79 lines
mm/slub.c +16 -5
 	slab_post_alloc_hook(s, objcg, gfpflags, 1, &object, init);
 
 	return object;
@@ -5092,6 +5103,7 @@ struct location {
 	depot_stack_handle_t handle;
 	unsigned long count;
 	unsigned long addr;
+	unsigned long waste;
 	long long sum_time;
 	long min_time;
 	long max_time;
@@ -5142,7 +5154,7 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
 {
 	long start, end, pos;
 	struct location *l;
-	unsigned long caddr, chandle;
+	unsigned long caddr, chandle, cwaste;
 	unsigned long age = jiffies - track->when;
 	depot_stack_handle_t handle = 0;
 
@@ -5162,11 +5174,13 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
 		if (pos == end)
 			break;
 
-		caddr = t->loc[pos].addr;
-		chandle = t->loc[pos].handle;
-		if ((track->addr == caddr) && (handle == chandle)) {
+		l = &t->loc[pos];
+		caddr = l->addr;
+		chandle = l->handle;
+		cwaste = l->waste;
+		if ((track->addr == caddr) && (handle == chandle) &&
+			(track->waste == cwaste)) {
 
-			l = &t->loc[pos];
 			l->count++;
 			if (track->when) {
 				l->sum_time += age;
@@ -5191,6 +5205,9 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
 			end = pos;
 		else if (track->addr == caddr && handle < chandle)
 			end = pos;
+		else if (track->addr == caddr && handle == chandle &&
+				track->waste < cwaste)
+			end = pos;
 		else
 			start = pos;
 	}
@@ -5214,6 +5231,7 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
 	l->min_pid = track->pid;
 	l->max_pid = track->pid;
 	l->handle = handle;
+	l->waste = track->waste;
 	cpumask_clear(to_cpumask(l->cpus));
 	cpumask_set_cpu(track->cpu, to_cpumask(l->cpus));
 	nodes_clear(l->nodes);
@@ -6102,6 +6120,10 @@ static int slab_debugfs_show(struct seq_file *seq, void *v)
 		else
 			seq_puts(seq, "<not-available>");
 
+		if (l->waste)
+			seq_printf(seq, " waste=%lu/%lu",
+				l->count * l->waste, l->waste);
+
 		if (l->sum_time != l->min_time) {
 			seq_printf(seq, " age=%ld/%llu/%ld",
 				l->min_time, div_u64(l->sum_time, l->count),

Thanks,
Feng

Quoted 8 lines

Thanks,
Hyeonggon

Quoted 5 lines

Thanks,
Feng

#7 lore raw
In reply to Feng Tang
Quoted 53 lines · Feng Tang

kmalloc's API family is critical for mm, with one shortcoming that its object size is fixed to be power of 2. When user requests memory for '2^n + 1' bytes, actually 2^(n+1) bytes will be allocated, so in worst case, there is around 50% memory space waste.

We've met a kernel boot OOM panic (v5.10), and from the dumped slab info:

    [   26.062145] kmalloc-2k            814056KB     814056KB

From debug we found there are huge number of 'struct iova_magazine', whose size is 1032 bytes (1024 + 8), so each allocation will waste 1016 bytes. Though the issue was solved by giving the right (bigger) size of RAM, it is still nice to optimize the size (either use a kmalloc friendly size or create a dedicated slab for it).

And from lkml archive, there was another crash kernel OOM case [1] back in 2019, which seems to be related with the similar slab waste situation, as the log is similar:

    [    4.332648] iommu: Adding device 0000:20:02.0 to group 16
    [    4.338946] swapper/0 invoked oom-killer: gfp_mask=0x6040c0(GFP_KERNEL|__GFP_COMP), nodemask=(null), order=0, oom_score_adj=0
    ...
    [    4.857565] kmalloc-2048           59164KB      59164KB

The crash kernel only has 256M memory, and 59M is pretty big here. (Note: the related code has been changed and optimised in recent kernel [2], these logs are picked just to demo the problem)

So add an way to track each kmalloc's memory waste info, and leverage the existing SLUB debug framework to show its call stack info, so that user can evaluate the waste situation, identify some hot spots and optimize accordingly, for a better utilization of memory.

The waste info is integrated into existing interface:
/sys/kernel/debug/slab/kmalloc-xx/alloc_traces, one example of 'kmalloc-4k' after boot is:

126 ixgbe_alloc_q_vector+0xa5/0x4a0 [ixgbe] waste=233856/1856 age=1493302/1493830/1494358 pid=1284 cpus=32 nodes=1

        __slab_alloc.isra.86+0x52/0x80
        __kmalloc_node+0x143/0x350
        ixgbe_alloc_q_vector+0xa5/0x4a0 [ixgbe]
        ixgbe_init_interrupt_scheme+0x1a6/0x730 [ixgbe]
        ixgbe_probe+0xc8e/0x10d0 [ixgbe]
        local_pci_probe+0x42/0x80
        work_for_cpu_fn+0x13/0x20
        process_one_work+0x1c5/0x390

which means in 'kmalloc-4k' slab, there are 126 requests of 2240 bytes which got a 4KB space (wasting 1856 bytes each and 233856 bytes in total). And when system starts some real workload like multiple docker instances, there are more severe waste.

Quoted 4 lines

[1]. https://lkml.org/lkml/2019/8/12/266
[2]. https://lore.kernel.org/lkml/[email protected]/

Hi and thanks.
I would suggest some improvements to consider:

- don't use the struct track to store orig_size, although it's an obvious first choice. It's unused waste for the free_track, and also for any non-kmalloc caches. I'd carve out an extra int next to the struct tracks. Only for kmalloc caches (probably a new kmem cache flag set on creation will be needed to easily distinguish them).
Besides the saved space, you can then set the field from ___slab_alloc() directly and not need to pass the orig_size also to alloc_debug_processing() etc.

- the knowledge of actual size could be used to improve poisoning checks as well, detect cases when there's buffer overrun over the orig_size but not cache's size. e.g. if you kmalloc(48) and overrun up to 64 we won't detect it now, but with orig_size stored we could?

Thanks!
Vlastimil

Quoted 208 lines

---
Changelog:

since RFC
* fix problems in kmem_cache_alloc_bulk() and records sorting, improve the print format (Hyeonggon Yoo)
* fix a compiling issue found by 0Day bot
* update the commit log based info from iova developers

 mm/slub.c | 52 +++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 35 insertions(+), 17 deletions(-)
mm/slub.c +35 -17
index b1281b8654bd3..97304ea1e6aa5 100644
@@ -271,6 +271,7 @@ struct track {
 #endif
 	int cpu;		/* Was running on cpu */
 	int pid;		/* Pid context */
+	unsigned long waste;	/* memory waste for a kmalloc-ed object */
 	unsigned long when;	/* When did the operation occur */
 };
 
@@ -747,6 +748,7 @@ static inline depot_stack_handle_t set_track_prepare(void)
 
 static void set_track_update(struct kmem_cache *s, void *object,
 			     enum track_item alloc, unsigned long addr,
+			     unsigned long waste,
 			     depot_stack_handle_t handle)
 {
 	struct track *p = get_track(s, object, alloc);
@@ -758,14 +760,16 @@ static void set_track_update(struct kmem_cache *s, void *object,
 	p->cpu = smp_processor_id();
 	p->pid = current->pid;
 	p->when = jiffies;
+	p->waste = waste;
 }
 
 static __always_inline void set_track(struct kmem_cache *s, void *object,
-				      enum track_item alloc, unsigned long addr)
+				      enum track_item alloc, unsigned long addr,
+				      unsigned long waste)
 {
 	depot_stack_handle_t handle = set_track_prepare();
 
-	set_track_update(s, object, alloc, addr, handle);
+	set_track_update(s, object, alloc, addr, waste, handle);
 }
 
 static void init_tracking(struct kmem_cache *s, void *object)
@@ -1325,7 +1329,9 @@ static inline int alloc_consistency_checks(struct kmem_cache *s,
 
 static noinline int alloc_debug_processing(struct kmem_cache *s,
 					struct slab *slab,
-					void *object, unsigned long addr)
+					void *object, unsigned long addr,
+					unsigned long waste
+					)
 {
 	if (s->flags & SLAB_CONSISTENCY_CHECKS) {
 		if (!alloc_consistency_checks(s, slab, object))
@@ -1334,7 +1340,7 @@ static noinline int alloc_debug_processing(struct kmem_cache *s,
 
 	/* Success perform special debug activities for allocs */
 	if (s->flags & SLAB_STORE_USER)
-		set_track(s, object, TRACK_ALLOC, addr);
+		set_track(s, object, TRACK_ALLOC, addr, waste);
 	trace(s, slab, object, 1);
 	init_object(s, object, SLUB_RED_ACTIVE);
 	return 1;
@@ -1418,7 +1424,7 @@ static noinline int free_debug_processing(
 	}
 
 	if (s->flags & SLAB_STORE_USER)
-		set_track_update(s, object, TRACK_FREE, addr, handle);
+		set_track_update(s, object, TRACK_FREE, addr, 0, handle);
 	trace(s, slab, object, 0);
 	/* Freepointer not overwritten by init_object(), SLAB_POISON moved it */
 	init_object(s, object, SLUB_RED_INACTIVE);
@@ -1661,7 +1667,8 @@ static inline
 void setup_slab_debug(struct kmem_cache *s, struct slab *slab, void *addr) {}
 
 static inline int alloc_debug_processing(struct kmem_cache *s,
-	struct slab *slab, void *object, unsigned long addr) { return 0; }
+	struct slab *slab, void *object, unsigned long addr,
+	unsigned long waste) { return 0; }
 
 static inline int free_debug_processing(
 	struct kmem_cache *s, struct slab *slab,
@@ -2905,7 +2912,7 @@ static inline void *get_freelist(struct kmem_cache *s, struct slab *slab)
  * already disabled (which is the case for bulk allocation).
  */
 static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
-			  unsigned long addr, struct kmem_cache_cpu *c)
+			  unsigned long addr, struct kmem_cache_cpu *c, unsigned int orig_size)
 {
 	void *freelist;
 	struct slab *slab;
@@ -3048,7 +3055,7 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 check_new_slab:
 
 	if (kmem_cache_debug(s)) {
-		if (!alloc_debug_processing(s, slab, freelist, addr)) {
+		if (!alloc_debug_processing(s, slab, freelist, addr, s->object_size - orig_size)) {
 			/* Slab failed checks. Next slab needed */
 			goto new_slab;
 		} else {
@@ -3102,7 +3109,7 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  * pointer.
  */
 static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
-			  unsigned long addr, struct kmem_cache_cpu *c)
+			  unsigned long addr, struct kmem_cache_cpu *c, unsigned int orig_size)
 {
 	void *p;
 
@@ -3115,7 +3122,7 @@ static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 	c = slub_get_cpu_ptr(s->cpu_slab);
 #endif
 
-	p = ___slab_alloc(s, gfpflags, node, addr, c);
+	p = ___slab_alloc(s, gfpflags, node, addr, c, orig_size);
 #ifdef CONFIG_PREEMPT_COUNT
 	slub_put_cpu_ptr(s->cpu_slab);
 #endif
@@ -3206,7 +3213,7 @@ static __always_inline void *slab_alloc_node(struct kmem_cache *s, struct list_l
 	 */
 	if (IS_ENABLED(CONFIG_PREEMPT_RT) ||
 	    unlikely(!object || !slab || !node_match(slab, node))) {
-		object = __slab_alloc(s, gfpflags, node, addr, c);
+		object = __slab_alloc(s, gfpflags, node, addr, c, orig_size);
 	} else {
 		void *next_object = get_freepointer_safe(s, object);
 
@@ -3731,7 +3738,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
 			 * of re-populating per CPU c->freelist
 			 */
 			p[i] = ___slab_alloc(s, flags, NUMA_NO_NODE,
-					    _RET_IP_, c);
+					    _RET_IP_, c, s->object_size);
 			if (unlikely(!p[i]))
 				goto error;
 
@@ -5092,6 +5099,7 @@ struct location {
 	depot_stack_handle_t handle;
 	unsigned long count;
 	unsigned long addr;
+	unsigned long waste;
 	long long sum_time;
 	long min_time;
 	long max_time;
@@ -5142,7 +5150,7 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
 {
 	long start, end, pos;
 	struct location *l;
-	unsigned long caddr, chandle;
+	unsigned long caddr, chandle, cwaste;
 	unsigned long age = jiffies - track->when;
 	depot_stack_handle_t handle = 0;
 
@@ -5162,11 +5170,13 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
 		if (pos == end)
 			break;
 
-		caddr = t->loc[pos].addr;
-		chandle = t->loc[pos].handle;
-		if ((track->addr == caddr) && (handle == chandle)) {
+		l = &t->loc[pos];
+		caddr = l->addr;
+		chandle = l->handle;
+		cwaste = l->waste;
+		if ((track->addr == caddr) && (handle == chandle) &&
+			(track->waste == cwaste)) {
 
-			l = &t->loc[pos];
 			l->count++;
 			if (track->when) {
 				l->sum_time += age;
@@ -5191,6 +5201,9 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
 			end = pos;
 		else if (track->addr == caddr && handle < chandle)
 			end = pos;
+		else if (track->addr == caddr && handle == chandle &&
+				track->waste < cwaste)
+			end = pos;
 		else
 			start = pos;
 	}
@@ -5214,6 +5227,7 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
 	l->min_pid = track->pid;
 	l->max_pid = track->pid;
 	l->handle = handle;
+	l->waste = track->waste;
 	cpumask_clear(to_cpumask(l->cpus));
 	cpumask_set_cpu(track->cpu, to_cpumask(l->cpus));
 	nodes_clear(l->nodes);
@@ -6102,6 +6116,10 @@ static int slab_debugfs_show(struct seq_file *seq, void *v)
 		else
 			seq_puts(seq, "<not-available>");
 
+		if (l->waste)
+			seq_printf(seq, " waste=%lu/%lu",
+				l->count * l->waste, l->waste);
+
 		if (l->sum_time != l->min_time) {
 			seq_printf(seq, " age=%ld/%llu/%ld",
 				l->min_time, div_u64(l->sum_time, l->count),
author
#8 lore raw
Quoted 1 line · Vlastimil Babka

On 7/1/22 15:59, Feng Tang wrote:

[...]

Quoted 20 lines
Quoted 20 lines

The waste info is integrated into existing interface:
/sys/kernel/debug/slab/kmalloc-xx/alloc_traces, one example of 'kmalloc-4k' after boot is:

126 ixgbe_alloc_q_vector+0xa5/0x4a0 [ixgbe] waste=233856/1856 age=1493302/1493830/1494358 pid=1284 cpus=32 nodes=1

        __slab_alloc.isra.86+0x52/0x80
        __kmalloc_node+0x143/0x350
        ixgbe_alloc_q_vector+0xa5/0x4a0 [ixgbe]
        ixgbe_init_interrupt_scheme+0x1a6/0x730 [ixgbe]
        ixgbe_probe+0xc8e/0x10d0 [ixgbe]
        local_pci_probe+0x42/0x80
        work_for_cpu_fn+0x13/0x20
        process_one_work+0x1c5/0x390

which means in 'kmalloc-4k' slab, there are 126 requests of 2240 bytes which got a 4KB space (wasting 1856 bytes each and 233856 bytes in total). And when system starts some real workload like multiple docker instances, there are more severe waste.

Quoted 7 lines
Quoted 4 lines

[1]. https://lkml.org/lkml/2019/8/12/266
[2]. https://lore.kernel.org/lkml/[email protected]/

Hi and thanks.
I would suggest some improvements to consider:

Hi Vlastimil,

Thanks for the review and detailed suggestions!

Quoted 8 lines

- don't use the struct track to store orig_size, although it's an obvious first choice. It's unused waste for the free_track, and also for any non-kmalloc caches. I'd carve out an extra int next to the struct tracks. Only for kmalloc caches (probably a new kmem cache flag set on creation will be needed to easily distinguish them).
Besides the saved space, you can then set the field from ___slab_alloc() directly and not need to pass the orig_size also to alloc_debug_processing() etc.

Do you mean decouple 'track' and the 'orig_size'(waste info), and add 'orig_size' for each kmalloc object (with the help of flag)? This solution depends hugely on 'track' framework.

Initially when implementing it, I met several problems:
1. where to save the orig_size(waste)
2. how to calcuate the waste info
3. where to show the waste info
4. how to show the full call stack for users' easy handling

I thought about saving the global waste in kmem_cache, and show it in 'slabinfo', then we have to handle it in all free calls or loop all objects in-use when 'cat slabinfo', still it lacks the detail info of each call stacks (some call points can be called with different sizes). And the 'track' seems to be ideal to solve them.

Or based on your suggestion, we still add 'waste' into 'track' but under a kernel config option 'SLUB_DEBUG_WASTE' for space saving.

Also I checked the struct track, it is well packed, only the 'int cpu' could be hacked, that it could spare 16 bits for storing the waste info:

struct track {
    ...
    unsigned short cpu; /* 0-65535 */
    unsigned short waste;
    ...
}
Quoted 4 lines

- the knowledge of actual size could be used to improve poisoning checks as well, detect cases when there's buffer overrun over the orig_size but not cache's size. e.g. if you kmalloc(48) and overrun up to 64 we won't detect it now, but with orig_size stored we could?

Yes! This could be imporoved.

Thanks,
Feng

Quoted 211 lines

Thanks!
Vlastimil

Quoted 208 lines

---
Changelog:

since RFC
* fix problems in kmem_cache_alloc_bulk() and records sorting, improve the print format (Hyeonggon Yoo)
* fix a compiling issue found by 0Day bot
* update the commit log based info from iova developers

 mm/slub.c | 52 +++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 35 insertions(+), 17 deletions(-)
mm/slub.c +35 -17
index b1281b8654bd3..97304ea1e6aa5 100644
@@ -271,6 +271,7 @@ struct track {
 #endif
 	int cpu;		/* Was running on cpu */
 	int pid;		/* Pid context */
+	unsigned long waste;	/* memory waste for a kmalloc-ed object */
 	unsigned long when;	/* When did the operation occur */
 };
 
@@ -747,6 +748,7 @@ static inline depot_stack_handle_t set_track_prepare(void)
 
 static void set_track_update(struct kmem_cache *s, void *object,
 			     enum track_item alloc, unsigned long addr,
+			     unsigned long waste,
 			     depot_stack_handle_t handle)
 {
 	struct track *p = get_track(s, object, alloc);
@@ -758,14 +760,16 @@ static void set_track_update(struct kmem_cache *s, void *object,
 	p->cpu = smp_processor_id();
 	p->pid = current->pid;
 	p->when = jiffies;
+	p->waste = waste;
 }
 
 static __always_inline void set_track(struct kmem_cache *s, void *object,
-				      enum track_item alloc, unsigned long addr)
+				      enum track_item alloc, unsigned long addr,
+				      unsigned long waste)
 {
 	depot_stack_handle_t handle = set_track_prepare();
 
-	set_track_update(s, object, alloc, addr, handle);
+	set_track_update(s, object, alloc, addr, waste, handle);
 }
 
 static void init_tracking(struct kmem_cache *s, void *object)
@@ -1325,7 +1329,9 @@ static inline int alloc_consistency_checks(struct kmem_cache *s,
 
 static noinline int alloc_debug_processing(struct kmem_cache *s,
 					struct slab *slab,
-					void *object, unsigned long addr)
+					void *object, unsigned long addr,
+					unsigned long waste
+					)
 {
 	if (s->flags & SLAB_CONSISTENCY_CHECKS) {
 		if (!alloc_consistency_checks(s, slab, object))
@@ -1334,7 +1340,7 @@ static noinline int alloc_debug_processing(struct kmem_cache *s,
 
 	/* Success perform special debug activities for allocs */
 	if (s->flags & SLAB_STORE_USER)
-		set_track(s, object, TRACK_ALLOC, addr);
+		set_track(s, object, TRACK_ALLOC, addr, waste);
 	trace(s, slab, object, 1);
 	init_object(s, object, SLUB_RED_ACTIVE);
 	return 1;
@@ -1418,7 +1424,7 @@ static noinline int free_debug_processing(
 	}
 
 	if (s->flags & SLAB_STORE_USER)
-		set_track_update(s, object, TRACK_FREE, addr, handle);
+		set_track_update(s, object, TRACK_FREE, addr, 0, handle);
 	trace(s, slab, object, 0);
 	/* Freepointer not overwritten by init_object(), SLAB_POISON moved it */
 	init_object(s, object, SLUB_RED_INACTIVE);
@@ -1661,7 +1667,8 @@ static inline
 void setup_slab_debug(struct kmem_cache *s, struct slab *slab, void *addr) {}
 
 static inline int alloc_debug_processing(struct kmem_cache *s,
-	struct slab *slab, void *object, unsigned long addr) { return 0; }
+	struct slab *slab, void *object, unsigned long addr,
+	unsigned long waste) { return 0; }
 
 static inline int free_debug_processing(
 	struct kmem_cache *s, struct slab *slab,
@@ -2905,7 +2912,7 @@ static inline void *get_freelist(struct kmem_cache *s, struct slab *slab)
  * already disabled (which is the case for bulk allocation).
  */
 static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
-			  unsigned long addr, struct kmem_cache_cpu *c)
+			  unsigned long addr, struct kmem_cache_cpu *c, unsigned int orig_size)
 {
 	void *freelist;
 	struct slab *slab;
@@ -3048,7 +3055,7 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 check_new_slab:
 
 	if (kmem_cache_debug(s)) {
-		if (!alloc_debug_processing(s, slab, freelist, addr)) {
+		if (!alloc_debug_processing(s, slab, freelist, addr, s->object_size - orig_size)) {
 			/* Slab failed checks. Next slab needed */
 			goto new_slab;
 		} else {
@@ -3102,7 +3109,7 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  * pointer.
  */
 static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
-			  unsigned long addr, struct kmem_cache_cpu *c)
+			  unsigned long addr, struct kmem_cache_cpu *c, unsigned int orig_size)
 {
 	void *p;
 
@@ -3115,7 +3122,7 @@ static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
 	c = slub_get_cpu_ptr(s->cpu_slab);
 #endif
 
-	p = ___slab_alloc(s, gfpflags, node, addr, c);
+	p = ___slab_alloc(s, gfpflags, node, addr, c, orig_size);
 #ifdef CONFIG_PREEMPT_COUNT
 	slub_put_cpu_ptr(s->cpu_slab);
 #endif
@@ -3206,7 +3213,7 @@ static __always_inline void *slab_alloc_node(struct kmem_cache *s, struct list_l
 	 */
 	if (IS_ENABLED(CONFIG_PREEMPT_RT) ||
 	    unlikely(!object || !slab || !node_match(slab, node))) {
-		object = __slab_alloc(s, gfpflags, node, addr, c);
+		object = __slab_alloc(s, gfpflags, node, addr, c, orig_size);
 	} else {
 		void *next_object = get_freepointer_safe(s, object);
 
@@ -3731,7 +3738,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
 			 * of re-populating per CPU c->freelist
 			 */
 			p[i] = ___slab_alloc(s, flags, NUMA_NO_NODE,
-					    _RET_IP_, c);
+					    _RET_IP_, c, s->object_size);
 			if (unlikely(!p[i]))
 				goto error;
 
@@ -5092,6 +5099,7 @@ struct location {
 	depot_stack_handle_t handle;
 	unsigned long count;
 	unsigned long addr;
+	unsigned long waste;
 	long long sum_time;
 	long min_time;
 	long max_time;
@@ -5142,7 +5150,7 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
 {
 	long start, end, pos;
 	struct location *l;
-	unsigned long caddr, chandle;
+	unsigned long caddr, chandle, cwaste;
 	unsigned long age = jiffies - track->when;
 	depot_stack_handle_t handle = 0;
 
@@ -5162,11 +5170,13 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
 		if (pos == end)
 			break;
 
-		caddr = t->loc[pos].addr;
-		chandle = t->loc[pos].handle;
-		if ((track->addr == caddr) && (handle == chandle)) {
+		l = &t->loc[pos];
+		caddr = l->addr;
+		chandle = l->handle;
+		cwaste = l->waste;
+		if ((track->addr == caddr) && (handle == chandle) &&
+			(track->waste == cwaste)) {
 
-			l = &t->loc[pos];
 			l->count++;
 			if (track->when) {
 				l->sum_time += age;
@@ -5191,6 +5201,9 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
 			end = pos;
 		else if (track->addr == caddr && handle < chandle)
 			end = pos;
+		else if (track->addr == caddr && handle == chandle &&
+				track->waste < cwaste)
+			end = pos;
 		else
 			start = pos;
 	}
@@ -5214,6 +5227,7 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
 	l->min_pid = track->pid;
 	l->max_pid = track->pid;
 	l->handle = handle;
+	l->waste = track->waste;
 	cpumask_clear(to_cpumask(l->cpus));
 	cpumask_set_cpu(track->cpu, to_cpumask(l->cpus));
 	nodes_clear(l->nodes);
@@ -6102,6 +6116,10 @@ static int slab_debugfs_show(struct seq_file *seq, void *v)
 		else
 			seq_puts(seq, "<not-available>");
 
+		if (l->waste)
+			seq_printf(seq, " waste=%lu/%lu",
+				l->count * l->waste, l->waste);
+
 		if (l->sum_time != l->min_time) {
 			seq_printf(seq, " age=%ld/%llu/%ld",
 				l->min_time, div_u64(l->sum_time, l->count),
author
#9 lore raw
In reply to Vlastimil Babka

Hi Vlastimil,

Quoted 15 lines · Vlastimil Babka
Quoted 14 lines · Feng Tang

kmalloc's API family is critical for mm, with one shortcoming that its object size is fixed to be power of 2. When user requests memory for '2^n + 1' bytes, actually 2^(n+1) bytes will be allocated, so in worst case, there is around 50% memory space waste.

We've met a kernel boot OOM panic (v5.10), and from the dumped slab info:

    [   26.062145] kmalloc-2k            814056KB     814056KB

From debug we found there are huge number of 'struct iova_magazine', whose size is 1032 bytes (1024 + 8), so each allocation will waste 1016 bytes. Though the issue was solved by giving the right (bigger) size of RAM, it is still nice to optimize the size (either use a kmalloc friendly size or create a dedicated slab for it).

[...]

Quoted 4 lines

Hi and thanks.
I would suggest some improvements to consider:

Quoted 8 lines

- don't use the struct track to store orig_size, although it's an obvious first choice. It's unused waste for the free_track, and also for any non-kmalloc caches. I'd carve out an extra int next to the struct tracks. Only for kmalloc caches (probably a new kmem cache flag set on creation will be needed to easily distinguish them).
Besides the saved space, you can then set the field from ___slab_alloc() directly and not need to pass the orig_size also to alloc_debug_processing() etc.

Here is a draft patch fowlling your suggestion, please check if I missed anything? (Quick test showed it achived similar effect as v1 patch). Thanks!

---

include/linux/slab.h +2 -0
index 0fefdf528e0d..d3dacb0f013f 100644
@@ -29,6 +29,8 @@
 #define SLAB_RED_ZONE		((slab_flags_t __force)0x00000400U)
 /* DEBUG: Poison objects */
 #define SLAB_POISON		((slab_flags_t __force)0x00000800U)
+/* Indicate a slab of kmalloc */
+#define SLAB_KMALLOC		((slab_flags_t __force)0x00001000U)
 /* Align objs on cache lines */
 #define SLAB_HWCACHE_ALIGN	((slab_flags_t __force)0x00002000U)
 /* Use GFP_DMA memory */
mm/slub.c +70 -16

208 lines not on this page · raw message

Quoted 4 lines

- the knowledge of actual size could be used to improve poisoning checks as well, detect cases when there's buffer overrun over the orig_size but not cache's size. e.g. if you kmalloc(48) and overrun up to 64 we won't detect it now, but with orig_size stored we could?

The above patch doesn't touch this. As I have a question, for the [orib_size, object_size) area, shall we fill it with POISON_XXX no matter REDZONE flag is set or not?

Thanks,
Feng

Quoted 2 lines

Thanks!
Vlastimil

#10 lore raw
Quoted 32 lines · Feng Tang

Hi Vlastimil,

Quoted 15 lines · Vlastimil Babka
Quoted 14 lines · Feng Tang

kmalloc's API family is critical for mm, with one shortcoming that its object size is fixed to be power of 2. When user requests memory for '2^n + 1' bytes, actually 2^(n+1) bytes will be allocated, so in worst case, there is around 50% memory space waste.

We've met a kernel boot OOM panic (v5.10), and from the dumped slab info:

    [   26.062145] kmalloc-2k            814056KB     814056KB

From debug we found there are huge number of 'struct iova_magazine', whose size is 1032 bytes (1024 + 8), so each allocation will waste 1016 bytes. Though the issue was solved by giving the right (bigger) size of RAM, it is still nice to optimize the size (either use a kmalloc friendly size or create a dedicated slab for it).

[...]

Quoted 4 lines

Hi and thanks.
I would suggest some improvements to consider:

Quoted 8 lines

- don't use the struct track to store orig_size, although it's an obvious first choice. It's unused waste for the free_track, and also for any non-kmalloc caches. I'd carve out an extra int next to the struct tracks. Only for kmalloc caches (probably a new kmem cache flag set on creation will be needed to easily distinguish them).
Besides the saved space, you can then set the field from ___slab_alloc() directly and not need to pass the orig_size also to alloc_debug_processing() etc.

Quoted 2 lines

Here is a draft patch fowlling your suggestion, please check if I missed anything? (Quick test showed it achived similar effect as v1 patch). Thanks!

Thanks, overal it looks at first glance!

include/linux/slab.h line 32

Quoted 10 lines

---

include/linux/slab.h +1 -0
index 0fefdf528e0d..d3dacb0f013f 100644
@@ -29,6 +29,8 @@
 #define SLAB_RED_ZONE		((slab_flags_t __force)0x00000400U)
 /* DEBUG: Poison objects */
 #define SLAB_POISON		((slab_flags_t __force)0x00000800U)
+/* Indicate a slab of kmalloc */

"Indicate a kmalloc cache" would be more precise.

Quoted 6 lines
include/linux/slab.h +1 -0
+#define SLAB_KMALLOC		((slab_flags_t __force)0x00001000U)
 /* Align objs on cache lines */
 #define SLAB_HWCACHE_ALIGN	((slab_flags_t __force)0x00002000U)
 /* Use GFP_DMA memory */
mm/slub.c +0 -0
index 26b00951aad1..3b0f80927817 100644

<snip>

Quoted 9 lines
Quoted 5 lines

- the knowledge of actual size could be used to improve poisoning checks as well, detect cases when there's buffer overrun over the orig_size but not cache's size. e.g. if you kmalloc(48) and overrun up to 64 we won't detect it now, but with orig_size stored we could?

The above patch doesn't touch this. As I have a question, for the [orib_size, object_size) area, shall we fill it with POISON_XXX no matter REDZONE flag is set or not?

Ah, looks like we use redzoning, not poisoning, for padding from s->object_size to word boundary. So it would be more consistent to use the redzone pattern (RED_ACTIVE) and check with the dynamic orig_size. Probably no change for RED_INACTIVE handling is needed though.

Quoted 5 lines

Thanks,
Feng

Quoted 2 lines

Thanks!
Vlastimil

author
#11 lore raw
Quoted 33 lines · Vlastimil Babka
Quoted 32 lines · Feng Tang

Hi Vlastimil,

Quoted 15 lines · Vlastimil Babka
Quoted 14 lines · Feng Tang

kmalloc's API family is critical for mm, with one shortcoming that its object size is fixed to be power of 2. When user requests memory for '2^n + 1' bytes, actually 2^(n+1) bytes will be allocated, so in worst case, there is around 50% memory space waste.

We've met a kernel boot OOM panic (v5.10), and from the dumped slab info:

    [   26.062145] kmalloc-2k            814056KB     814056KB

From debug we found there are huge number of 'struct iova_magazine', whose size is 1032 bytes (1024 + 8), so each allocation will waste 1016 bytes. Though the issue was solved by giving the right (bigger) size of RAM, it is still nice to optimize the size (either use a kmalloc friendly size or create a dedicated slab for it).

[...]

Quoted 12 lines

Hi and thanks.
I would suggest some improvements to consider:

- don't use the struct track to store orig_size, although it's an obvious first choice. It's unused waste for the free_track, and also for any non-kmalloc caches. I'd carve out an extra int next to the struct tracks. Only for kmalloc caches (probably a new kmem cache flag set on creation will be needed to easily distinguish them).
Besides the saved space, you can then set the field from ___slab_alloc() directly and not need to pass the orig_size also to alloc_debug_processing() etc.

Quoted 4 lines
Quoted 2 lines

Here is a draft patch fowlling your suggestion, please check if I missed anything? (Quick test showed it achived similar effect as v1 patch). Thanks!

Thanks, overal it looks at first glance!

Thanks!

Quoted 4 lines
Quoted 4 lines

---

include/linux/slab.h +0 -1
index 0fefdf528e0d..d3dacb0f013f 100644
--- a/include/linux/slab.h
Quoted 8 lines

include/linux/slab.h line 32

Quoted 6 lines

+++ b/include/linux/slab.h

include/linux/slab.h +1 -0
@@ -29,6 +29,8 @@
 #define SLAB_RED_ZONE		((slab_flags_t __force)0x00000400U)
 /* DEBUG: Poison objects */
 #define SLAB_POISON		((slab_flags_t __force)0x00000800U)
+/* Indicate a slab of kmalloc */

"Indicate a kmalloc cache" would be more precise.

Will use this in next version.

Quoted 15 lines
Quoted 6 lines
include/linux/slab.h +1 -0
+#define SLAB_KMALLOC		((slab_flags_t __force)0x00001000U)
 /* Align objs on cache lines */
 #define SLAB_HWCACHE_ALIGN	((slab_flags_t __force)0x00002000U)
 /* Use GFP_DMA memory */
mm/slub.c +0 -0
index 26b00951aad1..3b0f80927817 100644

<snip>

Quoted 6 lines
Quoted 5 lines

- the knowledge of actual size could be used to improve poisoning checks as well, detect cases when there's buffer overrun over the orig_size but not cache's size. e.g. if you kmalloc(48) and overrun up to 64 we won't detect it now, but with orig_size stored we could?

Quoted 8 lines
Quoted 3 lines

The above patch doesn't touch this. As I have a question, for the [orib_size, object_size) area, shall we fill it with POISON_XXX no matter REDZONE flag is set or not?

Ah, looks like we use redzoning, not poisoning, for padding from s->object_size to word boundary. So it would be more consistent to use the redzone pattern (RED_ACTIVE) and check with the dynamic orig_size. Probably no change for RED_INACTIVE handling is needed though.

Thanks for clarifying, will go this way and do more test. Also I'd make it a separate patch, as it is logically different from the space wastage.

Thanks,
Feng

author
#12 lore raw

Hi Vlastimil,

On Fri, Jul 15, 2022 at 04:29:22PM +0800, Tang, Feng wrote:
[...]

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 4 lines

- the knowledge of actual size could be used to improve poisoning checks as well, detect cases when there's buffer overrun over the orig_size but not cache's size. e.g. if you kmalloc(48) and overrun up to 64 we won't detect it now, but with orig_size stored we could?

The above patch doesn't touch this. As I have a question, for the [orib_size, object_size) area, shall we fill it with POISON_XXX no matter REDZONE flag is set or not?

Quoted 8 lines
Quoted 4 lines

Ah, looks like we use redzoning, not poisoning, for padding from s->object_size to word boundary. So it would be more consistent to use the redzone pattern (RED_ACTIVE) and check with the dynamic orig_size. Probably no change for RED_INACTIVE handling is needed though.

Thanks for clarifying, will go this way and do more test. Also I'd make it a separate patch, as it is logically different from the space wastage.

I made a draft to redzone the wasted space, which basically works (patch pasted at the end of the mail) as detecting corruption of below test code:

	size = 256;
	buf = kmalloc(size + 8, GFP_KERNEL);
	memset(buf + size + size/2, 0xff, size/4);
	print_section(KERN_ERR, "Corruptted-kmalloc-space", buf, size * 2);
	kfree(buf);

However when it is enabled globally, there are many places reporting corruption. I debugged one case, and found that the network(skb_buff) code already knows this "wasted" kmalloc space and utilize it which is detected by my patch.

The allocation stack is:

[    0.933675] BUG kmalloc-2k (Not tainted): kmalloc unused part overwritten
[    0.933675] -----------------------------------------------------------------------------
[    0.933675]
[    0.933675] 0xffff888237d026c0-0xffff888237d026e3 @offset=9920. First byte 0x0 instead of 0xcc
[    0.933675] Allocated in __alloc_skb+0x8e/0x1d0 age=5 cpu=0 pid=1
[    0.933675]  __slab_alloc.constprop.0+0x52/0x90
[    0.933675]  __kmalloc_node_track_caller+0x129/0x380
[    0.933675]  kmalloc_reserve+0x2a/0x70
[    0.933675]  __alloc_skb+0x8e/0x1d0
[    0.933675]  audit_buffer_alloc+0x3a/0xc0
[    0.933675]  audit_log_start.part.0+0xa3/0x300
[    0.933675]  audit_log+0x62/0xc0
[    0.933675]  audit_init+0x15c/0x16f

And the networking code which touches the [orig_size, object_size) area is in __build_skb_around(), which put a 'struct skb_shared_info' at the end of this area:

	static void __build_skb_around(struct sk_buff *skb, void *data,
				       unsigned int frag_size)
	{
		struct skb_shared_info *shinfo;
		unsigned int size = frag_size ? : ksize(data);

		size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
		-----> XXX carve the space  <-----

		...
		skb_set_end_offset(skb, size);
		...

		shinfo = skb_shinfo(skb);
		memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
		atomic_set(&shinfo->dataref, 1);

		-----> upper 2 lines changes the memory <-----
		...
	}

Then we end up seeing the corruption report:

[    0.933675] Object   ffff888237d026c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
[    0.933675] Object   ffff888237d026d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
[    0.933675] Object   ffff888237d026e0: 01 00 00 00 cc cc cc cc cc cc cc cc cc cc cc cc  ................

I haven't got time to chase other cases, and would update these first.

Following is the draft (not cleaned patch) patch to redzone the [orig_size, object_size) space.

Thanks,
Feng

---

mm/slab.c +4 -4
index 6474c515a664..2f1110b16463 100644
@@ -3229,7 +3229,7 @@ slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid, size_t orig_
 	init = slab_want_init_on_alloc(flags, cachep);
 
 out_hooks:
-	slab_post_alloc_hook(cachep, objcg, flags, 1, &ptr, init);
+	slab_post_alloc_hook(cachep, objcg, flags, 1, &ptr, init, 0);
 	return ptr;
 }
 
@@ -3291,7 +3291,7 @@ slab_alloc(struct kmem_cache *cachep, struct list_lru *lru, gfp_t flags,
 	init = slab_want_init_on_alloc(flags, cachep);
 
 out:
-	slab_post_alloc_hook(cachep, objcg, flags, 1, &objp, init);
+	slab_post_alloc_hook(cachep, objcg, flags, 1, &objp, init, 0);
 	return objp;
 }
 
@@ -3536,13 +3536,13 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
 	 * Done outside of the IRQ disabled section.
 	 */
 	slab_post_alloc_hook(s, objcg, flags, size, p,
-				slab_want_init_on_alloc(flags, s));
+				slab_want_init_on_alloc(flags, s), 0);
 	/* FIXME: Trace call missing. Christoph would like a bulk variant */
 	return size;
 error:
 	local_irq_enable();
 	cache_alloc_debugcheck_after_bulk(s, flags, i, p, _RET_IP_);
-	slab_post_alloc_hook(s, objcg, flags, i, p, false);
+	slab_post_alloc_hook(s, objcg, flags, i, p, false, 0);
 	__kmem_cache_free_bulk(s, i, p);
 	return 0;
 }
mm/slab.h +7 -2
index a8d5eb1c323f..938ec6454dbc 100644
@@ -719,12 +719,17 @@ static inline struct kmem_cache *slab_pre_alloc_hook(struct kmem_cache *s,
 
 static inline void slab_post_alloc_hook(struct kmem_cache *s,
 					struct obj_cgroup *objcg, gfp_t flags,
-					size_t size, void **p, bool init)
+					size_t size, void **p, bool init,
+					unsigned int orig_size)
 {
 	size_t i;
 
 	flags &= gfp_allowed_mask;
 
+	/* If original request size(kmalloc) is not set, use object_size */
+	if (!orig_size)
+		orig_size = s->object_size;
+
 	/*
 	 * As memory initialization might be integrated into KASAN,
 	 * kasan_slab_alloc and initialization memset must be
@@ -735,7 +740,7 @@ static inline void slab_post_alloc_hook(struct kmem_cache *s,
 	for (i = 0; i < size; i++) {
 		p[i] = kasan_slab_alloc(s, p[i], flags, init);
 		if (p[i] && init && !kasan_has_integrated_init())
-			memset(p[i], 0, s->object_size);
+			memset(p[i], 0, orig_size);
 		kmemleak_alloc_recursive(p[i], s->object_size, 1,
 					 s->flags, flags);
 	}
mm/slub.c +62 -11

163 lines not on this page · raw message

#13 lore raw
Quoted 56 lines · Feng Tang

Hi Vlastimil,

On Fri, Jul 15, 2022 at 04:29:22PM +0800, Tang, Feng wrote:
[...]

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 4 lines

- the knowledge of actual size could be used to improve poisoning checks as well, detect cases when there's buffer overrun over the orig_size but not cache's size. e.g. if you kmalloc(48) and overrun up to 64 we won't detect it now, but with orig_size stored we could?

The above patch doesn't touch this. As I have a question, for the [orib_size, object_size) area, shall we fill it with POISON_XXX no matter REDZONE flag is set or not?

Quoted 8 lines
Quoted 4 lines

Ah, looks like we use redzoning, not poisoning, for padding from s->object_size to word boundary. So it would be more consistent to use the redzone pattern (RED_ACTIVE) and check with the dynamic orig_size. Probably no change for RED_INACTIVE handling is needed though.

Thanks for clarifying, will go this way and do more test. Also I'd make it a separate patch, as it is logically different from the space wastage.

I made a draft to redzone the wasted space, which basically works (patch pasted at the end of the mail) as detecting corruption of below test code:

	size = 256;
	buf = kmalloc(size + 8, GFP_KERNEL);
	memset(buf + size + size/2, 0xff, size/4);
	print_section(KERN_ERR, "Corruptted-kmalloc-space", buf, size * 2);
	kfree(buf);

However when it is enabled globally, there are many places reporting corruption. I debugged one case, and found that the network(skb_buff) code already knows this "wasted" kmalloc space and utilize it which is detected by my patch.

The allocation stack is:

[    0.933675] BUG kmalloc-2k (Not tainted): kmalloc unused part overwritten
[    0.933675] -----------------------------------------------------------------------------
[    0.933675]
[    0.933675] 0xffff888237d026c0-0xffff888237d026e3 @offset=9920. First byte 0x0 instead of 0xcc
[    0.933675] Allocated in __alloc_skb+0x8e/0x1d0 age=5 cpu=0 pid=1
[    0.933675]  __slab_alloc.constprop.0+0x52/0x90
[    0.933675]  __kmalloc_node_track_caller+0x129/0x380
[    0.933675]  kmalloc_reserve+0x2a/0x70
[    0.933675]  __alloc_skb+0x8e/0x1d0
[    0.933675]  audit_buffer_alloc+0x3a/0xc0
[    0.933675]  audit_log_start.part.0+0xa3/0x300
[    0.933675]  audit_log+0x62/0xc0
[    0.933675]  audit_init+0x15c/0x16f

And the networking code which touches the [orig_size, object_size) area is in __build_skb_around(), which put a 'struct skb_shared_info' at the end of this area:

Quoted 5 lines
	static void __build_skb_around(struct sk_buff *skb, void *data,
				       unsigned int frag_size)
	{
		struct skb_shared_info *shinfo;
		unsigned int size = frag_size ? : ksize(data);

Hmm so it's a ksize() user, which should be legitimate way to use the "waste" data. Hopefully it should be then enough to patch __ksize() to set the object's tracked waste to 0 (orig_size to size) - assume that if somebody called ksize() they intend to use the space. That would also make the debugfs report more truthful.

author
#14 lore raw
Quoted 63 lines · Vlastimil Babka
Quoted 56 lines · Feng Tang

Hi Vlastimil,

On Fri, Jul 15, 2022 at 04:29:22PM +0800, Tang, Feng wrote:
[...]

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 4 lines

- the knowledge of actual size could be used to improve poisoning checks as well, detect cases when there's buffer overrun over the orig_size but not cache's size. e.g. if you kmalloc(48) and overrun up to 64 we won't detect it now, but with orig_size stored we could?

The above patch doesn't touch this. As I have a question, for the [orib_size, object_size) area, shall we fill it with POISON_XXX no matter REDZONE flag is set or not?

Quoted 8 lines
Quoted 4 lines

Ah, looks like we use redzoning, not poisoning, for padding from s->object_size to word boundary. So it would be more consistent to use the redzone pattern (RED_ACTIVE) and check with the dynamic orig_size. Probably no change for RED_INACTIVE handling is needed though.

Thanks for clarifying, will go this way and do more test. Also I'd make it a separate patch, as it is logically different from the space wastage.

I made a draft to redzone the wasted space, which basically works (patch pasted at the end of the mail) as detecting corruption of below test code:

	size = 256;
	buf = kmalloc(size + 8, GFP_KERNEL);
	memset(buf + size + size/2, 0xff, size/4);
	print_section(KERN_ERR, "Corruptted-kmalloc-space", buf, size * 2);
	kfree(buf);

However when it is enabled globally, there are many places reporting corruption. I debugged one case, and found that the network(skb_buff) code already knows this "wasted" kmalloc space and utilize it which is detected by my patch.

The allocation stack is:

[    0.933675] BUG kmalloc-2k (Not tainted): kmalloc unused part overwritten
[    0.933675] -----------------------------------------------------------------------------
[    0.933675]
[    0.933675] 0xffff888237d026c0-0xffff888237d026e3 @offset=9920. First byte 0x0 instead of 0xcc
[    0.933675] Allocated in __alloc_skb+0x8e/0x1d0 age=5 cpu=0 pid=1
[    0.933675]  __slab_alloc.constprop.0+0x52/0x90
[    0.933675]  __kmalloc_node_track_caller+0x129/0x380
[    0.933675]  kmalloc_reserve+0x2a/0x70
[    0.933675]  __alloc_skb+0x8e/0x1d0
[    0.933675]  audit_buffer_alloc+0x3a/0xc0
[    0.933675]  audit_log_start.part.0+0xa3/0x300
[    0.933675]  audit_log+0x62/0xc0
[    0.933675]  audit_init+0x15c/0x16f

And the networking code which touches the [orig_size, object_size) area is in __build_skb_around(), which put a 'struct skb_shared_info' at the end of this area:

Quoted 5 lines
	static void __build_skb_around(struct sk_buff *skb, void *data,
				       unsigned int frag_size)
	{
		struct skb_shared_info *shinfo;
		unsigned int size = frag_size ? : ksize(data);
Quoted 5 lines

Hmm so it's a ksize() user, which should be legitimate way to use the "waste" data. Hopefully it should be then enough to patch __ksize() to set the object's tracked waste to 0 (orig_size to size) - assume that if somebody called ksize() they intend to use the space. That would also make the debugfs report more truthful.

Yep, it sounds good to me. Will chase other corrupted places, hope they are legitimate users too :)

Thanks,
Feng