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