Skip to content

Commit 36aa1e6

Browse files
xairyakpm00
authored andcommitted
lib/stacktrace, kasan, kmsan: rework extra_bits interface
The current implementation of the extra_bits interface is confusing: passing extra_bits to __stack_depot_save makes it seem that the extra bits are somehow stored in stack depot. In reality, they are only embedded into a stack depot handle and are not used within stack depot. Drop the extra_bits argument from __stack_depot_save and instead provide a new stack_depot_set_extra_bits function (similar to the exsiting stack_depot_get_extra_bits) that saves extra bits into a stack depot handle. Update the callers of __stack_depot_save to use the new interace. This change also fixes a minor issue in the old code: __stack_depot_save does not return NULL if saving stack trace fails and extra_bits is used. Link: https://lkml.kernel.org/r/317123b5c05e2f82854fc55d8b285e0869d3cb77.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 d11a562 commit 36aa1e6

File tree

4 files changed

+44
-14
lines changed

4 files changed

+44
-14
lines changed

include/linux/stackdepot.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ static inline int stack_depot_early_init(void) { return 0; }
5757

5858
depot_stack_handle_t __stack_depot_save(unsigned long *entries,
5959
unsigned int nr_entries,
60-
unsigned int extra_bits,
6160
gfp_t gfp_flags, bool can_alloc);
6261

6362
depot_stack_handle_t stack_depot_save(unsigned long *entries,
@@ -71,6 +70,9 @@ void stack_depot_print(depot_stack_handle_t stack);
7170
int stack_depot_snprint(depot_stack_handle_t handle, char *buf, size_t size,
7271
int spaces);
7372

73+
depot_stack_handle_t __must_check stack_depot_set_extra_bits(
74+
depot_stack_handle_t handle, unsigned int extra_bits);
75+
7476
unsigned int stack_depot_get_extra_bits(depot_stack_handle_t handle);
7577

7678
#endif

lib/stackdepot.c

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,6 @@ static inline struct stack_record *find_stack(struct stack_record *bucket,
357357
*
358358
* @entries: Pointer to storage array
359359
* @nr_entries: Size of the storage array
360-
* @extra_bits: Flags to store in unused bits of depot_stack_handle_t
361360
* @alloc_flags: Allocation gfp flags
362361
* @can_alloc: Allocate stack pools (increased chance of failure if false)
363362
*
@@ -369,10 +368,6 @@ static inline struct stack_record *find_stack(struct stack_record *bucket,
369368
* If the stack trace in @entries is from an interrupt, only the portion up to
370369
* interrupt entry is saved.
371370
*
372-
* Additional opaque flags can be passed in @extra_bits, stored in the unused
373-
* bits of the stack handle, and retrieved using stack_depot_get_extra_bits()
374-
* without calling stack_depot_fetch().
375-
*
376371
* Context: Any context, but setting @can_alloc to %false is required if
377372
* alloc_pages() cannot be used from the current context. Currently
378373
* this is the case from contexts where neither %GFP_ATOMIC nor
@@ -382,7 +377,6 @@ static inline struct stack_record *find_stack(struct stack_record *bucket,
382377
*/
383378
depot_stack_handle_t __stack_depot_save(unsigned long *entries,
384379
unsigned int nr_entries,
385-
unsigned int extra_bits,
386380
gfp_t alloc_flags, bool can_alloc)
387381
{
388382
struct stack_record *found = NULL, **bucket;
@@ -471,8 +465,6 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries,
471465
if (found)
472466
retval.handle = found->handle.handle;
473467
fast_exit:
474-
retval.extra = extra_bits;
475-
476468
return retval.handle;
477469
}
478470
EXPORT_SYMBOL_GPL(__stack_depot_save);
@@ -493,7 +485,7 @@ depot_stack_handle_t stack_depot_save(unsigned long *entries,
493485
unsigned int nr_entries,
494486
gfp_t alloc_flags)
495487
{
496-
return __stack_depot_save(entries, nr_entries, 0, alloc_flags, true);
488+
return __stack_depot_save(entries, nr_entries, alloc_flags, true);
497489
}
498490
EXPORT_SYMBOL_GPL(stack_depot_save);
499491

@@ -576,6 +568,38 @@ int stack_depot_snprint(depot_stack_handle_t handle, char *buf, size_t size,
576568
}
577569
EXPORT_SYMBOL_GPL(stack_depot_snprint);
578570

571+
/**
572+
* stack_depot_set_extra_bits - Set extra bits in a stack depot handle
573+
*
574+
* @handle: Stack depot handle returned from stack_depot_save()
575+
* @extra_bits: Value to set the extra bits
576+
*
577+
* Return: Stack depot handle with extra bits set
578+
*
579+
* Stack depot handles have a few unused bits, which can be used for storing
580+
* user-specific information. These bits are transparent to the stack depot.
581+
*/
582+
depot_stack_handle_t __must_check stack_depot_set_extra_bits(
583+
depot_stack_handle_t handle, unsigned int extra_bits)
584+
{
585+
union handle_parts parts = { .handle = handle };
586+
587+
/* Don't set extra bits on empty handles. */
588+
if (!handle)
589+
return 0;
590+
591+
parts.extra = extra_bits;
592+
return parts.handle;
593+
}
594+
EXPORT_SYMBOL(stack_depot_set_extra_bits);
595+
596+
/**
597+
* stack_depot_get_extra_bits - Retrieve extra bits from a stack depot handle
598+
*
599+
* @handle: Stack depot handle with extra bits saved
600+
*
601+
* Return: Extra bits retrieved from the stack depot handle
602+
*/
579603
unsigned int stack_depot_get_extra_bits(depot_stack_handle_t handle)
580604
{
581605
union handle_parts parts = { .handle = handle };

mm/kasan/common.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ depot_stack_handle_t kasan_save_stack(gfp_t flags, bool can_alloc)
4343
unsigned int nr_entries;
4444

4545
nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0);
46-
return __stack_depot_save(entries, nr_entries, 0, flags, can_alloc);
46+
return __stack_depot_save(entries, nr_entries, flags, can_alloc);
4747
}
4848

4949
void kasan_set_track(struct kasan_track *track, gfp_t flags)

mm/kmsan/core.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,15 @@ depot_stack_handle_t kmsan_save_stack_with_flags(gfp_t flags,
6969
{
7070
unsigned long entries[KMSAN_STACK_DEPTH];
7171
unsigned int nr_entries;
72+
depot_stack_handle_t handle;
7273

7374
nr_entries = stack_trace_save(entries, KMSAN_STACK_DEPTH, 0);
7475

7576
/* Don't sleep (see might_sleep_if() in __alloc_pages_nodemask()). */
7677
flags &= ~__GFP_DIRECT_RECLAIM;
7778

78-
return __stack_depot_save(entries, nr_entries, extra, flags, true);
79+
handle = __stack_depot_save(entries, nr_entries, flags, true);
80+
return stack_depot_set_extra_bits(handle, extra);
7981
}
8082

8183
/* Copy the metadata following the memmove() behavior. */
@@ -215,6 +217,7 @@ depot_stack_handle_t kmsan_internal_chain_origin(depot_stack_handle_t id)
215217
u32 extra_bits;
216218
int depth;
217219
bool uaf;
220+
depot_stack_handle_t handle;
218221

219222
if (!id)
220223
return id;
@@ -250,8 +253,9 @@ depot_stack_handle_t kmsan_internal_chain_origin(depot_stack_handle_t id)
250253
* positives when __stack_depot_save() passes it to instrumented code.
251254
*/
252255
kmsan_internal_unpoison_memory(entries, sizeof(entries), false);
253-
return __stack_depot_save(entries, ARRAY_SIZE(entries), extra_bits,
254-
GFP_ATOMIC, true);
256+
handle = __stack_depot_save(entries, ARRAY_SIZE(entries), GFP_ATOMIC,
257+
true);
258+
return stack_depot_set_extra_bits(handle, extra_bits);
255259
}
256260

257261
void kmsan_internal_set_shadow_origin(void *addr, size_t size, int b,

0 commit comments

Comments
 (0)