Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3068697rdh; Mon, 27 Nov 2023 06:01:59 -0800 (PST) X-Google-Smtp-Source: AGHT+IHgMdpu35PF2XKetdZanWF76nLLpF/nGCxM9EAGbA4gfJrl+jfbPBUBcMpJ3/PuCZnXGzTr X-Received: by 2002:a0d:e885:0:b0:5cb:d85d:5a23 with SMTP id r127-20020a0de885000000b005cbd85d5a23mr11888179ywe.10.1701093718588; Mon, 27 Nov 2023 06:01:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701093718; cv=none; d=google.com; s=arc-20160816; b=cfyrbXsqpP4DCOF/CwB6FnN6PzU8Duoih9LYsXfhlXh1WyVQcmxtPXjUcUcOoWPwJH iURbCsylGZeqZtKUslU4MsbxdJwvSHcrvS3nqreqn3RVKh+FJlNjoSE1mn+dFMzzjXJd aUTWu5VlqeJFAcVGd8Q/Li6U4a8iO3OQdl5TaXHah/qlQwVwR0AJylx6+7L1b2i3Tq4H DeEaB0ZoJo9DnewMCXKdBvUt5svBVBrEHBI6rEbQtDPl3nfpGncv0sI/l2pTM+k2m/3S ktn8OZxIoKS80SxXbHapJerU3j+aN4A5Ux0FylTYm2IaenwQdxN4Vd/+ElW5t8PUAVd3 KSMQ== 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=XsLCt2zocbaN2T0HWhPnSL85Pn7Z9+qHfokTfISPpsQ=; fh=SIgps5XdV0XNwjZfT2uAI7g3mrspDldK9Qs8qQAfoa4=; b=QdXxZ9lOeOQysdSyYhS3rU+kz3TnrI4/Agzv2GDz2/vGOSZM1HRIulXmrkDu5Yz5UQ XMiAoNNzXXRhh9V7OXRZkxa8ar8z/y64jIJfVEXaas3G4/OlSdNLDQlDTxLHOwcAU4h0 bIu/GBi4qJa8vpUCFEsCDVAuXjxE4N8T4OWaREBjUoF7EFBdd6XqDBRBlf2LTFDF8fQ8 Ek8EM+8wtPM09Ml7hxkI+7G6b25ZvwUmssAresAvXULunZU1Ba02oaxvzTeJvmpRGDW0 u45HJyxQPWWAVgm6ddspGB+iW9LUbjiCddEP6nvPbTfkfspZiK5FJfDrMGaa5XEvhBUD KNfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=tc1U8C8B; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id fw5-20020a05690c374500b005734edce364si6652026ywb.548.2023.11.27.06.01.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 06:01:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=tc1U8C8B; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 483A0805E45D; Mon, 27 Nov 2023 06:01:52 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233099AbjK0OBb (ORCPT + 99 others); Mon, 27 Nov 2023 09:01:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58186 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233739AbjK0OA5 (ORCPT ); Mon, 27 Nov 2023 09:00:57 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 263E42701 for ; Mon, 27 Nov 2023 05:57:22 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E92A8C43142; Mon, 27 Nov 2023 13:57:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1701093441; bh=5BNVBB7peZKxOYYyHONtdZrMFDGpcSDPvsIQ08LtrvI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tc1U8C8BCNN5UzRem7hdSm4O98H/yFk/QaKBcxjKhB4Y15Ed35bD5P1DcNtu7Ly7L yDFDZM5DkWfQNzsY818YkfJMK4+lW1sEppUvuPGc7SneWKamDbU4hKW0Dzb1Bk/3yf ejFzvhMVB+8MDzkAzj2JqpeJgGQZFgRlcQY2hQ0KQv9jy8t+chs3E4F2Zq98+2dfla MHOPRV7nFL7fVmSxa69OyO9ke4gqW6ttpsfLK0C9pJj/1cA1gKjugk7V75VG2D+5/7 67cT19Igd4Vk0yODvBs6w8akuKyDEgi0fwnkLUTTEAV5yLWendJUvwysvzAhqnS9fu 29nl/Z1FmT++A== From: "Masami Hiramatsu (Google)" To: Alexei Starovoitov , Steven Rostedt , Florent Revest Cc: linux-trace-kernel@vger.kernel.org, LKML , Martin KaFai Lau , bpf , Sven Schnelle , Alexei Starovoitov , Jiri Olsa , Arnaldo Carvalho de Melo , Daniel Borkmann , Alan Maguire , Mark Rutland , Peter Zijlstra , Thomas Gleixner , Guo Ren Subject: [PATCH v3 21/33] x86/ftrace: Enable HAVE_FUNCTION_GRAPH_FREGS Date: Mon, 27 Nov 2023 22:57:15 +0900 Message-Id: <170109343500.343914.8822279017262195121.stgit@devnote2> X-Mailer: git-send-email 2.34.1 In-Reply-To: <170109317214.343914.4784420430328654397.stgit@devnote2> References: <170109317214.343914.4784420430328654397.stgit@devnote2> User-Agent: StGit/0.19 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Mon, 27 Nov 2023 06:01:52 -0800 (PST) From: Masami Hiramatsu (Google) Support HAVE_FUNCTION_GRAPH_FREGS on x86-64, which saves ftrace_regs on the stack in ftrace_graph return trampoline so that the callbacks can access registers via ftrace_regs APIs. Note that this only recovers 'rax' and 'rdx' registers because other registers are not used anymore and recovered by caller. 'rax' and 'rdx' will be used for passing the return value. Signed-off-by: Masami Hiramatsu (Google) --- Changes in v3: - Add a comment about rip. Changes in v2: - Save rsp register and drop clearing orig_ax. --- arch/x86/Kconfig | 3 ++- arch/x86/kernel/ftrace_64.S | 37 +++++++++++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 66bfabae8814..4b4c2f9d67da 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -219,7 +219,8 @@ config X86 select HAVE_FAST_GUP select HAVE_FENTRY if X86_64 || DYNAMIC_FTRACE select HAVE_FTRACE_MCOUNT_RECORD - select HAVE_FUNCTION_GRAPH_RETVAL if HAVE_FUNCTION_GRAPH_TRACER + select HAVE_FUNCTION_GRAPH_FREGS if HAVE_DYNAMIC_FTRACE_WITH_ARGS + select HAVE_FUNCTION_GRAPH_RETVAL if !HAVE_DYNAMIC_FTRACE_WITH_ARGS select HAVE_FUNCTION_GRAPH_TRACER if X86_32 || (X86_64 && DYNAMIC_FTRACE) select HAVE_FUNCTION_TRACER select HAVE_GCC_PLUGINS diff --git a/arch/x86/kernel/ftrace_64.S b/arch/x86/kernel/ftrace_64.S index 945cfa5f7239..89a479f0c332 100644 --- a/arch/x86/kernel/ftrace_64.S +++ b/arch/x86/kernel/ftrace_64.S @@ -348,21 +348,42 @@ STACK_FRAME_NON_STANDARD_FP(__fentry__) SYM_CODE_START(return_to_handler) UNWIND_HINT_UNDEFINED ANNOTATE_NOENDBR - subq $24, %rsp + /* + * Save the registers requires for ftrace_regs; + * rax, rcx, rdx, rdi, rsi, r8, r9 and rbp + */ + subq $(FRAME_SIZE), %rsp + movq %rax, RAX(%rsp) + movq %rcx, RCX(%rsp) + movq %rdx, RDX(%rsp) + movq %rsi, RSI(%rsp) + movq %rdi, RDI(%rsp) + movq %r8, R8(%rsp) + movq %r9, R9(%rsp) + movq %rbp, RBP(%rsp) + /* + * orig_ax is not cleared because it is used for indicating the direct + * trampoline in the fentry. And rip is not set because we don't know + * the correct return address here. + */ + + leaq FRAME_SIZE(%rsp), %rcx + movq %rcx, RSP(%rsp) - /* Save the return values */ - movq %rax, (%rsp) - movq %rdx, 8(%rsp) - movq %rbp, 16(%rsp) movq %rsp, %rdi call ftrace_return_to_handler movq %rax, %rdi - movq 8(%rsp), %rdx - movq (%rsp), %rax - addq $24, %rsp + /* + * Restore only rax and rdx because other registers are not used + * for return value nor callee saved. Caller will reuse/recover it. + */ + movq RDX(%rsp), %rdx + movq RAX(%rsp), %rax + + addq $(FRAME_SIZE), %rsp /* * Jump back to the old return address. This cannot be JMP_NOSPEC rdi * since IBT would demand that contain ENDBR, which simply isn't so for