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