Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/trivial
[muen/linux.git] / kernel / trace / trace.c
1 /*
2  * ring buffer based function tracer
3  *
4  * Copyright (C) 2007-2012 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 Nadia Yvette Chambers
13  */
14 #include <linux/ring_buffer.h>
15 #include <generated/utsrelease.h>
16 #include <linux/stacktrace.h>
17 #include <linux/writeback.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/notifier.h>
21 #include <linux/irqflags.h>
22 #include <linux/debugfs.h>
23 #include <linux/tracefs.h>
24 #include <linux/pagemap.h>
25 #include <linux/hardirq.h>
26 #include <linux/linkage.h>
27 #include <linux/uaccess.h>
28 #include <linux/vmalloc.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/mount.h>
36 #include <linux/rwsem.h>
37 #include <linux/slab.h>
38 #include <linux/ctype.h>
39 #include <linux/init.h>
40 #include <linux/poll.h>
41 #include <linux/nmi.h>
42 #include <linux/fs.h>
43 #include <linux/trace.h>
44 #include <linux/sched/rt.h>
45
46 #include "trace.h"
47 #include "trace_output.h"
48
49 /*
50  * On boot up, the ring buffer is set to the minimum size, so that
51  * we do not waste memory on systems that are not using tracing.
52  */
53 bool ring_buffer_expanded;
54
55 /*
56  * We need to change this state when a selftest is running.
57  * A selftest will lurk into the ring-buffer to count the
58  * entries inserted during the selftest although some concurrent
59  * insertions into the ring-buffer such as trace_printk could occurred
60  * at the same time, giving false positive or negative results.
61  */
62 static bool __read_mostly tracing_selftest_running;
63
64 /*
65  * If a tracer is running, we do not want to run SELFTEST.
66  */
67 bool __read_mostly tracing_selftest_disabled;
68
69 /* Pipe tracepoints to printk */
70 struct trace_iterator *tracepoint_print_iter;
71 int tracepoint_printk;
72 static DEFINE_STATIC_KEY_FALSE(tracepoint_printk_key);
73
74 /* For tracers that don't implement custom flags */
75 static struct tracer_opt dummy_tracer_opt[] = {
76         { }
77 };
78
79 static int
80 dummy_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
81 {
82         return 0;
83 }
84
85 /*
86  * To prevent the comm cache from being overwritten when no
87  * tracing is active, only save the comm when a trace event
88  * occurred.
89  */
90 static DEFINE_PER_CPU(bool, trace_taskinfo_save);
91
92 /*
93  * Kill all tracing for good (never come back).
94  * It is initialized to 1 but will turn to zero if the initialization
95  * of the tracer is successful. But that is the only place that sets
96  * this back to zero.
97  */
98 static int tracing_disabled = 1;
99
100 cpumask_var_t __read_mostly     tracing_buffer_mask;
101
102 /*
103  * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
104  *
105  * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
106  * is set, then ftrace_dump is called. This will output the contents
107  * of the ftrace buffers to the console.  This is very useful for
108  * capturing traces that lead to crashes and outputing it to a
109  * serial console.
110  *
111  * It is default off, but you can enable it with either specifying
112  * "ftrace_dump_on_oops" in the kernel command line, or setting
113  * /proc/sys/kernel/ftrace_dump_on_oops
114  * Set 1 if you want to dump buffers of all CPUs
115  * Set 2 if you want to dump the buffer of the CPU that triggered oops
116  */
117
118 enum ftrace_dump_mode ftrace_dump_on_oops;
119
120 /* When set, tracing will stop when a WARN*() is hit */
121 int __disable_trace_on_warning;
122
123 #ifdef CONFIG_TRACE_EVAL_MAP_FILE
124 /* Map of enums to their values, for "eval_map" file */
125 struct trace_eval_map_head {
126         struct module                   *mod;
127         unsigned long                   length;
128 };
129
130 union trace_eval_map_item;
131
132 struct trace_eval_map_tail {
133         /*
134          * "end" is first and points to NULL as it must be different
135          * than "mod" or "eval_string"
136          */
137         union trace_eval_map_item       *next;
138         const char                      *end;   /* points to NULL */
139 };
140
141 static DEFINE_MUTEX(trace_eval_mutex);
142
143 /*
144  * The trace_eval_maps are saved in an array with two extra elements,
145  * one at the beginning, and one at the end. The beginning item contains
146  * the count of the saved maps (head.length), and the module they
147  * belong to if not built in (head.mod). The ending item contains a
148  * pointer to the next array of saved eval_map items.
149  */
150 union trace_eval_map_item {
151         struct trace_eval_map           map;
152         struct trace_eval_map_head      head;
153         struct trace_eval_map_tail      tail;
154 };
155
156 static union trace_eval_map_item *trace_eval_maps;
157 #endif /* CONFIG_TRACE_EVAL_MAP_FILE */
158
159 static int tracing_set_tracer(struct trace_array *tr, const char *buf);
160
161 #define MAX_TRACER_SIZE         100
162 static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
163 static char *default_bootup_tracer;
164
165 static bool allocate_snapshot;
166
167 static int __init set_cmdline_ftrace(char *str)
168 {
169         strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
170         default_bootup_tracer = bootup_tracer_buf;
171         /* We are using ftrace early, expand it */
172         ring_buffer_expanded = true;
173         return 1;
174 }
175 __setup("ftrace=", set_cmdline_ftrace);
176
177 static int __init set_ftrace_dump_on_oops(char *str)
178 {
179         if (*str++ != '=' || !*str) {
180                 ftrace_dump_on_oops = DUMP_ALL;
181                 return 1;
182         }
183
184         if (!strcmp("orig_cpu", str)) {
185                 ftrace_dump_on_oops = DUMP_ORIG;
186                 return 1;
187         }
188
189         return 0;
190 }
191 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
192
193 static int __init stop_trace_on_warning(char *str)
194 {
195         if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
196                 __disable_trace_on_warning = 1;
197         return 1;
198 }
199 __setup("traceoff_on_warning", stop_trace_on_warning);
200
201 static int __init boot_alloc_snapshot(char *str)
202 {
203         allocate_snapshot = true;
204         /* We also need the main ring buffer expanded */
205         ring_buffer_expanded = true;
206         return 1;
207 }
208 __setup("alloc_snapshot", boot_alloc_snapshot);
209
210
211 static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
212
213 static int __init set_trace_boot_options(char *str)
214 {
215         strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
216         return 0;
217 }
218 __setup("trace_options=", set_trace_boot_options);
219
220 static char trace_boot_clock_buf[MAX_TRACER_SIZE] __initdata;
221 static char *trace_boot_clock __initdata;
222
223 static int __init set_trace_boot_clock(char *str)
224 {
225         strlcpy(trace_boot_clock_buf, str, MAX_TRACER_SIZE);
226         trace_boot_clock = trace_boot_clock_buf;
227         return 0;
228 }
229 __setup("trace_clock=", set_trace_boot_clock);
230
231 static int __init set_tracepoint_printk(char *str)
232 {
233         if ((strcmp(str, "=0") != 0 && strcmp(str, "=off") != 0))
234                 tracepoint_printk = 1;
235         return 1;
236 }
237 __setup("tp_printk", set_tracepoint_printk);
238
239 unsigned long long ns2usecs(u64 nsec)
240 {
241         nsec += 500;
242         do_div(nsec, 1000);
243         return nsec;
244 }
245
246 /* trace_flags holds trace_options default values */
247 #define TRACE_DEFAULT_FLAGS                                             \
248         (FUNCTION_DEFAULT_FLAGS |                                       \
249          TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |                  \
250          TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO |                \
251          TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |                 \
252          TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS)
253
254 /* trace_options that are only supported by global_trace */
255 #define TOP_LEVEL_TRACE_FLAGS (TRACE_ITER_PRINTK |                      \
256                TRACE_ITER_PRINTK_MSGONLY | TRACE_ITER_RECORD_CMD)
257
258 /* trace_flags that are default zero for instances */
259 #define ZEROED_TRACE_FLAGS \
260         (TRACE_ITER_EVENT_FORK | TRACE_ITER_FUNC_FORK)
261
262 /*
263  * The global_trace is the descriptor that holds the top-level tracing
264  * buffers for the live tracing.
265  */
266 static struct trace_array global_trace = {
267         .trace_flags = TRACE_DEFAULT_FLAGS,
268 };
269
270 LIST_HEAD(ftrace_trace_arrays);
271
272 int trace_array_get(struct trace_array *this_tr)
273 {
274         struct trace_array *tr;
275         int ret = -ENODEV;
276
277         mutex_lock(&trace_types_lock);
278         list_for_each_entry(tr, &ftrace_trace_arrays, list) {
279                 if (tr == this_tr) {
280                         tr->ref++;
281                         ret = 0;
282                         break;
283                 }
284         }
285         mutex_unlock(&trace_types_lock);
286
287         return ret;
288 }
289
290 static void __trace_array_put(struct trace_array *this_tr)
291 {
292         WARN_ON(!this_tr->ref);
293         this_tr->ref--;
294 }
295
296 void trace_array_put(struct trace_array *this_tr)
297 {
298         mutex_lock(&trace_types_lock);
299         __trace_array_put(this_tr);
300         mutex_unlock(&trace_types_lock);
301 }
302
303 int call_filter_check_discard(struct trace_event_call *call, void *rec,
304                               struct ring_buffer *buffer,
305                               struct ring_buffer_event *event)
306 {
307         if (unlikely(call->flags & TRACE_EVENT_FL_FILTERED) &&
308             !filter_match_preds(call->filter, rec)) {
309                 __trace_event_discard_commit(buffer, event);
310                 return 1;
311         }
312
313         return 0;
314 }
315
316 void trace_free_pid_list(struct trace_pid_list *pid_list)
317 {
318         vfree(pid_list->pids);
319         kfree(pid_list);
320 }
321
322 /**
323  * trace_find_filtered_pid - check if a pid exists in a filtered_pid list
324  * @filtered_pids: The list of pids to check
325  * @search_pid: The PID to find in @filtered_pids
326  *
327  * Returns true if @search_pid is fonud in @filtered_pids, and false otherwis.
328  */
329 bool
330 trace_find_filtered_pid(struct trace_pid_list *filtered_pids, pid_t search_pid)
331 {
332         /*
333          * If pid_max changed after filtered_pids was created, we
334          * by default ignore all pids greater than the previous pid_max.
335          */
336         if (search_pid >= filtered_pids->pid_max)
337                 return false;
338
339         return test_bit(search_pid, filtered_pids->pids);
340 }
341
342 /**
343  * trace_ignore_this_task - should a task be ignored for tracing
344  * @filtered_pids: The list of pids to check
345  * @task: The task that should be ignored if not filtered
346  *
347  * Checks if @task should be traced or not from @filtered_pids.
348  * Returns true if @task should *NOT* be traced.
349  * Returns false if @task should be traced.
350  */
351 bool
352 trace_ignore_this_task(struct trace_pid_list *filtered_pids, struct task_struct *task)
353 {
354         /*
355          * Return false, because if filtered_pids does not exist,
356          * all pids are good to trace.
357          */
358         if (!filtered_pids)
359                 return false;
360
361         return !trace_find_filtered_pid(filtered_pids, task->pid);
362 }
363
364 /**
365  * trace_pid_filter_add_remove_task - Add or remove a task from a pid_list
366  * @pid_list: The list to modify
367  * @self: The current task for fork or NULL for exit
368  * @task: The task to add or remove
369  *
370  * If adding a task, if @self is defined, the task is only added if @self
371  * is also included in @pid_list. This happens on fork and tasks should
372  * only be added when the parent is listed. If @self is NULL, then the
373  * @task pid will be removed from the list, which would happen on exit
374  * of a task.
375  */
376 void trace_filter_add_remove_task(struct trace_pid_list *pid_list,
377                                   struct task_struct *self,
378                                   struct task_struct *task)
379 {
380         if (!pid_list)
381                 return;
382
383         /* For forks, we only add if the forking task is listed */
384         if (self) {
385                 if (!trace_find_filtered_pid(pid_list, self->pid))
386                         return;
387         }
388
389         /* Sorry, but we don't support pid_max changing after setting */
390         if (task->pid >= pid_list->pid_max)
391                 return;
392
393         /* "self" is set for forks, and NULL for exits */
394         if (self)
395                 set_bit(task->pid, pid_list->pids);
396         else
397                 clear_bit(task->pid, pid_list->pids);
398 }
399
400 /**
401  * trace_pid_next - Used for seq_file to get to the next pid of a pid_list
402  * @pid_list: The pid list to show
403  * @v: The last pid that was shown (+1 the actual pid to let zero be displayed)
404  * @pos: The position of the file
405  *
406  * This is used by the seq_file "next" operation to iterate the pids
407  * listed in a trace_pid_list structure.
408  *
409  * Returns the pid+1 as we want to display pid of zero, but NULL would
410  * stop the iteration.
411  */
412 void *trace_pid_next(struct trace_pid_list *pid_list, void *v, loff_t *pos)
413 {
414         unsigned long pid = (unsigned long)v;
415
416         (*pos)++;
417
418         /* pid already is +1 of the actual prevous bit */
419         pid = find_next_bit(pid_list->pids, pid_list->pid_max, pid);
420
421         /* Return pid + 1 to allow zero to be represented */
422         if (pid < pid_list->pid_max)
423                 return (void *)(pid + 1);
424
425         return NULL;
426 }
427
428 /**
429  * trace_pid_start - Used for seq_file to start reading pid lists
430  * @pid_list: The pid list to show
431  * @pos: The position of the file
432  *
433  * This is used by seq_file "start" operation to start the iteration
434  * of listing pids.
435  *
436  * Returns the pid+1 as we want to display pid of zero, but NULL would
437  * stop the iteration.
438  */
439 void *trace_pid_start(struct trace_pid_list *pid_list, loff_t *pos)
440 {
441         unsigned long pid;
442         loff_t l = 0;
443
444         pid = find_first_bit(pid_list->pids, pid_list->pid_max);
445         if (pid >= pid_list->pid_max)
446                 return NULL;
447
448         /* Return pid + 1 so that zero can be the exit value */
449         for (pid++; pid && l < *pos;
450              pid = (unsigned long)trace_pid_next(pid_list, (void *)pid, &l))
451                 ;
452         return (void *)pid;
453 }
454
455 /**
456  * trace_pid_show - show the current pid in seq_file processing
457  * @m: The seq_file structure to write into
458  * @v: A void pointer of the pid (+1) value to display
459  *
460  * Can be directly used by seq_file operations to display the current
461  * pid value.
462  */
463 int trace_pid_show(struct seq_file *m, void *v)
464 {
465         unsigned long pid = (unsigned long)v - 1;
466
467         seq_printf(m, "%lu\n", pid);
468         return 0;
469 }
470
471 /* 128 should be much more than enough */
472 #define PID_BUF_SIZE            127
473
474 int trace_pid_write(struct trace_pid_list *filtered_pids,
475                     struct trace_pid_list **new_pid_list,
476                     const char __user *ubuf, size_t cnt)
477 {
478         struct trace_pid_list *pid_list;
479         struct trace_parser parser;
480         unsigned long val;
481         int nr_pids = 0;
482         ssize_t read = 0;
483         ssize_t ret = 0;
484         loff_t pos;
485         pid_t pid;
486
487         if (trace_parser_get_init(&parser, PID_BUF_SIZE + 1))
488                 return -ENOMEM;
489
490         /*
491          * Always recreate a new array. The write is an all or nothing
492          * operation. Always create a new array when adding new pids by
493          * the user. If the operation fails, then the current list is
494          * not modified.
495          */
496         pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL);
497         if (!pid_list)
498                 return -ENOMEM;
499
500         pid_list->pid_max = READ_ONCE(pid_max);
501
502         /* Only truncating will shrink pid_max */
503         if (filtered_pids && filtered_pids->pid_max > pid_list->pid_max)
504                 pid_list->pid_max = filtered_pids->pid_max;
505
506         pid_list->pids = vzalloc((pid_list->pid_max + 7) >> 3);
507         if (!pid_list->pids) {
508                 kfree(pid_list);
509                 return -ENOMEM;
510         }
511
512         if (filtered_pids) {
513                 /* copy the current bits to the new max */
514                 for_each_set_bit(pid, filtered_pids->pids,
515                                  filtered_pids->pid_max) {
516                         set_bit(pid, pid_list->pids);
517                         nr_pids++;
518                 }
519         }
520
521         while (cnt > 0) {
522
523                 pos = 0;
524
525                 ret = trace_get_user(&parser, ubuf, cnt, &pos);
526                 if (ret < 0 || !trace_parser_loaded(&parser))
527                         break;
528
529                 read += ret;
530                 ubuf += ret;
531                 cnt -= ret;
532
533                 ret = -EINVAL;
534                 if (kstrtoul(parser.buffer, 0, &val))
535                         break;
536                 if (val >= pid_list->pid_max)
537                         break;
538
539                 pid = (pid_t)val;
540
541                 set_bit(pid, pid_list->pids);
542                 nr_pids++;
543
544                 trace_parser_clear(&parser);
545                 ret = 0;
546         }
547         trace_parser_put(&parser);
548
549         if (ret < 0) {
550                 trace_free_pid_list(pid_list);
551                 return ret;
552         }
553
554         if (!nr_pids) {
555                 /* Cleared the list of pids */
556                 trace_free_pid_list(pid_list);
557                 read = ret;
558                 pid_list = NULL;
559         }
560
561         *new_pid_list = pid_list;
562
563         return read;
564 }
565
566 static u64 buffer_ftrace_now(struct trace_buffer *buf, int cpu)
567 {
568         u64 ts;
569
570         /* Early boot up does not have a buffer yet */
571         if (!buf->buffer)
572                 return trace_clock_local();
573
574         ts = ring_buffer_time_stamp(buf->buffer, cpu);
575         ring_buffer_normalize_time_stamp(buf->buffer, cpu, &ts);
576
577         return ts;
578 }
579
580 u64 ftrace_now(int cpu)
581 {
582         return buffer_ftrace_now(&global_trace.trace_buffer, cpu);
583 }
584
585 /**
586  * tracing_is_enabled - Show if global_trace has been disabled
587  *
588  * Shows if the global trace has been enabled or not. It uses the
589  * mirror flag "buffer_disabled" to be used in fast paths such as for
590  * the irqsoff tracer. But it may be inaccurate due to races. If you
591  * need to know the accurate state, use tracing_is_on() which is a little
592  * slower, but accurate.
593  */
594 int tracing_is_enabled(void)
595 {
596         /*
597          * For quick access (irqsoff uses this in fast path), just
598          * return the mirror variable of the state of the ring buffer.
599          * It's a little racy, but we don't really care.
600          */
601         smp_rmb();
602         return !global_trace.buffer_disabled;
603 }
604
605 /*
606  * trace_buf_size is the size in bytes that is allocated
607  * for a buffer. Note, the number of bytes is always rounded
608  * to page size.
609  *
610  * This number is purposely set to a low number of 16384.
611  * If the dump on oops happens, it will be much appreciated
612  * to not have to wait for all that output. Anyway this can be
613  * boot time and run time configurable.
614  */
615 #define TRACE_BUF_SIZE_DEFAULT  1441792UL /* 16384 * 88 (sizeof(entry)) */
616
617 static unsigned long            trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
618
619 /* trace_types holds a link list of available tracers. */
620 static struct tracer            *trace_types __read_mostly;
621
622 /*
623  * trace_types_lock is used to protect the trace_types list.
624  */
625 DEFINE_MUTEX(trace_types_lock);
626
627 /*
628  * serialize the access of the ring buffer
629  *
630  * ring buffer serializes readers, but it is low level protection.
631  * The validity of the events (which returns by ring_buffer_peek() ..etc)
632  * are not protected by ring buffer.
633  *
634  * The content of events may become garbage if we allow other process consumes
635  * these events concurrently:
636  *   A) the page of the consumed events may become a normal page
637  *      (not reader page) in ring buffer, and this page will be rewrited
638  *      by events producer.
639  *   B) The page of the consumed events may become a page for splice_read,
640  *      and this page will be returned to system.
641  *
642  * These primitives allow multi process access to different cpu ring buffer
643  * concurrently.
644  *
645  * These primitives don't distinguish read-only and read-consume access.
646  * Multi read-only access are also serialized.
647  */
648
649 #ifdef CONFIG_SMP
650 static DECLARE_RWSEM(all_cpu_access_lock);
651 static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
652
653 static inline void trace_access_lock(int cpu)
654 {
655         if (cpu == RING_BUFFER_ALL_CPUS) {
656                 /* gain it for accessing the whole ring buffer. */
657                 down_write(&all_cpu_access_lock);
658         } else {
659                 /* gain it for accessing a cpu ring buffer. */
660
661                 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
662                 down_read(&all_cpu_access_lock);
663
664                 /* Secondly block other access to this @cpu ring buffer. */
665                 mutex_lock(&per_cpu(cpu_access_lock, cpu));
666         }
667 }
668
669 static inline void trace_access_unlock(int cpu)
670 {
671         if (cpu == RING_BUFFER_ALL_CPUS) {
672                 up_write(&all_cpu_access_lock);
673         } else {
674                 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
675                 up_read(&all_cpu_access_lock);
676         }
677 }
678
679 static inline void trace_access_lock_init(void)
680 {
681         int cpu;
682
683         for_each_possible_cpu(cpu)
684                 mutex_init(&per_cpu(cpu_access_lock, cpu));
685 }
686
687 #else
688
689 static DEFINE_MUTEX(access_lock);
690
691 static inline void trace_access_lock(int cpu)
692 {
693         (void)cpu;
694         mutex_lock(&access_lock);
695 }
696
697 static inline void trace_access_unlock(int cpu)
698 {
699         (void)cpu;
700         mutex_unlock(&access_lock);
701 }
702
703 static inline void trace_access_lock_init(void)
704 {
705 }
706
707 #endif
708
709 #ifdef CONFIG_STACKTRACE
710 static void __ftrace_trace_stack(struct ring_buffer *buffer,
711                                  unsigned long flags,
712                                  int skip, int pc, struct pt_regs *regs);
713 static inline void ftrace_trace_stack(struct trace_array *tr,
714                                       struct ring_buffer *buffer,
715                                       unsigned long flags,
716                                       int skip, int pc, struct pt_regs *regs);
717
718 #else
719 static inline void __ftrace_trace_stack(struct ring_buffer *buffer,
720                                         unsigned long flags,
721                                         int skip, int pc, struct pt_regs *regs)
722 {
723 }
724 static inline void ftrace_trace_stack(struct trace_array *tr,
725                                       struct ring_buffer *buffer,
726                                       unsigned long flags,
727                                       int skip, int pc, struct pt_regs *regs)
728 {
729 }
730
731 #endif
732
733 static __always_inline void
734 trace_event_setup(struct ring_buffer_event *event,
735                   int type, unsigned long flags, int pc)
736 {
737         struct trace_entry *ent = ring_buffer_event_data(event);
738
739         tracing_generic_entry_update(ent, flags, pc);
740         ent->type = type;
741 }
742
743 static __always_inline struct ring_buffer_event *
744 __trace_buffer_lock_reserve(struct ring_buffer *buffer,
745                           int type,
746                           unsigned long len,
747                           unsigned long flags, int pc)
748 {
749         struct ring_buffer_event *event;
750
751         event = ring_buffer_lock_reserve(buffer, len);
752         if (event != NULL)
753                 trace_event_setup(event, type, flags, pc);
754
755         return event;
756 }
757
758 void tracer_tracing_on(struct trace_array *tr)
759 {
760         if (tr->trace_buffer.buffer)
761                 ring_buffer_record_on(tr->trace_buffer.buffer);
762         /*
763          * This flag is looked at when buffers haven't been allocated
764          * yet, or by some tracers (like irqsoff), that just want to
765          * know if the ring buffer has been disabled, but it can handle
766          * races of where it gets disabled but we still do a record.
767          * As the check is in the fast path of the tracers, it is more
768          * important to be fast than accurate.
769          */
770         tr->buffer_disabled = 0;
771         /* Make the flag seen by readers */
772         smp_wmb();
773 }
774
775 /**
776  * tracing_on - enable tracing buffers
777  *
778  * This function enables tracing buffers that may have been
779  * disabled with tracing_off.
780  */
781 void tracing_on(void)
782 {
783         tracer_tracing_on(&global_trace);
784 }
785 EXPORT_SYMBOL_GPL(tracing_on);
786
787
788 static __always_inline void
789 __buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
790 {
791         __this_cpu_write(trace_taskinfo_save, true);
792
793         /* If this is the temp buffer, we need to commit fully */
794         if (this_cpu_read(trace_buffered_event) == event) {
795                 /* Length is in event->array[0] */
796                 ring_buffer_write(buffer, event->array[0], &event->array[1]);
797                 /* Release the temp buffer */
798                 this_cpu_dec(trace_buffered_event_cnt);
799         } else
800                 ring_buffer_unlock_commit(buffer, event);
801 }
802
803 /**
804  * __trace_puts - write a constant string into the trace buffer.
805  * @ip:    The address of the caller
806  * @str:   The constant string to write
807  * @size:  The size of the string.
808  */
809 int __trace_puts(unsigned long ip, const char *str, int size)
810 {
811         struct ring_buffer_event *event;
812         struct ring_buffer *buffer;
813         struct print_entry *entry;
814         unsigned long irq_flags;
815         int alloc;
816         int pc;
817
818         if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
819                 return 0;
820
821         pc = preempt_count();
822
823         if (unlikely(tracing_selftest_running || tracing_disabled))
824                 return 0;
825
826         alloc = sizeof(*entry) + size + 2; /* possible \n added */
827
828         local_save_flags(irq_flags);
829         buffer = global_trace.trace_buffer.buffer;
830         event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc, 
831                                             irq_flags, pc);
832         if (!event)
833                 return 0;
834
835         entry = ring_buffer_event_data(event);
836         entry->ip = ip;
837
838         memcpy(&entry->buf, str, size);
839
840         /* Add a newline if necessary */
841         if (entry->buf[size - 1] != '\n') {
842                 entry->buf[size] = '\n';
843                 entry->buf[size + 1] = '\0';
844         } else
845                 entry->buf[size] = '\0';
846
847         __buffer_unlock_commit(buffer, event);
848         ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL);
849
850         return size;
851 }
852 EXPORT_SYMBOL_GPL(__trace_puts);
853
854 /**
855  * __trace_bputs - write the pointer to a constant string into trace buffer
856  * @ip:    The address of the caller
857  * @str:   The constant string to write to the buffer to
858  */
859 int __trace_bputs(unsigned long ip, const char *str)
860 {
861         struct ring_buffer_event *event;
862         struct ring_buffer *buffer;
863         struct bputs_entry *entry;
864         unsigned long irq_flags;
865         int size = sizeof(struct bputs_entry);
866         int pc;
867
868         if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
869                 return 0;
870
871         pc = preempt_count();
872
873         if (unlikely(tracing_selftest_running || tracing_disabled))
874                 return 0;
875
876         local_save_flags(irq_flags);
877         buffer = global_trace.trace_buffer.buffer;
878         event = __trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
879                                             irq_flags, pc);
880         if (!event)
881                 return 0;
882
883         entry = ring_buffer_event_data(event);
884         entry->ip                       = ip;
885         entry->str                      = str;
886
887         __buffer_unlock_commit(buffer, event);
888         ftrace_trace_stack(&global_trace, buffer, irq_flags, 4, pc, NULL);
889
890         return 1;
891 }
892 EXPORT_SYMBOL_GPL(__trace_bputs);
893
894 #ifdef CONFIG_TRACER_SNAPSHOT
895 static void tracing_snapshot_instance(struct trace_array *tr)
896 {
897         struct tracer *tracer = tr->current_trace;
898         unsigned long flags;
899
900         if (in_nmi()) {
901                 internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
902                 internal_trace_puts("*** snapshot is being ignored        ***\n");
903                 return;
904         }
905
906         if (!tr->allocated_snapshot) {
907                 internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n");
908                 internal_trace_puts("*** stopping trace here!   ***\n");
909                 tracing_off();
910                 return;
911         }
912
913         /* Note, snapshot can not be used when the tracer uses it */
914         if (tracer->use_max_tr) {
915                 internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n");
916                 internal_trace_puts("*** Can not use snapshot (sorry) ***\n");
917                 return;
918         }
919
920         local_irq_save(flags);
921         update_max_tr(tr, current, smp_processor_id());
922         local_irq_restore(flags);
923 }
924
925 /**
926  * tracing_snapshot - take a snapshot of the current buffer.
927  *
928  * This causes a swap between the snapshot buffer and the current live
929  * tracing buffer. You can use this to take snapshots of the live
930  * trace when some condition is triggered, but continue to trace.
931  *
932  * Note, make sure to allocate the snapshot with either
933  * a tracing_snapshot_alloc(), or by doing it manually
934  * with: echo 1 > /sys/kernel/debug/tracing/snapshot
935  *
936  * If the snapshot buffer is not allocated, it will stop tracing.
937  * Basically making a permanent snapshot.
938  */
939 void tracing_snapshot(void)
940 {
941         struct trace_array *tr = &global_trace;
942
943         tracing_snapshot_instance(tr);
944 }
945 EXPORT_SYMBOL_GPL(tracing_snapshot);
946
947 static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
948                                         struct trace_buffer *size_buf, int cpu_id);
949 static void set_buffer_entries(struct trace_buffer *buf, unsigned long val);
950
951 static int alloc_snapshot(struct trace_array *tr)
952 {
953         int ret;
954
955         if (!tr->allocated_snapshot) {
956
957                 /* allocate spare buffer */
958                 ret = resize_buffer_duplicate_size(&tr->max_buffer,
959                                    &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
960                 if (ret < 0)
961                         return ret;
962
963                 tr->allocated_snapshot = true;
964         }
965
966         return 0;
967 }
968
969 static void free_snapshot(struct trace_array *tr)
970 {
971         /*
972          * We don't free the ring buffer. instead, resize it because
973          * The max_tr ring buffer has some state (e.g. ring->clock) and
974          * we want preserve it.
975          */
976         ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
977         set_buffer_entries(&tr->max_buffer, 1);
978         tracing_reset_online_cpus(&tr->max_buffer);
979         tr->allocated_snapshot = false;
980 }
981
982 /**
983  * tracing_alloc_snapshot - allocate snapshot buffer.
984  *
985  * This only allocates the snapshot buffer if it isn't already
986  * allocated - it doesn't also take a snapshot.
987  *
988  * This is meant to be used in cases where the snapshot buffer needs
989  * to be set up for events that can't sleep but need to be able to
990  * trigger a snapshot.
991  */
992 int tracing_alloc_snapshot(void)
993 {
994         struct trace_array *tr = &global_trace;
995         int ret;
996
997         ret = alloc_snapshot(tr);
998         WARN_ON(ret < 0);
999
1000         return ret;
1001 }
1002 EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
1003
1004 /**
1005  * tracing_snapshot_alloc - allocate and take a snapshot of the current buffer.
1006  *
1007  * This is similar to tracing_snapshot(), but it will allocate the
1008  * snapshot buffer if it isn't already allocated. Use this only
1009  * where it is safe to sleep, as the allocation may sleep.
1010  *
1011  * This causes a swap between the snapshot buffer and the current live
1012  * tracing buffer. You can use this to take snapshots of the live
1013  * trace when some condition is triggered, but continue to trace.
1014  */
1015 void tracing_snapshot_alloc(void)
1016 {
1017         int ret;
1018
1019         ret = tracing_alloc_snapshot();
1020         if (ret < 0)
1021                 return;
1022
1023         tracing_snapshot();
1024 }
1025 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
1026 #else
1027 void tracing_snapshot(void)
1028 {
1029         WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
1030 }
1031 EXPORT_SYMBOL_GPL(tracing_snapshot);
1032 int tracing_alloc_snapshot(void)
1033 {
1034         WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used");
1035         return -ENODEV;
1036 }
1037 EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
1038 void tracing_snapshot_alloc(void)
1039 {
1040         /* Give warning */
1041         tracing_snapshot();
1042 }
1043 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
1044 #endif /* CONFIG_TRACER_SNAPSHOT */
1045
1046 void tracer_tracing_off(struct trace_array *tr)
1047 {
1048         if (tr->trace_buffer.buffer)
1049                 ring_buffer_record_off(tr->trace_buffer.buffer);
1050         /*
1051          * This flag is looked at when buffers haven't been allocated
1052          * yet, or by some tracers (like irqsoff), that just want to
1053          * know if the ring buffer has been disabled, but it can handle
1054          * races of where it gets disabled but we still do a record.
1055          * As the check is in the fast path of the tracers, it is more
1056          * important to be fast than accurate.
1057          */
1058         tr->buffer_disabled = 1;
1059         /* Make the flag seen by readers */
1060         smp_wmb();
1061 }
1062
1063 /**
1064  * tracing_off - turn off tracing buffers
1065  *
1066  * This function stops the tracing buffers from recording data.
1067  * It does not disable any overhead the tracers themselves may
1068  * be causing. This function simply causes all recording to
1069  * the ring buffers to fail.
1070  */
1071 void tracing_off(void)
1072 {
1073         tracer_tracing_off(&global_trace);
1074 }
1075 EXPORT_SYMBOL_GPL(tracing_off);
1076
1077 void disable_trace_on_warning(void)
1078 {
1079         if (__disable_trace_on_warning)
1080                 tracing_off();
1081 }
1082
1083 /**
1084  * tracer_tracing_is_on - show real state of ring buffer enabled
1085  * @tr : the trace array to know if ring buffer is enabled
1086  *
1087  * Shows real state of the ring buffer if it is enabled or not.
1088  */
1089 int tracer_tracing_is_on(struct trace_array *tr)
1090 {
1091         if (tr->trace_buffer.buffer)
1092                 return ring_buffer_record_is_on(tr->trace_buffer.buffer);
1093         return !tr->buffer_disabled;
1094 }
1095
1096 /**
1097  * tracing_is_on - show state of ring buffers enabled
1098  */
1099 int tracing_is_on(void)
1100 {
1101         return tracer_tracing_is_on(&global_trace);
1102 }
1103 EXPORT_SYMBOL_GPL(tracing_is_on);
1104
1105 static int __init set_buf_size(char *str)
1106 {
1107         unsigned long buf_size;
1108
1109         if (!str)
1110                 return 0;
1111         buf_size = memparse(str, &str);
1112         /* nr_entries can not be zero */
1113         if (buf_size == 0)
1114                 return 0;
1115         trace_buf_size = buf_size;
1116         return 1;
1117 }
1118 __setup("trace_buf_size=", set_buf_size);
1119
1120 static int __init set_tracing_thresh(char *str)
1121 {
1122         unsigned long threshold;
1123         int ret;
1124
1125         if (!str)
1126                 return 0;
1127         ret = kstrtoul(str, 0, &threshold);
1128         if (ret < 0)
1129                 return 0;
1130         tracing_thresh = threshold * 1000;
1131         return 1;
1132 }
1133 __setup("tracing_thresh=", set_tracing_thresh);
1134
1135 unsigned long nsecs_to_usecs(unsigned long nsecs)
1136 {
1137         return nsecs / 1000;
1138 }
1139
1140 /*
1141  * TRACE_FLAGS is defined as a tuple matching bit masks with strings.
1142  * It uses C(a, b) where 'a' is the eval (enum) name and 'b' is the string that
1143  * matches it. By defining "C(a, b) b", TRACE_FLAGS becomes a list
1144  * of strings in the order that the evals (enum) were defined.
1145  */
1146 #undef C
1147 #define C(a, b) b
1148
1149 /* These must match the bit postions in trace_iterator_flags */
1150 static const char *trace_options[] = {
1151         TRACE_FLAGS
1152         NULL
1153 };
1154
1155 static struct {
1156         u64 (*func)(void);
1157         const char *name;
1158         int in_ns;              /* is this clock in nanoseconds? */
1159 } trace_clocks[] = {
1160         { trace_clock_local,            "local",        1 },
1161         { trace_clock_global,           "global",       1 },
1162         { trace_clock_counter,          "counter",      0 },
1163         { trace_clock_jiffies,          "uptime",       0 },
1164         { trace_clock,                  "perf",         1 },
1165         { ktime_get_mono_fast_ns,       "mono",         1 },
1166         { ktime_get_raw_fast_ns,        "mono_raw",     1 },
1167         { ktime_get_mono_fast_ns,       "boot",         1 },
1168         ARCH_TRACE_CLOCKS
1169 };
1170
1171 /*
1172  * trace_parser_get_init - gets the buffer for trace parser
1173  */
1174 int trace_parser_get_init(struct trace_parser *parser, int size)
1175 {
1176         memset(parser, 0, sizeof(*parser));
1177
1178         parser->buffer = kmalloc(size, GFP_KERNEL);
1179         if (!parser->buffer)
1180                 return 1;
1181
1182         parser->size = size;
1183         return 0;
1184 }
1185
1186 /*
1187  * trace_parser_put - frees the buffer for trace parser
1188  */
1189 void trace_parser_put(struct trace_parser *parser)
1190 {
1191         kfree(parser->buffer);
1192         parser->buffer = NULL;
1193 }
1194
1195 /*
1196  * trace_get_user - reads the user input string separated by  space
1197  * (matched by isspace(ch))
1198  *
1199  * For each string found the 'struct trace_parser' is updated,
1200  * and the function returns.
1201  *
1202  * Returns number of bytes read.
1203  *
1204  * See kernel/trace/trace.h for 'struct trace_parser' details.
1205  */
1206 int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
1207         size_t cnt, loff_t *ppos)
1208 {
1209         char ch;
1210         size_t read = 0;
1211         ssize_t ret;
1212
1213         if (!*ppos)
1214                 trace_parser_clear(parser);
1215
1216         ret = get_user(ch, ubuf++);
1217         if (ret)
1218                 goto out;
1219
1220         read++;
1221         cnt--;
1222
1223         /*
1224          * The parser is not finished with the last write,
1225          * continue reading the user input without skipping spaces.
1226          */
1227         if (!parser->cont) {
1228                 /* skip white space */
1229                 while (cnt && isspace(ch)) {
1230                         ret = get_user(ch, ubuf++);
1231                         if (ret)
1232                                 goto out;
1233                         read++;
1234                         cnt--;
1235                 }
1236
1237                 parser->idx = 0;
1238
1239                 /* only spaces were written */
1240                 if (isspace(ch) || !ch) {
1241                         *ppos += read;
1242                         ret = read;
1243                         goto out;
1244                 }
1245         }
1246
1247         /* read the non-space input */
1248         while (cnt && !isspace(ch) && ch) {
1249                 if (parser->idx < parser->size - 1)
1250                         parser->buffer[parser->idx++] = ch;
1251                 else {
1252                         ret = -EINVAL;
1253                         goto out;
1254                 }
1255                 ret = get_user(ch, ubuf++);
1256                 if (ret)
1257                         goto out;
1258                 read++;
1259                 cnt--;
1260         }
1261
1262         /* We either got finished input or we have to wait for another call. */
1263         if (isspace(ch) || !ch) {
1264                 parser->buffer[parser->idx] = 0;
1265                 parser->cont = false;
1266         } else if (parser->idx < parser->size - 1) {
1267                 parser->cont = true;
1268                 parser->buffer[parser->idx++] = ch;
1269                 /* Make sure the parsed string always terminates with '\0'. */
1270                 parser->buffer[parser->idx] = 0;
1271         } else {
1272                 ret = -EINVAL;
1273                 goto out;
1274         }
1275
1276         *ppos += read;
1277         ret = read;
1278
1279 out:
1280         return ret;
1281 }
1282
1283 /* TODO add a seq_buf_to_buffer() */
1284 static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
1285 {
1286         int len;
1287
1288         if (trace_seq_used(s) <= s->seq.readpos)
1289                 return -EBUSY;
1290
1291         len = trace_seq_used(s) - s->seq.readpos;
1292         if (cnt > len)
1293                 cnt = len;
1294         memcpy(buf, s->buffer + s->seq.readpos, cnt);
1295
1296         s->seq.readpos += cnt;
1297         return cnt;
1298 }
1299
1300 unsigned long __read_mostly     tracing_thresh;
1301
1302 #ifdef CONFIG_TRACER_MAX_TRACE
1303 /*
1304  * Copy the new maximum trace into the separate maximum-trace
1305  * structure. (this way the maximum trace is permanently saved,
1306  * for later retrieval via /sys/kernel/tracing/tracing_max_latency)
1307  */
1308 static void
1309 __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
1310 {
1311         struct trace_buffer *trace_buf = &tr->trace_buffer;
1312         struct trace_buffer *max_buf = &tr->max_buffer;
1313         struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
1314         struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
1315
1316         max_buf->cpu = cpu;
1317         max_buf->time_start = data->preempt_timestamp;
1318
1319         max_data->saved_latency = tr->max_latency;
1320         max_data->critical_start = data->critical_start;
1321         max_data->critical_end = data->critical_end;
1322
1323         memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
1324         max_data->pid = tsk->pid;
1325         /*
1326          * If tsk == current, then use current_uid(), as that does not use
1327          * RCU. The irq tracer can be called out of RCU scope.
1328          */
1329         if (tsk == current)
1330                 max_data->uid = current_uid();
1331         else
1332                 max_data->uid = task_uid(tsk);
1333
1334         max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
1335         max_data->policy = tsk->policy;
1336         max_data->rt_priority = tsk->rt_priority;
1337
1338         /* record this tasks comm */
1339         tracing_record_cmdline(tsk);
1340 }
1341
1342 /**
1343  * update_max_tr - snapshot all trace buffers from global_trace to max_tr
1344  * @tr: tracer
1345  * @tsk: the task with the latency
1346  * @cpu: The cpu that initiated the trace.
1347  *
1348  * Flip the buffers between the @tr and the max_tr and record information
1349  * about which task was the cause of this latency.
1350  */
1351 void
1352 update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
1353 {
1354         struct ring_buffer *buf;
1355
1356         if (tr->stop_count)
1357                 return;
1358
1359         WARN_ON_ONCE(!irqs_disabled());
1360
1361         if (!tr->allocated_snapshot) {
1362                 /* Only the nop tracer should hit this when disabling */
1363                 WARN_ON_ONCE(tr->current_trace != &nop_trace);
1364                 return;
1365         }
1366
1367         arch_spin_lock(&tr->max_lock);
1368
1369         buf = tr->trace_buffer.buffer;
1370         tr->trace_buffer.buffer = tr->max_buffer.buffer;
1371         tr->max_buffer.buffer = buf;
1372
1373         __update_max_tr(tr, tsk, cpu);
1374         arch_spin_unlock(&tr->max_lock);
1375 }
1376
1377 /**
1378  * update_max_tr_single - only copy one trace over, and reset the rest
1379  * @tr - tracer
1380  * @tsk - task with the latency
1381  * @cpu - the cpu of the buffer to copy.
1382  *
1383  * Flip the trace of a single CPU buffer between the @tr and the max_tr.
1384  */
1385 void
1386 update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
1387 {
1388         int ret;
1389
1390         if (tr->stop_count)
1391                 return;
1392
1393         WARN_ON_ONCE(!irqs_disabled());
1394         if (!tr->allocated_snapshot) {
1395                 /* Only the nop tracer should hit this when disabling */
1396                 WARN_ON_ONCE(tr->current_trace != &nop_trace);
1397                 return;
1398         }
1399
1400         arch_spin_lock(&tr->max_lock);
1401
1402         ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
1403
1404         if (ret == -EBUSY) {
1405                 /*
1406                  * We failed to swap the buffer due to a commit taking
1407                  * place on this CPU. We fail to record, but we reset
1408                  * the max trace buffer (no one writes directly to it)
1409                  * and flag that it failed.
1410                  */
1411                 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
1412                         "Failed to swap buffers due to commit in progress\n");
1413         }
1414
1415         WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
1416
1417         __update_max_tr(tr, tsk, cpu);
1418         arch_spin_unlock(&tr->max_lock);
1419 }
1420 #endif /* CONFIG_TRACER_MAX_TRACE */
1421
1422 static int wait_on_pipe(struct trace_iterator *iter, bool full)
1423 {
1424         /* Iterators are static, they should be filled or empty */
1425         if (trace_buffer_iter(iter, iter->cpu_file))
1426                 return 0;
1427
1428         return ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file,
1429                                 full);
1430 }
1431
1432 #ifdef CONFIG_FTRACE_STARTUP_TEST
1433 static bool selftests_can_run;
1434
1435 struct trace_selftests {
1436         struct list_head                list;
1437         struct tracer                   *type;
1438 };
1439
1440 static LIST_HEAD(postponed_selftests);
1441
1442 static int save_selftest(struct tracer *type)
1443 {
1444         struct trace_selftests *selftest;
1445
1446         selftest = kmalloc(sizeof(*selftest), GFP_KERNEL);
1447         if (!selftest)
1448                 return -ENOMEM;
1449
1450         selftest->type = type;
1451         list_add(&selftest->list, &postponed_selftests);
1452         return 0;
1453 }
1454
1455 static int run_tracer_selftest(struct tracer *type)
1456 {
1457         struct trace_array *tr = &global_trace;
1458         struct tracer *saved_tracer = tr->current_trace;
1459         int ret;
1460
1461         if (!type->selftest || tracing_selftest_disabled)
1462                 return 0;
1463
1464         /*
1465          * If a tracer registers early in boot up (before scheduling is
1466          * initialized and such), then do not run its selftests yet.
1467          * Instead, run it a little later in the boot process.
1468          */
1469         if (!selftests_can_run)
1470                 return save_selftest(type);
1471
1472         /*
1473          * Run a selftest on this tracer.
1474          * Here we reset the trace buffer, and set the current
1475          * tracer to be this tracer. The tracer can then run some
1476          * internal tracing to verify that everything is in order.
1477          * If we fail, we do not register this tracer.
1478          */
1479         tracing_reset_online_cpus(&tr->trace_buffer);
1480
1481         tr->current_trace = type;
1482
1483 #ifdef CONFIG_TRACER_MAX_TRACE
1484         if (type->use_max_tr) {
1485                 /* If we expanded the buffers, make sure the max is expanded too */
1486                 if (ring_buffer_expanded)
1487                         ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
1488                                            RING_BUFFER_ALL_CPUS);
1489                 tr->allocated_snapshot = true;
1490         }
1491 #endif
1492
1493         /* the test is responsible for initializing and enabling */
1494         pr_info("Testing tracer %s: ", type->name);
1495         ret = type->selftest(type, tr);
1496         /* the test is responsible for resetting too */
1497         tr->current_trace = saved_tracer;
1498         if (ret) {
1499                 printk(KERN_CONT "FAILED!\n");
1500                 /* Add the warning after printing 'FAILED' */
1501                 WARN_ON(1);
1502                 return -1;
1503         }
1504         /* Only reset on passing, to avoid touching corrupted buffers */
1505         tracing_reset_online_cpus(&tr->trace_buffer);
1506
1507 #ifdef CONFIG_TRACER_MAX_TRACE
1508         if (type->use_max_tr) {
1509                 tr->allocated_snapshot = false;
1510
1511                 /* Shrink the max buffer again */
1512                 if (ring_buffer_expanded)
1513                         ring_buffer_resize(tr->max_buffer.buffer, 1,
1514                                            RING_BUFFER_ALL_CPUS);
1515         }
1516 #endif
1517
1518         printk(KERN_CONT "PASSED\n");
1519         return 0;
1520 }
1521
1522 static __init int init_trace_selftests(void)
1523 {
1524         struct trace_selftests *p, *n;
1525         struct tracer *t, **last;
1526         int ret;
1527
1528         selftests_can_run = true;
1529
1530         mutex_lock(&trace_types_lock);
1531
1532         if (list_empty(&postponed_selftests))
1533                 goto out;
1534
1535         pr_info("Running postponed tracer tests:\n");
1536
1537         list_for_each_entry_safe(p, n, &postponed_selftests, list) {
1538                 ret = run_tracer_selftest(p->type);
1539                 /* If the test fails, then warn and remove from available_tracers */
1540                 if (ret < 0) {
1541                         WARN(1, "tracer: %s failed selftest, disabling\n",
1542                              p->type->name);
1543                         last = &trace_types;
1544                         for (t = trace_types; t; t = t->next) {
1545                                 if (t == p->type) {
1546                                         *last = t->next;
1547                                         break;
1548                                 }
1549                                 last = &t->next;
1550                         }
1551                 }
1552                 list_del(&p->list);
1553                 kfree(p);
1554         }
1555
1556  out:
1557         mutex_unlock(&trace_types_lock);
1558
1559         return 0;
1560 }
1561 core_initcall(init_trace_selftests);
1562 #else
1563 static inline int run_tracer_selftest(struct tracer *type)
1564 {
1565         return 0;
1566 }
1567 #endif /* CONFIG_FTRACE_STARTUP_TEST */
1568
1569 static void add_tracer_options(struct trace_array *tr, struct tracer *t);
1570
1571 static void __init apply_trace_boot_options(void);
1572
1573 /**
1574  * register_tracer - register a tracer with the ftrace system.
1575  * @type - the plugin for the tracer
1576  *
1577  * Register a new plugin tracer.
1578  */
1579 int __init register_tracer(struct tracer *type)
1580 {
1581         struct tracer *t;
1582         int ret = 0;
1583
1584         if (!type->name) {
1585                 pr_info("Tracer must have a name\n");
1586                 return -1;
1587         }
1588
1589         if (strlen(type->name) >= MAX_TRACER_SIZE) {
1590                 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
1591                 return -1;
1592         }
1593
1594         mutex_lock(&trace_types_lock);
1595
1596         tracing_selftest_running = true;
1597
1598         for (t = trace_types; t; t = t->next) {
1599                 if (strcmp(type->name, t->name) == 0) {
1600                         /* already found */
1601                         pr_info("Tracer %s already registered\n",
1602                                 type->name);
1603                         ret = -1;
1604                         goto out;
1605                 }
1606         }
1607
1608         if (!type->set_flag)
1609                 type->set_flag = &dummy_set_flag;
1610         if (!type->flags) {
1611                 /*allocate a dummy tracer_flags*/
1612                 type->flags = kmalloc(sizeof(*type->flags), GFP_KERNEL);
1613                 if (!type->flags) {
1614                         ret = -ENOMEM;
1615                         goto out;
1616                 }
1617                 type->flags->val = 0;
1618                 type->flags->opts = dummy_tracer_opt;
1619         } else
1620                 if (!type->flags->opts)
1621                         type->flags->opts = dummy_tracer_opt;
1622
1623         /* store the tracer for __set_tracer_option */
1624         type->flags->trace = type;
1625
1626         ret = run_tracer_selftest(type);
1627         if (ret < 0)
1628                 goto out;
1629
1630         type->next = trace_types;
1631         trace_types = type;
1632         add_tracer_options(&global_trace, type);
1633
1634  out:
1635         tracing_selftest_running = false;
1636         mutex_unlock(&trace_types_lock);
1637
1638         if (ret || !default_bootup_tracer)
1639                 goto out_unlock;
1640
1641         if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
1642                 goto out_unlock;
1643
1644         printk(KERN_INFO "Starting tracer '%s'\n", type->name);
1645         /* Do we want this tracer to start on bootup? */
1646         tracing_set_tracer(&global_trace, type->name);
1647         default_bootup_tracer = NULL;
1648
1649         apply_trace_boot_options();
1650
1651         /* disable other selftests, since this will break it. */
1652         tracing_selftest_disabled = true;
1653 #ifdef CONFIG_FTRACE_STARTUP_TEST
1654         printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
1655                type->name);
1656 #endif
1657
1658  out_unlock:
1659         return ret;
1660 }
1661
1662 void tracing_reset(struct trace_buffer *buf, int cpu)
1663 {
1664         struct ring_buffer *buffer = buf->buffer;
1665
1666         if (!buffer)
1667                 return;
1668
1669         ring_buffer_record_disable(buffer);
1670
1671         /* Make sure all commits have finished */
1672         synchronize_sched();
1673         ring_buffer_reset_cpu(buffer, cpu);
1674
1675         ring_buffer_record_enable(buffer);
1676 }
1677
1678 void tracing_reset_online_cpus(struct trace_buffer *buf)
1679 {
1680         struct ring_buffer *buffer = buf->buffer;
1681         int cpu;
1682
1683         if (!buffer)
1684                 return;
1685
1686         ring_buffer_record_disable(buffer);
1687
1688         /* Make sure all commits have finished */
1689         synchronize_sched();
1690
1691         buf->time_start = buffer_ftrace_now(buf, buf->cpu);
1692
1693         for_each_online_cpu(cpu)
1694                 ring_buffer_reset_cpu(buffer, cpu);
1695
1696         ring_buffer_record_enable(buffer);
1697 }
1698
1699 /* Must have trace_types_lock held */
1700 void tracing_reset_all_online_cpus(void)
1701 {
1702         struct trace_array *tr;
1703
1704         list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1705                 if (!tr->clear_trace)
1706                         continue;
1707                 tr->clear_trace = false;
1708                 tracing_reset_online_cpus(&tr->trace_buffer);
1709 #ifdef CONFIG_TRACER_MAX_TRACE
1710                 tracing_reset_online_cpus(&tr->max_buffer);
1711 #endif
1712         }
1713 }
1714
1715 static int *tgid_map;
1716
1717 #define SAVED_CMDLINES_DEFAULT 128
1718 #define NO_CMDLINE_MAP UINT_MAX
1719 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
1720 struct saved_cmdlines_buffer {
1721         unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
1722         unsigned *map_cmdline_to_pid;
1723         unsigned cmdline_num;
1724         int cmdline_idx;
1725         char *saved_cmdlines;
1726 };
1727 static struct saved_cmdlines_buffer *savedcmd;
1728
1729 /* temporary disable recording */
1730 static atomic_t trace_record_taskinfo_disabled __read_mostly;
1731
1732 static inline char *get_saved_cmdlines(int idx)
1733 {
1734         return &savedcmd->saved_cmdlines[idx * TASK_COMM_LEN];
1735 }
1736
1737 static inline void set_cmdline(int idx, const char *cmdline)
1738 {
1739         memcpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN);
1740 }
1741
1742 static int allocate_cmdlines_buffer(unsigned int val,
1743                                     struct saved_cmdlines_buffer *s)
1744 {
1745         s->map_cmdline_to_pid = kmalloc(val * sizeof(*s->map_cmdline_to_pid),
1746                                         GFP_KERNEL);
1747         if (!s->map_cmdline_to_pid)
1748                 return -ENOMEM;
1749
1750         s->saved_cmdlines = kmalloc(val * TASK_COMM_LEN, GFP_KERNEL);
1751         if (!s->saved_cmdlines) {
1752                 kfree(s->map_cmdline_to_pid);
1753                 return -ENOMEM;
1754         }
1755
1756         s->cmdline_idx = 0;
1757         s->cmdline_num = val;
1758         memset(&s->map_pid_to_cmdline, NO_CMDLINE_MAP,
1759                sizeof(s->map_pid_to_cmdline));
1760         memset(s->map_cmdline_to_pid, NO_CMDLINE_MAP,
1761                val * sizeof(*s->map_cmdline_to_pid));
1762
1763         return 0;
1764 }
1765
1766 static int trace_create_savedcmd(void)
1767 {
1768         int ret;
1769
1770         savedcmd = kmalloc(sizeof(*savedcmd), GFP_KERNEL);
1771         if (!savedcmd)
1772                 return -ENOMEM;
1773
1774         ret = allocate_cmdlines_buffer(SAVED_CMDLINES_DEFAULT, savedcmd);
1775         if (ret < 0) {
1776                 kfree(savedcmd);
1777                 savedcmd = NULL;
1778                 return -ENOMEM;
1779         }
1780
1781         return 0;
1782 }
1783
1784 int is_tracing_stopped(void)
1785 {
1786         return global_trace.stop_count;
1787 }
1788
1789 /**
1790  * tracing_start - quick start of the tracer
1791  *
1792  * If tracing is enabled but was stopped by tracing_stop,
1793  * this will start the tracer back up.
1794  */
1795 void tracing_start(void)
1796 {
1797         struct ring_buffer *buffer;
1798         unsigned long flags;
1799
1800         if (tracing_disabled)
1801                 return;
1802
1803         raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1804         if (--global_trace.stop_count) {
1805                 if (global_trace.stop_count < 0) {
1806                         /* Someone screwed up their debugging */
1807                         WARN_ON_ONCE(1);
1808                         global_trace.stop_count = 0;
1809                 }
1810                 goto out;
1811         }
1812
1813         /* Prevent the buffers from switching */
1814         arch_spin_lock(&global_trace.max_lock);
1815
1816         buffer = global_trace.trace_buffer.buffer;
1817         if (buffer)
1818                 ring_buffer_record_enable(buffer);
1819
1820 #ifdef CONFIG_TRACER_MAX_TRACE
1821         buffer = global_trace.max_buffer.buffer;
1822         if (buffer)
1823                 ring_buffer_record_enable(buffer);
1824 #endif
1825
1826         arch_spin_unlock(&global_trace.max_lock);
1827
1828  out:
1829         raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1830 }
1831
1832 static void tracing_start_tr(struct trace_array *tr)
1833 {
1834         struct ring_buffer *buffer;
1835         unsigned long flags;
1836
1837         if (tracing_disabled)
1838                 return;
1839
1840         /* If global, we need to also start the max tracer */
1841         if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1842                 return tracing_start();
1843
1844         raw_spin_lock_irqsave(&tr->start_lock, flags);
1845
1846         if (--tr->stop_count) {
1847                 if (tr->stop_count < 0) {
1848                         /* Someone screwed up their debugging */
1849                         WARN_ON_ONCE(1);
1850                         tr->stop_count = 0;
1851                 }
1852                 goto out;
1853         }
1854
1855         buffer = tr->trace_buffer.buffer;
1856         if (buffer)
1857                 ring_buffer_record_enable(buffer);
1858
1859  out:
1860         raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1861 }
1862
1863 /**
1864  * tracing_stop - quick stop of the tracer
1865  *
1866  * Light weight way to stop tracing. Use in conjunction with
1867  * tracing_start.
1868  */
1869 void tracing_stop(void)
1870 {
1871         struct ring_buffer *buffer;
1872         unsigned long flags;
1873
1874         raw_spin_lock_irqsave(&global_trace.start_lock, flags);
1875         if (global_trace.stop_count++)
1876                 goto out;
1877
1878         /* Prevent the buffers from switching */
1879         arch_spin_lock(&global_trace.max_lock);
1880
1881         buffer = global_trace.trace_buffer.buffer;
1882         if (buffer)
1883                 ring_buffer_record_disable(buffer);
1884
1885 #ifdef CONFIG_TRACER_MAX_TRACE
1886         buffer = global_trace.max_buffer.buffer;
1887         if (buffer)
1888                 ring_buffer_record_disable(buffer);
1889 #endif
1890
1891         arch_spin_unlock(&global_trace.max_lock);
1892
1893  out:
1894         raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
1895 }
1896
1897 static void tracing_stop_tr(struct trace_array *tr)
1898 {
1899         struct ring_buffer *buffer;
1900         unsigned long flags;
1901
1902         /* If global, we need to also stop the max tracer */
1903         if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
1904                 return tracing_stop();
1905
1906         raw_spin_lock_irqsave(&tr->start_lock, flags);
1907         if (tr->stop_count++)
1908                 goto out;
1909
1910         buffer = tr->trace_buffer.buffer;
1911         if (buffer)
1912                 ring_buffer_record_disable(buffer);
1913
1914  out:
1915         raw_spin_unlock_irqrestore(&tr->start_lock, flags);
1916 }
1917
1918 static int trace_save_cmdline(struct task_struct *tsk)
1919 {
1920         unsigned pid, idx;
1921
1922         /* treat recording of idle task as a success */
1923         if (!tsk->pid)
1924                 return 1;
1925
1926         if (unlikely(tsk->pid > PID_MAX_DEFAULT))
1927                 return 0;
1928
1929         /*
1930          * It's not the end of the world if we don't get
1931          * the lock, but we also don't want to spin
1932          * nor do we want to disable interrupts,
1933          * so if we miss here, then better luck next time.
1934          */
1935         if (!arch_spin_trylock(&trace_cmdline_lock))
1936                 return 0;
1937
1938         idx = savedcmd->map_pid_to_cmdline[tsk->pid];
1939         if (idx == NO_CMDLINE_MAP) {
1940                 idx = (savedcmd->cmdline_idx + 1) % savedcmd->cmdline_num;
1941
1942                 /*
1943                  * Check whether the cmdline buffer at idx has a pid
1944                  * mapped. We are going to overwrite that entry so we
1945                  * need to clear the map_pid_to_cmdline. Otherwise we
1946                  * would read the new comm for the old pid.
1947                  */
1948                 pid = savedcmd->map_cmdline_to_pid[idx];
1949                 if (pid != NO_CMDLINE_MAP)
1950                         savedcmd->map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
1951
1952                 savedcmd->map_cmdline_to_pid[idx] = tsk->pid;
1953                 savedcmd->map_pid_to_cmdline[tsk->pid] = idx;
1954
1955                 savedcmd->cmdline_idx = idx;
1956         }
1957
1958         set_cmdline(idx, tsk->comm);
1959
1960         arch_spin_unlock(&trace_cmdline_lock);
1961
1962         return 1;
1963 }
1964
1965 static void __trace_find_cmdline(int pid, char comm[])
1966 {
1967         unsigned map;
1968
1969         if (!pid) {
1970                 strcpy(comm, "<idle>");
1971                 return;
1972         }
1973
1974         if (WARN_ON_ONCE(pid < 0)) {
1975                 strcpy(comm, "<XXX>");
1976                 return;
1977         }
1978
1979         if (pid > PID_MAX_DEFAULT) {
1980                 strcpy(comm, "<...>");
1981                 return;
1982         }
1983
1984         map = savedcmd->map_pid_to_cmdline[pid];
1985         if (map != NO_CMDLINE_MAP)
1986                 strlcpy(comm, get_saved_cmdlines(map), TASK_COMM_LEN);
1987         else
1988                 strcpy(comm, "<...>");
1989 }
1990
1991 void trace_find_cmdline(int pid, char comm[])
1992 {
1993         preempt_disable();
1994         arch_spin_lock(&trace_cmdline_lock);
1995
1996         __trace_find_cmdline(pid, comm);
1997
1998         arch_spin_unlock(&trace_cmdline_lock);
1999         preempt_enable();
2000 }
2001
2002 int trace_find_tgid(int pid)
2003 {
2004         if (unlikely(!tgid_map || !pid || pid > PID_MAX_DEFAULT))
2005                 return 0;
2006
2007         return tgid_map[pid];
2008 }
2009
2010 static int trace_save_tgid(struct task_struct *tsk)
2011 {
2012         /* treat recording of idle task as a success */
2013         if (!tsk->pid)
2014                 return 1;
2015
2016         if (unlikely(!tgid_map || tsk->pid > PID_MAX_DEFAULT))
2017                 return 0;
2018
2019         tgid_map[tsk->pid] = tsk->tgid;
2020         return 1;
2021 }
2022
2023 static bool tracing_record_taskinfo_skip(int flags)
2024 {
2025         if (unlikely(!(flags & (TRACE_RECORD_CMDLINE | TRACE_RECORD_TGID))))
2026                 return true;
2027         if (atomic_read(&trace_record_taskinfo_disabled) || !tracing_is_on())
2028                 return true;
2029         if (!__this_cpu_read(trace_taskinfo_save))
2030                 return true;
2031         return false;
2032 }
2033
2034 /**
2035  * tracing_record_taskinfo - record the task info of a task
2036  *
2037  * @task  - task to record
2038  * @flags - TRACE_RECORD_CMDLINE for recording comm
2039  *        - TRACE_RECORD_TGID for recording tgid
2040  */
2041 void tracing_record_taskinfo(struct task_struct *task, int flags)
2042 {
2043         bool done;
2044
2045         if (tracing_record_taskinfo_skip(flags))
2046                 return;
2047
2048         /*
2049          * Record as much task information as possible. If some fail, continue
2050          * to try to record the others.
2051          */
2052         done = !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(task);
2053         done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(task);
2054
2055         /* If recording any information failed, retry again soon. */
2056         if (!done)
2057                 return;
2058
2059         __this_cpu_write(trace_taskinfo_save, false);
2060 }
2061
2062 /**
2063  * tracing_record_taskinfo_sched_switch - record task info for sched_switch
2064  *
2065  * @prev - previous task during sched_switch
2066  * @next - next task during sched_switch
2067  * @flags - TRACE_RECORD_CMDLINE for recording comm
2068  *          TRACE_RECORD_TGID for recording tgid
2069  */
2070 void tracing_record_taskinfo_sched_switch(struct task_struct *prev,
2071                                           struct task_struct *next, int flags)
2072 {
2073         bool done;
2074
2075         if (tracing_record_taskinfo_skip(flags))
2076                 return;
2077
2078         /*
2079          * Record as much task information as possible. If some fail, continue
2080          * to try to record the others.
2081          */
2082         done  = !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(prev);
2083         done &= !(flags & TRACE_RECORD_CMDLINE) || trace_save_cmdline(next);
2084         done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(prev);
2085         done &= !(flags & TRACE_RECORD_TGID) || trace_save_tgid(next);
2086
2087         /* If recording any information failed, retry again soon. */
2088         if (!done)
2089                 return;
2090
2091         __this_cpu_write(trace_taskinfo_save, false);
2092 }
2093
2094 /* Helpers to record a specific task information */
2095 void tracing_record_cmdline(struct task_struct *task)
2096 {
2097         tracing_record_taskinfo(task, TRACE_RECORD_CMDLINE);
2098 }
2099
2100 void tracing_record_tgid(struct task_struct *task)
2101 {
2102         tracing_record_taskinfo(task, TRACE_RECORD_TGID);
2103 }
2104
2105 /*
2106  * Several functions return TRACE_TYPE_PARTIAL_LINE if the trace_seq
2107  * overflowed, and TRACE_TYPE_HANDLED otherwise. This helper function
2108  * simplifies those functions and keeps them in sync.
2109  */
2110 enum print_line_t trace_handle_return(struct trace_seq *s)
2111 {
2112         return trace_seq_has_overflowed(s) ?
2113                 TRACE_TYPE_PARTIAL_LINE : TRACE_TYPE_HANDLED;
2114 }
2115 EXPORT_SYMBOL_GPL(trace_handle_return);
2116
2117 void
2118 tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
2119                              int pc)
2120 {
2121         struct task_struct *tsk = current;
2122
2123         entry->preempt_count            = pc & 0xff;
2124         entry->pid                      = (tsk) ? tsk->pid : 0;
2125         entry->flags =
2126 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
2127                 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
2128 #else
2129                 TRACE_FLAG_IRQS_NOSUPPORT |
2130 #endif
2131                 ((pc & NMI_MASK    ) ? TRACE_FLAG_NMI     : 0) |
2132                 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
2133                 ((pc & SOFTIRQ_OFFSET) ? TRACE_FLAG_SOFTIRQ : 0) |
2134                 (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) |
2135                 (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0);
2136 }
2137 EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
2138
2139 struct ring_buffer_event *
2140 trace_buffer_lock_reserve(struct ring_buffer *buffer,
2141                           int type,
2142                           unsigned long len,
2143                           unsigned long flags, int pc)
2144 {
2145         return __trace_buffer_lock_reserve(buffer, type, len, flags, pc);
2146 }
2147
2148 DEFINE_PER_CPU(struct ring_buffer_event *, trace_buffered_event);
2149 DEFINE_PER_CPU(int, trace_buffered_event_cnt);
2150 static int trace_buffered_event_ref;
2151
2152 /**
2153  * trace_buffered_event_enable - enable buffering events
2154  *
2155  * When events are being filtered, it is quicker to use a temporary
2156  * buffer to write the event data into if there's a likely chance
2157  * that it will not be committed. The discard of the ring buffer
2158  * is not as fast as committing, and is much slower than copying
2159  * a commit.
2160  *
2161  * When an event is to be filtered, allocate per cpu buffers to
2162  * write the event data into, and if the event is filtered and discarded
2163  * it is simply dropped, otherwise, the entire data is to be committed
2164  * in one shot.
2165  */
2166 void trace_buffered_event_enable(void)
2167 {
2168         struct ring_buffer_event *event;
2169         struct page *page;
2170         int cpu;
2171
2172         WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
2173
2174         if (trace_buffered_event_ref++)
2175                 return;
2176
2177         for_each_tracing_cpu(cpu) {
2178                 page = alloc_pages_node(cpu_to_node(cpu),
2179                                         GFP_KERNEL | __GFP_NORETRY, 0);
2180                 if (!page)
2181                         goto failed;
2182
2183                 event = page_address(page);
2184                 memset(event, 0, sizeof(*event));
2185
2186                 per_cpu(trace_buffered_event, cpu) = event;
2187
2188                 preempt_disable();
2189                 if (cpu == smp_processor_id() &&
2190                     this_cpu_read(trace_buffered_event) !=
2191                     per_cpu(trace_buffered_event, cpu))
2192                         WARN_ON_ONCE(1);
2193                 preempt_enable();
2194         }
2195
2196         return;
2197  failed:
2198         trace_buffered_event_disable();
2199 }
2200
2201 static void enable_trace_buffered_event(void *data)
2202 {
2203         /* Probably not needed, but do it anyway */
2204         smp_rmb();
2205         this_cpu_dec(trace_buffered_event_cnt);
2206 }
2207
2208 static void disable_trace_buffered_event(void *data)
2209 {
2210         this_cpu_inc(trace_buffered_event_cnt);
2211 }
2212
2213 /**
2214  * trace_buffered_event_disable - disable buffering events
2215  *
2216  * When a filter is removed, it is faster to not use the buffered
2217  * events, and to commit directly into the ring buffer. Free up
2218  * the temp buffers when there are no more users. This requires
2219  * special synchronization with current events.
2220  */
2221 void trace_buffered_event_disable(void)
2222 {
2223         int cpu;
2224
2225         WARN_ON_ONCE(!mutex_is_locked(&event_mutex));
2226
2227         if (WARN_ON_ONCE(!trace_buffered_event_ref))
2228                 return;
2229
2230         if (--trace_buffered_event_ref)
2231                 return;
2232
2233         preempt_disable();
2234         /* For each CPU, set the buffer as used. */
2235         smp_call_function_many(tracing_buffer_mask,
2236                                disable_trace_buffered_event, NULL, 1);
2237         preempt_enable();
2238
2239         /* Wait for all current users to finish */
2240         synchronize_sched();
2241
2242         for_each_tracing_cpu(cpu) {
2243                 free_page((unsigned long)per_cpu(trace_buffered_event, cpu));
2244                 per_cpu(trace_buffered_event, cpu) = NULL;
2245         }
2246         /*
2247          * Make sure trace_buffered_event is NULL before clearing
2248          * trace_buffered_event_cnt.
2249          */
2250         smp_wmb();
2251
2252         preempt_disable();
2253         /* Do the work on each cpu */
2254         smp_call_function_many(tracing_buffer_mask,
2255                                enable_trace_buffered_event, NULL, 1);
2256         preempt_enable();
2257 }
2258
2259 static struct ring_buffer *temp_buffer;
2260
2261 struct ring_buffer_event *
2262 trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
2263                           struct trace_event_file *trace_file,
2264                           int type, unsigned long len,
2265                           unsigned long flags, int pc)
2266 {
2267         struct ring_buffer_event *entry;
2268         int val;
2269
2270         *current_rb = trace_file->tr->trace_buffer.buffer;
2271
2272         if ((trace_file->flags &
2273              (EVENT_FILE_FL_SOFT_DISABLED | EVENT_FILE_FL_FILTERED)) &&
2274             (entry = this_cpu_read(trace_buffered_event))) {
2275                 /* Try to use the per cpu buffer first */
2276                 val = this_cpu_inc_return(trace_buffered_event_cnt);
2277                 if (val == 1) {
2278                         trace_event_setup(entry, type, flags, pc);
2279                         entry->array[0] = len;
2280                         return entry;
2281                 }
2282                 this_cpu_dec(trace_buffered_event_cnt);
2283         }
2284
2285         entry = __trace_buffer_lock_reserve(*current_rb,
2286                                             type, len, flags, pc);
2287         /*
2288          * If tracing is off, but we have triggers enabled
2289          * we still need to look at the event data. Use the temp_buffer
2290          * to store the trace event for the tigger to use. It's recusive
2291          * safe and will not be recorded anywhere.
2292          */
2293         if (!entry && trace_file->flags & EVENT_FILE_FL_TRIGGER_COND) {
2294                 *current_rb = temp_buffer;
2295                 entry = __trace_buffer_lock_reserve(*current_rb,
2296                                                     type, len, flags, pc);
2297         }
2298         return entry;
2299 }
2300 EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
2301
2302 static DEFINE_SPINLOCK(tracepoint_iter_lock);
2303 static DEFINE_MUTEX(tracepoint_printk_mutex);
2304
2305 static void output_printk(struct trace_event_buffer *fbuffer)
2306 {
2307         struct trace_event_call *event_call;
2308         struct trace_event *event;
2309         unsigned long flags;
2310         struct trace_iterator *iter = tracepoint_print_iter;
2311
2312         /* We should never get here if iter is NULL */
2313         if (WARN_ON_ONCE(!iter))
2314                 return;
2315
2316         event_call = fbuffer->trace_file->event_call;
2317         if (!event_call || !event_call->event.funcs ||
2318             !event_call->event.funcs->trace)
2319                 return;
2320
2321         event = &fbuffer->trace_file->event_call->event;
2322
2323         spin_lock_irqsave(&tracepoint_iter_lock, flags);
2324         trace_seq_init(&iter->seq);
2325         iter->ent = fbuffer->entry;
2326         event_call->event.funcs->trace(iter, 0, event);
2327         trace_seq_putc(&iter->seq, 0);
2328         printk("%s", iter->seq.buffer);
2329
2330         spin_unlock_irqrestore(&tracepoint_iter_lock, flags);
2331 }
2332
2333 int tracepoint_printk_sysctl(struct ctl_table *table, int write,
2334                              void __user *buffer, size_t *lenp,
2335                              loff_t *ppos)
2336 {
2337         int save_tracepoint_printk;
2338         int ret;
2339
2340         mutex_lock(&tracepoint_printk_mutex);
2341         save_tracepoint_printk = tracepoint_printk;
2342
2343         ret = proc_dointvec(table, write, buffer, lenp, ppos);
2344
2345         /*
2346          * This will force exiting early, as tracepoint_printk
2347          * is always zero when tracepoint_printk_iter is not allocated
2348          */
2349         if (!tracepoint_print_iter)
2350                 tracepoint_printk = 0;
2351
2352         if (save_tracepoint_printk == tracepoint_printk)
2353                 goto out;
2354
2355         if (tracepoint_printk)
2356                 static_key_enable(&tracepoint_printk_key.key);
2357         else
2358                 static_key_disable(&tracepoint_printk_key.key);
2359
2360  out:
2361         mutex_unlock(&tracepoint_printk_mutex);
2362
2363         return ret;
2364 }
2365
2366 void trace_event_buffer_commit(struct trace_event_buffer *fbuffer)
2367 {
2368         if (static_key_false(&tracepoint_printk_key.key))
2369                 output_printk(fbuffer);
2370
2371         event_trigger_unlock_commit(fbuffer->trace_file, fbuffer->buffer,
2372                                     fbuffer->event, fbuffer->entry,
2373                                     fbuffer->flags, fbuffer->pc);
2374 }
2375 EXPORT_SYMBOL_GPL(trace_event_buffer_commit);
2376
2377 /*
2378  * Skip 3:
2379  *
2380  *   trace_buffer_unlock_commit_regs()
2381  *   trace_event_buffer_commit()
2382  *   trace_event_raw_event_xxx()
2383  */
2384 # define STACK_SKIP 3
2385
2386 void trace_buffer_unlock_commit_regs(struct trace_array *tr,
2387                                      struct ring_buffer *buffer,
2388                                      struct ring_buffer_event *event,
2389                                      unsigned long flags, int pc,
2390                                      struct pt_regs *regs)
2391 {
2392         __buffer_unlock_commit(buffer, event);
2393
2394         /*
2395          * If regs is not set, then skip the necessary functions.
2396          * Note, we can still get here via blktrace, wakeup tracer
2397          * and mmiotrace, but that's ok if they lose a function or
2398          * two. They are not that meaningful.
2399          */
2400         ftrace_trace_stack(tr, buffer, flags, regs ? 0 : STACK_SKIP, pc, regs);
2401         ftrace_trace_userstack(buffer, flags, pc);
2402 }
2403
2404 /*
2405  * Similar to trace_buffer_unlock_commit_regs() but do not dump stack.
2406  */
2407 void
2408 trace_buffer_unlock_commit_nostack(struct ring_buffer *buffer,
2409                                    struct ring_buffer_event *event)
2410 {
2411         __buffer_unlock_commit(buffer, event);
2412 }
2413
2414 static void
2415 trace_process_export(struct trace_export *export,
2416                struct ring_buffer_event *event)
2417 {
2418         struct trace_entry *entry;
2419         unsigned int size = 0;
2420
2421         entry = ring_buffer_event_data(event);
2422         size = ring_buffer_event_length(event);
2423         export->write(export, entry, size);
2424 }
2425
2426 static DEFINE_MUTEX(ftrace_export_lock);
2427
2428 static struct trace_export __rcu *ftrace_exports_list __read_mostly;
2429
2430 static DEFINE_STATIC_KEY_FALSE(ftrace_exports_enabled);
2431
2432 static inline void ftrace_exports_enable(void)
2433 {
2434         static_branch_enable(&ftrace_exports_enabled);
2435 }
2436
2437 static inline void ftrace_exports_disable(void)
2438 {
2439         static_branch_disable(&ftrace_exports_enabled);
2440 }
2441
2442 void ftrace_exports(struct ring_buffer_event *event)
2443 {
2444         struct trace_export *export;
2445
2446         preempt_disable_notrace();
2447
2448         export = rcu_dereference_raw_notrace(ftrace_exports_list);
2449         while (export) {
2450                 trace_process_export(export, event);
2451                 export = rcu_dereference_raw_notrace(export->next);
2452         }
2453
2454         preempt_enable_notrace();
2455 }
2456
2457 static inline void
2458 add_trace_export(struct trace_export **list, struct trace_export *export)
2459 {
2460         rcu_assign_pointer(export->next, *list);
2461         /*
2462          * We are entering export into the list but another
2463          * CPU might be walking that list. We need to make sure
2464          * the export->next pointer is valid before another CPU sees
2465          * the export pointer included into the list.
2466          */
2467         rcu_assign_pointer(*list, export);
2468 }
2469
2470 static inline int
2471 rm_trace_export(struct trace_export **list, struct trace_export *export)
2472 {
2473         struct trace_export **p;
2474
2475         for (p = list; *p != NULL; p = &(*p)->next)
2476                 if (*p == export)
2477                         break;
2478
2479         if (*p != export)
2480                 return -1;
2481
2482         rcu_assign_pointer(*p, (*p)->next);
2483
2484         return 0;
2485 }
2486
2487 static inline void
2488 add_ftrace_export(struct trace_export **list, struct trace_export *export)
2489 {
2490         if (*list == NULL)
2491                 ftrace_exports_enable();
2492
2493         add_trace_export(list, export);
2494 }
2495
2496 static inline int
2497 rm_ftrace_export(struct trace_export **list, struct trace_export *export)
2498 {
2499         int ret;
2500
2501         ret = rm_trace_export(list, export);
2502         if (*list == NULL)
2503                 ftrace_exports_disable();
2504
2505         return ret;
2506 }
2507
2508 int register_ftrace_export(struct trace_export *export)
2509 {
2510         if (WARN_ON_ONCE(!export->write))
2511                 return -1;
2512
2513         mutex_lock(&ftrace_export_lock);
2514
2515         add_ftrace_export(&ftrace_exports_list, export);
2516
2517         mutex_unlock(&ftrace_export_lock);
2518
2519         return 0;
2520 }
2521 EXPORT_SYMBOL_GPL(register_ftrace_export);
2522
2523 int unregister_ftrace_export(struct trace_export *export)
2524 {
2525         int ret;
2526
2527         mutex_lock(&ftrace_export_lock);
2528
2529         ret = rm_ftrace_export(&ftrace_exports_list, export);
2530
2531         mutex_unlock(&ftrace_export_lock);
2532
2533         return ret;
2534 }
2535 EXPORT_SYMBOL_GPL(unregister_ftrace_export);
2536
2537 void
2538 trace_function(struct trace_array *tr,
2539                unsigned long ip, unsigned long parent_ip, unsigned long flags,
2540                int pc)
2541 {
2542         struct trace_event_call *call = &event_function;
2543         struct ring_buffer *buffer = tr->trace_buffer.buffer;
2544         struct ring_buffer_event *event;
2545         struct ftrace_entry *entry;
2546
2547         event = __trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
2548                                             flags, pc);
2549         if (!event)
2550                 return;
2551         entry   = ring_buffer_event_data(event);
2552         entry->ip                       = ip;
2553         entry->parent_ip                = parent_ip;
2554
2555         if (!call_filter_check_discard(call, entry, buffer, event)) {
2556                 if (static_branch_unlikely(&ftrace_exports_enabled))
2557                         ftrace_exports(event);
2558                 __buffer_unlock_commit(buffer, event);
2559         }
2560 }
2561
2562 #ifdef CONFIG_STACKTRACE
2563
2564 #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
2565 struct ftrace_stack {
2566         unsigned long           calls[FTRACE_STACK_MAX_ENTRIES];
2567 };
2568
2569 static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
2570 static DEFINE_PER_CPU(int, ftrace_stack_reserve);
2571
2572 static void __ftrace_trace_stack(struct ring_buffer *buffer,
2573                                  unsigned long flags,
2574                                  int skip, int pc, struct pt_regs *regs)
2575 {
2576         struct trace_event_call *call = &event_kernel_stack;
2577         struct ring_buffer_event *event;
2578         struct stack_entry *entry;
2579         struct stack_trace trace;
2580         int use_stack;
2581         int size = FTRACE_STACK_ENTRIES;
2582
2583         trace.nr_entries        = 0;
2584         trace.skip              = skip;
2585
2586         /*
2587          * Add one, for this function and the call to save_stack_trace()
2588          * If regs is set, then these functions will not be in the way.
2589          */
2590 #ifndef CONFIG_UNWINDER_ORC
2591         if (!regs)
2592                 trace.skip++;
2593 #endif
2594
2595         /*
2596          * Since events can happen in NMIs there's no safe way to
2597          * use the per cpu ftrace_stacks. We reserve it and if an interrupt
2598          * or NMI comes in, it will just have to use the default
2599          * FTRACE_STACK_SIZE.
2600          */
2601         preempt_disable_notrace();
2602
2603         use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
2604         /*
2605          * We don't need any atomic variables, just a barrier.
2606          * If an interrupt comes in, we don't care, because it would
2607          * have exited and put the counter back to what we want.
2608          * We just need a barrier to keep gcc from moving things
2609          * around.
2610          */
2611         barrier();
2612         if (use_stack == 1) {
2613                 trace.entries           = this_cpu_ptr(ftrace_stack.calls);
2614                 trace.max_entries       = FTRACE_STACK_MAX_ENTRIES;
2615
2616                 if (regs)
2617                         save_stack_trace_regs(regs, &trace);
2618                 else
2619                         save_stack_trace(&trace);
2620
2621                 if (trace.nr_entries > size)
2622                         size = trace.nr_entries;
2623         } else
2624                 /* From now on, use_stack is a boolean */
2625                 use_stack = 0;
2626
2627         size *= sizeof(unsigned long);
2628
2629         event = __trace_buffer_lock_reserve(buffer, TRACE_STACK,
2630                                             sizeof(*entry) + size, flags, pc);
2631         if (!event)
2632                 goto out;
2633         entry = ring_buffer_event_data(event);
2634
2635         memset(&entry->caller, 0, size);
2636
2637         if (use_stack)
2638                 memcpy(&entry->caller, trace.entries,
2639                        trace.nr_entries * sizeof(unsigned long));
2640         else {
2641                 trace.max_entries       = FTRACE_STACK_ENTRIES;
2642                 trace.entries           = entry->caller;
2643                 if (regs)
2644                         save_stack_trace_regs(regs, &trace);
2645                 else
2646                         save_stack_trace(&trace);
2647         }
2648
2649         entry->size = trace.nr_entries;
2650
2651         if (!call_filter_check_discard(call, entry, buffer, event))
2652                 __buffer_unlock_commit(buffer, event);
2653
2654  out:
2655         /* Again, don't let gcc optimize things here */
2656         barrier();
2657         __this_cpu_dec(ftrace_stack_reserve);
2658         preempt_enable_notrace();
2659
2660 }
2661
2662 static inline void ftrace_trace_stack(struct trace_array *tr,
2663                                       struct ring_buffer *buffer,
2664                                       unsigned long flags,
2665                                       int skip, int pc, struct pt_regs *regs)
2666 {
2667         if (!(tr->trace_flags & TRACE_ITER_STACKTRACE))
2668                 return;
2669
2670         __ftrace_trace_stack(buffer, flags, skip, pc, regs);
2671 }
2672
2673 void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
2674                    int pc)
2675 {
2676         struct ring_buffer *buffer = tr->trace_buffer.buffer;
2677
2678         if (rcu_is_watching()) {
2679                 __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
2680                 return;
2681         }
2682
2683         /*
2684          * When an NMI triggers, RCU is enabled via rcu_nmi_enter(),
2685          * but if the above rcu_is_watching() failed, then the NMI
2686          * triggered someplace critical, and rcu_irq_enter() should
2687          * not be called from NMI.
2688          */
2689         if (unlikely(in_nmi()))
2690                 return;
2691
2692         rcu_irq_enter_irqson();
2693         __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
2694         rcu_irq_exit_irqson();
2695 }
2696
2697 /**
2698  * trace_dump_stack - record a stack back trace in the trace buffer
2699  * @skip: Number of functions to skip (helper handlers)
2700  */
2701 void trace_dump_stack(int skip)
2702 {
2703         unsigned long flags;
2704
2705         if (tracing_disabled || tracing_selftest_running)
2706                 return;
2707
2708         local_save_flags(flags);
2709
2710 #ifndef CONFIG_UNWINDER_ORC
2711         /* Skip 1 to skip this function. */
2712         skip++;
2713 #endif
2714         __ftrace_trace_stack(global_trace.trace_buffer.buffer,
2715                              flags, skip, preempt_count(), NULL);
2716 }
2717
2718 static DEFINE_PER_CPU(int, user_stack_count);
2719
2720 void
2721 ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
2722 {
2723         struct trace_event_call *call = &event_user_stack;
2724         struct ring_buffer_event *event;
2725         struct userstack_entry *entry;
2726         struct stack_trace trace;
2727
2728         if (!(global_trace.trace_flags & TRACE_ITER_USERSTACKTRACE))
2729                 return;
2730
2731         /*
2732          * NMIs can not handle page faults, even with fix ups.
2733          * The save user stack can (and often does) fault.
2734          */
2735         if (unlikely(in_nmi()))
2736                 return;
2737
2738         /*
2739          * prevent recursion, since the user stack tracing may
2740          * trigger other kernel events.
2741          */
2742         preempt_disable();
2743         if (__this_cpu_read(user_stack_count))
2744                 goto out;
2745
2746         __this_cpu_inc(user_stack_count);
2747
2748         event = __trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
2749                                             sizeof(*entry), flags, pc);
2750         if (!event)
2751                 goto out_drop_count;
2752         entry   = ring_buffer_event_data(event);
2753
2754         entry->tgid             = current->tgid;
2755         memset(&entry->caller, 0, sizeof(entry->caller));
2756
2757         trace.nr_entries        = 0;
2758         trace.max_entries       = FTRACE_STACK_ENTRIES;
2759         trace.skip              = 0;
2760         trace.entries           = entry->caller;
2761
2762         save_stack_trace_user(&trace);
2763         if (!call_filter_check_discard(call, entry, buffer, event))
2764                 __buffer_unlock_commit(buffer, event);
2765
2766  out_drop_count:
2767         __this_cpu_dec(user_stack_count);
2768  out:
2769         preempt_enable();
2770 }
2771
2772 #ifdef UNUSED
2773 static void __trace_userstack(struct trace_array *tr, unsigned long flags)
2774 {
2775         ftrace_trace_userstack(tr, flags, preempt_count());
2776 }
2777 #endif /* UNUSED */
2778
2779 #endif /* CONFIG_STACKTRACE */
2780
2781 /* created for use with alloc_percpu */
2782 struct trace_buffer_struct {
2783         int nesting;
2784         char buffer[4][TRACE_BUF_SIZE];
2785 };
2786
2787 static struct trace_buffer_struct *trace_percpu_buffer;
2788
2789 /*
2790  * Thise allows for lockless recording.  If we're nested too deeply, then
2791  * this returns NULL.
2792  */
2793 static char *get_trace_buf(void)
2794 {
2795         struct trace_buffer_struct *buffer = this_cpu_ptr(trace_percpu_buffer);
2796
2797         if (!buffer || buffer->nesting >= 4)
2798                 return NULL;
2799
2800         buffer->nesting++;
2801
2802         /* Interrupts must see nesting incremented before we use the buffer */
2803         barrier();
2804         return &buffer->buffer[buffer->nesting][0];
2805 }
2806
2807 static void put_trace_buf(void)
2808 {
2809         /* Don't let the decrement of nesting leak before this */
2810         barrier();
2811         this_cpu_dec(trace_percpu_buffer->nesting);
2812 }
2813
2814 static int alloc_percpu_trace_buffer(void)
2815 {
2816         struct trace_buffer_struct *buffers;
2817
2818         buffers = alloc_percpu(struct trace_buffer_struct);
2819         if (WARN(!buffers, "Could not allocate percpu trace_printk buffer"))
2820                 return -ENOMEM;
2821
2822         trace_percpu_buffer = buffers;
2823         return 0;
2824 }
2825
2826 static int buffers_allocated;
2827
2828 void trace_printk_init_buffers(void)
2829 {
2830         if (buffers_allocated)
2831                 return;
2832
2833         if (alloc_percpu_trace_buffer())
2834                 return;
2835
2836         /* trace_printk() is for debug use only. Don't use it in production. */
2837
2838         pr_warn("\n");
2839         pr_warn("**********************************************************\n");
2840         pr_warn("**   NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE   **\n");
2841         pr_warn("**                                                      **\n");
2842         pr_warn("** trace_printk() being used. Allocating extra memory.  **\n");
2843         pr_warn("**                                                      **\n");
2844         pr_warn("** This means that this is a DEBUG kernel and it is     **\n");
2845         pr_warn("** unsafe for production use.                           **\n");
2846         pr_warn("**                                                      **\n");
2847         pr_warn("** If you see this message and you are not debugging    **\n");
2848         pr_warn("** the kernel, report this immediately to your vendor!  **\n");
2849         pr_warn("**                                                      **\n");
2850         pr_warn("**   NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE   **\n");
2851         pr_warn("**********************************************************\n");
2852
2853         /* Expand the buffers to set size */
2854         tracing_update_buffers();
2855
2856         buffers_allocated = 1;
2857
2858         /*
2859          * trace_printk_init_buffers() can be called by modules.
2860          * If that happens, then we need to start cmdline recording
2861          * directly here. If the global_trace.buffer is already
2862          * allocated here, then this was called by module code.
2863          */
2864         if (global_trace.trace_buffer.buffer)
2865                 tracing_start_cmdline_record();
2866 }
2867
2868 void trace_printk_start_comm(void)
2869 {
2870         /* Start tracing comms if trace printk is set */
2871         if (!buffers_allocated)
2872                 return;
2873         tracing_start_cmdline_record();
2874 }
2875
2876 static void trace_printk_start_stop_comm(int enabled)
2877 {
2878         if (!buffers_allocated)
2879                 return;
2880
2881         if (enabled)
2882                 tracing_start_cmdline_record();
2883         else
2884                 tracing_stop_cmdline_record();
2885 }
2886
2887 /**
2888  * trace_vbprintk - write binary msg to tracing buffer
2889  *
2890  */
2891 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
2892 {
2893         struct trace_event_call *call = &event_bprint;
2894         struct ring_buffer_event *event;
2895         struct ring_buffer *buffer;
2896         struct trace_array *tr = &global_trace;
2897         struct bprint_entry *entry;
2898         unsigned long flags;
2899         char *tbuffer;
2900         int len = 0, size, pc;
2901
2902         if (unlikely(tracing_selftest_running || tracing_disabled))
2903                 return 0;
2904
2905         /* Don't pollute graph traces with trace_vprintk internals */
2906         pause_graph_tracing();
2907
2908         pc = preempt_count();
2909         preempt_disable_notrace();
2910
2911         tbuffer = get_trace_buf();
2912         if (!tbuffer) {
2913                 len = 0;
2914                 goto out_nobuffer;
2915         }
2916
2917         len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
2918
2919         if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
2920                 goto out;
2921
2922         local_save_flags(flags);
2923         size = sizeof(*entry) + sizeof(u32) * len;
2924         buffer = tr->trace_buffer.buffer;
2925         event = __trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
2926                                             flags, pc);
2927         if (!event)
2928                 goto out;
2929         entry = ring_buffer_event_data(event);
2930         entry->ip                       = ip;
2931         entry->fmt                      = fmt;
2932
2933         memcpy(entry->buf, tbuffer, sizeof(u32) * len);
2934         if (!call_filter_check_discard(call, entry, buffer, event)) {
2935                 __buffer_unlock_commit(buffer, event);
2936                 ftrace_trace_stack(tr, buffer, flags, 6, pc, NULL);
2937         }
2938
2939 out:
2940         put_trace_buf();
2941
2942 out_nobuffer:
2943         preempt_enable_notrace();
2944         unpause_graph_tracing();
2945
2946         return len;
2947 }
2948 EXPORT_SYMBOL_GPL(trace_vbprintk);
2949
2950 static int
2951 __trace_array_vprintk(struct ring_buffer *buffer,
2952                       unsigned long ip, const char *fmt, va_list args)
2953 {
2954         struct trace_event_call *call = &event_print;
2955         struct ring_buffer_event *event;
2956         int len = 0, size, pc;
2957         struct print_entry *entry;
2958         unsigned long flags;
2959         char *tbuffer;
2960
2961         if (tracing_disabled || tracing_selftest_running)
2962                 return 0;
2963
2964         /* Don't pollute graph traces with trace_vprintk internals */
2965         pause_graph_tracing();
2966
2967         pc = preempt_count();
2968         preempt_disable_notrace();
2969
2970
2971         tbuffer = get_trace_buf();
2972         if (!tbuffer) {
2973                 len = 0;
2974                 goto out_nobuffer;
2975         }
2976
2977         len = vscnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
2978
2979         local_save_flags(flags);
2980         size = sizeof(*entry) + len + 1;
2981         event = __trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
2982                                             flags, pc);
2983         if (!event)
2984                 goto out;
2985         entry = ring_buffer_event_data(event);
2986         entry->ip = ip;
2987
2988         memcpy(&entry->buf, tbuffer, len + 1);
2989         if (!call_filter_check_discard(call, entry, buffer, event)) {
2990                 __buffer_unlock_commit(buffer, event);
2991                 ftrace_trace_stack(&global_trace, buffer, flags, 6, pc, NULL);
2992         }
2993
2994 out:
2995         put_trace_buf();
2996
2997 out_nobuffer:
2998         preempt_enable_notrace();
2999         unpause_graph_tracing();
3000
3001         return len;
3002 }
3003
3004 int trace_array_vprintk(struct trace_array *tr,
3005                         unsigned long ip, const char *fmt, va_list args)
3006 {
3007         return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
3008 }
3009
3010 int trace_array_printk(struct trace_array *tr,
3011                        unsigned long ip, const char *fmt, ...)
3012 {
3013         int ret;
3014         va_list ap;
3015
3016         if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
3017                 return 0;
3018
3019         va_start(ap, fmt);
3020         ret = trace_array_vprintk(tr, ip, fmt, ap);
3021         va_end(ap);
3022         return ret;
3023 }
3024
3025 int trace_array_printk_buf(struct ring_buffer *buffer,
3026                            unsigned long ip, const char *fmt, ...)
3027 {
3028         int ret;
3029         va_list ap;
3030
3031         if (!(global_trace.trace_flags & TRACE_ITER_PRINTK))
3032                 return 0;
3033
3034         va_start(ap, fmt);
3035         ret = __trace_array_vprintk(buffer, ip, fmt, ap);
3036         va_end(ap);
3037         return ret;
3038 }
3039
3040 int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
3041 {
3042         return trace_array_vprintk(&global_trace, ip, fmt, args);
3043 }
3044 EXPORT_SYMBOL_GPL(trace_vprintk);
3045
3046 static void trace_iterator_increment(struct trace_iterator *iter)
3047 {
3048         struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
3049
3050         iter->idx++;
3051         if (buf_iter)
3052                 ring_buffer_read(buf_iter, NULL);
3053 }
3054
3055 static struct trace_entry *
3056 peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
3057                 unsigned long *lost_events)
3058 {
3059         struct ring_buffer_event *event;
3060         struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
3061
3062         if (buf_iter)
3063                 event = ring_buffer_iter_peek(buf_iter, ts);
3064         else
3065                 event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
3066                                          lost_events);
3067
3068         if (event) {
3069                 iter->ent_size = ring_buffer_event_length(event);
3070                 return ring_buffer_event_data(event);
3071         }
3072         iter->ent_size = 0;
3073         return NULL;
3074 }
3075
3076 static struct trace_entry *
3077 __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
3078                   unsigned long *missing_events, u64 *ent_ts)
3079 {
3080         struct ring_buffer *buffer = iter->trace_buffer->buffer;
3081         struct trace_entry *ent, *next = NULL;
3082         unsigned long lost_events = 0, next_lost = 0;
3083         int cpu_file = iter->cpu_file;
3084         u64 next_ts = 0, ts;
3085         int next_cpu = -1;
3086         int next_size = 0;
3087         int cpu;
3088
3089         /*
3090          * If we are in a per_cpu trace file, don't bother by iterating over
3091          * all cpu and peek directly.
3092          */
3093         if (cpu_file > RING_BUFFER_ALL_CPUS) {
3094                 if (ring_buffer_empty_cpu(buffer, cpu_file))
3095                         return NULL;
3096                 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
3097                 if (ent_cpu)
3098                         *ent_cpu = cpu_file;
3099
3100                 return ent;
3101         }
3102
3103         for_each_tracing_cpu(cpu) {
3104
3105                 if (ring_buffer_empty_cpu(buffer, cpu))
3106                         continue;
3107
3108                 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
3109
3110                 /*
3111                  * Pick the entry with the smallest timestamp:
3112                  */
3113                 if (ent && (!next || ts < next_ts)) {
3114                         next = ent;
3115                         next_cpu = cpu;
3116                         next_ts = ts;
3117                         next_lost = lost_events;
3118                         next_size = iter->ent_size;
3119                 }
3120         }
3121
3122         iter->ent_size = next_size;
3123
3124         if (ent_cpu)
3125                 *ent_cpu = next_cpu;
3126
3127         if (ent_ts)
3128                 *ent_ts = next_ts;
3129
3130         if (missing_events)
3131                 *missing_events = next_lost;
3132
3133         return next;
3134 }
3135
3136 /* Find the next real entry, without updating the iterator itself */
3137 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
3138                                           int *ent_cpu, u64 *ent_ts)
3139 {
3140         return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
3141 }
3142
3143 /* Find the next real entry, and increment the iterator to the next entry */
3144 void *trace_find_next_entry_inc(struct trace_iterator *iter)
3145 {
3146         iter->ent = __find_next_entry(iter, &iter->cpu,
3147                                       &iter->lost_events, &iter->ts);
3148
3149         if (iter->ent)
3150                 trace_iterator_increment(iter);
3151
3152         return iter->ent ? iter : NULL;
3153 }
3154
3155 static void trace_consume(struct trace_iterator *iter)
3156 {
3157         ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
3158                             &iter->lost_events);
3159 }
3160
3161 static void *s_next(struct seq_file *m, void *v, loff_t *pos)
3162 {
3163         struct trace_iterator *iter = m->private;
3164         int i = (int)*pos;
3165         void *ent;
3166
3167         WARN_ON_ONCE(iter->leftover);
3168
3169         (*pos)++;
3170
3171         /* can't go backwards */
3172         if (iter->idx > i)
3173                 return NULL;
3174
3175         if (iter->idx < 0)
3176                 ent = trace_find_next_entry_inc(iter);
3177         else
3178                 ent = iter;
3179
3180         while (ent && iter->idx < i)
3181                 ent = trace_find_next_entry_inc(iter);
3182
3183         iter->pos = *pos;
3184
3185         return ent;
3186 }
3187
3188 void tracing_iter_reset(struct trace_iterator *iter, int cpu)
3189 {
3190         struct ring_buffer_event *event;
3191         struct ring_buffer_iter *buf_iter;
3192         unsigned long entries = 0;
3193         u64 ts;
3194
3195         per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
3196
3197         buf_iter = trace_buffer_iter(iter, cpu);
3198         if (!buf_iter)
3199                 return;
3200
3201         ring_buffer_iter_reset(buf_iter);
3202
3203         /*
3204          * We could have the case with the max latency tracers
3205          * that a reset never took place on a cpu. This is evident
3206          * by the timestamp being before the start of the buffer.
3207          */
3208         while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
3209                 if (ts >= iter->trace_buffer->time_start)
3210                         break;
3211                 entries++;
3212                 ring_buffer_read(buf_iter, NULL);
3213         }
3214
3215         per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
3216 }
3217
3218 /*
3219  * The current tracer is copied to avoid a global locking
3220  * all around.
3221  */
3222 static void *s_start(struct seq_file *m, loff_t *pos)
3223 {
3224         struct trace_iterator *iter = m->private;
3225         struct trace_array *tr = iter->tr;
3226         int cpu_file = iter->cpu_file;
3227         void *p = NULL;
3228         loff_t l = 0;
3229         int cpu;
3230
3231         /*
3232          * copy the tracer to avoid using a global lock all around.
3233          * iter->trace is a copy of current_trace, the pointer to the
3234          * name may be used instead of a strcmp(), as iter->trace->name
3235          * will point to the same string as current_trace->name.
3236          */
3237         mutex_lock(&trace_types_lock);
3238         if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
3239                 *iter->trace = *tr->current_trace;
3240         mutex_unlock(&trace_types_lock);
3241
3242 #ifdef CONFIG_TRACER_MAX_TRACE
3243         if (iter->snapshot && iter->trace->use_max_tr)
3244                 return ERR_PTR(-EBUSY);
3245 #endif
3246
3247         if (!iter->snapshot)
3248                 atomic_inc(&trace_record_taskinfo_disabled);
3249
3250         if (*pos != iter->pos) {
3251                 iter->ent = NULL;
3252                 iter->cpu = 0;
3253                 iter->idx = -1;
3254
3255                 if (cpu_file == RING_BUFFER_ALL_CPUS) {
3256                         for_each_tracing_cpu(cpu)
3257                                 tracing_iter_reset(iter, cpu);
3258                 } else
3259                         tracing_iter_reset(iter, cpu_file);
3260
3261                 iter->leftover = 0;
3262                 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
3263                         ;
3264
3265         } else {
3266                 /*
3267                  * If we overflowed the seq_file before, then we want
3268                  * to just reuse the trace_seq buffer again.
3269                  */
3270                 if (iter->leftover)
3271                         p = iter;
3272                 else {
3273                         l = *pos - 1;
3274                         p = s_next(m, p, &l);
3275                 }
3276         }
3277
3278         trace_event_read_lock();
3279         trace_access_lock(cpu_file);
3280         return p;
3281 }
3282
3283 static void s_stop(struct seq_file *m, void *p)
3284 {
3285         struct trace_iterator *iter = m->private;
3286
3287 #ifdef CONFIG_TRACER_MAX_TRACE
3288         if (iter->snapshot && iter->trace->use_max_tr)
3289                 return;
3290 #endif
3291
3292         if (!iter->snapshot)
3293                 atomic_dec(&trace_record_taskinfo_disabled);
3294
3295         trace_access_unlock(iter->cpu_file);
3296         trace_event_read_unlock();
3297 }
3298
3299 static void
3300 get_total_entries(struct trace_buffer *buf,
3301                   unsigned long *total, unsigned long *entries)
3302 {
3303         unsigned long count;
3304         int cpu;
3305
3306         *total = 0;
3307         *entries = 0;
3308
3309         for_each_tracing_cpu(cpu) {
3310                 count = ring_buffer_entries_cpu(buf->buffer, cpu);
3311                 /*
3312                  * If this buffer has skipped entries, then we hold all
3313                  * entries for the trace and we need to ignore the
3314                  * ones before the time stamp.
3315                  */
3316                 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
3317                         count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
3318                         /* total is the same as the entries */
3319                         *total += count;
3320                 } else
3321                         *total += count +
3322                                 ring_buffer_overrun_cpu(buf->buffer, cpu);
3323                 *entries += count;
3324         }
3325 }
3326
3327 static void print_lat_help_header(struct seq_file *m)
3328 {
3329         seq_puts(m, "#                  _------=> CPU#            \n"
3330                     "#                 / _-----=> irqs-off        \n"
3331                     "#                | / _----=> need-resched    \n"
3332                     "#                || / _---=> hardirq/softirq \n"
3333                     "#                ||| / _--=> preempt-depth   \n"
3334                     "#                |||| /     delay            \n"
3335                     "#  cmd     pid   ||||| time  |   caller      \n"
3336                     "#     \\   /      |||||  \\    |   /         \n");
3337 }
3338
3339 static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
3340 {
3341         unsigned long total;
3342         unsigned long entries;
3343
3344         get_total_entries(buf, &total, &entries);
3345         seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu   #P:%d\n",
3346                    entries, total, num_online_cpus());
3347         seq_puts(m, "#\n");
3348 }
3349
3350 static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m,
3351                                    unsigned int flags)
3352 {
3353         bool tgid = flags & TRACE_ITER_RECORD_TGID;
3354
3355         print_event_info(buf, m);
3356
3357         seq_printf(m, "#           TASK-PID   CPU#   %s  TIMESTAMP  FUNCTION\n", tgid ? "TGID     " : "");
3358         seq_printf(m, "#              | |       |    %s     |         |\n",      tgid ? "  |      " : "");
3359 }
3360
3361 static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m,
3362                                        unsigned int flags)
3363 {
3364         bool tgid = flags & TRACE_ITER_RECORD_TGID;
3365         const char tgid_space[] = "          ";
3366         const char space[] = "  ";
3367
3368         seq_printf(m, "#                          %s  _-----=> irqs-off\n",
3369                    tgid ? tgid_space : space);
3370         seq_printf(m, "#                          %s / _----=> need-resched\n",
3371                    tgid ? tgid_space : space);
3372         seq_printf(m, "#                          %s| / _---=> hardirq/softirq\n",
3373                    tgid ? tgid_space : space);
3374         seq_printf(m, "#                          %s|| / _--=> preempt-depth\n",
3375                    tgid ? tgid_space : space);
3376         seq_printf(m, "#                          %s||| /     delay\n",
3377                    tgid ? tgid_space : space);
3378         seq_printf(m, "#           TASK-PID   CPU#%s||||    TIMESTAMP  FUNCTION\n",
3379                    tgid ? "   TGID   " : space);
3380         seq_printf(m, "#              | |       | %s||||       |         |\n",
3381                    tgid ? "     |    " : space);
3382 }
3383
3384 void
3385 print_trace_header(struct seq_file *m, struct trace_iterator *iter)
3386 {
3387         unsigned long sym_flags = (global_trace.trace_flags & TRACE_ITER_SYM_MASK);
3388         struct trace_buffer *buf = iter->trace_buffer;
3389         struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
3390         struct tracer *type = iter->trace;
3391         unsigned long entries;
3392         unsigned long total;
3393         const char *name = "preemption";
3394
3395         name = type->name;
3396
3397         get_total_entries(buf, &total, &entries);
3398
3399         seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
3400                    name, UTS_RELEASE);
3401         seq_puts(m, "# -----------------------------------"
3402                  "---------------------------------\n");
3403         seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
3404                    " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
3405                    nsecs_to_usecs(data->saved_latency),
3406                    entries,
3407                    total,
3408                    buf->cpu,
3409 #if defined(CONFIG_PREEMPT_NONE)
3410                    "server",
3411 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
3412                    "desktop",
3413 #elif defined(CONFIG_PREEMPT)
3414                    "preempt",
3415 #else
3416                    "unknown",
3417 #endif
3418                    /* These are reserved for later use */
3419                    0, 0, 0, 0);
3420 #ifdef CONFIG_SMP
3421         seq_printf(m, " #P:%d)\n", num_online_cpus());
3422 #else
3423         seq_puts(m, ")\n");
3424 #endif
3425         seq_puts(m, "#    -----------------\n");
3426         seq_printf(m, "#    | task: %.16s-%d "
3427                    "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
3428                    data->comm, data->pid,
3429                    from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
3430                    data->policy, data->rt_priority);
3431         seq_puts(m, "#    -----------------\n");
3432
3433         if (data->critical_start) {
3434                 seq_puts(m, "#  => started at: ");
3435                 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
3436                 trace_print_seq(m, &iter->seq);
3437                 seq_puts(m, "\n#  => ended at:   ");
3438                 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
3439                 trace_print_seq(m, &iter->seq);
3440                 seq_puts(m, "\n#\n");
3441         }
3442
3443         seq_puts(m, "#\n");
3444 }
3445
3446 static void test_cpu_buff_start(struct trace_iterator *iter)
3447 {
3448         struct trace_seq *s = &iter->seq;
3449         struct trace_array *tr = iter->tr;
3450
3451         if (!(tr->trace_flags & TRACE_ITER_ANNOTATE))
3452                 return;
3453
3454         if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
3455                 return;
3456
3457         if (cpumask_available(iter->started) &&
3458             cpumask_test_cpu(iter->cpu, iter->started))
3459                 return;
3460
3461         if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
3462                 return;
3463
3464         if (cpumask_available(iter->started))
3465                 cpumask_set_cpu(iter->cpu, iter->started);
3466
3467         /* Don't print started cpu buffer for the first entry of the trace */
3468         if (iter->idx > 1)
3469                 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
3470                                 iter->cpu);
3471 }
3472
3473 static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
3474 {
3475         struct trace_array *tr = iter->tr;
3476         struct trace_seq *s = &iter->seq;
3477         unsigned long sym_flags = (tr->trace_flags & TRACE_ITER_SYM_MASK);
3478         struct trace_entry *entry;
3479         struct trace_event *event;
3480
3481         entry = iter->ent;
3482
3483         test_cpu_buff_start(iter);
3484
3485         event = ftrace_find_event(entry->type);
3486
3487         if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
3488                 if (iter->iter_flags & TRACE_FILE_LAT_FMT)
3489                         trace_print_lat_context(iter);
3490                 else
3491                         trace_print_context(iter);
3492         }
3493
3494         if (trace_seq_has_overflowed(s))
3495                 return TRACE_TYPE_PARTIAL_LINE;
3496
3497         if (event)
3498                 return event->funcs->trace(iter, sym_flags, event);
3499
3500         trace_seq_printf(s, "Unknown type %d\n", entry->type);
3501
3502         return trace_handle_return(s);
3503 }
3504
3505 static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
3506 {
3507         struct trace_array *tr = iter->tr;
3508         struct trace_seq *s = &iter->seq;
3509         struct trace_entry *entry;
3510         struct trace_event *event;
3511
3512         entry = iter->ent;
3513
3514         if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO)
3515                 trace_seq_printf(s, "%d %d %llu ",
3516                                  entry->pid, iter->cpu, iter->ts);
3517
3518         if (trace_seq_has_overflowed(s))
3519                 return TRACE_TYPE_PARTIAL_LINE;
3520
3521         event = ftrace_find_event(entry->type);
3522         if (event)
3523                 return event->funcs->raw(iter, 0, event);
3524
3525         trace_seq_printf(s, "%d ?\n", entry->type);
3526
3527         return trace_handle_return(s);
3528 }
3529
3530 static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
3531 {
3532         struct trace_array *tr = iter->tr;
3533         struct trace_seq *s = &iter->seq;
3534         unsigned char newline = '\n';
3535         struct trace_entry *entry;
3536         struct trace_event *event;
3537
3538         entry = iter->ent;
3539
3540         if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
3541                 SEQ_PUT_HEX_FIELD(s, entry->pid);
3542                 SEQ_PUT_HEX_FIELD(s, iter->cpu);
3543                 SEQ_PUT_HEX_FIELD(s, iter->ts);
3544                 if (trace_seq_has_overflowed(s))
3545                         return TRACE_TYPE_PARTIAL_LINE;
3546         }
3547
3548         event = ftrace_find_event(entry->type);
3549         if (event) {
3550                 enum print_line_t ret = event->funcs->hex(iter, 0, event);
3551                 if (ret != TRACE_TYPE_HANDLED)
3552                         return ret;
3553         }
3554
3555         SEQ_PUT_FIELD(s, newline);
3556
3557         return trace_handle_return(s);
3558 }
3559
3560 static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
3561 {
3562         struct trace_array *tr = iter->tr;
3563         struct trace_seq *s = &iter->seq;
3564         struct trace_entry *entry;
3565         struct trace_event *event;
3566
3567         entry = iter->ent;
3568
3569         if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
3570                 SEQ_PUT_FIELD(s, entry->pid);
3571                 SEQ_PUT_FIELD(s, iter->cpu);
3572                 SEQ_PUT_FIELD(s, iter->ts);
3573                 if (trace_seq_has_overflowed(s))
3574                         return TRACE_TYPE_PARTIAL_LINE;
3575         }
3576
3577         event = ftrace_find_event(entry->type);
3578         return event ? event->funcs->binary(iter, 0, event) :
3579                 TRACE_TYPE_HANDLED;
3580 }
3581
3582 int trace_empty(struct trace_iterator *iter)
3583 {
3584         struct ring_buffer_iter *buf_iter;
3585         int cpu;
3586
3587         /* If we are looking at one CPU buffer, only check that one */
3588         if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
3589                 cpu = iter->cpu_file;
3590                 buf_iter = trace_buffer_iter(iter, cpu);
3591                 if (buf_iter) {
3592                         if (!ring_buffer_iter_empty(buf_iter))
3593                                 return 0;
3594                 } else {
3595                         if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
3596                                 return 0;
3597                 }
3598                 return 1;
3599         }
3600
3601         for_each_tracing_cpu(cpu) {
3602                 buf_iter = trace_buffer_iter(iter, cpu);
3603                 if (buf_iter) {
3604                         if (!ring_buffer_iter_empty(buf_iter))
3605                                 return 0;
3606                 } else {
3607                         if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
3608                                 return 0;
3609                 }
3610         }
3611
3612         return 1;
3613 }
3614
3615 /*  Called with trace_event_read_lock() held. */
3616 enum print_line_t print_trace_line(struct trace_iterator *iter)
3617 {
3618         struct trace_array *tr = iter->tr;
3619         unsigned long trace_flags = tr->trace_flags;
3620         enum print_line_t ret;
3621
3622         if (iter->lost_events) {
3623                 trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
3624                                  iter->cpu, iter->lost_events);
3625                 if (trace_seq_has_overflowed(&iter->seq))
3626                         return TRACE_TYPE_PARTIAL_LINE;
3627         }
3628
3629         if (iter->trace && iter->trace->print_line) {
3630                 ret = iter->trace->print_line(iter);
3631                 if (ret != TRACE_TYPE_UNHANDLED)
3632                         return ret;
3633         }
3634
3635         if (iter->ent->type == TRACE_BPUTS &&
3636                         trace_flags & TRACE_ITER_PRINTK &&
3637                         trace_flags & TRACE_ITER_PRINTK_MSGONLY)
3638                 return trace_print_bputs_msg_only(iter);
3639
3640         if (iter->ent->type == TRACE_BPRINT &&
3641                         trace_flags & TRACE_ITER_PRINTK &&
3642                         trace_flags & TRACE_ITER_PRINTK_MSGONLY)
3643                 return trace_print_bprintk_msg_only(iter);
3644
3645         if (iter->ent->type == TRACE_PRINT &&
3646                         trace_flags & TRACE_ITER_PRINTK &&
3647                         trace_flags & TRACE_ITER_PRINTK_MSGONLY)