History

bpf: Add bpf_send_signal_thread() helper

Commit 8b401f9ed244 ("bpf: implement bpf_send_signal() helper") added helper bpf_send_signal() which permits bpf program to send a signal to the current process. The signal may be delivered to any threads in the process. We found a use case where sending the signal to the current thread is more preferable. - A bpf program will collect the stack trace and then send signal to the user application. - The user application will add some thread specific information to the just collected stack trace for later analysis. If bpf_send_signal() is used, user application will need to check whether the thread receiving the signal matches the thread collecting the stack by checking thread id. If not, it will need to send signal to another thread through pthread_kill(). This patch proposed a new helper bpf_send_signal_thread(), which sends the signal to the thread corresponding to the current kernel task. This way, user space is guaranteed that bpf_program execution context and user space signal handling context are the same thread.

Yonghong Song committed 6 years ago

Commit 84829411 parent d3a56933 files · +58 −7Browse files

Showing the changes to include/uapi/linux (1 of 3 files). Show all

Unified · Split

include/uapi/linux/bpf.h+17−2
@@ -2714,7 +2714,8 @@ union bpf_attr { · 2713 unmodified lines
27142714 *
27152715 * int bpf_send_signal(u32 sig)
27162716 * Description
2717− * Send signal *sig* to the current task.
2717+ * Send signal *sig* to the process of the current task.
2718+ * The signal may be delivered to any of this process's threads.
27182719 * Return
27192720 * 0 on success or successfully queued.
27202721 *
@@ -2850,6 +2851,19 @@ union bpf_attr { · 129 unmodified lines
28502851 * Return
28512852 * 0 on success, or a negative error in case of failure.
28522853 *
2854+ * int bpf_send_signal_thread(u32 sig)
2855+ * Description
2856+ * Send signal *sig* to the thread corresponding to the current task.
2857+ * Return
2858+ * 0 on success or successfully queued.
2859+ *
2860+ * **-EBUSY** if work queue under nmi is full.
2861+ *
2862+ * **-EINVAL** if *sig* is invalid.
2863+ *
2864+ * **-EPERM** if no permission to send the *sig*.
2865+ *
2866+ * **-EAGAIN** if bpf program can try again.
28532867 */
28542868 #define __BPF_FUNC_MAPPER(FN) \
28552869 FN(unspec), \
@@ -2968,7 +2982,8 @@ union bpf_attr { · 112 unmodified lines
29682982 FN(probe_read_kernel), \
29692983 FN(probe_read_user_str), \
29702984 FN(probe_read_kernel_str), \
2971− FN(tcp_send_ack),
2985+ FN(tcp_send_ack), \
2986+ FN(send_signal_thread),
29722987
29732988 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
29742989 * function eBPF program intends to call