Skip to content

Commit 022012d

Browse files
xairyakpm00
authored andcommitted
lib/stackdepot, kasan: add flags to __stack_depot_save and rename
Change the bool can_alloc argument of __stack_depot_save to a u32 argument that accepts a set of flags. The following patch will add another flag to stack_depot_save_flags besides the existing STACK_DEPOT_FLAG_CAN_ALLOC. Also rename the function to stack_depot_save_flags, as __stack_depot_save is a cryptic name, Link: https://lkml.kernel.org/r/645fa15239621eebbd3a10331e5864b718839512.1700502145.git.andreyknvl@google.com Signed-off-by: Andrey Konovalov <[email protected]> Reviewed-by: Alexander Potapenko <[email protected]> Cc: Dmitry Vyukov <[email protected]> Cc: Evgenii Stepanov <[email protected]> Cc: Marco Elver <[email protected]> Cc: Oscar Salvador <[email protected]> Cc: Vlastimil Babka <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 3bddc31 commit 022012d

File tree

6 files changed

+48
-25
lines changed

6 files changed

+48
-25
lines changed

include/linux/stackdepot.h

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ typedef u32 depot_stack_handle_t;
3232
*/
3333
#define STACK_DEPOT_EXTRA_BITS 5
3434

35+
typedef u32 depot_flags_t;
36+
37+
/*
38+
* Flags that can be passed to stack_depot_save_flags(); see the comment next
39+
* to its declaration for more details.
40+
*/
41+
#define STACK_DEPOT_FLAG_CAN_ALLOC ((depot_flags_t)0x0001)
42+
43+
#define STACK_DEPOT_FLAGS_NUM 1
44+
#define STACK_DEPOT_FLAGS_MASK ((depot_flags_t)((1 << STACK_DEPOT_FLAGS_NUM) - 1))
45+
3546
/*
3647
* Using stack depot requires its initialization, which can be done in 3 ways:
3748
*
@@ -69,31 +80,34 @@ static inline int stack_depot_early_init(void) { return 0; }
6980
#endif
7081

7182
/**
72-
* __stack_depot_save - Save a stack trace to stack depot
83+
* stack_depot_save_flags - Save a stack trace to stack depot
7384
*
7485
* @entries: Pointer to the stack trace
7586
* @nr_entries: Number of frames in the stack
7687
* @alloc_flags: Allocation GFP flags
77-
* @can_alloc: Allocate stack pools (increased chance of failure if false)
88+
* @depot_flags: Stack depot flags
89+
*
90+
* Saves a stack trace from @entries array of size @nr_entries.
7891
*
79-
* Saves a stack trace from @entries array of size @nr_entries. If @can_alloc is
80-
* %true, stack depot can replenish the stack pools in case no space is left
81-
* (allocates using GFP flags of @alloc_flags). If @can_alloc is %false, avoids
82-
* any allocations and fails if no space is left to store the stack trace.
92+
* If STACK_DEPOT_FLAG_CAN_ALLOC is set in @depot_flags, stack depot can
93+
* replenish the stack pools in case no space is left (allocates using GFP
94+
* flags of @alloc_flags). Otherwise, stack depot avoids any allocations and
95+
* fails if no space is left to store the stack trace.
8396
*
8497
* If the provided stack trace comes from the interrupt context, only the part
8598
* up to the interrupt entry is saved.
8699
*
87-
* Context: Any context, but setting @can_alloc to %false is required if
100+
* Context: Any context, but setting STACK_DEPOT_FLAG_CAN_ALLOC is required if
88101
* alloc_pages() cannot be used from the current context. Currently
89102
* this is the case for contexts where neither %GFP_ATOMIC nor
90103
* %GFP_NOWAIT can be used (NMI, raw_spin_lock).
91104
*
92105
* Return: Handle of the stack struct stored in depot, 0 on failure
93106
*/
94-
depot_stack_handle_t __stack_depot_save(unsigned long *entries,
95-
unsigned int nr_entries,
96-
gfp_t gfp_flags, bool can_alloc);
107+
depot_stack_handle_t stack_depot_save_flags(unsigned long *entries,
108+
unsigned int nr_entries,
109+
gfp_t gfp_flags,
110+
depot_flags_t depot_flags);
97111

98112
/**
99113
* stack_depot_save - Save a stack trace to stack depot
@@ -103,7 +117,7 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries,
103117
* @alloc_flags: Allocation GFP flags
104118
*
105119
* Context: Contexts where allocations via alloc_pages() are allowed.
106-
* See __stack_depot_save() for more details.
120+
* See stack_depot_save_flags() for more details.
107121
*
108122
* Return: Handle of the stack trace stored in depot, 0 on failure
109123
*/

lib/stackdepot.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -450,19 +450,24 @@ static inline struct stack_record *find_stack(struct list_head *bucket,
450450
return NULL;
451451
}
452452

453-
depot_stack_handle_t __stack_depot_save(unsigned long *entries,
454-
unsigned int nr_entries,
455-
gfp_t alloc_flags, bool can_alloc)
453+
depot_stack_handle_t stack_depot_save_flags(unsigned long *entries,
454+
unsigned int nr_entries,
455+
gfp_t alloc_flags,
456+
depot_flags_t depot_flags)
456457
{
457458
struct list_head *bucket;
458459
struct stack_record *found = NULL;
459460
depot_stack_handle_t handle = 0;
460461
struct page *page = NULL;
461462
void *prealloc = NULL;
463+
bool can_alloc = depot_flags & STACK_DEPOT_FLAG_CAN_ALLOC;
462464
bool need_alloc = false;
463465
unsigned long flags;
464466
u32 hash;
465467

468+
if (WARN_ON(depot_flags & ~STACK_DEPOT_FLAGS_MASK))
469+
return 0;
470+
466471
/*
467472
* If this stack trace is from an interrupt, including anything before
468473
* interrupt entry usually leads to unbounded stack depot growth.
@@ -541,13 +546,14 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries,
541546
handle = found->handle.handle;
542547
return handle;
543548
}
544-
EXPORT_SYMBOL_GPL(__stack_depot_save);
549+
EXPORT_SYMBOL_GPL(stack_depot_save_flags);
545550

546551
depot_stack_handle_t stack_depot_save(unsigned long *entries,
547552
unsigned int nr_entries,
548553
gfp_t alloc_flags)
549554
{
550-
return __stack_depot_save(entries, nr_entries, alloc_flags, true);
555+
return stack_depot_save_flags(entries, nr_entries, alloc_flags,
556+
STACK_DEPOT_FLAG_CAN_ALLOC);
551557
}
552558
EXPORT_SYMBOL_GPL(stack_depot_save);
553559

mm/kasan/common.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <linux/sched.h>
2323
#include <linux/sched/task_stack.h>
2424
#include <linux/slab.h>
25+
#include <linux/stackdepot.h>
2526
#include <linux/stacktrace.h>
2627
#include <linux/string.h>
2728
#include <linux/types.h>
@@ -37,19 +38,19 @@ struct slab *kasan_addr_to_slab(const void *addr)
3738
return NULL;
3839
}
3940

40-
depot_stack_handle_t kasan_save_stack(gfp_t flags, bool can_alloc)
41+
depot_stack_handle_t kasan_save_stack(gfp_t flags, depot_flags_t depot_flags)
4142
{
4243
unsigned long entries[KASAN_STACK_DEPTH];
4344
unsigned int nr_entries;
4445

4546
nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0);
46-
return __stack_depot_save(entries, nr_entries, flags, can_alloc);
47+
return stack_depot_save_flags(entries, nr_entries, flags, depot_flags);
4748
}
4849

4950
void kasan_set_track(struct kasan_track *track, gfp_t flags)
5051
{
5152
track->pid = current->pid;
52-
track->stack = kasan_save_stack(flags, true);
53+
track->stack = kasan_save_stack(flags, STACK_DEPOT_FLAG_CAN_ALLOC);
5354
}
5455

5556
#if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)

mm/kasan/generic.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <linux/sched.h>
2626
#include <linux/sched/task_stack.h>
2727
#include <linux/slab.h>
28+
#include <linux/stackdepot.h>
2829
#include <linux/stacktrace.h>
2930
#include <linux/string.h>
3031
#include <linux/types.h>
@@ -472,7 +473,7 @@ size_t kasan_metadata_size(struct kmem_cache *cache, bool in_object)
472473
sizeof(struct kasan_free_meta) : 0);
473474
}
474475

475-
static void __kasan_record_aux_stack(void *addr, bool can_alloc)
476+
static void __kasan_record_aux_stack(void *addr, depot_flags_t depot_flags)
476477
{
477478
struct slab *slab = kasan_addr_to_slab(addr);
478479
struct kmem_cache *cache;
@@ -489,17 +490,17 @@ static void __kasan_record_aux_stack(void *addr, bool can_alloc)
489490
return;
490491

491492
alloc_meta->aux_stack[1] = alloc_meta->aux_stack[0];
492-
alloc_meta->aux_stack[0] = kasan_save_stack(0, can_alloc);
493+
alloc_meta->aux_stack[0] = kasan_save_stack(0, depot_flags);
493494
}
494495

495496
void kasan_record_aux_stack(void *addr)
496497
{
497-
return __kasan_record_aux_stack(addr, true);
498+
return __kasan_record_aux_stack(addr, STACK_DEPOT_FLAG_CAN_ALLOC);
498499
}
499500

500501
void kasan_record_aux_stack_noalloc(void *addr)
501502
{
502-
return __kasan_record_aux_stack(addr, false);
503+
return __kasan_record_aux_stack(addr, 0);
503504
}
504505

505506
void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags)

mm/kasan/kasan.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ static inline void kasan_init_cache_meta(struct kmem_cache *cache, unsigned int
368368
static inline void kasan_init_object_meta(struct kmem_cache *cache, const void *object) { }
369369
#endif
370370

371-
depot_stack_handle_t kasan_save_stack(gfp_t flags, bool can_alloc);
371+
depot_stack_handle_t kasan_save_stack(gfp_t flags, depot_flags_t depot_flags);
372372
void kasan_set_track(struct kasan_track *track, gfp_t flags);
373373
void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags);
374374
void kasan_save_free_info(struct kmem_cache *cache, void *object);

mm/kasan/tags.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/memblock.h>
1414
#include <linux/memory.h>
1515
#include <linux/mm.h>
16+
#include <linux/stackdepot.h>
1617
#include <linux/static_key.h>
1718
#include <linux/string.h>
1819
#include <linux/types.h>
@@ -101,7 +102,7 @@ static void save_stack_info(struct kmem_cache *cache, void *object,
101102
struct kasan_stack_ring_entry *entry;
102103
void *old_ptr;
103104

104-
stack = kasan_save_stack(gfp_flags, true);
105+
stack = kasan_save_stack(gfp_flags, STACK_DEPOT_FLAG_CAN_ALLOC);
105106

106107
/*
107108
* Prevent save_stack_info() from modifying stack ring

0 commit comments

Comments
 (0)