]> asedeno.scripts.mit.edu Git - linux.git/blob - tools/perf/examples/bpf/augmented_raw_syscalls.c
perf augmented_raw_syscalls: Move reading filename to the loop
[linux.git] / tools / perf / examples / bpf / augmented_raw_syscalls.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Augment the raw_syscalls tracepoints with the contents of the pointer arguments.
4  *
5  * Test it with:
6  *
7  * perf trace -e tools/perf/examples/bpf/augmented_raw_syscalls.c cat /etc/passwd > /dev/null
8  *
9  * This exactly matches what is marshalled into the raw_syscall:sys_enter
10  * payload expected by the 'perf trace' beautifiers.
11  *
12  * For now it just uses the existing tracepoint augmentation code in 'perf
13  * trace', in the next csets we'll hook up these with the sys_enter/sys_exit
14  * code that will combine entry/exit in a strace like way.
15  */
16
17 #include <unistd.h>
18 #include <linux/limits.h>
19 #include <pid_filter.h>
20
21 /* bpf-output associated map */
22 bpf_map(__augmented_syscalls__, PERF_EVENT_ARRAY, int, u32, __NR_CPUS__);
23
24 /*
25  * string_args_len: one per syscall arg, 0 means not a string or don't copy it,
26  *                  PATH_MAX for copying everything, any other value to limit
27  *                  it a la 'strace -s strsize'.
28  */
29 struct syscall {
30         bool    enabled;
31         u16     string_args_len[6];
32 };
33
34 bpf_map(syscalls, ARRAY, int, struct syscall, 512);
35
36 struct syscall_enter_args {
37         unsigned long long common_tp_fields;
38         long               syscall_nr;
39         unsigned long      args[6];
40 };
41
42 struct syscall_exit_args {
43         unsigned long long common_tp_fields;
44         long               syscall_nr;
45         long               ret;
46 };
47
48 struct augmented_filename {
49         unsigned int    size;
50         int             err;
51         char            value[PATH_MAX];
52 };
53
54 pid_filter(pids_filtered);
55
56 struct augmented_args_filename {
57        struct syscall_enter_args args;
58        struct augmented_filename filename;
59 };
60
61 bpf_map(augmented_filename_map, PERCPU_ARRAY, int, struct augmented_args_filename, 1);
62
63 static inline
64 unsigned int augmented_filename__read(struct augmented_filename *augmented_filename,
65                                       const void *filename_arg, unsigned int filename_len)
66 {
67         unsigned int len = sizeof(*augmented_filename);
68         int size = probe_read_str(&augmented_filename->value, filename_len, filename_arg);
69
70         augmented_filename->size = augmented_filename->err = 0;
71         /*
72          * probe_read_str may return < 0, e.g. -EFAULT
73          * So we leave that in the augmented_filename->size that userspace will
74          */
75         if (size > 0) {
76                 len -= sizeof(augmented_filename->value) - size;
77                 len &= sizeof(augmented_filename->value) - 1;
78                 augmented_filename->size = size;
79         } else {
80                 /*
81                  * So that username notice the error while still being able
82                  * to skip this augmented arg record
83                  */
84                 augmented_filename->err = size;
85                 len = offsetof(struct augmented_filename, value);
86         }
87
88         return len;
89 }
90
91 SEC("raw_syscalls:sys_enter")
92 int sys_enter(struct syscall_enter_args *args)
93 {
94         struct augmented_args_filename *augmented_args;
95         /*
96          * We start len, the amount of data that will be in the perf ring
97          * buffer, if this is not filtered out by one of pid_filter__has(),
98          * syscall->enabled, etc, with the non-augmented raw syscall payload,
99          * i.e. sizeof(augmented_args->args).
100          *
101          * We'll add to this as we add augmented syscalls right after that
102          * initial, non-augmented raw_syscalls:sys_enter payload.
103          */
104         unsigned int len = sizeof(augmented_args->args);
105         struct syscall *syscall;
106         int key = 0;
107
108         augmented_args = bpf_map_lookup_elem(&augmented_filename_map, &key);
109         if (augmented_args == NULL)
110                 return 1;
111
112         if (pid_filter__has(&pids_filtered, getpid()))
113                 return 0;
114
115         probe_read(&augmented_args->args, sizeof(augmented_args->args), args);
116
117         syscall = bpf_map_lookup_elem(&syscalls, &augmented_args->args.syscall_nr);
118         if (syscall == NULL || !syscall->enabled)
119                 return 0;
120         /*
121          * Yonghong and Edward Cree sayz:
122          *
123          * https://www.spinics.net/lists/netdev/msg531645.html
124          *
125          * >>   R0=inv(id=0) R1=inv2 R6=ctx(id=0,off=0,imm=0) R7=inv64 R10=fp0,call_-1
126          * >> 10: (bf) r1 = r6
127          * >> 11: (07) r1 += 16
128          * >> 12: (05) goto pc+2
129          * >> 15: (79) r3 = *(u64 *)(r1 +0)
130          * >> dereference of modified ctx ptr R1 off=16 disallowed
131          * > Aha, we at least got a different error message this time.
132          * > And indeed llvm has done that optimisation, rather than the more obvious
133          * > 11: r3 = *(u64 *)(r1 +16)
134          * > because it wants to have lots of reads share a single insn.  You may be able
135          * > to defeat that optimisation by adding compiler barriers, idk.  Maybe someone
136          * > with llvm knowledge can figure out how to stop it (ideally, llvm would know
137          * > when it's generating for bpf backend and not do that).  -O0?  ¯\_(ツ)_/¯
138          *
139          * The optimization mostly likes below:
140          *
141          *      br1:
142          *      ...
143          *      r1 += 16
144          *      goto merge
145          *      br2:
146          *      ...
147          *      r1 += 20
148          *      goto merge
149          *      merge:
150          *      *(u64 *)(r1 + 0)
151          *
152          * The compiler tries to merge common loads. There is no easy way to
153          * stop this compiler optimization without turning off a lot of other
154          * optimizations. The easiest way is to add barriers:
155          *
156          *       __asm__ __volatile__("": : :"memory")
157          *
158          *       after the ctx memory access to prevent their down stream merging.
159          */
160         /*
161          * For now copy just the first string arg, we need to improve the protocol
162          * and have more than one.
163          *
164          * Using the unrolled loop is not working, only when we do it manually,
165          * check this out later...
166
167         u8 arg;
168 #pragma clang loop unroll(full)
169         for (arg = 0; arg < 6; ++arg) {
170                 if (syscall->string_args_len[arg] != 0) {
171                         filename_len = syscall->string_args_len[arg];
172                         filename_arg = (const void *)args->args[arg];
173                         __asm__ __volatile__("": : :"memory");
174                         break;
175                 }
176         }
177
178         verifier log:
179
180 ; if (syscall->string_args_len[arg] != 0) {
181 37: (69) r3 = *(u16 *)(r0 +2)
182  R0=map_value(id=0,off=0,ks=4,vs=14,imm=0) R1_w=inv0 R2_w=map_value(id=0,off=2,ks=4,vs=14,imm=0) R6=ctx(id=0,off=0,imm=0) R7=map_value(id=0,off=0,ks=4,vs=4168,imm=0) R10=fp0,call_-1 fp-8=mmmmmmmm
183 ; if (syscall->string_args_len[arg] != 0) {
184 38: (55) if r3 != 0x0 goto pc+5
185  R0=map_value(id=0,off=0,ks=4,vs=14,imm=0) R1=inv0 R2=map_value(id=0,off=2,ks=4,vs=14,imm=0) R3=inv0 R6=ctx(id=0,off=0,imm=0) R7=map_value(id=0,off=0,ks=4,vs=4168,imm=0) R10=fp0,call_-1 fp-8=mmmmmmmm
186 39: (b7) r1 = 1
187 ; if (syscall->string_args_len[arg] != 0) {
188 40: (bf) r2 = r0
189 41: (07) r2 += 4
190 42: (69) r3 = *(u16 *)(r0 +4)
191  R0=map_value(id=0,off=0,ks=4,vs=14,imm=0) R1_w=inv1 R2_w=map_value(id=0,off=4,ks=4,vs=14,imm=0) R3_w=inv0 R6=ctx(id=0,off=0,imm=0) R7=map_value(id=0,off=0,ks=4,vs=4168,imm=0) R10=fp0,call_-1 fp-8=mmmmmmmm
192 ; if (syscall->string_args_len[arg] != 0) {
193 43: (15) if r3 == 0x0 goto pc+32
194  R0=map_value(id=0,off=0,ks=4,vs=14,imm=0) R1=inv1 R2=map_value(id=0,off=4,ks=4,vs=14,imm=0) R3=inv(id=0,umax_value=65535,var_off=(0x0; 0xffff)) R6=ctx(id=0,off=0,imm=0) R7=map_value(id=0,off=0,ks=4,vs=4168,imm=0) R10=fp0,call_-1 fp-8=mmmmmmmm
195 ; filename_arg = (const void *)args->args[arg];
196 44: (67) r1 <<= 3
197 45: (bf) r3 = r6
198 46: (0f) r3 += r1
199 47: (b7) r5 = 64
200 48: (79) r3 = *(u64 *)(r3 +16)
201 dereference of modified ctx ptr R3 off=8 disallowed
202 processed 46 insns (limit 1000000) max_states_per_insn 0 total_states 12 peak_states 12 mark_read 7
203         */
204
205 #define __loop_iter(arg) \
206         if (syscall->string_args_len[arg] != 0) { \
207                 unsigned int filename_len = syscall->string_args_len[arg]; \
208                 const void *filename_arg = (const void *)args->args[arg]; \
209                 if (filename_len <= sizeof(augmented_args->filename.value)) \
210                         len += augmented_filename__read(&augmented_args->filename, filename_arg, filename_len);
211 #define loop_iter_first() __loop_iter(0); }
212 #define loop_iter(arg) else __loop_iter(arg); }
213 #define loop_iter_last(arg) else __loop_iter(arg); __asm__ __volatile__("": : :"memory"); }
214
215         loop_iter_first()
216         loop_iter(1)
217         loop_iter(2)
218         loop_iter(3)
219         loop_iter(4)
220         loop_iter_last(5)
221
222         /* If perf_event_output fails, return non-zero so that it gets recorded unaugmented */
223         return perf_event_output(args, &__augmented_syscalls__, BPF_F_CURRENT_CPU, augmented_args, len);
224 }
225
226 SEC("raw_syscalls:sys_exit")
227 int sys_exit(struct syscall_exit_args *args)
228 {
229         struct syscall_exit_args exit_args;
230         struct syscall *syscall;
231
232         if (pid_filter__has(&pids_filtered, getpid()))
233                 return 0;
234
235         probe_read(&exit_args, sizeof(exit_args), args);
236
237         syscall = bpf_map_lookup_elem(&syscalls, &exit_args.syscall_nr);
238         if (syscall == NULL || !syscall->enabled)
239                 return 0;
240
241         return 1;
242 }
243
244 license(GPL);