From nobody Sat Feb 7 20:58:02 2026 Received: from out-182.mta1.migadu.com (out-182.mta1.migadu.com [95.215.58.182]) (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 0F0FE30F7FD for ; Sat, 31 Jan 2026 14:50:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769871052; cv=none; b=P3z0vmXafXAX/NaTSvK8d6iVYEHtlOsAbaiEogAfRRm/qhB8s3XGBYjXLYW2qN9jmkq0Iu921zi68EmbjYo8QFfn9TYLRoMJiYKOZ14A2im68/tkIyxBVFQ338V8Vpfgf4EEyLzP8tZr5/GzIaLvQnRCutlRi2tTohGU30W8Tv0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769871052; c=relaxed/simple; bh=CwmBe6TWFR/q9XzSqvxcKoENIoluZ3iAcs0SQHXnNNw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZrgWqKsAjR2+jMnFNRhP/RoLOuabU/7RYVdPyFKR6SwPSkNB09HGxjWI8neaZy8N63NqtIiacUy/WwGwCflprpc5/Kmew6WbgYvv9xjKgyB1vETHR/Q+Snp7tCIioN/7+K/VZC+bam86Z65KGKMM+Z2Y6yhDmI1MH1kOo1tlgQY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=qSIKDIo2; arc=none smtp.client-ip=95.215.58.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="qSIKDIo2" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1769871049; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iF1W/fFiptW5YzYqA4hrXfI2kCVLhtS2I4vAaZcmEv0=; b=qSIKDIo2zYPgjZcK2IWaEGAziZ6UBqPWi3FWoAPDCeAh11ThhaLZvcYEgp04YeL8vHvx1V FXJsCBAQT3L8K51NWCHarO1sUtQwI5ZNt38B5rtcS8rrm7dR0n5Pcwh0zbTqCCFea1k8C5 jHMvOgrfh52wSfty9zqetPjs3/ItSqU= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Puranjay Mohan , Xu Kuohai , Catalin Marinas , Will Deacon , Shuah Khan , Menglong Dong , Leon Hwang , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-patches-bot@fb.com Subject: [PATCH bpf-next v5 2/3] bpf, arm64: Add fsession support Date: Sat, 31 Jan 2026 22:49:49 +0800 Message-ID: <20260131144950.16294-3-leon.hwang@linux.dev> In-Reply-To: <20260131144950.16294-1-leon.hwang@linux.dev> References: <20260131144950.16294-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Implement fsession support in the arm64 BPF JIT trampoline. Extend the trampoline stack layout to store function metadata and session cookies, and pass the appropriate metadata to fentry and fexit programs. This mirrors the existing x86 behavior and enables session cookies on arm64. Acked-by: Puranjay Mohan Tested-by: Puranjay Mohan Signed-off-by: Leon Hwang --- arch/arm64/net/bpf_jit_comp.c | 71 ++++++++++++++++++++++++++++++----- include/linux/bpf.h | 7 +++- 2 files changed, 68 insertions(+), 10 deletions(-) diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c index 0c4d44bcfbf4..2dc5037694ba 100644 --- a/arch/arm64/net/bpf_jit_comp.c +++ b/arch/arm64/net/bpf_jit_comp.c @@ -2510,6 +2510,12 @@ static bool is_struct_ops_tramp(const struct bpf_tra= mp_links *fentry_links) fentry_links->links[0]->link.type =3D=3D BPF_LINK_TYPE_STRUCT_OPS; } =20 +static void store_func_meta(struct jit_ctx *ctx, u64 func_meta, int func_m= eta_off) +{ + emit_a64_mov_i64(A64_R(10), func_meta, ctx); + emit(A64_STR64I(A64_R(10), A64_SP, func_meta_off), ctx); +} + /* Based on the x86's implementation of arch_prepare_bpf_trampoline(). * * bpf prog and function entry before bpf trampoline hooked: @@ -2533,7 +2539,7 @@ static int prepare_trampoline(struct jit_ctx *ctx, st= ruct bpf_tramp_image *im, int regs_off; int retval_off; int bargs_off; - int nfuncargs_off; + int func_meta_off; int ip_off; int run_ctx_off; int oargs_off; @@ -2544,6 +2550,9 @@ static int prepare_trampoline(struct jit_ctx *ctx, st= ruct bpf_tramp_image *im, bool save_ret; __le32 **branches =3D NULL; bool is_struct_ops =3D is_struct_ops_tramp(fentry); + int cookie_off, cookie_cnt, cookie_bargs_off; + int fsession_cnt =3D bpf_fsession_cnt(tlinks); + u64 func_meta; =20 /* trampoline stack layout: * [ parent ip ] @@ -2562,10 +2571,14 @@ static int prepare_trampoline(struct jit_ctx *ctx, = struct bpf_tramp_image *im, * [ ... ] * SP + bargs_off [ arg reg 1 ] for bpf * - * SP + nfuncargs_off [ arg regs count ] + * SP + func_meta_off [ regs count, etc ] * * SP + ip_off [ traced function ] BPF_TRAMP_F_IP_ARG flag * + * [ stack cookie N ] + * [ ... ] + * SP + cookie_off [ stack cookie 1 ] + * * SP + run_ctx_off [ bpf_tramp_run_ctx ] * * [ stack arg N ] @@ -2582,13 +2595,18 @@ static int prepare_trampoline(struct jit_ctx *ctx, = struct bpf_tramp_image *im, /* room for bpf_tramp_run_ctx */ stack_size +=3D round_up(sizeof(struct bpf_tramp_run_ctx), 8); =20 + cookie_off =3D stack_size; + /* room for session cookies */ + cookie_cnt =3D bpf_fsession_cookie_cnt(tlinks); + stack_size +=3D cookie_cnt * 8; + ip_off =3D stack_size; /* room for IP address argument */ if (flags & BPF_TRAMP_F_IP_ARG) stack_size +=3D 8; =20 - nfuncargs_off =3D stack_size; - /* room for args count */ + func_meta_off =3D stack_size; + /* room for function metadata, such as regs count */ stack_size +=3D 8; =20 bargs_off =3D stack_size; @@ -2646,9 +2664,9 @@ static int prepare_trampoline(struct jit_ctx *ctx, st= ruct bpf_tramp_image *im, emit(A64_STR64I(A64_R(10), A64_SP, ip_off), ctx); } =20 - /* save arg regs count*/ - emit(A64_MOVZ(1, A64_R(10), nfuncargs, 0), ctx); - emit(A64_STR64I(A64_R(10), A64_SP, nfuncargs_off), ctx); + /* save function metadata */ + func_meta =3D nfuncargs; + store_func_meta(ctx, func_meta, func_meta_off); =20 /* save args for bpf */ save_args(ctx, bargs_off, oargs_off, m, a, false); @@ -2666,10 +2684,27 @@ static int prepare_trampoline(struct jit_ctx *ctx, = struct bpf_tramp_image *im, emit_call((const u64)__bpf_tramp_enter, ctx); } =20 - for (i =3D 0; i < fentry->nr_links; i++) + if (fsession_cnt) { + /* clear all the session cookies' value */ + emit(A64_MOVZ(1, A64_R(10), 0, 0), ctx); + for (int i =3D 0; i < cookie_cnt; i++) + emit(A64_STR64I(A64_R(10), A64_SP, cookie_off + 8 * i), ctx); + /* clear the return value to make sure fentry always gets 0 */ + emit(A64_STR64I(A64_R(10), A64_SP, retval_off), ctx); + } + + cookie_bargs_off =3D (bargs_off - cookie_off) / 8; + for (i =3D 0; i < fentry->nr_links; i++) { + if (bpf_prog_calls_session_cookie(fentry->links[i])) { + u64 meta =3D func_meta | (cookie_bargs_off << BPF_TRAMP_COOKIE_INDEX_SH= IFT); + + store_func_meta(ctx, meta, func_meta_off); + cookie_bargs_off--; + } invoke_bpf_prog(ctx, fentry->links[i], bargs_off, retval_off, run_ctx_off, flags & BPF_TRAMP_F_RET_FENTRY_RET); + } =20 if (fmod_ret->nr_links) { branches =3D kcalloc(fmod_ret->nr_links, sizeof(__le32 *), @@ -2701,9 +2736,22 @@ static int prepare_trampoline(struct jit_ctx *ctx, s= truct bpf_tramp_image *im, *branches[i] =3D cpu_to_le32(A64_CBNZ(1, A64_R(10), offset)); } =20 - for (i =3D 0; i < fexit->nr_links; i++) + /* set the "is_return" flag for fsession */ + func_meta |=3D (1ULL << BPF_TRAMP_IS_RETURN_SHIFT); + if (fsession_cnt) + store_func_meta(ctx, func_meta, func_meta_off); + + cookie_bargs_off =3D (bargs_off - cookie_off) / 8; + for (i =3D 0; i < fexit->nr_links; i++) { + if (bpf_prog_calls_session_cookie(fexit->links[i])) { + u64 meta =3D func_meta | (cookie_bargs_off << BPF_TRAMP_COOKIE_INDEX_SH= IFT); + + store_func_meta(ctx, meta, func_meta_off); + cookie_bargs_off--; + } invoke_bpf_prog(ctx, fexit->links[i], bargs_off, retval_off, run_ctx_off, false); + } =20 if (flags & BPF_TRAMP_F_CALL_ORIG) { im->ip_epilogue =3D ctx->ro_image + ctx->idx; @@ -2753,6 +2801,11 @@ static int prepare_trampoline(struct jit_ctx *ctx, s= truct bpf_tramp_image *im, return ctx->idx; } =20 +bool bpf_jit_supports_fsession(void) +{ + return true; +} + int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags, struct bpf_tramp_links *tlinks, void *func_addr) { diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 3b0ceb759075..cd9b96434904 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2196,13 +2196,18 @@ static inline int bpf_fsession_cnt(struct bpf_tramp= _links *links) return cnt; } =20 +static inline bool bpf_prog_calls_session_cookie(struct bpf_tramp_link *li= nk) +{ + return link->link.prog->call_session_cookie; +} + static inline int bpf_fsession_cookie_cnt(struct bpf_tramp_links *links) { struct bpf_tramp_links fentries =3D links[BPF_TRAMP_FENTRY]; int cnt =3D 0; =20 for (int i =3D 0; i < links[BPF_TRAMP_FENTRY].nr_links; i++) { - if (fentries.links[i]->link.prog->call_session_cookie) + if (bpf_prog_calls_session_cookie(fentries.links[i])) cnt++; } =20 --=20 2.52.0