xref: /linux/tools/testing/selftests/bpf/progs/trigger_bench.c (revision 9d56c248e5030d17ea9cd132634e86fdf0622d0e)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2020 Facebook
3 
4 #include <linux/bpf.h>
5 #include <asm/unistd.h>
6 #include <bpf/bpf_helpers.h>
7 #include <bpf/bpf_tracing.h>
8 #include "bpf_misc.h"
9 
10 char _license[] SEC("license") = "GPL";
11 
12 #define CPU_MASK 255
13 #define MAX_CPUS (CPU_MASK + 1) /* should match MAX_BUCKETS in benchs/bench_trigger.c */
14 
15 /* matches struct counter in bench.h */
16 struct counter {
17 	long value;
18 } __attribute__((aligned(128)));
19 
20 struct counter hits[MAX_CPUS];
21 
22 static __always_inline void inc_counter(void)
23 {
24 	int cpu = bpf_get_smp_processor_id();
25 
26 	__sync_add_and_fetch(&hits[cpu & CPU_MASK].value, 1);
27 }
28 
29 SEC("tp/syscalls/sys_enter_getpgid")
30 int bench_trigger_tp(void *ctx)
31 {
32 	inc_counter();
33 	return 0;
34 }
35 
36 SEC("raw_tp/sys_enter")
37 int BPF_PROG(bench_trigger_raw_tp, struct pt_regs *regs, long id)
38 {
39 	if (id == __NR_getpgid)
40 		inc_counter();
41 	return 0;
42 }
43 
44 SEC("kprobe/" SYS_PREFIX "sys_getpgid")
45 int bench_trigger_kprobe(void *ctx)
46 {
47 	inc_counter();
48 	return 0;
49 }
50 
51 SEC("kretprobe/" SYS_PREFIX "sys_getpgid")
52 int bench_trigger_kretprobe(void *ctx)
53 {
54 	inc_counter();
55 	return 0;
56 }
57 
58 SEC("kprobe.multi/" SYS_PREFIX "sys_getpgid")
59 int bench_trigger_kprobe_multi(void *ctx)
60 {
61 	inc_counter();
62 	return 0;
63 }
64 
65 SEC("kretprobe.multi/" SYS_PREFIX "sys_getpgid")
66 int bench_trigger_kretprobe_multi(void *ctx)
67 {
68 	inc_counter();
69 	return 0;
70 }
71 
72 SEC("fentry/" SYS_PREFIX "sys_getpgid")
73 int bench_trigger_fentry(void *ctx)
74 {
75 	inc_counter();
76 	return 0;
77 }
78 
79 SEC("fexit/" SYS_PREFIX "sys_getpgid")
80 int bench_trigger_fexit(void *ctx)
81 {
82 	inc_counter();
83 	return 0;
84 }
85 
86 SEC("fentry.s/" SYS_PREFIX "sys_getpgid")
87 int bench_trigger_fentry_sleep(void *ctx)
88 {
89 	inc_counter();
90 	return 0;
91 }
92 
93 SEC("fmod_ret/" SYS_PREFIX "sys_getpgid")
94 int bench_trigger_fmodret(void *ctx)
95 {
96 	inc_counter();
97 	return -22;
98 }
99 
100 SEC("uprobe")
101 int bench_trigger_uprobe(void *ctx)
102 {
103 	inc_counter();
104 	return 0;
105 }
106