Skip to content

Commit d40797d

Browse files
Peter Zijlstraakpm00
Peter Zijlstra
authored andcommitted
kasan: make kasan_record_aux_stack_noalloc() the default behaviour
kasan_record_aux_stack_noalloc() was introduced to record a stack trace without allocating memory in the process. It has been added to callers which were invoked while a raw_spinlock_t was held. More and more callers were identified and changed over time. Is it a good thing to have this while functions try their best to do a locklessly setup? The only downside of having kasan_record_aux_stack() not allocate any memory is that we end up without a stacktrace if stackdepot runs out of memory and at the same stacktrace was not recorded before To quote Marco Elver from https://lore.kernel.org/all/CANpmjNPmQYJ7pv1N3cuU8cP18u7PP_uoZD8YxwZd4jtbof9nVQ@mail.gmail.com/ | I'd be in favor, it simplifies things. And stack depot should be | able to replenish its pool sufficiently in the "non-aux" cases | i.e. regular allocations. Worst case we fail to record some | aux stacks, but I think that's only really bad if there's a bug | around one of these allocations. In general the probabilities | of this being a regression are extremely small [...] Make the kasan_record_aux_stack_noalloc() behaviour default as kasan_record_aux_stack(). [[email protected]: dressed the diff as patch] Link: https://lkml.kernel.org/r/[email protected] Fixes: 7cb3007 ("kasan: generic: introduce kasan_record_aux_stack_noalloc()") Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Signed-off-by: Sebastian Andrzej Siewior <[email protected]> Reported-by: [email protected] Closes: https://lore.kernel.org/all/[email protected] Reviewed-by: Andrey Konovalov <[email protected]> Reviewed-by: Marco Elver <[email protected]> Reviewed-by: Waiman Long <[email protected]> Cc: Alexander Potapenko <[email protected]> Cc: Andrey Ryabinin <[email protected]> Cc: Ben Segall <[email protected]> Cc: Boqun Feng <[email protected]> Cc: Christoph Lameter <[email protected]> Cc: David Rientjes <[email protected]> Cc: Dietmar Eggemann <[email protected]> Cc: Dmitry Vyukov <[email protected]> Cc: Frederic Weisbecker <[email protected]> Cc: Hyeonggon Yoo <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jann Horn <[email protected]> Cc: Joel Fernandes (Google) <[email protected]> Cc: Joonsoo Kim <[email protected]> Cc: Josh Triplett <[email protected]> Cc: Juri Lelli <[email protected]> Cc: <[email protected]> Cc: Lai Jiangshan <[email protected]> Cc: Liam R. Howlett <[email protected]> Cc: Lorenzo Stoakes <[email protected]> Cc: Mathieu Desnoyers <[email protected]> Cc: Mel Gorman <[email protected]> Cc: Neeraj Upadhyay <[email protected]> Cc: Paul E. McKenney <[email protected]> Cc: Pekka Enberg <[email protected]> Cc: Roman Gushchin <[email protected]> Cc: Steven Rostedt <[email protected]> Cc: [email protected] Cc: Tejun Heo <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Uladzislau Rezki (Sony) <[email protected]> Cc: Valentin Schneider <[email protected]> Cc: Vincent Guittot <[email protected]> Cc: Vincenzo Frascino <[email protected]> Cc: Vlastimil Babka <[email protected]> Cc: Zqiang <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 773fc6a commit d40797d

File tree

10 files changed

+14
-37
lines changed

10 files changed

+14
-37
lines changed

include/linux/kasan.h

-2
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,6 @@ void kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
491491
void kasan_cache_shrink(struct kmem_cache *cache);
492492
void kasan_cache_shutdown(struct kmem_cache *cache);
493493
void kasan_record_aux_stack(void *ptr);
494-
void kasan_record_aux_stack_noalloc(void *ptr);
495494

496495
#else /* CONFIG_KASAN_GENERIC */
497496

@@ -509,7 +508,6 @@ static inline void kasan_cache_create(struct kmem_cache *cache,
509508
static inline void kasan_cache_shrink(struct kmem_cache *cache) {}
510509
static inline void kasan_cache_shutdown(struct kmem_cache *cache) {}
511510
static inline void kasan_record_aux_stack(void *ptr) {}
512-
static inline void kasan_record_aux_stack_noalloc(void *ptr) {}
513511

514512
#endif /* CONFIG_KASAN_GENERIC */
515513

include/linux/task_work.h

-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ enum task_work_notify_mode {
1919
TWA_SIGNAL,
2020
TWA_SIGNAL_NO_IPI,
2121
TWA_NMI_CURRENT,
22-
23-
TWA_FLAGS = 0xff00,
24-
TWAF_NO_ALLOC = 0x0100,
2522
};
2623

2724
static inline bool task_work_pending(struct task_struct *task)

kernel/irq_work.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ bool irq_work_queue_on(struct irq_work *work, int cpu)
147147
if (!irq_work_claim(work))
148148
return false;
149149

150-
kasan_record_aux_stack_noalloc(work);
150+
kasan_record_aux_stack(work);
151151

152152
preempt_disable();
153153
if (cpu != smp_processor_id()) {

kernel/rcu/tiny.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ EXPORT_SYMBOL_GPL(poll_state_synchronize_rcu);
250250
void kvfree_call_rcu(struct rcu_head *head, void *ptr)
251251
{
252252
if (head)
253-
kasan_record_aux_stack_noalloc(ptr);
253+
kasan_record_aux_stack(ptr);
254254

255255
__kvfree_call_rcu(head, ptr);
256256
}

kernel/rcu/tree.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -3083,7 +3083,7 @@ __call_rcu_common(struct rcu_head *head, rcu_callback_t func, bool lazy_in)
30833083
}
30843084
head->func = func;
30853085
head->next = NULL;
3086-
kasan_record_aux_stack_noalloc(head);
3086+
kasan_record_aux_stack(head);
30873087
local_irq_save(flags);
30883088
rdp = this_cpu_ptr(&rcu_data);
30893089
lazy = lazy_in && !rcu_async_should_hurry();
@@ -3817,7 +3817,7 @@ void kvfree_call_rcu(struct rcu_head *head, void *ptr)
38173817
return;
38183818
}
38193819

3820-
kasan_record_aux_stack_noalloc(ptr);
3820+
kasan_record_aux_stack(ptr);
38213821
success = add_ptr_to_bulk_krc_lock(&krcp, &flags, ptr, !head);
38223822
if (!success) {
38233823
run_page_cache_worker(krcp);

kernel/sched/core.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -10590,7 +10590,7 @@ void task_tick_mm_cid(struct rq *rq, struct task_struct *curr)
1059010590
return;
1059110591

1059210592
/* No page allocation under rq lock */
10593-
task_work_add(curr, work, TWA_RESUME | TWAF_NO_ALLOC);
10593+
task_work_add(curr, work, TWA_RESUME);
1059410594
}
1059510595

1059610596
void sched_mm_cid_exit_signals(struct task_struct *t)

kernel/task_work.c

+1-13
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,14 @@ int task_work_add(struct task_struct *task, struct callback_head *work,
5555
enum task_work_notify_mode notify)
5656
{
5757
struct callback_head *head;
58-
int flags = notify & TWA_FLAGS;
5958

60-
notify &= ~TWA_FLAGS;
6159
if (notify == TWA_NMI_CURRENT) {
6260
if (WARN_ON_ONCE(task != current))
6361
return -EINVAL;
6462
if (!IS_ENABLED(CONFIG_IRQ_WORK))
6563
return -EINVAL;
6664
} else {
67-
/*
68-
* Record the work call stack in order to print it in KASAN
69-
* reports.
70-
*
71-
* Note that stack allocation can fail if TWAF_NO_ALLOC flag
72-
* is set and new page is needed to expand the stack buffer.
73-
*/
74-
if (flags & TWAF_NO_ALLOC)
75-
kasan_record_aux_stack_noalloc(work);
76-
else
77-
kasan_record_aux_stack(work);
65+
kasan_record_aux_stack(work);
7866
}
7967

8068
head = READ_ONCE(task->task_works);

kernel/workqueue.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2180,7 +2180,7 @@ static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
21802180
debug_work_activate(work);
21812181

21822182
/* record the work call stack in order to print it in KASAN reports */
2183-
kasan_record_aux_stack_noalloc(work);
2183+
kasan_record_aux_stack(work);
21842184

21852185
/* we own @work, set data and link */
21862186
set_work_pwq(work, pwq, extra_flags);

mm/kasan/generic.c

+6-12
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,11 @@ size_t kasan_metadata_size(struct kmem_cache *cache, bool in_object)
524524
sizeof(struct kasan_free_meta) : 0);
525525
}
526526

527-
static void __kasan_record_aux_stack(void *addr, depot_flags_t depot_flags)
527+
/*
528+
* This function avoids dynamic memory allocations and thus can be called from
529+
* contexts that do not allow allocating memory.
530+
*/
531+
void kasan_record_aux_stack(void *addr)
528532
{
529533
struct slab *slab = kasan_addr_to_slab(addr);
530534
struct kmem_cache *cache;
@@ -541,17 +545,7 @@ static void __kasan_record_aux_stack(void *addr, depot_flags_t depot_flags)
541545
return;
542546

543547
alloc_meta->aux_stack[1] = alloc_meta->aux_stack[0];
544-
alloc_meta->aux_stack[0] = kasan_save_stack(0, depot_flags);
545-
}
546-
547-
void kasan_record_aux_stack(void *addr)
548-
{
549-
return __kasan_record_aux_stack(addr, STACK_DEPOT_FLAG_CAN_ALLOC);
550-
}
551-
552-
void kasan_record_aux_stack_noalloc(void *addr)
553-
{
554-
return __kasan_record_aux_stack(addr, 0);
548+
alloc_meta->aux_stack[0] = kasan_save_stack(0, 0);
555549
}
556550

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

mm/slub.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2311,7 +2311,7 @@ bool slab_free_hook(struct kmem_cache *s, void *x, bool init,
23112311
* We have to do this manually because the rcu_head is
23122312
* not located inside the object.
23132313
*/
2314-
kasan_record_aux_stack_noalloc(x);
2314+
kasan_record_aux_stack(x);
23152315

23162316
delayed_free->object = x;
23172317
call_rcu(&delayed_free->head, slab_free_after_rcu_debug);

0 commit comments

Comments
 (0)