[PATCH RFC 2/4] bpf, perf: add ability to attach complete array of bpf prog to perf event

From: Kenny Ho
Date: Thu Nov 18 2021 - 15:29:09 EST


Change-Id: Ie2580c3a71e2a5116551879358cb5304b04d3838
Signed-off-by: Kenny Ho <Kenny.Ho@xxxxxxx>
---
include/linux/trace_events.h | 9 +++++++++
kernel/trace/bpf_trace.c | 28 ++++++++++++++++++++++++++++
2 files changed, 37 insertions(+)

diff --git a/include/linux/trace_events.h b/include/linux/trace_events.h
index 3e475eeb5a99..5cfe3d08966c 100644
--- a/include/linux/trace_events.h
+++ b/include/linux/trace_events.h
@@ -725,6 +725,8 @@ trace_trigger_soft_disabled(struct trace_event_file *file)

#ifdef CONFIG_BPF_EVENTS
unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx);
+int perf_event_attach_bpf_prog_array(struct perf_event *event,
+ struct bpf_prog_array *new_array);
int perf_event_attach_bpf_prog(struct perf_event *event, struct bpf_prog *prog, u64 bpf_cookie);
void perf_event_detach_bpf_prog(struct perf_event *event);
int perf_event_query_prog_array(struct perf_event *event, void __user *info);
@@ -741,6 +743,13 @@ static inline unsigned int trace_call_bpf(struct trace_event_call *call, void *c
return 1;
}

+static inline int
+int perf_event_attach_bpf_prog_array(struct perf_event *event,
+ struct bpf_prog_array *new_array)
+{
+ return -EOPNOTSUPP;
+}
+
static inline int
perf_event_attach_bpf_prog(struct perf_event *event, struct bpf_prog *prog, u64 bpf_cookie)
{
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index 6b3153841a33..8addd10202c2 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -1802,6 +1802,34 @@ static DEFINE_MUTEX(bpf_event_mutex);

#define BPF_TRACE_MAX_PROGS 64

+int perf_event_attach_bpf_prog_array(struct perf_event *event,
+ struct bpf_prog_array *new_array)
+{
+ struct bpf_prog_array_item *item;
+ struct bpf_prog_array *old_array;
+
+ if (!new_array)
+ return -EINVAL;
+
+ if (bpf_prog_array_length(new_array) >= BPF_TRACE_MAX_PROGS)
+ return -E2BIG;
+
+ if (!trace_kprobe_on_func_entry(event->tp_event) ||
+ !trace_kprobe_error_injectable(event->tp_event))
+ for (item = new_array->items; item->prog; item++)
+ if (item->prog->kprobe_override)
+ return -EINVAL;
+
+ mutex_lock(&bpf_event_mutex);
+
+ old_array = bpf_event_rcu_dereference(event->tp_event->prog_array);
+ rcu_assign_pointer(event->tp_event->prog_array, new_array);
+ bpf_prog_array_free(old_array);
+
+ mutex_unlock(&bpf_event_mutex);
+ return 0;
+}
+
int perf_event_attach_bpf_prog(struct perf_event *event,
struct bpf_prog *prog,
u64 bpf_cookie)
--
2.25.1