Received: by 2002:ab2:5d18:0:b0:1ef:7a0f:c32d with SMTP id j24csp421265lqk; Sat, 9 Mar 2024 17:38:08 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWIAPY0VNo7DViWpOgKs9zacDuEi99BpOXC4HgUu2/CPO0iDkEGEIuq7PkrUckEOL65VdOqT1vwqKxnLeYZ+pFejMsBjwgLr9x9tKcYSg== X-Google-Smtp-Source: AGHT+IGaI1fgKmkdHlqkBKXzklH93BWNnELRCojThjJrQVflpgkdLj6aPsu8HSYBEMMYU6yLY5ff X-Received: by 2002:ac8:5a0b:0:b0:42e:6f56:c358 with SMTP id n11-20020ac85a0b000000b0042e6f56c358mr4394154qta.42.1710034688431; Sat, 09 Mar 2024 17:38:08 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1710034688; cv=pass; d=google.com; s=arc-20160816; b=yau8almUTFqT3dyEgBmOw61QopomuMiIfW8NAYN/fUUiQFcZqM0CWR+BnlzymgNFN4 GnFfZkQgTnaE2XsvE54WTzMPHlzV+5NS0M3iIiTKFpmM6ZDlXf2FwmuUlS2huPkh2qKX GTbSvTkw3mLA0NF9Fc2nupHmVG5BvwcblVVbWeCv7EhyTwvsUGl/EMhBjMxBH/N6soVT atRBCnzXpyoK8gtfQ2ADmt+5AWzNDy3Mj/kVVIVfa43MWHcX3d9Y4xzTxa6q5SL6nfXN P0sOsrLZcvKpNoCRqY3AY1Qe19wCNZ4Qgdq/raMV3F7mjrcpy38oLOVvoeE7hB1F25EP 13xg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:message-id:subject:cc:to:from:date:dkim-signature; bh=OAMrPg1yA7fpBYU45KPtCnkTYogqCg4+DC4eyzReujM=; fh=CDSRHwQuc/m+AsbfmucyYQQv+DLy2lcLZd/1NzGXW4c=; b=iVLJjoAzx6/IFpfTf4SgjSJ/u1+mTh9g6NJaxaJ+vXx6KhFBdnen0ivofGxFur/pth Cw+Bd0jL/ATStT7M6l0RCuPycqfRo1w6t1HxzwogWeC00WiRCKtLgzNcKzd8+uD2wFn7 CPIb/sBXwQ64hR0i5YHHkpGIUffvyXygOeptkgvHXXQ/MeaswdXufS0wTpzMfok0Plpe JrIlX7MpkOWPut4MxfoGN/s4cnz4Lr/kbRUhTJqYMBBjAM35N+zWNxROkHAQFqetHTkH 6t+JiWoUt01nbaImO0HhI/2urdpzM1r0OQGzYESa6jJxlli2ThhggJcSFF5D6Flu+dRA 4u3g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=VI3jRqHX; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-98082-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-98082-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 x24-20020a05620a099800b007882536ae13si2552529qkx.624.2024.03.09.17.38.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 09 Mar 2024 17:38:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-98082-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=VI3jRqHX; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-98082-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-98082-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 D24851C2097C for ; Sun, 10 Mar 2024 01:38:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3ECFC1366; Sun, 10 Mar 2024 01:38:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="VI3jRqHX" 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 3D856EC5; Sun, 10 Mar 2024 01:38:00 +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=1710034681; cv=none; b=QHq0tVGHB6IzYbD70rpGhlqcCqtE5NLoP8J+kLo7JCdN/Q4P8SIRGoEiSqPRHN4cHiMi2eZ8sRYzLUY8QxnwRcWDd9pVVbYXFqfCp4hMkMLm8K14+rcnvYfJ9PaIUM+UVVIwMt9jFJV0D4r1rBM46b7CC5K3KJ4JRt4wQQHWTEk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710034681; c=relaxed/simple; bh=mLysYKJMnp+csqFKfyy0BM0x56EkfLIUO0ZrRPScLJE=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=pw1AFhlLFUVXnlWpzOcJzbXrJ/V+bg2RHVt7QCdTe6YG8y86vI22/Oqly9WfRAOsN149gpWAmpCAypFVGO8ZbxNvchL9BYRQ9fJp0Eph6AdXKENNtWiXB7MvJIY9/ohMPX+dtnKHfJMxNeaAydcMRw2oKMVdWyYGpSoTxhUOTNA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=VI3jRqHX; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id BC111C433F1; Sun, 10 Mar 2024 01:37:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1710034680; bh=mLysYKJMnp+csqFKfyy0BM0x56EkfLIUO0ZrRPScLJE=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=VI3jRqHX/kHfxrP0VnhRu3y7u0mu3xQ8TIoyJmBFZNx941GnzQV8toeJaP36FC+DH HEueSM7E64MbMnMr60kH22/WYUmzgioKEc/Nck9e5kAsV61lW2RiuCH/50nyRVMpcU znIT/4zIf/62xcCXme/FgoGoeQskQG4fvKXX79W9trkq69ZwsdfINM7xfySEoWrzmV z+dhzvqVLAGAGt6UV2HfoRBlb9oNXl+ocV/hT5MoXiN33gD9bgnzIeRrJjPQ2+dtQx qGH+rfFK4oaBpurYC0XVJDase4EAC8qoDeegclHz0svqH6+rftsXciktdh5Vn6vXCf bTWzAWA/P6V9A== Date: Sat, 9 Mar 2024 20:37:51 -0500 From: Guo Ren To: Andy Chiu Cc: Puranjay Mohan , =?iso-8859-1?Q?Bj=F6rn_T=F6pel?= , Paul Walmsley , Palmer Dabbelt , Albert Ou , Steven Rostedt , Masami Hiramatsu , Mark Rutland , Sami Tolvanen , Ley Foon Tan , Deepak Gupta , Sia Jee Heng , =?iso-8859-1?Q?Bj=F6rn_T=F6pel?= , Song Shuai , =?iso-8859-1?Q?Cl=E9ment_L=E9ger?= , Al Viro , Jisheng Zhang , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: Re: [RFC PATCH] riscv: Implement HAVE_DYNAMIC_FTRACE_WITH_CALL_OPS Message-ID: References: <20240306165904.108141-1-puranjay12@gmail.com> <87ttlhdeqb.fsf@all.your.base.are.belong.to.us> 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-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Fri, Mar 08, 2024 at 05:18:21PM +0800, Andy Chiu wrote: > Hi Puranjay, > > On Fri, Mar 8, 2024 at 3:53 AM Puranjay Mohan wrote: > > > > Hi Björn, > > > > On Thu, Mar 7, 2024 at 8:27 PM Björn Töpel wrote: > > > > > > Puranjay! > > > > > > Puranjay Mohan writes: > > > > > > > This patch enables support for DYNAMIC_FTRACE_WITH_CALL_OPS on RISC-V. > > > > This allows each ftrace callsite to provide an ftrace_ops to the common > > > > ftrace trampoline, allowing each callsite to invoke distinct tracer > > > > functions without the need to fall back to list processing or to > > > > allocate custom trampolines for each callsite. This significantly speeds > > > > up cases where multiple distinct trace functions are used and callsites > > > > are mostly traced by a single tracer. > > > > > > > > The idea and most of the implementation is taken from the ARM64's > > > > implementation of the same feature. The idea is to place a pointer to > > > > the ftrace_ops as a literal at a fixed offset from the function entry > > > > point, which can be recovered by the common ftrace trampoline. > > > > > > Not really a review, but some more background; Another rationale (on-top > > > of the improved per-call performance!) for CALL_OPS was to use it to > > > build ftrace direct call support (which BPF uses a lot!). Mark, please > > > correct me if I'm lying here! > > > > > > On Arm64, CALL_OPS makes it possible to implement direct calls, while > > > only patching one BL instruction -- nice! > > > > > > On RISC-V we cannot use use the same ideas as Arm64 straight off, > > > because the range of jal (compare to BL) is simply too short (+/-1M). > > > So, on RISC-V we need to use a full auipc/jal pair (the text patching > > > story is another chapter, but let's leave that aside for now). Since we > > > have to patch multiple instructions, the cmodx situation doesn't really > > > improve with CALL_OPS. > > > > > > Let's say that we continue building on your patch and implement direct > > > calls on CALL_OPS for RISC-V as well. > > > > > > From Florent's commit message for direct calls: > > > > > > | There are a few cases to distinguish: > > > | - If a direct call ops is the only one tracing a function: > > > | - If the direct called trampoline is within the reach of a BL > > > | instruction > > > | -> the ftrace patchsite jumps to the trampoline > > > | - Else > > > | -> the ftrace patchsite jumps to the ftrace_caller trampoline which > > > | reads the ops pointer in the patchsite and jumps to the direct > > > | call address stored in the ops > > > | - Else > > > | -> the ftrace patchsite jumps to the ftrace_caller trampoline and its > > > | ops literal points to ftrace_list_ops so it iterates over all > > > | registered ftrace ops, including the direct call ops and calls its > > > | call_direct_funcs handler which stores the direct called > > > | trampoline's address in the ftrace_regs and the ftrace_caller > > > | trampoline will return to that address instead of returning to the > > > | traced function > > > > > > On RISC-V, where auipc/jalr is used, the direct called trampoline would > > > always be reachable, and then first Else-clause would never be entered. > > > This means the the performance for direct calls would be the same as the > > > one we have today (i.e. no regression!). > > > > > > RISC-V does like x86 does (-ish) -- patch multiple instructions, long > > > reach. > > > > > > Arm64 uses CALL_OPS and patch one instruction BL. > > > > > > Now, with this background in mind, compared to what we have today, > > > CALL_OPS would give us (again assuming we're using it for direct calls): > > > > > > * Better performance for tracer per-call (faster ops lookup) GOOD > > > > ^ this was the only motivation for me to implement this patch. > > > > I don't think implementing direct calls over call ops is fruitful for > > RISC-V because once > > the auipc/jalr can be patched atomically, the direct call trampoline > > is always reachable. > > Yes, the auipc/jalr instruction pair can be patched atomically just as > long as their size is naturally aligned on. However, we cannot prevent > 2 instructions from being fetched atomically :P There are some micro-arch optimization methods here, such as: - Disable interrupt when auipc retired. - When auipc -> auipc, the second one still could cause an interruption. > > > Solving the atomic text patching problem would be fun!! I am eager to > > see how it will be > > solved. > > I have a patch series to solve the atomic code patching issue, which I > am about to respin [1]. The idea is to solve it with yet another layer > of indirection. We add a 8-B aligned space at each function entry. The > space is a pointer to the ftrace entry. During boot, each function > entry code is updated to perform a load and then take the jump from > the 8-B space. When ftrace is disabled, we patch the first 4B-aligned > instruction to a jump so as to skip the ftrace entry. > > We are still discussing with Alex to see if we have a better way to do > it. If not then I'd update the patchset and re-send it. There's a > pending improvement in the series to reduce complexity. The 8-B > aligned space can be added before the function entry (just like your > patch). > > > > > > * Larger text size (function alignment + extra nops) BAD > > > * Same direct call performance NEUTRAL > > > * Same complicated text patching required NEUTRAL > > > > > > It would be interesting to see how the per-call performance would > > > improve on x86 with CALL_OPS! ;-) > > > > If I remember from Steven's talk, x86 uses dynamically allocated trampolines > > for per callsite tracers, would CALL_OPS provide better performance than that? > > > > > > > > I'm trying to wrap my head if it makes sense to have it on RISC-V, given > > > that we're a bit different from Arm64. Does the scale tip to the GOOD > > > side? > > > > > > Oh, and we really need to see performance numbers on real HW! I have a > > > VF2 that I could try this series on. > > > > It would be great if you can do it :D. > > > > Thanks, > > Puranjay > > > > _______________________________________________ > > linux-riscv mailing list > > linux-riscv@lists.infradead.org > > http://lists.infradead.org/mailman/listinfo/linux-riscv > > - [1] https://yhbt.net/lore/all/CAJF2gTSn3_cDYsF9D8dt-br2Wf_M8y02A09xgRq8kXi91sN3Aw@mail.gmail.com/T/ > > Regards, > Andy > > _______________________________________________ > linux-riscv mailing list > linux-riscv@lists.infradead.org > http://lists.infradead.org/mailman/listinfo/linux-riscv