1bcea3f96SSteven Rostedt (VMware) // SPDX-License-Identifier: GPL-2.0
285f2b082STom Zanussi /*
385f2b082STom Zanussi  * trace_events_trigger - trace event triggers
485f2b082STom Zanussi  *
585f2b082STom Zanussi  * Copyright (C) 2013 Tom Zanussi <[email protected]>
685f2b082STom Zanussi  */
785f2b082STom Zanussi 
817911ff3SSteven Rostedt (VMware) #include <linux/security.h>
985f2b082STom Zanussi #include <linux/module.h>
1085f2b082STom Zanussi #include <linux/ctype.h>
1185f2b082STom Zanussi #include <linux/mutex.h>
1285f2b082STom Zanussi #include <linux/slab.h>
13b2d09103SIngo Molnar #include <linux/rculist.h>
1485f2b082STom Zanussi 
1585f2b082STom Zanussi #include "trace.h"
1685f2b082STom Zanussi 
1785f2b082STom Zanussi static LIST_HEAD(trigger_commands);
1885f2b082STom Zanussi static DEFINE_MUTEX(trigger_cmd_mutex);
1985f2b082STom Zanussi 
trigger_data_free(struct event_trigger_data * data)20ab4bf008STom Zanussi void trigger_data_free(struct event_trigger_data *data)
212a2df321STom Zanussi {
22bac5fb97STom Zanussi 	if (data->cmd_ops->set_filter)
23bac5fb97STom Zanussi 		data->cmd_ops->set_filter(NULL, data, NULL);
24bac5fb97STom Zanussi 
25e0a568dcSSteven Rostedt (VMware) 	/* make sure current triggers exit before free */
26e0a568dcSSteven Rostedt (VMware) 	tracepoint_synchronize_unregister();
27e0a568dcSSteven Rostedt (VMware) 
282a2df321STom Zanussi 	kfree(data);
292a2df321STom Zanussi }
302a2df321STom Zanussi 
3185f2b082STom Zanussi /**
3285f2b082STom Zanussi  * event_triggers_call - Call triggers associated with a trace event
337f1d2f82SSteven Rostedt (Red Hat)  * @file: The trace_event_file associated with the event
34bd7217f3SGaosheng Cui  * @buffer: The ring buffer that the event is being written to
35bac5fb97STom Zanussi  * @rec: The trace entry for the event, NULL for unconditional invocation
36bd7217f3SGaosheng Cui  * @event: The event meta data in the ring buffer
3785f2b082STom Zanussi  *
3885f2b082STom Zanussi  * For each trigger associated with an event, invoke the trigger
39bac5fb97STom Zanussi  * function registered with the associated trigger command.  If rec is
40bac5fb97STom Zanussi  * non-NULL, it means that the trigger requires further processing and
41bac5fb97STom Zanussi  * shouldn't be unconditionally invoked.  If rec is non-NULL and the
42bac5fb97STom Zanussi  * trigger has a filter associated with it, rec will checked against
43bac5fb97STom Zanussi  * the filter and if the record matches the trigger will be invoked.
44bac5fb97STom Zanussi  * If the trigger is a 'post_trigger', meaning it shouldn't be invoked
45bac5fb97STom Zanussi  * in any case until the current event is written, the trigger
46bac5fb97STom Zanussi  * function isn't invoked but the bit associated with the deferred
47bac5fb97STom Zanussi  * trigger is set in the return value.
48bac5fb97STom Zanussi  *
49bac5fb97STom Zanussi  * Returns an enum event_trigger_type value containing a set bit for
50bac5fb97STom Zanussi  * any trigger that should be deferred, ETT_NONE if nothing to defer.
5185f2b082STom Zanussi  *
5285f2b082STom Zanussi  * Called from tracepoint handlers (with rcu_read_lock_sched() held).
5385f2b082STom Zanussi  *
5485f2b082STom Zanussi  * Return: an enum event_trigger_type value containing a set bit for
5585f2b082STom Zanussi  * any trigger that should be deferred, ETT_NONE if nothing to defer.
5685f2b082STom Zanussi  */
57bac5fb97STom Zanussi enum event_trigger_type
event_triggers_call(struct trace_event_file * file,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * event)58b47e3302SSteven Rostedt (VMware) event_triggers_call(struct trace_event_file *file,
59b47e3302SSteven Rostedt (VMware) 		    struct trace_buffer *buffer, void *rec,
601ac4f51cSTom Zanussi 		    struct ring_buffer_event *event)
61bac5fb97STom Zanussi {
62bac5fb97STom Zanussi 	struct event_trigger_data *data;
63bac5fb97STom Zanussi 	enum event_trigger_type tt = ETT_NONE;
64d8a30f20SSteven Rostedt (Red Hat) 	struct event_filter *filter;
65bac5fb97STom Zanussi 
66bac5fb97STom Zanussi 	if (list_empty(&file->triggers))
67bac5fb97STom Zanussi 		return tt;
68bac5fb97STom Zanussi 
69bac5fb97STom Zanussi 	list_for_each_entry_rcu(data, &file->triggers, list) {
70104f2810STom Zanussi 		if (data->paused)
71104f2810STom Zanussi 			continue;
72bac5fb97STom Zanussi 		if (!rec) {
73fb339e53STom Zanussi 			data->ops->trigger(data, buffer, rec, event);
74bac5fb97STom Zanussi 			continue;
75bac5fb97STom Zanussi 		}
76561a4fe8SSteven Rostedt (Red Hat) 		filter = rcu_dereference_sched(data->filter);
77d8a30f20SSteven Rostedt (Red Hat) 		if (filter && !filter_match_preds(filter, rec))
78bac5fb97STom Zanussi 			continue;
79353206f5SSteven Rostedt (Red Hat) 		if (event_command_post_trigger(data->cmd_ops)) {
80bac5fb97STom Zanussi 			tt |= data->cmd_ops->trigger_type;
81bac5fb97STom Zanussi 			continue;
82bac5fb97STom Zanussi 		}
83fb339e53STom Zanussi 		data->ops->trigger(data, buffer, rec, event);
84bac5fb97STom Zanussi 	}
85bac5fb97STom Zanussi 	return tt;
86bac5fb97STom Zanussi }
87bac5fb97STom Zanussi EXPORT_SYMBOL_GPL(event_triggers_call);
88bac5fb97STom Zanussi 
__trace_trigger_soft_disabled(struct trace_event_file * file)89bc82c38aSChristophe Leroy bool __trace_trigger_soft_disabled(struct trace_event_file *file)
90bc82c38aSChristophe Leroy {
91bc82c38aSChristophe Leroy 	unsigned long eflags = file->flags;
92bc82c38aSChristophe Leroy 
93bc82c38aSChristophe Leroy 	if (eflags & EVENT_FILE_FL_TRIGGER_MODE)
94bc82c38aSChristophe Leroy 		event_triggers_call(file, NULL, NULL, NULL);
95bc82c38aSChristophe Leroy 	if (eflags & EVENT_FILE_FL_SOFT_DISABLED)
96bc82c38aSChristophe Leroy 		return true;
97bc82c38aSChristophe Leroy 	if (eflags & EVENT_FILE_FL_PID_FILTER)
98bc82c38aSChristophe Leroy 		return trace_event_ignore_this_pid(file);
99bc82c38aSChristophe Leroy 	return false;
100bc82c38aSChristophe Leroy }
101bc82c38aSChristophe Leroy EXPORT_SYMBOL_GPL(__trace_trigger_soft_disabled);
102bc82c38aSChristophe Leroy 
103bac5fb97STom Zanussi /**
104bac5fb97STom Zanussi  * event_triggers_post_call - Call 'post_triggers' for a trace event
1057f1d2f82SSteven Rostedt (Red Hat)  * @file: The trace_event_file associated with the event
106bac5fb97STom Zanussi  * @tt: enum event_trigger_type containing a set bit for each trigger to invoke
107bac5fb97STom Zanussi  *
108bac5fb97STom Zanussi  * For each trigger associated with an event, invoke the trigger
109bac5fb97STom Zanussi  * function registered with the associated trigger command, if the
110bac5fb97STom Zanussi  * corresponding bit is set in the tt enum passed into this function.
111bac5fb97STom Zanussi  * See @event_triggers_call for details on how those bits are set.
112bac5fb97STom Zanussi  *
113bac5fb97STom Zanussi  * Called from tracepoint handlers (with rcu_read_lock_sched() held).
114bac5fb97STom Zanussi  */
115bac5fb97STom Zanussi void
event_triggers_post_call(struct trace_event_file * file,enum event_trigger_type tt)1167f1d2f82SSteven Rostedt (Red Hat) event_triggers_post_call(struct trace_event_file *file,
117c94e45bcSSteven Rostedt (VMware) 			 enum event_trigger_type tt)
11885f2b082STom Zanussi {
11985f2b082STom Zanussi 	struct event_trigger_data *data;
12085f2b082STom Zanussi 
121bac5fb97STom Zanussi 	list_for_each_entry_rcu(data, &file->triggers, list) {
122104f2810STom Zanussi 		if (data->paused)
123104f2810STom Zanussi 			continue;
124bac5fb97STom Zanussi 		if (data->cmd_ops->trigger_type & tt)
125fb339e53STom Zanussi 			data->ops->trigger(data, NULL, NULL, NULL);
12685f2b082STom Zanussi 	}
127bac5fb97STom Zanussi }
128bac5fb97STom Zanussi EXPORT_SYMBOL_GPL(event_triggers_post_call);
12985f2b082STom Zanussi 
130dd97b954SSteven Rostedt (Red Hat) #define SHOW_AVAILABLE_TRIGGERS	(void *)(1UL)
131dd97b954SSteven Rostedt (Red Hat) 
trigger_next(struct seq_file * m,void * t,loff_t * pos)13285f2b082STom Zanussi static void *trigger_next(struct seq_file *m, void *t, loff_t *pos)
13385f2b082STom Zanussi {
1347f1d2f82SSteven Rostedt (Red Hat) 	struct trace_event_file *event_file = event_file_data(m->private);
13585f2b082STom Zanussi 
1366722b23eSVasily Averin 	if (t == SHOW_AVAILABLE_TRIGGERS) {
1376722b23eSVasily Averin 		(*pos)++;
138dd97b954SSteven Rostedt (Red Hat) 		return NULL;
1396722b23eSVasily Averin 	}
14085f2b082STom Zanussi 	return seq_list_next(t, &event_file->triggers, pos);
14185f2b082STom Zanussi }
14285f2b082STom Zanussi 
check_user_trigger(struct trace_event_file * file)1437491e2c4STzvetomir Stoyanov (VMware) static bool check_user_trigger(struct trace_event_file *file)
1447491e2c4STzvetomir Stoyanov (VMware) {
1457491e2c4STzvetomir Stoyanov (VMware) 	struct event_trigger_data *data;
1467491e2c4STzvetomir Stoyanov (VMware) 
147cecf8e12SMasami Hiramatsu (Google) 	list_for_each_entry_rcu(data, &file->triggers, list,
148cecf8e12SMasami Hiramatsu (Google) 				lockdep_is_held(&event_mutex)) {
1497491e2c4STzvetomir Stoyanov (VMware) 		if (data->flags & EVENT_TRIGGER_FL_PROBE)
1507491e2c4STzvetomir Stoyanov (VMware) 			continue;
1517491e2c4STzvetomir Stoyanov (VMware) 		return true;
1527491e2c4STzvetomir Stoyanov (VMware) 	}
1537491e2c4STzvetomir Stoyanov (VMware) 	return false;
1547491e2c4STzvetomir Stoyanov (VMware) }
1557491e2c4STzvetomir Stoyanov (VMware) 
trigger_start(struct seq_file * m,loff_t * pos)15685f2b082STom Zanussi static void *trigger_start(struct seq_file *m, loff_t *pos)
15785f2b082STom Zanussi {
1587f1d2f82SSteven Rostedt (Red Hat) 	struct trace_event_file *event_file;
15985f2b082STom Zanussi 
16085f2b082STom Zanussi 	/* ->stop() is called even if ->start() fails */
16185f2b082STom Zanussi 	mutex_lock(&event_mutex);
162b1560408SSteven Rostedt 	event_file = event_file_file(m->private);
16385f2b082STom Zanussi 	if (unlikely(!event_file))
16485f2b082STom Zanussi 		return ERR_PTR(-ENODEV);
16585f2b082STom Zanussi 
1667491e2c4STzvetomir Stoyanov (VMware) 	if (list_empty(&event_file->triggers) || !check_user_trigger(event_file))
167dd97b954SSteven Rostedt (Red Hat) 		return *pos == 0 ? SHOW_AVAILABLE_TRIGGERS : NULL;
168dd97b954SSteven Rostedt (Red Hat) 
16985f2b082STom Zanussi 	return seq_list_start(&event_file->triggers, *pos);
17085f2b082STom Zanussi }
17185f2b082STom Zanussi 
trigger_stop(struct seq_file * m,void * t)17285f2b082STom Zanussi static void trigger_stop(struct seq_file *m, void *t)
17385f2b082STom Zanussi {
17485f2b082STom Zanussi 	mutex_unlock(&event_mutex);
17585f2b082STom Zanussi }
17685f2b082STom Zanussi 
trigger_show(struct seq_file * m,void * v)17785f2b082STom Zanussi static int trigger_show(struct seq_file *m, void *v)
17885f2b082STom Zanussi {
17985f2b082STom Zanussi 	struct event_trigger_data *data;
180dd97b954SSteven Rostedt (Red Hat) 	struct event_command *p;
181dd97b954SSteven Rostedt (Red Hat) 
182dd97b954SSteven Rostedt (Red Hat) 	if (v == SHOW_AVAILABLE_TRIGGERS) {
183dd97b954SSteven Rostedt (Red Hat) 		seq_puts(m, "# Available triggers:\n");
184dd97b954SSteven Rostedt (Red Hat) 		seq_putc(m, '#');
185dd97b954SSteven Rostedt (Red Hat) 		mutex_lock(&trigger_cmd_mutex);
186dd97b954SSteven Rostedt (Red Hat) 		list_for_each_entry_reverse(p, &trigger_commands, list)
187dd97b954SSteven Rostedt (Red Hat) 			seq_printf(m, " %s", p->name);
188dd97b954SSteven Rostedt (Red Hat) 		seq_putc(m, '\n');
189dd97b954SSteven Rostedt (Red Hat) 		mutex_unlock(&trigger_cmd_mutex);
190dd97b954SSteven Rostedt (Red Hat) 		return 0;
191dd97b954SSteven Rostedt (Red Hat) 	}
19285f2b082STom Zanussi 
19385f2b082STom Zanussi 	data = list_entry(v, struct event_trigger_data, list);
19447670541STom Zanussi 	data->ops->print(m, data);
19585f2b082STom Zanussi 
19685f2b082STom Zanussi 	return 0;
19785f2b082STom Zanussi }
19885f2b082STom Zanussi 
19985f2b082STom Zanussi static const struct seq_operations event_triggers_seq_ops = {
20085f2b082STom Zanussi 	.start = trigger_start,
20185f2b082STom Zanussi 	.next = trigger_next,
20285f2b082STom Zanussi 	.stop = trigger_stop,
20385f2b082STom Zanussi 	.show = trigger_show,
20485f2b082STom Zanussi };
20585f2b082STom Zanussi 
event_trigger_regex_open(struct inode * inode,struct file * file)20685f2b082STom Zanussi static int event_trigger_regex_open(struct inode *inode, struct file *file)
20785f2b082STom Zanussi {
20817911ff3SSteven Rostedt (VMware) 	int ret;
20917911ff3SSteven Rostedt (VMware) 
21017911ff3SSteven Rostedt (VMware) 	ret = security_locked_down(LOCKDOWN_TRACEFS);
21117911ff3SSteven Rostedt (VMware) 	if (ret)
21217911ff3SSteven Rostedt (VMware) 		return ret;
21385f2b082STom Zanussi 
21463c72641SSteven Rostedt 	guard(mutex)(&event_mutex);
21585f2b082STom Zanussi 
21663c72641SSteven Rostedt 	if (unlikely(!event_file_file(file)))
21785f2b082STom Zanussi 		return -ENODEV;
21885f2b082STom Zanussi 
219a88e1cfbSTom Zanussi 	if ((file->f_mode & FMODE_WRITE) &&
220a88e1cfbSTom Zanussi 	    (file->f_flags & O_TRUNC)) {
221a88e1cfbSTom Zanussi 		struct trace_event_file *event_file;
222a88e1cfbSTom Zanussi 		struct event_command *p;
223a88e1cfbSTom Zanussi 
224a88e1cfbSTom Zanussi 		event_file = event_file_data(file);
225a88e1cfbSTom Zanussi 
226a88e1cfbSTom Zanussi 		list_for_each_entry(p, &trigger_commands, list) {
227a88e1cfbSTom Zanussi 			if (p->unreg_all)
228a88e1cfbSTom Zanussi 				p->unreg_all(event_file);
229a88e1cfbSTom Zanussi 		}
230a88e1cfbSTom Zanussi 	}
231a88e1cfbSTom Zanussi 
23285f2b082STom Zanussi 	if (file->f_mode & FMODE_READ) {
23385f2b082STom Zanussi 		ret = seq_open(file, &event_triggers_seq_ops);
23485f2b082STom Zanussi 		if (!ret) {
23585f2b082STom Zanussi 			struct seq_file *m = file->private_data;
23685f2b082STom Zanussi 			m->private = file;
23785f2b082STom Zanussi 		}
23885f2b082STom Zanussi 	}
23985f2b082STom Zanussi 
24085f2b082STom Zanussi 	return ret;
24185f2b082STom Zanussi }
24285f2b082STom Zanussi 
trigger_process_regex(struct trace_event_file * file,char * buff)24381a59555SMasami Hiramatsu int trigger_process_regex(struct trace_event_file *file, char *buff)
24485f2b082STom Zanussi {
2456784beadSMasami Hiramatsu 	char *command, *next;
24685f2b082STom Zanussi 	struct event_command *p;
24785f2b082STom Zanussi 
2486784beadSMasami Hiramatsu 	next = buff = skip_spaces(buff);
24985f2b082STom Zanussi 	command = strsep(&next, ": \t");
2506784beadSMasami Hiramatsu 	if (next) {
2516784beadSMasami Hiramatsu 		next = skip_spaces(next);
2526784beadSMasami Hiramatsu 		if (!*next)
2536784beadSMasami Hiramatsu 			next = NULL;
2546784beadSMasami Hiramatsu 	}
25585f2b082STom Zanussi 	command = (command[0] != '!') ? command : command + 1;
25685f2b082STom Zanussi 
25763c72641SSteven Rostedt 	guard(mutex)(&trigger_cmd_mutex);
25885f2b082STom Zanussi 
25963c72641SSteven Rostedt 	list_for_each_entry(p, &trigger_commands, list) {
26063c72641SSteven Rostedt 		if (strcmp(p->name, command) == 0)
26163c72641SSteven Rostedt 			return p->parse(p, file, buff, command, next);
26263c72641SSteven Rostedt 	}
26363c72641SSteven Rostedt 
26463c72641SSteven Rostedt 	return -EINVAL;
26585f2b082STom Zanussi }
26685f2b082STom Zanussi 
event_trigger_regex_write(struct file * file,const char __user * ubuf,size_t cnt,loff_t * ppos)26785f2b082STom Zanussi static ssize_t event_trigger_regex_write(struct file *file,
26885f2b082STom Zanussi 					 const char __user *ubuf,
26985f2b082STom Zanussi 					 size_t cnt, loff_t *ppos)
27085f2b082STom Zanussi {
2717f1d2f82SSteven Rostedt (Red Hat) 	struct trace_event_file *event_file;
27285f2b082STom Zanussi 	ssize_t ret;
27363c72641SSteven Rostedt 	char *buf __free(kfree) = NULL;
27485f2b082STom Zanussi 
27585f2b082STom Zanussi 	if (!cnt)
27685f2b082STom Zanussi 		return 0;
27785f2b082STom Zanussi 
27885f2b082STom Zanussi 	if (cnt >= PAGE_SIZE)
27985f2b082STom Zanussi 		return -EINVAL;
28085f2b082STom Zanussi 
28170f6cbb6SAl Viro 	buf = memdup_user_nul(ubuf, cnt);
28270f6cbb6SAl Viro 	if (IS_ERR(buf))
28370f6cbb6SAl Viro 		return PTR_ERR(buf);
28485f2b082STom Zanussi 
28585f2b082STom Zanussi 	strim(buf);
28685f2b082STom Zanussi 
28763c72641SSteven Rostedt 	guard(mutex)(&event_mutex);
28885f2b082STom Zanussi 
28963c72641SSteven Rostedt 	event_file = event_file_file(file);
29063c72641SSteven Rostedt 	if (unlikely(!event_file))
29163c72641SSteven Rostedt 		return -ENODEV;
29263c72641SSteven Rostedt 
29363c72641SSteven Rostedt 	ret = trigger_process_regex(event_file, buf);
29485f2b082STom Zanussi 	if (ret < 0)
29563c72641SSteven Rostedt 		return ret;
29685f2b082STom Zanussi 
29785f2b082STom Zanussi 	*ppos += cnt;
29863c72641SSteven Rostedt 	return cnt;
29985f2b082STom Zanussi }
30085f2b082STom Zanussi 
event_trigger_regex_release(struct inode * inode,struct file * file)30185f2b082STom Zanussi static int event_trigger_regex_release(struct inode *inode, struct file *file)
30285f2b082STom Zanussi {
30385f2b082STom Zanussi 	mutex_lock(&event_mutex);
30485f2b082STom Zanussi 
30585f2b082STom Zanussi 	if (file->f_mode & FMODE_READ)
30685f2b082STom Zanussi 		seq_release(inode, file);
30785f2b082STom Zanussi 
30885f2b082STom Zanussi 	mutex_unlock(&event_mutex);
30985f2b082STom Zanussi 
31085f2b082STom Zanussi 	return 0;
31185f2b082STom Zanussi }
31285f2b082STom Zanussi 
31385f2b082STom Zanussi static ssize_t
event_trigger_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos)31485f2b082STom Zanussi event_trigger_write(struct file *filp, const char __user *ubuf,
31585f2b082STom Zanussi 		    size_t cnt, loff_t *ppos)
31685f2b082STom Zanussi {
31785f2b082STom Zanussi 	return event_trigger_regex_write(filp, ubuf, cnt, ppos);
31885f2b082STom Zanussi }
31985f2b082STom Zanussi 
32085f2b082STom Zanussi static int
event_trigger_open(struct inode * inode,struct file * filp)32185f2b082STom Zanussi event_trigger_open(struct inode *inode, struct file *filp)
32285f2b082STom Zanussi {
32317911ff3SSteven Rostedt (VMware) 	/* Checks for tracefs lockdown */
32485f2b082STom Zanussi 	return event_trigger_regex_open(inode, filp);
32585f2b082STom Zanussi }
32685f2b082STom Zanussi 
32785f2b082STom Zanussi static int
event_trigger_release(struct inode * inode,struct file * file)32885f2b082STom Zanussi event_trigger_release(struct inode *inode, struct file *file)
32985f2b082STom Zanussi {
33085f2b082STom Zanussi 	return event_trigger_regex_release(inode, file);
33185f2b082STom Zanussi }
33285f2b082STom Zanussi 
33385f2b082STom Zanussi const struct file_operations event_trigger_fops = {
33485f2b082STom Zanussi 	.open = event_trigger_open,
33585f2b082STom Zanussi 	.read = seq_read,
33685f2b082STom Zanussi 	.write = event_trigger_write,
337098c879eSSteven Rostedt (Red Hat) 	.llseek = tracing_lseek,
33885f2b082STom Zanussi 	.release = event_trigger_release,
33985f2b082STom Zanussi };
34085f2b082STom Zanussi 
3412a2df321STom Zanussi /*
3422a2df321STom Zanussi  * Currently we only register event commands from __init, so mark this
3432a2df321STom Zanussi  * __init too.
3442a2df321STom Zanussi  */
register_event_command(struct event_command * cmd)345ab4bf008STom Zanussi __init int register_event_command(struct event_command *cmd)
3462a2df321STom Zanussi {
3472a2df321STom Zanussi 	struct event_command *p;
3482a2df321STom Zanussi 
34963c72641SSteven Rostedt 	guard(mutex)(&trigger_cmd_mutex);
35063c72641SSteven Rostedt 
3512a2df321STom Zanussi 	list_for_each_entry(p, &trigger_commands, list) {
35263c72641SSteven Rostedt 		if (strcmp(cmd->name, p->name) == 0)
35363c72641SSteven Rostedt 			return -EBUSY;
3542a2df321STom Zanussi 	}
3552a2df321STom Zanussi 	list_add(&cmd->list, &trigger_commands);
3562a2df321STom Zanussi 
35763c72641SSteven Rostedt 	return 0;
3582a2df321STom Zanussi }
3592a2df321STom Zanussi 
3602a2df321STom Zanussi /*
3612a2df321STom Zanussi  * Currently we only unregister event commands from __init, so mark
3622a2df321STom Zanussi  * this __init too.
3632a2df321STom Zanussi  */
unregister_event_command(struct event_command * cmd)364d0bad49bSTom Zanussi __init int unregister_event_command(struct event_command *cmd)
3652a2df321STom Zanussi {
3662a2df321STom Zanussi 	struct event_command *p, *n;
3672a2df321STom Zanussi 
36863c72641SSteven Rostedt 	guard(mutex)(&trigger_cmd_mutex);
36963c72641SSteven Rostedt 
3702a2df321STom Zanussi 	list_for_each_entry_safe(p, n, &trigger_commands, list) {
3712a2df321STom Zanussi 		if (strcmp(cmd->name, p->name) == 0) {
3722a2df321STom Zanussi 			list_del_init(&p->list);
37363c72641SSteven Rostedt 			return 0;
3742a2df321STom Zanussi 		}
3752a2df321STom Zanussi 	}
3762a2df321STom Zanussi 
37763c72641SSteven Rostedt 	return -ENODEV;
3782a2df321STom Zanussi }
3792a2df321STom Zanussi 
3802a2df321STom Zanussi /**
3812a2df321STom Zanussi  * event_trigger_print - Generic event_trigger_ops @print implementation
3822a2df321STom Zanussi  * @name: The name of the event trigger
3832a2df321STom Zanussi  * @m: The seq_file being printed to
3842a2df321STom Zanussi  * @data: Trigger-specific data
3852a2df321STom Zanussi  * @filter_str: filter_str to print, if present
3862a2df321STom Zanussi  *
3872a2df321STom Zanussi  * Common implementation for event triggers to print themselves.
3882a2df321STom Zanussi  *
3892a2df321STom Zanussi  * Usually wrapped by a function that simply sets the @name of the
3902a2df321STom Zanussi  * trigger command and then invokes this.
3912a2df321STom Zanussi  *
3922a2df321STom Zanussi  * Return: 0 on success, errno otherwise
3932a2df321STom Zanussi  */
3942a2df321STom Zanussi static int
event_trigger_print(const char * name,struct seq_file * m,void * data,char * filter_str)3952a2df321STom Zanussi event_trigger_print(const char *name, struct seq_file *m,
3962a2df321STom Zanussi 		    void *data, char *filter_str)
3972a2df321STom Zanussi {
3982a2df321STom Zanussi 	long count = (long)data;
3992a2df321STom Zanussi 
400fa6f0cc7SRasmus Villemoes 	seq_puts(m, name);
4012a2df321STom Zanussi 
4022a2df321STom Zanussi 	if (count == -1)
4032a2df321STom Zanussi 		seq_puts(m, ":unlimited");
4042a2df321STom Zanussi 	else
4052a2df321STom Zanussi 		seq_printf(m, ":count=%ld", count);
4062a2df321STom Zanussi 
4072a2df321STom Zanussi 	if (filter_str)
4082a2df321STom Zanussi 		seq_printf(m, " if %s\n", filter_str);
4092a2df321STom Zanussi 	else
4101177e436SRasmus Villemoes 		seq_putc(m, '\n');
4112a2df321STom Zanussi 
4122a2df321STom Zanussi 	return 0;
4132a2df321STom Zanussi }
4142a2df321STom Zanussi 
4152a2df321STom Zanussi /**
4162a2df321STom Zanussi  * event_trigger_init - Generic event_trigger_ops @init implementation
4172a2df321STom Zanussi  * @data: Trigger-specific data
4182a2df321STom Zanussi  *
4192a2df321STom Zanussi  * Common implementation of event trigger initialization.
4202a2df321STom Zanussi  *
4212a2df321STom Zanussi  * Usually used directly as the @init method in event trigger
4222a2df321STom Zanussi  * implementations.
4232a2df321STom Zanussi  *
4242a2df321STom Zanussi  * Return: 0 on success, errno otherwise
4252a2df321STom Zanussi  */
event_trigger_init(struct event_trigger_data * data)42647670541STom Zanussi int event_trigger_init(struct event_trigger_data *data)
4272a2df321STom Zanussi {
4282a2df321STom Zanussi 	data->ref++;
4292a2df321STom Zanussi 	return 0;
4302a2df321STom Zanussi }
4312a2df321STom Zanussi 
4322a2df321STom Zanussi /**
4332a2df321STom Zanussi  * event_trigger_free - Generic event_trigger_ops @free implementation
4342a2df321STom Zanussi  * @data: Trigger-specific data
4352a2df321STom Zanussi  *
4362a2df321STom Zanussi  * Common implementation of event trigger de-initialization.
4372a2df321STom Zanussi  *
4382a2df321STom Zanussi  * Usually used directly as the @free method in event trigger
4392a2df321STom Zanussi  * implementations.
4402a2df321STom Zanussi  */
4412a2df321STom Zanussi static void
event_trigger_free(struct event_trigger_data * data)44247670541STom Zanussi event_trigger_free(struct event_trigger_data *data)
4432a2df321STom Zanussi {
4442a2df321STom Zanussi 	if (WARN_ON_ONCE(data->ref <= 0))
4452a2df321STom Zanussi 		return;
4462a2df321STom Zanussi 
4472a2df321STom Zanussi 	data->ref--;
4482a2df321STom Zanussi 	if (!data->ref)
4492a2df321STom Zanussi 		trigger_data_free(data);
4502a2df321STom Zanussi }
4512a2df321STom Zanussi 
trace_event_trigger_enable_disable(struct trace_event_file * file,int trigger_enable)452ab4bf008STom Zanussi int trace_event_trigger_enable_disable(struct trace_event_file *file,
45385f2b082STom Zanussi 				       int trigger_enable)
45485f2b082STom Zanussi {
45585f2b082STom Zanussi 	int ret = 0;
45685f2b082STom Zanussi 
45785f2b082STom Zanussi 	if (trigger_enable) {
45885f2b082STom Zanussi 		if (atomic_inc_return(&file->tm_ref) > 1)
45985f2b082STom Zanussi 			return ret;
4605d6ad960SSteven Rostedt (Red Hat) 		set_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
46185f2b082STom Zanussi 		ret = trace_event_enable_disable(file, 1, 1);
46285f2b082STom Zanussi 	} else {
46385f2b082STom Zanussi 		if (atomic_dec_return(&file->tm_ref) > 0)
46485f2b082STom Zanussi 			return ret;
4655d6ad960SSteven Rostedt (Red Hat) 		clear_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
46685f2b082STom Zanussi 		ret = trace_event_enable_disable(file, 0, 1);
46785f2b082STom Zanussi 	}
46885f2b082STom Zanussi 
46985f2b082STom Zanussi 	return ret;
47085f2b082STom Zanussi }
47185f2b082STom Zanussi 
47285f2b082STom Zanussi /**
47385f2b082STom Zanussi  * clear_event_triggers - Clear all triggers associated with a trace array
47485f2b082STom Zanussi  * @tr: The trace array to clear
47585f2b082STom Zanussi  *
47685f2b082STom Zanussi  * For each trigger, the triggering event has its tm_ref decremented
47785f2b082STom Zanussi  * via trace_event_trigger_enable_disable(), and any associated event
47885f2b082STom Zanussi  * (in the case of enable/disable_event triggers) will have its sm_ref
47985f2b082STom Zanussi  * decremented via free()->trace_event_enable_disable().  That
48085f2b082STom Zanussi  * combination effectively reverses the soft-mode/trigger state added
48185f2b082STom Zanussi  * by trigger registration.
48285f2b082STom Zanussi  *
48385f2b082STom Zanussi  * Must be called with event_mutex held.
48485f2b082STom Zanussi  */
48585f2b082STom Zanussi void
clear_event_triggers(struct trace_array * tr)48685f2b082STom Zanussi clear_event_triggers(struct trace_array *tr)
48785f2b082STom Zanussi {
4887f1d2f82SSteven Rostedt (Red Hat) 	struct trace_event_file *file;
48985f2b082STom Zanussi 
49085f2b082STom Zanussi 	list_for_each_entry(file, &tr->events, list) {
49186b389ffSSteven Rostedt (VMware) 		struct event_trigger_data *data, *n;
49286b389ffSSteven Rostedt (VMware) 		list_for_each_entry_safe(data, n, &file->triggers, list) {
49385f2b082STom Zanussi 			trace_event_trigger_enable_disable(file, 0);
49486b389ffSSteven Rostedt (VMware) 			list_del_rcu(&data->list);
49585f2b082STom Zanussi 			if (data->ops->free)
49647670541STom Zanussi 				data->ops->free(data);
49785f2b082STom Zanussi 		}
49885f2b082STom Zanussi 	}
49985f2b082STom Zanussi }
50085f2b082STom Zanussi 
5012a2df321STom Zanussi /**
502bac5fb97STom Zanussi  * update_cond_flag - Set or reset the TRIGGER_COND bit
5037f1d2f82SSteven Rostedt (Red Hat)  * @file: The trace_event_file associated with the event
504bac5fb97STom Zanussi  *
505bac5fb97STom Zanussi  * If an event has triggers and any of those triggers has a filter or
506bac5fb97STom Zanussi  * a post_trigger, trigger invocation needs to be deferred until after
507bac5fb97STom Zanussi  * the current event has logged its data, and the event should have
508bac5fb97STom Zanussi  * its TRIGGER_COND bit set, otherwise the TRIGGER_COND bit should be
509bac5fb97STom Zanussi  * cleared.
510bac5fb97STom Zanussi  */
update_cond_flag(struct trace_event_file * file)511ab4bf008STom Zanussi void update_cond_flag(struct trace_event_file *file)
512bac5fb97STom Zanussi {
513bac5fb97STom Zanussi 	struct event_trigger_data *data;
514bac5fb97STom Zanussi 	bool set_cond = false;
515bac5fb97STom Zanussi 
5163b42a4c8SMasami Hiramatsu 	lockdep_assert_held(&event_mutex);
5173b42a4c8SMasami Hiramatsu 
5183b42a4c8SMasami Hiramatsu 	list_for_each_entry(data, &file->triggers, list) {
519353206f5SSteven Rostedt (Red Hat) 		if (data->filter || event_command_post_trigger(data->cmd_ops) ||
520353206f5SSteven Rostedt (Red Hat) 		    event_command_needs_rec(data->cmd_ops)) {
521bac5fb97STom Zanussi 			set_cond = true;
522bac5fb97STom Zanussi 			break;
523bac5fb97STom Zanussi 		}
524bac5fb97STom Zanussi 	}
525bac5fb97STom Zanussi 
526bac5fb97STom Zanussi 	if (set_cond)
5275d6ad960SSteven Rostedt (Red Hat) 		set_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
528bac5fb97STom Zanussi 	else
5295d6ad960SSteven Rostedt (Red Hat) 		clear_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
530bac5fb97STom Zanussi }
531bac5fb97STom Zanussi 
532bac5fb97STom Zanussi /**
5332a2df321STom Zanussi  * register_trigger - Generic event_command @reg implementation
5342a2df321STom Zanussi  * @glob: The raw string used to register the trigger
5352a2df321STom Zanussi  * @data: Trigger-specific data to associate with the trigger
5367f1d2f82SSteven Rostedt (Red Hat)  * @file: The trace_event_file associated with the event
5372a2df321STom Zanussi  *
5382a2df321STom Zanussi  * Common implementation for event trigger registration.
5392a2df321STom Zanussi  *
5402a2df321STom Zanussi  * Usually used directly as the @reg method in event command
5412a2df321STom Zanussi  * implementations.
5422a2df321STom Zanussi  *
5432a2df321STom Zanussi  * Return: 0 on success, errno otherwise
5442a2df321STom Zanussi  */
register_trigger(char * glob,struct event_trigger_data * data,struct trace_event_file * file)5452378a2d6STom Zanussi static int register_trigger(char *glob,
5462a2df321STom Zanussi 			    struct event_trigger_data *data,
5477f1d2f82SSteven Rostedt (Red Hat) 			    struct trace_event_file *file)
5482a2df321STom Zanussi {
5492a2df321STom Zanussi 	struct event_trigger_data *test;
5502a2df321STom Zanussi 	int ret = 0;
5512a2df321STom Zanussi 
5523b42a4c8SMasami Hiramatsu 	lockdep_assert_held(&event_mutex);
5533b42a4c8SMasami Hiramatsu 
5543b42a4c8SMasami Hiramatsu 	list_for_each_entry(test, &file->triggers, list) {
5552a2df321STom Zanussi 		if (test->cmd_ops->trigger_type == data->cmd_ops->trigger_type) {
5562a2df321STom Zanussi 			ret = -EEXIST;
5572a2df321STom Zanussi 			goto out;
5582a2df321STom Zanussi 		}
5592a2df321STom Zanussi 	}
5602a2df321STom Zanussi 
5612a2df321STom Zanussi 	if (data->ops->init) {
56247670541STom Zanussi 		ret = data->ops->init(data);
5632a2df321STom Zanussi 		if (ret < 0)
5642a2df321STom Zanussi 			goto out;
5652a2df321STom Zanussi 	}
5662a2df321STom Zanussi 
5672a2df321STom Zanussi 	list_add_rcu(&data->list, &file->triggers);
5682a2df321STom Zanussi 
5694e4a4d75STom Zanussi 	update_cond_flag(file);
570b8cc44a4STom Zanussi 	ret = trace_event_trigger_enable_disable(file, 1);
571b8cc44a4STom Zanussi 	if (ret < 0) {
5722a2df321STom Zanussi 		list_del_rcu(&data->list);
5734e4a4d75STom Zanussi 		update_cond_flag(file);
5742a2df321STom Zanussi 	}
5752a2df321STom Zanussi out:
5762a2df321STom Zanussi 	return ret;
5772a2df321STom Zanussi }
5782a2df321STom Zanussi 
579180e4e39SVincent Donnefort /*
580180e4e39SVincent Donnefort  * True if the trigger was found and unregistered, else false.
5812a2df321STom Zanussi  */
try_unregister_trigger(char * glob,struct event_trigger_data * test,struct trace_event_file * file)582180e4e39SVincent Donnefort static bool try_unregister_trigger(char *glob,
5832a2df321STom Zanussi 				   struct event_trigger_data *test,
5847f1d2f82SSteven Rostedt (Red Hat) 				   struct trace_event_file *file)
5852a2df321STom Zanussi {
58645e333ceSJakob Koschel 	struct event_trigger_data *data = NULL, *iter;
5872a2df321STom Zanussi 
5883b42a4c8SMasami Hiramatsu 	lockdep_assert_held(&event_mutex);
5893b42a4c8SMasami Hiramatsu 
59045e333ceSJakob Koschel 	list_for_each_entry(iter, &file->triggers, list) {
59145e333ceSJakob Koschel 		if (iter->cmd_ops->trigger_type == test->cmd_ops->trigger_type) {
59245e333ceSJakob Koschel 			data = iter;
5932a2df321STom Zanussi 			list_del_rcu(&data->list);
5942a2df321STom Zanussi 			trace_event_trigger_enable_disable(file, 0);
5954e4a4d75STom Zanussi 			update_cond_flag(file);
5962a2df321STom Zanussi 			break;
5972a2df321STom Zanussi 		}
5982a2df321STom Zanussi 	}
5992a2df321STom Zanussi 
600180e4e39SVincent Donnefort 	if (data) {
601180e4e39SVincent Donnefort 		if (data->ops->free)
60247670541STom Zanussi 			data->ops->free(data);
603180e4e39SVincent Donnefort 
604180e4e39SVincent Donnefort 		return true;
605180e4e39SVincent Donnefort 	}
606180e4e39SVincent Donnefort 
607180e4e39SVincent Donnefort 	return false;
608180e4e39SVincent Donnefort }
609180e4e39SVincent Donnefort 
610180e4e39SVincent Donnefort /**
611180e4e39SVincent Donnefort  * unregister_trigger - Generic event_command @unreg implementation
612180e4e39SVincent Donnefort  * @glob: The raw string used to register the trigger
613180e4e39SVincent Donnefort  * @test: Trigger-specific data used to find the trigger to remove
614180e4e39SVincent Donnefort  * @file: The trace_event_file associated with the event
615180e4e39SVincent Donnefort  *
616180e4e39SVincent Donnefort  * Common implementation for event trigger unregistration.
617180e4e39SVincent Donnefort  *
618180e4e39SVincent Donnefort  * Usually used directly as the @unreg method in event command
619180e4e39SVincent Donnefort  * implementations.
620180e4e39SVincent Donnefort  */
unregister_trigger(char * glob,struct event_trigger_data * test,struct trace_event_file * file)621180e4e39SVincent Donnefort static void unregister_trigger(char *glob,
622180e4e39SVincent Donnefort 			       struct event_trigger_data *test,
623180e4e39SVincent Donnefort 			       struct trace_event_file *file)
624180e4e39SVincent Donnefort {
625180e4e39SVincent Donnefort 	try_unregister_trigger(glob, test, file);
6262a2df321STom Zanussi }
6272a2df321STom Zanussi 
62886599dbeSTom Zanussi /*
62986599dbeSTom Zanussi  * Event trigger parsing helper functions.
63086599dbeSTom Zanussi  *
63186599dbeSTom Zanussi  * These functions help make it easier to write an event trigger
63286599dbeSTom Zanussi  * parsing function i.e. the struct event_command.parse() callback
63386599dbeSTom Zanussi  * function responsible for parsing and registering a trigger command
63486599dbeSTom Zanussi  * written to the 'trigger' file.
63586599dbeSTom Zanussi  *
63686599dbeSTom Zanussi  * A trigger command (or just 'trigger' for short) takes the form:
63786599dbeSTom Zanussi  *   [trigger] [if filter]
63886599dbeSTom Zanussi  *
63986599dbeSTom Zanussi  * The struct event_command.parse() callback (and other struct
64086599dbeSTom Zanussi  * event_command functions) refer to several components of a trigger
64186599dbeSTom Zanussi  * command.  Those same components are referenced by the event trigger
64286599dbeSTom Zanussi  * parsing helper functions defined below.  These components are:
64386599dbeSTom Zanussi  *
64486599dbeSTom Zanussi  *   cmd               - the trigger command name
64586599dbeSTom Zanussi  *   glob              - the trigger command name optionally prefaced with '!'
64686599dbeSTom Zanussi  *   param_and_filter  - text following cmd and ':'
64786599dbeSTom Zanussi  *   param             - text following cmd and ':' and stripped of filter
64886599dbeSTom Zanussi  *   filter            - the optional filter text following (and including) 'if'
64986599dbeSTom Zanussi  *
65086599dbeSTom Zanussi  * To illustrate the use of these componenents, here are some concrete
65186599dbeSTom Zanussi  * examples. For the following triggers:
65286599dbeSTom Zanussi  *
65386599dbeSTom Zanussi  *   echo 'traceon:5 if pid == 0' > trigger
65486599dbeSTom Zanussi  *     - 'traceon' is both cmd and glob
65586599dbeSTom Zanussi  *     - '5 if pid == 0' is the param_and_filter
65686599dbeSTom Zanussi  *     - '5' is the param
65786599dbeSTom Zanussi  *     - 'if pid == 0' is the filter
65886599dbeSTom Zanussi  *
65986599dbeSTom Zanussi  *   echo 'enable_event:sys:event:n' > trigger
66086599dbeSTom Zanussi  *     - 'enable_event' is both cmd and glob
66186599dbeSTom Zanussi  *     - 'sys:event:n' is the param_and_filter
66286599dbeSTom Zanussi  *     - 'sys:event:n' is the param
66386599dbeSTom Zanussi  *     - there is no filter
66486599dbeSTom Zanussi  *
66586599dbeSTom Zanussi  *   echo 'hist:keys=pid if prio > 50' > trigger
66686599dbeSTom Zanussi  *     - 'hist' is both cmd and glob
66786599dbeSTom Zanussi  *     - 'keys=pid if prio > 50' is the param_and_filter
66886599dbeSTom Zanussi  *     - 'keys=pid' is the param
66986599dbeSTom Zanussi  *     - 'if prio > 50' is the filter
67086599dbeSTom Zanussi  *
67186599dbeSTom Zanussi  *   echo '!enable_event:sys:event:n' > trigger
67286599dbeSTom Zanussi  *     - 'enable_event' the cmd
67386599dbeSTom Zanussi  *     - '!enable_event' is the glob
67486599dbeSTom Zanussi  *     - 'sys:event:n' is the param_and_filter
67586599dbeSTom Zanussi  *     - 'sys:event:n' is the param
67686599dbeSTom Zanussi  *     - there is no filter
67786599dbeSTom Zanussi  *
67886599dbeSTom Zanussi  *   echo 'traceoff' > trigger
67986599dbeSTom Zanussi  *     - 'traceoff' is both cmd and glob
68086599dbeSTom Zanussi  *     - there is no param_and_filter
68186599dbeSTom Zanussi  *     - there is no param
68286599dbeSTom Zanussi  *     - there is no filter
68386599dbeSTom Zanussi  *
68486599dbeSTom Zanussi  * There are a few different categories of event trigger covered by
68586599dbeSTom Zanussi  * these helpers:
68686599dbeSTom Zanussi  *
68786599dbeSTom Zanussi  *  - triggers that don't require a parameter e.g. traceon
68886599dbeSTom Zanussi  *  - triggers that do require a parameter e.g. enable_event and hist
68986599dbeSTom Zanussi  *  - triggers that though they may not require a param may support an
69086599dbeSTom Zanussi  *    optional 'n' param (n = number of times the trigger should fire)
69186599dbeSTom Zanussi  *    e.g.: traceon:5 or enable_event:sys:event:n
69286599dbeSTom Zanussi  *  - triggers that do not support an 'n' param e.g. hist
69386599dbeSTom Zanussi  *
69486599dbeSTom Zanussi  * These functions can be used or ignored as necessary - it all
69586599dbeSTom Zanussi  * depends on the complexity of the trigger, and the granularity of
69686599dbeSTom Zanussi  * the functions supported reflects the fact that some implementations
69786599dbeSTom Zanussi  * may need to customize certain aspects of their implementations and
69886599dbeSTom Zanussi  * won't need certain functions.  For instance, the hist trigger
69986599dbeSTom Zanussi  * implementation doesn't use event_trigger_separate_filter() because
70086599dbeSTom Zanussi  * it has special requirements for handling the filter.
70186599dbeSTom Zanussi  */
70286599dbeSTom Zanussi 
70386599dbeSTom Zanussi /**
70486599dbeSTom Zanussi  * event_trigger_check_remove - check whether an event trigger specifies remove
70586599dbeSTom Zanussi  * @glob: The trigger command string, with optional remove(!) operator
70686599dbeSTom Zanussi  *
70786599dbeSTom Zanussi  * The event trigger callback implementations pass in 'glob' as a
70886599dbeSTom Zanussi  * parameter.  This is the command name either with or without a
70986599dbeSTom Zanussi  * remove(!)  operator.  This function simply parses the glob and
71086599dbeSTom Zanussi  * determines whether the command corresponds to a trigger removal or
71186599dbeSTom Zanussi  * a trigger addition.
71286599dbeSTom Zanussi  *
71386599dbeSTom Zanussi  * Return: true if this is a remove command, false otherwise
71486599dbeSTom Zanussi  */
event_trigger_check_remove(const char * glob)71586599dbeSTom Zanussi bool event_trigger_check_remove(const char *glob)
71686599dbeSTom Zanussi {
71786599dbeSTom Zanussi 	return (glob && glob[0] == '!') ? true : false;
71886599dbeSTom Zanussi }
71986599dbeSTom Zanussi 
72086599dbeSTom Zanussi /**
72186599dbeSTom Zanussi  * event_trigger_empty_param - check whether the param is empty
72286599dbeSTom Zanussi  * @param: The trigger param string
72386599dbeSTom Zanussi  *
72486599dbeSTom Zanussi  * The event trigger callback implementations pass in 'param' as a
72586599dbeSTom Zanussi  * parameter.  This corresponds to the string following the command
72686599dbeSTom Zanussi  * name minus the command name.  This function can be called by a
72786599dbeSTom Zanussi  * callback implementation for any command that requires a param; a
72886599dbeSTom Zanussi  * callback that doesn't require a param can ignore it.
72986599dbeSTom Zanussi  *
73086599dbeSTom Zanussi  * Return: true if this is an empty param, false otherwise
73186599dbeSTom Zanussi  */
event_trigger_empty_param(const char * param)73286599dbeSTom Zanussi bool event_trigger_empty_param(const char *param)
73386599dbeSTom Zanussi {
73486599dbeSTom Zanussi 	return !param;
73586599dbeSTom Zanussi }
73686599dbeSTom Zanussi 
73786599dbeSTom Zanussi /**
73886599dbeSTom Zanussi  * event_trigger_separate_filter - separate an event trigger from a filter
7398d4a21b5Ssunliming  * @param_and_filter: String containing trigger and possibly filter
7408d4a21b5Ssunliming  * @param: outparam, will be filled with a pointer to the trigger
74186599dbeSTom Zanussi  * @filter: outparam, will be filled with a pointer to the filter
74286599dbeSTom Zanussi  * @param_required: Specifies whether or not the param string is required
74386599dbeSTom Zanussi  *
74486599dbeSTom Zanussi  * Given a param string of the form '[trigger] [if filter]', this
74586599dbeSTom Zanussi  * function separates the filter from the trigger and returns the
7468d4a21b5Ssunliming  * trigger in @param and the filter in @filter.  Either the @param
7478d4a21b5Ssunliming  * or the @filter may be set to NULL by this function - if not set to
74886599dbeSTom Zanussi  * NULL, they will contain strings corresponding to the trigger and
74986599dbeSTom Zanussi  * filter.
75086599dbeSTom Zanussi  *
75186599dbeSTom Zanussi  * There are two cases that need to be handled with respect to the
75286599dbeSTom Zanussi  * passed-in param: either the param is required, or it is not
75386599dbeSTom Zanussi  * required.  If @param_required is set, and there's no param, it will
75486599dbeSTom Zanussi  * return -EINVAL.  If @param_required is not set and there's a param
75586599dbeSTom Zanussi  * that starts with a number, that corresponds to the case of a
75686599dbeSTom Zanussi  * trigger with :n (n = number of times the trigger should fire) and
75786599dbeSTom Zanussi  * the parsing continues normally; otherwise the function just returns
75886599dbeSTom Zanussi  * and assumes param just contains a filter and there's nothing else
75986599dbeSTom Zanussi  * to do.
76086599dbeSTom Zanussi  *
76186599dbeSTom Zanussi  * Return: 0 on success, errno otherwise
76286599dbeSTom Zanussi  */
event_trigger_separate_filter(char * param_and_filter,char ** param,char ** filter,bool param_required)76386599dbeSTom Zanussi int event_trigger_separate_filter(char *param_and_filter, char **param,
76486599dbeSTom Zanussi 				  char **filter, bool param_required)
76586599dbeSTom Zanussi {
76686599dbeSTom Zanussi 	int ret = 0;
76786599dbeSTom Zanussi 
76886599dbeSTom Zanussi 	*param = *filter = NULL;
76986599dbeSTom Zanussi 
77086599dbeSTom Zanussi 	if (!param_and_filter) {
77186599dbeSTom Zanussi 		if (param_required)
77286599dbeSTom Zanussi 			ret = -EINVAL;
77386599dbeSTom Zanussi 		goto out;
77486599dbeSTom Zanussi 	}
77586599dbeSTom Zanussi 
77686599dbeSTom Zanussi 	/*
77786599dbeSTom Zanussi 	 * Here we check for an optional param. The only legal
77886599dbeSTom Zanussi 	 * optional param is :n, and if that's the case, continue
77986599dbeSTom Zanussi 	 * below. Otherwise we assume what's left is a filter and
78086599dbeSTom Zanussi 	 * return it as the filter string for the caller to deal with.
78186599dbeSTom Zanussi 	 */
78286599dbeSTom Zanussi 	if (!param_required && param_and_filter && !isdigit(param_and_filter[0])) {
78386599dbeSTom Zanussi 		*filter = param_and_filter;
78486599dbeSTom Zanussi 		goto out;
78586599dbeSTom Zanussi 	}
78686599dbeSTom Zanussi 
78786599dbeSTom Zanussi 	/*
78886599dbeSTom Zanussi 	 * Separate the param from the filter (param [if filter]).
78986599dbeSTom Zanussi 	 * Here we have either an optional :n param or a required
79086599dbeSTom Zanussi 	 * param and an optional filter.
79186599dbeSTom Zanussi 	 */
79286599dbeSTom Zanussi 	*param = strsep(&param_and_filter, " \t");
79386599dbeSTom Zanussi 
79486599dbeSTom Zanussi 	/*
79586599dbeSTom Zanussi 	 * Here we have a filter, though it may be empty.
79686599dbeSTom Zanussi 	 */
79786599dbeSTom Zanussi 	if (param_and_filter) {
79886599dbeSTom Zanussi 		*filter = skip_spaces(param_and_filter);
79986599dbeSTom Zanussi 		if (!**filter)
80086599dbeSTom Zanussi 			*filter = NULL;
80186599dbeSTom Zanussi 	}
80286599dbeSTom Zanussi out:
80386599dbeSTom Zanussi 	return ret;
80486599dbeSTom Zanussi }
80586599dbeSTom Zanussi 
80686599dbeSTom Zanussi /**
80786599dbeSTom Zanussi  * event_trigger_alloc - allocate and init event_trigger_data for a trigger
80886599dbeSTom Zanussi  * @cmd_ops: The event_command operations for the trigger
80986599dbeSTom Zanussi  * @cmd: The cmd string
81086599dbeSTom Zanussi  * @param: The param string
81186599dbeSTom Zanussi  * @private_data: User data to associate with the event trigger
81286599dbeSTom Zanussi  *
81386599dbeSTom Zanussi  * Allocate an event_trigger_data instance and initialize it.  The
81486599dbeSTom Zanussi  * @cmd_ops are used along with the @cmd and @param to get the
81586599dbeSTom Zanussi  * trigger_ops to assign to the event_trigger_data.  @private_data can
81686599dbeSTom Zanussi  * also be passed in and associated with the event_trigger_data.
81786599dbeSTom Zanussi  *
81886599dbeSTom Zanussi  * Use event_trigger_free() to free an event_trigger_data object.
81986599dbeSTom Zanussi  *
82086599dbeSTom Zanussi  * Return: The trigger_data object success, NULL otherwise
82186599dbeSTom Zanussi  */
event_trigger_alloc(struct event_command * cmd_ops,char * cmd,char * param,void * private_data)82286599dbeSTom Zanussi struct event_trigger_data *event_trigger_alloc(struct event_command *cmd_ops,
82386599dbeSTom Zanussi 					       char *cmd,
82486599dbeSTom Zanussi 					       char *param,
82586599dbeSTom Zanussi 					       void *private_data)
82686599dbeSTom Zanussi {
82786599dbeSTom Zanussi 	struct event_trigger_data *trigger_data;
828502d2e71SChristophe JAILLET 	const struct event_trigger_ops *trigger_ops;
82986599dbeSTom Zanussi 
83086599dbeSTom Zanussi 	trigger_ops = cmd_ops->get_trigger_ops(cmd, param);
83186599dbeSTom Zanussi 
83286599dbeSTom Zanussi 	trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
83386599dbeSTom Zanussi 	if (!trigger_data)
83486599dbeSTom Zanussi 		return NULL;
83586599dbeSTom Zanussi 
83686599dbeSTom Zanussi 	trigger_data->count = -1;
83786599dbeSTom Zanussi 	trigger_data->ops = trigger_ops;
83886599dbeSTom Zanussi 	trigger_data->cmd_ops = cmd_ops;
83986599dbeSTom Zanussi 	trigger_data->private_data = private_data;
84086599dbeSTom Zanussi 
84186599dbeSTom Zanussi 	INIT_LIST_HEAD(&trigger_data->list);
84286599dbeSTom Zanussi 	INIT_LIST_HEAD(&trigger_data->named_list);
84386599dbeSTom Zanussi 	RCU_INIT_POINTER(trigger_data->filter, NULL);
84486599dbeSTom Zanussi 
84586599dbeSTom Zanussi 	return trigger_data;
84686599dbeSTom Zanussi }
84786599dbeSTom Zanussi 
84886599dbeSTom Zanussi /**
84986599dbeSTom Zanussi  * event_trigger_parse_num - parse and return the number param for a trigger
85086599dbeSTom Zanussi  * @param: The param string
85186599dbeSTom Zanussi  * @trigger_data: The trigger_data for the trigger
85286599dbeSTom Zanussi  *
85386599dbeSTom Zanussi  * Parse the :n (n = number of times the trigger should fire) param
85486599dbeSTom Zanussi  * and set the count variable in the trigger_data to the parsed count.
85586599dbeSTom Zanussi  *
85686599dbeSTom Zanussi  * Return: 0 on success, errno otherwise
85786599dbeSTom Zanussi  */
event_trigger_parse_num(char * param,struct event_trigger_data * trigger_data)85886599dbeSTom Zanussi int event_trigger_parse_num(char *param,
85986599dbeSTom Zanussi 			    struct event_trigger_data *trigger_data)
86086599dbeSTom Zanussi {
86186599dbeSTom Zanussi 	char *number;
86286599dbeSTom Zanussi 	int ret = 0;
86386599dbeSTom Zanussi 
86486599dbeSTom Zanussi 	if (param) {
86586599dbeSTom Zanussi 		number = strsep(&param, ":");
86686599dbeSTom Zanussi 
86786599dbeSTom Zanussi 		if (!strlen(number))
86886599dbeSTom Zanussi 			return -EINVAL;
86986599dbeSTom Zanussi 
87086599dbeSTom Zanussi 		/*
87186599dbeSTom Zanussi 		 * We use the callback data field (which is a pointer)
87286599dbeSTom Zanussi 		 * as our counter.
87386599dbeSTom Zanussi 		 */
87486599dbeSTom Zanussi 		ret = kstrtoul(number, 0, &trigger_data->count);
87586599dbeSTom Zanussi 	}
87686599dbeSTom Zanussi 
87786599dbeSTom Zanussi 	return ret;
87886599dbeSTom Zanussi }
87986599dbeSTom Zanussi 
88086599dbeSTom Zanussi /**
88186599dbeSTom Zanussi  * event_trigger_set_filter - set an event trigger's filter
88286599dbeSTom Zanussi  * @cmd_ops: The event_command operations for the trigger
88386599dbeSTom Zanussi  * @file: The event file for the trigger's event
88486599dbeSTom Zanussi  * @param: The string containing the filter
88586599dbeSTom Zanussi  * @trigger_data: The trigger_data for the trigger
88686599dbeSTom Zanussi  *
88786599dbeSTom Zanussi  * Set the filter for the trigger.  If the filter is NULL, just return
88886599dbeSTom Zanussi  * without error.
88986599dbeSTom Zanussi  *
89086599dbeSTom Zanussi  * Return: 0 on success, errno otherwise
89186599dbeSTom Zanussi  */
event_trigger_set_filter(struct event_command * cmd_ops,struct trace_event_file * file,char * param,struct event_trigger_data * trigger_data)89286599dbeSTom Zanussi int event_trigger_set_filter(struct event_command *cmd_ops,
89386599dbeSTom Zanussi 			     struct trace_event_file *file,
89486599dbeSTom Zanussi 			     char *param,
89586599dbeSTom Zanussi 			     struct event_trigger_data *trigger_data)
89686599dbeSTom Zanussi {
89786599dbeSTom Zanussi 	if (param && cmd_ops->set_filter)
89886599dbeSTom Zanussi 		return cmd_ops->set_filter(param, trigger_data, file);
89986599dbeSTom Zanussi 
90086599dbeSTom Zanussi 	return 0;
90186599dbeSTom Zanussi }
90286599dbeSTom Zanussi 
90386599dbeSTom Zanussi /**
90486599dbeSTom Zanussi  * event_trigger_reset_filter - reset an event trigger's filter
90586599dbeSTom Zanussi  * @cmd_ops: The event_command operations for the trigger
90686599dbeSTom Zanussi  * @trigger_data: The trigger_data for the trigger
90786599dbeSTom Zanussi  *
90886599dbeSTom Zanussi  * Reset the filter for the trigger to no filter.
90986599dbeSTom Zanussi  */
event_trigger_reset_filter(struct event_command * cmd_ops,struct event_trigger_data * trigger_data)91086599dbeSTom Zanussi void event_trigger_reset_filter(struct event_command *cmd_ops,
91186599dbeSTom Zanussi 				struct event_trigger_data *trigger_data)
91286599dbeSTom Zanussi {
91386599dbeSTom Zanussi 	if (cmd_ops->set_filter)
91486599dbeSTom Zanussi 		cmd_ops->set_filter(NULL, trigger_data, NULL);
91586599dbeSTom Zanussi }
91686599dbeSTom Zanussi 
91786599dbeSTom Zanussi /**
91886599dbeSTom Zanussi  * event_trigger_register - register an event trigger
91986599dbeSTom Zanussi  * @cmd_ops: The event_command operations for the trigger
92086599dbeSTom Zanussi  * @file: The event file for the trigger's event
92186599dbeSTom Zanussi  * @glob: The trigger command string, with optional remove(!) operator
92286599dbeSTom Zanussi  * @trigger_data: The trigger_data for the trigger
92386599dbeSTom Zanussi  *
92486599dbeSTom Zanussi  * Register an event trigger.  The @cmd_ops are used to call the
925b8cc44a4STom Zanussi  * cmd_ops->reg() function which actually does the registration.
92686599dbeSTom Zanussi  *
92786599dbeSTom Zanussi  * Return: 0 on success, errno otherwise
92886599dbeSTom Zanussi  */
event_trigger_register(struct event_command * cmd_ops,struct trace_event_file * file,char * glob,struct event_trigger_data * trigger_data)92986599dbeSTom Zanussi int event_trigger_register(struct event_command *cmd_ops,
93086599dbeSTom Zanussi 			   struct trace_event_file *file,
93186599dbeSTom Zanussi 			   char *glob,
932b8cc44a4STom Zanussi 			   struct event_trigger_data *trigger_data)
93386599dbeSTom Zanussi {
934b8cc44a4STom Zanussi 	return cmd_ops->reg(glob, trigger_data, file);
93586599dbeSTom Zanussi }
93686599dbeSTom Zanussi 
937b8cc44a4STom Zanussi /**
938b8cc44a4STom Zanussi  * event_trigger_unregister - unregister an event trigger
939b8cc44a4STom Zanussi  * @cmd_ops: The event_command operations for the trigger
940b8cc44a4STom Zanussi  * @file: The event file for the trigger's event
941b8cc44a4STom Zanussi  * @glob: The trigger command string, with optional remove(!) operator
942b8cc44a4STom Zanussi  * @trigger_data: The trigger_data for the trigger
943b8cc44a4STom Zanussi  *
944b8cc44a4STom Zanussi  * Unregister an event trigger.  The @cmd_ops are used to call the
945b8cc44a4STom Zanussi  * cmd_ops->unreg() function which actually does the unregistration.
946b8cc44a4STom Zanussi  */
event_trigger_unregister(struct event_command * cmd_ops,struct trace_event_file * file,char * glob,struct event_trigger_data * trigger_data)947b8cc44a4STom Zanussi void event_trigger_unregister(struct event_command *cmd_ops,
948b8cc44a4STom Zanussi 			      struct trace_event_file *file,
949b8cc44a4STom Zanussi 			      char *glob,
950b8cc44a4STom Zanussi 			      struct event_trigger_data *trigger_data)
951b8cc44a4STom Zanussi {
952b8cc44a4STom Zanussi 	cmd_ops->unreg(glob, trigger_data, file);
95386599dbeSTom Zanussi }
95486599dbeSTom Zanussi 
95586599dbeSTom Zanussi /*
95686599dbeSTom Zanussi  * End event trigger parsing helper functions.
95786599dbeSTom Zanussi  */
95886599dbeSTom Zanussi 
9592a2df321STom Zanussi /**
9609ec5a7d1STom Zanussi  * event_trigger_parse - Generic event_command @parse implementation
9612a2df321STom Zanussi  * @cmd_ops: The command ops, used for trigger registration
9627f1d2f82SSteven Rostedt (Red Hat)  * @file: The trace_event_file associated with the event
9632a2df321STom Zanussi  * @glob: The raw string used to register the trigger
9642a2df321STom Zanussi  * @cmd: The cmd portion of the string used to register the trigger
965e1f187d0STom Zanussi  * @param_and_filter: The param and filter portion of the string used to register the trigger
9662a2df321STom Zanussi  *
9672a2df321STom Zanussi  * Common implementation for event command parsing and trigger
9682a2df321STom Zanussi  * instantiation.
9692a2df321STom Zanussi  *
9709ec5a7d1STom Zanussi  * Usually used directly as the @parse method in event command
9712a2df321STom Zanussi  * implementations.
9722a2df321STom Zanussi  *
9732a2df321STom Zanussi  * Return: 0 on success, errno otherwise
9742a2df321STom Zanussi  */
9752a2df321STom Zanussi static int
event_trigger_parse(struct event_command * cmd_ops,struct trace_event_file * file,char * glob,char * cmd,char * param_and_filter)9769ec5a7d1STom Zanussi event_trigger_parse(struct event_command *cmd_ops,
9777f1d2f82SSteven Rostedt (Red Hat) 		    struct trace_event_file *file,
978e1f187d0STom Zanussi 		    char *glob, char *cmd, char *param_and_filter)
9792a2df321STom Zanussi {
9802a2df321STom Zanussi 	struct event_trigger_data *trigger_data;
981e1f187d0STom Zanussi 	char *param, *filter;
982e1f187d0STom Zanussi 	bool remove;
9832a2df321STom Zanussi 	int ret;
9842a2df321STom Zanussi 
985e1f187d0STom Zanussi 	remove = event_trigger_check_remove(glob);
9862a2df321STom Zanussi 
987e1f187d0STom Zanussi 	ret = event_trigger_separate_filter(param_and_filter, &param, &filter, false);
988e1f187d0STom Zanussi 	if (ret)
989e1f187d0STom Zanussi 		return ret;
9902a2df321STom Zanussi 
9912a2df321STom Zanussi 	ret = -ENOMEM;
992e1f187d0STom Zanussi 	trigger_data = event_trigger_alloc(cmd_ops, cmd, param, file);
9932a2df321STom Zanussi 	if (!trigger_data)
9942a2df321STom Zanussi 		goto out;
9952a2df321STom Zanussi 
996e1f187d0STom Zanussi 	if (remove) {
997b8cc44a4STom Zanussi 		event_trigger_unregister(cmd_ops, file, glob+1, trigger_data);
9982a2df321STom Zanussi 		kfree(trigger_data);
9992a2df321STom Zanussi 		ret = 0;
10002a2df321STom Zanussi 		goto out;
10012a2df321STom Zanussi 	}
10022a2df321STom Zanussi 
1003e1f187d0STom Zanussi 	ret = event_trigger_parse_num(param, trigger_data);
10042a2df321STom Zanussi 	if (ret)
10052a2df321STom Zanussi 		goto out_free;
10062a2df321STom Zanussi 
1007e1f187d0STom Zanussi 	ret = event_trigger_set_filter(cmd_ops, file, filter, trigger_data);
10082a2df321STom Zanussi 	if (ret < 0)
10092a2df321STom Zanussi 		goto out_free;
10102a2df321STom Zanussi 
10111863c387SSteven Rostedt (VMware) 	/* Up the trigger_data count to make sure reg doesn't free it on failure */
101247670541STom Zanussi 	event_trigger_init(trigger_data);
1013b8cc44a4STom Zanussi 
1014b8cc44a4STom Zanussi 	ret = event_trigger_register(cmd_ops, file, glob, trigger_data);
1015b8cc44a4STom Zanussi 	if (ret)
1016b8cc44a4STom Zanussi 		goto out_free;
10171863c387SSteven Rostedt (VMware) 
10181863c387SSteven Rostedt (VMware) 	/* Down the counter of trigger_data or free it if not used anymore */
101947670541STom Zanussi 	event_trigger_free(trigger_data);
10202a2df321STom Zanussi  out:
10212a2df321STom Zanussi 	return ret;
10222a2df321STom Zanussi 
10232a2df321STom Zanussi  out_free:
1024e1f187d0STom Zanussi 	event_trigger_reset_filter(cmd_ops, trigger_data);
10252a2df321STom Zanussi 	kfree(trigger_data);
10262a2df321STom Zanussi 	goto out;
10272a2df321STom Zanussi }
10282a2df321STom Zanussi 
1029bac5fb97STom Zanussi /**
1030bac5fb97STom Zanussi  * set_trigger_filter - Generic event_command @set_filter implementation
1031bac5fb97STom Zanussi  * @filter_str: The filter string for the trigger, NULL to remove filter
1032bac5fb97STom Zanussi  * @trigger_data: Trigger-specific data
10337f1d2f82SSteven Rostedt (Red Hat)  * @file: The trace_event_file associated with the event
1034bac5fb97STom Zanussi  *
1035bac5fb97STom Zanussi  * Common implementation for event command filter parsing and filter
1036bac5fb97STom Zanussi  * instantiation.
1037bac5fb97STom Zanussi  *
1038bac5fb97STom Zanussi  * Usually used directly as the @set_filter method in event command
1039bac5fb97STom Zanussi  * implementations.
1040bac5fb97STom Zanussi  *
1041bac5fb97STom Zanussi  * Also used to remove a filter (if filter_str = NULL).
1042bac5fb97STom Zanussi  *
1043bac5fb97STom Zanussi  * Return: 0 on success, errno otherwise
1044bac5fb97STom Zanussi  */
set_trigger_filter(char * filter_str,struct event_trigger_data * trigger_data,struct trace_event_file * file)1045ab4bf008STom Zanussi int set_trigger_filter(char *filter_str,
1046bac5fb97STom Zanussi 		       struct event_trigger_data *trigger_data,
10477f1d2f82SSteven Rostedt (Red Hat) 		       struct trace_event_file *file)
1048bac5fb97STom Zanussi {
1049bac5fb97STom Zanussi 	struct event_trigger_data *data = trigger_data;
1050bac5fb97STom Zanussi 	struct event_filter *filter = NULL, *tmp;
1051bac5fb97STom Zanussi 	int ret = -EINVAL;
1052bac5fb97STom Zanussi 	char *s;
1053bac5fb97STom Zanussi 
1054bac5fb97STom Zanussi 	if (!filter_str) /* clear the current filter */
1055bac5fb97STom Zanussi 		goto assign;
1056bac5fb97STom Zanussi 
1057bac5fb97STom Zanussi 	s = strsep(&filter_str, " \t");
1058bac5fb97STom Zanussi 
1059bac5fb97STom Zanussi 	if (!strlen(s) || strcmp(s, "if") != 0)
1060bac5fb97STom Zanussi 		goto out;
1061bac5fb97STom Zanussi 
1062bac5fb97STom Zanussi 	if (!filter_str)
1063bac5fb97STom Zanussi 		goto out;
1064bac5fb97STom Zanussi 
1065bac5fb97STom Zanussi 	/* The filter is for the 'trigger' event, not the triggered event */
10661e144d73SSteven Rostedt (VMware) 	ret = create_event_filter(file->tr, file->event_call,
1067a785736dSSteven Rostedt (Google) 				  filter_str, true, &filter);
1068a785736dSSteven Rostedt (Google) 
1069a785736dSSteven Rostedt (Google) 	/* Only enabled set_str for error handling */
1070a785736dSSteven Rostedt (Google) 	if (filter) {
1071a785736dSSteven Rostedt (Google) 		kfree(filter->filter_string);
1072a785736dSSteven Rostedt (Google) 		filter->filter_string = NULL;
1073a785736dSSteven Rostedt (Google) 	}
1074a785736dSSteven Rostedt (Google) 
10753cec638bSSteven Rostedt (VMware) 	/*
10763cec638bSSteven Rostedt (VMware) 	 * If create_event_filter() fails, filter still needs to be freed.
10773cec638bSSteven Rostedt (VMware) 	 * Which the calling code will do with data->filter.
10783cec638bSSteven Rostedt (VMware) 	 */
1079bac5fb97STom Zanussi  assign:
1080d8a30f20SSteven Rostedt (Red Hat) 	tmp = rcu_access_pointer(data->filter);
1081bac5fb97STom Zanussi 
1082bac5fb97STom Zanussi 	rcu_assign_pointer(data->filter, filter);
1083bac5fb97STom Zanussi 
1084bac5fb97STom Zanussi 	if (tmp) {
1085fb9f5ee9SSteven Rostedt (Google) 		/*
1086fb9f5ee9SSteven Rostedt (Google) 		 * Make sure the call is done with the filter.
1087fb9f5ee9SSteven Rostedt (Google) 		 * It is possible that a filter could fail at boot up,
1088fb9f5ee9SSteven Rostedt (Google) 		 * and then this path will be called. Avoid the synchronization
1089fb9f5ee9SSteven Rostedt (Google) 		 * in that case.
1090fb9f5ee9SSteven Rostedt (Google) 		 */
1091fb9f5ee9SSteven Rostedt (Google) 		if (system_state != SYSTEM_BOOTING)
1092e0a568dcSSteven Rostedt (VMware) 			tracepoint_synchronize_unregister();
1093bac5fb97STom Zanussi 		free_event_filter(tmp);
1094bac5fb97STom Zanussi 	}
1095bac5fb97STom Zanussi 
1096bac5fb97STom Zanussi 	kfree(data->filter_str);
1097bac5fb97STom Zanussi 	data->filter_str = NULL;
1098bac5fb97STom Zanussi 
1099bac5fb97STom Zanussi 	if (filter_str) {
1100bac5fb97STom Zanussi 		data->filter_str = kstrdup(filter_str, GFP_KERNEL);
1101bac5fb97STom Zanussi 		if (!data->filter_str) {
1102d8a30f20SSteven Rostedt (Red Hat) 			free_event_filter(rcu_access_pointer(data->filter));
1103bac5fb97STom Zanussi 			data->filter = NULL;
1104bac5fb97STom Zanussi 			ret = -ENOMEM;
1105bac5fb97STom Zanussi 		}
1106bac5fb97STom Zanussi 	}
1107bac5fb97STom Zanussi  out:
1108bac5fb97STom Zanussi 	return ret;
1109bac5fb97STom Zanussi }
1110bac5fb97STom Zanussi 
1111db1388b4STom Zanussi static LIST_HEAD(named_triggers);
1112db1388b4STom Zanussi 
1113db1388b4STom Zanussi /**
1114db1388b4STom Zanussi  * find_named_trigger - Find the common named trigger associated with @name
1115db1388b4STom Zanussi  * @name: The name of the set of named triggers to find the common data for
1116db1388b4STom Zanussi  *
1117db1388b4STom Zanussi  * Named triggers are sets of triggers that share a common set of
1118db1388b4STom Zanussi  * trigger data.  The first named trigger registered with a given name
1119db1388b4STom Zanussi  * owns the common trigger data that the others subsequently
1120db1388b4STom Zanussi  * registered with the same name will reference.  This function
1121db1388b4STom Zanussi  * returns the common trigger data associated with that first
1122db1388b4STom Zanussi  * registered instance.
1123db1388b4STom Zanussi  *
1124db1388b4STom Zanussi  * Return: the common trigger data for the given named trigger on
1125db1388b4STom Zanussi  * success, NULL otherwise.
1126db1388b4STom Zanussi  */
find_named_trigger(const char * name)1127db1388b4STom Zanussi struct event_trigger_data *find_named_trigger(const char *name)
1128db1388b4STom Zanussi {
1129db1388b4STom Zanussi 	struct event_trigger_data *data;
1130db1388b4STom Zanussi 
1131db1388b4STom Zanussi 	if (!name)
1132db1388b4STom Zanussi 		return NULL;
1133db1388b4STom Zanussi 
1134db1388b4STom Zanussi 	list_for_each_entry(data, &named_triggers, named_list) {
1135db1388b4STom Zanussi 		if (data->named_data)
1136db1388b4STom Zanussi 			continue;
1137db1388b4STom Zanussi 		if (strcmp(data->name, name) == 0)
1138db1388b4STom Zanussi 			return data;
1139db1388b4STom Zanussi 	}
1140db1388b4STom Zanussi 
1141db1388b4STom Zanussi 	return NULL;
1142db1388b4STom Zanussi }
1143db1388b4STom Zanussi 
1144db1388b4STom Zanussi /**
1145db1388b4STom Zanussi  * is_named_trigger - determine if a given trigger is a named trigger
1146db1388b4STom Zanussi  * @test: The trigger data to test
1147db1388b4STom Zanussi  *
1148db1388b4STom Zanussi  * Return: true if 'test' is a named trigger, false otherwise.
1149db1388b4STom Zanussi  */
is_named_trigger(struct event_trigger_data * test)1150db1388b4STom Zanussi bool is_named_trigger(struct event_trigger_data *test)
1151db1388b4STom Zanussi {
1152db1388b4STom Zanussi 	struct event_trigger_data *data;
1153db1388b4STom Zanussi 
1154db1388b4STom Zanussi 	list_for_each_entry(data, &named_triggers, named_list) {
1155db1388b4STom Zanussi 		if (test == data)
1156db1388b4STom Zanussi 			return true;
1157db1388b4STom Zanussi 	}
1158db1388b4STom Zanussi 
1159db1388b4STom Zanussi 	return false;
1160db1388b4STom Zanussi }
1161db1388b4STom Zanussi 
1162db1388b4STom Zanussi /**
1163db1388b4STom Zanussi  * save_named_trigger - save the trigger in the named trigger list
1164db1388b4STom Zanussi  * @name: The name of the named trigger set
1165db1388b4STom Zanussi  * @data: The trigger data to save
1166db1388b4STom Zanussi  *
1167db1388b4STom Zanussi  * Return: 0 if successful, negative error otherwise.
1168db1388b4STom Zanussi  */
save_named_trigger(const char * name,struct event_trigger_data * data)1169db1388b4STom Zanussi int save_named_trigger(const char *name, struct event_trigger_data *data)
1170db1388b4STom Zanussi {
1171db1388b4STom Zanussi 	data->name = kstrdup(name, GFP_KERNEL);
1172db1388b4STom Zanussi 	if (!data->name)
1173db1388b4STom Zanussi 		return -ENOMEM;
1174db1388b4STom Zanussi 
1175db1388b4STom Zanussi 	list_add(&data->named_list, &named_triggers);
1176db1388b4STom Zanussi 
1177db1388b4STom Zanussi 	return 0;
1178db1388b4STom Zanussi }
1179db1388b4STom Zanussi 
1180db1388b4STom Zanussi /**
1181db1388b4STom Zanussi  * del_named_trigger - delete a trigger from the named trigger list
1182db1388b4STom Zanussi  * @data: The trigger data to delete
1183db1388b4STom Zanussi  */
del_named_trigger(struct event_trigger_data * data)1184db1388b4STom Zanussi void del_named_trigger(struct event_trigger_data *data)
1185db1388b4STom Zanussi {
1186db1388b4STom Zanussi 	kfree(data->name);
1187db1388b4STom Zanussi 	data->name = NULL;
1188db1388b4STom Zanussi 
1189db1388b4STom Zanussi 	list_del(&data->named_list);
1190db1388b4STom Zanussi }
1191db1388b4STom Zanussi 
__pause_named_trigger(struct event_trigger_data * data,bool pause)1192db1388b4STom Zanussi static void __pause_named_trigger(struct event_trigger_data *data, bool pause)
1193db1388b4STom Zanussi {
1194db1388b4STom Zanussi 	struct event_trigger_data *test;
1195db1388b4STom Zanussi 
1196db1388b4STom Zanussi 	list_for_each_entry(test, &named_triggers, named_list) {
1197db1388b4STom Zanussi 		if (strcmp(test->name, data->name) == 0) {
1198db1388b4STom Zanussi 			if (pause) {
1199db1388b4STom Zanussi 				test->paused_tmp = test->paused;
1200db1388b4STom Zanussi 				test->paused = true;
1201db1388b4STom Zanussi 			} else {
1202db1388b4STom Zanussi 				test->paused = test->paused_tmp;
1203db1388b4STom Zanussi 			}
1204db1388b4STom Zanussi 		}
1205db1388b4STom Zanussi 	}
1206db1388b4STom Zanussi }
1207db1388b4STom Zanussi 
1208db1388b4STom Zanussi /**
1209db1388b4STom Zanussi  * pause_named_trigger - Pause all named triggers with the same name
1210db1388b4STom Zanussi  * @data: The trigger data of a named trigger to pause
1211db1388b4STom Zanussi  *
1212db1388b4STom Zanussi  * Pauses a named trigger along with all other triggers having the
1213db1388b4STom Zanussi  * same name.  Because named triggers share a common set of data,
1214db1388b4STom Zanussi  * pausing only one is meaningless, so pausing one named trigger needs
1215db1388b4STom Zanussi  * to pause all triggers with the same name.
1216db1388b4STom Zanussi  */
pause_named_trigger(struct event_trigger_data * data)1217db1388b4STom Zanussi void pause_named_trigger(struct event_trigger_data *data)
1218db1388b4STom Zanussi {
1219db1388b4STom Zanussi 	__pause_named_trigger(data, true);
1220db1388b4STom Zanussi }
1221db1388b4STom Zanussi 
1222db1388b4STom Zanussi /**
1223db1388b4STom Zanussi  * unpause_named_trigger - Un-pause all named triggers with the same name
1224db1388b4STom Zanussi  * @data: The trigger data of a named trigger to unpause
1225db1388b4STom Zanussi  *
1226db1388b4STom Zanussi  * Un-pauses a named trigger along with all other triggers having the
1227db1388b4STom Zanussi  * same name.  Because named triggers share a common set of data,
1228db1388b4STom Zanussi  * unpausing only one is meaningless, so unpausing one named trigger
1229db1388b4STom Zanussi  * needs to unpause all triggers with the same name.
1230db1388b4STom Zanussi  */
unpause_named_trigger(struct event_trigger_data * data)1231db1388b4STom Zanussi void unpause_named_trigger(struct event_trigger_data *data)
1232db1388b4STom Zanussi {
1233db1388b4STom Zanussi 	__pause_named_trigger(data, false);
1234db1388b4STom Zanussi }
1235db1388b4STom Zanussi 
1236db1388b4STom Zanussi /**
1237db1388b4STom Zanussi  * set_named_trigger_data - Associate common named trigger data
1238099dcc18SQiujun Huang  * @data: The trigger data to associate
1239099dcc18SQiujun Huang  * @named_data: The common named trigger to be associated
1240db1388b4STom Zanussi  *
1241db1388b4STom Zanussi  * Named triggers are sets of triggers that share a common set of
1242db1388b4STom Zanussi  * trigger data.  The first named trigger registered with a given name
1243db1388b4STom Zanussi  * owns the common trigger data that the others subsequently
1244db1388b4STom Zanussi  * registered with the same name will reference.  This function
1245db1388b4STom Zanussi  * associates the common trigger data from the first trigger with the
1246db1388b4STom Zanussi  * given trigger.
1247db1388b4STom Zanussi  */
set_named_trigger_data(struct event_trigger_data * data,struct event_trigger_data * named_data)1248db1388b4STom Zanussi void set_named_trigger_data(struct event_trigger_data *data,
1249db1388b4STom Zanussi 			    struct event_trigger_data *named_data)
1250db1388b4STom Zanussi {
1251db1388b4STom Zanussi 	data->named_data = named_data;
1252db1388b4STom Zanussi }
1253db1388b4STom Zanussi 
1254067fe038STom Zanussi struct event_trigger_data *
get_named_trigger_data(struct event_trigger_data * data)1255067fe038STom Zanussi get_named_trigger_data(struct event_trigger_data *data)
1256067fe038STom Zanussi {
1257067fe038STom Zanussi 	return data->named_data;
1258067fe038STom Zanussi }
1259067fe038STom Zanussi 
12602a2df321STom Zanussi static void
traceon_trigger(struct event_trigger_data * data,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * event)1261b47e3302SSteven Rostedt (VMware) traceon_trigger(struct event_trigger_data *data,
1262b47e3302SSteven Rostedt (VMware) 		struct trace_buffer *buffer, void *rec,
12631ac4f51cSTom Zanussi 		struct ring_buffer_event *event)
12642a2df321STom Zanussi {
1265302e9eddSSteven Rostedt (Google) 	struct trace_event_file *file = data->private_data;
1266302e9eddSSteven Rostedt (Google) 
1267302e9eddSSteven Rostedt (Google) 	if (file) {
1268302e9eddSSteven Rostedt (Google) 		if (tracer_tracing_is_on(file->tr))
1269302e9eddSSteven Rostedt (Google) 			return;
1270302e9eddSSteven Rostedt (Google) 
1271302e9eddSSteven Rostedt (Google) 		tracer_tracing_on(file->tr);
1272302e9eddSSteven Rostedt (Google) 		return;
1273302e9eddSSteven Rostedt (Google) 	}
1274302e9eddSSteven Rostedt (Google) 
12752a2df321STom Zanussi 	if (tracing_is_on())
12762a2df321STom Zanussi 		return;
12772a2df321STom Zanussi 
12782a2df321STom Zanussi 	tracing_on();
12792a2df321STom Zanussi }
12802a2df321STom Zanussi 
12812a2df321STom Zanussi static void
traceon_count_trigger(struct event_trigger_data * data,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * event)1282b47e3302SSteven Rostedt (VMware) traceon_count_trigger(struct event_trigger_data *data,
1283b47e3302SSteven Rostedt (VMware) 		      struct trace_buffer *buffer, void *rec,
12841ac4f51cSTom Zanussi 		      struct ring_buffer_event *event)
12852a2df321STom Zanussi {
1286302e9eddSSteven Rostedt (Google) 	struct trace_event_file *file = data->private_data;
1287302e9eddSSteven Rostedt (Google) 
1288302e9eddSSteven Rostedt (Google) 	if (file) {
1289302e9eddSSteven Rostedt (Google) 		if (tracer_tracing_is_on(file->tr))
1290302e9eddSSteven Rostedt (Google) 			return;
1291302e9eddSSteven Rostedt (Google) 	} else {
1292e8dc6371SSteven Rostedt (Red Hat) 		if (tracing_is_on())
1293e8dc6371SSteven Rostedt (Red Hat) 			return;
1294302e9eddSSteven Rostedt (Google) 	}
1295e8dc6371SSteven Rostedt (Red Hat) 
12962a2df321STom Zanussi 	if (!data->count)
12972a2df321STom Zanussi 		return;
12982a2df321STom Zanussi 
12992a2df321STom Zanussi 	if (data->count != -1)
13002a2df321STom Zanussi 		(data->count)--;
13012a2df321STom Zanussi 
1302302e9eddSSteven Rostedt (Google) 	if (file)
1303302e9eddSSteven Rostedt (Google) 		tracer_tracing_on(file->tr);
1304302e9eddSSteven Rostedt (Google) 	else
1305e8dc6371SSteven Rostedt (Red Hat) 		tracing_on();
13062a2df321STom Zanussi }
13072a2df321STom Zanussi 
13082a2df321STom Zanussi static void
traceoff_trigger(struct event_trigger_data * data,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * event)1309b47e3302SSteven Rostedt (VMware) traceoff_trigger(struct event_trigger_data *data,
1310b47e3302SSteven Rostedt (VMware) 		 struct trace_buffer *buffer, void *rec,
13111ac4f51cSTom Zanussi 		 struct ring_buffer_event *event)
13122a2df321STom Zanussi {
1313302e9eddSSteven Rostedt (Google) 	struct trace_event_file *file = data->private_data;
1314302e9eddSSteven Rostedt (Google) 
1315302e9eddSSteven Rostedt (Google) 	if (file) {
1316302e9eddSSteven Rostedt (Google) 		if (!tracer_tracing_is_on(file->tr))
1317302e9eddSSteven Rostedt (Google) 			return;
1318302e9eddSSteven Rostedt (Google) 
1319302e9eddSSteven Rostedt (Google) 		tracer_tracing_off(file->tr);
1320302e9eddSSteven Rostedt (Google) 		return;
1321302e9eddSSteven Rostedt (Google) 	}
1322302e9eddSSteven Rostedt (Google) 
13232a2df321STom Zanussi 	if (!tracing_is_on())
13242a2df321STom Zanussi 		return;
13252a2df321STom Zanussi 
13262a2df321STom Zanussi 	tracing_off();
13272a2df321STom Zanussi }
13282a2df321STom Zanussi 
13292a2df321STom Zanussi static void
traceoff_count_trigger(struct event_trigger_data * data,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * event)1330b47e3302SSteven Rostedt (VMware) traceoff_count_trigger(struct event_trigger_data *data,
1331b47e3302SSteven Rostedt (VMware) 		       struct trace_buffer *buffer, void *rec,
13321ac4f51cSTom Zanussi 		       struct ring_buffer_event *event)
13332a2df321STom Zanussi {
1334302e9eddSSteven Rostedt (Google) 	struct trace_event_file *file = data->private_data;
1335302e9eddSSteven Rostedt (Google) 
1336302e9eddSSteven Rostedt (Google) 	if (file) {
1337302e9eddSSteven Rostedt (Google) 		if (!tracer_tracing_is_on(file->tr))
1338302e9eddSSteven Rostedt (Google) 			return;
1339302e9eddSSteven Rostedt (Google) 	} else {
1340e8dc6371SSteven Rostedt (Red Hat) 		if (!tracing_is_on())
1341e8dc6371SSteven Rostedt (Red Hat) 			return;
1342302e9eddSSteven Rostedt (Google) 	}
1343e8dc6371SSteven Rostedt (Red Hat) 
13442a2df321STom Zanussi 	if (!data->count)
13452a2df321STom Zanussi 		return;
13462a2df321STom Zanussi 
13472a2df321STom Zanussi 	if (data->count != -1)
13482a2df321STom Zanussi 		(data->count)--;
13492a2df321STom Zanussi 
1350302e9eddSSteven Rostedt (Google) 	if (file)
1351302e9eddSSteven Rostedt (Google) 		tracer_tracing_off(file->tr);
1352302e9eddSSteven Rostedt (Google) 	else
1353e8dc6371SSteven Rostedt (Red Hat) 		tracing_off();
13542a2df321STom Zanussi }
13552a2df321STom Zanussi 
13562a2df321STom Zanussi static int
traceon_trigger_print(struct seq_file * m,struct event_trigger_data * data)135747670541STom Zanussi traceon_trigger_print(struct seq_file *m, struct event_trigger_data *data)
13582a2df321STom Zanussi {
13592a2df321STom Zanussi 	return event_trigger_print("traceon", m, (void *)data->count,
13602a2df321STom Zanussi 				   data->filter_str);
13612a2df321STom Zanussi }
13622a2df321STom Zanussi 
13632a2df321STom Zanussi static int
traceoff_trigger_print(struct seq_file * m,struct event_trigger_data * data)136447670541STom Zanussi traceoff_trigger_print(struct seq_file *m, struct event_trigger_data *data)
13652a2df321STom Zanussi {
13662a2df321STom Zanussi 	return event_trigger_print("traceoff", m, (void *)data->count,
13672a2df321STom Zanussi 				   data->filter_str);
13682a2df321STom Zanussi }
13692a2df321STom Zanussi 
1370502d2e71SChristophe JAILLET static const struct event_trigger_ops traceon_trigger_ops = {
1371fb339e53STom Zanussi 	.trigger		= traceon_trigger,
13722a2df321STom Zanussi 	.print			= traceon_trigger_print,
13732a2df321STom Zanussi 	.init			= event_trigger_init,
13742a2df321STom Zanussi 	.free			= event_trigger_free,
13752a2df321STom Zanussi };
13762a2df321STom Zanussi 
1377502d2e71SChristophe JAILLET static const struct event_trigger_ops traceon_count_trigger_ops = {
1378fb339e53STom Zanussi 	.trigger		= traceon_count_trigger,
13792a2df321STom Zanussi 	.print			= traceon_trigger_print,
13802a2df321STom Zanussi 	.init			= event_trigger_init,
13812a2df321STom Zanussi 	.free			= event_trigger_free,
13822a2df321STom Zanussi };
13832a2df321STom Zanussi 
1384502d2e71SChristophe JAILLET static const struct event_trigger_ops traceoff_trigger_ops = {
1385fb339e53STom Zanussi 	.trigger		= traceoff_trigger,
13862a2df321STom Zanussi 	.print			= traceoff_trigger_print,
13872a2df321STom Zanussi 	.init			= event_trigger_init,
13882a2df321STom Zanussi 	.free			= event_trigger_free,
13892a2df321STom Zanussi };
13902a2df321STom Zanussi 
1391502d2e71SChristophe JAILLET static const struct event_trigger_ops traceoff_count_trigger_ops = {
1392fb339e53STom Zanussi 	.trigger		= traceoff_count_trigger,
13932a2df321STom Zanussi 	.print			= traceoff_trigger_print,
13942a2df321STom Zanussi 	.init			= event_trigger_init,
13952a2df321STom Zanussi 	.free			= event_trigger_free,
13962a2df321STom Zanussi };
13972a2df321STom Zanussi 
1398502d2e71SChristophe JAILLET static const struct event_trigger_ops *
onoff_get_trigger_ops(char * cmd,char * param)13992a2df321STom Zanussi onoff_get_trigger_ops(char *cmd, char *param)
14002a2df321STom Zanussi {
1401502d2e71SChristophe JAILLET 	const struct event_trigger_ops *ops;
14022a2df321STom Zanussi 
14032a2df321STom Zanussi 	/* we register both traceon and traceoff to this callback */
14042a2df321STom Zanussi 	if (strcmp(cmd, "traceon") == 0)
14052a2df321STom Zanussi 		ops = param ? &traceon_count_trigger_ops :
14062a2df321STom Zanussi 			&traceon_trigger_ops;
14072a2df321STom Zanussi 	else
14082a2df321STom Zanussi 		ops = param ? &traceoff_count_trigger_ops :
14092a2df321STom Zanussi 			&traceoff_trigger_ops;
14102a2df321STom Zanussi 
14112a2df321STom Zanussi 	return ops;
14122a2df321STom Zanussi }
14132a2df321STom Zanussi 
14142a2df321STom Zanussi static struct event_command trigger_traceon_cmd = {
14152a2df321STom Zanussi 	.name			= "traceon",
14162a2df321STom Zanussi 	.trigger_type		= ETT_TRACE_ONOFF,
14179ec5a7d1STom Zanussi 	.parse			= event_trigger_parse,
14182a2df321STom Zanussi 	.reg			= register_trigger,
14192a2df321STom Zanussi 	.unreg			= unregister_trigger,
14202a2df321STom Zanussi 	.get_trigger_ops	= onoff_get_trigger_ops,
1421bac5fb97STom Zanussi 	.set_filter		= set_trigger_filter,
14222a2df321STom Zanussi };
14232a2df321STom Zanussi 
14242a2df321STom Zanussi static struct event_command trigger_traceoff_cmd = {
14252a2df321STom Zanussi 	.name			= "traceoff",
14262a2df321STom Zanussi 	.trigger_type		= ETT_TRACE_ONOFF,
1427a0d0c621SMasami Hiramatsu 	.flags			= EVENT_CMD_FL_POST_TRIGGER,
14289ec5a7d1STom Zanussi 	.parse			= event_trigger_parse,
14292a2df321STom Zanussi 	.reg			= register_trigger,
14302a2df321STom Zanussi 	.unreg			= unregister_trigger,
14312a2df321STom Zanussi 	.get_trigger_ops	= onoff_get_trigger_ops,
1432bac5fb97STom Zanussi 	.set_filter		= set_trigger_filter,
14332a2df321STom Zanussi };
14342a2df321STom Zanussi 
143593e31ffbSTom Zanussi #ifdef CONFIG_TRACER_SNAPSHOT
143693e31ffbSTom Zanussi static void
snapshot_trigger(struct event_trigger_data * data,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * event)1437b47e3302SSteven Rostedt (VMware) snapshot_trigger(struct event_trigger_data *data,
1438b47e3302SSteven Rostedt (VMware) 		 struct trace_buffer *buffer, void *rec,
14391ac4f51cSTom Zanussi 		 struct ring_buffer_event *event)
144093e31ffbSTom Zanussi {
14412824f503SSteven Rostedt (VMware) 	struct trace_event_file *file = data->private_data;
14422824f503SSteven Rostedt (VMware) 
14432824f503SSteven Rostedt (VMware) 	if (file)
14442824f503SSteven Rostedt (VMware) 		tracing_snapshot_instance(file->tr);
14452824f503SSteven Rostedt (VMware) 	else
144693e31ffbSTom Zanussi 		tracing_snapshot();
144793e31ffbSTom Zanussi }
144893e31ffbSTom Zanussi 
144993e31ffbSTom Zanussi static void
snapshot_count_trigger(struct event_trigger_data * data,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * event)1450b47e3302SSteven Rostedt (VMware) snapshot_count_trigger(struct event_trigger_data *data,
1451b47e3302SSteven Rostedt (VMware) 		       struct trace_buffer *buffer, void *rec,
14521ac4f51cSTom Zanussi 		       struct ring_buffer_event *event)
145393e31ffbSTom Zanussi {
145493e31ffbSTom Zanussi 	if (!data->count)
145593e31ffbSTom Zanussi 		return;
145693e31ffbSTom Zanussi 
145793e31ffbSTom Zanussi 	if (data->count != -1)
145893e31ffbSTom Zanussi 		(data->count)--;
145993e31ffbSTom Zanussi 
1460b47e3302SSteven Rostedt (VMware) 	snapshot_trigger(data, buffer, rec, event);
146193e31ffbSTom Zanussi }
146293e31ffbSTom Zanussi 
146393e31ffbSTom Zanussi static int
register_snapshot_trigger(char * glob,struct event_trigger_data * data,struct trace_event_file * file)14642378a2d6STom Zanussi register_snapshot_trigger(char *glob,
146593e31ffbSTom Zanussi 			  struct event_trigger_data *data,
14667f1d2f82SSteven Rostedt (Red Hat) 			  struct trace_event_file *file)
146793e31ffbSTom Zanussi {
1468180e4e39SVincent Donnefort 	int ret = tracing_arm_snapshot(file->tr);
14690958b33eSMasami Hiramatsu (Google) 
14700958b33eSMasami Hiramatsu (Google) 	if (ret < 0)
14710958b33eSMasami Hiramatsu (Google) 		return ret;
147293e31ffbSTom Zanussi 
14732048fdc2SSteven Rostedt (Google) 	ret = register_trigger(glob, data, file);
14742048fdc2SSteven Rostedt (Google) 	if (ret < 0)
14752048fdc2SSteven Rostedt (Google) 		tracing_disarm_snapshot(file->tr);
14762048fdc2SSteven Rostedt (Google) 	return ret;
147793e31ffbSTom Zanussi }
147893e31ffbSTom Zanussi 
unregister_snapshot_trigger(char * glob,struct event_trigger_data * data,struct trace_event_file * file)1479180e4e39SVincent Donnefort static void unregister_snapshot_trigger(char *glob,
1480180e4e39SVincent Donnefort 					struct event_trigger_data *data,
1481180e4e39SVincent Donnefort 					struct trace_event_file *file)
1482180e4e39SVincent Donnefort {
1483180e4e39SVincent Donnefort 	if (try_unregister_trigger(glob, data, file))
1484180e4e39SVincent Donnefort 		tracing_disarm_snapshot(file->tr);
1485180e4e39SVincent Donnefort }
1486180e4e39SVincent Donnefort 
148793e31ffbSTom Zanussi static int
snapshot_trigger_print(struct seq_file * m,struct event_trigger_data * data)148847670541STom Zanussi snapshot_trigger_print(struct seq_file *m, struct event_trigger_data *data)
148993e31ffbSTom Zanussi {
149093e31ffbSTom Zanussi 	return event_trigger_print("snapshot", m, (void *)data->count,
149193e31ffbSTom Zanussi 				   data->filter_str);
149293e31ffbSTom Zanussi }
149393e31ffbSTom Zanussi 
1494502d2e71SChristophe JAILLET static const struct event_trigger_ops snapshot_trigger_ops = {
1495fb339e53STom Zanussi 	.trigger		= snapshot_trigger,
149693e31ffbSTom Zanussi 	.print			= snapshot_trigger_print,
149793e31ffbSTom Zanussi 	.init			= event_trigger_init,
149893e31ffbSTom Zanussi 	.free			= event_trigger_free,
149993e31ffbSTom Zanussi };
150093e31ffbSTom Zanussi 
1501502d2e71SChristophe JAILLET static const struct event_trigger_ops snapshot_count_trigger_ops = {
1502fb339e53STom Zanussi 	.trigger		= snapshot_count_trigger,
150393e31ffbSTom Zanussi 	.print			= snapshot_trigger_print,
150493e31ffbSTom Zanussi 	.init			= event_trigger_init,
150593e31ffbSTom Zanussi 	.free			= event_trigger_free,
150693e31ffbSTom Zanussi };
150793e31ffbSTom Zanussi 
1508502d2e71SChristophe JAILLET static const struct event_trigger_ops *
snapshot_get_trigger_ops(char * cmd,char * param)150993e31ffbSTom Zanussi snapshot_get_trigger_ops(char *cmd, char *param)
151093e31ffbSTom Zanussi {
151193e31ffbSTom Zanussi 	return param ? &snapshot_count_trigger_ops : &snapshot_trigger_ops;
151293e31ffbSTom Zanussi }
151393e31ffbSTom Zanussi 
151493e31ffbSTom Zanussi static struct event_command trigger_snapshot_cmd = {
151593e31ffbSTom Zanussi 	.name			= "snapshot",
151693e31ffbSTom Zanussi 	.trigger_type		= ETT_SNAPSHOT,
15179ec5a7d1STom Zanussi 	.parse			= event_trigger_parse,
151893e31ffbSTom Zanussi 	.reg			= register_snapshot_trigger,
1519180e4e39SVincent Donnefort 	.unreg			= unregister_snapshot_trigger,
152093e31ffbSTom Zanussi 	.get_trigger_ops	= snapshot_get_trigger_ops,
1521bac5fb97STom Zanussi 	.set_filter		= set_trigger_filter,
152293e31ffbSTom Zanussi };
152393e31ffbSTom Zanussi 
register_trigger_snapshot_cmd(void)152493e31ffbSTom Zanussi static __init int register_trigger_snapshot_cmd(void)
152593e31ffbSTom Zanussi {
152693e31ffbSTom Zanussi 	int ret;
152793e31ffbSTom Zanussi 
152893e31ffbSTom Zanussi 	ret = register_event_command(&trigger_snapshot_cmd);
152993e31ffbSTom Zanussi 	WARN_ON(ret < 0);
153093e31ffbSTom Zanussi 
153193e31ffbSTom Zanussi 	return ret;
153293e31ffbSTom Zanussi }
153393e31ffbSTom Zanussi #else
register_trigger_snapshot_cmd(void)153493e31ffbSTom Zanussi static __init int register_trigger_snapshot_cmd(void) { return 0; }
153593e31ffbSTom Zanussi #endif /* CONFIG_TRACER_SNAPSHOT */
153693e31ffbSTom Zanussi 
1537f21ecbb3STom Zanussi #ifdef CONFIG_STACKTRACE
15382ee5b92aSSteven Rostedt (VMware) #ifdef CONFIG_UNWINDER_ORC
15392ee5b92aSSteven Rostedt (VMware) /* Skip 2:
1540f21ecbb3STom Zanussi  *   event_triggers_post_call()
1541a7237765SSteven Rostedt (Red Hat)  *   trace_event_raw_event_xxx()
1542f21ecbb3STom Zanussi  */
15432ee5b92aSSteven Rostedt (VMware) # define STACK_SKIP 2
15442ee5b92aSSteven Rostedt (VMware) #else
15452ee5b92aSSteven Rostedt (VMware) /*
15462ee5b92aSSteven Rostedt (VMware)  * Skip 4:
15472ee5b92aSSteven Rostedt (VMware)  *   stacktrace_trigger()
15482ee5b92aSSteven Rostedt (VMware)  *   event_triggers_post_call()
15492ee5b92aSSteven Rostedt (VMware)  *   trace_event_buffer_commit()
15502ee5b92aSSteven Rostedt (VMware)  *   trace_event_raw_event_xxx()
15512ee5b92aSSteven Rostedt (VMware)  */
15522ee5b92aSSteven Rostedt (VMware) #define STACK_SKIP 4
15532ee5b92aSSteven Rostedt (VMware) #endif
1554f21ecbb3STom Zanussi 
1555f21ecbb3STom Zanussi static void
stacktrace_trigger(struct event_trigger_data * data,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * event)1556b47e3302SSteven Rostedt (VMware) stacktrace_trigger(struct event_trigger_data *data,
1557b47e3302SSteven Rostedt (VMware) 		   struct trace_buffer *buffer,  void *rec,
15581ac4f51cSTom Zanussi 		   struct ring_buffer_event *event)
1559f21ecbb3STom Zanussi {
1560ce33c845SDaniel Bristot de Oliveira 	struct trace_event_file *file = data->private_data;
1561ce33c845SDaniel Bristot de Oliveira 
1562ce33c845SDaniel Bristot de Oliveira 	if (file)
1563*e3333326Spengdonglin 		__trace_stack(file->tr, tracing_gen_ctx_dec(), STACK_SKIP);
1564ce33c845SDaniel Bristot de Oliveira 	else
1565f21ecbb3STom Zanussi 		trace_dump_stack(STACK_SKIP);
1566f21ecbb3STom Zanussi }
1567f21ecbb3STom Zanussi 
1568f21ecbb3STom Zanussi static void
stacktrace_count_trigger(struct event_trigger_data * data,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * event)1569b47e3302SSteven Rostedt (VMware) stacktrace_count_trigger(struct event_trigger_data *data,
1570b47e3302SSteven Rostedt (VMware) 			 struct trace_buffer *buffer, void *rec,
15711ac4f51cSTom Zanussi 			 struct ring_buffer_event *event)
1572f21ecbb3STom Zanussi {
1573f21ecbb3STom Zanussi 	if (!data->count)
1574f21ecbb3STom Zanussi 		return;
1575f21ecbb3STom Zanussi 
1576f21ecbb3STom Zanussi 	if (data->count != -1)
1577f21ecbb3STom Zanussi 		(data->count)--;
1578f21ecbb3STom Zanussi 
1579b47e3302SSteven Rostedt (VMware) 	stacktrace_trigger(data, buffer, rec, event);
1580f21ecbb3STom Zanussi }
1581f21ecbb3STom Zanussi 
1582f21ecbb3STom Zanussi static int
stacktrace_trigger_print(struct seq_file * m,struct event_trigger_data * data)158347670541STom Zanussi stacktrace_trigger_print(struct seq_file *m, struct event_trigger_data *data)
1584f21ecbb3STom Zanussi {
1585f21ecbb3STom Zanussi 	return event_trigger_print("stacktrace", m, (void *)data->count,
1586f21ecbb3STom Zanussi 				   data->filter_str);
1587f21ecbb3STom Zanussi }
1588f21ecbb3STom Zanussi 
1589502d2e71SChristophe JAILLET static const struct event_trigger_ops stacktrace_trigger_ops = {
1590fb339e53STom Zanussi 	.trigger		= stacktrace_trigger,
1591f21ecbb3STom Zanussi 	.print			= stacktrace_trigger_print,
1592f21ecbb3STom Zanussi 	.init			= event_trigger_init,
1593f21ecbb3STom Zanussi 	.free			= event_trigger_free,
1594f21ecbb3STom Zanussi };
1595f21ecbb3STom Zanussi 
1596502d2e71SChristophe JAILLET static const struct event_trigger_ops stacktrace_count_trigger_ops = {
1597fb339e53STom Zanussi 	.trigger		= stacktrace_count_trigger,
1598f21ecbb3STom Zanussi 	.print			= stacktrace_trigger_print,
1599f21ecbb3STom Zanussi 	.init			= event_trigger_init,
1600f21ecbb3STom Zanussi 	.free			= event_trigger_free,
1601f21ecbb3STom Zanussi };
1602f21ecbb3STom Zanussi 
1603502d2e71SChristophe JAILLET static const struct event_trigger_ops *
stacktrace_get_trigger_ops(char * cmd,char * param)1604f21ecbb3STom Zanussi stacktrace_get_trigger_ops(char *cmd, char *param)
1605f21ecbb3STom Zanussi {
1606f21ecbb3STom Zanussi 	return param ? &stacktrace_count_trigger_ops : &stacktrace_trigger_ops;
1607f21ecbb3STom Zanussi }
1608f21ecbb3STom Zanussi 
1609f21ecbb3STom Zanussi static struct event_command trigger_stacktrace_cmd = {
1610f21ecbb3STom Zanussi 	.name			= "stacktrace",
1611f21ecbb3STom Zanussi 	.trigger_type		= ETT_STACKTRACE,
1612353206f5SSteven Rostedt (Red Hat) 	.flags			= EVENT_CMD_FL_POST_TRIGGER,
16139ec5a7d1STom Zanussi 	.parse			= event_trigger_parse,
1614f21ecbb3STom Zanussi 	.reg			= register_trigger,
1615f21ecbb3STom Zanussi 	.unreg			= unregister_trigger,
1616f21ecbb3STom Zanussi 	.get_trigger_ops	= stacktrace_get_trigger_ops,
1617bac5fb97STom Zanussi 	.set_filter		= set_trigger_filter,
1618f21ecbb3STom Zanussi };
1619f21ecbb3STom Zanussi 
register_trigger_stacktrace_cmd(void)1620f21ecbb3STom Zanussi static __init int register_trigger_stacktrace_cmd(void)
1621f21ecbb3STom Zanussi {
1622f21ecbb3STom Zanussi 	int ret;
1623f21ecbb3STom Zanussi 
1624f21ecbb3STom Zanussi 	ret = register_event_command(&trigger_stacktrace_cmd);
1625f21ecbb3STom Zanussi 	WARN_ON(ret < 0);
1626f21ecbb3STom Zanussi 
1627f21ecbb3STom Zanussi 	return ret;
1628f21ecbb3STom Zanussi }
1629f21ecbb3STom Zanussi #else
register_trigger_stacktrace_cmd(void)1630f21ecbb3STom Zanussi static __init int register_trigger_stacktrace_cmd(void) { return 0; }
1631f21ecbb3STom Zanussi #endif /* CONFIG_STACKTRACE */
1632f21ecbb3STom Zanussi 
unregister_trigger_traceon_traceoff_cmds(void)16332a2df321STom Zanussi static __init void unregister_trigger_traceon_traceoff_cmds(void)
16342a2df321STom Zanussi {
16352a2df321STom Zanussi 	unregister_event_command(&trigger_traceon_cmd);
16362a2df321STom Zanussi 	unregister_event_command(&trigger_traceoff_cmd);
16372a2df321STom Zanussi }
16382a2df321STom Zanussi 
16397862ad18STom Zanussi static void
event_enable_trigger(struct event_trigger_data * data,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * event)1640b47e3302SSteven Rostedt (VMware) event_enable_trigger(struct event_trigger_data *data,
1641b47e3302SSteven Rostedt (VMware) 		     struct trace_buffer *buffer,  void *rec,
16421ac4f51cSTom Zanussi 		     struct ring_buffer_event *event)
16437862ad18STom Zanussi {
16447862ad18STom Zanussi 	struct enable_trigger_data *enable_data = data->private_data;
16457862ad18STom Zanussi 
16467862ad18STom Zanussi 	if (enable_data->enable)
16475d6ad960SSteven Rostedt (Red Hat) 		clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
16487862ad18STom Zanussi 	else
16495d6ad960SSteven Rostedt (Red Hat) 		set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
16507862ad18STom Zanussi }
16517862ad18STom Zanussi 
16527862ad18STom Zanussi static void
event_enable_count_trigger(struct event_trigger_data * data,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * event)1653b47e3302SSteven Rostedt (VMware) event_enable_count_trigger(struct event_trigger_data *data,
1654b47e3302SSteven Rostedt (VMware) 			   struct trace_buffer *buffer,  void *rec,
16551ac4f51cSTom Zanussi 			   struct ring_buffer_event *event)
16567862ad18STom Zanussi {
16577862ad18STom Zanussi 	struct enable_trigger_data *enable_data = data->private_data;
16587862ad18STom Zanussi 
16597862ad18STom Zanussi 	if (!data->count)
16607862ad18STom Zanussi 		return;
16617862ad18STom Zanussi 
16627862ad18STom Zanussi 	/* Skip if the event is in a state we want to switch to */
16635d6ad960SSteven Rostedt (Red Hat) 	if (enable_data->enable == !(enable_data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
16647862ad18STom Zanussi 		return;
16657862ad18STom Zanussi 
16667862ad18STom Zanussi 	if (data->count != -1)
16677862ad18STom Zanussi 		(data->count)--;
16687862ad18STom Zanussi 
1669b47e3302SSteven Rostedt (VMware) 	event_enable_trigger(data, buffer, rec, event);
16707862ad18STom Zanussi }
16717862ad18STom Zanussi 
event_enable_trigger_print(struct seq_file * m,struct event_trigger_data * data)1672d0bad49bSTom Zanussi int event_enable_trigger_print(struct seq_file *m,
16737862ad18STom Zanussi 			       struct event_trigger_data *data)
16747862ad18STom Zanussi {
16757862ad18STom Zanussi 	struct enable_trigger_data *enable_data = data->private_data;
16767862ad18STom Zanussi 
16777862ad18STom Zanussi 	seq_printf(m, "%s:%s:%s",
1678d0bad49bSTom Zanussi 		   enable_data->hist ?
1679d0bad49bSTom Zanussi 		   (enable_data->enable ? ENABLE_HIST_STR : DISABLE_HIST_STR) :
1680d0bad49bSTom Zanussi 		   (enable_data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR),
16817862ad18STom Zanussi 		   enable_data->file->event_call->class->system,
1682687fcc4aSSteven Rostedt (Red Hat) 		   trace_event_name(enable_data->file->event_call));
16837862ad18STom Zanussi 
16847862ad18STom Zanussi 	if (data->count == -1)
16857862ad18STom Zanussi 		seq_puts(m, ":unlimited");
16867862ad18STom Zanussi 	else
16877862ad18STom Zanussi 		seq_printf(m, ":count=%ld", data->count);
16887862ad18STom Zanussi 
16897862ad18STom Zanussi 	if (data->filter_str)
16907862ad18STom Zanussi 		seq_printf(m, " if %s\n", data->filter_str);
16917862ad18STom Zanussi 	else
16921177e436SRasmus Villemoes 		seq_putc(m, '\n');
16937862ad18STom Zanussi 
16947862ad18STom Zanussi 	return 0;
16957862ad18STom Zanussi }
16967862ad18STom Zanussi 
event_enable_trigger_free(struct event_trigger_data * data)169747670541STom Zanussi void event_enable_trigger_free(struct event_trigger_data *data)
16987862ad18STom Zanussi {
16997862ad18STom Zanussi 	struct enable_trigger_data *enable_data = data->private_data;
17007862ad18STom Zanussi 
17017862ad18STom Zanussi 	if (WARN_ON_ONCE(data->ref <= 0))
17027862ad18STom Zanussi 		return;
17037862ad18STom Zanussi 
17047862ad18STom Zanussi 	data->ref--;
17057862ad18STom Zanussi 	if (!data->ref) {
17067862ad18STom Zanussi 		/* Remove the SOFT_MODE flag */
17077862ad18STom Zanussi 		trace_event_enable_disable(enable_data->file, 0, 1);
17081d18538eSSteven Rostedt (VMware) 		trace_event_put_ref(enable_data->file->event_call);
17097862ad18STom Zanussi 		trigger_data_free(data);
17107862ad18STom Zanussi 		kfree(enable_data);
17117862ad18STom Zanussi 	}
17127862ad18STom Zanussi }
17137862ad18STom Zanussi 
1714502d2e71SChristophe JAILLET static const struct event_trigger_ops event_enable_trigger_ops = {
1715fb339e53STom Zanussi 	.trigger		= event_enable_trigger,
17167862ad18STom Zanussi 	.print			= event_enable_trigger_print,
17177862ad18STom Zanussi 	.init			= event_trigger_init,
17187862ad18STom Zanussi 	.free			= event_enable_trigger_free,
17197862ad18STom Zanussi };
17207862ad18STom Zanussi 
1721502d2e71SChristophe JAILLET static const struct event_trigger_ops event_enable_count_trigger_ops = {
1722fb339e53STom Zanussi 	.trigger		= event_enable_count_trigger,
17237862ad18STom Zanussi 	.print			= event_enable_trigger_print,
17247862ad18STom Zanussi 	.init			= event_trigger_init,
17257862ad18STom Zanussi 	.free			= event_enable_trigger_free,
17267862ad18STom Zanussi };
17277862ad18STom Zanussi 
1728502d2e71SChristophe JAILLET static const struct event_trigger_ops event_disable_trigger_ops = {
1729fb339e53STom Zanussi 	.trigger		= event_enable_trigger,
17307862ad18STom Zanussi 	.print			= event_enable_trigger_print,
17317862ad18STom Zanussi 	.init			= event_trigger_init,
17327862ad18STom Zanussi 	.free			= event_enable_trigger_free,
17337862ad18STom Zanussi };
17347862ad18STom Zanussi 
1735502d2e71SChristophe JAILLET static const struct event_trigger_ops event_disable_count_trigger_ops = {
1736fb339e53STom Zanussi 	.trigger		= event_enable_count_trigger,
17377862ad18STom Zanussi 	.print			= event_enable_trigger_print,
17387862ad18STom Zanussi 	.init			= event_trigger_init,
17397862ad18STom Zanussi 	.free			= event_enable_trigger_free,
17407862ad18STom Zanussi };
17417862ad18STom Zanussi 
event_enable_trigger_parse(struct event_command * cmd_ops,struct trace_event_file * file,char * glob,char * cmd,char * param_and_filter)17429ec5a7d1STom Zanussi int event_enable_trigger_parse(struct event_command *cmd_ops,
17437f1d2f82SSteven Rostedt (Red Hat) 			       struct trace_event_file *file,
1744e1f187d0STom Zanussi 			       char *glob, char *cmd, char *param_and_filter)
17457862ad18STom Zanussi {
17467f1d2f82SSteven Rostedt (Red Hat) 	struct trace_event_file *event_enable_file;
17477862ad18STom Zanussi 	struct enable_trigger_data *enable_data;
17487862ad18STom Zanussi 	struct event_trigger_data *trigger_data;
17497862ad18STom Zanussi 	struct trace_array *tr = file->tr;
1750e1f187d0STom Zanussi 	char *param, *filter;
1751e1f187d0STom Zanussi 	bool enable, remove;
17527862ad18STom Zanussi 	const char *system;
17537862ad18STom Zanussi 	const char *event;
1754d0bad49bSTom Zanussi 	bool hist = false;
17557862ad18STom Zanussi 	int ret;
17567862ad18STom Zanussi 
1757e1f187d0STom Zanussi 	remove = event_trigger_check_remove(glob);
1758e1f187d0STom Zanussi 
1759e1f187d0STom Zanussi 	if (event_trigger_empty_param(param_and_filter))
1760e1f187d0STom Zanussi 		return -EINVAL;
1761e1f187d0STom Zanussi 
1762e1f187d0STom Zanussi 	ret = event_trigger_separate_filter(param_and_filter, &param, &filter, true);
1763e1f187d0STom Zanussi 	if (ret)
1764e1f187d0STom Zanussi 		return ret;
1765e1f187d0STom Zanussi 
1766e1f187d0STom Zanussi 	system = strsep(&param, ":");
17677862ad18STom Zanussi 	if (!param)
17687862ad18STom Zanussi 		return -EINVAL;
17697862ad18STom Zanussi 
1770e1f187d0STom Zanussi 	event = strsep(&param, ":");
17717862ad18STom Zanussi 
17727862ad18STom Zanussi 	ret = -EINVAL;
17737862ad18STom Zanussi 	event_enable_file = find_event_file(tr, system, event);
17747862ad18STom Zanussi 	if (!event_enable_file)
17757862ad18STom Zanussi 		goto out;
17767862ad18STom Zanussi 
1777d0bad49bSTom Zanussi #ifdef CONFIG_HIST_TRIGGERS
1778d0bad49bSTom Zanussi 	hist = ((strcmp(cmd, ENABLE_HIST_STR) == 0) ||
1779d0bad49bSTom Zanussi 		(strcmp(cmd, DISABLE_HIST_STR) == 0));
17807862ad18STom Zanussi 
1781d0bad49bSTom Zanussi 	enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
1782d0bad49bSTom Zanussi 		  (strcmp(cmd, ENABLE_HIST_STR) == 0));
1783d0bad49bSTom Zanussi #else
1784d0bad49bSTom Zanussi 	enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1785d0bad49bSTom Zanussi #endif
17867862ad18STom Zanussi 	ret = -ENOMEM;
17877862ad18STom Zanussi 
17887862ad18STom Zanussi 	enable_data = kzalloc(sizeof(*enable_data), GFP_KERNEL);
1789e1f187d0STom Zanussi 	if (!enable_data)
17907862ad18STom Zanussi 		goto out;
17917862ad18STom Zanussi 
1792d0bad49bSTom Zanussi 	enable_data->hist = hist;
17937862ad18STom Zanussi 	enable_data->enable = enable;
17947862ad18STom Zanussi 	enable_data->file = event_enable_file;
17957862ad18STom Zanussi 
1796e1f187d0STom Zanussi 	trigger_data = event_trigger_alloc(cmd_ops, cmd, param, enable_data);
1797e1f187d0STom Zanussi 	if (!trigger_data) {
1798e1f187d0STom Zanussi 		kfree(enable_data);
1799e1f187d0STom Zanussi 		goto out;
1800e1f187d0STom Zanussi 	}
1801e1f187d0STom Zanussi 
1802e1f187d0STom Zanussi 	if (remove) {
1803b8cc44a4STom Zanussi 		event_trigger_unregister(cmd_ops, file, glob+1, trigger_data);
18047862ad18STom Zanussi 		kfree(trigger_data);
18057862ad18STom Zanussi 		kfree(enable_data);
18067862ad18STom Zanussi 		ret = 0;
18077862ad18STom Zanussi 		goto out;
18087862ad18STom Zanussi 	}
18097862ad18STom Zanussi 
181015cc7864SSteven Rostedt (VMware) 	/* Up the trigger_data count to make sure nothing frees it on failure */
181147670541STom Zanussi 	event_trigger_init(trigger_data);
181215cc7864SSteven Rostedt (VMware) 
1813e1f187d0STom Zanussi 	ret = event_trigger_parse_num(param, trigger_data);
18147862ad18STom Zanussi 	if (ret)
18157862ad18STom Zanussi 		goto out_free;
18167862ad18STom Zanussi 
1817e1f187d0STom Zanussi 	ret = event_trigger_set_filter(cmd_ops, file, filter, trigger_data);
18187862ad18STom Zanussi 	if (ret < 0)
18197862ad18STom Zanussi 		goto out_free;
18207862ad18STom Zanussi 
18217862ad18STom Zanussi 	/* Don't let event modules unload while probe registered */
18221d18538eSSteven Rostedt (VMware) 	ret = trace_event_try_get_ref(event_enable_file->event_call);
18237862ad18STom Zanussi 	if (!ret) {
18247862ad18STom Zanussi 		ret = -EBUSY;
18257862ad18STom Zanussi 		goto out_free;
18267862ad18STom Zanussi 	}
18277862ad18STom Zanussi 
18287862ad18STom Zanussi 	ret = trace_event_enable_disable(event_enable_file, 1, 1);
18297862ad18STom Zanussi 	if (ret < 0)
18307862ad18STom Zanussi 		goto out_put;
1831b8cc44a4STom Zanussi 
1832b8cc44a4STom Zanussi 	ret = event_trigger_register(cmd_ops, file, glob, trigger_data);
1833b8cc44a4STom Zanussi 	if (ret)
18347862ad18STom Zanussi 		goto out_disable;
1835b8cc44a4STom Zanussi 
183647670541STom Zanussi 	event_trigger_free(trigger_data);
18377862ad18STom Zanussi  out:
18387862ad18STom Zanussi 	return ret;
18397862ad18STom Zanussi  out_disable:
18407862ad18STom Zanussi 	trace_event_enable_disable(event_enable_file, 0, 1);
18417862ad18STom Zanussi  out_put:
18421d18538eSSteven Rostedt (VMware) 	trace_event_put_ref(event_enable_file->event_call);
18437862ad18STom Zanussi  out_free:
1844e1f187d0STom Zanussi 	event_trigger_reset_filter(cmd_ops, trigger_data);
184547670541STom Zanussi 	event_trigger_free(trigger_data);
18467862ad18STom Zanussi 	kfree(enable_data);
1847e1f187d0STom Zanussi 
18487862ad18STom Zanussi 	goto out;
18497862ad18STom Zanussi }
18507862ad18STom Zanussi 
event_enable_register_trigger(char * glob,struct event_trigger_data * data,struct trace_event_file * file)1851d0bad49bSTom Zanussi int event_enable_register_trigger(char *glob,
18527862ad18STom Zanussi 				  struct event_trigger_data *data,
18537f1d2f82SSteven Rostedt (Red Hat) 				  struct trace_event_file *file)
18547862ad18STom Zanussi {
18557862ad18STom Zanussi 	struct enable_trigger_data *enable_data = data->private_data;
18567862ad18STom Zanussi 	struct enable_trigger_data *test_enable_data;
18577862ad18STom Zanussi 	struct event_trigger_data *test;
18587862ad18STom Zanussi 	int ret = 0;
18597862ad18STom Zanussi 
18603b42a4c8SMasami Hiramatsu 	lockdep_assert_held(&event_mutex);
18613b42a4c8SMasami Hiramatsu 
18623b42a4c8SMasami Hiramatsu 	list_for_each_entry(test, &file->triggers, list) {
18637862ad18STom Zanussi 		test_enable_data = test->private_data;
18647862ad18STom Zanussi 		if (test_enable_data &&
1865d0bad49bSTom Zanussi 		    (test->cmd_ops->trigger_type ==
1866d0bad49bSTom Zanussi 		     data->cmd_ops->trigger_type) &&
18677862ad18STom Zanussi 		    (test_enable_data->file == enable_data->file)) {
18687862ad18STom Zanussi 			ret = -EEXIST;
18697862ad18STom Zanussi 			goto out;
18707862ad18STom Zanussi 		}
18717862ad18STom Zanussi 	}
18727862ad18STom Zanussi 
18737862ad18STom Zanussi 	if (data->ops->init) {
187447670541STom Zanussi 		ret = data->ops->init(data);
18757862ad18STom Zanussi 		if (ret < 0)
18767862ad18STom Zanussi 			goto out;
18777862ad18STom Zanussi 	}
18787862ad18STom Zanussi 
18797862ad18STom Zanussi 	list_add_rcu(&data->list, &file->triggers);
18807862ad18STom Zanussi 
18814e4a4d75STom Zanussi 	update_cond_flag(file);
1882b8cc44a4STom Zanussi 	ret = trace_event_trigger_enable_disable(file, 1);
1883b8cc44a4STom Zanussi 	if (ret < 0) {
18847862ad18STom Zanussi 		list_del_rcu(&data->list);
18854e4a4d75STom Zanussi 		update_cond_flag(file);
18867862ad18STom Zanussi 	}
18877862ad18STom Zanussi out:
18887862ad18STom Zanussi 	return ret;
18897862ad18STom Zanussi }
18907862ad18STom Zanussi 
event_enable_unregister_trigger(char * glob,struct event_trigger_data * test,struct trace_event_file * file)1891d0bad49bSTom Zanussi void event_enable_unregister_trigger(char *glob,
18927862ad18STom Zanussi 				     struct event_trigger_data *test,
18937f1d2f82SSteven Rostedt (Red Hat) 				     struct trace_event_file *file)
18947862ad18STom Zanussi {
18957862ad18STom Zanussi 	struct enable_trigger_data *test_enable_data = test->private_data;
189645e333ceSJakob Koschel 	struct event_trigger_data *data = NULL, *iter;
18977862ad18STom Zanussi 	struct enable_trigger_data *enable_data;
18987862ad18STom Zanussi 
18993b42a4c8SMasami Hiramatsu 	lockdep_assert_held(&event_mutex);
19003b42a4c8SMasami Hiramatsu 
190145e333ceSJakob Koschel 	list_for_each_entry(iter, &file->triggers, list) {
190245e333ceSJakob Koschel 		enable_data = iter->private_data;
19037862ad18STom Zanussi 		if (enable_data &&
190445e333ceSJakob Koschel 		    (iter->cmd_ops->trigger_type ==
1905d0bad49bSTom Zanussi 		     test->cmd_ops->trigger_type) &&
19067862ad18STom Zanussi 		    (enable_data->file == test_enable_data->file)) {
190745e333ceSJakob Koschel 			data = iter;
19087862ad18STom Zanussi 			list_del_rcu(&data->list);
19097862ad18STom Zanussi 			trace_event_trigger_enable_disable(file, 0);
19104e4a4d75STom Zanussi 			update_cond_flag(file);
19117862ad18STom Zanussi 			break;
19127862ad18STom Zanussi 		}
19137862ad18STom Zanussi 	}
19147862ad18STom Zanussi 
191545e333ceSJakob Koschel 	if (data && data->ops->free)
191647670541STom Zanussi 		data->ops->free(data);
19177862ad18STom Zanussi }
19187862ad18STom Zanussi 
1919502d2e71SChristophe JAILLET static const struct event_trigger_ops *
event_enable_get_trigger_ops(char * cmd,char * param)19207862ad18STom Zanussi event_enable_get_trigger_ops(char *cmd, char *param)
19217862ad18STom Zanussi {
1922502d2e71SChristophe JAILLET 	const struct event_trigger_ops *ops;
19237862ad18STom Zanussi 	bool enable;
19247862ad18STom Zanussi 
1925d0bad49bSTom Zanussi #ifdef CONFIG_HIST_TRIGGERS
1926d0bad49bSTom Zanussi 	enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
1927d0bad49bSTom Zanussi 		  (strcmp(cmd, ENABLE_HIST_STR) == 0));
1928d0bad49bSTom Zanussi #else
19297862ad18STom Zanussi 	enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1930d0bad49bSTom Zanussi #endif
19317862ad18STom Zanussi 	if (enable)
19327862ad18STom Zanussi 		ops = param ? &event_enable_count_trigger_ops :
19337862ad18STom Zanussi 			&event_enable_trigger_ops;
19347862ad18STom Zanussi 	else
19357862ad18STom Zanussi 		ops = param ? &event_disable_count_trigger_ops :
19367862ad18STom Zanussi 			&event_disable_trigger_ops;
19377862ad18STom Zanussi 
19387862ad18STom Zanussi 	return ops;
19397862ad18STom Zanussi }
19407862ad18STom Zanussi 
19417862ad18STom Zanussi static struct event_command trigger_enable_cmd = {
19427862ad18STom Zanussi 	.name			= ENABLE_EVENT_STR,
19437862ad18STom Zanussi 	.trigger_type		= ETT_EVENT_ENABLE,
19449ec5a7d1STom Zanussi 	.parse			= event_enable_trigger_parse,
19457862ad18STom Zanussi 	.reg			= event_enable_register_trigger,
19467862ad18STom Zanussi 	.unreg			= event_enable_unregister_trigger,
19477862ad18STom Zanussi 	.get_trigger_ops	= event_enable_get_trigger_ops,
1948bac5fb97STom Zanussi 	.set_filter		= set_trigger_filter,
19497862ad18STom Zanussi };
19507862ad18STom Zanussi 
19517862ad18STom Zanussi static struct event_command trigger_disable_cmd = {
19527862ad18STom Zanussi 	.name			= DISABLE_EVENT_STR,
19537862ad18STom Zanussi 	.trigger_type		= ETT_EVENT_ENABLE,
19549ec5a7d1STom Zanussi 	.parse			= event_enable_trigger_parse,
19557862ad18STom Zanussi 	.reg			= event_enable_register_trigger,
19567862ad18STom Zanussi 	.unreg			= event_enable_unregister_trigger,
19577862ad18STom Zanussi 	.get_trigger_ops	= event_enable_get_trigger_ops,
1958bac5fb97STom Zanussi 	.set_filter		= set_trigger_filter,
19597862ad18STom Zanussi };
19607862ad18STom Zanussi 
unregister_trigger_enable_disable_cmds(void)19617862ad18STom Zanussi static __init void unregister_trigger_enable_disable_cmds(void)
19627862ad18STom Zanussi {
19637862ad18STom Zanussi 	unregister_event_command(&trigger_enable_cmd);
19647862ad18STom Zanussi 	unregister_event_command(&trigger_disable_cmd);
19657862ad18STom Zanussi }
19667862ad18STom Zanussi 
register_trigger_enable_disable_cmds(void)19677862ad18STom Zanussi static __init int register_trigger_enable_disable_cmds(void)
19687862ad18STom Zanussi {
19697862ad18STom Zanussi 	int ret;
19707862ad18STom Zanussi 
19717862ad18STom Zanussi 	ret = register_event_command(&trigger_enable_cmd);
19727862ad18STom Zanussi 	if (WARN_ON(ret < 0))
19737862ad18STom Zanussi 		return ret;
19747862ad18STom Zanussi 	ret = register_event_command(&trigger_disable_cmd);
19757862ad18STom Zanussi 	if (WARN_ON(ret < 0))
19767862ad18STom Zanussi 		unregister_trigger_enable_disable_cmds();
19777862ad18STom Zanussi 
19787862ad18STom Zanussi 	return ret;
19797862ad18STom Zanussi }
19807862ad18STom Zanussi 
register_trigger_traceon_traceoff_cmds(void)19812a2df321STom Zanussi static __init int register_trigger_traceon_traceoff_cmds(void)
19822a2df321STom Zanussi {
19832a2df321STom Zanussi 	int ret;
19842a2df321STom Zanussi 
19852a2df321STom Zanussi 	ret = register_event_command(&trigger_traceon_cmd);
19862a2df321STom Zanussi 	if (WARN_ON(ret < 0))
19872a2df321STom Zanussi 		return ret;
19882a2df321STom Zanussi 	ret = register_event_command(&trigger_traceoff_cmd);
19892a2df321STom Zanussi 	if (WARN_ON(ret < 0))
19902a2df321STom Zanussi 		unregister_trigger_traceon_traceoff_cmds();
19912a2df321STom Zanussi 
19922a2df321STom Zanussi 	return ret;
19932a2df321STom Zanussi }
19942a2df321STom Zanussi 
register_trigger_cmds(void)199585f2b082STom Zanussi __init int register_trigger_cmds(void)
199685f2b082STom Zanussi {
19972a2df321STom Zanussi 	register_trigger_traceon_traceoff_cmds();
199893e31ffbSTom Zanussi 	register_trigger_snapshot_cmd();
1999f21ecbb3STom Zanussi 	register_trigger_stacktrace_cmd();
20007862ad18STom Zanussi 	register_trigger_enable_disable_cmds();
2001d0bad49bSTom Zanussi 	register_trigger_hist_enable_disable_cmds();
20027ef224d1STom Zanussi 	register_trigger_hist_cmd();
20032a2df321STom Zanussi 
200485f2b082STom Zanussi 	return 0;
200585f2b082STom Zanussi }
2006