Skip to content

Commit 1e33759

Browse files
borkmanndavem330
authored andcommitted
bpf, trace: add BPF_F_CURRENT_CPU flag for bpf_perf_event_output
Add a BPF_F_CURRENT_CPU flag to optimize the use-case where user space has per-CPU ring buffers and the eBPF program pushes the data into the current CPU's ring buffer which saves us an extra helper function call in eBPF. Also, make sure to properly reserve the remaining flags which are not used. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 553bc08 commit 1e33759

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

include/uapi/linux/bpf.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,10 @@ enum bpf_func_id {
347347
#define BPF_F_ZERO_CSUM_TX (1ULL << 1)
348348
#define BPF_F_DONT_FRAGMENT (1ULL << 2)
349349

350+
/* BPF_FUNC_perf_event_output flags. */
351+
#define BPF_F_INDEX_MASK 0xffffffffULL
352+
#define BPF_F_CURRENT_CPU BPF_F_INDEX_MASK
353+
350354
/* user accessible mirror of in-kernel sk_buff.
351355
* new fields can only be added to the end of this structure
352356
*/

kernel/trace/bpf_trace.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,12 @@ static const struct bpf_func_proto bpf_perf_event_read_proto = {
225225
.arg2_type = ARG_ANYTHING,
226226
};
227227

228-
static u64 bpf_perf_event_output(u64 r1, u64 r2, u64 index, u64 r4, u64 size)
228+
static u64 bpf_perf_event_output(u64 r1, u64 r2, u64 flags, u64 r4, u64 size)
229229
{
230230
struct pt_regs *regs = (struct pt_regs *) (long) r1;
231231
struct bpf_map *map = (struct bpf_map *) (long) r2;
232232
struct bpf_array *array = container_of(map, struct bpf_array, map);
233+
u64 index = flags & BPF_F_INDEX_MASK;
233234
void *data = (void *) (long) r4;
234235
struct perf_sample_data sample_data;
235236
struct perf_event *event;
@@ -239,6 +240,10 @@ static u64 bpf_perf_event_output(u64 r1, u64 r2, u64 index, u64 r4, u64 size)
239240
.data = data,
240241
};
241242

243+
if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
244+
return -EINVAL;
245+
if (index == BPF_F_CURRENT_CPU)
246+
index = raw_smp_processor_id();
242247
if (unlikely(index >= array->map.max_entries))
243248
return -E2BIG;
244249

0 commit comments

Comments
 (0)