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