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