Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.kernel > #1473914 > unrolled thread
| Started by | Alexei Starovoitov <ast@fb.com> |
|---|---|
| First post | 2016-09-01 00:00 +0200 |
| Last post | 2016-09-01 18:40 +0200 |
| Articles | 4 — 3 participants |
Back to article view | Back to linux.kernel
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
[PATCH v2 net-next 4/6] perf, bpf: add perf events core support for BPF_PROG_TYPE_PERF_EVENT programs Alexei Starovoitov <ast@fb.com> - 2016-09-01 00:00 +0200
Re: [PATCH v2 net-next 4/6] perf, bpf: add perf events core support for BPF_PROG_TYPE_PERF_EVENT programs Peter Zijlstra <peterz@infradead.org> - 2016-09-01 10:20 +0200
Re: [PATCH v2 net-next 4/6] perf, bpf: add perf events core support for BPF_PROG_TYPE_PERF_EVENT programs Alexei Starovoitov <alexei.starovoitov@gmail.com> - 2016-09-01 18:00 +0200
Re: [PATCH v2 net-next 4/6] perf, bpf: add perf events core support for BPF_PROG_TYPE_PERF_EVENT programs Peter Zijlstra <peterz@infradead.org> - 2016-09-01 18:40 +0200
| From | Alexei Starovoitov <ast@fb.com> |
|---|---|
| Date | 2016-09-01 00:00 +0200 |
| Subject | [PATCH v2 net-next 4/6] perf, bpf: add perf events core support for BPF_PROG_TYPE_PERF_EVENT programs |
| Message-ID | <sclI5-7NG-7@gated-at.bofh.it> |
Allow attaching BPF_PROG_TYPE_PERF_EVENT programs to sw and hw perf events
via overflow_handler mechanism.
When program is attached the overflow_handlers become stacked.
The program acts as a filter.
Returning zero from the program means that the normal perf_event_output handler
will not be called and sampling event won't be stored in the ring buffer.
The overflow_handler_context==NULL is an additional safety check
to make sure programs are not attached to hw breakpoints and watchdog
in case other checks (that prevent that now anyway) get accidentally
relaxed in the future.
The program refcnt is incremented in case perf_events are inhereted
when target task is forked.
Similar to kprobe and tracepoint programs there is no ioctl to
detach the program or swap already attached program. The user space
expected to close(perf_event_fd) like it does right now for kprobe+bpf.
That restriction simplifies the code quite a bit.
The invocation of overflow_handler in __perf_event_overflow() is now
done via READ_ONCE, since that pointer can be replaced when the program
is attached while perf_event itself could have been active already.
There is no need to do similar treatment for event->prog, since it's
assigned only once before it's accessed.
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
include/linux/bpf.h | 4 +++
include/linux/perf_event.h | 2 ++
kernel/events/core.c | 85 +++++++++++++++++++++++++++++++++++++++++++++-
3 files changed, 90 insertions(+), 1 deletion(-)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 11134238417d..9a904f63f8c1 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -297,6 +297,10 @@ static inline struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
static inline void bpf_prog_put(struct bpf_prog *prog)
{
}
+static inline struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
+{
+ return ERR_PTR(-EOPNOTSUPP);
+}
#endif /* CONFIG_BPF_SYSCALL */
/* verifier prototypes for helper functions called from eBPF programs */
diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
index 97bfe62f30d7..dcaaaf3ec8e6 100644
--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -679,6 +679,8 @@ struct perf_event {
u64 (*clock)(void);
perf_overflow_handler_t overflow_handler;
void *overflow_handler_context;
+ perf_overflow_handler_t orig_overflow_handler;
+ struct bpf_prog *prog;
#ifdef CONFIG_EVENT_TRACING
struct trace_event_call *tp_event;
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 3cfabdf7b942..305433ab2447 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -7022,7 +7022,7 @@ static int __perf_event_overflow(struct perf_event *event,
irq_work_queue(&event->pending);
}
- event->overflow_handler(event, data, regs);
+ READ_ONCE(event->overflow_handler)(event, data, regs);
if (*perf_event_fasync(event) && event->pending_kill) {
event->pending_wakeup = 1;
@@ -7637,11 +7637,75 @@ static void perf_event_free_filter(struct perf_event *event)
ftrace_profile_free_filter(event);
}
+static void bpf_overflow_handler(struct perf_event *event,
+ struct perf_sample_data *data,
+ struct pt_regs *regs)
+{
+ struct bpf_perf_event_data_kern ctx = {
+ .data = data,
+ .regs = regs,
+ };
+ int ret = 0;
+
+#ifdef CONFIG_BPF_SYSCALL
+ preempt_disable();
+ if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1))
+ goto out;
+ rcu_read_lock();
+ ret = BPF_PROG_RUN(event->prog, (void *)&ctx);
+ rcu_read_unlock();
+ out:
+ __this_cpu_dec(bpf_prog_active);
+ preempt_enable();
+#endif
+ if (!ret)
+ return;
+
+ event->orig_overflow_handler(event, data, regs);
+}
+
+static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd)
+{
+ struct bpf_prog *prog;
+
+ if (event->overflow_handler_context)
+ /* hw breakpoint or kernel counter */
+ return -EINVAL;
+
+ if (event->prog)
+ return -EEXIST;
+
+ prog = bpf_prog_get_type(prog_fd, BPF_PROG_TYPE_PERF_EVENT);
+ if (IS_ERR(prog))
+ return PTR_ERR(prog);
+
+ event->prog = prog;
+ event->orig_overflow_handler = READ_ONCE(event->overflow_handler);
+ WRITE_ONCE(event->overflow_handler, bpf_overflow_handler);
+ return 0;
+}
+
+static void perf_event_free_bpf_handler(struct perf_event *event)
+{
+ struct bpf_prog *prog = event->prog;
+
+ if (!prog)
+ return;
+
+ WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler);
+ event->prog = NULL;
+ bpf_prog_put(prog);
+}
+
static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
{
bool is_kprobe, is_tracepoint;
struct bpf_prog *prog;
+ if (event->attr.type == PERF_TYPE_HARDWARE ||
+ event->attr.type == PERF_TYPE_SOFTWARE)
+ return perf_event_set_bpf_handler(event, prog_fd);
+
if (event->attr.type != PERF_TYPE_TRACEPOINT)
return -EINVAL;
@@ -7682,6 +7746,8 @@ static void perf_event_free_bpf_prog(struct perf_event *event)
{
struct bpf_prog *prog;
+ perf_event_free_bpf_handler(event);
+
if (!event->tp_event)
return;
@@ -7710,6 +7776,12 @@ static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
static void perf_event_free_bpf_prog(struct perf_event *event)
{
}
+
+static void bpf_overflow_handler(struct perf_event *event,
+ struct perf_sample_data *data,
+ struct pt_regs *regs)
+{
+}
#endif /* CONFIG_EVENT_TRACING */
#ifdef CONFIG_HAVE_HW_BREAKPOINT
@@ -8998,6 +9070,17 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,
if (!overflow_handler && parent_event) {
overflow_handler = parent_event->overflow_handler;
context = parent_event->overflow_handler_context;
+ if (overflow_handler == bpf_overflow_handler) {
+ struct bpf_prog *prog = bpf_prog_inc(parent_event->prog);
+
+ if (IS_ERR(prog)) {
+ err = PTR_ERR(prog);
+ goto err_ns;
+ }
+ event->prog = prog;
+ event->orig_overflow_handler =
+ parent_event->orig_overflow_handler;
+ }
}
if (overflow_handler) {
--
2.8.0
[toc] | [next] | [standalone]
| From | Peter Zijlstra <peterz@infradead.org> |
|---|---|
| Date | 2016-09-01 10:20 +0200 |
| Subject | Re: [PATCH v2 net-next 4/6] perf, bpf: add perf events core support for BPF_PROG_TYPE_PERF_EVENT programs |
| Message-ID | <scvo5-6SP-13@gated-at.bofh.it> |
| In reply to | #1473914 |
On Wed, Aug 31, 2016 at 02:50:41PM -0700, Alexei Starovoitov wrote:
> diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
> index 97bfe62f30d7..dcaaaf3ec8e6 100644
> --- a/include/linux/perf_event.h
> +++ b/include/linux/perf_event.h
> @@ -679,6 +679,8 @@ struct perf_event {
> u64 (*clock)(void);
> perf_overflow_handler_t overflow_handler;
> void *overflow_handler_context;
> + perf_overflow_handler_t orig_overflow_handler;
> + struct bpf_prog *prog;
Should we put that under CONFIG_BPF_SYSCALL too?
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index 3cfabdf7b942..305433ab2447 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -7637,11 +7637,75 @@ static void perf_event_free_filter(struct perf_event *event)
> ftrace_profile_free_filter(event);
> }
>
> +static void bpf_overflow_handler(struct perf_event *event,
> + struct perf_sample_data *data,
> + struct pt_regs *regs)
> +{
> + struct bpf_perf_event_data_kern ctx = {
> + .data = data,
> + .regs = regs,
> + };
> + int ret = 0;
> +
> +#ifdef CONFIG_BPF_SYSCALL
> + preempt_disable();
> + if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1))
> + goto out;
> + rcu_read_lock();
> + ret = BPF_PROG_RUN(event->prog, (void *)&ctx);
> + rcu_read_unlock();
> + out:
Please, no leading space before labels. Use something like:
[diff "default"]
xfuncname = "^[[:alpha:]$_].*[^:]$"
In your .gitconfig if you want to keep diff output 'sane'.
> + __this_cpu_dec(bpf_prog_active);
> + preempt_enable();
> +#endif
> + if (!ret)
> + return;
> +
> + event->orig_overflow_handler(event, data, regs);
> +}
Other than that, ACK.
[toc] | [prev] | [next] | [standalone]
| From | Alexei Starovoitov <alexei.starovoitov@gmail.com> |
|---|---|
| Date | 2016-09-01 18:00 +0200 |
| Subject | Re: [PATCH v2 net-next 4/6] perf, bpf: add perf events core support for BPF_PROG_TYPE_PERF_EVENT programs |
| Message-ID | <scCzg-3Lb-3@gated-at.bofh.it> |
| In reply to | #1474160 |
On Thu, Sep 01, 2016 at 10:12:51AM +0200, Peter Zijlstra wrote:
> On Wed, Aug 31, 2016 at 02:50:41PM -0700, Alexei Starovoitov wrote:
> > diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
> > index 97bfe62f30d7..dcaaaf3ec8e6 100644
> > --- a/include/linux/perf_event.h
> > +++ b/include/linux/perf_event.h
> > @@ -679,6 +679,8 @@ struct perf_event {
> > u64 (*clock)(void);
> > perf_overflow_handler_t overflow_handler;
> > void *overflow_handler_context;
>
> > + perf_overflow_handler_t orig_overflow_handler;
> > + struct bpf_prog *prog;
>
> Should we put that under CONFIG_BPF_SYSCALL too?
sure. will do.
> > diff --git a/kernel/events/core.c b/kernel/events/core.c
> > index 3cfabdf7b942..305433ab2447 100644
> > --- a/kernel/events/core.c
> > +++ b/kernel/events/core.c
>
> > @@ -7637,11 +7637,75 @@ static void perf_event_free_filter(struct perf_event *event)
> > ftrace_profile_free_filter(event);
> > }
> >
> > +static void bpf_overflow_handler(struct perf_event *event,
> > + struct perf_sample_data *data,
> > + struct pt_regs *regs)
> > +{
> > + struct bpf_perf_event_data_kern ctx = {
> > + .data = data,
> > + .regs = regs,
> > + };
> > + int ret = 0;
> > +
> > +#ifdef CONFIG_BPF_SYSCALL
> > + preempt_disable();
> > + if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1))
> > + goto out;
> > + rcu_read_lock();
> > + ret = BPF_PROG_RUN(event->prog, (void *)&ctx);
> > + rcu_read_unlock();
> > + out:
>
> Please, no leading space before labels. Use something like:
good catch.
sadly checkpatch didn't complain.
> [diff "default"]
> xfuncname = "^[[:alpha:]$_].*[^:]$"
>
> In your .gitconfig if you want to keep diff output 'sane'.
interesting trick. don't remember being bitten by it.
This extra space was a typo.
> > + __this_cpu_dec(bpf_prog_active);
> > + preempt_enable();
> > +#endif
> > + if (!ret)
> > + return;
> > +
> > + event->orig_overflow_handler(event, data, regs);
> > +}
>
> Other than that, ACK.
Thanks!
[toc] | [prev] | [next] | [standalone]
| From | Peter Zijlstra <peterz@infradead.org> |
|---|---|
| Date | 2016-09-01 18:40 +0200 |
| Subject | Re: [PATCH v2 net-next 4/6] perf, bpf: add perf events core support for BPF_PROG_TYPE_PERF_EVENT programs |
| Message-ID | <scDbY-4hA-27@gated-at.bofh.it> |
| In reply to | #1474507 |
On Thu, Sep 01, 2016 at 08:53:51AM -0700, Alexei Starovoitov wrote: > On Thu, Sep 01, 2016 at 10:12:51AM +0200, Peter Zijlstra wrote: > > Please, no leading space before labels. Use something like: > > good catch. > sadly checkpatch didn't complain. Right, a lot of people do it on purpose to avoid diff seeing the label as the function for --show-c-function. I just find the style annoying and 'fixed' diff. Now, if the option would've been called --show-function (without the explicit reference to C) one could argue that seeing this form is useful for .S files but... ;-) > > [diff "default"] > > xfuncname = "^[[:alpha:]$_].*[^:]$" > > > > In your .gitconfig if you want to keep diff output 'sane'. > > interesting trick. don't remember being bitten by it. > This extra space was a typo. Example below, see the difference in the @@ line. I find the first (fixed) to be much more useful. With: diff --git a/kernel/events/core.c b/kernel/events/core.c index 8b3ad4e26548..cc2af0188924 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -6238,6 +6238,7 @@ static void perf_pmu_output_stop(struct perf_event *event) goto restart; } } + /* ponies */ rcu_read_unlock(); } Without: diff --git a/kernel/events/core.c b/kernel/events/core.c index 8b3ad4e26548..cc2af0188924 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -6238,6 +6238,7 @@ restart: goto restart; } } + /* ponies */ rcu_read_unlock(); }
[toc] | [prev] | [standalone]
Back to top | Article view | linux.kernel
csiph-web