blob: f93a56d2db275be64df083344b68ec65f3c32473 [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>
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +010010#include <asm/syscall.h>
11
12#include "trace_output.h"
13#include "trace.h"
14
Frederic Weisbecker5be71b62009-03-15 22:10:37 +010015static DEFINE_MUTEX(syscall_trace_lock);
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +010016
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -040017static int syscall_enter_register(struct trace_event_call *event,
Jiri Olsaceec0b62012-02-15 15:51:49 +010018 enum trace_reg type, void *data);
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -040019static int syscall_exit_register(struct trace_event_call *event,
Jiri Olsaceec0b62012-02-15 15:51:49 +010020 enum trace_reg type, void *data);
Steven Rostedt22392912010-04-21 12:27:06 -040021
Steven Rostedt2e33af02010-04-22 10:35:55 -040022static struct list_head *
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -040023syscall_get_enter_fields(struct trace_event_call *call)
Steven Rostedt2e33af02010-04-22 10:35:55 -040024{
25 struct syscall_metadata *entry = call->data;
26
27 return &entry->enter_fields;
28}
29
Steven Rostedt3d56e332011-02-02 17:06:09 -050030extern struct syscall_metadata *__start_syscalls_metadata[];
31extern struct syscall_metadata *__stop_syscalls_metadata[];
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020032
33static struct syscall_metadata **syscalls_metadata;
34
Ian Munsieb2d55492011-02-03 14:27:23 +110035#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
zhangwei(Jovi)36a78e92013-03-11 15:13:51 +080041 * with ".SyS" or ".sys" instead of "sys", leading to an unwanted
Ian Munsieb2d55492011-02-03 14:27:23 +110042 * mismatch.
43 */
44 return !strcmp(sym + 3, name + 3);
45}
46#endif
47
Steven Rostedtf431b632013-02-12 16:18:59 -050048#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
Steven Rostedt3d56e332011-02-02 17:06:09 -050080static __init struct syscall_metadata *
81find_syscall_meta(unsigned long syscall)
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020082{
Steven Rostedt3d56e332011-02-02 17:06:09 -050083 struct syscall_metadata **start;
84 struct syscall_metadata **stop;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020085 char str[KSYM_SYMBOL_LEN];
86
87
Steven Rostedt3d56e332011-02-02 17:06:09 -050088 start = __start_syscalls_metadata;
89 stop = __stop_syscalls_metadata;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020090 kallsyms_lookup(syscall, NULL, NULL, NULL, str);
91
Ian Munsieae07f552011-02-03 14:27:25 +110092 if (arch_syscall_match_sym_name(str, "sys_ni_syscall"))
93 return NULL;
94
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020095 for ( ; start < stop; start++) {
Ian Munsieb2d55492011-02-03 14:27:23 +110096 if ((*start)->name && arch_syscall_match_sym_name(str, (*start)->name))
Steven Rostedt3d56e332011-02-02 17:06:09 -050097 return *start;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020098 }
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
Tom Zanussidbfeaa72015-12-10 12:50:46 -0600110const 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
Fengguang Wu6aea49c2012-11-21 15:13:47 +0800121static enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -0400122print_syscall_enter(struct trace_iterator *iter, int flags,
123 struct trace_event *event)
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100124{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400125 struct trace_array *tr = iter->tr;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100126 struct trace_seq *s = &iter->seq;
127 struct trace_entry *ent = iter->ent;
128 struct syscall_trace_enter *trace;
129 struct syscall_metadata *entry;
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500130 int i, syscall;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100131
Jason Baron64c12e02009-08-10 16:52:53 -0400132 trace = (typeof(trace))ent;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100133 syscall = trace->nr;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100134 entry = syscall_nr_to_meta(syscall);
Jason Baron64c12e02009-08-10 16:52:53 -0400135
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100136 if (!entry)
137 goto end;
138
Steven Rostedt32c0eda2010-04-23 10:38:03 -0400139 if (entry->enter_event->event.type != ent->type) {
Jason Baron64c12e02009-08-10 16:52:53 -0400140 WARN_ON_ONCE(1);
141 goto end;
142 }
143
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500144 trace_seq_printf(s, "%s(", entry->name);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100145
146 for (i = 0; i < entry->nb_args; i++) {
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500147
148 if (trace_seq_has_overflowed(s))
149 goto end;
150
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100151 /* parameter types */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400152 if (tr->trace_flags & TRACE_ITER_VERBOSE)
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500153 trace_seq_printf(s, "%s ", entry->types[i]);
154
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100155 /* parameter values */
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500156 trace_seq_printf(s, "%s: %lx%s", entry->args[i],
157 trace->args[i],
158 i == entry->nb_args - 1 ? "" : ", ");
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100159 }
160
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500161 trace_seq_putc(s, ')');
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100162end:
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500163 trace_seq_putc(s, '\n');
Li Zefan4539f072009-08-20 16:13:35 +0800164
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500165 return trace_handle_return(s);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100166}
167
Fengguang Wu6aea49c2012-11-21 15:13:47 +0800168static enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -0400169print_syscall_exit(struct trace_iterator *iter, int flags,
170 struct trace_event *event)
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100171{
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;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100177
Jason Baron64c12e02009-08-10 16:52:53 -0400178 trace = (typeof(trace))ent;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100179 syscall = trace->nr;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100180 entry = syscall_nr_to_meta(syscall);
Jason Baron64c12e02009-08-10 16:52:53 -0400181
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100182 if (!entry) {
zhangwei(Jovi)146c3442013-07-15 16:32:44 +0800183 trace_seq_putc(s, '\n');
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500184 goto out;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100185 }
186
Steven Rostedt32c0eda2010-04-23 10:38:03 -0400187 if (entry->exit_event->event.type != ent->type) {
Jason Baron64c12e02009-08-10 16:52:53 -0400188 WARN_ON_ONCE(1);
189 return TRACE_TYPE_UNHANDLED;
190 }
191
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500192 trace_seq_printf(s, "%s -> 0x%lx\n", entry->name,
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100193 trace->ret);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100194
Steven Rostedt (Red Hat)183742f2014-11-12 17:41:33 -0500195 out:
196 return trace_handle_return(s);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100197}
198
Li Zefane6971962009-08-19 15:52:25 +0800199extern char *__bad_type_size(void);
200
Taeung Song026842d2016-02-26 13:23:01 -0500201#define SYSCALL_FIELD(type, field, name) \
202 sizeof(type) != sizeof(trace.field) ? \
Li Zefane6971962009-08-19 15:52:25 +0800203 __bad_type_size() : \
Taeung Song026842d2016-02-26 13:23:01 -0500204 #type, #name, offsetof(typeof(trace), field), \
205 sizeof(trace.field), is_signed_type(type)
Li Zefane6971962009-08-19 15:52:25 +0800206
Li Zefan3ddc77f2013-03-27 14:15:37 +0800207static int __init
208__set_enter_print_fmt(struct syscall_metadata *entry, char *buf, int len)
Lai Jiangshan50307a42009-12-15 15:39:45 +0800209{
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
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400235static int __init set_syscall_print_fmt(struct trace_event_call *call)
Lai Jiangshan50307a42009-12-15 15:39:45 +0800236{
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
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400260static void __init free_syscall_print_fmt(struct trace_event_call *call)
Lai Jiangshan50307a42009-12-15 15:39:45 +0800261{
262 struct syscall_metadata *entry = call->data;
263
264 if (entry->enter_event == call)
265 kfree(call->print_fmt);
266}
267
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400268static int __init syscall_enter_define_fields(struct trace_event_call *call)
Li Zefan540b7b82009-08-19 15:54:51 +0800269{
270 struct syscall_trace_enter trace;
Lai Jiangshan31c16b12009-12-01 16:23:30 +0800271 struct syscall_metadata *meta = call->data;
Li Zefan540b7b82009-08-19 15:54:51 +0800272 int ret;
Li Zefan540b7b82009-08-19 15:54:51 +0800273 int i;
274 int offset = offsetof(typeof(trace), args);
275
Taeung Song026842d2016-02-26 13:23:01 -0500276 ret = trace_define_field(call, SYSCALL_FIELD(int, nr, __syscall_nr),
277 FILTER_OTHER);
Lai Jiangshan0f1ef512009-11-26 15:49:33 +0800278 if (ret)
279 return ret;
280
Li Zefan540b7b82009-08-19 15:54:51 +0800281 for (i = 0; i < meta->nb_args; i++) {
Frederic Weisbeckeraeaeae12009-08-27 05:09:51 +0200282 ret = trace_define_field(call, meta->types[i],
283 meta->args[i], offset,
Li Zefan43b51ea2009-08-07 10:33:22 +0800284 sizeof(unsigned long), 0,
285 FILTER_OTHER);
Li Zefan540b7b82009-08-19 15:54:51 +0800286 offset += sizeof(unsigned long);
287 }
288
289 return ret;
290}
291
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400292static int __init syscall_exit_define_fields(struct trace_event_call *call)
Li Zefan540b7b82009-08-19 15:54:51 +0800293{
294 struct syscall_trace_exit trace;
295 int ret;
296
Taeung Song026842d2016-02-26 13:23:01 -0500297 ret = trace_define_field(call, SYSCALL_FIELD(int, nr, __syscall_nr),
298 FILTER_OTHER);
Lai Jiangshan0f1ef512009-11-26 15:49:33 +0800299 if (ret)
300 return ret;
301
Taeung Song026842d2016-02-26 13:23:01 -0500302 ret = trace_define_field(call, SYSCALL_FIELD(long, ret, ret),
Li Zefan43b51ea2009-08-07 10:33:22 +0800303 FILTER_OTHER);
Li Zefan540b7b82009-08-19 15:54:51 +0800304
305 return ret;
306}
307
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400308static void ftrace_syscall_enter(void *data, struct pt_regs *regs, long id)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100309{
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400310 struct trace_array *tr = data;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400311 struct trace_event_file *trace_file;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100312 struct syscall_trace_enter *entry;
313 struct syscall_metadata *sys_data;
314 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -0400315 struct ring_buffer *buffer;
zhangwei(Jovi)11034ae2013-04-10 11:26:23 +0800316 unsigned long irq_flags;
317 int pc;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100318 int syscall_nr;
Steven Rostedtf431b632013-02-12 16:18:59 -0500319 int size;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100320
Steven Rostedtf431b632013-02-12 16:18:59 -0500321 syscall_nr = trace_get_syscall_nr(current, regs);
Rabin Vincent086ba772014-10-29 23:06:58 +0100322 if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
Hendrik Bruecknercd0980f2009-08-25 14:50:27 +0200323 return;
Tom Zanussid562aff2013-10-24 08:34:19 -0500324
325 /* Here we're inside tp handler's rcu_read_lock_sched (__DO_TRACE) */
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400326 trace_file = rcu_dereference_sched(tr->enter_syscall_files[syscall_nr]);
327 if (!trace_file)
Tom Zanussid562aff2013-10-24 08:34:19 -0500328 return;
329
Steven Rostedt (Red Hat)09a50592015-05-13 15:21:25 -0400330 if (trace_trigger_soft_disabled(trace_file))
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -0500331 return;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100332
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100333 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
zhangwei(Jovi)11034ae2013-04-10 11:26:23 +0800339 local_save_flags(irq_flags);
340 pc = preempt_count();
341
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500342 buffer = tr->trace_buffer.buffer;
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400343 event = trace_buffer_lock_reserve(buffer,
zhangwei(Jovi)11034ae2013-04-10 11:26:23 +0800344 sys_data->enter_event->event.type, size, irq_flags, pc);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100345 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
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400352 event_trigger_unlock_commit(trace_file, buffer, event, entry,
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -0500353 irq_flags, pc);
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100354}
355
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400356static void ftrace_syscall_exit(void *data, struct pt_regs *regs, long ret)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100357{
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400358 struct trace_array *tr = data;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400359 struct trace_event_file *trace_file;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100360 struct syscall_trace_exit *entry;
361 struct syscall_metadata *sys_data;
362 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -0400363 struct ring_buffer *buffer;
zhangwei(Jovi)11034ae2013-04-10 11:26:23 +0800364 unsigned long irq_flags;
365 int pc;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100366 int syscall_nr;
367
Steven Rostedtf431b632013-02-12 16:18:59 -0500368 syscall_nr = trace_get_syscall_nr(current, regs);
Rabin Vincent086ba772014-10-29 23:06:58 +0100369 if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
Hendrik Bruecknercd0980f2009-08-25 14:50:27 +0200370 return;
Tom Zanussid562aff2013-10-24 08:34:19 -0500371
372 /* Here we're inside tp handler's rcu_read_lock_sched (__DO_TRACE()) */
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400373 trace_file = rcu_dereference_sched(tr->exit_syscall_files[syscall_nr]);
374 if (!trace_file)
Tom Zanussid562aff2013-10-24 08:34:19 -0500375 return;
376
Steven Rostedt (Red Hat)09a50592015-05-13 15:21:25 -0400377 if (trace_trigger_soft_disabled(trace_file))
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -0500378 return;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100379
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100380 sys_data = syscall_nr_to_meta(syscall_nr);
381 if (!sys_data)
382 return;
383
zhangwei(Jovi)11034ae2013-04-10 11:26:23 +0800384 local_save_flags(irq_flags);
385 pc = preempt_count();
386
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500387 buffer = tr->trace_buffer.buffer;
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400388 event = trace_buffer_lock_reserve(buffer,
zhangwei(Jovi)11034ae2013-04-10 11:26:23 +0800389 sys_data->exit_event->event.type, sizeof(*entry),
390 irq_flags, pc);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100391 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
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400398 event_trigger_unlock_commit(trace_file, buffer, event, entry,
Steven Rostedt (Red Hat)13a1e4a2014-01-06 21:32:10 -0500399 irq_flags, pc);
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100400}
401
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400402static int reg_event_syscall_enter(struct trace_event_file *file,
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400403 struct trace_event_call *call)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100404{
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400405 struct trace_array *tr = file->tr;
Jason Baronfb34a082009-08-10 16:52:47 -0400406 int ret = 0;
407 int num;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100408
Lai Jiangshanc252f652009-12-01 16:23:47 +0800409 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Ian Munsie3773b3892011-02-03 14:27:21 +1100410 if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
Jason Baronfb34a082009-08-10 16:52:47 -0400411 return -ENOSYS;
412 mutex_lock(&syscall_trace_lock);
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400413 if (!tr->sys_refcount_enter)
414 ret = register_trace_sys_enter(ftrace_syscall_enter, tr);
Li Zefan3b8e4272009-12-08 11:14:52 +0800415 if (!ret) {
Tom Zanussid562aff2013-10-24 08:34:19 -0500416 rcu_assign_pointer(tr->enter_syscall_files[num], file);
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400417 tr->sys_refcount_enter++;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100418 }
Jason Baronfb34a082009-08-10 16:52:47 -0400419 mutex_unlock(&syscall_trace_lock);
420 return ret;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100421}
Jason Baronfb34a082009-08-10 16:52:47 -0400422
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400423static void unreg_event_syscall_enter(struct trace_event_file *file,
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400424 struct trace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400425{
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400426 struct trace_array *tr = file->tr;
Jason Baronfb34a082009-08-10 16:52:47 -0400427 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400428
Lai Jiangshanc252f652009-12-01 16:23:47 +0800429 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Ian Munsie3773b3892011-02-03 14:27:21 +1100430 if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
Jason Baronfb34a082009-08-10 16:52:47 -0400431 return;
432 mutex_lock(&syscall_trace_lock);
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400433 tr->sys_refcount_enter--;
Andreea-Cristina Bernatfb5a6132014-08-22 17:28:22 +0300434 RCU_INIT_POINTER(tr->enter_syscall_files[num], NULL);
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400435 if (!tr->sys_refcount_enter)
436 unregister_trace_sys_enter(ftrace_syscall_enter, tr);
Jason Baronfb34a082009-08-10 16:52:47 -0400437 mutex_unlock(&syscall_trace_lock);
438}
439
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400440static int reg_event_syscall_exit(struct trace_event_file *file,
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400441 struct trace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400442{
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400443 struct trace_array *tr = file->tr;
Jason Baronfb34a082009-08-10 16:52:47 -0400444 int ret = 0;
445 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400446
Lai Jiangshanc252f652009-12-01 16:23:47 +0800447 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Ian Munsie3773b3892011-02-03 14:27:21 +1100448 if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
Jason Baronfb34a082009-08-10 16:52:47 -0400449 return -ENOSYS;
450 mutex_lock(&syscall_trace_lock);
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400451 if (!tr->sys_refcount_exit)
452 ret = register_trace_sys_exit(ftrace_syscall_exit, tr);
Li Zefan3b8e4272009-12-08 11:14:52 +0800453 if (!ret) {
Tom Zanussid562aff2013-10-24 08:34:19 -0500454 rcu_assign_pointer(tr->exit_syscall_files[num], file);
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400455 tr->sys_refcount_exit++;
Jason Baronfb34a082009-08-10 16:52:47 -0400456 }
457 mutex_unlock(&syscall_trace_lock);
458 return ret;
459}
460
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400461static void unreg_event_syscall_exit(struct trace_event_file *file,
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400462 struct trace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400463{
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400464 struct trace_array *tr = file->tr;
Jason Baronfb34a082009-08-10 16:52:47 -0400465 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400466
Lai Jiangshanc252f652009-12-01 16:23:47 +0800467 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Ian Munsie3773b3892011-02-03 14:27:21 +1100468 if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls))
Jason Baronfb34a082009-08-10 16:52:47 -0400469 return;
470 mutex_lock(&syscall_trace_lock);
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400471 tr->sys_refcount_exit--;
Andreea-Cristina Bernatfb5a6132014-08-22 17:28:22 +0300472 RCU_INIT_POINTER(tr->exit_syscall_files[num], NULL);
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400473 if (!tr->sys_refcount_exit)
474 unregister_trace_sys_exit(ftrace_syscall_exit, tr);
Jason Baronfb34a082009-08-10 16:52:47 -0400475 mutex_unlock(&syscall_trace_lock);
476}
477
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400478static int __init init_syscall_trace(struct trace_event_call *call)
Lai Jiangshana1301da2009-12-01 16:23:55 +0800479{
480 int id;
Ian Munsieba976972011-02-03 14:27:20 +1100481 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 }
Lai Jiangshana1301da2009-12-01 16:23:55 +0800489
Lai Jiangshan50307a42009-12-15 15:39:45 +0800490 if (set_syscall_print_fmt(call) < 0)
491 return -ENOMEM;
492
Steven Rostedtc7ef3a92009-12-28 21:13:59 -0500493 id = trace_event_raw_init(call);
494
495 if (id < 0) {
Lai Jiangshan50307a42009-12-15 15:39:45 +0800496 free_syscall_print_fmt(call);
Steven Rostedtc7ef3a92009-12-28 21:13:59 -0500497 return id;
Lai Jiangshan50307a42009-12-15 15:39:45 +0800498 }
Steven Rostedtc7ef3a92009-12-28 21:13:59 -0500499
500 return id;
Lai Jiangshana1301da2009-12-01 16:23:55 +0800501}
502
Vaibhav Nagarnaik6f86ab92012-06-07 16:46:25 -0700503struct 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
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400511struct trace_event_class __refdata event_class_syscall_enter = {
Vaibhav Nagarnaik6f86ab92012-06-07 16:46:25 -0700512 .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
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400519struct trace_event_class __refdata event_class_syscall_exit = {
Vaibhav Nagarnaik6f86ab92012-06-07 16:46:25 -0700520 .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
Ian Munsiec763ba02011-02-03 14:27:22 +1100527unsigned long __init __weak arch_syscall_addr(int nr)
Mike Frysingere7b8e672010-01-26 04:40:03 -0500528{
529 return (unsigned long)sys_call_table[nr];
530}
531
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -0500532void __init init_ftrace_syscalls(void)
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200533{
534 struct syscall_metadata *meta;
535 unsigned long addr;
536 int i;
537
Thomas Meyer47b0edc2011-11-29 22:08:00 +0100538 syscalls_metadata = kcalloc(NR_syscalls, sizeof(*syscalls_metadata),
539 GFP_KERNEL);
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200540 if (!syscalls_metadata) {
541 WARN_ON(1);
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -0500542 return;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200543 }
544
545 for (i = 0; i < NR_syscalls; i++) {
546 addr = arch_syscall_addr(i);
547 meta = find_syscall_meta(addr);
Lai Jiangshanc252f652009-12-01 16:23:47 +0800548 if (!meta)
549 continue;
550
551 meta->syscall_nr = i;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200552 syscalls_metadata[i] = meta;
553 }
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200554}
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200555
Li Zefan07b139c2009-12-21 14:27:35 +0800556#ifdef CONFIG_PERF_EVENTS
Frederic Weisbecker19007a672009-08-11 20:22:53 +0200557
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100558static 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;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400562
Yonghong Songe87c6bc2017-10-23 23:53:08 -0700563static 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{
Yonghong Songcf5f5ce2017-08-04 16:00:09 -0700567 struct syscall_tp_t {
568 unsigned long long regs;
569 unsigned long syscall_nr;
Yonghong Song609320c2017-09-07 18:36:15 -0700570 unsigned long args[SYSCALL_DEFINE_MAXARGS];
Yonghong Songcf5f5ce2017-08-04 16:00:09 -0700571 } 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];
Yonghong Songe87c6bc2017-10-23 23:53:08 -0700578 return trace_call_bpf(call, &param);
Yonghong Songcf5f5ce2017-08-04 16:00:09 -0700579}
580
Steven Rostedt38516ab2010-04-20 17:04:50 -0400581static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400582{
583 struct syscall_metadata *sys_data;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200584 struct syscall_trace_enter *rec;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +0200585 struct hlist_head *head;
Yonghong Songe87c6bc2017-10-23 23:53:08 -0700586 bool valid_prog_array;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400587 int syscall_nr;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100588 int rctx;
Frederic Weisbecker19007a672009-08-11 20:22:53 +0200589 int size;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400590
Steven Rostedtf431b632013-02-12 16:18:59 -0500591 syscall_nr = trace_get_syscall_nr(current, regs);
Rabin Vincent086ba772014-10-29 23:06:58 +0100592 if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
Will Deacon60916a92012-08-16 18:14:14 +0100593 return;
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100594 if (!test_bit(syscall_nr, enabled_perf_enter_syscalls))
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400595 return;
596
597 sys_data = syscall_nr_to_meta(syscall_nr);
598 if (!sys_data)
599 return;
600
Oleg Nesterov421c7862013-06-17 19:02:07 +0200601 head = this_cpu_ptr(sys_data->enter_event->perf_events);
Yonghong Songe87c6bc2017-10-23 23:53:08 -0700602 valid_prog_array = bpf_prog_array_valid(sys_data->enter_event);
603 if (!valid_prog_array && hlist_empty(head))
Oleg Nesterov421c7862013-06-17 19:02:07 +0200604 return;
605
Frederic Weisbecker19007a672009-08-11 20:22:53 +0200606 /* 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
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -0700611 rec = perf_trace_buf_alloc(size, NULL, &rctx);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +0800612 if (!rec)
613 return;
Frederic Weisbecker19007a672009-08-11 20:22:53 +0200614
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200615 rec->nr = syscall_nr;
616 syscall_get_arguments(current, regs, 0, sys_data->nb_args,
617 (unsigned long *)&rec->args);
Yonghong Songcf5f5ce2017-08-04 16:00:09 -0700618
Yonghong Songe87c6bc2017-10-23 23:53:08 -0700619 if ((valid_prog_array &&
620 !perf_call_bpf_enter(sys_data->enter_event, regs, sys_data, rec)) ||
Yonghong Songcf5f5ce2017-08-04 16:00:09 -0700621 hlist_empty(head)) {
622 perf_swevent_put_recursion_context(rctx);
623 return;
624 }
625
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -0700626 perf_trace_buf_submit(rec, size, rctx,
627 sys_data->enter_event->event.type, 1, regs,
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +0200628 head, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400629}
630
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400631static int perf_sysenter_enable(struct trace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400632{
633 int ret = 0;
634 int num;
635
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800636 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400637
638 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100639 if (!sys_perf_refcount_enter)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400640 ret = register_trace_sys_enter(perf_syscall_enter, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400641 if (ret) {
Colin Ian Kingd282b9c2016-09-12 14:46:23 +0100642 pr_info("event trace: Could not activate syscall entry trace point");
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400643 } else {
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100644 set_bit(num, enabled_perf_enter_syscalls);
645 sys_perf_refcount_enter++;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400646 }
647 mutex_unlock(&syscall_trace_lock);
648 return ret;
649}
650
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400651static void perf_sysenter_disable(struct trace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400652{
653 int num;
654
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800655 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400656
657 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100658 sys_perf_refcount_enter--;
659 clear_bit(num, enabled_perf_enter_syscalls);
660 if (!sys_perf_refcount_enter)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400661 unregister_trace_sys_enter(perf_syscall_enter, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400662 mutex_unlock(&syscall_trace_lock);
663}
664
Yonghong Songe87c6bc2017-10-23 23:53:08 -0700665static int perf_call_bpf_exit(struct trace_event_call *call, struct pt_regs *regs,
666 struct syscall_trace_exit *rec)
667{
Yonghong Songcf5f5ce2017-08-04 16:00:09 -0700668 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;
Yonghong Songe87c6bc2017-10-23 23:53:08 -0700677 return trace_call_bpf(call, &param);
Yonghong Songcf5f5ce2017-08-04 16:00:09 -0700678}
679
Steven Rostedt38516ab2010-04-20 17:04:50 -0400680static void perf_syscall_exit(void *ignore, struct pt_regs *regs, long ret)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400681{
682 struct syscall_metadata *sys_data;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200683 struct syscall_trace_exit *rec;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +0200684 struct hlist_head *head;
Yonghong Songe87c6bc2017-10-23 23:53:08 -0700685 bool valid_prog_array;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400686 int syscall_nr;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100687 int rctx;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200688 int size;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400689
Steven Rostedtf431b632013-02-12 16:18:59 -0500690 syscall_nr = trace_get_syscall_nr(current, regs);
Rabin Vincent086ba772014-10-29 23:06:58 +0100691 if (syscall_nr < 0 || syscall_nr >= NR_syscalls)
Will Deacon60916a92012-08-16 18:14:14 +0100692 return;
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100693 if (!test_bit(syscall_nr, enabled_perf_exit_syscalls))
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400694 return;
695
696 sys_data = syscall_nr_to_meta(syscall_nr);
697 if (!sys_data)
698 return;
699
Oleg Nesterov421c7862013-06-17 19:02:07 +0200700 head = this_cpu_ptr(sys_data->exit_event->perf_events);
Yonghong Songe87c6bc2017-10-23 23:53:08 -0700701 valid_prog_array = bpf_prog_array_valid(sys_data->exit_event);
702 if (!valid_prog_array && hlist_empty(head))
Oleg Nesterov421c7862013-06-17 19:02:07 +0200703 return;
704
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200705 /* We can probably do that at build time */
706 size = ALIGN(sizeof(*rec) + sizeof(u32), sizeof(u64));
707 size -= sizeof(u32);
Frederic Weisbecker19007a672009-08-11 20:22:53 +0200708
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -0700709 rec = perf_trace_buf_alloc(size, NULL, &rctx);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +0800710 if (!rec)
711 return;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100712
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200713 rec->nr = syscall_nr;
714 rec->ret = syscall_get_return_value(current, regs);
Yonghong Songcf5f5ce2017-08-04 16:00:09 -0700715
Yonghong Songe87c6bc2017-10-23 23:53:08 -0700716 if ((valid_prog_array &&
717 !perf_call_bpf_exit(sys_data->exit_event, regs, rec)) ||
Yonghong Songcf5f5ce2017-08-04 16:00:09 -0700718 hlist_empty(head)) {
719 perf_swevent_put_recursion_context(rctx);
720 return;
721 }
722
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -0700723 perf_trace_buf_submit(rec, size, rctx, sys_data->exit_event->event.type,
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +0200724 1, regs, head, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400725}
726
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400727static int perf_sysexit_enable(struct trace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400728{
729 int ret = 0;
730 int num;
731
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800732 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400733
734 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100735 if (!sys_perf_refcount_exit)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400736 ret = register_trace_sys_exit(perf_syscall_exit, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400737 if (ret) {
Colin Ian Kingd282b9c2016-09-12 14:46:23 +0100738 pr_info("event trace: Could not activate syscall exit trace point");
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400739 } else {
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100740 set_bit(num, enabled_perf_exit_syscalls);
741 sys_perf_refcount_exit++;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400742 }
743 mutex_unlock(&syscall_trace_lock);
744 return ret;
745}
746
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400747static void perf_sysexit_disable(struct trace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400748{
749 int num;
750
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800751 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400752
753 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100754 sys_perf_refcount_exit--;
755 clear_bit(num, enabled_perf_exit_syscalls);
756 if (!sys_perf_refcount_exit)
Steven Rostedt38516ab2010-04-20 17:04:50 -0400757 unregister_trace_sys_exit(perf_syscall_exit, NULL);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400758 mutex_unlock(&syscall_trace_lock);
759}
760
Li Zefan07b139c2009-12-21 14:27:35 +0800761#endif /* CONFIG_PERF_EVENTS */
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400762
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400763static int syscall_enter_register(struct trace_event_call *event,
Jiri Olsaceec0b62012-02-15 15:51:49 +0100764 enum trace_reg type, void *data)
Steven Rostedt22392912010-04-21 12:27:06 -0400765{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400766 struct trace_event_file *file = data;
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400767
Steven Rostedt22392912010-04-21 12:27:06 -0400768 switch (type) {
769 case TRACE_REG_REGISTER:
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400770 return reg_event_syscall_enter(file, event);
Steven Rostedt22392912010-04-21 12:27:06 -0400771 case TRACE_REG_UNREGISTER:
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400772 unreg_event_syscall_enter(file, event);
Steven Rostedt22392912010-04-21 12:27:06 -0400773 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;
Jiri Olsaceec0b62012-02-15 15:51:49 +0100781 case TRACE_REG_PERF_OPEN:
782 case TRACE_REG_PERF_CLOSE:
Jiri Olsa489c75c2012-02-15 15:51:50 +0100783 case TRACE_REG_PERF_ADD:
784 case TRACE_REG_PERF_DEL:
Jiri Olsaceec0b62012-02-15 15:51:49 +0100785 return 0;
Steven Rostedt22392912010-04-21 12:27:06 -0400786#endif
787 }
788 return 0;
789}
790
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400791static int syscall_exit_register(struct trace_event_call *event,
Jiri Olsaceec0b62012-02-15 15:51:49 +0100792 enum trace_reg type, void *data)
Steven Rostedt22392912010-04-21 12:27:06 -0400793{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400794 struct trace_event_file *file = data;
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400795
Steven Rostedt22392912010-04-21 12:27:06 -0400796 switch (type) {
797 case TRACE_REG_REGISTER:
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400798 return reg_event_syscall_exit(file, event);
Steven Rostedt22392912010-04-21 12:27:06 -0400799 case TRACE_REG_UNREGISTER:
Steven Rostedt12ab74e2012-08-08 14:48:20 -0400800 unreg_event_syscall_exit(file, event);
Steven Rostedt22392912010-04-21 12:27:06 -0400801 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;
Jiri Olsaceec0b62012-02-15 15:51:49 +0100809 case TRACE_REG_PERF_OPEN:
810 case TRACE_REG_PERF_CLOSE:
Jiri Olsa489c75c2012-02-15 15:51:50 +0100811 case TRACE_REG_PERF_ADD:
812 case TRACE_REG_PERF_DEL:
Jiri Olsaceec0b62012-02-15 15:51:49 +0100813 return 0;
Steven Rostedt22392912010-04-21 12:27:06 -0400814#endif
815 }
816 return 0;
817}