From patchwork Tue Aug 10 17:30:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg Kroah-Hartman X-Patchwork-Id: 494368 Delivered-To: patch@linaro.org Received: by 2002:a05:6638:396:0:0:0:0 with SMTP id y22csp4193679jap; Tue, 10 Aug 2021 11:00:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxcG5Vxa1e5scHeHxwZIOYIu2WsEfWbludeBD25dUDGgfFKwJMhYRK+i53VQbNF1fBd2JE3 X-Received: by 2002:a02:29c1:: with SMTP id p184mr17194210jap.32.1628618401896; Tue, 10 Aug 2021 11:00:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628618401; cv=none; d=google.com; s=arc-20160816; b=bGQzv/peg0Y3tDsT/XWroLW76vBcANyTPBj4IoduAb6ErWvKRiUTW32y//JDg6CJBZ kHAfUe+1qSTDolAE+hgxCSXc/q91/0Pi+dzDpKJkSd0+Wts/6Ad/NYt9Oh2n47cvtIcQ 7P3aCmm8zVJL/uxpS9h4pRtUfoO/Bc/joH3I6INiQ7OBfdWU1fFshB70KmWrg5rEAUPy IknuV4VlqviOtHI9GEoqesIS04uUrwVa3SVos2RdA8izl9eAl4lCkEvBpyHlMN25zlH4 k6QzviXRPphDCHLuzCjWdri+8oz5q2Qt22ocTbvvOfgnNSIjv/kPbjeCZSOBLyDmiCuW Tvhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=AGu7WLW0xljPK+r7uWD4v+eCS45AVYS1nFgLF9PXNE0=; b=gGeq0/5QPTgLHFgtKW8SREkGbbr2akENEiKNwbKWqfLq4K43JVycRNEj3vb8/vsdKh kZ+ckHmKabUlnVn6mYInU8aWpSjyVeF9UU+B9ACkns/Prd9294Yejx20sNapSHbbEvrz XZXPTA6uVCGAXcu3xnr8Wy0CMYiPGe5mAebielR1Uv4djH3SPcuoWxAXVh8CixGqd+Cd OQ0hA7JJbwiz1d3DbxjGHo6x9D6C1ZkzR15GobIg4iEbxNuCR/GLWLrOfHXA8ShVcIjN Reqw96a8ldN3ez33lLUgT81tEl7GMcASwBpmGfShpXTTvJuvfdL4O+kZ/dPkpKKKbXIZ UB5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="J/oYtnbN"; spf=pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d6si19403639ioe.34.2021.08.10.11.00.01; Tue, 10 Aug 2021 11:00:01 -0700 (PDT) Received-SPF: pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="J/oYtnbN"; spf=pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236703AbhHJSAI (ORCPT + 12 others); Tue, 10 Aug 2021 14:00:08 -0400 Received: from mail.kernel.org ([198.145.29.99]:58908 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233561AbhHJR6f (ORCPT ); Tue, 10 Aug 2021 13:58:35 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id C0303613A5; Tue, 10 Aug 2021 17:45:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1628617549; bh=qyHYpfO6S+3MYiwQ7LxkJNtfn/CQ6Me4JgV38nc+KkM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=J/oYtnbNDtc52biReV75Unkd49GWYCHdhCangH/Esb5+V49dkgY4W5w0/+U1EHuzb nVaLmC/l3LHBYyvtR74catDnfVtbP3Qg9K7nizrodpF5IQOdaR1C3TM/Z6FaedObfe BVvE3JQIwo7lACIMC7r2bv468alsMBi/sP1qRukI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Mark Rutland , Catalin Marinas , "Madhavan T. Venkataraman" , Mark Brown , Will Deacon Subject: [PATCH 5.13 108/175] arm64: stacktrace: avoid tracing arch_stack_walk() Date: Tue, 10 Aug 2021 19:30:16 +0200 Message-Id: <20210810173004.517693481@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210810173000.928681411@linuxfoundation.org> References: <20210810173000.928681411@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Mark Rutland commit 0c32706dac1b0a72713184246952ab0f54327c21 upstream. When the function_graph tracer is in use, arch_stack_walk() may unwind the stack incorrectly, erroneously reporting itself, missing the final entry which is being traced, and reporting all traced entries between these off-by-one from where they should be. When ftrace hooks a function return, the original return address is saved to the fgraph ret_stack, and the return address in the LR (or the function's frame record) is replaced with `return_to_handler`. When arm64's unwinder encounter frames returning to `return_to_handler`, it finds the associated original return address from the fgraph ret stack, assuming the most recent `ret_to_hander` entry on the stack corresponds to the most recent entry in the fgraph ret stack, and so on. When arch_stack_walk() is used to dump the current task's stack, it starts from the caller of arch_stack_walk(). However, arch_stack_walk() can be traced, and so may push an entry on to the fgraph ret stack, leaving the fgraph ret stack offset by one from the expected position. This can be seen when dumping the stack via /proc/self/stack, where enabling the graph tracer results in an unexpected `stack_trace_save_tsk` entry at the start of the trace, and `el0_svc` missing form the end of the trace. This patch fixes this by marking arch_stack_walk() as notrace, as we do for all other functions on the path to ftrace_graph_get_ret_stack(). While a few helper functions are not marked notrace, their calls/returns are balanced, and will have no observable effect when examining the fgraph ret stack. It is possible for an exeption boundary to cause a similar offset if the return address of the interrupted context was in the LR. Fixing those cases will require some more substantial rework, and is left for subsequent patches. Before: | # cat /proc/self/stack | [<0>] proc_pid_stack+0xc4/0x140 | [<0>] proc_single_show+0x6c/0x120 | [<0>] seq_read_iter+0x240/0x4e0 | [<0>] seq_read+0xe8/0x140 | [<0>] vfs_read+0xb8/0x1e4 | [<0>] ksys_read+0x74/0x100 | [<0>] __arm64_sys_read+0x28/0x3c | [<0>] invoke_syscall+0x50/0x120 | [<0>] el0_svc_common.constprop.0+0xc4/0xd4 | [<0>] do_el0_svc+0x30/0x9c | [<0>] el0_svc+0x2c/0x54 | [<0>] el0t_64_sync_handler+0x1a8/0x1b0 | [<0>] el0t_64_sync+0x198/0x19c | # echo function_graph > /sys/kernel/tracing/current_tracer | # cat /proc/self/stack | [<0>] stack_trace_save_tsk+0xa4/0x110 | [<0>] proc_pid_stack+0xc4/0x140 | [<0>] proc_single_show+0x6c/0x120 | [<0>] seq_read_iter+0x240/0x4e0 | [<0>] seq_read+0xe8/0x140 | [<0>] vfs_read+0xb8/0x1e4 | [<0>] ksys_read+0x74/0x100 | [<0>] __arm64_sys_read+0x28/0x3c | [<0>] invoke_syscall+0x50/0x120 | [<0>] el0_svc_common.constprop.0+0xc4/0xd4 | [<0>] do_el0_svc+0x30/0x9c | [<0>] el0t_64_sync_handler+0x1a8/0x1b0 | [<0>] el0t_64_sync+0x198/0x19c After: | # cat /proc/self/stack | [<0>] proc_pid_stack+0xc4/0x140 | [<0>] proc_single_show+0x6c/0x120 | [<0>] seq_read_iter+0x240/0x4e0 | [<0>] seq_read+0xe8/0x140 | [<0>] vfs_read+0xb8/0x1e4 | [<0>] ksys_read+0x74/0x100 | [<0>] __arm64_sys_read+0x28/0x3c | [<0>] invoke_syscall+0x50/0x120 | [<0>] el0_svc_common.constprop.0+0xc4/0xd4 | [<0>] do_el0_svc+0x30/0x9c | [<0>] el0_svc+0x2c/0x54 | [<0>] el0t_64_sync_handler+0x1a8/0x1b0 | [<0>] el0t_64_sync+0x198/0x19c | # echo function_graph > /sys/kernel/tracing/current_tracer | # cat /proc/self/stack | [<0>] proc_pid_stack+0xc4/0x140 | [<0>] proc_single_show+0x6c/0x120 | [<0>] seq_read_iter+0x240/0x4e0 | [<0>] seq_read+0xe8/0x140 | [<0>] vfs_read+0xb8/0x1e4 | [<0>] ksys_read+0x74/0x100 | [<0>] __arm64_sys_read+0x28/0x3c | [<0>] invoke_syscall+0x50/0x120 | [<0>] el0_svc_common.constprop.0+0xc4/0xd4 | [<0>] do_el0_svc+0x30/0x9c | [<0>] el0_svc+0x2c/0x54 | [<0>] el0t_64_sync_handler+0x1a8/0x1b0 | [<0>] el0t_64_sync+0x198/0x19c Cc: Signed-off-by: Mark Rutland Cc: Catalin Marinas Cc: Madhavan T. Venkataraman Cc: Mark Brown Cc: Will Deacon Reviwed-by: Mark Brown Link: https://lore.kernel.org/r/20210802164845.45506-3-mark.rutland@arm.com Signed-off-by: Will Deacon Signed-off-by: Greg Kroah-Hartman --- arch/arm64/kernel/stacktrace.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -220,7 +220,7 @@ void show_stack(struct task_struct *tsk, #ifdef CONFIG_STACKTRACE -noinline void arch_stack_walk(stack_trace_consume_fn consume_entry, +noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie, struct task_struct *task, struct pt_regs *regs) {