From: Andrii Nakryiko <andrii@kernel.org>
To: bpf@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net,
martin.lau@kernel.org
Cc: peterz@infradead.org, song@kernel.org,
Andrii Nakryiko <andrii@kernel.org>
Subject: [PATCH bpf-next 2/3] bpf: inline bpf_get_branch_snapshot() helper
Date: Thu, 21 Mar 2024 11:05:00 -0700 [thread overview]
Message-ID: <20240321180501.734779-3-andrii@kernel.org> (raw)
In-Reply-To: <20240321180501.734779-1-andrii@kernel.org>
Inline bpf_get_branch_snapshot() helper using architecture-agnostic
inline BPF code which calls directly into underlying callback of
perf_snapshot_branch_stack static call. This callback is set early
during kernel initialization and is never updated or reset, so it's ok
to fetch actual implementation using static_call_query() and call
directly into it.
This change eliminates a full function call and saves one LBR entry
in PERF_SAMPLE_BRANCH_ANY LBR mode.
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
---
kernel/bpf/verifier.c | 37 +++++++++++++++++++++++++++++++++++++
1 file changed, 37 insertions(+)
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index de7813947981..4fb6c468e199 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -20130,6 +20130,43 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
goto next_insn;
}
+ /* Implement bpf_get_branch_snapshot inline. */
+ if (prog->jit_requested && BITS_PER_LONG == 64 &&
+ insn->imm == BPF_FUNC_get_branch_snapshot) {
+ /* We are dealing with the following func protos:
+ * u64 bpf_get_branch_snapshot(void *buf, u32 size, u64 flags);
+ * int perf_snapshot_branch_stack(struct perf_branch_entry *entries, u32 cnt);
+ */
+ const u32 br_entry_size = sizeof(struct perf_branch_entry);
+
+ /* if (unlikely(flags)) return -EINVAL */
+ insn_buf[0] = BPF_JMP_IMM(BPF_JNE, BPF_REG_3, 0, 5);
+ /* transform size (bytes) into entry_cnt */
+ insn_buf[1] = BPF_ALU32_IMM(BPF_DIV, BPF_REG_2, br_entry_size);
+ /* call perf_snapshot_branch_stack implementation */
+ insn_buf[2] = BPF_EMIT_CALL(static_call_query(perf_snapshot_branch_stack));
+ /* if (entry_cnt == 0) return -ENOENT */
+ insn_buf[3] = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 4);
+ /* return entry_cnt * sizeof(struct perf_branch_entry) */
+ insn_buf[4] = BPF_ALU32_IMM(BPF_MUL, BPF_REG_0, br_entry_size);
+ insn_buf[5] = BPF_JMP_A(3);
+ /* return -EINVAL; */
+ insn_buf[6] = BPF_MOV64_IMM(BPF_REG_0, -EINVAL);
+ insn_buf[7] = BPF_JMP_A(1);
+ /* return -ENOENT; */
+ insn_buf[8] = BPF_MOV64_IMM(BPF_REG_0, -ENOENT);
+ cnt = 9;
+
+ new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
+ if (!new_prog)
+ return -ENOMEM;
+
+ delta += cnt - 1;
+ env->prog = prog = new_prog;
+ insn = new_prog->insnsi + i + delta;
+ continue;
+ }
+
/* Implement bpf_kptr_xchg inline */
if (prog->jit_requested && BITS_PER_LONG == 64 &&
insn->imm == BPF_FUNC_kptr_xchg &&
--
2.43.0
next prev parent reply other threads:[~2024-03-21 18:05 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-21 18:04 [PATCH bpf-next 0/3] Inline two LBR-related helpers Andrii Nakryiko
2024-03-21 18:04 ` [PATCH bpf-next 1/3] bpf: make bpf_get_branch_snapshot() architecture-agnostic Andrii Nakryiko
2024-03-21 21:08 ` Jiri Olsa
2024-03-21 18:05 ` Andrii Nakryiko [this message]
2024-03-21 21:08 ` [PATCH bpf-next 2/3] bpf: inline bpf_get_branch_snapshot() helper Jiri Olsa
2024-03-21 21:27 ` Andrii Nakryiko
2024-03-21 18:05 ` [PATCH bpf-next 3/3] bpf,x86: inline bpf_get_smp_processor_id() on x86-64 Andrii Nakryiko
2024-03-21 21:08 ` Jiri Olsa
2024-03-21 21:09 ` Andrii Nakryiko
2024-03-21 22:57 ` Jiri Olsa
2024-03-21 23:38 ` Andrii Nakryiko
2024-03-21 23:49 ` Alexei Starovoitov
2024-03-22 16:45 ` Andrii Nakryiko
2024-03-25 3:28 ` Alexei Starovoitov
2024-03-25 17:01 ` Andrii Nakryiko
2024-03-21 23:46 ` [PATCH bpf-next 0/3] Inline two LBR-related helpers Alexei Starovoitov
2024-03-22 16:45 ` Andrii Nakryiko
2024-03-25 2:05 ` Alexei Starovoitov
2024-03-25 17:20 ` Andrii Nakryiko
2024-03-26 3:13 ` Alexei Starovoitov
2024-03-26 16:50 ` Andrii Nakryiko
2024-03-27 21:59 ` Alexei Starovoitov
2024-03-28 22:53 ` Andrii Nakryiko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240321180501.734779-3-andrii@kernel.org \
--to=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=martin.lau@kernel.org \
--cc=peterz@infradead.org \
--cc=song@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox