blob: 4d0a209ecfdac745f20facd4ac0b9de1cba4ef72 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001#include <trace/syscall.h>
2#include <trace/events/syscalls.h>
3#include <linux/slab.h>
4#include <linux/kernel.h>
5#include <linux/module.h> /* for MODULE_NAME_LEN via KSYM_SYMBOL_LEN */
6#include <linux/ftrace.h>
7#include <linux/perf_event.h>
8#include <asm/syscall.h>
9
10#include "trace_output.h"
11#include "trace.h"
12
13static DEFINE_MUTEX(syscall_trace_lock);
14static int sys_refcount_enter;
15static int sys_refcount_exit;
16static DECLARE_BITMAP(enabled_enter_syscalls, NR_syscalls);
17static DECLARE_BITMAP(enabled_exit_syscalls, NR_syscalls);
18
19static int syscall_enter_register(struct ftrace_event_call *event,
20 enum trace_reg type, void *data);
21static int syscall_exit_register(struct ftrace_event_call *event,
22 enum trace_reg type, void *data);
23
24static int syscall_enter_define_fields(struct ftrace_event_call *call);
25static int syscall_exit_define_fields(struct ftrace_event_call *call);
26
27static struct list_head *
28syscall_get_enter_fields(struct ftrace_event_call *call)
29{
30 struct syscall_metadata *entry = call->data;
31
32 return &entry->enter_fields;
33}
34
35struct trace_event_functions enter_syscall_print_funcs = {
36 .trace = print_syscall_enter,
37};
38
39struct trace_event_functions exit_syscall_print_funcs = {
40 .trace = print_syscall_exit,
41};
42
43struct ftrace_event_class event_class_syscall_enter = {
44 .system = "syscalls",
45 .reg = syscall_enter_register,
46 .define_fields = syscall_enter_define_fields,
47 .get_fields = syscall_get_enter_fields,
48 .raw_init = init_syscall_trace,
49};
50
51struct ftrace_event_class event_class_syscall_exit = {
52 .system = "syscalls",
53 .reg = syscall_exit_register,
54 .define_fields = syscall_exit_define_fields,
55 .fields = LIST_HEAD_INIT(event_class_syscall_exit.fields),
56 .raw_init = init_syscall_trace,
57};
58
59extern struct syscall_metadata *__start_syscalls_metadata[];
60extern struct syscall_metadata *__stop_syscalls_metadata[];
61
62static struct syscall_metadata **syscalls_metadata;
63
64#ifndef ARCH_HAS_SYSCALL_MATCH_SYM_NAME
65static inline bool arch_syscall_match_sym_name(const char *sym, const char *name)
66{
67 /*
68 * Only compare after the "sys" prefix. Archs that use
69 * syscall wrappers may have syscalls symbols aliases prefixed
70 * with "SyS" instead of "sys", leading to an unwanted
71 * mismatch.
72 */
73 return !strcmp(sym + 3, name + 3);
74}
75#endif
76
77static __init struct syscall_metadata *
78find_syscall_meta(unsigned long syscall)
79{
80 struct syscall_metadata **start;
81 struct syscall_metadata **stop;
82 char str[KSYM_SYMBOL_LEN];
83
84
85 start = __start_syscalls_metadata;
86 stop = __stop_syscalls_metadata;
87 kallsyms_lookup(syscall, NULL, NULL, NULL, str);
88
89 if (arch_syscall_match_sym_name(str, "sys_ni_syscall"))
90 return NULL;
91
92 for ( ; start < stop; start++) {
93 if ((*start)->name && arch_syscall_match_sym_name(str, (*start)->name))
94 return *start;
95 }
96 return NULL;
97}
98
99static struct syscall_metadata *syscall_nr_to_meta(int nr)
100{
101 if (!syscalls_metadata || nr >= NR_syscalls || nr < 0)
102 return NULL;
103
104 return syscalls_metadata[nr];
105}
106
107enum print_line_t
108print_syscall_enter(struct trace_iterator *iter, int flags,
109 struct trace_event *event)
110{
111 struct trace_seq *s = &iter->seq;
112 struct trace_entry *ent = iter->ent;
113 struct syscall_trace_enter *trace;
114 struct syscall_metadata *entry;
115 int i, ret, syscall;
116
117 trace = (typeof(trace))ent;
118 syscall = trace->nr;
119 entry = syscall_nr_to_meta(syscall);
120
121 if (!entry)
122 goto end;
123
124 if (entry->enter_event->event.type != ent->type) {
125 WARN_ON_ONCE(1);
126 goto end;
127 }
128
129 ret = trace_seq_printf(s, "%s(", entry->name);
130 if (!ret)
131 return TRACE_TYPE_PARTIAL_LINE;
132
133 for (i = 0; i < entry->nb_args; i++) {
134 /* parameter types */
135 if (trace_flags & TRACE_ITER_VERBOSE) {
136 ret = trace_seq_printf(s, "%s ", entry->types[i]);
137 if (!ret)
138 return TRACE_TYPE_PARTIAL_LINE;
139 }
140 /* parameter values */
141 ret = trace_seq_printf(s, "%s: %lx%s", entry->args[i],
142 trace->args[i],
143 i == entry->nb_args - 1 ? "" : ", ");
144 if (!ret)
145 return TRACE_TYPE_PARTIAL_LINE;
146 }
147
148 ret = trace_seq_putc(s, ')');
149 if (!ret)
150 return TRACE_TYPE_PARTIAL_LINE;
151
152end:
153 ret = trace_seq_putc(s, '\n');
154 if (!ret)
155 return TRACE_TYPE_PARTIAL_LINE;
156
157 return TRACE_TYPE_HANDLED;
158}
159
160enum print_line_t
161print_syscall_exit(struct trace_iterator *iter, int flags,
162 struct trace_event *event)
163{
164 struct trace_seq *s = &iter->seq;
165 struct trace_entry *ent = iter->ent;
166 struct syscall_trace_exit *trace;
167 int syscall;
168 struct syscall_metadata *entry;
169 int ret;
170
171 trace = (typeof(trace))ent;
172 syscall = trace->nr;
173 entry = syscall_nr_to_meta(syscall);
174
175 if (!entry) {
176 trace_seq_printf(s, "\n");
177 return TRACE_TYPE_HANDLED;
178 }
179
180 if (entry->exit_event->event.type != ent->type) {
181 WARN_ON_ONCE(1);
182 return TRACE_TYPE_UNHANDLED;
183 }
184
185 ret = trace_seq_printf(s, "%s -> 0x%lx\n", entry->name,
186 trace->ret);
187 if (!ret)
188 return TRACE_TYPE_PARTIAL_LINE;
189
190 return TRACE_TYPE_HANDLED;
191}
192
193extern char *__bad_type_size(void);
194
195#define SYSCALL_FIELD(type, name) \
196 sizeof(type) != sizeof(trace.name) ? \
197 __bad_type_size() : \
198 #type, #name, offsetof(typeof(trace), name), \
199 sizeof(trace.name), is_signed_type(type)
200
201static
202int __set_enter_print_fmt(struct syscall_metadata *entry, char *buf, int len)
203{
204 int i;
205 int pos = 0;
206
207 /* When len=0, we just calculate the needed length */
208#define LEN_OR_ZERO (len ? len - pos : 0)
209
210 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
211 for (i = 0; i < entry->nb_args; i++) {
212 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s: 0x%%0%zulx%s",
213 entry->args[i], sizeof(unsigned long),
214 i == entry->nb_args - 1 ? "" : ", ");
215 }
216 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
217
218 for (i = 0; i < entry->nb_args; i++) {
219 pos += snprintf(buf + pos, LEN_OR_ZERO,
220 ", ((unsigned long)(REC->%s))", entry->args[i]);
221 }
222
223#undef LEN_OR_ZERO
224
225 /* return the length of print_fmt */
226 return pos;
227}
228
229static int set_syscall_print_fmt(struct ftrace_event_call *call)
230{
231 char *print_fmt;
232 int len;
233 struct syscall_metadata *entry = call->data;
234
235 if (entry->enter_event != call) {
236 call->print_fmt = "\"0x%lx\", REC->ret";
237 return 0;
238 }
239
240 /* First: called with 0 length to calculate the needed length */
241 len = __set_enter_print_fmt(entry, NULL, 0);
242
243 print_fmt = kmalloc(len + 1, GFP_KERNEL);
244 if (!print_fmt)
245 return -ENOMEM;
246
247 /* Second: actually write the @print_fmt */
248 __set_enter_print_fmt(entry, print_fmt, len + 1);
249 call->print_fmt = print_fmt;
250
251 return 0;
252}
253
254static void free_syscall_print_fmt(struct ftrace_event_call *call)
255{
256 struct syscall_metadata *entry = call->data;
257
258 if (entry->enter_event == call)
259 kfree(call->print_fmt);
260}
261
262static int syscall_enter_define_fields(struct ftrace_event_call *call)
263{
264 struct syscall_trace_enter trace;
265 struct syscall_metadata *meta = call->data;
266 int ret;
267 int i;
268 int offset = offsetof(typeof(trace), args);
269
270 ret = trace_define_field(call, SYSCALL_FIELD(int, nr), FILTER_OTHER);
271 if (ret)
272 return ret;
273
274 for (i = 0; i < meta->nb_args; i++) {
275 ret = trace_define_field(call, meta->types[i],
276 meta->args[i], offset,
277 sizeof(unsigned long), 0,
278 FILTER_OTHER);
279 offset += sizeof(unsigned long);
280 }
281
282 return ret;
283}
284
285static int syscall_exit_define_fields(struct ftrace_event_call *call)
286{
287 struct syscall_trace_exit trace;
288 int ret;
289
290 ret = trace_define_field(call, SYSCALL_FIELD(int, nr), FILTER_OTHER);
291 if (ret)
292 return ret;
293
294 ret = trace_define_field(call, SYSCALL_FIELD(long, ret),
295 FILTER_OTHER);
296
297 return ret;
298}
299
300void ftrace_syscall_enter(void *ignore, struct pt_regs *regs, long id)
301{
302 struct syscall_trace_enter *entry;
303 struct syscall_metadata *sys_data;
304 struct ring_buffer_event *event;
305 struct ring_buffer *buffer;
306 int size;
307 int syscall_nr;
308 unsigned long irq_flags;
309 int pc;
310
311 syscall_nr = syscall_get_nr(current, regs);
312 if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
313 return;
314 if (!test_bit(syscall_nr, enabled_enter_syscalls))
315 return;
316
317 sys_data = syscall_nr_to_meta(syscall_nr);
318 if (!sys_data)
319 return;
320
321 size = sizeof(*entry) + sizeof(unsigned long) * sys_data->nb_args;
322
323 local_save_flags(irq_flags);
324 pc = preempt_count();
325
326 event = trace_current_buffer_lock_reserve(&buffer,
327 sys_data->enter_event->event.type, size, irq_flags, pc);
328 if (!event)
329 return;
330
331 entry = ring_buffer_event_data(event);
332 entry->nr = syscall_nr;
333 syscall_get_arguments(current, regs, 0, sys_data->nb_args, entry->args);
334
335 if (!filter_current_check_discard(buffer, sys_data->enter_event,
336 entry, event))
337 trace_current_buffer_unlock_commit(buffer, event,
338 irq_flags, pc);
339}
340
341void ftrace_syscall_exit(void *ignore, struct pt_regs *regs, long ret)
342{
343 struct syscall_trace_exit *entry;
344 struct syscall_metadata *sys_data;
345 struct ring_buffer_event *event;
346 struct ring_buffer *buffer;
347 int syscall_nr;
348 unsigned long irq_flags;
349 int pc;
350
351 syscall_nr = syscall_get_nr(current, regs);
352 if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
353 return;
354 if (!test_bit(syscall_nr, enabled_exit_syscalls))
355 return;
356
357 sys_data = syscall_nr_to_meta(syscall_nr);
358 if (!sys_data)
359 return;
360
361 local_save_flags(irq_flags);
362 pc = preempt_count();
363
364 event = trace_current_buffer_lock_reserve(&buffer,
365 sys_data->exit_event->event.type, sizeof(*entry),
366 irq_flags, pc);
367 if (!event)
368 return;
369
370 entry = ring_buffer_event_data(event);
371 entry->nr = syscall_nr;
372 entry->ret = syscall_get_return_value(current, regs);
373
374 if (!filter_current_check_discard(buffer, sys_data->exit_event,
375 entry, event))
376 trace_current_buffer_unlock_commit(buffer, event,
377 irq_flags, pc);
378}
379
380int reg_event_syscall_enter(struct ftrace_event_call *call)
381{
382 int ret = 0;
383 int num;
384
385 num = ((struct syscall_metadata *)call->data)->syscall_nr;
386 if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
387 return -ENOSYS;
388 mutex_lock(&syscall_trace_lock);
389 if (!sys_refcount_enter)
390 ret = register_trace_sys_enter(ftrace_syscall_enter, NULL);
391 if (!ret) {
392 set_bit(num, enabled_enter_syscalls);
393 sys_refcount_enter++;
394 }
395 mutex_unlock(&syscall_trace_lock);
396 return ret;
397}
398
399void unreg_event_syscall_enter(struct ftrace_event_call *call)
400{
401 int num;
402
403 num = ((struct syscall_metadata *)call->data)->syscall_nr;
404 if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
405 return;
406 mutex_lock(&syscall_trace_lock);
407 sys_refcount_enter--;
408 clear_bit(num, enabled_enter_syscalls);
409 if (!sys_refcount_enter)
410 unregister_trace_sys_enter(ftrace_syscall_enter, NULL);
411 mutex_unlock(&syscall_trace_lock);
412}
413
414int reg_event_syscall_exit(struct ftrace_event_call *call)
415{
416 int ret = 0;
417 int num;
418
419 num = ((struct syscall_metadata *)call->data)->syscall_nr;
420 if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
421 return -ENOSYS;
422 mutex_lock(&syscall_trace_lock);
423 if (!sys_refcount_exit)
424 ret = register_trace_sys_exit(ftrace_syscall_exit, NULL);
425 if (!ret) {
426 set_bit(num, enabled_exit_syscalls);
427 sys_refcount_exit++;
428 }
429 mutex_unlock(&syscall_trace_lock);
430 return ret;
431}
432
433void unreg_event_syscall_exit(struct ftrace_event_call *call)
434{
435 int num;
436
437 num = ((struct syscall_metadata *)call->data)->syscall_nr;
438 if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
439 return;
440 mutex_lock(&syscall_trace_lock);
441 sys_refcount_exit--;
442 clear_bit(num, enabled_exit_syscalls);
443 if (!sys_refcount_exit)
444 unregister_trace_sys_exit(ftrace_syscall_exit, NULL);
445 mutex_unlock(&syscall_trace_lock);
446}
447
448int init_syscall_trace(struct ftrace_event_call *call)
449{
450 int id;
451 int num;
452
453 num = ((struct syscall_metadata *)call->data)->syscall_nr;
454 if (num < 0 || num >= NR_syscalls) {
455 pr_debug("syscall %s metadata not mapped, disabling ftrace event\n",
456 ((struct syscall_metadata *)call->data)->name);
457 return -ENOSYS;
458 }
459
460 if (set_syscall_print_fmt(call) < 0)
461 return -ENOMEM;
462
463 id = trace_event_raw_init(call);
464
465 if (id < 0) {
466 free_syscall_print_fmt(call);
467 return id;
468 }
469
470 return id;
471}
472
473unsigned long __init __weak arch_syscall_addr(int nr)
474{
475 return (unsigned long)sys_call_table[nr];
476}
477
478int __init init_ftrace_syscalls(void)
479{
480 struct syscall_metadata *meta;
481 unsigned long addr;
482 int i;
483
484 syscalls_metadata = kcalloc(NR_syscalls, sizeof(*syscalls_metadata),
485 GFP_KERNEL);
486 if (!syscalls_metadata) {
487 WARN_ON(1);
488 return -ENOMEM;
489 }
490
491 for (i = 0; i < NR_syscalls; i++) {
492 addr = arch_syscall_addr(i);
493 meta = find_syscall_meta(addr);
494 if (!meta)
495 continue;
496
497 meta->syscall_nr = i;
498 syscalls_metadata[i] = meta;
499 }
500
501 return 0;
502}
503core_initcall(init_ftrace_syscalls);
504
505#ifdef CONFIG_PERF_EVENTS
506
507static DECLARE_BITMAP(enabled_perf_enter_syscalls, NR_syscalls);
508static DECLARE_BITMAP(enabled_perf_exit_syscalls, NR_syscalls);
509static int sys_perf_refcount_enter;
510static int sys_perf_refcount_exit;
511
512static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id)
513{
514 struct syscall_metadata *sys_data;
515 struct syscall_trace_enter *rec;
516 struct hlist_head *head;
517 int syscall_nr;
518 int rctx;
519 int size;
520
521 syscall_nr = syscall_get_nr(current, regs);
522 if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
523 return;
524 if (!test_bit(syscall_nr, enabled_perf_enter_syscalls))
525 return;
526
527 sys_data = syscall_nr_to_meta(syscall_nr);
528 if (!sys_data)
529 return;
530
531 /* get the size after alignment with the u32 buffer size field */
532 size = sizeof(unsigned long) * sys_data->nb_args + sizeof(*rec);
533 size = ALIGN(size + sizeof(u32), sizeof(u64));
534 size -= sizeof(u32);
535
536 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
537 "perf buffer not large enough"))
538 return;
539
540 rec = (struct syscall_trace_enter *)perf_trace_buf_prepare(size,
541 sys_data->enter_event->event.type, regs, &rctx);
542 if (!rec)
543 return;
544
545 rec->nr = syscall_nr;
546 syscall_get_arguments(current, regs, 0, sys_data->nb_args,
547 (unsigned long *)&rec->args);
548
549 head = this_cpu_ptr(sys_data->enter_event->perf_events);
550 perf_trace_buf_submit(rec, size, rctx, 0, 1, regs, head);
551}
552
553int perf_sysenter_enable(struct ftrace_event_call *call)
554{
555 int ret = 0;
556 int num;
557
558 num = ((struct syscall_metadata *)call->data)->syscall_nr;
559
560 mutex_lock(&syscall_trace_lock);
561 if (!sys_perf_refcount_enter)
562 ret = register_trace_sys_enter(perf_syscall_enter, NULL);
563 if (ret) {
564 pr_info("event trace: Could not activate"
565 "syscall entry trace point");
566 } else {
567 set_bit(num, enabled_perf_enter_syscalls);
568 sys_perf_refcount_enter++;
569 }
570 mutex_unlock(&syscall_trace_lock);
571 return ret;
572}
573
574void perf_sysenter_disable(struct ftrace_event_call *call)
575{
576 int num;
577
578 num = ((struct syscall_metadata *)call->data)->syscall_nr;
579
580 mutex_lock(&syscall_trace_lock);
581 sys_perf_refcount_enter--;
582 clear_bit(num, enabled_perf_enter_syscalls);
583 if (!sys_perf_refcount_enter)
584 unregister_trace_sys_enter(perf_syscall_enter, NULL);
585 mutex_unlock(&syscall_trace_lock);
586}
587
588static void perf_syscall_exit(void *ignore, struct pt_regs *regs, long ret)
589{
590 struct syscall_metadata *sys_data;
591 struct syscall_trace_exit *rec;
592 struct hlist_head *head;
593 int syscall_nr;
594 int rctx;
595 int size;
596
597 syscall_nr = syscall_get_nr(current, regs);
598 if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
599 return;
600 if (!test_bit(syscall_nr, enabled_perf_exit_syscalls))
601 return;
602
603 sys_data = syscall_nr_to_meta(syscall_nr);
604 if (!sys_data)
605 return;
606
607 /* We can probably do that at build time */
608 size = ALIGN(sizeof(*rec) + sizeof(u32), sizeof(u64));
609 size -= sizeof(u32);
610
611 /*
612 * Impossible, but be paranoid with the future
613 * How to put this check outside runtime?
614 */
615 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
616 "exit event has grown above perf buffer size"))
617 return;
618
619 rec = (struct syscall_trace_exit *)perf_trace_buf_prepare(size,
620 sys_data->exit_event->event.type, regs, &rctx);
621 if (!rec)
622 return;
623
624 rec->nr = syscall_nr;
625 rec->ret = syscall_get_return_value(current, regs);
626
627 head = this_cpu_ptr(sys_data->exit_event->perf_events);
628 perf_trace_buf_submit(rec, size, rctx, 0, 1, regs, head);
629}
630
631int perf_sysexit_enable(struct ftrace_event_call *call)
632{
633 int ret = 0;
634 int num;
635
636 num = ((struct syscall_metadata *)call->data)->syscall_nr;
637
638 mutex_lock(&syscall_trace_lock);
639 if (!sys_perf_refcount_exit)
640 ret = register_trace_sys_exit(perf_syscall_exit, NULL);
641 if (ret) {
642 pr_info("event trace: Could not activate"
643 "syscall exit trace point");
644 } else {
645 set_bit(num, enabled_perf_exit_syscalls);
646 sys_perf_refcount_exit++;
647 }
648 mutex_unlock(&syscall_trace_lock);
649 return ret;
650}
651
652void perf_sysexit_disable(struct ftrace_event_call *call)
653{
654 int num;
655
656 num = ((struct syscall_metadata *)call->data)->syscall_nr;
657
658 mutex_lock(&syscall_trace_lock);
659 sys_perf_refcount_exit--;
660 clear_bit(num, enabled_perf_exit_syscalls);
661 if (!sys_perf_refcount_exit)
662 unregister_trace_sys_exit(perf_syscall_exit, NULL);
663 mutex_unlock(&syscall_trace_lock);
664}
665
666#endif /* CONFIG_PERF_EVENTS */
667
668static int syscall_enter_register(struct ftrace_event_call *event,
669 enum trace_reg type, void *data)
670{
671 switch (type) {
672 case TRACE_REG_REGISTER:
673 return reg_event_syscall_enter(event);
674 case TRACE_REG_UNREGISTER:
675 unreg_event_syscall_enter(event);
676 return 0;
677
678#ifdef CONFIG_PERF_EVENTS
679 case TRACE_REG_PERF_REGISTER:
680 return perf_sysenter_enable(event);
681 case TRACE_REG_PERF_UNREGISTER:
682 perf_sysenter_disable(event);
683 return 0;
684 case TRACE_REG_PERF_OPEN:
685 case TRACE_REG_PERF_CLOSE:
686 case TRACE_REG_PERF_ADD:
687 case TRACE_REG_PERF_DEL:
688 return 0;
689#endif
690 }
691 return 0;
692}
693
694static int syscall_exit_register(struct ftrace_event_call *event,
695 enum trace_reg type, void *data)
696{
697 switch (type) {
698 case TRACE_REG_REGISTER:
699 return reg_event_syscall_exit(event);
700 case TRACE_REG_UNREGISTER:
701 unreg_event_syscall_exit(event);
702 return 0;
703
704#ifdef CONFIG_PERF_EVENTS
705 case TRACE_REG_PERF_REGISTER:
706 return perf_sysexit_enable(event);
707 case TRACE_REG_PERF_UNREGISTER:
708 perf_sysexit_disable(event);
709 return 0;
710 case TRACE_REG_PERF_OPEN:
711 case TRACE_REG_PERF_CLOSE:
712 case TRACE_REG_PERF_ADD:
713 case TRACE_REG_PERF_DEL:
714 return 0;
715#endif
716 }
717 return 0;
718}