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

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

Version: ~ [ linux-5.8-rc4 ] ~ [ linux-5.7.7 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.50 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.131 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.187 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.229 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.229 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * event tracer
  3  *
  4  * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
  5  *
  6  *  - Added format output of fields of the trace point.
  7  *    This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
  8  *
  9  */
 10 
 11 #include <linux/workqueue.h>
 12 #include <linux/spinlock.h>
 13 #include <linux/kthread.h>
 14 #include <linux/debugfs.h>
 15 #include <linux/uaccess.h>
 16 #include <linux/module.h>
 17 #include <linux/ctype.h>
 18 #include <linux/slab.h>
 19 #include <linux/delay.h>
 20 
 21 #include <asm/setup.h>
 22 
 23 #include "trace_output.h"
 24 
 25 #undef TRACE_SYSTEM
 26 #define TRACE_SYSTEM "TRACE_SYSTEM"
 27 
 28 DEFINE_MUTEX(event_mutex);
 29 
 30 LIST_HEAD(ftrace_events);
 31 static LIST_HEAD(ftrace_common_fields);
 32 
 33 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
 34 
 35 static struct kmem_cache *field_cachep;
 36 static struct kmem_cache *file_cachep;
 37 
 38 #define SYSTEM_FL_FREE_NAME             (1 << 31)
 39 
 40 static inline int system_refcount(struct event_subsystem *system)
 41 {
 42         return system->ref_count & ~SYSTEM_FL_FREE_NAME;
 43 }
 44 
 45 static int system_refcount_inc(struct event_subsystem *system)
 46 {
 47         return (system->ref_count++) & ~SYSTEM_FL_FREE_NAME;
 48 }
 49 
 50 static int system_refcount_dec(struct event_subsystem *system)
 51 {
 52         return (--system->ref_count) & ~SYSTEM_FL_FREE_NAME;
 53 }
 54 
 55 /* Double loops, do not use break, only goto's work */
 56 #define do_for_each_event_file(tr, file)                        \
 57         list_for_each_entry(tr, &ftrace_trace_arrays, list) {   \
 58                 list_for_each_entry(file, &tr->events, list)
 59 
 60 #define do_for_each_event_file_safe(tr, file)                   \
 61         list_for_each_entry(tr, &ftrace_trace_arrays, list) {   \
 62                 struct ftrace_event_file *___n;                         \
 63                 list_for_each_entry_safe(file, ___n, &tr->events, list)
 64 
 65 #define while_for_each_event_file()             \
 66         }
 67 
 68 static struct list_head *
 69 trace_get_fields(struct ftrace_event_call *event_call)
 70 {
 71         if (!event_call->class->get_fields)
 72                 return &event_call->class->fields;
 73         return event_call->class->get_fields(event_call);
 74 }
 75 
 76 static struct ftrace_event_field *
 77 __find_event_field(struct list_head *head, char *name)
 78 {
 79         struct ftrace_event_field *field;
 80 
 81         list_for_each_entry(field, head, link) {
 82                 if (!strcmp(field->name, name))
 83                         return field;
 84         }
 85 
 86         return NULL;
 87 }
 88 
 89 struct ftrace_event_field *
 90 trace_find_event_field(struct ftrace_event_call *call, char *name)
 91 {
 92         struct ftrace_event_field *field;
 93         struct list_head *head;
 94 
 95         field = __find_event_field(&ftrace_common_fields, name);
 96         if (field)
 97                 return field;
 98 
 99         head = trace_get_fields(call);
100         return __find_event_field(head, name);
101 }
102 
103 static int __trace_define_field(struct list_head *head, const char *type,
104                                 const char *name, int offset, int size,
105                                 int is_signed, int filter_type)
106 {
107         struct ftrace_event_field *field;
108 
109         field = kmem_cache_alloc(field_cachep, GFP_TRACE);
110         if (!field)
111                 return -ENOMEM;
112 
113         field->name = name;
114         field->type = type;
115 
116         if (filter_type == FILTER_OTHER)
117                 field->filter_type = filter_assign_type(type);
118         else
119                 field->filter_type = filter_type;
120 
121         field->offset = offset;
122         field->size = size;
123         field->is_signed = is_signed;
124 
125         list_add(&field->link, head);
126 
127         return 0;
128 }
129 
130 int trace_define_field(struct ftrace_event_call *call, const char *type,
131                        const char *name, int offset, int size, int is_signed,
132                        int filter_type)
133 {
134         struct list_head *head;
135 
136         if (WARN_ON(!call->class))
137                 return 0;
138 
139         head = trace_get_fields(call);
140         return __trace_define_field(head, type, name, offset, size,
141                                     is_signed, filter_type);
142 }
143 EXPORT_SYMBOL_GPL(trace_define_field);
144 
145 #define __common_field(type, item)                                      \
146         ret = __trace_define_field(&ftrace_common_fields, #type,        \
147                                    "common_" #item,                     \
148                                    offsetof(typeof(ent), item),         \
149                                    sizeof(ent.item),                    \
150                                    is_signed_type(type), FILTER_OTHER); \
151         if (ret)                                                        \
152                 return ret;
153 
154 static int trace_define_common_fields(void)
155 {
156         int ret;
157         struct trace_entry ent;
158 
159         __common_field(unsigned short, type);
160         __common_field(unsigned char, flags);
161         __common_field(unsigned char, preempt_count);
162         __common_field(int, pid);
163 
164         return ret;
165 }
166 
167 static void trace_destroy_fields(struct ftrace_event_call *call)
168 {
169         struct ftrace_event_field *field, *next;
170         struct list_head *head;
171 
172         head = trace_get_fields(call);
173         list_for_each_entry_safe(field, next, head, link) {
174                 list_del(&field->link);
175                 kmem_cache_free(field_cachep, field);
176         }
177 }
178 
179 int trace_event_raw_init(struct ftrace_event_call *call)
180 {
181         int id;
182 
183         id = register_ftrace_event(&call->event);
184         if (!id)
185                 return -ENODEV;
186 
187         return 0;
188 }
189 EXPORT_SYMBOL_GPL(trace_event_raw_init);
190 
191 int ftrace_event_reg(struct ftrace_event_call *call,
192                      enum trace_reg type, void *data)
193 {
194         struct ftrace_event_file *file = data;
195 
196         switch (type) {
197         case TRACE_REG_REGISTER:
198                 return tracepoint_probe_register(call->name,
199                                                  call->class->probe,
200                                                  file);
201         case TRACE_REG_UNREGISTER:
202                 tracepoint_probe_unregister(call->name,
203                                             call->class->probe,
204                                             file);
205                 return 0;
206 
207 #ifdef CONFIG_PERF_EVENTS
208         case TRACE_REG_PERF_REGISTER:
209                 return tracepoint_probe_register(call->name,
210                                                  call->class->perf_probe,
211                                                  call);
212         case TRACE_REG_PERF_UNREGISTER:
213                 tracepoint_probe_unregister(call->name,
214                                             call->class->perf_probe,
215                                             call);
216                 return 0;
217         case TRACE_REG_PERF_OPEN:
218         case TRACE_REG_PERF_CLOSE:
219         case TRACE_REG_PERF_ADD:
220         case TRACE_REG_PERF_DEL:
221                 return 0;
222 #endif
223         }
224         return 0;
225 }
226 EXPORT_SYMBOL_GPL(ftrace_event_reg);
227 
228 void trace_event_enable_cmd_record(bool enable)
229 {
230         struct ftrace_event_file *file;
231         struct trace_array *tr;
232 
233         mutex_lock(&event_mutex);
234         do_for_each_event_file(tr, file) {
235 
236                 if (!(file->flags & FTRACE_EVENT_FL_ENABLED))
237                         continue;
238 
239                 if (enable) {
240                         tracing_start_cmdline_record();
241                         set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
242                 } else {
243                         tracing_stop_cmdline_record();
244                         clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
245                 }
246         } while_for_each_event_file();
247         mutex_unlock(&event_mutex);
248 }
249 
250 static int __ftrace_event_enable_disable(struct ftrace_event_file *file,
251                                          int enable, int soft_disable)
252 {
253         struct ftrace_event_call *call = file->event_call;
254         int ret = 0;
255         int disable;
256 
257         switch (enable) {
258         case 0:
259                 /*
260                  * When soft_disable is set and enable is cleared, the sm_ref
261                  * reference counter is decremented. If it reaches 0, we want
262                  * to clear the SOFT_DISABLED flag but leave the event in the
263                  * state that it was. That is, if the event was enabled and
264                  * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
265                  * is set we do not want the event to be enabled before we
266                  * clear the bit.
267                  *
268                  * When soft_disable is not set but the SOFT_MODE flag is,
269                  * we do nothing. Do not disable the tracepoint, otherwise
270                  * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
271                  */
272                 if (soft_disable) {
273                         if (atomic_dec_return(&file->sm_ref) > 0)
274                                 break;
275                         disable = file->flags & FTRACE_EVENT_FL_SOFT_DISABLED;
276                         clear_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
277                 } else
278                         disable = !(file->flags & FTRACE_EVENT_FL_SOFT_MODE);
279 
280                 if (disable && (file->flags & FTRACE_EVENT_FL_ENABLED)) {
281                         clear_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
282                         if (file->flags & FTRACE_EVENT_FL_RECORDED_CMD) {
283                                 tracing_stop_cmdline_record();
284                                 clear_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
285                         }
286                         call->class->reg(call, TRACE_REG_UNREGISTER, file);
287                 }
288                 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
289                 if (file->flags & FTRACE_EVENT_FL_SOFT_MODE)
290                         set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
291                 else
292                         clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
293                 break;
294         case 1:
295                 /*
296                  * When soft_disable is set and enable is set, we want to
297                  * register the tracepoint for the event, but leave the event
298                  * as is. That means, if the event was already enabled, we do
299                  * nothing (but set SOFT_MODE). If the event is disabled, we
300                  * set SOFT_DISABLED before enabling the event tracepoint, so
301                  * it still seems to be disabled.
302                  */
303                 if (!soft_disable)
304                         clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
305                 else {
306                         if (atomic_inc_return(&file->sm_ref) > 1)
307                                 break;
308                         set_bit(FTRACE_EVENT_FL_SOFT_MODE_BIT, &file->flags);
309                 }
310 
311                 if (!(file->flags & FTRACE_EVENT_FL_ENABLED)) {
312 
313                         /* Keep the event disabled, when going to SOFT_MODE. */
314                         if (soft_disable)
315                                 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags);
316 
317                         if (trace_flags & TRACE_ITER_RECORD_CMD) {
318                                 tracing_start_cmdline_record();
319                                 set_bit(FTRACE_EVENT_FL_RECORDED_CMD_BIT, &file->flags);
320                         }
321                         ret = call->class->reg(call, TRACE_REG_REGISTER, file);
322                         if (ret) {
323                                 tracing_stop_cmdline_record();
324                                 pr_info("event trace: Could not enable event "
325                                         "%s\n", call->name);
326                                 break;
327                         }
328                         set_bit(FTRACE_EVENT_FL_ENABLED_BIT, &file->flags);
329 
330                         /* WAS_ENABLED gets set but never cleared. */
331                         call->flags |= TRACE_EVENT_FL_WAS_ENABLED;
332                 }
333                 break;
334         }
335 
336         return ret;
337 }
338 
339 static int ftrace_event_enable_disable(struct ftrace_event_file *file,
340                                        int enable)
341 {
342         return __ftrace_event_enable_disable(file, enable, 0);
343 }
344 
345 static void ftrace_clear_events(struct trace_array *tr)
346 {
347         struct ftrace_event_file *file;
348 
349         mutex_lock(&event_mutex);
350         list_for_each_entry(file, &tr->events, list) {
351                 ftrace_event_enable_disable(file, 0);
352         }
353         mutex_unlock(&event_mutex);
354 }
355 
356 static void __put_system(struct event_subsystem *system)
357 {
358         struct event_filter *filter = system->filter;
359 
360         WARN_ON_ONCE(system_refcount(system) == 0);
361         if (system_refcount_dec(system))
362                 return;
363 
364         list_del(&system->list);
365 
366         if (filter) {
367                 kfree(filter->filter_string);
368                 kfree(filter);
369         }
370         if (system->ref_count & SYSTEM_FL_FREE_NAME)
371                 kfree(system->name);
372         kfree(system);
373 }
374 
375 static void __get_system(struct event_subsystem *system)
376 {
377         WARN_ON_ONCE(system_refcount(system) == 0);
378         system_refcount_inc(system);
379 }
380 
381 static void __get_system_dir(struct ftrace_subsystem_dir *dir)
382 {
383         WARN_ON_ONCE(dir->ref_count == 0);
384         dir->ref_count++;
385         __get_system(dir->subsystem);
386 }
387 
388 static void __put_system_dir(struct ftrace_subsystem_dir *dir)
389 {
390         WARN_ON_ONCE(dir->ref_count == 0);
391         /* If the subsystem is about to be freed, the dir must be too */
392         WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
393 
394         __put_system(dir->subsystem);
395         if (!--dir->ref_count)
396                 kfree(dir);
397 }
398 
399 static void put_system(struct ftrace_subsystem_dir *dir)
400 {
401         mutex_lock(&event_mutex);
402         __put_system_dir(dir);
403         mutex_unlock(&event_mutex);
404 }
405 
406 static void remove_subsystem(struct ftrace_subsystem_dir *dir)
407 {
408         if (!dir)
409                 return;
410 
411         if (!--dir->nr_events) {
412                 debugfs_remove_recursive(dir->entry);
413                 list_del(&dir->list);
414                 __put_system_dir(dir);
415         }
416 }
417 
418 static void *event_file_data(struct file *filp)
419 {
420         return ACCESS_ONCE(file_inode(filp)->i_private);
421 }
422 
423 static void remove_event_file_dir(struct ftrace_event_file *file)
424 {
425         struct dentry *dir = file->dir;
426         struct dentry *child;
427 
428         if (dir) {
429                 spin_lock(&dir->d_lock);        /* probably unneeded */
430                 list_for_each_entry(child, &dir->d_subdirs, d_child) {
431                         if (child->d_inode)     /* probably unneeded */
432                                 child->d_inode->i_private = NULL;
433                 }
434                 spin_unlock(&dir->d_lock);
435 
436                 debugfs_remove_recursive(dir);
437         }
438 
439         list_del(&file->list);
440         remove_subsystem(file->system);
441         kmem_cache_free(file_cachep, file);
442 }
443 
444 /*
445  * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
446  */
447 static int
448 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
449                               const char *sub, const char *event, int set)
450 {
451         struct ftrace_event_file *file;
452         struct ftrace_event_call *call;
453         int ret = -EINVAL;
454 
455         list_for_each_entry(file, &tr->events, list) {
456 
457                 call = file->event_call;
458 
459                 if (!call->name || !call->class || !call->class->reg)
460                         continue;
461 
462                 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
463                         continue;
464 
465                 if (match &&
466                     strcmp(match, call->name) != 0 &&
467                     strcmp(match, call->class->system) != 0)
468                         continue;
469 
470                 if (sub && strcmp(sub, call->class->system) != 0)
471                         continue;
472 
473                 if (event && strcmp(event, call->name) != 0)
474                         continue;
475 
476                 ftrace_event_enable_disable(file, set);
477 
478                 ret = 0;
479         }
480 
481         return ret;
482 }
483 
484 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
485                                   const char *sub, const char *event, int set)
486 {
487         int ret;
488 
489         mutex_lock(&event_mutex);
490         ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
491         mutex_unlock(&event_mutex);
492 
493         return ret;
494 }
495 
496 static int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
497 {
498         char *event = NULL, *sub = NULL, *match;
499 
500         /*
501          * The buf format can be <subsystem>:<event-name>
502          *  *:<event-name> means any event by that name.
503          *  :<event-name> is the same.
504          *
505          *  <subsystem>:* means all events in that subsystem
506          *  <subsystem>: means the same.
507          *
508          *  <name> (no ':') means all events in a subsystem with
509          *  the name <name> or any event that matches <name>
510          */
511 
512         match = strsep(&buf, ":");
513         if (buf) {
514                 sub = match;
515                 event = buf;
516                 match = NULL;
517 
518                 if (!strlen(sub) || strcmp(sub, "*") == 0)
519                         sub = NULL;
520                 if (!strlen(event) || strcmp(event, "*") == 0)
521                         event = NULL;
522         }
523 
524         return __ftrace_set_clr_event(tr, match, sub, event, set);
525 }
526 
527 /**
528  * trace_set_clr_event - enable or disable an event
529  * @system: system name to match (NULL for any system)
530  * @event: event name to match (NULL for all events, within system)
531  * @set: 1 to enable, 0 to disable
532  *
533  * This is a way for other parts of the kernel to enable or disable
534  * event recording.
535  *
536  * Returns 0 on success, -EINVAL if the parameters do not match any
537  * registered events.
538  */
539 int trace_set_clr_event(const char *system, const char *event, int set)
540 {
541         struct trace_array *tr = top_trace_array();
542 
543         return __ftrace_set_clr_event(tr, NULL, system, event, set);
544 }
545 EXPORT_SYMBOL_GPL(trace_set_clr_event);
546 
547 /* 128 should be much more than enough */
548 #define EVENT_BUF_SIZE          127
549 
550 static ssize_t
551 ftrace_event_write(struct file *file, const char __user *ubuf,
552                    size_t cnt, loff_t *ppos)
553 {
554         struct trace_parser parser;
555         struct seq_file *m = file->private_data;
556         struct trace_array *tr = m->private;
557         ssize_t read, ret;
558 
559         if (!cnt)
560                 return 0;
561 
562         ret = tracing_update_buffers();
563         if (ret < 0)
564                 return ret;
565 
566         if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
567                 return -ENOMEM;
568 
569         read = trace_get_user(&parser, ubuf, cnt, ppos);
570 
571         if (read >= 0 && trace_parser_loaded((&parser))) {
572                 int set = 1;
573 
574                 if (*parser.buffer == '!')
575                         set = 0;
576 
577                 parser.buffer[parser.idx] = 0;
578 
579                 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
580                 if (ret)
581                         goto out_put;
582         }
583 
584         ret = read;
585 
586  out_put:
587         trace_parser_put(&parser);
588 
589         return ret;
590 }
591 
592 static void *
593 t_next(struct seq_file *m, void *v, loff_t *pos)
594 {
595         struct ftrace_event_file *file = v;
596         struct ftrace_event_call *call;
597         struct trace_array *tr = m->private;
598 
599         (*pos)++;
600 
601         list_for_each_entry_continue(file, &tr->events, list) {
602                 call = file->event_call;
603                 /*
604                  * The ftrace subsystem is for showing formats only.
605                  * They can not be enabled or disabled via the event files.
606                  */
607                 if (call->class && call->class->reg &&
608                     !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
609                         return file;
610         }
611 
612         return NULL;
613 }
614 
615 static void *t_start(struct seq_file *m, loff_t *pos)
616 {
617         struct ftrace_event_file *file;
618         struct trace_array *tr = m->private;
619         loff_t l;
620 
621         mutex_lock(&event_mutex);
622 
623         file = list_entry(&tr->events, struct ftrace_event_file, list);
624         for (l = 0; l <= *pos; ) {
625                 file = t_next(m, file, &l);
626                 if (!file)
627                         break;
628         }
629         return file;
630 }
631 
632 static void *
633 s_next(struct seq_file *m, void *v, loff_t *pos)
634 {
635         struct ftrace_event_file *file = v;
636         struct trace_array *tr = m->private;
637 
638         (*pos)++;
639 
640         list_for_each_entry_continue(file, &tr->events, list) {
641                 if (file->flags & FTRACE_EVENT_FL_ENABLED)
642                         return file;
643         }
644 
645         return NULL;
646 }
647 
648 static void *s_start(struct seq_file *m, loff_t *pos)
649 {
650         struct ftrace_event_file *file;
651         struct trace_array *tr = m->private;
652         loff_t l;
653 
654         mutex_lock(&event_mutex);
655 
656         file = list_entry(&tr->events, struct ftrace_event_file, list);
657         for (l = 0; l <= *pos; ) {
658                 file = s_next(m, file, &l);
659                 if (!file)
660                         break;
661         }
662         return file;
663 }
664 
665 static int t_show(struct seq_file *m, void *v)
666 {
667         struct ftrace_event_file *file = v;
668         struct ftrace_event_call *call = file->event_call;
669 
670         if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
671                 seq_printf(m, "%s:", call->class->system);
672         seq_printf(m, "%s\n", call->name);
673 
674         return 0;
675 }
676 
677 static void t_stop(struct seq_file *m, void *p)
678 {
679         mutex_unlock(&event_mutex);
680 }
681 
682 static ssize_t
683 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
684                   loff_t *ppos)
685 {
686         struct ftrace_event_file *file;
687         unsigned long flags;
688         char buf[4] = "";
689 
690         mutex_lock(&event_mutex);
691         file = event_file_data(filp);
692         if (likely(file))
693                 flags = file->flags;
694         mutex_unlock(&event_mutex);
695 
696         if (!file)
697                 return -ENODEV;
698 
699         if (flags & FTRACE_EVENT_FL_ENABLED &&
700             !(flags & FTRACE_EVENT_FL_SOFT_DISABLED))
701                 strcpy(buf, "1");
702 
703         if (flags & FTRACE_EVENT_FL_SOFT_DISABLED ||
704             flags & FTRACE_EVENT_FL_SOFT_MODE)
705                 strcat(buf, "*");
706 
707         strcat(buf, "\n");
708 
709         return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
710 }
711 
712 static ssize_t
713 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
714                    loff_t *ppos)
715 {
716         struct ftrace_event_file *file;
717         unsigned long val;
718         int ret;
719 
720         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
721         if (ret)
722                 return ret;
723 
724         ret = tracing_update_buffers();
725         if (ret < 0)
726                 return ret;
727 
728         switch (val) {
729         case 0:
730         case 1:
731                 ret = -ENODEV;
732                 mutex_lock(&event_mutex);
733                 file = event_file_data(filp);
734                 if (likely(file))
735                         ret = ftrace_event_enable_disable(file, val);
736                 mutex_unlock(&event_mutex);
737                 break;
738 
739         default:
740                 return -EINVAL;
741         }
742 
743         *ppos += cnt;
744 
745         return ret ? ret : cnt;
746 }
747 
748 static ssize_t
749 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
750                    loff_t *ppos)
751 {
752         const char set_to_char[4] = { '?', '', '1', 'X' };
753         struct ftrace_subsystem_dir *dir = filp->private_data;
754         struct event_subsystem *system = dir->subsystem;
755         struct ftrace_event_call *call;
756         struct ftrace_event_file *file;
757         struct trace_array *tr = dir->tr;
758         char buf[2];
759         int set = 0;
760         int ret;
761 
762         mutex_lock(&event_mutex);
763         list_for_each_entry(file, &tr->events, list) {
764                 call = file->event_call;
765                 if (!call->name || !call->class || !call->class->reg)
766                         continue;
767 
768                 if (system && strcmp(call->class->system, system->name) != 0)
769                         continue;
770 
771                 /*
772                  * We need to find out if all the events are set
773                  * or if all events or cleared, or if we have
774                  * a mixture.
775                  */
776                 set |= (1 << !!(file->flags & FTRACE_EVENT_FL_ENABLED));
777 
778                 /*
779                  * If we have a mixture, no need to look further.
780                  */
781                 if (set == 3)
782                         break;
783         }
784         mutex_unlock(&event_mutex);
785 
786         buf[0] = set_to_char[set];
787         buf[1] = '\n';
788 
789         ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
790 
791         return ret;
792 }
793 
794 static ssize_t
795 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
796                     loff_t *ppos)
797 {
798         struct ftrace_subsystem_dir *dir = filp->private_data;
799         struct event_subsystem *system = dir->subsystem;
800         const char *name = NULL;
801         unsigned long val;
802         ssize_t ret;
803 
804         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
805         if (ret)
806                 return ret;
807 
808         ret = tracing_update_buffers();
809         if (ret < 0)
810                 return ret;
811 
812         if (val != 0 && val != 1)
813                 return -EINVAL;
814 
815         /*
816          * Opening of "enable" adds a ref count to system,
817          * so the name is safe to use.
818          */
819         if (system)
820                 name = system->name;
821 
822         ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
823         if (ret)
824                 goto out;
825 
826         ret = cnt;
827 
828 out:
829         *ppos += cnt;
830 
831         return ret;
832 }
833 
834 enum {
835         FORMAT_HEADER           = 1,
836         FORMAT_FIELD_SEPERATOR  = 2,
837         FORMAT_PRINTFMT         = 3,
838 };
839 
840 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
841 {
842         struct ftrace_event_call *call = event_file_data(m->private);
843         struct list_head *common_head = &ftrace_common_fields;
844         struct list_head *head = trace_get_fields(call);
845         struct list_head *node = v;
846 
847         (*pos)++;
848 
849         switch ((unsigned long)v) {
850         case FORMAT_HEADER:
851                 node = common_head;
852                 break;
853 
854         case FORMAT_FIELD_SEPERATOR:
855                 node = head;
856                 break;
857 
858         case FORMAT_PRINTFMT:
859                 /* all done */
860                 return NULL;
861         }
862 
863         node = node->prev;
864         if (node == common_head)
865                 return (void *)FORMAT_FIELD_SEPERATOR;
866         else if (node == head)
867                 return (void *)FORMAT_PRINTFMT;
868         else
869                 return node;
870 }
871 
872 static int f_show(struct seq_file *m, void *v)
873 {
874         struct ftrace_event_call *call = event_file_data(m->private);
875         struct ftrace_event_field *field;
876         const char *array_descriptor;
877 
878         switch ((unsigned long)v) {
879         case FORMAT_HEADER:
880                 seq_printf(m, "name: %s\n", call->name);
881                 seq_printf(m, "ID: %d\n", call->event.type);
882                 seq_printf(m, "format:\n");
883                 return 0;
884 
885         case FORMAT_FIELD_SEPERATOR:
886                 seq_putc(m, '\n');
887                 return 0;
888 
889         case FORMAT_PRINTFMT:
890                 seq_printf(m, "\nprint fmt: %s\n",
891                            call->print_fmt);
892                 return 0;
893         }
894 
895         field = list_entry(v, struct ftrace_event_field, link);
896         /*
897          * Smartly shows the array type(except dynamic array).
898          * Normal:
899          *      field:TYPE VAR
900          * If TYPE := TYPE[LEN], it is shown:
901          *      field:TYPE VAR[LEN]
902          */
903         array_descriptor = strchr(field->type, '[');
904 
905         if (!strncmp(field->type, "__data_loc", 10))
906                 array_descriptor = NULL;
907 
908         if (!array_descriptor)
909                 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
910                            field->type, field->name, field->offset,
911                            field->size, !!field->is_signed);
912         else
913                 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
914                            (int)(array_descriptor - field->type),
915                            field->type, field->name,
916                            array_descriptor, field->offset,
917                            field->size, !!field->is_signed);
918 
919         return 0;
920 }
921 
922 static void *f_start(struct seq_file *m, loff_t *pos)
923 {
924         void *p = (void *)FORMAT_HEADER;
925         loff_t l = 0;
926 
927         /* ->stop() is called even if ->start() fails */
928         mutex_lock(&event_mutex);
929         if (!event_file_data(m->private))
930                 return ERR_PTR(-ENODEV);
931 
932         while (l < *pos && p)
933                 p = f_next(m, p, &l);
934 
935         return p;
936 }
937 
938 static void f_stop(struct seq_file *m, void *p)
939 {
940         mutex_unlock(&event_mutex);
941 }
942 
943 static const struct seq_operations trace_format_seq_ops = {
944         .start          = f_start,
945         .next           = f_next,
946         .stop           = f_stop,
947         .show           = f_show,
948 };
949 
950 static int trace_format_open(struct inode *inode, struct file *file)
951 {
952         struct seq_file *m;
953         int ret;
954 
955         ret = seq_open(file, &trace_format_seq_ops);
956         if (ret < 0)
957                 return ret;
958 
959         m = file->private_data;
960         m->private = file;
961 
962         return 0;
963 }
964 
965 static ssize_t
966 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
967 {
968         int id = (long)event_file_data(filp);
969         char buf[32];
970         int len;
971 
972         if (*ppos)
973                 return 0;
974 
975         if (unlikely(!id))
976                 return -ENODEV;
977 
978         len = sprintf(buf, "%d\n", id);
979 
980         return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
981 }
982 
983 static ssize_t
984 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
985                   loff_t *ppos)
986 {
987         struct ftrace_event_call *call;
988         struct trace_seq *s;
989         int r = -ENODEV;
990 
991         if (*ppos)
992                 return 0;
993 
994         s = kmalloc(sizeof(*s), GFP_KERNEL);
995 
996         if (!s)
997                 return -ENOMEM;
998 
999         trace_seq_init(s);
1000 
1001         mutex_lock(&event_mutex);
1002         call = event_file_data(filp);
1003         if (call)
1004                 print_event_filter(call, s);
1005         mutex_unlock(&event_mutex);
1006 
1007         if (call)
1008                 r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1009 
1010         kfree(s);
1011 
1012         return r;
1013 }
1014 
1015 static ssize_t
1016 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1017                    loff_t *ppos)
1018 {
1019         struct ftrace_event_call *call;
1020         char *buf;
1021         int err = -ENODEV;
1022 
1023         if (cnt >= PAGE_SIZE)
1024                 return -EINVAL;
1025 
1026         buf = (char *)__get_free_page(GFP_TEMPORARY);
1027         if (!buf)
1028                 return -ENOMEM;
1029 
1030         if (copy_from_user(buf, ubuf, cnt)) {
1031                 free_page((unsigned long) buf);
1032                 return -EFAULT;
1033         }
1034         buf[cnt] = '\0';
1035 
1036         mutex_lock(&event_mutex);
1037         call = event_file_data(filp);
1038         if (call)
1039                 err = apply_event_filter(call, buf);
1040         mutex_unlock(&event_mutex);
1041 
1042         free_page((unsigned long) buf);
1043         if (err < 0)
1044                 return err;
1045 
1046         *ppos += cnt;
1047 
1048         return cnt;
1049 }
1050 
1051 static LIST_HEAD(event_subsystems);
1052 
1053 static int subsystem_open(struct inode *inode, struct file *filp)
1054 {
1055         struct event_subsystem *system = NULL;
1056         struct ftrace_subsystem_dir *dir = NULL; /* Initialize for gcc */
1057         struct trace_array *tr;
1058         int ret;
1059 
1060         /* Make sure the system still exists */
1061         mutex_lock(&trace_types_lock);
1062         mutex_lock(&event_mutex);
1063         list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1064                 list_for_each_entry(dir, &tr->systems, list) {
1065                         if (dir == inode->i_private) {
1066                                 /* Don't open systems with no events */
1067                                 if (dir->nr_events) {
1068                                         __get_system_dir(dir);
1069                                         system = dir->subsystem;
1070                                 }
1071                                 goto exit_loop;
1072                         }
1073                 }
1074         }
1075  exit_loop:
1076         mutex_unlock(&event_mutex);
1077         mutex_unlock(&trace_types_lock);
1078 
1079         if (!system)
1080                 return -ENODEV;
1081 
1082         /* Some versions of gcc think dir can be uninitialized here */
1083         WARN_ON(!dir);
1084 
1085         /* Still need to increment the ref count of the system */
1086         if (trace_array_get(tr) < 0) {
1087                 put_system(dir);
1088                 return -ENODEV;
1089         }
1090 
1091         ret = tracing_open_generic(inode, filp);
1092         if (ret < 0) {
1093                 trace_array_put(tr);
1094                 put_system(dir);
1095         }
1096 
1097         return ret;
1098 }
1099 
1100 static int system_tr_open(struct inode *inode, struct file *filp)
1101 {
1102         struct ftrace_subsystem_dir *dir;
1103         struct trace_array *tr = inode->i_private;
1104         int ret;
1105 
1106         if (trace_array_get(tr) < 0)
1107                 return -ENODEV;
1108 
1109         /* Make a temporary dir that has no system but points to tr */
1110         dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1111         if (!dir) {
1112                 trace_array_put(tr);
1113                 return -ENOMEM;
1114         }
1115 
1116         dir->tr = tr;
1117 
1118         ret = tracing_open_generic(inode, filp);
1119         if (ret < 0) {
1120                 trace_array_put(tr);
1121                 kfree(dir);
1122         }
1123 
1124         filp->private_data = dir;
1125 
1126         return ret;
1127 }
1128 
1129 static int subsystem_release(struct inode *inode, struct file *file)
1130 {
1131         struct ftrace_subsystem_dir *dir = file->private_data;
1132 
1133         trace_array_put(dir->tr);
1134 
1135         /*
1136          * If dir->subsystem is NULL, then this is a temporary
1137          * descriptor that was made for a trace_array to enable
1138          * all subsystems.
1139          */
1140         if (dir->subsystem)
1141                 put_system(dir);
1142         else
1143                 kfree(dir);
1144 
1145         return 0;
1146 }
1147 
1148 static ssize_t
1149 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1150                       loff_t *ppos)
1151 {
1152         struct ftrace_subsystem_dir *dir = filp->private_data;
1153         struct event_subsystem *system = dir->subsystem;
1154         struct trace_seq *s;
1155         int r;
1156 
1157         if (*ppos)
1158                 return 0;
1159 
1160         s = kmalloc(sizeof(*s), GFP_KERNEL);
1161         if (!s)
1162                 return -ENOMEM;
1163 
1164         trace_seq_init(s);
1165 
1166         print_subsystem_event_filter(system, s);
1167         r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1168 
1169         kfree(s);
1170 
1171         return r;
1172 }
1173 
1174 static ssize_t
1175 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1176                        loff_t *ppos)
1177 {
1178         struct ftrace_subsystem_dir *dir = filp->private_data;
1179         char *buf;
1180         int err;
1181 
1182         if (cnt >= PAGE_SIZE)
1183                 return -EINVAL;
1184 
1185         buf = (char *)__get_free_page(GFP_TEMPORARY);
1186         if (!buf)
1187                 return -ENOMEM;
1188 
1189         if (copy_from_user(buf, ubuf, cnt)) {
1190                 free_page((unsigned long) buf);
1191                 return -EFAULT;
1192         }
1193         buf[cnt] = '\0';
1194 
1195         err = apply_subsystem_event_filter(dir, buf);
1196         free_page((unsigned long) buf);
1197         if (err < 0)
1198                 return err;
1199 
1200         *ppos += cnt;
1201 
1202         return cnt;
1203 }
1204 
1205 static ssize_t
1206 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1207 {
1208         int (*func)(struct trace_seq *s) = filp->private_data;
1209         struct trace_seq *s;
1210         int r;
1211 
1212         if (*ppos)
1213                 return 0;
1214 
1215         s = kmalloc(sizeof(*s), GFP_KERNEL);
1216         if (!s)
1217                 return -ENOMEM;
1218 
1219         trace_seq_init(s);
1220 
1221         func(s);
1222         r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
1223 
1224         kfree(s);
1225 
1226         return r;
1227 }
1228 
1229 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1230 static int ftrace_event_set_open(struct inode *inode, struct file *file);
1231 static int ftrace_event_release(struct inode *inode, struct file *file);
1232 
1233 static const struct seq_operations show_event_seq_ops = {
1234         .start = t_start,
1235         .next = t_next,
1236         .show = t_show,
1237         .stop = t_stop,
1238 };
1239 
1240 static const struct seq_operations show_set_event_seq_ops = {
1241         .start = s_start,
1242         .next = s_next,
1243         .show = t_show,
1244         .stop = t_stop,
1245 };
1246 
1247 static const struct file_operations ftrace_avail_fops = {
1248         .open = ftrace_event_avail_open,
1249         .read = seq_read,
1250         .llseek = seq_lseek,
1251         .release = seq_release,
1252 };
1253 
1254 static const struct file_operations ftrace_set_event_fops = {
1255         .open = ftrace_event_set_open,
1256         .read = seq_read,
1257         .write = ftrace_event_write,
1258         .llseek = seq_lseek,
1259         .release = ftrace_event_release,
1260 };
1261 
1262 static const struct file_operations ftrace_enable_fops = {
1263         .open = tracing_open_generic,
1264         .read = event_enable_read,
1265         .write = event_enable_write,
1266         .llseek = default_llseek,
1267 };
1268 
1269 static const struct file_operations ftrace_event_format_fops = {
1270         .open = trace_format_open,
1271         .read = seq_read,
1272         .llseek = seq_lseek,
1273         .release = seq_release,
1274 };
1275 
1276 static const struct file_operations ftrace_event_id_fops = {
1277         .read = event_id_read,
1278         .llseek = default_llseek,
1279 };
1280 
1281 static const struct file_operations ftrace_event_filter_fops = {
1282         .open = tracing_open_generic,
1283         .read = event_filter_read,
1284         .write = event_filter_write,
1285         .llseek = default_llseek,
1286 };
1287 
1288 static const struct file_operations ftrace_subsystem_filter_fops = {
1289         .open = subsystem_open,
1290         .read = subsystem_filter_read,
1291         .write = subsystem_filter_write,
1292         .llseek = default_llseek,
1293         .release = subsystem_release,
1294 };
1295 
1296 static const struct file_operations ftrace_system_enable_fops = {
1297         .open = subsystem_open,
1298         .read = system_enable_read,
1299         .write = system_enable_write,
1300         .llseek = default_llseek,
1301         .release = subsystem_release,
1302 };
1303 
1304 static const struct file_operations ftrace_tr_enable_fops = {
1305         .open = system_tr_open,
1306         .read = system_enable_read,
1307         .write = system_enable_write,
1308         .llseek = default_llseek,
1309         .release = subsystem_release,
1310 };
1311 
1312 static const struct file_operations ftrace_show_header_fops = {
1313         .open = tracing_open_generic,
1314         .read = show_header,
1315         .llseek = default_llseek,
1316 };
1317 
1318 static int
1319 ftrace_event_open(struct inode *inode, struct file *file,
1320                   const struct seq_operations *seq_ops)
1321 {
1322         struct seq_file *m;
1323         int ret;
1324 
1325         ret = seq_open(file, seq_ops);
1326         if (ret < 0)
1327                 return ret;
1328         m = file->private_data;
1329         /* copy tr over to seq ops */
1330         m->private = inode->i_private;
1331 
1332         return ret;
1333 }
1334 
1335 static int ftrace_event_release(struct inode *inode, struct file *file)
1336 {
1337         struct trace_array *tr = inode->i_private;
1338 
1339         trace_array_put(tr);
1340 
1341         return seq_release(inode, file);
1342 }
1343 
1344 static int
1345 ftrace_event_avail_open(struct inode *inode, struct file *file)
1346 {
1347         const struct seq_operations *seq_ops = &show_event_seq_ops;
1348 
1349         return ftrace_event_open(inode, file, seq_ops);
1350 }
1351 
1352 static int
1353 ftrace_event_set_open(struct inode *inode, struct file *file)
1354 {
1355         const struct seq_operations *seq_ops = &show_set_event_seq_ops;
1356         struct trace_array *tr = inode->i_private;
1357         int ret;
1358 
1359         if (trace_array_get(tr) < 0)
1360                 return -ENODEV;
1361 
1362         if ((file->f_mode & FMODE_WRITE) &&
1363             (file->f_flags & O_TRUNC))
1364                 ftrace_clear_events(tr);
1365 
1366         ret = ftrace_event_open(inode, file, seq_ops);
1367         if (ret < 0)
1368                 trace_array_put(tr);
1369         return ret;
1370 }
1371 
1372 static struct event_subsystem *
1373 create_new_subsystem(const char *name)
1374 {
1375         struct event_subsystem *system;
1376 
1377         /* need to create new entry */
1378         system = kmalloc(sizeof(*system), GFP_KERNEL);
1379         if (!system)
1380                 return NULL;
1381 
1382         system->ref_count = 1;
1383 
1384         /* Only allocate if dynamic (kprobes and modules) */
1385         if (!core_kernel_data((unsigned long)name)) {
1386                 system->ref_count |= SYSTEM_FL_FREE_NAME;
1387                 system->name = kstrdup(name, GFP_KERNEL);
1388                 if (!system->name)
1389                         goto out_free;
1390         } else
1391                 system->name = name;
1392 
1393         system->filter = NULL;
1394 
1395         system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
1396         if (!system->filter)
1397                 goto out_free;
1398 
1399         list_add(&system->list, &event_subsystems);
1400 
1401         return system;
1402 
1403  out_free:
1404         if (system->ref_count & SYSTEM_FL_FREE_NAME)
1405                 kfree(system->name);
1406         kfree(system);
1407         return NULL;
1408 }
1409 
1410 static struct dentry *
1411 event_subsystem_dir(struct trace_array *tr, const char *name,
1412                     struct ftrace_event_file *file, struct dentry *parent)
1413 {
1414         struct ftrace_subsystem_dir *dir;
1415         struct event_subsystem *system;
1416         struct dentry *entry;
1417 
1418         /* First see if we did not already create this dir */
1419         list_for_each_entry(dir, &tr->systems, list) {
1420                 system = dir->subsystem;
1421                 if (strcmp(system->name, name) == 0) {
1422                         dir->nr_events++;
1423                         file->system = dir;
1424                         return dir->entry;
1425                 }
1426         }
1427 
1428         /* Now see if the system itself exists. */
1429         list_for_each_entry(system, &event_subsystems, list) {
1430                 if (strcmp(system->name, name) == 0)
1431                         break;
1432         }
1433         /* Reset system variable when not found */
1434         if (&system->list == &event_subsystems)
1435                 system = NULL;
1436 
1437         dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1438         if (!dir)
1439                 goto out_fail;
1440 
1441         if (!system) {
1442                 system = create_new_subsystem(name);
1443                 if (!system)
1444                         goto out_free;
1445         } else
1446                 __get_system(system);
1447 
1448         dir->entry = debugfs_create_dir(name, parent);
1449         if (!dir->entry) {
1450                 pr_warning("Failed to create system directory %s\n", name);
1451                 __put_system(system);
1452                 goto out_free;
1453         }
1454 
1455         dir->tr = tr;
1456         dir->ref_count = 1;
1457         dir->nr_events = 1;
1458         dir->subsystem = system;
1459         file->system = dir;
1460 
1461         entry = debugfs_create_file("filter", 0644, dir->entry, dir,
1462                                     &ftrace_subsystem_filter_fops);
1463         if (!entry) {
1464                 kfree(system->filter);
1465                 system->filter = NULL;
1466                 pr_warning("Could not create debugfs '%s/filter' entry\n", name);
1467         }
1468 
1469         trace_create_file("enable", 0644, dir->entry, dir,
1470                           &ftrace_system_enable_fops);
1471 
1472         list_add(&dir->list, &tr->systems);
1473 
1474         return dir->entry;
1475 
1476  out_free:
1477         kfree(dir);
1478  out_fail:
1479         /* Only print this message if failed on memory allocation */
1480         if (!dir || !system)
1481                 pr_warning("No memory to create event subsystem %s\n",
1482                            name);
1483         return NULL;
1484 }
1485 
1486 static int
1487 event_create_dir(struct dentry *parent, struct ftrace_event_file *file)
1488 {
1489         struct ftrace_event_call *call = file->event_call;
1490         struct trace_array *tr = file->tr;
1491         struct list_head *head;
1492         struct dentry *d_events;
1493         int ret;
1494 
1495         /*
1496          * If the trace point header did not define TRACE_SYSTEM
1497          * then the system would be called "TRACE_SYSTEM".
1498          */
1499         if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1500                 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1501                 if (!d_events)
1502                         return -ENOMEM;
1503         } else
1504                 d_events = parent;
1505 
1506         file->dir = debugfs_create_dir(call->name, d_events);
1507         if (!file->dir) {
1508                 pr_warning("Could not create debugfs '%s' directory\n",
1509                            call->name);
1510                 return -1;
1511         }
1512 
1513         if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1514                 trace_create_file("enable", 0644, file->dir, file,
1515                                   &ftrace_enable_fops);
1516 
1517 #ifdef CONFIG_PERF_EVENTS
1518         if (call->event.type && call->class->reg)
1519                 trace_create_file("id", 0444, file->dir,
1520                                   (void *)(long)call->event.type,
1521                                   &ftrace_event_id_fops);
1522 #endif
1523 
1524         /*
1525          * Other events may have the same class. Only update
1526          * the fields if they are not already defined.
1527          */
1528         head = trace_get_fields(call);
1529         if (list_empty(head)) {
1530                 ret = call->class->define_fields(call);
1531                 if (ret < 0) {
1532                         pr_warning("Could not initialize trace point"
1533                                    " events/%s\n", call->name);
1534                         return -1;
1535                 }
1536         }
1537         trace_create_file("filter", 0644, file->dir, call,
1538                           &ftrace_event_filter_fops);
1539 
1540         trace_create_file("format", 0444, file->dir, call,
1541                           &ftrace_event_format_fops);
1542 
1543         return 0;
1544 }
1545 
1546 static void remove_event_from_tracers(struct ftrace_event_call *call)
1547 {
1548         struct ftrace_event_file *file;
1549         struct trace_array *tr;
1550 
1551         do_for_each_event_file_safe(tr, file) {
1552                 if (file->event_call != call)
1553                         continue;
1554 
1555                 remove_event_file_dir(file);
1556                 /*
1557                  * The do_for_each_event_file_safe() is
1558                  * a double loop. After finding the call for this
1559                  * trace_array, we use break to jump to the next
1560                  * trace_array.
1561                  */
1562                 break;
1563         } while_for_each_event_file();
1564 }
1565 
1566 static void event_remove(struct ftrace_event_call *call)
1567 {
1568         struct trace_array *tr;
1569         struct ftrace_event_file *file;
1570 
1571         do_for_each_event_file(tr, file) {
1572                 if (file->event_call != call)
1573                         continue;
1574                 ftrace_event_enable_disable(file, 0);
1575                 /*
1576                  * The do_for_each_event_file() is
1577                  * a double loop. After finding the call for this
1578                  * trace_array, we use break to jump to the next
1579                  * trace_array.
1580                  */
1581                 break;
1582         } while_for_each_event_file();
1583 
1584         if (call->event.funcs)
1585                 __unregister_ftrace_event(&call->event);
1586         remove_event_from_tracers(call);
1587         list_del(&call->list);
1588 }
1589 
1590 static int event_init(struct ftrace_event_call *call)
1591 {
1592         int ret = 0;
1593 
1594         if (WARN_ON(!call->name))
1595                 return -EINVAL;
1596 
1597         if (call->class->raw_init) {
1598                 ret = call->class->raw_init(call);
1599                 if (ret < 0 && ret != -ENOSYS)
1600                         pr_warn("Could not initialize trace events/%s\n",
1601                                 call->name);
1602         }
1603 
1604         return ret;
1605 }
1606 
1607 static int
1608 __register_event(struct ftrace_event_call *call, struct module *mod)
1609 {
1610         int ret;
1611 
1612         ret = event_init(call);
1613         if (ret < 0)
1614                 return ret;
1615 
1616         list_add(&call->list, &ftrace_events);
1617         call->mod = mod;
1618 
1619         return 0;
1620 }
1621 
1622 static struct ftrace_event_file *
1623 trace_create_new_event(struct ftrace_event_call *call,
1624                        struct trace_array *tr)
1625 {
1626         struct ftrace_event_file *file;
1627 
1628         file = kmem_cache_alloc(file_cachep, GFP_TRACE);
1629         if (!file)
1630                 return NULL;
1631 
1632         file->event_call = call;
1633         file->tr = tr;
1634         atomic_set(&file->sm_ref, 0);
1635         list_add(&file->list, &tr->events);
1636 
1637         return file;
1638 }
1639 
1640 /* Add an event to a trace directory */
1641 static int
1642 __trace_add_new_event(struct ftrace_event_call *call, struct trace_array *tr)
1643 {
1644         struct ftrace_event_file *file;
1645 
1646         file = trace_create_new_event(call, tr);
1647         if (!file)
1648                 return -ENOMEM;
1649 
1650         return event_create_dir(tr->event_dir, file);
1651 }
1652 
1653 /*
1654  * Just create a decriptor for early init. A descriptor is required
1655  * for enabling events at boot. We want to enable events before
1656  * the filesystem is initialized.
1657  */
1658 static __init int
1659 __trace_early_add_new_event(struct ftrace_event_call *call,
1660                             struct trace_array *tr)
1661 {
1662         struct ftrace_event_file *file;
1663 
1664         file = trace_create_new_event(call, tr);
1665         if (!file)
1666                 return -ENOMEM;
1667 
1668         return 0;
1669 }
1670 
1671 struct ftrace_module_file_ops;
1672 static void __add_event_to_tracers(struct ftrace_event_call *call);
1673 
1674 /* Add an additional event_call dynamically */
1675 int trace_add_event_call(struct ftrace_event_call *call)
1676 {
1677         int ret;
1678         mutex_lock(&trace_types_lock);
1679         mutex_lock(&event_mutex);
1680 
1681         ret = __register_event(call, NULL);
1682         if (ret >= 0)
1683                 __add_event_to_tracers(call);
1684 
1685         mutex_unlock(&event_mutex);
1686         mutex_unlock(&trace_types_lock);
1687         return ret;
1688 }
1689 
1690 /*
1691  * Must be called under locking of trace_types_lock, event_mutex and
1692  * trace_event_sem.
1693  */
1694 static void __trace_remove_event_call(struct ftrace_event_call *call)
1695 {
1696         event_remove(call);
1697         trace_destroy_fields(call);
1698         destroy_preds(call);
1699 }
1700 
1701 static int probe_remove_event_call(struct ftrace_event_call *call)
1702 {
1703         struct trace_array *tr;
1704         struct ftrace_event_file *file;
1705 
1706 #ifdef CONFIG_PERF_EVENTS
1707         if (call->perf_refcount)
1708                 return -EBUSY;
1709 #endif
1710         do_for_each_event_file(tr, file) {
1711                 if (file->event_call != call)
1712                         continue;
1713                 /*
1714                  * We can't rely on ftrace_event_enable_disable(enable => 0)
1715                  * we are going to do, FTRACE_EVENT_FL_SOFT_MODE can suppress
1716                  * TRACE_REG_UNREGISTER.
1717                  */
1718                 if (file->flags & FTRACE_EVENT_FL_ENABLED)
1719                         return -EBUSY;
1720                 /*
1721                  * The do_for_each_event_file_safe() is
1722                  * a double loop. After finding the call for this
1723                  * trace_array, we use break to jump to the next
1724                  * trace_array.
1725                  */
1726                 break;
1727         } while_for_each_event_file();
1728 
1729         __trace_remove_event_call(call);
1730 
1731         return 0;
1732 }
1733 
1734 /* Remove an event_call */
1735 int trace_remove_event_call(struct ftrace_event_call *call)
1736 {
1737         int ret;
1738 
1739         mutex_lock(&trace_types_lock);
1740         mutex_lock(&event_mutex);
1741         down_write(&trace_event_sem);
1742         ret = probe_remove_event_call(call);
1743         up_write(&trace_event_sem);
1744         mutex_unlock(&event_mutex);
1745         mutex_unlock(&trace_types_lock);
1746 
1747         return ret;
1748 }
1749 
1750 #define for_each_event(event, start, end)                       \
1751         for (event = start;                                     \
1752              (unsigned long)event < (unsigned long)end;         \
1753              event++)
1754 
1755 #ifdef CONFIG_MODULES
1756 
1757 static void trace_module_add_events(struct module *mod)
1758 {
1759         struct ftrace_event_call **call, **start, **end;
1760 
1761         if (!mod->num_trace_events)
1762                 return;
1763 
1764         /* Don't add infrastructure for mods without tracepoints */
1765         if (trace_module_has_bad_taint(mod)) {
1766                 pr_err("%s: module has bad taint, not creating trace events\n",
1767                        mod->name);
1768                 return;
1769         }
1770 
1771         start = mod->trace_events;
1772         end = mod->trace_events + mod->num_trace_events;
1773 
1774         for_each_event(call, start, end) {
1775                 __register_event(*call, mod);
1776                 __add_event_to_tracers(*call);
1777         }
1778 }
1779 
1780 static void trace_module_remove_events(struct module *mod)
1781 {
1782         struct ftrace_event_call *call, *p;
1783         bool clear_trace = false;
1784 
1785         down_write(&trace_event_sem);
1786         list_for_each_entry_safe(call, p, &ftrace_events, list) {
1787                 if (call->mod == mod) {
1788                         if (call->flags & TRACE_EVENT_FL_WAS_ENABLED)
1789                                 clear_trace = true;
1790                         __trace_remove_event_call(call);
1791                 }
1792         }
1793         up_write(&trace_event_sem);
1794 
1795         /*
1796          * It is safest to reset the ring buffer if the module being unloaded
1797          * registered any events that were used. The only worry is if
1798          * a new module gets loaded, and takes on the same id as the events
1799          * of this module. When printing out the buffer, traced events left
1800          * over from this module may be passed to the new module events and
1801          * unexpected results may occur.
1802          */
1803         if (clear_trace)
1804                 tracing_reset_all_online_cpus();
1805 }
1806 
1807 static int trace_module_notify(struct notifier_block *self,
1808                                unsigned long val, void *data)
1809 {
1810         struct module *mod = data;
1811 
1812         mutex_lock(&trace_types_lock);
1813         mutex_lock(&event_mutex);
1814         switch (val) {
1815         case MODULE_STATE_COMING:
1816                 trace_module_add_events(mod);
1817                 break;
1818         case MODULE_STATE_GOING:
1819                 trace_module_remove_events(mod);
1820                 break;
1821         }
1822         mutex_unlock(&event_mutex);
1823         mutex_unlock(&trace_types_lock);
1824 
1825         return 0;
1826 }
1827 
1828 static struct notifier_block trace_module_nb = {
1829         .notifier_call = trace_module_notify,
1830         .priority = 0,
1831 };
1832 #endif /* CONFIG_MODULES */
1833 
1834 /* Create a new event directory structure for a trace directory. */
1835 static void
1836 __trace_add_event_dirs(struct trace_array *tr)
1837 {
1838         struct ftrace_event_call *call;
1839         int ret;
1840 
1841         list_for_each_entry(call, &ftrace_events, list) {
1842                 ret = __trace_add_new_event(call, tr);
1843                 if (ret < 0)
1844                         pr_warning("Could not create directory for event %s\n",
1845                                    call->name);
1846         }
1847 }
1848 
1849 #ifdef CONFIG_DYNAMIC_FTRACE
1850 
1851 /* Avoid typos */
1852 #define ENABLE_EVENT_STR        "enable_event"
1853 #define DISABLE_EVENT_STR       "disable_event"
1854 
1855 struct event_probe_data {
1856         struct ftrace_event_file        *file;
1857         unsigned long                   count;
1858         int                             ref;
1859         bool                            enable;
1860 };
1861 
1862 static struct ftrace_event_file *
1863 find_event_file(struct trace_array *tr, const char *system,  const char *event)
1864 {
1865         struct ftrace_event_file *file;
1866         struct ftrace_event_call *call;
1867 
1868         list_for_each_entry(file, &tr->events, list) {
1869 
1870                 call = file->event_call;
1871 
1872                 if (!call->name || !call->class || !call->class->reg)
1873                         continue;
1874 
1875                 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
1876                         continue;
1877 
1878                 if (strcmp(event, call->name) == 0 &&
1879                     strcmp(system, call->class->system) == 0)
1880                         return file;
1881         }
1882         return NULL;
1883 }
1884 
1885 static void
1886 event_enable_probe(unsigned long ip, unsigned long parent_ip, void **_data)
1887 {
1888         struct event_probe_data **pdata = (struct event_probe_data **)_data;
1889         struct event_probe_data *data = *pdata;
1890 
1891         if (!data)
1892                 return;
1893 
1894         if (data->enable)
1895                 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
1896         else
1897                 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &data->file->flags);
1898 }
1899 
1900 static void
1901 event_enable_count_probe(unsigned long ip, unsigned long parent_ip, void **_data)
1902 {
1903         struct event_probe_data **pdata = (struct event_probe_data **)_data;
1904         struct event_probe_data *data = *pdata;
1905 
1906         if (!data)
1907                 return;
1908 
1909         if (!data->count)
1910                 return;
1911 
1912         /* Skip if the event is in a state we want to switch to */
1913         if (data->enable == !(data->file->flags & FTRACE_EVENT_FL_SOFT_DISABLED))
1914                 return;
1915 
1916         if (data->count != -1)
1917                 (data->count)--;
1918 
1919         event_enable_probe(ip, parent_ip, _data);
1920 }
1921 
1922 static int
1923 event_enable_print(struct seq_file *m, unsigned long ip,
1924                       struct ftrace_probe_ops *ops, void *_data)
1925 {
1926         struct event_probe_data *data = _data;
1927 
1928         seq_printf(m, "%ps:", (void *)ip);
1929 
1930         seq_printf(m, "%s:%s:%s",
1931                    data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
1932                    data->file->event_call->class->system,
1933                    data->file->event_call->name);
1934 
1935         if (data->count == -1)
1936                 seq_printf(m, ":unlimited\n");
1937         else
1938                 seq_printf(m, ":count=%ld\n", data->count);
1939 
1940         return 0;
1941 }
1942 
1943 static int
1944 event_enable_init(struct ftrace_probe_ops *ops, unsigned long ip,
1945                   void **_data)
1946 {
1947         struct event_probe_data **pdata = (struct event_probe_data **)_data;
1948         struct event_probe_data *data = *pdata;
1949 
1950         data->ref++;
1951         return 0;
1952 }
1953 
1954 static void
1955 event_enable_free(struct ftrace_probe_ops *ops, unsigned long ip,
1956                   void **_data)
1957 {
1958         struct event_probe_data **pdata = (struct event_probe_data **)_data;
1959         struct event_probe_data *data = *pdata;
1960 
1961         if (WARN_ON_ONCE(data->ref <= 0))
1962                 return;
1963 
1964         data->ref--;
1965         if (!data->ref) {
1966                 /* Remove the SOFT_MODE flag */
1967                 __ftrace_event_enable_disable(data->file, 0, 1);
1968                 module_put(data->file->event_call->mod);
1969                 kfree(data);
1970         }
1971         *pdata = NULL;
1972 }
1973 
1974 static struct ftrace_probe_ops event_enable_probe_ops = {
1975         .func                   = event_enable_probe,
1976         .print                  = event_enable_print,
1977         .init                   = event_enable_init,
1978         .free                   = event_enable_free,
1979 };
1980 
1981 static struct ftrace_probe_ops event_enable_count_probe_ops = {
1982         .func                   = event_enable_count_probe,
1983         .print                  = event_enable_print,
1984         .init                   = event_enable_init,
1985         .free                   = event_enable_free,
1986 };
1987 
1988 static struct ftrace_probe_ops event_disable_probe_ops = {
1989         .func                   = event_enable_probe,
1990         .print                  = event_enable_print,
1991         .init                   = event_enable_init,
1992         .free                   = event_enable_free,
1993 };
1994 
1995 static struct ftrace_probe_ops event_disable_count_probe_ops = {
1996         .func                   = event_enable_count_probe,
1997         .print                  = event_enable_print,
1998         .init                   = event_enable_init,
1999         .free                   = event_enable_free,
2000 };
2001 
2002 static int
2003 event_enable_func(struct ftrace_hash *hash,
2004                   char *glob, char *cmd, char *param, int enabled)
2005 {
2006         struct trace_array *tr = top_trace_array();
2007         struct ftrace_event_file *file;
2008         struct ftrace_probe_ops *ops;
2009         struct event_probe_data *data;
2010         const char *system;
2011         const char *event;
2012         char *number;
2013         bool enable;
2014         int ret;
2015 
2016         /* hash funcs only work with set_ftrace_filter */
2017         if (!enabled || !param)
2018                 return -EINVAL;
2019 
2020         system = strsep(&param, ":");
2021         if (!param)
2022                 return -EINVAL;
2023 
2024         event = strsep(&param, ":");
2025 
2026         mutex_lock(&event_mutex);
2027 
2028         ret = -EINVAL;
2029         file = find_event_file(tr, system, event);
2030         if (!file)
2031                 goto out;
2032 
2033         enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2034 
2035         if (enable)
2036                 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2037         else
2038                 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2039 
2040         if (glob[0] == '!') {
2041                 unregister_ftrace_function_probe_func(glob+1, ops);
2042                 ret = 0;
2043                 goto out;
2044         }
2045 
2046         ret = -ENOMEM;
2047         data = kzalloc(sizeof(*data), GFP_KERNEL);
2048         if (!data)
2049                 goto out;
2050 
2051         data->enable = enable;
2052         data->count = -1;
2053         data->file = file;
2054 
2055         if (!param)
2056                 goto out_reg;
2057 
2058         number = strsep(&param, ":");
2059 
2060         ret = -EINVAL;
2061         if (!strlen(number))
2062                 goto out_free;
2063 
2064         /*
2065          * We use the callback data field (which is a pointer)
2066          * as our counter.
2067          */
2068         ret = kstrtoul(number, 0, &data->count);
2069         if (ret)
2070                 goto out_free;
2071 
2072  out_reg:
2073         /* Don't let event modules unload while probe registered */
2074         ret = try_module_get(file->event_call->mod);
2075         if (!ret) {
2076                 ret = -EBUSY;
2077                 goto out_free;
2078         }
2079 
2080         ret = __ftrace_event_enable_disable(file, 1, 1);
2081         if (ret < 0)
2082                 goto out_put;
2083         ret = register_ftrace_function_probe(glob, ops, data);
2084         /*
2085          * The above returns on success the # of functions enabled,
2086          * but if it didn't find any functions it returns zero.
2087          * Consider no functions a failure too.
2088          */
2089         if (!ret) {
2090                 ret = -ENOENT;
2091                 goto out_disable;
2092         } else if (ret < 0)
2093                 goto out_disable;
2094         /* Just return zero, not the number of enabled functions */
2095         ret = 0;
2096  out:
2097         mutex_unlock(&event_mutex);
2098         return ret;
2099 
2100  out_disable:
2101         __ftrace_event_enable_disable(file, 0, 1);
2102  out_put:
2103         module_put(file->event_call->mod);
2104  out_free:
2105         kfree(data);
2106         goto out;
2107 }
2108 
2109 static struct ftrace_func_command event_enable_cmd = {
2110         .name                   = ENABLE_EVENT_STR,
2111         .func                   = event_enable_func,
2112 };
2113 
2114 static struct ftrace_func_command event_disable_cmd = {
2115         .name                   = DISABLE_EVENT_STR,
2116         .func                   = event_enable_func,
2117 };
2118 
2119 static __init int register_event_cmds(void)
2120 {
2121         int ret;
2122 
2123         ret = register_ftrace_command(&event_enable_cmd);
2124         if (WARN_ON(ret < 0))
2125                 return ret;
2126         ret = register_ftrace_command(&event_disable_cmd);
2127         if (WARN_ON(ret < 0))
2128                 unregister_ftrace_command(&event_enable_cmd);
2129         return ret;
2130 }
2131 #else
2132 static inline int register_event_cmds(void) { return 0; }
2133 #endif /* CONFIG_DYNAMIC_FTRACE */
2134 
2135 /*
2136  * The top level array has already had its ftrace_event_file
2137  * descriptors created in order to allow for early events to
2138  * be recorded. This function is called after the debugfs has been
2139  * initialized, and we now have to create the files associated
2140  * to the events.
2141  */
2142 static __init void
2143 __trace_early_add_event_dirs(struct trace_array *tr)
2144 {
2145         struct ftrace_event_file *file;
2146         int ret;
2147 
2148 
2149         list_for_each_entry(file, &tr->events, list) {
2150                 ret = event_create_dir(tr->event_dir, file);
2151                 if (ret < 0)
2152                         pr_warning("Could not create directory for event %s\n",
2153                                    file->event_call->name);
2154         }
2155 }
2156 
2157 /*
2158  * For early boot up, the top trace array requires to have
2159  * a list of events that can be enabled. This must be done before
2160  * the filesystem is set up in order to allow events to be traced
2161  * early.
2162  */
2163 static __init void
2164 __trace_early_add_events(struct trace_array *tr)
2165 {
2166         struct ftrace_event_call *call;
2167         int ret;
2168 
2169         list_for_each_entry(call, &ftrace_events, list) {
2170                 /* Early boot up should not have any modules loaded */
2171                 if (WARN_ON_ONCE(call->mod))
2172                         continue;
2173 
2174                 ret = __trace_early_add_new_event(call, tr);
2175                 if (ret < 0)
2176                         pr_warning("Could not create early event %s\n",
2177                                    call->name);
2178         }
2179 }
2180 
2181 /* Remove the event directory structure for a trace directory. */
2182 static void
2183 __trace_remove_event_dirs(struct trace_array *tr)
2184 {
2185         struct ftrace_event_file *file, *next;
2186 
2187         list_for_each_entry_safe(file, next, &tr->events, list)
2188                 remove_event_file_dir(file);
2189 }
2190 
2191 static void __add_event_to_tracers(struct ftrace_event_call *call)
2192 {
2193         struct trace_array *tr;
2194 
2195         list_for_each_entry(tr, &ftrace_trace_arrays, list)
2196                 __trace_add_new_event(call, tr);
2197 }
2198 
2199 extern struct ftrace_event_call *__start_ftrace_events[];
2200 extern struct ftrace_event_call *__stop_ftrace_events[];
2201 
2202 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2203 
2204 static __init int setup_trace_event(char *str)
2205 {
2206         strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
2207         ring_buffer_expanded = true;
2208         tracing_selftest_disabled = true;
2209 
2210         return 1;
2211 }
2212 __setup("trace_event=", setup_trace_event);
2213 
2214 /* Expects to have event_mutex held when called */
2215 static int
2216 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
2217 {
2218         struct dentry *d_events;
2219         struct dentry *entry;
2220 
2221         entry = debugfs_create_file("set_event", 0644, parent,
2222                                     tr, &ftrace_set_event_fops);
2223         if (!entry) {
2224                 pr_warning("Could not create debugfs 'set_event' entry\n");
2225                 return -ENOMEM;
2226         }
2227 
2228         d_events = debugfs_create_dir("events", parent);
2229         if (!d_events) {
2230                 pr_warning("Could not create debugfs 'events' directory\n");
2231                 return -ENOMEM;
2232         }
2233 
2234         /* ring buffer internal formats */
2235         trace_create_file("header_page", 0444, d_events,
2236                           ring_buffer_print_page_header,
2237                           &ftrace_show_header_fops);
2238 
2239         trace_create_file("header_event", 0444, d_events,
2240                           ring_buffer_print_entry_header,
2241                           &ftrace_show_header_fops);
2242 
2243         trace_create_file("enable", 0644, d_events,
2244                           tr, &ftrace_tr_enable_fops);
2245 
2246         tr->event_dir = d_events;
2247 
2248         return 0;
2249 }
2250 
2251 /**
2252  * event_trace_add_tracer - add a instance of a trace_array to events
2253  * @parent: The parent dentry to place the files/directories for events in
2254  * @tr: The trace array associated with these events
2255  *
2256  * When a new instance is created, it needs to set up its events
2257  * directory, as well as other files associated with events. It also
2258  * creates the event hierachry in the @parent/events directory.
2259  *
2260  * Returns 0 on success.
2261  */
2262 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2263 {
2264         int ret;
2265 
2266         mutex_lock(&event_mutex);
2267 
2268         ret = create_event_toplevel_files(parent, tr);
2269         if (ret)
2270                 goto out_unlock;
2271 
2272         down_write(&trace_event_sem);
2273         __trace_add_event_dirs(tr);
2274         up_write(&trace_event_sem);
2275 
2276  out_unlock:
2277         mutex_unlock(&event_mutex);
2278 
2279         return ret;
2280 }
2281 
2282 /*
2283  * The top trace array already had its file descriptors created.
2284  * Now the files themselves need to be created.
2285  */
2286 static __init int
2287 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2288 {
2289         int ret;
2290 
2291         mutex_lock(&event_mutex);
2292 
2293         ret = create_event_toplevel_files(parent, tr);
2294         if (ret)
2295                 goto out_unlock;
2296 
2297         down_write(&trace_event_sem);
2298         __trace_early_add_event_dirs(tr);
2299         up_write(&trace_event_sem);
2300 
2301  out_unlock:
2302         mutex_unlock(&event_mutex);
2303 
2304         return ret;
2305 }
2306 
2307 int event_trace_del_tracer(struct trace_array *tr)
2308 {
2309         mutex_lock(&event_mutex);
2310 
2311         /* Disable any running events */
2312         __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
2313 
2314         down_write(&trace_event_sem);
2315         __trace_remove_event_dirs(tr);
2316         debugfs_remove_recursive(tr->event_dir);
2317         up_write(&trace_event_sem);
2318 
2319         tr->event_dir = NULL;
2320 
2321         mutex_unlock(&event_mutex);
2322 
2323         return 0;
2324 }
2325 
2326 static __init int event_trace_memsetup(void)
2327 {
2328         field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
2329         file_cachep = KMEM_CACHE(ftrace_event_file, SLAB_PANIC);
2330         return 0;
2331 }
2332 
2333 static __init int event_trace_enable(void)
2334 {
2335         struct trace_array *tr = top_trace_array();
2336         struct ftrace_event_call **iter, *call;
2337         char *buf = bootup_event_buf;
2338         char *token;
2339         int ret;
2340 
2341         for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
2342 
2343                 call = *iter;
2344                 ret = event_init(call);
2345                 if (!ret)
2346                         list_add(&call->list, &ftrace_events);
2347         }
2348 
2349         /*
2350          * We need the top trace array to have a working set of trace
2351          * points at early init, before the debug files and directories
2352          * are created. Create the file entries now, and attach them
2353          * to the actual file dentries later.
2354          */
2355         __trace_early_add_events(tr);
2356 
2357         while (true) {
2358                 token = strsep(&buf, ",");
2359 
2360                 if (!token)
2361                         break;
2362                 if (!*token)
2363                         continue;
2364 
2365                 ret = ftrace_set_clr_event(tr, token, 1);
2366                 if (ret)
2367                         pr_warn("Failed to enable trace event: %s\n", token);
2368         }
2369 
2370         trace_printk_start_comm();
2371 
2372         register_event_cmds();
2373 
2374         return 0;
2375 }
2376 
2377 static __init int event_trace_init(void)
2378 {
2379         struct trace_array *tr;
2380         struct dentry *d_tracer;
2381         struct dentry *entry;
2382         int ret;
2383 
2384         tr = top_trace_array();
2385 
2386         d_tracer = tracing_init_dentry();
2387         if (!d_tracer)
2388                 return 0;
2389 
2390         entry = debugfs_create_file("available_events", 0444, d_tracer,
2391                                     tr, &ftrace_avail_fops);
2392         if (!entry)
2393                 pr_warning("Could not create debugfs "
2394                            "'available_events' entry\n");
2395 
2396         if (trace_define_common_fields())
2397                 pr_warning("tracing: Failed to allocate common fields");
2398 
2399         ret = early_event_add_tracer(d_tracer, tr);
2400         if (ret)
2401                 return ret;
2402 
2403 #ifdef CONFIG_MODULES
2404         ret = register_module_notifier(&trace_module_nb);
2405         if (ret)
2406                 pr_warning("Failed to register trace events module notifier\n");
2407 #endif
2408         return 0;
2409 }
2410 early_initcall(event_trace_memsetup);
2411 core_initcall(event_trace_enable);
2412 fs_initcall(event_trace_init);
2413 
2414 #ifdef CONFIG_FTRACE_STARTUP_TEST
2415 
2416 static DEFINE_SPINLOCK(test_spinlock);
2417 static DEFINE_SPINLOCK(test_spinlock_irq);
2418 static DEFINE_MUTEX(test_mutex);
2419 
2420 static __init void test_work(struct work_struct *dummy)
2421 {
2422         spin_lock(&test_spinlock);
2423         spin_lock_irq(&test_spinlock_irq);
2424         udelay(1);
2425         spin_unlock_irq(&test_spinlock_irq);
2426         spin_unlock(&test_spinlock);
2427 
2428         mutex_lock(&test_mutex);
2429         msleep(1);
2430         mutex_unlock(&test_mutex);
2431 }
2432 
2433 static __init int event_test_thread(void *unused)
2434 {
2435         void *test_malloc;
2436 
2437         test_malloc = kmalloc(1234, GFP_KERNEL);
2438         if (!test_malloc)
2439                 pr_info("failed to kmalloc\n");
2440 
2441         schedule_on_each_cpu(test_work);
2442 
2443         kfree(test_malloc);
2444 
2445         set_current_state(TASK_INTERRUPTIBLE);
2446         while (!kthread_should_stop())
2447                 schedule();
2448 
2449         return 0;
2450 }
2451 
2452 /*
2453  * Do various things that may trigger events.
2454  */
2455 static __init void event_test_stuff(void)
2456 {
2457         struct task_struct *test_thread;
2458 
2459         test_thread = kthread_run(event_test_thread, NULL, "test-events");
2460         msleep(1);
2461         kthread_stop(test_thread);
2462 }
2463 
2464 /*
2465  * For every trace event defined, we will test each trace point separately,
2466  * and then by groups, and finally all trace points.
2467  */
2468 static __init void event_trace_self_tests(void)
2469 {
2470         struct ftrace_subsystem_dir *dir;
2471         struct ftrace_event_file *file;
2472         struct ftrace_event_call *call;
2473         struct event_subsystem *system;
2474         struct trace_array *tr;
2475         int ret;
2476 
2477         tr = top_trace_array();
2478 
2479         pr_info("Running tests on trace events:\n");
2480 
2481         list_for_each_entry(file, &tr->events, list) {
2482 
2483                 call = file->event_call;
2484 
2485                 /* Only test those that have a probe */
2486                 if (!call->class || !call->class->probe)
2487                         continue;
2488 
2489 /*
2490  * Testing syscall events here is pretty useless, but
2491  * we still do it if configured. But this is time consuming.
2492  * What we really need is a user thread to perform the
2493  * syscalls as we test.
2494  */
2495 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
2496                 if (call->class->system &&
2497                     strcmp(call->class->system, "syscalls") == 0)
2498                         continue;
2499 #endif
2500 
2501                 pr_info("Testing event %s: ", call->name);
2502 
2503                 /*
2504                  * If an event is already enabled, someone is using
2505                  * it and the self test should not be on.
2506                  */
2507                 if (file->flags & FTRACE_EVENT_FL_ENABLED) {
2508                         pr_warning("Enabled event during self test!\n");
2509                         WARN_ON_ONCE(1);
2510                         continue;
2511                 }
2512 
2513                 ftrace_event_enable_disable(file, 1);
2514                 event_test_stuff();
2515                 ftrace_event_enable_disable(file, 0);
2516 
2517                 pr_cont("OK\n");
2518         }
2519 
2520         /* Now test at the sub system level */
2521 
2522         pr_info("Running tests on trace event systems:\n");
2523 
2524         list_for_each_entry(dir, &tr->systems, list) {
2525 
2526                 system = dir->subsystem;
2527 
2528                 /* the ftrace system is special, skip it */
2529                 if (strcmp(system->name, "ftrace") == 0)
2530                         continue;
2531 
2532                 pr_info("Testing event system %s: ", system->name);
2533 
2534                 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
2535                 if (WARN_ON_ONCE(ret)) {
2536                         pr_warning("error enabling system %s\n",
2537                                    system->name);
2538                         continue;
2539                 }
2540 
2541                 event_test_stuff();
2542 
2543                 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
2544                 if (WARN_ON_ONCE(ret)) {
2545                         pr_warning("error disabling system %s\n",
2546                                    system->name);
2547                         continue;
2548                 }
2549 
2550                 pr_cont("OK\n");
2551         }
2552 
2553         /* Test with all events enabled */
2554 
2555         pr_info("Running tests on all trace events:\n");
2556         pr_info("Testing all events: ");
2557 
2558         ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
2559         if (WARN_ON_ONCE(ret)) {
2560                 pr_warning("error enabling all events\n");
2561                 return;
2562         }
2563 
2564         event_test_stuff();
2565 
2566         /* reset sysname */
2567         ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
2568         if (WARN_ON_ONCE(ret)) {
2569                 pr_warning("error disabling all events\n");
2570                 return;
2571         }
2572 
2573         pr_cont("OK\n");
2574 }
2575 
2576 #ifdef CONFIG_FUNCTION_TRACER
2577 
2578 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
2579 
2580 static void
2581 function_test_events_call(unsigned long ip, unsigned long parent_ip,
2582                           struct ftrace_ops *op, struct pt_regs *pt_regs)
2583 {
2584         struct ring_buffer_event *event;
2585         struct ring_buffer *buffer;
2586         struct ftrace_entry *entry;
2587         unsigned long flags;
2588         long disabled;
2589         int cpu;
2590         int pc;
2591 
2592         pc = preempt_count();
2593         preempt_disable_notrace();
2594         cpu = raw_smp_processor_id();
2595         disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
2596 
2597         if (disabled != 1)
2598                 goto out;
2599 
2600         local_save_flags(flags);
2601 
2602         event = trace_current_buffer_lock_reserve(&buffer,
2603                                                   TRACE_FN, sizeof(*entry),
2604                                                   flags, pc);
2605         if (!event)
2606                 goto out;
2607         entry   = ring_buffer_event_data(event);
2608         entry->ip                       = ip;
2609         entry->parent_ip                = parent_ip;
2610 
2611         trace_buffer_unlock_commit(buffer, event, flags, pc);
2612 
2613  out:
2614         atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
2615         preempt_enable_notrace();
2616 }
2617 
2618 static struct ftrace_ops trace_ops __initdata  =
2619 {
2620         .func = function_test_events_call,
2621         .flags = FTRACE_OPS_FL_RECURSION_SAFE,
2622 };
2623 
2624 static __init void event_trace_self_test_with_function(void)
2625 {
2626         int ret;
2627         ret = register_ftrace_function(&trace_ops);
2628         if (WARN_ON(ret < 0)) {
2629                 pr_info("Failed to enable function tracer for event tests\n");
2630                 return;
2631         }
2632         pr_info("Running tests again, along with the function tracer\n");
2633         event_trace_self_tests();
2634         unregister_ftrace_function(&trace_ops);
2635 }
2636 #else
2637 static __init void event_trace_self_test_with_function(void)
2638 {
2639 }
2640 #endif
2641 
2642 static __init int event_trace_self_tests_init(void)
2643 {
2644         if (!tracing_selftest_disabled) {
2645                 event_trace_self_tests();
2646                 event_trace_self_test_with_function();
2647         }
2648 
2649         return 0;
2650 }
2651 
2652 late_initcall(event_trace_self_tests_init);
2653 
2654 #endif
2655 

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

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

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

osdn.jp