Received: by 2002:a05:7412:1e0b:b0:fc:a2b0:25d7 with SMTP id kr11csp557587rdb; Thu, 15 Feb 2024 08:12:17 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUyjUn4PzBRh3zOACyr9zSfQGGSKpfWnwtcbxF4wn6ADFzqa5bWPvwgJ3XWqioEvEE8pVLisJDMjG6Day4iwW2NPBRz4c4cYonXralHpw== X-Google-Smtp-Source: AGHT+IFPW+Mawb8nV29SlGllQsuZNFgU4oBEmXq9nrjixhEe1iBz5URWpGDql6dymP+RxCPxj8Wn X-Received: by 2002:ac8:5a8f:0:b0:42d:bf54:75d6 with SMTP id c15-20020ac85a8f000000b0042dbf5475d6mr2850816qtc.64.1708013537636; Thu, 15 Feb 2024 08:12:17 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708013537; cv=pass; d=google.com; s=arc-20160816; b=AFKU2CgQ1vZ8SQljIAHGglIjf2j+q9PLVFpVZzb0kWoq9azlYDgVTrDsCxC5WL4FEX 1gNFdt5nR5u2+B3k9ZUgSUjVJNk6km9bjn1XZ8rdBhzNDqKSLe7eAhrNcjVv9z8YrX9w FMVf4VAPJYs0n5vaY2Ws7pG9HdxkPY8mR5/A3qAlIVbKAnLOultloipPZOdzoec5Yq8p STAo0XWx3ys7YvvOHnlZPDDXGc+7Zegg956qY7d5JWnB/jJLQXhg7PGEaHMp9FYlQocZ jL7vShWQd7ER0d/yzKqH5EWVIldx6QduWp2QbDXnj2WRB9myzJaYq7CWCWhXaRin7Yg9 DDow== 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; bh=JFzmAsLHeGYvyb0Q1WtsDlCLwoRFCYkOgBwNoWfU/iA=; fh=VaF/w+FGpk/prTFLJfCoV+GJDmV0Sd6dt8EMZrBTbz0=; b=Jcasfg6JJPm9qHnhxdw1DbMDyG3VAdAFcfrsihTuhC+Im+KDsdeVDrqYFdHPv6Wvry OZjSA86OEMKwBKsHHi3V3Z9btWiREDw25lFUUd0gydQ5QPVndXt37Sdlz8GsagqmXNQX yo4RwbcJa8hR9Rrf6CHp8SvFVn72IAb1wyHjleddYyHOC0KpE7w5iiKM73yCqLJ+wF5S uH6rcwfBhebiJIu/nDdfFq7dxRig1DTOdoL6SzKuDjJ68U36GwvmWqakYGMRldRNacde s9VbingObqNyOjZdj4A+5OwUliCBHD2QFlHIAyZXF5mk3EEB+04uI6g3VwaLATAu2zPP YTTA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-67279-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67279-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id z6-20020ac875c6000000b0042c6b1f2e07si1589877qtq.123.2024.02.15.08.12.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 08:12:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-67279-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-67279-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67279-linux.lists.archive=gmail.com@vger.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 6272A1C2358B for ; Thu, 15 Feb 2024 16:12:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CA84F1350D1; Thu, 15 Feb 2024 16:06:37 +0000 (UTC) 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 4CFF013342A; Thu, 15 Feb 2024 16:06:36 +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=1708013197; cv=none; b=CddAc8iwQt5auDUeW4hfYbs34zpzsX0yas+GgITu4gY9TO/6GRs+WmfB2tRyBAuMz17L4Tg7QS4iVQGJ/9Key2/GWxhDmDr9swo23o/bkBiVo4+kwLridClvkdbIVNcXgO4zYpiAktIcD72OBbtU4BWx/9EGyJJPhERboZtxCCo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708013197; c=relaxed/simple; bh=JICvr0D4tYvl5DaKYMCiv1tn6ushEDdrtmyTwJvTNJU=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=COjHdklXc+JVZrh1C0JSVEsGrIXS+R3lBP9M7SIgnCqYh7Q12CGBMietnhQCmR+aKbxQFSlTYZmPrD/gzArm64bxk9LP+FP8DZTRhvaglxNWgMRM4/dJSZLJV30bVKedLIALUqWMRRMQs8HYt8/wPzt96GCPE2lCg8fW1mu7FVY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 00AA4C43390; Thu, 15 Feb 2024 16:06:34 +0000 (UTC) Date: Thu, 15 Feb 2024 11:08:08 -0500 From: Steven Rostedt To: "Masami Hiramatsu (Google)" 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: <20240215110808.752c9b67@gandalf.local.home> In-Reply-To: <170723231592.502590.12367006830540525214.stgit@devnote2> References: <170723204881.502590.11906735097521170661.stgit@devnote2> <170723231592.502590.12367006830540525214.stgit@devnote2> X-Mailer: Claws Mail 3.19.1 (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 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. -- 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