LCOV - code coverage report
Current view: top level - kernel/trace - trace_events.c (source / functions) Hit Total Coverage
Test: landlock.info Lines: 320 1386 23.1 %
Date: 2021-04-22 12:43:58 Functions: 27 121 22.3 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0
       2             : /*
       3             :  * event tracer
       4             :  *
       5             :  * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
       6             :  *
       7             :  *  - Added format output of fields of the trace point.
       8             :  *    This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
       9             :  *
      10             :  */
      11             : 
      12             : #define pr_fmt(fmt) fmt
      13             : 
      14             : #include <linux/workqueue.h>
      15             : #include <linux/security.h>
      16             : #include <linux/spinlock.h>
      17             : #include <linux/kthread.h>
      18             : #include <linux/tracefs.h>
      19             : #include <linux/uaccess.h>
      20             : #include <linux/module.h>
      21             : #include <linux/ctype.h>
      22             : #include <linux/sort.h>
      23             : #include <linux/slab.h>
      24             : #include <linux/delay.h>
      25             : 
      26             : #include <trace/events/sched.h>
      27             : #include <trace/syscall.h>
      28             : 
      29             : #include <asm/setup.h>
      30             : 
      31             : #include "trace_output.h"
      32             : 
      33             : #undef TRACE_SYSTEM
      34             : #define TRACE_SYSTEM "TRACE_SYSTEM"
      35             : 
      36             : DEFINE_MUTEX(event_mutex);
      37             : 
      38             : LIST_HEAD(ftrace_events);
      39             : static LIST_HEAD(ftrace_generic_fields);
      40             : static LIST_HEAD(ftrace_common_fields);
      41             : static bool eventdir_initialized;
      42             : 
      43             : #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
      44             : 
      45             : static struct kmem_cache *field_cachep;
      46             : static struct kmem_cache *file_cachep;
      47             : 
      48           0 : static inline int system_refcount(struct event_subsystem *system)
      49             : {
      50           0 :         return system->ref_count;
      51             : }
      52             : 
      53           0 : static int system_refcount_inc(struct event_subsystem *system)
      54             : {
      55           0 :         return system->ref_count++;
      56             : }
      57             : 
      58           0 : static int system_refcount_dec(struct event_subsystem *system)
      59             : {
      60           0 :         return --system->ref_count;
      61             : }
      62             : 
      63             : /* Double loops, do not use break, only goto's work */
      64             : #define do_for_each_event_file(tr, file)                        \
      65             :         list_for_each_entry(tr, &ftrace_trace_arrays, list) {       \
      66             :                 list_for_each_entry(file, &tr->events, list)
      67             : 
      68             : #define do_for_each_event_file_safe(tr, file)                   \
      69             :         list_for_each_entry(tr, &ftrace_trace_arrays, list) {       \
      70             :                 struct trace_event_file *___n;                          \
      71             :                 list_for_each_entry_safe(file, ___n, &tr->events, list)
      72             : 
      73             : #define while_for_each_event_file()             \
      74             :         }
      75             : 
      76             : static struct ftrace_event_field *
      77           0 : __find_event_field(struct list_head *head, char *name)
      78             : {
      79           0 :         struct ftrace_event_field *field;
      80             : 
      81           0 :         list_for_each_entry(field, head, link) {
      82           0 :                 if (!strcmp(field->name, name))
      83           0 :                         return field;
      84             :         }
      85             : 
      86             :         return NULL;
      87             : }
      88             : 
      89             : struct ftrace_event_field *
      90           0 : trace_find_event_field(struct trace_event_call *call, char *name)
      91             : {
      92           0 :         struct ftrace_event_field *field;
      93           0 :         struct list_head *head;
      94             : 
      95           0 :         head = trace_get_fields(call);
      96           0 :         field = __find_event_field(head, name);
      97           0 :         if (field)
      98             :                 return field;
      99             : 
     100           0 :         field = __find_event_field(&ftrace_generic_fields, name);
     101           0 :         if (field)
     102             :                 return field;
     103             : 
     104           0 :         return __find_event_field(&ftrace_common_fields, name);
     105             : }
     106             : 
     107        1686 : static int __trace_define_field(struct list_head *head, const char *type,
     108             :                                 const char *name, int offset, int size,
     109             :                                 int is_signed, int filter_type)
     110             : {
     111        1686 :         struct ftrace_event_field *field;
     112             : 
     113        1686 :         field = kmem_cache_alloc(field_cachep, GFP_TRACE);
     114        1686 :         if (!field)
     115             :                 return -ENOMEM;
     116             : 
     117        1686 :         field->name = name;
     118        1686 :         field->type = type;
     119             : 
     120        1686 :         if (filter_type == FILTER_OTHER)
     121        1680 :                 field->filter_type = filter_assign_type(type);
     122             :         else
     123           6 :                 field->filter_type = filter_type;
     124             : 
     125        1686 :         field->offset = offset;
     126        1686 :         field->size = size;
     127        1686 :         field->is_signed = is_signed;
     128             : 
     129        1686 :         list_add(&field->link, head);
     130             : 
     131        1686 :         return 0;
     132             : }
     133             : 
     134        1678 : int trace_define_field(struct trace_event_call *call, const char *type,
     135             :                        const char *name, int offset, int size, int is_signed,
     136             :                        int filter_type)
     137             : {
     138        1678 :         struct list_head *head;
     139             : 
     140        1678 :         if (WARN_ON(!call->class))
     141             :                 return 0;
     142             : 
     143        1678 :         head = trace_get_fields(call);
     144        1678 :         return __trace_define_field(head, type, name, offset, size,
     145             :                                     is_signed, filter_type);
     146             : }
     147             : EXPORT_SYMBOL_GPL(trace_define_field);
     148             : 
     149             : #define __generic_field(type, item, filter_type)                        \
     150             :         ret = __trace_define_field(&ftrace_generic_fields, #type,   \
     151             :                                    #item, 0, 0, is_signed_type(type),   \
     152             :                                    filter_type);                        \
     153             :         if (ret)                                                        \
     154             :                 return ret;
     155             : 
     156             : #define __common_field(type, item)                                      \
     157             :         ret = __trace_define_field(&ftrace_common_fields, #type,    \
     158             :                                    "common_" #item,                   \
     159             :                                    offsetof(typeof(ent), item),         \
     160             :                                    sizeof(ent.item),                    \
     161             :                                    is_signed_type(type), FILTER_OTHER); \
     162             :         if (ret)                                                        \
     163             :                 return ret;
     164             : 
     165           1 : static int trace_define_generic_fields(void)
     166             : {
     167           1 :         int ret;
     168             : 
     169           1 :         __generic_field(int, CPU, FILTER_CPU);
     170           1 :         __generic_field(int, cpu, FILTER_CPU);
     171           1 :         __generic_field(char *, COMM, FILTER_COMM);
     172           1 :         __generic_field(char *, comm, FILTER_COMM);
     173             : 
     174             :         return ret;
     175             : }
     176             : 
     177           1 : static int trace_define_common_fields(void)
     178             : {
     179           1 :         int ret;
     180           1 :         struct trace_entry ent;
     181             : 
     182           1 :         __common_field(unsigned short, type);
     183           1 :         __common_field(unsigned char, flags);
     184           1 :         __common_field(unsigned char, preempt_count);
     185           1 :         __common_field(int, pid);
     186             : 
     187             :         return ret;
     188             : }
     189             : 
     190           0 : static void trace_destroy_fields(struct trace_event_call *call)
     191             : {
     192           0 :         struct ftrace_event_field *field, *next;
     193           0 :         struct list_head *head;
     194             : 
     195           0 :         head = trace_get_fields(call);
     196           0 :         list_for_each_entry_safe(field, next, head, link) {
     197           0 :                 list_del(&field->link);
     198           0 :                 kmem_cache_free(field_cachep, field);
     199             :         }
     200           0 : }
     201             : 
     202             : /*
     203             :  * run-time version of trace_event_get_offsets_<call>() that returns the last
     204             :  * accessible offset of trace fields excluding __dynamic_array bytes
     205             :  */
     206           0 : int trace_event_get_offsets(struct trace_event_call *call)
     207             : {
     208           0 :         struct ftrace_event_field *tail;
     209           0 :         struct list_head *head;
     210             : 
     211           0 :         head = trace_get_fields(call);
     212             :         /*
     213             :          * head->next points to the last field with the largest offset,
     214             :          * since it was added last by trace_define_field()
     215             :          */
     216           0 :         tail = list_first_entry(head, struct ftrace_event_field, link);
     217           0 :         return tail->offset + tail->size;
     218             : }
     219             : 
     220         527 : int trace_event_raw_init(struct trace_event_call *call)
     221             : {
     222         527 :         int id;
     223             : 
     224         527 :         id = register_trace_event(&call->event);
     225         527 :         if (!id)
     226           0 :                 return -ENODEV;
     227             : 
     228             :         return 0;
     229             : }
     230             : EXPORT_SYMBOL_GPL(trace_event_raw_init);
     231             : 
     232           0 : bool trace_event_ignore_this_pid(struct trace_event_file *trace_file)
     233             : {
     234           0 :         struct trace_array *tr = trace_file->tr;
     235           0 :         struct trace_array_cpu *data;
     236           0 :         struct trace_pid_list *no_pid_list;
     237           0 :         struct trace_pid_list *pid_list;
     238             : 
     239           0 :         pid_list = rcu_dereference_raw(tr->filtered_pids);
     240           0 :         no_pid_list = rcu_dereference_raw(tr->filtered_no_pids);
     241             : 
     242           0 :         if (!pid_list && !no_pid_list)
     243             :                 return false;
     244             : 
     245           0 :         data = this_cpu_ptr(tr->array_buffer.data);
     246             : 
     247           0 :         return data->ignore_pid;
     248             : }
     249             : EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid);
     250             : 
     251           0 : void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
     252             :                                  struct trace_event_file *trace_file,
     253             :                                  unsigned long len)
     254             : {
     255           0 :         struct trace_event_call *event_call = trace_file->event_call;
     256             : 
     257           0 :         if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) &&
     258           0 :             trace_event_ignore_this_pid(trace_file))
     259             :                 return NULL;
     260             : 
     261             :         /*
     262             :          * If CONFIG_PREEMPTION is enabled, then the tracepoint itself disables
     263             :          * preemption (adding one to the preempt_count). Since we are
     264             :          * interested in the preempt_count at the time the tracepoint was
     265             :          * hit, we need to subtract one to offset the increment.
     266             :          */
     267           0 :         fbuffer->trace_ctx = tracing_gen_ctx_dec();
     268           0 :         fbuffer->trace_file = trace_file;
     269             : 
     270           0 :         fbuffer->event =
     271           0 :                 trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
     272             :                                                 event_call->event.type, len,
     273             :                                                 fbuffer->trace_ctx);
     274           0 :         if (!fbuffer->event)
     275             :                 return NULL;
     276             : 
     277           0 :         fbuffer->regs = NULL;
     278           0 :         fbuffer->entry = ring_buffer_event_data(fbuffer->event);
     279           0 :         return fbuffer->entry;
     280             : }
     281             : EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
     282             : 
     283           0 : int trace_event_reg(struct trace_event_call *call,
     284             :                     enum trace_reg type, void *data)
     285             : {
     286           0 :         struct trace_event_file *file = data;
     287             : 
     288           0 :         WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
     289           0 :         switch (type) {
     290           0 :         case TRACE_REG_REGISTER:
     291           0 :                 return tracepoint_probe_register(call->tp,
     292           0 :                                                  call->class->probe,
     293             :                                                  file);
     294           0 :         case TRACE_REG_UNREGISTER:
     295           0 :                 tracepoint_probe_unregister(call->tp,
     296           0 :                                             call->class->probe,
     297             :                                             file);
     298           0 :                 return 0;
     299             : 
     300             : #ifdef CONFIG_PERF_EVENTS
     301           0 :         case TRACE_REG_PERF_REGISTER:
     302           0 :                 return tracepoint_probe_register(call->tp,
     303           0 :                                                  call->class->perf_probe,
     304             :                                                  call);
     305           0 :         case TRACE_REG_PERF_UNREGISTER:
     306           0 :                 tracepoint_probe_unregister(call->tp,
     307           0 :                                             call->class->perf_probe,
     308             :                                             call);
     309           0 :                 return 0;
     310             :         case TRACE_REG_PERF_OPEN:
     311             :         case TRACE_REG_PERF_CLOSE:
     312             :         case TRACE_REG_PERF_ADD:
     313             :         case TRACE_REG_PERF_DEL:
     314             :                 return 0;
     315             : #endif
     316             :         }
     317             :         return 0;
     318             : }
     319             : EXPORT_SYMBOL_GPL(trace_event_reg);
     320             : 
     321           0 : void trace_event_enable_cmd_record(bool enable)
     322             : {
     323           0 :         struct trace_event_file *file;
     324           0 :         struct trace_array *tr;
     325             : 
     326           0 :         lockdep_assert_held(&event_mutex);
     327             : 
     328           0 :         do_for_each_event_file(tr, file) {
     329             : 
     330           0 :                 if (!(file->flags & EVENT_FILE_FL_ENABLED))
     331           0 :                         continue;
     332             : 
     333           0 :                 if (enable) {
     334           0 :                         tracing_start_cmdline_record();
     335           0 :                         set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
     336             :                 } else {
     337           0 :                         tracing_stop_cmdline_record();
     338           0 :                         clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
     339             :                 }
     340           0 :         } while_for_each_event_file();
     341           0 : }
     342             : 
     343           0 : void trace_event_enable_tgid_record(bool enable)
     344             : {
     345           0 :         struct trace_event_file *file;
     346           0 :         struct trace_array *tr;
     347             : 
     348           0 :         lockdep_assert_held(&event_mutex);
     349             : 
     350           0 :         do_for_each_event_file(tr, file) {
     351           0 :                 if (!(file->flags & EVENT_FILE_FL_ENABLED))
     352           0 :                         continue;
     353             : 
     354           0 :                 if (enable) {
     355           0 :                         tracing_start_tgid_record();
     356           0 :                         set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
     357             :                 } else {
     358           0 :                         tracing_stop_tgid_record();
     359           0 :                         clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT,
     360           0 :                                   &file->flags);
     361             :                 }
     362           0 :         } while_for_each_event_file();
     363           0 : }
     364             : 
     365           0 : static int __ftrace_event_enable_disable(struct trace_event_file *file,
     366             :                                          int enable, int soft_disable)
     367             : {
     368           0 :         struct trace_event_call *call = file->event_call;
     369           0 :         struct trace_array *tr = file->tr;
     370           0 :         unsigned long file_flags = file->flags;
     371           0 :         int ret = 0;
     372           0 :         int disable;
     373             : 
     374           0 :         switch (enable) {
     375           0 :         case 0:
     376             :                 /*
     377             :                  * When soft_disable is set and enable is cleared, the sm_ref
     378             :                  * reference counter is decremented. If it reaches 0, we want
     379             :                  * to clear the SOFT_DISABLED flag but leave the event in the
     380             :                  * state that it was. That is, if the event was enabled and
     381             :                  * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
     382             :                  * is set we do not want the event to be enabled before we
     383             :                  * clear the bit.
     384             :                  *
     385             :                  * When soft_disable is not set but the SOFT_MODE flag is,
     386             :                  * we do nothing. Do not disable the tracepoint, otherwise
     387             :                  * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
     388             :                  */
     389           0 :                 if (soft_disable) {
     390           0 :                         if (atomic_dec_return(&file->sm_ref) > 0)
     391             :                                 break;
     392           0 :                         disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
     393           0 :                         clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
     394             :                 } else
     395           0 :                         disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
     396             : 
     397           0 :                 if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
     398           0 :                         clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
     399           0 :                         if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
     400           0 :                                 tracing_stop_cmdline_record();
     401           0 :                                 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
     402             :                         }
     403             : 
     404           0 :                         if (file->flags & EVENT_FILE_FL_RECORDED_TGID) {
     405           0 :                                 tracing_stop_tgid_record();
     406           0 :                                 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
     407             :                         }
     408             : 
     409           0 :                         call->class->reg(call, TRACE_REG_UNREGISTER, file);
     410             :                 }
     411             :                 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
     412           0 :                 if (file->flags & EVENT_FILE_FL_SOFT_MODE)
     413           0 :                         set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
     414             :                 else
     415           0 :                         clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
     416             :                 break;
     417           0 :         case 1:
     418             :                 /*
     419             :                  * When soft_disable is set and enable is set, we want to
     420             :                  * register the tracepoint for the event, but leave the event
     421             :                  * as is. That means, if the event was already enabled, we do
     422             :                  * nothing (but set SOFT_MODE). If the event is disabled, we
     423             :                  * set SOFT_DISABLED before enabling the event tracepoint, so
     424             :                  * it still seems to be disabled.
     425             :                  */
     426           0 :                 if (!soft_disable)
     427           0 :                         clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
     428             :                 else {
     429           0 :                         if (atomic_inc_return(&file->sm_ref) > 1)
     430             :                                 break;
     431           0 :                         set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
     432             :                 }
     433             : 
     434           0 :                 if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
     435           0 :                         bool cmd = false, tgid = false;
     436             : 
     437             :                         /* Keep the event disabled, when going to SOFT_MODE. */
     438           0 :                         if (soft_disable)
     439           0 :                                 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
     440             : 
     441           0 :                         if (tr->trace_flags & TRACE_ITER_RECORD_CMD) {
     442           0 :                                 cmd = true;
     443           0 :                                 tracing_start_cmdline_record();
     444           0 :                                 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
     445             :                         }
     446             : 
     447           0 :                         if (tr->trace_flags & TRACE_ITER_RECORD_TGID) {
     448           0 :                                 tgid = true;
     449           0 :                                 tracing_start_tgid_record();
     450           0 :                                 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
     451             :                         }
     452             : 
     453           0 :                         ret = call->class->reg(call, TRACE_REG_REGISTER, file);
     454           0 :                         if (ret) {
     455           0 :                                 if (cmd)
     456           0 :                                         tracing_stop_cmdline_record();
     457           0 :                                 if (tgid)
     458           0 :                                         tracing_stop_tgid_record();
     459           0 :                                 pr_info("event trace: Could not enable event "
     460             :                                         "%s\n", trace_event_name(call));
     461           0 :                                 break;
     462             :                         }
     463           0 :                         set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
     464             : 
     465             :                         /* WAS_ENABLED gets set but never cleared. */
     466           0 :                         set_bit(EVENT_FILE_FL_WAS_ENABLED_BIT, &file->flags);
     467             :                 }
     468             :                 break;
     469             :         }
     470             : 
     471             :         /* Enable or disable use of trace_buffered_event */
     472           0 :         if ((file_flags & EVENT_FILE_FL_SOFT_DISABLED) !=
     473           0 :             (file->flags & EVENT_FILE_FL_SOFT_DISABLED)) {
     474           0 :                 if (file->flags & EVENT_FILE_FL_SOFT_DISABLED)
     475           0 :                         trace_buffered_event_enable();
     476             :                 else
     477           0 :                         trace_buffered_event_disable();
     478             :         }
     479             : 
     480           0 :         return ret;
     481             : }
     482             : 
     483           0 : int trace_event_enable_disable(struct trace_event_file *file,
     484             :                                int enable, int soft_disable)
     485             : {
     486           0 :         return __ftrace_event_enable_disable(file, enable, soft_disable);
     487             : }
     488             : 
     489           0 : static int ftrace_event_enable_disable(struct trace_event_file *file,
     490             :                                        int enable)
     491             : {
     492           0 :         return __ftrace_event_enable_disable(file, enable, 0);
     493             : }
     494             : 
     495           0 : static void ftrace_clear_events(struct trace_array *tr)
     496             : {
     497           0 :         struct trace_event_file *file;
     498             : 
     499           0 :         mutex_lock(&event_mutex);
     500           0 :         list_for_each_entry(file, &tr->events, list) {
     501           0 :                 ftrace_event_enable_disable(file, 0);
     502             :         }
     503           0 :         mutex_unlock(&event_mutex);
     504           0 : }
     505             : 
     506             : static void
     507           0 : event_filter_pid_sched_process_exit(void *data, struct task_struct *task)
     508             : {
     509           0 :         struct trace_pid_list *pid_list;
     510           0 :         struct trace_array *tr = data;
     511             : 
     512           0 :         pid_list = rcu_dereference_raw(tr->filtered_pids);
     513           0 :         trace_filter_add_remove_task(pid_list, NULL, task);
     514             : 
     515           0 :         pid_list = rcu_dereference_raw(tr->filtered_no_pids);
     516           0 :         trace_filter_add_remove_task(pid_list, NULL, task);
     517           0 : }
     518             : 
     519             : static void
     520           0 : event_filter_pid_sched_process_fork(void *data,
     521             :                                     struct task_struct *self,
     522             :                                     struct task_struct *task)
     523             : {
     524           0 :         struct trace_pid_list *pid_list;
     525           0 :         struct trace_array *tr = data;
     526             : 
     527           0 :         pid_list = rcu_dereference_sched(tr->filtered_pids);
     528           0 :         trace_filter_add_remove_task(pid_list, self, task);
     529             : 
     530           0 :         pid_list = rcu_dereference_sched(tr->filtered_no_pids);
     531           0 :         trace_filter_add_remove_task(pid_list, self, task);
     532           0 : }
     533             : 
     534           0 : void trace_event_follow_fork(struct trace_array *tr, bool enable)
     535             : {
     536           0 :         if (enable) {
     537           0 :                 register_trace_prio_sched_process_fork(event_filter_pid_sched_process_fork,
     538             :                                                        tr, INT_MIN);
     539           0 :                 register_trace_prio_sched_process_free(event_filter_pid_sched_process_exit,
     540             :                                                        tr, INT_MAX);
     541             :         } else {
     542           0 :                 unregister_trace_sched_process_fork(event_filter_pid_sched_process_fork,
     543             :                                                     tr);
     544           0 :                 unregister_trace_sched_process_free(event_filter_pid_sched_process_exit,
     545             :                                                     tr);
     546             :         }
     547           0 : }
     548             : 
     549             : static void
     550           0 : event_filter_pid_sched_switch_probe_pre(void *data, bool preempt,
     551             :                     struct task_struct *prev, struct task_struct *next)
     552             : {
     553           0 :         struct trace_array *tr = data;
     554           0 :         struct trace_pid_list *no_pid_list;
     555           0 :         struct trace_pid_list *pid_list;
     556           0 :         bool ret;
     557             : 
     558           0 :         pid_list = rcu_dereference_sched(tr->filtered_pids);
     559           0 :         no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
     560             : 
     561             :         /*
     562             :          * Sched switch is funny, as we only want to ignore it
     563             :          * in the notrace case if both prev and next should be ignored.
     564             :          */
     565           0 :         ret = trace_ignore_this_task(NULL, no_pid_list, prev) &&
     566           0 :                 trace_ignore_this_task(NULL, no_pid_list, next);
     567             : 
     568           0 :         this_cpu_write(tr->array_buffer.data->ignore_pid, ret ||
     569             :                        (trace_ignore_this_task(pid_list, NULL, prev) &&
     570             :                         trace_ignore_this_task(pid_list, NULL, next)));
     571           0 : }
     572             : 
     573             : static void
     574           0 : event_filter_pid_sched_switch_probe_post(void *data, bool preempt,
     575             :                     struct task_struct *prev, struct task_struct *next)
     576             : {
     577           0 :         struct trace_array *tr = data;
     578           0 :         struct trace_pid_list *no_pid_list;
     579           0 :         struct trace_pid_list *pid_list;
     580             : 
     581           0 :         pid_list = rcu_dereference_sched(tr->filtered_pids);
     582           0 :         no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
     583             : 
     584           0 :         this_cpu_write(tr->array_buffer.data->ignore_pid,
     585             :                        trace_ignore_this_task(pid_list, no_pid_list, next));
     586           0 : }
     587             : 
     588             : static void
     589           0 : event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task)
     590             : {
     591           0 :         struct trace_array *tr = data;
     592           0 :         struct trace_pid_list *no_pid_list;
     593           0 :         struct trace_pid_list *pid_list;
     594             : 
     595             :         /* Nothing to do if we are already tracing */
     596           0 :         if (!this_cpu_read(tr->array_buffer.data->ignore_pid))
     597             :                 return;
     598             : 
     599           0 :         pid_list = rcu_dereference_sched(tr->filtered_pids);
     600           0 :         no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
     601             : 
     602           0 :         this_cpu_write(tr->array_buffer.data->ignore_pid,
     603             :                        trace_ignore_this_task(pid_list, no_pid_list, task));
     604             : }
     605             : 
     606             : static void
     607           0 : event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task)
     608             : {
     609           0 :         struct trace_array *tr = data;
     610           0 :         struct trace_pid_list *no_pid_list;
     611           0 :         struct trace_pid_list *pid_list;
     612             : 
     613             :         /* Nothing to do if we are not tracing */
     614           0 :         if (this_cpu_read(tr->array_buffer.data->ignore_pid))
     615             :                 return;
     616             : 
     617           0 :         pid_list = rcu_dereference_sched(tr->filtered_pids);
     618           0 :         no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
     619             : 
     620             :         /* Set tracing if current is enabled */
     621           0 :         this_cpu_write(tr->array_buffer.data->ignore_pid,
     622             :                        trace_ignore_this_task(pid_list, no_pid_list, current));
     623             : }
     624             : 
     625           0 : static void unregister_pid_events(struct trace_array *tr)
     626             : {
     627           0 :         unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr);
     628           0 :         unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr);
     629             : 
     630           0 :         unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr);
     631           0 :         unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr);
     632             : 
     633           0 :         unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, tr);
     634           0 :         unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, tr);
     635             : 
     636           0 :         unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_pre, tr);
     637           0 :         unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_post, tr);
     638           0 : }
     639             : 
     640           0 : static void __ftrace_clear_event_pids(struct trace_array *tr, int type)
     641             : {
     642           0 :         struct trace_pid_list *pid_list;
     643           0 :         struct trace_pid_list *no_pid_list;
     644           0 :         struct trace_event_file *file;
     645           0 :         int cpu;
     646             : 
     647           0 :         pid_list = rcu_dereference_protected(tr->filtered_pids,
     648             :                                              lockdep_is_held(&event_mutex));
     649           0 :         no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
     650             :                                              lockdep_is_held(&event_mutex));
     651             : 
     652             :         /* Make sure there's something to do */
     653           0 :         if (!pid_type_enabled(type, pid_list, no_pid_list))
     654             :                 return;
     655             : 
     656           0 :         if (!still_need_pid_events(type, pid_list, no_pid_list)) {
     657           0 :                 unregister_pid_events(tr);
     658             : 
     659           0 :                 list_for_each_entry(file, &tr->events, list) {
     660           0 :                         clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
     661             :                 }
     662             : 
     663           0 :                 for_each_possible_cpu(cpu)
     664           0 :                         per_cpu_ptr(tr->array_buffer.data, cpu)->ignore_pid = false;
     665             :         }
     666             : 
     667           0 :         if (type & TRACE_PIDS)
     668           0 :                 rcu_assign_pointer(tr->filtered_pids, NULL);
     669             : 
     670           0 :         if (type & TRACE_NO_PIDS)
     671           0 :                 rcu_assign_pointer(tr->filtered_no_pids, NULL);
     672             : 
     673             :         /* Wait till all users are no longer using pid filtering */
     674           0 :         tracepoint_synchronize_unregister();
     675             : 
     676           0 :         if ((type & TRACE_PIDS) && pid_list)
     677           0 :                 trace_free_pid_list(pid_list);
     678             : 
     679           0 :         if ((type & TRACE_NO_PIDS) && no_pid_list)
     680           0 :                 trace_free_pid_list(no_pid_list);
     681             : }
     682             : 
     683           0 : static void ftrace_clear_event_pids(struct trace_array *tr, int type)
     684             : {
     685           0 :         mutex_lock(&event_mutex);
     686           0 :         __ftrace_clear_event_pids(tr, type);
     687           0 :         mutex_unlock(&event_mutex);
     688           0 : }
     689             : 
     690           0 : static void __put_system(struct event_subsystem *system)
     691             : {
     692           0 :         struct event_filter *filter = system->filter;
     693             : 
     694           0 :         WARN_ON_ONCE(system_refcount(system) == 0);
     695           0 :         if (system_refcount_dec(system))
     696             :                 return;
     697             : 
     698           0 :         list_del(&system->list);
     699             : 
     700           0 :         if (filter) {
     701           0 :                 kfree(filter->filter_string);
     702           0 :                 kfree(filter);
     703             :         }
     704           0 :         kfree_const(system->name);
     705           0 :         kfree(system);
     706             : }
     707             : 
     708           0 : static void __get_system(struct event_subsystem *system)
     709             : {
     710           0 :         WARN_ON_ONCE(system_refcount(system) == 0);
     711           0 :         system_refcount_inc(system);
     712           0 : }
     713             : 
     714           0 : static void __get_system_dir(struct trace_subsystem_dir *dir)
     715             : {
     716           0 :         WARN_ON_ONCE(dir->ref_count == 0);
     717           0 :         dir->ref_count++;
     718           0 :         __get_system(dir->subsystem);
     719           0 : }
     720             : 
     721           0 : static void __put_system_dir(struct trace_subsystem_dir *dir)
     722             : {
     723           0 :         WARN_ON_ONCE(dir->ref_count == 0);
     724             :         /* If the subsystem is about to be freed, the dir must be too */
     725           0 :         WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
     726             : 
     727           0 :         __put_system(dir->subsystem);
     728           0 :         if (!--dir->ref_count)
     729           0 :                 kfree(dir);
     730           0 : }
     731             : 
     732           0 : static void put_system(struct trace_subsystem_dir *dir)
     733             : {
     734           0 :         mutex_lock(&event_mutex);
     735           0 :         __put_system_dir(dir);
     736           0 :         mutex_unlock(&event_mutex);
     737           0 : }
     738             : 
     739           0 : static void remove_subsystem(struct trace_subsystem_dir *dir)
     740             : {
     741           0 :         if (!dir)
     742             :                 return;
     743             : 
     744           0 :         if (!--dir->nr_events) {
     745           0 :                 tracefs_remove(dir->entry);
     746           0 :                 list_del(&dir->list);
     747           0 :                 __put_system_dir(dir);
     748             :         }
     749             : }
     750             : 
     751           0 : static void remove_event_file_dir(struct trace_event_file *file)
     752             : {
     753           0 :         struct dentry *dir = file->dir;
     754           0 :         struct dentry *child;
     755             : 
     756           0 :         if (dir) {
     757           0 :                 spin_lock(&dir->d_lock); /* probably unneeded */
     758           0 :                 list_for_each_entry(child, &dir->d_subdirs, d_child) {
     759           0 :                         if (d_really_is_positive(child))        /* probably unneeded */
     760           0 :                                 d_inode(child)->i_private = NULL;
     761             :                 }
     762           0 :                 spin_unlock(&dir->d_lock);
     763             : 
     764           0 :                 tracefs_remove(dir);
     765             :         }
     766             : 
     767           0 :         list_del(&file->list);
     768           0 :         remove_subsystem(file->system);
     769           0 :         free_event_filter(file->filter);
     770           0 :         kmem_cache_free(file_cachep, file);
     771           0 : }
     772             : 
     773             : /*
     774             :  * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
     775             :  */
     776             : static int
     777           0 : __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
     778             :                               const char *sub, const char *event, int set)
     779             : {
     780           0 :         struct trace_event_file *file;
     781           0 :         struct trace_event_call *call;
     782           0 :         const char *name;
     783           0 :         int ret = -EINVAL;
     784           0 :         int eret = 0;
     785             : 
     786           0 :         list_for_each_entry(file, &tr->events, list) {
     787             : 
     788           0 :                 call = file->event_call;
     789           0 :                 name = trace_event_name(call);
     790             : 
     791           0 :                 if (!name || !call->class || !call->class->reg)
     792           0 :                         continue;
     793             : 
     794           0 :                 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
     795           0 :                         continue;
     796             : 
     797           0 :                 if (match &&
     798           0 :                     strcmp(match, name) != 0 &&
     799           0 :                     strcmp(match, call->class->system) != 0)
     800           0 :                         continue;
     801             : 
     802           0 :                 if (sub && strcmp(sub, call->class->system) != 0)
     803           0 :                         continue;
     804             : 
     805           0 :                 if (event && strcmp(event, name) != 0)
     806           0 :                         continue;
     807             : 
     808           0 :                 ret = ftrace_event_enable_disable(file, set);
     809             : 
     810             :                 /*
     811             :                  * Save the first error and return that. Some events
     812             :                  * may still have been enabled, but let the user
     813             :                  * know that something went wrong.
     814             :                  */
     815           0 :                 if (ret && !eret)
     816           0 :                         eret = ret;
     817             : 
     818             :                 ret = eret;
     819             :         }
     820             : 
     821           0 :         return ret;
     822             : }
     823             : 
     824           0 : static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
     825             :                                   const char *sub, const char *event, int set)
     826             : {
     827           0 :         int ret;
     828             : 
     829           0 :         mutex_lock(&event_mutex);
     830           0 :         ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
     831           0 :         mutex_unlock(&event_mutex);
     832             : 
     833           0 :         return ret;
     834             : }
     835             : 
     836           0 : int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
     837             : {
     838           0 :         char *event = NULL, *sub = NULL, *match;
     839           0 :         int ret;
     840             : 
     841           0 :         if (!tr)
     842             :                 return -ENOENT;
     843             :         /*
     844             :          * The buf format can be <subsystem>:<event-name>
     845             :          *  *:<event-name> means any event by that name.
     846             :          *  :<event-name> is the same.
     847             :          *
     848             :          *  <subsystem>:* means all events in that subsystem
     849             :          *  <subsystem>: means the same.
     850             :          *
     851             :          *  <name> (no ':') means all events in a subsystem with
     852             :          *  the name <name> or any event that matches <name>
     853             :          */
     854             : 
     855           0 :         match = strsep(&buf, ":");
     856           0 :         if (buf) {
     857           0 :                 sub = match;
     858           0 :                 event = buf;
     859           0 :                 match = NULL;
     860             : 
     861           0 :                 if (!strlen(sub) || strcmp(sub, "*") == 0)
     862           0 :                         sub = NULL;
     863           0 :                 if (!strlen(event) || strcmp(event, "*") == 0)
     864           0 :                         event = NULL;
     865             :         }
     866             : 
     867           0 :         ret = __ftrace_set_clr_event(tr, match, sub, event, set);
     868             : 
     869             :         /* Put back the colon to allow this to be called again */
     870           0 :         if (buf)
     871           0 :                 *(buf - 1) = ':';
     872             : 
     873             :         return ret;
     874             : }
     875             : 
     876             : /**
     877             :  * trace_set_clr_event - enable or disable an event
     878             :  * @system: system name to match (NULL for any system)
     879             :  * @event: event name to match (NULL for all events, within system)
     880             :  * @set: 1 to enable, 0 to disable
     881             :  *
     882             :  * This is a way for other parts of the kernel to enable or disable
     883             :  * event recording.
     884             :  *
     885             :  * Returns 0 on success, -EINVAL if the parameters do not match any
     886             :  * registered events.
     887             :  */
     888           0 : int trace_set_clr_event(const char *system, const char *event, int set)
     889             : {
     890           0 :         struct trace_array *tr = top_trace_array();
     891             : 
     892           0 :         if (!tr)
     893             :                 return -ENODEV;
     894             : 
     895           0 :         return __ftrace_set_clr_event(tr, NULL, system, event, set);
     896             : }
     897             : EXPORT_SYMBOL_GPL(trace_set_clr_event);
     898             : 
     899             : /**
     900             :  * trace_array_set_clr_event - enable or disable an event for a trace array.
     901             :  * @tr: concerned trace array.
     902             :  * @system: system name to match (NULL for any system)
     903             :  * @event: event name to match (NULL for all events, within system)
     904             :  * @enable: true to enable, false to disable
     905             :  *
     906             :  * This is a way for other parts of the kernel to enable or disable
     907             :  * event recording.
     908             :  *
     909             :  * Returns 0 on success, -EINVAL if the parameters do not match any
     910             :  * registered events.
     911             :  */
     912           0 : int trace_array_set_clr_event(struct trace_array *tr, const char *system,
     913             :                 const char *event, bool enable)
     914             : {
     915           0 :         int set;
     916             : 
     917           0 :         if (!tr)
     918             :                 return -ENOENT;
     919             : 
     920           0 :         set = (enable == true) ? 1 : 0;
     921           0 :         return __ftrace_set_clr_event(tr, NULL, system, event, set);
     922             : }
     923             : EXPORT_SYMBOL_GPL(trace_array_set_clr_event);
     924             : 
     925             : /* 128 should be much more than enough */
     926             : #define EVENT_BUF_SIZE          127
     927             : 
     928             : static ssize_t
     929           0 : ftrace_event_write(struct file *file, const char __user *ubuf,
     930             :                    size_t cnt, loff_t *ppos)
     931             : {
     932           0 :         struct trace_parser parser;
     933           0 :         struct seq_file *m = file->private_data;
     934           0 :         struct trace_array *tr = m->private;
     935           0 :         ssize_t read, ret;
     936             : 
     937           0 :         if (!cnt)
     938             :                 return 0;
     939             : 
     940           0 :         ret = tracing_update_buffers();
     941           0 :         if (ret < 0)
     942             :                 return ret;
     943             : 
     944           0 :         if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
     945             :                 return -ENOMEM;
     946             : 
     947           0 :         read = trace_get_user(&parser, ubuf, cnt, ppos);
     948             : 
     949           0 :         if (read >= 0 && trace_parser_loaded((&parser))) {
     950           0 :                 int set = 1;
     951             : 
     952           0 :                 if (*parser.buffer == '!')
     953           0 :                         set = 0;
     954             : 
     955           0 :                 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
     956           0 :                 if (ret)
     957           0 :                         goto out_put;
     958             :         }
     959             : 
     960             :         ret = read;
     961             : 
     962           0 :  out_put:
     963           0 :         trace_parser_put(&parser);
     964             : 
     965           0 :         return ret;
     966             : }
     967             : 
     968             : static void *
     969           0 : t_next(struct seq_file *m, void *v, loff_t *pos)
     970             : {
     971           0 :         struct trace_event_file *file = v;
     972           0 :         struct trace_event_call *call;
     973           0 :         struct trace_array *tr = m->private;
     974             : 
     975           0 :         (*pos)++;
     976             : 
     977           0 :         list_for_each_entry_continue(file, &tr->events, list) {
     978           0 :                 call = file->event_call;
     979             :                 /*
     980             :                  * The ftrace subsystem is for showing formats only.
     981             :                  * They can not be enabled or disabled via the event files.
     982             :                  */
     983           0 :                 if (call->class && call->class->reg &&
     984           0 :                     !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
     985           0 :                         return file;
     986             :         }
     987             : 
     988             :         return NULL;
     989             : }
     990             : 
     991           0 : static void *t_start(struct seq_file *m, loff_t *pos)
     992             : {
     993           0 :         struct trace_event_file *file;
     994           0 :         struct trace_array *tr = m->private;
     995           0 :         loff_t l;
     996             : 
     997           0 :         mutex_lock(&event_mutex);
     998             : 
     999           0 :         file = list_entry(&tr->events, struct trace_event_file, list);
    1000           0 :         for (l = 0; l <= *pos; ) {
    1001           0 :                 file = t_next(m, file, &l);
    1002           0 :                 if (!file)
    1003             :                         break;
    1004             :         }
    1005           0 :         return file;
    1006             : }
    1007             : 
    1008             : static void *
    1009           0 : s_next(struct seq_file *m, void *v, loff_t *pos)
    1010             : {
    1011           0 :         struct trace_event_file *file = v;
    1012           0 :         struct trace_array *tr = m->private;
    1013             : 
    1014           0 :         (*pos)++;
    1015             : 
    1016           0 :         list_for_each_entry_continue(file, &tr->events, list) {
    1017           0 :                 if (file->flags & EVENT_FILE_FL_ENABLED)
    1018           0 :                         return file;
    1019             :         }
    1020             : 
    1021             :         return NULL;
    1022             : }
    1023             : 
    1024           0 : static void *s_start(struct seq_file *m, loff_t *pos)
    1025             : {
    1026           0 :         struct trace_event_file *file;
    1027           0 :         struct trace_array *tr = m->private;
    1028           0 :         loff_t l;
    1029             : 
    1030           0 :         mutex_lock(&event_mutex);
    1031             : 
    1032           0 :         file = list_entry(&tr->events, struct trace_event_file, list);
    1033           0 :         for (l = 0; l <= *pos; ) {
    1034           0 :                 file = s_next(m, file, &l);
    1035           0 :                 if (!file)
    1036             :                         break;
    1037             :         }
    1038           0 :         return file;
    1039             : }
    1040             : 
    1041           0 : static int t_show(struct seq_file *m, void *v)
    1042             : {
    1043           0 :         struct trace_event_file *file = v;
    1044           0 :         struct trace_event_call *call = file->event_call;
    1045             : 
    1046           0 :         if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
    1047           0 :                 seq_printf(m, "%s:", call->class->system);
    1048           0 :         seq_printf(m, "%s\n", trace_event_name(call));
    1049             : 
    1050           0 :         return 0;
    1051             : }
    1052             : 
    1053           0 : static void t_stop(struct seq_file *m, void *p)
    1054             : {
    1055           0 :         mutex_unlock(&event_mutex);
    1056           0 : }
    1057             : 
    1058             : static void *
    1059           0 : __next(struct seq_file *m, void *v, loff_t *pos, int type)
    1060             : {
    1061           0 :         struct trace_array *tr = m->private;
    1062           0 :         struct trace_pid_list *pid_list;
    1063             : 
    1064           0 :         if (type == TRACE_PIDS)
    1065           0 :                 pid_list = rcu_dereference_sched(tr->filtered_pids);
    1066             :         else
    1067           0 :                 pid_list = rcu_dereference_sched(tr->filtered_no_pids);
    1068             : 
    1069           0 :         return trace_pid_next(pid_list, v, pos);
    1070             : }
    1071             : 
    1072             : static void *
    1073           0 : p_next(struct seq_file *m, void *v, loff_t *pos)
    1074             : {
    1075           0 :         return __next(m, v, pos, TRACE_PIDS);
    1076             : }
    1077             : 
    1078             : static void *
    1079           0 : np_next(struct seq_file *m, void *v, loff_t *pos)
    1080             : {
    1081           0 :         return __next(m, v, pos, TRACE_NO_PIDS);
    1082             : }
    1083             : 
    1084           0 : static void *__start(struct seq_file *m, loff_t *pos, int type)
    1085             :         __acquires(RCU)
    1086             : {
    1087           0 :         struct trace_pid_list *pid_list;
    1088           0 :         struct trace_array *tr = m->private;
    1089             : 
    1090             :         /*
    1091             :          * Grab the mutex, to keep calls to p_next() having the same
    1092             :          * tr->filtered_pids as p_start() has.
    1093             :          * If we just passed the tr->filtered_pids around, then RCU would
    1094             :          * have been enough, but doing that makes things more complex.
    1095             :          */
    1096           0 :         mutex_lock(&event_mutex);
    1097           0 :         rcu_read_lock_sched();
    1098             : 
    1099           0 :         if (type == TRACE_PIDS)
    1100           0 :                 pid_list = rcu_dereference_sched(tr->filtered_pids);
    1101             :         else
    1102           0 :                 pid_list = rcu_dereference_sched(tr->filtered_no_pids);
    1103             : 
    1104           0 :         if (!pid_list)
    1105             :                 return NULL;
    1106             : 
    1107           0 :         return trace_pid_start(pid_list, pos);
    1108             : }
    1109             : 
    1110           0 : static void *p_start(struct seq_file *m, loff_t *pos)
    1111             :         __acquires(RCU)
    1112             : {
    1113           0 :         return __start(m, pos, TRACE_PIDS);
    1114             : }
    1115             : 
    1116           0 : static void *np_start(struct seq_file *m, loff_t *pos)
    1117             :         __acquires(RCU)
    1118             : {
    1119           0 :         return __start(m, pos, TRACE_NO_PIDS);
    1120             : }
    1121             : 
    1122           0 : static void p_stop(struct seq_file *m, void *p)
    1123             :         __releases(RCU)
    1124             : {
    1125           0 :         rcu_read_unlock_sched();
    1126           0 :         mutex_unlock(&event_mutex);
    1127           0 : }
    1128             : 
    1129             : static ssize_t
    1130           0 : event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
    1131             :                   loff_t *ppos)
    1132             : {
    1133           0 :         struct trace_event_file *file;
    1134           0 :         unsigned long flags;
    1135           0 :         char buf[4] = "0";
    1136             : 
    1137           0 :         mutex_lock(&event_mutex);
    1138           0 :         file = event_file_data(filp);
    1139           0 :         if (likely(file))
    1140           0 :                 flags = file->flags;
    1141           0 :         mutex_unlock(&event_mutex);
    1142             : 
    1143           0 :         if (!file)
    1144             :                 return -ENODEV;
    1145             : 
    1146           0 :         if (flags & EVENT_FILE_FL_ENABLED &&
    1147             :             !(flags & EVENT_FILE_FL_SOFT_DISABLED))
    1148           0 :                 strcpy(buf, "1");
    1149             : 
    1150           0 :         if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
    1151             :             flags & EVENT_FILE_FL_SOFT_MODE)
    1152           0 :                 strcat(buf, "*");
    1153             : 
    1154           0 :         strcat(buf, "\n");
    1155             : 
    1156           0 :         return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
    1157             : }
    1158             : 
    1159             : static ssize_t
    1160           0 : event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
    1161             :                    loff_t *ppos)
    1162             : {
    1163           0 :         struct trace_event_file *file;
    1164           0 :         unsigned long val;
    1165           0 :         int ret;
    1166             : 
    1167           0 :         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
    1168           0 :         if (ret)
    1169           0 :                 return ret;
    1170             : 
    1171           0 :         ret = tracing_update_buffers();
    1172           0 :         if (ret < 0)
    1173           0 :                 return ret;
    1174             : 
    1175           0 :         switch (val) {
    1176           0 :         case 0:
    1177             :         case 1:
    1178           0 :                 ret = -ENODEV;
    1179           0 :                 mutex_lock(&event_mutex);
    1180           0 :                 file = event_file_data(filp);
    1181           0 :                 if (likely(file))
    1182           0 :                         ret = ftrace_event_enable_disable(file, val);
    1183           0 :                 mutex_unlock(&event_mutex);
    1184           0 :                 break;
    1185             : 
    1186             :         default:
    1187             :                 return -EINVAL;
    1188             :         }
    1189             : 
    1190           0 :         *ppos += cnt;
    1191             : 
    1192           0 :         return ret ? ret : cnt;
    1193             : }
    1194             : 
    1195             : static ssize_t
    1196           0 : system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
    1197             :                    loff_t *ppos)
    1198             : {
    1199           0 :         const char set_to_char[4] = { '?', '0', '1', 'X' };
    1200           0 :         struct trace_subsystem_dir *dir = filp->private_data;
    1201           0 :         struct event_subsystem *system = dir->subsystem;
    1202           0 :         struct trace_event_call *call;
    1203           0 :         struct trace_event_file *file;
    1204           0 :         struct trace_array *tr = dir->tr;
    1205           0 :         char buf[2];
    1206           0 :         int set = 0;
    1207           0 :         int ret;
    1208             : 
    1209           0 :         mutex_lock(&event_mutex);
    1210           0 :         list_for_each_entry(file, &tr->events, list) {
    1211           0 :                 call = file->event_call;
    1212           0 :                 if ((call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) ||
    1213           0 :                     !trace_event_name(call) || !call->class || !call->class->reg)
    1214           0 :                         continue;
    1215             : 
    1216           0 :                 if (system && strcmp(call->class->system, system->name) != 0)
    1217           0 :                         continue;
    1218             : 
    1219             :                 /*
    1220             :                  * We need to find out if all the events are set
    1221             :                  * or if all events or cleared, or if we have
    1222             :                  * a mixture.
    1223             :                  */
    1224           0 :                 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
    1225             : 
    1226             :                 /*
    1227             :                  * If we have a mixture, no need to look further.
    1228             :                  */
    1229           0 :                 if (set == 3)
    1230             :                         break;
    1231             :         }
    1232           0 :         mutex_unlock(&event_mutex);
    1233             : 
    1234           0 :         buf[0] = set_to_char[set];
    1235           0 :         buf[1] = '\n';
    1236             : 
    1237           0 :         ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
    1238             : 
    1239           0 :         return ret;
    1240             : }
    1241             : 
    1242             : static ssize_t
    1243           0 : system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
    1244             :                     loff_t *ppos)
    1245             : {
    1246           0 :         struct trace_subsystem_dir *dir = filp->private_data;
    1247           0 :         struct event_subsystem *system = dir->subsystem;
    1248           0 :         const char *name = NULL;
    1249           0 :         unsigned long val;
    1250           0 :         ssize_t ret;
    1251             : 
    1252           0 :         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
    1253           0 :         if (ret)
    1254             :                 return ret;
    1255             : 
    1256           0 :         ret = tracing_update_buffers();
    1257           0 :         if (ret < 0)
    1258             :                 return ret;
    1259             : 
    1260           0 :         if (val != 0 && val != 1)
    1261             :                 return -EINVAL;
    1262             : 
    1263             :         /*
    1264             :          * Opening of "enable" adds a ref count to system,
    1265             :          * so the name is safe to use.
    1266             :          */
    1267           0 :         if (system)
    1268           0 :                 name = system->name;
    1269             : 
    1270           0 :         ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
    1271           0 :         if (ret)
    1272           0 :                 goto out;
    1273             : 
    1274           0 :         ret = cnt;
    1275             : 
    1276           0 : out:
    1277           0 :         *ppos += cnt;
    1278             : 
    1279           0 :         return ret;
    1280             : }
    1281             : 
    1282             : enum {
    1283             :         FORMAT_HEADER           = 1,
    1284             :         FORMAT_FIELD_SEPERATOR  = 2,
    1285             :         FORMAT_PRINTFMT         = 3,
    1286             : };
    1287             : 
    1288           0 : static void *f_next(struct seq_file *m, void *v, loff_t *pos)
    1289             : {
    1290           0 :         struct trace_event_call *call = event_file_data(m->private);
    1291           0 :         struct list_head *common_head = &ftrace_common_fields;
    1292           0 :         struct list_head *head = trace_get_fields(call);
    1293           0 :         struct list_head *node = v;
    1294             : 
    1295           0 :         (*pos)++;
    1296             : 
    1297           0 :         switch ((unsigned long)v) {
    1298           0 :         case FORMAT_HEADER:
    1299           0 :                 node = common_head;
    1300           0 :                 break;
    1301             : 
    1302           0 :         case FORMAT_FIELD_SEPERATOR:
    1303           0 :                 node = head;
    1304           0 :                 break;
    1305             : 
    1306             :         case FORMAT_PRINTFMT:
    1307             :                 /* all done */
    1308             :                 return NULL;
    1309             :         }
    1310             : 
    1311           0 :         node = node->prev;
    1312           0 :         if (node == common_head)
    1313             :                 return (void *)FORMAT_FIELD_SEPERATOR;
    1314           0 :         else if (node == head)
    1315             :                 return (void *)FORMAT_PRINTFMT;
    1316             :         else
    1317           0 :                 return node;
    1318             : }
    1319             : 
    1320           0 : static int f_show(struct seq_file *m, void *v)
    1321             : {
    1322           0 :         struct trace_event_call *call = event_file_data(m->private);
    1323           0 :         struct ftrace_event_field *field;
    1324           0 :         const char *array_descriptor;
    1325             : 
    1326           0 :         switch ((unsigned long)v) {
    1327             :         case FORMAT_HEADER:
    1328           0 :                 seq_printf(m, "name: %s\n", trace_event_name(call));
    1329           0 :                 seq_printf(m, "ID: %d\n", call->event.type);
    1330           0 :                 seq_puts(m, "format:\n");
    1331           0 :                 return 0;
    1332             : 
    1333           0 :         case FORMAT_FIELD_SEPERATOR:
    1334           0 :                 seq_putc(m, '\n');
    1335           0 :                 return 0;
    1336             : 
    1337           0 :         case FORMAT_PRINTFMT:
    1338           0 :                 seq_printf(m, "\nprint fmt: %s\n",
    1339             :                            call->print_fmt);
    1340           0 :                 return 0;
    1341             :         }
    1342             : 
    1343           0 :         field = list_entry(v, struct ftrace_event_field, link);
    1344             :         /*
    1345             :          * Smartly shows the array type(except dynamic array).
    1346             :          * Normal:
    1347             :          *      field:TYPE VAR
    1348             :          * If TYPE := TYPE[LEN], it is shown:
    1349             :          *      field:TYPE VAR[LEN]
    1350             :          */
    1351           0 :         array_descriptor = strchr(field->type, '[');
    1352             : 
    1353           0 :         if (str_has_prefix(field->type, "__data_loc"))
    1354             :                 array_descriptor = NULL;
    1355             : 
    1356           0 :         if (!array_descriptor)
    1357           0 :                 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
    1358             :                            field->type, field->name, field->offset,
    1359           0 :                            field->size, !!field->is_signed);
    1360             :         else
    1361           0 :                 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
    1362           0 :                            (int)(array_descriptor - field->type),
    1363             :                            field->type, field->name,
    1364             :                            array_descriptor, field->offset,
    1365           0 :                            field->size, !!field->is_signed);
    1366             : 
    1367             :         return 0;
    1368             : }
    1369             : 
    1370           0 : static void *f_start(struct seq_file *m, loff_t *pos)
    1371             : {
    1372           0 :         void *p = (void *)FORMAT_HEADER;
    1373           0 :         loff_t l = 0;
    1374             : 
    1375             :         /* ->stop() is called even if ->start() fails */
    1376           0 :         mutex_lock(&event_mutex);
    1377           0 :         if (!event_file_data(m->private))
    1378           0 :                 return ERR_PTR(-ENODEV);
    1379             : 
    1380           0 :         while (l < *pos && p)
    1381           0 :                 p = f_next(m, p, &l);
    1382             : 
    1383             :         return p;
    1384             : }
    1385             : 
    1386           0 : static void f_stop(struct seq_file *m, void *p)
    1387             : {
    1388           0 :         mutex_unlock(&event_mutex);
    1389           0 : }
    1390             : 
    1391             : static const struct seq_operations trace_format_seq_ops = {
    1392             :         .start          = f_start,
    1393             :         .next           = f_next,
    1394             :         .stop           = f_stop,
    1395             :         .show           = f_show,
    1396             : };
    1397             : 
    1398           0 : static int trace_format_open(struct inode *inode, struct file *file)
    1399             : {
    1400           0 :         struct seq_file *m;
    1401           0 :         int ret;
    1402             : 
    1403             :         /* Do we want to hide event format files on tracefs lockdown? */
    1404             : 
    1405           0 :         ret = seq_open(file, &trace_format_seq_ops);
    1406           0 :         if (ret < 0)
    1407             :                 return ret;
    1408             : 
    1409           0 :         m = file->private_data;
    1410           0 :         m->private = file;
    1411             : 
    1412           0 :         return 0;
    1413             : }
    1414             : 
    1415             : static ssize_t
    1416           0 : event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
    1417             : {
    1418           0 :         int id = (long)event_file_data(filp);
    1419           0 :         char buf[32];
    1420           0 :         int len;
    1421             : 
    1422           0 :         if (unlikely(!id))
    1423             :                 return -ENODEV;
    1424             : 
    1425           0 :         len = sprintf(buf, "%d\n", id);
    1426             : 
    1427           0 :         return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
    1428             : }
    1429             : 
    1430             : static ssize_t
    1431           0 : event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
    1432             :                   loff_t *ppos)
    1433             : {
    1434           0 :         struct trace_event_file *file;
    1435           0 :         struct trace_seq *s;
    1436           0 :         int r = -ENODEV;
    1437             : 
    1438           0 :         if (*ppos)
    1439             :                 return 0;
    1440             : 
    1441           0 :         s = kmalloc(sizeof(*s), GFP_KERNEL);
    1442             : 
    1443           0 :         if (!s)
    1444             :                 return -ENOMEM;
    1445             : 
    1446           0 :         trace_seq_init(s);
    1447             : 
    1448           0 :         mutex_lock(&event_mutex);
    1449           0 :         file = event_file_data(filp);
    1450           0 :         if (file)
    1451           0 :                 print_event_filter(file, s);
    1452           0 :         mutex_unlock(&event_mutex);
    1453             : 
    1454           0 :         if (file)
    1455           0 :                 r = simple_read_from_buffer(ubuf, cnt, ppos,
    1456           0 :                                             s->buffer, trace_seq_used(s));
    1457             : 
    1458           0 :         kfree(s);
    1459             : 
    1460           0 :         return r;
    1461             : }
    1462             : 
    1463             : static ssize_t
    1464           0 : event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
    1465             :                    loff_t *ppos)
    1466             : {
    1467           0 :         struct trace_event_file *file;
    1468           0 :         char *buf;
    1469           0 :         int err = -ENODEV;
    1470             : 
    1471           0 :         if (cnt >= PAGE_SIZE)
    1472             :                 return -EINVAL;
    1473             : 
    1474           0 :         buf = memdup_user_nul(ubuf, cnt);
    1475           0 :         if (IS_ERR(buf))
    1476           0 :                 return PTR_ERR(buf);
    1477             : 
    1478           0 :         mutex_lock(&event_mutex);
    1479           0 :         file = event_file_data(filp);
    1480           0 :         if (file)
    1481           0 :                 err = apply_event_filter(file, buf);
    1482           0 :         mutex_unlock(&event_mutex);
    1483             : 
    1484           0 :         kfree(buf);
    1485           0 :         if (err < 0)
    1486           0 :                 return err;
    1487             : 
    1488           0 :         *ppos += cnt;
    1489             : 
    1490           0 :         return cnt;
    1491             : }
    1492             : 
    1493             : static LIST_HEAD(event_subsystems);
    1494             : 
    1495           0 : static int subsystem_open(struct inode *inode, struct file *filp)
    1496             : {
    1497           0 :         struct event_subsystem *system = NULL;
    1498           0 :         struct trace_subsystem_dir *dir = NULL; /* Initialize for gcc */
    1499           0 :         struct trace_array *tr;
    1500           0 :         int ret;
    1501             : 
    1502           0 :         if (tracing_is_disabled())
    1503             :                 return -ENODEV;
    1504             : 
    1505             :         /* Make sure the system still exists */
    1506           0 :         mutex_lock(&event_mutex);
    1507           0 :         mutex_lock(&trace_types_lock);
    1508           0 :         list_for_each_entry(tr, &ftrace_trace_arrays, list) {
    1509           0 :                 list_for_each_entry(dir, &tr->systems, list) {
    1510           0 :                         if (dir == inode->i_private) {
    1511             :                                 /* Don't open systems with no events */
    1512           0 :                                 if (dir->nr_events) {
    1513           0 :                                         __get_system_dir(dir);
    1514           0 :                                         system = dir->subsystem;
    1515             :                                 }
    1516           0 :                                 goto exit_loop;
    1517             :                         }
    1518             :                 }
    1519             :         }
    1520           0 :  exit_loop:
    1521           0 :         mutex_unlock(&trace_types_lock);
    1522           0 :         mutex_unlock(&event_mutex);
    1523             : 
    1524           0 :         if (!system)
    1525             :                 return -ENODEV;
    1526             : 
    1527             :         /* Some versions of gcc think dir can be uninitialized here */
    1528           0 :         WARN_ON(!dir);
    1529             : 
    1530             :         /* Still need to increment the ref count of the system */
    1531           0 :         if (trace_array_get(tr) < 0) {
    1532           0 :                 put_system(dir);
    1533           0 :                 return -ENODEV;
    1534             :         }
    1535             : 
    1536           0 :         ret = tracing_open_generic(inode, filp);
    1537           0 :         if (ret < 0) {
    1538           0 :                 trace_array_put(tr);
    1539           0 :                 put_system(dir);
    1540             :         }
    1541             : 
    1542             :         return ret;
    1543             : }
    1544             : 
    1545           0 : static int system_tr_open(struct inode *inode, struct file *filp)
    1546             : {
    1547           0 :         struct trace_subsystem_dir *dir;
    1548           0 :         struct trace_array *tr = inode->i_private;
    1549           0 :         int ret;
    1550             : 
    1551             :         /* Make a temporary dir that has no system but points to tr */
    1552           0 :         dir = kzalloc(sizeof(*dir), GFP_KERNEL);
    1553           0 :         if (!dir)
    1554             :                 return -ENOMEM;
    1555             : 
    1556           0 :         ret = tracing_open_generic_tr(inode, filp);
    1557           0 :         if (ret < 0) {
    1558           0 :                 kfree(dir);
    1559           0 :                 return ret;
    1560             :         }
    1561           0 :         dir->tr = tr;
    1562           0 :         filp->private_data = dir;
    1563             : 
    1564           0 :         return 0;
    1565             : }
    1566             : 
    1567           0 : static int subsystem_release(struct inode *inode, struct file *file)
    1568             : {
    1569           0 :         struct trace_subsystem_dir *dir = file->private_data;
    1570             : 
    1571           0 :         trace_array_put(dir->tr);
    1572             : 
    1573             :         /*
    1574             :          * If dir->subsystem is NULL, then this is a temporary
    1575             :          * descriptor that was made for a trace_array to enable
    1576             :          * all subsystems.
    1577             :          */
    1578           0 :         if (dir->subsystem)
    1579           0 :                 put_system(dir);
    1580             :         else
    1581           0 :                 kfree(dir);
    1582             : 
    1583           0 :         return 0;
    1584             : }
    1585             : 
    1586             : static ssize_t
    1587           0 : subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
    1588             :                       loff_t *ppos)
    1589             : {
    1590           0 :         struct trace_subsystem_dir *dir = filp->private_data;
    1591           0 :         struct event_subsystem *system = dir->subsystem;
    1592           0 :         struct trace_seq *s;
    1593           0 :         int r;
    1594             : 
    1595           0 :         if (*ppos)
    1596             :                 return 0;
    1597             : 
    1598           0 :         s = kmalloc(sizeof(*s), GFP_KERNEL);
    1599           0 :         if (!s)
    1600             :                 return -ENOMEM;
    1601             : 
    1602           0 :         trace_seq_init(s);
    1603             : 
    1604           0 :         print_subsystem_event_filter(system, s);
    1605           0 :         r = simple_read_from_buffer(ubuf, cnt, ppos,
    1606           0 :                                     s->buffer, trace_seq_used(s));
    1607             : 
    1608           0 :         kfree(s);
    1609             : 
    1610           0 :         return r;
    1611             : }
    1612             : 
    1613             : static ssize_t
    1614           0 : subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
    1615             :                        loff_t *ppos)
    1616             : {
    1617           0 :         struct trace_subsystem_dir *dir = filp->private_data;
    1618           0 :         char *buf;
    1619           0 :         int err;
    1620             : 
    1621           0 :         if (cnt >= PAGE_SIZE)
    1622             :                 return -EINVAL;
    1623             : 
    1624           0 :         buf = memdup_user_nul(ubuf, cnt);
    1625           0 :         if (IS_ERR(buf))
    1626           0 :                 return PTR_ERR(buf);
    1627             : 
    1628           0 :         err = apply_subsystem_event_filter(dir, buf);
    1629           0 :         kfree(buf);
    1630           0 :         if (err < 0)
    1631           0 :                 return err;
    1632             : 
    1633           0 :         *ppos += cnt;
    1634             : 
    1635           0 :         return cnt;
    1636             : }
    1637             : 
    1638             : static ssize_t
    1639           0 : show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
    1640             : {
    1641           0 :         int (*func)(struct trace_seq *s) = filp->private_data;
    1642           0 :         struct trace_seq *s;
    1643           0 :         int r;
    1644             : 
    1645           0 :         if (*ppos)
    1646             :                 return 0;
    1647             : 
    1648           0 :         s = kmalloc(sizeof(*s), GFP_KERNEL);
    1649           0 :         if (!s)
    1650             :                 return -ENOMEM;
    1651             : 
    1652           0 :         trace_seq_init(s);
    1653             : 
    1654           0 :         func(s);
    1655           0 :         r = simple_read_from_buffer(ubuf, cnt, ppos,
    1656           0 :                                     s->buffer, trace_seq_used(s));
    1657             : 
    1658           0 :         kfree(s);
    1659             : 
    1660           0 :         return r;
    1661             : }
    1662             : 
    1663           0 : static void ignore_task_cpu(void *data)
    1664             : {
    1665           0 :         struct trace_array *tr = data;
    1666           0 :         struct trace_pid_list *pid_list;
    1667           0 :         struct trace_pid_list *no_pid_list;
    1668             : 
    1669             :         /*
    1670             :          * This function is called by on_each_cpu() while the
    1671             :          * event_mutex is held.
    1672             :          */
    1673           0 :         pid_list = rcu_dereference_protected(tr->filtered_pids,
    1674             :                                              mutex_is_locked(&event_mutex));
    1675           0 :         no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
    1676             :                                              mutex_is_locked(&event_mutex));
    1677             : 
    1678           0 :         this_cpu_write(tr->array_buffer.data->ignore_pid,
    1679             :                        trace_ignore_this_task(pid_list, no_pid_list, current));
    1680           0 : }
    1681             : 
    1682           0 : static void register_pid_events(struct trace_array *tr)
    1683             : {
    1684             :         /*
    1685             :          * Register a probe that is called before all other probes
    1686             :          * to set ignore_pid if next or prev do not match.
    1687             :          * Register a probe this is called after all other probes
    1688             :          * to only keep ignore_pid set if next pid matches.
    1689             :          */
    1690           0 :         register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre,
    1691             :                                          tr, INT_MAX);
    1692           0 :         register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post,
    1693             :                                          tr, 0);
    1694             : 
    1695           0 :         register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre,
    1696             :                                          tr, INT_MAX);
    1697           0 :         register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post,
    1698             :                                          tr, 0);
    1699             : 
    1700           0 :         register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre,
    1701             :                                              tr, INT_MAX);
    1702           0 :         register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post,
    1703             :                                              tr, 0);
    1704             : 
    1705           0 :         register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre,
    1706             :                                          tr, INT_MAX);
    1707           0 :         register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post,
    1708             :                                          tr, 0);
    1709           0 : }
    1710             : 
    1711             : static ssize_t
    1712           0 : event_pid_write(struct file *filp, const char __user *ubuf,
    1713             :                 size_t cnt, loff_t *ppos, int type)
    1714             : {
    1715           0 :         struct seq_file *m = filp->private_data;
    1716           0 :         struct trace_array *tr = m->private;
    1717           0 :         struct trace_pid_list *filtered_pids = NULL;
    1718           0 :         struct trace_pid_list *other_pids = NULL;
    1719           0 :         struct trace_pid_list *pid_list;
    1720           0 :         struct trace_event_file *file;
    1721           0 :         ssize_t ret;
    1722             : 
    1723           0 :         if (!cnt)
    1724             :                 return 0;
    1725             : 
    1726           0 :         ret = tracing_update_buffers();
    1727           0 :         if (ret < 0)
    1728             :                 return ret;
    1729             : 
    1730           0 :         mutex_lock(&event_mutex);
    1731             : 
    1732           0 :         if (type == TRACE_PIDS) {
    1733           0 :                 filtered_pids = rcu_dereference_protected(tr->filtered_pids,
    1734             :                                                           lockdep_is_held(&event_mutex));
    1735           0 :                 other_pids = rcu_dereference_protected(tr->filtered_no_pids,
    1736             :                                                           lockdep_is_held(&event_mutex));
    1737             :         } else {
    1738           0 :                 filtered_pids = rcu_dereference_protected(tr->filtered_no_pids,
    1739             :                                                           lockdep_is_held(&event_mutex));
    1740           0 :                 other_pids = rcu_dereference_protected(tr->filtered_pids,
    1741             :                                                           lockdep_is_held(&event_mutex));
    1742             :         }
    1743             : 
    1744           0 :         ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
    1745           0 :         if (ret < 0)
    1746           0 :                 goto out;
    1747             : 
    1748           0 :         if (type == TRACE_PIDS)
    1749           0 :                 rcu_assign_pointer(tr->filtered_pids, pid_list);
    1750             :         else
    1751           0 :                 rcu_assign_pointer(tr->filtered_no_pids, pid_list);
    1752             : 
    1753           0 :         list_for_each_entry(file, &tr->events, list) {
    1754           0 :                 set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
    1755             :         }
    1756             : 
    1757           0 :         if (filtered_pids) {
    1758           0 :                 tracepoint_synchronize_unregister();
    1759           0 :                 trace_free_pid_list(filtered_pids);
    1760           0 :         } else if (pid_list && !other_pids) {
    1761           0 :                 register_pid_events(tr);
    1762             :         }
    1763             : 
    1764             :         /*
    1765             :          * Ignoring of pids is done at task switch. But we have to
    1766             :          * check for those tasks that are currently running.
    1767             :          * Always do this in case a pid was appended or removed.
    1768             :          */
    1769           0 :         on_each_cpu(ignore_task_cpu, tr, 1);
    1770             : 
    1771           0 :  out:
    1772           0 :         mutex_unlock(&event_mutex);
    1773             : 
    1774           0 :         if (ret > 0)
    1775           0 :                 *ppos += ret;
    1776             : 
    1777             :         return ret;
    1778             : }
    1779             : 
    1780             : static ssize_t
    1781           0 : ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
    1782             :                        size_t cnt, loff_t *ppos)
    1783             : {
    1784           0 :         return event_pid_write(filp, ubuf, cnt, ppos, TRACE_PIDS);
    1785             : }
    1786             : 
    1787             : static ssize_t
    1788           0 : ftrace_event_npid_write(struct file *filp, const char __user *ubuf,
    1789             :                         size_t cnt, loff_t *ppos)
    1790             : {
    1791           0 :         return event_pid_write(filp, ubuf, cnt, ppos, TRACE_NO_PIDS);
    1792             : }
    1793             : 
    1794             : static int ftrace_event_avail_open(struct inode *inode, struct file *file);
    1795             : static int ftrace_event_set_open(struct inode *inode, struct file *file);
    1796             : static int ftrace_event_set_pid_open(struct inode *inode, struct file *file);
    1797             : static int ftrace_event_set_npid_open(struct inode *inode, struct file *file);
    1798             : static int ftrace_event_release(struct inode *inode, struct file *file);
    1799             : 
    1800             : static const struct seq_operations show_event_seq_ops = {
    1801             :         .start = t_start,
    1802             :         .next = t_next,
    1803             :         .show = t_show,
    1804             :         .stop = t_stop,
    1805             : };
    1806             : 
    1807             : static const struct seq_operations show_set_event_seq_ops = {
    1808             :         .start = s_start,
    1809             :         .next = s_next,
    1810             :         .show = t_show,
    1811             :         .stop = t_stop,
    1812             : };
    1813             : 
    1814             : static const struct seq_operations show_set_pid_seq_ops = {
    1815             :         .start = p_start,
    1816             :         .next = p_next,
    1817             :         .show = trace_pid_show,
    1818             :         .stop = p_stop,
    1819             : };
    1820             : 
    1821             : static const struct seq_operations show_set_no_pid_seq_ops = {
    1822             :         .start = np_start,
    1823             :         .next = np_next,
    1824             :         .show = trace_pid_show,
    1825             :         .stop = p_stop,
    1826             : };
    1827             : 
    1828             : static const struct file_operations ftrace_avail_fops = {
    1829             :         .open = ftrace_event_avail_open,
    1830             :         .read = seq_read,
    1831             :         .llseek = seq_lseek,
    1832             :         .release = seq_release,
    1833             : };
    1834             : 
    1835             : static const struct file_operations ftrace_set_event_fops = {
    1836             :         .open = ftrace_event_set_open,
    1837             :         .read = seq_read,
    1838             :         .write = ftrace_event_write,
    1839             :         .llseek = seq_lseek,
    1840             :         .release = ftrace_event_release,
    1841             : };
    1842             : 
    1843             : static const struct file_operations ftrace_set_event_pid_fops = {
    1844             :         .open = ftrace_event_set_pid_open,
    1845             :         .read = seq_read,
    1846             :         .write = ftrace_event_pid_write,
    1847             :         .llseek = seq_lseek,
    1848             :         .release = ftrace_event_release,
    1849             : };
    1850             : 
    1851             : static const struct file_operations ftrace_set_event_notrace_pid_fops = {
    1852             :         .open = ftrace_event_set_npid_open,
    1853             :         .read = seq_read,
    1854             :         .write = ftrace_event_npid_write,
    1855             :         .llseek = seq_lseek,
    1856             :         .release = ftrace_event_release,
    1857             : };
    1858             : 
    1859             : static const struct file_operations ftrace_enable_fops = {
    1860             :         .open = tracing_open_generic,
    1861             :         .read = event_enable_read,
    1862             :         .write = event_enable_write,
    1863             :         .llseek = default_llseek,
    1864             : };
    1865             : 
    1866             : static const struct file_operations ftrace_event_format_fops = {
    1867             :         .open = trace_format_open,
    1868             :         .read = seq_read,
    1869             :         .llseek = seq_lseek,
    1870             :         .release = seq_release,
    1871             : };
    1872             : 
    1873             : static const struct file_operations ftrace_event_id_fops = {
    1874             :         .read = event_id_read,
    1875             :         .llseek = default_llseek,
    1876             : };
    1877             : 
    1878             : static const struct file_operations ftrace_event_filter_fops = {
    1879             :         .open = tracing_open_generic,
    1880             :         .read = event_filter_read,
    1881             :         .write = event_filter_write,
    1882             :         .llseek = default_llseek,
    1883             : };
    1884             : 
    1885             : static const struct file_operations ftrace_subsystem_filter_fops = {
    1886             :         .open = subsystem_open,
    1887             :         .read = subsystem_filter_read,
    1888             :         .write = subsystem_filter_write,
    1889             :         .llseek = default_llseek,
    1890             :         .release = subsystem_release,
    1891             : };
    1892             : 
    1893             : static const struct file_operations ftrace_system_enable_fops = {
    1894             :         .open = subsystem_open,
    1895             :         .read = system_enable_read,
    1896             :         .write = system_enable_write,
    1897             :         .llseek = default_llseek,
    1898             :         .release = subsystem_release,
    1899             : };
    1900             : 
    1901             : static const struct file_operations ftrace_tr_enable_fops = {
    1902             :         .open = system_tr_open,
    1903             :         .read = system_enable_read,
    1904             :         .write = system_enable_write,
    1905             :         .llseek = default_llseek,
    1906             :         .release = subsystem_release,
    1907             : };
    1908             : 
    1909             : static const struct file_operations ftrace_show_header_fops = {
    1910             :         .open = tracing_open_generic,
    1911             :         .read = show_header,
    1912             :         .llseek = default_llseek,
    1913             : };
    1914             : 
    1915             : static int
    1916           0 : ftrace_event_open(struct inode *inode, struct file *file,
    1917             :                   const struct seq_operations *seq_ops)
    1918             : {
    1919           0 :         struct seq_file *m;
    1920           0 :         int ret;
    1921             : 
    1922           0 :         ret = security_locked_down(LOCKDOWN_TRACEFS);
    1923           0 :         if (ret)
    1924             :                 return ret;
    1925             : 
    1926           0 :         ret = seq_open(file, seq_ops);
    1927           0 :         if (ret < 0)
    1928             :                 return ret;
    1929           0 :         m = file->private_data;
    1930             :         /* copy tr over to seq ops */
    1931           0 :         m->private = inode->i_private;
    1932             : 
    1933           0 :         return ret;
    1934             : }
    1935             : 
    1936           0 : static int ftrace_event_release(struct inode *inode, struct file *file)
    1937             : {
    1938           0 :         struct trace_array *tr = inode->i_private;
    1939             : 
    1940           0 :         trace_array_put(tr);
    1941             : 
    1942           0 :         return seq_release(inode, file);
    1943             : }
    1944             : 
    1945             : static int
    1946           0 : ftrace_event_avail_open(struct inode *inode, struct file *file)
    1947             : {
    1948           0 :         const struct seq_operations *seq_ops = &show_event_seq_ops;
    1949             : 
    1950             :         /* Checks for tracefs lockdown */
    1951           0 :         return ftrace_event_open(inode, file, seq_ops);
    1952             : }
    1953             : 
    1954             : static int
    1955           0 : ftrace_event_set_open(struct inode *inode, struct file *file)
    1956             : {
    1957           0 :         const struct seq_operations *seq_ops = &show_set_event_seq_ops;
    1958           0 :         struct trace_array *tr = inode->i_private;
    1959           0 :         int ret;
    1960             : 
    1961           0 :         ret = tracing_check_open_get_tr(tr);
    1962           0 :         if (ret)
    1963             :                 return ret;
    1964             : 
    1965           0 :         if ((file->f_mode & FMODE_WRITE) &&
    1966             :             (file->f_flags & O_TRUNC))
    1967           0 :                 ftrace_clear_events(tr);
    1968             : 
    1969           0 :         ret = ftrace_event_open(inode, file, seq_ops);
    1970           0 :         if (ret < 0)
    1971           0 :                 trace_array_put(tr);
    1972             :         return ret;
    1973             : }
    1974             : 
    1975             : static int
    1976           0 : ftrace_event_set_pid_open(struct inode *inode, struct file *file)
    1977             : {
    1978           0 :         const struct seq_operations *seq_ops = &show_set_pid_seq_ops;
    1979           0 :         struct trace_array *tr = inode->i_private;
    1980           0 :         int ret;
    1981             : 
    1982           0 :         ret = tracing_check_open_get_tr(tr);
    1983           0 :         if (ret)
    1984             :                 return ret;
    1985             : 
    1986           0 :         if ((file->f_mode & FMODE_WRITE) &&
    1987             :             (file->f_flags & O_TRUNC))
    1988           0 :                 ftrace_clear_event_pids(tr, TRACE_PIDS);
    1989             : 
    1990           0 :         ret = ftrace_event_open(inode, file, seq_ops);
    1991           0 :         if (ret < 0)
    1992           0 :                 trace_array_put(tr);
    1993             :         return ret;
    1994             : }
    1995             : 
    1996             : static int
    1997           0 : ftrace_event_set_npid_open(struct inode *inode, struct file *file)
    1998             : {
    1999           0 :         const struct seq_operations *seq_ops = &show_set_no_pid_seq_ops;
    2000           0 :         struct trace_array *tr = inode->i_private;
    2001           0 :         int ret;
    2002             : 
    2003           0 :         ret = tracing_check_open_get_tr(tr);
    2004           0 :         if (ret)
    2005             :                 return ret;
    2006             : 
    2007           0 :         if ((file->f_mode & FMODE_WRITE) &&
    2008             :             (file->f_flags & O_TRUNC))
    2009           0 :                 ftrace_clear_event_pids(tr, TRACE_NO_PIDS);
    2010             : 
    2011           0 :         ret = ftrace_event_open(inode, file, seq_ops);
    2012           0 :         if (ret < 0)
    2013           0 :                 trace_array_put(tr);
    2014             :         return ret;
    2015             : }
    2016             : 
    2017             : static struct event_subsystem *
    2018          58 : create_new_subsystem(const char *name)
    2019             : {
    2020          58 :         struct event_subsystem *system;
    2021             : 
    2022             :         /* need to create new entry */
    2023          58 :         system = kmalloc(sizeof(*system), GFP_KERNEL);
    2024          58 :         if (!system)
    2025             :                 return NULL;
    2026             : 
    2027          58 :         system->ref_count = 1;
    2028             : 
    2029             :         /* Only allocate if dynamic (kprobes and modules) */
    2030          58 :         system->name = kstrdup_const(name, GFP_KERNEL);
    2031          58 :         if (!system->name)
    2032           0 :                 goto out_free;
    2033             : 
    2034          58 :         system->filter = NULL;
    2035             : 
    2036          58 :         system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
    2037          58 :         if (!system->filter)
    2038           0 :                 goto out_free;
    2039             : 
    2040          58 :         list_add(&system->list, &event_subsystems);
    2041             : 
    2042          58 :         return system;
    2043             : 
    2044           0 :  out_free:
    2045           0 :         kfree_const(system->name);
    2046           0 :         kfree(system);
    2047           0 :         return NULL;
    2048             : }
    2049             : 
    2050             : static struct dentry *
    2051         542 : event_subsystem_dir(struct trace_array *tr, const char *name,
    2052             :                     struct trace_event_file *file, struct dentry *parent)
    2053             : {
    2054         542 :         struct trace_subsystem_dir *dir;
    2055         542 :         struct event_subsystem *system;
    2056         542 :         struct dentry *entry;
    2057             : 
    2058             :         /* First see if we did not already create this dir */
    2059        2195 :         list_for_each_entry(dir, &tr->systems, list) {
    2060        2137 :                 system = dir->subsystem;
    2061        2137 :                 if (strcmp(system->name, name) == 0) {
    2062         484 :                         dir->nr_events++;
    2063         484 :                         file->system = dir;
    2064         484 :                         return dir->entry;
    2065             :                 }
    2066             :         }
    2067             : 
    2068             :         /* Now see if the system itself exists. */
    2069        1711 :         list_for_each_entry(system, &event_subsystems, list) {
    2070        1653 :                 if (strcmp(system->name, name) == 0)
    2071             :                         break;
    2072             :         }
    2073             :         /* Reset system variable when not found */
    2074          58 :         if (&system->list == &event_subsystems)
    2075          58 :                 system = NULL;
    2076             : 
    2077          58 :         dir = kmalloc(sizeof(*dir), GFP_KERNEL);
    2078          58 :         if (!dir)
    2079           0 :                 goto out_fail;
    2080             : 
    2081          58 :         if (!system) {
    2082          58 :                 system = create_new_subsystem(name);
    2083          58 :                 if (!system)
    2084           0 :                         goto out_free;
    2085             :         } else
    2086           0 :                 __get_system(system);
    2087             : 
    2088          58 :         dir->entry = tracefs_create_dir(name, parent);
    2089          58 :         if (!dir->entry) {
    2090           0 :                 pr_warn("Failed to create system directory %s\n", name);
    2091           0 :                 __put_system(system);
    2092           0 :                 goto out_free;
    2093             :         }
    2094             : 
    2095          58 :         dir->tr = tr;
    2096          58 :         dir->ref_count = 1;
    2097          58 :         dir->nr_events = 1;
    2098          58 :         dir->subsystem = system;
    2099          58 :         file->system = dir;
    2100             : 
    2101             :         /* the ftrace system is special, do not create enable or filter files */
    2102          58 :         if (strcmp(name, "ftrace") != 0) {
    2103             : 
    2104          57 :                 entry = tracefs_create_file("filter", 0644, dir->entry, dir,
    2105             :                                             &ftrace_subsystem_filter_fops);
    2106          57 :                 if (!entry) {
    2107           0 :                         kfree(system->filter);
    2108           0 :                         system->filter = NULL;
    2109           0 :                         pr_warn("Could not create tracefs '%s/filter' entry\n", name);
    2110             :                 }
    2111             : 
    2112          57 :                 trace_create_file("enable", 0644, dir->entry, dir,
    2113             :                                   &ftrace_system_enable_fops);
    2114             :         }
    2115             : 
    2116          58 :         list_add(&dir->list, &tr->systems);
    2117             : 
    2118          58 :         return dir->entry;
    2119             : 
    2120           0 :  out_free:
    2121           0 :         kfree(dir);
    2122           0 :  out_fail:
    2123             :         /* Only print this message if failed on memory allocation */
    2124           0 :         if (!dir || !system)
    2125           0 :                 pr_warn("No memory to create event subsystem %s\n", name);
    2126             :         return NULL;
    2127             : }
    2128             : 
    2129             : static int
    2130        1084 : event_define_fields(struct trace_event_call *call)
    2131             : {
    2132        1084 :         struct list_head *head;
    2133        1084 :         int ret = 0;
    2134             : 
    2135             :         /*
    2136             :          * Other events may have the same class. Only update
    2137             :          * the fields if they are not already defined.
    2138             :          */
    2139        1084 :         head = trace_get_fields(call);
    2140        1084 :         if (list_empty(head)) {
    2141         370 :                 struct trace_event_fields *field = call->class->fields_array;
    2142         370 :                 unsigned int offset = sizeof(struct trace_entry);
    2143             : 
    2144        2048 :                 for (; field->type; field++) {
    2145        1678 :                         if (field->type == TRACE_FUNCTION_TYPE) {
    2146           0 :                                 field->define_fields(call);
    2147           0 :                                 break;
    2148             :                         }
    2149             : 
    2150        1678 :                         offset = ALIGN(offset, field->align);
    2151        1678 :                         ret = trace_define_field(call, field->type, field->name,
    2152             :                                                  offset, field->size,
    2153             :                                                  field->is_signed, field->filter_type);
    2154        1678 :                         if (WARN_ON_ONCE(ret)) {
    2155           0 :                                 pr_err("error code is %d\n", ret);
    2156           0 :                                 break;
    2157             :                         }
    2158             : 
    2159        1678 :                         offset += field->size;
    2160             :                 }
    2161             :         }
    2162             : 
    2163        1084 :         return ret;
    2164             : }
    2165             : 
    2166             : static int
    2167         542 : event_create_dir(struct dentry *parent, struct trace_event_file *file)
    2168             : {
    2169         542 :         struct trace_event_call *call = file->event_call;
    2170         542 :         struct trace_array *tr = file->tr;
    2171         542 :         struct dentry *d_events;
    2172         542 :         const char *name;
    2173         542 :         int ret;
    2174             : 
    2175             :         /*
    2176             :          * If the trace point header did not define TRACE_SYSTEM
    2177             :          * then the system would be called "TRACE_SYSTEM".
    2178             :          */
    2179         542 :         if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
    2180         542 :                 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
    2181         542 :                 if (!d_events)
    2182             :                         return -ENOMEM;
    2183             :         } else
    2184             :                 d_events = parent;
    2185             : 
    2186         542 :         name = trace_event_name(call);
    2187         542 :         file->dir = tracefs_create_dir(name, d_events);
    2188         542 :         if (!file->dir) {
    2189           0 :                 pr_warn("Could not create tracefs '%s' directory\n", name);
    2190           0 :                 return -1;
    2191             :         }
    2192             : 
    2193         542 :         if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
    2194         527 :                 trace_create_file("enable", 0644, file->dir, file,
    2195             :                                   &ftrace_enable_fops);
    2196             : 
    2197             : #ifdef CONFIG_PERF_EVENTS
    2198         542 :         if (call->event.type && call->class->reg)
    2199         528 :                 trace_create_file("id", 0444, file->dir,
    2200         528 :                                   (void *)(long)call->event.type,
    2201             :                                   &ftrace_event_id_fops);
    2202             : #endif
    2203             : 
    2204         542 :         ret = event_define_fields(call);
    2205         542 :         if (ret < 0) {
    2206           0 :                 pr_warn("Could not initialize trace point events/%s\n", name);
    2207           0 :                 return ret;
    2208             :         }
    2209             : 
    2210             :         /*
    2211             :          * Only event directories that can be enabled should have
    2212             :          * triggers or filters.
    2213             :          */
    2214         542 :         if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)) {
    2215         527 :                 trace_create_file("filter", 0644, file->dir, file,
    2216             :                                   &ftrace_event_filter_fops);
    2217             : 
    2218         527 :                 trace_create_file("trigger", 0644, file->dir, file,
    2219             :                                   &event_trigger_fops);
    2220             :         }
    2221             : 
    2222             : #ifdef CONFIG_HIST_TRIGGERS
    2223             :         trace_create_file("hist", 0444, file->dir, file,
    2224             :                           &event_hist_fops);
    2225             : #endif
    2226             : #ifdef CONFIG_HIST_TRIGGERS_DEBUG
    2227             :         trace_create_file("hist_debug", 0444, file->dir, file,
    2228             :                           &event_hist_debug_fops);
    2229             : #endif
    2230         542 :         trace_create_file("format", 0444, file->dir, call,
    2231             :                           &ftrace_event_format_fops);
    2232             : 
    2233             : #ifdef CONFIG_TRACE_EVENT_INJECT
    2234             :         if (call->event.type && call->class->reg)
    2235             :                 trace_create_file("inject", 0200, file->dir, file,
    2236             :                                   &event_inject_fops);
    2237             : #endif
    2238             : 
    2239         542 :         return 0;
    2240             : }
    2241             : 
    2242           0 : static void remove_event_from_tracers(struct trace_event_call *call)
    2243             : {
    2244           0 :         struct trace_event_file *file;
    2245           0 :         struct trace_array *tr;
    2246             : 
    2247           0 :         do_for_each_event_file_safe(tr, file) {
    2248           0 :                 if (file->event_call != call)
    2249           0 :                         continue;
    2250             : 
    2251           0 :                 remove_event_file_dir(file);
    2252             :                 /*
    2253             :                  * The do_for_each_event_file_safe() is
    2254             :                  * a double loop. After finding the call for this
    2255             :                  * trace_array, we use break to jump to the next
    2256             :                  * trace_array.
    2257             :                  */
    2258           0 :                 break;
    2259           0 :         } while_for_each_event_file();
    2260           0 : }
    2261             : 
    2262           0 : static void event_remove(struct trace_event_call *call)
    2263             : {
    2264           0 :         struct trace_array *tr;
    2265           0 :         struct trace_event_file *file;
    2266             : 
    2267           0 :         do_for_each_event_file(tr, file) {
    2268           0 :                 if (file->event_call != call)
    2269           0 :                         continue;
    2270             : 
    2271           0 :                 if (file->flags & EVENT_FILE_FL_WAS_ENABLED)
    2272           0 :                         tr->clear_trace = true;
    2273             : 
    2274           0 :                 ftrace_event_enable_disable(file, 0);
    2275             :                 /*
    2276             :                  * The do_for_each_event_file() is
    2277             :                  * a double loop. After finding the call for this
    2278             :                  * trace_array, we use break to jump to the next
    2279             :                  * trace_array.
    2280             :                  */
    2281             :                 break;
    2282           0 :         } while_for_each_event_file();
    2283             : 
    2284           0 :         if (call->event.funcs)
    2285           0 :                 __unregister_trace_event(&call->event);
    2286           0 :         remove_event_from_tracers(call);
    2287           0 :         list_del(&call->list);
    2288           0 : }
    2289             : 
    2290         542 : static int event_init(struct trace_event_call *call)
    2291             : {
    2292         542 :         int ret = 0;
    2293         542 :         const char *name;
    2294             : 
    2295         542 :         name = trace_event_name(call);
    2296         542 :         if (WARN_ON(!name))
    2297             :                 return -EINVAL;
    2298             : 
    2299         542 :         if (call->class->raw_init) {
    2300         527 :                 ret = call->class->raw_init(call);
    2301         527 :                 if (ret < 0 && ret != -ENOSYS)
    2302           0 :                         pr_warn("Could not initialize trace events/%s\n", name);
    2303             :         }
    2304             : 
    2305             :         return ret;
    2306             : }
    2307             : 
    2308             : static int
    2309           0 : __register_event(struct trace_event_call *call, struct module *mod)
    2310             : {
    2311           0 :         int ret;
    2312             : 
    2313           0 :         ret = event_init(call);
    2314           0 :         if (ret < 0)
    2315             :                 return ret;
    2316             : 
    2317           0 :         list_add(&call->list, &ftrace_events);
    2318           0 :         call->mod = mod;
    2319             : 
    2320           0 :         return 0;
    2321             : }
    2322             : 
    2323         479 : static char *eval_replace(char *ptr, struct trace_eval_map *map, int len)
    2324             : {
    2325         479 :         int rlen;
    2326         479 :         int elen;
    2327             : 
    2328             :         /* Find the length of the eval value as a string */
    2329         479 :         elen = snprintf(ptr, 0, "%ld", map->eval_value);
    2330             :         /* Make sure there's enough room to replace the string with the value */
    2331         479 :         if (len < elen)
    2332             :                 return NULL;
    2333             : 
    2334         479 :         snprintf(ptr, elen + 1, "%ld", map->eval_value);
    2335             : 
    2336             :         /* Get the rest of the string of ptr */
    2337         479 :         rlen = strlen(ptr + len);
    2338         479 :         memmove(ptr + elen, ptr + len, rlen);
    2339             :         /* Make sure we end the new string */
    2340         479 :         ptr[elen + rlen] = 0;
    2341             : 
    2342         479 :         return ptr + elen;
    2343             : }
    2344             : 
    2345        2949 : static void update_event_printk(struct trace_event_call *call,
    2346             :                                 struct trace_eval_map *map)
    2347             : {
    2348        2949 :         char *ptr;
    2349        2949 :         int quote = 0;
    2350        2949 :         int len = strlen(map->eval_string);
    2351             : 
    2352     1005338 :         for (ptr = call->print_fmt; *ptr; ptr++) {
    2353     1004624 :                 if (*ptr == '\\') {
    2354           0 :                         ptr++;
    2355             :                         /* paranoid */
    2356           0 :                         if (!*ptr)
    2357             :                                 break;
    2358           0 :                         continue;
    2359             :                 }
    2360     1004624 :                 if (*ptr == '"') {
    2361       68172 :                         quote ^= 1;
    2362       68172 :                         continue;
    2363             :                 }
    2364      936452 :                 if (quote)
    2365      476472 :                         continue;
    2366      459980 :                 if (isdigit(*ptr)) {
    2367             :                         /* skip numbers */
    2368      152110 :                         do {
    2369      152110 :                                 ptr++;
    2370             :                                 /* Check for alpha chars like ULL */
    2371      152110 :                         } while (isalnum(*ptr));
    2372       37013 :                         if (!*ptr)
    2373             :                                 break;
    2374             :                         /*
    2375             :                          * A number must have some kind of delimiter after
    2376             :                          * it, and we can ignore that too.
    2377             :                          */
    2378       36905 :                         continue;
    2379             :                 }
    2380      422967 :                 if (isalpha(*ptr) || *ptr == '_') {
    2381       65685 :                         if (strncmp(map->eval_string, ptr, len) == 0 &&
    2382         498 :                             !isalnum(ptr[len]) && ptr[len] != '_') {
    2383         479 :                                 ptr = eval_replace(ptr, map, len);
    2384             :                                 /* enum/sizeof string smaller than value */
    2385         479 :                                 if (WARN_ON_ONCE(!ptr))
    2386             :                                         return;
    2387             :                                 /*
    2388             :                                  * No need to decrement here, as eval_replace()
    2389             :                                  * returns the pointer to the character passed
    2390             :                                  * the eval, and two evals can not be placed
    2391             :                                  * back to back without something in between.
    2392             :                                  * We can skip that something in between.
    2393             :                                  */
    2394         479 :                                 continue;
    2395             :                         }
    2396       65206 :                 skip_more:
    2397      509543 :                         do {
    2398      509543 :                                 ptr++;
    2399      509543 :                         } while (isalnum(*ptr) || *ptr == '_');
    2400       80596 :                         if (!*ptr)
    2401             :                                 break;
    2402             :                         /*
    2403             :                          * If what comes after this variable is a '.' or
    2404             :                          * '->' then we can continue to ignore that string.
    2405             :                          */
    2406       78469 :                         if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
    2407       15390 :                                 ptr += *ptr == '.' ? 1 : 2;
    2408       15390 :                                 if (!*ptr)
    2409             :                                         break;
    2410       15390 :                                 goto skip_more;
    2411             :                         }
    2412             :                         /*
    2413             :                          * Once again, we can skip the delimiter that came
    2414             :                          * after the string.
    2415             :                          */
    2416       63079 :                         continue;
    2417             :                 }
    2418             :         }
    2419             : }
    2420             : 
    2421           1 : void trace_event_eval_update(struct trace_eval_map **map, int len)
    2422             : {
    2423           1 :         struct trace_event_call *call, *p;
    2424           1 :         const char *last_system = NULL;
    2425           1 :         bool first = false;
    2426           1 :         int last_i;
    2427           1 :         int i;
    2428             : 
    2429           1 :         down_write(&trace_event_sem);
    2430         543 :         list_for_each_entry_safe(call, p, &ftrace_events, list) {
    2431             :                 /* events are usually grouped together with systems */
    2432         542 :                 if (!last_system || call->class->system != last_system) {
    2433          58 :                         first = true;
    2434          58 :                         last_i = 0;
    2435          58 :                         last_system = call->class->system;
    2436             :                 }
    2437             : 
    2438             :                 /*
    2439             :                  * Since calls are grouped by systems, the likelyhood that the
    2440             :                  * next call in the iteration belongs to the same system as the
    2441             :                  * previous call is high. As an optimization, we skip searching
    2442             :                  * for a map[] that matches the call's system if the last call
    2443             :                  * was from the same system. That's what last_i is for. If the
    2444             :                  * call has the same system as the previous call, then last_i
    2445             :                  * will be the index of the first map[] that has a matching
    2446             :                  * system.
    2447             :                  */
    2448      113700 :                 for (i = last_i; i < len; i++) {
    2449      113158 :                         if (call->class->system == map[i]->system) {
    2450             :                                 /* Save the first system if need be */
    2451        2949 :                                 if (first) {
    2452          16 :                                         last_i = i;
    2453          16 :                                         first = false;
    2454             :                                 }
    2455        2949 :                                 update_event_printk(call, map[i]);
    2456             :                         }
    2457             :                 }
    2458             :         }
    2459           1 :         up_write(&trace_event_sem);
    2460           1 : }
    2461             : 
    2462             : static struct trace_event_file *
    2463         542 : trace_create_new_event(struct trace_event_call *call,
    2464             :                        struct trace_array *tr)
    2465             : {
    2466         542 :         struct trace_event_file *file;
    2467             : 
    2468         542 :         file = kmem_cache_alloc(file_cachep, GFP_TRACE);
    2469         542 :         if (!file)
    2470             :                 return NULL;
    2471             : 
    2472         542 :         file->event_call = call;
    2473         542 :         file->tr = tr;
    2474         542 :         atomic_set(&file->sm_ref, 0);
    2475         542 :         atomic_set(&file->tm_ref, 0);
    2476         542 :         INIT_LIST_HEAD(&file->triggers);
    2477         542 :         list_add(&file->list, &tr->events);
    2478             : 
    2479         542 :         return file;
    2480             : }
    2481             : 
    2482             : /* Add an event to a trace directory */
    2483             : static int
    2484           0 : __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
    2485             : {
    2486           0 :         struct trace_event_file *file;
    2487             : 
    2488           0 :         file = trace_create_new_event(call, tr);
    2489           0 :         if (!file)
    2490             :                 return -ENOMEM;
    2491             : 
    2492           0 :         if (eventdir_initialized)
    2493           0 :                 return event_create_dir(tr->event_dir, file);
    2494             :         else
    2495           0 :                 return event_define_fields(call);
    2496             : }
    2497             : 
    2498             : /*
    2499             :  * Just create a decriptor for early init. A descriptor is required
    2500             :  * for enabling events at boot. We want to enable events before
    2501             :  * the filesystem is initialized.
    2502             :  */
    2503             : static int
    2504         542 : __trace_early_add_new_event(struct trace_event_call *call,
    2505             :                             struct trace_array *tr)
    2506             : {
    2507         542 :         struct trace_event_file *file;
    2508             : 
    2509         542 :         file = trace_create_new_event(call, tr);
    2510         542 :         if (!file)
    2511             :                 return -ENOMEM;
    2512             : 
    2513         542 :         return event_define_fields(call);
    2514             : }
    2515             : 
    2516             : struct ftrace_module_file_ops;
    2517             : static void __add_event_to_tracers(struct trace_event_call *call);
    2518             : 
    2519             : /* Add an additional event_call dynamically */
    2520           0 : int trace_add_event_call(struct trace_event_call *call)
    2521             : {
    2522           0 :         int ret;
    2523           0 :         lockdep_assert_held(&event_mutex);
    2524             : 
    2525           0 :         mutex_lock(&trace_types_lock);
    2526             : 
    2527           0 :         ret = __register_event(call, NULL);
    2528           0 :         if (ret >= 0)
    2529           0 :                 __add_event_to_tracers(call);
    2530             : 
    2531           0 :         mutex_unlock(&trace_types_lock);
    2532           0 :         return ret;
    2533             : }
    2534             : 
    2535             : /*
    2536             :  * Must be called under locking of trace_types_lock, event_mutex and
    2537             :  * trace_event_sem.
    2538             :  */
    2539           0 : static void __trace_remove_event_call(struct trace_event_call *call)
    2540             : {
    2541           0 :         event_remove(call);
    2542           0 :         trace_destroy_fields(call);
    2543           0 :         free_event_filter(call->filter);
    2544           0 :         call->filter = NULL;
    2545           0 : }
    2546             : 
    2547           0 : static int probe_remove_event_call(struct trace_event_call *call)
    2548             : {
    2549           0 :         struct trace_array *tr;
    2550           0 :         struct trace_event_file *file;
    2551             : 
    2552             : #ifdef CONFIG_PERF_EVENTS
    2553           0 :         if (call->perf_refcount)
    2554             :                 return -EBUSY;
    2555             : #endif
    2556           0 :         do_for_each_event_file(tr, file) {
    2557           0 :                 if (file->event_call != call)
    2558           0 :                         continue;
    2559             :                 /*
    2560             :                  * We can't rely on ftrace_event_enable_disable(enable => 0)
    2561             :                  * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
    2562             :                  * TRACE_REG_UNREGISTER.
    2563             :                  */
    2564           0 :                 if (file->flags & EVENT_FILE_FL_ENABLED)
    2565             :                         return -EBUSY;
    2566             :                 /*
    2567             :                  * The do_for_each_event_file_safe() is
    2568             :                  * a double loop. After finding the call for this
    2569             :                  * trace_array, we use break to jump to the next
    2570             :                  * trace_array.
    2571             :                  */
    2572             :                 break;
    2573           0 :         } while_for_each_event_file();
    2574             : 
    2575           0 :         __trace_remove_event_call(call);
    2576             : 
    2577           0 :         return 0;
    2578             : }
    2579             : 
    2580             : /* Remove an event_call */
    2581           0 : int trace_remove_event_call(struct trace_event_call *call)
    2582             : {
    2583           0 :         int ret;
    2584             : 
    2585           0 :         lockdep_assert_held(&event_mutex);
    2586             : 
    2587           0 :         mutex_lock(&trace_types_lock);
    2588           0 :         down_write(&trace_event_sem);
    2589           0 :         ret = probe_remove_event_call(call);
    2590           0 :         up_write(&trace_event_sem);
    2591           0 :         mutex_unlock(&trace_types_lock);
    2592             : 
    2593           0 :         return ret;
    2594             : }
    2595             : 
    2596             : #define for_each_event(event, start, end)                       \
    2597             :         for (event = start;                                     \
    2598             :              (unsigned long)event < (unsigned long)end;              \
    2599             :              event++)
    2600             : 
    2601             : #ifdef CONFIG_MODULES
    2602             : 
    2603             : static void trace_module_add_events(struct module *mod)
    2604             : {
    2605             :         struct trace_event_call **call, **start, **end;
    2606             : 
    2607             :         if (!mod->num_trace_events)
    2608             :                 return;
    2609             : 
    2610             :         /* Don't add infrastructure for mods without tracepoints */
    2611             :         if (trace_module_has_bad_taint(mod)) {
    2612             :                 pr_err("%s: module has bad taint, not creating trace events\n",
    2613             :                        mod->name);
    2614             :                 return;
    2615             :         }
    2616             : 
    2617             :         start = mod->trace_events;
    2618             :         end = mod->trace_events + mod->num_trace_events;
    2619             : 
    2620             :         for_each_event(call, start, end) {
    2621             :                 __register_event(*call, mod);
    2622             :                 __add_event_to_tracers(*call);
    2623             :         }
    2624             : }
    2625             : 
    2626             : static void trace_module_remove_events(struct module *mod)
    2627             : {
    2628             :         struct trace_event_call *call, *p;
    2629             : 
    2630             :         down_write(&trace_event_sem);
    2631             :         list_for_each_entry_safe(call, p, &ftrace_events, list) {
    2632             :                 if (call->mod == mod)
    2633             :                         __trace_remove_event_call(call);
    2634             :         }
    2635             :         up_write(&trace_event_sem);
    2636             : 
    2637             :         /*
    2638             :          * It is safest to reset the ring buffer if the module being unloaded
    2639             :          * registered any events that were used. The only worry is if
    2640             :          * a new module gets loaded, and takes on the same id as the events
    2641             :          * of this module. When printing out the buffer, traced events left
    2642             :          * over from this module may be passed to the new module events and
    2643             :          * unexpected results may occur.
    2644             :          */
    2645             :         tracing_reset_all_online_cpus();
    2646             : }
    2647             : 
    2648             : static int trace_module_notify(struct notifier_block *self,
    2649             :                                unsigned long val, void *data)
    2650             : {
    2651             :         struct module *mod = data;
    2652             : 
    2653             :         mutex_lock(&event_mutex);
    2654             :         mutex_lock(&trace_types_lock);
    2655             :         switch (val) {
    2656             :         case MODULE_STATE_COMING:
    2657             :                 trace_module_add_events(mod);
    2658             :                 break;
    2659             :         case MODULE_STATE_GOING:
    2660             :                 trace_module_remove_events(mod);
    2661             :                 break;
    2662             :         }
    2663             :         mutex_unlock(&trace_types_lock);
    2664             :         mutex_unlock(&event_mutex);
    2665             : 
    2666             :         return NOTIFY_OK;
    2667             : }
    2668             : 
    2669             : static struct notifier_block trace_module_nb = {
    2670             :         .notifier_call = trace_module_notify,
    2671             :         .priority = 1, /* higher than trace.c module notify */
    2672             : };
    2673             : #endif /* CONFIG_MODULES */
    2674             : 
    2675             : /* Create a new event directory structure for a trace directory. */
    2676             : static void
    2677           0 : __trace_add_event_dirs(struct trace_array *tr)
    2678             : {
    2679           0 :         struct trace_event_call *call;
    2680           0 :         int ret;
    2681             : 
    2682           0 :         list_for_each_entry(call, &ftrace_events, list) {
    2683           0 :                 ret = __trace_add_new_event(call, tr);
    2684           0 :                 if (ret < 0)
    2685           0 :                         pr_warn("Could not create directory for event %s\n",
    2686             :                                 trace_event_name(call));
    2687             :         }
    2688           0 : }
    2689             : 
    2690             : /* Returns any file that matches the system and event */
    2691             : struct trace_event_file *
    2692           1 : __find_event_file(struct trace_array *tr, const char *system, const char *event)
    2693             : {
    2694           1 :         struct trace_event_file *file;
    2695           1 :         struct trace_event_call *call;
    2696           1 :         const char *name;
    2697             : 
    2698         142 :         list_for_each_entry(file, &tr->events, list) {
    2699             : 
    2700         142 :                 call = file->event_call;
    2701         142 :                 name = trace_event_name(call);
    2702             : 
    2703         142 :                 if (!name || !call->class)
    2704           0 :                         continue;
    2705             : 
    2706         142 :                 if (strcmp(event, name) == 0 &&
    2707           1 :                     strcmp(system, call->class->system) == 0)
    2708           1 :                         return file;
    2709             :         }
    2710             :         return NULL;
    2711             : }
    2712             : 
    2713             : /* Returns valid trace event files that match system and event */
    2714             : struct trace_event_file *
    2715           0 : find_event_file(struct trace_array *tr, const char *system, const char *event)
    2716             : {
    2717           0 :         struct trace_event_file *file;
    2718             : 
    2719           0 :         file = __find_event_file(tr, system, event);
    2720           0 :         if (!file || !file->event_call->class->reg ||
    2721           0 :             file->event_call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
    2722           0 :                 return NULL;
    2723             : 
    2724             :         return file;
    2725             : }
    2726             : 
    2727             : /**
    2728             :  * trace_get_event_file - Find and return a trace event file
    2729             :  * @instance: The name of the trace instance containing the event
    2730             :  * @system: The name of the system containing the event
    2731             :  * @event: The name of the event
    2732             :  *
    2733             :  * Return a trace event file given the trace instance name, trace
    2734             :  * system, and trace event name.  If the instance name is NULL, it
    2735             :  * refers to the top-level trace array.
    2736             :  *
    2737             :  * This function will look it up and return it if found, after calling
    2738             :  * trace_array_get() to prevent the instance from going away, and
    2739             :  * increment the event's module refcount to prevent it from being
    2740             :  * removed.
    2741             :  *
    2742             :  * To release the file, call trace_put_event_file(), which will call
    2743             :  * trace_array_put() and decrement the event's module refcount.
    2744             :  *
    2745             :  * Return: The trace event on success, ERR_PTR otherwise.
    2746             :  */
    2747           0 : struct trace_event_file *trace_get_event_file(const char *instance,
    2748             :                                               const char *system,
    2749             :                                               const char *event)
    2750             : {
    2751           0 :         struct trace_array *tr = top_trace_array();
    2752           0 :         struct trace_event_file *file = NULL;
    2753           0 :         int ret = -EINVAL;
    2754             : 
    2755           0 :         if (instance) {
    2756           0 :                 tr = trace_array_find_get(instance);
    2757           0 :                 if (!tr)
    2758           0 :                         return ERR_PTR(-ENOENT);
    2759             :         } else {
    2760           0 :                 ret = trace_array_get(tr);
    2761           0 :                 if (ret)
    2762           0 :                         return ERR_PTR(ret);
    2763             :         }
    2764             : 
    2765           0 :         mutex_lock(&event_mutex);
    2766             : 
    2767           0 :         file = find_event_file(tr, system, event);
    2768           0 :         if (!file) {
    2769           0 :                 trace_array_put(tr);
    2770           0 :                 ret = -EINVAL;
    2771           0 :                 goto out;
    2772             :         }
    2773             : 
    2774             :         /* Don't let event modules unload while in use */
    2775           0 :         ret = try_module_get(file->event_call->mod);
    2776             :         if (!ret) {
    2777             :                 trace_array_put(tr);
    2778             :                 ret = -EBUSY;
    2779             :                 goto out;
    2780             :         }
    2781             : 
    2782           0 :         ret = 0;
    2783           0 :  out:
    2784           0 :         mutex_unlock(&event_mutex);
    2785             : 
    2786           0 :         if (ret)
    2787           0 :                 file = ERR_PTR(ret);
    2788             : 
    2789             :         return file;
    2790             : }
    2791             : EXPORT_SYMBOL_GPL(trace_get_event_file);
    2792             : 
    2793             : /**
    2794             :  * trace_put_event_file - Release a file from trace_get_event_file()
    2795             :  * @file: The trace event file
    2796             :  *
    2797             :  * If a file was retrieved using trace_get_event_file(), this should
    2798             :  * be called when it's no longer needed.  It will cancel the previous
    2799             :  * trace_array_get() called by that function, and decrement the
    2800             :  * event's module refcount.
    2801             :  */
    2802           0 : void trace_put_event_file(struct trace_event_file *file)
    2803             : {
    2804           0 :         mutex_lock(&event_mutex);
    2805           0 :         module_put(file->event_call->mod);
    2806           0 :         mutex_unlock(&event_mutex);
    2807             : 
    2808           0 :         trace_array_put(file->tr);
    2809           0 : }
    2810             : EXPORT_SYMBOL_GPL(trace_put_event_file);
    2811             : 
    2812             : #ifdef CONFIG_DYNAMIC_FTRACE
    2813             : 
    2814             : /* Avoid typos */
    2815             : #define ENABLE_EVENT_STR        "enable_event"
    2816             : #define DISABLE_EVENT_STR       "disable_event"
    2817             : 
    2818             : struct event_probe_data {
    2819             :         struct trace_event_file *file;
    2820             :         unsigned long                   count;
    2821             :         int                             ref;
    2822             :         bool                            enable;
    2823             : };
    2824             : 
    2825             : static void update_event_probe(struct event_probe_data *data)
    2826             : {
    2827             :         if (data->enable)
    2828             :                 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
    2829             :         else
    2830             :                 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
    2831             : }
    2832             : 
    2833             : static void
    2834             : event_enable_probe(unsigned long ip, unsigned long parent_ip,
    2835             :                    struct trace_array *tr, struct ftrace_probe_ops *ops,
    2836             :                    void *data)
    2837             : {
    2838             :         struct ftrace_func_mapper *mapper = data;
    2839             :         struct event_probe_data *edata;
    2840             :         void **pdata;
    2841             : 
    2842             :         pdata = ftrace_func_mapper_find_ip(mapper, ip);
    2843             :         if (!pdata || !*pdata)
    2844             :                 return;
    2845             : 
    2846             :         edata = *pdata;
    2847             :         update_event_probe(edata);
    2848             : }
    2849             : 
    2850             : static void
    2851             : event_enable_count_probe(unsigned long ip, unsigned long parent_ip,
    2852             :                          struct trace_array *tr, struct ftrace_probe_ops *ops,
    2853             :                          void *data)
    2854             : {
    2855             :         struct ftrace_func_mapper *mapper = data;
    2856             :         struct event_probe_data *edata;
    2857             :         void **pdata;
    2858             : 
    2859             :         pdata = ftrace_func_mapper_find_ip(mapper, ip);
    2860             :         if (!pdata || !*pdata)
    2861             :                 return;
    2862             : 
    2863             :         edata = *pdata;
    2864             : 
    2865             :         if (!edata->count)
    2866             :                 return;
    2867             : 
    2868             :         /* Skip if the event is in a state we want to switch to */
    2869             :         if (edata->enable == !(edata->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
    2870             :                 return;
    2871             : 
    2872             :         if (edata->count != -1)
    2873             :                 (edata->count)--;
    2874             : 
    2875             :         update_event_probe(edata);
    2876             : }
    2877             : 
    2878             : static int
    2879             : event_enable_print(struct seq_file *m, unsigned long ip,
    2880             :                    struct ftrace_probe_ops *ops, void *data)
    2881             : {
    2882             :         struct ftrace_func_mapper *mapper = data;
    2883             :         struct event_probe_data *edata;
    2884             :         void **pdata;
    2885             : 
    2886             :         pdata = ftrace_func_mapper_find_ip(mapper, ip);
    2887             : 
    2888             :         if (WARN_ON_ONCE(!pdata || !*pdata))
    2889             :                 return 0;
    2890             : 
    2891             :         edata = *pdata;
    2892             : 
    2893             :         seq_printf(m, "%ps:", (void *)ip);
    2894             : 
    2895             :         seq_printf(m, "%s:%s:%s",
    2896             :                    edata->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
    2897             :                    edata->file->event_call->class->system,
    2898             :                    trace_event_name(edata->file->event_call));
    2899             : 
    2900             :         if (edata->count == -1)
    2901             :                 seq_puts(m, ":unlimited\n");
    2902             :         else
    2903             :                 seq_printf(m, ":count=%ld\n", edata->count);
    2904             : 
    2905             :         return 0;
    2906             : }
    2907             : 
    2908             : static int
    2909             : event_enable_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
    2910             :                   unsigned long ip, void *init_data, void **data)
    2911             : {
    2912             :         struct ftrace_func_mapper *mapper = *data;
    2913             :         struct event_probe_data *edata = init_data;
    2914             :         int ret;
    2915             : 
    2916             :         if (!mapper) {
    2917             :                 mapper = allocate_ftrace_func_mapper();
    2918             :                 if (!mapper)
    2919             :                         return -ENODEV;
    2920             :                 *data = mapper;
    2921             :         }
    2922             : 
    2923             :         ret = ftrace_func_mapper_add_ip(mapper, ip, edata);
    2924             :         if (ret < 0)
    2925             :                 return ret;
    2926             : 
    2927             :         edata->ref++;
    2928             : 
    2929             :         return 0;
    2930             : }
    2931             : 
    2932             : static int free_probe_data(void *data)
    2933             : {
    2934             :         struct event_probe_data *edata = data;
    2935             : 
    2936             :         edata->ref--;
    2937             :         if (!edata->ref) {
    2938             :                 /* Remove the SOFT_MODE flag */
    2939             :                 __ftrace_event_enable_disable(edata->file, 0, 1);
    2940             :                 module_put(edata->file->event_call->mod);
    2941             :                 kfree(edata);
    2942             :         }
    2943             :         return 0;
    2944             : }
    2945             : 
    2946             : static void
    2947             : event_enable_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
    2948             :                   unsigned long ip, void *data)
    2949             : {
    2950             :         struct ftrace_func_mapper *mapper = data;
    2951             :         struct event_probe_data *edata;
    2952             : 
    2953             :         if (!ip) {
    2954             :                 if (!mapper)
    2955             :                         return;
    2956             :                 free_ftrace_func_mapper(mapper, free_probe_data);
    2957             :                 return;
    2958             :         }
    2959             : 
    2960             :         edata = ftrace_func_mapper_remove_ip(mapper, ip);
    2961             : 
    2962             :         if (WARN_ON_ONCE(!edata))
    2963             :                 return;
    2964             : 
    2965             :         if (WARN_ON_ONCE(edata->ref <= 0))
    2966             :                 return;
    2967             : 
    2968             :         free_probe_data(edata);
    2969             : }
    2970             : 
    2971             : static struct ftrace_probe_ops event_enable_probe_ops = {
    2972             :         .func                   = event_enable_probe,
    2973             :         .print                  = event_enable_print,
    2974             :         .init                   = event_enable_init,
    2975             :         .free                   = event_enable_free,
    2976             : };
    2977             : 
    2978             : static struct ftrace_probe_ops event_enable_count_probe_ops = {
    2979             :         .func                   = event_enable_count_probe,
    2980             :         .print                  = event_enable_print,
    2981             :         .init                   = event_enable_init,
    2982             :         .free                   = event_enable_free,
    2983             : };
    2984             : 
    2985             : static struct ftrace_probe_ops event_disable_probe_ops = {
    2986             :         .func                   = event_enable_probe,
    2987             :         .print                  = event_enable_print,
    2988             :         .init                   = event_enable_init,
    2989             :         .free                   = event_enable_free,
    2990             : };
    2991             : 
    2992             : static struct ftrace_probe_ops event_disable_count_probe_ops = {
    2993             :         .func                   = event_enable_count_probe,
    2994             :         .print                  = event_enable_print,
    2995             :         .init                   = event_enable_init,
    2996             :         .free                   = event_enable_free,
    2997             : };
    2998             : 
    2999             : static int
    3000             : event_enable_func(struct trace_array *tr, struct ftrace_hash *hash,
    3001             :                   char *glob, char *cmd, char *param, int enabled)
    3002             : {
    3003             :         struct trace_event_file *file;
    3004             :         struct ftrace_probe_ops *ops;
    3005             :         struct event_probe_data *data;
    3006             :         const char *system;
    3007             :         const char *event;
    3008             :         char *number;
    3009             :         bool enable;
    3010             :         int ret;
    3011             : 
    3012             :         if (!tr)
    3013             :                 return -ENODEV;
    3014             : 
    3015             :         /* hash funcs only work with set_ftrace_filter */
    3016             :         if (!enabled || !param)
    3017             :                 return -EINVAL;
    3018             : 
    3019             :         system = strsep(&param, ":");
    3020             :         if (!param)
    3021             :                 return -EINVAL;
    3022             : 
    3023             :         event = strsep(&param, ":");
    3024             : 
    3025             :         mutex_lock(&event_mutex);
    3026             : 
    3027             :         ret = -EINVAL;
    3028             :         file = find_event_file(tr, system, event);
    3029             :         if (!file)
    3030             :                 goto out;
    3031             : 
    3032             :         enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
    3033             : 
    3034             :         if (enable)
    3035             :                 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
    3036             :         else
    3037             :                 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
    3038             : 
    3039             :         if (glob[0] == '!') {
    3040             :                 ret = unregister_ftrace_function_probe_func(glob+1, tr, ops);
    3041             :                 goto out;
    3042             :         }
    3043             : 
    3044             :         ret = -ENOMEM;
    3045             : 
    3046             :         data = kzalloc(sizeof(*data), GFP_KERNEL);
    3047             :         if (!data)
    3048             :                 goto out;
    3049             : 
    3050             :         data->enable = enable;
    3051             :         data->count = -1;
    3052             :         data->file = file;
    3053             : 
    3054             :         if (!param)
    3055             :                 goto out_reg;
    3056             : 
    3057             :         number = strsep(&param, ":");
    3058             : 
    3059             :         ret = -EINVAL;
    3060             :         if (!strlen(number))
    3061             :                 goto out_free;
    3062             : 
    3063             :         /*
    3064             :          * We use the callback data field (which is a pointer)
    3065             :          * as our counter.
    3066             :          */
    3067             :         ret = kstrtoul(number, 0, &data->count);
    3068             :         if (ret)
    3069             :                 goto out_free;
    3070             : 
    3071             :  out_reg:
    3072             :         /* Don't let event modules unload while probe registered */
    3073             :         ret = try_module_get(file->event_call->mod);
    3074             :         if (!ret) {
    3075             :                 ret = -EBUSY;
    3076             :                 goto out_free;
    3077             :         }
    3078             : 
    3079             :         ret = __ftrace_event_enable_disable(file, 1, 1);
    3080             :         if (ret < 0)
    3081             :                 goto out_put;
    3082             : 
    3083             :         ret = register_ftrace_function_probe(glob, tr, ops, data);
    3084             :         /*
    3085             :          * The above returns on success the # of functions enabled,
    3086             :          * but if it didn't find any functions it returns zero.
    3087             :          * Consider no functions a failure too.
    3088             :          */
    3089             :         if (!ret) {
    3090             :                 ret = -ENOENT;
    3091             :                 goto out_disable;
    3092             :         } else if (ret < 0)
    3093             :                 goto out_disable;
    3094             :         /* Just return zero, not the number of enabled functions */
    3095             :         ret = 0;
    3096             :  out:
    3097             :         mutex_unlock(&event_mutex);
    3098             :         return ret;
    3099             : 
    3100             :  out_disable:
    3101             :         __ftrace_event_enable_disable(file, 0, 1);
    3102             :  out_put:
    3103             :         module_put(file->event_call->mod);
    3104             :  out_free:
    3105             :         kfree(data);
    3106             :         goto out;
    3107             : }
    3108             : 
    3109             : static struct ftrace_func_command event_enable_cmd = {
    3110             :         .name                   = ENABLE_EVENT_STR,
    3111             :         .func                   = event_enable_func,
    3112             : };
    3113             : 
    3114             : static struct ftrace_func_command event_disable_cmd = {
    3115             :         .name                   = DISABLE_EVENT_STR,
    3116             :         .func                   = event_enable_func,
    3117             : };
    3118             : 
    3119             : static __init int register_event_cmds(void)
    3120             : {
    3121             :         int ret;
    3122             : 
    3123             :         ret = register_ftrace_command(&event_enable_cmd);
    3124             :         if (WARN_ON(ret < 0))
    3125             :                 return ret;
    3126             :         ret = register_ftrace_command(&event_disable_cmd);
    3127             :         if (WARN_ON(ret < 0))
    3128             :                 unregister_ftrace_command(&event_enable_cmd);
    3129             :         return ret;
    3130             : }
    3131             : #else
    3132           1 : static inline int register_event_cmds(void) { return 0; }
    3133             : #endif /* CONFIG_DYNAMIC_FTRACE */
    3134             : 
    3135             : /*
    3136             :  * The top level array and trace arrays created by boot-time tracing
    3137             :  * have already had its trace_event_file descriptors created in order
    3138             :  * to allow for early events to be recorded.
    3139             :  * This function is called after the tracefs has been initialized,
    3140             :  * and we now have to create the files associated to the events.
    3141             :  */
    3142           1 : static void __trace_early_add_event_dirs(struct trace_array *tr)
    3143             : {
    3144           1 :         struct trace_event_file *file;
    3145           1 :         int ret;
    3146             : 
    3147             : 
    3148         543 :         list_for_each_entry(file, &tr->events, list) {
    3149         542 :                 ret = event_create_dir(tr->event_dir, file);
    3150         542 :                 if (ret < 0)
    3151           0 :                         pr_warn("Could not create directory for event %s\n",
    3152             :                                 trace_event_name(file->event_call));
    3153             :         }
    3154           1 : }
    3155             : 
    3156             : /*
    3157             :  * For early boot up, the top trace array and the trace arrays created
    3158             :  * by boot-time tracing require to have a list of events that can be
    3159             :  * enabled. This must be done before the filesystem is set up in order
    3160             :  * to allow events to be traced early.
    3161             :  */
    3162           1 : void __trace_early_add_events(struct trace_array *tr)
    3163             : {
    3164           1 :         struct trace_event_call *call;
    3165           1 :         int ret;
    3166             : 
    3167         543 :         list_for_each_entry(call, &ftrace_events, list) {
    3168             :                 /* Early boot up should not have any modules loaded */
    3169         542 :                 if (WARN_ON_ONCE(call->mod))
    3170           0 :                         continue;
    3171             : 
    3172         542 :                 ret = __trace_early_add_new_event(call, tr);
    3173         542 :                 if (ret < 0)
    3174           0 :                         pr_warn("Could not create early event %s\n",
    3175             :                                 trace_event_name(call));
    3176             :         }
    3177           1 : }
    3178             : 
    3179             : /* Remove the event directory structure for a trace directory. */
    3180             : static void
    3181           0 : __trace_remove_event_dirs(struct trace_array *tr)
    3182             : {
    3183           0 :         struct trace_event_file *file, *next;
    3184             : 
    3185           0 :         list_for_each_entry_safe(file, next, &tr->events, list)
    3186           0 :                 remove_event_file_dir(file);
    3187           0 : }
    3188             : 
    3189           0 : static void __add_event_to_tracers(struct trace_event_call *call)
    3190             : {
    3191           0 :         struct trace_array *tr;
    3192             : 
    3193           0 :         list_for_each_entry(tr, &ftrace_trace_arrays, list)
    3194           0 :                 __trace_add_new_event(call, tr);
    3195           0 : }
    3196             : 
    3197             : extern struct trace_event_call *__start_ftrace_events[];
    3198             : extern struct trace_event_call *__stop_ftrace_events[];
    3199             : 
    3200             : static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
    3201             : 
    3202           0 : static __init int setup_trace_event(char *str)
    3203             : {
    3204           0 :         strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
    3205           0 :         ring_buffer_expanded = true;
    3206           0 :         disable_tracing_selftest("running event tracing");
    3207             : 
    3208           0 :         return 1;
    3209             : }
    3210             : __setup("trace_event=", setup_trace_event);
    3211             : 
    3212             : /* Expects to have event_mutex held when called */
    3213             : static int
    3214           1 : create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
    3215             : {
    3216           1 :         struct dentry *d_events;
    3217           1 :         struct dentry *entry;
    3218             : 
    3219           1 :         entry = tracefs_create_file("set_event", 0644, parent,
    3220             :                                     tr, &ftrace_set_event_fops);
    3221           1 :         if (!entry) {
    3222           0 :                 pr_warn("Could not create tracefs 'set_event' entry\n");
    3223           0 :                 return -ENOMEM;
    3224             :         }
    3225             : 
    3226           1 :         d_events = tracefs_create_dir("events", parent);
    3227           1 :         if (!d_events) {
    3228           0 :                 pr_warn("Could not create tracefs 'events' directory\n");
    3229           0 :                 return -ENOMEM;
    3230             :         }
    3231             : 
    3232           1 :         entry = trace_create_file("enable", 0644, d_events,
    3233             :                                   tr, &ftrace_tr_enable_fops);
    3234           1 :         if (!entry) {
    3235           0 :                 pr_warn("Could not create tracefs 'enable' entry\n");
    3236           0 :                 return -ENOMEM;
    3237             :         }
    3238             : 
    3239             :         /* There are not as crucial, just warn if they are not created */
    3240             : 
    3241           1 :         entry = tracefs_create_file("set_event_pid", 0644, parent,
    3242             :                                     tr, &ftrace_set_event_pid_fops);
    3243           1 :         if (!entry)
    3244           0 :                 pr_warn("Could not create tracefs 'set_event_pid' entry\n");
    3245             : 
    3246           1 :         entry = tracefs_create_file("set_event_notrace_pid", 0644, parent,
    3247             :                                     tr, &ftrace_set_event_notrace_pid_fops);
    3248           1 :         if (!entry)
    3249           0 :                 pr_warn("Could not create tracefs 'set_event_notrace_pid' entry\n");
    3250             : 
    3251             :         /* ring buffer internal formats */
    3252           1 :         entry = trace_create_file("header_page", 0444, d_events,
    3253             :                                   ring_buffer_print_page_header,
    3254             :                                   &ftrace_show_header_fops);
    3255           1 :         if (!entry)
    3256           0 :                 pr_warn("Could not create tracefs 'header_page' entry\n");
    3257             : 
    3258           1 :         entry = trace_create_file("header_event", 0444, d_events,
    3259             :                                   ring_buffer_print_entry_header,
    3260             :                                   &ftrace_show_header_fops);
    3261           1 :         if (!entry)
    3262           0 :                 pr_warn("Could not create tracefs 'header_event' entry\n");
    3263             : 
    3264           1 :         tr->event_dir = d_events;
    3265             : 
    3266           1 :         return 0;
    3267             : }
    3268             : 
    3269             : /**
    3270             :  * event_trace_add_tracer - add a instance of a trace_array to events
    3271             :  * @parent: The parent dentry to place the files/directories for events in
    3272             :  * @tr: The trace array associated with these events
    3273             :  *
    3274             :  * When a new instance is created, it needs to set up its events
    3275             :  * directory, as well as other files associated with events. It also
    3276             :  * creates the event hierarchy in the @parent/events directory.
    3277             :  *
    3278             :  * Returns 0 on success.
    3279             :  *
    3280             :  * Must be called with event_mutex held.
    3281             :  */
    3282           0 : int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
    3283             : {
    3284           0 :         int ret;
    3285             : 
    3286           0 :         lockdep_assert_held(&event_mutex);
    3287             : 
    3288           0 :         ret = create_event_toplevel_files(parent, tr);
    3289           0 :         if (ret)
    3290           0 :                 goto out;
    3291             : 
    3292           0 :         down_write(&trace_event_sem);
    3293             :         /* If tr already has the event list, it is initialized in early boot. */
    3294           0 :         if (unlikely(!list_empty(&tr->events)))
    3295           0 :                 __trace_early_add_event_dirs(tr);
    3296             :         else
    3297           0 :                 __trace_add_event_dirs(tr);
    3298           0 :         up_write(&trace_event_sem);
    3299             : 
    3300           0 :  out:
    3301           0 :         return ret;
    3302             : }
    3303             : 
    3304             : /*
    3305             :  * The top trace array already had its file descriptors created.
    3306             :  * Now the files themselves need to be created.
    3307             :  */
    3308             : static __init int
    3309           1 : early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
    3310             : {
    3311           1 :         int ret;
    3312             : 
    3313           1 :         mutex_lock(&event_mutex);
    3314             : 
    3315           1 :         ret = create_event_toplevel_files(parent, tr);
    3316           1 :         if (ret)
    3317           0 :                 goto out_unlock;
    3318             : 
    3319           1 :         down_write(&trace_event_sem);
    3320           1 :         __trace_early_add_event_dirs(tr);
    3321           1 :         up_write(&trace_event_sem);
    3322             : 
    3323           1 :  out_unlock:
    3324           1 :         mutex_unlock(&event_mutex);
    3325             : 
    3326           1 :         return ret;
    3327             : }
    3328             : 
    3329             : /* Must be called with event_mutex held */
    3330           0 : int event_trace_del_tracer(struct trace_array *tr)
    3331             : {
    3332           0 :         lockdep_assert_held(&event_mutex);
    3333             : 
    3334             :         /* Disable any event triggers and associated soft-disabled events */
    3335           0 :         clear_event_triggers(tr);
    3336             : 
    3337             :         /* Clear the pid list */
    3338           0 :         __ftrace_clear_event_pids(tr, TRACE_PIDS | TRACE_NO_PIDS);
    3339             : 
    3340             :         /* Disable any running events */
    3341           0 :         __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
    3342             : 
    3343             :         /* Make sure no more events are being executed */
    3344           0 :         tracepoint_synchronize_unregister();
    3345             : 
    3346           0 :         down_write(&trace_event_sem);
    3347           0 :         __trace_remove_event_dirs(tr);
    3348           0 :         tracefs_remove(tr->event_dir);
    3349           0 :         up_write(&trace_event_sem);
    3350             : 
    3351           0 :         tr->event_dir = NULL;
    3352             : 
    3353           0 :         return 0;
    3354             : }
    3355             : 
    3356           1 : static __init int event_trace_memsetup(void)
    3357             : {
    3358           1 :         field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
    3359           1 :         file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
    3360           1 :         return 0;
    3361             : }
    3362             : 
    3363             : static __init void
    3364           2 : early_enable_events(struct trace_array *tr, bool disable_first)
    3365             : {
    3366           2 :         char *buf = bootup_event_buf;
    3367           4 :         char *token;
    3368           4 :         int ret;
    3369             : 
    3370           4 :         while (true) {
    3371           4 :                 token = strsep(&buf, ",");
    3372             : 
    3373           4 :                 if (!token)
    3374             :                         break;
    3375             : 
    3376           2 :                 if (*token) {
    3377             :                         /* Restarting syscalls requires that we stop them first */
    3378           0 :                         if (disable_first)
    3379           0 :                                 ftrace_set_clr_event(tr, token, 0);
    3380             : 
    3381           0 :                         ret = ftrace_set_clr_event(tr, token, 1);
    3382           0 :                         if (ret)
    3383           0 :                                 pr_warn("Failed to enable trace event: %s\n", token);
    3384             :                 }
    3385             : 
    3386             :                 /* Put back the comma to allow this to be called again */
    3387           2 :                 if (buf)
    3388           0 :                         *(buf - 1) = ',';
    3389             :         }
    3390           2 : }
    3391             : 
    3392           1 : static __init int event_trace_enable(void)
    3393             : {
    3394           1 :         struct trace_array *tr = top_trace_array();
    3395           1 :         struct trace_event_call **iter, *call;
    3396           1 :         int ret;
    3397             : 
    3398           1 :         if (!tr)
    3399             :                 return -ENODEV;
    3400             : 
    3401         543 :         for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
    3402             : 
    3403         542 :                 call = *iter;
    3404         542 :                 ret = event_init(call);
    3405         542 :                 if (!ret)
    3406         542 :                         list_add(&call->list, &ftrace_events);
    3407             :         }
    3408             : 
    3409             :         /*
    3410             :          * We need the top trace array to have a working set of trace
    3411             :          * points at early init, before the debug files and directories
    3412             :          * are created. Create the file entries now, and attach them
    3413             :          * to the actual file dentries later.
    3414             :          */
    3415           1 :         __trace_early_add_events(tr);
    3416             : 
    3417           1 :         early_enable_events(tr, false);
    3418             : 
    3419           1 :         trace_printk_start_comm();
    3420             : 
    3421           1 :         register_event_cmds();
    3422             : 
    3423           1 :         register_trigger_cmds();
    3424             : 
    3425           1 :         return 0;
    3426             : }
    3427             : 
    3428             : /*
    3429             :  * event_trace_enable() is called from trace_event_init() first to
    3430             :  * initialize events and perhaps start any events that are on the
    3431             :  * command line. Unfortunately, there are some events that will not
    3432             :  * start this early, like the system call tracepoints that need
    3433             :  * to set the %SYSCALL_WORK_SYSCALL_TRACEPOINT flag of pid 1. But
    3434             :  * event_trace_enable() is called before pid 1 starts, and this flag
    3435             :  * is never set, making the syscall tracepoint never get reached, but
    3436             :  * the event is enabled regardless (and not doing anything).
    3437             :  */
    3438           1 : static __init int event_trace_enable_again(void)
    3439             : {
    3440           1 :         struct trace_array *tr;
    3441             : 
    3442           1 :         tr = top_trace_array();
    3443           1 :         if (!tr)
    3444             :                 return -ENODEV;
    3445             : 
    3446           1 :         early_enable_events(tr, true);
    3447             : 
    3448           1 :         return 0;
    3449             : }
    3450             : 
    3451             : early_initcall(event_trace_enable_again);
    3452             : 
    3453             : /* Init fields which doesn't related to the tracefs */
    3454           1 : static __init int event_trace_init_fields(void)
    3455             : {
    3456           1 :         if (trace_define_generic_fields())
    3457           0 :                 pr_warn("tracing: Failed to allocated generic fields");
    3458             : 
    3459           1 :         if (trace_define_common_fields())
    3460           0 :                 pr_warn("tracing: Failed to allocate common fields");
    3461             : 
    3462           1 :         return 0;
    3463             : }
    3464             : 
    3465           1 : __init int event_trace_init(void)
    3466             : {
    3467           1 :         struct trace_array *tr;
    3468           1 :         struct dentry *entry;
    3469           1 :         int ret;
    3470             : 
    3471           1 :         tr = top_trace_array();
    3472           1 :         if (!tr)
    3473             :                 return -ENODEV;
    3474             : 
    3475           1 :         entry = tracefs_create_file("available_events", 0444, NULL,
    3476             :                                     tr, &ftrace_avail_fops);
    3477           1 :         if (!entry)
    3478           0 :                 pr_warn("Could not create tracefs 'available_events' entry\n");
    3479             : 
    3480           1 :         ret = early_event_add_tracer(NULL, tr);
    3481           1 :         if (ret)
    3482             :                 return ret;
    3483             : 
    3484             : #ifdef CONFIG_MODULES
    3485             :         ret = register_module_notifier(&trace_module_nb);
    3486             :         if (ret)
    3487             :                 pr_warn("Failed to register trace events module notifier\n");
    3488             : #endif
    3489             : 
    3490           1 :         eventdir_initialized = true;
    3491             : 
    3492           1 :         return 0;
    3493             : }
    3494             : 
    3495           1 : void __init trace_event_init(void)
    3496             : {
    3497           1 :         event_trace_memsetup();
    3498           1 :         init_ftrace_syscalls();
    3499           1 :         event_trace_enable();
    3500           1 :         event_trace_init_fields();
    3501           1 : }
    3502             : 
    3503             : #ifdef CONFIG_EVENT_TRACE_STARTUP_TEST
    3504             : 
    3505             : static DEFINE_SPINLOCK(test_spinlock);
    3506             : static DEFINE_SPINLOCK(test_spinlock_irq);
    3507             : static DEFINE_MUTEX(test_mutex);
    3508             : 
    3509             : static __init void test_work(struct work_struct *dummy)
    3510             : {
    3511             :         spin_lock(&test_spinlock);
    3512             :         spin_lock_irq(&test_spinlock_irq);
    3513             :         udelay(1);
    3514             :         spin_unlock_irq(&test_spinlock_irq);
    3515             :         spin_unlock(&test_spinlock);
    3516             : 
    3517             :         mutex_lock(&test_mutex);
    3518             :         msleep(1);
    3519             :         mutex_unlock(&test_mutex);
    3520             : }
    3521             : 
    3522             : static __init int event_test_thread(void *unused)
    3523             : {
    3524             :         void *test_malloc;
    3525             : 
    3526             :         test_malloc = kmalloc(1234, GFP_KERNEL);
    3527             :         if (!test_malloc)
    3528             :                 pr_info("failed to kmalloc\n");
    3529             : 
    3530             :         schedule_on_each_cpu(test_work);
    3531             : 
    3532             :         kfree(test_malloc);
    3533             : 
    3534             :         set_current_state(TASK_INTERRUPTIBLE);
    3535             :         while (!kthread_should_stop()) {
    3536             :                 schedule();
    3537             :                 set_current_state(TASK_INTERRUPTIBLE);
    3538             :         }
    3539             :         __set_current_state(TASK_RUNNING);
    3540             : 
    3541             :         return 0;
    3542             : }
    3543             : 
    3544             : /*
    3545             :  * Do various things that may trigger events.
    3546             :  */
    3547             : static __init void event_test_stuff(void)
    3548             : {
    3549             :         struct task_struct *test_thread;
    3550             : 
    3551             :         test_thread = kthread_run(event_test_thread, NULL, "test-events");
    3552             :         msleep(1);
    3553             :         kthread_stop(test_thread);
    3554             : }
    3555             : 
    3556             : /*
    3557             :  * For every trace event defined, we will test each trace point separately,
    3558             :  * and then by groups, and finally all trace points.
    3559             :  */
    3560             : static __init void event_trace_self_tests(void)
    3561             : {
    3562             :         struct trace_subsystem_dir *dir;
    3563             :         struct trace_event_file *file;
    3564             :         struct trace_event_call *call;
    3565             :         struct event_subsystem *system;
    3566             :         struct trace_array *tr;
    3567             :         int ret;
    3568             : 
    3569             :         tr = top_trace_array();
    3570             :         if (!tr)
    3571             :                 return;
    3572             : 
    3573             :         pr_info("Running tests on trace events:\n");
    3574             : 
    3575             :         list_for_each_entry(file, &tr->events, list) {
    3576             : 
    3577             :                 call = file->event_call;
    3578             : 
    3579             :                 /* Only test those that have a probe */
    3580             :                 if (!call->class || !call->class->probe)
    3581             :                         continue;
    3582             : 
    3583             : /*
    3584             :  * Testing syscall events here is pretty useless, but
    3585             :  * we still do it if configured. But this is time consuming.
    3586             :  * What we really need is a user thread to perform the
    3587             :  * syscalls as we test.
    3588             :  */
    3589             : #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
    3590             :                 if (call->class->system &&
    3591             :                     strcmp(call->class->system, "syscalls") == 0)
    3592             :                         continue;
    3593             : #endif
    3594             : 
    3595             :                 pr_info("Testing event %s: ", trace_event_name(call));
    3596             : 
    3597             :                 /*
    3598             :                  * If an event is already enabled, someone is using
    3599             :                  * it and the self test should not be on.
    3600             :                  */
    3601             :                 if (file->flags & EVENT_FILE_FL_ENABLED) {
    3602             :                         pr_warn("Enabled event during self test!\n");
    3603             :                         WARN_ON_ONCE(1);
    3604             :                         continue;
    3605             :                 }
    3606             : 
    3607             :                 ftrace_event_enable_disable(file, 1);
    3608             :                 event_test_stuff();
    3609             :                 ftrace_event_enable_disable(file, 0);
    3610             : 
    3611             :                 pr_cont("OK\n");
    3612             :         }
    3613             : 
    3614             :         /* Now test at the sub system level */
    3615             : 
    3616             :         pr_info("Running tests on trace event systems:\n");
    3617             : 
    3618             :         list_for_each_entry(dir, &tr->systems, list) {
    3619             : 
    3620             :                 system = dir->subsystem;
    3621             : 
    3622             :                 /* the ftrace system is special, skip it */
    3623             :                 if (strcmp(system->name, "ftrace") == 0)
    3624             :                         continue;
    3625             : 
    3626             :                 pr_info("Testing event system %s: ", system->name);
    3627             : 
    3628             :                 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
    3629             :                 if (WARN_ON_ONCE(ret)) {
    3630             :                         pr_warn("error enabling system %s\n",
    3631             :                                 system->name);
    3632             :                         continue;
    3633             :                 }
    3634             : 
    3635             :                 event_test_stuff();
    3636             : 
    3637             :                 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
    3638             :                 if (WARN_ON_ONCE(ret)) {
    3639             :                         pr_warn("error disabling system %s\n",
    3640             :                                 system->name);
    3641             :                         continue;
    3642             :                 }
    3643             : 
    3644             :                 pr_cont("OK\n");
    3645             :         }
    3646             : 
    3647             :         /* Test with all events enabled */
    3648             : 
    3649             :         pr_info("Running tests on all trace events:\n");
    3650             :         pr_info("Testing all events: ");
    3651             : 
    3652             :         ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
    3653             :         if (WARN_ON_ONCE(ret)) {
    3654             :                 pr_warn("error enabling all events\n");
    3655             :                 return;
    3656             :         }
    3657             : 
    3658             :         event_test_stuff();
    3659             : 
    3660             :         /* reset sysname */
    3661             :         ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
    3662             :         if (WARN_ON_ONCE(ret)) {
    3663             :                 pr_warn("error disabling all events\n");
    3664             :                 return;
    3665             :         }
    3666             : 
    3667             :         pr_cont("OK\n");
    3668             : }
    3669             : 
    3670             : #ifdef CONFIG_FUNCTION_TRACER
    3671             : 
    3672             : static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
    3673             : 
    3674             : static struct trace_event_file event_trace_file __initdata;
    3675             : 
    3676             : static void __init
    3677             : function_test_events_call(unsigned long ip, unsigned long parent_ip,
    3678             :                           struct ftrace_ops *op, struct ftrace_regs *regs)
    3679             : {
    3680             :         struct trace_buffer *buffer;
    3681             :         struct ring_buffer_event *event;
    3682             :         struct ftrace_entry *entry;
    3683             :         unsigned int trace_ctx;
    3684             :         long disabled;
    3685             :         int cpu;
    3686             : 
    3687             :         trace_ctx = tracing_gen_ctx();
    3688             :         preempt_disable_notrace();
    3689             :         cpu = raw_smp_processor_id();
    3690             :         disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
    3691             : 
    3692             :         if (disabled != 1)
    3693             :                 goto out;
    3694             : 
    3695             :         event = trace_event_buffer_lock_reserve(&buffer, &event_trace_file,
    3696             :                                                 TRACE_FN, sizeof(*entry),
    3697             :                                                 trace_ctx);
    3698             :         if (!event)
    3699             :                 goto out;
    3700             :         entry   = ring_buffer_event_data(event);
    3701             :         entry->ip                    = ip;
    3702             :         entry->parent_ip             = parent_ip;
    3703             : 
    3704             :         event_trigger_unlock_commit(&event_trace_file, buffer, event,
    3705             :                                     entry, trace_ctx);
    3706             :  out:
    3707             :         atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
    3708             :         preempt_enable_notrace();
    3709             : }
    3710             : 
    3711             : static struct ftrace_ops trace_ops __initdata  =
    3712             : {
    3713             :         .func = function_test_events_call,
    3714             : };
    3715             : 
    3716             : static __init void event_trace_self_test_with_function(void)
    3717             : {
    3718             :         int ret;
    3719             : 
    3720             :         event_trace_file.tr = top_trace_array();
    3721             :         if (WARN_ON(!event_trace_file.tr))
    3722             :                 return;
    3723             : 
    3724             :         ret = register_ftrace_function(&trace_ops);
    3725             :         if (WARN_ON(ret < 0)) {
    3726             :                 pr_info("Failed to enable function tracer for event tests\n");
    3727             :                 return;
    3728             :         }
    3729             :         pr_info("Running tests again, along with the function tracer\n");
    3730             :         event_trace_self_tests();
    3731             :         unregister_ftrace_function(&trace_ops);
    3732             : }
    3733             : #else
    3734             : static __init void event_trace_self_test_with_function(void)
    3735             : {
    3736             : }
    3737             : #endif
    3738             : 
    3739             : static __init int event_trace_self_tests_init(void)
    3740             : {
    3741             :         if (!tracing_selftest_disabled) {
    3742             :                 event_trace_self_tests();
    3743             :                 event_trace_self_test_with_function();
    3744             :         }
    3745             : 
    3746             :         return 0;
    3747             : }
    3748             : 
    3749             : late_initcall(event_trace_self_tests_init);
    3750             : 
    3751             : #endif

Generated by: LCOV version 1.14