1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * trace_events_trigger - trace event triggers
4  *
5  * Copyright (C) 2013 Tom Zanussi <[email protected]>
6  */
7 
8 #include <linux/security.h>
9 #include <linux/module.h>
10 #include <linux/ctype.h>
11 #include <linux/mutex.h>
12 #include <linux/slab.h>
13 #include <linux/rculist.h>
14 
15 #include "trace.h"
16 
17 static LIST_HEAD(trigger_commands);
18 static DEFINE_MUTEX(trigger_cmd_mutex);
19 
20 void trigger_data_free(struct event_trigger_data *data)
21 {
22 	if (data->cmd_ops->set_filter)
23 		data->cmd_ops->set_filter(NULL, data, NULL);
24 
25 	/* make sure current triggers exit before free */
26 	tracepoint_synchronize_unregister();
27 
28 	kfree(data);
29 }
30 
31 /**
32  * event_triggers_call - Call triggers associated with a trace event
33  * @file: The trace_event_file associated with the event
34  * @rec: The trace entry for the event, NULL for unconditional invocation
35  *
36  * For each trigger associated with an event, invoke the trigger
37  * function registered with the associated trigger command.  If rec is
38  * non-NULL, it means that the trigger requires further processing and
39  * shouldn't be unconditionally invoked.  If rec is non-NULL and the
40  * trigger has a filter associated with it, rec will checked against
41  * the filter and if the record matches the trigger will be invoked.
42  * If the trigger is a 'post_trigger', meaning it shouldn't be invoked
43  * in any case until the current event is written, the trigger
44  * function isn't invoked but the bit associated with the deferred
45  * trigger is set in the return value.
46  *
47  * Returns an enum event_trigger_type value containing a set bit for
48  * any trigger that should be deferred, ETT_NONE if nothing to defer.
49  *
50  * Called from tracepoint handlers (with rcu_read_lock_sched() held).
51  *
52  * Return: an enum event_trigger_type value containing a set bit for
53  * any trigger that should be deferred, ETT_NONE if nothing to defer.
54  */
55 enum event_trigger_type
56 event_triggers_call(struct trace_event_file *file,
57 		    struct trace_buffer *buffer, void *rec,
58 		    struct ring_buffer_event *event)
59 {
60 	struct event_trigger_data *data;
61 	enum event_trigger_type tt = ETT_NONE;
62 	struct event_filter *filter;
63 
64 	if (list_empty(&file->triggers))
65 		return tt;
66 
67 	list_for_each_entry_rcu(data, &file->triggers, list) {
68 		if (data->paused)
69 			continue;
70 		if (!rec) {
71 			data->ops->trigger(data, buffer, rec, event);
72 			continue;
73 		}
74 		filter = rcu_dereference_sched(data->filter);
75 		if (filter && !filter_match_preds(filter, rec))
76 			continue;
77 		if (event_command_post_trigger(data->cmd_ops)) {
78 			tt |= data->cmd_ops->trigger_type;
79 			continue;
80 		}
81 		data->ops->trigger(data, buffer, rec, event);
82 	}
83 	return tt;
84 }
85 EXPORT_SYMBOL_GPL(event_triggers_call);
86 
87 /**
88  * event_triggers_post_call - Call 'post_triggers' for a trace event
89  * @file: The trace_event_file associated with the event
90  * @tt: enum event_trigger_type containing a set bit for each trigger to invoke
91  *
92  * For each trigger associated with an event, invoke the trigger
93  * function registered with the associated trigger command, if the
94  * corresponding bit is set in the tt enum passed into this function.
95  * See @event_triggers_call for details on how those bits are set.
96  *
97  * Called from tracepoint handlers (with rcu_read_lock_sched() held).
98  */
99 void
100 event_triggers_post_call(struct trace_event_file *file,
101 			 enum event_trigger_type tt)
102 {
103 	struct event_trigger_data *data;
104 
105 	list_for_each_entry_rcu(data, &file->triggers, list) {
106 		if (data->paused)
107 			continue;
108 		if (data->cmd_ops->trigger_type & tt)
109 			data->ops->trigger(data, NULL, NULL, NULL);
110 	}
111 }
112 EXPORT_SYMBOL_GPL(event_triggers_post_call);
113 
114 #define SHOW_AVAILABLE_TRIGGERS	(void *)(1UL)
115 
116 static void *trigger_next(struct seq_file *m, void *t, loff_t *pos)
117 {
118 	struct trace_event_file *event_file = event_file_data(m->private);
119 
120 	if (t == SHOW_AVAILABLE_TRIGGERS) {
121 		(*pos)++;
122 		return NULL;
123 	}
124 	return seq_list_next(t, &event_file->triggers, pos);
125 }
126 
127 static bool check_user_trigger(struct trace_event_file *file)
128 {
129 	struct event_trigger_data *data;
130 
131 	list_for_each_entry_rcu(data, &file->triggers, list) {
132 		if (data->flags & EVENT_TRIGGER_FL_PROBE)
133 			continue;
134 		return true;
135 	}
136 	return false;
137 }
138 
139 static void *trigger_start(struct seq_file *m, loff_t *pos)
140 {
141 	struct trace_event_file *event_file;
142 
143 	/* ->stop() is called even if ->start() fails */
144 	mutex_lock(&event_mutex);
145 	event_file = event_file_data(m->private);
146 	if (unlikely(!event_file))
147 		return ERR_PTR(-ENODEV);
148 
149 	if (list_empty(&event_file->triggers) || !check_user_trigger(event_file))
150 		return *pos == 0 ? SHOW_AVAILABLE_TRIGGERS : NULL;
151 
152 	return seq_list_start(&event_file->triggers, *pos);
153 }
154 
155 static void trigger_stop(struct seq_file *m, void *t)
156 {
157 	mutex_unlock(&event_mutex);
158 }
159 
160 static int trigger_show(struct seq_file *m, void *v)
161 {
162 	struct event_trigger_data *data;
163 	struct event_command *p;
164 
165 	if (v == SHOW_AVAILABLE_TRIGGERS) {
166 		seq_puts(m, "# Available triggers:\n");
167 		seq_putc(m, '#');
168 		mutex_lock(&trigger_cmd_mutex);
169 		list_for_each_entry_reverse(p, &trigger_commands, list)
170 			seq_printf(m, " %s", p->name);
171 		seq_putc(m, '\n');
172 		mutex_unlock(&trigger_cmd_mutex);
173 		return 0;
174 	}
175 
176 	data = list_entry(v, struct event_trigger_data, list);
177 	data->ops->print(m, data->ops, data);
178 
179 	return 0;
180 }
181 
182 static const struct seq_operations event_triggers_seq_ops = {
183 	.start = trigger_start,
184 	.next = trigger_next,
185 	.stop = trigger_stop,
186 	.show = trigger_show,
187 };
188 
189 static int event_trigger_regex_open(struct inode *inode, struct file *file)
190 {
191 	int ret;
192 
193 	ret = security_locked_down(LOCKDOWN_TRACEFS);
194 	if (ret)
195 		return ret;
196 
197 	mutex_lock(&event_mutex);
198 
199 	if (unlikely(!event_file_data(file))) {
200 		mutex_unlock(&event_mutex);
201 		return -ENODEV;
202 	}
203 
204 	if ((file->f_mode & FMODE_WRITE) &&
205 	    (file->f_flags & O_TRUNC)) {
206 		struct trace_event_file *event_file;
207 		struct event_command *p;
208 
209 		event_file = event_file_data(file);
210 
211 		list_for_each_entry(p, &trigger_commands, list) {
212 			if (p->unreg_all)
213 				p->unreg_all(event_file);
214 		}
215 	}
216 
217 	if (file->f_mode & FMODE_READ) {
218 		ret = seq_open(file, &event_triggers_seq_ops);
219 		if (!ret) {
220 			struct seq_file *m = file->private_data;
221 			m->private = file;
222 		}
223 	}
224 
225 	mutex_unlock(&event_mutex);
226 
227 	return ret;
228 }
229 
230 int trigger_process_regex(struct trace_event_file *file, char *buff)
231 {
232 	char *command, *next;
233 	struct event_command *p;
234 	int ret = -EINVAL;
235 
236 	next = buff = skip_spaces(buff);
237 	command = strsep(&next, ": \t");
238 	if (next) {
239 		next = skip_spaces(next);
240 		if (!*next)
241 			next = NULL;
242 	}
243 	command = (command[0] != '!') ? command : command + 1;
244 
245 	mutex_lock(&trigger_cmd_mutex);
246 	list_for_each_entry(p, &trigger_commands, list) {
247 		if (strcmp(p->name, command) == 0) {
248 			ret = p->parse(p, file, buff, command, next);
249 			goto out_unlock;
250 		}
251 	}
252  out_unlock:
253 	mutex_unlock(&trigger_cmd_mutex);
254 
255 	return ret;
256 }
257 
258 static ssize_t event_trigger_regex_write(struct file *file,
259 					 const char __user *ubuf,
260 					 size_t cnt, loff_t *ppos)
261 {
262 	struct trace_event_file *event_file;
263 	ssize_t ret;
264 	char *buf;
265 
266 	if (!cnt)
267 		return 0;
268 
269 	if (cnt >= PAGE_SIZE)
270 		return -EINVAL;
271 
272 	buf = memdup_user_nul(ubuf, cnt);
273 	if (IS_ERR(buf))
274 		return PTR_ERR(buf);
275 
276 	strim(buf);
277 
278 	mutex_lock(&event_mutex);
279 	event_file = event_file_data(file);
280 	if (unlikely(!event_file)) {
281 		mutex_unlock(&event_mutex);
282 		kfree(buf);
283 		return -ENODEV;
284 	}
285 	ret = trigger_process_regex(event_file, buf);
286 	mutex_unlock(&event_mutex);
287 
288 	kfree(buf);
289 	if (ret < 0)
290 		goto out;
291 
292 	*ppos += cnt;
293 	ret = cnt;
294  out:
295 	return ret;
296 }
297 
298 static int event_trigger_regex_release(struct inode *inode, struct file *file)
299 {
300 	mutex_lock(&event_mutex);
301 
302 	if (file->f_mode & FMODE_READ)
303 		seq_release(inode, file);
304 
305 	mutex_unlock(&event_mutex);
306 
307 	return 0;
308 }
309 
310 static ssize_t
311 event_trigger_write(struct file *filp, const char __user *ubuf,
312 		    size_t cnt, loff_t *ppos)
313 {
314 	return event_trigger_regex_write(filp, ubuf, cnt, ppos);
315 }
316 
317 static int
318 event_trigger_open(struct inode *inode, struct file *filp)
319 {
320 	/* Checks for tracefs lockdown */
321 	return event_trigger_regex_open(inode, filp);
322 }
323 
324 static int
325 event_trigger_release(struct inode *inode, struct file *file)
326 {
327 	return event_trigger_regex_release(inode, file);
328 }
329 
330 const struct file_operations event_trigger_fops = {
331 	.open = event_trigger_open,
332 	.read = seq_read,
333 	.write = event_trigger_write,
334 	.llseek = tracing_lseek,
335 	.release = event_trigger_release,
336 };
337 
338 /*
339  * Currently we only register event commands from __init, so mark this
340  * __init too.
341  */
342 __init int register_event_command(struct event_command *cmd)
343 {
344 	struct event_command *p;
345 	int ret = 0;
346 
347 	mutex_lock(&trigger_cmd_mutex);
348 	list_for_each_entry(p, &trigger_commands, list) {
349 		if (strcmp(cmd->name, p->name) == 0) {
350 			ret = -EBUSY;
351 			goto out_unlock;
352 		}
353 	}
354 	list_add(&cmd->list, &trigger_commands);
355  out_unlock:
356 	mutex_unlock(&trigger_cmd_mutex);
357 
358 	return ret;
359 }
360 
361 /*
362  * Currently we only unregister event commands from __init, so mark
363  * this __init too.
364  */
365 __init int unregister_event_command(struct event_command *cmd)
366 {
367 	struct event_command *p, *n;
368 	int ret = -ENODEV;
369 
370 	mutex_lock(&trigger_cmd_mutex);
371 	list_for_each_entry_safe(p, n, &trigger_commands, list) {
372 		if (strcmp(cmd->name, p->name) == 0) {
373 			ret = 0;
374 			list_del_init(&p->list);
375 			goto out_unlock;
376 		}
377 	}
378  out_unlock:
379 	mutex_unlock(&trigger_cmd_mutex);
380 
381 	return ret;
382 }
383 
384 /**
385  * event_trigger_print - Generic event_trigger_ops @print implementation
386  * @name: The name of the event trigger
387  * @m: The seq_file being printed to
388  * @data: Trigger-specific data
389  * @filter_str: filter_str to print, if present
390  *
391  * Common implementation for event triggers to print themselves.
392  *
393  * Usually wrapped by a function that simply sets the @name of the
394  * trigger command and then invokes this.
395  *
396  * Return: 0 on success, errno otherwise
397  */
398 static int
399 event_trigger_print(const char *name, struct seq_file *m,
400 		    void *data, char *filter_str)
401 {
402 	long count = (long)data;
403 
404 	seq_puts(m, name);
405 
406 	if (count == -1)
407 		seq_puts(m, ":unlimited");
408 	else
409 		seq_printf(m, ":count=%ld", count);
410 
411 	if (filter_str)
412 		seq_printf(m, " if %s\n", filter_str);
413 	else
414 		seq_putc(m, '\n');
415 
416 	return 0;
417 }
418 
419 /**
420  * event_trigger_init - Generic event_trigger_ops @init implementation
421  * @ops: The trigger ops associated with the trigger
422  * @data: Trigger-specific data
423  *
424  * Common implementation of event trigger initialization.
425  *
426  * Usually used directly as the @init method in event trigger
427  * implementations.
428  *
429  * Return: 0 on success, errno otherwise
430  */
431 int event_trigger_init(struct event_trigger_ops *ops,
432 		       struct event_trigger_data *data)
433 {
434 	data->ref++;
435 	return 0;
436 }
437 
438 /**
439  * event_trigger_free - Generic event_trigger_ops @free implementation
440  * @ops: The trigger ops associated with the trigger
441  * @data: Trigger-specific data
442  *
443  * Common implementation of event trigger de-initialization.
444  *
445  * Usually used directly as the @free method in event trigger
446  * implementations.
447  */
448 static void
449 event_trigger_free(struct event_trigger_ops *ops,
450 		   struct event_trigger_data *data)
451 {
452 	if (WARN_ON_ONCE(data->ref <= 0))
453 		return;
454 
455 	data->ref--;
456 	if (!data->ref)
457 		trigger_data_free(data);
458 }
459 
460 int trace_event_trigger_enable_disable(struct trace_event_file *file,
461 				       int trigger_enable)
462 {
463 	int ret = 0;
464 
465 	if (trigger_enable) {
466 		if (atomic_inc_return(&file->tm_ref) > 1)
467 			return ret;
468 		set_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
469 		ret = trace_event_enable_disable(file, 1, 1);
470 	} else {
471 		if (atomic_dec_return(&file->tm_ref) > 0)
472 			return ret;
473 		clear_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
474 		ret = trace_event_enable_disable(file, 0, 1);
475 	}
476 
477 	return ret;
478 }
479 
480 /**
481  * clear_event_triggers - Clear all triggers associated with a trace array
482  * @tr: The trace array to clear
483  *
484  * For each trigger, the triggering event has its tm_ref decremented
485  * via trace_event_trigger_enable_disable(), and any associated event
486  * (in the case of enable/disable_event triggers) will have its sm_ref
487  * decremented via free()->trace_event_enable_disable().  That
488  * combination effectively reverses the soft-mode/trigger state added
489  * by trigger registration.
490  *
491  * Must be called with event_mutex held.
492  */
493 void
494 clear_event_triggers(struct trace_array *tr)
495 {
496 	struct trace_event_file *file;
497 
498 	list_for_each_entry(file, &tr->events, list) {
499 		struct event_trigger_data *data, *n;
500 		list_for_each_entry_safe(data, n, &file->triggers, list) {
501 			trace_event_trigger_enable_disable(file, 0);
502 			list_del_rcu(&data->list);
503 			if (data->ops->free)
504 				data->ops->free(data->ops, data);
505 		}
506 	}
507 }
508 
509 /**
510  * update_cond_flag - Set or reset the TRIGGER_COND bit
511  * @file: The trace_event_file associated with the event
512  *
513  * If an event has triggers and any of those triggers has a filter or
514  * a post_trigger, trigger invocation needs to be deferred until after
515  * the current event has logged its data, and the event should have
516  * its TRIGGER_COND bit set, otherwise the TRIGGER_COND bit should be
517  * cleared.
518  */
519 void update_cond_flag(struct trace_event_file *file)
520 {
521 	struct event_trigger_data *data;
522 	bool set_cond = false;
523 
524 	lockdep_assert_held(&event_mutex);
525 
526 	list_for_each_entry(data, &file->triggers, list) {
527 		if (data->filter || event_command_post_trigger(data->cmd_ops) ||
528 		    event_command_needs_rec(data->cmd_ops)) {
529 			set_cond = true;
530 			break;
531 		}
532 	}
533 
534 	if (set_cond)
535 		set_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
536 	else
537 		clear_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
538 }
539 
540 /**
541  * register_trigger - Generic event_command @reg implementation
542  * @glob: The raw string used to register the trigger
543  * @data: Trigger-specific data to associate with the trigger
544  * @file: The trace_event_file associated with the event
545  *
546  * Common implementation for event trigger registration.
547  *
548  * Usually used directly as the @reg method in event command
549  * implementations.
550  *
551  * Return: 0 on success, errno otherwise
552  */
553 static int register_trigger(char *glob,
554 			    struct event_trigger_data *data,
555 			    struct trace_event_file *file)
556 {
557 	struct event_trigger_data *test;
558 	int ret = 0;
559 
560 	lockdep_assert_held(&event_mutex);
561 
562 	list_for_each_entry(test, &file->triggers, list) {
563 		if (test->cmd_ops->trigger_type == data->cmd_ops->trigger_type) {
564 			ret = -EEXIST;
565 			goto out;
566 		}
567 	}
568 
569 	if (data->ops->init) {
570 		ret = data->ops->init(data->ops, data);
571 		if (ret < 0)
572 			goto out;
573 	}
574 
575 	list_add_rcu(&data->list, &file->triggers);
576 	ret++;
577 
578 	update_cond_flag(file);
579 	if (trace_event_trigger_enable_disable(file, 1) < 0) {
580 		list_del_rcu(&data->list);
581 		update_cond_flag(file);
582 		ret--;
583 	}
584 out:
585 	return ret;
586 }
587 
588 /**
589  * unregister_trigger - Generic event_command @unreg implementation
590  * @glob: The raw string used to register the trigger
591  * @test: Trigger-specific data used to find the trigger to remove
592  * @file: The trace_event_file associated with the event
593  *
594  * Common implementation for event trigger unregistration.
595  *
596  * Usually used directly as the @unreg method in event command
597  * implementations.
598  */
599 static void unregister_trigger(char *glob,
600 			       struct event_trigger_data *test,
601 			       struct trace_event_file *file)
602 {
603 	struct event_trigger_data *data;
604 	bool unregistered = false;
605 
606 	lockdep_assert_held(&event_mutex);
607 
608 	list_for_each_entry(data, &file->triggers, list) {
609 		if (data->cmd_ops->trigger_type == test->cmd_ops->trigger_type) {
610 			unregistered = true;
611 			list_del_rcu(&data->list);
612 			trace_event_trigger_enable_disable(file, 0);
613 			update_cond_flag(file);
614 			break;
615 		}
616 	}
617 
618 	if (unregistered && data->ops->free)
619 		data->ops->free(data->ops, data);
620 }
621 
622 /*
623  * Event trigger parsing helper functions.
624  *
625  * These functions help make it easier to write an event trigger
626  * parsing function i.e. the struct event_command.parse() callback
627  * function responsible for parsing and registering a trigger command
628  * written to the 'trigger' file.
629  *
630  * A trigger command (or just 'trigger' for short) takes the form:
631  *   [trigger] [if filter]
632  *
633  * The struct event_command.parse() callback (and other struct
634  * event_command functions) refer to several components of a trigger
635  * command.  Those same components are referenced by the event trigger
636  * parsing helper functions defined below.  These components are:
637  *
638  *   cmd               - the trigger command name
639  *   glob              - the trigger command name optionally prefaced with '!'
640  *   param_and_filter  - text following cmd and ':'
641  *   param             - text following cmd and ':' and stripped of filter
642  *   filter            - the optional filter text following (and including) 'if'
643  *
644  * To illustrate the use of these componenents, here are some concrete
645  * examples. For the following triggers:
646  *
647  *   echo 'traceon:5 if pid == 0' > trigger
648  *     - 'traceon' is both cmd and glob
649  *     - '5 if pid == 0' is the param_and_filter
650  *     - '5' is the param
651  *     - 'if pid == 0' is the filter
652  *
653  *   echo 'enable_event:sys:event:n' > trigger
654  *     - 'enable_event' is both cmd and glob
655  *     - 'sys:event:n' is the param_and_filter
656  *     - 'sys:event:n' is the param
657  *     - there is no filter
658  *
659  *   echo 'hist:keys=pid if prio > 50' > trigger
660  *     - 'hist' is both cmd and glob
661  *     - 'keys=pid if prio > 50' is the param_and_filter
662  *     - 'keys=pid' is the param
663  *     - 'if prio > 50' is the filter
664  *
665  *   echo '!enable_event:sys:event:n' > trigger
666  *     - 'enable_event' the cmd
667  *     - '!enable_event' is the glob
668  *     - 'sys:event:n' is the param_and_filter
669  *     - 'sys:event:n' is the param
670  *     - there is no filter
671  *
672  *   echo 'traceoff' > trigger
673  *     - 'traceoff' is both cmd and glob
674  *     - there is no param_and_filter
675  *     - there is no param
676  *     - there is no filter
677  *
678  * There are a few different categories of event trigger covered by
679  * these helpers:
680  *
681  *  - triggers that don't require a parameter e.g. traceon
682  *  - triggers that do require a parameter e.g. enable_event and hist
683  *  - triggers that though they may not require a param may support an
684  *    optional 'n' param (n = number of times the trigger should fire)
685  *    e.g.: traceon:5 or enable_event:sys:event:n
686  *  - triggers that do not support an 'n' param e.g. hist
687  *
688  * These functions can be used or ignored as necessary - it all
689  * depends on the complexity of the trigger, and the granularity of
690  * the functions supported reflects the fact that some implementations
691  * may need to customize certain aspects of their implementations and
692  * won't need certain functions.  For instance, the hist trigger
693  * implementation doesn't use event_trigger_separate_filter() because
694  * it has special requirements for handling the filter.
695  */
696 
697 /**
698  * event_trigger_check_remove - check whether an event trigger specifies remove
699  * @glob: The trigger command string, with optional remove(!) operator
700  *
701  * The event trigger callback implementations pass in 'glob' as a
702  * parameter.  This is the command name either with or without a
703  * remove(!)  operator.  This function simply parses the glob and
704  * determines whether the command corresponds to a trigger removal or
705  * a trigger addition.
706  *
707  * Return: true if this is a remove command, false otherwise
708  */
709 bool event_trigger_check_remove(const char *glob)
710 {
711 	return (glob && glob[0] == '!') ? true : false;
712 }
713 
714 /**
715  * event_trigger_empty_param - check whether the param is empty
716  * @param: The trigger param string
717  *
718  * The event trigger callback implementations pass in 'param' as a
719  * parameter.  This corresponds to the string following the command
720  * name minus the command name.  This function can be called by a
721  * callback implementation for any command that requires a param; a
722  * callback that doesn't require a param can ignore it.
723  *
724  * Return: true if this is an empty param, false otherwise
725  */
726 bool event_trigger_empty_param(const char *param)
727 {
728 	return !param;
729 }
730 
731 /**
732  * event_trigger_separate_filter - separate an event trigger from a filter
733  * @param: The param string containing trigger and possibly filter
734  * @trigger: outparam, will be filled with a pointer to the trigger
735  * @filter: outparam, will be filled with a pointer to the filter
736  * @param_required: Specifies whether or not the param string is required
737  *
738  * Given a param string of the form '[trigger] [if filter]', this
739  * function separates the filter from the trigger and returns the
740  * trigger in *trigger and the filter in *filter.  Either the *trigger
741  * or the *filter may be set to NULL by this function - if not set to
742  * NULL, they will contain strings corresponding to the trigger and
743  * filter.
744  *
745  * There are two cases that need to be handled with respect to the
746  * passed-in param: either the param is required, or it is not
747  * required.  If @param_required is set, and there's no param, it will
748  * return -EINVAL.  If @param_required is not set and there's a param
749  * that starts with a number, that corresponds to the case of a
750  * trigger with :n (n = number of times the trigger should fire) and
751  * the parsing continues normally; otherwise the function just returns
752  * and assumes param just contains a filter and there's nothing else
753  * to do.
754  *
755  * Return: 0 on success, errno otherwise
756  */
757 int event_trigger_separate_filter(char *param_and_filter, char **param,
758 				  char **filter, bool param_required)
759 {
760 	int ret = 0;
761 
762 	*param = *filter = NULL;
763 
764 	if (!param_and_filter) {
765 		if (param_required)
766 			ret = -EINVAL;
767 		goto out;
768 	}
769 
770 	/*
771 	 * Here we check for an optional param. The only legal
772 	 * optional param is :n, and if that's the case, continue
773 	 * below. Otherwise we assume what's left is a filter and
774 	 * return it as the filter string for the caller to deal with.
775 	 */
776 	if (!param_required && param_and_filter && !isdigit(param_and_filter[0])) {
777 		*filter = param_and_filter;
778 		goto out;
779 	}
780 
781 	/*
782 	 * Separate the param from the filter (param [if filter]).
783 	 * Here we have either an optional :n param or a required
784 	 * param and an optional filter.
785 	 */
786 	*param = strsep(&param_and_filter, " \t");
787 
788 	/*
789 	 * Here we have a filter, though it may be empty.
790 	 */
791 	if (param_and_filter) {
792 		*filter = skip_spaces(param_and_filter);
793 		if (!**filter)
794 			*filter = NULL;
795 	}
796 out:
797 	return ret;
798 }
799 
800 /**
801  * event_trigger_alloc - allocate and init event_trigger_data for a trigger
802  * @cmd_ops: The event_command operations for the trigger
803  * @cmd: The cmd string
804  * @param: The param string
805  * @private_data: User data to associate with the event trigger
806  *
807  * Allocate an event_trigger_data instance and initialize it.  The
808  * @cmd_ops are used along with the @cmd and @param to get the
809  * trigger_ops to assign to the event_trigger_data.  @private_data can
810  * also be passed in and associated with the event_trigger_data.
811  *
812  * Use event_trigger_free() to free an event_trigger_data object.
813  *
814  * Return: The trigger_data object success, NULL otherwise
815  */
816 struct event_trigger_data *event_trigger_alloc(struct event_command *cmd_ops,
817 					       char *cmd,
818 					       char *param,
819 					       void *private_data)
820 {
821 	struct event_trigger_data *trigger_data;
822 	struct event_trigger_ops *trigger_ops;
823 
824 	trigger_ops = cmd_ops->get_trigger_ops(cmd, param);
825 
826 	trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
827 	if (!trigger_data)
828 		return NULL;
829 
830 	trigger_data->count = -1;
831 	trigger_data->ops = trigger_ops;
832 	trigger_data->cmd_ops = cmd_ops;
833 	trigger_data->private_data = private_data;
834 
835 	INIT_LIST_HEAD(&trigger_data->list);
836 	INIT_LIST_HEAD(&trigger_data->named_list);
837 	RCU_INIT_POINTER(trigger_data->filter, NULL);
838 
839 	return trigger_data;
840 }
841 
842 /**
843  * event_trigger_parse_num - parse and return the number param for a trigger
844  * @param: The param string
845  * @trigger_data: The trigger_data for the trigger
846  *
847  * Parse the :n (n = number of times the trigger should fire) param
848  * and set the count variable in the trigger_data to the parsed count.
849  *
850  * Return: 0 on success, errno otherwise
851  */
852 int event_trigger_parse_num(char *param,
853 			    struct event_trigger_data *trigger_data)
854 {
855 	char *number;
856 	int ret = 0;
857 
858 	if (param) {
859 		number = strsep(&param, ":");
860 
861 		if (!strlen(number))
862 			return -EINVAL;
863 
864 		/*
865 		 * We use the callback data field (which is a pointer)
866 		 * as our counter.
867 		 */
868 		ret = kstrtoul(number, 0, &trigger_data->count);
869 	}
870 
871 	return ret;
872 }
873 
874 /**
875  * event_trigger_set_filter - set an event trigger's filter
876  * @cmd_ops: The event_command operations for the trigger
877  * @file: The event file for the trigger's event
878  * @param: The string containing the filter
879  * @trigger_data: The trigger_data for the trigger
880  *
881  * Set the filter for the trigger.  If the filter is NULL, just return
882  * without error.
883  *
884  * Return: 0 on success, errno otherwise
885  */
886 int event_trigger_set_filter(struct event_command *cmd_ops,
887 			     struct trace_event_file *file,
888 			     char *param,
889 			     struct event_trigger_data *trigger_data)
890 {
891 	if (param && cmd_ops->set_filter)
892 		return cmd_ops->set_filter(param, trigger_data, file);
893 
894 	return 0;
895 }
896 
897 /**
898  * event_trigger_reset_filter - reset an event trigger's filter
899  * @cmd_ops: The event_command operations for the trigger
900  * @trigger_data: The trigger_data for the trigger
901  *
902  * Reset the filter for the trigger to no filter.
903  */
904 void event_trigger_reset_filter(struct event_command *cmd_ops,
905 				struct event_trigger_data *trigger_data)
906 {
907 	if (cmd_ops->set_filter)
908 		cmd_ops->set_filter(NULL, trigger_data, NULL);
909 }
910 
911 /**
912  * event_trigger_register - register an event trigger
913  * @cmd_ops: The event_command operations for the trigger
914  * @file: The event file for the trigger's event
915  * @glob: The trigger command string, with optional remove(!) operator
916  * @cmd: The cmd string
917  * @param: The param string
918  * @trigger_data: The trigger_data for the trigger
919  * @n_registered: optional outparam, the number of triggers registered
920  *
921  * Register an event trigger.  The @cmd_ops are used to call the
922  * cmd_ops->reg() function which actually does the registration. The
923  * cmd_ops->reg() function returns the number of triggers registered,
924  * which is assigned to n_registered, if n_registered is non-NULL.
925  *
926  * Return: 0 on success, errno otherwise
927  */
928 int event_trigger_register(struct event_command *cmd_ops,
929 			   struct trace_event_file *file,
930 			   char *glob,
931 			   char *cmd,
932 			   char *param,
933 			   struct event_trigger_data *trigger_data,
934 			   int *n_registered)
935 {
936 	int ret;
937 
938 	if (n_registered)
939 		*n_registered = 0;
940 
941 	ret = cmd_ops->reg(glob, trigger_data, file);
942 	/*
943 	 * The above returns on success the # of functions enabled,
944 	 * but if it didn't find any functions it returns zero.
945 	 * Consider no functions a failure too.
946 	 */
947 	if (!ret) {
948 		cmd_ops->unreg(glob, trigger_data, file);
949 		ret = -ENOENT;
950 	} else if (ret > 0) {
951 		if (n_registered)
952 			*n_registered = ret;
953 		/* Just return zero, not the number of enabled functions */
954 		ret = 0;
955 	}
956 
957 	return ret;
958 }
959 
960 /*
961  * End event trigger parsing helper functions.
962  */
963 
964 /**
965  * event_trigger_parse - Generic event_command @parse implementation
966  * @cmd_ops: The command ops, used for trigger registration
967  * @file: The trace_event_file associated with the event
968  * @glob: The raw string used to register the trigger
969  * @cmd: The cmd portion of the string used to register the trigger
970  * @param: The params portion of the string used to register the trigger
971  *
972  * Common implementation for event command parsing and trigger
973  * instantiation.
974  *
975  * Usually used directly as the @parse method in event command
976  * implementations.
977  *
978  * Return: 0 on success, errno otherwise
979  */
980 static int
981 event_trigger_parse(struct event_command *cmd_ops,
982 		    struct trace_event_file *file,
983 		    char *glob, char *cmd, char *param)
984 {
985 	struct event_trigger_data *trigger_data;
986 	struct event_trigger_ops *trigger_ops;
987 	char *trigger = NULL;
988 	char *number;
989 	int ret;
990 
991 	/* separate the trigger from the filter (t:n [if filter]) */
992 	if (param && isdigit(param[0])) {
993 		trigger = strsep(&param, " \t");
994 		if (param) {
995 			param = skip_spaces(param);
996 			if (!*param)
997 				param = NULL;
998 		}
999 	}
1000 
1001 	trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
1002 
1003 	ret = -ENOMEM;
1004 	trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
1005 	if (!trigger_data)
1006 		goto out;
1007 
1008 	trigger_data->count = -1;
1009 	trigger_data->ops = trigger_ops;
1010 	trigger_data->cmd_ops = cmd_ops;
1011 	trigger_data->private_data = file;
1012 	INIT_LIST_HEAD(&trigger_data->list);
1013 	INIT_LIST_HEAD(&trigger_data->named_list);
1014 
1015 	if (glob[0] == '!') {
1016 		cmd_ops->unreg(glob+1, trigger_data, file);
1017 		kfree(trigger_data);
1018 		ret = 0;
1019 		goto out;
1020 	}
1021 
1022 	if (trigger) {
1023 		number = strsep(&trigger, ":");
1024 
1025 		ret = -EINVAL;
1026 		if (!strlen(number))
1027 			goto out_free;
1028 
1029 		/*
1030 		 * We use the callback data field (which is a pointer)
1031 		 * as our counter.
1032 		 */
1033 		ret = kstrtoul(number, 0, &trigger_data->count);
1034 		if (ret)
1035 			goto out_free;
1036 	}
1037 
1038 	if (!param) /* if param is non-empty, it's supposed to be a filter */
1039 		goto out_reg;
1040 
1041 	if (!cmd_ops->set_filter)
1042 		goto out_reg;
1043 
1044 	ret = cmd_ops->set_filter(param, trigger_data, file);
1045 	if (ret < 0)
1046 		goto out_free;
1047 
1048  out_reg:
1049 	/* Up the trigger_data count to make sure reg doesn't free it on failure */
1050 	event_trigger_init(trigger_ops, trigger_data);
1051 	ret = cmd_ops->reg(glob, trigger_data, file);
1052 	/*
1053 	 * The above returns on success the # of functions enabled,
1054 	 * but if it didn't find any functions it returns zero.
1055 	 * Consider no functions a failure too.
1056 	 */
1057 	if (!ret) {
1058 		cmd_ops->unreg(glob, trigger_data, file);
1059 		ret = -ENOENT;
1060 	} else if (ret > 0)
1061 		ret = 0;
1062 
1063 	/* Down the counter of trigger_data or free it if not used anymore */
1064 	event_trigger_free(trigger_ops, trigger_data);
1065  out:
1066 	return ret;
1067 
1068  out_free:
1069 	if (cmd_ops->set_filter)
1070 		cmd_ops->set_filter(NULL, trigger_data, NULL);
1071 	kfree(trigger_data);
1072 	goto out;
1073 }
1074 
1075 /**
1076  * set_trigger_filter - Generic event_command @set_filter implementation
1077  * @filter_str: The filter string for the trigger, NULL to remove filter
1078  * @trigger_data: Trigger-specific data
1079  * @file: The trace_event_file associated with the event
1080  *
1081  * Common implementation for event command filter parsing and filter
1082  * instantiation.
1083  *
1084  * Usually used directly as the @set_filter method in event command
1085  * implementations.
1086  *
1087  * Also used to remove a filter (if filter_str = NULL).
1088  *
1089  * Return: 0 on success, errno otherwise
1090  */
1091 int set_trigger_filter(char *filter_str,
1092 		       struct event_trigger_data *trigger_data,
1093 		       struct trace_event_file *file)
1094 {
1095 	struct event_trigger_data *data = trigger_data;
1096 	struct event_filter *filter = NULL, *tmp;
1097 	int ret = -EINVAL;
1098 	char *s;
1099 
1100 	if (!filter_str) /* clear the current filter */
1101 		goto assign;
1102 
1103 	s = strsep(&filter_str, " \t");
1104 
1105 	if (!strlen(s) || strcmp(s, "if") != 0)
1106 		goto out;
1107 
1108 	if (!filter_str)
1109 		goto out;
1110 
1111 	/* The filter is for the 'trigger' event, not the triggered event */
1112 	ret = create_event_filter(file->tr, file->event_call,
1113 				  filter_str, false, &filter);
1114 	/*
1115 	 * If create_event_filter() fails, filter still needs to be freed.
1116 	 * Which the calling code will do with data->filter.
1117 	 */
1118  assign:
1119 	tmp = rcu_access_pointer(data->filter);
1120 
1121 	rcu_assign_pointer(data->filter, filter);
1122 
1123 	if (tmp) {
1124 		/* Make sure the call is done with the filter */
1125 		tracepoint_synchronize_unregister();
1126 		free_event_filter(tmp);
1127 	}
1128 
1129 	kfree(data->filter_str);
1130 	data->filter_str = NULL;
1131 
1132 	if (filter_str) {
1133 		data->filter_str = kstrdup(filter_str, GFP_KERNEL);
1134 		if (!data->filter_str) {
1135 			free_event_filter(rcu_access_pointer(data->filter));
1136 			data->filter = NULL;
1137 			ret = -ENOMEM;
1138 		}
1139 	}
1140  out:
1141 	return ret;
1142 }
1143 
1144 static LIST_HEAD(named_triggers);
1145 
1146 /**
1147  * find_named_trigger - Find the common named trigger associated with @name
1148  * @name: The name of the set of named triggers to find the common data for
1149  *
1150  * Named triggers are sets of triggers that share a common set of
1151  * trigger data.  The first named trigger registered with a given name
1152  * owns the common trigger data that the others subsequently
1153  * registered with the same name will reference.  This function
1154  * returns the common trigger data associated with that first
1155  * registered instance.
1156  *
1157  * Return: the common trigger data for the given named trigger on
1158  * success, NULL otherwise.
1159  */
1160 struct event_trigger_data *find_named_trigger(const char *name)
1161 {
1162 	struct event_trigger_data *data;
1163 
1164 	if (!name)
1165 		return NULL;
1166 
1167 	list_for_each_entry(data, &named_triggers, named_list) {
1168 		if (data->named_data)
1169 			continue;
1170 		if (strcmp(data->name, name) == 0)
1171 			return data;
1172 	}
1173 
1174 	return NULL;
1175 }
1176 
1177 /**
1178  * is_named_trigger - determine if a given trigger is a named trigger
1179  * @test: The trigger data to test
1180  *
1181  * Return: true if 'test' is a named trigger, false otherwise.
1182  */
1183 bool is_named_trigger(struct event_trigger_data *test)
1184 {
1185 	struct event_trigger_data *data;
1186 
1187 	list_for_each_entry(data, &named_triggers, named_list) {
1188 		if (test == data)
1189 			return true;
1190 	}
1191 
1192 	return false;
1193 }
1194 
1195 /**
1196  * save_named_trigger - save the trigger in the named trigger list
1197  * @name: The name of the named trigger set
1198  * @data: The trigger data to save
1199  *
1200  * Return: 0 if successful, negative error otherwise.
1201  */
1202 int save_named_trigger(const char *name, struct event_trigger_data *data)
1203 {
1204 	data->name = kstrdup(name, GFP_KERNEL);
1205 	if (!data->name)
1206 		return -ENOMEM;
1207 
1208 	list_add(&data->named_list, &named_triggers);
1209 
1210 	return 0;
1211 }
1212 
1213 /**
1214  * del_named_trigger - delete a trigger from the named trigger list
1215  * @data: The trigger data to delete
1216  */
1217 void del_named_trigger(struct event_trigger_data *data)
1218 {
1219 	kfree(data->name);
1220 	data->name = NULL;
1221 
1222 	list_del(&data->named_list);
1223 }
1224 
1225 static void __pause_named_trigger(struct event_trigger_data *data, bool pause)
1226 {
1227 	struct event_trigger_data *test;
1228 
1229 	list_for_each_entry(test, &named_triggers, named_list) {
1230 		if (strcmp(test->name, data->name) == 0) {
1231 			if (pause) {
1232 				test->paused_tmp = test->paused;
1233 				test->paused = true;
1234 			} else {
1235 				test->paused = test->paused_tmp;
1236 			}
1237 		}
1238 	}
1239 }
1240 
1241 /**
1242  * pause_named_trigger - Pause all named triggers with the same name
1243  * @data: The trigger data of a named trigger to pause
1244  *
1245  * Pauses a named trigger along with all other triggers having the
1246  * same name.  Because named triggers share a common set of data,
1247  * pausing only one is meaningless, so pausing one named trigger needs
1248  * to pause all triggers with the same name.
1249  */
1250 void pause_named_trigger(struct event_trigger_data *data)
1251 {
1252 	__pause_named_trigger(data, true);
1253 }
1254 
1255 /**
1256  * unpause_named_trigger - Un-pause all named triggers with the same name
1257  * @data: The trigger data of a named trigger to unpause
1258  *
1259  * Un-pauses a named trigger along with all other triggers having the
1260  * same name.  Because named triggers share a common set of data,
1261  * unpausing only one is meaningless, so unpausing one named trigger
1262  * needs to unpause all triggers with the same name.
1263  */
1264 void unpause_named_trigger(struct event_trigger_data *data)
1265 {
1266 	__pause_named_trigger(data, false);
1267 }
1268 
1269 /**
1270  * set_named_trigger_data - Associate common named trigger data
1271  * @data: The trigger data to associate
1272  * @named_data: The common named trigger to be associated
1273  *
1274  * Named triggers are sets of triggers that share a common set of
1275  * trigger data.  The first named trigger registered with a given name
1276  * owns the common trigger data that the others subsequently
1277  * registered with the same name will reference.  This function
1278  * associates the common trigger data from the first trigger with the
1279  * given trigger.
1280  */
1281 void set_named_trigger_data(struct event_trigger_data *data,
1282 			    struct event_trigger_data *named_data)
1283 {
1284 	data->named_data = named_data;
1285 }
1286 
1287 struct event_trigger_data *
1288 get_named_trigger_data(struct event_trigger_data *data)
1289 {
1290 	return data->named_data;
1291 }
1292 
1293 static void
1294 traceon_trigger(struct event_trigger_data *data,
1295 		struct trace_buffer *buffer, void *rec,
1296 		struct ring_buffer_event *event)
1297 {
1298 	struct trace_event_file *file = data->private_data;
1299 
1300 	if (file) {
1301 		if (tracer_tracing_is_on(file->tr))
1302 			return;
1303 
1304 		tracer_tracing_on(file->tr);
1305 		return;
1306 	}
1307 
1308 	if (tracing_is_on())
1309 		return;
1310 
1311 	tracing_on();
1312 }
1313 
1314 static void
1315 traceon_count_trigger(struct event_trigger_data *data,
1316 		      struct trace_buffer *buffer, void *rec,
1317 		      struct ring_buffer_event *event)
1318 {
1319 	struct trace_event_file *file = data->private_data;
1320 
1321 	if (file) {
1322 		if (tracer_tracing_is_on(file->tr))
1323 			return;
1324 	} else {
1325 		if (tracing_is_on())
1326 			return;
1327 	}
1328 
1329 	if (!data->count)
1330 		return;
1331 
1332 	if (data->count != -1)
1333 		(data->count)--;
1334 
1335 	if (file)
1336 		tracer_tracing_on(file->tr);
1337 	else
1338 		tracing_on();
1339 }
1340 
1341 static void
1342 traceoff_trigger(struct event_trigger_data *data,
1343 		 struct trace_buffer *buffer, void *rec,
1344 		 struct ring_buffer_event *event)
1345 {
1346 	struct trace_event_file *file = data->private_data;
1347 
1348 	if (file) {
1349 		if (!tracer_tracing_is_on(file->tr))
1350 			return;
1351 
1352 		tracer_tracing_off(file->tr);
1353 		return;
1354 	}
1355 
1356 	if (!tracing_is_on())
1357 		return;
1358 
1359 	tracing_off();
1360 }
1361 
1362 static void
1363 traceoff_count_trigger(struct event_trigger_data *data,
1364 		       struct trace_buffer *buffer, void *rec,
1365 		       struct ring_buffer_event *event)
1366 {
1367 	struct trace_event_file *file = data->private_data;
1368 
1369 	if (file) {
1370 		if (!tracer_tracing_is_on(file->tr))
1371 			return;
1372 	} else {
1373 		if (!tracing_is_on())
1374 			return;
1375 	}
1376 
1377 	if (!data->count)
1378 		return;
1379 
1380 	if (data->count != -1)
1381 		(data->count)--;
1382 
1383 	if (file)
1384 		tracer_tracing_off(file->tr);
1385 	else
1386 		tracing_off();
1387 }
1388 
1389 static int
1390 traceon_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1391 		      struct event_trigger_data *data)
1392 {
1393 	return event_trigger_print("traceon", m, (void *)data->count,
1394 				   data->filter_str);
1395 }
1396 
1397 static int
1398 traceoff_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1399 		       struct event_trigger_data *data)
1400 {
1401 	return event_trigger_print("traceoff", m, (void *)data->count,
1402 				   data->filter_str);
1403 }
1404 
1405 static struct event_trigger_ops traceon_trigger_ops = {
1406 	.trigger		= traceon_trigger,
1407 	.print			= traceon_trigger_print,
1408 	.init			= event_trigger_init,
1409 	.free			= event_trigger_free,
1410 };
1411 
1412 static struct event_trigger_ops traceon_count_trigger_ops = {
1413 	.trigger		= traceon_count_trigger,
1414 	.print			= traceon_trigger_print,
1415 	.init			= event_trigger_init,
1416 	.free			= event_trigger_free,
1417 };
1418 
1419 static struct event_trigger_ops traceoff_trigger_ops = {
1420 	.trigger		= traceoff_trigger,
1421 	.print			= traceoff_trigger_print,
1422 	.init			= event_trigger_init,
1423 	.free			= event_trigger_free,
1424 };
1425 
1426 static struct event_trigger_ops traceoff_count_trigger_ops = {
1427 	.trigger		= traceoff_count_trigger,
1428 	.print			= traceoff_trigger_print,
1429 	.init			= event_trigger_init,
1430 	.free			= event_trigger_free,
1431 };
1432 
1433 static struct event_trigger_ops *
1434 onoff_get_trigger_ops(char *cmd, char *param)
1435 {
1436 	struct event_trigger_ops *ops;
1437 
1438 	/* we register both traceon and traceoff to this callback */
1439 	if (strcmp(cmd, "traceon") == 0)
1440 		ops = param ? &traceon_count_trigger_ops :
1441 			&traceon_trigger_ops;
1442 	else
1443 		ops = param ? &traceoff_count_trigger_ops :
1444 			&traceoff_trigger_ops;
1445 
1446 	return ops;
1447 }
1448 
1449 static struct event_command trigger_traceon_cmd = {
1450 	.name			= "traceon",
1451 	.trigger_type		= ETT_TRACE_ONOFF,
1452 	.parse			= event_trigger_parse,
1453 	.reg			= register_trigger,
1454 	.unreg			= unregister_trigger,
1455 	.get_trigger_ops	= onoff_get_trigger_ops,
1456 	.set_filter		= set_trigger_filter,
1457 };
1458 
1459 static struct event_command trigger_traceoff_cmd = {
1460 	.name			= "traceoff",
1461 	.trigger_type		= ETT_TRACE_ONOFF,
1462 	.flags			= EVENT_CMD_FL_POST_TRIGGER,
1463 	.parse			= event_trigger_parse,
1464 	.reg			= register_trigger,
1465 	.unreg			= unregister_trigger,
1466 	.get_trigger_ops	= onoff_get_trigger_ops,
1467 	.set_filter		= set_trigger_filter,
1468 };
1469 
1470 #ifdef CONFIG_TRACER_SNAPSHOT
1471 static void
1472 snapshot_trigger(struct event_trigger_data *data,
1473 		 struct trace_buffer *buffer, void *rec,
1474 		 struct ring_buffer_event *event)
1475 {
1476 	struct trace_event_file *file = data->private_data;
1477 
1478 	if (file)
1479 		tracing_snapshot_instance(file->tr);
1480 	else
1481 		tracing_snapshot();
1482 }
1483 
1484 static void
1485 snapshot_count_trigger(struct event_trigger_data *data,
1486 		       struct trace_buffer *buffer, void *rec,
1487 		       struct ring_buffer_event *event)
1488 {
1489 	if (!data->count)
1490 		return;
1491 
1492 	if (data->count != -1)
1493 		(data->count)--;
1494 
1495 	snapshot_trigger(data, buffer, rec, event);
1496 }
1497 
1498 static int
1499 register_snapshot_trigger(char *glob,
1500 			  struct event_trigger_data *data,
1501 			  struct trace_event_file *file)
1502 {
1503 	if (tracing_alloc_snapshot_instance(file->tr) != 0)
1504 		return 0;
1505 
1506 	return register_trigger(glob, data, file);
1507 }
1508 
1509 static int
1510 snapshot_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1511 		       struct event_trigger_data *data)
1512 {
1513 	return event_trigger_print("snapshot", m, (void *)data->count,
1514 				   data->filter_str);
1515 }
1516 
1517 static struct event_trigger_ops snapshot_trigger_ops = {
1518 	.trigger		= snapshot_trigger,
1519 	.print			= snapshot_trigger_print,
1520 	.init			= event_trigger_init,
1521 	.free			= event_trigger_free,
1522 };
1523 
1524 static struct event_trigger_ops snapshot_count_trigger_ops = {
1525 	.trigger		= snapshot_count_trigger,
1526 	.print			= snapshot_trigger_print,
1527 	.init			= event_trigger_init,
1528 	.free			= event_trigger_free,
1529 };
1530 
1531 static struct event_trigger_ops *
1532 snapshot_get_trigger_ops(char *cmd, char *param)
1533 {
1534 	return param ? &snapshot_count_trigger_ops : &snapshot_trigger_ops;
1535 }
1536 
1537 static struct event_command trigger_snapshot_cmd = {
1538 	.name			= "snapshot",
1539 	.trigger_type		= ETT_SNAPSHOT,
1540 	.parse			= event_trigger_parse,
1541 	.reg			= register_snapshot_trigger,
1542 	.unreg			= unregister_trigger,
1543 	.get_trigger_ops	= snapshot_get_trigger_ops,
1544 	.set_filter		= set_trigger_filter,
1545 };
1546 
1547 static __init int register_trigger_snapshot_cmd(void)
1548 {
1549 	int ret;
1550 
1551 	ret = register_event_command(&trigger_snapshot_cmd);
1552 	WARN_ON(ret < 0);
1553 
1554 	return ret;
1555 }
1556 #else
1557 static __init int register_trigger_snapshot_cmd(void) { return 0; }
1558 #endif /* CONFIG_TRACER_SNAPSHOT */
1559 
1560 #ifdef CONFIG_STACKTRACE
1561 #ifdef CONFIG_UNWINDER_ORC
1562 /* Skip 2:
1563  *   event_triggers_post_call()
1564  *   trace_event_raw_event_xxx()
1565  */
1566 # define STACK_SKIP 2
1567 #else
1568 /*
1569  * Skip 4:
1570  *   stacktrace_trigger()
1571  *   event_triggers_post_call()
1572  *   trace_event_buffer_commit()
1573  *   trace_event_raw_event_xxx()
1574  */
1575 #define STACK_SKIP 4
1576 #endif
1577 
1578 static void
1579 stacktrace_trigger(struct event_trigger_data *data,
1580 		   struct trace_buffer *buffer,  void *rec,
1581 		   struct ring_buffer_event *event)
1582 {
1583 	struct trace_event_file *file = data->private_data;
1584 
1585 	if (file)
1586 		__trace_stack(file->tr, tracing_gen_ctx(), STACK_SKIP);
1587 	else
1588 		trace_dump_stack(STACK_SKIP);
1589 }
1590 
1591 static void
1592 stacktrace_count_trigger(struct event_trigger_data *data,
1593 			 struct trace_buffer *buffer, void *rec,
1594 			 struct ring_buffer_event *event)
1595 {
1596 	if (!data->count)
1597 		return;
1598 
1599 	if (data->count != -1)
1600 		(data->count)--;
1601 
1602 	stacktrace_trigger(data, buffer, rec, event);
1603 }
1604 
1605 static int
1606 stacktrace_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1607 			 struct event_trigger_data *data)
1608 {
1609 	return event_trigger_print("stacktrace", m, (void *)data->count,
1610 				   data->filter_str);
1611 }
1612 
1613 static struct event_trigger_ops stacktrace_trigger_ops = {
1614 	.trigger		= stacktrace_trigger,
1615 	.print			= stacktrace_trigger_print,
1616 	.init			= event_trigger_init,
1617 	.free			= event_trigger_free,
1618 };
1619 
1620 static struct event_trigger_ops stacktrace_count_trigger_ops = {
1621 	.trigger		= stacktrace_count_trigger,
1622 	.print			= stacktrace_trigger_print,
1623 	.init			= event_trigger_init,
1624 	.free			= event_trigger_free,
1625 };
1626 
1627 static struct event_trigger_ops *
1628 stacktrace_get_trigger_ops(char *cmd, char *param)
1629 {
1630 	return param ? &stacktrace_count_trigger_ops : &stacktrace_trigger_ops;
1631 }
1632 
1633 static struct event_command trigger_stacktrace_cmd = {
1634 	.name			= "stacktrace",
1635 	.trigger_type		= ETT_STACKTRACE,
1636 	.flags			= EVENT_CMD_FL_POST_TRIGGER,
1637 	.parse			= event_trigger_parse,
1638 	.reg			= register_trigger,
1639 	.unreg			= unregister_trigger,
1640 	.get_trigger_ops	= stacktrace_get_trigger_ops,
1641 	.set_filter		= set_trigger_filter,
1642 };
1643 
1644 static __init int register_trigger_stacktrace_cmd(void)
1645 {
1646 	int ret;
1647 
1648 	ret = register_event_command(&trigger_stacktrace_cmd);
1649 	WARN_ON(ret < 0);
1650 
1651 	return ret;
1652 }
1653 #else
1654 static __init int register_trigger_stacktrace_cmd(void) { return 0; }
1655 #endif /* CONFIG_STACKTRACE */
1656 
1657 static __init void unregister_trigger_traceon_traceoff_cmds(void)
1658 {
1659 	unregister_event_command(&trigger_traceon_cmd);
1660 	unregister_event_command(&trigger_traceoff_cmd);
1661 }
1662 
1663 static void
1664 event_enable_trigger(struct event_trigger_data *data,
1665 		     struct trace_buffer *buffer,  void *rec,
1666 		     struct ring_buffer_event *event)
1667 {
1668 	struct enable_trigger_data *enable_data = data->private_data;
1669 
1670 	if (enable_data->enable)
1671 		clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
1672 	else
1673 		set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
1674 }
1675 
1676 static void
1677 event_enable_count_trigger(struct event_trigger_data *data,
1678 			   struct trace_buffer *buffer,  void *rec,
1679 			   struct ring_buffer_event *event)
1680 {
1681 	struct enable_trigger_data *enable_data = data->private_data;
1682 
1683 	if (!data->count)
1684 		return;
1685 
1686 	/* Skip if the event is in a state we want to switch to */
1687 	if (enable_data->enable == !(enable_data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
1688 		return;
1689 
1690 	if (data->count != -1)
1691 		(data->count)--;
1692 
1693 	event_enable_trigger(data, buffer, rec, event);
1694 }
1695 
1696 int event_enable_trigger_print(struct seq_file *m,
1697 			       struct event_trigger_ops *ops,
1698 			       struct event_trigger_data *data)
1699 {
1700 	struct enable_trigger_data *enable_data = data->private_data;
1701 
1702 	seq_printf(m, "%s:%s:%s",
1703 		   enable_data->hist ?
1704 		   (enable_data->enable ? ENABLE_HIST_STR : DISABLE_HIST_STR) :
1705 		   (enable_data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR),
1706 		   enable_data->file->event_call->class->system,
1707 		   trace_event_name(enable_data->file->event_call));
1708 
1709 	if (data->count == -1)
1710 		seq_puts(m, ":unlimited");
1711 	else
1712 		seq_printf(m, ":count=%ld", data->count);
1713 
1714 	if (data->filter_str)
1715 		seq_printf(m, " if %s\n", data->filter_str);
1716 	else
1717 		seq_putc(m, '\n');
1718 
1719 	return 0;
1720 }
1721 
1722 void event_enable_trigger_free(struct event_trigger_ops *ops,
1723 			       struct event_trigger_data *data)
1724 {
1725 	struct enable_trigger_data *enable_data = data->private_data;
1726 
1727 	if (WARN_ON_ONCE(data->ref <= 0))
1728 		return;
1729 
1730 	data->ref--;
1731 	if (!data->ref) {
1732 		/* Remove the SOFT_MODE flag */
1733 		trace_event_enable_disable(enable_data->file, 0, 1);
1734 		trace_event_put_ref(enable_data->file->event_call);
1735 		trigger_data_free(data);
1736 		kfree(enable_data);
1737 	}
1738 }
1739 
1740 static struct event_trigger_ops event_enable_trigger_ops = {
1741 	.trigger		= event_enable_trigger,
1742 	.print			= event_enable_trigger_print,
1743 	.init			= event_trigger_init,
1744 	.free			= event_enable_trigger_free,
1745 };
1746 
1747 static struct event_trigger_ops event_enable_count_trigger_ops = {
1748 	.trigger		= event_enable_count_trigger,
1749 	.print			= event_enable_trigger_print,
1750 	.init			= event_trigger_init,
1751 	.free			= event_enable_trigger_free,
1752 };
1753 
1754 static struct event_trigger_ops event_disable_trigger_ops = {
1755 	.trigger		= event_enable_trigger,
1756 	.print			= event_enable_trigger_print,
1757 	.init			= event_trigger_init,
1758 	.free			= event_enable_trigger_free,
1759 };
1760 
1761 static struct event_trigger_ops event_disable_count_trigger_ops = {
1762 	.trigger		= event_enable_count_trigger,
1763 	.print			= event_enable_trigger_print,
1764 	.init			= event_trigger_init,
1765 	.free			= event_enable_trigger_free,
1766 };
1767 
1768 int event_enable_trigger_parse(struct event_command *cmd_ops,
1769 			       struct trace_event_file *file,
1770 			       char *glob, char *cmd, char *param)
1771 {
1772 	struct trace_event_file *event_enable_file;
1773 	struct enable_trigger_data *enable_data;
1774 	struct event_trigger_data *trigger_data;
1775 	struct event_trigger_ops *trigger_ops;
1776 	struct trace_array *tr = file->tr;
1777 	const char *system;
1778 	const char *event;
1779 	bool hist = false;
1780 	char *trigger;
1781 	char *number;
1782 	bool enable;
1783 	int ret;
1784 
1785 	if (!param)
1786 		return -EINVAL;
1787 
1788 	/* separate the trigger from the filter (s:e:n [if filter]) */
1789 	trigger = strsep(&param, " \t");
1790 	if (!trigger)
1791 		return -EINVAL;
1792 	if (param) {
1793 		param = skip_spaces(param);
1794 		if (!*param)
1795 			param = NULL;
1796 	}
1797 
1798 	system = strsep(&trigger, ":");
1799 	if (!trigger)
1800 		return -EINVAL;
1801 
1802 	event = strsep(&trigger, ":");
1803 
1804 	ret = -EINVAL;
1805 	event_enable_file = find_event_file(tr, system, event);
1806 	if (!event_enable_file)
1807 		goto out;
1808 
1809 #ifdef CONFIG_HIST_TRIGGERS
1810 	hist = ((strcmp(cmd, ENABLE_HIST_STR) == 0) ||
1811 		(strcmp(cmd, DISABLE_HIST_STR) == 0));
1812 
1813 	enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
1814 		  (strcmp(cmd, ENABLE_HIST_STR) == 0));
1815 #else
1816 	enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1817 #endif
1818 	trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
1819 
1820 	ret = -ENOMEM;
1821 	trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
1822 	if (!trigger_data)
1823 		goto out;
1824 
1825 	enable_data = kzalloc(sizeof(*enable_data), GFP_KERNEL);
1826 	if (!enable_data) {
1827 		kfree(trigger_data);
1828 		goto out;
1829 	}
1830 
1831 	trigger_data->count = -1;
1832 	trigger_data->ops = trigger_ops;
1833 	trigger_data->cmd_ops = cmd_ops;
1834 	INIT_LIST_HEAD(&trigger_data->list);
1835 	RCU_INIT_POINTER(trigger_data->filter, NULL);
1836 
1837 	enable_data->hist = hist;
1838 	enable_data->enable = enable;
1839 	enable_data->file = event_enable_file;
1840 	trigger_data->private_data = enable_data;
1841 
1842 	if (glob[0] == '!') {
1843 		cmd_ops->unreg(glob+1, trigger_data, file);
1844 		kfree(trigger_data);
1845 		kfree(enable_data);
1846 		ret = 0;
1847 		goto out;
1848 	}
1849 
1850 	/* Up the trigger_data count to make sure nothing frees it on failure */
1851 	event_trigger_init(trigger_ops, trigger_data);
1852 
1853 	if (trigger) {
1854 		number = strsep(&trigger, ":");
1855 
1856 		ret = -EINVAL;
1857 		if (!strlen(number))
1858 			goto out_free;
1859 
1860 		/*
1861 		 * We use the callback data field (which is a pointer)
1862 		 * as our counter.
1863 		 */
1864 		ret = kstrtoul(number, 0, &trigger_data->count);
1865 		if (ret)
1866 			goto out_free;
1867 	}
1868 
1869 	if (!param) /* if param is non-empty, it's supposed to be a filter */
1870 		goto out_reg;
1871 
1872 	if (!cmd_ops->set_filter)
1873 		goto out_reg;
1874 
1875 	ret = cmd_ops->set_filter(param, trigger_data, file);
1876 	if (ret < 0)
1877 		goto out_free;
1878 
1879  out_reg:
1880 	/* Don't let event modules unload while probe registered */
1881 	ret = trace_event_try_get_ref(event_enable_file->event_call);
1882 	if (!ret) {
1883 		ret = -EBUSY;
1884 		goto out_free;
1885 	}
1886 
1887 	ret = trace_event_enable_disable(event_enable_file, 1, 1);
1888 	if (ret < 0)
1889 		goto out_put;
1890 	ret = cmd_ops->reg(glob, trigger_data, file);
1891 	/*
1892 	 * The above returns on success the # of functions enabled,
1893 	 * but if it didn't find any functions it returns zero.
1894 	 * Consider no functions a failure too.
1895 	 */
1896 	if (!ret) {
1897 		ret = -ENOENT;
1898 		goto out_disable;
1899 	} else if (ret < 0)
1900 		goto out_disable;
1901 	/* Just return zero, not the number of enabled functions */
1902 	ret = 0;
1903 	event_trigger_free(trigger_ops, trigger_data);
1904  out:
1905 	return ret;
1906 
1907  out_disable:
1908 	trace_event_enable_disable(event_enable_file, 0, 1);
1909  out_put:
1910 	trace_event_put_ref(event_enable_file->event_call);
1911  out_free:
1912 	if (cmd_ops->set_filter)
1913 		cmd_ops->set_filter(NULL, trigger_data, NULL);
1914 	event_trigger_free(trigger_ops, trigger_data);
1915 	kfree(enable_data);
1916 	goto out;
1917 }
1918 
1919 int event_enable_register_trigger(char *glob,
1920 				  struct event_trigger_data *data,
1921 				  struct trace_event_file *file)
1922 {
1923 	struct enable_trigger_data *enable_data = data->private_data;
1924 	struct enable_trigger_data *test_enable_data;
1925 	struct event_trigger_data *test;
1926 	int ret = 0;
1927 
1928 	lockdep_assert_held(&event_mutex);
1929 
1930 	list_for_each_entry(test, &file->triggers, list) {
1931 		test_enable_data = test->private_data;
1932 		if (test_enable_data &&
1933 		    (test->cmd_ops->trigger_type ==
1934 		     data->cmd_ops->trigger_type) &&
1935 		    (test_enable_data->file == enable_data->file)) {
1936 			ret = -EEXIST;
1937 			goto out;
1938 		}
1939 	}
1940 
1941 	if (data->ops->init) {
1942 		ret = data->ops->init(data->ops, data);
1943 		if (ret < 0)
1944 			goto out;
1945 	}
1946 
1947 	list_add_rcu(&data->list, &file->triggers);
1948 	ret++;
1949 
1950 	update_cond_flag(file);
1951 	if (trace_event_trigger_enable_disable(file, 1) < 0) {
1952 		list_del_rcu(&data->list);
1953 		update_cond_flag(file);
1954 		ret--;
1955 	}
1956 out:
1957 	return ret;
1958 }
1959 
1960 void event_enable_unregister_trigger(char *glob,
1961 				     struct event_trigger_data *test,
1962 				     struct trace_event_file *file)
1963 {
1964 	struct enable_trigger_data *test_enable_data = test->private_data;
1965 	struct enable_trigger_data *enable_data;
1966 	struct event_trigger_data *data;
1967 	bool unregistered = false;
1968 
1969 	lockdep_assert_held(&event_mutex);
1970 
1971 	list_for_each_entry(data, &file->triggers, list) {
1972 		enable_data = data->private_data;
1973 		if (enable_data &&
1974 		    (data->cmd_ops->trigger_type ==
1975 		     test->cmd_ops->trigger_type) &&
1976 		    (enable_data->file == test_enable_data->file)) {
1977 			unregistered = true;
1978 			list_del_rcu(&data->list);
1979 			trace_event_trigger_enable_disable(file, 0);
1980 			update_cond_flag(file);
1981 			break;
1982 		}
1983 	}
1984 
1985 	if (unregistered && data->ops->free)
1986 		data->ops->free(data->ops, data);
1987 }
1988 
1989 static struct event_trigger_ops *
1990 event_enable_get_trigger_ops(char *cmd, char *param)
1991 {
1992 	struct event_trigger_ops *ops;
1993 	bool enable;
1994 
1995 #ifdef CONFIG_HIST_TRIGGERS
1996 	enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
1997 		  (strcmp(cmd, ENABLE_HIST_STR) == 0));
1998 #else
1999 	enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2000 #endif
2001 	if (enable)
2002 		ops = param ? &event_enable_count_trigger_ops :
2003 			&event_enable_trigger_ops;
2004 	else
2005 		ops = param ? &event_disable_count_trigger_ops :
2006 			&event_disable_trigger_ops;
2007 
2008 	return ops;
2009 }
2010 
2011 static struct event_command trigger_enable_cmd = {
2012 	.name			= ENABLE_EVENT_STR,
2013 	.trigger_type		= ETT_EVENT_ENABLE,
2014 	.parse			= event_enable_trigger_parse,
2015 	.reg			= event_enable_register_trigger,
2016 	.unreg			= event_enable_unregister_trigger,
2017 	.get_trigger_ops	= event_enable_get_trigger_ops,
2018 	.set_filter		= set_trigger_filter,
2019 };
2020 
2021 static struct event_command trigger_disable_cmd = {
2022 	.name			= DISABLE_EVENT_STR,
2023 	.trigger_type		= ETT_EVENT_ENABLE,
2024 	.parse			= event_enable_trigger_parse,
2025 	.reg			= event_enable_register_trigger,
2026 	.unreg			= event_enable_unregister_trigger,
2027 	.get_trigger_ops	= event_enable_get_trigger_ops,
2028 	.set_filter		= set_trigger_filter,
2029 };
2030 
2031 static __init void unregister_trigger_enable_disable_cmds(void)
2032 {
2033 	unregister_event_command(&trigger_enable_cmd);
2034 	unregister_event_command(&trigger_disable_cmd);
2035 }
2036 
2037 static __init int register_trigger_enable_disable_cmds(void)
2038 {
2039 	int ret;
2040 
2041 	ret = register_event_command(&trigger_enable_cmd);
2042 	if (WARN_ON(ret < 0))
2043 		return ret;
2044 	ret = register_event_command(&trigger_disable_cmd);
2045 	if (WARN_ON(ret < 0))
2046 		unregister_trigger_enable_disable_cmds();
2047 
2048 	return ret;
2049 }
2050 
2051 static __init int register_trigger_traceon_traceoff_cmds(void)
2052 {
2053 	int ret;
2054 
2055 	ret = register_event_command(&trigger_traceon_cmd);
2056 	if (WARN_ON(ret < 0))
2057 		return ret;
2058 	ret = register_event_command(&trigger_traceoff_cmd);
2059 	if (WARN_ON(ret < 0))
2060 		unregister_trigger_traceon_traceoff_cmds();
2061 
2062 	return ret;
2063 }
2064 
2065 __init int register_trigger_cmds(void)
2066 {
2067 	register_trigger_traceon_traceoff_cmds();
2068 	register_trigger_snapshot_cmd();
2069 	register_trigger_stacktrace_cmd();
2070 	register_trigger_enable_disable_cmds();
2071 	register_trigger_hist_enable_disable_cmds();
2072 	register_trigger_hist_cmd();
2073 
2074 	return 0;
2075 }
2076