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