xref: /linux-6.15/include/linux/stackdepot.h (revision b232b999)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Stack depot - a stack trace storage that avoids duplication.
4  *
5  * Author: Alexander Potapenko <[email protected]>
6  * Copyright (C) 2016 Google, Inc.
7  *
8  * Based on the code by Dmitry Chernenkov.
9  */
10 
11 #ifndef _LINUX_STACKDEPOT_H
12 #define _LINUX_STACKDEPOT_H
13 
14 #include <linux/gfp.h>
15 
16 typedef u32 depot_stack_handle_t;
17 
18 /*
19  * Number of bits in the handle that stack depot doesn't use. Users may store
20  * information in them via stack_depot_set/get_extra_bits.
21  */
22 #define STACK_DEPOT_EXTRA_BITS 5
23 
24 /*
25  * Using stack depot requires its initialization, which can be done in 3 ways:
26  *
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().
30  *
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.
38  *
39  * stack_depot_init() and stack_depot_request_early_init() can be called
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.
44  */
45 #ifdef CONFIG_STACKDEPOT
46 int stack_depot_init(void);
47 
48 void __init stack_depot_request_early_init(void);
49 
50 /* Must be only called from mm_init(). */
51 int __init stack_depot_early_init(void);
52 #else
53 static inline int stack_depot_init(void) { return 0; }
54 
55 static inline void stack_depot_request_early_init(void) { }
56 
57 static inline int stack_depot_early_init(void)	{ return 0; }
58 #endif
59 
60 depot_stack_handle_t __stack_depot_save(unsigned long *entries,
61 					unsigned int nr_entries,
62 					gfp_t gfp_flags, bool can_alloc);
63 
64 depot_stack_handle_t stack_depot_save(unsigned long *entries,
65 				      unsigned int nr_entries, gfp_t gfp_flags);
66 
67 unsigned int stack_depot_fetch(depot_stack_handle_t handle,
68 			       unsigned long **entries);
69 
70 void stack_depot_print(depot_stack_handle_t stack);
71 
72 int stack_depot_snprint(depot_stack_handle_t handle, char *buf, size_t size,
73 		       int spaces);
74 
75 depot_stack_handle_t __must_check stack_depot_set_extra_bits(
76 			depot_stack_handle_t handle, unsigned int extra_bits);
77 
78 unsigned int stack_depot_get_extra_bits(depot_stack_handle_t handle);
79 
80 #endif
81