From patchwork Fri Dec 8 10:28:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Masami Hiramatsu (Google)" X-Patchwork-Id: 175750 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp5363823vqy; Fri, 8 Dec 2023 02:29:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IEz+FYkESbHi1yTFTcRDvxYct6gTw4sMvaHsfgSMeMMuGco6E1Wo/Ct0UpGFhgGN8KyrQQF X-Received: by 2002:a17:902:d511:b0:1d0:6ffd:9e13 with SMTP id b17-20020a170902d51100b001d06ffd9e13mr4836710plg.101.1702031387614; Fri, 08 Dec 2023 02:29:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702031387; cv=none; d=google.com; s=arc-20160816; b=kdfNRtAI7cxfeGQ1R89b/HA2uPielFBjepDSZtFYpVjNdRmm6MlFK3RPgZiMpfzKRx YYlVF+yyZPzg5am5voULaow6q0euUBgTwDzwABn/6zgrAbN0xZqffgQLJYiQ2ZvV5XIY GjfuHUbMHUfihoCqL6MEFJAQ3DUpXr/IGDTZqlpXbIu/BeXb1ZbNc0gaLjXTVrDaaebe s4R7h4srunOusPd7nk2ko4AGjalHHDCfu12EfT87lsk/gPk5Xjb6l0IEWzG9t3vLFUwU UzI1KlQy3ScuPDa+9U8xGlhmpE9+xMEleiAs+Kl6qEHGfMG5E4okKlEj8O/Hoqr4o+Hd 0aSg== 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=DTVHGVwQZZBvqCqPcw35Ltjz0zNvoABeQNqXyujJvmA=; fh=SIgps5XdV0XNwjZfT2uAI7g3mrspDldK9Qs8qQAfoa4=; b=hfee85baSQdFgeofCfK8VjP55uvGKQQpwZRJ1PvVn74mkWt/EWvkXBKXE8HmDNDH5z txCkSwMTC9LV24XksGnwt8jVHoC0FdhALiHtEhD4PCccVUuAmXWDIid4iSi1kz/2XEdg Qo56/jUUCv0LPKWi+ubP/GFq8ExgjP0rjldAe6cITT4t8EvHt/2xDE34BHZ2KicaqR5+ xD81aDbrIsDFISvYNT1DFMPHYV85nTiIrhqpdEIq9Hl119RHq7q8WEgGMmXKTjMOysfo hvgmkri5mEYzuUCajCfWNy9nvQqnQ90IOxeqfThZ3FLe1tSjo7fI8f6YEA/QbaROJpdA eZHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=M4lND6uY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id x10-20020a170902ec8a00b001d1ccbe983asi1388532plg.383.2023.12.08.02.29.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Dec 2023 02:29:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=M4lND6uY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (Postfix) with ESMTP id 3DACC80ABFC8; Fri, 8 Dec 2023 02:29:45 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235986AbjLHK3f (ORCPT + 99 others); Fri, 8 Dec 2023 05:29:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233360AbjLHK3R (ORCPT ); Fri, 8 Dec 2023 05:29:17 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 265AE1FC4 for ; Fri, 8 Dec 2023 02:28:37 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C2033C433C7; Fri, 8 Dec 2023 10:28:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1702031316; bh=pPNJXjygt/V4vNYQ4X8NJkqM9twm0EdHZQuhceTJqsQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=M4lND6uY3SW66V0a116zBlvW7YKyDx2JWvKdATv15tvxVs1GXs5ibzMrf3AouWWlm VZWOWCEUTOmagWiyUuRHNRrYwpEFM2i8rqFUGPXtxhe9R8vuP4FuPk6DGFskiMSYdZ CGvTIJGqE2DFalwb9Qh8MJVZc6au4hduQkmMIEdF78R+E3QMXePZ5uhXgXJsVUnumd +4UTBzNiyf6godknzWaq4txBMIOe0FUd4rgCF8GlQCE+UaxYGZ3CeF/qJd4u+6Wie5 vE2dtpOLCaYuJQSww8ynTzuiHDYocyqtb4P2ePNFWFpvGkerx7xxLW/kEF3wQRV8ps bug+7C7g9HEXA== 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 v4 21/33] x86/ftrace: Enable HAVE_FUNCTION_GRAPH_FREGS Date: Fri, 8 Dec 2023 19:28:30 +0900 Message-Id: <170203130982.579004.8630034805609766591.stgit@devnote2> X-Mailer: git-send-email 2.34.1 In-Reply-To: <170203105427.579004.8033550792660734570.stgit@devnote2> References: <170203105427.579004.8033550792660734570.stgit@devnote2> User-Agent: StGit/0.19 MIME-Version: 1.0 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 morse.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 (morse.vger.email [0.0.0.0]); Fri, 08 Dec 2023 02:29:45 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784709264206676767 X-GMAIL-MSGID: 1784709264206676767 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 3762f41bb092..3b955c9e4eb6 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 214f30e9f0c0..8a16f774604e 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