From nobody Sat Feb 7 08:23:08 2026 Received: from mail-pf1-f195.google.com (mail-pf1-f195.google.com [209.85.210.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 DE988286D60 for ; Sat, 18 Oct 2025 14:21:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.195 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760797304; cv=none; b=bIS9nE8zVFpnXu3yfHRMgsQsUGrQirBj9huiPd9SWiU5amTsq1lzOZKEpYt/t7IV67pHzSPzagMW94B7sc38SjmbsAxtg8hMC9fN4dE9jXuyFU7G12l15LZ0FF/PsVf7cs+ntFmQDFyJymjZR+7zWxkcv621gTIrczQd2EPC3Rk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760797304; c=relaxed/simple; bh=j4FoV6+5qJQDy+qnJ1ZhU8GvU8qSxrOVwW4PgSBB4+A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BVDFQO+rJvYraBtWT+u8NtSWIe4tPTnBuwDClQLK9Se8b8hLCmGQENWCj7S0jy59c4oO06SHfgmxf9nfKupwaX7/uzaL9oJUAjcHcMib2/WMhf+Yp9VmUxkdulTajOxhaXu1/1s6mUzuM2Ak9eBk/9VwFDdKXjlfD1eRDwImNKU= 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=dtyLwIdN; arc=none smtp.client-ip=209.85.210.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="dtyLwIdN" Received: by mail-pf1-f195.google.com with SMTP id d2e1a72fcca58-79ef9d1805fso2778488b3a.1 for ; Sat, 18 Oct 2025 07:21:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1760797302; x=1761402102; 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=KFo/rBG24KK8EblRFPIaZrGm9X7amIHu1zrb5RTdJ5g=; b=dtyLwIdN9U8nVF4z0tGhCtvJrdRRQZe1n/uuiWlDVIhFY8hT8Jj/FSnq39tddB614X jI+DwfzT9IR4OUDgB2/DvPQ/mqzf8Uu3ObBxbYxzxb/2u9zPUv1rNySxBbBOcy2l4r9/ FCAhqyR88Rii3AZw+T3haaxagm6ur6gzhNimE9/K1jfOsg1vsGh7WNkwZC/IwQy8TwQt aoryyCjzdB+0Vp1wYS6ElfS2toupfy41GPOnnPpgn1vLGQIpuYOjNVZNJr/6/TEEbFNp 2zeyRpoZUL09c1li2npLP+s8vYAQF+wTg6xUunohbG6uah8xOwCWSlMGNpHRhR1VU7km tQCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760797302; x=1761402102; 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=KFo/rBG24KK8EblRFPIaZrGm9X7amIHu1zrb5RTdJ5g=; b=LEuzRyouH+1uFXYrv1KenoNcdLW+bD4Dwj0rKr+WzahzvpBgShDsnGgpi244y+Uykp qqNM0aHN3VB+gMk/H/7UGJQHcpcf5KeBxVf38mVl9m2TG5RZIFht+rul+3IWxlYpKG7M nIiNuBbeGfn5aumEzF8fslHmONseUtqJdzz6TZVP6q1YITcH2Ki/6dho0D9kImmMDF7D 3Eys/LKpWWtz6sracY84h9hqr4oi/s1d3hiC/UuTiCziSuPHizSiwhK258SXtbQCq6u4 /LZnFJkT9ItcjB5dVw2nmeI+0GVPQmJRVKw3JTJADsPc5eCh+DScsIk91VKc1yXCxbxm Yaww== X-Forwarded-Encrypted: i=1; AJvYcCV7OC6X9i8EyNez5IOq93n4w7+qdryPIsGnXY9LPujWIxcOIqYecXTfwAla9knwqsOazksrsdVQzAft1wc=@vger.kernel.org X-Gm-Message-State: AOJu0Yzc1CNv2lOd2BjHfUwuH8i5Szy31KBNVVe0DlfRkUR7tjmfMsZV HhH9mqB7MgVhX+hr8yuU1H88dh1uYBkdHBDeuY5tSqFg4HFdjX7CfH9w X-Gm-Gg: ASbGncs1PN+CutzMCbMO0Pkf/JZK2JzrRf34ZzUnoFxkxVCeRHIE/axR7VH9fwbb3dY j3tp66987PeAUMSSQDwGdWhJTFkJt1DcftQkK/Whb3y5vlxxw00EQRrf/am+7P7WY8yc8XdKBPq qve4ZeMLlUL5oIGemsVMs1X5r4dphC8iq4gNdq1ECSJEb8zlxwyYkJ5gmFLaD+/EZrrKbg+q5Rj Qxf2lp5IbaASDuzAy8T+rzdLkfUn6yXEOIgz/TmfYjlqNw0Huqgjiyg7/nW8W2U8mSrsF6VtH1Q 1BWXd/SvesnvLjK8JiBsMdMUMLarMXFWM2gSfpAcCHRmEqDk5I/UkixjFhqb3Pd6XzeaJuPuXKl aFBQ/kLZcHbyQPFMa6W32cVIfggDD8dXwLdz4nw47Ri07CG3J0c8pc2KkHUnT2kPuxR0WLRHeRM XBasPMpToyUFo= X-Google-Smtp-Source: AGHT+IFRhX3XY3uOEHTyrIWY0QhaxXyizShmzTfEyYUzW7jXb7ZaXfHCLMSHap1oXfphklzQDRm5Ig== X-Received: by 2002:a05:6a00:4602:b0:77f:324a:6037 with SMTP id d2e1a72fcca58-7a220a59102mr7392649b3a.7.1760797302078; Sat, 18 Oct 2025 07:21:42 -0700 (PDT) Received: from 7950hx ([43.129.244.20]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7a23010d818sm2913589b3a.53.2025.10.18.07.21.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Oct 2025 07:21:41 -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, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH RFC bpf-next 1/5] bpf: add tracing session support Date: Sat, 18 Oct 2025 22:21:20 +0800 Message-ID: <20251018142124.783206-2-dongml2@chinatelecom.cn> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251018142124.783206-1-dongml2@chinatelecom.cn> References: <20251018142124.783206-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" The tracing session is something that similar to kprobe session. It allow to attach a single BPF program to both the entry and the exit of the target functions. While a non-zero value is returned by the fentry, the fexit will be skipped, which is similar to kprobe session. Signed-off-by: Menglong Dong Co-developed-by: Leon Hwang Signed-off-by: Leon Hwang --- arch/arm64/net/bpf_jit_comp.c | 3 +++ arch/loongarch/net/bpf_jit.c | 3 +++ arch/powerpc/net/bpf_jit_comp.c | 3 +++ arch/riscv/net/bpf_jit_comp64.c | 3 +++ arch/s390/net/bpf_jit_comp.c | 3 +++ include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 1 + kernel/bpf/btf.c | 2 ++ kernel/bpf/syscall.c | 2 ++ kernel/bpf/trampoline.c | 5 ++++- kernel/bpf/verifier.c | 12 +++++++++--- net/bpf/test_run.c | 1 + net/core/bpf_sk_storage.c | 1 + tools/include/uapi/linux/bpf.h | 1 + 14 files changed, 37 insertions(+), 4 deletions(-) diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c index ab83089c3d8f..06f4bd6c6755 100644 --- a/arch/arm64/net/bpf_jit_comp.c +++ b/arch/arm64/net/bpf_jit_comp.c @@ -2788,6 +2788,9 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_imag= e *im, void *ro_image, void *image, *tmp; int ret; =20 + if (tlinks[BPF_TRAMP_SESSION].nr_links) + return -EOPNOTSUPP; + /* image doesn't need to be in module memory range, so we can * use kvmalloc. */ diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c index cbe53d0b7fb0..ad596341658a 100644 --- a/arch/loongarch/net/bpf_jit.c +++ b/arch/loongarch/net/bpf_jit.c @@ -1739,6 +1739,9 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_imag= e *im, void *ro_image, void *image, *tmp; struct jit_ctx ctx; =20 + if (tlinks[BPF_TRAMP_SESSION].nr_links) + return -EOPNOTSUPP; + size =3D ro_image_end - ro_image; image =3D kvmalloc(size, GFP_KERNEL); if (!image) diff --git a/arch/powerpc/net/bpf_jit_comp.c b/arch/powerpc/net/bpf_jit_com= p.c index 88ad5ba7b87f..bcc0ce09f6fa 100644 --- a/arch/powerpc/net/bpf_jit_comp.c +++ b/arch/powerpc/net/bpf_jit_comp.c @@ -1017,6 +1017,9 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_imag= e *im, void *image, void *i void *rw_image, *tmp; int ret; =20 + if (tlinks[BPF_TRAMP_SESSION].nr_links) + return -EOPNOTSUPP; + /* * rw_image doesn't need to be in module memory range, so we can * use kvmalloc. diff --git a/arch/riscv/net/bpf_jit_comp64.c b/arch/riscv/net/bpf_jit_comp6= 4.c index 45cbc7c6fe49..55b0284bf177 100644 --- a/arch/riscv/net/bpf_jit_comp64.c +++ b/arch/riscv/net/bpf_jit_comp64.c @@ -1286,6 +1286,9 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_imag= e *im, void *ro_image, struct rv_jit_context ctx; u32 size =3D ro_image_end - ro_image; =20 + if (tlinks[BPF_TRAMP_SESSION].nr_links) + return -EOPNOTSUPP; + image =3D kvmalloc(size, GFP_KERNEL); if (!image) return -ENOMEM; diff --git a/arch/s390/net/bpf_jit_comp.c b/arch/s390/net/bpf_jit_comp.c index cf461d76e9da..3f25bf55b150 100644 --- a/arch/s390/net/bpf_jit_comp.c +++ b/arch/s390/net/bpf_jit_comp.c @@ -2924,6 +2924,9 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_imag= e *im, void *image, struct bpf_tramp_jit tjit; int ret; =20 + if (tlinks[BPF_TRAMP_SESSION].nr_links) + return -EOPNOTSUPP; + /* Compute offsets, check whether the code fits. */ memset(&tjit, 0, sizeof(tjit)); ret =3D __arch_prepare_bpf_trampoline(im, &tjit, m, flags, diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 86afd9ac6848..aa9f02b56edd 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1270,6 +1270,7 @@ enum bpf_tramp_prog_type { BPF_TRAMP_FENTRY, BPF_TRAMP_FEXIT, BPF_TRAMP_MODIFY_RETURN, + BPF_TRAMP_SESSION, BPF_TRAMP_MAX, BPF_TRAMP_REPLACE, /* more than MAX */ }; diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 6829936d33f5..79ba3023e8be 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -1133,6 +1133,7 @@ enum bpf_attach_type { BPF_NETKIT_PEER, BPF_TRACE_KPROBE_SESSION, BPF_TRACE_UPROBE_SESSION, + BPF_TRACE_SESSION, __MAX_BPF_ATTACH_TYPE }; =20 diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 0de8fc8a0e0b..2c1c3e0caff8 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -6107,6 +6107,7 @@ static int btf_validate_prog_ctx_type(struct bpf_veri= fier_log *log, const struct case BPF_TRACE_FENTRY: case BPF_TRACE_FEXIT: case BPF_MODIFY_RETURN: + case BPF_TRACE_SESSION: /* allow u64* as ctx */ if (btf_is_int(t) && t->size =3D=3D 8) return 0; @@ -6704,6 +6705,7 @@ bool btf_ctx_access(int off, int size, enum bpf_acces= s_type type, fallthrough; case BPF_LSM_CGROUP: case BPF_TRACE_FEXIT: + case BPF_TRACE_SESSION: /* When LSM programs are attached to void LSM hooks * they use FEXIT trampolines and when attached to * int LSM hooks, they use MODIFY_RETURN trampolines. diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 2a9456a3e730..15ce86b19ca4 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3549,6 +3549,7 @@ static int bpf_tracing_prog_attach(struct bpf_prog *p= rog, case BPF_PROG_TYPE_TRACING: if (prog->expected_attach_type !=3D BPF_TRACE_FENTRY && prog->expected_attach_type !=3D BPF_TRACE_FEXIT && + prog->expected_attach_type !=3D BPF_TRACE_SESSION && prog->expected_attach_type !=3D BPF_MODIFY_RETURN) { err =3D -EINVAL; goto out_put_prog; @@ -4322,6 +4323,7 @@ attach_type_to_prog_type(enum bpf_attach_type attach_= type) case BPF_TRACE_RAW_TP: case BPF_TRACE_FENTRY: case BPF_TRACE_FEXIT: + case BPF_TRACE_SESSION: case BPF_MODIFY_RETURN: return BPF_PROG_TYPE_TRACING; case BPF_LSM_MAC: diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c index 5949095e51c3..f6d4dea3461e 100644 --- a/kernel/bpf/trampoline.c +++ b/kernel/bpf/trampoline.c @@ -111,7 +111,7 @@ bool bpf_prog_has_trampoline(const struct bpf_prog *pro= g) =20 return (ptype =3D=3D BPF_PROG_TYPE_TRACING && (eatype =3D=3D BPF_TRACE_FENTRY || eatype =3D=3D BPF_TRACE_FEXIT || - eatype =3D=3D BPF_MODIFY_RETURN)) || + eatype =3D=3D BPF_MODIFY_RETURN || eatype =3D=3D BPF_TRACE_SESSION)) || (ptype =3D=3D BPF_PROG_TYPE_LSM && eatype =3D=3D BPF_LSM_MAC); } =20 @@ -418,6 +418,7 @@ static int bpf_trampoline_update(struct bpf_trampoline = *tr, bool lock_direct_mut tr->flags &=3D (BPF_TRAMP_F_SHARE_IPMODIFY | BPF_TRAMP_F_TAIL_CALL_CTX); =20 if (tlinks[BPF_TRAMP_FEXIT].nr_links || + tlinks[BPF_TRAMP_SESSION].nr_links || tlinks[BPF_TRAMP_MODIFY_RETURN].nr_links) { /* NOTE: BPF_TRAMP_F_RESTORE_REGS and BPF_TRAMP_F_SKIP_FRAME * should not be set together. @@ -515,6 +516,8 @@ static enum bpf_tramp_prog_type bpf_attach_type_to_tram= p(struct bpf_prog *prog) return BPF_TRAMP_MODIFY_RETURN; case BPF_TRACE_FEXIT: return BPF_TRAMP_FEXIT; + case BPF_TRACE_SESSION: + return BPF_TRAMP_SESSION; case BPF_LSM_MAC: if (!prog->aux->attach_func_proto->type) /* The function returns void, we cannot modify its diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index c908015b2d34..40e3274e8bc2 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -17272,6 +17272,7 @@ static int check_return_code(struct bpf_verifier_en= v *env, int regno, const char break; case BPF_TRACE_RAW_TP: case BPF_MODIFY_RETURN: + case BPF_TRACE_SESSION: return 0; case BPF_TRACE_ITER: break; @@ -22727,6 +22728,7 @@ static int do_misc_fixups(struct bpf_verifier_env *= env) if (prog_type =3D=3D BPF_PROG_TYPE_TRACING && insn->imm =3D=3D BPF_FUNC_get_func_ret) { if (eatype =3D=3D BPF_TRACE_FEXIT || + eatype =3D=3D BPF_TRACE_SESSION || eatype =3D=3D BPF_MODIFY_RETURN) { /* Load nr_args from ctx - 8 */ insn_buf[0] =3D BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); @@ -23668,7 +23670,8 @@ int bpf_check_attach_target(struct bpf_verifier_log= *log, if (tgt_prog->type =3D=3D BPF_PROG_TYPE_TRACING && prog_extension && (tgt_prog->expected_attach_type =3D=3D BPF_TRACE_FENTRY || - tgt_prog->expected_attach_type =3D=3D BPF_TRACE_FEXIT)) { + tgt_prog->expected_attach_type =3D=3D BPF_TRACE_FEXIT || + tgt_prog->expected_attach_type =3D=3D BPF_TRACE_SESSION)) { /* Program extensions can extend all program types * except fentry/fexit. The reason is the following. * The fentry/fexit programs are used for performance @@ -23683,7 +23686,7 @@ int bpf_check_attach_target(struct bpf_verifier_log= *log, * beyond reasonable stack size. Hence extending fentry * is not allowed. */ - bpf_log(log, "Cannot extend fentry/fexit\n"); + bpf_log(log, "Cannot extend fentry/fexit/session\n"); return -EINVAL; } } else { @@ -23767,6 +23770,7 @@ int bpf_check_attach_target(struct bpf_verifier_log= *log, case BPF_LSM_CGROUP: case BPF_TRACE_FENTRY: case BPF_TRACE_FEXIT: + case BPF_TRACE_SESSION: if (!btf_type_is_func(t)) { bpf_log(log, "attach_btf_id %u is not a function\n", btf_id); @@ -23933,6 +23937,7 @@ static bool can_be_sleepable(struct bpf_prog *prog) case BPF_TRACE_FEXIT: case BPF_MODIFY_RETURN: case BPF_TRACE_ITER: + case BPF_TRACE_SESSION: return true; default: return false; @@ -24014,9 +24019,10 @@ static int check_attach_btf_id(struct bpf_verifier= _env *env) tgt_info.tgt_name); return -EINVAL; } else if ((prog->expected_attach_type =3D=3D BPF_TRACE_FEXIT || + prog->expected_attach_type =3D=3D BPF_TRACE_SESSION || prog->expected_attach_type =3D=3D BPF_MODIFY_RETURN) && btf_id_set_contains(&noreturn_deny, btf_id)) { - verbose(env, "Attaching fexit/fmod_ret to __noreturn function '%s' is re= jected.\n", + verbose(env, "Attaching fexit/session/fmod_ret to __noreturn function '%= s' is rejected.\n", tgt_info.tgt_name); return -EINVAL; } diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c index 05e30ff5b6f9..aa2b5b17a7c7 100644 --- a/net/bpf/test_run.c +++ b/net/bpf/test_run.c @@ -696,6 +696,7 @@ int bpf_prog_test_run_tracing(struct bpf_prog *prog, switch (prog->expected_attach_type) { case BPF_TRACE_FENTRY: case BPF_TRACE_FEXIT: + case BPF_TRACE_SESSION: if (bpf_fentry_test1(1) !=3D 2 || bpf_fentry_test2(2, 3) !=3D 5 || bpf_fentry_test3(4, 5, 6) !=3D 15 || diff --git a/net/core/bpf_sk_storage.c b/net/core/bpf_sk_storage.c index d3fbaf89a698..8da8834aa134 100644 --- a/net/core/bpf_sk_storage.c +++ b/net/core/bpf_sk_storage.c @@ -365,6 +365,7 @@ static bool bpf_sk_storage_tracing_allowed(const struct= bpf_prog *prog) return true; case BPF_TRACE_FENTRY: case BPF_TRACE_FEXIT: + case BPF_TRACE_SESSION: return !!strncmp(prog->aux->attach_func_name, "bpf_sk_storage", strlen("bpf_sk_storage")); default: diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 6829936d33f5..79ba3023e8be 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -1133,6 +1133,7 @@ enum bpf_attach_type { BPF_NETKIT_PEER, BPF_TRACE_KPROBE_SESSION, BPF_TRACE_UPROBE_SESSION, + BPF_TRACE_SESSION, __MAX_BPF_ATTACH_TYPE }; =20 --=20 2.51.0 From nobody Sat Feb 7 08:23:08 2026 Received: from mail-pf1-f194.google.com (mail-pf1-f194.google.com [209.85.210.194]) (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 863DC1B4247 for ; Sat, 18 Oct 2025 14:21:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.194 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760797312; cv=none; b=GvEiXNpqt7WYFqMKZNWi1ZOZ4TmKjU8O/7MbhcuFVnet1lMOVNIPmzgS3fiVCtuNfp59jSLYkv61g1RZIH/wYcce/YcUvelsh0jEmSSGCUSH9gjWa4LJvtgRkzOocIP/jJSkKFQ6Flcy8RhTl1Q34JOcE6FDprGBpF96Zj/uUcU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760797312; c=relaxed/simple; bh=c99jLCl4oK0lYbHL6l7ZVaK/eHqsZKgxrs6O+Xjt2Ro=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hyU4/ZS3riGmqg1QjOdaaPJuqOkwQfzYN0NT0P3xc6Oi0CugZBs3fSd2RtH841CNUldmeWt4C0WnC4cA4n+LKJ0fu+pHa8VzFCqqzqEz6kgCj0msgWg56GDjlDK+tE6/k6ebWAVS/pmnq3FIXsJqdS3Tdw3/F9kI79nBtF+e82k= 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=hhk061bR; arc=none smtp.client-ip=209.85.210.194 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="hhk061bR" Received: by mail-pf1-f194.google.com with SMTP id d2e1a72fcca58-78af743c232so2503551b3a.1 for ; Sat, 18 Oct 2025 07:21:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1760797310; x=1761402110; 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=NYwvvMkNsizkLkVR3lZyOmzWt267ljepeEAqISwgDss=; b=hhk061bR+uw2b/fjfGSSxs2NgEfBA3i/2BjMPWD4K4YGGrDtKRipuD+F2mS58iT5PO sufWTv1GBXwxjahpsL/1Ut3FsgiQ34iTfXQ3ZUaD8tbarSZ9j/wt3I18xA4VJ+OcFE3z woBY9UxLzJPQLp9vxh6oWS4YCZ4WG7eZTj93Cd/TiaaiY7BolYpBxliIXOP8shWny1yE wJwJVxxdIFV5+dSCK/YZAGZtUBTSluj89gW0u3qFTSF0goxkdrQzEctBFQ9k47zC7/E8 E4I2esZ8dLyYtq9c9H8Q3qGmkfnMeJa2fMqz2vJl8TW5ek22Y9r/taZyrWrkQFRMgaJe 4CQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760797310; x=1761402110; 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=NYwvvMkNsizkLkVR3lZyOmzWt267ljepeEAqISwgDss=; b=Pakw+6Vi0IPQ+HTnC5iDuptdEo17j5ZmUmHGu8she1/yzxVltdwE/RoOF3mEwwwKTL eOO3xqPjPIKZfW3Q8TgaJdIXYhagEPEK9tnJ1YLjKsMS4stbxY46voNWBMhWGdF1mRPg RdNWCuBvCsJulh+gFXx/y0g+LR/A5YOs3lzwzzp/7Xb1WchI3THuuGDg53SWSmstplSM fZFdI6VyDWLNDE2R45vjaHc825q2idufWfKFcGWLK4WL9EUulIIS14y0/0CUEjdMVLoS Gx7D/lsRr7caB9SQ5lbmI85mHiqahv5VfOGRK9kmLYLSFbzmMvThLy3OOf/qsd+NNCBt CknA== X-Forwarded-Encrypted: i=1; AJvYcCV9QhnvBsmyUnquHvXwmKlnNg6uGjjMfug05vluAErL5A42SV/C67Un230ccw8mUn1YP06P3eojpJEKdFc=@vger.kernel.org X-Gm-Message-State: AOJu0Yx7K32wXDVoeL+Q+xBDjp+wvHwiUzWYfBZW3N1zWvCiCYRSl9qy XrmX2GBH4nK/6m1nmtInSjcaHjOQypJbgHXkAV1ClbniX8j7tGfJkZ4w X-Gm-Gg: ASbGnct8qioS17YaDTrxPNJYceqPAUyWUUhz8VasIufQafoFn+Gu32H+2E99NWeufyK nHZqwzouaYCFErUKKV8k+vRV11XN/345Hdvg95YphG1t8eVa3bp6OWfuvcOZ2FmufSVD2kHTJ1+ hGsudMb+Y393Y+PuTyEgcNzSpRRqXyJdPP1RtdJ1dlJi2FF6fwMfQP8rucYypgPH24D8lLktVSO oFgToc98w0NjXPa4KEAQ+1gAYGlyjmyEOQqyTwNeyOvduPLIgPIOPHXGkKShYy9wIkUdSwwmHcs +tK/ZBYaOK3/jHpIxlkOCxHIyj205ces7oW+R6syQ+zBm2Isxpj5u5Ti4aNWYmpEuZb/446X65p ytv5JQDDTtlcm+KYmRR1o+qMv9ZShhSjakFAxcoClABrxNY3oH8eTClvMVduOXZyCK6WZKrtuPj NrA9vUYqqQXKk= X-Google-Smtp-Source: AGHT+IE88gDZ5ZuEI9rRpvXV/ekTZy+PSm2f/QXfC+JY64iWLG600+Hr2zdXGf21CmwOwy+JFCe24w== X-Received: by 2002:a05:6a00:2e99:b0:781:c54:4d24 with SMTP id d2e1a72fcca58-7a220ab6c7bmr7997114b3a.21.1760797309777; Sat, 18 Oct 2025 07:21:49 -0700 (PDT) Received: from 7950hx ([43.129.244.20]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7a23010d818sm2913589b3a.53.2025.10.18.07.21.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Oct 2025 07:21:49 -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, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH RFC bpf-next 2/5] bpf: add kfunc bpf_tracing_is_exit for TRACE_SESSION Date: Sat, 18 Oct 2025 22:21:21 +0800 Message-ID: <20251018142124.783206-3-dongml2@chinatelecom.cn> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251018142124.783206-1-dongml2@chinatelecom.cn> References: <20251018142124.783206-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 before the function argument count, which means ctx[-2]. 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. Signed-off-by: Menglong Dong Co-developed-by: Leon Hwang Signed-off-by: Leon Hwang --- kernel/bpf/verifier.c | 5 ++++- kernel/trace/bpf_trace.c | 43 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 40e3274e8bc2..a1db11818d01 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -12284,6 +12284,7 @@ enum special_kfunc_type { KF___bpf_trap, KF_bpf_task_work_schedule_signal, KF_bpf_task_work_schedule_resume, + KF_bpf_tracing_is_exit, }; =20 BTF_ID_LIST(special_kfunc_list) @@ -12356,6 +12357,7 @@ 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) =20 static bool is_task_work_add_kfunc(u32 func_id) { @@ -12410,7 +12412,8 @@ 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]) return KF_ARG_PTR_TO_CTX; =20 /* In this function, we verify the kfunc's BTF as per the argument type, diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 4f87c16d915a..6dde48b9d27f 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -3356,12 +3356,49 @@ 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) +{ + /* ctx[-2] is the session flags, and the last bit is is_exit */ + return ((u64 *)ctx)[-2] & 1; +} + +__bpf_kfunc_end_defs(); + +BTF_KFUNCS_START(tracing_kfunc_set_ids) +BTF_ID_FLAGS(func, bpf_tracing_is_exit) +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) { - return register_btf_kfunc_id_set(BPF_PROG_TYPE_KPROBE, &bpf_kprobe_multi_= kfunc_set); + 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.0 From nobody Sat Feb 7 08:23:08 2026 Received: from mail-pf1-f196.google.com (mail-pf1-f196.google.com [209.85.210.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 5937F1F63D9 for ; Sat, 18 Oct 2025 14:21:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.196 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760797317; cv=none; b=KdBAaO23+XovCunElhGq0yeArpp4rAupsbt3DpjNhmvMSI7gChdO6yKGOLBKLujD2hbUOxZ6jngTHkLuLA2CTSWivTdiYIuBqO3xVjnz1Cr7/iVqhK2V3WxotobCrMjmrOIqpNPKitJqFJm5/c6xRj9PJp/QBUzfYD5iFDHUuFE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760797317; c=relaxed/simple; bh=WcS0cISeU0EB2e5zpIl+V1jFsW9ukANz44z7QELkexs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TV/yymWzLAT8Um6OPBijGSPp8GBVG3q5R9MbrA6K8m9iAqte8wrSF4YnRcAeHmBm5MJhfHY2Rdp3wolbwnhU3LzXeXX/PBu19qiUdmH2DLCwf5GUZ6gTIRgVKRbK49QukgY5LynRQCAQPFIqK93gSWmsR06p3ugJcXgJPcMKLLk= 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=PpDE45Jm; arc=none smtp.client-ip=209.85.210.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="PpDE45Jm" Received: by mail-pf1-f196.google.com with SMTP id d2e1a72fcca58-781206cce18so2984871b3a.0 for ; Sat, 18 Oct 2025 07:21:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1760797315; x=1761402115; 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=YTklFQfNZ228hGLJV/pCwVZEy0WyML+yDbo3ZhiifPY=; b=PpDE45JmMmmUTPya2JZTuw41jIv82iJSEZ86rrmkh+NIGfaPcRrhlUhN+BD9L12zDJ Xgs9V+ecE8lO7Jj4n2wAPzGw753FGyOztl3egILzneEQOcGul8bs5onZJEODqcvLjVLI pGgF5eXnKcNWJxZPxK1m1m6eIJZKxCxcb6KkiG6DMaqF7OjrbN7quvmZBy9jpOTBNhtW Sc39ZsOSNoNtm/rQ6twEGIm9WFceHrGo6PB0VbdiPB1eyQseUTJmXZnTNeiw6ePgruJM BxGaxPQD1DLB9A3gs60+uivmCpVC4Alyvxas7ErMg5W1vmhXkCpmRjSo7DHmnSJre4Ku bgYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760797315; x=1761402115; 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=YTklFQfNZ228hGLJV/pCwVZEy0WyML+yDbo3ZhiifPY=; b=tE3Up581u70qXu/Ed9iyKqDUZWByKCPj+uaUEFaFHzgNOYRh15VP13NMcCPHTLhLQy FbCV0jRWGL6ccLYlvkFQkcLd4wDGb97T0jnMcsjwIS8ZZvVsAaG8ZRLACDuZ2zvDJBgh hPQQGvBoNZzEVnlIDSX6bGE4zA0pjLEI2IrrCx1fSYnPSlmx1ANa73hWvggQ+t/WTJdz 7gE+Pd34W99OAj9iZPU2HjfK/4QiGCECQrAe4tvU3y3sE4k+93MtXwo9G0XA+qy2ig+/ Thb8dYlE5Cl42ejyQo/WHenzNsPAbLyf1nQQvQi8K+AODTuTycY74Cnr65XbhCPT3jmP 7IjA== X-Forwarded-Encrypted: i=1; AJvYcCWUHPEF7c1frYwM/RBWF+8xVd69wlJ/AZLF+ZXoasZlLjh5RpDNXvOTA18qsl3pDQlEv9XeH/fTIc3XDOU=@vger.kernel.org X-Gm-Message-State: AOJu0YyLMnjKYNKCyqD54QXhYxe63QPAP4TolPaSlJ5HIa1agz8WIzCB VKRmUq4GuGrbN2PYFb6WYuvVL8DI2yt0M+nM1pbCuDG5rVud8gfNn00w X-Gm-Gg: ASbGnctWvdt/cnRxWR4KiIw6gl9OYU2o+3iLeoB5vw5NJhJUz/Vb70SAUKOkbqvB/IG 6e/dM8giZJniQYGd2mAO8Ckuh68CEnouDmGyCtW+YqntP66z9TvKQlK4FN6dJ7W1auVU0bPxBqZ gf03ePteIFRfiUFnmk8n/jaHo/oPHExKHGZylwdMxc0jmoLv2vqqQZquU0pGIP7gNHFx/z6XrsA 32Y9LGeTGOY0KHD2uWP4EVYFAdt1WSFGuhhkhhzLfOK/gxiLh//Bc6TE+SGO4dhfuhVJPypqwoT nH43nHmdu3gATwNzsUIfJ1Wbhm1Jp1bmSwsIWjqwQ2jDQm4xyHsD1b4AiDoVTVM2130RsBlxPfk zBdKHQmUZVgEGbwgNUQX5nCkqt6XEtNzkftRcSJB8Y8I0eOH2ZTWL9jVI4khreHyLDqqGF9VA6t xzUWhWTJ/+pLQ= X-Google-Smtp-Source: AGHT+IFiyeFV4ZjR0KYrd7quycgpOQAPP9ZCPLUVqrumP5YZutEAczLmSZCJGuRx899rUbbRobXMFQ== X-Received: by 2002:a05:6a20:9149:b0:309:48d8:cf1d with SMTP id adf61e73a8af0-334a78fdda8mr10451087637.18.1760797315482; Sat, 18 Oct 2025 07:21:55 -0700 (PDT) Received: from 7950hx ([43.129.244.20]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7a23010d818sm2913589b3a.53.2025.10.18.07.21.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Oct 2025 07:21:55 -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, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH RFC bpf-next 3/5] bpf,x86: add tracing session supporting for x86_64 Date: Sat, 18 Oct 2025 22:21:22 +0800 Message-ID: <20251018142124.783206-4-dongml2@chinatelecom.cn> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251018142124.783206-1-dongml2@chinatelecom.cn> References: <20251018142124.783206-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 BPF_TRACE_SESSION supporting to x86_64. invoke_bpf_session_entry and invoke_bpf_session_exit is introduced for this purpose. In invoke_bpf_session_entry(), we will check if the return value of the fentry is 0, and clear the corresponding flag if not. And in invoke_bpf_session_exit(), we will check if the corresponding flag is set. If not set, the fexit will be skipped. Signed-off-by: Menglong Dong Co-developed-by: Leon Hwang Signed-off-by: Leon Hwang --- arch/x86/net/bpf_jit_comp.c | 115 +++++++++++++++++++++++++++++++++++- 1 file changed, 114 insertions(+), 1 deletion(-) diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index d4c93d9e73e4..0586b96ed529 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -3108,6 +3108,97 @@ static int invoke_bpf_mod_ret(const struct btf_func_= model *m, u8 **pprog, return 0; } =20 +static int invoke_bpf_session_entry(const struct btf_func_model *m, u8 **p= prog, + struct bpf_tramp_links *tl, int stack_size, + int run_ctx_off, int session_off, + void *image, void *rw_image) +{ + u64 session_flags; + u8 *prog =3D *pprog; + u8 *jmp_insn; + int i; + + /* clear the session flags: + * + * xor rax, rax + * mov QWORD PTR [rbp - session_off], rax + */ + EMIT3(0x48, 0x31, 0xC0); + emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_0, -session_off); + + for (i =3D 0; i < tl->nr_links; i++) { + if (invoke_bpf_prog(m, &prog, tl->links[i], stack_size, run_ctx_off, tru= e, + image, rw_image)) + return -EINVAL; + + /* fentry prog stored return value into [rbp - 8]. Emit: + * if (*(u64 *)(rbp - 8) !=3D 0) + * *(u64 *)(rbp - session_off) |=3D (1 << (i + 1)); + */ + /* cmp QWORD PTR [rbp - 0x8], 0x0 */ + EMIT4(0x48, 0x83, 0x7d, 0xf8); EMIT1(0x00); + /* emit 2 nops that will be replaced with JE insn */ + jmp_insn =3D prog; + emit_nops(&prog, 2); + + session_flags =3D (1ULL << (i + 1)); + /* mov rax, $session_flags */ + emit_mov_imm64(&prog, BPF_REG_0, session_flags >> 32, (u32) session_flag= s); + /* or QWORD PTR [rbp - session_off], rax */ + EMIT2(0x48, 0x09); + emit_insn_suffix(&prog, BPF_REG_FP, BPF_REG_0, -session_off); + + jmp_insn[0] =3D X86_JE; + jmp_insn[1] =3D prog - jmp_insn - 2; + } + + *pprog =3D prog; + return 0; +} + +static int invoke_bpf_session_exit(const struct btf_func_model *m, u8 **pp= rog, + struct bpf_tramp_links *tl, int stack_size, + int run_ctx_off, int session_off, + void *image, void *rw_image) +{ + u64 session_flags; + u8 *prog =3D *pprog; + u8 *jmp_insn; + int i; + + /* set the bpf_trace_is_exit flag to the session flags */ + /* mov rax, 1 */ + emit_mov_imm32(&prog, false, BPF_REG_0, 1); + /* or QWORD PTR [rbp - session_off], rax */ + EMIT2(0x48, 0x09); + emit_insn_suffix(&prog, BPF_REG_FP, BPF_REG_0, -session_off); + + for (i =3D 0; i < tl->nr_links; i++) { + /* check if (1 << (i+1)) is set in the session flags, and + * skip the execution of the fexit program if it is. + */ + session_flags =3D 1ULL << (i + 1); + /* mov rax, $session_flags */ + emit_mov_imm64(&prog, BPF_REG_1, session_flags >> 32, (u32) session_flag= s); + /* test QWORD PTR [rbp - session_off], rax */ + EMIT2(0x48, 0x85); + emit_insn_suffix(&prog, BPF_REG_FP, BPF_REG_1, -session_off); + /* emit 2 nops that will be replaced with JE insn */ + jmp_insn =3D prog; + emit_nops(&prog, 2); + + if (invoke_bpf_prog(m, &prog, tl->links[i], stack_size, run_ctx_off, fal= se, + image, rw_image)) + return -EINVAL; + + jmp_insn[0] =3D X86_JNE; + jmp_insn[1] =3D prog - jmp_insn - 2; + } + + *pprog =3D prog; + return 0; +} + /* mov rax, qword ptr [rbp - rounded_stack_depth - 8] */ #define LOAD_TRAMP_TAIL_CALL_CNT_PTR(stack) \ __LOAD_TCC_PTR(-round_up(stack, 8) - 8) @@ -3179,8 +3270,10 @@ static int __arch_prepare_bpf_trampoline(struct bpf_= tramp_image *im, void *rw_im void *func_addr) { int i, ret, nr_regs =3D m->nr_args, stack_size =3D 0; - int regs_off, nregs_off, ip_off, run_ctx_off, arg_stack_off, rbx_off; + int regs_off, nregs_off, session_off, ip_off, run_ctx_off, + arg_stack_off, rbx_off; struct bpf_tramp_links *fentry =3D &tlinks[BPF_TRAMP_FENTRY]; + struct bpf_tramp_links *session =3D &tlinks[BPF_TRAMP_SESSION]; struct bpf_tramp_links *fexit =3D &tlinks[BPF_TRAMP_FEXIT]; struct bpf_tramp_links *fmod_ret =3D &tlinks[BPF_TRAMP_MODIFY_RETURN]; void *orig_call =3D func_addr; @@ -3222,6 +3315,8 @@ static int __arch_prepare_bpf_trampoline(struct bpf_t= ramp_image *im, void *rw_im * * RBP - nregs_off [ regs count ] always * + * RBP - session_off [ session flags ] tracing session + * * RBP - ip_off [ traced function ] BPF_TRAMP_F_IP_ARG flag * * RBP - rbx_off [ rbx value ] always @@ -3246,6 +3341,8 @@ static int __arch_prepare_bpf_trampoline(struct bpf_t= ramp_image *im, void *rw_im /* regs count */ stack_size +=3D 8; nregs_off =3D stack_size; + stack_size +=3D 8; + session_off =3D stack_size; =20 if (flags & BPF_TRAMP_F_IP_ARG) stack_size +=3D 8; /* room for IP address argument */ @@ -3345,6 +3442,13 @@ static int __arch_prepare_bpf_trampoline(struct bpf_= tramp_image *im, void *rw_im return -EINVAL; } =20 + if (session->nr_links) { + if (invoke_bpf_session_entry(m, &prog, session, regs_off, + run_ctx_off, session_off, + image, rw_image)) + return -EINVAL; + } + if (fmod_ret->nr_links) { branches =3D kcalloc(fmod_ret->nr_links, sizeof(u8 *), GFP_KERNEL); @@ -3409,6 +3513,15 @@ static int __arch_prepare_bpf_trampoline(struct bpf_= tramp_image *im, void *rw_im } } =20 + if (session->nr_links) { + if (invoke_bpf_session_exit(m, &prog, session, regs_off, + run_ctx_off, session_off, + image, rw_image)) { + ret =3D -EINVAL; + goto cleanup; + } + } + if (flags & BPF_TRAMP_F_RESTORE_REGS) restore_regs(m, &prog, regs_off); =20 --=20 2.51.0 From nobody Sat Feb 7 08:23:08 2026 Received: from mail-pf1-f193.google.com (mail-pf1-f193.google.com [209.85.210.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 41A721C28E for ; Sat, 18 Oct 2025 14:22:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.193 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760797323; cv=none; b=qMgUq9n9WhOHgRHd4TBEgjFUYI6P975QrGZD3c1jFydaPq0LOzBbHKIpn7KuQlSbd5wya9NzS4QBpo9XXciHAMNduy3+fTH/4Eo1eibKuw1HGMUCqlwHaK93vxVqud2d16F5iecw1pqwP3yvyL2AKrk0jM/ZZf/AgkDKKfQY2QI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760797323; c=relaxed/simple; bh=JKPhFCZ2Wzt7BBEmHO2+O8K1JfWX20E1BadKWktgF0k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rSZ2Mjh30j1Fj79YzRsJpwAe0zSZQQvFtgBTL08EoYRfDaXTWPFaI+dSNyNBa545bj5wlZurai4xcQYKMF7LWm7+bKioh2Woyp2ezPLs/tHyRMYxdUJS7mE5LfSyrKGpQpUsUgWzv/hcGRpOlz4Hmbw9NXfkdnfmd/6FSUTtafQ= 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=GZ2Azo1W; arc=none smtp.client-ip=209.85.210.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="GZ2Azo1W" Received: by mail-pf1-f193.google.com with SMTP id d2e1a72fcca58-7a213c3c3f5so3916692b3a.3 for ; Sat, 18 Oct 2025 07:22:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1760797321; x=1761402121; 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=xTgng+JyAPm5fFCGDOYluLNqW4oXLGf4l2RyL+9kLZs=; b=GZ2Azo1W6m0Cr00/OUykd77R6nZ89QqJgHsS5f9luRgInIWWaf02X1zCrAKvn/b+JP aj3WVxRCnHGEz5mvOAwLyqEf88/9glfQ2AJ+WxXrR1dsbNHWQ4/w78Ik+ulHZ9PMVGfd idUhdCWDtPcqD5nzOOHK9PUEJl351JgyswLykDcSQ/MdjZRUCZbM8WmJvR/Xggfau5Hr 1ZeedhkW7Xr6fmRT0ULOHKPfJ/+wBr6xPjIT+W5TwW7jO6Eptn6s/WxxmAxUsG62f1G2 XPmSdfSLUeNRsTQ4xApdSRMsIDRwIsVn7tawhg8hN6MsoKltz1HC0cN29DEy698rUElG hrEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760797321; x=1761402121; 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=xTgng+JyAPm5fFCGDOYluLNqW4oXLGf4l2RyL+9kLZs=; b=KcQqPdCnBmnYh/6D5nT+KRmYnqmInQnwAaHy0Vh0vt+6l2G7dOmfuZXjD/sIcX9pMZ F90I4+6gQU53tDAGIswuHRwu7OoBHU6Vh2yVQzOALlt1Nb33B4H+lBtzqlwWddJbgqUD RF46bYrHOh2ay3WtTbhH9LaeJkbI3dBQ2miSWumFFrwhFEtRvxBMQwhuzH734WPmg3Vn QBVCtAM8k2qJcWy2IBZ+PuRn4wZTWCuydHh/XJ/akKHgMMQaVLXKQSQ80s7qGLN5mzko jgDyuxYzh9JB9sun23br2cm36V6HJBPEZKKedJ1fOn5A8tx9rh4tFAvsTlBs43Gom8yQ flvQ== X-Forwarded-Encrypted: i=1; AJvYcCX95ivu1KrMUlWQrKbVI8eG0sO112huZkuGD2JGyHCEiQieDropx9nfUSpIyGiS3sgNSoxOGcIldQSJfiI=@vger.kernel.org X-Gm-Message-State: AOJu0YwoSZOtnqsaDsw+NzAX7WARYG1qH4EwkmsmCURYgKdih/80xlEd xXO4vduHHOEgoG0loW6EirONhsleGIyI37Ngmp0FS3+TE719DnS7lSF0 X-Gm-Gg: ASbGncs73Lt/U/nIjwqvAYmZm4f7jYv7xJnbB/7fLOEu6Dlz8prw85CB/DuilZkXNZw rUvH4rzDiPYVUmDvF9FtO4CfwNc/PYU+JCEsj0U/o+8ZTXLjJDCg5/4I9/vrb6ea5foRfImsMQ0 2RizakBrp9W69NP3Pd5uV5Efn89y9ItEozZbRVLwurWBvs1K1dAuwjqQVbc+w5Cnonnn/j4RFdY kaHSiONenhHLoYaJbgdnOOyEUJ1w6Ph/7KbEdxcLNi4akgpPuggog7YCnPuEd+ur4rD79/lkasV MfNKjhDaiDE+NbEY/A9sltSEPFI/3UTxdkkC/nSMUU79O8uX2GBcmZrs6y7oJqzHQuDxpdouex1 8K2gfb0a4Q9MTICD0M1p+9sFahwvtJ1rAJjDqCvd9WP2+ozN4hSmrHUSG/YwCOKmo6s6IUKNVnj aJ X-Google-Smtp-Source: AGHT+IFm9xR2qdw43jegcye4ND/f40zSsvNQsshT2VKSlXk2RtQ50rm4cgIWORQV6xJDs47kUiNJtQ== X-Received: by 2002:a05:6a00:a589:b0:77d:51e5:e5d1 with SMTP id d2e1a72fcca58-7a220d2328emr8535692b3a.19.1760797321344; Sat, 18 Oct 2025 07:22:01 -0700 (PDT) Received: from 7950hx ([43.129.244.20]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7a23010d818sm2913589b3a.53.2025.10.18.07.21.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Oct 2025 07:22:01 -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, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH RFC bpf-next 4/5] libbpf: add support for tracing session Date: Sat, 18 Oct 2025 22:21:23 +0800 Message-ID: <20251018142124.783206-5-dongml2@chinatelecom.cn> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251018142124.783206-1-dongml2@chinatelecom.cn> References: <20251018142124.783206-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 BPF_TRACE_SESSION to libbpf and bpftool. Signed-off-by: Menglong Dong --- tools/bpf/bpftool/common.c | 1 + tools/lib/bpf/bpf.c | 2 ++ tools/lib/bpf/libbpf.c | 3 +++ 3 files changed, 6 insertions(+) diff --git a/tools/bpf/bpftool/common.c b/tools/bpf/bpftool/common.c index e8daf963ecef..534be6cfa2be 100644 --- a/tools/bpf/bpftool/common.c +++ b/tools/bpf/bpftool/common.c @@ -1191,6 +1191,7 @@ const char *bpf_attach_type_input_str(enum bpf_attach= _type t) case BPF_TRACE_FENTRY: return "fentry"; case BPF_TRACE_FEXIT: return "fexit"; case BPF_MODIFY_RETURN: return "mod_ret"; + case BPF_TRACE_SESSION: return "fsession"; case BPF_SK_REUSEPORT_SELECT: return "sk_skb_reuseport_select"; case BPF_SK_REUSEPORT_SELECT_OR_MIGRATE: return "sk_skb_reuseport_select_= or_migrate"; default: return libbpf_bpf_attach_type_str(t); diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index 339b19797237..caed2b689068 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -794,6 +794,7 @@ int bpf_link_create(int prog_fd, int target_fd, case BPF_TRACE_FENTRY: case BPF_TRACE_FEXIT: case BPF_MODIFY_RETURN: + case BPF_TRACE_SESSION: case BPF_LSM_MAC: attr.link_create.tracing.cookie =3D OPTS_GET(opts, tracing.cookie, 0); if (!OPTS_ZEROED(opts, tracing)) @@ -917,6 +918,7 @@ int bpf_link_create(int prog_fd, int target_fd, case BPF_TRACE_FENTRY: case BPF_TRACE_FEXIT: case BPF_MODIFY_RETURN: + case BPF_TRACE_SESSION: return bpf_raw_tracepoint_open(NULL, prog_fd); default: return libbpf_err(err); diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index dd3b2f57082d..e582620cd097 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -115,6 +115,7 @@ static const char * const attach_type_name[] =3D { [BPF_TRACE_FENTRY] =3D "trace_fentry", [BPF_TRACE_FEXIT] =3D "trace_fexit", [BPF_MODIFY_RETURN] =3D "modify_return", + [BPF_TRACE_SESSION] =3D "trace_session", [BPF_LSM_MAC] =3D "lsm_mac", [BPF_LSM_CGROUP] =3D "lsm_cgroup", [BPF_SK_LOOKUP] =3D "sk_lookup", @@ -9607,6 +9608,8 @@ static const struct bpf_sec_def section_defs[] =3D { SEC_DEF("fentry.s+", TRACING, BPF_TRACE_FENTRY, SEC_ATTACH_BTF | SEC_SLE= EPABLE, attach_trace), SEC_DEF("fmod_ret.s+", TRACING, BPF_MODIFY_RETURN, SEC_ATTACH_BTF | SEC_= SLEEPABLE, attach_trace), SEC_DEF("fexit.s+", TRACING, BPF_TRACE_FEXIT, SEC_ATTACH_BTF | SEC_SLEEP= ABLE, attach_trace), + SEC_DEF("fsession+", TRACING, BPF_TRACE_SESSION, SEC_ATTACH_BTF, attach_= trace), + SEC_DEF("fsession.s+", TRACING, BPF_TRACE_SESSION, SEC_ATTACH_BTF | SEC_= SLEEPABLE, attach_trace), SEC_DEF("freplace+", EXT, 0, SEC_ATTACH_BTF, attach_trace), SEC_DEF("lsm+", LSM, BPF_LSM_MAC, SEC_ATTACH_BTF, attach_lsm), SEC_DEF("lsm.s+", LSM, BPF_LSM_MAC, SEC_ATTACH_BTF | SEC_SLEEPABLE, atta= ch_lsm), --=20 2.51.0 From nobody Sat Feb 7 08:23:08 2026 Received: from mail-pf1-f196.google.com (mail-pf1-f196.google.com [209.85.210.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 E8F322E1722 for ; Sat, 18 Oct 2025 14:22:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.196 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760797329; cv=none; b=DM1FvNh5N095Sb+RZ2U9DfzD/cj1MDb+t1kvQiWzED0YEqFKU0v2HyplnqcGxzIF0QZu/1vwC0Xpftj8vwxjCDmTB2T6WaGlwQ+OaPBLUl4bfndQKkMiZYkuq3jLsvDh5f+oV91EivmP0M3FCrxb2breBAxOsfZ5RJHDaWcKjGk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760797329; c=relaxed/simple; bh=kXsLBaOuiWzTxOYqwGnvvLF5/8s/zxozt6M4TBM1QdA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iVqWto6WG6rfr4/e059tX2PFt6xCmPiQpiNIDzTWZz8uBL1gZInKwkaqA1ZlSkIeCPfABLL9BTcjEvB7wYA0Ufv7qxtoy/11EPJCDPYiZZMBJdSy00sLWL2xi0ssK3jMrW6w0w5iLFhyIih0+Qfak3tQ0KA1+95Mf/UD0fd1DcQ= 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=C//25bkJ; arc=none smtp.client-ip=209.85.210.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="C//25bkJ" Received: by mail-pf1-f196.google.com with SMTP id d2e1a72fcca58-76e2ea933b7so2722032b3a.1 for ; Sat, 18 Oct 2025 07:22:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1760797327; x=1761402127; 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=2jT5zqdbDY8x74jTyYOhN+JPqZ1LIOOhcVlrRdTY1/4=; b=C//25bkJ6+qadg2daRdwmKMUMOCyEAV8lHInF2CBkYFk68VJy83Xp1JK4uNPevsRbx Shqaib9lLYCm0zPJId+DSQyoL4ayNMbQrDp0WiF/GD9iSpbuJk0aHuX+a0j1oSDEEO13 EpIJfo7eLjBx9oIiXdMy0suMWX2QuObiAyv55P9pI0gSuv57bIhxXVf4kFRYU9UgHfgh q5xKDJW93EGdwqkiJqo/sRNF2yK20ei2PY6FVJnuHTKvBT9JmZjAfYwUXq9IddCGxD3p tB+qAYrrny+eSN7FKb05rm2jBEHY/YzJtgX1ryiLNS6RI2hF0q6J+G3HkZnomrux/Heh 0JuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760797327; x=1761402127; 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=2jT5zqdbDY8x74jTyYOhN+JPqZ1LIOOhcVlrRdTY1/4=; b=GmFzu8B8Am3x1dgrUvnkCqUIE1O55kmSoe2sLc9vqcFJSbBwJc7PzHbe7fRJzBYWKs nR+xSnb1rJvNq7vKol3vQ+lkN57vtHmwvsGMh52tW5ugZM1cAlCViZ+eyFeikQRu/8uK Tv4PCtlGOV6+OA1s5Pk4dLmwlEppzCdRQlfmfOcBTvrwzk1ARm7WVoOslq6Z65BEuS+N UWeFkgirz5Fg2Sqb2pZVH2Dlv0iUbQshNuzb8kjj1VWN/KGUa7rxNrOISu40Umoz8lwa iDQmM0wBw92xOhw0yTHSOaK4etgxotberAor2xGuVP5NCBx1ungRdhq7HvH097UH54ZQ h4Ww== X-Forwarded-Encrypted: i=1; AJvYcCW2ngo3/048nxHpHT4cvu102oFp8esAIl7fT54F7Eqza1BJMWAsyWUbSlZYoKA85CPmgTQOXAUM8FzexyQ=@vger.kernel.org X-Gm-Message-State: AOJu0YzmgPR+HRacS4xdoPvJtkcQFazUzgWRMyzJ3ltUf9PUni/GC78j yDVQdIl7nYvm3/jpeBXft4RztKKbpdvQ0scipnEvK0q3UB4yoP2X6ErS X-Gm-Gg: ASbGnctK+8ZRxlre3naJBtDtBUuwE8+KxsSbc/yMP+aI/FaAFkPL+Zmnkij5LcTIA7n yxD6IOAMGxHlo1lzSNkslRv6JP0fO3mB/P5Yb6ahRmNT2K/7Tt1lof29gVLtuGKTgGFfSBB7SRL ByA+R5PNvy+Tn6QO2BtbfYw3dmThJEJxZjBSIw84D7XP76SqB1EWeH0uo47W7dp3W9lEWnGFpBJ IncrfHYTM9fP1KGNUvcx5rEg3USlna0ydUs6GaTgSqCajnrMD71bHy9q6d2suIQJMoc0M8v/T0F rJuLKFHqcFNsNQT0Ay484E3AU73uH0efbVVqw0VQme49FDjZ6DhZRviYpTiYw+JoK+psTQxEJaC wUslmCf8X0gwIj83NSncEWALvKgJi6952RxBUkuy1UMsOJpvQnokhUPuKOtExkQ/EOfNC1+2ubK KbVc8niFhb6hQ= X-Google-Smtp-Source: AGHT+IF8mZYG06kBMFtFukpoGor04JUZ4l0MlY/GtJi7DQKN6Uz0Qp4laos6d3KuLxChec3J3KhCrA== X-Received: by 2002:a05:6a00:17a0:b0:78c:a3a6:a1bf with SMTP id d2e1a72fcca58-7a21f96567fmr8686639b3a.7.1760797326983; Sat, 18 Oct 2025 07:22:06 -0700 (PDT) Received: from 7950hx ([43.129.244.20]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7a23010d818sm2913589b3a.53.2025.10.18.07.22.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Oct 2025 07:22:06 -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, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH RFC bpf-next 5/5] selftests/bpf: add testcases for tracing session Date: Sat, 18 Oct 2025 22:21:24 +0800 Message-ID: <20251018142124.783206-6-dongml2@chinatelecom.cn> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251018142124.783206-1-dongml2@chinatelecom.cn> References: <20251018142124.783206-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 testcases for BPF_TRACE_SESSION. Signed-off-by: Menglong Dong --- .../selftests/bpf/prog_tests/fsession_test.c | 136 +++++++++++++ .../selftests/bpf/progs/fsession_test.c | 178 ++++++++++++++++++ 2 files changed, 314 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/fsession_test.c create mode 100644 tools/testing/selftests/bpf/progs/fsession_test.c diff --git a/tools/testing/selftests/bpf/prog_tests/fsession_test.c b/tools= /testing/selftests/bpf/prog_tests/fsession_test.c new file mode 100644 index 000000000000..e2913da57b38 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/fsession_test.c @@ -0,0 +1,136 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 ChinaTelecom */ +#include +#include "fsession_test.skel.h" + +static void test_fsession_basic(void) +{ + LIBBPF_OPTS(bpf_test_run_opts, topts); + struct fsession_test *skel =3D NULL; + int err, prog_fd; + + skel =3D fsession_test__open_and_load(); + if (!ASSERT_OK_PTR(skel, "fsession_test__open_and_load")) + goto cleanup; + + err =3D fsession_test__attach(skel); + if (!ASSERT_OK(err, "fsession_attach")) + goto cleanup; + + /* Trigger test function calls */ + prog_fd =3D bpf_program__fd(skel->progs.test1); + err =3D bpf_prog_test_run_opts(prog_fd, &topts); + if (!ASSERT_OK(err, "test_run_opts err")) + return; + if (!ASSERT_OK(topts.retval, "test_run_opts retval")) + return; + + /* Verify test1: both entry and exit are called */ + ASSERT_EQ(skel->bss->test1_entry_called, 1, "test1_entry_called"); + ASSERT_EQ(skel->bss->test1_exit_called, 1, "test1_exit_called"); + ASSERT_EQ(skel->bss->test1_entry_result, 1, "test1_entry_result"); + ASSERT_EQ(skel->bss->test1_exit_result, 1, "test1_exit_result"); + + /* Verify test2: entry is called but exit is blocked */ + ASSERT_EQ(skel->bss->test2_entry_called, 1, "test2_entry_called"); + ASSERT_EQ(skel->bss->test2_exit_called, 0, "test2_exit_not_called"); + ASSERT_EQ(skel->bss->test2_entry_result, 1, "test2_entry_result"); + ASSERT_EQ(skel->bss->test2_exit_result, 0, "test2_exit_result"); + + /* Verify test3: both entry and exit are called */ + ASSERT_EQ(skel->bss->test3_entry_called, 1, "test3_entry_called"); + ASSERT_EQ(skel->bss->test3_exit_called, 1, "test3_exit_called"); + ASSERT_EQ(skel->bss->test3_entry_result, 1, "test3_entry_result"); + ASSERT_EQ(skel->bss->test3_exit_result, 1, "test3_exit_result"); + + /* Verify test4: both entry and exit are called */ + ASSERT_EQ(skel->bss->test4_entry_called, 1, "test4_entry_called"); + ASSERT_EQ(skel->bss->test4_exit_called, 1, "test4_exit_called"); + ASSERT_EQ(skel->bss->test4_entry_result, 1, "test4_entry_result"); + ASSERT_EQ(skel->bss->test4_exit_result, 1, "test4_exit_result"); + + /* Verify test5: both entry and exit are called */ + ASSERT_EQ(skel->bss->test5_entry_called, 1, "test5_entry_called"); + ASSERT_EQ(skel->bss->test5_exit_called, 1, "test5_exit_called"); + ASSERT_EQ(skel->bss->test5_entry_result, 1, "test5_entry_result"); + ASSERT_EQ(skel->bss->test5_exit_result, 1, "test5_exit_result"); + + /* Verify test6: entry is called but exit is blocked */ + ASSERT_EQ(skel->bss->test6_entry_called, 1, "test6_entry_called"); + ASSERT_EQ(skel->bss->test6_exit_called, 0, "test6_exit_not_called"); + ASSERT_EQ(skel->bss->test6_entry_result, 1, "test6_entry_result"); + ASSERT_EQ(skel->bss->test6_exit_result, 0, "test6_exit_result"); + + /* Verify test7: entry is called but exit is blocked */ + ASSERT_EQ(skel->bss->test7_entry_called, 1, "test7_entry_called"); + ASSERT_EQ(skel->bss->test7_exit_called, 0, "test7_exit_not_called"); + ASSERT_EQ(skel->bss->test7_entry_result, 1, "test7_entry_result"); + ASSERT_EQ(skel->bss->test7_exit_result, 0, "test7_exit_result"); + +cleanup: + fsession_test__destroy(skel); +} + +static void test_fsession_reattach(void) +{ + struct fsession_test *skel =3D NULL; + int err, prog_fd; + LIBBPF_OPTS(bpf_test_run_opts, topts); + + skel =3D fsession_test__open_and_load(); + if (!ASSERT_OK_PTR(skel, "fsession_test__open_and_load")) + goto cleanup; + + /* First attach */ + err =3D fsession_test__attach(skel); + if (!ASSERT_OK(err, "fsession_first_attach")) + goto cleanup; + + /* Trigger test function calls */ + prog_fd =3D bpf_program__fd(skel->progs.test1); + err =3D bpf_prog_test_run_opts(prog_fd, &topts); + if (!ASSERT_OK(err, "test_run_opts err")) + return; + if (!ASSERT_OK(topts.retval, "test_run_opts retval")) + return; + + /* Verify first call */ + ASSERT_EQ(skel->bss->test1_entry_called, 1, "test1_entry_first"); + ASSERT_EQ(skel->bss->test1_exit_called, 1, "test1_exit_first"); + + /* Detach */ + fsession_test__detach(skel); + + /* Reset counters */ + memset(skel->bss, 0, sizeof(*skel->bss)); + + /* Second attach */ + err =3D fsession_test__attach(skel); + if (!ASSERT_OK(err, "fsession_second_attach")) + goto cleanup; + + err =3D bpf_prog_test_run_opts(prog_fd, &topts); + if (!ASSERT_OK(err, "test_run_opts err")) + return; + if (!ASSERT_OK(topts.retval, "test_run_opts retval")) + return; + + /* Verify second call */ + ASSERT_EQ(skel->bss->test1_entry_called, 1, "test1_entry_second"); + ASSERT_EQ(skel->bss->test1_exit_called, 1, "test1_exit_second"); + +cleanup: + fsession_test__destroy(skel); +} + +void test_fsession_test(void) +{ +#if !defined(__x86_64__) + test__skip(); + return; +#endif + if (test__start_subtest("fsession_basic")) + test_fsession_basic(); + if (test__start_subtest("fsession_reattach")) + test_fsession_reattach(); +} diff --git a/tools/testing/selftests/bpf/progs/fsession_test.c b/tools/test= ing/selftests/bpf/progs/fsession_test.c new file mode 100644 index 000000000000..cce2b32f7c2c --- /dev/null +++ b/tools/testing/selftests/bpf/progs/fsession_test.c @@ -0,0 +1,178 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 ChinaTelecom */ +#include +#include +#include + +char _license[] SEC("license") =3D "GPL"; + +__u64 test1_entry_result =3D 0; +__u64 test1_exit_result =3D 0; +__u64 test1_entry_called =3D 0; +__u64 test1_exit_called =3D 0; + +SEC("fsession/bpf_fentry_test1") +int BPF_PROG(test1, int a) +{ + bool is_exit =3D bpf_tracing_is_exit(ctx); + + if (!is_exit) { + /* This is entry */ + test1_entry_called =3D 1; + test1_entry_result =3D a =3D=3D 1; + return 0; /* Return 0 to allow exit to be called */ + } + + /* This is exit */ + test1_exit_called =3D 1; + test1_exit_result =3D a =3D=3D 1; + return 0; +} + +__u64 test2_entry_result =3D 0; +__u64 test2_exit_result =3D 0; +__u64 test2_entry_called =3D 0; +__u64 test2_exit_called =3D 0; + +SEC("fsession/bpf_fentry_test2") +int BPF_PROG(test2, int a, __u64 b) +{ + bool is_exit =3D bpf_tracing_is_exit(ctx); + + if (!is_exit) { + /* This is entry */ + test2_entry_called =3D 1; + test2_entry_result =3D a =3D=3D 2 && b =3D=3D 3; + return 1; /* Return non-zero value to block exit call */ + } + + /* This is exit - should not be called due to blocking */ + test2_exit_called =3D 1; + test2_exit_result =3D a =3D=3D 2 && b =3D=3D 3; + return 0; +} + +__u64 test3_entry_result =3D 0; +__u64 test3_exit_result =3D 0; +__u64 test3_entry_called =3D 0; +__u64 test3_exit_called =3D 0; + +SEC("fsession/bpf_fentry_test3") +int BPF_PROG(test3, char a, int b, __u64 c) +{ + bool is_exit =3D bpf_tracing_is_exit(ctx); + + if (!is_exit) { + /* This is entry */ + test3_entry_called =3D 1; + test3_entry_result =3D a =3D=3D 4 && b =3D=3D 5 && c =3D=3D 6; + return 0; /* Allow exit to be called */ + } + + /* This is exit */ + test3_exit_called =3D 1; + test3_exit_result =3D a =3D=3D 4 && b =3D=3D 5 && c =3D=3D 6; + return 0; +} + +__u64 test4_entry_result =3D 0; +__u64 test4_exit_result =3D 0; +__u64 test4_entry_called =3D 0; +__u64 test4_exit_called =3D 0; + +SEC("fsession/bpf_fentry_test4") +int BPF_PROG(test4, void *a, char b, int c, __u64 d) +{ + bool is_exit =3D bpf_tracing_is_exit(ctx); + + if (!is_exit) { + /* This is entry */ + test4_entry_called =3D 1; + test4_entry_result =3D a =3D=3D (void *)7 && b =3D=3D 8 && c =3D=3D 9 &&= d =3D=3D 10; + return 0; /* Allow exit to be called */ + } + + /* This is exit */ + test4_exit_called =3D 1; + test4_exit_result =3D a =3D=3D (void *)7 && b =3D=3D 8 && c =3D=3D 9 && d= =3D=3D 10; + return 0; +} + +__u64 test5_entry_result =3D 0; +__u64 test5_exit_result =3D 0; +__u64 test5_entry_called =3D 0; +__u64 test5_exit_called =3D 0; + +SEC("fsession/bpf_fentry_test7") +int BPF_PROG(test5, struct bpf_fentry_test_t *arg) +{ + bool is_exit =3D bpf_tracing_is_exit(ctx); + + if (!is_exit) { + /* This is entry */ + test5_entry_called =3D 1; + if (!arg) + test5_entry_result =3D 1; + return 0; /* Allow exit to be called */ + } + + /* This is exit */ + test5_exit_called =3D 1; + if (!arg) + test5_exit_result =3D 1; + return 0; +} + +__u64 test6_entry_result =3D 0; +__u64 test6_exit_result =3D 0; +__u64 test6_entry_called =3D 0; +__u64 test6_exit_called =3D 0; + +SEC("fsession/bpf_fentry_test5") +int BPF_PROG(test6, __u64 a, void *b, short c, int d, __u64 e) +{ + bool is_exit =3D bpf_tracing_is_exit(ctx); + + if (!is_exit) { + /* This is entry */ + test6_entry_called =3D 1; + test6_entry_result =3D a =3D=3D 11 && b =3D=3D (void *)12 && c =3D=3D 13= && d =3D=3D 14 && + e =3D=3D 15; + /* Decide whether to block exit call based on condition */ + if (a =3D=3D 11) + return 1; /* Block exit call */ + return 0; + } + + /* This is exit - should not be called due to blocking */ + test6_exit_called =3D 1; + test6_exit_result =3D a =3D=3D 11 && b =3D=3D (void *)12 && c =3D=3D 13 &= & d =3D=3D 14 && + e =3D=3D 15; + return 0; +} + +__u64 test7_entry_result =3D 0; +__u64 test7_exit_result =3D 0; +__u64 test7_entry_called =3D 0; +__u64 test7_exit_called =3D 0; + +SEC("fsession/bpf_fentry_test6") +int BPF_PROG(test7, __u64 a, void *b, short c, int d, void *e, __u64 f) +{ + bool is_exit =3D bpf_tracing_is_exit(ctx); + + if (!is_exit) { + /* This is entry */ + test7_entry_called =3D 1; + test7_entry_result =3D a =3D=3D 16 && b =3D=3D (void *)17 && c =3D=3D 18= && d =3D=3D 19 && + e =3D=3D (void *)20 && f =3D=3D 21; + /* Return non-zero to block exit call */ + return 1; + } + + /* This is exit - should not be called due to blocking */ + test7_exit_called =3D 1; + test7_exit_result =3D a =3D=3D 16 && b =3D=3D (void *)17 && c =3D=3D 18 &= & d =3D=3D 19 && + e =3D=3D (void *)20 && f =3D=3D 21; + return 0; +} --=20 2.51.0