From patchwork Mon Nov 27 13:57:15 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: 170157 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce62:0:b0:403:3b70:6f57 with SMTP id o2csp3133722vqx; Mon, 27 Nov 2023 06:01:56 -0800 (PST) X-Google-Smtp-Source: AGHT+IFRhYj77PEXkJQVnGLcLtwsKAq7NDRoH/j8Ypg2FeIjqsocFR5hwPthiBqz5jtUDu4gD4uC X-Received: by 2002:a05:6870:b512:b0:1fa:2014:6d6e with SMTP id v18-20020a056870b51200b001fa20146d6emr9499453oap.39.1701093716651; Mon, 27 Nov 2023 06:01:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701093716; cv=none; d=google.com; s=arc-20160816; b=Ke5hs64fX8FJ/GDGEromulSi3LvNlLi2Z8mSZL2b+WLrL1suDYRH8ymNtfdFRT2Trx cwlz6wCazilaRu9KZEyu/nP9x3I8Yv2033h4qNGWr5wm+DPGfbhzue9hcaUT0KfjprET 4Olu5X36dhXYROHAKE9Yy7HT7UVwJ77mLqmB8sOeCzUyGn6O6+7e75pXqNY22tsNfEgM Nb0CN/jFhPToLOMNdlIc9yLFz5xSSfFs2hPsUbCDQtjvFWizyTfbnUb+XEpMn0ORoD8P 81jIIw9z5Ro17++Eqso+HggxX7aseasNWMDSGLR0yA2EZ7xpYT5gqPKnYul4EBvbSq7X 4Qag== 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 23.128.96.32 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 agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id z12-20020a05683020cc00b006d7eee62b0fsi3605351otq.328.2023.11.27.06.01.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 06:01:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; 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 23.128.96.32 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 1A68380964D0; Mon, 27 Nov 2023 06:01:46 -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 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:46 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783726045039380561 X-GMAIL-MSGID: 1783726045039380561 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