xref: /linux/kernel/trace/trace.c (revision 44eeab67416711db9b84610ef18c99a60415dff8)
1 /*
2  * ring buffer based function tracer
3  *
4  * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5  * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6  *
7  * Originally taken from the RT patch by:
8  *    Arnaldo Carvalho de Melo <acme@redhat.com>
9  *
10  * Based on code from the latency_tracer, that is:
11  *  Copyright (C) 2004-2006 Ingo Molnar
12  *  Copyright (C) 2004 William Lee Irwin III
13  */
14 #include <linux/ring_buffer.h>
15 #include <linux/utsrelease.h>
16 #include <linux/stacktrace.h>
17 #include <linux/writeback.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/notifier.h>
21 #include <linux/irqflags.h>
22 #include <linux/debugfs.h>
23 #include <linux/pagemap.h>
24 #include <linux/hardirq.h>
25 #include <linux/linkage.h>
26 #include <linux/uaccess.h>
27 #include <linux/kprobes.h>
28 #include <linux/ftrace.h>
29 #include <linux/module.h>
30 #include <linux/percpu.h>
31 #include <linux/splice.h>
32 #include <linux/kdebug.h>
33 #include <linux/string.h>
34 #include <linux/ctype.h>
35 #include <linux/init.h>
36 #include <linux/poll.h>
37 #include <linux/gfp.h>
38 #include <linux/fs.h>
39 
40 #include "trace.h"
41 #include "trace_output.h"
42 
43 #define TRACE_BUFFER_FLAGS	(RB_FL_OVERWRITE)
44 
45 unsigned long __read_mostly	tracing_max_latency;
46 unsigned long __read_mostly	tracing_thresh;
47 
48 /*
49  * On boot up, the ring buffer is set to the minimum size, so that
50  * we do not waste memory on systems that are not using tracing.
51  */
52 static int ring_buffer_expanded;
53 
54 /*
55  * We need to change this state when a selftest is running.
56  * A selftest will lurk into the ring-buffer to count the
57  * entries inserted during the selftest although some concurrent
58  * insertions into the ring-buffer such as trace_printk could occurred
59  * at the same time, giving false positive or negative results.
60  */
61 static bool __read_mostly tracing_selftest_running;
62 
63 /*
64  * If a tracer is running, we do not want to run SELFTEST.
65  */
66 static bool __read_mostly tracing_selftest_disabled;
67 
68 /* For tracers that don't implement custom flags */
69 static struct tracer_opt dummy_tracer_opt[] = {
70 	{ }
71 };
72 
73 static struct tracer_flags dummy_tracer_flags = {
74 	.val = 0,
75 	.opts = dummy_tracer_opt
76 };
77 
78 static int dummy_set_flag(u32 old_flags, u32 bit, int set)
79 {
80 	return 0;
81 }
82 
83 /*
84  * Kill all tracing for good (never come back).
85  * It is initialized to 1 but will turn to zero if the initialization
86  * of the tracer is successful. But that is the only place that sets
87  * this back to zero.
88  */
89 static int tracing_disabled = 1;
90 
91 static DEFINE_PER_CPU(local_t, ftrace_cpu_disabled);
92 
93 static inline void ftrace_disable_cpu(void)
94 {
95 	preempt_disable();
96 	local_inc(&__get_cpu_var(ftrace_cpu_disabled));
97 }
98 
99 static inline void ftrace_enable_cpu(void)
100 {
101 	local_dec(&__get_cpu_var(ftrace_cpu_disabled));
102 	preempt_enable();
103 }
104 
105 static cpumask_var_t __read_mostly	tracing_buffer_mask;
106 
107 /* Define which cpu buffers are currently read in trace_pipe */
108 static cpumask_var_t			tracing_reader_cpumask;
109 
110 #define for_each_tracing_cpu(cpu)	\
111 	for_each_cpu(cpu, tracing_buffer_mask)
112 
113 /*
114  * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
115  *
116  * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
117  * is set, then ftrace_dump is called. This will output the contents
118  * of the ftrace buffers to the console.  This is very useful for
119  * capturing traces that lead to crashes and outputing it to a
120  * serial console.
121  *
122  * It is default off, but you can enable it with either specifying
123  * "ftrace_dump_on_oops" in the kernel command line, or setting
124  * /proc/sys/kernel/ftrace_dump_on_oops to true.
125  */
126 int ftrace_dump_on_oops;
127 
128 static int tracing_set_tracer(const char *buf);
129 
130 #define BOOTUP_TRACER_SIZE		100
131 static char bootup_tracer_buf[BOOTUP_TRACER_SIZE] __initdata;
132 static char *default_bootup_tracer;
133 
134 static int __init set_ftrace(char *str)
135 {
136 	strncpy(bootup_tracer_buf, str, BOOTUP_TRACER_SIZE);
137 	default_bootup_tracer = bootup_tracer_buf;
138 	/* We are using ftrace early, expand it */
139 	ring_buffer_expanded = 1;
140 	return 1;
141 }
142 __setup("ftrace=", set_ftrace);
143 
144 static int __init set_ftrace_dump_on_oops(char *str)
145 {
146 	ftrace_dump_on_oops = 1;
147 	return 1;
148 }
149 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
150 
151 unsigned long long ns2usecs(cycle_t nsec)
152 {
153 	nsec += 500;
154 	do_div(nsec, 1000);
155 	return nsec;
156 }
157 
158 /*
159  * The global_trace is the descriptor that holds the tracing
160  * buffers for the live tracing. For each CPU, it contains
161  * a link list of pages that will store trace entries. The
162  * page descriptor of the pages in the memory is used to hold
163  * the link list by linking the lru item in the page descriptor
164  * to each of the pages in the buffer per CPU.
165  *
166  * For each active CPU there is a data field that holds the
167  * pages for the buffer for that CPU. Each CPU has the same number
168  * of pages allocated for its buffer.
169  */
170 static struct trace_array	global_trace;
171 
172 static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
173 
174 int filter_current_check_discard(struct ftrace_event_call *call, void *rec,
175 				 struct ring_buffer_event *event)
176 {
177 	return filter_check_discard(call, rec, global_trace.buffer, event);
178 }
179 EXPORT_SYMBOL_GPL(filter_current_check_discard);
180 
181 cycle_t ftrace_now(int cpu)
182 {
183 	u64 ts;
184 
185 	/* Early boot up does not have a buffer yet */
186 	if (!global_trace.buffer)
187 		return trace_clock_local();
188 
189 	ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
190 	ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
191 
192 	return ts;
193 }
194 
195 /*
196  * The max_tr is used to snapshot the global_trace when a maximum
197  * latency is reached. Some tracers will use this to store a maximum
198  * trace while it continues examining live traces.
199  *
200  * The buffers for the max_tr are set up the same as the global_trace.
201  * When a snapshot is taken, the link list of the max_tr is swapped
202  * with the link list of the global_trace and the buffers are reset for
203  * the global_trace so the tracing can continue.
204  */
205 static struct trace_array	max_tr;
206 
207 static DEFINE_PER_CPU(struct trace_array_cpu, max_data);
208 
209 /* tracer_enabled is used to toggle activation of a tracer */
210 static int			tracer_enabled = 1;
211 
212 /**
213  * tracing_is_enabled - return tracer_enabled status
214  *
215  * This function is used by other tracers to know the status
216  * of the tracer_enabled flag.  Tracers may use this function
217  * to know if it should enable their features when starting
218  * up. See irqsoff tracer for an example (start_irqsoff_tracer).
219  */
220 int tracing_is_enabled(void)
221 {
222 	return tracer_enabled;
223 }
224 
225 /*
226  * trace_buf_size is the size in bytes that is allocated
227  * for a buffer. Note, the number of bytes is always rounded
228  * to page size.
229  *
230  * This number is purposely set to a low number of 16384.
231  * If the dump on oops happens, it will be much appreciated
232  * to not have to wait for all that output. Anyway this can be
233  * boot time and run time configurable.
234  */
235 #define TRACE_BUF_SIZE_DEFAULT	1441792UL /* 16384 * 88 (sizeof(entry)) */
236 
237 static unsigned long		trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
238 
239 /* trace_types holds a link list of available tracers. */
240 static struct tracer		*trace_types __read_mostly;
241 
242 /* current_trace points to the tracer that is currently active */
243 static struct tracer		*current_trace __read_mostly;
244 
245 /*
246  * max_tracer_type_len is used to simplify the allocating of
247  * buffers to read userspace tracer names. We keep track of
248  * the longest tracer name registered.
249  */
250 static int			max_tracer_type_len;
251 
252 /*
253  * trace_types_lock is used to protect the trace_types list.
254  * This lock is also used to keep user access serialized.
255  * Accesses from userspace will grab this lock while userspace
256  * activities happen inside the kernel.
257  */
258 static DEFINE_MUTEX(trace_types_lock);
259 
260 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
261 static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
262 
263 /* trace_flags holds trace_options default values */
264 unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
265 	TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
266 	TRACE_ITER_GRAPH_TIME;
267 
268 /**
269  * trace_wake_up - wake up tasks waiting for trace input
270  *
271  * Simply wakes up any task that is blocked on the trace_wait
272  * queue. These is used with trace_poll for tasks polling the trace.
273  */
274 void trace_wake_up(void)
275 {
276 	/*
277 	 * The runqueue_is_locked() can fail, but this is the best we
278 	 * have for now:
279 	 */
280 	if (!(trace_flags & TRACE_ITER_BLOCK) && !runqueue_is_locked())
281 		wake_up(&trace_wait);
282 }
283 
284 static int __init set_buf_size(char *str)
285 {
286 	unsigned long buf_size;
287 	int ret;
288 
289 	if (!str)
290 		return 0;
291 	ret = strict_strtoul(str, 0, &buf_size);
292 	/* nr_entries can not be zero */
293 	if (ret < 0 || buf_size == 0)
294 		return 0;
295 	trace_buf_size = buf_size;
296 	return 1;
297 }
298 __setup("trace_buf_size=", set_buf_size);
299 
300 unsigned long nsecs_to_usecs(unsigned long nsecs)
301 {
302 	return nsecs / 1000;
303 }
304 
305 /* These must match the bit postions in trace_iterator_flags */
306 static const char *trace_options[] = {
307 	"print-parent",
308 	"sym-offset",
309 	"sym-addr",
310 	"verbose",
311 	"raw",
312 	"hex",
313 	"bin",
314 	"block",
315 	"stacktrace",
316 	"sched-tree",
317 	"trace_printk",
318 	"ftrace_preempt",
319 	"branch",
320 	"annotate",
321 	"userstacktrace",
322 	"sym-userobj",
323 	"printk-msg-only",
324 	"context-info",
325 	"latency-format",
326 	"global-clock",
327 	"sleep-time",
328 	"graph-time",
329 	NULL
330 };
331 
332 /*
333  * ftrace_max_lock is used to protect the swapping of buffers
334  * when taking a max snapshot. The buffers themselves are
335  * protected by per_cpu spinlocks. But the action of the swap
336  * needs its own lock.
337  *
338  * This is defined as a raw_spinlock_t in order to help
339  * with performance when lockdep debugging is enabled.
340  */
341 static raw_spinlock_t ftrace_max_lock =
342 	(raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
343 
344 /*
345  * Copy the new maximum trace into the separate maximum-trace
346  * structure. (this way the maximum trace is permanently saved,
347  * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
348  */
349 static void
350 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
351 {
352 	struct trace_array_cpu *data = tr->data[cpu];
353 
354 	max_tr.cpu = cpu;
355 	max_tr.time_start = data->preempt_timestamp;
356 
357 	data = max_tr.data[cpu];
358 	data->saved_latency = tracing_max_latency;
359 
360 	memcpy(data->comm, tsk->comm, TASK_COMM_LEN);
361 	data->pid = tsk->pid;
362 	data->uid = task_uid(tsk);
363 	data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
364 	data->policy = tsk->policy;
365 	data->rt_priority = tsk->rt_priority;
366 
367 	/* record this tasks comm */
368 	tracing_record_cmdline(tsk);
369 }
370 
371 ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
372 {
373 	int len;
374 	int ret;
375 
376 	if (!cnt)
377 		return 0;
378 
379 	if (s->len <= s->readpos)
380 		return -EBUSY;
381 
382 	len = s->len - s->readpos;
383 	if (cnt > len)
384 		cnt = len;
385 	ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
386 	if (ret == cnt)
387 		return -EFAULT;
388 
389 	cnt -= ret;
390 
391 	s->readpos += cnt;
392 	return cnt;
393 }
394 
395 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
396 {
397 	int len;
398 	void *ret;
399 
400 	if (s->len <= s->readpos)
401 		return -EBUSY;
402 
403 	len = s->len - s->readpos;
404 	if (cnt > len)
405 		cnt = len;
406 	ret = memcpy(buf, s->buffer + s->readpos, cnt);
407 	if (!ret)
408 		return -EFAULT;
409 
410 	s->readpos += cnt;
411 	return cnt;
412 }
413 
414 /**
415  * update_max_tr - snapshot all trace buffers from global_trace to max_tr
416  * @tr: tracer
417  * @tsk: the task with the latency
418  * @cpu: The cpu that initiated the trace.
419  *
420  * Flip the buffers between the @tr and the max_tr and record information
421  * about which task was the cause of this latency.
422  */
423 void
424 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
425 {
426 	struct ring_buffer *buf = tr->buffer;
427 
428 	WARN_ON_ONCE(!irqs_disabled());
429 	__raw_spin_lock(&ftrace_max_lock);
430 
431 	tr->buffer = max_tr.buffer;
432 	max_tr.buffer = buf;
433 
434 	ftrace_disable_cpu();
435 	ring_buffer_reset(tr->buffer);
436 	ftrace_enable_cpu();
437 
438 	__update_max_tr(tr, tsk, cpu);
439 	__raw_spin_unlock(&ftrace_max_lock);
440 }
441 
442 /**
443  * update_max_tr_single - only copy one trace over, and reset the rest
444  * @tr - tracer
445  * @tsk - task with the latency
446  * @cpu - the cpu of the buffer to copy.
447  *
448  * Flip the trace of a single CPU buffer between the @tr and the max_tr.
449  */
450 void
451 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
452 {
453 	int ret;
454 
455 	WARN_ON_ONCE(!irqs_disabled());
456 	__raw_spin_lock(&ftrace_max_lock);
457 
458 	ftrace_disable_cpu();
459 
460 	ring_buffer_reset(max_tr.buffer);
461 	ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
462 
463 	ftrace_enable_cpu();
464 
465 	WARN_ON_ONCE(ret && ret != -EAGAIN);
466 
467 	__update_max_tr(tr, tsk, cpu);
468 	__raw_spin_unlock(&ftrace_max_lock);
469 }
470 
471 /**
472  * register_tracer - register a tracer with the ftrace system.
473  * @type - the plugin for the tracer
474  *
475  * Register a new plugin tracer.
476  */
477 int register_tracer(struct tracer *type)
478 __releases(kernel_lock)
479 __acquires(kernel_lock)
480 {
481 	struct tracer *t;
482 	int len;
483 	int ret = 0;
484 
485 	if (!type->name) {
486 		pr_info("Tracer must have a name\n");
487 		return -1;
488 	}
489 
490 	/*
491 	 * When this gets called we hold the BKL which means that
492 	 * preemption is disabled. Various trace selftests however
493 	 * need to disable and enable preemption for successful tests.
494 	 * So we drop the BKL here and grab it after the tests again.
495 	 */
496 	unlock_kernel();
497 	mutex_lock(&trace_types_lock);
498 
499 	tracing_selftest_running = true;
500 
501 	for (t = trace_types; t; t = t->next) {
502 		if (strcmp(type->name, t->name) == 0) {
503 			/* already found */
504 			pr_info("Trace %s already registered\n",
505 				type->name);
506 			ret = -1;
507 			goto out;
508 		}
509 	}
510 
511 	if (!type->set_flag)
512 		type->set_flag = &dummy_set_flag;
513 	if (!type->flags)
514 		type->flags = &dummy_tracer_flags;
515 	else
516 		if (!type->flags->opts)
517 			type->flags->opts = dummy_tracer_opt;
518 	if (!type->wait_pipe)
519 		type->wait_pipe = default_wait_pipe;
520 
521 
522 #ifdef CONFIG_FTRACE_STARTUP_TEST
523 	if (type->selftest && !tracing_selftest_disabled) {
524 		struct tracer *saved_tracer = current_trace;
525 		struct trace_array *tr = &global_trace;
526 		int i;
527 
528 		/*
529 		 * Run a selftest on this tracer.
530 		 * Here we reset the trace buffer, and set the current
531 		 * tracer to be this tracer. The tracer can then run some
532 		 * internal tracing to verify that everything is in order.
533 		 * If we fail, we do not register this tracer.
534 		 */
535 		for_each_tracing_cpu(i)
536 			tracing_reset(tr, i);
537 
538 		current_trace = type;
539 		/* the test is responsible for initializing and enabling */
540 		pr_info("Testing tracer %s: ", type->name);
541 		ret = type->selftest(type, tr);
542 		/* the test is responsible for resetting too */
543 		current_trace = saved_tracer;
544 		if (ret) {
545 			printk(KERN_CONT "FAILED!\n");
546 			goto out;
547 		}
548 		/* Only reset on passing, to avoid touching corrupted buffers */
549 		for_each_tracing_cpu(i)
550 			tracing_reset(tr, i);
551 
552 		printk(KERN_CONT "PASSED\n");
553 	}
554 #endif
555 
556 	type->next = trace_types;
557 	trace_types = type;
558 	len = strlen(type->name);
559 	if (len > max_tracer_type_len)
560 		max_tracer_type_len = len;
561 
562  out:
563 	tracing_selftest_running = false;
564 	mutex_unlock(&trace_types_lock);
565 
566 	if (ret || !default_bootup_tracer)
567 		goto out_unlock;
568 
569 	if (strncmp(default_bootup_tracer, type->name, BOOTUP_TRACER_SIZE))
570 		goto out_unlock;
571 
572 	printk(KERN_INFO "Starting tracer '%s'\n", type->name);
573 	/* Do we want this tracer to start on bootup? */
574 	tracing_set_tracer(type->name);
575 	default_bootup_tracer = NULL;
576 	/* disable other selftests, since this will break it. */
577 	tracing_selftest_disabled = 1;
578 #ifdef CONFIG_FTRACE_STARTUP_TEST
579 	printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
580 	       type->name);
581 #endif
582 
583  out_unlock:
584 	lock_kernel();
585 	return ret;
586 }
587 
588 void unregister_tracer(struct tracer *type)
589 {
590 	struct tracer **t;
591 	int len;
592 
593 	mutex_lock(&trace_types_lock);
594 	for (t = &trace_types; *t; t = &(*t)->next) {
595 		if (*t == type)
596 			goto found;
597 	}
598 	pr_info("Trace %s not registered\n", type->name);
599 	goto out;
600 
601  found:
602 	*t = (*t)->next;
603 
604 	if (type == current_trace && tracer_enabled) {
605 		tracer_enabled = 0;
606 		tracing_stop();
607 		if (current_trace->stop)
608 			current_trace->stop(&global_trace);
609 		current_trace = &nop_trace;
610 	}
611 
612 	if (strlen(type->name) != max_tracer_type_len)
613 		goto out;
614 
615 	max_tracer_type_len = 0;
616 	for (t = &trace_types; *t; t = &(*t)->next) {
617 		len = strlen((*t)->name);
618 		if (len > max_tracer_type_len)
619 			max_tracer_type_len = len;
620 	}
621  out:
622 	mutex_unlock(&trace_types_lock);
623 }
624 
625 void tracing_reset(struct trace_array *tr, int cpu)
626 {
627 	ftrace_disable_cpu();
628 	ring_buffer_reset_cpu(tr->buffer, cpu);
629 	ftrace_enable_cpu();
630 }
631 
632 void tracing_reset_online_cpus(struct trace_array *tr)
633 {
634 	int cpu;
635 
636 	tr->time_start = ftrace_now(tr->cpu);
637 
638 	for_each_online_cpu(cpu)
639 		tracing_reset(tr, cpu);
640 }
641 
642 void tracing_reset_current(int cpu)
643 {
644 	tracing_reset(&global_trace, cpu);
645 }
646 
647 void tracing_reset_current_online_cpus(void)
648 {
649 	tracing_reset_online_cpus(&global_trace);
650 }
651 
652 #define SAVED_CMDLINES 128
653 #define NO_CMDLINE_MAP UINT_MAX
654 static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
655 static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
656 static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
657 static int cmdline_idx;
658 static raw_spinlock_t trace_cmdline_lock = __RAW_SPIN_LOCK_UNLOCKED;
659 
660 /* temporary disable recording */
661 static atomic_t trace_record_cmdline_disabled __read_mostly;
662 
663 static void trace_init_cmdlines(void)
664 {
665 	memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
666 	memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
667 	cmdline_idx = 0;
668 }
669 
670 static int trace_stop_count;
671 static DEFINE_SPINLOCK(tracing_start_lock);
672 
673 /**
674  * ftrace_off_permanent - disable all ftrace code permanently
675  *
676  * This should only be called when a serious anomally has
677  * been detected.  This will turn off the function tracing,
678  * ring buffers, and other tracing utilites. It takes no
679  * locks and can be called from any context.
680  */
681 void ftrace_off_permanent(void)
682 {
683 	tracing_disabled = 1;
684 	ftrace_stop();
685 	tracing_off_permanent();
686 }
687 
688 /**
689  * tracing_start - quick start of the tracer
690  *
691  * If tracing is enabled but was stopped by tracing_stop,
692  * this will start the tracer back up.
693  */
694 void tracing_start(void)
695 {
696 	struct ring_buffer *buffer;
697 	unsigned long flags;
698 
699 	if (tracing_disabled)
700 		return;
701 
702 	spin_lock_irqsave(&tracing_start_lock, flags);
703 	if (--trace_stop_count) {
704 		if (trace_stop_count < 0) {
705 			/* Someone screwed up their debugging */
706 			WARN_ON_ONCE(1);
707 			trace_stop_count = 0;
708 		}
709 		goto out;
710 	}
711 
712 
713 	buffer = global_trace.buffer;
714 	if (buffer)
715 		ring_buffer_record_enable(buffer);
716 
717 	buffer = max_tr.buffer;
718 	if (buffer)
719 		ring_buffer_record_enable(buffer);
720 
721 	ftrace_start();
722  out:
723 	spin_unlock_irqrestore(&tracing_start_lock, flags);
724 }
725 
726 /**
727  * tracing_stop - quick stop of the tracer
728  *
729  * Light weight way to stop tracing. Use in conjunction with
730  * tracing_start.
731  */
732 void tracing_stop(void)
733 {
734 	struct ring_buffer *buffer;
735 	unsigned long flags;
736 
737 	ftrace_stop();
738 	spin_lock_irqsave(&tracing_start_lock, flags);
739 	if (trace_stop_count++)
740 		goto out;
741 
742 	buffer = global_trace.buffer;
743 	if (buffer)
744 		ring_buffer_record_disable(buffer);
745 
746 	buffer = max_tr.buffer;
747 	if (buffer)
748 		ring_buffer_record_disable(buffer);
749 
750  out:
751 	spin_unlock_irqrestore(&tracing_start_lock, flags);
752 }
753 
754 void trace_stop_cmdline_recording(void);
755 
756 static void trace_save_cmdline(struct task_struct *tsk)
757 {
758 	unsigned pid, idx;
759 
760 	if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
761 		return;
762 
763 	/*
764 	 * It's not the end of the world if we don't get
765 	 * the lock, but we also don't want to spin
766 	 * nor do we want to disable interrupts,
767 	 * so if we miss here, then better luck next time.
768 	 */
769 	if (!__raw_spin_trylock(&trace_cmdline_lock))
770 		return;
771 
772 	idx = map_pid_to_cmdline[tsk->pid];
773 	if (idx == NO_CMDLINE_MAP) {
774 		idx = (cmdline_idx + 1) % SAVED_CMDLINES;
775 
776 		/*
777 		 * Check whether the cmdline buffer at idx has a pid
778 		 * mapped. We are going to overwrite that entry so we
779 		 * need to clear the map_pid_to_cmdline. Otherwise we
780 		 * would read the new comm for the old pid.
781 		 */
782 		pid = map_cmdline_to_pid[idx];
783 		if (pid != NO_CMDLINE_MAP)
784 			map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
785 
786 		map_cmdline_to_pid[idx] = tsk->pid;
787 		map_pid_to_cmdline[tsk->pid] = idx;
788 
789 		cmdline_idx = idx;
790 	}
791 
792 	memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
793 
794 	__raw_spin_unlock(&trace_cmdline_lock);
795 }
796 
797 void trace_find_cmdline(int pid, char comm[])
798 {
799 	unsigned map;
800 
801 	if (!pid) {
802 		strcpy(comm, "<idle>");
803 		return;
804 	}
805 
806 	if (pid > PID_MAX_DEFAULT) {
807 		strcpy(comm, "<...>");
808 		return;
809 	}
810 
811 	preempt_disable();
812 	__raw_spin_lock(&trace_cmdline_lock);
813 	map = map_pid_to_cmdline[pid];
814 	if (map != NO_CMDLINE_MAP)
815 		strcpy(comm, saved_cmdlines[map]);
816 	else
817 		strcpy(comm, "<...>");
818 
819 	__raw_spin_unlock(&trace_cmdline_lock);
820 	preempt_enable();
821 }
822 
823 void tracing_record_cmdline(struct task_struct *tsk)
824 {
825 	if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
826 	    !tracing_is_on())
827 		return;
828 
829 	trace_save_cmdline(tsk);
830 }
831 
832 void
833 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
834 			     int pc)
835 {
836 	struct task_struct *tsk = current;
837 
838 	entry->preempt_count		= pc & 0xff;
839 	entry->pid			= (tsk) ? tsk->pid : 0;
840 	entry->tgid			= (tsk) ? tsk->tgid : 0;
841 	entry->flags =
842 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
843 		(irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
844 #else
845 		TRACE_FLAG_IRQS_NOSUPPORT |
846 #endif
847 		((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
848 		((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
849 		(need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
850 }
851 
852 struct ring_buffer_event *trace_buffer_lock_reserve(struct trace_array *tr,
853 						    int type,
854 						    unsigned long len,
855 						    unsigned long flags, int pc)
856 {
857 	struct ring_buffer_event *event;
858 
859 	event = ring_buffer_lock_reserve(tr->buffer, len);
860 	if (event != NULL) {
861 		struct trace_entry *ent = ring_buffer_event_data(event);
862 
863 		tracing_generic_entry_update(ent, flags, pc);
864 		ent->type = type;
865 	}
866 
867 	return event;
868 }
869 static void ftrace_trace_stack(struct trace_array *tr,
870 			       unsigned long flags, int skip, int pc);
871 static void ftrace_trace_userstack(struct trace_array *tr,
872 				   unsigned long flags, int pc);
873 
874 static inline void __trace_buffer_unlock_commit(struct trace_array *tr,
875 					struct ring_buffer_event *event,
876 					unsigned long flags, int pc,
877 					int wake)
878 {
879 	ring_buffer_unlock_commit(tr->buffer, event);
880 
881 	ftrace_trace_stack(tr, flags, 6, pc);
882 	ftrace_trace_userstack(tr, flags, pc);
883 
884 	if (wake)
885 		trace_wake_up();
886 }
887 
888 void trace_buffer_unlock_commit(struct trace_array *tr,
889 					struct ring_buffer_event *event,
890 					unsigned long flags, int pc)
891 {
892 	__trace_buffer_unlock_commit(tr, event, flags, pc, 1);
893 }
894 
895 struct ring_buffer_event *
896 trace_current_buffer_lock_reserve(int type, unsigned long len,
897 				  unsigned long flags, int pc)
898 {
899 	return trace_buffer_lock_reserve(&global_trace,
900 					 type, len, flags, pc);
901 }
902 EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
903 
904 void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
905 					unsigned long flags, int pc)
906 {
907 	__trace_buffer_unlock_commit(&global_trace, event, flags, pc, 1);
908 }
909 EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
910 
911 void trace_nowake_buffer_unlock_commit(struct ring_buffer_event *event,
912 					unsigned long flags, int pc)
913 {
914 	__trace_buffer_unlock_commit(&global_trace, event, flags, pc, 0);
915 }
916 EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
917 
918 void trace_current_buffer_discard_commit(struct ring_buffer_event *event)
919 {
920 	ring_buffer_discard_commit(global_trace.buffer, event);
921 }
922 EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
923 
924 void
925 trace_function(struct trace_array *tr,
926 	       unsigned long ip, unsigned long parent_ip, unsigned long flags,
927 	       int pc)
928 {
929 	struct ftrace_event_call *call = &event_function;
930 	struct ring_buffer_event *event;
931 	struct ftrace_entry *entry;
932 
933 	/* If we are reading the ring buffer, don't trace */
934 	if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
935 		return;
936 
937 	event = trace_buffer_lock_reserve(tr, TRACE_FN, sizeof(*entry),
938 					  flags, pc);
939 	if (!event)
940 		return;
941 	entry	= ring_buffer_event_data(event);
942 	entry->ip			= ip;
943 	entry->parent_ip		= parent_ip;
944 
945 	if (!filter_check_discard(call, entry, tr->buffer, event))
946 		ring_buffer_unlock_commit(tr->buffer, event);
947 }
948 
949 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
950 static int __trace_graph_entry(struct trace_array *tr,
951 				struct ftrace_graph_ent *trace,
952 				unsigned long flags,
953 				int pc)
954 {
955 	struct ftrace_event_call *call = &event_funcgraph_entry;
956 	struct ring_buffer_event *event;
957 	struct ftrace_graph_ent_entry *entry;
958 
959 	if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
960 		return 0;
961 
962 	event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_ENT,
963 					  sizeof(*entry), flags, pc);
964 	if (!event)
965 		return 0;
966 	entry	= ring_buffer_event_data(event);
967 	entry->graph_ent			= *trace;
968 	if (!filter_current_check_discard(call, entry, event))
969 		ring_buffer_unlock_commit(global_trace.buffer, event);
970 
971 	return 1;
972 }
973 
974 static void __trace_graph_return(struct trace_array *tr,
975 				struct ftrace_graph_ret *trace,
976 				unsigned long flags,
977 				int pc)
978 {
979 	struct ftrace_event_call *call = &event_funcgraph_exit;
980 	struct ring_buffer_event *event;
981 	struct ftrace_graph_ret_entry *entry;
982 
983 	if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
984 		return;
985 
986 	event = trace_buffer_lock_reserve(&global_trace, TRACE_GRAPH_RET,
987 					  sizeof(*entry), flags, pc);
988 	if (!event)
989 		return;
990 	entry	= ring_buffer_event_data(event);
991 	entry->ret				= *trace;
992 	if (!filter_current_check_discard(call, entry, event))
993 		ring_buffer_unlock_commit(global_trace.buffer, event);
994 }
995 #endif
996 
997 void
998 ftrace(struct trace_array *tr, struct trace_array_cpu *data,
999        unsigned long ip, unsigned long parent_ip, unsigned long flags,
1000        int pc)
1001 {
1002 	if (likely(!atomic_read(&data->disabled)))
1003 		trace_function(tr, ip, parent_ip, flags, pc);
1004 }
1005 
1006 static void __ftrace_trace_stack(struct trace_array *tr,
1007 				 unsigned long flags,
1008 				 int skip, int pc)
1009 {
1010 #ifdef CONFIG_STACKTRACE
1011 	struct ftrace_event_call *call = &event_kernel_stack;
1012 	struct ring_buffer_event *event;
1013 	struct stack_entry *entry;
1014 	struct stack_trace trace;
1015 
1016 	event = trace_buffer_lock_reserve(tr, TRACE_STACK,
1017 					  sizeof(*entry), flags, pc);
1018 	if (!event)
1019 		return;
1020 	entry	= ring_buffer_event_data(event);
1021 	memset(&entry->caller, 0, sizeof(entry->caller));
1022 
1023 	trace.nr_entries	= 0;
1024 	trace.max_entries	= FTRACE_STACK_ENTRIES;
1025 	trace.skip		= skip;
1026 	trace.entries		= entry->caller;
1027 
1028 	save_stack_trace(&trace);
1029 	if (!filter_check_discard(call, entry, tr->buffer, event))
1030 		ring_buffer_unlock_commit(tr->buffer, event);
1031 #endif
1032 }
1033 
1034 static void ftrace_trace_stack(struct trace_array *tr,
1035 			       unsigned long flags,
1036 			       int skip, int pc)
1037 {
1038 	if (!(trace_flags & TRACE_ITER_STACKTRACE))
1039 		return;
1040 
1041 	__ftrace_trace_stack(tr, flags, skip, pc);
1042 }
1043 
1044 void __trace_stack(struct trace_array *tr,
1045 		   unsigned long flags,
1046 		   int skip, int pc)
1047 {
1048 	__ftrace_trace_stack(tr, flags, skip, pc);
1049 }
1050 
1051 static void ftrace_trace_userstack(struct trace_array *tr,
1052 				   unsigned long flags, int pc)
1053 {
1054 #ifdef CONFIG_STACKTRACE
1055 	struct ftrace_event_call *call = &event_user_stack;
1056 	struct ring_buffer_event *event;
1057 	struct userstack_entry *entry;
1058 	struct stack_trace trace;
1059 
1060 	if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
1061 		return;
1062 
1063 	event = trace_buffer_lock_reserve(tr, TRACE_USER_STACK,
1064 					  sizeof(*entry), flags, pc);
1065 	if (!event)
1066 		return;
1067 	entry	= ring_buffer_event_data(event);
1068 
1069 	memset(&entry->caller, 0, sizeof(entry->caller));
1070 
1071 	trace.nr_entries	= 0;
1072 	trace.max_entries	= FTRACE_STACK_ENTRIES;
1073 	trace.skip		= 0;
1074 	trace.entries		= entry->caller;
1075 
1076 	save_stack_trace_user(&trace);
1077 	if (!filter_check_discard(call, entry, tr->buffer, event))
1078 		ring_buffer_unlock_commit(tr->buffer, event);
1079 #endif
1080 }
1081 
1082 #ifdef UNUSED
1083 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
1084 {
1085 	ftrace_trace_userstack(tr, flags, preempt_count());
1086 }
1087 #endif /* UNUSED */
1088 
1089 static void
1090 ftrace_trace_special(void *__tr,
1091 		     unsigned long arg1, unsigned long arg2, unsigned long arg3,
1092 		     int pc)
1093 {
1094 	struct ring_buffer_event *event;
1095 	struct trace_array *tr = __tr;
1096 	struct special_entry *entry;
1097 
1098 	event = trace_buffer_lock_reserve(tr, TRACE_SPECIAL,
1099 					  sizeof(*entry), 0, pc);
1100 	if (!event)
1101 		return;
1102 	entry	= ring_buffer_event_data(event);
1103 	entry->arg1			= arg1;
1104 	entry->arg2			= arg2;
1105 	entry->arg3			= arg3;
1106 	trace_buffer_unlock_commit(tr, event, 0, pc);
1107 }
1108 
1109 void
1110 __trace_special(void *__tr, void *__data,
1111 		unsigned long arg1, unsigned long arg2, unsigned long arg3)
1112 {
1113 	ftrace_trace_special(__tr, arg1, arg2, arg3, preempt_count());
1114 }
1115 
1116 void
1117 tracing_sched_switch_trace(struct trace_array *tr,
1118 			   struct task_struct *prev,
1119 			   struct task_struct *next,
1120 			   unsigned long flags, int pc)
1121 {
1122 	struct ftrace_event_call *call = &event_context_switch;
1123 	struct ring_buffer_event *event;
1124 	struct ctx_switch_entry *entry;
1125 
1126 	event = trace_buffer_lock_reserve(tr, TRACE_CTX,
1127 					  sizeof(*entry), flags, pc);
1128 	if (!event)
1129 		return;
1130 	entry	= ring_buffer_event_data(event);
1131 	entry->prev_pid			= prev->pid;
1132 	entry->prev_prio		= prev->prio;
1133 	entry->prev_state		= prev->state;
1134 	entry->next_pid			= next->pid;
1135 	entry->next_prio		= next->prio;
1136 	entry->next_state		= next->state;
1137 	entry->next_cpu	= task_cpu(next);
1138 
1139 	if (!filter_check_discard(call, entry, tr->buffer, event))
1140 		trace_buffer_unlock_commit(tr, event, flags, pc);
1141 }
1142 
1143 void
1144 tracing_sched_wakeup_trace(struct trace_array *tr,
1145 			   struct task_struct *wakee,
1146 			   struct task_struct *curr,
1147 			   unsigned long flags, int pc)
1148 {
1149 	struct ftrace_event_call *call = &event_wakeup;
1150 	struct ring_buffer_event *event;
1151 	struct ctx_switch_entry *entry;
1152 
1153 	event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
1154 					  sizeof(*entry), flags, pc);
1155 	if (!event)
1156 		return;
1157 	entry	= ring_buffer_event_data(event);
1158 	entry->prev_pid			= curr->pid;
1159 	entry->prev_prio		= curr->prio;
1160 	entry->prev_state		= curr->state;
1161 	entry->next_pid			= wakee->pid;
1162 	entry->next_prio		= wakee->prio;
1163 	entry->next_state		= wakee->state;
1164 	entry->next_cpu			= task_cpu(wakee);
1165 
1166 	if (!filter_check_discard(call, entry, tr->buffer, event))
1167 		ring_buffer_unlock_commit(tr->buffer, event);
1168 	ftrace_trace_stack(tr, flags, 6, pc);
1169 	ftrace_trace_userstack(tr, flags, pc);
1170 }
1171 
1172 void
1173 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3)
1174 {
1175 	struct trace_array *tr = &global_trace;
1176 	struct trace_array_cpu *data;
1177 	unsigned long flags;
1178 	int cpu;
1179 	int pc;
1180 
1181 	if (tracing_disabled)
1182 		return;
1183 
1184 	pc = preempt_count();
1185 	local_irq_save(flags);
1186 	cpu = raw_smp_processor_id();
1187 	data = tr->data[cpu];
1188 
1189 	if (likely(atomic_inc_return(&data->disabled) == 1))
1190 		ftrace_trace_special(tr, arg1, arg2, arg3, pc);
1191 
1192 	atomic_dec(&data->disabled);
1193 	local_irq_restore(flags);
1194 }
1195 
1196 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1197 int trace_graph_entry(struct ftrace_graph_ent *trace)
1198 {
1199 	struct trace_array *tr = &global_trace;
1200 	struct trace_array_cpu *data;
1201 	unsigned long flags;
1202 	long disabled;
1203 	int ret;
1204 	int cpu;
1205 	int pc;
1206 
1207 	if (!ftrace_trace_task(current))
1208 		return 0;
1209 
1210 	if (!ftrace_graph_addr(trace->func))
1211 		return 0;
1212 
1213 	local_irq_save(flags);
1214 	cpu = raw_smp_processor_id();
1215 	data = tr->data[cpu];
1216 	disabled = atomic_inc_return(&data->disabled);
1217 	if (likely(disabled == 1)) {
1218 		pc = preempt_count();
1219 		ret = __trace_graph_entry(tr, trace, flags, pc);
1220 	} else {
1221 		ret = 0;
1222 	}
1223 	/* Only do the atomic if it is not already set */
1224 	if (!test_tsk_trace_graph(current))
1225 		set_tsk_trace_graph(current);
1226 
1227 	atomic_dec(&data->disabled);
1228 	local_irq_restore(flags);
1229 
1230 	return ret;
1231 }
1232 
1233 void trace_graph_return(struct ftrace_graph_ret *trace)
1234 {
1235 	struct trace_array *tr = &global_trace;
1236 	struct trace_array_cpu *data;
1237 	unsigned long flags;
1238 	long disabled;
1239 	int cpu;
1240 	int pc;
1241 
1242 	local_irq_save(flags);
1243 	cpu = raw_smp_processor_id();
1244 	data = tr->data[cpu];
1245 	disabled = atomic_inc_return(&data->disabled);
1246 	if (likely(disabled == 1)) {
1247 		pc = preempt_count();
1248 		__trace_graph_return(tr, trace, flags, pc);
1249 	}
1250 	if (!trace->depth)
1251 		clear_tsk_trace_graph(current);
1252 	atomic_dec(&data->disabled);
1253 	local_irq_restore(flags);
1254 }
1255 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1256 
1257 
1258 /**
1259  * trace_vbprintk - write binary msg to tracing buffer
1260  *
1261  */
1262 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
1263 {
1264 	static raw_spinlock_t trace_buf_lock =
1265 		(raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
1266 	static u32 trace_buf[TRACE_BUF_SIZE];
1267 
1268 	struct ftrace_event_call *call = &event_bprint;
1269 	struct ring_buffer_event *event;
1270 	struct trace_array *tr = &global_trace;
1271 	struct trace_array_cpu *data;
1272 	struct bprint_entry *entry;
1273 	unsigned long flags;
1274 	int disable;
1275 	int resched;
1276 	int cpu, len = 0, size, pc;
1277 
1278 	if (unlikely(tracing_selftest_running || tracing_disabled))
1279 		return 0;
1280 
1281 	/* Don't pollute graph traces with trace_vprintk internals */
1282 	pause_graph_tracing();
1283 
1284 	pc = preempt_count();
1285 	resched = ftrace_preempt_disable();
1286 	cpu = raw_smp_processor_id();
1287 	data = tr->data[cpu];
1288 
1289 	disable = atomic_inc_return(&data->disabled);
1290 	if (unlikely(disable != 1))
1291 		goto out;
1292 
1293 	/* Lockdep uses trace_printk for lock tracing */
1294 	local_irq_save(flags);
1295 	__raw_spin_lock(&trace_buf_lock);
1296 	len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1297 
1298 	if (len > TRACE_BUF_SIZE || len < 0)
1299 		goto out_unlock;
1300 
1301 	size = sizeof(*entry) + sizeof(u32) * len;
1302 	event = trace_buffer_lock_reserve(tr, TRACE_BPRINT, size, flags, pc);
1303 	if (!event)
1304 		goto out_unlock;
1305 	entry = ring_buffer_event_data(event);
1306 	entry->ip			= ip;
1307 	entry->fmt			= fmt;
1308 
1309 	memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1310 	if (!filter_check_discard(call, entry, tr->buffer, event))
1311 		ring_buffer_unlock_commit(tr->buffer, event);
1312 
1313 out_unlock:
1314 	__raw_spin_unlock(&trace_buf_lock);
1315 	local_irq_restore(flags);
1316 
1317 out:
1318 	atomic_dec_return(&data->disabled);
1319 	ftrace_preempt_enable(resched);
1320 	unpause_graph_tracing();
1321 
1322 	return len;
1323 }
1324 EXPORT_SYMBOL_GPL(trace_vbprintk);
1325 
1326 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
1327 {
1328 	static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
1329 	static char trace_buf[TRACE_BUF_SIZE];
1330 
1331 	struct ftrace_event_call *call = &event_print;
1332 	struct ring_buffer_event *event;
1333 	struct trace_array *tr = &global_trace;
1334 	struct trace_array_cpu *data;
1335 	int cpu, len = 0, size, pc;
1336 	struct print_entry *entry;
1337 	unsigned long irq_flags;
1338 	int disable;
1339 
1340 	if (tracing_disabled || tracing_selftest_running)
1341 		return 0;
1342 
1343 	pc = preempt_count();
1344 	preempt_disable_notrace();
1345 	cpu = raw_smp_processor_id();
1346 	data = tr->data[cpu];
1347 
1348 	disable = atomic_inc_return(&data->disabled);
1349 	if (unlikely(disable != 1))
1350 		goto out;
1351 
1352 	pause_graph_tracing();
1353 	raw_local_irq_save(irq_flags);
1354 	__raw_spin_lock(&trace_buf_lock);
1355 	len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1356 
1357 	len = min(len, TRACE_BUF_SIZE-1);
1358 	trace_buf[len] = 0;
1359 
1360 	size = sizeof(*entry) + len + 1;
1361 	event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
1362 	if (!event)
1363 		goto out_unlock;
1364 	entry = ring_buffer_event_data(event);
1365 	entry->ip			= ip;
1366 
1367 	memcpy(&entry->buf, trace_buf, len);
1368 	entry->buf[len] = 0;
1369 	if (!filter_check_discard(call, entry, tr->buffer, event))
1370 		ring_buffer_unlock_commit(tr->buffer, event);
1371 
1372  out_unlock:
1373 	__raw_spin_unlock(&trace_buf_lock);
1374 	raw_local_irq_restore(irq_flags);
1375 	unpause_graph_tracing();
1376  out:
1377 	atomic_dec_return(&data->disabled);
1378 	preempt_enable_notrace();
1379 
1380 	return len;
1381 }
1382 EXPORT_SYMBOL_GPL(trace_vprintk);
1383 
1384 enum trace_file_type {
1385 	TRACE_FILE_LAT_FMT	= 1,
1386 	TRACE_FILE_ANNOTATE	= 2,
1387 };
1388 
1389 static void trace_iterator_increment(struct trace_iterator *iter)
1390 {
1391 	/* Don't allow ftrace to trace into the ring buffers */
1392 	ftrace_disable_cpu();
1393 
1394 	iter->idx++;
1395 	if (iter->buffer_iter[iter->cpu])
1396 		ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
1397 
1398 	ftrace_enable_cpu();
1399 }
1400 
1401 static struct trace_entry *
1402 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts)
1403 {
1404 	struct ring_buffer_event *event;
1405 	struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
1406 
1407 	/* Don't allow ftrace to trace into the ring buffers */
1408 	ftrace_disable_cpu();
1409 
1410 	if (buf_iter)
1411 		event = ring_buffer_iter_peek(buf_iter, ts);
1412 	else
1413 		event = ring_buffer_peek(iter->tr->buffer, cpu, ts);
1414 
1415 	ftrace_enable_cpu();
1416 
1417 	return event ? ring_buffer_event_data(event) : NULL;
1418 }
1419 
1420 static struct trace_entry *
1421 __find_next_entry(struct trace_iterator *iter, int *ent_cpu, u64 *ent_ts)
1422 {
1423 	struct ring_buffer *buffer = iter->tr->buffer;
1424 	struct trace_entry *ent, *next = NULL;
1425 	int cpu_file = iter->cpu_file;
1426 	u64 next_ts = 0, ts;
1427 	int next_cpu = -1;
1428 	int cpu;
1429 
1430 	/*
1431 	 * If we are in a per_cpu trace file, don't bother by iterating over
1432 	 * all cpu and peek directly.
1433 	 */
1434 	if (cpu_file > TRACE_PIPE_ALL_CPU) {
1435 		if (ring_buffer_empty_cpu(buffer, cpu_file))
1436 			return NULL;
1437 		ent = peek_next_entry(iter, cpu_file, ent_ts);
1438 		if (ent_cpu)
1439 			*ent_cpu = cpu_file;
1440 
1441 		return ent;
1442 	}
1443 
1444 	for_each_tracing_cpu(cpu) {
1445 
1446 		if (ring_buffer_empty_cpu(buffer, cpu))
1447 			continue;
1448 
1449 		ent = peek_next_entry(iter, cpu, &ts);
1450 
1451 		/*
1452 		 * Pick the entry with the smallest timestamp:
1453 		 */
1454 		if (ent && (!next || ts < next_ts)) {
1455 			next = ent;
1456 			next_cpu = cpu;
1457 			next_ts = ts;
1458 		}
1459 	}
1460 
1461 	if (ent_cpu)
1462 		*ent_cpu = next_cpu;
1463 
1464 	if (ent_ts)
1465 		*ent_ts = next_ts;
1466 
1467 	return next;
1468 }
1469 
1470 /* Find the next real entry, without updating the iterator itself */
1471 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
1472 					  int *ent_cpu, u64 *ent_ts)
1473 {
1474 	return __find_next_entry(iter, ent_cpu, ent_ts);
1475 }
1476 
1477 /* Find the next real entry, and increment the iterator to the next entry */
1478 static void *find_next_entry_inc(struct trace_iterator *iter)
1479 {
1480 	iter->ent = __find_next_entry(iter, &iter->cpu, &iter->ts);
1481 
1482 	if (iter->ent)
1483 		trace_iterator_increment(iter);
1484 
1485 	return iter->ent ? iter : NULL;
1486 }
1487 
1488 static void trace_consume(struct trace_iterator *iter)
1489 {
1490 	/* Don't allow ftrace to trace into the ring buffers */
1491 	ftrace_disable_cpu();
1492 	ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts);
1493 	ftrace_enable_cpu();
1494 }
1495 
1496 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
1497 {
1498 	struct trace_iterator *iter = m->private;
1499 	int i = (int)*pos;
1500 	void *ent;
1501 
1502 	(*pos)++;
1503 
1504 	/* can't go backwards */
1505 	if (iter->idx > i)
1506 		return NULL;
1507 
1508 	if (iter->idx < 0)
1509 		ent = find_next_entry_inc(iter);
1510 	else
1511 		ent = iter;
1512 
1513 	while (ent && iter->idx < i)
1514 		ent = find_next_entry_inc(iter);
1515 
1516 	iter->pos = *pos;
1517 
1518 	return ent;
1519 }
1520 
1521 /*
1522  * No necessary locking here. The worst thing which can
1523  * happen is loosing events consumed at the same time
1524  * by a trace_pipe reader.
1525  * Other than that, we don't risk to crash the ring buffer
1526  * because it serializes the readers.
1527  *
1528  * The current tracer is copied to avoid a global locking
1529  * all around.
1530  */
1531 static void *s_start(struct seq_file *m, loff_t *pos)
1532 {
1533 	struct trace_iterator *iter = m->private;
1534 	static struct tracer *old_tracer;
1535 	int cpu_file = iter->cpu_file;
1536 	void *p = NULL;
1537 	loff_t l = 0;
1538 	int cpu;
1539 
1540 	/* copy the tracer to avoid using a global lock all around */
1541 	mutex_lock(&trace_types_lock);
1542 	if (unlikely(old_tracer != current_trace && current_trace)) {
1543 		old_tracer = current_trace;
1544 		*iter->trace = *current_trace;
1545 	}
1546 	mutex_unlock(&trace_types_lock);
1547 
1548 	atomic_inc(&trace_record_cmdline_disabled);
1549 
1550 	if (*pos != iter->pos) {
1551 		iter->ent = NULL;
1552 		iter->cpu = 0;
1553 		iter->idx = -1;
1554 
1555 		ftrace_disable_cpu();
1556 
1557 		if (cpu_file == TRACE_PIPE_ALL_CPU) {
1558 			for_each_tracing_cpu(cpu)
1559 				ring_buffer_iter_reset(iter->buffer_iter[cpu]);
1560 		} else
1561 			ring_buffer_iter_reset(iter->buffer_iter[cpu_file]);
1562 
1563 
1564 		ftrace_enable_cpu();
1565 
1566 		for (p = iter; p && l < *pos; p = s_next(m, p, &l))
1567 			;
1568 
1569 	} else {
1570 		l = *pos - 1;
1571 		p = s_next(m, p, &l);
1572 	}
1573 
1574 	trace_event_read_lock();
1575 	return p;
1576 }
1577 
1578 static void s_stop(struct seq_file *m, void *p)
1579 {
1580 	atomic_dec(&trace_record_cmdline_disabled);
1581 	trace_event_read_unlock();
1582 }
1583 
1584 static void print_lat_help_header(struct seq_file *m)
1585 {
1586 	seq_puts(m, "#                  _------=> CPU#            \n");
1587 	seq_puts(m, "#                 / _-----=> irqs-off        \n");
1588 	seq_puts(m, "#                | / _----=> need-resched    \n");
1589 	seq_puts(m, "#                || / _---=> hardirq/softirq \n");
1590 	seq_puts(m, "#                ||| / _--=> preempt-depth   \n");
1591 	seq_puts(m, "#                |||| /                      \n");
1592 	seq_puts(m, "#                |||||     delay             \n");
1593 	seq_puts(m, "#  cmd     pid   ||||| time  |   caller      \n");
1594 	seq_puts(m, "#     \\   /      |||||   \\   |   /           \n");
1595 }
1596 
1597 static void print_func_help_header(struct seq_file *m)
1598 {
1599 	seq_puts(m, "#           TASK-PID    CPU#    TIMESTAMP  FUNCTION\n");
1600 	seq_puts(m, "#              | |       |          |         |\n");
1601 }
1602 
1603 
1604 static void
1605 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
1606 {
1607 	unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1608 	struct trace_array *tr = iter->tr;
1609 	struct trace_array_cpu *data = tr->data[tr->cpu];
1610 	struct tracer *type = current_trace;
1611 	unsigned long total;
1612 	unsigned long entries;
1613 	const char *name = "preemption";
1614 
1615 	if (type)
1616 		name = type->name;
1617 
1618 	entries = ring_buffer_entries(iter->tr->buffer);
1619 	total = entries +
1620 		ring_buffer_overruns(iter->tr->buffer);
1621 
1622 	seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
1623 		   name, UTS_RELEASE);
1624 	seq_puts(m, "# -----------------------------------"
1625 		 "---------------------------------\n");
1626 	seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
1627 		   " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1628 		   nsecs_to_usecs(data->saved_latency),
1629 		   entries,
1630 		   total,
1631 		   tr->cpu,
1632 #if defined(CONFIG_PREEMPT_NONE)
1633 		   "server",
1634 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1635 		   "desktop",
1636 #elif defined(CONFIG_PREEMPT)
1637 		   "preempt",
1638 #else
1639 		   "unknown",
1640 #endif
1641 		   /* These are reserved for later use */
1642 		   0, 0, 0, 0);
1643 #ifdef CONFIG_SMP
1644 	seq_printf(m, " #P:%d)\n", num_online_cpus());
1645 #else
1646 	seq_puts(m, ")\n");
1647 #endif
1648 	seq_puts(m, "#    -----------------\n");
1649 	seq_printf(m, "#    | task: %.16s-%d "
1650 		   "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1651 		   data->comm, data->pid, data->uid, data->nice,
1652 		   data->policy, data->rt_priority);
1653 	seq_puts(m, "#    -----------------\n");
1654 
1655 	if (data->critical_start) {
1656 		seq_puts(m, "#  => started at: ");
1657 		seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
1658 		trace_print_seq(m, &iter->seq);
1659 		seq_puts(m, "\n#  => ended at:   ");
1660 		seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
1661 		trace_print_seq(m, &iter->seq);
1662 		seq_puts(m, "#\n");
1663 	}
1664 
1665 	seq_puts(m, "#\n");
1666 }
1667 
1668 static void test_cpu_buff_start(struct trace_iterator *iter)
1669 {
1670 	struct trace_seq *s = &iter->seq;
1671 
1672 	if (!(trace_flags & TRACE_ITER_ANNOTATE))
1673 		return;
1674 
1675 	if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
1676 		return;
1677 
1678 	if (cpumask_test_cpu(iter->cpu, iter->started))
1679 		return;
1680 
1681 	cpumask_set_cpu(iter->cpu, iter->started);
1682 
1683 	/* Don't print started cpu buffer for the first entry of the trace */
1684 	if (iter->idx > 1)
1685 		trace_seq_printf(s, "##### CPU %u buffer started ####\n",
1686 				iter->cpu);
1687 }
1688 
1689 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
1690 {
1691 	struct trace_seq *s = &iter->seq;
1692 	unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
1693 	struct trace_entry *entry;
1694 	struct trace_event *event;
1695 
1696 	entry = iter->ent;
1697 
1698 	test_cpu_buff_start(iter);
1699 
1700 	event = ftrace_find_event(entry->type);
1701 
1702 	if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1703 		if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1704 			if (!trace_print_lat_context(iter))
1705 				goto partial;
1706 		} else {
1707 			if (!trace_print_context(iter))
1708 				goto partial;
1709 		}
1710 	}
1711 
1712 	if (event)
1713 		return event->trace(iter, sym_flags);
1714 
1715 	if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
1716 		goto partial;
1717 
1718 	return TRACE_TYPE_HANDLED;
1719 partial:
1720 	return TRACE_TYPE_PARTIAL_LINE;
1721 }
1722 
1723 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
1724 {
1725 	struct trace_seq *s = &iter->seq;
1726 	struct trace_entry *entry;
1727 	struct trace_event *event;
1728 
1729 	entry = iter->ent;
1730 
1731 	if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1732 		if (!trace_seq_printf(s, "%d %d %llu ",
1733 				      entry->pid, iter->cpu, iter->ts))
1734 			goto partial;
1735 	}
1736 
1737 	event = ftrace_find_event(entry->type);
1738 	if (event)
1739 		return event->raw(iter, 0);
1740 
1741 	if (!trace_seq_printf(s, "%d ?\n", entry->type))
1742 		goto partial;
1743 
1744 	return TRACE_TYPE_HANDLED;
1745 partial:
1746 	return TRACE_TYPE_PARTIAL_LINE;
1747 }
1748 
1749 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
1750 {
1751 	struct trace_seq *s = &iter->seq;
1752 	unsigned char newline = '\n';
1753 	struct trace_entry *entry;
1754 	struct trace_event *event;
1755 
1756 	entry = iter->ent;
1757 
1758 	if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1759 		SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
1760 		SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
1761 		SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
1762 	}
1763 
1764 	event = ftrace_find_event(entry->type);
1765 	if (event) {
1766 		enum print_line_t ret = event->hex(iter, 0);
1767 		if (ret != TRACE_TYPE_HANDLED)
1768 			return ret;
1769 	}
1770 
1771 	SEQ_PUT_FIELD_RET(s, newline);
1772 
1773 	return TRACE_TYPE_HANDLED;
1774 }
1775 
1776 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
1777 {
1778 	struct trace_seq *s = &iter->seq;
1779 	struct trace_entry *entry;
1780 	struct trace_event *event;
1781 
1782 	entry = iter->ent;
1783 
1784 	if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
1785 		SEQ_PUT_FIELD_RET(s, entry->pid);
1786 		SEQ_PUT_FIELD_RET(s, iter->cpu);
1787 		SEQ_PUT_FIELD_RET(s, iter->ts);
1788 	}
1789 
1790 	event = ftrace_find_event(entry->type);
1791 	return event ? event->binary(iter, 0) : TRACE_TYPE_HANDLED;
1792 }
1793 
1794 static int trace_empty(struct trace_iterator *iter)
1795 {
1796 	int cpu;
1797 
1798 	/* If we are looking at one CPU buffer, only check that one */
1799 	if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
1800 		cpu = iter->cpu_file;
1801 		if (iter->buffer_iter[cpu]) {
1802 			if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1803 				return 0;
1804 		} else {
1805 			if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1806 				return 0;
1807 		}
1808 		return 1;
1809 	}
1810 
1811 	for_each_tracing_cpu(cpu) {
1812 		if (iter->buffer_iter[cpu]) {
1813 			if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
1814 				return 0;
1815 		} else {
1816 			if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
1817 				return 0;
1818 		}
1819 	}
1820 
1821 	return 1;
1822 }
1823 
1824 /*  Called with trace_event_read_lock() held. */
1825 static enum print_line_t print_trace_line(struct trace_iterator *iter)
1826 {
1827 	enum print_line_t ret;
1828 
1829 	if (iter->trace && iter->trace->print_line) {
1830 		ret = iter->trace->print_line(iter);
1831 		if (ret != TRACE_TYPE_UNHANDLED)
1832 			return ret;
1833 	}
1834 
1835 	if (iter->ent->type == TRACE_BPRINT &&
1836 			trace_flags & TRACE_ITER_PRINTK &&
1837 			trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1838 		return trace_print_bprintk_msg_only(iter);
1839 
1840 	if (iter->ent->type == TRACE_PRINT &&
1841 			trace_flags & TRACE_ITER_PRINTK &&
1842 			trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1843 		return trace_print_printk_msg_only(iter);
1844 
1845 	if (trace_flags & TRACE_ITER_BIN)
1846 		return print_bin_fmt(iter);
1847 
1848 	if (trace_flags & TRACE_ITER_HEX)
1849 		return print_hex_fmt(iter);
1850 
1851 	if (trace_flags & TRACE_ITER_RAW)
1852 		return print_raw_fmt(iter);
1853 
1854 	return print_trace_fmt(iter);
1855 }
1856 
1857 static int s_show(struct seq_file *m, void *v)
1858 {
1859 	struct trace_iterator *iter = v;
1860 
1861 	if (iter->ent == NULL) {
1862 		if (iter->tr) {
1863 			seq_printf(m, "# tracer: %s\n", iter->trace->name);
1864 			seq_puts(m, "#\n");
1865 		}
1866 		if (iter->trace && iter->trace->print_header)
1867 			iter->trace->print_header(m);
1868 		else if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
1869 			/* print nothing if the buffers are empty */
1870 			if (trace_empty(iter))
1871 				return 0;
1872 			print_trace_header(m, iter);
1873 			if (!(trace_flags & TRACE_ITER_VERBOSE))
1874 				print_lat_help_header(m);
1875 		} else {
1876 			if (!(trace_flags & TRACE_ITER_VERBOSE))
1877 				print_func_help_header(m);
1878 		}
1879 	} else {
1880 		print_trace_line(iter);
1881 		trace_print_seq(m, &iter->seq);
1882 	}
1883 
1884 	return 0;
1885 }
1886 
1887 static struct seq_operations tracer_seq_ops = {
1888 	.start		= s_start,
1889 	.next		= s_next,
1890 	.stop		= s_stop,
1891 	.show		= s_show,
1892 };
1893 
1894 static struct trace_iterator *
1895 __tracing_open(struct inode *inode, struct file *file)
1896 {
1897 	long cpu_file = (long) inode->i_private;
1898 	void *fail_ret = ERR_PTR(-ENOMEM);
1899 	struct trace_iterator *iter;
1900 	struct seq_file *m;
1901 	int cpu, ret;
1902 
1903 	if (tracing_disabled)
1904 		return ERR_PTR(-ENODEV);
1905 
1906 	iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1907 	if (!iter)
1908 		return ERR_PTR(-ENOMEM);
1909 
1910 	/*
1911 	 * We make a copy of the current tracer to avoid concurrent
1912 	 * changes on it while we are reading.
1913 	 */
1914 	mutex_lock(&trace_types_lock);
1915 	iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
1916 	if (!iter->trace)
1917 		goto fail;
1918 
1919 	if (current_trace)
1920 		*iter->trace = *current_trace;
1921 
1922 	if (!alloc_cpumask_var(&iter->started, GFP_KERNEL))
1923 		goto fail;
1924 
1925 	cpumask_clear(iter->started);
1926 
1927 	if (current_trace && current_trace->print_max)
1928 		iter->tr = &max_tr;
1929 	else
1930 		iter->tr = &global_trace;
1931 	iter->pos = -1;
1932 	mutex_init(&iter->mutex);
1933 	iter->cpu_file = cpu_file;
1934 
1935 	/* Notify the tracer early; before we stop tracing. */
1936 	if (iter->trace && iter->trace->open)
1937 		iter->trace->open(iter);
1938 
1939 	/* Annotate start of buffers if we had overruns */
1940 	if (ring_buffer_overruns(iter->tr->buffer))
1941 		iter->iter_flags |= TRACE_FILE_ANNOTATE;
1942 
1943 	if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
1944 		for_each_tracing_cpu(cpu) {
1945 
1946 			iter->buffer_iter[cpu] =
1947 				ring_buffer_read_start(iter->tr->buffer, cpu);
1948 		}
1949 	} else {
1950 		cpu = iter->cpu_file;
1951 		iter->buffer_iter[cpu] =
1952 				ring_buffer_read_start(iter->tr->buffer, cpu);
1953 	}
1954 
1955 	/* TODO stop tracer */
1956 	ret = seq_open(file, &tracer_seq_ops);
1957 	if (ret < 0) {
1958 		fail_ret = ERR_PTR(ret);
1959 		goto fail_buffer;
1960 	}
1961 
1962 	m = file->private_data;
1963 	m->private = iter;
1964 
1965 	/* stop the trace while dumping */
1966 	tracing_stop();
1967 
1968 	mutex_unlock(&trace_types_lock);
1969 
1970 	return iter;
1971 
1972  fail_buffer:
1973 	for_each_tracing_cpu(cpu) {
1974 		if (iter->buffer_iter[cpu])
1975 			ring_buffer_read_finish(iter->buffer_iter[cpu]);
1976 	}
1977 	free_cpumask_var(iter->started);
1978  fail:
1979 	mutex_unlock(&trace_types_lock);
1980 	kfree(iter->trace);
1981 	kfree(iter);
1982 
1983 	return fail_ret;
1984 }
1985 
1986 int tracing_open_generic(struct inode *inode, struct file *filp)
1987 {
1988 	if (tracing_disabled)
1989 		return -ENODEV;
1990 
1991 	filp->private_data = inode->i_private;
1992 	return 0;
1993 }
1994 
1995 static int tracing_release(struct inode *inode, struct file *file)
1996 {
1997 	struct seq_file *m = (struct seq_file *)file->private_data;
1998 	struct trace_iterator *iter;
1999 	int cpu;
2000 
2001 	if (!(file->f_mode & FMODE_READ))
2002 		return 0;
2003 
2004 	iter = m->private;
2005 
2006 	mutex_lock(&trace_types_lock);
2007 	for_each_tracing_cpu(cpu) {
2008 		if (iter->buffer_iter[cpu])
2009 			ring_buffer_read_finish(iter->buffer_iter[cpu]);
2010 	}
2011 
2012 	if (iter->trace && iter->trace->close)
2013 		iter->trace->close(iter);
2014 
2015 	/* reenable tracing if it was previously enabled */
2016 	tracing_start();
2017 	mutex_unlock(&trace_types_lock);
2018 
2019 	seq_release(inode, file);
2020 	mutex_destroy(&iter->mutex);
2021 	free_cpumask_var(iter->started);
2022 	kfree(iter->trace);
2023 	kfree(iter);
2024 	return 0;
2025 }
2026 
2027 static int tracing_open(struct inode *inode, struct file *file)
2028 {
2029 	struct trace_iterator *iter;
2030 	int ret = 0;
2031 
2032 	/* If this file was open for write, then erase contents */
2033 	if ((file->f_mode & FMODE_WRITE) &&
2034 	    !(file->f_flags & O_APPEND)) {
2035 		long cpu = (long) inode->i_private;
2036 
2037 		if (cpu == TRACE_PIPE_ALL_CPU)
2038 			tracing_reset_online_cpus(&global_trace);
2039 		else
2040 			tracing_reset(&global_trace, cpu);
2041 	}
2042 
2043 	if (file->f_mode & FMODE_READ) {
2044 		iter = __tracing_open(inode, file);
2045 		if (IS_ERR(iter))
2046 			ret = PTR_ERR(iter);
2047 		else if (trace_flags & TRACE_ITER_LATENCY_FMT)
2048 			iter->iter_flags |= TRACE_FILE_LAT_FMT;
2049 	}
2050 	return ret;
2051 }
2052 
2053 static void *
2054 t_next(struct seq_file *m, void *v, loff_t *pos)
2055 {
2056 	struct tracer *t = m->private;
2057 
2058 	(*pos)++;
2059 
2060 	if (t)
2061 		t = t->next;
2062 
2063 	m->private = t;
2064 
2065 	return t;
2066 }
2067 
2068 static void *t_start(struct seq_file *m, loff_t *pos)
2069 {
2070 	struct tracer *t = m->private;
2071 	loff_t l = 0;
2072 
2073 	mutex_lock(&trace_types_lock);
2074 	for (; t && l < *pos; t = t_next(m, t, &l))
2075 		;
2076 
2077 	return t;
2078 }
2079 
2080 static void t_stop(struct seq_file *m, void *p)
2081 {
2082 	mutex_unlock(&trace_types_lock);
2083 }
2084 
2085 static int t_show(struct seq_file *m, void *v)
2086 {
2087 	struct tracer *t = v;
2088 
2089 	if (!t)
2090 		return 0;
2091 
2092 	seq_printf(m, "%s", t->name);
2093 	if (t->next)
2094 		seq_putc(m, ' ');
2095 	else
2096 		seq_putc(m, '\n');
2097 
2098 	return 0;
2099 }
2100 
2101 static struct seq_operations show_traces_seq_ops = {
2102 	.start		= t_start,
2103 	.next		= t_next,
2104 	.stop		= t_stop,
2105 	.show		= t_show,
2106 };
2107 
2108 static int show_traces_open(struct inode *inode, struct file *file)
2109 {
2110 	int ret;
2111 
2112 	if (tracing_disabled)
2113 		return -ENODEV;
2114 
2115 	ret = seq_open(file, &show_traces_seq_ops);
2116 	if (!ret) {
2117 		struct seq_file *m = file->private_data;
2118 		m->private = trace_types;
2119 	}
2120 
2121 	return ret;
2122 }
2123 
2124 static ssize_t
2125 tracing_write_stub(struct file *filp, const char __user *ubuf,
2126 		   size_t count, loff_t *ppos)
2127 {
2128 	return count;
2129 }
2130 
2131 static const struct file_operations tracing_fops = {
2132 	.open		= tracing_open,
2133 	.read		= seq_read,
2134 	.write		= tracing_write_stub,
2135 	.llseek		= seq_lseek,
2136 	.release	= tracing_release,
2137 };
2138 
2139 static const struct file_operations show_traces_fops = {
2140 	.open		= show_traces_open,
2141 	.read		= seq_read,
2142 	.release	= seq_release,
2143 };
2144 
2145 /*
2146  * Only trace on a CPU if the bitmask is set:
2147  */
2148 static cpumask_var_t tracing_cpumask;
2149 
2150 /*
2151  * The tracer itself will not take this lock, but still we want
2152  * to provide a consistent cpumask to user-space:
2153  */
2154 static DEFINE_MUTEX(tracing_cpumask_update_lock);
2155 
2156 /*
2157  * Temporary storage for the character representation of the
2158  * CPU bitmask (and one more byte for the newline):
2159  */
2160 static char mask_str[NR_CPUS + 1];
2161 
2162 static ssize_t
2163 tracing_cpumask_read(struct file *filp, char __user *ubuf,
2164 		     size_t count, loff_t *ppos)
2165 {
2166 	int len;
2167 
2168 	mutex_lock(&tracing_cpumask_update_lock);
2169 
2170 	len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
2171 	if (count - len < 2) {
2172 		count = -EINVAL;
2173 		goto out_err;
2174 	}
2175 	len += sprintf(mask_str + len, "\n");
2176 	count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
2177 
2178 out_err:
2179 	mutex_unlock(&tracing_cpumask_update_lock);
2180 
2181 	return count;
2182 }
2183 
2184 static ssize_t
2185 tracing_cpumask_write(struct file *filp, const char __user *ubuf,
2186 		      size_t count, loff_t *ppos)
2187 {
2188 	int err, cpu;
2189 	cpumask_var_t tracing_cpumask_new;
2190 
2191 	if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
2192 		return -ENOMEM;
2193 
2194 	err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
2195 	if (err)
2196 		goto err_unlock;
2197 
2198 	mutex_lock(&tracing_cpumask_update_lock);
2199 
2200 	local_irq_disable();
2201 	__raw_spin_lock(&ftrace_max_lock);
2202 	for_each_tracing_cpu(cpu) {
2203 		/*
2204 		 * Increase/decrease the disabled counter if we are
2205 		 * about to flip a bit in the cpumask:
2206 		 */
2207 		if (cpumask_test_cpu(cpu, tracing_cpumask) &&
2208 				!cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2209 			atomic_inc(&global_trace.data[cpu]->disabled);
2210 		}
2211 		if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
2212 				cpumask_test_cpu(cpu, tracing_cpumask_new)) {
2213 			atomic_dec(&global_trace.data[cpu]->disabled);
2214 		}
2215 	}
2216 	__raw_spin_unlock(&ftrace_max_lock);
2217 	local_irq_enable();
2218 
2219 	cpumask_copy(tracing_cpumask, tracing_cpumask_new);
2220 
2221 	mutex_unlock(&tracing_cpumask_update_lock);
2222 	free_cpumask_var(tracing_cpumask_new);
2223 
2224 	return count;
2225 
2226 err_unlock:
2227 	free_cpumask_var(tracing_cpumask_new);
2228 
2229 	return err;
2230 }
2231 
2232 static const struct file_operations tracing_cpumask_fops = {
2233 	.open		= tracing_open_generic,
2234 	.read		= tracing_cpumask_read,
2235 	.write		= tracing_cpumask_write,
2236 };
2237 
2238 static ssize_t
2239 tracing_trace_options_read(struct file *filp, char __user *ubuf,
2240 		       size_t cnt, loff_t *ppos)
2241 {
2242 	struct tracer_opt *trace_opts;
2243 	u32 tracer_flags;
2244 	int len = 0;
2245 	char *buf;
2246 	int r = 0;
2247 	int i;
2248 
2249 
2250 	/* calculate max size */
2251 	for (i = 0; trace_options[i]; i++) {
2252 		len += strlen(trace_options[i]);
2253 		len += 3; /* "no" and newline */
2254 	}
2255 
2256 	mutex_lock(&trace_types_lock);
2257 	tracer_flags = current_trace->flags->val;
2258 	trace_opts = current_trace->flags->opts;
2259 
2260 	/*
2261 	 * Increase the size with names of options specific
2262 	 * of the current tracer.
2263 	 */
2264 	for (i = 0; trace_opts[i].name; i++) {
2265 		len += strlen(trace_opts[i].name);
2266 		len += 3; /* "no" and newline */
2267 	}
2268 
2269 	/* +2 for \n and \0 */
2270 	buf = kmalloc(len + 2, GFP_KERNEL);
2271 	if (!buf) {
2272 		mutex_unlock(&trace_types_lock);
2273 		return -ENOMEM;
2274 	}
2275 
2276 	for (i = 0; trace_options[i]; i++) {
2277 		if (trace_flags & (1 << i))
2278 			r += sprintf(buf + r, "%s\n", trace_options[i]);
2279 		else
2280 			r += sprintf(buf + r, "no%s\n", trace_options[i]);
2281 	}
2282 
2283 	for (i = 0; trace_opts[i].name; i++) {
2284 		if (tracer_flags & trace_opts[i].bit)
2285 			r += sprintf(buf + r, "%s\n",
2286 				trace_opts[i].name);
2287 		else
2288 			r += sprintf(buf + r, "no%s\n",
2289 				trace_opts[i].name);
2290 	}
2291 	mutex_unlock(&trace_types_lock);
2292 
2293 	WARN_ON(r >= len + 2);
2294 
2295 	r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2296 
2297 	kfree(buf);
2298 	return r;
2299 }
2300 
2301 /* Try to assign a tracer specific option */
2302 static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
2303 {
2304 	struct tracer_flags *trace_flags = trace->flags;
2305 	struct tracer_opt *opts = NULL;
2306 	int ret = 0, i = 0;
2307 	int len;
2308 
2309 	for (i = 0; trace_flags->opts[i].name; i++) {
2310 		opts = &trace_flags->opts[i];
2311 		len = strlen(opts->name);
2312 
2313 		if (strncmp(cmp, opts->name, len) == 0) {
2314 			ret = trace->set_flag(trace_flags->val,
2315 				opts->bit, !neg);
2316 			break;
2317 		}
2318 	}
2319 	/* Not found */
2320 	if (!trace_flags->opts[i].name)
2321 		return -EINVAL;
2322 
2323 	/* Refused to handle */
2324 	if (ret)
2325 		return ret;
2326 
2327 	if (neg)
2328 		trace_flags->val &= ~opts->bit;
2329 	else
2330 		trace_flags->val |= opts->bit;
2331 
2332 	return 0;
2333 }
2334 
2335 static void set_tracer_flags(unsigned int mask, int enabled)
2336 {
2337 	/* do nothing if flag is already set */
2338 	if (!!(trace_flags & mask) == !!enabled)
2339 		return;
2340 
2341 	if (enabled)
2342 		trace_flags |= mask;
2343 	else
2344 		trace_flags &= ~mask;
2345 
2346 	if (mask == TRACE_ITER_GLOBAL_CLK) {
2347 		u64 (*func)(void);
2348 
2349 		if (enabled)
2350 			func = trace_clock_global;
2351 		else
2352 			func = trace_clock_local;
2353 
2354 		mutex_lock(&trace_types_lock);
2355 		ring_buffer_set_clock(global_trace.buffer, func);
2356 
2357 		if (max_tr.buffer)
2358 			ring_buffer_set_clock(max_tr.buffer, func);
2359 		mutex_unlock(&trace_types_lock);
2360 	}
2361 }
2362 
2363 static ssize_t
2364 tracing_trace_options_write(struct file *filp, const char __user *ubuf,
2365 			size_t cnt, loff_t *ppos)
2366 {
2367 	char buf[64];
2368 	char *cmp = buf;
2369 	int neg = 0;
2370 	int ret;
2371 	int i;
2372 
2373 	if (cnt >= sizeof(buf))
2374 		return -EINVAL;
2375 
2376 	if (copy_from_user(&buf, ubuf, cnt))
2377 		return -EFAULT;
2378 
2379 	buf[cnt] = 0;
2380 
2381 	if (strncmp(buf, "no", 2) == 0) {
2382 		neg = 1;
2383 		cmp += 2;
2384 	}
2385 
2386 	for (i = 0; trace_options[i]; i++) {
2387 		int len = strlen(trace_options[i]);
2388 
2389 		if (strncmp(cmp, trace_options[i], len) == 0) {
2390 			set_tracer_flags(1 << i, !neg);
2391 			break;
2392 		}
2393 	}
2394 
2395 	/* If no option could be set, test the specific tracer options */
2396 	if (!trace_options[i]) {
2397 		mutex_lock(&trace_types_lock);
2398 		ret = set_tracer_option(current_trace, cmp, neg);
2399 		mutex_unlock(&trace_types_lock);
2400 		if (ret)
2401 			return ret;
2402 	}
2403 
2404 	filp->f_pos += cnt;
2405 
2406 	return cnt;
2407 }
2408 
2409 static const struct file_operations tracing_iter_fops = {
2410 	.open		= tracing_open_generic,
2411 	.read		= tracing_trace_options_read,
2412 	.write		= tracing_trace_options_write,
2413 };
2414 
2415 static const char readme_msg[] =
2416 	"tracing mini-HOWTO:\n\n"
2417 	"# mount -t debugfs nodev /sys/kernel/debug\n\n"
2418 	"# cat /sys/kernel/debug/tracing/available_tracers\n"
2419 	"wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
2420 	"# cat /sys/kernel/debug/tracing/current_tracer\n"
2421 	"nop\n"
2422 	"# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
2423 	"# cat /sys/kernel/debug/tracing/current_tracer\n"
2424 	"sched_switch\n"
2425 	"# cat /sys/kernel/debug/tracing/trace_options\n"
2426 	"noprint-parent nosym-offset nosym-addr noverbose\n"
2427 	"# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
2428 	"# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
2429 	"# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
2430 	"# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
2431 ;
2432 
2433 static ssize_t
2434 tracing_readme_read(struct file *filp, char __user *ubuf,
2435 		       size_t cnt, loff_t *ppos)
2436 {
2437 	return simple_read_from_buffer(ubuf, cnt, ppos,
2438 					readme_msg, strlen(readme_msg));
2439 }
2440 
2441 static const struct file_operations tracing_readme_fops = {
2442 	.open		= tracing_open_generic,
2443 	.read		= tracing_readme_read,
2444 };
2445 
2446 static ssize_t
2447 tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
2448 				size_t cnt, loff_t *ppos)
2449 {
2450 	char *buf_comm;
2451 	char *file_buf;
2452 	char *buf;
2453 	int len = 0;
2454 	int pid;
2455 	int i;
2456 
2457 	file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
2458 	if (!file_buf)
2459 		return -ENOMEM;
2460 
2461 	buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
2462 	if (!buf_comm) {
2463 		kfree(file_buf);
2464 		return -ENOMEM;
2465 	}
2466 
2467 	buf = file_buf;
2468 
2469 	for (i = 0; i < SAVED_CMDLINES; i++) {
2470 		int r;
2471 
2472 		pid = map_cmdline_to_pid[i];
2473 		if (pid == -1 || pid == NO_CMDLINE_MAP)
2474 			continue;
2475 
2476 		trace_find_cmdline(pid, buf_comm);
2477 		r = sprintf(buf, "%d %s\n", pid, buf_comm);
2478 		buf += r;
2479 		len += r;
2480 	}
2481 
2482 	len = simple_read_from_buffer(ubuf, cnt, ppos,
2483 				      file_buf, len);
2484 
2485 	kfree(file_buf);
2486 	kfree(buf_comm);
2487 
2488 	return len;
2489 }
2490 
2491 static const struct file_operations tracing_saved_cmdlines_fops = {
2492     .open       = tracing_open_generic,
2493     .read       = tracing_saved_cmdlines_read,
2494 };
2495 
2496 static ssize_t
2497 tracing_ctrl_read(struct file *filp, char __user *ubuf,
2498 		  size_t cnt, loff_t *ppos)
2499 {
2500 	char buf[64];
2501 	int r;
2502 
2503 	r = sprintf(buf, "%u\n", tracer_enabled);
2504 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2505 }
2506 
2507 static ssize_t
2508 tracing_ctrl_write(struct file *filp, const char __user *ubuf,
2509 		   size_t cnt, loff_t *ppos)
2510 {
2511 	struct trace_array *tr = filp->private_data;
2512 	char buf[64];
2513 	unsigned long val;
2514 	int ret;
2515 
2516 	if (cnt >= sizeof(buf))
2517 		return -EINVAL;
2518 
2519 	if (copy_from_user(&buf, ubuf, cnt))
2520 		return -EFAULT;
2521 
2522 	buf[cnt] = 0;
2523 
2524 	ret = strict_strtoul(buf, 10, &val);
2525 	if (ret < 0)
2526 		return ret;
2527 
2528 	val = !!val;
2529 
2530 	mutex_lock(&trace_types_lock);
2531 	if (tracer_enabled ^ val) {
2532 		if (val) {
2533 			tracer_enabled = 1;
2534 			if (current_trace->start)
2535 				current_trace->start(tr);
2536 			tracing_start();
2537 		} else {
2538 			tracer_enabled = 0;
2539 			tracing_stop();
2540 			if (current_trace->stop)
2541 				current_trace->stop(tr);
2542 		}
2543 	}
2544 	mutex_unlock(&trace_types_lock);
2545 
2546 	filp->f_pos += cnt;
2547 
2548 	return cnt;
2549 }
2550 
2551 static ssize_t
2552 tracing_set_trace_read(struct file *filp, char __user *ubuf,
2553 		       size_t cnt, loff_t *ppos)
2554 {
2555 	char buf[max_tracer_type_len+2];
2556 	int r;
2557 
2558 	mutex_lock(&trace_types_lock);
2559 	if (current_trace)
2560 		r = sprintf(buf, "%s\n", current_trace->name);
2561 	else
2562 		r = sprintf(buf, "\n");
2563 	mutex_unlock(&trace_types_lock);
2564 
2565 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2566 }
2567 
2568 int tracer_init(struct tracer *t, struct trace_array *tr)
2569 {
2570 	tracing_reset_online_cpus(tr);
2571 	return t->init(tr);
2572 }
2573 
2574 static int tracing_resize_ring_buffer(unsigned long size)
2575 {
2576 	int ret;
2577 
2578 	/*
2579 	 * If kernel or user changes the size of the ring buffer
2580 	 * we use the size that was given, and we can forget about
2581 	 * expanding it later.
2582 	 */
2583 	ring_buffer_expanded = 1;
2584 
2585 	ret = ring_buffer_resize(global_trace.buffer, size);
2586 	if (ret < 0)
2587 		return ret;
2588 
2589 	ret = ring_buffer_resize(max_tr.buffer, size);
2590 	if (ret < 0) {
2591 		int r;
2592 
2593 		r = ring_buffer_resize(global_trace.buffer,
2594 				       global_trace.entries);
2595 		if (r < 0) {
2596 			/*
2597 			 * AARGH! We are left with different
2598 			 * size max buffer!!!!
2599 			 * The max buffer is our "snapshot" buffer.
2600 			 * When a tracer needs a snapshot (one of the
2601 			 * latency tracers), it swaps the max buffer
2602 			 * with the saved snap shot. We succeeded to
2603 			 * update the size of the main buffer, but failed to
2604 			 * update the size of the max buffer. But when we tried
2605 			 * to reset the main buffer to the original size, we
2606 			 * failed there too. This is very unlikely to
2607 			 * happen, but if it does, warn and kill all
2608 			 * tracing.
2609 			 */
2610 			WARN_ON(1);
2611 			tracing_disabled = 1;
2612 		}
2613 		return ret;
2614 	}
2615 
2616 	global_trace.entries = size;
2617 
2618 	return ret;
2619 }
2620 
2621 /**
2622  * tracing_update_buffers - used by tracing facility to expand ring buffers
2623  *
2624  * To save on memory when the tracing is never used on a system with it
2625  * configured in. The ring buffers are set to a minimum size. But once
2626  * a user starts to use the tracing facility, then they need to grow
2627  * to their default size.
2628  *
2629  * This function is to be called when a tracer is about to be used.
2630  */
2631 int tracing_update_buffers(void)
2632 {
2633 	int ret = 0;
2634 
2635 	mutex_lock(&trace_types_lock);
2636 	if (!ring_buffer_expanded)
2637 		ret = tracing_resize_ring_buffer(trace_buf_size);
2638 	mutex_unlock(&trace_types_lock);
2639 
2640 	return ret;
2641 }
2642 
2643 struct trace_option_dentry;
2644 
2645 static struct trace_option_dentry *
2646 create_trace_option_files(struct tracer *tracer);
2647 
2648 static void
2649 destroy_trace_option_files(struct trace_option_dentry *topts);
2650 
2651 static int tracing_set_tracer(const char *buf)
2652 {
2653 	static struct trace_option_dentry *topts;
2654 	struct trace_array *tr = &global_trace;
2655 	struct tracer *t;
2656 	int ret = 0;
2657 
2658 	mutex_lock(&trace_types_lock);
2659 
2660 	if (!ring_buffer_expanded) {
2661 		ret = tracing_resize_ring_buffer(trace_buf_size);
2662 		if (ret < 0)
2663 			goto out;
2664 		ret = 0;
2665 	}
2666 
2667 	for (t = trace_types; t; t = t->next) {
2668 		if (strcmp(t->name, buf) == 0)
2669 			break;
2670 	}
2671 	if (!t) {
2672 		ret = -EINVAL;
2673 		goto out;
2674 	}
2675 	if (t == current_trace)
2676 		goto out;
2677 
2678 	trace_branch_disable();
2679 	if (current_trace && current_trace->reset)
2680 		current_trace->reset(tr);
2681 
2682 	destroy_trace_option_files(topts);
2683 
2684 	current_trace = t;
2685 
2686 	topts = create_trace_option_files(current_trace);
2687 
2688 	if (t->init) {
2689 		ret = tracer_init(t, tr);
2690 		if (ret)
2691 			goto out;
2692 	}
2693 
2694 	trace_branch_enable(tr);
2695  out:
2696 	mutex_unlock(&trace_types_lock);
2697 
2698 	return ret;
2699 }
2700 
2701 static ssize_t
2702 tracing_set_trace_write(struct file *filp, const char __user *ubuf,
2703 			size_t cnt, loff_t *ppos)
2704 {
2705 	char buf[max_tracer_type_len+1];
2706 	int i;
2707 	size_t ret;
2708 	int err;
2709 
2710 	ret = cnt;
2711 
2712 	if (cnt > max_tracer_type_len)
2713 		cnt = max_tracer_type_len;
2714 
2715 	if (copy_from_user(&buf, ubuf, cnt))
2716 		return -EFAULT;
2717 
2718 	buf[cnt] = 0;
2719 
2720 	/* strip ending whitespace. */
2721 	for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
2722 		buf[i] = 0;
2723 
2724 	err = tracing_set_tracer(buf);
2725 	if (err)
2726 		return err;
2727 
2728 	filp->f_pos += ret;
2729 
2730 	return ret;
2731 }
2732 
2733 static ssize_t
2734 tracing_max_lat_read(struct file *filp, char __user *ubuf,
2735 		     size_t cnt, loff_t *ppos)
2736 {
2737 	unsigned long *ptr = filp->private_data;
2738 	char buf[64];
2739 	int r;
2740 
2741 	r = snprintf(buf, sizeof(buf), "%ld\n",
2742 		     *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
2743 	if (r > sizeof(buf))
2744 		r = sizeof(buf);
2745 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2746 }
2747 
2748 static ssize_t
2749 tracing_max_lat_write(struct file *filp, const char __user *ubuf,
2750 		      size_t cnt, loff_t *ppos)
2751 {
2752 	unsigned long *ptr = filp->private_data;
2753 	char buf[64];
2754 	unsigned long val;
2755 	int ret;
2756 
2757 	if (cnt >= sizeof(buf))
2758 		return -EINVAL;
2759 
2760 	if (copy_from_user(&buf, ubuf, cnt))
2761 		return -EFAULT;
2762 
2763 	buf[cnt] = 0;
2764 
2765 	ret = strict_strtoul(buf, 10, &val);
2766 	if (ret < 0)
2767 		return ret;
2768 
2769 	*ptr = val * 1000;
2770 
2771 	return cnt;
2772 }
2773 
2774 static int tracing_open_pipe(struct inode *inode, struct file *filp)
2775 {
2776 	long cpu_file = (long) inode->i_private;
2777 	struct trace_iterator *iter;
2778 	int ret = 0;
2779 
2780 	if (tracing_disabled)
2781 		return -ENODEV;
2782 
2783 	mutex_lock(&trace_types_lock);
2784 
2785 	/* We only allow one reader per cpu */
2786 	if (cpu_file == TRACE_PIPE_ALL_CPU) {
2787 		if (!cpumask_empty(tracing_reader_cpumask)) {
2788 			ret = -EBUSY;
2789 			goto out;
2790 		}
2791 		cpumask_setall(tracing_reader_cpumask);
2792 	} else {
2793 		if (!cpumask_test_cpu(cpu_file, tracing_reader_cpumask))
2794 			cpumask_set_cpu(cpu_file, tracing_reader_cpumask);
2795 		else {
2796 			ret = -EBUSY;
2797 			goto out;
2798 		}
2799 	}
2800 
2801 	/* create a buffer to store the information to pass to userspace */
2802 	iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2803 	if (!iter) {
2804 		ret = -ENOMEM;
2805 		goto out;
2806 	}
2807 
2808 	/*
2809 	 * We make a copy of the current tracer to avoid concurrent
2810 	 * changes on it while we are reading.
2811 	 */
2812 	iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
2813 	if (!iter->trace) {
2814 		ret = -ENOMEM;
2815 		goto fail;
2816 	}
2817 	if (current_trace)
2818 		*iter->trace = *current_trace;
2819 
2820 	if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
2821 		ret = -ENOMEM;
2822 		goto fail;
2823 	}
2824 
2825 	/* trace pipe does not show start of buffer */
2826 	cpumask_setall(iter->started);
2827 
2828 	if (trace_flags & TRACE_ITER_LATENCY_FMT)
2829 		iter->iter_flags |= TRACE_FILE_LAT_FMT;
2830 
2831 	iter->cpu_file = cpu_file;
2832 	iter->tr = &global_trace;
2833 	mutex_init(&iter->mutex);
2834 	filp->private_data = iter;
2835 
2836 	if (iter->trace->pipe_open)
2837 		iter->trace->pipe_open(iter);
2838 
2839 out:
2840 	mutex_unlock(&trace_types_lock);
2841 	return ret;
2842 
2843 fail:
2844 	kfree(iter->trace);
2845 	kfree(iter);
2846 	mutex_unlock(&trace_types_lock);
2847 	return ret;
2848 }
2849 
2850 static int tracing_release_pipe(struct inode *inode, struct file *file)
2851 {
2852 	struct trace_iterator *iter = file->private_data;
2853 
2854 	mutex_lock(&trace_types_lock);
2855 
2856 	if (iter->cpu_file == TRACE_PIPE_ALL_CPU)
2857 		cpumask_clear(tracing_reader_cpumask);
2858 	else
2859 		cpumask_clear_cpu(iter->cpu_file, tracing_reader_cpumask);
2860 
2861 	mutex_unlock(&trace_types_lock);
2862 
2863 	free_cpumask_var(iter->started);
2864 	mutex_destroy(&iter->mutex);
2865 	kfree(iter->trace);
2866 	kfree(iter);
2867 
2868 	return 0;
2869 }
2870 
2871 static unsigned int
2872 tracing_poll_pipe(struct file *filp, poll_table *poll_table)
2873 {
2874 	struct trace_iterator *iter = filp->private_data;
2875 
2876 	if (trace_flags & TRACE_ITER_BLOCK) {
2877 		/*
2878 		 * Always select as readable when in blocking mode
2879 		 */
2880 		return POLLIN | POLLRDNORM;
2881 	} else {
2882 		if (!trace_empty(iter))
2883 			return POLLIN | POLLRDNORM;
2884 		poll_wait(filp, &trace_wait, poll_table);
2885 		if (!trace_empty(iter))
2886 			return POLLIN | POLLRDNORM;
2887 
2888 		return 0;
2889 	}
2890 }
2891 
2892 
2893 void default_wait_pipe(struct trace_iterator *iter)
2894 {
2895 	DEFINE_WAIT(wait);
2896 
2897 	prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
2898 
2899 	if (trace_empty(iter))
2900 		schedule();
2901 
2902 	finish_wait(&trace_wait, &wait);
2903 }
2904 
2905 /*
2906  * This is a make-shift waitqueue.
2907  * A tracer might use this callback on some rare cases:
2908  *
2909  *  1) the current tracer might hold the runqueue lock when it wakes up
2910  *     a reader, hence a deadlock (sched, function, and function graph tracers)
2911  *  2) the function tracers, trace all functions, we don't want
2912  *     the overhead of calling wake_up and friends
2913  *     (and tracing them too)
2914  *
2915  *     Anyway, this is really very primitive wakeup.
2916  */
2917 void poll_wait_pipe(struct trace_iterator *iter)
2918 {
2919 	set_current_state(TASK_INTERRUPTIBLE);
2920 	/* sleep for 100 msecs, and try again. */
2921 	schedule_timeout(HZ / 10);
2922 }
2923 
2924 /* Must be called with trace_types_lock mutex held. */
2925 static int tracing_wait_pipe(struct file *filp)
2926 {
2927 	struct trace_iterator *iter = filp->private_data;
2928 
2929 	while (trace_empty(iter)) {
2930 
2931 		if ((filp->f_flags & O_NONBLOCK)) {
2932 			return -EAGAIN;
2933 		}
2934 
2935 		mutex_unlock(&iter->mutex);
2936 
2937 		iter->trace->wait_pipe(iter);
2938 
2939 		mutex_lock(&iter->mutex);
2940 
2941 		if (signal_pending(current))
2942 			return -EINTR;
2943 
2944 		/*
2945 		 * We block until we read something and tracing is disabled.
2946 		 * We still block if tracing is disabled, but we have never
2947 		 * read anything. This allows a user to cat this file, and
2948 		 * then enable tracing. But after we have read something,
2949 		 * we give an EOF when tracing is again disabled.
2950 		 *
2951 		 * iter->pos will be 0 if we haven't read anything.
2952 		 */
2953 		if (!tracer_enabled && iter->pos)
2954 			break;
2955 	}
2956 
2957 	return 1;
2958 }
2959 
2960 /*
2961  * Consumer reader.
2962  */
2963 static ssize_t
2964 tracing_read_pipe(struct file *filp, char __user *ubuf,
2965 		  size_t cnt, loff_t *ppos)
2966 {
2967 	struct trace_iterator *iter = filp->private_data;
2968 	static struct tracer *old_tracer;
2969 	ssize_t sret;
2970 
2971 	/* return any leftover data */
2972 	sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
2973 	if (sret != -EBUSY)
2974 		return sret;
2975 
2976 	trace_seq_init(&iter->seq);
2977 
2978 	/* copy the tracer to avoid using a global lock all around */
2979 	mutex_lock(&trace_types_lock);
2980 	if (unlikely(old_tracer != current_trace && current_trace)) {
2981 		old_tracer = current_trace;
2982 		*iter->trace = *current_trace;
2983 	}
2984 	mutex_unlock(&trace_types_lock);
2985 
2986 	/*
2987 	 * Avoid more than one consumer on a single file descriptor
2988 	 * This is just a matter of traces coherency, the ring buffer itself
2989 	 * is protected.
2990 	 */
2991 	mutex_lock(&iter->mutex);
2992 	if (iter->trace->read) {
2993 		sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
2994 		if (sret)
2995 			goto out;
2996 	}
2997 
2998 waitagain:
2999 	sret = tracing_wait_pipe(filp);
3000 	if (sret <= 0)
3001 		goto out;
3002 
3003 	/* stop when tracing is finished */
3004 	if (trace_empty(iter)) {
3005 		sret = 0;
3006 		goto out;
3007 	}
3008 
3009 	if (cnt >= PAGE_SIZE)
3010 		cnt = PAGE_SIZE - 1;
3011 
3012 	/* reset all but tr, trace, and overruns */
3013 	memset(&iter->seq, 0,
3014 	       sizeof(struct trace_iterator) -
3015 	       offsetof(struct trace_iterator, seq));
3016 	iter->pos = -1;
3017 
3018 	trace_event_read_lock();
3019 	while (find_next_entry_inc(iter) != NULL) {
3020 		enum print_line_t ret;
3021 		int len = iter->seq.len;
3022 
3023 		ret = print_trace_line(iter);
3024 		if (ret == TRACE_TYPE_PARTIAL_LINE) {
3025 			/* don't print partial lines */
3026 			iter->seq.len = len;
3027 			break;
3028 		}
3029 		if (ret != TRACE_TYPE_NO_CONSUME)
3030 			trace_consume(iter);
3031 
3032 		if (iter->seq.len >= cnt)
3033 			break;
3034 	}
3035 	trace_event_read_unlock();
3036 
3037 	/* Now copy what we have to the user */
3038 	sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
3039 	if (iter->seq.readpos >= iter->seq.len)
3040 		trace_seq_init(&iter->seq);
3041 
3042 	/*
3043 	 * If there was nothing to send to user, inspite of consuming trace
3044 	 * entries, go back to wait for more entries.
3045 	 */
3046 	if (sret == -EBUSY)
3047 		goto waitagain;
3048 
3049 out:
3050 	mutex_unlock(&iter->mutex);
3051 
3052 	return sret;
3053 }
3054 
3055 static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
3056 				     struct pipe_buffer *buf)
3057 {
3058 	__free_page(buf->page);
3059 }
3060 
3061 static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
3062 				     unsigned int idx)
3063 {
3064 	__free_page(spd->pages[idx]);
3065 }
3066 
3067 static struct pipe_buf_operations tracing_pipe_buf_ops = {
3068 	.can_merge		= 0,
3069 	.map			= generic_pipe_buf_map,
3070 	.unmap			= generic_pipe_buf_unmap,
3071 	.confirm		= generic_pipe_buf_confirm,
3072 	.release		= tracing_pipe_buf_release,
3073 	.steal			= generic_pipe_buf_steal,
3074 	.get			= generic_pipe_buf_get,
3075 };
3076 
3077 static size_t
3078 tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
3079 {
3080 	size_t count;
3081 	int ret;
3082 
3083 	/* Seq buffer is page-sized, exactly what we need. */
3084 	for (;;) {
3085 		count = iter->seq.len;
3086 		ret = print_trace_line(iter);
3087 		count = iter->seq.len - count;
3088 		if (rem < count) {
3089 			rem = 0;
3090 			iter->seq.len -= count;
3091 			break;
3092 		}
3093 		if (ret == TRACE_TYPE_PARTIAL_LINE) {
3094 			iter->seq.len -= count;
3095 			break;
3096 		}
3097 
3098 		trace_consume(iter);
3099 		rem -= count;
3100 		if (!find_next_entry_inc(iter))	{
3101 			rem = 0;
3102 			iter->ent = NULL;
3103 			break;
3104 		}
3105 	}
3106 
3107 	return rem;
3108 }
3109 
3110 static ssize_t tracing_splice_read_pipe(struct file *filp,
3111 					loff_t *ppos,
3112 					struct pipe_inode_info *pipe,
3113 					size_t len,
3114 					unsigned int flags)
3115 {
3116 	struct page *pages[PIPE_BUFFERS];
3117 	struct partial_page partial[PIPE_BUFFERS];
3118 	struct trace_iterator *iter = filp->private_data;
3119 	struct splice_pipe_desc spd = {
3120 		.pages		= pages,
3121 		.partial	= partial,
3122 		.nr_pages	= 0, /* This gets updated below. */
3123 		.flags		= flags,
3124 		.ops		= &tracing_pipe_buf_ops,
3125 		.spd_release	= tracing_spd_release_pipe,
3126 	};
3127 	static struct tracer *old_tracer;
3128 	ssize_t ret;
3129 	size_t rem;
3130 	unsigned int i;
3131 
3132 	/* copy the tracer to avoid using a global lock all around */
3133 	mutex_lock(&trace_types_lock);
3134 	if (unlikely(old_tracer != current_trace && current_trace)) {
3135 		old_tracer = current_trace;
3136 		*iter->trace = *current_trace;
3137 	}
3138 	mutex_unlock(&trace_types_lock);
3139 
3140 	mutex_lock(&iter->mutex);
3141 
3142 	if (iter->trace->splice_read) {
3143 		ret = iter->trace->splice_read(iter, filp,
3144 					       ppos, pipe, len, flags);
3145 		if (ret)
3146 			goto out_err;
3147 	}
3148 
3149 	ret = tracing_wait_pipe(filp);
3150 	if (ret <= 0)
3151 		goto out_err;
3152 
3153 	if (!iter->ent && !find_next_entry_inc(iter)) {
3154 		ret = -EFAULT;
3155 		goto out_err;
3156 	}
3157 
3158 	trace_event_read_lock();
3159 
3160 	/* Fill as many pages as possible. */
3161 	for (i = 0, rem = len; i < PIPE_BUFFERS && rem; i++) {
3162 		pages[i] = alloc_page(GFP_KERNEL);
3163 		if (!pages[i])
3164 			break;
3165 
3166 		rem = tracing_fill_pipe_page(rem, iter);
3167 
3168 		/* Copy the data into the page, so we can start over. */
3169 		ret = trace_seq_to_buffer(&iter->seq,
3170 					  page_address(pages[i]),
3171 					  iter->seq.len);
3172 		if (ret < 0) {
3173 			__free_page(pages[i]);
3174 			break;
3175 		}
3176 		partial[i].offset = 0;
3177 		partial[i].len = iter->seq.len;
3178 
3179 		trace_seq_init(&iter->seq);
3180 	}
3181 
3182 	trace_event_read_unlock();
3183 	mutex_unlock(&iter->mutex);
3184 
3185 	spd.nr_pages = i;
3186 
3187 	return splice_to_pipe(pipe, &spd);
3188 
3189 out_err:
3190 	mutex_unlock(&iter->mutex);
3191 
3192 	return ret;
3193 }
3194 
3195 static ssize_t
3196 tracing_entries_read(struct file *filp, char __user *ubuf,
3197 		     size_t cnt, loff_t *ppos)
3198 {
3199 	struct trace_array *tr = filp->private_data;
3200 	char buf[96];
3201 	int r;
3202 
3203 	mutex_lock(&trace_types_lock);
3204 	if (!ring_buffer_expanded)
3205 		r = sprintf(buf, "%lu (expanded: %lu)\n",
3206 			    tr->entries >> 10,
3207 			    trace_buf_size >> 10);
3208 	else
3209 		r = sprintf(buf, "%lu\n", tr->entries >> 10);
3210 	mutex_unlock(&trace_types_lock);
3211 
3212 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3213 }
3214 
3215 static ssize_t
3216 tracing_entries_write(struct file *filp, const char __user *ubuf,
3217 		      size_t cnt, loff_t *ppos)
3218 {
3219 	unsigned long val;
3220 	char buf[64];
3221 	int ret, cpu;
3222 
3223 	if (cnt >= sizeof(buf))
3224 		return -EINVAL;
3225 
3226 	if (copy_from_user(&buf, ubuf, cnt))
3227 		return -EFAULT;
3228 
3229 	buf[cnt] = 0;
3230 
3231 	ret = strict_strtoul(buf, 10, &val);
3232 	if (ret < 0)
3233 		return ret;
3234 
3235 	/* must have at least 1 entry */
3236 	if (!val)
3237 		return -EINVAL;
3238 
3239 	mutex_lock(&trace_types_lock);
3240 
3241 	tracing_stop();
3242 
3243 	/* disable all cpu buffers */
3244 	for_each_tracing_cpu(cpu) {
3245 		if (global_trace.data[cpu])
3246 			atomic_inc(&global_trace.data[cpu]->disabled);
3247 		if (max_tr.data[cpu])
3248 			atomic_inc(&max_tr.data[cpu]->disabled);
3249 	}
3250 
3251 	/* value is in KB */
3252 	val <<= 10;
3253 
3254 	if (val != global_trace.entries) {
3255 		ret = tracing_resize_ring_buffer(val);
3256 		if (ret < 0) {
3257 			cnt = ret;
3258 			goto out;
3259 		}
3260 	}
3261 
3262 	filp->f_pos += cnt;
3263 
3264 	/* If check pages failed, return ENOMEM */
3265 	if (tracing_disabled)
3266 		cnt = -ENOMEM;
3267  out:
3268 	for_each_tracing_cpu(cpu) {
3269 		if (global_trace.data[cpu])
3270 			atomic_dec(&global_trace.data[cpu]->disabled);
3271 		if (max_tr.data[cpu])
3272 			atomic_dec(&max_tr.data[cpu]->disabled);
3273 	}
3274 
3275 	tracing_start();
3276 	max_tr.entries = global_trace.entries;
3277 	mutex_unlock(&trace_types_lock);
3278 
3279 	return cnt;
3280 }
3281 
3282 static int mark_printk(const char *fmt, ...)
3283 {
3284 	int ret;
3285 	va_list args;
3286 	va_start(args, fmt);
3287 	ret = trace_vprintk(0, fmt, args);
3288 	va_end(args);
3289 	return ret;
3290 }
3291 
3292 static ssize_t
3293 tracing_mark_write(struct file *filp, const char __user *ubuf,
3294 					size_t cnt, loff_t *fpos)
3295 {
3296 	char *buf;
3297 	char *end;
3298 
3299 	if (tracing_disabled)
3300 		return -EINVAL;
3301 
3302 	if (cnt > TRACE_BUF_SIZE)
3303 		cnt = TRACE_BUF_SIZE;
3304 
3305 	buf = kmalloc(cnt + 1, GFP_KERNEL);
3306 	if (buf == NULL)
3307 		return -ENOMEM;
3308 
3309 	if (copy_from_user(buf, ubuf, cnt)) {
3310 		kfree(buf);
3311 		return -EFAULT;
3312 	}
3313 
3314 	/* Cut from the first nil or newline. */
3315 	buf[cnt] = '\0';
3316 	end = strchr(buf, '\n');
3317 	if (end)
3318 		*end = '\0';
3319 
3320 	cnt = mark_printk("%s\n", buf);
3321 	kfree(buf);
3322 	*fpos += cnt;
3323 
3324 	return cnt;
3325 }
3326 
3327 static const struct file_operations tracing_max_lat_fops = {
3328 	.open		= tracing_open_generic,
3329 	.read		= tracing_max_lat_read,
3330 	.write		= tracing_max_lat_write,
3331 };
3332 
3333 static const struct file_operations tracing_ctrl_fops = {
3334 	.open		= tracing_open_generic,
3335 	.read		= tracing_ctrl_read,
3336 	.write		= tracing_ctrl_write,
3337 };
3338 
3339 static const struct file_operations set_tracer_fops = {
3340 	.open		= tracing_open_generic,
3341 	.read		= tracing_set_trace_read,
3342 	.write		= tracing_set_trace_write,
3343 };
3344 
3345 static const struct file_operations tracing_pipe_fops = {
3346 	.open		= tracing_open_pipe,
3347 	.poll		= tracing_poll_pipe,
3348 	.read		= tracing_read_pipe,
3349 	.splice_read	= tracing_splice_read_pipe,
3350 	.release	= tracing_release_pipe,
3351 };
3352 
3353 static const struct file_operations tracing_entries_fops = {
3354 	.open		= tracing_open_generic,
3355 	.read		= tracing_entries_read,
3356 	.write		= tracing_entries_write,
3357 };
3358 
3359 static const struct file_operations tracing_mark_fops = {
3360 	.open		= tracing_open_generic,
3361 	.write		= tracing_mark_write,
3362 };
3363 
3364 struct ftrace_buffer_info {
3365 	struct trace_array	*tr;
3366 	void			*spare;
3367 	int			cpu;
3368 	unsigned int		read;
3369 };
3370 
3371 static int tracing_buffers_open(struct inode *inode, struct file *filp)
3372 {
3373 	int cpu = (int)(long)inode->i_private;
3374 	struct ftrace_buffer_info *info;
3375 
3376 	if (tracing_disabled)
3377 		return -ENODEV;
3378 
3379 	info = kzalloc(sizeof(*info), GFP_KERNEL);
3380 	if (!info)
3381 		return -ENOMEM;
3382 
3383 	info->tr	= &global_trace;
3384 	info->cpu	= cpu;
3385 	info->spare	= NULL;
3386 	/* Force reading ring buffer for first read */
3387 	info->read	= (unsigned int)-1;
3388 
3389 	filp->private_data = info;
3390 
3391 	return nonseekable_open(inode, filp);
3392 }
3393 
3394 static ssize_t
3395 tracing_buffers_read(struct file *filp, char __user *ubuf,
3396 		     size_t count, loff_t *ppos)
3397 {
3398 	struct ftrace_buffer_info *info = filp->private_data;
3399 	unsigned int pos;
3400 	ssize_t ret;
3401 	size_t size;
3402 
3403 	if (!count)
3404 		return 0;
3405 
3406 	if (!info->spare)
3407 		info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
3408 	if (!info->spare)
3409 		return -ENOMEM;
3410 
3411 	/* Do we have previous read data to read? */
3412 	if (info->read < PAGE_SIZE)
3413 		goto read;
3414 
3415 	info->read = 0;
3416 
3417 	ret = ring_buffer_read_page(info->tr->buffer,
3418 				    &info->spare,
3419 				    count,
3420 				    info->cpu, 0);
3421 	if (ret < 0)
3422 		return 0;
3423 
3424 	pos = ring_buffer_page_len(info->spare);
3425 
3426 	if (pos < PAGE_SIZE)
3427 		memset(info->spare + pos, 0, PAGE_SIZE - pos);
3428 
3429 read:
3430 	size = PAGE_SIZE - info->read;
3431 	if (size > count)
3432 		size = count;
3433 
3434 	ret = copy_to_user(ubuf, info->spare + info->read, size);
3435 	if (ret == size)
3436 		return -EFAULT;
3437 	size -= ret;
3438 
3439 	*ppos += size;
3440 	info->read += size;
3441 
3442 	return size;
3443 }
3444 
3445 static int tracing_buffers_release(struct inode *inode, struct file *file)
3446 {
3447 	struct ftrace_buffer_info *info = file->private_data;
3448 
3449 	if (info->spare)
3450 		ring_buffer_free_read_page(info->tr->buffer, info->spare);
3451 	kfree(info);
3452 
3453 	return 0;
3454 }
3455 
3456 struct buffer_ref {
3457 	struct ring_buffer	*buffer;
3458 	void			*page;
3459 	int			ref;
3460 };
3461 
3462 static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
3463 				    struct pipe_buffer *buf)
3464 {
3465 	struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3466 
3467 	if (--ref->ref)
3468 		return;
3469 
3470 	ring_buffer_free_read_page(ref->buffer, ref->page);
3471 	kfree(ref);
3472 	buf->private = 0;
3473 }
3474 
3475 static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
3476 				 struct pipe_buffer *buf)
3477 {
3478 	return 1;
3479 }
3480 
3481 static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
3482 				struct pipe_buffer *buf)
3483 {
3484 	struct buffer_ref *ref = (struct buffer_ref *)buf->private;
3485 
3486 	ref->ref++;
3487 }
3488 
3489 /* Pipe buffer operations for a buffer. */
3490 static struct pipe_buf_operations buffer_pipe_buf_ops = {
3491 	.can_merge		= 0,
3492 	.map			= generic_pipe_buf_map,
3493 	.unmap			= generic_pipe_buf_unmap,
3494 	.confirm		= generic_pipe_buf_confirm,
3495 	.release		= buffer_pipe_buf_release,
3496 	.steal			= buffer_pipe_buf_steal,
3497 	.get			= buffer_pipe_buf_get,
3498 };
3499 
3500 /*
3501  * Callback from splice_to_pipe(), if we need to release some pages
3502  * at the end of the spd in case we error'ed out in filling the pipe.
3503  */
3504 static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
3505 {
3506 	struct buffer_ref *ref =
3507 		(struct buffer_ref *)spd->partial[i].private;
3508 
3509 	if (--ref->ref)
3510 		return;
3511 
3512 	ring_buffer_free_read_page(ref->buffer, ref->page);
3513 	kfree(ref);
3514 	spd->partial[i].private = 0;
3515 }
3516 
3517 static ssize_t
3518 tracing_buffers_splice_read(struct file *file, loff_t *ppos,
3519 			    struct pipe_inode_info *pipe, size_t len,
3520 			    unsigned int flags)
3521 {
3522 	struct ftrace_buffer_info *info = file->private_data;
3523 	struct partial_page partial[PIPE_BUFFERS];
3524 	struct page *pages[PIPE_BUFFERS];
3525 	struct splice_pipe_desc spd = {
3526 		.pages		= pages,
3527 		.partial	= partial,
3528 		.flags		= flags,
3529 		.ops		= &buffer_pipe_buf_ops,
3530 		.spd_release	= buffer_spd_release,
3531 	};
3532 	struct buffer_ref *ref;
3533 	int entries, size, i;
3534 	size_t ret;
3535 
3536 	if (*ppos & (PAGE_SIZE - 1)) {
3537 		WARN_ONCE(1, "Ftrace: previous read must page-align\n");
3538 		return -EINVAL;
3539 	}
3540 
3541 	if (len & (PAGE_SIZE - 1)) {
3542 		WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
3543 		if (len < PAGE_SIZE)
3544 			return -EINVAL;
3545 		len &= PAGE_MASK;
3546 	}
3547 
3548 	entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3549 
3550 	for (i = 0; i < PIPE_BUFFERS && len && entries; i++, len -= PAGE_SIZE) {
3551 		struct page *page;
3552 		int r;
3553 
3554 		ref = kzalloc(sizeof(*ref), GFP_KERNEL);
3555 		if (!ref)
3556 			break;
3557 
3558 		ref->ref = 1;
3559 		ref->buffer = info->tr->buffer;
3560 		ref->page = ring_buffer_alloc_read_page(ref->buffer);
3561 		if (!ref->page) {
3562 			kfree(ref);
3563 			break;
3564 		}
3565 
3566 		r = ring_buffer_read_page(ref->buffer, &ref->page,
3567 					  len, info->cpu, 1);
3568 		if (r < 0) {
3569 			ring_buffer_free_read_page(ref->buffer,
3570 						   ref->page);
3571 			kfree(ref);
3572 			break;
3573 		}
3574 
3575 		/*
3576 		 * zero out any left over data, this is going to
3577 		 * user land.
3578 		 */
3579 		size = ring_buffer_page_len(ref->page);
3580 		if (size < PAGE_SIZE)
3581 			memset(ref->page + size, 0, PAGE_SIZE - size);
3582 
3583 		page = virt_to_page(ref->page);
3584 
3585 		spd.pages[i] = page;
3586 		spd.partial[i].len = PAGE_SIZE;
3587 		spd.partial[i].offset = 0;
3588 		spd.partial[i].private = (unsigned long)ref;
3589 		spd.nr_pages++;
3590 		*ppos += PAGE_SIZE;
3591 
3592 		entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
3593 	}
3594 
3595 	spd.nr_pages = i;
3596 
3597 	/* did we read anything? */
3598 	if (!spd.nr_pages) {
3599 		if (flags & SPLICE_F_NONBLOCK)
3600 			ret = -EAGAIN;
3601 		else
3602 			ret = 0;
3603 		/* TODO: block */
3604 		return ret;
3605 	}
3606 
3607 	ret = splice_to_pipe(pipe, &spd);
3608 
3609 	return ret;
3610 }
3611 
3612 static const struct file_operations tracing_buffers_fops = {
3613 	.open		= tracing_buffers_open,
3614 	.read		= tracing_buffers_read,
3615 	.release	= tracing_buffers_release,
3616 	.splice_read	= tracing_buffers_splice_read,
3617 	.llseek		= no_llseek,
3618 };
3619 
3620 static ssize_t
3621 tracing_stats_read(struct file *filp, char __user *ubuf,
3622 		   size_t count, loff_t *ppos)
3623 {
3624 	unsigned long cpu = (unsigned long)filp->private_data;
3625 	struct trace_array *tr = &global_trace;
3626 	struct trace_seq *s;
3627 	unsigned long cnt;
3628 
3629 	s = kmalloc(sizeof(*s), GFP_KERNEL);
3630 	if (!s)
3631 		return ENOMEM;
3632 
3633 	trace_seq_init(s);
3634 
3635 	cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
3636 	trace_seq_printf(s, "entries: %ld\n", cnt);
3637 
3638 	cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
3639 	trace_seq_printf(s, "overrun: %ld\n", cnt);
3640 
3641 	cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
3642 	trace_seq_printf(s, "commit overrun: %ld\n", cnt);
3643 
3644 	cnt = ring_buffer_nmi_dropped_cpu(tr->buffer, cpu);
3645 	trace_seq_printf(s, "nmi dropped: %ld\n", cnt);
3646 
3647 	count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
3648 
3649 	kfree(s);
3650 
3651 	return count;
3652 }
3653 
3654 static const struct file_operations tracing_stats_fops = {
3655 	.open		= tracing_open_generic,
3656 	.read		= tracing_stats_read,
3657 };
3658 
3659 #ifdef CONFIG_DYNAMIC_FTRACE
3660 
3661 int __weak ftrace_arch_read_dyn_info(char *buf, int size)
3662 {
3663 	return 0;
3664 }
3665 
3666 static ssize_t
3667 tracing_read_dyn_info(struct file *filp, char __user *ubuf,
3668 		  size_t cnt, loff_t *ppos)
3669 {
3670 	static char ftrace_dyn_info_buffer[1024];
3671 	static DEFINE_MUTEX(dyn_info_mutex);
3672 	unsigned long *p = filp->private_data;
3673 	char *buf = ftrace_dyn_info_buffer;
3674 	int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
3675 	int r;
3676 
3677 	mutex_lock(&dyn_info_mutex);
3678 	r = sprintf(buf, "%ld ", *p);
3679 
3680 	r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
3681 	buf[r++] = '\n';
3682 
3683 	r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
3684 
3685 	mutex_unlock(&dyn_info_mutex);
3686 
3687 	return r;
3688 }
3689 
3690 static const struct file_operations tracing_dyn_info_fops = {
3691 	.open		= tracing_open_generic,
3692 	.read		= tracing_read_dyn_info,
3693 };
3694 #endif
3695 
3696 static struct dentry *d_tracer;
3697 
3698 struct dentry *tracing_init_dentry(void)
3699 {
3700 	static int once;
3701 
3702 	if (d_tracer)
3703 		return d_tracer;
3704 
3705 	if (!debugfs_initialized())
3706 		return NULL;
3707 
3708 	d_tracer = debugfs_create_dir("tracing", NULL);
3709 
3710 	if (!d_tracer && !once) {
3711 		once = 1;
3712 		pr_warning("Could not create debugfs directory 'tracing'\n");
3713 		return NULL;
3714 	}
3715 
3716 	return d_tracer;
3717 }
3718 
3719 static struct dentry *d_percpu;
3720 
3721 struct dentry *tracing_dentry_percpu(void)
3722 {
3723 	static int once;
3724 	struct dentry *d_tracer;
3725 
3726 	if (d_percpu)
3727 		return d_percpu;
3728 
3729 	d_tracer = tracing_init_dentry();
3730 
3731 	if (!d_tracer)
3732 		return NULL;
3733 
3734 	d_percpu = debugfs_create_dir("per_cpu", d_tracer);
3735 
3736 	if (!d_percpu && !once) {
3737 		once = 1;
3738 		pr_warning("Could not create debugfs directory 'per_cpu'\n");
3739 		return NULL;
3740 	}
3741 
3742 	return d_percpu;
3743 }
3744 
3745 static void tracing_init_debugfs_percpu(long cpu)
3746 {
3747 	struct dentry *d_percpu = tracing_dentry_percpu();
3748 	struct dentry *d_cpu;
3749 	/* strlen(cpu) + MAX(log10(cpu)) + '\0' */
3750 	char cpu_dir[7];
3751 
3752 	if (cpu > 999 || cpu < 0)
3753 		return;
3754 
3755 	sprintf(cpu_dir, "cpu%ld", cpu);
3756 	d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
3757 	if (!d_cpu) {
3758 		pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
3759 		return;
3760 	}
3761 
3762 	/* per cpu trace_pipe */
3763 	trace_create_file("trace_pipe", 0444, d_cpu,
3764 			(void *) cpu, &tracing_pipe_fops);
3765 
3766 	/* per cpu trace */
3767 	trace_create_file("trace", 0644, d_cpu,
3768 			(void *) cpu, &tracing_fops);
3769 
3770 	trace_create_file("trace_pipe_raw", 0444, d_cpu,
3771 			(void *) cpu, &tracing_buffers_fops);
3772 
3773 	trace_create_file("stats", 0444, d_cpu,
3774 			(void *) cpu, &tracing_stats_fops);
3775 }
3776 
3777 #ifdef CONFIG_FTRACE_SELFTEST
3778 /* Let selftest have access to static functions in this file */
3779 #include "trace_selftest.c"
3780 #endif
3781 
3782 struct trace_option_dentry {
3783 	struct tracer_opt		*opt;
3784 	struct tracer_flags		*flags;
3785 	struct dentry			*entry;
3786 };
3787 
3788 static ssize_t
3789 trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
3790 			loff_t *ppos)
3791 {
3792 	struct trace_option_dentry *topt = filp->private_data;
3793 	char *buf;
3794 
3795 	if (topt->flags->val & topt->opt->bit)
3796 		buf = "1\n";
3797 	else
3798 		buf = "0\n";
3799 
3800 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3801 }
3802 
3803 static ssize_t
3804 trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
3805 			 loff_t *ppos)
3806 {
3807 	struct trace_option_dentry *topt = filp->private_data;
3808 	unsigned long val;
3809 	char buf[64];
3810 	int ret;
3811 
3812 	if (cnt >= sizeof(buf))
3813 		return -EINVAL;
3814 
3815 	if (copy_from_user(&buf, ubuf, cnt))
3816 		return -EFAULT;
3817 
3818 	buf[cnt] = 0;
3819 
3820 	ret = strict_strtoul(buf, 10, &val);
3821 	if (ret < 0)
3822 		return ret;
3823 
3824 	ret = 0;
3825 	switch (val) {
3826 	case 0:
3827 		/* do nothing if already cleared */
3828 		if (!(topt->flags->val & topt->opt->bit))
3829 			break;
3830 
3831 		mutex_lock(&trace_types_lock);
3832 		if (current_trace->set_flag)
3833 			ret = current_trace->set_flag(topt->flags->val,
3834 						      topt->opt->bit, 0);
3835 		mutex_unlock(&trace_types_lock);
3836 		if (ret)
3837 			return ret;
3838 		topt->flags->val &= ~topt->opt->bit;
3839 		break;
3840 	case 1:
3841 		/* do nothing if already set */
3842 		if (topt->flags->val & topt->opt->bit)
3843 			break;
3844 
3845 		mutex_lock(&trace_types_lock);
3846 		if (current_trace->set_flag)
3847 			ret = current_trace->set_flag(topt->flags->val,
3848 						      topt->opt->bit, 1);
3849 		mutex_unlock(&trace_types_lock);
3850 		if (ret)
3851 			return ret;
3852 		topt->flags->val |= topt->opt->bit;
3853 		break;
3854 
3855 	default:
3856 		return -EINVAL;
3857 	}
3858 
3859 	*ppos += cnt;
3860 
3861 	return cnt;
3862 }
3863 
3864 
3865 static const struct file_operations trace_options_fops = {
3866 	.open = tracing_open_generic,
3867 	.read = trace_options_read,
3868 	.write = trace_options_write,
3869 };
3870 
3871 static ssize_t
3872 trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
3873 			loff_t *ppos)
3874 {
3875 	long index = (long)filp->private_data;
3876 	char *buf;
3877 
3878 	if (trace_flags & (1 << index))
3879 		buf = "1\n";
3880 	else
3881 		buf = "0\n";
3882 
3883 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
3884 }
3885 
3886 static ssize_t
3887 trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
3888 			 loff_t *ppos)
3889 {
3890 	long index = (long)filp->private_data;
3891 	char buf[64];
3892 	unsigned long val;
3893 	int ret;
3894 
3895 	if (cnt >= sizeof(buf))
3896 		return -EINVAL;
3897 
3898 	if (copy_from_user(&buf, ubuf, cnt))
3899 		return -EFAULT;
3900 
3901 	buf[cnt] = 0;
3902 
3903 	ret = strict_strtoul(buf, 10, &val);
3904 	if (ret < 0)
3905 		return ret;
3906 
3907 	switch (val) {
3908 	case 0:
3909 		trace_flags &= ~(1 << index);
3910 		break;
3911 	case 1:
3912 		trace_flags |= 1 << index;
3913 		break;
3914 
3915 	default:
3916 		return -EINVAL;
3917 	}
3918 
3919 	*ppos += cnt;
3920 
3921 	return cnt;
3922 }
3923 
3924 static const struct file_operations trace_options_core_fops = {
3925 	.open = tracing_open_generic,
3926 	.read = trace_options_core_read,
3927 	.write = trace_options_core_write,
3928 };
3929 
3930 struct dentry *trace_create_file(const char *name,
3931 				 mode_t mode,
3932 				 struct dentry *parent,
3933 				 void *data,
3934 				 const struct file_operations *fops)
3935 {
3936 	struct dentry *ret;
3937 
3938 	ret = debugfs_create_file(name, mode, parent, data, fops);
3939 	if (!ret)
3940 		pr_warning("Could not create debugfs '%s' entry\n", name);
3941 
3942 	return ret;
3943 }
3944 
3945 
3946 static struct dentry *trace_options_init_dentry(void)
3947 {
3948 	struct dentry *d_tracer;
3949 	static struct dentry *t_options;
3950 
3951 	if (t_options)
3952 		return t_options;
3953 
3954 	d_tracer = tracing_init_dentry();
3955 	if (!d_tracer)
3956 		return NULL;
3957 
3958 	t_options = debugfs_create_dir("options", d_tracer);
3959 	if (!t_options) {
3960 		pr_warning("Could not create debugfs directory 'options'\n");
3961 		return NULL;
3962 	}
3963 
3964 	return t_options;
3965 }
3966 
3967 static void
3968 create_trace_option_file(struct trace_option_dentry *topt,
3969 			 struct tracer_flags *flags,
3970 			 struct tracer_opt *opt)
3971 {
3972 	struct dentry *t_options;
3973 
3974 	t_options = trace_options_init_dentry();
3975 	if (!t_options)
3976 		return;
3977 
3978 	topt->flags = flags;
3979 	topt->opt = opt;
3980 
3981 	topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
3982 				    &trace_options_fops);
3983 
3984 }
3985 
3986 static struct trace_option_dentry *
3987 create_trace_option_files(struct tracer *tracer)
3988 {
3989 	struct trace_option_dentry *topts;
3990 	struct tracer_flags *flags;
3991 	struct tracer_opt *opts;
3992 	int cnt;
3993 
3994 	if (!tracer)
3995 		return NULL;
3996 
3997 	flags = tracer->flags;
3998 
3999 	if (!flags || !flags->opts)
4000 		return NULL;
4001 
4002 	opts = flags->opts;
4003 
4004 	for (cnt = 0; opts[cnt].name; cnt++)
4005 		;
4006 
4007 	topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
4008 	if (!topts)
4009 		return NULL;
4010 
4011 	for (cnt = 0; opts[cnt].name; cnt++)
4012 		create_trace_option_file(&topts[cnt], flags,
4013 					 &opts[cnt]);
4014 
4015 	return topts;
4016 }
4017 
4018 static void
4019 destroy_trace_option_files(struct trace_option_dentry *topts)
4020 {
4021 	int cnt;
4022 
4023 	if (!topts)
4024 		return;
4025 
4026 	for (cnt = 0; topts[cnt].opt; cnt++) {
4027 		if (topts[cnt].entry)
4028 			debugfs_remove(topts[cnt].entry);
4029 	}
4030 
4031 	kfree(topts);
4032 }
4033 
4034 static struct dentry *
4035 create_trace_option_core_file(const char *option, long index)
4036 {
4037 	struct dentry *t_options;
4038 
4039 	t_options = trace_options_init_dentry();
4040 	if (!t_options)
4041 		return NULL;
4042 
4043 	return trace_create_file(option, 0644, t_options, (void *)index,
4044 				    &trace_options_core_fops);
4045 }
4046 
4047 static __init void create_trace_options_dir(void)
4048 {
4049 	struct dentry *t_options;
4050 	int i;
4051 
4052 	t_options = trace_options_init_dentry();
4053 	if (!t_options)
4054 		return;
4055 
4056 	for (i = 0; trace_options[i]; i++)
4057 		create_trace_option_core_file(trace_options[i], i);
4058 }
4059 
4060 static __init int tracer_init_debugfs(void)
4061 {
4062 	struct dentry *d_tracer;
4063 	int cpu;
4064 
4065 	d_tracer = tracing_init_dentry();
4066 
4067 	trace_create_file("tracing_enabled", 0644, d_tracer,
4068 			&global_trace, &tracing_ctrl_fops);
4069 
4070 	trace_create_file("trace_options", 0644, d_tracer,
4071 			NULL, &tracing_iter_fops);
4072 
4073 	trace_create_file("tracing_cpumask", 0644, d_tracer,
4074 			NULL, &tracing_cpumask_fops);
4075 
4076 	trace_create_file("trace", 0644, d_tracer,
4077 			(void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
4078 
4079 	trace_create_file("available_tracers", 0444, d_tracer,
4080 			&global_trace, &show_traces_fops);
4081 
4082 	trace_create_file("current_tracer", 0644, d_tracer,
4083 			&global_trace, &set_tracer_fops);
4084 
4085 	trace_create_file("tracing_max_latency", 0644, d_tracer,
4086 			&tracing_max_latency, &tracing_max_lat_fops);
4087 
4088 	trace_create_file("tracing_thresh", 0644, d_tracer,
4089 			&tracing_thresh, &tracing_max_lat_fops);
4090 
4091 	trace_create_file("README", 0444, d_tracer,
4092 			NULL, &tracing_readme_fops);
4093 
4094 	trace_create_file("trace_pipe", 0444, d_tracer,
4095 			(void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
4096 
4097 	trace_create_file("buffer_size_kb", 0644, d_tracer,
4098 			&global_trace, &tracing_entries_fops);
4099 
4100 	trace_create_file("trace_marker", 0220, d_tracer,
4101 			NULL, &tracing_mark_fops);
4102 
4103 	trace_create_file("saved_cmdlines", 0444, d_tracer,
4104 			NULL, &tracing_saved_cmdlines_fops);
4105 
4106 #ifdef CONFIG_DYNAMIC_FTRACE
4107 	trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
4108 			&ftrace_update_tot_cnt, &tracing_dyn_info_fops);
4109 #endif
4110 #ifdef CONFIG_SYSPROF_TRACER
4111 	init_tracer_sysprof_debugfs(d_tracer);
4112 #endif
4113 
4114 	create_trace_options_dir();
4115 
4116 	for_each_tracing_cpu(cpu)
4117 		tracing_init_debugfs_percpu(cpu);
4118 
4119 	return 0;
4120 }
4121 
4122 static int trace_panic_handler(struct notifier_block *this,
4123 			       unsigned long event, void *unused)
4124 {
4125 	if (ftrace_dump_on_oops)
4126 		ftrace_dump();
4127 	return NOTIFY_OK;
4128 }
4129 
4130 static struct notifier_block trace_panic_notifier = {
4131 	.notifier_call  = trace_panic_handler,
4132 	.next           = NULL,
4133 	.priority       = 150   /* priority: INT_MAX >= x >= 0 */
4134 };
4135 
4136 static int trace_die_handler(struct notifier_block *self,
4137 			     unsigned long val,
4138 			     void *data)
4139 {
4140 	switch (val) {
4141 	case DIE_OOPS:
4142 		if (ftrace_dump_on_oops)
4143 			ftrace_dump();
4144 		break;
4145 	default:
4146 		break;
4147 	}
4148 	return NOTIFY_OK;
4149 }
4150 
4151 static struct notifier_block trace_die_notifier = {
4152 	.notifier_call = trace_die_handler,
4153 	.priority = 200
4154 };
4155 
4156 /*
4157  * printk is set to max of 1024, we really don't need it that big.
4158  * Nothing should be printing 1000 characters anyway.
4159  */
4160 #define TRACE_MAX_PRINT		1000
4161 
4162 /*
4163  * Define here KERN_TRACE so that we have one place to modify
4164  * it if we decide to change what log level the ftrace dump
4165  * should be at.
4166  */
4167 #define KERN_TRACE		KERN_EMERG
4168 
4169 static void
4170 trace_printk_seq(struct trace_seq *s)
4171 {
4172 	/* Probably should print a warning here. */
4173 	if (s->len >= 1000)
4174 		s->len = 1000;
4175 
4176 	/* should be zero ended, but we are paranoid. */
4177 	s->buffer[s->len] = 0;
4178 
4179 	printk(KERN_TRACE "%s", s->buffer);
4180 
4181 	trace_seq_init(s);
4182 }
4183 
4184 static void __ftrace_dump(bool disable_tracing)
4185 {
4186 	static raw_spinlock_t ftrace_dump_lock =
4187 		(raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
4188 	/* use static because iter can be a bit big for the stack */
4189 	static struct trace_iterator iter;
4190 	unsigned int old_userobj;
4191 	static int dump_ran;
4192 	unsigned long flags;
4193 	int cnt = 0, cpu;
4194 
4195 	/* only one dump */
4196 	local_irq_save(flags);
4197 	__raw_spin_lock(&ftrace_dump_lock);
4198 	if (dump_ran)
4199 		goto out;
4200 
4201 	dump_ran = 1;
4202 
4203 	tracing_off();
4204 
4205 	if (disable_tracing)
4206 		ftrace_kill();
4207 
4208 	for_each_tracing_cpu(cpu) {
4209 		atomic_inc(&global_trace.data[cpu]->disabled);
4210 	}
4211 
4212 	old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
4213 
4214 	/* don't look at user memory in panic mode */
4215 	trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
4216 
4217 	printk(KERN_TRACE "Dumping ftrace buffer:\n");
4218 
4219 	/* Simulate the iterator */
4220 	iter.tr = &global_trace;
4221 	iter.trace = current_trace;
4222 	iter.cpu_file = TRACE_PIPE_ALL_CPU;
4223 
4224 	/*
4225 	 * We need to stop all tracing on all CPUS to read the
4226 	 * the next buffer. This is a bit expensive, but is
4227 	 * not done often. We fill all what we can read,
4228 	 * and then release the locks again.
4229 	 */
4230 
4231 	while (!trace_empty(&iter)) {
4232 
4233 		if (!cnt)
4234 			printk(KERN_TRACE "---------------------------------\n");
4235 
4236 		cnt++;
4237 
4238 		/* reset all but tr, trace, and overruns */
4239 		memset(&iter.seq, 0,
4240 		       sizeof(struct trace_iterator) -
4241 		       offsetof(struct trace_iterator, seq));
4242 		iter.iter_flags |= TRACE_FILE_LAT_FMT;
4243 		iter.pos = -1;
4244 
4245 		if (find_next_entry_inc(&iter) != NULL) {
4246 			print_trace_line(&iter);
4247 			trace_consume(&iter);
4248 		}
4249 
4250 		trace_printk_seq(&iter.seq);
4251 	}
4252 
4253 	if (!cnt)
4254 		printk(KERN_TRACE "   (ftrace buffer empty)\n");
4255 	else
4256 		printk(KERN_TRACE "---------------------------------\n");
4257 
4258 	/* Re-enable tracing if requested */
4259 	if (!disable_tracing) {
4260 		trace_flags |= old_userobj;
4261 
4262 		for_each_tracing_cpu(cpu) {
4263 			atomic_dec(&global_trace.data[cpu]->disabled);
4264 		}
4265 		tracing_on();
4266 	}
4267 
4268  out:
4269 	__raw_spin_unlock(&ftrace_dump_lock);
4270 	local_irq_restore(flags);
4271 }
4272 
4273 /* By default: disable tracing after the dump */
4274 void ftrace_dump(void)
4275 {
4276 	__ftrace_dump(true);
4277 }
4278 
4279 __init static int tracer_alloc_buffers(void)
4280 {
4281 	struct trace_array_cpu *data;
4282 	int ring_buf_size;
4283 	int i;
4284 	int ret = -ENOMEM;
4285 
4286 	if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
4287 		goto out;
4288 
4289 	if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
4290 		goto out_free_buffer_mask;
4291 
4292 	if (!alloc_cpumask_var(&tracing_reader_cpumask, GFP_KERNEL))
4293 		goto out_free_tracing_cpumask;
4294 
4295 	/* To save memory, keep the ring buffer size to its minimum */
4296 	if (ring_buffer_expanded)
4297 		ring_buf_size = trace_buf_size;
4298 	else
4299 		ring_buf_size = 1;
4300 
4301 	cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
4302 	cpumask_copy(tracing_cpumask, cpu_all_mask);
4303 	cpumask_clear(tracing_reader_cpumask);
4304 
4305 	/* TODO: make the number of buffers hot pluggable with CPUS */
4306 	global_trace.buffer = ring_buffer_alloc(ring_buf_size,
4307 						   TRACE_BUFFER_FLAGS);
4308 	if (!global_trace.buffer) {
4309 		printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
4310 		WARN_ON(1);
4311 		goto out_free_cpumask;
4312 	}
4313 	global_trace.entries = ring_buffer_size(global_trace.buffer);
4314 
4315 
4316 #ifdef CONFIG_TRACER_MAX_TRACE
4317 	max_tr.buffer = ring_buffer_alloc(ring_buf_size,
4318 					     TRACE_BUFFER_FLAGS);
4319 	if (!max_tr.buffer) {
4320 		printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
4321 		WARN_ON(1);
4322 		ring_buffer_free(global_trace.buffer);
4323 		goto out_free_cpumask;
4324 	}
4325 	max_tr.entries = ring_buffer_size(max_tr.buffer);
4326 	WARN_ON(max_tr.entries != global_trace.entries);
4327 #endif
4328 
4329 	/* Allocate the first page for all buffers */
4330 	for_each_tracing_cpu(i) {
4331 		data = global_trace.data[i] = &per_cpu(global_trace_cpu, i);
4332 		max_tr.data[i] = &per_cpu(max_data, i);
4333 	}
4334 
4335 	trace_init_cmdlines();
4336 
4337 	register_tracer(&nop_trace);
4338 	current_trace = &nop_trace;
4339 #ifdef CONFIG_BOOT_TRACER
4340 	register_tracer(&boot_tracer);
4341 #endif
4342 	/* All seems OK, enable tracing */
4343 	tracing_disabled = 0;
4344 
4345 	atomic_notifier_chain_register(&panic_notifier_list,
4346 				       &trace_panic_notifier);
4347 
4348 	register_die_notifier(&trace_die_notifier);
4349 
4350 	return 0;
4351 
4352 out_free_cpumask:
4353 	free_cpumask_var(tracing_reader_cpumask);
4354 out_free_tracing_cpumask:
4355 	free_cpumask_var(tracing_cpumask);
4356 out_free_buffer_mask:
4357 	free_cpumask_var(tracing_buffer_mask);
4358 out:
4359 	return ret;
4360 }
4361 
4362 __init static int clear_boot_tracer(void)
4363 {
4364 	/*
4365 	 * The default tracer at boot buffer is an init section.
4366 	 * This function is called in lateinit. If we did not
4367 	 * find the boot tracer, then clear it out, to prevent
4368 	 * later registration from accessing the buffer that is
4369 	 * about to be freed.
4370 	 */
4371 	if (!default_bootup_tracer)
4372 		return 0;
4373 
4374 	printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
4375 	       default_bootup_tracer);
4376 	default_bootup_tracer = NULL;
4377 
4378 	return 0;
4379 }
4380 
4381 early_initcall(tracer_alloc_buffers);
4382 fs_initcall(tracer_init_debugfs);
4383 late_initcall(clear_boot_tracer);
4384