Merge tag 'trace-v5.1' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux...
[sfrench/cifs-2.6.git] / kernel / trace / trace.c
index c4238b441624415cfd6113bb36ea5d71b6eaaa54..d7325eb1bc83585fb0b0cac0827ab94d1c7f1735 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);
 }
 
@@ -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,
@@ -4702,6 +4877,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"
@@ -4742,8 +4918,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
 ;
 
@@ -5388,6 +5577,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",
@@ -5626,7 +5825,6 @@ out:
        return ret;
 
 fail:
-       kfree(iter->trace);
        kfree(iter);
        __trace_array_put(tr);
        mutex_unlock(&trace_types_lock);
@@ -6470,6 +6668,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) {
@@ -6495,7 +6700,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();