From nobody Mon Feb 9 14:33:08 2026 Received: from mail-pg1-f193.google.com (mail-pg1-f193.google.com [209.85.215.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 B139B2571DA for ; Sun, 26 Oct 2025 03:02:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.193 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761447725; cv=none; b=khQ44e/dWO4Lr3z8OrVAermtjNJhxnrL5UIPwT/fKp+HCZK0hQyjCuoSgNPF6xkutzkB5UO1sHQYKqARedO1LFisqzGL0gt3ubWkxR9sRP/Hs3K/+p7ZPkYzCvo7OJk/pVLZEiAXTiNlg4pKQ0IQTxHF+WUr00p1O2cTGnC8VdM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761447725; c=relaxed/simple; bh=vK0EtXyF0TAv0W+LwES+hsw/IVhCieW5oKRtWSpXi08=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lO9yZ/T2wN0n7y4TgxcWorUbVNr0sxAump76txty9853PvKMPtNHOUtHa1K79tl6SVx0Ibt6eLCW/ml85S5yOaH+a7kPot5RIvTh6r426X6/qEjPi2BxadQrTFPZF+vkgvsdk97ozrqGSd35U6ztrfPTgSZDBQGIBewuwi26vS0= 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=B0s9p8z2; arc=none smtp.client-ip=209.85.215.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="B0s9p8z2" Received: by mail-pg1-f193.google.com with SMTP id 41be03b00d2f7-b6cf25c9ad5so2662535a12.0 for ; Sat, 25 Oct 2025 20:02:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1761447723; x=1762052523; 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=oa5InkxvVUN642DJUe0KYXiTiyc8NAW7OtkrLGo/nXg=; b=B0s9p8z2rqLzmu0ZCL5H24uT5TNaB6J5AVngfJezN2R9593CoXQ0znRk25WKqdBy/1 q2OqwKAATNzs5pKGi8hSc6vAjOUspSZ2q7WcujoW9AGPtdsyhQg5McPpDcIZIaw5za7z r7bt3Nibu3JqXdbxTDyvCYnt/2r+yEwdg6CjuRM4LeC27v93YqI582TL6oMSGacEtmeY 6WmZSGQXw5RJIz2yrqV5podl0rfuRqYFMurAtTl33SDHs3jp/5//fyJuVCww/mrHeKjj O4vrn5J2cUqHlzwwqdKVCY1J/mE0tq6dzp0m+SzIZnpXfkqgtTyWd7r01SbdwgBTANZw nBiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761447723; x=1762052523; 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=oa5InkxvVUN642DJUe0KYXiTiyc8NAW7OtkrLGo/nXg=; b=R7pPgKF8uD/UtPpylINlSuddrebC7f79IzUNg/41tZ/J/L7ubzixP05jX9zXioRCMx YDoxyTfwVYQrTDsqsCLvx7NljUMekRakC7ervmgcL7Rc09Fh5V0L7bG+SXgt503HoZuO KTZgx/xsPDUbUScLj8OLyYdPBnitYvioFWweh+wkbuc+ErDV2GaFT2pH/aR4sEK5Ilrg iLLt8gQNRkha6N0QdXppqh+4jNUNW5GGxSUV+V7xjH3ZDMb/az43hfp962nRyvuBPm8Q 75q6m5FG/7RS6IdmceZoij/1zo7WPnGwOQMYIf8OnV/5Gp23t8E803Jt111CrltfJmGE HZ0A== X-Forwarded-Encrypted: i=1; AJvYcCUDF1NS6R1BkmcOIy4E16ggwYi2lex21xmiKtsuur1+Of88ycyBktCfdy4ZJz7NRzBhCYnh+Wi7QDb+epA=@vger.kernel.org X-Gm-Message-State: AOJu0YyvPshvQ2wqxOEn74DTmN3nx8JVLo3NZz9TP5WAHssxHB9G96k0 x7tMIw6c4pvt2/n7AajSxL9O/Vh5aF70bJUDr4RKlLyEl91k3pjxvfrt X-Gm-Gg: ASbGncsjz+mitprQWdsna7axHU2gW8w+qa3X+ZbK3JkoSRZOSi4E1Lt6cQhJ+fFUAcH tvf79RTOJmaXuzMxriUqNY1sXTg18CGjRJ819CuMPlGCgwycq0Wi2hO7+HVA2XEtWtCKLLjMCEl qh2m6Zmwg3USIfVuahfovWsHmvqOSo4uEqIUcunvY6Zfoi5YXzf6jUYAA8ZPvcSuJpYpBTZ4+MK 7LAPGJGD4qbzRC4MS3NurwYSX5WtSf0fj2RdB7ta/f087wiYBWuRZ/S3Lw4rwSJc+cszbI9tqt/ vX0J5w3FK2l2MpaBm5MLRDhLjEAouYlBR4cPw9YkTAPr0Mco3lJ5H27RD6QpE76Cpak7UIAwq84 xOV2V/gRInU2uM9RZrvg51FK/Ic2BwS1PyrEarQquKnI/sf1JjRAj7f+wPF+8BfLrt8jgSiry8L PH/BEfGgW776E= X-Google-Smtp-Source: AGHT+IFxWgkIq1VivRbF+1Yvqo5Ua7kf/vo2jP2bHKSgdoIAHVPL9mtpbM8fwrjkk+f/fOyMBUBZsQ== X-Received: by 2002:a17:902:e88e:b0:27e:f018:d312 with SMTP id d9443c01a7336-290c9cf350amr421236215ad.1.1761447722885; Sat, 25 Oct 2025 20:02:02 -0700 (PDT) Received: from 7950hx ([43.129.244.20]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-29498d40b1esm38100645ad.73.2025.10.25.20.01.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Oct 2025 20:02:02 -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, 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 v3 2/7] bpf: add two kfunc for TRACE_SESSION Date: Sun, 26 Oct 2025 11:01:38 +0800 Message-ID: <20251026030143.23807-3-dongml2@chinatelecom.cn> X-Mailer: git-send-email 2.51.1 In-Reply-To: <20251026030143.23807-1-dongml2@chinatelecom.cn> References: <20251026030143.23807-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" If TRACE_SESSION exists, we will use extra 8-bytes in the stack of the trampoline to store the flags that we needed, and the 8-bytes lie after the return value, which means ctx[nr_args + 1]. And we will store the flag "is_exit" to the first bit of it. Introduce the kfunc bpf_tracing_is_exit(), which is used to tell if it is fexit currently. Meanwhile, inline it in the verifier. 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 Co-developed-by: Leon Hwang Signed-off-by: Leon Hwang --- v3: - merge the bpf_tracing_is_exit and bpf_fsession_cookie into a single patch v2: - store the session flags after return value, instead of before nr_args - inline the bpf_tracing_is_exit, as Jiri suggested --- include/linux/bpf.h | 1 + kernel/bpf/verifier.c | 33 ++++++++++++++++++++-- kernel/trace/bpf_trace.c | 59 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 88 insertions(+), 5 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 6b5855c80fa6..ce55d3881c0d 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1736,6 +1736,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 818deb6a06e4..6f8aa4718d6f 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -12293,6 +12293,8 @@ enum special_kfunc_type { KF___bpf_trap, 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) @@ -12365,6 +12367,8 @@ BTF_ID(func, bpf_res_spin_unlock_irqrestore) 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) { @@ -12419,7 +12423,9 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env, struct bpf_reg_state *reg =3D ®s[regno]; bool arg_mem_size =3D false; =20 - if (meta->func_id =3D=3D special_kfunc_list[KF_bpf_cast_to_kern_ctx]) + 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_fsession_cookie]) return KF_ARG_PTR_TO_CTX; =20 /* In this function, we verify the kfunc's BTF as per the argument type, @@ -13912,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; } @@ -14193,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 @@ -22012,6 +22022,25 @@ static int fixup_kfunc_call(struct bpf_verifier_en= v *env, struct bpf_insn *insn, desc->func_id =3D=3D special_kfunc_list[KF_bpf_rdonly_cast]) { insn_buf[0] =3D BPF_MOV64_REG(BPF_REG_0, BPF_REG_1); *cnt =3D 1; + } else if (desc->func_id =3D=3D special_kfunc_list[KF_bpf_tracing_is_exit= ]) { + /* Load nr_args from ctx - 8 */ + insn_buf[0] =3D BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); + /* add rax, 1 */ + insn_buf[1] =3D BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 1); + 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); + 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 4f87c16d915a..4a8568bd654d 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -3356,12 +3356,65 @@ static const struct btf_kfunc_id_set bpf_kprobe_mul= ti_kfunc_set =3D { .filter =3D bpf_kprobe_multi_filter, }; =20 -static int __init bpf_kprobe_multi_kfuncs_init(void) +__bpf_kfunc_start_defs(); + +__bpf_kfunc bool bpf_tracing_is_exit(void *ctx) { - return register_btf_kfunc_id_set(BPF_PROG_TYPE_KPROBE, &bpf_kprobe_multi_= kfunc_set); + /* This helper call is inlined by verifier. */ + u64 nr_args =3D ((u64 *)ctx)[-1]; + + /* + * ctx[nr_args + 1] is the session flags, and the last bit is + * is_exit. + */ + return ((u64 *)ctx)[nr_args + 1] & 1; +} + +__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(); + +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) + +static int bpf_tracing_filter(const struct bpf_prog *prog, u32 kfunc_id) +{ + if (!btf_id_set8_contains(&tracing_kfunc_set_ids, kfunc_id)) + return 0; + + if (prog->type !=3D BPF_PROG_TYPE_TRACING || + prog->expected_attach_type !=3D BPF_TRACE_SESSION) + return -EINVAL; + + return 0; +} + +static const struct btf_kfunc_id_set bpf_tracing_kfunc_set =3D { + .owner =3D THIS_MODULE, + .set =3D &tracing_kfunc_set_ids, + .filter =3D bpf_tracing_filter, +}; + +static int __init bpf_trace_kfuncs_init(void) +{ + int err =3D 0; + + err =3D err ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_KPROBE, &bpf_kprob= e_multi_kfunc_set); + err =3D err ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_trac= ing_kfunc_set); + + return err; } =20 -late_initcall(bpf_kprobe_multi_kfuncs_init); +late_initcall(bpf_trace_kfuncs_init); =20 typedef int (*copy_fn_t)(void *dst, const void *src, u32 size, struct task= _struct *tsk); =20 --=20 2.51.1