Skip to content

Commit 35578d7

Browse files
Kaixu Xiadavem330
authored andcommitted
bpf: Implement function bpf_perf_event_read() that get the selected hardware PMU conuter
According to the perf_event_map_fd and index, the function bpf_perf_event_read() can convert the corresponding map value to the pointer to struct perf_event and return the Hardware PMU counter value. Signed-off-by: Kaixu Xia <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent ea317b2 commit 35578d7

File tree

4 files changed

+66
-15
lines changed

4 files changed

+66
-15
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
190190
extern const struct bpf_func_proto bpf_map_update_elem_proto;
191191
extern const struct bpf_func_proto bpf_map_delete_elem_proto;
192192

193+
extern const struct bpf_func_proto bpf_perf_event_read_proto;
193194
extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
194195
extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
195196
extern const struct bpf_func_proto bpf_tail_call_proto;

include/uapi/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ enum bpf_func_id {
271271
*/
272272
BPF_FUNC_skb_get_tunnel_key,
273273
BPF_FUNC_skb_set_tunnel_key,
274+
BPF_FUNC_perf_event_read, /* u64 bpf_perf_event_read(&map, index) */
274275
__BPF_FUNC_MAX_ID,
275276
};
276277

kernel/bpf/verifier.c

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,14 @@ static const char * const reg_type_str[] = {
238238
[CONST_IMM] = "imm",
239239
};
240240

241+
static const struct {
242+
int map_type;
243+
int func_id;
244+
} func_limit[] = {
245+
{BPF_MAP_TYPE_PROG_ARRAY, BPF_FUNC_tail_call},
246+
{BPF_MAP_TYPE_PERF_EVENT_ARRAY, BPF_FUNC_perf_event_read},
247+
};
248+
241249
static void print_verifier_state(struct verifier_env *env)
242250
{
243251
enum bpf_reg_type t;
@@ -837,6 +845,28 @@ static int check_func_arg(struct verifier_env *env, u32 regno,
837845
return err;
838846
}
839847

848+
static int check_map_func_compatibility(struct bpf_map *map, int func_id)
849+
{
850+
bool bool_map, bool_func;
851+
int i;
852+
853+
if (!map)
854+
return 0;
855+
856+
for (i = 0; i <= ARRAY_SIZE(func_limit); i++) {
857+
bool_map = (map->map_type == func_limit[i].map_type);
858+
bool_func = (func_id == func_limit[i].func_id);
859+
/* only when map & func pair match it can continue.
860+
* don't allow any other map type to be passed into
861+
* the special func;
862+
*/
863+
if (bool_map != bool_func)
864+
return -EINVAL;
865+
}
866+
867+
return 0;
868+
}
869+
840870
static int check_call(struct verifier_env *env, int func_id)
841871
{
842872
struct verifier_state *state = &env->cur_state;
@@ -912,21 +942,9 @@ static int check_call(struct verifier_env *env, int func_id)
912942
return -EINVAL;
913943
}
914944

915-
if (map && map->map_type == BPF_MAP_TYPE_PROG_ARRAY &&
916-
func_id != BPF_FUNC_tail_call)
917-
/* prog_array map type needs extra care:
918-
* only allow to pass it into bpf_tail_call() for now.
919-
* bpf_map_delete_elem() can be allowed in the future,
920-
* while bpf_map_update_elem() must only be done via syscall
921-
*/
922-
return -EINVAL;
923-
924-
if (func_id == BPF_FUNC_tail_call &&
925-
map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
926-
/* don't allow any other map type to be passed into
927-
* bpf_tail_call()
928-
*/
929-
return -EINVAL;
945+
err = check_map_func_compatibility(map, func_id);
946+
if (err)
947+
return err;
930948

931949
return 0;
932950
}

kernel/trace/bpf_trace.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,35 @@ const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
158158
return &bpf_trace_printk_proto;
159159
}
160160

161+
static u64 bpf_perf_event_read(u64 r1, u64 index, u64 r3, u64 r4, u64 r5)
162+
{
163+
struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
164+
struct bpf_array *array = container_of(map, struct bpf_array, map);
165+
struct perf_event *event;
166+
167+
if (unlikely(index >= array->map.max_entries))
168+
return -E2BIG;
169+
170+
event = (struct perf_event *)array->ptrs[index];
171+
if (!event)
172+
return -ENOENT;
173+
174+
/*
175+
* we don't know if the function is run successfully by the
176+
* return value. It can be judged in other places, such as
177+
* eBPF programs.
178+
*/
179+
return perf_event_read_local(event);
180+
}
181+
182+
const struct bpf_func_proto bpf_perf_event_read_proto = {
183+
.func = bpf_perf_event_read,
184+
.gpl_only = false,
185+
.ret_type = RET_INTEGER,
186+
.arg1_type = ARG_CONST_MAP_PTR,
187+
.arg2_type = ARG_ANYTHING,
188+
};
189+
161190
static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func_id)
162191
{
163192
switch (func_id) {
@@ -183,6 +212,8 @@ static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func
183212
return bpf_get_trace_printk_proto();
184213
case BPF_FUNC_get_smp_processor_id:
185214
return &bpf_get_smp_processor_id_proto;
215+
case BPF_FUNC_perf_event_read:
216+
return &bpf_perf_event_read_proto;
186217
default:
187218
return NULL;
188219
}

0 commit comments

Comments
 (0)