79
79
#include <linux/mutex.h>
80
80
#include <linux/rcupdate.h>
81
81
#include <linux/stacktrace.h>
82
+ #include <linux/stackdepot.h>
82
83
#include <linux/cache.h>
83
84
#include <linux/percpu.h>
84
85
#include <linux/memblock.h>
@@ -159,8 +160,7 @@ struct kmemleak_object {
159
160
u32 checksum ;
160
161
/* memory ranges to be scanned inside an object (empty for all) */
161
162
struct hlist_head area_list ;
162
- unsigned long trace [MAX_TRACE ];
163
- unsigned int trace_len ;
163
+ depot_stack_handle_t trace_handle ;
164
164
unsigned long jiffies ; /* creation timestamp */
165
165
pid_t pid ; /* pid of the current task */
166
166
char comm [TASK_COMM_LEN ]; /* executable name */
@@ -346,18 +346,21 @@ static void print_unreferenced(struct seq_file *seq,
346
346
struct kmemleak_object * object )
347
347
{
348
348
int i ;
349
+ unsigned long * entries ;
350
+ unsigned int nr_entries ;
349
351
unsigned int msecs_age = jiffies_to_msecs (jiffies - object -> jiffies );
350
352
353
+ nr_entries = stack_depot_fetch (object -> trace_handle , & entries );
351
354
warn_or_seq_printf (seq , "unreferenced object 0x%08lx (size %zu):\n" ,
352
- object -> pointer , object -> size );
355
+ object -> pointer , object -> size );
353
356
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 );
356
359
hex_dump_object (seq , object );
357
360
warn_or_seq_printf (seq , " backtrace:\n" );
358
361
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 ];
361
364
warn_or_seq_printf (seq , " [<%p>] %pS\n" , ptr , ptr );
362
365
}
363
366
}
@@ -370,15 +373,16 @@ static void print_unreferenced(struct seq_file *seq,
370
373
static void dump_object_info (struct kmemleak_object * object )
371
374
{
372
375
pr_notice ("Object 0x%08lx (size %zu):\n" ,
373
- object -> pointer , object -> size );
376
+ object -> pointer , object -> size );
374
377
pr_notice (" comm \"%s\", pid %d, jiffies %lu\n" ,
375
- object -> comm , object -> pid , object -> jiffies );
378
+ object -> comm , object -> pid , object -> jiffies );
376
379
pr_notice (" min_count = %d\n" , object -> min_count );
377
380
pr_notice (" count = %d\n" , object -> count );
378
381
pr_notice (" flags = 0x%x\n" , object -> flags );
379
382
pr_notice (" checksum = %u\n" , object -> checksum );
380
383
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 );
382
386
}
383
387
384
388
/*
@@ -591,12 +595,18 @@ static struct kmemleak_object *find_and_remove_object(unsigned long ptr, int ali
591
595
return object ;
592
596
}
593
597
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 )
598
599
{
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 ;
600
610
}
601
611
602
612
/*
@@ -653,7 +663,7 @@ static void __create_object(unsigned long ptr, size_t size,
653
663
}
654
664
655
665
/* kernel backtrace */
656
- object -> trace_len = __save_stack_trace ( object -> trace );
666
+ object -> trace_handle = set_track_prepare ( );
657
667
658
668
raw_spin_lock_irqsave (& kmemleak_lock , flags );
659
669
@@ -692,7 +702,6 @@ static void __create_object(unsigned long ptr, size_t size,
692
702
rb_link_node (& object -> rb_node , rb_parent , link );
693
703
rb_insert_color (& object -> rb_node , is_phys ? & object_phys_tree_root :
694
704
& object_tree_root );
695
-
696
705
list_add_tail_rcu (& object -> object_list , & object_list );
697
706
out :
698
707
raw_spin_unlock_irqrestore (& kmemleak_lock , flags );
@@ -1091,7 +1100,7 @@ void __ref kmemleak_update_trace(const void *ptr)
1091
1100
}
1092
1101
1093
1102
raw_spin_lock_irqsave (& object -> lock , flags );
1094
- object -> trace_len = __save_stack_trace ( object -> trace );
1103
+ object -> trace_handle = set_track_prepare ( );
1095
1104
raw_spin_unlock_irqrestore (& object -> lock , flags );
1096
1105
1097
1106
put_object (object );
@@ -2084,6 +2093,7 @@ void __init kmemleak_init(void)
2084
2093
if (kmemleak_error )
2085
2094
return ;
2086
2095
2096
+ stack_depot_init ();
2087
2097
jiffies_min_age = msecs_to_jiffies (MSECS_MIN_AGE );
2088
2098
jiffies_scan_wait = msecs_to_jiffies (SECS_SCAN_WAIT * 1000 );
2089
2099
0 commit comments