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