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