LCOV - code coverage report
Current view: top level - kernel/trace - trace.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 1968 0.0 %
Date: 2014-02-18 Functions: 0 202 0.0 %
Branches: 0 1206 0.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * ring buffer based function tracer
       3                 :            :  *
       4                 :            :  * Copyright (C) 2007-2012 Steven Rostedt <srostedt@redhat.com>
       5                 :            :  * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
       6                 :            :  *
       7                 :            :  * Originally taken from the RT patch by:
       8                 :            :  *    Arnaldo Carvalho de Melo <acme@redhat.com>
       9                 :            :  *
      10                 :            :  * Based on code from the latency_tracer, that is:
      11                 :            :  *  Copyright (C) 2004-2006 Ingo Molnar
      12                 :            :  *  Copyright (C) 2004 Nadia Yvette Chambers
      13                 :            :  */
      14                 :            : #include <linux/ring_buffer.h>
      15                 :            : #include <generated/utsrelease.h>
      16                 :            : #include <linux/stacktrace.h>
      17                 :            : #include <linux/writeback.h>
      18                 :            : #include <linux/kallsyms.h>
      19                 :            : #include <linux/seq_file.h>
      20                 :            : #include <linux/notifier.h>
      21                 :            : #include <linux/irqflags.h>
      22                 :            : #include <linux/debugfs.h>
      23                 :            : #include <linux/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/nmi.h>
      40                 :            : #include <linux/fs.h>
      41                 :            : #include <linux/sched/rt.h>
      42                 :            : 
      43                 :            : #include "trace.h"
      44                 :            : #include "trace_output.h"
      45                 :            : 
      46                 :            : /*
      47                 :            :  * On boot up, the ring buffer is set to the minimum size, so that
      48                 :            :  * we do not waste memory on systems that are not using tracing.
      49                 :            :  */
      50                 :            : bool ring_buffer_expanded;
      51                 :            : 
      52                 :            : /*
      53                 :            :  * We need to change this state when a selftest is running.
      54                 :            :  * A selftest will lurk into the ring-buffer to count the
      55                 :            :  * entries inserted during the selftest although some concurrent
      56                 :            :  * insertions into the ring-buffer such as trace_printk could occurred
      57                 :            :  * at the same time, giving false positive or negative results.
      58                 :            :  */
      59                 :            : static bool __read_mostly tracing_selftest_running;
      60                 :            : 
      61                 :            : /*
      62                 :            :  * If a tracer is running, we do not want to run SELFTEST.
      63                 :            :  */
      64                 :            : bool __read_mostly tracing_selftest_disabled;
      65                 :            : 
      66                 :            : /* For tracers that don't implement custom flags */
      67                 :            : static struct tracer_opt dummy_tracer_opt[] = {
      68                 :            :         { }
      69                 :            : };
      70                 :            : 
      71                 :            : static struct tracer_flags dummy_tracer_flags = {
      72                 :            :         .val = 0,
      73                 :            :         .opts = dummy_tracer_opt
      74                 :            : };
      75                 :            : 
      76                 :          0 : static int dummy_set_flag(u32 old_flags, u32 bit, int set)
      77                 :            : {
      78                 :          0 :         return 0;
      79                 :            : }
      80                 :            : 
      81                 :            : /*
      82                 :            :  * To prevent the comm cache from being overwritten when no
      83                 :            :  * tracing is active, only save the comm when a trace event
      84                 :            :  * occurred.
      85                 :            :  */
      86                 :            : static DEFINE_PER_CPU(bool, trace_cmdline_save);
      87                 :            : 
      88                 :            : /*
      89                 :            :  * Kill all tracing for good (never come back).
      90                 :            :  * It is initialized to 1 but will turn to zero if the initialization
      91                 :            :  * of the tracer is successful. But that is the only place that sets
      92                 :            :  * this back to zero.
      93                 :            :  */
      94                 :            : static int tracing_disabled = 1;
      95                 :            : 
      96                 :            : DEFINE_PER_CPU(int, ftrace_cpu_disabled);
      97                 :            : 
      98                 :            : cpumask_var_t __read_mostly     tracing_buffer_mask;
      99                 :            : 
     100                 :            : /*
     101                 :            :  * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
     102                 :            :  *
     103                 :            :  * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
     104                 :            :  * is set, then ftrace_dump is called. This will output the contents
     105                 :            :  * of the ftrace buffers to the console.  This is very useful for
     106                 :            :  * capturing traces that lead to crashes and outputing it to a
     107                 :            :  * serial console.
     108                 :            :  *
     109                 :            :  * It is default off, but you can enable it with either specifying
     110                 :            :  * "ftrace_dump_on_oops" in the kernel command line, or setting
     111                 :            :  * /proc/sys/kernel/ftrace_dump_on_oops
     112                 :            :  * Set 1 if you want to dump buffers of all CPUs
     113                 :            :  * Set 2 if you want to dump the buffer of the CPU that triggered oops
     114                 :            :  */
     115                 :            : 
     116                 :            : enum ftrace_dump_mode ftrace_dump_on_oops;
     117                 :            : 
     118                 :            : /* When set, tracing will stop when a WARN*() is hit */
     119                 :            : int __disable_trace_on_warning;
     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 bool allocate_snapshot;
     128                 :            : 
     129                 :          0 : static int __init set_cmdline_ftrace(char *str)
     130                 :            : {
     131                 :          0 :         strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
     132                 :          0 :         default_bootup_tracer = bootup_tracer_buf;
     133                 :            :         /* We are using ftrace early, expand it */
     134                 :          0 :         ring_buffer_expanded = true;
     135                 :          0 :         return 1;
     136                 :            : }
     137                 :            : __setup("ftrace=", set_cmdline_ftrace);
     138                 :            : 
     139                 :          0 : static int __init set_ftrace_dump_on_oops(char *str)
     140                 :            : {
     141 [ #  # ][ #  # ]:          0 :         if (*str++ != '=' || !*str) {
     142                 :          0 :                 ftrace_dump_on_oops = DUMP_ALL;
     143                 :          0 :                 return 1;
     144                 :            :         }
     145                 :            : 
     146         [ #  # ]:          0 :         if (!strcmp("orig_cpu", str)) {
     147                 :          0 :                 ftrace_dump_on_oops = DUMP_ORIG;
     148                 :          0 :                 return 1;
     149                 :            :         }
     150                 :            : 
     151                 :            :         return 0;
     152                 :            : }
     153                 :            : __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
     154                 :            : 
     155                 :          0 : static int __init stop_trace_on_warning(char *str)
     156                 :            : {
     157                 :          0 :         __disable_trace_on_warning = 1;
     158                 :          0 :         return 1;
     159                 :            : }
     160                 :            : __setup("traceoff_on_warning=", stop_trace_on_warning);
     161                 :            : 
     162                 :          0 : static int __init boot_alloc_snapshot(char *str)
     163                 :            : {
     164                 :          0 :         allocate_snapshot = true;
     165                 :            :         /* We also need the main ring buffer expanded */
     166                 :          0 :         ring_buffer_expanded = true;
     167                 :          0 :         return 1;
     168                 :            : }
     169                 :            : __setup("alloc_snapshot", boot_alloc_snapshot);
     170                 :            : 
     171                 :            : 
     172                 :            : static char trace_boot_options_buf[MAX_TRACER_SIZE] __initdata;
     173                 :            : static char *trace_boot_options __initdata;
     174                 :            : 
     175                 :          0 : static int __init set_trace_boot_options(char *str)
     176                 :            : {
     177                 :          0 :         strlcpy(trace_boot_options_buf, str, MAX_TRACER_SIZE);
     178                 :          0 :         trace_boot_options = trace_boot_options_buf;
     179                 :          0 :         return 0;
     180                 :            : }
     181                 :            : __setup("trace_options=", set_trace_boot_options);
     182                 :            : 
     183                 :            : 
     184                 :          0 : unsigned long long ns2usecs(cycle_t nsec)
     185                 :            : {
     186                 :          0 :         nsec += 500;
     187                 :          0 :         do_div(nsec, 1000);
     188                 :          0 :         return nsec;
     189                 :            : }
     190                 :            : 
     191                 :            : /*
     192                 :            :  * The global_trace is the descriptor that holds the tracing
     193                 :            :  * buffers for the live tracing. For each CPU, it contains
     194                 :            :  * a link list of pages that will store trace entries. The
     195                 :            :  * page descriptor of the pages in the memory is used to hold
     196                 :            :  * the link list by linking the lru item in the page descriptor
     197                 :            :  * to each of the pages in the buffer per CPU.
     198                 :            :  *
     199                 :            :  * For each active CPU there is a data field that holds the
     200                 :            :  * pages for the buffer for that CPU. Each CPU has the same number
     201                 :            :  * of pages allocated for its buffer.
     202                 :            :  */
     203                 :            : static struct trace_array       global_trace;
     204                 :            : 
     205                 :            : LIST_HEAD(ftrace_trace_arrays);
     206                 :            : 
     207                 :          0 : int trace_array_get(struct trace_array *this_tr)
     208                 :            : {
     209                 :            :         struct trace_array *tr;
     210                 :            :         int ret = -ENODEV;
     211                 :            : 
     212                 :          0 :         mutex_lock(&trace_types_lock);
     213         [ #  # ]:          0 :         list_for_each_entry(tr, &ftrace_trace_arrays, list) {
     214         [ #  # ]:          0 :                 if (tr == this_tr) {
     215                 :          0 :                         tr->ref++;
     216                 :            :                         ret = 0;
     217                 :          0 :                         break;
     218                 :            :                 }
     219                 :            :         }
     220                 :          0 :         mutex_unlock(&trace_types_lock);
     221                 :            : 
     222                 :          0 :         return ret;
     223                 :            : }
     224                 :            : 
     225                 :          0 : static void __trace_array_put(struct trace_array *this_tr)
     226                 :            : {
     227         [ #  # ]:          0 :         WARN_ON(!this_tr->ref);
     228                 :          0 :         this_tr->ref--;
     229                 :          0 : }
     230                 :            : 
     231                 :          0 : void trace_array_put(struct trace_array *this_tr)
     232                 :            : {
     233                 :          0 :         mutex_lock(&trace_types_lock);
     234                 :          0 :         __trace_array_put(this_tr);
     235                 :          0 :         mutex_unlock(&trace_types_lock);
     236                 :          0 : }
     237                 :            : 
     238                 :          0 : int filter_check_discard(struct ftrace_event_file *file, void *rec,
     239                 :            :                          struct ring_buffer *buffer,
     240                 :            :                          struct ring_buffer_event *event)
     241                 :            : {
     242   [ #  #  #  # ]:          0 :         if (unlikely(file->flags & FTRACE_EVENT_FL_FILTERED) &&
     243                 :          0 :             !filter_match_preds(file->filter, rec)) {
     244                 :          0 :                 ring_buffer_discard_commit(buffer, event);
     245                 :          0 :                 return 1;
     246                 :            :         }
     247                 :            : 
     248                 :            :         return 0;
     249                 :            : }
     250                 :            : EXPORT_SYMBOL_GPL(filter_check_discard);
     251                 :            : 
     252                 :          0 : int call_filter_check_discard(struct ftrace_event_call *call, void *rec,
     253                 :            :                               struct ring_buffer *buffer,
     254                 :            :                               struct ring_buffer_event *event)
     255                 :            : {
     256   [ #  #  #  # ]:          0 :         if (unlikely(call->flags & TRACE_EVENT_FL_FILTERED) &&
     257                 :          0 :             !filter_match_preds(call->filter, rec)) {
     258                 :          0 :                 ring_buffer_discard_commit(buffer, event);
     259                 :          0 :                 return 1;
     260                 :            :         }
     261                 :            : 
     262                 :            :         return 0;
     263                 :            : }
     264                 :            : EXPORT_SYMBOL_GPL(call_filter_check_discard);
     265                 :            : 
     266                 :          0 : cycle_t buffer_ftrace_now(struct trace_buffer *buf, int cpu)
     267                 :            : {
     268                 :            :         u64 ts;
     269                 :            : 
     270                 :            :         /* Early boot up does not have a buffer yet */
     271         [ #  # ]:          0 :         if (!buf->buffer)
     272                 :          0 :                 return trace_clock_local();
     273                 :            : 
     274                 :          0 :         ts = ring_buffer_time_stamp(buf->buffer, cpu);
     275                 :          0 :         ring_buffer_normalize_time_stamp(buf->buffer, cpu, &ts);
     276                 :            : 
     277                 :          0 :         return ts;
     278                 :            : }
     279                 :            : 
     280                 :          0 : cycle_t ftrace_now(int cpu)
     281                 :            : {
     282                 :          0 :         return buffer_ftrace_now(&global_trace.trace_buffer, cpu);
     283                 :            : }
     284                 :            : 
     285                 :            : /**
     286                 :            :  * tracing_is_enabled - Show if global_trace has been disabled
     287                 :            :  *
     288                 :            :  * Shows if the global trace has been enabled or not. It uses the
     289                 :            :  * mirror flag "buffer_disabled" to be used in fast paths such as for
     290                 :            :  * the irqsoff tracer. But it may be inaccurate due to races. If you
     291                 :            :  * need to know the accurate state, use tracing_is_on() which is a little
     292                 :            :  * slower, but accurate.
     293                 :            :  */
     294                 :          0 : int tracing_is_enabled(void)
     295                 :            : {
     296                 :            :         /*
     297                 :            :          * For quick access (irqsoff uses this in fast path), just
     298                 :            :          * return the mirror variable of the state of the ring buffer.
     299                 :            :          * It's a little racy, but we don't really care.
     300                 :            :          */
     301                 :          0 :         smp_rmb();
     302                 :          0 :         return !global_trace.buffer_disabled;
     303                 :            : }
     304                 :            : 
     305                 :            : /*
     306                 :            :  * trace_buf_size is the size in bytes that is allocated
     307                 :            :  * for a buffer. Note, the number of bytes is always rounded
     308                 :            :  * to page size.
     309                 :            :  *
     310                 :            :  * This number is purposely set to a low number of 16384.
     311                 :            :  * If the dump on oops happens, it will be much appreciated
     312                 :            :  * to not have to wait for all that output. Anyway this can be
     313                 :            :  * boot time and run time configurable.
     314                 :            :  */
     315                 :            : #define TRACE_BUF_SIZE_DEFAULT  1441792UL /* 16384 * 88 (sizeof(entry)) */
     316                 :            : 
     317                 :            : static unsigned long            trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
     318                 :            : 
     319                 :            : /* trace_types holds a link list of available tracers. */
     320                 :            : static struct tracer            *trace_types __read_mostly;
     321                 :            : 
     322                 :            : /*
     323                 :            :  * trace_types_lock is used to protect the trace_types list.
     324                 :            :  */
     325                 :            : DEFINE_MUTEX(trace_types_lock);
     326                 :            : 
     327                 :            : /*
     328                 :            :  * serialize the access of the ring buffer
     329                 :            :  *
     330                 :            :  * ring buffer serializes readers, but it is low level protection.
     331                 :            :  * The validity of the events (which returns by ring_buffer_peek() ..etc)
     332                 :            :  * are not protected by ring buffer.
     333                 :            :  *
     334                 :            :  * The content of events may become garbage if we allow other process consumes
     335                 :            :  * these events concurrently:
     336                 :            :  *   A) the page of the consumed events may become a normal page
     337                 :            :  *      (not reader page) in ring buffer, and this page will be rewrited
     338                 :            :  *      by events producer.
     339                 :            :  *   B) The page of the consumed events may become a page for splice_read,
     340                 :            :  *      and this page will be returned to system.
     341                 :            :  *
     342                 :            :  * These primitives allow multi process access to different cpu ring buffer
     343                 :            :  * concurrently.
     344                 :            :  *
     345                 :            :  * These primitives don't distinguish read-only and read-consume access.
     346                 :            :  * Multi read-only access are also serialized.
     347                 :            :  */
     348                 :            : 
     349                 :            : #ifdef CONFIG_SMP
     350                 :            : static DECLARE_RWSEM(all_cpu_access_lock);
     351                 :            : static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
     352                 :            : 
     353                 :            : static inline void trace_access_lock(int cpu)
     354                 :            : {
     355 [ #  # ][ #  #  :          0 :         if (cpu == RING_BUFFER_ALL_CPUS) {
          #  #  #  #  #  
                      # ]
     356                 :            :                 /* gain it for accessing the whole ring buffer. */
     357                 :          0 :                 down_write(&all_cpu_access_lock);
     358                 :            :         } else {
     359                 :            :                 /* gain it for accessing a cpu ring buffer. */
     360                 :            : 
     361                 :            :                 /* Firstly block other trace_access_lock(RING_BUFFER_ALL_CPUS). */
     362                 :          0 :                 down_read(&all_cpu_access_lock);
     363                 :            : 
     364                 :            :                 /* Secondly block other access to this @cpu ring buffer. */
     365                 :          0 :                 mutex_lock(&per_cpu(cpu_access_lock, cpu));
     366                 :            :         }
     367                 :            : }
     368                 :            : 
     369                 :            : static inline void trace_access_unlock(int cpu)
     370                 :            : {
     371   [ #  #  #  # ]:          0 :         if (cpu == RING_BUFFER_ALL_CPUS) {
         [ #  # ][ #  # ]
                 [ #  # ]
     372                 :          0 :                 up_write(&all_cpu_access_lock);
     373                 :            :         } else {
     374                 :          0 :                 mutex_unlock(&per_cpu(cpu_access_lock, cpu));
     375                 :          0 :                 up_read(&all_cpu_access_lock);
     376                 :            :         }
     377                 :            : }
     378                 :            : 
     379                 :            : static inline void trace_access_lock_init(void)
     380                 :            : {
     381                 :            :         int cpu;
     382                 :            : 
     383         [ #  # ]:          0 :         for_each_possible_cpu(cpu)
     384                 :          0 :                 mutex_init(&per_cpu(cpu_access_lock, cpu));
     385                 :            : }
     386                 :            : 
     387                 :            : #else
     388                 :            : 
     389                 :            : static DEFINE_MUTEX(access_lock);
     390                 :            : 
     391                 :            : static inline void trace_access_lock(int cpu)
     392                 :            : {
     393                 :            :         (void)cpu;
     394                 :            :         mutex_lock(&access_lock);
     395                 :            : }
     396                 :            : 
     397                 :            : static inline void trace_access_unlock(int cpu)
     398                 :            : {
     399                 :            :         (void)cpu;
     400                 :            :         mutex_unlock(&access_lock);
     401                 :            : }
     402                 :            : 
     403                 :            : static inline void trace_access_lock_init(void)
     404                 :            : {
     405                 :            : }
     406                 :            : 
     407                 :            : #endif
     408                 :            : 
     409                 :            : /* trace_flags holds trace_options default values */
     410                 :            : unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
     411                 :            :         TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
     412                 :            :         TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
     413                 :            :         TRACE_ITER_IRQ_INFO | TRACE_ITER_MARKERS | TRACE_ITER_FUNCTION;
     414                 :            : 
     415                 :            : static void tracer_tracing_on(struct trace_array *tr)
     416                 :            : {
     417 [ #  # ][ #  # ]:          0 :         if (tr->trace_buffer.buffer)
     418                 :          0 :                 ring_buffer_record_on(tr->trace_buffer.buffer);
     419                 :            :         /*
     420                 :            :          * This flag is looked at when buffers haven't been allocated
     421                 :            :          * yet, or by some tracers (like irqsoff), that just want to
     422                 :            :          * know if the ring buffer has been disabled, but it can handle
     423                 :            :          * races of where it gets disabled but we still do a record.
     424                 :            :          * As the check is in the fast path of the tracers, it is more
     425                 :            :          * important to be fast than accurate.
     426                 :            :          */
     427                 :          0 :         tr->buffer_disabled = 0;
     428                 :            :         /* Make the flag seen by readers */
     429                 :          0 :         smp_wmb();
     430                 :            : }
     431                 :            : 
     432                 :            : /**
     433                 :            :  * tracing_on - enable tracing buffers
     434                 :            :  *
     435                 :            :  * This function enables tracing buffers that may have been
     436                 :            :  * disabled with tracing_off.
     437                 :            :  */
     438                 :          0 : void tracing_on(void)
     439                 :            : {
     440                 :          0 :         tracer_tracing_on(&global_trace);
     441                 :          0 : }
     442                 :            : EXPORT_SYMBOL_GPL(tracing_on);
     443                 :            : 
     444                 :            : /**
     445                 :            :  * __trace_puts - write a constant string into the trace buffer.
     446                 :            :  * @ip:    The address of the caller
     447                 :            :  * @str:   The constant string to write
     448                 :            :  * @size:  The size of the string.
     449                 :            :  */
     450                 :          0 : int __trace_puts(unsigned long ip, const char *str, int size)
     451                 :            : {
     452                 :            :         struct ring_buffer_event *event;
     453                 :            :         struct ring_buffer *buffer;
     454                 :            :         struct print_entry *entry;
     455                 :            :         unsigned long irq_flags;
     456                 :            :         int alloc;
     457                 :            : 
     458                 :          0 :         alloc = sizeof(*entry) + size + 2; /* possible \n added */
     459                 :            : 
     460                 :            :         local_save_flags(irq_flags);
     461                 :          0 :         buffer = global_trace.trace_buffer.buffer;
     462                 :          0 :         event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, alloc, 
     463                 :            :                                           irq_flags, preempt_count());
     464         [ #  # ]:          0 :         if (!event)
     465                 :            :                 return 0;
     466                 :            : 
     467                 :          0 :         entry = ring_buffer_event_data(event);
     468                 :          0 :         entry->ip = ip;
     469                 :            : 
     470                 :          0 :         memcpy(&entry->buf, str, size);
     471                 :            : 
     472                 :            :         /* Add a newline if necessary */
     473         [ #  # ]:          0 :         if (entry->buf[size - 1] != '\n') {
     474                 :          0 :                 entry->buf[size] = '\n';
     475                 :          0 :                 entry->buf[size + 1] = '\0';
     476                 :            :         } else
     477                 :          0 :                 entry->buf[size] = '\0';
     478                 :            : 
     479                 :            :         __buffer_unlock_commit(buffer, event);
     480                 :            : 
     481                 :          0 :         return size;
     482                 :            : }
     483                 :            : EXPORT_SYMBOL_GPL(__trace_puts);
     484                 :            : 
     485                 :            : /**
     486                 :            :  * __trace_bputs - write the pointer to a constant string into trace buffer
     487                 :            :  * @ip:    The address of the caller
     488                 :            :  * @str:   The constant string to write to the buffer to
     489                 :            :  */
     490                 :          0 : int __trace_bputs(unsigned long ip, const char *str)
     491                 :            : {
     492                 :            :         struct ring_buffer_event *event;
     493                 :            :         struct ring_buffer *buffer;
     494                 :            :         struct bputs_entry *entry;
     495                 :            :         unsigned long irq_flags;
     496                 :            :         int size = sizeof(struct bputs_entry);
     497                 :            : 
     498                 :            :         local_save_flags(irq_flags);
     499                 :          0 :         buffer = global_trace.trace_buffer.buffer;
     500                 :          0 :         event = trace_buffer_lock_reserve(buffer, TRACE_BPUTS, size,
     501                 :            :                                           irq_flags, preempt_count());
     502         [ #  # ]:          0 :         if (!event)
     503                 :            :                 return 0;
     504                 :            : 
     505                 :          0 :         entry = ring_buffer_event_data(event);
     506                 :          0 :         entry->ip                    = ip;
     507                 :          0 :         entry->str                   = str;
     508                 :            : 
     509                 :            :         __buffer_unlock_commit(buffer, event);
     510                 :            : 
     511                 :          0 :         return 1;
     512                 :            : }
     513                 :            : EXPORT_SYMBOL_GPL(__trace_bputs);
     514                 :            : 
     515                 :            : #ifdef CONFIG_TRACER_SNAPSHOT
     516                 :            : /**
     517                 :            :  * trace_snapshot - take a snapshot of the current buffer.
     518                 :            :  *
     519                 :            :  * This causes a swap between the snapshot buffer and the current live
     520                 :            :  * tracing buffer. You can use this to take snapshots of the live
     521                 :            :  * trace when some condition is triggered, but continue to trace.
     522                 :            :  *
     523                 :            :  * Note, make sure to allocate the snapshot with either
     524                 :            :  * a tracing_snapshot_alloc(), or by doing it manually
     525                 :            :  * with: echo 1 > /sys/kernel/debug/tracing/snapshot
     526                 :            :  *
     527                 :            :  * If the snapshot buffer is not allocated, it will stop tracing.
     528                 :            :  * Basically making a permanent snapshot.
     529                 :            :  */
     530                 :            : void tracing_snapshot(void)
     531                 :            : {
     532                 :            :         struct trace_array *tr = &global_trace;
     533                 :            :         struct tracer *tracer = tr->current_trace;
     534                 :            :         unsigned long flags;
     535                 :            : 
     536                 :            :         if (in_nmi()) {
     537                 :            :                 internal_trace_puts("*** SNAPSHOT CALLED FROM NMI CONTEXT ***\n");
     538                 :            :                 internal_trace_puts("*** snapshot is being ignored        ***\n");
     539                 :            :                 return;
     540                 :            :         }
     541                 :            : 
     542                 :            :         if (!tr->allocated_snapshot) {
     543                 :            :                 internal_trace_puts("*** SNAPSHOT NOT ALLOCATED ***\n");
     544                 :            :                 internal_trace_puts("*** stopping trace here!   ***\n");
     545                 :            :                 tracing_off();
     546                 :            :                 return;
     547                 :            :         }
     548                 :            : 
     549                 :            :         /* Note, snapshot can not be used when the tracer uses it */
     550                 :            :         if (tracer->use_max_tr) {
     551                 :            :                 internal_trace_puts("*** LATENCY TRACER ACTIVE ***\n");
     552                 :            :                 internal_trace_puts("*** Can not use snapshot (sorry) ***\n");
     553                 :            :                 return;
     554                 :            :         }
     555                 :            : 
     556                 :            :         local_irq_save(flags);
     557                 :            :         update_max_tr(tr, current, smp_processor_id());
     558                 :            :         local_irq_restore(flags);
     559                 :            : }
     560                 :            : EXPORT_SYMBOL_GPL(tracing_snapshot);
     561                 :            : 
     562                 :            : static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
     563                 :            :                                         struct trace_buffer *size_buf, int cpu_id);
     564                 :            : static void set_buffer_entries(struct trace_buffer *buf, unsigned long val);
     565                 :            : 
     566                 :            : static int alloc_snapshot(struct trace_array *tr)
     567                 :            : {
     568                 :            :         int ret;
     569                 :            : 
     570                 :            :         if (!tr->allocated_snapshot) {
     571                 :            : 
     572                 :            :                 /* allocate spare buffer */
     573                 :            :                 ret = resize_buffer_duplicate_size(&tr->max_buffer,
     574                 :            :                                    &tr->trace_buffer, RING_BUFFER_ALL_CPUS);
     575                 :            :                 if (ret < 0)
     576                 :            :                         return ret;
     577                 :            : 
     578                 :            :                 tr->allocated_snapshot = true;
     579                 :            :         }
     580                 :            : 
     581                 :            :         return 0;
     582                 :            : }
     583                 :            : 
     584                 :            : void free_snapshot(struct trace_array *tr)
     585                 :            : {
     586                 :            :         /*
     587                 :            :          * We don't free the ring buffer. instead, resize it because
     588                 :            :          * The max_tr ring buffer has some state (e.g. ring->clock) and
     589                 :            :          * we want preserve it.
     590                 :            :          */
     591                 :            :         ring_buffer_resize(tr->max_buffer.buffer, 1, RING_BUFFER_ALL_CPUS);
     592                 :            :         set_buffer_entries(&tr->max_buffer, 1);
     593                 :            :         tracing_reset_online_cpus(&tr->max_buffer);
     594                 :            :         tr->allocated_snapshot = false;
     595                 :            : }
     596                 :            : 
     597                 :            : /**
     598                 :            :  * trace_snapshot_alloc - allocate and take a snapshot of the current buffer.
     599                 :            :  *
     600                 :            :  * This is similar to trace_snapshot(), but it will allocate the
     601                 :            :  * snapshot buffer if it isn't already allocated. Use this only
     602                 :            :  * where it is safe to sleep, as the allocation may sleep.
     603                 :            :  *
     604                 :            :  * This causes a swap between the snapshot buffer and the current live
     605                 :            :  * tracing buffer. You can use this to take snapshots of the live
     606                 :            :  * trace when some condition is triggered, but continue to trace.
     607                 :            :  */
     608                 :            : void tracing_snapshot_alloc(void)
     609                 :            : {
     610                 :            :         struct trace_array *tr = &global_trace;
     611                 :            :         int ret;
     612                 :            : 
     613                 :            :         ret = alloc_snapshot(tr);
     614                 :            :         if (WARN_ON(ret < 0))
     615                 :            :                 return;
     616                 :            : 
     617                 :            :         tracing_snapshot();
     618                 :            : }
     619                 :            : EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
     620                 :            : #else
     621                 :          0 : void tracing_snapshot(void)
     622                 :            : {
     623 [ #  # ][ #  # ]:          0 :         WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
     624                 :          0 : }
     625                 :            : EXPORT_SYMBOL_GPL(tracing_snapshot);
     626                 :          0 : void tracing_snapshot_alloc(void)
     627                 :            : {
     628                 :            :         /* Give warning */
     629                 :          0 :         tracing_snapshot();
     630                 :          0 : }
     631                 :            : EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
     632                 :            : #endif /* CONFIG_TRACER_SNAPSHOT */
     633                 :            : 
     634                 :            : static void tracer_tracing_off(struct trace_array *tr)
     635                 :            : {
     636 [ #  # ][ #  # ]:          0 :         if (tr->trace_buffer.buffer)
                 [ #  # ]
     637                 :          0 :                 ring_buffer_record_off(tr->trace_buffer.buffer);
     638                 :            :         /*
     639                 :            :          * This flag is looked at when buffers haven't been allocated
     640                 :            :          * yet, or by some tracers (like irqsoff), that just want to
     641                 :            :          * know if the ring buffer has been disabled, but it can handle
     642                 :            :          * races of where it gets disabled but we still do a record.
     643                 :            :          * As the check is in the fast path of the tracers, it is more
     644                 :            :          * important to be fast than accurate.
     645                 :            :          */
     646                 :          0 :         tr->buffer_disabled = 1;
     647                 :            :         /* Make the flag seen by readers */
     648                 :          0 :         smp_wmb();
     649                 :            : }
     650                 :            : 
     651                 :            : /**
     652                 :            :  * tracing_off - turn off tracing buffers
     653                 :            :  *
     654                 :            :  * This function stops the tracing buffers from recording data.
     655                 :            :  * It does not disable any overhead the tracers themselves may
     656                 :            :  * be causing. This function simply causes all recording to
     657                 :            :  * the ring buffers to fail.
     658                 :            :  */
     659                 :          0 : void tracing_off(void)
     660                 :            : {
     661                 :          0 :         tracer_tracing_off(&global_trace);
     662                 :          0 : }
     663                 :            : EXPORT_SYMBOL_GPL(tracing_off);
     664                 :            : 
     665                 :          0 : void disable_trace_on_warning(void)
     666                 :            : {
     667         [ #  # ]:          0 :         if (__disable_trace_on_warning)
     668                 :          0 :                 tracing_off();
     669                 :          0 : }
     670                 :            : 
     671                 :            : /**
     672                 :            :  * tracer_tracing_is_on - show real state of ring buffer enabled
     673                 :            :  * @tr : the trace array to know if ring buffer is enabled
     674                 :            :  *
     675                 :            :  * Shows real state of the ring buffer if it is enabled or not.
     676                 :            :  */
     677                 :            : static int tracer_tracing_is_on(struct trace_array *tr)
     678                 :            : {
     679 [ #  # ][ #  # ]:          0 :         if (tr->trace_buffer.buffer)
     680                 :          0 :                 return ring_buffer_record_is_on(tr->trace_buffer.buffer);
     681                 :          0 :         return !tr->buffer_disabled;
     682                 :            : }
     683                 :            : 
     684                 :            : /**
     685                 :            :  * tracing_is_on - show state of ring buffers enabled
     686                 :            :  */
     687                 :          0 : int tracing_is_on(void)
     688                 :            : {
     689                 :          0 :         return tracer_tracing_is_on(&global_trace);
     690                 :            : }
     691                 :            : EXPORT_SYMBOL_GPL(tracing_is_on);
     692                 :            : 
     693                 :          0 : static int __init set_buf_size(char *str)
     694                 :            : {
     695                 :            :         unsigned long buf_size;
     696                 :            : 
     697         [ #  # ]:          0 :         if (!str)
     698                 :            :                 return 0;
     699                 :          0 :         buf_size = memparse(str, &str);
     700                 :            :         /* nr_entries can not be zero */
     701         [ #  # ]:          0 :         if (buf_size == 0)
     702                 :            :                 return 0;
     703                 :          0 :         trace_buf_size = buf_size;
     704                 :          0 :         return 1;
     705                 :            : }
     706                 :            : __setup("trace_buf_size=", set_buf_size);
     707                 :            : 
     708                 :          0 : static int __init set_tracing_thresh(char *str)
     709                 :            : {
     710                 :            :         unsigned long threshold;
     711                 :            :         int ret;
     712                 :            : 
     713         [ #  # ]:          0 :         if (!str)
     714                 :            :                 return 0;
     715                 :            :         ret = kstrtoul(str, 0, &threshold);
     716         [ #  # ]:          0 :         if (ret < 0)
     717                 :            :                 return 0;
     718                 :          0 :         tracing_thresh = threshold * 1000;
     719                 :          0 :         return 1;
     720                 :            : }
     721                 :            : __setup("tracing_thresh=", set_tracing_thresh);
     722                 :            : 
     723                 :          0 : unsigned long nsecs_to_usecs(unsigned long nsecs)
     724                 :            : {
     725                 :          0 :         return nsecs / 1000;
     726                 :            : }
     727                 :            : 
     728                 :            : /* These must match the bit postions in trace_iterator_flags */
     729                 :            : static const char *trace_options[] = {
     730                 :            :         "print-parent",
     731                 :            :         "sym-offset",
     732                 :            :         "sym-addr",
     733                 :            :         "verbose",
     734                 :            :         "raw",
     735                 :            :         "hex",
     736                 :            :         "bin",
     737                 :            :         "block",
     738                 :            :         "stacktrace",
     739                 :            :         "trace_printk",
     740                 :            :         "ftrace_preempt",
     741                 :            :         "branch",
     742                 :            :         "annotate",
     743                 :            :         "userstacktrace",
     744                 :            :         "sym-userobj",
     745                 :            :         "printk-msg-only",
     746                 :            :         "context-info",
     747                 :            :         "latency-format",
     748                 :            :         "sleep-time",
     749                 :            :         "graph-time",
     750                 :            :         "record-cmd",
     751                 :            :         "overwrite",
     752                 :            :         "disable_on_free",
     753                 :            :         "irq-info",
     754                 :            :         "markers",
     755                 :            :         "function-trace",
     756                 :            :         "print-tgid",
     757                 :            :         NULL
     758                 :            : };
     759                 :            : 
     760                 :            : static struct {
     761                 :            :         u64 (*func)(void);
     762                 :            :         const char *name;
     763                 :            :         int in_ns;              /* is this clock in nanoseconds? */
     764                 :            : } trace_clocks[] = {
     765                 :            :         { trace_clock_local,    "local",      1 },
     766                 :            :         { trace_clock_global,   "global",     1 },
     767                 :            :         { trace_clock_counter,  "counter",    0 },
     768                 :            :         { trace_clock_jiffies,  "uptime",     1 },
     769                 :            :         { trace_clock,          "perf",               1 },
     770                 :            :         ARCH_TRACE_CLOCKS
     771                 :            : };
     772                 :            : 
     773                 :            : /*
     774                 :            :  * trace_parser_get_init - gets the buffer for trace parser
     775                 :            :  */
     776                 :          0 : int trace_parser_get_init(struct trace_parser *parser, int size)
     777                 :            : {
     778                 :          0 :         memset(parser, 0, sizeof(*parser));
     779                 :            : 
     780                 :          0 :         parser->buffer = kmalloc(size, GFP_KERNEL);
     781         [ #  # ]:          0 :         if (!parser->buffer)
     782                 :            :                 return 1;
     783                 :            : 
     784                 :          0 :         parser->size = size;
     785                 :          0 :         return 0;
     786                 :            : }
     787                 :            : 
     788                 :            : /*
     789                 :            :  * trace_parser_put - frees the buffer for trace parser
     790                 :            :  */
     791                 :          0 : void trace_parser_put(struct trace_parser *parser)
     792                 :            : {
     793                 :          0 :         kfree(parser->buffer);
     794                 :          0 : }
     795                 :            : 
     796                 :            : /*
     797                 :            :  * trace_get_user - reads the user input string separated by  space
     798                 :            :  * (matched by isspace(ch))
     799                 :            :  *
     800                 :            :  * For each string found the 'struct trace_parser' is updated,
     801                 :            :  * and the function returns.
     802                 :            :  *
     803                 :            :  * Returns number of bytes read.
     804                 :            :  *
     805                 :            :  * See kernel/trace/trace.h for 'struct trace_parser' details.
     806                 :            :  */
     807                 :          0 : int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
     808                 :            :         size_t cnt, loff_t *ppos)
     809                 :            : {
     810                 :            :         char ch;
     811                 :            :         size_t read = 0;
     812                 :            :         ssize_t ret;
     813                 :            : 
     814         [ #  # ]:          0 :         if (!*ppos)
     815                 :            :                 trace_parser_clear(parser);
     816                 :            : 
     817                 :          0 :         ret = get_user(ch, ubuf++);
     818         [ #  # ]:          0 :         if (ret)
     819                 :            :                 goto out;
     820                 :            : 
     821                 :            :         read++;
     822                 :          0 :         cnt--;
     823                 :            : 
     824                 :            :         /*
     825                 :            :          * The parser is not finished with the last write,
     826                 :            :          * continue reading the user input without skipping spaces.
     827                 :            :          */
     828         [ #  # ]:          0 :         if (!parser->cont) {
     829                 :            :                 /* skip white space */
     830 [ #  # ][ #  # ]:          0 :                 while (cnt && isspace(ch)) {
     831                 :          0 :                         ret = get_user(ch, ubuf++);
     832         [ #  # ]:          0 :                         if (ret)
     833                 :            :                                 goto out;
     834                 :          0 :                         read++;
     835                 :          0 :                         cnt--;
     836                 :            :                 }
     837                 :            : 
     838                 :            :                 /* only spaces were written */
     839         [ #  # ]:          0 :                 if (isspace(ch)) {
     840                 :          0 :                         *ppos += read;
     841                 :          0 :                         ret = read;
     842                 :          0 :                         goto out;
     843                 :            :                 }
     844                 :            : 
     845                 :          0 :                 parser->idx = 0;
     846                 :            :         }
     847                 :            : 
     848                 :            :         /* read the non-space input */
     849 [ #  # ][ #  # ]:          0 :         while (cnt && !isspace(ch)) {
     850         [ #  # ]:          0 :                 if (parser->idx < parser->size - 1)
     851                 :          0 :                         parser->buffer[parser->idx++] = ch;
     852                 :            :                 else {
     853                 :            :                         ret = -EINVAL;
     854                 :            :                         goto out;
     855                 :            :                 }
     856                 :          0 :                 ret = get_user(ch, ubuf++);
     857         [ #  # ]:          0 :                 if (ret)
     858                 :            :                         goto out;
     859                 :          0 :                 read++;
     860                 :          0 :                 cnt--;
     861                 :            :         }
     862                 :            : 
     863                 :            :         /* We either got finished input or we have to wait for another call. */
     864         [ #  # ]:          0 :         if (isspace(ch)) {
     865                 :          0 :                 parser->buffer[parser->idx] = 0;
     866                 :          0 :                 parser->cont = false;
     867         [ #  # ]:          0 :         } else if (parser->idx < parser->size - 1) {
     868                 :          0 :                 parser->cont = true;
     869                 :          0 :                 parser->buffer[parser->idx++] = ch;
     870                 :            :         } else {
     871                 :            :                 ret = -EINVAL;
     872                 :            :                 goto out;
     873                 :            :         }
     874                 :            : 
     875                 :          0 :         *ppos += read;
     876                 :          0 :         ret = read;
     877                 :            : 
     878                 :            : out:
     879                 :          0 :         return ret;
     880                 :            : }
     881                 :            : 
     882                 :          0 : ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
     883                 :            : {
     884                 :            :         int len;
     885                 :            :         int ret;
     886                 :            : 
     887         [ #  # ]:          0 :         if (!cnt)
     888                 :            :                 return 0;
     889                 :            : 
     890         [ #  # ]:          0 :         if (s->len <= s->readpos)
     891                 :            :                 return -EBUSY;
     892                 :            : 
     893                 :          0 :         len = s->len - s->readpos;
     894         [ #  # ]:          0 :         if (cnt > len)
     895                 :            :                 cnt = len;
     896                 :          0 :         ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
     897         [ #  # ]:          0 :         if (ret == cnt)
     898                 :            :                 return -EFAULT;
     899                 :            : 
     900                 :          0 :         cnt -= ret;
     901                 :            : 
     902                 :          0 :         s->readpos += cnt;
     903                 :          0 :         return cnt;
     904                 :            : }
     905                 :            : 
     906                 :          0 : static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
     907                 :            : {
     908                 :            :         int len;
     909                 :            : 
     910         [ #  # ]:          0 :         if (s->len <= s->readpos)
     911                 :            :                 return -EBUSY;
     912                 :            : 
     913                 :          0 :         len = s->len - s->readpos;
     914         [ #  # ]:          0 :         if (cnt > len)
     915                 :            :                 cnt = len;
     916                 :          0 :         memcpy(buf, s->buffer + s->readpos, cnt);
     917                 :            : 
     918                 :          0 :         s->readpos += cnt;
     919                 :          0 :         return cnt;
     920                 :            : }
     921                 :            : 
     922                 :            : /*
     923                 :            :  * ftrace_max_lock is used to protect the swapping of buffers
     924                 :            :  * when taking a max snapshot. The buffers themselves are
     925                 :            :  * protected by per_cpu spinlocks. But the action of the swap
     926                 :            :  * needs its own lock.
     927                 :            :  *
     928                 :            :  * This is defined as a arch_spinlock_t in order to help
     929                 :            :  * with performance when lockdep debugging is enabled.
     930                 :            :  *
     931                 :            :  * It is also used in other places outside the update_max_tr
     932                 :            :  * so it needs to be defined outside of the
     933                 :            :  * CONFIG_TRACER_MAX_TRACE.
     934                 :            :  */
     935                 :            : static arch_spinlock_t ftrace_max_lock =
     936                 :            :         (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
     937                 :            : 
     938                 :            : unsigned long __read_mostly     tracing_thresh;
     939                 :            : 
     940                 :            : #ifdef CONFIG_TRACER_MAX_TRACE
     941                 :            : unsigned long __read_mostly     tracing_max_latency;
     942                 :            : 
     943                 :            : /*
     944                 :            :  * Copy the new maximum trace into the separate maximum-trace
     945                 :            :  * structure. (this way the maximum trace is permanently saved,
     946                 :            :  * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
     947                 :            :  */
     948                 :            : static void
     949                 :            : __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
     950                 :            : {
     951                 :            :         struct trace_buffer *trace_buf = &tr->trace_buffer;
     952                 :            :         struct trace_buffer *max_buf = &tr->max_buffer;
     953                 :            :         struct trace_array_cpu *data = per_cpu_ptr(trace_buf->data, cpu);
     954                 :            :         struct trace_array_cpu *max_data = per_cpu_ptr(max_buf->data, cpu);
     955                 :            : 
     956                 :            :         max_buf->cpu = cpu;
     957                 :            :         max_buf->time_start = data->preempt_timestamp;
     958                 :            : 
     959                 :            :         max_data->saved_latency = tracing_max_latency;
     960                 :            :         max_data->critical_start = data->critical_start;
     961                 :            :         max_data->critical_end = data->critical_end;
     962                 :            : 
     963                 :            :         memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
     964                 :            :         max_data->pid = tsk->pid;
     965                 :            :         /*
     966                 :            :          * If tsk == current, then use current_uid(), as that does not use
     967                 :            :          * RCU. The irq tracer can be called out of RCU scope.
     968                 :            :          */
     969                 :            :         if (tsk == current)
     970                 :            :                 max_data->uid = current_uid();
     971                 :            :         else
     972                 :            :                 max_data->uid = task_uid(tsk);
     973                 :            : 
     974                 :            :         max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
     975                 :            :         max_data->policy = tsk->policy;
     976                 :            :         max_data->rt_priority = tsk->rt_priority;
     977                 :            : 
     978                 :            :         /* record this tasks comm */
     979                 :            :         tracing_record_cmdline(tsk);
     980                 :            : }
     981                 :            : 
     982                 :            : /**
     983                 :            :  * update_max_tr - snapshot all trace buffers from global_trace to max_tr
     984                 :            :  * @tr: tracer
     985                 :            :  * @tsk: the task with the latency
     986                 :            :  * @cpu: The cpu that initiated the trace.
     987                 :            :  *
     988                 :            :  * Flip the buffers between the @tr and the max_tr and record information
     989                 :            :  * about which task was the cause of this latency.
     990                 :            :  */
     991                 :            : void
     992                 :            : update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
     993                 :            : {
     994                 :            :         struct ring_buffer *buf;
     995                 :            : 
     996                 :            :         if (tr->stop_count)
     997                 :            :                 return;
     998                 :            : 
     999                 :            :         WARN_ON_ONCE(!irqs_disabled());
    1000                 :            : 
    1001                 :            :         if (!tr->allocated_snapshot) {
    1002                 :            :                 /* Only the nop tracer should hit this when disabling */
    1003                 :            :                 WARN_ON_ONCE(tr->current_trace != &nop_trace);
    1004                 :            :                 return;
    1005                 :            :         }
    1006                 :            : 
    1007                 :            :         arch_spin_lock(&ftrace_max_lock);
    1008                 :            : 
    1009                 :            :         buf = tr->trace_buffer.buffer;
    1010                 :            :         tr->trace_buffer.buffer = tr->max_buffer.buffer;
    1011                 :            :         tr->max_buffer.buffer = buf;
    1012                 :            : 
    1013                 :            :         __update_max_tr(tr, tsk, cpu);
    1014                 :            :         arch_spin_unlock(&ftrace_max_lock);
    1015                 :            : }
    1016                 :            : 
    1017                 :            : /**
    1018                 :            :  * update_max_tr_single - only copy one trace over, and reset the rest
    1019                 :            :  * @tr - tracer
    1020                 :            :  * @tsk - task with the latency
    1021                 :            :  * @cpu - the cpu of the buffer to copy.
    1022                 :            :  *
    1023                 :            :  * Flip the trace of a single CPU buffer between the @tr and the max_tr.
    1024                 :            :  */
    1025                 :            : void
    1026                 :            : update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
    1027                 :            : {
    1028                 :            :         int ret;
    1029                 :            : 
    1030                 :            :         if (tr->stop_count)
    1031                 :            :                 return;
    1032                 :            : 
    1033                 :            :         WARN_ON_ONCE(!irqs_disabled());
    1034                 :            :         if (!tr->allocated_snapshot) {
    1035                 :            :                 /* Only the nop tracer should hit this when disabling */
    1036                 :            :                 WARN_ON_ONCE(tr->current_trace != &nop_trace);
    1037                 :            :                 return;
    1038                 :            :         }
    1039                 :            : 
    1040                 :            :         arch_spin_lock(&ftrace_max_lock);
    1041                 :            : 
    1042                 :            :         ret = ring_buffer_swap_cpu(tr->max_buffer.buffer, tr->trace_buffer.buffer, cpu);
    1043                 :            : 
    1044                 :            :         if (ret == -EBUSY) {
    1045                 :            :                 /*
    1046                 :            :                  * We failed to swap the buffer due to a commit taking
    1047                 :            :                  * place on this CPU. We fail to record, but we reset
    1048                 :            :                  * the max trace buffer (no one writes directly to it)
    1049                 :            :                  * and flag that it failed.
    1050                 :            :                  */
    1051                 :            :                 trace_array_printk_buf(tr->max_buffer.buffer, _THIS_IP_,
    1052                 :            :                         "Failed to swap buffers due to commit in progress\n");
    1053                 :            :         }
    1054                 :            : 
    1055                 :            :         WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
    1056                 :            : 
    1057                 :            :         __update_max_tr(tr, tsk, cpu);
    1058                 :            :         arch_spin_unlock(&ftrace_max_lock);
    1059                 :            : }
    1060                 :            : #endif /* CONFIG_TRACER_MAX_TRACE */
    1061                 :            : 
    1062                 :          0 : static void default_wait_pipe(struct trace_iterator *iter)
    1063                 :            : {
    1064                 :            :         /* Iterators are static, they should be filled or empty */
    1065         [ #  # ]:          0 :         if (trace_buffer_iter(iter, iter->cpu_file))
    1066                 :          0 :                 return;
    1067                 :            : 
    1068                 :          0 :         ring_buffer_wait(iter->trace_buffer->buffer, iter->cpu_file);
    1069                 :            : }
    1070                 :            : 
    1071                 :            : #ifdef CONFIG_FTRACE_STARTUP_TEST
    1072                 :            : static int run_tracer_selftest(struct tracer *type)
    1073                 :            : {
    1074                 :            :         struct trace_array *tr = &global_trace;
    1075                 :            :         struct tracer *saved_tracer = tr->current_trace;
    1076                 :            :         int ret;
    1077                 :            : 
    1078                 :            :         if (!type->selftest || tracing_selftest_disabled)
    1079                 :            :                 return 0;
    1080                 :            : 
    1081                 :            :         /*
    1082                 :            :          * Run a selftest on this tracer.
    1083                 :            :          * Here we reset the trace buffer, and set the current
    1084                 :            :          * tracer to be this tracer. The tracer can then run some
    1085                 :            :          * internal tracing to verify that everything is in order.
    1086                 :            :          * If we fail, we do not register this tracer.
    1087                 :            :          */
    1088                 :            :         tracing_reset_online_cpus(&tr->trace_buffer);
    1089                 :            : 
    1090                 :            :         tr->current_trace = type;
    1091                 :            : 
    1092                 :            : #ifdef CONFIG_TRACER_MAX_TRACE
    1093                 :            :         if (type->use_max_tr) {
    1094                 :            :                 /* If we expanded the buffers, make sure the max is expanded too */
    1095                 :            :                 if (ring_buffer_expanded)
    1096                 :            :                         ring_buffer_resize(tr->max_buffer.buffer, trace_buf_size,
    1097                 :            :                                            RING_BUFFER_ALL_CPUS);
    1098                 :            :                 tr->allocated_snapshot = true;
    1099                 :            :         }
    1100                 :            : #endif
    1101                 :            : 
    1102                 :            :         /* the test is responsible for initializing and enabling */
    1103                 :            :         pr_info("Testing tracer %s: ", type->name);
    1104                 :            :         ret = type->selftest(type, tr);
    1105                 :            :         /* the test is responsible for resetting too */
    1106                 :            :         tr->current_trace = saved_tracer;
    1107                 :            :         if (ret) {
    1108                 :            :                 printk(KERN_CONT "FAILED!\n");
    1109                 :            :                 /* Add the warning after printing 'FAILED' */
    1110                 :            :                 WARN_ON(1);
    1111                 :            :                 return -1;
    1112                 :            :         }
    1113                 :            :         /* Only reset on passing, to avoid touching corrupted buffers */
    1114                 :            :         tracing_reset_online_cpus(&tr->trace_buffer);
    1115                 :            : 
    1116                 :            : #ifdef CONFIG_TRACER_MAX_TRACE
    1117                 :            :         if (type->use_max_tr) {
    1118                 :            :                 tr->allocated_snapshot = false;
    1119                 :            : 
    1120                 :            :                 /* Shrink the max buffer again */
    1121                 :            :                 if (ring_buffer_expanded)
    1122                 :            :                         ring_buffer_resize(tr->max_buffer.buffer, 1,
    1123                 :            :                                            RING_BUFFER_ALL_CPUS);
    1124                 :            :         }
    1125                 :            : #endif
    1126                 :            : 
    1127                 :            :         printk(KERN_CONT "PASSED\n");
    1128                 :            :         return 0;
    1129                 :            : }
    1130                 :            : #else
    1131                 :            : static inline int run_tracer_selftest(struct tracer *type)
    1132                 :            : {
    1133                 :            :         return 0;
    1134                 :            : }
    1135                 :            : #endif /* CONFIG_FTRACE_STARTUP_TEST */
    1136                 :            : 
    1137                 :            : /**
    1138                 :            :  * register_tracer - register a tracer with the ftrace system.
    1139                 :            :  * @type - the plugin for the tracer
    1140                 :            :  *
    1141                 :            :  * Register a new plugin tracer.
    1142                 :            :  */
    1143                 :          0 : int register_tracer(struct tracer *type)
    1144                 :            : {
    1145                 :            :         struct tracer *t;
    1146                 :            :         int ret = 0;
    1147                 :            : 
    1148         [ #  # ]:          0 :         if (!type->name) {
    1149                 :          0 :                 pr_info("Tracer must have a name\n");
    1150                 :          0 :                 return -1;
    1151                 :            :         }
    1152                 :            : 
    1153         [ #  # ]:          0 :         if (strlen(type->name) >= MAX_TRACER_SIZE) {
    1154                 :          0 :                 pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
    1155                 :          0 :                 return -1;
    1156                 :            :         }
    1157                 :            : 
    1158                 :          0 :         mutex_lock(&trace_types_lock);
    1159                 :            : 
    1160                 :          0 :         tracing_selftest_running = true;
    1161                 :            : 
    1162         [ #  # ]:          0 :         for (t = trace_types; t; t = t->next) {
    1163         [ #  # ]:          0 :                 if (strcmp(type->name, t->name) == 0) {
    1164                 :            :                         /* already found */
    1165                 :          0 :                         pr_info("Tracer %s already registered\n",
    1166                 :            :                                 type->name);
    1167                 :            :                         ret = -1;
    1168                 :          0 :                         goto out;
    1169                 :            :                 }
    1170                 :            :         }
    1171                 :            : 
    1172         [ #  # ]:          0 :         if (!type->set_flag)
    1173                 :          0 :                 type->set_flag = &dummy_set_flag;
    1174         [ #  # ]:          0 :         if (!type->flags)
    1175                 :          0 :                 type->flags = &dummy_tracer_flags;
    1176                 :            :         else
    1177         [ #  # ]:          0 :                 if (!type->flags->opts)
    1178                 :          0 :                         type->flags->opts = dummy_tracer_opt;
    1179         [ #  # ]:          0 :         if (!type->wait_pipe)
    1180                 :          0 :                 type->wait_pipe = default_wait_pipe;
    1181                 :            : 
    1182                 :            :         ret = run_tracer_selftest(type);
    1183                 :            :         if (ret < 0)
    1184                 :            :                 goto out;
    1185                 :            : 
    1186                 :          0 :         type->next = trace_types;
    1187                 :          0 :         trace_types = type;
    1188                 :            : 
    1189                 :            :  out:
    1190                 :          0 :         tracing_selftest_running = false;
    1191                 :          0 :         mutex_unlock(&trace_types_lock);
    1192                 :            : 
    1193 [ #  # ][ #  # ]:          0 :         if (ret || !default_bootup_tracer)
    1194                 :            :                 goto out_unlock;
    1195                 :            : 
    1196         [ #  # ]:          0 :         if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
    1197                 :            :                 goto out_unlock;
    1198                 :            : 
    1199                 :          0 :         printk(KERN_INFO "Starting tracer '%s'\n", type->name);
    1200                 :            :         /* Do we want this tracer to start on bootup? */
    1201                 :          0 :         tracing_set_tracer(type->name);
    1202                 :          0 :         default_bootup_tracer = NULL;
    1203                 :            :         /* disable other selftests, since this will break it. */
    1204                 :          0 :         tracing_selftest_disabled = true;
    1205                 :            : #ifdef CONFIG_FTRACE_STARTUP_TEST
    1206                 :            :         printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
    1207                 :            :                type->name);
    1208                 :            : #endif
    1209                 :            : 
    1210                 :            :  out_unlock:
    1211                 :          0 :         return ret;
    1212                 :            : }
    1213                 :            : 
    1214                 :          0 : void tracing_reset(struct trace_buffer *buf, int cpu)
    1215                 :            : {
    1216                 :          0 :         struct ring_buffer *buffer = buf->buffer;
    1217                 :            : 
    1218         [ #  # ]:          0 :         if (!buffer)
    1219                 :          0 :                 return;
    1220                 :            : 
    1221                 :          0 :         ring_buffer_record_disable(buffer);
    1222                 :            : 
    1223                 :            :         /* Make sure all commits have finished */
    1224                 :          0 :         synchronize_sched();
    1225                 :          0 :         ring_buffer_reset_cpu(buffer, cpu);
    1226                 :            : 
    1227                 :          0 :         ring_buffer_record_enable(buffer);
    1228                 :            : }
    1229                 :            : 
    1230                 :          0 : void tracing_reset_online_cpus(struct trace_buffer *buf)
    1231                 :            : {
    1232                 :          0 :         struct ring_buffer *buffer = buf->buffer;
    1233                 :            :         int cpu;
    1234                 :            : 
    1235         [ #  # ]:          0 :         if (!buffer)
    1236                 :          0 :                 return;
    1237                 :            : 
    1238                 :          0 :         ring_buffer_record_disable(buffer);
    1239                 :            : 
    1240                 :            :         /* Make sure all commits have finished */
    1241                 :          0 :         synchronize_sched();
    1242                 :            : 
    1243                 :          0 :         buf->time_start = buffer_ftrace_now(buf, buf->cpu);
    1244                 :            : 
    1245         [ #  # ]:          0 :         for_each_online_cpu(cpu)
    1246                 :          0 :                 ring_buffer_reset_cpu(buffer, cpu);
    1247                 :            : 
    1248                 :          0 :         ring_buffer_record_enable(buffer);
    1249                 :            : }
    1250                 :            : 
    1251                 :            : /* Must have trace_types_lock held */
    1252                 :          0 : void tracing_reset_all_online_cpus(void)
    1253                 :            : {
    1254                 :            :         struct trace_array *tr;
    1255                 :            : 
    1256         [ #  # ]:          0 :         list_for_each_entry(tr, &ftrace_trace_arrays, list) {
    1257                 :          0 :                 tracing_reset_online_cpus(&tr->trace_buffer);
    1258                 :            : #ifdef CONFIG_TRACER_MAX_TRACE
    1259                 :            :                 tracing_reset_online_cpus(&tr->max_buffer);
    1260                 :            : #endif
    1261                 :            :         }
    1262                 :          0 : }
    1263                 :            : 
    1264                 :            : #define SAVED_CMDLINES 128
    1265                 :            : #define NO_CMDLINE_MAP UINT_MAX
    1266                 :            : static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
    1267                 :            : static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
    1268                 :            : static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
    1269                 :            : static unsigned saved_tgids[SAVED_CMDLINES];
    1270                 :            : static int cmdline_idx;
    1271                 :            : static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
    1272                 :            : 
    1273                 :            : /* temporary disable recording */
    1274                 :            : static atomic_t trace_record_cmdline_disabled __read_mostly;
    1275                 :            : 
    1276                 :          0 : static void trace_init_cmdlines(void)
    1277                 :            : {
    1278                 :          0 :         memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
    1279                 :          0 :         memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
    1280                 :          0 :         cmdline_idx = 0;
    1281                 :          0 : }
    1282                 :            : 
    1283                 :          0 : int is_tracing_stopped(void)
    1284                 :            : {
    1285                 :          0 :         return global_trace.stop_count;
    1286                 :            : }
    1287                 :            : 
    1288                 :            : /**
    1289                 :            :  * tracing_start - quick start of the tracer
    1290                 :            :  *
    1291                 :            :  * If tracing is enabled but was stopped by tracing_stop,
    1292                 :            :  * this will start the tracer back up.
    1293                 :            :  */
    1294                 :          0 : void tracing_start(void)
    1295                 :            : {
    1296                 :            :         struct ring_buffer *buffer;
    1297                 :            :         unsigned long flags;
    1298                 :            : 
    1299         [ #  # ]:          0 :         if (tracing_disabled)
    1300                 :          0 :                 return;
    1301                 :            : 
    1302                 :          0 :         raw_spin_lock_irqsave(&global_trace.start_lock, flags);
    1303         [ #  # ]:          0 :         if (--global_trace.stop_count) {
    1304         [ #  # ]:          0 :                 if (global_trace.stop_count < 0) {
    1305                 :            :                         /* Someone screwed up their debugging */
    1306 [ #  # ][ #  # ]:          0 :                         WARN_ON_ONCE(1);
    1307                 :          0 :                         global_trace.stop_count = 0;
    1308                 :            :                 }
    1309                 :            :                 goto out;
    1310                 :            :         }
    1311                 :            : 
    1312                 :            :         /* Prevent the buffers from switching */
    1313                 :            :         arch_spin_lock(&ftrace_max_lock);
    1314                 :            : 
    1315                 :          0 :         buffer = global_trace.trace_buffer.buffer;
    1316         [ #  # ]:          0 :         if (buffer)
    1317                 :          0 :                 ring_buffer_record_enable(buffer);
    1318                 :            : 
    1319                 :            : #ifdef CONFIG_TRACER_MAX_TRACE
    1320                 :            :         buffer = global_trace.max_buffer.buffer;
    1321                 :            :         if (buffer)
    1322                 :            :                 ring_buffer_record_enable(buffer);
    1323                 :            : #endif
    1324                 :            : 
    1325                 :            :         arch_spin_unlock(&ftrace_max_lock);
    1326                 :            : 
    1327                 :            :         ftrace_start();
    1328                 :            :  out:
    1329                 :          0 :         raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
    1330                 :            : }
    1331                 :            : 
    1332                 :          0 : static void tracing_start_tr(struct trace_array *tr)
    1333                 :            : {
    1334                 :            :         struct ring_buffer *buffer;
    1335                 :            :         unsigned long flags;
    1336                 :            : 
    1337         [ #  # ]:          0 :         if (tracing_disabled)
    1338                 :            :                 return;
    1339                 :            : 
    1340                 :            :         /* If global, we need to also start the max tracer */
    1341         [ #  # ]:          0 :         if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
    1342                 :          0 :                 return tracing_start();
    1343                 :            : 
    1344                 :          0 :         raw_spin_lock_irqsave(&tr->start_lock, flags);
    1345                 :            : 
    1346         [ #  # ]:          0 :         if (--tr->stop_count) {
    1347         [ #  # ]:          0 :                 if (tr->stop_count < 0) {
    1348                 :            :                         /* Someone screwed up their debugging */
    1349 [ #  # ][ #  # ]:          0 :                         WARN_ON_ONCE(1);
    1350                 :          0 :                         tr->stop_count = 0;
    1351                 :            :                 }
    1352                 :            :                 goto out;
    1353                 :            :         }
    1354                 :            : 
    1355                 :          0 :         buffer = tr->trace_buffer.buffer;
    1356         [ #  # ]:          0 :         if (buffer)
    1357                 :          0 :                 ring_buffer_record_enable(buffer);
    1358                 :            : 
    1359                 :            :  out:
    1360                 :          0 :         raw_spin_unlock_irqrestore(&tr->start_lock, flags);
    1361                 :            : }
    1362                 :            : 
    1363                 :            : /**
    1364                 :            :  * tracing_stop - quick stop of the tracer
    1365                 :            :  *
    1366                 :            :  * Light weight way to stop tracing. Use in conjunction with
    1367                 :            :  * tracing_start.
    1368                 :            :  */
    1369                 :          0 : void tracing_stop(void)
    1370                 :            : {
    1371                 :            :         struct ring_buffer *buffer;
    1372                 :            :         unsigned long flags;
    1373                 :            : 
    1374                 :            :         ftrace_stop();
    1375                 :          0 :         raw_spin_lock_irqsave(&global_trace.start_lock, flags);
    1376         [ #  # ]:          0 :         if (global_trace.stop_count++)
    1377                 :            :                 goto out;
    1378                 :            : 
    1379                 :            :         /* Prevent the buffers from switching */
    1380                 :            :         arch_spin_lock(&ftrace_max_lock);
    1381                 :            : 
    1382                 :          0 :         buffer = global_trace.trace_buffer.buffer;
    1383         [ #  # ]:          0 :         if (buffer)
    1384                 :          0 :                 ring_buffer_record_disable(buffer);
    1385                 :            : 
    1386                 :            : #ifdef CONFIG_TRACER_MAX_TRACE
    1387                 :            :         buffer = global_trace.max_buffer.buffer;
    1388                 :            :         if (buffer)
    1389                 :            :                 ring_buffer_record_disable(buffer);
    1390                 :            : #endif
    1391                 :            : 
    1392                 :            :         arch_spin_unlock(&ftrace_max_lock);
    1393                 :            : 
    1394                 :            :  out:
    1395                 :          0 :         raw_spin_unlock_irqrestore(&global_trace.start_lock, flags);
    1396                 :          0 : }
    1397                 :            : 
    1398                 :          0 : static void tracing_stop_tr(struct trace_array *tr)
    1399                 :            : {
    1400                 :            :         struct ring_buffer *buffer;
    1401                 :            :         unsigned long flags;
    1402                 :            : 
    1403                 :            :         /* If global, we need to also stop the max tracer */
    1404         [ #  # ]:          0 :         if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
    1405                 :          0 :                 return tracing_stop();
    1406                 :            : 
    1407                 :          0 :         raw_spin_lock_irqsave(&tr->start_lock, flags);
    1408         [ #  # ]:          0 :         if (tr->stop_count++)
    1409                 :            :                 goto out;
    1410                 :            : 
    1411                 :          0 :         buffer = tr->trace_buffer.buffer;
    1412         [ #  # ]:          0 :         if (buffer)
    1413                 :          0 :                 ring_buffer_record_disable(buffer);
    1414                 :            : 
    1415                 :            :  out:
    1416                 :          0 :         raw_spin_unlock_irqrestore(&tr->start_lock, flags);
    1417                 :            : }
    1418                 :            : 
    1419                 :            : void trace_stop_cmdline_recording(void);
    1420                 :            : 
    1421                 :          0 : static void trace_save_cmdline(struct task_struct *tsk)
    1422                 :            : {
    1423                 :            :         unsigned pid, idx;
    1424                 :            : 
    1425 [ #  # ][ #  # ]:          0 :         if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
    1426                 :            :                 return;
    1427                 :            : 
    1428                 :            :         /*
    1429                 :            :          * It's not the end of the world if we don't get
    1430                 :            :          * the lock, but we also don't want to spin
    1431                 :            :          * nor do we want to disable interrupts,
    1432                 :            :          * so if we miss here, then better luck next time.
    1433                 :            :          */
    1434         [ #  # ]:          0 :         if (!arch_spin_trylock(&trace_cmdline_lock))
    1435                 :            :                 return;
    1436                 :            : 
    1437                 :          0 :         idx = map_pid_to_cmdline[tsk->pid];
    1438         [ #  # ]:          0 :         if (idx == NO_CMDLINE_MAP) {
    1439                 :          0 :                 idx = (cmdline_idx + 1) % SAVED_CMDLINES;
    1440                 :            : 
    1441                 :            :                 /*
    1442                 :            :                  * Check whether the cmdline buffer at idx has a pid
    1443                 :            :                  * mapped. We are going to overwrite that entry so we
    1444                 :            :                  * need to clear the map_pid_to_cmdline. Otherwise we
    1445                 :            :                  * would read the new comm for the old pid.
    1446                 :            :                  */
    1447                 :          0 :                 pid = map_cmdline_to_pid[idx];
    1448         [ #  # ]:          0 :                 if (pid != NO_CMDLINE_MAP)
    1449                 :          0 :                         map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
    1450                 :            : 
    1451                 :          0 :                 map_cmdline_to_pid[idx] = tsk->pid;
    1452                 :          0 :                 map_pid_to_cmdline[tsk->pid] = idx;
    1453                 :            : 
    1454                 :          0 :                 cmdline_idx = idx;
    1455                 :            :         }
    1456                 :            : 
    1457                 :          0 :         memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
    1458                 :          0 :         saved_tgids[idx] = tsk->tgid;
    1459                 :            : 
    1460                 :            :         arch_spin_unlock(&trace_cmdline_lock);
    1461                 :            : }
    1462                 :            : 
    1463                 :          0 : void trace_find_cmdline(int pid, char comm[])
    1464                 :            : {
    1465                 :            :         unsigned map;
    1466                 :            : 
    1467         [ #  # ]:          0 :         if (!pid) {
    1468                 :          0 :                 strcpy(comm, "<idle>");
    1469                 :          0 :                 return;
    1470                 :            :         }
    1471                 :            : 
    1472 [ #  # ][ #  # ]:          0 :         if (WARN_ON_ONCE(pid < 0)) {
         [ #  # ][ #  # ]
    1473                 :          0 :                 strcpy(comm, "<XXX>");
    1474                 :          0 :                 return;
    1475                 :            :         }
    1476                 :            : 
    1477         [ #  # ]:          0 :         if (pid > PID_MAX_DEFAULT) {
    1478                 :          0 :                 strcpy(comm, "<...>");
    1479                 :          0 :                 return;
    1480                 :            :         }
    1481                 :            : 
    1482                 :          0 :         preempt_disable();
    1483                 :            :         arch_spin_lock(&trace_cmdline_lock);
    1484                 :          0 :         map = map_pid_to_cmdline[pid];
    1485         [ #  # ]:          0 :         if (map != NO_CMDLINE_MAP)
    1486                 :          0 :                 strcpy(comm, saved_cmdlines[map]);
    1487                 :            :         else
    1488                 :          0 :                 strcpy(comm, "<...>");
    1489                 :            : 
    1490                 :            :         arch_spin_unlock(&trace_cmdline_lock);
    1491                 :          0 :         preempt_enable();
    1492                 :            : }
    1493                 :            : 
    1494                 :          0 : int trace_find_tgid(int pid)
    1495                 :            : {
    1496                 :            :         unsigned map;
    1497                 :            :         int tgid;
    1498                 :            : 
    1499                 :          0 :         preempt_disable();
    1500                 :            :         arch_spin_lock(&trace_cmdline_lock);
    1501                 :          0 :         map = map_pid_to_cmdline[pid];
    1502         [ #  # ]:          0 :         if (map != NO_CMDLINE_MAP)
    1503                 :          0 :                 tgid = saved_tgids[map];
    1504                 :            :         else
    1505                 :            :                 tgid = -1;
    1506                 :            : 
    1507                 :            :         arch_spin_unlock(&trace_cmdline_lock);
    1508                 :          0 :         preempt_enable();
    1509                 :            : 
    1510                 :          0 :         return tgid;
    1511                 :            : }
    1512                 :            : 
    1513                 :          0 : void tracing_record_cmdline(struct task_struct *tsk)
    1514                 :            : {
    1515 [ #  # ][ #  # ]:          0 :         if (atomic_read(&trace_record_cmdline_disabled) || !tracing_is_on())
    1516                 :            :                 return;
    1517                 :            : 
    1518         [ #  # ]:          0 :         if (!__this_cpu_read(trace_cmdline_save))
    1519                 :            :                 return;
    1520                 :            : 
    1521                 :          0 :         __this_cpu_write(trace_cmdline_save, false);
    1522                 :            : 
    1523                 :          0 :         trace_save_cmdline(tsk);
    1524                 :            : }
    1525                 :            : 
    1526                 :            : void
    1527                 :          0 : tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
    1528                 :            :                              int pc)
    1529                 :            : {
    1530                 :          0 :         struct task_struct *tsk = current;
    1531                 :            : 
    1532                 :          0 :         entry->preempt_count         = pc & 0xff;
    1533         [ #  # ]:          0 :         entry->pid                   = (tsk) ? tsk->pid : 0;
    1534 [ #  # ][ #  # ]:          0 :         entry->flags =
                 [ #  # ]
    1535                 :            : #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
    1536                 :          0 :                 (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
    1537                 :            : #else
    1538                 :            :                 TRACE_FLAG_IRQS_NOSUPPORT |
    1539                 :            : #endif
    1540                 :          0 :                 ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
    1541                 :          0 :                 ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
    1542                 :            :                 (tif_need_resched() ? TRACE_FLAG_NEED_RESCHED : 0) |
    1543                 :            :                 (test_preempt_need_resched() ? TRACE_FLAG_PREEMPT_RESCHED : 0);
    1544                 :          0 : }
    1545                 :            : EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
    1546                 :            : 
    1547                 :            : struct ring_buffer_event *
    1548                 :          0 : trace_buffer_lock_reserve(struct ring_buffer *buffer,
    1549                 :            :                           int type,
    1550                 :            :                           unsigned long len,
    1551                 :            :                           unsigned long flags, int pc)
    1552                 :            : {
    1553                 :            :         struct ring_buffer_event *event;
    1554                 :            : 
    1555                 :          0 :         event = ring_buffer_lock_reserve(buffer, len);
    1556         [ #  # ]:          0 :         if (event != NULL) {
    1557                 :          0 :                 struct trace_entry *ent = ring_buffer_event_data(event);
    1558                 :            : 
    1559                 :          0 :                 tracing_generic_entry_update(ent, flags, pc);
    1560                 :          0 :                 ent->type = type;
    1561                 :            :         }
    1562                 :            : 
    1563                 :          0 :         return event;
    1564                 :            : }
    1565                 :            : 
    1566                 :            : void
    1567                 :          0 : __buffer_unlock_commit(struct ring_buffer *buffer, struct ring_buffer_event *event)
    1568                 :            : {
    1569                 :          0 :         __this_cpu_write(trace_cmdline_save, true);
    1570                 :          0 :         ring_buffer_unlock_commit(buffer, event);
    1571                 :          0 : }
    1572                 :            : 
    1573                 :            : static inline void
    1574                 :            : __trace_buffer_unlock_commit(struct ring_buffer *buffer,
    1575                 :            :                              struct ring_buffer_event *event,
    1576                 :            :                              unsigned long flags, int pc)
    1577                 :            : {
    1578                 :          0 :         __buffer_unlock_commit(buffer, event);
    1579                 :            : 
    1580                 :          0 :         ftrace_trace_stack(buffer, flags, 6, pc);
    1581                 :          0 :         ftrace_trace_userstack(buffer, flags, pc);
    1582                 :            : }
    1583                 :            : 
    1584                 :          0 : void trace_buffer_unlock_commit(struct ring_buffer *buffer,
    1585                 :            :                                 struct ring_buffer_event *event,
    1586                 :            :                                 unsigned long flags, int pc)
    1587                 :            : {
    1588                 :            :         __trace_buffer_unlock_commit(buffer, event, flags, pc);
    1589                 :          0 : }
    1590                 :            : EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit);
    1591                 :            : 
    1592                 :            : struct ring_buffer_event *
    1593                 :          0 : trace_event_buffer_lock_reserve(struct ring_buffer **current_rb,
    1594                 :            :                           struct ftrace_event_file *ftrace_file,
    1595                 :            :                           int type, unsigned long len,
    1596                 :            :                           unsigned long flags, int pc)
    1597                 :            : {
    1598                 :          0 :         *current_rb = ftrace_file->tr->trace_buffer.buffer;
    1599                 :          0 :         return trace_buffer_lock_reserve(*current_rb,
    1600                 :            :                                          type, len, flags, pc);
    1601                 :            : }
    1602                 :            : EXPORT_SYMBOL_GPL(trace_event_buffer_lock_reserve);
    1603                 :            : 
    1604                 :            : struct ring_buffer_event *
    1605                 :          0 : trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
    1606                 :            :                                   int type, unsigned long len,
    1607                 :            :                                   unsigned long flags, int pc)
    1608                 :            : {
    1609                 :          0 :         *current_rb = global_trace.trace_buffer.buffer;
    1610                 :          0 :         return trace_buffer_lock_reserve(*current_rb,
    1611                 :            :                                          type, len, flags, pc);
    1612                 :            : }
    1613                 :            : EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
    1614                 :            : 
    1615                 :          0 : void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
    1616                 :            :                                         struct ring_buffer_event *event,
    1617                 :            :                                         unsigned long flags, int pc)
    1618                 :            : {
    1619                 :            :         __trace_buffer_unlock_commit(buffer, event, flags, pc);
    1620                 :          0 : }
    1621                 :            : EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
    1622                 :            : 
    1623                 :          0 : void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer,
    1624                 :            :                                      struct ring_buffer_event *event,
    1625                 :            :                                      unsigned long flags, int pc,
    1626                 :            :                                      struct pt_regs *regs)
    1627                 :            : {
    1628                 :            :         __buffer_unlock_commit(buffer, event);
    1629                 :            : 
    1630                 :            :         ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
    1631                 :          0 :         ftrace_trace_userstack(buffer, flags, pc);
    1632                 :          0 : }
    1633                 :            : EXPORT_SYMBOL_GPL(trace_buffer_unlock_commit_regs);
    1634                 :            : 
    1635                 :          0 : void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
    1636                 :            :                                          struct ring_buffer_event *event)
    1637                 :            : {
    1638                 :          0 :         ring_buffer_discard_commit(buffer, event);
    1639                 :          0 : }
    1640                 :            : EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
    1641                 :            : 
    1642                 :            : void
    1643                 :          0 : trace_function(struct trace_array *tr,
    1644                 :            :                unsigned long ip, unsigned long parent_ip, unsigned long flags,
    1645                 :            :                int pc)
    1646                 :            : {
    1647                 :            :         struct ftrace_event_call *call = &event_function;
    1648                 :          0 :         struct ring_buffer *buffer = tr->trace_buffer.buffer;
    1649                 :            :         struct ring_buffer_event *event;
    1650                 :            :         struct ftrace_entry *entry;
    1651                 :            : 
    1652                 :            :         /* If we are reading the ring buffer, don't trace */
    1653         [ #  # ]:          0 :         if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
    1654                 :            :                 return;
    1655                 :            : 
    1656                 :          0 :         event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
    1657                 :            :                                           flags, pc);
    1658         [ #  # ]:          0 :         if (!event)
    1659                 :            :                 return;
    1660                 :          0 :         entry   = ring_buffer_event_data(event);
    1661                 :          0 :         entry->ip                    = ip;
    1662                 :          0 :         entry->parent_ip             = parent_ip;
    1663                 :            : 
    1664         [ #  # ]:          0 :         if (!call_filter_check_discard(call, entry, buffer, event))
    1665                 :            :                 __buffer_unlock_commit(buffer, event);
    1666                 :            : }
    1667                 :            : 
    1668                 :            : #ifdef CONFIG_STACKTRACE
    1669                 :            : 
    1670                 :            : #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
    1671                 :            : struct ftrace_stack {
    1672                 :            :         unsigned long           calls[FTRACE_STACK_MAX_ENTRIES];
    1673                 :            : };
    1674                 :            : 
    1675                 :            : static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
    1676                 :            : static DEFINE_PER_CPU(int, ftrace_stack_reserve);
    1677                 :            : 
    1678                 :          0 : static void __ftrace_trace_stack(struct ring_buffer *buffer,
    1679                 :            :                                  unsigned long flags,
    1680                 :            :                                  int skip, int pc, struct pt_regs *regs)
    1681                 :            : {
    1682                 :            :         struct ftrace_event_call *call = &event_kernel_stack;
    1683                 :            :         struct ring_buffer_event *event;
    1684                 :            :         struct stack_entry *entry;
    1685                 :            :         struct stack_trace trace;
    1686                 :            :         int use_stack;
    1687                 :            :         int size = FTRACE_STACK_ENTRIES;
    1688                 :            : 
    1689                 :          0 :         trace.nr_entries        = 0;
    1690                 :          0 :         trace.skip              = skip;
    1691                 :            : 
    1692                 :            :         /*
    1693                 :            :          * Since events can happen in NMIs there's no safe way to
    1694                 :            :          * use the per cpu ftrace_stacks. We reserve it and if an interrupt
    1695                 :            :          * or NMI comes in, it will just have to use the default
    1696                 :            :          * FTRACE_STACK_SIZE.
    1697                 :            :          */
    1698                 :          0 :         preempt_disable_notrace();
    1699                 :            : 
    1700                 :          0 :         use_stack = __this_cpu_inc_return(ftrace_stack_reserve);
    1701                 :            :         /*
    1702                 :            :          * We don't need any atomic variables, just a barrier.
    1703                 :            :          * If an interrupt comes in, we don't care, because it would
    1704                 :            :          * have exited and put the counter back to what we want.
    1705                 :            :          * We just need a barrier to keep gcc from moving things
    1706                 :            :          * around.
    1707                 :            :          */
    1708                 :          0 :         barrier();
    1709         [ #  # ]:          0 :         if (use_stack == 1) {
    1710                 :          0 :                 trace.entries           = &__get_cpu_var(ftrace_stack).calls[0];
    1711                 :          0 :                 trace.max_entries       = FTRACE_STACK_MAX_ENTRIES;
    1712                 :            : 
    1713         [ #  # ]:          0 :                 if (regs)
    1714                 :          0 :                         save_stack_trace_regs(regs, &trace);
    1715                 :            :                 else
    1716                 :          0 :                         save_stack_trace(&trace);
    1717                 :            : 
    1718         [ #  # ]:          0 :                 if (trace.nr_entries > size)
    1719                 :          0 :                         size = trace.nr_entries;
    1720                 :            :         } else
    1721                 :            :                 /* From now on, use_stack is a boolean */
    1722                 :            :                 use_stack = 0;
    1723                 :            : 
    1724                 :          0 :         size *= sizeof(unsigned long);
    1725                 :            : 
    1726                 :          0 :         event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
    1727                 :          0 :                                           sizeof(*entry) + size, flags, pc);
    1728         [ #  # ]:          0 :         if (!event)
    1729                 :            :                 goto out;
    1730                 :          0 :         entry = ring_buffer_event_data(event);
    1731                 :            : 
    1732         [ #  # ]:          0 :         memset(&entry->caller, 0, size);
    1733                 :            : 
    1734         [ #  # ]:          0 :         if (use_stack)
    1735                 :          0 :                 memcpy(&entry->caller, trace.entries,
    1736                 :          0 :                        trace.nr_entries * sizeof(unsigned long));
    1737                 :            :         else {
    1738                 :          0 :                 trace.max_entries       = FTRACE_STACK_ENTRIES;
    1739                 :          0 :                 trace.entries           = entry->caller;
    1740         [ #  # ]:          0 :                 if (regs)
    1741                 :          0 :                         save_stack_trace_regs(regs, &trace);
    1742                 :            :                 else
    1743                 :          0 :                         save_stack_trace(&trace);
    1744                 :            :         }
    1745                 :            : 
    1746                 :          0 :         entry->size = trace.nr_entries;
    1747                 :            : 
    1748         [ #  # ]:          0 :         if (!call_filter_check_discard(call, entry, buffer, event))
    1749                 :            :                 __buffer_unlock_commit(buffer, event);
    1750                 :            : 
    1751                 :            :  out:
    1752                 :            :         /* Again, don't let gcc optimize things here */
    1753                 :          0 :         barrier();
    1754                 :          0 :         __this_cpu_dec(ftrace_stack_reserve);
    1755                 :          0 :         preempt_enable_notrace();
    1756                 :            : 
    1757                 :          0 : }
    1758                 :            : 
    1759                 :          0 : void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
    1760                 :            :                              int skip, int pc, struct pt_regs *regs)
    1761                 :            : {
    1762   [ #  #  #  # ]:          0 :         if (!(trace_flags & TRACE_ITER_STACKTRACE))
    1763                 :          0 :                 return;
    1764                 :            : 
    1765                 :          0 :         __ftrace_trace_stack(buffer, flags, skip, pc, regs);
    1766                 :            : }
    1767                 :            : 
    1768                 :          0 : void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
    1769                 :            :                         int skip, int pc)
    1770                 :            : {
    1771   [ #  #  #  # ]:          0 :         if (!(trace_flags & TRACE_ITER_STACKTRACE))
                 [ #  # ]
    1772                 :          0 :                 return;
    1773                 :            : 
    1774                 :          0 :         __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
    1775                 :            : }
    1776                 :            : 
    1777                 :          0 : void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
    1778                 :            :                    int pc)
    1779                 :            : {
    1780                 :          0 :         __ftrace_trace_stack(tr->trace_buffer.buffer, flags, skip, pc, NULL);
    1781                 :          0 : }
    1782                 :            : 
    1783                 :            : /**
    1784                 :            :  * trace_dump_stack - record a stack back trace in the trace buffer
    1785                 :            :  * @skip: Number of functions to skip (helper handlers)
    1786                 :            :  */
    1787                 :          0 : void trace_dump_stack(int skip)
    1788                 :            : {
    1789                 :            :         unsigned long flags;
    1790                 :            : 
    1791 [ #  # ][ #  # ]:          0 :         if (tracing_disabled || tracing_selftest_running)
    1792                 :          0 :                 return;
    1793                 :            : 
    1794                 :            :         local_save_flags(flags);
    1795                 :            : 
    1796                 :            :         /*
    1797                 :            :          * Skip 3 more, seems to get us at the caller of
    1798                 :            :          * this function.
    1799                 :            :          */
    1800                 :          0 :         skip += 3;
    1801                 :          0 :         __ftrace_trace_stack(global_trace.trace_buffer.buffer,
    1802                 :            :                              flags, skip, preempt_count(), NULL);
    1803                 :            : }
    1804                 :            : 
    1805                 :            : static DEFINE_PER_CPU(int, user_stack_count);
    1806                 :            : 
    1807                 :            : void
    1808                 :          0 : ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
    1809                 :            : {
    1810                 :            :         struct ftrace_event_call *call = &event_user_stack;
    1811                 :            :         struct ring_buffer_event *event;
    1812                 :            :         struct userstack_entry *entry;
    1813                 :            :         struct stack_trace trace;
    1814                 :            : 
    1815         [ #  # ]:          0 :         if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
    1816                 :            :                 return;
    1817                 :            : 
    1818                 :            :         /*
    1819                 :            :          * NMIs can not handle page faults, even with fix ups.
    1820                 :            :          * The save user stack can (and often does) fault.
    1821                 :            :          */
    1822         [ #  # ]:          0 :         if (unlikely(in_nmi()))
    1823                 :            :                 return;
    1824                 :            : 
    1825                 :            :         /*
    1826                 :            :          * prevent recursion, since the user stack tracing may
    1827                 :            :          * trigger other kernel events.
    1828                 :            :          */
    1829                 :          0 :         preempt_disable();
    1830         [ #  # ]:          0 :         if (__this_cpu_read(user_stack_count))
    1831                 :            :                 goto out;
    1832                 :            : 
    1833                 :          0 :         __this_cpu_inc(user_stack_count);
    1834                 :            : 
    1835                 :          0 :         event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
    1836                 :            :                                           sizeof(*entry), flags, pc);
    1837         [ #  # ]:          0 :         if (!event)
    1838                 :            :                 goto out_drop_count;
    1839                 :          0 :         entry   = ring_buffer_event_data(event);
    1840                 :            : 
    1841                 :          0 :         entry->tgid          = current->tgid;
    1842                 :          0 :         memset(&entry->caller, 0, sizeof(entry->caller));
    1843                 :            : 
    1844                 :            :         trace.nr_entries        = 0;
    1845                 :            :         trace.max_entries       = FTRACE_STACK_ENTRIES;
    1846                 :            :         trace.skip              = 0;
    1847                 :            :         trace.entries           = entry->caller;
    1848                 :            : 
    1849                 :            :         save_stack_trace_user(&trace);
    1850         [ #  # ]:          0 :         if (!call_filter_check_discard(call, entry, buffer, event))
    1851                 :            :                 __buffer_unlock_commit(buffer, event);
    1852                 :            : 
    1853                 :            :  out_drop_count:
    1854                 :          0 :         __this_cpu_dec(user_stack_count);
    1855                 :            :  out:
    1856                 :          0 :         preempt_enable();
    1857                 :            : }
    1858                 :            : 
    1859                 :            : #ifdef UNUSED
    1860                 :            : static void __trace_userstack(struct trace_array *tr, unsigned long flags)
    1861                 :            : {
    1862                 :            :         ftrace_trace_userstack(tr, flags, preempt_count());
    1863                 :            : }
    1864                 :            : #endif /* UNUSED */
    1865                 :            : 
    1866                 :            : #endif /* CONFIG_STACKTRACE */
    1867                 :            : 
    1868                 :            : /* created for use with alloc_percpu */
    1869                 :            : struct trace_buffer_struct {
    1870                 :            :         char buffer[TRACE_BUF_SIZE];
    1871                 :            : };
    1872                 :            : 
    1873                 :            : static struct trace_buffer_struct *trace_percpu_buffer;
    1874                 :            : static struct trace_buffer_struct *trace_percpu_sirq_buffer;
    1875                 :            : static struct trace_buffer_struct *trace_percpu_irq_buffer;
    1876                 :            : static struct trace_buffer_struct *trace_percpu_nmi_buffer;
    1877                 :            : 
    1878                 :            : /*
    1879                 :            :  * The buffer used is dependent on the context. There is a per cpu
    1880                 :            :  * buffer for normal context, softirq contex, hard irq context and
    1881                 :            :  * for NMI context. Thise allows for lockless recording.
    1882                 :            :  *
    1883                 :            :  * Note, if the buffers failed to be allocated, then this returns NULL
    1884                 :            :  */
    1885                 :          0 : static char *get_trace_buf(void)
    1886                 :            : {
    1887                 :            :         struct trace_buffer_struct *percpu_buffer;
    1888                 :            : 
    1889                 :            :         /*
    1890                 :            :          * If we have allocated per cpu buffers, then we do not
    1891                 :            :          * need to do any locking.
    1892                 :            :          */
    1893         [ #  # ]:          0 :         if (in_nmi())
    1894                 :          0 :                 percpu_buffer = trace_percpu_nmi_buffer;
    1895         [ #  # ]:          0 :         else if (in_irq())
    1896                 :          0 :                 percpu_buffer = trace_percpu_irq_buffer;
    1897         [ #  # ]:          0 :         else if (in_softirq())
    1898                 :          0 :                 percpu_buffer = trace_percpu_sirq_buffer;
    1899                 :            :         else
    1900                 :          0 :                 percpu_buffer = trace_percpu_buffer;
    1901                 :            : 
    1902         [ #  # ]:          0 :         if (!percpu_buffer)
    1903                 :            :                 return NULL;
    1904                 :            : 
    1905                 :          0 :         return this_cpu_ptr(&percpu_buffer->buffer[0]);
    1906                 :            : }
    1907                 :            : 
    1908                 :          0 : static int alloc_percpu_trace_buffer(void)
    1909                 :            : {
    1910                 :            :         struct trace_buffer_struct *buffers;
    1911                 :            :         struct trace_buffer_struct *sirq_buffers;
    1912                 :            :         struct trace_buffer_struct *irq_buffers;
    1913                 :            :         struct trace_buffer_struct *nmi_buffers;
    1914                 :            : 
    1915                 :          0 :         buffers = alloc_percpu(struct trace_buffer_struct);
    1916         [ #  # ]:          0 :         if (!buffers)
    1917                 :            :                 goto err_warn;
    1918                 :            : 
    1919                 :          0 :         sirq_buffers = alloc_percpu(struct trace_buffer_struct);
    1920         [ #  # ]:          0 :         if (!sirq_buffers)
    1921                 :            :                 goto err_sirq;
    1922                 :            : 
    1923                 :          0 :         irq_buffers = alloc_percpu(struct trace_buffer_struct);
    1924         [ #  # ]:          0 :         if (!irq_buffers)
    1925                 :            :                 goto err_irq;
    1926                 :            : 
    1927                 :          0 :         nmi_buffers = alloc_percpu(struct trace_buffer_struct);
    1928         [ #  # ]:          0 :         if (!nmi_buffers)
    1929                 :            :                 goto err_nmi;
    1930                 :            : 
    1931                 :          0 :         trace_percpu_buffer = buffers;
    1932                 :          0 :         trace_percpu_sirq_buffer = sirq_buffers;
    1933                 :          0 :         trace_percpu_irq_buffer = irq_buffers;
    1934                 :          0 :         trace_percpu_nmi_buffer = nmi_buffers;
    1935                 :            : 
    1936                 :          0 :         return 0;
    1937                 :            : 
    1938                 :            :  err_nmi:
    1939                 :          0 :         free_percpu(irq_buffers);
    1940                 :            :  err_irq:
    1941                 :          0 :         free_percpu(sirq_buffers);
    1942                 :            :  err_sirq:
    1943                 :          0 :         free_percpu(buffers);
    1944                 :            :  err_warn:
    1945                 :          0 :         WARN(1, "Could not allocate percpu trace_printk buffer");
    1946                 :          0 :         return -ENOMEM;
    1947                 :            : }
    1948                 :            : 
    1949                 :            : static int buffers_allocated;
    1950                 :            : 
    1951                 :          0 : void trace_printk_init_buffers(void)
    1952                 :            : {
    1953         [ #  # ]:          0 :         if (buffers_allocated)
    1954                 :            :                 return;
    1955                 :            : 
    1956         [ #  # ]:          0 :         if (alloc_percpu_trace_buffer())
    1957                 :            :                 return;
    1958                 :            : 
    1959                 :          0 :         pr_info("ftrace: Allocated trace_printk buffers\n");
    1960                 :            : 
    1961                 :            :         /* Expand the buffers to set size */
    1962                 :          0 :         tracing_update_buffers();
    1963                 :            : 
    1964                 :          0 :         buffers_allocated = 1;
    1965                 :            : 
    1966                 :            :         /*
    1967                 :            :          * trace_printk_init_buffers() can be called by modules.
    1968                 :            :          * If that happens, then we need to start cmdline recording
    1969                 :            :          * directly here. If the global_trace.buffer is already
    1970                 :            :          * allocated here, then this was called by module code.
    1971                 :            :          */
    1972         [ #  # ]:          0 :         if (global_trace.trace_buffer.buffer)
    1973                 :          0 :                 tracing_start_cmdline_record();
    1974                 :            : }
    1975                 :            : 
    1976                 :          0 : void trace_printk_start_comm(void)
    1977                 :            : {
    1978                 :            :         /* Start tracing comms if trace printk is set */
    1979         [ #  # ]:          0 :         if (!buffers_allocated)
    1980                 :          0 :                 return;
    1981                 :          0 :         tracing_start_cmdline_record();
    1982                 :            : }
    1983                 :            : 
    1984                 :          0 : static void trace_printk_start_stop_comm(int enabled)
    1985                 :            : {
    1986         [ #  # ]:          0 :         if (!buffers_allocated)
    1987                 :          0 :                 return;
    1988                 :            : 
    1989         [ #  # ]:          0 :         if (enabled)
    1990                 :          0 :                 tracing_start_cmdline_record();
    1991                 :            :         else
    1992                 :          0 :                 tracing_stop_cmdline_record();
    1993                 :            : }
    1994                 :            : 
    1995                 :            : /**
    1996                 :            :  * trace_vbprintk - write binary msg to tracing buffer
    1997                 :            :  *
    1998                 :            :  */
    1999                 :          0 : int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
    2000                 :            : {
    2001                 :            :         struct ftrace_event_call *call = &event_bprint;
    2002                 :            :         struct ring_buffer_event *event;
    2003                 :            :         struct ring_buffer *buffer;
    2004                 :            :         struct trace_array *tr = &global_trace;
    2005                 :            :         struct bprint_entry *entry;
    2006                 :            :         unsigned long flags;
    2007                 :            :         char *tbuffer;
    2008                 :            :         int len = 0, size, pc;
    2009                 :            : 
    2010 [ #  # ][ #  # ]:          0 :         if (unlikely(tracing_selftest_running || tracing_disabled))
    2011                 :            :                 return 0;
    2012                 :            : 
    2013                 :            :         /* Don't pollute graph traces with trace_vprintk internals */
    2014                 :            :         pause_graph_tracing();
    2015                 :            : 
    2016                 :            :         pc = preempt_count();
    2017                 :          0 :         preempt_disable_notrace();
    2018                 :            : 
    2019                 :          0 :         tbuffer = get_trace_buf();
    2020         [ #  # ]:          0 :         if (!tbuffer) {
    2021                 :            :                 len = 0;
    2022                 :            :                 goto out;
    2023                 :            :         }
    2024                 :            : 
    2025                 :          0 :         len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
    2026                 :            : 
    2027         [ #  # ]:          0 :         if (len > TRACE_BUF_SIZE/sizeof(int) || len < 0)
    2028                 :            :                 goto out;
    2029                 :            : 
    2030                 :            :         local_save_flags(flags);
    2031                 :          0 :         size = sizeof(*entry) + sizeof(u32) * len;
    2032                 :          0 :         buffer = tr->trace_buffer.buffer;
    2033                 :          0 :         event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
    2034                 :            :                                           flags, pc);
    2035         [ #  # ]:          0 :         if (!event)
    2036                 :            :                 goto out;
    2037                 :          0 :         entry = ring_buffer_event_data(event);
    2038                 :          0 :         entry->ip                    = ip;
    2039                 :          0 :         entry->fmt                   = fmt;
    2040                 :            : 
    2041                 :          0 :         memcpy(entry->buf, tbuffer, sizeof(u32) * len);
    2042         [ #  # ]:          0 :         if (!call_filter_check_discard(call, entry, buffer, event)) {
    2043                 :            :                 __buffer_unlock_commit(buffer, event);
    2044                 :            :                 ftrace_trace_stack(buffer, flags, 6, pc);
    2045                 :            :         }
    2046                 :            : 
    2047                 :            : out:
    2048                 :          0 :         preempt_enable_notrace();
    2049                 :            :         unpause_graph_tracing();
    2050                 :            : 
    2051                 :          0 :         return len;
    2052                 :            : }
    2053                 :            : EXPORT_SYMBOL_GPL(trace_vbprintk);
    2054                 :            : 
    2055                 :            : static int
    2056                 :          0 : __trace_array_vprintk(struct ring_buffer *buffer,
    2057                 :            :                       unsigned long ip, const char *fmt, va_list args)
    2058                 :            : {
    2059                 :            :         struct ftrace_event_call *call = &event_print;
    2060                 :            :         struct ring_buffer_event *event;
    2061                 :            :         int len = 0, size, pc;
    2062                 :            :         struct print_entry *entry;
    2063                 :            :         unsigned long flags;
    2064                 :            :         char *tbuffer;
    2065                 :            : 
    2066 [ #  # ][ #  # ]:          0 :         if (tracing_disabled || tracing_selftest_running)
    2067                 :            :                 return 0;
    2068                 :            : 
    2069                 :            :         /* Don't pollute graph traces with trace_vprintk internals */
    2070                 :            :         pause_graph_tracing();
    2071                 :            : 
    2072                 :            :         pc = preempt_count();
    2073                 :          0 :         preempt_disable_notrace();
    2074                 :            : 
    2075                 :            : 
    2076                 :          0 :         tbuffer = get_trace_buf();
    2077         [ #  # ]:          0 :         if (!tbuffer) {
    2078                 :            :                 len = 0;
    2079                 :            :                 goto out;
    2080                 :            :         }
    2081                 :            : 
    2082                 :          0 :         len = vsnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
    2083         [ #  # ]:          0 :         if (len > TRACE_BUF_SIZE)
    2084                 :            :                 goto out;
    2085                 :            : 
    2086                 :            :         local_save_flags(flags);
    2087                 :          0 :         size = sizeof(*entry) + len + 1;
    2088                 :          0 :         event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
    2089                 :            :                                           flags, pc);
    2090         [ #  # ]:          0 :         if (!event)
    2091                 :            :                 goto out;
    2092                 :          0 :         entry = ring_buffer_event_data(event);
    2093                 :          0 :         entry->ip = ip;
    2094                 :            : 
    2095                 :          0 :         memcpy(&entry->buf, tbuffer, len);
    2096                 :          0 :         entry->buf[len] = '\0';
    2097         [ #  # ]:          0 :         if (!call_filter_check_discard(call, entry, buffer, event)) {
    2098                 :            :                 __buffer_unlock_commit(buffer, event);
    2099                 :            :                 ftrace_trace_stack(buffer, flags, 6, pc);
    2100                 :            :         }
    2101                 :            :  out:
    2102                 :          0 :         preempt_enable_notrace();
    2103                 :            :         unpause_graph_tracing();
    2104                 :            : 
    2105                 :          0 :         return len;
    2106                 :            : }
    2107                 :            : 
    2108                 :          0 : int trace_array_vprintk(struct trace_array *tr,
    2109                 :            :                         unsigned long ip, const char *fmt, va_list args)
    2110                 :            : {
    2111                 :          0 :         return __trace_array_vprintk(tr->trace_buffer.buffer, ip, fmt, args);
    2112                 :            : }
    2113                 :            : 
    2114                 :          0 : int trace_array_printk(struct trace_array *tr,
    2115                 :            :                        unsigned long ip, const char *fmt, ...)
    2116                 :            : {
    2117                 :            :         int ret;
    2118                 :            :         va_list ap;
    2119                 :            : 
    2120         [ #  # ]:          0 :         if (!(trace_flags & TRACE_ITER_PRINTK))
    2121                 :            :                 return 0;
    2122                 :            : 
    2123                 :          0 :         va_start(ap, fmt);
    2124                 :            :         ret = trace_array_vprintk(tr, ip, fmt, ap);
    2125                 :          0 :         va_end(ap);
    2126                 :          0 :         return ret;
    2127                 :            : }
    2128                 :            : 
    2129                 :          0 : int trace_array_printk_buf(struct ring_buffer *buffer,
    2130                 :            :                            unsigned long ip, const char *fmt, ...)
    2131                 :            : {
    2132                 :            :         int ret;
    2133                 :            :         va_list ap;
    2134                 :            : 
    2135         [ #  # ]:          0 :         if (!(trace_flags & TRACE_ITER_PRINTK))
    2136                 :            :                 return 0;
    2137                 :            : 
    2138                 :          0 :         va_start(ap, fmt);
    2139                 :          0 :         ret = __trace_array_vprintk(buffer, ip, fmt, ap);
    2140                 :          0 :         va_end(ap);
    2141                 :          0 :         return ret;
    2142                 :            : }
    2143                 :            : 
    2144                 :          0 : int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
    2145                 :            : {
    2146                 :          0 :         return trace_array_vprintk(&global_trace, ip, fmt, args);
    2147                 :            : }
    2148                 :            : EXPORT_SYMBOL_GPL(trace_vprintk);
    2149                 :            : 
    2150                 :          0 : static void trace_iterator_increment(struct trace_iterator *iter)
    2151                 :            : {
    2152                 :          0 :         struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, iter->cpu);
    2153                 :            : 
    2154                 :          0 :         iter->idx++;
    2155         [ #  # ]:          0 :         if (buf_iter)
    2156                 :          0 :                 ring_buffer_read(buf_iter, NULL);
    2157                 :          0 : }
    2158                 :            : 
    2159                 :            : static struct trace_entry *
    2160                 :          0 : peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
    2161                 :            :                 unsigned long *lost_events)
    2162                 :            : {
    2163                 :            :         struct ring_buffer_event *event;
    2164                 :            :         struct ring_buffer_iter *buf_iter = trace_buffer_iter(iter, cpu);
    2165                 :            : 
    2166         [ #  # ]:          0 :         if (buf_iter)
    2167                 :          0 :                 event = ring_buffer_iter_peek(buf_iter, ts);
    2168                 :            :         else
    2169                 :          0 :                 event = ring_buffer_peek(iter->trace_buffer->buffer, cpu, ts,
    2170                 :            :                                          lost_events);
    2171                 :            : 
    2172         [ #  # ]:          0 :         if (event) {
    2173                 :          0 :                 iter->ent_size = ring_buffer_event_length(event);
    2174                 :          0 :                 return ring_buffer_event_data(event);
    2175                 :            :         }
    2176                 :          0 :         iter->ent_size = 0;
    2177                 :          0 :         return NULL;
    2178                 :            : }
    2179                 :            : 
    2180                 :            : static struct trace_entry *
    2181                 :          0 : __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
    2182                 :            :                   unsigned long *missing_events, u64 *ent_ts)
    2183                 :            : {
    2184                 :          0 :         struct ring_buffer *buffer = iter->trace_buffer->buffer;
    2185                 :            :         struct trace_entry *ent, *next = NULL;
    2186                 :          0 :         unsigned long lost_events = 0, next_lost = 0;
    2187                 :          0 :         int cpu_file = iter->cpu_file;
    2188                 :            :         u64 next_ts = 0, ts;
    2189                 :            :         int next_cpu = -1;
    2190                 :            :         int next_size = 0;
    2191                 :            :         int cpu;
    2192                 :            : 
    2193                 :            :         /*
    2194                 :            :          * If we are in a per_cpu trace file, don't bother by iterating over
    2195                 :            :          * all cpu and peek directly.
    2196                 :            :          */
    2197         [ #  # ]:          0 :         if (cpu_file > RING_BUFFER_ALL_CPUS) {
    2198         [ #  # ]:          0 :                 if (ring_buffer_empty_cpu(buffer, cpu_file))
    2199                 :            :                         return NULL;
    2200                 :          0 :                 ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
    2201         [ #  # ]:          0 :                 if (ent_cpu)
    2202                 :          0 :                         *ent_cpu = cpu_file;
    2203                 :            : 
    2204                 :          0 :                 return ent;
    2205                 :            :         }
    2206                 :            : 
    2207         [ #  # ]:          0 :         for_each_tracing_cpu(cpu) {
    2208                 :            : 
    2209         [ #  # ]:          0 :                 if (ring_buffer_empty_cpu(buffer, cpu))
    2210                 :          0 :                         continue;
    2211                 :            : 
    2212                 :          0 :                 ent = peek_next_entry(iter, cpu, &ts, &lost_events);
    2213                 :            : 
    2214                 :            :                 /*
    2215                 :            :                  * Pick the entry with the smallest timestamp:
    2216                 :            :                  */
    2217 [ #  # ][ #  # ]:          0 :                 if (ent && (!next || ts < next_ts)) {
                 [ #  # ]
    2218                 :            :                         next = ent;
    2219                 :            :                         next_cpu = cpu;
    2220                 :          0 :                         next_ts = ts;
    2221                 :          0 :                         next_lost = lost_events;
    2222                 :          0 :                         next_size = iter->ent_size;
    2223                 :            :                 }
    2224                 :            :         }
    2225                 :            : 
    2226                 :          0 :         iter->ent_size = next_size;
    2227                 :            : 
    2228         [ #  # ]:          0 :         if (ent_cpu)
    2229                 :          0 :                 *ent_cpu = next_cpu;
    2230                 :            : 
    2231         [ #  # ]:          0 :         if (ent_ts)
    2232                 :          0 :                 *ent_ts = next_ts;
    2233                 :            : 
    2234         [ #  # ]:          0 :         if (missing_events)
    2235                 :          0 :                 *missing_events = next_lost;
    2236                 :            : 
    2237                 :          0 :         return next;
    2238                 :            : }
    2239                 :            : 
    2240                 :            : /* Find the next real entry, without updating the iterator itself */
    2241                 :          0 : struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
    2242                 :            :                                           int *ent_cpu, u64 *ent_ts)
    2243                 :            : {
    2244                 :          0 :         return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
    2245                 :            : }
    2246                 :            : 
    2247                 :            : /* Find the next real entry, and increment the iterator to the next entry */
    2248                 :          0 : void *trace_find_next_entry_inc(struct trace_iterator *iter)
    2249                 :            : {
    2250                 :          0 :         iter->ent = __find_next_entry(iter, &iter->cpu,
    2251                 :            :                                       &iter->lost_events, &iter->ts);
    2252                 :            : 
    2253         [ #  # ]:          0 :         if (iter->ent)
    2254                 :          0 :                 trace_iterator_increment(iter);
    2255                 :            : 
    2256         [ #  # ]:          0 :         return iter->ent ? iter : NULL;
    2257                 :            : }
    2258                 :            : 
    2259                 :            : static void trace_consume(struct trace_iterator *iter)
    2260                 :            : {
    2261                 :          0 :         ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu, &iter->ts,
    2262                 :            :                             &iter->lost_events);
    2263                 :            : }
    2264                 :            : 
    2265                 :          0 : static void *s_next(struct seq_file *m, void *v, loff_t *pos)
    2266                 :            : {
    2267                 :          0 :         struct trace_iterator *iter = m->private;
    2268                 :          0 :         int i = (int)*pos;
    2269                 :            :         void *ent;
    2270                 :            : 
    2271 [ #  # ][ #  # ]:          0 :         WARN_ON_ONCE(iter->leftover);
                 [ #  # ]
    2272                 :            : 
    2273                 :          0 :         (*pos)++;
    2274                 :            : 
    2275                 :            :         /* can't go backwards */
    2276         [ #  # ]:          0 :         if (iter->idx > i)
    2277                 :            :                 return NULL;
    2278                 :            : 
    2279         [ #  # ]:          0 :         if (iter->idx < 0)
    2280                 :          0 :                 ent = trace_find_next_entry_inc(iter);
    2281                 :            :         else
    2282                 :            :                 ent = iter;
    2283                 :            : 
    2284 [ #  # ][ #  # ]:          0 :         while (ent && iter->idx < i)
    2285                 :          0 :                 ent = trace_find_next_entry_inc(iter);
    2286                 :            : 
    2287                 :          0 :         iter->pos = *pos;
    2288                 :            : 
    2289                 :          0 :         return ent;
    2290                 :            : }
    2291                 :            : 
    2292                 :          0 : void tracing_iter_reset(struct trace_iterator *iter, int cpu)
    2293                 :            : {
    2294                 :            :         struct ring_buffer_event *event;
    2295                 :            :         struct ring_buffer_iter *buf_iter;
    2296                 :            :         unsigned long entries = 0;
    2297                 :            :         u64 ts;
    2298                 :            : 
    2299                 :          0 :         per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = 0;
    2300                 :            : 
    2301                 :            :         buf_iter = trace_buffer_iter(iter, cpu);
    2302         [ #  # ]:          0 :         if (!buf_iter)
    2303                 :          0 :                 return;
    2304                 :            : 
    2305                 :          0 :         ring_buffer_iter_reset(buf_iter);
    2306                 :            : 
    2307                 :            :         /*
    2308                 :            :          * We could have the case with the max latency tracers
    2309                 :            :          * that a reset never took place on a cpu. This is evident
    2310                 :            :          * by the timestamp being before the start of the buffer.
    2311                 :            :          */
    2312         [ #  # ]:          0 :         while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
    2313         [ #  # ]:          0 :                 if (ts >= iter->trace_buffer->time_start)
    2314                 :            :                         break;
    2315                 :          0 :                 entries++;
    2316                 :          0 :                 ring_buffer_read(buf_iter, NULL);
    2317                 :            :         }
    2318                 :            : 
    2319                 :          0 :         per_cpu_ptr(iter->trace_buffer->data, cpu)->skipped_entries = entries;
    2320                 :            : }
    2321                 :            : 
    2322                 :            : /*
    2323                 :            :  * The current tracer is copied to avoid a global locking
    2324                 :            :  * all around.
    2325                 :            :  */
    2326                 :          0 : static void *s_start(struct seq_file *m, loff_t *pos)
    2327                 :            : {
    2328                 :          0 :         struct trace_iterator *iter = m->private;
    2329                 :          0 :         struct trace_array *tr = iter->tr;
    2330                 :          0 :         int cpu_file = iter->cpu_file;
    2331                 :            :         void *p = NULL;
    2332                 :          0 :         loff_t l = 0;
    2333                 :            :         int cpu;
    2334                 :            : 
    2335                 :            :         /*
    2336                 :            :          * copy the tracer to avoid using a global lock all around.
    2337                 :            :          * iter->trace is a copy of current_trace, the pointer to the
    2338                 :            :          * name may be used instead of a strcmp(), as iter->trace->name
    2339                 :            :          * will point to the same string as current_trace->name.
    2340                 :            :          */
    2341                 :          0 :         mutex_lock(&trace_types_lock);
    2342 [ #  # ][ #  # ]:          0 :         if (unlikely(tr->current_trace && iter->trace->name != tr->current_trace->name))
    2343                 :          0 :                 *iter->trace = *tr->current_trace;
    2344                 :          0 :         mutex_unlock(&trace_types_lock);
    2345                 :            : 
    2346                 :            : #ifdef CONFIG_TRACER_MAX_TRACE
    2347                 :            :         if (iter->snapshot && iter->trace->use_max_tr)
    2348                 :            :                 return ERR_PTR(-EBUSY);
    2349                 :            : #endif
    2350                 :            : 
    2351         [ #  # ]:          0 :         if (!iter->snapshot)
    2352                 :            :                 atomic_inc(&trace_record_cmdline_disabled);
    2353                 :            : 
    2354         [ #  # ]:          0 :         if (*pos != iter->pos) {
    2355                 :          0 :                 iter->ent = NULL;
    2356                 :          0 :                 iter->cpu = 0;
    2357                 :          0 :                 iter->idx = -1;
    2358                 :            : 
    2359         [ #  # ]:          0 :                 if (cpu_file == RING_BUFFER_ALL_CPUS) {
    2360         [ #  # ]:          0 :                         for_each_tracing_cpu(cpu)
    2361                 :          0 :                                 tracing_iter_reset(iter, cpu);
    2362                 :            :                 } else
    2363                 :          0 :                         tracing_iter_reset(iter, cpu_file);
    2364                 :            : 
    2365                 :          0 :                 iter->leftover = 0;
    2366 [ #  # ][ #  # ]:          0 :                 for (p = iter; p && l < *pos; p = s_next(m, p, &l))
    2367                 :            :                         ;
    2368                 :            : 
    2369                 :            :         } else {
    2370                 :            :                 /*
    2371                 :            :                  * If we overflowed the seq_file before, then we want
    2372                 :            :                  * to just reuse the trace_seq buffer again.
    2373                 :            :                  */
    2374         [ #  # ]:          0 :                 if (iter->leftover)
    2375                 :            :                         p = iter;
    2376                 :            :                 else {
    2377                 :          0 :                         l = *pos - 1;
    2378                 :          0 :                         p = s_next(m, p, &l);
    2379                 :            :                 }
    2380                 :            :         }
    2381                 :            : 
    2382                 :          0 :         trace_event_read_lock();
    2383                 :            :         trace_access_lock(cpu_file);
    2384                 :          0 :         return p;
    2385                 :            : }
    2386                 :            : 
    2387                 :          0 : static void s_stop(struct seq_file *m, void *p)
    2388                 :            : {
    2389                 :          0 :         struct trace_iterator *iter = m->private;
    2390                 :            : 
    2391                 :            : #ifdef CONFIG_TRACER_MAX_TRACE
    2392                 :            :         if (iter->snapshot && iter->trace->use_max_tr)
    2393                 :            :                 return;
    2394                 :            : #endif
    2395                 :            : 
    2396         [ #  # ]:          0 :         if (!iter->snapshot)
    2397                 :            :                 atomic_dec(&trace_record_cmdline_disabled);
    2398                 :            : 
    2399                 :          0 :         trace_access_unlock(iter->cpu_file);
    2400                 :          0 :         trace_event_read_unlock();
    2401                 :          0 : }
    2402                 :            : 
    2403                 :            : static void
    2404                 :          0 : get_total_entries(struct trace_buffer *buf,
    2405                 :            :                   unsigned long *total, unsigned long *entries)
    2406                 :            : {
    2407                 :            :         unsigned long count;
    2408                 :            :         int cpu;
    2409                 :            : 
    2410                 :          0 :         *total = 0;
    2411                 :          0 :         *entries = 0;
    2412                 :            : 
    2413         [ #  # ]:          0 :         for_each_tracing_cpu(cpu) {
    2414                 :          0 :                 count = ring_buffer_entries_cpu(buf->buffer, cpu);
    2415                 :            :                 /*
    2416                 :            :                  * If this buffer has skipped entries, then we hold all
    2417                 :            :                  * entries for the trace and we need to ignore the
    2418                 :            :                  * ones before the time stamp.
    2419                 :            :                  */
    2420         [ #  # ]:          0 :                 if (per_cpu_ptr(buf->data, cpu)->skipped_entries) {
    2421                 :          0 :                         count -= per_cpu_ptr(buf->data, cpu)->skipped_entries;
    2422                 :            :                         /* total is the same as the entries */
    2423                 :          0 :                         *total += count;
    2424                 :            :                 } else
    2425                 :          0 :                         *total += count +
    2426                 :          0 :                                 ring_buffer_overrun_cpu(buf->buffer, cpu);
    2427                 :          0 :                 *entries += count;
    2428                 :            :         }
    2429                 :          0 : }
    2430                 :            : 
    2431                 :          0 : static void print_lat_help_header(struct seq_file *m)
    2432                 :            : {
    2433                 :          0 :         seq_puts(m, "#                  _------=> CPU#            \n");
    2434                 :          0 :         seq_puts(m, "#                 / _-----=> irqs-off        \n");
    2435                 :          0 :         seq_puts(m, "#                | / _----=> need-resched    \n");
    2436                 :          0 :         seq_puts(m, "#                || / _---=> hardirq/softirq \n");
    2437                 :          0 :         seq_puts(m, "#                ||| / _--=> preempt-depth   \n");
    2438                 :          0 :         seq_puts(m, "#                |||| /     delay             \n");
    2439                 :          0 :         seq_puts(m, "#  cmd     pid   ||||| time  |   caller      \n");
    2440                 :          0 :         seq_puts(m, "#     \\   /      |||||  \\    |   /           \n");
    2441                 :          0 : }
    2442                 :            : 
    2443                 :          0 : static void print_event_info(struct trace_buffer *buf, struct seq_file *m)
    2444                 :            : {
    2445                 :            :         unsigned long total;
    2446                 :            :         unsigned long entries;
    2447                 :            : 
    2448                 :          0 :         get_total_entries(buf, &total, &entries);
    2449                 :          0 :         seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu   #P:%d\n",
    2450                 :            :                    entries, total, num_online_cpus());
    2451                 :          0 :         seq_puts(m, "#\n");
    2452                 :          0 : }
    2453                 :            : 
    2454                 :          0 : static void print_func_help_header(struct trace_buffer *buf, struct seq_file *m)
    2455                 :            : {
    2456                 :          0 :         print_event_info(buf, m);
    2457                 :          0 :         seq_puts(m, "#           TASK-PID   CPU#      TIMESTAMP  FUNCTION\n");
    2458                 :          0 :         seq_puts(m, "#              | |       |          |         |\n");
    2459                 :          0 : }
    2460                 :            : 
    2461                 :          0 : static void print_func_help_header_tgid(struct trace_buffer *buf, struct seq_file *m)
    2462                 :            : {
    2463                 :          0 :         print_event_info(buf, m);
    2464                 :          0 :         seq_puts(m, "#           TASK-PID    TGID   CPU#      TIMESTAMP  FUNCTION\n");
    2465                 :          0 :         seq_puts(m, "#              | |        |      |          |         |\n");
    2466                 :          0 : }
    2467                 :            : 
    2468                 :          0 : static void print_func_help_header_irq(struct trace_buffer *buf, struct seq_file *m)
    2469                 :            : {
    2470                 :          0 :         print_event_info(buf, m);
    2471                 :          0 :         seq_puts(m, "#                              _-----=> irqs-off\n");
    2472                 :          0 :         seq_puts(m, "#                             / _----=> need-resched\n");
    2473                 :          0 :         seq_puts(m, "#                            | / _---=> hardirq/softirq\n");
    2474                 :          0 :         seq_puts(m, "#                            || / _--=> preempt-depth\n");
    2475                 :          0 :         seq_puts(m, "#                            ||| /     delay\n");
    2476                 :          0 :         seq_puts(m, "#           TASK-PID   CPU#  ||||    TIMESTAMP  FUNCTION\n");
    2477                 :          0 :         seq_puts(m, "#              | |       |   ||||       |         |\n");
    2478                 :          0 : }
    2479                 :            : 
    2480                 :          0 : static void print_func_help_header_irq_tgid(struct trace_buffer *buf, struct seq_file *m)
    2481                 :            : {
    2482                 :          0 :         print_event_info(buf, m);
    2483                 :          0 :         seq_puts(m, "#                                      _-----=> irqs-off\n");
    2484                 :          0 :         seq_puts(m, "#                                     / _----=> need-resched\n");
    2485                 :          0 :         seq_puts(m, "#                                    | / _---=> hardirq/softirq\n");
    2486                 :          0 :         seq_puts(m, "#                                    || / _--=> preempt-depth\n");
    2487                 :          0 :         seq_puts(m, "#                                    ||| /     delay\n");
    2488                 :          0 :         seq_puts(m, "#           TASK-PID    TGID   CPU#  ||||    TIMESTAMP  FUNCTION\n");
    2489                 :          0 :         seq_puts(m, "#              | |        |      |   ||||       |         |\n");
    2490                 :          0 : }
    2491                 :            : 
    2492                 :            : void
    2493                 :          0 : print_trace_header(struct seq_file *m, struct trace_iterator *iter)
    2494                 :            : {
    2495                 :          0 :         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
    2496                 :          0 :         struct trace_buffer *buf = iter->trace_buffer;
    2497                 :          0 :         struct trace_array_cpu *data = per_cpu_ptr(buf->data, buf->cpu);
    2498                 :          0 :         struct tracer *type = iter->trace;
    2499                 :            :         unsigned long entries;
    2500                 :            :         unsigned long total;
    2501                 :            :         const char *name = "preemption";
    2502                 :            : 
    2503                 :          0 :         name = type->name;
    2504                 :            : 
    2505                 :          0 :         get_total_entries(buf, &total, &entries);
    2506                 :            : 
    2507                 :          0 :         seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
    2508                 :            :                    name, UTS_RELEASE);
    2509                 :          0 :         seq_puts(m, "# -----------------------------------"
    2510                 :            :                  "---------------------------------\n");
    2511                 :          0 :         seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
    2512                 :            :                    " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
    2513                 :            :                    nsecs_to_usecs(data->saved_latency),
    2514                 :            :                    entries,
    2515                 :            :                    total,
    2516                 :            :                    buf->cpu,
    2517                 :            : #if defined(CONFIG_PREEMPT_NONE)
    2518                 :            :                    "server",
    2519                 :            : #elif defined(CONFIG_PREEMPT_VOLUNTARY)
    2520                 :            :                    "desktop",
    2521                 :            : #elif defined(CONFIG_PREEMPT)
    2522                 :            :                    "preempt",
    2523                 :            : #else
    2524                 :            :                    "unknown",
    2525                 :            : #endif
    2526                 :            :                    /* These are reserved for later use */
    2527                 :            :                    0, 0, 0, 0);
    2528                 :            : #ifdef CONFIG_SMP
    2529                 :          0 :         seq_printf(m, " #P:%d)\n", num_online_cpus());
    2530                 :            : #else
    2531                 :            :         seq_puts(m, ")\n");
    2532                 :            : #endif
    2533                 :          0 :         seq_puts(m, "#    -----------------\n");
    2534                 :          0 :         seq_printf(m, "#    | task: %.16s-%d "
    2535                 :            :                    "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
    2536                 :          0 :                    data->comm, data->pid,
    2537                 :            :                    from_kuid_munged(seq_user_ns(m), data->uid), data->nice,
    2538                 :            :                    data->policy, data->rt_priority);
    2539                 :          0 :         seq_puts(m, "#    -----------------\n");
    2540                 :            : 
    2541         [ #  # ]:          0 :         if (data->critical_start) {
    2542                 :          0 :                 seq_puts(m, "#  => started at: ");
    2543                 :          0 :                 seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
    2544                 :          0 :                 trace_print_seq(m, &iter->seq);
    2545                 :          0 :                 seq_puts(m, "\n#  => ended at:   ");
    2546                 :          0 :                 seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
    2547                 :          0 :                 trace_print_seq(m, &iter->seq);
    2548                 :          0 :                 seq_puts(m, "\n#\n");
    2549                 :            :         }
    2550                 :            : 
    2551                 :          0 :         seq_puts(m, "#\n");
    2552                 :          0 : }
    2553                 :            : 
    2554                 :          0 : static void test_cpu_buff_start(struct trace_iterator *iter)
    2555                 :            : {
    2556                 :          0 :         struct trace_seq *s = &iter->seq;
    2557                 :            : 
    2558         [ #  # ]:          0 :         if (!(trace_flags & TRACE_ITER_ANNOTATE))
    2559                 :            :                 return;
    2560                 :            : 
    2561         [ #  # ]:          0 :         if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
    2562                 :            :                 return;
    2563                 :            : 
    2564         [ #  # ]:          0 :         if (cpumask_test_cpu(iter->cpu, iter->started))
    2565                 :            :                 return;
    2566                 :            : 
    2567         [ #  # ]:          0 :         if (per_cpu_ptr(iter->trace_buffer->data, iter->cpu)->skipped_entries)
    2568                 :            :                 return;
    2569                 :            : 
    2570                 :            :         cpumask_set_cpu(iter->cpu, iter->started);
    2571                 :            : 
    2572                 :            :         /* Don't print started cpu buffer for the first entry of the trace */
    2573         [ #  # ]:          0 :         if (iter->idx > 1)
    2574                 :          0 :                 trace_seq_printf(s, "##### CPU %u buffer started ####\n",
    2575                 :            :                                 iter->cpu);
    2576                 :            : }
    2577                 :            : 
    2578                 :          0 : static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
    2579                 :            : {
    2580                 :          0 :         struct trace_seq *s = &iter->seq;
    2581                 :          0 :         unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
    2582                 :            :         struct trace_entry *entry;
    2583                 :            :         struct trace_event *event;
    2584                 :            : 
    2585                 :          0 :         entry = iter->ent;
    2586                 :            : 
    2587                 :          0 :         test_cpu_buff_start(iter);
    2588                 :            : 
    2589                 :          0 :         event = ftrace_find_event(entry->type);
    2590                 :            : 
    2591         [ #  # ]:          0 :         if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
    2592         [ #  # ]:          0 :                 if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
    2593         [ #  # ]:          0 :                         if (!trace_print_lat_context(iter))
    2594                 :            :                                 goto partial;
    2595                 :            :                 } else {
    2596         [ #  # ]:          0 :                         if (!trace_print_context(iter))
    2597                 :            :                                 goto partial;
    2598                 :            :                 }
    2599                 :            :         }
    2600                 :            : 
    2601         [ #  # ]:          0 :         if (event)
    2602                 :          0 :                 return event->funcs->trace(iter, sym_flags, event);
    2603                 :            : 
    2604         [ #  # ]:          0 :         if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
    2605                 :            :                 goto partial;
    2606                 :            : 
    2607                 :            :         return TRACE_TYPE_HANDLED;
    2608                 :            : partial:
    2609                 :            :         return TRACE_TYPE_PARTIAL_LINE;
    2610                 :            : }
    2611                 :            : 
    2612                 :          0 : static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
    2613                 :            : {
    2614                 :          0 :         struct trace_seq *s = &iter->seq;
    2615                 :            :         struct trace_entry *entry;
    2616                 :            :         struct trace_event *event;
    2617                 :            : 
    2618                 :          0 :         entry = iter->ent;
    2619                 :            : 
    2620         [ #  # ]:          0 :         if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
    2621         [ #  # ]:          0 :                 if (!trace_seq_printf(s, "%d %d %llu ",
    2622                 :            :                                       entry->pid, iter->cpu, iter->ts))
    2623                 :            :                         goto partial;
    2624                 :            :         }
    2625                 :            : 
    2626                 :          0 :         event = ftrace_find_event(entry->type);
    2627         [ #  # ]:          0 :         if (event)
    2628                 :          0 :                 return event->funcs->raw(iter, 0, event);
    2629                 :            : 
    2630         [ #  # ]:          0 :         if (!trace_seq_printf(s, "%d ?\n", entry->type))
    2631                 :            :                 goto partial;
    2632                 :            : 
    2633                 :            :         return TRACE_TYPE_HANDLED;
    2634                 :            : partial:
    2635                 :            :         return TRACE_TYPE_PARTIAL_LINE;
    2636                 :            : }
    2637                 :            : 
    2638                 :          0 : static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
    2639                 :            : {
    2640                 :          0 :         struct trace_seq *s = &iter->seq;
    2641                 :          0 :         unsigned char newline = '\n';
    2642                 :            :         struct trace_entry *entry;
    2643                 :            :         struct trace_event *event;
    2644                 :            : 
    2645                 :          0 :         entry = iter->ent;
    2646                 :            : 
    2647         [ #  # ]:          0 :         if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
    2648         [ #  # ]:          0 :                 SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
    2649         [ #  # ]:          0 :                 SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
    2650         [ #  # ]:          0 :                 SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
    2651                 :            :         }
    2652                 :            : 
    2653                 :          0 :         event = ftrace_find_event(entry->type);
    2654         [ #  # ]:          0 :         if (event) {
    2655                 :          0 :                 enum print_line_t ret = event->funcs->hex(iter, 0, event);
    2656         [ #  # ]:          0 :                 if (ret != TRACE_TYPE_HANDLED)
    2657                 :            :                         return ret;
    2658                 :            :         }
    2659                 :            : 
    2660         [ #  # ]:          0 :         SEQ_PUT_FIELD_RET(s, newline);
    2661                 :            : 
    2662                 :          0 :         return TRACE_TYPE_HANDLED;
    2663                 :            : }
    2664                 :            : 
    2665                 :          0 : static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
    2666                 :            : {
    2667                 :          0 :         struct trace_seq *s = &iter->seq;
    2668                 :            :         struct trace_entry *entry;
    2669                 :            :         struct trace_event *event;
    2670                 :            : 
    2671                 :          0 :         entry = iter->ent;
    2672                 :            : 
    2673         [ #  # ]:          0 :         if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
    2674         [ #  # ]:          0 :                 SEQ_PUT_FIELD_RET(s, entry->pid);
    2675         [ #  # ]:          0 :                 SEQ_PUT_FIELD_RET(s, iter->cpu);
    2676         [ #  # ]:          0 :                 SEQ_PUT_FIELD_RET(s, iter->ts);
    2677                 :            :         }
    2678                 :            : 
    2679                 :          0 :         event = ftrace_find_event(entry->type);
    2680         [ #  # ]:          0 :         return event ? event->funcs->binary(iter, 0, event) :
    2681                 :            :                 TRACE_TYPE_HANDLED;
    2682                 :            : }
    2683                 :            : 
    2684                 :          0 : int trace_empty(struct trace_iterator *iter)
    2685                 :            : {
    2686                 :            :         struct ring_buffer_iter *buf_iter;
    2687                 :            :         int cpu;
    2688                 :            : 
    2689                 :            :         /* If we are looking at one CPU buffer, only check that one */
    2690         [ #  # ]:          0 :         if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
    2691                 :            :                 cpu = iter->cpu_file;
    2692                 :            :                 buf_iter = trace_buffer_iter(iter, cpu);
    2693         [ #  # ]:          0 :                 if (buf_iter) {
    2694         [ #  # ]:          0 :                         if (!ring_buffer_iter_empty(buf_iter))
    2695                 :            :                                 return 0;
    2696                 :            :                 } else {
    2697         [ #  # ]:          0 :                         if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
    2698                 :            :                                 return 0;
    2699                 :            :                 }
    2700                 :            :                 return 1;
    2701                 :            :         }
    2702                 :            : 
    2703         [ #  # ]:          0 :         for_each_tracing_cpu(cpu) {
    2704                 :            :                 buf_iter = trace_buffer_iter(iter, cpu);
    2705         [ #  # ]:          0 :                 if (buf_iter) {
    2706         [ #  # ]:          0 :                         if (!ring_buffer_iter_empty(buf_iter))
    2707                 :            :                                 return 0;
    2708                 :            :                 } else {
    2709         [ #  # ]:          0 :                         if (!ring_buffer_empty_cpu(iter->trace_buffer->buffer, cpu))
    2710                 :            :                                 return 0;
    2711                 :            :                 }
    2712                 :            :         }
    2713                 :            : 
    2714                 :            :         return 1;
    2715                 :            : }
    2716                 :            : 
    2717                 :            : /*  Called with trace_event_read_lock() held. */
    2718                 :          0 : enum print_line_t print_trace_line(struct trace_iterator *iter)
    2719                 :            : {
    2720                 :            :         enum print_line_t ret;
    2721                 :            : 
    2722   [ #  #  #  # ]:          0 :         if (iter->lost_events &&
    2723                 :          0 :             !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
    2724                 :            :                                  iter->cpu, iter->lost_events))
    2725                 :            :                 return TRACE_TYPE_PARTIAL_LINE;
    2726                 :            : 
    2727 [ #  # ][ #  # ]:          0 :         if (iter->trace && iter->trace->print_line) {
    2728                 :          0 :                 ret = iter->trace->print_line(iter);
    2729         [ #  # ]:          0 :                 if (ret != TRACE_TYPE_UNHANDLED)
    2730                 :            :                         return ret;
    2731                 :            :         }
    2732                 :            : 
    2733         [ #  # ]:          0 :         if (iter->ent->type == TRACE_BPUTS &&
    2734         [ #  # ]:          0 :                         trace_flags & TRACE_ITER_PRINTK &&
    2735                 :            :                         trace_flags & TRACE_ITER_PRINTK_MSGONLY)
    2736                 :          0 :                 return trace_print_bputs_msg_only(iter);
    2737                 :            : 
    2738         [ #  # ]:          0 :         if (iter->ent->type == TRACE_BPRINT &&
    2739         [ #  # ]:          0 :                         trace_flags & TRACE_ITER_PRINTK &&
    2740                 :            :                         trace_flags & TRACE_ITER_PRINTK_MSGONLY)
    2741                 :          0 :                 return trace_print_bprintk_msg_only(iter);
    2742                 :            : 
    2743         [ #  # ]:          0 :         if (iter->ent->type == TRACE_PRINT &&
    2744         [ #  # ]:          0 :                         trace_flags & TRACE_ITER_PRINTK &&
    2745                 :            :                         trace_flags & TRACE_ITER_PRINTK_MSGONLY)
    2746                 :          0 :                 return trace_print_printk_msg_only(iter);
    2747                 :            : 
    2748         [ #  # ]:          0 :         if (trace_flags & TRACE_ITER_BIN)
    2749                 :          0 :                 return print_bin_fmt(iter);
    2750                 :            : 
    2751         [ #  # ]:          0 :         if (trace_flags & TRACE_ITER_HEX)
    2752                 :          0 :                 return print_hex_fmt(iter);
    2753                 :            : 
    2754         [ #  # ]:          0 :         if (trace_flags & TRACE_ITER_RAW)
    2755                 :          0 :                 return print_raw_fmt(iter);
    2756                 :            : 
    2757                 :          0 :         return print_trace_fmt(iter);
    2758                 :            : }
    2759                 :            : 
    2760                 :          0 : void trace_latency_header(struct seq_file *m)
    2761                 :            : {
    2762                 :          0 :         struct trace_iterator *iter = m->private;
    2763                 :            : 
    2764                 :            :         /* print nothing if the buffers are empty */
    2765         [ #  # ]:          0 :         if (trace_empty(iter))
    2766                 :          0 :                 return;
    2767                 :            : 
    2768         [ #  # ]:          0 :         if (iter->iter_flags & TRACE_FILE_LAT_FMT)
    2769                 :          0 :                 print_trace_header(m, iter);
    2770                 :            : 
    2771         [ #  # ]:          0 :         if (!(trace_flags & TRACE_ITER_VERBOSE))
    2772                 :          0 :                 print_lat_help_header(m);
    2773                 :            : }
    2774                 :            : 
    2775                 :          0 : void trace_default_header(struct seq_file *m)
    2776                 :            : {
    2777                 :          0 :         struct trace_iterator *iter = m->private;
    2778                 :            : 
    2779         [ #  # ]:          0 :         if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
    2780                 :            :                 return;
    2781                 :            : 
    2782         [ #  # ]:          0 :         if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
    2783                 :            :                 /* print nothing if the buffers are empty */
    2784         [ #  # ]:          0 :                 if (trace_empty(iter))
    2785                 :            :                         return;
    2786                 :          0 :                 print_trace_header(m, iter);
    2787         [ #  # ]:          0 :                 if (!(trace_flags & TRACE_ITER_VERBOSE))
    2788                 :          0 :                         print_lat_help_header(m);
    2789                 :            :         } else {
    2790         [ #  # ]:          0 :                 if (!(trace_flags & TRACE_ITER_VERBOSE)) {
    2791         [ #  # ]:          0 :                         if (trace_flags & TRACE_ITER_IRQ_INFO)
    2792         [ #  # ]:          0 :                                 if (trace_flags & TRACE_ITER_TGID)
    2793                 :          0 :                                         print_func_help_header_irq_tgid(iter->trace_buffer, m);
    2794                 :            :                                 else
    2795                 :          0 :                                         print_func_help_header_irq(iter->trace_buffer, m);
    2796                 :            :                         else
    2797         [ #  # ]:          0 :                                 if (trace_flags & TRACE_ITER_TGID)
    2798                 :          0 :                                         print_func_help_header_tgid(iter->trace_buffer, m);
    2799                 :            :                                 else
    2800                 :          0 :                                         print_func_help_header(iter->trace_buffer, m);
    2801                 :            :                 }
    2802                 :            :         }
    2803                 :            : }
    2804                 :            : 
    2805                 :          0 : static void test_ftrace_alive(struct seq_file *m)
    2806                 :            : {
    2807         [ #  # ]:          0 :         if (!ftrace_is_dead())
    2808                 :          0 :                 return;
    2809                 :          0 :         seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
    2810                 :          0 :         seq_printf(m, "#          MAY BE MISSING FUNCTION EVENTS\n");
    2811                 :            : }
    2812                 :            : 
    2813                 :            : #ifdef CONFIG_TRACER_MAX_TRACE
    2814                 :            : static void show_snapshot_main_help(struct seq_file *m)
    2815                 :            : {
    2816                 :            :         seq_printf(m, "# echo 0 > snapshot : Clears and frees snapshot buffer\n");
    2817                 :            :         seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
    2818                 :            :         seq_printf(m, "#                      Takes a snapshot of the main buffer.\n");
    2819                 :            :         seq_printf(m, "# echo 2 > snapshot : Clears snapshot buffer (but does not allocate or free)\n");
    2820                 :            :         seq_printf(m, "#                      (Doesn't have to be '2' works with any number that\n");
    2821                 :            :         seq_printf(m, "#                       is not a '0' or '1')\n");
    2822                 :            : }
    2823                 :            : 
    2824                 :            : static void show_snapshot_percpu_help(struct seq_file *m)
    2825                 :            : {
    2826                 :            :         seq_printf(m, "# echo 0 > snapshot : Invalid for per_cpu snapshot file.\n");
    2827                 :            : #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
    2828                 :            :         seq_printf(m, "# echo 1 > snapshot : Allocates snapshot buffer, if not already allocated.\n");
    2829                 :            :         seq_printf(m, "#                      Takes a snapshot of the main buffer for this cpu.\n");
    2830                 :            : #else
    2831                 :            :         seq_printf(m, "# echo 1 > snapshot : Not supported with this kernel.\n");
    2832                 :            :         seq_printf(m, "#                     Must use main snapshot file to allocate.\n");
    2833                 :            : #endif
    2834                 :            :         seq_printf(m, "# echo 2 > snapshot : Clears this cpu's snapshot buffer (but does not allocate)\n");
    2835                 :            :         seq_printf(m, "#                      (Doesn't have to be '2' works with any number that\n");
    2836                 :            :         seq_printf(m, "#                       is not a '0' or '1')\n");
    2837                 :            : }
    2838                 :            : 
    2839                 :            : static void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter)
    2840                 :            : {
    2841                 :            :         if (iter->tr->allocated_snapshot)
    2842                 :            :                 seq_printf(m, "#\n# * Snapshot is allocated *\n#\n");
    2843                 :            :         else
    2844                 :            :                 seq_printf(m, "#\n# * Snapshot is freed *\n#\n");
    2845                 :            : 
    2846                 :            :         seq_printf(m, "# Snapshot commands:\n");
    2847                 :            :         if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
    2848                 :            :                 show_snapshot_main_help(m);
    2849                 :            :         else
    2850                 :            :                 show_snapshot_percpu_help(m);
    2851                 :            : }
    2852                 :            : #else
    2853                 :            : /* Should never be called */
    2854                 :            : static inline void print_snapshot_help(struct seq_file *m, struct trace_iterator *iter) { }
    2855                 :            : #endif
    2856                 :            : 
    2857                 :          0 : static int s_show(struct seq_file *m, void *v)
    2858                 :            : {
    2859                 :            :         struct trace_iterator *iter = v;
    2860                 :            :         int ret;
    2861                 :            : 
    2862         [ #  # ]:          0 :         if (iter->ent == NULL) {
    2863         [ #  # ]:          0 :                 if (iter->tr) {
    2864                 :          0 :                         seq_printf(m, "# tracer: %s\n", iter->trace->name);
    2865                 :          0 :                         seq_puts(m, "#\n");
    2866                 :          0 :                         test_ftrace_alive(m);
    2867                 :            :                 }
    2868 [ #  # ][ #  # ]:          0 :                 if (iter->snapshot && trace_empty(iter))
    2869                 :            :                         print_snapshot_help(m, iter);
    2870 [ #  # ][ #  # ]:          0 :                 else if (iter->trace && iter->trace->print_header)
    2871                 :          0 :                         iter->trace->print_header(m);
    2872                 :            :                 else
    2873                 :          0 :                         trace_default_header(m);
    2874                 :            : 
    2875         [ #  # ]:          0 :         } else if (iter->leftover) {
    2876                 :            :                 /*
    2877                 :            :                  * If we filled the seq_file buffer earlier, we
    2878                 :            :                  * want to just show it now.
    2879                 :            :                  */
    2880                 :          0 :                 ret = trace_print_seq(m, &iter->seq);
    2881                 :            : 
    2882                 :            :                 /* ret should this time be zero, but you never know */
    2883                 :          0 :                 iter->leftover = ret;
    2884                 :            : 
    2885                 :            :         } else {
    2886                 :          0 :                 print_trace_line(iter);
    2887                 :          0 :                 ret = trace_print_seq(m, &iter->seq);
    2888                 :            :                 /*
    2889                 :            :                  * If we overflow the seq_file buffer, then it will
    2890                 :            :                  * ask us for this data again at start up.
    2891                 :            :                  * Use that instead.
    2892                 :            :                  *  ret is 0 if seq_file write succeeded.
    2893                 :            :                  *        -1 otherwise.
    2894                 :            :                  */
    2895                 :          0 :                 iter->leftover = ret;
    2896                 :            :         }
    2897                 :            : 
    2898                 :          0 :         return 0;
    2899                 :            : }
    2900                 :            : 
    2901                 :            : /*
    2902                 :            :  * Should be used after trace_array_get(), trace_types_lock
    2903                 :            :  * ensures that i_cdev was already initialized.
    2904                 :            :  */
    2905                 :            : static inline int tracing_get_cpu(struct inode *inode)
    2906                 :            : {
    2907   [ #  #  #  # ]:          0 :         if (inode->i_cdev) /* See trace_create_cpu_file() */
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
    2908                 :          0 :                 return (long)inode->i_cdev - 1;
    2909                 :            :         return RING_BUFFER_ALL_CPUS;
    2910                 :            : }
    2911                 :            : 
    2912                 :            : static const struct seq_operations tracer_seq_ops = {
    2913                 :            :         .start          = s_start,
    2914                 :            :         .next           = s_next,
    2915                 :            :         .stop           = s_stop,
    2916                 :            :         .show           = s_show,
    2917                 :            : };
    2918                 :            : 
    2919                 :            : static struct trace_iterator *
    2920                 :          0 : __tracing_open(struct inode *inode, struct file *file, bool snapshot)
    2921                 :            : {
    2922                 :          0 :         struct trace_array *tr = inode->i_private;
    2923                 :            :         struct trace_iterator *iter;
    2924                 :            :         int cpu;
    2925                 :            : 
    2926         [ #  # ]:          0 :         if (tracing_disabled)
    2927                 :            :                 return ERR_PTR(-ENODEV);
    2928                 :            : 
    2929                 :          0 :         iter = __seq_open_private(file, &tracer_seq_ops, sizeof(*iter));
    2930         [ #  # ]:          0 :         if (!iter)
    2931                 :            :                 return ERR_PTR(-ENOMEM);
    2932                 :            : 
    2933                 :          0 :         iter->buffer_iter = kzalloc(sizeof(*iter->buffer_iter) * num_possible_cpus(),
    2934                 :            :                                     GFP_KERNEL);
    2935         [ #  # ]:          0 :         if (!iter->buffer_iter)
    2936                 :            :                 goto release;
    2937                 :            : 
    2938                 :            :         /*
    2939                 :            :          * We make a copy of the current tracer to avoid concurrent
    2940                 :            :          * changes on it while we are reading.
    2941                 :            :          */
    2942                 :          0 :         mutex_lock(&trace_types_lock);
    2943                 :          0 :         iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
    2944         [ #  # ]:          0 :         if (!iter->trace)
    2945                 :            :                 goto fail;
    2946                 :            : 
    2947                 :          0 :         *iter->trace = *tr->current_trace;
    2948                 :            : 
    2949                 :            :         if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
    2950                 :            :                 goto fail;
    2951                 :            : 
    2952                 :          0 :         iter->tr = tr;
    2953                 :            : 
    2954                 :            : #ifdef CONFIG_TRACER_MAX_TRACE
    2955                 :            :         /* Currently only the top directory has a snapshot */
    2956                 :            :         if (tr->current_trace->print_max || snapshot)
    2957                 :            :                 iter->trace_buffer = &tr->max_buffer;
    2958                 :            :         else
    2959                 :            : #endif
    2960                 :          0 :                 iter->trace_buffer = &tr->trace_buffer;
    2961                 :          0 :         iter->snapshot = snapshot;
    2962                 :          0 :         iter->pos = -1;
    2963                 :          0 :         iter->cpu_file = tracing_get_cpu(inode);
    2964                 :          0 :         mutex_init(&iter->mutex);
    2965                 :            : 
    2966                 :            :         /* Notify the tracer early; before we stop tracing. */
    2967 [ #  # ][ #  # ]:          0 :         if (iter->trace && iter->trace->open)
    2968                 :          0 :                 iter->trace->open(iter);
    2969                 :            : 
    2970                 :            :         /* Annotate start of buffers if we had overruns */
    2971         [ #  # ]:          0 :         if (ring_buffer_overruns(iter->trace_buffer->buffer))
    2972                 :          0 :                 iter->iter_flags |= TRACE_FILE_ANNOTATE;
    2973                 :            : 
    2974                 :            :         /* Output in nanoseconds only if we are using a clock in nanoseconds. */
    2975         [ #  # ]:          0 :         if (trace_clocks[tr->clock_id].in_ns)
    2976                 :          0 :                 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
    2977                 :            : 
    2978                 :            :         /* stop the trace while dumping if we are not opening "snapshot" */
    2979         [ #  # ]:          0 :         if (!iter->snapshot)
    2980                 :          0 :                 tracing_stop_tr(tr);
    2981                 :            : 
    2982         [ #  # ]:          0 :         if (iter->cpu_file == RING_BUFFER_ALL_CPUS) {
    2983         [ #  # ]:          0 :                 for_each_tracing_cpu(cpu) {
    2984                 :          0 :                         iter->buffer_iter[cpu] =
    2985                 :          0 :                                 ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
    2986                 :            :                 }
    2987                 :          0 :                 ring_buffer_read_prepare_sync();
    2988         [ #  # ]:          0 :                 for_each_tracing_cpu(cpu) {
    2989                 :          0 :                         ring_buffer_read_start(iter->buffer_iter[cpu]);
    2990                 :          0 :                         tracing_iter_reset(iter, cpu);
    2991                 :            :                 }
    2992                 :            :         } else {
    2993                 :            :                 cpu = iter->cpu_file;
    2994                 :          0 :                 iter->buffer_iter[cpu] =
    2995                 :          0 :                         ring_buffer_read_prepare(iter->trace_buffer->buffer, cpu);
    2996                 :          0 :                 ring_buffer_read_prepare_sync();
    2997                 :          0 :                 ring_buffer_read_start(iter->buffer_iter[cpu]);
    2998                 :          0 :                 tracing_iter_reset(iter, cpu);
    2999                 :            :         }
    3000                 :            : 
    3001                 :          0 :         mutex_unlock(&trace_types_lock);
    3002                 :            : 
    3003                 :          0 :         return iter;
    3004                 :            : 
    3005                 :            :  fail:
    3006                 :          0 :         mutex_unlock(&trace_types_lock);
    3007                 :          0 :         kfree(iter->trace);
    3008                 :          0 :         kfree(iter->buffer_iter);
    3009                 :            : release:
    3010                 :          0 :         seq_release_private(inode, file);
    3011                 :          0 :         return ERR_PTR(-ENOMEM);
    3012                 :            : }
    3013                 :            : 
    3014                 :          0 : int tracing_open_generic(struct inode *inode, struct file *filp)
    3015                 :            : {
    3016         [ #  # ]:          0 :         if (tracing_disabled)
    3017                 :            :                 return -ENODEV;
    3018                 :            : 
    3019                 :          0 :         filp->private_data = inode->i_private;
    3020                 :          0 :         return 0;
    3021                 :            : }
    3022                 :            : 
    3023                 :          0 : bool tracing_is_disabled(void)
    3024                 :            : {
    3025                 :          0 :         return (tracing_disabled) ? true: false;
    3026                 :            : }
    3027                 :            : 
    3028                 :            : /*
    3029                 :            :  * Open and update trace_array ref count.
    3030                 :            :  * Must have the current trace_array passed to it.
    3031                 :            :  */
    3032                 :          0 : static int tracing_open_generic_tr(struct inode *inode, struct file *filp)
    3033                 :            : {
    3034                 :          0 :         struct trace_array *tr = inode->i_private;
    3035                 :            : 
    3036         [ #  # ]:          0 :         if (tracing_disabled)
    3037                 :            :                 return -ENODEV;
    3038                 :            : 
    3039         [ #  # ]:          0 :         if (trace_array_get(tr) < 0)
    3040                 :            :                 return -ENODEV;
    3041                 :            : 
    3042                 :          0 :         filp->private_data = inode->i_private;
    3043                 :            : 
    3044                 :          0 :         return 0;
    3045                 :            : }
    3046                 :            : 
    3047                 :          0 : static int tracing_release(struct inode *inode, struct file *file)
    3048                 :            : {
    3049                 :          0 :         struct trace_array *tr = inode->i_private;
    3050                 :          0 :         struct seq_file *m = file->private_data;
    3051                 :            :         struct trace_iterator *iter;
    3052                 :            :         int cpu;
    3053                 :            : 
    3054         [ #  # ]:          0 :         if (!(file->f_mode & FMODE_READ)) {
    3055                 :          0 :                 trace_array_put(tr);
    3056                 :          0 :                 return 0;
    3057                 :            :         }
    3058                 :            : 
    3059                 :            :         /* Writes do not use seq_file */
    3060                 :          0 :         iter = m->private;
    3061                 :          0 :         mutex_lock(&trace_types_lock);
    3062                 :            : 
    3063         [ #  # ]:          0 :         for_each_tracing_cpu(cpu) {
    3064         [ #  # ]:          0 :                 if (iter->buffer_iter[cpu])
    3065                 :          0 :                         ring_buffer_read_finish(iter->buffer_iter[cpu]);
    3066                 :            :         }
    3067                 :            : 
    3068 [ #  # ][ #  # ]:          0 :         if (iter->trace && iter->trace->close)
    3069                 :          0 :                 iter->trace->close(iter);
    3070                 :            : 
    3071         [ #  # ]:          0 :         if (!iter->snapshot)
    3072                 :            :                 /* reenable tracing if it was previously enabled */
    3073                 :          0 :                 tracing_start_tr(tr);
    3074                 :            : 
    3075                 :          0 :         __trace_array_put(tr);
    3076                 :            : 
    3077                 :          0 :         mutex_unlock(&trace_types_lock);
    3078                 :            : 
    3079                 :            :         mutex_destroy(&iter->mutex);
    3080                 :            :         free_cpumask_var(iter->started);
    3081                 :          0 :         kfree(iter->trace);
    3082                 :          0 :         kfree(iter->buffer_iter);
    3083                 :          0 :         seq_release_private(inode, file);
    3084                 :            : 
    3085                 :          0 :         return 0;
    3086                 :            : }
    3087                 :            : 
    3088                 :          0 : static int tracing_release_generic_tr(struct inode *inode, struct file *file)
    3089                 :            : {
    3090                 :          0 :         struct trace_array *tr = inode->i_private;
    3091                 :            : 
    3092                 :          0 :         trace_array_put(tr);
    3093                 :          0 :         return 0;
    3094                 :            : }
    3095                 :            : 
    3096                 :          0 : static int tracing_single_release_tr(struct inode *inode, struct file *file)
    3097                 :            : {
    3098                 :          0 :         struct trace_array *tr = inode->i_private;
    3099                 :            : 
    3100                 :          0 :         trace_array_put(tr);
    3101                 :            : 
    3102                 :          0 :         return single_release(inode, file);
    3103                 :            : }
    3104                 :            : 
    3105                 :          0 : static int tracing_open(struct inode *inode, struct file *file)
    3106                 :            : {
    3107                 :          0 :         struct trace_array *tr = inode->i_private;
    3108                 :            :         struct trace_iterator *iter;
    3109                 :            :         int ret = 0;
    3110                 :            : 
    3111         [ #  # ]:          0 :         if (trace_array_get(tr) < 0)
    3112                 :            :                 return -ENODEV;
    3113                 :            : 
    3114                 :            :         /* If this file was open for write, then erase contents */
    3115 [ #  # ][ #  # ]:          0 :         if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
    3116                 :            :                 int cpu = tracing_get_cpu(inode);
    3117                 :            : 
    3118         [ #  # ]:          0 :                 if (cpu == RING_BUFFER_ALL_CPUS)
    3119                 :          0 :                         tracing_reset_online_cpus(&tr->trace_buffer);
    3120                 :            :                 else
    3121                 :          0 :                         tracing_reset(&tr->trace_buffer, cpu);
    3122                 :            :         }
    3123                 :            : 
    3124         [ #  # ]:          0 :         if (file->f_mode & FMODE_READ) {
    3125                 :          0 :                 iter = __tracing_open(inode, file, false);
    3126         [ #  # ]:          0 :                 if (IS_ERR(iter))
    3127                 :            :                         ret = PTR_ERR(iter);
    3128         [ #  # ]:          0 :                 else if (trace_flags & TRACE_ITER_LATENCY_FMT)
    3129                 :          0 :                         iter->iter_flags |= TRACE_FILE_LAT_FMT;
    3130                 :            :         }
    3131                 :            : 
    3132         [ #  # ]:          0 :         if (ret < 0)
    3133                 :          0 :                 trace_array_put(tr);
    3134                 :            : 
    3135                 :          0 :         return ret;
    3136                 :            : }
    3137                 :            : 
    3138                 :            : static void *
    3139                 :          0 : t_next(struct seq_file *m, void *v, loff_t *pos)
    3140                 :            : {
    3141                 :            :         struct tracer *t = v;
    3142                 :            : 
    3143                 :          0 :         (*pos)++;
    3144                 :            : 
    3145 [ #  # ][ #  # ]:          0 :         if (t)
    3146                 :          0 :                 t = t->next;
    3147                 :            : 
    3148                 :          0 :         return t;
    3149                 :            : }
    3150                 :            : 
    3151                 :          0 : static void *t_start(struct seq_file *m, loff_t *pos)
    3152                 :            : {
    3153                 :            :         struct tracer *t;
    3154                 :            :         loff_t l = 0;
    3155                 :            : 
    3156                 :          0 :         mutex_lock(&trace_types_lock);
    3157 [ #  # ][ #  # ]:          0 :         for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
    3158                 :            :                 ;
    3159                 :            : 
    3160                 :          0 :         return t;
    3161                 :            : }
    3162                 :            : 
    3163                 :          0 : static void t_stop(struct seq_file *m, void *p)
    3164                 :            : {
    3165                 :          0 :         mutex_unlock(&trace_types_lock);
    3166                 :          0 : }
    3167                 :            : 
    3168                 :          0 : static int t_show(struct seq_file *m, void *v)
    3169                 :            : {
    3170                 :            :         struct tracer *t = v;
    3171                 :            : 
    3172         [ #  # ]:          0 :         if (!t)
    3173                 :            :                 return 0;
    3174                 :            : 
    3175                 :          0 :         seq_printf(m, "%s", t->name);
    3176         [ #  # ]:          0 :         if (t->next)
    3177                 :          0 :                 seq_putc(m, ' ');
    3178                 :            :         else
    3179                 :          0 :                 seq_putc(m, '\n');
    3180                 :            : 
    3181                 :            :         return 0;
    3182                 :            : }
    3183                 :            : 
    3184                 :            : static const struct seq_operations show_traces_seq_ops = {
    3185                 :            :         .start          = t_start,
    3186                 :            :         .next           = t_next,
    3187                 :            :         .stop           = t_stop,
    3188                 :            :         .show           = t_show,
    3189                 :            : };
    3190                 :            : 
    3191                 :          0 : static int show_traces_open(struct inode *inode, struct file *file)
    3192                 :            : {
    3193         [ #  # ]:          0 :         if (tracing_disabled)
    3194                 :            :                 return -ENODEV;
    3195                 :            : 
    3196                 :          0 :         return seq_open(file, &show_traces_seq_ops);
    3197                 :            : }
    3198                 :            : 
    3199                 :            : static ssize_t
    3200                 :          0 : tracing_write_stub(struct file *filp, const char __user *ubuf,
    3201                 :            :                    size_t count, loff_t *ppos)
    3202                 :            : {
    3203                 :          0 :         return count;
    3204                 :            : }
    3205                 :            : 
    3206                 :          0 : static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
    3207                 :            : {
    3208         [ #  # ]:          0 :         if (file->f_mode & FMODE_READ)
    3209                 :          0 :                 return seq_lseek(file, offset, origin);
    3210                 :            :         else
    3211                 :            :                 return 0;
    3212                 :            : }
    3213                 :            : 
    3214                 :            : static const struct file_operations tracing_fops = {
    3215                 :            :         .open           = tracing_open,
    3216                 :            :         .read           = seq_read,
    3217                 :            :         .write          = tracing_write_stub,
    3218                 :            :         .llseek         = tracing_seek,
    3219                 :            :         .release        = tracing_release,
    3220                 :            : };
    3221                 :            : 
    3222                 :            : static const struct file_operations show_traces_fops = {
    3223                 :            :         .open           = show_traces_open,
    3224                 :            :         .read           = seq_read,
    3225                 :            :         .release        = seq_release,
    3226                 :            :         .llseek         = seq_lseek,
    3227                 :            : };
    3228                 :            : 
    3229                 :            : /*
    3230                 :            :  * The tracer itself will not take this lock, but still we want
    3231                 :            :  * to provide a consistent cpumask to user-space:
    3232                 :            :  */
    3233                 :            : static DEFINE_MUTEX(tracing_cpumask_update_lock);
    3234                 :            : 
    3235                 :            : /*
    3236                 :            :  * Temporary storage for the character representation of the
    3237                 :            :  * CPU bitmask (and one more byte for the newline):
    3238                 :            :  */
    3239                 :            : static char mask_str[NR_CPUS + 1];
    3240                 :            : 
    3241                 :            : static ssize_t
    3242                 :          0 : tracing_cpumask_read(struct file *filp, char __user *ubuf,
    3243                 :            :                      size_t count, loff_t *ppos)
    3244                 :            : {
    3245                 :          0 :         struct trace_array *tr = file_inode(filp)->i_private;
    3246                 :            :         int len;
    3247                 :            : 
    3248                 :          0 :         mutex_lock(&tracing_cpumask_update_lock);
    3249                 :            : 
    3250                 :            :         len = cpumask_scnprintf(mask_str, count, tr->tracing_cpumask);
    3251         [ #  # ]:          0 :         if (count - len < 2) {
    3252                 :            :                 count = -EINVAL;
    3253                 :            :                 goto out_err;
    3254                 :            :         }
    3255                 :          0 :         len += sprintf(mask_str + len, "\n");
    3256                 :          0 :         count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
    3257                 :            : 
    3258                 :            : out_err:
    3259                 :          0 :         mutex_unlock(&tracing_cpumask_update_lock);
    3260                 :            : 
    3261                 :          0 :         return count;
    3262                 :            : }
    3263                 :            : 
    3264                 :            : static ssize_t
    3265                 :          0 : tracing_cpumask_write(struct file *filp, const char __user *ubuf,
    3266                 :            :                       size_t count, loff_t *ppos)
    3267                 :            : {
    3268                 :          0 :         struct trace_array *tr = file_inode(filp)->i_private;
    3269                 :            :         cpumask_var_t tracing_cpumask_new;
    3270                 :            :         int err, cpu;
    3271                 :            : 
    3272                 :            :         if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
    3273                 :            :                 return -ENOMEM;
    3274                 :            : 
    3275                 :            :         err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
    3276         [ #  # ]:          0 :         if (err)
    3277                 :            :                 goto err_unlock;
    3278                 :            : 
    3279                 :          0 :         mutex_lock(&tracing_cpumask_update_lock);
    3280                 :            : 
    3281                 :            :         local_irq_disable();
    3282                 :            :         arch_spin_lock(&ftrace_max_lock);
    3283         [ #  # ]:          0 :         for_each_tracing_cpu(cpu) {
    3284                 :            :                 /*
    3285                 :            :                  * Increase/decrease the disabled counter if we are
    3286                 :            :                  * about to flip a bit in the cpumask:
    3287                 :            :                  */
    3288 [ #  # ][ #  # ]:          0 :                 if (cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
    3289                 :            :                                 !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
    3290                 :          0 :                         atomic_inc(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
    3291                 :          0 :                         ring_buffer_record_disable_cpu(tr->trace_buffer.buffer, cpu);
    3292                 :            :                 }
    3293 [ #  # ][ #  # ]:          0 :                 if (!cpumask_test_cpu(cpu, tr->tracing_cpumask) &&
    3294                 :            :                                 cpumask_test_cpu(cpu, tracing_cpumask_new)) {
    3295                 :          0 :                         atomic_dec(&per_cpu_ptr(tr->trace_buffer.data, cpu)->disabled);
    3296                 :          0 :                         ring_buffer_record_enable_cpu(tr->trace_buffer.buffer, cpu);
    3297                 :            :                 }
    3298                 :            :         }
    3299                 :            :         arch_spin_unlock(&ftrace_max_lock);
    3300                 :            :         local_irq_enable();
    3301                 :            : 
    3302                 :            :         cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new);
    3303                 :            : 
    3304                 :          0 :         mutex_unlock(&tracing_cpumask_update_lock);
    3305                 :            :         free_cpumask_var(tracing_cpumask_new);
    3306                 :            : 
    3307                 :          0 :         return count;
    3308                 :            : 
    3309                 :            : err_unlock:
    3310                 :            :         free_cpumask_var(tracing_cpumask_new);
    3311                 :            : 
    3312                 :            :         return err;
    3313                 :            : }
    3314                 :            : 
    3315                 :            : static const struct file_operations tracing_cpumask_fops = {
    3316                 :            :         .open           = tracing_open_generic_tr,
    3317                 :            :         .read           = tracing_cpumask_read,
    3318                 :            :         .write          = tracing_cpumask_write,
    3319                 :            :         .release        = tracing_release_generic_tr,
    3320                 :            :         .llseek         = generic_file_llseek,
    3321                 :            : };
    3322                 :            : 
    3323                 :          0 : static int tracing_trace_options_show(struct seq_file *m, void *v)
    3324                 :            : {
    3325                 :            :         struct tracer_opt *trace_opts;
    3326                 :          0 :         struct trace_array *tr = m->private;
    3327                 :            :         u32 tracer_flags;
    3328                 :            :         int i;
    3329                 :            : 
    3330                 :          0 :         mutex_lock(&trace_types_lock);
    3331                 :          0 :         tracer_flags = tr->current_trace->flags->val;
    3332                 :          0 :         trace_opts = tr->current_trace->flags->opts;
    3333                 :            : 
    3334         [ #  # ]:          0 :         for (i = 0; trace_options[i]; i++) {
    3335         [ #  # ]:          0 :                 if (trace_flags & (1 << i))
    3336                 :          0 :                         seq_printf(m, "%s\n", trace_options[i]);
    3337                 :            :                 else
    3338                 :          0 :                         seq_printf(m, "no%s\n", trace_options[i]);
    3339                 :            :         }
    3340                 :            : 
    3341         [ #  # ]:          0 :         for (i = 0; trace_opts[i].name; i++) {
    3342         [ #  # ]:          0 :                 if (tracer_flags & trace_opts[i].bit)
    3343                 :          0 :                         seq_printf(m, "%s\n", trace_opts[i].name);
    3344                 :            :                 else
    3345                 :          0 :                         seq_printf(m, "no%s\n", trace_opts[i].name);
    3346                 :            :         }
    3347                 :          0 :         mutex_unlock(&trace_types_lock);
    3348                 :            : 
    3349                 :          0 :         return 0;
    3350                 :            : }
    3351                 :            : 
    3352                 :          0 : static int __set_tracer_option(struct tracer *trace,
    3353                 :            :                                struct tracer_flags *tracer_flags,
    3354                 :            :                                struct tracer_opt *opts, int neg)
    3355                 :            : {
    3356                 :            :         int ret;
    3357                 :            : 
    3358                 :          0 :         ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
    3359         [ #  # ]:          0 :         if (ret)
    3360                 :            :                 return ret;
    3361                 :            : 
    3362         [ #  # ]:          0 :         if (neg)
    3363                 :          0 :                 tracer_flags->val &= ~opts->bit;
    3364                 :            :         else
    3365                 :          0 :                 tracer_flags->val |= opts->bit;
    3366                 :            :         return 0;
    3367                 :            : }
    3368                 :            : 
    3369                 :            : /* Try to assign a tracer specific option */
    3370                 :          0 : static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
    3371                 :            : {
    3372                 :          0 :         struct tracer_flags *tracer_flags = trace->flags;
    3373                 :            :         struct tracer_opt *opts = NULL;
    3374                 :            :         int i;
    3375                 :            : 
    3376         [ #  # ]:          0 :         for (i = 0; tracer_flags->opts[i].name; i++) {
    3377                 :            :                 opts = &tracer_flags->opts[i];
    3378                 :            : 
    3379         [ #  # ]:          0 :                 if (strcmp(cmp, opts->name) == 0)
    3380                 :          0 :                         return __set_tracer_option(trace, trace->flags,
    3381                 :            :                                                    opts, neg);
    3382                 :            :         }
    3383                 :            : 
    3384                 :            :         return -EINVAL;
    3385                 :            : }
    3386                 :            : 
    3387                 :            : /* Some tracers require overwrite to stay enabled */
    3388                 :          0 : int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
    3389                 :            : {
    3390 [ #  # ][ #  # ]:          0 :         if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
                 [ #  # ]
    3391                 :            :                 return -1;
    3392                 :            : 
    3393                 :          0 :         return 0;
    3394                 :            : }
    3395                 :            : 
    3396                 :          0 : int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
    3397                 :            : {
    3398                 :            :         /* do nothing if flag is already set */
    3399         [ #  # ]:          0 :         if (!!(trace_flags & mask) == !!enabled)
    3400                 :            :                 return 0;
    3401                 :            : 
    3402                 :            :         /* Give the tracer a chance to approve the change */
    3403         [ #  # ]:          0 :         if (tr->current_trace->flag_changed)
    3404         [ #  # ]:          0 :                 if (tr->current_trace->flag_changed(tr->current_trace, mask, !!enabled))
    3405                 :            :                         return -EINVAL;
    3406                 :            : 
    3407         [ #  # ]:          0 :         if (enabled)
    3408                 :          0 :                 trace_flags |= mask;
    3409                 :            :         else
    3410                 :          0 :                 trace_flags &= ~mask;
    3411                 :            : 
    3412         [ #  # ]:          0 :         if (mask == TRACE_ITER_RECORD_CMD)
    3413                 :          0 :                 trace_event_enable_cmd_record(enabled);
    3414                 :            : 
    3415         [ #  # ]:          0 :         if (mask == TRACE_ITER_OVERWRITE) {
    3416                 :          0 :                 ring_buffer_change_overwrite(tr->trace_buffer.buffer, enabled);
    3417                 :            : #ifdef CONFIG_TRACER_MAX_TRACE
    3418                 :            :                 ring_buffer_change_overwrite(tr->max_buffer.buffer, enabled);
    3419                 :            : #endif
    3420                 :            :         }
    3421                 :            : 
    3422         [ #  # ]:          0 :         if (mask == TRACE_ITER_PRINTK)
    3423                 :          0 :                 trace_printk_start_stop_comm(enabled);
    3424                 :            : 
    3425                 :            :         return 0;
    3426                 :            : }
    3427                 :            : 
    3428                 :          0 : static int trace_set_options(struct trace_array *tr, char *option)
    3429                 :            : {
    3430                 :            :         char *cmp;
    3431                 :            :         int neg = 0;
    3432                 :            :         int ret = -ENODEV;
    3433                 :            :         int i;
    3434                 :            : 
    3435                 :            :         cmp = strstrip(option);
    3436                 :            : 
    3437         [ #  # ]:          0 :         if (strncmp(cmp, "no", 2) == 0) {
    3438                 :            :                 neg = 1;
    3439                 :          0 :                 cmp += 2;
    3440                 :            :         }
    3441                 :            : 
    3442                 :          0 :         mutex_lock(&trace_types_lock);
    3443                 :            : 
    3444         [ #  # ]:          0 :         for (i = 0; trace_options[i]; i++) {
    3445         [ #  # ]:          0 :                 if (strcmp(cmp, trace_options[i]) == 0) {
    3446                 :          0 :                         ret = set_tracer_flag(tr, 1 << i, !neg);
    3447                 :          0 :                         break;
    3448                 :            :                 }
    3449                 :            :         }
    3450                 :            : 
    3451                 :            :         /* If no option could be set, test the specific tracer options */
    3452         [ #  # ]:          0 :         if (!trace_options[i])
    3453                 :          0 :                 ret = set_tracer_option(tr->current_trace, cmp, neg);
    3454                 :            : 
    3455                 :          0 :         mutex_unlock(&trace_types_lock);
    3456                 :            : 
    3457                 :          0 :         return ret;
    3458                 :            : }
    3459                 :            : 
    3460                 :            : static ssize_t
    3461                 :          0 : tracing_trace_options_write(struct file *filp, const char __user *ubuf,
    3462                 :            :                         size_t cnt, loff_t *ppos)
    3463                 :            : {
    3464                 :          0 :         struct seq_file *m = filp->private_data;
    3465                 :          0 :         struct trace_array *tr = m->private;
    3466                 :            :         char buf[64];
    3467                 :            :         int ret;
    3468                 :            : 
    3469         [ #  # ]:          0 :         if (cnt >= sizeof(buf))
    3470                 :            :                 return -EINVAL;
    3471                 :            : 
    3472         [ #  # ]:          0 :         if (copy_from_user(&buf, ubuf, cnt))
    3473                 :            :                 return -EFAULT;
    3474                 :            : 
    3475                 :          0 :         buf[cnt] = 0;
    3476                 :            : 
    3477                 :          0 :         ret = trace_set_options(tr, buf);
    3478         [ #  # ]:          0 :         if (ret < 0)
    3479                 :            :                 return ret;
    3480                 :            : 
    3481                 :          0 :         *ppos += cnt;
    3482                 :            : 
    3483                 :          0 :         return cnt;
    3484                 :            : }
    3485                 :            : 
    3486                 :          0 : static int tracing_trace_options_open(struct inode *inode, struct file *file)
    3487                 :            : {
    3488                 :          0 :         struct trace_array *tr = inode->i_private;
    3489                 :            :         int ret;
    3490                 :            : 
    3491         [ #  # ]:          0 :         if (tracing_disabled)
    3492                 :            :                 return -ENODEV;
    3493                 :            : 
    3494         [ #  # ]:          0 :         if (trace_array_get(tr) < 0)
    3495                 :            :                 return -ENODEV;
    3496                 :            : 
    3497                 :          0 :         ret = single_open(file, tracing_trace_options_show, inode->i_private);
    3498         [ #  # ]:          0 :         if (ret < 0)
    3499                 :          0 :                 trace_array_put(tr);
    3500                 :            : 
    3501                 :          0 :         return ret;
    3502                 :            : }
    3503                 :            : 
    3504                 :            : static const struct file_operations tracing_iter_fops = {
    3505                 :            :         .open           = tracing_trace_options_open,
    3506                 :            :         .read           = seq_read,
    3507                 :            :         .llseek         = seq_lseek,
    3508                 :            :         .release        = tracing_single_release_tr,
    3509                 :            :         .write          = tracing_trace_options_write,
    3510                 :            : };
    3511                 :            : 
    3512                 :            : static const char readme_msg[] =
    3513                 :            :         "tracing mini-HOWTO:\n\n"
    3514                 :            :         "# echo 0 > tracing_on : quick way to disable tracing\n"
    3515                 :            :         "# echo 1 > tracing_on : quick way to re-enable tracing\n\n"
    3516                 :            :         " Important files:\n"
    3517                 :            :         "  trace\t\t\t- The static contents of the buffer\n"
    3518                 :            :         "\t\t\t  To clear the buffer write into this file: echo > trace\n"
    3519                 :            :         "  trace_pipe\t\t- A consuming read to see the contents of the buffer\n"
    3520                 :            :         "  current_tracer\t- function and latency tracers\n"
    3521                 :            :         "  available_tracers\t- list of configured tracers for current_tracer\n"
    3522                 :            :         "  buffer_size_kb\t- view and modify size of per cpu buffer\n"
    3523                 :            :         "  buffer_total_size_kb  - view total size of all cpu buffers\n\n"
    3524                 :            :         "  trace_clock\t\t-change the clock used to order events\n"
    3525                 :            :         "       local:   Per cpu clock but may not be synced across CPUs\n"
    3526                 :            :         "      global:   Synced across CPUs but slows tracing down.\n"
    3527                 :            :         "     counter:   Not a clock, but just an increment\n"
    3528                 :            :         "      uptime:   Jiffy counter from time of boot\n"
    3529                 :            :         "        perf:   Same clock that perf events use\n"
    3530                 :            : #ifdef CONFIG_X86_64
    3531                 :            :         "     x86-tsc:   TSC cycle counter\n"
    3532                 :            : #endif
    3533                 :            :         "\n  trace_marker\t\t- Writes into this file writes into the kernel buffer\n"
    3534                 :            :         "  tracing_cpumask\t- Limit which CPUs to trace\n"
    3535                 :            :         "  instances\t\t- Make sub-buffers with: mkdir instances/foo\n"
    3536                 :            :         "\t\t\t  Remove sub-buffer with rmdir\n"
    3537                 :            :         "  trace_options\t\t- Set format or modify how tracing happens\n"
    3538                 :            :         "\t\t\t  Disable an option by adding a suffix 'no' to the option name\n"
    3539                 :            : #ifdef CONFIG_DYNAMIC_FTRACE
    3540                 :            :         "\n  available_filter_functions - list of functions that can be filtered on\n"
    3541                 :            :         "  set_ftrace_filter\t- echo function name in here to only trace these functions\n"
    3542                 :            :         "            accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
    3543                 :            :         "            modules: Can select a group via module\n"
    3544                 :            :         "             Format: :mod:<module-name>\n"
    3545                 :            :         "             example: echo :mod:ext3 > set_ftrace_filter\n"
    3546                 :            :         "            triggers: a command to perform when function is hit\n"
    3547                 :            :         "              Format: <function>:<trigger>[:count]\n"
    3548                 :            :         "             trigger: traceon, traceoff\n"
    3549                 :            :         "                      enable_event:<system>:<event>\n"
    3550                 :            :         "                      disable_event:<system>:<event>\n"
    3551                 :            : #ifdef CONFIG_STACKTRACE
    3552                 :            :         "                      stacktrace\n"
    3553                 :            : #endif
    3554                 :            : #ifdef CONFIG_TRACER_SNAPSHOT
    3555                 :            :         "                      snapshot\n"
    3556                 :            : #endif
    3557                 :            :         "             example: echo do_fault:traceoff > set_ftrace_filter\n"
    3558                 :            :         "                      echo do_trap:traceoff:3 > set_ftrace_filter\n"
    3559                 :            :         "             The first one will disable tracing every time do_fault is hit\n"
    3560                 :            :         "             The second will disable tracing at most 3 times when do_trap is hit\n"
    3561                 :            :         "               The first time do trap is hit and it disables tracing, the counter\n"
    3562                 :            :         "               will decrement to 2. If tracing is already disabled, the counter\n"
    3563                 :            :         "               will not decrement. It only decrements when the trigger did work\n"
    3564                 :            :         "             To remove trigger without count:\n"
    3565                 :            :         "               echo '!<function>:<trigger> > set_ftrace_filter\n"
    3566                 :            :         "             To remove trigger with a count:\n"
    3567                 :            :         "               echo '!<function>:<trigger>:0 > set_ftrace_filter\n"
    3568                 :            :         "  set_ftrace_notrace\t- echo function name in here to never trace.\n"
    3569                 :            :         "            accepts: func_full_name, *func_end, func_begin*, *func_middle*\n"
    3570                 :            :         "            modules: Can select a group via module command :mod:\n"
    3571                 :            :         "            Does not accept triggers\n"
    3572                 :            : #endif /* CONFIG_DYNAMIC_FTRACE */
    3573                 :            : #ifdef CONFIG_FUNCTION_TRACER
    3574                 :            :         "  set_ftrace_pid\t- Write pid(s) to only function trace those pids (function)\n"
    3575                 :            : #endif
    3576                 :            : #ifdef CONFIG_FUNCTION_GRAPH_TRACER
    3577                 :            :         "  set_graph_function\t- Trace the nested calls of a function (function_graph)\n"
    3578                 :            :         "  max_graph_depth\t- Trace a limited depth of nested calls (0 is unlimited)\n"
    3579                 :            : #endif
    3580                 :            : #ifdef CONFIG_TRACER_SNAPSHOT
    3581                 :            :         "\n  snapshot\t\t- Like 'trace' but shows the content of the static snapshot buffer\n"
    3582                 :            :         "\t\t\t  Read the contents for more information\n"
    3583                 :            : #endif
    3584                 :            : #ifdef CONFIG_STACK_TRACER
    3585                 :            :         "  stack_trace\t\t- Shows the max stack trace when active\n"
    3586                 :            :         "  stack_max_size\t- Shows current max stack size that was traced\n"
    3587                 :            :         "\t\t\t  Write into this file to reset the max size (trigger a new trace)\n"
    3588                 :            : #ifdef CONFIG_DYNAMIC_FTRACE
    3589                 :            :         "  stack_trace_filter\t- Like set_ftrace_filter but limits what stack_trace traces\n"
    3590                 :            : #endif
    3591                 :            : #endif /* CONFIG_STACK_TRACER */
    3592                 :            : ;
    3593                 :            : 
    3594                 :            : static ssize_t
    3595                 :          0 : tracing_readme_read(struct file *filp, char __user *ubuf,
    3596                 :            :                        size_t cnt, loff_t *ppos)
    3597                 :            : {
    3598                 :          0 :         return simple_read_from_buffer(ubuf, cnt, ppos,
    3599                 :            :                                         readme_msg, strlen(readme_msg));
    3600                 :            : }
    3601                 :            : 
    3602                 :            : static const struct file_operations tracing_readme_fops = {
    3603                 :            :         .open           = tracing_open_generic,
    3604                 :            :         .read           = tracing_readme_read,
    3605                 :            :         .llseek         = generic_file_llseek,
    3606                 :            : };
    3607                 :            : 
    3608                 :            : static ssize_t
    3609                 :          0 : tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
    3610                 :            :                                 size_t cnt, loff_t *ppos)
    3611                 :            : {
    3612                 :            :         char *buf_comm;
    3613                 :            :         char *file_buf;
    3614                 :            :         char *buf;
    3615                 :            :         int len = 0;
    3616                 :            :         int pid;
    3617                 :            :         int i;
    3618                 :            : 
    3619                 :            :         file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
    3620         [ #  # ]:          0 :         if (!file_buf)
    3621                 :            :                 return -ENOMEM;
    3622                 :            : 
    3623                 :            :         buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
    3624         [ #  # ]:          0 :         if (!buf_comm) {
    3625                 :          0 :                 kfree(file_buf);
    3626                 :          0 :                 return -ENOMEM;
    3627                 :            :         }
    3628                 :            : 
    3629                 :            :         buf = file_buf;
    3630                 :            : 
    3631         [ #  # ]:          0 :         for (i = 0; i < SAVED_CMDLINES; i++) {
    3632                 :            :                 int r;
    3633                 :            : 
    3634                 :          0 :                 pid = map_cmdline_to_pid[i];
    3635         [ #  # ]:          0 :                 if (pid == -1 || pid == NO_CMDLINE_MAP)
    3636                 :          0 :                         continue;
    3637                 :            : 
    3638                 :          0 :                 trace_find_cmdline(pid, buf_comm);
    3639                 :          0 :                 r = sprintf(buf, "%d %s\n", pid, buf_comm);
    3640                 :          0 :                 buf += r;
    3641                 :          0 :                 len += r;
    3642                 :            :         }
    3643                 :            : 
    3644                 :          0 :         len = simple_read_from_buffer(ubuf, cnt, ppos,
    3645                 :            :                                       file_buf, len);
    3646                 :            : 
    3647                 :          0 :         kfree(file_buf);
    3648                 :          0 :         kfree(buf_comm);
    3649                 :            : 
    3650                 :          0 :         return len;
    3651                 :            : }
    3652                 :            : 
    3653                 :            : static const struct file_operations tracing_saved_cmdlines_fops = {
    3654                 :            :         .open   = tracing_open_generic,
    3655                 :            :         .read   = tracing_saved_cmdlines_read,
    3656                 :            :         .llseek = generic_file_llseek,
    3657                 :            : };
    3658                 :            : 
    3659                 :            : static ssize_t
    3660                 :          0 : tracing_saved_tgids_read(struct file *file, char __user *ubuf,
    3661                 :            :                                 size_t cnt, loff_t *ppos)
    3662                 :            : {
    3663                 :            :         char *file_buf;
    3664                 :            :         char *buf;
    3665                 :            :         int len = 0;
    3666                 :            :         int pid;
    3667                 :            :         int i;
    3668                 :            : 
    3669                 :            :         file_buf = kmalloc(SAVED_CMDLINES*(16+1+16), GFP_KERNEL);
    3670         [ #  # ]:          0 :         if (!file_buf)
    3671                 :            :                 return -ENOMEM;
    3672                 :            : 
    3673                 :            :         buf = file_buf;
    3674                 :            : 
    3675         [ #  # ]:          0 :         for (i = 0; i < SAVED_CMDLINES; i++) {
    3676                 :            :                 int tgid;
    3677                 :            :                 int r;
    3678                 :            : 
    3679                 :          0 :                 pid = map_cmdline_to_pid[i];
    3680         [ #  # ]:          0 :                 if (pid == -1 || pid == NO_CMDLINE_MAP)
    3681                 :          0 :                         continue;
    3682                 :            : 
    3683                 :          0 :                 tgid = trace_find_tgid(pid);
    3684                 :          0 :                 r = sprintf(buf, "%d %d\n", pid, tgid);
    3685                 :          0 :                 buf += r;
    3686                 :          0 :                 len += r;
    3687                 :            :         }
    3688                 :            : 
    3689                 :          0 :         len = simple_read_from_buffer(ubuf, cnt, ppos,
    3690                 :            :                                       file_buf, len);
    3691                 :            : 
    3692                 :          0 :         kfree(file_buf);
    3693                 :            : 
    3694                 :          0 :         return len;
    3695                 :            : }
    3696                 :            : 
    3697                 :            : static const struct file_operations tracing_saved_tgids_fops = {
    3698                 :            :         .open   = tracing_open_generic,
    3699                 :            :         .read   = tracing_saved_tgids_read,
    3700                 :            :         .llseek = generic_file_llseek,
    3701                 :            : };
    3702                 :            : 
    3703                 :            : static ssize_t
    3704                 :          0 : tracing_set_trace_read(struct file *filp, char __user *ubuf,
    3705                 :            :                        size_t cnt, loff_t *ppos)
    3706                 :            : {
    3707                 :          0 :         struct trace_array *tr = filp->private_data;
    3708                 :            :         char buf[MAX_TRACER_SIZE+2];
    3709                 :            :         int r;
    3710                 :            : 
    3711                 :          0 :         mutex_lock(&trace_types_lock);
    3712                 :          0 :         r = sprintf(buf, "%s\n", tr->current_trace->name);
    3713                 :          0 :         mutex_unlock(&trace_types_lock);
    3714                 :            : 
    3715                 :          0 :         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
    3716                 :            : }
    3717                 :            : 
    3718                 :          0 : int tracer_init(struct tracer *t, struct trace_array *tr)
    3719                 :            : {
    3720                 :          0 :         tracing_reset_online_cpus(&tr->trace_buffer);
    3721                 :          0 :         return t->init(tr);
    3722                 :            : }
    3723                 :            : 
    3724                 :          0 : static void set_buffer_entries(struct trace_buffer *buf, unsigned long val)
    3725                 :            : {
    3726                 :            :         int cpu;
    3727                 :            : 
    3728         [ #  # ]:          0 :         for_each_tracing_cpu(cpu)
    3729                 :          0 :                 per_cpu_ptr(buf->data, cpu)->entries = val;
    3730                 :          0 : }
    3731                 :            : 
    3732                 :            : #ifdef CONFIG_TRACER_MAX_TRACE
    3733                 :            : /* resize @tr's buffer to the size of @size_tr's entries */
    3734                 :            : static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
    3735                 :            :                                         struct trace_buffer *size_buf, int cpu_id)
    3736                 :            : {
    3737                 :            :         int cpu, ret = 0;
    3738                 :            : 
    3739                 :            :         if (cpu_id == RING_BUFFER_ALL_CPUS) {
    3740                 :            :                 for_each_tracing_cpu(cpu) {
    3741                 :            :                         ret = ring_buffer_resize(trace_buf->buffer,
    3742                 :            :                                  per_cpu_ptr(size_buf->data, cpu)->entries, cpu);
    3743                 :            :                         if (ret < 0)
    3744                 :            :                                 break;
    3745                 :            :                         per_cpu_ptr(trace_buf->data, cpu)->entries =
    3746                 :            :                                 per_cpu_ptr(size_buf->data, cpu)->entries;
    3747                 :            :                 }
    3748                 :            :         } else {
    3749                 :            :                 ret = ring_buffer_resize(trace_buf->buffer,
    3750                 :            :                                  per_cpu_ptr(size_buf->data, cpu_id)->entries, cpu_id);
    3751                 :            :                 if (ret == 0)
    3752                 :            :                         per_cpu_ptr(trace_buf->data, cpu_id)->entries =
    3753                 :            :                                 per_cpu_ptr(size_buf->data, cpu_id)->entries;
    3754                 :            :         }
    3755                 :            : 
    3756                 :            :         return ret;
    3757                 :            : }
    3758                 :            : #endif /* CONFIG_TRACER_MAX_TRACE */
    3759                 :            : 
    3760                 :          0 : static int __tracing_resize_ring_buffer(struct trace_array *tr,
    3761                 :            :                                         unsigned long size, int cpu)
    3762                 :            : {
    3763                 :            :         int ret;
    3764                 :            : 
    3765                 :            :         /*
    3766                 :            :          * If kernel or user changes the size of the ring buffer
    3767                 :            :          * we use the size that was given, and we can forget about
    3768                 :            :          * expanding it later.
    3769                 :            :          */
    3770                 :          0 :         ring_buffer_expanded = true;
    3771                 :            : 
    3772                 :            :         /* May be called before buffers are initialized */
    3773         [ #  # ]:          0 :         if (!tr->trace_buffer.buffer)
    3774                 :            :                 return 0;
    3775                 :            : 
    3776                 :          0 :         ret = ring_buffer_resize(tr->trace_buffer.buffer, size, cpu);
    3777         [ #  # ]:          0 :         if (ret < 0)
    3778                 :            :                 return ret;
    3779                 :            : 
    3780                 :            : #ifdef CONFIG_TRACER_MAX_TRACE
    3781                 :            :         if (!(tr->flags & TRACE_ARRAY_FL_GLOBAL) ||
    3782                 :            :             !tr->current_trace->use_max_tr)
    3783                 :            :                 goto out;
    3784                 :            : 
    3785                 :            :         ret = ring_buffer_resize(tr->max_buffer.buffer, size, cpu);
    3786                 :            :         if (ret < 0) {
    3787                 :            :                 int r = resize_buffer_duplicate_size(&tr->trace_buffer,
    3788                 :            :                                                      &tr->trace_buffer, cpu);
    3789                 :            :                 if (r < 0) {
    3790                 :            :                         /*
    3791                 :            :                          * AARGH! We are left with different
    3792                 :            :                          * size max buffer!!!!
    3793                 :            :                          * The max buffer is our "snapshot" buffer.
    3794                 :            :                          * When a tracer needs a snapshot (one of the
    3795                 :            :                          * latency tracers), it swaps the max buffer
    3796                 :            :                          * with the saved snap shot. We succeeded to
    3797                 :            :                          * update the size of the main buffer, but failed to
    3798                 :            :                          * update the size of the max buffer. But when we tried
    3799                 :            :                          * to reset the main buffer to the original size, we
    3800                 :            :                          * failed there too. This is very unlikely to
    3801                 :            :                          * happen, but if it does, warn and kill all
    3802                 :            :                          * tracing.
    3803                 :            :                          */
    3804                 :            :                         WARN_ON(1);
    3805                 :            :                         tracing_disabled = 1;
    3806                 :            :                 }
    3807                 :            :                 return ret;
    3808                 :            :         }
    3809                 :            : 
    3810                 :            :         if (cpu == RING_BUFFER_ALL_CPUS)
    3811                 :            :                 set_buffer_entries(&tr->max_buffer, size);
    3812                 :            :         else
    3813                 :            :                 per_cpu_ptr(tr->max_buffer.data, cpu)->entries = size;
    3814                 :            : 
    3815                 :            :  out:
    3816                 :            : #endif /* CONFIG_TRACER_MAX_TRACE */
    3817                 :            : 
    3818         [ #  # ]:          0 :         if (cpu == RING_BUFFER_ALL_CPUS)
    3819                 :          0 :                 set_buffer_entries(&tr->trace_buffer, size);
    3820                 :            :         else
    3821                 :          0 :                 per_cpu_ptr(tr->trace_buffer.data, cpu)->entries = size;
    3822                 :            : 
    3823                 :          0 :         return ret;
    3824                 :            : }
    3825                 :            : 
    3826                 :          0 : static ssize_t tracing_resize_ring_buffer(struct trace_array *tr,
    3827                 :            :                                           unsigned long size, int cpu_id)
    3828                 :            : {
    3829                 :            :         int ret = size;
    3830                 :            : 
    3831                 :          0 :         mutex_lock(&trace_types_lock);
    3832                 :            : 
    3833         [ #  # ]:          0 :         if (cpu_id != RING_BUFFER_ALL_CPUS) {
    3834                 :            :                 /* make sure, this cpu is enabled in the mask */
    3835         [ #  # ]:          0 :                 if (!cpumask_test_cpu(cpu_id, tracing_buffer_mask)) {
    3836                 :            :                         ret = -EINVAL;
    3837                 :            :                         goto out;
    3838                 :            :                 }
    3839                 :            :         }
    3840                 :            : 
    3841                 :          0 :         ret = __tracing_resize_ring_buffer(tr, size, cpu_id);
    3842         [ #  # ]:          0 :         if (ret < 0)
    3843                 :            :                 ret = -ENOMEM;
    3844                 :            : 
    3845                 :            : out:
    3846                 :          0 :         mutex_unlock(&trace_types_lock);
    3847                 :            : 
    3848                 :          0 :         return ret;
    3849                 :            : }
    3850                 :            : 
    3851                 :            : 
    3852                 :            : /**
    3853                 :            :  * tracing_update_buffers - used by tracing facility to expand ring buffers
    3854                 :            :  *
    3855                 :            :  * To save on memory when the tracing is never used on a system with it
    3856                 :            :  * configured in. The ring buffers are set to a minimum size. But once
    3857                 :            :  * a user starts to use the tracing facility, then they need to grow
    3858                 :            :  * to their default size.
    3859                 :            :  *
    3860                 :            :  * This function is to be called when a tracer is about to be used.
    3861                 :            :  */
    3862                 :          0 : int tracing_update_buffers(void)
    3863                 :            : {
    3864                 :            :         int ret = 0;
    3865                 :            : 
    3866                 :          0 :         mutex_lock(&trace_types_lock);
    3867         [ #  # ]:          0 :         if (!ring_buffer_expanded)
    3868                 :          0 :                 ret = __tracing_resize_ring_buffer(&global_trace, trace_buf_size,
    3869                 :            :                                                 RING_BUFFER_ALL_CPUS);
    3870                 :          0 :         mutex_unlock(&trace_types_lock);
    3871                 :            : 
    3872                 :          0 :         return ret;
    3873                 :            : }
    3874                 :            : 
    3875                 :            : struct trace_option_dentry;
    3876                 :            : 
    3877                 :            : static struct trace_option_dentry *
    3878                 :            : create_trace_option_files(struct trace_array *tr, struct tracer *tracer);
    3879                 :            : 
    3880                 :            : static void
    3881                 :            : destroy_trace_option_files(struct trace_option_dentry *topts);
    3882                 :            : 
    3883                 :          0 : static int tracing_set_tracer(const char *buf)
    3884                 :            : {
    3885                 :            :         static struct trace_option_dentry *topts;
    3886                 :            :         struct trace_array *tr = &global_trace;
    3887                 :            :         struct tracer *t;
    3888                 :            : #ifdef CONFIG_TRACER_MAX_TRACE
    3889                 :            :         bool had_max_tr;
    3890                 :            : #endif
    3891                 :            :         int ret = 0;
    3892                 :            : 
    3893                 :          0 :         mutex_lock(&trace_types_lock);
    3894                 :            : 
    3895         [ #  # ]:          0 :         if (!ring_buffer_expanded) {
    3896                 :          0 :                 ret = __tracing_resize_ring_buffer(tr, trace_buf_size,
    3897                 :            :                                                 RING_BUFFER_ALL_CPUS);
    3898         [ #  # ]:          0 :                 if (ret < 0)
    3899                 :            :                         goto out;
    3900                 :            :                 ret = 0;
    3901                 :            :         }
    3902                 :            : 
    3903         [ #  # ]:          0 :         for (t = trace_types; t; t = t->next) {
    3904         [ #  # ]:          0 :                 if (strcmp(t->name, buf) == 0)
    3905                 :            :                         break;
    3906                 :            :         }
    3907         [ #  # ]:          0 :         if (!t) {
    3908                 :            :                 ret = -EINVAL;
    3909                 :            :                 goto out;
    3910                 :            :         }
    3911         [ #  # ]:          0 :         if (t == tr->current_trace)
    3912                 :            :                 goto out;
    3913                 :            : 
    3914                 :            :         trace_branch_disable();
    3915                 :            : 
    3916                 :          0 :         tr->current_trace->enabled = false;
    3917                 :            : 
    3918         [ #  # ]:          0 :         if (tr->current_trace->reset)
    3919                 :          0 :                 tr->current_trace->reset(tr);
    3920                 :            : 
    3921                 :            :         /* Current trace needs to be nop_trace before synchronize_sched */
    3922                 :          0 :         tr->current_trace = &nop_trace;
    3923                 :            : 
    3924                 :            : #ifdef CONFIG_TRACER_MAX_TRACE
    3925                 :            :         had_max_tr = tr->allocated_snapshot;
    3926                 :            : 
    3927                 :            :         if (had_max_tr && !t->use_max_tr) {
    3928                 :            :                 /*
    3929                 :            :                  * We need to make sure that the update_max_tr sees that
    3930                 :            :                  * current_trace changed to nop_trace to keep it from
    3931                 :            :                  * swapping the buffers after we resize it.
    3932                 :            :                  * The update_max_tr is called from interrupts disabled
    3933                 :            :                  * so a synchronized_sched() is sufficient.
    3934                 :            :                  */
    3935                 :            :                 synchronize_sched();
    3936                 :            :                 free_snapshot(tr);
    3937                 :            :         }
    3938                 :            : #endif
    3939                 :          0 :         destroy_trace_option_files(topts);
    3940                 :            : 
    3941                 :          0 :         topts = create_trace_option_files(tr, t);
    3942                 :            : 
    3943                 :            : #ifdef CONFIG_TRACER_MAX_TRACE
    3944                 :            :         if (t->use_max_tr && !had_max_tr) {
    3945                 :            :                 ret = alloc_snapshot(tr);
    3946                 :            :                 if (ret < 0)
    3947                 :            :                         goto out;
    3948                 :            :         }
    3949                 :            : #endif
    3950                 :            : 
    3951         [ #  # ]:          0 :         if (t->init) {
    3952                 :            :                 ret = tracer_init(t, tr);
    3953         [ #  # ]:          0 :                 if (ret)
    3954                 :            :                         goto out;
    3955                 :            :         }
    3956                 :            : 
    3957                 :          0 :         tr->current_trace = t;
    3958                 :          0 :         tr->current_trace->enabled = true;
    3959                 :            :         trace_branch_enable(tr);
    3960                 :            :  out:
    3961                 :          0 :         mutex_unlock(&trace_types_lock);
    3962                 :            : 
    3963                 :          0 :         return ret;
    3964                 :            : }
    3965                 :            : 
    3966                 :            : static ssize_t
    3967                 :          0 : tracing_set_trace_write(struct file *filp, const char __user *ubuf,
    3968                 :            :                         size_t cnt, loff_t *ppos)
    3969                 :            : {
    3970                 :            :         char buf[MAX_TRACER_SIZE+1];
    3971                 :            :         int i;
    3972                 :            :         size_t ret;
    3973                 :            :         int err;
    3974                 :            : 
    3975                 :            :         ret = cnt;
    3976                 :            : 
    3977         [ #  # ]:          0 :         if (cnt > MAX_TRACER_SIZE)
    3978                 :            :                 cnt = MAX_TRACER_SIZE;
    3979                 :            : 
    3980         [ #  # ]:          0 :         if (copy_from_user(&buf, ubuf, cnt))
    3981                 :            :                 return -EFAULT;
    3982                 :            : 
    3983                 :          0 :         buf[cnt] = 0;
    3984                 :            : 
    3985                 :            :         /* strip ending whitespace. */
    3986 [ #  # ][ #  # ]:          0 :         for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
    3987                 :          0 :                 buf[i] = 0;
    3988                 :            : 
    3989                 :          0 :         err = tracing_set_tracer(buf);
    3990         [ #  # ]:          0 :         if (err)
    3991                 :            :                 return err;
    3992                 :            : 
    3993                 :          0 :         *ppos += ret;
    3994                 :            : 
    3995                 :          0 :         return ret;
    3996                 :            : }
    3997                 :            : 
    3998                 :            : static ssize_t
    3999                 :          0 : tracing_max_lat_read(struct file *filp, char __user *ubuf,
    4000                 :            :                      size_t cnt, loff_t *ppos)
    4001                 :            : {
    4002                 :          0 :         unsigned long *ptr = filp->private_data;
    4003                 :            :         char buf[64];
    4004                 :            :         int r;
    4005                 :            : 
    4006         [ #  # ]:          0 :         r = snprintf(buf, sizeof(buf), "%ld\n",
    4007                 :          0 :                      *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
    4008         [ #  # ]:          0 :         if (r > sizeof(buf))
    4009                 :            :                 r = sizeof(buf);
    4010                 :          0 :         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
    4011                 :            : }
    4012                 :            : 
    4013                 :            : static ssize_t
    4014                 :          0 : tracing_max_lat_write(struct file *filp, const char __user *ubuf,
    4015                 :            :                       size_t cnt, loff_t *ppos)
    4016                 :            : {
    4017                 :          0 :         unsigned long *ptr = filp->private_data;
    4018                 :            :         unsigned long val;
    4019                 :            :         int ret;
    4020                 :            : 
    4021                 :          0 :         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
    4022         [ #  # ]:          0 :         if (ret)
    4023                 :            :                 return ret;
    4024                 :            : 
    4025                 :          0 :         *ptr = val * 1000;
    4026                 :            : 
    4027                 :          0 :         return cnt;
    4028                 :            : }
    4029                 :            : 
    4030                 :          0 : static int tracing_open_pipe(struct inode *inode, struct file *filp)
    4031                 :            : {
    4032                 :          0 :         struct trace_array *tr = inode->i_private;
    4033                 :            :         struct trace_iterator *iter;
    4034                 :            :         int ret = 0;
    4035                 :            : 
    4036         [ #  # ]:          0 :         if (tracing_disabled)
    4037                 :            :                 return -ENODEV;
    4038                 :            : 
    4039         [ #  # ]:          0 :         if (trace_array_get(tr) < 0)
    4040                 :            :                 return -ENODEV;
    4041                 :            : 
    4042                 :          0 :         mutex_lock(&trace_types_lock);
    4043                 :            : 
    4044                 :            :         /* create a buffer to store the information to pass to userspace */
    4045                 :            :         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
    4046         [ #  # ]:          0 :         if (!iter) {
    4047                 :            :                 ret = -ENOMEM;
    4048                 :          0 :                 __trace_array_put(tr);
    4049                 :          0 :                 goto out;
    4050                 :            :         }
    4051                 :            : 
    4052                 :            :         /*
    4053                 :            :          * We make a copy of the current tracer to avoid concurrent
    4054                 :            :          * changes on it while we are reading.
    4055                 :            :          */
    4056                 :          0 :         iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
    4057         [ #  # ]:          0 :         if (!iter->trace) {
    4058                 :            :                 ret = -ENOMEM;
    4059                 :            :                 goto fail;
    4060                 :            :         }
    4061                 :          0 :         *iter->trace = *tr->current_trace;
    4062                 :            : 
    4063                 :            :         if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
    4064                 :            :                 ret = -ENOMEM;
    4065                 :            :                 goto fail;
    4066                 :            :         }
    4067                 :            : 
    4068                 :            :         /* trace pipe does not show start of buffer */
    4069                 :            :         cpumask_setall(iter->started);
    4070                 :            : 
    4071         [ #  # ]:          0 :         if (trace_flags & TRACE_ITER_LATENCY_FMT)
    4072                 :          0 :                 iter->iter_flags |= TRACE_FILE_LAT_FMT;
    4073                 :            : 
    4074                 :            :         /* Output in nanoseconds only if we are using a clock in nanoseconds. */
    4075         [ #  # ]:          0 :         if (trace_clocks[tr->clock_id].in_ns)
    4076                 :          0 :                 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
    4077                 :            : 
    4078                 :          0 :         iter->tr = tr;
    4079                 :          0 :         iter->trace_buffer = &tr->trace_buffer;
    4080                 :          0 :         iter->cpu_file = tracing_get_cpu(inode);
    4081                 :          0 :         mutex_init(&iter->mutex);
    4082                 :          0 :         filp->private_data = iter;
    4083                 :            : 
    4084         [ #  # ]:          0 :         if (iter->trace->pipe_open)
    4085                 :          0 :                 iter->trace->pipe_open(iter);
    4086                 :            : 
    4087                 :          0 :         nonseekable_open(inode, filp);
    4088                 :            : out:
    4089                 :          0 :         mutex_unlock(&trace_types_lock);
    4090                 :          0 :         return ret;
    4091                 :            : 
    4092                 :            : fail:
    4093                 :          0 :         kfree(iter->trace);
    4094                 :          0 :         kfree(iter);
    4095                 :          0 :         __trace_array_put(tr);
    4096                 :          0 :         mutex_unlock(&trace_types_lock);
    4097                 :          0 :         return ret;
    4098                 :            : }
    4099                 :            : 
    4100                 :          0 : static int tracing_release_pipe(struct inode *inode, struct file *file)
    4101                 :            : {
    4102                 :          0 :         struct trace_iterator *iter = file->private_data;
    4103                 :          0 :         struct trace_array *tr = inode->i_private;
    4104                 :            : 
    4105                 :          0 :         mutex_lock(&trace_types_lock);
    4106                 :            : 
    4107         [ #  # ]:          0 :         if (iter->trace->pipe_close)
    4108                 :          0 :                 iter->trace->pipe_close(iter);
    4109                 :            : 
    4110                 :          0 :         mutex_unlock(&trace_types_lock);
    4111                 :            : 
    4112                 :            :         free_cpumask_var(iter->started);
    4113                 :            :         mutex_destroy(&iter->mutex);
    4114                 :          0 :         kfree(iter->trace);
    4115                 :          0 :         kfree(iter);
    4116                 :            : 
    4117                 :          0 :         trace_array_put(tr);
    4118                 :            : 
    4119                 :          0 :         return 0;
    4120                 :            : }
    4121                 :            : 
    4122                 :            : static unsigned int
    4123                 :          0 : trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
    4124                 :            : {
    4125                 :            :         /* Iterators are static, they should be filled or empty */
    4126         [ #  # ]:          0 :         if (trace_buffer_iter(iter, iter->cpu_file))
    4127                 :            :                 return POLLIN | POLLRDNORM;
    4128                 :            : 
    4129         [ #  # ]:          0 :         if (trace_flags & TRACE_ITER_BLOCK)
    4130                 :            :                 /*
    4131                 :            :                  * Always select as readable when in blocking mode
    4132                 :            :                  */
    4133                 :            :                 return POLLIN | POLLRDNORM;
    4134                 :            :         else
    4135                 :          0 :                 return ring_buffer_poll_wait(iter->trace_buffer->buffer, iter->cpu_file,
    4136                 :            :                                              filp, poll_table);
    4137                 :            : }
    4138                 :            : 
    4139                 :            : static unsigned int
    4140                 :          0 : tracing_poll_pipe(struct file *filp, poll_table *poll_table)
    4141                 :            : {
    4142                 :          0 :         struct trace_iterator *iter = filp->private_data;
    4143                 :            : 
    4144                 :          0 :         return trace_poll(iter, filp, poll_table);
    4145                 :            : }
    4146                 :            : 
    4147                 :            : /*
    4148                 :            :  * This is a make-shift waitqueue.
    4149                 :            :  * A tracer might use this callback on some rare cases:
    4150                 :            :  *
    4151                 :            :  *  1) the current tracer might hold the runqueue lock when it wakes up
    4152                 :            :  *     a reader, hence a deadlock (sched, function, and function graph tracers)
    4153                 :            :  *  2) the function tracers, trace all functions, we don't want
    4154                 :            :  *     the overhead of calling wake_up and friends
    4155                 :            :  *     (and tracing them too)
    4156                 :            :  *
    4157                 :            :  *     Anyway, this is really very primitive wakeup.
    4158                 :            :  */
    4159                 :          0 : void poll_wait_pipe(struct trace_iterator *iter)
    4160                 :            : {
    4161                 :          0 :         set_current_state(TASK_INTERRUPTIBLE);
    4162                 :            :         /* sleep for 100 msecs, and try again. */
    4163                 :          0 :         schedule_timeout(HZ / 10);
    4164                 :          0 : }
    4165                 :            : 
    4166                 :            : /* Must be called with trace_types_lock mutex held. */
    4167                 :          0 : static int tracing_wait_pipe(struct file *filp)
    4168                 :            : {
    4169                 :          0 :         struct trace_iterator *iter = filp->private_data;
    4170                 :            : 
    4171         [ #  # ]:          0 :         while (trace_empty(iter)) {
    4172                 :            : 
    4173         [ #  # ]:          0 :                 if ((filp->f_flags & O_NONBLOCK)) {
    4174                 :            :                         return -EAGAIN;
    4175                 :            :                 }
    4176                 :            : 
    4177                 :          0 :                 mutex_unlock(&iter->mutex);
    4178                 :            : 
    4179                 :          0 :                 iter->trace->wait_pipe(iter);
    4180                 :            : 
    4181                 :          0 :                 mutex_lock(&iter->mutex);
    4182                 :            : 
    4183         [ #  # ]:          0 :                 if (signal_pending(current))
    4184                 :            :                         return -EINTR;
    4185                 :            : 
    4186                 :            :                 /*
    4187                 :            :                  * We block until we read something and tracing is disabled.
    4188                 :            :                  * We still block if tracing is disabled, but we have never
    4189                 :            :                  * read anything. This allows a user to cat this file, and
    4190                 :            :                  * then enable tracing. But after we have read something,
    4191                 :            :                  * we give an EOF when tracing is again disabled.
    4192                 :            :                  *
    4193                 :            :                  * iter->pos will be 0 if we haven't read anything.
    4194                 :            :                  */
    4195 [ #  # ][ #  # ]:          0 :                 if (!tracing_is_on() && iter->pos)
    4196                 :            :                         break;
    4197                 :            :         }
    4198                 :            : 
    4199                 :            :         return 1;
    4200                 :            : }
    4201                 :            : 
    4202                 :            : /*
    4203                 :            :  * Consumer reader.
    4204                 :            :  */
    4205                 :            : static ssize_t
    4206                 :          0 : tracing_read_pipe(struct file *filp, char __user *ubuf,
    4207                 :            :                   size_t cnt, loff_t *ppos)
    4208                 :            : {
    4209                 :          0 :         struct trace_iterator *iter = filp->private_data;
    4210                 :          0 :         struct trace_array *tr = iter->tr;
    4211                 :            :         ssize_t sret;
    4212                 :            : 
    4213                 :            :         /* return any leftover data */
    4214                 :          0 :         sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
    4215         [ #  # ]:          0 :         if (sret != -EBUSY)
    4216                 :            :                 return sret;
    4217                 :            : 
    4218                 :            :         trace_seq_init(&iter->seq);
    4219                 :            : 
    4220                 :            :         /* copy the tracer to avoid using a global lock all around */
    4221                 :          0 :         mutex_lock(&trace_types_lock);
    4222         [ #  # ]:          0 :         if (unlikely(iter->trace->name != tr->current_trace->name))
    4223                 :          0 :                 *iter->trace = *tr->current_trace;
    4224                 :          0 :         mutex_unlock(&trace_types_lock);
    4225                 :            : 
    4226                 :            :         /*
    4227                 :            :          * Avoid more than one consumer on a single file descriptor
    4228                 :            :          * This is just a matter of traces coherency, the ring buffer itself
    4229                 :            :          * is protected.
    4230                 :            :          */
    4231                 :          0 :         mutex_lock(&iter->mutex);
    4232         [ #  # ]:          0 :         if (iter->trace->read) {
    4233                 :          0 :                 sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
    4234         [ #  # ]:          0 :                 if (sret)
    4235                 :            :                         goto out;
    4236                 :            :         }
    4237                 :            : 
    4238                 :            : waitagain:
    4239                 :          0 :         sret = tracing_wait_pipe(filp);
    4240         [ #  # ]:          0 :         if (sret <= 0)
    4241                 :            :                 goto out;
    4242                 :            : 
    4243                 :            :         /* stop when tracing is finished */
    4244         [ #  # ]:          0 :         if (trace_empty(iter)) {
    4245                 :            :                 sret = 0;
    4246                 :            :                 goto out;
    4247                 :            :         }
    4248                 :            : 
    4249         [ #  # ]:          0 :         if (cnt >= PAGE_SIZE)
    4250                 :            :                 cnt = PAGE_SIZE - 1;
    4251                 :            : 
    4252                 :            :         /* reset all but tr, trace, and overruns */
    4253                 :          0 :         memset(&iter->seq, 0,
    4254                 :            :                sizeof(struct trace_iterator) -
    4255                 :            :                offsetof(struct trace_iterator, seq));
    4256                 :            :         cpumask_clear(iter->started);
    4257                 :          0 :         iter->pos = -1;
    4258                 :            : 
    4259                 :          0 :         trace_event_read_lock();
    4260                 :          0 :         trace_access_lock(iter->cpu_file);
    4261         [ #  # ]:          0 :         while (trace_find_next_entry_inc(iter) != NULL) {
    4262                 :            :                 enum print_line_t ret;
    4263                 :          0 :                 int len = iter->seq.len;
    4264                 :            : 
    4265                 :          0 :                 ret = print_trace_line(iter);
    4266         [ #  # ]:          0 :                 if (ret == TRACE_TYPE_PARTIAL_LINE) {
    4267                 :            :                         /* don't print partial lines */
    4268                 :          0 :                         iter->seq.len = len;
    4269                 :          0 :                         break;
    4270                 :            :                 }
    4271         [ #  # ]:          0 :                 if (ret != TRACE_TYPE_NO_CONSUME)
    4272                 :            :                         trace_consume(iter);
    4273                 :            : 
    4274         [ #  # ]:          0 :                 if (iter->seq.len >= cnt)
    4275                 :            :                         break;
    4276                 :            : 
    4277                 :            :                 /*
    4278                 :            :                  * Setting the full flag means we reached the trace_seq buffer
    4279                 :            :                  * size and we should leave by partial output condition above.
    4280                 :            :                  * One of the trace_seq_* functions is not used properly.
    4281                 :            :                  */
    4282 [ #  # ][ #  # ]:          0 :                 WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
                 [ #  # ]
    4283                 :            :                           iter->ent->type);
    4284                 :            :         }
    4285                 :          0 :         trace_access_unlock(iter->cpu_file);
    4286                 :          0 :         trace_event_read_unlock();
    4287                 :            : 
    4288                 :            :         /* Now copy what we have to the user */
    4289                 :          0 :         sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
    4290         [ #  # ]:          0 :         if (iter->seq.readpos >= iter->seq.len)
    4291                 :            :                 trace_seq_init(&iter->seq);
    4292                 :            : 
    4293                 :            :         /*
    4294                 :            :          * If there was nothing to send to user, in spite of consuming trace
    4295                 :            :          * entries, go back to wait for more entries.
    4296                 :            :          */
    4297         [ #  # ]:          0 :         if (sret == -EBUSY)
    4298                 :            :                 goto waitagain;
    4299                 :            : 
    4300                 :            : out:
    4301                 :          0 :         mutex_unlock(&iter->mutex);
    4302                 :            : 
    4303                 :          0 :         return sret;
    4304                 :            : }
    4305                 :            : 
    4306                 :          0 : static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
    4307                 :            :                                      struct pipe_buffer *buf)
    4308                 :            : {
    4309                 :          0 :         __free_page(buf->page);
    4310                 :          0 : }
    4311                 :            : 
    4312                 :          0 : static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
    4313                 :            :                                      unsigned int idx)
    4314                 :            : {
    4315                 :          0 :         __free_page(spd->pages[idx]);
    4316                 :          0 : }
    4317                 :            : 
    4318                 :            : static const struct pipe_buf_operations tracing_pipe_buf_ops = {
    4319                 :            :         .can_merge              = 0,
    4320                 :            :         .map                    = generic_pipe_buf_map,
    4321                 :            :         .unmap                  = generic_pipe_buf_unmap,
    4322                 :            :         .confirm                = generic_pipe_buf_confirm,
    4323                 :            :         .release                = tracing_pipe_buf_release,
    4324                 :            :         .steal                  = generic_pipe_buf_steal,
    4325                 :            :         .get                    = generic_pipe_buf_get,
    4326                 :            : };
    4327                 :            : 
    4328                 :            : static size_t
    4329                 :          0 : tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
    4330                 :            : {
    4331                 :            :         size_t count;
    4332                 :            :         int ret;
    4333                 :            : 
    4334                 :            :         /* Seq buffer is page-sized, exactly what we need. */
    4335                 :            :         for (;;) {
    4336                 :          0 :                 count = iter->seq.len;
    4337                 :          0 :                 ret = print_trace_line(iter);
    4338                 :          0 :                 count = iter->seq.len - count;
    4339         [ #  # ]:          0 :                 if (rem < count) {
    4340                 :            :                         rem = 0;
    4341                 :          0 :                         iter->seq.len -= count;
    4342                 :          0 :                         break;
    4343                 :            :                 }
    4344         [ #  # ]:          0 :                 if (ret == TRACE_TYPE_PARTIAL_LINE) {
    4345                 :          0 :                         iter->seq.len -= count;
    4346                 :          0 :                         break;
    4347                 :            :                 }
    4348                 :            : 
    4349         [ #  # ]:          0 :                 if (ret != TRACE_TYPE_NO_CONSUME)
    4350                 :            :                         trace_consume(iter);
    4351                 :          0 :                 rem -= count;
    4352         [ #  # ]:          0 :                 if (!trace_find_next_entry_inc(iter))   {
    4353                 :            :                         rem = 0;
    4354                 :          0 :                         iter->ent = NULL;
    4355                 :          0 :                         break;
    4356                 :            :                 }
    4357                 :            :         }
    4358                 :            : 
    4359                 :          0 :         return rem;
    4360                 :            : }
    4361                 :            : 
    4362                 :          0 : static ssize_t tracing_splice_read_pipe(struct file *filp,
    4363                 :            :                                         loff_t *ppos,
    4364                 :            :                                         struct pipe_inode_info *pipe,
    4365                 :            :                                         size_t len,
    4366                 :            :                                         unsigned int flags)
    4367                 :            : {
    4368                 :            :         struct page *pages_def[PIPE_DEF_BUFFERS];
    4369                 :            :         struct partial_page partial_def[PIPE_DEF_BUFFERS];
    4370                 :          0 :         struct trace_iterator *iter = filp->private_data;
    4371                 :          0 :         struct splice_pipe_desc spd = {
    4372                 :            :                 .pages          = pages_def,
    4373                 :            :                 .partial        = partial_def,
    4374                 :            :                 .nr_pages       = 0, /* This gets updated below. */
    4375                 :            :                 .nr_pages_max   = PIPE_DEF_BUFFERS,
    4376                 :            :                 .flags          = flags,
    4377                 :            :                 .ops            = &tracing_pipe_buf_ops,
    4378                 :            :                 .spd_release    = tracing_spd_release_pipe,
    4379                 :            :         };
    4380                 :          0 :         struct trace_array *tr = iter->tr;
    4381                 :            :         ssize_t ret;
    4382                 :            :         size_t rem;
    4383                 :            :         unsigned int i;
    4384                 :            : 
    4385         [ #  # ]:          0 :         if (splice_grow_spd(pipe, &spd))
    4386                 :            :                 return -ENOMEM;
    4387                 :            : 
    4388                 :            :         /* copy the tracer to avoid using a global lock all around */
    4389                 :          0 :         mutex_lock(&trace_types_lock);
    4390         [ #  # ]:          0 :         if (unlikely(iter->trace->name != tr->current_trace->name))
    4391                 :          0 :                 *iter->trace = *tr->current_trace;
    4392                 :          0 :         mutex_unlock(&trace_types_lock);
    4393                 :            : 
    4394                 :          0 :         mutex_lock(&iter->mutex);
    4395                 :            : 
    4396         [ #  # ]:          0 :         if (iter->trace->splice_read) {
    4397                 :          0 :                 ret = iter->trace->splice_read(iter, filp,
    4398                 :            :                                                ppos, pipe, len, flags);
    4399         [ #  # ]:          0 :                 if (ret)
    4400                 :            :                         goto out_err;
    4401                 :            :         }
    4402                 :            : 
    4403                 :          0 :         ret = tracing_wait_pipe(filp);
    4404         [ #  # ]:          0 :         if (ret <= 0)
    4405                 :            :                 goto out_err;
    4406                 :            : 
    4407 [ #  # ][ #  # ]:          0 :         if (!iter->ent && !trace_find_next_entry_inc(iter)) {
    4408                 :            :                 ret = -EFAULT;
    4409                 :            :                 goto out_err;
    4410                 :            :         }
    4411                 :            : 
    4412                 :          0 :         trace_event_read_lock();
    4413                 :          0 :         trace_access_lock(iter->cpu_file);
    4414                 :            : 
    4415                 :            :         /* Fill as many pages as possible. */
    4416 [ #  # ][ #  # ]:          0 :         for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
    4417                 :          0 :                 spd.pages[i] = alloc_page(GFP_KERNEL);
    4418         [ #  # ]:          0 :                 if (!spd.pages[i])
    4419                 :            :                         break;
    4420                 :            : 
    4421                 :          0 :                 rem = tracing_fill_pipe_page(rem, iter);
    4422                 :            : 
    4423                 :            :                 /* Copy the data into the page, so we can start over. */
    4424                 :          0 :                 ret = trace_seq_to_buffer(&iter->seq,
    4425                 :          0 :                                           page_address(spd.pages[i]),
    4426                 :            :                                           iter->seq.len);
    4427         [ #  # ]:          0 :                 if (ret < 0) {
    4428                 :          0 :                         __free_page(spd.pages[i]);
    4429                 :          0 :                         break;
    4430                 :            :                 }
    4431                 :          0 :                 spd.partial[i].offset = 0;
    4432                 :          0 :                 spd.partial[i].len = iter->seq.len;
    4433                 :            : 
    4434                 :            :                 trace_seq_init(&iter->seq);
    4435                 :            :         }
    4436                 :            : 
    4437                 :          0 :         trace_access_unlock(iter->cpu_file);
    4438                 :          0 :         trace_event_read_unlock();
    4439                 :          0 :         mutex_unlock(&iter->mutex);
    4440                 :            : 
    4441                 :          0 :         spd.nr_pages = i;
    4442                 :            : 
    4443                 :          0 :         ret = splice_to_pipe(pipe, &spd);
    4444                 :            : out:
    4445                 :          0 :         splice_shrink_spd(&spd);
    4446                 :          0 :         return ret;
    4447                 :            : 
    4448                 :            : out_err:
    4449                 :          0 :         mutex_unlock(&iter->mutex);
    4450                 :          0 :         goto out;
    4451                 :            : }
    4452                 :            : 
    4453                 :            : static ssize_t
    4454                 :          0 : tracing_entries_read(struct file *filp, char __user *ubuf,
    4455                 :            :                      size_t cnt, loff_t *ppos)
    4456                 :            : {
    4457                 :          0 :         struct inode *inode = file_inode(filp);
    4458                 :          0 :         struct trace_array *tr = inode->i_private;
    4459                 :            :         int cpu = tracing_get_cpu(inode);
    4460                 :            :         char buf[64];
    4461                 :            :         int r = 0;
    4462                 :            :         ssize_t ret;
    4463                 :            : 
    4464                 :          0 :         mutex_lock(&trace_types_lock);
    4465                 :            : 
    4466         [ #  # ]:          0 :         if (cpu == RING_BUFFER_ALL_CPUS) {
    4467                 :            :                 int cpu, buf_size_same;
    4468                 :            :                 unsigned long size;
    4469                 :            : 
    4470                 :            :                 size = 0;
    4471                 :            :                 buf_size_same = 1;
    4472                 :            :                 /* check if all cpu sizes are same */
    4473         [ #  # ]:          0 :                 for_each_tracing_cpu(cpu) {
    4474                 :            :                         /* fill in the size from first enabled cpu */
    4475         [ #  # ]:          0 :                         if (size == 0)
    4476                 :          0 :                                 size = per_cpu_ptr(tr->trace_buffer.data, cpu)->entries;
    4477         [ #  # ]:          0 :                         if (size != per_cpu_ptr(tr->trace_buffer.data, cpu)->entries) {
    4478                 :            :                                 buf_size_same = 0;
    4479                 :            :                                 break;
    4480                 :            :                         }
    4481                 :            :                 }
    4482                 :            : 
    4483         [ #  # ]:          0 :                 if (buf_size_same) {
    4484         [ #  # ]:          0 :                         if (!ring_buffer_expanded)
    4485                 :          0 :                                 r = sprintf(buf, "%lu (expanded: %lu)\n",
    4486                 :            :                                             size >> 10,
    4487                 :            :                                             trace_buf_size >> 10);
    4488                 :            :                         else
    4489                 :          0 :                                 r = sprintf(buf, "%lu\n", size >> 10);
    4490                 :            :                 } else
    4491                 :          0 :                         r = sprintf(buf, "X\n");
    4492                 :            :         } else
    4493                 :          0 :                 r = sprintf(buf, "%lu\n", per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10);
    4494                 :            : 
    4495                 :          0 :         mutex_unlock(&trace_types_lock);
    4496                 :            : 
    4497                 :          0 :         ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
    4498                 :          0 :         return ret;
    4499                 :            : }
    4500                 :            : 
    4501                 :            : static ssize_t
    4502                 :          0 : tracing_entries_write(struct file *filp, const char __user *ubuf,
    4503                 :            :                       size_t cnt, loff_t *ppos)
    4504                 :            : {
    4505                 :          0 :         struct inode *inode = file_inode(filp);
    4506                 :          0 :         struct trace_array *tr = inode->i_private;
    4507                 :            :         unsigned long val;
    4508                 :            :         int ret;
    4509                 :            : 
    4510                 :          0 :         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
    4511         [ #  # ]:          0 :         if (ret)
    4512                 :            :                 return ret;
    4513                 :            : 
    4514                 :            :         /* must have at least 1 entry */
    4515         [ #  # ]:          0 :         if (!val)
    4516                 :            :                 return -EINVAL;
    4517                 :            : 
    4518                 :            :         /* value is in KB */
    4519                 :          0 :         val <<= 10;
    4520                 :          0 :         ret = tracing_resize_ring_buffer(tr, val, tracing_get_cpu(inode));
    4521         [ #  # ]:          0 :         if (ret < 0)
    4522                 :            :                 return ret;
    4523                 :            : 
    4524                 :          0 :         *ppos += cnt;
    4525                 :            : 
    4526                 :          0 :         return cnt;
    4527                 :            : }
    4528                 :            : 
    4529                 :            : static ssize_t
    4530                 :          0 : tracing_total_entries_read(struct file *filp, char __user *ubuf,
    4531                 :            :                                 size_t cnt, loff_t *ppos)
    4532                 :            : {
    4533                 :          0 :         struct trace_array *tr = filp->private_data;
    4534                 :            :         char buf[64];
    4535                 :            :         int r, cpu;
    4536                 :            :         unsigned long size = 0, expanded_size = 0;
    4537                 :            : 
    4538                 :          0 :         mutex_lock(&trace_types_lock);
    4539         [ #  # ]:          0 :         for_each_tracing_cpu(cpu) {
    4540                 :          0 :                 size += per_cpu_ptr(tr->trace_buffer.data, cpu)->entries >> 10;
    4541         [ #  # ]:          0 :                 if (!ring_buffer_expanded)
    4542                 :          0 :                         expanded_size += trace_buf_size >> 10;
    4543                 :            :         }
    4544         [ #  # ]:          0 :         if (ring_buffer_expanded)
    4545                 :          0 :                 r = sprintf(buf, "%lu\n", size);
    4546                 :            :         else
    4547                 :          0 :                 r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
    4548                 :          0 :         mutex_unlock(&trace_types_lock);
    4549                 :            : 
    4550                 :          0 :         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
    4551                 :            : }
    4552                 :            : 
    4553                 :            : static ssize_t
    4554                 :          0 : tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
    4555                 :            :                           size_t cnt, loff_t *ppos)
    4556                 :            : {
    4557                 :            :         /*
    4558                 :            :          * There is no need to read what the user has written, this function
    4559                 :            :          * is just to make sure that there is no error when "echo" is used
    4560                 :            :          */
    4561                 :            : 
    4562                 :          0 :         *ppos += cnt;
    4563                 :            : 
    4564                 :          0 :         return cnt;
    4565                 :            : }
    4566                 :            : 
    4567                 :            : static int
    4568                 :          0 : tracing_free_buffer_release(struct inode *inode, struct file *filp)
    4569                 :            : {
    4570                 :          0 :         struct trace_array *tr = inode->i_private;
    4571                 :            : 
    4572                 :            :         /* disable tracing ? */
    4573         [ #  # ]:          0 :         if (trace_flags & TRACE_ITER_STOP_ON_FREE)
    4574                 :            :                 tracer_tracing_off(tr);
    4575                 :            :         /* resize the ring buffer to 0 */
    4576                 :          0 :         tracing_resize_ring_buffer(tr, 0, RING_BUFFER_ALL_CPUS);
    4577                 :            : 
    4578                 :          0 :         trace_array_put(tr);
    4579                 :            : 
    4580                 :          0 :         return 0;
    4581                 :            : }
    4582                 :            : 
    4583                 :            : static ssize_t
    4584                 :          0 : tracing_mark_write(struct file *filp, const char __user *ubuf,
    4585                 :            :                                         size_t cnt, loff_t *fpos)
    4586                 :            : {
    4587                 :          0 :         unsigned long addr = (unsigned long)ubuf;
    4588                 :          0 :         struct trace_array *tr = filp->private_data;
    4589                 :            :         struct ring_buffer_event *event;
    4590                 :            :         struct ring_buffer *buffer;
    4591                 :            :         struct print_entry *entry;
    4592                 :            :         unsigned long irq_flags;
    4593                 :            :         struct page *pages[2];
    4594                 :            :         void *map_page[2];
    4595                 :            :         int nr_pages = 1;
    4596                 :            :         ssize_t written;
    4597                 :            :         int offset;
    4598                 :            :         int size;
    4599                 :            :         int len;
    4600                 :            :         int ret;
    4601                 :            :         int i;
    4602                 :            : 
    4603         [ #  # ]:          0 :         if (tracing_disabled)
    4604                 :            :                 return -EINVAL;
    4605                 :            : 
    4606         [ #  # ]:          0 :         if (!(trace_flags & TRACE_ITER_MARKERS))
    4607                 :            :                 return -EINVAL;
    4608                 :            : 
    4609         [ #  # ]:          0 :         if (cnt > TRACE_BUF_SIZE)
    4610                 :            :                 cnt = TRACE_BUF_SIZE;
    4611                 :            : 
    4612                 :            :         /*
    4613                 :            :          * Userspace is injecting traces into the kernel trace buffer.
    4614                 :            :          * We want to be as non intrusive as possible.
    4615                 :            :          * To do so, we do not want to allocate any special buffers
    4616                 :            :          * or take any locks, but instead write the userspace data
    4617                 :            :          * straight into the ring buffer.
    4618                 :            :          *
    4619                 :            :          * First we need to pin the userspace buffer into memory,
    4620                 :            :          * which, most likely it is, because it just referenced it.
    4621                 :            :          * But there's no guarantee that it is. By using get_user_pages_fast()
    4622                 :            :          * and kmap_atomic/kunmap_atomic() we can get access to the
    4623                 :            :          * pages directly. We then write the data directly into the
    4624                 :            :          * ring buffer.
    4625                 :            :          */
    4626                 :            :         BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
    4627                 :            : 
    4628                 :            :         /* check if we cross pages */
    4629         [ #  # ]:          0 :         if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
    4630                 :            :                 nr_pages = 2;
    4631                 :            : 
    4632                 :          0 :         offset = addr & (PAGE_SIZE - 1);
    4633                 :          0 :         addr &= PAGE_MASK;
    4634                 :            : 
    4635                 :          0 :         ret = get_user_pages_fast(addr, nr_pages, 0, pages);
    4636         [ #  # ]:          0 :         if (ret < nr_pages) {
    4637         [ #  # ]:          0 :                 while (--ret >= 0)
    4638                 :          0 :                         put_page(pages[ret]);
    4639                 :            :                 written = -EFAULT;
    4640                 :            :                 goto out;
    4641                 :            :         }
    4642                 :            : 
    4643         [ #  # ]:          0 :         for (i = 0; i < nr_pages; i++)
    4644                 :          0 :                 map_page[i] = kmap_atomic(pages[i]);
    4645                 :            : 
    4646                 :            :         local_save_flags(irq_flags);
    4647                 :          0 :         size = sizeof(*entry) + cnt + 2; /* possible \n added */
    4648                 :          0 :         buffer = tr->trace_buffer.buffer;
    4649                 :          0 :         event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
    4650                 :            :                                           irq_flags, preempt_count());
    4651         [ #  # ]:          0 :         if (!event) {
    4652                 :            :                 /* Ring buffer disabled, return as if not open for write */
    4653                 :            :                 written = -EBADF;
    4654                 :            :                 goto out_unlock;
    4655                 :            :         }
    4656                 :            : 
    4657                 :          0 :         entry = ring_buffer_event_data(event);
    4658                 :          0 :         entry->ip = _THIS_IP_;
    4659                 :            : 
    4660         [ #  # ]:          0 :         if (nr_pages == 2) {
    4661                 :          0 :                 len = PAGE_SIZE - offset;
    4662                 :          0 :                 memcpy(&entry->buf, map_page[0] + offset, len);
    4663                 :          0 :                 memcpy(&entry->buf[len], map_page[1], cnt - len);
    4664                 :            :         } else
    4665                 :          0 :                 memcpy(&entry->buf, map_page[0] + offset, cnt);
    4666                 :            : 
    4667         [ #  # ]:          0 :         if (entry->buf[cnt - 1] != '\n') {
    4668                 :          0 :                 entry->buf[cnt] = '\n';
    4669                 :          0 :                 entry->buf[cnt + 1] = '\0';
    4670                 :            :         } else
    4671                 :          0 :                 entry->buf[cnt] = '\0';
    4672                 :            : 
    4673                 :            :         __buffer_unlock_commit(buffer, event);
    4674                 :            : 
    4675                 :          0 :         written = cnt;
    4676                 :            : 
    4677                 :          0 :         *fpos += written;
    4678                 :            : 
    4679                 :            :  out_unlock:
    4680         [ #  # ]:          0 :         for (i = 0; i < nr_pages; i++){
    4681                 :          0 :                 kunmap_atomic(map_page[i]);
    4682                 :          0 :                 put_page(pages[i]);
    4683                 :            :         }
    4684                 :            :  out:
    4685                 :          0 :         return written;
    4686                 :            : }
    4687                 :            : 
    4688                 :          0 : static int tracing_clock_show(struct seq_file *m, void *v)
    4689                 :            : {
    4690                 :          0 :         struct trace_array *tr = m->private;
    4691                 :            :         int i;
    4692                 :            : 
    4693         [ #  # ]:          0 :         for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
    4694 [ #  # ][ #  # ]:          0 :                 seq_printf(m,
                 [ #  # ]
    4695                 :            :                         "%s%s%s%s", i ? " " : "",
    4696                 :          0 :                         i == tr->clock_id ? "[" : "", trace_clocks[i].name,
    4697                 :            :                         i == tr->clock_id ? "]" : "");
    4698                 :          0 :         seq_putc(m, '\n');
    4699                 :            : 
    4700                 :          0 :         return 0;
    4701                 :            : }
    4702                 :            : 
    4703                 :          0 : static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
    4704                 :            :                                    size_t cnt, loff_t *fpos)
    4705                 :            : {
    4706                 :          0 :         struct seq_file *m = filp->private_data;
    4707                 :          0 :         struct trace_array *tr = m->private;
    4708                 :            :         char buf[64];
    4709                 :            :         const char *clockstr;
    4710                 :            :         int i;
    4711                 :            : 
    4712         [ #  # ]:          0 :         if (cnt >= sizeof(buf))
    4713                 :            :                 return -EINVAL;
    4714                 :            : 
    4715         [ #  # ]:          0 :         if (copy_from_user(&buf, ubuf, cnt))
    4716                 :            :                 return -EFAULT;
    4717                 :            : 
    4718                 :          0 :         buf[cnt] = 0;
    4719                 :            : 
    4720                 :            :         clockstr = strstrip(buf);
    4721                 :            : 
    4722         [ #  # ]:          0 :         for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
    4723         [ #  # ]:          0 :                 if (strcmp(trace_clocks[i].name, clockstr) == 0)
    4724                 :            :                         break;
    4725                 :            :         }
    4726         [ #  # ]:          0 :         if (i == ARRAY_SIZE(trace_clocks))
    4727                 :            :                 return -EINVAL;
    4728                 :            : 
    4729                 :          0 :         mutex_lock(&trace_types_lock);
    4730                 :            : 
    4731                 :          0 :         tr->clock_id = i;
    4732                 :            : 
    4733                 :          0 :         ring_buffer_set_clock(tr->trace_buffer.buffer, trace_clocks[i].func);
    4734                 :            : 
    4735                 :            :         /*
    4736                 :            :          * New clock may not be consistent with the previous clock.
    4737                 :            :          * Reset the buffer so that it doesn't have incomparable timestamps.
    4738                 :            :          */
    4739                 :          0 :         tracing_reset_online_cpus(&tr->trace_buffer);
    4740                 :            : 
    4741                 :            : #ifdef CONFIG_TRACER_MAX_TRACE
    4742                 :            :         if (tr->flags & TRACE_ARRAY_FL_GLOBAL && tr->max_buffer.buffer)
    4743                 :            :                 ring_buffer_set_clock(tr->max_buffer.buffer, trace_clocks[i].func);
    4744                 :            :         tracing_reset_online_cpus(&tr->max_buffer);
    4745                 :            : #endif
    4746                 :            : 
    4747                 :          0 :         mutex_unlock(&trace_types_lock);
    4748                 :            : 
    4749                 :          0 :         *fpos += cnt;
    4750                 :            : 
    4751                 :          0 :         return cnt;
    4752                 :            : }
    4753                 :            : 
    4754                 :          0 : static int tracing_clock_open(struct inode *inode, struct file *file)
    4755                 :            : {
    4756                 :          0 :         struct trace_array *tr = inode->i_private;
    4757                 :            :         int ret;
    4758                 :            : 
    4759         [ #  # ]:          0 :         if (tracing_disabled)
    4760                 :            :                 return -ENODEV;
    4761                 :            : 
    4762         [ #  # ]:          0 :         if (trace_array_get(tr))
    4763                 :            :                 return -ENODEV;
    4764                 :            : 
    4765                 :          0 :         ret = single_open(file, tracing_clock_show, inode->i_private);
    4766         [ #  # ]:          0 :         if (ret < 0)
    4767                 :          0 :                 trace_array_put(tr);
    4768                 :            : 
    4769                 :          0 :         return ret;
    4770                 :            : }
    4771                 :            : 
    4772                 :            : struct ftrace_buffer_info {
    4773                 :            :         struct trace_iterator   iter;
    4774                 :            :         void                    *spare;
    4775                 :            :         unsigned int            read;
    4776                 :            : };
    4777                 :            : 
    4778                 :            : #ifdef CONFIG_TRACER_SNAPSHOT
    4779                 :            : static int tracing_snapshot_open(struct inode *inode, struct file *file)
    4780                 :            : {
    4781                 :            :         struct trace_array *tr = inode->i_private;
    4782                 :            :         struct trace_iterator *iter;
    4783                 :            :         struct seq_file *m;
    4784                 :            :         int ret = 0;
    4785                 :            : 
    4786                 :            :         if (trace_array_get(tr) < 0)
    4787                 :            :                 return -ENODEV;
    4788                 :            : 
    4789                 :            :         if (file->f_mode & FMODE_READ) {
    4790                 :            :                 iter = __tracing_open(inode, file, true);
    4791                 :            :                 if (IS_ERR(iter))
    4792                 :            :                         ret = PTR_ERR(iter);
    4793                 :            :         } else {
    4794                 :            :                 /* Writes still need the seq_file to hold the private data */
    4795                 :            :                 ret = -ENOMEM;
    4796                 :            :                 m = kzalloc(sizeof(*m), GFP_KERNEL);
    4797                 :            :                 if (!m)
    4798                 :            :                         goto out;
    4799                 :            :                 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
    4800                 :            :                 if (!iter) {
    4801                 :            :                         kfree(m);
    4802                 :            :                         goto out;
    4803                 :            :                 }
    4804                 :            :                 ret = 0;
    4805                 :            : 
    4806                 :            :                 iter->tr = tr;
    4807                 :            :                 iter->trace_buffer = &tr->max_buffer;
    4808                 :            :                 iter->cpu_file = tracing_get_cpu(inode);
    4809                 :            :                 m->private = iter;
    4810                 :            :                 file->private_data = m;
    4811                 :            :         }
    4812                 :            : out:
    4813                 :            :         if (ret < 0)
    4814                 :            :                 trace_array_put(tr);
    4815                 :            : 
    4816                 :            :         return ret;
    4817                 :            : }
    4818                 :            : 
    4819                 :            : static ssize_t
    4820                 :            : tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
    4821                 :            :                        loff_t *ppos)
    4822                 :            : {
    4823                 :            :         struct seq_file *m = filp->private_data;
    4824                 :            :         struct trace_iterator *iter = m->private;
    4825                 :            :         struct trace_array *tr = iter->tr;
    4826                 :            :         unsigned long val;
    4827                 :            :         int ret;
    4828                 :            : 
    4829                 :            :         ret = tracing_update_buffers();
    4830                 :            :         if (ret < 0)
    4831                 :            :                 return ret;
    4832                 :            : 
    4833                 :            :         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
    4834                 :            :         if (ret)
    4835                 :            :                 return ret;
    4836                 :            : 
    4837                 :            :         mutex_lock(&trace_types_lock);
    4838                 :            : 
    4839                 :            :         if (tr->current_trace->use_max_tr) {
    4840                 :            :                 ret = -EBUSY;
    4841                 :            :                 goto out;
    4842                 :            :         }
    4843                 :            : 
    4844                 :            :         switch (val) {
    4845                 :            :         case 0:
    4846                 :            :                 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
    4847                 :            :                         ret = -EINVAL;
    4848                 :            :                         break;
    4849                 :            :                 }
    4850                 :            :                 if (tr->allocated_snapshot)
    4851                 :            :                         free_snapshot(tr);
    4852                 :            :                 break;
    4853                 :            :         case 1:
    4854                 :            : /* Only allow per-cpu swap if the ring buffer supports it */
    4855                 :            : #ifndef CONFIG_RING_BUFFER_ALLOW_SWAP
    4856                 :            :                 if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
    4857                 :            :                         ret = -EINVAL;
    4858                 :            :                         break;
    4859                 :            :                 }
    4860                 :            : #endif
    4861                 :            :                 if (!tr->allocated_snapshot) {
    4862                 :            :                         ret = alloc_snapshot(tr);
    4863                 :            :                         if (ret < 0)
    4864                 :            :                                 break;
    4865                 :            :                 }
    4866                 :            :                 local_irq_disable();
    4867                 :            :                 /* Now, we're going to swap */
    4868                 :            :                 if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
    4869                 :            :                         update_max_tr(tr, current, smp_processor_id());
    4870                 :            :                 else
    4871                 :            :                         update_max_tr_single(tr, current, iter->cpu_file);
    4872                 :            :                 local_irq_enable();
    4873                 :            :                 break;
    4874                 :            :         default:
    4875                 :            :                 if (tr->allocated_snapshot) {
    4876                 :            :                         if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
    4877                 :            :                                 tracing_reset_online_cpus(&tr->max_buffer);
    4878                 :            :                         else
    4879                 :            :                                 tracing_reset(&tr->max_buffer, iter->cpu_file);
    4880                 :            :                 }
    4881                 :            :                 break;
    4882                 :            :         }
    4883                 :            : 
    4884                 :            :         if (ret >= 0) {
    4885                 :            :                 *ppos += cnt;
    4886                 :            :                 ret = cnt;
    4887                 :            :         }
    4888                 :            : out:
    4889                 :            :         mutex_unlock(&trace_types_lock);
    4890                 :            :         return ret;
    4891                 :            : }
    4892                 :            : 
    4893                 :            : static int tracing_snapshot_release(struct inode *inode, struct file *file)
    4894                 :            : {
    4895                 :            :         struct seq_file *m = file->private_data;
    4896                 :            :         int ret;
    4897                 :            : 
    4898                 :            :         ret = tracing_release(inode, file);
    4899                 :            : 
    4900                 :            :         if (file->f_mode & FMODE_READ)
    4901                 :            :                 return ret;
    4902                 :            : 
    4903                 :            :         /* If write only, the seq_file is just a stub */
    4904                 :            :         if (m)
    4905                 :            :                 kfree(m->private);
    4906                 :            :         kfree(m);
    4907                 :            : 
    4908                 :            :         return 0;
    4909                 :            : }
    4910                 :            : 
    4911                 :            : static int tracing_buffers_open(struct inode *inode, struct file *filp);
    4912                 :            : static ssize_t tracing_buffers_read(struct file *filp, char __user *ubuf,
    4913                 :            :                                     size_t count, loff_t *ppos);
    4914                 :            : static int tracing_buffers_release(struct inode *inode, struct file *file);
    4915                 :            : static ssize_t tracing_buffers_splice_read(struct file *file, loff_t *ppos,
    4916                 :            :                    struct pipe_inode_info *pipe, size_t len, unsigned int flags);
    4917                 :            : 
    4918                 :            : static int snapshot_raw_open(struct inode *inode, struct file *filp)
    4919                 :            : {
    4920                 :            :         struct ftrace_buffer_info *info;
    4921                 :            :         int ret;
    4922                 :            : 
    4923                 :            :         ret = tracing_buffers_open(inode, filp);
    4924                 :            :         if (ret < 0)
    4925                 :            :                 return ret;
    4926                 :            : 
    4927                 :            :         info = filp->private_data;
    4928                 :            : 
    4929                 :            :         if (info->iter.trace->use_max_tr) {
    4930                 :            :                 tracing_buffers_release(inode, filp);
    4931                 :            :                 return -EBUSY;
    4932                 :            :         }
    4933                 :            : 
    4934                 :            :         info->iter.snapshot = true;
    4935                 :            :         info->iter.trace_buffer = &info->iter.tr->max_buffer;
    4936                 :            : 
    4937                 :            :         return ret;
    4938                 :            : }
    4939                 :            : 
    4940                 :            : #endif /* CONFIG_TRACER_SNAPSHOT */
    4941                 :            : 
    4942                 :            : 
    4943                 :            : static const struct file_operations tracing_max_lat_fops = {
    4944                 :            :         .open           = tracing_open_generic,
    4945                 :            :         .read           = tracing_max_lat_read,
    4946                 :            :         .write          = tracing_max_lat_write,
    4947                 :            :         .llseek         = generic_file_llseek,
    4948                 :            : };
    4949                 :            : 
    4950                 :            : static const struct file_operations set_tracer_fops = {
    4951                 :            :         .open           = tracing_open_generic,
    4952                 :            :         .read           = tracing_set_trace_read,
    4953                 :            :         .write          = tracing_set_trace_write,
    4954                 :            :         .llseek         = generic_file_llseek,
    4955                 :            : };
    4956                 :            : 
    4957                 :            : static const struct file_operations tracing_pipe_fops = {
    4958                 :            :         .open           = tracing_open_pipe,
    4959                 :            :         .poll           = tracing_poll_pipe,
    4960                 :            :         .read           = tracing_read_pipe,
    4961                 :            :         .splice_read    = tracing_splice_read_pipe,
    4962                 :            :         .release        = tracing_release_pipe,
    4963                 :            :         .llseek         = no_llseek,
    4964                 :            : };
    4965                 :            : 
    4966                 :            : static const struct file_operations tracing_entries_fops = {
    4967                 :            :         .open           = tracing_open_generic_tr,
    4968                 :            :         .read           = tracing_entries_read,
    4969                 :            :         .write          = tracing_entries_write,
    4970                 :            :         .llseek         = generic_file_llseek,
    4971                 :            :         .release        = tracing_release_generic_tr,
    4972                 :            : };
    4973                 :            : 
    4974                 :            : static const struct file_operations tracing_total_entries_fops = {
    4975                 :            :         .open           = tracing_open_generic_tr,
    4976                 :            :         .read           = tracing_total_entries_read,
    4977                 :            :         .llseek         = generic_file_llseek,
    4978                 :            :         .release        = tracing_release_generic_tr,
    4979                 :            : };
    4980                 :            : 
    4981                 :            : static const struct file_operations tracing_free_buffer_fops = {
    4982                 :            :         .open           = tracing_open_generic_tr,
    4983                 :            :         .write          = tracing_free_buffer_write,
    4984                 :            :         .release        = tracing_free_buffer_release,
    4985                 :            : };
    4986                 :            : 
    4987                 :            : static const struct file_operations tracing_mark_fops = {
    4988                 :            :         .open           = tracing_open_generic_tr,
    4989                 :            :         .write          = tracing_mark_write,
    4990                 :            :         .llseek         = generic_file_llseek,
    4991                 :            :         .release        = tracing_release_generic_tr,
    4992                 :            : };
    4993                 :            : 
    4994                 :            : static const struct file_operations trace_clock_fops = {
    4995                 :            :         .open           = tracing_clock_open,
    4996                 :            :         .read           = seq_read,
    4997                 :            :         .llseek         = seq_lseek,
    4998                 :            :         .release        = tracing_single_release_tr,
    4999                 :            :         .write          = tracing_clock_write,
    5000                 :            : };
    5001                 :            : 
    5002                 :            : #ifdef CONFIG_TRACER_SNAPSHOT
    5003                 :            : static const struct file_operations snapshot_fops = {
    5004                 :            :         .open           = tracing_snapshot_open,
    5005                 :            :         .read           = seq_read,
    5006                 :            :         .write          = tracing_snapshot_write,
    5007                 :            :         .llseek         = tracing_seek,
    5008                 :            :         .release        = tracing_snapshot_release,
    5009                 :            : };
    5010                 :            : 
    5011                 :            : static const struct file_operations snapshot_raw_fops = {
    5012                 :            :         .open           = snapshot_raw_open,
    5013                 :            :         .read           = tracing_buffers_read,
    5014                 :            :         .release        = tracing_buffers_release,
    5015                 :            :         .splice_read    = tracing_buffers_splice_read,
    5016                 :            :         .llseek         = no_llseek,
    5017                 :            : };
    5018                 :            : 
    5019                 :            : #endif /* CONFIG_TRACER_SNAPSHOT */
    5020                 :            : 
    5021                 :          0 : static int tracing_buffers_open(struct inode *inode, struct file *filp)
    5022                 :            : {
    5023                 :          0 :         struct trace_array *tr = inode->i_private;
    5024                 :            :         struct ftrace_buffer_info *info;
    5025                 :            :         int ret;
    5026                 :            : 
    5027         [ #  # ]:          0 :         if (tracing_disabled)
    5028                 :            :                 return -ENODEV;
    5029                 :            : 
    5030         [ #  # ]:          0 :         if (trace_array_get(tr) < 0)
    5031                 :            :                 return -ENODEV;
    5032                 :            : 
    5033                 :            :         info = kzalloc(sizeof(*info), GFP_KERNEL);
    5034         [ #  # ]:          0 :         if (!info) {
    5035                 :          0 :                 trace_array_put(tr);
    5036                 :          0 :                 return -ENOMEM;
    5037                 :            :         }
    5038                 :            : 
    5039                 :          0 :         mutex_lock(&trace_types_lock);
    5040                 :            : 
    5041                 :          0 :         info->iter.tr                = tr;
    5042                 :          0 :         info->iter.cpu_file  = tracing_get_cpu(inode);
    5043                 :          0 :         info->iter.trace     = tr->current_trace;
    5044                 :          0 :         info->iter.trace_buffer = &tr->trace_buffer;
    5045                 :          0 :         info->spare          = NULL;
    5046                 :            :         /* Force reading ring buffer for first read */
    5047                 :          0 :         info->read           = (unsigned int)-1;
    5048                 :            : 
    5049                 :          0 :         filp->private_data = info;
    5050                 :            : 
    5051                 :          0 :         mutex_unlock(&trace_types_lock);
    5052                 :            : 
    5053                 :          0 :         ret = nonseekable_open(inode, filp);
    5054         [ #  # ]:          0 :         if (ret < 0)
    5055                 :          0 :                 trace_array_put(tr);
    5056                 :            : 
    5057                 :          0 :         return ret;
    5058                 :            : }
    5059                 :            : 
    5060                 :            : static unsigned int
    5061                 :          0 : tracing_buffers_poll(struct file *filp, poll_table *poll_table)
    5062                 :            : {
    5063                 :          0 :         struct ftrace_buffer_info *info = filp->private_data;
    5064                 :          0 :         struct trace_iterator *iter = &info->iter;
    5065                 :            : 
    5066                 :          0 :         return trace_poll(iter, filp, poll_table);
    5067                 :            : }
    5068                 :            : 
    5069                 :            : static ssize_t
    5070                 :          0 : tracing_buffers_read(struct file *filp, char __user *ubuf,
    5071                 :            :                      size_t count, loff_t *ppos)
    5072                 :            : {
    5073                 :          0 :         struct ftrace_buffer_info *info = filp->private_data;
    5074                 :          0 :         struct trace_iterator *iter = &info->iter;
    5075                 :            :         ssize_t ret;
    5076                 :            :         ssize_t size;
    5077                 :            : 
    5078         [ #  # ]:          0 :         if (!count)
    5079                 :            :                 return 0;
    5080                 :            : 
    5081                 :          0 :         mutex_lock(&trace_types_lock);
    5082                 :            : 
    5083                 :            : #ifdef CONFIG_TRACER_MAX_TRACE
    5084                 :            :         if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
    5085                 :            :                 size = -EBUSY;
    5086                 :            :                 goto out_unlock;
    5087                 :            :         }
    5088                 :            : #endif
    5089                 :            : 
    5090         [ #  # ]:          0 :         if (!info->spare)
    5091                 :          0 :                 info->spare = ring_buffer_alloc_read_page(iter->trace_buffer->buffer,
    5092                 :            :                                                           iter->cpu_file);
    5093                 :            :         size = -ENOMEM;
    5094         [ #  # ]:          0 :         if (!info->spare)
    5095                 :            :                 goto out_unlock;
    5096                 :            : 
    5097                 :            :         /* Do we have previous read data to read? */
    5098         [ #  # ]:          0 :         if (info->read < PAGE_SIZE)
    5099                 :            :                 goto read;
    5100                 :            : 
    5101                 :            :  again:
    5102                 :          0 :         trace_access_lock(iter->cpu_file);
    5103                 :          0 :         ret = ring_buffer_read_page(iter->trace_buffer->buffer,
    5104                 :            :                                     &info->spare,
    5105                 :            :                                     count,
    5106                 :            :                                     iter->cpu_file, 0);
    5107                 :          0 :         trace_access_unlock(iter->cpu_file);
    5108                 :            : 
    5109         [ #  # ]:          0 :         if (ret < 0) {
    5110         [ #  # ]:          0 :                 if (trace_empty(iter)) {
    5111         [ #  # ]:          0 :                         if ((filp->f_flags & O_NONBLOCK)) {
    5112                 :            :                                 size = -EAGAIN;
    5113                 :            :                                 goto out_unlock;
    5114                 :            :                         }
    5115                 :          0 :                         mutex_unlock(&trace_types_lock);
    5116                 :          0 :                         iter->trace->wait_pipe(iter);
    5117                 :          0 :                         mutex_lock(&trace_types_lock);
    5118         [ #  # ]:          0 :                         if (signal_pending(current)) {
    5119                 :            :                                 size = -EINTR;
    5120                 :            :                                 goto out_unlock;
    5121                 :            :                         }
    5122                 :            :                         goto again;
    5123                 :            :                 }
    5124                 :            :                 size = 0;
    5125                 :            :                 goto out_unlock;
    5126                 :            :         }
    5127                 :            : 
    5128                 :          0 :         info->read = 0;
    5129                 :            :  read:
    5130                 :          0 :         size = PAGE_SIZE - info->read;
    5131         [ #  # ]:          0 :         if (size > count)
    5132                 :          0 :                 size = count;
    5133                 :            : 
    5134                 :          0 :         ret = copy_to_user(ubuf, info->spare + info->read, size);
    5135         [ #  # ]:          0 :         if (ret == size) {
    5136                 :            :                 size = -EFAULT;
    5137                 :            :                 goto out_unlock;
    5138                 :            :         }
    5139                 :          0 :         size -= ret;
    5140                 :            : 
    5141                 :          0 :         *ppos += size;
    5142                 :          0 :         info->read += size;
    5143                 :            : 
    5144                 :            :  out_unlock:
    5145                 :          0 :         mutex_unlock(&trace_types_lock);
    5146                 :            : 
    5147                 :          0 :         return size;
    5148                 :            : }
    5149                 :            : 
    5150                 :          0 : static int tracing_buffers_release(struct inode *inode, struct file *file)
    5151                 :            : {
    5152                 :          0 :         struct ftrace_buffer_info *info = file->private_data;
    5153                 :            :         struct trace_iterator *iter = &info->iter;
    5154                 :            : 
    5155                 :          0 :         mutex_lock(&trace_types_lock);
    5156                 :            : 
    5157                 :          0 :         __trace_array_put(iter->tr);
    5158                 :            : 
    5159         [ #  # ]:          0 :         if (info->spare)
    5160                 :          0 :                 ring_buffer_free_read_page(iter->trace_buffer->buffer, info->spare);
    5161                 :          0 :         kfree(info);
    5162                 :            : 
    5163                 :          0 :         mutex_unlock(&trace_types_lock);
    5164                 :            : 
    5165                 :          0 :         return 0;
    5166                 :            : }
    5167                 :            : 
    5168                 :            : struct buffer_ref {
    5169                 :            :         struct ring_buffer      *buffer;
    5170                 :            :         void                    *page;
    5171                 :            :         int                     ref;
    5172                 :            : };
    5173                 :            : 
    5174                 :          0 : static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
    5175                 :            :                                     struct pipe_buffer *buf)
    5176                 :            : {
    5177                 :          0 :         struct buffer_ref *ref = (struct buffer_ref *)buf->private;
    5178                 :            : 
    5179         [ #  # ]:          0 :         if (--ref->ref)
    5180                 :          0 :                 return;
    5181                 :            : 
    5182                 :          0 :         ring_buffer_free_read_page(ref->buffer, ref->page);
    5183                 :          0 :         kfree(ref);
    5184                 :          0 :         buf->private = 0;
    5185                 :            : }
    5186                 :            : 
    5187                 :          0 : static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
    5188                 :            :                                 struct pipe_buffer *buf)
    5189                 :            : {
    5190                 :          0 :         struct buffer_ref *ref = (struct buffer_ref *)buf->private;
    5191                 :            : 
    5192                 :          0 :         ref->ref++;
    5193                 :          0 : }
    5194                 :            : 
    5195                 :            : /* Pipe buffer operations for a buffer. */
    5196                 :            : static const struct pipe_buf_operations buffer_pipe_buf_ops = {
    5197                 :            :         .can_merge              = 0,
    5198                 :            :         .map                    = generic_pipe_buf_map,
    5199                 :            :         .unmap                  = generic_pipe_buf_unmap,
    5200                 :            :         .confirm                = generic_pipe_buf_confirm,
    5201                 :            :         .release                = buffer_pipe_buf_release,
    5202                 :            :         .steal                  = generic_pipe_buf_steal,
    5203                 :            :         .get                    = buffer_pipe_buf_get,
    5204                 :            : };
    5205                 :            : 
    5206                 :            : /*
    5207                 :            :  * Callback from splice_to_pipe(), if we need to release some pages
    5208                 :            :  * at the end of the spd in case we error'ed out in filling the pipe.
    5209                 :            :  */
    5210                 :          0 : static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
    5211                 :            : {
    5212                 :          0 :         struct buffer_ref *ref =
    5213                 :          0 :                 (struct buffer_ref *)spd->partial[i].private;
    5214                 :            : 
    5215         [ #  # ]:          0 :         if (--ref->ref)
    5216                 :          0 :                 return;
    5217                 :            : 
    5218                 :          0 :         ring_buffer_free_read_page(ref->buffer, ref->page);
    5219                 :          0 :         kfree(ref);
    5220                 :          0 :         spd->partial[i].private = 0;
    5221                 :            : }
    5222                 :            : 
    5223                 :            : static ssize_t
    5224                 :          0 : tracing_buffers_splice_read(struct file *file, loff_t *ppos,
    5225                 :            :                             struct pipe_inode_info *pipe, size_t len,
    5226                 :            :                             unsigned int flags)
    5227                 :            : {
    5228                 :          0 :         struct ftrace_buffer_info *info = file->private_data;
    5229                 :          0 :         struct trace_iterator *iter = &info->iter;
    5230                 :            :         struct partial_page partial_def[PIPE_DEF_BUFFERS];
    5231                 :            :         struct page *pages_def[PIPE_DEF_BUFFERS];
    5232                 :          0 :         struct splice_pipe_desc spd = {
    5233                 :            :                 .pages          = pages_def,
    5234                 :            :                 .partial        = partial_def,
    5235                 :            :                 .nr_pages_max   = PIPE_DEF_BUFFERS,
    5236                 :            :                 .flags          = flags,
    5237                 :            :                 .ops            = &buffer_pipe_buf_ops,
    5238                 :            :                 .spd_release    = buffer_spd_release,
    5239                 :            :         };
    5240                 :            :         struct buffer_ref *ref;
    5241                 :            :         int entries, size, i;
    5242                 :            :         ssize_t ret;
    5243                 :            : 
    5244                 :          0 :         mutex_lock(&trace_types_lock);
    5245                 :            : 
    5246                 :            : #ifdef CONFIG_TRACER_MAX_TRACE
    5247                 :            :         if (iter->snapshot && iter->tr->current_trace->use_max_tr) {
    5248                 :            :                 ret = -EBUSY;
    5249                 :            :                 goto out;
    5250                 :            :         }
    5251                 :            : #endif
    5252                 :            : 
    5253         [ #  # ]:          0 :         if (splice_grow_spd(pipe, &spd)) {
    5254                 :            :                 ret = -ENOMEM;
    5255                 :            :                 goto out;
    5256                 :            :         }
    5257                 :            : 
    5258         [ #  # ]:          0 :         if (*ppos & (PAGE_SIZE - 1)) {
    5259                 :            :                 ret = -EINVAL;
    5260                 :            :                 goto out;
    5261                 :            :         }
    5262                 :            : 
    5263         [ #  # ]:          0 :         if (len & (PAGE_SIZE - 1)) {
    5264         [ #  # ]:          0 :                 if (len < PAGE_SIZE) {
    5265                 :            :                         ret = -EINVAL;
    5266                 :            :                         goto out;
    5267                 :            :                 }
    5268                 :          0 :                 len &= PAGE_MASK;
    5269                 :            :         }
    5270                 :            : 
    5271                 :            :  again:
    5272                 :          0 :         trace_access_lock(iter->cpu_file);
    5273                 :          0 :         entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
    5274                 :            : 
    5275 [ #  # ][ #  # ]:          0 :         for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
    5276                 :            :                 struct page *page;
    5277                 :            :                 int r;
    5278                 :            : 
    5279                 :            :                 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
    5280         [ #  # ]:          0 :                 if (!ref)
    5281                 :            :                         break;
    5282                 :            : 
    5283                 :          0 :                 ref->ref = 1;
    5284                 :          0 :                 ref->buffer = iter->trace_buffer->buffer;
    5285                 :          0 :                 ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file);
    5286         [ #  # ]:          0 :                 if (!ref->page) {
    5287                 :          0 :                         kfree(ref);
    5288                 :          0 :                         break;
    5289                 :            :                 }
    5290                 :            : 
    5291                 :          0 :                 r = ring_buffer_read_page(ref->buffer, &ref->page,
    5292                 :            :                                           len, iter->cpu_file, 1);
    5293         [ #  # ]:          0 :                 if (r < 0) {
    5294                 :          0 :                         ring_buffer_free_read_page(ref->buffer, ref->page);
    5295                 :          0 :                         kfree(ref);
    5296                 :          0 :                         break;
    5297                 :            :                 }
    5298                 :            : 
    5299                 :            :                 /*
    5300                 :            :                  * zero out any left over data, this is going to
    5301                 :            :                  * user land.
    5302                 :            :                  */
    5303                 :          0 :                 size = ring_buffer_page_len(ref->page);
    5304         [ #  # ]:          0 :                 if (size < PAGE_SIZE)
    5305         [ #  # ]:          0 :                         memset(ref->page + size, 0, PAGE_SIZE - size);
    5306                 :            : 
    5307                 :          0 :                 page = virt_to_page(ref->page);
    5308                 :            : 
    5309                 :          0 :                 spd.pages[i] = page;
    5310                 :          0 :                 spd.partial[i].len = PAGE_SIZE;
    5311                 :          0 :                 spd.partial[i].offset = 0;
    5312                 :          0 :                 spd.partial[i].private = (unsigned long)ref;
    5313                 :          0 :                 spd.nr_pages++;
    5314                 :          0 :                 *ppos += PAGE_SIZE;
    5315                 :            : 
    5316                 :          0 :                 entries = ring_buffer_entries_cpu(iter->trace_buffer->buffer, iter->cpu_file);
    5317                 :            :         }
    5318                 :            : 
    5319                 :          0 :         trace_access_unlock(iter->cpu_file);
    5320                 :          0 :         spd.nr_pages = i;
    5321                 :            : 
    5322                 :            :         /* did we read anything? */
    5323         [ #  # ]:          0 :         if (!spd.nr_pages) {
    5324 [ #  # ][ #  # ]:          0 :                 if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK)) {
    5325                 :            :                         ret = -EAGAIN;
    5326                 :            :                         goto out;
    5327                 :            :                 }
    5328                 :          0 :                 mutex_unlock(&trace_types_lock);
    5329                 :          0 :                 iter->trace->wait_pipe(iter);
    5330                 :          0 :                 mutex_lock(&trace_types_lock);
    5331         [ #  # ]:          0 :                 if (signal_pending(current)) {
    5332                 :            :                         ret = -EINTR;
    5333                 :            :                         goto out;
    5334                 :            :                 }
    5335                 :            :                 goto again;
    5336                 :            :         }
    5337                 :            : 
    5338                 :          0 :         ret = splice_to_pipe(pipe, &spd);
    5339                 :          0 :         splice_shrink_spd(&spd);
    5340                 :            : out:
    5341                 :          0 :         mutex_unlock(&trace_types_lock);
    5342                 :            : 
    5343                 :          0 :         return ret;
    5344                 :            : }
    5345                 :            : 
    5346                 :            : static const struct file_operations tracing_buffers_fops = {
    5347                 :            :         .open           = tracing_buffers_open,
    5348                 :            :         .read           = tracing_buffers_read,
    5349                 :            :         .poll           = tracing_buffers_poll,
    5350                 :            :         .release        = tracing_buffers_release,
    5351                 :            :         .splice_read    = tracing_buffers_splice_read,
    5352                 :            :         .llseek         = no_llseek,
    5353                 :            : };
    5354                 :            : 
    5355                 :            : static ssize_t
    5356                 :          0 : tracing_stats_read(struct file *filp, char __user *ubuf,
    5357                 :            :                    size_t count, loff_t *ppos)
    5358                 :            : {
    5359                 :          0 :         struct inode *inode = file_inode(filp);
    5360                 :          0 :         struct trace_array *tr = inode->i_private;
    5361                 :            :         struct trace_buffer *trace_buf = &tr->trace_buffer;
    5362                 :            :         int cpu = tracing_get_cpu(inode);
    5363                 :            :         struct trace_seq *s;
    5364                 :            :         unsigned long cnt;
    5365                 :            :         unsigned long long t;
    5366                 :            :         unsigned long usec_rem;
    5367                 :            : 
    5368                 :            :         s = kmalloc(sizeof(*s), GFP_KERNEL);
    5369         [ #  # ]:          0 :         if (!s)
    5370                 :            :                 return -ENOMEM;
    5371                 :            : 
    5372                 :            :         trace_seq_init(s);
    5373                 :            : 
    5374                 :          0 :         cnt = ring_buffer_entries_cpu(trace_buf->buffer, cpu);
    5375                 :          0 :         trace_seq_printf(s, "entries: %ld\n", cnt);
    5376                 :            : 
    5377                 :          0 :         cnt = ring_buffer_overrun_cpu(trace_buf->buffer, cpu);
    5378                 :          0 :         trace_seq_printf(s, "overrun: %ld\n", cnt);
    5379                 :            : 
    5380                 :          0 :         cnt = ring_buffer_commit_overrun_cpu(trace_buf->buffer, cpu);
    5381                 :          0 :         trace_seq_printf(s, "commit overrun: %ld\n", cnt);
    5382                 :            : 
    5383                 :          0 :         cnt = ring_buffer_bytes_cpu(trace_buf->buffer, cpu);
    5384                 :          0 :         trace_seq_printf(s, "bytes: %ld\n", cnt);
    5385                 :            : 
    5386         [ #  # ]:          0 :         if (trace_clocks[tr->clock_id].in_ns) {
    5387                 :            :                 /* local or global for trace_clock */
    5388                 :          0 :                 t = ns2usecs(ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
    5389                 :          0 :                 usec_rem = do_div(t, USEC_PER_SEC);
    5390                 :          0 :                 trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n",
    5391                 :            :                                                                 t, usec_rem);
    5392                 :            : 
    5393                 :          0 :                 t = ns2usecs(ring_buffer_time_stamp(trace_buf->buffer, cpu));
    5394                 :          0 :                 usec_rem = do_div(t, USEC_PER_SEC);
    5395                 :          0 :                 trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
    5396                 :            :         } else {
    5397                 :            :                 /* counter or tsc mode for trace_clock */
    5398                 :          0 :                 trace_seq_printf(s, "oldest event ts: %llu\n",
    5399                 :            :                                 ring_buffer_oldest_event_ts(trace_buf->buffer, cpu));
    5400                 :            : 
    5401                 :          0 :                 trace_seq_printf(s, "now ts: %llu\n",
    5402                 :            :                                 ring_buffer_time_stamp(trace_buf->buffer, cpu));
    5403                 :            :         }
    5404                 :            : 
    5405                 :          0 :         cnt = ring_buffer_dropped_events_cpu(trace_buf->buffer, cpu);
    5406                 :          0 :         trace_seq_printf(s, "dropped events: %ld\n", cnt);
    5407                 :            : 
    5408                 :          0 :         cnt = ring_buffer_read_events_cpu(trace_buf->buffer, cpu);
    5409                 :          0 :         trace_seq_printf(s, "read events: %ld\n", cnt);
    5410                 :            : 
    5411                 :          0 :         count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
    5412                 :            : 
    5413                 :          0 :         kfree(s);
    5414                 :            : 
    5415                 :          0 :         return count;
    5416                 :            : }
    5417                 :            : 
    5418                 :            : static const struct file_operations tracing_stats_fops = {
    5419                 :            :         .open           = tracing_open_generic_tr,
    5420                 :            :         .read           = tracing_stats_read,
    5421                 :            :         .llseek         = generic_file_llseek,
    5422                 :            :         .release        = tracing_release_generic_tr,
    5423                 :            : };
    5424                 :            : 
    5425                 :            : #ifdef CONFIG_DYNAMIC_FTRACE
    5426                 :            : 
    5427                 :          0 : int __weak ftrace_arch_read_dyn_info(char *buf, int size)
    5428                 :            : {
    5429                 :          0 :         return 0;
    5430                 :            : }
    5431                 :            : 
    5432                 :            : static ssize_t
    5433                 :          0 : tracing_read_dyn_info(struct file *filp, char __user *ubuf,
    5434                 :            :                   size_t cnt, loff_t *ppos)
    5435                 :            : {
    5436                 :            :         static char ftrace_dyn_info_buffer[1024];
    5437                 :            :         static DEFINE_MUTEX(dyn_info_mutex);
    5438                 :          0 :         unsigned long *p = filp->private_data;
    5439                 :            :         char *buf = ftrace_dyn_info_buffer;
    5440                 :            :         int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
    5441                 :            :         int r;
    5442                 :            : 
    5443                 :          0 :         mutex_lock(&dyn_info_mutex);
    5444                 :          0 :         r = sprintf(buf, "%ld ", *p);
    5445                 :            : 
    5446                 :          0 :         r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
    5447                 :          0 :         buf[r++] = '\n';
    5448                 :            : 
    5449                 :          0 :         r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
    5450                 :            : 
    5451                 :          0 :         mutex_unlock(&dyn_info_mutex);
    5452                 :            : 
    5453                 :          0 :         return r;
    5454                 :            : }
    5455                 :            : 
    5456                 :            : static const struct file_operations tracing_dyn_info_fops = {
    5457                 :            :         .open           = tracing_open_generic,
    5458                 :            :         .read           = tracing_read_dyn_info,
    5459                 :            :         .llseek         = generic_file_llseek,
    5460                 :            : };
    5461                 :            : #endif /* CONFIG_DYNAMIC_FTRACE */
    5462                 :            : 
    5463                 :            : #if defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE)
    5464                 :            : static void
    5465                 :            : ftrace_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
    5466                 :            : {
    5467                 :            :         tracing_snapshot();
    5468                 :            : }
    5469                 :            : 
    5470                 :            : static void
    5471                 :            : ftrace_count_snapshot(unsigned long ip, unsigned long parent_ip, void **data)
    5472                 :            : {
    5473                 :            :         unsigned long *count = (long *)data;
    5474                 :            : 
    5475                 :            :         if (!*count)
    5476                 :            :                 return;
    5477                 :            : 
    5478                 :            :         if (*count != -1)
    5479                 :            :                 (*count)--;
    5480                 :            : 
    5481                 :            :         tracing_snapshot();
    5482                 :            : }
    5483                 :            : 
    5484                 :            : static int
    5485                 :            : ftrace_snapshot_print(struct seq_file *m, unsigned long ip,
    5486                 :            :                       struct ftrace_probe_ops *ops, void *data)
    5487                 :            : {
    5488                 :            :         long count = (long)data;
    5489                 :            : 
    5490                 :            :         seq_printf(m, "%ps:", (void *)ip);
    5491                 :            : 
    5492                 :            :         seq_printf(m, "snapshot");
    5493                 :            : 
    5494                 :            :         if (count == -1)
    5495                 :            :                 seq_printf(m, ":unlimited\n");
    5496                 :            :         else
    5497                 :            :                 seq_printf(m, ":count=%ld\n", count);
    5498                 :            : 
    5499                 :            :         return 0;
    5500                 :            : }
    5501                 :            : 
    5502                 :            : static struct ftrace_probe_ops snapshot_probe_ops = {
    5503                 :            :         .func                   = ftrace_snapshot,
    5504                 :            :         .print                  = ftrace_snapshot_print,
    5505                 :            : };
    5506                 :            : 
    5507                 :            : static struct ftrace_probe_ops snapshot_count_probe_ops = {
    5508                 :            :         .func                   = ftrace_count_snapshot,
    5509                 :            :         .print                  = ftrace_snapshot_print,
    5510                 :            : };
    5511                 :            : 
    5512                 :            : static int
    5513                 :            : ftrace_trace_snapshot_callback(struct ftrace_hash *hash,
    5514                 :            :                                char *glob, char *cmd, char *param, int enable)
    5515                 :            : {
    5516                 :            :         struct ftrace_probe_ops *ops;
    5517                 :            :         void *count = (void *)-1;
    5518                 :            :         char *number;
    5519                 :            :         int ret;
    5520                 :            : 
    5521                 :            :         /* hash funcs only work with set_ftrace_filter */
    5522                 :            :         if (!enable)
    5523                 :            :                 return -EINVAL;
    5524                 :            : 
    5525                 :            :         ops = param ? &snapshot_count_probe_ops :  &snapshot_probe_ops;
    5526                 :            : 
    5527                 :            :         if (glob[0] == '!') {
    5528                 :            :                 unregister_ftrace_function_probe_func(glob+1, ops);
    5529                 :            :                 return 0;
    5530                 :            :         }
    5531                 :            : 
    5532                 :            :         if (!param)
    5533                 :            :                 goto out_reg;
    5534                 :            : 
    5535                 :            :         number = strsep(&param, ":");
    5536                 :            : 
    5537                 :            :         if (!strlen(number))
    5538                 :            :                 goto out_reg;
    5539                 :            : 
    5540                 :            :         /*
    5541                 :            :          * We use the callback data field (which is a pointer)
    5542                 :            :          * as our counter.
    5543                 :            :          */
    5544                 :            :         ret = kstrtoul(number, 0, (unsigned long *)&count);
    5545                 :            :         if (ret)
    5546                 :            :                 return ret;
    5547                 :            : 
    5548                 :            :  out_reg:
    5549                 :            :         ret = register_ftrace_function_probe(glob, ops, count);
    5550                 :            : 
    5551                 :            :         if (ret >= 0)
    5552                 :            :                 alloc_snapshot(&global_trace);
    5553                 :            : 
    5554                 :            :         return ret < 0 ? ret : 0;
    5555                 :            : }
    5556                 :            : 
    5557                 :            : static struct ftrace_func_command ftrace_snapshot_cmd = {
    5558                 :            :         .name                   = "snapshot",
    5559                 :            :         .func                   = ftrace_trace_snapshot_callback,
    5560                 :            : };
    5561                 :            : 
    5562                 :            : static __init int register_snapshot_cmd(void)
    5563                 :            : {
    5564                 :            :         return register_ftrace_command(&ftrace_snapshot_cmd);
    5565                 :            : }
    5566                 :            : #else
    5567                 :            : static inline __init int register_snapshot_cmd(void) { return 0; }
    5568                 :            : #endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
    5569                 :            : 
    5570                 :          0 : struct dentry *tracing_init_dentry_tr(struct trace_array *tr)
    5571                 :            : {
    5572         [ #  # ]:          0 :         if (tr->dir)
    5573                 :            :                 return tr->dir;
    5574                 :            : 
    5575         [ #  # ]:          0 :         if (!debugfs_initialized())
    5576                 :            :                 return NULL;
    5577                 :            : 
    5578         [ #  # ]:          0 :         if (tr->flags & TRACE_ARRAY_FL_GLOBAL)
    5579                 :          0 :                 tr->dir = debugfs_create_dir("tracing", NULL);
    5580                 :            : 
    5581         [ #  # ]:          0 :         if (!tr->dir)
    5582         [ #  # ]:          0 :                 pr_warn_once("Could not create debugfs directory 'tracing'\n");
    5583                 :            : 
    5584                 :          0 :         return tr->dir;
    5585                 :            : }
    5586                 :            : 
    5587                 :          0 : struct dentry *tracing_init_dentry(void)
    5588                 :            : {
    5589                 :          0 :         return tracing_init_dentry_tr(&global_trace);
    5590                 :            : }
    5591                 :            : 
    5592                 :          0 : static struct dentry *tracing_dentry_percpu(struct trace_array *tr, int cpu)
    5593                 :            : {
    5594                 :            :         struct dentry *d_tracer;
    5595                 :            : 
    5596         [ #  # ]:          0 :         if (tr->percpu_dir)
    5597                 :            :                 return tr->percpu_dir;
    5598                 :            : 
    5599                 :          0 :         d_tracer = tracing_init_dentry_tr(tr);
    5600         [ #  # ]:          0 :         if (!d_tracer)
    5601                 :            :                 return NULL;
    5602                 :            : 
    5603                 :          0 :         tr->percpu_dir = debugfs_create_dir("per_cpu", d_tracer);
    5604                 :            : 
    5605 [ #  # ][ #  # ]:          0 :         WARN_ONCE(!tr->percpu_dir,
                 [ #  # ]
    5606                 :            :                   "Could not create debugfs directory 'per_cpu/%d'\n", cpu);
    5607                 :            : 
    5608                 :          0 :         return tr->percpu_dir;
    5609                 :            : }
    5610                 :            : 
    5611                 :            : static struct dentry *
    5612                 :            : trace_create_cpu_file(const char *name, umode_t mode, struct dentry *parent,
    5613                 :            :                       void *data, long cpu, const struct file_operations *fops)
    5614                 :            : {
    5615                 :          0 :         struct dentry *ret = trace_create_file(name, mode, parent, data, fops);
    5616                 :            : 
    5617   [ #  #  #  #  :          0 :         if (ret) /* See tracing_get_cpu() */
          #  #  #  #  #  
                      # ]
    5618                 :          0 :                 ret->d_inode->i_cdev = (void *)(cpu + 1);
    5619                 :            :         return ret;
    5620                 :            : }
    5621                 :            : 
    5622                 :            : static void
    5623                 :          0 : tracing_init_debugfs_percpu(struct trace_array *tr, long cpu)
    5624                 :            : {
    5625                 :          0 :         struct dentry *d_percpu = tracing_dentry_percpu(tr, cpu);
    5626                 :            :         struct dentry *d_cpu;
    5627                 :            :         char cpu_dir[30]; /* 30 characters should be more than enough */
    5628                 :            : 
    5629         [ #  # ]:          0 :         if (!d_percpu)
    5630                 :          0 :                 return;
    5631                 :            : 
    5632                 :          0 :         snprintf(cpu_dir, 30, "cpu%ld", cpu);
    5633                 :          0 :         d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
    5634         [ #  # ]:          0 :         if (!d_cpu) {
    5635                 :          0 :                 pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
    5636                 :          0 :                 return;
    5637                 :            :         }
    5638                 :            : 
    5639                 :            :         /* per cpu trace_pipe */
    5640                 :            :         trace_create_cpu_file("trace_pipe", 0444, d_cpu,
    5641                 :            :                                 tr, cpu, &tracing_pipe_fops);
    5642                 :            : 
    5643                 :            :         /* per cpu trace */
    5644                 :            :         trace_create_cpu_file("trace", 0644, d_cpu,
    5645                 :            :                                 tr, cpu, &tracing_fops);
    5646                 :            : 
    5647                 :            :         trace_create_cpu_file("trace_pipe_raw", 0444, d_cpu,
    5648                 :            :                                 tr, cpu, &tracing_buffers_fops);
    5649                 :            : 
    5650                 :            :         trace_create_cpu_file("stats", 0444, d_cpu,
    5651                 :            :                                 tr, cpu, &tracing_stats_fops);
    5652                 :            : 
    5653                 :            :         trace_create_cpu_file("buffer_size_kb", 0444, d_cpu,
    5654                 :            :                                 tr, cpu, &tracing_entries_fops);
    5655                 :            : 
    5656                 :            : #ifdef CONFIG_TRACER_SNAPSHOT
    5657                 :            :         trace_create_cpu_file("snapshot", 0644, d_cpu,
    5658                 :            :                                 tr, cpu, &snapshot_fops);
    5659                 :            : 
    5660                 :            :         trace_create_cpu_file("snapshot_raw", 0444, d_cpu,
    5661                 :            :                                 tr, cpu, &snapshot_raw_fops);
    5662                 :            : #endif
    5663                 :            : }
    5664                 :            : 
    5665                 :            : #ifdef CONFIG_FTRACE_SELFTEST
    5666                 :            : /* Let selftest have access to static functions in this file */
    5667                 :            : #include "trace_selftest.c"
    5668                 :            : #endif
    5669                 :            : 
    5670                 :            : struct trace_option_dentry {
    5671                 :            :         struct tracer_opt               *opt;
    5672                 :            :         struct tracer_flags             *flags;
    5673                 :            :         struct trace_array              *tr;
    5674                 :            :         struct dentry                   *entry;
    5675                 :            : };
    5676                 :            : 
    5677                 :            : static ssize_t
    5678                 :          0 : trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
    5679                 :            :                         loff_t *ppos)
    5680                 :            : {
    5681                 :          0 :         struct trace_option_dentry *topt = filp->private_data;
    5682                 :            :         char *buf;
    5683                 :            : 
    5684         [ #  # ]:          0 :         if (topt->flags->val & topt->opt->bit)
    5685                 :            :                 buf = "1\n";
    5686                 :            :         else
    5687                 :            :                 buf = "0\n";
    5688                 :            : 
    5689                 :          0 :         return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
    5690                 :            : }
    5691                 :            : 
    5692                 :            : static ssize_t
    5693                 :          0 : trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
    5694                 :            :                          loff_t *ppos)
    5695                 :            : {
    5696                 :          0 :         struct trace_option_dentry *topt = filp->private_data;
    5697                 :            :         unsigned long val;
    5698                 :            :         int ret;
    5699                 :            : 
    5700                 :          0 :         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
    5701         [ #  # ]:          0 :         if (ret)
    5702                 :            :                 return ret;
    5703                 :            : 
    5704         [ #  # ]:          0 :         if (val != 0 && val != 1)
    5705                 :            :                 return -EINVAL;
    5706                 :            : 
    5707         [ #  # ]:          0 :         if (!!(topt->flags->val & topt->opt->bit) != val) {
    5708                 :          0 :                 mutex_lock(&trace_types_lock);
    5709                 :          0 :                 ret = __set_tracer_option(topt->tr->current_trace, topt->flags,
    5710                 :            :                                           topt->opt, !val);
    5711                 :          0 :                 mutex_unlock(&trace_types_lock);
    5712         [ #  # ]:          0 :                 if (ret)
    5713                 :            :                         return ret;
    5714                 :            :         }
    5715                 :            : 
    5716                 :          0 :         *ppos += cnt;
    5717                 :            : 
    5718                 :          0 :         return cnt;
    5719                 :            : }
    5720                 :            : 
    5721                 :            : 
    5722                 :            : static const struct file_operations trace_options_fops = {
    5723                 :            :         .open = tracing_open_generic,
    5724                 :            :         .read = trace_options_read,
    5725                 :            :         .write = trace_options_write,
    5726                 :            :         .llseek = generic_file_llseek,
    5727                 :            : };
    5728                 :            : 
    5729                 :            : static ssize_t
    5730                 :          0 : trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
    5731                 :            :                         loff_t *ppos)
    5732                 :            : {
    5733                 :          0 :         long index = (long)filp->private_data;
    5734                 :            :         char *buf;
    5735                 :            : 
    5736         [ #  # ]:          0 :         if (trace_flags & (1 << index))
    5737                 :            :                 buf = "1\n";
    5738                 :            :         else
    5739                 :            :                 buf = "0\n";
    5740                 :            : 
    5741                 :          0 :         return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
    5742                 :            : }
    5743                 :            : 
    5744                 :            : static ssize_t
    5745                 :          0 : trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
    5746                 :            :                          loff_t *ppos)
    5747                 :            : {
    5748                 :            :         struct trace_array *tr = &global_trace;
    5749                 :          0 :         long index = (long)filp->private_data;
    5750                 :            :         unsigned long val;
    5751                 :            :         int ret;
    5752                 :            : 
    5753                 :          0 :         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
    5754         [ #  # ]:          0 :         if (ret)
    5755                 :            :                 return ret;
    5756                 :            : 
    5757         [ #  # ]:          0 :         if (val != 0 && val != 1)
    5758                 :            :                 return -EINVAL;
    5759                 :            : 
    5760                 :          0 :         mutex_lock(&trace_types_lock);
    5761                 :          0 :         ret = set_tracer_flag(tr, 1 << index, val);
    5762                 :          0 :         mutex_unlock(&trace_types_lock);
    5763                 :            : 
    5764         [ #  # ]:          0 :         if (ret < 0)
    5765                 :            :                 return ret;
    5766                 :            : 
    5767                 :          0 :         *ppos += cnt;
    5768                 :            : 
    5769                 :          0 :         return cnt;
    5770                 :            : }
    5771                 :            : 
    5772                 :            : static const struct file_operations trace_options_core_fops = {
    5773                 :            :         .open = tracing_open_generic,
    5774                 :            :         .read = trace_options_core_read,
    5775                 :            :         .write = trace_options_core_write,
    5776                 :            :         .llseek = generic_file_llseek,
    5777                 :            : };
    5778                 :            : 
    5779                 :          0 : struct dentry *trace_create_file(const char *name,
    5780                 :            :                                  umode_t mode,
    5781                 :            :                                  struct dentry *parent,
    5782                 :            :                                  void *data,
    5783                 :            :                                  const struct file_operations *fops)
    5784                 :            : {
    5785                 :            :         struct dentry *ret;
    5786                 :            : 
    5787                 :          0 :         ret = debugfs_create_file(name, mode, parent, data, fops);
    5788         [ #  # ]:          0 :         if (!ret)
    5789                 :          0 :                 pr_warning("Could not create debugfs '%s' entry\n", name);
    5790                 :            : 
    5791                 :          0 :         return ret;
    5792                 :            : }
    5793                 :            : 
    5794                 :            : 
    5795                 :          0 : static struct dentry *trace_options_init_dentry(struct trace_array *tr)
    5796                 :            : {
    5797                 :            :         struct dentry *d_tracer;
    5798                 :            : 
    5799         [ #  # ]:          0 :         if (tr->options)
    5800                 :            :                 return tr->options;
    5801                 :            : 
    5802                 :          0 :         d_tracer = tracing_init_dentry_tr(tr);
    5803         [ #  # ]:          0 :         if (!d_tracer)
    5804                 :            :                 return NULL;
    5805                 :            : 
    5806                 :          0 :         tr->options = debugfs_create_dir("options", d_tracer);
    5807         [ #  # ]:          0 :         if (!tr->options) {
    5808                 :          0 :                 pr_warning("Could not create debugfs directory 'options'\n");
    5809                 :          0 :                 return NULL;
    5810                 :            :         }
    5811                 :            : 
    5812                 :            :         return tr->options;
    5813                 :            : }
    5814                 :            : 
    5815                 :            : static void
    5816                 :          0 : create_trace_option_file(struct trace_array *tr,
    5817                 :            :                          struct trace_option_dentry *topt,
    5818                 :            :                          struct tracer_flags *flags,
    5819                 :            :                          struct tracer_opt *opt)
    5820                 :            : {
    5821                 :            :         struct dentry *t_options;
    5822                 :            : 
    5823                 :          0 :         t_options = trace_options_init_dentry(tr);
    5824         [ #  # ]:          0 :         if (!t_options)
    5825                 :          0 :                 return;
    5826                 :            : 
    5827                 :          0 :         topt->flags = flags;
    5828                 :          0 :         topt->opt = opt;
    5829                 :          0 :         topt->tr = tr;
    5830                 :            : 
    5831                 :          0 :         topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
    5832                 :            :                                     &trace_options_fops);
    5833                 :            : 
    5834                 :            : }
    5835                 :            : 
    5836                 :            : static struct trace_option_dentry *
    5837                 :          0 : create_trace_option_files(struct trace_array *tr, struct tracer *tracer)
    5838                 :            : {
    5839                 :            :         struct trace_option_dentry *topts;
    5840                 :            :         struct tracer_flags *flags;
    5841                 :            :         struct tracer_opt *opts;
    5842                 :            :         int cnt;
    5843                 :            : 
    5844         [ #  # ]:          0 :         if (!tracer)
    5845                 :            :                 return NULL;
    5846                 :            : 
    5847                 :          0 :         flags = tracer->flags;
    5848                 :            : 
    5849 [ #  # ][ #  # ]:          0 :         if (!flags || !flags->opts)
    5850                 :            :                 return NULL;
    5851                 :            : 
    5852                 :            :         opts = flags->opts;
    5853                 :            : 
    5854         [ #  # ]:          0 :         for (cnt = 0; opts[cnt].name; cnt++)
    5855                 :            :                 ;
    5856                 :            : 
    5857                 :          0 :         topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
    5858         [ #  # ]:          0 :         if (!topts)
    5859                 :            :                 return NULL;
    5860                 :            : 
    5861         [ #  # ]:          0 :         for (cnt = 0; opts[cnt].name; cnt++)
    5862                 :          0 :                 create_trace_option_file(tr, &topts[cnt], flags,
    5863                 :            :                                          &opts[cnt]);
    5864                 :            : 
    5865                 :            :         return topts;
    5866                 :            : }
    5867                 :            : 
    5868                 :            : static void
    5869                 :          0 : destroy_trace_option_files(struct trace_option_dentry *topts)
    5870                 :            : {
    5871                 :            :         int cnt;
    5872                 :            : 
    5873         [ #  # ]:          0 :         if (!topts)
    5874                 :          0 :                 return;
    5875                 :            : 
    5876         [ #  # ]:          0 :         for (cnt = 0; topts[cnt].opt; cnt++) {
    5877         [ #  # ]:          0 :                 if (topts[cnt].entry)
    5878                 :          0 :                         debugfs_remove(topts[cnt].entry);
    5879                 :            :         }
    5880                 :            : 
    5881                 :          0 :         kfree(topts);
    5882                 :            : }
    5883                 :            : 
    5884                 :            : static struct dentry *
    5885                 :          0 : create_trace_option_core_file(struct trace_array *tr,
    5886                 :            :                               const char *option, long index)
    5887                 :            : {
    5888                 :            :         struct dentry *t_options;
    5889                 :            : 
    5890                 :          0 :         t_options = trace_options_init_dentry(tr);
    5891         [ #  # ]:          0 :         if (!t_options)
    5892                 :            :                 return NULL;
    5893                 :            : 
    5894                 :          0 :         return trace_create_file(option, 0644, t_options, (void *)index,
    5895                 :            :                                     &trace_options_core_fops);
    5896                 :            : }
    5897                 :            : 
    5898                 :          0 : static __init void create_trace_options_dir(struct trace_array *tr)
    5899                 :            : {
    5900                 :            :         struct dentry *t_options;
    5901                 :            :         int i;
    5902                 :            : 
    5903                 :          0 :         t_options = trace_options_init_dentry(tr);
    5904         [ #  # ]:          0 :         if (!t_options)
    5905                 :          0 :                 return;
    5906                 :            : 
    5907         [ #  # ]:          0 :         for (i = 0; trace_options[i]; i++)
    5908                 :          0 :                 create_trace_option_core_file(tr, trace_options[i], i);
    5909                 :            : }
    5910                 :            : 
    5911                 :            : static ssize_t
    5912                 :          0 : rb_simple_read(struct file *filp, char __user *ubuf,
    5913                 :            :                size_t cnt, loff_t *ppos)
    5914                 :            : {
    5915                 :          0 :         struct trace_array *tr = filp->private_data;
    5916                 :            :         char buf[64];
    5917                 :            :         int r;
    5918                 :            : 
    5919                 :            :         r = tracer_tracing_is_on(tr);
    5920                 :          0 :         r = sprintf(buf, "%d\n", r);
    5921                 :            : 
    5922                 :          0 :         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
    5923                 :            : }
    5924                 :            : 
    5925                 :            : static ssize_t
    5926                 :          0 : rb_simple_write(struct file *filp, const char __user *ubuf,
    5927                 :            :                 size_t cnt, loff_t *ppos)
    5928                 :            : {
    5929                 :          0 :         struct trace_array *tr = filp->private_data;
    5930                 :          0 :         struct ring_buffer *buffer = tr->trace_buffer.buffer;
    5931                 :            :         unsigned long val;
    5932                 :            :         int ret;
    5933                 :            : 
    5934                 :          0 :         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
    5935         [ #  # ]:          0 :         if (ret)
    5936                 :            :                 return ret;
    5937                 :            : 
    5938         [ #  # ]:          0 :         if (buffer) {
    5939                 :          0 :                 mutex_lock(&trace_types_lock);
    5940         [ #  # ]:          0 :                 if (val) {
    5941                 :            :                         tracer_tracing_on(tr);
    5942         [ #  # ]:          0 :                         if (tr->current_trace->start)
    5943                 :          0 :                                 tr->current_trace->start(tr);
    5944                 :            :                 } else {
    5945                 :            :                         tracer_tracing_off(tr);
    5946         [ #  # ]:          0 :                         if (tr->current_trace->stop)
    5947                 :          0 :                                 tr->current_trace->stop(tr);
    5948                 :            :                 }
    5949                 :          0 :                 mutex_unlock(&trace_types_lock);
    5950                 :            :         }
    5951                 :            : 
    5952                 :          0 :         (*ppos)++;
    5953                 :            : 
    5954                 :          0 :         return cnt;
    5955                 :            : }
    5956                 :            : 
    5957                 :            : static const struct file_operations rb_simple_fops = {
    5958                 :            :         .open           = tracing_open_generic_tr,
    5959                 :            :         .read           = rb_simple_read,
    5960                 :            :         .write          = rb_simple_write,
    5961                 :            :         .release        = tracing_release_generic_tr,
    5962                 :            :         .llseek         = default_llseek,
    5963                 :            : };
    5964                 :            : 
    5965                 :            : struct dentry *trace_instance_dir;
    5966                 :            : 
    5967                 :            : static void
    5968                 :            : init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer);
    5969                 :            : 
    5970                 :            : static int
    5971                 :          0 : allocate_trace_buffer(struct trace_array *tr, struct trace_buffer *buf, int size)
    5972                 :            : {
    5973                 :            :         enum ring_buffer_flags rb_flags;
    5974                 :            : 
    5975                 :          0 :         rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
    5976                 :            : 
    5977                 :          0 :         buf->buffer = ring_buffer_alloc(size, rb_flags);
    5978         [ #  # ]:          0 :         if (!buf->buffer)
    5979                 :            :                 return -ENOMEM;
    5980                 :            : 
    5981                 :          0 :         buf->data = alloc_percpu(struct trace_array_cpu);
    5982         [ #  # ]:          0 :         if (!buf->data) {
    5983                 :          0 :                 ring_buffer_free(buf->buffer);
    5984                 :            :                 return -ENOMEM;
    5985                 :            :         }
    5986                 :            : 
    5987                 :            :         /* Allocate the first page for all buffers */
    5988                 :          0 :         set_buffer_entries(&tr->trace_buffer,
    5989                 :            :                            ring_buffer_size(tr->trace_buffer.buffer, 0));
    5990                 :            : 
    5991                 :            :         return 0;
    5992                 :            : }
    5993                 :            : 
    5994                 :            : static int allocate_trace_buffers(struct trace_array *tr, int size)
    5995                 :            : {
    5996                 :            :         int ret;
    5997                 :            : 
    5998                 :          0 :         ret = allocate_trace_buffer(tr, &tr->trace_buffer, size);
    5999 [ #  # ][ #  # ]:          0 :         if (ret)
    6000                 :            :                 return ret;
    6001                 :            : 
    6002                 :            : #ifdef CONFIG_TRACER_MAX_TRACE
    6003                 :            :         ret = allocate_trace_buffer(tr, &tr->max_buffer,
    6004                 :            :                                     allocate_snapshot ? size : 1);
    6005                 :            :         if (WARN_ON(ret)) {
    6006                 :            :                 ring_buffer_free(tr->trace_buffer.buffer);
    6007                 :            :                 free_percpu(tr->trace_buffer.data);
    6008                 :            :                 return -ENOMEM;
    6009                 :            :         }
    6010                 :            :         tr->allocated_snapshot = allocate_snapshot;
    6011                 :            : 
    6012                 :            :         /*
    6013                 :            :          * Only the top level trace array gets its snapshot allocated
    6014                 :            :          * from the kernel command line.
    6015                 :            :          */
    6016                 :            :         allocate_snapshot = false;
    6017                 :            : #endif
    6018                 :            :         return 0;
    6019                 :            : }
    6020                 :            : 
    6021                 :          0 : static int new_instance_create(const char *name)
    6022                 :            : {
    6023                 :            :         struct trace_array *tr;
    6024                 :            :         int ret;
    6025                 :            : 
    6026                 :          0 :         mutex_lock(&trace_types_lock);
    6027                 :            : 
    6028                 :            :         ret = -EEXIST;
    6029         [ #  # ]:          0 :         list_for_each_entry(tr, &ftrace_trace_arrays, list) {
    6030 [ #  # ][ #  # ]:          0 :                 if (tr->name && strcmp(tr->name, name) == 0)
    6031                 :            :                         goto out_unlock;
    6032                 :            :         }
    6033                 :            : 
    6034                 :            :         ret = -ENOMEM;
    6035                 :            :         tr = kzalloc(sizeof(*tr), GFP_KERNEL);
    6036         [ #  # ]:          0 :         if (!tr)
    6037                 :            :                 goto out_unlock;
    6038                 :            : 
    6039                 :          0 :         tr->name = kstrdup(name, GFP_KERNEL);
    6040         [ #  # ]:          0 :         if (!tr->name)
    6041                 :            :                 goto out_free_tr;
    6042                 :            : 
    6043                 :            :         if (!alloc_cpumask_var(&tr->tracing_cpumask, GFP_KERNEL))
    6044                 :            :                 goto out_free_tr;
    6045                 :            : 
    6046                 :            :         cpumask_copy(tr->tracing_cpumask, cpu_all_mask);
    6047                 :            : 
    6048                 :          0 :         raw_spin_lock_init(&tr->start_lock);
    6049                 :            : 
    6050                 :          0 :         tr->current_trace = &nop_trace;
    6051                 :            : 
    6052                 :          0 :         INIT_LIST_HEAD(&tr->systems);
    6053                 :          0 :         INIT_LIST_HEAD(&tr->events);
    6054                 :            : 
    6055         [ #  # ]:          0 :         if (allocate_trace_buffers(tr, trace_buf_size) < 0)
    6056                 :            :                 goto out_free_tr;
    6057                 :            : 
    6058                 :          0 :         tr->dir = debugfs_create_dir(name, trace_instance_dir);
    6059         [ #  # ]:          0 :         if (!tr->dir)
    6060                 :            :                 goto out_free_tr;
    6061                 :            : 
    6062                 :          0 :         ret = event_trace_add_tracer(tr->dir, tr);
    6063         [ #  # ]:          0 :         if (ret) {
    6064                 :          0 :                 debugfs_remove_recursive(tr->dir);
    6065                 :          0 :                 goto out_free_tr;
    6066                 :            :         }
    6067                 :            : 
    6068                 :          0 :         init_tracer_debugfs(tr, tr->dir);
    6069                 :            : 
    6070                 :          0 :         list_add(&tr->list, &ftrace_trace_arrays);
    6071                 :            : 
    6072                 :          0 :         mutex_unlock(&trace_types_lock);
    6073                 :            : 
    6074                 :          0 :         return 0;
    6075                 :            : 
    6076                 :            :  out_free_tr:
    6077         [ #  # ]:          0 :         if (tr->trace_buffer.buffer)
    6078                 :          0 :                 ring_buffer_free(tr->trace_buffer.buffer);
    6079                 :            :         free_cpumask_var(tr->tracing_cpumask);
    6080                 :          0 :         kfree(tr->name);
    6081                 :          0 :         kfree(tr);
    6082                 :            : 
    6083                 :            :  out_unlock:
    6084                 :          0 :         mutex_unlock(&trace_types_lock);
    6085                 :            : 
    6086                 :          0 :         return ret;
    6087                 :            : 
    6088                 :            : }
    6089                 :            : 
    6090                 :          0 : static int instance_delete(const char *name)
    6091                 :            : {
    6092                 :            :         struct trace_array *tr;
    6093                 :            :         int found = 0;
    6094                 :            :         int ret;
    6095                 :            : 
    6096                 :          0 :         mutex_lock(&trace_types_lock);
    6097                 :            : 
    6098                 :            :         ret = -ENODEV;
    6099         [ #  # ]:          0 :         list_for_each_entry(tr, &ftrace_trace_arrays, list) {
    6100 [ #  # ][ #  # ]:          0 :                 if (tr->name && strcmp(tr->name, name) == 0) {
    6101                 :            :                         found = 1;
    6102                 :            :                         break;
    6103                 :            :                 }
    6104                 :            :         }
    6105         [ #  # ]:          0 :         if (!found)
    6106                 :            :                 goto out_unlock;
    6107                 :            : 
    6108                 :            :         ret = -EBUSY;
    6109         [ #  # ]:          0 :         if (tr->ref)
    6110                 :            :                 goto out_unlock;
    6111                 :            : 
    6112                 :            :         list_del(&tr->list);
    6113                 :            : 
    6114                 :          0 :         event_trace_del_tracer(tr);
    6115                 :          0 :         debugfs_remove_recursive(tr->dir);
    6116                 :          0 :         free_percpu(tr->trace_buffer.data);
    6117                 :          0 :         ring_buffer_free(tr->trace_buffer.buffer);
    6118                 :            : 
    6119                 :          0 :         kfree(tr->name);
    6120                 :          0 :         kfree(tr);
    6121                 :            : 
    6122                 :            :         ret = 0;
    6123                 :            : 
    6124                 :            :  out_unlock:
    6125                 :          0 :         mutex_unlock(&trace_types_lock);
    6126                 :            : 
    6127                 :          0 :         return ret;
    6128                 :            : }
    6129                 :            : 
    6130                 :          0 : static int instance_mkdir (struct inode *inode, struct dentry *dentry, umode_t mode)
    6131                 :            : {
    6132                 :            :         struct dentry *parent;
    6133                 :            :         int ret;
    6134                 :            : 
    6135                 :            :         /* Paranoid: Make sure the parent is the "instances" directory */
    6136                 :          0 :         parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
    6137 [ #  # ][ #  # ]:          0 :         if (WARN_ON_ONCE(parent != trace_instance_dir))
         [ #  # ][ #  # ]
    6138                 :            :                 return -ENOENT;
    6139                 :            : 
    6140                 :            :         /*
    6141                 :            :          * The inode mutex is locked, but debugfs_create_dir() will also
    6142                 :            :          * take the mutex. As the instances directory can not be destroyed
    6143                 :            :          * or changed in any other way, it is safe to unlock it, and
    6144                 :            :          * let the dentry try. If two users try to make the same dir at
    6145                 :            :          * the same time, then the new_instance_create() will determine the
    6146                 :            :          * winner.
    6147                 :            :          */
    6148                 :          0 :         mutex_unlock(&inode->i_mutex);
    6149                 :            : 
    6150                 :          0 :         ret = new_instance_create(dentry->d_iname);
    6151                 :            : 
    6152                 :          0 :         mutex_lock(&inode->i_mutex);
    6153                 :            : 
    6154                 :          0 :         return ret;
    6155                 :            : }
    6156                 :            : 
    6157                 :          0 : static int instance_rmdir(struct inode *inode, struct dentry *dentry)
    6158                 :            : {
    6159                 :            :         struct dentry *parent;
    6160                 :            :         int ret;
    6161                 :            : 
    6162                 :            :         /* Paranoid: Make sure the parent is the "instances" directory */
    6163                 :          0 :         parent = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
    6164 [ #  # ][ #  # ]:          0 :         if (WARN_ON_ONCE(parent != trace_instance_dir))
         [ #  # ][ #  # ]
    6165                 :            :                 return -ENOENT;
    6166                 :            : 
    6167                 :            :         /* The caller did a dget() on dentry */
    6168                 :          0 :         mutex_unlock(&dentry->d_inode->i_mutex);
    6169                 :            : 
    6170                 :            :         /*
    6171                 :            :          * The inode mutex is locked, but debugfs_create_dir() will also
    6172                 :            :          * take the mutex. As the instances directory can not be destroyed
    6173                 :            :          * or changed in any other way, it is safe to unlock it, and
    6174                 :            :          * let the dentry try. If two users try to make the same dir at
    6175                 :            :          * the same time, then the instance_delete() will determine the
    6176                 :            :          * winner.
    6177                 :            :          */
    6178                 :          0 :         mutex_unlock(&inode->i_mutex);
    6179                 :            : 
    6180                 :          0 :         ret = instance_delete(dentry->d_iname);
    6181                 :            : 
    6182                 :          0 :         mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
    6183                 :          0 :         mutex_lock(&dentry->d_inode->i_mutex);
    6184                 :            : 
    6185                 :          0 :         return ret;
    6186                 :            : }
    6187                 :            : 
    6188                 :            : static const struct inode_operations instance_dir_inode_operations = {
    6189                 :            :         .lookup         = simple_lookup,
    6190                 :            :         .mkdir          = instance_mkdir,
    6191                 :            :         .rmdir          = instance_rmdir,
    6192                 :            : };
    6193                 :            : 
    6194                 :          0 : static __init void create_trace_instances(struct dentry *d_tracer)
    6195                 :            : {
    6196                 :          0 :         trace_instance_dir = debugfs_create_dir("instances", d_tracer);
    6197 [ #  # ][ #  # ]:          0 :         if (WARN_ON(!trace_instance_dir))
    6198                 :          0 :                 return;
    6199                 :            : 
    6200                 :            :         /* Hijack the dir inode operations, to allow mkdir */
    6201                 :          0 :         trace_instance_dir->d_inode->i_op = &instance_dir_inode_operations;
    6202                 :            : }
    6203                 :            : 
    6204                 :            : static void
    6205                 :          0 : init_tracer_debugfs(struct trace_array *tr, struct dentry *d_tracer)
    6206                 :            : {
    6207                 :            :         int cpu;
    6208                 :            : 
    6209                 :          0 :         trace_create_file("tracing_cpumask", 0644, d_tracer,
    6210                 :            :                           tr, &tracing_cpumask_fops);
    6211                 :            : 
    6212                 :          0 :         trace_create_file("trace_options", 0644, d_tracer,
    6213                 :            :                           tr, &tracing_iter_fops);
    6214                 :            : 
    6215                 :          0 :         trace_create_file("trace", 0644, d_tracer,
    6216                 :            :                           tr, &tracing_fops);
    6217                 :            : 
    6218                 :          0 :         trace_create_file("trace_pipe", 0444, d_tracer,
    6219                 :            :                           tr, &tracing_pipe_fops);
    6220                 :            : 
    6221                 :          0 :         trace_create_file("buffer_size_kb", 0644, d_tracer,
    6222                 :            :                           tr, &tracing_entries_fops);
    6223                 :            : 
    6224                 :          0 :         trace_create_file("buffer_total_size_kb", 0444, d_tracer,
    6225                 :            :                           tr, &tracing_total_entries_fops);
    6226                 :            : 
    6227                 :          0 :         trace_create_file("free_buffer", 0200, d_tracer,
    6228                 :            :                           tr, &tracing_free_buffer_fops);
    6229                 :            : 
    6230                 :          0 :         trace_create_file("trace_marker", 0220, d_tracer,
    6231                 :            :                           tr, &tracing_mark_fops);
    6232                 :            : 
    6233                 :          0 :         trace_create_file("saved_tgids", 0444, d_tracer,
    6234                 :            :                           tr, &tracing_saved_tgids_fops);
    6235                 :            : 
    6236                 :          0 :         trace_create_file("trace_clock", 0644, d_tracer, tr,
    6237                 :            :                           &trace_clock_fops);
    6238                 :            : 
    6239                 :          0 :         trace_create_file("tracing_on", 0644, d_tracer,
    6240                 :            :                           tr, &rb_simple_fops);
    6241                 :            : 
    6242                 :            : #ifdef CONFIG_TRACER_SNAPSHOT
    6243                 :            :         trace_create_file("snapshot", 0644, d_tracer,
    6244                 :            :                           tr, &snapshot_fops);
    6245                 :            : #endif
    6246                 :            : 
    6247         [ #  # ]:          0 :         for_each_tracing_cpu(cpu)
    6248                 :          0 :                 tracing_init_debugfs_percpu(tr, cpu);
    6249                 :            : 
    6250                 :          0 : }
    6251                 :            : 
    6252                 :          0 : static __init int tracer_init_debugfs(void)
    6253                 :            : {
    6254                 :            :         struct dentry *d_tracer;
    6255                 :            : 
    6256                 :            :         trace_access_lock_init();
    6257                 :            : 
    6258                 :            :         d_tracer = tracing_init_dentry();
    6259         [ #  # ]:          0 :         if (!d_tracer)
    6260                 :            :                 return 0;
    6261                 :            : 
    6262                 :          0 :         init_tracer_debugfs(&global_trace, d_tracer);
    6263                 :            : 
    6264                 :          0 :         trace_create_file("available_tracers", 0444, d_tracer,
    6265                 :            :                         &global_trace, &show_traces_fops);
    6266                 :            : 
    6267                 :          0 :         trace_create_file("current_tracer", 0644, d_tracer,
    6268                 :            :                         &global_trace, &set_tracer_fops);
    6269                 :            : 
    6270                 :            : #ifdef CONFIG_TRACER_MAX_TRACE
    6271                 :            :         trace_create_file("tracing_max_latency", 0644, d_tracer,
    6272                 :            :                         &tracing_max_latency, &tracing_max_lat_fops);
    6273                 :            : #endif
    6274                 :            : 
    6275                 :          0 :         trace_create_file("tracing_thresh", 0644, d_tracer,
    6276                 :            :                         &tracing_thresh, &tracing_max_lat_fops);
    6277                 :            : 
    6278                 :          0 :         trace_create_file("README", 0444, d_tracer,
    6279                 :            :                         NULL, &tracing_readme_fops);
    6280                 :            : 
    6281                 :          0 :         trace_create_file("saved_cmdlines", 0444, d_tracer,
    6282                 :            :                         NULL, &tracing_saved_cmdlines_fops);
    6283                 :            : 
    6284                 :            : #ifdef CONFIG_DYNAMIC_FTRACE
    6285                 :          0 :         trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
    6286                 :            :                         &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
    6287                 :            : #endif
    6288                 :            : 
    6289                 :          0 :         create_trace_instances(d_tracer);
    6290                 :            : 
    6291                 :          0 :         create_trace_options_dir(&global_trace);
    6292                 :            : 
    6293                 :          0 :         return 0;
    6294                 :            : }
    6295                 :            : 
    6296                 :          0 : static int trace_panic_handler(struct notifier_block *this,
    6297                 :            :                                unsigned long event, void *unused)
    6298                 :            : {
    6299         [ #  # ]:          0 :         if (ftrace_dump_on_oops)
    6300                 :          0 :                 ftrace_dump(ftrace_dump_on_oops);
    6301                 :          0 :         return NOTIFY_OK;
    6302                 :            : }
    6303                 :            : 
    6304                 :            : static struct notifier_block trace_panic_notifier = {
    6305                 :            :         .notifier_call  = trace_panic_handler,
    6306                 :            :         .next           = NULL,
    6307                 :            :         .priority       = 150   /* priority: INT_MAX >= x >= 0 */
    6308                 :            : };
    6309                 :            : 
    6310                 :          0 : static int trace_die_handler(struct notifier_block *self,
    6311                 :            :                              unsigned long val,
    6312                 :            :                              void *data)
    6313                 :            : {
    6314         [ #  # ]:          0 :         switch (val) {
    6315                 :            :         case DIE_OOPS:
    6316         [ #  # ]:          0 :                 if (ftrace_dump_on_oops)
    6317                 :          0 :                         ftrace_dump(ftrace_dump_on_oops);
    6318                 :            :                 break;
    6319                 :            :         default:
    6320                 :            :                 break;
    6321                 :            :         }
    6322                 :          0 :         return NOTIFY_OK;
    6323                 :            : }
    6324                 :            : 
    6325                 :            : static struct notifier_block trace_die_notifier = {
    6326                 :            :         .notifier_call = trace_die_handler,
    6327                 :            :         .priority = 200
    6328                 :            : };
    6329                 :            : 
    6330                 :            : /*
    6331                 :            :  * printk is set to max of 1024, we really don't need it that big.
    6332                 :            :  * Nothing should be printing 1000 characters anyway.
    6333                 :            :  */
    6334                 :            : #define TRACE_MAX_PRINT         1000
    6335                 :            : 
    6336                 :            : /*
    6337                 :            :  * Define here KERN_TRACE so that we have one place to modify
    6338                 :            :  * it if we decide to change what log level the ftrace dump
    6339                 :            :  * should be at.
    6340                 :            :  */
    6341                 :            : #define KERN_TRACE              KERN_EMERG
    6342                 :            : 
    6343                 :            : void
    6344                 :          0 : trace_printk_seq(struct trace_seq *s)
    6345                 :            : {
    6346                 :            :         /* Probably should print a warning here. */
    6347         [ #  # ]:          0 :         if (s->len >= TRACE_MAX_PRINT)
    6348                 :          0 :                 s->len = TRACE_MAX_PRINT;
    6349                 :            : 
    6350                 :            :         /* should be zero ended, but we are paranoid. */
    6351                 :          0 :         s->buffer[s->len] = 0;
    6352                 :            : 
    6353                 :          0 :         printk(KERN_TRACE "%s", s->buffer);
    6354                 :            : 
    6355                 :            :         trace_seq_init(s);
    6356                 :          0 : }
    6357                 :            : 
    6358                 :          0 : void trace_init_global_iter(struct trace_iterator *iter)
    6359                 :            : {
    6360                 :          0 :         iter->tr = &global_trace;
    6361                 :          0 :         iter->trace = iter->tr->current_trace;
    6362                 :          0 :         iter->cpu_file = RING_BUFFER_ALL_CPUS;
    6363                 :          0 :         iter->trace_buffer = &global_trace.trace_buffer;
    6364                 :            : 
    6365 [ #  # ][ #  # ]:          0 :         if (iter->trace && iter->trace->open)
    6366                 :          0 :                 iter->trace->open(iter);
    6367                 :            : 
    6368                 :            :         /* Annotate start of buffers if we had overruns */
    6369         [ #  # ]:          0 :         if (ring_buffer_overruns(iter->trace_buffer->buffer))
    6370                 :          0 :                 iter->iter_flags |= TRACE_FILE_ANNOTATE;
    6371                 :            : 
    6372                 :            :         /* Output in nanoseconds only if we are using a clock in nanoseconds. */
    6373         [ #  # ]:          0 :         if (trace_clocks[iter->tr->clock_id].in_ns)
    6374                 :          0 :                 iter->iter_flags |= TRACE_FILE_TIME_IN_NS;
    6375                 :          0 : }
    6376                 :            : 
    6377                 :          0 : void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
    6378                 :            : {
    6379                 :            :         /* use static because iter can be a bit big for the stack */
    6380                 :            :         static struct trace_iterator iter;
    6381                 :            :         static atomic_t dump_running;
    6382                 :            :         unsigned int old_userobj;
    6383                 :            :         unsigned long flags;
    6384                 :            :         int cnt = 0, cpu;
    6385                 :            : 
    6386                 :            :         /* Only allow one dump user at a time. */
    6387         [ #  # ]:          0 :         if (atomic_inc_return(&dump_running) != 1) {
    6388                 :            :                 atomic_dec(&dump_running);
    6389                 :          0 :                 return;
    6390                 :            :         }
    6391                 :            : 
    6392                 :            :         /*
    6393                 :            :          * Always turn off tracing when we dump.
    6394                 :            :          * We don't need to show trace output of what happens
    6395                 :            :          * between multiple crashes.
    6396                 :            :          *
    6397                 :            :          * If the user does a sysrq-z, then they can re-enable
    6398                 :            :          * tracing with echo 1 > tracing_on.
    6399                 :            :          */
    6400                 :          0 :         tracing_off();
    6401                 :            : 
    6402                 :            :         local_irq_save(flags);
    6403                 :            : 
    6404                 :            :         /* Simulate the iterator */
    6405                 :          0 :         trace_init_global_iter(&iter);
    6406                 :            : 
    6407         [ #  # ]:          0 :         for_each_tracing_cpu(cpu) {
    6408                 :          0 :                 atomic_inc(&per_cpu_ptr(iter.tr->trace_buffer.data, cpu)->disabled);
    6409                 :            :         }
    6410                 :            : 
    6411                 :          0 :         old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
    6412                 :            : 
    6413                 :            :         /* don't look at user memory in panic mode */
    6414                 :          0 :         trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
    6415                 :            : 
    6416   [ #  #  #  # ]:          0 :         switch (oops_dump_mode) {
    6417                 :            :         case DUMP_ALL:
    6418                 :          0 :                 iter.cpu_file = RING_BUFFER_ALL_CPUS;
    6419                 :          0 :                 break;
    6420                 :            :         case DUMP_ORIG:
    6421                 :          0 :                 iter.cpu_file = raw_smp_processor_id();
    6422                 :          0 :                 break;
    6423                 :            :         case DUMP_NONE:
    6424                 :            :                 goto out_enable;
    6425                 :            :         default:
    6426                 :          0 :                 printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
    6427                 :          0 :                 iter.cpu_file = RING_BUFFER_ALL_CPUS;
    6428                 :            :         }
    6429                 :            : 
    6430                 :          0 :         printk(KERN_TRACE "Dumping ftrace buffer:\n");
    6431                 :            : 
    6432                 :            :         /* Did function tracer already get disabled? */
    6433         [ #  # ]:          0 :         if (ftrace_is_dead()) {
    6434                 :          0 :                 printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
    6435                 :          0 :                 printk("#          MAY BE MISSING FUNCTION EVENTS\n");
    6436                 :            :         }
    6437                 :            : 
    6438                 :            :         /*
    6439                 :            :          * We need to stop all tracing on all CPUS to read the
    6440                 :            :          * the next buffer. This is a bit expensive, but is
    6441                 :            :          * not done often. We fill all what we can read,
    6442                 :            :          * and then release the locks again.
    6443                 :            :          */
    6444                 :            : 
    6445         [ #  # ]:          0 :         while (!trace_empty(&iter)) {
    6446                 :            : 
    6447         [ #  # ]:          0 :                 if (!cnt)
    6448                 :          0 :                         printk(KERN_TRACE "---------------------------------\n");
    6449                 :            : 
    6450                 :          0 :                 cnt++;
    6451                 :            : 
    6452                 :            :                 /* reset all but tr, trace, and overruns */
    6453                 :          0 :                 memset(&iter.seq, 0,
    6454                 :            :                        sizeof(struct trace_iterator) -
    6455                 :            :                        offsetof(struct trace_iterator, seq));
    6456                 :          0 :                 iter.iter_flags |= TRACE_FILE_LAT_FMT;
    6457                 :          0 :                 iter.pos = -1;
    6458                 :            : 
    6459         [ #  # ]:          0 :                 if (trace_find_next_entry_inc(&iter) != NULL) {
    6460                 :            :                         int ret;
    6461                 :            : 
    6462                 :          0 :                         ret = print_trace_line(&iter);
    6463         [ #  # ]:          0 :                         if (ret != TRACE_TYPE_NO_CONSUME)
    6464                 :            :                                 trace_consume(&iter);
    6465                 :            :                 }
    6466                 :            :                 touch_nmi_watchdog();
    6467                 :            : 
    6468                 :          0 :                 trace_printk_seq(&iter.seq);
    6469                 :            :         }
    6470                 :            : 
    6471         [ #  # ]:          0 :         if (!cnt)
    6472                 :          0 :                 printk(KERN_TRACE "   (ftrace buffer empty)\n");
    6473                 :            :         else
    6474                 :          0 :                 printk(KERN_TRACE "---------------------------------\n");
    6475                 :            : 
    6476                 :            :  out_enable:
    6477                 :          0 :         trace_flags |= old_userobj;
    6478                 :            : 
    6479         [ #  # ]:          0 :         for_each_tracing_cpu(cpu) {
    6480                 :          0 :                 atomic_dec(&per_cpu_ptr(iter.trace_buffer->data, cpu)->disabled);
    6481                 :            :         }
    6482                 :            :         atomic_dec(&dump_running);
    6483         [ #  # ]:          0 :         local_irq_restore(flags);
    6484                 :            : }
    6485                 :            : EXPORT_SYMBOL_GPL(ftrace_dump);
    6486                 :            : 
    6487                 :          0 : __init static int tracer_alloc_buffers(void)
    6488                 :            : {
    6489                 :            :         int ring_buf_size;
    6490                 :            :         int ret = -ENOMEM;
    6491                 :            : 
    6492                 :            : 
    6493                 :            :         if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
    6494                 :            :                 goto out;
    6495                 :            : 
    6496                 :            :         if (!alloc_cpumask_var(&global_trace.tracing_cpumask, GFP_KERNEL))
    6497                 :            :                 goto out_free_buffer_mask;
    6498                 :            : 
    6499                 :            :         /* Only allocate trace_printk buffers if a trace_printk exists */
    6500         [ #  # ]:          0 :         if (__stop___trace_bprintk_fmt != __start___trace_bprintk_fmt)
    6501                 :            :                 /* Must be called before global_trace.buffer is allocated */
    6502                 :          0 :                 trace_printk_init_buffers();
    6503                 :            : 
    6504                 :            :         /* To save memory, keep the ring buffer size to its minimum */
    6505         [ #  # ]:          0 :         if (ring_buffer_expanded)
    6506                 :          0 :                 ring_buf_size = trace_buf_size;
    6507                 :            :         else
    6508                 :            :                 ring_buf_size = 1;
    6509                 :            : 
    6510                 :          0 :         cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
    6511                 :            :         cpumask_copy(global_trace.tracing_cpumask, cpu_all_mask);
    6512                 :            : 
    6513                 :          0 :         raw_spin_lock_init(&global_trace.start_lock);
    6514                 :            : 
    6515                 :            :         /* TODO: make the number of buffers hot pluggable with CPUS */
    6516         [ #  # ]:          0 :         if (allocate_trace_buffers(&global_trace, ring_buf_size) < 0) {
    6517                 :          0 :                 printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
    6518                 :          0 :                 WARN_ON(1);
    6519                 :            :                 goto out_free_cpumask;
    6520                 :            :         }
    6521                 :            : 
    6522         [ #  # ]:          0 :         if (global_trace.buffer_disabled)
    6523                 :          0 :                 tracing_off();
    6524                 :            : 
    6525                 :          0 :         trace_init_cmdlines();
    6526                 :            : 
    6527                 :            :         /*
    6528                 :            :          * register_tracer() might reference current_trace, so it
    6529                 :            :          * needs to be set before we register anything. This is
    6530                 :            :          * just a bootstrap of current_trace anyway.
    6531                 :            :          */
    6532                 :          0 :         global_trace.current_trace = &nop_trace;
    6533                 :            : 
    6534                 :          0 :         register_tracer(&nop_trace);
    6535                 :            : 
    6536                 :            :         /* All seems OK, enable tracing */
    6537                 :          0 :         tracing_disabled = 0;
    6538                 :            : 
    6539                 :          0 :         atomic_notifier_chain_register(&panic_notifier_list,
    6540                 :            :                                        &trace_panic_notifier);
    6541                 :            : 
    6542                 :          0 :         register_die_notifier(&trace_die_notifier);
    6543                 :            : 
    6544                 :          0 :         global_trace.flags = TRACE_ARRAY_FL_GLOBAL;
    6545                 :            : 
    6546                 :            :         INIT_LIST_HEAD(&global_trace.systems);
    6547                 :            :         INIT_LIST_HEAD(&global_trace.events);
    6548                 :            :         list_add(&global_trace.list, &ftrace_trace_arrays);
    6549                 :            : 
    6550         [ #  # ]:          0 :         while (trace_boot_options) {
    6551                 :            :                 char *option;
    6552                 :            : 
    6553                 :          0 :                 option = strsep(&trace_boot_options, ",");
    6554                 :          0 :                 trace_set_options(&global_trace, option);
    6555                 :            :         }
    6556                 :            : 
    6557                 :            :         register_snapshot_cmd();
    6558                 :            : 
    6559                 :          0 :         return 0;
    6560                 :            : 
    6561                 :            : out_free_cpumask:
    6562                 :          0 :         free_percpu(global_trace.trace_buffer.data);
    6563                 :            : #ifdef CONFIG_TRACER_MAX_TRACE
    6564                 :            :         free_percpu(global_trace.max_buffer.data);
    6565                 :            : #endif
    6566                 :            :         free_cpumask_var(global_trace.tracing_cpumask);
    6567                 :            : out_free_buffer_mask:
    6568                 :            :         free_cpumask_var(tracing_buffer_mask);
    6569                 :            : out:
    6570                 :          0 :         return ret;
    6571                 :            : }
    6572                 :            : 
    6573                 :          0 : __init static int clear_boot_tracer(void)
    6574                 :            : {
    6575                 :            :         /*
    6576                 :            :          * The default tracer at boot buffer is an init section.
    6577                 :            :          * This function is called in lateinit. If we did not
    6578                 :            :          * find the boot tracer, then clear it out, to prevent
    6579                 :            :          * later registration from accessing the buffer that is
    6580                 :            :          * about to be freed.
    6581                 :            :          */
    6582         [ #  # ]:          0 :         if (!default_bootup_tracer)
    6583                 :            :                 return 0;
    6584                 :            : 
    6585                 :          0 :         printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
    6586                 :            :                default_bootup_tracer);
    6587                 :          0 :         default_bootup_tracer = NULL;
    6588                 :            : 
    6589                 :          0 :         return 0;
    6590                 :            : }
    6591                 :            : 
    6592                 :            : early_initcall(tracer_alloc_buffers);
    6593                 :            : fs_initcall(tracer_init_debugfs);
    6594                 :            : late_initcall(clear_boot_tracer);

Generated by: LCOV version 1.9