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