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