GNU Linux-libre 4.14.332-gnu1
[releases.git] / kernel / trace / trace_events_trigger.c
1 /*
2  * trace_events_trigger - trace event triggers
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  * Copyright (C) 2013 Tom Zanussi <tom.zanussi@linux.intel.com>
19  */
20
21 #include <linux/module.h>
22 #include <linux/ctype.h>
23 #include <linux/mutex.h>
24 #include <linux/slab.h>
25 #include <linux/rculist.h>
26
27 #include "trace.h"
28
29 static LIST_HEAD(trigger_commands);
30 static DEFINE_MUTEX(trigger_cmd_mutex);
31
32 void trigger_data_free(struct event_trigger_data *data)
33 {
34         if (data->cmd_ops->set_filter)
35                 data->cmd_ops->set_filter(NULL, data, NULL);
36
37         synchronize_sched(); /* make sure current triggers exit before free */
38         kfree(data);
39 }
40
41 /**
42  * event_triggers_call - Call triggers associated with a trace event
43  * @file: The trace_event_file associated with the event
44  * @rec: The trace entry for the event, NULL for unconditional invocation
45  *
46  * For each trigger associated with an event, invoke the trigger
47  * function registered with the associated trigger command.  If rec is
48  * non-NULL, it means that the trigger requires further processing and
49  * shouldn't be unconditionally invoked.  If rec is non-NULL and the
50  * trigger has a filter associated with it, rec will checked against
51  * the filter and if the record matches the trigger will be invoked.
52  * If the trigger is a 'post_trigger', meaning it shouldn't be invoked
53  * in any case until the current event is written, the trigger
54  * function isn't invoked but the bit associated with the deferred
55  * trigger is set in the return value.
56  *
57  * Returns an enum event_trigger_type value containing a set bit for
58  * any trigger that should be deferred, ETT_NONE if nothing to defer.
59  *
60  * Called from tracepoint handlers (with rcu_read_lock_sched() held).
61  *
62  * Return: an enum event_trigger_type value containing a set bit for
63  * any trigger that should be deferred, ETT_NONE if nothing to defer.
64  */
65 enum event_trigger_type
66 event_triggers_call(struct trace_event_file *file, void *rec)
67 {
68         struct event_trigger_data *data;
69         enum event_trigger_type tt = ETT_NONE;
70         struct event_filter *filter;
71
72         if (list_empty(&file->triggers))
73                 return tt;
74
75         list_for_each_entry_rcu(data, &file->triggers, list) {
76                 if (data->paused)
77                         continue;
78                 if (!rec) {
79                         data->ops->func(data, rec);
80                         continue;
81                 }
82                 filter = rcu_dereference_sched(data->filter);
83                 if (filter && !filter_match_preds(filter, rec))
84                         continue;
85                 if (event_command_post_trigger(data->cmd_ops)) {
86                         tt |= data->cmd_ops->trigger_type;
87                         continue;
88                 }
89                 data->ops->func(data, rec);
90         }
91         return tt;
92 }
93 EXPORT_SYMBOL_GPL(event_triggers_call);
94
95 /**
96  * event_triggers_post_call - Call 'post_triggers' for a trace event
97  * @file: The trace_event_file associated with the event
98  * @tt: enum event_trigger_type containing a set bit for each trigger to invoke
99  * @rec: The trace entry for the event
100  *
101  * For each trigger associated with an event, invoke the trigger
102  * function registered with the associated trigger command, if the
103  * corresponding bit is set in the tt enum passed into this function.
104  * See @event_triggers_call for details on how those bits are set.
105  *
106  * Called from tracepoint handlers (with rcu_read_lock_sched() held).
107  */
108 void
109 event_triggers_post_call(struct trace_event_file *file,
110                          enum event_trigger_type tt,
111                          void *rec)
112 {
113         struct event_trigger_data *data;
114
115         list_for_each_entry_rcu(data, &file->triggers, list) {
116                 if (data->paused)
117                         continue;
118                 if (data->cmd_ops->trigger_type & tt)
119                         data->ops->func(data, rec);
120         }
121 }
122 EXPORT_SYMBOL_GPL(event_triggers_post_call);
123
124 #define SHOW_AVAILABLE_TRIGGERS (void *)(1UL)
125
126 static void *trigger_next(struct seq_file *m, void *t, loff_t *pos)
127 {
128         struct trace_event_file *event_file = event_file_data(m->private);
129
130         if (t == SHOW_AVAILABLE_TRIGGERS) {
131                 (*pos)++;
132                 return NULL;
133         }
134         return seq_list_next(t, &event_file->triggers, pos);
135 }
136
137 static void *trigger_start(struct seq_file *m, loff_t *pos)
138 {
139         struct trace_event_file *event_file;
140
141         /* ->stop() is called even if ->start() fails */
142         mutex_lock(&event_mutex);
143         event_file = event_file_data(m->private);
144         if (unlikely(!event_file))
145                 return ERR_PTR(-ENODEV);
146
147         if (list_empty(&event_file->triggers))
148                 return *pos == 0 ? SHOW_AVAILABLE_TRIGGERS : NULL;
149
150         return seq_list_start(&event_file->triggers, *pos);
151 }
152
153 static void trigger_stop(struct seq_file *m, void *t)
154 {
155         mutex_unlock(&event_mutex);
156 }
157
158 static int trigger_show(struct seq_file *m, void *v)
159 {
160         struct event_trigger_data *data;
161         struct event_command *p;
162
163         if (v == SHOW_AVAILABLE_TRIGGERS) {
164                 seq_puts(m, "# Available triggers:\n");
165                 seq_putc(m, '#');
166                 mutex_lock(&trigger_cmd_mutex);
167                 list_for_each_entry_reverse(p, &trigger_commands, list)
168                         seq_printf(m, " %s", p->name);
169                 seq_putc(m, '\n');
170                 mutex_unlock(&trigger_cmd_mutex);
171                 return 0;
172         }
173
174         data = list_entry(v, struct event_trigger_data, list);
175         data->ops->print(m, data->ops, data);
176
177         return 0;
178 }
179
180 static const struct seq_operations event_triggers_seq_ops = {
181         .start = trigger_start,
182         .next = trigger_next,
183         .stop = trigger_stop,
184         .show = trigger_show,
185 };
186
187 static int event_trigger_regex_open(struct inode *inode, struct file *file)
188 {
189         int ret = 0;
190
191         mutex_lock(&event_mutex);
192
193         if (unlikely(!event_file_data(file))) {
194                 mutex_unlock(&event_mutex);
195                 return -ENODEV;
196         }
197
198         if ((file->f_mode & FMODE_WRITE) &&
199             (file->f_flags & O_TRUNC)) {
200                 struct trace_event_file *event_file;
201                 struct event_command *p;
202
203                 event_file = event_file_data(file);
204
205                 list_for_each_entry(p, &trigger_commands, list) {
206                         if (p->unreg_all)
207                                 p->unreg_all(event_file);
208                 }
209         }
210
211         if (file->f_mode & FMODE_READ) {
212                 ret = seq_open(file, &event_triggers_seq_ops);
213                 if (!ret) {
214                         struct seq_file *m = file->private_data;
215                         m->private = file;
216                 }
217         }
218
219         mutex_unlock(&event_mutex);
220
221         return ret;
222 }
223
224 static int trigger_process_regex(struct trace_event_file *file, char *buff)
225 {
226         char *command, *next;
227         struct event_command *p;
228         int ret = -EINVAL;
229
230         next = buff = skip_spaces(buff);
231         command = strsep(&next, ": \t");
232         if (next) {
233                 next = skip_spaces(next);
234                 if (!*next)
235                         next = NULL;
236         }
237         command = (command[0] != '!') ? command : command + 1;
238
239         mutex_lock(&trigger_cmd_mutex);
240         list_for_each_entry(p, &trigger_commands, list) {
241                 if (strcmp(p->name, command) == 0) {
242                         ret = p->func(p, file, buff, command, next);
243                         goto out_unlock;
244                 }
245         }
246  out_unlock:
247         mutex_unlock(&trigger_cmd_mutex);
248
249         return ret;
250 }
251
252 static ssize_t event_trigger_regex_write(struct file *file,
253                                          const char __user *ubuf,
254                                          size_t cnt, loff_t *ppos)
255 {
256         struct trace_event_file *event_file;
257         ssize_t ret;
258         char *buf;
259
260         if (!cnt)
261                 return 0;
262
263         if (cnt >= PAGE_SIZE)
264                 return -EINVAL;
265
266         buf = memdup_user_nul(ubuf, cnt);
267         if (IS_ERR(buf))
268                 return PTR_ERR(buf);
269
270         strim(buf);
271
272         mutex_lock(&event_mutex);
273         event_file = event_file_data(file);
274         if (unlikely(!event_file)) {
275                 mutex_unlock(&event_mutex);
276                 kfree(buf);
277                 return -ENODEV;
278         }
279         ret = trigger_process_regex(event_file, buf);
280         mutex_unlock(&event_mutex);
281
282         kfree(buf);
283         if (ret < 0)
284                 goto out;
285
286         *ppos += cnt;
287         ret = cnt;
288  out:
289         return ret;
290 }
291
292 static int event_trigger_regex_release(struct inode *inode, struct file *file)
293 {
294         mutex_lock(&event_mutex);
295
296         if (file->f_mode & FMODE_READ)
297                 seq_release(inode, file);
298
299         mutex_unlock(&event_mutex);
300
301         return 0;
302 }
303
304 static ssize_t
305 event_trigger_write(struct file *filp, const char __user *ubuf,
306                     size_t cnt, loff_t *ppos)
307 {
308         return event_trigger_regex_write(filp, ubuf, cnt, ppos);
309 }
310
311 static int
312 event_trigger_open(struct inode *inode, struct file *filp)
313 {
314         return event_trigger_regex_open(inode, filp);
315 }
316
317 static int
318 event_trigger_release(struct inode *inode, struct file *file)
319 {
320         return event_trigger_regex_release(inode, file);
321 }
322
323 const struct file_operations event_trigger_fops = {
324         .open = event_trigger_open,
325         .read = seq_read,
326         .write = event_trigger_write,
327         .llseek = tracing_lseek,
328         .release = event_trigger_release,
329 };
330
331 /*
332  * Currently we only register event commands from __init, so mark this
333  * __init too.
334  */
335 __init int register_event_command(struct event_command *cmd)
336 {
337         struct event_command *p;
338         int ret = 0;
339
340         mutex_lock(&trigger_cmd_mutex);
341         list_for_each_entry(p, &trigger_commands, list) {
342                 if (strcmp(cmd->name, p->name) == 0) {
343                         ret = -EBUSY;
344                         goto out_unlock;
345                 }
346         }
347         list_add(&cmd->list, &trigger_commands);
348  out_unlock:
349         mutex_unlock(&trigger_cmd_mutex);
350
351         return ret;
352 }
353
354 /*
355  * Currently we only unregister event commands from __init, so mark
356  * this __init too.
357  */
358 __init int unregister_event_command(struct event_command *cmd)
359 {
360         struct event_command *p, *n;
361         int ret = -ENODEV;
362
363         mutex_lock(&trigger_cmd_mutex);
364         list_for_each_entry_safe(p, n, &trigger_commands, list) {
365                 if (strcmp(cmd->name, p->name) == 0) {
366                         ret = 0;
367                         list_del_init(&p->list);
368                         goto out_unlock;
369                 }
370         }
371  out_unlock:
372         mutex_unlock(&trigger_cmd_mutex);
373
374         return ret;
375 }
376
377 /**
378  * event_trigger_print - Generic event_trigger_ops @print implementation
379  * @name: The name of the event trigger
380  * @m: The seq_file being printed to
381  * @data: Trigger-specific data
382  * @filter_str: filter_str to print, if present
383  *
384  * Common implementation for event triggers to print themselves.
385  *
386  * Usually wrapped by a function that simply sets the @name of the
387  * trigger command and then invokes this.
388  *
389  * Return: 0 on success, errno otherwise
390  */
391 static int
392 event_trigger_print(const char *name, struct seq_file *m,
393                     void *data, char *filter_str)
394 {
395         long count = (long)data;
396
397         seq_puts(m, name);
398
399         if (count == -1)
400                 seq_puts(m, ":unlimited");
401         else
402                 seq_printf(m, ":count=%ld", count);
403
404         if (filter_str)
405                 seq_printf(m, " if %s\n", filter_str);
406         else
407                 seq_putc(m, '\n');
408
409         return 0;
410 }
411
412 /**
413  * event_trigger_init - Generic event_trigger_ops @init implementation
414  * @ops: The trigger ops associated with the trigger
415  * @data: Trigger-specific data
416  *
417  * Common implementation of event trigger initialization.
418  *
419  * Usually used directly as the @init method in event trigger
420  * implementations.
421  *
422  * Return: 0 on success, errno otherwise
423  */
424 int event_trigger_init(struct event_trigger_ops *ops,
425                        struct event_trigger_data *data)
426 {
427         data->ref++;
428         return 0;
429 }
430
431 /**
432  * event_trigger_free - Generic event_trigger_ops @free implementation
433  * @ops: The trigger ops associated with the trigger
434  * @data: Trigger-specific data
435  *
436  * Common implementation of event trigger de-initialization.
437  *
438  * Usually used directly as the @free method in event trigger
439  * implementations.
440  */
441 static void
442 event_trigger_free(struct event_trigger_ops *ops,
443                    struct event_trigger_data *data)
444 {
445         if (WARN_ON_ONCE(data->ref <= 0))
446                 return;
447
448         data->ref--;
449         if (!data->ref)
450                 trigger_data_free(data);
451 }
452
453 int trace_event_trigger_enable_disable(struct trace_event_file *file,
454                                        int trigger_enable)
455 {
456         int ret = 0;
457
458         if (trigger_enable) {
459                 if (atomic_inc_return(&file->tm_ref) > 1)
460                         return ret;
461                 set_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
462                 ret = trace_event_enable_disable(file, 1, 1);
463         } else {
464                 if (atomic_dec_return(&file->tm_ref) > 0)
465                         return ret;
466                 clear_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
467                 ret = trace_event_enable_disable(file, 0, 1);
468         }
469
470         return ret;
471 }
472
473 /**
474  * clear_event_triggers - Clear all triggers associated with a trace array
475  * @tr: The trace array to clear
476  *
477  * For each trigger, the triggering event has its tm_ref decremented
478  * via trace_event_trigger_enable_disable(), and any associated event
479  * (in the case of enable/disable_event triggers) will have its sm_ref
480  * decremented via free()->trace_event_enable_disable().  That
481  * combination effectively reverses the soft-mode/trigger state added
482  * by trigger registration.
483  *
484  * Must be called with event_mutex held.
485  */
486 void
487 clear_event_triggers(struct trace_array *tr)
488 {
489         struct trace_event_file *file;
490
491         list_for_each_entry(file, &tr->events, list) {
492                 struct event_trigger_data *data, *n;
493                 list_for_each_entry_safe(data, n, &file->triggers, list) {
494                         trace_event_trigger_enable_disable(file, 0);
495                         list_del_rcu(&data->list);
496                         if (data->ops->free)
497                                 data->ops->free(data->ops, data);
498                 }
499         }
500 }
501
502 /**
503  * update_cond_flag - Set or reset the TRIGGER_COND bit
504  * @file: The trace_event_file associated with the event
505  *
506  * If an event has triggers and any of those triggers has a filter or
507  * a post_trigger, trigger invocation needs to be deferred until after
508  * the current event has logged its data, and the event should have
509  * its TRIGGER_COND bit set, otherwise the TRIGGER_COND bit should be
510  * cleared.
511  */
512 void update_cond_flag(struct trace_event_file *file)
513 {
514         struct event_trigger_data *data;
515         bool set_cond = false;
516
517         list_for_each_entry_rcu(data, &file->triggers, list) {
518                 if (data->filter || event_command_post_trigger(data->cmd_ops) ||
519                     event_command_needs_rec(data->cmd_ops)) {
520                         set_cond = true;
521                         break;
522                 }
523         }
524
525         if (set_cond)
526                 set_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
527         else
528                 clear_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
529 }
530
531 /**
532  * register_trigger - Generic event_command @reg implementation
533  * @glob: The raw string used to register the trigger
534  * @ops: The trigger ops associated with the trigger
535  * @data: Trigger-specific data to associate with the trigger
536  * @file: The trace_event_file associated with the event
537  *
538  * Common implementation for event trigger registration.
539  *
540  * Usually used directly as the @reg method in event command
541  * implementations.
542  *
543  * Return: 0 on success, errno otherwise
544  */
545 static int register_trigger(char *glob, struct event_trigger_ops *ops,
546                             struct event_trigger_data *data,
547                             struct trace_event_file *file)
548 {
549         struct event_trigger_data *test;
550         int ret = 0;
551
552         list_for_each_entry_rcu(test, &file->triggers, list) {
553                 if (test->cmd_ops->trigger_type == data->cmd_ops->trigger_type) {
554                         ret = -EEXIST;
555                         goto out;
556                 }
557         }
558
559         if (data->ops->init) {
560                 ret = data->ops->init(data->ops, data);
561                 if (ret < 0)
562                         goto out;
563         }
564
565         list_add_rcu(&data->list, &file->triggers);
566         ret++;
567
568         update_cond_flag(file);
569         if (trace_event_trigger_enable_disable(file, 1) < 0) {
570                 list_del_rcu(&data->list);
571                 update_cond_flag(file);
572                 ret--;
573         }
574 out:
575         return ret;
576 }
577
578 /**
579  * unregister_trigger - Generic event_command @unreg implementation
580  * @glob: The raw string used to register the trigger
581  * @ops: The trigger ops associated with the trigger
582  * @test: Trigger-specific data used to find the trigger to remove
583  * @file: The trace_event_file associated with the event
584  *
585  * Common implementation for event trigger unregistration.
586  *
587  * Usually used directly as the @unreg method in event command
588  * implementations.
589  */
590 void unregister_trigger(char *glob, struct event_trigger_ops *ops,
591                         struct event_trigger_data *test,
592                         struct trace_event_file *file)
593 {
594         struct event_trigger_data *data;
595         bool unregistered = false;
596
597         list_for_each_entry_rcu(data, &file->triggers, list) {
598                 if (data->cmd_ops->trigger_type == test->cmd_ops->trigger_type) {
599                         unregistered = true;
600                         list_del_rcu(&data->list);
601                         trace_event_trigger_enable_disable(file, 0);
602                         update_cond_flag(file);
603                         break;
604                 }
605         }
606
607         if (unregistered && data->ops->free)
608                 data->ops->free(data->ops, data);
609 }
610
611 /**
612  * event_trigger_callback - Generic event_command @func implementation
613  * @cmd_ops: The command ops, used for trigger registration
614  * @file: The trace_event_file associated with the event
615  * @glob: The raw string used to register the trigger
616  * @cmd: The cmd portion of the string used to register the trigger
617  * @param: The params portion of the string used to register the trigger
618  *
619  * Common implementation for event command parsing and trigger
620  * instantiation.
621  *
622  * Usually used directly as the @func method in event command
623  * implementations.
624  *
625  * Return: 0 on success, errno otherwise
626  */
627 static int
628 event_trigger_callback(struct event_command *cmd_ops,
629                        struct trace_event_file *file,
630                        char *glob, char *cmd, char *param)
631 {
632         struct event_trigger_data *trigger_data;
633         struct event_trigger_ops *trigger_ops;
634         char *trigger = NULL;
635         char *number;
636         int ret;
637
638         /* separate the trigger from the filter (t:n [if filter]) */
639         if (param && isdigit(param[0])) {
640                 trigger = strsep(&param, " \t");
641                 if (param) {
642                         param = skip_spaces(param);
643                         if (!*param)
644                                 param = NULL;
645                 }
646         }
647
648         trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
649
650         ret = -ENOMEM;
651         trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
652         if (!trigger_data)
653                 goto out;
654
655         trigger_data->count = -1;
656         trigger_data->ops = trigger_ops;
657         trigger_data->cmd_ops = cmd_ops;
658         trigger_data->private_data = file;
659         INIT_LIST_HEAD(&trigger_data->list);
660         INIT_LIST_HEAD(&trigger_data->named_list);
661
662         if (glob[0] == '!') {
663                 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
664                 kfree(trigger_data);
665                 ret = 0;
666                 goto out;
667         }
668
669         if (trigger) {
670                 number = strsep(&trigger, ":");
671
672                 ret = -EINVAL;
673                 if (!strlen(number))
674                         goto out_free;
675
676                 /*
677                  * We use the callback data field (which is a pointer)
678                  * as our counter.
679                  */
680                 ret = kstrtoul(number, 0, &trigger_data->count);
681                 if (ret)
682                         goto out_free;
683         }
684
685         if (!param) /* if param is non-empty, it's supposed to be a filter */
686                 goto out_reg;
687
688         if (!cmd_ops->set_filter)
689                 goto out_reg;
690
691         ret = cmd_ops->set_filter(param, trigger_data, file);
692         if (ret < 0)
693                 goto out_free;
694
695  out_reg:
696         /* Up the trigger_data count to make sure reg doesn't free it on failure */
697         event_trigger_init(trigger_ops, trigger_data);
698         ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
699         /*
700          * The above returns on success the # of functions enabled,
701          * but if it didn't find any functions it returns zero.
702          * Consider no functions a failure too.
703          */
704         if (!ret) {
705                 cmd_ops->unreg(glob, trigger_ops, trigger_data, file);
706                 ret = -ENOENT;
707         } else if (ret > 0)
708                 ret = 0;
709
710         /* Down the counter of trigger_data or free it if not used anymore */
711         event_trigger_free(trigger_ops, trigger_data);
712  out:
713         return ret;
714
715  out_free:
716         if (cmd_ops->set_filter)
717                 cmd_ops->set_filter(NULL, trigger_data, NULL);
718         kfree(trigger_data);
719         goto out;
720 }
721
722 /**
723  * set_trigger_filter - Generic event_command @set_filter implementation
724  * @filter_str: The filter string for the trigger, NULL to remove filter
725  * @trigger_data: Trigger-specific data
726  * @file: The trace_event_file associated with the event
727  *
728  * Common implementation for event command filter parsing and filter
729  * instantiation.
730  *
731  * Usually used directly as the @set_filter method in event command
732  * implementations.
733  *
734  * Also used to remove a filter (if filter_str = NULL).
735  *
736  * Return: 0 on success, errno otherwise
737  */
738 int set_trigger_filter(char *filter_str,
739                        struct event_trigger_data *trigger_data,
740                        struct trace_event_file *file)
741 {
742         struct event_trigger_data *data = trigger_data;
743         struct event_filter *filter = NULL, *tmp;
744         int ret = -EINVAL;
745         char *s;
746
747         if (!filter_str) /* clear the current filter */
748                 goto assign;
749
750         s = strsep(&filter_str, " \t");
751
752         if (!strlen(s) || strcmp(s, "if") != 0)
753                 goto out;
754
755         if (!filter_str)
756                 goto out;
757
758         /* The filter is for the 'trigger' event, not the triggered event */
759         ret = create_event_filter(file->event_call, filter_str, false, &filter);
760         /*
761          * If create_event_filter() fails, filter still needs to be freed.
762          * Which the calling code will do with data->filter.
763          */
764  assign:
765         tmp = rcu_access_pointer(data->filter);
766
767         rcu_assign_pointer(data->filter, filter);
768
769         if (tmp) {
770                 /* Make sure the call is done with the filter */
771                 synchronize_sched();
772                 free_event_filter(tmp);
773         }
774
775         kfree(data->filter_str);
776         data->filter_str = NULL;
777
778         if (filter_str) {
779                 data->filter_str = kstrdup(filter_str, GFP_KERNEL);
780                 if (!data->filter_str) {
781                         free_event_filter(rcu_access_pointer(data->filter));
782                         data->filter = NULL;
783                         ret = -ENOMEM;
784                 }
785         }
786  out:
787         return ret;
788 }
789
790 static LIST_HEAD(named_triggers);
791
792 /**
793  * find_named_trigger - Find the common named trigger associated with @name
794  * @name: The name of the set of named triggers to find the common data for
795  *
796  * Named triggers are sets of triggers that share a common set of
797  * trigger data.  The first named trigger registered with a given name
798  * owns the common trigger data that the others subsequently
799  * registered with the same name will reference.  This function
800  * returns the common trigger data associated with that first
801  * registered instance.
802  *
803  * Return: the common trigger data for the given named trigger on
804  * success, NULL otherwise.
805  */
806 struct event_trigger_data *find_named_trigger(const char *name)
807 {
808         struct event_trigger_data *data;
809
810         if (!name)
811                 return NULL;
812
813         list_for_each_entry(data, &named_triggers, named_list) {
814                 if (data->named_data)
815                         continue;
816                 if (strcmp(data->name, name) == 0)
817                         return data;
818         }
819
820         return NULL;
821 }
822
823 /**
824  * is_named_trigger - determine if a given trigger is a named trigger
825  * @test: The trigger data to test
826  *
827  * Return: true if 'test' is a named trigger, false otherwise.
828  */
829 bool is_named_trigger(struct event_trigger_data *test)
830 {
831         struct event_trigger_data *data;
832
833         list_for_each_entry(data, &named_triggers, named_list) {
834                 if (test == data)
835                         return true;
836         }
837
838         return false;
839 }
840
841 /**
842  * save_named_trigger - save the trigger in the named trigger list
843  * @name: The name of the named trigger set
844  * @data: The trigger data to save
845  *
846  * Return: 0 if successful, negative error otherwise.
847  */
848 int save_named_trigger(const char *name, struct event_trigger_data *data)
849 {
850         data->name = kstrdup(name, GFP_KERNEL);
851         if (!data->name)
852                 return -ENOMEM;
853
854         list_add(&data->named_list, &named_triggers);
855
856         return 0;
857 }
858
859 /**
860  * del_named_trigger - delete a trigger from the named trigger list
861  * @data: The trigger data to delete
862  */
863 void del_named_trigger(struct event_trigger_data *data)
864 {
865         kfree(data->name);
866         data->name = NULL;
867
868         list_del(&data->named_list);
869 }
870
871 static void __pause_named_trigger(struct event_trigger_data *data, bool pause)
872 {
873         struct event_trigger_data *test;
874
875         list_for_each_entry(test, &named_triggers, named_list) {
876                 if (strcmp(test->name, data->name) == 0) {
877                         if (pause) {
878                                 test->paused_tmp = test->paused;
879                                 test->paused = true;
880                         } else {
881                                 test->paused = test->paused_tmp;
882                         }
883                 }
884         }
885 }
886
887 /**
888  * pause_named_trigger - Pause all named triggers with the same name
889  * @data: The trigger data of a named trigger to pause
890  *
891  * Pauses a named trigger along with all other triggers having the
892  * same name.  Because named triggers share a common set of data,
893  * pausing only one is meaningless, so pausing one named trigger needs
894  * to pause all triggers with the same name.
895  */
896 void pause_named_trigger(struct event_trigger_data *data)
897 {
898         __pause_named_trigger(data, true);
899 }
900
901 /**
902  * unpause_named_trigger - Un-pause all named triggers with the same name
903  * @data: The trigger data of a named trigger to unpause
904  *
905  * Un-pauses a named trigger along with all other triggers having the
906  * same name.  Because named triggers share a common set of data,
907  * unpausing only one is meaningless, so unpausing one named trigger
908  * needs to unpause all triggers with the same name.
909  */
910 void unpause_named_trigger(struct event_trigger_data *data)
911 {
912         __pause_named_trigger(data, false);
913 }
914
915 /**
916  * set_named_trigger_data - Associate common named trigger data
917  * @data: The trigger data of a named trigger to unpause
918  *
919  * Named triggers are sets of triggers that share a common set of
920  * trigger data.  The first named trigger registered with a given name
921  * owns the common trigger data that the others subsequently
922  * registered with the same name will reference.  This function
923  * associates the common trigger data from the first trigger with the
924  * given trigger.
925  */
926 void set_named_trigger_data(struct event_trigger_data *data,
927                             struct event_trigger_data *named_data)
928 {
929         data->named_data = named_data;
930 }
931
932 static void
933 traceon_trigger(struct event_trigger_data *data, void *rec)
934 {
935         struct trace_event_file *file = data->private_data;
936
937         if (file) {
938                 if (tracer_tracing_is_on(file->tr))
939                         return;
940
941                 tracer_tracing_on(file->tr);
942                 return;
943         }
944
945         if (tracing_is_on())
946                 return;
947
948         tracing_on();
949 }
950
951 static void
952 traceon_count_trigger(struct event_trigger_data *data, void *rec)
953 {
954         struct trace_event_file *file = data->private_data;
955
956         if (file) {
957                 if (tracer_tracing_is_on(file->tr))
958                         return;
959         } else {
960                 if (tracing_is_on())
961                         return;
962         }
963
964         if (!data->count)
965                 return;
966
967         if (data->count != -1)
968                 (data->count)--;
969
970         if (file)
971                 tracer_tracing_on(file->tr);
972         else
973                 tracing_on();
974 }
975
976 static void
977 traceoff_trigger(struct event_trigger_data *data, void *rec)
978 {
979         struct trace_event_file *file = data->private_data;
980
981         if (file) {
982                 if (!tracer_tracing_is_on(file->tr))
983                         return;
984
985                 tracer_tracing_off(file->tr);
986                 return;
987         }
988
989         if (!tracing_is_on())
990                 return;
991
992         tracing_off();
993 }
994
995 static void
996 traceoff_count_trigger(struct event_trigger_data *data, void *rec)
997 {
998         struct trace_event_file *file = data->private_data;
999
1000         if (file) {
1001                 if (!tracer_tracing_is_on(file->tr))
1002                         return;
1003         } else {
1004                 if (!tracing_is_on())
1005                         return;
1006         }
1007
1008         if (!data->count)
1009                 return;
1010
1011         if (data->count != -1)
1012                 (data->count)--;
1013
1014         if (file)
1015                 tracer_tracing_off(file->tr);
1016         else
1017                 tracing_off();
1018 }
1019
1020 static int
1021 traceon_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1022                       struct event_trigger_data *data)
1023 {
1024         return event_trigger_print("traceon", m, (void *)data->count,
1025                                    data->filter_str);
1026 }
1027
1028 static int
1029 traceoff_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1030                        struct event_trigger_data *data)
1031 {
1032         return event_trigger_print("traceoff", m, (void *)data->count,
1033                                    data->filter_str);
1034 }
1035
1036 static struct event_trigger_ops traceon_trigger_ops = {
1037         .func                   = traceon_trigger,
1038         .print                  = traceon_trigger_print,
1039         .init                   = event_trigger_init,
1040         .free                   = event_trigger_free,
1041 };
1042
1043 static struct event_trigger_ops traceon_count_trigger_ops = {
1044         .func                   = traceon_count_trigger,
1045         .print                  = traceon_trigger_print,
1046         .init                   = event_trigger_init,
1047         .free                   = event_trigger_free,
1048 };
1049
1050 static struct event_trigger_ops traceoff_trigger_ops = {
1051         .func                   = traceoff_trigger,
1052         .print                  = traceoff_trigger_print,
1053         .init                   = event_trigger_init,
1054         .free                   = event_trigger_free,
1055 };
1056
1057 static struct event_trigger_ops traceoff_count_trigger_ops = {
1058         .func                   = traceoff_count_trigger,
1059         .print                  = traceoff_trigger_print,
1060         .init                   = event_trigger_init,
1061         .free                   = event_trigger_free,
1062 };
1063
1064 static struct event_trigger_ops *
1065 onoff_get_trigger_ops(char *cmd, char *param)
1066 {
1067         struct event_trigger_ops *ops;
1068
1069         /* we register both traceon and traceoff to this callback */
1070         if (strcmp(cmd, "traceon") == 0)
1071                 ops = param ? &traceon_count_trigger_ops :
1072                         &traceon_trigger_ops;
1073         else
1074                 ops = param ? &traceoff_count_trigger_ops :
1075                         &traceoff_trigger_ops;
1076
1077         return ops;
1078 }
1079
1080 static struct event_command trigger_traceon_cmd = {
1081         .name                   = "traceon",
1082         .trigger_type           = ETT_TRACE_ONOFF,
1083         .func                   = event_trigger_callback,
1084         .reg                    = register_trigger,
1085         .unreg                  = unregister_trigger,
1086         .get_trigger_ops        = onoff_get_trigger_ops,
1087         .set_filter             = set_trigger_filter,
1088 };
1089
1090 static struct event_command trigger_traceoff_cmd = {
1091         .name                   = "traceoff",
1092         .trigger_type           = ETT_TRACE_ONOFF,
1093         .flags                  = EVENT_CMD_FL_POST_TRIGGER,
1094         .func                   = event_trigger_callback,
1095         .reg                    = register_trigger,
1096         .unreg                  = unregister_trigger,
1097         .get_trigger_ops        = onoff_get_trigger_ops,
1098         .set_filter             = set_trigger_filter,
1099 };
1100
1101 #ifdef CONFIG_TRACER_SNAPSHOT
1102 static void
1103 snapshot_trigger(struct event_trigger_data *data, void *rec)
1104 {
1105         struct trace_event_file *file = data->private_data;
1106
1107         if (file)
1108                 tracing_snapshot_instance(file->tr);
1109         else
1110                 tracing_snapshot();
1111 }
1112
1113 static void
1114 snapshot_count_trigger(struct event_trigger_data *data, void *rec)
1115 {
1116         if (!data->count)
1117                 return;
1118
1119         if (data->count != -1)
1120                 (data->count)--;
1121
1122         snapshot_trigger(data, rec);
1123 }
1124
1125 static int
1126 register_snapshot_trigger(char *glob, struct event_trigger_ops *ops,
1127                           struct event_trigger_data *data,
1128                           struct trace_event_file *file)
1129 {
1130         if (tracing_alloc_snapshot_instance(file->tr) != 0)
1131                 return 0;
1132
1133         return register_trigger(glob, ops, data, file);
1134 }
1135
1136 static int
1137 snapshot_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1138                        struct event_trigger_data *data)
1139 {
1140         return event_trigger_print("snapshot", m, (void *)data->count,
1141                                    data->filter_str);
1142 }
1143
1144 static struct event_trigger_ops snapshot_trigger_ops = {
1145         .func                   = snapshot_trigger,
1146         .print                  = snapshot_trigger_print,
1147         .init                   = event_trigger_init,
1148         .free                   = event_trigger_free,
1149 };
1150
1151 static struct event_trigger_ops snapshot_count_trigger_ops = {
1152         .func                   = snapshot_count_trigger,
1153         .print                  = snapshot_trigger_print,
1154         .init                   = event_trigger_init,
1155         .free                   = event_trigger_free,
1156 };
1157
1158 static struct event_trigger_ops *
1159 snapshot_get_trigger_ops(char *cmd, char *param)
1160 {
1161         return param ? &snapshot_count_trigger_ops : &snapshot_trigger_ops;
1162 }
1163
1164 static struct event_command trigger_snapshot_cmd = {
1165         .name                   = "snapshot",
1166         .trigger_type           = ETT_SNAPSHOT,
1167         .func                   = event_trigger_callback,
1168         .reg                    = register_snapshot_trigger,
1169         .unreg                  = unregister_trigger,
1170         .get_trigger_ops        = snapshot_get_trigger_ops,
1171         .set_filter             = set_trigger_filter,
1172 };
1173
1174 static __init int register_trigger_snapshot_cmd(void)
1175 {
1176         int ret;
1177
1178         ret = register_event_command(&trigger_snapshot_cmd);
1179         WARN_ON(ret < 0);
1180
1181         return ret;
1182 }
1183 #else
1184 static __init int register_trigger_snapshot_cmd(void) { return 0; }
1185 #endif /* CONFIG_TRACER_SNAPSHOT */
1186
1187 #ifdef CONFIG_STACKTRACE
1188 /*
1189  * Skip 3:
1190  *   stacktrace_trigger()
1191  *   event_triggers_post_call()
1192  *   trace_event_raw_event_xxx()
1193  */
1194 #define STACK_SKIP 3
1195
1196 static void
1197 stacktrace_trigger(struct event_trigger_data *data, void *rec)
1198 {
1199         struct trace_event_file *file = data->private_data;
1200         unsigned long flags;
1201
1202         if (file) {
1203                 local_save_flags(flags);
1204                 __trace_stack(file->tr, flags, STACK_SKIP, preempt_count());
1205         } else
1206                 trace_dump_stack(STACK_SKIP);
1207 }
1208
1209 static void
1210 stacktrace_count_trigger(struct event_trigger_data *data, void *rec)
1211 {
1212         if (!data->count)
1213                 return;
1214
1215         if (data->count != -1)
1216                 (data->count)--;
1217
1218         stacktrace_trigger(data, rec);
1219 }
1220
1221 static int
1222 stacktrace_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1223                          struct event_trigger_data *data)
1224 {
1225         return event_trigger_print("stacktrace", m, (void *)data->count,
1226                                    data->filter_str);
1227 }
1228
1229 static struct event_trigger_ops stacktrace_trigger_ops = {
1230         .func                   = stacktrace_trigger,
1231         .print                  = stacktrace_trigger_print,
1232         .init                   = event_trigger_init,
1233         .free                   = event_trigger_free,
1234 };
1235
1236 static struct event_trigger_ops stacktrace_count_trigger_ops = {
1237         .func                   = stacktrace_count_trigger,
1238         .print                  = stacktrace_trigger_print,
1239         .init                   = event_trigger_init,
1240         .free                   = event_trigger_free,
1241 };
1242
1243 static struct event_trigger_ops *
1244 stacktrace_get_trigger_ops(char *cmd, char *param)
1245 {
1246         return param ? &stacktrace_count_trigger_ops : &stacktrace_trigger_ops;
1247 }
1248
1249 static struct event_command trigger_stacktrace_cmd = {
1250         .name                   = "stacktrace",
1251         .trigger_type           = ETT_STACKTRACE,
1252         .flags                  = EVENT_CMD_FL_POST_TRIGGER,
1253         .func                   = event_trigger_callback,
1254         .reg                    = register_trigger,
1255         .unreg                  = unregister_trigger,
1256         .get_trigger_ops        = stacktrace_get_trigger_ops,
1257         .set_filter             = set_trigger_filter,
1258 };
1259
1260 static __init int register_trigger_stacktrace_cmd(void)
1261 {
1262         int ret;
1263
1264         ret = register_event_command(&trigger_stacktrace_cmd);
1265         WARN_ON(ret < 0);
1266
1267         return ret;
1268 }
1269 #else
1270 static __init int register_trigger_stacktrace_cmd(void) { return 0; }
1271 #endif /* CONFIG_STACKTRACE */
1272
1273 static __init void unregister_trigger_traceon_traceoff_cmds(void)
1274 {
1275         unregister_event_command(&trigger_traceon_cmd);
1276         unregister_event_command(&trigger_traceoff_cmd);
1277 }
1278
1279 static void
1280 event_enable_trigger(struct event_trigger_data *data, void *rec)
1281 {
1282         struct enable_trigger_data *enable_data = data->private_data;
1283
1284         if (enable_data->enable)
1285                 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
1286         else
1287                 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
1288 }
1289
1290 static void
1291 event_enable_count_trigger(struct event_trigger_data *data, void *rec)
1292 {
1293         struct enable_trigger_data *enable_data = data->private_data;
1294
1295         if (!data->count)
1296                 return;
1297
1298         /* Skip if the event is in a state we want to switch to */
1299         if (enable_data->enable == !(enable_data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
1300                 return;
1301
1302         if (data->count != -1)
1303                 (data->count)--;
1304
1305         event_enable_trigger(data, rec);
1306 }
1307
1308 int event_enable_trigger_print(struct seq_file *m,
1309                                struct event_trigger_ops *ops,
1310                                struct event_trigger_data *data)
1311 {
1312         struct enable_trigger_data *enable_data = data->private_data;
1313
1314         seq_printf(m, "%s:%s:%s",
1315                    enable_data->hist ?
1316                    (enable_data->enable ? ENABLE_HIST_STR : DISABLE_HIST_STR) :
1317                    (enable_data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR),
1318                    enable_data->file->event_call->class->system,
1319                    trace_event_name(enable_data->file->event_call));
1320
1321         if (data->count == -1)
1322                 seq_puts(m, ":unlimited");
1323         else
1324                 seq_printf(m, ":count=%ld", data->count);
1325
1326         if (data->filter_str)
1327                 seq_printf(m, " if %s\n", data->filter_str);
1328         else
1329                 seq_putc(m, '\n');
1330
1331         return 0;
1332 }
1333
1334 void event_enable_trigger_free(struct event_trigger_ops *ops,
1335                                struct event_trigger_data *data)
1336 {
1337         struct enable_trigger_data *enable_data = data->private_data;
1338
1339         if (WARN_ON_ONCE(data->ref <= 0))
1340                 return;
1341
1342         data->ref--;
1343         if (!data->ref) {
1344                 /* Remove the SOFT_MODE flag */
1345                 trace_event_enable_disable(enable_data->file, 0, 1);
1346                 module_put(enable_data->file->event_call->mod);
1347                 trigger_data_free(data);
1348                 kfree(enable_data);
1349         }
1350 }
1351
1352 static struct event_trigger_ops event_enable_trigger_ops = {
1353         .func                   = event_enable_trigger,
1354         .print                  = event_enable_trigger_print,
1355         .init                   = event_trigger_init,
1356         .free                   = event_enable_trigger_free,
1357 };
1358
1359 static struct event_trigger_ops event_enable_count_trigger_ops = {
1360         .func                   = event_enable_count_trigger,
1361         .print                  = event_enable_trigger_print,
1362         .init                   = event_trigger_init,
1363         .free                   = event_enable_trigger_free,
1364 };
1365
1366 static struct event_trigger_ops event_disable_trigger_ops = {
1367         .func                   = event_enable_trigger,
1368         .print                  = event_enable_trigger_print,
1369         .init                   = event_trigger_init,
1370         .free                   = event_enable_trigger_free,
1371 };
1372
1373 static struct event_trigger_ops event_disable_count_trigger_ops = {
1374         .func                   = event_enable_count_trigger,
1375         .print                  = event_enable_trigger_print,
1376         .init                   = event_trigger_init,
1377         .free                   = event_enable_trigger_free,
1378 };
1379
1380 int event_enable_trigger_func(struct event_command *cmd_ops,
1381                               struct trace_event_file *file,
1382                               char *glob, char *cmd, char *param)
1383 {
1384         struct trace_event_file *event_enable_file;
1385         struct enable_trigger_data *enable_data;
1386         struct event_trigger_data *trigger_data;
1387         struct event_trigger_ops *trigger_ops;
1388         struct trace_array *tr = file->tr;
1389         const char *system;
1390         const char *event;
1391         bool hist = false;
1392         char *trigger;
1393         char *number;
1394         bool enable;
1395         int ret;
1396
1397         if (!param)
1398                 return -EINVAL;
1399
1400         /* separate the trigger from the filter (s:e:n [if filter]) */
1401         trigger = strsep(&param, " \t");
1402         if (!trigger)
1403                 return -EINVAL;
1404         if (param) {
1405                 param = skip_spaces(param);
1406                 if (!*param)
1407                         param = NULL;
1408         }
1409
1410         system = strsep(&trigger, ":");
1411         if (!trigger)
1412                 return -EINVAL;
1413
1414         event = strsep(&trigger, ":");
1415
1416         ret = -EINVAL;
1417         event_enable_file = find_event_file(tr, system, event);
1418         if (!event_enable_file)
1419                 goto out;
1420
1421 #ifdef CONFIG_HIST_TRIGGERS
1422         hist = ((strcmp(cmd, ENABLE_HIST_STR) == 0) ||
1423                 (strcmp(cmd, DISABLE_HIST_STR) == 0));
1424
1425         enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
1426                   (strcmp(cmd, ENABLE_HIST_STR) == 0));
1427 #else
1428         enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1429 #endif
1430         trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
1431
1432         ret = -ENOMEM;
1433         trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
1434         if (!trigger_data)
1435                 goto out;
1436
1437         enable_data = kzalloc(sizeof(*enable_data), GFP_KERNEL);
1438         if (!enable_data) {
1439                 kfree(trigger_data);
1440                 goto out;
1441         }
1442
1443         trigger_data->count = -1;
1444         trigger_data->ops = trigger_ops;
1445         trigger_data->cmd_ops = cmd_ops;
1446         INIT_LIST_HEAD(&trigger_data->list);
1447         RCU_INIT_POINTER(trigger_data->filter, NULL);
1448
1449         enable_data->hist = hist;
1450         enable_data->enable = enable;
1451         enable_data->file = event_enable_file;
1452         trigger_data->private_data = enable_data;
1453
1454         if (glob[0] == '!') {
1455                 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
1456                 kfree(trigger_data);
1457                 kfree(enable_data);
1458                 ret = 0;
1459                 goto out;
1460         }
1461
1462         /* Up the trigger_data count to make sure nothing frees it on failure */
1463         event_trigger_init(trigger_ops, trigger_data);
1464
1465         if (trigger) {
1466                 number = strsep(&trigger, ":");
1467
1468                 ret = -EINVAL;
1469                 if (!strlen(number))
1470                         goto out_free;
1471
1472                 /*
1473                  * We use the callback data field (which is a pointer)
1474                  * as our counter.
1475                  */
1476                 ret = kstrtoul(number, 0, &trigger_data->count);
1477                 if (ret)
1478                         goto out_free;
1479         }
1480
1481         if (!param) /* if param is non-empty, it's supposed to be a filter */
1482                 goto out_reg;
1483
1484         if (!cmd_ops->set_filter)
1485                 goto out_reg;
1486
1487         ret = cmd_ops->set_filter(param, trigger_data, file);
1488         if (ret < 0)
1489                 goto out_free;
1490
1491  out_reg:
1492         /* Don't let event modules unload while probe registered */
1493         ret = try_module_get(event_enable_file->event_call->mod);
1494         if (!ret) {
1495                 ret = -EBUSY;
1496                 goto out_free;
1497         }
1498
1499         ret = trace_event_enable_disable(event_enable_file, 1, 1);
1500         if (ret < 0)
1501                 goto out_put;
1502         ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
1503         /*
1504          * The above returns on success the # of functions enabled,
1505          * but if it didn't find any functions it returns zero.
1506          * Consider no functions a failure too.
1507          */
1508         if (!ret) {
1509                 ret = -ENOENT;
1510                 goto out_disable;
1511         } else if (ret < 0)
1512                 goto out_disable;
1513         /* Just return zero, not the number of enabled functions */
1514         ret = 0;
1515         event_trigger_free(trigger_ops, trigger_data);
1516  out:
1517         return ret;
1518
1519  out_disable:
1520         trace_event_enable_disable(event_enable_file, 0, 1);
1521  out_put:
1522         module_put(event_enable_file->event_call->mod);
1523  out_free:
1524         if (cmd_ops->set_filter)
1525                 cmd_ops->set_filter(NULL, trigger_data, NULL);
1526         event_trigger_free(trigger_ops, trigger_data);
1527         kfree(enable_data);
1528         goto out;
1529 }
1530
1531 int event_enable_register_trigger(char *glob,
1532                                   struct event_trigger_ops *ops,
1533                                   struct event_trigger_data *data,
1534                                   struct trace_event_file *file)
1535 {
1536         struct enable_trigger_data *enable_data = data->private_data;
1537         struct enable_trigger_data *test_enable_data;
1538         struct event_trigger_data *test;
1539         int ret = 0;
1540
1541         list_for_each_entry_rcu(test, &file->triggers, list) {
1542                 test_enable_data = test->private_data;
1543                 if (test_enable_data &&
1544                     (test->cmd_ops->trigger_type ==
1545                      data->cmd_ops->trigger_type) &&
1546                     (test_enable_data->file == enable_data->file)) {
1547                         ret = -EEXIST;
1548                         goto out;
1549                 }
1550         }
1551
1552         if (data->ops->init) {
1553                 ret = data->ops->init(data->ops, data);
1554                 if (ret < 0)
1555                         goto out;
1556         }
1557
1558         list_add_rcu(&data->list, &file->triggers);
1559         ret++;
1560
1561         update_cond_flag(file);
1562         if (trace_event_trigger_enable_disable(file, 1) < 0) {
1563                 list_del_rcu(&data->list);
1564                 update_cond_flag(file);
1565                 ret--;
1566         }
1567 out:
1568         return ret;
1569 }
1570
1571 void event_enable_unregister_trigger(char *glob,
1572                                      struct event_trigger_ops *ops,
1573                                      struct event_trigger_data *test,
1574                                      struct trace_event_file *file)
1575 {
1576         struct enable_trigger_data *test_enable_data = test->private_data;
1577         struct enable_trigger_data *enable_data;
1578         struct event_trigger_data *data;
1579         bool unregistered = false;
1580
1581         list_for_each_entry_rcu(data, &file->triggers, list) {
1582                 enable_data = data->private_data;
1583                 if (enable_data &&
1584                     (data->cmd_ops->trigger_type ==
1585                      test->cmd_ops->trigger_type) &&
1586                     (enable_data->file == test_enable_data->file)) {
1587                         unregistered = true;
1588                         list_del_rcu(&data->list);
1589                         trace_event_trigger_enable_disable(file, 0);
1590                         update_cond_flag(file);
1591                         break;
1592                 }
1593         }
1594
1595         if (unregistered && data->ops->free)
1596                 data->ops->free(data->ops, data);
1597 }
1598
1599 static struct event_trigger_ops *
1600 event_enable_get_trigger_ops(char *cmd, char *param)
1601 {
1602         struct event_trigger_ops *ops;
1603         bool enable;
1604
1605 #ifdef CONFIG_HIST_TRIGGERS
1606         enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
1607                   (strcmp(cmd, ENABLE_HIST_STR) == 0));
1608 #else
1609         enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1610 #endif
1611         if (enable)
1612                 ops = param ? &event_enable_count_trigger_ops :
1613                         &event_enable_trigger_ops;
1614         else
1615                 ops = param ? &event_disable_count_trigger_ops :
1616                         &event_disable_trigger_ops;
1617
1618         return ops;
1619 }
1620
1621 static struct event_command trigger_enable_cmd = {
1622         .name                   = ENABLE_EVENT_STR,
1623         .trigger_type           = ETT_EVENT_ENABLE,
1624         .func                   = event_enable_trigger_func,
1625         .reg                    = event_enable_register_trigger,
1626         .unreg                  = event_enable_unregister_trigger,
1627         .get_trigger_ops        = event_enable_get_trigger_ops,
1628         .set_filter             = set_trigger_filter,
1629 };
1630
1631 static struct event_command trigger_disable_cmd = {
1632         .name                   = DISABLE_EVENT_STR,
1633         .trigger_type           = ETT_EVENT_ENABLE,
1634         .func                   = event_enable_trigger_func,
1635         .reg                    = event_enable_register_trigger,
1636         .unreg                  = event_enable_unregister_trigger,
1637         .get_trigger_ops        = event_enable_get_trigger_ops,
1638         .set_filter             = set_trigger_filter,
1639 };
1640
1641 static __init void unregister_trigger_enable_disable_cmds(void)
1642 {
1643         unregister_event_command(&trigger_enable_cmd);
1644         unregister_event_command(&trigger_disable_cmd);
1645 }
1646
1647 static __init int register_trigger_enable_disable_cmds(void)
1648 {
1649         int ret;
1650
1651         ret = register_event_command(&trigger_enable_cmd);
1652         if (WARN_ON(ret < 0))
1653                 return ret;
1654         ret = register_event_command(&trigger_disable_cmd);
1655         if (WARN_ON(ret < 0))
1656                 unregister_trigger_enable_disable_cmds();
1657
1658         return ret;
1659 }
1660
1661 static __init int register_trigger_traceon_traceoff_cmds(void)
1662 {
1663         int ret;
1664
1665         ret = register_event_command(&trigger_traceon_cmd);
1666         if (WARN_ON(ret < 0))
1667                 return ret;
1668         ret = register_event_command(&trigger_traceoff_cmd);
1669         if (WARN_ON(ret < 0))
1670                 unregister_trigger_traceon_traceoff_cmds();
1671
1672         return ret;
1673 }
1674
1675 __init int register_trigger_cmds(void)
1676 {
1677         register_trigger_traceon_traceoff_cmds();
1678         register_trigger_snapshot_cmd();
1679         register_trigger_stacktrace_cmd();
1680         register_trigger_enable_disable_cmds();
1681         register_trigger_hist_enable_disable_cmds();
1682         register_trigger_hist_cmd();
1683
1684         return 0;
1685 }