From nobody Mon Feb 9 14:38:29 2026 Received: from mail-pf1-f193.google.com (mail-pf1-f193.google.com [209.85.210.193]) (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 A3BBD3446B9 for ; Sat, 10 Jan 2026 14:12:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.193 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768054366; cv=none; b=CSXqPf5cbB5SUAbQTpzTzmQCz/4jBVReKmScL8mO7Qr0s+rXIAybhhq1fspdxCNp0JZDCgCw4+nlwhl7bQoISAlXMau3rV+GPID990EzPZ4SEwIRUye9OrgXpbfA9SwgbooTGZJqxxxNno/G6+FfjhYMQ8bbZ/oH8/NqGyJDhfI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768054366; c=relaxed/simple; bh=UKWXQ8lrzEPWgxemDQ6yw1XM1w6GFT2HGNkxpdD4QCQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cZgbSO0+qw/i686ys/FobdZAGtcilM73hfl5ds7xEBdFvowaNvSi+6rwLMDz9UV46eeSoa5HeWoa54sP5XXBoJgGG1KhjQUI5wLP1CIlIAfxsP1fNjG7tAtA3xhTFmuCJkJJdfzrcTbbcF94FoWoN31f3liREVevQVOKFHHXO3E= 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=gr3NB1Ou; arc=none smtp.client-ip=209.85.210.193 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="gr3NB1Ou" Received: by mail-pf1-f193.google.com with SMTP id d2e1a72fcca58-81e8b1bdf0cso662488b3a.3 for ; Sat, 10 Jan 2026 06:12:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768054364; x=1768659164; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=cuDJq9OcqlXOoYELFFiMxpUFbHiZNG+R5rDqqX2qwUQ=; b=gr3NB1OuUqE8dhGgStPxbNpGmJCzdiGVIJH0J0Ai2da/zqHsC6FhVyDeExcn04poYh wteR8fNdsrrD1fvOtPdNNeqSlCibNDmv7WfiMhAh1GyI4c4n147jsGiBW2s9txPrwQg8 MB1CNUXRhmEHx5Y+YuOnXNjFoCDD/dphHABmucrk0O8APrjuX7MdTOiMDesPoK1CJF1R NumXRbBQ4RffGmUY41CrWnK8x08AnopFAB/w/LEg3dqtCyknD29V2MdkS0cy71w2sHo1 1GSp2PvMFTuVZ5bx4Vx8iNDO3i/ZJujyQ4m8lxs3Z//26ZVGGy4humW9or8toFg/ZXk2 Idug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768054364; x=1768659164; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=cuDJq9OcqlXOoYELFFiMxpUFbHiZNG+R5rDqqX2qwUQ=; b=ii/CKrtP82UnUgHU92oyLt4N5Em0NDzkYqYlvTm4RDtW2Y2dAwPFp+BSjkPszrcSUA EHIS+31xqrtBbqBEXjxTx+VhQGm6u174ELyeSGXyalcrdJgEIHgIdc2PcrwYO3csga6X XmakLhCWgAsir3n5fJiso7TGwVLGtbcAIm86pw5RyT2BRehJBxCNXP/kfQyuVZFBX629 QEihrQSWdbDkgmTmHM2noL1cjK//JxVuj3Uza9Jam1Je7Xjozszc1TZnqrQ3TLpVCuGl b3AFhPJ6idmGi4YOk0iD/zv2g4QD0/OWGEnF0GxzdsYxPa8APuqto4tyBS2E1LF3+UvH bEsQ== X-Forwarded-Encrypted: i=1; AJvYcCX1GID89ULobkA651ypQeVX2QIAh8kmbkzTkMgmHdJKogQq2NpsTF9+xJgSdkcxBMVi9BLk7e0id/ZNUy8=@vger.kernel.org X-Gm-Message-State: AOJu0Yz56balXxFto56H1JsJXt91rVC3l/hRKKMpE18rLrypq5Bsolrl dCbOySJ3r5vNJ76JRamyF9yQRziKC4f81PmMqgtsgy2/Ml9K7Uo4D8QA X-Gm-Gg: AY/fxX6trQvpM4Gs1grpwJmXr0skOb6Ir7NlzxdNB8DxlJyNssR3EA7QK8YnGtRTBHc 6uWa5Z+okuxC8Rpewupb+OXQqBdjQm7VqvdO6fld1KSH0GzYzbVThGLyc35TGWd/ZNEMUh4Vpcr /QI38WqkcuSWPtBVKUVIVkm3KF3dbpblw1KsDdF6T3VrG430z6cgCEWrSSw2enU9p7RTh3kUDyO LM7PBNUcS1hth4KaEtqTmo7MkwJ5ZKwOdkFrhs5wu4Vew7FxSP4rI+mBga1ViFriMjbZo77Th/t ZfMJQFhWp0/0rWHeLDE7XPGA3+gn7i/z8xk2czlhtQPDdfA/NqHmcMOEJqxW3ts33ER/DYkgAGP 3xkKVcDXTSJnNqasYxoll1lu2CJREMINg+2504f7t1cyAONlukDPi1OjKFSa641yDEKj0nl9KSr BxzCdJaoU= X-Google-Smtp-Source: AGHT+IFLWfpiux/Kh9OQtDcuqehT+WANU8TvRohmEzGWI5MjMm7PzROsSMC/B8nDbc23kmcVsN6PRw== X-Received: by 2002:a05:6a00:ad8a:b0:7e8:4587:e8c5 with SMTP id d2e1a72fcca58-81b80cb141cmr11319417b3a.56.1768054364182; Sat, 10 Jan 2026 06:12:44 -0800 (PST) Received: from 7950hx ([160.187.0.149]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-81f42658f03sm1481079b3a.20.2026.01.10.06.12.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 10 Jan 2026 06:12:43 -0800 (PST) From: Menglong Dong X-Google-Original-From: Menglong Dong To: ast@kernel.org, andrii@kernel.org Cc: daniel@iogearbox.net, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, davem@davemloft.net, dsahern@kernel.org, tglx@linutronix.de, mingo@redhat.com, jiang.biao@linux.dev, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, bpf@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH bpf-next v9 07/11] bpf,x86: add fsession support for x86_64 Date: Sat, 10 Jan 2026 22:11:11 +0800 Message-ID: <20260110141115.537055-8-dongml2@chinatelecom.cn> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260110141115.537055-1-dongml2@chinatelecom.cn> References: <20260110141115.537055-1-dongml2@chinatelecom.cn> 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 Content-Type: text/plain; charset="utf-8" Add BPF_TRACE_FSESSION supporting to x86_64, including: 1. clear the return value in the stack before fentry to make the fentry of the fsession can only get 0 with bpf_get_func_ret(). 2. clear all the session cookies' value in the stack. 2. store the index of the cookie to ctx[-1] before the calling to fsession 3. store the "is_return" flag to ctx[-1] before the calling to fexit of the fsession. Signed-off-by: Menglong Dong Co-developed-by: Leon Hwang Signed-off-by: Leon Hwang --- v5: - add the variable "func_meta" - define cookie_off in a new line v4: - some adjustment to the 1st patch, such as we get the fsession prog from fentry and fexit hlist - remove the supporting of skipping fexit with fentry return non-zero v2: - add session cookie support - add the session stuff after return value, instead of before nr_args --- arch/x86/net/bpf_jit_comp.c | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index d94f7038c441..0671a434c00d 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -3094,12 +3094,17 @@ static int emit_cond_near_jump(u8 **pprog, void *fu= nc, void *ip, u8 jmp_cond) static int invoke_bpf(const struct btf_func_model *m, u8 **pprog, struct bpf_tramp_links *tl, int stack_size, int run_ctx_off, bool save_ret, - void *image, void *rw_image) + void *image, void *rw_image, u64 func_meta) { int i; u8 *prog =3D *pprog; =20 for (i =3D 0; i < tl->nr_links; i++) { + if (tl->links[i]->link.prog->call_session_cookie) { + /* 'stack_size + 8' is the offset of func_md in stack */ + emit_store_stack_imm64(&prog, stack_size + 8, func_meta); + func_meta -=3D (1 << BPF_TRAMP_M_COOKIE); + } if (invoke_bpf_prog(m, &prog, tl->links[i], stack_size, run_ctx_off, save_ret, image, rw_image)) return -EINVAL; @@ -3222,7 +3227,9 @@ static int __arch_prepare_bpf_trampoline(struct bpf_t= ramp_image *im, void *rw_im struct bpf_tramp_links *fexit =3D &tlinks[BPF_TRAMP_FEXIT]; struct bpf_tramp_links *fmod_ret =3D &tlinks[BPF_TRAMP_MODIFY_RETURN]; void *orig_call =3D func_addr; + int cookie_off, cookie_cnt; u8 **branches =3D NULL; + u64 func_meta; u8 *prog; bool save_ret; =20 @@ -3290,6 +3297,11 @@ static int __arch_prepare_bpf_trampoline(struct bpf_= tramp_image *im, void *rw_im =20 ip_off =3D stack_size; =20 + cookie_cnt =3D bpf_fsession_cookie_cnt(tlinks); + /* room for session cookies */ + stack_size +=3D cookie_cnt * 8; + cookie_off =3D stack_size; + stack_size +=3D 8; rbx_off =3D stack_size; =20 @@ -3383,9 +3395,19 @@ static int __arch_prepare_bpf_trampoline(struct bpf_= tramp_image *im, void *rw_im } } =20 + if (bpf_fsession_cnt(tlinks)) { + /* clear all the session cookies' value */ + for (int i =3D 0; i < cookie_cnt; i++) + emit_store_stack_imm64(&prog, cookie_off - 8 * i, 0); + /* clear the return value to make sure fentry always get 0 */ + emit_store_stack_imm64(&prog, 8, 0); + } + func_meta =3D nr_regs + (((cookie_off - regs_off) / 8) << BPF_TRAMP_M_COO= KIE); + if (fentry->nr_links) { if (invoke_bpf(m, &prog, fentry, regs_off, run_ctx_off, - flags & BPF_TRAMP_F_RET_FENTRY_RET, image, rw_image)) + flags & BPF_TRAMP_F_RET_FENTRY_RET, image, rw_image, + func_meta)) return -EINVAL; } =20 @@ -3445,9 +3467,14 @@ static int __arch_prepare_bpf_trampoline(struct bpf_= tramp_image *im, void *rw_im } } =20 + /* set the "is_return" flag for fsession */ + func_meta +=3D (1 << BPF_TRAMP_M_IS_RETURN); + if (bpf_fsession_cnt(tlinks)) + emit_store_stack_imm64(&prog, nregs_off, func_meta); + if (fexit->nr_links) { if (invoke_bpf(m, &prog, fexit, regs_off, run_ctx_off, - false, image, rw_image)) { + false, image, rw_image, func_meta)) { ret =3D -EINVAL; goto cleanup; } --=20 2.52.0