From nobody Mon Feb 9 10:12:43 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 9262A302175 for ; Wed, 22 Oct 2025 08:02:26 +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=1761120150; cv=none; b=DvH0F+r6NFELZ3jkIzvkXxtko9FH9Zy3Hy85CT7LZTw+Nvg81jxQpHAT/DKjkv2GQwUuoDIZjx2+zUJP++wP4gNGFyY31HvSBr+jLwMalASEXGzJmyEURpArxN8VtOCVDDP23UbCxndRhs9JpfUql3ESRVlTRFweNLhnDEc6qjM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761120150; c=relaxed/simple; bh=E3vAsVhEKLpEuWi7Z5YqNSEtnj0vJWvd3Pb3WZ9xabM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NiLdAUdn0SsgwovnzmMtqb9cGI0BREt5q5T7/oeQ5svRjHaO4XBuXoIT20IbTloctsSwzSREhaCNo1DgQJeABK1qqpVAACN/1k5wdiN0GC9hOZMlFZvSlbM1BUBdkh0k6AEhe8+fUQc4JrN91jGMmq7WfXvpDWgqtmSZveDO15s= 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=Zt08qrWC; 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="Zt08qrWC" Received: by mail-pl1-f196.google.com with SMTP id d9443c01a7336-28e7cd6dbc0so82697345ad.0 for ; Wed, 22 Oct 2025 01:02:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1761120146; x=1761724946; 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=ylOZLeoyykhuoaz4lbTwEjPsmcSXlb4GkDnmde4T+hQ=; b=Zt08qrWCatcHaC9a5Bj26UDTgjcTHU0Rh/n4rmrjCkYK7ps6oBB25BHd2rrBT6KAIF QiaoRiCc3+ofgkIVcS7HaCTPGmwI2iMtDFwyK08eWe4s7bMq39x52+qafApj/iRP+93R h76W64bXTKptvv6tvpCV+msjPbXHppnoCKNanpx2mkN77CSnu5ufaz4O2uE0rayIMhYj N0UZDneEdxBReI1QjPvMgep0k2MSOp0EbfKwbLI+8nvwCuBuh49eiCw0RsDePphLifrq WUpeKRhKHf1nFBAFQDlElDeyeCaPZCVAdBVkyS41fg6pEQEaavyprJZcUl9mXkE2UKnv naJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761120146; x=1761724946; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ylOZLeoyykhuoaz4lbTwEjPsmcSXlb4GkDnmde4T+hQ=; b=JcFSGQA0WE/YYpiTHBclZApmsY6qD4Sj3LviQmugWwhxR11GqaR8MbAiOaSJ6y5Z2G +VuZY26dE+gVUWwnMMgUwwS4ifpstRgEWeYsVwmPmZow3G51I1uz75pmhaTXTcF07fIj HrZFZkg0hDbimbuJXN7ZrnvIGkyb7krR1CBe+Kl2XPRfzZqArkcFVudvXmpRAT7feZZc EA6c9yaEgBS6t7yWMAgjo/50+dWlVZP82TdqeI4F8O370ZC5L0qTzFSnYrgYdqsCVyWn ZF0EUgoer3XSYsXZSYuXgI3dzh+bjhGOUb4LTH28tQyyTRu60ivjf/O8MFIcRYq3M034 YcBg== X-Forwarded-Encrypted: i=1; AJvYcCWbR2nS/WP9j4GUPdJNh5+Bbd740qZCCCJbGiICh6r6SosVydbVV8A+x6k76fmHcOtXObT66hV1qySGI1s=@vger.kernel.org X-Gm-Message-State: AOJu0YylHKLrLJtNoNAY2RinQ3OqMb79lqPheQYA3UyDSOrVc9xOr2QP O7bUmVnEsd8/mDxqj/dxfMaV1a6WogBqlFDxSFnmGDhvwzjGcYy6plnO X-Gm-Gg: ASbGncuPW3Kb6U3vC6lwsfn5ITXyLqX8kQa0ai62uvC8MMUvCV38P4X9EF7IUh/M5Bs iq1Zvvs60fRsK4mxoVye9/kPBfNj6hZabMRbSijVgToDRgGOztiKkix9JNelkq2xFhcPZSgxL7K a5on4xeQ5XRdQwC3r14d6UcV9HMFvdcAZkCYA+eC6nwS77Ayx/2d6lLGnCZdt2f4quYFvajvY2R y9/8r1DxAlWM1iO8rtbDvkH6s2liIHP7VIlG/rUZSLtWRfRt0t/xej9xfGQUbyZQWi3ghi979f3 Qf4qk55UwrtuxkcMZEEwP8L7p1y/jghHbp3W7pGvQlT2uuxWN2t2RbpO5fDj2yLj2YSBAckPGkR B/faB544wDwx6Nt0PYJYZ9rv1ua79qNA4MNoHjFGRgOqE/lOgILCwA0H9u6fA8F5PhqIub1Xplq 7hHJVzilE= X-Google-Smtp-Source: AGHT+IElFYc85CKhS9KfCJHXRWtMMxrtc4wlc15KexTf7Z4eltq5dEOMAG1bp2AuYLzlNwgLZOKhew== X-Received: by 2002:a17:902:ef4f:b0:275:b1cf:6ddc with SMTP id d9443c01a7336-290c9cf8f20mr260206065ad.5.1761120145882; Wed, 22 Oct 2025 01:02:25 -0700 (PDT) Received: from 7950hx ([43.129.244.20]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-292471d7e41sm131947785ad.57.2025.10.22.01.02.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Oct 2025 01:02:25 -0700 (PDT) From: Menglong Dong X-Google-Original-From: Menglong Dong To: ast@kernel.org, jolsa@kernel.org Cc: daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, mattbobrowski@google.com, rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, leon.hwang@linux.dev, jiang.biao@linux.dev, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH bpf-next v2 03/10] bpf: add kfunc bpf_fsession_cookie for TRACING SESSION Date: Wed, 22 Oct 2025 16:01:52 +0800 Message-ID: <20251022080159.553805-4-dongml2@chinatelecom.cn> X-Mailer: git-send-email 2.51.1.dirty In-Reply-To: <20251022080159.553805-1-dongml2@chinatelecom.cn> References: <20251022080159.553805-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 the kfunc bpf_fsession_cookie(), which is similar to bpf_session_cookie() and return the address of the session cookie. The address of the session cookie is stored after session flags, which means ctx[nr_args + 2]. Inline this kfunc in the verifier too. Signed-off-by: Menglong Dong --- include/linux/bpf.h | 1 + kernel/bpf/verifier.c | 20 ++++++++++++++++++-- kernel/trace/bpf_trace.c | 10 ++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 3f6cad5df2db..83d5d4d3120d 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1735,6 +1735,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_fsession_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 */ diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index a4d0dd4440fd..ab46e5fbc7a6 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -12294,6 +12294,7 @@ enum special_kfunc_type { KF_bpf_task_work_schedule_signal, KF_bpf_task_work_schedule_resume, KF_bpf_tracing_is_exit, + KF_bpf_fsession_cookie, }; =20 BTF_ID_LIST(special_kfunc_list) @@ -12367,6 +12368,7 @@ BTF_ID(func, __bpf_trap) BTF_ID(func, bpf_task_work_schedule_signal) BTF_ID(func, bpf_task_work_schedule_resume) BTF_ID(func, bpf_tracing_is_exit) +BTF_ID(func, bpf_fsession_cookie) =20 static bool is_task_work_add_kfunc(u32 func_id) { @@ -12422,7 +12424,8 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env, bool arg_mem_size =3D false; =20 if (meta->func_id =3D=3D special_kfunc_list[KF_bpf_cast_to_kern_ctx] || - meta->func_id =3D=3D special_kfunc_list[KF_bpf_tracing_is_exit]) + meta->func_id =3D=3D special_kfunc_list[KF_bpf_tracing_is_exit] || + meta->func_id =3D=3D special_kfunc_list[KF_bpf_fsession_cookie]) return KF_ARG_PTR_TO_CTX; =20 /* In this function, we verify the kfunc's BTF as per the argument type, @@ -13915,7 +13918,8 @@ static int check_kfunc_call(struct bpf_verifier_env= *env, struct bpf_insn *insn, } } =20 - if (meta.func_id =3D=3D special_kfunc_list[KF_bpf_session_cookie]) { + if (meta.func_id =3D=3D special_kfunc_list[KF_bpf_session_cookie] || + meta.func_id =3D=3D special_kfunc_list[KF_bpf_fsession_cookie]) { meta.r0_size =3D sizeof(u64); meta.r0_rdonly =3D false; } @@ -14196,6 +14200,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_fsession_cookie]) + env->prog->call_session_cookie =3D true; + return 0; } =20 @@ -22007,6 +22014,15 @@ static int fixup_kfunc_call(struct bpf_verifier_en= v *env, struct bpf_insn *insn, insn_buf[4] =3D BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 0); insn_buf[5] =3D BPF_ALU64_IMM(BPF_AND, BPF_REG_0, 1); *cnt =3D 6; + } else if (desc->func_id =3D=3D special_kfunc_list[KF_bpf_fsession_cookie= ]) { + /* Load nr_args from ctx - 8 */ + insn_buf[0] =3D BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); + /* add rax, 2 */ + insn_buf[1] =3D BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 2); + insn_buf[2] =3D BPF_ALU64_IMM(BPF_LSH, BPF_REG_0, 3); + insn_buf[3] =3D BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1); + insn_buf[4] =3D BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_0, 0); + *cnt =3D 5; } =20 if (env->insn_aux_data[insn_idx].arg_prog) { diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index d0720d850621..4a8568bd654d 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -3370,10 +3370,20 @@ __bpf_kfunc bool bpf_tracing_is_exit(void *ctx) return ((u64 *)ctx)[nr_args + 1] & 1; } =20 +__bpf_kfunc u64 *bpf_fsession_cookie(void *ctx) +{ + /* This helper call is inlined by verifier. */ + u64 nr_args =3D ((u64 *)ctx)[-1]; + + /* ctx[nr_args + 2] is the session cookie address */ + return (u64 *)((u64 *)ctx)[nr_args + 2]; +} + __bpf_kfunc_end_defs(); =20 BTF_KFUNCS_START(tracing_kfunc_set_ids) BTF_ID_FLAGS(func, bpf_tracing_is_exit, KF_FASTCALL) +BTF_ID_FLAGS(func, bpf_fsession_cookie, KF_FASTCALL) BTF_KFUNCS_END(tracing_kfunc_set_ids) =20 static int bpf_tracing_filter(const struct bpf_prog *prog, u32 kfunc_id) --=20 2.51.1.dirty