From patchwork Wed Mar 23 15:32:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 12789842 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 EA4CEC433EF for ; Wed, 23 Mar 2022 15:33:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 78DD26B0074; Wed, 23 Mar 2022 11:33:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7176D6B0075; Wed, 23 Mar 2022 11:33:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5915E6B0078; Wed, 23 Mar 2022 11:33:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0060.hostedemail.com [216.40.44.60]) by kanga.kvack.org (Postfix) with ESMTP id 48F236B0074 for ; Wed, 23 Mar 2022 11:33:05 -0400 (EDT) Received: from smtpin29.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 420D918286210 for ; Wed, 23 Mar 2022 15:33:04 +0000 (UTC) X-FDA: 79276044288.29.8B9557A Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by imf12.hostedemail.com (Postfix) with ESMTP id 961D74003B for ; Wed, 23 Mar 2022 15:33:03 +0000 (UTC) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1648049582; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Mmc3nZQPUIWzZbIw9R7zdg/6fIrR3qfP7vluF20CZjk=; b=UTJfa80A9sjfbSyh7XvFmbslezbBssH3pgpUdBaLBgs3IL2A3I6NA415UycL5yEGhbwHb6 vpUhIr+s+DTQPNB7OYobvxHrvMaOkJFmdVvDpJbnJ21kD5Gc+xjflW3QFSbiifKEwW887J jc+p/iTWkpBgyYYx+H4uOeaKNyAMx8U= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko , Catalin Marinas , Will Deacon , Andrew Morton Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Mark Rutland , Vincenzo Frascino , Sami Tolvanen , Peter Collingbourne , Evgenii Stepanov , Florian Mayer , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v2 1/4] stacktrace: add interface based on shadow call stack Date: Wed, 23 Mar 2022 16:32:52 +0100 Message-Id: <21e3e20ea58e242e3c82c19abbfe65b579e0e4b8.1648049113.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=UTJfa80A; spf=pass (imf12.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Rspam-User: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 961D74003B X-Stat-Signature: 8yh5fzmtg9s4cs3ayrme8b9d8un67asa X-HE-Tag: 1648049583-378539 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: From: Andrey Konovalov Add a new interface stack_trace_save_shadow() for collecting stack traces by copying frames from the Shadow Call Stack. Collecting stack traces this way is significantly faster: boot time of a defconfig build with KASAN enabled gets descreased by ~30%. The few patches following this one add an implementation of stack_trace_save_shadow() for arm64. The implementation of the added interface is not meant to use stack_trace_consume_fn to avoid making a function call for each collected frame to further improve performance. Signed-off-by: Andrey Konovalov --- arch/Kconfig | 6 ++++++ include/linux/stacktrace.h | 15 +++++++++++++++ kernel/stacktrace.c | 21 +++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/arch/Kconfig b/arch/Kconfig index e12a4268c01d..207c1679c53a 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -1041,6 +1041,12 @@ config HAVE_RELIABLE_STACKTRACE arch_stack_walk_reliable() function which only returns a stack trace if it can guarantee the trace is reliable. +config HAVE_SHADOW_STACKTRACE + bool + help + If this is set, the architecture provides the arch_stack_walk_shadow() + function, which collects the stack trace from the shadow call stack. + config HAVE_ARCH_HASH bool default n diff --git a/include/linux/stacktrace.h b/include/linux/stacktrace.h index 97455880ac41..b74d1e42e157 100644 --- a/include/linux/stacktrace.h +++ b/include/linux/stacktrace.h @@ -60,6 +60,9 @@ int arch_stack_walk_reliable(stack_trace_consume_fn consume_entry, void *cookie, void arch_stack_walk_user(stack_trace_consume_fn consume_entry, void *cookie, const struct pt_regs *regs); + +int arch_stack_walk_shadow(unsigned long *store, unsigned int size, + unsigned int skipnr); #endif /* CONFIG_ARCH_STACKWALK */ #ifdef CONFIG_STACKTRACE @@ -108,4 +111,16 @@ static inline int stack_trace_save_tsk_reliable(struct task_struct *tsk, } #endif +#if defined(CONFIG_STACKTRACE) && defined(CONFIG_HAVE_SHADOW_STACKTRACE) +int stack_trace_save_shadow(unsigned long *store, unsigned int size, + unsigned int skipnr); +#else +static inline int stack_trace_save_shadow(unsigned long *store, + unsigned int size, + unsigned int skipnr) +{ + return -ENOSYS; +} +#endif + #endif /* __LINUX_STACKTRACE_H */ diff --git a/kernel/stacktrace.c b/kernel/stacktrace.c index 9ed5ce989415..fe305861fd55 100644 --- a/kernel/stacktrace.c +++ b/kernel/stacktrace.c @@ -237,6 +237,27 @@ unsigned int stack_trace_save_user(unsigned long *store, unsigned int size) } #endif +#ifdef CONFIG_HAVE_SHADOW_STACKTRACE +/** + * stack_trace_save_shadow - Save a stack trace based on shadow call stack + * @store: Pointer to the storage array + * @size: Size of the storage array + * @skipnr: Number of entries to skip at the start of the stack trace + * + * Return: Number of trace entries stored. + */ +int stack_trace_save_shadow(unsigned long *store, unsigned int size, + unsigned int skipnr) +{ + /* + * Do not use stack_trace_consume_fn to avoid making a function + * call for each collected frame to improve performance. + * Skip + 1 frame to skip stack_trace_save_shadow. + */ + return arch_stack_walk_shadow(store, size, skipnr + 1); +} +#endif + #else /* CONFIG_ARCH_STACKWALK */ /* From patchwork Wed Mar 23 15:32:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 12789843 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 B52D1C433F5 for ; Wed, 23 Mar 2022 15:33:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DAD986B0075; Wed, 23 Mar 2022 11:33:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D31596B0078; Wed, 23 Mar 2022 11:33:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B86016B007B; Wed, 23 Mar 2022 11:33:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0106.hostedemail.com [216.40.44.106]) by kanga.kvack.org (Postfix) with ESMTP id 96EEE6B0078 for ; Wed, 23 Mar 2022 11:33:05 -0400 (EDT) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 5C7F618286210 for ; Wed, 23 Mar 2022 15:33:05 +0000 (UTC) X-FDA: 79276044330.28.4D5E98D Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by imf27.hostedemail.com (Postfix) with ESMTP id AF33240035 for ; Wed, 23 Mar 2022 15:33:04 +0000 (UTC) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1648049583; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=T6yJKZfG38tzu4tVtlVcnWPw3LrS2FpvC9JjJySIrEg=; b=EQv2T55iO0XNuE9Zocz+eSnxdpOE37a0eKTUoOWjQBf0G/rtWzaOW94txvknnNt6fjH8rH TMhoiE+1d2dOiYsiXfYUwVsvDk9DQN2SMx0EnangRmRQWooE8wLmODk2yHTk7sEwRvtlAj gVqG0GwWr/hvjjulhh0g+wg3PbIeo/o= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko , Catalin Marinas , Will Deacon , Andrew Morton Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Mark Rutland , Vincenzo Frascino , Sami Tolvanen , Peter Collingbourne , Evgenii Stepanov , Florian Mayer , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v2 2/4] arm64, scs: save scs_sp values per-cpu when switching stacks Date: Wed, 23 Mar 2022 16:32:53 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: AF33240035 X-Stat-Signature: 63dyijutaxkxq7s1q4qqpfp7c1c81aef X-Rspam-User: Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=EQv2T55i; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf27.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-HE-Tag: 1648049584-634850 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: From: Andrey Konovalov When an interrupt happens, the current Shadow Call Stack (SCS) pointer is switched to a per-interrupt one stored in a per-CPU variable. The old pointer is then saved on the normal stack and restored when the interrupt is handled. To collect the current stack trace based on SCS when the interrupt is being handled, we need to know the SCS pointers that belonged to the task and potentially other interrupts that were interrupted. Instead of trying to retrieve the SCS pointers from the stack, change interrupt handlers (for hard IRQ, Normal and Critical SDEI) to save the previous SCS pointer in a per-CPU variable. Note that interrupts stack. A task can be interrupted by a hard IRQ, which then can interrupted by a normal SDEI, etc. This is handled by using a separate per-CPU variable for each interrupt type. Also reset the saved SCS pointer when exiting the interrupt. This allows checking whether we should include any interrupt frames when collecting the stack trace. While we could use in_hardirq(), there seems to be no easy way to check whether we are in an SDEI handler. Directly checking the per-CPU variables for being non-zero is more resilient. Also expose both the the added saved SCS variables and the existing SCS base variables in arch/arm64/include/asm/scs.h so that the stack trace collection impementation can use them. Signed-off-by: Andrey Konovalov Reported-by: kernel test robot Reported-by: kernel test robot --- arch/arm64/include/asm/assembler.h | 12 ++++++++++++ arch/arm64/include/asm/scs.h | 13 ++++++++++++- arch/arm64/kernel/entry.S | 28 ++++++++++++++++++++++++---- arch/arm64/kernel/irq.c | 4 +--- arch/arm64/kernel/sdei.c | 5 ++--- 5 files changed, 51 insertions(+), 11 deletions(-) diff --git a/arch/arm64/include/asm/assembler.h b/arch/arm64/include/asm/assembler.h index 8c5a61aeaf8e..ca018e981d13 100644 --- a/arch/arm64/include/asm/assembler.h +++ b/arch/arm64/include/asm/assembler.h @@ -270,6 +270,18 @@ alternative_endif ldr \dst, [\dst, \tmp] .endm + /* + * @src: Register whose value gets stored in sym + * @sym: The name of the per-cpu variable + * @tmp0: Scratch register + * @tmp1: Another scratch register + */ + .macro str_this_cpu src, sym, tmp0, tmp1 + adr_l \tmp0, \sym + get_this_cpu_offset \tmp1 + str \src, [\tmp0, \tmp1] + .endm + /* * vma_vm_mm - get mm pointer from vma pointer (vma->vm_mm) */ diff --git a/arch/arm64/include/asm/scs.h b/arch/arm64/include/asm/scs.h index 8297bccf0784..2bb2b32f787b 100644 --- a/arch/arm64/include/asm/scs.h +++ b/arch/arm64/include/asm/scs.h @@ -24,6 +24,17 @@ .endm #endif /* CONFIG_SHADOW_CALL_STACK */ -#endif /* __ASSEMBLY __ */ +#else /* __ASSEMBLY__ */ + +#include + +DECLARE_PER_CPU(unsigned long *, irq_shadow_call_stack_ptr); +DECLARE_PER_CPU(unsigned long *, irq_shadow_call_stack_saved_ptr); +DECLARE_PER_CPU(unsigned long *, sdei_shadow_call_stack_normal_ptr); +DECLARE_PER_CPU(unsigned long *, sdei_shadow_call_stack_normal_saved_ptr); +DECLARE_PER_CPU(unsigned long *, sdei_shadow_call_stack_critical_ptr); +DECLARE_PER_CPU(unsigned long *, sdei_shadow_call_stack_critical_saved_ptr); + +#endif /* __ASSEMBLY__ */ #endif /* _ASM_SCS_H */ diff --git a/arch/arm64/kernel/entry.S b/arch/arm64/kernel/entry.S index ede028dee81b..1c62fecda172 100644 --- a/arch/arm64/kernel/entry.S +++ b/arch/arm64/kernel/entry.S @@ -880,7 +880,8 @@ NOKPROBE(ret_from_fork) */ SYM_FUNC_START(call_on_irq_stack) #ifdef CONFIG_SHADOW_CALL_STACK - stp scs_sp, xzr, [sp, #-16]! + /* Save the current SCS pointer and load the per-IRQ one. */ + str_this_cpu scs_sp, irq_shadow_call_stack_saved_ptr, x15, x17 ldr_this_cpu scs_sp, irq_shadow_call_stack_ptr, x17 #endif /* Create a frame record to save our LR and SP (implicit in FP) */ @@ -902,7 +903,9 @@ SYM_FUNC_START(call_on_irq_stack) mov sp, x29 ldp x29, x30, [sp], #16 #ifdef CONFIG_SHADOW_CALL_STACK - ldp scs_sp, xzr, [sp], #16 + /* Restore saved SCS pointer and reset the saved value. */ + ldr_this_cpu scs_sp, irq_shadow_call_stack_saved_ptr, x17 + str_this_cpu xzr, irq_shadow_call_stack_saved_ptr, x15, x17 #endif ret SYM_FUNC_END(call_on_irq_stack) @@ -1024,11 +1027,16 @@ SYM_CODE_START(__sdei_asm_handler) #endif #ifdef CONFIG_SHADOW_CALL_STACK - /* Use a separate shadow call stack for normal and critical events */ + /* + * Use a separate shadow call stack for normal and critical events. + * Save the current SCS pointer and load the per-SDEI one. + */ cbnz w4, 3f + str_this_cpu src=scs_sp, sym=sdei_shadow_call_stack_normal_saved_ptr, tmp0=x5, tmp1=x6 ldr_this_cpu dst=scs_sp, sym=sdei_shadow_call_stack_normal_ptr, tmp=x6 b 4f -3: ldr_this_cpu dst=scs_sp, sym=sdei_shadow_call_stack_critical_ptr, tmp=x6 +3: str_this_cpu src=scs_sp, sym=sdei_shadow_call_stack_critical_saved_ptr, tmp0=x5, tmp1=x6 + ldr_this_cpu dst=scs_sp, sym=sdei_shadow_call_stack_critical_ptr, tmp=x6 4: #endif @@ -1062,6 +1070,18 @@ SYM_CODE_START(__sdei_asm_handler) ldp lr, x1, [x4, #SDEI_EVENT_INTREGS + S_LR] mov sp, x1 +#ifdef CONFIG_SHADOW_CALL_STACK + /* Restore saved SCS pointer and reset the saved value. */ + ldrb w5, [x4, #SDEI_EVENT_PRIORITY] + cbnz w5, 5f + ldr_this_cpu dst=scs_sp, sym=sdei_shadow_call_stack_normal_saved_ptr, tmp=x6 + str_this_cpu src=xzr, sym=sdei_shadow_call_stack_normal_saved_ptr, tmp0=x5, tmp1=x6 + b 6f +5: ldr_this_cpu dst=scs_sp, sym=sdei_shadow_call_stack_critical_saved_ptr, tmp=x6 + str_this_cpu src=xzr, sym=sdei_shadow_call_stack_critical_saved_ptr, tmp0=x5, tmp1=x6 +6: +#endif + mov x1, x0 // address to complete_and_resume /* x0 = (x0 <= SDEI_EV_FAILED) ? * EVENT_COMPLETE:EVENT_COMPLETE_AND_RESUME diff --git a/arch/arm64/kernel/irq.c b/arch/arm64/kernel/irq.c index bda49430c9ea..4199f900714a 100644 --- a/arch/arm64/kernel/irq.c +++ b/arch/arm64/kernel/irq.c @@ -28,11 +28,9 @@ DEFINE_PER_CPU(struct nmi_ctx, nmi_contexts); DEFINE_PER_CPU(unsigned long *, irq_stack_ptr); - -DECLARE_PER_CPU(unsigned long *, irq_shadow_call_stack_ptr); - #ifdef CONFIG_SHADOW_CALL_STACK DEFINE_PER_CPU(unsigned long *, irq_shadow_call_stack_ptr); +DEFINE_PER_CPU(unsigned long *, irq_shadow_call_stack_saved_ptr); #endif static void init_irq_scs(void) diff --git a/arch/arm64/kernel/sdei.c b/arch/arm64/kernel/sdei.c index d20620a1c51a..269adcb9e854 100644 --- a/arch/arm64/kernel/sdei.c +++ b/arch/arm64/kernel/sdei.c @@ -39,12 +39,11 @@ DEFINE_PER_CPU(unsigned long *, sdei_stack_normal_ptr); DEFINE_PER_CPU(unsigned long *, sdei_stack_critical_ptr); #endif -DECLARE_PER_CPU(unsigned long *, sdei_shadow_call_stack_normal_ptr); -DECLARE_PER_CPU(unsigned long *, sdei_shadow_call_stack_critical_ptr); - #ifdef CONFIG_SHADOW_CALL_STACK DEFINE_PER_CPU(unsigned long *, sdei_shadow_call_stack_normal_ptr); +DEFINE_PER_CPU(unsigned long *, sdei_shadow_call_stack_normal_saved_ptr); DEFINE_PER_CPU(unsigned long *, sdei_shadow_call_stack_critical_ptr); +DEFINE_PER_CPU(unsigned long *, sdei_shadow_call_stack_critical_saved_ptr); #endif static void _free_sdei_stack(unsigned long * __percpu *ptr, int cpu) From patchwork Wed Mar 23 15:32:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 12789844 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 AB4B9C433EF for ; Wed, 23 Mar 2022 15:33:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A51EF6B0078; Wed, 23 Mar 2022 11:33:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9D7D66B007B; Wed, 23 Mar 2022 11:33:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 78E9E6B007D; Wed, 23 Mar 2022 11:33:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0046.hostedemail.com [216.40.44.46]) by kanga.kvack.org (Postfix) with ESMTP id 5E9E26B0078 for ; Wed, 23 Mar 2022 11:33:06 -0400 (EDT) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 272C11828DEBB for ; Wed, 23 Mar 2022 15:33:06 +0000 (UTC) X-FDA: 79276044372.20.B72D0D0 Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by imf07.hostedemail.com (Postfix) with ESMTP id 5C7AC4003F for ; Wed, 23 Mar 2022 15:33:05 +0000 (UTC) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1648049584; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=MP8VVStpf2u5pFq9LJYVTfW5maMxRrDNwrS4C/iOhFE=; b=CHABMq/eZMH3CKmnepGiem7lAUyvtGITme8Qll4LQCROHN+V6C06OVGLwZuteRZ+PbizzX B/d0tsbkAj03Q2KNrb7PC4vE3rYeOl8qckRDCHswffvIEUY+QTm6icfg6nxgM6fpgtv52V PXKw6cD+WU2EotoizAcwfEiGoH3Y8cA= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko , Catalin Marinas , Will Deacon , Andrew Morton Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Mark Rutland , Vincenzo Frascino , Sami Tolvanen , Peter Collingbourne , Evgenii Stepanov , Florian Mayer , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v2 3/4] arm64: implement stack_trace_save_shadow Date: Wed, 23 Mar 2022 16:32:54 +0100 Message-Id: <0bb72ea8fa88ef9ae3508c23d993952a0ae6f0f9.1648049113.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev X-Stat-Signature: rjwyzh8yq8f1ebrdh7izupc9dsr97kq6 Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="CHABMq/e"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf07.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev X-Rspam-User: X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 5C7AC4003F X-HE-Tag: 1648049585-483019 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: From: Andrey Konovalov Implement the stack_trace_save_shadow() interface that collects stack traces based on the Shadow Call Stack (SCS) for arm64. The implementation walks through available SCS pointers (the per-task one and the per-interrupt-type ones) and copies the frames. Note that the frame of the interrupted function is not included into the stack trace, as it is not yet saved on the SCS when an interrupt happens. Signed-off-by: Andrey Konovalov Reported-by: kernel test robot --- arch/arm64/Kconfig | 1 + arch/arm64/kernel/stacktrace.c | 83 ++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index a659e238f196..d89cecf6c923 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -201,6 +201,7 @@ config ARM64 select MMU_GATHER_RCU_TABLE_FREE select HAVE_RSEQ select HAVE_RUST + select HAVE_SHADOW_STACKTRACE select HAVE_STACKPROTECTOR select HAVE_SYSCALL_TRACEPOINTS select HAVE_KPROBES diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index e4103e085681..89daa710d91b 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -12,9 +12,11 @@ #include #include #include +#include #include #include +#include #include #include @@ -210,3 +212,84 @@ noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, walk_stackframe(task, &frame, consume_entry, cookie); } + +static const struct { + unsigned long ** __percpu saved; + unsigned long ** __percpu base; +} scs_parts[] = { +#ifdef CONFIG_ARM_SDE_INTERFACE + { + .saved = &sdei_shadow_call_stack_critical_saved_ptr, + .base = &sdei_shadow_call_stack_critical_ptr, + }, + { + .saved = &sdei_shadow_call_stack_normal_saved_ptr, + .base = &sdei_shadow_call_stack_normal_ptr, + }, +#endif /* CONFIG_ARM_SDE_INTERFACE */ + { + .saved = &irq_shadow_call_stack_saved_ptr, + .base = &irq_shadow_call_stack_ptr, + }, +}; + +static inline bool walk_shadow_stack_part( + unsigned long *scs_top, unsigned long *scs_base, + unsigned long *store, unsigned int size, + unsigned int *skipnr, unsigned int *len) +{ + unsigned long *frame; + + for (frame = scs_top; frame >= scs_base; frame--) { + if (*skipnr > 0) { + (*skipnr)--; + continue; + } + /* + * Do not leak PTR_AUTH tags in stack traces. + * Use READ_ONCE_NOCHECK as SCS is poisoned with Generic KASAN. + */ + store[(*len)++] = + ptrauth_strip_insn_pac(READ_ONCE_NOCHECK(*frame)); + if (*len >= size) + return true; + } + + return false; +} + +noinline notrace int arch_stack_walk_shadow(unsigned long *store, + unsigned int size, + unsigned int skipnr) +{ + unsigned long *scs_top, *scs_base, *scs_next; + unsigned int len = 0, part; + + preempt_disable(); + + /* Get the SCS pointer. */ + asm volatile("mov %0, x18" : "=&r" (scs_top)); + + /* The top SCS slot is empty. */ + scs_top -= 1; + + /* Handle SDEI and hardirq frames. */ + for (part = 0; part < ARRAY_SIZE(scs_parts); part++) { + scs_next = *this_cpu_ptr(scs_parts[part].saved); + if (scs_next) { + scs_base = *this_cpu_ptr(scs_parts[part].base); + if (walk_shadow_stack_part(scs_top, scs_base, store, + size, &skipnr, &len)) + goto out; + scs_top = scs_next; + } + } + + /* Handle task and softirq frames. */ + scs_base = task_scs(current); + walk_shadow_stack_part(scs_top, scs_base, store, size, &skipnr, &len); + +out: + preempt_enable(); + return len; +} From patchwork Wed Mar 23 15:32:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 12789845 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 B0545C433FE for ; Wed, 23 Mar 2022 15:33:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5E4E78D0001; Wed, 23 Mar 2022 11:33:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4F6216B007D; Wed, 23 Mar 2022 11:33:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2FB008D0001; Wed, 23 Mar 2022 11:33:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.28]) by kanga.kvack.org (Postfix) with ESMTP id 165D76B007B for ; Wed, 23 Mar 2022 11:33:07 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id DD0BD23184 for ; Wed, 23 Mar 2022 15:33:06 +0000 (UTC) X-FDA: 79276044372.07.2D83089 Received: from out0.migadu.com (out0.migadu.com [94.23.1.103]) by imf23.hostedemail.com (Postfix) with ESMTP id 3633B14002E for ; Wed, 23 Mar 2022 15:33:06 +0000 (UTC) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1648049585; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BKf6jB0gbxzfxKMTwtGAqqdjGZr2rSaHYmUmfoDqc2M=; b=BHH7IXDotxEtbDywjIVk+wOZWOrLCY8o1Y0wKr7beWzVUIIg6NcRjZo4Ng098t1O+hRQBY Hj+XGflhkg2q6NrW+V2es5S1tFSC2M9D+KPkQyagcNNNo5WUxx/gSWh9/nZ8KEiacAZ/0J wefcvgBqi5K7ERAx+/NxwhoJ1KWavK4= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko , Catalin Marinas , Will Deacon , Andrew Morton Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Mark Rutland , Vincenzo Frascino , Sami Tolvanen , Peter Collingbourne , Evgenii Stepanov , Florian Mayer , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v2 4/4] kasan: use stack_trace_save_shadow Date: Wed, 23 Mar 2022 16:32:55 +0100 Message-Id: <7027b9b6b0cae2921ff65739582ae499bf61470c.1648049113.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=BHH7IXDo; spf=pass (imf23.hostedemail.com: domain of andrey.konovalov@linux.dev designates 94.23.1.103 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Stat-Signature: hn4pqrqezoa3cr64okf17ei1rdfxtr8y X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 3633B14002E X-HE-Tag: 1648049586-370348 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: From: Andrey Konovalov Now that stack_trace_save_shadow() is implemented by arm64, use it whenever CONFIG_HAVE_SHADOW_STACKTRACE is enabled. This improves the boot time of a defconfig build by ~30% for all KASAN modes. Signed-off-by: Andrey Konovalov --- mm/kasan/common.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index d9079ec11f31..8d9d35c6562b 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -33,10 +33,13 @@ depot_stack_handle_t kasan_save_stack(gfp_t flags, bool can_alloc) { unsigned long entries[KASAN_STACK_DEPTH]; - unsigned int nr_entries; + unsigned int size; - nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0); - return __stack_depot_save(entries, nr_entries, flags, can_alloc); + if (IS_ENABLED(CONFIG_HAVE_SHADOW_STACKTRACE)) + size = stack_trace_save_shadow(entries, ARRAY_SIZE(entries), 0); + else + size = stack_trace_save(entries, ARRAY_SIZE(entries), 0); + return __stack_depot_save(entries, size, flags, can_alloc); } void kasan_set_track(struct kasan_track *track, gfp_t flags)