Commit 652c1b17 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Daniel Borkmann

bpf: Refactor perf_event_set_bpf_prog() to use struct bpf_prog input

Make internal perf_event_set_bpf_prog() use struct bpf_prog pointer as an
input argument, which makes it easier to re-use for other internal uses
(coming up for BPF link in the next patch). BPF program FD is not as
convenient and in some cases it's not available. So switch to struct bpf_prog,
move out refcounting outside and let caller do bpf_prog_put() in case of an
error. This follows the approach of most of the other BPF internal functions.
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Acked-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: default avatarYonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20210815070609.987780-4-andrii@kernel.org
parent 7d08c2c9
...@@ -5574,7 +5574,7 @@ static inline int perf_fget_light(int fd, struct fd *p) ...@@ -5574,7 +5574,7 @@ static inline int perf_fget_light(int fd, struct fd *p)
static int perf_event_set_output(struct perf_event *event, static int perf_event_set_output(struct perf_event *event,
struct perf_event *output_event); struct perf_event *output_event);
static int perf_event_set_filter(struct perf_event *event, void __user *arg); static int perf_event_set_filter(struct perf_event *event, void __user *arg);
static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd); static int perf_event_set_bpf_prog(struct perf_event *event, struct bpf_prog *prog);
static int perf_copy_attr(struct perf_event_attr __user *uattr, static int perf_copy_attr(struct perf_event_attr __user *uattr,
struct perf_event_attr *attr); struct perf_event_attr *attr);
...@@ -5637,7 +5637,22 @@ static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned lon ...@@ -5637,7 +5637,22 @@ static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned lon
return perf_event_set_filter(event, (void __user *)arg); return perf_event_set_filter(event, (void __user *)arg);
case PERF_EVENT_IOC_SET_BPF: case PERF_EVENT_IOC_SET_BPF:
return perf_event_set_bpf_prog(event, arg); {
struct bpf_prog *prog;
int err;
prog = bpf_prog_get(arg);
if (IS_ERR(prog))
return PTR_ERR(prog);
err = perf_event_set_bpf_prog(event, prog);
if (err) {
bpf_prog_put(prog);
return err;
}
return 0;
}
case PERF_EVENT_IOC_PAUSE_OUTPUT: { case PERF_EVENT_IOC_PAUSE_OUTPUT: {
struct perf_buffer *rb; struct perf_buffer *rb;
...@@ -9923,10 +9938,8 @@ static void bpf_overflow_handler(struct perf_event *event, ...@@ -9923,10 +9938,8 @@ static void bpf_overflow_handler(struct perf_event *event,
event->orig_overflow_handler(event, data, regs); event->orig_overflow_handler(event, data, regs);
} }
static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd) static int perf_event_set_bpf_handler(struct perf_event *event, struct bpf_prog *prog)
{ {
struct bpf_prog *prog;
if (event->overflow_handler_context) if (event->overflow_handler_context)
/* hw breakpoint or kernel counter */ /* hw breakpoint or kernel counter */
return -EINVAL; return -EINVAL;
...@@ -9934,9 +9947,8 @@ static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd) ...@@ -9934,9 +9947,8 @@ static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd)
if (event->prog) if (event->prog)
return -EEXIST; return -EEXIST;
prog = bpf_prog_get_type(prog_fd, BPF_PROG_TYPE_PERF_EVENT); if (prog->type != BPF_PROG_TYPE_PERF_EVENT)
if (IS_ERR(prog)) return -EINVAL;
return PTR_ERR(prog);
if (event->attr.precise_ip && if (event->attr.precise_ip &&
prog->call_get_stack && prog->call_get_stack &&
...@@ -9952,7 +9964,6 @@ static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd) ...@@ -9952,7 +9964,6 @@ static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd)
* attached to perf_sample_data, do not allow attaching BPF * attached to perf_sample_data, do not allow attaching BPF
* program that calls bpf_get_[stack|stackid]. * program that calls bpf_get_[stack|stackid].
*/ */
bpf_prog_put(prog);
return -EPROTO; return -EPROTO;
} }
...@@ -9974,7 +9985,7 @@ static void perf_event_free_bpf_handler(struct perf_event *event) ...@@ -9974,7 +9985,7 @@ static void perf_event_free_bpf_handler(struct perf_event *event)
bpf_prog_put(prog); bpf_prog_put(prog);
} }
#else #else
static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd) static int perf_event_set_bpf_handler(struct perf_event *event, struct bpf_prog *prog)
{ {
return -EOPNOTSUPP; return -EOPNOTSUPP;
} }
...@@ -10002,14 +10013,12 @@ static inline bool perf_event_is_tracing(struct perf_event *event) ...@@ -10002,14 +10013,12 @@ static inline bool perf_event_is_tracing(struct perf_event *event)
return false; return false;
} }
static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd) static int perf_event_set_bpf_prog(struct perf_event *event, struct bpf_prog *prog)
{ {
bool is_kprobe, is_tracepoint, is_syscall_tp; bool is_kprobe, is_tracepoint, is_syscall_tp;
struct bpf_prog *prog;
int ret;
if (!perf_event_is_tracing(event)) if (!perf_event_is_tracing(event))
return perf_event_set_bpf_handler(event, prog_fd); return perf_event_set_bpf_handler(event, prog);
is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_UKPROBE; is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_UKPROBE;
is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT; is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT;
...@@ -10018,38 +10027,24 @@ static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd) ...@@ -10018,38 +10027,24 @@ static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
/* bpf programs can only be attached to u/kprobe or tracepoint */ /* bpf programs can only be attached to u/kprobe or tracepoint */
return -EINVAL; return -EINVAL;
prog = bpf_prog_get(prog_fd);
if (IS_ERR(prog))
return PTR_ERR(prog);
if ((is_kprobe && prog->type != BPF_PROG_TYPE_KPROBE) || if ((is_kprobe && prog->type != BPF_PROG_TYPE_KPROBE) ||
(is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT) || (is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT) ||
(is_syscall_tp && prog->type != BPF_PROG_TYPE_TRACEPOINT)) { (is_syscall_tp && prog->type != BPF_PROG_TYPE_TRACEPOINT))
/* valid fd, but invalid bpf program type */
bpf_prog_put(prog);
return -EINVAL; return -EINVAL;
}
/* Kprobe override only works for kprobes, not uprobes. */ /* Kprobe override only works for kprobes, not uprobes. */
if (prog->kprobe_override && if (prog->kprobe_override &&
!(event->tp_event->flags & TRACE_EVENT_FL_KPROBE)) { !(event->tp_event->flags & TRACE_EVENT_FL_KPROBE))
bpf_prog_put(prog);
return -EINVAL; return -EINVAL;
}
if (is_tracepoint || is_syscall_tp) { if (is_tracepoint || is_syscall_tp) {
int off = trace_event_get_offsets(event->tp_event); int off = trace_event_get_offsets(event->tp_event);
if (prog->aux->max_ctx_offset > off) { if (prog->aux->max_ctx_offset > off)
bpf_prog_put(prog);
return -EACCES; return -EACCES;
}
} }
ret = perf_event_attach_bpf_prog(event, prog); return perf_event_attach_bpf_prog(event, prog);
if (ret)
bpf_prog_put(prog);
return ret;
} }
static void perf_event_free_bpf_prog(struct perf_event *event) static void perf_event_free_bpf_prog(struct perf_event *event)
...@@ -10071,7 +10066,7 @@ static void perf_event_free_filter(struct perf_event *event) ...@@ -10071,7 +10066,7 @@ static void perf_event_free_filter(struct perf_event *event)
{ {
} }
static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd) static int perf_event_set_bpf_prog(struct perf_event *event, struct bpf_prog *prog)
{ {
return -ENOENT; return -ENOENT;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment