History

bpf/tracing: allow user space to query prog array on the same tp

Commit e87c6bc3852b ("bpf: permit multiple bpf attachments for a single perf event") added support to attach multiple bpf programs to a single perf event. Although this provides flexibility, users may want to know what other bpf programs attached to the same tp interface. Besides getting visibility for the underlying bpf system, such information may also help consolidate multiple bpf programs, understand potential performance issues due to a large array, and debug (e.g., one bpf program which overwrites return code may impact subsequent program results). Commit 2541517c32be ("tracing, perf: Implement BPF programs attached to kprobes") utilized the existing perf ioctl interface and added the command PERF_EVENT_IOC_SET_BPF to attach a bpf program to a tracepoint. This patch adds a new ioctl command, given a perf event fd, to query the bpf program array attached to the same perf tracepoint event. The new uapi ioctl command: PERF_EVENT_IOC_QUERY_BPF The new uapi/linux/perf_event.h structure: struct perf_event_query_bpf { __u32 ids_len; __u32 prog_cnt; __u32 ids[0]; }; User space provides buffer "ids" for kernel to copy to. When returning from the kernel, the number of available programs in the array is set in "prog_cnt". The usage: struct perf_event_query_bpf *query = malloc(sizeof(*query) + sizeof(u32) * ids_len); query.ids_len = ids_len; err = ioctl(pmu_efd, PERF_EVENT_IOC_QUERY_BPF, query); if (err == 0) { /* query.prog_cnt is the number of available progs, * number of progs in ids: (ids_len == 0) ? 0 : query.prog_cnt */ } else if (errno == ENOSPC) { /* query.ids_len number of progs copied, * query.prog_cnt is the number of available progs */ } else { /* other errors */ }

Yonghong Song committed 8 years ago

Commit f371b301 parent 63060c35 files · +73 −0Browse files

Showing the changes to include/uapi/linux (1 of 5 files). Show all

Unified · Split

include/uapi/linux/perf_event.h+22−0
@@ -418,6 +418,27 @@ struct perf_event_attr { · 417 unmodified lines
418418 __u16 __reserved_2; /* align to __u64 */
419419 };
420420
421+/*
422+ * Structure used by below PERF_EVENT_IOC_QUERY_BPF command
423+ * to query bpf programs attached to the same perf tracepoint
424+ * as the given perf event.
425+ */
426+struct perf_event_query_bpf {
427+ /*
428+ * The below ids array length
429+ */
430+ __u32 ids_len;
431+ /*
432+ * Set by the kernel to indicate the number of
433+ * available programs
434+ */
435+ __u32 prog_cnt;
436+ /*
437+ * User provided buffer to store program ids
438+ */
439+ __u32 ids[0];
440+};
441+
421442 #define perf_flags(attr) (*(&(attr)->read_format + 1))
422443
423444 /*
@@ -433,6 +454,7 @@ struct perf_event_attr { · 9 unmodified lines
433454 #define PERF_EVENT_IOC_ID _IOR('$', 7, __u64 *)
434455 #define PERF_EVENT_IOC_SET_BPF _IOW('$', 8, __u32)
435456 #define PERF_EVENT_IOC_PAUSE_OUTPUT _IOW('$', 9, __u32)
457+#define PERF_EVENT_IOC_QUERY_BPF _IOWR('$', 10, struct perf_event_query_bpf *)
436458
437459 enum perf_event_ioc_flags {
438460 PERF_IOC_FLAG_GROUP = 1U << 0,