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