~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/kernel/trace/trace.c

Version: ~ [ linux-5.1-rc1 ] ~ [ linux-5.0.3 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.30 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.107 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.164 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.176 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.136 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.63 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp