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