blob: f755bde42fd07c1be41445f02b895a4e4828cf88 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Frederic Weisbecker47788c52009-04-08 20:40:59 +02002#include <trace/syscall.h>
Josh Stone1c569f02009-08-24 14:43:14 -07003#include <trace/events/syscalls.h>
Steven Rostedtf431b632013-02-12 16:18:59 -05004#include <linux/syscalls.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09005#include <linux/slab.h>
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +01006#include <linux/kernel.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -04007#include <linux/module.h> /* for MODULE_NAME_LEN via KSYM_SYMBOL_LEN */
Jason Baronfb34a082009-08-10 16:52:47 -04008#include <linux/ftrace.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009#include <linux/perf_event.h>
Hassan Naveed0e242202019-11-15 23:44:42 +000010#include <linux/xarray.h>
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +010011#include <asm/syscall.h>
12
13#include "trace_output.h"
14#include "trace.h"
15
Frederic Weisbecker5be71b62009-03-15 22:10:37 +010016static DEFINE_MUTEX(syscall_trace_lock);
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +010017
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -040018static int syscall_enter_register(struct trace_event_call *event,
Jiri Olsaceec0b62012-02-15 15:51:49 +010019 enum trace_reg type, void *data);
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -040020static int syscall_exit_register(struct trace_event_call *event,
Jiri Olsaceec0b62012-02-15 15:51:49 +010021 enum trace_reg type, void *data);
Steven Rostedt22392912010-04-21 12:27:06 -040022
Steven Rostedt2e33af02010-04-22 10:35:55 -040023static struct list_head *
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -040024syscall_get_enter_fields(struct trace_event_call *call)
Steven Rostedt2e33af02010-04-22 10:35:55 -040025{
26 struct syscall_metadata *entry = call->data;
27
28 return &entry->enter_fields;
29}
30
Steven Rostedt3d56e332011-02-02 17:06:09 -050031extern struct syscall_metadata *__start_syscalls_metadata[];
32extern struct syscall_metadata *__stop_syscalls_metadata[];
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020033
Hassan Naveed0e242202019-11-15 23:44:42 +000034static DEFINE_XARRAY(syscalls_metadata_sparse);
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020035static struct syscall_metadata **syscalls_metadata;
36
Ian Munsieb2d55492011-02-03 14:27:23 +110037#ifndef ARCH_HAS_SYSCALL_MATCH_SYM_NAME
38static 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
zhangwei(Jovi)36a78e92013-03-11 15:13:51 +080043 * with ".SyS" or ".sys" instead of "sys", leading to an unwanted
Ian Munsieb2d55492011-02-03 14:27:23 +110044 * mismatch.
45 */
46 return !strcmp(sym + 3, name + 3);
47}
48#endif
49
Steven Rostedtf431b632013-02-12 16:18:59 -050050#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 */
66static int
67trace_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
75static inline int
76trace_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
Steven Rostedt3d56e332011-02-02 17:06:09 -050082static __init struct syscall_metadata *
83find_syscall_meta(unsigned long syscall)
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020084{
Steven Rostedt3d56e332011-02-02 17:06:09 -050085 struct syscall_metadata **start;
86 struct syscall_metadata **stop;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020087 char str[KSYM_SYMBOL_LEN];
88
89
Steven Rostedt3d56e332011-02-02 17:06:09 -050090 start = __start_syscalls_metadata;
91 stop = __stop_syscalls_metadata;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020092 kallsyms_lookup(syscall, NULL, NULL, NULL, str);
93
Ian Munsieae07f552011-02-03 14:27:25 +110094 if (arch_syscall_match_sym_name(str, "sys_ni_syscall"))
95 return NULL;
96
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020097 for ( ; start < stop; start++) {
Ian Munsieb2d55492011-02-03 14:27:23 +110098 if ((*start)->name && arch_syscall_match_sym_name(str, (*start)->name))
Steven Rostedt3d56e332011-02-02 17:06:09 -050099 return *start;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200100 }
101 return NULL;
102}
103
104static struct syscall_metadata *syscall_nr_to_meta(int nr)
105{
Hassan Naveed0e242202019-11-15 23:44:42 +0000106 if (IS_ENABLED(CONFIG_HAVE_SPARSE_SYSCALL_NR))
107 return xa_load(&syscalls_metadata_sparse, (unsigned long)nr);
108
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200109 if (!syscalls_metadata || nr >= NR_syscalls || nr < 0)
110 return NULL;
111
112 return syscalls_metadata[nr];
113}
114
Tom Zanussidbfeaa72015-12-10 12:50:46 -0600115const 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
Fengguang Wu6aea49c2012-11-21 15:13:47 +0800126static enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -0400127print_syscall_enter(struct trace_iterator *iter, int flags,
128 struct trace_event *event)
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100129{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400130 struct trace_array *tr = iter->tr;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100131 struct trace_seq *s = &iter->seq;
132 struct trace_entry *ent = iter->ent;
133 struct syscall_trace_enter *trace;
134 struct syscall_metadata *entry;
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500135 int i, syscall;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100136
Jason Baron64c12e02009-08-10 16:52:53 -0400137 trace = (typeof(trace))ent;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100138 syscall = trace->nr;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100139 entry = syscall_nr_to_meta(syscall);
Jason Baron64c12e02009-08-10 16:52:53 -0400140
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100141 if (!entry)
142 goto end;
143
Steven Rostedt32c0eda2010-04-23 10:38:03 -0400144 if (entry->enter_event->event.type != ent->type) {
Jason Baron64c12e02009-08-10 16:52:53 -0400145 WARN_ON_ONCE(1);
146 goto end;
147 }
148
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500149 trace_seq_printf(s, "%s(", entry->name);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100150
151 for (i = 0; i < entry->nb_args; i++) {
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500152
153 if (trace_seq_has_overflowed(s))
154 goto end;
155
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100156 /* parameter types */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400157 if (tr->trace_flags & TRACE_ITER_VERBOSE)
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500158 trace_seq_printf(s, "%s ", entry->types[i]);
159
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100160 /* parameter values */
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500161 trace_seq_printf(s, "%s: %lx%s", entry->args[i],
162 trace->args[i],
163 i == entry->nb_args - 1 ? "" : ", ");
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100164 }
165
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500166 trace_seq_putc(s, ')');
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100167end:
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500168 trace_seq_putc(s, '\n');
Li Zefan4539f072009-08-20 16:13:35 +0800169
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500170 return trace_handle_return(s);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100171}
172
Fengguang Wu6aea49c2012-11-21 15:13:47 +0800173static enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -0400174print_syscall_exit(struct trace_iterator *iter, int flags,
175 struct trace_event *event)
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100176{
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;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100182
Jason Baron64c12e02009-08-10 16:52:53 -0400183 trace = (typeof(trace))ent;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100184 syscall = trace->nr;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100185 entry = syscall_nr_to_meta(syscall);
Jason Baron64c12e02009-08-10 16:52:53 -0400186
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100187 if (!entry) {
zhangwei(Jovi)146c3442013-07-15 16:32:44 +0800188 trace_seq_putc(s, '\n');
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500189 goto out;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100190 }
191
Steven Rostedt32c0eda2010-04-23 10:38:03 -0400192 if (entry->exit_event->event.type != ent->type) {
Jason Baron64c12e02009-08-10 16:52:53 -0400193 WARN_ON_ONCE(1);
194 return TRACE_TYPE_UNHANDLED;
195 }
196
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500197 trace_seq_printf(s, "%s -> 0x%lx\n", entry->name,
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100198 trace->ret);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100199
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500200 out:
201 return trace_handle_return(s);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100202}
203
Li Zefane6971962009-08-19 15:52:25 +0800204extern char *__bad_type_size(void);
205
Peter Zijlstra04ae87a2019-10-24 22:26:59 +0200206#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 }
Li Zefane6971962009-08-19 15:52:25 +0800210
Li Zefan3ddc77f2013-03-27 14:15:37 +0800211static int __init
212__set_enter_print_fmt(struct syscall_metadata *entry, char *buf, int len)
Lai Jiangshan50307a42009-12-15 15:39:45 +0800213{
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
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400239static int __init set_syscall_print_fmt(struct trace_event_call *call)
Lai Jiangshan50307a42009-12-15 15:39:45 +0800240{
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
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400264static void __init free_syscall_print_fmt(struct trace_event_call *call)
Lai Jiangshan50307a42009-12-15 15:39:45 +0800265{
266 struct syscall_metadata *entry = call->data;
267
268 if (entry->enter_event == call)
269 kfree(call->print_fmt);
270}
271
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400272static int __init syscall_enter_define_fields(struct trace_event_call *call)
Li Zefan540b7b82009-08-19 15:54:51 +0800273{
274 struct syscall_trace_enter trace;
Lai Jiangshan31c16b12009-12-01 16:23:30 +0800275 struct syscall_metadata *meta = call->data;
Li Zefan540b7b82009-08-19 15:54:51 +0800276 int offset = offsetof(typeof(trace), args);
Steven Rostedt (VMware)31537cf2020-01-08 08:57:55 -0500277 int ret = 0;
278 int i;
Lai Jiangshan0f1ef512009-11-26 15:49:33 +0800279
Li Zefan540b7b82009-08-19 15:54:51 +0800280 for (i = 0; i < meta->nb_args; i++) {
Frederic Weisbeckeraeaeae12009-08-27 05:09:51 +0200281 ret = trace_define_field(call, meta->types[i],
282 meta->args[i], offset,
Li Zefan43b51ea2009-08-07 10:33:22 +0800283 sizeof(unsigned long), 0,
284 FILTER_OTHER);
Peter Zijlstra04ae87a2019-10-24 22:26:59 +0200285 if (ret)
286 break;
Li Zefan540b7b82009-08-19 15:54:51 +0800287 offset += sizeof(unsigned long);
288 }
289
290 return ret;
291}
292
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400293static void ftrace_syscall_enter(void *data, struct pt_regs *regs, long id)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100294{
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400295 struct trace_array *tr = data;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400296 struct trace_event_file *trace_file;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100297 struct syscall_trace_enter *entry;
298 struct syscall_metadata *sys_data;
299 struct ring_buffer_event *event;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500300 struct trace_buffer *buffer;
Sebastian Andrzej Siewior36590c502021-01-25 20:45:08 +0100301 unsigned int trace_ctx;
Steven Rostedt (Red Hat)d08e4112016-11-07 16:26:36 -0500302 unsigned long args[6];
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100303 int syscall_nr;
Steven Rostedtf431b632013-02-12 16:18:59 -0500304 int size;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100305
Steven Rostedtf431b632013-02-12 16:18:59 -0500306 syscall_nr = trace_get_syscall_nr(current, regs);
Rabin Vincent086ba772014-10-29 23:06:58 +0100307 if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
Hendrik Bruecknercd0980f2009-08-25 14:50:27 +0200308 return;
Tom Zanussid562aff2013-10-24 08:34:19 -0500309
310 /* Here we're inside tp handler's rcu_read_lock_sched (__DO_TRACE) */
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400311 trace_file = rcu_dereference_sched(tr->enter_syscall_files[syscall_nr]);
312 if (!trace_file)
Tom Zanussid562aff2013-10-24 08:34:19 -0500313 return;
314
Steven Rostedt (Red Hat)09a50592015-05-13 15:21:25 -0400315 if (trace_trigger_soft_disabled(trace_file))
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -0500316 return;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100317
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100318 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
Sebastian Andrzej Siewior36590c502021-01-25 20:45:08 +0100324 trace_ctx = tracing_gen_ctx();
zhangwei(Jovi)11034ae2013-04-10 11:26:23 +0800325
Steven Rostedt3e2a56e2022-01-07 17:56:56 -0500326 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
Sebastian Andrzej Siewior36590c502021-01-25 20:45:08 +0100327 sys_data->enter_event->event.type, size, trace_ctx);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100328 if (!event)
329 return;
330
331 entry = ring_buffer_event_data(event);
332 entry->nr = syscall_nr;
Steven Rostedt (Red Hat)b35f5492016-11-07 16:26:37 -0500333 syscall_get_arguments(current, regs, args);
Steven Rostedt (Red Hat)d08e4112016-11-07 16:26:36 -0500334 memcpy(entry->args, args, sizeof(unsigned long) * sys_data->nb_args);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100335
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400336 event_trigger_unlock_commit(trace_file, buffer, event, entry,
Sebastian Andrzej Siewior36590c502021-01-25 20:45:08 +0100337 trace_ctx);
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100338}
339
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400340static void ftrace_syscall_exit(void *data, struct pt_regs *regs, long ret)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100341{
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400342 struct trace_array *tr = data;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400343 struct trace_event_file *trace_file;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100344 struct syscall_trace_exit *entry;
345 struct syscall_metadata *sys_data;
346 struct ring_buffer_event *event;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500347 struct trace_buffer *buffer;
Sebastian Andrzej Siewior36590c502021-01-25 20:45:08 +0100348 unsigned int trace_ctx;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100349 int syscall_nr;
350
Steven Rostedtf431b632013-02-12 16:18:59 -0500351 syscall_nr = trace_get_syscall_nr(current, regs);
Rabin Vincent086ba772014-10-29 23:06:58 +0100352 if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
Hendrik Bruecknercd0980f2009-08-25 14:50:27 +0200353 return;
Tom Zanussid562aff2013-10-24 08:34:19 -0500354
355 /* Here we're inside tp handler's rcu_read_lock_sched (__DO_TRACE()) */
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400356 trace_file = rcu_dereference_sched(tr->exit_syscall_files[syscall_nr]);
357 if (!trace_file)
Tom Zanussid562aff2013-10-24 08:34:19 -0500358 return;
359
Steven Rostedt (Red Hat)09a50592015-05-13 15:21:25 -0400360 if (trace_trigger_soft_disabled(trace_file))
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -0500361 return;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100362
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100363 sys_data = syscall_nr_to_meta(syscall_nr);
364 if (!sys_data)
365 return;
366
Sebastian Andrzej Siewior36590c502021-01-25 20:45:08 +0100367 trace_ctx = tracing_gen_ctx();
zhangwei(Jovi)11034ae2013-04-10 11:26:23 +0800368
Steven Rostedt3e2a56e2022-01-07 17:56:56 -0500369 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
zhangwei(Jovi)11034ae2013-04-10 11:26:23 +0800370 sys_data->exit_event->event.type, sizeof(*entry),
Sebastian Andrzej Siewior36590c502021-01-25 20:45:08 +0100371 trace_ctx);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100372 if (!event)
373 return;
374
375 entry = ring_buffer_event_data(event);
376 entry->nr = syscall_nr;
377 entry->ret = syscall_get_return_value(current, regs);
378
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400379 event_trigger_unlock_commit(trace_file, buffer, event, entry,
Sebastian Andrzej Siewior36590c502021-01-25 20:45:08 +0100380 trace_ctx);
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100381}
382
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400383static int reg_event_syscall_enter(struct trace_event_file *file,
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400384 struct trace_event_call *call)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100385{
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400386 struct trace_array *tr = file->tr;
Jason Baronfb34a082009-08-10 16:52:47 -0400387 int ret = 0;
388 int num;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100389
Lai Jiangshanc252f652009-12-01 16:23:47 +0800390 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Ian Munsie3773b3892011-02-03 14:27:21 +1100391 if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
Jason Baronfb34a082009-08-10 16:52:47 -0400392 return -ENOSYS;
393 mutex_lock(&syscall_trace_lock);
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400394 if (!tr->sys_refcount_enter)
395 ret = register_trace_sys_enter(ftrace_syscall_enter, tr);
Li Zefan3b8e4272009-12-08 11:14:52 +0800396 if (!ret) {
Tom Zanussid562aff2013-10-24 08:34:19 -0500397 rcu_assign_pointer(tr->enter_syscall_files[num], file);
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400398 tr->sys_refcount_enter++;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100399 }
Jason Baronfb34a082009-08-10 16:52:47 -0400400 mutex_unlock(&syscall_trace_lock);
401 return ret;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100402}
Jason Baronfb34a082009-08-10 16:52:47 -0400403
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400404static void unreg_event_syscall_enter(struct trace_event_file *file,
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400405 struct trace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400406{
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400407 struct trace_array *tr = file->tr;
Jason Baronfb34a082009-08-10 16:52:47 -0400408 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400409
Lai Jiangshanc252f652009-12-01 16:23:47 +0800410 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Ian Munsie3773b3892011-02-03 14:27:21 +1100411 if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
Jason Baronfb34a082009-08-10 16:52:47 -0400412 return;
413 mutex_lock(&syscall_trace_lock);
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400414 tr->sys_refcount_enter--;
Andreea-Cristina Bernatfb5a6132014-08-22 17:28:22 +0300415 RCU_INIT_POINTER(tr->enter_syscall_files[num], NULL);
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400416 if (!tr->sys_refcount_enter)
417 unregister_trace_sys_enter(ftrace_syscall_enter, tr);
Jason Baronfb34a082009-08-10 16:52:47 -0400418 mutex_unlock(&syscall_trace_lock);
419}
420
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400421static int reg_event_syscall_exit(struct trace_event_file *file,
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400422 struct trace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400423{
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400424 struct trace_array *tr = file->tr;
Jason Baronfb34a082009-08-10 16:52:47 -0400425 int ret = 0;
426 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400427
Lai Jiangshanc252f652009-12-01 16:23:47 +0800428 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Ian Munsie3773b3892011-02-03 14:27:21 +1100429 if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
Jason Baronfb34a082009-08-10 16:52:47 -0400430 return -ENOSYS;
431 mutex_lock(&syscall_trace_lock);
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400432 if (!tr->sys_refcount_exit)
433 ret = register_trace_sys_exit(ftrace_syscall_exit, tr);
Li Zefan3b8e4272009-12-08 11:14:52 +0800434 if (!ret) {
Tom Zanussid562aff2013-10-24 08:34:19 -0500435 rcu_assign_pointer(tr->exit_syscall_files[num], file);
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400436 tr->sys_refcount_exit++;
Jason Baronfb34a082009-08-10 16:52:47 -0400437 }
438 mutex_unlock(&syscall_trace_lock);
439 return ret;
440}
441
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400442static void unreg_event_syscall_exit(struct trace_event_file *file,
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400443 struct trace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400444{
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400445 struct trace_array *tr = file->tr;
Jason Baronfb34a082009-08-10 16:52:47 -0400446 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400447
Lai Jiangshanc252f652009-12-01 16:23:47 +0800448 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Ian Munsie3773b3892011-02-03 14:27:21 +1100449 if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
Jason Baronfb34a082009-08-10 16:52:47 -0400450 return;
451 mutex_lock(&syscall_trace_lock);
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400452 tr->sys_refcount_exit--;
Andreea-Cristina Bernatfb5a6132014-08-22 17:28:22 +0300453 RCU_INIT_POINTER(tr->exit_syscall_files[num], NULL);
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400454 if (!tr->sys_refcount_exit)
455 unregister_trace_sys_exit(ftrace_syscall_exit, tr);
Jason Baronfb34a082009-08-10 16:52:47 -0400456 mutex_unlock(&syscall_trace_lock);
457}
458
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400459static int __init init_syscall_trace(struct trace_event_call *call)
Lai Jiangshana1301da2009-12-01 16:23:55 +0800460{
461 int id;
Ian Munsieba976972011-02-03 14:27:20 +1100462 int num;
463
464 num = ((struct syscall_metadata *)call->data)->syscall_nr;
465 if (num < 0 || num >= NR_syscalls) {
466 pr_debug("syscall %s metadata not mapped, disabling ftrace event\n",
467 ((struct syscall_metadata *)call->data)->name);
468 return -ENOSYS;
469 }
Lai Jiangshana1301da2009-12-01 16:23:55 +0800470
Lai Jiangshan50307a42009-12-15 15:39:45 +0800471 if (set_syscall_print_fmt(call) < 0)
472 return -ENOMEM;
473
Steven Rostedtc7ef3a92009-12-28 21:13:59 -0500474 id = trace_event_raw_init(call);
475
476 if (id < 0) {
Lai Jiangshan50307a42009-12-15 15:39:45 +0800477 free_syscall_print_fmt(call);
Steven Rostedtc7ef3a92009-12-28 21:13:59 -0500478 return id;
Lai Jiangshan50307a42009-12-15 15:39:45 +0800479 }
Steven Rostedtc7ef3a92009-12-28 21:13:59 -0500480
481 return id;
Lai Jiangshana1301da2009-12-01 16:23:55 +0800482}
483
Peter Zijlstra04ae87a2019-10-24 22:26:59 +0200484static struct trace_event_fields __refdata syscall_enter_fields_array[] = {
485 SYSCALL_FIELD(int, __syscall_nr),
486 { .type = TRACE_FUNCTION_TYPE,
487 .define_fields = syscall_enter_define_fields },
488 {}
489};
490
Vaibhav Nagarnaik6f86ab92012-06-07 16:46:25 -0700491struct trace_event_functions enter_syscall_print_funcs = {
492 .trace = print_syscall_enter,
493};
494
495struct trace_event_functions exit_syscall_print_funcs = {
496 .trace = print_syscall_exit,
497};
498
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400499struct trace_event_class __refdata event_class_syscall_enter = {
Vaibhav Nagarnaik6f86ab92012-06-07 16:46:25 -0700500 .system = "syscalls",
501 .reg = syscall_enter_register,
Peter Zijlstra04ae87a2019-10-24 22:26:59 +0200502 .fields_array = syscall_enter_fields_array,
Vaibhav Nagarnaik6f86ab92012-06-07 16:46:25 -0700503 .get_fields = syscall_get_enter_fields,
504 .raw_init = init_syscall_trace,
505};
506
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400507struct trace_event_class __refdata event_class_syscall_exit = {
Vaibhav Nagarnaik6f86ab92012-06-07 16:46:25 -0700508 .system = "syscalls",
509 .reg = syscall_exit_register,
Peter Zijlstra04ae87a2019-10-24 22:26:59 +0200510 .fields_array = (struct trace_event_fields[]){
511 SYSCALL_FIELD(int, __syscall_nr),
512 SYSCALL_FIELD(long, ret),
513 {}
514 },
Vaibhav Nagarnaik6f86ab92012-06-07 16:46:25 -0700515 .fields = LIST_HEAD_INIT(event_class_syscall_exit.fields),
516 .raw_init = init_syscall_trace,
517};
518
Ian Munsiec763ba02011-02-03 14:27:22 +1100519unsigned long __init __weak arch_syscall_addr(int nr)
Mike Frysingere7b8e672010-01-26 04:40:03 -0500520{
521 return (unsigned long)sys_call_table[nr];
522}
523
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -0500524void __init init_ftrace_syscalls(void)
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200525{
526 struct syscall_metadata *meta;
527 unsigned long addr;
528 int i;
Hassan Naveed0e242202019-11-15 23:44:42 +0000529 void *ret;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200530
Hassan Naveed0e242202019-11-15 23:44:42 +0000531 if (!IS_ENABLED(CONFIG_HAVE_SPARSE_SYSCALL_NR)) {
532 syscalls_metadata = kcalloc(NR_syscalls,
533 sizeof(*syscalls_metadata),
534 GFP_KERNEL);
535 if (!syscalls_metadata) {
536 WARN_ON(1);
537 return;
538 }
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200539 }
540
541 for (i = 0; i < NR_syscalls; i++) {
542 addr = arch_syscall_addr(i);
543 meta = find_syscall_meta(addr);
Lai Jiangshanc252f652009-12-01 16:23:47 +0800544 if (!meta)
545 continue;
546
547 meta->syscall_nr = i;
Hassan Naveed0e242202019-11-15 23:44:42 +0000548
549 if (!IS_ENABLED(CONFIG_HAVE_SPARSE_SYSCALL_NR)) {
550 syscalls_metadata[i] = meta;
551 } else {
552 ret = xa_store(&syscalls_metadata_sparse, i, meta,
553 GFP_KERNEL);
554 WARN(xa_is_err(ret),
555 "Syscall memory allocation failed\n");
556 }
557
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200558 }
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200559}
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200560
Li Zefan07b139c2009-12-21 14:27:35 +0800561#ifdef CONFIG_PERF_EVENTS
Frederic Weisbecker19007a672009-08-11 20:22:53 +0200562
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100563static DECLARE_BITMAP(enabled_perf_enter_syscalls, NR_syscalls);
564static DECLARE_BITMAP(enabled_perf_exit_syscalls, NR_syscalls);
565static int sys_perf_refcount_enter;
566static int sys_perf_refcount_exit;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400567
Yonghong Songe87c6bc2017-10-23 23:53:08 -0700568static int perf_call_bpf_enter(struct trace_event_call *call, struct pt_regs *regs,
569 struct syscall_metadata *sys_data,
570 struct syscall_trace_enter *rec)
571{
Yonghong Songcf5f5ce2017-08-04 16:00:09 -0700572 struct syscall_tp_t {
573 unsigned long long regs;
574 unsigned long syscall_nr;
Yonghong Song609320c2017-09-07 18:36:15 -0700575 unsigned long args[SYSCALL_DEFINE_MAXARGS];
Yonghong Songcf5f5ce2017-08-04 16:00:09 -0700576 } param;
577 int i;
578
579 *(struct pt_regs **)&param = regs;
580 param.syscall_nr = rec->nr;
581 for (i = 0; i < sys_data->nb_args; i++)
582 param.args[i] = rec->args[i];
Yonghong Songe87c6bc2017-10-23 23:53:08 -0700583 return trace_call_bpf(call, &param);
Yonghong Songcf5f5ce2017-08-04 16:00:09 -0700584}
585
Steven Rostedt38516ab2010-04-20 17:04:50 -0400586static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400587{
588 struct syscall_metadata *sys_data;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200589 struct syscall_trace_enter *rec;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +0200590 struct hlist_head *head;
Steven Rostedt (Red Hat)d08e4112016-11-07 16:26:36 -0500591 unsigned long args[6];
Yonghong Songe87c6bc2017-10-23 23:53:08 -0700592 bool valid_prog_array;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400593 int syscall_nr;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100594 int rctx;
Frederic Weisbecker19007a672009-08-11 20:22:53 +0200595 int size;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400596
Steven Rostedtf431b632013-02-12 16:18:59 -0500597 syscall_nr = trace_get_syscall_nr(current, regs);
Rabin Vincent086ba772014-10-29 23:06:58 +0100598 if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
Will Deacon60916a92012-08-16 18:14:14 +0100599 return;
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100600 if (!test_bit(syscall_nr, enabled_perf_enter_syscalls))
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400601 return;
602
603 sys_data = syscall_nr_to_meta(syscall_nr);
604 if (!sys_data)
605 return;
606
Oleg Nesterov421c7862013-06-17 19:02:07 +0200607 head = this_cpu_ptr(sys_data->enter_event->perf_events);
Yonghong Songe87c6bc2017-10-23 23:53:08 -0700608 valid_prog_array = bpf_prog_array_valid(sys_data->enter_event);
609 if (!valid_prog_array && hlist_empty(head))
Oleg Nesterov421c7862013-06-17 19:02:07 +0200610 return;
611
Frederic Weisbecker19007a672009-08-11 20:22:53 +0200612 /* 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
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -0700617 rec = perf_trace_buf_alloc(size, NULL, &rctx);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +0800618 if (!rec)
619 return;
Frederic Weisbecker19007a672009-08-11 20:22:53 +0200620
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200621 rec->nr = syscall_nr;
Steven Rostedt (Red Hat)b35f5492016-11-07 16:26:37 -0500622 syscall_get_arguments(current, regs, args);
Steven Rostedt (Red Hat)d08e4112016-11-07 16:26:36 -0500623 memcpy(&rec->args, args, sizeof(unsigned long) * sys_data->nb_args);
Yonghong Songcf5f5ce2017-08-04 16:00:09 -0700624
Yonghong Songe87c6bc2017-10-23 23:53:08 -0700625 if ((valid_prog_array &&
626 !perf_call_bpf_enter(sys_data->enter_event, regs, sys_data, rec)) ||
Yonghong Songcf5f5ce2017-08-04 16:00:09 -0700627 hlist_empty(head)) {
628 perf_swevent_put_recursion_context(rctx);
629 return;
630 }
631
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -0700632 perf_trace_buf_submit(rec, size, rctx,
633 sys_data->enter_event->event.type, 1, regs,
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +0200634 head, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400635}
636
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400637static int perf_sysenter_enable(struct trace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400638{
639 int ret = 0;
640 int num;
641
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800642 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400643
644 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100645 if (!sys_perf_refcount_enter)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400646 ret = register_trace_sys_enter(perf_syscall_enter, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400647 if (ret) {
Colin Ian Kingd282b9c2016-09-12 14:46:23 +0100648 pr_info("event trace: Could not activate syscall entry trace point");
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400649 } else {
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100650 set_bit(num, enabled_perf_enter_syscalls);
651 sys_perf_refcount_enter++;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400652 }
653 mutex_unlock(&syscall_trace_lock);
654 return ret;
655}
656
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400657static void perf_sysenter_disable(struct trace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400658{
659 int num;
660
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800661 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400662
663 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100664 sys_perf_refcount_enter--;
665 clear_bit(num, enabled_perf_enter_syscalls);
666 if (!sys_perf_refcount_enter)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400667 unregister_trace_sys_enter(perf_syscall_enter, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400668 mutex_unlock(&syscall_trace_lock);
669}
670
Yonghong Songe87c6bc2017-10-23 23:53:08 -0700671static int perf_call_bpf_exit(struct trace_event_call *call, struct pt_regs *regs,
672 struct syscall_trace_exit *rec)
673{
Yonghong Songcf5f5ce2017-08-04 16:00:09 -0700674 struct syscall_tp_t {
675 unsigned long long regs;
676 unsigned long syscall_nr;
677 unsigned long ret;
678 } param;
679
680 *(struct pt_regs **)&param = regs;
681 param.syscall_nr = rec->nr;
682 param.ret = rec->ret;
Yonghong Songe87c6bc2017-10-23 23:53:08 -0700683 return trace_call_bpf(call, &param);
Yonghong Songcf5f5ce2017-08-04 16:00:09 -0700684}
685
Steven Rostedt38516ab2010-04-20 17:04:50 -0400686static void perf_syscall_exit(void *ignore, struct pt_regs *regs, long ret)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400687{
688 struct syscall_metadata *sys_data;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200689 struct syscall_trace_exit *rec;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +0200690 struct hlist_head *head;
Yonghong Songe87c6bc2017-10-23 23:53:08 -0700691 bool valid_prog_array;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400692 int syscall_nr;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100693 int rctx;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200694 int size;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400695
Steven Rostedtf431b632013-02-12 16:18:59 -0500696 syscall_nr = trace_get_syscall_nr(current, regs);
Rabin Vincent086ba772014-10-29 23:06:58 +0100697 if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
Will Deacon60916a92012-08-16 18:14:14 +0100698 return;
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100699 if (!test_bit(syscall_nr, enabled_perf_exit_syscalls))
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400700 return;
701
702 sys_data = syscall_nr_to_meta(syscall_nr);
703 if (!sys_data)
704 return;
705
Oleg Nesterov421c7862013-06-17 19:02:07 +0200706 head = this_cpu_ptr(sys_data->exit_event->perf_events);
Yonghong Songe87c6bc2017-10-23 23:53:08 -0700707 valid_prog_array = bpf_prog_array_valid(sys_data->exit_event);
708 if (!valid_prog_array && hlist_empty(head))
Oleg Nesterov421c7862013-06-17 19:02:07 +0200709 return;
710
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200711 /* We can probably do that at build time */
712 size = ALIGN(sizeof(*rec) + sizeof(u32), sizeof(u64));
713 size -= sizeof(u32);
Frederic Weisbecker19007a672009-08-11 20:22:53 +0200714
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -0700715 rec = perf_trace_buf_alloc(size, NULL, &rctx);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +0800716 if (!rec)
717 return;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100718
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200719 rec->nr = syscall_nr;
720 rec->ret = syscall_get_return_value(current, regs);
Yonghong Songcf5f5ce2017-08-04 16:00:09 -0700721
Yonghong Songe87c6bc2017-10-23 23:53:08 -0700722 if ((valid_prog_array &&
723 !perf_call_bpf_exit(sys_data->exit_event, regs, rec)) ||
Yonghong Songcf5f5ce2017-08-04 16:00:09 -0700724 hlist_empty(head)) {
725 perf_swevent_put_recursion_context(rctx);
726 return;
727 }
728
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -0700729 perf_trace_buf_submit(rec, size, rctx, sys_data->exit_event->event.type,
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +0200730 1, regs, head, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400731}
732
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400733static int perf_sysexit_enable(struct trace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400734{
735 int ret = 0;
736 int num;
737
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800738 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400739
740 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100741 if (!sys_perf_refcount_exit)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400742 ret = register_trace_sys_exit(perf_syscall_exit, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400743 if (ret) {
Colin Ian Kingd282b9c2016-09-12 14:46:23 +0100744 pr_info("event trace: Could not activate syscall exit trace point");
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400745 } else {
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100746 set_bit(num, enabled_perf_exit_syscalls);
747 sys_perf_refcount_exit++;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400748 }
749 mutex_unlock(&syscall_trace_lock);
750 return ret;
751}
752
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400753static void perf_sysexit_disable(struct trace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400754{
755 int num;
756
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800757 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400758
759 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100760 sys_perf_refcount_exit--;
761 clear_bit(num, enabled_perf_exit_syscalls);
762 if (!sys_perf_refcount_exit)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400763 unregister_trace_sys_exit(perf_syscall_exit, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400764 mutex_unlock(&syscall_trace_lock);
765}
766
Li Zefan07b139c2009-12-21 14:27:35 +0800767#endif /* CONFIG_PERF_EVENTS */
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400768
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400769static int syscall_enter_register(struct trace_event_call *event,
Jiri Olsaceec0b62012-02-15 15:51:49 +0100770 enum trace_reg type, void *data)
Steven Rostedt22392912010-04-21 12:27:06 -0400771{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400772 struct trace_event_file *file = data;
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400773
Steven Rostedt22392912010-04-21 12:27:06 -0400774 switch (type) {
775 case TRACE_REG_REGISTER:
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400776 return reg_event_syscall_enter(file, event);
Steven Rostedt22392912010-04-21 12:27:06 -0400777 case TRACE_REG_UNREGISTER:
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400778 unreg_event_syscall_enter(file, event);
Steven Rostedt22392912010-04-21 12:27:06 -0400779 return 0;
780
781#ifdef CONFIG_PERF_EVENTS
782 case TRACE_REG_PERF_REGISTER:
783 return perf_sysenter_enable(event);
784 case TRACE_REG_PERF_UNREGISTER:
785 perf_sysenter_disable(event);
786 return 0;
Jiri Olsaceec0b62012-02-15 15:51:49 +0100787 case TRACE_REG_PERF_OPEN:
788 case TRACE_REG_PERF_CLOSE:
Jiri Olsa489c75c2012-02-15 15:51:50 +0100789 case TRACE_REG_PERF_ADD:
790 case TRACE_REG_PERF_DEL:
Jiri Olsaceec0b62012-02-15 15:51:49 +0100791 return 0;
Steven Rostedt22392912010-04-21 12:27:06 -0400792#endif
793 }
794 return 0;
795}
796
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400797static int syscall_exit_register(struct trace_event_call *event,
Jiri Olsaceec0b62012-02-15 15:51:49 +0100798 enum trace_reg type, void *data)
Steven Rostedt22392912010-04-21 12:27:06 -0400799{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400800 struct trace_event_file *file = data;
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400801
Steven Rostedt22392912010-04-21 12:27:06 -0400802 switch (type) {
803 case TRACE_REG_REGISTER:
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400804 return reg_event_syscall_exit(file, event);
Steven Rostedt22392912010-04-21 12:27:06 -0400805 case TRACE_REG_UNREGISTER:
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400806 unreg_event_syscall_exit(file, event);
Steven Rostedt22392912010-04-21 12:27:06 -0400807 return 0;
808
809#ifdef CONFIG_PERF_EVENTS
810 case TRACE_REG_PERF_REGISTER:
811 return perf_sysexit_enable(event);
812 case TRACE_REG_PERF_UNREGISTER:
813 perf_sysexit_disable(event);
814 return 0;
Jiri Olsaceec0b62012-02-15 15:51:49 +0100815 case TRACE_REG_PERF_OPEN:
816 case TRACE_REG_PERF_CLOSE:
Jiri Olsa489c75c2012-02-15 15:51:50 +0100817 case TRACE_REG_PERF_ADD:
818 case TRACE_REG_PERF_DEL:
Jiri Olsaceec0b62012-02-15 15:51:49 +0100819 return 0;
Steven Rostedt22392912010-04-21 12:27:06 -0400820#endif
821 }
822 return 0;
823}