xref: /linux/tools/testing/selftests/bpf/progs/tramp_prog_detach.c (revision d5288c519bbd0df9dff9f8dce2a8ce24e2ca9c8a)
1*d5288c51SFlorent Revest (Anthropic) // SPDX-License-Identifier: GPL-2.0
2*d5288c51SFlorent Revest (Anthropic) #include "vmlinux.h"
3*d5288c51SFlorent Revest (Anthropic) #include <bpf/bpf_helpers.h>
4*d5288c51SFlorent Revest (Anthropic) #include <bpf/bpf_tracing.h>
5*d5288c51SFlorent Revest (Anthropic) 
6*d5288c51SFlorent Revest (Anthropic) char _license[] SEC("license") = "GPL";
7*d5288c51SFlorent Revest (Anthropic) 
8*d5288c51SFlorent Revest (Anthropic) int pid;
9*d5288c51SFlorent Revest (Anthropic) void *fault_addr;
10*d5288c51SFlorent Revest (Anthropic) __u64 ts;
11*d5288c51SFlorent Revest (Anthropic) 
12*d5288c51SFlorent Revest (Anthropic) static int do_sleepable(void)
13*d5288c51SFlorent Revest (Anthropic) {
14*d5288c51SFlorent Revest (Anthropic) 	char dst;
15*d5288c51SFlorent Revest (Anthropic) 
16*d5288c51SFlorent Revest (Anthropic) 	if (bpf_get_current_pid_tgid() >> 32 != pid)
17*d5288c51SFlorent Revest (Anthropic) 		return 0;
18*d5288c51SFlorent Revest (Anthropic) 
19*d5288c51SFlorent Revest (Anthropic) 	ts = bpf_ktime_get_ns();
20*d5288c51SFlorent Revest (Anthropic) 	/* blocks for as long as user space wants when fault_addr is armed */
21*d5288c51SFlorent Revest (Anthropic) 	bpf_copy_from_user(&dst, sizeof(dst), fault_addr);
22*d5288c51SFlorent Revest (Anthropic) 	return 0;
23*d5288c51SFlorent Revest (Anthropic) }
24*d5288c51SFlorent Revest (Anthropic) 
25*d5288c51SFlorent Revest (Anthropic) static int do_victim(void)
26*d5288c51SFlorent Revest (Anthropic) {
27*d5288c51SFlorent Revest (Anthropic) 	if (bpf_get_current_pid_tgid() >> 32 != pid)
28*d5288c51SFlorent Revest (Anthropic) 		return 0;
29*d5288c51SFlorent Revest (Anthropic) 
30*d5288c51SFlorent Revest (Anthropic) 	ts = bpf_ktime_get_ns();
31*d5288c51SFlorent Revest (Anthropic) 	return 0;
32*d5288c51SFlorent Revest (Anthropic) }
33*d5288c51SFlorent Revest (Anthropic) 
34*d5288c51SFlorent Revest (Anthropic) SEC("?fentry.s/bpf_fentry_test1")
35*d5288c51SFlorent Revest (Anthropic) int BPF_PROG(fentry_sleepable, int a)
36*d5288c51SFlorent Revest (Anthropic) {
37*d5288c51SFlorent Revest (Anthropic) 	return do_sleepable();
38*d5288c51SFlorent Revest (Anthropic) }
39*d5288c51SFlorent Revest (Anthropic) 
40*d5288c51SFlorent Revest (Anthropic) SEC("?fentry/bpf_fentry_test1")
41*d5288c51SFlorent Revest (Anthropic) int BPF_PROG(fentry_victim, int a)
42*d5288c51SFlorent Revest (Anthropic) {
43*d5288c51SFlorent Revest (Anthropic) 	return do_victim();
44*d5288c51SFlorent Revest (Anthropic) }
45*d5288c51SFlorent Revest (Anthropic) 
46*d5288c51SFlorent Revest (Anthropic) SEC("?fexit.s/bpf_fentry_test1")
47*d5288c51SFlorent Revest (Anthropic) int BPF_PROG(fexit_sleepable, int a, int ret)
48*d5288c51SFlorent Revest (Anthropic) {
49*d5288c51SFlorent Revest (Anthropic) 	return do_sleepable();
50*d5288c51SFlorent Revest (Anthropic) }
51*d5288c51SFlorent Revest (Anthropic) 
52*d5288c51SFlorent Revest (Anthropic) SEC("?fexit/bpf_fentry_test1")
53*d5288c51SFlorent Revest (Anthropic) int BPF_PROG(fexit_victim, int a, int ret)
54*d5288c51SFlorent Revest (Anthropic) {
55*d5288c51SFlorent Revest (Anthropic) 	return do_victim();
56*d5288c51SFlorent Revest (Anthropic) }
57