xref: /linux/kernel/trace/trace_events_trigger.c (revision fab89a09c86f948adfc7e20a7d608bd9f323bbe1)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * trace_events_trigger - trace event triggers
4  *
5  * Copyright (C) 2013 Tom Zanussi <tom.zanussi@linux.intel.com>
6  */
7 
8 #include <linux/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 				lockdep_is_held(&event_mutex)) {
147 		if (data->flags & EVENT_TRIGGER_FL_PROBE)
148 			continue;
149 		return true;
150 	}
151 	return false;
152 }
153 
154 static void *trigger_start(struct seq_file *m, loff_t *pos)
155 {
156 	struct trace_event_file *event_file;
157 
158 	/* ->stop() is called even if ->start() fails */
159 	mutex_lock(&event_mutex);
160 	event_file = event_file_data(m->private);
161 	if (unlikely(!event_file))
162 		return ERR_PTR(-ENODEV);
163 
164 	if (list_empty(&event_file->triggers) || !check_user_trigger(event_file))
165 		return *pos == 0 ? SHOW_AVAILABLE_TRIGGERS : NULL;
166 
167 	return seq_list_start(&event_file->triggers, *pos);
168 }
169 
170 static void trigger_stop(struct seq_file *m, void *t)
171 {
172 	mutex_unlock(&event_mutex);
173 }
174 
175 static int trigger_show(struct seq_file *m, void *v)
176 {
177 	struct event_trigger_data *data;
178 	struct event_command *p;
179 
180 	if (v == SHOW_AVAILABLE_TRIGGERS) {
181 		seq_puts(m, "# Available triggers:\n");
182 		seq_putc(m, '#');
183 		mutex_lock(&trigger_cmd_mutex);
184 		list_for_each_entry_reverse(p, &trigger_commands, list)
185 			seq_printf(m, " %s", p->name);
186 		seq_putc(m, '\n');
187 		mutex_unlock(&trigger_cmd_mutex);
188 		return 0;
189 	}
190 
191 	data = list_entry(v, struct event_trigger_data, list);
192 	data->ops->print(m, data);
193 
194 	return 0;
195 }
196 
197 static const struct seq_operations event_triggers_seq_ops = {
198 	.start = trigger_start,
199 	.next = trigger_next,
200 	.stop = trigger_stop,
201 	.show = trigger_show,
202 };
203 
204 static int event_trigger_regex_open(struct inode *inode, struct file *file)
205 {
206 	int ret;
207 
208 	ret = security_locked_down(LOCKDOWN_TRACEFS);
209 	if (ret)
210 		return ret;
211 
212 	mutex_lock(&event_mutex);
213 
214 	if (unlikely(!event_file_data(file))) {
215 		mutex_unlock(&event_mutex);
216 		return -ENODEV;
217 	}
218 
219 	if ((file->f_mode & FMODE_WRITE) &&
220 	    (file->f_flags & O_TRUNC)) {
221 		struct trace_event_file *event_file;
222 		struct event_command *p;
223 
224 		event_file = event_file_data(file);
225 
226 		list_for_each_entry(p, &trigger_commands, list) {
227 			if (p->unreg_all)
228 				p->unreg_all(event_file);
229 		}
230 	}
231 
232 	if (file->f_mode & FMODE_READ) {
233 		ret = seq_open(file, &event_triggers_seq_ops);
234 		if (!ret) {
235 			struct seq_file *m = file->private_data;
236 			m->private = file;
237 		}
238 	}
239 
240 	mutex_unlock(&event_mutex);
241 
242 	return ret;
243 }
244 
245 int trigger_process_regex(struct trace_event_file *file, char *buff)
246 {
247 	char *command, *next;
248 	struct event_command *p;
249 	int ret = -EINVAL;
250 
251 	next = buff = skip_spaces(buff);
252 	command = strsep(&next, ": \t");
253 	if (next) {
254 		next = skip_spaces(next);
255 		if (!*next)
256 			next = NULL;
257 	}
258 	command = (command[0] != '!') ? command : command + 1;
259 
260 	mutex_lock(&trigger_cmd_mutex);
261 	list_for_each_entry(p, &trigger_commands, list) {
262 		if (strcmp(p->name, command) == 0) {
263 			ret = p->parse(p, file, buff, command, next);
264 			goto out_unlock;
265 		}
266 	}
267  out_unlock:
268 	mutex_unlock(&trigger_cmd_mutex);
269 
270 	return ret;
271 }
272 
273 static ssize_t event_trigger_regex_write(struct file *file,
274 					 const char __user *ubuf,
275 					 size_t cnt, loff_t *ppos)
276 {
277 	struct trace_event_file *event_file;
278 	ssize_t ret;
279 	char *buf;
280 
281 	if (!cnt)
282 		return 0;
283 
284 	if (cnt >= PAGE_SIZE)
285 		return -EINVAL;
286 
287 	buf = memdup_user_nul(ubuf, cnt);
288 	if (IS_ERR(buf))
289 		return PTR_ERR(buf);
290 
291 	strim(buf);
292 
293 	mutex_lock(&event_mutex);
294 	event_file = event_file_data(file);
295 	if (unlikely(!event_file)) {
296 		mutex_unlock(&event_mutex);
297 		kfree(buf);
298 		return -ENODEV;
299 	}
300 	ret = trigger_process_regex(event_file, buf);
301 	mutex_unlock(&event_mutex);
302 
303 	kfree(buf);
304 	if (ret < 0)
305 		goto out;
306 
307 	*ppos += cnt;
308 	ret = cnt;
309  out:
310 	return ret;
311 }
312 
313 static int event_trigger_regex_release(struct inode *inode, struct file *file)
314 {
315 	mutex_lock(&event_mutex);
316 
317 	if (file->f_mode & FMODE_READ)
318 		seq_release(inode, file);
319 
320 	mutex_unlock(&event_mutex);
321 
322 	return 0;
323 }
324 
325 static ssize_t
326 event_trigger_write(struct file *filp, const char __user *ubuf,
327 		    size_t cnt, loff_t *ppos)
328 {
329 	return event_trigger_regex_write(filp, ubuf, cnt, ppos);
330 }
331 
332 static int
333 event_trigger_open(struct inode *inode, struct file *filp)
334 {
335 	/* Checks for tracefs lockdown */
336 	return event_trigger_regex_open(inode, filp);
337 }
338 
339 static int
340 event_trigger_release(struct inode *inode, struct file *file)
341 {
342 	return event_trigger_regex_release(inode, file);
343 }
344 
345 const struct file_operations event_trigger_fops = {
346 	.open = event_trigger_open,
347 	.read = seq_read,
348 	.write = event_trigger_write,
349 	.llseek = tracing_lseek,
350 	.release = event_trigger_release,
351 };
352 
353 /*
354  * Currently we only register event commands from __init, so mark this
355  * __init too.
356  */
357 __init int register_event_command(struct event_command *cmd)
358 {
359 	struct event_command *p;
360 	int ret = 0;
361 
362 	mutex_lock(&trigger_cmd_mutex);
363 	list_for_each_entry(p, &trigger_commands, list) {
364 		if (strcmp(cmd->name, p->name) == 0) {
365 			ret = -EBUSY;
366 			goto out_unlock;
367 		}
368 	}
369 	list_add(&cmd->list, &trigger_commands);
370  out_unlock:
371 	mutex_unlock(&trigger_cmd_mutex);
372 
373 	return ret;
374 }
375 
376 /*
377  * Currently we only unregister event commands from __init, so mark
378  * this __init too.
379  */
380 __init int unregister_event_command(struct event_command *cmd)
381 {
382 	struct event_command *p, *n;
383 	int ret = -ENODEV;
384 
385 	mutex_lock(&trigger_cmd_mutex);
386 	list_for_each_entry_safe(p, n, &trigger_commands, list) {
387 		if (strcmp(cmd->name, p->name) == 0) {
388 			ret = 0;
389 			list_del_init(&p->list);
390 			goto out_unlock;
391 		}
392 	}
393  out_unlock:
394 	mutex_unlock(&trigger_cmd_mutex);
395 
396 	return ret;
397 }
398 
399 /**
400  * event_trigger_print - Generic event_trigger_ops @print implementation
401  * @name: The name of the event trigger
402  * @m: The seq_file being printed to
403  * @data: Trigger-specific data
404  * @filter_str: filter_str to print, if present
405  *
406  * Common implementation for event triggers to print themselves.
407  *
408  * Usually wrapped by a function that simply sets the @name of the
409  * trigger command and then invokes this.
410  *
411  * Return: 0 on success, errno otherwise
412  */
413 static int
414 event_trigger_print(const char *name, struct seq_file *m,
415 		    void *data, char *filter_str)
416 {
417 	long count = (long)data;
418 
419 	seq_puts(m, name);
420 
421 	if (count == -1)
422 		seq_puts(m, ":unlimited");
423 	else
424 		seq_printf(m, ":count=%ld", count);
425 
426 	if (filter_str)
427 		seq_printf(m, " if %s\n", filter_str);
428 	else
429 		seq_putc(m, '\n');
430 
431 	return 0;
432 }
433 
434 /**
435  * event_trigger_init - Generic event_trigger_ops @init implementation
436  * @data: Trigger-specific data
437  *
438  * Common implementation of event trigger initialization.
439  *
440  * Usually used directly as the @init method in event trigger
441  * implementations.
442  *
443  * Return: 0 on success, errno otherwise
444  */
445 int event_trigger_init(struct event_trigger_data *data)
446 {
447 	data->ref++;
448 	return 0;
449 }
450 
451 /**
452  * event_trigger_free - Generic event_trigger_ops @free implementation
453  * @data: Trigger-specific data
454  *
455  * Common implementation of event trigger de-initialization.
456  *
457  * Usually used directly as the @free method in event trigger
458  * implementations.
459  */
460 static void
461 event_trigger_free(struct event_trigger_data *data)
462 {
463 	if (WARN_ON_ONCE(data->ref <= 0))
464 		return;
465 
466 	data->ref--;
467 	if (!data->ref)
468 		trigger_data_free(data);
469 }
470 
471 int trace_event_trigger_enable_disable(struct trace_event_file *file,
472 				       int trigger_enable)
473 {
474 	int ret = 0;
475 
476 	if (trigger_enable) {
477 		if (atomic_inc_return(&file->tm_ref) > 1)
478 			return ret;
479 		set_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
480 		ret = trace_event_enable_disable(file, 1, 1);
481 	} else {
482 		if (atomic_dec_return(&file->tm_ref) > 0)
483 			return ret;
484 		clear_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
485 		ret = trace_event_enable_disable(file, 0, 1);
486 	}
487 
488 	return ret;
489 }
490 
491 /**
492  * clear_event_triggers - Clear all triggers associated with a trace array
493  * @tr: The trace array to clear
494  *
495  * For each trigger, the triggering event has its tm_ref decremented
496  * via trace_event_trigger_enable_disable(), and any associated event
497  * (in the case of enable/disable_event triggers) will have its sm_ref
498  * decremented via free()->trace_event_enable_disable().  That
499  * combination effectively reverses the soft-mode/trigger state added
500  * by trigger registration.
501  *
502  * Must be called with event_mutex held.
503  */
504 void
505 clear_event_triggers(struct trace_array *tr)
506 {
507 	struct trace_event_file *file;
508 
509 	list_for_each_entry(file, &tr->events, list) {
510 		struct event_trigger_data *data, *n;
511 		list_for_each_entry_safe(data, n, &file->triggers, list) {
512 			trace_event_trigger_enable_disable(file, 0);
513 			list_del_rcu(&data->list);
514 			if (data->ops->free)
515 				data->ops->free(data);
516 		}
517 	}
518 }
519 
520 /**
521  * update_cond_flag - Set or reset the TRIGGER_COND bit
522  * @file: The trace_event_file associated with the event
523  *
524  * If an event has triggers and any of those triggers has a filter or
525  * a post_trigger, trigger invocation needs to be deferred until after
526  * the current event has logged its data, and the event should have
527  * its TRIGGER_COND bit set, otherwise the TRIGGER_COND bit should be
528  * cleared.
529  */
530 void update_cond_flag(struct trace_event_file *file)
531 {
532 	struct event_trigger_data *data;
533 	bool set_cond = false;
534 
535 	lockdep_assert_held(&event_mutex);
536 
537 	list_for_each_entry(data, &file->triggers, list) {
538 		if (data->filter || event_command_post_trigger(data->cmd_ops) ||
539 		    event_command_needs_rec(data->cmd_ops)) {
540 			set_cond = true;
541 			break;
542 		}
543 	}
544 
545 	if (set_cond)
546 		set_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
547 	else
548 		clear_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
549 }
550 
551 /**
552  * register_trigger - Generic event_command @reg implementation
553  * @glob: The raw string used to register the trigger
554  * @data: Trigger-specific data to associate with the trigger
555  * @file: The trace_event_file associated with the event
556  *
557  * Common implementation for event trigger registration.
558  *
559  * Usually used directly as the @reg method in event command
560  * implementations.
561  *
562  * Return: 0 on success, errno otherwise
563  */
564 static int register_trigger(char *glob,
565 			    struct event_trigger_data *data,
566 			    struct trace_event_file *file)
567 {
568 	struct event_trigger_data *test;
569 	int ret = 0;
570 
571 	lockdep_assert_held(&event_mutex);
572 
573 	list_for_each_entry(test, &file->triggers, list) {
574 		if (test->cmd_ops->trigger_type == data->cmd_ops->trigger_type) {
575 			ret = -EEXIST;
576 			goto out;
577 		}
578 	}
579 
580 	if (data->ops->init) {
581 		ret = data->ops->init(data);
582 		if (ret < 0)
583 			goto out;
584 	}
585 
586 	list_add_rcu(&data->list, &file->triggers);
587 
588 	update_cond_flag(file);
589 	ret = trace_event_trigger_enable_disable(file, 1);
590 	if (ret < 0) {
591 		list_del_rcu(&data->list);
592 		update_cond_flag(file);
593 	}
594 out:
595 	return ret;
596 }
597 
598 /**
599  * unregister_trigger - Generic event_command @unreg implementation
600  * @glob: The raw string used to register the trigger
601  * @test: Trigger-specific data used to find the trigger to remove
602  * @file: The trace_event_file associated with the event
603  *
604  * Common implementation for event trigger unregistration.
605  *
606  * Usually used directly as the @unreg method in event command
607  * implementations.
608  */
609 static void unregister_trigger(char *glob,
610 			       struct event_trigger_data *test,
611 			       struct trace_event_file *file)
612 {
613 	struct event_trigger_data *data = NULL, *iter;
614 
615 	lockdep_assert_held(&event_mutex);
616 
617 	list_for_each_entry(iter, &file->triggers, list) {
618 		if (iter->cmd_ops->trigger_type == test->cmd_ops->trigger_type) {
619 			data = iter;
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 (data && 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_and_filter: String containing trigger and possibly filter
743  * @param: 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 @param and the filter in @filter.  Either the @param
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_and_filter: The param and filter 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_and_filter)
982 {
983 	struct event_trigger_data *trigger_data;
984 	char *param, *filter;
985 	bool remove;
986 	int ret;
987 
988 	remove = event_trigger_check_remove(glob);
989 
990 	ret = event_trigger_separate_filter(param_and_filter, &param, &filter, false);
991 	if (ret)
992 		return ret;
993 
994 	ret = -ENOMEM;
995 	trigger_data = event_trigger_alloc(cmd_ops, cmd, param, file);
996 	if (!trigger_data)
997 		goto out;
998 
999 	if (remove) {
1000 		event_trigger_unregister(cmd_ops, file, glob+1, trigger_data);
1001 		kfree(trigger_data);
1002 		ret = 0;
1003 		goto out;
1004 	}
1005 
1006 	ret = event_trigger_parse_num(param, trigger_data);
1007 	if (ret)
1008 		goto out_free;
1009 
1010 	ret = event_trigger_set_filter(cmd_ops, file, filter, trigger_data);
1011 	if (ret < 0)
1012 		goto out_free;
1013 
1014 	/* Up the trigger_data count to make sure reg doesn't free it on failure */
1015 	event_trigger_init(trigger_data);
1016 
1017 	ret = event_trigger_register(cmd_ops, file, glob, trigger_data);
1018 	if (ret)
1019 		goto out_free;
1020 
1021 	/* Down the counter of trigger_data or free it if not used anymore */
1022 	event_trigger_free(trigger_data);
1023  out:
1024 	return ret;
1025 
1026  out_free:
1027 	event_trigger_reset_filter(cmd_ops, trigger_data);
1028 	kfree(trigger_data);
1029 	goto out;
1030 }
1031 
1032 /**
1033  * set_trigger_filter - Generic event_command @set_filter implementation
1034  * @filter_str: The filter string for the trigger, NULL to remove filter
1035  * @trigger_data: Trigger-specific data
1036  * @file: The trace_event_file associated with the event
1037  *
1038  * Common implementation for event command filter parsing and filter
1039  * instantiation.
1040  *
1041  * Usually used directly as the @set_filter method in event command
1042  * implementations.
1043  *
1044  * Also used to remove a filter (if filter_str = NULL).
1045  *
1046  * Return: 0 on success, errno otherwise
1047  */
1048 int set_trigger_filter(char *filter_str,
1049 		       struct event_trigger_data *trigger_data,
1050 		       struct trace_event_file *file)
1051 {
1052 	struct event_trigger_data *data = trigger_data;
1053 	struct event_filter *filter = NULL, *tmp;
1054 	int ret = -EINVAL;
1055 	char *s;
1056 
1057 	if (!filter_str) /* clear the current filter */
1058 		goto assign;
1059 
1060 	s = strsep(&filter_str, " \t");
1061 
1062 	if (!strlen(s) || strcmp(s, "if") != 0)
1063 		goto out;
1064 
1065 	if (!filter_str)
1066 		goto out;
1067 
1068 	/* The filter is for the 'trigger' event, not the triggered event */
1069 	ret = create_event_filter(file->tr, file->event_call,
1070 				  filter_str, true, &filter);
1071 
1072 	/* Only enabled set_str for error handling */
1073 	if (filter) {
1074 		kfree(filter->filter_string);
1075 		filter->filter_string = NULL;
1076 	}
1077 
1078 	/*
1079 	 * If create_event_filter() fails, filter still needs to be freed.
1080 	 * Which the calling code will do with data->filter.
1081 	 */
1082  assign:
1083 	tmp = rcu_access_pointer(data->filter);
1084 
1085 	rcu_assign_pointer(data->filter, filter);
1086 
1087 	if (tmp) {
1088 		/* Make sure the call is done with the filter */
1089 		tracepoint_synchronize_unregister();
1090 		free_event_filter(tmp);
1091 	}
1092 
1093 	kfree(data->filter_str);
1094 	data->filter_str = NULL;
1095 
1096 	if (filter_str) {
1097 		data->filter_str = kstrdup(filter_str, GFP_KERNEL);
1098 		if (!data->filter_str) {
1099 			free_event_filter(rcu_access_pointer(data->filter));
1100 			data->filter = NULL;
1101 			ret = -ENOMEM;
1102 		}
1103 	}
1104  out:
1105 	return ret;
1106 }
1107 
1108 static LIST_HEAD(named_triggers);
1109 
1110 /**
1111  * find_named_trigger - Find the common named trigger associated with @name
1112  * @name: The name of the set of named triggers to find the common data for
1113  *
1114  * Named triggers are sets of triggers that share a common set of
1115  * trigger data.  The first named trigger registered with a given name
1116  * owns the common trigger data that the others subsequently
1117  * registered with the same name will reference.  This function
1118  * returns the common trigger data associated with that first
1119  * registered instance.
1120  *
1121  * Return: the common trigger data for the given named trigger on
1122  * success, NULL otherwise.
1123  */
1124 struct event_trigger_data *find_named_trigger(const char *name)
1125 {
1126 	struct event_trigger_data *data;
1127 
1128 	if (!name)
1129 		return NULL;
1130 
1131 	list_for_each_entry(data, &named_triggers, named_list) {
1132 		if (data->named_data)
1133 			continue;
1134 		if (strcmp(data->name, name) == 0)
1135 			return data;
1136 	}
1137 
1138 	return NULL;
1139 }
1140 
1141 /**
1142  * is_named_trigger - determine if a given trigger is a named trigger
1143  * @test: The trigger data to test
1144  *
1145  * Return: true if 'test' is a named trigger, false otherwise.
1146  */
1147 bool is_named_trigger(struct event_trigger_data *test)
1148 {
1149 	struct event_trigger_data *data;
1150 
1151 	list_for_each_entry(data, &named_triggers, named_list) {
1152 		if (test == data)
1153 			return true;
1154 	}
1155 
1156 	return false;
1157 }
1158 
1159 /**
1160  * save_named_trigger - save the trigger in the named trigger list
1161  * @name: The name of the named trigger set
1162  * @data: The trigger data to save
1163  *
1164  * Return: 0 if successful, negative error otherwise.
1165  */
1166 int save_named_trigger(const char *name, struct event_trigger_data *data)
1167 {
1168 	data->name = kstrdup(name, GFP_KERNEL);
1169 	if (!data->name)
1170 		return -ENOMEM;
1171 
1172 	list_add(&data->named_list, &named_triggers);
1173 
1174 	return 0;
1175 }
1176 
1177 /**
1178  * del_named_trigger - delete a trigger from the named trigger list
1179  * @data: The trigger data to delete
1180  */
1181 void del_named_trigger(struct event_trigger_data *data)
1182 {
1183 	kfree(data->name);
1184 	data->name = NULL;
1185 
1186 	list_del(&data->named_list);
1187 }
1188 
1189 static void __pause_named_trigger(struct event_trigger_data *data, bool pause)
1190 {
1191 	struct event_trigger_data *test;
1192 
1193 	list_for_each_entry(test, &named_triggers, named_list) {
1194 		if (strcmp(test->name, data->name) == 0) {
1195 			if (pause) {
1196 				test->paused_tmp = test->paused;
1197 				test->paused = true;
1198 			} else {
1199 				test->paused = test->paused_tmp;
1200 			}
1201 		}
1202 	}
1203 }
1204 
1205 /**
1206  * pause_named_trigger - Pause all named triggers with the same name
1207  * @data: The trigger data of a named trigger to pause
1208  *
1209  * Pauses a named trigger along with all other triggers having the
1210  * same name.  Because named triggers share a common set of data,
1211  * pausing only one is meaningless, so pausing one named trigger needs
1212  * to pause all triggers with the same name.
1213  */
1214 void pause_named_trigger(struct event_trigger_data *data)
1215 {
1216 	__pause_named_trigger(data, true);
1217 }
1218 
1219 /**
1220  * unpause_named_trigger - Un-pause all named triggers with the same name
1221  * @data: The trigger data of a named trigger to unpause
1222  *
1223  * Un-pauses a named trigger along with all other triggers having the
1224  * same name.  Because named triggers share a common set of data,
1225  * unpausing only one is meaningless, so unpausing one named trigger
1226  * needs to unpause all triggers with the same name.
1227  */
1228 void unpause_named_trigger(struct event_trigger_data *data)
1229 {
1230 	__pause_named_trigger(data, false);
1231 }
1232 
1233 /**
1234  * set_named_trigger_data - Associate common named trigger data
1235  * @data: The trigger data to associate
1236  * @named_data: The common named trigger to be associated
1237  *
1238  * Named triggers are sets of triggers that share a common set of
1239  * trigger data.  The first named trigger registered with a given name
1240  * owns the common trigger data that the others subsequently
1241  * registered with the same name will reference.  This function
1242  * associates the common trigger data from the first trigger with the
1243  * given trigger.
1244  */
1245 void set_named_trigger_data(struct event_trigger_data *data,
1246 			    struct event_trigger_data *named_data)
1247 {
1248 	data->named_data = named_data;
1249 }
1250 
1251 struct event_trigger_data *
1252 get_named_trigger_data(struct event_trigger_data *data)
1253 {
1254 	return data->named_data;
1255 }
1256 
1257 static void
1258 traceon_trigger(struct event_trigger_data *data,
1259 		struct trace_buffer *buffer, void *rec,
1260 		struct ring_buffer_event *event)
1261 {
1262 	struct trace_event_file *file = data->private_data;
1263 
1264 	if (file) {
1265 		if (tracer_tracing_is_on(file->tr))
1266 			return;
1267 
1268 		tracer_tracing_on(file->tr);
1269 		return;
1270 	}
1271 
1272 	if (tracing_is_on())
1273 		return;
1274 
1275 	tracing_on();
1276 }
1277 
1278 static void
1279 traceon_count_trigger(struct event_trigger_data *data,
1280 		      struct trace_buffer *buffer, void *rec,
1281 		      struct ring_buffer_event *event)
1282 {
1283 	struct trace_event_file *file = data->private_data;
1284 
1285 	if (file) {
1286 		if (tracer_tracing_is_on(file->tr))
1287 			return;
1288 	} else {
1289 		if (tracing_is_on())
1290 			return;
1291 	}
1292 
1293 	if (!data->count)
1294 		return;
1295 
1296 	if (data->count != -1)
1297 		(data->count)--;
1298 
1299 	if (file)
1300 		tracer_tracing_on(file->tr);
1301 	else
1302 		tracing_on();
1303 }
1304 
1305 static void
1306 traceoff_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 
1316 		tracer_tracing_off(file->tr);
1317 		return;
1318 	}
1319 
1320 	if (!tracing_is_on())
1321 		return;
1322 
1323 	tracing_off();
1324 }
1325 
1326 static void
1327 traceoff_count_trigger(struct event_trigger_data *data,
1328 		       struct trace_buffer *buffer, void *rec,
1329 		       struct ring_buffer_event *event)
1330 {
1331 	struct trace_event_file *file = data->private_data;
1332 
1333 	if (file) {
1334 		if (!tracer_tracing_is_on(file->tr))
1335 			return;
1336 	} else {
1337 		if (!tracing_is_on())
1338 			return;
1339 	}
1340 
1341 	if (!data->count)
1342 		return;
1343 
1344 	if (data->count != -1)
1345 		(data->count)--;
1346 
1347 	if (file)
1348 		tracer_tracing_off(file->tr);
1349 	else
1350 		tracing_off();
1351 }
1352 
1353 static int
1354 traceon_trigger_print(struct seq_file *m, struct event_trigger_data *data)
1355 {
1356 	return event_trigger_print("traceon", m, (void *)data->count,
1357 				   data->filter_str);
1358 }
1359 
1360 static int
1361 traceoff_trigger_print(struct seq_file *m, struct event_trigger_data *data)
1362 {
1363 	return event_trigger_print("traceoff", m, (void *)data->count,
1364 				   data->filter_str);
1365 }
1366 
1367 static struct event_trigger_ops traceon_trigger_ops = {
1368 	.trigger		= traceon_trigger,
1369 	.print			= traceon_trigger_print,
1370 	.init			= event_trigger_init,
1371 	.free			= event_trigger_free,
1372 };
1373 
1374 static struct event_trigger_ops traceon_count_trigger_ops = {
1375 	.trigger		= traceon_count_trigger,
1376 	.print			= traceon_trigger_print,
1377 	.init			= event_trigger_init,
1378 	.free			= event_trigger_free,
1379 };
1380 
1381 static struct event_trigger_ops traceoff_trigger_ops = {
1382 	.trigger		= traceoff_trigger,
1383 	.print			= traceoff_trigger_print,
1384 	.init			= event_trigger_init,
1385 	.free			= event_trigger_free,
1386 };
1387 
1388 static struct event_trigger_ops traceoff_count_trigger_ops = {
1389 	.trigger		= traceoff_count_trigger,
1390 	.print			= traceoff_trigger_print,
1391 	.init			= event_trigger_init,
1392 	.free			= event_trigger_free,
1393 };
1394 
1395 static struct event_trigger_ops *
1396 onoff_get_trigger_ops(char *cmd, char *param)
1397 {
1398 	struct event_trigger_ops *ops;
1399 
1400 	/* we register both traceon and traceoff to this callback */
1401 	if (strcmp(cmd, "traceon") == 0)
1402 		ops = param ? &traceon_count_trigger_ops :
1403 			&traceon_trigger_ops;
1404 	else
1405 		ops = param ? &traceoff_count_trigger_ops :
1406 			&traceoff_trigger_ops;
1407 
1408 	return ops;
1409 }
1410 
1411 static struct event_command trigger_traceon_cmd = {
1412 	.name			= "traceon",
1413 	.trigger_type		= ETT_TRACE_ONOFF,
1414 	.parse			= event_trigger_parse,
1415 	.reg			= register_trigger,
1416 	.unreg			= unregister_trigger,
1417 	.get_trigger_ops	= onoff_get_trigger_ops,
1418 	.set_filter		= set_trigger_filter,
1419 };
1420 
1421 static struct event_command trigger_traceoff_cmd = {
1422 	.name			= "traceoff",
1423 	.trigger_type		= ETT_TRACE_ONOFF,
1424 	.flags			= EVENT_CMD_FL_POST_TRIGGER,
1425 	.parse			= event_trigger_parse,
1426 	.reg			= register_trigger,
1427 	.unreg			= unregister_trigger,
1428 	.get_trigger_ops	= onoff_get_trigger_ops,
1429 	.set_filter		= set_trigger_filter,
1430 };
1431 
1432 #ifdef CONFIG_TRACER_SNAPSHOT
1433 static void
1434 snapshot_trigger(struct event_trigger_data *data,
1435 		 struct trace_buffer *buffer, void *rec,
1436 		 struct ring_buffer_event *event)
1437 {
1438 	struct trace_event_file *file = data->private_data;
1439 
1440 	if (file)
1441 		tracing_snapshot_instance(file->tr);
1442 	else
1443 		tracing_snapshot();
1444 }
1445 
1446 static void
1447 snapshot_count_trigger(struct event_trigger_data *data,
1448 		       struct trace_buffer *buffer, void *rec,
1449 		       struct ring_buffer_event *event)
1450 {
1451 	if (!data->count)
1452 		return;
1453 
1454 	if (data->count != -1)
1455 		(data->count)--;
1456 
1457 	snapshot_trigger(data, buffer, rec, event);
1458 }
1459 
1460 static int
1461 register_snapshot_trigger(char *glob,
1462 			  struct event_trigger_data *data,
1463 			  struct trace_event_file *file)
1464 {
1465 	if (tracing_alloc_snapshot_instance(file->tr) != 0)
1466 		return 0;
1467 
1468 	return register_trigger(glob, data, file);
1469 }
1470 
1471 static int
1472 snapshot_trigger_print(struct seq_file *m, struct event_trigger_data *data)
1473 {
1474 	return event_trigger_print("snapshot", m, (void *)data->count,
1475 				   data->filter_str);
1476 }
1477 
1478 static struct event_trigger_ops snapshot_trigger_ops = {
1479 	.trigger		= snapshot_trigger,
1480 	.print			= snapshot_trigger_print,
1481 	.init			= event_trigger_init,
1482 	.free			= event_trigger_free,
1483 };
1484 
1485 static struct event_trigger_ops snapshot_count_trigger_ops = {
1486 	.trigger		= snapshot_count_trigger,
1487 	.print			= snapshot_trigger_print,
1488 	.init			= event_trigger_init,
1489 	.free			= event_trigger_free,
1490 };
1491 
1492 static struct event_trigger_ops *
1493 snapshot_get_trigger_ops(char *cmd, char *param)
1494 {
1495 	return param ? &snapshot_count_trigger_ops : &snapshot_trigger_ops;
1496 }
1497 
1498 static struct event_command trigger_snapshot_cmd = {
1499 	.name			= "snapshot",
1500 	.trigger_type		= ETT_SNAPSHOT,
1501 	.parse			= event_trigger_parse,
1502 	.reg			= register_snapshot_trigger,
1503 	.unreg			= unregister_trigger,
1504 	.get_trigger_ops	= snapshot_get_trigger_ops,
1505 	.set_filter		= set_trigger_filter,
1506 };
1507 
1508 static __init int register_trigger_snapshot_cmd(void)
1509 {
1510 	int ret;
1511 
1512 	ret = register_event_command(&trigger_snapshot_cmd);
1513 	WARN_ON(ret < 0);
1514 
1515 	return ret;
1516 }
1517 #else
1518 static __init int register_trigger_snapshot_cmd(void) { return 0; }
1519 #endif /* CONFIG_TRACER_SNAPSHOT */
1520 
1521 #ifdef CONFIG_STACKTRACE
1522 #ifdef CONFIG_UNWINDER_ORC
1523 /* Skip 2:
1524  *   event_triggers_post_call()
1525  *   trace_event_raw_event_xxx()
1526  */
1527 # define STACK_SKIP 2
1528 #else
1529 /*
1530  * Skip 4:
1531  *   stacktrace_trigger()
1532  *   event_triggers_post_call()
1533  *   trace_event_buffer_commit()
1534  *   trace_event_raw_event_xxx()
1535  */
1536 #define STACK_SKIP 4
1537 #endif
1538 
1539 static void
1540 stacktrace_trigger(struct event_trigger_data *data,
1541 		   struct trace_buffer *buffer,  void *rec,
1542 		   struct ring_buffer_event *event)
1543 {
1544 	struct trace_event_file *file = data->private_data;
1545 
1546 	if (file)
1547 		__trace_stack(file->tr, tracing_gen_ctx(), STACK_SKIP);
1548 	else
1549 		trace_dump_stack(STACK_SKIP);
1550 }
1551 
1552 static void
1553 stacktrace_count_trigger(struct event_trigger_data *data,
1554 			 struct trace_buffer *buffer, void *rec,
1555 			 struct ring_buffer_event *event)
1556 {
1557 	if (!data->count)
1558 		return;
1559 
1560 	if (data->count != -1)
1561 		(data->count)--;
1562 
1563 	stacktrace_trigger(data, buffer, rec, event);
1564 }
1565 
1566 static int
1567 stacktrace_trigger_print(struct seq_file *m, struct event_trigger_data *data)
1568 {
1569 	return event_trigger_print("stacktrace", m, (void *)data->count,
1570 				   data->filter_str);
1571 }
1572 
1573 static struct event_trigger_ops stacktrace_trigger_ops = {
1574 	.trigger		= stacktrace_trigger,
1575 	.print			= stacktrace_trigger_print,
1576 	.init			= event_trigger_init,
1577 	.free			= event_trigger_free,
1578 };
1579 
1580 static struct event_trigger_ops stacktrace_count_trigger_ops = {
1581 	.trigger		= stacktrace_count_trigger,
1582 	.print			= stacktrace_trigger_print,
1583 	.init			= event_trigger_init,
1584 	.free			= event_trigger_free,
1585 };
1586 
1587 static struct event_trigger_ops *
1588 stacktrace_get_trigger_ops(char *cmd, char *param)
1589 {
1590 	return param ? &stacktrace_count_trigger_ops : &stacktrace_trigger_ops;
1591 }
1592 
1593 static struct event_command trigger_stacktrace_cmd = {
1594 	.name			= "stacktrace",
1595 	.trigger_type		= ETT_STACKTRACE,
1596 	.flags			= EVENT_CMD_FL_POST_TRIGGER,
1597 	.parse			= event_trigger_parse,
1598 	.reg			= register_trigger,
1599 	.unreg			= unregister_trigger,
1600 	.get_trigger_ops	= stacktrace_get_trigger_ops,
1601 	.set_filter		= set_trigger_filter,
1602 };
1603 
1604 static __init int register_trigger_stacktrace_cmd(void)
1605 {
1606 	int ret;
1607 
1608 	ret = register_event_command(&trigger_stacktrace_cmd);
1609 	WARN_ON(ret < 0);
1610 
1611 	return ret;
1612 }
1613 #else
1614 static __init int register_trigger_stacktrace_cmd(void) { return 0; }
1615 #endif /* CONFIG_STACKTRACE */
1616 
1617 static __init void unregister_trigger_traceon_traceoff_cmds(void)
1618 {
1619 	unregister_event_command(&trigger_traceon_cmd);
1620 	unregister_event_command(&trigger_traceoff_cmd);
1621 }
1622 
1623 static void
1624 event_enable_trigger(struct event_trigger_data *data,
1625 		     struct trace_buffer *buffer,  void *rec,
1626 		     struct ring_buffer_event *event)
1627 {
1628 	struct enable_trigger_data *enable_data = data->private_data;
1629 
1630 	if (enable_data->enable)
1631 		clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
1632 	else
1633 		set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
1634 }
1635 
1636 static void
1637 event_enable_count_trigger(struct event_trigger_data *data,
1638 			   struct trace_buffer *buffer,  void *rec,
1639 			   struct ring_buffer_event *event)
1640 {
1641 	struct enable_trigger_data *enable_data = data->private_data;
1642 
1643 	if (!data->count)
1644 		return;
1645 
1646 	/* Skip if the event is in a state we want to switch to */
1647 	if (enable_data->enable == !(enable_data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
1648 		return;
1649 
1650 	if (data->count != -1)
1651 		(data->count)--;
1652 
1653 	event_enable_trigger(data, buffer, rec, event);
1654 }
1655 
1656 int event_enable_trigger_print(struct seq_file *m,
1657 			       struct event_trigger_data *data)
1658 {
1659 	struct enable_trigger_data *enable_data = data->private_data;
1660 
1661 	seq_printf(m, "%s:%s:%s",
1662 		   enable_data->hist ?
1663 		   (enable_data->enable ? ENABLE_HIST_STR : DISABLE_HIST_STR) :
1664 		   (enable_data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR),
1665 		   enable_data->file->event_call->class->system,
1666 		   trace_event_name(enable_data->file->event_call));
1667 
1668 	if (data->count == -1)
1669 		seq_puts(m, ":unlimited");
1670 	else
1671 		seq_printf(m, ":count=%ld", data->count);
1672 
1673 	if (data->filter_str)
1674 		seq_printf(m, " if %s\n", data->filter_str);
1675 	else
1676 		seq_putc(m, '\n');
1677 
1678 	return 0;
1679 }
1680 
1681 void event_enable_trigger_free(struct event_trigger_data *data)
1682 {
1683 	struct enable_trigger_data *enable_data = data->private_data;
1684 
1685 	if (WARN_ON_ONCE(data->ref <= 0))
1686 		return;
1687 
1688 	data->ref--;
1689 	if (!data->ref) {
1690 		/* Remove the SOFT_MODE flag */
1691 		trace_event_enable_disable(enable_data->file, 0, 1);
1692 		trace_event_put_ref(enable_data->file->event_call);
1693 		trigger_data_free(data);
1694 		kfree(enable_data);
1695 	}
1696 }
1697 
1698 static struct event_trigger_ops event_enable_trigger_ops = {
1699 	.trigger		= event_enable_trigger,
1700 	.print			= event_enable_trigger_print,
1701 	.init			= event_trigger_init,
1702 	.free			= event_enable_trigger_free,
1703 };
1704 
1705 static struct event_trigger_ops event_enable_count_trigger_ops = {
1706 	.trigger		= event_enable_count_trigger,
1707 	.print			= event_enable_trigger_print,
1708 	.init			= event_trigger_init,
1709 	.free			= event_enable_trigger_free,
1710 };
1711 
1712 static struct event_trigger_ops event_disable_trigger_ops = {
1713 	.trigger		= event_enable_trigger,
1714 	.print			= event_enable_trigger_print,
1715 	.init			= event_trigger_init,
1716 	.free			= event_enable_trigger_free,
1717 };
1718 
1719 static struct event_trigger_ops event_disable_count_trigger_ops = {
1720 	.trigger		= event_enable_count_trigger,
1721 	.print			= event_enable_trigger_print,
1722 	.init			= event_trigger_init,
1723 	.free			= event_enable_trigger_free,
1724 };
1725 
1726 int event_enable_trigger_parse(struct event_command *cmd_ops,
1727 			       struct trace_event_file *file,
1728 			       char *glob, char *cmd, char *param_and_filter)
1729 {
1730 	struct trace_event_file *event_enable_file;
1731 	struct enable_trigger_data *enable_data;
1732 	struct event_trigger_data *trigger_data;
1733 	struct trace_array *tr = file->tr;
1734 	char *param, *filter;
1735 	bool enable, remove;
1736 	const char *system;
1737 	const char *event;
1738 	bool hist = false;
1739 	int ret;
1740 
1741 	remove = event_trigger_check_remove(glob);
1742 
1743 	if (event_trigger_empty_param(param_and_filter))
1744 		return -EINVAL;
1745 
1746 	ret = event_trigger_separate_filter(param_and_filter, &param, &filter, true);
1747 	if (ret)
1748 		return ret;
1749 
1750 	system = strsep(&param, ":");
1751 	if (!param)
1752 		return -EINVAL;
1753 
1754 	event = strsep(&param, ":");
1755 
1756 	ret = -EINVAL;
1757 	event_enable_file = find_event_file(tr, system, event);
1758 	if (!event_enable_file)
1759 		goto out;
1760 
1761 #ifdef CONFIG_HIST_TRIGGERS
1762 	hist = ((strcmp(cmd, ENABLE_HIST_STR) == 0) ||
1763 		(strcmp(cmd, DISABLE_HIST_STR) == 0));
1764 
1765 	enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
1766 		  (strcmp(cmd, ENABLE_HIST_STR) == 0));
1767 #else
1768 	enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1769 #endif
1770 	ret = -ENOMEM;
1771 
1772 	enable_data = kzalloc(sizeof(*enable_data), GFP_KERNEL);
1773 	if (!enable_data)
1774 		goto out;
1775 
1776 	enable_data->hist = hist;
1777 	enable_data->enable = enable;
1778 	enable_data->file = event_enable_file;
1779 
1780 	trigger_data = event_trigger_alloc(cmd_ops, cmd, param, enable_data);
1781 	if (!trigger_data) {
1782 		kfree(enable_data);
1783 		goto out;
1784 	}
1785 
1786 	if (remove) {
1787 		event_trigger_unregister(cmd_ops, file, glob+1, trigger_data);
1788 		kfree(trigger_data);
1789 		kfree(enable_data);
1790 		ret = 0;
1791 		goto out;
1792 	}
1793 
1794 	/* Up the trigger_data count to make sure nothing frees it on failure */
1795 	event_trigger_init(trigger_data);
1796 
1797 	ret = event_trigger_parse_num(param, trigger_data);
1798 	if (ret)
1799 		goto out_free;
1800 
1801 	ret = event_trigger_set_filter(cmd_ops, file, filter, trigger_data);
1802 	if (ret < 0)
1803 		goto out_free;
1804 
1805 	/* Don't let event modules unload while probe registered */
1806 	ret = trace_event_try_get_ref(event_enable_file->event_call);
1807 	if (!ret) {
1808 		ret = -EBUSY;
1809 		goto out_free;
1810 	}
1811 
1812 	ret = trace_event_enable_disable(event_enable_file, 1, 1);
1813 	if (ret < 0)
1814 		goto out_put;
1815 
1816 	ret = event_trigger_register(cmd_ops, file, glob, trigger_data);
1817 	if (ret)
1818 		goto out_disable;
1819 
1820 	event_trigger_free(trigger_data);
1821  out:
1822 	return ret;
1823  out_disable:
1824 	trace_event_enable_disable(event_enable_file, 0, 1);
1825  out_put:
1826 	trace_event_put_ref(event_enable_file->event_call);
1827  out_free:
1828 	event_trigger_reset_filter(cmd_ops, trigger_data);
1829 	event_trigger_free(trigger_data);
1830 	kfree(enable_data);
1831 
1832 	goto out;
1833 }
1834 
1835 int event_enable_register_trigger(char *glob,
1836 				  struct event_trigger_data *data,
1837 				  struct trace_event_file *file)
1838 {
1839 	struct enable_trigger_data *enable_data = data->private_data;
1840 	struct enable_trigger_data *test_enable_data;
1841 	struct event_trigger_data *test;
1842 	int ret = 0;
1843 
1844 	lockdep_assert_held(&event_mutex);
1845 
1846 	list_for_each_entry(test, &file->triggers, list) {
1847 		test_enable_data = test->private_data;
1848 		if (test_enable_data &&
1849 		    (test->cmd_ops->trigger_type ==
1850 		     data->cmd_ops->trigger_type) &&
1851 		    (test_enable_data->file == enable_data->file)) {
1852 			ret = -EEXIST;
1853 			goto out;
1854 		}
1855 	}
1856 
1857 	if (data->ops->init) {
1858 		ret = data->ops->init(data);
1859 		if (ret < 0)
1860 			goto out;
1861 	}
1862 
1863 	list_add_rcu(&data->list, &file->triggers);
1864 
1865 	update_cond_flag(file);
1866 	ret = trace_event_trigger_enable_disable(file, 1);
1867 	if (ret < 0) {
1868 		list_del_rcu(&data->list);
1869 		update_cond_flag(file);
1870 	}
1871 out:
1872 	return ret;
1873 }
1874 
1875 void event_enable_unregister_trigger(char *glob,
1876 				     struct event_trigger_data *test,
1877 				     struct trace_event_file *file)
1878 {
1879 	struct enable_trigger_data *test_enable_data = test->private_data;
1880 	struct event_trigger_data *data = NULL, *iter;
1881 	struct enable_trigger_data *enable_data;
1882 
1883 	lockdep_assert_held(&event_mutex);
1884 
1885 	list_for_each_entry(iter, &file->triggers, list) {
1886 		enable_data = iter->private_data;
1887 		if (enable_data &&
1888 		    (iter->cmd_ops->trigger_type ==
1889 		     test->cmd_ops->trigger_type) &&
1890 		    (enable_data->file == test_enable_data->file)) {
1891 			data = iter;
1892 			list_del_rcu(&data->list);
1893 			trace_event_trigger_enable_disable(file, 0);
1894 			update_cond_flag(file);
1895 			break;
1896 		}
1897 	}
1898 
1899 	if (data && data->ops->free)
1900 		data->ops->free(data);
1901 }
1902 
1903 static struct event_trigger_ops *
1904 event_enable_get_trigger_ops(char *cmd, char *param)
1905 {
1906 	struct event_trigger_ops *ops;
1907 	bool enable;
1908 
1909 #ifdef CONFIG_HIST_TRIGGERS
1910 	enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
1911 		  (strcmp(cmd, ENABLE_HIST_STR) == 0));
1912 #else
1913 	enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1914 #endif
1915 	if (enable)
1916 		ops = param ? &event_enable_count_trigger_ops :
1917 			&event_enable_trigger_ops;
1918 	else
1919 		ops = param ? &event_disable_count_trigger_ops :
1920 			&event_disable_trigger_ops;
1921 
1922 	return ops;
1923 }
1924 
1925 static struct event_command trigger_enable_cmd = {
1926 	.name			= ENABLE_EVENT_STR,
1927 	.trigger_type		= ETT_EVENT_ENABLE,
1928 	.parse			= event_enable_trigger_parse,
1929 	.reg			= event_enable_register_trigger,
1930 	.unreg			= event_enable_unregister_trigger,
1931 	.get_trigger_ops	= event_enable_get_trigger_ops,
1932 	.set_filter		= set_trigger_filter,
1933 };
1934 
1935 static struct event_command trigger_disable_cmd = {
1936 	.name			= DISABLE_EVENT_STR,
1937 	.trigger_type		= ETT_EVENT_ENABLE,
1938 	.parse			= event_enable_trigger_parse,
1939 	.reg			= event_enable_register_trigger,
1940 	.unreg			= event_enable_unregister_trigger,
1941 	.get_trigger_ops	= event_enable_get_trigger_ops,
1942 	.set_filter		= set_trigger_filter,
1943 };
1944 
1945 static __init void unregister_trigger_enable_disable_cmds(void)
1946 {
1947 	unregister_event_command(&trigger_enable_cmd);
1948 	unregister_event_command(&trigger_disable_cmd);
1949 }
1950 
1951 static __init int register_trigger_enable_disable_cmds(void)
1952 {
1953 	int ret;
1954 
1955 	ret = register_event_command(&trigger_enable_cmd);
1956 	if (WARN_ON(ret < 0))
1957 		return ret;
1958 	ret = register_event_command(&trigger_disable_cmd);
1959 	if (WARN_ON(ret < 0))
1960 		unregister_trigger_enable_disable_cmds();
1961 
1962 	return ret;
1963 }
1964 
1965 static __init int register_trigger_traceon_traceoff_cmds(void)
1966 {
1967 	int ret;
1968 
1969 	ret = register_event_command(&trigger_traceon_cmd);
1970 	if (WARN_ON(ret < 0))
1971 		return ret;
1972 	ret = register_event_command(&trigger_traceoff_cmd);
1973 	if (WARN_ON(ret < 0))
1974 		unregister_trigger_traceon_traceoff_cmds();
1975 
1976 	return ret;
1977 }
1978 
1979 __init int register_trigger_cmds(void)
1980 {
1981 	register_trigger_traceon_traceoff_cmds();
1982 	register_trigger_snapshot_cmd();
1983 	register_trigger_stacktrace_cmd();
1984 	register_trigger_enable_disable_cmds();
1985 	register_trigger_hist_enable_disable_cmds();
1986 	register_trigger_hist_cmd();
1987 
1988 	return 0;
1989 }
1990