Skip to contentSkip to conversation

bpf: Add BPF support to all perf_event

PATCH Superseded Alexei Starovoitov 4 participants 14 messages May 26, 2017 – Jun 1, 2017

Subsystems (7)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover bpf: Add BPF support to all perf_event

v1->v2: address Peter's feedback. Refactor patch 1 to allow attaching bpf programs to all event types and reading counters from all of them as well patch 2 - more tests
patch 3 - address Dave's feedback and document bpf_perf_event_read() and bpf_perf_event_output() properly

Teng Qin (3):
perf, bpf: Add BPF support to all perf_event types samples/bpf: add samples for more perf event types
bpf: update perf event helper functions documentation

 include/uapi/linux/bpf.h       |  11 ++-
 kernel/bpf/arraymap.c          |  26 +++---
 kernel/events/core.c           |   6 +-
 kernel/trace/bpf_trace.c       |   4 +-
 samples/bpf/bpf_helpers.h      |   3 +-
 samples/bpf/trace_event_user.c |  46 ++++++++++-
 samples/bpf/tracex6_kern.c     |  28 +++++--
 samples/bpf/tracex6_user.c     | 176 ++++++++++++++++++++++++++++++++---------
 tools/include/uapi/linux/bpf.h |  11 ++-
 9 files changed, 232 insertions(+), 79 deletions(-)
Signature · 1 line
2.9.3
#1 lore raw

PATCH v2 1/3 perf, bpf: Add BPF support to all perf_event types

From: Teng Qin <[email protected]>

Allow BPF program to attach to all perf_event types supported by the current bpf and perf code logic, including HW_CACHE, RAW, and dynamic pmu events.

Also add support for reading these event counters using bpf_perf_event_read() helper.

---
 kernel/bpf/arraymap.c    | 26 +++++++++++---------------
 kernel/events/core.c     |  6 +-----
 kernel/trace/bpf_trace.c |  4 ++--
 3 files changed, 14 insertions(+), 22 deletions(-)
kernel/bpf/arraymap.c +11 -15
index 5e00b2333c26..55ffa9949128 100644
@@ -462,26 +462,22 @@ static void *perf_event_fd_array_get_ptr(struct bpf_map *map,
 
 	event = perf_file->private_data;
 	ee = ERR_PTR(-EINVAL);
+	/* Per-task events are not supported */
+	if (event->attach_state & PERF_ATTACH_TASK)
+		goto err_out;
 
 	attr = perf_event_attrs(event);
 	if (IS_ERR(attr) || attr->inherit)
 		goto err_out;
+	/* TRACEPOINT and BREAKPOINT not supported in perf_event_read_local */
+	if (attr->type == PERF_TYPE_TRACEPOINT ||
+	    attr->type == PERF_TYPE_BREAKPOINT)
+		goto err_out;
 
-	switch (attr->type) {
-	case PERF_TYPE_SOFTWARE:
-		if (attr->config != PERF_COUNT_SW_BPF_OUTPUT)
-			goto err_out;
-		/* fall-through */
-	case PERF_TYPE_RAW:
-	case PERF_TYPE_HARDWARE:
-		ee = bpf_event_entry_gen(perf_file, map_file);
-		if (ee)
-			return ee;
-		ee = ERR_PTR(-ENOMEM);
-		/* fall-through */
-	default:
-		break;
-	}
+	ee = bpf_event_entry_gen(perf_file, map_file);
+	if (ee)
+		return ee;
+	ee = ERR_PTR(-ENOMEM);
 
 err_out:
 	fput(perf_file);
kernel/events/core.c +1 -5
index 6e75a5c9412d..52f667046599 100644
@@ -8037,12 +8037,8 @@ 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;
+		return perf_event_set_bpf_handler(event, prog_fd);
 
 	if (event->tp_event->prog)
 		return -EEXIST;
kernel/trace/bpf_trace.c +2 -2
index 460a031c77e5..8425bf193f39 100644
@@ -248,8 +248,8 @@ BPF_CALL_2(bpf_perf_event_read, struct bpf_map *, map, u64, flags)
 		return -ENOENT;
 
 	event = ee->event;
-	if (unlikely(event->attr.type != PERF_TYPE_HARDWARE &&
-		     event->attr.type != PERF_TYPE_RAW))
+	if (unlikely(event->attr.type == PERF_TYPE_SOFTWARE &&
+		     event->attr.config == PERF_COUNT_SW_BPF_OUTPUT))
 		return -EINVAL;
 
 	/* make sure event is local and doesn't have pmu::count */
Signature · 1 line
2.9.3
#2 lore raw

Re: [PATCH v2 net-next 1/3] perf, bpf: Add BPF support to all perf_event types

url: https://github.com/0day-ci/linux/commits/Alexei-Starovoitov/bpf-Add-BPF-support-to-all-perf_event/20170526-171542 config: ia64-allmodconfig (attached as .config) compiler: ia64-linux-gcc (GCC)…

Hi Teng,

[auto build test ERROR on net-next/master]

url: https://github.com/0day-ci/linux/commits/Alexei-Starovoitov/bpf-Add-BPF-support-to-all-perf_event/20170526-171542
config: ia64-allmodconfig (attached as .config)
compiler: ia64-linux-gcc (GCC) 6.2.0
reproduce:
wget https://raw.githubusercontent.com/01org/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=ia64

All errors (new ones prefixed by >>):

kernel//bpf/arraymap.c: In function 'perf_event_fd_array_get_ptr':

>> kernel//bpf/arraymap.c:466:11: error: 'struct perf_event' has no member named 'attach_state'
     if (event->attach_state & PERF_ATTACH_TASK)
              ^~

vim +466 kernel//bpf/arraymap.c

460 if (IS_ERR(perf_file))
461 return perf_file;
462
463 event = perf_file->private_data;
464 ee = ERR_PTR(-EINVAL);
465 /* Per-task events are not supported */

 > 466		if (event->attach_state & PERF_ATTACH_TASK)

467 goto err_out;
468
469 attr = perf_event_attrs(event);

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

maintainer
#3 lore raw

Re: [PATCH v2 net-next 1/3] perf, bpf: Add BPF support to all perf_event types

In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>
Date: Thu, 25 May 2017 22:55:47 -0700

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (event->attach_state & PERF_ATTACH_TASK)
+		goto err_out;

The attach_state member only exists when PERF_EVENTS is enabled.

maintainer
#4 lore raw

Re: [PATCH v2 net-next 1/3] perf, bpf: Add BPF support to all perf_event types

In reply to Alexei Starovoitov
Quoted 4 lines · Alexei Starovoitov

+++ b/kernel/bpf/arraymap.c

kernel/bpf/arraymap.c +0 -0
@@ -462,26 +462,22 @@ static void *perf_event_fd_array_get_ptr(struct bpf_map *map,
 
 	event = perf_file->private_data;

kernel/bpf/arraymap.c static void *perf_event_fd_array_get_ptr(struct bpf_map *map, · line 473

Quoted 10 lines
kernel/bpf/arraymap.c +4 -0
 	ee = ERR_PTR(-EINVAL);
+	/* Per-task events are not supported */
+	if (event->attach_state & PERF_ATTACH_TASK)
+		goto err_out;
 
 	attr = perf_event_attrs(event);
 	if (IS_ERR(attr) || attr->inherit)
 		goto err_out;
 
+	/* TRACEPOINT and BREAKPOINT not supported in perf_event_read_local */

I cannot find reason for this comment. That is, why would perf_event_read_local() not support those two types?

Quoted 11 lines
kernel/bpf/arraymap.c +7 -0
+	if (attr->type == PERF_TYPE_TRACEPOINT ||
+	    attr->type == PERF_TYPE_BREAKPOINT)
+		goto err_out;
 
+	ee = bpf_event_entry_gen(perf_file, map_file);
+	if (ee)
+		return ee;
+	ee = ERR_PTR(-ENOMEM);
 
 err_out:
 	fput(perf_file);
maintainer
#5 lore raw

Re: [PATCH v2 net-next 1/3] perf, bpf: Add BPF support to all perf_event types

Quoted 24 lines · Peter Zijlstra
Quoted 4 lines · Alexei Starovoitov

+++ b/kernel/bpf/arraymap.c

(quoted diff) +0 -0
@@ -462,26 +462,22 @@ static void *perf_event_fd_array_get_ptr(struct bpf_map *map,
 
 	event = perf_file->private_data;

Review static void *perf_event_fd_array_get_ptr(struct bpf_map *map, · line 473

Quoted 10 lines
(quoted diff) +4 -0
 	ee = ERR_PTR(-EINVAL);
+	/* Per-task events are not supported */
+	if (event->attach_state & PERF_ATTACH_TASK)
+		goto err_out;
 
 	attr = perf_event_attrs(event);
 	if (IS_ERR(attr) || attr->inherit)
 		goto err_out;
 
+	/* TRACEPOINT and BREAKPOINT not supported in perf_event_read_local */

I cannot find reason for this comment. That is, why would perf_event_read_local() not support those two types?

Quoted 4 lines
(quoted diff) +3 -0
+	if (attr->type == PERF_TYPE_TRACEPOINT ||
+	    attr->type == PERF_TYPE_BREAKPOINT)
+		goto err_out;
 
Quoted 7 lines

Review static void *perf_event_fd_array_get_ptr(struct bpf_map *map,

Quoted 7 lines
(quoted diff) +4 -0
+	ee = bpf_event_entry_gen(perf_file, map_file);
+	if (ee)
+		return ee;
+	ee = ERR_PTR(-ENOMEM);
 
 err_out:
 	fput(perf_file);

Do we want something like the below to replace much of the above?

	if (!perf_event_valid_local(event, NULL, cpu))
		goto err_out;

Seems to be roughly what you're after, although I suppose @cpu might be hard to determine a priory, so maybe we should allow a magic value to short-circuit that test.

---
 kernel/events/core.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
kernel/events/core.c +30 -0
index 8d6acaeeea17..a7dc34f19568 100644
@@ -3630,6 +3630,36 @@ static inline u64 perf_event_count(struct perf_event *event)
 }
 
 /*
+ * perf_event_valid_local() - validates if the event is usable by perf_event_read_local()
+ * event: the event to validate
+ * task:  the task the @event will be used in
+ * cpu:   the cpu the @event will be used on
+ *
+ * In case one wants to disallow all per-task events, use @task = NULL.
+ * In case one wants to disallow all per-cpu events, use @cpu = -1.
+ */
+bool perf_event_valid_local(struct perf_event *event, struct task_struct *task, int cpu)
+{
+	/* See perf_event_read_local() for the reasons for these tests */
+
+	if ((event->attach_state & PERF_ATTACH_TASK) &&
+	    event->hw.target != task)
+		return false;
+
+	if (!(event->attach_state & PERF_ATTACH_TASK) &&
+	    event->cpu != cpu)
+		return false;
+
+	if (event->attr.inherit)
+		return false;
+
+	if (event->pmu->count)
+		return false;
+
+	return true;
+}
+
+/*
  * NMI-safe method to read a local event, that is an event that
  * is:
  *   - either for the current task, or for this CPU
#6 lore raw

Re: [PATCH v2 net-next 1/3] perf, bpf: Add BPF support to all perf_event types

Quoted 32 lines · Peter Zijlstra

Do we want something like the below to replace much of the above?

	if (!perf_event_valid_local(event, NULL, cpu))
		goto err_out;

Seems to be roughly what you're after, although I suppose @cpu might be hard to determine a priory, so maybe we should allow a magic value to short-circuit that test.

---
 kernel/events/core.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
kernel/events/core.c +9 -0
index 8d6acaeeea17..a7dc34f19568 100644
@@ -3630,6 +3630,36 @@ static inline u64 perf_event_count(struct perf_event *event)
 }
 
 /*
+ * perf_event_valid_local() - validates if the event is usable by perf_event_read_local()
+ * event: the event to validate
+ * task:  the task the @event will be used in
+ * cpu:   the cpu the @event will be used on
+ *
+ * In case one wants to disallow all per-task events, use @task = NULL.
+ * In case one wants to disallow all per-cpu events, use @cpu = -1.
+ */
+bool perf_event_valid_local(struct perf_event *event, struct task_struct *task, int cpu)

kernel/events/core.c static inline u64 perf_event_count(struct perf_event *event) · line 3651

Quoted 10 lines
kernel/events/core.c +10 -0
+{
+	/* See perf_event_read_local() for the reasons for these tests */
+
+	if ((event->attach_state & PERF_ATTACH_TASK) &&
+	    event->hw.target != task)
+		return false;
+
+	if (!(event->attach_state & PERF_ATTACH_TASK) &&
+	    event->cpu != cpu)
+		return false;

we do if (unlikely(event->oncpu != cpu))
as dynamic check inside bpf_perf_event_read(), since we cannot do it statically at perf_event_array update time.
If we drop the above 'if' and keep 'task==null' trick, then indeed we can use this function as static check.

Right now we're trying to keep as many checks as possible as static checks to make bpf_perf_event_read() faster.
I guess we can drop that approach and do perf_event_valid_local() check for every read since perf_event_read_local() does all the same checks anyway.
So how about converting all WARN_ON in perf_event_read_local() into 'return -EINVAL' and change func proto into:
int perf_event_read_local(struct perf_event *even, u64 *counter_val)

Quoted 2 lines

I cannot find reason for this comment. That is, why would perf_event_read_local() not support those two types?

I don't know. What is the meaning of
reading tracepoint/breakpoint counter?
Because of 'event->oncpu != cpu' dynamic check all counters are expected to be per-cpu. I'm not sure how uncore counters work. What do they have in event->oncpu? -1? I guess they have pmu->count? So we cannot read them from bpf program anyway?

If we change warn_ons in perf_event_read_local() to returns them we can make per-task counters working.
User side will open per-task counter and bpf program will do current->pid != expected_pid check before calling bpf_perf_event_read(). bpf scripts often do that already.

int perf_event_read_local(struct perf_event *even, u64 *counter_val) {
local_irq_save(flags);
if ((event->attach_state & PERF_ATTACH_TASK) &&

       event->hw.target != current)
    return -EINVAL;

if (!(event->attach_state & PERF_ATTACH_TASK) &&

      event->cpu != smp_processor_id()
    return -EINVAL;

... inherit and pmu->count checks here ...
*counter_val = local64_read(&event->count)
local_irq_restore(flags);
return 0;
}

thoughts?

maintainer
#7 lore raw

Re: [PATCH v2 net-next 1/3] perf, bpf: Add BPF support to all perf_event types

Quoted 7 lines · Alexei Starovoitov

kernel/events/core.c static inline u64 perf_event_count(struct perf_event *event) · line 3651

Quoted 3 lines
kernel/events/core.c +3 -0
+	if (!(event->attach_state & PERF_ATTACH_TASK) &&
+	    event->cpu != cpu)
+		return false;

we do if (unlikely(event->oncpu != cpu))
as dynamic check inside bpf_perf_event_read(), since we cannot do it statically at perf_event_array update time.

Right, that's what I thought.

Quoted 2 lines

If we drop the above 'if' and keep 'task==null' trick, then indeed we can use this function as static check.

Right, or otherwise have a special value to disable it.

Quoted 8 lines

Right now we're trying to keep as many checks as possible as static checks to make bpf_perf_event_read() faster.
I guess we can drop that approach and do perf_event_valid_local() check for every read since perf_event_read_local() does all the same checks anyway.
So how about converting all WARN_ON in perf_event_read_local() into 'return -EINVAL' and change func proto into:
int perf_event_read_local(struct perf_event *even, u64 *counter_val)

I'm confused on how that is better. My recent patches to WARN should have greatly improved performance of WARN_ON_ONCE(). And looking at that code, I suspect its dominated by the POPF for inactive events.

Quoted 5 lines
Quoted 2 lines

I cannot find reason for this comment. That is, why would perf_event_read_local() not support those two types?

I don't know. What is the meaning of
reading tracepoint/breakpoint counter?

They count like all other software events. +1 for each occurrence.

So for instance, if you use irq_vectors:local_timer_entry you get how many cpu local timer instances happened during your measurement window.

Same with a breakpoint, it counts how many times it got hit. Typically you'd want to install a custom handler on breakpoints to do something 'interesting', but even without that its acts like a normal software event.

Quoted 2 lines

Because of 'event->oncpu != cpu' dynamic check all counters are expected to be per-cpu. I'm not sure how uncore counters work.

Uncore thingies are assigned to any online CPU in their 'domain'.

Quoted 2 lines

What do they have in event->oncpu? -1? I guess they have pmu->count? So we cannot read them from bpf program anyway?

They have the CPU number of the CPU that's assigned to them. So you _could_ make use of them, but its a bit tricky to get them to work reliably because you'd have to get that CPU 'right' and it can change.

Typically they would end up on the first CPU in their domain, but with CPU hotplug you can move them about and get confusion.

I'd have to think on how to do that nicely.

Quoted 2 lines

If we change warn_ons in perf_event_read_local() to returns them we can make per-task counters working.

I'm not entirely sure I see how that is required. Should per task not already work? The WARN that's there will only trigger if you call them on the wrong task, which is something you shouldn't do anyway.

#8 lore raw

Re: [PATCH v2 net-next 1/3] perf, bpf: Add BPF support to all perf_event types

Quoted 61 lines · Peter Zijlstra
Quoted 7 lines · Alexei Starovoitov

kernel/events/core.c static inline u64 perf_event_count(struct perf_event *event) · line 3651

Quoted 3 lines
kernel/events/core.c +3 -0
+	if (!(event->attach_state & PERF_ATTACH_TASK) &&
+	    event->cpu != cpu)
+		return false;

we do if (unlikely(event->oncpu != cpu))
as dynamic check inside bpf_perf_event_read(), since we cannot do it statically at perf_event_array update time.

Right, that's what I thought.

Quoted 2 lines

If we drop the above 'if' and keep 'task==null' trick, then indeed we can use this function as static check.

Right, or otherwise have a special value to disable it.

Quoted 8 lines

Right now we're trying to keep as many checks as possible as static checks to make bpf_perf_event_read() faster.
I guess we can drop that approach and do perf_event_valid_local() check for every read since perf_event_read_local() does all the same checks anyway.
So how about converting all WARN_ON in perf_event_read_local() into 'return -EINVAL' and change func proto into:
int perf_event_read_local(struct perf_event *even, u64 *counter_val)

I'm confused on how that is better. My recent patches to WARN should have greatly improved performance of WARN_ON_ONCE(). And looking at that code, I suspect its dominated by the POPF for inactive events.

Quoted 5 lines
Quoted 2 lines

I cannot find reason for this comment. That is, why would perf_event_read_local() not support those two types?

I don't know. What is the meaning of
reading tracepoint/breakpoint counter?

They count like all other software events. +1 for each occurrence.

So for instance, if you use irq_vectors:local_timer_entry you get how many cpu local timer instances happened during your measurement window.

Same with a breakpoint, it counts how many times it got hit. Typically you'd want to install a custom handler on breakpoints to do something 'interesting', but even without that its acts like a normal software event.

Quoted 2 lines

Because of 'event->oncpu != cpu' dynamic check all counters are expected to be per-cpu. I'm not sure how uncore counters work.

Uncore thingies are assigned to any online CPU in their 'domain'.

Quoted 2 lines

What do they have in event->oncpu? -1? I guess they have pmu->count? So we cannot read them from bpf program anyway?

They have the CPU number of the CPU that's assigned to them. So you _could_ make use of them, but its a bit tricky to get them to work reliably because you'd have to get that CPU 'right' and it can change.

Typically they would end up on the first CPU in their domain, but with CPU hotplug you can move them about and get confusion.

Quoted 8 lines

I'd have to think on how to do that nicely.

Quoted 2 lines

If we change warn_ons in perf_event_read_local() to returns them we can make per-task counters working.

I'm not entirely sure I see how that is required. Should per task not already work? The WARN that's there will only trigger if you call them on the wrong task, which is something you shouldn't do anyway.

The kernel WARN is considered to be a bug of bpf infra. That's the reason we do all these checks at map update time and at run-time. The bpf program authors should be able to do all possible experiments until their scripts work. Dealing with kernel warns and reboots is not something user space folks like to do.
Today bpf_perf_event_read() for per-task events isn't really working due to event->oncpu != cpu runtime check in there. If we convert warns to returns the existing scripts will continue to work as-is and per-task will be possible.

maintainer
#9 lore raw

Re: [PATCH v2 net-next 1/3] perf, bpf: Add BPF support to all perf_event types

Quoted 6 lines · Alexei Starovoitov
Quoted 3 lines · Peter Zijlstra

I'm not entirely sure I see how that is required. Should per task not already work? The WARN that's there will only trigger if you call them on the wrong task, which is something you shouldn't do anyway.

Quoted 9 lines

The kernel WARN is considered to be a bug of bpf infra. That's the reason we do all these checks at map update time and at run-time. The bpf program authors should be able to do all possible experiments until their scripts work. Dealing with kernel warns and reboots is not something user space folks like to do.
Today bpf_perf_event_read() for per-task events isn't really working due to event->oncpu != cpu runtime check in there. If we convert warns to returns the existing scripts will continue to work as-is and per-task will be possible.

Ah yes.. I always forget that side of things (not ever having seen a bpf thing working doesn't help either of course).

Let me consider that for a bit..

maintainer
#10 lore raw

Re: [PATCH v2 net-next 1/3] perf, bpf: Add BPF support to all perf_event types

Quoted 17 lines · Peter Zijlstra
Quoted 6 lines · Alexei Starovoitov
Quoted 3 lines · Peter Zijlstra

I'm not entirely sure I see how that is required. Should per task not already work? The WARN that's there will only trigger if you call them on the wrong task, which is something you shouldn't do anyway.

Quoted 9 lines

The kernel WARN is considered to be a bug of bpf infra. That's the reason we do all these checks at map update time and at run-time. The bpf program authors should be able to do all possible experiments until their scripts work. Dealing with kernel warns and reboots is not something user space folks like to do.
Today bpf_perf_event_read() for per-task events isn't really working due to event->oncpu != cpu runtime check in there. If we convert warns to returns the existing scripts will continue to work as-is and per-task will be possible.

Quoted 4 lines

Ah yes.. I always forget that side of things (not ever having seen a bpf thing working doesn't help either of course).

Let me consider that for a bit..

OK, do that. Something like the below should do I suppose.

It will return -EOPNOTSUPP for permanent failure (it cannot ever work) and -EINVAL for temporary failure (could work if you call it on another task/cpu).

---
 kernel/events/core.c | 41 +++++++++++++++++++++++++++--------------
 1 file changed, 27 insertions(+), 14 deletions(-)
kernel/events/core.c +27 -14
index 8d6acaeeea17..22b6407da374 100644
@@ -3637,10 +3637,10 @@ static inline u64 perf_event_count(struct perf_event *event)
  *     will not be local and we cannot read them atomically
  *   - must not have a pmu::count method
  */
-u64 perf_event_read_local(struct perf_event *event)
+int perf_event_read_local(struct perf_event *event, u64 *value)
 {
 	unsigned long flags;
-	u64 val;
+	int ret = 0;
 
 	/*
 	 * Disabling interrupts avoids all counter scheduling (context
@@ -3648,25 +3648,37 @@ u64 perf_event_read_local(struct perf_event *event)
 	 */
 	local_irq_save(flags);
 
-	/* If this is a per-task event, it must be for current */
-	WARN_ON_ONCE((event->attach_state & PERF_ATTACH_TASK) &&
-		     event->hw.target != current);
-
-	/* If this is a per-CPU event, it must be for this CPU */
-	WARN_ON_ONCE(!(event->attach_state & PERF_ATTACH_TASK) &&
-		     event->cpu != smp_processor_id());
-
 	/*
 	 * It must not be an event with inherit set, we cannot read
 	 * all child counters from atomic context.
 	 */
-	WARN_ON_ONCE(event->attr.inherit);
+	if (event->attr.inherit) {
+		ret = -EOPNOTSUPP;
+		goto out;
+	}
 
 	/*
 	 * It must not have a pmu::count method, those are not
 	 * NMI safe.
 	 */
-	WARN_ON_ONCE(event->pmu->count);
+	if (event->pmu->count) {
+		ret = -EOPNOTSUPP;
+		goto out;
+	}
+
+	/* If this is a per-task event, it must be for current */
+	if ((event->attach_state & PERF_ATTACH_TASK) &&
+	    event->hw.target != current) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	/* If this is a per-CPU event, it must be for this CPU */
+	if (!(event->attach_state & PERF_ATTACH_TASK) &&
+	    event->cpu != smp_processor_id()) {
+		ret = -EINVAL;
+		goto out;
+	}
 
 	/*
 	 * If the event is currently on this CPU, its either a per-task event,
@@ -3676,10 +3688,11 @@ u64 perf_event_read_local(struct perf_event *event)
 	if (event->oncpu == smp_processor_id())
 		event->pmu->read(event);
 
-	val = local64_read(&event->count);
+	*value = local64_read(&event->count);
+out:
 	local_irq_restore(flags);
 
-	return val;
+	return ret;
 }
 
 static int perf_event_read(struct perf_event *event, bool group)
#11 lore raw

Re: [PATCH v2 net-next 1/3] perf, bpf: Add BPF support to all perf_event types

Quoted 5 lines · Peter Zijlstra

OK, do that. Something like the below should do I suppose.

It will return -EOPNOTSUPP for permanent failure (it cannot ever work) and -EINVAL for temporary failure (could work if you call it on another task/cpu).

Thanks! Will test it and report back.

#12 lore raw

PATCH v2 2/3 samples/bpf: add samples for more perf event types

In reply to Alexei Starovoitov

From: Teng Qin <[email protected]>

This commit adds test code to attach BPF to HW_CACHE and RAW type events and updates clean-up logic to disable the perf events before closing pmu_fd.

This commit also adds test code to read SOFTWARE, HW_CACHE, RAW and dynamic pmu events from BPF program using bpf_perf_event_read(). Refactored the existing sample to fork individual task on each CPU, attach kprobe to more controllable function, and more accurately check if each read on every CPU returned with good value.

---
 samples/bpf/bpf_helpers.h      |   3 +-
 samples/bpf/trace_event_user.c |  46 ++++++++++-
 samples/bpf/tracex6_kern.c     |  28 +++++--
 samples/bpf/tracex6_user.c     | 176 ++++++++++++++++++++++++++++++++---------
 4 files changed, 204 insertions(+), 49 deletions(-)
samples/bpf/bpf_helpers.h +2 -1
index 9a9c95f2c9fb..51e567bc70fc 100644
@@ -31,7 +31,8 @@ static unsigned long long (*bpf_get_current_uid_gid)(void) =
 	(void *) BPF_FUNC_get_current_uid_gid;
 static int (*bpf_get_current_comm)(void *buf, int buf_size) =
 	(void *) BPF_FUNC_get_current_comm;
-static int (*bpf_perf_event_read)(void *map, int index) =
+static unsigned long long (*bpf_perf_event_read)(void *map,
+						 unsigned long long flags) =
 	(void *) BPF_FUNC_perf_event_read;
 static int (*bpf_clone_redirect)(void *ctx, int ifindex, int flags) =
 	(void *) BPF_FUNC_clone_redirect;
samples/bpf/trace_event_user.c +43 -3

89 lines not on this page · raw message

samples/bpf/tracex6_kern.c +21 -7
index be479c4af9e2..646f86426d09 100644
@@ -3,22 +3,36 @@
 #include <uapi/linux/bpf.h>
 #include "bpf_helpers.h"
 
-struct bpf_map_def SEC("maps") my_map = {
+struct bpf_map_def SEC("maps") counters = {
 	.type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
 	.key_size = sizeof(int),
 	.value_size = sizeof(u32),
-	.max_entries = 32,
+	.max_entries = 64,
+};
+struct bpf_map_def SEC("maps") values = {
+	.type = BPF_MAP_TYPE_HASH,
+	.key_size = sizeof(int),
+	.value_size = sizeof(u64),
+	.max_entries = 64,
 };
 
-SEC("kprobe/sys_write")
+SEC("kprobe/htab_map_get_next_key")
 int bpf_prog1(struct pt_regs *ctx)
 {
-	u64 count;
+	u64 count, *val;
+	s64 error;
 	u32 key = bpf_get_smp_processor_id();
-	char fmt[] = "CPU-%d   %llu\n";
 
-	count = bpf_perf_event_read(&my_map, key);
-	bpf_trace_printk(fmt, sizeof(fmt), key, count);
+	count = bpf_perf_event_read(&counters, key);
+	error = (s64)count;
+	if (error < 0 && error > -256)
+		return 0;
+
+	val = bpf_map_lookup_elem(&values, &key);
+	if (val)
+		*val = count;
+	else
+		bpf_map_update_elem(&values, &key, &count, BPF_NOEXIST);
 
 	return 0;
 }
samples/bpf/tracex6_user.c +138 -38

211 lines not on this page · raw message

Signature · 1 line
2.9.3
#13 lore raw

PATCH v2 3/3 bpf: update perf event helper functions documentation

In reply to Alexei Starovoitov

From: Teng Qin <[email protected]>

This commit updates documentation of the bpf_perf_event_output and bpf_perf_event_read helpers to match their implementation.

---
 include/uapi/linux/bpf.h       | 11 +++++++----
 tools/include/uapi/linux/bpf.h | 11 +++++++----
 2 files changed, 14 insertions(+), 8 deletions(-)
include/uapi/linux/bpf.h +7 -4
index 94dfa9def355..e78aece03628 100644
@@ -313,8 +313,11 @@ union bpf_attr {
  *     @flags: room for future extensions
  *     Return: 0 on success or negative error
  *
- * u64 bpf_perf_event_read(&map, index)
- *     Return: Number events read or error code
+ * u64 bpf_perf_event_read(map, flags)
+ *     read perf event counter value
+ *     @map: pointer to perf_event_array map
+ *     @flags: index of event in the map or bitmask flags
+ *     Return: value of perf event counter read or error code
  *
  * int bpf_redirect(ifindex, flags)
  *     redirect to another netdev
@@ -328,11 +331,11 @@ union bpf_attr {
  *     @skb: pointer to skb
  *     Return: realm if != 0
  *
- * int bpf_perf_event_output(ctx, map, index, data, size)
+ * int bpf_perf_event_output(ctx, map, flags, data, size)
  *     output perf raw sample
  *     @ctx: struct pt_regs*
  *     @map: pointer to perf_event_array map
- *     @index: index of event in the map
+ *     @flags: index of event in the map or bitmask flags
  *     @data: data on stack to be output as raw data
  *     @size: size of data
  *     Return: 0 on success or negative error
tools/include/uapi/linux/bpf.h +7 -4
index 94dfa9def355..e78aece03628 100644
@@ -313,8 +313,11 @@ union bpf_attr {
  *     @flags: room for future extensions
  *     Return: 0 on success or negative error
  *
- * u64 bpf_perf_event_read(&map, index)
- *     Return: Number events read or error code
+ * u64 bpf_perf_event_read(map, flags)
+ *     read perf event counter value
+ *     @map: pointer to perf_event_array map
+ *     @flags: index of event in the map or bitmask flags
+ *     Return: value of perf event counter read or error code
  *
  * int bpf_redirect(ifindex, flags)
  *     redirect to another netdev
@@ -328,11 +331,11 @@ union bpf_attr {
  *     @skb: pointer to skb
  *     Return: realm if != 0
  *
- * int bpf_perf_event_output(ctx, map, index, data, size)
+ * int bpf_perf_event_output(ctx, map, flags, data, size)
  *     output perf raw sample
  *     @ctx: struct pt_regs*
  *     @map: pointer to perf_event_array map
- *     @index: index of event in the map
+ *     @flags: index of event in the map or bitmask flags
  *     @data: data on stack to be output as raw data
  *     @size: size of data
  *     Return: 0 on success or negative error
Signature · 1 line
2.9.3