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