From patchwork Mon Jan 3 16:52:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Madhavan T. Venkataraman" X-Patchwork-Id: 12702673 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3920BC433FE for ; Mon, 3 Jan 2022 16:54:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=1TEywLRQxdMHNo9akPd84w3VRMvQY14/xPoTXN3UlGQ=; b=BdjBZAymUssauY oFF/fXU8GTokSiPNNBYcC28yKWBxeX6VQDqE5wiYFSMDxCY4E4IBWemy+1x8tySKPp9xJaaMl5O7f OZRO/raToMkqKCP4R2L+a1ztg4oS0eJKFKKQ+c7TaG/LtR6HLY+XzFdFCR8IdmAV4qw3ctYPtS2Uq cLVg0YUF/n5W+0tYjjbmaOYTNFEFgdF0xidHS8x2JRaZIKRkmuhmjwD+VrAnr20HCaMY0Lc2qf99M lAZD8Dz+tCWLRlLEHmIdly04u10X8UWlGtast2/0B5Mg3J31iaMyQc3M89EsoUPDzci0XG5MYjRb1 qb46ZLZMuAWhXrX0lSmg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1n4QZo-009YtT-By; Mon, 03 Jan 2022 16:52:52 +0000 Received: from linux.microsoft.com ([13.77.154.182]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1n4QZj-009Yqp-B0 for linux-arm-kernel@lists.infradead.org; Mon, 03 Jan 2022 16:52:48 +0000 Received: from x64host.home (unknown [47.187.212.181]) by linux.microsoft.com (Postfix) with ESMTPSA id CE75920B717A; Mon, 3 Jan 2022 08:52:44 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com CE75920B717A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1641228765; bh=YL0/rwGfLusLcXbXJfaP6PJh5+D2PxMoErb3hzo3tiU=; h=From:To:Subject:Date:In-Reply-To:References:From; b=GDc/rmeHIUeY0VZNBUg9VQctr1fTXiY1RP9TcKim+A4HKwoeO7IpOVOYc+emp90Jt MjbIlZReiueS5r8yh0mV6DKoMdMcFU03C+PTtO5L9QKwJHIoNgLsNQVkuFO9mbOg1Y aqgiAHniikgTsfk3KTMqlhrmZWSCOrbpeGFe2m0k= From: madvenka@linux.microsoft.com To: mark.rutland@arm.com, broonie@kernel.org, jpoimboe@redhat.com, ardb@kernel.org, nobuta.keiya@fujitsu.com, sjitindarsingh@gmail.com, catalin.marinas@arm.com, will@kernel.org, jmorris@namei.org, linux-arm-kernel@lists.infradead.org, live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, madvenka@linux.microsoft.com Subject: [PATCH v12 01/10] arm64: Remove NULL task check from unwind_frame() Date: Mon, 3 Jan 2022 10:52:03 -0600 Message-Id: <20220103165212.9303-2-madvenka@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220103165212.9303-1-madvenka@linux.microsoft.com> References: <0d0eb36f348fb5a6af6eb592c0525f6e94007328> <20220103165212.9303-1-madvenka@linux.microsoft.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220103_085247_506906_AB0D4D3F X-CRM114-Status: UNSURE ( 8.14 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: "Madhavan T. Venkataraman" Currently, there is a check for a NULL task in unwind_frame(). It is not needed since all current consumers pass a non-NULL task. Signed-off-by: Madhavan T. Venkataraman Reviewed-by: Mark Brown Reviewed-by: Mark Rutland --- arch/arm64/kernel/stacktrace.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index 0fb58fed54cb..5f5bb35b7b41 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -69,9 +69,6 @@ static int notrace unwind_frame(struct task_struct *tsk, unsigned long fp = frame->fp; struct stack_info info; - if (!tsk) - tsk = current; - /* Final frame; nothing to unwind */ if (fp == (unsigned long)task_pt_regs(tsk)->stackframe) return -ENOENT; From patchwork Mon Jan 3 16:52:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Madhavan T. Venkataraman" X-Patchwork-Id: 12702674 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A59BAC433EF for ; Mon, 3 Jan 2022 16:54:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=hsEMNDqGZzZFgOtvpzI5s4WPE5z0lQNBH9wne5waEVc=; b=s0jB8sIFOOo6ru DBdxf2/yheDWo6M6gEmI3r2cU1Obb9rYyZcE97CAaL+hxGVWKw4+mmvK7D+T9t6Ur14KucHlFenri kM7R8KptWgQcRpYx1S+fmPgJ4bCVbDb8EbsBdvIsVK5wcE6t2660JIMHz+TbhvULSaDUS7X5OctHZ mz5KzNZvTfSi1BVxHxDs5lcSvzYkU3n2AxDkm/QHRot1ZJrwU9t6ZNyqTQa9plaXfzvGo+Iya7lhN Jc+7DOwBA0K5aOuhYUd4Nne0LiSCo8Kdc4S40uomKePFyEibHr/gXV4XAuXuhjMH1+miybLFsIj1V 2MQib+kvomoeYYSJWu2w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1n4QZz-009Yzy-T1; Mon, 03 Jan 2022 16:53:04 +0000 Received: from linux.microsoft.com ([13.77.154.182]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1n4QZj-009Yr3-Sh for linux-arm-kernel@lists.infradead.org; Mon, 03 Jan 2022 16:52:50 +0000 Received: from x64host.home (unknown [47.187.212.181]) by linux.microsoft.com (Postfix) with ESMTPSA id CE69A20B717B; Mon, 3 Jan 2022 08:52:45 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com CE69A20B717B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1641228766; bh=0z4dz17XEhEO2tKNFKhxIoucu9B1B3gwz0Y3fd4GX/k=; h=From:To:Subject:Date:In-Reply-To:References:From; b=TUJsd1usC1aJ3pIXMUhT4sJG7FEFCHQ+om8kfgoPKUfrHyB1GlR6eDblR62j1iAUQ qnRgoXkXoWiBu1j9GWGjMKS+Ijc64sjrlV13bxZIDQQARDE6TG0pxO0VYHd7HBBS8K iLAmPnji8aeLiPg2YGPQgZPzixEhIS8RibT7kjR8= From: madvenka@linux.microsoft.com To: mark.rutland@arm.com, broonie@kernel.org, jpoimboe@redhat.com, ardb@kernel.org, nobuta.keiya@fujitsu.com, sjitindarsingh@gmail.com, catalin.marinas@arm.com, will@kernel.org, jmorris@namei.org, linux-arm-kernel@lists.infradead.org, live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, madvenka@linux.microsoft.com Subject: [PATCH v12 02/10] arm64: Rename unwinder functions Date: Mon, 3 Jan 2022 10:52:04 -0600 Message-Id: <20220103165212.9303-3-madvenka@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220103165212.9303-1-madvenka@linux.microsoft.com> References: <0d0eb36f348fb5a6af6eb592c0525f6e94007328> <20220103165212.9303-1-madvenka@linux.microsoft.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220103_085248_047960_621A9E17 X-CRM114-Status: GOOD ( 14.38 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: "Madhavan T. Venkataraman" Rename unwinder functions for consistency and better naming. - Rename start_backtrace() to unwind_init(). - Rename unwind_frame() to unwind_next(). - Rename walk_stackframe() to unwind(). Signed-off-by: Madhavan T. Venkataraman Reviewed-by: Mark Brown Acked-by: Mark Rutland Reviewed-by: Mark Rutland --- arch/arm64/kernel/stacktrace.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index 5f5bb35b7b41..b980d96dccfc 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -33,8 +33,8 @@ */ -static void start_backtrace(struct stackframe *frame, unsigned long fp, - unsigned long pc) +static void unwind_init(struct stackframe *frame, unsigned long fp, + unsigned long pc) { frame->fp = fp; frame->pc = pc; @@ -45,7 +45,7 @@ static void start_backtrace(struct stackframe *frame, unsigned long fp, /* * Prime the first unwind. * - * In unwind_frame() we'll check that the FP points to a valid stack, + * In unwind_next() we'll check that the FP points to a valid stack, * which can't be STACK_TYPE_UNKNOWN, and the first unwind will be * treated as a transition to whichever stack that happens to be. The * prev_fp value won't be used, but we set it to 0 such that it is @@ -63,8 +63,8 @@ static void start_backtrace(struct stackframe *frame, unsigned long fp, * records (e.g. a cycle), determined based on the location and fp value of A * and the location (but not the fp value) of B. */ -static int notrace unwind_frame(struct task_struct *tsk, - struct stackframe *frame) +static int notrace unwind_next(struct task_struct *tsk, + struct stackframe *frame) { unsigned long fp = frame->fp; struct stack_info info; @@ -104,7 +104,7 @@ static int notrace unwind_frame(struct task_struct *tsk, /* * Record this frame record's values and location. The prev_fp and - * prev_type are only meaningful to the next unwind_frame() invocation. + * prev_type are only meaningful to the next unwind_next() invocation. */ frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp)); frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp + 8)); @@ -137,23 +137,23 @@ static int notrace unwind_frame(struct task_struct *tsk, return 0; } -NOKPROBE_SYMBOL(unwind_frame); +NOKPROBE_SYMBOL(unwind_next); -static void notrace walk_stackframe(struct task_struct *tsk, - struct stackframe *frame, - bool (*fn)(void *, unsigned long), void *data) +static void notrace unwind(struct task_struct *tsk, + struct stackframe *frame, + bool (*fn)(void *, unsigned long), void *data) { while (1) { int ret; if (!fn(data, frame->pc)) break; - ret = unwind_frame(tsk, frame); + ret = unwind_next(tsk, frame); if (ret < 0) break; } } -NOKPROBE_SYMBOL(walk_stackframe); +NOKPROBE_SYMBOL(unwind); static bool dump_backtrace_entry(void *arg, unsigned long where) { @@ -195,14 +195,14 @@ noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, struct stackframe frame; if (regs) - start_backtrace(&frame, regs->regs[29], regs->pc); + unwind_init(&frame, regs->regs[29], regs->pc); else if (task == current) - start_backtrace(&frame, + unwind_init(&frame, (unsigned long)__builtin_frame_address(1), (unsigned long)__builtin_return_address(0)); else - start_backtrace(&frame, thread_saved_fp(task), + unwind_init(&frame, thread_saved_fp(task), thread_saved_pc(task)); - walk_stackframe(task, &frame, consume_entry, cookie); + unwind(task, &frame, consume_entry, cookie); } From patchwork Mon Jan 3 16:52:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Madhavan T. Venkataraman" X-Patchwork-Id: 12702675 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A638DC433EF for ; Mon, 3 Jan 2022 16:54:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=T2CtE2k5seSXlbWgZIUaePZyQz55ZO4IzL2+lrOE+Qs=; b=jWNaJsiza8UKfG b8gMhFyHpOGxoSbQHTRUnfytroK5c6PO0VCJlb3oWrDLvDZCQKoMYvuy/gM+4q7e7t2OjWsURPxrd 9DAbglTmvHdZ90nl716u73OoXTbkEVrhDcjrEygoSLCiZATcNI3PRPwJGMUb55aTQV2OLzBGe5F2z XyFfLBj6XYbEfGbYIkQIaPf2eoLg9tu73bTRZoxtnHOFB5eLj2unod1g+XeaKYiuByACug2E8yE+P noqiuTY3KL99OWM3CdWtVF9z/1Z1z9sDwH1HMeO7CJebJks58beQ50WdW0MVh/3RtmA3jpqtDE1Wi HvJ44rfOj5pPjafvgpBg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1n4Qa9-009Z4D-M5; Mon, 03 Jan 2022 16:53:13 +0000 Received: from linux.microsoft.com ([13.77.154.182]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1n4QZk-009Yrq-5W for linux-arm-kernel@lists.infradead.org; Mon, 03 Jan 2022 16:52:50 +0000 Received: from x64host.home (unknown [47.187.212.181]) by linux.microsoft.com (Postfix) with ESMTPSA id CE73A20B7185; Mon, 3 Jan 2022 08:52:46 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com CE73A20B7185 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1641228767; bh=CypTM8/tYm9H4rq+eanWDFrMlvgKtxFpVbwHffdlOTw=; h=From:To:Subject:Date:In-Reply-To:References:From; b=cGXHjtTndquuMwttSOuTNfzk+aaI7X6j3AtZ6zGgXvg75D8LVeY3SoNXNAOAVTam9 VQxadth6hjIbmL4nTt7/iwVwLfjnG7ztsrPrNRtnX1q+1roCmm/e6Oq/4O8lL6jp9u /1q0KfcNpcMWTCMUQIFE3V4ve4CjOYxF9rxCIa0M= From: madvenka@linux.microsoft.com To: mark.rutland@arm.com, broonie@kernel.org, jpoimboe@redhat.com, ardb@kernel.org, nobuta.keiya@fujitsu.com, sjitindarsingh@gmail.com, catalin.marinas@arm.com, will@kernel.org, jmorris@namei.org, linux-arm-kernel@lists.infradead.org, live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, madvenka@linux.microsoft.com Subject: [PATCH v12 03/10] arm64: Rename stackframe to unwind_state Date: Mon, 3 Jan 2022 10:52:05 -0600 Message-Id: <20220103165212.9303-4-madvenka@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220103165212.9303-1-madvenka@linux.microsoft.com> References: <0d0eb36f348fb5a6af6eb592c0525f6e94007328> <20220103165212.9303-1-madvenka@linux.microsoft.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220103_085248_298416_31FFD413 X-CRM114-Status: GOOD ( 18.17 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: "Madhavan T. Venkataraman" Rename "struct stackframe" to "struct unwind_state" for consistency and better naming. Accordingly, rename variable/argument "frame" to "state". Signed-off-by: Madhavan T. Venkataraman Reviewed-by: Mark Brown Reviewed-by: Mark Rutland --- arch/arm64/include/asm/stacktrace.h | 2 +- arch/arm64/kernel/stacktrace.c | 66 ++++++++++++++--------------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/arch/arm64/include/asm/stacktrace.h b/arch/arm64/include/asm/stacktrace.h index 3a15d376ab36..fc828c3c5dfd 100644 --- a/arch/arm64/include/asm/stacktrace.h +++ b/arch/arm64/include/asm/stacktrace.h @@ -52,7 +52,7 @@ struct stack_info { * associated with the most recently encountered replacement lr * value. */ -struct stackframe { +struct unwind_state { unsigned long fp; unsigned long pc; DECLARE_BITMAP(stacks_done, __NR_STACK_TYPES); diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index b980d96dccfc..a1a7ff93b84f 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -33,13 +33,13 @@ */ -static void unwind_init(struct stackframe *frame, unsigned long fp, +static void unwind_init(struct unwind_state *state, unsigned long fp, unsigned long pc) { - frame->fp = fp; - frame->pc = pc; + state->fp = fp; + state->pc = pc; #ifdef CONFIG_KRETPROBES - frame->kr_cur = NULL; + state->kr_cur = NULL; #endif /* @@ -51,9 +51,9 @@ static void unwind_init(struct stackframe *frame, unsigned long fp, * prev_fp value won't be used, but we set it to 0 such that it is * definitely not an accessible stack address. */ - bitmap_zero(frame->stacks_done, __NR_STACK_TYPES); - frame->prev_fp = 0; - frame->prev_type = STACK_TYPE_UNKNOWN; + bitmap_zero(state->stacks_done, __NR_STACK_TYPES); + state->prev_fp = 0; + state->prev_type = STACK_TYPE_UNKNOWN; } /* @@ -64,9 +64,9 @@ static void unwind_init(struct stackframe *frame, unsigned long fp, * and the location (but not the fp value) of B. */ static int notrace unwind_next(struct task_struct *tsk, - struct stackframe *frame) + struct unwind_state *state) { - unsigned long fp = frame->fp; + unsigned long fp = state->fp; struct stack_info info; /* Final frame; nothing to unwind */ @@ -79,7 +79,7 @@ static int notrace unwind_next(struct task_struct *tsk, if (!on_accessible_stack(tsk, fp, 16, &info)) return -EINVAL; - if (test_bit(info.type, frame->stacks_done)) + if (test_bit(info.type, state->stacks_done)) return -EINVAL; /* @@ -95,27 +95,27 @@ static int notrace unwind_next(struct task_struct *tsk, * stack to another, it's never valid to unwind back to that first * stack. */ - if (info.type == frame->prev_type) { - if (fp <= frame->prev_fp) + if (info.type == state->prev_type) { + if (fp <= state->prev_fp) return -EINVAL; } else { - set_bit(frame->prev_type, frame->stacks_done); + set_bit(state->prev_type, state->stacks_done); } /* * Record this frame record's values and location. The prev_fp and * prev_type are only meaningful to the next unwind_next() invocation. */ - frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp)); - frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp + 8)); - frame->prev_fp = fp; - frame->prev_type = info.type; + state->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp)); + state->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp + 8)); + state->prev_fp = fp; + state->prev_type = info.type; - frame->pc = ptrauth_strip_insn_pac(frame->pc); + state->pc = ptrauth_strip_insn_pac(state->pc); #ifdef CONFIG_FUNCTION_GRAPH_TRACER if (tsk->ret_stack && - (frame->pc == (unsigned long)return_to_handler)) { + (state->pc == (unsigned long)return_to_handler)) { unsigned long orig_pc; /* * This is a case where function graph tracer has @@ -123,16 +123,16 @@ static int notrace unwind_next(struct task_struct *tsk, * to hook a function return. * So replace it to an original value. */ - orig_pc = ftrace_graph_ret_addr(tsk, NULL, frame->pc, - (void *)frame->fp); - if (WARN_ON_ONCE(frame->pc == orig_pc)) + orig_pc = ftrace_graph_ret_addr(tsk, NULL, state->pc, + (void *)state->fp); + if (WARN_ON_ONCE(state->pc == orig_pc)) return -EINVAL; - frame->pc = orig_pc; + state->pc = orig_pc; } #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ #ifdef CONFIG_KRETPROBES - if (is_kretprobe_trampoline(frame->pc)) - frame->pc = kretprobe_find_ret_addr(tsk, (void *)frame->fp, &frame->kr_cur); + if (is_kretprobe_trampoline(state->pc)) + state->pc = kretprobe_find_ret_addr(tsk, (void *)state->fp, &state->kr_cur); #endif return 0; @@ -140,15 +140,15 @@ static int notrace unwind_next(struct task_struct *tsk, NOKPROBE_SYMBOL(unwind_next); static void notrace unwind(struct task_struct *tsk, - struct stackframe *frame, + struct unwind_state *state, bool (*fn)(void *, unsigned long), void *data) { while (1) { int ret; - if (!fn(data, frame->pc)) + if (!fn(data, state->pc)) break; - ret = unwind_next(tsk, frame); + ret = unwind_next(tsk, state); if (ret < 0) break; } @@ -192,17 +192,17 @@ noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie, struct task_struct *task, struct pt_regs *regs) { - struct stackframe frame; + struct unwind_state state; if (regs) - unwind_init(&frame, regs->regs[29], regs->pc); + unwind_init(&state, regs->regs[29], regs->pc); else if (task == current) - unwind_init(&frame, + unwind_init(&state, (unsigned long)__builtin_frame_address(1), (unsigned long)__builtin_return_address(0)); else - unwind_init(&frame, thread_saved_fp(task), + unwind_init(&state, thread_saved_fp(task), thread_saved_pc(task)); - unwind(task, &frame, consume_entry, cookie); + unwind(task, &state, consume_entry, cookie); } From patchwork Mon Jan 3 16:52:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Madhavan T. Venkataraman" X-Patchwork-Id: 12702677 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5FE85C433EF for ; Mon, 3 Jan 2022 16:54:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=I6OWzXp6HiBJS4wraVPxPayctcBn4dS5odCueynAAfM=; b=vHllGC97QAwuFy SUcYcfsDtxDMlXg41uHmhsfPREtWPJCcTh4BL/MX338hzK2ULM0elXAvVcGwjUGq8iMZT7QNOZNXs q1OxqGs3yNjZdOiKX0dFexLHXDiZK+ydlBcSnl122T6/vjpKke2FASOhHUdmWb9lzcW4cZ9W5vHCZ 7MRyPJZh22PIBe69aEZLN+V+X14nmC2K8NLb/ZFu7asPDDMRJ8JFgq7OT4ZKC7XFQxaTCdrG1pp83 h6UJxpNKeTRs1UxoBDyei/3UKuH4/BV3UdUA2Xq7QvT/mF3Gq3qSnogy2ZqLn9guWmxnWjO11eQ6T Wirox6x02+o/YTV0oj1Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1n4QaW-009ZBF-PX; Mon, 03 Jan 2022 16:53:37 +0000 Received: from linux.microsoft.com ([13.77.154.182]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1n4QZl-009YsT-4X for linux-arm-kernel@lists.infradead.org; Mon, 03 Jan 2022 16:52:51 +0000 Received: from x64host.home (unknown [47.187.212.181]) by linux.microsoft.com (Postfix) with ESMTPSA id CE74C20B7187; Mon, 3 Jan 2022 08:52:47 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com CE74C20B7187 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1641228768; bh=3lzVOaR35pHb/fHnIwSFLrwaATuOq5V8yBUdf85S2So=; h=From:To:Subject:Date:In-Reply-To:References:From; b=p2npznnDJMamREe4Ij37w4jczzWls1u96tyFnLpJMrTubpsjQayaHgc0lh0THTno3 az7fWl0yNeN1d2bp3AjJWYlX0WifnePtntc5VIyOlwwf69ps/hDx81dNBPQX0cu/Ow ElqSITmCNGHAy0ufY3+hSnJnlXGG4TqAASmYHL+Y= From: madvenka@linux.microsoft.com To: mark.rutland@arm.com, broonie@kernel.org, jpoimboe@redhat.com, ardb@kernel.org, nobuta.keiya@fujitsu.com, sjitindarsingh@gmail.com, catalin.marinas@arm.com, will@kernel.org, jmorris@namei.org, linux-arm-kernel@lists.infradead.org, live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, madvenka@linux.microsoft.com Subject: [PATCH v12 04/10] arm64: Split unwind_init() Date: Mon, 3 Jan 2022 10:52:06 -0600 Message-Id: <20220103165212.9303-5-madvenka@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220103165212.9303-1-madvenka@linux.microsoft.com> References: <0d0eb36f348fb5a6af6eb592c0525f6e94007328> <20220103165212.9303-1-madvenka@linux.microsoft.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220103_085249_297926_208D7184 X-CRM114-Status: GOOD ( 13.43 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: "Madhavan T. Venkataraman" unwind_init() is currently a single function that initializes all of the unwind state. Split it into the following functions and call them appropriately: - unwind_init_regs() - initialize from regs passed by caller. - unwind_init_current() - initialize for the current task from the caller of arch_stack_walk(). - unwind_init_from_task() - initialize from the saved state of a task other than the current task. In this case, the other task must not be running. - unwind_init_common() - initialize fields that are common across the above 3 cases. This is done so that specialized initialization can be added to each case in the future. Signed-off-by: Madhavan T. Venkataraman --- arch/arm64/kernel/stacktrace.c | 50 +++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index a1a7ff93b84f..bd797e3f7789 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -33,11 +33,8 @@ */ -static void unwind_init(struct unwind_state *state, unsigned long fp, - unsigned long pc) +static void unwind_init_common(struct unwind_state *state) { - state->fp = fp; - state->pc = pc; #ifdef CONFIG_KRETPROBES state->kr_cur = NULL; #endif @@ -56,6 +53,40 @@ static void unwind_init(struct unwind_state *state, unsigned long fp, state->prev_type = STACK_TYPE_UNKNOWN; } +/* + * TODO: document requirements here. + */ +static inline void unwind_init_regs(struct unwind_state *state, + struct pt_regs *regs) +{ + state->fp = regs->regs[29]; + state->pc = regs->pc; +} + +/* + * TODO: document requirements here. + * + * Note: this is always inlined, and we expect our caller to be a noinline + * function, such that this starts from our caller's caller. + */ +static __always_inline void unwind_init_current(struct unwind_state *state) +{ + state->fp = (unsigned long)__builtin_frame_address(1); + state->pc = (unsigned long)__builtin_return_address(0); +} + +/* + * TODO: document requirements here. + * + * The caller guarantees that the task is not running. + */ +static inline void unwind_init_task(struct unwind_state *state, + struct task_struct *task) +{ + state->fp = thread_saved_fp(task); + state->pc = thread_saved_pc(task); +} + /* * Unwind from one frame record (A) to the next frame record (B). * @@ -194,15 +225,14 @@ noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, { struct unwind_state state; + unwind_init_common(&state); + if (regs) - unwind_init(&state, regs->regs[29], regs->pc); + unwind_init_regs(&state, regs); else if (task == current) - unwind_init(&state, - (unsigned long)__builtin_frame_address(1), - (unsigned long)__builtin_return_address(0)); + unwind_init_current(&state); else - unwind_init(&state, thread_saved_fp(task), - thread_saved_pc(task)); + unwind_init_task(&state, task); unwind(task, &state, consume_entry, cookie); } From patchwork Mon Jan 3 16:52:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Madhavan T. Venkataraman" X-Patchwork-Id: 12702678 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5F09EC433EF for ; Mon, 3 Jan 2022 16:55:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=flkM6z+uGo/Y403Ee59eAwkUr0Ql73CdcZfDGD82M54=; b=ITbjSScukDwwI4 wcMrTVF8U3TRqir9Lvc7DZ9Fql26U2bIv/l1d5K5uRV8dEO0Z4cbt1M+ZeskyE7gvIld121o0oTiE vKbkz8bJN3dFe10EI50tMGA4wdhMgB/Ux5gexmv0JVHfR+2n8vUNc5jFMgtHyoF4yxpfHOX2j4yh6 QrrAaAalBB0ys8URXZyE5GetOroPhAwYXD8iZNH6WHh/JdsuWhtxBU4lCa+9norQm3fZ0oyYTIArc 4tii3wOgiCi9yr5WrgKiIOPVL3hfZtWVe7h0tikvAjHNkQJM2ObZbI2EdVqwDYOv9Jvd0ieewQ6cd zFpyaEjQAQ7nsQOjwFVg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1n4Qap-009ZJI-6k; Mon, 03 Jan 2022 16:53:56 +0000 Received: from linux.microsoft.com ([13.77.154.182]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1n4QZl-009Ysm-SG for linux-arm-kernel@lists.infradead.org; Mon, 03 Jan 2022 16:52:52 +0000 Received: from x64host.home (unknown [47.187.212.181]) by linux.microsoft.com (Postfix) with ESMTPSA id CE70320B7186; Mon, 3 Jan 2022 08:52:48 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com CE70320B7186 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1641228769; bh=bx33Z9TGjAIotITO/hUtroGQmWsHZP2ZOAiT/00Ga/w=; h=From:To:Subject:Date:In-Reply-To:References:From; b=myo2GkUADBptsn+WGGTBtrYijOeaPCgwn08mvcDN7sGkFyWbB+LVLp1V85b8BuFDd XrX+IRxOxUZIehAfywNZ6GT70k7utPbWlO8SIcALKf+UXHFbZjNdrXQ6S5+AwtXf+I O/hEQmmnf8J3GckFsq6MYIQStWSeytes5P+q36H8= From: madvenka@linux.microsoft.com To: mark.rutland@arm.com, broonie@kernel.org, jpoimboe@redhat.com, ardb@kernel.org, nobuta.keiya@fujitsu.com, sjitindarsingh@gmail.com, catalin.marinas@arm.com, will@kernel.org, jmorris@namei.org, linux-arm-kernel@lists.infradead.org, live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, madvenka@linux.microsoft.com Subject: [PATCH v12 05/10] arm64: Copy unwind arguments to unwind_state Date: Mon, 3 Jan 2022 10:52:07 -0600 Message-Id: <20220103165212.9303-6-madvenka@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220103165212.9303-1-madvenka@linux.microsoft.com> References: <0d0eb36f348fb5a6af6eb592c0525f6e94007328> <20220103165212.9303-1-madvenka@linux.microsoft.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220103_085250_022499_DDFCD65A X-CRM114-Status: GOOD ( 14.75 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: "Madhavan T. Venkataraman" Copy the following arguments passed to arch_stack_walk() to unwind_state so that they can be passed to unwind functions via unwind_state rather than as separate arguments: - task - regs - consume_entry - cookie Signed-off-by: Madhavan T. Venkataraman --- arch/arm64/include/asm/stacktrace.h | 12 ++++++++ arch/arm64/kernel/stacktrace.c | 45 ++++++++++++++++------------- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/arch/arm64/include/asm/stacktrace.h b/arch/arm64/include/asm/stacktrace.h index fc828c3c5dfd..322817d40a75 100644 --- a/arch/arm64/include/asm/stacktrace.h +++ b/arch/arm64/include/asm/stacktrace.h @@ -51,6 +51,14 @@ struct stack_info { * @kr_cur: When KRETPOLINES is selected, holds the kretprobe instance * associated with the most recently encountered replacement lr * value. + * + * @task: Pointer to the task structure. + * + * @regs: Registers, if any. + * + * @consume_pc Consume PC function pointer. + * + * @cookie Argument to consume_pc(). */ struct unwind_state { unsigned long fp; @@ -61,6 +69,10 @@ struct unwind_state { #ifdef CONFIG_KRETPROBES struct llist_node *kr_cur; #endif + struct task_struct *task; + struct pt_regs *regs; + stack_trace_consume_fn consume_pc; + void *cookie; }; extern void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk, diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index bd797e3f7789..3ecb8242caa5 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -33,8 +33,17 @@ */ -static void unwind_init_common(struct unwind_state *state) +static void unwind_init_common(struct unwind_state *state, + struct task_struct *task, + struct pt_regs *regs, + stack_trace_consume_fn consume_pc, + void *cookie) { + state->task = task; + state->regs = regs; + state->consume_pc = consume_pc; + state->cookie = cookie; + #ifdef CONFIG_KRETPROBES state->kr_cur = NULL; #endif @@ -56,11 +65,10 @@ static void unwind_init_common(struct unwind_state *state) /* * TODO: document requirements here. */ -static inline void unwind_init_regs(struct unwind_state *state, - struct pt_regs *regs) +static inline void unwind_init_regs(struct unwind_state *state) { - state->fp = regs->regs[29]; - state->pc = regs->pc; + state->fp = state->regs->regs[29]; + state->pc = state->regs->pc; } /* @@ -80,11 +88,10 @@ static __always_inline void unwind_init_current(struct unwind_state *state) * * The caller guarantees that the task is not running. */ -static inline void unwind_init_task(struct unwind_state *state, - struct task_struct *task) +static inline void unwind_init_task(struct unwind_state *state) { - state->fp = thread_saved_fp(task); - state->pc = thread_saved_pc(task); + state->fp = thread_saved_fp(state->task); + state->pc = thread_saved_pc(state->task); } /* @@ -94,9 +101,9 @@ static inline void unwind_init_task(struct unwind_state *state, * records (e.g. a cycle), determined based on the location and fp value of A * and the location (but not the fp value) of B. */ -static int notrace unwind_next(struct task_struct *tsk, - struct unwind_state *state) +static int notrace unwind_next(struct unwind_state *state) { + struct task_struct *tsk = state->task; unsigned long fp = state->fp; struct stack_info info; @@ -170,16 +177,14 @@ static int notrace unwind_next(struct task_struct *tsk, } NOKPROBE_SYMBOL(unwind_next); -static void notrace unwind(struct task_struct *tsk, - struct unwind_state *state, - bool (*fn)(void *, unsigned long), void *data) +static void notrace unwind(struct unwind_state *state) { while (1) { int ret; - if (!fn(data, state->pc)) + if (!state->consume_pc(state->cookie, state->pc)) break; - ret = unwind_next(tsk, state); + ret = unwind_next(state); if (ret < 0) break; } @@ -225,14 +230,14 @@ noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, { struct unwind_state state; - unwind_init_common(&state); + unwind_init_common(&state, task, regs, consume_entry, cookie); if (regs) - unwind_init_regs(&state, regs); + unwind_init_regs(&state); else if (task == current) unwind_init_current(&state); else - unwind_init_task(&state, task); + unwind_init_task(&state); - unwind(task, &state, consume_entry, cookie); + unwind(&state); } From patchwork Mon Jan 3 16:52:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Madhavan T. Venkataraman" X-Patchwork-Id: 12702679 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 120DBC433EF for ; Mon, 3 Jan 2022 16:55:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=bUcjvfZWeiICFRpbLzw4otDP9UBaGP4j5Jk7QvwjOD0=; b=Hvie5mukjYEqqZ 3SXRYoEmI+YowBL4xTBfvX0qb+xQaLFeHYtEVe5C3rWP91pLXO71WxYYm4Eso22rXoIiJUjiyAdno JAPjmbK5bF6PzaKFiob62ENz5QzZhvzL9O+DSTDPA53WWjWpJHHuohVqpCUIBei/D9A148GLydvVC If/ytSD6wbpWING2zfh4iNDMGgZpj65GIQCYP1QF4bAfzdavW1SiweJhN4baIudhG6eaqP4t8qzYv 4egEJJItx3idPb9ItqMKFM4D2v8UvS+lz/SO+wF4L/AVq78YjXYiN2fddWMckD9DKOLRAX+Qc+FWH KZmeuhxyfJAgZdDSU/8Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1n4QbJ-009ZZ0-LM; Mon, 03 Jan 2022 16:54:26 +0000 Received: from linux.microsoft.com ([13.77.154.182]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1n4QZm-009Ytd-SU for linux-arm-kernel@lists.infradead.org; Mon, 03 Jan 2022 16:52:52 +0000 Received: from x64host.home (unknown [47.187.212.181]) by linux.microsoft.com (Postfix) with ESMTPSA id CD83920B7189; Mon, 3 Jan 2022 08:52:49 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com CD83920B7189 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1641228770; bh=ZYsAIIQWNkzUjPZHyFb4uiuraQ+DJVDMfx0RavVNtlw=; h=From:To:Subject:Date:In-Reply-To:References:From; b=rLG4paSwHHt9dfLGWUHF9y/6vMCw61ZwGhiCBpXWIvlVm16p6geI9IslDjRQW6t7q zwCfQJXPLRUim9WZ27Eel7yv8Av5yV8j14DbWKyRDX9QPpoalUweWlTVGjeGLuUSV9 /xEI9H7+DG9/3nWQ3q+G/Fid0IEwigqtQTvJOAaA= From: madvenka@linux.microsoft.com To: mark.rutland@arm.com, broonie@kernel.org, jpoimboe@redhat.com, ardb@kernel.org, nobuta.keiya@fujitsu.com, sjitindarsingh@gmail.com, catalin.marinas@arm.com, will@kernel.org, jmorris@namei.org, linux-arm-kernel@lists.infradead.org, live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, madvenka@linux.microsoft.com Subject: [PATCH v12 06/10] arm64: Make the unwind loop in unwind() similar to other architectures Date: Mon, 3 Jan 2022 10:52:08 -0600 Message-Id: <20220103165212.9303-7-madvenka@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220103165212.9303-1-madvenka@linux.microsoft.com> References: <0d0eb36f348fb5a6af6eb592c0525f6e94007328> <20220103165212.9303-1-madvenka@linux.microsoft.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220103_085251_032596_B8D13F04 X-CRM114-Status: GOOD ( 19.95 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: "Madhavan T. Venkataraman" Change the loop in unwind() =========================== Change the unwind loop in unwind() to: while (unwind_continue(state)) unwind_next(state); This is easy to understand and maintain. New function unwind_continue() ============================== Define a new function unwind_continue() that is used in the unwind loop to check for conditions that terminate a stack trace. The conditions checked are: - If the bottom of the stack (final frame) has been reached, terminate. - If the consume_entry() function returns false, the caller of unwind has asked to terminate the stack trace. So, terminate. - If unwind_next() failed for some reason (like stack corruption), terminate. Do not return an error value from unwind_next() =============================================== We want to check for terminating conditions only in unwind_continue() from the unwinder loop. So, do not return an error value from unwind_next(). Simply set a flag in unwind_state and check the flag in unwind_continue(). Final FP ======== Introduce a new field "final_fp" in "struct unwind_state". Initialize this to the final frame of the stack trace: task_pt_regs(task)->stackframe This is where the stacktrace must terminate if it is successful. Add an explicit comment to that effect. Signed-off-by: Madhavan T. Venkataraman Reviewed-by: Mark Brown --- arch/arm64/include/asm/stacktrace.h | 6 +++ arch/arm64/kernel/stacktrace.c | 70 ++++++++++++++++++----------- 2 files changed, 50 insertions(+), 26 deletions(-) diff --git a/arch/arm64/include/asm/stacktrace.h b/arch/arm64/include/asm/stacktrace.h index 322817d40a75..9d1fddc26586 100644 --- a/arch/arm64/include/asm/stacktrace.h +++ b/arch/arm64/include/asm/stacktrace.h @@ -59,6 +59,10 @@ struct stack_info { * @consume_pc Consume PC function pointer. * * @cookie Argument to consume_pc(). + * + * @final_fp Pointer to the final frame. + * + * @failed: Unwind failed. */ struct unwind_state { unsigned long fp; @@ -73,6 +77,8 @@ struct unwind_state { struct pt_regs *regs; stack_trace_consume_fn consume_pc; void *cookie; + unsigned long final_fp; + bool failed; }; extern void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk, diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index 3ecb8242caa5..af0949f028c9 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -60,6 +60,10 @@ static void unwind_init_common(struct unwind_state *state, bitmap_zero(state->stacks_done, __NR_STACK_TYPES); state->prev_fp = 0; state->prev_type = STACK_TYPE_UNKNOWN; + state->failed = false; + + /* Stack trace terminates here. */ + state->final_fp = (unsigned long)task_pt_regs(task)->stackframe; } /* @@ -94,6 +98,23 @@ static inline void unwind_init_task(struct unwind_state *state) state->pc = thread_saved_pc(state->task); } +static bool notrace unwind_continue(struct unwind_state *state) +{ + if (state->failed) { + /* PC is suspect. Cannot consume it. */ + return false; + } + + if (!state->consume_pc(state->cookie, state->pc)) { + /* Caller terminated the unwind. */ + state->failed = true; + return false; + } + + return state->fp != state->final_fp; +} +NOKPROBE_SYMBOL(unwind_continue); + /* * Unwind from one frame record (A) to the next frame record (B). * @@ -101,24 +122,26 @@ static inline void unwind_init_task(struct unwind_state *state) * records (e.g. a cycle), determined based on the location and fp value of A * and the location (but not the fp value) of B. */ -static int notrace unwind_next(struct unwind_state *state) +static void notrace unwind_next(struct unwind_state *state) { struct task_struct *tsk = state->task; unsigned long fp = state->fp; struct stack_info info; - /* Final frame; nothing to unwind */ - if (fp == (unsigned long)task_pt_regs(tsk)->stackframe) - return -ENOENT; - - if (fp & 0x7) - return -EINVAL; + if (fp & 0x7) { + state->failed = true; + return; + } - if (!on_accessible_stack(tsk, fp, 16, &info)) - return -EINVAL; + if (!on_accessible_stack(tsk, fp, 16, &info)) { + state->failed = true; + return; + } - if (test_bit(info.type, state->stacks_done)) - return -EINVAL; + if (test_bit(info.type, state->stacks_done)) { + state->failed = true; + return; + } /* * As stacks grow downward, any valid record on the same stack must be @@ -134,8 +157,10 @@ static int notrace unwind_next(struct unwind_state *state) * stack. */ if (info.type == state->prev_type) { - if (fp <= state->prev_fp) - return -EINVAL; + if (fp <= state->prev_fp) { + state->failed = true; + return; + } } else { set_bit(state->prev_type, state->stacks_done); } @@ -163,8 +188,10 @@ static int notrace unwind_next(struct unwind_state *state) */ orig_pc = ftrace_graph_ret_addr(tsk, NULL, state->pc, (void *)state->fp); - if (WARN_ON_ONCE(state->pc == orig_pc)) - return -EINVAL; + if (WARN_ON_ONCE(state->pc == orig_pc)) { + state->failed = true; + return; + } state->pc = orig_pc; } #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ @@ -172,22 +199,13 @@ static int notrace unwind_next(struct unwind_state *state) if (is_kretprobe_trampoline(state->pc)) state->pc = kretprobe_find_ret_addr(tsk, (void *)state->fp, &state->kr_cur); #endif - - return 0; } NOKPROBE_SYMBOL(unwind_next); static void notrace unwind(struct unwind_state *state) { - while (1) { - int ret; - - if (!state->consume_pc(state->cookie, state->pc)) - break; - ret = unwind_next(state); - if (ret < 0) - break; - } + while (unwind_continue(state)) + unwind_next(state); } NOKPROBE_SYMBOL(unwind); From patchwork Mon Jan 3 16:52:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Madhavan T. Venkataraman" X-Patchwork-Id: 12702680 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 248F0C433F5 for ; Mon, 3 Jan 2022 16:56:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=SA1epGU59b6aiNSPL8a1AU3Fw8VzASFKB9teo2qmJKw=; b=IGO0ZF4Y6QOTME SaxoNZK19qnVc4GmFkM+atUIHfEuRZhebE7EknLnbNEr+Cy77TMxWgQ24EJxFaXZchxuuDXK4O3fY sd63TXV71ZtYMxzkHvp5174AcXdZwfcosHHyRZIqoJnl1cD3R9jsucoBz7387UHTDEL7A73wR+3ri n6noxQVkIZjkx48jc+doJRx+fG9wV392uKEL4wh9LJWds0em8NOJv+CNO1bs/75FLC0YCS1A/jvEM b9Dk8BqYHhlM4nwKiEq78aZ0pAZHd6ybflovLwbwPEPFRVBoCLh69MLd9tLAPLwUQSTYEk81WB/qh 6MmnGYcc2XBntD2QQgtg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1n4Qbt-009ZtA-1K; Mon, 03 Jan 2022 16:55:01 +0000 Received: from linux.microsoft.com ([13.77.154.182]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1n4QZn-009YsT-PM for linux-arm-kernel@lists.infradead.org; Mon, 03 Jan 2022 16:52:53 +0000 Received: from x64host.home (unknown [47.187.212.181]) by linux.microsoft.com (Postfix) with ESMTPSA id CD79E20B718A; Mon, 3 Jan 2022 08:52:50 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com CD79E20B718A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1641228771; bh=k0L/6HF0XSKKkYTWUToL3wE6EoMVilDa95cDIedw94E=; h=From:To:Subject:Date:In-Reply-To:References:From; b=pY92LoKgoXa2SoD4BwTeElCgeiSgs8onNLmiX4+ShFKPz7yM2gvTEcAEqQ0l2vUev R+ApgO3xM78oO3lKMxJf3x7As3Cme4PjSaXoz5DMDWmbap0kwZg3hQWfBJvp1X65mK qRB+Vtpt6MuRRyGQKed9jEI9QsDDXsxhZuaDz1B4= From: madvenka@linux.microsoft.com To: mark.rutland@arm.com, broonie@kernel.org, jpoimboe@redhat.com, ardb@kernel.org, nobuta.keiya@fujitsu.com, sjitindarsingh@gmail.com, catalin.marinas@arm.com, will@kernel.org, jmorris@namei.org, linux-arm-kernel@lists.infradead.org, live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, madvenka@linux.microsoft.com Subject: [PATCH v12 07/10] arm64: Introduce stack trace reliability checks in the unwinder Date: Mon, 3 Jan 2022 10:52:09 -0600 Message-Id: <20220103165212.9303-8-madvenka@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220103165212.9303-1-madvenka@linux.microsoft.com> References: <0d0eb36f348fb5a6af6eb592c0525f6e94007328> <20220103165212.9303-1-madvenka@linux.microsoft.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220103_085251_916208_46FEEEC5 X-CRM114-Status: GOOD ( 18.22 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: "Madhavan T. Venkataraman" There are some kernel features and conditions that make a stack trace unreliable. Callers may require the unwinder to detect these cases. E.g., livepatch. Introduce a new function called unwind_check_reliability() that will detect these cases and set a flag in the stack frame. Call unwind_check_reliability() for every frame in unwind(). Introduce the first reliability check in unwind_check_reliability() - If a return PC is not a valid kernel text address, consider the stack trace unreliable. It could be some generated code. Other reliability checks will be added in the future. Let unwind() return a boolean to indicate if the stack trace is reliable. Signed-off-by: Madhavan T. Venkataraman --- arch/arm64/include/asm/stacktrace.h | 3 +++ arch/arm64/kernel/stacktrace.c | 29 +++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/arch/arm64/include/asm/stacktrace.h b/arch/arm64/include/asm/stacktrace.h index 9d1fddc26586..47d4be69799a 100644 --- a/arch/arm64/include/asm/stacktrace.h +++ b/arch/arm64/include/asm/stacktrace.h @@ -63,6 +63,8 @@ struct stack_info { * @final_fp Pointer to the final frame. * * @failed: Unwind failed. + * + * @reliable: Stack trace is reliable. */ struct unwind_state { unsigned long fp; @@ -79,6 +81,7 @@ struct unwind_state { void *cookie; unsigned long final_fp; bool failed; + bool reliable; }; extern void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk, diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index af0949f028c9..54c3396a65c3 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -18,6 +18,25 @@ #include #include +/* + * Check the stack frame for conditions that make further unwinding unreliable. + */ +static void unwind_check_reliability(struct unwind_state *state) +{ + if (state->fp == state->final_fp) { + /* Final frame; no more unwind, no need to check reliability */ + return; + } + + /* + * If the PC is not a known kernel text address, then we cannot + * be sure that a subsequent unwind will be reliable, as we + * don't know that the code follows our unwind requirements. + */ + if (!__kernel_text_address(state->pc)) + state->reliable = false; +} + /* * AArch64 PCS assigns the frame pointer to x29. * @@ -64,6 +83,8 @@ static void unwind_init_common(struct unwind_state *state, /* Stack trace terminates here. */ state->final_fp = (unsigned long)task_pt_regs(task)->stackframe; + + state->reliable = true; } /* @@ -202,10 +223,14 @@ static void notrace unwind_next(struct unwind_state *state) } NOKPROBE_SYMBOL(unwind_next); -static void notrace unwind(struct unwind_state *state) +static bool notrace unwind(struct unwind_state *state) { - while (unwind_continue(state)) + unwind_check_reliability(state); + while (unwind_continue(state)) { unwind_next(state); + unwind_check_reliability(state); + } + return !state->failed && state->reliable; } NOKPROBE_SYMBOL(unwind); From patchwork Mon Jan 3 16:52:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Madhavan T. Venkataraman" X-Patchwork-Id: 12702681 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 411CBC433EF for ; Mon, 3 Jan 2022 16:57:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=zr9JyCQuGgduQ93SX8sf/eNIwYej/kyqm99NMS0BzO0=; b=OtnOJL3Nz2oW93 kD9gUTCu9jR4T2IQCjwTZZD8RRWJvUZNF5pneKVgUrOCBq7w5zWWBXNSyKsbfhMatem/mjEKnsWeK 1AJmzHFGhvQBkqbTIn8g4bgr92kVX6rr8KWFcHRQEMEOQT1/KwXt4mxy1y++5gj7N0CdhRqdCceQv ywMZM5IITEyILPhRUcFsddQLWHZySWcCsbaBJu3nqNGh5B5VqNvd9/dSpJ4wzvn8c0hlCBWZm1o/1 8mhEx2PQt6GGCh6Y/UhjFh9MgQv5S1M+otflSb0jIBWxkV5Yd/e6MEgxWHUbqoNSOnMBDZqs9jyiZ 8J69MAt/CTVRlzKxJZxg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1n4QcR-009aBD-Ds; Mon, 03 Jan 2022 16:55:36 +0000 Received: from linux.microsoft.com ([13.77.154.182]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1n4QZo-009Ysm-PZ for linux-arm-kernel@lists.infradead.org; Mon, 03 Jan 2022 16:52:54 +0000 Received: from x64host.home (unknown [47.187.212.181]) by linux.microsoft.com (Postfix) with ESMTPSA id CD84E20B718B; Mon, 3 Jan 2022 08:52:51 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com CD84E20B718B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1641228772; bh=b8ocIST8dkvkqzM1Zo0e6yg5+m3v/9B9ltdqLSOrdH8=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Nsa1lWVNHt8GFQ1gXq6KQPOdJm1eLLkI7FItpxjZ2zBO+vH/WqzDLQP6k8TKlUnqd 7rh7k3gDC3U8GIaWy05Iv3flz9b6nsZmUf7I/z79Xv0P2e4rzbS/F+WHJZx7RifxGG bikE8syh3RcFAM0rMVdXJT1vyltsJoPm1ZENiogw= From: madvenka@linux.microsoft.com To: mark.rutland@arm.com, broonie@kernel.org, jpoimboe@redhat.com, ardb@kernel.org, nobuta.keiya@fujitsu.com, sjitindarsingh@gmail.com, catalin.marinas@arm.com, will@kernel.org, jmorris@namei.org, linux-arm-kernel@lists.infradead.org, live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, madvenka@linux.microsoft.com Subject: [PATCH v12 08/10] arm64: Create a list of SYM_CODE functions, check return PC against list Date: Mon, 3 Jan 2022 10:52:10 -0600 Message-Id: <20220103165212.9303-9-madvenka@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220103165212.9303-1-madvenka@linux.microsoft.com> References: <0d0eb36f348fb5a6af6eb592c0525f6e94007328> <20220103165212.9303-1-madvenka@linux.microsoft.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220103_085252_922506_3455B7ED X-CRM114-Status: GOOD ( 20.38 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: "Madhavan T. Venkataraman" SYM_CODE functions don't follow the usual calling conventions. Check if the return PC in a stack frame falls in any of these. If it does, consider the stack trace unreliable. Define a special section for unreliable functions ================================================= Define a SYM_CODE_END() macro for arm64 that adds the function address range to a new section called "sym_code_functions". Linker file =========== Include the "sym_code_functions" section under read-only data in vmlinux.lds.S. Initialization ============== Define an early_initcall() to create a sym_code_functions[] array from the linker data. Unwinder check ============== Add a reliability check in unwind_check_reliability() that compares a return PC with sym_code_functions[]. If there is a match, then return failure. Signed-off-by: Madhavan T. Venkataraman Reviewed-by: Mark Brown --- arch/arm64/include/asm/linkage.h | 12 +++++++ arch/arm64/include/asm/sections.h | 1 + arch/arm64/kernel/stacktrace.c | 55 +++++++++++++++++++++++++++++++ arch/arm64/kernel/vmlinux.lds.S | 10 ++++++ 4 files changed, 78 insertions(+) diff --git a/arch/arm64/include/asm/linkage.h b/arch/arm64/include/asm/linkage.h index 9906541a6861..616bad74e297 100644 --- a/arch/arm64/include/asm/linkage.h +++ b/arch/arm64/include/asm/linkage.h @@ -68,4 +68,16 @@ SYM_FUNC_END_ALIAS(x); \ SYM_FUNC_END_ALIAS(__pi_##x) +/* + * Record the address range of each SYM_CODE function in a struct code_range + * in a special section. + */ +#define SYM_CODE_END(name) \ + SYM_END(name, SYM_T_NONE) ;\ + 99: ;\ + .pushsection "sym_code_functions", "aw" ;\ + .quad name ;\ + .quad 99b ;\ + .popsection + #endif diff --git a/arch/arm64/include/asm/sections.h b/arch/arm64/include/asm/sections.h index 152cb35bf9df..ac01189668c5 100644 --- a/arch/arm64/include/asm/sections.h +++ b/arch/arm64/include/asm/sections.h @@ -22,5 +22,6 @@ extern char __irqentry_text_start[], __irqentry_text_end[]; extern char __mmuoff_data_start[], __mmuoff_data_end[]; extern char __entry_tramp_text_start[], __entry_tramp_text_end[]; extern char __relocate_new_kernel_start[], __relocate_new_kernel_end[]; +extern char __sym_code_functions_start[], __sym_code_functions_end[]; #endif /* __ASM_SECTIONS_H */ diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index 54c3396a65c3..1db1ccb61241 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -18,11 +18,40 @@ #include #include +struct code_range { + unsigned long start; + unsigned long end; +}; + +static struct code_range *sym_code_functions; +static int num_sym_code_functions; + +int __init init_sym_code_functions(void) +{ + size_t size = (unsigned long)__sym_code_functions_end - + (unsigned long)__sym_code_functions_start; + + sym_code_functions = (struct code_range *)__sym_code_functions_start; + /* + * Order it so that sym_code_functions is not visible before + * num_sym_code_functions. + */ + smp_mb(); + num_sym_code_functions = size / sizeof(struct code_range); + + return 0; +} +early_initcall(init_sym_code_functions); + /* * Check the stack frame for conditions that make further unwinding unreliable. */ static void unwind_check_reliability(struct unwind_state *state) { + const struct code_range *range; + unsigned long pc; + int i; + if (state->fp == state->final_fp) { /* Final frame; no more unwind, no need to check reliability */ return; @@ -35,6 +64,32 @@ static void unwind_check_reliability(struct unwind_state *state) */ if (!__kernel_text_address(state->pc)) state->reliable = false; + + /* + * Check the return PC against sym_code_functions[]. If there is a + * match, then the consider the stack frame unreliable. + * + * As SYM_CODE functions don't follow the usual calling conventions, + * we assume by default that any SYM_CODE function cannot be unwound + * reliably. + * + * Note that this includes: + * + * - Exception handlers and entry assembly + * - Trampoline assembly (e.g., ftrace, kprobes) + * - Hypervisor-related assembly + * - Hibernation-related assembly + * - CPU start-stop, suspend-resume assembly + * - Kernel relocation assembly + */ + pc = state->pc; + for (i = 0; i < num_sym_code_functions; i++) { + range = &sym_code_functions[i]; + if (pc >= range->start && pc < range->end) { + state->reliable = false; + return; + } + } } /* diff --git a/arch/arm64/kernel/vmlinux.lds.S b/arch/arm64/kernel/vmlinux.lds.S index 50bab186c49b..6381e75e566e 100644 --- a/arch/arm64/kernel/vmlinux.lds.S +++ b/arch/arm64/kernel/vmlinux.lds.S @@ -122,6 +122,14 @@ jiffies = jiffies_64; #define TRAMP_TEXT #endif +#define SYM_CODE_FUNCTIONS \ + . = ALIGN(16); \ + .symcode : AT(ADDR(.symcode) - LOAD_OFFSET) { \ + __sym_code_functions_start = .; \ + KEEP(*(sym_code_functions)) \ + __sym_code_functions_end = .; \ + } + /* * The size of the PE/COFF section that covers the kernel image, which * runs from _stext to _edata, must be a round multiple of the PE/COFF @@ -209,6 +217,8 @@ SECTIONS swapper_pg_dir = .; . += PAGE_SIZE; + SYM_CODE_FUNCTIONS + . = ALIGN(SEGMENT_ALIGN); __init_begin = .; __inittext_begin = .; From patchwork Mon Jan 3 16:52:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Madhavan T. Venkataraman" X-Patchwork-Id: 12702685 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 46979C433EF for ; Mon, 3 Jan 2022 16:58:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=MTr5R8QFW+IWcxzXDL9547M6PYtoP0RlCVeyplzhUFM=; b=RBBYSxNLNCd9GY l1FSJEIJPlPD7pn4V0RuG48G6cffN5MxCrobv5R8PeOC6UldPus2cMr6UtMhsmguombHRW3/dtOky Q5q6GCdBBr3VQKTW5sZJ/oqwwDO2Nz5HFx6551KiGUwoCCGOKFKF4jVvsEDTzDuWRg+7NkBSDjHRm hIEKqbnQlzGsJu0Xyg6ep6AE+hFm0MrK9e5e03uiH8Pbpz/lJC6Y1ZCBRGGA+yXUtq7bHaNhCN6KV Oq60j8AvUZtRIWilTGkrSEp8/e5ld5A/OMCqm46Lzr+0dVpfbIOF4Y7gbxOpGDilgzD/SOnlI/e39 IJ+MpKpJdedPDe9J4TGA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1n4Qd6-009aUt-Q3; Mon, 03 Jan 2022 16:56:17 +0000 Received: from linux.microsoft.com ([13.77.154.182]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1n4QZp-009Ytd-PA for linux-arm-kernel@lists.infradead.org; Mon, 03 Jan 2022 16:52:55 +0000 Received: from x64host.home (unknown [47.187.212.181]) by linux.microsoft.com (Postfix) with ESMTPSA id CD80820B718C; Mon, 3 Jan 2022 08:52:52 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com CD80820B718C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1641228773; bh=Xl9WNOn2tIO3ZNBmKyfL3l0gpuWXAeRGb/t+fi8lT7g=; h=From:To:Subject:Date:In-Reply-To:References:From; b=DLLIH/0vgG5cY3yu8dxaLG84DI5X1L8kuUyceZqgHJ8WBRCQynD9wt8f1nvnpmNuf AXGUshRAiIAx6uEP3IYinO7Elp2/gKAfFOMNm52uy618CDHh3HaJL3kA/LPIM+lKhH U9OuORKn2LgyTs9Z5cWrV0z8tQRAnjhsfhV/cmhE= From: madvenka@linux.microsoft.com To: mark.rutland@arm.com, broonie@kernel.org, jpoimboe@redhat.com, ardb@kernel.org, nobuta.keiya@fujitsu.com, sjitindarsingh@gmail.com, catalin.marinas@arm.com, will@kernel.org, jmorris@namei.org, linux-arm-kernel@lists.infradead.org, live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, madvenka@linux.microsoft.com Subject: [PATCH v12 09/10] arm64: Introduce arch_stack_walk_reliable() Date: Mon, 3 Jan 2022 10:52:11 -0600 Message-Id: <20220103165212.9303-10-madvenka@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220103165212.9303-1-madvenka@linux.microsoft.com> References: <0d0eb36f348fb5a6af6eb592c0525f6e94007328> <20220103165212.9303-1-madvenka@linux.microsoft.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220103_085253_877996_AC324165 X-CRM114-Status: GOOD ( 10.80 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: "Madhavan T. Venkataraman" Introduce arch_stack_walk_reliable() for ARM64. This works like arch_stack_walk() except that it returns -EINVAL if the stack trace is not reliable. Until all the reliability checks are in place, arch_stack_walk_reliable() may not be used by livepatch. But it may be used by debug and test code. Signed-off-by: Madhavan T. Venkataraman Reviewed-by: Mark Brown --- arch/arm64/kernel/stacktrace.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index 1db1ccb61241..717d30833252 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -339,3 +339,27 @@ noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, unwind(&state); } + +/* + * arch_stack_walk_reliable() may not be used for livepatch until all of + * the reliability checks are in place in unwind_consume(). However, + * debug and test code can choose to use it even if all the checks are not + * in place. + */ +noinline int notrace arch_stack_walk_reliable(stack_trace_consume_fn consume_fn, + void *cookie, + struct task_struct *task) +{ + struct unwind_state state; + bool reliable; + + unwind_init_common(&state, task, NULL, consume_fn, cookie); + + if (task == current) + unwind_init_current(&state); + else + unwind_init_task(&state); + + reliable = unwind(&state); + return reliable ? 0 : -EINVAL; +} From patchwork Mon Jan 3 16:52:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Madhavan T. Venkataraman" X-Patchwork-Id: 12702686 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2D799C433EF for ; Mon, 3 Jan 2022 16:58:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=uGN239n2YGZHokvh/heprTpSwyEB7pkR/JZ+LMofdZU=; b=buixIpFAcYC1+4 kZeFbqq97zFxfdoSzo5w0Ei97HyEr+SXV+TK9tF2n/Bk4vOAmQ5oyL8jlbpvHynpl4cTxAyZkHCWe tL9+Dp83s/N2eZffUcqbdvyMtbqk5egCjqvox/dORUDF5tspH8sUZCqeuvOUOC2vpWG8OKLv92KsL GrVIXSya1g7xSOL4cfZedwbzkORVN3Cl96e5X+i3WPISZlYX8fs2AERN7hpFdxhM+SwnKBCnNpgdV 9z4rdl7BK4C+sNDMOwIikP2XWd9/nzMZ+jP1zt6oqY7ywtPmjIwOaRM0VRqf4d8MrXhIL4nLmOUUP 3R/xDwCX4tnhUhBwwhQA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1n4Qdn-009aoK-9l; Mon, 03 Jan 2022 16:56:59 +0000 Received: from linux.microsoft.com ([13.77.154.182]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1n4QZq-009YsT-RQ for linux-arm-kernel@lists.infradead.org; Mon, 03 Jan 2022 16:52:56 +0000 Received: from x64host.home (unknown [47.187.212.181]) by linux.microsoft.com (Postfix) with ESMTPSA id CD85420B718D; Mon, 3 Jan 2022 08:52:53 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com CD85420B718D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1641228774; bh=L7BINNZKLPpPqJ8YVvJ1Ceh6JZSHPJZT929hJRDYxLM=; h=From:To:Subject:Date:In-Reply-To:References:From; b=BgFvMnffEcMsB/tWPa67ElUGVIsgWCNjz/7jBojGCmyp8WVfd9ecskZUTib1C/D9q IdSIOQcqGxJyN9uN0YakvIL7Ho5+/cB6O+nYX7wKJAGN9jpyKN53eQZavQH8QZCDJO JQfKtpbSY0cCCJnMkI+AeRtHSi8omCIxkRskEaYI= From: madvenka@linux.microsoft.com To: mark.rutland@arm.com, broonie@kernel.org, jpoimboe@redhat.com, ardb@kernel.org, nobuta.keiya@fujitsu.com, sjitindarsingh@gmail.com, catalin.marinas@arm.com, will@kernel.org, jmorris@namei.org, linux-arm-kernel@lists.infradead.org, live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, madvenka@linux.microsoft.com Subject: [PATCH v12 10/10] arm64: Select HAVE_RELIABLE_STACKTRACE Date: Mon, 3 Jan 2022 10:52:12 -0600 Message-Id: <20220103165212.9303-11-madvenka@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220103165212.9303-1-madvenka@linux.microsoft.com> References: <0d0eb36f348fb5a6af6eb592c0525f6e94007328> <20220103165212.9303-1-madvenka@linux.microsoft.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220103_085254_941486_D8B61B14 X-CRM114-Status: UNSURE ( 7.36 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: "Madhavan T. Venkataraman" Select HAVE_RELIABLE_STACKTRACE in arm64/Kconfig to allow arch_stack_walk_reliable() to be used. Note that this is conditional upon STACK_VALIDATION which will be added when frame pointer validation is implemented (say via objtool). Signed-off-by: Madhavan T. Venkataraman --- arch/arm64/Kconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index c4207cf9bb17..4f6312784650 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -222,6 +222,7 @@ config ARM64 select THREAD_INFO_IN_TASK select HAVE_ARCH_USERFAULTFD_MINOR if USERFAULTFD select TRACE_IRQFLAGS_SUPPORT + select HAVE_RELIABLE_STACKTRACE if FRAME_POINTER && STACK_VALIDATION help ARM 64-bit (AArch64) Linux support.