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