1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * event tracer
4 *
5 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
6 *
7 * - Added format output of fields of the trace point.
8 * This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
9 *
10 */
11
12 #define pr_fmt(fmt) fmt
13
14 #include <linux/workqueue.h>
15 #include <linux/security.h>
16 #include <linux/spinlock.h>
17 #include <linux/kthread.h>
18 #include <linux/tracefs.h>
19 #include <linux/uaccess.h>
20 #include <linux/module.h>
21 #include <linux/ctype.h>
22 #include <linux/sort.h>
23 #include <linux/slab.h>
24 #include <linux/delay.h>
25
26 #include <trace/events/sched.h>
27 #include <trace/syscall.h>
28
29 #include <asm/setup.h>
30
31 #include "trace_output.h"
32
33 #undef TRACE_SYSTEM
34 #define TRACE_SYSTEM "TRACE_SYSTEM"
35
36 DEFINE_MUTEX(event_mutex);
37
38 LIST_HEAD(ftrace_events);
39 static LIST_HEAD(ftrace_generic_fields);
40 static LIST_HEAD(ftrace_common_fields);
41 static bool eventdir_initialized;
42
43 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
44
45 static struct kmem_cache *field_cachep;
46 static struct kmem_cache *file_cachep;
47
system_refcount(struct event_subsystem * system)48 static inline int system_refcount(struct event_subsystem *system)
49 {
50 return system->ref_count;
51 }
52
system_refcount_inc(struct event_subsystem * system)53 static int system_refcount_inc(struct event_subsystem *system)
54 {
55 return system->ref_count++;
56 }
57
system_refcount_dec(struct event_subsystem * system)58 static int system_refcount_dec(struct event_subsystem *system)
59 {
60 return --system->ref_count;
61 }
62
63 /* Double loops, do not use break, only goto's work */
64 #define do_for_each_event_file(tr, file) \
65 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
66 list_for_each_entry(file, &tr->events, list)
67
68 #define do_for_each_event_file_safe(tr, file) \
69 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
70 struct trace_event_file *___n; \
71 list_for_each_entry_safe(file, ___n, &tr->events, list)
72
73 #define while_for_each_event_file() \
74 }
75
76 static struct ftrace_event_field *
__find_event_field(struct list_head * head,char * name)77 __find_event_field(struct list_head *head, char *name)
78 {
79 struct ftrace_event_field *field;
80
81 list_for_each_entry(field, head, link) {
82 if (!strcmp(field->name, name))
83 return field;
84 }
85
86 return NULL;
87 }
88
89 struct ftrace_event_field *
trace_find_event_field(struct trace_event_call * call,char * name)90 trace_find_event_field(struct trace_event_call *call, char *name)
91 {
92 struct ftrace_event_field *field;
93 struct list_head *head;
94
95 head = trace_get_fields(call);
96 field = __find_event_field(head, name);
97 if (field)
98 return field;
99
100 field = __find_event_field(&ftrace_generic_fields, name);
101 if (field)
102 return field;
103
104 return __find_event_field(&ftrace_common_fields, name);
105 }
106
__trace_define_field(struct list_head * head,const char * type,const char * name,int offset,int size,int is_signed,int filter_type)107 static int __trace_define_field(struct list_head *head, const char *type,
108 const char *name, int offset, int size,
109 int is_signed, int filter_type)
110 {
111 struct ftrace_event_field *field;
112
113 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
114 if (!field)
115 return -ENOMEM;
116
117 field->name = name;
118 field->type = type;
119
120 if (filter_type == FILTER_OTHER)
121 field->filter_type = filter_assign_type(type);
122 else
123 field->filter_type = filter_type;
124
125 field->offset = offset;
126 field->size = size;
127 field->is_signed = is_signed;
128
129 list_add(&field->link, head);
130
131 return 0;
132 }
133
trace_define_field(struct trace_event_call * call,const char * type,const char * name,int offset,int size,int is_signed,int filter_type)134 int trace_define_field(struct trace_event_call *call, const char *type,
135 const char *name, int offset, int size, int is_signed,
136 int filter_type)
137 {
138 struct list_head *head;
139
140 if (WARN_ON(!call->class))
141 return 0;
142
143 head = trace_get_fields(call);
144 return __trace_define_field(head, type, name, offset, size,
145 is_signed, filter_type);
146 }
147 EXPORT_SYMBOL_GPL(trace_define_field);
148
149 #define __generic_field(type, item, filter_type) \
150 ret = __trace_define_field(&ftrace_generic_fields, #type, \
151 #item, 0, 0, is_signed_type(type), \
152 filter_type); \
153 if (ret) \
154 return ret;
155
156 #define __common_field(type, item) \
157 ret = __trace_define_field(&ftrace_common_fields, #type, \
158 "common_" #item, \
159 offsetof(typeof(ent), item), \
160 sizeof(ent.item), \
161 is_signed_type(type), FILTER_OTHER); \
162 if (ret) \
163 return ret;
164
trace_define_generic_fields(void)165 static int trace_define_generic_fields(void)
166 {
167 int ret;
168
169 __generic_field(int, CPU, FILTER_CPU);
170 __generic_field(int, cpu, FILTER_CPU);
171 __generic_field(int, common_cpu, FILTER_CPU);
172 __generic_field(char *, COMM, FILTER_COMM);
173 __generic_field(char *, comm, FILTER_COMM);
174
175 return ret;
176 }
177
trace_define_common_fields(void)178 static int trace_define_common_fields(void)
179 {
180 int ret;
181 struct trace_entry ent;
182
183 __common_field(unsigned short, type);
184 __common_field(unsigned char, flags);
185 __common_field(unsigned char, preempt_count);
186 __common_field(int, pid);
187
188 return ret;
189 }
190
trace_destroy_fields(struct trace_event_call * call)191 static void trace_destroy_fields(struct trace_event_call *call)
192 {
193 struct ftrace_event_field *field, *next;
194 struct list_head *head;
195
196 head = trace_get_fields(call);
197 list_for_each_entry_safe(field, next, head, link) {
198 list_del(&field->link);
199 kmem_cache_free(field_cachep, field);
200 }
201 }
202
203 /*
204 * run-time version of trace_event_get_offsets_<call>() that returns the last
205 * accessible offset of trace fields excluding __dynamic_array bytes
206 */
trace_event_get_offsets(struct trace_event_call * call)207 int trace_event_get_offsets(struct trace_event_call *call)
208 {
209 struct ftrace_event_field *tail;
210 struct list_head *head;
211
212 head = trace_get_fields(call);
213 /*
214 * head->next points to the last field with the largest offset,
215 * since it was added last by trace_define_field()
216 */
217 tail = list_first_entry(head, struct ftrace_event_field, link);
218 return tail->offset + tail->size;
219 }
220
trace_event_raw_init(struct trace_event_call * call)221 int trace_event_raw_init(struct trace_event_call *call)
222 {
223 int id;
224
225 id = register_trace_event(&call->event);
226 if (!id)
227 return -ENODEV;
228
229 return 0;
230 }
231 EXPORT_SYMBOL_GPL(trace_event_raw_init);
232
trace_event_ignore_this_pid(struct trace_event_file * trace_file)233 bool trace_event_ignore_this_pid(struct trace_event_file *trace_file)
234 {
235 struct trace_array *tr = trace_file->tr;
236 struct trace_array_cpu *data;
237 struct trace_pid_list *no_pid_list;
238 struct trace_pid_list *pid_list;
239
240 pid_list = rcu_dereference_raw(tr->filtered_pids);
241 no_pid_list = rcu_dereference_raw(tr->filtered_no_pids);
242
243 if (!pid_list && !no_pid_list)
244 return false;
245
246 data = this_cpu_ptr(tr->array_buffer.data);
247
248 return data->ignore_pid;
249 }
250 EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid);
251
trace_event_buffer_reserve(struct trace_event_buffer * fbuffer,struct trace_event_file * trace_file,unsigned long len)252 void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
253 struct trace_event_file *trace_file,
254 unsigned long len)
255 {
256 struct trace_event_call *event_call = trace_file->event_call;
257
258 if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) &&
259 trace_event_ignore_this_pid(trace_file))
260 return NULL;
261
262 local_save_flags(fbuffer->flags);
263 fbuffer->pc = preempt_count();
264 /*
265 * If CONFIG_PREEMPTION is enabled, then the tracepoint itself disables
266 * preemption (adding one to the preempt_count). Since we are
267 * interested in the preempt_count at the time the tracepoint was
268 * hit, we need to subtract one to offset the increment.
269 */
270 if (IS_ENABLED(CONFIG_PREEMPTION))
271 fbuffer->pc--;
272 fbuffer->trace_file = trace_file;
273
274 fbuffer->event =
275 trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
276 event_call->event.type, len,
277 fbuffer->flags, fbuffer->pc);
278 if (!fbuffer->event)
279 return NULL;
280
281 fbuffer->regs = NULL;
282 fbuffer->entry = ring_buffer_event_data(fbuffer->event);
283 return fbuffer->entry;
284 }
285 EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
286
trace_event_reg(struct trace_event_call * call,enum trace_reg type,void * data)287 int trace_event_reg(struct trace_event_call *call,
288 enum trace_reg type, void *data)
289 {
290 struct trace_event_file *file = data;
291
292 WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
293 switch (type) {
294 case TRACE_REG_REGISTER:
295 return tracepoint_probe_register(call->tp,
296 call->class->probe,
297 file);
298 case TRACE_REG_UNREGISTER:
299 tracepoint_probe_unregister(call->tp,
300 call->class->probe,
301 file);
302 return 0;
303
304 #ifdef CONFIG_PERF_EVENTS
305 case TRACE_REG_PERF_REGISTER:
306 return tracepoint_probe_register(call->tp,
307 call->class->perf_probe,
308 call);
309 case TRACE_REG_PERF_UNREGISTER:
310 tracepoint_probe_unregister(call->tp,
311 call->class->perf_probe,
312 call);
313 return 0;
314 case TRACE_REG_PERF_OPEN:
315 case TRACE_REG_PERF_CLOSE:
316 case TRACE_REG_PERF_ADD:
317 case TRACE_REG_PERF_DEL:
318 return 0;
319 #endif
320 }
321 return 0;
322 }
323 EXPORT_SYMBOL_GPL(trace_event_reg);
324
trace_event_enable_cmd_record(bool enable)325 void trace_event_enable_cmd_record(bool enable)
326 {
327 struct trace_event_file *file;
328 struct trace_array *tr;
329
330 lockdep_assert_held(&event_mutex);
331
332 do_for_each_event_file(tr, file) {
333
334 if (!(file->flags & EVENT_FILE_FL_ENABLED))
335 continue;
336
337 if (enable) {
338 tracing_start_cmdline_record();
339 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
340 } else {
341 tracing_stop_cmdline_record();
342 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
343 }
344 } while_for_each_event_file();
345 }
346
trace_event_enable_tgid_record(bool enable)347 void trace_event_enable_tgid_record(bool enable)
348 {
349 struct trace_event_file *file;
350 struct trace_array *tr;
351
352 lockdep_assert_held(&event_mutex);
353
354 do_for_each_event_file(tr, file) {
355 if (!(file->flags & EVENT_FILE_FL_ENABLED))
356 continue;
357
358 if (enable) {
359 tracing_start_tgid_record();
360 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
361 } else {
362 tracing_stop_tgid_record();
363 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT,
364 &file->flags);
365 }
366 } while_for_each_event_file();
367 }
368
__ftrace_event_enable_disable(struct trace_event_file * file,int enable,int soft_disable)369 static int __ftrace_event_enable_disable(struct trace_event_file *file,
370 int enable, int soft_disable)
371 {
372 struct trace_event_call *call = file->event_call;
373 struct trace_array *tr = file->tr;
374 int ret = 0;
375 int disable;
376
377 switch (enable) {
378 case 0:
379 /*
380 * When soft_disable is set and enable is cleared, the sm_ref
381 * reference counter is decremented. If it reaches 0, we want
382 * to clear the SOFT_DISABLED flag but leave the event in the
383 * state that it was. That is, if the event was enabled and
384 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
385 * is set we do not want the event to be enabled before we
386 * clear the bit.
387 *
388 * When soft_disable is not set but the SOFT_MODE flag is,
389 * we do nothing. Do not disable the tracepoint, otherwise
390 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
391 */
392 if (soft_disable) {
393 if (atomic_dec_return(&file->sm_ref) > 0)
394 break;
395 disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
396 clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
397 /* Disable use of trace_buffered_event */
398 trace_buffered_event_disable();
399 } else
400 disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
401
402 if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
403 clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
404 if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
405 tracing_stop_cmdline_record();
406 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
407 }
408
409 if (file->flags & EVENT_FILE_FL_RECORDED_TGID) {
410 tracing_stop_tgid_record();
411 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
412 }
413
414 call->class->reg(call, TRACE_REG_UNREGISTER, file);
415 }
416 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
417 if (file->flags & EVENT_FILE_FL_SOFT_MODE)
418 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
419 else
420 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
421 break;
422 case 1:
423 /*
424 * When soft_disable is set and enable is set, we want to
425 * register the tracepoint for the event, but leave the event
426 * as is. That means, if the event was already enabled, we do
427 * nothing (but set SOFT_MODE). If the event is disabled, we
428 * set SOFT_DISABLED before enabling the event tracepoint, so
429 * it still seems to be disabled.
430 */
431 if (!soft_disable)
432 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
433 else {
434 if (atomic_inc_return(&file->sm_ref) > 1)
435 break;
436 set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
437 /* Enable use of trace_buffered_event */
438 trace_buffered_event_enable();
439 }
440
441 if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
442 bool cmd = false, tgid = false;
443
444 /* Keep the event disabled, when going to SOFT_MODE. */
445 if (soft_disable)
446 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
447
448 if (tr->trace_flags & TRACE_ITER_RECORD_CMD) {
449 cmd = true;
450 tracing_start_cmdline_record();
451 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
452 }
453
454 if (tr->trace_flags & TRACE_ITER_RECORD_TGID) {
455 tgid = true;
456 tracing_start_tgid_record();
457 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
458 }
459
460 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
461 if (ret) {
462 if (cmd)
463 tracing_stop_cmdline_record();
464 if (tgid)
465 tracing_stop_tgid_record();
466 pr_info("event trace: Could not enable event "
467 "%s\n", trace_event_name(call));
468 break;
469 }
470 set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
471
472 /* WAS_ENABLED gets set but never cleared. */
473 set_bit(EVENT_FILE_FL_WAS_ENABLED_BIT, &file->flags);
474 }
475 break;
476 }
477
478 return ret;
479 }
480
trace_event_enable_disable(struct trace_event_file * file,int enable,int soft_disable)481 int trace_event_enable_disable(struct trace_event_file *file,
482 int enable, int soft_disable)
483 {
484 return __ftrace_event_enable_disable(file, enable, soft_disable);
485 }
486
ftrace_event_enable_disable(struct trace_event_file * file,int enable)487 static int ftrace_event_enable_disable(struct trace_event_file *file,
488 int enable)
489 {
490 return __ftrace_event_enable_disable(file, enable, 0);
491 }
492
ftrace_clear_events(struct trace_array * tr)493 static void ftrace_clear_events(struct trace_array *tr)
494 {
495 struct trace_event_file *file;
496
497 mutex_lock(&event_mutex);
498 list_for_each_entry(file, &tr->events, list) {
499 ftrace_event_enable_disable(file, 0);
500 }
501 mutex_unlock(&event_mutex);
502 }
503
504 static void
event_filter_pid_sched_process_exit(void * data,struct task_struct * task)505 event_filter_pid_sched_process_exit(void *data, struct task_struct *task)
506 {
507 struct trace_pid_list *pid_list;
508 struct trace_array *tr = data;
509
510 pid_list = rcu_dereference_raw(tr->filtered_pids);
511 trace_filter_add_remove_task(pid_list, NULL, task);
512
513 pid_list = rcu_dereference_raw(tr->filtered_no_pids);
514 trace_filter_add_remove_task(pid_list, NULL, task);
515 }
516
517 static void
event_filter_pid_sched_process_fork(void * data,struct task_struct * self,struct task_struct * task)518 event_filter_pid_sched_process_fork(void *data,
519 struct task_struct *self,
520 struct task_struct *task)
521 {
522 struct trace_pid_list *pid_list;
523 struct trace_array *tr = data;
524
525 pid_list = rcu_dereference_sched(tr->filtered_pids);
526 trace_filter_add_remove_task(pid_list, self, task);
527
528 pid_list = rcu_dereference_sched(tr->filtered_no_pids);
529 trace_filter_add_remove_task(pid_list, self, task);
530 }
531
trace_event_follow_fork(struct trace_array * tr,bool enable)532 void trace_event_follow_fork(struct trace_array *tr, bool enable)
533 {
534 if (enable) {
535 register_trace_prio_sched_process_fork(event_filter_pid_sched_process_fork,
536 tr, INT_MIN);
537 register_trace_prio_sched_process_free(event_filter_pid_sched_process_exit,
538 tr, INT_MAX);
539 } else {
540 unregister_trace_sched_process_fork(event_filter_pid_sched_process_fork,
541 tr);
542 unregister_trace_sched_process_free(event_filter_pid_sched_process_exit,
543 tr);
544 }
545 }
546
547 static void
event_filter_pid_sched_switch_probe_pre(void * data,bool preempt,struct task_struct * prev,struct task_struct * next)548 event_filter_pid_sched_switch_probe_pre(void *data, bool preempt,
549 struct task_struct *prev, struct task_struct *next)
550 {
551 struct trace_array *tr = data;
552 struct trace_pid_list *no_pid_list;
553 struct trace_pid_list *pid_list;
554 bool ret;
555
556 pid_list = rcu_dereference_sched(tr->filtered_pids);
557 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
558
559 /*
560 * Sched switch is funny, as we only want to ignore it
561 * in the notrace case if both prev and next should be ignored.
562 */
563 ret = trace_ignore_this_task(NULL, no_pid_list, prev) &&
564 trace_ignore_this_task(NULL, no_pid_list, next);
565
566 this_cpu_write(tr->array_buffer.data->ignore_pid, ret ||
567 (trace_ignore_this_task(pid_list, NULL, prev) &&
568 trace_ignore_this_task(pid_list, NULL, next)));
569 }
570
571 static void
event_filter_pid_sched_switch_probe_post(void * data,bool preempt,struct task_struct * prev,struct task_struct * next)572 event_filter_pid_sched_switch_probe_post(void *data, bool preempt,
573 struct task_struct *prev, struct task_struct *next)
574 {
575 struct trace_array *tr = data;
576 struct trace_pid_list *no_pid_list;
577 struct trace_pid_list *pid_list;
578
579 pid_list = rcu_dereference_sched(tr->filtered_pids);
580 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
581
582 this_cpu_write(tr->array_buffer.data->ignore_pid,
583 trace_ignore_this_task(pid_list, no_pid_list, next));
584 }
585
586 static void
event_filter_pid_sched_wakeup_probe_pre(void * data,struct task_struct * task)587 event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task)
588 {
589 struct trace_array *tr = data;
590 struct trace_pid_list *no_pid_list;
591 struct trace_pid_list *pid_list;
592
593 /* Nothing to do if we are already tracing */
594 if (!this_cpu_read(tr->array_buffer.data->ignore_pid))
595 return;
596
597 pid_list = rcu_dereference_sched(tr->filtered_pids);
598 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
599
600 this_cpu_write(tr->array_buffer.data->ignore_pid,
601 trace_ignore_this_task(pid_list, no_pid_list, task));
602 }
603
604 static void
event_filter_pid_sched_wakeup_probe_post(void * data,struct task_struct * task)605 event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task)
606 {
607 struct trace_array *tr = data;
608 struct trace_pid_list *no_pid_list;
609 struct trace_pid_list *pid_list;
610
611 /* Nothing to do if we are not tracing */
612 if (this_cpu_read(tr->array_buffer.data->ignore_pid))
613 return;
614
615 pid_list = rcu_dereference_sched(tr->filtered_pids);
616 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
617
618 /* Set tracing if current is enabled */
619 this_cpu_write(tr->array_buffer.data->ignore_pid,
620 trace_ignore_this_task(pid_list, no_pid_list, current));
621 }
622
unregister_pid_events(struct trace_array * tr)623 static void unregister_pid_events(struct trace_array *tr)
624 {
625 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr);
626 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr);
627
628 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr);
629 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr);
630
631 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, tr);
632 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, tr);
633
634 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_pre, tr);
635 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_post, tr);
636 }
637
__ftrace_clear_event_pids(struct trace_array * tr,int type)638 static void __ftrace_clear_event_pids(struct trace_array *tr, int type)
639 {
640 struct trace_pid_list *pid_list;
641 struct trace_pid_list *no_pid_list;
642 struct trace_event_file *file;
643 int cpu;
644
645 pid_list = rcu_dereference_protected(tr->filtered_pids,
646 lockdep_is_held(&event_mutex));
647 no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
648 lockdep_is_held(&event_mutex));
649
650 /* Make sure there's something to do */
651 if (!pid_type_enabled(type, pid_list, no_pid_list))
652 return;
653
654 if (!still_need_pid_events(type, pid_list, no_pid_list)) {
655 unregister_pid_events(tr);
656
657 list_for_each_entry(file, &tr->events, list) {
658 clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
659 }
660
661 for_each_possible_cpu(cpu)
662 per_cpu_ptr(tr->array_buffer.data, cpu)->ignore_pid = false;
663 }
664
665 if (type & TRACE_PIDS)
666 rcu_assign_pointer(tr->filtered_pids, NULL);
667
668 if (type & TRACE_NO_PIDS)
669 rcu_assign_pointer(tr->filtered_no_pids, NULL);
670
671 /* Wait till all users are no longer using pid filtering */
672 tracepoint_synchronize_unregister();
673
674 if ((type & TRACE_PIDS) && pid_list)
675 trace_free_pid_list(pid_list);
676
677 if ((type & TRACE_NO_PIDS) && no_pid_list)
678 trace_free_pid_list(no_pid_list);
679 }
680
ftrace_clear_event_pids(struct trace_array * tr,int type)681 static void ftrace_clear_event_pids(struct trace_array *tr, int type)
682 {
683 mutex_lock(&event_mutex);
684 __ftrace_clear_event_pids(tr, type);
685 mutex_unlock(&event_mutex);
686 }
687
__put_system(struct event_subsystem * system)688 static void __put_system(struct event_subsystem *system)
689 {
690 struct event_filter *filter = system->filter;
691
692 WARN_ON_ONCE(system_refcount(system) == 0);
693 if (system_refcount_dec(system))
694 return;
695
696 list_del(&system->list);
697
698 if (filter) {
699 kfree(filter->filter_string);
700 kfree(filter);
701 }
702 kfree_const(system->name);
703 kfree(system);
704 }
705
__get_system(struct event_subsystem * system)706 static void __get_system(struct event_subsystem *system)
707 {
708 WARN_ON_ONCE(system_refcount(system) == 0);
709 system_refcount_inc(system);
710 }
711
__get_system_dir(struct trace_subsystem_dir * dir)712 static void __get_system_dir(struct trace_subsystem_dir *dir)
713 {
714 WARN_ON_ONCE(dir->ref_count == 0);
715 dir->ref_count++;
716 __get_system(dir->subsystem);
717 }
718
__put_system_dir(struct trace_subsystem_dir * dir)719 static void __put_system_dir(struct trace_subsystem_dir *dir)
720 {
721 WARN_ON_ONCE(dir->ref_count == 0);
722 /* If the subsystem is about to be freed, the dir must be too */
723 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
724
725 __put_system(dir->subsystem);
726 if (!--dir->ref_count)
727 kfree(dir);
728 }
729
put_system(struct trace_subsystem_dir * dir)730 static void put_system(struct trace_subsystem_dir *dir)
731 {
732 mutex_lock(&event_mutex);
733 __put_system_dir(dir);
734 mutex_unlock(&event_mutex);
735 }
736
remove_subsystem(struct trace_subsystem_dir * dir)737 static void remove_subsystem(struct trace_subsystem_dir *dir)
738 {
739 if (!dir)
740 return;
741
742 if (!--dir->nr_events) {
743 tracefs_remove(dir->entry);
744 list_del(&dir->list);
745 __put_system_dir(dir);
746 }
747 }
748
remove_event_file_dir(struct trace_event_file * file)749 static void remove_event_file_dir(struct trace_event_file *file)
750 {
751 struct dentry *dir = file->dir;
752 struct dentry *child;
753
754 if (dir) {
755 spin_lock(&dir->d_lock); /* probably unneeded */
756 list_for_each_entry(child, &dir->d_subdirs, d_child) {
757 if (d_really_is_positive(child)) /* probably unneeded */
758 d_inode(child)->i_private = NULL;
759 }
760 spin_unlock(&dir->d_lock);
761
762 tracefs_remove(dir);
763 }
764
765 list_del(&file->list);
766 remove_subsystem(file->system);
767 free_event_filter(file->filter);
768 kmem_cache_free(file_cachep, file);
769 }
770
771 /*
772 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
773 */
774 static int
__ftrace_set_clr_event_nolock(struct trace_array * tr,const char * match,const char * sub,const char * event,int set)775 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
776 const char *sub, const char *event, int set)
777 {
778 struct trace_event_file *file;
779 struct trace_event_call *call;
780 const char *name;
781 int ret = -EINVAL;
782 int eret = 0;
783
784 list_for_each_entry(file, &tr->events, list) {
785
786 call = file->event_call;
787 name = trace_event_name(call);
788
789 if (!name || !call->class || !call->class->reg)
790 continue;
791
792 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
793 continue;
794
795 if (match &&
796 strcmp(match, name) != 0 &&
797 strcmp(match, call->class->system) != 0)
798 continue;
799
800 if (sub && strcmp(sub, call->class->system) != 0)
801 continue;
802
803 if (event && strcmp(event, name) != 0)
804 continue;
805
806 ret = ftrace_event_enable_disable(file, set);
807
808 /*
809 * Save the first error and return that. Some events
810 * may still have been enabled, but let the user
811 * know that something went wrong.
812 */
813 if (ret && !eret)
814 eret = ret;
815
816 ret = eret;
817 }
818
819 return ret;
820 }
821
__ftrace_set_clr_event(struct trace_array * tr,const char * match,const char * sub,const char * event,int set)822 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
823 const char *sub, const char *event, int set)
824 {
825 int ret;
826
827 mutex_lock(&event_mutex);
828 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
829 mutex_unlock(&event_mutex);
830
831 return ret;
832 }
833
ftrace_set_clr_event(struct trace_array * tr,char * buf,int set)834 int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
835 {
836 char *event = NULL, *sub = NULL, *match;
837 int ret;
838
839 if (!tr)
840 return -ENOENT;
841 /*
842 * The buf format can be <subsystem>:<event-name>
843 * *:<event-name> means any event by that name.
844 * :<event-name> is the same.
845 *
846 * <subsystem>:* means all events in that subsystem
847 * <subsystem>: means the same.
848 *
849 * <name> (no ':') means all events in a subsystem with
850 * the name <name> or any event that matches <name>
851 */
852
853 match = strsep(&buf, ":");
854 if (buf) {
855 sub = match;
856 event = buf;
857 match = NULL;
858
859 if (!strlen(sub) || strcmp(sub, "*") == 0)
860 sub = NULL;
861 if (!strlen(event) || strcmp(event, "*") == 0)
862 event = NULL;
863 }
864
865 ret = __ftrace_set_clr_event(tr, match, sub, event, set);
866
867 /* Put back the colon to allow this to be called again */
868 if (buf)
869 *(buf - 1) = ':';
870
871 return ret;
872 }
873
874 /**
875 * trace_set_clr_event - enable or disable an event
876 * @system: system name to match (NULL for any system)
877 * @event: event name to match (NULL for all events, within system)
878 * @set: 1 to enable, 0 to disable
879 *
880 * This is a way for other parts of the kernel to enable or disable
881 * event recording.
882 *
883 * Returns 0 on success, -EINVAL if the parameters do not match any
884 * registered events.
885 */
trace_set_clr_event(const char * system,const char * event,int set)886 int trace_set_clr_event(const char *system, const char *event, int set)
887 {
888 struct trace_array *tr = top_trace_array();
889
890 if (!tr)
891 return -ENODEV;
892
893 return __ftrace_set_clr_event(tr, NULL, system, event, set);
894 }
895 EXPORT_SYMBOL_GPL(trace_set_clr_event);
896
897 /**
898 * trace_array_set_clr_event - enable or disable an event for a trace array.
899 * @tr: concerned trace array.
900 * @system: system name to match (NULL for any system)
901 * @event: event name to match (NULL for all events, within system)
902 * @enable: true to enable, false to disable
903 *
904 * This is a way for other parts of the kernel to enable or disable
905 * event recording.
906 *
907 * Returns 0 on success, -EINVAL if the parameters do not match any
908 * registered events.
909 */
trace_array_set_clr_event(struct trace_array * tr,const char * system,const char * event,bool enable)910 int trace_array_set_clr_event(struct trace_array *tr, const char *system,
911 const char *event, bool enable)
912 {
913 int set;
914
915 if (!tr)
916 return -ENOENT;
917
918 set = (enable == true) ? 1 : 0;
919 return __ftrace_set_clr_event(tr, NULL, system, event, set);
920 }
921 EXPORT_SYMBOL_GPL(trace_array_set_clr_event);
922
923 /* 128 should be much more than enough */
924 #define EVENT_BUF_SIZE 127
925
926 static ssize_t
ftrace_event_write(struct file * file,const char __user * ubuf,size_t cnt,loff_t * ppos)927 ftrace_event_write(struct file *file, const char __user *ubuf,
928 size_t cnt, loff_t *ppos)
929 {
930 struct trace_parser parser;
931 struct seq_file *m = file->private_data;
932 struct trace_array *tr = m->private;
933 ssize_t read, ret;
934
935 if (!cnt)
936 return 0;
937
938 ret = tracing_update_buffers();
939 if (ret < 0)
940 return ret;
941
942 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
943 return -ENOMEM;
944
945 read = trace_get_user(&parser, ubuf, cnt, ppos);
946
947 if (read >= 0 && trace_parser_loaded((&parser))) {
948 int set = 1;
949
950 if (*parser.buffer == '!')
951 set = 0;
952
953 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
954 if (ret)
955 goto out_put;
956 }
957
958 ret = read;
959
960 out_put:
961 trace_parser_put(&parser);
962
963 return ret;
964 }
965
966 static void *
t_next(struct seq_file * m,void * v,loff_t * pos)967 t_next(struct seq_file *m, void *v, loff_t *pos)
968 {
969 struct trace_event_file *file = v;
970 struct trace_event_call *call;
971 struct trace_array *tr = m->private;
972
973 (*pos)++;
974
975 list_for_each_entry_continue(file, &tr->events, list) {
976 call = file->event_call;
977 /*
978 * The ftrace subsystem is for showing formats only.
979 * They can not be enabled or disabled via the event files.
980 */
981 if (call->class && call->class->reg &&
982 !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
983 return file;
984 }
985
986 return NULL;
987 }
988
t_start(struct seq_file * m,loff_t * pos)989 static void *t_start(struct seq_file *m, loff_t *pos)
990 {
991 struct trace_event_file *file;
992 struct trace_array *tr = m->private;
993 loff_t l;
994
995 mutex_lock(&event_mutex);
996
997 file = list_entry(&tr->events, struct trace_event_file, list);
998 for (l = 0; l <= *pos; ) {
999 file = t_next(m, file, &l);
1000 if (!file)
1001 break;
1002 }
1003 return file;
1004 }
1005
1006 static void *
s_next(struct seq_file * m,void * v,loff_t * pos)1007 s_next(struct seq_file *m, void *v, loff_t *pos)
1008 {
1009 struct trace_event_file *file = v;
1010 struct trace_array *tr = m->private;
1011
1012 (*pos)++;
1013
1014 list_for_each_entry_continue(file, &tr->events, list) {
1015 if (file->flags & EVENT_FILE_FL_ENABLED)
1016 return file;
1017 }
1018
1019 return NULL;
1020 }
1021
s_start(struct seq_file * m,loff_t * pos)1022 static void *s_start(struct seq_file *m, loff_t *pos)
1023 {
1024 struct trace_event_file *file;
1025 struct trace_array *tr = m->private;
1026 loff_t l;
1027
1028 mutex_lock(&event_mutex);
1029
1030 file = list_entry(&tr->events, struct trace_event_file, list);
1031 for (l = 0; l <= *pos; ) {
1032 file = s_next(m, file, &l);
1033 if (!file)
1034 break;
1035 }
1036 return file;
1037 }
1038
t_show(struct seq_file * m,void * v)1039 static int t_show(struct seq_file *m, void *v)
1040 {
1041 struct trace_event_file *file = v;
1042 struct trace_event_call *call = file->event_call;
1043
1044 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
1045 seq_printf(m, "%s:", call->class->system);
1046 seq_printf(m, "%s\n", trace_event_name(call));
1047
1048 return 0;
1049 }
1050
t_stop(struct seq_file * m,void * p)1051 static void t_stop(struct seq_file *m, void *p)
1052 {
1053 mutex_unlock(&event_mutex);
1054 }
1055
1056 static void *
__next(struct seq_file * m,void * v,loff_t * pos,int type)1057 __next(struct seq_file *m, void *v, loff_t *pos, int type)
1058 {
1059 struct trace_array *tr = m->private;
1060 struct trace_pid_list *pid_list;
1061
1062 if (type == TRACE_PIDS)
1063 pid_list = rcu_dereference_sched(tr->filtered_pids);
1064 else
1065 pid_list = rcu_dereference_sched(tr->filtered_no_pids);
1066
1067 return trace_pid_next(pid_list, v, pos);
1068 }
1069
1070 static void *
p_next(struct seq_file * m,void * v,loff_t * pos)1071 p_next(struct seq_file *m, void *v, loff_t *pos)
1072 {
1073 return __next(m, v, pos, TRACE_PIDS);
1074 }
1075
1076 static void *
np_next(struct seq_file * m,void * v,loff_t * pos)1077 np_next(struct seq_file *m, void *v, loff_t *pos)
1078 {
1079 return __next(m, v, pos, TRACE_NO_PIDS);
1080 }
1081
__start(struct seq_file * m,loff_t * pos,int type)1082 static void *__start(struct seq_file *m, loff_t *pos, int type)
1083 __acquires(RCU)
1084 {
1085 struct trace_pid_list *pid_list;
1086 struct trace_array *tr = m->private;
1087
1088 /*
1089 * Grab the mutex, to keep calls to p_next() having the same
1090 * tr->filtered_pids as p_start() has.
1091 * If we just passed the tr->filtered_pids around, then RCU would
1092 * have been enough, but doing that makes things more complex.
1093 */
1094 mutex_lock(&event_mutex);
1095 rcu_read_lock_sched();
1096
1097 if (type == TRACE_PIDS)
1098 pid_list = rcu_dereference_sched(tr->filtered_pids);
1099 else
1100 pid_list = rcu_dereference_sched(tr->filtered_no_pids);
1101
1102 if (!pid_list)
1103 return NULL;
1104
1105 return trace_pid_start(pid_list, pos);
1106 }
1107
p_start(struct seq_file * m,loff_t * pos)1108 static void *p_start(struct seq_file *m, loff_t *pos)
1109 __acquires(RCU)
1110 {
1111 return __start(m, pos, TRACE_PIDS);
1112 }
1113
np_start(struct seq_file * m,loff_t * pos)1114 static void *np_start(struct seq_file *m, loff_t *pos)
1115 __acquires(RCU)
1116 {
1117 return __start(m, pos, TRACE_NO_PIDS);
1118 }
1119
p_stop(struct seq_file * m,void * p)1120 static void p_stop(struct seq_file *m, void *p)
1121 __releases(RCU)
1122 {
1123 rcu_read_unlock_sched();
1124 mutex_unlock(&event_mutex);
1125 }
1126
1127 static ssize_t
event_enable_read(struct file * filp,char __user * ubuf,size_t cnt,loff_t * ppos)1128 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1129 loff_t *ppos)
1130 {
1131 struct trace_event_file *file;
1132 unsigned long flags;
1133 char buf[4] = "0";
1134
1135 mutex_lock(&event_mutex);
1136 file = event_file_data(filp);
1137 if (likely(file))
1138 flags = file->flags;
1139 mutex_unlock(&event_mutex);
1140
1141 if (!file)
1142 return -ENODEV;
1143
1144 if (flags & EVENT_FILE_FL_ENABLED &&
1145 !(flags & EVENT_FILE_FL_SOFT_DISABLED))
1146 strcpy(buf, "1");
1147
1148 if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
1149 flags & EVENT_FILE_FL_SOFT_MODE)
1150 strcat(buf, "*");
1151
1152 strcat(buf, "\n");
1153
1154 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
1155 }
1156
1157 static ssize_t
event_enable_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos)1158 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1159 loff_t *ppos)
1160 {
1161 struct trace_event_file *file;
1162 unsigned long val;
1163 int ret;
1164
1165 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1166 if (ret)
1167 return ret;
1168
1169 ret = tracing_update_buffers();
1170 if (ret < 0)
1171 return ret;
1172
1173 switch (val) {
1174 case 0:
1175 case 1:
1176 ret = -ENODEV;
1177 mutex_lock(&event_mutex);
1178 file = event_file_data(filp);
1179 if (likely(file))
1180 ret = ftrace_event_enable_disable(file, val);
1181 mutex_unlock(&event_mutex);
1182 break;
1183
1184 default:
1185 return -EINVAL;
1186 }
1187
1188 *ppos += cnt;
1189
1190 return ret ? ret : cnt;
1191 }
1192
1193 static ssize_t
system_enable_read(struct file * filp,char __user * ubuf,size_t cnt,loff_t * ppos)1194 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1195 loff_t *ppos)
1196 {
1197 const char set_to_char[4] = { '?', '0', '1', 'X' };
1198 struct trace_subsystem_dir *dir = filp->private_data;
1199 struct event_subsystem *system = dir->subsystem;
1200 struct trace_event_call *call;
1201 struct trace_event_file *file;
1202 struct trace_array *tr = dir->tr;
1203 char buf[2];
1204 int set = 0;
1205 int ret;
1206
1207 mutex_lock(&event_mutex);
1208 list_for_each_entry(file, &tr->events, list) {
1209 call = file->event_call;
1210 if ((call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) ||
1211 !trace_event_name(call) || !call->class || !call->class->reg)
1212 continue;
1213
1214 if (system && strcmp(call->class->system, system->name) != 0)
1215 continue;
1216
1217 /*
1218 * We need to find out if all the events are set
1219 * or if all events or cleared, or if we have
1220 * a mixture.
1221 */
1222 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
1223
1224 /*
1225 * If we have a mixture, no need to look further.
1226 */
1227 if (set == 3)
1228 break;
1229 }
1230 mutex_unlock(&event_mutex);
1231
1232 buf[0] = set_to_char[set];
1233 buf[1] = '\n';
1234
1235 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
1236
1237 return ret;
1238 }
1239
1240 static ssize_t
system_enable_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos)1241 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1242 loff_t *ppos)
1243 {
1244 struct trace_subsystem_dir *dir = filp->private_data;
1245 struct event_subsystem *system = dir->subsystem;
1246 const char *name = NULL;
1247 unsigned long val;
1248 ssize_t ret;
1249
1250 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1251 if (ret)
1252 return ret;
1253
1254 ret = tracing_update_buffers();
1255 if (ret < 0)
1256 return ret;
1257
1258 if (val != 0 && val != 1)
1259 return -EINVAL;
1260
1261 /*
1262 * Opening of "enable" adds a ref count to system,
1263 * so the name is safe to use.
1264 */
1265 if (system)
1266 name = system->name;
1267
1268 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
1269 if (ret)
1270 goto out;
1271
1272 ret = cnt;
1273
1274 out:
1275 *ppos += cnt;
1276
1277 return ret;
1278 }
1279
1280 enum {
1281 FORMAT_HEADER = 1,
1282 FORMAT_FIELD_SEPERATOR = 2,
1283 FORMAT_PRINTFMT = 3,
1284 };
1285
f_next(struct seq_file * m,void * v,loff_t * pos)1286 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
1287 {
1288 struct trace_event_call *call = event_file_data(m->private);
1289 struct list_head *common_head = &ftrace_common_fields;
1290 struct list_head *head = trace_get_fields(call);
1291 struct list_head *node = v;
1292
1293 (*pos)++;
1294
1295 switch ((unsigned long)v) {
1296 case FORMAT_HEADER:
1297 node = common_head;
1298 break;
1299
1300 case FORMAT_FIELD_SEPERATOR:
1301 node = head;
1302 break;
1303
1304 case FORMAT_PRINTFMT:
1305 /* all done */
1306 return NULL;
1307 }
1308
1309 node = node->prev;
1310 if (node == common_head)
1311 return (void *)FORMAT_FIELD_SEPERATOR;
1312 else if (node == head)
1313 return (void *)FORMAT_PRINTFMT;
1314 else
1315 return node;
1316 }
1317
f_show(struct seq_file * m,void * v)1318 static int f_show(struct seq_file *m, void *v)
1319 {
1320 struct trace_event_call *call = event_file_data(m->private);
1321 struct ftrace_event_field *field;
1322 const char *array_descriptor;
1323
1324 switch ((unsigned long)v) {
1325 case FORMAT_HEADER:
1326 seq_printf(m, "name: %s\n", trace_event_name(call));
1327 seq_printf(m, "ID: %d\n", call->event.type);
1328 seq_puts(m, "format:\n");
1329 return 0;
1330
1331 case FORMAT_FIELD_SEPERATOR:
1332 seq_putc(m, '\n');
1333 return 0;
1334
1335 case FORMAT_PRINTFMT:
1336 seq_printf(m, "\nprint fmt: %s\n",
1337 call->print_fmt);
1338 return 0;
1339 }
1340
1341 field = list_entry(v, struct ftrace_event_field, link);
1342 /*
1343 * Smartly shows the array type(except dynamic array).
1344 * Normal:
1345 * field:TYPE VAR
1346 * If TYPE := TYPE[LEN], it is shown:
1347 * field:TYPE VAR[LEN]
1348 */
1349 array_descriptor = strchr(field->type, '[');
1350
1351 if (str_has_prefix(field->type, "__data_loc"))
1352 array_descriptor = NULL;
1353
1354 if (!array_descriptor)
1355 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1356 field->type, field->name, field->offset,
1357 field->size, !!field->is_signed);
1358 else
1359 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1360 (int)(array_descriptor - field->type),
1361 field->type, field->name,
1362 array_descriptor, field->offset,
1363 field->size, !!field->is_signed);
1364
1365 return 0;
1366 }
1367
f_start(struct seq_file * m,loff_t * pos)1368 static void *f_start(struct seq_file *m, loff_t *pos)
1369 {
1370 void *p = (void *)FORMAT_HEADER;
1371 loff_t l = 0;
1372
1373 /* ->stop() is called even if ->start() fails */
1374 mutex_lock(&event_mutex);
1375 if (!event_file_data(m->private))
1376 return ERR_PTR(-ENODEV);
1377
1378 while (l < *pos && p)
1379 p = f_next(m, p, &l);
1380
1381 return p;
1382 }
1383
f_stop(struct seq_file * m,void * p)1384 static void f_stop(struct seq_file *m, void *p)
1385 {
1386 mutex_unlock(&event_mutex);
1387 }
1388
1389 static const struct seq_operations trace_format_seq_ops = {
1390 .start = f_start,
1391 .next = f_next,
1392 .stop = f_stop,
1393 .show = f_show,
1394 };
1395
trace_format_open(struct inode * inode,struct file * file)1396 static int trace_format_open(struct inode *inode, struct file *file)
1397 {
1398 struct seq_file *m;
1399 int ret;
1400
1401 /* Do we want to hide event format files on tracefs lockdown? */
1402
1403 ret = seq_open(file, &trace_format_seq_ops);
1404 if (ret < 0)
1405 return ret;
1406
1407 m = file->private_data;
1408 m->private = file;
1409
1410 return 0;
1411 }
1412
1413 static ssize_t
event_id_read(struct file * filp,char __user * ubuf,size_t cnt,loff_t * ppos)1414 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1415 {
1416 int id = (long)event_file_data(filp);
1417 char buf[32];
1418 int len;
1419
1420 if (unlikely(!id))
1421 return -ENODEV;
1422
1423 len = sprintf(buf, "%d\n", id);
1424
1425 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
1426 }
1427
1428 static ssize_t
event_filter_read(struct file * filp,char __user * ubuf,size_t cnt,loff_t * ppos)1429 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1430 loff_t *ppos)
1431 {
1432 struct trace_event_file *file;
1433 struct trace_seq *s;
1434 int r = -ENODEV;
1435
1436 if (*ppos)
1437 return 0;
1438
1439 s = kmalloc(sizeof(*s), GFP_KERNEL);
1440
1441 if (!s)
1442 return -ENOMEM;
1443
1444 trace_seq_init(s);
1445
1446 mutex_lock(&event_mutex);
1447 file = event_file_data(filp);
1448 if (file)
1449 print_event_filter(file, s);
1450 mutex_unlock(&event_mutex);
1451
1452 if (file)
1453 r = simple_read_from_buffer(ubuf, cnt, ppos,
1454 s->buffer, trace_seq_used(s));
1455
1456 kfree(s);
1457
1458 return r;
1459 }
1460
1461 static ssize_t
event_filter_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos)1462 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1463 loff_t *ppos)
1464 {
1465 struct trace_event_file *file;
1466 char *buf;
1467 int err = -ENODEV;
1468
1469 if (cnt >= PAGE_SIZE)
1470 return -EINVAL;
1471
1472 buf = memdup_user_nul(ubuf, cnt);
1473 if (IS_ERR(buf))
1474 return PTR_ERR(buf);
1475
1476 mutex_lock(&event_mutex);
1477 file = event_file_data(filp);
1478 if (file)
1479 err = apply_event_filter(file, buf);
1480 mutex_unlock(&event_mutex);
1481
1482 kfree(buf);
1483 if (err < 0)
1484 return err;
1485
1486 *ppos += cnt;
1487
1488 return cnt;
1489 }
1490
1491 static LIST_HEAD(event_subsystems);
1492
subsystem_open(struct inode * inode,struct file * filp)1493 static int subsystem_open(struct inode *inode, struct file *filp)
1494 {
1495 struct event_subsystem *system = NULL;
1496 struct trace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1497 struct trace_array *tr;
1498 int ret;
1499
1500 if (tracing_is_disabled())
1501 return -ENODEV;
1502
1503 /* Make sure the system still exists */
1504 mutex_lock(&event_mutex);
1505 mutex_lock(&trace_types_lock);
1506 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1507 list_for_each_entry(dir, &tr->systems, list) {
1508 if (dir == inode->i_private) {
1509 /* Don't open systems with no events */
1510 if (dir->nr_events) {
1511 __get_system_dir(dir);
1512 system = dir->subsystem;
1513 }
1514 goto exit_loop;
1515 }
1516 }
1517 }
1518 exit_loop:
1519 mutex_unlock(&trace_types_lock);
1520 mutex_unlock(&event_mutex);
1521
1522 if (!system)
1523 return -ENODEV;
1524
1525 /* Some versions of gcc think dir can be uninitialized here */
1526 WARN_ON(!dir);
1527
1528 /* Still need to increment the ref count of the system */
1529 if (trace_array_get(tr) < 0) {
1530 put_system(dir);
1531 return -ENODEV;
1532 }
1533
1534 ret = tracing_open_generic(inode, filp);
1535 if (ret < 0) {
1536 trace_array_put(tr);
1537 put_system(dir);
1538 }
1539
1540 return ret;
1541 }
1542
system_tr_open(struct inode * inode,struct file * filp)1543 static int system_tr_open(struct inode *inode, struct file *filp)
1544 {
1545 struct trace_subsystem_dir *dir;
1546 struct trace_array *tr = inode->i_private;
1547 int ret;
1548
1549 /* Make a temporary dir that has no system but points to tr */
1550 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1551 if (!dir)
1552 return -ENOMEM;
1553
1554 ret = tracing_open_generic_tr(inode, filp);
1555 if (ret < 0) {
1556 kfree(dir);
1557 return ret;
1558 }
1559 dir->tr = tr;
1560 filp->private_data = dir;
1561
1562 return 0;
1563 }
1564
subsystem_release(struct inode * inode,struct file * file)1565 static int subsystem_release(struct inode *inode, struct file *file)
1566 {
1567 struct trace_subsystem_dir *dir = file->private_data;
1568
1569 trace_array_put(dir->tr);
1570
1571 /*
1572 * If dir->subsystem is NULL, then this is a temporary
1573 * descriptor that was made for a trace_array to enable
1574 * all subsystems.
1575 */
1576 if (dir->subsystem)
1577 put_system(dir);
1578 else
1579 kfree(dir);
1580
1581 return 0;
1582 }
1583
1584 static ssize_t
subsystem_filter_read(struct file * filp,char __user * ubuf,size_t cnt,loff_t * ppos)1585 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1586 loff_t *ppos)
1587 {
1588 struct trace_subsystem_dir *dir = filp->private_data;
1589 struct event_subsystem *system = dir->subsystem;
1590 struct trace_seq *s;
1591 int r;
1592
1593 if (*ppos)
1594 return 0;
1595
1596 s = kmalloc(sizeof(*s), GFP_KERNEL);
1597 if (!s)
1598 return -ENOMEM;
1599
1600 trace_seq_init(s);
1601
1602 print_subsystem_event_filter(system, s);
1603 r = simple_read_from_buffer(ubuf, cnt, ppos,
1604 s->buffer, trace_seq_used(s));
1605
1606 kfree(s);
1607
1608 return r;
1609 }
1610
1611 static ssize_t
subsystem_filter_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos)1612 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1613 loff_t *ppos)
1614 {
1615 struct trace_subsystem_dir *dir = filp->private_data;
1616 char *buf;
1617 int err;
1618
1619 if (cnt >= PAGE_SIZE)
1620 return -EINVAL;
1621
1622 buf = memdup_user_nul(ubuf, cnt);
1623 if (IS_ERR(buf))
1624 return PTR_ERR(buf);
1625
1626 err = apply_subsystem_event_filter(dir, buf);
1627 kfree(buf);
1628 if (err < 0)
1629 return err;
1630
1631 *ppos += cnt;
1632
1633 return cnt;
1634 }
1635
1636 static ssize_t
show_header(struct file * filp,char __user * ubuf,size_t cnt,loff_t * ppos)1637 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1638 {
1639 int (*func)(struct trace_seq *s) = filp->private_data;
1640 struct trace_seq *s;
1641 int r;
1642
1643 if (*ppos)
1644 return 0;
1645
1646 s = kmalloc(sizeof(*s), GFP_KERNEL);
1647 if (!s)
1648 return -ENOMEM;
1649
1650 trace_seq_init(s);
1651
1652 func(s);
1653 r = simple_read_from_buffer(ubuf, cnt, ppos,
1654 s->buffer, trace_seq_used(s));
1655
1656 kfree(s);
1657
1658 return r;
1659 }
1660
ignore_task_cpu(void * data)1661 static void ignore_task_cpu(void *data)
1662 {
1663 struct trace_array *tr = data;
1664 struct trace_pid_list *pid_list;
1665 struct trace_pid_list *no_pid_list;
1666
1667 /*
1668 * This function is called by on_each_cpu() while the
1669 * event_mutex is held.
1670 */
1671 pid_list = rcu_dereference_protected(tr->filtered_pids,
1672 mutex_is_locked(&event_mutex));
1673 no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
1674 mutex_is_locked(&event_mutex));
1675
1676 this_cpu_write(tr->array_buffer.data->ignore_pid,
1677 trace_ignore_this_task(pid_list, no_pid_list, current));
1678 }
1679
register_pid_events(struct trace_array * tr)1680 static void register_pid_events(struct trace_array *tr)
1681 {
1682 /*
1683 * Register a probe that is called before all other probes
1684 * to set ignore_pid if next or prev do not match.
1685 * Register a probe this is called after all other probes
1686 * to only keep ignore_pid set if next pid matches.
1687 */
1688 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre,
1689 tr, INT_MAX);
1690 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post,
1691 tr, 0);
1692
1693 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre,
1694 tr, INT_MAX);
1695 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post,
1696 tr, 0);
1697
1698 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre,
1699 tr, INT_MAX);
1700 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post,
1701 tr, 0);
1702
1703 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre,
1704 tr, INT_MAX);
1705 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post,
1706 tr, 0);
1707 }
1708
1709 static ssize_t
event_pid_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos,int type)1710 event_pid_write(struct file *filp, const char __user *ubuf,
1711 size_t cnt, loff_t *ppos, int type)
1712 {
1713 struct seq_file *m = filp->private_data;
1714 struct trace_array *tr = m->private;
1715 struct trace_pid_list *filtered_pids = NULL;
1716 struct trace_pid_list *other_pids = NULL;
1717 struct trace_pid_list *pid_list;
1718 struct trace_event_file *file;
1719 ssize_t ret;
1720
1721 if (!cnt)
1722 return 0;
1723
1724 ret = tracing_update_buffers();
1725 if (ret < 0)
1726 return ret;
1727
1728 mutex_lock(&event_mutex);
1729
1730 if (type == TRACE_PIDS) {
1731 filtered_pids = rcu_dereference_protected(tr->filtered_pids,
1732 lockdep_is_held(&event_mutex));
1733 other_pids = rcu_dereference_protected(tr->filtered_no_pids,
1734 lockdep_is_held(&event_mutex));
1735 } else {
1736 filtered_pids = rcu_dereference_protected(tr->filtered_no_pids,
1737 lockdep_is_held(&event_mutex));
1738 other_pids = rcu_dereference_protected(tr->filtered_pids,
1739 lockdep_is_held(&event_mutex));
1740 }
1741
1742 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
1743 if (ret < 0)
1744 goto out;
1745
1746 if (type == TRACE_PIDS)
1747 rcu_assign_pointer(tr->filtered_pids, pid_list);
1748 else
1749 rcu_assign_pointer(tr->filtered_no_pids, pid_list);
1750
1751 list_for_each_entry(file, &tr->events, list) {
1752 set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
1753 }
1754
1755 if (filtered_pids) {
1756 tracepoint_synchronize_unregister();
1757 trace_free_pid_list(filtered_pids);
1758 } else if (pid_list && !other_pids) {
1759 register_pid_events(tr);
1760 }
1761
1762 /*
1763 * Ignoring of pids is done at task switch. But we have to
1764 * check for those tasks that are currently running.
1765 * Always do this in case a pid was appended or removed.
1766 */
1767 on_each_cpu(ignore_task_cpu, tr, 1);
1768
1769 out:
1770 mutex_unlock(&event_mutex);
1771
1772 if (ret > 0)
1773 *ppos += ret;
1774
1775 return ret;
1776 }
1777
1778 static ssize_t
ftrace_event_pid_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos)1779 ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
1780 size_t cnt, loff_t *ppos)
1781 {
1782 return event_pid_write(filp, ubuf, cnt, ppos, TRACE_PIDS);
1783 }
1784
1785 static ssize_t
ftrace_event_npid_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos)1786 ftrace_event_npid_write(struct file *filp, const char __user *ubuf,
1787 size_t cnt, loff_t *ppos)
1788 {
1789 return event_pid_write(filp, ubuf, cnt, ppos, TRACE_NO_PIDS);
1790 }
1791
1792 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1793 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1794 static int ftrace_event_set_pid_open(struct inode *inode, struct file *file);
1795 static int ftrace_event_set_npid_open(struct inode *inode, struct file *file);
1796 static int ftrace_event_release(struct inode *inode, struct file *file);
1797
1798 static const struct seq_operations show_event_seq_ops = {
1799 .start = t_start,
1800 .next = t_next,
1801 .show = t_show,
1802 .stop = t_stop,
1803 };
1804
1805 static const struct seq_operations show_set_event_seq_ops = {
1806 .start = s_start,
1807 .next = s_next,
1808 .show = t_show,
1809 .stop = t_stop,
1810 };
1811
1812 static const struct seq_operations show_set_pid_seq_ops = {
1813 .start = p_start,
1814 .next = p_next,
1815 .show = trace_pid_show,
1816 .stop = p_stop,
1817 };
1818
1819 static const struct seq_operations show_set_no_pid_seq_ops = {
1820 .start = np_start,
1821 .next = np_next,
1822 .show = trace_pid_show,
1823 .stop = p_stop,
1824 };
1825
1826 static const struct file_operations ftrace_avail_fops = {
1827 .open = ftrace_event_avail_open,
1828 .read = seq_read,
1829 .llseek = seq_lseek,
1830 .release = seq_release,
1831 };
1832
1833 static const struct file_operations ftrace_set_event_fops = {
1834 .open = ftrace_event_set_open,
1835 .read = seq_read,
1836 .write = ftrace_event_write,
1837 .llseek = seq_lseek,
1838 .release = ftrace_event_release,
1839 };
1840
1841 static const struct file_operations ftrace_set_event_pid_fops = {
1842 .open = ftrace_event_set_pid_open,
1843 .read = seq_read,
1844 .write = ftrace_event_pid_write,
1845 .llseek = seq_lseek,
1846 .release = ftrace_event_release,
1847 };
1848
1849 static const struct file_operations ftrace_set_event_notrace_pid_fops = {
1850 .open = ftrace_event_set_npid_open,
1851 .read = seq_read,
1852 .write = ftrace_event_npid_write,
1853 .llseek = seq_lseek,
1854 .release = ftrace_event_release,
1855 };
1856
1857 static const struct file_operations ftrace_enable_fops = {
1858 .open = tracing_open_file_tr,
1859 .read = event_enable_read,
1860 .write = event_enable_write,
1861 .release = tracing_release_file_tr,
1862 .llseek = default_llseek,
1863 };
1864
1865 static const struct file_operations ftrace_event_format_fops = {
1866 .open = trace_format_open,
1867 .read = seq_read,
1868 .llseek = seq_lseek,
1869 .release = seq_release,
1870 };
1871
1872 static const struct file_operations ftrace_event_id_fops = {
1873 .read = event_id_read,
1874 .llseek = default_llseek,
1875 };
1876
1877 static const struct file_operations ftrace_event_filter_fops = {
1878 .open = tracing_open_file_tr,
1879 .read = event_filter_read,
1880 .write = event_filter_write,
1881 .release = tracing_release_file_tr,
1882 .llseek = default_llseek,
1883 };
1884
1885 static const struct file_operations ftrace_subsystem_filter_fops = {
1886 .open = subsystem_open,
1887 .read = subsystem_filter_read,
1888 .write = subsystem_filter_write,
1889 .llseek = default_llseek,
1890 .release = subsystem_release,
1891 };
1892
1893 static const struct file_operations ftrace_system_enable_fops = {
1894 .open = subsystem_open,
1895 .read = system_enable_read,
1896 .write = system_enable_write,
1897 .llseek = default_llseek,
1898 .release = subsystem_release,
1899 };
1900
1901 static const struct file_operations ftrace_tr_enable_fops = {
1902 .open = system_tr_open,
1903 .read = system_enable_read,
1904 .write = system_enable_write,
1905 .llseek = default_llseek,
1906 .release = subsystem_release,
1907 };
1908
1909 static const struct file_operations ftrace_show_header_fops = {
1910 .open = tracing_open_generic,
1911 .read = show_header,
1912 .llseek = default_llseek,
1913 };
1914
1915 static int
ftrace_event_open(struct inode * inode,struct file * file,const struct seq_operations * seq_ops)1916 ftrace_event_open(struct inode *inode, struct file *file,
1917 const struct seq_operations *seq_ops)
1918 {
1919 struct seq_file *m;
1920 int ret;
1921
1922 ret = security_locked_down(LOCKDOWN_TRACEFS);
1923 if (ret)
1924 return ret;
1925
1926 ret = seq_open(file, seq_ops);
1927 if (ret < 0)
1928 return ret;
1929 m = file->private_data;
1930 /* copy tr over to seq ops */
1931 m->private = inode->i_private;
1932
1933 return ret;
1934 }
1935
ftrace_event_release(struct inode * inode,struct file * file)1936 static int ftrace_event_release(struct inode *inode, struct file *file)
1937 {
1938 struct trace_array *tr = inode->i_private;
1939
1940 trace_array_put(tr);
1941
1942 return seq_release(inode, file);
1943 }
1944
1945 static int
ftrace_event_avail_open(struct inode * inode,struct file * file)1946 ftrace_event_avail_open(struct inode *inode, struct file *file)
1947 {
1948 const struct seq_operations *seq_ops = &show_event_seq_ops;
1949
1950 /* Checks for tracefs lockdown */
1951 return ftrace_event_open(inode, file, seq_ops);
1952 }
1953
1954 static int
ftrace_event_set_open(struct inode * inode,struct file * file)1955 ftrace_event_set_open(struct inode *inode, struct file *file)
1956 {
1957 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1958 struct trace_array *tr = inode->i_private;
1959 int ret;
1960
1961 ret = tracing_check_open_get_tr(tr);
1962 if (ret)
1963 return ret;
1964
1965 if ((file->f_mode & FMODE_WRITE) &&
1966 (file->f_flags & O_TRUNC))
1967 ftrace_clear_events(tr);
1968
1969 ret = ftrace_event_open(inode, file, seq_ops);
1970 if (ret < 0)
1971 trace_array_put(tr);
1972 return ret;
1973 }
1974
1975 static int
ftrace_event_set_pid_open(struct inode * inode,struct file * file)1976 ftrace_event_set_pid_open(struct inode *inode, struct file *file)
1977 {
1978 const struct seq_operations *seq_ops = &show_set_pid_seq_ops;
1979 struct trace_array *tr = inode->i_private;
1980 int ret;
1981
1982 ret = tracing_check_open_get_tr(tr);
1983 if (ret)
1984 return ret;
1985
1986 if ((file->f_mode & FMODE_WRITE) &&
1987 (file->f_flags & O_TRUNC))
1988 ftrace_clear_event_pids(tr, TRACE_PIDS);
1989
1990 ret = ftrace_event_open(inode, file, seq_ops);
1991 if (ret < 0)
1992 trace_array_put(tr);
1993 return ret;
1994 }
1995
1996 static int
ftrace_event_set_npid_open(struct inode * inode,struct file * file)1997 ftrace_event_set_npid_open(struct inode *inode, struct file *file)
1998 {
1999 const struct seq_operations *seq_ops = &show_set_no_pid_seq_ops;
2000 struct trace_array *tr = inode->i_private;
2001 int ret;
2002
2003 ret = tracing_check_open_get_tr(tr);
2004 if (ret)
2005 return ret;
2006
2007 if ((file->f_mode & FMODE_WRITE) &&
2008 (file->f_flags & O_TRUNC))
2009 ftrace_clear_event_pids(tr, TRACE_NO_PIDS);
2010
2011 ret = ftrace_event_open(inode, file, seq_ops);
2012 if (ret < 0)
2013 trace_array_put(tr);
2014 return ret;
2015 }
2016
2017 static struct event_subsystem *
create_new_subsystem(const char * name)2018 create_new_subsystem(const char *name)
2019 {
2020 struct event_subsystem *system;
2021
2022 /* need to create new entry */
2023 system = kmalloc(sizeof(*system), GFP_KERNEL);
2024 if (!system)
2025 return NULL;
2026
2027 system->ref_count = 1;
2028
2029 /* Only allocate if dynamic (kprobes and modules) */
2030 system->name = kstrdup_const(name, GFP_KERNEL);
2031 if (!system->name)
2032 goto out_free;
2033
2034 system->filter = NULL;
2035
2036 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
2037 if (!system->filter)
2038 goto out_free;
2039
2040 list_add(&system->list, &event_subsystems);
2041
2042 return system;
2043
2044 out_free:
2045 kfree_const(system->name);
2046 kfree(system);
2047 return NULL;
2048 }
2049
2050 static struct dentry *
event_subsystem_dir(struct trace_array * tr,const char * name,struct trace_event_file * file,struct dentry * parent)2051 event_subsystem_dir(struct trace_array *tr, const char *name,
2052 struct trace_event_file *file, struct dentry *parent)
2053 {
2054 struct trace_subsystem_dir *dir;
2055 struct event_subsystem *system;
2056 struct dentry *entry;
2057
2058 /* First see if we did not already create this dir */
2059 list_for_each_entry(dir, &tr->systems, list) {
2060 system = dir->subsystem;
2061 if (strcmp(system->name, name) == 0) {
2062 dir->nr_events++;
2063 file->system = dir;
2064 return dir->entry;
2065 }
2066 }
2067
2068 /* Now see if the system itself exists. */
2069 list_for_each_entry(system, &event_subsystems, list) {
2070 if (strcmp(system->name, name) == 0)
2071 break;
2072 }
2073 /* Reset system variable when not found */
2074 if (&system->list == &event_subsystems)
2075 system = NULL;
2076
2077 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
2078 if (!dir)
2079 goto out_fail;
2080
2081 if (!system) {
2082 system = create_new_subsystem(name);
2083 if (!system)
2084 goto out_free;
2085 } else
2086 __get_system(system);
2087
2088 dir->entry = tracefs_create_dir(name, parent);
2089 if (!dir->entry) {
2090 pr_warn("Failed to create system directory %s\n", name);
2091 __put_system(system);
2092 goto out_free;
2093 }
2094
2095 dir->tr = tr;
2096 dir->ref_count = 1;
2097 dir->nr_events = 1;
2098 dir->subsystem = system;
2099 file->system = dir;
2100
2101 entry = tracefs_create_file("filter", 0644, dir->entry, dir,
2102 &ftrace_subsystem_filter_fops);
2103 if (!entry) {
2104 kfree(system->filter);
2105 system->filter = NULL;
2106 pr_warn("Could not create tracefs '%s/filter' entry\n", name);
2107 }
2108
2109 trace_create_file("enable", 0644, dir->entry, dir,
2110 &ftrace_system_enable_fops);
2111
2112 list_add(&dir->list, &tr->systems);
2113
2114 return dir->entry;
2115
2116 out_free:
2117 kfree(dir);
2118 out_fail:
2119 /* Only print this message if failed on memory allocation */
2120 if (!dir || !system)
2121 pr_warn("No memory to create event subsystem %s\n", name);
2122 return NULL;
2123 }
2124
2125 static int
event_define_fields(struct trace_event_call * call)2126 event_define_fields(struct trace_event_call *call)
2127 {
2128 struct list_head *head;
2129 int ret = 0;
2130
2131 /*
2132 * Other events may have the same class. Only update
2133 * the fields if they are not already defined.
2134 */
2135 head = trace_get_fields(call);
2136 if (list_empty(head)) {
2137 struct trace_event_fields *field = call->class->fields_array;
2138 unsigned int offset = sizeof(struct trace_entry);
2139
2140 for (; field->type; field++) {
2141 if (field->type == TRACE_FUNCTION_TYPE) {
2142 field->define_fields(call);
2143 break;
2144 }
2145
2146 offset = ALIGN(offset, field->align);
2147 ret = trace_define_field(call, field->type, field->name,
2148 offset, field->size,
2149 field->is_signed, field->filter_type);
2150 if (WARN_ON_ONCE(ret)) {
2151 pr_err("error code is %d\n", ret);
2152 break;
2153 }
2154
2155 offset += field->size;
2156 }
2157 }
2158
2159 return ret;
2160 }
2161
2162 static int
event_create_dir(struct dentry * parent,struct trace_event_file * file)2163 event_create_dir(struct dentry *parent, struct trace_event_file *file)
2164 {
2165 struct trace_event_call *call = file->event_call;
2166 struct trace_array *tr = file->tr;
2167 struct dentry *d_events;
2168 const char *name;
2169 int ret;
2170
2171 /*
2172 * If the trace point header did not define TRACE_SYSTEM
2173 * then the system would be called "TRACE_SYSTEM".
2174 */
2175 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
2176 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
2177 if (!d_events)
2178 return -ENOMEM;
2179 } else
2180 d_events = parent;
2181
2182 name = trace_event_name(call);
2183 file->dir = tracefs_create_dir(name, d_events);
2184 if (!file->dir) {
2185 pr_warn("Could not create tracefs '%s' directory\n", name);
2186 return -1;
2187 }
2188
2189 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
2190 trace_create_file("enable", 0644, file->dir, file,
2191 &ftrace_enable_fops);
2192
2193 #ifdef CONFIG_PERF_EVENTS
2194 if (call->event.type && call->class->reg)
2195 trace_create_file("id", 0444, file->dir,
2196 (void *)(long)call->event.type,
2197 &ftrace_event_id_fops);
2198 #endif
2199
2200 ret = event_define_fields(call);
2201 if (ret < 0) {
2202 pr_warn("Could not initialize trace point events/%s\n", name);
2203 return ret;
2204 }
2205
2206 /*
2207 * Only event directories that can be enabled should have
2208 * triggers or filters.
2209 */
2210 if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)) {
2211 trace_create_file("filter", 0644, file->dir, file,
2212 &ftrace_event_filter_fops);
2213
2214 trace_create_file("trigger", 0644, file->dir, file,
2215 &event_trigger_fops);
2216 }
2217
2218 #ifdef CONFIG_HIST_TRIGGERS
2219 trace_create_file("hist", 0444, file->dir, file,
2220 &event_hist_fops);
2221 #endif
2222 #ifdef CONFIG_HIST_TRIGGERS_DEBUG
2223 trace_create_file("hist_debug", 0444, file->dir, file,
2224 &event_hist_debug_fops);
2225 #endif
2226 trace_create_file("format", 0444, file->dir, call,
2227 &ftrace_event_format_fops);
2228
2229 #ifdef CONFIG_TRACE_EVENT_INJECT
2230 if (call->event.type && call->class->reg)
2231 trace_create_file("inject", 0200, file->dir, file,
2232 &event_inject_fops);
2233 #endif
2234
2235 return 0;
2236 }
2237
remove_event_from_tracers(struct trace_event_call * call)2238 static void remove_event_from_tracers(struct trace_event_call *call)
2239 {
2240 struct trace_event_file *file;
2241 struct trace_array *tr;
2242
2243 do_for_each_event_file_safe(tr, file) {
2244 if (file->event_call != call)
2245 continue;
2246
2247 remove_event_file_dir(file);
2248 /*
2249 * The do_for_each_event_file_safe() is
2250 * a double loop. After finding the call for this
2251 * trace_array, we use break to jump to the next
2252 * trace_array.
2253 */
2254 break;
2255 } while_for_each_event_file();
2256 }
2257
event_remove(struct trace_event_call * call)2258 static void event_remove(struct trace_event_call *call)
2259 {
2260 struct trace_array *tr;
2261 struct trace_event_file *file;
2262
2263 do_for_each_event_file(tr, file) {
2264 if (file->event_call != call)
2265 continue;
2266
2267 if (file->flags & EVENT_FILE_FL_WAS_ENABLED)
2268 tr->clear_trace = true;
2269
2270 ftrace_event_enable_disable(file, 0);
2271 /*
2272 * The do_for_each_event_file() is
2273 * a double loop. After finding the call for this
2274 * trace_array, we use break to jump to the next
2275 * trace_array.
2276 */
2277 break;
2278 } while_for_each_event_file();
2279
2280 if (call->event.funcs)
2281 __unregister_trace_event(&call->event);
2282 remove_event_from_tracers(call);
2283 list_del(&call->list);
2284 }
2285
event_init(struct trace_event_call * call)2286 static int event_init(struct trace_event_call *call)
2287 {
2288 int ret = 0;
2289 const char *name;
2290
2291 name = trace_event_name(call);
2292 if (WARN_ON(!name))
2293 return -EINVAL;
2294
2295 if (call->class->raw_init) {
2296 ret = call->class->raw_init(call);
2297 if (ret < 0 && ret != -ENOSYS)
2298 pr_warn("Could not initialize trace events/%s\n", name);
2299 }
2300
2301 return ret;
2302 }
2303
2304 static int
__register_event(struct trace_event_call * call,struct module * mod)2305 __register_event(struct trace_event_call *call, struct module *mod)
2306 {
2307 int ret;
2308
2309 ret = event_init(call);
2310 if (ret < 0)
2311 return ret;
2312
2313 list_add(&call->list, &ftrace_events);
2314 call->mod = mod;
2315
2316 return 0;
2317 }
2318
eval_replace(char * ptr,struct trace_eval_map * map,int len)2319 static char *eval_replace(char *ptr, struct trace_eval_map *map, int len)
2320 {
2321 int rlen;
2322 int elen;
2323
2324 /* Find the length of the eval value as a string */
2325 elen = snprintf(ptr, 0, "%ld", map->eval_value);
2326 /* Make sure there's enough room to replace the string with the value */
2327 if (len < elen)
2328 return NULL;
2329
2330 snprintf(ptr, elen + 1, "%ld", map->eval_value);
2331
2332 /* Get the rest of the string of ptr */
2333 rlen = strlen(ptr + len);
2334 memmove(ptr + elen, ptr + len, rlen);
2335 /* Make sure we end the new string */
2336 ptr[elen + rlen] = 0;
2337
2338 return ptr + elen;
2339 }
2340
update_event_printk(struct trace_event_call * call,struct trace_eval_map * map)2341 static void update_event_printk(struct trace_event_call *call,
2342 struct trace_eval_map *map)
2343 {
2344 char *ptr;
2345 int quote = 0;
2346 int len = strlen(map->eval_string);
2347
2348 for (ptr = call->print_fmt; *ptr; ptr++) {
2349 if (*ptr == '\\') {
2350 ptr++;
2351 /* paranoid */
2352 if (!*ptr)
2353 break;
2354 continue;
2355 }
2356 if (*ptr == '"') {
2357 quote ^= 1;
2358 continue;
2359 }
2360 if (quote)
2361 continue;
2362 if (isdigit(*ptr)) {
2363 /* skip numbers */
2364 do {
2365 ptr++;
2366 /* Check for alpha chars like ULL */
2367 } while (isalnum(*ptr));
2368 if (!*ptr)
2369 break;
2370 /*
2371 * A number must have some kind of delimiter after
2372 * it, and we can ignore that too.
2373 */
2374 continue;
2375 }
2376 if (isalpha(*ptr) || *ptr == '_') {
2377 if (strncmp(map->eval_string, ptr, len) == 0 &&
2378 !isalnum(ptr[len]) && ptr[len] != '_') {
2379 ptr = eval_replace(ptr, map, len);
2380 /* enum/sizeof string smaller than value */
2381 if (WARN_ON_ONCE(!ptr))
2382 return;
2383 /*
2384 * No need to decrement here, as eval_replace()
2385 * returns the pointer to the character passed
2386 * the eval, and two evals can not be placed
2387 * back to back without something in between.
2388 * We can skip that something in between.
2389 */
2390 continue;
2391 }
2392 skip_more:
2393 do {
2394 ptr++;
2395 } while (isalnum(*ptr) || *ptr == '_');
2396 if (!*ptr)
2397 break;
2398 /*
2399 * If what comes after this variable is a '.' or
2400 * '->' then we can continue to ignore that string.
2401 */
2402 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
2403 ptr += *ptr == '.' ? 1 : 2;
2404 if (!*ptr)
2405 break;
2406 goto skip_more;
2407 }
2408 /*
2409 * Once again, we can skip the delimiter that came
2410 * after the string.
2411 */
2412 continue;
2413 }
2414 }
2415 }
2416
trace_event_eval_update(struct trace_eval_map ** map,int len)2417 void trace_event_eval_update(struct trace_eval_map **map, int len)
2418 {
2419 struct trace_event_call *call, *p;
2420 const char *last_system = NULL;
2421 bool first = false;
2422 int last_i;
2423 int i;
2424
2425 down_write(&trace_event_sem);
2426 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2427 /* events are usually grouped together with systems */
2428 if (!last_system || call->class->system != last_system) {
2429 first = true;
2430 last_i = 0;
2431 last_system = call->class->system;
2432 }
2433
2434 /*
2435 * Since calls are grouped by systems, the likelyhood that the
2436 * next call in the iteration belongs to the same system as the
2437 * previous call is high. As an optimization, we skip seaching
2438 * for a map[] that matches the call's system if the last call
2439 * was from the same system. That's what last_i is for. If the
2440 * call has the same system as the previous call, then last_i
2441 * will be the index of the first map[] that has a matching
2442 * system.
2443 */
2444 for (i = last_i; i < len; i++) {
2445 if (call->class->system == map[i]->system) {
2446 /* Save the first system if need be */
2447 if (first) {
2448 last_i = i;
2449 first = false;
2450 }
2451 update_event_printk(call, map[i]);
2452 }
2453 }
2454 cond_resched();
2455 }
2456 up_write(&trace_event_sem);
2457 }
2458
2459 static struct trace_event_file *
trace_create_new_event(struct trace_event_call * call,struct trace_array * tr)2460 trace_create_new_event(struct trace_event_call *call,
2461 struct trace_array *tr)
2462 {
2463 struct trace_pid_list *no_pid_list;
2464 struct trace_pid_list *pid_list;
2465 struct trace_event_file *file;
2466
2467 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
2468 if (!file)
2469 return NULL;
2470
2471 pid_list = rcu_dereference_protected(tr->filtered_pids,
2472 lockdep_is_held(&event_mutex));
2473 no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
2474 lockdep_is_held(&event_mutex));
2475
2476 if (pid_list || no_pid_list)
2477 file->flags |= EVENT_FILE_FL_PID_FILTER;
2478
2479 file->event_call = call;
2480 file->tr = tr;
2481 atomic_set(&file->sm_ref, 0);
2482 atomic_set(&file->tm_ref, 0);
2483 INIT_LIST_HEAD(&file->triggers);
2484 list_add(&file->list, &tr->events);
2485
2486 return file;
2487 }
2488
2489 /* Add an event to a trace directory */
2490 static int
__trace_add_new_event(struct trace_event_call * call,struct trace_array * tr)2491 __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
2492 {
2493 struct trace_event_file *file;
2494
2495 file = trace_create_new_event(call, tr);
2496 if (!file)
2497 return -ENOMEM;
2498
2499 if (eventdir_initialized)
2500 return event_create_dir(tr->event_dir, file);
2501 else
2502 return event_define_fields(call);
2503 }
2504
2505 /*
2506 * Just create a decriptor for early init. A descriptor is required
2507 * for enabling events at boot. We want to enable events before
2508 * the filesystem is initialized.
2509 */
2510 static int
__trace_early_add_new_event(struct trace_event_call * call,struct trace_array * tr)2511 __trace_early_add_new_event(struct trace_event_call *call,
2512 struct trace_array *tr)
2513 {
2514 struct trace_event_file *file;
2515
2516 file = trace_create_new_event(call, tr);
2517 if (!file)
2518 return -ENOMEM;
2519
2520 return event_define_fields(call);
2521 }
2522
2523 struct ftrace_module_file_ops;
2524 static void __add_event_to_tracers(struct trace_event_call *call);
2525
2526 /* Add an additional event_call dynamically */
trace_add_event_call(struct trace_event_call * call)2527 int trace_add_event_call(struct trace_event_call *call)
2528 {
2529 int ret;
2530 lockdep_assert_held(&event_mutex);
2531
2532 mutex_lock(&trace_types_lock);
2533
2534 ret = __register_event(call, NULL);
2535 if (ret >= 0)
2536 __add_event_to_tracers(call);
2537
2538 mutex_unlock(&trace_types_lock);
2539 return ret;
2540 }
2541
2542 /*
2543 * Must be called under locking of trace_types_lock, event_mutex and
2544 * trace_event_sem.
2545 */
__trace_remove_event_call(struct trace_event_call * call)2546 static void __trace_remove_event_call(struct trace_event_call *call)
2547 {
2548 event_remove(call);
2549 trace_destroy_fields(call);
2550 free_event_filter(call->filter);
2551 call->filter = NULL;
2552 }
2553
probe_remove_event_call(struct trace_event_call * call)2554 static int probe_remove_event_call(struct trace_event_call *call)
2555 {
2556 struct trace_array *tr;
2557 struct trace_event_file *file;
2558
2559 #ifdef CONFIG_PERF_EVENTS
2560 if (call->perf_refcount)
2561 return -EBUSY;
2562 #endif
2563 do_for_each_event_file(tr, file) {
2564 if (file->event_call != call)
2565 continue;
2566 /*
2567 * We can't rely on ftrace_event_enable_disable(enable => 0)
2568 * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
2569 * TRACE_REG_UNREGISTER.
2570 */
2571 if (file->flags & EVENT_FILE_FL_ENABLED)
2572 goto busy;
2573
2574 if (file->flags & EVENT_FILE_FL_WAS_ENABLED)
2575 tr->clear_trace = true;
2576 /*
2577 * The do_for_each_event_file_safe() is
2578 * a double loop. After finding the call for this
2579 * trace_array, we use break to jump to the next
2580 * trace_array.
2581 */
2582 break;
2583 } while_for_each_event_file();
2584
2585 __trace_remove_event_call(call);
2586
2587 return 0;
2588 busy:
2589 /* No need to clear the trace now */
2590 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
2591 tr->clear_trace = false;
2592 }
2593 return -EBUSY;
2594 }
2595
2596 /* Remove an event_call */
trace_remove_event_call(struct trace_event_call * call)2597 int trace_remove_event_call(struct trace_event_call *call)
2598 {
2599 int ret;
2600
2601 lockdep_assert_held(&event_mutex);
2602
2603 mutex_lock(&trace_types_lock);
2604 down_write(&trace_event_sem);
2605 ret = probe_remove_event_call(call);
2606 up_write(&trace_event_sem);
2607 mutex_unlock(&trace_types_lock);
2608
2609 return ret;
2610 }
2611
2612 #define for_each_event(event, start, end) \
2613 for (event = start; \
2614 (unsigned long)event < (unsigned long)end; \
2615 event++)
2616
2617 #ifdef CONFIG_MODULES
2618
trace_module_add_events(struct module * mod)2619 static void trace_module_add_events(struct module *mod)
2620 {
2621 struct trace_event_call **call, **start, **end;
2622
2623 if (!mod->num_trace_events)
2624 return;
2625
2626 /* Don't add infrastructure for mods without tracepoints */
2627 if (trace_module_has_bad_taint(mod)) {
2628 pr_err("%s: module has bad taint, not creating trace events\n",
2629 mod->name);
2630 return;
2631 }
2632
2633 start = mod->trace_events;
2634 end = mod->trace_events + mod->num_trace_events;
2635
2636 for_each_event(call, start, end) {
2637 __register_event(*call, mod);
2638 __add_event_to_tracers(*call);
2639 }
2640 }
2641
trace_module_remove_events(struct module * mod)2642 static void trace_module_remove_events(struct module *mod)
2643 {
2644 struct trace_event_call *call, *p;
2645
2646 down_write(&trace_event_sem);
2647 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2648 if (call->mod == mod)
2649 __trace_remove_event_call(call);
2650 }
2651 up_write(&trace_event_sem);
2652
2653 /*
2654 * It is safest to reset the ring buffer if the module being unloaded
2655 * registered any events that were used. The only worry is if
2656 * a new module gets loaded, and takes on the same id as the events
2657 * of this module. When printing out the buffer, traced events left
2658 * over from this module may be passed to the new module events and
2659 * unexpected results may occur.
2660 */
2661 tracing_reset_all_online_cpus_unlocked();
2662 }
2663
trace_module_notify(struct notifier_block * self,unsigned long val,void * data)2664 static int trace_module_notify(struct notifier_block *self,
2665 unsigned long val, void *data)
2666 {
2667 struct module *mod = data;
2668
2669 mutex_lock(&event_mutex);
2670 mutex_lock(&trace_types_lock);
2671 switch (val) {
2672 case MODULE_STATE_COMING:
2673 trace_module_add_events(mod);
2674 break;
2675 case MODULE_STATE_GOING:
2676 trace_module_remove_events(mod);
2677 break;
2678 }
2679 mutex_unlock(&trace_types_lock);
2680 mutex_unlock(&event_mutex);
2681
2682 return NOTIFY_OK;
2683 }
2684
2685 static struct notifier_block trace_module_nb = {
2686 .notifier_call = trace_module_notify,
2687 .priority = 1, /* higher than trace.c module notify */
2688 };
2689 #endif /* CONFIG_MODULES */
2690
2691 /* Create a new event directory structure for a trace directory. */
2692 static void
__trace_add_event_dirs(struct trace_array * tr)2693 __trace_add_event_dirs(struct trace_array *tr)
2694 {
2695 struct trace_event_call *call;
2696 int ret;
2697
2698 list_for_each_entry(call, &ftrace_events, list) {
2699 ret = __trace_add_new_event(call, tr);
2700 if (ret < 0)
2701 pr_warn("Could not create directory for event %s\n",
2702 trace_event_name(call));
2703 }
2704 }
2705
2706 /* Returns any file that matches the system and event */
2707 struct trace_event_file *
__find_event_file(struct trace_array * tr,const char * system,const char * event)2708 __find_event_file(struct trace_array *tr, const char *system, const char *event)
2709 {
2710 struct trace_event_file *file;
2711 struct trace_event_call *call;
2712 const char *name;
2713
2714 list_for_each_entry(file, &tr->events, list) {
2715
2716 call = file->event_call;
2717 name = trace_event_name(call);
2718
2719 if (!name || !call->class)
2720 continue;
2721
2722 if (strcmp(event, name) == 0 &&
2723 strcmp(system, call->class->system) == 0)
2724 return file;
2725 }
2726 return NULL;
2727 }
2728
2729 /* Returns valid trace event files that match system and event */
2730 struct trace_event_file *
find_event_file(struct trace_array * tr,const char * system,const char * event)2731 find_event_file(struct trace_array *tr, const char *system, const char *event)
2732 {
2733 struct trace_event_file *file;
2734
2735 file = __find_event_file(tr, system, event);
2736 if (!file || !file->event_call->class->reg ||
2737 file->event_call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
2738 return NULL;
2739
2740 return file;
2741 }
2742
2743 /**
2744 * trace_get_event_file - Find and return a trace event file
2745 * @instance: The name of the trace instance containing the event
2746 * @system: The name of the system containing the event
2747 * @event: The name of the event
2748 *
2749 * Return a trace event file given the trace instance name, trace
2750 * system, and trace event name. If the instance name is NULL, it
2751 * refers to the top-level trace array.
2752 *
2753 * This function will look it up and return it if found, after calling
2754 * trace_array_get() to prevent the instance from going away, and
2755 * increment the event's module refcount to prevent it from being
2756 * removed.
2757 *
2758 * To release the file, call trace_put_event_file(), which will call
2759 * trace_array_put() and decrement the event's module refcount.
2760 *
2761 * Return: The trace event on success, ERR_PTR otherwise.
2762 */
trace_get_event_file(const char * instance,const char * system,const char * event)2763 struct trace_event_file *trace_get_event_file(const char *instance,
2764 const char *system,
2765 const char *event)
2766 {
2767 struct trace_array *tr = top_trace_array();
2768 struct trace_event_file *file = NULL;
2769 int ret = -EINVAL;
2770
2771 if (instance) {
2772 tr = trace_array_find_get(instance);
2773 if (!tr)
2774 return ERR_PTR(-ENOENT);
2775 } else {
2776 ret = trace_array_get(tr);
2777 if (ret)
2778 return ERR_PTR(ret);
2779 }
2780
2781 mutex_lock(&event_mutex);
2782
2783 file = find_event_file(tr, system, event);
2784 if (!file) {
2785 trace_array_put(tr);
2786 ret = -EINVAL;
2787 goto out;
2788 }
2789
2790 /* Don't let event modules unload while in use */
2791 ret = try_module_get(file->event_call->mod);
2792 if (!ret) {
2793 trace_array_put(tr);
2794 ret = -EBUSY;
2795 goto out;
2796 }
2797
2798 ret = 0;
2799 out:
2800 mutex_unlock(&event_mutex);
2801
2802 if (ret)
2803 file = ERR_PTR(ret);
2804
2805 return file;
2806 }
2807 EXPORT_SYMBOL_GPL(trace_get_event_file);
2808
2809 /**
2810 * trace_put_event_file - Release a file from trace_get_event_file()
2811 * @file: The trace event file
2812 *
2813 * If a file was retrieved using trace_get_event_file(), this should
2814 * be called when it's no longer needed. It will cancel the previous
2815 * trace_array_get() called by that function, and decrement the
2816 * event's module refcount.
2817 */
trace_put_event_file(struct trace_event_file * file)2818 void trace_put_event_file(struct trace_event_file *file)
2819 {
2820 mutex_lock(&event_mutex);
2821 module_put(file->event_call->mod);
2822 mutex_unlock(&event_mutex);
2823
2824 trace_array_put(file->tr);
2825 }
2826 EXPORT_SYMBOL_GPL(trace_put_event_file);
2827
2828 #ifdef CONFIG_DYNAMIC_FTRACE
2829
2830 /* Avoid typos */
2831 #define ENABLE_EVENT_STR "enable_event"
2832 #define DISABLE_EVENT_STR "disable_event"
2833
2834 struct event_probe_data {
2835 struct trace_event_file *file;
2836 unsigned long count;
2837 int ref;
2838 bool enable;
2839 };
2840
update_event_probe(struct event_probe_data * data)2841 static void update_event_probe(struct event_probe_data *data)
2842 {
2843 if (data->enable)
2844 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2845 else
2846 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
2847 }
2848
2849 static void
event_enable_probe(unsigned long ip,unsigned long parent_ip,struct trace_array * tr,struct ftrace_probe_ops * ops,void * data)2850 event_enable_probe(unsigned long ip, unsigned long parent_ip,
2851 struct trace_array *tr, struct ftrace_probe_ops *ops,
2852 void *data)
2853 {
2854 struct ftrace_func_mapper *mapper = data;
2855 struct event_probe_data *edata;
2856 void **pdata;
2857
2858 pdata = ftrace_func_mapper_find_ip(mapper, ip);
2859 if (!pdata || !*pdata)
2860 return;
2861
2862 edata = *pdata;
2863 update_event_probe(edata);
2864 }
2865
2866 static void
event_enable_count_probe(unsigned long ip,unsigned long parent_ip,struct trace_array * tr,struct ftrace_probe_ops * ops,void * data)2867 event_enable_count_probe(unsigned long ip, unsigned long parent_ip,
2868 struct trace_array *tr, struct ftrace_probe_ops *ops,
2869 void *data)
2870 {
2871 struct ftrace_func_mapper *mapper = data;
2872 struct event_probe_data *edata;
2873 void **pdata;
2874
2875 pdata = ftrace_func_mapper_find_ip(mapper, ip);
2876 if (!pdata || !*pdata)
2877 return;
2878
2879 edata = *pdata;
2880
2881 if (!edata->count)
2882 return;
2883
2884 /* Skip if the event is in a state we want to switch to */
2885 if (edata->enable == !(edata->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
2886 return;
2887
2888 if (edata->count != -1)
2889 (edata->count)--;
2890
2891 update_event_probe(edata);
2892 }
2893
2894 static int
event_enable_print(struct seq_file * m,unsigned long ip,struct ftrace_probe_ops * ops,void * data)2895 event_enable_print(struct seq_file *m, unsigned long ip,
2896 struct ftrace_probe_ops *ops, void *data)
2897 {
2898 struct ftrace_func_mapper *mapper = data;
2899 struct event_probe_data *edata;
2900 void **pdata;
2901
2902 pdata = ftrace_func_mapper_find_ip(mapper, ip);
2903
2904 if (WARN_ON_ONCE(!pdata || !*pdata))
2905 return 0;
2906
2907 edata = *pdata;
2908
2909 seq_printf(m, "%ps:", (void *)ip);
2910
2911 seq_printf(m, "%s:%s:%s",
2912 edata->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2913 edata->file->event_call->class->system,
2914 trace_event_name(edata->file->event_call));
2915
2916 if (edata->count == -1)
2917 seq_puts(m, ":unlimited\n");
2918 else
2919 seq_printf(m, ":count=%ld\n", edata->count);
2920
2921 return 0;
2922 }
2923
2924 static int
event_enable_init(struct ftrace_probe_ops * ops,struct trace_array * tr,unsigned long ip,void * init_data,void ** data)2925 event_enable_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
2926 unsigned long ip, void *init_data, void **data)
2927 {
2928 struct ftrace_func_mapper *mapper = *data;
2929 struct event_probe_data *edata = init_data;
2930 int ret;
2931
2932 if (!mapper) {
2933 mapper = allocate_ftrace_func_mapper();
2934 if (!mapper)
2935 return -ENODEV;
2936 *data = mapper;
2937 }
2938
2939 ret = ftrace_func_mapper_add_ip(mapper, ip, edata);
2940 if (ret < 0)
2941 return ret;
2942
2943 edata->ref++;
2944
2945 return 0;
2946 }
2947
free_probe_data(void * data)2948 static int free_probe_data(void *data)
2949 {
2950 struct event_probe_data *edata = data;
2951
2952 edata->ref--;
2953 if (!edata->ref) {
2954 /* Remove the SOFT_MODE flag */
2955 __ftrace_event_enable_disable(edata->file, 0, 1);
2956 module_put(edata->file->event_call->mod);
2957 kfree(edata);
2958 }
2959 return 0;
2960 }
2961
2962 static void
event_enable_free(struct ftrace_probe_ops * ops,struct trace_array * tr,unsigned long ip,void * data)2963 event_enable_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
2964 unsigned long ip, void *data)
2965 {
2966 struct ftrace_func_mapper *mapper = data;
2967 struct event_probe_data *edata;
2968
2969 if (!ip) {
2970 if (!mapper)
2971 return;
2972 free_ftrace_func_mapper(mapper, free_probe_data);
2973 return;
2974 }
2975
2976 edata = ftrace_func_mapper_remove_ip(mapper, ip);
2977
2978 if (WARN_ON_ONCE(!edata))
2979 return;
2980
2981 if (WARN_ON_ONCE(edata->ref <= 0))
2982 return;
2983
2984 free_probe_data(edata);
2985 }
2986
2987 static struct ftrace_probe_ops event_enable_probe_ops = {
2988 .func = event_enable_probe,
2989 .print = event_enable_print,
2990 .init = event_enable_init,
2991 .free = event_enable_free,
2992 };
2993
2994 static struct ftrace_probe_ops event_enable_count_probe_ops = {
2995 .func = event_enable_count_probe,
2996 .print = event_enable_print,
2997 .init = event_enable_init,
2998 .free = event_enable_free,
2999 };
3000
3001 static struct ftrace_probe_ops event_disable_probe_ops = {
3002 .func = event_enable_probe,
3003 .print = event_enable_print,
3004 .init = event_enable_init,
3005 .free = event_enable_free,
3006 };
3007
3008 static struct ftrace_probe_ops event_disable_count_probe_ops = {
3009 .func = event_enable_count_probe,
3010 .print = event_enable_print,
3011 .init = event_enable_init,
3012 .free = event_enable_free,
3013 };
3014
3015 static int
event_enable_func(struct trace_array * tr,struct ftrace_hash * hash,char * glob,char * cmd,char * param,int enabled)3016 event_enable_func(struct trace_array *tr, struct ftrace_hash *hash,
3017 char *glob, char *cmd, char *param, int enabled)
3018 {
3019 struct trace_event_file *file;
3020 struct ftrace_probe_ops *ops;
3021 struct event_probe_data *data;
3022 const char *system;
3023 const char *event;
3024 char *number;
3025 bool enable;
3026 int ret;
3027
3028 if (!tr)
3029 return -ENODEV;
3030
3031 /* hash funcs only work with set_ftrace_filter */
3032 if (!enabled || !param)
3033 return -EINVAL;
3034
3035 system = strsep(¶m, ":");
3036 if (!param)
3037 return -EINVAL;
3038
3039 event = strsep(¶m, ":");
3040
3041 mutex_lock(&event_mutex);
3042
3043 ret = -EINVAL;
3044 file = find_event_file(tr, system, event);
3045 if (!file)
3046 goto out;
3047
3048 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
3049
3050 if (enable)
3051 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
3052 else
3053 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
3054
3055 if (glob[0] == '!') {
3056 ret = unregister_ftrace_function_probe_func(glob+1, tr, ops);
3057 goto out;
3058 }
3059
3060 ret = -ENOMEM;
3061
3062 data = kzalloc(sizeof(*data), GFP_KERNEL);
3063 if (!data)
3064 goto out;
3065
3066 data->enable = enable;
3067 data->count = -1;
3068 data->file = file;
3069
3070 if (!param)
3071 goto out_reg;
3072
3073 number = strsep(¶m, ":");
3074
3075 ret = -EINVAL;
3076 if (!strlen(number))
3077 goto out_free;
3078
3079 /*
3080 * We use the callback data field (which is a pointer)
3081 * as our counter.
3082 */
3083 ret = kstrtoul(number, 0, &data->count);
3084 if (ret)
3085 goto out_free;
3086
3087 out_reg:
3088 /* Don't let event modules unload while probe registered */
3089 ret = try_module_get(file->event_call->mod);
3090 if (!ret) {
3091 ret = -EBUSY;
3092 goto out_free;
3093 }
3094
3095 ret = __ftrace_event_enable_disable(file, 1, 1);
3096 if (ret < 0)
3097 goto out_put;
3098
3099 ret = register_ftrace_function_probe(glob, tr, ops, data);
3100 /*
3101 * The above returns on success the # of functions enabled,
3102 * but if it didn't find any functions it returns zero.
3103 * Consider no functions a failure too.
3104 */
3105 if (!ret) {
3106 ret = -ENOENT;
3107 goto out_disable;
3108 } else if (ret < 0)
3109 goto out_disable;
3110 /* Just return zero, not the number of enabled functions */
3111 ret = 0;
3112 out:
3113 mutex_unlock(&event_mutex);
3114 return ret;
3115
3116 out_disable:
3117 __ftrace_event_enable_disable(file, 0, 1);
3118 out_put:
3119 module_put(file->event_call->mod);
3120 out_free:
3121 kfree(data);
3122 goto out;
3123 }
3124
3125 static struct ftrace_func_command event_enable_cmd = {
3126 .name = ENABLE_EVENT_STR,
3127 .func = event_enable_func,
3128 };
3129
3130 static struct ftrace_func_command event_disable_cmd = {
3131 .name = DISABLE_EVENT_STR,
3132 .func = event_enable_func,
3133 };
3134
register_event_cmds(void)3135 static __init int register_event_cmds(void)
3136 {
3137 int ret;
3138
3139 ret = register_ftrace_command(&event_enable_cmd);
3140 if (WARN_ON(ret < 0))
3141 return ret;
3142 ret = register_ftrace_command(&event_disable_cmd);
3143 if (WARN_ON(ret < 0))
3144 unregister_ftrace_command(&event_enable_cmd);
3145 return ret;
3146 }
3147 #else
register_event_cmds(void)3148 static inline int register_event_cmds(void) { return 0; }
3149 #endif /* CONFIG_DYNAMIC_FTRACE */
3150
3151 /*
3152 * The top level array and trace arrays created by boot-time tracing
3153 * have already had its trace_event_file descriptors created in order
3154 * to allow for early events to be recorded.
3155 * This function is called after the tracefs has been initialized,
3156 * and we now have to create the files associated to the events.
3157 */
__trace_early_add_event_dirs(struct trace_array * tr)3158 static void __trace_early_add_event_dirs(struct trace_array *tr)
3159 {
3160 struct trace_event_file *file;
3161 int ret;
3162
3163
3164 list_for_each_entry(file, &tr->events, list) {
3165 ret = event_create_dir(tr->event_dir, file);
3166 if (ret < 0)
3167 pr_warn("Could not create directory for event %s\n",
3168 trace_event_name(file->event_call));
3169 }
3170 }
3171
3172 /*
3173 * For early boot up, the top trace array and the trace arrays created
3174 * by boot-time tracing require to have a list of events that can be
3175 * enabled. This must be done before the filesystem is set up in order
3176 * to allow events to be traced early.
3177 */
__trace_early_add_events(struct trace_array * tr)3178 void __trace_early_add_events(struct trace_array *tr)
3179 {
3180 struct trace_event_call *call;
3181 int ret;
3182
3183 list_for_each_entry(call, &ftrace_events, list) {
3184 /* Early boot up should not have any modules loaded */
3185 if (WARN_ON_ONCE(call->mod))
3186 continue;
3187
3188 ret = __trace_early_add_new_event(call, tr);
3189 if (ret < 0)
3190 pr_warn("Could not create early event %s\n",
3191 trace_event_name(call));
3192 }
3193 }
3194
3195 /* Remove the event directory structure for a trace directory. */
3196 static void
__trace_remove_event_dirs(struct trace_array * tr)3197 __trace_remove_event_dirs(struct trace_array *tr)
3198 {
3199 struct trace_event_file *file, *next;
3200
3201 list_for_each_entry_safe(file, next, &tr->events, list)
3202 remove_event_file_dir(file);
3203 }
3204
__add_event_to_tracers(struct trace_event_call * call)3205 static void __add_event_to_tracers(struct trace_event_call *call)
3206 {
3207 struct trace_array *tr;
3208
3209 list_for_each_entry(tr, &ftrace_trace_arrays, list)
3210 __trace_add_new_event(call, tr);
3211 }
3212
3213 extern struct trace_event_call *__start_ftrace_events[];
3214 extern struct trace_event_call *__stop_ftrace_events[];
3215
3216 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
3217
setup_trace_event(char * str)3218 static __init int setup_trace_event(char *str)
3219 {
3220 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
3221 ring_buffer_expanded = true;
3222 disable_tracing_selftest("running event tracing");
3223
3224 return 1;
3225 }
3226 __setup("trace_event=", setup_trace_event);
3227
3228 /* Expects to have event_mutex held when called */
3229 static int
create_event_toplevel_files(struct dentry * parent,struct trace_array * tr)3230 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
3231 {
3232 struct dentry *d_events;
3233 struct dentry *entry;
3234
3235 entry = tracefs_create_file("set_event", 0644, parent,
3236 tr, &ftrace_set_event_fops);
3237 if (!entry) {
3238 pr_warn("Could not create tracefs 'set_event' entry\n");
3239 return -ENOMEM;
3240 }
3241
3242 d_events = tracefs_create_dir("events", parent);
3243 if (!d_events) {
3244 pr_warn("Could not create tracefs 'events' directory\n");
3245 return -ENOMEM;
3246 }
3247
3248 entry = trace_create_file("enable", 0644, d_events,
3249 tr, &ftrace_tr_enable_fops);
3250 if (!entry) {
3251 pr_warn("Could not create tracefs 'enable' entry\n");
3252 return -ENOMEM;
3253 }
3254
3255 /* There are not as crucial, just warn if they are not created */
3256
3257 entry = tracefs_create_file("set_event_pid", 0644, parent,
3258 tr, &ftrace_set_event_pid_fops);
3259 if (!entry)
3260 pr_warn("Could not create tracefs 'set_event_pid' entry\n");
3261
3262 entry = tracefs_create_file("set_event_notrace_pid", 0644, parent,
3263 tr, &ftrace_set_event_notrace_pid_fops);
3264 if (!entry)
3265 pr_warn("Could not create tracefs 'set_event_notrace_pid' entry\n");
3266
3267 /* ring buffer internal formats */
3268 entry = trace_create_file("header_page", 0444, d_events,
3269 ring_buffer_print_page_header,
3270 &ftrace_show_header_fops);
3271 if (!entry)
3272 pr_warn("Could not create tracefs 'header_page' entry\n");
3273
3274 entry = trace_create_file("header_event", 0444, d_events,
3275 ring_buffer_print_entry_header,
3276 &ftrace_show_header_fops);
3277 if (!entry)
3278 pr_warn("Could not create tracefs 'header_event' entry\n");
3279
3280 tr->event_dir = d_events;
3281
3282 return 0;
3283 }
3284
3285 /**
3286 * event_trace_add_tracer - add a instance of a trace_array to events
3287 * @parent: The parent dentry to place the files/directories for events in
3288 * @tr: The trace array associated with these events
3289 *
3290 * When a new instance is created, it needs to set up its events
3291 * directory, as well as other files associated with events. It also
3292 * creates the event hierachry in the @parent/events directory.
3293 *
3294 * Returns 0 on success.
3295 *
3296 * Must be called with event_mutex held.
3297 */
event_trace_add_tracer(struct dentry * parent,struct trace_array * tr)3298 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
3299 {
3300 int ret;
3301
3302 lockdep_assert_held(&event_mutex);
3303
3304 ret = create_event_toplevel_files(parent, tr);
3305 if (ret)
3306 goto out;
3307
3308 down_write(&trace_event_sem);
3309 /* If tr already has the event list, it is initialized in early boot. */
3310 if (unlikely(!list_empty(&tr->events)))
3311 __trace_early_add_event_dirs(tr);
3312 else
3313 __trace_add_event_dirs(tr);
3314 up_write(&trace_event_sem);
3315
3316 out:
3317 return ret;
3318 }
3319
3320 /*
3321 * The top trace array already had its file descriptors created.
3322 * Now the files themselves need to be created.
3323 */
3324 static __init int
early_event_add_tracer(struct dentry * parent,struct trace_array * tr)3325 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
3326 {
3327 int ret;
3328
3329 mutex_lock(&event_mutex);
3330
3331 ret = create_event_toplevel_files(parent, tr);
3332 if (ret)
3333 goto out_unlock;
3334
3335 down_write(&trace_event_sem);
3336 __trace_early_add_event_dirs(tr);
3337 up_write(&trace_event_sem);
3338
3339 out_unlock:
3340 mutex_unlock(&event_mutex);
3341
3342 return ret;
3343 }
3344
3345 /* Must be called with event_mutex held */
event_trace_del_tracer(struct trace_array * tr)3346 int event_trace_del_tracer(struct trace_array *tr)
3347 {
3348 lockdep_assert_held(&event_mutex);
3349
3350 /* Disable any event triggers and associated soft-disabled events */
3351 clear_event_triggers(tr);
3352
3353 /* Clear the pid list */
3354 __ftrace_clear_event_pids(tr, TRACE_PIDS | TRACE_NO_PIDS);
3355
3356 /* Disable any running events */
3357 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
3358
3359 /* Make sure no more events are being executed */
3360 tracepoint_synchronize_unregister();
3361
3362 down_write(&trace_event_sem);
3363 __trace_remove_event_dirs(tr);
3364 tracefs_remove(tr->event_dir);
3365 up_write(&trace_event_sem);
3366
3367 tr->event_dir = NULL;
3368
3369 return 0;
3370 }
3371
event_trace_memsetup(void)3372 static __init int event_trace_memsetup(void)
3373 {
3374 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
3375 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
3376 return 0;
3377 }
3378
3379 static __init void
early_enable_events(struct trace_array * tr,bool disable_first)3380 early_enable_events(struct trace_array *tr, bool disable_first)
3381 {
3382 char *buf = bootup_event_buf;
3383 char *token;
3384 int ret;
3385
3386 while (true) {
3387 token = strsep(&buf, ",");
3388
3389 if (!token)
3390 break;
3391
3392 if (*token) {
3393 /* Restarting syscalls requires that we stop them first */
3394 if (disable_first)
3395 ftrace_set_clr_event(tr, token, 0);
3396
3397 ret = ftrace_set_clr_event(tr, token, 1);
3398 if (ret)
3399 pr_warn("Failed to enable trace event: %s\n", token);
3400 }
3401
3402 /* Put back the comma to allow this to be called again */
3403 if (buf)
3404 *(buf - 1) = ',';
3405 }
3406 }
3407
event_trace_enable(void)3408 static __init int event_trace_enable(void)
3409 {
3410 struct trace_array *tr = top_trace_array();
3411 struct trace_event_call **iter, *call;
3412 int ret;
3413
3414 if (!tr)
3415 return -ENODEV;
3416
3417 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
3418
3419 call = *iter;
3420 ret = event_init(call);
3421 if (!ret)
3422 list_add(&call->list, &ftrace_events);
3423 }
3424
3425 /*
3426 * We need the top trace array to have a working set of trace
3427 * points at early init, before the debug files and directories
3428 * are created. Create the file entries now, and attach them
3429 * to the actual file dentries later.
3430 */
3431 __trace_early_add_events(tr);
3432
3433 early_enable_events(tr, false);
3434
3435 trace_printk_start_comm();
3436
3437 register_event_cmds();
3438
3439 register_trigger_cmds();
3440
3441 return 0;
3442 }
3443
3444 /*
3445 * event_trace_enable() is called from trace_event_init() first to
3446 * initialize events and perhaps start any events that are on the
3447 * command line. Unfortunately, there are some events that will not
3448 * start this early, like the system call tracepoints that need
3449 * to set the TIF_SYSCALL_TRACEPOINT flag of pid 1. But event_trace_enable()
3450 * is called before pid 1 starts, and this flag is never set, making
3451 * the syscall tracepoint never get reached, but the event is enabled
3452 * regardless (and not doing anything).
3453 */
event_trace_enable_again(void)3454 static __init int event_trace_enable_again(void)
3455 {
3456 struct trace_array *tr;
3457
3458 tr = top_trace_array();
3459 if (!tr)
3460 return -ENODEV;
3461
3462 early_enable_events(tr, true);
3463
3464 return 0;
3465 }
3466
3467 early_initcall(event_trace_enable_again);
3468
3469 /* Init fields which doesn't related to the tracefs */
event_trace_init_fields(void)3470 static __init int event_trace_init_fields(void)
3471 {
3472 if (trace_define_generic_fields())
3473 pr_warn("tracing: Failed to allocated generic fields");
3474
3475 if (trace_define_common_fields())
3476 pr_warn("tracing: Failed to allocate common fields");
3477
3478 return 0;
3479 }
3480
event_trace_init(void)3481 __init int event_trace_init(void)
3482 {
3483 struct trace_array *tr;
3484 struct dentry *entry;
3485 int ret;
3486
3487 tr = top_trace_array();
3488 if (!tr)
3489 return -ENODEV;
3490
3491 entry = tracefs_create_file("available_events", 0444, NULL,
3492 tr, &ftrace_avail_fops);
3493 if (!entry)
3494 pr_warn("Could not create tracefs 'available_events' entry\n");
3495
3496 ret = early_event_add_tracer(NULL, tr);
3497 if (ret)
3498 return ret;
3499
3500 #ifdef CONFIG_MODULES
3501 ret = register_module_notifier(&trace_module_nb);
3502 if (ret)
3503 pr_warn("Failed to register trace events module notifier\n");
3504 #endif
3505
3506 eventdir_initialized = true;
3507
3508 return 0;
3509 }
3510
trace_event_init(void)3511 void __init trace_event_init(void)
3512 {
3513 event_trace_memsetup();
3514 init_ftrace_syscalls();
3515 event_trace_enable();
3516 event_trace_init_fields();
3517 }
3518
3519 #ifdef CONFIG_EVENT_TRACE_STARTUP_TEST
3520
3521 static DEFINE_SPINLOCK(test_spinlock);
3522 static DEFINE_SPINLOCK(test_spinlock_irq);
3523 static DEFINE_MUTEX(test_mutex);
3524
test_work(struct work_struct * dummy)3525 static __init void test_work(struct work_struct *dummy)
3526 {
3527 spin_lock(&test_spinlock);
3528 spin_lock_irq(&test_spinlock_irq);
3529 udelay(1);
3530 spin_unlock_irq(&test_spinlock_irq);
3531 spin_unlock(&test_spinlock);
3532
3533 mutex_lock(&test_mutex);
3534 msleep(1);
3535 mutex_unlock(&test_mutex);
3536 }
3537
event_test_thread(void * unused)3538 static __init int event_test_thread(void *unused)
3539 {
3540 void *test_malloc;
3541
3542 test_malloc = kmalloc(1234, GFP_KERNEL);
3543 if (!test_malloc)
3544 pr_info("failed to kmalloc\n");
3545
3546 schedule_on_each_cpu(test_work);
3547
3548 kfree(test_malloc);
3549
3550 set_current_state(TASK_INTERRUPTIBLE);
3551 while (!kthread_should_stop()) {
3552 schedule();
3553 set_current_state(TASK_INTERRUPTIBLE);
3554 }
3555 __set_current_state(TASK_RUNNING);
3556
3557 return 0;
3558 }
3559
3560 /*
3561 * Do various things that may trigger events.
3562 */
event_test_stuff(void)3563 static __init void event_test_stuff(void)
3564 {
3565 struct task_struct *test_thread;
3566
3567 test_thread = kthread_run(event_test_thread, NULL, "test-events");
3568 msleep(1);
3569 kthread_stop(test_thread);
3570 }
3571
3572 /*
3573 * For every trace event defined, we will test each trace point separately,
3574 * and then by groups, and finally all trace points.
3575 */
event_trace_self_tests(void)3576 static __init void event_trace_self_tests(void)
3577 {
3578 struct trace_subsystem_dir *dir;
3579 struct trace_event_file *file;
3580 struct trace_event_call *call;
3581 struct event_subsystem *system;
3582 struct trace_array *tr;
3583 int ret;
3584
3585 tr = top_trace_array();
3586 if (!tr)
3587 return;
3588
3589 pr_info("Running tests on trace events:\n");
3590
3591 list_for_each_entry(file, &tr->events, list) {
3592
3593 call = file->event_call;
3594
3595 /* Only test those that have a probe */
3596 if (!call->class || !call->class->probe)
3597 continue;
3598
3599 /*
3600 * Testing syscall events here is pretty useless, but
3601 * we still do it if configured. But this is time consuming.
3602 * What we really need is a user thread to perform the
3603 * syscalls as we test.
3604 */
3605 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
3606 if (call->class->system &&
3607 strcmp(call->class->system, "syscalls") == 0)
3608 continue;
3609 #endif
3610
3611 pr_info("Testing event %s: ", trace_event_name(call));
3612
3613 /*
3614 * If an event is already enabled, someone is using
3615 * it and the self test should not be on.
3616 */
3617 if (file->flags & EVENT_FILE_FL_ENABLED) {
3618 pr_warn("Enabled event during self test!\n");
3619 WARN_ON_ONCE(1);
3620 continue;
3621 }
3622
3623 ftrace_event_enable_disable(file, 1);
3624 event_test_stuff();
3625 ftrace_event_enable_disable(file, 0);
3626
3627 pr_cont("OK\n");
3628 }
3629
3630 /* Now test at the sub system level */
3631
3632 pr_info("Running tests on trace event systems:\n");
3633
3634 list_for_each_entry(dir, &tr->systems, list) {
3635
3636 system = dir->subsystem;
3637
3638 /* the ftrace system is special, skip it */
3639 if (strcmp(system->name, "ftrace") == 0)
3640 continue;
3641
3642 pr_info("Testing event system %s: ", system->name);
3643
3644 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
3645 if (WARN_ON_ONCE(ret)) {
3646 pr_warn("error enabling system %s\n",
3647 system->name);
3648 continue;
3649 }
3650
3651 event_test_stuff();
3652
3653 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
3654 if (WARN_ON_ONCE(ret)) {
3655 pr_warn("error disabling system %s\n",
3656 system->name);
3657 continue;
3658 }
3659
3660 pr_cont("OK\n");
3661 }
3662
3663 /* Test with all events enabled */
3664
3665 pr_info("Running tests on all trace events:\n");
3666 pr_info("Testing all events: ");
3667
3668 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
3669 if (WARN_ON_ONCE(ret)) {
3670 pr_warn("error enabling all events\n");
3671 return;
3672 }
3673
3674 event_test_stuff();
3675
3676 /* reset sysname */
3677 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
3678 if (WARN_ON_ONCE(ret)) {
3679 pr_warn("error disabling all events\n");
3680 return;
3681 }
3682
3683 pr_cont("OK\n");
3684 }
3685
3686 #ifdef CONFIG_FUNCTION_TRACER
3687
3688 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
3689
3690 static struct trace_event_file event_trace_file __initdata;
3691
3692 static void __init
function_test_events_call(unsigned long ip,unsigned long parent_ip,struct ftrace_ops * op,struct pt_regs * pt_regs)3693 function_test_events_call(unsigned long ip, unsigned long parent_ip,
3694 struct ftrace_ops *op, struct pt_regs *pt_regs)
3695 {
3696 struct trace_buffer *buffer;
3697 struct ring_buffer_event *event;
3698 struct ftrace_entry *entry;
3699 unsigned long flags;
3700 long disabled;
3701 int cpu;
3702 int pc;
3703
3704 pc = preempt_count();
3705 preempt_disable_notrace();
3706 cpu = raw_smp_processor_id();
3707 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
3708
3709 if (disabled != 1)
3710 goto out;
3711
3712 local_save_flags(flags);
3713
3714 event = trace_event_buffer_lock_reserve(&buffer, &event_trace_file,
3715 TRACE_FN, sizeof(*entry),
3716 flags, pc);
3717 if (!event)
3718 goto out;
3719 entry = ring_buffer_event_data(event);
3720 entry->ip = ip;
3721 entry->parent_ip = parent_ip;
3722
3723 event_trigger_unlock_commit(&event_trace_file, buffer, event,
3724 entry, flags, pc);
3725 out:
3726 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
3727 preempt_enable_notrace();
3728 }
3729
3730 static struct ftrace_ops trace_ops __initdata =
3731 {
3732 .func = function_test_events_call,
3733 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
3734 };
3735
event_trace_self_test_with_function(void)3736 static __init void event_trace_self_test_with_function(void)
3737 {
3738 int ret;
3739
3740 event_trace_file.tr = top_trace_array();
3741 if (WARN_ON(!event_trace_file.tr))
3742 return;
3743
3744 ret = register_ftrace_function(&trace_ops);
3745 if (WARN_ON(ret < 0)) {
3746 pr_info("Failed to enable function tracer for event tests\n");
3747 return;
3748 }
3749 pr_info("Running tests again, along with the function tracer\n");
3750 event_trace_self_tests();
3751 unregister_ftrace_function(&trace_ops);
3752 }
3753 #else
event_trace_self_test_with_function(void)3754 static __init void event_trace_self_test_with_function(void)
3755 {
3756 }
3757 #endif
3758
event_trace_self_tests_init(void)3759 static __init int event_trace_self_tests_init(void)
3760 {
3761 if (!tracing_selftest_disabled) {
3762 event_trace_self_tests();
3763 event_trace_self_test_with_function();
3764 }
3765
3766 return 0;
3767 }
3768
3769 late_initcall(event_trace_self_tests_init);
3770
3771 #endif
3772