From patchwork Fri Jan 12 10:15:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Masami Hiramatsu (Google)" X-Patchwork-Id: 187684 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2614:b0:101:6a76:bbe3 with SMTP id mm20csp78195dyc; Fri, 12 Jan 2024 02:19:26 -0800 (PST) X-Google-Smtp-Source: AGHT+IE29vtxiib6Y1rwzYdYjl9d6HpV5ReCcqTP6AFP249JDGtM1ooZ7eUpZLAdhPHyDfPfYIjI X-Received: by 2002:a17:907:7eaa:b0:a2c:96a1:99ed with SMTP id qb42-20020a1709077eaa00b00a2c96a199edmr1436506ejc.11.1705054766426; Fri, 12 Jan 2024 02:19:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1705054766; cv=none; d=google.com; s=arc-20160816; b=n4DNBYeyyJGX4PLRr5hvPMzSkiMstTW4gbCysmqKoSZOVBWBfamXgt7AY+SLgWbZnb LrMmAicbuBJG5mYxnvkvVWQ2ZHHMT+nihElrAL5Oe/ENLd3Y2A0ud0b6kAXSsWQE0cTH Fxj8mfyIDcgt3/bljJCXqbM8sA2pNTENW/+oThiA75+hNuGERVvO2K82fdkraIVgK5fz qpKA0FeLIWnEeQsJhz52UwqQQ0TQgNb4JZ9izQwHp7JtBP+cIa8FV95mjV8Xt3hCW8jb UaVb7eeAkeIlVrgM+0xae+pEKFyN/NEGQ9AwCXZc3NLawBi+VvM9ZNewQrivpbim8GhP X5Xw== 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-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-24572-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id cf4-20020a170906b2c400b00a2335936fc9si1320390ejb.458.2024.01.12.02.19.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Jan 2024 02:19:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-24572-ouuuleilei=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-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-24572-ouuuleilei=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 06FEF1F236E6 for ; Fri, 12 Jan 2024 10:19:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4217160EE8; Fri, 12 Jan 2024 10:15:43 +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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787879506492396031 X-GMAIL-MSGID: 1787879506492396031 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