From patchwork Fri Nov 22 15:54:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sebastian Andrzej Siewior X-Patchwork-Id: 13883316 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4A1FDE69173 for ; Fri, 22 Nov 2024 15:54:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C90FE6B00A1; Fri, 22 Nov 2024 10:54:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C411B6B00B0; Fri, 22 Nov 2024 10:54:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AE11F6B00B8; Fri, 22 Nov 2024 10:54:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 9017F6B00A1 for ; Fri, 22 Nov 2024 10:54:57 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 53999A188A for ; Fri, 22 Nov 2024 15:54:57 +0000 (UTC) X-FDA: 82814177880.12.FECFF33 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by imf08.hostedemail.com (Postfix) with ESMTP id 69372160013 for ; Fri, 22 Nov 2024 15:54:19 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=linutronix.de header.s=2020 header.b=dE3cTHlA; dkim=pass header.d=linutronix.de header.s=2020e header.b=c1juVXva; dmarc=pass (policy=none) header.from=linutronix.de; spf=pass (imf08.hostedemail.com: domain of bigeasy@linutronix.de designates 193.142.43.55 as permitted sender) smtp.mailfrom=bigeasy@linutronix.de ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732290827; a=rsa-sha256; cv=none; b=NqG7mMntZg+Jy6yyeiE4p+jIVVqjQk0/V5hZEkEFtrN+x2M+SI3uNkwINknUNiwWbWKxo3 tQNtDQj/JUfhaOrfmAIrGPWKpCZDanKI7qvEELQhKxbtiIhMiL0WxayXPaSZJxmcL7gojL qbVF0D7B50sSD1MU1GbKjf/aGMqHzeY= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=linutronix.de header.s=2020 header.b=dE3cTHlA; dkim=pass header.d=linutronix.de header.s=2020e header.b=c1juVXva; dmarc=pass (policy=none) header.from=linutronix.de; spf=pass (imf08.hostedemail.com: domain of bigeasy@linutronix.de designates 193.142.43.55 as permitted sender) smtp.mailfrom=bigeasy@linutronix.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732290827; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ZbQPnVZqE+G9GCldWBdvS97jPY1ZKwkWnp4Cqk0q/4c=; b=4FHVoZZ8QlmsGbW0BKMWigdhc2BQtKhKIDOb/4UVlxHdK+VWRPNflsAQAzAW6nKrk/ngD/ Vh5wW8cKkWgzFHfiB34QNkq7DP63K5Wt8JeXbh4ZIXXfHk39Nu8xUdl8pOiD4IMsYonGbS QTZ2ToMtu4q+MA7egLlLWS924/+MBSI= Date: Fri, 22 Nov 2024 16:54:51 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1732290893; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ZbQPnVZqE+G9GCldWBdvS97jPY1ZKwkWnp4Cqk0q/4c=; b=dE3cTHlAmV6SQCh/hIIKb1EH3qWyk/DQ9F1NlLIFayhk1DOzId1QyRaM2XjvFXqowf3NBt ExHCn+6ZAlYOoWgutnqtL6YzWbF1/DlNIBLVXjtWpqf9k7XFTP9F/n7bwqdy37UOb4q3YJ /3n4OeknXfMpegog2CNel1988YguShNX0zVyZPUpjhukEWKp7nuBFTP5f7n2Cf9kPd2HSw RtVzEHL8zs+VJ+OnwxH8sgL9OOxwArEICOXIUeiwnYmPXTU2hF7mUv5pOxg+EuRcjl/YYp O7xc/mKlSknU9ChHBn0XeN18SV6kEs1pobYj9ckMpF4Xjasf12JKiz5hWKxZJA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1732290893; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ZbQPnVZqE+G9GCldWBdvS97jPY1ZKwkWnp4Cqk0q/4c=; b=c1juVXva7gE2YlTOtf3dCdu6XZ7N/p1rXxISDXAHeG68isyz78O3FVRRF5C+AkDbEGPz+t IJjpwxgAbjyj8XBA== From: Sebastian Andrzej Siewior To: Andrey Konovalov Cc: Marco Elver , Peter Zijlstra , Vlastimil Babka , syzbot , Liam.Howlett@oracle.com, akpm@linux-foundation.org, jannh@google.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, lorenzo.stoakes@oracle.com, syzkaller-bugs@googlegroups.com, kasan-dev , Andrey Ryabinin , Alexander Potapenko , Waiman Long , dvyukov@google.com, vincenzo.frascino@arm.com, paulmck@kernel.org, frederic@kernel.org, neeraj.upadhyay@kernel.org, joel@joelfernandes.org, josh@joshtriplett.org, boqun.feng@gmail.com, urezki@gmail.com, rostedt@goodmis.org, mathieu.desnoyers@efficios.com, jiangshanlai@gmail.com, qiang.zhang1211@gmail.com, mingo@redhat.com, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, bsegall@google.com, mgorman@suse.de, vschneid@redhat.com, tj@kernel.org, cl@linux.com, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, Thomas Gleixner , roman.gushchin@linux.dev, 42.hyeyoo@gmail.com, rcu@vger.kernel.org Subject: [PATCH v2] kasan: Make kasan_record_aux_stack_noalloc() the default behaviour Message-ID: <20241122155451.Mb2pmeyJ@linutronix.de> References: <67275485.050a0220.3c8d68.0a37.GAE@google.com> <20241104114506.GC24862@noisy.programming.kicks-ass.net> <20241119155701.GYennzPF@linutronix.de> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Rspam-User: X-Rspamd-Queue-Id: 69372160013 X-Rspamd-Server: rspam01 X-Stat-Signature: pb3d98ysstmxmsbhh78ohhqzm3g4h4k3 X-HE-Tag: 1732290859-123304 X-HE-Meta: U2FsdGVkX19SO2vBu8C+aHbXZR6YT/e+nALRiUJfpT1pInOoUyyC92NS8yWUdbo8q9Dgstf4BC/XKcH78veQfVoJsEy3JKmvODmJW4wjEdfA4EHgbGux77+jVkI7bWd4jIRRiYcpNiS9PGKZsoRJ/6nPzoHyhpGhWt3gXMm5kUz2ehhXwGDT2oIST+ST9duTNO5uJ4Z1zdYZSxBkd+A44zooCSEwff+VTFFJ9+HsTizmib8WAJz/r94QFFL+UIK9ltm+wdSn1wKxjcLL2q5Y1PVN1xGsFVE+jC+kAeC6AjL2Sf013R+5EJfN84HJWwQv/bTG82hlCfrVhw+IWu0sufaAKJkwe9Y5np1moDL5mi8Q7XjrF28NdAw8hchWLvlMlaNl5HmjYjLT4YKVxHynjTOqoKPjQ1yDHibyZeUhUp+ZF5J2tvsuKXBug1rjV5mfiWShUQG8p30hhTR9GlNJQ8F/LxT/dlJcsIXB4vck3khv2KN/LSHTQdtQZgVOSGOYrKuW5SdHsSHU0z/g6CT0zFqa35KtW17g4GBaZj22fA4i5ucrOoiRmsy98bZp7osshZs9ARO71TtxbRwDw4hC6aMS9zSkBAMNhLMrCBpVMALsnU4ESNTzFlfr8Q30SbE6OyReWB+vKflSyj0oAGHsuW8rn1m2mok9k6xkMphxFQz3sx5OwjNWU0EtK7Pl/N1CApcnBACb/Z5qGWBqUA4e/8OGZvzdcPI2tbaRE7/pnCQKAOD/haep4LI4nR9kxjMsBWG24e4gYz9ngTF6YABWJG/x+AL4m8Ir2CqGOVTzYBqN+nwaHUmt9NdSizTpDA5B3CTi+btAnlYEurZ9O3NOQhdxht/6QOp7fDw/ehUXdz+GPaqImiZQduzzqifjEHGKT/wvcta9JZk+k/mfzSvRYggKF0VcK3GteAfWAhsYkkxZaAUTvauf5U1th8IXE5rH6WbLe7peKYNq8KjLXfx br16FDTS +5k8CBkWOjyVAx09CRDSYGKqynTigXlrmEv+OBtVdbam/HLsVD9ZselA5iXNn4t1Kaowhrt4H+0NAljYyA6915Wfpfg60uUmZ+KFKnLryyV2gu95grcQBO/8nefuRQp9NMtx9NL+uMqHGDal4z8aoW+SqTatCOxgrFOUH8UyA1DVrQqI6HMf4YU8HprKbOg34ONihByKcWYsnffr1+afy3BWQ7XXxeiVmxzifcFChPmUYbMbsEZbfDKblOEB87cagBG0obGh02haj6GbFAxosjCwAjqN3pLvZ6EZNaXryAt39H9qjKmzlTl5tSZNBfyUINv0KafpX+0SNkxYINDUeVDn6pTcBXX5wqtync1W3cgwdhjjdZKNjCFS05Qnyijhw1Upj7P7tglHkVZ5boW4QThK5aNhdAdKPq+w2kgPIh1GiVNZp0rghYueOzFXfW0rHJdr30gnAsGSxCvYeHAHs2aiVcyCPoHMwiWk6kVtmGCDou36/VgwygZpMfAAog3OPE6voPhLYWegy2GY= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Peter Zijlstra 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: Dressed the diff as patch. ] Reported-by: syzbot+39f85d612b7c20d8db48@syzkaller.appspotmail.com Closes: https://lore.kernel.org/all/67275485.050a0220.3c8d68.0a37.GAE@google.com Acked-by: Waiman Long Reviewed-by: Andrey Konovalov Reviewed-by: Marco Elver Fixes: 7cb3007ce2da2 ("kasan: generic: introduce kasan_record_aux_stack_noalloc()") Signed-off-by: Peter Zijlstra (Intel) Signed-off-by: Sebastian Andrzej Siewior Reviewed-by: Waiman Long --- v1…v2: - Renamed the patch as per Marco. - Added comment to kasan_record_aux_stack() as per Andrey. - Added fixes tag since Waiman that it is the only user. - Added Marco's quote from the mail to the commit description. include/linux/kasan.h | 2 -- include/linux/task_work.h | 3 --- kernel/irq_work.c | 2 +- kernel/rcu/tiny.c | 2 +- kernel/rcu/tree.c | 4 ++-- kernel/sched/core.c | 2 +- kernel/task_work.c | 14 +------------- kernel/workqueue.c | 2 +- mm/kasan/generic.c | 18 ++++++------------ mm/slub.c | 2 +- 10 files changed, 14 insertions(+), 37 deletions(-) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index 00a3bf7c0d8f0..1a623818e8b39 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -488,7 +488,6 @@ void kasan_cache_create(struct kmem_cache *cache, unsigned int *size, void kasan_cache_shrink(struct kmem_cache *cache); void kasan_cache_shutdown(struct kmem_cache *cache); void kasan_record_aux_stack(void *ptr); -void kasan_record_aux_stack_noalloc(void *ptr); #else /* CONFIG_KASAN_GENERIC */ @@ -506,7 +505,6 @@ static inline void kasan_cache_create(struct kmem_cache *cache, static inline void kasan_cache_shrink(struct kmem_cache *cache) {} static inline void kasan_cache_shutdown(struct kmem_cache *cache) {} static inline void kasan_record_aux_stack(void *ptr) {} -static inline void kasan_record_aux_stack_noalloc(void *ptr) {} #endif /* CONFIG_KASAN_GENERIC */ diff --git a/include/linux/task_work.h b/include/linux/task_work.h index 2964171856e00..0646804860ff1 100644 --- a/include/linux/task_work.h +++ b/include/linux/task_work.h @@ -19,9 +19,6 @@ enum task_work_notify_mode { TWA_SIGNAL, TWA_SIGNAL_NO_IPI, TWA_NMI_CURRENT, - - TWA_FLAGS = 0xff00, - TWAF_NO_ALLOC = 0x0100, }; static inline bool task_work_pending(struct task_struct *task) diff --git a/kernel/irq_work.c b/kernel/irq_work.c index 2f4fb336dda17..73f7e1fd4ab4d 100644 --- a/kernel/irq_work.c +++ b/kernel/irq_work.c @@ -147,7 +147,7 @@ bool irq_work_queue_on(struct irq_work *work, int cpu) if (!irq_work_claim(work)) return false; - kasan_record_aux_stack_noalloc(work); + kasan_record_aux_stack(work); preempt_disable(); if (cpu != smp_processor_id()) { diff --git a/kernel/rcu/tiny.c b/kernel/rcu/tiny.c index b3b3ce34df631..4b3f319114650 100644 --- a/kernel/rcu/tiny.c +++ b/kernel/rcu/tiny.c @@ -250,7 +250,7 @@ EXPORT_SYMBOL_GPL(poll_state_synchronize_rcu); void kvfree_call_rcu(struct rcu_head *head, void *ptr) { if (head) - kasan_record_aux_stack_noalloc(ptr); + kasan_record_aux_stack(ptr); __kvfree_call_rcu(head, ptr); } diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c index b1f883fcd9185..7eae9bd818a90 100644 --- a/kernel/rcu/tree.c +++ b/kernel/rcu/tree.c @@ -3083,7 +3083,7 @@ __call_rcu_common(struct rcu_head *head, rcu_callback_t func, bool lazy_in) } head->func = func; head->next = NULL; - kasan_record_aux_stack_noalloc(head); + kasan_record_aux_stack(head); local_irq_save(flags); rdp = this_cpu_ptr(&rcu_data); lazy = lazy_in && !rcu_async_should_hurry(); @@ -3807,7 +3807,7 @@ void kvfree_call_rcu(struct rcu_head *head, void *ptr) return; } - kasan_record_aux_stack_noalloc(ptr); + kasan_record_aux_stack(ptr); success = add_ptr_to_bulk_krc_lock(&krcp, &flags, ptr, !head); if (!success) { run_page_cache_worker(krcp); diff --git a/kernel/sched/core.c b/kernel/sched/core.c index a1c353a62c568..3717360a940d2 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -10485,7 +10485,7 @@ void task_tick_mm_cid(struct rq *rq, struct task_struct *curr) return; /* No page allocation under rq lock */ - task_work_add(curr, work, TWA_RESUME | TWAF_NO_ALLOC); + task_work_add(curr, work, TWA_RESUME); } void sched_mm_cid_exit_signals(struct task_struct *t) diff --git a/kernel/task_work.c b/kernel/task_work.c index c969f1f26be58..d1efec571a4a4 100644 --- a/kernel/task_work.c +++ b/kernel/task_work.c @@ -55,26 +55,14 @@ int task_work_add(struct task_struct *task, struct callback_head *work, enum task_work_notify_mode notify) { struct callback_head *head; - int flags = notify & TWA_FLAGS; - notify &= ~TWA_FLAGS; if (notify == TWA_NMI_CURRENT) { if (WARN_ON_ONCE(task != current)) return -EINVAL; if (!IS_ENABLED(CONFIG_IRQ_WORK)) return -EINVAL; } else { - /* - * Record the work call stack in order to print it in KASAN - * reports. - * - * Note that stack allocation can fail if TWAF_NO_ALLOC flag - * is set and new page is needed to expand the stack buffer. - */ - if (flags & TWAF_NO_ALLOC) - kasan_record_aux_stack_noalloc(work); - else - kasan_record_aux_stack(work); + kasan_record_aux_stack(work); } head = READ_ONCE(task->task_works); diff --git a/kernel/workqueue.c b/kernel/workqueue.c index 9949ffad8df09..65b8314b2d538 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -2180,7 +2180,7 @@ static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, debug_work_activate(work); /* record the work call stack in order to print it in KASAN reports */ - kasan_record_aux_stack_noalloc(work); + kasan_record_aux_stack(work); /* we own @work, set data and link */ set_work_pwq(work, pwq, extra_flags); diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c index 6310a180278b6..2242249c2d50d 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -521,7 +521,11 @@ size_t kasan_metadata_size(struct kmem_cache *cache, bool in_object) sizeof(struct kasan_free_meta) : 0); } -static void __kasan_record_aux_stack(void *addr, depot_flags_t depot_flags) +/* + * This function avoids dynamic memory allocations and thus can be called from + * contexts that do not allow allocating memory. + */ +void kasan_record_aux_stack(void *addr) { struct slab *slab = kasan_addr_to_slab(addr); struct kmem_cache *cache; @@ -538,17 +542,7 @@ static void __kasan_record_aux_stack(void *addr, depot_flags_t depot_flags) return; alloc_meta->aux_stack[1] = alloc_meta->aux_stack[0]; - alloc_meta->aux_stack[0] = kasan_save_stack(0, depot_flags); -} - -void kasan_record_aux_stack(void *addr) -{ - return __kasan_record_aux_stack(addr, STACK_DEPOT_FLAG_CAN_ALLOC); -} - -void kasan_record_aux_stack_noalloc(void *addr) -{ - return __kasan_record_aux_stack(addr, 0); + alloc_meta->aux_stack[0] = kasan_save_stack(0, 0); } void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags) diff --git a/mm/slub.c b/mm/slub.c index 5b832512044e3..b8c4bf3fe0d07 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -2300,7 +2300,7 @@ bool slab_free_hook(struct kmem_cache *s, void *x, bool init, * We have to do this manually because the rcu_head is * not located inside the object. */ - kasan_record_aux_stack_noalloc(x); + kasan_record_aux_stack(x); delayed_free->object = x; call_rcu(&delayed_free->head, slab_free_after_rcu_debug);