From nobody Tue Feb 10 04:12:35 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 D2E4126E719 for ; Sat, 24 Jan 2026 06:21:06 +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=1769235668; cv=none; b=ccIXE4ydGz0nZJZ6AOl7hzAk4ilde2WvDBMgdil31/URpSwf0Mic3fPn31YlVqyuOJrBmR0tligCiKufiY2FbZ+GpOOF9a7/R1ekjxpxR1aMTqoTZj7+dSqMMS5IVUVlJM6V4GM7q6kYlhP4g6Rv70kASfVJ1FE07dJ+hw4Eqtk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769235668; c=relaxed/simple; bh=kjcr3U+Eej2jkvLdErMnd6YR7FAn98KttpJB/4PZM7c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SOfhOFXXknJOj3F6SVazy3+qpVR7atQIC3u8WATlZL/vHTXZRC4L9qm4sa6Mxf84zufNk0E/w7MbAFlFFcTjzS3tpjSWHfjjrOSa7vorwieuLnRtQXqcgiXuSa2/w9Zjr2EzEMi9I1XtNVXNJYEwoPlH7g9zr4heAeGXBUbeFRM= 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=YUIsKtpn; 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="YUIsKtpn" Received: by mail-pl1-f196.google.com with SMTP id d9443c01a7336-2a7a9b8ed69so32665145ad.2 for ; Fri, 23 Jan 2026 22:21:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769235666; x=1769840466; 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=YUIsKtpnltVxgATzRFvFwDTjGamy3m4CrBUo03J/TfZ5HtM1RVIAJuvOtK4JOtldyF i2J5erBEAnJmrBqftfteGjqM2lu0Kyu/V/r2wD0cRPn9qPC3s1bvioivDd0NKYq7o5oS jAB1mzh+w783RDSIzgQydnFBQ9GmbkkcbSk6H3K8zpV7q9F5TFHRqjHoCLSKenG/hg+9 k2h27c91rSjx5tOXVjTj19ChIaqclBTKWcGycH56ouLZtiXMqkpHONsdFKkQEV7dbUx4 O4HZMhmepH8Eodvl39m6qOh40OV+NlmOX6oAnv6mmvlbDw/JFlIJ9AE/PiZA/B5OaM6p qUXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769235666; x=1769840466; 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=svplZWDOT9IGHP5WoMhKxYrIzyvIvpnI3hLlDlL9LJ+PztmoL/XP3WDi8PqUd3kGbG vKXppb4qkZ5LHls67sRXdlgxWyolnKHvHi7nGNIazoZnaxxTFnQkVeIvjrGFaqbKoPgu 4gvghK7ltReQJamegNfelZ9zGINDrbONYkXIIGxFO1U9WQXHvsmwtNdztir71f5a0Wd+ 2uMETUi4RFuQBkaoyxDdgVDewRUxf9ywcN+3cCIU+AfWud/rlw9ZmczieugcVQwQ4vLc 5HxZzYf/KSOHFbw2T0qPPReLqD7HFb0skXduFnLW7u1sGnzbrwMhyPAeFq7qhyTKmxaA Iz/A== X-Forwarded-Encrypted: i=1; AJvYcCXSPTEpOjaJpUqmsaKQFbtT2U7j+WCWxrfVk+80q4TWIDyHdc2KB/X+r1iAPna7nNVO4sgr7nVbdJZkJSo=@vger.kernel.org X-Gm-Message-State: AOJu0YxVrl9q8gKE2lg3Gbd6SrNFFB4Hn+Fpoo8VjT3YLjlWTpPrH6IM DyUCUaBsJ6mMV9uDF08DR4JGhVoBAhGrVL25RlVuSKULZNFusz7mqlIy X-Gm-Gg: AZuq6aIM5+Vd/4ccfFsc5f808X/s0HxUpC13dNs99fGPHWlpBmTlIEn2mrSy5JOdzFF gNeT1Fz2VYRQiX3K3tp2nsC1S3OenBoHPudh4/0y9nWiFNqeFplpwAaxjweUq+d3C/W1RDGEI1a aJQhVpPfERHjo3vBrlqNoBocfDs+4ufJX9eXByEl5A/PFVGJoSwXqW0NuVtiGJqlvcZyLkPEOs8 G05u5p/N3qff9TAUxOeoQFunpPbPS5/rBm/bwYBPnHIv1+tatfJ9bCGEswNxb3BSodh2NQsgAAa yaAeaGBmW+sq6+bC2jz+q4c/XF3R1ieU85jnRDi1phxkLvkjQG4HpF3Y2P9zXN4jc0OO652Axg+ wceR5I8mvX0AGjhJt3VYUsk656S9Tnbiuzsj229n5b9itZjPomuRji5FMHGQ74oxEf4CKvl/9BW DY2PktdspI X-Received: by 2002:a17:902:f54f:b0:2a7:3daa:5822 with SMTP id d9443c01a7336-2a7fe77dd97mr45112005ad.60.1769235666125; Fri, 23 Jan 2026 22:21:06 -0800 (PST) Received: from 7950hx ([103.173.155.241]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-353354a6b5asm6496537a91.9.2026.01.23.22.20.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jan 2026 22:21:05 -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 v13 05/13] bpf: support fsession for bpf_session_cookie Date: Sat, 24 Jan 2026 14:20:00 +0800 Message-ID: <20260124062008.8657-6-dongml2@chinatelecom.cn> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260124062008.8657-1-dongml2@chinatelecom.cn> References: <20260124062008.8657-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