From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f195.google.com (mail-pl1-f195.google.com [209.85.214.195]) (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 0567114EC73 for ; Sat, 24 Jan 2026 06:21:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.195 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769235668; cv=none; b=u73HR9pNr/ykoeM2NJFB94vbe+JZnYksntiBq5iNbgiRLRXfy+2i+JOuhKh+m9tDtKlemJnr4pZDXnrLkGnRe2hCAtd7ln6xHh6MMHY+v+nPw6cf4egpSXsoC7LN+0ZX0BugszWXWUdt7VMtpnYx9saas4url9z0PvNDqqvRGok= 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.195 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-f195.google.com with SMTP id d9443c01a7336-2a7a9b8ed69so32665135ad.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=scbAyTpHD4URcL+xJtcuJU2mH5/gIIMnT41QtPmAOOf54A4EqE5WAcashuQZxLcnKE G+byGcbowpZt3RC77VOGRZ2zS9GagylA1GQZKWLRjJ9cqMdSqV5MFEziQnAIaogLXNNu JpYemX8xEp20kTsLsG1Ii28b78l5RoxKRfK/tnYABTGL0FZHgXDgpiuEI1k6woCGYoRO Owhq2KKgpNAniUKLJPjgnI+OJXHyo7qGsC/0acBrHYev11omj7El0INk0ihGBSjltdW+ t4lDbO9ZdiS7MdAWO6p7pQUSCiV6W/rhjlcx3rGQriY/BJ8WjVesR5CSF3WRdA6is+aQ 8nKQ== X-Forwarded-Encrypted: i=1; AJvYcCXVtbKlPDLGlZVifFfWGsHT4nbCUzhNbX69neMk386q61P1GQdos65MxD9eewrTs3DNuTAVdmE=@vger.kernel.org X-Gm-Message-State: AOJu0YxtC853k2Ecy3uLKL3ulN7kZSQCocg2LLVGqCt3U++4ASYxDhzE Q1hWX5MjdYoF3MEQyLWSADDSA4OWfZ+KSqQDlNU2v0QAWNJlapQilpjx X-Gm-Gg: AZuq6aLl8B8ehid2J8GTVJXg7XSNABTLydg2B6Ol4itjvAhxFa5PV8tQtFeeZ7H5E3A leJCrMzRk6le/LM0+P8PgKTiFitY5A5aNNFkB6MR4+IuVR3ARbnAUiynGqvFMzhmqSd+veq2DNk 5EWe6HUHkoi75Ei//Mc8DshCmeeM+3EG45NeDI81VSeFqfYGQ9Ye4itx/Nl+1O3XC8BjfBYSYhS athx4ze3b5S6QsPqQRw+JCh8Kt1zEgJn6GRLIoqfrsfD+mA8YoLE7YCXxCR1un4TuQAqtoxL15e OgyrucOgP5uOXKzlN/XKmaxrpmyahQgGFrQ+gIyxbprU2+KnHH8xUbOYQ+No9rtu/bhQtBtFoaB vYtFFikptat8JCsBSY8pJvRB0ShqtvTx0vaZDwITmeQkXY036gz3FR7Drrg96YLY51igLn80fPA 23f6WWchXW 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: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 }; +#define BPF_TRAMP_COOKIE_INDEX_SHIFT 8 #define BPF_TRAMP_IS_RETURN_SHIFT 63 struct bpf_tramp_links { @@ -1782,6 +1783,7 @@ struct bpf_prog { enforce_expected_attach_type:1, /* Enforce expected_attach_type checking 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; } +static inline int bpf_fsession_cookie_cnt(struct bpf_tramp_links *links) +{ + struct bpf_tramp_links fentries = links[BPF_TRAMP_FENTRY]; + int cnt = 0; + + for (int i = 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); 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; } + if (meta.func_id == special_kfunc_list[KF_bpf_session_cookie]) + env->prog->call_session_cookie = true; + return 0; } @@ -23024,6 +23027,23 @@ static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, insn_buf[1] = BPF_ALU64_IMM(BPF_RSH, BPF_REG_0, BPF_TRAMP_IS_RETURN_SHIFT); insn_buf[2] = BPF_ALU64_IMM(BPF_AND, BPF_REG_0, 1); *cnt = 3; + } else if (desc->func_id == special_kfunc_list[KF_bpf_session_cookie] && + env->prog->expected_attach_type == BPF_TRACE_FSESSION) { + /* + * inline bpf_session_cookie() for fsession: + * __u64 *bpf_session_cookie(void *ctx) + * { + * u64 off = (((u64 *)ctx)[-1] >> BPF_TRAMP_COOKIE_INDEX_SHIFT) & 0xFF; + * return &((u64 *)ctx)[-off]; + * } + */ + insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); + insn_buf[1] = BPF_ALU64_IMM(BPF_RSH, BPF_REG_0, BPF_TRAMP_COOKIE_INDEX_SHIFT); + insn_buf[2] = BPF_ALU64_IMM(BPF_AND, BPF_REG_0, 0xFF); + insn_buf[3] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_0, 3); + insn_buf[4] = BPF_ALU64_REG(BPF_SUB, BPF_REG_0, BPF_REG_1); + insn_buf[5] = BPF_ALU64_IMM(BPF_NEG, BPF_REG_0, 0); + *cnt = 6; } if (env->insn_aux_data[insn_idx].arg_prog) { -- 2.52.0