1 // SPDX-License-Identifier: GPL-2.0
2 #include <trace/syscall.h>
3 #include <trace/events/syscalls.h>
4 #include <linux/syscalls.h>
5 #include <linux/slab.h>
6 #include <linux/kernel.h>
7 #include <linux/module.h> /* for MODULE_NAME_LEN via KSYM_SYMBOL_LEN */
8 #include <linux/ftrace.h>
9 #include <linux/perf_event.h>
10 #include <linux/xarray.h>
11 #include <asm/syscall.h>
12
13 #include "trace_output.h"
14 #include "trace.h"
15
16 static DEFINE_MUTEX(syscall_trace_lock);
17
18 static int syscall_enter_register(struct trace_event_call *event,
19 enum trace_reg type, void *data);
20 static int syscall_exit_register(struct trace_event_call *event,
21 enum trace_reg type, void *data);
22
23 static struct list_head *
syscall_get_enter_fields(struct trace_event_call * call)24 syscall_get_enter_fields(struct trace_event_call *call)
25 {
26 struct syscall_metadata *entry = call->data;
27
28 return &entry->enter_fields;
29 }
30
31 extern struct syscall_metadata *__start_syscalls_metadata[];
32 extern struct syscall_metadata *__stop_syscalls_metadata[];
33
34 static DEFINE_XARRAY(syscalls_metadata_sparse);
35 static struct syscall_metadata **syscalls_metadata;
36
37 #ifndef ARCH_HAS_SYSCALL_MATCH_SYM_NAME
arch_syscall_match_sym_name(const char * sym,const char * 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
43 * with ".SyS" or ".sys" instead of "sys", leading to an unwanted
44 * mismatch.
45 */
46 return !strcmp(sym + 3, name + 3);
47 }
48 #endif
49
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
trace_get_syscall_nr(struct task_struct * task,struct pt_regs * regs)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
trace_get_syscall_nr(struct task_struct * task,struct pt_regs * regs)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
82 static __init struct syscall_metadata *
find_syscall_meta(unsigned long syscall)83 find_syscall_meta(unsigned long syscall)
84 {
85 struct syscall_metadata **start;
86 struct syscall_metadata **stop;
87 char str[KSYM_SYMBOL_LEN];
88
89
90 start = __start_syscalls_metadata;
91 stop = __stop_syscalls_metadata;
92 kallsyms_lookup(syscall, NULL, NULL, NULL, str);
93
94 if (arch_syscall_match_sym_name(str, "sys_ni_syscall"))
95 return NULL;
96
97 for ( ; start < stop; start++) {
98 if ((*start)->name && arch_syscall_match_sym_name(str, (*start)->name))
99 return *start;
100 }
101 return NULL;
102 }
103
syscall_nr_to_meta(int nr)104 static struct syscall_metadata *syscall_nr_to_meta(int nr)
105 {
106 if (IS_ENABLED(CONFIG_HAVE_SPARSE_SYSCALL_NR))
107 return xa_load(&syscalls_metadata_sparse, (unsigned long)nr);
108
109 if (!syscalls_metadata || nr >= NR_syscalls || nr < 0)
110 return NULL;
111
112 return syscalls_metadata[nr];
113 }
114
get_syscall_name(int syscall)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
126 static enum print_line_t
print_syscall_enter(struct trace_iterator * iter,int flags,struct trace_event * event)127 print_syscall_enter(struct trace_iterator *iter, int flags,
128 struct trace_event *event)
129 {
130 struct trace_array *tr = iter->tr;
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;
135 int i, syscall;
136
137 trace = (typeof(trace))ent;
138 syscall = trace->nr;
139 entry = syscall_nr_to_meta(syscall);
140
141 if (!entry)
142 goto end;
143
144 if (entry->enter_event->event.type != ent->type) {
145 WARN_ON_ONCE(1);
146 goto end;
147 }
148
149 trace_seq_printf(s, "%s(", entry->name);
150
151 for (i = 0; i < entry->nb_args; i++) {
152
153 if (trace_seq_has_overflowed(s))
154 goto end;
155
156 /* parameter types */
157 if (tr && tr->trace_flags & TRACE_ITER_VERBOSE)
158 trace_seq_printf(s, "%s ", entry->types[i]);
159
160 /* parameter values */
161 trace_seq_printf(s, "%s: %lx%s", entry->args[i],
162 trace->args[i],
163 i == entry->nb_args - 1 ? "" : ", ");
164 }
165
166 trace_seq_putc(s, ')');
167 end:
168 trace_seq_putc(s, '\n');
169
170 return trace_handle_return(s);
171 }
172
173 static enum print_line_t
print_syscall_exit(struct trace_iterator * iter,int flags,struct trace_event * event)174 print_syscall_exit(struct trace_iterator *iter, int flags,
175 struct trace_event *event)
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;
182
183 trace = (typeof(trace))ent;
184 syscall = trace->nr;
185 entry = syscall_nr_to_meta(syscall);
186
187 if (!entry) {
188 trace_seq_putc(s, '\n');
189 goto out;
190 }
191
192 if (entry->exit_event->event.type != ent->type) {
193 WARN_ON_ONCE(1);
194 return TRACE_TYPE_UNHANDLED;
195 }
196
197 trace_seq_printf(s, "%s -> 0x%lx\n", entry->name,
198 trace->ret);
199
200 out:
201 return trace_handle_return(s);
202 }
203
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 }
208
209 static int __init
__set_enter_print_fmt(struct syscall_metadata * entry,char * buf,int len)210 __set_enter_print_fmt(struct syscall_metadata *entry, char *buf, int len)
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
set_syscall_print_fmt(struct trace_event_call * call)237 static int __init set_syscall_print_fmt(struct trace_event_call *call)
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
free_syscall_print_fmt(struct trace_event_call * call)262 static void __init free_syscall_print_fmt(struct trace_event_call *call)
263 {
264 struct syscall_metadata *entry = call->data;
265
266 if (entry->enter_event == call)
267 kfree(call->print_fmt);
268 }
269
syscall_enter_define_fields(struct trace_event_call * call)270 static int __init syscall_enter_define_fields(struct trace_event_call *call)
271 {
272 struct syscall_trace_enter trace;
273 struct syscall_metadata *meta = call->data;
274 int offset = offsetof(typeof(trace), args);
275 int ret = 0;
276 int i;
277
278 for (i = 0; i < meta->nb_args; i++) {
279 ret = trace_define_field(call, meta->types[i],
280 meta->args[i], offset,
281 sizeof(unsigned long), 0,
282 FILTER_OTHER);
283 if (ret)
284 break;
285 offset += sizeof(unsigned long);
286 }
287
288 return ret;
289 }
290
ftrace_syscall_enter(void * data,struct pt_regs * regs,long id)291 static void ftrace_syscall_enter(void *data, struct pt_regs *regs, long id)
292 {
293 struct trace_array *tr = data;
294 struct trace_event_file *trace_file;
295 struct syscall_trace_enter *entry;
296 struct syscall_metadata *sys_data;
297 struct trace_event_buffer fbuffer;
298 unsigned long args[6];
299 int syscall_nr;
300 int size;
301
302 /*
303 * Syscall probe called with preemption enabled, but the ring
304 * buffer and per-cpu data require preemption to be disabled.
305 */
306 guard(preempt_notrace)();
307
308 syscall_nr = trace_get_syscall_nr(current, regs);
309 if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
310 return;
311
312 /* Here we're inside tp handler's rcu_read_lock_sched (__DO_TRACE) */
313 trace_file = rcu_dereference_sched(tr->enter_syscall_files[syscall_nr]);
314 if (!trace_file)
315 return;
316
317 if (trace_trigger_soft_disabled(trace_file))
318 return;
319
320 sys_data = syscall_nr_to_meta(syscall_nr);
321 if (!sys_data)
322 return;
323
324 size = sizeof(*entry) + sizeof(unsigned long) * sys_data->nb_args;
325
326 entry = trace_event_buffer_reserve(&fbuffer, trace_file, size);
327 if (!entry)
328 return;
329
330 entry = ring_buffer_event_data(fbuffer.event);
331 entry->nr = syscall_nr;
332 syscall_get_arguments(current, regs, args);
333 memcpy(entry->args, args, sizeof(unsigned long) * sys_data->nb_args);
334
335 trace_event_buffer_commit(&fbuffer);
336 }
337
ftrace_syscall_exit(void * data,struct pt_regs * regs,long ret)338 static void ftrace_syscall_exit(void *data, struct pt_regs *regs, long ret)
339 {
340 struct trace_array *tr = data;
341 struct trace_event_file *trace_file;
342 struct syscall_trace_exit *entry;
343 struct syscall_metadata *sys_data;
344 struct trace_event_buffer fbuffer;
345 int syscall_nr;
346
347 /*
348 * Syscall probe called with preemption enabled, but the ring
349 * buffer and per-cpu data require preemption to be disabled.
350 */
351 guard(preempt_notrace)();
352
353 syscall_nr = trace_get_syscall_nr(current, regs);
354 if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
355 return;
356
357 /* Here we're inside tp handler's rcu_read_lock_sched (__DO_TRACE()) */
358 trace_file = rcu_dereference_sched(tr->exit_syscall_files[syscall_nr]);
359 if (!trace_file)
360 return;
361
362 if (trace_trigger_soft_disabled(trace_file))
363 return;
364
365 sys_data = syscall_nr_to_meta(syscall_nr);
366 if (!sys_data)
367 return;
368
369 entry = trace_event_buffer_reserve(&fbuffer, trace_file, sizeof(*entry));
370 if (!entry)
371 return;
372
373 entry = ring_buffer_event_data(fbuffer.event);
374 entry->nr = syscall_nr;
375 entry->ret = syscall_get_return_value(current, regs);
376
377 trace_event_buffer_commit(&fbuffer);
378 }
379
reg_event_syscall_enter(struct trace_event_file * file,struct trace_event_call * call)380 static int reg_event_syscall_enter(struct trace_event_file *file,
381 struct trace_event_call *call)
382 {
383 struct trace_array *tr = file->tr;
384 int ret = 0;
385 int num;
386
387 num = ((struct syscall_metadata *)call->data)->syscall_nr;
388 if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
389 return -ENOSYS;
390 mutex_lock(&syscall_trace_lock);
391 if (!tr->sys_refcount_enter)
392 ret = register_trace_sys_enter(ftrace_syscall_enter, tr);
393 if (!ret) {
394 rcu_assign_pointer(tr->enter_syscall_files[num], file);
395 tr->sys_refcount_enter++;
396 }
397 mutex_unlock(&syscall_trace_lock);
398 return ret;
399 }
400
unreg_event_syscall_enter(struct trace_event_file * file,struct trace_event_call * call)401 static void unreg_event_syscall_enter(struct trace_event_file *file,
402 struct trace_event_call *call)
403 {
404 struct trace_array *tr = file->tr;
405 int num;
406
407 num = ((struct syscall_metadata *)call->data)->syscall_nr;
408 if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
409 return;
410 mutex_lock(&syscall_trace_lock);
411 tr->sys_refcount_enter--;
412 RCU_INIT_POINTER(tr->enter_syscall_files[num], NULL);
413 if (!tr->sys_refcount_enter)
414 unregister_trace_sys_enter(ftrace_syscall_enter, tr);
415 mutex_unlock(&syscall_trace_lock);
416 }
417
reg_event_syscall_exit(struct trace_event_file * file,struct trace_event_call * call)418 static int reg_event_syscall_exit(struct trace_event_file *file,
419 struct trace_event_call *call)
420 {
421 struct trace_array *tr = file->tr;
422 int ret = 0;
423 int num;
424
425 num = ((struct syscall_metadata *)call->data)->syscall_nr;
426 if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
427 return -ENOSYS;
428 mutex_lock(&syscall_trace_lock);
429 if (!tr->sys_refcount_exit)
430 ret = register_trace_sys_exit(ftrace_syscall_exit, tr);
431 if (!ret) {
432 rcu_assign_pointer(tr->exit_syscall_files[num], file);
433 tr->sys_refcount_exit++;
434 }
435 mutex_unlock(&syscall_trace_lock);
436 return ret;
437 }
438
unreg_event_syscall_exit(struct trace_event_file * file,struct trace_event_call * call)439 static void unreg_event_syscall_exit(struct trace_event_file *file,
440 struct trace_event_call *call)
441 {
442 struct trace_array *tr = file->tr;
443 int num;
444
445 num = ((struct syscall_metadata *)call->data)->syscall_nr;
446 if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
447 return;
448 mutex_lock(&syscall_trace_lock);
449 tr->sys_refcount_exit--;
450 RCU_INIT_POINTER(tr->exit_syscall_files[num], NULL);
451 if (!tr->sys_refcount_exit)
452 unregister_trace_sys_exit(ftrace_syscall_exit, tr);
453 mutex_unlock(&syscall_trace_lock);
454 }
455
init_syscall_trace(struct trace_event_call * call)456 static int __init init_syscall_trace(struct trace_event_call *call)
457 {
458 int id;
459 int num;
460
461 num = ((struct syscall_metadata *)call->data)->syscall_nr;
462 if (num < 0 || num >= NR_syscalls) {
463 pr_debug("syscall %s metadata not mapped, disabling ftrace event\n",
464 ((struct syscall_metadata *)call->data)->name);
465 return -ENOSYS;
466 }
467
468 if (set_syscall_print_fmt(call) < 0)
469 return -ENOMEM;
470
471 id = trace_event_raw_init(call);
472
473 if (id < 0) {
474 free_syscall_print_fmt(call);
475 return id;
476 }
477
478 return id;
479 }
480
481 static struct trace_event_fields __refdata syscall_enter_fields_array[] = {
482 SYSCALL_FIELD(int, __syscall_nr),
483 { .type = TRACE_FUNCTION_TYPE,
484 .define_fields = syscall_enter_define_fields },
485 {}
486 };
487
488 struct trace_event_functions enter_syscall_print_funcs = {
489 .trace = print_syscall_enter,
490 };
491
492 struct trace_event_functions exit_syscall_print_funcs = {
493 .trace = print_syscall_exit,
494 };
495
496 struct trace_event_class __refdata event_class_syscall_enter = {
497 .system = "syscalls",
498 .reg = syscall_enter_register,
499 .fields_array = syscall_enter_fields_array,
500 .get_fields = syscall_get_enter_fields,
501 .raw_init = init_syscall_trace,
502 };
503
504 struct trace_event_class __refdata event_class_syscall_exit = {
505 .system = "syscalls",
506 .reg = syscall_exit_register,
507 .fields_array = (struct trace_event_fields[]){
508 SYSCALL_FIELD(int, __syscall_nr),
509 SYSCALL_FIELD(long, ret),
510 {}
511 },
512 .fields = LIST_HEAD_INIT(event_class_syscall_exit.fields),
513 .raw_init = init_syscall_trace,
514 };
515
arch_syscall_addr(int nr)516 unsigned long __init __weak arch_syscall_addr(int nr)
517 {
518 return (unsigned long)sys_call_table[nr];
519 }
520
init_ftrace_syscalls(void)521 void __init init_ftrace_syscalls(void)
522 {
523 struct syscall_metadata *meta;
524 unsigned long addr;
525 int i;
526 void *ret;
527
528 if (!IS_ENABLED(CONFIG_HAVE_SPARSE_SYSCALL_NR)) {
529 syscalls_metadata = kcalloc(NR_syscalls,
530 sizeof(*syscalls_metadata),
531 GFP_KERNEL);
532 if (!syscalls_metadata) {
533 WARN_ON(1);
534 return;
535 }
536 }
537
538 for (i = 0; i < NR_syscalls; i++) {
539 addr = arch_syscall_addr(i);
540 meta = find_syscall_meta(addr);
541 if (!meta)
542 continue;
543
544 meta->syscall_nr = i;
545
546 if (!IS_ENABLED(CONFIG_HAVE_SPARSE_SYSCALL_NR)) {
547 syscalls_metadata[i] = meta;
548 } else {
549 ret = xa_store(&syscalls_metadata_sparse, i, meta,
550 GFP_KERNEL);
551 WARN(xa_is_err(ret),
552 "Syscall memory allocation failed\n");
553 }
554
555 }
556 }
557
558 #ifdef CONFIG_PERF_EVENTS
559
560 static DECLARE_BITMAP(enabled_perf_enter_syscalls, NR_syscalls);
561 static DECLARE_BITMAP(enabled_perf_exit_syscalls, NR_syscalls);
562 static int sys_perf_refcount_enter;
563 static int sys_perf_refcount_exit;
564
perf_call_bpf_enter(struct trace_event_call * call,struct pt_regs * regs,struct syscall_metadata * sys_data,struct syscall_trace_enter * rec)565 static int perf_call_bpf_enter(struct trace_event_call *call, struct pt_regs *regs,
566 struct syscall_metadata *sys_data,
567 struct syscall_trace_enter *rec)
568 {
569 struct syscall_tp_t {
570 struct trace_entry ent;
571 unsigned long syscall_nr;
572 unsigned long args[SYSCALL_DEFINE_MAXARGS];
573 } __aligned(8) param;
574 int i;
575
576 BUILD_BUG_ON(sizeof(param.ent) < sizeof(void *));
577
578 /* bpf prog requires 'regs' to be the first member in the ctx (a.k.a. ¶m) */
579 *(struct pt_regs **)¶m = regs;
580 param.syscall_nr = rec->nr;
581 for (i = 0; i < sys_data->nb_args; i++)
582 param.args[i] = rec->args[i];
583 return trace_call_bpf(call, ¶m);
584 }
585
perf_syscall_enter(void * ignore,struct pt_regs * regs,long id)586 static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id)
587 {
588 struct syscall_metadata *sys_data;
589 struct syscall_trace_enter *rec;
590 struct hlist_head *head;
591 unsigned long args[6];
592 bool valid_prog_array;
593 int syscall_nr;
594 int rctx;
595 int size;
596
597 syscall_nr = trace_get_syscall_nr(current, regs);
598 if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
599 return;
600 if (!test_bit(syscall_nr, enabled_perf_enter_syscalls))
601 return;
602
603 sys_data = syscall_nr_to_meta(syscall_nr);
604 if (!sys_data)
605 return;
606
607 head = this_cpu_ptr(sys_data->enter_event->perf_events);
608 valid_prog_array = bpf_prog_array_valid(sys_data->enter_event);
609 if (!valid_prog_array && hlist_empty(head))
610 return;
611
612 /* get the size after alignment with the u32 buffer size field */
613 size = sizeof(unsigned long) * sys_data->nb_args + sizeof(*rec);
614 size = ALIGN(size + sizeof(u32), sizeof(u64));
615 size -= sizeof(u32);
616
617 rec = perf_trace_buf_alloc(size, NULL, &rctx);
618 if (!rec)
619 return;
620
621 rec->nr = syscall_nr;
622 syscall_get_arguments(current, regs, args);
623 memcpy(&rec->args, args, sizeof(unsigned long) * sys_data->nb_args);
624
625 if ((valid_prog_array &&
626 !perf_call_bpf_enter(sys_data->enter_event, regs, sys_data, rec)) ||
627 hlist_empty(head)) {
628 perf_swevent_put_recursion_context(rctx);
629 return;
630 }
631
632 perf_trace_buf_submit(rec, size, rctx,
633 sys_data->enter_event->event.type, 1, regs,
634 head, NULL);
635 }
636
perf_sysenter_enable(struct trace_event_call * call)637 static int perf_sysenter_enable(struct trace_event_call *call)
638 {
639 int ret = 0;
640 int num;
641
642 num = ((struct syscall_metadata *)call->data)->syscall_nr;
643
644 mutex_lock(&syscall_trace_lock);
645 if (!sys_perf_refcount_enter)
646 ret = register_trace_sys_enter(perf_syscall_enter, NULL);
647 if (ret) {
648 pr_info("event trace: Could not activate syscall entry trace point");
649 } else {
650 set_bit(num, enabled_perf_enter_syscalls);
651 sys_perf_refcount_enter++;
652 }
653 mutex_unlock(&syscall_trace_lock);
654 return ret;
655 }
656
perf_sysenter_disable(struct trace_event_call * call)657 static void perf_sysenter_disable(struct trace_event_call *call)
658 {
659 int num;
660
661 num = ((struct syscall_metadata *)call->data)->syscall_nr;
662
663 mutex_lock(&syscall_trace_lock);
664 sys_perf_refcount_enter--;
665 clear_bit(num, enabled_perf_enter_syscalls);
666 if (!sys_perf_refcount_enter)
667 unregister_trace_sys_enter(perf_syscall_enter, NULL);
668 mutex_unlock(&syscall_trace_lock);
669 }
670
perf_call_bpf_exit(struct trace_event_call * call,struct pt_regs * regs,struct syscall_trace_exit * rec)671 static int perf_call_bpf_exit(struct trace_event_call *call, struct pt_regs *regs,
672 struct syscall_trace_exit *rec)
673 {
674 struct syscall_tp_t {
675 struct trace_entry ent;
676 unsigned long syscall_nr;
677 unsigned long ret;
678 } __aligned(8) param;
679
680 /* bpf prog requires 'regs' to be the first member in the ctx (a.k.a. ¶m) */
681 *(struct pt_regs **)¶m = regs;
682 param.syscall_nr = rec->nr;
683 param.ret = rec->ret;
684 return trace_call_bpf(call, ¶m);
685 }
686
perf_syscall_exit(void * ignore,struct pt_regs * regs,long ret)687 static void perf_syscall_exit(void *ignore, struct pt_regs *regs, long ret)
688 {
689 struct syscall_metadata *sys_data;
690 struct syscall_trace_exit *rec;
691 struct hlist_head *head;
692 bool valid_prog_array;
693 int syscall_nr;
694 int rctx;
695 int size;
696
697 syscall_nr = trace_get_syscall_nr(current, regs);
698 if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
699 return;
700 if (!test_bit(syscall_nr, enabled_perf_exit_syscalls))
701 return;
702
703 sys_data = syscall_nr_to_meta(syscall_nr);
704 if (!sys_data)
705 return;
706
707 head = this_cpu_ptr(sys_data->exit_event->perf_events);
708 valid_prog_array = bpf_prog_array_valid(sys_data->exit_event);
709 if (!valid_prog_array && hlist_empty(head))
710 return;
711
712 /* We can probably do that at build time */
713 size = ALIGN(sizeof(*rec) + sizeof(u32), sizeof(u64));
714 size -= sizeof(u32);
715
716 rec = perf_trace_buf_alloc(size, NULL, &rctx);
717 if (!rec)
718 return;
719
720 rec->nr = syscall_nr;
721 rec->ret = syscall_get_return_value(current, regs);
722
723 if ((valid_prog_array &&
724 !perf_call_bpf_exit(sys_data->exit_event, regs, rec)) ||
725 hlist_empty(head)) {
726 perf_swevent_put_recursion_context(rctx);
727 return;
728 }
729
730 perf_trace_buf_submit(rec, size, rctx, sys_data->exit_event->event.type,
731 1, regs, head, NULL);
732 }
733
perf_sysexit_enable(struct trace_event_call * call)734 static int perf_sysexit_enable(struct trace_event_call *call)
735 {
736 int ret = 0;
737 int num;
738
739 num = ((struct syscall_metadata *)call->data)->syscall_nr;
740
741 mutex_lock(&syscall_trace_lock);
742 if (!sys_perf_refcount_exit)
743 ret = register_trace_sys_exit(perf_syscall_exit, NULL);
744 if (ret) {
745 pr_info("event trace: Could not activate syscall exit trace point");
746 } else {
747 set_bit(num, enabled_perf_exit_syscalls);
748 sys_perf_refcount_exit++;
749 }
750 mutex_unlock(&syscall_trace_lock);
751 return ret;
752 }
753
perf_sysexit_disable(struct trace_event_call * call)754 static void perf_sysexit_disable(struct trace_event_call *call)
755 {
756 int num;
757
758 num = ((struct syscall_metadata *)call->data)->syscall_nr;
759
760 mutex_lock(&syscall_trace_lock);
761 sys_perf_refcount_exit--;
762 clear_bit(num, enabled_perf_exit_syscalls);
763 if (!sys_perf_refcount_exit)
764 unregister_trace_sys_exit(perf_syscall_exit, NULL);
765 mutex_unlock(&syscall_trace_lock);
766 }
767
768 #endif /* CONFIG_PERF_EVENTS */
769
syscall_enter_register(struct trace_event_call * event,enum trace_reg type,void * data)770 static int syscall_enter_register(struct trace_event_call *event,
771 enum trace_reg type, void *data)
772 {
773 struct trace_event_file *file = data;
774
775 switch (type) {
776 case TRACE_REG_REGISTER:
777 return reg_event_syscall_enter(file, event);
778 case TRACE_REG_UNREGISTER:
779 unreg_event_syscall_enter(file, event);
780 return 0;
781
782 #ifdef CONFIG_PERF_EVENTS
783 case TRACE_REG_PERF_REGISTER:
784 return perf_sysenter_enable(event);
785 case TRACE_REG_PERF_UNREGISTER:
786 perf_sysenter_disable(event);
787 return 0;
788 case TRACE_REG_PERF_OPEN:
789 case TRACE_REG_PERF_CLOSE:
790 case TRACE_REG_PERF_ADD:
791 case TRACE_REG_PERF_DEL:
792 return 0;
793 #endif
794 }
795 return 0;
796 }
797
syscall_exit_register(struct trace_event_call * event,enum trace_reg type,void * data)798 static int syscall_exit_register(struct trace_event_call *event,
799 enum trace_reg type, void *data)
800 {
801 struct trace_event_file *file = data;
802
803 switch (type) {
804 case TRACE_REG_REGISTER:
805 return reg_event_syscall_exit(file, event);
806 case TRACE_REG_UNREGISTER:
807 unreg_event_syscall_exit(file, event);
808 return 0;
809
810 #ifdef CONFIG_PERF_EVENTS
811 case TRACE_REG_PERF_REGISTER:
812 return perf_sysexit_enable(event);
813 case TRACE_REG_PERF_UNREGISTER:
814 perf_sysexit_disable(event);
815 return 0;
816 case TRACE_REG_PERF_OPEN:
817 case TRACE_REG_PERF_CLOSE:
818 case TRACE_REG_PERF_ADD:
819 case TRACE_REG_PERF_DEL:
820 return 0;
821 #endif
822 }
823 return 0;
824 }
825