Received: by 2002:a05:7412:1e0b:b0:fc:a2b0:25d7 with SMTP id kr11csp971450rdb; Fri, 16 Feb 2024 00:55:26 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWvfcyj1uBEO6xFEvjWnNN9zwcog9pXvnNURHNUzFmGFtRiKqlKWSWhOyNGIHVfbSKO9fWty+uK0bqg1+uECidFqB0PVU8u/8mJ2XdI7A== X-Google-Smtp-Source: AGHT+IG2FwWaBlSblr5DMVJCq/cGdt3c+HWbTfUM5bf0BrXIxXt1Gvs1oWehXRF0dlXjPslNVCah X-Received: by 2002:a05:6214:212b:b0:68f:301b:5482 with SMTP id r11-20020a056214212b00b0068f301b5482mr1543919qvc.8.1708073725930; Fri, 16 Feb 2024 00:55:25 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708073725; cv=pass; d=google.com; s=arc-20160816; b=lcFwnB5+VMrGTlaPlt794eryh/AX/cvonrwG86OxGQ4xU7Neyg7YE5BvPHQ8B8OthM r4T4mslkgWKNyzxbdGDLyyV2rDpte6rSgijKQ7STWOnqNjOu4EK3iiVEemTWq8GiCO3u AjkUEhcFebG1WajE5918GizXBVEi3syytKTmf4xrO9aceZ73lMDqqQq/sHGlIgf1SfS4 hFqPMUA91W2K1q589JqmWs/s5Sa4hpL3EAeWAaphh9mbzOBqGCgDdpTd7kwq6+4fpdHx 560poFHvaxpKOoXrf7aNPWV3xTltj/SY99YwyPh4hvNdGkcriQeNYn5Pu1drks6NNRq+ VNaw== ARC-Message-Signature: i=2; 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:references:in-reply-to:message-id :subject:cc:to:from:date:dkim-signature; bh=RSdn8tubIxskMFRkBOEiLV2uEA5V/kKFgiwv1sk1Cic=; fh=Ub95fBsiseQVLo56fdyDA2zH9Ec5p8qFryhFaIE+jLo=; b=Tni+2TwGbkxp8v5N5im0TuYa660VOHftbpdAyRj+BfnWdO/UxYuUmAek4JlB7elEKb CwnOXhB8JhlU5FGxsoJez2gF5ZZQvsc0Z3/oCvVtZ21gf/8y3jseXRygvefsuXoi4xZe STqpj1UM7RILVNhCZim0o73sWLEVAtxLmXo2gQ0TddCcFdWXmOWoV6pn+vmw1JRfIa2f QfC8UzyFGIa5ilpL0K0wK5CDA/va/702wccPZnjmD6ttw3N2D7Q+M4GKQ9FFcgLOtXWC H75QQOf14m4n2LxUBS6hGqJl3VvY0jbLe6uWGYUMwweprii7FNJKxJBNtJyBDEyj5EHa kWEA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Hjza4dJc; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-68278-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-68278-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id iu1-20020ad45cc1000000b006819995dec8si3391438qvb.251.2024.02.16.00.55.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 00:55:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-68278-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Hjza4dJc; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-68278-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-68278-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 651C31C21311 for ; Fri, 16 Feb 2024 08:55:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3C7871B959; Fri, 16 Feb 2024 08:54:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Hjza4dJc" 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 4D2A31B946; Fri, 16 Feb 2024 08:54:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708073670; cv=none; b=JO3N3NEVOxOjbxUwjLbfcs9sx33vD752qVtpQJzGYHZVex0WtdLh4hxxMpbKlc2StXmjacxSvZlpXtyj4Pi2jZJcwusyfFo7JvW5HgBEScx5uWOGkwL+N7k6fpOQIxDop0BjdENQC1P8FvDUhQRwbElO+tVWQwI4YB2j7Ejy/7M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708073670; c=relaxed/simple; bh=8yoLoVuMaVp/tWX6NtXM03pCFmXTbQbmXsQiE0p0b/A=; h=Date:From:To:Cc:Subject:Message-Id:In-Reply-To:References: Mime-Version:Content-Type; b=D6vxtixH3aJlOsmai392mjnf6nb1Ym8/LeueJayP4lV5sckdouJqH5NQ+YqvOUw9Gi3tkVSPwEBzn6Agi8Q4qG4cNU6H2C/AMOdeFECHGlDxJD0MyB9CtsUSLyM3D46BbVBedcyfQp4mzE2C2QdOKnVlyhmfr1t1QbCalD9GUiE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Hjza4dJc; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id B7678C433F1; Fri, 16 Feb 2024 08:54:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708073669; bh=8yoLoVuMaVp/tWX6NtXM03pCFmXTbQbmXsQiE0p0b/A=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=Hjza4dJcDPk2BdpZ3yzVgVWVaslyT2Wn7Knq/7ZeALFYddcDeBm7KaW9L2YVVDVHe Y6JSuvyEevYLGUkf+4WXAiMlC0DI8WJCQETjx4Ruqh/5W8RFxUxQ+uZDM7p1IwM/62 YqgXqK/RbNo7rrKmVbbBpa6qY41McsPsjpM2I46LH6g7RKa4VneWr77aj70zYvu6Qo qRzdy4UnBcovwBJCUuHDfHD6HVImBd0qkSuYn5tm0ZuTBB2pPyv9MNlFQ2oITaZQOC n13j7JX85t/kWyLpddo550Nwk4MJ4MuUnqSXj/vatzKXECnBRBtExeBnJofpiCM+SJ qzHPjso2tMXGQ== Date: Fri, 16 Feb 2024 17:54:23 +0900 From: Masami Hiramatsu (Google) To: Steven Rostedt Cc: Alexei Starovoitov , Florent Revest , 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: Re: [PATCH v7 24/36] x86/ftrace: Enable HAVE_FUNCTION_GRAPH_FREGS Message-Id: <20240216175423.1a5d7a9762b5a693e273b40f@kernel.org> In-Reply-To: <20240215110808.752c9b67@gandalf.local.home> References: <170723204881.502590.11906735097521170661.stgit@devnote2> <170723231592.502590.12367006830540525214.stgit@devnote2> <20240215110808.752c9b67@gandalf.local.home> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-pc-linux-gnu) 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=US-ASCII Content-Transfer-Encoding: 7bit On Thu, 15 Feb 2024 11:08:08 -0500 Steven Rostedt wrote: > On Wed, 7 Feb 2024 00:11:56 +0900 > "Masami Hiramatsu (Google)" wrote: > > > 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 5edec175b9bf..ccf17d8b6f5f 100644 > > --- a/arch/x86/Kconfig > > +++ b/arch/x86/Kconfig > > @@ -223,7 +223,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) > > This unconditionally slows down function graph tracer for no good reason. > > Most of the above is going to be garbage anyway, except the rax and rdx. > > I would recommend than we set something else in the ftrace regs that states > this only holds return values. Anything else will just get invalid. > > I'm really against saving garbage. The purpose of ftrace_regs is that it > can hold incomplete data. Ah, OK. I misunderstood. I thought ftrace_regs was expected to be filled with reduced (arch-defined) register set. But it just ensures that holds some registers depends on the context. Thank you, > > -- Steve > > > > + /* > > + * 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 > -- Masami Hiramatsu (Google)