GNU Linux-libre 4.14.251-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         if (tracing_is_on())
936                 return;
937
938         tracing_on();
939 }
940
941 static void
942 traceon_count_trigger(struct event_trigger_data *data, void *rec)
943 {
944         if (tracing_is_on())
945                 return;
946
947         if (!data->count)
948                 return;
949
950         if (data->count != -1)
951                 (data->count)--;
952
953         tracing_on();
954 }
955
956 static void
957 traceoff_trigger(struct event_trigger_data *data, void *rec)
958 {
959         if (!tracing_is_on())
960                 return;
961
962         tracing_off();
963 }
964
965 static void
966 traceoff_count_trigger(struct event_trigger_data *data, void *rec)
967 {
968         if (!tracing_is_on())
969                 return;
970
971         if (!data->count)
972                 return;
973
974         if (data->count != -1)
975                 (data->count)--;
976
977         tracing_off();
978 }
979
980 static int
981 traceon_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
982                       struct event_trigger_data *data)
983 {
984         return event_trigger_print("traceon", m, (void *)data->count,
985                                    data->filter_str);
986 }
987
988 static int
989 traceoff_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
990                        struct event_trigger_data *data)
991 {
992         return event_trigger_print("traceoff", m, (void *)data->count,
993                                    data->filter_str);
994 }
995
996 static struct event_trigger_ops traceon_trigger_ops = {
997         .func                   = traceon_trigger,
998         .print                  = traceon_trigger_print,
999         .init                   = event_trigger_init,
1000         .free                   = event_trigger_free,
1001 };
1002
1003 static struct event_trigger_ops traceon_count_trigger_ops = {
1004         .func                   = traceon_count_trigger,
1005         .print                  = traceon_trigger_print,
1006         .init                   = event_trigger_init,
1007         .free                   = event_trigger_free,
1008 };
1009
1010 static struct event_trigger_ops traceoff_trigger_ops = {
1011         .func                   = traceoff_trigger,
1012         .print                  = traceoff_trigger_print,
1013         .init                   = event_trigger_init,
1014         .free                   = event_trigger_free,
1015 };
1016
1017 static struct event_trigger_ops traceoff_count_trigger_ops = {
1018         .func                   = traceoff_count_trigger,
1019         .print                  = traceoff_trigger_print,
1020         .init                   = event_trigger_init,
1021         .free                   = event_trigger_free,
1022 };
1023
1024 static struct event_trigger_ops *
1025 onoff_get_trigger_ops(char *cmd, char *param)
1026 {
1027         struct event_trigger_ops *ops;
1028
1029         /* we register both traceon and traceoff to this callback */
1030         if (strcmp(cmd, "traceon") == 0)
1031                 ops = param ? &traceon_count_trigger_ops :
1032                         &traceon_trigger_ops;
1033         else
1034                 ops = param ? &traceoff_count_trigger_ops :
1035                         &traceoff_trigger_ops;
1036
1037         return ops;
1038 }
1039
1040 static struct event_command trigger_traceon_cmd = {
1041         .name                   = "traceon",
1042         .trigger_type           = ETT_TRACE_ONOFF,
1043         .func                   = event_trigger_callback,
1044         .reg                    = register_trigger,
1045         .unreg                  = unregister_trigger,
1046         .get_trigger_ops        = onoff_get_trigger_ops,
1047         .set_filter             = set_trigger_filter,
1048 };
1049
1050 static struct event_command trigger_traceoff_cmd = {
1051         .name                   = "traceoff",
1052         .trigger_type           = ETT_TRACE_ONOFF,
1053         .flags                  = EVENT_CMD_FL_POST_TRIGGER,
1054         .func                   = event_trigger_callback,
1055         .reg                    = register_trigger,
1056         .unreg                  = unregister_trigger,
1057         .get_trigger_ops        = onoff_get_trigger_ops,
1058         .set_filter             = set_trigger_filter,
1059 };
1060
1061 #ifdef CONFIG_TRACER_SNAPSHOT
1062 static void
1063 snapshot_trigger(struct event_trigger_data *data, void *rec)
1064 {
1065         struct trace_event_file *file = data->private_data;
1066
1067         if (file)
1068                 tracing_snapshot_instance(file->tr);
1069         else
1070                 tracing_snapshot();
1071 }
1072
1073 static void
1074 snapshot_count_trigger(struct event_trigger_data *data, void *rec)
1075 {
1076         if (!data->count)
1077                 return;
1078
1079         if (data->count != -1)
1080                 (data->count)--;
1081
1082         snapshot_trigger(data, rec);
1083 }
1084
1085 static int
1086 register_snapshot_trigger(char *glob, struct event_trigger_ops *ops,
1087                           struct event_trigger_data *data,
1088                           struct trace_event_file *file)
1089 {
1090         if (tracing_alloc_snapshot_instance(file->tr) != 0)
1091                 return 0;
1092
1093         return register_trigger(glob, ops, data, file);
1094 }
1095
1096 static int
1097 snapshot_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1098                        struct event_trigger_data *data)
1099 {
1100         return event_trigger_print("snapshot", m, (void *)data->count,
1101                                    data->filter_str);
1102 }
1103
1104 static struct event_trigger_ops snapshot_trigger_ops = {
1105         .func                   = snapshot_trigger,
1106         .print                  = snapshot_trigger_print,
1107         .init                   = event_trigger_init,
1108         .free                   = event_trigger_free,
1109 };
1110
1111 static struct event_trigger_ops snapshot_count_trigger_ops = {
1112         .func                   = snapshot_count_trigger,
1113         .print                  = snapshot_trigger_print,
1114         .init                   = event_trigger_init,
1115         .free                   = event_trigger_free,
1116 };
1117
1118 static struct event_trigger_ops *
1119 snapshot_get_trigger_ops(char *cmd, char *param)
1120 {
1121         return param ? &snapshot_count_trigger_ops : &snapshot_trigger_ops;
1122 }
1123
1124 static struct event_command trigger_snapshot_cmd = {
1125         .name                   = "snapshot",
1126         .trigger_type           = ETT_SNAPSHOT,
1127         .func                   = event_trigger_callback,
1128         .reg                    = register_snapshot_trigger,
1129         .unreg                  = unregister_trigger,
1130         .get_trigger_ops        = snapshot_get_trigger_ops,
1131         .set_filter             = set_trigger_filter,
1132 };
1133
1134 static __init int register_trigger_snapshot_cmd(void)
1135 {
1136         int ret;
1137
1138         ret = register_event_command(&trigger_snapshot_cmd);
1139         WARN_ON(ret < 0);
1140
1141         return ret;
1142 }
1143 #else
1144 static __init int register_trigger_snapshot_cmd(void) { return 0; }
1145 #endif /* CONFIG_TRACER_SNAPSHOT */
1146
1147 #ifdef CONFIG_STACKTRACE
1148 /*
1149  * Skip 3:
1150  *   stacktrace_trigger()
1151  *   event_triggers_post_call()
1152  *   trace_event_raw_event_xxx()
1153  */
1154 #define STACK_SKIP 3
1155
1156 static void
1157 stacktrace_trigger(struct event_trigger_data *data, void *rec)
1158 {
1159         trace_dump_stack(STACK_SKIP);
1160 }
1161
1162 static void
1163 stacktrace_count_trigger(struct event_trigger_data *data, void *rec)
1164 {
1165         if (!data->count)
1166                 return;
1167
1168         if (data->count != -1)
1169                 (data->count)--;
1170
1171         stacktrace_trigger(data, rec);
1172 }
1173
1174 static int
1175 stacktrace_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1176                          struct event_trigger_data *data)
1177 {
1178         return event_trigger_print("stacktrace", m, (void *)data->count,
1179                                    data->filter_str);
1180 }
1181
1182 static struct event_trigger_ops stacktrace_trigger_ops = {
1183         .func                   = stacktrace_trigger,
1184         .print                  = stacktrace_trigger_print,
1185         .init                   = event_trigger_init,
1186         .free                   = event_trigger_free,
1187 };
1188
1189 static struct event_trigger_ops stacktrace_count_trigger_ops = {
1190         .func                   = stacktrace_count_trigger,
1191         .print                  = stacktrace_trigger_print,
1192         .init                   = event_trigger_init,
1193         .free                   = event_trigger_free,
1194 };
1195
1196 static struct event_trigger_ops *
1197 stacktrace_get_trigger_ops(char *cmd, char *param)
1198 {
1199         return param ? &stacktrace_count_trigger_ops : &stacktrace_trigger_ops;
1200 }
1201
1202 static struct event_command trigger_stacktrace_cmd = {
1203         .name                   = "stacktrace",
1204         .trigger_type           = ETT_STACKTRACE,
1205         .flags                  = EVENT_CMD_FL_POST_TRIGGER,
1206         .func                   = event_trigger_callback,
1207         .reg                    = register_trigger,
1208         .unreg                  = unregister_trigger,
1209         .get_trigger_ops        = stacktrace_get_trigger_ops,
1210         .set_filter             = set_trigger_filter,
1211 };
1212
1213 static __init int register_trigger_stacktrace_cmd(void)
1214 {
1215         int ret;
1216
1217         ret = register_event_command(&trigger_stacktrace_cmd);
1218         WARN_ON(ret < 0);
1219
1220         return ret;
1221 }
1222 #else
1223 static __init int register_trigger_stacktrace_cmd(void) { return 0; }
1224 #endif /* CONFIG_STACKTRACE */
1225
1226 static __init void unregister_trigger_traceon_traceoff_cmds(void)
1227 {
1228         unregister_event_command(&trigger_traceon_cmd);
1229         unregister_event_command(&trigger_traceoff_cmd);
1230 }
1231
1232 static void
1233 event_enable_trigger(struct event_trigger_data *data, void *rec)
1234 {
1235         struct enable_trigger_data *enable_data = data->private_data;
1236
1237         if (enable_data->enable)
1238                 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
1239         else
1240                 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
1241 }
1242
1243 static void
1244 event_enable_count_trigger(struct event_trigger_data *data, void *rec)
1245 {
1246         struct enable_trigger_data *enable_data = data->private_data;
1247
1248         if (!data->count)
1249                 return;
1250
1251         /* Skip if the event is in a state we want to switch to */
1252         if (enable_data->enable == !(enable_data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
1253                 return;
1254
1255         if (data->count != -1)
1256                 (data->count)--;
1257
1258         event_enable_trigger(data, rec);
1259 }
1260
1261 int event_enable_trigger_print(struct seq_file *m,
1262                                struct event_trigger_ops *ops,
1263                                struct event_trigger_data *data)
1264 {
1265         struct enable_trigger_data *enable_data = data->private_data;
1266
1267         seq_printf(m, "%s:%s:%s",
1268                    enable_data->hist ?
1269                    (enable_data->enable ? ENABLE_HIST_STR : DISABLE_HIST_STR) :
1270                    (enable_data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR),
1271                    enable_data->file->event_call->class->system,
1272                    trace_event_name(enable_data->file->event_call));
1273
1274         if (data->count == -1)
1275                 seq_puts(m, ":unlimited");
1276         else
1277                 seq_printf(m, ":count=%ld", data->count);
1278
1279         if (data->filter_str)
1280                 seq_printf(m, " if %s\n", data->filter_str);
1281         else
1282                 seq_putc(m, '\n');
1283
1284         return 0;
1285 }
1286
1287 void event_enable_trigger_free(struct event_trigger_ops *ops,
1288                                struct event_trigger_data *data)
1289 {
1290         struct enable_trigger_data *enable_data = data->private_data;
1291
1292         if (WARN_ON_ONCE(data->ref <= 0))
1293                 return;
1294
1295         data->ref--;
1296         if (!data->ref) {
1297                 /* Remove the SOFT_MODE flag */
1298                 trace_event_enable_disable(enable_data->file, 0, 1);
1299                 module_put(enable_data->file->event_call->mod);
1300                 trigger_data_free(data);
1301                 kfree(enable_data);
1302         }
1303 }
1304
1305 static struct event_trigger_ops event_enable_trigger_ops = {
1306         .func                   = event_enable_trigger,
1307         .print                  = event_enable_trigger_print,
1308         .init                   = event_trigger_init,
1309         .free                   = event_enable_trigger_free,
1310 };
1311
1312 static struct event_trigger_ops event_enable_count_trigger_ops = {
1313         .func                   = event_enable_count_trigger,
1314         .print                  = event_enable_trigger_print,
1315         .init                   = event_trigger_init,
1316         .free                   = event_enable_trigger_free,
1317 };
1318
1319 static struct event_trigger_ops event_disable_trigger_ops = {
1320         .func                   = event_enable_trigger,
1321         .print                  = event_enable_trigger_print,
1322         .init                   = event_trigger_init,
1323         .free                   = event_enable_trigger_free,
1324 };
1325
1326 static struct event_trigger_ops event_disable_count_trigger_ops = {
1327         .func                   = event_enable_count_trigger,
1328         .print                  = event_enable_trigger_print,
1329         .init                   = event_trigger_init,
1330         .free                   = event_enable_trigger_free,
1331 };
1332
1333 int event_enable_trigger_func(struct event_command *cmd_ops,
1334                               struct trace_event_file *file,
1335                               char *glob, char *cmd, char *param)
1336 {
1337         struct trace_event_file *event_enable_file;
1338         struct enable_trigger_data *enable_data;
1339         struct event_trigger_data *trigger_data;
1340         struct event_trigger_ops *trigger_ops;
1341         struct trace_array *tr = file->tr;
1342         const char *system;
1343         const char *event;
1344         bool hist = false;
1345         char *trigger;
1346         char *number;
1347         bool enable;
1348         int ret;
1349
1350         if (!param)
1351                 return -EINVAL;
1352
1353         /* separate the trigger from the filter (s:e:n [if filter]) */
1354         trigger = strsep(&param, " \t");
1355         if (!trigger)
1356                 return -EINVAL;
1357         if (param) {
1358                 param = skip_spaces(param);
1359                 if (!*param)
1360                         param = NULL;
1361         }
1362
1363         system = strsep(&trigger, ":");
1364         if (!trigger)
1365                 return -EINVAL;
1366
1367         event = strsep(&trigger, ":");
1368
1369         ret = -EINVAL;
1370         event_enable_file = find_event_file(tr, system, event);
1371         if (!event_enable_file)
1372                 goto out;
1373
1374 #ifdef CONFIG_HIST_TRIGGERS
1375         hist = ((strcmp(cmd, ENABLE_HIST_STR) == 0) ||
1376                 (strcmp(cmd, DISABLE_HIST_STR) == 0));
1377
1378         enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
1379                   (strcmp(cmd, ENABLE_HIST_STR) == 0));
1380 #else
1381         enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1382 #endif
1383         trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
1384
1385         ret = -ENOMEM;
1386         trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
1387         if (!trigger_data)
1388                 goto out;
1389
1390         enable_data = kzalloc(sizeof(*enable_data), GFP_KERNEL);
1391         if (!enable_data) {
1392                 kfree(trigger_data);
1393                 goto out;
1394         }
1395
1396         trigger_data->count = -1;
1397         trigger_data->ops = trigger_ops;
1398         trigger_data->cmd_ops = cmd_ops;
1399         INIT_LIST_HEAD(&trigger_data->list);
1400         RCU_INIT_POINTER(trigger_data->filter, NULL);
1401
1402         enable_data->hist = hist;
1403         enable_data->enable = enable;
1404         enable_data->file = event_enable_file;
1405         trigger_data->private_data = enable_data;
1406
1407         if (glob[0] == '!') {
1408                 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
1409                 kfree(trigger_data);
1410                 kfree(enable_data);
1411                 ret = 0;
1412                 goto out;
1413         }
1414
1415         /* Up the trigger_data count to make sure nothing frees it on failure */
1416         event_trigger_init(trigger_ops, trigger_data);
1417
1418         if (trigger) {
1419                 number = strsep(&trigger, ":");
1420
1421                 ret = -EINVAL;
1422                 if (!strlen(number))
1423                         goto out_free;
1424
1425                 /*
1426                  * We use the callback data field (which is a pointer)
1427                  * as our counter.
1428                  */
1429                 ret = kstrtoul(number, 0, &trigger_data->count);
1430                 if (ret)
1431                         goto out_free;
1432         }
1433
1434         if (!param) /* if param is non-empty, it's supposed to be a filter */
1435                 goto out_reg;
1436
1437         if (!cmd_ops->set_filter)
1438                 goto out_reg;
1439
1440         ret = cmd_ops->set_filter(param, trigger_data, file);
1441         if (ret < 0)
1442                 goto out_free;
1443
1444  out_reg:
1445         /* Don't let event modules unload while probe registered */
1446         ret = try_module_get(event_enable_file->event_call->mod);
1447         if (!ret) {
1448                 ret = -EBUSY;
1449                 goto out_free;
1450         }
1451
1452         ret = trace_event_enable_disable(event_enable_file, 1, 1);
1453         if (ret < 0)
1454                 goto out_put;
1455         ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
1456         /*
1457          * The above returns on success the # of functions enabled,
1458          * but if it didn't find any functions it returns zero.
1459          * Consider no functions a failure too.
1460          */
1461         if (!ret) {
1462                 ret = -ENOENT;
1463                 goto out_disable;
1464         } else if (ret < 0)
1465                 goto out_disable;
1466         /* Just return zero, not the number of enabled functions */
1467         ret = 0;
1468         event_trigger_free(trigger_ops, trigger_data);
1469  out:
1470         return ret;
1471
1472  out_disable:
1473         trace_event_enable_disable(event_enable_file, 0, 1);
1474  out_put:
1475         module_put(event_enable_file->event_call->mod);
1476  out_free:
1477         if (cmd_ops->set_filter)
1478                 cmd_ops->set_filter(NULL, trigger_data, NULL);
1479         event_trigger_free(trigger_ops, trigger_data);
1480         kfree(enable_data);
1481         goto out;
1482 }
1483
1484 int event_enable_register_trigger(char *glob,
1485                                   struct event_trigger_ops *ops,
1486                                   struct event_trigger_data *data,
1487                                   struct trace_event_file *file)
1488 {
1489         struct enable_trigger_data *enable_data = data->private_data;
1490         struct enable_trigger_data *test_enable_data;
1491         struct event_trigger_data *test;
1492         int ret = 0;
1493
1494         list_for_each_entry_rcu(test, &file->triggers, list) {
1495                 test_enable_data = test->private_data;
1496                 if (test_enable_data &&
1497                     (test->cmd_ops->trigger_type ==
1498                      data->cmd_ops->trigger_type) &&
1499                     (test_enable_data->file == enable_data->file)) {
1500                         ret = -EEXIST;
1501                         goto out;
1502                 }
1503         }
1504
1505         if (data->ops->init) {
1506                 ret = data->ops->init(data->ops, data);
1507                 if (ret < 0)
1508                         goto out;
1509         }
1510
1511         list_add_rcu(&data->list, &file->triggers);
1512         ret++;
1513
1514         update_cond_flag(file);
1515         if (trace_event_trigger_enable_disable(file, 1) < 0) {
1516                 list_del_rcu(&data->list);
1517                 update_cond_flag(file);
1518                 ret--;
1519         }
1520 out:
1521         return ret;
1522 }
1523
1524 void event_enable_unregister_trigger(char *glob,
1525                                      struct event_trigger_ops *ops,
1526                                      struct event_trigger_data *test,
1527                                      struct trace_event_file *file)
1528 {
1529         struct enable_trigger_data *test_enable_data = test->private_data;
1530         struct enable_trigger_data *enable_data;
1531         struct event_trigger_data *data;
1532         bool unregistered = false;
1533
1534         list_for_each_entry_rcu(data, &file->triggers, list) {
1535                 enable_data = data->private_data;
1536                 if (enable_data &&
1537                     (data->cmd_ops->trigger_type ==
1538                      test->cmd_ops->trigger_type) &&
1539                     (enable_data->file == test_enable_data->file)) {
1540                         unregistered = true;
1541                         list_del_rcu(&data->list);
1542                         trace_event_trigger_enable_disable(file, 0);
1543                         update_cond_flag(file);
1544                         break;
1545                 }
1546         }
1547
1548         if (unregistered && data->ops->free)
1549                 data->ops->free(data->ops, data);
1550 }
1551
1552 static struct event_trigger_ops *
1553 event_enable_get_trigger_ops(char *cmd, char *param)
1554 {
1555         struct event_trigger_ops *ops;
1556         bool enable;
1557
1558 #ifdef CONFIG_HIST_TRIGGERS
1559         enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
1560                   (strcmp(cmd, ENABLE_HIST_STR) == 0));
1561 #else
1562         enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1563 #endif
1564         if (enable)
1565                 ops = param ? &event_enable_count_trigger_ops :
1566                         &event_enable_trigger_ops;
1567         else
1568                 ops = param ? &event_disable_count_trigger_ops :
1569                         &event_disable_trigger_ops;
1570
1571         return ops;
1572 }
1573
1574 static struct event_command trigger_enable_cmd = {
1575         .name                   = ENABLE_EVENT_STR,
1576         .trigger_type           = ETT_EVENT_ENABLE,
1577         .func                   = event_enable_trigger_func,
1578         .reg                    = event_enable_register_trigger,
1579         .unreg                  = event_enable_unregister_trigger,
1580         .get_trigger_ops        = event_enable_get_trigger_ops,
1581         .set_filter             = set_trigger_filter,
1582 };
1583
1584 static struct event_command trigger_disable_cmd = {
1585         .name                   = DISABLE_EVENT_STR,
1586         .trigger_type           = ETT_EVENT_ENABLE,
1587         .func                   = event_enable_trigger_func,
1588         .reg                    = event_enable_register_trigger,
1589         .unreg                  = event_enable_unregister_trigger,
1590         .get_trigger_ops        = event_enable_get_trigger_ops,
1591         .set_filter             = set_trigger_filter,
1592 };
1593
1594 static __init void unregister_trigger_enable_disable_cmds(void)
1595 {
1596         unregister_event_command(&trigger_enable_cmd);
1597         unregister_event_command(&trigger_disable_cmd);
1598 }
1599
1600 static __init int register_trigger_enable_disable_cmds(void)
1601 {
1602         int ret;
1603
1604         ret = register_event_command(&trigger_enable_cmd);
1605         if (WARN_ON(ret < 0))
1606                 return ret;
1607         ret = register_event_command(&trigger_disable_cmd);
1608         if (WARN_ON(ret < 0))
1609                 unregister_trigger_enable_disable_cmds();
1610
1611         return ret;
1612 }
1613
1614 static __init int register_trigger_traceon_traceoff_cmds(void)
1615 {
1616         int ret;
1617
1618         ret = register_event_command(&trigger_traceon_cmd);
1619         if (WARN_ON(ret < 0))
1620                 return ret;
1621         ret = register_event_command(&trigger_traceoff_cmd);
1622         if (WARN_ON(ret < 0))
1623                 unregister_trigger_traceon_traceoff_cmds();
1624
1625         return ret;
1626 }
1627
1628 __init int register_trigger_cmds(void)
1629 {
1630         register_trigger_traceon_traceoff_cmds();
1631         register_trigger_snapshot_cmd();
1632         register_trigger_stacktrace_cmd();
1633         register_trigger_enable_disable_cmds();
1634         register_trigger_hist_enable_disable_cmds();
1635         register_trigger_hist_cmd();
1636
1637         return 0;
1638 }