Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
47788c58 | 2 | #include <trace/syscall.h> |
1c569f02 | 3 | #include <trace/events/syscalls.h> |
f431b634 | 4 | #include <linux/syscalls.h> |
5a0e3ad6 | 5 | #include <linux/slab.h> |
ee08c6ec | 6 | #include <linux/kernel.h> |
56d82e00 | 7 | #include <linux/module.h> /* for MODULE_NAME_LEN via KSYM_SYMBOL_LEN */ |
fb34a08c | 8 | #include <linux/ftrace.h> |
cdd6c482 | 9 | #include <linux/perf_event.h> |
0e242208 | 10 | #include <linux/xarray.h> |
ee08c6ec FW |
11 | #include <asm/syscall.h> |
12 | ||
13 | #include "trace_output.h" | |
14 | #include "trace.h" | |
15 | ||
5be71b61 | 16 | static DEFINE_MUTEX(syscall_trace_lock); |
ee08c6ec | 17 | |
2425bcb9 | 18 | static int syscall_enter_register(struct trace_event_call *event, |
ceec0b6f | 19 | enum trace_reg type, void *data); |
2425bcb9 | 20 | static int syscall_exit_register(struct trace_event_call *event, |
ceec0b6f | 21 | enum trace_reg type, void *data); |
2239291a | 22 | |
2e33af02 | 23 | static struct list_head * |
2425bcb9 | 24 | syscall_get_enter_fields(struct trace_event_call *call) |
2e33af02 SR |
25 | { |
26 | struct syscall_metadata *entry = call->data; | |
27 | ||
28 | return &entry->enter_fields; | |
29 | } | |
30 | ||
3d56e331 SR |
31 | extern struct syscall_metadata *__start_syscalls_metadata[]; |
32 | extern struct syscall_metadata *__stop_syscalls_metadata[]; | |
c44fc770 | 33 | |
0e242208 | 34 | static DEFINE_XARRAY(syscalls_metadata_sparse); |
c44fc770 FW |
35 | static struct syscall_metadata **syscalls_metadata; |
36 | ||
b2d55496 IM |
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 | |
36a78e9e | 43 | * with ".SyS" or ".sys" instead of "sys", leading to an unwanted |
b2d55496 IM |
44 | * mismatch. |
45 | */ | |
46 | return !strcmp(sym + 3, name + 3); | |
47 | } | |
48 | #endif | |
49 | ||
f431b634 SR |
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 | ||
3d56e331 SR |
82 | static __init struct syscall_metadata * |
83 | find_syscall_meta(unsigned long syscall) | |
c44fc770 | 84 | { |
3d56e331 SR |
85 | struct syscall_metadata **start; |
86 | struct syscall_metadata **stop; | |
c44fc770 FW |
87 | char str[KSYM_SYMBOL_LEN]; |
88 | ||
89 | ||
3d56e331 SR |
90 | start = __start_syscalls_metadata; |
91 | stop = __stop_syscalls_metadata; | |
c44fc770 FW |
92 | kallsyms_lookup(syscall, NULL, NULL, NULL, str); |
93 | ||
ae07f551 IM |
94 | if (arch_syscall_match_sym_name(str, "sys_ni_syscall")) |
95 | return NULL; | |
96 | ||
c44fc770 | 97 | for ( ; start < stop; start++) { |
b2d55496 | 98 | if ((*start)->name && arch_syscall_match_sym_name(str, (*start)->name)) |
3d56e331 | 99 | return *start; |
c44fc770 FW |
100 | } |
101 | return NULL; | |
102 | } | |
103 | ||
104 | static struct syscall_metadata *syscall_nr_to_meta(int nr) | |
105 | { | |
0e242208 HN |
106 | if (IS_ENABLED(CONFIG_HAVE_SPARSE_SYSCALL_NR)) |
107 | return xa_load(&syscalls_metadata_sparse, (unsigned long)nr); | |
108 | ||
c44fc770 FW |
109 | if (!syscalls_metadata || nr >= NR_syscalls || nr < 0) |
110 | return NULL; | |
111 | ||
112 | return syscalls_metadata[nr]; | |
113 | } | |
114 | ||
dbfeaa7a TZ |
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 | ||
6aea49cb | 126 | static enum print_line_t |
a9a57763 SR |
127 | print_syscall_enter(struct trace_iterator *iter, int flags, |
128 | struct trace_event *event) | |
bed1ffca | 129 | { |
983f938a | 130 | struct trace_array *tr = iter->tr; |
bed1ffca FW |
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; | |
183742f0 | 135 | int i, syscall; |
bed1ffca | 136 | |
64c12e04 | 137 | trace = (typeof(trace))ent; |
bed1ffca | 138 | syscall = trace->nr; |
bed1ffca | 139 | entry = syscall_nr_to_meta(syscall); |
64c12e04 | 140 | |
bed1ffca FW |
141 | if (!entry) |
142 | goto end; | |
143 | ||
32c0edae | 144 | if (entry->enter_event->event.type != ent->type) { |
64c12e04 JB |
145 | WARN_ON_ONCE(1); |
146 | goto end; | |
147 | } | |
148 | ||
183742f0 | 149 | trace_seq_printf(s, "%s(", entry->name); |
bed1ffca FW |
150 | |
151 | for (i = 0; i < entry->nb_args; i++) { | |
183742f0 SRRH |
152 | |
153 | if (trace_seq_has_overflowed(s)) | |
154 | goto end; | |
155 | ||
bed1ffca | 156 | /* parameter types */ |
983f938a | 157 | if (tr->trace_flags & TRACE_ITER_VERBOSE) |
183742f0 SRRH |
158 | trace_seq_printf(s, "%s ", entry->types[i]); |
159 | ||
bed1ffca | 160 | /* parameter values */ |
183742f0 SRRH |
161 | trace_seq_printf(s, "%s: %lx%s", entry->args[i], |
162 | trace->args[i], | |
163 | i == entry->nb_args - 1 ? "" : ", "); | |
bed1ffca FW |
164 | } |
165 | ||
183742f0 | 166 | trace_seq_putc(s, ')'); |
bed1ffca | 167 | end: |
183742f0 | 168 | trace_seq_putc(s, '\n'); |
4539f077 | 169 | |
183742f0 | 170 | return trace_handle_return(s); |
bed1ffca FW |
171 | } |
172 | ||
6aea49cb | 173 | static enum print_line_t |
a9a57763 SR |
174 | print_syscall_exit(struct trace_iterator *iter, int flags, |
175 | struct trace_event *event) | |
bed1ffca FW |
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; | |
bed1ffca | 182 | |
64c12e04 | 183 | trace = (typeof(trace))ent; |
bed1ffca | 184 | syscall = trace->nr; |
bed1ffca | 185 | entry = syscall_nr_to_meta(syscall); |
64c12e04 | 186 | |
bed1ffca | 187 | if (!entry) { |
146c3442 | 188 | trace_seq_putc(s, '\n'); |
183742f0 | 189 | goto out; |
bed1ffca FW |
190 | } |
191 | ||
32c0edae | 192 | if (entry->exit_event->event.type != ent->type) { |
64c12e04 JB |
193 | WARN_ON_ONCE(1); |
194 | return TRACE_TYPE_UNHANDLED; | |
195 | } | |
196 | ||
183742f0 | 197 | trace_seq_printf(s, "%s -> 0x%lx\n", entry->name, |
bed1ffca | 198 | trace->ret); |
bed1ffca | 199 | |
183742f0 SRRH |
200 | out: |
201 | return trace_handle_return(s); | |
bed1ffca FW |
202 | } |
203 | ||
e6971969 LZ |
204 | extern char *__bad_type_size(void); |
205 | ||
04ae87a5 PZ |
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 } | |
e6971969 | 210 | |
3ddc77f6 LZ |
211 | static int __init |
212 | __set_enter_print_fmt(struct syscall_metadata *entry, char *buf, int len) | |
50307a45 LJ |
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 | ||
2425bcb9 | 239 | static int __init set_syscall_print_fmt(struct trace_event_call *call) |
50307a45 LJ |
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 | ||
2425bcb9 | 264 | static void __init free_syscall_print_fmt(struct trace_event_call *call) |
50307a45 LJ |
265 | { |
266 | struct syscall_metadata *entry = call->data; | |
267 | ||
268 | if (entry->enter_event == call) | |
269 | kfree(call->print_fmt); | |
270 | } | |
271 | ||
2425bcb9 | 272 | static int __init syscall_enter_define_fields(struct trace_event_call *call) |
540b7b8d LZ |
273 | { |
274 | struct syscall_trace_enter trace; | |
31c16b13 | 275 | struct syscall_metadata *meta = call->data; |
540b7b8d | 276 | int offset = offsetof(typeof(trace), args); |
31537cf8 SRV |
277 | int ret = 0; |
278 | int i; | |
0f1ef51d | 279 | |
540b7b8d | 280 | for (i = 0; i < meta->nb_args; i++) { |
aeaeae11 FW |
281 | ret = trace_define_field(call, meta->types[i], |
282 | meta->args[i], offset, | |
43b51ead LZ |
283 | sizeof(unsigned long), 0, |
284 | FILTER_OTHER); | |
04ae87a5 PZ |
285 | if (ret) |
286 | break; | |
540b7b8d LZ |
287 | offset += sizeof(unsigned long); |
288 | } | |
289 | ||
290 | return ret; | |
291 | } | |
292 | ||
12ab74ee | 293 | static void ftrace_syscall_enter(void *data, struct pt_regs *regs, long id) |
ee08c6ec | 294 | { |
12ab74ee | 295 | struct trace_array *tr = data; |
7f1d2f82 | 296 | struct trace_event_file *trace_file; |
bed1ffca FW |
297 | struct syscall_trace_enter *entry; |
298 | struct syscall_metadata *sys_data; | |
299 | struct ring_buffer_event *event; | |
13292494 | 300 | struct trace_buffer *buffer; |
36590c50 | 301 | unsigned int trace_ctx; |
d08e4113 | 302 | unsigned long args[6]; |
ee08c6ec | 303 | int syscall_nr; |
f431b634 | 304 | int size; |
ee08c6ec | 305 | |
f431b634 | 306 | syscall_nr = trace_get_syscall_nr(current, regs); |
086ba77a | 307 | if (syscall_nr < 0 || syscall_nr >= NR_syscalls) |
cd0980fc | 308 | return; |
d562aff9 TZ |
309 | |
310 | /* Here we're inside tp handler's rcu_read_lock_sched (__DO_TRACE) */ | |
7f1d2f82 SRRH |
311 | trace_file = rcu_dereference_sched(tr->enter_syscall_files[syscall_nr]); |
312 | if (!trace_file) | |
d562aff9 TZ |
313 | return; |
314 | ||
09a5059a | 315 | if (trace_trigger_soft_disabled(trace_file)) |
13a1e4ae | 316 | return; |
ee08c6ec | 317 | |
bed1ffca FW |
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 | ||
36590c50 | 324 | trace_ctx = tracing_gen_ctx(); |
11034ae9 | 325 | |
3e2a56e6 | 326 | event = trace_event_buffer_lock_reserve(&buffer, trace_file, |
36590c50 | 327 | sys_data->enter_event->event.type, size, trace_ctx); |
bed1ffca FW |
328 | if (!event) |
329 | return; | |
330 | ||
331 | entry = ring_buffer_event_data(event); | |
332 | entry->nr = syscall_nr; | |
b35f549d | 333 | syscall_get_arguments(current, regs, args); |
d08e4113 | 334 | memcpy(entry->args, args, sizeof(unsigned long) * sys_data->nb_args); |
bed1ffca | 335 | |
7f1d2f82 | 336 | event_trigger_unlock_commit(trace_file, buffer, event, entry, |
36590c50 | 337 | trace_ctx); |
ee08c6ec FW |
338 | } |
339 | ||
12ab74ee | 340 | static void ftrace_syscall_exit(void *data, struct pt_regs *regs, long ret) |
ee08c6ec | 341 | { |
12ab74ee | 342 | struct trace_array *tr = data; |
7f1d2f82 | 343 | struct trace_event_file *trace_file; |
bed1ffca FW |
344 | struct syscall_trace_exit *entry; |
345 | struct syscall_metadata *sys_data; | |
346 | struct ring_buffer_event *event; | |
13292494 | 347 | struct trace_buffer *buffer; |
36590c50 | 348 | unsigned int trace_ctx; |
ee08c6ec FW |
349 | int syscall_nr; |
350 | ||
f431b634 | 351 | syscall_nr = trace_get_syscall_nr(current, regs); |
086ba77a | 352 | if (syscall_nr < 0 || syscall_nr >= NR_syscalls) |
cd0980fc | 353 | return; |
d562aff9 TZ |
354 | |
355 | /* Here we're inside tp handler's rcu_read_lock_sched (__DO_TRACE()) */ | |
7f1d2f82 SRRH |
356 | trace_file = rcu_dereference_sched(tr->exit_syscall_files[syscall_nr]); |
357 | if (!trace_file) | |
d562aff9 TZ |
358 | return; |
359 | ||
09a5059a | 360 | if (trace_trigger_soft_disabled(trace_file)) |
13a1e4ae | 361 | return; |
ee08c6ec | 362 | |
bed1ffca FW |
363 | sys_data = syscall_nr_to_meta(syscall_nr); |
364 | if (!sys_data) | |
365 | return; | |
366 | ||
36590c50 | 367 | trace_ctx = tracing_gen_ctx(); |
11034ae9 | 368 | |
3e2a56e6 | 369 | event = trace_event_buffer_lock_reserve(&buffer, trace_file, |
11034ae9 | 370 | sys_data->exit_event->event.type, sizeof(*entry), |
36590c50 | 371 | trace_ctx); |
bed1ffca FW |
372 | 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 | ||
7f1d2f82 | 379 | event_trigger_unlock_commit(trace_file, buffer, event, entry, |
36590c50 | 380 | trace_ctx); |
ee08c6ec FW |
381 | } |
382 | ||
7f1d2f82 | 383 | static int reg_event_syscall_enter(struct trace_event_file *file, |
2425bcb9 | 384 | struct trace_event_call *call) |
ee08c6ec | 385 | { |
12ab74ee | 386 | struct trace_array *tr = file->tr; |
fb34a08c JB |
387 | int ret = 0; |
388 | int num; | |
fb34a08c | 389 | |
c252f657 | 390 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
3773b389 | 391 | if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls)) |
fb34a08c JB |
392 | return -ENOSYS; |
393 | mutex_lock(&syscall_trace_lock); | |
12ab74ee SR |
394 | if (!tr->sys_refcount_enter) |
395 | ret = register_trace_sys_enter(ftrace_syscall_enter, tr); | |
3b8e4273 | 396 | if (!ret) { |
d562aff9 | 397 | rcu_assign_pointer(tr->enter_syscall_files[num], file); |
12ab74ee | 398 | tr->sys_refcount_enter++; |
fb34a08c JB |
399 | } |
400 | mutex_unlock(&syscall_trace_lock); | |
401 | return ret; | |
ee08c6ec FW |
402 | } |
403 | ||
7f1d2f82 | 404 | static void unreg_event_syscall_enter(struct trace_event_file *file, |
2425bcb9 | 405 | struct trace_event_call *call) |
ee08c6ec | 406 | { |
12ab74ee | 407 | struct trace_array *tr = file->tr; |
fb34a08c | 408 | int num; |
ee08c6ec | 409 | |
c252f657 | 410 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
3773b389 | 411 | if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls)) |
fb34a08c JB |
412 | return; |
413 | mutex_lock(&syscall_trace_lock); | |
12ab74ee | 414 | tr->sys_refcount_enter--; |
fb5a613b | 415 | RCU_INIT_POINTER(tr->enter_syscall_files[num], NULL); |
12ab74ee SR |
416 | if (!tr->sys_refcount_enter) |
417 | unregister_trace_sys_enter(ftrace_syscall_enter, tr); | |
fb34a08c JB |
418 | mutex_unlock(&syscall_trace_lock); |
419 | } | |
ee08c6ec | 420 | |
7f1d2f82 | 421 | static int reg_event_syscall_exit(struct trace_event_file *file, |
2425bcb9 | 422 | struct trace_event_call *call) |
ee08c6ec | 423 | { |
12ab74ee | 424 | struct trace_array *tr = file->tr; |
fb34a08c JB |
425 | int ret = 0; |
426 | int num; | |
fb34a08c | 427 | |
c252f657 | 428 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
3773b389 | 429 | if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls)) |
fb34a08c JB |
430 | return -ENOSYS; |
431 | mutex_lock(&syscall_trace_lock); | |
12ab74ee SR |
432 | if (!tr->sys_refcount_exit) |
433 | ret = register_trace_sys_exit(ftrace_syscall_exit, tr); | |
3b8e4273 | 434 | if (!ret) { |
d562aff9 | 435 | rcu_assign_pointer(tr->exit_syscall_files[num], file); |
12ab74ee | 436 | tr->sys_refcount_exit++; |
ee08c6ec | 437 | } |
fb34a08c JB |
438 | mutex_unlock(&syscall_trace_lock); |
439 | return ret; | |
440 | } | |
ee08c6ec | 441 | |
7f1d2f82 | 442 | static void unreg_event_syscall_exit(struct trace_event_file *file, |
2425bcb9 | 443 | struct trace_event_call *call) |
fb34a08c | 444 | { |
12ab74ee | 445 | struct trace_array *tr = file->tr; |
fb34a08c | 446 | int num; |
ee08c6ec | 447 | |
c252f657 | 448 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
3773b389 | 449 | if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls)) |
fb34a08c JB |
450 | return; |
451 | mutex_lock(&syscall_trace_lock); | |
12ab74ee | 452 | tr->sys_refcount_exit--; |
fb5a613b | 453 | RCU_INIT_POINTER(tr->exit_syscall_files[num], NULL); |
12ab74ee SR |
454 | if (!tr->sys_refcount_exit) |
455 | unregister_trace_sys_exit(ftrace_syscall_exit, tr); | |
fb34a08c | 456 | mutex_unlock(&syscall_trace_lock); |
ee08c6ec | 457 | } |
fb34a08c | 458 | |
2425bcb9 | 459 | static int __init init_syscall_trace(struct trace_event_call *call) |
a1301da0 LJ |
460 | { |
461 | int id; | |
ba976970 IM |
462 | 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 | } | |
a1301da0 | 470 | |
50307a45 LJ |
471 | if (set_syscall_print_fmt(call) < 0) |
472 | return -ENOMEM; | |
473 | ||
c7ef3a90 SR |
474 | id = trace_event_raw_init(call); |
475 | ||
476 | if (id < 0) { | |
50307a45 | 477 | free_syscall_print_fmt(call); |
c7ef3a90 | 478 | return id; |
50307a45 | 479 | } |
c7ef3a90 SR |
480 | |
481 | return id; | |
a1301da0 LJ |
482 | } |
483 | ||
04ae87a5 PZ |
484 | static 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 | ||
6f86ab9f VN |
491 | struct trace_event_functions enter_syscall_print_funcs = { |
492 | .trace = print_syscall_enter, | |
493 | }; | |
494 | ||
495 | struct trace_event_functions exit_syscall_print_funcs = { | |
496 | .trace = print_syscall_exit, | |
497 | }; | |
498 | ||
2425bcb9 | 499 | struct trace_event_class __refdata event_class_syscall_enter = { |
6f86ab9f VN |
500 | .system = "syscalls", |
501 | .reg = syscall_enter_register, | |
04ae87a5 | 502 | .fields_array = syscall_enter_fields_array, |
6f86ab9f VN |
503 | .get_fields = syscall_get_enter_fields, |
504 | .raw_init = init_syscall_trace, | |
505 | }; | |
506 | ||
2425bcb9 | 507 | struct trace_event_class __refdata event_class_syscall_exit = { |
6f86ab9f VN |
508 | .system = "syscalls", |
509 | .reg = syscall_exit_register, | |
04ae87a5 PZ |
510 | .fields_array = (struct trace_event_fields[]){ |
511 | SYSCALL_FIELD(int, __syscall_nr), | |
512 | SYSCALL_FIELD(long, ret), | |
513 | {} | |
514 | }, | |
6f86ab9f VN |
515 | .fields = LIST_HEAD_INIT(event_class_syscall_exit.fields), |
516 | .raw_init = init_syscall_trace, | |
517 | }; | |
518 | ||
c763ba06 | 519 | unsigned long __init __weak arch_syscall_addr(int nr) |
e7b8e675 MF |
520 | { |
521 | return (unsigned long)sys_call_table[nr]; | |
522 | } | |
523 | ||
5f893b26 | 524 | void __init init_ftrace_syscalls(void) |
c44fc770 FW |
525 | { |
526 | struct syscall_metadata *meta; | |
527 | unsigned long addr; | |
528 | int i; | |
0e242208 HN |
529 | void *ret; |
530 | ||
531 | 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 | } | |
c44fc770 FW |
539 | } |
540 | ||
541 | for (i = 0; i < NR_syscalls; i++) { | |
542 | addr = arch_syscall_addr(i); | |
543 | meta = find_syscall_meta(addr); | |
c252f657 LJ |
544 | if (!meta) |
545 | continue; | |
546 | ||
547 | meta->syscall_nr = i; | |
0e242208 HN |
548 | |
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 | ||
c44fc770 | 558 | } |
c44fc770 | 559 | } |
c44fc770 | 560 | |
07b139c8 | 561 | #ifdef CONFIG_PERF_EVENTS |
19007a67 | 562 | |
97d5a220 FW |
563 | static DECLARE_BITMAP(enabled_perf_enter_syscalls, NR_syscalls); |
564 | static DECLARE_BITMAP(enabled_perf_exit_syscalls, NR_syscalls); | |
565 | static int sys_perf_refcount_enter; | |
566 | static int sys_perf_refcount_exit; | |
f4b5ffcc | 567 | |
e87c6bc3 YS |
568 | static 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 | { | |
cf5f5cea YS |
572 | struct syscall_tp_t { |
573 | unsigned long long regs; | |
574 | unsigned long syscall_nr; | |
609320c8 | 575 | unsigned long args[SYSCALL_DEFINE_MAXARGS]; |
cf5f5cea YS |
576 | } param; |
577 | int i; | |
578 | ||
579 | *(struct pt_regs **)¶m = regs; | |
580 | param.syscall_nr = rec->nr; | |
581 | for (i = 0; i < sys_data->nb_args; i++) | |
582 | param.args[i] = rec->args[i]; | |
e87c6bc3 | 583 | return trace_call_bpf(call, ¶m); |
cf5f5cea YS |
584 | } |
585 | ||
38516ab5 | 586 | static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id) |
f4b5ffcc JB |
587 | { |
588 | struct syscall_metadata *sys_data; | |
20ab4425 | 589 | struct syscall_trace_enter *rec; |
1c024eca | 590 | struct hlist_head *head; |
d08e4113 | 591 | unsigned long args[6]; |
e87c6bc3 | 592 | bool valid_prog_array; |
f4b5ffcc | 593 | int syscall_nr; |
4ed7c92d | 594 | int rctx; |
19007a67 | 595 | int size; |
f4b5ffcc | 596 | |
f431b634 | 597 | syscall_nr = trace_get_syscall_nr(current, regs); |
086ba77a | 598 | if (syscall_nr < 0 || syscall_nr >= NR_syscalls) |
60916a93 | 599 | return; |
97d5a220 | 600 | if (!test_bit(syscall_nr, enabled_perf_enter_syscalls)) |
f4b5ffcc JB |
601 | return; |
602 | ||
603 | sys_data = syscall_nr_to_meta(syscall_nr); | |
604 | if (!sys_data) | |
605 | return; | |
606 | ||
421c7860 | 607 | head = this_cpu_ptr(sys_data->enter_event->perf_events); |
e87c6bc3 YS |
608 | valid_prog_array = bpf_prog_array_valid(sys_data->enter_event); |
609 | if (!valid_prog_array && hlist_empty(head)) | |
421c7860 ON |
610 | return; |
611 | ||
19007a67 FW |
612 | /* 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 | ||
1e1dcd93 | 617 | rec = perf_trace_buf_alloc(size, NULL, &rctx); |
430ad5a6 XG |
618 | if (!rec) |
619 | return; | |
20ab4425 | 620 | |
20ab4425 | 621 | rec->nr = syscall_nr; |
b35f549d | 622 | syscall_get_arguments(current, regs, args); |
d08e4113 | 623 | memcpy(&rec->args, args, sizeof(unsigned long) * sys_data->nb_args); |
cf5f5cea | 624 | |
e87c6bc3 YS |
625 | if ((valid_prog_array && |
626 | !perf_call_bpf_enter(sys_data->enter_event, regs, sys_data, rec)) || | |
cf5f5cea YS |
627 | hlist_empty(head)) { |
628 | perf_swevent_put_recursion_context(rctx); | |
629 | return; | |
630 | } | |
631 | ||
1e1dcd93 AS |
632 | perf_trace_buf_submit(rec, size, rctx, |
633 | sys_data->enter_event->event.type, 1, regs, | |
8fd0fbbe | 634 | head, NULL); |
f4b5ffcc JB |
635 | } |
636 | ||
2425bcb9 | 637 | static int perf_sysenter_enable(struct trace_event_call *call) |
f4b5ffcc JB |
638 | { |
639 | int ret = 0; | |
640 | int num; | |
641 | ||
3bbe84e9 | 642 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
f4b5ffcc JB |
643 | |
644 | mutex_lock(&syscall_trace_lock); | |
97d5a220 | 645 | if (!sys_perf_refcount_enter) |
38516ab5 | 646 | ret = register_trace_sys_enter(perf_syscall_enter, NULL); |
f4b5ffcc | 647 | if (ret) { |
d282b9c0 | 648 | pr_info("event trace: Could not activate syscall entry trace point"); |
f4b5ffcc | 649 | } else { |
97d5a220 FW |
650 | set_bit(num, enabled_perf_enter_syscalls); |
651 | sys_perf_refcount_enter++; | |
f4b5ffcc JB |
652 | } |
653 | mutex_unlock(&syscall_trace_lock); | |
654 | return ret; | |
655 | } | |
656 | ||
2425bcb9 | 657 | static void perf_sysenter_disable(struct trace_event_call *call) |
f4b5ffcc JB |
658 | { |
659 | int num; | |
660 | ||
3bbe84e9 | 661 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
f4b5ffcc JB |
662 | |
663 | mutex_lock(&syscall_trace_lock); | |
97d5a220 FW |
664 | sys_perf_refcount_enter--; |
665 | clear_bit(num, enabled_perf_enter_syscalls); | |
666 | if (!sys_perf_refcount_enter) | |
38516ab5 | 667 | unregister_trace_sys_enter(perf_syscall_enter, NULL); |
f4b5ffcc JB |
668 | mutex_unlock(&syscall_trace_lock); |
669 | } | |
670 | ||
e87c6bc3 YS |
671 | static int perf_call_bpf_exit(struct trace_event_call *call, struct pt_regs *regs, |
672 | struct syscall_trace_exit *rec) | |
673 | { | |
cf5f5cea YS |
674 | 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 **)¶m = regs; | |
681 | param.syscall_nr = rec->nr; | |
682 | param.ret = rec->ret; | |
e87c6bc3 | 683 | return trace_call_bpf(call, ¶m); |
cf5f5cea YS |
684 | } |
685 | ||
38516ab5 | 686 | static void perf_syscall_exit(void *ignore, struct pt_regs *regs, long ret) |
f4b5ffcc JB |
687 | { |
688 | struct syscall_metadata *sys_data; | |
20ab4425 | 689 | struct syscall_trace_exit *rec; |
1c024eca | 690 | struct hlist_head *head; |
e87c6bc3 | 691 | bool valid_prog_array; |
f4b5ffcc | 692 | int syscall_nr; |
4ed7c92d | 693 | int rctx; |
20ab4425 | 694 | int size; |
f4b5ffcc | 695 | |
f431b634 | 696 | syscall_nr = trace_get_syscall_nr(current, regs); |
086ba77a | 697 | if (syscall_nr < 0 || syscall_nr >= NR_syscalls) |
60916a93 | 698 | return; |
97d5a220 | 699 | if (!test_bit(syscall_nr, enabled_perf_exit_syscalls)) |
f4b5ffcc JB |
700 | return; |
701 | ||
702 | sys_data = syscall_nr_to_meta(syscall_nr); | |
703 | if (!sys_data) | |
704 | return; | |
705 | ||
421c7860 | 706 | head = this_cpu_ptr(sys_data->exit_event->perf_events); |
e87c6bc3 YS |
707 | valid_prog_array = bpf_prog_array_valid(sys_data->exit_event); |
708 | if (!valid_prog_array && hlist_empty(head)) | |
421c7860 ON |
709 | return; |
710 | ||
20ab4425 FW |
711 | /* We can probably do that at build time */ |
712 | size = ALIGN(sizeof(*rec) + sizeof(u32), sizeof(u64)); | |
713 | size -= sizeof(u32); | |
19007a67 | 714 | |
1e1dcd93 | 715 | rec = perf_trace_buf_alloc(size, NULL, &rctx); |
430ad5a6 XG |
716 | if (!rec) |
717 | return; | |
20ab4425 | 718 | |
20ab4425 FW |
719 | rec->nr = syscall_nr; |
720 | rec->ret = syscall_get_return_value(current, regs); | |
cf5f5cea | 721 | |
e87c6bc3 YS |
722 | if ((valid_prog_array && |
723 | !perf_call_bpf_exit(sys_data->exit_event, regs, rec)) || | |
cf5f5cea YS |
724 | hlist_empty(head)) { |
725 | perf_swevent_put_recursion_context(rctx); | |
726 | return; | |
727 | } | |
728 | ||
1e1dcd93 | 729 | perf_trace_buf_submit(rec, size, rctx, sys_data->exit_event->event.type, |
8fd0fbbe | 730 | 1, regs, head, NULL); |
f4b5ffcc JB |
731 | } |
732 | ||
2425bcb9 | 733 | static int perf_sysexit_enable(struct trace_event_call *call) |
f4b5ffcc JB |
734 | { |
735 | int ret = 0; | |
736 | int num; | |
737 | ||
3bbe84e9 | 738 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
f4b5ffcc JB |
739 | |
740 | mutex_lock(&syscall_trace_lock); | |
97d5a220 | 741 | if (!sys_perf_refcount_exit) |
38516ab5 | 742 | ret = register_trace_sys_exit(perf_syscall_exit, NULL); |
f4b5ffcc | 743 | if (ret) { |
d282b9c0 | 744 | pr_info("event trace: Could not activate syscall exit trace point"); |
f4b5ffcc | 745 | } else { |
97d5a220 FW |
746 | set_bit(num, enabled_perf_exit_syscalls); |
747 | sys_perf_refcount_exit++; | |
f4b5ffcc JB |
748 | } |
749 | mutex_unlock(&syscall_trace_lock); | |
750 | return ret; | |
751 | } | |
752 | ||
2425bcb9 | 753 | static void perf_sysexit_disable(struct trace_event_call *call) |
f4b5ffcc JB |
754 | { |
755 | int num; | |
756 | ||
3bbe84e9 | 757 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
f4b5ffcc JB |
758 | |
759 | mutex_lock(&syscall_trace_lock); | |
97d5a220 FW |
760 | sys_perf_refcount_exit--; |
761 | clear_bit(num, enabled_perf_exit_syscalls); | |
762 | if (!sys_perf_refcount_exit) | |
38516ab5 | 763 | unregister_trace_sys_exit(perf_syscall_exit, NULL); |
f4b5ffcc JB |
764 | mutex_unlock(&syscall_trace_lock); |
765 | } | |
766 | ||
07b139c8 | 767 | #endif /* CONFIG_PERF_EVENTS */ |
f4b5ffcc | 768 | |
2425bcb9 | 769 | static int syscall_enter_register(struct trace_event_call *event, |
ceec0b6f | 770 | enum trace_reg type, void *data) |
2239291a | 771 | { |
7f1d2f82 | 772 | struct trace_event_file *file = data; |
12ab74ee | 773 | |
2239291a SR |
774 | switch (type) { |
775 | case TRACE_REG_REGISTER: | |
12ab74ee | 776 | return reg_event_syscall_enter(file, event); |
2239291a | 777 | case TRACE_REG_UNREGISTER: |
12ab74ee | 778 | unreg_event_syscall_enter(file, event); |
2239291a SR |
779 | 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; | |
ceec0b6f JO |
787 | case TRACE_REG_PERF_OPEN: |
788 | case TRACE_REG_PERF_CLOSE: | |
489c75c3 JO |
789 | case TRACE_REG_PERF_ADD: |
790 | case TRACE_REG_PERF_DEL: | |
ceec0b6f | 791 | return 0; |
2239291a SR |
792 | #endif |
793 | } | |
794 | return 0; | |
795 | } | |
796 | ||
2425bcb9 | 797 | static int syscall_exit_register(struct trace_event_call *event, |
ceec0b6f | 798 | enum trace_reg type, void *data) |
2239291a | 799 | { |
7f1d2f82 | 800 | struct trace_event_file *file = data; |
12ab74ee | 801 | |
2239291a SR |
802 | switch (type) { |
803 | case TRACE_REG_REGISTER: | |
12ab74ee | 804 | return reg_event_syscall_exit(file, event); |
2239291a | 805 | case TRACE_REG_UNREGISTER: |
12ab74ee | 806 | unreg_event_syscall_exit(file, event); |
2239291a SR |
807 | 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; | |
ceec0b6f JO |
815 | case TRACE_REG_PERF_OPEN: |
816 | case TRACE_REG_PERF_CLOSE: | |
489c75c3 JO |
817 | case TRACE_REG_PERF_ADD: |
818 | case TRACE_REG_PERF_DEL: | |
ceec0b6f | 819 | return 0; |
2239291a SR |
820 | #endif |
821 | } | |
822 | return 0; | |
823 | } |