From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f44.google.com (mail-wr1-f44.google.com [209.85.221.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2CA9829C327 for ; Sun, 16 Aug 2026 20:44:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.44 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786913093; cv=none; b=uYiK2WKn+IxRGhQH1/IzX8G2pvOYMhVw7fCoY8rSyntu8aJdJrJKbEsDRCYGUYbot85u+ajqJ3rT1dyegfbQUIvnC+QTdwjmqnSvOznTl5w6aL21bE9MeTI+/jD3H4XCldA3DfFYEdr8mQ9AMsdp7QernhxRRAArBGelS58YGmg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786913093; c=relaxed/simple; bh=qfHa7Bz92IskvnpEI/0GIr4jnmqJsZ8MM3R91QgoPuY=; h=From:Date:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=cvHVqgrxeWdnhWfzPeXJThQSxvkO4EOuvF1uHrK0klZPoyKXT6FaSsysQDrBS+qmlAy4kBxXVyLTUe4X/k2zm0JJfvGL/GjRTsPutRjUMBp6UhsXkkafg098lWdcEOm7Uuc94wcyLUNDgQldvHPW1ZGu+bnm6v1G1Ko7wV0koLA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=l6C3am3f; arc=none smtp.client-ip=209.85.221.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="l6C3am3f" Received: by mail-wr1-f44.google.com with SMTP id ffacd0b85a97d-47fe2d179e2so1755424f8f.1 for ; Sun, 16 Aug 2026 13:44:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786913089; x=1787517889; darn=vger.kernel.org; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:date:from:from:to:cc:subject :date:message-id:reply-to:content-type; bh=CHWpGY/dpT3kUP2xxLApnQIlJZH8C24mnr9pYwyZJ1Q=; b=l6C3am3fD/f7k7Kuy+zZv9R+ya9KynhHRA/z5eKUp9a/+LKrTNb6jjq/JmKx1XTHYe 60A9QGUujQz4gEBrpevPYohsTT3T2rjsUUGE94EU/3/vY5PmYXKTHHsXwMAaHvwfiCUZ dI1hJNLVvM200HFSIJlmIOm3jbEYLpt2W4Z/THjS5vzVAqdNeHDNCithO0YHfZka+naw v2OKfx7DJVIRpt4X4zOA/62PEKGZo2Q6TRt42XDNxtbHuFn3K0x5r23rWPjPTACT6Zva V8jK8EMAMR2PkiVWem/c8kE3pA6dUKc5b3jSuxF0gJW1p5JoTxi/0p02h8OZkCSSYlEY CfrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786913089; x=1787517889; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=CHWpGY/dpT3kUP2xxLApnQIlJZH8C24mnr9pYwyZJ1Q=; b=LULTpKwKYGnej5v1Q2/tPh/oPHoiL8ki4enOY6Oynj7lgLI/S9G8+KUCGxVdxG/e2d E4c0JJIYOZG0iSBtYq+tsS5Jiq9QIg8wBdCUynoFYyb35ElKmimhA3rmNpY9Ry9BmRxP cdI3QamO5ClsHmHQ2+mWHupbOMdoQwnDym1eEw3BDQudppFGw8qASHv0uYCZDXeb3maU AfmqXxQv1baBs/PKTr11y3OggQqmtwaE0yk9hvzNf3AJnrwGbr1ZpL5DanpFIwLLkP6m 6JzoL0zweXxd27KZuzsBQXs76YKiyap6jXdB6BVrsR+FZ+4oHz+1RPhhVMnkgW2mE3C5 BfEw== X-Forwarded-Encrypted: i=1; AHgh+RqWEODeEpkg9V/vAJclx/3u41DTllFIRKIAUBYzqdffgG4OyJsuaySRIf0zrb1icShuvJVcdfFpmuU34WY=@vger.kernel.org X-Gm-Message-State: AOJu0YxsZq2On2p11oVnT7Z4fTuY08ZZEXpMNgTmchr03b8aunoY/H/0 BKtcIkz45ogtEXB7iZWRr+Kd0Gaer6Wj/AhRKqYbtYUfBTeln/x4OfNY X-Gm-Gg: AR+sD10YVUoWc4VIaxWSYWff478yiPnGsXYZGXm5TRI9IETdiUB7ZlgAd79H6JlcgbQ TxaDBTAW/u5ghiwXkN55EVICc1tBGx08p0oVfvsrgC9FPcz4L8Fb8HZm2UoYj+gbD9n3ZWhyiVg y3OaZtljYyKIb6HiphP/PDJSDxuTmCG+dh4x04uU3o/34AwLWHIOksow1DH5b4V0eYreqGsrAb6 9H05PasSyCxuZz9N7AdFQpgzWx0/VzqrdUK3tQHMXIdAAW1zGgXKPijAYoFIwqBSHeTWQkZQZN7 KSd53KMKqw4ZdTBzW4dpy66Jx9mtssxX4gXe/D8RrhZ3EeezCWZ9LfUx8d8Oj2wXhRYn4xAgU9b Pqgd7MX73YVGUuscun+Skx287dYzugDudRDcA9OpYTCjnjlDjxmaf1LpqU68fonrtr2H+KqNdnE bwJ1IP92KD6JklHb9alePaGdEf004Hlqc2sjTx7v4nt3ttqyE= X-Received: by 2002:a05:600c:564a:b0:498:ff3:71ed with SMTP id 5b1f17b1804b1-49987993affmr244683295e9.17.1786913089155; Sun, 16 Aug 2026 13:44:49 -0700 (PDT) Received: from krava ([176.74.159.170]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4815f21a31dsm28434597f8f.9.2026.08.16.13.44.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Aug 2026 13:44:48 -0700 (PDT) From: Jiri Olsa X-Google-Original-From: Jiri Olsa Date: Sun, 16 Aug 2026 22:44:46 +0200 To: Mike Rapoport Cc: Jiri Olsa , Alexei Starovoitov , Andrii Nakryiko , Andy Lutomirski , Borislav Petkov , Daniel Borkmann , Dave Hansen , Eduard Zingerman , Ingo Molnar , Kumar Kartikeya Dwivedi , Peter Zijlstra , Song Liu , Thomas Gleixner , Emil Tsalapatis , John Fastabend , Martin KaFai Lau , "H. Peter Anvin" , Yonghong Song , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org, Leon Hwang Subject: Re: [PATCH bpf-next v3 4/5] bpf, x86: make sure allocation in arch_bpf_trampoline_size() is writable Message-ID: References: <20260716-execmem-x86-rox-bpf-v0-v3-0-4e76158c01c5@kernel.org> <20260716-execmem-x86-rox-bpf-v0-v3-4-4e76158c01c5@kernel.org> 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-Disposition: inline In-Reply-To: On Fri, Aug 14, 2026 at 04:53:32PM +0200, Jiri Olsa wrote: > On Thu, Jul 16, 2026 at 10:51:38AM +0300, Mike Rapoport (Microsoft) wrote: > > arch_bpf_trampoline_size() allocates a buffer to get actual size required > > for a trampoline. > > > > This buffer must be in the module address space because > > __arch_prepare_bpf_trampoline() calculates rel32 offsets relatively to > > that buffer. > > > > In preparation for enabling ROX mode for EXECMEM_BPF make sure that the > > allocated memory is writable. > > > > Add bpf_jit_alloc_exec_rw() wrapper for execmem_alloc_rw() and use it for > buffer allocation in arch_bpf_trampoline_size(). > > > > Signed-off-by: Mike Rapoport (Microsoft) > > --- > > arch/x86/net/bpf_jit_comp.c | 5 ++--- > > include/linux/filter.h | 1 + > > kernel/bpf/core.c | 5 +++++ > > 3 files changed, 8 insertions(+), 3 deletions(-) > > > > diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c > > index de7515ea1bea..b2feec81e231 100644 > > --- a/arch/x86/net/bpf_jit_comp.c > > +++ b/arch/x86/net/bpf_jit_comp.c > > @@ -3703,13 +3703,12 @@ int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags, > > int ret; > > > > /* Allocate a temporary buffer for __arch_prepare_bpf_trampoline(). > > - * This will NOT cause fragmentation in direct map, as we do not > > - * call set_memory_*() on this buffer. > > * > > * We cannot use kvmalloc here, because we need image to be in > > * module memory range. > > + * Since it must be writable use bpf_jit_alloc_exec_rw(). > > */ > > - image = bpf_jit_alloc_exec(PAGE_SIZE); > > + image = bpf_jit_alloc_exec_rw(PAGE_SIZE); > > hi, > this change (this particular patch plus possibly others in this set) is > causing tracing_multi attachment bench slowdown > > the benchmark allocates huge number of trampolines and I'm seeing extra > arch_bpf_trampoline_size code paths in the attached perf profile > > I'm not that familiar with the allocator, but following hack makes the > benchmark ok again: > > diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c > index 6a94370a2448..bbff3c9c6681 100644 > --- a/kernel/bpf/core.c > +++ b/kernel/bpf/core.c > @@ -1130,7 +1130,7 @@ void *bpf_jit_alloc_exec(unsigned long size) > > void *bpf_jit_alloc_exec_rw(unsigned long size) > { > - return execmem_alloc_rw(EXECMEM_BPF, size); > + return execmem_alloc(EXECMEM_MODULE_DATA, size); > } > > void bpf_jit_free_exec(void *addr) > > > I still need to do more checks, but I'm wondering if we could actually fix > this by not allocating image data in arch_bpf_trampoline_size at all.. > and just teach __arch_prepare_bpf_trampoline to survive NULL image data > and just return the size in such case fyi doing ^^^ is not so straigh forward as I hoped for, but if we just allocate dummy page at init and used that in arch_bpf_trampoline_size we get some speedup I'll rebase that on top of your fix and send it out later with your fix: serial_test_tracing_multi_bench_attach: found 55077 functions serial_test_tracing_multi_bench_attach: attached in 1.470s serial_test_tracing_multi_bench_attach: detached in 0.249s with change below: serial_test_tracing_multi_bench_attach: found 55079 functions serial_test_tracing_multi_bench_attach: attached in 0.754s serial_test_tracing_multi_bench_attach: detached in 0.247s thanks, jirka --- diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index 1a9fb530adc3..3e8ba33944c9 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -23,6 +24,19 @@ static bool all_callee_regs_used[4] = {true, true, true, true}; +/* + * Reuse a writable image in the BPF execmem range for size calculation. + * Its contents do not affect size calculation. + */ +static void *trampoline_size_image; + +static int __init init_trampoline_size_image(void) +{ + trampoline_size_image = bpf_jit_alloc_exec_rw(PAGE_SIZE); + return trampoline_size_image ? 0 : -ENOMEM; +} +late_initcall(init_trampoline_size_image); + static u8 *emit_code(u8 *ptr, u32 bytes, unsigned int len) { if (len == 1) @@ -3811,23 +3825,11 @@ int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags, struct bpf_tramp_nodes *tnodes, void *func_addr) { struct bpf_tramp_image im; - void *image; - int ret; - - /* Allocate a temporary buffer for __arch_prepare_bpf_trampoline(). - * - * We cannot use kvmalloc here, because we need image to be in - * module memory range. - * Since it must be writable use bpf_jit_alloc_exec_rw(). - */ - image = bpf_jit_alloc_exec_rw(PAGE_SIZE); - if (!image) - return -ENOMEM; - ret = __arch_prepare_bpf_trampoline(&im, image, image + PAGE_SIZE, image, - m, flags, tnodes, func_addr); - bpf_jit_free_exec(image); - return ret; + return __arch_prepare_bpf_trampoline(&im, trampoline_size_image, + trampoline_size_image + PAGE_SIZE, + trampoline_size_image, m, flags, + tnodes, func_addr); } static int emit_bpf_dispatcher(u8 **pprog, int a, int b, s64 *progs, u8 *image, u8 *buf)