xref: /openbmc/linux/kernel/trace/trace_syscalls.c (revision 3cf3cdea)
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 *
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
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
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 
82 static __init struct syscall_metadata *
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 
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 
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
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->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
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 extern char *__bad_type_size(void);
205 
206 #define SYSCALL_FIELD(_type, _name) {					\
207 	.type = #_type, .name = #_name,					\
208 	.size = sizeof(_type), .align = __alignof__(_type),		\
209 	.is_signed = is_signed_type(_type), .filter_type = FILTER_OTHER }
210 
211 static int __init
212 __set_enter_print_fmt(struct syscall_metadata *entry, char *buf, int len)
213 {
214 	int i;
215 	int pos = 0;
216 
217 	/* When len=0, we just calculate the needed length */
218 #define LEN_OR_ZERO (len ? len - pos : 0)
219 
220 	pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
221 	for (i = 0; i < entry->nb_args; i++) {
222 		pos += snprintf(buf + pos, LEN_OR_ZERO, "%s: 0x%%0%zulx%s",
223 				entry->args[i], sizeof(unsigned long),
224 				i == entry->nb_args - 1 ? "" : ", ");
225 	}
226 	pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
227 
228 	for (i = 0; i < entry->nb_args; i++) {
229 		pos += snprintf(buf + pos, LEN_OR_ZERO,
230 				", ((unsigned long)(REC->%s))", entry->args[i]);
231 	}
232 
233 #undef LEN_OR_ZERO
234 
235 	/* return the length of print_fmt */
236 	return pos;
237 }
238 
239 static int __init set_syscall_print_fmt(struct trace_event_call *call)
240 {
241 	char *print_fmt;
242 	int len;
243 	struct syscall_metadata *entry = call->data;
244 
245 	if (entry->enter_event != call) {
246 		call->print_fmt = "\"0x%lx\", REC->ret";
247 		return 0;
248 	}
249 
250 	/* First: called with 0 length to calculate the needed length */
251 	len = __set_enter_print_fmt(entry, NULL, 0);
252 
253 	print_fmt = kmalloc(len + 1, GFP_KERNEL);
254 	if (!print_fmt)
255 		return -ENOMEM;
256 
257 	/* Second: actually write the @print_fmt */
258 	__set_enter_print_fmt(entry, print_fmt, len + 1);
259 	call->print_fmt = print_fmt;
260 
261 	return 0;
262 }
263 
264 static void __init free_syscall_print_fmt(struct trace_event_call *call)
265 {
266 	struct syscall_metadata *entry = call->data;
267 
268 	if (entry->enter_event == call)
269 		kfree(call->print_fmt);
270 }
271 
272 static int __init syscall_enter_define_fields(struct trace_event_call *call)
273 {
274 	struct syscall_trace_enter trace;
275 	struct syscall_metadata *meta = call->data;
276 	int offset = offsetof(typeof(trace), args);
277 	int ret = 0;
278 	int i;
279 
280 	for (i = 0; i < meta->nb_args; i++) {
281 		ret = trace_define_field(call, meta->types[i],
282 					 meta->args[i], offset,
283 					 sizeof(unsigned long), 0,
284 					 FILTER_OTHER);
285 		if (ret)
286 			break;
287 		offset += sizeof(unsigned long);
288 	}
289 
290 	return ret;
291 }
292 
293 static void ftrace_syscall_enter(void *data, struct pt_regs *regs, long id)
294 {
295 	struct trace_array *tr = data;
296 	struct trace_event_file *trace_file;
297 	struct syscall_trace_enter *entry;
298 	struct syscall_metadata *sys_data;
299 	struct ring_buffer_event *event;
300 	struct trace_buffer *buffer;
301 	unsigned int trace_ctx;
302 	unsigned long args[6];
303 	int syscall_nr;
304 	int size;
305 
306 	syscall_nr = trace_get_syscall_nr(current, regs);
307 	if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
308 		return;
309 
310 	/* Here we're inside tp handler's rcu_read_lock_sched (__DO_TRACE) */
311 	trace_file = rcu_dereference_sched(tr->enter_syscall_files[syscall_nr]);
312 	if (!trace_file)
313 		return;
314 
315 	if (trace_trigger_soft_disabled(trace_file))
316 		return;
317 
318 	sys_data = syscall_nr_to_meta(syscall_nr);
319 	if (!sys_data)
320 		return;
321 
322 	size = sizeof(*entry) + sizeof(unsigned long) * sys_data->nb_args;
323 
324 	trace_ctx = tracing_gen_ctx();
325 
326 	buffer = tr->array_buffer.buffer;
327 	event = trace_buffer_lock_reserve(buffer,
328 			sys_data->enter_event->event.type, size, trace_ctx);
329 	if (!event)
330 		return;
331 
332 	entry = ring_buffer_event_data(event);
333 	entry->nr = syscall_nr;
334 	syscall_get_arguments(current, regs, args);
335 	memcpy(entry->args, args, sizeof(unsigned long) * sys_data->nb_args);
336 
337 	event_trigger_unlock_commit(trace_file, buffer, event, entry,
338 				    trace_ctx);
339 }
340 
341 static void ftrace_syscall_exit(void *data, struct pt_regs *regs, long ret)
342 {
343 	struct trace_array *tr = data;
344 	struct trace_event_file *trace_file;
345 	struct syscall_trace_exit *entry;
346 	struct syscall_metadata *sys_data;
347 	struct ring_buffer_event *event;
348 	struct trace_buffer *buffer;
349 	unsigned int trace_ctx;
350 	int syscall_nr;
351 
352 	syscall_nr = trace_get_syscall_nr(current, regs);
353 	if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
354 		return;
355 
356 	/* Here we're inside tp handler's rcu_read_lock_sched (__DO_TRACE()) */
357 	trace_file = rcu_dereference_sched(tr->exit_syscall_files[syscall_nr]);
358 	if (!trace_file)
359 		return;
360 
361 	if (trace_trigger_soft_disabled(trace_file))
362 		return;
363 
364 	sys_data = syscall_nr_to_meta(syscall_nr);
365 	if (!sys_data)
366 		return;
367 
368 	trace_ctx = tracing_gen_ctx();
369 
370 	buffer = tr->array_buffer.buffer;
371 	event = trace_buffer_lock_reserve(buffer,
372 			sys_data->exit_event->event.type, sizeof(*entry),
373 			trace_ctx);
374 	if (!event)
375 		return;
376 
377 	entry = ring_buffer_event_data(event);
378 	entry->nr = syscall_nr;
379 	entry->ret = syscall_get_return_value(current, regs);
380 
381 	event_trigger_unlock_commit(trace_file, buffer, event, entry,
382 				    trace_ctx);
383 }
384 
385 static int reg_event_syscall_enter(struct trace_event_file *file,
386 				   struct trace_event_call *call)
387 {
388 	struct trace_array *tr = file->tr;
389 	int ret = 0;
390 	int num;
391 
392 	num = ((struct syscall_metadata *)call->data)->syscall_nr;
393 	if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
394 		return -ENOSYS;
395 	mutex_lock(&syscall_trace_lock);
396 	if (!tr->sys_refcount_enter)
397 		ret = register_trace_sys_enter(ftrace_syscall_enter, tr);
398 	if (!ret) {
399 		rcu_assign_pointer(tr->enter_syscall_files[num], file);
400 		tr->sys_refcount_enter++;
401 	}
402 	mutex_unlock(&syscall_trace_lock);
403 	return ret;
404 }
405 
406 static void unreg_event_syscall_enter(struct trace_event_file *file,
407 				      struct trace_event_call *call)
408 {
409 	struct trace_array *tr = file->tr;
410 	int num;
411 
412 	num = ((struct syscall_metadata *)call->data)->syscall_nr;
413 	if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
414 		return;
415 	mutex_lock(&syscall_trace_lock);
416 	tr->sys_refcount_enter--;
417 	RCU_INIT_POINTER(tr->enter_syscall_files[num], NULL);
418 	if (!tr->sys_refcount_enter)
419 		unregister_trace_sys_enter(ftrace_syscall_enter, tr);
420 	mutex_unlock(&syscall_trace_lock);
421 }
422 
423 static int reg_event_syscall_exit(struct trace_event_file *file,
424 				  struct trace_event_call *call)
425 {
426 	struct trace_array *tr = file->tr;
427 	int ret = 0;
428 	int num;
429 
430 	num = ((struct syscall_metadata *)call->data)->syscall_nr;
431 	if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
432 		return -ENOSYS;
433 	mutex_lock(&syscall_trace_lock);
434 	if (!tr->sys_refcount_exit)
435 		ret = register_trace_sys_exit(ftrace_syscall_exit, tr);
436 	if (!ret) {
437 		rcu_assign_pointer(tr->exit_syscall_files[num], file);
438 		tr->sys_refcount_exit++;
439 	}
440 	mutex_unlock(&syscall_trace_lock);
441 	return ret;
442 }
443 
444 static void unreg_event_syscall_exit(struct trace_event_file *file,
445 				     struct trace_event_call *call)
446 {
447 	struct trace_array *tr = file->tr;
448 	int num;
449 
450 	num = ((struct syscall_metadata *)call->data)->syscall_nr;
451 	if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
452 		return;
453 	mutex_lock(&syscall_trace_lock);
454 	tr->sys_refcount_exit--;
455 	RCU_INIT_POINTER(tr->exit_syscall_files[num], NULL);
456 	if (!tr->sys_refcount_exit)
457 		unregister_trace_sys_exit(ftrace_syscall_exit, tr);
458 	mutex_unlock(&syscall_trace_lock);
459 }
460 
461 static int __init init_syscall_trace(struct trace_event_call *call)
462 {
463 	int id;
464 	int num;
465 
466 	num = ((struct syscall_metadata *)call->data)->syscall_nr;
467 	if (num < 0 || num >= NR_syscalls) {
468 		pr_debug("syscall %s metadata not mapped, disabling ftrace event\n",
469 				((struct syscall_metadata *)call->data)->name);
470 		return -ENOSYS;
471 	}
472 
473 	if (set_syscall_print_fmt(call) < 0)
474 		return -ENOMEM;
475 
476 	id = trace_event_raw_init(call);
477 
478 	if (id < 0) {
479 		free_syscall_print_fmt(call);
480 		return id;
481 	}
482 
483 	return id;
484 }
485 
486 static struct trace_event_fields __refdata syscall_enter_fields_array[] = {
487 	SYSCALL_FIELD(int, __syscall_nr),
488 	{ .type = TRACE_FUNCTION_TYPE,
489 	  .define_fields = syscall_enter_define_fields },
490 	{}
491 };
492 
493 struct trace_event_functions enter_syscall_print_funcs = {
494 	.trace		= print_syscall_enter,
495 };
496 
497 struct trace_event_functions exit_syscall_print_funcs = {
498 	.trace		= print_syscall_exit,
499 };
500 
501 struct trace_event_class __refdata event_class_syscall_enter = {
502 	.system		= "syscalls",
503 	.reg		= syscall_enter_register,
504 	.fields_array	= syscall_enter_fields_array,
505 	.get_fields	= syscall_get_enter_fields,
506 	.raw_init	= init_syscall_trace,
507 };
508 
509 struct trace_event_class __refdata event_class_syscall_exit = {
510 	.system		= "syscalls",
511 	.reg		= syscall_exit_register,
512 	.fields_array	= (struct trace_event_fields[]){
513 		SYSCALL_FIELD(int, __syscall_nr),
514 		SYSCALL_FIELD(long, ret),
515 		{}
516 	},
517 	.fields		= LIST_HEAD_INIT(event_class_syscall_exit.fields),
518 	.raw_init	= init_syscall_trace,
519 };
520 
521 unsigned long __init __weak arch_syscall_addr(int nr)
522 {
523 	return (unsigned long)sys_call_table[nr];
524 }
525 
526 void __init init_ftrace_syscalls(void)
527 {
528 	struct syscall_metadata *meta;
529 	unsigned long addr;
530 	int i;
531 	void *ret;
532 
533 	if (!IS_ENABLED(CONFIG_HAVE_SPARSE_SYSCALL_NR)) {
534 		syscalls_metadata = kcalloc(NR_syscalls,
535 					sizeof(*syscalls_metadata),
536 					GFP_KERNEL);
537 		if (!syscalls_metadata) {
538 			WARN_ON(1);
539 			return;
540 		}
541 	}
542 
543 	for (i = 0; i < NR_syscalls; i++) {
544 		addr = arch_syscall_addr(i);
545 		meta = find_syscall_meta(addr);
546 		if (!meta)
547 			continue;
548 
549 		meta->syscall_nr = i;
550 
551 		if (!IS_ENABLED(CONFIG_HAVE_SPARSE_SYSCALL_NR)) {
552 			syscalls_metadata[i] = meta;
553 		} else {
554 			ret = xa_store(&syscalls_metadata_sparse, i, meta,
555 					GFP_KERNEL);
556 			WARN(xa_is_err(ret),
557 				"Syscall memory allocation failed\n");
558 		}
559 
560 	}
561 }
562 
563 #ifdef CONFIG_PERF_EVENTS
564 
565 static DECLARE_BITMAP(enabled_perf_enter_syscalls, NR_syscalls);
566 static DECLARE_BITMAP(enabled_perf_exit_syscalls, NR_syscalls);
567 static int sys_perf_refcount_enter;
568 static int sys_perf_refcount_exit;
569 
570 static int perf_call_bpf_enter(struct trace_event_call *call, struct pt_regs *regs,
571 			       struct syscall_metadata *sys_data,
572 			       struct syscall_trace_enter *rec)
573 {
574 	struct syscall_tp_t {
575 		unsigned long long regs;
576 		unsigned long syscall_nr;
577 		unsigned long args[SYSCALL_DEFINE_MAXARGS];
578 	} param;
579 	int i;
580 
581 	*(struct pt_regs **)&param = regs;
582 	param.syscall_nr = rec->nr;
583 	for (i = 0; i < sys_data->nb_args; i++)
584 		param.args[i] = rec->args[i];
585 	return trace_call_bpf(call, &param);
586 }
587 
588 static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id)
589 {
590 	struct syscall_metadata *sys_data;
591 	struct syscall_trace_enter *rec;
592 	struct hlist_head *head;
593 	unsigned long args[6];
594 	bool valid_prog_array;
595 	int syscall_nr;
596 	int rctx;
597 	int size;
598 
599 	syscall_nr = trace_get_syscall_nr(current, regs);
600 	if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
601 		return;
602 	if (!test_bit(syscall_nr, enabled_perf_enter_syscalls))
603 		return;
604 
605 	sys_data = syscall_nr_to_meta(syscall_nr);
606 	if (!sys_data)
607 		return;
608 
609 	head = this_cpu_ptr(sys_data->enter_event->perf_events);
610 	valid_prog_array = bpf_prog_array_valid(sys_data->enter_event);
611 	if (!valid_prog_array && hlist_empty(head))
612 		return;
613 
614 	/* get the size after alignment with the u32 buffer size field */
615 	size = sizeof(unsigned long) * sys_data->nb_args + sizeof(*rec);
616 	size = ALIGN(size + sizeof(u32), sizeof(u64));
617 	size -= sizeof(u32);
618 
619 	rec = perf_trace_buf_alloc(size, NULL, &rctx);
620 	if (!rec)
621 		return;
622 
623 	rec->nr = syscall_nr;
624 	syscall_get_arguments(current, regs, args);
625 	memcpy(&rec->args, args, sizeof(unsigned long) * sys_data->nb_args);
626 
627 	if ((valid_prog_array &&
628 	     !perf_call_bpf_enter(sys_data->enter_event, regs, sys_data, rec)) ||
629 	    hlist_empty(head)) {
630 		perf_swevent_put_recursion_context(rctx);
631 		return;
632 	}
633 
634 	perf_trace_buf_submit(rec, size, rctx,
635 			      sys_data->enter_event->event.type, 1, regs,
636 			      head, NULL);
637 }
638 
639 static int perf_sysenter_enable(struct trace_event_call *call)
640 {
641 	int ret = 0;
642 	int num;
643 
644 	num = ((struct syscall_metadata *)call->data)->syscall_nr;
645 
646 	mutex_lock(&syscall_trace_lock);
647 	if (!sys_perf_refcount_enter)
648 		ret = register_trace_sys_enter(perf_syscall_enter, NULL);
649 	if (ret) {
650 		pr_info("event trace: Could not activate syscall entry trace point");
651 	} else {
652 		set_bit(num, enabled_perf_enter_syscalls);
653 		sys_perf_refcount_enter++;
654 	}
655 	mutex_unlock(&syscall_trace_lock);
656 	return ret;
657 }
658 
659 static void perf_sysenter_disable(struct trace_event_call *call)
660 {
661 	int num;
662 
663 	num = ((struct syscall_metadata *)call->data)->syscall_nr;
664 
665 	mutex_lock(&syscall_trace_lock);
666 	sys_perf_refcount_enter--;
667 	clear_bit(num, enabled_perf_enter_syscalls);
668 	if (!sys_perf_refcount_enter)
669 		unregister_trace_sys_enter(perf_syscall_enter, NULL);
670 	mutex_unlock(&syscall_trace_lock);
671 }
672 
673 static int perf_call_bpf_exit(struct trace_event_call *call, struct pt_regs *regs,
674 			      struct syscall_trace_exit *rec)
675 {
676 	struct syscall_tp_t {
677 		unsigned long long regs;
678 		unsigned long syscall_nr;
679 		unsigned long ret;
680 	} param;
681 
682 	*(struct pt_regs **)&param = regs;
683 	param.syscall_nr = rec->nr;
684 	param.ret = rec->ret;
685 	return trace_call_bpf(call, &param);
686 }
687 
688 static void perf_syscall_exit(void *ignore, struct pt_regs *regs, long ret)
689 {
690 	struct syscall_metadata *sys_data;
691 	struct syscall_trace_exit *rec;
692 	struct hlist_head *head;
693 	bool valid_prog_array;
694 	int syscall_nr;
695 	int rctx;
696 	int size;
697 
698 	syscall_nr = trace_get_syscall_nr(current, regs);
699 	if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
700 		return;
701 	if (!test_bit(syscall_nr, enabled_perf_exit_syscalls))
702 		return;
703 
704 	sys_data = syscall_nr_to_meta(syscall_nr);
705 	if (!sys_data)
706 		return;
707 
708 	head = this_cpu_ptr(sys_data->exit_event->perf_events);
709 	valid_prog_array = bpf_prog_array_valid(sys_data->exit_event);
710 	if (!valid_prog_array && hlist_empty(head))
711 		return;
712 
713 	/* We can probably do that at build time */
714 	size = ALIGN(sizeof(*rec) + sizeof(u32), sizeof(u64));
715 	size -= sizeof(u32);
716 
717 	rec = perf_trace_buf_alloc(size, NULL, &rctx);
718 	if (!rec)
719 		return;
720 
721 	rec->nr = syscall_nr;
722 	rec->ret = syscall_get_return_value(current, regs);
723 
724 	if ((valid_prog_array &&
725 	     !perf_call_bpf_exit(sys_data->exit_event, regs, rec)) ||
726 	    hlist_empty(head)) {
727 		perf_swevent_put_recursion_context(rctx);
728 		return;
729 	}
730 
731 	perf_trace_buf_submit(rec, size, rctx, sys_data->exit_event->event.type,
732 			      1, regs, head, NULL);
733 }
734 
735 static int perf_sysexit_enable(struct trace_event_call *call)
736 {
737 	int ret = 0;
738 	int num;
739 
740 	num = ((struct syscall_metadata *)call->data)->syscall_nr;
741 
742 	mutex_lock(&syscall_trace_lock);
743 	if (!sys_perf_refcount_exit)
744 		ret = register_trace_sys_exit(perf_syscall_exit, NULL);
745 	if (ret) {
746 		pr_info("event trace: Could not activate syscall exit trace point");
747 	} else {
748 		set_bit(num, enabled_perf_exit_syscalls);
749 		sys_perf_refcount_exit++;
750 	}
751 	mutex_unlock(&syscall_trace_lock);
752 	return ret;
753 }
754 
755 static void perf_sysexit_disable(struct trace_event_call *call)
756 {
757 	int num;
758 
759 	num = ((struct syscall_metadata *)call->data)->syscall_nr;
760 
761 	mutex_lock(&syscall_trace_lock);
762 	sys_perf_refcount_exit--;
763 	clear_bit(num, enabled_perf_exit_syscalls);
764 	if (!sys_perf_refcount_exit)
765 		unregister_trace_sys_exit(perf_syscall_exit, NULL);
766 	mutex_unlock(&syscall_trace_lock);
767 }
768 
769 #endif /* CONFIG_PERF_EVENTS */
770 
771 static int syscall_enter_register(struct trace_event_call *event,
772 				 enum trace_reg type, void *data)
773 {
774 	struct trace_event_file *file = data;
775 
776 	switch (type) {
777 	case TRACE_REG_REGISTER:
778 		return reg_event_syscall_enter(file, event);
779 	case TRACE_REG_UNREGISTER:
780 		unreg_event_syscall_enter(file, event);
781 		return 0;
782 
783 #ifdef CONFIG_PERF_EVENTS
784 	case TRACE_REG_PERF_REGISTER:
785 		return perf_sysenter_enable(event);
786 	case TRACE_REG_PERF_UNREGISTER:
787 		perf_sysenter_disable(event);
788 		return 0;
789 	case TRACE_REG_PERF_OPEN:
790 	case TRACE_REG_PERF_CLOSE:
791 	case TRACE_REG_PERF_ADD:
792 	case TRACE_REG_PERF_DEL:
793 		return 0;
794 #endif
795 	}
796 	return 0;
797 }
798 
799 static int syscall_exit_register(struct trace_event_call *event,
800 				 enum trace_reg type, void *data)
801 {
802 	struct trace_event_file *file = data;
803 
804 	switch (type) {
805 	case TRACE_REG_REGISTER:
806 		return reg_event_syscall_exit(file, event);
807 	case TRACE_REG_UNREGISTER:
808 		unreg_event_syscall_exit(file, event);
809 		return 0;
810 
811 #ifdef CONFIG_PERF_EVENTS
812 	case TRACE_REG_PERF_REGISTER:
813 		return perf_sysexit_enable(event);
814 	case TRACE_REG_PERF_UNREGISTER:
815 		perf_sysexit_disable(event);
816 		return 0;
817 	case TRACE_REG_PERF_OPEN:
818 	case TRACE_REG_PERF_CLOSE:
819 	case TRACE_REG_PERF_ADD:
820 	case TRACE_REG_PERF_DEL:
821 		return 0;
822 #endif
823 	}
824 	return 0;
825 }
826