Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp2197742rdd; Fri, 12 Jan 2024 02:19:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IGt0UX9JwFn9Uhtflbo5zst+76xJ0Uax2L7s0TyWoTBiG4xoH3JZCHCUVzRdE5MQDarwxC/ X-Received: by 2002:a17:907:5c8:b0:a2a:c113:2677 with SMTP id wg8-20020a17090705c800b00a2ac1132677mr1361513ejb.61.1705054758469; Fri, 12 Jan 2024 02:19:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1705054758; cv=none; d=google.com; s=arc-20160816; b=Qd93ikoEJmAhPDiI/1aoX+//hE4MW+lzRHbkKYUsoIvrikT6qntPE93HUZydv7c9Qd lwCPxDQz/CH1KwembAHb6AcwNaE4BPkzRWRzevMfahhDMBMyjTGYXSBMoJjlyxeqjnPl zm3gfcQB8/5GfO7x2MmXP3nLkecgNUNf1Qtuc9d4m797gH4MlK/82tsgd2+WjYSIDFYm 2FWZyiv4JDUQ1zp1pqcg5IVs5BvYD1otlw2NumoXbu8bHjEhsMsjM6TpOw+V3BC0+Kxw 5LPV70VL+4okwcMV5f9wX1doy/PkSl+JwsA22UJFY7gLuRhcR2RH3OJV2yPv9NaS4tve pEpw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:user-agent:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=WXaO3rPugAi0HaTx2w271P8mFLQmqt9kUi2g1TRQzOI=; fh=SIgps5XdV0XNwjZfT2uAI7g3mrspDldK9Qs8qQAfoa4=; b=hCj6HfYcJPQNjE+fcqqgzyDitZrwRIXa9mpmDUUCeEqHRn7qdJh6zULwnGEWIR1Vtq ASu8Rafga4D00gC8dBZpUbSs5G0J5+jL47gz3z/p36acxGkGgpVZkGbzPri+puWoYAvY u3RFQwgsPKKSvemITDAyOd+4i6UPW8UGlHFIGT/A74AHrZu+MaH2VhHyTmXF1hL9fNKz b6sPZD7/uFPA/sZavuZtRR7FPV32eX6WM+Uue4OHMK4y5IMAnZoIaPSvJ6C/I/jfYReA ac0vLGGrJ3kmIduzSPHdKT9OxzmLNdNriy+KeEbiZNL3ulgY/3sTnvHTWb7QBcCZeS8o 72JA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=UyqSz4nE; spf=pass (google.com: domain of linux-kernel+bounces-24572-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-24572-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id ks3-20020a170906f84300b00a26b9a288bfsi1295052ejb.370.2024.01.12.02.19.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Jan 2024 02:19:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-24572-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=UyqSz4nE; spf=pass (google.com: domain of linux-kernel+bounces-24572-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-24572-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=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 am.mirrors.kernel.org (Postfix) with ESMTPS id 37CB61F22659 for ; Fri, 12 Jan 2024 10:19:18 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 74AE660EE0; Fri, 12 Jan 2024 10:15:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="UyqSz4nE" 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 A09A66350B; Fri, 12 Jan 2024 10:15:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 85A87C433C7; Fri, 12 Jan 2024 10:15:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1705054540; bh=5r0EiHY3VraDrYlZ1BWHRcwyp2h65Zz0Zx9/NZDFHmM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UyqSz4nEzBqO7v4mv9knVojGcee0dJ02LGk/tC8RUqRoNURh5WVDuP4dgerOMDYbE e6BxgQoO2bfbJrTE5uImzIRxDmdkM5ZaK0ajJu/rq9CcjQkDQ2L5SQB511/GHkt0Si E4nIh4BlwBmDbDtmxVSS6JrfEK94XmjB9GBRhCHY2mmBzr6BiXr+gd6Lx17qSRk+Gi dKl5OlUDcSWgn26aYD37e8QQtpAuOzCmBlVUwbAJFMF3z27mF27wAhJpk5vheQhpYb YGajNiqKfDMYrwZCKYJUFxKUYuJ78R4HeVR7WrCqV4dPigf2TZD9/7+ixtj0PxvUVt pnuiVjUjqgLkw== 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 v6 24/36] x86/ftrace: Enable HAVE_FUNCTION_GRAPH_FREGS Date: Fri, 12 Jan 2024 19:15:34 +0900 Message-Id: <170505453413.459169.7198256674189199722.stgit@devnote2> X-Mailer: git-send-email 2.34.1 In-Reply-To: <170505424954.459169.10630626365737237288.stgit@devnote2> References: <170505424954.459169.10630626365737237288.stgit@devnote2> User-Agent: StGit/0.19 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" Content-Transfer-Encoding: 8bit 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 1566748f16c4..375ad280ee75 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