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