Skip to content

Commit 56a6161

Browse files
Zhaoyang Huangakpm00
authored andcommitted
mm: use stack_depot for recording kmemleak's backtrace
Using stack_depot to record kmemleak's backtrace which has been implemented on slub for reducing redundant information. [akpm@linux-foundation.org: fix build - remove now-unused __save_stack_trace()] [zhaoyang.huang@unisoc.com: v3] Link: https://lkml.kernel.org/r/1667101354-4669-1-git-send-email-zhaoyang.huang@unisoc.com [akpm@linux-foundation.org: fix v3 layout oddities] [akpm@linux-foundation.org: coding-style cleanups] Link: https://lkml.kernel.org/r/1666864224-27541-1-git-send-email-zhaoyang.huang@unisoc.com Signed-off-by: Zhaoyang Huang <zhaoyang.huang@unisoc.com> Acked-by: Catalin Marinas <catalin.marinas@arm.com> Cc: ke.wang <ke.wang@unisoc.com> Cc: Matthew Wilcox (Oracle) <willy@infradead.org> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: Zhaoyang Huang <huangzhaoyang@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 9102b78 commit 56a6161

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

lib/Kconfig.debug

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,7 @@ config DEBUG_KMEMLEAK
728728
select STACKTRACE if STACKTRACE_SUPPORT
729729
select KALLSYMS
730730
select CRC32
731+
select STACKDEPOT
731732
help
732733
Say Y here if you want to enable the memory leak
733734
detector. The memory allocation/freeing is traced in a way

mm/kmemleak.c

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
#include <linux/mutex.h>
8080
#include <linux/rcupdate.h>
8181
#include <linux/stacktrace.h>
82+
#include <linux/stackdepot.h>
8283
#include <linux/cache.h>
8384
#include <linux/percpu.h>
8485
#include <linux/memblock.h>
@@ -159,8 +160,7 @@ struct kmemleak_object {
159160
u32 checksum;
160161
/* memory ranges to be scanned inside an object (empty for all) */
161162
struct hlist_head area_list;
162-
unsigned long trace[MAX_TRACE];
163-
unsigned int trace_len;
163+
depot_stack_handle_t trace_handle;
164164
unsigned long jiffies; /* creation timestamp */
165165
pid_t pid; /* pid of the current task */
166166
char comm[TASK_COMM_LEN]; /* executable name */
@@ -346,18 +346,21 @@ static void print_unreferenced(struct seq_file *seq,
346346
struct kmemleak_object *object)
347347
{
348348
int i;
349+
unsigned long *entries;
350+
unsigned int nr_entries;
349351
unsigned int msecs_age = jiffies_to_msecs(jiffies - object->jiffies);
350352

353+
nr_entries = stack_depot_fetch(object->trace_handle, &entries);
351354
warn_or_seq_printf(seq, "unreferenced object 0x%08lx (size %zu):\n",
352-
object->pointer, object->size);
355+
object->pointer, object->size);
353356
warn_or_seq_printf(seq, " comm \"%s\", pid %d, jiffies %lu (age %d.%03ds)\n",
354-
object->comm, object->pid, object->jiffies,
355-
msecs_age / 1000, msecs_age % 1000);
357+
object->comm, object->pid, object->jiffies,
358+
msecs_age / 1000, msecs_age % 1000);
356359
hex_dump_object(seq, object);
357360
warn_or_seq_printf(seq, " backtrace:\n");
358361

359-
for (i = 0; i < object->trace_len; i++) {
360-
void *ptr = (void *)object->trace[i];
362+
for (i = 0; i < nr_entries; i++) {
363+
void *ptr = (void *)entries[i];
361364
warn_or_seq_printf(seq, " [<%p>] %pS\n", ptr, ptr);
362365
}
363366
}
@@ -370,15 +373,16 @@ static void print_unreferenced(struct seq_file *seq,
370373
static void dump_object_info(struct kmemleak_object *object)
371374
{
372375
pr_notice("Object 0x%08lx (size %zu):\n",
373-
object->pointer, object->size);
376+
object->pointer, object->size);
374377
pr_notice(" comm \"%s\", pid %d, jiffies %lu\n",
375-
object->comm, object->pid, object->jiffies);
378+
object->comm, object->pid, object->jiffies);
376379
pr_notice(" min_count = %d\n", object->min_count);
377380
pr_notice(" count = %d\n", object->count);
378381
pr_notice(" flags = 0x%x\n", object->flags);
379382
pr_notice(" checksum = %u\n", object->checksum);
380383
pr_notice(" backtrace:\n");
381-
stack_trace_print(object->trace, object->trace_len, 4);
384+
if (object->trace_handle)
385+
stack_depot_print(object->trace_handle);
382386
}
383387

384388
/*
@@ -591,12 +595,18 @@ static struct kmemleak_object *find_and_remove_object(unsigned long ptr, int ali
591595
return object;
592596
}
593597

594-
/*
595-
* Save stack trace to the given array of MAX_TRACE size.
596-
*/
597-
static int __save_stack_trace(unsigned long *trace)
598+
static noinline depot_stack_handle_t set_track_prepare(void)
598599
{
599-
return stack_trace_save(trace, MAX_TRACE, 2);
600+
depot_stack_handle_t trace_handle;
601+
unsigned long entries[MAX_TRACE];
602+
unsigned int nr_entries;
603+
604+
if (!kmemleak_initialized)
605+
return 0;
606+
nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 3);
607+
trace_handle = stack_depot_save(entries, nr_entries, GFP_NOWAIT);
608+
609+
return trace_handle;
600610
}
601611

602612
/*
@@ -653,7 +663,7 @@ static void __create_object(unsigned long ptr, size_t size,
653663
}
654664

655665
/* kernel backtrace */
656-
object->trace_len = __save_stack_trace(object->trace);
666+
object->trace_handle = set_track_prepare();
657667

658668
raw_spin_lock_irqsave(&kmemleak_lock, flags);
659669

@@ -692,7 +702,6 @@ static void __create_object(unsigned long ptr, size_t size,
692702
rb_link_node(&object->rb_node, rb_parent, link);
693703
rb_insert_color(&object->rb_node, is_phys ? &object_phys_tree_root :
694704
&object_tree_root);
695-
696705
list_add_tail_rcu(&object->object_list, &object_list);
697706
out:
698707
raw_spin_unlock_irqrestore(&kmemleak_lock, flags);
@@ -1091,7 +1100,7 @@ void __ref kmemleak_update_trace(const void *ptr)
10911100
}
10921101

10931102
raw_spin_lock_irqsave(&object->lock, flags);
1094-
object->trace_len = __save_stack_trace(object->trace);
1103+
object->trace_handle = set_track_prepare();
10951104
raw_spin_unlock_irqrestore(&object->lock, flags);
10961105

10971106
put_object(object);
@@ -2084,6 +2093,7 @@ void __init kmemleak_init(void)
20842093
if (kmemleak_error)
20852094
return;
20862095

2096+
stack_depot_init();
20872097
jiffies_min_age = msecs_to_jiffies(MSECS_MIN_AGE);
20882098
jiffies_scan_wait = msecs_to_jiffies(SECS_SCAN_WAIT * 1000);
20892099

0 commit comments

Comments
 (0)