Fix safety checks for bpf_perf_event_read():
- only non-inherited events can be added to perf_event_array map
(do this check statically at map insertion time)
- dynamically check that event is local and !pmu->count
Otherwise buggy bpf program can cause kernel splat.
Also fix error path after perf_event_attrs()
and remove redundant 'extern'.
Fixes: 35578d798400 ("bpf: Implement function bpf_perf_event_read() that get the selected hardware PMU conuter")
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
v2->v3:
. refactor checks based on Wangnan's and Peter's feedback
while refactoring realized that these two issues need fixes as well:
. fix perf_event_attrs() error path
. remove redundant extern
v1->v2: fix compile in case of !CONFIG_PERF_EVENTS
Even in the worst case the crash is not possible.
Only warn_on_once, so imo net-next is ok.
include/linux/bpf.h | 1 -
kernel/bpf/arraymap.c | 25 ++++++++++++++++---------
kernel/trace/bpf_trace.c | 7 ++++++-
3 files changed, 22 insertions(+), 11 deletions(-)
Fix safety checks for bpf_perf_event_read():
- only non-inherited events can be added to perf_event_array map
(do this check statically at map insertion time)
- dynamically check that event is local and !pmu->count
Otherwise buggy bpf program can cause kernel splat.
Also fix error path after perf_event_attrs()
and remove redundant 'extern'.
Fixes: 35578d798400 ("bpf: Implement function bpf_perf_event_read() that get the selected hardware PMU conuter")
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
v2->v3:
. refactor checks based on Wangnan's and Peter's feedback
while refactoring realized that these two issues need fixes as well:
. fix perf_event_attrs() error path
. remove redundant extern
v1->v2: fix compile in case of !CONFIG_PERF_EVENTS
Even in the worst case the crash is not possible.
Only warn_on_once, so imo net-next is ok.
include/linux/bpf.h | 1 -
kernel/bpf/arraymap.c | 25 ++++++++++++++++---------
kernel/trace/bpf_trace.c | 7 ++++++-
3 files changed, 22 insertions(+), 11 deletions(-)
Since Peter suggest it is pointless for a system-wide perf_event
has inherit bit set [1], I think it should be safe to enable
system-wide perf_event pass this check?
here we don't know whether it's system wide or not, so the check
is needed.
The patch is the fix that should have been there from day one.
We must be safe first and relax later.
Fix safety checks for bpf_perf_event_read():
- only non-inherited events can be added to perf_event_array map
(do this check statically at map insertion time)
- dynamically check that event is local and !pmu->count
Otherwise buggy bpf program can cause kernel splat.
Also fix error path after perf_event_attrs()
and remove redundant 'extern'.
Fixes: 35578d798400 ("bpf: Implement function bpf_perf_event_read() that get the selected hardware PMU conuter")
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
So the bpf_perf_event_read() returns the count value, does this not also
mean that returning -EINVAL here is also 'wrong'?
I mean, sure an actual count value that high is unlikely, but its still
a broken interface.
So the bpf_perf_event_read() returns the count value, does this not also
mean that returning -EINVAL here is also 'wrong'?
I mean, sure an actual count value that high is unlikely, but its still
a broken interface.
Agree. that's not pretty interface. I wish I looked at it more carefully
when it was introduced. Now it's too late to change.
From: Peter Zijlstra <peterz@infradead.org> Date: 2015-10-23 14:54:12
On Fri, Oct 23, 2015 at 07:42:22AM -0700, Alexei Starovoitov wrote:
On 10/23/15 5:03 AM, Peter Zijlstra wrote:
quoted
So the bpf_perf_event_read() returns the count value, does this not also
mean that returning -EINVAL here is also 'wrong'?
I mean, sure an actual count value that high is unlikely, but its still
a broken interface.
Agree. that's not pretty interface. I wish I looked at it more carefully
when it was introduced. Now it's too late to change.
Right; and I figure changing the function signature is not done because
the eBPF stuff is ABI? Unfortunate indeed.
So the bpf_perf_event_read() returns the count value, does this not also
mean that returning -EINVAL here is also 'wrong'?
I mean, sure an actual count value that high is unlikely, but its still
a broken interface.
Agree. that's not pretty interface. I wish I looked at it more carefully
when it was introduced. Now it's too late to change.
So I really, really think eBPF needs to have an easy to use mechanism to phase out
old ABI components and introducing new (better) ones!
Then old crap can be de-emphasised and eventually removed, instead of having to
live with crap forever ...
Thanks,
Ingo
Then old crap can be de-emphasised and eventually removed, instead of having to
live with crap forever ...
strongly disagree. none of the helpers are 'crap'.
bpf_perf_event_read() muxes of -EINVAL into return value, but it's non
ambiguous to the program whether it got an error or real counter value.
So it's not pretty, but it's a reasonable trade off.
Properly written bpf programs will not be hitting the error path (which
is there for safety and protection against buggy programs) and will
consume return value without any extra checks.
bpf_perf_event_read() could have been done via passing a pointer to
stack where counter value can be stored, but that is much slower,
since program would need to init the stack and pass pointers while
helpers are not inlined, so the cost of return via stack is higher
than returning by value. In this case bpf_perf_event_read() can be hot,
so makes sense to optimize and sacrifice 'pretty' factor.
All existing helpers have use cases behind them and none overlap,
so not a single one can be 'deprecated'.
In general I don't think it's worth to make an exception in the kernel
that some interfaces are not ABI. That will give a bad impression on
the kernel overall. Either we have generic deprecation mechanism for
everything or none and my vote is for none.
On Sun, Oct 25, 2015 at 09:23:36AM -0700, Alexei Starovoitov wrote:
quoted
bpf_perf_event_read() muxes of -EINVAL into return value, but it's non
ambiguous to the program whether it got an error or real counter value.
How can that be, the (u64)-EINVAL value is a valid counter value..
unlikely maybe, but still quite possible.
In our real usecase we simply treat return value larger than
0x7fffffffffffffff
as error result. We can make it even larger, for example, to
0xffffffffffff0000.
Resuling values can be pre-processed by a script to filter potential
error result
out so it is not a very big problem for our real usecases.
For a better interface, I suggest
u64 bpf_perf_event_read(bool *perror);
which still returns counter value through its return value but put error
code
to stack. Then BPF program can pass NULL to the function if BPF problem
doesn't want to deal with error code.
Thank you.
On Sun, Oct 25, 2015 at 09:23:36AM -0700, Alexei Starovoitov wrote:
quoted
bpf_perf_event_read() muxes of -EINVAL into return value, but it's non
ambiguous to the program whether it got an error or real counter value.
How can that be, the (u64)-EINVAL value is a valid counter value..
unlikely maybe, but still quite possible.
In our real usecase we simply treat return value larger than
0x7fffffffffffffff
as error result. We can make it even larger, for example, to
0xffffffffffff0000.
either above or write the program that index is valid, then you
don't need to check for errors.
Resuling values can be pre-processed by a script to filter potential
error result
out so it is not a very big problem for our real usecases.
For a better interface, I suggest
u64 bpf_perf_event_read(bool *perror);
which still returns counter value through its return value but put error
code
to stack. Then BPF program can pass NULL to the function if BPF problem
doesn't want to deal with error code.
no. we're not going to introduce another interface for this.
The current one is fine. Don't pass incorrect index and you won't see
einval. Returning ints or bools via stack is much slower.
Fix safety checks for bpf_perf_event_read():
- only non-inherited events can be added to perf_event_array map
(do this check statically at map insertion time)
- dynamically check that event is local and !pmu->count
Otherwise buggy bpf program can cause kernel splat.
Also fix error path after perf_event_attrs()
and remove redundant 'extern'.
Fixes: 35578d798400 ("bpf: Implement function bpf_perf_event_read() that get the selected hardware PMU conuter")
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Applied, although my tendancy is to agree with the sentiment that you must
respect the entire universe of valid 64-bit counter values. I do not buy
the arguments about values overlapping error codes being unlikely or not
worth worrying about.
Just FYI...