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