From nobody Sat Feb 7 08:58:22 2026 Received: from mail-pl1-f196.google.com (mail-pl1-f196.google.com [209.85.214.196]) (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 74FA914EC73 for ; Sat, 24 Jan 2026 03:32:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.196 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769225538; cv=none; b=QPGJrJExr5Y87+2WZyMSrOvJkPYTXUG+tqXTxawQyqvqDbI6f3WLUj9Scdts7BWGFlPGhWA7Xe48YXfywPjOzhnBg/9/6VCa3dSR9W2RVb7AEPmGaah8F7E38bv5N9aHzNQPfSeRVdSCkxmGNbyN75TywKoapH5cWB11YJTnD2k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769225538; c=relaxed/simple; bh=kjcr3U+Eej2jkvLdErMnd6YR7FAn98KttpJB/4PZM7c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LJgtNsLjxevI+DwuLrBWU+3D2YN2JwQKsWA+22q68139nTgQAamPFkZTPk9q7iWc9wm5kWFyNZNtP1V/1q0aKPVK5Gjh3c/ZCEOxyZAdpvstRVsgcfxQWWdYeSIKVKaiQqgZzl7ENXAcFXqRCeLJV2LN1Q6cRVT1oqlTcbeZOis= 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=eS6IdWkj; arc=none smtp.client-ip=209.85.214.196 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="eS6IdWkj" Received: by mail-pl1-f196.google.com with SMTP id d9443c01a7336-2a743050256so19515855ad.3 for ; Fri, 23 Jan 2026 19:32:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769225537; x=1769830337; 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=LRFPc/7UJMwRjQVl87T1d/8z+6sYjAGIhOZdpUb7X2k=; b=eS6IdWkjCUdqG9J8pG3uEMgxQGAr9KOVpwPoKUHj3GbIh90zP22hIvD9YWdiCsnzPs wwY8fwoG7Nxhjs6dGwGX9lbqdO/BYvac69UB5pEyq3OYo58R97ZPfOlcLCHPbPMAk+UQ qrQRaoH/ZnfGtgrFsrLT7tUGX+F99dfj+vHVBCL3qmHfSP36c5rsP/3YZ8rmtAqfjdX+ IIg8JtbMMYpgfFYq2sKb//pOECAevwd/pdMsaR7gslVsa7VqOxjk0U/0Fq/vyTrJV7lf 74vJ9cLYvKzUE2A59yVE/r7CllnrkmCOEM4/awtYKEaCGFgjtDxyAwAzign7SSkyRFdV 9HQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769225537; x=1769830337; 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=LRFPc/7UJMwRjQVl87T1d/8z+6sYjAGIhOZdpUb7X2k=; b=wkvZDkZC9f75YtkwTV5xgeWNy6D3eOGdamo2JFxnZyiLe0xcRc1w2RQBsQIShOTHnL W3RCgJIoWZV6aD5rHehvK5T6nIPN+e9aDGrfInCU3p/JZqcAZCppdtSKocFR4cJPGALK qeE+UZd5iPJAzk4NXRdgsTbF3fe3CBU2GjUPj7HzW/NB4ZuP2FyFGBxI6JzDUmD38Hjy v6QhiyLlobzFzvCaJ51DIgNeJPk/B0p7g+qhQlHi2rcUQm+E0ogQUgE+ir7a4j7MFfYZ W1Sv72sQYG09KXXPWmdL8RLSNafwOzH9daqW6emD1dIFr+lxou74AJBBGu638SscY2sm C4Ng== X-Forwarded-Encrypted: i=1; AJvYcCVrngf4eBz3xos65ghZv4YknfojgAQHLbjxM9aDxfzEJGBpVvfPW/o3Xs+hCAn+ivdlIj/4FwAUjYe8oWM=@vger.kernel.org X-Gm-Message-State: AOJu0YzXy+T6dxnioCo2pKxfI/jL4edXy+DbW2121b/rA49CcTR63i8W foB9cOvR6QJBG00ESS4Ea4eF7HgtNUY2MDcUySWqlxB0tiCgT3jIGnoy X-Gm-Gg: AZuq6aItnBTbmcsGJOk3FxvwftVxLEIBZiT6lj2dyoM6+xNLnGcne5rOVXVKdnlGcBH m1H9Xnritiz5N8YzegF0Ed6Ha9h4NpYZz+4K4sFeK81aYpOlkS8S+5JA88W9KlNbHQm8nGB2ShQ YSrivmfGn8vIi3lod2LzLyZ9uhVYDPeJG4Yzo3cY4lLpF5/nj26rdaWm1MD/JiJu2wBJHJyoBrx rKfGlp6VkUa4Oac+dmhmLGpyO1uHnfMKBdXZP4Y2swlmCsf2DQJNWlg+sWa1hxhPJyHZuyu+Y7o wFcOlUPBdGC7roQdRrrmGWZ94aRkjHK1+TfQB0dsSgurkZexbNW4r3uhT7KPldojPxFMOfA7n0T o6nYCBN307UxX1pyJmqRnoddKDZKqtr0yKjTUau63r98Sn4Vy++p9w5E7N6B2loArTOW6JBBhi4 D6MyUzQGEL X-Received: by 2002:a17:902:d2ce:b0:29f:2b84:652b with SMTP id d9443c01a7336-2a7fe625ffemr60090195ad.29.1769225536593; Fri, 23 Jan 2026 19:32:16 -0800 (PST) Received: from 7950hx ([103.173.155.241]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2a802dcddb0sm32539805ad.38.2026.01.23.19.32.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jan 2026 19:32:16 -0800 (PST) From: Menglong Dong X-Google-Original-From: Menglong Dong To: andrii@kernel.org Cc: ast@kernel.org, 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 v12 05/13] bpf: support fsession for bpf_session_cookie Date: Sat, 24 Jan 2026 11:31:11 +0800 Message-ID: <20260124033119.28682-6-dongml2@chinatelecom.cn> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260124033119.28682-1-dongml2@chinatelecom.cn> References: <20260124033119.28682-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" Implement session cookie for fsession. The session cookies will be stored in the stack, and the layout of the stack will look like this: return value -> 8 bytes argN -> 8 bytes ... arg1 -> 8 bytes nr_args -> 8 bytes ip (optional) -> 8 bytes cookie2 -> 8 bytes cookie1 -> 8 bytes The offset of the cookie for the current bpf program, which is in 8-byte units, is stored in the "(((u64 *)ctx)[-1] >> BPF_TRAMP_COOKIE_INDEX_SHIFT) & 0xFF". Therefore, we can get the session cookie with ((u64 *)ctx)[-offset]. Implement and inline the bpf_session_cookie() for the fsession in the verifier. Signed-off-by: Menglong Dong --- v11: - rename BPF_TRAMP_SHIFT_COOKIE to BPF_TRAMP_COOKIE_INDEX_SHIFT v10: - describe the offset of the session cookie more explicit - make 8 as the bit shift of session cookie - remove the session cookie count limitation v9: - remove the definition of bpf_fsession_cookie() v7: - reuse bpf_session_cookie() instead of introduce new kfunc v5: - remove "cookie_cnt" in struct bpf_trampoline v4: - limit the maximum of the cookie count to 4 - store the session cookies before nr_regs in stack --- include/linux/bpf.h | 15 +++++++++++++++ kernel/bpf/verifier.c | 20 ++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 29eecd79352e..4427c6e98331 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1229,6 +1229,7 @@ enum { #endif }; =20 +#define BPF_TRAMP_COOKIE_INDEX_SHIFT 8 #define BPF_TRAMP_IS_RETURN_SHIFT 63 =20 struct bpf_tramp_links { @@ -1782,6 +1783,7 @@ struct bpf_prog { enforce_expected_attach_type:1, /* Enforce expected_attach_type checki= ng at attach time */ call_get_stack:1, /* Do we call bpf_get_stack() or bpf_get_stackid() */ call_get_func_ip:1, /* Do we call get_func_ip() */ + call_session_cookie:1, /* Do we call bpf_session_cookie() */ tstamp_type_access:1, /* Accessed __sk_buff->tstamp_type */ sleepable:1; /* BPF program is sleepable */ enum bpf_prog_type type; /* Type of BPF program */ @@ -2190,6 +2192,19 @@ static inline int bpf_fsession_cnt(struct bpf_tramp_= links *links) return cnt; } =20 +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; + + for (int i =3D 0; i < links[BPF_TRAMP_FENTRY].nr_links; i++) { + if (fentries.links[i]->link.prog->call_session_cookie) + cnt++; + } + + return cnt; +} + int bpf_prog_ctx_arg_info_init(struct bpf_prog *prog, const struct bpf_ctx_arg_aux *info, u32 cnt); =20 diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index d04aea235a12..c2f2650db9fd 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -14406,6 +14406,9 @@ static int check_kfunc_call(struct bpf_verifier_env= *env, struct bpf_insn *insn, return err; } =20 + if (meta.func_id =3D=3D special_kfunc_list[KF_bpf_session_cookie]) + env->prog->call_session_cookie =3D true; + return 0; } =20 @@ -23024,6 +23027,23 @@ static int fixup_kfunc_call(struct bpf_verifier_en= v *env, struct bpf_insn *insn, insn_buf[1] =3D BPF_ALU64_IMM(BPF_RSH, BPF_REG_0, BPF_TRAMP_IS_RETURN_SH= IFT); insn_buf[2] =3D BPF_ALU64_IMM(BPF_AND, BPF_REG_0, 1); *cnt =3D 3; + } else if (desc->func_id =3D=3D special_kfunc_list[KF_bpf_session_cookie]= && + env->prog->expected_attach_type =3D=3D BPF_TRACE_FSESSION) { + /* + * inline bpf_session_cookie() for fsession: + * __u64 *bpf_session_cookie(void *ctx) + * { + * u64 off =3D (((u64 *)ctx)[-1] >> BPF_TRAMP_COOKIE_INDEX_SHIFT) = & 0xFF; + * return &((u64 *)ctx)[-off]; + * } + */ + insn_buf[0] =3D BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); + insn_buf[1] =3D BPF_ALU64_IMM(BPF_RSH, BPF_REG_0, BPF_TRAMP_COOKIE_INDEX= _SHIFT); + insn_buf[2] =3D BPF_ALU64_IMM(BPF_AND, BPF_REG_0, 0xFF); + insn_buf[3] =3D BPF_ALU64_IMM(BPF_LSH, BPF_REG_0, 3); + insn_buf[4] =3D BPF_ALU64_REG(BPF_SUB, BPF_REG_0, BPF_REG_1); + insn_buf[5] =3D BPF_ALU64_IMM(BPF_NEG, BPF_REG_0, 0); + *cnt =3D 6; } =20 if (env->insn_aux_data[insn_idx].arg_prog) { --=20 2.52.0