Skip to content

Commit b232b99

Browse files
xairyakpm00
authored andcommitted
lib/stackdepot: various comments clean-ups
Clean up comments in include/linux/stackdepot.h and lib/stackdepot.c: 1. Rework the initialization comment in stackdepot.h. 2. Rework the header comment in stackdepot.c. 3. Various clean-ups for other comments. Also adjust whitespaces for find_stack and depot_alloc_stack call sites. No functional changes. Link: https://lkml.kernel.org/r/5836231b7954355e2311fc9b5870f697ea8e1f7d.1676063693.git.andreyknvl@google.com Signed-off-by: Andrey Konovalov <[email protected]> Reviewed-by: Alexander Potapenko <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent beb3c23 commit b232b99

File tree

2 files changed

+78
-78
lines changed

2 files changed

+78
-78
lines changed

include/linux/stackdepot.h

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/* SPDX-License-Identifier: GPL-2.0-or-later */
22
/*
3-
* A generic stack depot implementation
3+
* Stack depot - a stack trace storage that avoids duplication.
44
*
55
* Author: Alexander Potapenko <[email protected]>
66
* Copyright (C) 2016 Google, Inc.
77
*
8-
* Based on code by Dmitry Chernenkov.
8+
* Based on the code by Dmitry Chernenkov.
99
*/
1010

1111
#ifndef _LINUX_STACKDEPOT_H
@@ -17,35 +17,37 @@ typedef u32 depot_stack_handle_t;
1717

1818
/*
1919
* Number of bits in the handle that stack depot doesn't use. Users may store
20-
* information in them.
20+
* information in them via stack_depot_set/get_extra_bits.
2121
*/
2222
#define STACK_DEPOT_EXTRA_BITS 5
2323

2424
/*
25-
* Every user of stack depot has to call stack_depot_init() during its own init
26-
* when it's decided that it will be calling stack_depot_save() later. This is
27-
* recommended for e.g. modules initialized later in the boot process, when
28-
* slab_is_available() is true.
25+
* Using stack depot requires its initialization, which can be done in 3 ways:
2926
*
30-
* The alternative is to select STACKDEPOT_ALWAYS_INIT to have stack depot
31-
* enabled as part of mm_init(), for subsystems where it's known at compile time
32-
* that stack depot will be used.
27+
* 1. Selecting CONFIG_STACKDEPOT_ALWAYS_INIT. This option is suitable in
28+
* scenarios where it's known at compile time that stack depot will be used.
29+
* Enabling this config makes the kernel initialize stack depot in mm_init().
3330
*
34-
* Another alternative is to call stack_depot_request_early_init(), when the
35-
* decision to use stack depot is taken e.g. when evaluating kernel boot
36-
* parameters, which precedes the enablement point in mm_init().
31+
* 2. Calling stack_depot_request_early_init() during early boot, before
32+
* stack_depot_early_init() in mm_init() completes. For example, this can
33+
* be done when evaluating kernel boot parameters.
34+
*
35+
* 3. Calling stack_depot_init(). Possible after boot is complete. This option
36+
* is recommended for modules initialized later in the boot process, after
37+
* mm_init() completes.
3738
*
3839
* stack_depot_init() and stack_depot_request_early_init() can be called
39-
* regardless of CONFIG_STACKDEPOT and are no-op when disabled. The actual
40-
* save/fetch/print functions should only be called from code that makes sure
41-
* CONFIG_STACKDEPOT is enabled.
40+
* regardless of whether CONFIG_STACKDEPOT is enabled and are no-op when this
41+
* config is disabled. The save/fetch/print stack depot functions can only be
42+
* called from the code that makes sure CONFIG_STACKDEPOT is enabled _and_
43+
* initializes stack depot via one of the ways listed above.
4244
*/
4345
#ifdef CONFIG_STACKDEPOT
4446
int stack_depot_init(void);
4547

4648
void __init stack_depot_request_early_init(void);
4749

48-
/* This is supposed to be called only from mm_init() */
50+
/* Must be only called from mm_init(). */
4951
int __init stack_depot_early_init(void);
5052
#else
5153
static inline int stack_depot_init(void) { return 0; }

lib/stackdepot.c

Lines changed: 59 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
// SPDX-License-Identifier: GPL-2.0-only
22
/*
3-
* Generic stack depot for storing stack traces.
3+
* Stack depot - a stack trace storage that avoids duplication.
44
*
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.
108
*
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.
1519
*
1620
* Author: Alexander Potapenko <[email protected]>
1721
* Copyright (C) 2016 Google, Inc.
1822
*
19-
* Based on code by Dmitry Chernenkov.
23+
* Based on the code by Dmitry Chernenkov.
2024
*/
2125

2226
#define pr_fmt(fmt) "stackdepot: " fmt
@@ -50,7 +54,7 @@
5054
(((1LL << (DEPOT_POOL_INDEX_BITS)) < DEPOT_POOLS_CAP) ? \
5155
(1LL << (DEPOT_POOL_INDEX_BITS)) : DEPOT_POOLS_CAP)
5256

53-
/* The compact structure to store the reference to stacks. */
57+
/* Compact structure that stores a reference to a stack. */
5458
union handle_parts {
5559
depot_stack_handle_t handle;
5660
struct {
@@ -62,11 +66,11 @@ union handle_parts {
6266
};
6367

6468
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 */
6872
union handle_parts handle;
69-
unsigned long entries[]; /* Variable-sized array of entries. */
73+
unsigned long entries[]; /* Variable-sized array of frames */
7074
};
7175

7276
static bool stack_depot_disabled;
@@ -317,17 +321,17 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc)
317321
return stack;
318322
}
319323

320-
/* Calculate hash for a stack */
324+
/* Calculates the hash for a stack. */
321325
static inline u32 hash_stack(unsigned long *entries, unsigned int size)
322326
{
323327
return jhash2((u32 *)entries,
324328
array_size(size, sizeof(*entries)) / sizeof(u32),
325329
STACK_HASH_SEED);
326330
}
327331

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.
331335
*/
332336
static inline
333337
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,
340344
return 0;
341345
}
342346

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. */
344348
static inline struct stack_record *find_stack(struct stack_record *bucket,
345349
unsigned long *entries, int size,
346350
u32 hash)
@@ -357,27 +361,27 @@ static inline struct stack_record *find_stack(struct stack_record *bucket,
357361
}
358362

359363
/**
360-
* __stack_depot_save - Save a stack trace from an array
364+
* __stack_depot_save - Save a stack trace to stack depot
361365
*
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
365369
* @can_alloc: Allocate stack pools (increased chance of failure if false)
366370
*
367371
* 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
369373
* (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.
371375
*
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.
374378
*
375379
* Context: Any context, but setting @can_alloc to %false is required if
376380
* 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
378382
* %GFP_NOWAIT can be used (NMI, raw_spin_lock).
379383
*
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
381385
*/
382386
depot_stack_handle_t __stack_depot_save(unsigned long *entries,
383387
unsigned int nr_entries,
@@ -392,11 +396,11 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries,
392396

393397
/*
394398
* 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.
396400
*
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.
400404
*/
401405
nr_entries = filter_irq_stacks(entries, nr_entries);
402406

@@ -411,8 +415,7 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries,
411415
* The smp_load_acquire() here pairs with smp_store_release() to
412416
* |bucket| below.
413417
*/
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);
416419
if (found)
417420
goto exit;
418421

@@ -441,7 +444,8 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries,
441444

442445
found = find_stack(*bucket, entries, nr_entries, hash);
443446
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);
445449

446450
if (new) {
447451
new->next = *bucket;
@@ -454,16 +458,16 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries,
454458
}
455459
} else if (prealloc) {
456460
/*
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.
459463
*/
460464
depot_init_pool(&prealloc);
461465
}
462466

463467
raw_spin_unlock_irqrestore(&pool_lock, flags);
464468
exit:
465469
if (prealloc) {
466-
/* Nobody used this memory, ok to free it. */
470+
/* Stack depot didn't use this memory, free it. */
467471
free_pages((unsigned long)prealloc, DEPOT_POOL_ORDER);
468472
}
469473
if (found)
@@ -474,16 +478,16 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries,
474478
EXPORT_SYMBOL_GPL(__stack_depot_save);
475479

476480
/**
477-
* stack_depot_save - Save a stack trace from an array
481+
* stack_depot_save - Save a stack trace to stack depot
478482
*
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
482486
*
483487
* Context: Contexts where allocations via alloc_pages() are allowed.
484488
* See __stack_depot_save() for more details.
485489
*
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
487491
*/
488492
depot_stack_handle_t stack_depot_save(unsigned long *entries,
489493
unsigned int nr_entries,
@@ -494,13 +498,12 @@ depot_stack_handle_t stack_depot_save(unsigned long *entries,
494498
EXPORT_SYMBOL_GPL(stack_depot_save);
495499

496500
/**
497-
* stack_depot_fetch - Fetch stack entries from a depot
501+
* stack_depot_fetch - Fetch a stack trace from stack depot
498502
*
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
502505
*
503-
* Return: The number of trace entries for this depot.
506+
* Return: Number of frames for the fetched stack
504507
*/
505508
unsigned int stack_depot_fetch(depot_stack_handle_t handle,
506509
unsigned long **entries)
@@ -535,11 +538,9 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle,
535538
EXPORT_SYMBOL_GPL(stack_depot_fetch);
536539

537540
/**
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
542542
*
543+
* @stack: Stack depot handle returned from stack_depot_save()
543544
*/
544545
void stack_depot_print(depot_stack_handle_t stack)
545546
{
@@ -553,17 +554,14 @@ void stack_depot_print(depot_stack_handle_t stack)
553554
EXPORT_SYMBOL_GPL(stack_depot_print);
554555

555556
/**
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
557558
*
558-
* @handle: Stack depot handle which was returned from
559-
* stack_depot_save().
559+
* @handle: Stack depot handle returned from stack_depot_save()
560560
* @buf: Pointer to the print buffer
561-
*
562561
* @size: Size of the print buffer
563-
*
564562
* @spaces: Number of leading spaces to print
565563
*
566-
* Return: Number of bytes printed.
564+
* Return: Number of bytes printed
567565
*/
568566
int stack_depot_snprint(depot_stack_handle_t handle, char *buf, size_t size,
569567
int spaces)

0 commit comments

Comments
 (0)