Skip to content

Commit e993265

Browse files
Zhaoyang Huangakpm00
Zhaoyang Huang
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. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Zhaoyang Huang <[email protected]> Acked-by: Catalin Marinas <[email protected]> Cc: ke.wang <[email protected]> Cc: Matthew Wilcox (Oracle) <[email protected]> Cc: Vlastimil Babka <[email protected]> Cc: Zhaoyang Huang <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 4b4b89c commit e993265

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

mm/kmemleak.c

+35-10
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,8 +346,11 @@ 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",
352355
object->pointer, object->size);
353356
warn_or_seq_printf(seq, " comm \"%s\", pid %d, jiffies %lu (age %d.%03ds)\n",
@@ -356,10 +359,10 @@ static void print_unreferenced(struct seq_file *seq,
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];
361-
warn_or_seq_printf(seq, " [<%p>] %pS\n", ptr, ptr);
362-
}
362+
for (i = 0; i < nr_entries; i++) {
363+
void *ptr = (void *)entries[i];
364+
warn_or_seq_printf(seq, " [<%p>] %pS\n", ptr, ptr);
365+
}
363366
}
364367

365368
/*
@@ -378,7 +381,8 @@ static void dump_object_info(struct kmemleak_object *object)
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,6 +595,27 @@ static struct kmemleak_object *find_and_remove_object(unsigned long ptr, int ali
591595
return object;
592596
}
593597

598+
#ifdef CONFIG_STACKDEPOT
599+
static noinline depot_stack_handle_t set_track_prepare(void)
600+
{
601+
depot_stack_handle_t trace_handle;
602+
unsigned long entries[MAX_TRACE];
603+
unsigned int nr_entries;
604+
605+
if (!kmemleak_initialized)
606+
return 0;
607+
nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 3);
608+
trace_handle = stack_depot_save(entries, nr_entries, GFP_NOWAIT);
609+
610+
return trace_handle;
611+
}
612+
#else
613+
static inline depot_stack_handle_t set_track_prepare(void)
614+
{
615+
return 0;
616+
}
617+
#endif
618+
594619
/*
595620
* Save stack trace to the given array of MAX_TRACE size.
596621
*/
@@ -653,7 +678,7 @@ static void __create_object(unsigned long ptr, size_t size,
653678
}
654679

655680
/* kernel backtrace */
656-
object->trace_len = __save_stack_trace(object->trace);
681+
object->trace_handle = set_track_prepare();
657682

658683
raw_spin_lock_irqsave(&kmemleak_lock, flags);
659684

@@ -692,7 +717,6 @@ static void __create_object(unsigned long ptr, size_t size,
692717
rb_link_node(&object->rb_node, rb_parent, link);
693718
rb_insert_color(&object->rb_node, is_phys ? &object_phys_tree_root :
694719
&object_tree_root);
695-
696720
list_add_tail_rcu(&object->object_list, &object_list);
697721
out:
698722
raw_spin_unlock_irqrestore(&kmemleak_lock, flags);
@@ -1091,7 +1115,7 @@ void __ref kmemleak_update_trace(const void *ptr)
10911115
}
10921116

10931117
raw_spin_lock_irqsave(&object->lock, flags);
1094-
object->trace_len = __save_stack_trace(object->trace);
1118+
object->trace_handle = set_track_prepare();
10951119
raw_spin_unlock_irqrestore(&object->lock, flags);
10961120

10971121
put_object(object);
@@ -2084,6 +2108,7 @@ void __init kmemleak_init(void)
20842108
if (kmemleak_error)
20852109
return;
20862110

2111+
stack_depot_init();
20872112
jiffies_min_age = msecs_to_jiffies(MSECS_MIN_AGE);
20882113
jiffies_scan_wait = msecs_to_jiffies(SECS_SCAN_WAIT * 1000);
20892114

0 commit comments

Comments
 (0)