]> asedeno.scripts.mit.edu Git - linux.git/blobdiff - kernel/trace/trace.c
trace/probes: Remove kernel doc style from non kernel doc comment
[linux.git] / kernel / trace / trace.c
index ff1c4b20cd0a6d29209950fa006542c71b8c43ef..e9cc47e59d25e6d60db9eae88bdbdfd21fc609e8 100644 (file)
@@ -894,7 +894,7 @@ int __trace_bputs(unsigned long ip, const char *str)
 EXPORT_SYMBOL_GPL(__trace_bputs);
 
 #ifdef CONFIG_TRACER_SNAPSHOT
-void tracing_snapshot_instance(struct trace_array *tr)
+void tracing_snapshot_instance_cond(struct trace_array *tr, void *cond_data)
 {
        struct tracer *tracer = tr->current_trace;
        unsigned long flags;
@@ -920,10 +920,15 @@ void tracing_snapshot_instance(struct trace_array *tr)
        }
 
        local_irq_save(flags);
-       update_max_tr(tr, current, smp_processor_id());
+       update_max_tr(tr, current, smp_processor_id(), cond_data);
        local_irq_restore(flags);
 }
 
+void tracing_snapshot_instance(struct trace_array *tr)
+{
+       tracing_snapshot_instance_cond(tr, NULL);
+}
+
 /**
  * tracing_snapshot - take a snapshot of the current buffer.
  *
@@ -946,6 +951,54 @@ void tracing_snapshot(void)
 }
 EXPORT_SYMBOL_GPL(tracing_snapshot);
 
+/**
+ * tracing_snapshot_cond - conditionally take a snapshot of the current buffer.
+ * @tr:                The tracing instance to snapshot
+ * @cond_data: The data to be tested conditionally, and possibly saved
+ *
+ * This is the same as tracing_snapshot() except that the snapshot is
+ * conditional - the snapshot will only happen if the
+ * cond_snapshot.update() implementation receiving the cond_data
+ * returns true, which means that the trace array's cond_snapshot
+ * update() operation used the cond_data to determine whether the
+ * snapshot should be taken, and if it was, presumably saved it along
+ * with the snapshot.
+ */
+void tracing_snapshot_cond(struct trace_array *tr, void *cond_data)
+{
+       tracing_snapshot_instance_cond(tr, cond_data);
+}
+EXPORT_SYMBOL_GPL(tracing_snapshot_cond);
+
+/**
+ * tracing_snapshot_cond_data - get the user data associated with a snapshot
+ * @tr:                The tracing instance
+ *
+ * When the user enables a conditional snapshot using
+ * tracing_snapshot_cond_enable(), the user-defined cond_data is saved
+ * with the snapshot.  This accessor is used to retrieve it.
+ *
+ * Should not be called from cond_snapshot.update(), since it takes
+ * the tr->max_lock lock, which the code calling
+ * cond_snapshot.update() has already done.
+ *
+ * Returns the cond_data associated with the trace array's snapshot.
+ */
+void *tracing_cond_snapshot_data(struct trace_array *tr)
+{
+       void *cond_data = NULL;
+
+       arch_spin_lock(&tr->max_lock);
+
+       if (tr->cond_snapshot)
+               cond_data = tr->cond_snapshot->cond_data;
+
+       arch_spin_unlock(&tr->max_lock);
+
+       return cond_data;
+}
+EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data);
+
 static int resize_buffer_duplicate_size(struct trace_buffer *trace_buf,
                                        struct trace_buffer *size_buf, int cpu_id);
 static void set_buffer_entries(struct trace_buffer *buf, unsigned long val);
@@ -1025,12 +1078,111 @@ void tracing_snapshot_alloc(void)
        tracing_snapshot();
 }
 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
+
+/**
+ * tracing_snapshot_cond_enable - enable conditional snapshot for an instance
+ * @tr:                The tracing instance
+ * @cond_data: User data to associate with the snapshot
+ * @update:    Implementation of the cond_snapshot update function
+ *
+ * Check whether the conditional snapshot for the given instance has
+ * already been enabled, or if the current tracer is already using a
+ * snapshot; if so, return -EBUSY, else create a cond_snapshot and
+ * save the cond_data and update function inside.
+ *
+ * Returns 0 if successful, error otherwise.
+ */
+int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data,
+                                cond_update_fn_t update)
+{
+       struct cond_snapshot *cond_snapshot;
+       int ret = 0;
+
+       cond_snapshot = kzalloc(sizeof(*cond_snapshot), GFP_KERNEL);
+       if (!cond_snapshot)
+               return -ENOMEM;
+
+       cond_snapshot->cond_data = cond_data;
+       cond_snapshot->update = update;
+
+       mutex_lock(&trace_types_lock);
+
+       ret = tracing_alloc_snapshot_instance(tr);
+       if (ret)
+               goto fail_unlock;
+
+       if (tr->current_trace->use_max_tr) {
+               ret = -EBUSY;
+               goto fail_unlock;
+       }
+
+       /*
+        * The cond_snapshot can only change to NULL without the
+        * trace_types_lock. We don't care if we race with it going
+        * to NULL, but we want to make sure that it's not set to
+        * something other than NULL when we get here, which we can
+        * do safely with only holding the trace_types_lock and not
+        * having to take the max_lock.
+        */
+       if (tr->cond_snapshot) {
+               ret = -EBUSY;
+               goto fail_unlock;
+       }
+
+       arch_spin_lock(&tr->max_lock);
+       tr->cond_snapshot = cond_snapshot;
+       arch_spin_unlock(&tr->max_lock);
+
+       mutex_unlock(&trace_types_lock);
+
+       return ret;
+
+ fail_unlock:
+       mutex_unlock(&trace_types_lock);
+       kfree(cond_snapshot);
+       return ret;
+}
+EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable);
+
+/**
+ * tracing_snapshot_cond_disable - disable conditional snapshot for an instance
+ * @tr:                The tracing instance
+ *
+ * Check whether the conditional snapshot for the given instance is
+ * enabled; if so, free the cond_snapshot associated with it,
+ * otherwise return -EINVAL.
+ *
+ * Returns 0 if successful, error otherwise.
+ */
+int tracing_snapshot_cond_disable(struct trace_array *tr)
+{
+       int ret = 0;
+
+       arch_spin_lock(&tr->max_lock);
+
+       if (!tr->cond_snapshot)
+               ret = -EINVAL;
+       else {
+               kfree(tr->cond_snapshot);
+               tr->cond_snapshot = NULL;
+       }
+
+       arch_spin_unlock(&tr->max_lock);
+
+       return ret;
+}
+EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable);
 #else
 void tracing_snapshot(void)
 {
        WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
 }
 EXPORT_SYMBOL_GPL(tracing_snapshot);
+void tracing_snapshot_cond(struct trace_array *tr, void *cond_data)
+{
+       WARN_ONCE(1, "Snapshot feature not enabled, but internal conditional snapshot used");
+}
+EXPORT_SYMBOL_GPL(tracing_snapshot_cond);
 int tracing_alloc_snapshot(void)
 {
        WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used");
@@ -1043,6 +1195,21 @@ void tracing_snapshot_alloc(void)
        tracing_snapshot();
 }
 EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
+void *tracing_cond_snapshot_data(struct trace_array *tr)
+{
+       return NULL;
+}
+EXPORT_SYMBOL_GPL(tracing_cond_snapshot_data);
+int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data, cond_update_fn_t update)
+{
+       return -ENODEV;
+}
+EXPORT_SYMBOL_GPL(tracing_snapshot_cond_enable);
+int tracing_snapshot_cond_disable(struct trace_array *tr)
+{
+       return false;
+}
+EXPORT_SYMBOL_GPL(tracing_snapshot_cond_disable);
 #endif /* CONFIG_TRACER_SNAPSHOT */
 
 void tracer_tracing_off(struct trace_array *tr)
@@ -1330,7 +1497,7 @@ __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
        max_data->critical_start = data->critical_start;
        max_data->critical_end = data->critical_end;
 
-       memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
+       strncpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
        max_data->pid = tsk->pid;
        /*
         * If tsk == current, then use current_uid(), as that does not use
@@ -1354,12 +1521,14 @@ __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  * @tr: tracer
  * @tsk: the task with the latency
  * @cpu: The cpu that initiated the trace.
+ * @cond_data: User data associated with a conditional snapshot
  *
  * Flip the buffers between the @tr and the max_tr and record information
  * about which task was the cause of this latency.
  */
 void
-update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
+update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu,
+             void *cond_data)
 {
        if (tr->stop_count)
                return;
@@ -1380,9 +1549,15 @@ update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
        else
                ring_buffer_record_off(tr->max_buffer.buffer);
 
+#ifdef CONFIG_TRACER_SNAPSHOT
+       if (tr->cond_snapshot && !tr->cond_snapshot->update(tr, cond_data))
+               goto out_unlock;
+#endif
        swap(tr->trace_buffer.buffer, tr->max_buffer.buffer);
 
        __update_max_tr(tr, tsk, cpu);
+
+ out_unlock:
        arch_spin_unlock(&tr->max_lock);
 }
 
@@ -1431,7 +1606,7 @@ update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
 }
 #endif /* CONFIG_TRACER_MAX_TRACE */
 
-static int wait_on_pipe(struct trace_iterator *iter, bool full)
+static int wait_on_pipe(struct trace_iterator *iter, int full)
 {
        /* Iterators are static, they should be filled or empty */
        if (trace_buffer_iter(iter, iter->cpu_file))
@@ -1681,7 +1856,7 @@ void tracing_reset(struct trace_buffer *buf, int cpu)
        ring_buffer_record_disable(buffer);
 
        /* Make sure all commits have finished */
-       synchronize_sched();
+       synchronize_rcu();
        ring_buffer_reset_cpu(buffer, cpu);
 
        ring_buffer_record_enable(buffer);
@@ -1698,7 +1873,7 @@ void tracing_reset_online_cpus(struct trace_buffer *buf)
        ring_buffer_record_disable(buffer);
 
        /* Make sure all commits have finished */
-       synchronize_sched();
+       synchronize_rcu();
 
        buf->time_start = buffer_ftrace_now(buf, buf->cpu);
 
@@ -1748,7 +1923,7 @@ static inline char *get_saved_cmdlines(int idx)
 
 static inline void set_cmdline(int idx, const char *cmdline)
 {
-       memcpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN);
+       strncpy(get_saved_cmdlines(idx), cmdline, TASK_COMM_LEN);
 }
 
 static int allocate_cmdlines_buffer(unsigned int val,
@@ -2250,7 +2425,7 @@ void trace_buffered_event_disable(void)
        preempt_enable();
 
        /* Wait for all current users to finish */
-       synchronize_sched();
+       synchronize_rcu();
 
        for_each_tracing_cpu(cpu) {
                free_page((unsigned long)per_cpu(trace_buffered_event, cpu));
@@ -2452,7 +2627,7 @@ static inline void ftrace_exports_disable(void)
        static_branch_disable(&ftrace_exports_enabled);
 }
 
-void ftrace_exports(struct ring_buffer_event *event)
+static void ftrace_exports(struct ring_buffer_event *event)
 {
        struct trace_export *export;
 
@@ -4408,13 +4583,15 @@ static int trace_set_options(struct trace_array *tr, char *option)
        int neg = 0;
        int ret;
        size_t orig_len = strlen(option);
+       int len;
 
        cmp = strstrip(option);
 
-       if (strncmp(cmp, "no", 2) == 0) {
+       len = str_has_prefix(cmp, "no");
+       if (len)
                neg = 1;
-               cmp += 2;
-       }
+
+       cmp += len;
 
        mutex_lock(&trace_types_lock);
 
@@ -4604,6 +4781,10 @@ static const char readme_msg[] =
        "\t\t\t  traces\n"
 #endif
 #endif /* CONFIG_STACK_TRACER */
+#ifdef CONFIG_DYNAMIC_EVENTS
+       "  dynamic_events\t\t- Add/remove/show the generic dynamic events\n"
+       "\t\t\t  Write into this file to define/undefine new trace events.\n"
+#endif
 #ifdef CONFIG_KPROBE_EVENTS
        "  kprobe_events\t\t- Add/remove/show the kernel dynamic events\n"
        "\t\t\t  Write into this file to define/undefine new trace events.\n"
@@ -4616,6 +4797,9 @@ static const char readme_msg[] =
        "\t  accepts: event-definitions (one definition per line)\n"
        "\t   Format: p[:[<group>/]<event>] <place> [<args>]\n"
        "\t           r[maxactive][:[<group>/]<event>] <place> [<args>]\n"
+#ifdef CONFIG_HIST_TRIGGERS
+       "\t           s:[synthetic/]<event> <field> [<field>]\n"
+#endif
        "\t           -:[<group>/]<event>\n"
 #ifdef CONFIG_KPROBE_EVENTS
        "\t    place: [<module>:]<symbol>[+<offset>]|<memaddr>\n"
@@ -4634,6 +4818,11 @@ static const char readme_msg[] =
        "\t     type: s8/16/32/64, u8/16/32/64, x8/16/32/64, string, symbol,\n"
        "\t           b<bit-width>@<bit-offset>/<container-size>,\n"
        "\t           <type>\\[<array-size>\\]\n"
+#ifdef CONFIG_HIST_TRIGGERS
+       "\t    field: <stype> <name>;\n"
+       "\t    stype: u8/u16/u32/u64, s8/s16/s32/s64, pid_t,\n"
+       "\t           [unsigned] char/int/long\n"
+#endif
 #endif
        "  events/\t\t- Directory containing all trace event subsystems:\n"
        "      enable\t\t- Write 0/1 to enable/disable tracing of all events\n"
@@ -4686,6 +4875,7 @@ static const char readme_msg[] =
        "\t            [:size=#entries]\n"
        "\t            [:pause][:continue][:clear]\n"
        "\t            [:name=histname1]\n"
+       "\t            [:<handler>.<action>]\n"
        "\t            [if <filter>]\n\n"
        "\t    When a matching event is hit, an entry is added to a hash\n"
        "\t    table using the key(s) and value(s) named, and the value of a\n"
@@ -4726,8 +4916,21 @@ static const char readme_msg[] =
        "\t    unchanged.\n\n"
        "\t    The enable_hist and disable_hist triggers can be used to\n"
        "\t    have one event conditionally start and stop another event's\n"
-       "\t    already-attached hist trigger.  The syntax is analagous to\n"
-       "\t    the enable_event and disable_event triggers.\n"
+       "\t    already-attached hist trigger.  The syntax is analogous to\n"
+       "\t    the enable_event and disable_event triggers.\n\n"
+       "\t    Hist trigger handlers and actions are executed whenever a\n"
+       "\t    a histogram entry is added or updated.  They take the form:\n\n"
+       "\t        <handler>.<action>\n\n"
+       "\t    The available handlers are:\n\n"
+       "\t        onmatch(matching.event)  - invoke on addition or update\n"
+       "\t        onmax(var)               - invoke if var exceeds current max\n"
+       "\t        onchange(var)            - invoke action if var changes\n\n"
+       "\t    The available actions are:\n\n"
+       "\t        trace(<synthetic_event>,param list)  - generate synthetic event\n"
+       "\t        save(field,...)                      - save current event fields\n"
+#ifdef CONFIG_TRACER_SNAPSHOT
+       "\t        snapshot()                           - snapshot the trace buffer\n"
+#endif
 #endif
 ;
 
@@ -5372,6 +5575,16 @@ static int tracing_set_tracer(struct trace_array *tr, const char *buf)
        if (t == tr->current_trace)
                goto out;
 
+#ifdef CONFIG_TRACER_SNAPSHOT
+       if (t->use_max_tr) {
+               arch_spin_lock(&tr->max_lock);
+               if (tr->cond_snapshot)
+                       ret = -EBUSY;
+               arch_spin_unlock(&tr->max_lock);
+               if (ret)
+                       goto out;
+       }
+#endif
        /* Some tracers won't work on kernel command line */
        if (system_state < SYSTEM_RUNNING && t->noboot) {
                pr_warn("Tracer '%s' is not allowed on command line, ignored\n",
@@ -5398,7 +5611,7 @@ static int tracing_set_tracer(struct trace_array *tr, const char *buf)
        if (tr->current_trace->reset)
                tr->current_trace->reset(tr);
 
-       /* Current trace needs to be nop_trace before synchronize_sched */
+       /* Current trace needs to be nop_trace before synchronize_rcu */
        tr->current_trace = &nop_trace;
 
 #ifdef CONFIG_TRACER_MAX_TRACE
@@ -5412,7 +5625,7 @@ static int tracing_set_tracer(struct trace_array *tr, const char *buf)
                 * The update_max_tr is called from interrupts disabled
                 * so a synchronized_sched() is sufficient.
                 */
-               synchronize_sched();
+               synchronize_rcu();
                free_snapshot(tr);
        }
 #endif
@@ -5610,7 +5823,6 @@ static int tracing_open_pipe(struct inode *inode, struct file *filp)
        return ret;
 
 fail:
-       kfree(iter->trace);
        kfree(iter);
        __trace_array_put(tr);
        mutex_unlock(&trace_types_lock);
@@ -5693,7 +5905,7 @@ static int tracing_wait_pipe(struct file *filp)
 
                mutex_unlock(&iter->mutex);
 
-               ret = wait_on_pipe(iter, false);
+               ret = wait_on_pipe(iter, 0);
 
                mutex_lock(&iter->mutex);
 
@@ -6454,6 +6666,13 @@ tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
                goto out;
        }
 
+       arch_spin_lock(&tr->max_lock);
+       if (tr->cond_snapshot)
+               ret = -EBUSY;
+       arch_spin_unlock(&tr->max_lock);
+       if (ret)
+               goto out;
+
        switch (val) {
        case 0:
                if (iter->cpu_file != RING_BUFFER_ALL_CPUS) {
@@ -6479,7 +6698,7 @@ tracing_snapshot_write(struct file *filp, const char __user *ubuf, size_t cnt,
                local_irq_disable();
                /* Now, we're going to swap */
                if (iter->cpu_file == RING_BUFFER_ALL_CPUS)
-                       update_max_tr(tr, current, smp_processor_id());
+                       update_max_tr(tr, current, smp_processor_id(), NULL);
                else
                        update_max_tr_single(tr, current, iter->cpu_file);
                local_irq_enable();
@@ -6751,7 +6970,7 @@ tracing_buffers_read(struct file *filp, char __user *ubuf,
                        if ((filp->f_flags & O_NONBLOCK))
                                return -EAGAIN;
 
-                       ret = wait_on_pipe(iter, false);
+                       ret = wait_on_pipe(iter, 0);
                        if (ret)
                                return ret;
 
@@ -6948,7 +7167,7 @@ tracing_buffers_splice_read(struct file *file, loff_t *ppos,
                if ((file->f_flags & O_NONBLOCK) || (flags & SPLICE_F_NONBLOCK))
                        goto out;
 
-               ret = wait_on_pipe(iter, true);
+               ret = wait_on_pipe(iter, iter->tr->buffer_percent);
                if (ret)
                        goto out;
 
@@ -7662,6 +7881,53 @@ static const struct file_operations rb_simple_fops = {
        .llseek         = default_llseek,
 };
 
+static ssize_t
+buffer_percent_read(struct file *filp, char __user *ubuf,
+                   size_t cnt, loff_t *ppos)
+{
+       struct trace_array *tr = filp->private_data;
+       char buf[64];
+       int r;
+
+       r = tr->buffer_percent;
+       r = sprintf(buf, "%d\n", r);
+
+       return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
+}
+
+static ssize_t
+buffer_percent_write(struct file *filp, const char __user *ubuf,
+                    size_t cnt, loff_t *ppos)
+{
+       struct trace_array *tr = filp->private_data;
+       unsigned long val;
+       int ret;
+
+       ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
+       if (ret)
+               return ret;
+
+       if (val > 100)
+               return -EINVAL;
+
+       if (!val)
+               val = 1;
+
+       tr->buffer_percent = val;
+
+       (*ppos)++;
+
+       return cnt;
+}
+
+static const struct file_operations buffer_percent_fops = {
+       .open           = tracing_open_generic_tr,
+       .read           = buffer_percent_read,
+       .write          = buffer_percent_write,
+       .release        = tracing_release_generic_tr,
+       .llseek         = default_llseek,
+};
+
 struct dentry *trace_instance_dir;
 
 static void
@@ -7970,6 +8236,11 @@ init_tracer_tracefs(struct trace_array *tr, struct dentry *d_tracer)
        trace_create_file("timestamp_mode", 0444, d_tracer, tr,
                          &trace_time_stamp_mode_fops);
 
+       tr->buffer_percent = 50;
+
+       trace_create_file("buffer_percent", 0444, d_tracer,
+                       tr, &buffer_percent_fops);
+
        create_trace_options_dir(tr);
 
 #if defined(CONFIG_TRACER_MAX_TRACE) || defined(CONFIG_HWLAT_TRACER)