From nobody Sun Feb 8 11:26:36 2026 Received: from out-181.mta1.migadu.com (out-181.mta1.migadu.com [95.215.58.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7E00E325720 for ; Fri, 9 Jan 2026 15:37:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767973032; cv=none; b=sGWZ126fZgJY+9Wt+I4JOnW6XmQLR54xO0rPcTIFZPPkv+rBM//zgBNn0Vyxbnw255g1b4lRf1rS/E8w2V3h4CGDi3jbY8cd5feOQBbPlD5IKA6gJdbkQkXg7Ud5U4wlwN0kDjjCSS6R7/n2wBKoLWDaGfiY5qdR4gWzPcmW6po= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767973032; c=relaxed/simple; bh=l/hupaNnVXU7GiVcnweVKH9y/0ngIhXBvVKYebvSiYc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fDnohQnzT1e3b8JAUJN4nh7dY+hU5qvGqU6xZuv0VDVX/37eMLqUSQpoTU1/2XnF/jawHOzCoEfV9cxOGqSujPNAuoVHF9sgly/Bg0xJDo+EnIfjc+4O6XX4xBEo15fp2kkUWv+7mJGIFQvUlNsQsdvUPmhOZiA1DFltuFB4Qv4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=iDNCykDA; arc=none smtp.client-ip=95.215.58.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="iDNCykDA" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1767973028; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=D5p/tww7cB1FEKleJGQjJQqwkIHUCww35xYq5j4Io0w=; b=iDNCykDAhIo+5NV1zsDlYIlDVQG2Y9ILsjezMyikGMeFitDAluKSyPmX5BAnpxDgfpCB7x InHGYYg4tl5591BtQyo4fsiAj0WfmHe2/1uM39GkpD6mb574Hoan28pyxVh7o5SjQnAX00 gPT7+2uh7ZbcM5UM82XV3Amx0x8ZJYY= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , "David S . Miller" , David Ahern , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" , Matt Bobrowski , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Shuah Khan , Leon Hwang , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-patches-bot@fb.com Subject: [PATCH bpf-next 1/3] bpf, x64: Call perf_snapshot_branch_stack in trampoline Date: Fri, 9 Jan 2026 23:34:18 +0800 Message-ID: <20260109153420.32181-2-leon.hwang@linux.dev> In-Reply-To: <20260109153420.32181-1-leon.hwang@linux.dev> References: <20260109153420.32181-1-leon.hwang@linux.dev> 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 X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" When the PMU LBR is running in branch-sensitive mode, 'perf_snapshot_branch_stack()' may capture branch entries from the trampoline entry up to the call site inside a BPF program. These branch entries are not useful for analyzing the control flow of the tracee. To eliminate such noise for tracing programs, the branch snapshot should be taken as early as possible: * Call 'perf_snapshot_branch_stack()' at the very beginning of the trampoline for fentry programs. * Call 'perf_snapshot_branch_stack()' immediately after invoking the tracee for fexit programs. With this change, LBR snapshots remain meaningful even when multiple BPF programs execute before the one requesting LBR data. In addition, more relevant branch entries can be captured on AMD CPUs, which provide a 16-entry-deep LBR stack. Signed-off-by: Leon Hwang --- arch/x86/net/bpf_jit_comp.c | 66 +++++++++++++++++++++++++++++++++++++ include/linux/bpf.h | 16 ++++++++- 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index e3b1c4b1d550..a71a6c675392 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -19,6 +20,7 @@ #include #include #include +#include "../events/perf_event.h" =20 static bool all_callee_regs_used[4] =3D {true, true, true, true}; =20 @@ -3137,6 +3139,54 @@ static int invoke_bpf_mod_ret(const struct btf_func_= model *m, u8 **pprog, return 0; } =20 +DEFINE_PER_CPU(struct bpf_tramp_branch_entries, bpf_branch_snapshot); + +static int invoke_branch_snapshot(u8 **pprog, void *image, void *rw_image) +{ + struct bpf_tramp_branch_entries __percpu *pptr =3D &bpf_branch_snapshot; + u8 *prog =3D *pprog; + + /* + * Emit: + * + * struct bpf_tramp_branch_entries *br =3D this_cpu_ptr(&bpf_branch_snaps= hot); + * br->cnt =3D static_call(perf_snapshot_branch_stack)(br->entries, x86_p= mu.lbr_nr); + */ + + /* mov rbx, &bpf_branch_snapshot */ + emit_mov_imm64(&prog, BPF_REG_6, (long) pptr >> 32, (u32)(long) pptr); +#ifdef CONFIG_SMP + /* add rbx, gs:[] */ + EMIT2(0x65, 0x48); + EMIT3(0x03, 0x1C, 0x25); + EMIT((u32)(unsigned long)&this_cpu_off, 4); +#endif + /* mov esi, x86_pmu.lbr_nr */ + EMIT1_off32(0xBE, x86_pmu.lbr_nr); + /* lea rdi, [rbx + offsetof(struct bpf_tramp_branch_entries, entries)] */ + EMIT4(0x48, 0x8D, 0x7B, offsetof(struct bpf_tramp_branch_entries, entries= )); + /* call static_call_query(perf_snapshot_branch_stack) */ + if (emit_rsb_call(&prog, static_call_query(perf_snapshot_branch_stack), + image + (prog - (u8 *)rw_image))) + return -EINVAL; + /* mov dword ptr [rbx], eax */ + EMIT2(0x89, 0x03); + + *pprog =3D prog; + return 0; +} + +static bool bpf_prog_copy_branch_snapshot(struct bpf_tramp_links *tl) +{ + bool copy =3D false; + int i; + + for (i =3D 0; i < tl->nr_links; i++) + copy =3D copy || tl->links[i]->link.prog->copy_branch_snapshot; + + return copy; +} + /* 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) @@ -3366,6 +3416,14 @@ static int __arch_prepare_bpf_trampoline(struct bpf_= tramp_image *im, void *rw_im =20 save_args(m, &prog, regs_off, false, flags); =20 + if (bpf_prog_copy_branch_snapshot(fentry)) { + /* Get branch snapshot asap. */ + if (invoke_branch_snapshot(&prog, image, rw_image)) { + ret =3D -EINVAL; + goto cleanup; + } + } + if (flags & BPF_TRAMP_F_CALL_ORIG) { /* arg1: mov rdi, im */ emit_mov_imm64(&prog, BPF_REG_1, (long) im >> 32, (u32) (long) im); @@ -3422,6 +3480,14 @@ static int __arch_prepare_bpf_trampoline(struct bpf_= tramp_image *im, void *rw_im emit_nops(&prog, X86_PATCH_SIZE); } =20 + if (bpf_prog_copy_branch_snapshot(fexit)) { + /* Get branch snapshot asap. */ + if (invoke_branch_snapshot(&prog, image, rw_image)) { + ret =3D -EINVAL; + goto cleanup; + } + } + if (fmod_ret->nr_links) { /* From Intel 64 and IA-32 Architectures Optimization * Reference Manual, 3.4.1.4 Code Alignment, Assembly/Compiler diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 5936f8e2996f..16dc21836a06 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -6,6 +6,7 @@ =20 #include #include +#include =20 #include #include @@ -1236,6 +1237,18 @@ struct bpf_tramp_links { =20 struct bpf_tramp_run_ctx; =20 +#ifdef CONFIG_X86_64 +/* Same as MAX_LBR_ENTRIES in arch/x86/events/perf_event.h */ +#define MAX_BRANCH_ENTRIES 32 + +struct bpf_tramp_branch_entries { + int cnt; + struct perf_branch_entry entries[MAX_BRANCH_ENTRIES]; +}; + +DECLARE_PER_CPU(struct bpf_tramp_branch_entries, bpf_branch_snapshot); +#endif + /* Different use cases for BPF trampoline: * 1. replace nop at the function entry (kprobe equivalent) * flags =3D BPF_TRAMP_F_RESTORE_REGS @@ -1780,7 +1793,8 @@ struct bpf_prog { 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() */ tstamp_type_access:1, /* Accessed __sk_buff->tstamp_type */ - sleepable:1; /* BPF program is sleepable */ + sleepable:1, /* BPF program is sleepable */ + copy_branch_snapshot:1; /* Copy branch snapshot from prefetched buffer= */ enum bpf_prog_type type; /* Type of BPF program */ enum bpf_attach_type expected_attach_type; /* For some prog types */ u32 len; /* Number of filter blocks */ --=20 2.52.0 From nobody Sun Feb 8 11:26:36 2026 Received: from out-181.mta1.migadu.com (out-181.mta1.migadu.com [95.215.58.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ECC0A2C21E6; Fri, 9 Jan 2026 15:37:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767973045; cv=none; b=rn+Cbm61nuqfwprn5emtk/5G2aTGdVqEn+K5FtLZtVapKTTVRX4uALpCsAAIgXux3kK8k2W5BP+t7XJDoKO1bqw2shDl9h2Dif8fV8VW5l+KZTXD2jRrfTaLHNZz8RQtEXzJM4EMM/Q0cmv9zzybUoqdYl9cPnNAj69pN9sqH38= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767973045; c=relaxed/simple; bh=rqfMJJtPdBcZvpTr2g0yJoZkcjlR+c5YphY+4gxnujQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=C81phBAOIoc9T3k5Yr8IAQASWqlshYjGbeTwg8D4crpEROe5JU0Qf+i0He+/e1IDzsclrvxGGwCu+EAYhuAsXA9ClT5/W/1BrUBGwuZ1Z2aqbKScbIi+ZTrS/upTnnzB9AzHxxaY9w5GP57RRGdIPi1Os7ue/8LGAbYPytw/XAY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=Al1nrBrE; arc=none smtp.client-ip=95.215.58.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="Al1nrBrE" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1767973041; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=y/YV8q+S59t939howf4hH6znlW4fUA9Hm9Vtbx1c9V4=; b=Al1nrBrEqSSCF0Hol59g8tD1ncBex/8y1ucgdOTpLFHLlsuQsRHlpMGoipWVGqrqSX1D+P Bs2ZNYEAbLCCgBd3Bu42U+3AiOhDBhIczdYyXgj8wg3PlsgdOimHKATvI1usKQ6s+Tp0EH xw6N+eHAdFR45dq3p//DS/o3S/0iSVM= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , "David S . Miller" , David Ahern , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" , Matt Bobrowski , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Shuah Khan , Leon Hwang , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-patches-bot@fb.com Subject: [PATCH bpf-next 2/3] bpf: Introduce BPF_BRANCH_SNAPSHOT_F_COPY flag for bpf_get_branch_snapshot helper Date: Fri, 9 Jan 2026 23:34:19 +0800 Message-ID: <20260109153420.32181-3-leon.hwang@linux.dev> In-Reply-To: <20260109153420.32181-1-leon.hwang@linux.dev> References: <20260109153420.32181-1-leon.hwang@linux.dev> 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 X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Introduce BPF_BRANCH_SNAPSHOT_F_COPY flag for tracing programs to copy branch entries from *bpf_branch_snapshot*. Instead of introducing a new kfunc, extend bpf_get_branch_snapshot helper to add the BPF_BRANCH_SNAPSHOT_F_COPY flag support. Therefore, when BPF_BRANCH_SNAPSHOT_F_COPY is specified: * Check the *flags* value in verifier's 'check_helper_call()'. * Skip inlining 'bpf_get_branch_snapshot()' helper in verifier's 'do_misc_fixups()'. * 'memcpy()' branch entries in the 'bpf_get_branch_snapshot()' helper. Signed-off-by: Leon Hwang --- include/linux/bpf.h | 4 ++++ include/linux/bpf_verifier.h | 1 + kernel/bpf/verifier.c | 30 ++++++++++++++++++++++++++++++ kernel/trace/bpf_trace.c | 17 ++++++++++++++--- 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 16dc21836a06..71ce225e5160 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1249,6 +1249,10 @@ struct bpf_tramp_branch_entries { DECLARE_PER_CPU(struct bpf_tramp_branch_entries, bpf_branch_snapshot); #endif +enum { + BPF_BRANCH_SNAPSHOT_F_COPY =3D 1, /* Copy branch snapshot from bpf_branch= _snapshot. */ +}; + /* Different use cases for BPF trampoline: * 1. replace nop at the function entry (kprobe equivalent) * flags =3D BPF_TRAMP_F_RESTORE_REGS diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 130bcbd66f60..c60a145e0466 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -561,6 +561,7 @@ struct bpf_insn_aux_data { bool non_sleepable; /* helper/kfunc may be called from non-sleepable cont= ext */ bool is_iter_next; /* bpf_iter__next() kfunc call */ bool call_with_percpu_alloc_ptr; /* {this,per}_cpu_ptr() with prog percpu= alloc */ + bool copy_branch_snapshot; /* BPF_BRANCH_SNAPSHOT_F_COPY for bpf_get_bran= ch_snapshot helper */ u8 alu_state; /* used in combination with alu_limit */ /* true if STX or LDX instruction is a part of a spill/fill * pattern for a bpf_fastcall call. diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 53635ea2e41b..0a537f9c2f8c 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -11772,6 +11772,33 @@ static int check_helper_call(struct bpf_verifier_e= nv *env, struct bpf_insn *insn err =3D push_callback_call(env, insn, insn_idx, meta.subprogno, set_user_ringbuf_callback_state); break; + case BPF_FUNC_get_branch_snapshot: + { + u64 flags; + + if (!is_reg_const(®s[BPF_REG_3], false)) { + verbose(env, "Flags in bpf_get_branch_snapshot helper must be const.\n"= ); + return -EINVAL; + } + flags =3D reg_const_value(®s[BPF_REG_3], false); + if (flags & ~BPF_BRANCH_SNAPSHOT_F_COPY) { + verbose(env, "Invalid flags in bpf_get_branch_snapshot helper.\n"); + return -EINVAL; + } + + if (flags & BPF_BRANCH_SNAPSHOT_F_COPY) { + if (env->prog->type !=3D BPF_PROG_TYPE_TRACING || + (env->prog->expected_attach_type !=3D BPF_TRACE_FENTRY && + env->prog->expected_attach_type !=3D BPF_TRACE_FEXIT)) { + verbose(env, "Only fentry and fexit programs support BPF_BRANCH_SNAPSH= OT_F_COPY.\n"); + return -EINVAL; + } + + env->insn_aux_data[insn_idx].copy_branch_snapshot =3D true; + env->prog->copy_branch_snapshot =3D true; + } + break; + } } if (err) @@ -23370,6 +23397,9 @@ static int do_misc_fixups(struct bpf_verifier_env *= env) */ BUILD_BUG_ON(br_entry_size !=3D 24); + if (env->insn_aux_data[i + delta].copy_branch_snapshot) + goto patch_call_imm; + /* if (unlikely(flags)) return -EINVAL */ insn_buf[0] =3D BPF_JMP_IMM(BPF_JNE, BPF_REG_3, 0, 7); diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 6e076485bf70..e9e1698cf608 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -1172,10 +1172,20 @@ BPF_CALL_3(bpf_get_branch_snapshot, void *, buf, u3= 2, size, u64, flags) static const u32 br_entry_size =3D sizeof(struct perf_branch_entry); u32 entry_cnt =3D size / br_entry_size; - entry_cnt =3D static_call(perf_snapshot_branch_stack)(buf, entry_cnt); - - if (unlikely(flags)) + if (likely(!flags)) { + entry_cnt =3D static_call(perf_snapshot_branch_stack)(buf, entry_cnt); +#ifdef CONFIG_X86_64 + } else if (flags & BPF_BRANCH_SNAPSHOT_F_COPY) { + struct bpf_tramp_branch_entries *br; + + br =3D this_cpu_ptr(&bpf_branch_snapshot); + entry_cnt =3D min_t(u32, entry_cnt, br->cnt); + if (entry_cnt) + memcpy(buf, (void *) br->entries, entry_cnt * br_entry_size); +#endif + } else { return -EINVAL; + } if (!entry_cnt) return -ENOENT; @@ -1189,6 +1199,7 @@ const struct bpf_func_proto bpf_get_branch_snapshot_p= roto =3D { .ret_type =3D RET_INTEGER, .arg1_type =3D ARG_PTR_TO_UNINIT_MEM, .arg2_type =3D ARG_CONST_SIZE_OR_ZERO, + .arg3_type =3D ARG_ANYTHING, }; BPF_CALL_3(get_func_arg, void *, ctx, u32, n, u64 *, value) -- 2.52.0 From nobody Sun Feb 8 11:26:36 2026 Received: from out-181.mta1.migadu.com (out-181.mta1.migadu.com [95.215.58.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EE91C328B6C for ; Fri, 9 Jan 2026 15:37:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767973057; cv=none; b=c/i0APVOkfoTubay4bSscLXQlH6I6ZYamVQz+EWjK39VNUvUM4gtvy3vFSwnMHTYll9pstYQd2s7mw7MEj3T0F2cpRYgLmpyqPy854+2wjM++INCNJN4F8nLaiwheLxQ4CJ2kL1GObcIa88X87ZbX68dpjgnCzcEUYrcLQHHG9o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767973057; c=relaxed/simple; bh=olXGm4lhKZ/DUlPJ4xDJl2D4L2YZF2AiKKcbPFW+qYo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mrxFtC8yiXIDhTUkpFEcomdOW9YKSKEteaII0sI6oUA+oTI6XsWBrneozsvOCpT1ek8Xnjuz8Zjr6u9D6O9POGqJQj2O1s6DrNBM3J3MWUPcpJLRTZfSv+m60h4CCkP1iSUqeUxXu/ZkgHvbxgfLgIZ2mZaBfcU3wHSTchroVMk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=Mm1147DI; arc=none smtp.client-ip=95.215.58.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="Mm1147DI" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1767973053; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zGmQZRRwHlrtsRbft3WSiOCAz3aFQwD5zodc3tv48HM=; b=Mm1147DIHhLW7zP2EauxyHdsXI9RKxz3GC3qUwmoLQ4yuNjCPdidgxYYT9PcDapwg/HC7B MIvVhpXMv5LqUDNM6kDVe2b6YwJnRCuX5+WfKrlJoHQ8IOb6UHl2QQHnDRVNPXoNFbNqIf R0eRMuWuubcK/ROndIFPVPTrdcatV40= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , "David S . Miller" , David Ahern , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" , Matt Bobrowski , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Shuah Khan , Leon Hwang , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-patches-bot@fb.com Subject: [PATCH bpf-next 3/3] selftests/bpf: Add BPF_BRANCH_SNAPSHOT_F_COPY test Date: Fri, 9 Jan 2026 23:34:20 +0800 Message-ID: <20260109153420.32181-4-leon.hwang@linux.dev> In-Reply-To: <20260109153420.32181-1-leon.hwang@linux.dev> References: <20260109153420.32181-1-leon.hwang@linux.dev> 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 X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Add test for BPF_BRANCH_SNAPSHOT_F_COPY flag by adding flag to the callsite of bpf_get_branch_snapshot helper. Signed-off-by: Leon Hwang --- .../bpf/prog_tests/get_branch_snapshot.c | 26 ++++++++++++++++--- .../selftests/bpf/progs/get_branch_snapshot.c | 3 ++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/get_branch_snapshot.c b= /tools/testing/selftests/bpf/prog_tests/get_branch_snapshot.c index 0394a1156d99..6b8ab1655ab0 100644 --- a/tools/testing/selftests/bpf/prog_tests/get_branch_snapshot.c +++ b/tools/testing/selftests/bpf/prog_tests/get_branch_snapshot.c @@ -73,7 +73,7 @@ static void close_perf_events(void) free(pfd_array); } =20 -void serial_test_get_branch_snapshot(void) +static void test_branch_snapshot(int flags) { struct get_branch_snapshot *skel =3D NULL; int err; @@ -89,8 +89,14 @@ void serial_test_get_branch_snapshot(void) goto cleanup; } =20 - skel =3D get_branch_snapshot__open_and_load(); - if (!ASSERT_OK_PTR(skel, "get_branch_snapshot__open_and_load")) + skel =3D get_branch_snapshot__open(); + if (!ASSERT_OK_PTR(skel, "get_branch_snapshot__open")) + goto cleanup; + + skel->rodata->flags =3D flags; + + err =3D get_branch_snapshot__load(skel); + if (!ASSERT_OK(err, "get_branch_snapshot__load")) goto cleanup; =20 err =3D kallsyms_find("bpf_testmod_loop_test", &skel->bss->address_low); @@ -128,3 +134,17 @@ void serial_test_get_branch_snapshot(void) get_branch_snapshot__destroy(skel); close_perf_events(); } + +void serial_test_get_branch_snapshot(void) +{ + test_branch_snapshot(0); +} + +enum { + BPF_BRANCH_SNAPSHOT_F_COPY =3D 1, /* Copy branch snapshot from bpf_branch= _snapshot. */ +}; + +void serial_test_copy_branch_snapshot(void) +{ + test_branch_snapshot(BPF_BRANCH_SNAPSHOT_F_COPY); +} diff --git a/tools/testing/selftests/bpf/progs/get_branch_snapshot.c b/tool= s/testing/selftests/bpf/progs/get_branch_snapshot.c index 511ac634eef0..47a1984bdf46 100644 --- a/tools/testing/selftests/bpf/progs/get_branch_snapshot.c +++ b/tools/testing/selftests/bpf/progs/get_branch_snapshot.c @@ -6,6 +6,7 @@ =20 char _license[] SEC("license") =3D "GPL"; =20 +volatile const int flags =3D 0; __u64 test1_hits =3D 0; __u64 address_low =3D 0; __u64 address_high =3D 0; @@ -25,7 +26,7 @@ int BPF_PROG(test1, int n, int ret) { long i; =20 - total_entries =3D bpf_get_branch_snapshot(entries, sizeof(entries), 0); + total_entries =3D bpf_get_branch_snapshot(entries, sizeof(entries), flags= ); total_entries /=3D sizeof(struct perf_branch_entry); =20 for (i =3D 0; i < ENTRY_CNT; i++) { --=20 2.52.0