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