Skip to content

Commit 7411135

Browse files
committed
Merge: kasan: make kasan_record_aux_stack_noalloc() the default behaviour
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-10/-/merge_requests/208 JIRA: https://issues.redhat.com/browse/RHEL-71050 Upstream Status: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm commit d40797d Author: Peter Zijlstra <peterz@infradead.org> Date: Fri, 22 Nov 2024 16:54:51 +0100 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(). [bigeasy@linutronix.de: dressed the diff as patch] Link: https://lkml.kernel.org/r/20241122155451.Mb2pmeyJ@linutronix.de Fixes: 7cb3007 ("kasan: generic: introduce kasan_record_aux_stack_noalloc()") Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de> Reported-by: syzbot+39f85d612b7c20d8db48@syzkaller.appspotmail.com Closes: https://lore.kernel.org/all/67275485.050a0220.3c8d68.0a37.GAE@google.com Reviewed-by: Andrey Konovalov <andreyknvl@gmail.com> Reviewed-by: Marco Elver <elver@google.com> Reviewed-by: Waiman Long <longman@redhat.com> Cc: Alexander Potapenko <glider@google.com> Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com> Cc: Ben Segall <bsegall@google.com> Cc: Boqun Feng <boqun.feng@gmail.com> Cc: Christoph Lameter <cl@linux.com> Cc: David Rientjes <rientjes@google.com> Cc: Dietmar Eggemann <dietmar.eggemann@arm.com> Cc: Dmitry Vyukov <dvyukov@google.com> Cc: Frederic Weisbecker <frederic@kernel.org> Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: Jann Horn <jannh@google.com> Cc: Joel Fernandes (Google) <joel@joelfernandes.org> Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> Cc: Josh Triplett <josh@joshtriplett.org> Cc: Juri Lelli <juri.lelli@redhat.com> Cc: <kasan-dev@googlegroups.com> Cc: Lai Jiangshan <jiangshanlai@gmail.com> Cc: Liam R. Howlett <Liam.Howlett@Oracle.com> Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Cc: Mel Gorman <mgorman@suse.de> Cc: Neeraj Upadhyay <neeraj.upadhyay@kernel.org> Cc: Paul E. McKenney <paulmck@kernel.org> Cc: Pekka Enberg <penberg@kernel.org> Cc: Roman Gushchin <roman.gushchin@linux.dev> Cc: Steven Rostedt <rostedt@goodmis.org> Cc: syzkaller-bugs@googlegroups.com Cc: Tejun Heo <tj@kernel.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Uladzislau Rezki (Sony) <urezki@gmail.com> Cc: Valentin Schneider <vschneid@redhat.com> Cc: Vincent Guittot <vincent.guittot@linaro.org> Cc: Vincenzo Frascino <vincenzo.frascino@arm.com> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: Zqiang <qiang.zhang1211@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Waiman Long <longman@redhat.com> Approved-by: Phil Auld <pauld@redhat.com> Approved-by: Wander Lairson Costa <wander@redhat.com> Approved-by: Rafael Aquini <raquini@redhat.com> Approved-by: Aristeu Rozanski <arozansk@redhat.com> Approved-by: CKI KWF Bot <cki-ci-bot+kwf-gitlab-com@redhat.com> Merged-by: Jan Stancek <jstancek@redhat.com>
2 parents 6c01456 + 13b77b1 commit 7411135

File tree

10 files changed

+14
-37
lines changed

10 files changed

+14
-37
lines changed

include/linux/kasan.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,6 @@ void kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
488488
void kasan_cache_shrink(struct kmem_cache *cache);
489489
void kasan_cache_shutdown(struct kmem_cache *cache);
490490
void kasan_record_aux_stack(void *ptr);
491-
void kasan_record_aux_stack_noalloc(void *ptr);
492491

493492
#else /* CONFIG_KASAN_GENERIC */
494493

@@ -506,7 +505,6 @@ static inline void kasan_cache_create(struct kmem_cache *cache,
506505
static inline void kasan_cache_shrink(struct kmem_cache *cache) {}
507506
static inline void kasan_cache_shutdown(struct kmem_cache *cache) {}
508507
static inline void kasan_record_aux_stack(void *ptr) {}
509-
static inline void kasan_record_aux_stack_noalloc(void *ptr) {}
510508

511509
#endif /* CONFIG_KASAN_GENERIC */
512510

include/linux/task_work.h

Lines changed: 0 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10614,7 +10614,7 @@ void task_tick_mm_cid(struct rq *rq, struct task_struct *curr)
1061410614
return;
1061510615

1061610616
/* No page allocation under rq lock */
10617-
task_work_add(curr, work, TWA_RESUME | TWAF_NO_ALLOC);
10617+
task_work_add(curr, work, TWA_RESUME);
1061810618
}
1061910619

1062010620
void sched_mm_cid_exit_signals(struct task_struct *t)

kernel/task_work.c

Lines changed: 1 addition & 13 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,11 @@ size_t kasan_metadata_size(struct kmem_cache *cache, bool in_object)
521521
sizeof(struct kasan_free_meta) : 0);
522522
}
523523

524-
static void __kasan_record_aux_stack(void *addr, depot_flags_t depot_flags)
524+
/*
525+
* This function avoids dynamic memory allocations and thus can be called from
526+
* contexts that do not allow allocating memory.
527+
*/
528+
void kasan_record_aux_stack(void *addr)
525529
{
526530
struct slab *slab = kasan_addr_to_slab(addr);
527531
struct kmem_cache *cache;
@@ -538,17 +542,7 @@ static void __kasan_record_aux_stack(void *addr, depot_flags_t depot_flags)
538542
return;
539543

540544
alloc_meta->aux_stack[1] = alloc_meta->aux_stack[0];
541-
alloc_meta->aux_stack[0] = kasan_save_stack(0, depot_flags);
542-
}
543-
544-
void kasan_record_aux_stack(void *addr)
545-
{
546-
return __kasan_record_aux_stack(addr, STACK_DEPOT_FLAG_CAN_ALLOC);
547-
}
548-
549-
void kasan_record_aux_stack_noalloc(void *addr)
550-
{
551-
return __kasan_record_aux_stack(addr, 0);
545+
alloc_meta->aux_stack[0] = kasan_save_stack(0, 0);
552546
}
553547

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

mm/slub.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2306,7 +2306,7 @@ bool slab_free_hook(struct kmem_cache *s, void *x, bool init,
23062306
* We have to do this manually because the rcu_head is
23072307
* not located inside the object.
23082308
*/
2309-
kasan_record_aux_stack_noalloc(x);
2309+
kasan_record_aux_stack(x);
23102310

23112311
delayed_free->object = x;
23122312
call_rcu(&delayed_free->head, slab_free_after_rcu_debug);

0 commit comments

Comments
 (0)