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