xref: /linux/kernel/trace/trace.c (revision 7f4f3b14e8079ecde096bd734af10e30d40c27b7)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * ring buffer based function tracer
4  *
5  * Copyright (C) 2007-2012 Steven Rostedt <srostedt@redhat.com>
6  * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7  *
8  * Originally taken from the RT patch by:
9  *    Arnaldo Carvalho de Melo <acme@redhat.com>
10  *
11  * Based on code from the latency_tracer, that is:
12  *  Copyright (C) 2004-2006 Ingo Molnar
13  *  Copyright (C) 2004 Nadia Yvette Chambers
14  */
15 #include <linux/ring_buffer.h>
16 #include <linux/utsname.h>
17 #include <linux/stacktrace.h>
18 #include <linux/writeback.h>
19 #include <linux/kallsyms.h>
20 #include <linux/security.h>
21 #include <linux/seq_file.h>
22 #include <linux/irqflags.h>
23 #include <linux/debugfs.h>
24 #include <linux/tracefs.h>
25 #include <linux/pagemap.h>
26 #include <linux/hardirq.h>
27 #include <linux/linkage.h>
28 #include <linux/uaccess.h>
29 #include <linux/vmalloc.h>
30 #include <linux/ftrace.h>
31 #include <linux/module.h>
32 #include <linux/percpu.h>
33 #include <linux/splice.h>
34 #include <linux/kdebug.h>
35 #include <linux/string.h>
36 #include <linux/mount.h>
37 #include <linux/rwsem.h>
38 #include <linux/slab.h>
39 #include <linux/ctype.h>
40 #include <linux/init.h>
41 #include <linux/panic_notifier.h>
42 #include <linux/poll.h>
43 #include <linux/nmi.h>
44 #include <linux/fs.h>
45 #include <linux/trace.h>
46 #include <linux/sched/clock.h>
47 #include <linux/sched/rt.h>
48 #include <linux/fsnotify.h>
49 #include <linux/irq_work.h>
50 #include <linux/workqueue.h>
51 
52 #include <asm/setup.h> /* COMMAND_LINE_SIZE */
53 
54 #include "trace.h"
55 #include "trace_output.h"
56 
57 #ifdef CONFIG_FTRACE_STARTUP_TEST
58 /*
59  * We need to change this state when a selftest is running.
60  * A selftest will lurk into the ring-buffer to count the
61  * entries inserted during the selftest although some concurrent
62  * insertions into the ring-buffer such as trace_printk could occurred
63  * at the same time, giving false positive or negative results.
64  */
65 static bool __read_mostly tracing_selftest_running;
66 
67 /*
68  * If boot-time tracing including tracers/events via kernel cmdline
69  * is running, we do not want to run SELFTEST.
70  */
71 bool __read_mostly tracing_selftest_disabled;
72 
73 void __init disable_tracing_selftest(const char *reason)
74 {
75 	if (!tracing_selftest_disabled) {
76 		tracing_selftest_disabled = true;
77 		pr_info("Ftrace startup test is disabled due to %s\n", reason);
78 	}
79 }
80 #else
81 #define tracing_selftest_running	0
82 #define tracing_selftest_disabled	0
83 #endif
84 
85 /* Pipe tracepoints to printk */
86 static struct trace_iterator *tracepoint_print_iter;
87 int tracepoint_printk;
88 static bool tracepoint_printk_stop_on_boot __initdata;
89 static DEFINE_STATIC_KEY_FALSE(tracepoint_printk_key);
90 
91 /* For tracers that don't implement custom flags */
92 static struct tracer_opt dummy_tracer_opt[] = {
93 	{ }
94 };
95 
96 static int
97 dummy_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
98 {
99 	return 0;
100 }
101 
102 /*
103  * To prevent the comm cache from being overwritten when no
104  * tracing is active, only save the comm when a trace event
105  * occurred.
106  */
107 DEFINE_PER_CPU(bool, trace_taskinfo_save);
108 
109 /*
110  * Kill all tracing for good (never come back).
111  * It is initialized to 1 but will turn to zero if the initialization
112  * of the tracer is successful. But that is the only place that sets
113  * this back to zero.
114  */
115 static int tracing_disabled = 1;
116 
117 cpumask_var_t __read_mostly	tracing_buffer_mask;
118 
119 /*
120  * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
121  *
122  * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
123  * is set, then ftrace_dump is called. This will output the contents
124  * of the ftrace buffers to the console.  This is very useful for
125  * capturing traces that lead to crashes and outputing it to a
126  * serial console.
127  *
128  * It is default off, but you can enable it with either specifying
129  * "ftrace_dump_on_oops" in the kernel command line, or setting
130  * /proc/sys/kernel/ftrace_dump_on_oops
131  * Set 1 if you want to dump buffers of all CPUs
132  * Set 2 if you want to dump the buffer of the CPU that triggered oops
133  * Set instance name if you want to dump the specific trace instance
134  * Multiple instance dump is also supported, and instances are seperated
135  * by commas.
136  */
137 /* Set to string format zero to disable by default */
138 char ftrace_dump_on_oops[MAX_TRACER_SIZE] = "0";
139 
140 /* When set, tracing will stop when a WARN*() is hit */
141 int __disable_trace_on_warning;
142 
143 #ifdef CONFIG_TRACE_EVAL_MAP_FILE
144 /* Map of enums to their values, for "eval_map" file */
145 struct trace_eval_map_head {
146 	struct module			*mod;
147 	unsigned long			length;
148 };
149 
150 union trace_eval_map_item;
151 
152 struct trace_eval_map_tail {
153 	/*
154 	 * "end" is first and points to NULL as it must be different
155 	 * than "mod" or "eval_string"
156 	 */
157 	union trace_eval_map_item	*next;
158 	const char			*end;	/* points to NULL */
159 };
160 
161 static DEFINE_MUTEX(trace_eval_mutex);
162 
163 /*
164  * The trace_eval_maps are saved in an array with two extra elements,
165  * one at the beginning, and one at the end. The beginning item contains
166  * the count of the saved maps (head.length), and the module they
167  * belong to if not built in (head.mod). The ending item contains a
168  * pointer to the next array of saved eval_map items.
169  */
170 union trace_eval_map_item {
171 	struct trace_eval_map		map;
172 	struct trace_eval_map_head	head;
173 	struct trace_eval_map_tail	tail;
174 };
175 
176 static union trace_eval_map_item *trace_eval_maps;
177 #endif /* CONFIG_TRACE_EVAL_MAP_FILE */
178 
179 int tracing_set_tracer(struct trace_array *tr, const char *buf);
180 static void ftrace_trace_userstack(struct trace_array *tr,
181 				   struct trace_buffer *buffer,
182 				   unsigned int trace_ctx);
183 
184 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
185 static char *default_bootup_tracer;
186 
187 static bool allocate_snapshot;
188 static bool snapshot_at_boot;
189 
190 static char boot_instance_info[COMMAND_LINE_SIZE] __initdata;
191 static int boot_instance_index;
192 
193 static char boot_snapshot_info[COMMAND_LINE_SIZE] __initdata;
194 static int boot_snapshot_index;
195 
196 static int __init set_cmdline_ftrace(char *str)
197 {
198 	strscpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
199 	default_bootup_tracer = bootup_tracer_buf;
200 	/* We are using ftrace early, expand it */
201 	trace_set_ring_buffer_expanded(NULL);
202 	return 1;
203 }
204 __setup("ftrace=", set_cmdline_ftrace);
205 
206 int ftrace_dump_on_oops_enabled(void)
207 {
208 	if (!strcmp("0", ftrace_dump_on_oops))
209 		return 0;
210 	else
211 		return 1;
212 }
213 
214 static int __init set_ftrace_dump_on_oops(char *str)
215 {
216 	if (!*str) {
217 		strscpy(ftrace_dump_on_oops, "1", MAX_TRACER_SIZE);
218 		return 1;
219 	}
220 
221 	if (*str == ',') {
222 		strscpy(ftrace_dump_on_oops, "1", MAX_TRACER_SIZE);
223 		strscpy(ftrace_dump_on_oops + 1, str, MAX_TRACER_SIZE - 1);
224 		return 1;
225 	}
226 
227 	if (*str++ == '=') {
228 		strscpy(ftrace_dump_on_oops, str, MAX_TRACER_SIZE);
229 		return 1;
230 	}
231 
232 	return 0;
233 }
234 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
235 
236 static int __init stop_trace_on_warning(char *str)
237 {
238 	if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
239 		__disable_trace_on_warning = 1;
240 	return 1;
241 }
242 __setup("traceoff_on_warning", stop_trace_on_warning);
243 
244 static int __init boot_alloc_snapshot(char *str)
245 {
246 	char *slot = boot_snapshot_info + boot_snapshot_index;
247 	int left = sizeof(boot_snapshot_info) - boot_snapshot_index;
248 	int ret;
249 
250 	if (str[0] == '=') {
251 		str++;
252 		if (strlen(str) >= left)
253 			return -1;
254 
255 		ret = snprintf(slot, left, "%s\t", str);
256 		boot_snapshot_index += ret;
257 	} else {
258 		allocate_snapshot = true;
259 		/* We also need the main ring buffer expanded */
260 		trace_set_ring_buffer_expanded(NULL);
261 	}
262 	return 1;
263 }
264 __setup("alloc_snapshot", boot_alloc_snapshot);
265 
266 
267 static int __init boot_snapshot(char *str)
268 {
269 	snapshot_at_boot = true;
270 	boot_alloc_snapshot(str);
271 	return 1;
272 }
273 __setup("ftrace_boot_snapshot", boot_snapshot);
274 
275 
276 static int __init boot_instance(char *str)
277 {
278 	char *slot = boot_instance_info + boot_instance_index;
279 	int left = sizeof(boot_instance_info) - boot_instance_index;
280 	int ret;
281 
282 	if (strlen(str) >= left)
283 		return -1;
284 
285 	ret = snprintf(slot, left, "%s\t", str);
286 	boot_instance_index += ret;
287 
288 	return 1;
289 }
290 __setup("trace_instance=", boot_instance);
291 
292 
293 static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
294 
295 static int __init set_trace_boot_options(char *str)
296 {
297 	strscpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
298 	return 1;
299 }
300 __setup("trace_options=", set_trace_boot_options);
301 
302 static char trace_boot_clock_buf[MAX_TRACER_SIZE] __initdata;
303 static char *trace_boot_clock __initdata;
304 
305 static int __init set_trace_boot_clock(char *str)
306 {
307 	strscpy(trace_boot_clock_buf, str, MAX_TRACER_SIZE);
308 	trace_boot_clock = trace_boot_clock_buf;
309 	return 1;
310 }
311 __setup("trace_clock=", set_trace_boot_clock);
312 
313 static int __init set_tracepoint_printk(char *str)
314 {
315 	/* Ignore the "tp_printk_stop_on_boot" param */
316 	if (*str == '_')
317 		return 0;
318 
319 	if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
320 		tracepoint_printk = 1;
321 	return 1;
322 }
323 __setup("tp_printk", set_tracepoint_printk);
324 
325 static int __init set_tracepoint_printk_stop(char *str)
326 {
327 	tracepoint_printk_stop_on_boot = true;
328 	return 1;
329 }
330 __setup("tp_printk_stop_on_boot", set_tracepoint_printk_stop);
331 
332 unsigned long long ns2usecs(u64 nsec)
333 {
334 	nsec += 500;
335 	do_div(nsec, 1000);
336 	return nsec;
337 }
338 
339 static void
340 trace_process_export(struct trace_export *export,
341 	       struct ring_buffer_event *event, int flag)
342 {
343 	struct trace_entry *entry;
344 	unsigned int size = 0;
345 
346 	if (export->flags & flag) {
347 		entry = ring_buffer_event_data(event);
348 		size = ring_buffer_event_length(event);
349 		export->write(export, entry, size);
350 	}
351 }
352 
353 static DEFINE_MUTEX(ftrace_export_lock);
354 
355 static struct trace_export __rcu *ftrace_exports_list __read_mostly;
356 
357 static DEFINE_STATIC_KEY_FALSE(trace_function_exports_enabled);
358 static DEFINE_STATIC_KEY_FALSE(trace_event_exports_enabled);
359 static DEFINE_STATIC_KEY_FALSE(trace_marker_exports_enabled);
360 
361 static inline void ftrace_exports_enable(struct trace_export *export)
362 {
363 	if (export->flags & TRACE_EXPORT_FUNCTION)
364 		static_branch_inc(&trace_function_exports_enabled);
365 
366 	if (export->flags & TRACE_EXPORT_EVENT)
367 		static_branch_inc(&trace_event_exports_enabled);
368 
369 	if (export->flags & TRACE_EXPORT_MARKER)
370 		static_branch_inc(&trace_marker_exports_enabled);
371 }
372 
373 static inline void ftrace_exports_disable(struct trace_export *export)
374 {
375 	if (export->flags & TRACE_EXPORT_FUNCTION)
376 		static_branch_dec(&trace_function_exports_enabled);
377 
378 	if (export->flags & TRACE_EXPORT_EVENT)
379 		static_branch_dec(&trace_event_exports_enabled);
380 
381 	if (export->flags & TRACE_EXPORT_MARKER)
382 		static_branch_dec(&trace_marker_exports_enabled);
383 }
384 
385 static void ftrace_exports(struct ring_buffer_event *event, int flag)
386 {
387 	struct trace_export *export;
388 
389 	preempt_disable_notrace();
390 
391 	export = rcu_dereference_raw_check(ftrace_exports_list);
392 	while (export) {
393 		trace_process_export(export, event, flag);
394 		export = rcu_dereference_raw_check(export->next);
395 	}
396 
397 	preempt_enable_notrace();
398 }
399 
400 static inline void
401 add_trace_export(struct trace_export **list, struct trace_export *export)
402 {
403 	rcu_assign_pointer(export->next, *list);
404 	/*
405 	 * We are entering export into the list but another
406 	 * CPU might be walking that list. We need to make sure
407 	 * the export->next pointer is valid before another CPU sees
408 	 * the export pointer included into the list.
409 	 */
410 	rcu_assign_pointer(*list, export);
411 }
412 
413 static inline int
414 rm_trace_export(struct trace_export **list, struct trace_export *export)
415 {
416 	struct trace_export **p;
417 
418 	for (p = list; *p != NULL; p = &(*p)->next)
419 		if (*p == export)
420 			break;
421 
422 	if (*p != export)
423 		return -1;
424 
425 	rcu_assign_pointer(*p, (*p)->next);
426 
427 	return 0;
428 }
429 
430 static inline void
431 add_ftrace_export(struct trace_export **list, struct trace_export *export)
432 {
433 	ftrace_exports_enable(export);
434 
435 	add_trace_export(list, export);
436 }
437 
438 static inline int
439 rm_ftrace_export(struct trace_export **list, struct trace_export *export)
440 {
441 	int ret;
442 
443 	ret = rm_trace_export(list, export);
444 	ftrace_exports_disable(export);
445 
446 	return ret;
447 }
448 
449 int register_ftrace_export(struct trace_export *export)
450 {
451 	if (WARN_ON_ONCE(!export->write))
452 		return -1;
453 
454 	mutex_lock(&ftrace_export_lock);
455 
456 	add_ftrace_export(&ftrace_exports_list, export);
457 
458 	mutex_unlock(&ftrace_export_lock);
459 
460 	return 0;
461 }
462 EXPORT_SYMBOL_GPL(register_ftrace_export);
463 
464 int unregister_ftrace_export(struct trace_export *export)
465 {
466 	int ret;
467 
468 	mutex_lock(&ftrace_export_lock);
469 
470 	ret = rm_ftrace_export(&ftrace_exports_list, export);
471 
472 	mutex_unlock(&ftrace_export_lock);
473 
474 	return ret;
475 }
476 EXPORT_SYMBOL_GPL(unregister_ftrace_export);
477 
478 /* trace_flags holds trace_options default values */
479 #define TRACE_DEFAULT_FLAGS						\
480 	(FUNCTION_DEFAULT_FLAGS |					\
481 	 TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |			\
482 	 TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO |		\
483 	 TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |			\
484 	 TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS |			\
485 	 TRACE_ITER_HASH_PTR | TRACE_ITER_TRACE_PRINTK)
486 
487 /* trace_options that are only supported by global_trace */
488 #define TOP_LEVEL_TRACE_FLAGS (TRACE_ITER_PRINTK |			\
489 	       TRACE_ITER_PRINTK_MSGONLY | TRACE_ITER_RECORD_CMD)
490 
491 /* trace_flags that are default zero for instances */
492 #define ZEROED_TRACE_FLAGS \
493 	(TRACE_ITER_EVENT_FORK | TRACE_ITER_FUNC_FORK | TRACE_ITER_TRACE_PRINTK)
494 
495 /*
496  * The global_trace is the descriptor that holds the top-level tracing
497  * buffers for the live tracing.
498  */
499 static struct trace_array global_trace = {
500 	.trace_flags = TRACE_DEFAULT_FLAGS,
501 };
502 
503 static struct trace_array *printk_trace = &global_trace;
504 
505 static __always_inline bool printk_binsafe(struct trace_array *tr)
506 {
507 	/*
508 	 * The binary format of traceprintk can cause a crash if used
509 	 * by a buffer from another boot. Force the use of the
510 	 * non binary version of trace_printk if the trace_printk
511 	 * buffer is a boot mapped ring buffer.
512 	 */
513 	return !(tr->flags & TRACE_ARRAY_FL_BOOT);
514 }
515 
516 static void update_printk_trace(struct trace_array *tr)
517 {
518 	if (printk_trace == tr)
519 		return;
520 
521 	printk_trace->trace_flags &= ~TRACE_ITER_TRACE_PRINTK;
522 	printk_trace = tr;
523 	tr->trace_flags |= TRACE_ITER_TRACE_PRINTK;
524 }
525 
526 void trace_set_ring_buffer_expanded(struct trace_array *tr)
527 {
528 	if (!tr)
529 		tr = &global_trace;
530 	tr->ring_buffer_expanded = true;
531 }
532 
533 LIST_HEAD(ftrace_trace_arrays);
534 
535 int trace_array_get(struct trace_array *this_tr)
536 {
537 	struct trace_array *tr;
538 	int ret = -ENODEV;
539 
540 	mutex_lock(&trace_types_lock);
541 	list_for_each_entry(tr, &ftrace_trace_arrays, list) {
542 		if (tr == this_tr) {
543 			tr->ref++;
544 			ret = 0;
545 			break;
546 		}
547 	}
548 	mutex_unlock(&trace_types_lock);
549 
550 	return ret;
551 }
552 
553 static void __trace_array_put(struct trace_array *this_tr)
554 {
555 	WARN_ON(!this_tr->ref);
556 	this_tr->ref--;
557 }
558 
559 /**
560  * trace_array_put - Decrement the reference counter for this trace array.
561  * @this_tr : pointer to the trace array
562  *
563  * NOTE: Use this when we no longer need the trace array returned by
564  * trace_array_get_by_name(). This ensures the trace array can be later
565  * destroyed.
566  *
567  */
568 void trace_array_put(struct trace_array *this_tr)
569 {
570 	if (!this_tr)
571 		return;
572 
573 	mutex_lock(&trace_types_lock);
574 	__trace_array_put(this_tr);
575 	mutex_unlock(&trace_types_lock);
576 }
577 EXPORT_SYMBOL_GPL(trace_array_put);
578 
579 int tracing_check_open_get_tr(struct trace_array *tr)
580 {
581 	int ret;
582 
583 	ret = security_locked_down(LOCKDOWN_TRACEFS);
584 	if (ret)
585 		return ret;
586 
587 	if (tracing_disabled)
588 		return -ENODEV;
589 
590 	if (tr && trace_array_get(tr) < 0)
591 		return -ENODEV;
592 
593 	return 0;
594 }
595 
596 /**
597  * trace_find_filtered_pid - check if a pid exists in a filtered_pid list
598  * @filtered_pids: The list of pids to check
599  * @search_pid: The PID to find in @filtered_pids
600  *
601  * Returns true if @search_pid is found in @filtered_pids, and false otherwise.
602  */
603 bool
604 trace_find_filtered_pid(struct trace_pid_list *filtered_pids, pid_t search_pid)
605 {
606 	return trace_pid_list_is_set(filtered_pids, search_pid);
607 }
608 
609 /**
610  * trace_ignore_this_task - should a task be ignored for tracing
611  * @filtered_pids: The list of pids to check
612  * @filtered_no_pids: The list of pids not to be traced
613  * @task: The task that should be ignored if not filtered
614  *
615  * Checks if @task should be traced or not from @filtered_pids.
616  * Returns true if @task should *NOT* be traced.
617  * Returns false if @task should be traced.
618  */
619 bool
620 trace_ignore_this_task(struct trace_pid_list *filtered_pids,
621 		       struct trace_pid_list *filtered_no_pids,
622 		       struct task_struct *task)
623 {
624 	/*
625 	 * If filtered_no_pids is not empty, and the task's pid is listed
626 	 * in filtered_no_pids, then return true.
627 	 * Otherwise, if filtered_pids is empty, that means we can
628 	 * trace all tasks. If it has content, then only trace pids
629 	 * within filtered_pids.
630 	 */
631 
632 	return (filtered_pids &&
633 		!trace_find_filtered_pid(filtered_pids, task->pid)) ||
634 		(filtered_no_pids &&
635 		 trace_find_filtered_pid(filtered_no_pids, task->pid));
636 }
637 
638 /**
639  * trace_filter_add_remove_task - Add or remove a task from a pid_list
640  * @pid_list: The list to modify
641  * @self: The current task for fork or NULL for exit
642  * @task: The task to add or remove
643  *
644  * If adding a task, if @self is defined, the task is only added if @self
645  * is also included in @pid_list. This happens on fork and tasks should
646  * only be added when the parent is listed. If @self is NULL, then the
647  * @task pid will be removed from the list, which would happen on exit
648  * of a task.
649  */
650 void trace_filter_add_remove_task(struct trace_pid_list *pid_list,
651 				  struct task_struct *self,
652 				  struct task_struct *task)
653 {
654 	if (!pid_list)
655 		return;
656 
657 	/* For forks, we only add if the forking task is listed */
658 	if (self) {
659 		if (!trace_find_filtered_pid(pid_list, self->pid))
660 			return;
661 	}
662 
663 	/* "self" is set for forks, and NULL for exits */
664 	if (self)
665 		trace_pid_list_set(pid_list, task->pid);
666 	else
667 		trace_pid_list_clear(pid_list, task->pid);
668 }
669 
670 /**
671  * trace_pid_next - Used for seq_file to get to the next pid of a pid_list
672  * @pid_list: The pid list to show
673  * @v: The last pid that was shown (+1 the actual pid to let zero be displayed)
674  * @pos: The position of the file
675  *
676  * This is used by the seq_file "next" operation to iterate the pids
677  * listed in a trace_pid_list structure.
678  *
679  * Returns the pid+1 as we want to display pid of zero, but NULL would
680  * stop the iteration.
681  */
682 void *trace_pid_next(struct trace_pid_list *pid_list, void *v, loff_t *pos)
683 {
684 	long pid = (unsigned long)v;
685 	unsigned int next;
686 
687 	(*pos)++;
688 
689 	/* pid already is +1 of the actual previous bit */
690 	if (trace_pid_list_next(pid_list, pid, &next) < 0)
691 		return NULL;
692 
693 	pid = next;
694 
695 	/* Return pid + 1 to allow zero to be represented */
696 	return (void *)(pid + 1);
697 }
698 
699 /**
700  * trace_pid_start - Used for seq_file to start reading pid lists
701  * @pid_list: The pid list to show
702  * @pos: The position of the file
703  *
704  * This is used by seq_file "start" operation to start the iteration
705  * of listing pids.
706  *
707  * Returns the pid+1 as we want to display pid of zero, but NULL would
708  * stop the iteration.
709  */
710 void *trace_pid_start(struct trace_pid_list *pid_list, loff_t *pos)
711 {
712 	unsigned long pid;
713 	unsigned int first;
714 	loff_t l = 0;
715 
716 	if (trace_pid_list_first(pid_list, &first) < 0)
717 		return NULL;
718 
719 	pid = first;
720 
721 	/* Return pid + 1 so that zero can be the exit value */
722 	for (pid++; pid && l < *pos;
723 	     pid = (unsigned long)trace_pid_next(pid_list, (void *)pid, &l))
724 		;
725 	return (void *)pid;
726 }
727 
728 /**
729  * trace_pid_show - show the current pid in seq_file processing
730  * @m: The seq_file structure to write into
731  * @v: A void pointer of the pid (+1) value to display
732  *
733  * Can be directly used by seq_file operations to display the current
734  * pid value.
735  */
736 int trace_pid_show(struct seq_file *m, void *v)
737 {
738 	unsigned long pid = (unsigned long)v - 1;
739 
740 	seq_printf(m, "%lu\n", pid);
741 	return 0;
742 }
743 
744 /* 128 should be much more than enough */
745 #define PID_BUF_SIZE		127
746 
747 int trace_pid_write(struct trace_pid_list *filtered_pids,
748 		    struct trace_pid_list **new_pid_list,
749 		    const char __user *ubuf, size_t cnt)
750 {
751 	struct trace_pid_list *pid_list;
752 	struct trace_parser parser;
753 	unsigned long val;
754 	int nr_pids = 0;
755 	ssize_t read = 0;
756 	ssize_t ret;
757 	loff_t pos;
758 	pid_t pid;
759 
760 	if (trace_parser_get_init(&parser, PID_BUF_SIZE + 1))
761 		return -ENOMEM;
762 
763 	/*
764 	 * Always recreate a new array. The write is an all or nothing
765 	 * operation. Always create a new array when adding new pids by
766 	 * the user. If the operation fails, then the current list is
767 	 * not modified.
768 	 */
769 	pid_list = trace_pid_list_alloc();
770 	if (!pid_list) {
771 		trace_parser_put(&parser);
772 		return -ENOMEM;
773 	}
774 
775 	if (filtered_pids) {
776 		/* copy the current bits to the new max */
777 		ret = trace_pid_list_first(filtered_pids, &pid);
778 		while (!ret) {
779 			trace_pid_list_set(pid_list, pid);
780 			ret = trace_pid_list_next(filtered_pids, pid + 1, &pid);
781 			nr_pids++;
782 		}
783 	}
784 
785 	ret = 0;
786 	while (cnt > 0) {
787 
788 		pos = 0;
789 
790 		ret = trace_get_user(&parser, ubuf, cnt, &pos);
791 		if (ret < 0)
792 			break;
793 
794 		read += ret;
795 		ubuf += ret;
796 		cnt -= ret;
797 
798 		if (!trace_parser_loaded(&parser))
799 			break;
800 
801 		ret = -EINVAL;
802 		if (kstrtoul(parser.buffer, 0, &val))
803 			break;
804 
805 		pid = (pid_t)val;
806 
807 		if (trace_pid_list_set(pid_list, pid) < 0) {
808 			ret = -1;
809 			break;
810 		}
811 		nr_pids++;
812 
813 		trace_parser_clear(&parser);
814 		ret = 0;
815 	}
816 	trace_parser_put(&parser);
817 
818 	if (ret < 0) {
819 		trace_pid_list_free(pid_list);
820 		return ret;
821 	}
822 
823 	if (!nr_pids) {
824 		/* Cleared the list of pids */
825 		trace_pid_list_free(pid_list);
826 		pid_list = NULL;
827 	}
828 
829 	*new_pid_list = pid_list;
830 
831 	return read;
832 }
833 
834 static u64 buffer_ftrace_now(struct array_buffer *buf, int cpu)
835 {
836 	u64 ts;
837 
838 	/* Early boot up does not have a buffer yet */
839 	if (!buf->buffer)
840 		return trace_clock_local();
841 
842 	ts = ring_buffer_time_stamp(buf->buffer);
843 	ring_buffer_normalize_time_stamp(buf->buffer, cpu, &ts);
844 
845 	return ts;
846 }
847 
848 u64 ftrace_now(int cpu)
849 {
850 	return buffer_ftrace_now(&global_trace.array_buffer, cpu);
851 }
852 
853 /**
854  * tracing_is_enabled - Show if global_trace has been enabled
855  *
856  * Shows if the global trace has been enabled or not. It uses the
857  * mirror flag "buffer_disabled" to be used in fast paths such as for
858  * the irqsoff tracer. But it may be inaccurate due to races. If you
859  * need to know the accurate state, use tracing_is_on() which is a little
860  * slower, but accurate.
861  */
862 int tracing_is_enabled(void)
863 {
864 	/*
865 	 * For quick access (irqsoff uses this in fast path), just
866 	 * return the mirror variable of the state of the ring buffer.
867 	 * It's a little racy, but we don't really care.
868 	 */
869 	smp_rmb();
870 	return !global_trace.buffer_disabled;
871 }
872 
873 /*
874  * trace_buf_size is the size in bytes that is allocated
875  * for a buffer. Note, the number of bytes is always rounded
876  * to page size.
877  *
878  * This number is purposely set to a low number of 16384.
879  * If the dump on oops happens, it will be much appreciated
880  * to not have to wait for all that output. Anyway this can be
881  * boot time and run time configurable.
882  */
883 #define TRACE_BUF_SIZE_DEFAULT	1441792UL /* 16384 * 88 (sizeof(entry)) */
884 
885 static unsigned long		trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
886 
887 /* trace_types holds a link list of available tracers. */
888 static struct tracer		*trace_types __read_mostly;
889 
890 /*
891  * trace_types_lock is used to protect the trace_types list.
892  */
893 DEFINE_MUTEX(trace_types_lock);
894 
895 /*
896  * serialize the access of the ring buffer
897  *
898  * ring buffer serializes readers, but it is low level protection.
899  * The validity of the events (which returns by ring_buffer_peek() ..etc)
900  * are not protected by ring buffer.
901  *
902  * The content of events may become garbage if we allow other process consumes
903  * these events concurrently:
904  *   A) the page of the consumed events may become a normal page
905  *      (not reader page) in ring buffer, and this page will be rewritten
906  *      by events producer.
907  *   B) The page of the consumed events may become a page for splice_read,
908  *      and this page will be returned to system.
909  *
910  * These primitives allow multi process access to different cpu ring buffer
911  * concurrently.
912  *
913  * These primitives don't distinguish read-only and read-consume access.
914  * Multi read-only access are also serialized.
915  */
916 
917 #ifdef CONFIG_SMP
918 static DECLARE_RWSEM(all_cpu_access_lock);
919 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
920 
921 static inline void trace_access_lock(int cpu)
922 {
923 	if (cpu == RING_BUFFER_ALL_CPUS) {
924 		/* gain it for accessing the whole ring buffer. */
925 		down_write(&all_cpu_access_lock);
926 	} else {
927 		/* gain it for accessing a cpu ring buffer. */
928 
929 		/* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
930 		down_read(&all_cpu_access_lock);
931 
932 		/* Secondly block other access to this @cpu ring buffer. */
933 		mutex_lock(&per_cpu(cpu_access_lock, cpu));
934 	}
935 }
936 
937 static inline void trace_access_unlock(int cpu)
938 {
939 	if (cpu == RING_BUFFER_ALL_CPUS) {
940 		up_write(&all_cpu_access_lock);
941 	} else {
942 		mutex_unlock(&per_cpu(cpu_access_lock, cpu));
943 		up_read(&all_cpu_access_lock);
944 	}
945 }
946 
947 static inline void trace_access_lock_init(void)
948 {
949 	int cpu;
950 
951 	for_each_possible_cpu(cpu)
952 		mutex_init(&per_cpu(cpu_access_lock, cpu));
953 }
954 
955 #else
956 
957 static DEFINE_MUTEX(access_lock);
958 
959 static inline void trace_access_lock(int cpu)
960 {
961 	(void)cpu;
962 	mutex_lock(&access_lock);
963 }
964 
965 static inline void trace_access_unlock(int cpu)
966 {
967 	(void)cpu;
968 	mutex_unlock(&access_lock);
969 }
970 
971 static inline void trace_access_lock_init(void)
972 {
973 }
974 
975 #endif
976 
977 #ifdef CONFIG_STACKTRACE
978 static void __ftrace_trace_stack(struct trace_array *tr,
979 				 struct trace_buffer *buffer,
980 				 unsigned int trace_ctx,
981 				 int skip, struct pt_regs *regs);
982 static inline void ftrace_trace_stack(struct trace_array *tr,
983 				      struct trace_buffer *buffer,
984 				      unsigned int trace_ctx,
985 				      int skip, struct pt_regs *regs);
986 
987 #else
988 static inline void __ftrace_trace_stack(struct trace_array *tr,
989 					struct trace_buffer *buffer,
990 					unsigned int trace_ctx,
991 					int skip, struct pt_regs *regs)
992 {
993 }
994 static inline void ftrace_trace_stack(struct trace_array *tr,
995 				      struct trace_buffer *buffer,
996 				      unsigned long trace_ctx,
997 				      int skip, struct pt_regs *regs)
998 {
999 }
1000 
1001 #endif
1002 
1003 static __always_inline void
1004 trace_event_setup(struct ring_buffer_event *event,
1005 		  int type, unsigned int trace_ctx)
1006 {
1007 	struct trace_entry *ent = ring_buffer_event_data(event);
1008 
1009 	tracing_generic_entry_update(ent, type, trace_ctx);
1010 }
1011 
1012 static __always_inline struct ring_buffer_event *
1013 __trace_buffer_lock_reserve(struct trace_buffer *buffer,
1014 			  int type,
1015 			  unsigned long len,
1016 			  unsigned int trace_ctx)
1017 {
1018 	struct ring_buffer_event *event;
1019 
1020 	event = ring_buffer_lock_reserve(buffer, len);
1021 	if (event != NULL)
1022 		trace_event_setup(event, type, trace_ctx);
1023 
1024 	return event;
1025 }
1026 
1027 void tracer_tracing_on(struct trace_array *tr)
1028 {
1029 	if (tr->array_buffer.buffer)
1030 		ring_buffer_record_on(tr->array_buffer.buffer);
1031 	/*
1032 	 * This flag is looked at when buffers haven't been allocated
1033 	 * yet, or by some tracers (like irqsoff), that just want to
1034 	 * know if the ring buffer has been disabled, but it can handle
1035 	 * races of where it gets disabled but we still do a record.
1036 	 * As the check is in the fast path of the tracers, it is more
1037 	 * important to be fast than accurate.
1038 	 */
1039 	tr->buffer_disabled = 0;
1040 	/* Make the flag seen by readers */
1041 	smp_wmb();
1042 }
1043 
1044 /**
1045  * tracing_on - enable tracing buffers
1046  *
1047  * This function enables tracing buffers that may have been
1048  * disabled with tracing_off.
1049  */
1050 void tracing_on(void)
1051 {
1052 	tracer_tracing_on(&global_trace);
1053 }
1054 EXPORT_SYMBOL_GPL(tracing_on);
1055 
1056 
1057 static __always_inline void
1058 __buffer_unlock_commit(struct trace_buffer *buffer, struct ring_buffer_event *event)
1059 {
1060 	__this_cpu_write(trace_taskinfo_save, true);
1061 
1062 	/* If this is the temp buffer, we need to commit fully */
1063 	if (this_cpu_read(trace_buffered_event) == event) {
1064 		/* Length is in event->array[0] */
1065 		ring_buffer_write(buffer, event->array[0], &event->array[1]);
1066 		/* Release the temp buffer */
1067 		this_cpu_dec(trace_buffered_event_cnt);
1068 		/* ring_buffer_unlock_commit() enables preemption */
1069 		preempt_enable_notrace();
1070 	} else
1071 		ring_buffer_unlock_commit(buffer);
1072 }
1073 
1074 int __trace_array_puts(struct trace_array *tr, unsigned long ip,
1075 		       const char *str, int size)
1076 {
1077 	struct ring_buffer_event *event;
1078 	struct trace_buffer *buffer;
1079 	struct print_entry *entry;
1080 	unsigned int trace_ctx;
1081 	int alloc;
1082 
1083 	if (!(tr->trace_flags & TRACE_ITER_PRINTK))
1084 		return 0;
1085 
1086 	if (unlikely(tracing_selftest_running && tr == &global_trace))
1087 		return 0;
1088 
1089 	if (unlikely(tracing_disabled))
1090 		return 0;
1091 
1092 	alloc = sizeof(*entry) + size + 2; /* possible \n added */
1093 
1094 	trace_ctx = tracing_gen_ctx();
1095 	buffer = tr->array_buffer.buffer;
1096 	ring_buffer_nest_start(buffer);
1097 	event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc,
1098 					    trace_ctx);
1099 	if (!event) {
1100 		size = 0;
1101 		goto out;
1102 	}
1103 
1104 	entry = ring_buffer_event_data(event);
1105 	entry->ip = ip;
1106 
1107 	memcpy(&entry->buf, str, size);
1108 
1109 	/* Add a newline if necessary */
1110 	if (entry->buf[size - 1] != '\n') {
1111 		entry->buf[size] = '\n';
1112 		entry->buf[size + 1] = '\0';
1113 	} else
1114 		entry->buf[size] = '\0';
1115 
1116 	__buffer_unlock_commit(buffer, event);
1117 	ftrace_trace_stack(tr, buffer, trace_ctx, 4, NULL);
1118  out:
1119 	ring_buffer_nest_end(buffer);
1120 	return size;
1121 }
1122 EXPORT_SYMBOL_GPL(__trace_array_puts);
1123 
1124 /**
1125  * __trace_puts - write a constant string into the trace buffer.
1126  * @ip:	   The address of the caller
1127  * @str:   The constant string to write
1128  * @size:  The size of the string.
1129  */
1130 int __trace_puts(unsigned long ip, const char *str, int size)
1131 {
1132 	return __trace_array_puts(printk_trace, ip, str, size);
1133 }
1134 EXPORT_SYMBOL_GPL(__trace_puts);
1135 
1136 /**
1137  * __trace_bputs - write the pointer to a constant string into trace buffer
1138  * @ip:	   The address of the caller
1139  * @str:   The constant string to write to the buffer to
1140  */
1141 int __trace_bputs(unsigned long ip, const char *str)
1142 {
1143 	struct trace_array *tr = READ_ONCE(printk_trace);
1144 	struct ring_buffer_event *event;
1145 	struct trace_buffer *buffer;
1146 	struct bputs_entry *entry;
1147 	unsigned int trace_ctx;
1148 	int size = sizeof(struct bputs_entry);
1149 	int ret = 0;
1150 
1151 	if (!printk_binsafe(tr))
1152 		return __trace_puts(ip, str, strlen(str));
1153 
1154 	if (!(tr->trace_flags & TRACE_ITER_PRINTK))
1155 		return 0;
1156 
1157 	if (unlikely(tracing_selftest_running || tracing_disabled))
1158 		return 0;
1159 
1160 	trace_ctx = tracing_gen_ctx();
1161 	buffer = tr->array_buffer.buffer;
1162 
1163 	ring_buffer_nest_start(buffer);
1164 	event = __trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
1165 					    trace_ctx);
1166 	if (!event)
1167 		goto out;
1168 
1169 	entry = ring_buffer_event_data(event);
1170 	entry->ip			= ip;
1171 	entry->str			= str;
1172 
1173 	__buffer_unlock_commit(buffer, event);
1174 	ftrace_trace_stack(tr, buffer, trace_ctx, 4, NULL);
1175 
1176 	ret = 1;
1177  out:
1178 	ring_buffer_nest_end(buffer);
1179 	return ret;
1180 }
1181 EXPORT_SYMBOL_GPL(__trace_bputs);
1182 
1183 #ifdef CONFIG_TRACER_SNAPSHOT
1184 static void tracing_snapshot_instance_cond(struct trace_array *tr,
1185 					   void *cond_data)
1186 {
1187 	struct tracer *tracer = tr->current_trace;
1188 	unsigned long flags;
1189 
1190 	if (in_nmi()) {
1191 		trace_array_puts(tr, "*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
1192 		trace_array_puts(tr, "*** snapshot is being ignored        ***\n");
1193 		return;
1194 	}
1195 
1196 	if (!tr->allocated_snapshot) {
1197 		trace_array_puts(tr, "*** SNAPSHOT NOT ALLOCATED ***\n");
1198 		trace_array_puts(tr, "*** stopping trace here!   ***\n");
1199 		tracer_tracing_off(tr);
1200 		return;
1201 	}
1202 
1203 	/* Note, snapshot can not be used when the tracer uses it */
1204 	if (tracer->use_max_tr) {
1205 		trace_array_puts(tr, "*** LATENCY TRACER ACTIVE ***\n");
1206 		trace_array_puts(tr, "*** Can not use snapshot (sorry) ***\n");
1207 		return;
1208 	}
1209 
1210 	if (tr->mapped) {
1211 		trace_array_puts(tr, "*** BUFFER MEMORY MAPPED ***\n");
1212 		trace_array_puts(tr, "*** Can not use snapshot (sorry) ***\n");
1213 		return;
1214 	}
1215 
1216 	local_irq_save(flags);
1217 	update_max_tr(tr, current, smp_processor_id(), cond_data);
1218 	local_irq_restore(flags);
1219 }
1220 
1221 void tracing_snapshot_instance(struct trace_array *tr)
1222 {
1223 	tracing_snapshot_instance_cond(tr, NULL);
1224 }
1225 
1226 /**
1227  * tracing_snapshot - take a snapshot of the current buffer.
1228  *
1229  * This causes a swap between the snapshot buffer and the current live
1230  * tracing buffer. You can use this to take snapshots of the live
1231  * trace when some condition is triggered, but continue to trace.
1232  *
1233  * Note, make sure to allocate the snapshot with either
1234  * a tracing_snapshot_alloc(), or by doing it manually
1235  * with: echo 1 > /sys/kernel/tracing/snapshot
1236  *
1237  * If the snapshot buffer is not allocated, it will stop tracing.
1238  * Basically making a permanent snapshot.
1239  */
1240 void tracing_snapshot(void)
1241 {
1242 	struct trace_array *tr = &global_trace;
1243 
1244 	tracing_snapshot_instance(tr);
1245 }
1246 EXPORT_SYMBOL_GPL(tracing_snapshot);
1247 
1248 /**
1249  * tracing_snapshot_cond - conditionally take a snapshot of the current buffer.
1250  * @tr:		The tracing instance to snapshot
1251  * @cond_data:	The data to be tested conditionally, and possibly saved
1252  *
1253  * This is the same as tracing_snapshot() except that the snapshot is
1254  * conditional - the snapshot will only happen if the
1255  * cond_snapshot.update() implementation receiving the cond_data
1256  * returns true, which means that the trace array's cond_snapshot
1257  * update() operation used the cond_data to determine whether the
1258  * snapshot should be taken, and if it was, presumably saved it along
1259  * with the snapshot.
1260  */
1261 void tracing_snapshot_cond(struct trace_array *tr, void *cond_data)
1262 {
1263 	tracing_snapshot_instance_cond(tr, cond_data);
1264 }
1265 EXPORT_SYMBOL_GPL(tracing_snapshot_cond);
1266 
1267 /**
1268  * tracing_cond_snapshot_data - get the user data associated with a snapshot
1269  * @tr:		The tracing instance
1270  *
1271  * When the user enables a conditional snapshot using
1272  * tracing_snapshot_cond_enable(), the user-defined cond_data is saved
1273  * with the snapshot.  This accessor is used to retrieve it.
1274  *
1275  * Should not be called from cond_snapshot.update(), since it takes
1276  * the tr->max_lock lock, which the code calling
1277  * cond_snapshot.update() has already done.
1278  *
1279  * Returns the cond_data associated with the trace array's snapshot.
1280  */
1281 void *tracing_cond_snapshot_data(struct trace_array *tr)
1282 {
1283 	void *cond_data = NULL;
1284 
1285 	local_irq_disable();
1286 	arch_spin_lock(&tr->max_lock);
1287 
1288 	if (tr->cond_snapshot)
1289 		cond_data = tr->cond_snapshot->cond_data;
1290 
1291 	arch_spin_unlock(&tr->max_lock);
1292 	local_irq_enable();
1293 
1294 	return cond_data;
1295 }
1296 EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data);
1297 
1298 static int resize_buffer_duplicate_size(struct array_buffer *trace_buf,
1299 					struct array_buffer *size_buf, int cpu_id);
1300 static void set_buffer_entries(struct array_buffer *buf, unsigned long val);
1301 
1302 int tracing_alloc_snapshot_instance(struct trace_array *tr)
1303 {
1304 	int order;
1305 	int ret;
1306 
1307 	if (!tr->allocated_snapshot) {
1308 
1309 		/* Make the snapshot buffer have the same order as main buffer */
1310 		order = ring_buffer_subbuf_order_get(tr->array_buffer.buffer);
1311 		ret = ring_buffer_subbuf_order_set(tr->max_buffer.buffer, order);
1312 		if (ret < 0)
1313 			return ret;
1314 
1315 		/* allocate spare buffer */
1316 		ret = resize_buffer_duplicate_size(&tr->max_buffer,
1317 				   &tr->array_buffer, RING_BUFFER_ALL_CPUS);
1318 		if (ret < 0)
1319 			return ret;
1320 
1321 		tr->allocated_snapshot = true;
1322 	}
1323 
1324 	return 0;
1325 }
1326 
1327 static void free_snapshot(struct trace_array *tr)
1328 {
1329 	/*
1330 	 * We don't free the ring buffer. instead, resize it because
1331 	 * The max_tr ring buffer has some state (e.g. ring->clock) and
1332 	 * we want preserve it.
1333 	 */
1334 	ring_buffer_subbuf_order_set(tr->max_buffer.buffer, 0);
1335 	ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
1336 	set_buffer_entries(&tr->max_buffer, 1);
1337 	tracing_reset_online_cpus(&tr->max_buffer);
1338 	tr->allocated_snapshot = false;
1339 }
1340 
1341 static int tracing_arm_snapshot_locked(struct trace_array *tr)
1342 {
1343 	int ret;
1344 
1345 	lockdep_assert_held(&trace_types_lock);
1346 
1347 	spin_lock(&tr->snapshot_trigger_lock);
1348 	if (tr->snapshot == UINT_MAX || tr->mapped) {
1349 		spin_unlock(&tr->snapshot_trigger_lock);
1350 		return -EBUSY;
1351 	}
1352 
1353 	tr->snapshot++;
1354 	spin_unlock(&tr->snapshot_trigger_lock);
1355 
1356 	ret = tracing_alloc_snapshot_instance(tr);
1357 	if (ret) {
1358 		spin_lock(&tr->snapshot_trigger_lock);
1359 		tr->snapshot--;
1360 		spin_unlock(&tr->snapshot_trigger_lock);
1361 	}
1362 
1363 	return ret;
1364 }
1365 
1366 int tracing_arm_snapshot(struct trace_array *tr)
1367 {
1368 	int ret;
1369 
1370 	mutex_lock(&trace_types_lock);
1371 	ret = tracing_arm_snapshot_locked(tr);
1372 	mutex_unlock(&trace_types_lock);
1373 
1374 	return ret;
1375 }
1376 
1377 void tracing_disarm_snapshot(struct trace_array *tr)
1378 {
1379 	spin_lock(&tr->snapshot_trigger_lock);
1380 	if (!WARN_ON(!tr->snapshot))
1381 		tr->snapshot--;
1382 	spin_unlock(&tr->snapshot_trigger_lock);
1383 }
1384 
1385 /**
1386  * tracing_alloc_snapshot - allocate snapshot buffer.
1387  *
1388  * This only allocates the snapshot buffer if it isn't already
1389  * allocated - it doesn't also take a snapshot.
1390  *
1391  * This is meant to be used in cases where the snapshot buffer needs
1392  * to be set up for events that can't sleep but need to be able to
1393  * trigger a snapshot.
1394  */
1395 int tracing_alloc_snapshot(void)
1396 {
1397 	struct trace_array *tr = &global_trace;
1398 	int ret;
1399 
1400 	ret = tracing_alloc_snapshot_instance(tr);
1401 	WARN_ON(ret < 0);
1402 
1403 	return ret;
1404 }
1405 EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
1406 
1407 /**
1408  * tracing_snapshot_alloc - allocate and take a snapshot of the current buffer.
1409  *
1410  * This is similar to tracing_snapshot(), but it will allocate the
1411  * snapshot buffer if it isn't already allocated. Use this only
1412  * where it is safe to sleep, as the allocation may sleep.
1413  *
1414  * This causes a swap between the snapshot buffer and the current live
1415  * tracing buffer. You can use this to take snapshots of the live
1416  * trace when some condition is triggered, but continue to trace.
1417  */
1418 void tracing_snapshot_alloc(void)
1419 {
1420 	int ret;
1421 
1422 	ret = tracing_alloc_snapshot();
1423 	if (ret < 0)
1424 		return;
1425 
1426 	tracing_snapshot();
1427 }
1428 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
1429 
1430 /**
1431  * tracing_snapshot_cond_enable - enable conditional snapshot for an instance
1432  * @tr:		The tracing instance
1433  * @cond_data:	User data to associate with the snapshot
1434  * @update:	Implementation of the cond_snapshot update function
1435  *
1436  * Check whether the conditional snapshot for the given instance has
1437  * already been enabled, or if the current tracer is already using a
1438  * snapshot; if so, return -EBUSY, else create a cond_snapshot and
1439  * save the cond_data and update function inside.
1440  *
1441  * Returns 0 if successful, error otherwise.
1442  */
1443 int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data,
1444 				 cond_update_fn_t update)
1445 {
1446 	struct cond_snapshot *cond_snapshot;
1447 	int ret = 0;
1448 
1449 	cond_snapshot = kzalloc(sizeof(*cond_snapshot), GFP_KERNEL);
1450 	if (!cond_snapshot)
1451 		return -ENOMEM;
1452 
1453 	cond_snapshot->cond_data = cond_data;
1454 	cond_snapshot->update = update;
1455 
1456 	mutex_lock(&trace_types_lock);
1457 
1458 	if (tr->current_trace->use_max_tr) {
1459 		ret = -EBUSY;
1460 		goto fail_unlock;
1461 	}
1462 
1463 	/*
1464 	 * The cond_snapshot can only change to NULL without the
1465 	 * trace_types_lock. We don't care if we race with it going
1466 	 * to NULL, but we want to make sure that it's not set to
1467 	 * something other than NULL when we get here, which we can
1468 	 * do safely with only holding the trace_types_lock and not
1469 	 * having to take the max_lock.
1470 	 */
1471 	if (tr->cond_snapshot) {
1472 		ret = -EBUSY;
1473 		goto fail_unlock;
1474 	}
1475 
1476 	ret = tracing_arm_snapshot_locked(tr);
1477 	if (ret)
1478 		goto fail_unlock;
1479 
1480 	local_irq_disable();
1481 	arch_spin_lock(&tr->max_lock);
1482 	tr->cond_snapshot = cond_snapshot;
1483 	arch_spin_unlock(&tr->max_lock);
1484 	local_irq_enable();
1485 
1486 	mutex_unlock(&trace_types_lock);
1487 
1488 	return ret;
1489 
1490  fail_unlock:
1491 	mutex_unlock(&trace_types_lock);
1492 	kfree(cond_snapshot);
1493 	return ret;
1494 }
1495 EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable);
1496 
1497 /**
1498  * tracing_snapshot_cond_disable - disable conditional snapshot for an instance
1499  * @tr:		The tracing instance
1500  *
1501  * Check whether the conditional snapshot for the given instance is
1502  * enabled; if so, free the cond_snapshot associated with it,
1503  * otherwise return -EINVAL.
1504  *
1505  * Returns 0 if successful, error otherwise.
1506  */
1507 int tracing_snapshot_cond_disable(struct trace_array *tr)
1508 {
1509 	int ret = 0;
1510 
1511 	local_irq_disable();
1512 	arch_spin_lock(&tr->max_lock);
1513 
1514 	if (!tr->cond_snapshot)
1515 		ret = -EINVAL;
1516 	else {
1517 		kfree(tr->cond_snapshot);
1518 		tr->cond_snapshot = NULL;
1519 	}
1520 
1521 	arch_spin_unlock(&tr->max_lock);
1522 	local_irq_enable();
1523 
1524 	tracing_disarm_snapshot(tr);
1525 
1526 	return ret;
1527 }
1528 EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable);
1529 #else
1530 void tracing_snapshot(void)
1531 {
1532 	WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
1533 }
1534 EXPORT_SYMBOL_GPL(tracing_snapshot);
1535 void tracing_snapshot_cond(struct trace_array *tr, void *cond_data)
1536 {
1537 	WARN_ONCE(1, "Snapshot feature not enabled, but internal conditional snapshot used");
1538 }
1539 EXPORT_SYMBOL_GPL(tracing_snapshot_cond);
1540 int tracing_alloc_snapshot(void)
1541 {
1542 	WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used");
1543 	return -ENODEV;
1544 }
1545 EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
1546 void tracing_snapshot_alloc(void)
1547 {
1548 	/* Give warning */
1549 	tracing_snapshot();
1550 }
1551 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
1552 void *tracing_cond_snapshot_data(struct trace_array *tr)
1553 {
1554 	return NULL;
1555 }
1556 EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data);
1557 int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data, cond_update_fn_t update)
1558 {
1559 	return -ENODEV;
1560 }
1561 EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable);
1562 int tracing_snapshot_cond_disable(struct trace_array *tr)
1563 {
1564 	return false;
1565 }
1566 EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable);
1567 #define free_snapshot(tr)	do { } while (0)
1568 #define tracing_arm_snapshot_locked(tr) ({ -EBUSY; })
1569 #endif /* CONFIG_TRACER_SNAPSHOT */
1570 
1571 void tracer_tracing_off(struct trace_array *tr)
1572 {
1573 	if (tr->array_buffer.buffer)
1574 		ring_buffer_record_off(tr->array_buffer.buffer);
1575 	/*
1576 	 * This flag is looked at when buffers haven't been allocated
1577 	 * yet, or by some tracers (like irqsoff), that just want to
1578 	 * know if the ring buffer has been disabled, but it can handle
1579 	 * races of where it gets disabled but we still do a record.
1580 	 * As the check is in the fast path of the tracers, it is more
1581 	 * important to be fast than accurate.
1582 	 */
1583 	tr->buffer_disabled = 1;
1584 	/* Make the flag seen by readers */
1585 	smp_wmb();
1586 }
1587 
1588 /**
1589  * tracing_off - turn off tracing buffers
1590  *
1591  * This function stops the tracing buffers from recording data.
1592  * It does not disable any overhead the tracers themselves may
1593  * be causing. This function simply causes all recording to
1594  * the ring buffers to fail.
1595  */
1596 void tracing_off(void)
1597 {
1598 	tracer_tracing_off(&global_trace);
1599 }
1600 EXPORT_SYMBOL_GPL(tracing_off);
1601 
1602 void disable_trace_on_warning(void)
1603 {
1604 	if (__disable_trace_on_warning) {
1605 		trace_array_printk_buf(global_trace.array_buffer.buffer, _THIS_IP_,
1606 			"Disabling tracing due to warning\n");
1607 		tracing_off();
1608 	}
1609 }
1610 
1611 /**
1612  * tracer_tracing_is_on - show real state of ring buffer enabled
1613  * @tr : the trace array to know if ring buffer is enabled
1614  *
1615  * Shows real state of the ring buffer if it is enabled or not.
1616  */
1617 bool tracer_tracing_is_on(struct trace_array *tr)
1618 {
1619 	if (tr->array_buffer.buffer)
1620 		return ring_buffer_record_is_set_on(tr->array_buffer.buffer);
1621 	return !tr->buffer_disabled;
1622 }
1623 
1624 /**
1625  * tracing_is_on - show state of ring buffers enabled
1626  */
1627 int tracing_is_on(void)
1628 {
1629 	return tracer_tracing_is_on(&global_trace);
1630 }
1631 EXPORT_SYMBOL_GPL(tracing_is_on);
1632 
1633 static int __init set_buf_size(char *str)
1634 {
1635 	unsigned long buf_size;
1636 
1637 	if (!str)
1638 		return 0;
1639 	buf_size = memparse(str, &str);
1640 	/*
1641 	 * nr_entries can not be zero and the startup
1642 	 * tests require some buffer space. Therefore
1643 	 * ensure we have at least 4096 bytes of buffer.
1644 	 */
1645 	trace_buf_size = max(4096UL, buf_size);
1646 	return 1;
1647 }
1648 __setup("trace_buf_size=", set_buf_size);
1649 
1650 static int __init set_tracing_thresh(char *str)
1651 {
1652 	unsigned long threshold;
1653 	int ret;
1654 
1655 	if (!str)
1656 		return 0;
1657 	ret = kstrtoul(str, 0, &threshold);
1658 	if (ret < 0)
1659 		return 0;
1660 	tracing_thresh = threshold * 1000;
1661 	return 1;
1662 }
1663 __setup("tracing_thresh=", set_tracing_thresh);
1664 
1665 unsigned long nsecs_to_usecs(unsigned long nsecs)
1666 {
1667 	return nsecs / 1000;
1668 }
1669 
1670 /*
1671  * TRACE_FLAGS is defined as a tuple matching bit masks with strings.
1672  * It uses C(a, b) where 'a' is the eval (enum) name and 'b' is the string that
1673  * matches it. By defining "C(a, b) b", TRACE_FLAGS becomes a list
1674  * of strings in the order that the evals (enum) were defined.
1675  */
1676 #undef C
1677 #define C(a, b) b
1678 
1679 /* These must match the bit positions in trace_iterator_flags */
1680 static const char *trace_options[] = {
1681 	TRACE_FLAGS
1682 	NULL
1683 };
1684 
1685 static struct {
1686 	u64 (*func)(void);
1687 	const char *name;
1688 	int in_ns;		/* is this clock in nanoseconds? */
1689 } trace_clocks[] = {
1690 	{ trace_clock_local,		"local",	1 },
1691 	{ trace_clock_global,		"global",	1 },
1692 	{ trace_clock_counter,		"counter",	0 },
1693 	{ trace_clock_jiffies,		"uptime",	0 },
1694 	{ trace_clock,			"perf",		1 },
1695 	{ ktime_get_mono_fast_ns,	"mono",		1 },
1696 	{ ktime_get_raw_fast_ns,	"mono_raw",	1 },
1697 	{ ktime_get_boot_fast_ns,	"boot",		1 },
1698 	{ ktime_get_tai_fast_ns,	"tai",		1 },
1699 	ARCH_TRACE_CLOCKS
1700 };
1701 
1702 bool trace_clock_in_ns(struct trace_array *tr)
1703 {
1704 	if (trace_clocks[tr->clock_id].in_ns)
1705 		return true;
1706 
1707 	return false;
1708 }
1709 
1710 /*
1711  * trace_parser_get_init - gets the buffer for trace parser
1712  */
1713 int trace_parser_get_init(struct trace_parser *parser, int size)
1714 {
1715 	memset(parser, 0, sizeof(*parser));
1716 
1717 	parser->buffer = kmalloc(size, GFP_KERNEL);
1718 	if (!parser->buffer)
1719 		return 1;
1720 
1721 	parser->size = size;
1722 	return 0;
1723 }
1724 
1725 /*
1726  * trace_parser_put - frees the buffer for trace parser
1727  */
1728 void trace_parser_put(struct trace_parser *parser)
1729 {
1730 	kfree(parser->buffer);
1731 	parser->buffer = NULL;
1732 }
1733 
1734 /*
1735  * trace_get_user - reads the user input string separated by  space
1736  * (matched by isspace(ch))
1737  *
1738  * For each string found the 'struct trace_parser' is updated,
1739  * and the function returns.
1740  *
1741  * Returns number of bytes read.
1742  *
1743  * See kernel/trace/trace.h for 'struct trace_parser' details.
1744  */
1745 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
1746 	size_t cnt, loff_t *ppos)
1747 {
1748 	char ch;
1749 	size_t read = 0;
1750 	ssize_t ret;
1751 
1752 	if (!*ppos)
1753 		trace_parser_clear(parser);
1754 
1755 	ret = get_user(ch, ubuf++);
1756 	if (ret)
1757 		goto out;
1758 
1759 	read++;
1760 	cnt--;
1761 
1762 	/*
1763 	 * The parser is not finished with the last write,
1764 	 * continue reading the user input without skipping spaces.
1765 	 */
1766 	if (!parser->cont) {
1767 		/* skip white space */
1768 		while (cnt && isspace(ch)) {
1769 			ret = get_user(ch, ubuf++);
1770 			if (ret)
1771 				goto out;
1772 			read++;
1773 			cnt--;
1774 		}
1775 
1776 		parser->idx = 0;
1777 
1778 		/* only spaces were written */
1779 		if (isspace(ch) || !ch) {
1780 			*ppos += read;
1781 			ret = read;
1782 			goto out;
1783 		}
1784 	}
1785 
1786 	/* read the non-space input */
1787 	while (cnt && !isspace(ch) && ch) {
1788 		if (parser->idx < parser->size - 1)
1789 			parser->buffer[parser->idx++] = ch;
1790 		else {
1791 			ret = -EINVAL;
1792 			goto out;
1793 		}
1794 		ret = get_user(ch, ubuf++);
1795 		if (ret)
1796 			goto out;
1797 		read++;
1798 		cnt--;
1799 	}
1800 
1801 	/* We either got finished input or we have to wait for another call. */
1802 	if (isspace(ch) || !ch) {
1803 		parser->buffer[parser->idx] = 0;
1804 		parser->cont = false;
1805 	} else if (parser->idx < parser->size - 1) {
1806 		parser->cont = true;
1807 		parser->buffer[parser->idx++] = ch;
1808 		/* Make sure the parsed string always terminates with '\0'. */
1809 		parser->buffer[parser->idx] = 0;
1810 	} else {
1811 		ret = -EINVAL;
1812 		goto out;
1813 	}
1814 
1815 	*ppos += read;
1816 	ret = read;
1817 
1818 out:
1819 	return ret;
1820 }
1821 
1822 /* TODO add a seq_buf_to_buffer() */
1823 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
1824 {
1825 	int len;
1826 
1827 	if (trace_seq_used(s) <= s->readpos)
1828 		return -EBUSY;
1829 
1830 	len = trace_seq_used(s) - s->readpos;
1831 	if (cnt > len)
1832 		cnt = len;
1833 	memcpy(buf, s->buffer + s->readpos, cnt);
1834 
1835 	s->readpos += cnt;
1836 	return cnt;
1837 }
1838 
1839 unsigned long __read_mostly	tracing_thresh;
1840 
1841 #ifdef CONFIG_TRACER_MAX_TRACE
1842 static const struct file_operations tracing_max_lat_fops;
1843 
1844 #ifdef LATENCY_FS_NOTIFY
1845 
1846 static struct workqueue_struct *fsnotify_wq;
1847 
1848 static void latency_fsnotify_workfn(struct work_struct *work)
1849 {
1850 	struct trace_array *tr = container_of(work, struct trace_array,
1851 					      fsnotify_work);
1852 	fsnotify_inode(tr->d_max_latency->d_inode, FS_MODIFY);
1853 }
1854 
1855 static void latency_fsnotify_workfn_irq(struct irq_work *iwork)
1856 {
1857 	struct trace_array *tr = container_of(iwork, struct trace_array,
1858 					      fsnotify_irqwork);
1859 	queue_work(fsnotify_wq, &tr->fsnotify_work);
1860 }
1861 
1862 static void trace_create_maxlat_file(struct trace_array *tr,
1863 				     struct dentry *d_tracer)
1864 {
1865 	INIT_WORK(&tr->fsnotify_work, latency_fsnotify_workfn);
1866 	init_irq_work(&tr->fsnotify_irqwork, latency_fsnotify_workfn_irq);
1867 	tr->d_max_latency = trace_create_file("tracing_max_latency",
1868 					      TRACE_MODE_WRITE,
1869 					      d_tracer, tr,
1870 					      &tracing_max_lat_fops);
1871 }
1872 
1873 __init static int latency_fsnotify_init(void)
1874 {
1875 	fsnotify_wq = alloc_workqueue("tr_max_lat_wq",
1876 				      WQ_UNBOUND | WQ_HIGHPRI, 0);
1877 	if (!fsnotify_wq) {
1878 		pr_err("Unable to allocate tr_max_lat_wq\n");
1879 		return -ENOMEM;
1880 	}
1881 	return 0;
1882 }
1883 
1884 late_initcall_sync(latency_fsnotify_init);
1885 
1886 void latency_fsnotify(struct trace_array *tr)
1887 {
1888 	if (!fsnotify_wq)
1889 		return;
1890 	/*
1891 	 * We cannot call queue_work(&tr->fsnotify_work) from here because it's
1892 	 * possible that we are called from __schedule() or do_idle(), which
1893 	 * could cause a deadlock.
1894 	 */
1895 	irq_work_queue(&tr->fsnotify_irqwork);
1896 }
1897 
1898 #else /* !LATENCY_FS_NOTIFY */
1899 
1900 #define trace_create_maxlat_file(tr, d_tracer)				\
1901 	trace_create_file("tracing_max_latency", TRACE_MODE_WRITE,	\
1902 			  d_tracer, tr, &tracing_max_lat_fops)
1903 
1904 #endif
1905 
1906 /*
1907  * Copy the new maximum trace into the separate maximum-trace
1908  * structure. (this way the maximum trace is permanently saved,
1909  * for later retrieval via /sys/kernel/tracing/tracing_max_latency)
1910  */
1911 static void
1912 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
1913 {
1914 	struct array_buffer *trace_buf = &tr->array_buffer;
1915 	struct array_buffer *max_buf = &tr->max_buffer;
1916 	struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
1917 	struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
1918 
1919 	max_buf->cpu = cpu;
1920 	max_buf->time_start = data->preempt_timestamp;
1921 
1922 	max_data->saved_latency = tr->max_latency;
1923 	max_data->critical_start = data->critical_start;
1924 	max_data->critical_end = data->critical_end;
1925 
1926 	strscpy(max_data->comm, tsk->comm);
1927 	max_data->pid = tsk->pid;
1928 	/*
1929 	 * If tsk == current, then use current_uid(), as that does not use
1930 	 * RCU. The irq tracer can be called out of RCU scope.
1931 	 */
1932 	if (tsk == current)
1933 		max_data->uid = current_uid();
1934 	else
1935 		max_data->uid = task_uid(tsk);
1936 
1937 	max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
1938 	max_data->policy = tsk->policy;
1939 	max_data->rt_priority = tsk->rt_priority;
1940 
1941 	/* record this tasks comm */
1942 	tracing_record_cmdline(tsk);
1943 	latency_fsnotify(tr);
1944 }
1945 
1946 /**
1947  * update_max_tr - snapshot all trace buffers from global_trace to max_tr
1948  * @tr: tracer
1949  * @tsk: the task with the latency
1950  * @cpu: The cpu that initiated the trace.
1951  * @cond_data: User data associated with a conditional snapshot
1952  *
1953  * Flip the buffers between the @tr and the max_tr and record information
1954  * about which task was the cause of this latency.
1955  */
1956 void
1957 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu,
1958 	      void *cond_data)
1959 {
1960 	if (tr->stop_count)
1961 		return;
1962 
1963 	WARN_ON_ONCE(!irqs_disabled());
1964 
1965 	if (!tr->allocated_snapshot) {
1966 		/* Only the nop tracer should hit this when disabling */
1967 		WARN_ON_ONCE(tr->current_trace != &nop_trace);
1968 		return;
1969 	}
1970 
1971 	arch_spin_lock(&tr->max_lock);
1972 
1973 	/* Inherit the recordable setting from array_buffer */
1974 	if (ring_buffer_record_is_set_on(tr->array_buffer.buffer))
1975 		ring_buffer_record_on(tr->max_buffer.buffer);
1976 	else
1977 		ring_buffer_record_off(tr->max_buffer.buffer);
1978 
1979 #ifdef CONFIG_TRACER_SNAPSHOT
1980 	if (tr->cond_snapshot && !tr->cond_snapshot->update(tr, cond_data)) {
1981 		arch_spin_unlock(&tr->max_lock);
1982 		return;
1983 	}
1984 #endif
1985 	swap(tr->array_buffer.buffer, tr->max_buffer.buffer);
1986 
1987 	__update_max_tr(tr, tsk, cpu);
1988 
1989 	arch_spin_unlock(&tr->max_lock);
1990 
1991 	/* Any waiters on the old snapshot buffer need to wake up */
1992 	ring_buffer_wake_waiters(tr->array_buffer.buffer, RING_BUFFER_ALL_CPUS);
1993 }
1994 
1995 /**
1996  * update_max_tr_single - only copy one trace over, and reset the rest
1997  * @tr: tracer
1998  * @tsk: task with the latency
1999  * @cpu: the cpu of the buffer to copy.
2000  *
2001  * Flip the trace of a single CPU buffer between the @tr and the max_tr.
2002  */
2003 void
2004 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
2005 {
2006 	int ret;
2007 
2008 	if (tr->stop_count)
2009 		return;
2010 
2011 	WARN_ON_ONCE(!irqs_disabled());
2012 	if (!tr->allocated_snapshot) {
2013 		/* Only the nop tracer should hit this when disabling */
2014 		WARN_ON_ONCE(tr->current_trace != &nop_trace);
2015 		return;
2016 	}
2017 
2018 	arch_spin_lock(&tr->max_lock);
2019 
2020 	ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->array_buffer.buffer, cpu);
2021 
2022 	if (ret == -EBUSY) {
2023 		/*
2024 		 * We failed to swap the buffer due to a commit taking
2025 		 * place on this CPU. We fail to record, but we reset
2026 		 * the max trace buffer (no one writes directly to it)
2027 		 * and flag that it failed.
2028 		 * Another reason is resize is in progress.
2029 		 */
2030 		trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
2031 			"Failed to swap buffers due to commit or resize in progress\n");
2032 	}
2033 
2034 	WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
2035 
2036 	__update_max_tr(tr, tsk, cpu);
2037 	arch_spin_unlock(&tr->max_lock);
2038 }
2039 
2040 #endif /* CONFIG_TRACER_MAX_TRACE */
2041 
2042 struct pipe_wait {
2043 	struct trace_iterator		*iter;
2044 	int				wait_index;
2045 };
2046 
2047 static bool wait_pipe_cond(void *data)
2048 {
2049 	struct pipe_wait *pwait = data;
2050 	struct trace_iterator *iter = pwait->iter;
2051 
2052 	if (atomic_read_acquire(&iter->wait_index) != pwait->wait_index)
2053 		return true;
2054 
2055 	return iter->closed;
2056 }
2057 
2058 static int wait_on_pipe(struct trace_iterator *iter, int full)
2059 {
2060 	struct pipe_wait pwait;
2061 	int ret;
2062 
2063 	/* Iterators are static, they should be filled or empty */
2064 	if (trace_buffer_iter(iter, iter->cpu_file))
2065 		return 0;
2066 
2067 	pwait.wait_index = atomic_read_acquire(&iter->wait_index);
2068 	pwait.iter = iter;
2069 
2070 	ret = ring_buffer_wait(iter->array_buffer->buffer, iter->cpu_file, full,
2071 			       wait_pipe_cond, &pwait);
2072 
2073 #ifdef CONFIG_TRACER_MAX_TRACE
2074 	/*
2075 	 * Make sure this is still the snapshot buffer, as if a snapshot were
2076 	 * to happen, this would now be the main buffer.
2077 	 */
2078 	if (iter->snapshot)
2079 		iter->array_buffer = &iter->tr->max_buffer;
2080 #endif
2081 	return ret;
2082 }
2083 
2084 #ifdef CONFIG_FTRACE_STARTUP_TEST
2085 static bool selftests_can_run;
2086 
2087 struct trace_selftests {
2088 	struct list_head		list;
2089 	struct tracer			*type;
2090 };
2091 
2092 static LIST_HEAD(postponed_selftests);
2093 
2094 static int save_selftest(struct tracer *type)
2095 {
2096 	struct trace_selftests *selftest;
2097 
2098 	selftest = kmalloc(sizeof(*selftest), GFP_KERNEL);
2099 	if (!selftest)
2100 		return -ENOMEM;
2101 
2102 	selftest->type = type;
2103 	list_add(&selftest->list, &postponed_selftests);
2104 	return 0;
2105 }
2106 
2107 static int run_tracer_selftest(struct tracer *type)
2108 {
2109 	struct trace_array *tr = &global_trace;
2110 	struct tracer *saved_tracer = tr->current_trace;
2111 	int ret;
2112 
2113 	if (!type->selftest || tracing_selftest_disabled)
2114 		return 0;
2115 
2116 	/*
2117 	 * If a tracer registers early in boot up (before scheduling is
2118 	 * initialized and such), then do not run its selftests yet.
2119 	 * Instead, run it a little later in the boot process.
2120 	 */
2121 	if (!selftests_can_run)
2122 		return save_selftest(type);
2123 
2124 	if (!tracing_is_on()) {
2125 		pr_warn("Selftest for tracer %s skipped due to tracing disabled\n",
2126 			type->name);
2127 		return 0;
2128 	}
2129 
2130 	/*
2131 	 * Run a selftest on this tracer.
2132 	 * Here we reset the trace buffer, and set the current
2133 	 * tracer to be this tracer. The tracer can then run some
2134 	 * internal tracing to verify that everything is in order.
2135 	 * If we fail, we do not register this tracer.
2136 	 */
2137 	tracing_reset_online_cpus(&tr->array_buffer);
2138 
2139 	tr->current_trace = type;
2140 
2141 #ifdef CONFIG_TRACER_MAX_TRACE
2142 	if (type->use_max_tr) {
2143 		/* If we expanded the buffers, make sure the max is expanded too */
2144 		if (tr->ring_buffer_expanded)
2145 			ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
2146 					   RING_BUFFER_ALL_CPUS);
2147 		tr->allocated_snapshot = true;
2148 	}
2149 #endif
2150 
2151 	/* the test is responsible for initializing and enabling */
2152 	pr_info("Testing tracer %s: ", type->name);
2153 	ret = type->selftest(type, tr);
2154 	/* the test is responsible for resetting too */
2155 	tr->current_trace = saved_tracer;
2156 	if (ret) {
2157 		printk(KERN_CONT "FAILED!\n");
2158 		/* Add the warning after printing 'FAILED' */
2159 		WARN_ON(1);
2160 		return -1;
2161 	}
2162 	/* Only reset on passing, to avoid touching corrupted buffers */
2163 	tracing_reset_online_cpus(&tr->array_buffer);
2164 
2165 #ifdef CONFIG_TRACER_MAX_TRACE
2166 	if (type->use_max_tr) {
2167 		tr->allocated_snapshot = false;
2168 
2169 		/* Shrink the max buffer again */
2170 		if (tr->ring_buffer_expanded)
2171 			ring_buffer_resize(tr->max_buffer.buffer, 1,
2172 					   RING_BUFFER_ALL_CPUS);
2173 	}
2174 #endif
2175 
2176 	printk(KERN_CONT "PASSED\n");
2177 	return 0;
2178 }
2179 
2180 static int do_run_tracer_selftest(struct tracer *type)
2181 {
2182 	int ret;
2183 
2184 	/*
2185 	 * Tests can take a long time, especially if they are run one after the
2186 	 * other, as does happen during bootup when all the tracers are
2187 	 * registered. This could cause the soft lockup watchdog to trigger.
2188 	 */
2189 	cond_resched();
2190 
2191 	tracing_selftest_running = true;
2192 	ret = run_tracer_selftest(type);
2193 	tracing_selftest_running = false;
2194 
2195 	return ret;
2196 }
2197 
2198 static __init int init_trace_selftests(void)
2199 {
2200 	struct trace_selftests *p, *n;
2201 	struct tracer *t, **last;
2202 	int ret;
2203 
2204 	selftests_can_run = true;
2205 
2206 	mutex_lock(&trace_types_lock);
2207 
2208 	if (list_empty(&postponed_selftests))
2209 		goto out;
2210 
2211 	pr_info("Running postponed tracer tests:\n");
2212 
2213 	tracing_selftest_running = true;
2214 	list_for_each_entry_safe(p, n, &postponed_selftests, list) {
2215 		/* This loop can take minutes when sanitizers are enabled, so
2216 		 * lets make sure we allow RCU processing.
2217 		 */
2218 		cond_resched();
2219 		ret = run_tracer_selftest(p->type);
2220 		/* If the test fails, then warn and remove from available_tracers */
2221 		if (ret < 0) {
2222 			WARN(1, "tracer: %s failed selftest, disabling\n",
2223 			     p->type->name);
2224 			last = &trace_types;
2225 			for (t = trace_types; t; t = t->next) {
2226 				if (t == p->type) {
2227 					*last = t->next;
2228 					break;
2229 				}
2230 				last = &t->next;
2231 			}
2232 		}
2233 		list_del(&p->list);
2234 		kfree(p);
2235 	}
2236 	tracing_selftest_running = false;
2237 
2238  out:
2239 	mutex_unlock(&trace_types_lock);
2240 
2241 	return 0;
2242 }
2243 core_initcall(init_trace_selftests);
2244 #else
2245 static inline int do_run_tracer_selftest(struct tracer *type)
2246 {
2247 	return 0;
2248 }
2249 #endif /* CONFIG_FTRACE_STARTUP_TEST */
2250 
2251 static void add_tracer_options(struct trace_array *tr, struct tracer *t);
2252 
2253 static void __init apply_trace_boot_options(void);
2254 
2255 /**
2256  * register_tracer - register a tracer with the ftrace system.
2257  * @type: the plugin for the tracer
2258  *
2259  * Register a new plugin tracer.
2260  */
2261 int __init register_tracer(struct tracer *type)
2262 {
2263 	struct tracer *t;
2264 	int ret = 0;
2265 
2266 	if (!type->name) {
2267 		pr_info("Tracer must have a name\n");
2268 		return -1;
2269 	}
2270 
2271 	if (strlen(type->name) >= MAX_TRACER_SIZE) {
2272 		pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
2273 		return -1;
2274 	}
2275 
2276 	if (security_locked_down(LOCKDOWN_TRACEFS)) {
2277 		pr_warn("Can not register tracer %s due to lockdown\n",
2278 			   type->name);
2279 		return -EPERM;
2280 	}
2281 
2282 	mutex_lock(&trace_types_lock);
2283 
2284 	for (t = trace_types; t; t = t->next) {
2285 		if (strcmp(type->name, t->name) == 0) {
2286 			/* already found */
2287 			pr_info("Tracer %s already registered\n",
2288 				type->name);
2289 			ret = -1;
2290 			goto out;
2291 		}
2292 	}
2293 
2294 	if (!type->set_flag)
2295 		type->set_flag = &dummy_set_flag;
2296 	if (!type->flags) {
2297 		/*allocate a dummy tracer_flags*/
2298 		type->flags = kmalloc(sizeof(*type->flags), GFP_KERNEL);
2299 		if (!type->flags) {
2300 			ret = -ENOMEM;
2301 			goto out;
2302 		}
2303 		type->flags->val = 0;
2304 		type->flags->opts = dummy_tracer_opt;
2305 	} else
2306 		if (!type->flags->opts)
2307 			type->flags->opts = dummy_tracer_opt;
2308 
2309 	/* store the tracer for __set_tracer_option */
2310 	type->flags->trace = type;
2311 
2312 	ret = do_run_tracer_selftest(type);
2313 	if (ret < 0)
2314 		goto out;
2315 
2316 	type->next = trace_types;
2317 	trace_types = type;
2318 	add_tracer_options(&global_trace, type);
2319 
2320  out:
2321 	mutex_unlock(&trace_types_lock);
2322 
2323 	if (ret || !default_bootup_tracer)
2324 		goto out_unlock;
2325 
2326 	if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
2327 		goto out_unlock;
2328 
2329 	printk(KERN_INFO "Starting tracer '%s'\n", type->name);
2330 	/* Do we want this tracer to start on bootup? */
2331 	tracing_set_tracer(&global_trace, type->name);
2332 	default_bootup_tracer = NULL;
2333 
2334 	apply_trace_boot_options();
2335 
2336 	/* disable other selftests, since this will break it. */
2337 	disable_tracing_selftest("running a tracer");
2338 
2339  out_unlock:
2340 	return ret;
2341 }
2342 
2343 static void tracing_reset_cpu(struct array_buffer *buf, int cpu)
2344 {
2345 	struct trace_buffer *buffer = buf->buffer;
2346 
2347 	if (!buffer)
2348 		return;
2349 
2350 	ring_buffer_record_disable(buffer);
2351 
2352 	/* Make sure all commits have finished */
2353 	synchronize_rcu();
2354 	ring_buffer_reset_cpu(buffer, cpu);
2355 
2356 	ring_buffer_record_enable(buffer);
2357 }
2358 
2359 void tracing_reset_online_cpus(struct array_buffer *buf)
2360 {
2361 	struct trace_buffer *buffer = buf->buffer;
2362 
2363 	if (!buffer)
2364 		return;
2365 
2366 	ring_buffer_record_disable(buffer);
2367 
2368 	/* Make sure all commits have finished */
2369 	synchronize_rcu();
2370 
2371 	buf->time_start = buffer_ftrace_now(buf, buf->cpu);
2372 
2373 	ring_buffer_reset_online_cpus(buffer);
2374 
2375 	ring_buffer_record_enable(buffer);
2376 }
2377 
2378 static void tracing_reset_all_cpus(struct array_buffer *buf)
2379 {
2380 	struct trace_buffer *buffer = buf->buffer;
2381 
2382 	if (!buffer)
2383 		return;
2384 
2385 	ring_buffer_record_disable(buffer);
2386 
2387 	/* Make sure all commits have finished */
2388 	synchronize_rcu();
2389 
2390 	buf->time_start = buffer_ftrace_now(buf, buf->cpu);
2391 
2392 	ring_buffer_reset(buffer);
2393 
2394 	ring_buffer_record_enable(buffer);
2395 }
2396 
2397 /* Must have trace_types_lock held */
2398 void tracing_reset_all_online_cpus_unlocked(void)
2399 {
2400 	struct trace_array *tr;
2401 
2402 	lockdep_assert_held(&trace_types_lock);
2403 
2404 	list_for_each_entry(tr, &ftrace_trace_arrays, list) {
2405 		if (!tr->clear_trace)
2406 			continue;
2407 		tr->clear_trace = false;
2408 		tracing_reset_online_cpus(&tr->array_buffer);
2409 #ifdef CONFIG_TRACER_MAX_TRACE
2410 		tracing_reset_online_cpus(&tr->max_buffer);
2411 #endif
2412 	}
2413 }
2414 
2415 void tracing_reset_all_online_cpus(void)
2416 {
2417 	mutex_lock(&trace_types_lock);
2418 	tracing_reset_all_online_cpus_unlocked();
2419 	mutex_unlock(&trace_types_lock);
2420 }
2421 
2422 int is_tracing_stopped(void)
2423 {
2424 	return global_trace.stop_count;
2425 }
2426 
2427 static void tracing_start_tr(struct trace_array *tr)
2428 {
2429 	struct trace_buffer *buffer;
2430 	unsigned long flags;
2431 
2432 	if (tracing_disabled)
2433 		return;
2434 
2435 	raw_spin_lock_irqsave(&tr->start_lock, flags);
2436 	if (--tr->stop_count) {
2437 		if (WARN_ON_ONCE(tr->stop_count < 0)) {
2438 			/* Someone screwed up their debugging */
2439 			tr->stop_count = 0;
2440 		}
2441 		goto out;
2442 	}
2443 
2444 	/* Prevent the buffers from switching */
2445 	arch_spin_lock(&tr->max_lock);
2446 
2447 	buffer = tr->array_buffer.buffer;
2448 	if (buffer)
2449 		ring_buffer_record_enable(buffer);
2450 
2451 #ifdef CONFIG_TRACER_MAX_TRACE
2452 	buffer = tr->max_buffer.buffer;
2453 	if (buffer)
2454 		ring_buffer_record_enable(buffer);
2455 #endif
2456 
2457 	arch_spin_unlock(&tr->max_lock);
2458 
2459  out:
2460 	raw_spin_unlock_irqrestore(&tr->start_lock, flags);
2461 }
2462 
2463 /**
2464  * tracing_start - quick start of the tracer
2465  *
2466  * If tracing is enabled but was stopped by tracing_stop,
2467  * this will start the tracer back up.
2468  */
2469 void tracing_start(void)
2470 
2471 {
2472 	return tracing_start_tr(&global_trace);
2473 }
2474 
2475 static void tracing_stop_tr(struct trace_array *tr)
2476 {
2477 	struct trace_buffer *buffer;
2478 	unsigned long flags;
2479 
2480 	raw_spin_lock_irqsave(&tr->start_lock, flags);
2481 	if (tr->stop_count++)
2482 		goto out;
2483 
2484 	/* Prevent the buffers from switching */
2485 	arch_spin_lock(&tr->max_lock);
2486 
2487 	buffer = tr->array_buffer.buffer;
2488 	if (buffer)
2489 		ring_buffer_record_disable(buffer);
2490 
2491 #ifdef CONFIG_TRACER_MAX_TRACE
2492 	buffer = tr->max_buffer.buffer;
2493 	if (buffer)
2494 		ring_buffer_record_disable(buffer);
2495 #endif
2496 
2497 	arch_spin_unlock(&tr->max_lock);
2498 
2499  out:
2500 	raw_spin_unlock_irqrestore(&tr->start_lock, flags);
2501 }
2502 
2503 /**
2504  * tracing_stop - quick stop of the tracer
2505  *
2506  * Light weight way to stop tracing. Use in conjunction with
2507  * tracing_start.
2508  */
2509 void tracing_stop(void)
2510 {
2511 	return tracing_stop_tr(&global_trace);
2512 }
2513 
2514 /*
2515  * Several functions return TRACE_TYPE_PARTIAL_LINE if the trace_seq
2516  * overflowed, and TRACE_TYPE_HANDLED otherwise. This helper function
2517  * simplifies those functions and keeps them in sync.
2518  */
2519 enum print_line_t trace_handle_return(struct trace_seq *s)
2520 {
2521 	return trace_seq_has_overflowed(s) ?
2522 		TRACE_TYPE_PARTIAL_LINE : TRACE_TYPE_HANDLED;
2523 }
2524 EXPORT_SYMBOL_GPL(trace_handle_return);
2525 
2526 static unsigned short migration_disable_value(void)
2527 {
2528 #if defined(CONFIG_SMP)
2529 	return current->migration_disabled;
2530 #else
2531 	return 0;
2532 #endif
2533 }
2534 
2535 unsigned int tracing_gen_ctx_irq_test(unsigned int irqs_status)
2536 {
2537 	unsigned int trace_flags = irqs_status;
2538 	unsigned int pc;
2539 
2540 	pc = preempt_count();
2541 
2542 	if (pc & NMI_MASK)
2543 		trace_flags |= TRACE_FLAG_NMI;
2544 	if (pc & HARDIRQ_MASK)
2545 		trace_flags |= TRACE_FLAG_HARDIRQ;
2546 	if (in_serving_softirq())
2547 		trace_flags |= TRACE_FLAG_SOFTIRQ;
2548 	if (softirq_count() >> (SOFTIRQ_SHIFT + 1))
2549 		trace_flags |= TRACE_FLAG_BH_OFF;
2550 
2551 	if (tif_need_resched())
2552 		trace_flags |= TRACE_FLAG_NEED_RESCHED;
2553 	if (test_preempt_need_resched())
2554 		trace_flags |= TRACE_FLAG_PREEMPT_RESCHED;
2555 	return (trace_flags << 16) | (min_t(unsigned int, pc & 0xff, 0xf)) |
2556 		(min_t(unsigned int, migration_disable_value(), 0xf)) << 4;
2557 }
2558 
2559 struct ring_buffer_event *
2560 trace_buffer_lock_reserve(struct trace_buffer *buffer,
2561 			  int type,
2562 			  unsigned long len,
2563 			  unsigned int trace_ctx)
2564 {
2565 	return __trace_buffer_lock_reserve(buffer, type, len, trace_ctx);
2566 }
2567 
2568 DEFINE_PER_CPU(struct ring_buffer_event *, trace_buffered_event);
2569 DEFINE_PER_CPU(int, trace_buffered_event_cnt);
2570 static int trace_buffered_event_ref;
2571 
2572 /**
2573  * trace_buffered_event_enable - enable buffering events
2574  *
2575  * When events are being filtered, it is quicker to use a temporary
2576  * buffer to write the event data into if there's a likely chance
2577  * that it will not be committed. The discard of the ring buffer
2578  * is not as fast as committing, and is much slower than copying
2579  * a commit.
2580  *
2581  * When an event is to be filtered, allocate per cpu buffers to
2582  * write the event data into, and if the event is filtered and discarded
2583  * it is simply dropped, otherwise, the entire data is to be committed
2584  * in one shot.
2585  */
2586 void trace_buffered_event_enable(void)
2587 {
2588 	struct ring_buffer_event *event;
2589 	struct page *page;
2590 	int cpu;
2591 
2592 	WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
2593 
2594 	if (trace_buffered_event_ref++)
2595 		return;
2596 
2597 	for_each_tracing_cpu(cpu) {
2598 		page = alloc_pages_node(cpu_to_node(cpu),
2599 					GFP_KERNEL | __GFP_NORETRY, 0);
2600 		/* This is just an optimization and can handle failures */
2601 		if (!page) {
2602 			pr_err("Failed to allocate event buffer\n");
2603 			break;
2604 		}
2605 
2606 		event = page_address(page);
2607 		memset(event, 0, sizeof(*event));
2608 
2609 		per_cpu(trace_buffered_event, cpu) = event;
2610 
2611 		preempt_disable();
2612 		if (cpu == smp_processor_id() &&
2613 		    __this_cpu_read(trace_buffered_event) !=
2614 		    per_cpu(trace_buffered_event, cpu))
2615 			WARN_ON_ONCE(1);
2616 		preempt_enable();
2617 	}
2618 }
2619 
2620 static void enable_trace_buffered_event(void *data)
2621 {
2622 	/* Probably not needed, but do it anyway */
2623 	smp_rmb();
2624 	this_cpu_dec(trace_buffered_event_cnt);
2625 }
2626 
2627 static void disable_trace_buffered_event(void *data)
2628 {
2629 	this_cpu_inc(trace_buffered_event_cnt);
2630 }
2631 
2632 /**
2633  * trace_buffered_event_disable - disable buffering events
2634  *
2635  * When a filter is removed, it is faster to not use the buffered
2636  * events, and to commit directly into the ring buffer. Free up
2637  * the temp buffers when there are no more users. This requires
2638  * special synchronization with current events.
2639  */
2640 void trace_buffered_event_disable(void)
2641 {
2642 	int cpu;
2643 
2644 	WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
2645 
2646 	if (WARN_ON_ONCE(!trace_buffered_event_ref))
2647 		return;
2648 
2649 	if (--trace_buffered_event_ref)
2650 		return;
2651 
2652 	/* For each CPU, set the buffer as used. */
2653 	on_each_cpu_mask(tracing_buffer_mask, disable_trace_buffered_event,
2654 			 NULL, true);
2655 
2656 	/* Wait for all current users to finish */
2657 	synchronize_rcu();
2658 
2659 	for_each_tracing_cpu(cpu) {
2660 		free_page((unsigned long)per_cpu(trace_buffered_event, cpu));
2661 		per_cpu(trace_buffered_event, cpu) = NULL;
2662 	}
2663 
2664 	/*
2665 	 * Wait for all CPUs that potentially started checking if they can use
2666 	 * their event buffer only after the previous synchronize_rcu() call and
2667 	 * they still read a valid pointer from trace_buffered_event. It must be
2668 	 * ensured they don't see cleared trace_buffered_event_cnt else they
2669 	 * could wrongly decide to use the pointed-to buffer which is now freed.
2670 	 */
2671 	synchronize_rcu();
2672 
2673 	/* For each CPU, relinquish the buffer */
2674 	on_each_cpu_mask(tracing_buffer_mask, enable_trace_buffered_event, NULL,
2675 			 true);
2676 }
2677 
2678 static struct trace_buffer *temp_buffer;
2679 
2680 struct ring_buffer_event *
2681 trace_event_buffer_lock_reserve(struct trace_buffer **current_rb,
2682 			  struct trace_event_file *trace_file,
2683 			  int type, unsigned long len,
2684 			  unsigned int trace_ctx)
2685 {
2686 	struct ring_buffer_event *entry;
2687 	struct trace_array *tr = trace_file->tr;
2688 	int val;
2689 
2690 	*current_rb = tr->array_buffer.buffer;
2691 
2692 	if (!tr->no_filter_buffering_ref &&
2693 	    (trace_file->flags & (EVENT_FILE_FL_SOFT_DISABLED | EVENT_FILE_FL_FILTERED))) {
2694 		preempt_disable_notrace();
2695 		/*
2696 		 * Filtering is on, so try to use the per cpu buffer first.
2697 		 * This buffer will simulate a ring_buffer_event,
2698 		 * where the type_len is zero and the array[0] will
2699 		 * hold the full length.
2700 		 * (see include/linux/ring-buffer.h for details on
2701 		 *  how the ring_buffer_event is structured).
2702 		 *
2703 		 * Using a temp buffer during filtering and copying it
2704 		 * on a matched filter is quicker than writing directly
2705 		 * into the ring buffer and then discarding it when
2706 		 * it doesn't match. That is because the discard
2707 		 * requires several atomic operations to get right.
2708 		 * Copying on match and doing nothing on a failed match
2709 		 * is still quicker than no copy on match, but having
2710 		 * to discard out of the ring buffer on a failed match.
2711 		 */
2712 		if ((entry = __this_cpu_read(trace_buffered_event))) {
2713 			int max_len = PAGE_SIZE - struct_size(entry, array, 1);
2714 
2715 			val = this_cpu_inc_return(trace_buffered_event_cnt);
2716 
2717 			/*
2718 			 * Preemption is disabled, but interrupts and NMIs
2719 			 * can still come in now. If that happens after
2720 			 * the above increment, then it will have to go
2721 			 * back to the old method of allocating the event
2722 			 * on the ring buffer, and if the filter fails, it
2723 			 * will have to call ring_buffer_discard_commit()
2724 			 * to remove it.
2725 			 *
2726 			 * Need to also check the unlikely case that the
2727 			 * length is bigger than the temp buffer size.
2728 			 * If that happens, then the reserve is pretty much
2729 			 * guaranteed to fail, as the ring buffer currently
2730 			 * only allows events less than a page. But that may
2731 			 * change in the future, so let the ring buffer reserve
2732 			 * handle the failure in that case.
2733 			 */
2734 			if (val == 1 && likely(len <= max_len)) {
2735 				trace_event_setup(entry, type, trace_ctx);
2736 				entry->array[0] = len;
2737 				/* Return with preemption disabled */
2738 				return entry;
2739 			}
2740 			this_cpu_dec(trace_buffered_event_cnt);
2741 		}
2742 		/* __trace_buffer_lock_reserve() disables preemption */
2743 		preempt_enable_notrace();
2744 	}
2745 
2746 	entry = __trace_buffer_lock_reserve(*current_rb, type, len,
2747 					    trace_ctx);
2748 	/*
2749 	 * If tracing is off, but we have triggers enabled
2750 	 * we still need to look at the event data. Use the temp_buffer
2751 	 * to store the trace event for the trigger to use. It's recursive
2752 	 * safe and will not be recorded anywhere.
2753 	 */
2754 	if (!entry && trace_file->flags & EVENT_FILE_FL_TRIGGER_COND) {
2755 		*current_rb = temp_buffer;
2756 		entry = __trace_buffer_lock_reserve(*current_rb, type, len,
2757 						    trace_ctx);
2758 	}
2759 	return entry;
2760 }
2761 EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
2762 
2763 static DEFINE_RAW_SPINLOCK(tracepoint_iter_lock);
2764 static DEFINE_MUTEX(tracepoint_printk_mutex);
2765 
2766 static void output_printk(struct trace_event_buffer *fbuffer)
2767 {
2768 	struct trace_event_call *event_call;
2769 	struct trace_event_file *file;
2770 	struct trace_event *event;
2771 	unsigned long flags;
2772 	struct trace_iterator *iter = tracepoint_print_iter;
2773 
2774 	/* We should never get here if iter is NULL */
2775 	if (WARN_ON_ONCE(!iter))
2776 		return;
2777 
2778 	event_call = fbuffer->trace_file->event_call;
2779 	if (!event_call || !event_call->event.funcs ||
2780 	    !event_call->event.funcs->trace)
2781 		return;
2782 
2783 	file = fbuffer->trace_file;
2784 	if (test_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags) ||
2785 	    (unlikely(file->flags & EVENT_FILE_FL_FILTERED) &&
2786 	     !filter_match_preds(file->filter, fbuffer->entry)))
2787 		return;
2788 
2789 	event = &fbuffer->trace_file->event_call->event;
2790 
2791 	raw_spin_lock_irqsave(&tracepoint_iter_lock, flags);
2792 	trace_seq_init(&iter->seq);
2793 	iter->ent = fbuffer->entry;
2794 	event_call->event.funcs->trace(iter, 0, event);
2795 	trace_seq_putc(&iter->seq, 0);
2796 	printk("%s", iter->seq.buffer);
2797 
2798 	raw_spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
2799 }
2800 
2801 int tracepoint_printk_sysctl(const struct ctl_table *table, int write,
2802 			     void *buffer, size_t *lenp,
2803 			     loff_t *ppos)
2804 {
2805 	int save_tracepoint_printk;
2806 	int ret;
2807 
2808 	mutex_lock(&tracepoint_printk_mutex);
2809 	save_tracepoint_printk = tracepoint_printk;
2810 
2811 	ret = proc_dointvec(table, write, buffer, lenp, ppos);
2812 
2813 	/*
2814 	 * This will force exiting early, as tracepoint_printk
2815 	 * is always zero when tracepoint_printk_iter is not allocated
2816 	 */
2817 	if (!tracepoint_print_iter)
2818 		tracepoint_printk = 0;
2819 
2820 	if (save_tracepoint_printk == tracepoint_printk)
2821 		goto out;
2822 
2823 	if (tracepoint_printk)
2824 		static_key_enable(&tracepoint_printk_key.key);
2825 	else
2826 		static_key_disable(&tracepoint_printk_key.key);
2827 
2828  out:
2829 	mutex_unlock(&tracepoint_printk_mutex);
2830 
2831 	return ret;
2832 }
2833 
2834 void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
2835 {
2836 	enum event_trigger_type tt = ETT_NONE;
2837 	struct trace_event_file *file = fbuffer->trace_file;
2838 
2839 	if (__event_trigger_test_discard(file, fbuffer->buffer, fbuffer->event,
2840 			fbuffer->entry, &tt))
2841 		goto discard;
2842 
2843 	if (static_key_false(&tracepoint_printk_key.key))
2844 		output_printk(fbuffer);
2845 
2846 	if (static_branch_unlikely(&trace_event_exports_enabled))
2847 		ftrace_exports(fbuffer->event, TRACE_EXPORT_EVENT);
2848 
2849 	trace_buffer_unlock_commit_regs(file->tr, fbuffer->buffer,
2850 			fbuffer->event, fbuffer->trace_ctx, fbuffer->regs);
2851 
2852 discard:
2853 	if (tt)
2854 		event_triggers_post_call(file, tt);
2855 
2856 }
2857 EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
2858 
2859 /*
2860  * Skip 3:
2861  *
2862  *   trace_buffer_unlock_commit_regs()
2863  *   trace_event_buffer_commit()
2864  *   trace_event_raw_event_xxx()
2865  */
2866 # define STACK_SKIP 3
2867 
2868 void trace_buffer_unlock_commit_regs(struct trace_array *tr,
2869 				     struct trace_buffer *buffer,
2870 				     struct ring_buffer_event *event,
2871 				     unsigned int trace_ctx,
2872 				     struct pt_regs *regs)
2873 {
2874 	__buffer_unlock_commit(buffer, event);
2875 
2876 	/*
2877 	 * If regs is not set, then skip the necessary functions.
2878 	 * Note, we can still get here via blktrace, wakeup tracer
2879 	 * and mmiotrace, but that's ok if they lose a function or
2880 	 * two. They are not that meaningful.
2881 	 */
2882 	ftrace_trace_stack(tr, buffer, trace_ctx, regs ? 0 : STACK_SKIP, regs);
2883 	ftrace_trace_userstack(tr, buffer, trace_ctx);
2884 }
2885 
2886 /*
2887  * Similar to trace_buffer_unlock_commit_regs() but do not dump stack.
2888  */
2889 void
2890 trace_buffer_unlock_commit_nostack(struct trace_buffer *buffer,
2891 				   struct ring_buffer_event *event)
2892 {
2893 	__buffer_unlock_commit(buffer, event);
2894 }
2895 
2896 void
2897 trace_function(struct trace_array *tr, unsigned long ip, unsigned long
2898 	       parent_ip, unsigned int trace_ctx)
2899 {
2900 	struct trace_buffer *buffer = tr->array_buffer.buffer;
2901 	struct ring_buffer_event *event;
2902 	struct ftrace_entry *entry;
2903 
2904 	event = __trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
2905 					    trace_ctx);
2906 	if (!event)
2907 		return;
2908 	entry	= ring_buffer_event_data(event);
2909 	entry->ip			= ip;
2910 	entry->parent_ip		= parent_ip;
2911 
2912 	if (static_branch_unlikely(&trace_function_exports_enabled))
2913 		ftrace_exports(event, TRACE_EXPORT_FUNCTION);
2914 	__buffer_unlock_commit(buffer, event);
2915 }
2916 
2917 #ifdef CONFIG_STACKTRACE
2918 
2919 /* Allow 4 levels of nesting: normal, softirq, irq, NMI */
2920 #define FTRACE_KSTACK_NESTING	4
2921 
2922 #define FTRACE_KSTACK_ENTRIES	(SZ_4K / FTRACE_KSTACK_NESTING)
2923 
2924 struct ftrace_stack {
2925 	unsigned long		calls[FTRACE_KSTACK_ENTRIES];
2926 };
2927 
2928 
2929 struct ftrace_stacks {
2930 	struct ftrace_stack	stacks[FTRACE_KSTACK_NESTING];
2931 };
2932 
2933 static DEFINE_PER_CPU(struct ftrace_stacks, ftrace_stacks);
2934 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
2935 
2936 static void __ftrace_trace_stack(struct trace_array *tr,
2937 				 struct trace_buffer *buffer,
2938 				 unsigned int trace_ctx,
2939 				 int skip, struct pt_regs *regs)
2940 {
2941 	struct ring_buffer_event *event;
2942 	unsigned int size, nr_entries;
2943 	struct ftrace_stack *fstack;
2944 	struct stack_entry *entry;
2945 	int stackidx;
2946 
2947 	/*
2948 	 * Add one, for this function and the call to save_stack_trace()
2949 	 * If regs is set, then these functions will not be in the way.
2950 	 */
2951 #ifndef CONFIG_UNWINDER_ORC
2952 	if (!regs)
2953 		skip++;
2954 #endif
2955 
2956 	preempt_disable_notrace();
2957 
2958 	stackidx = __this_cpu_inc_return(ftrace_stack_reserve) - 1;
2959 
2960 	/* This should never happen. If it does, yell once and skip */
2961 	if (WARN_ON_ONCE(stackidx >= FTRACE_KSTACK_NESTING))
2962 		goto out;
2963 
2964 	/*
2965 	 * The above __this_cpu_inc_return() is 'atomic' cpu local. An
2966 	 * interrupt will either see the value pre increment or post
2967 	 * increment. If the interrupt happens pre increment it will have
2968 	 * restored the counter when it returns.  We just need a barrier to
2969 	 * keep gcc from moving things around.
2970 	 */
2971 	barrier();
2972 
2973 	fstack = this_cpu_ptr(ftrace_stacks.stacks) + stackidx;
2974 	size = ARRAY_SIZE(fstack->calls);
2975 
2976 	if (regs) {
2977 		nr_entries = stack_trace_save_regs(regs, fstack->calls,
2978 						   size, skip);
2979 	} else {
2980 		nr_entries = stack_trace_save(fstack->calls, size, skip);
2981 	}
2982 
2983 #ifdef CONFIG_DYNAMIC_FTRACE
2984 	/* Mark entry of stack trace as trampoline code */
2985 	if (tr->ops && tr->ops->trampoline) {
2986 		unsigned long tramp_start = tr->ops->trampoline;
2987 		unsigned long tramp_end = tramp_start + tr->ops->trampoline_size;
2988 		unsigned long *calls = fstack->calls;
2989 
2990 		for (int i = 0; i < nr_entries; i++) {
2991 			if (calls[i] >= tramp_start && calls[i] < tramp_end)
2992 				calls[i] = FTRACE_TRAMPOLINE_MARKER;
2993 		}
2994 	}
2995 #endif
2996 
2997 	event = __trace_buffer_lock_reserve(buffer, TRACE_STACK,
2998 				    struct_size(entry, caller, nr_entries),
2999 				    trace_ctx);
3000 	if (!event)
3001 		goto out;
3002 	entry = ring_buffer_event_data(event);
3003 
3004 	entry->size = nr_entries;
3005 	memcpy(&entry->caller, fstack->calls,
3006 	       flex_array_size(entry, caller, nr_entries));
3007 
3008 	__buffer_unlock_commit(buffer, event);
3009 
3010  out:
3011 	/* Again, don't let gcc optimize things here */
3012 	barrier();
3013 	__this_cpu_dec(ftrace_stack_reserve);
3014 	preempt_enable_notrace();
3015 
3016 }
3017 
3018 static inline void ftrace_trace_stack(struct trace_array *tr,
3019 				      struct trace_buffer *buffer,
3020 				      unsigned int trace_ctx,
3021 				      int skip, struct pt_regs *regs)
3022 {
3023 	if (!(tr->trace_flags & TRACE_ITER_STACKTRACE))
3024 		return;
3025 
3026 	__ftrace_trace_stack(tr, buffer, trace_ctx, skip, regs);
3027 }
3028 
3029 void __trace_stack(struct trace_array *tr, unsigned int trace_ctx,
3030 		   int skip)
3031 {
3032 	struct trace_buffer *buffer = tr->array_buffer.buffer;
3033 
3034 	if (rcu_is_watching()) {
3035 		__ftrace_trace_stack(tr, buffer, trace_ctx, skip, NULL);
3036 		return;
3037 	}
3038 
3039 	if (WARN_ON_ONCE(IS_ENABLED(CONFIG_GENERIC_ENTRY)))
3040 		return;
3041 
3042 	/*
3043 	 * When an NMI triggers, RCU is enabled via ct_nmi_enter(),
3044 	 * but if the above rcu_is_watching() failed, then the NMI
3045 	 * triggered someplace critical, and ct_irq_enter() should
3046 	 * not be called from NMI.
3047 	 */
3048 	if (unlikely(in_nmi()))
3049 		return;
3050 
3051 	ct_irq_enter_irqson();
3052 	__ftrace_trace_stack(tr, buffer, trace_ctx, skip, NULL);
3053 	ct_irq_exit_irqson();
3054 }
3055 
3056 /**
3057  * trace_dump_stack - record a stack back trace in the trace buffer
3058  * @skip: Number of functions to skip (helper handlers)
3059  */
3060 void trace_dump_stack(int skip)
3061 {
3062 	if (tracing_disabled || tracing_selftest_running)
3063 		return;
3064 
3065 #ifndef CONFIG_UNWINDER_ORC
3066 	/* Skip 1 to skip this function. */
3067 	skip++;
3068 #endif
3069 	__ftrace_trace_stack(printk_trace, printk_trace->array_buffer.buffer,
3070 				tracing_gen_ctx(), skip, NULL);
3071 }
3072 EXPORT_SYMBOL_GPL(trace_dump_stack);
3073 
3074 #ifdef CONFIG_USER_STACKTRACE_SUPPORT
3075 static DEFINE_PER_CPU(int, user_stack_count);
3076 
3077 static void
3078 ftrace_trace_userstack(struct trace_array *tr,
3079 		       struct trace_buffer *buffer, unsigned int trace_ctx)
3080 {
3081 	struct ring_buffer_event *event;
3082 	struct userstack_entry *entry;
3083 
3084 	if (!(tr->trace_flags & TRACE_ITER_USERSTACKTRACE))
3085 		return;
3086 
3087 	/*
3088 	 * NMIs can not handle page faults, even with fix ups.
3089 	 * The save user stack can (and often does) fault.
3090 	 */
3091 	if (unlikely(in_nmi()))
3092 		return;
3093 
3094 	/*
3095 	 * prevent recursion, since the user stack tracing may
3096 	 * trigger other kernel events.
3097 	 */
3098 	preempt_disable();
3099 	if (__this_cpu_read(user_stack_count))
3100 		goto out;
3101 
3102 	__this_cpu_inc(user_stack_count);
3103 
3104 	event = __trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
3105 					    sizeof(*entry), trace_ctx);
3106 	if (!event)
3107 		goto out_drop_count;
3108 	entry	= ring_buffer_event_data(event);
3109 
3110 	entry->tgid		= current->tgid;
3111 	memset(&entry->caller, 0, sizeof(entry->caller));
3112 
3113 	stack_trace_save_user(entry->caller, FTRACE_STACK_ENTRIES);
3114 	__buffer_unlock_commit(buffer, event);
3115 
3116  out_drop_count:
3117 	__this_cpu_dec(user_stack_count);
3118  out:
3119 	preempt_enable();
3120 }
3121 #else /* CONFIG_USER_STACKTRACE_SUPPORT */
3122 static void ftrace_trace_userstack(struct trace_array *tr,
3123 				   struct trace_buffer *buffer,
3124 				   unsigned int trace_ctx)
3125 {
3126 }
3127 #endif /* !CONFIG_USER_STACKTRACE_SUPPORT */
3128 
3129 #endif /* CONFIG_STACKTRACE */
3130 
3131 static inline void
3132 func_repeats_set_delta_ts(struct func_repeats_entry *entry,
3133 			  unsigned long long delta)
3134 {
3135 	entry->bottom_delta_ts = delta & U32_MAX;
3136 	entry->top_delta_ts = (delta >> 32);
3137 }
3138 
3139 void trace_last_func_repeats(struct trace_array *tr,
3140 			     struct trace_func_repeats *last_info,
3141 			     unsigned int trace_ctx)
3142 {
3143 	struct trace_buffer *buffer = tr->array_buffer.buffer;
3144 	struct func_repeats_entry *entry;
3145 	struct ring_buffer_event *event;
3146 	u64 delta;
3147 
3148 	event = __trace_buffer_lock_reserve(buffer, TRACE_FUNC_REPEATS,
3149 					    sizeof(*entry), trace_ctx);
3150 	if (!event)
3151 		return;
3152 
3153 	delta = ring_buffer_event_time_stamp(buffer, event) -
3154 		last_info->ts_last_call;
3155 
3156 	entry = ring_buffer_event_data(event);
3157 	entry->ip = last_info->ip;
3158 	entry->parent_ip = last_info->parent_ip;
3159 	entry->count = last_info->count;
3160 	func_repeats_set_delta_ts(entry, delta);
3161 
3162 	__buffer_unlock_commit(buffer, event);
3163 }
3164 
3165 /* created for use with alloc_percpu */
3166 struct trace_buffer_struct {
3167 	int nesting;
3168 	char buffer[4][TRACE_BUF_SIZE];
3169 };
3170 
3171 static struct trace_buffer_struct __percpu *trace_percpu_buffer;
3172 
3173 /*
3174  * This allows for lockless recording.  If we're nested too deeply, then
3175  * this returns NULL.
3176  */
3177 static char *get_trace_buf(void)
3178 {
3179 	struct trace_buffer_struct *buffer = this_cpu_ptr(trace_percpu_buffer);
3180 
3181 	if (!trace_percpu_buffer || buffer->nesting >= 4)
3182 		return NULL;
3183 
3184 	buffer->nesting++;
3185 
3186 	/* Interrupts must see nesting incremented before we use the buffer */
3187 	barrier();
3188 	return &buffer->buffer[buffer->nesting - 1][0];
3189 }
3190 
3191 static void put_trace_buf(void)
3192 {
3193 	/* Don't let the decrement of nesting leak before this */
3194 	barrier();
3195 	this_cpu_dec(trace_percpu_buffer->nesting);
3196 }
3197 
3198 static int alloc_percpu_trace_buffer(void)
3199 {
3200 	struct trace_buffer_struct __percpu *buffers;
3201 
3202 	if (trace_percpu_buffer)
3203 		return 0;
3204 
3205 	buffers = alloc_percpu(struct trace_buffer_struct);
3206 	if (MEM_FAIL(!buffers, "Could not allocate percpu trace_printk buffer"))
3207 		return -ENOMEM;
3208 
3209 	trace_percpu_buffer = buffers;
3210 	return 0;
3211 }
3212 
3213 static int buffers_allocated;
3214 
3215 void trace_printk_init_buffers(void)
3216 {
3217 	if (buffers_allocated)
3218 		return;
3219 
3220 	if (alloc_percpu_trace_buffer())
3221 		return;
3222 
3223 	/* trace_printk() is for debug use only. Don't use it in production. */
3224 
3225 	pr_warn("\n");
3226 	pr_warn("**********************************************************\n");
3227 	pr_warn("**   NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE   **\n");
3228 	pr_warn("**                                                      **\n");
3229 	pr_warn("** trace_printk() being used. Allocating extra memory.  **\n");
3230 	pr_warn("**                                                      **\n");
3231 	pr_warn("** This means that this is a DEBUG kernel and it is     **\n");
3232 	pr_warn("** unsafe for production use.                           **\n");
3233 	pr_warn("**                                                      **\n");
3234 	pr_warn("** If you see this message and you are not debugging    **\n");
3235 	pr_warn("** the kernel, report this immediately to your vendor!  **\n");
3236 	pr_warn("**                                                      **\n");
3237 	pr_warn("**   NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE   **\n");
3238 	pr_warn("**********************************************************\n");
3239 
3240 	/* Expand the buffers to set size */
3241 	tracing_update_buffers(&global_trace);
3242 
3243 	buffers_allocated = 1;
3244 
3245 	/*
3246 	 * trace_printk_init_buffers() can be called by modules.
3247 	 * If that happens, then we need to start cmdline recording
3248 	 * directly here. If the global_trace.buffer is already
3249 	 * allocated here, then this was called by module code.
3250 	 */
3251 	if (global_trace.array_buffer.buffer)
3252 		tracing_start_cmdline_record();
3253 }
3254 EXPORT_SYMBOL_GPL(trace_printk_init_buffers);
3255 
3256 void trace_printk_start_comm(void)
3257 {
3258 	/* Start tracing comms if trace printk is set */
3259 	if (!buffers_allocated)
3260 		return;
3261 	tracing_start_cmdline_record();
3262 }
3263 
3264 static void trace_printk_start_stop_comm(int enabled)
3265 {
3266 	if (!buffers_allocated)
3267 		return;
3268 
3269 	if (enabled)
3270 		tracing_start_cmdline_record();
3271 	else
3272 		tracing_stop_cmdline_record();
3273 }
3274 
3275 /**
3276  * trace_vbprintk - write binary msg to tracing buffer
3277  * @ip:    The address of the caller
3278  * @fmt:   The string format to write to the buffer
3279  * @args:  Arguments for @fmt
3280  */
3281 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
3282 {
3283 	struct ring_buffer_event *event;
3284 	struct trace_buffer *buffer;
3285 	struct trace_array *tr = READ_ONCE(printk_trace);
3286 	struct bprint_entry *entry;
3287 	unsigned int trace_ctx;
3288 	char *tbuffer;
3289 	int len = 0, size;
3290 
3291 	if (!printk_binsafe(tr))
3292 		return trace_vprintk(ip, fmt, args);
3293 
3294 	if (unlikely(tracing_selftest_running || tracing_disabled))
3295 		return 0;
3296 
3297 	/* Don't pollute graph traces with trace_vprintk internals */
3298 	pause_graph_tracing();
3299 
3300 	trace_ctx = tracing_gen_ctx();
3301 	preempt_disable_notrace();
3302 
3303 	tbuffer = get_trace_buf();
3304 	if (!tbuffer) {
3305 		len = 0;
3306 		goto out_nobuffer;
3307 	}
3308 
3309 	len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
3310 
3311 	if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
3312 		goto out_put;
3313 
3314 	size = sizeof(*entry) + sizeof(u32) * len;
3315 	buffer = tr->array_buffer.buffer;
3316 	ring_buffer_nest_start(buffer);
3317 	event = __trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
3318 					    trace_ctx);
3319 	if (!event)
3320 		goto out;
3321 	entry = ring_buffer_event_data(event);
3322 	entry->ip			= ip;
3323 	entry->fmt			= fmt;
3324 
3325 	memcpy(entry->buf, tbuffer, sizeof(u32) * len);
3326 	__buffer_unlock_commit(buffer, event);
3327 	ftrace_trace_stack(tr, buffer, trace_ctx, 6, NULL);
3328 
3329 out:
3330 	ring_buffer_nest_end(buffer);
3331 out_put:
3332 	put_trace_buf();
3333 
3334 out_nobuffer:
3335 	preempt_enable_notrace();
3336 	unpause_graph_tracing();
3337 
3338 	return len;
3339 }
3340 EXPORT_SYMBOL_GPL(trace_vbprintk);
3341 
3342 __printf(3, 0)
3343 static int
3344 __trace_array_vprintk(struct trace_buffer *buffer,
3345 		      unsigned long ip, const char *fmt, va_list args)
3346 {
3347 	struct ring_buffer_event *event;
3348 	int len = 0, size;
3349 	struct print_entry *entry;
3350 	unsigned int trace_ctx;
3351 	char *tbuffer;
3352 
3353 	if (tracing_disabled)
3354 		return 0;
3355 
3356 	/* Don't pollute graph traces with trace_vprintk internals */
3357 	pause_graph_tracing();
3358 
3359 	trace_ctx = tracing_gen_ctx();
3360 	preempt_disable_notrace();
3361 
3362 
3363 	tbuffer = get_trace_buf();
3364 	if (!tbuffer) {
3365 		len = 0;
3366 		goto out_nobuffer;
3367 	}
3368 
3369 	len = vscnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
3370 
3371 	size = sizeof(*entry) + len + 1;
3372 	ring_buffer_nest_start(buffer);
3373 	event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
3374 					    trace_ctx);
3375 	if (!event)
3376 		goto out;
3377 	entry = ring_buffer_event_data(event);
3378 	entry->ip = ip;
3379 
3380 	memcpy(&entry->buf, tbuffer, len + 1);
3381 	__buffer_unlock_commit(buffer, event);
3382 	ftrace_trace_stack(printk_trace, buffer, trace_ctx, 6, NULL);
3383 
3384 out:
3385 	ring_buffer_nest_end(buffer);
3386 	put_trace_buf();
3387 
3388 out_nobuffer:
3389 	preempt_enable_notrace();
3390 	unpause_graph_tracing();
3391 
3392 	return len;
3393 }
3394 
3395 __printf(3, 0)
3396 int trace_array_vprintk(struct trace_array *tr,
3397 			unsigned long ip, const char *fmt, va_list args)
3398 {
3399 	if (tracing_selftest_running && tr == &global_trace)
3400 		return 0;
3401 
3402 	return __trace_array_vprintk(tr->array_buffer.buffer, ip, fmt, args);
3403 }
3404 
3405 /**
3406  * trace_array_printk - Print a message to a specific instance
3407  * @tr: The instance trace_array descriptor
3408  * @ip: The instruction pointer that this is called from.
3409  * @fmt: The format to print (printf format)
3410  *
3411  * If a subsystem sets up its own instance, they have the right to
3412  * printk strings into their tracing instance buffer using this
3413  * function. Note, this function will not write into the top level
3414  * buffer (use trace_printk() for that), as writing into the top level
3415  * buffer should only have events that can be individually disabled.
3416  * trace_printk() is only used for debugging a kernel, and should not
3417  * be ever incorporated in normal use.
3418  *
3419  * trace_array_printk() can be used, as it will not add noise to the
3420  * top level tracing buffer.
3421  *
3422  * Note, trace_array_init_printk() must be called on @tr before this
3423  * can be used.
3424  */
3425 __printf(3, 0)
3426 int trace_array_printk(struct trace_array *tr,
3427 		       unsigned long ip, const char *fmt, ...)
3428 {
3429 	int ret;
3430 	va_list ap;
3431 
3432 	if (!tr)
3433 		return -ENOENT;
3434 
3435 	/* This is only allowed for created instances */
3436 	if (tr == &global_trace)
3437 		return 0;
3438 
3439 	if (!(tr->trace_flags & TRACE_ITER_PRINTK))
3440 		return 0;
3441 
3442 	va_start(ap, fmt);
3443 	ret = trace_array_vprintk(tr, ip, fmt, ap);
3444 	va_end(ap);
3445 	return ret;
3446 }
3447 EXPORT_SYMBOL_GPL(trace_array_printk);
3448 
3449 /**
3450  * trace_array_init_printk - Initialize buffers for trace_array_printk()
3451  * @tr: The trace array to initialize the buffers for
3452  *
3453  * As trace_array_printk() only writes into instances, they are OK to
3454  * have in the kernel (unlike trace_printk()). This needs to be called
3455  * before trace_array_printk() can be used on a trace_array.
3456  */
3457 int trace_array_init_printk(struct trace_array *tr)
3458 {
3459 	if (!tr)
3460 		return -ENOENT;
3461 
3462 	/* This is only allowed for created instances */
3463 	if (tr == &global_trace)
3464 		return -EINVAL;
3465 
3466 	return alloc_percpu_trace_buffer();
3467 }
3468 EXPORT_SYMBOL_GPL(trace_array_init_printk);
3469 
3470 __printf(3, 4)
3471 int trace_array_printk_buf(struct trace_buffer *buffer,
3472 			   unsigned long ip, const char *fmt, ...)
3473 {
3474 	int ret;
3475 	va_list ap;
3476 
3477 	if (!(printk_trace->trace_flags & TRACE_ITER_PRINTK))
3478 		return 0;
3479 
3480 	va_start(ap, fmt);
3481 	ret = __trace_array_vprintk(buffer, ip, fmt, ap);
3482 	va_end(ap);
3483 	return ret;
3484 }
3485 
3486 __printf(2, 0)
3487 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
3488 {
3489 	return trace_array_vprintk(printk_trace, ip, fmt, args);
3490 }
3491 EXPORT_SYMBOL_GPL(trace_vprintk);
3492 
3493 static void trace_iterator_increment(struct trace_iterator *iter)
3494 {
3495 	struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
3496 
3497 	iter->idx++;
3498 	if (buf_iter)
3499 		ring_buffer_iter_advance(buf_iter);
3500 }
3501 
3502 static struct trace_entry *
3503 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
3504 		unsigned long *lost_events)
3505 {
3506 	struct ring_buffer_event *event;
3507 	struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
3508 
3509 	if (buf_iter) {
3510 		event = ring_buffer_iter_peek(buf_iter, ts);
3511 		if (lost_events)
3512 			*lost_events = ring_buffer_iter_dropped(buf_iter) ?
3513 				(unsigned long)-1 : 0;
3514 	} else {
3515 		event = ring_buffer_peek(iter->array_buffer->buffer, cpu, ts,
3516 					 lost_events);
3517 	}
3518 
3519 	if (event) {
3520 		iter->ent_size = ring_buffer_event_length(event);
3521 		return ring_buffer_event_data(event);
3522 	}
3523 	iter->ent_size = 0;
3524 	return NULL;
3525 }
3526 
3527 static struct trace_entry *
3528 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
3529 		  unsigned long *missing_events, u64 *ent_ts)
3530 {
3531 	struct trace_buffer *buffer = iter->array_buffer->buffer;
3532 	struct trace_entry *ent, *next = NULL;
3533 	unsigned long lost_events = 0, next_lost = 0;
3534 	int cpu_file = iter->cpu_file;
3535 	u64 next_ts = 0, ts;
3536 	int next_cpu = -1;
3537 	int next_size = 0;
3538 	int cpu;
3539 
3540 	/*
3541 	 * If we are in a per_cpu trace file, don't bother by iterating over
3542 	 * all cpu and peek directly.
3543 	 */
3544 	if (cpu_file > RING_BUFFER_ALL_CPUS) {
3545 		if (ring_buffer_empty_cpu(buffer, cpu_file))
3546 			return NULL;
3547 		ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
3548 		if (ent_cpu)
3549 			*ent_cpu = cpu_file;
3550 
3551 		return ent;
3552 	}
3553 
3554 	for_each_tracing_cpu(cpu) {
3555 
3556 		if (ring_buffer_empty_cpu(buffer, cpu))
3557 			continue;
3558 
3559 		ent = peek_next_entry(iter, cpu, &ts, &lost_events);
3560 
3561 		/*
3562 		 * Pick the entry with the smallest timestamp:
3563 		 */
3564 		if (ent && (!next || ts < next_ts)) {
3565 			next = ent;
3566 			next_cpu = cpu;
3567 			next_ts = ts;
3568 			next_lost = lost_events;
3569 			next_size = iter->ent_size;
3570 		}
3571 	}
3572 
3573 	iter->ent_size = next_size;
3574 
3575 	if (ent_cpu)
3576 		*ent_cpu = next_cpu;
3577 
3578 	if (ent_ts)
3579 		*ent_ts = next_ts;
3580 
3581 	if (missing_events)
3582 		*missing_events = next_lost;
3583 
3584 	return next;
3585 }
3586 
3587 #define STATIC_FMT_BUF_SIZE	128
3588 static char static_fmt_buf[STATIC_FMT_BUF_SIZE];
3589 
3590 char *trace_iter_expand_format(struct trace_iterator *iter)
3591 {
3592 	char *tmp;
3593 
3594 	/*
3595 	 * iter->tr is NULL when used with tp_printk, which makes
3596 	 * this get called where it is not safe to call krealloc().
3597 	 */
3598 	if (!iter->tr || iter->fmt == static_fmt_buf)
3599 		return NULL;
3600 
3601 	tmp = krealloc(iter->fmt, iter->fmt_size + STATIC_FMT_BUF_SIZE,
3602 		       GFP_KERNEL);
3603 	if (tmp) {
3604 		iter->fmt_size += STATIC_FMT_BUF_SIZE;
3605 		iter->fmt = tmp;
3606 	}
3607 
3608 	return tmp;
3609 }
3610 
3611 /* Returns true if the string is safe to dereference from an event */
3612 static bool trace_safe_str(struct trace_iterator *iter, const char *str,
3613 			   bool star, int len)
3614 {
3615 	unsigned long addr = (unsigned long)str;
3616 	struct trace_event *trace_event;
3617 	struct trace_event_call *event;
3618 
3619 	/* Ignore strings with no length */
3620 	if (star && !len)
3621 		return true;
3622 
3623 	/* OK if part of the event data */
3624 	if ((addr >= (unsigned long)iter->ent) &&
3625 	    (addr < (unsigned long)iter->ent + iter->ent_size))
3626 		return true;
3627 
3628 	/* OK if part of the temp seq buffer */
3629 	if ((addr >= (unsigned long)iter->tmp_seq.buffer) &&
3630 	    (addr < (unsigned long)iter->tmp_seq.buffer + TRACE_SEQ_BUFFER_SIZE))
3631 		return true;
3632 
3633 	/* Core rodata can not be freed */
3634 	if (is_kernel_rodata(addr))
3635 		return true;
3636 
3637 	if (trace_is_tracepoint_string(str))
3638 		return true;
3639 
3640 	/*
3641 	 * Now this could be a module event, referencing core module
3642 	 * data, which is OK.
3643 	 */
3644 	if (!iter->ent)
3645 		return false;
3646 
3647 	trace_event = ftrace_find_event(iter->ent->type);
3648 	if (!trace_event)
3649 		return false;
3650 
3651 	event = container_of(trace_event, struct trace_event_call, event);
3652 	if ((event->flags & TRACE_EVENT_FL_DYNAMIC) || !event->module)
3653 		return false;
3654 
3655 	/* Would rather have rodata, but this will suffice */
3656 	if (within_module_core(addr, event->module))
3657 		return true;
3658 
3659 	return false;
3660 }
3661 
3662 static DEFINE_STATIC_KEY_FALSE(trace_no_verify);
3663 
3664 static int test_can_verify_check(const char *fmt, ...)
3665 {
3666 	char buf[16];
3667 	va_list ap;
3668 	int ret;
3669 
3670 	/*
3671 	 * The verifier is dependent on vsnprintf() modifies the va_list
3672 	 * passed to it, where it is sent as a reference. Some architectures
3673 	 * (like x86_32) passes it by value, which means that vsnprintf()
3674 	 * does not modify the va_list passed to it, and the verifier
3675 	 * would then need to be able to understand all the values that
3676 	 * vsnprintf can use. If it is passed by value, then the verifier
3677 	 * is disabled.
3678 	 */
3679 	va_start(ap, fmt);
3680 	vsnprintf(buf, 16, "%d", ap);
3681 	ret = va_arg(ap, int);
3682 	va_end(ap);
3683 
3684 	return ret;
3685 }
3686 
3687 static void test_can_verify(void)
3688 {
3689 	if (!test_can_verify_check("%d %d", 0, 1)) {
3690 		pr_info("trace event string verifier disabled\n");
3691 		static_branch_inc(&trace_no_verify);
3692 	}
3693 }
3694 
3695 /**
3696  * trace_check_vprintf - Check dereferenced strings while writing to the seq buffer
3697  * @iter: The iterator that holds the seq buffer and the event being printed
3698  * @fmt: The format used to print the event
3699  * @ap: The va_list holding the data to print from @fmt.
3700  *
3701  * This writes the data into the @iter->seq buffer using the data from
3702  * @fmt and @ap. If the format has a %s, then the source of the string
3703  * is examined to make sure it is safe to print, otherwise it will
3704  * warn and print "[UNSAFE MEMORY]" in place of the dereferenced string
3705  * pointer.
3706  */
3707 void trace_check_vprintf(struct trace_iterator *iter, const char *fmt,
3708 			 va_list ap)
3709 {
3710 	long text_delta = 0;
3711 	long data_delta = 0;
3712 	const char *p = fmt;
3713 	const char *str;
3714 	bool good;
3715 	int i, j;
3716 
3717 	if (WARN_ON_ONCE(!fmt))
3718 		return;
3719 
3720 	if (static_branch_unlikely(&trace_no_verify))
3721 		goto print;
3722 
3723 	/*
3724 	 * When the kernel is booted with the tp_printk command line
3725 	 * parameter, trace events go directly through to printk().
3726 	 * It also is checked by this function, but it does not
3727 	 * have an associated trace_array (tr) for it.
3728 	 */
3729 	if (iter->tr) {
3730 		text_delta = iter->tr->text_delta;
3731 		data_delta = iter->tr->data_delta;
3732 	}
3733 
3734 	/* Don't bother checking when doing a ftrace_dump() */
3735 	if (iter->fmt == static_fmt_buf)
3736 		goto print;
3737 
3738 	while (*p) {
3739 		bool star = false;
3740 		int len = 0;
3741 
3742 		j = 0;
3743 
3744 		/*
3745 		 * We only care about %s and variants
3746 		 * as well as %p[sS] if delta is non-zero
3747 		 */
3748 		for (i = 0; p[i]; i++) {
3749 			if (i + 1 >= iter->fmt_size) {
3750 				/*
3751 				 * If we can't expand the copy buffer,
3752 				 * just print it.
3753 				 */
3754 				if (!trace_iter_expand_format(iter))
3755 					goto print;
3756 			}
3757 
3758 			if (p[i] == '\\' && p[i+1]) {
3759 				i++;
3760 				continue;
3761 			}
3762 			if (p[i] == '%') {
3763 				/* Need to test cases like %08.*s */
3764 				for (j = 1; p[i+j]; j++) {
3765 					if (isdigit(p[i+j]) ||
3766 					    p[i+j] == '.')
3767 						continue;
3768 					if (p[i+j] == '*') {
3769 						star = true;
3770 						continue;
3771 					}
3772 					break;
3773 				}
3774 				if (p[i+j] == 's')
3775 					break;
3776 
3777 				if (text_delta && p[i+1] == 'p' &&
3778 				    ((p[i+2] == 's' || p[i+2] == 'S')))
3779 					break;
3780 
3781 				star = false;
3782 			}
3783 			j = 0;
3784 		}
3785 		/* If no %s found then just print normally */
3786 		if (!p[i])
3787 			break;
3788 
3789 		/* Copy up to the %s, and print that */
3790 		strncpy(iter->fmt, p, i);
3791 		iter->fmt[i] = '\0';
3792 		trace_seq_vprintf(&iter->seq, iter->fmt, ap);
3793 
3794 		/* Add delta to %pS pointers */
3795 		if (p[i+1] == 'p') {
3796 			unsigned long addr;
3797 			char fmt[4];
3798 
3799 			fmt[0] = '%';
3800 			fmt[1] = 'p';
3801 			fmt[2] = p[i+2]; /* Either %ps or %pS */
3802 			fmt[3] = '\0';
3803 
3804 			addr = va_arg(ap, unsigned long);
3805 			addr += text_delta;
3806 			trace_seq_printf(&iter->seq, fmt, (void *)addr);
3807 
3808 			p += i + 3;
3809 			continue;
3810 		}
3811 
3812 		/*
3813 		 * If iter->seq is full, the above call no longer guarantees
3814 		 * that ap is in sync with fmt processing, and further calls
3815 		 * to va_arg() can return wrong positional arguments.
3816 		 *
3817 		 * Ensure that ap is no longer used in this case.
3818 		 */
3819 		if (iter->seq.full) {
3820 			p = "";
3821 			break;
3822 		}
3823 
3824 		if (star)
3825 			len = va_arg(ap, int);
3826 
3827 		/* The ap now points to the string data of the %s */
3828 		str = va_arg(ap, const char *);
3829 
3830 		good = trace_safe_str(iter, str, star, len);
3831 
3832 		/* Could be from the last boot */
3833 		if (data_delta && !good) {
3834 			str += data_delta;
3835 			good = trace_safe_str(iter, str, star, len);
3836 		}
3837 
3838 		/*
3839 		 * If you hit this warning, it is likely that the
3840 		 * trace event in question used %s on a string that
3841 		 * was saved at the time of the event, but may not be
3842 		 * around when the trace is read. Use __string(),
3843 		 * __assign_str() and __get_str() helpers in the TRACE_EVENT()
3844 		 * instead. See samples/trace_events/trace-events-sample.h
3845 		 * for reference.
3846 		 */
3847 		if (WARN_ONCE(!good, "fmt: '%s' current_buffer: '%s'",
3848 			      fmt, seq_buf_str(&iter->seq.seq))) {
3849 			int ret;
3850 
3851 			/* Try to safely read the string */
3852 			if (star) {
3853 				if (len + 1 > iter->fmt_size)
3854 					len = iter->fmt_size - 1;
3855 				if (len < 0)
3856 					len = 0;
3857 				ret = copy_from_kernel_nofault(iter->fmt, str, len);
3858 				iter->fmt[len] = 0;
3859 				star = false;
3860 			} else {
3861 				ret = strncpy_from_kernel_nofault(iter->fmt, str,
3862 								  iter->fmt_size);
3863 			}
3864 			if (ret < 0)
3865 				trace_seq_printf(&iter->seq, "(0x%px)", str);
3866 			else
3867 				trace_seq_printf(&iter->seq, "(0x%px:%s)",
3868 						 str, iter->fmt);
3869 			str = "[UNSAFE-MEMORY]";
3870 			strcpy(iter->fmt, "%s");
3871 		} else {
3872 			strncpy(iter->fmt, p + i, j + 1);
3873 			iter->fmt[j+1] = '\0';
3874 		}
3875 		if (star)
3876 			trace_seq_printf(&iter->seq, iter->fmt, len, str);
3877 		else
3878 			trace_seq_printf(&iter->seq, iter->fmt, str);
3879 
3880 		p += i + j + 1;
3881 	}
3882  print:
3883 	if (*p)
3884 		trace_seq_vprintf(&iter->seq, p, ap);
3885 }
3886 
3887 const char *trace_event_format(struct trace_iterator *iter, const char *fmt)
3888 {
3889 	const char *p, *new_fmt;
3890 	char *q;
3891 
3892 	if (WARN_ON_ONCE(!fmt))
3893 		return fmt;
3894 
3895 	if (!iter->tr || iter->tr->trace_flags & TRACE_ITER_HASH_PTR)
3896 		return fmt;
3897 
3898 	p = fmt;
3899 	new_fmt = q = iter->fmt;
3900 	while (*p) {
3901 		if (unlikely(q - new_fmt + 3 > iter->fmt_size)) {
3902 			if (!trace_iter_expand_format(iter))
3903 				return fmt;
3904 
3905 			q += iter->fmt - new_fmt;
3906 			new_fmt = iter->fmt;
3907 		}
3908 
3909 		*q++ = *p++;
3910 
3911 		/* Replace %p with %px */
3912 		if (p[-1] == '%') {
3913 			if (p[0] == '%') {
3914 				*q++ = *p++;
3915 			} else if (p[0] == 'p' && !isalnum(p[1])) {
3916 				*q++ = *p++;
3917 				*q++ = 'x';
3918 			}
3919 		}
3920 	}
3921 	*q = '\0';
3922 
3923 	return new_fmt;
3924 }
3925 
3926 #define STATIC_TEMP_BUF_SIZE	128
3927 static char static_temp_buf[STATIC_TEMP_BUF_SIZE] __aligned(4);
3928 
3929 /* Find the next real entry, without updating the iterator itself */
3930 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
3931 					  int *ent_cpu, u64 *ent_ts)
3932 {
3933 	/* __find_next_entry will reset ent_size */
3934 	int ent_size = iter->ent_size;
3935 	struct trace_entry *entry;
3936 
3937 	/*
3938 	 * If called from ftrace_dump(), then the iter->temp buffer
3939 	 * will be the static_temp_buf and not created from kmalloc.
3940 	 * If the entry size is greater than the buffer, we can
3941 	 * not save it. Just return NULL in that case. This is only
3942 	 * used to add markers when two consecutive events' time
3943 	 * stamps have a large delta. See trace_print_lat_context()
3944 	 */
3945 	if (iter->temp == static_temp_buf &&
3946 	    STATIC_TEMP_BUF_SIZE < ent_size)
3947 		return NULL;
3948 
3949 	/*
3950 	 * The __find_next_entry() may call peek_next_entry(), which may
3951 	 * call ring_buffer_peek() that may make the contents of iter->ent
3952 	 * undefined. Need to copy iter->ent now.
3953 	 */
3954 	if (iter->ent && iter->ent != iter->temp) {
3955 		if ((!iter->temp || iter->temp_size < iter->ent_size) &&
3956 		    !WARN_ON_ONCE(iter->temp == static_temp_buf)) {
3957 			void *temp;
3958 			temp = kmalloc(iter->ent_size, GFP_KERNEL);
3959 			if (!temp)
3960 				return NULL;
3961 			kfree(iter->temp);
3962 			iter->temp = temp;
3963 			iter->temp_size = iter->ent_size;
3964 		}
3965 		memcpy(iter->temp, iter->ent, iter->ent_size);
3966 		iter->ent = iter->temp;
3967 	}
3968 	entry = __find_next_entry(iter, ent_cpu, NULL, ent_ts);
3969 	/* Put back the original ent_size */
3970 	iter->ent_size = ent_size;
3971 
3972 	return entry;
3973 }
3974 
3975 /* Find the next real entry, and increment the iterator to the next entry */
3976 void *trace_find_next_entry_inc(struct trace_iterator *iter)
3977 {
3978 	iter->ent = __find_next_entry(iter, &iter->cpu,
3979 				      &iter->lost_events, &iter->ts);
3980 
3981 	if (iter->ent)
3982 		trace_iterator_increment(iter);
3983 
3984 	return iter->ent ? iter : NULL;
3985 }
3986 
3987 static void trace_consume(struct trace_iterator *iter)
3988 {
3989 	ring_buffer_consume(iter->array_buffer->buffer, iter->cpu, &iter->ts,
3990 			    &iter->lost_events);
3991 }
3992 
3993 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
3994 {
3995 	struct trace_iterator *iter = m->private;
3996 	int i = (int)*pos;
3997 	void *ent;
3998 
3999 	WARN_ON_ONCE(iter->leftover);
4000 
4001 	(*pos)++;
4002 
4003 	/* can't go backwards */
4004 	if (iter->idx > i)
4005 		return NULL;
4006 
4007 	if (iter->idx < 0)
4008 		ent = trace_find_next_entry_inc(iter);
4009 	else
4010 		ent = iter;
4011 
4012 	while (ent && iter->idx < i)
4013 		ent = trace_find_next_entry_inc(iter);
4014 
4015 	iter->pos = *pos;
4016 
4017 	return ent;
4018 }
4019 
4020 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
4021 {
4022 	struct ring_buffer_iter *buf_iter;
4023 	unsigned long entries = 0;
4024 	u64 ts;
4025 
4026 	per_cpu_ptr(iter->array_buffer->data, cpu)->skipped_entries = 0;
4027 
4028 	buf_iter = trace_buffer_iter(iter, cpu);
4029 	if (!buf_iter)
4030 		return;
4031 
4032 	ring_buffer_iter_reset(buf_iter);
4033 
4034 	/*
4035 	 * We could have the case with the max latency tracers
4036 	 * that a reset never took place on a cpu. This is evident
4037 	 * by the timestamp being before the start of the buffer.
4038 	 */
4039 	while (ring_buffer_iter_peek(buf_iter, &ts)) {
4040 		if (ts >= iter->array_buffer->time_start)
4041 			break;
4042 		entries++;
4043 		ring_buffer_iter_advance(buf_iter);
4044 		/* This could be a big loop */
4045 		cond_resched();
4046 	}
4047 
4048 	per_cpu_ptr(iter->array_buffer->data, cpu)->skipped_entries = entries;
4049 }
4050 
4051 /*
4052  * The current tracer is copied to avoid a global locking
4053  * all around.
4054  */
4055 static void *s_start(struct seq_file *m, loff_t *pos)
4056 {
4057 	struct trace_iterator *iter = m->private;
4058 	struct trace_array *tr = iter->tr;
4059 	int cpu_file = iter->cpu_file;
4060 	void *p = NULL;
4061 	loff_t l = 0;
4062 	int cpu;
4063 
4064 	mutex_lock(&trace_types_lock);
4065 	if (unlikely(tr->current_trace != iter->trace)) {
4066 		/* Close iter->trace before switching to the new current tracer */
4067 		if (iter->trace->close)
4068 			iter->trace->close(iter);
4069 		iter->trace = tr->current_trace;
4070 		/* Reopen the new current tracer */
4071 		if (iter->trace->open)
4072 			iter->trace->open(iter);
4073 	}
4074 	mutex_unlock(&trace_types_lock);
4075 
4076 #ifdef CONFIG_TRACER_MAX_TRACE
4077 	if (iter->snapshot && iter->trace->use_max_tr)
4078 		return ERR_PTR(-EBUSY);
4079 #endif
4080 
4081 	if (*pos != iter->pos) {
4082 		iter->ent = NULL;
4083 		iter->cpu = 0;
4084 		iter->idx = -1;
4085 
4086 		if (cpu_file == RING_BUFFER_ALL_CPUS) {
4087 			for_each_tracing_cpu(cpu)
4088 				tracing_iter_reset(iter, cpu);
4089 		} else
4090 			tracing_iter_reset(iter, cpu_file);
4091 
4092 		iter->leftover = 0;
4093 		for (p = iter; p && l < *pos; p = s_next(m, p, &l))
4094 			;
4095 
4096 	} else {
4097 		/*
4098 		 * If we overflowed the seq_file before, then we want
4099 		 * to just reuse the trace_seq buffer again.
4100 		 */
4101 		if (iter->leftover)
4102 			p = iter;
4103 		else {
4104 			l = *pos - 1;
4105 			p = s_next(m, p, &l);
4106 		}
4107 	}
4108 
4109 	trace_event_read_lock();
4110 	trace_access_lock(cpu_file);
4111 	return p;
4112 }
4113 
4114 static void s_stop(struct seq_file *m, void *p)
4115 {
4116 	struct trace_iterator *iter = m->private;
4117 
4118 #ifdef CONFIG_TRACER_MAX_TRACE
4119 	if (iter->snapshot && iter->trace->use_max_tr)
4120 		return;
4121 #endif
4122 
4123 	trace_access_unlock(iter->cpu_file);
4124 	trace_event_read_unlock();
4125 }
4126 
4127 static void
4128 get_total_entries_cpu(struct array_buffer *buf, unsigned long *total,
4129 		      unsigned long *entries, int cpu)
4130 {
4131 	unsigned long count;
4132 
4133 	count = ring_buffer_entries_cpu(buf->buffer, cpu);
4134 	/*
4135 	 * If this buffer has skipped entries, then we hold all
4136 	 * entries for the trace and we need to ignore the
4137 	 * ones before the time stamp.
4138 	 */
4139 	if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
4140 		count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
4141 		/* total is the same as the entries */
4142 		*total = count;
4143 	} else
4144 		*total = count +
4145 			ring_buffer_overrun_cpu(buf->buffer, cpu);
4146 	*entries = count;
4147 }
4148 
4149 static void
4150 get_total_entries(struct array_buffer *buf,
4151 		  unsigned long *total, unsigned long *entries)
4152 {
4153 	unsigned long t, e;
4154 	int cpu;
4155 
4156 	*total = 0;
4157 	*entries = 0;
4158 
4159 	for_each_tracing_cpu(cpu) {
4160 		get_total_entries_cpu(buf, &t, &e, cpu);
4161 		*total += t;
4162 		*entries += e;
4163 	}
4164 }
4165 
4166 unsigned long trace_total_entries_cpu(struct trace_array *tr, int cpu)
4167 {
4168 	unsigned long total, entries;
4169 
4170 	if (!tr)
4171 		tr = &global_trace;
4172 
4173 	get_total_entries_cpu(&tr->array_buffer, &total, &entries, cpu);
4174 
4175 	return entries;
4176 }
4177 
4178 unsigned long trace_total_entries(struct trace_array *tr)
4179 {
4180 	unsigned long total, entries;
4181 
4182 	if (!tr)
4183 		tr = &global_trace;
4184 
4185 	get_total_entries(&tr->array_buffer, &total, &entries);
4186 
4187 	return entries;
4188 }
4189 
4190 static void print_lat_help_header(struct seq_file *m)
4191 {
4192 	seq_puts(m, "#                    _------=> CPU#            \n"
4193 		    "#                   / _-----=> irqs-off/BH-disabled\n"
4194 		    "#                  | / _----=> need-resched    \n"
4195 		    "#                  || / _---=> hardirq/softirq \n"
4196 		    "#                  ||| / _--=> preempt-depth   \n"
4197 		    "#                  |||| / _-=> migrate-disable \n"
4198 		    "#                  ||||| /     delay           \n"
4199 		    "#  cmd     pid     |||||| time  |   caller     \n"
4200 		    "#     \\   /        ||||||  \\    |    /       \n");
4201 }
4202 
4203 static void print_event_info(struct array_buffer *buf, struct seq_file *m)
4204 {
4205 	unsigned long total;
4206 	unsigned long entries;
4207 
4208 	get_total_entries(buf, &total, &entries);
4209 	seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu   #P:%d\n",
4210 		   entries, total, num_online_cpus());
4211 	seq_puts(m, "#\n");
4212 }
4213 
4214 static void print_func_help_header(struct array_buffer *buf, struct seq_file *m,
4215 				   unsigned int flags)
4216 {
4217 	bool tgid = flags & TRACE_ITER_RECORD_TGID;
4218 
4219 	print_event_info(buf, m);
4220 
4221 	seq_printf(m, "#           TASK-PID    %s CPU#     TIMESTAMP  FUNCTION\n", tgid ? "   TGID   " : "");
4222 	seq_printf(m, "#              | |      %s   |         |         |\n",      tgid ? "     |    " : "");
4223 }
4224 
4225 static void print_func_help_header_irq(struct array_buffer *buf, struct seq_file *m,
4226 				       unsigned int flags)
4227 {
4228 	bool tgid = flags & TRACE_ITER_RECORD_TGID;
4229 	static const char space[] = "            ";
4230 	int prec = tgid ? 12 : 2;
4231 
4232 	print_event_info(buf, m);
4233 
4234 	seq_printf(m, "#                            %.*s  _-----=> irqs-off/BH-disabled\n", prec, space);
4235 	seq_printf(m, "#                            %.*s / _----=> need-resched\n", prec, space);
4236 	seq_printf(m, "#                            %.*s| / _---=> hardirq/softirq\n", prec, space);
4237 	seq_printf(m, "#                            %.*s|| / _--=> preempt-depth\n", prec, space);
4238 	seq_printf(m, "#                            %.*s||| / _-=> migrate-disable\n", prec, space);
4239 	seq_printf(m, "#                            %.*s|||| /     delay\n", prec, space);
4240 	seq_printf(m, "#           TASK-PID  %.*s CPU#  |||||  TIMESTAMP  FUNCTION\n", prec, "     TGID   ");
4241 	seq_printf(m, "#              | |    %.*s   |   |||||     |         |\n", prec, "       |    ");
4242 }
4243 
4244 void
4245 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
4246 {
4247 	unsigned long sym_flags = (global_trace.trace_flags & TRACE_ITER_SYM_MASK);
4248 	struct array_buffer *buf = iter->array_buffer;
4249 	struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
4250 	struct tracer *type = iter->trace;
4251 	unsigned long entries;
4252 	unsigned long total;
4253 	const char *name = type->name;
4254 
4255 	get_total_entries(buf, &total, &entries);
4256 
4257 	seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
4258 		   name, init_utsname()->release);
4259 	seq_puts(m, "# -----------------------------------"
4260 		 "---------------------------------\n");
4261 	seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
4262 		   " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
4263 		   nsecs_to_usecs(data->saved_latency),
4264 		   entries,
4265 		   total,
4266 		   buf->cpu,
4267 		   preempt_model_none()      ? "server" :
4268 		   preempt_model_voluntary() ? "desktop" :
4269 		   preempt_model_full()      ? "preempt" :
4270 		   preempt_model_rt()        ? "preempt_rt" :
4271 		   "unknown",
4272 		   /* These are reserved for later use */
4273 		   0, 0, 0, 0);
4274 #ifdef CONFIG_SMP
4275 	seq_printf(m, " #P:%d)\n", num_online_cpus());
4276 #else
4277 	seq_puts(m, ")\n");
4278 #endif
4279 	seq_puts(m, "#    -----------------\n");
4280 	seq_printf(m, "#    | task: %.16s-%d "
4281 		   "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
4282 		   data->comm, data->pid,
4283 		   from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
4284 		   data->policy, data->rt_priority);
4285 	seq_puts(m, "#    -----------------\n");
4286 
4287 	if (data->critical_start) {
4288 		seq_puts(m, "#  => started at: ");
4289 		seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
4290 		trace_print_seq(m, &iter->seq);
4291 		seq_puts(m, "\n#  => ended at:   ");
4292 		seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
4293 		trace_print_seq(m, &iter->seq);
4294 		seq_puts(m, "\n#\n");
4295 	}
4296 
4297 	seq_puts(m, "#\n");
4298 }
4299 
4300 static void test_cpu_buff_start(struct trace_iterator *iter)
4301 {
4302 	struct trace_seq *s = &iter->seq;
4303 	struct trace_array *tr = iter->tr;
4304 
4305 	if (!(tr->trace_flags & TRACE_ITER_ANNOTATE))
4306 		return;
4307 
4308 	if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
4309 		return;
4310 
4311 	if (cpumask_available(iter->started) &&
4312 	    cpumask_test_cpu(iter->cpu, iter->started))
4313 		return;
4314 
4315 	if (per_cpu_ptr(iter->array_buffer->data, iter->cpu)->skipped_entries)
4316 		return;
4317 
4318 	if (cpumask_available(iter->started))
4319 		cpumask_set_cpu(iter->cpu, iter->started);
4320 
4321 	/* Don't print started cpu buffer for the first entry of the trace */
4322 	if (iter->idx > 1)
4323 		trace_seq_printf(s, "##### CPU %u buffer started ####\n",
4324 				iter->cpu);
4325 }
4326 
4327 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
4328 {
4329 	struct trace_array *tr = iter->tr;
4330 	struct trace_seq *s = &iter->seq;
4331 	unsigned long sym_flags = (tr->trace_flags & TRACE_ITER_SYM_MASK);
4332 	struct trace_entry *entry;
4333 	struct trace_event *event;
4334 
4335 	entry = iter->ent;
4336 
4337 	test_cpu_buff_start(iter);
4338 
4339 	event = ftrace_find_event(entry->type);
4340 
4341 	if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
4342 		if (iter->iter_flags & TRACE_FILE_LAT_FMT)
4343 			trace_print_lat_context(iter);
4344 		else
4345 			trace_print_context(iter);
4346 	}
4347 
4348 	if (trace_seq_has_overflowed(s))
4349 		return TRACE_TYPE_PARTIAL_LINE;
4350 
4351 	if (event) {
4352 		if (tr->trace_flags & TRACE_ITER_FIELDS)
4353 			return print_event_fields(iter, event);
4354 		return event->funcs->trace(iter, sym_flags, event);
4355 	}
4356 
4357 	trace_seq_printf(s, "Unknown type %d\n", entry->type);
4358 
4359 	return trace_handle_return(s);
4360 }
4361 
4362 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
4363 {
4364 	struct trace_array *tr = iter->tr;
4365 	struct trace_seq *s = &iter->seq;
4366 	struct trace_entry *entry;
4367 	struct trace_event *event;
4368 
4369 	entry = iter->ent;
4370 
4371 	if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO)
4372 		trace_seq_printf(s, "%d %d %llu ",
4373 				 entry->pid, iter->cpu, iter->ts);
4374 
4375 	if (trace_seq_has_overflowed(s))
4376 		return TRACE_TYPE_PARTIAL_LINE;
4377 
4378 	event = ftrace_find_event(entry->type);
4379 	if (event)
4380 		return event->funcs->raw(iter, 0, event);
4381 
4382 	trace_seq_printf(s, "%d ?\n", entry->type);
4383 
4384 	return trace_handle_return(s);
4385 }
4386 
4387 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
4388 {
4389 	struct trace_array *tr = iter->tr;
4390 	struct trace_seq *s = &iter->seq;
4391 	unsigned char newline = '\n';
4392 	struct trace_entry *entry;
4393 	struct trace_event *event;
4394 
4395 	entry = iter->ent;
4396 
4397 	if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
4398 		SEQ_PUT_HEX_FIELD(s, entry->pid);
4399 		SEQ_PUT_HEX_FIELD(s, iter->cpu);
4400 		SEQ_PUT_HEX_FIELD(s, iter->ts);
4401 		if (trace_seq_has_overflowed(s))
4402 			return TRACE_TYPE_PARTIAL_LINE;
4403 	}
4404 
4405 	event = ftrace_find_event(entry->type);
4406 	if (event) {
4407 		enum print_line_t ret = event->funcs->hex(iter, 0, event);
4408 		if (ret != TRACE_TYPE_HANDLED)
4409 			return ret;
4410 	}
4411 
4412 	SEQ_PUT_FIELD(s, newline);
4413 
4414 	return trace_handle_return(s);
4415 }
4416 
4417 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
4418 {
4419 	struct trace_array *tr = iter->tr;
4420 	struct trace_seq *s = &iter->seq;
4421 	struct trace_entry *entry;
4422 	struct trace_event *event;
4423 
4424 	entry = iter->ent;
4425 
4426 	if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
4427 		SEQ_PUT_FIELD(s, entry->pid);
4428 		SEQ_PUT_FIELD(s, iter->cpu);
4429 		SEQ_PUT_FIELD(s, iter->ts);
4430 		if (trace_seq_has_overflowed(s))
4431 			return TRACE_TYPE_PARTIAL_LINE;
4432 	}
4433 
4434 	event = ftrace_find_event(entry->type);
4435 	return event ? event->funcs->binary(iter, 0, event) :
4436 		TRACE_TYPE_HANDLED;
4437 }
4438 
4439 int trace_empty(struct trace_iterator *iter)
4440 {
4441 	struct ring_buffer_iter *buf_iter;
4442 	int cpu;
4443 
4444 	/* If we are looking at one CPU buffer, only check that one */
4445 	if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
4446 		cpu = iter->cpu_file;
4447 		buf_iter = trace_buffer_iter(iter, cpu);
4448 		if (buf_iter) {
4449 			if (!ring_buffer_iter_empty(buf_iter))
4450 				return 0;
4451 		} else {
4452 			if (!ring_buffer_empty_cpu(iter->array_buffer->buffer, cpu))
4453 				return 0;
4454 		}
4455 		return 1;
4456 	}
4457 
4458 	for_each_tracing_cpu(cpu) {
4459 		buf_iter = trace_buffer_iter(iter, cpu);
4460 		if (buf_iter) {
4461 			if (!ring_buffer_iter_empty(buf_iter))
4462 				return 0;
4463 		} else {
4464 			if (!ring_buffer_empty_cpu(iter->array_buffer->buffer, cpu))
4465 				return 0;
4466 		}
4467 	}
4468 
4469 	return 1;
4470 }
4471 
4472 /*  Called with trace_event_read_lock() held. */
4473 enum print_line_t print_trace_line(struct trace_iterator *iter)
4474 {
4475 	struct trace_array *tr = iter->tr;
4476 	unsigned long trace_flags = tr->trace_flags;
4477 	enum print_line_t ret;
4478 
4479 	if (iter->lost_events) {
4480 		if (iter->lost_events == (unsigned long)-1)
4481 			trace_seq_printf(&iter->seq, "CPU:%d [LOST EVENTS]\n",
4482 					 iter->cpu);
4483 		else
4484 			trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
4485 					 iter->cpu, iter->lost_events);
4486 		if (trace_seq_has_overflowed(&iter->seq))
4487 			return TRACE_TYPE_PARTIAL_LINE;
4488 	}
4489 
4490 	if (iter->trace && iter->trace->print_line) {
4491 		ret = iter->trace->print_line(iter);
4492 		if (ret != TRACE_TYPE_UNHANDLED)
4493 			return ret;
4494 	}
4495 
4496 	if (iter->ent->type == TRACE_BPUTS &&
4497 			trace_flags & TRACE_ITER_PRINTK &&
4498 			trace_flags & TRACE_ITER_PRINTK_MSGONLY)
4499 		return trace_print_bputs_msg_only(iter);
4500 
4501 	if (iter->ent->type == TRACE_BPRINT &&
4502 			trace_flags & TRACE_ITER_PRINTK &&
4503 			trace_flags & TRACE_ITER_PRINTK_MSGONLY)
4504 		return trace_print_bprintk_msg_only(iter);
4505 
4506 	if (iter->ent->type == TRACE_PRINT &&
4507 			trace_flags & TRACE_ITER_PRINTK &&
4508 			trace_flags & TRACE_ITER_PRINTK_MSGONLY)
4509 		return trace_print_printk_msg_only(iter);
4510 
4511 	if (trace_flags & TRACE_ITER_BIN)
4512 		return print_bin_fmt(iter);
4513 
4514 	if (trace_flags & TRACE_ITER_HEX)
4515 		return print_hex_fmt(iter);
4516 
4517 	if (trace_flags & TRACE_ITER_RAW)
4518 		return print_raw_fmt(iter);
4519 
4520 	return print_trace_fmt(iter);
4521 }
4522 
4523 void trace_latency_header(struct seq_file *m)
4524 {
4525 	struct trace_iterator *iter = m->private;
4526 	struct trace_array *tr = iter->tr;
4527 
4528 	/* print nothing if the buffers are empty */
4529 	if (trace_empty(iter))
4530 		return;
4531 
4532 	if (iter->iter_flags & TRACE_FILE_LAT_FMT)
4533 		print_trace_header(m, iter);
4534 
4535 	if (!(tr->trace_flags & TRACE_ITER_VERBOSE))
4536 		print_lat_help_header(m);
4537 }
4538 
4539 void trace_default_header(struct seq_file *m)
4540 {
4541 	struct trace_iterator *iter = m->private;
4542 	struct trace_array *tr = iter->tr;
4543 	unsigned long trace_flags = tr->trace_flags;
4544 
4545 	if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
4546 		return;
4547 
4548 	if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
4549 		/* print nothing if the buffers are empty */
4550 		if (trace_empty(iter))
4551 			return;
4552 		print_trace_header(m, iter);
4553 		if (!(trace_flags & TRACE_ITER_VERBOSE))
4554 			print_lat_help_header(m);
4555 	} else {
4556 		if (!(trace_flags & TRACE_ITER_VERBOSE)) {
4557 			if (trace_flags & TRACE_ITER_IRQ_INFO)
4558 				print_func_help_header_irq(iter->array_buffer,
4559 							   m, trace_flags);
4560 			else
4561 				print_func_help_header(iter->array_buffer, m,
4562 						       trace_flags);
4563 		}
4564 	}
4565 }
4566 
4567 static void test_ftrace_alive(struct seq_file *m)
4568 {
4569 	if (!ftrace_is_dead())
4570 		return;
4571 	seq_puts(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n"
4572 		    "#          MAY BE MISSING FUNCTION EVENTS\n");
4573 }
4574 
4575 #ifdef CONFIG_TRACER_MAX_TRACE
4576 static void show_snapshot_main_help(struct seq_file *m)
4577 {
4578 	seq_puts(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n"
4579 		    "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
4580 		    "#                      Takes a snapshot of the main buffer.\n"
4581 		    "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate or free)\n"
4582 		    "#                      (Doesn't have to be '2' works with any number that\n"
4583 		    "#                       is not a '0' or '1')\n");
4584 }
4585 
4586 static void show_snapshot_percpu_help(struct seq_file *m)
4587 {
4588 	seq_puts(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
4589 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
4590 	seq_puts(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n"
4591 		    "#                      Takes a snapshot of the main buffer for this cpu.\n");
4592 #else
4593 	seq_puts(m, "# echo 1 > snapshot : Not supported with this kernel.\n"
4594 		    "#                     Must use main snapshot file to allocate.\n");
4595 #endif
4596 	seq_puts(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n"
4597 		    "#                      (Doesn't have to be '2' works with any number that\n"
4598 		    "#                       is not a '0' or '1')\n");
4599 }
4600 
4601 static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
4602 {
4603 	if (iter->tr->allocated_snapshot)
4604 		seq_puts(m, "#\n# * Snapshot is allocated *\n#\n");
4605 	else
4606 		seq_puts(m, "#\n# * Snapshot is freed *\n#\n");
4607 
4608 	seq_puts(m, "# Snapshot commands:\n");
4609 	if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
4610 		show_snapshot_main_help(m);
4611 	else
4612 		show_snapshot_percpu_help(m);
4613 }
4614 #else
4615 /* Should never be called */
4616 static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
4617 #endif
4618 
4619 static int s_show(struct seq_file *m, void *v)
4620 {
4621 	struct trace_iterator *iter = v;
4622 	int ret;
4623 
4624 	if (iter->ent == NULL) {
4625 		if (iter->tr) {
4626 			seq_printf(m, "# tracer: %s\n", iter->trace->name);
4627 			seq_puts(m, "#\n");
4628 			test_ftrace_alive(m);
4629 		}
4630 		if (iter->snapshot && trace_empty(iter))
4631 			print_snapshot_help(m, iter);
4632 		else if (iter->trace && iter->trace->print_header)
4633 			iter->trace->print_header(m);
4634 		else
4635 			trace_default_header(m);
4636 
4637 	} else if (iter->leftover) {
4638 		/*
4639 		 * If we filled the seq_file buffer earlier, we
4640 		 * want to just show it now.
4641 		 */
4642 		ret = trace_print_seq(m, &iter->seq);
4643 
4644 		/* ret should this time be zero, but you never know */
4645 		iter->leftover = ret;
4646 
4647 	} else {
4648 		ret = print_trace_line(iter);
4649 		if (ret == TRACE_TYPE_PARTIAL_LINE) {
4650 			iter->seq.full = 0;
4651 			trace_seq_puts(&iter->seq, "[LINE TOO BIG]\n");
4652 		}
4653 		ret = trace_print_seq(m, &iter->seq);
4654 		/*
4655 		 * If we overflow the seq_file buffer, then it will
4656 		 * ask us for this data again at start up.
4657 		 * Use that instead.
4658 		 *  ret is 0 if seq_file write succeeded.
4659 		 *        -1 otherwise.
4660 		 */
4661 		iter->leftover = ret;
4662 	}
4663 
4664 	return 0;
4665 }
4666 
4667 /*
4668  * Should be used after trace_array_get(), trace_types_lock
4669  * ensures that i_cdev was already initialized.
4670  */
4671 static inline int tracing_get_cpu(struct inode *inode)
4672 {
4673 	if (inode->i_cdev) /* See trace_create_cpu_file() */
4674 		return (long)inode->i_cdev - 1;
4675 	return RING_BUFFER_ALL_CPUS;
4676 }
4677 
4678 static const struct seq_operations tracer_seq_ops = {
4679 	.start		= s_start,
4680 	.next		= s_next,
4681 	.stop		= s_stop,
4682 	.show		= s_show,
4683 };
4684 
4685 /*
4686  * Note, as iter itself can be allocated and freed in different
4687  * ways, this function is only used to free its content, and not
4688  * the iterator itself. The only requirement to all the allocations
4689  * is that it must zero all fields (kzalloc), as freeing works with
4690  * ethier allocated content or NULL.
4691  */
4692 static void free_trace_iter_content(struct trace_iterator *iter)
4693 {
4694 	/* The fmt is either NULL, allocated or points to static_fmt_buf */
4695 	if (iter->fmt != static_fmt_buf)
4696 		kfree(iter->fmt);
4697 
4698 	kfree(iter->temp);
4699 	kfree(iter->buffer_iter);
4700 	mutex_destroy(&iter->mutex);
4701 	free_cpumask_var(iter->started);
4702 }
4703 
4704 static struct trace_iterator *
4705 __tracing_open(struct inode *inode, struct file *file, bool snapshot)
4706 {
4707 	struct trace_array *tr = inode->i_private;
4708 	struct trace_iterator *iter;
4709 	int cpu;
4710 
4711 	if (tracing_disabled)
4712 		return ERR_PTR(-ENODEV);
4713 
4714 	iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
4715 	if (!iter)
4716 		return ERR_PTR(-ENOMEM);
4717 
4718 	iter->buffer_iter = kcalloc(nr_cpu_ids, sizeof(*iter->buffer_iter),
4719 				    GFP_KERNEL);
4720 	if (!iter->buffer_iter)
4721 		goto release;
4722 
4723 	/*
4724 	 * trace_find_next_entry() may need to save off iter->ent.
4725 	 * It will place it into the iter->temp buffer. As most
4726 	 * events are less than 128, allocate a buffer of that size.
4727 	 * If one is greater, then trace_find_next_entry() will
4728 	 * allocate a new buffer to adjust for the bigger iter->ent.
4729 	 * It's not critical if it fails to get allocated here.
4730 	 */
4731 	iter->temp = kmalloc(128, GFP_KERNEL);
4732 	if (iter->temp)
4733 		iter->temp_size = 128;
4734 
4735 	/*
4736 	 * trace_event_printf() may need to modify given format
4737 	 * string to replace %p with %px so that it shows real address
4738 	 * instead of hash value. However, that is only for the event
4739 	 * tracing, other tracer may not need. Defer the allocation
4740 	 * until it is needed.
4741 	 */
4742 	iter->fmt = NULL;
4743 	iter->fmt_size = 0;
4744 
4745 	mutex_lock(&trace_types_lock);
4746 	iter->trace = tr->current_trace;
4747 
4748 	if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
4749 		goto fail;
4750 
4751 	iter->tr = tr;
4752 
4753 #ifdef CONFIG_TRACER_MAX_TRACE
4754 	/* Currently only the top directory has a snapshot */
4755 	if (tr->current_trace->print_max || snapshot)
4756 		iter->array_buffer = &tr->max_buffer;
4757 	else
4758 #endif
4759 		iter->array_buffer = &tr->array_buffer;
4760 	iter->snapshot = snapshot;
4761 	iter->pos = -1;
4762 	iter->cpu_file = tracing_get_cpu(inode);
4763 	mutex_init(&iter->mutex);
4764 
4765 	/* Notify the tracer early; before we stop tracing. */
4766 	if (iter->trace->open)
4767 		iter->trace->open(iter);
4768 
4769 	/* Annotate start of buffers if we had overruns */
4770 	if (ring_buffer_overruns(iter->array_buffer->buffer))
4771 		iter->iter_flags |= TRACE_FILE_ANNOTATE;
4772 
4773 	/* Output in nanoseconds only if we are using a clock in nanoseconds. */
4774 	if (trace_clocks[tr->clock_id].in_ns)
4775 		iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
4776 
4777 	/*
4778 	 * If pause-on-trace is enabled, then stop the trace while
4779 	 * dumping, unless this is the "snapshot" file
4780 	 */
4781 	if (!iter->snapshot && (tr->trace_flags & TRACE_ITER_PAUSE_ON_TRACE))
4782 		tracing_stop_tr(tr);
4783 
4784 	if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
4785 		for_each_tracing_cpu(cpu) {
4786 			iter->buffer_iter[cpu] =
4787 				ring_buffer_read_prepare(iter->array_buffer->buffer,
4788 							 cpu, GFP_KERNEL);
4789 		}
4790 		ring_buffer_read_prepare_sync();
4791 		for_each_tracing_cpu(cpu) {
4792 			ring_buffer_read_start(iter->buffer_iter[cpu]);
4793 			tracing_iter_reset(iter, cpu);
4794 		}
4795 	} else {
4796 		cpu = iter->cpu_file;
4797 		iter->buffer_iter[cpu] =
4798 			ring_buffer_read_prepare(iter->array_buffer->buffer,
4799 						 cpu, GFP_KERNEL);
4800 		ring_buffer_read_prepare_sync();
4801 		ring_buffer_read_start(iter->buffer_iter[cpu]);
4802 		tracing_iter_reset(iter, cpu);
4803 	}
4804 
4805 	mutex_unlock(&trace_types_lock);
4806 
4807 	return iter;
4808 
4809  fail:
4810 	mutex_unlock(&trace_types_lock);
4811 	free_trace_iter_content(iter);
4812 release:
4813 	seq_release_private(inode, file);
4814 	return ERR_PTR(-ENOMEM);
4815 }
4816 
4817 int tracing_open_generic(struct inode *inode, struct file *filp)
4818 {
4819 	int ret;
4820 
4821 	ret = tracing_check_open_get_tr(NULL);
4822 	if (ret)
4823 		return ret;
4824 
4825 	filp->private_data = inode->i_private;
4826 	return 0;
4827 }
4828 
4829 bool tracing_is_disabled(void)
4830 {
4831 	return (tracing_disabled) ? true: false;
4832 }
4833 
4834 /*
4835  * Open and update trace_array ref count.
4836  * Must have the current trace_array passed to it.
4837  */
4838 int tracing_open_generic_tr(struct inode *inode, struct file *filp)
4839 {
4840 	struct trace_array *tr = inode->i_private;
4841 	int ret;
4842 
4843 	ret = tracing_check_open_get_tr(tr);
4844 	if (ret)
4845 		return ret;
4846 
4847 	filp->private_data = inode->i_private;
4848 
4849 	return 0;
4850 }
4851 
4852 /*
4853  * The private pointer of the inode is the trace_event_file.
4854  * Update the tr ref count associated to it.
4855  */
4856 int tracing_open_file_tr(struct inode *inode, struct file *filp)
4857 {
4858 	struct trace_event_file *file = inode->i_private;
4859 	int ret;
4860 
4861 	ret = tracing_check_open_get_tr(file->tr);
4862 	if (ret)
4863 		return ret;
4864 
4865 	mutex_lock(&event_mutex);
4866 
4867 	/* Fail if the file is marked for removal */
4868 	if (file->flags & EVENT_FILE_FL_FREED) {
4869 		trace_array_put(file->tr);
4870 		ret = -ENODEV;
4871 	} else {
4872 		event_file_get(file);
4873 	}
4874 
4875 	mutex_unlock(&event_mutex);
4876 	if (ret)
4877 		return ret;
4878 
4879 	filp->private_data = inode->i_private;
4880 
4881 	return 0;
4882 }
4883 
4884 int tracing_release_file_tr(struct inode *inode, struct file *filp)
4885 {
4886 	struct trace_event_file *file = inode->i_private;
4887 
4888 	trace_array_put(file->tr);
4889 	event_file_put(file);
4890 
4891 	return 0;
4892 }
4893 
4894 int tracing_single_release_file_tr(struct inode *inode, struct file *filp)
4895 {
4896 	tracing_release_file_tr(inode, filp);
4897 	return single_release(inode, filp);
4898 }
4899 
4900 static int tracing_mark_open(struct inode *inode, struct file *filp)
4901 {
4902 	stream_open(inode, filp);
4903 	return tracing_open_generic_tr(inode, filp);
4904 }
4905 
4906 static int tracing_release(struct inode *inode, struct file *file)
4907 {
4908 	struct trace_array *tr = inode->i_private;
4909 	struct seq_file *m = file->private_data;
4910 	struct trace_iterator *iter;
4911 	int cpu;
4912 
4913 	if (!(file->f_mode & FMODE_READ)) {
4914 		trace_array_put(tr);
4915 		return 0;
4916 	}
4917 
4918 	/* Writes do not use seq_file */
4919 	iter = m->private;
4920 	mutex_lock(&trace_types_lock);
4921 
4922 	for_each_tracing_cpu(cpu) {
4923 		if (iter->buffer_iter[cpu])
4924 			ring_buffer_read_finish(iter->buffer_iter[cpu]);
4925 	}
4926 
4927 	if (iter->trace && iter->trace->close)
4928 		iter->trace->close(iter);
4929 
4930 	if (!iter->snapshot && tr->stop_count)
4931 		/* reenable tracing if it was previously enabled */
4932 		tracing_start_tr(tr);
4933 
4934 	__trace_array_put(tr);
4935 
4936 	mutex_unlock(&trace_types_lock);
4937 
4938 	free_trace_iter_content(iter);
4939 	seq_release_private(inode, file);
4940 
4941 	return 0;
4942 }
4943 
4944 int tracing_release_generic_tr(struct inode *inode, struct file *file)
4945 {
4946 	struct trace_array *tr = inode->i_private;
4947 
4948 	trace_array_put(tr);
4949 	return 0;
4950 }
4951 
4952 static int tracing_single_release_tr(struct inode *inode, struct file *file)
4953 {
4954 	struct trace_array *tr = inode->i_private;
4955 
4956 	trace_array_put(tr);
4957 
4958 	return single_release(inode, file);
4959 }
4960 
4961 static int tracing_open(struct inode *inode, struct file *file)
4962 {
4963 	struct trace_array *tr = inode->i_private;
4964 	struct trace_iterator *iter;
4965 	int ret;
4966 
4967 	ret = tracing_check_open_get_tr(tr);
4968 	if (ret)
4969 		return ret;
4970 
4971 	/* If this file was open for write, then erase contents */
4972 	if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
4973 		int cpu = tracing_get_cpu(inode);
4974 		struct array_buffer *trace_buf = &tr->array_buffer;
4975 
4976 #ifdef CONFIG_TRACER_MAX_TRACE
4977 		if (tr->current_trace->print_max)
4978 			trace_buf = &tr->max_buffer;
4979 #endif
4980 
4981 		if (cpu == RING_BUFFER_ALL_CPUS)
4982 			tracing_reset_online_cpus(trace_buf);
4983 		else
4984 			tracing_reset_cpu(trace_buf, cpu);
4985 	}
4986 
4987 	if (file->f_mode & FMODE_READ) {
4988 		iter = __tracing_open(inode, file, false);
4989 		if (IS_ERR(iter))
4990 			ret = PTR_ERR(iter);
4991 		else if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
4992 			iter->iter_flags |= TRACE_FILE_LAT_FMT;
4993 	}
4994 
4995 	if (ret < 0)
4996 		trace_array_put(tr);
4997 
4998 	return ret;
4999 }
5000 
5001 /*
5002  * Some tracers are not suitable for instance buffers.
5003  * A tracer is always available for the global array (toplevel)
5004  * or if it explicitly states that it is.
5005  */
5006 static bool
5007 trace_ok_for_array(struct tracer *t, struct trace_array *tr)
5008 {
5009 #ifdef CONFIG_TRACER_SNAPSHOT
5010 	/* arrays with mapped buffer range do not have snapshots */
5011 	if (tr->range_addr_start && t->use_max_tr)
5012 		return false;
5013 #endif
5014 	return (tr->flags & TRACE_ARRAY_FL_GLOBAL) || t->allow_instances;
5015 }
5016 
5017 /* Find the next tracer that this trace array may use */
5018 static struct tracer *
5019 get_tracer_for_array(struct trace_array *tr, struct tracer *t)
5020 {
5021 	while (t && !trace_ok_for_array(t, tr))
5022 		t = t->next;
5023 
5024 	return t;
5025 }
5026 
5027 static void *
5028 t_next(struct seq_file *m, void *v, loff_t *pos)
5029 {
5030 	struct trace_array *tr = m->private;
5031 	struct tracer *t = v;
5032 
5033 	(*pos)++;
5034 
5035 	if (t)
5036 		t = get_tracer_for_array(tr, t->next);
5037 
5038 	return t;
5039 }
5040 
5041 static void *t_start(struct seq_file *m, loff_t *pos)
5042 {
5043 	struct trace_array *tr = m->private;
5044 	struct tracer *t;
5045 	loff_t l = 0;
5046 
5047 	mutex_lock(&trace_types_lock);
5048 
5049 	t = get_tracer_for_array(tr, trace_types);
5050 	for (; t && l < *pos; t = t_next(m, t, &l))
5051 			;
5052 
5053 	return t;
5054 }
5055 
5056 static void t_stop(struct seq_file *m, void *p)
5057 {
5058 	mutex_unlock(&trace_types_lock);
5059 }
5060 
5061 static int t_show(struct seq_file *m, void *v)
5062 {
5063 	struct tracer *t = v;
5064 
5065 	if (!t)
5066 		return 0;
5067 
5068 	seq_puts(m, t->name);
5069 	if (t->next)
5070 		seq_putc(m, ' ');
5071 	else
5072 		seq_putc(m, '\n');
5073 
5074 	return 0;
5075 }
5076 
5077 static const struct seq_operations show_traces_seq_ops = {
5078 	.start		= t_start,
5079 	.next		= t_next,
5080 	.stop		= t_stop,
5081 	.show		= t_show,
5082 };
5083 
5084 static int show_traces_open(struct inode *inode, struct file *file)
5085 {
5086 	struct trace_array *tr = inode->i_private;
5087 	struct seq_file *m;
5088 	int ret;
5089 
5090 	ret = tracing_check_open_get_tr(tr);
5091 	if (ret)
5092 		return ret;
5093 
5094 	ret = seq_open(file, &show_traces_seq_ops);
5095 	if (ret) {
5096 		trace_array_put(tr);
5097 		return ret;
5098 	}
5099 
5100 	m = file->private_data;
5101 	m->private = tr;
5102 
5103 	return 0;
5104 }
5105 
5106 static int tracing_seq_release(struct inode *inode, struct file *file)
5107 {
5108 	struct trace_array *tr = inode->i_private;
5109 
5110 	trace_array_put(tr);
5111 	return seq_release(inode, file);
5112 }
5113 
5114 static ssize_t
5115 tracing_write_stub(struct file *filp, const char __user *ubuf,
5116 		   size_t count, loff_t *ppos)
5117 {
5118 	return count;
5119 }
5120 
5121 loff_t tracing_lseek(struct file *file, loff_t offset, int whence)
5122 {
5123 	int ret;
5124 
5125 	if (file->f_mode & FMODE_READ)
5126 		ret = seq_lseek(file, offset, whence);
5127 	else
5128 		file->f_pos = ret = 0;
5129 
5130 	return ret;
5131 }
5132 
5133 static const struct file_operations tracing_fops = {
5134 	.open		= tracing_open,
5135 	.read		= seq_read,
5136 	.read_iter	= seq_read_iter,
5137 	.splice_read	= copy_splice_read,
5138 	.write		= tracing_write_stub,
5139 	.llseek		= tracing_lseek,
5140 	.release	= tracing_release,
5141 };
5142 
5143 static const struct file_operations show_traces_fops = {
5144 	.open		= show_traces_open,
5145 	.read		= seq_read,
5146 	.llseek		= seq_lseek,
5147 	.release	= tracing_seq_release,
5148 };
5149 
5150 static ssize_t
5151 tracing_cpumask_read(struct file *filp, char __user *ubuf,
5152 		     size_t count, loff_t *ppos)
5153 {
5154 	struct trace_array *tr = file_inode(filp)->i_private;
5155 	char *mask_str;
5156 	int len;
5157 
5158 	len = snprintf(NULL, 0, "%*pb\n",
5159 		       cpumask_pr_args(tr->tracing_cpumask)) + 1;
5160 	mask_str = kmalloc(len, GFP_KERNEL);
5161 	if (!mask_str)
5162 		return -ENOMEM;
5163 
5164 	len = snprintf(mask_str, len, "%*pb\n",
5165 		       cpumask_pr_args(tr->tracing_cpumask));
5166 	if (len >= count) {
5167 		count = -EINVAL;
5168 		goto out_err;
5169 	}
5170 	count = simple_read_from_buffer(ubuf, count, ppos, mask_str, len);
5171 
5172 out_err:
5173 	kfree(mask_str);
5174 
5175 	return count;
5176 }
5177 
5178 int tracing_set_cpumask(struct trace_array *tr,
5179 			cpumask_var_t tracing_cpumask_new)
5180 {
5181 	int cpu;
5182 
5183 	if (!tr)
5184 		return -EINVAL;
5185 
5186 	local_irq_disable();
5187 	arch_spin_lock(&tr->max_lock);
5188 	for_each_tracing_cpu(cpu) {
5189 		/*
5190 		 * Increase/decrease the disabled counter if we are
5191 		 * about to flip a bit in the cpumask:
5192 		 */
5193 		if (cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
5194 				!cpumask_test_cpu(cpu, tracing_cpumask_new)) {
5195 			atomic_inc(&per_cpu_ptr(tr->array_buffer.data, cpu)->disabled);
5196 			ring_buffer_record_disable_cpu(tr->array_buffer.buffer, cpu);
5197 #ifdef CONFIG_TRACER_MAX_TRACE
5198 			ring_buffer_record_disable_cpu(tr->max_buffer.buffer, cpu);
5199 #endif
5200 		}
5201 		if (!cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
5202 				cpumask_test_cpu(cpu, tracing_cpumask_new)) {
5203 			atomic_dec(&per_cpu_ptr(tr->array_buffer.data, cpu)->disabled);
5204 			ring_buffer_record_enable_cpu(tr->array_buffer.buffer, cpu);
5205 #ifdef CONFIG_TRACER_MAX_TRACE
5206 			ring_buffer_record_enable_cpu(tr->max_buffer.buffer, cpu);
5207 #endif
5208 		}
5209 	}
5210 	arch_spin_unlock(&tr->max_lock);
5211 	local_irq_enable();
5212 
5213 	cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new);
5214 
5215 	return 0;
5216 }
5217 
5218 static ssize_t
5219 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
5220 		      size_t count, loff_t *ppos)
5221 {
5222 	struct trace_array *tr = file_inode(filp)->i_private;
5223 	cpumask_var_t tracing_cpumask_new;
5224 	int err;
5225 
5226 	if (!zalloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
5227 		return -ENOMEM;
5228 
5229 	err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
5230 	if (err)
5231 		goto err_free;
5232 
5233 	err = tracing_set_cpumask(tr, tracing_cpumask_new);
5234 	if (err)
5235 		goto err_free;
5236 
5237 	free_cpumask_var(tracing_cpumask_new);
5238 
5239 	return count;
5240 
5241 err_free:
5242 	free_cpumask_var(tracing_cpumask_new);
5243 
5244 	return err;
5245 }
5246 
5247 static const struct file_operations tracing_cpumask_fops = {
5248 	.open		= tracing_open_generic_tr,
5249 	.read		= tracing_cpumask_read,
5250 	.write		= tracing_cpumask_write,
5251 	.release	= tracing_release_generic_tr,
5252 	.llseek		= generic_file_llseek,
5253 };
5254 
5255 static int tracing_trace_options_show(struct seq_file *m, void *v)
5256 {
5257 	struct tracer_opt *trace_opts;
5258 	struct trace_array *tr = m->private;
5259 	u32 tracer_flags;
5260 	int i;
5261 
5262 	mutex_lock(&trace_types_lock);
5263 	tracer_flags = tr->current_trace->flags->val;
5264 	trace_opts = tr->current_trace->flags->opts;
5265 
5266 	for (i = 0; trace_options[i]; i++) {
5267 		if (tr->trace_flags & (1 << i))
5268 			seq_printf(m, "%s\n", trace_options[i]);
5269 		else
5270 			seq_printf(m, "no%s\n", trace_options[i]);
5271 	}
5272 
5273 	for (i = 0; trace_opts[i].name; i++) {
5274 		if (tracer_flags & trace_opts[i].bit)
5275 			seq_printf(m, "%s\n", trace_opts[i].name);
5276 		else
5277 			seq_printf(m, "no%s\n", trace_opts[i].name);
5278 	}
5279 	mutex_unlock(&trace_types_lock);
5280 
5281 	return 0;
5282 }
5283 
5284 static int __set_tracer_option(struct trace_array *tr,
5285 			       struct tracer_flags *tracer_flags,
5286 			       struct tracer_opt *opts, int neg)
5287 {
5288 	struct tracer *trace = tracer_flags->trace;
5289 	int ret;
5290 
5291 	ret = trace->set_flag(tr, tracer_flags->val, opts->bit, !neg);
5292 	if (ret)
5293 		return ret;
5294 
5295 	if (neg)
5296 		tracer_flags->val &= ~opts->bit;
5297 	else
5298 		tracer_flags->val |= opts->bit;
5299 	return 0;
5300 }
5301 
5302 /* Try to assign a tracer specific option */
5303 static int set_tracer_option(struct trace_array *tr, char *cmp, int neg)
5304 {
5305 	struct tracer *trace = tr->current_trace;
5306 	struct tracer_flags *tracer_flags = trace->flags;
5307 	struct tracer_opt *opts = NULL;
5308 	int i;
5309 
5310 	for (i = 0; tracer_flags->opts[i].name; i++) {
5311 		opts = &tracer_flags->opts[i];
5312 
5313 		if (strcmp(cmp, opts->name) == 0)
5314 			return __set_tracer_option(tr, trace->flags, opts, neg);
5315 	}
5316 
5317 	return -EINVAL;
5318 }
5319 
5320 /* Some tracers require overwrite to stay enabled */
5321 int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
5322 {
5323 	if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
5324 		return -1;
5325 
5326 	return 0;
5327 }
5328 
5329 int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
5330 {
5331 	if ((mask == TRACE_ITER_RECORD_TGID) ||
5332 	    (mask == TRACE_ITER_RECORD_CMD) ||
5333 	    (mask == TRACE_ITER_TRACE_PRINTK))
5334 		lockdep_assert_held(&event_mutex);
5335 
5336 	/* do nothing if flag is already set */
5337 	if (!!(tr->trace_flags & mask) == !!enabled)
5338 		return 0;
5339 
5340 	/* Give the tracer a chance to approve the change */
5341 	if (tr->current_trace->flag_changed)
5342 		if (tr->current_trace->flag_changed(tr, mask, !!enabled))
5343 			return -EINVAL;
5344 
5345 	if (mask == TRACE_ITER_TRACE_PRINTK) {
5346 		if (enabled) {
5347 			update_printk_trace(tr);
5348 		} else {
5349 			/*
5350 			 * The global_trace cannot clear this.
5351 			 * It's flag only gets cleared if another instance sets it.
5352 			 */
5353 			if (printk_trace == &global_trace)
5354 				return -EINVAL;
5355 			/*
5356 			 * An instance must always have it set.
5357 			 * by default, that's the global_trace instane.
5358 			 */
5359 			if (printk_trace == tr)
5360 				update_printk_trace(&global_trace);
5361 		}
5362 	}
5363 
5364 	if (enabled)
5365 		tr->trace_flags |= mask;
5366 	else
5367 		tr->trace_flags &= ~mask;
5368 
5369 	if (mask == TRACE_ITER_RECORD_CMD)
5370 		trace_event_enable_cmd_record(enabled);
5371 
5372 	if (mask == TRACE_ITER_RECORD_TGID) {
5373 
5374 		if (trace_alloc_tgid_map() < 0) {
5375 			tr->trace_flags &= ~TRACE_ITER_RECORD_TGID;
5376 			return -ENOMEM;
5377 		}
5378 
5379 		trace_event_enable_tgid_record(enabled);
5380 	}
5381 
5382 	if (mask == TRACE_ITER_EVENT_FORK)
5383 		trace_event_follow_fork(tr, enabled);
5384 
5385 	if (mask == TRACE_ITER_FUNC_FORK)
5386 		ftrace_pid_follow_fork(tr, enabled);
5387 
5388 	if (mask == TRACE_ITER_OVERWRITE) {
5389 		ring_buffer_change_overwrite(tr->array_buffer.buffer, enabled);
5390 #ifdef CONFIG_TRACER_MAX_TRACE
5391 		ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
5392 #endif
5393 	}
5394 
5395 	if (mask == TRACE_ITER_PRINTK) {
5396 		trace_printk_start_stop_comm(enabled);
5397 		trace_printk_control(enabled);
5398 	}
5399 
5400 	return 0;
5401 }
5402 
5403 int trace_set_options(struct trace_array *tr, char *option)
5404 {
5405 	char *cmp;
5406 	int neg = 0;
5407 	int ret;
5408 	size_t orig_len = strlen(option);
5409 	int len;
5410 
5411 	cmp = strstrip(option);
5412 
5413 	len = str_has_prefix(cmp, "no");
5414 	if (len)
5415 		neg = 1;
5416 
5417 	cmp += len;
5418 
5419 	mutex_lock(&event_mutex);
5420 	mutex_lock(&trace_types_lock);
5421 
5422 	ret = match_string(trace_options, -1, cmp);
5423 	/* If no option could be set, test the specific tracer options */
5424 	if (ret < 0)
5425 		ret = set_tracer_option(tr, cmp, neg);
5426 	else
5427 		ret = set_tracer_flag(tr, 1 << ret, !neg);
5428 
5429 	mutex_unlock(&trace_types_lock);
5430 	mutex_unlock(&event_mutex);
5431 
5432 	/*
5433 	 * If the first trailing whitespace is replaced with '\0' by strstrip,
5434 	 * turn it back into a space.
5435 	 */
5436 	if (orig_len > strlen(option))
5437 		option[strlen(option)] = ' ';
5438 
5439 	return ret;
5440 }
5441 
5442 static void __init apply_trace_boot_options(void)
5443 {
5444 	char *buf = trace_boot_options_buf;
5445 	char *option;
5446 
5447 	while (true) {
5448 		option = strsep(&buf, ",");
5449 
5450 		if (!option)
5451 			break;
5452 
5453 		if (*option)
5454 			trace_set_options(&global_trace, option);
5455 
5456 		/* Put back the comma to allow this to be called again */
5457 		if (buf)
5458 			*(buf - 1) = ',';
5459 	}
5460 }
5461 
5462 static ssize_t
5463 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
5464 			size_t cnt, loff_t *ppos)
5465 {
5466 	struct seq_file *m = filp->private_data;
5467 	struct trace_array *tr = m->private;
5468 	char buf[64];
5469 	int ret;
5470 
5471 	if (cnt >= sizeof(buf))
5472 		return -EINVAL;
5473 
5474 	if (copy_from_user(buf, ubuf, cnt))
5475 		return -EFAULT;
5476 
5477 	buf[cnt] = 0;
5478 
5479 	ret = trace_set_options(tr, buf);
5480 	if (ret < 0)
5481 		return ret;
5482 
5483 	*ppos += cnt;
5484 
5485 	return cnt;
5486 }
5487 
5488 static int tracing_trace_options_open(struct inode *inode, struct file *file)
5489 {
5490 	struct trace_array *tr = inode->i_private;
5491 	int ret;
5492 
5493 	ret = tracing_check_open_get_tr(tr);
5494 	if (ret)
5495 		return ret;
5496 
5497 	ret = single_open(file, tracing_trace_options_show, inode->i_private);
5498 	if (ret < 0)
5499 		trace_array_put(tr);
5500 
5501 	return ret;
5502 }
5503 
5504 static const struct file_operations tracing_iter_fops = {
5505 	.open		= tracing_trace_options_open,
5506 	.read		= seq_read,
5507 	.llseek		= seq_lseek,
5508 	.release	= tracing_single_release_tr,
5509 	.write		= tracing_trace_options_write,
5510 };
5511 
5512 static const char readme_msg[] =
5513 	"tracing mini-HOWTO:\n\n"
5514 	"By default tracefs removes all OTH file permission bits.\n"
5515 	"When mounting tracefs an optional group id can be specified\n"
5516 	"which adds the group to every directory and file in tracefs:\n\n"
5517 	"\t e.g. mount -t tracefs [-o [gid=<gid>]] nodev /sys/kernel/tracing\n\n"
5518 	"# echo 0 > tracing_on : quick way to disable tracing\n"
5519 	"# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
5520 	" Important files:\n"
5521 	"  trace\t\t\t- The static contents of the buffer\n"
5522 	"\t\t\t  To clear the buffer write into this file: echo > trace\n"
5523 	"  trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
5524 	"  current_tracer\t- function and latency tracers\n"
5525 	"  available_tracers\t- list of configured tracers for current_tracer\n"
5526 	"  error_log\t- error log for failed commands (that support it)\n"
5527 	"  buffer_size_kb\t- view and modify size of per cpu buffer\n"
5528 	"  buffer_total_size_kb  - view total size of all cpu buffers\n\n"
5529 	"  trace_clock\t\t- change the clock used to order events\n"
5530 	"       local:   Per cpu clock but may not be synced across CPUs\n"
5531 	"      global:   Synced across CPUs but slows tracing down.\n"
5532 	"     counter:   Not a clock, but just an increment\n"
5533 	"      uptime:   Jiffy counter from time of boot\n"
5534 	"        perf:   Same clock that perf events use\n"
5535 #ifdef CONFIG_X86_64
5536 	"     x86-tsc:   TSC cycle counter\n"
5537 #endif
5538 	"\n  timestamp_mode\t- view the mode used to timestamp events\n"
5539 	"       delta:   Delta difference against a buffer-wide timestamp\n"
5540 	"    absolute:   Absolute (standalone) timestamp\n"
5541 	"\n  trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
5542 	"\n  trace_marker_raw\t\t- Writes into this file writes binary data into the kernel buffer\n"
5543 	"  tracing_cpumask\t- Limit which CPUs to trace\n"
5544 	"  instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
5545 	"\t\t\t  Remove sub-buffer with rmdir\n"
5546 	"  trace_options\t\t- Set format or modify how tracing happens\n"
5547 	"\t\t\t  Disable an option by prefixing 'no' to the\n"
5548 	"\t\t\t  option name\n"
5549 	"  saved_cmdlines_size\t- echo command number in here to store comm-pid list\n"
5550 #ifdef CONFIG_DYNAMIC_FTRACE
5551 	"\n  available_filter_functions - list of functions that can be filtered on\n"
5552 	"  set_ftrace_filter\t- echo function name in here to only trace these\n"
5553 	"\t\t\t  functions\n"
5554 	"\t     accepts: func_full_name or glob-matching-pattern\n"
5555 	"\t     modules: Can select a group via module\n"
5556 	"\t      Format: :mod:<module-name>\n"
5557 	"\t     example: echo :mod:ext3 > set_ftrace_filter\n"
5558 	"\t    triggers: a command to perform when function is hit\n"
5559 	"\t      Format: <function>:<trigger>[:count]\n"
5560 	"\t     trigger: traceon, traceoff\n"
5561 	"\t\t      enable_event:<system>:<event>\n"
5562 	"\t\t      disable_event:<system>:<event>\n"
5563 #ifdef CONFIG_STACKTRACE
5564 	"\t\t      stacktrace\n"
5565 #endif
5566 #ifdef CONFIG_TRACER_SNAPSHOT
5567 	"\t\t      snapshot\n"
5568 #endif
5569 	"\t\t      dump\n"
5570 	"\t\t      cpudump\n"
5571 	"\t     example: echo do_fault:traceoff > set_ftrace_filter\n"
5572 	"\t              echo do_trap:traceoff:3 > set_ftrace_filter\n"
5573 	"\t     The first one will disable tracing every time do_fault is hit\n"
5574 	"\t     The second will disable tracing at most 3 times when do_trap is hit\n"
5575 	"\t       The first time do trap is hit and it disables tracing, the\n"
5576 	"\t       counter will decrement to 2. If tracing is already disabled,\n"
5577 	"\t       the counter will not decrement. It only decrements when the\n"
5578 	"\t       trigger did work\n"
5579 	"\t     To remove trigger without count:\n"
5580 	"\t       echo '!<function>:<trigger> > set_ftrace_filter\n"
5581 	"\t     To remove trigger with a count:\n"
5582 	"\t       echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
5583 	"  set_ftrace_notrace\t- echo function name in here to never trace.\n"
5584 	"\t    accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
5585 	"\t    modules: Can select a group via module command :mod:\n"
5586 	"\t    Does not accept triggers\n"
5587 #endif /* CONFIG_DYNAMIC_FTRACE */
5588 #ifdef CONFIG_FUNCTION_TRACER
5589 	"  set_ftrace_pid\t- Write pid(s) to only function trace those pids\n"
5590 	"\t\t    (function)\n"
5591 	"  set_ftrace_notrace_pid\t- Write pid(s) to not function trace those pids\n"
5592 	"\t\t    (function)\n"
5593 #endif
5594 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5595 	"  set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
5596 	"  set_graph_notrace\t- Do not trace the nested calls of a function (function_graph)\n"
5597 	"  max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
5598 #endif
5599 #ifdef CONFIG_TRACER_SNAPSHOT
5600 	"\n  snapshot\t\t- Like 'trace' but shows the content of the static\n"
5601 	"\t\t\t  snapshot buffer. Read the contents for more\n"
5602 	"\t\t\t  information\n"
5603 #endif
5604 #ifdef CONFIG_STACK_TRACER
5605 	"  stack_trace\t\t- Shows the max stack trace when active\n"
5606 	"  stack_max_size\t- Shows current max stack size that was traced\n"
5607 	"\t\t\t  Write into this file to reset the max size (trigger a\n"
5608 	"\t\t\t  new trace)\n"
5609 #ifdef CONFIG_DYNAMIC_FTRACE
5610 	"  stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace\n"
5611 	"\t\t\t  traces\n"
5612 #endif
5613 #endif /* CONFIG_STACK_TRACER */
5614 #ifdef CONFIG_DYNAMIC_EVENTS
5615 	"  dynamic_events\t\t- Create/append/remove/show the generic dynamic events\n"
5616 	"\t\t\t  Write into this file to define/undefine new trace events.\n"
5617 #endif
5618 #ifdef CONFIG_KPROBE_EVENTS
5619 	"  kprobe_events\t\t- Create/append/remove/show the kernel dynamic events\n"
5620 	"\t\t\t  Write into this file to define/undefine new trace events.\n"
5621 #endif
5622 #ifdef CONFIG_UPROBE_EVENTS
5623 	"  uprobe_events\t\t- Create/append/remove/show the userspace dynamic events\n"
5624 	"\t\t\t  Write into this file to define/undefine new trace events.\n"
5625 #endif
5626 #if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS) || \
5627     defined(CONFIG_FPROBE_EVENTS)
5628 	"\t  accepts: event-definitions (one definition per line)\n"
5629 #if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS)
5630 	"\t   Format: p[:[<group>/][<event>]] <place> [<args>]\n"
5631 	"\t           r[maxactive][:[<group>/][<event>]] <place> [<args>]\n"
5632 #endif
5633 #ifdef CONFIG_FPROBE_EVENTS
5634 	"\t           f[:[<group>/][<event>]] <func-name>[%return] [<args>]\n"
5635 	"\t           t[:[<group>/][<event>]] <tracepoint> [<args>]\n"
5636 #endif
5637 #ifdef CONFIG_HIST_TRIGGERS
5638 	"\t           s:[synthetic/]<event> <field> [<field>]\n"
5639 #endif
5640 	"\t           e[:[<group>/][<event>]] <attached-group>.<attached-event> [<args>] [if <filter>]\n"
5641 	"\t           -:[<group>/][<event>]\n"
5642 #ifdef CONFIG_KPROBE_EVENTS
5643 	"\t    place: [<module>:]<symbol>[+<offset>]|<memaddr>\n"
5644   "place (kretprobe): [<module>:]<symbol>[+<offset>]%return|<memaddr>\n"
5645 #endif
5646 #ifdef CONFIG_UPROBE_EVENTS
5647   "   place (uprobe): <path>:<offset>[%return][(ref_ctr_offset)]\n"
5648 #endif
5649 	"\t     args: <name>=fetcharg[:type]\n"
5650 	"\t fetcharg: (%<register>|$<efield>), @<address>, @<symbol>[+|-<offset>],\n"
5651 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
5652 	"\t           $stack<index>, $stack, $retval, $comm, $arg<N>,\n"
5653 #ifdef CONFIG_PROBE_EVENTS_BTF_ARGS
5654 	"\t           <argname>[->field[->field|.field...]],\n"
5655 #endif
5656 #else
5657 	"\t           $stack<index>, $stack, $retval, $comm,\n"
5658 #endif
5659 	"\t           +|-[u]<offset>(<fetcharg>), \\imm-value, \\\"imm-string\"\n"
5660 	"\t     kernel return probes support: $retval, $arg<N>, $comm\n"
5661 	"\t     type: s8/16/32/64, u8/16/32/64, x8/16/32/64, char, string, symbol,\n"
5662 	"\t           b<bit-width>@<bit-offset>/<container-size>, ustring,\n"
5663 	"\t           symstr, %pd/%pD, <type>\\[<array-size>\\]\n"
5664 #ifdef CONFIG_HIST_TRIGGERS
5665 	"\t    field: <stype> <name>;\n"
5666 	"\t    stype: u8/u16/u32/u64, s8/s16/s32/s64, pid_t,\n"
5667 	"\t           [unsigned] char/int/long\n"
5668 #endif
5669 	"\t    efield: For event probes ('e' types), the field is on of the fields\n"
5670 	"\t            of the <attached-group>/<attached-event>.\n"
5671 #endif
5672 	"  events/\t\t- Directory containing all trace event subsystems:\n"
5673 	"      enable\t\t- Write 0/1 to enable/disable tracing of all events\n"
5674 	"  events/<system>/\t- Directory containing all trace events for <system>:\n"
5675 	"      enable\t\t- Write 0/1 to enable/disable tracing of all <system>\n"
5676 	"\t\t\t  events\n"
5677 	"      filter\t\t- If set, only events passing filter are traced\n"
5678 	"  events/<system>/<event>/\t- Directory containing control files for\n"
5679 	"\t\t\t  <event>:\n"
5680 	"      enable\t\t- Write 0/1 to enable/disable tracing of <event>\n"
5681 	"      filter\t\t- If set, only events passing filter are traced\n"
5682 	"      trigger\t\t- If set, a command to perform when event is hit\n"
5683 	"\t    Format: <trigger>[:count][if <filter>]\n"
5684 	"\t   trigger: traceon, traceoff\n"
5685 	"\t            enable_event:<system>:<event>\n"
5686 	"\t            disable_event:<system>:<event>\n"
5687 #ifdef CONFIG_HIST_TRIGGERS
5688 	"\t            enable_hist:<system>:<event>\n"
5689 	"\t            disable_hist:<system>:<event>\n"
5690 #endif
5691 #ifdef CONFIG_STACKTRACE
5692 	"\t\t    stacktrace\n"
5693 #endif
5694 #ifdef CONFIG_TRACER_SNAPSHOT
5695 	"\t\t    snapshot\n"
5696 #endif
5697 #ifdef CONFIG_HIST_TRIGGERS
5698 	"\t\t    hist (see below)\n"
5699 #endif
5700 	"\t   example: echo traceoff > events/block/block_unplug/trigger\n"
5701 	"\t            echo traceoff:3 > events/block/block_unplug/trigger\n"
5702 	"\t            echo 'enable_event:kmem:kmalloc:3 if nr_rq > 1' > \\\n"
5703 	"\t                  events/block/block_unplug/trigger\n"
5704 	"\t   The first disables tracing every time block_unplug is hit.\n"
5705 	"\t   The second disables tracing the first 3 times block_unplug is hit.\n"
5706 	"\t   The third enables the kmalloc event the first 3 times block_unplug\n"
5707 	"\t     is hit and has value of greater than 1 for the 'nr_rq' event field.\n"
5708 	"\t   Like function triggers, the counter is only decremented if it\n"
5709 	"\t    enabled or disabled tracing.\n"
5710 	"\t   To remove a trigger without a count:\n"
5711 	"\t     echo '!<trigger> > <system>/<event>/trigger\n"
5712 	"\t   To remove a trigger with a count:\n"
5713 	"\t     echo '!<trigger>:0 > <system>/<event>/trigger\n"
5714 	"\t   Filters can be ignored when removing a trigger.\n"
5715 #ifdef CONFIG_HIST_TRIGGERS
5716 	"      hist trigger\t- If set, event hits are aggregated into a hash table\n"
5717 	"\t    Format: hist:keys=<field1[,field2,...]>\n"
5718 	"\t            [:<var1>=<field|var_ref|numeric_literal>[,<var2>=...]]\n"
5719 	"\t            [:values=<field1[,field2,...]>]\n"
5720 	"\t            [:sort=<field1[,field2,...]>]\n"
5721 	"\t            [:size=#entries]\n"
5722 	"\t            [:pause][:continue][:clear]\n"
5723 	"\t            [:name=histname1]\n"
5724 	"\t            [:nohitcount]\n"
5725 	"\t            [:<handler>.<action>]\n"
5726 	"\t            [if <filter>]\n\n"
5727 	"\t    Note, special fields can be used as well:\n"
5728 	"\t            common_timestamp - to record current timestamp\n"
5729 	"\t            common_cpu - to record the CPU the event happened on\n"
5730 	"\n"
5731 	"\t    A hist trigger variable can be:\n"
5732 	"\t        - a reference to a field e.g. x=current_timestamp,\n"
5733 	"\t        - a reference to another variable e.g. y=$x,\n"
5734 	"\t        - a numeric literal: e.g. ms_per_sec=1000,\n"
5735 	"\t        - an arithmetic expression: e.g. time_secs=current_timestamp/1000\n"
5736 	"\n"
5737 	"\t    hist trigger arithmetic expressions support addition(+), subtraction(-),\n"
5738 	"\t    multiplication(*) and division(/) operators. An operand can be either a\n"
5739 	"\t    variable reference, field or numeric literal.\n"
5740 	"\n"
5741 	"\t    When a matching event is hit, an entry is added to a hash\n"
5742 	"\t    table using the key(s) and value(s) named, and the value of a\n"
5743 	"\t    sum called 'hitcount' is incremented.  Keys and values\n"
5744 	"\t    correspond to fields in the event's format description.  Keys\n"
5745 	"\t    can be any field, or the special string 'common_stacktrace'.\n"
5746 	"\t    Compound keys consisting of up to two fields can be specified\n"
5747 	"\t    by the 'keys' keyword.  Values must correspond to numeric\n"
5748 	"\t    fields.  Sort keys consisting of up to two fields can be\n"
5749 	"\t    specified using the 'sort' keyword.  The sort direction can\n"
5750 	"\t    be modified by appending '.descending' or '.ascending' to a\n"
5751 	"\t    sort field.  The 'size' parameter can be used to specify more\n"
5752 	"\t    or fewer than the default 2048 entries for the hashtable size.\n"
5753 	"\t    If a hist trigger is given a name using the 'name' parameter,\n"
5754 	"\t    its histogram data will be shared with other triggers of the\n"
5755 	"\t    same name, and trigger hits will update this common data.\n\n"
5756 	"\t    Reading the 'hist' file for the event will dump the hash\n"
5757 	"\t    table in its entirety to stdout.  If there are multiple hist\n"
5758 	"\t    triggers attached to an event, there will be a table for each\n"
5759 	"\t    trigger in the output.  The table displayed for a named\n"
5760 	"\t    trigger will be the same as any other instance having the\n"
5761 	"\t    same name.  The default format used to display a given field\n"
5762 	"\t    can be modified by appending any of the following modifiers\n"
5763 	"\t    to the field name, as applicable:\n\n"
5764 	"\t            .hex        display a number as a hex value\n"
5765 	"\t            .sym        display an address as a symbol\n"
5766 	"\t            .sym-offset display an address as a symbol and offset\n"
5767 	"\t            .execname   display a common_pid as a program name\n"
5768 	"\t            .syscall    display a syscall id as a syscall name\n"
5769 	"\t            .log2       display log2 value rather than raw number\n"
5770 	"\t            .buckets=size  display values in groups of size rather than raw number\n"
5771 	"\t            .usecs      display a common_timestamp in microseconds\n"
5772 	"\t            .percent    display a number of percentage value\n"
5773 	"\t            .graph      display a bar-graph of a value\n\n"
5774 	"\t    The 'pause' parameter can be used to pause an existing hist\n"
5775 	"\t    trigger or to start a hist trigger but not log any events\n"
5776 	"\t    until told to do so.  'continue' can be used to start or\n"
5777 	"\t    restart a paused hist trigger.\n\n"
5778 	"\t    The 'clear' parameter will clear the contents of a running\n"
5779 	"\t    hist trigger and leave its current paused/active state\n"
5780 	"\t    unchanged.\n\n"
5781 	"\t    The 'nohitcount' (or NOHC) parameter will suppress display of\n"
5782 	"\t    raw hitcount in the histogram.\n\n"
5783 	"\t    The enable_hist and disable_hist triggers can be used to\n"
5784 	"\t    have one event conditionally start and stop another event's\n"
5785 	"\t    already-attached hist trigger.  The syntax is analogous to\n"
5786 	"\t    the enable_event and disable_event triggers.\n\n"
5787 	"\t    Hist trigger handlers and actions are executed whenever a\n"
5788 	"\t    a histogram entry is added or updated.  They take the form:\n\n"
5789 	"\t        <handler>.<action>\n\n"
5790 	"\t    The available handlers are:\n\n"
5791 	"\t        onmatch(matching.event)  - invoke on addition or update\n"
5792 	"\t        onmax(var)               - invoke if var exceeds current max\n"
5793 	"\t        onchange(var)            - invoke action if var changes\n\n"
5794 	"\t    The available actions are:\n\n"
5795 	"\t        trace(<synthetic_event>,param list)  - generate synthetic event\n"
5796 	"\t        save(field,...)                      - save current event fields\n"
5797 #ifdef CONFIG_TRACER_SNAPSHOT
5798 	"\t        snapshot()                           - snapshot the trace buffer\n\n"
5799 #endif
5800 #ifdef CONFIG_SYNTH_EVENTS
5801 	"  events/synthetic_events\t- Create/append/remove/show synthetic events\n"
5802 	"\t  Write into this file to define/undefine new synthetic events.\n"
5803 	"\t     example: echo 'myevent u64 lat; char name[]; long[] stack' >> synthetic_events\n"
5804 #endif
5805 #endif
5806 ;
5807 
5808 static ssize_t
5809 tracing_readme_read(struct file *filp, char __user *ubuf,
5810 		       size_t cnt, loff_t *ppos)
5811 {
5812 	return simple_read_from_buffer(ubuf, cnt, ppos,
5813 					readme_msg, strlen(readme_msg));
5814 }
5815 
5816 static const struct file_operations tracing_readme_fops = {
5817 	.open		= tracing_open_generic,
5818 	.read		= tracing_readme_read,
5819 	.llseek		= generic_file_llseek,
5820 };
5821 
5822 #ifdef CONFIG_TRACE_EVAL_MAP_FILE
5823 static union trace_eval_map_item *
5824 update_eval_map(union trace_eval_map_item *ptr)
5825 {
5826 	if (!ptr->map.eval_string) {
5827 		if (ptr->tail.next) {
5828 			ptr = ptr->tail.next;
5829 			/* Set ptr to the next real item (skip head) */
5830 			ptr++;
5831 		} else
5832 			return NULL;
5833 	}
5834 	return ptr;
5835 }
5836 
5837 static void *eval_map_next(struct seq_file *m, void *v, loff_t *pos)
5838 {
5839 	union trace_eval_map_item *ptr = v;
5840 
5841 	/*
5842 	 * Paranoid! If ptr points to end, we don't want to increment past it.
5843 	 * This really should never happen.
5844 	 */
5845 	(*pos)++;
5846 	ptr = update_eval_map(ptr);
5847 	if (WARN_ON_ONCE(!ptr))
5848 		return NULL;
5849 
5850 	ptr++;
5851 	ptr = update_eval_map(ptr);
5852 
5853 	return ptr;
5854 }
5855 
5856 static void *eval_map_start(struct seq_file *m, loff_t *pos)
5857 {
5858 	union trace_eval_map_item *v;
5859 	loff_t l = 0;
5860 
5861 	mutex_lock(&trace_eval_mutex);
5862 
5863 	v = trace_eval_maps;
5864 	if (v)
5865 		v++;
5866 
5867 	while (v && l < *pos) {
5868 		v = eval_map_next(m, v, &l);
5869 	}
5870 
5871 	return v;
5872 }
5873 
5874 static void eval_map_stop(struct seq_file *m, void *v)
5875 {
5876 	mutex_unlock(&trace_eval_mutex);
5877 }
5878 
5879 static int eval_map_show(struct seq_file *m, void *v)
5880 {
5881 	union trace_eval_map_item *ptr = v;
5882 
5883 	seq_printf(m, "%s %ld (%s)\n",
5884 		   ptr->map.eval_string, ptr->map.eval_value,
5885 		   ptr->map.system);
5886 
5887 	return 0;
5888 }
5889 
5890 static const struct seq_operations tracing_eval_map_seq_ops = {
5891 	.start		= eval_map_start,
5892 	.next		= eval_map_next,
5893 	.stop		= eval_map_stop,
5894 	.show		= eval_map_show,
5895 };
5896 
5897 static int tracing_eval_map_open(struct inode *inode, struct file *filp)
5898 {
5899 	int ret;
5900 
5901 	ret = tracing_check_open_get_tr(NULL);
5902 	if (ret)
5903 		return ret;
5904 
5905 	return seq_open(filp, &tracing_eval_map_seq_ops);
5906 }
5907 
5908 static const struct file_operations tracing_eval_map_fops = {
5909 	.open		= tracing_eval_map_open,
5910 	.read		= seq_read,
5911 	.llseek		= seq_lseek,
5912 	.release	= seq_release,
5913 };
5914 
5915 static inline union trace_eval_map_item *
5916 trace_eval_jmp_to_tail(union trace_eval_map_item *ptr)
5917 {
5918 	/* Return tail of array given the head */
5919 	return ptr + ptr->head.length + 1;
5920 }
5921 
5922 static void
5923 trace_insert_eval_map_file(struct module *mod, struct trace_eval_map **start,
5924 			   int len)
5925 {
5926 	struct trace_eval_map **stop;
5927 	struct trace_eval_map **map;
5928 	union trace_eval_map_item *map_array;
5929 	union trace_eval_map_item *ptr;
5930 
5931 	stop = start + len;
5932 
5933 	/*
5934 	 * The trace_eval_maps contains the map plus a head and tail item,
5935 	 * where the head holds the module and length of array, and the
5936 	 * tail holds a pointer to the next list.
5937 	 */
5938 	map_array = kmalloc_array(len + 2, sizeof(*map_array), GFP_KERNEL);
5939 	if (!map_array) {
5940 		pr_warn("Unable to allocate trace eval mapping\n");
5941 		return;
5942 	}
5943 
5944 	mutex_lock(&trace_eval_mutex);
5945 
5946 	if (!trace_eval_maps)
5947 		trace_eval_maps = map_array;
5948 	else {
5949 		ptr = trace_eval_maps;
5950 		for (;;) {
5951 			ptr = trace_eval_jmp_to_tail(ptr);
5952 			if (!ptr->tail.next)
5953 				break;
5954 			ptr = ptr->tail.next;
5955 
5956 		}
5957 		ptr->tail.next = map_array;
5958 	}
5959 	map_array->head.mod = mod;
5960 	map_array->head.length = len;
5961 	map_array++;
5962 
5963 	for (map = start; (unsigned long)map < (unsigned long)stop; map++) {
5964 		map_array->map = **map;
5965 		map_array++;
5966 	}
5967 	memset(map_array, 0, sizeof(*map_array));
5968 
5969 	mutex_unlock(&trace_eval_mutex);
5970 }
5971 
5972 static void trace_create_eval_file(struct dentry *d_tracer)
5973 {
5974 	trace_create_file("eval_map", TRACE_MODE_READ, d_tracer,
5975 			  NULL, &tracing_eval_map_fops);
5976 }
5977 
5978 #else /* CONFIG_TRACE_EVAL_MAP_FILE */
5979 static inline void trace_create_eval_file(struct dentry *d_tracer) { }
5980 static inline void trace_insert_eval_map_file(struct module *mod,
5981 			      struct trace_eval_map **start, int len) { }
5982 #endif /* !CONFIG_TRACE_EVAL_MAP_FILE */
5983 
5984 static void trace_insert_eval_map(struct module *mod,
5985 				  struct trace_eval_map **start, int len)
5986 {
5987 	struct trace_eval_map **map;
5988 
5989 	if (len <= 0)
5990 		return;
5991 
5992 	map = start;
5993 
5994 	trace_event_eval_update(map, len);
5995 
5996 	trace_insert_eval_map_file(mod, start, len);
5997 }
5998 
5999 static ssize_t
6000 tracing_set_trace_read(struct file *filp, char __user *ubuf,
6001 		       size_t cnt, loff_t *ppos)
6002 {
6003 	struct trace_array *tr = filp->private_data;
6004 	char buf[MAX_TRACER_SIZE+2];
6005 	int r;
6006 
6007 	mutex_lock(&trace_types_lock);
6008 	r = sprintf(buf, "%s\n", tr->current_trace->name);
6009 	mutex_unlock(&trace_types_lock);
6010 
6011 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
6012 }
6013 
6014 int tracer_init(struct tracer *t, struct trace_array *tr)
6015 {
6016 	tracing_reset_online_cpus(&tr->array_buffer);
6017 	return t->init(tr);
6018 }
6019 
6020 static void set_buffer_entries(struct array_buffer *buf, unsigned long val)
6021 {
6022 	int cpu;
6023 
6024 	for_each_tracing_cpu(cpu)
6025 		per_cpu_ptr(buf->data, cpu)->entries = val;
6026 }
6027 
6028 static void update_buffer_entries(struct array_buffer *buf, int cpu)
6029 {
6030 	if (cpu == RING_BUFFER_ALL_CPUS) {
6031 		set_buffer_entries(buf, ring_buffer_size(buf->buffer, 0));
6032 	} else {
6033 		per_cpu_ptr(buf->data, cpu)->entries = ring_buffer_size(buf->buffer, cpu);
6034 	}
6035 }
6036 
6037 #ifdef CONFIG_TRACER_MAX_TRACE
6038 /* resize @tr's buffer to the size of @size_tr's entries */
6039 static int resize_buffer_duplicate_size(struct array_buffer *trace_buf,
6040 					struct array_buffer *size_buf, int cpu_id)
6041 {
6042 	int cpu, ret = 0;
6043 
6044 	if (cpu_id == RING_BUFFER_ALL_CPUS) {
6045 		for_each_tracing_cpu(cpu) {
6046 			ret = ring_buffer_resize(trace_buf->buffer,
6047 				 per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
6048 			if (ret < 0)
6049 				break;
6050 			per_cpu_ptr(trace_buf->data, cpu)->entries =
6051 				per_cpu_ptr(size_buf->data, cpu)->entries;
6052 		}
6053 	} else {
6054 		ret = ring_buffer_resize(trace_buf->buffer,
6055 				 per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
6056 		if (ret == 0)
6057 			per_cpu_ptr(trace_buf->data, cpu_id)->entries =
6058 				per_cpu_ptr(size_buf->data, cpu_id)->entries;
6059 	}
6060 
6061 	return ret;
6062 }
6063 #endif /* CONFIG_TRACER_MAX_TRACE */
6064 
6065 static int __tracing_resize_ring_buffer(struct trace_array *tr,
6066 					unsigned long size, int cpu)
6067 {
6068 	int ret;
6069 
6070 	/*
6071 	 * If kernel or user changes the size of the ring buffer
6072 	 * we use the size that was given, and we can forget about
6073 	 * expanding it later.
6074 	 */
6075 	trace_set_ring_buffer_expanded(tr);
6076 
6077 	/* May be called before buffers are initialized */
6078 	if (!tr->array_buffer.buffer)
6079 		return 0;
6080 
6081 	/* Do not allow tracing while resizing ring buffer */
6082 	tracing_stop_tr(tr);
6083 
6084 	ret = ring_buffer_resize(tr->array_buffer.buffer, size, cpu);
6085 	if (ret < 0)
6086 		goto out_start;
6087 
6088 #ifdef CONFIG_TRACER_MAX_TRACE
6089 	if (!tr->allocated_snapshot)
6090 		goto out;
6091 
6092 	ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
6093 	if (ret < 0) {
6094 		int r = resize_buffer_duplicate_size(&tr->array_buffer,
6095 						     &tr->array_buffer, cpu);
6096 		if (r < 0) {
6097 			/*
6098 			 * AARGH! We are left with different
6099 			 * size max buffer!!!!
6100 			 * The max buffer is our "snapshot" buffer.
6101 			 * When a tracer needs a snapshot (one of the
6102 			 * latency tracers), it swaps the max buffer
6103 			 * with the saved snap shot. We succeeded to
6104 			 * update the size of the main buffer, but failed to
6105 			 * update the size of the max buffer. But when we tried
6106 			 * to reset the main buffer to the original size, we
6107 			 * failed there too. This is very unlikely to
6108 			 * happen, but if it does, warn and kill all
6109 			 * tracing.
6110 			 */
6111 			WARN_ON(1);
6112 			tracing_disabled = 1;
6113 		}
6114 		goto out_start;
6115 	}
6116 
6117 	update_buffer_entries(&tr->max_buffer, cpu);
6118 
6119  out:
6120 #endif /* CONFIG_TRACER_MAX_TRACE */
6121 
6122 	update_buffer_entries(&tr->array_buffer, cpu);
6123  out_start:
6124 	tracing_start_tr(tr);
6125 	return ret;
6126 }
6127 
6128 ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
6129 				  unsigned long size, int cpu_id)
6130 {
6131 	int ret;
6132 
6133 	mutex_lock(&trace_types_lock);
6134 
6135 	if (cpu_id != RING_BUFFER_ALL_CPUS) {
6136 		/* make sure, this cpu is enabled in the mask */
6137 		if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
6138 			ret = -EINVAL;
6139 			goto out;
6140 		}
6141 	}
6142 
6143 	ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
6144 	if (ret < 0)
6145 		ret = -ENOMEM;
6146 
6147 out:
6148 	mutex_unlock(&trace_types_lock);
6149 
6150 	return ret;
6151 }
6152 
6153 static void update_last_data(struct trace_array *tr)
6154 {
6155 	if (!tr->text_delta && !tr->data_delta)
6156 		return;
6157 
6158 	/*
6159 	 * Need to clear all CPU buffers as there cannot be events
6160 	 * from the previous boot mixed with events with this boot
6161 	 * as that will cause a confusing trace. Need to clear all
6162 	 * CPU buffers, even for those that may currently be offline.
6163 	 */
6164 	tracing_reset_all_cpus(&tr->array_buffer);
6165 
6166 	/* Using current data now */
6167 	tr->text_delta = 0;
6168 	tr->data_delta = 0;
6169 }
6170 
6171 /**
6172  * tracing_update_buffers - used by tracing facility to expand ring buffers
6173  * @tr: The tracing instance
6174  *
6175  * To save on memory when the tracing is never used on a system with it
6176  * configured in. The ring buffers are set to a minimum size. But once
6177  * a user starts to use the tracing facility, then they need to grow
6178  * to their default size.
6179  *
6180  * This function is to be called when a tracer is about to be used.
6181  */
6182 int tracing_update_buffers(struct trace_array *tr)
6183 {
6184 	int ret = 0;
6185 
6186 	mutex_lock(&trace_types_lock);
6187 
6188 	update_last_data(tr);
6189 
6190 	if (!tr->ring_buffer_expanded)
6191 		ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
6192 						RING_BUFFER_ALL_CPUS);
6193 	mutex_unlock(&trace_types_lock);
6194 
6195 	return ret;
6196 }
6197 
6198 struct trace_option_dentry;
6199 
6200 static void
6201 create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
6202 
6203 /*
6204  * Used to clear out the tracer before deletion of an instance.
6205  * Must have trace_types_lock held.
6206  */
6207 static void tracing_set_nop(struct trace_array *tr)
6208 {
6209 	if (tr->current_trace == &nop_trace)
6210 		return;
6211 
6212 	tr->current_trace->enabled--;
6213 
6214 	if (tr->current_trace->reset)
6215 		tr->current_trace->reset(tr);
6216 
6217 	tr->current_trace = &nop_trace;
6218 }
6219 
6220 static bool tracer_options_updated;
6221 
6222 static void add_tracer_options(struct trace_array *tr, struct tracer *t)
6223 {
6224 	/* Only enable if the directory has been created already. */
6225 	if (!tr->dir)
6226 		return;
6227 
6228 	/* Only create trace option files after update_tracer_options finish */
6229 	if (!tracer_options_updated)
6230 		return;
6231 
6232 	create_trace_option_files(tr, t);
6233 }
6234 
6235 int tracing_set_tracer(struct trace_array *tr, const char *buf)
6236 {
6237 	struct tracer *t;
6238 #ifdef CONFIG_TRACER_MAX_TRACE
6239 	bool had_max_tr;
6240 #endif
6241 	int ret = 0;
6242 
6243 	mutex_lock(&trace_types_lock);
6244 
6245 	update_last_data(tr);
6246 
6247 	if (!tr->ring_buffer_expanded) {
6248 		ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
6249 						RING_BUFFER_ALL_CPUS);
6250 		if (ret < 0)
6251 			goto out;
6252 		ret = 0;
6253 	}
6254 
6255 	for (t = trace_types; t; t = t->next) {
6256 		if (strcmp(t->name, buf) == 0)
6257 			break;
6258 	}
6259 	if (!t) {
6260 		ret = -EINVAL;
6261 		goto out;
6262 	}
6263 	if (t == tr->current_trace)
6264 		goto out;
6265 
6266 #ifdef CONFIG_TRACER_SNAPSHOT
6267 	if (t->use_max_tr) {
6268 		local_irq_disable();
6269 		arch_spin_lock(&tr->max_lock);
6270 		if (tr->cond_snapshot)
6271 			ret = -EBUSY;
6272 		arch_spin_unlock(&tr->max_lock);
6273 		local_irq_enable();
6274 		if (ret)
6275 			goto out;
6276 	}
6277 #endif
6278 	/* Some tracers won't work on kernel command line */
6279 	if (system_state < SYSTEM_RUNNING && t->noboot) {
6280 		pr_warn("Tracer '%s' is not allowed on command line, ignored\n",
6281 			t->name);
6282 		goto out;
6283 	}
6284 
6285 	/* Some tracers are only allowed for the top level buffer */
6286 	if (!trace_ok_for_array(t, tr)) {
6287 		ret = -EINVAL;
6288 		goto out;
6289 	}
6290 
6291 	/* If trace pipe files are being read, we can't change the tracer */
6292 	if (tr->trace_ref) {
6293 		ret = -EBUSY;
6294 		goto out;
6295 	}
6296 
6297 	trace_branch_disable();
6298 
6299 	tr->current_trace->enabled--;
6300 
6301 	if (tr->current_trace->reset)
6302 		tr->current_trace->reset(tr);
6303 
6304 #ifdef CONFIG_TRACER_MAX_TRACE
6305 	had_max_tr = tr->current_trace->use_max_tr;
6306 
6307 	/* Current trace needs to be nop_trace before synchronize_rcu */
6308 	tr->current_trace = &nop_trace;
6309 
6310 	if (had_max_tr && !t->use_max_tr) {
6311 		/*
6312 		 * We need to make sure that the update_max_tr sees that
6313 		 * current_trace changed to nop_trace to keep it from
6314 		 * swapping the buffers after we resize it.
6315 		 * The update_max_tr is called from interrupts disabled
6316 		 * so a synchronized_sched() is sufficient.
6317 		 */
6318 		synchronize_rcu();
6319 		free_snapshot(tr);
6320 		tracing_disarm_snapshot(tr);
6321 	}
6322 
6323 	if (!had_max_tr && t->use_max_tr) {
6324 		ret = tracing_arm_snapshot_locked(tr);
6325 		if (ret)
6326 			goto out;
6327 	}
6328 #else
6329 	tr->current_trace = &nop_trace;
6330 #endif
6331 
6332 	if (t->init) {
6333 		ret = tracer_init(t, tr);
6334 		if (ret) {
6335 #ifdef CONFIG_TRACER_MAX_TRACE
6336 			if (t->use_max_tr)
6337 				tracing_disarm_snapshot(tr);
6338 #endif
6339 			goto out;
6340 		}
6341 	}
6342 
6343 	tr->current_trace = t;
6344 	tr->current_trace->enabled++;
6345 	trace_branch_enable(tr);
6346  out:
6347 	mutex_unlock(&trace_types_lock);
6348 
6349 	return ret;
6350 }
6351 
6352 static ssize_t
6353 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
6354 			size_t cnt, loff_t *ppos)
6355 {
6356 	struct trace_array *tr = filp->private_data;
6357 	char buf[MAX_TRACER_SIZE+1];
6358 	char *name;
6359 	size_t ret;
6360 	int err;
6361 
6362 	ret = cnt;
6363 
6364 	if (cnt > MAX_TRACER_SIZE)
6365 		cnt = MAX_TRACER_SIZE;
6366 
6367 	if (copy_from_user(buf, ubuf, cnt))
6368 		return -EFAULT;
6369 
6370 	buf[cnt] = 0;
6371 
6372 	name = strim(buf);
6373 
6374 	err = tracing_set_tracer(tr, name);
6375 	if (err)
6376 		return err;
6377 
6378 	*ppos += ret;
6379 
6380 	return ret;
6381 }
6382 
6383 static ssize_t
6384 tracing_nsecs_read(unsigned long *ptr, char __user *ubuf,
6385 		   size_t cnt, loff_t *ppos)
6386 {
6387 	char buf[64];
6388 	int r;
6389 
6390 	r = snprintf(buf, sizeof(buf), "%ld\n",
6391 		     *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
6392 	if (r > sizeof(buf))
6393 		r = sizeof(buf);
6394 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
6395 }
6396 
6397 static ssize_t
6398 tracing_nsecs_write(unsigned long *ptr, const char __user *ubuf,
6399 		    size_t cnt, loff_t *ppos)
6400 {
6401 	unsigned long val;
6402 	int ret;
6403 
6404 	ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6405 	if (ret)
6406 		return ret;
6407 
6408 	*ptr = val * 1000;
6409 
6410 	return cnt;
6411 }
6412 
6413 static ssize_t
6414 tracing_thresh_read(struct file *filp, char __user *ubuf,
6415 		    size_t cnt, loff_t *ppos)
6416 {
6417 	return tracing_nsecs_read(&tracing_thresh, ubuf, cnt, ppos);
6418 }
6419 
6420 static ssize_t
6421 tracing_thresh_write(struct file *filp, const char __user *ubuf,
6422 		     size_t cnt, loff_t *ppos)
6423 {
6424 	struct trace_array *tr = filp->private_data;
6425 	int ret;
6426 
6427 	mutex_lock(&trace_types_lock);
6428 	ret = tracing_nsecs_write(&tracing_thresh, ubuf, cnt, ppos);
6429 	if (ret < 0)
6430 		goto out;
6431 
6432 	if (tr->current_trace->update_thresh) {
6433 		ret = tr->current_trace->update_thresh(tr);
6434 		if (ret < 0)
6435 			goto out;
6436 	}
6437 
6438 	ret = cnt;
6439 out:
6440 	mutex_unlock(&trace_types_lock);
6441 
6442 	return ret;
6443 }
6444 
6445 #ifdef CONFIG_TRACER_MAX_TRACE
6446 
6447 static ssize_t
6448 tracing_max_lat_read(struct file *filp, char __user *ubuf,
6449 		     size_t cnt, loff_t *ppos)
6450 {
6451 	struct trace_array *tr = filp->private_data;
6452 
6453 	return tracing_nsecs_read(&tr->max_latency, ubuf, cnt, ppos);
6454 }
6455 
6456 static ssize_t
6457 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
6458 		      size_t cnt, loff_t *ppos)
6459 {
6460 	struct trace_array *tr = filp->private_data;
6461 
6462 	return tracing_nsecs_write(&tr->max_latency, ubuf, cnt, ppos);
6463 }
6464 
6465 #endif
6466 
6467 static int open_pipe_on_cpu(struct trace_array *tr, int cpu)
6468 {
6469 	if (cpu == RING_BUFFER_ALL_CPUS) {
6470 		if (cpumask_empty(tr->pipe_cpumask)) {
6471 			cpumask_setall(tr->pipe_cpumask);
6472 			return 0;
6473 		}
6474 	} else if (!cpumask_test_cpu(cpu, tr->pipe_cpumask)) {
6475 		cpumask_set_cpu(cpu, tr->pipe_cpumask);
6476 		return 0;
6477 	}
6478 	return -EBUSY;
6479 }
6480 
6481 static void close_pipe_on_cpu(struct trace_array *tr, int cpu)
6482 {
6483 	if (cpu == RING_BUFFER_ALL_CPUS) {
6484 		WARN_ON(!cpumask_full(tr->pipe_cpumask));
6485 		cpumask_clear(tr->pipe_cpumask);
6486 	} else {
6487 		WARN_ON(!cpumask_test_cpu(cpu, tr->pipe_cpumask));
6488 		cpumask_clear_cpu(cpu, tr->pipe_cpumask);
6489 	}
6490 }
6491 
6492 static int tracing_open_pipe(struct inode *inode, struct file *filp)
6493 {
6494 	struct trace_array *tr = inode->i_private;
6495 	struct trace_iterator *iter;
6496 	int cpu;
6497 	int ret;
6498 
6499 	ret = tracing_check_open_get_tr(tr);
6500 	if (ret)
6501 		return ret;
6502 
6503 	mutex_lock(&trace_types_lock);
6504 	cpu = tracing_get_cpu(inode);
6505 	ret = open_pipe_on_cpu(tr, cpu);
6506 	if (ret)
6507 		goto fail_pipe_on_cpu;
6508 
6509 	/* create a buffer to store the information to pass to userspace */
6510 	iter = kzalloc(sizeof(*iter), GFP_KERNEL);
6511 	if (!iter) {
6512 		ret = -ENOMEM;
6513 		goto fail_alloc_iter;
6514 	}
6515 
6516 	trace_seq_init(&iter->seq);
6517 	iter->trace = tr->current_trace;
6518 
6519 	if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
6520 		ret = -ENOMEM;
6521 		goto fail;
6522 	}
6523 
6524 	/* trace pipe does not show start of buffer */
6525 	cpumask_setall(iter->started);
6526 
6527 	if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
6528 		iter->iter_flags |= TRACE_FILE_LAT_FMT;
6529 
6530 	/* Output in nanoseconds only if we are using a clock in nanoseconds. */
6531 	if (trace_clocks[tr->clock_id].in_ns)
6532 		iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
6533 
6534 	iter->tr = tr;
6535 	iter->array_buffer = &tr->array_buffer;
6536 	iter->cpu_file = cpu;
6537 	mutex_init(&iter->mutex);
6538 	filp->private_data = iter;
6539 
6540 	if (iter->trace->pipe_open)
6541 		iter->trace->pipe_open(iter);
6542 
6543 	nonseekable_open(inode, filp);
6544 
6545 	tr->trace_ref++;
6546 
6547 	mutex_unlock(&trace_types_lock);
6548 	return ret;
6549 
6550 fail:
6551 	kfree(iter);
6552 fail_alloc_iter:
6553 	close_pipe_on_cpu(tr, cpu);
6554 fail_pipe_on_cpu:
6555 	__trace_array_put(tr);
6556 	mutex_unlock(&trace_types_lock);
6557 	return ret;
6558 }
6559 
6560 static int tracing_release_pipe(struct inode *inode, struct file *file)
6561 {
6562 	struct trace_iterator *iter = file->private_data;
6563 	struct trace_array *tr = inode->i_private;
6564 
6565 	mutex_lock(&trace_types_lock);
6566 
6567 	tr->trace_ref--;
6568 
6569 	if (iter->trace->pipe_close)
6570 		iter->trace->pipe_close(iter);
6571 	close_pipe_on_cpu(tr, iter->cpu_file);
6572 	mutex_unlock(&trace_types_lock);
6573 
6574 	free_trace_iter_content(iter);
6575 	kfree(iter);
6576 
6577 	trace_array_put(tr);
6578 
6579 	return 0;
6580 }
6581 
6582 static __poll_t
6583 trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
6584 {
6585 	struct trace_array *tr = iter->tr;
6586 
6587 	/* Iterators are static, they should be filled or empty */
6588 	if (trace_buffer_iter(iter, iter->cpu_file))
6589 		return EPOLLIN | EPOLLRDNORM;
6590 
6591 	if (tr->trace_flags & TRACE_ITER_BLOCK)
6592 		/*
6593 		 * Always select as readable when in blocking mode
6594 		 */
6595 		return EPOLLIN | EPOLLRDNORM;
6596 	else
6597 		return ring_buffer_poll_wait(iter->array_buffer->buffer, iter->cpu_file,
6598 					     filp, poll_table, iter->tr->buffer_percent);
6599 }
6600 
6601 static __poll_t
6602 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
6603 {
6604 	struct trace_iterator *iter = filp->private_data;
6605 
6606 	return trace_poll(iter, filp, poll_table);
6607 }
6608 
6609 /* Must be called with iter->mutex held. */
6610 static int tracing_wait_pipe(struct file *filp)
6611 {
6612 	struct trace_iterator *iter = filp->private_data;
6613 	int ret;
6614 
6615 	while (trace_empty(iter)) {
6616 
6617 		if ((filp->f_flags & O_NONBLOCK)) {
6618 			return -EAGAIN;
6619 		}
6620 
6621 		/*
6622 		 * We block until we read something and tracing is disabled.
6623 		 * We still block if tracing is disabled, but we have never
6624 		 * read anything. This allows a user to cat this file, and
6625 		 * then enable tracing. But after we have read something,
6626 		 * we give an EOF when tracing is again disabled.
6627 		 *
6628 		 * iter->pos will be 0 if we haven't read anything.
6629 		 */
6630 		if (!tracer_tracing_is_on(iter->tr) && iter->pos)
6631 			break;
6632 
6633 		mutex_unlock(&iter->mutex);
6634 
6635 		ret = wait_on_pipe(iter, 0);
6636 
6637 		mutex_lock(&iter->mutex);
6638 
6639 		if (ret)
6640 			return ret;
6641 	}
6642 
6643 	return 1;
6644 }
6645 
6646 /*
6647  * Consumer reader.
6648  */
6649 static ssize_t
6650 tracing_read_pipe(struct file *filp, char __user *ubuf,
6651 		  size_t cnt, loff_t *ppos)
6652 {
6653 	struct trace_iterator *iter = filp->private_data;
6654 	ssize_t sret;
6655 
6656 	/*
6657 	 * Avoid more than one consumer on a single file descriptor
6658 	 * This is just a matter of traces coherency, the ring buffer itself
6659 	 * is protected.
6660 	 */
6661 	mutex_lock(&iter->mutex);
6662 
6663 	/* return any leftover data */
6664 	sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
6665 	if (sret != -EBUSY)
6666 		goto out;
6667 
6668 	trace_seq_init(&iter->seq);
6669 
6670 	if (iter->trace->read) {
6671 		sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
6672 		if (sret)
6673 			goto out;
6674 	}
6675 
6676 waitagain:
6677 	sret = tracing_wait_pipe(filp);
6678 	if (sret <= 0)
6679 		goto out;
6680 
6681 	/* stop when tracing is finished */
6682 	if (trace_empty(iter)) {
6683 		sret = 0;
6684 		goto out;
6685 	}
6686 
6687 	if (cnt >= TRACE_SEQ_BUFFER_SIZE)
6688 		cnt = TRACE_SEQ_BUFFER_SIZE - 1;
6689 
6690 	/* reset all but tr, trace, and overruns */
6691 	trace_iterator_reset(iter);
6692 	cpumask_clear(iter->started);
6693 	trace_seq_init(&iter->seq);
6694 
6695 	trace_event_read_lock();
6696 	trace_access_lock(iter->cpu_file);
6697 	while (trace_find_next_entry_inc(iter) != NULL) {
6698 		enum print_line_t ret;
6699 		int save_len = iter->seq.seq.len;
6700 
6701 		ret = print_trace_line(iter);
6702 		if (ret == TRACE_TYPE_PARTIAL_LINE) {
6703 			/*
6704 			 * If one print_trace_line() fills entire trace_seq in one shot,
6705 			 * trace_seq_to_user() will returns -EBUSY because save_len == 0,
6706 			 * In this case, we need to consume it, otherwise, loop will peek
6707 			 * this event next time, resulting in an infinite loop.
6708 			 */
6709 			if (save_len == 0) {
6710 				iter->seq.full = 0;
6711 				trace_seq_puts(&iter->seq, "[LINE TOO BIG]\n");
6712 				trace_consume(iter);
6713 				break;
6714 			}
6715 
6716 			/* In other cases, don't print partial lines */
6717 			iter->seq.seq.len = save_len;
6718 			break;
6719 		}
6720 		if (ret != TRACE_TYPE_NO_CONSUME)
6721 			trace_consume(iter);
6722 
6723 		if (trace_seq_used(&iter->seq) >= cnt)
6724 			break;
6725 
6726 		/*
6727 		 * Setting the full flag means we reached the trace_seq buffer
6728 		 * size and we should leave by partial output condition above.
6729 		 * One of the trace_seq_* functions is not used properly.
6730 		 */
6731 		WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
6732 			  iter->ent->type);
6733 	}
6734 	trace_access_unlock(iter->cpu_file);
6735 	trace_event_read_unlock();
6736 
6737 	/* Now copy what we have to the user */
6738 	sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
6739 	if (iter->seq.readpos >= trace_seq_used(&iter->seq))
6740 		trace_seq_init(&iter->seq);
6741 
6742 	/*
6743 	 * If there was nothing to send to user, in spite of consuming trace
6744 	 * entries, go back to wait for more entries.
6745 	 */
6746 	if (sret == -EBUSY)
6747 		goto waitagain;
6748 
6749 out:
6750 	mutex_unlock(&iter->mutex);
6751 
6752 	return sret;
6753 }
6754 
6755 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
6756 				     unsigned int idx)
6757 {
6758 	__free_page(spd->pages[idx]);
6759 }
6760 
6761 static size_t
6762 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
6763 {
6764 	size_t count;
6765 	int save_len;
6766 	int ret;
6767 
6768 	/* Seq buffer is page-sized, exactly what we need. */
6769 	for (;;) {
6770 		save_len = iter->seq.seq.len;
6771 		ret = print_trace_line(iter);
6772 
6773 		if (trace_seq_has_overflowed(&iter->seq)) {
6774 			iter->seq.seq.len = save_len;
6775 			break;
6776 		}
6777 
6778 		/*
6779 		 * This should not be hit, because it should only
6780 		 * be set if the iter->seq overflowed. But check it
6781 		 * anyway to be safe.
6782 		 */
6783 		if (ret == TRACE_TYPE_PARTIAL_LINE) {
6784 			iter->seq.seq.len = save_len;
6785 			break;
6786 		}
6787 
6788 		count = trace_seq_used(&iter->seq) - save_len;
6789 		if (rem < count) {
6790 			rem = 0;
6791 			iter->seq.seq.len = save_len;
6792 			break;
6793 		}
6794 
6795 		if (ret != TRACE_TYPE_NO_CONSUME)
6796 			trace_consume(iter);
6797 		rem -= count;
6798 		if (!trace_find_next_entry_inc(iter))	{
6799 			rem = 0;
6800 			iter->ent = NULL;
6801 			break;
6802 		}
6803 	}
6804 
6805 	return rem;
6806 }
6807 
6808 static ssize_t tracing_splice_read_pipe(struct file *filp,
6809 					loff_t *ppos,
6810 					struct pipe_inode_info *pipe,
6811 					size_t len,
6812 					unsigned int flags)
6813 {
6814 	struct page *pages_def[PIPE_DEF_BUFFERS];
6815 	struct partial_page partial_def[PIPE_DEF_BUFFERS];
6816 	struct trace_iterator *iter = filp->private_data;
6817 	struct splice_pipe_desc spd = {
6818 		.pages		= pages_def,
6819 		.partial	= partial_def,
6820 		.nr_pages	= 0, /* This gets updated below. */
6821 		.nr_pages_max	= PIPE_DEF_BUFFERS,
6822 		.ops		= &default_pipe_buf_ops,
6823 		.spd_release	= tracing_spd_release_pipe,
6824 	};
6825 	ssize_t ret;
6826 	size_t rem;
6827 	unsigned int i;
6828 
6829 	if (splice_grow_spd(pipe, &spd))
6830 		return -ENOMEM;
6831 
6832 	mutex_lock(&iter->mutex);
6833 
6834 	if (iter->trace->splice_read) {
6835 		ret = iter->trace->splice_read(iter, filp,
6836 					       ppos, pipe, len, flags);
6837 		if (ret)
6838 			goto out_err;
6839 	}
6840 
6841 	ret = tracing_wait_pipe(filp);
6842 	if (ret <= 0)
6843 		goto out_err;
6844 
6845 	if (!iter->ent && !trace_find_next_entry_inc(iter)) {
6846 		ret = -EFAULT;
6847 		goto out_err;
6848 	}
6849 
6850 	trace_event_read_lock();
6851 	trace_access_lock(iter->cpu_file);
6852 
6853 	/* Fill as many pages as possible. */
6854 	for (i = 0, rem = len; i < spd.nr_pages_max && rem; i++) {
6855 		spd.pages[i] = alloc_page(GFP_KERNEL);
6856 		if (!spd.pages[i])
6857 			break;
6858 
6859 		rem = tracing_fill_pipe_page(rem, iter);
6860 
6861 		/* Copy the data into the page, so we can start over. */
6862 		ret = trace_seq_to_buffer(&iter->seq,
6863 					  page_address(spd.pages[i]),
6864 					  trace_seq_used(&iter->seq));
6865 		if (ret < 0) {
6866 			__free_page(spd.pages[i]);
6867 			break;
6868 		}
6869 		spd.partial[i].offset = 0;
6870 		spd.partial[i].len = trace_seq_used(&iter->seq);
6871 
6872 		trace_seq_init(&iter->seq);
6873 	}
6874 
6875 	trace_access_unlock(iter->cpu_file);
6876 	trace_event_read_unlock();
6877 	mutex_unlock(&iter->mutex);
6878 
6879 	spd.nr_pages = i;
6880 
6881 	if (i)
6882 		ret = splice_to_pipe(pipe, &spd);
6883 	else
6884 		ret = 0;
6885 out:
6886 	splice_shrink_spd(&spd);
6887 	return ret;
6888 
6889 out_err:
6890 	mutex_unlock(&iter->mutex);
6891 	goto out;
6892 }
6893 
6894 static ssize_t
6895 tracing_entries_read(struct file *filp, char __user *ubuf,
6896 		     size_t cnt, loff_t *ppos)
6897 {
6898 	struct inode *inode = file_inode(filp);
6899 	struct trace_array *tr = inode->i_private;
6900 	int cpu = tracing_get_cpu(inode);
6901 	char buf[64];
6902 	int r = 0;
6903 	ssize_t ret;
6904 
6905 	mutex_lock(&trace_types_lock);
6906 
6907 	if (cpu == RING_BUFFER_ALL_CPUS) {
6908 		int cpu, buf_size_same;
6909 		unsigned long size;
6910 
6911 		size = 0;
6912 		buf_size_same = 1;
6913 		/* check if all cpu sizes are same */
6914 		for_each_tracing_cpu(cpu) {
6915 			/* fill in the size from first enabled cpu */
6916 			if (size == 0)
6917 				size = per_cpu_ptr(tr->array_buffer.data, cpu)->entries;
6918 			if (size != per_cpu_ptr(tr->array_buffer.data, cpu)->entries) {
6919 				buf_size_same = 0;
6920 				break;
6921 			}
6922 		}
6923 
6924 		if (buf_size_same) {
6925 			if (!tr->ring_buffer_expanded)
6926 				r = sprintf(buf, "%lu (expanded: %lu)\n",
6927 					    size >> 10,
6928 					    trace_buf_size >> 10);
6929 			else
6930 				r = sprintf(buf, "%lu\n", size >> 10);
6931 		} else
6932 			r = sprintf(buf, "X\n");
6933 	} else
6934 		r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->array_buffer.data, cpu)->entries >> 10);
6935 
6936 	mutex_unlock(&trace_types_lock);
6937 
6938 	ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
6939 	return ret;
6940 }
6941 
6942 static ssize_t
6943 tracing_entries_write(struct file *filp, const char __user *ubuf,
6944 		      size_t cnt, loff_t *ppos)
6945 {
6946 	struct inode *inode = file_inode(filp);
6947 	struct trace_array *tr = inode->i_private;
6948 	unsigned long val;
6949 	int ret;
6950 
6951 	ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
6952 	if (ret)
6953 		return ret;
6954 
6955 	/* must have at least 1 entry */
6956 	if (!val)
6957 		return -EINVAL;
6958 
6959 	/* value is in KB */
6960 	val <<= 10;
6961 	ret = tracing_resize_ring_buffer(tr, val, tracing_get_cpu(inode));
6962 	if (ret < 0)
6963 		return ret;
6964 
6965 	*ppos += cnt;
6966 
6967 	return cnt;
6968 }
6969 
6970 static ssize_t
6971 tracing_total_entries_read(struct file *filp, char __user *ubuf,
6972 				size_t cnt, loff_t *ppos)
6973 {
6974 	struct trace_array *tr = filp->private_data;
6975 	char buf[64];
6976 	int r, cpu;
6977 	unsigned long size = 0, expanded_size = 0;
6978 
6979 	mutex_lock(&trace_types_lock);
6980 	for_each_tracing_cpu(cpu) {
6981 		size += per_cpu_ptr(tr->array_buffer.data, cpu)->entries >> 10;
6982 		if (!tr->ring_buffer_expanded)
6983 			expanded_size += trace_buf_size >> 10;
6984 	}
6985 	if (tr->ring_buffer_expanded)
6986 		r = sprintf(buf, "%lu\n", size);
6987 	else
6988 		r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
6989 	mutex_unlock(&trace_types_lock);
6990 
6991 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
6992 }
6993 
6994 static ssize_t
6995 tracing_last_boot_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
6996 {
6997 	struct trace_array *tr = filp->private_data;
6998 	struct seq_buf seq;
6999 	char buf[64];
7000 
7001 	seq_buf_init(&seq, buf, 64);
7002 
7003 	seq_buf_printf(&seq, "text delta:\t%ld\n", tr->text_delta);
7004 	seq_buf_printf(&seq, "data delta:\t%ld\n", tr->data_delta);
7005 
7006 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, seq_buf_used(&seq));
7007 }
7008 
7009 static int tracing_buffer_meta_open(struct inode *inode, struct file *filp)
7010 {
7011 	struct trace_array *tr = inode->i_private;
7012 	int cpu = tracing_get_cpu(inode);
7013 	int ret;
7014 
7015 	ret = tracing_check_open_get_tr(tr);
7016 	if (ret)
7017 		return ret;
7018 
7019 	ret = ring_buffer_meta_seq_init(filp, tr->array_buffer.buffer, cpu);
7020 	if (ret < 0)
7021 		__trace_array_put(tr);
7022 	return ret;
7023 }
7024 
7025 static ssize_t
7026 tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
7027 			  size_t cnt, loff_t *ppos)
7028 {
7029 	/*
7030 	 * There is no need to read what the user has written, this function
7031 	 * is just to make sure that there is no error when "echo" is used
7032 	 */
7033 
7034 	*ppos += cnt;
7035 
7036 	return cnt;
7037 }
7038 
7039 static int
7040 tracing_free_buffer_release(struct inode *inode, struct file *filp)
7041 {
7042 	struct trace_array *tr = inode->i_private;
7043 
7044 	/* disable tracing ? */
7045 	if (tr->trace_flags & TRACE_ITER_STOP_ON_FREE)
7046 		tracer_tracing_off(tr);
7047 	/* resize the ring buffer to 0 */
7048 	tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
7049 
7050 	trace_array_put(tr);
7051 
7052 	return 0;
7053 }
7054 
7055 #define TRACE_MARKER_MAX_SIZE		4096
7056 
7057 static ssize_t
7058 tracing_mark_write(struct file *filp, const char __user *ubuf,
7059 					size_t cnt, loff_t *fpos)
7060 {
7061 	struct trace_array *tr = filp->private_data;
7062 	struct ring_buffer_event *event;
7063 	enum event_trigger_type tt = ETT_NONE;
7064 	struct trace_buffer *buffer;
7065 	struct print_entry *entry;
7066 	int meta_size;
7067 	ssize_t written;
7068 	size_t size;
7069 	int len;
7070 
7071 /* Used in tracing_mark_raw_write() as well */
7072 #define FAULTED_STR "<faulted>"
7073 #define FAULTED_SIZE (sizeof(FAULTED_STR) - 1) /* '\0' is already accounted for */
7074 
7075 	if (tracing_disabled)
7076 		return -EINVAL;
7077 
7078 	if (!(tr->trace_flags & TRACE_ITER_MARKERS))
7079 		return -EINVAL;
7080 
7081 	if ((ssize_t)cnt < 0)
7082 		return -EINVAL;
7083 
7084 	if (cnt > TRACE_MARKER_MAX_SIZE)
7085 		cnt = TRACE_MARKER_MAX_SIZE;
7086 
7087 	meta_size = sizeof(*entry) + 2;  /* add '\0' and possible '\n' */
7088  again:
7089 	size = cnt + meta_size;
7090 
7091 	/* If less than "<faulted>", then make sure we can still add that */
7092 	if (cnt < FAULTED_SIZE)
7093 		size += FAULTED_SIZE - cnt;
7094 
7095 	buffer = tr->array_buffer.buffer;
7096 	event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
7097 					    tracing_gen_ctx());
7098 	if (unlikely(!event)) {
7099 		/*
7100 		 * If the size was greater than what was allowed, then
7101 		 * make it smaller and try again.
7102 		 */
7103 		if (size > ring_buffer_max_event_size(buffer)) {
7104 			/* cnt < FAULTED size should never be bigger than max */
7105 			if (WARN_ON_ONCE(cnt < FAULTED_SIZE))
7106 				return -EBADF;
7107 			cnt = ring_buffer_max_event_size(buffer) - meta_size;
7108 			/* The above should only happen once */
7109 			if (WARN_ON_ONCE(cnt + meta_size == size))
7110 				return -EBADF;
7111 			goto again;
7112 		}
7113 
7114 		/* Ring buffer disabled, return as if not open for write */
7115 		return -EBADF;
7116 	}
7117 
7118 	entry = ring_buffer_event_data(event);
7119 	entry->ip = _THIS_IP_;
7120 
7121 	len = __copy_from_user_inatomic(&entry->buf, ubuf, cnt);
7122 	if (len) {
7123 		memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE);
7124 		cnt = FAULTED_SIZE;
7125 		written = -EFAULT;
7126 	} else
7127 		written = cnt;
7128 
7129 	if (tr->trace_marker_file && !list_empty(&tr->trace_marker_file->triggers)) {
7130 		/* do not add \n before testing triggers, but add \0 */
7131 		entry->buf[cnt] = '\0';
7132 		tt = event_triggers_call(tr->trace_marker_file, buffer, entry, event);
7133 	}
7134 
7135 	if (entry->buf[cnt - 1] != '\n') {
7136 		entry->buf[cnt] = '\n';
7137 		entry->buf[cnt + 1] = '\0';
7138 	} else
7139 		entry->buf[cnt] = '\0';
7140 
7141 	if (static_branch_unlikely(&trace_marker_exports_enabled))
7142 		ftrace_exports(event, TRACE_EXPORT_MARKER);
7143 	__buffer_unlock_commit(buffer, event);
7144 
7145 	if (tt)
7146 		event_triggers_post_call(tr->trace_marker_file, tt);
7147 
7148 	return written;
7149 }
7150 
7151 static ssize_t
7152 tracing_mark_raw_write(struct file *filp, const char __user *ubuf,
7153 					size_t cnt, loff_t *fpos)
7154 {
7155 	struct trace_array *tr = filp->private_data;
7156 	struct ring_buffer_event *event;
7157 	struct trace_buffer *buffer;
7158 	struct raw_data_entry *entry;
7159 	ssize_t written;
7160 	int size;
7161 	int len;
7162 
7163 #define FAULT_SIZE_ID (FAULTED_SIZE + sizeof(int))
7164 
7165 	if (tracing_disabled)
7166 		return -EINVAL;
7167 
7168 	if (!(tr->trace_flags & TRACE_ITER_MARKERS))
7169 		return -EINVAL;
7170 
7171 	/* The marker must at least have a tag id */
7172 	if (cnt < sizeof(unsigned int))
7173 		return -EINVAL;
7174 
7175 	size = sizeof(*entry) + cnt;
7176 	if (cnt < FAULT_SIZE_ID)
7177 		size += FAULT_SIZE_ID - cnt;
7178 
7179 	buffer = tr->array_buffer.buffer;
7180 
7181 	if (size > ring_buffer_max_event_size(buffer))
7182 		return -EINVAL;
7183 
7184 	event = __trace_buffer_lock_reserve(buffer, TRACE_RAW_DATA, size,
7185 					    tracing_gen_ctx());
7186 	if (!event)
7187 		/* Ring buffer disabled, return as if not open for write */
7188 		return -EBADF;
7189 
7190 	entry = ring_buffer_event_data(event);
7191 
7192 	len = __copy_from_user_inatomic(&entry->id, ubuf, cnt);
7193 	if (len) {
7194 		entry->id = -1;
7195 		memcpy(&entry->buf, FAULTED_STR, FAULTED_SIZE);
7196 		written = -EFAULT;
7197 	} else
7198 		written = cnt;
7199 
7200 	__buffer_unlock_commit(buffer, event);
7201 
7202 	return written;
7203 }
7204 
7205 static int tracing_clock_show(struct seq_file *m, void *v)
7206 {
7207 	struct trace_array *tr = m->private;
7208 	int i;
7209 
7210 	for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
7211 		seq_printf(m,
7212 			"%s%s%s%s", i ? " " : "",
7213 			i == tr->clock_id ? "[" : "", trace_clocks[i].name,
7214 			i == tr->clock_id ? "]" : "");
7215 	seq_putc(m, '\n');
7216 
7217 	return 0;
7218 }
7219 
7220 int tracing_set_clock(struct trace_array *tr, const char *clockstr)
7221 {
7222 	int i;
7223 
7224 	for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
7225 		if (strcmp(trace_clocks[i].name, clockstr) == 0)
7226 			break;
7227 	}
7228 	if (i == ARRAY_SIZE(trace_clocks))
7229 		return -EINVAL;
7230 
7231 	mutex_lock(&trace_types_lock);
7232 
7233 	tr->clock_id = i;
7234 
7235 	ring_buffer_set_clock(tr->array_buffer.buffer, trace_clocks[i].func);
7236 
7237 	/*
7238 	 * New clock may not be consistent with the previous clock.
7239 	 * Reset the buffer so that it doesn't have incomparable timestamps.
7240 	 */
7241 	tracing_reset_online_cpus(&tr->array_buffer);
7242 
7243 #ifdef CONFIG_TRACER_MAX_TRACE
7244 	if (tr->max_buffer.buffer)
7245 		ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
7246 	tracing_reset_online_cpus(&tr->max_buffer);
7247 #endif
7248 
7249 	mutex_unlock(&trace_types_lock);
7250 
7251 	return 0;
7252 }
7253 
7254 static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
7255 				   size_t cnt, loff_t *fpos)
7256 {
7257 	struct seq_file *m = filp->private_data;
7258 	struct trace_array *tr = m->private;
7259 	char buf[64];
7260 	const char *clockstr;
7261 	int ret;
7262 
7263 	if (cnt >= sizeof(buf))
7264 		return -EINVAL;
7265 
7266 	if (copy_from_user(buf, ubuf, cnt))
7267 		return -EFAULT;
7268 
7269 	buf[cnt] = 0;
7270 
7271 	clockstr = strstrip(buf);
7272 
7273 	ret = tracing_set_clock(tr, clockstr);
7274 	if (ret)
7275 		return ret;
7276 
7277 	*fpos += cnt;
7278 
7279 	return cnt;
7280 }
7281 
7282 static int tracing_clock_open(struct inode *inode, struct file *file)
7283 {
7284 	struct trace_array *tr = inode->i_private;
7285 	int ret;
7286 
7287 	ret = tracing_check_open_get_tr(tr);
7288 	if (ret)
7289 		return ret;
7290 
7291 	ret = single_open(file, tracing_clock_show, inode->i_private);
7292 	if (ret < 0)
7293 		trace_array_put(tr);
7294 
7295 	return ret;
7296 }
7297 
7298 static int tracing_time_stamp_mode_show(struct seq_file *m, void *v)
7299 {
7300 	struct trace_array *tr = m->private;
7301 
7302 	mutex_lock(&trace_types_lock);
7303 
7304 	if (ring_buffer_time_stamp_abs(tr->array_buffer.buffer))
7305 		seq_puts(m, "delta [absolute]\n");
7306 	else
7307 		seq_puts(m, "[delta] absolute\n");
7308 
7309 	mutex_unlock(&trace_types_lock);
7310 
7311 	return 0;
7312 }
7313 
7314 static int tracing_time_stamp_mode_open(struct inode *inode, struct file *file)
7315 {
7316 	struct trace_array *tr = inode->i_private;
7317 	int ret;
7318 
7319 	ret = tracing_check_open_get_tr(tr);
7320 	if (ret)
7321 		return ret;
7322 
7323 	ret = single_open(file, tracing_time_stamp_mode_show, inode->i_private);
7324 	if (ret < 0)
7325 		trace_array_put(tr);
7326 
7327 	return ret;
7328 }
7329 
7330 u64 tracing_event_time_stamp(struct trace_buffer *buffer, struct ring_buffer_event *rbe)
7331 {
7332 	if (rbe == this_cpu_read(trace_buffered_event))
7333 		return ring_buffer_time_stamp(buffer);
7334 
7335 	return ring_buffer_event_time_stamp(buffer, rbe);
7336 }
7337 
7338 /*
7339  * Set or disable using the per CPU trace_buffer_event when possible.
7340  */
7341 int tracing_set_filter_buffering(struct trace_array *tr, bool set)
7342 {
7343 	int ret = 0;
7344 
7345 	mutex_lock(&trace_types_lock);
7346 
7347 	if (set && tr->no_filter_buffering_ref++)
7348 		goto out;
7349 
7350 	if (!set) {
7351 		if (WARN_ON_ONCE(!tr->no_filter_buffering_ref)) {
7352 			ret = -EINVAL;
7353 			goto out;
7354 		}
7355 
7356 		--tr->no_filter_buffering_ref;
7357 	}
7358  out:
7359 	mutex_unlock(&trace_types_lock);
7360 
7361 	return ret;
7362 }
7363 
7364 struct ftrace_buffer_info {
7365 	struct trace_iterator	iter;
7366 	void			*spare;
7367 	unsigned int		spare_cpu;
7368 	unsigned int		spare_size;
7369 	unsigned int		read;
7370 };
7371 
7372 #ifdef CONFIG_TRACER_SNAPSHOT
7373 static int tracing_snapshot_open(struct inode *inode, struct file *file)
7374 {
7375 	struct trace_array *tr = inode->i_private;
7376 	struct trace_iterator *iter;
7377 	struct seq_file *m;
7378 	int ret;
7379 
7380 	ret = tracing_check_open_get_tr(tr);
7381 	if (ret)
7382 		return ret;
7383 
7384 	if (file->f_mode & FMODE_READ) {
7385 		iter = __tracing_open(inode, file, true);
7386 		if (IS_ERR(iter))
7387 			ret = PTR_ERR(iter);
7388 	} else {
7389 		/* Writes still need the seq_file to hold the private data */
7390 		ret = -ENOMEM;
7391 		m = kzalloc(sizeof(*m), GFP_KERNEL);
7392 		if (!m)
7393 			goto out;
7394 		iter = kzalloc(sizeof(*iter), GFP_KERNEL);
7395 		if (!iter) {
7396 			kfree(m);
7397 			goto out;
7398 		}
7399 		ret = 0;
7400 
7401 		iter->tr = tr;
7402 		iter->array_buffer = &tr->max_buffer;
7403 		iter->cpu_file = tracing_get_cpu(inode);
7404 		m->private = iter;
7405 		file->private_data = m;
7406 	}
7407 out:
7408 	if (ret < 0)
7409 		trace_array_put(tr);
7410 
7411 	return ret;
7412 }
7413 
7414 static void tracing_swap_cpu_buffer(void *tr)
7415 {
7416 	update_max_tr_single((struct trace_array *)tr, current, smp_processor_id());
7417 }
7418 
7419 static ssize_t
7420 tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
7421 		       loff_t *ppos)
7422 {
7423 	struct seq_file *m = filp->private_data;
7424 	struct trace_iterator *iter = m->private;
7425 	struct trace_array *tr = iter->tr;
7426 	unsigned long val;
7427 	int ret;
7428 
7429 	ret = tracing_update_buffers(tr);
7430 	if (ret < 0)
7431 		return ret;
7432 
7433 	ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
7434 	if (ret)
7435 		return ret;
7436 
7437 	mutex_lock(&trace_types_lock);
7438 
7439 	if (tr->current_trace->use_max_tr) {
7440 		ret = -EBUSY;
7441 		goto out;
7442 	}
7443 
7444 	local_irq_disable();
7445 	arch_spin_lock(&tr->max_lock);
7446 	if (tr->cond_snapshot)
7447 		ret = -EBUSY;
7448 	arch_spin_unlock(&tr->max_lock);
7449 	local_irq_enable();
7450 	if (ret)
7451 		goto out;
7452 
7453 	switch (val) {
7454 	case 0:
7455 		if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
7456 			ret = -EINVAL;
7457 			break;
7458 		}
7459 		if (tr->allocated_snapshot)
7460 			free_snapshot(tr);
7461 		break;
7462 	case 1:
7463 /* Only allow per-cpu swap if the ring buffer supports it */
7464 #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
7465 		if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
7466 			ret = -EINVAL;
7467 			break;
7468 		}
7469 #endif
7470 		if (tr->allocated_snapshot)
7471 			ret = resize_buffer_duplicate_size(&tr->max_buffer,
7472 					&tr->array_buffer, iter->cpu_file);
7473 
7474 		ret = tracing_arm_snapshot_locked(tr);
7475 		if (ret)
7476 			break;
7477 
7478 		/* Now, we're going to swap */
7479 		if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
7480 			local_irq_disable();
7481 			update_max_tr(tr, current, smp_processor_id(), NULL);
7482 			local_irq_enable();
7483 		} else {
7484 			smp_call_function_single(iter->cpu_file, tracing_swap_cpu_buffer,
7485 						 (void *)tr, 1);
7486 		}
7487 		tracing_disarm_snapshot(tr);
7488 		break;
7489 	default:
7490 		if (tr->allocated_snapshot) {
7491 			if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
7492 				tracing_reset_online_cpus(&tr->max_buffer);
7493 			else
7494 				tracing_reset_cpu(&tr->max_buffer, iter->cpu_file);
7495 		}
7496 		break;
7497 	}
7498 
7499 	if (ret >= 0) {
7500 		*ppos += cnt;
7501 		ret = cnt;
7502 	}
7503 out:
7504 	mutex_unlock(&trace_types_lock);
7505 	return ret;
7506 }
7507 
7508 static int tracing_snapshot_release(struct inode *inode, struct file *file)
7509 {
7510 	struct seq_file *m = file->private_data;
7511 	int ret;
7512 
7513 	ret = tracing_release(inode, file);
7514 
7515 	if (file->f_mode & FMODE_READ)
7516 		return ret;
7517 
7518 	/* If write only, the seq_file is just a stub */
7519 	if (m)
7520 		kfree(m->private);
7521 	kfree(m);
7522 
7523 	return 0;
7524 }
7525 
7526 static int tracing_buffers_open(struct inode *inode, struct file *filp);
7527 static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
7528 				    size_t count, loff_t *ppos);
7529 static int tracing_buffers_release(struct inode *inode, struct file *file);
7530 static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
7531 		   struct pipe_inode_info *pipe, size_t len, unsigned int flags);
7532 
7533 static int snapshot_raw_open(struct inode *inode, struct file *filp)
7534 {
7535 	struct ftrace_buffer_info *info;
7536 	int ret;
7537 
7538 	/* The following checks for tracefs lockdown */
7539 	ret = tracing_buffers_open(inode, filp);
7540 	if (ret < 0)
7541 		return ret;
7542 
7543 	info = filp->private_data;
7544 
7545 	if (info->iter.trace->use_max_tr) {
7546 		tracing_buffers_release(inode, filp);
7547 		return -EBUSY;
7548 	}
7549 
7550 	info->iter.snapshot = true;
7551 	info->iter.array_buffer = &info->iter.tr->max_buffer;
7552 
7553 	return ret;
7554 }
7555 
7556 #endif /* CONFIG_TRACER_SNAPSHOT */
7557 
7558 
7559 static const struct file_operations tracing_thresh_fops = {
7560 	.open		= tracing_open_generic,
7561 	.read		= tracing_thresh_read,
7562 	.write		= tracing_thresh_write,
7563 	.llseek		= generic_file_llseek,
7564 };
7565 
7566 #ifdef CONFIG_TRACER_MAX_TRACE
7567 static const struct file_operations tracing_max_lat_fops = {
7568 	.open		= tracing_open_generic_tr,
7569 	.read		= tracing_max_lat_read,
7570 	.write		= tracing_max_lat_write,
7571 	.llseek		= generic_file_llseek,
7572 	.release	= tracing_release_generic_tr,
7573 };
7574 #endif
7575 
7576 static const struct file_operations set_tracer_fops = {
7577 	.open		= tracing_open_generic_tr,
7578 	.read		= tracing_set_trace_read,
7579 	.write		= tracing_set_trace_write,
7580 	.llseek		= generic_file_llseek,
7581 	.release	= tracing_release_generic_tr,
7582 };
7583 
7584 static const struct file_operations tracing_pipe_fops = {
7585 	.open		= tracing_open_pipe,
7586 	.poll		= tracing_poll_pipe,
7587 	.read		= tracing_read_pipe,
7588 	.splice_read	= tracing_splice_read_pipe,
7589 	.release	= tracing_release_pipe,
7590 };
7591 
7592 static const struct file_operations tracing_entries_fops = {
7593 	.open		= tracing_open_generic_tr,
7594 	.read		= tracing_entries_read,
7595 	.write		= tracing_entries_write,
7596 	.llseek		= generic_file_llseek,
7597 	.release	= tracing_release_generic_tr,
7598 };
7599 
7600 static const struct file_operations tracing_buffer_meta_fops = {
7601 	.open		= tracing_buffer_meta_open,
7602 	.read		= seq_read,
7603 	.llseek		= seq_lseek,
7604 	.release	= tracing_seq_release,
7605 };
7606 
7607 static const struct file_operations tracing_total_entries_fops = {
7608 	.open		= tracing_open_generic_tr,
7609 	.read		= tracing_total_entries_read,
7610 	.llseek		= generic_file_llseek,
7611 	.release	= tracing_release_generic_tr,
7612 };
7613 
7614 static const struct file_operations tracing_free_buffer_fops = {
7615 	.open		= tracing_open_generic_tr,
7616 	.write		= tracing_free_buffer_write,
7617 	.release	= tracing_free_buffer_release,
7618 };
7619 
7620 static const struct file_operations tracing_mark_fops = {
7621 	.open		= tracing_mark_open,
7622 	.write		= tracing_mark_write,
7623 	.release	= tracing_release_generic_tr,
7624 };
7625 
7626 static const struct file_operations tracing_mark_raw_fops = {
7627 	.open		= tracing_mark_open,
7628 	.write		= tracing_mark_raw_write,
7629 	.release	= tracing_release_generic_tr,
7630 };
7631 
7632 static const struct file_operations trace_clock_fops = {
7633 	.open		= tracing_clock_open,
7634 	.read		= seq_read,
7635 	.llseek		= seq_lseek,
7636 	.release	= tracing_single_release_tr,
7637 	.write		= tracing_clock_write,
7638 };
7639 
7640 static const struct file_operations trace_time_stamp_mode_fops = {
7641 	.open		= tracing_time_stamp_mode_open,
7642 	.read		= seq_read,
7643 	.llseek		= seq_lseek,
7644 	.release	= tracing_single_release_tr,
7645 };
7646 
7647 static const struct file_operations last_boot_fops = {
7648 	.open		= tracing_open_generic_tr,
7649 	.read		= tracing_last_boot_read,
7650 	.llseek		= generic_file_llseek,
7651 	.release	= tracing_release_generic_tr,
7652 };
7653 
7654 #ifdef CONFIG_TRACER_SNAPSHOT
7655 static const struct file_operations snapshot_fops = {
7656 	.open		= tracing_snapshot_open,
7657 	.read		= seq_read,
7658 	.write		= tracing_snapshot_write,
7659 	.llseek		= tracing_lseek,
7660 	.release	= tracing_snapshot_release,
7661 };
7662 
7663 static const struct file_operations snapshot_raw_fops = {
7664 	.open		= snapshot_raw_open,
7665 	.read		= tracing_buffers_read,
7666 	.release	= tracing_buffers_release,
7667 	.splice_read	= tracing_buffers_splice_read,
7668 };
7669 
7670 #endif /* CONFIG_TRACER_SNAPSHOT */
7671 
7672 /*
7673  * trace_min_max_write - Write a u64 value to a trace_min_max_param struct
7674  * @filp: The active open file structure
7675  * @ubuf: The userspace provided buffer to read value into
7676  * @cnt: The maximum number of bytes to read
7677  * @ppos: The current "file" position
7678  *
7679  * This function implements the write interface for a struct trace_min_max_param.
7680  * The filp->private_data must point to a trace_min_max_param structure that
7681  * defines where to write the value, the min and the max acceptable values,
7682  * and a lock to protect the write.
7683  */
7684 static ssize_t
7685 trace_min_max_write(struct file *filp, const char __user *ubuf, size_t cnt, loff_t *ppos)
7686 {
7687 	struct trace_min_max_param *param = filp->private_data;
7688 	u64 val;
7689 	int err;
7690 
7691 	if (!param)
7692 		return -EFAULT;
7693 
7694 	err = kstrtoull_from_user(ubuf, cnt, 10, &val);
7695 	if (err)
7696 		return err;
7697 
7698 	if (param->lock)
7699 		mutex_lock(param->lock);
7700 
7701 	if (param->min && val < *param->min)
7702 		err = -EINVAL;
7703 
7704 	if (param->max && val > *param->max)
7705 		err = -EINVAL;
7706 
7707 	if (!err)
7708 		*param->val = val;
7709 
7710 	if (param->lock)
7711 		mutex_unlock(param->lock);
7712 
7713 	if (err)
7714 		return err;
7715 
7716 	return cnt;
7717 }
7718 
7719 /*
7720  * trace_min_max_read - Read a u64 value from a trace_min_max_param struct
7721  * @filp: The active open file structure
7722  * @ubuf: The userspace provided buffer to read value into
7723  * @cnt: The maximum number of bytes to read
7724  * @ppos: The current "file" position
7725  *
7726  * This function implements the read interface for a struct trace_min_max_param.
7727  * The filp->private_data must point to a trace_min_max_param struct with valid
7728  * data.
7729  */
7730 static ssize_t
7731 trace_min_max_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
7732 {
7733 	struct trace_min_max_param *param = filp->private_data;
7734 	char buf[U64_STR_SIZE];
7735 	int len;
7736 	u64 val;
7737 
7738 	if (!param)
7739 		return -EFAULT;
7740 
7741 	val = *param->val;
7742 
7743 	if (cnt > sizeof(buf))
7744 		cnt = sizeof(buf);
7745 
7746 	len = snprintf(buf, sizeof(buf), "%llu\n", val);
7747 
7748 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
7749 }
7750 
7751 const struct file_operations trace_min_max_fops = {
7752 	.open		= tracing_open_generic,
7753 	.read		= trace_min_max_read,
7754 	.write		= trace_min_max_write,
7755 };
7756 
7757 #define TRACING_LOG_ERRS_MAX	8
7758 #define TRACING_LOG_LOC_MAX	128
7759 
7760 #define CMD_PREFIX "  Command: "
7761 
7762 struct err_info {
7763 	const char	**errs;	/* ptr to loc-specific array of err strings */
7764 	u8		type;	/* index into errs -> specific err string */
7765 	u16		pos;	/* caret position */
7766 	u64		ts;
7767 };
7768 
7769 struct tracing_log_err {
7770 	struct list_head	list;
7771 	struct err_info		info;
7772 	char			loc[TRACING_LOG_LOC_MAX]; /* err location */
7773 	char			*cmd;                     /* what caused err */
7774 };
7775 
7776 static DEFINE_MUTEX(tracing_err_log_lock);
7777 
7778 static struct tracing_log_err *alloc_tracing_log_err(int len)
7779 {
7780 	struct tracing_log_err *err;
7781 
7782 	err = kzalloc(sizeof(*err), GFP_KERNEL);
7783 	if (!err)
7784 		return ERR_PTR(-ENOMEM);
7785 
7786 	err->cmd = kzalloc(len, GFP_KERNEL);
7787 	if (!err->cmd) {
7788 		kfree(err);
7789 		return ERR_PTR(-ENOMEM);
7790 	}
7791 
7792 	return err;
7793 }
7794 
7795 static void free_tracing_log_err(struct tracing_log_err *err)
7796 {
7797 	kfree(err->cmd);
7798 	kfree(err);
7799 }
7800 
7801 static struct tracing_log_err *get_tracing_log_err(struct trace_array *tr,
7802 						   int len)
7803 {
7804 	struct tracing_log_err *err;
7805 	char *cmd;
7806 
7807 	if (tr->n_err_log_entries < TRACING_LOG_ERRS_MAX) {
7808 		err = alloc_tracing_log_err(len);
7809 		if (PTR_ERR(err) != -ENOMEM)
7810 			tr->n_err_log_entries++;
7811 
7812 		return err;
7813 	}
7814 	cmd = kzalloc(len, GFP_KERNEL);
7815 	if (!cmd)
7816 		return ERR_PTR(-ENOMEM);
7817 	err = list_first_entry(&tr->err_log, struct tracing_log_err, list);
7818 	kfree(err->cmd);
7819 	err->cmd = cmd;
7820 	list_del(&err->list);
7821 
7822 	return err;
7823 }
7824 
7825 /**
7826  * err_pos - find the position of a string within a command for error careting
7827  * @cmd: The tracing command that caused the error
7828  * @str: The string to position the caret at within @cmd
7829  *
7830  * Finds the position of the first occurrence of @str within @cmd.  The
7831  * return value can be passed to tracing_log_err() for caret placement
7832  * within @cmd.
7833  *
7834  * Returns the index within @cmd of the first occurrence of @str or 0
7835  * if @str was not found.
7836  */
7837 unsigned int err_pos(char *cmd, const char *str)
7838 {
7839 	char *found;
7840 
7841 	if (WARN_ON(!strlen(cmd)))
7842 		return 0;
7843 
7844 	found = strstr(cmd, str);
7845 	if (found)
7846 		return found - cmd;
7847 
7848 	return 0;
7849 }
7850 
7851 /**
7852  * tracing_log_err - write an error to the tracing error log
7853  * @tr: The associated trace array for the error (NULL for top level array)
7854  * @loc: A string describing where the error occurred
7855  * @cmd: The tracing command that caused the error
7856  * @errs: The array of loc-specific static error strings
7857  * @type: The index into errs[], which produces the specific static err string
7858  * @pos: The position the caret should be placed in the cmd
7859  *
7860  * Writes an error into tracing/error_log of the form:
7861  *
7862  * <loc>: error: <text>
7863  *   Command: <cmd>
7864  *              ^
7865  *
7866  * tracing/error_log is a small log file containing the last
7867  * TRACING_LOG_ERRS_MAX errors (8).  Memory for errors isn't allocated
7868  * unless there has been a tracing error, and the error log can be
7869  * cleared and have its memory freed by writing the empty string in
7870  * truncation mode to it i.e. echo > tracing/error_log.
7871  *
7872  * NOTE: the @errs array along with the @type param are used to
7873  * produce a static error string - this string is not copied and saved
7874  * when the error is logged - only a pointer to it is saved.  See
7875  * existing callers for examples of how static strings are typically
7876  * defined for use with tracing_log_err().
7877  */
7878 void tracing_log_err(struct trace_array *tr,
7879 		     const char *loc, const char *cmd,
7880 		     const char **errs, u8 type, u16 pos)
7881 {
7882 	struct tracing_log_err *err;
7883 	int len = 0;
7884 
7885 	if (!tr)
7886 		tr = &global_trace;
7887 
7888 	len += sizeof(CMD_PREFIX) + 2 * sizeof("\n") + strlen(cmd) + 1;
7889 
7890 	mutex_lock(&tracing_err_log_lock);
7891 	err = get_tracing_log_err(tr, len);
7892 	if (PTR_ERR(err) == -ENOMEM) {
7893 		mutex_unlock(&tracing_err_log_lock);
7894 		return;
7895 	}
7896 
7897 	snprintf(err->loc, TRACING_LOG_LOC_MAX, "%s: error: ", loc);
7898 	snprintf(err->cmd, len, "\n" CMD_PREFIX "%s\n", cmd);
7899 
7900 	err->info.errs = errs;
7901 	err->info.type = type;
7902 	err->info.pos = pos;
7903 	err->info.ts = local_clock();
7904 
7905 	list_add_tail(&err->list, &tr->err_log);
7906 	mutex_unlock(&tracing_err_log_lock);
7907 }
7908 
7909 static void clear_tracing_err_log(struct trace_array *tr)
7910 {
7911 	struct tracing_log_err *err, *next;
7912 
7913 	mutex_lock(&tracing_err_log_lock);
7914 	list_for_each_entry_safe(err, next, &tr->err_log, list) {
7915 		list_del(&err->list);
7916 		free_tracing_log_err(err);
7917 	}
7918 
7919 	tr->n_err_log_entries = 0;
7920 	mutex_unlock(&tracing_err_log_lock);
7921 }
7922 
7923 static void *tracing_err_log_seq_start(struct seq_file *m, loff_t *pos)
7924 {
7925 	struct trace_array *tr = m->private;
7926 
7927 	mutex_lock(&tracing_err_log_lock);
7928 
7929 	return seq_list_start(&tr->err_log, *pos);
7930 }
7931 
7932 static void *tracing_err_log_seq_next(struct seq_file *m, void *v, loff_t *pos)
7933 {
7934 	struct trace_array *tr = m->private;
7935 
7936 	return seq_list_next(v, &tr->err_log, pos);
7937 }
7938 
7939 static void tracing_err_log_seq_stop(struct seq_file *m, void *v)
7940 {
7941 	mutex_unlock(&tracing_err_log_lock);
7942 }
7943 
7944 static void tracing_err_log_show_pos(struct seq_file *m, u16 pos)
7945 {
7946 	u16 i;
7947 
7948 	for (i = 0; i < sizeof(CMD_PREFIX) - 1; i++)
7949 		seq_putc(m, ' ');
7950 	for (i = 0; i < pos; i++)
7951 		seq_putc(m, ' ');
7952 	seq_puts(m, "^\n");
7953 }
7954 
7955 static int tracing_err_log_seq_show(struct seq_file *m, void *v)
7956 {
7957 	struct tracing_log_err *err = v;
7958 
7959 	if (err) {
7960 		const char *err_text = err->info.errs[err->info.type];
7961 		u64 sec = err->info.ts;
7962 		u32 nsec;
7963 
7964 		nsec = do_div(sec, NSEC_PER_SEC);
7965 		seq_printf(m, "[%5llu.%06u] %s%s", sec, nsec / 1000,
7966 			   err->loc, err_text);
7967 		seq_printf(m, "%s", err->cmd);
7968 		tracing_err_log_show_pos(m, err->info.pos);
7969 	}
7970 
7971 	return 0;
7972 }
7973 
7974 static const struct seq_operations tracing_err_log_seq_ops = {
7975 	.start  = tracing_err_log_seq_start,
7976 	.next   = tracing_err_log_seq_next,
7977 	.stop   = tracing_err_log_seq_stop,
7978 	.show   = tracing_err_log_seq_show
7979 };
7980 
7981 static int tracing_err_log_open(struct inode *inode, struct file *file)
7982 {
7983 	struct trace_array *tr = inode->i_private;
7984 	int ret = 0;
7985 
7986 	ret = tracing_check_open_get_tr(tr);
7987 	if (ret)
7988 		return ret;
7989 
7990 	/* If this file was opened for write, then erase contents */
7991 	if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC))
7992 		clear_tracing_err_log(tr);
7993 
7994 	if (file->f_mode & FMODE_READ) {
7995 		ret = seq_open(file, &tracing_err_log_seq_ops);
7996 		if (!ret) {
7997 			struct seq_file *m = file->private_data;
7998 			m->private = tr;
7999 		} else {
8000 			trace_array_put(tr);
8001 		}
8002 	}
8003 	return ret;
8004 }
8005 
8006 static ssize_t tracing_err_log_write(struct file *file,
8007 				     const char __user *buffer,
8008 				     size_t count, loff_t *ppos)
8009 {
8010 	return count;
8011 }
8012 
8013 static int tracing_err_log_release(struct inode *inode, struct file *file)
8014 {
8015 	struct trace_array *tr = inode->i_private;
8016 
8017 	trace_array_put(tr);
8018 
8019 	if (file->f_mode & FMODE_READ)
8020 		seq_release(inode, file);
8021 
8022 	return 0;
8023 }
8024 
8025 static const struct file_operations tracing_err_log_fops = {
8026 	.open           = tracing_err_log_open,
8027 	.write		= tracing_err_log_write,
8028 	.read           = seq_read,
8029 	.llseek         = tracing_lseek,
8030 	.release        = tracing_err_log_release,
8031 };
8032 
8033 static int tracing_buffers_open(struct inode *inode, struct file *filp)
8034 {
8035 	struct trace_array *tr = inode->i_private;
8036 	struct ftrace_buffer_info *info;
8037 	int ret;
8038 
8039 	ret = tracing_check_open_get_tr(tr);
8040 	if (ret)
8041 		return ret;
8042 
8043 	info = kvzalloc(sizeof(*info), GFP_KERNEL);
8044 	if (!info) {
8045 		trace_array_put(tr);
8046 		return -ENOMEM;
8047 	}
8048 
8049 	mutex_lock(&trace_types_lock);
8050 
8051 	info->iter.tr		= tr;
8052 	info->iter.cpu_file	= tracing_get_cpu(inode);
8053 	info->iter.trace	= tr->current_trace;
8054 	info->iter.array_buffer = &tr->array_buffer;
8055 	info->spare		= NULL;
8056 	/* Force reading ring buffer for first read */
8057 	info->read		= (unsigned int)-1;
8058 
8059 	filp->private_data = info;
8060 
8061 	tr->trace_ref++;
8062 
8063 	mutex_unlock(&trace_types_lock);
8064 
8065 	ret = nonseekable_open(inode, filp);
8066 	if (ret < 0)
8067 		trace_array_put(tr);
8068 
8069 	return ret;
8070 }
8071 
8072 static __poll_t
8073 tracing_buffers_poll(struct file *filp, poll_table *poll_table)
8074 {
8075 	struct ftrace_buffer_info *info = filp->private_data;
8076 	struct trace_iterator *iter = &info->iter;
8077 
8078 	return trace_poll(iter, filp, poll_table);
8079 }
8080 
8081 static ssize_t
8082 tracing_buffers_read(struct file *filp, char __user *ubuf,
8083 		     size_t count, loff_t *ppos)
8084 {
8085 	struct ftrace_buffer_info *info = filp->private_data;
8086 	struct trace_iterator *iter = &info->iter;
8087 	void *trace_data;
8088 	int page_size;
8089 	ssize_t ret = 0;
8090 	ssize_t size;
8091 
8092 	if (!count)
8093 		return 0;
8094 
8095 #ifdef CONFIG_TRACER_MAX_TRACE
8096 	if (iter->snapshot && iter->tr->current_trace->use_max_tr)
8097 		return -EBUSY;
8098 #endif
8099 
8100 	page_size = ring_buffer_subbuf_size_get(iter->array_buffer->buffer);
8101 
8102 	/* Make sure the spare matches the current sub buffer size */
8103 	if (info->spare) {
8104 		if (page_size != info->spare_size) {
8105 			ring_buffer_free_read_page(iter->array_buffer->buffer,
8106 						   info->spare_cpu, info->spare);
8107 			info->spare = NULL;
8108 		}
8109 	}
8110 
8111 	if (!info->spare) {
8112 		info->spare = ring_buffer_alloc_read_page(iter->array_buffer->buffer,
8113 							  iter->cpu_file);
8114 		if (IS_ERR(info->spare)) {
8115 			ret = PTR_ERR(info->spare);
8116 			info->spare = NULL;
8117 		} else {
8118 			info->spare_cpu = iter->cpu_file;
8119 			info->spare_size = page_size;
8120 		}
8121 	}
8122 	if (!info->spare)
8123 		return ret;
8124 
8125 	/* Do we have previous read data to read? */
8126 	if (info->read < page_size)
8127 		goto read;
8128 
8129  again:
8130 	trace_access_lock(iter->cpu_file);
8131 	ret = ring_buffer_read_page(iter->array_buffer->buffer,
8132 				    info->spare,
8133 				    count,
8134 				    iter->cpu_file, 0);
8135 	trace_access_unlock(iter->cpu_file);
8136 
8137 	if (ret < 0) {
8138 		if (trace_empty(iter) && !iter->closed) {
8139 			if ((filp->f_flags & O_NONBLOCK))
8140 				return -EAGAIN;
8141 
8142 			ret = wait_on_pipe(iter, 0);
8143 			if (ret)
8144 				return ret;
8145 
8146 			goto again;
8147 		}
8148 		return 0;
8149 	}
8150 
8151 	info->read = 0;
8152  read:
8153 	size = page_size - info->read;
8154 	if (size > count)
8155 		size = count;
8156 	trace_data = ring_buffer_read_page_data(info->spare);
8157 	ret = copy_to_user(ubuf, trace_data + info->read, size);
8158 	if (ret == size)
8159 		return -EFAULT;
8160 
8161 	size -= ret;
8162 
8163 	*ppos += size;
8164 	info->read += size;
8165 
8166 	return size;
8167 }
8168 
8169 static int tracing_buffers_flush(struct file *file, fl_owner_t id)
8170 {
8171 	struct ftrace_buffer_info *info = file->private_data;
8172 	struct trace_iterator *iter = &info->iter;
8173 
8174 	iter->closed = true;
8175 	/* Make sure the waiters see the new wait_index */
8176 	(void)atomic_fetch_inc_release(&iter->wait_index);
8177 
8178 	ring_buffer_wake_waiters(iter->array_buffer->buffer, iter->cpu_file);
8179 
8180 	return 0;
8181 }
8182 
8183 static int tracing_buffers_release(struct inode *inode, struct file *file)
8184 {
8185 	struct ftrace_buffer_info *info = file->private_data;
8186 	struct trace_iterator *iter = &info->iter;
8187 
8188 	mutex_lock(&trace_types_lock);
8189 
8190 	iter->tr->trace_ref--;
8191 
8192 	__trace_array_put(iter->tr);
8193 
8194 	if (info->spare)
8195 		ring_buffer_free_read_page(iter->array_buffer->buffer,
8196 					   info->spare_cpu, info->spare);
8197 	kvfree(info);
8198 
8199 	mutex_unlock(&trace_types_lock);
8200 
8201 	return 0;
8202 }
8203 
8204 struct buffer_ref {
8205 	struct trace_buffer	*buffer;
8206 	void			*page;
8207 	int			cpu;
8208 	refcount_t		refcount;
8209 };
8210 
8211 static void buffer_ref_release(struct buffer_ref *ref)
8212 {
8213 	if (!refcount_dec_and_test(&ref->refcount))
8214 		return;
8215 	ring_buffer_free_read_page(ref->buffer, ref->cpu, ref->page);
8216 	kfree(ref);
8217 }
8218 
8219 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
8220 				    struct pipe_buffer *buf)
8221 {
8222 	struct buffer_ref *ref = (struct buffer_ref *)buf->private;
8223 
8224 	buffer_ref_release(ref);
8225 	buf->private = 0;
8226 }
8227 
8228 static bool buffer_pipe_buf_get(struct pipe_inode_info *pipe,
8229 				struct pipe_buffer *buf)
8230 {
8231 	struct buffer_ref *ref = (struct buffer_ref *)buf->private;
8232 
8233 	if (refcount_read(&ref->refcount) > INT_MAX/2)
8234 		return false;
8235 
8236 	refcount_inc(&ref->refcount);
8237 	return true;
8238 }
8239 
8240 /* Pipe buffer operations for a buffer. */
8241 static const struct pipe_buf_operations buffer_pipe_buf_ops = {
8242 	.release		= buffer_pipe_buf_release,
8243 	.get			= buffer_pipe_buf_get,
8244 };
8245 
8246 /*
8247  * Callback from splice_to_pipe(), if we need to release some pages
8248  * at the end of the spd in case we error'ed out in filling the pipe.
8249  */
8250 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
8251 {
8252 	struct buffer_ref *ref =
8253 		(struct buffer_ref *)spd->partial[i].private;
8254 
8255 	buffer_ref_release(ref);
8256 	spd->partial[i].private = 0;
8257 }
8258 
8259 static ssize_t
8260 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
8261 			    struct pipe_inode_info *pipe, size_t len,
8262 			    unsigned int flags)
8263 {
8264 	struct ftrace_buffer_info *info = file->private_data;
8265 	struct trace_iterator *iter = &info->iter;
8266 	struct partial_page partial_def[PIPE_DEF_BUFFERS];
8267 	struct page *pages_def[PIPE_DEF_BUFFERS];
8268 	struct splice_pipe_desc spd = {
8269 		.pages		= pages_def,
8270 		.partial	= partial_def,
8271 		.nr_pages_max	= PIPE_DEF_BUFFERS,
8272 		.ops		= &buffer_pipe_buf_ops,
8273 		.spd_release	= buffer_spd_release,
8274 	};
8275 	struct buffer_ref *ref;
8276 	bool woken = false;
8277 	int page_size;
8278 	int entries, i;
8279 	ssize_t ret = 0;
8280 
8281 #ifdef CONFIG_TRACER_MAX_TRACE
8282 	if (iter->snapshot && iter->tr->current_trace->use_max_tr)
8283 		return -EBUSY;
8284 #endif
8285 
8286 	page_size = ring_buffer_subbuf_size_get(iter->array_buffer->buffer);
8287 	if (*ppos & (page_size - 1))
8288 		return -EINVAL;
8289 
8290 	if (len & (page_size - 1)) {
8291 		if (len < page_size)
8292 			return -EINVAL;
8293 		len &= (~(page_size - 1));
8294 	}
8295 
8296 	if (splice_grow_spd(pipe, &spd))
8297 		return -ENOMEM;
8298 
8299  again:
8300 	trace_access_lock(iter->cpu_file);
8301 	entries = ring_buffer_entries_cpu(iter->array_buffer->buffer, iter->cpu_file);
8302 
8303 	for (i = 0; i < spd.nr_pages_max && len && entries; i++, len -= page_size) {
8304 		struct page *page;
8305 		int r;
8306 
8307 		ref = kzalloc(sizeof(*ref), GFP_KERNEL);
8308 		if (!ref) {
8309 			ret = -ENOMEM;
8310 			break;
8311 		}
8312 
8313 		refcount_set(&ref->refcount, 1);
8314 		ref->buffer = iter->array_buffer->buffer;
8315 		ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
8316 		if (IS_ERR(ref->page)) {
8317 			ret = PTR_ERR(ref->page);
8318 			ref->page = NULL;
8319 			kfree(ref);
8320 			break;
8321 		}
8322 		ref->cpu = iter->cpu_file;
8323 
8324 		r = ring_buffer_read_page(ref->buffer, ref->page,
8325 					  len, iter->cpu_file, 1);
8326 		if (r < 0) {
8327 			ring_buffer_free_read_page(ref->buffer, ref->cpu,
8328 						   ref->page);
8329 			kfree(ref);
8330 			break;
8331 		}
8332 
8333 		page = virt_to_page(ring_buffer_read_page_data(ref->page));
8334 
8335 		spd.pages[i] = page;
8336 		spd.partial[i].len = page_size;
8337 		spd.partial[i].offset = 0;
8338 		spd.partial[i].private = (unsigned long)ref;
8339 		spd.nr_pages++;
8340 		*ppos += page_size;
8341 
8342 		entries = ring_buffer_entries_cpu(iter->array_buffer->buffer, iter->cpu_file);
8343 	}
8344 
8345 	trace_access_unlock(iter->cpu_file);
8346 	spd.nr_pages = i;
8347 
8348 	/* did we read anything? */
8349 	if (!spd.nr_pages) {
8350 
8351 		if (ret)
8352 			goto out;
8353 
8354 		if (woken)
8355 			goto out;
8356 
8357 		ret = -EAGAIN;
8358 		if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK))
8359 			goto out;
8360 
8361 		ret = wait_on_pipe(iter, iter->snapshot ? 0 : iter->tr->buffer_percent);
8362 		if (ret)
8363 			goto out;
8364 
8365 		/* No need to wait after waking up when tracing is off */
8366 		if (!tracer_tracing_is_on(iter->tr))
8367 			goto out;
8368 
8369 		/* Iterate one more time to collect any new data then exit */
8370 		woken = true;
8371 
8372 		goto again;
8373 	}
8374 
8375 	ret = splice_to_pipe(pipe, &spd);
8376 out:
8377 	splice_shrink_spd(&spd);
8378 
8379 	return ret;
8380 }
8381 
8382 static long tracing_buffers_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
8383 {
8384 	struct ftrace_buffer_info *info = file->private_data;
8385 	struct trace_iterator *iter = &info->iter;
8386 	int err;
8387 
8388 	if (cmd == TRACE_MMAP_IOCTL_GET_READER) {
8389 		if (!(file->f_flags & O_NONBLOCK)) {
8390 			err = ring_buffer_wait(iter->array_buffer->buffer,
8391 					       iter->cpu_file,
8392 					       iter->tr->buffer_percent,
8393 					       NULL, NULL);
8394 			if (err)
8395 				return err;
8396 		}
8397 
8398 		return ring_buffer_map_get_reader(iter->array_buffer->buffer,
8399 						  iter->cpu_file);
8400 	} else if (cmd) {
8401 		return -ENOTTY;
8402 	}
8403 
8404 	/*
8405 	 * An ioctl call with cmd 0 to the ring buffer file will wake up all
8406 	 * waiters
8407 	 */
8408 	mutex_lock(&trace_types_lock);
8409 
8410 	/* Make sure the waiters see the new wait_index */
8411 	(void)atomic_fetch_inc_release(&iter->wait_index);
8412 
8413 	ring_buffer_wake_waiters(iter->array_buffer->buffer, iter->cpu_file);
8414 
8415 	mutex_unlock(&trace_types_lock);
8416 	return 0;
8417 }
8418 
8419 #ifdef CONFIG_TRACER_MAX_TRACE
8420 static int get_snapshot_map(struct trace_array *tr)
8421 {
8422 	int err = 0;
8423 
8424 	/*
8425 	 * Called with mmap_lock held. lockdep would be unhappy if we would now
8426 	 * take trace_types_lock. Instead use the specific
8427 	 * snapshot_trigger_lock.
8428 	 */
8429 	spin_lock(&tr->snapshot_trigger_lock);
8430 
8431 	if (tr->snapshot || tr->mapped == UINT_MAX)
8432 		err = -EBUSY;
8433 	else
8434 		tr->mapped++;
8435 
8436 	spin_unlock(&tr->snapshot_trigger_lock);
8437 
8438 	/* Wait for update_max_tr() to observe iter->tr->mapped */
8439 	if (tr->mapped == 1)
8440 		synchronize_rcu();
8441 
8442 	return err;
8443 
8444 }
8445 static void put_snapshot_map(struct trace_array *tr)
8446 {
8447 	spin_lock(&tr->snapshot_trigger_lock);
8448 	if (!WARN_ON(!tr->mapped))
8449 		tr->mapped--;
8450 	spin_unlock(&tr->snapshot_trigger_lock);
8451 }
8452 #else
8453 static inline int get_snapshot_map(struct trace_array *tr) { return 0; }
8454 static inline void put_snapshot_map(struct trace_array *tr) { }
8455 #endif
8456 
8457 static void tracing_buffers_mmap_close(struct vm_area_struct *vma)
8458 {
8459 	struct ftrace_buffer_info *info = vma->vm_file->private_data;
8460 	struct trace_iterator *iter = &info->iter;
8461 
8462 	WARN_ON(ring_buffer_unmap(iter->array_buffer->buffer, iter->cpu_file));
8463 	put_snapshot_map(iter->tr);
8464 }
8465 
8466 static const struct vm_operations_struct tracing_buffers_vmops = {
8467 	.close		= tracing_buffers_mmap_close,
8468 };
8469 
8470 static int tracing_buffers_mmap(struct file *filp, struct vm_area_struct *vma)
8471 {
8472 	struct ftrace_buffer_info *info = filp->private_data;
8473 	struct trace_iterator *iter = &info->iter;
8474 	int ret = 0;
8475 
8476 	ret = get_snapshot_map(iter->tr);
8477 	if (ret)
8478 		return ret;
8479 
8480 	ret = ring_buffer_map(iter->array_buffer->buffer, iter->cpu_file, vma);
8481 	if (ret)
8482 		put_snapshot_map(iter->tr);
8483 
8484 	vma->vm_ops = &tracing_buffers_vmops;
8485 
8486 	return ret;
8487 }
8488 
8489 static const struct file_operations tracing_buffers_fops = {
8490 	.open		= tracing_buffers_open,
8491 	.read		= tracing_buffers_read,
8492 	.poll		= tracing_buffers_poll,
8493 	.release	= tracing_buffers_release,
8494 	.flush		= tracing_buffers_flush,
8495 	.splice_read	= tracing_buffers_splice_read,
8496 	.unlocked_ioctl = tracing_buffers_ioctl,
8497 	.mmap		= tracing_buffers_mmap,
8498 };
8499 
8500 static ssize_t
8501 tracing_stats_read(struct file *filp, char __user *ubuf,
8502 		   size_t count, loff_t *ppos)
8503 {
8504 	struct inode *inode = file_inode(filp);
8505 	struct trace_array *tr = inode->i_private;
8506 	struct array_buffer *trace_buf = &tr->array_buffer;
8507 	int cpu = tracing_get_cpu(inode);
8508 	struct trace_seq *s;
8509 	unsigned long cnt;
8510 	unsigned long long t;
8511 	unsigned long usec_rem;
8512 
8513 	s = kmalloc(sizeof(*s), GFP_KERNEL);
8514 	if (!s)
8515 		return -ENOMEM;
8516 
8517 	trace_seq_init(s);
8518 
8519 	cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
8520 	trace_seq_printf(s, "entries: %ld\n", cnt);
8521 
8522 	cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
8523 	trace_seq_printf(s, "overrun: %ld\n", cnt);
8524 
8525 	cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
8526 	trace_seq_printf(s, "commit overrun: %ld\n", cnt);
8527 
8528 	cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
8529 	trace_seq_printf(s, "bytes: %ld\n", cnt);
8530 
8531 	if (trace_clocks[tr->clock_id].in_ns) {
8532 		/* local or global for trace_clock */
8533 		t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
8534 		usec_rem = do_div(t, USEC_PER_SEC);
8535 		trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
8536 								t, usec_rem);
8537 
8538 		t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer));
8539 		usec_rem = do_div(t, USEC_PER_SEC);
8540 		trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
8541 	} else {
8542 		/* counter or tsc mode for trace_clock */
8543 		trace_seq_printf(s, "oldest event ts: %llu\n",
8544 				ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
8545 
8546 		trace_seq_printf(s, "now ts: %llu\n",
8547 				ring_buffer_time_stamp(trace_buf->buffer));
8548 	}
8549 
8550 	cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
8551 	trace_seq_printf(s, "dropped events: %ld\n", cnt);
8552 
8553 	cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
8554 	trace_seq_printf(s, "read events: %ld\n", cnt);
8555 
8556 	count = simple_read_from_buffer(ubuf, count, ppos,
8557 					s->buffer, trace_seq_used(s));
8558 
8559 	kfree(s);
8560 
8561 	return count;
8562 }
8563 
8564 static const struct file_operations tracing_stats_fops = {
8565 	.open		= tracing_open_generic_tr,
8566 	.read		= tracing_stats_read,
8567 	.llseek		= generic_file_llseek,
8568 	.release	= tracing_release_generic_tr,
8569 };
8570 
8571 #ifdef CONFIG_DYNAMIC_FTRACE
8572 
8573 static ssize_t
8574 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
8575 		  size_t cnt, loff_t *ppos)
8576 {
8577 	ssize_t ret;
8578 	char *buf;
8579 	int r;
8580 
8581 	/* 512 should be plenty to hold the amount needed */
8582 #define DYN_INFO_BUF_SIZE	512
8583 
8584 	buf = kmalloc(DYN_INFO_BUF_SIZE, GFP_KERNEL);
8585 	if (!buf)
8586 		return -ENOMEM;
8587 
8588 	r = scnprintf(buf, DYN_INFO_BUF_SIZE,
8589 		      "%ld pages:%ld groups: %ld\n"
8590 		      "ftrace boot update time = %llu (ns)\n"
8591 		      "ftrace module total update time = %llu (ns)\n",
8592 		      ftrace_update_tot_cnt,
8593 		      ftrace_number_of_pages,
8594 		      ftrace_number_of_groups,
8595 		      ftrace_update_time,
8596 		      ftrace_total_mod_time);
8597 
8598 	ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
8599 	kfree(buf);
8600 	return ret;
8601 }
8602 
8603 static const struct file_operations tracing_dyn_info_fops = {
8604 	.open		= tracing_open_generic,
8605 	.read		= tracing_read_dyn_info,
8606 	.llseek		= generic_file_llseek,
8607 };
8608 #endif /* CONFIG_DYNAMIC_FTRACE */
8609 
8610 #if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
8611 static void
8612 ftrace_snapshot(unsigned long ip, unsigned long parent_ip,
8613 		struct trace_array *tr, struct ftrace_probe_ops *ops,
8614 		void *data)
8615 {
8616 	tracing_snapshot_instance(tr);
8617 }
8618 
8619 static void
8620 ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip,
8621 		      struct trace_array *tr, struct ftrace_probe_ops *ops,
8622 		      void *data)
8623 {
8624 	struct ftrace_func_mapper *mapper = data;
8625 	long *count = NULL;
8626 
8627 	if (mapper)
8628 		count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
8629 
8630 	if (count) {
8631 
8632 		if (*count <= 0)
8633 			return;
8634 
8635 		(*count)--;
8636 	}
8637 
8638 	tracing_snapshot_instance(tr);
8639 }
8640 
8641 static int
8642 ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
8643 		      struct ftrace_probe_ops *ops, void *data)
8644 {
8645 	struct ftrace_func_mapper *mapper = data;
8646 	long *count = NULL;
8647 
8648 	seq_printf(m, "%ps:", (void *)ip);
8649 
8650 	seq_puts(m, "snapshot");
8651 
8652 	if (mapper)
8653 		count = (long *)ftrace_func_mapper_find_ip(mapper, ip);
8654 
8655 	if (count)
8656 		seq_printf(m, ":count=%ld\n", *count);
8657 	else
8658 		seq_puts(m, ":unlimited\n");
8659 
8660 	return 0;
8661 }
8662 
8663 static int
8664 ftrace_snapshot_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
8665 		     unsigned long ip, void *init_data, void **data)
8666 {
8667 	struct ftrace_func_mapper *mapper = *data;
8668 
8669 	if (!mapper) {
8670 		mapper = allocate_ftrace_func_mapper();
8671 		if (!mapper)
8672 			return -ENOMEM;
8673 		*data = mapper;
8674 	}
8675 
8676 	return ftrace_func_mapper_add_ip(mapper, ip, init_data);
8677 }
8678 
8679 static void
8680 ftrace_snapshot_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
8681 		     unsigned long ip, void *data)
8682 {
8683 	struct ftrace_func_mapper *mapper = data;
8684 
8685 	if (!ip) {
8686 		if (!mapper)
8687 			return;
8688 		free_ftrace_func_mapper(mapper, NULL);
8689 		return;
8690 	}
8691 
8692 	ftrace_func_mapper_remove_ip(mapper, ip);
8693 }
8694 
8695 static struct ftrace_probe_ops snapshot_probe_ops = {
8696 	.func			= ftrace_snapshot,
8697 	.print			= ftrace_snapshot_print,
8698 };
8699 
8700 static struct ftrace_probe_ops snapshot_count_probe_ops = {
8701 	.func			= ftrace_count_snapshot,
8702 	.print			= ftrace_snapshot_print,
8703 	.init			= ftrace_snapshot_init,
8704 	.free			= ftrace_snapshot_free,
8705 };
8706 
8707 static int
8708 ftrace_trace_snapshot_callback(struct trace_array *tr, struct ftrace_hash *hash,
8709 			       char *glob, char *cmd, char *param, int enable)
8710 {
8711 	struct ftrace_probe_ops *ops;
8712 	void *count = (void *)-1;
8713 	char *number;
8714 	int ret;
8715 
8716 	if (!tr)
8717 		return -ENODEV;
8718 
8719 	/* hash funcs only work with set_ftrace_filter */
8720 	if (!enable)
8721 		return -EINVAL;
8722 
8723 	ops = param ? &snapshot_count_probe_ops :  &snapshot_probe_ops;
8724 
8725 	if (glob[0] == '!') {
8726 		ret = unregister_ftrace_function_probe_func(glob+1, tr, ops);
8727 		if (!ret)
8728 			tracing_disarm_snapshot(tr);
8729 
8730 		return ret;
8731 	}
8732 
8733 	if (!param)
8734 		goto out_reg;
8735 
8736 	number = strsep(&param, ":");
8737 
8738 	if (!strlen(number))
8739 		goto out_reg;
8740 
8741 	/*
8742 	 * We use the callback data field (which is a pointer)
8743 	 * as our counter.
8744 	 */
8745 	ret = kstrtoul(number, 0, (unsigned long *)&count);
8746 	if (ret)
8747 		return ret;
8748 
8749  out_reg:
8750 	ret = tracing_arm_snapshot(tr);
8751 	if (ret < 0)
8752 		goto out;
8753 
8754 	ret = register_ftrace_function_probe(glob, tr, ops, count);
8755 	if (ret < 0)
8756 		tracing_disarm_snapshot(tr);
8757  out:
8758 	return ret < 0 ? ret : 0;
8759 }
8760 
8761 static struct ftrace_func_command ftrace_snapshot_cmd = {
8762 	.name			= "snapshot",
8763 	.func			= ftrace_trace_snapshot_callback,
8764 };
8765 
8766 static __init int register_snapshot_cmd(void)
8767 {
8768 	return register_ftrace_command(&ftrace_snapshot_cmd);
8769 }
8770 #else
8771 static inline __init int register_snapshot_cmd(void) { return 0; }
8772 #endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
8773 
8774 static struct dentry *tracing_get_dentry(struct trace_array *tr)
8775 {
8776 	if (WARN_ON(!tr->dir))
8777 		return ERR_PTR(-ENODEV);
8778 
8779 	/* Top directory uses NULL as the parent */
8780 	if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
8781 		return NULL;
8782 
8783 	/* All sub buffers have a descriptor */
8784 	return tr->dir;
8785 }
8786 
8787 static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
8788 {
8789 	struct dentry *d_tracer;
8790 
8791 	if (tr->percpu_dir)
8792 		return tr->percpu_dir;
8793 
8794 	d_tracer = tracing_get_dentry(tr);
8795 	if (IS_ERR(d_tracer))
8796 		return NULL;
8797 
8798 	tr->percpu_dir = tracefs_create_dir("per_cpu", d_tracer);
8799 
8800 	MEM_FAIL(!tr->percpu_dir,
8801 		  "Could not create tracefs directory 'per_cpu/%d'\n", cpu);
8802 
8803 	return tr->percpu_dir;
8804 }
8805 
8806 static struct dentry *
8807 trace_create_cpu_file(const char *name, umode_t mode, struct dentry *parent,
8808 		      void *data, long cpu, const struct file_operations *fops)
8809 {
8810 	struct dentry *ret = trace_create_file(name, mode, parent, data, fops);
8811 
8812 	if (ret) /* See tracing_get_cpu() */
8813 		d_inode(ret)->i_cdev = (void *)(cpu + 1);
8814 	return ret;
8815 }
8816 
8817 static void
8818 tracing_init_tracefs_percpu(struct trace_array *tr, long cpu)
8819 {
8820 	struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
8821 	struct dentry *d_cpu;
8822 	char cpu_dir[30]; /* 30 characters should be more than enough */
8823 
8824 	if (!d_percpu)
8825 		return;
8826 
8827 	snprintf(cpu_dir, 30, "cpu%ld", cpu);
8828 	d_cpu = tracefs_create_dir(cpu_dir, d_percpu);
8829 	if (!d_cpu) {
8830 		pr_warn("Could not create tracefs '%s' entry\n", cpu_dir);
8831 		return;
8832 	}
8833 
8834 	/* per cpu trace_pipe */
8835 	trace_create_cpu_file("trace_pipe", TRACE_MODE_READ, d_cpu,
8836 				tr, cpu, &tracing_pipe_fops);
8837 
8838 	/* per cpu trace */
8839 	trace_create_cpu_file("trace", TRACE_MODE_WRITE, d_cpu,
8840 				tr, cpu, &tracing_fops);
8841 
8842 	trace_create_cpu_file("trace_pipe_raw", TRACE_MODE_READ, d_cpu,
8843 				tr, cpu, &tracing_buffers_fops);
8844 
8845 	trace_create_cpu_file("stats", TRACE_MODE_READ, d_cpu,
8846 				tr, cpu, &tracing_stats_fops);
8847 
8848 	trace_create_cpu_file("buffer_size_kb", TRACE_MODE_READ, d_cpu,
8849 				tr, cpu, &tracing_entries_fops);
8850 
8851 	if (tr->range_addr_start)
8852 		trace_create_cpu_file("buffer_meta", TRACE_MODE_READ, d_cpu,
8853 				      tr, cpu, &tracing_buffer_meta_fops);
8854 #ifdef CONFIG_TRACER_SNAPSHOT
8855 	if (!tr->range_addr_start) {
8856 		trace_create_cpu_file("snapshot", TRACE_MODE_WRITE, d_cpu,
8857 				      tr, cpu, &snapshot_fops);
8858 
8859 		trace_create_cpu_file("snapshot_raw", TRACE_MODE_READ, d_cpu,
8860 				      tr, cpu, &snapshot_raw_fops);
8861 	}
8862 #endif
8863 }
8864 
8865 #ifdef CONFIG_FTRACE_SELFTEST
8866 /* Let selftest have access to static functions in this file */
8867 #include "trace_selftest.c"
8868 #endif
8869 
8870 static ssize_t
8871 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
8872 			loff_t *ppos)
8873 {
8874 	struct trace_option_dentry *topt = filp->private_data;
8875 	char *buf;
8876 
8877 	if (topt->flags->val & topt->opt->bit)
8878 		buf = "1\n";
8879 	else
8880 		buf = "0\n";
8881 
8882 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
8883 }
8884 
8885 static ssize_t
8886 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
8887 			 loff_t *ppos)
8888 {
8889 	struct trace_option_dentry *topt = filp->private_data;
8890 	unsigned long val;
8891 	int ret;
8892 
8893 	ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
8894 	if (ret)
8895 		return ret;
8896 
8897 	if (val != 0 && val != 1)
8898 		return -EINVAL;
8899 
8900 	if (!!(topt->flags->val & topt->opt->bit) != val) {
8901 		mutex_lock(&trace_types_lock);
8902 		ret = __set_tracer_option(topt->tr, topt->flags,
8903 					  topt->opt, !val);
8904 		mutex_unlock(&trace_types_lock);
8905 		if (ret)
8906 			return ret;
8907 	}
8908 
8909 	*ppos += cnt;
8910 
8911 	return cnt;
8912 }
8913 
8914 static int tracing_open_options(struct inode *inode, struct file *filp)
8915 {
8916 	struct trace_option_dentry *topt = inode->i_private;
8917 	int ret;
8918 
8919 	ret = tracing_check_open_get_tr(topt->tr);
8920 	if (ret)
8921 		return ret;
8922 
8923 	filp->private_data = inode->i_private;
8924 	return 0;
8925 }
8926 
8927 static int tracing_release_options(struct inode *inode, struct file *file)
8928 {
8929 	struct trace_option_dentry *topt = file->private_data;
8930 
8931 	trace_array_put(topt->tr);
8932 	return 0;
8933 }
8934 
8935 static const struct file_operations trace_options_fops = {
8936 	.open = tracing_open_options,
8937 	.read = trace_options_read,
8938 	.write = trace_options_write,
8939 	.llseek	= generic_file_llseek,
8940 	.release = tracing_release_options,
8941 };
8942 
8943 /*
8944  * In order to pass in both the trace_array descriptor as well as the index
8945  * to the flag that the trace option file represents, the trace_array
8946  * has a character array of trace_flags_index[], which holds the index
8947  * of the bit for the flag it represents. index[0] == 0, index[1] == 1, etc.
8948  * The address of this character array is passed to the flag option file
8949  * read/write callbacks.
8950  *
8951  * In order to extract both the index and the trace_array descriptor,
8952  * get_tr_index() uses the following algorithm.
8953  *
8954  *   idx = *ptr;
8955  *
8956  * As the pointer itself contains the address of the index (remember
8957  * index[1] == 1).
8958  *
8959  * Then to get the trace_array descriptor, by subtracting that index
8960  * from the ptr, we get to the start of the index itself.
8961  *
8962  *   ptr - idx == &index[0]
8963  *
8964  * Then a simple container_of() from that pointer gets us to the
8965  * trace_array descriptor.
8966  */
8967 static void get_tr_index(void *data, struct trace_array **ptr,
8968 			 unsigned int *pindex)
8969 {
8970 	*pindex = *(unsigned char *)data;
8971 
8972 	*ptr = container_of(data - *pindex, struct trace_array,
8973 			    trace_flags_index);
8974 }
8975 
8976 static ssize_t
8977 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
8978 			loff_t *ppos)
8979 {
8980 	void *tr_index = filp->private_data;
8981 	struct trace_array *tr;
8982 	unsigned int index;
8983 	char *buf;
8984 
8985 	get_tr_index(tr_index, &tr, &index);
8986 
8987 	if (tr->trace_flags & (1 << index))
8988 		buf = "1\n";
8989 	else
8990 		buf = "0\n";
8991 
8992 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
8993 }
8994 
8995 static ssize_t
8996 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
8997 			 loff_t *ppos)
8998 {
8999 	void *tr_index = filp->private_data;
9000 	struct trace_array *tr;
9001 	unsigned int index;
9002 	unsigned long val;
9003 	int ret;
9004 
9005 	get_tr_index(tr_index, &tr, &index);
9006 
9007 	ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
9008 	if (ret)
9009 		return ret;
9010 
9011 	if (val != 0 && val != 1)
9012 		return -EINVAL;
9013 
9014 	mutex_lock(&event_mutex);
9015 	mutex_lock(&trace_types_lock);
9016 	ret = set_tracer_flag(tr, 1 << index, val);
9017 	mutex_unlock(&trace_types_lock);
9018 	mutex_unlock(&event_mutex);
9019 
9020 	if (ret < 0)
9021 		return ret;
9022 
9023 	*ppos += cnt;
9024 
9025 	return cnt;
9026 }
9027 
9028 static const struct file_operations trace_options_core_fops = {
9029 	.open = tracing_open_generic,
9030 	.read = trace_options_core_read,
9031 	.write = trace_options_core_write,
9032 	.llseek = generic_file_llseek,
9033 };
9034 
9035 struct dentry *trace_create_file(const char *name,
9036 				 umode_t mode,
9037 				 struct dentry *parent,
9038 				 void *data,
9039 				 const struct file_operations *fops)
9040 {
9041 	struct dentry *ret;
9042 
9043 	ret = tracefs_create_file(name, mode, parent, data, fops);
9044 	if (!ret)
9045 		pr_warn("Could not create tracefs '%s' entry\n", name);
9046 
9047 	return ret;
9048 }
9049 
9050 
9051 static struct dentry *trace_options_init_dentry(struct trace_array *tr)
9052 {
9053 	struct dentry *d_tracer;
9054 
9055 	if (tr->options)
9056 		return tr->options;
9057 
9058 	d_tracer = tracing_get_dentry(tr);
9059 	if (IS_ERR(d_tracer))
9060 		return NULL;
9061 
9062 	tr->options = tracefs_create_dir("options", d_tracer);
9063 	if (!tr->options) {
9064 		pr_warn("Could not create tracefs directory 'options'\n");
9065 		return NULL;
9066 	}
9067 
9068 	return tr->options;
9069 }
9070 
9071 static void
9072 create_trace_option_file(struct trace_array *tr,
9073 			 struct trace_option_dentry *topt,
9074 			 struct tracer_flags *flags,
9075 			 struct tracer_opt *opt)
9076 {
9077 	struct dentry *t_options;
9078 
9079 	t_options = trace_options_init_dentry(tr);
9080 	if (!t_options)
9081 		return;
9082 
9083 	topt->flags = flags;
9084 	topt->opt = opt;
9085 	topt->tr = tr;
9086 
9087 	topt->entry = trace_create_file(opt->name, TRACE_MODE_WRITE,
9088 					t_options, topt, &trace_options_fops);
9089 
9090 }
9091 
9092 static void
9093 create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
9094 {
9095 	struct trace_option_dentry *topts;
9096 	struct trace_options *tr_topts;
9097 	struct tracer_flags *flags;
9098 	struct tracer_opt *opts;
9099 	int cnt;
9100 	int i;
9101 
9102 	if (!tracer)
9103 		return;
9104 
9105 	flags = tracer->flags;
9106 
9107 	if (!flags || !flags->opts)
9108 		return;
9109 
9110 	/*
9111 	 * If this is an instance, only create flags for tracers
9112 	 * the instance may have.
9113 	 */
9114 	if (!trace_ok_for_array(tracer, tr))
9115 		return;
9116 
9117 	for (i = 0; i < tr->nr_topts; i++) {
9118 		/* Make sure there's no duplicate flags. */
9119 		if (WARN_ON_ONCE(tr->topts[i].tracer->flags == tracer->flags))
9120 			return;
9121 	}
9122 
9123 	opts = flags->opts;
9124 
9125 	for (cnt = 0; opts[cnt].name; cnt++)
9126 		;
9127 
9128 	topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
9129 	if (!topts)
9130 		return;
9131 
9132 	tr_topts = krealloc(tr->topts, sizeof(*tr->topts) * (tr->nr_topts + 1),
9133 			    GFP_KERNEL);
9134 	if (!tr_topts) {
9135 		kfree(topts);
9136 		return;
9137 	}
9138 
9139 	tr->topts = tr_topts;
9140 	tr->topts[tr->nr_topts].tracer = tracer;
9141 	tr->topts[tr->nr_topts].topts = topts;
9142 	tr->nr_topts++;
9143 
9144 	for (cnt = 0; opts[cnt].name; cnt++) {
9145 		create_trace_option_file(tr, &topts[cnt], flags,
9146 					 &opts[cnt]);
9147 		MEM_FAIL(topts[cnt].entry == NULL,
9148 			  "Failed to create trace option: %s",
9149 			  opts[cnt].name);
9150 	}
9151 }
9152 
9153 static struct dentry *
9154 create_trace_option_core_file(struct trace_array *tr,
9155 			      const char *option, long index)
9156 {
9157 	struct dentry *t_options;
9158 
9159 	t_options = trace_options_init_dentry(tr);
9160 	if (!t_options)
9161 		return NULL;
9162 
9163 	return trace_create_file(option, TRACE_MODE_WRITE, t_options,
9164 				 (void *)&tr->trace_flags_index[index],
9165 				 &trace_options_core_fops);
9166 }
9167 
9168 static void create_trace_options_dir(struct trace_array *tr)
9169 {
9170 	struct dentry *t_options;
9171 	bool top_level = tr == &global_trace;
9172 	int i;
9173 
9174 	t_options = trace_options_init_dentry(tr);
9175 	if (!t_options)
9176 		return;
9177 
9178 	for (i = 0; trace_options[i]; i++) {
9179 		if (top_level ||
9180 		    !((1 << i) & TOP_LEVEL_TRACE_FLAGS))
9181 			create_trace_option_core_file(tr, trace_options[i], i);
9182 	}
9183 }
9184 
9185 static ssize_t
9186 rb_simple_read(struct file *filp, char __user *ubuf,
9187 	       size_t cnt, loff_t *ppos)
9188 {
9189 	struct trace_array *tr = filp->private_data;
9190 	char buf[64];
9191 	int r;
9192 
9193 	r = tracer_tracing_is_on(tr);
9194 	r = sprintf(buf, "%d\n", r);
9195 
9196 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
9197 }
9198 
9199 static ssize_t
9200 rb_simple_write(struct file *filp, const char __user *ubuf,
9201 		size_t cnt, loff_t *ppos)
9202 {
9203 	struct trace_array *tr = filp->private_data;
9204 	struct trace_buffer *buffer = tr->array_buffer.buffer;
9205 	unsigned long val;
9206 	int ret;
9207 
9208 	ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
9209 	if (ret)
9210 		return ret;
9211 
9212 	if (buffer) {
9213 		mutex_lock(&trace_types_lock);
9214 		if (!!val == tracer_tracing_is_on(tr)) {
9215 			val = 0; /* do nothing */
9216 		} else if (val) {
9217 			tracer_tracing_on(tr);
9218 			if (tr->current_trace->start)
9219 				tr->current_trace->start(tr);
9220 		} else {
9221 			tracer_tracing_off(tr);
9222 			if (tr->current_trace->stop)
9223 				tr->current_trace->stop(tr);
9224 			/* Wake up any waiters */
9225 			ring_buffer_wake_waiters(buffer, RING_BUFFER_ALL_CPUS);
9226 		}
9227 		mutex_unlock(&trace_types_lock);
9228 	}
9229 
9230 	(*ppos)++;
9231 
9232 	return cnt;
9233 }
9234 
9235 static const struct file_operations rb_simple_fops = {
9236 	.open		= tracing_open_generic_tr,
9237 	.read		= rb_simple_read,
9238 	.write		= rb_simple_write,
9239 	.release	= tracing_release_generic_tr,
9240 	.llseek		= default_llseek,
9241 };
9242 
9243 static ssize_t
9244 buffer_percent_read(struct file *filp, char __user *ubuf,
9245 		    size_t cnt, loff_t *ppos)
9246 {
9247 	struct trace_array *tr = filp->private_data;
9248 	char buf[64];
9249 	int r;
9250 
9251 	r = tr->buffer_percent;
9252 	r = sprintf(buf, "%d\n", r);
9253 
9254 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
9255 }
9256 
9257 static ssize_t
9258 buffer_percent_write(struct file *filp, const char __user *ubuf,
9259 		     size_t cnt, loff_t *ppos)
9260 {
9261 	struct trace_array *tr = filp->private_data;
9262 	unsigned long val;
9263 	int ret;
9264 
9265 	ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
9266 	if (ret)
9267 		return ret;
9268 
9269 	if (val > 100)
9270 		return -EINVAL;
9271 
9272 	tr->buffer_percent = val;
9273 
9274 	(*ppos)++;
9275 
9276 	return cnt;
9277 }
9278 
9279 static const struct file_operations buffer_percent_fops = {
9280 	.open		= tracing_open_generic_tr,
9281 	.read		= buffer_percent_read,
9282 	.write		= buffer_percent_write,
9283 	.release	= tracing_release_generic_tr,
9284 	.llseek		= default_llseek,
9285 };
9286 
9287 static ssize_t
9288 buffer_subbuf_size_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
9289 {
9290 	struct trace_array *tr = filp->private_data;
9291 	size_t size;
9292 	char buf[64];
9293 	int order;
9294 	int r;
9295 
9296 	order = ring_buffer_subbuf_order_get(tr->array_buffer.buffer);
9297 	size = (PAGE_SIZE << order) / 1024;
9298 
9299 	r = sprintf(buf, "%zd\n", size);
9300 
9301 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
9302 }
9303 
9304 static ssize_t
9305 buffer_subbuf_size_write(struct file *filp, const char __user *ubuf,
9306 			 size_t cnt, loff_t *ppos)
9307 {
9308 	struct trace_array *tr = filp->private_data;
9309 	unsigned long val;
9310 	int old_order;
9311 	int order;
9312 	int pages;
9313 	int ret;
9314 
9315 	ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
9316 	if (ret)
9317 		return ret;
9318 
9319 	val *= 1024; /* value passed in is in KB */
9320 
9321 	pages = DIV_ROUND_UP(val, PAGE_SIZE);
9322 	order = fls(pages - 1);
9323 
9324 	/* limit between 1 and 128 system pages */
9325 	if (order < 0 || order > 7)
9326 		return -EINVAL;
9327 
9328 	/* Do not allow tracing while changing the order of the ring buffer */
9329 	tracing_stop_tr(tr);
9330 
9331 	old_order = ring_buffer_subbuf_order_get(tr->array_buffer.buffer);
9332 	if (old_order == order)
9333 		goto out;
9334 
9335 	ret = ring_buffer_subbuf_order_set(tr->array_buffer.buffer, order);
9336 	if (ret)
9337 		goto out;
9338 
9339 #ifdef CONFIG_TRACER_MAX_TRACE
9340 
9341 	if (!tr->allocated_snapshot)
9342 		goto out_max;
9343 
9344 	ret = ring_buffer_subbuf_order_set(tr->max_buffer.buffer, order);
9345 	if (ret) {
9346 		/* Put back the old order */
9347 		cnt = ring_buffer_subbuf_order_set(tr->array_buffer.buffer, old_order);
9348 		if (WARN_ON_ONCE(cnt)) {
9349 			/*
9350 			 * AARGH! We are left with different orders!
9351 			 * The max buffer is our "snapshot" buffer.
9352 			 * When a tracer needs a snapshot (one of the
9353 			 * latency tracers), it swaps the max buffer
9354 			 * with the saved snap shot. We succeeded to
9355 			 * update the order of the main buffer, but failed to
9356 			 * update the order of the max buffer. But when we tried
9357 			 * to reset the main buffer to the original size, we
9358 			 * failed there too. This is very unlikely to
9359 			 * happen, but if it does, warn and kill all
9360 			 * tracing.
9361 			 */
9362 			tracing_disabled = 1;
9363 		}
9364 		goto out;
9365 	}
9366  out_max:
9367 #endif
9368 	(*ppos)++;
9369  out:
9370 	if (ret)
9371 		cnt = ret;
9372 	tracing_start_tr(tr);
9373 	return cnt;
9374 }
9375 
9376 static const struct file_operations buffer_subbuf_size_fops = {
9377 	.open		= tracing_open_generic_tr,
9378 	.read		= buffer_subbuf_size_read,
9379 	.write		= buffer_subbuf_size_write,
9380 	.release	= tracing_release_generic_tr,
9381 	.llseek		= default_llseek,
9382 };
9383 
9384 static struct dentry *trace_instance_dir;
9385 
9386 static void
9387 init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer);
9388 
9389 static int
9390 allocate_trace_buffer(struct trace_array *tr, struct array_buffer *buf, int size)
9391 {
9392 	enum ring_buffer_flags rb_flags;
9393 
9394 	rb_flags = tr->trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
9395 
9396 	buf->tr = tr;
9397 
9398 	if (tr->range_addr_start && tr->range_addr_size) {
9399 		buf->buffer = ring_buffer_alloc_range(size, rb_flags, 0,
9400 						      tr->range_addr_start,
9401 						      tr->range_addr_size);
9402 
9403 		ring_buffer_last_boot_delta(buf->buffer,
9404 					    &tr->text_delta, &tr->data_delta);
9405 		/*
9406 		 * This is basically the same as a mapped buffer,
9407 		 * with the same restrictions.
9408 		 */
9409 		tr->mapped++;
9410 	} else {
9411 		buf->buffer = ring_buffer_alloc(size, rb_flags);
9412 	}
9413 	if (!buf->buffer)
9414 		return -ENOMEM;
9415 
9416 	buf->data = alloc_percpu(struct trace_array_cpu);
9417 	if (!buf->data) {
9418 		ring_buffer_free(buf->buffer);
9419 		buf->buffer = NULL;
9420 		return -ENOMEM;
9421 	}
9422 
9423 	/* Allocate the first page for all buffers */
9424 	set_buffer_entries(&tr->array_buffer,
9425 			   ring_buffer_size(tr->array_buffer.buffer, 0));
9426 
9427 	return 0;
9428 }
9429 
9430 static void free_trace_buffer(struct array_buffer *buf)
9431 {
9432 	if (buf->buffer) {
9433 		ring_buffer_free(buf->buffer);
9434 		buf->buffer = NULL;
9435 		free_percpu(buf->data);
9436 		buf->data = NULL;
9437 	}
9438 }
9439 
9440 static int allocate_trace_buffers(struct trace_array *tr, int size)
9441 {
9442 	int ret;
9443 
9444 	ret = allocate_trace_buffer(tr, &tr->array_buffer, size);
9445 	if (ret)
9446 		return ret;
9447 
9448 #ifdef CONFIG_TRACER_MAX_TRACE
9449 	/* Fix mapped buffer trace arrays do not have snapshot buffers */
9450 	if (tr->range_addr_start)
9451 		return 0;
9452 
9453 	ret = allocate_trace_buffer(tr, &tr->max_buffer,
9454 				    allocate_snapshot ? size : 1);
9455 	if (MEM_FAIL(ret, "Failed to allocate trace buffer\n")) {
9456 		free_trace_buffer(&tr->array_buffer);
9457 		return -ENOMEM;
9458 	}
9459 	tr->allocated_snapshot = allocate_snapshot;
9460 
9461 	allocate_snapshot = false;
9462 #endif
9463 
9464 	return 0;
9465 }
9466 
9467 static void free_trace_buffers(struct trace_array *tr)
9468 {
9469 	if (!tr)
9470 		return;
9471 
9472 	free_trace_buffer(&tr->array_buffer);
9473 
9474 #ifdef CONFIG_TRACER_MAX_TRACE
9475 	free_trace_buffer(&tr->max_buffer);
9476 #endif
9477 }
9478 
9479 static void init_trace_flags_index(struct trace_array *tr)
9480 {
9481 	int i;
9482 
9483 	/* Used by the trace options files */
9484 	for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++)
9485 		tr->trace_flags_index[i] = i;
9486 }
9487 
9488 static void __update_tracer_options(struct trace_array *tr)
9489 {
9490 	struct tracer *t;
9491 
9492 	for (t = trace_types; t; t = t->next)
9493 		add_tracer_options(tr, t);
9494 }
9495 
9496 static void update_tracer_options(struct trace_array *tr)
9497 {
9498 	mutex_lock(&trace_types_lock);
9499 	tracer_options_updated = true;
9500 	__update_tracer_options(tr);
9501 	mutex_unlock(&trace_types_lock);
9502 }
9503 
9504 /* Must have trace_types_lock held */
9505 struct trace_array *trace_array_find(const char *instance)
9506 {
9507 	struct trace_array *tr, *found = NULL;
9508 
9509 	list_for_each_entry(tr, &ftrace_trace_arrays, list) {
9510 		if (tr->name && strcmp(tr->name, instance) == 0) {
9511 			found = tr;
9512 			break;
9513 		}
9514 	}
9515 
9516 	return found;
9517 }
9518 
9519 struct trace_array *trace_array_find_get(const char *instance)
9520 {
9521 	struct trace_array *tr;
9522 
9523 	mutex_lock(&trace_types_lock);
9524 	tr = trace_array_find(instance);
9525 	if (tr)
9526 		tr->ref++;
9527 	mutex_unlock(&trace_types_lock);
9528 
9529 	return tr;
9530 }
9531 
9532 static int trace_array_create_dir(struct trace_array *tr)
9533 {
9534 	int ret;
9535 
9536 	tr->dir = tracefs_create_dir(tr->name, trace_instance_dir);
9537 	if (!tr->dir)
9538 		return -EINVAL;
9539 
9540 	ret = event_trace_add_tracer(tr->dir, tr);
9541 	if (ret) {
9542 		tracefs_remove(tr->dir);
9543 		return ret;
9544 	}
9545 
9546 	init_tracer_tracefs(tr, tr->dir);
9547 	__update_tracer_options(tr);
9548 
9549 	return ret;
9550 }
9551 
9552 static struct trace_array *
9553 trace_array_create_systems(const char *name, const char *systems,
9554 			   unsigned long range_addr_start,
9555 			   unsigned long range_addr_size)
9556 {
9557 	struct trace_array *tr;
9558 	int ret;
9559 
9560 	ret = -ENOMEM;
9561 	tr = kzalloc(sizeof(*tr), GFP_KERNEL);
9562 	if (!tr)
9563 		return ERR_PTR(ret);
9564 
9565 	tr->name = kstrdup(name, GFP_KERNEL);
9566 	if (!tr->name)
9567 		goto out_free_tr;
9568 
9569 	if (!alloc_cpumask_var(&tr->tracing_cpumask, GFP_KERNEL))
9570 		goto out_free_tr;
9571 
9572 	if (!zalloc_cpumask_var(&tr->pipe_cpumask, GFP_KERNEL))
9573 		goto out_free_tr;
9574 
9575 	if (systems) {
9576 		tr->system_names = kstrdup_const(systems, GFP_KERNEL);
9577 		if (!tr->system_names)
9578 			goto out_free_tr;
9579 	}
9580 
9581 	/* Only for boot up memory mapped ring buffers */
9582 	tr->range_addr_start = range_addr_start;
9583 	tr->range_addr_size = range_addr_size;
9584 
9585 	tr->trace_flags = global_trace.trace_flags & ~ZEROED_TRACE_FLAGS;
9586 
9587 	cpumask_copy(tr->tracing_cpumask, cpu_all_mask);
9588 
9589 	raw_spin_lock_init(&tr->start_lock);
9590 
9591 	tr->max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
9592 #ifdef CONFIG_TRACER_MAX_TRACE
9593 	spin_lock_init(&tr->snapshot_trigger_lock);
9594 #endif
9595 	tr->current_trace = &nop_trace;
9596 
9597 	INIT_LIST_HEAD(&tr->systems);
9598 	INIT_LIST_HEAD(&tr->events);
9599 	INIT_LIST_HEAD(&tr->hist_vars);
9600 	INIT_LIST_HEAD(&tr->err_log);
9601 
9602 	if (allocate_trace_buffers(tr, trace_buf_size) < 0)
9603 		goto out_free_tr;
9604 
9605 	/* The ring buffer is defaultly expanded */
9606 	trace_set_ring_buffer_expanded(tr);
9607 
9608 	if (ftrace_allocate_ftrace_ops(tr) < 0)
9609 		goto out_free_tr;
9610 
9611 	ftrace_init_trace_array(tr);
9612 
9613 	init_trace_flags_index(tr);
9614 
9615 	if (trace_instance_dir) {
9616 		ret = trace_array_create_dir(tr);
9617 		if (ret)
9618 			goto out_free_tr;
9619 	} else
9620 		__trace_early_add_events(tr);
9621 
9622 	list_add(&tr->list, &ftrace_trace_arrays);
9623 
9624 	tr->ref++;
9625 
9626 	return tr;
9627 
9628  out_free_tr:
9629 	ftrace_free_ftrace_ops(tr);
9630 	free_trace_buffers(tr);
9631 	free_cpumask_var(tr->pipe_cpumask);
9632 	free_cpumask_var(tr->tracing_cpumask);
9633 	kfree_const(tr->system_names);
9634 	kfree(tr->name);
9635 	kfree(tr);
9636 
9637 	return ERR_PTR(ret);
9638 }
9639 
9640 static struct trace_array *trace_array_create(const char *name)
9641 {
9642 	return trace_array_create_systems(name, NULL, 0, 0);
9643 }
9644 
9645 static int instance_mkdir(const char *name)
9646 {
9647 	struct trace_array *tr;
9648 	int ret;
9649 
9650 	mutex_lock(&event_mutex);
9651 	mutex_lock(&trace_types_lock);
9652 
9653 	ret = -EEXIST;
9654 	if (trace_array_find(name))
9655 		goto out_unlock;
9656 
9657 	tr = trace_array_create(name);
9658 
9659 	ret = PTR_ERR_OR_ZERO(tr);
9660 
9661 out_unlock:
9662 	mutex_unlock(&trace_types_lock);
9663 	mutex_unlock(&event_mutex);
9664 	return ret;
9665 }
9666 
9667 static u64 map_pages(u64 start, u64 size)
9668 {
9669 	struct page **pages;
9670 	phys_addr_t page_start;
9671 	unsigned int page_count;
9672 	unsigned int i;
9673 	void *vaddr;
9674 
9675 	page_count = DIV_ROUND_UP(size, PAGE_SIZE);
9676 
9677 	page_start = start;
9678 	pages = kmalloc_array(page_count, sizeof(struct page *), GFP_KERNEL);
9679 	if (!pages)
9680 		return 0;
9681 
9682 	for (i = 0; i < page_count; i++) {
9683 		phys_addr_t addr = page_start + i * PAGE_SIZE;
9684 		pages[i] = pfn_to_page(addr >> PAGE_SHIFT);
9685 	}
9686 	vaddr = vmap(pages, page_count, VM_MAP, PAGE_KERNEL);
9687 	kfree(pages);
9688 
9689 	return (u64)(unsigned long)vaddr;
9690 }
9691 
9692 /**
9693  * trace_array_get_by_name - Create/Lookup a trace array, given its name.
9694  * @name: The name of the trace array to be looked up/created.
9695  * @systems: A list of systems to create event directories for (NULL for all)
9696  *
9697  * Returns pointer to trace array with given name.
9698  * NULL, if it cannot be created.
9699  *
9700  * NOTE: This function increments the reference counter associated with the
9701  * trace array returned. This makes sure it cannot be freed while in use.
9702  * Use trace_array_put() once the trace array is no longer needed.
9703  * If the trace_array is to be freed, trace_array_destroy() needs to
9704  * be called after the trace_array_put(), or simply let user space delete
9705  * it from the tracefs instances directory. But until the
9706  * trace_array_put() is called, user space can not delete it.
9707  *
9708  */
9709 struct trace_array *trace_array_get_by_name(const char *name, const char *systems)
9710 {
9711 	struct trace_array *tr;
9712 
9713 	mutex_lock(&event_mutex);
9714 	mutex_lock(&trace_types_lock);
9715 
9716 	list_for_each_entry(tr, &ftrace_trace_arrays, list) {
9717 		if (tr->name && strcmp(tr->name, name) == 0)
9718 			goto out_unlock;
9719 	}
9720 
9721 	tr = trace_array_create_systems(name, systems, 0, 0);
9722 
9723 	if (IS_ERR(tr))
9724 		tr = NULL;
9725 out_unlock:
9726 	if (tr)
9727 		tr->ref++;
9728 
9729 	mutex_unlock(&trace_types_lock);
9730 	mutex_unlock(&event_mutex);
9731 	return tr;
9732 }
9733 EXPORT_SYMBOL_GPL(trace_array_get_by_name);
9734 
9735 static int __remove_instance(struct trace_array *tr)
9736 {
9737 	int i;
9738 
9739 	/* Reference counter for a newly created trace array = 1. */
9740 	if (tr->ref > 1 || (tr->current_trace && tr->trace_ref))
9741 		return -EBUSY;
9742 
9743 	list_del(&tr->list);
9744 
9745 	/* Disable all the flags that were enabled coming in */
9746 	for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++) {
9747 		if ((1 << i) & ZEROED_TRACE_FLAGS)
9748 			set_tracer_flag(tr, 1 << i, 0);
9749 	}
9750 
9751 	if (printk_trace == tr)
9752 		update_printk_trace(&global_trace);
9753 
9754 	tracing_set_nop(tr);
9755 	clear_ftrace_function_probes(tr);
9756 	event_trace_del_tracer(tr);
9757 	ftrace_clear_pids(tr);
9758 	ftrace_destroy_function_files(tr);
9759 	tracefs_remove(tr->dir);
9760 	free_percpu(tr->last_func_repeats);
9761 	free_trace_buffers(tr);
9762 	clear_tracing_err_log(tr);
9763 
9764 	for (i = 0; i < tr->nr_topts; i++) {
9765 		kfree(tr->topts[i].topts);
9766 	}
9767 	kfree(tr->topts);
9768 
9769 	free_cpumask_var(tr->pipe_cpumask);
9770 	free_cpumask_var(tr->tracing_cpumask);
9771 	kfree_const(tr->system_names);
9772 	kfree(tr->name);
9773 	kfree(tr);
9774 
9775 	return 0;
9776 }
9777 
9778 int trace_array_destroy(struct trace_array *this_tr)
9779 {
9780 	struct trace_array *tr;
9781 	int ret;
9782 
9783 	if (!this_tr)
9784 		return -EINVAL;
9785 
9786 	mutex_lock(&event_mutex);
9787 	mutex_lock(&trace_types_lock);
9788 
9789 	ret = -ENODEV;
9790 
9791 	/* Making sure trace array exists before destroying it. */
9792 	list_for_each_entry(tr, &ftrace_trace_arrays, list) {
9793 		if (tr == this_tr) {
9794 			ret = __remove_instance(tr);
9795 			break;
9796 		}
9797 	}
9798 
9799 	mutex_unlock(&trace_types_lock);
9800 	mutex_unlock(&event_mutex);
9801 
9802 	return ret;
9803 }
9804 EXPORT_SYMBOL_GPL(trace_array_destroy);
9805 
9806 static int instance_rmdir(const char *name)
9807 {
9808 	struct trace_array *tr;
9809 	int ret;
9810 
9811 	mutex_lock(&event_mutex);
9812 	mutex_lock(&trace_types_lock);
9813 
9814 	ret = -ENODEV;
9815 	tr = trace_array_find(name);
9816 	if (tr)
9817 		ret = __remove_instance(tr);
9818 
9819 	mutex_unlock(&trace_types_lock);
9820 	mutex_unlock(&event_mutex);
9821 
9822 	return ret;
9823 }
9824 
9825 static __init void create_trace_instances(struct dentry *d_tracer)
9826 {
9827 	struct trace_array *tr;
9828 
9829 	trace_instance_dir = tracefs_create_instance_dir("instances", d_tracer,
9830 							 instance_mkdir,
9831 							 instance_rmdir);
9832 	if (MEM_FAIL(!trace_instance_dir, "Failed to create instances directory\n"))
9833 		return;
9834 
9835 	mutex_lock(&event_mutex);
9836 	mutex_lock(&trace_types_lock);
9837 
9838 	list_for_each_entry(tr, &ftrace_trace_arrays, list) {
9839 		if (!tr->name)
9840 			continue;
9841 		if (MEM_FAIL(trace_array_create_dir(tr) < 0,
9842 			     "Failed to create instance directory\n"))
9843 			break;
9844 	}
9845 
9846 	mutex_unlock(&trace_types_lock);
9847 	mutex_unlock(&event_mutex);
9848 }
9849 
9850 static void
9851 init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer)
9852 {
9853 	int cpu;
9854 
9855 	trace_create_file("available_tracers", TRACE_MODE_READ, d_tracer,
9856 			tr, &show_traces_fops);
9857 
9858 	trace_create_file("current_tracer", TRACE_MODE_WRITE, d_tracer,
9859 			tr, &set_tracer_fops);
9860 
9861 	trace_create_file("tracing_cpumask", TRACE_MODE_WRITE, d_tracer,
9862 			  tr, &tracing_cpumask_fops);
9863 
9864 	trace_create_file("trace_options", TRACE_MODE_WRITE, d_tracer,
9865 			  tr, &tracing_iter_fops);
9866 
9867 	trace_create_file("trace", TRACE_MODE_WRITE, d_tracer,
9868 			  tr, &tracing_fops);
9869 
9870 	trace_create_file("trace_pipe", TRACE_MODE_READ, d_tracer,
9871 			  tr, &tracing_pipe_fops);
9872 
9873 	trace_create_file("buffer_size_kb", TRACE_MODE_WRITE, d_tracer,
9874 			  tr, &tracing_entries_fops);
9875 
9876 	trace_create_file("buffer_total_size_kb", TRACE_MODE_READ, d_tracer,
9877 			  tr, &tracing_total_entries_fops);
9878 
9879 	trace_create_file("free_buffer", 0200, d_tracer,
9880 			  tr, &tracing_free_buffer_fops);
9881 
9882 	trace_create_file("trace_marker", 0220, d_tracer,
9883 			  tr, &tracing_mark_fops);
9884 
9885 	tr->trace_marker_file = __find_event_file(tr, "ftrace", "print");
9886 
9887 	trace_create_file("trace_marker_raw", 0220, d_tracer,
9888 			  tr, &tracing_mark_raw_fops);
9889 
9890 	trace_create_file("trace_clock", TRACE_MODE_WRITE, d_tracer, tr,
9891 			  &trace_clock_fops);
9892 
9893 	trace_create_file("tracing_on", TRACE_MODE_WRITE, d_tracer,
9894 			  tr, &rb_simple_fops);
9895 
9896 	trace_create_file("timestamp_mode", TRACE_MODE_READ, d_tracer, tr,
9897 			  &trace_time_stamp_mode_fops);
9898 
9899 	tr->buffer_percent = 50;
9900 
9901 	trace_create_file("buffer_percent", TRACE_MODE_WRITE, d_tracer,
9902 			tr, &buffer_percent_fops);
9903 
9904 	trace_create_file("buffer_subbuf_size_kb", TRACE_MODE_WRITE, d_tracer,
9905 			  tr, &buffer_subbuf_size_fops);
9906 
9907 	create_trace_options_dir(tr);
9908 
9909 #ifdef CONFIG_TRACER_MAX_TRACE
9910 	trace_create_maxlat_file(tr, d_tracer);
9911 #endif
9912 
9913 	if (ftrace_create_function_files(tr, d_tracer))
9914 		MEM_FAIL(1, "Could not allocate function filter files");
9915 
9916 	if (tr->range_addr_start) {
9917 		trace_create_file("last_boot_info", TRACE_MODE_READ, d_tracer,
9918 				  tr, &last_boot_fops);
9919 #ifdef CONFIG_TRACER_SNAPSHOT
9920 	} else {
9921 		trace_create_file("snapshot", TRACE_MODE_WRITE, d_tracer,
9922 				  tr, &snapshot_fops);
9923 #endif
9924 	}
9925 
9926 	trace_create_file("error_log", TRACE_MODE_WRITE, d_tracer,
9927 			  tr, &tracing_err_log_fops);
9928 
9929 	for_each_tracing_cpu(cpu)
9930 		tracing_init_tracefs_percpu(tr, cpu);
9931 
9932 	ftrace_init_tracefs(tr, d_tracer);
9933 }
9934 
9935 static struct vfsmount *trace_automount(struct dentry *mntpt, void *ingore)
9936 {
9937 	struct vfsmount *mnt;
9938 	struct file_system_type *type;
9939 
9940 	/*
9941 	 * To maintain backward compatibility for tools that mount
9942 	 * debugfs to get to the tracing facility, tracefs is automatically
9943 	 * mounted to the debugfs/tracing directory.
9944 	 */
9945 	type = get_fs_type("tracefs");
9946 	if (!type)
9947 		return NULL;
9948 	mnt = vfs_submount(mntpt, type, "tracefs", NULL);
9949 	put_filesystem(type);
9950 	if (IS_ERR(mnt))
9951 		return NULL;
9952 	mntget(mnt);
9953 
9954 	return mnt;
9955 }
9956 
9957 /**
9958  * tracing_init_dentry - initialize top level trace array
9959  *
9960  * This is called when creating files or directories in the tracing
9961  * directory. It is called via fs_initcall() by any of the boot up code
9962  * and expects to return the dentry of the top level tracing directory.
9963  */
9964 int tracing_init_dentry(void)
9965 {
9966 	struct trace_array *tr = &global_trace;
9967 
9968 	if (security_locked_down(LOCKDOWN_TRACEFS)) {
9969 		pr_warn("Tracing disabled due to lockdown\n");
9970 		return -EPERM;
9971 	}
9972 
9973 	/* The top level trace array uses  NULL as parent */
9974 	if (tr->dir)
9975 		return 0;
9976 
9977 	if (WARN_ON(!tracefs_initialized()))
9978 		return -ENODEV;
9979 
9980 	/*
9981 	 * As there may still be users that expect the tracing
9982 	 * files to exist in debugfs/tracing, we must automount
9983 	 * the tracefs file system there, so older tools still
9984 	 * work with the newer kernel.
9985 	 */
9986 	tr->dir = debugfs_create_automount("tracing", NULL,
9987 					   trace_automount, NULL);
9988 
9989 	return 0;
9990 }
9991 
9992 extern struct trace_eval_map *__start_ftrace_eval_maps[];
9993 extern struct trace_eval_map *__stop_ftrace_eval_maps[];
9994 
9995 static struct workqueue_struct *eval_map_wq __initdata;
9996 static struct work_struct eval_map_work __initdata;
9997 static struct work_struct tracerfs_init_work __initdata;
9998 
9999 static void __init eval_map_work_func(struct work_struct *work)
10000 {
10001 	int len;
10002 
10003 	len = __stop_ftrace_eval_maps - __start_ftrace_eval_maps;
10004 	trace_insert_eval_map(NULL, __start_ftrace_eval_maps, len);
10005 }
10006 
10007 static int __init trace_eval_init(void)
10008 {
10009 	INIT_WORK(&eval_map_work, eval_map_work_func);
10010 
10011 	eval_map_wq = alloc_workqueue("eval_map_wq", WQ_UNBOUND, 0);
10012 	if (!eval_map_wq) {
10013 		pr_err("Unable to allocate eval_map_wq\n");
10014 		/* Do work here */
10015 		eval_map_work_func(&eval_map_work);
10016 		return -ENOMEM;
10017 	}
10018 
10019 	queue_work(eval_map_wq, &eval_map_work);
10020 	return 0;
10021 }
10022 
10023 subsys_initcall(trace_eval_init);
10024 
10025 static int __init trace_eval_sync(void)
10026 {
10027 	/* Make sure the eval map updates are finished */
10028 	if (eval_map_wq)
10029 		destroy_workqueue(eval_map_wq);
10030 	return 0;
10031 }
10032 
10033 late_initcall_sync(trace_eval_sync);
10034 
10035 
10036 #ifdef CONFIG_MODULES
10037 static void trace_module_add_evals(struct module *mod)
10038 {
10039 	if (!mod->num_trace_evals)
10040 		return;
10041 
10042 	/*
10043 	 * Modules with bad taint do not have events created, do
10044 	 * not bother with enums either.
10045 	 */
10046 	if (trace_module_has_bad_taint(mod))
10047 		return;
10048 
10049 	trace_insert_eval_map(mod, mod->trace_evals, mod->num_trace_evals);
10050 }
10051 
10052 #ifdef CONFIG_TRACE_EVAL_MAP_FILE
10053 static void trace_module_remove_evals(struct module *mod)
10054 {
10055 	union trace_eval_map_item *map;
10056 	union trace_eval_map_item **last = &trace_eval_maps;
10057 
10058 	if (!mod->num_trace_evals)
10059 		return;
10060 
10061 	mutex_lock(&trace_eval_mutex);
10062 
10063 	map = trace_eval_maps;
10064 
10065 	while (map) {
10066 		if (map->head.mod == mod)
10067 			break;
10068 		map = trace_eval_jmp_to_tail(map);
10069 		last = &map->tail.next;
10070 		map = map->tail.next;
10071 	}
10072 	if (!map)
10073 		goto out;
10074 
10075 	*last = trace_eval_jmp_to_tail(map)->tail.next;
10076 	kfree(map);
10077  out:
10078 	mutex_unlock(&trace_eval_mutex);
10079 }
10080 #else
10081 static inline void trace_module_remove_evals(struct module *mod) { }
10082 #endif /* CONFIG_TRACE_EVAL_MAP_FILE */
10083 
10084 static int trace_module_notify(struct notifier_block *self,
10085 			       unsigned long val, void *data)
10086 {
10087 	struct module *mod = data;
10088 
10089 	switch (val) {
10090 	case MODULE_STATE_COMING:
10091 		trace_module_add_evals(mod);
10092 		break;
10093 	case MODULE_STATE_GOING:
10094 		trace_module_remove_evals(mod);
10095 		break;
10096 	}
10097 
10098 	return NOTIFY_OK;
10099 }
10100 
10101 static struct notifier_block trace_module_nb = {
10102 	.notifier_call = trace_module_notify,
10103 	.priority = 0,
10104 };
10105 #endif /* CONFIG_MODULES */
10106 
10107 static __init void tracer_init_tracefs_work_func(struct work_struct *work)
10108 {
10109 
10110 	event_trace_init();
10111 
10112 	init_tracer_tracefs(&global_trace, NULL);
10113 	ftrace_init_tracefs_toplevel(&global_trace, NULL);
10114 
10115 	trace_create_file("tracing_thresh", TRACE_MODE_WRITE, NULL,
10116 			&global_trace, &tracing_thresh_fops);
10117 
10118 	trace_create_file("README", TRACE_MODE_READ, NULL,
10119 			NULL, &tracing_readme_fops);
10120 
10121 	trace_create_file("saved_cmdlines", TRACE_MODE_READ, NULL,
10122 			NULL, &tracing_saved_cmdlines_fops);
10123 
10124 	trace_create_file("saved_cmdlines_size", TRACE_MODE_WRITE, NULL,
10125 			  NULL, &tracing_saved_cmdlines_size_fops);
10126 
10127 	trace_create_file("saved_tgids", TRACE_MODE_READ, NULL,
10128 			NULL, &tracing_saved_tgids_fops);
10129 
10130 	trace_create_eval_file(NULL);
10131 
10132 #ifdef CONFIG_MODULES
10133 	register_module_notifier(&trace_module_nb);
10134 #endif
10135 
10136 #ifdef CONFIG_DYNAMIC_FTRACE
10137 	trace_create_file("dyn_ftrace_total_info", TRACE_MODE_READ, NULL,
10138 			NULL, &tracing_dyn_info_fops);
10139 #endif
10140 
10141 	create_trace_instances(NULL);
10142 
10143 	update_tracer_options(&global_trace);
10144 }
10145 
10146 static __init int tracer_init_tracefs(void)
10147 {
10148 	int ret;
10149 
10150 	trace_access_lock_init();
10151 
10152 	ret = tracing_init_dentry();
10153 	if (ret)
10154 		return 0;
10155 
10156 	if (eval_map_wq) {
10157 		INIT_WORK(&tracerfs_init_work, tracer_init_tracefs_work_func);
10158 		queue_work(eval_map_wq, &tracerfs_init_work);
10159 	} else {
10160 		tracer_init_tracefs_work_func(NULL);
10161 	}
10162 
10163 	rv_init_interface();
10164 
10165 	return 0;
10166 }
10167 
10168 fs_initcall(tracer_init_tracefs);
10169 
10170 static int trace_die_panic_handler(struct notifier_block *self,
10171 				unsigned long ev, void *unused);
10172 
10173 static struct notifier_block trace_panic_notifier = {
10174 	.notifier_call = trace_die_panic_handler,
10175 	.priority = INT_MAX - 1,
10176 };
10177 
10178 static struct notifier_block trace_die_notifier = {
10179 	.notifier_call = trace_die_panic_handler,
10180 	.priority = INT_MAX - 1,
10181 };
10182 
10183 /*
10184  * The idea is to execute the following die/panic callback early, in order
10185  * to avoid showing irrelevant information in the trace (like other panic
10186  * notifier functions); we are the 2nd to run, after hung_task/rcu_stall
10187  * warnings get disabled (to prevent potential log flooding).
10188  */
10189 static int trace_die_panic_handler(struct notifier_block *self,
10190 				unsigned long ev, void *unused)
10191 {
10192 	if (!ftrace_dump_on_oops_enabled())
10193 		return NOTIFY_DONE;
10194 
10195 	/* The die notifier requires DIE_OOPS to trigger */
10196 	if (self == &trace_die_notifier && ev != DIE_OOPS)
10197 		return NOTIFY_DONE;
10198 
10199 	ftrace_dump(DUMP_PARAM);
10200 
10201 	return NOTIFY_DONE;
10202 }
10203 
10204 /*
10205  * printk is set to max of 1024, we really don't need it that big.
10206  * Nothing should be printing 1000 characters anyway.
10207  */
10208 #define TRACE_MAX_PRINT		1000
10209 
10210 /*
10211  * Define here KERN_TRACE so that we have one place to modify
10212  * it if we decide to change what log level the ftrace dump
10213  * should be at.
10214  */
10215 #define KERN_TRACE		KERN_EMERG
10216 
10217 void
10218 trace_printk_seq(struct trace_seq *s)
10219 {
10220 	/* Probably should print a warning here. */
10221 	if (s->seq.len >= TRACE_MAX_PRINT)
10222 		s->seq.len = TRACE_MAX_PRINT;
10223 
10224 	/*
10225 	 * More paranoid code. Although the buffer size is set to
10226 	 * PAGE_SIZE, and TRACE_MAX_PRINT is 1000, this is just
10227 	 * an extra layer of protection.
10228 	 */
10229 	if (WARN_ON_ONCE(s->seq.len >= s->seq.size))
10230 		s->seq.len = s->seq.size - 1;
10231 
10232 	/* should be zero ended, but we are paranoid. */
10233 	s->buffer[s->seq.len] = 0;
10234 
10235 	printk(KERN_TRACE "%s", s->buffer);
10236 
10237 	trace_seq_init(s);
10238 }
10239 
10240 static void trace_init_iter(struct trace_iterator *iter, struct trace_array *tr)
10241 {
10242 	iter->tr = tr;
10243 	iter->trace = iter->tr->current_trace;
10244 	iter->cpu_file = RING_BUFFER_ALL_CPUS;
10245 	iter->array_buffer = &tr->array_buffer;
10246 
10247 	if (iter->trace && iter->trace->open)
10248 		iter->trace->open(iter);
10249 
10250 	/* Annotate start of buffers if we had overruns */
10251 	if (ring_buffer_overruns(iter->array_buffer->buffer))
10252 		iter->iter_flags |= TRACE_FILE_ANNOTATE;
10253 
10254 	/* Output in nanoseconds only if we are using a clock in nanoseconds. */
10255 	if (trace_clocks[iter->tr->clock_id].in_ns)
10256 		iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
10257 
10258 	/* Can not use kmalloc for iter.temp and iter.fmt */
10259 	iter->temp = static_temp_buf;
10260 	iter->temp_size = STATIC_TEMP_BUF_SIZE;
10261 	iter->fmt = static_fmt_buf;
10262 	iter->fmt_size = STATIC_FMT_BUF_SIZE;
10263 }
10264 
10265 void trace_init_global_iter(struct trace_iterator *iter)
10266 {
10267 	trace_init_iter(iter, &global_trace);
10268 }
10269 
10270 static void ftrace_dump_one(struct trace_array *tr, enum ftrace_dump_mode dump_mode)
10271 {
10272 	/* use static because iter can be a bit big for the stack */
10273 	static struct trace_iterator iter;
10274 	unsigned int old_userobj;
10275 	unsigned long flags;
10276 	int cnt = 0, cpu;
10277 
10278 	/*
10279 	 * Always turn off tracing when we dump.
10280 	 * We don't need to show trace output of what happens
10281 	 * between multiple crashes.
10282 	 *
10283 	 * If the user does a sysrq-z, then they can re-enable
10284 	 * tracing with echo 1 > tracing_on.
10285 	 */
10286 	tracer_tracing_off(tr);
10287 
10288 	local_irq_save(flags);
10289 
10290 	/* Simulate the iterator */
10291 	trace_init_iter(&iter, tr);
10292 
10293 	for_each_tracing_cpu(cpu) {
10294 		atomic_inc(&per_cpu_ptr(iter.array_buffer->data, cpu)->disabled);
10295 	}
10296 
10297 	old_userobj = tr->trace_flags & TRACE_ITER_SYM_USEROBJ;
10298 
10299 	/* don't look at user memory in panic mode */
10300 	tr->trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
10301 
10302 	if (dump_mode == DUMP_ORIG)
10303 		iter.cpu_file = raw_smp_processor_id();
10304 	else
10305 		iter.cpu_file = RING_BUFFER_ALL_CPUS;
10306 
10307 	if (tr == &global_trace)
10308 		printk(KERN_TRACE "Dumping ftrace buffer:\n");
10309 	else
10310 		printk(KERN_TRACE "Dumping ftrace instance %s buffer:\n", tr->name);
10311 
10312 	/* Did function tracer already get disabled? */
10313 	if (ftrace_is_dead()) {
10314 		printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
10315 		printk("#          MAY BE MISSING FUNCTION EVENTS\n");
10316 	}
10317 
10318 	/*
10319 	 * We need to stop all tracing on all CPUS to read
10320 	 * the next buffer. This is a bit expensive, but is
10321 	 * not done often. We fill all what we can read,
10322 	 * and then release the locks again.
10323 	 */
10324 
10325 	while (!trace_empty(&iter)) {
10326 
10327 		if (!cnt)
10328 			printk(KERN_TRACE "---------------------------------\n");
10329 
10330 		cnt++;
10331 
10332 		trace_iterator_reset(&iter);
10333 		iter.iter_flags |= TRACE_FILE_LAT_FMT;
10334 
10335 		if (trace_find_next_entry_inc(&iter) != NULL) {
10336 			int ret;
10337 
10338 			ret = print_trace_line(&iter);
10339 			if (ret != TRACE_TYPE_NO_CONSUME)
10340 				trace_consume(&iter);
10341 		}
10342 		touch_nmi_watchdog();
10343 
10344 		trace_printk_seq(&iter.seq);
10345 	}
10346 
10347 	if (!cnt)
10348 		printk(KERN_TRACE "   (ftrace buffer empty)\n");
10349 	else
10350 		printk(KERN_TRACE "---------------------------------\n");
10351 
10352 	tr->trace_flags |= old_userobj;
10353 
10354 	for_each_tracing_cpu(cpu) {
10355 		atomic_dec(&per_cpu_ptr(iter.array_buffer->data, cpu)->disabled);
10356 	}
10357 	local_irq_restore(flags);
10358 }
10359 
10360 static void ftrace_dump_by_param(void)
10361 {
10362 	bool first_param = true;
10363 	char dump_param[MAX_TRACER_SIZE];
10364 	char *buf, *token, *inst_name;
10365 	struct trace_array *tr;
10366 
10367 	strscpy(dump_param, ftrace_dump_on_oops, MAX_TRACER_SIZE);
10368 	buf = dump_param;
10369 
10370 	while ((token = strsep(&buf, ",")) != NULL) {
10371 		if (first_param) {
10372 			first_param = false;
10373 			if (!strcmp("0", token))
10374 				continue;
10375 			else if (!strcmp("1", token)) {
10376 				ftrace_dump_one(&global_trace, DUMP_ALL);
10377 				continue;
10378 			}
10379 			else if (!strcmp("2", token) ||
10380 			  !strcmp("orig_cpu", token)) {
10381 				ftrace_dump_one(&global_trace, DUMP_ORIG);
10382 				continue;
10383 			}
10384 		}
10385 
10386 		inst_name = strsep(&token, "=");
10387 		tr = trace_array_find(inst_name);
10388 		if (!tr) {
10389 			printk(KERN_TRACE "Instance %s not found\n", inst_name);
10390 			continue;
10391 		}
10392 
10393 		if (token && (!strcmp("2", token) ||
10394 			  !strcmp("orig_cpu", token)))
10395 			ftrace_dump_one(tr, DUMP_ORIG);
10396 		else
10397 			ftrace_dump_one(tr, DUMP_ALL);
10398 	}
10399 }
10400 
10401 void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
10402 {
10403 	static atomic_t dump_running;
10404 
10405 	/* Only allow one dump user at a time. */
10406 	if (atomic_inc_return(&dump_running) != 1) {
10407 		atomic_dec(&dump_running);
10408 		return;
10409 	}
10410 
10411 	switch (oops_dump_mode) {
10412 	case DUMP_ALL:
10413 		ftrace_dump_one(&global_trace, DUMP_ALL);
10414 		break;
10415 	case DUMP_ORIG:
10416 		ftrace_dump_one(&global_trace, DUMP_ORIG);
10417 		break;
10418 	case DUMP_PARAM:
10419 		ftrace_dump_by_param();
10420 		break;
10421 	case DUMP_NONE:
10422 		break;
10423 	default:
10424 		printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
10425 		ftrace_dump_one(&global_trace, DUMP_ALL);
10426 	}
10427 
10428 	atomic_dec(&dump_running);
10429 }
10430 EXPORT_SYMBOL_GPL(ftrace_dump);
10431 
10432 #define WRITE_BUFSIZE  4096
10433 
10434 ssize_t trace_parse_run_command(struct file *file, const char __user *buffer,
10435 				size_t count, loff_t *ppos,
10436 				int (*createfn)(const char *))
10437 {
10438 	char *kbuf, *buf, *tmp;
10439 	int ret = 0;
10440 	size_t done = 0;
10441 	size_t size;
10442 
10443 	kbuf = kmalloc(WRITE_BUFSIZE, GFP_KERNEL);
10444 	if (!kbuf)
10445 		return -ENOMEM;
10446 
10447 	while (done < count) {
10448 		size = count - done;
10449 
10450 		if (size >= WRITE_BUFSIZE)
10451 			size = WRITE_BUFSIZE - 1;
10452 
10453 		if (copy_from_user(kbuf, buffer + done, size)) {
10454 			ret = -EFAULT;
10455 			goto out;
10456 		}
10457 		kbuf[size] = '\0';
10458 		buf = kbuf;
10459 		do {
10460 			tmp = strchr(buf, '\n');
10461 			if (tmp) {
10462 				*tmp = '\0';
10463 				size = tmp - buf + 1;
10464 			} else {
10465 				size = strlen(buf);
10466 				if (done + size < count) {
10467 					if (buf != kbuf)
10468 						break;
10469 					/* This can accept WRITE_BUFSIZE - 2 ('\n' + '\0') */
10470 					pr_warn("Line length is too long: Should be less than %d\n",
10471 						WRITE_BUFSIZE - 2);
10472 					ret = -EINVAL;
10473 					goto out;
10474 				}
10475 			}
10476 			done += size;
10477 
10478 			/* Remove comments */
10479 			tmp = strchr(buf, '#');
10480 
10481 			if (tmp)
10482 				*tmp = '\0';
10483 
10484 			ret = createfn(buf);
10485 			if (ret)
10486 				goto out;
10487 			buf += size;
10488 
10489 		} while (done < count);
10490 	}
10491 	ret = done;
10492 
10493 out:
10494 	kfree(kbuf);
10495 
10496 	return ret;
10497 }
10498 
10499 #ifdef CONFIG_TRACER_MAX_TRACE
10500 __init static bool tr_needs_alloc_snapshot(const char *name)
10501 {
10502 	char *test;
10503 	int len = strlen(name);
10504 	bool ret;
10505 
10506 	if (!boot_snapshot_index)
10507 		return false;
10508 
10509 	if (strncmp(name, boot_snapshot_info, len) == 0 &&
10510 	    boot_snapshot_info[len] == '\t')
10511 		return true;
10512 
10513 	test = kmalloc(strlen(name) + 3, GFP_KERNEL);
10514 	if (!test)
10515 		return false;
10516 
10517 	sprintf(test, "\t%s\t", name);
10518 	ret = strstr(boot_snapshot_info, test) == NULL;
10519 	kfree(test);
10520 	return ret;
10521 }
10522 
10523 __init static void do_allocate_snapshot(const char *name)
10524 {
10525 	if (!tr_needs_alloc_snapshot(name))
10526 		return;
10527 
10528 	/*
10529 	 * When allocate_snapshot is set, the next call to
10530 	 * allocate_trace_buffers() (called by trace_array_get_by_name())
10531 	 * will allocate the snapshot buffer. That will alse clear
10532 	 * this flag.
10533 	 */
10534 	allocate_snapshot = true;
10535 }
10536 #else
10537 static inline void do_allocate_snapshot(const char *name) { }
10538 #endif
10539 
10540 __init static void enable_instances(void)
10541 {
10542 	struct trace_array *tr;
10543 	char *curr_str;
10544 	char *name;
10545 	char *str;
10546 	char *tok;
10547 
10548 	/* A tab is always appended */
10549 	boot_instance_info[boot_instance_index - 1] = '\0';
10550 	str = boot_instance_info;
10551 
10552 	while ((curr_str = strsep(&str, "\t"))) {
10553 		phys_addr_t start = 0;
10554 		phys_addr_t size = 0;
10555 		unsigned long addr = 0;
10556 		bool traceprintk = false;
10557 		bool traceoff = false;
10558 		char *flag_delim;
10559 		char *addr_delim;
10560 
10561 		tok = strsep(&curr_str, ",");
10562 
10563 		flag_delim = strchr(tok, '^');
10564 		addr_delim = strchr(tok, '@');
10565 
10566 		if (addr_delim)
10567 			*addr_delim++ = '\0';
10568 
10569 		if (flag_delim)
10570 			*flag_delim++ = '\0';
10571 
10572 		name = tok;
10573 
10574 		if (flag_delim) {
10575 			char *flag;
10576 
10577 			while ((flag = strsep(&flag_delim, "^"))) {
10578 				if (strcmp(flag, "traceoff") == 0) {
10579 					traceoff = true;
10580 				} else if ((strcmp(flag, "printk") == 0) ||
10581 					   (strcmp(flag, "traceprintk") == 0) ||
10582 					   (strcmp(flag, "trace_printk") == 0)) {
10583 					traceprintk = true;
10584 				} else {
10585 					pr_info("Tracing: Invalid instance flag '%s' for %s\n",
10586 						flag, name);
10587 				}
10588 			}
10589 		}
10590 
10591 		tok = addr_delim;
10592 		if (tok && isdigit(*tok)) {
10593 			start = memparse(tok, &tok);
10594 			if (!start) {
10595 				pr_warn("Tracing: Invalid boot instance address for %s\n",
10596 					name);
10597 				continue;
10598 			}
10599 			if (*tok != ':') {
10600 				pr_warn("Tracing: No size specified for instance %s\n", name);
10601 				continue;
10602 			}
10603 			tok++;
10604 			size = memparse(tok, &tok);
10605 			if (!size) {
10606 				pr_warn("Tracing: Invalid boot instance size for %s\n",
10607 					name);
10608 				continue;
10609 			}
10610 		} else if (tok) {
10611 			if (!reserve_mem_find_by_name(tok, &start, &size)) {
10612 				start = 0;
10613 				pr_warn("Failed to map boot instance %s to %s\n", name, tok);
10614 				continue;
10615 			}
10616 		}
10617 
10618 		if (start) {
10619 			addr = map_pages(start, size);
10620 			if (addr) {
10621 				pr_info("Tracing: mapped boot instance %s at physical memory %pa of size 0x%lx\n",
10622 					name, &start, (unsigned long)size);
10623 			} else {
10624 				pr_warn("Tracing: Failed to map boot instance %s\n", name);
10625 				continue;
10626 			}
10627 		} else {
10628 			/* Only non mapped buffers have snapshot buffers */
10629 			if (IS_ENABLED(CONFIG_TRACER_MAX_TRACE))
10630 				do_allocate_snapshot(name);
10631 		}
10632 
10633 		tr = trace_array_create_systems(name, NULL, addr, size);
10634 		if (IS_ERR(tr)) {
10635 			pr_warn("Tracing: Failed to create instance buffer %s\n", curr_str);
10636 			continue;
10637 		}
10638 
10639 		if (traceoff)
10640 			tracer_tracing_off(tr);
10641 
10642 		if (traceprintk)
10643 			update_printk_trace(tr);
10644 
10645 		/*
10646 		 * If start is set, then this is a mapped buffer, and
10647 		 * cannot be deleted by user space, so keep the reference
10648 		 * to it.
10649 		 */
10650 		if (start) {
10651 			tr->flags |= TRACE_ARRAY_FL_BOOT;
10652 			tr->ref++;
10653 		}
10654 
10655 		while ((tok = strsep(&curr_str, ","))) {
10656 			early_enable_events(tr, tok, true);
10657 		}
10658 	}
10659 }
10660 
10661 __init static int tracer_alloc_buffers(void)
10662 {
10663 	int ring_buf_size;
10664 	int ret = -ENOMEM;
10665 
10666 
10667 	if (security_locked_down(LOCKDOWN_TRACEFS)) {
10668 		pr_warn("Tracing disabled due to lockdown\n");
10669 		return -EPERM;
10670 	}
10671 
10672 	/*
10673 	 * Make sure we don't accidentally add more trace options
10674 	 * than we have bits for.
10675 	 */
10676 	BUILD_BUG_ON(TRACE_ITER_LAST_BIT > TRACE_FLAGS_MAX_SIZE);
10677 
10678 	if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
10679 		goto out;
10680 
10681 	if (!alloc_cpumask_var(&global_trace.tracing_cpumask, GFP_KERNEL))
10682 		goto out_free_buffer_mask;
10683 
10684 	/* Only allocate trace_printk buffers if a trace_printk exists */
10685 	if (&__stop___trace_bprintk_fmt != &__start___trace_bprintk_fmt)
10686 		/* Must be called before global_trace.buffer is allocated */
10687 		trace_printk_init_buffers();
10688 
10689 	/* To save memory, keep the ring buffer size to its minimum */
10690 	if (global_trace.ring_buffer_expanded)
10691 		ring_buf_size = trace_buf_size;
10692 	else
10693 		ring_buf_size = 1;
10694 
10695 	cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
10696 	cpumask_copy(global_trace.tracing_cpumask, cpu_all_mask);
10697 
10698 	raw_spin_lock_init(&global_trace.start_lock);
10699 
10700 	/*
10701 	 * The prepare callbacks allocates some memory for the ring buffer. We
10702 	 * don't free the buffer if the CPU goes down. If we were to free
10703 	 * the buffer, then the user would lose any trace that was in the
10704 	 * buffer. The memory will be removed once the "instance" is removed.
10705 	 */
10706 	ret = cpuhp_setup_state_multi(CPUHP_TRACE_RB_PREPARE,
10707 				      "trace/RB:prepare", trace_rb_cpu_prepare,
10708 				      NULL);
10709 	if (ret < 0)
10710 		goto out_free_cpumask;
10711 	/* Used for event triggers */
10712 	ret = -ENOMEM;
10713 	temp_buffer = ring_buffer_alloc(PAGE_SIZE, RB_FL_OVERWRITE);
10714 	if (!temp_buffer)
10715 		goto out_rm_hp_state;
10716 
10717 	if (trace_create_savedcmd() < 0)
10718 		goto out_free_temp_buffer;
10719 
10720 	if (!zalloc_cpumask_var(&global_trace.pipe_cpumask, GFP_KERNEL))
10721 		goto out_free_savedcmd;
10722 
10723 	/* TODO: make the number of buffers hot pluggable with CPUS */
10724 	if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
10725 		MEM_FAIL(1, "tracer: failed to allocate ring buffer!\n");
10726 		goto out_free_pipe_cpumask;
10727 	}
10728 	if (global_trace.buffer_disabled)
10729 		tracing_off();
10730 
10731 	if (trace_boot_clock) {
10732 		ret = tracing_set_clock(&global_trace, trace_boot_clock);
10733 		if (ret < 0)
10734 			pr_warn("Trace clock %s not defined, going back to default\n",
10735 				trace_boot_clock);
10736 	}
10737 
10738 	/*
10739 	 * register_tracer() might reference current_trace, so it
10740 	 * needs to be set before we register anything. This is
10741 	 * just a bootstrap of current_trace anyway.
10742 	 */
10743 	global_trace.current_trace = &nop_trace;
10744 
10745 	global_trace.max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
10746 #ifdef CONFIG_TRACER_MAX_TRACE
10747 	spin_lock_init(&global_trace.snapshot_trigger_lock);
10748 #endif
10749 	ftrace_init_global_array_ops(&global_trace);
10750 
10751 	init_trace_flags_index(&global_trace);
10752 
10753 	register_tracer(&nop_trace);
10754 
10755 	/* Function tracing may start here (via kernel command line) */
10756 	init_function_trace();
10757 
10758 	/* All seems OK, enable tracing */
10759 	tracing_disabled = 0;
10760 
10761 	atomic_notifier_chain_register(&panic_notifier_list,
10762 				       &trace_panic_notifier);
10763 
10764 	register_die_notifier(&trace_die_notifier);
10765 
10766 	global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
10767 
10768 	INIT_LIST_HEAD(&global_trace.systems);
10769 	INIT_LIST_HEAD(&global_trace.events);
10770 	INIT_LIST_HEAD(&global_trace.hist_vars);
10771 	INIT_LIST_HEAD(&global_trace.err_log);
10772 	list_add(&global_trace.list, &ftrace_trace_arrays);
10773 
10774 	apply_trace_boot_options();
10775 
10776 	register_snapshot_cmd();
10777 
10778 	test_can_verify();
10779 
10780 	return 0;
10781 
10782 out_free_pipe_cpumask:
10783 	free_cpumask_var(global_trace.pipe_cpumask);
10784 out_free_savedcmd:
10785 	trace_free_saved_cmdlines_buffer();
10786 out_free_temp_buffer:
10787 	ring_buffer_free(temp_buffer);
10788 out_rm_hp_state:
10789 	cpuhp_remove_multi_state(CPUHP_TRACE_RB_PREPARE);
10790 out_free_cpumask:
10791 	free_cpumask_var(global_trace.tracing_cpumask);
10792 out_free_buffer_mask:
10793 	free_cpumask_var(tracing_buffer_mask);
10794 out:
10795 	return ret;
10796 }
10797 
10798 void __init ftrace_boot_snapshot(void)
10799 {
10800 #ifdef CONFIG_TRACER_MAX_TRACE
10801 	struct trace_array *tr;
10802 
10803 	if (!snapshot_at_boot)
10804 		return;
10805 
10806 	list_for_each_entry(tr, &ftrace_trace_arrays, list) {
10807 		if (!tr->allocated_snapshot)
10808 			continue;
10809 
10810 		tracing_snapshot_instance(tr);
10811 		trace_array_puts(tr, "** Boot snapshot taken **\n");
10812 	}
10813 #endif
10814 }
10815 
10816 void __init early_trace_init(void)
10817 {
10818 	if (tracepoint_printk) {
10819 		tracepoint_print_iter =
10820 			kzalloc(sizeof(*tracepoint_print_iter), GFP_KERNEL);
10821 		if (MEM_FAIL(!tracepoint_print_iter,
10822 			     "Failed to allocate trace iterator\n"))
10823 			tracepoint_printk = 0;
10824 		else
10825 			static_key_enable(&tracepoint_printk_key.key);
10826 	}
10827 	tracer_alloc_buffers();
10828 
10829 	init_events();
10830 }
10831 
10832 void __init trace_init(void)
10833 {
10834 	trace_event_init();
10835 
10836 	if (boot_instance_index)
10837 		enable_instances();
10838 }
10839 
10840 __init static void clear_boot_tracer(void)
10841 {
10842 	/*
10843 	 * The default tracer at boot buffer is an init section.
10844 	 * This function is called in lateinit. If we did not
10845 	 * find the boot tracer, then clear it out, to prevent
10846 	 * later registration from accessing the buffer that is
10847 	 * about to be freed.
10848 	 */
10849 	if (!default_bootup_tracer)
10850 		return;
10851 
10852 	printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
10853 	       default_bootup_tracer);
10854 	default_bootup_tracer = NULL;
10855 }
10856 
10857 #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
10858 __init static void tracing_set_default_clock(void)
10859 {
10860 	/* sched_clock_stable() is determined in late_initcall */
10861 	if (!trace_boot_clock && !sched_clock_stable()) {
10862 		if (security_locked_down(LOCKDOWN_TRACEFS)) {
10863 			pr_warn("Can not set tracing clock due to lockdown\n");
10864 			return;
10865 		}
10866 
10867 		printk(KERN_WARNING
10868 		       "Unstable clock detected, switching default tracing clock to \"global\"\n"
10869 		       "If you want to keep using the local clock, then add:\n"
10870 		       "  \"trace_clock=local\"\n"
10871 		       "on the kernel command line\n");
10872 		tracing_set_clock(&global_trace, "global");
10873 	}
10874 }
10875 #else
10876 static inline void tracing_set_default_clock(void) { }
10877 #endif
10878 
10879 __init static int late_trace_init(void)
10880 {
10881 	if (tracepoint_printk && tracepoint_printk_stop_on_boot) {
10882 		static_key_disable(&tracepoint_printk_key.key);
10883 		tracepoint_printk = 0;
10884 	}
10885 
10886 	tracing_set_default_clock();
10887 	clear_boot_tracer();
10888 	return 0;
10889 }
10890 
10891 late_initcall_sync(late_trace_init);
10892