Received: by 2002:a05:6500:1b41:b0:1fb:d597:ff75 with SMTP id cz1csp159326lqb; Tue, 4 Jun 2024 07:54:09 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWkQKm3niQVHE6LQNByYJw8AZcGCrAhvtIWLUm3sgvL7cIblB5UVKq+dd5BAN7d9dRJEF5mXeHvG3KkVnAbjLvO9TfABzjyazU6anMJGw== X-Google-Smtp-Source: AGHT+IGnXIOQXa6wCvNLwciZtkui2Zoi4Ad2RxTmVZS/PsEoTig2rY63QZnMAimIChzu4DlUG21N X-Received: by 2002:a17:902:fb0f:b0:1f6:3721:bca with SMTP id d9443c01a7336-1f637210d84mr108917795ad.15.1717512848854; Tue, 04 Jun 2024 07:54:08 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717512848; cv=pass; d=google.com; s=arc-20160816; b=Th3skuYBFMbZKbnY7yg5GLHmU8tMl5VduOe3dJ+fNaVoNsxdRghnWTu2QEY38aGmb0 FlH5PHWDzTM1zi+NI3ObQm0Px9VdSM3e88wGfDnhpCGLqadavDAD+BOFCZY/DKQtQ796 obk6Gsaaabz50A5+/Mf1S5FBg4o5V2ZlQZkx0uL8Iqb4dWFp5AB5eHS6WwJFyvhLMSUr F8Mf/vM3dKlusqWFIFZJUIoFBvZC/FF3SceDpId2nYKgo91/okOcCJaKeSB29rXorf62 siUyN0muDCj3vFtbqcosztP2ANY5mu2HqsPmHKhDH5/vdgdgviJ6rTmDTIvmCcvHrp+2 rKVw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:subject:cc:to:from:date:user-agent:message-id; bh=BhRKWVAOW9uvpm2rSbvx7ywqsl/xTJui3ZyB6QGNuUU=; fh=/wy5e/q+z2qlOMkv0QH/u80KV4ZXPtz68q6c1OyjqGY=; b=tI5d+ivGu5WMptLAZe3gqRud25xlwwtqoC1yJOKTHWP/f4X7OmD+ODReJDXYk+9Bzh 0rGIEnkn5whV/C6K0PtDqmRcDJe3rhKCE3OoLp5kYoJSeWwe8LeUNeF7ERYdfzmPUJdm 1aepEhQqoQC9FJ96EF+WWzoQ71lVtUYswSAQqvAmBeSnuN0EX5MA08M4k/6GRbwqiaXq OVgKSxk+VSQvYRtE7Dkuowz1U+dtWOvn9rY0lGPiM6uqKBgB4ORLC1UnnuYxG79e2ZNk J8V3DyIzNX4RU2r0+Fuxa1XYSiadcx8pA4yX7K6fVGRwlCuci4kE0NxofFO4ghpC0Mvc hzkg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-200868-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-200868-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id d9443c01a7336-1f662b063d6si51835775ad.650.2024.06.04.07.54.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Jun 2024 07:54:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-200868-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-200868-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-200868-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 238B8284943 for ; Tue, 4 Jun 2024 14:45:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C9AD4149C79; Tue, 4 Jun 2024 14:42:17 +0000 (UTC) Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7C524145A1F; Tue, 4 Jun 2024 14:42:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717512135; cv=none; b=dukI7NayS87twWgErfpBw51xmah/uRqnCbsIncDcyG0ZQyTE66DHVK1vJJyEtEjaivatcVabqwks2Yhg9r2oGEHx2YtYSp4fUz9vAx9ZNIFQaxH1dc9nRMcgk6GS8oayst6rX6h3kOWVmv79Jv+/nwOQRLQfKlFBOhALnGf6V50= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717512135; c=relaxed/simple; bh=c9vm0JziVDoqmP/XAmZSco8JE8aUdkPgChYrMgrLdC8=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=PUjT0U6I5fWGlpymGyuIpdSfZI0ANQ8tOGp5w6i3tgKhD6ou0vS6IOIN2KceFhiL0g7uVHag9akOIrwTEmQxYZEMcxV1yyWW+zsCE8xsWis2xUkAfkDnJII/ThgBohODQXr37er3ibv6/ebhYm5/QnwDyImzQa6Pf6wNx8VePG0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 02B18C32786; Tue, 4 Jun 2024 14:42:15 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1sEVMc-00000000Yvb-3oeR; Tue, 04 Jun 2024 10:42:14 -0400 Message-ID: <20240604144214.765821705@goodmis.org> User-Agent: quilt/0.68 Date: Tue, 04 Jun 2024 10:41:08 -0400 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Alexei Starovoitov , Florent Revest , Martin KaFai Lau , bpf , Sven Schnelle , Alexei Starovoitov , Jiri Olsa , Arnaldo Carvalho de Melo , Daniel Borkmann , Alan Maguire , Peter Zijlstra , Thomas Gleixner , Guo Ren Subject: [for-next][PATCH 05/27] function_graph: Handle tail calls for stack unwinding References: <20240604144103.293353991@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 From: "Masami Hiramatsu (Google)" For the tail-call, there would be 2 or more ftrace_ret_stacks on the ret_stack, which records "return_to_handler" as the return address except for the last one. But on the real stack, there should be 1 entry because tail-call reuses the return address on the stack and jump to the next function. In ftrace_graph_ret_addr() that is used for stack unwinding, skip tail calls as a real stack unwinder would do. Link: https://lore.kernel.org/linux-trace-kernel/171509096221.162236.8806372072523195752.stgit@devnote2 Link: https://lore.kernel.org/linux-trace-kernel/20240603190821.717065217@goodmis.org Cc: Mark Rutland Cc: Mathieu Desnoyers Cc: Andrew Morton Cc: Alexei Starovoitov Cc: Florent Revest Cc: Martin KaFai Lau Cc: bpf Cc: Sven Schnelle Cc: Alexei Starovoitov Cc: Jiri Olsa Cc: Arnaldo Carvalho de Melo Cc: Daniel Borkmann Cc: Alan Maguire Cc: Peter Zijlstra Cc: Thomas Gleixner Cc: Guo Ren Signed-off-by: Masami Hiramatsu (Google) Signed-off-by: Steven Rostedt (Google) --- kernel/trace/fgraph.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/kernel/trace/fgraph.c b/kernel/trace/fgraph.c index aae51f746828..8de2a2662281 100644 --- a/kernel/trace/fgraph.c +++ b/kernel/trace/fgraph.c @@ -594,16 +594,26 @@ unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx, unsigned long ret, unsigned long *retp) { struct ftrace_ret_stack *ret_stack; + unsigned long return_handler = (unsigned long)dereference_kernel_function_descriptor(return_to_handler); int i = task->curr_ret_stack; - if (ret != (unsigned long)dereference_kernel_function_descriptor(return_to_handler)) + if (ret != return_handler) return ret; while (i > 0) { ret_stack = get_ret_stack(current, i, &i); if (!ret_stack) break; - if (ret_stack->retp == retp) + /* + * For the tail-call, there would be 2 or more ftrace_ret_stacks on + * the ret_stack, which records "return_to_handler" as the return + * address except for the last one. + * But on the real stack, there should be 1 entry because tail-call + * reuses the return address on the stack and jump to the next function. + * Thus we will continue to find real return address. + */ + if (ret_stack->retp == retp && + ret_stack->ret != return_handler) return ret_stack->ret; } @@ -614,10 +624,11 @@ unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx, unsigned long ret, unsigned long *retp) { struct ftrace_ret_stack *ret_stack; + unsigned long return_handler = (unsigned long)dereference_kernel_function_descriptor(return_to_handler); int offset = task->curr_ret_stack; int i; - if (ret != (unsigned long)dereference_kernel_function_descriptor(return_to_handler)) + if (ret != return_handler) return ret; if (!idx) @@ -626,6 +637,8 @@ unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx, i = *idx; do { ret_stack = get_ret_stack(task, offset, &offset); + if (ret_stack && ret_stack->ret == return_handler) + continue; i--; } while (i >= 0 && ret_stack); -- 2.43.0