]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - kernel/trace/trace_events.c
tracing: trace_remove_event_call() should fail if call/file is in use
[mirror_ubuntu-focal-kernel.git] / kernel / trace / trace_events.c
CommitLineData
b77e38aa
SR
1/*
2 * event tracer
3 *
4 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
5 *
981d081e
SR
6 * - Added format output of fields of the trace point.
7 * This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
8 *
b77e38aa
SR
9 */
10
e6187007
SR
11#include <linux/workqueue.h>
12#include <linux/spinlock.h>
13#include <linux/kthread.h>
b77e38aa
SR
14#include <linux/debugfs.h>
15#include <linux/uaccess.h>
16#include <linux/module.h>
17#include <linux/ctype.h>
5a0e3ad6 18#include <linux/slab.h>
e6187007 19#include <linux/delay.h>
b77e38aa 20
020e5f85
LZ
21#include <asm/setup.h>
22
91729ef9 23#include "trace_output.h"
b77e38aa 24
4e5292ea 25#undef TRACE_SYSTEM
b628b3e6
SR
26#define TRACE_SYSTEM "TRACE_SYSTEM"
27
20c8928a 28DEFINE_MUTEX(event_mutex);
11a241a3 29
04295780
SR
30DEFINE_MUTEX(event_storage_mutex);
31EXPORT_SYMBOL_GPL(event_storage_mutex);
32
33char event_storage[EVENT_STORAGE_SIZE];
34EXPORT_SYMBOL_GPL(event_storage);
35
a59fd602 36LIST_HEAD(ftrace_events);
b3a8c6fd 37static LIST_HEAD(ftrace_common_fields);
a59fd602 38
d1a29143
SR
39#define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
40
41static struct kmem_cache *field_cachep;
42static struct kmem_cache *file_cachep;
43
6e94a780
SR
44#define SYSTEM_FL_FREE_NAME (1 << 31)
45
46static inline int system_refcount(struct event_subsystem *system)
47{
48 return system->ref_count & ~SYSTEM_FL_FREE_NAME;
49}
50
51static int system_refcount_inc(struct event_subsystem *system)
52{
53 return (system->ref_count++) & ~SYSTEM_FL_FREE_NAME;
54}
55
56static int system_refcount_dec(struct event_subsystem *system)
57{
58 return (--system->ref_count) & ~SYSTEM_FL_FREE_NAME;
59}
60
ae63b31e
SR
61/* Double loops, do not use break, only goto's work */
62#define do_for_each_event_file(tr, file) \
63 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
64 list_for_each_entry(file, &tr->events, list)
65
66#define do_for_each_event_file_safe(tr, file) \
67 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
68 struct ftrace_event_file *___n; \
69 list_for_each_entry_safe(file, ___n, &tr->events, list)
70
71#define while_for_each_event_file() \
72 }
73
b3a8c6fd 74static struct list_head *
2e33af02
SR
75trace_get_fields(struct ftrace_event_call *event_call)
76{
77 if (!event_call->class->get_fields)
78 return &event_call->class->fields;
79 return event_call->class->get_fields(event_call);
80}
81
b3a8c6fd
J
82static struct ftrace_event_field *
83__find_event_field(struct list_head *head, char *name)
84{
85 struct ftrace_event_field *field;
86
87 list_for_each_entry(field, head, link) {
88 if (!strcmp(field->name, name))
89 return field;
90 }
91
92 return NULL;
93}
94
95struct ftrace_event_field *
96trace_find_event_field(struct ftrace_event_call *call, char *name)
97{
98 struct ftrace_event_field *field;
99 struct list_head *head;
100
101 field = __find_event_field(&ftrace_common_fields, name);
102 if (field)
103 return field;
104
105 head = trace_get_fields(call);
106 return __find_event_field(head, name);
107}
108
8728fe50
LZ
109static int __trace_define_field(struct list_head *head, const char *type,
110 const char *name, int offset, int size,
111 int is_signed, int filter_type)
cf027f64
TZ
112{
113 struct ftrace_event_field *field;
114
d1a29143 115 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
cf027f64 116 if (!field)
aaf6ac0f 117 return -ENOMEM;
fe9f57f2 118
92edca07
SR
119 field->name = name;
120 field->type = type;
fe9f57f2 121
43b51ead
LZ
122 if (filter_type == FILTER_OTHER)
123 field->filter_type = filter_assign_type(type);
124 else
125 field->filter_type = filter_type;
126
cf027f64
TZ
127 field->offset = offset;
128 field->size = size;
a118e4d1 129 field->is_signed = is_signed;
aa38e9fc 130
2e33af02 131 list_add(&field->link, head);
cf027f64
TZ
132
133 return 0;
cf027f64 134}
8728fe50
LZ
135
136int trace_define_field(struct ftrace_event_call *call, const char *type,
137 const char *name, int offset, int size, int is_signed,
138 int filter_type)
139{
140 struct list_head *head;
141
142 if (WARN_ON(!call->class))
143 return 0;
144
145 head = trace_get_fields(call);
146 return __trace_define_field(head, type, name, offset, size,
147 is_signed, filter_type);
148}
17c873ec 149EXPORT_SYMBOL_GPL(trace_define_field);
cf027f64 150
e647d6b3 151#define __common_field(type, item) \
8728fe50
LZ
152 ret = __trace_define_field(&ftrace_common_fields, #type, \
153 "common_" #item, \
154 offsetof(typeof(ent), item), \
155 sizeof(ent.item), \
156 is_signed_type(type), FILTER_OTHER); \
e647d6b3
LZ
157 if (ret) \
158 return ret;
159
8728fe50 160static int trace_define_common_fields(void)
e647d6b3
LZ
161{
162 int ret;
163 struct trace_entry ent;
164
165 __common_field(unsigned short, type);
166 __common_field(unsigned char, flags);
167 __common_field(unsigned char, preempt_count);
168 __common_field(int, pid);
e647d6b3
LZ
169
170 return ret;
171}
172
ad7067ce 173static void trace_destroy_fields(struct ftrace_event_call *call)
2df75e41
LZ
174{
175 struct ftrace_event_field *field, *next;
2e33af02 176 struct list_head *head;
2df75e41 177
2e33af02
SR
178 head = trace_get_fields(call);
179 list_for_each_entry_safe(field, next, head, link) {
2df75e41 180 list_del(&field->link);
d1a29143 181 kmem_cache_free(field_cachep, field);
2df75e41
LZ
182 }
183}
184
87d9b4e1
LZ
185int trace_event_raw_init(struct ftrace_event_call *call)
186{
187 int id;
188
80decc70 189 id = register_ftrace_event(&call->event);
87d9b4e1
LZ
190 if (!id)
191 return -ENODEV;
87d9b4e1
LZ
192
193 return 0;
194}
195EXPORT_SYMBOL_GPL(trace_event_raw_init);
196
ceec0b6f
JO
197int ftrace_event_reg(struct ftrace_event_call *call,
198 enum trace_reg type, void *data)
a1d0ce82 199{
ae63b31e
SR
200 struct ftrace_event_file *file = data;
201
a1d0ce82
SR
202 switch (type) {
203 case TRACE_REG_REGISTER:
204 return tracepoint_probe_register(call->name,
205 call->class->probe,
ae63b31e 206 file);
a1d0ce82
SR
207 case TRACE_REG_UNREGISTER:
208 tracepoint_probe_unregister(call->name,
209 call->class->probe,
ae63b31e 210 file);
a1d0ce82
SR
211 return 0;
212
213#ifdef CONFIG_PERF_EVENTS
214 case TRACE_REG_PERF_REGISTER:
215 return tracepoint_probe_register(call->name,
216 call->class->perf_probe,
217 call);
218 case TRACE_REG_PERF_UNREGISTER:
219 tracepoint_probe_unregister(call->name,
220 call->class->perf_probe,
221 call);
222 return 0;
ceec0b6f
JO
223 case TRACE_REG_PERF_OPEN:
224 case TRACE_REG_PERF_CLOSE:
489c75c3
JO
225 case TRACE_REG_PERF_ADD:
226 case TRACE_REG_PERF_DEL:
ceec0b6f 227 return 0;
a1d0ce82
SR
228#endif
229 }
230 return 0;
231}
232EXPORT_SYMBOL_GPL(ftrace_event_reg);
233
e870e9a1
LZ
234void trace_event_enable_cmd_record(bool enable)
235{
ae63b31e
SR
236 struct ftrace_event_file *file;
237 struct trace_array *tr;
e870e9a1
LZ
238
239 mutex_lock(&event_mutex);
ae63b31e
SR
240 do_for_each_event_file(tr, file) {
241
242 if (!(file->flags & FTRACE_EVENT_FL_ENABLED))
e870e9a1
LZ
243 continue;
244
245 if (enable) {
246 tracing_start_cmdline_record();
417944c4 247 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
e870e9a1
LZ
248 } else {
249 tracing_stop_cmdline_record();
417944c4 250 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
e870e9a1 251 }
ae63b31e 252 } while_for_each_event_file();
e870e9a1
LZ
253 mutex_unlock(&event_mutex);
254}
255
417944c4
SRRH
256static int __ftrace_event_enable_disable(struct ftrace_event_file *file,
257 int enable, int soft_disable)
fd994989 258{
ae63b31e 259 struct ftrace_event_call *call = file->event_call;
3b8e4273 260 int ret = 0;
417944c4 261 int disable;
3b8e4273 262
fd994989
SR
263 switch (enable) {
264 case 0:
417944c4 265 /*
1cf4c073
MH
266 * When soft_disable is set and enable is cleared, the sm_ref
267 * reference counter is decremented. If it reaches 0, we want
417944c4
SRRH
268 * to clear the SOFT_DISABLED flag but leave the event in the
269 * state that it was. That is, if the event was enabled and
270 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
271 * is set we do not want the event to be enabled before we
272 * clear the bit.
273 *
274 * When soft_disable is not set but the SOFT_MODE flag is,
275 * we do nothing. Do not disable the tracepoint, otherwise
276 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
277 */
278 if (soft_disable) {
1cf4c073
MH
279 if (atomic_dec_return(&file->sm_ref) > 0)
280 break;
417944c4
SRRH
281 disable = file->flags & FTRACE_EVENT_FL_SOFT_DISABLED;
282 clear_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
283 } else
284 disable = !(file->flags & FTRACE_EVENT_FL_SOFT_MODE);
285
286 if (disable && (file->flags & FTRACE_EVENT_FL_ENABLED)) {
287 clear_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
ae63b31e 288 if (file->flags & FTRACE_EVENT_FL_RECORDED_CMD) {
e870e9a1 289 tracing_stop_cmdline_record();
417944c4 290 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
e870e9a1 291 }
ae63b31e 292 call->class->reg(call, TRACE_REG_UNREGISTER, file);
fd994989 293 }
3baa5e4c 294 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
417944c4
SRRH
295 if (file->flags & FTRACE_EVENT_FL_SOFT_MODE)
296 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
3baa5e4c
TZ
297 else
298 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
fd994989
SR
299 break;
300 case 1:
417944c4
SRRH
301 /*
302 * When soft_disable is set and enable is set, we want to
303 * register the tracepoint for the event, but leave the event
304 * as is. That means, if the event was already enabled, we do
305 * nothing (but set SOFT_MODE). If the event is disabled, we
306 * set SOFT_DISABLED before enabling the event tracepoint, so
307 * it still seems to be disabled.
308 */
309 if (!soft_disable)
310 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
1cf4c073
MH
311 else {
312 if (atomic_inc_return(&file->sm_ref) > 1)
313 break;
417944c4 314 set_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
1cf4c073 315 }
417944c4 316
ae63b31e 317 if (!(file->flags & FTRACE_EVENT_FL_ENABLED)) {
417944c4
SRRH
318
319 /* Keep the event disabled, when going to SOFT_MODE. */
320 if (soft_disable)
321 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
322
e870e9a1
LZ
323 if (trace_flags & TRACE_ITER_RECORD_CMD) {
324 tracing_start_cmdline_record();
417944c4 325 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
e870e9a1 326 }
ae63b31e 327 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
3b8e4273
LZ
328 if (ret) {
329 tracing_stop_cmdline_record();
330 pr_info("event trace: Could not enable event "
331 "%s\n", call->name);
332 break;
333 }
417944c4 334 set_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
575380da
SRRH
335
336 /* WAS_ENABLED gets set but never cleared. */
337 call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
fd994989 338 }
fd994989
SR
339 break;
340 }
3b8e4273
LZ
341
342 return ret;
fd994989
SR
343}
344
417944c4
SRRH
345static int ftrace_event_enable_disable(struct ftrace_event_file *file,
346 int enable)
347{
348 return __ftrace_event_enable_disable(file, enable, 0);
349}
350
ae63b31e 351static void ftrace_clear_events(struct trace_array *tr)
0e907c99 352{
ae63b31e 353 struct ftrace_event_file *file;
0e907c99
Z
354
355 mutex_lock(&event_mutex);
ae63b31e
SR
356 list_for_each_entry(file, &tr->events, list) {
357 ftrace_event_enable_disable(file, 0);
0e907c99
Z
358 }
359 mutex_unlock(&event_mutex);
360}
361
e9dbfae5
SR
362static void __put_system(struct event_subsystem *system)
363{
364 struct event_filter *filter = system->filter;
365
6e94a780
SR
366 WARN_ON_ONCE(system_refcount(system) == 0);
367 if (system_refcount_dec(system))
e9dbfae5
SR
368 return;
369
ae63b31e
SR
370 list_del(&system->list);
371
e9dbfae5
SR
372 if (filter) {
373 kfree(filter->filter_string);
374 kfree(filter);
375 }
6e94a780
SR
376 if (system->ref_count & SYSTEM_FL_FREE_NAME)
377 kfree(system->name);
e9dbfae5
SR
378 kfree(system);
379}
380
381static void __get_system(struct event_subsystem *system)
382{
6e94a780
SR
383 WARN_ON_ONCE(system_refcount(system) == 0);
384 system_refcount_inc(system);
e9dbfae5
SR
385}
386
ae63b31e
SR
387static void __get_system_dir(struct ftrace_subsystem_dir *dir)
388{
389 WARN_ON_ONCE(dir->ref_count == 0);
390 dir->ref_count++;
391 __get_system(dir->subsystem);
392}
393
394static void __put_system_dir(struct ftrace_subsystem_dir *dir)
395{
396 WARN_ON_ONCE(dir->ref_count == 0);
397 /* If the subsystem is about to be freed, the dir must be too */
6e94a780 398 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
ae63b31e
SR
399
400 __put_system(dir->subsystem);
401 if (!--dir->ref_count)
402 kfree(dir);
403}
404
405static void put_system(struct ftrace_subsystem_dir *dir)
e9dbfae5
SR
406{
407 mutex_lock(&event_mutex);
ae63b31e 408 __put_system_dir(dir);
e9dbfae5
SR
409 mutex_unlock(&event_mutex);
410}
411
f6a84bdc
ON
412static void remove_subsystem(struct ftrace_subsystem_dir *dir)
413{
414 if (!dir)
415 return;
416
417 if (!--dir->nr_events) {
418 debugfs_remove_recursive(dir->entry);
419 list_del(&dir->list);
420 __put_system_dir(dir);
421 }
422}
423
1a11126b
ON
424static void *event_file_data(struct file *filp)
425{
426 return ACCESS_ONCE(file_inode(filp)->i_private);
427}
428
f6a84bdc
ON
429static void remove_event_file_dir(struct ftrace_event_file *file)
430{
bf682c31
ON
431 struct dentry *dir = file->dir;
432 struct dentry *child;
433
434 if (dir) {
435 spin_lock(&dir->d_lock); /* probably unneeded */
436 list_for_each_entry(child, &dir->d_subdirs, d_u.d_child) {
437 if (child->d_inode) /* probably unneeded */
438 child->d_inode->i_private = NULL;
439 }
440 spin_unlock(&dir->d_lock);
441
442 debugfs_remove_recursive(dir);
443 }
444
f6a84bdc 445 list_del(&file->list);
f6a84bdc
ON
446 remove_subsystem(file->system);
447 kmem_cache_free(file_cachep, file);
448}
449
8f31bfe5
LZ
450/*
451 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
452 */
2a6c24af
SRRH
453static int
454__ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
455 const char *sub, const char *event, int set)
b77e38aa 456{
ae63b31e 457 struct ftrace_event_file *file;
a59fd602 458 struct ftrace_event_call *call;
29f93943 459 int ret = -EINVAL;
8f31bfe5 460
ae63b31e
SR
461 list_for_each_entry(file, &tr->events, list) {
462
463 call = file->event_call;
8f31bfe5 464
a1d0ce82 465 if (!call->name || !call->class || !call->class->reg)
8f31bfe5
LZ
466 continue;
467
9b63776f
SR
468 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
469 continue;
470
8f31bfe5
LZ
471 if (match &&
472 strcmp(match, call->name) != 0 &&
8f082018 473 strcmp(match, call->class->system) != 0)
8f31bfe5
LZ
474 continue;
475
8f082018 476 if (sub && strcmp(sub, call->class->system) != 0)
8f31bfe5
LZ
477 continue;
478
479 if (event && strcmp(event, call->name) != 0)
480 continue;
481
ae63b31e 482 ftrace_event_enable_disable(file, set);
8f31bfe5
LZ
483
484 ret = 0;
485 }
2a6c24af
SRRH
486
487 return ret;
488}
489
490static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
491 const char *sub, const char *event, int set)
492{
493 int ret;
494
495 mutex_lock(&event_mutex);
496 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
8f31bfe5
LZ
497 mutex_unlock(&event_mutex);
498
499 return ret;
500}
501
ae63b31e 502static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
8f31bfe5 503{
b628b3e6 504 char *event = NULL, *sub = NULL, *match;
b628b3e6
SR
505
506 /*
507 * The buf format can be <subsystem>:<event-name>
508 * *:<event-name> means any event by that name.
509 * :<event-name> is the same.
510 *
511 * <subsystem>:* means all events in that subsystem
512 * <subsystem>: means the same.
513 *
514 * <name> (no ':') means all events in a subsystem with
515 * the name <name> or any event that matches <name>
516 */
517
518 match = strsep(&buf, ":");
519 if (buf) {
520 sub = match;
521 event = buf;
522 match = NULL;
523
524 if (!strlen(sub) || strcmp(sub, "*") == 0)
525 sub = NULL;
526 if (!strlen(event) || strcmp(event, "*") == 0)
527 event = NULL;
528 }
b77e38aa 529
ae63b31e 530 return __ftrace_set_clr_event(tr, match, sub, event, set);
b77e38aa
SR
531}
532
4671c794
SR
533/**
534 * trace_set_clr_event - enable or disable an event
535 * @system: system name to match (NULL for any system)
536 * @event: event name to match (NULL for all events, within system)
537 * @set: 1 to enable, 0 to disable
538 *
539 * This is a way for other parts of the kernel to enable or disable
540 * event recording.
541 *
542 * Returns 0 on success, -EINVAL if the parameters do not match any
543 * registered events.
544 */
545int trace_set_clr_event(const char *system, const char *event, int set)
546{
ae63b31e
SR
547 struct trace_array *tr = top_trace_array();
548
549 return __ftrace_set_clr_event(tr, NULL, system, event, set);
4671c794 550}
56355b83 551EXPORT_SYMBOL_GPL(trace_set_clr_event);
4671c794 552
b77e38aa
SR
553/* 128 should be much more than enough */
554#define EVENT_BUF_SIZE 127
555
556static ssize_t
557ftrace_event_write(struct file *file, const char __user *ubuf,
558 size_t cnt, loff_t *ppos)
559{
48966364 560 struct trace_parser parser;
ae63b31e
SR
561 struct seq_file *m = file->private_data;
562 struct trace_array *tr = m->private;
4ba7978e 563 ssize_t read, ret;
b77e38aa 564
4ba7978e 565 if (!cnt)
b77e38aa
SR
566 return 0;
567
1852fcce
SR
568 ret = tracing_update_buffers();
569 if (ret < 0)
570 return ret;
571
48966364 572 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
b77e38aa
SR
573 return -ENOMEM;
574
48966364 575 read = trace_get_user(&parser, ubuf, cnt, ppos);
576
4ba7978e 577 if (read >= 0 && trace_parser_loaded((&parser))) {
48966364 578 int set = 1;
b77e38aa 579
48966364 580 if (*parser.buffer == '!')
b77e38aa 581 set = 0;
b77e38aa 582
48966364 583 parser.buffer[parser.idx] = 0;
584
ae63b31e 585 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
b77e38aa 586 if (ret)
48966364 587 goto out_put;
b77e38aa 588 }
b77e38aa
SR
589
590 ret = read;
591
48966364 592 out_put:
593 trace_parser_put(&parser);
b77e38aa
SR
594
595 return ret;
596}
597
598static void *
599t_next(struct seq_file *m, void *v, loff_t *pos)
600{
ae63b31e
SR
601 struct ftrace_event_file *file = v;
602 struct ftrace_event_call *call;
603 struct trace_array *tr = m->private;
b77e38aa
SR
604
605 (*pos)++;
606
ae63b31e
SR
607 list_for_each_entry_continue(file, &tr->events, list) {
608 call = file->event_call;
40e26815
SR
609 /*
610 * The ftrace subsystem is for showing formats only.
611 * They can not be enabled or disabled via the event files.
612 */
a1d0ce82 613 if (call->class && call->class->reg)
ae63b31e 614 return file;
40e26815 615 }
b77e38aa 616
30bd39cd 617 return NULL;
b77e38aa
SR
618}
619
620static void *t_start(struct seq_file *m, loff_t *pos)
621{
ae63b31e
SR
622 struct ftrace_event_file *file;
623 struct trace_array *tr = m->private;
e1c7e2a6
LZ
624 loff_t l;
625
20c8928a 626 mutex_lock(&event_mutex);
e1c7e2a6 627
ae63b31e 628 file = list_entry(&tr->events, struct ftrace_event_file, list);
e1c7e2a6 629 for (l = 0; l <= *pos; ) {
ae63b31e
SR
630 file = t_next(m, file, &l);
631 if (!file)
e1c7e2a6
LZ
632 break;
633 }
ae63b31e 634 return file;
b77e38aa
SR
635}
636
637static void *
638s_next(struct seq_file *m, void *v, loff_t *pos)
639{
ae63b31e
SR
640 struct ftrace_event_file *file = v;
641 struct trace_array *tr = m->private;
b77e38aa
SR
642
643 (*pos)++;
644
ae63b31e
SR
645 list_for_each_entry_continue(file, &tr->events, list) {
646 if (file->flags & FTRACE_EVENT_FL_ENABLED)
647 return file;
b77e38aa
SR
648 }
649
30bd39cd 650 return NULL;
b77e38aa
SR
651}
652
653static void *s_start(struct seq_file *m, loff_t *pos)
654{
ae63b31e
SR
655 struct ftrace_event_file *file;
656 struct trace_array *tr = m->private;
e1c7e2a6
LZ
657 loff_t l;
658
20c8928a 659 mutex_lock(&event_mutex);
e1c7e2a6 660
ae63b31e 661 file = list_entry(&tr->events, struct ftrace_event_file, list);
e1c7e2a6 662 for (l = 0; l <= *pos; ) {
ae63b31e
SR
663 file = s_next(m, file, &l);
664 if (!file)
e1c7e2a6
LZ
665 break;
666 }
ae63b31e 667 return file;
b77e38aa
SR
668}
669
670static int t_show(struct seq_file *m, void *v)
671{
ae63b31e
SR
672 struct ftrace_event_file *file = v;
673 struct ftrace_event_call *call = file->event_call;
b77e38aa 674
8f082018
SR
675 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
676 seq_printf(m, "%s:", call->class->system);
b77e38aa
SR
677 seq_printf(m, "%s\n", call->name);
678
679 return 0;
680}
681
682static void t_stop(struct seq_file *m, void *p)
683{
20c8928a 684 mutex_unlock(&event_mutex);
b77e38aa
SR
685}
686
1473e441
SR
687static ssize_t
688event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
689 loff_t *ppos)
690{
bc6f6b08
ON
691 struct ftrace_event_file *file;
692 unsigned long flags;
a4390596
TZ
693 char buf[4] = "0";
694
bc6f6b08
ON
695 mutex_lock(&event_mutex);
696 file = event_file_data(filp);
697 if (likely(file))
698 flags = file->flags;
699 mutex_unlock(&event_mutex);
700
701 if (!file)
702 return -ENODEV;
703
704 if (flags & FTRACE_EVENT_FL_ENABLED &&
705 !(flags & FTRACE_EVENT_FL_SOFT_DISABLED))
a4390596
TZ
706 strcpy(buf, "1");
707
bc6f6b08
ON
708 if (flags & FTRACE_EVENT_FL_SOFT_DISABLED ||
709 flags & FTRACE_EVENT_FL_SOFT_MODE)
a4390596
TZ
710 strcat(buf, "*");
711
712 strcat(buf, "\n");
1473e441 713
417944c4 714 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
1473e441
SR
715}
716
717static ssize_t
718event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
719 loff_t *ppos)
720{
bc6f6b08 721 struct ftrace_event_file *file;
1473e441
SR
722 unsigned long val;
723 int ret;
724
22fe9b54
PH
725 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
726 if (ret)
1473e441
SR
727 return ret;
728
1852fcce
SR
729 ret = tracing_update_buffers();
730 if (ret < 0)
731 return ret;
732
1473e441
SR
733 switch (val) {
734 case 0:
1473e441 735 case 1:
bc6f6b08 736 ret = -ENODEV;
11a241a3 737 mutex_lock(&event_mutex);
bc6f6b08
ON
738 file = event_file_data(filp);
739 if (likely(file))
740 ret = ftrace_event_enable_disable(file, val);
11a241a3 741 mutex_unlock(&event_mutex);
1473e441
SR
742 break;
743
744 default:
745 return -EINVAL;
746 }
747
748 *ppos += cnt;
749
3b8e4273 750 return ret ? ret : cnt;
1473e441
SR
751}
752
8ae79a13
SR
753static ssize_t
754system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
755 loff_t *ppos)
756{
c142b15d 757 const char set_to_char[4] = { '?', '0', '1', 'X' };
ae63b31e
SR
758 struct ftrace_subsystem_dir *dir = filp->private_data;
759 struct event_subsystem *system = dir->subsystem;
8ae79a13 760 struct ftrace_event_call *call;
ae63b31e
SR
761 struct ftrace_event_file *file;
762 struct trace_array *tr = dir->tr;
8ae79a13 763 char buf[2];
c142b15d 764 int set = 0;
8ae79a13
SR
765 int ret;
766
8ae79a13 767 mutex_lock(&event_mutex);
ae63b31e
SR
768 list_for_each_entry(file, &tr->events, list) {
769 call = file->event_call;
a1d0ce82 770 if (!call->name || !call->class || !call->class->reg)
8ae79a13
SR
771 continue;
772
40ee4dff 773 if (system && strcmp(call->class->system, system->name) != 0)
8ae79a13
SR
774 continue;
775
776 /*
777 * We need to find out if all the events are set
778 * or if all events or cleared, or if we have
779 * a mixture.
780 */
ae63b31e 781 set |= (1 << !!(file->flags & FTRACE_EVENT_FL_ENABLED));
c142b15d 782
8ae79a13
SR
783 /*
784 * If we have a mixture, no need to look further.
785 */
c142b15d 786 if (set == 3)
8ae79a13
SR
787 break;
788 }
789 mutex_unlock(&event_mutex);
790
c142b15d 791 buf[0] = set_to_char[set];
8ae79a13 792 buf[1] = '\n';
8ae79a13
SR
793
794 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
795
796 return ret;
797}
798
799static ssize_t
800system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
801 loff_t *ppos)
802{
ae63b31e
SR
803 struct ftrace_subsystem_dir *dir = filp->private_data;
804 struct event_subsystem *system = dir->subsystem;
40ee4dff 805 const char *name = NULL;
8ae79a13 806 unsigned long val;
8ae79a13
SR
807 ssize_t ret;
808
22fe9b54
PH
809 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
810 if (ret)
8ae79a13
SR
811 return ret;
812
813 ret = tracing_update_buffers();
814 if (ret < 0)
815 return ret;
816
8f31bfe5 817 if (val != 0 && val != 1)
8ae79a13 818 return -EINVAL;
8ae79a13 819
40ee4dff
SR
820 /*
821 * Opening of "enable" adds a ref count to system,
822 * so the name is safe to use.
823 */
824 if (system)
825 name = system->name;
826
ae63b31e 827 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
8ae79a13 828 if (ret)
8f31bfe5 829 goto out;
8ae79a13
SR
830
831 ret = cnt;
832
8f31bfe5 833out:
8ae79a13
SR
834 *ppos += cnt;
835
836 return ret;
837}
838
2a37a3df
SR
839enum {
840 FORMAT_HEADER = 1,
86397dc3
LZ
841 FORMAT_FIELD_SEPERATOR = 2,
842 FORMAT_PRINTFMT = 3,
2a37a3df
SR
843};
844
845static void *f_next(struct seq_file *m, void *v, loff_t *pos)
981d081e 846{
c5a44a12 847 struct ftrace_event_call *call = event_file_data(m->private);
86397dc3
LZ
848 struct list_head *common_head = &ftrace_common_fields;
849 struct list_head *head = trace_get_fields(call);
7710b639 850 struct list_head *node = v;
981d081e 851
2a37a3df 852 (*pos)++;
5a65e956 853
2a37a3df
SR
854 switch ((unsigned long)v) {
855 case FORMAT_HEADER:
7710b639
ON
856 node = common_head;
857 break;
5a65e956 858
86397dc3 859 case FORMAT_FIELD_SEPERATOR:
7710b639
ON
860 node = head;
861 break;
5a65e956 862
2a37a3df
SR
863 case FORMAT_PRINTFMT:
864 /* all done */
865 return NULL;
5a65e956
LJ
866 }
867
7710b639
ON
868 node = node->prev;
869 if (node == common_head)
86397dc3 870 return (void *)FORMAT_FIELD_SEPERATOR;
7710b639 871 else if (node == head)
2a37a3df 872 return (void *)FORMAT_PRINTFMT;
7710b639
ON
873 else
874 return node;
2a37a3df
SR
875}
876
877static int f_show(struct seq_file *m, void *v)
878{
c5a44a12 879 struct ftrace_event_call *call = event_file_data(m->private);
2a37a3df
SR
880 struct ftrace_event_field *field;
881 const char *array_descriptor;
882
883 switch ((unsigned long)v) {
884 case FORMAT_HEADER:
885 seq_printf(m, "name: %s\n", call->name);
886 seq_printf(m, "ID: %d\n", call->event.type);
887 seq_printf(m, "format:\n");
8728fe50 888 return 0;
5a65e956 889
86397dc3
LZ
890 case FORMAT_FIELD_SEPERATOR:
891 seq_putc(m, '\n');
892 return 0;
893
2a37a3df
SR
894 case FORMAT_PRINTFMT:
895 seq_printf(m, "\nprint fmt: %s\n",
896 call->print_fmt);
897 return 0;
981d081e 898 }
8728fe50 899
7710b639 900 field = list_entry(v, struct ftrace_event_field, link);
2a37a3df
SR
901 /*
902 * Smartly shows the array type(except dynamic array).
903 * Normal:
904 * field:TYPE VAR
905 * If TYPE := TYPE[LEN], it is shown:
906 * field:TYPE VAR[LEN]
907 */
908 array_descriptor = strchr(field->type, '[');
8728fe50 909
2a37a3df
SR
910 if (!strncmp(field->type, "__data_loc", 10))
911 array_descriptor = NULL;
8728fe50 912
2a37a3df
SR
913 if (!array_descriptor)
914 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
915 field->type, field->name, field->offset,
916 field->size, !!field->is_signed);
917 else
918 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
919 (int)(array_descriptor - field->type),
920 field->type, field->name,
921 array_descriptor, field->offset,
922 field->size, !!field->is_signed);
8728fe50 923
2a37a3df
SR
924 return 0;
925}
5a65e956 926
7710b639
ON
927static void *f_start(struct seq_file *m, loff_t *pos)
928{
929 void *p = (void *)FORMAT_HEADER;
930 loff_t l = 0;
931
c5a44a12
ON
932 /* ->stop() is called even if ->start() fails */
933 mutex_lock(&event_mutex);
934 if (!event_file_data(m->private))
935 return ERR_PTR(-ENODEV);
936
7710b639
ON
937 while (l < *pos && p)
938 p = f_next(m, p, &l);
939
940 return p;
941}
942
2a37a3df
SR
943static void f_stop(struct seq_file *m, void *p)
944{
c5a44a12 945 mutex_unlock(&event_mutex);
2a37a3df 946}
981d081e 947
2a37a3df
SR
948static const struct seq_operations trace_format_seq_ops = {
949 .start = f_start,
950 .next = f_next,
951 .stop = f_stop,
952 .show = f_show,
953};
954
955static int trace_format_open(struct inode *inode, struct file *file)
956{
2a37a3df
SR
957 struct seq_file *m;
958 int ret;
959
960 ret = seq_open(file, &trace_format_seq_ops);
961 if (ret < 0)
962 return ret;
963
964 m = file->private_data;
c5a44a12 965 m->private = file;
2a37a3df
SR
966
967 return 0;
981d081e
SR
968}
969
23725aee
PZ
970static ssize_t
971event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
972{
1a11126b 973 int id = (long)event_file_data(filp);
cd458ba9
ON
974 char buf[32];
975 int len;
23725aee
PZ
976
977 if (*ppos)
978 return 0;
979
1a11126b
ON
980 if (unlikely(!id))
981 return -ENODEV;
982
983 len = sprintf(buf, "%d\n", id);
984
cd458ba9 985 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
23725aee
PZ
986}
987
7ce7e424
TZ
988static ssize_t
989event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
990 loff_t *ppos)
991{
e2912b09 992 struct ftrace_event_call *call;
7ce7e424 993 struct trace_seq *s;
e2912b09 994 int r = -ENODEV;
7ce7e424
TZ
995
996 if (*ppos)
997 return 0;
998
999 s = kmalloc(sizeof(*s), GFP_KERNEL);
e2912b09 1000
7ce7e424
TZ
1001 if (!s)
1002 return -ENOMEM;
1003
1004 trace_seq_init(s);
1005
e2912b09
ON
1006 mutex_lock(&event_mutex);
1007 call = event_file_data(filp);
1008 if (call)
1009 print_event_filter(call, s);
1010 mutex_unlock(&event_mutex);
1011
1012 if (call)
1013 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
7ce7e424
TZ
1014
1015 kfree(s);
1016
1017 return r;
1018}
1019
1020static ssize_t
1021event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1022 loff_t *ppos)
1023{
e2912b09 1024 struct ftrace_event_call *call;
8b372562 1025 char *buf;
e2912b09 1026 int err = -ENODEV;
7ce7e424 1027
8b372562 1028 if (cnt >= PAGE_SIZE)
7ce7e424
TZ
1029 return -EINVAL;
1030
8b372562
TZ
1031 buf = (char *)__get_free_page(GFP_TEMPORARY);
1032 if (!buf)
7ce7e424
TZ
1033 return -ENOMEM;
1034
8b372562
TZ
1035 if (copy_from_user(buf, ubuf, cnt)) {
1036 free_page((unsigned long) buf);
1037 return -EFAULT;
7ce7e424 1038 }
8b372562 1039 buf[cnt] = '\0';
7ce7e424 1040
e2912b09
ON
1041 mutex_lock(&event_mutex);
1042 call = event_file_data(filp);
1043 if (call)
1044 err = apply_event_filter(call, buf);
1045 mutex_unlock(&event_mutex);
1046
8b372562
TZ
1047 free_page((unsigned long) buf);
1048 if (err < 0)
44e9c8b7 1049 return err;
0a19e53c 1050
7ce7e424
TZ
1051 *ppos += cnt;
1052
1053 return cnt;
1054}
1055
e9dbfae5
SR
1056static LIST_HEAD(event_subsystems);
1057
1058static int subsystem_open(struct inode *inode, struct file *filp)
1059{
1060 struct event_subsystem *system = NULL;
ae63b31e
SR
1061 struct ftrace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1062 struct trace_array *tr;
e9dbfae5
SR
1063 int ret;
1064
1065 /* Make sure the system still exists */
a8227415 1066 mutex_lock(&trace_types_lock);
e9dbfae5 1067 mutex_lock(&event_mutex);
ae63b31e
SR
1068 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1069 list_for_each_entry(dir, &tr->systems, list) {
1070 if (dir == inode->i_private) {
1071 /* Don't open systems with no events */
1072 if (dir->nr_events) {
1073 __get_system_dir(dir);
1074 system = dir->subsystem;
1075 }
1076 goto exit_loop;
e9dbfae5 1077 }
e9dbfae5
SR
1078 }
1079 }
ae63b31e 1080 exit_loop:
e9dbfae5 1081 mutex_unlock(&event_mutex);
a8227415 1082 mutex_unlock(&trace_types_lock);
e9dbfae5 1083
ae63b31e 1084 if (!system)
e9dbfae5
SR
1085 return -ENODEV;
1086
ae63b31e
SR
1087 /* Some versions of gcc think dir can be uninitialized here */
1088 WARN_ON(!dir);
1089
8e2e2fa4
SRRH
1090 /* Still need to increment the ref count of the system */
1091 if (trace_array_get(tr) < 0) {
1092 put_system(dir);
1093 return -ENODEV;
1094 }
1095
e9dbfae5 1096 ret = tracing_open_generic(inode, filp);
8e2e2fa4
SRRH
1097 if (ret < 0) {
1098 trace_array_put(tr);
ae63b31e 1099 put_system(dir);
8e2e2fa4 1100 }
ae63b31e
SR
1101
1102 return ret;
1103}
1104
1105static int system_tr_open(struct inode *inode, struct file *filp)
1106{
1107 struct ftrace_subsystem_dir *dir;
1108 struct trace_array *tr = inode->i_private;
1109 int ret;
1110
8e2e2fa4
SRRH
1111 if (trace_array_get(tr) < 0)
1112 return -ENODEV;
1113
ae63b31e
SR
1114 /* Make a temporary dir that has no system but points to tr */
1115 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
8e2e2fa4
SRRH
1116 if (!dir) {
1117 trace_array_put(tr);
ae63b31e 1118 return -ENOMEM;
8e2e2fa4 1119 }
ae63b31e
SR
1120
1121 dir->tr = tr;
1122
1123 ret = tracing_open_generic(inode, filp);
8e2e2fa4
SRRH
1124 if (ret < 0) {
1125 trace_array_put(tr);
ae63b31e 1126 kfree(dir);
8e2e2fa4 1127 }
ae63b31e
SR
1128
1129 filp->private_data = dir;
e9dbfae5
SR
1130
1131 return ret;
1132}
1133
1134static int subsystem_release(struct inode *inode, struct file *file)
1135{
ae63b31e 1136 struct ftrace_subsystem_dir *dir = file->private_data;
e9dbfae5 1137
8e2e2fa4
SRRH
1138 trace_array_put(dir->tr);
1139
ae63b31e
SR
1140 /*
1141 * If dir->subsystem is NULL, then this is a temporary
1142 * descriptor that was made for a trace_array to enable
1143 * all subsystems.
1144 */
1145 if (dir->subsystem)
1146 put_system(dir);
1147 else
1148 kfree(dir);
e9dbfae5
SR
1149
1150 return 0;
1151}
1152
cfb180f3
TZ
1153static ssize_t
1154subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1155 loff_t *ppos)
1156{
ae63b31e
SR
1157 struct ftrace_subsystem_dir *dir = filp->private_data;
1158 struct event_subsystem *system = dir->subsystem;
cfb180f3
TZ
1159 struct trace_seq *s;
1160 int r;
1161
1162 if (*ppos)
1163 return 0;
1164
1165 s = kmalloc(sizeof(*s), GFP_KERNEL);
1166 if (!s)
1167 return -ENOMEM;
1168
1169 trace_seq_init(s);
1170
8b372562 1171 print_subsystem_event_filter(system, s);
4bda2d51 1172 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
cfb180f3
TZ
1173
1174 kfree(s);
1175
1176 return r;
1177}
1178
1179static ssize_t
1180subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1181 loff_t *ppos)
1182{
ae63b31e 1183 struct ftrace_subsystem_dir *dir = filp->private_data;
8b372562 1184 char *buf;
cfb180f3
TZ
1185 int err;
1186
8b372562 1187 if (cnt >= PAGE_SIZE)
cfb180f3
TZ
1188 return -EINVAL;
1189
8b372562
TZ
1190 buf = (char *)__get_free_page(GFP_TEMPORARY);
1191 if (!buf)
cfb180f3
TZ
1192 return -ENOMEM;
1193
8b372562
TZ
1194 if (copy_from_user(buf, ubuf, cnt)) {
1195 free_page((unsigned long) buf);
1196 return -EFAULT;
cfb180f3 1197 }
8b372562 1198 buf[cnt] = '\0';
cfb180f3 1199
ae63b31e 1200 err = apply_subsystem_event_filter(dir, buf);
8b372562
TZ
1201 free_page((unsigned long) buf);
1202 if (err < 0)
44e9c8b7 1203 return err;
cfb180f3
TZ
1204
1205 *ppos += cnt;
1206
1207 return cnt;
1208}
1209
d1b182a8
SR
1210static ssize_t
1211show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1212{
1213 int (*func)(struct trace_seq *s) = filp->private_data;
1214 struct trace_seq *s;
1215 int r;
1216
1217 if (*ppos)
1218 return 0;
1219
1220 s = kmalloc(sizeof(*s), GFP_KERNEL);
1221 if (!s)
1222 return -ENOMEM;
1223
1224 trace_seq_init(s);
1225
1226 func(s);
1227 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1228
1229 kfree(s);
1230
1231 return r;
1232}
1233
15075cac
SR
1234static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1235static int ftrace_event_set_open(struct inode *inode, struct file *file);
f77d09a3 1236static int ftrace_event_release(struct inode *inode, struct file *file);
15075cac 1237
b77e38aa
SR
1238static const struct seq_operations show_event_seq_ops = {
1239 .start = t_start,
1240 .next = t_next,
1241 .show = t_show,
1242 .stop = t_stop,
1243};
1244
1245static const struct seq_operations show_set_event_seq_ops = {
1246 .start = s_start,
1247 .next = s_next,
1248 .show = t_show,
1249 .stop = t_stop,
1250};
1251
2314c4ae 1252static const struct file_operations ftrace_avail_fops = {
15075cac 1253 .open = ftrace_event_avail_open,
2314c4ae
SR
1254 .read = seq_read,
1255 .llseek = seq_lseek,
1256 .release = seq_release,
1257};
1258
b77e38aa 1259static const struct file_operations ftrace_set_event_fops = {
15075cac 1260 .open = ftrace_event_set_open,
b77e38aa
SR
1261 .read = seq_read,
1262 .write = ftrace_event_write,
1263 .llseek = seq_lseek,
f77d09a3 1264 .release = ftrace_event_release,
b77e38aa
SR
1265};
1266
1473e441 1267static const struct file_operations ftrace_enable_fops = {
bf682c31 1268 .open = tracing_open_generic,
1473e441
SR
1269 .read = event_enable_read,
1270 .write = event_enable_write,
6038f373 1271 .llseek = default_llseek,
1473e441
SR
1272};
1273
981d081e 1274static const struct file_operations ftrace_event_format_fops = {
2a37a3df
SR
1275 .open = trace_format_open,
1276 .read = seq_read,
1277 .llseek = seq_lseek,
1278 .release = seq_release,
981d081e
SR
1279};
1280
23725aee 1281static const struct file_operations ftrace_event_id_fops = {
23725aee 1282 .read = event_id_read,
6038f373 1283 .llseek = default_llseek,
23725aee
PZ
1284};
1285
7ce7e424
TZ
1286static const struct file_operations ftrace_event_filter_fops = {
1287 .open = tracing_open_generic,
1288 .read = event_filter_read,
1289 .write = event_filter_write,
6038f373 1290 .llseek = default_llseek,
7ce7e424
TZ
1291};
1292
cfb180f3 1293static const struct file_operations ftrace_subsystem_filter_fops = {
e9dbfae5 1294 .open = subsystem_open,
cfb180f3
TZ
1295 .read = subsystem_filter_read,
1296 .write = subsystem_filter_write,
6038f373 1297 .llseek = default_llseek,
e9dbfae5 1298 .release = subsystem_release,
cfb180f3
TZ
1299};
1300
8ae79a13 1301static const struct file_operations ftrace_system_enable_fops = {
40ee4dff 1302 .open = subsystem_open,
8ae79a13
SR
1303 .read = system_enable_read,
1304 .write = system_enable_write,
6038f373 1305 .llseek = default_llseek,
40ee4dff 1306 .release = subsystem_release,
8ae79a13
SR
1307};
1308
ae63b31e
SR
1309static const struct file_operations ftrace_tr_enable_fops = {
1310 .open = system_tr_open,
1311 .read = system_enable_read,
1312 .write = system_enable_write,
1313 .llseek = default_llseek,
1314 .release = subsystem_release,
1315};
1316
d1b182a8
SR
1317static const struct file_operations ftrace_show_header_fops = {
1318 .open = tracing_open_generic,
1319 .read = show_header,
6038f373 1320 .llseek = default_llseek,
d1b182a8
SR
1321};
1322
ae63b31e
SR
1323static int
1324ftrace_event_open(struct inode *inode, struct file *file,
1325 const struct seq_operations *seq_ops)
1473e441 1326{
ae63b31e
SR
1327 struct seq_file *m;
1328 int ret;
1473e441 1329
ae63b31e
SR
1330 ret = seq_open(file, seq_ops);
1331 if (ret < 0)
1332 return ret;
1333 m = file->private_data;
1334 /* copy tr over to seq ops */
1335 m->private = inode->i_private;
1473e441 1336
ae63b31e 1337 return ret;
1473e441
SR
1338}
1339
f77d09a3
AL
1340static int ftrace_event_release(struct inode *inode, struct file *file)
1341{
1342 struct trace_array *tr = inode->i_private;
1343
1344 trace_array_put(tr);
1345
1346 return seq_release(inode, file);
1347}
1348
15075cac
SR
1349static int
1350ftrace_event_avail_open(struct inode *inode, struct file *file)
1351{
1352 const struct seq_operations *seq_ops = &show_event_seq_ops;
1353
ae63b31e 1354 return ftrace_event_open(inode, file, seq_ops);
15075cac
SR
1355}
1356
1357static int
1358ftrace_event_set_open(struct inode *inode, struct file *file)
1359{
1360 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
ae63b31e 1361 struct trace_array *tr = inode->i_private;
f77d09a3
AL
1362 int ret;
1363
1364 if (trace_array_get(tr) < 0)
1365 return -ENODEV;
15075cac
SR
1366
1367 if ((file->f_mode & FMODE_WRITE) &&
1368 (file->f_flags & O_TRUNC))
ae63b31e 1369 ftrace_clear_events(tr);
15075cac 1370
f77d09a3
AL
1371 ret = ftrace_event_open(inode, file, seq_ops);
1372 if (ret < 0)
1373 trace_array_put(tr);
1374 return ret;
ae63b31e
SR
1375}
1376
1377static struct event_subsystem *
1378create_new_subsystem(const char *name)
1379{
1380 struct event_subsystem *system;
1381
1382 /* need to create new entry */
1383 system = kmalloc(sizeof(*system), GFP_KERNEL);
1384 if (!system)
1385 return NULL;
1386
1387 system->ref_count = 1;
6e94a780
SR
1388
1389 /* Only allocate if dynamic (kprobes and modules) */
1390 if (!core_kernel_data((unsigned long)name)) {
1391 system->ref_count |= SYSTEM_FL_FREE_NAME;
1392 system->name = kstrdup(name, GFP_KERNEL);
1393 if (!system->name)
1394 goto out_free;
1395 } else
1396 system->name = name;
ae63b31e
SR
1397
1398 system->filter = NULL;
1399
1400 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1401 if (!system->filter)
1402 goto out_free;
1403
1404 list_add(&system->list, &event_subsystems);
1405
1406 return system;
1407
1408 out_free:
6e94a780
SR
1409 if (system->ref_count & SYSTEM_FL_FREE_NAME)
1410 kfree(system->name);
ae63b31e
SR
1411 kfree(system);
1412 return NULL;
15075cac
SR
1413}
1414
6ecc2d1c 1415static struct dentry *
ae63b31e
SR
1416event_subsystem_dir(struct trace_array *tr, const char *name,
1417 struct ftrace_event_file *file, struct dentry *parent)
6ecc2d1c 1418{
ae63b31e 1419 struct ftrace_subsystem_dir *dir;
6ecc2d1c 1420 struct event_subsystem *system;
e1112b4d 1421 struct dentry *entry;
6ecc2d1c
SR
1422
1423 /* First see if we did not already create this dir */
ae63b31e
SR
1424 list_for_each_entry(dir, &tr->systems, list) {
1425 system = dir->subsystem;
dc82ec98 1426 if (strcmp(system->name, name) == 0) {
ae63b31e
SR
1427 dir->nr_events++;
1428 file->system = dir;
1429 return dir->entry;
dc82ec98 1430 }
6ecc2d1c
SR
1431 }
1432
ae63b31e
SR
1433 /* Now see if the system itself exists. */
1434 list_for_each_entry(system, &event_subsystems, list) {
1435 if (strcmp(system->name, name) == 0)
1436 break;
6ecc2d1c 1437 }
ae63b31e
SR
1438 /* Reset system variable when not found */
1439 if (&system->list == &event_subsystems)
1440 system = NULL;
6ecc2d1c 1441
ae63b31e
SR
1442 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1443 if (!dir)
1444 goto out_fail;
6ecc2d1c 1445
ae63b31e
SR
1446 if (!system) {
1447 system = create_new_subsystem(name);
1448 if (!system)
1449 goto out_free;
1450 } else
1451 __get_system(system);
1452
1453 dir->entry = debugfs_create_dir(name, parent);
1454 if (!dir->entry) {
1455 pr_warning("Failed to create system directory %s\n", name);
1456 __put_system(system);
1457 goto out_free;
6d723736
SR
1458 }
1459
ae63b31e
SR
1460 dir->tr = tr;
1461 dir->ref_count = 1;
1462 dir->nr_events = 1;
1463 dir->subsystem = system;
1464 file->system = dir;
8b372562 1465
ae63b31e 1466 entry = debugfs_create_file("filter", 0644, dir->entry, dir,
e1112b4d 1467 &ftrace_subsystem_filter_fops);
8b372562
TZ
1468 if (!entry) {
1469 kfree(system->filter);
1470 system->filter = NULL;
ae63b31e 1471 pr_warning("Could not create debugfs '%s/filter' entry\n", name);
8b372562 1472 }
e1112b4d 1473
ae63b31e 1474 trace_create_file("enable", 0644, dir->entry, dir,
f3f3f009 1475 &ftrace_system_enable_fops);
8ae79a13 1476
ae63b31e
SR
1477 list_add(&dir->list, &tr->systems);
1478
1479 return dir->entry;
1480
1481 out_free:
1482 kfree(dir);
1483 out_fail:
1484 /* Only print this message if failed on memory allocation */
1485 if (!dir || !system)
1486 pr_warning("No memory to create event subsystem %s\n",
1487 name);
1488 return NULL;
6ecc2d1c
SR
1489}
1490
1473e441 1491static int
ae63b31e
SR
1492event_create_dir(struct dentry *parent,
1493 struct ftrace_event_file *file,
701970b3
SR
1494 const struct file_operations *id,
1495 const struct file_operations *enable,
1496 const struct file_operations *filter,
1497 const struct file_operations *format)
1473e441 1498{
ae63b31e
SR
1499 struct ftrace_event_call *call = file->event_call;
1500 struct trace_array *tr = file->tr;
2e33af02 1501 struct list_head *head;
ae63b31e 1502 struct dentry *d_events;
fd994989 1503 int ret;
1473e441 1504
6ecc2d1c
SR
1505 /*
1506 * If the trace point header did not define TRACE_SYSTEM
1507 * then the system would be called "TRACE_SYSTEM".
1508 */
ae63b31e
SR
1509 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1510 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1511 if (!d_events)
1512 return -ENOMEM;
1513 } else
1514 d_events = parent;
1515
1516 file->dir = debugfs_create_dir(call->name, d_events);
1517 if (!file->dir) {
1518 pr_warning("Could not create debugfs '%s' directory\n",
1519 call->name);
1473e441
SR
1520 return -1;
1521 }
1522
9b63776f 1523 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
ae63b31e 1524 trace_create_file("enable", 0644, file->dir, file,
f3f3f009 1525 enable);
1473e441 1526
2239291a 1527#ifdef CONFIG_PERF_EVENTS
a1d0ce82 1528 if (call->event.type && call->class->reg)
1a11126b
ON
1529 trace_create_file("id", 0444, file->dir,
1530 (void *)(long)call->event.type, id);
2239291a 1531#endif
23725aee 1532
c9d932cf
LZ
1533 /*
1534 * Other events may have the same class. Only update
1535 * the fields if they are not already defined.
1536 */
1537 head = trace_get_fields(call);
1538 if (list_empty(head)) {
1539 ret = call->class->define_fields(call);
1540 if (ret < 0) {
1541 pr_warning("Could not initialize trace point"
1542 " events/%s\n", call->name);
ae63b31e 1543 return -1;
cf027f64
TZ
1544 }
1545 }
ae63b31e 1546 trace_create_file("filter", 0644, file->dir, call,
c9d932cf 1547 filter);
cf027f64 1548
ae63b31e 1549 trace_create_file("format", 0444, file->dir, call,
f3f3f009 1550 format);
6d723736
SR
1551
1552 return 0;
1553}
1554
ae63b31e
SR
1555static void remove_event_from_tracers(struct ftrace_event_call *call)
1556{
1557 struct ftrace_event_file *file;
1558 struct trace_array *tr;
1559
1560 do_for_each_event_file_safe(tr, file) {
ae63b31e
SR
1561 if (file->event_call != call)
1562 continue;
1563
f6a84bdc 1564 remove_event_file_dir(file);
ae63b31e
SR
1565 /*
1566 * The do_for_each_event_file_safe() is
1567 * a double loop. After finding the call for this
1568 * trace_array, we use break to jump to the next
1569 * trace_array.
1570 */
1571 break;
1572 } while_for_each_event_file();
1573}
1574
8781915a
EG
1575static void event_remove(struct ftrace_event_call *call)
1576{
ae63b31e
SR
1577 struct trace_array *tr;
1578 struct ftrace_event_file *file;
1579
1580 do_for_each_event_file(tr, file) {
1581 if (file->event_call != call)
1582 continue;
1583 ftrace_event_enable_disable(file, 0);
1584 /*
1585 * The do_for_each_event_file() is
1586 * a double loop. After finding the call for this
1587 * trace_array, we use break to jump to the next
1588 * trace_array.
1589 */
1590 break;
1591 } while_for_each_event_file();
1592
8781915a
EG
1593 if (call->event.funcs)
1594 __unregister_ftrace_event(&call->event);
ae63b31e 1595 remove_event_from_tracers(call);
8781915a
EG
1596 list_del(&call->list);
1597}
1598
1599static int event_init(struct ftrace_event_call *call)
1600{
1601 int ret = 0;
1602
1603 if (WARN_ON(!call->name))
1604 return -EINVAL;
1605
1606 if (call->class->raw_init) {
1607 ret = call->class->raw_init(call);
1608 if (ret < 0 && ret != -ENOSYS)
1609 pr_warn("Could not initialize trace events/%s\n",
1610 call->name);
1611 }
1612
1613 return ret;
1614}
1615
67ead0a6 1616static int
ae63b31e 1617__register_event(struct ftrace_event_call *call, struct module *mod)
bd1a5c84 1618{
bd1a5c84 1619 int ret;
6d723736 1620
8781915a
EG
1621 ret = event_init(call);
1622 if (ret < 0)
1623 return ret;
701970b3 1624
ae63b31e 1625 list_add(&call->list, &ftrace_events);
67ead0a6 1626 call->mod = mod;
88f70d75 1627
ae63b31e 1628 return 0;
bd1a5c84
MH
1629}
1630
da511bf3
SRRH
1631static struct ftrace_event_file *
1632trace_create_new_event(struct ftrace_event_call *call,
1633 struct trace_array *tr)
1634{
1635 struct ftrace_event_file *file;
1636
1637 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1638 if (!file)
1639 return NULL;
1640
1641 file->event_call = call;
1642 file->tr = tr;
1643 atomic_set(&file->sm_ref, 0);
1644 list_add(&file->list, &tr->events);
1645
1646 return file;
1647}
1648
ae63b31e
SR
1649/* Add an event to a trace directory */
1650static int
1651__trace_add_new_event(struct ftrace_event_call *call,
1652 struct trace_array *tr,
1653 const struct file_operations *id,
1654 const struct file_operations *enable,
1655 const struct file_operations *filter,
1656 const struct file_operations *format)
1657{
1658 struct ftrace_event_file *file;
1659
da511bf3 1660 file = trace_create_new_event(call, tr);
ae63b31e
SR
1661 if (!file)
1662 return -ENOMEM;
1663
ae63b31e
SR
1664 return event_create_dir(tr->event_dir, file, id, enable, filter, format);
1665}
1666
77248221
SR
1667/*
1668 * Just create a decriptor for early init. A descriptor is required
1669 * for enabling events at boot. We want to enable events before
1670 * the filesystem is initialized.
1671 */
1672static __init int
1673__trace_early_add_new_event(struct ftrace_event_call *call,
1674 struct trace_array *tr)
1675{
1676 struct ftrace_event_file *file;
1677
da511bf3 1678 file = trace_create_new_event(call, tr);
77248221
SR
1679 if (!file)
1680 return -ENOMEM;
1681
77248221
SR
1682 return 0;
1683}
1684
ae63b31e
SR
1685struct ftrace_module_file_ops;
1686static void __add_event_to_tracers(struct ftrace_event_call *call,
1687 struct ftrace_module_file_ops *file_ops);
1688
bd1a5c84
MH
1689/* Add an additional event_call dynamically */
1690int trace_add_event_call(struct ftrace_event_call *call)
1691{
1692 int ret;
a8227415 1693 mutex_lock(&trace_types_lock);
bd1a5c84 1694 mutex_lock(&event_mutex);
701970b3 1695
ae63b31e
SR
1696 ret = __register_event(call, NULL);
1697 if (ret >= 0)
1698 __add_event_to_tracers(call, NULL);
a2ca5e03 1699
ae63b31e 1700 mutex_unlock(&event_mutex);
a8227415 1701 mutex_unlock(&trace_types_lock);
ae63b31e 1702 return ret;
a2ca5e03
FW
1703}
1704
4fead8e4 1705/*
a8227415
AL
1706 * Must be called under locking of trace_types_lock, event_mutex and
1707 * trace_event_sem.
4fead8e4 1708 */
bd1a5c84
MH
1709static void __trace_remove_event_call(struct ftrace_event_call *call)
1710{
8781915a 1711 event_remove(call);
bd1a5c84
MH
1712 trace_destroy_fields(call);
1713 destroy_preds(call);
bd1a5c84
MH
1714}
1715
2816c551
ON
1716static int probe_remove_event_call(struct ftrace_event_call *call)
1717{
1718 struct trace_array *tr;
1719 struct ftrace_event_file *file;
1720
1721#ifdef CONFIG_PERF_EVENTS
1722 if (call->perf_refcount)
1723 return -EBUSY;
1724#endif
1725 do_for_each_event_file(tr, file) {
1726 if (file->event_call != call)
1727 continue;
1728 /*
1729 * We can't rely on ftrace_event_enable_disable(enable => 0)
1730 * we are going to do, FTRACE_EVENT_FL_SOFT_MODE can suppress
1731 * TRACE_REG_UNREGISTER.
1732 */
1733 if (file->flags & FTRACE_EVENT_FL_ENABLED)
1734 return -EBUSY;
1735 break;
1736 } while_for_each_event_file();
1737
1738 __trace_remove_event_call(call);
1739
1740 return 0;
1741}
1742
bd1a5c84 1743/* Remove an event_call */
2816c551 1744int trace_remove_event_call(struct ftrace_event_call *call)
bd1a5c84 1745{
2816c551
ON
1746 int ret;
1747
a8227415 1748 mutex_lock(&trace_types_lock);
bd1a5c84 1749 mutex_lock(&event_mutex);
52f6ad6d 1750 down_write(&trace_event_sem);
2816c551 1751 ret = probe_remove_event_call(call);
52f6ad6d 1752 up_write(&trace_event_sem);
bd1a5c84 1753 mutex_unlock(&event_mutex);
a8227415 1754 mutex_unlock(&trace_types_lock);
2816c551
ON
1755
1756 return ret;
bd1a5c84
MH
1757}
1758
1759#define for_each_event(event, start, end) \
1760 for (event = start; \
1761 (unsigned long)event < (unsigned long)end; \
1762 event++)
1763
1764#ifdef CONFIG_MODULES
1765
1766static LIST_HEAD(ftrace_module_file_list);
1767
1768/*
1769 * Modules must own their file_operations to keep up with
1770 * reference counting.
1771 */
1772struct ftrace_module_file_ops {
1773 struct list_head list;
1774 struct module *mod;
1775 struct file_operations id;
1776 struct file_operations enable;
1777 struct file_operations format;
1778 struct file_operations filter;
1779};
1780
315326c1
SRRH
1781static struct ftrace_module_file_ops *
1782find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
ae63b31e 1783{
315326c1
SRRH
1784 /*
1785 * As event_calls are added in groups by module,
1786 * when we find one file_ops, we don't need to search for
1787 * each call in that module, as the rest should be the
1788 * same. Only search for a new one if the last one did
1789 * not match.
1790 */
1791 if (file_ops && mod == file_ops->mod)
1792 return file_ops;
ae63b31e
SR
1793
1794 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1795 if (file_ops->mod == mod)
1796 return file_ops;
1797 }
1798 return NULL;
1799}
1800
701970b3
SR
1801static struct ftrace_module_file_ops *
1802trace_create_file_ops(struct module *mod)
1803{
1804 struct ftrace_module_file_ops *file_ops;
1805
1806 /*
1807 * This is a bit of a PITA. To allow for correct reference
1808 * counting, modules must "own" their file_operations.
1809 * To do this, we allocate the file operations that will be
1810 * used in the event directory.
1811 */
1812
1813 file_ops = kmalloc(sizeof(*file_ops), GFP_KERNEL);
1814 if (!file_ops)
1815 return NULL;
1816
1817 file_ops->mod = mod;
1818
1819 file_ops->id = ftrace_event_id_fops;
1820 file_ops->id.owner = mod;
1821
1822 file_ops->enable = ftrace_enable_fops;
1823 file_ops->enable.owner = mod;
1824
1825 file_ops->filter = ftrace_event_filter_fops;
1826 file_ops->filter.owner = mod;
1827
1828 file_ops->format = ftrace_event_format_fops;
1829 file_ops->format.owner = mod;
1830
1831 list_add(&file_ops->list, &ftrace_module_file_list);
1832
1833 return file_ops;
1834}
1835
6d723736
SR
1836static void trace_module_add_events(struct module *mod)
1837{
701970b3 1838 struct ftrace_module_file_ops *file_ops = NULL;
e4a9ea5e 1839 struct ftrace_event_call **call, **start, **end;
6d723736
SR
1840
1841 start = mod->trace_events;
1842 end = mod->trace_events + mod->num_trace_events;
1843
1844 if (start == end)
1845 return;
1846
67ead0a6
LZ
1847 file_ops = trace_create_file_ops(mod);
1848 if (!file_ops)
6d723736
SR
1849 return;
1850
1851 for_each_event(call, start, end) {
ae63b31e
SR
1852 __register_event(*call, mod);
1853 __add_event_to_tracers(*call, file_ops);
6d723736
SR
1854 }
1855}
1856
1857static void trace_module_remove_events(struct module *mod)
1858{
701970b3 1859 struct ftrace_module_file_ops *file_ops;
6d723736 1860 struct ftrace_event_call *call, *p;
575380da 1861 bool clear_trace = false;
6d723736 1862
52f6ad6d 1863 down_write(&trace_event_sem);
6d723736
SR
1864 list_for_each_entry_safe(call, p, &ftrace_events, list) {
1865 if (call->mod == mod) {
575380da
SRRH
1866 if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
1867 clear_trace = true;
bd1a5c84 1868 __trace_remove_event_call(call);
6d723736
SR
1869 }
1870 }
701970b3
SR
1871
1872 /* Now free the file_operations */
1873 list_for_each_entry(file_ops, &ftrace_module_file_list, list) {
1874 if (file_ops->mod == mod)
1875 break;
1876 }
1877 if (&file_ops->list != &ftrace_module_file_list) {
1878 list_del(&file_ops->list);
1879 kfree(file_ops);
1880 }
52f6ad6d 1881 up_write(&trace_event_sem);
9456f0fa
SR
1882
1883 /*
1884 * It is safest to reset the ring buffer if the module being unloaded
873c642f
SRRH
1885 * registered any events that were used. The only worry is if
1886 * a new module gets loaded, and takes on the same id as the events
1887 * of this module. When printing out the buffer, traced events left
1888 * over from this module may be passed to the new module events and
1889 * unexpected results may occur.
9456f0fa 1890 */
575380da 1891 if (clear_trace)
873c642f 1892 tracing_reset_all_online_cpus();
6d723736
SR
1893}
1894
61f919a1
SR
1895static int trace_module_notify(struct notifier_block *self,
1896 unsigned long val, void *data)
6d723736
SR
1897{
1898 struct module *mod = data;
1899
a8227415 1900 mutex_lock(&trace_types_lock);
6d723736
SR
1901 mutex_lock(&event_mutex);
1902 switch (val) {
1903 case MODULE_STATE_COMING:
1904 trace_module_add_events(mod);
1905 break;
1906 case MODULE_STATE_GOING:
1907 trace_module_remove_events(mod);
1908 break;
1909 }
1910 mutex_unlock(&event_mutex);
a8227415 1911 mutex_unlock(&trace_types_lock);
fd994989 1912
1473e441
SR
1913 return 0;
1914}
315326c1
SRRH
1915
1916static int
1917__trace_add_new_mod_event(struct ftrace_event_call *call,
1918 struct trace_array *tr,
1919 struct ftrace_module_file_ops *file_ops)
1920{
1921 return __trace_add_new_event(call, tr,
1922 &file_ops->id, &file_ops->enable,
1923 &file_ops->filter, &file_ops->format);
1924}
1925
61f919a1 1926#else
315326c1
SRRH
1927static inline struct ftrace_module_file_ops *
1928find_ftrace_file_ops(struct ftrace_module_file_ops *file_ops, struct module *mod)
ae63b31e
SR
1929{
1930 return NULL;
1931}
315326c1
SRRH
1932static inline int trace_module_notify(struct notifier_block *self,
1933 unsigned long val, void *data)
61f919a1
SR
1934{
1935 return 0;
1936}
315326c1
SRRH
1937static inline int
1938__trace_add_new_mod_event(struct ftrace_event_call *call,
1939 struct trace_array *tr,
1940 struct ftrace_module_file_ops *file_ops)
1941{
1942 return -ENODEV;
1943}
61f919a1 1944#endif /* CONFIG_MODULES */
1473e441 1945
ae63b31e
SR
1946/* Create a new event directory structure for a trace directory. */
1947static void
1948__trace_add_event_dirs(struct trace_array *tr)
1949{
1950 struct ftrace_module_file_ops *file_ops = NULL;
1951 struct ftrace_event_call *call;
1952 int ret;
1953
1954 list_for_each_entry(call, &ftrace_events, list) {
1955 if (call->mod) {
1956 /*
1957 * Directories for events by modules need to
1958 * keep module ref counts when opened (as we don't
1959 * want the module to disappear when reading one
1960 * of these files). The file_ops keep account of
1961 * the module ref count.
ae63b31e 1962 */
315326c1 1963 file_ops = find_ftrace_file_ops(file_ops, call->mod);
ae63b31e
SR
1964 if (!file_ops)
1965 continue; /* Warn? */
315326c1 1966 ret = __trace_add_new_mod_event(call, tr, file_ops);
ae63b31e
SR
1967 if (ret < 0)
1968 pr_warning("Could not create directory for event %s\n",
1969 call->name);
1970 continue;
1971 }
1972 ret = __trace_add_new_event(call, tr,
1973 &ftrace_event_id_fops,
1974 &ftrace_enable_fops,
1975 &ftrace_event_filter_fops,
1976 &ftrace_event_format_fops);
1977 if (ret < 0)
1978 pr_warning("Could not create directory for event %s\n",
1979 call->name);
1980 }
1981}
1982
3cd715de
SRRH
1983#ifdef CONFIG_DYNAMIC_FTRACE
1984
1985/* Avoid typos */
1986#define ENABLE_EVENT_STR "enable_event"
1987#define DISABLE_EVENT_STR "disable_event"
1988
1989struct event_probe_data {
1990 struct ftrace_event_file *file;
1991 unsigned long count;
1992 int ref;
1993 bool enable;
1994};
1995
1996static struct ftrace_event_file *
1997find_event_file(struct trace_array *tr, const char *system, const char *event)
1998{
1999 struct ftrace_event_file *file;
2000 struct ftrace_event_call *call;
2001
2002 list_for_each_entry(file, &tr->events, list) {
2003
2004 call = file->event_call;
2005
2006 if (!call->name || !call->class || !call->class->reg)
2007 continue;
2008
2009 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
2010 continue;
2011
2012 if (strcmp(event, call->name) == 0 &&
2013 strcmp(system, call->class->system) == 0)
2014 return file;
2015 }
2016 return NULL;
2017}
2018
2019static void
2020event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2021{
2022 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2023 struct event_probe_data *data = *pdata;
2024
2025 if (!data)
2026 return;
2027
2028 if (data->enable)
2029 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
2030 else
2031 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
2032}
2033
2034static void
2035event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
2036{
2037 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2038 struct event_probe_data *data = *pdata;
2039
2040 if (!data)
2041 return;
2042
2043 if (!data->count)
2044 return;
2045
2046 /* Skip if the event is in a state we want to switch to */
2047 if (data->enable == !(data->file->flags & FTRACE_EVENT_FL_SOFT_DISABLED))
2048 return;
2049
2050 if (data->count != -1)
2051 (data->count)--;
2052
2053 event_enable_probe(ip, parent_ip, _data);
2054}
2055
2056static int
2057event_enable_print(struct seq_file *m, unsigned long ip,
2058 struct ftrace_probe_ops *ops, void *_data)
2059{
2060 struct event_probe_data *data = _data;
2061
2062 seq_printf(m, "%ps:", (void *)ip);
2063
2064 seq_printf(m, "%s:%s:%s",
2065 data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2066 data->file->event_call->class->system,
2067 data->file->event_call->name);
2068
2069 if (data->count == -1)
2070 seq_printf(m, ":unlimited\n");
2071 else
2072 seq_printf(m, ":count=%ld\n", data->count);
2073
2074 return 0;
2075}
2076
2077static int
2078event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
2079 void **_data)
2080{
2081 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2082 struct event_probe_data *data = *pdata;
2083
2084 data->ref++;
2085 return 0;
2086}
2087
2088static void
2089event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
2090 void **_data)
2091{
2092 struct event_probe_data **pdata = (struct event_probe_data **)_data;
2093 struct event_probe_data *data = *pdata;
2094
2095 if (WARN_ON_ONCE(data->ref <= 0))
2096 return;
2097
2098 data->ref--;
2099 if (!data->ref) {
2100 /* Remove the SOFT_MODE flag */
2101 __ftrace_event_enable_disable(data->file, 0, 1);
2102 module_put(data->file->event_call->mod);
2103 kfree(data);
2104 }
2105 *pdata = NULL;
2106}
2107
2108static struct ftrace_probe_ops event_enable_probe_ops = {
2109 .func = event_enable_probe,
2110 .print = event_enable_print,
2111 .init = event_enable_init,
2112 .free = event_enable_free,
2113};
2114
2115static struct ftrace_probe_ops event_enable_count_probe_ops = {
2116 .func = event_enable_count_probe,
2117 .print = event_enable_print,
2118 .init = event_enable_init,
2119 .free = event_enable_free,
2120};
2121
2122static struct ftrace_probe_ops event_disable_probe_ops = {
2123 .func = event_enable_probe,
2124 .print = event_enable_print,
2125 .init = event_enable_init,
2126 .free = event_enable_free,
2127};
2128
2129static struct ftrace_probe_ops event_disable_count_probe_ops = {
2130 .func = event_enable_count_probe,
2131 .print = event_enable_print,
2132 .init = event_enable_init,
2133 .free = event_enable_free,
2134};
2135
2136static int
2137event_enable_func(struct ftrace_hash *hash,
2138 char *glob, char *cmd, char *param, int enabled)
2139{
2140 struct trace_array *tr = top_trace_array();
2141 struct ftrace_event_file *file;
2142 struct ftrace_probe_ops *ops;
2143 struct event_probe_data *data;
2144 const char *system;
2145 const char *event;
2146 char *number;
2147 bool enable;
2148 int ret;
2149
2150 /* hash funcs only work with set_ftrace_filter */
8092e808 2151 if (!enabled || !param)
3cd715de
SRRH
2152 return -EINVAL;
2153
2154 system = strsep(&param, ":");
2155 if (!param)
2156 return -EINVAL;
2157
2158 event = strsep(&param, ":");
2159
2160 mutex_lock(&event_mutex);
2161
2162 ret = -EINVAL;
2163 file = find_event_file(tr, system, event);
2164 if (!file)
2165 goto out;
2166
2167 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2168
2169 if (enable)
2170 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2171 else
2172 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2173
2174 if (glob[0] == '!') {
2175 unregister_ftrace_function_probe_func(glob+1, ops);
2176 ret = 0;
2177 goto out;
2178 }
2179
2180 ret = -ENOMEM;
2181 data = kzalloc(sizeof(*data), GFP_KERNEL);
2182 if (!data)
2183 goto out;
2184
2185 data->enable = enable;
2186 data->count = -1;
2187 data->file = file;
2188
2189 if (!param)
2190 goto out_reg;
2191
2192 number = strsep(&param, ":");
2193
2194 ret = -EINVAL;
2195 if (!strlen(number))
2196 goto out_free;
2197
2198 /*
2199 * We use the callback data field (which is a pointer)
2200 * as our counter.
2201 */
2202 ret = kstrtoul(number, 0, &data->count);
2203 if (ret)
2204 goto out_free;
2205
2206 out_reg:
2207 /* Don't let event modules unload while probe registered */
2208 ret = try_module_get(file->event_call->mod);
6ed01066
MH
2209 if (!ret) {
2210 ret = -EBUSY;
3cd715de 2211 goto out_free;
6ed01066 2212 }
3cd715de
SRRH
2213
2214 ret = __ftrace_event_enable_disable(file, 1, 1);
2215 if (ret < 0)
2216 goto out_put;
2217 ret = register_ftrace_function_probe(glob, ops, data);
ff305ded
SRRH
2218 /*
2219 * The above returns on success the # of functions enabled,
2220 * but if it didn't find any functions it returns zero.
2221 * Consider no functions a failure too.
2222 */
a5b85bd1
MH
2223 if (!ret) {
2224 ret = -ENOENT;
3cd715de 2225 goto out_disable;
ff305ded
SRRH
2226 } else if (ret < 0)
2227 goto out_disable;
2228 /* Just return zero, not the number of enabled functions */
2229 ret = 0;
3cd715de
SRRH
2230 out:
2231 mutex_unlock(&event_mutex);
2232 return ret;
2233
2234 out_disable:
2235 __ftrace_event_enable_disable(file, 0, 1);
2236 out_put:
2237 module_put(file->event_call->mod);
2238 out_free:
2239 kfree(data);
2240 goto out;
2241}
2242
2243static struct ftrace_func_command event_enable_cmd = {
2244 .name = ENABLE_EVENT_STR,
2245 .func = event_enable_func,
2246};
2247
2248static struct ftrace_func_command event_disable_cmd = {
2249 .name = DISABLE_EVENT_STR,
2250 .func = event_enable_func,
2251};
2252
2253static __init int register_event_cmds(void)
2254{
2255 int ret;
2256
2257 ret = register_ftrace_command(&event_enable_cmd);
2258 if (WARN_ON(ret < 0))
2259 return ret;
2260 ret = register_ftrace_command(&event_disable_cmd);
2261 if (WARN_ON(ret < 0))
2262 unregister_ftrace_command(&event_enable_cmd);
2263 return ret;
2264}
2265#else
2266static inline int register_event_cmds(void) { return 0; }
2267#endif /* CONFIG_DYNAMIC_FTRACE */
2268
77248221
SR
2269/*
2270 * The top level array has already had its ftrace_event_file
2271 * descriptors created in order to allow for early events to
2272 * be recorded. This function is called after the debugfs has been
2273 * initialized, and we now have to create the files associated
2274 * to the events.
2275 */
2276static __init void
2277__trace_early_add_event_dirs(struct trace_array *tr)
2278{
2279 struct ftrace_event_file *file;
2280 int ret;
2281
2282
2283 list_for_each_entry(file, &tr->events, list) {
2284 ret = event_create_dir(tr->event_dir, file,
2285 &ftrace_event_id_fops,
2286 &ftrace_enable_fops,
2287 &ftrace_event_filter_fops,
2288 &ftrace_event_format_fops);
2289 if (ret < 0)
2290 pr_warning("Could not create directory for event %s\n",
2291 file->event_call->name);
2292 }
2293}
2294
2295/*
2296 * For early boot up, the top trace array requires to have
2297 * a list of events that can be enabled. This must be done before
2298 * the filesystem is set up in order to allow events to be traced
2299 * early.
2300 */
2301static __init void
2302__trace_early_add_events(struct trace_array *tr)
2303{
2304 struct ftrace_event_call *call;
2305 int ret;
2306
2307 list_for_each_entry(call, &ftrace_events, list) {
2308 /* Early boot up should not have any modules loaded */
2309 if (WARN_ON_ONCE(call->mod))
2310 continue;
2311
2312 ret = __trace_early_add_new_event(call, tr);
2313 if (ret < 0)
2314 pr_warning("Could not create early event %s\n",
2315 call->name);
2316 }
2317}
2318
0c8916c3
SR
2319/* Remove the event directory structure for a trace directory. */
2320static void
2321__trace_remove_event_dirs(struct trace_array *tr)
2322{
2323 struct ftrace_event_file *file, *next;
2324
f6a84bdc
ON
2325 list_for_each_entry_safe(file, next, &tr->events, list)
2326 remove_event_file_dir(file);
0c8916c3
SR
2327}
2328
ae63b31e
SR
2329static void
2330__add_event_to_tracers(struct ftrace_event_call *call,
2331 struct ftrace_module_file_ops *file_ops)
2332{
2333 struct trace_array *tr;
2334
2335 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
2336 if (file_ops)
315326c1 2337 __trace_add_new_mod_event(call, tr, file_ops);
ae63b31e
SR
2338 else
2339 __trace_add_new_event(call, tr,
2340 &ftrace_event_id_fops,
2341 &ftrace_enable_fops,
2342 &ftrace_event_filter_fops,
2343 &ftrace_event_format_fops);
2344 }
2345}
2346
ec827c7e 2347static struct notifier_block trace_module_nb = {
6d723736
SR
2348 .notifier_call = trace_module_notify,
2349 .priority = 0,
2350};
2351
e4a9ea5e
SR
2352extern struct ftrace_event_call *__start_ftrace_events[];
2353extern struct ftrace_event_call *__stop_ftrace_events[];
a59fd602 2354
020e5f85
LZ
2355static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2356
2357static __init int setup_trace_event(char *str)
2358{
2359 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
55034cd6
SRRH
2360 ring_buffer_expanded = true;
2361 tracing_selftest_disabled = true;
020e5f85
LZ
2362
2363 return 1;
2364}
2365__setup("trace_event=", setup_trace_event);
2366
77248221
SR
2367/* Expects to have event_mutex held when called */
2368static int
2369create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
ae63b31e
SR
2370{
2371 struct dentry *d_events;
2372 struct dentry *entry;
2373
2374 entry = debugfs_create_file("set_event", 0644, parent,
2375 tr, &ftrace_set_event_fops);
2376 if (!entry) {
2377 pr_warning("Could not create debugfs 'set_event' entry\n");
2378 return -ENOMEM;
2379 }
2380
2381 d_events = debugfs_create_dir("events", parent);
277ba044 2382 if (!d_events) {
ae63b31e 2383 pr_warning("Could not create debugfs 'events' directory\n");
277ba044
SR
2384 return -ENOMEM;
2385 }
ae63b31e
SR
2386
2387 /* ring buffer internal formats */
2388 trace_create_file("header_page", 0444, d_events,
2389 ring_buffer_print_page_header,
2390 &ftrace_show_header_fops);
2391
2392 trace_create_file("header_event", 0444, d_events,
2393 ring_buffer_print_entry_header,
2394 &ftrace_show_header_fops);
2395
2396 trace_create_file("enable", 0644, d_events,
2397 tr, &ftrace_tr_enable_fops);
2398
2399 tr->event_dir = d_events;
77248221
SR
2400
2401 return 0;
2402}
2403
2404/**
2405 * event_trace_add_tracer - add a instance of a trace_array to events
2406 * @parent: The parent dentry to place the files/directories for events in
2407 * @tr: The trace array associated with these events
2408 *
2409 * When a new instance is created, it needs to set up its events
2410 * directory, as well as other files associated with events. It also
2411 * creates the event hierachry in the @parent/events directory.
2412 *
2413 * Returns 0 on success.
2414 */
2415int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2416{
2417 int ret;
2418
2419 mutex_lock(&event_mutex);
2420
2421 ret = create_event_toplevel_files(parent, tr);
2422 if (ret)
2423 goto out_unlock;
2424
52f6ad6d 2425 down_write(&trace_event_sem);
ae63b31e 2426 __trace_add_event_dirs(tr);
52f6ad6d 2427 up_write(&trace_event_sem);
277ba044 2428
77248221 2429 out_unlock:
277ba044 2430 mutex_unlock(&event_mutex);
ae63b31e 2431
77248221
SR
2432 return ret;
2433}
2434
2435/*
2436 * The top trace array already had its file descriptors created.
2437 * Now the files themselves need to be created.
2438 */
2439static __init int
2440early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2441{
2442 int ret;
2443
2444 mutex_lock(&event_mutex);
2445
2446 ret = create_event_toplevel_files(parent, tr);
2447 if (ret)
2448 goto out_unlock;
2449
52f6ad6d 2450 down_write(&trace_event_sem);
77248221 2451 __trace_early_add_event_dirs(tr);
52f6ad6d 2452 up_write(&trace_event_sem);
77248221
SR
2453
2454 out_unlock:
2455 mutex_unlock(&event_mutex);
2456
2457 return ret;
ae63b31e
SR
2458}
2459
0c8916c3
SR
2460int event_trace_del_tracer(struct trace_array *tr)
2461{
0c8916c3
SR
2462 mutex_lock(&event_mutex);
2463
2a6c24af
SRRH
2464 /* Disable any running events */
2465 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
2466
52f6ad6d 2467 down_write(&trace_event_sem);
0c8916c3
SR
2468 __trace_remove_event_dirs(tr);
2469 debugfs_remove_recursive(tr->event_dir);
52f6ad6d 2470 up_write(&trace_event_sem);
0c8916c3
SR
2471
2472 tr->event_dir = NULL;
2473
2474 mutex_unlock(&event_mutex);
2475
2476 return 0;
2477}
2478
d1a29143
SR
2479static __init int event_trace_memsetup(void)
2480{
2481 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
2482 file_cachep = KMEM_CACHE(ftrace_event_file, SLAB_PANIC);
2483 return 0;
2484}
2485
8781915a
EG
2486static __init int event_trace_enable(void)
2487{
ae63b31e 2488 struct trace_array *tr = top_trace_array();
8781915a
EG
2489 struct ftrace_event_call **iter, *call;
2490 char *buf = bootup_event_buf;
2491 char *token;
2492 int ret;
2493
2494 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
2495
2496 call = *iter;
2497 ret = event_init(call);
2498 if (!ret)
2499 list_add(&call->list, &ftrace_events);
2500 }
2501
77248221
SR
2502 /*
2503 * We need the top trace array to have a working set of trace
2504 * points at early init, before the debug files and directories
2505 * are created. Create the file entries now, and attach them
2506 * to the actual file dentries later.
2507 */
2508 __trace_early_add_events(tr);
2509
8781915a
EG
2510 while (true) {
2511 token = strsep(&buf, ",");
2512
2513 if (!token)
2514 break;
2515 if (!*token)
2516 continue;
2517
ae63b31e 2518 ret = ftrace_set_clr_event(tr, token, 1);
8781915a
EG
2519 if (ret)
2520 pr_warn("Failed to enable trace event: %s\n", token);
2521 }
81698831
SR
2522
2523 trace_printk_start_comm();
2524
3cd715de
SRRH
2525 register_event_cmds();
2526
8781915a
EG
2527 return 0;
2528}
2529
b77e38aa
SR
2530static __init int event_trace_init(void)
2531{
ae63b31e 2532 struct trace_array *tr;
b77e38aa
SR
2533 struct dentry *d_tracer;
2534 struct dentry *entry;
6d723736 2535 int ret;
b77e38aa 2536
ae63b31e
SR
2537 tr = top_trace_array();
2538
b77e38aa
SR
2539 d_tracer = tracing_init_dentry();
2540 if (!d_tracer)
2541 return 0;
2542
2314c4ae 2543 entry = debugfs_create_file("available_events", 0444, d_tracer,
ae63b31e 2544 tr, &ftrace_avail_fops);
2314c4ae
SR
2545 if (!entry)
2546 pr_warning("Could not create debugfs "
2547 "'available_events' entry\n");
2548
8728fe50
LZ
2549 if (trace_define_common_fields())
2550 pr_warning("tracing: Failed to allocate common fields");
2551
77248221 2552 ret = early_event_add_tracer(d_tracer, tr);
ae63b31e
SR
2553 if (ret)
2554 return ret;
020e5f85 2555
6d723736 2556 ret = register_module_notifier(&trace_module_nb);
55379376 2557 if (ret)
6d723736
SR
2558 pr_warning("Failed to register trace events module notifier\n");
2559
b77e38aa
SR
2560 return 0;
2561}
d1a29143 2562early_initcall(event_trace_memsetup);
8781915a 2563core_initcall(event_trace_enable);
b77e38aa 2564fs_initcall(event_trace_init);
e6187007
SR
2565
2566#ifdef CONFIG_FTRACE_STARTUP_TEST
2567
2568static DEFINE_SPINLOCK(test_spinlock);
2569static DEFINE_SPINLOCK(test_spinlock_irq);
2570static DEFINE_MUTEX(test_mutex);
2571
2572static __init void test_work(struct work_struct *dummy)
2573{
2574 spin_lock(&test_spinlock);
2575 spin_lock_irq(&test_spinlock_irq);
2576 udelay(1);
2577 spin_unlock_irq(&test_spinlock_irq);
2578 spin_unlock(&test_spinlock);
2579
2580 mutex_lock(&test_mutex);
2581 msleep(1);
2582 mutex_unlock(&test_mutex);
2583}
2584
2585static __init int event_test_thread(void *unused)
2586{
2587 void *test_malloc;
2588
2589 test_malloc = kmalloc(1234, GFP_KERNEL);
2590 if (!test_malloc)
2591 pr_info("failed to kmalloc\n");
2592
2593 schedule_on_each_cpu(test_work);
2594
2595 kfree(test_malloc);
2596
2597 set_current_state(TASK_INTERRUPTIBLE);
2598 while (!kthread_should_stop())
2599 schedule();
2600
2601 return 0;
2602}
2603
2604/*
2605 * Do various things that may trigger events.
2606 */
2607static __init void event_test_stuff(void)
2608{
2609 struct task_struct *test_thread;
2610
2611 test_thread = kthread_run(event_test_thread, NULL, "test-events");
2612 msleep(1);
2613 kthread_stop(test_thread);
2614}
2615
2616/*
2617 * For every trace event defined, we will test each trace point separately,
2618 * and then by groups, and finally all trace points.
2619 */
9ea21c1e 2620static __init void event_trace_self_tests(void)
e6187007 2621{
ae63b31e
SR
2622 struct ftrace_subsystem_dir *dir;
2623 struct ftrace_event_file *file;
e6187007
SR
2624 struct ftrace_event_call *call;
2625 struct event_subsystem *system;
ae63b31e 2626 struct trace_array *tr;
e6187007
SR
2627 int ret;
2628
ae63b31e
SR
2629 tr = top_trace_array();
2630
e6187007
SR
2631 pr_info("Running tests on trace events:\n");
2632
ae63b31e
SR
2633 list_for_each_entry(file, &tr->events, list) {
2634
2635 call = file->event_call;
e6187007 2636
2239291a
SR
2637 /* Only test those that have a probe */
2638 if (!call->class || !call->class->probe)
e6187007
SR
2639 continue;
2640
1f5a6b45
SR
2641/*
2642 * Testing syscall events here is pretty useless, but
2643 * we still do it if configured. But this is time consuming.
2644 * What we really need is a user thread to perform the
2645 * syscalls as we test.
2646 */
2647#ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
8f082018
SR
2648 if (call->class->system &&
2649 strcmp(call->class->system, "syscalls") == 0)
1f5a6b45
SR
2650 continue;
2651#endif
2652
e6187007
SR
2653 pr_info("Testing event %s: ", call->name);
2654
2655 /*
2656 * If an event is already enabled, someone is using
2657 * it and the self test should not be on.
2658 */
ae63b31e 2659 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
e6187007
SR
2660 pr_warning("Enabled event during self test!\n");
2661 WARN_ON_ONCE(1);
2662 continue;
2663 }
2664
ae63b31e 2665 ftrace_event_enable_disable(file, 1);
e6187007 2666 event_test_stuff();
ae63b31e 2667 ftrace_event_enable_disable(file, 0);
e6187007
SR
2668
2669 pr_cont("OK\n");
2670 }
2671
2672 /* Now test at the sub system level */
2673
2674 pr_info("Running tests on trace event systems:\n");
2675
ae63b31e
SR
2676 list_for_each_entry(dir, &tr->systems, list) {
2677
2678 system = dir->subsystem;
e6187007
SR
2679
2680 /* the ftrace system is special, skip it */
2681 if (strcmp(system->name, "ftrace") == 0)
2682 continue;
2683
2684 pr_info("Testing event system %s: ", system->name);
2685
ae63b31e 2686 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
e6187007
SR
2687 if (WARN_ON_ONCE(ret)) {
2688 pr_warning("error enabling system %s\n",
2689 system->name);
2690 continue;
2691 }
2692
2693 event_test_stuff();
2694
ae63b31e 2695 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
76bab1b7 2696 if (WARN_ON_ONCE(ret)) {
e6187007
SR
2697 pr_warning("error disabling system %s\n",
2698 system->name);
76bab1b7
YL
2699 continue;
2700 }
e6187007
SR
2701
2702 pr_cont("OK\n");
2703 }
2704
2705 /* Test with all events enabled */
2706
2707 pr_info("Running tests on all trace events:\n");
2708 pr_info("Testing all events: ");
2709
ae63b31e 2710 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
e6187007 2711 if (WARN_ON_ONCE(ret)) {
e6187007 2712 pr_warning("error enabling all events\n");
9ea21c1e 2713 return;
e6187007
SR
2714 }
2715
2716 event_test_stuff();
2717
2718 /* reset sysname */
ae63b31e 2719 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
e6187007
SR
2720 if (WARN_ON_ONCE(ret)) {
2721 pr_warning("error disabling all events\n");
9ea21c1e 2722 return;
e6187007
SR
2723 }
2724
2725 pr_cont("OK\n");
9ea21c1e
SR
2726}
2727
2728#ifdef CONFIG_FUNCTION_TRACER
2729
245b2e70 2730static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
9ea21c1e
SR
2731
2732static void
2f5f6ad9 2733function_test_events_call(unsigned long ip, unsigned long parent_ip,
a1e2e31d 2734 struct ftrace_ops *op, struct pt_regs *pt_regs)
9ea21c1e
SR
2735{
2736 struct ring_buffer_event *event;
e77405ad 2737 struct ring_buffer *buffer;
9ea21c1e
SR
2738 struct ftrace_entry *entry;
2739 unsigned long flags;
2740 long disabled;
9ea21c1e
SR
2741 int cpu;
2742 int pc;
2743
2744 pc = preempt_count();
5168ae50 2745 preempt_disable_notrace();
9ea21c1e 2746 cpu = raw_smp_processor_id();
245b2e70 2747 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
9ea21c1e
SR
2748
2749 if (disabled != 1)
2750 goto out;
2751
2752 local_save_flags(flags);
2753
e77405ad
SR
2754 event = trace_current_buffer_lock_reserve(&buffer,
2755 TRACE_FN, sizeof(*entry),
9ea21c1e
SR
2756 flags, pc);
2757 if (!event)
2758 goto out;
2759 entry = ring_buffer_event_data(event);
2760 entry->ip = ip;
2761 entry->parent_ip = parent_ip;
2762
0d5c6e1c 2763 trace_buffer_unlock_commit(buffer, event, flags, pc);
9ea21c1e
SR
2764
2765 out:
245b2e70 2766 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
5168ae50 2767 preempt_enable_notrace();
9ea21c1e
SR
2768}
2769
2770static struct ftrace_ops trace_ops __initdata =
2771{
2772 .func = function_test_events_call,
4740974a 2773 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
9ea21c1e
SR
2774};
2775
2776static __init void event_trace_self_test_with_function(void)
2777{
17bb615a
SR
2778 int ret;
2779 ret = register_ftrace_function(&trace_ops);
2780 if (WARN_ON(ret < 0)) {
2781 pr_info("Failed to enable function tracer for event tests\n");
2782 return;
2783 }
9ea21c1e
SR
2784 pr_info("Running tests again, along with the function tracer\n");
2785 event_trace_self_tests();
2786 unregister_ftrace_function(&trace_ops);
2787}
2788#else
2789static __init void event_trace_self_test_with_function(void)
2790{
2791}
2792#endif
2793
2794static __init int event_trace_self_tests_init(void)
2795{
020e5f85
LZ
2796 if (!tracing_selftest_disabled) {
2797 event_trace_self_tests();
2798 event_trace_self_test_with_function();
2799 }
e6187007
SR
2800
2801 return 0;
2802}
2803
28d20e2d 2804late_initcall(event_trace_self_tests_init);
e6187007
SR
2805
2806#endif