Received: by 2002:ab2:6c55:0:b0:1fd:c486:4f03 with SMTP id v21csp41332lqp; Tue, 11 Jun 2024 13:59:23 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWd74mg5HXanAjcQRjfnq2thACf3y3qskRhyVjOxoZReP/qz4HKsrJmn5c8olDXxaCX5t2ICnSB2THz45mWyLzIJQeAe1aKE/Gn/qBeOQ== X-Google-Smtp-Source: AGHT+IEX1rufYlv23+qBgb1GNlflhpAHfbWKshh4fULjkao55JhpJtfXnADLQe/A5swnFH5MF0xP X-Received: by 2002:a05:6e02:1385:b0:375:a3d8:97be with SMTP id e9e14a558f8ab-375a3d89aa6mr68857405ab.9.1718139563487; Tue, 11 Jun 2024 13:59:23 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718139563; cv=pass; d=google.com; s=arc-20160816; b=s4cRVTFjI94FS6xhRGG9QGmf31FAhOSi/fs5vFqoxgJuo6jMzvk5/cqSsQFbcoUI26 2w6okPdMiAfH9lXnJnTD8kiCFLpWxF2lrkBpvwwQxRS8WYoRYZ6u95H0wH//dK5yB9A3 U6xp1iowsBqXE9T8isQjXswBNrWUp839nWg6nFXrNrUnWN0oZ08EgCmBElVzBl2HgnIF mpCMcVuUqLaFQS31e/iAwcmcIGPIRvA9cpaFwxBdyzN1dX4z5lyC/p9xbkw203xK39t4 UiIuQc+WP9plQXS8s7wQkiR0A2qRczIFFOWmaUA0BukymNmGnV/N0M8apy/8b5u1PMKM wi3g== 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=V7SaRHxn+mfhvExrAHZdS2a3LQAtfr1KXLgl1sSKqvw=; fh=MjrxN4dwCX+029T59eRtc56QDjTvt2NG1IGQ8zAoWJY=; b=fmdYzHVhHDRUTS6iw0VC26rO/gVi9MncvnipNcOCJ9SNEDP/2TLOwn73O/RjzKV1+K XnlwbHf4SwyNmnLxCMcVXzKn6kE29yWVAn5uUS+FCbMLTrZxFCh3f9ZyHIoqFbetn7+F 4r7Mgz6iPvpKPwYmMc8MS2zKvUIhZkepUrCpMSmzYiUpTUzOHb5FGcaAYIBavevJWaYY WeV01YIjHHAihpDUbqa9b/pWtq1TZ0YzsfQUbDTSCA3IGmfIZmMYrsEZeEJOgl9f20mN sOdpZv/dhLVdTnptC6MP97EMas+h8UC5IQaIxA5Loyc4foidYfGwYVgUwTT+WKhXIICf /hjQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-210589-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-210589-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id 41be03b00d2f7-6f7c458079csi1365894a12.168.2024.06.11.13.59.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jun 2024 13:59:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-210589-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-210589-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-210589-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id EA977B21E1C for ; Tue, 11 Jun 2024 20:46:30 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2AFFE1534ED; Tue, 11 Jun 2024 20:46:13 +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 869C38488 for ; Tue, 11 Jun 2024 20:46:12 +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=1718138772; cv=none; b=F2VN4aW3kyf/k55n8hbBIk5me9/xmOtS/54XRoN7Wn2nwSonSak4E8HH5Rrd1I29ji6OnnKnW51LXlf0BTSvYsYzdfEYf94PpwM8zqId9CueMUEYKW7vCXYTNDWgdJ1JJgABgitq+TwExqStsHtdG0CoYJe9HbclLVQAK/JT2yI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718138772; c=relaxed/simple; bh=+6gP8t0w01bewNTXjkKuZ7tRG/S6J++weKA38nZj6JM=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=RqhYN6OKNbcBnnS+x3RKbiiYqV1FzU45K2kkKu+WxCNReS+O1tX2Ifvb0qkYCsu9jhehcTuze22QBT5Dk8MdkBvku6gjlBQh6ODwGdN4Z+OibFKz6d4INisqghBNZhKYnjS5L52DPyWwmgx4OuBoMIQviypURJ5q+CMIqQN00Cw= 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 3026FC4AF4D; Tue, 11 Jun 2024 20:46:12 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.97) (envelope-from ) id 1sH8Nw-00000001V39-3J5g; Tue, 11 Jun 2024 16:46:28 -0400 Message-ID: <20240611204628.651616294@goodmis.org> User-Agent: quilt/0.68 Date: Tue, 11 Jun 2024 16:45:56 -0400 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Jonathan Corbet , Catalin Marinas , Will Deacon , Guo Ren , Huacai Chen , WANG Xuerui , Michael Ellerman , Nicholas Piggin , Christophe Leroy , "Naveen N. Rao" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Yang Li Subject: [for-next][PATCH 2/4] function_graph: Fix up ftrace_graph_ret_addr() References: <20240611204554.092271761@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: "Steven Rostedt (Google)" Yang Li sent a patch to fix the kerneldoc of ftrace_graph_ret_addr(). While reviewing it, I realized that the comments in the entire function header needed a rewrite. When doing that, I realized that @idx parameter was being ignored. Every time this was called by the unwinder, it would start the loop at the top of the shadow stack and look for the matching stack pointer. When it found it, it would return it. When the unwinder asked for the next function, it would search from the beginning again. In reality, it should start from where it left off. That was the reason for the @idx parameter in the first place. The first time the unwinder calls this function, the @idx pointer would contain zero. That would mean to start from the top of the stack. The function was supposed to update the @idx with the index where it found the return address, so that the next time the unwinder calls this function it doesn't have to search through the previous addresses it found (making it O(n^2)!). This speeds up the unwinder's use of ftrace_graph_ret_addr() by an order of magnitude. Link: https://lore.kernel.org/linux-trace-kernel/20240610181746.656e3759@gandalf.local.home/ Link: https://lore.kernel.org/linux-trace-kernel/20240611031737.821995106@goodmis.org Cc: Masami Hiramatsu Cc: Mark Rutland Cc: Mathieu Desnoyers Cc: Jonathan Corbet Cc: Catalin Marinas Cc: Will Deacon Cc: Guo Ren Cc: Huacai Chen Cc: WANG Xuerui Cc: Michael Ellerman Cc: Nicholas Piggin Cc: Christophe Leroy Cc: "Naveen N. Rao" Cc: Paul Walmsley Cc: Palmer Dabbelt Cc: Albert Ou Cc: Heiko Carstens Cc: Vasily Gorbik Cc: Alexander Gordeev Cc: Christian Borntraeger Cc: Sven Schnelle Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: "H. Peter Anvin" Reported-by: Yang Li Fixes: 7aa1eaef9f428 ("function_graph: Allow multiple users to attach to function graph") Signed-off-by: Steven Rostedt (Google) --- kernel/trace/fgraph.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/kernel/trace/fgraph.c b/kernel/trace/fgraph.c index 63d0c2f84ce1..91f1eef256af 100644 --- a/kernel/trace/fgraph.c +++ b/kernel/trace/fgraph.c @@ -870,18 +870,24 @@ ftrace_graph_get_ret_stack(struct task_struct *task, int idx) } /** - * ftrace_graph_ret_addr - convert a potentially modified stack return address - * to its original value + * ftrace_graph_ret_addr - return the original value of the return address + * @task: The task the unwinder is being executed on + * @idx: An initialized pointer to the next stack index to use + * @ret: The current return address (likely pointing to return_handler) + * @retp: The address on the stack of the current return location * * This function can be called by stack unwinding code to convert a found stack - * return address ('ret') to its original value, in case the function graph + * return address (@ret) to its original value, in case the function graph * tracer has modified it to be 'return_to_handler'. If the address hasn't - * been modified, the unchanged value of 'ret' is returned. + * been modified, the unchanged value of @ret is returned. * - * 'idx' is a state variable which should be initialized by the caller to zero - * before the first call. + * @idx holds the last index used to know where to start from. It should be + * initialized to zero for the first iteration as that will mean to start + * at the top of the shadow stack. If the location is found, this pointer + * will be assigned that location so that if called again, it will continue + * where it left off. * - * 'retp' is a pointer to the return address on the stack. It's ignored if + * @retp is a pointer to the return address on the stack. It's ignored if * the arch doesn't have HAVE_FUNCTION_GRAPH_RET_ADDR_PTR defined. */ #ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR @@ -895,6 +901,10 @@ unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx, if (ret != return_handler) return ret; + if (!idx) + return ret; + + i = *idx ? : task->curr_ret_stack; while (i > 0) { ret_stack = get_ret_stack(current, i, &i); if (!ret_stack) @@ -908,8 +918,10 @@ unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx, * Thus we will continue to find real return address. */ if (ret_stack->retp == retp && - ret_stack->ret != return_handler) + ret_stack->ret != return_handler) { + *idx = i; return ret_stack->ret; + } } return ret; -- 2.43.0