bpf: implement bpf_send_signal() helper
Subsystems (8)
v4 cover bpf: implement bpf_send_signal() helper
This patch tries to solve the following specific use case.
Currently, bpf program can already collect stack traces through kernel function get_perf_callchain()
when certain events happens (e.g., cache miss counter or cpu clock counter overflows). But such stack traces are not enough for jitted programs, e.g., hhvm (jited php). To get real stack trace, jit engine internal data structures need to be traversed in order to get the real user functions.
bpf program itself may not be the best place to traverse the jit engine as the traversing logic could be complex and it is not a stable interface either.
Instead, hhvm implements a signal handler,
e.g. for SIGALARM, and a set of program locations which it can dump stack traces. When it receives a signal, it will dump the stack in next such program location.
This patch implements bpf_send_signal() helper to send a signal to hhvm in real time, resulting in intended stack traces.
Patch #1 implemented the bpf_send_helper() in the kernel. Patch #2 synced uapi header bpf.h to tools directory. Patch #3 added a self test which covers tracepoint and perf_event bpf programs.
Changelogs:
v3 => v4:
. fix one typo and declare "const char *id_path = ..."
to avoid directly use the long string in the func body in Patch #3.
v2 => v3:
. change the standalone test to be part of prog_tests.
RFC v1 => v2:
. previous version allows to send signal to an arbitrary
pid. This version just sends the signal to current task to avoid unstable pid and potential races between sending signals and task state changes for the pid.
Yonghong Song (3):
bpf: implement bpf_send_signal() helper
tools/bpf: sync bpf uapi header bpf.h to tools directory tools/bpf: add selftest in test_progs for bpf_send_signal() helper
include/uapi/linux/bpf.h | 17 +- kernel/trace/bpf_trace.c | 67 ++++++ tools/include/uapi/linux/bpf.h | 17 +- tools/testing/selftests/bpf/bpf_helpers.h | 1 + .../selftests/bpf/prog_tests/send_signal.c | 192 ++++++++++++++++++ .../bpf/progs/test_send_signal_kern.c | 51 +++++ 6 files changed, 343 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/send_signal.c create mode 100644 tools/testing/selftests/bpf/progs/test_send_signal_kern.c
Signature · 1 line
2.17.1
PATCH v4 1/3 bpf: implement bpf_send_signal() helper
This patch tries to solve the following specific use case.
Currently, bpf program can already collect stack traces through kernel function get_perf_callchain()
when certain events happens (e.g., cache miss counter or cpu clock counter overflows). But such stack traces are not enough for jitted programs, e.g., hhvm (jited php). To get real stack trace, jit engine internal data structures need to be traversed in order to get the real user functions.
bpf program itself may not be the best place to traverse the jit engine as the traversing logic could be complex and it is not a stable interface either.
Instead, hhvm implements a signal handler,
e.g. for SIGALARM, and a set of program locations which it can dump stack traces. When it receives a signal, it will dump the stack in next such program location.
Such a mechanism can be implemented in the following way:
. a perf ring buffer is created between bpf program
and tracing app.
. once a particular event happens, bpf program writes
to the ring buffer and the tracing app gets notified.
. the tracing app sends a signal SIGALARM to the hhvm.
But this method could have large delays and causing profiling results skewed.
This patch implements bpf_send_signal() helper to send a signal to hhvm in real time, resulting in intended stack traces.
--- include/uapi/linux/bpf.h | 17 +++++++++- kernel/trace/bpf_trace.c | 67 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-)
include/uapi/linux/bpf.h +16 -1
@@ -2672,6 +2672,20 @@ union bpf_attr {* 0 on success. * * **-ENOENT** if the bpf-local-storage cannot be found. +* +* int bpf_send_signal(u32 sig) +* Description +* Send signal *sig* to the current task. +* Return +* 0 on success or successfully queued. +* +* **-EBUSY** if work queue under nmi is full. +* +* **-EINVAL** if *sig* is invalid. +* +* **-EPERM** if no permission to send the *sig*. +* +* **-EAGAIN** if bpf program can try again. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -2782,7 +2796,8 @@ union bpf_attr {FN (strtol), \FN (strtoul), \FN (sk_storage_get), \ -FN (sk_storage_delete), +FN (sk_storage_delete), \ +FN (send_signal),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
kernel/trace/bpf_trace.c +67 -0
84 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v4 2/3 tools/bpf: sync bpf uapi header bpf.h to tools directory
In reply to Yonghong SongThe bpf uapi header include/uapi/linux/bpf.h is sync'ed to tools/include/uapi/linux/bpf.h.
--- tools/include/uapi/linux/bpf.h | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)
tools/include/uapi/linux/bpf.h +16 -1
@@ -2672,6 +2672,20 @@ union bpf_attr {* 0 on success. * * **-ENOENT** if the bpf-local-storage cannot be found. +* +* int bpf_send_signal(u32 sig) +* Description +* Send signal *sig* to the current task. +* Return +* 0 on success or successfully queued. +* +* **-EBUSY** if work queue under nmi is full. +* +* **-EINVAL** if *sig* is invalid. +* +* **-EPERM** if no permission to send the *sig*. +* +* **-EAGAIN** if bpf program can try again. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -2782,7 +2796,8 @@ union bpf_attr {FN (strtol), \FN (strtoul), \FN (sk_storage_get), \ -FN (sk_storage_delete), +FN (sk_storage_delete), \ +FN (send_signal),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
Signature · 1 line
2.17.1
PATCH v4 3/3 tools/bpf: add selftest in test_progs for bpf_send_signal() helper
In reply to Yonghong SongThe test covered both nmi and tracepoint perf events.
$ ./test_progs ... test_send_signal_tracepoint:PASS:tracepoint 0 nsec ... test_send_signal_common:PASS:tracepoint 0 nsec ... test_send_signal_common:PASS:perf_event 0 nsec ... test_send_signal:OK
--- tools/testing/selftests/bpf/bpf_helpers.h | 1 + .../selftests/bpf/prog_tests/send_signal.c | 192 ++++++++++++++++++ .../bpf/progs/test_send_signal_kern.c | 51 +++++ 3 files changed, 244 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/send_signal.c create mode 100644 tools/testing/selftests/bpf/progs/test_send_signal_kern.c
tools/testing/selftests/bpf/bpf_helpers.h +1 -0
@@ -216,6 +216,7 @@ static void *(*bpf_sk_storage_get)(void *map, struct bpf_sock *sk, (void *) BPF_FUNC_sk_storage_get;static int (*bpf_sk_storage_delete)(void *map,struct bpf_sock *sk) = (void *)BPF_FUNC_sk_storage_delete; +static int (*bpf_send_signal)(unsigned sig) = (void *)BPF_FUNC_send_signal;/* llvm builtin functions that eBPF C program may use to * emit BPF_LD_ABS and BPF_LD_IND instructions
tools/testing/selftests/bpf/prog_tests/send_signal.c new file +192 -0
192 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_send_signal_kern.c new file +51 -0
51 lines not on this page · raw message
Signature · 1 line
2.17.1