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