Skip to content

Commit 6816a7f

Browse files
borkmanndavem330
authored andcommitted
bpf, trace: add BPF_F_CURRENT_CPU flag for bpf_perf_event_read
Follow-up commit to 1e33759 ("bpf, trace: add BPF_F_CURRENT_CPU flag for bpf_perf_event_output") to add the same functionality into bpf_perf_event_read() helper. The split of index into flags and index component is also safe here, since such large maps are rejected during map allocation time. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent d793133 commit 6816a7f

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

include/uapi/linux/bpf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ 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. */
350+
/* BPF_FUNC_perf_event_output and BPF_FUNC_perf_event_read flags. */
351351
#define BPF_F_INDEX_MASK 0xffffffffULL
352352
#define BPF_F_CURRENT_CPU BPF_F_INDEX_MASK
353353

kernel/trace/bpf_trace.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,19 @@ const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
188188
return &bpf_trace_printk_proto;
189189
}
190190

191-
static u64 bpf_perf_event_read(u64 r1, u64 index, u64 r3, u64 r4, u64 r5)
191+
static u64 bpf_perf_event_read(u64 r1, u64 flags, u64 r3, u64 r4, u64 r5)
192192
{
193193
struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
194194
struct bpf_array *array = container_of(map, struct bpf_array, map);
195+
unsigned int cpu = smp_processor_id();
196+
u64 index = flags & BPF_F_INDEX_MASK;
195197
struct bpf_event_entry *ee;
196198
struct perf_event *event;
197199

200+
if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
201+
return -EINVAL;
202+
if (index == BPF_F_CURRENT_CPU)
203+
index = cpu;
198204
if (unlikely(index >= array->map.max_entries))
199205
return -E2BIG;
200206

@@ -208,8 +214,7 @@ static u64 bpf_perf_event_read(u64 r1, u64 index, u64 r3, u64 r4, u64 r5)
208214
return -EINVAL;
209215

210216
/* make sure event is local and doesn't have pmu::count */
211-
if (event->oncpu != smp_processor_id() ||
212-
event->pmu->count)
217+
if (unlikely(event->oncpu != cpu || event->pmu->count))
213218
return -EINVAL;
214219

215220
/*

0 commit comments

Comments
 (0)