1
1
// SPDX-License-Identifier: GPL-2.0-only
2
2
/*
3
- * Generic stack depot for storing stack traces .
3
+ * Stack depot - a stack trace storage that avoids duplication .
4
4
*
5
- * Some debugging tools need to save stack traces of certain events which can
6
- * be later presented to the user. For example, KASAN needs to safe alloc and
7
- * free stacks for each object, but storing two stack traces per object
8
- * requires too much memory (e.g. SLUB_DEBUG needs 256 bytes per object for
9
- * that).
5
+ * Stack depot is intended to be used by subsystems that need to store and
6
+ * later retrieve many potentially duplicated stack traces without wasting
7
+ * memory.
10
8
*
11
- * Instead, stack depot maintains a hashtable of unique stacktraces. Since alloc
12
- * and free stacks repeat a lot, we save about 100x space.
13
- * Stacks are never removed from depot, so we store them contiguously one after
14
- * another in a contiguous memory allocation.
9
+ * For example, KASAN needs to save allocation and free stack traces for each
10
+ * object. Storing two stack traces per object requires a lot of memory (e.g.
11
+ * SLUB_DEBUG needs 256 bytes per object for that). Since allocation and free
12
+ * stack traces often repeat, using stack depot allows to save about 100x space.
13
+ *
14
+ * Internally, stack depot maintains a hash table of unique stacktraces. The
15
+ * stack traces themselves are stored contiguously one after another in a set
16
+ * of separate page allocations.
17
+ *
18
+ * Stack traces are never removed from stack depot.
15
19
*
16
20
* Author: Alexander Potapenko <[email protected] >
17
21
* Copyright (C) 2016 Google, Inc.
18
22
*
19
- * Based on code by Dmitry Chernenkov.
23
+ * Based on the code by Dmitry Chernenkov.
20
24
*/
21
25
22
26
#define pr_fmt (fmt ) "stackdepot: " fmt
50
54
(((1LL << (DEPOT_POOL_INDEX_BITS)) < DEPOT_POOLS_CAP) ? \
51
55
(1LL << (DEPOT_POOL_INDEX_BITS)) : DEPOT_POOLS_CAP)
52
56
53
- /* The compact structure to store the reference to stacks . */
57
+ /* Compact structure that stores a reference to a stack . */
54
58
union handle_parts {
55
59
depot_stack_handle_t handle ;
56
60
struct {
@@ -62,11 +66,11 @@ union handle_parts {
62
66
};
63
67
64
68
struct stack_record {
65
- struct stack_record * next ; /* Link in the hashtable */
66
- u32 hash ; /* Hash in the hastable */
67
- u32 size ; /* Number of frames in the stack */
69
+ struct stack_record * next ; /* Link in the hash table */
70
+ u32 hash ; /* Hash in the hash table */
71
+ u32 size ; /* Number of stored frames */
68
72
union handle_parts handle ;
69
- unsigned long entries []; /* Variable-sized array of entries. */
73
+ unsigned long entries []; /* Variable-sized array of frames */
70
74
};
71
75
72
76
static bool stack_depot_disabled ;
@@ -317,17 +321,17 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc)
317
321
return stack ;
318
322
}
319
323
320
- /* Calculate hash for a stack */
324
+ /* Calculates the hash for a stack. */
321
325
static inline u32 hash_stack (unsigned long * entries , unsigned int size )
322
326
{
323
327
return jhash2 ((u32 * )entries ,
324
328
array_size (size , sizeof (* entries )) / sizeof (u32 ),
325
329
STACK_HASH_SEED );
326
330
}
327
331
328
- /* Use our own, non-instrumented version of memcmp().
329
- *
330
- * We actually don't care about the order, just the equality.
332
+ /*
333
+ * Non-instrumented version of memcmp().
334
+ * Does not check the lexicographical order, only the equality.
331
335
*/
332
336
static inline
333
337
int stackdepot_memcmp (const unsigned long * u1 , const unsigned long * u2 ,
@@ -340,7 +344,7 @@ int stackdepot_memcmp(const unsigned long *u1, const unsigned long *u2,
340
344
return 0 ;
341
345
}
342
346
343
- /* Find a stack that is equal to the one stored in entries in the hash */
347
+ /* Finds a stack in a bucket of the hash table. */
344
348
static inline struct stack_record * find_stack (struct stack_record * bucket ,
345
349
unsigned long * entries , int size ,
346
350
u32 hash )
@@ -357,27 +361,27 @@ static inline struct stack_record *find_stack(struct stack_record *bucket,
357
361
}
358
362
359
363
/**
360
- * __stack_depot_save - Save a stack trace from an array
364
+ * __stack_depot_save - Save a stack trace to stack depot
361
365
*
362
- * @entries: Pointer to storage array
363
- * @nr_entries: Size of the storage array
364
- * @alloc_flags: Allocation gfp flags
366
+ * @entries: Pointer to the stack trace
367
+ * @nr_entries: Number of frames in the stack
368
+ * @alloc_flags: Allocation GFP flags
365
369
* @can_alloc: Allocate stack pools (increased chance of failure if false)
366
370
*
367
371
* Saves a stack trace from @entries array of size @nr_entries. If @can_alloc is
368
- * %true, is allowed to replenish the stack pool in case no space is left
372
+ * %true, stack depot can replenish the stack pools in case no space is left
369
373
* (allocates using GFP flags of @alloc_flags). If @can_alloc is %false, avoids
370
- * any allocations and will fail if no space is left to store the stack trace.
374
+ * any allocations and fails if no space is left to store the stack trace.
371
375
*
372
- * If the stack trace in @entries is from an interrupt, only the portion up to
373
- * interrupt entry is saved.
376
+ * If the provided stack trace comes from the interrupt context , only the part
377
+ * up to the interrupt entry is saved.
374
378
*
375
379
* Context: Any context, but setting @can_alloc to %false is required if
376
380
* alloc_pages() cannot be used from the current context. Currently
377
- * this is the case from contexts where neither %GFP_ATOMIC nor
381
+ * this is the case for contexts where neither %GFP_ATOMIC nor
378
382
* %GFP_NOWAIT can be used (NMI, raw_spin_lock).
379
383
*
380
- * Return: The handle of the stack struct stored in depot, 0 on failure.
384
+ * Return: Handle of the stack struct stored in depot, 0 on failure
381
385
*/
382
386
depot_stack_handle_t __stack_depot_save (unsigned long * entries ,
383
387
unsigned int nr_entries ,
@@ -392,11 +396,11 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries,
392
396
393
397
/*
394
398
* If this stack trace is from an interrupt, including anything before
395
- * interrupt entry usually leads to unbounded stackdepot growth.
399
+ * interrupt entry usually leads to unbounded stack depot growth.
396
400
*
397
- * Because use of filter_irq_stacks() is a requirement to ensure
398
- * stackdepot can efficiently deduplicate interrupt stacks, always
399
- * filter_irq_stacks() to simplify all callers' use of stackdepot .
401
+ * Since use of filter_irq_stacks() is a requirement to ensure stack
402
+ * depot can efficiently deduplicate interrupt stacks, always
403
+ * filter_irq_stacks() to simplify all callers' use of stack depot .
400
404
*/
401
405
nr_entries = filter_irq_stacks (entries , nr_entries );
402
406
@@ -411,8 +415,7 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries,
411
415
* The smp_load_acquire() here pairs with smp_store_release() to
412
416
* |bucket| below.
413
417
*/
414
- found = find_stack (smp_load_acquire (bucket ), entries ,
415
- nr_entries , hash );
418
+ found = find_stack (smp_load_acquire (bucket ), entries , nr_entries , hash );
416
419
if (found )
417
420
goto exit ;
418
421
@@ -441,7 +444,8 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries,
441
444
442
445
found = find_stack (* bucket , entries , nr_entries , hash );
443
446
if (!found ) {
444
- struct stack_record * new = depot_alloc_stack (entries , nr_entries , hash , & prealloc );
447
+ struct stack_record * new =
448
+ depot_alloc_stack (entries , nr_entries , hash , & prealloc );
445
449
446
450
if (new ) {
447
451
new -> next = * bucket ;
@@ -454,16 +458,16 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries,
454
458
}
455
459
} else if (prealloc ) {
456
460
/*
457
- * We didn't need to store this stack trace, but let's keep
458
- * the preallocated memory for the future.
461
+ * Stack depot already contains this stack trace, but let's
462
+ * keep the preallocated memory for the future.
459
463
*/
460
464
depot_init_pool (& prealloc );
461
465
}
462
466
463
467
raw_spin_unlock_irqrestore (& pool_lock , flags );
464
468
exit :
465
469
if (prealloc ) {
466
- /* Nobody used this memory, ok to free it. */
470
+ /* Stack depot didn't use this memory, free it. */
467
471
free_pages ((unsigned long )prealloc , DEPOT_POOL_ORDER );
468
472
}
469
473
if (found )
@@ -474,16 +478,16 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries,
474
478
EXPORT_SYMBOL_GPL (__stack_depot_save );
475
479
476
480
/**
477
- * stack_depot_save - Save a stack trace from an array
481
+ * stack_depot_save - Save a stack trace to stack depot
478
482
*
479
- * @entries: Pointer to storage array
480
- * @nr_entries: Size of the storage array
481
- * @alloc_flags: Allocation gfp flags
483
+ * @entries: Pointer to the stack trace
484
+ * @nr_entries: Number of frames in the stack
485
+ * @alloc_flags: Allocation GFP flags
482
486
*
483
487
* Context: Contexts where allocations via alloc_pages() are allowed.
484
488
* See __stack_depot_save() for more details.
485
489
*
486
- * Return: The handle of the stack struct stored in depot, 0 on failure.
490
+ * Return: Handle of the stack trace stored in depot, 0 on failure
487
491
*/
488
492
depot_stack_handle_t stack_depot_save (unsigned long * entries ,
489
493
unsigned int nr_entries ,
@@ -494,13 +498,12 @@ depot_stack_handle_t stack_depot_save(unsigned long *entries,
494
498
EXPORT_SYMBOL_GPL (stack_depot_save );
495
499
496
500
/**
497
- * stack_depot_fetch - Fetch stack entries from a depot
501
+ * stack_depot_fetch - Fetch a stack trace from stack depot
498
502
*
499
- * @handle: Stack depot handle which was returned from
500
- * stack_depot_save().
501
- * @entries: Pointer to store the entries address
503
+ * @handle: Stack depot handle returned from stack_depot_save()
504
+ * @entries: Pointer to store the address of the stack trace
502
505
*
503
- * Return: The number of trace entries for this depot.
506
+ * Return: Number of frames for the fetched stack
504
507
*/
505
508
unsigned int stack_depot_fetch (depot_stack_handle_t handle ,
506
509
unsigned long * * entries )
@@ -535,11 +538,9 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle,
535
538
EXPORT_SYMBOL_GPL (stack_depot_fetch );
536
539
537
540
/**
538
- * stack_depot_print - print stack entries from a depot
539
- *
540
- * @stack: Stack depot handle which was returned from
541
- * stack_depot_save().
541
+ * stack_depot_print - Print a stack trace from stack depot
542
542
*
543
+ * @stack: Stack depot handle returned from stack_depot_save()
543
544
*/
544
545
void stack_depot_print (depot_stack_handle_t stack )
545
546
{
@@ -553,17 +554,14 @@ void stack_depot_print(depot_stack_handle_t stack)
553
554
EXPORT_SYMBOL_GPL (stack_depot_print );
554
555
555
556
/**
556
- * stack_depot_snprint - print stack entries from a depot into a buffer
557
+ * stack_depot_snprint - Print a stack trace from stack depot into a buffer
557
558
*
558
- * @handle: Stack depot handle which was returned from
559
- * stack_depot_save().
559
+ * @handle: Stack depot handle returned from stack_depot_save()
560
560
* @buf: Pointer to the print buffer
561
- *
562
561
* @size: Size of the print buffer
563
- *
564
562
* @spaces: Number of leading spaces to print
565
563
*
566
- * Return: Number of bytes printed.
564
+ * Return: Number of bytes printed
567
565
*/
568
566
int stack_depot_snprint (depot_stack_handle_t handle , char * buf , size_t size ,
569
567
int spaces )
0 commit comments