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