Skip to content

Commit 6ee52e2

Browse files
liu-song-6acmel
authored andcommitted
perf, bpf: Introduce PERF_RECORD_BPF_EVENT
For better performance analysis of BPF programs, this patch introduces PERF_RECORD_BPF_EVENT, a new perf_event_type that exposes BPF program load/unload information to user space. Each BPF program may contain up to BPF_MAX_SUBPROGS (256) sub programs. The following example shows kernel symbols for a BPF program with 7 sub programs: ffffffffa0257cf9 t bpf_prog_b07ccb89267cf242_F ffffffffa02592e1 t bpf_prog_2dcecc18072623fc_F ffffffffa025b0e9 t bpf_prog_bb7a405ebaec5d5c_F ffffffffa025dd2c t bpf_prog_a7540d4a39ec1fc7_F ffffffffa025fcca t bpf_prog_05762d4ade0e3737_F ffffffffa026108f t bpf_prog_db4bd11e35df90d4_F ffffffffa0263f00 t bpf_prog_89d64e4abf0f0126_F ffffffffa0257cf9 t bpf_prog_ae31629322c4b018__dummy_tracepoi When a bpf program is loaded, PERF_RECORD_KSYMBOL is generated for each of these sub programs. Therefore, PERF_RECORD_BPF_EVENT is not needed for simple profiling. For annotation, user space need to listen to PERF_RECORD_BPF_EVENT and gather more information about these (sub) programs via sys_bpf. Signed-off-by: Song Liu <songliubraving@fb.com> Reviewed-by: Arnaldo Carvalho de Melo <acme@redhat.com> Acked-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Peter Zijlstra (Intel) <peterz@infradeaed.org> Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Daniel Borkmann <daniel@iogearbox.net> Cc: Peter Zijlstra <peterz@infradead.org> Cc: kernel-team@fb.com Cc: netdev@vger.kernel.org Link: http://lkml.kernel.org/r/20190117161521.1341602-4-songliubraving@fb.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent d764ac6 commit 6ee52e2

6 files changed

Lines changed: 159 additions & 2 deletions

File tree

‎include/linux/filter.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,7 @@ bpf_address_lookup(unsigned long addr, unsigned long *size,
951951

952952
void bpf_prog_kallsyms_add(struct bpf_prog *fp);
953953
void bpf_prog_kallsyms_del(struct bpf_prog *fp);
954+
void bpf_get_prog_name(const struct bpf_prog *prog, char *sym);
954955

955956
#else /* CONFIG_BPF_JIT */
956957

@@ -1006,6 +1007,12 @@ static inline void bpf_prog_kallsyms_add(struct bpf_prog *fp)
10061007
static inline void bpf_prog_kallsyms_del(struct bpf_prog *fp)
10071008
{
10081009
}
1010+
1011+
static inline void bpf_get_prog_name(const struct bpf_prog *prog, char *sym)
1012+
{
1013+
sym[0] = '\0';
1014+
}
1015+
10091016
#endif /* CONFIG_BPF_JIT */
10101017

10111018
void bpf_prog_kallsyms_del_subprogs(struct bpf_prog *fp);

‎include/linux/perf_event.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,9 @@ extern void perf_event_mmap(struct vm_area_struct *vma);
11251125

11261126
extern void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len,
11271127
bool unregister, const char *sym);
1128+
extern void perf_event_bpf_event(struct bpf_prog *prog,
1129+
enum perf_bpf_event_type type,
1130+
u16 flags);
11281131

11291132
extern struct perf_guest_info_callbacks *perf_guest_cbs;
11301133
extern int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
@@ -1350,6 +1353,9 @@ static inline void perf_event_mmap(struct vm_area_struct *vma) { }
13501353
typedef int (perf_ksymbol_get_name_f)(char *name, int name_len, void *data);
13511354
static inline void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len,
13521355
bool unregister, const char *sym) { }
1356+
static inline void perf_event_bpf_event(struct bpf_prog *prog,
1357+
enum perf_bpf_event_type type,
1358+
u16 flags) { }
13531359
static inline void perf_event_exec(void) { }
13541360
static inline void perf_event_comm(struct task_struct *tsk, bool exec) { }
13551361
static inline void perf_event_namespaces(struct task_struct *tsk) { }

‎include/uapi/linux/perf_event.h‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,8 @@ struct perf_event_attr {
373373
write_backward : 1, /* Write ring buffer from end to beginning */
374374
namespaces : 1, /* include namespaces data */
375375
ksymbol : 1, /* include ksymbol events */
376-
__reserved_1 : 34;
376+
bpf_event : 1, /* include bpf events */
377+
__reserved_1 : 33;
377378

378379
union {
379380
__u32 wakeup_events; /* wakeup every n events */
@@ -979,6 +980,25 @@ enum perf_event_type {
979980
*/
980981
PERF_RECORD_KSYMBOL = 17,
981982

983+
/*
984+
* Record bpf events:
985+
* enum perf_bpf_event_type {
986+
* PERF_BPF_EVENT_UNKNOWN = 0,
987+
* PERF_BPF_EVENT_PROG_LOAD = 1,
988+
* PERF_BPF_EVENT_PROG_UNLOAD = 2,
989+
* };
990+
*
991+
* struct {
992+
* struct perf_event_header header;
993+
* u16 type;
994+
* u16 flags;
995+
* u32 id;
996+
* u8 tag[BPF_TAG_SIZE];
997+
* struct sample_id sample_id;
998+
* };
999+
*/
1000+
PERF_RECORD_BPF_EVENT = 18,
1001+
9821002
PERF_RECORD_MAX, /* non-ABI */
9831003
};
9841004

@@ -990,6 +1010,13 @@ enum perf_record_ksymbol_type {
9901010

9911011
#define PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER (1 << 0)
9921012

1013+
enum perf_bpf_event_type {
1014+
PERF_BPF_EVENT_UNKNOWN = 0,
1015+
PERF_BPF_EVENT_PROG_LOAD = 1,
1016+
PERF_BPF_EVENT_PROG_UNLOAD = 2,
1017+
PERF_BPF_EVENT_MAX, /* non-ABI */
1018+
};
1019+
9931020
#define PERF_MAX_STACK_DEPTH 127
9941021
#define PERF_MAX_CONTEXTS_PER_STACK 8
9951022

‎kernel/bpf/core.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ bpf_get_prog_addr_region(const struct bpf_prog *prog,
495495
*symbol_end = addr + hdr->pages * PAGE_SIZE;
496496
}
497497

498-
static void bpf_get_prog_name(const struct bpf_prog *prog, char *sym)
498+
void bpf_get_prog_name(const struct bpf_prog *prog, char *sym)
499499
{
500500
const char *end = sym + KSYM_NAME_LEN;
501501
const struct btf_type *type;

‎kernel/bpf/syscall.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,7 @@ static void __bpf_prog_put_rcu(struct rcu_head *rcu)
12111211
static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
12121212
{
12131213
if (atomic_dec_and_test(&prog->aux->refcnt)) {
1214+
perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
12141215
/* bpf_prog_free_id() must be called first */
12151216
bpf_prog_free_id(prog, do_idr_lock);
12161217
bpf_prog_kallsyms_del_all(prog);
@@ -1554,6 +1555,7 @@ static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
15541555
}
15551556

15561557
bpf_prog_kallsyms_add(prog);
1558+
perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
15571559
return err;
15581560

15591561
free_used_maps:

‎kernel/events/core.c‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ static atomic_t nr_task_events __read_mostly;
386386
static atomic_t nr_freq_events __read_mostly;
387387
static atomic_t nr_switch_events __read_mostly;
388388
static atomic_t nr_ksymbol_events __read_mostly;
389+
static atomic_t nr_bpf_events __read_mostly;
389390

390391
static LIST_HEAD(pmus);
391392
static DEFINE_MUTEX(pmus_lock);
@@ -4308,6 +4309,8 @@ static void unaccount_event(struct perf_event *event)
43084309
dec = true;
43094310
if (event->attr.ksymbol)
43104311
atomic_dec(&nr_ksymbol_events);
4312+
if (event->attr.bpf_event)
4313+
atomic_dec(&nr_bpf_events);
43114314

43124315
if (dec) {
43134316
if (!atomic_add_unless(&perf_sched_count, -1, 1))
@@ -7747,6 +7750,116 @@ void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len, bool unregister,
77477750
WARN_ONCE(1, "%s: Invalid KSYMBOL type 0x%x\n", __func__, ksym_type);
77487751
}
77497752

7753+
/*
7754+
* bpf program load/unload tracking
7755+
*/
7756+
7757+
struct perf_bpf_event {
7758+
struct bpf_prog *prog;
7759+
struct {
7760+
struct perf_event_header header;
7761+
u16 type;
7762+
u16 flags;
7763+
u32 id;
7764+
u8 tag[BPF_TAG_SIZE];
7765+
} event_id;
7766+
};
7767+
7768+
static int perf_event_bpf_match(struct perf_event *event)
7769+
{
7770+
return event->attr.bpf_event;
7771+
}
7772+
7773+
static void perf_event_bpf_output(struct perf_event *event, void *data)
7774+
{
7775+
struct perf_bpf_event *bpf_event = data;
7776+
struct perf_output_handle handle;
7777+
struct perf_sample_data sample;
7778+
int ret;
7779+
7780+
if (!perf_event_bpf_match(event))
7781+
return;
7782+
7783+
perf_event_header__init_id(&bpf_event->event_id.header,
7784+
&sample, event);
7785+
ret = perf_output_begin(&handle, event,
7786+
bpf_event->event_id.header.size);
7787+
if (ret)
7788+
return;
7789+
7790+
perf_output_put(&handle, bpf_event->event_id);
7791+
perf_event__output_id_sample(event, &handle, &sample);
7792+
7793+
perf_output_end(&handle);
7794+
}
7795+
7796+
static void perf_event_bpf_emit_ksymbols(struct bpf_prog *prog,
7797+
enum perf_bpf_event_type type)
7798+
{
7799+
bool unregister = type == PERF_BPF_EVENT_PROG_UNLOAD;
7800+
char sym[KSYM_NAME_LEN];
7801+
int i;
7802+
7803+
if (prog->aux->func_cnt == 0) {
7804+
bpf_get_prog_name(prog, sym);
7805+
perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF,
7806+
(u64)(unsigned long)prog->bpf_func,
7807+
prog->jited_len, unregister, sym);
7808+
} else {
7809+
for (i = 0; i < prog->aux->func_cnt; i++) {
7810+
struct bpf_prog *subprog = prog->aux->func[i];
7811+
7812+
bpf_get_prog_name(subprog, sym);
7813+
perf_event_ksymbol(
7814+
PERF_RECORD_KSYMBOL_TYPE_BPF,
7815+
(u64)(unsigned long)subprog->bpf_func,
7816+
subprog->jited_len, unregister, sym);
7817+
}
7818+
}
7819+
}
7820+
7821+
void perf_event_bpf_event(struct bpf_prog *prog,
7822+
enum perf_bpf_event_type type,
7823+
u16 flags)
7824+
{
7825+
struct perf_bpf_event bpf_event;
7826+
7827+
if (type <= PERF_BPF_EVENT_UNKNOWN ||
7828+
type >= PERF_BPF_EVENT_MAX)
7829+
return;
7830+
7831+
switch (type) {
7832+
case PERF_BPF_EVENT_PROG_LOAD:
7833+
case PERF_BPF_EVENT_PROG_UNLOAD:
7834+
if (atomic_read(&nr_ksymbol_events))
7835+
perf_event_bpf_emit_ksymbols(prog, type);
7836+
break;
7837+
default:
7838+
break;
7839+
}
7840+
7841+
if (!atomic_read(&nr_bpf_events))
7842+
return;
7843+
7844+
bpf_event = (struct perf_bpf_event){
7845+
.prog = prog,
7846+
.event_id = {
7847+
.header = {
7848+
.type = PERF_RECORD_BPF_EVENT,
7849+
.size = sizeof(bpf_event.event_id),
7850+
},
7851+
.type = type,
7852+
.flags = flags,
7853+
.id = prog->aux->id,
7854+
},
7855+
};
7856+
7857+
BUILD_BUG_ON(BPF_TAG_SIZE % sizeof(u64));
7858+
7859+
memcpy(bpf_event.event_id.tag, prog->tag, BPF_TAG_SIZE);
7860+
perf_iterate_sb(perf_event_bpf_output, &bpf_event, NULL);
7861+
}
7862+
77507863
void perf_event_itrace_started(struct perf_event *event)
77517864
{
77527865
event->attach_state |= PERF_ATTACH_ITRACE;
@@ -10008,6 +10121,8 @@ static void account_event(struct perf_event *event)
1000810121
inc = true;
1000910122
if (event->attr.ksymbol)
1001010123
atomic_inc(&nr_ksymbol_events);
10124+
if (event->attr.bpf_event)
10125+
atomic_inc(&nr_bpf_events);
1001110126

1001210127
if (inc) {
1001310128
/*

0 commit comments

Comments
 (0)