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