Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
47788c58 | 2 | #include <trace/syscall.h> |
1c569f02 | 3 | #include <trace/events/syscalls.h> |
f431b634 | 4 | #include <linux/syscalls.h> |
5a0e3ad6 | 5 | #include <linux/slab.h> |
ee08c6ec | 6 | #include <linux/kernel.h> |
56d82e00 | 7 | #include <linux/module.h> /* for MODULE_NAME_LEN via KSYM_SYMBOL_LEN */ |
fb34a08c | 8 | #include <linux/ftrace.h> |
cdd6c482 | 9 | #include <linux/perf_event.h> |
0e242208 | 10 | #include <linux/xarray.h> |
ee08c6ec FW |
11 | #include <asm/syscall.h> |
12 | ||
13 | #include "trace_output.h" | |
14 | #include "trace.h" | |
15 | ||
5be71b61 | 16 | static DEFINE_MUTEX(syscall_trace_lock); |
ee08c6ec | 17 | |
2425bcb9 | 18 | static int syscall_enter_register(struct trace_event_call *event, |
ceec0b6f | 19 | enum trace_reg type, void *data); |
2425bcb9 | 20 | static int syscall_exit_register(struct trace_event_call *event, |
ceec0b6f | 21 | enum trace_reg type, void *data); |
2239291a | 22 | |
2e33af02 | 23 | static struct list_head * |
2425bcb9 | 24 | syscall_get_enter_fields(struct trace_event_call *call) |
2e33af02 SR |
25 | { |
26 | struct syscall_metadata *entry = call->data; | |
27 | ||
28 | return &entry->enter_fields; | |
29 | } | |
30 | ||
3d56e331 SR |
31 | extern struct syscall_metadata *__start_syscalls_metadata[]; |
32 | extern struct syscall_metadata *__stop_syscalls_metadata[]; | |
c44fc770 | 33 | |
0e242208 | 34 | static DEFINE_XARRAY(syscalls_metadata_sparse); |
c44fc770 FW |
35 | static struct syscall_metadata **syscalls_metadata; |
36 | ||
b2d55496 IM |
37 | #ifndef ARCH_HAS_SYSCALL_MATCH_SYM_NAME |
38 | static inline bool arch_syscall_match_sym_name(const char *sym, const char *name) | |
39 | { | |
40 | /* | |
41 | * Only compare after the "sys" prefix. Archs that use | |
42 | * syscall wrappers may have syscalls symbols aliases prefixed | |
36a78e9e | 43 | * with ".SyS" or ".sys" instead of "sys", leading to an unwanted |
b2d55496 IM |
44 | * mismatch. |
45 | */ | |
46 | return !strcmp(sym + 3, name + 3); | |
47 | } | |
48 | #endif | |
49 | ||
f431b634 SR |
50 | #ifdef ARCH_TRACE_IGNORE_COMPAT_SYSCALLS |
51 | /* | |
52 | * Some architectures that allow for 32bit applications | |
53 | * to run on a 64bit kernel, do not map the syscalls for | |
54 | * the 32bit tasks the same as they do for 64bit tasks. | |
55 | * | |
56 | * *cough*x86*cough* | |
57 | * | |
58 | * In such a case, instead of reporting the wrong syscalls, | |
59 | * simply ignore them. | |
60 | * | |
61 | * For an arch to ignore the compat syscalls it needs to | |
62 | * define ARCH_TRACE_IGNORE_COMPAT_SYSCALLS as well as | |
63 | * define the function arch_trace_is_compat_syscall() to let | |
64 | * the tracing system know that it should ignore it. | |
65 | */ | |
66 | static int | |
67 | trace_get_syscall_nr(struct task_struct *task, struct pt_regs *regs) | |
68 | { | |
69 | if (unlikely(arch_trace_is_compat_syscall(regs))) | |
70 | return -1; | |
71 | ||
72 | return syscall_get_nr(task, regs); | |
73 | } | |
74 | #else | |
75 | static inline int | |
76 | trace_get_syscall_nr(struct task_struct *task, struct pt_regs *regs) | |
77 | { | |
78 | return syscall_get_nr(task, regs); | |
79 | } | |
80 | #endif /* ARCH_TRACE_IGNORE_COMPAT_SYSCALLS */ | |
81 | ||
3d56e331 SR |
82 | static __init struct syscall_metadata * |
83 | find_syscall_meta(unsigned long syscall) | |
c44fc770 | 84 | { |
3d56e331 SR |
85 | struct syscall_metadata **start; |
86 | struct syscall_metadata **stop; | |
c44fc770 FW |
87 | char str[KSYM_SYMBOL_LEN]; |
88 | ||
89 | ||
3d56e331 SR |
90 | start = __start_syscalls_metadata; |
91 | stop = __stop_syscalls_metadata; | |
c44fc770 FW |
92 | kallsyms_lookup(syscall, NULL, NULL, NULL, str); |
93 | ||
ae07f551 IM |
94 | if (arch_syscall_match_sym_name(str, "sys_ni_syscall")) |
95 | return NULL; | |
96 | ||
c44fc770 | 97 | for ( ; start < stop; start++) { |
b2d55496 | 98 | if ((*start)->name && arch_syscall_match_sym_name(str, (*start)->name)) |
3d56e331 | 99 | return *start; |
c44fc770 FW |
100 | } |
101 | return NULL; | |
102 | } | |
103 | ||
104 | static struct syscall_metadata *syscall_nr_to_meta(int nr) | |
105 | { | |
0e242208 HN |
106 | if (IS_ENABLED(CONFIG_HAVE_SPARSE_SYSCALL_NR)) |
107 | return xa_load(&syscalls_metadata_sparse, (unsigned long)nr); | |
108 | ||
c44fc770 FW |
109 | if (!syscalls_metadata || nr >= NR_syscalls || nr < 0) |
110 | return NULL; | |
111 | ||
112 | return syscalls_metadata[nr]; | |
113 | } | |
114 | ||
dbfeaa7a TZ |
115 | const char *get_syscall_name(int syscall) |
116 | { | |
117 | struct syscall_metadata *entry; | |
118 | ||
119 | entry = syscall_nr_to_meta(syscall); | |
120 | if (!entry) | |
121 | return NULL; | |
122 | ||
123 | return entry->name; | |
124 | } | |
125 | ||
6aea49cb | 126 | static enum print_line_t |
a9a57763 SR |
127 | print_syscall_enter(struct trace_iterator *iter, int flags, |
128 | struct trace_event *event) | |
bed1ffca | 129 | { |
983f938a | 130 | struct trace_array *tr = iter->tr; |
bed1ffca FW |
131 | struct trace_seq *s = &iter->seq; |
132 | struct trace_entry *ent = iter->ent; | |
133 | struct syscall_trace_enter *trace; | |
134 | struct syscall_metadata *entry; | |
183742f0 | 135 | int i, syscall; |
bed1ffca | 136 | |
64c12e04 | 137 | trace = (typeof(trace))ent; |
bed1ffca | 138 | syscall = trace->nr; |
bed1ffca | 139 | entry = syscall_nr_to_meta(syscall); |
64c12e04 | 140 | |
bed1ffca FW |
141 | if (!entry) |
142 | goto end; | |
143 | ||
32c0edae | 144 | if (entry->enter_event->event.type != ent->type) { |
64c12e04 JB |
145 | WARN_ON_ONCE(1); |
146 | goto end; | |
147 | } | |
148 | ||
183742f0 | 149 | trace_seq_printf(s, "%s(", entry->name); |
bed1ffca FW |
150 | |
151 | for (i = 0; i < entry->nb_args; i++) { | |
183742f0 SRRH |
152 | |
153 | if (trace_seq_has_overflowed(s)) | |
154 | goto end; | |
155 | ||
bed1ffca | 156 | /* parameter types */ |
cb1c45fb | 157 | if (tr && tr->trace_flags & TRACE_ITER_VERBOSE) |
183742f0 SRRH |
158 | trace_seq_printf(s, "%s ", entry->types[i]); |
159 | ||
bed1ffca | 160 | /* parameter values */ |
183742f0 SRRH |
161 | trace_seq_printf(s, "%s: %lx%s", entry->args[i], |
162 | trace->args[i], | |
163 | i == entry->nb_args - 1 ? "" : ", "); | |
bed1ffca FW |
164 | } |
165 | ||
183742f0 | 166 | trace_seq_putc(s, ')'); |
bed1ffca | 167 | end: |
183742f0 | 168 | trace_seq_putc(s, '\n'); |
4539f077 | 169 | |
183742f0 | 170 | return trace_handle_return(s); |
bed1ffca FW |
171 | } |
172 | ||
6aea49cb | 173 | static enum print_line_t |
a9a57763 SR |
174 | print_syscall_exit(struct trace_iterator *iter, int flags, |
175 | struct trace_event *event) | |
bed1ffca FW |
176 | { |
177 | struct trace_seq *s = &iter->seq; | |
178 | struct trace_entry *ent = iter->ent; | |
179 | struct syscall_trace_exit *trace; | |
180 | int syscall; | |
181 | struct syscall_metadata *entry; | |
bed1ffca | 182 | |
64c12e04 | 183 | trace = (typeof(trace))ent; |
bed1ffca | 184 | syscall = trace->nr; |
bed1ffca | 185 | entry = syscall_nr_to_meta(syscall); |
64c12e04 | 186 | |
bed1ffca | 187 | if (!entry) { |
146c3442 | 188 | trace_seq_putc(s, '\n'); |
183742f0 | 189 | goto out; |
bed1ffca FW |
190 | } |
191 | ||
32c0edae | 192 | if (entry->exit_event->event.type != ent->type) { |
64c12e04 JB |
193 | WARN_ON_ONCE(1); |
194 | return TRACE_TYPE_UNHANDLED; | |
195 | } | |
196 | ||
183742f0 | 197 | trace_seq_printf(s, "%s -> 0x%lx\n", entry->name, |
bed1ffca | 198 | trace->ret); |
bed1ffca | 199 | |
183742f0 SRRH |
200 | out: |
201 | return trace_handle_return(s); | |
bed1ffca FW |
202 | } |
203 | ||
04ae87a5 PZ |
204 | #define SYSCALL_FIELD(_type, _name) { \ |
205 | .type = #_type, .name = #_name, \ | |
206 | .size = sizeof(_type), .align = __alignof__(_type), \ | |
207 | .is_signed = is_signed_type(_type), .filter_type = FILTER_OTHER } | |
e6971969 | 208 | |
3ddc77f6 LZ |
209 | static int __init |
210 | __set_enter_print_fmt(struct syscall_metadata *entry, char *buf, int len) | |
50307a45 LJ |
211 | { |
212 | int i; | |
213 | int pos = 0; | |
214 | ||
215 | /* When len=0, we just calculate the needed length */ | |
216 | #define LEN_OR_ZERO (len ? len - pos : 0) | |
217 | ||
218 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\""); | |
219 | for (i = 0; i < entry->nb_args; i++) { | |
220 | pos += snprintf(buf + pos, LEN_OR_ZERO, "%s: 0x%%0%zulx%s", | |
221 | entry->args[i], sizeof(unsigned long), | |
222 | i == entry->nb_args - 1 ? "" : ", "); | |
223 | } | |
224 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\""); | |
225 | ||
226 | for (i = 0; i < entry->nb_args; i++) { | |
227 | pos += snprintf(buf + pos, LEN_OR_ZERO, | |
228 | ", ((unsigned long)(REC->%s))", entry->args[i]); | |
229 | } | |
230 | ||
231 | #undef LEN_OR_ZERO | |
232 | ||
233 | /* return the length of print_fmt */ | |
234 | return pos; | |
235 | } | |
236 | ||
2425bcb9 | 237 | static int __init set_syscall_print_fmt(struct trace_event_call *call) |
50307a45 LJ |
238 | { |
239 | char *print_fmt; | |
240 | int len; | |
241 | struct syscall_metadata *entry = call->data; | |
242 | ||
243 | if (entry->enter_event != call) { | |
244 | call->print_fmt = "\"0x%lx\", REC->ret"; | |
245 | return 0; | |
246 | } | |
247 | ||
248 | /* First: called with 0 length to calculate the needed length */ | |
249 | len = __set_enter_print_fmt(entry, NULL, 0); | |
250 | ||
251 | print_fmt = kmalloc(len + 1, GFP_KERNEL); | |
252 | if (!print_fmt) | |
253 | return -ENOMEM; | |
254 | ||
255 | /* Second: actually write the @print_fmt */ | |
256 | __set_enter_print_fmt(entry, print_fmt, len + 1); | |
257 | call->print_fmt = print_fmt; | |
258 | ||
259 | return 0; | |
260 | } | |
261 | ||
2425bcb9 | 262 | static void __init free_syscall_print_fmt(struct trace_event_call *call) |
50307a45 LJ |
263 | { |
264 | struct syscall_metadata *entry = call->data; | |
265 | ||
266 | if (entry->enter_event == call) | |
267 | kfree(call->print_fmt); | |
268 | } | |
269 | ||
2425bcb9 | 270 | static int __init syscall_enter_define_fields(struct trace_event_call *call) |
540b7b8d LZ |
271 | { |
272 | struct syscall_trace_enter trace; | |
31c16b13 | 273 | struct syscall_metadata *meta = call->data; |
540b7b8d | 274 | int offset = offsetof(typeof(trace), args); |
31537cf8 SRV |
275 | int ret = 0; |
276 | int i; | |
0f1ef51d | 277 | |
540b7b8d | 278 | for (i = 0; i < meta->nb_args; i++) { |
aeaeae11 FW |
279 | ret = trace_define_field(call, meta->types[i], |
280 | meta->args[i], offset, | |
43b51ead LZ |
281 | sizeof(unsigned long), 0, |
282 | FILTER_OTHER); | |
04ae87a5 PZ |
283 | if (ret) |
284 | break; | |
540b7b8d LZ |
285 | offset += sizeof(unsigned long); |
286 | } | |
287 | ||
288 | return ret; | |
289 | } | |
290 | ||
12ab74ee | 291 | static void ftrace_syscall_enter(void *data, struct pt_regs *regs, long id) |
ee08c6ec | 292 | { |
12ab74ee | 293 | struct trace_array *tr = data; |
7f1d2f82 | 294 | struct trace_event_file *trace_file; |
bed1ffca FW |
295 | struct syscall_trace_enter *entry; |
296 | struct syscall_metadata *sys_data; | |
cb1c45fb | 297 | struct trace_event_buffer fbuffer; |
d08e4113 | 298 | unsigned long args[6]; |
ee08c6ec | 299 | int syscall_nr; |
f431b634 | 300 | int size; |
ee08c6ec | 301 | |
f431b634 | 302 | syscall_nr = trace_get_syscall_nr(current, regs); |
086ba77a | 303 | if (syscall_nr < 0 || syscall_nr >= NR_syscalls) |
cd0980fc | 304 | return; |
d562aff9 TZ |
305 | |
306 | /* Here we're inside tp handler's rcu_read_lock_sched (__DO_TRACE) */ | |
7f1d2f82 SRRH |
307 | trace_file = rcu_dereference_sched(tr->enter_syscall_files[syscall_nr]); |
308 | if (!trace_file) | |
d562aff9 TZ |
309 | return; |
310 | ||
09a5059a | 311 | if (trace_trigger_soft_disabled(trace_file)) |
13a1e4ae | 312 | return; |
ee08c6ec | 313 | |
bed1ffca FW |
314 | sys_data = syscall_nr_to_meta(syscall_nr); |
315 | if (!sys_data) | |
316 | return; | |
317 | ||
318 | size = sizeof(*entry) + sizeof(unsigned long) * sys_data->nb_args; | |
319 | ||
cb1c45fb JX |
320 | entry = trace_event_buffer_reserve(&fbuffer, trace_file, size); |
321 | if (!entry) | |
bed1ffca FW |
322 | return; |
323 | ||
cb1c45fb | 324 | entry = ring_buffer_event_data(fbuffer.event); |
bed1ffca | 325 | entry->nr = syscall_nr; |
b35f549d | 326 | syscall_get_arguments(current, regs, args); |
d08e4113 | 327 | memcpy(entry->args, args, sizeof(unsigned long) * sys_data->nb_args); |
bed1ffca | 328 | |
cb1c45fb | 329 | trace_event_buffer_commit(&fbuffer); |
ee08c6ec FW |
330 | } |
331 | ||
12ab74ee | 332 | static void ftrace_syscall_exit(void *data, struct pt_regs *regs, long ret) |
ee08c6ec | 333 | { |
12ab74ee | 334 | struct trace_array *tr = data; |
7f1d2f82 | 335 | struct trace_event_file *trace_file; |
bed1ffca FW |
336 | struct syscall_trace_exit *entry; |
337 | struct syscall_metadata *sys_data; | |
cb1c45fb | 338 | struct trace_event_buffer fbuffer; |
ee08c6ec FW |
339 | int syscall_nr; |
340 | ||
f431b634 | 341 | syscall_nr = trace_get_syscall_nr(current, regs); |
086ba77a | 342 | if (syscall_nr < 0 || syscall_nr >= NR_syscalls) |
cd0980fc | 343 | return; |
d562aff9 TZ |
344 | |
345 | /* Here we're inside tp handler's rcu_read_lock_sched (__DO_TRACE()) */ | |
7f1d2f82 SRRH |
346 | trace_file = rcu_dereference_sched(tr->exit_syscall_files[syscall_nr]); |
347 | if (!trace_file) | |
d562aff9 TZ |
348 | return; |
349 | ||
09a5059a | 350 | if (trace_trigger_soft_disabled(trace_file)) |
13a1e4ae | 351 | return; |
ee08c6ec | 352 | |
bed1ffca FW |
353 | sys_data = syscall_nr_to_meta(syscall_nr); |
354 | if (!sys_data) | |
355 | return; | |
356 | ||
cb1c45fb JX |
357 | entry = trace_event_buffer_reserve(&fbuffer, trace_file, sizeof(*entry)); |
358 | if (!entry) | |
bed1ffca FW |
359 | return; |
360 | ||
cb1c45fb | 361 | entry = ring_buffer_event_data(fbuffer.event); |
bed1ffca FW |
362 | entry->nr = syscall_nr; |
363 | entry->ret = syscall_get_return_value(current, regs); | |
364 | ||
cb1c45fb | 365 | trace_event_buffer_commit(&fbuffer); |
ee08c6ec FW |
366 | } |
367 | ||
7f1d2f82 | 368 | static int reg_event_syscall_enter(struct trace_event_file *file, |
2425bcb9 | 369 | struct trace_event_call *call) |
ee08c6ec | 370 | { |
12ab74ee | 371 | struct trace_array *tr = file->tr; |
fb34a08c JB |
372 | int ret = 0; |
373 | int num; | |
fb34a08c | 374 | |
c252f657 | 375 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
3773b389 | 376 | if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls)) |
fb34a08c JB |
377 | return -ENOSYS; |
378 | mutex_lock(&syscall_trace_lock); | |
12ab74ee SR |
379 | if (!tr->sys_refcount_enter) |
380 | ret = register_trace_sys_enter(ftrace_syscall_enter, tr); | |
3b8e4273 | 381 | if (!ret) { |
d562aff9 | 382 | rcu_assign_pointer(tr->enter_syscall_files[num], file); |
12ab74ee | 383 | tr->sys_refcount_enter++; |
fb34a08c JB |
384 | } |
385 | mutex_unlock(&syscall_trace_lock); | |
386 | return ret; | |
ee08c6ec FW |
387 | } |
388 | ||
7f1d2f82 | 389 | static void unreg_event_syscall_enter(struct trace_event_file *file, |
2425bcb9 | 390 | struct trace_event_call *call) |
ee08c6ec | 391 | { |
12ab74ee | 392 | struct trace_array *tr = file->tr; |
fb34a08c | 393 | int num; |
ee08c6ec | 394 | |
c252f657 | 395 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
3773b389 | 396 | if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls)) |
fb34a08c JB |
397 | return; |
398 | mutex_lock(&syscall_trace_lock); | |
12ab74ee | 399 | tr->sys_refcount_enter--; |
fb5a613b | 400 | RCU_INIT_POINTER(tr->enter_syscall_files[num], NULL); |
12ab74ee SR |
401 | if (!tr->sys_refcount_enter) |
402 | unregister_trace_sys_enter(ftrace_syscall_enter, tr); | |
fb34a08c JB |
403 | mutex_unlock(&syscall_trace_lock); |
404 | } | |
ee08c6ec | 405 | |
7f1d2f82 | 406 | static int reg_event_syscall_exit(struct trace_event_file *file, |
2425bcb9 | 407 | struct trace_event_call *call) |
ee08c6ec | 408 | { |
12ab74ee | 409 | struct trace_array *tr = file->tr; |
fb34a08c JB |
410 | int ret = 0; |
411 | int num; | |
fb34a08c | 412 | |
c252f657 | 413 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
3773b389 | 414 | if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls)) |
fb34a08c JB |
415 | return -ENOSYS; |
416 | mutex_lock(&syscall_trace_lock); | |
12ab74ee SR |
417 | if (!tr->sys_refcount_exit) |
418 | ret = register_trace_sys_exit(ftrace_syscall_exit, tr); | |
3b8e4273 | 419 | if (!ret) { |
d562aff9 | 420 | rcu_assign_pointer(tr->exit_syscall_files[num], file); |
12ab74ee | 421 | tr->sys_refcount_exit++; |
ee08c6ec | 422 | } |
fb34a08c JB |
423 | mutex_unlock(&syscall_trace_lock); |
424 | return ret; | |
425 | } | |
ee08c6ec | 426 | |
7f1d2f82 | 427 | static void unreg_event_syscall_exit(struct trace_event_file *file, |
2425bcb9 | 428 | struct trace_event_call *call) |
fb34a08c | 429 | { |
12ab74ee | 430 | struct trace_array *tr = file->tr; |
fb34a08c | 431 | int num; |
ee08c6ec | 432 | |
c252f657 | 433 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
3773b389 | 434 | if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls)) |
fb34a08c JB |
435 | return; |
436 | mutex_lock(&syscall_trace_lock); | |
12ab74ee | 437 | tr->sys_refcount_exit--; |
fb5a613b | 438 | RCU_INIT_POINTER(tr->exit_syscall_files[num], NULL); |
12ab74ee SR |
439 | if (!tr->sys_refcount_exit) |
440 | unregister_trace_sys_exit(ftrace_syscall_exit, tr); | |
fb34a08c | 441 | mutex_unlock(&syscall_trace_lock); |
ee08c6ec | 442 | } |
fb34a08c | 443 | |
2425bcb9 | 444 | static int __init init_syscall_trace(struct trace_event_call *call) |
a1301da0 LJ |
445 | { |
446 | int id; | |
ba976970 IM |
447 | int num; |
448 | ||
449 | num = ((struct syscall_metadata *)call->data)->syscall_nr; | |
450 | if (num < 0 || num >= NR_syscalls) { | |
451 | pr_debug("syscall %s metadata not mapped, disabling ftrace event\n", | |
452 | ((struct syscall_metadata *)call->data)->name); | |
453 | return -ENOSYS; | |
454 | } | |
a1301da0 | 455 | |
50307a45 LJ |
456 | if (set_syscall_print_fmt(call) < 0) |
457 | return -ENOMEM; | |
458 | ||
c7ef3a90 SR |
459 | id = trace_event_raw_init(call); |
460 | ||
461 | if (id < 0) { | |
50307a45 | 462 | free_syscall_print_fmt(call); |
c7ef3a90 | 463 | return id; |
50307a45 | 464 | } |
c7ef3a90 SR |
465 | |
466 | return id; | |
a1301da0 LJ |
467 | } |
468 | ||
04ae87a5 PZ |
469 | static struct trace_event_fields __refdata syscall_enter_fields_array[] = { |
470 | SYSCALL_FIELD(int, __syscall_nr), | |
471 | { .type = TRACE_FUNCTION_TYPE, | |
472 | .define_fields = syscall_enter_define_fields }, | |
473 | {} | |
474 | }; | |
475 | ||
6f86ab9f VN |
476 | struct trace_event_functions enter_syscall_print_funcs = { |
477 | .trace = print_syscall_enter, | |
478 | }; | |
479 | ||
480 | struct trace_event_functions exit_syscall_print_funcs = { | |
481 | .trace = print_syscall_exit, | |
482 | }; | |
483 | ||
2425bcb9 | 484 | struct trace_event_class __refdata event_class_syscall_enter = { |
6f86ab9f VN |
485 | .system = "syscalls", |
486 | .reg = syscall_enter_register, | |
04ae87a5 | 487 | .fields_array = syscall_enter_fields_array, |
6f86ab9f VN |
488 | .get_fields = syscall_get_enter_fields, |
489 | .raw_init = init_syscall_trace, | |
490 | }; | |
491 | ||
2425bcb9 | 492 | struct trace_event_class __refdata event_class_syscall_exit = { |
6f86ab9f VN |
493 | .system = "syscalls", |
494 | .reg = syscall_exit_register, | |
04ae87a5 PZ |
495 | .fields_array = (struct trace_event_fields[]){ |
496 | SYSCALL_FIELD(int, __syscall_nr), | |
497 | SYSCALL_FIELD(long, ret), | |
498 | {} | |
499 | }, | |
6f86ab9f VN |
500 | .fields = LIST_HEAD_INIT(event_class_syscall_exit.fields), |
501 | .raw_init = init_syscall_trace, | |
502 | }; | |
503 | ||
c763ba06 | 504 | unsigned long __init __weak arch_syscall_addr(int nr) |
e7b8e675 MF |
505 | { |
506 | return (unsigned long)sys_call_table[nr]; | |
507 | } | |
508 | ||
5f893b26 | 509 | void __init init_ftrace_syscalls(void) |
c44fc770 FW |
510 | { |
511 | struct syscall_metadata *meta; | |
512 | unsigned long addr; | |
513 | int i; | |
0e242208 HN |
514 | void *ret; |
515 | ||
516 | if (!IS_ENABLED(CONFIG_HAVE_SPARSE_SYSCALL_NR)) { | |
517 | syscalls_metadata = kcalloc(NR_syscalls, | |
518 | sizeof(*syscalls_metadata), | |
519 | GFP_KERNEL); | |
520 | if (!syscalls_metadata) { | |
521 | WARN_ON(1); | |
522 | return; | |
523 | } | |
c44fc770 FW |
524 | } |
525 | ||
526 | for (i = 0; i < NR_syscalls; i++) { | |
527 | addr = arch_syscall_addr(i); | |
528 | meta = find_syscall_meta(addr); | |
c252f657 LJ |
529 | if (!meta) |
530 | continue; | |
531 | ||
532 | meta->syscall_nr = i; | |
0e242208 HN |
533 | |
534 | if (!IS_ENABLED(CONFIG_HAVE_SPARSE_SYSCALL_NR)) { | |
535 | syscalls_metadata[i] = meta; | |
536 | } else { | |
537 | ret = xa_store(&syscalls_metadata_sparse, i, meta, | |
538 | GFP_KERNEL); | |
539 | WARN(xa_is_err(ret), | |
540 | "Syscall memory allocation failed\n"); | |
541 | } | |
542 | ||
c44fc770 | 543 | } |
c44fc770 | 544 | } |
c44fc770 | 545 | |
07b139c8 | 546 | #ifdef CONFIG_PERF_EVENTS |
19007a67 | 547 | |
97d5a220 FW |
548 | static DECLARE_BITMAP(enabled_perf_enter_syscalls, NR_syscalls); |
549 | static DECLARE_BITMAP(enabled_perf_exit_syscalls, NR_syscalls); | |
550 | static int sys_perf_refcount_enter; | |
551 | static int sys_perf_refcount_exit; | |
f4b5ffcc | 552 | |
e87c6bc3 YS |
553 | static int perf_call_bpf_enter(struct trace_event_call *call, struct pt_regs *regs, |
554 | struct syscall_metadata *sys_data, | |
555 | struct syscall_trace_enter *rec) | |
556 | { | |
cf5f5cea | 557 | struct syscall_tp_t { |
d3c4db86 | 558 | struct trace_entry ent; |
ba8ea723 | 559 | int syscall_nr; |
609320c8 | 560 | unsigned long args[SYSCALL_DEFINE_MAXARGS]; |
d3c4db86 | 561 | } __aligned(8) param; |
cf5f5cea YS |
562 | int i; |
563 | ||
d3c4db86 YK |
564 | BUILD_BUG_ON(sizeof(param.ent) < sizeof(void *)); |
565 | ||
566 | /* bpf prog requires 'regs' to be the first member in the ctx (a.k.a. ¶m) */ | |
cf5f5cea YS |
567 | *(struct pt_regs **)¶m = regs; |
568 | param.syscall_nr = rec->nr; | |
569 | for (i = 0; i < sys_data->nb_args; i++) | |
570 | param.args[i] = rec->args[i]; | |
e87c6bc3 | 571 | return trace_call_bpf(call, ¶m); |
cf5f5cea YS |
572 | } |
573 | ||
38516ab5 | 574 | static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id) |
f4b5ffcc JB |
575 | { |
576 | struct syscall_metadata *sys_data; | |
20ab4425 | 577 | struct syscall_trace_enter *rec; |
1c024eca | 578 | struct hlist_head *head; |
d08e4113 | 579 | unsigned long args[6]; |
e87c6bc3 | 580 | bool valid_prog_array; |
f4b5ffcc | 581 | int syscall_nr; |
4ed7c92d | 582 | int rctx; |
19007a67 | 583 | int size; |
f4b5ffcc | 584 | |
f431b634 | 585 | syscall_nr = trace_get_syscall_nr(current, regs); |
086ba77a | 586 | if (syscall_nr < 0 || syscall_nr >= NR_syscalls) |
60916a93 | 587 | return; |
97d5a220 | 588 | if (!test_bit(syscall_nr, enabled_perf_enter_syscalls)) |
f4b5ffcc JB |
589 | return; |
590 | ||
591 | sys_data = syscall_nr_to_meta(syscall_nr); | |
592 | if (!sys_data) | |
593 | return; | |
594 | ||
421c7860 | 595 | head = this_cpu_ptr(sys_data->enter_event->perf_events); |
e87c6bc3 YS |
596 | valid_prog_array = bpf_prog_array_valid(sys_data->enter_event); |
597 | if (!valid_prog_array && hlist_empty(head)) | |
421c7860 ON |
598 | return; |
599 | ||
19007a67 FW |
600 | /* get the size after alignment with the u32 buffer size field */ |
601 | size = sizeof(unsigned long) * sys_data->nb_args + sizeof(*rec); | |
602 | size = ALIGN(size + sizeof(u32), sizeof(u64)); | |
603 | size -= sizeof(u32); | |
604 | ||
1e1dcd93 | 605 | rec = perf_trace_buf_alloc(size, NULL, &rctx); |
430ad5a6 XG |
606 | if (!rec) |
607 | return; | |
20ab4425 | 608 | |
20ab4425 | 609 | rec->nr = syscall_nr; |
b35f549d | 610 | syscall_get_arguments(current, regs, args); |
d08e4113 | 611 | memcpy(&rec->args, args, sizeof(unsigned long) * sys_data->nb_args); |
cf5f5cea | 612 | |
e87c6bc3 YS |
613 | if ((valid_prog_array && |
614 | !perf_call_bpf_enter(sys_data->enter_event, regs, sys_data, rec)) || | |
cf5f5cea YS |
615 | hlist_empty(head)) { |
616 | perf_swevent_put_recursion_context(rctx); | |
617 | return; | |
618 | } | |
619 | ||
1e1dcd93 AS |
620 | perf_trace_buf_submit(rec, size, rctx, |
621 | sys_data->enter_event->event.type, 1, regs, | |
8fd0fbbe | 622 | head, NULL); |
f4b5ffcc JB |
623 | } |
624 | ||
2425bcb9 | 625 | static int perf_sysenter_enable(struct trace_event_call *call) |
f4b5ffcc JB |
626 | { |
627 | int ret = 0; | |
628 | int num; | |
629 | ||
3bbe84e9 | 630 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
f4b5ffcc JB |
631 | |
632 | mutex_lock(&syscall_trace_lock); | |
97d5a220 | 633 | if (!sys_perf_refcount_enter) |
38516ab5 | 634 | ret = register_trace_sys_enter(perf_syscall_enter, NULL); |
f4b5ffcc | 635 | if (ret) { |
d282b9c0 | 636 | pr_info("event trace: Could not activate syscall entry trace point"); |
f4b5ffcc | 637 | } else { |
97d5a220 FW |
638 | set_bit(num, enabled_perf_enter_syscalls); |
639 | sys_perf_refcount_enter++; | |
f4b5ffcc JB |
640 | } |
641 | mutex_unlock(&syscall_trace_lock); | |
642 | return ret; | |
643 | } | |
644 | ||
2425bcb9 | 645 | static void perf_sysenter_disable(struct trace_event_call *call) |
f4b5ffcc JB |
646 | { |
647 | int num; | |
648 | ||
3bbe84e9 | 649 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
f4b5ffcc JB |
650 | |
651 | mutex_lock(&syscall_trace_lock); | |
97d5a220 FW |
652 | sys_perf_refcount_enter--; |
653 | clear_bit(num, enabled_perf_enter_syscalls); | |
654 | if (!sys_perf_refcount_enter) | |
38516ab5 | 655 | unregister_trace_sys_enter(perf_syscall_enter, NULL); |
f4b5ffcc JB |
656 | mutex_unlock(&syscall_trace_lock); |
657 | } | |
658 | ||
e87c6bc3 YS |
659 | static int perf_call_bpf_exit(struct trace_event_call *call, struct pt_regs *regs, |
660 | struct syscall_trace_exit *rec) | |
661 | { | |
cf5f5cea | 662 | struct syscall_tp_t { |
d3c4db86 | 663 | struct trace_entry ent; |
ba8ea723 | 664 | int syscall_nr; |
cf5f5cea | 665 | unsigned long ret; |
d3c4db86 | 666 | } __aligned(8) param; |
cf5f5cea | 667 | |
d3c4db86 | 668 | /* bpf prog requires 'regs' to be the first member in the ctx (a.k.a. ¶m) */ |
cf5f5cea YS |
669 | *(struct pt_regs **)¶m = regs; |
670 | param.syscall_nr = rec->nr; | |
671 | param.ret = rec->ret; | |
e87c6bc3 | 672 | return trace_call_bpf(call, ¶m); |
cf5f5cea YS |
673 | } |
674 | ||
38516ab5 | 675 | static void perf_syscall_exit(void *ignore, struct pt_regs *regs, long ret) |
f4b5ffcc JB |
676 | { |
677 | struct syscall_metadata *sys_data; | |
20ab4425 | 678 | struct syscall_trace_exit *rec; |
1c024eca | 679 | struct hlist_head *head; |
e87c6bc3 | 680 | bool valid_prog_array; |
f4b5ffcc | 681 | int syscall_nr; |
4ed7c92d | 682 | int rctx; |
20ab4425 | 683 | int size; |
f4b5ffcc | 684 | |
f431b634 | 685 | syscall_nr = trace_get_syscall_nr(current, regs); |
086ba77a | 686 | if (syscall_nr < 0 || syscall_nr >= NR_syscalls) |
60916a93 | 687 | return; |
97d5a220 | 688 | if (!test_bit(syscall_nr, enabled_perf_exit_syscalls)) |
f4b5ffcc JB |
689 | return; |
690 | ||
691 | sys_data = syscall_nr_to_meta(syscall_nr); | |
692 | if (!sys_data) | |
693 | return; | |
694 | ||
421c7860 | 695 | head = this_cpu_ptr(sys_data->exit_event->perf_events); |
e87c6bc3 YS |
696 | valid_prog_array = bpf_prog_array_valid(sys_data->exit_event); |
697 | if (!valid_prog_array && hlist_empty(head)) | |
421c7860 ON |
698 | return; |
699 | ||
20ab4425 FW |
700 | /* We can probably do that at build time */ |
701 | size = ALIGN(sizeof(*rec) + sizeof(u32), sizeof(u64)); | |
702 | size -= sizeof(u32); | |
19007a67 | 703 | |
1e1dcd93 | 704 | rec = perf_trace_buf_alloc(size, NULL, &rctx); |
430ad5a6 XG |
705 | if (!rec) |
706 | return; | |
20ab4425 | 707 | |
20ab4425 FW |
708 | rec->nr = syscall_nr; |
709 | rec->ret = syscall_get_return_value(current, regs); | |
cf5f5cea | 710 | |
e87c6bc3 YS |
711 | if ((valid_prog_array && |
712 | !perf_call_bpf_exit(sys_data->exit_event, regs, rec)) || | |
cf5f5cea YS |
713 | hlist_empty(head)) { |
714 | perf_swevent_put_recursion_context(rctx); | |
715 | return; | |
716 | } | |
717 | ||
1e1dcd93 | 718 | perf_trace_buf_submit(rec, size, rctx, sys_data->exit_event->event.type, |
8fd0fbbe | 719 | 1, regs, head, NULL); |
f4b5ffcc JB |
720 | } |
721 | ||
2425bcb9 | 722 | static int perf_sysexit_enable(struct trace_event_call *call) |
f4b5ffcc JB |
723 | { |
724 | int ret = 0; | |
725 | int num; | |
726 | ||
3bbe84e9 | 727 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
f4b5ffcc JB |
728 | |
729 | mutex_lock(&syscall_trace_lock); | |
97d5a220 | 730 | if (!sys_perf_refcount_exit) |
38516ab5 | 731 | ret = register_trace_sys_exit(perf_syscall_exit, NULL); |
f4b5ffcc | 732 | if (ret) { |
d282b9c0 | 733 | pr_info("event trace: Could not activate syscall exit trace point"); |
f4b5ffcc | 734 | } else { |
97d5a220 FW |
735 | set_bit(num, enabled_perf_exit_syscalls); |
736 | sys_perf_refcount_exit++; | |
f4b5ffcc JB |
737 | } |
738 | mutex_unlock(&syscall_trace_lock); | |
739 | return ret; | |
740 | } | |
741 | ||
2425bcb9 | 742 | static void perf_sysexit_disable(struct trace_event_call *call) |
f4b5ffcc JB |
743 | { |
744 | int num; | |
745 | ||
3bbe84e9 | 746 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
f4b5ffcc JB |
747 | |
748 | mutex_lock(&syscall_trace_lock); | |
97d5a220 FW |
749 | sys_perf_refcount_exit--; |
750 | clear_bit(num, enabled_perf_exit_syscalls); | |
751 | if (!sys_perf_refcount_exit) | |
38516ab5 | 752 | unregister_trace_sys_exit(perf_syscall_exit, NULL); |
f4b5ffcc JB |
753 | mutex_unlock(&syscall_trace_lock); |
754 | } | |
755 | ||
07b139c8 | 756 | #endif /* CONFIG_PERF_EVENTS */ |
f4b5ffcc | 757 | |
2425bcb9 | 758 | static int syscall_enter_register(struct trace_event_call *event, |
ceec0b6f | 759 | enum trace_reg type, void *data) |
2239291a | 760 | { |
7f1d2f82 | 761 | struct trace_event_file *file = data; |
12ab74ee | 762 | |
2239291a SR |
763 | switch (type) { |
764 | case TRACE_REG_REGISTER: | |
12ab74ee | 765 | return reg_event_syscall_enter(file, event); |
2239291a | 766 | case TRACE_REG_UNREGISTER: |
12ab74ee | 767 | unreg_event_syscall_enter(file, event); |
2239291a SR |
768 | return 0; |
769 | ||
770 | #ifdef CONFIG_PERF_EVENTS | |
771 | case TRACE_REG_PERF_REGISTER: | |
772 | return perf_sysenter_enable(event); | |
773 | case TRACE_REG_PERF_UNREGISTER: | |
774 | perf_sysenter_disable(event); | |
775 | return 0; | |
ceec0b6f JO |
776 | case TRACE_REG_PERF_OPEN: |
777 | case TRACE_REG_PERF_CLOSE: | |
489c75c3 JO |
778 | case TRACE_REG_PERF_ADD: |
779 | case TRACE_REG_PERF_DEL: | |
ceec0b6f | 780 | return 0; |
2239291a SR |
781 | #endif |
782 | } | |
783 | return 0; | |
784 | } | |
785 | ||
2425bcb9 | 786 | static int syscall_exit_register(struct trace_event_call *event, |
ceec0b6f | 787 | enum trace_reg type, void *data) |
2239291a | 788 | { |
7f1d2f82 | 789 | struct trace_event_file *file = data; |
12ab74ee | 790 | |
2239291a SR |
791 | switch (type) { |
792 | case TRACE_REG_REGISTER: | |
12ab74ee | 793 | return reg_event_syscall_exit(file, event); |
2239291a | 794 | case TRACE_REG_UNREGISTER: |
12ab74ee | 795 | unreg_event_syscall_exit(file, event); |
2239291a SR |
796 | return 0; |
797 | ||
798 | #ifdef CONFIG_PERF_EVENTS | |
799 | case TRACE_REG_PERF_REGISTER: | |
800 | return perf_sysexit_enable(event); | |
801 | case TRACE_REG_PERF_UNREGISTER: | |
802 | perf_sysexit_disable(event); | |
803 | return 0; | |
ceec0b6f JO |
804 | case TRACE_REG_PERF_OPEN: |
805 | case TRACE_REG_PERF_CLOSE: | |
489c75c3 JO |
806 | case TRACE_REG_PERF_ADD: |
807 | case TRACE_REG_PERF_DEL: | |
ceec0b6f | 808 | return 0; |
2239291a SR |
809 | #endif |
810 | } | |
811 | return 0; | |
812 | } |