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