1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Infrastructure for profiling code inserted by 'gcc -pg'. 4 * 5 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com> 6 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com> 7 * 8 * Originally ported from the -rt patch by: 9 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com> 10 * 11 * Based on code in the latency_tracer, that is: 12 * 13 * Copyright (C) 2004-2006 Ingo Molnar 14 * Copyright (C) 2004 Nadia Yvette Chambers 15 */ 16 17 #include <linux/stop_machine.h> 18 #include <linux/clocksource.h> 19 #include <linux/sched/task.h> 20 #include <linux/kallsyms.h> 21 #include <linux/security.h> 22 #include <linux/seq_file.h> 23 #include <linux/tracefs.h> 24 #include <linux/hardirq.h> 25 #include <linux/kthread.h> 26 #include <linux/uaccess.h> 27 #include <linux/bsearch.h> 28 #include <linux/module.h> 29 #include <linux/ftrace.h> 30 #include <linux/sysctl.h> 31 #include <linux/slab.h> 32 #include <linux/ctype.h> 33 #include <linux/sort.h> 34 #include <linux/list.h> 35 #include <linux/hash.h> 36 #include <linux/rcupdate.h> 37 #include <linux/kprobes.h> 38 39 #include <trace/events/sched.h> 40 41 #include <asm/sections.h> 42 #include <asm/setup.h> 43 44 #include "ftrace_internal.h" 45 #include "trace_output.h" 46 #include "trace_stat.h" 47 48 #define FTRACE_INVALID_FUNCTION "__ftrace_invalid_address__" 49 50 #define FTRACE_WARN_ON(cond) \ 51 ({ \ 52 int ___r = cond; \ 53 if (WARN_ON(___r)) \ 54 ftrace_kill(); \ 55 ___r; \ 56 }) 57 58 #define FTRACE_WARN_ON_ONCE(cond) \ 59 ({ \ 60 int ___r = cond; \ 61 if (WARN_ON_ONCE(___r)) \ 62 ftrace_kill(); \ 63 ___r; \ 64 }) 65 66 /* hash bits for specific function selection */ 67 #define FTRACE_HASH_DEFAULT_BITS 10 68 #define FTRACE_HASH_MAX_BITS 12 69 70 #ifdef CONFIG_DYNAMIC_FTRACE 71 #define INIT_OPS_HASH(opsname) \ 72 .func_hash = &opsname.local_hash, \ 73 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock), 74 #else 75 #define INIT_OPS_HASH(opsname) 76 #endif 77 78 enum { 79 FTRACE_MODIFY_ENABLE_FL = (1 << 0), 80 FTRACE_MODIFY_MAY_SLEEP_FL = (1 << 1), 81 }; 82 83 struct ftrace_ops ftrace_list_end __read_mostly = { 84 .func = ftrace_stub, 85 .flags = FTRACE_OPS_FL_STUB, 86 INIT_OPS_HASH(ftrace_list_end) 87 }; 88 89 /* ftrace_enabled is a method to turn ftrace on or off */ 90 int ftrace_enabled __read_mostly; 91 static int __maybe_unused last_ftrace_enabled; 92 93 /* Current function tracing op */ 94 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end; 95 /* What to set function_trace_op to */ 96 static struct ftrace_ops *set_function_trace_op; 97 98 static bool ftrace_pids_enabled(struct ftrace_ops *ops) 99 { 100 struct trace_array *tr; 101 102 if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private) 103 return false; 104 105 tr = ops->private; 106 107 return tr->function_pids != NULL || tr->function_no_pids != NULL; 108 } 109 110 static void ftrace_update_trampoline(struct ftrace_ops *ops); 111 112 /* 113 * ftrace_disabled is set when an anomaly is discovered. 114 * ftrace_disabled is much stronger than ftrace_enabled. 115 */ 116 static int ftrace_disabled __read_mostly; 117 118 DEFINE_MUTEX(ftrace_lock); 119 120 struct ftrace_ops __rcu *ftrace_ops_list __read_mostly = &ftrace_list_end; 121 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub; 122 struct ftrace_ops global_ops; 123 124 /* Defined by vmlinux.lds.h see the comment above arch_ftrace_ops_list_func for details */ 125 void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, 126 struct ftrace_ops *op, struct ftrace_regs *fregs); 127 128 static inline void ftrace_ops_init(struct ftrace_ops *ops) 129 { 130 #ifdef CONFIG_DYNAMIC_FTRACE 131 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) { 132 mutex_init(&ops->local_hash.regex_lock); 133 ops->func_hash = &ops->local_hash; 134 ops->flags |= FTRACE_OPS_FL_INITIALIZED; 135 } 136 #endif 137 } 138 139 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip, 140 struct ftrace_ops *op, struct ftrace_regs *fregs) 141 { 142 struct trace_array *tr = op->private; 143 int pid; 144 145 if (tr) { 146 pid = this_cpu_read(tr->array_buffer.data->ftrace_ignore_pid); 147 if (pid == FTRACE_PID_IGNORE) 148 return; 149 if (pid != FTRACE_PID_TRACE && 150 pid != current->pid) 151 return; 152 } 153 154 op->saved_func(ip, parent_ip, op, fregs); 155 } 156 157 static void ftrace_sync_ipi(void *data) 158 { 159 /* Probably not needed, but do it anyway */ 160 smp_rmb(); 161 } 162 163 static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops) 164 { 165 /* 166 * If this is a dynamic or RCU ops, or we force list func, 167 * then it needs to call the list anyway. 168 */ 169 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_RCU) || 170 FTRACE_FORCE_LIST_FUNC) 171 return ftrace_ops_list_func; 172 173 return ftrace_ops_get_func(ops); 174 } 175 176 static void update_ftrace_function(void) 177 { 178 ftrace_func_t func; 179 180 /* 181 * Prepare the ftrace_ops that the arch callback will use. 182 * If there's only one ftrace_ops registered, the ftrace_ops_list 183 * will point to the ops we want. 184 */ 185 set_function_trace_op = rcu_dereference_protected(ftrace_ops_list, 186 lockdep_is_held(&ftrace_lock)); 187 188 /* If there's no ftrace_ops registered, just call the stub function */ 189 if (set_function_trace_op == &ftrace_list_end) { 190 func = ftrace_stub; 191 192 /* 193 * If we are at the end of the list and this ops is 194 * recursion safe and not dynamic and the arch supports passing ops, 195 * then have the mcount trampoline call the function directly. 196 */ 197 } else if (rcu_dereference_protected(ftrace_ops_list->next, 198 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) { 199 func = ftrace_ops_get_list_func(ftrace_ops_list); 200 201 } else { 202 /* Just use the default ftrace_ops */ 203 set_function_trace_op = &ftrace_list_end; 204 func = ftrace_ops_list_func; 205 } 206 207 update_function_graph_func(); 208 209 /* If there's no change, then do nothing more here */ 210 if (ftrace_trace_function == func) 211 return; 212 213 /* 214 * If we are using the list function, it doesn't care 215 * about the function_trace_ops. 216 */ 217 if (func == ftrace_ops_list_func) { 218 ftrace_trace_function = func; 219 /* 220 * Don't even bother setting function_trace_ops, 221 * it would be racy to do so anyway. 222 */ 223 return; 224 } 225 226 #ifndef CONFIG_DYNAMIC_FTRACE 227 /* 228 * For static tracing, we need to be a bit more careful. 229 * The function change takes affect immediately. Thus, 230 * we need to coordinate the setting of the function_trace_ops 231 * with the setting of the ftrace_trace_function. 232 * 233 * Set the function to the list ops, which will call the 234 * function we want, albeit indirectly, but it handles the 235 * ftrace_ops and doesn't depend on function_trace_op. 236 */ 237 ftrace_trace_function = ftrace_ops_list_func; 238 /* 239 * Make sure all CPUs see this. Yes this is slow, but static 240 * tracing is slow and nasty to have enabled. 241 */ 242 synchronize_rcu_tasks_rude(); 243 /* Now all cpus are using the list ops. */ 244 function_trace_op = set_function_trace_op; 245 /* Make sure the function_trace_op is visible on all CPUs */ 246 smp_wmb(); 247 /* Nasty way to force a rmb on all cpus */ 248 smp_call_function(ftrace_sync_ipi, NULL, 1); 249 /* OK, we are all set to update the ftrace_trace_function now! */ 250 #endif /* !CONFIG_DYNAMIC_FTRACE */ 251 252 ftrace_trace_function = func; 253 } 254 255 static void add_ftrace_ops(struct ftrace_ops __rcu **list, 256 struct ftrace_ops *ops) 257 { 258 rcu_assign_pointer(ops->next, *list); 259 260 /* 261 * We are entering ops into the list but another 262 * CPU might be walking that list. We need to make sure 263 * the ops->next pointer is valid before another CPU sees 264 * the ops pointer included into the list. 265 */ 266 rcu_assign_pointer(*list, ops); 267 } 268 269 static int remove_ftrace_ops(struct ftrace_ops __rcu **list, 270 struct ftrace_ops *ops) 271 { 272 struct ftrace_ops **p; 273 274 /* 275 * If we are removing the last function, then simply point 276 * to the ftrace_stub. 277 */ 278 if (rcu_dereference_protected(*list, 279 lockdep_is_held(&ftrace_lock)) == ops && 280 rcu_dereference_protected(ops->next, 281 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) { 282 *list = &ftrace_list_end; 283 return 0; 284 } 285 286 for (p = list; *p != &ftrace_list_end; p = &(*p)->next) 287 if (*p == ops) 288 break; 289 290 if (*p != ops) 291 return -1; 292 293 *p = (*p)->next; 294 return 0; 295 } 296 297 static void ftrace_update_trampoline(struct ftrace_ops *ops); 298 299 int __register_ftrace_function(struct ftrace_ops *ops) 300 { 301 if (ops->flags & FTRACE_OPS_FL_DELETED) 302 return -EINVAL; 303 304 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED)) 305 return -EBUSY; 306 307 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS 308 /* 309 * If the ftrace_ops specifies SAVE_REGS, then it only can be used 310 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set. 311 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant. 312 */ 313 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS && 314 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)) 315 return -EINVAL; 316 317 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED) 318 ops->flags |= FTRACE_OPS_FL_SAVE_REGS; 319 #endif 320 if (!ftrace_enabled && (ops->flags & FTRACE_OPS_FL_PERMANENT)) 321 return -EBUSY; 322 323 if (!is_kernel_core_data((unsigned long)ops)) 324 ops->flags |= FTRACE_OPS_FL_DYNAMIC; 325 326 add_ftrace_ops(&ftrace_ops_list, ops); 327 328 /* Always save the function, and reset at unregistering */ 329 ops->saved_func = ops->func; 330 331 if (ftrace_pids_enabled(ops)) 332 ops->func = ftrace_pid_func; 333 334 ftrace_update_trampoline(ops); 335 336 if (ftrace_enabled) 337 update_ftrace_function(); 338 339 return 0; 340 } 341 342 int __unregister_ftrace_function(struct ftrace_ops *ops) 343 { 344 int ret; 345 346 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED))) 347 return -EBUSY; 348 349 ret = remove_ftrace_ops(&ftrace_ops_list, ops); 350 351 if (ret < 0) 352 return ret; 353 354 if (ftrace_enabled) 355 update_ftrace_function(); 356 357 ops->func = ops->saved_func; 358 359 return 0; 360 } 361 362 static void ftrace_update_pid_func(void) 363 { 364 struct ftrace_ops *op; 365 366 /* Only do something if we are tracing something */ 367 if (ftrace_trace_function == ftrace_stub) 368 return; 369 370 do_for_each_ftrace_op(op, ftrace_ops_list) { 371 if (op->flags & FTRACE_OPS_FL_PID) { 372 op->func = ftrace_pids_enabled(op) ? 373 ftrace_pid_func : op->saved_func; 374 ftrace_update_trampoline(op); 375 } 376 } while_for_each_ftrace_op(op); 377 378 update_ftrace_function(); 379 } 380 381 #ifdef CONFIG_FUNCTION_PROFILER 382 struct ftrace_profile { 383 struct hlist_node node; 384 unsigned long ip; 385 unsigned long counter; 386 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 387 unsigned long long time; 388 unsigned long long time_squared; 389 #endif 390 }; 391 392 struct ftrace_profile_page { 393 struct ftrace_profile_page *next; 394 unsigned long index; 395 struct ftrace_profile records[]; 396 }; 397 398 struct ftrace_profile_stat { 399 atomic_t disabled; 400 struct hlist_head *hash; 401 struct ftrace_profile_page *pages; 402 struct ftrace_profile_page *start; 403 struct tracer_stat stat; 404 }; 405 406 #define PROFILE_RECORDS_SIZE \ 407 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records)) 408 409 #define PROFILES_PER_PAGE \ 410 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile)) 411 412 static int ftrace_profile_enabled __read_mostly; 413 414 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */ 415 static DEFINE_MUTEX(ftrace_profile_lock); 416 417 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats); 418 419 #define FTRACE_PROFILE_HASH_BITS 10 420 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS) 421 422 static void * 423 function_stat_next(void *v, int idx) 424 { 425 struct ftrace_profile *rec = v; 426 struct ftrace_profile_page *pg; 427 428 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK); 429 430 again: 431 if (idx != 0) 432 rec++; 433 434 if ((void *)rec >= (void *)&pg->records[pg->index]) { 435 pg = pg->next; 436 if (!pg) 437 return NULL; 438 rec = &pg->records[0]; 439 if (!rec->counter) 440 goto again; 441 } 442 443 return rec; 444 } 445 446 static void *function_stat_start(struct tracer_stat *trace) 447 { 448 struct ftrace_profile_stat *stat = 449 container_of(trace, struct ftrace_profile_stat, stat); 450 451 if (!stat || !stat->start) 452 return NULL; 453 454 return function_stat_next(&stat->start->records[0], 0); 455 } 456 457 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 458 /* function graph compares on total time */ 459 static int function_stat_cmp(const void *p1, const void *p2) 460 { 461 const struct ftrace_profile *a = p1; 462 const struct ftrace_profile *b = p2; 463 464 if (a->time < b->time) 465 return -1; 466 if (a->time > b->time) 467 return 1; 468 else 469 return 0; 470 } 471 #else 472 /* not function graph compares against hits */ 473 static int function_stat_cmp(const void *p1, const void *p2) 474 { 475 const struct ftrace_profile *a = p1; 476 const struct ftrace_profile *b = p2; 477 478 if (a->counter < b->counter) 479 return -1; 480 if (a->counter > b->counter) 481 return 1; 482 else 483 return 0; 484 } 485 #endif 486 487 static int function_stat_headers(struct seq_file *m) 488 { 489 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 490 seq_puts(m, " Function " 491 "Hit Time Avg s^2\n" 492 " -------- " 493 "--- ---- --- ---\n"); 494 #else 495 seq_puts(m, " Function Hit\n" 496 " -------- ---\n"); 497 #endif 498 return 0; 499 } 500 501 static int function_stat_show(struct seq_file *m, void *v) 502 { 503 struct ftrace_profile *rec = v; 504 char str[KSYM_SYMBOL_LEN]; 505 int ret = 0; 506 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 507 static struct trace_seq s; 508 unsigned long long avg; 509 unsigned long long stddev; 510 #endif 511 mutex_lock(&ftrace_profile_lock); 512 513 /* we raced with function_profile_reset() */ 514 if (unlikely(rec->counter == 0)) { 515 ret = -EBUSY; 516 goto out; 517 } 518 519 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 520 avg = div64_ul(rec->time, rec->counter); 521 if (tracing_thresh && (avg < tracing_thresh)) 522 goto out; 523 #endif 524 525 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str); 526 seq_printf(m, " %-30.30s %10lu", str, rec->counter); 527 528 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 529 seq_puts(m, " "); 530 531 /* Sample standard deviation (s^2) */ 532 if (rec->counter <= 1) 533 stddev = 0; 534 else { 535 /* 536 * Apply Welford's method: 537 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2) 538 */ 539 stddev = rec->counter * rec->time_squared - 540 rec->time * rec->time; 541 542 /* 543 * Divide only 1000 for ns^2 -> us^2 conversion. 544 * trace_print_graph_duration will divide 1000 again. 545 */ 546 stddev = div64_ul(stddev, 547 rec->counter * (rec->counter - 1) * 1000); 548 } 549 550 trace_seq_init(&s); 551 trace_print_graph_duration(rec->time, &s); 552 trace_seq_puts(&s, " "); 553 trace_print_graph_duration(avg, &s); 554 trace_seq_puts(&s, " "); 555 trace_print_graph_duration(stddev, &s); 556 trace_print_seq(m, &s); 557 #endif 558 seq_putc(m, '\n'); 559 out: 560 mutex_unlock(&ftrace_profile_lock); 561 562 return ret; 563 } 564 565 static void ftrace_profile_reset(struct ftrace_profile_stat *stat) 566 { 567 struct ftrace_profile_page *pg; 568 569 pg = stat->pages = stat->start; 570 571 while (pg) { 572 memset(pg->records, 0, PROFILE_RECORDS_SIZE); 573 pg->index = 0; 574 pg = pg->next; 575 } 576 577 memset(stat->hash, 0, 578 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head)); 579 } 580 581 static int ftrace_profile_pages_init(struct ftrace_profile_stat *stat) 582 { 583 struct ftrace_profile_page *pg; 584 int functions; 585 int pages; 586 int i; 587 588 /* If we already allocated, do nothing */ 589 if (stat->pages) 590 return 0; 591 592 stat->pages = (void *)get_zeroed_page(GFP_KERNEL); 593 if (!stat->pages) 594 return -ENOMEM; 595 596 #ifdef CONFIG_DYNAMIC_FTRACE 597 functions = ftrace_update_tot_cnt; 598 #else 599 /* 600 * We do not know the number of functions that exist because 601 * dynamic tracing is what counts them. With past experience 602 * we have around 20K functions. That should be more than enough. 603 * It is highly unlikely we will execute every function in 604 * the kernel. 605 */ 606 functions = 20000; 607 #endif 608 609 pg = stat->start = stat->pages; 610 611 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE); 612 613 for (i = 1; i < pages; i++) { 614 pg->next = (void *)get_zeroed_page(GFP_KERNEL); 615 if (!pg->next) 616 goto out_free; 617 pg = pg->next; 618 } 619 620 return 0; 621 622 out_free: 623 pg = stat->start; 624 while (pg) { 625 unsigned long tmp = (unsigned long)pg; 626 627 pg = pg->next; 628 free_page(tmp); 629 } 630 631 stat->pages = NULL; 632 stat->start = NULL; 633 634 return -ENOMEM; 635 } 636 637 static int ftrace_profile_init_cpu(int cpu) 638 { 639 struct ftrace_profile_stat *stat; 640 int size; 641 642 stat = &per_cpu(ftrace_profile_stats, cpu); 643 644 if (stat->hash) { 645 /* If the profile is already created, simply reset it */ 646 ftrace_profile_reset(stat); 647 return 0; 648 } 649 650 /* 651 * We are profiling all functions, but usually only a few thousand 652 * functions are hit. We'll make a hash of 1024 items. 653 */ 654 size = FTRACE_PROFILE_HASH_SIZE; 655 656 stat->hash = kcalloc(size, sizeof(struct hlist_head), GFP_KERNEL); 657 658 if (!stat->hash) 659 return -ENOMEM; 660 661 /* Preallocate the function profiling pages */ 662 if (ftrace_profile_pages_init(stat) < 0) { 663 kfree(stat->hash); 664 stat->hash = NULL; 665 return -ENOMEM; 666 } 667 668 return 0; 669 } 670 671 static int ftrace_profile_init(void) 672 { 673 int cpu; 674 int ret = 0; 675 676 for_each_possible_cpu(cpu) { 677 ret = ftrace_profile_init_cpu(cpu); 678 if (ret) 679 break; 680 } 681 682 return ret; 683 } 684 685 /* interrupts must be disabled */ 686 static struct ftrace_profile * 687 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip) 688 { 689 struct ftrace_profile *rec; 690 struct hlist_head *hhd; 691 unsigned long key; 692 693 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS); 694 hhd = &stat->hash[key]; 695 696 if (hlist_empty(hhd)) 697 return NULL; 698 699 hlist_for_each_entry_rcu_notrace(rec, hhd, node) { 700 if (rec->ip == ip) 701 return rec; 702 } 703 704 return NULL; 705 } 706 707 static void ftrace_add_profile(struct ftrace_profile_stat *stat, 708 struct ftrace_profile *rec) 709 { 710 unsigned long key; 711 712 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS); 713 hlist_add_head_rcu(&rec->node, &stat->hash[key]); 714 } 715 716 /* 717 * The memory is already allocated, this simply finds a new record to use. 718 */ 719 static struct ftrace_profile * 720 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip) 721 { 722 struct ftrace_profile *rec = NULL; 723 724 /* prevent recursion (from NMIs) */ 725 if (atomic_inc_return(&stat->disabled) != 1) 726 goto out; 727 728 /* 729 * Try to find the function again since an NMI 730 * could have added it 731 */ 732 rec = ftrace_find_profiled_func(stat, ip); 733 if (rec) 734 goto out; 735 736 if (stat->pages->index == PROFILES_PER_PAGE) { 737 if (!stat->pages->next) 738 goto out; 739 stat->pages = stat->pages->next; 740 } 741 742 rec = &stat->pages->records[stat->pages->index++]; 743 rec->ip = ip; 744 ftrace_add_profile(stat, rec); 745 746 out: 747 atomic_dec(&stat->disabled); 748 749 return rec; 750 } 751 752 static void 753 function_profile_call(unsigned long ip, unsigned long parent_ip, 754 struct ftrace_ops *ops, struct ftrace_regs *fregs) 755 { 756 struct ftrace_profile_stat *stat; 757 struct ftrace_profile *rec; 758 unsigned long flags; 759 760 if (!ftrace_profile_enabled) 761 return; 762 763 local_irq_save(flags); 764 765 stat = this_cpu_ptr(&ftrace_profile_stats); 766 if (!stat->hash || !ftrace_profile_enabled) 767 goto out; 768 769 rec = ftrace_find_profiled_func(stat, ip); 770 if (!rec) { 771 rec = ftrace_profile_alloc(stat, ip); 772 if (!rec) 773 goto out; 774 } 775 776 rec->counter++; 777 out: 778 local_irq_restore(flags); 779 } 780 781 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 782 static bool fgraph_graph_time = true; 783 784 void ftrace_graph_graph_time_control(bool enable) 785 { 786 fgraph_graph_time = enable; 787 } 788 789 static int profile_graph_entry(struct ftrace_graph_ent *trace) 790 { 791 struct ftrace_ret_stack *ret_stack; 792 793 function_profile_call(trace->func, 0, NULL, NULL); 794 795 /* If function graph is shutting down, ret_stack can be NULL */ 796 if (!current->ret_stack) 797 return 0; 798 799 ret_stack = ftrace_graph_get_ret_stack(current, 0); 800 if (ret_stack) 801 ret_stack->subtime = 0; 802 803 return 1; 804 } 805 806 static void profile_graph_return(struct ftrace_graph_ret *trace) 807 { 808 struct ftrace_ret_stack *ret_stack; 809 struct ftrace_profile_stat *stat; 810 unsigned long long calltime; 811 struct ftrace_profile *rec; 812 unsigned long flags; 813 814 local_irq_save(flags); 815 stat = this_cpu_ptr(&ftrace_profile_stats); 816 if (!stat->hash || !ftrace_profile_enabled) 817 goto out; 818 819 /* If the calltime was zero'd ignore it */ 820 if (!trace->calltime) 821 goto out; 822 823 calltime = trace->rettime - trace->calltime; 824 825 if (!fgraph_graph_time) { 826 827 /* Append this call time to the parent time to subtract */ 828 ret_stack = ftrace_graph_get_ret_stack(current, 1); 829 if (ret_stack) 830 ret_stack->subtime += calltime; 831 832 ret_stack = ftrace_graph_get_ret_stack(current, 0); 833 if (ret_stack && ret_stack->subtime < calltime) 834 calltime -= ret_stack->subtime; 835 else 836 calltime = 0; 837 } 838 839 rec = ftrace_find_profiled_func(stat, trace->func); 840 if (rec) { 841 rec->time += calltime; 842 rec->time_squared += calltime * calltime; 843 } 844 845 out: 846 local_irq_restore(flags); 847 } 848 849 static struct fgraph_ops fprofiler_ops = { 850 .entryfunc = &profile_graph_entry, 851 .retfunc = &profile_graph_return, 852 }; 853 854 static int register_ftrace_profiler(void) 855 { 856 return register_ftrace_graph(&fprofiler_ops); 857 } 858 859 static void unregister_ftrace_profiler(void) 860 { 861 unregister_ftrace_graph(&fprofiler_ops); 862 } 863 #else 864 static struct ftrace_ops ftrace_profile_ops __read_mostly = { 865 .func = function_profile_call, 866 .flags = FTRACE_OPS_FL_INITIALIZED, 867 INIT_OPS_HASH(ftrace_profile_ops) 868 }; 869 870 static int register_ftrace_profiler(void) 871 { 872 return register_ftrace_function(&ftrace_profile_ops); 873 } 874 875 static void unregister_ftrace_profiler(void) 876 { 877 unregister_ftrace_function(&ftrace_profile_ops); 878 } 879 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ 880 881 static ssize_t 882 ftrace_profile_write(struct file *filp, const char __user *ubuf, 883 size_t cnt, loff_t *ppos) 884 { 885 unsigned long val; 886 int ret; 887 888 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 889 if (ret) 890 return ret; 891 892 val = !!val; 893 894 mutex_lock(&ftrace_profile_lock); 895 if (ftrace_profile_enabled ^ val) { 896 if (val) { 897 ret = ftrace_profile_init(); 898 if (ret < 0) { 899 cnt = ret; 900 goto out; 901 } 902 903 ret = register_ftrace_profiler(); 904 if (ret < 0) { 905 cnt = ret; 906 goto out; 907 } 908 ftrace_profile_enabled = 1; 909 } else { 910 ftrace_profile_enabled = 0; 911 /* 912 * unregister_ftrace_profiler calls stop_machine 913 * so this acts like an synchronize_rcu. 914 */ 915 unregister_ftrace_profiler(); 916 } 917 } 918 out: 919 mutex_unlock(&ftrace_profile_lock); 920 921 *ppos += cnt; 922 923 return cnt; 924 } 925 926 static ssize_t 927 ftrace_profile_read(struct file *filp, char __user *ubuf, 928 size_t cnt, loff_t *ppos) 929 { 930 char buf[64]; /* big enough to hold a number */ 931 int r; 932 933 r = sprintf(buf, "%u\n", ftrace_profile_enabled); 934 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); 935 } 936 937 static const struct file_operations ftrace_profile_fops = { 938 .open = tracing_open_generic, 939 .read = ftrace_profile_read, 940 .write = ftrace_profile_write, 941 .llseek = default_llseek, 942 }; 943 944 /* used to initialize the real stat files */ 945 static struct tracer_stat function_stats __initdata = { 946 .name = "functions", 947 .stat_start = function_stat_start, 948 .stat_next = function_stat_next, 949 .stat_cmp = function_stat_cmp, 950 .stat_headers = function_stat_headers, 951 .stat_show = function_stat_show 952 }; 953 954 static __init void ftrace_profile_tracefs(struct dentry *d_tracer) 955 { 956 struct ftrace_profile_stat *stat; 957 char *name; 958 int ret; 959 int cpu; 960 961 for_each_possible_cpu(cpu) { 962 stat = &per_cpu(ftrace_profile_stats, cpu); 963 964 name = kasprintf(GFP_KERNEL, "function%d", cpu); 965 if (!name) { 966 /* 967 * The files created are permanent, if something happens 968 * we still do not free memory. 969 */ 970 WARN(1, 971 "Could not allocate stat file for cpu %d\n", 972 cpu); 973 return; 974 } 975 stat->stat = function_stats; 976 stat->stat.name = name; 977 ret = register_stat_tracer(&stat->stat); 978 if (ret) { 979 WARN(1, 980 "Could not register function stat for cpu %d\n", 981 cpu); 982 kfree(name); 983 return; 984 } 985 } 986 987 trace_create_file("function_profile_enabled", 988 TRACE_MODE_WRITE, d_tracer, NULL, 989 &ftrace_profile_fops); 990 } 991 992 #else /* CONFIG_FUNCTION_PROFILER */ 993 static __init void ftrace_profile_tracefs(struct dentry *d_tracer) 994 { 995 } 996 #endif /* CONFIG_FUNCTION_PROFILER */ 997 998 #ifdef CONFIG_DYNAMIC_FTRACE 999 1000 static struct ftrace_ops *removed_ops; 1001 1002 /* 1003 * Set when doing a global update, like enabling all recs or disabling them. 1004 * It is not set when just updating a single ftrace_ops. 1005 */ 1006 static bool update_all_ops; 1007 1008 #ifndef CONFIG_FTRACE_MCOUNT_RECORD 1009 # error Dynamic ftrace depends on MCOUNT_RECORD 1010 #endif 1011 1012 struct ftrace_func_probe { 1013 struct ftrace_probe_ops *probe_ops; 1014 struct ftrace_ops ops; 1015 struct trace_array *tr; 1016 struct list_head list; 1017 void *data; 1018 int ref; 1019 }; 1020 1021 /* 1022 * We make these constant because no one should touch them, 1023 * but they are used as the default "empty hash", to avoid allocating 1024 * it all the time. These are in a read only section such that if 1025 * anyone does try to modify it, it will cause an exception. 1026 */ 1027 static const struct hlist_head empty_buckets[1]; 1028 static const struct ftrace_hash empty_hash = { 1029 .buckets = (struct hlist_head *)empty_buckets, 1030 }; 1031 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash) 1032 1033 struct ftrace_ops global_ops = { 1034 .func = ftrace_stub, 1035 .local_hash.notrace_hash = EMPTY_HASH, 1036 .local_hash.filter_hash = EMPTY_HASH, 1037 INIT_OPS_HASH(global_ops) 1038 .flags = FTRACE_OPS_FL_INITIALIZED | 1039 FTRACE_OPS_FL_PID, 1040 }; 1041 1042 /* 1043 * Used by the stack unwinder to know about dynamic ftrace trampolines. 1044 */ 1045 struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr) 1046 { 1047 struct ftrace_ops *op = NULL; 1048 1049 /* 1050 * Some of the ops may be dynamically allocated, 1051 * they are freed after a synchronize_rcu(). 1052 */ 1053 preempt_disable_notrace(); 1054 1055 do_for_each_ftrace_op(op, ftrace_ops_list) { 1056 /* 1057 * This is to check for dynamically allocated trampolines. 1058 * Trampolines that are in kernel text will have 1059 * core_kernel_text() return true. 1060 */ 1061 if (op->trampoline && op->trampoline_size) 1062 if (addr >= op->trampoline && 1063 addr < op->trampoline + op->trampoline_size) { 1064 preempt_enable_notrace(); 1065 return op; 1066 } 1067 } while_for_each_ftrace_op(op); 1068 preempt_enable_notrace(); 1069 1070 return NULL; 1071 } 1072 1073 /* 1074 * This is used by __kernel_text_address() to return true if the 1075 * address is on a dynamically allocated trampoline that would 1076 * not return true for either core_kernel_text() or 1077 * is_module_text_address(). 1078 */ 1079 bool is_ftrace_trampoline(unsigned long addr) 1080 { 1081 return ftrace_ops_trampoline(addr) != NULL; 1082 } 1083 1084 struct ftrace_page { 1085 struct ftrace_page *next; 1086 struct dyn_ftrace *records; 1087 int index; 1088 int order; 1089 }; 1090 1091 #define ENTRY_SIZE sizeof(struct dyn_ftrace) 1092 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE) 1093 1094 static struct ftrace_page *ftrace_pages_start; 1095 static struct ftrace_page *ftrace_pages; 1096 1097 static __always_inline unsigned long 1098 ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip) 1099 { 1100 if (hash->size_bits > 0) 1101 return hash_long(ip, hash->size_bits); 1102 1103 return 0; 1104 } 1105 1106 /* Only use this function if ftrace_hash_empty() has already been tested */ 1107 static __always_inline struct ftrace_func_entry * 1108 __ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip) 1109 { 1110 unsigned long key; 1111 struct ftrace_func_entry *entry; 1112 struct hlist_head *hhd; 1113 1114 key = ftrace_hash_key(hash, ip); 1115 hhd = &hash->buckets[key]; 1116 1117 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) { 1118 if (entry->ip == ip) 1119 return entry; 1120 } 1121 return NULL; 1122 } 1123 1124 /** 1125 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash 1126 * @hash: The hash to look at 1127 * @ip: The instruction pointer to test 1128 * 1129 * Search a given @hash to see if a given instruction pointer (@ip) 1130 * exists in it. 1131 * 1132 * Returns the entry that holds the @ip if found. NULL otherwise. 1133 */ 1134 struct ftrace_func_entry * 1135 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip) 1136 { 1137 if (ftrace_hash_empty(hash)) 1138 return NULL; 1139 1140 return __ftrace_lookup_ip(hash, ip); 1141 } 1142 1143 static void __add_hash_entry(struct ftrace_hash *hash, 1144 struct ftrace_func_entry *entry) 1145 { 1146 struct hlist_head *hhd; 1147 unsigned long key; 1148 1149 key = ftrace_hash_key(hash, entry->ip); 1150 hhd = &hash->buckets[key]; 1151 hlist_add_head(&entry->hlist, hhd); 1152 hash->count++; 1153 } 1154 1155 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip) 1156 { 1157 struct ftrace_func_entry *entry; 1158 1159 entry = kmalloc(sizeof(*entry), GFP_KERNEL); 1160 if (!entry) 1161 return -ENOMEM; 1162 1163 entry->ip = ip; 1164 __add_hash_entry(hash, entry); 1165 1166 return 0; 1167 } 1168 1169 static void 1170 free_hash_entry(struct ftrace_hash *hash, 1171 struct ftrace_func_entry *entry) 1172 { 1173 hlist_del(&entry->hlist); 1174 kfree(entry); 1175 hash->count--; 1176 } 1177 1178 static void 1179 remove_hash_entry(struct ftrace_hash *hash, 1180 struct ftrace_func_entry *entry) 1181 { 1182 hlist_del_rcu(&entry->hlist); 1183 hash->count--; 1184 } 1185 1186 static void ftrace_hash_clear(struct ftrace_hash *hash) 1187 { 1188 struct hlist_head *hhd; 1189 struct hlist_node *tn; 1190 struct ftrace_func_entry *entry; 1191 int size = 1 << hash->size_bits; 1192 int i; 1193 1194 if (!hash->count) 1195 return; 1196 1197 for (i = 0; i < size; i++) { 1198 hhd = &hash->buckets[i]; 1199 hlist_for_each_entry_safe(entry, tn, hhd, hlist) 1200 free_hash_entry(hash, entry); 1201 } 1202 FTRACE_WARN_ON(hash->count); 1203 } 1204 1205 static void free_ftrace_mod(struct ftrace_mod_load *ftrace_mod) 1206 { 1207 list_del(&ftrace_mod->list); 1208 kfree(ftrace_mod->module); 1209 kfree(ftrace_mod->func); 1210 kfree(ftrace_mod); 1211 } 1212 1213 static void clear_ftrace_mod_list(struct list_head *head) 1214 { 1215 struct ftrace_mod_load *p, *n; 1216 1217 /* stack tracer isn't supported yet */ 1218 if (!head) 1219 return; 1220 1221 mutex_lock(&ftrace_lock); 1222 list_for_each_entry_safe(p, n, head, list) 1223 free_ftrace_mod(p); 1224 mutex_unlock(&ftrace_lock); 1225 } 1226 1227 static void free_ftrace_hash(struct ftrace_hash *hash) 1228 { 1229 if (!hash || hash == EMPTY_HASH) 1230 return; 1231 ftrace_hash_clear(hash); 1232 kfree(hash->buckets); 1233 kfree(hash); 1234 } 1235 1236 static void __free_ftrace_hash_rcu(struct rcu_head *rcu) 1237 { 1238 struct ftrace_hash *hash; 1239 1240 hash = container_of(rcu, struct ftrace_hash, rcu); 1241 free_ftrace_hash(hash); 1242 } 1243 1244 static void free_ftrace_hash_rcu(struct ftrace_hash *hash) 1245 { 1246 if (!hash || hash == EMPTY_HASH) 1247 return; 1248 call_rcu(&hash->rcu, __free_ftrace_hash_rcu); 1249 } 1250 1251 void ftrace_free_filter(struct ftrace_ops *ops) 1252 { 1253 ftrace_ops_init(ops); 1254 free_ftrace_hash(ops->func_hash->filter_hash); 1255 free_ftrace_hash(ops->func_hash->notrace_hash); 1256 } 1257 1258 static struct ftrace_hash *alloc_ftrace_hash(int size_bits) 1259 { 1260 struct ftrace_hash *hash; 1261 int size; 1262 1263 hash = kzalloc(sizeof(*hash), GFP_KERNEL); 1264 if (!hash) 1265 return NULL; 1266 1267 size = 1 << size_bits; 1268 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL); 1269 1270 if (!hash->buckets) { 1271 kfree(hash); 1272 return NULL; 1273 } 1274 1275 hash->size_bits = size_bits; 1276 1277 return hash; 1278 } 1279 1280 1281 static int ftrace_add_mod(struct trace_array *tr, 1282 const char *func, const char *module, 1283 int enable) 1284 { 1285 struct ftrace_mod_load *ftrace_mod; 1286 struct list_head *mod_head = enable ? &tr->mod_trace : &tr->mod_notrace; 1287 1288 ftrace_mod = kzalloc(sizeof(*ftrace_mod), GFP_KERNEL); 1289 if (!ftrace_mod) 1290 return -ENOMEM; 1291 1292 INIT_LIST_HEAD(&ftrace_mod->list); 1293 ftrace_mod->func = kstrdup(func, GFP_KERNEL); 1294 ftrace_mod->module = kstrdup(module, GFP_KERNEL); 1295 ftrace_mod->enable = enable; 1296 1297 if (!ftrace_mod->func || !ftrace_mod->module) 1298 goto out_free; 1299 1300 list_add(&ftrace_mod->list, mod_head); 1301 1302 return 0; 1303 1304 out_free: 1305 free_ftrace_mod(ftrace_mod); 1306 1307 return -ENOMEM; 1308 } 1309 1310 static struct ftrace_hash * 1311 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash) 1312 { 1313 struct ftrace_func_entry *entry; 1314 struct ftrace_hash *new_hash; 1315 int size; 1316 int ret; 1317 int i; 1318 1319 new_hash = alloc_ftrace_hash(size_bits); 1320 if (!new_hash) 1321 return NULL; 1322 1323 if (hash) 1324 new_hash->flags = hash->flags; 1325 1326 /* Empty hash? */ 1327 if (ftrace_hash_empty(hash)) 1328 return new_hash; 1329 1330 size = 1 << hash->size_bits; 1331 for (i = 0; i < size; i++) { 1332 hlist_for_each_entry(entry, &hash->buckets[i], hlist) { 1333 ret = add_hash_entry(new_hash, entry->ip); 1334 if (ret < 0) 1335 goto free_hash; 1336 } 1337 } 1338 1339 FTRACE_WARN_ON(new_hash->count != hash->count); 1340 1341 return new_hash; 1342 1343 free_hash: 1344 free_ftrace_hash(new_hash); 1345 return NULL; 1346 } 1347 1348 static void 1349 ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash); 1350 static void 1351 ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash); 1352 1353 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops, 1354 struct ftrace_hash *new_hash); 1355 1356 static struct ftrace_hash *dup_hash(struct ftrace_hash *src, int size) 1357 { 1358 struct ftrace_func_entry *entry; 1359 struct ftrace_hash *new_hash; 1360 struct hlist_head *hhd; 1361 struct hlist_node *tn; 1362 int bits = 0; 1363 int i; 1364 1365 /* 1366 * Use around half the size (max bit of it), but 1367 * a minimum of 2 is fine (as size of 0 or 1 both give 1 for bits). 1368 */ 1369 bits = fls(size / 2); 1370 1371 /* Don't allocate too much */ 1372 if (bits > FTRACE_HASH_MAX_BITS) 1373 bits = FTRACE_HASH_MAX_BITS; 1374 1375 new_hash = alloc_ftrace_hash(bits); 1376 if (!new_hash) 1377 return NULL; 1378 1379 new_hash->flags = src->flags; 1380 1381 size = 1 << src->size_bits; 1382 for (i = 0; i < size; i++) { 1383 hhd = &src->buckets[i]; 1384 hlist_for_each_entry_safe(entry, tn, hhd, hlist) { 1385 remove_hash_entry(src, entry); 1386 __add_hash_entry(new_hash, entry); 1387 } 1388 } 1389 return new_hash; 1390 } 1391 1392 static struct ftrace_hash * 1393 __ftrace_hash_move(struct ftrace_hash *src) 1394 { 1395 int size = src->count; 1396 1397 /* 1398 * If the new source is empty, just return the empty_hash. 1399 */ 1400 if (ftrace_hash_empty(src)) 1401 return EMPTY_HASH; 1402 1403 return dup_hash(src, size); 1404 } 1405 1406 static int 1407 ftrace_hash_move(struct ftrace_ops *ops, int enable, 1408 struct ftrace_hash **dst, struct ftrace_hash *src) 1409 { 1410 struct ftrace_hash *new_hash; 1411 int ret; 1412 1413 /* Reject setting notrace hash on IPMODIFY ftrace_ops */ 1414 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable) 1415 return -EINVAL; 1416 1417 new_hash = __ftrace_hash_move(src); 1418 if (!new_hash) 1419 return -ENOMEM; 1420 1421 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */ 1422 if (enable) { 1423 /* IPMODIFY should be updated only when filter_hash updating */ 1424 ret = ftrace_hash_ipmodify_update(ops, new_hash); 1425 if (ret < 0) { 1426 free_ftrace_hash(new_hash); 1427 return ret; 1428 } 1429 } 1430 1431 /* 1432 * Remove the current set, update the hash and add 1433 * them back. 1434 */ 1435 ftrace_hash_rec_disable_modify(ops, enable); 1436 1437 rcu_assign_pointer(*dst, new_hash); 1438 1439 ftrace_hash_rec_enable_modify(ops, enable); 1440 1441 return 0; 1442 } 1443 1444 static bool hash_contains_ip(unsigned long ip, 1445 struct ftrace_ops_hash *hash) 1446 { 1447 /* 1448 * The function record is a match if it exists in the filter 1449 * hash and not in the notrace hash. Note, an empty hash is 1450 * considered a match for the filter hash, but an empty 1451 * notrace hash is considered not in the notrace hash. 1452 */ 1453 return (ftrace_hash_empty(hash->filter_hash) || 1454 __ftrace_lookup_ip(hash->filter_hash, ip)) && 1455 (ftrace_hash_empty(hash->notrace_hash) || 1456 !__ftrace_lookup_ip(hash->notrace_hash, ip)); 1457 } 1458 1459 /* 1460 * Test the hashes for this ops to see if we want to call 1461 * the ops->func or not. 1462 * 1463 * It's a match if the ip is in the ops->filter_hash or 1464 * the filter_hash does not exist or is empty, 1465 * AND 1466 * the ip is not in the ops->notrace_hash. 1467 * 1468 * This needs to be called with preemption disabled as 1469 * the hashes are freed with call_rcu(). 1470 */ 1471 int 1472 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs) 1473 { 1474 struct ftrace_ops_hash hash; 1475 int ret; 1476 1477 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS 1478 /* 1479 * There's a small race when adding ops that the ftrace handler 1480 * that wants regs, may be called without them. We can not 1481 * allow that handler to be called if regs is NULL. 1482 */ 1483 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS)) 1484 return 0; 1485 #endif 1486 1487 rcu_assign_pointer(hash.filter_hash, ops->func_hash->filter_hash); 1488 rcu_assign_pointer(hash.notrace_hash, ops->func_hash->notrace_hash); 1489 1490 if (hash_contains_ip(ip, &hash)) 1491 ret = 1; 1492 else 1493 ret = 0; 1494 1495 return ret; 1496 } 1497 1498 /* 1499 * This is a double for. Do not use 'break' to break out of the loop, 1500 * you must use a goto. 1501 */ 1502 #define do_for_each_ftrace_rec(pg, rec) \ 1503 for (pg = ftrace_pages_start; pg; pg = pg->next) { \ 1504 int _____i; \ 1505 for (_____i = 0; _____i < pg->index; _____i++) { \ 1506 rec = &pg->records[_____i]; 1507 1508 #define while_for_each_ftrace_rec() \ 1509 } \ 1510 } 1511 1512 1513 static int ftrace_cmp_recs(const void *a, const void *b) 1514 { 1515 const struct dyn_ftrace *key = a; 1516 const struct dyn_ftrace *rec = b; 1517 1518 if (key->flags < rec->ip) 1519 return -1; 1520 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE) 1521 return 1; 1522 return 0; 1523 } 1524 1525 static struct dyn_ftrace *lookup_rec(unsigned long start, unsigned long end) 1526 { 1527 struct ftrace_page *pg; 1528 struct dyn_ftrace *rec = NULL; 1529 struct dyn_ftrace key; 1530 1531 key.ip = start; 1532 key.flags = end; /* overload flags, as it is unsigned long */ 1533 1534 for (pg = ftrace_pages_start; pg; pg = pg->next) { 1535 if (end < pg->records[0].ip || 1536 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE)) 1537 continue; 1538 rec = bsearch(&key, pg->records, pg->index, 1539 sizeof(struct dyn_ftrace), 1540 ftrace_cmp_recs); 1541 if (rec) 1542 break; 1543 } 1544 return rec; 1545 } 1546 1547 /** 1548 * ftrace_location_range - return the first address of a traced location 1549 * if it touches the given ip range 1550 * @start: start of range to search. 1551 * @end: end of range to search (inclusive). @end points to the last byte 1552 * to check. 1553 * 1554 * Returns rec->ip if the related ftrace location is a least partly within 1555 * the given address range. That is, the first address of the instruction 1556 * that is either a NOP or call to the function tracer. It checks the ftrace 1557 * internal tables to determine if the address belongs or not. 1558 */ 1559 unsigned long ftrace_location_range(unsigned long start, unsigned long end) 1560 { 1561 struct dyn_ftrace *rec; 1562 1563 rec = lookup_rec(start, end); 1564 if (rec) 1565 return rec->ip; 1566 1567 return 0; 1568 } 1569 1570 /** 1571 * ftrace_location - return the ftrace location 1572 * @ip: the instruction pointer to check 1573 * 1574 * If @ip matches the ftrace location, return @ip. 1575 * If @ip matches sym+0, return sym's ftrace location. 1576 * Otherwise, return 0. 1577 */ 1578 unsigned long ftrace_location(unsigned long ip) 1579 { 1580 struct dyn_ftrace *rec; 1581 unsigned long offset; 1582 unsigned long size; 1583 1584 rec = lookup_rec(ip, ip); 1585 if (!rec) { 1586 if (!kallsyms_lookup_size_offset(ip, &size, &offset)) 1587 goto out; 1588 1589 /* map sym+0 to __fentry__ */ 1590 if (!offset) 1591 rec = lookup_rec(ip, ip + size - 1); 1592 } 1593 1594 if (rec) 1595 return rec->ip; 1596 1597 out: 1598 return 0; 1599 } 1600 1601 /** 1602 * ftrace_text_reserved - return true if range contains an ftrace location 1603 * @start: start of range to search 1604 * @end: end of range to search (inclusive). @end points to the last byte to check. 1605 * 1606 * Returns 1 if @start and @end contains a ftrace location. 1607 * That is, the instruction that is either a NOP or call to 1608 * the function tracer. It checks the ftrace internal tables to 1609 * determine if the address belongs or not. 1610 */ 1611 int ftrace_text_reserved(const void *start, const void *end) 1612 { 1613 unsigned long ret; 1614 1615 ret = ftrace_location_range((unsigned long)start, 1616 (unsigned long)end); 1617 1618 return (int)!!ret; 1619 } 1620 1621 /* Test if ops registered to this rec needs regs */ 1622 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec) 1623 { 1624 struct ftrace_ops *ops; 1625 bool keep_regs = false; 1626 1627 for (ops = ftrace_ops_list; 1628 ops != &ftrace_list_end; ops = ops->next) { 1629 /* pass rec in as regs to have non-NULL val */ 1630 if (ftrace_ops_test(ops, rec->ip, rec)) { 1631 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) { 1632 keep_regs = true; 1633 break; 1634 } 1635 } 1636 } 1637 1638 return keep_regs; 1639 } 1640 1641 static struct ftrace_ops * 1642 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec); 1643 static struct ftrace_ops * 1644 ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude); 1645 static struct ftrace_ops * 1646 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops); 1647 1648 static bool skip_record(struct dyn_ftrace *rec) 1649 { 1650 /* 1651 * At boot up, weak functions are set to disable. Function tracing 1652 * can be enabled before they are, and they still need to be disabled now. 1653 * If the record is disabled, still continue if it is marked as already 1654 * enabled (this is needed to keep the accounting working). 1655 */ 1656 return rec->flags & FTRACE_FL_DISABLED && 1657 !(rec->flags & FTRACE_FL_ENABLED); 1658 } 1659 1660 static bool __ftrace_hash_rec_update(struct ftrace_ops *ops, 1661 int filter_hash, 1662 bool inc) 1663 { 1664 struct ftrace_hash *hash; 1665 struct ftrace_hash *other_hash; 1666 struct ftrace_page *pg; 1667 struct dyn_ftrace *rec; 1668 bool update = false; 1669 int count = 0; 1670 int all = false; 1671 1672 /* Only update if the ops has been registered */ 1673 if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) 1674 return false; 1675 1676 /* 1677 * In the filter_hash case: 1678 * If the count is zero, we update all records. 1679 * Otherwise we just update the items in the hash. 1680 * 1681 * In the notrace_hash case: 1682 * We enable the update in the hash. 1683 * As disabling notrace means enabling the tracing, 1684 * and enabling notrace means disabling, the inc variable 1685 * gets inversed. 1686 */ 1687 if (filter_hash) { 1688 hash = ops->func_hash->filter_hash; 1689 other_hash = ops->func_hash->notrace_hash; 1690 if (ftrace_hash_empty(hash)) 1691 all = true; 1692 } else { 1693 inc = !inc; 1694 hash = ops->func_hash->notrace_hash; 1695 other_hash = ops->func_hash->filter_hash; 1696 /* 1697 * If the notrace hash has no items, 1698 * then there's nothing to do. 1699 */ 1700 if (ftrace_hash_empty(hash)) 1701 return false; 1702 } 1703 1704 do_for_each_ftrace_rec(pg, rec) { 1705 int in_other_hash = 0; 1706 int in_hash = 0; 1707 int match = 0; 1708 1709 if (skip_record(rec)) 1710 continue; 1711 1712 if (all) { 1713 /* 1714 * Only the filter_hash affects all records. 1715 * Update if the record is not in the notrace hash. 1716 */ 1717 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip)) 1718 match = 1; 1719 } else { 1720 in_hash = !!ftrace_lookup_ip(hash, rec->ip); 1721 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip); 1722 1723 /* 1724 * If filter_hash is set, we want to match all functions 1725 * that are in the hash but not in the other hash. 1726 * 1727 * If filter_hash is not set, then we are decrementing. 1728 * That means we match anything that is in the hash 1729 * and also in the other_hash. That is, we need to turn 1730 * off functions in the other hash because they are disabled 1731 * by this hash. 1732 */ 1733 if (filter_hash && in_hash && !in_other_hash) 1734 match = 1; 1735 else if (!filter_hash && in_hash && 1736 (in_other_hash || ftrace_hash_empty(other_hash))) 1737 match = 1; 1738 } 1739 if (!match) 1740 continue; 1741 1742 if (inc) { 1743 rec->flags++; 1744 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX)) 1745 return false; 1746 1747 if (ops->flags & FTRACE_OPS_FL_DIRECT) 1748 rec->flags |= FTRACE_FL_DIRECT; 1749 1750 /* 1751 * If there's only a single callback registered to a 1752 * function, and the ops has a trampoline registered 1753 * for it, then we can call it directly. 1754 */ 1755 if (ftrace_rec_count(rec) == 1 && ops->trampoline) 1756 rec->flags |= FTRACE_FL_TRAMP; 1757 else 1758 /* 1759 * If we are adding another function callback 1760 * to this function, and the previous had a 1761 * custom trampoline in use, then we need to go 1762 * back to the default trampoline. 1763 */ 1764 rec->flags &= ~FTRACE_FL_TRAMP; 1765 1766 /* 1767 * If any ops wants regs saved for this function 1768 * then all ops will get saved regs. 1769 */ 1770 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) 1771 rec->flags |= FTRACE_FL_REGS; 1772 } else { 1773 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0)) 1774 return false; 1775 rec->flags--; 1776 1777 /* 1778 * Only the internal direct_ops should have the 1779 * DIRECT flag set. Thus, if it is removing a 1780 * function, then that function should no longer 1781 * be direct. 1782 */ 1783 if (ops->flags & FTRACE_OPS_FL_DIRECT) 1784 rec->flags &= ~FTRACE_FL_DIRECT; 1785 1786 /* 1787 * If the rec had REGS enabled and the ops that is 1788 * being removed had REGS set, then see if there is 1789 * still any ops for this record that wants regs. 1790 * If not, we can stop recording them. 1791 */ 1792 if (ftrace_rec_count(rec) > 0 && 1793 rec->flags & FTRACE_FL_REGS && 1794 ops->flags & FTRACE_OPS_FL_SAVE_REGS) { 1795 if (!test_rec_ops_needs_regs(rec)) 1796 rec->flags &= ~FTRACE_FL_REGS; 1797 } 1798 1799 /* 1800 * The TRAMP needs to be set only if rec count 1801 * is decremented to one, and the ops that is 1802 * left has a trampoline. As TRAMP can only be 1803 * enabled if there is only a single ops attached 1804 * to it. 1805 */ 1806 if (ftrace_rec_count(rec) == 1 && 1807 ftrace_find_tramp_ops_any_other(rec, ops)) 1808 rec->flags |= FTRACE_FL_TRAMP; 1809 else 1810 rec->flags &= ~FTRACE_FL_TRAMP; 1811 1812 /* 1813 * flags will be cleared in ftrace_check_record() 1814 * if rec count is zero. 1815 */ 1816 } 1817 count++; 1818 1819 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */ 1820 update |= ftrace_test_record(rec, true) != FTRACE_UPDATE_IGNORE; 1821 1822 /* Shortcut, if we handled all records, we are done. */ 1823 if (!all && count == hash->count) 1824 return update; 1825 } while_for_each_ftrace_rec(); 1826 1827 return update; 1828 } 1829 1830 static bool ftrace_hash_rec_disable(struct ftrace_ops *ops, 1831 int filter_hash) 1832 { 1833 return __ftrace_hash_rec_update(ops, filter_hash, 0); 1834 } 1835 1836 static bool ftrace_hash_rec_enable(struct ftrace_ops *ops, 1837 int filter_hash) 1838 { 1839 return __ftrace_hash_rec_update(ops, filter_hash, 1); 1840 } 1841 1842 static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops, 1843 int filter_hash, int inc) 1844 { 1845 struct ftrace_ops *op; 1846 1847 __ftrace_hash_rec_update(ops, filter_hash, inc); 1848 1849 if (ops->func_hash != &global_ops.local_hash) 1850 return; 1851 1852 /* 1853 * If the ops shares the global_ops hash, then we need to update 1854 * all ops that are enabled and use this hash. 1855 */ 1856 do_for_each_ftrace_op(op, ftrace_ops_list) { 1857 /* Already done */ 1858 if (op == ops) 1859 continue; 1860 if (op->func_hash == &global_ops.local_hash) 1861 __ftrace_hash_rec_update(op, filter_hash, inc); 1862 } while_for_each_ftrace_op(op); 1863 } 1864 1865 static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, 1866 int filter_hash) 1867 { 1868 ftrace_hash_rec_update_modify(ops, filter_hash, 0); 1869 } 1870 1871 static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, 1872 int filter_hash) 1873 { 1874 ftrace_hash_rec_update_modify(ops, filter_hash, 1); 1875 } 1876 1877 /* 1878 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK 1879 * or no-needed to update, -EBUSY if it detects a conflict of the flag 1880 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs. 1881 * Note that old_hash and new_hash has below meanings 1882 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected) 1883 * - If the hash is EMPTY_HASH, it hits nothing 1884 * - Anything else hits the recs which match the hash entries. 1885 * 1886 * DIRECT ops does not have IPMODIFY flag, but we still need to check it 1887 * against functions with FTRACE_FL_IPMODIFY. If there is any overlap, call 1888 * ops_func(SHARE_IPMODIFY_SELF) to make sure current ops can share with 1889 * IPMODIFY. If ops_func(SHARE_IPMODIFY_SELF) returns non-zero, propagate 1890 * the return value to the caller and eventually to the owner of the DIRECT 1891 * ops. 1892 */ 1893 static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops, 1894 struct ftrace_hash *old_hash, 1895 struct ftrace_hash *new_hash) 1896 { 1897 struct ftrace_page *pg; 1898 struct dyn_ftrace *rec, *end = NULL; 1899 int in_old, in_new; 1900 bool is_ipmodify, is_direct; 1901 1902 /* Only update if the ops has been registered */ 1903 if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) 1904 return 0; 1905 1906 is_ipmodify = ops->flags & FTRACE_OPS_FL_IPMODIFY; 1907 is_direct = ops->flags & FTRACE_OPS_FL_DIRECT; 1908 1909 /* neither IPMODIFY nor DIRECT, skip */ 1910 if (!is_ipmodify && !is_direct) 1911 return 0; 1912 1913 if (WARN_ON_ONCE(is_ipmodify && is_direct)) 1914 return 0; 1915 1916 /* 1917 * Since the IPMODIFY and DIRECT are very address sensitive 1918 * actions, we do not allow ftrace_ops to set all functions to new 1919 * hash. 1920 */ 1921 if (!new_hash || !old_hash) 1922 return -EINVAL; 1923 1924 /* Update rec->flags */ 1925 do_for_each_ftrace_rec(pg, rec) { 1926 1927 if (rec->flags & FTRACE_FL_DISABLED) 1928 continue; 1929 1930 /* We need to update only differences of filter_hash */ 1931 in_old = !!ftrace_lookup_ip(old_hash, rec->ip); 1932 in_new = !!ftrace_lookup_ip(new_hash, rec->ip); 1933 if (in_old == in_new) 1934 continue; 1935 1936 if (in_new) { 1937 if (rec->flags & FTRACE_FL_IPMODIFY) { 1938 int ret; 1939 1940 /* Cannot have two ipmodify on same rec */ 1941 if (is_ipmodify) 1942 goto rollback; 1943 1944 FTRACE_WARN_ON(rec->flags & FTRACE_FL_DIRECT); 1945 1946 /* 1947 * Another ops with IPMODIFY is already 1948 * attached. We are now attaching a direct 1949 * ops. Run SHARE_IPMODIFY_SELF, to check 1950 * whether sharing is supported. 1951 */ 1952 if (!ops->ops_func) 1953 return -EBUSY; 1954 ret = ops->ops_func(ops, FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_SELF); 1955 if (ret) 1956 return ret; 1957 } else if (is_ipmodify) { 1958 rec->flags |= FTRACE_FL_IPMODIFY; 1959 } 1960 } else if (is_ipmodify) { 1961 rec->flags &= ~FTRACE_FL_IPMODIFY; 1962 } 1963 } while_for_each_ftrace_rec(); 1964 1965 return 0; 1966 1967 rollback: 1968 end = rec; 1969 1970 /* Roll back what we did above */ 1971 do_for_each_ftrace_rec(pg, rec) { 1972 1973 if (rec->flags & FTRACE_FL_DISABLED) 1974 continue; 1975 1976 if (rec == end) 1977 goto err_out; 1978 1979 in_old = !!ftrace_lookup_ip(old_hash, rec->ip); 1980 in_new = !!ftrace_lookup_ip(new_hash, rec->ip); 1981 if (in_old == in_new) 1982 continue; 1983 1984 if (in_new) 1985 rec->flags &= ~FTRACE_FL_IPMODIFY; 1986 else 1987 rec->flags |= FTRACE_FL_IPMODIFY; 1988 } while_for_each_ftrace_rec(); 1989 1990 err_out: 1991 return -EBUSY; 1992 } 1993 1994 static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops) 1995 { 1996 struct ftrace_hash *hash = ops->func_hash->filter_hash; 1997 1998 if (ftrace_hash_empty(hash)) 1999 hash = NULL; 2000 2001 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash); 2002 } 2003 2004 /* Disabling always succeeds */ 2005 static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops) 2006 { 2007 struct ftrace_hash *hash = ops->func_hash->filter_hash; 2008 2009 if (ftrace_hash_empty(hash)) 2010 hash = NULL; 2011 2012 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH); 2013 } 2014 2015 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops, 2016 struct ftrace_hash *new_hash) 2017 { 2018 struct ftrace_hash *old_hash = ops->func_hash->filter_hash; 2019 2020 if (ftrace_hash_empty(old_hash)) 2021 old_hash = NULL; 2022 2023 if (ftrace_hash_empty(new_hash)) 2024 new_hash = NULL; 2025 2026 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash); 2027 } 2028 2029 static void print_ip_ins(const char *fmt, const unsigned char *p) 2030 { 2031 char ins[MCOUNT_INSN_SIZE]; 2032 2033 if (copy_from_kernel_nofault(ins, p, MCOUNT_INSN_SIZE)) { 2034 printk(KERN_CONT "%s[FAULT] %px\n", fmt, p); 2035 return; 2036 } 2037 2038 printk(KERN_CONT "%s", fmt); 2039 pr_cont("%*phC", MCOUNT_INSN_SIZE, ins); 2040 } 2041 2042 enum ftrace_bug_type ftrace_bug_type; 2043 const void *ftrace_expected; 2044 2045 static void print_bug_type(void) 2046 { 2047 switch (ftrace_bug_type) { 2048 case FTRACE_BUG_UNKNOWN: 2049 break; 2050 case FTRACE_BUG_INIT: 2051 pr_info("Initializing ftrace call sites\n"); 2052 break; 2053 case FTRACE_BUG_NOP: 2054 pr_info("Setting ftrace call site to NOP\n"); 2055 break; 2056 case FTRACE_BUG_CALL: 2057 pr_info("Setting ftrace call site to call ftrace function\n"); 2058 break; 2059 case FTRACE_BUG_UPDATE: 2060 pr_info("Updating ftrace call site to call a different ftrace function\n"); 2061 break; 2062 } 2063 } 2064 2065 /** 2066 * ftrace_bug - report and shutdown function tracer 2067 * @failed: The failed type (EFAULT, EINVAL, EPERM) 2068 * @rec: The record that failed 2069 * 2070 * The arch code that enables or disables the function tracing 2071 * can call ftrace_bug() when it has detected a problem in 2072 * modifying the code. @failed should be one of either: 2073 * EFAULT - if the problem happens on reading the @ip address 2074 * EINVAL - if what is read at @ip is not what was expected 2075 * EPERM - if the problem happens on writing to the @ip address 2076 */ 2077 void ftrace_bug(int failed, struct dyn_ftrace *rec) 2078 { 2079 unsigned long ip = rec ? rec->ip : 0; 2080 2081 pr_info("------------[ ftrace bug ]------------\n"); 2082 2083 switch (failed) { 2084 case -EFAULT: 2085 pr_info("ftrace faulted on modifying "); 2086 print_ip_sym(KERN_INFO, ip); 2087 break; 2088 case -EINVAL: 2089 pr_info("ftrace failed to modify "); 2090 print_ip_sym(KERN_INFO, ip); 2091 print_ip_ins(" actual: ", (unsigned char *)ip); 2092 pr_cont("\n"); 2093 if (ftrace_expected) { 2094 print_ip_ins(" expected: ", ftrace_expected); 2095 pr_cont("\n"); 2096 } 2097 break; 2098 case -EPERM: 2099 pr_info("ftrace faulted on writing "); 2100 print_ip_sym(KERN_INFO, ip); 2101 break; 2102 default: 2103 pr_info("ftrace faulted on unknown error "); 2104 print_ip_sym(KERN_INFO, ip); 2105 } 2106 print_bug_type(); 2107 if (rec) { 2108 struct ftrace_ops *ops = NULL; 2109 2110 pr_info("ftrace record flags: %lx\n", rec->flags); 2111 pr_cont(" (%ld)%s", ftrace_rec_count(rec), 2112 rec->flags & FTRACE_FL_REGS ? " R" : " "); 2113 if (rec->flags & FTRACE_FL_TRAMP_EN) { 2114 ops = ftrace_find_tramp_ops_any(rec); 2115 if (ops) { 2116 do { 2117 pr_cont("\ttramp: %pS (%pS)", 2118 (void *)ops->trampoline, 2119 (void *)ops->func); 2120 ops = ftrace_find_tramp_ops_next(rec, ops); 2121 } while (ops); 2122 } else 2123 pr_cont("\ttramp: ERROR!"); 2124 2125 } 2126 ip = ftrace_get_addr_curr(rec); 2127 pr_cont("\n expected tramp: %lx\n", ip); 2128 } 2129 2130 FTRACE_WARN_ON_ONCE(1); 2131 } 2132 2133 static int ftrace_check_record(struct dyn_ftrace *rec, bool enable, bool update) 2134 { 2135 unsigned long flag = 0UL; 2136 2137 ftrace_bug_type = FTRACE_BUG_UNKNOWN; 2138 2139 if (skip_record(rec)) 2140 return FTRACE_UPDATE_IGNORE; 2141 2142 /* 2143 * If we are updating calls: 2144 * 2145 * If the record has a ref count, then we need to enable it 2146 * because someone is using it. 2147 * 2148 * Otherwise we make sure its disabled. 2149 * 2150 * If we are disabling calls, then disable all records that 2151 * are enabled. 2152 */ 2153 if (enable && ftrace_rec_count(rec)) 2154 flag = FTRACE_FL_ENABLED; 2155 2156 /* 2157 * If enabling and the REGS flag does not match the REGS_EN, or 2158 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore 2159 * this record. Set flags to fail the compare against ENABLED. 2160 * Same for direct calls. 2161 */ 2162 if (flag) { 2163 if (!(rec->flags & FTRACE_FL_REGS) != 2164 !(rec->flags & FTRACE_FL_REGS_EN)) 2165 flag |= FTRACE_FL_REGS; 2166 2167 if (!(rec->flags & FTRACE_FL_TRAMP) != 2168 !(rec->flags & FTRACE_FL_TRAMP_EN)) 2169 flag |= FTRACE_FL_TRAMP; 2170 2171 /* 2172 * Direct calls are special, as count matters. 2173 * We must test the record for direct, if the 2174 * DIRECT and DIRECT_EN do not match, but only 2175 * if the count is 1. That's because, if the 2176 * count is something other than one, we do not 2177 * want the direct enabled (it will be done via the 2178 * direct helper). But if DIRECT_EN is set, and 2179 * the count is not one, we need to clear it. 2180 */ 2181 if (ftrace_rec_count(rec) == 1) { 2182 if (!(rec->flags & FTRACE_FL_DIRECT) != 2183 !(rec->flags & FTRACE_FL_DIRECT_EN)) 2184 flag |= FTRACE_FL_DIRECT; 2185 } else if (rec->flags & FTRACE_FL_DIRECT_EN) { 2186 flag |= FTRACE_FL_DIRECT; 2187 } 2188 } 2189 2190 /* If the state of this record hasn't changed, then do nothing */ 2191 if ((rec->flags & FTRACE_FL_ENABLED) == flag) 2192 return FTRACE_UPDATE_IGNORE; 2193 2194 if (flag) { 2195 /* Save off if rec is being enabled (for return value) */ 2196 flag ^= rec->flags & FTRACE_FL_ENABLED; 2197 2198 if (update) { 2199 rec->flags |= FTRACE_FL_ENABLED; 2200 if (flag & FTRACE_FL_REGS) { 2201 if (rec->flags & FTRACE_FL_REGS) 2202 rec->flags |= FTRACE_FL_REGS_EN; 2203 else 2204 rec->flags &= ~FTRACE_FL_REGS_EN; 2205 } 2206 if (flag & FTRACE_FL_TRAMP) { 2207 if (rec->flags & FTRACE_FL_TRAMP) 2208 rec->flags |= FTRACE_FL_TRAMP_EN; 2209 else 2210 rec->flags &= ~FTRACE_FL_TRAMP_EN; 2211 } 2212 2213 if (flag & FTRACE_FL_DIRECT) { 2214 /* 2215 * If there's only one user (direct_ops helper) 2216 * then we can call the direct function 2217 * directly (no ftrace trampoline). 2218 */ 2219 if (ftrace_rec_count(rec) == 1) { 2220 if (rec->flags & FTRACE_FL_DIRECT) 2221 rec->flags |= FTRACE_FL_DIRECT_EN; 2222 else 2223 rec->flags &= ~FTRACE_FL_DIRECT_EN; 2224 } else { 2225 /* 2226 * Can only call directly if there's 2227 * only one callback to the function. 2228 */ 2229 rec->flags &= ~FTRACE_FL_DIRECT_EN; 2230 } 2231 } 2232 } 2233 2234 /* 2235 * If this record is being updated from a nop, then 2236 * return UPDATE_MAKE_CALL. 2237 * Otherwise, 2238 * return UPDATE_MODIFY_CALL to tell the caller to convert 2239 * from the save regs, to a non-save regs function or 2240 * vice versa, or from a trampoline call. 2241 */ 2242 if (flag & FTRACE_FL_ENABLED) { 2243 ftrace_bug_type = FTRACE_BUG_CALL; 2244 return FTRACE_UPDATE_MAKE_CALL; 2245 } 2246 2247 ftrace_bug_type = FTRACE_BUG_UPDATE; 2248 return FTRACE_UPDATE_MODIFY_CALL; 2249 } 2250 2251 if (update) { 2252 /* If there's no more users, clear all flags */ 2253 if (!ftrace_rec_count(rec)) 2254 rec->flags &= FTRACE_FL_DISABLED; 2255 else 2256 /* 2257 * Just disable the record, but keep the ops TRAMP 2258 * and REGS states. The _EN flags must be disabled though. 2259 */ 2260 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN | 2261 FTRACE_FL_REGS_EN | FTRACE_FL_DIRECT_EN); 2262 } 2263 2264 ftrace_bug_type = FTRACE_BUG_NOP; 2265 return FTRACE_UPDATE_MAKE_NOP; 2266 } 2267 2268 /** 2269 * ftrace_update_record - set a record that now is tracing or not 2270 * @rec: the record to update 2271 * @enable: set to true if the record is tracing, false to force disable 2272 * 2273 * The records that represent all functions that can be traced need 2274 * to be updated when tracing has been enabled. 2275 */ 2276 int ftrace_update_record(struct dyn_ftrace *rec, bool enable) 2277 { 2278 return ftrace_check_record(rec, enable, true); 2279 } 2280 2281 /** 2282 * ftrace_test_record - check if the record has been enabled or not 2283 * @rec: the record to test 2284 * @enable: set to true to check if enabled, false if it is disabled 2285 * 2286 * The arch code may need to test if a record is already set to 2287 * tracing to determine how to modify the function code that it 2288 * represents. 2289 */ 2290 int ftrace_test_record(struct dyn_ftrace *rec, bool enable) 2291 { 2292 return ftrace_check_record(rec, enable, false); 2293 } 2294 2295 static struct ftrace_ops * 2296 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec) 2297 { 2298 struct ftrace_ops *op; 2299 unsigned long ip = rec->ip; 2300 2301 do_for_each_ftrace_op(op, ftrace_ops_list) { 2302 2303 if (!op->trampoline) 2304 continue; 2305 2306 if (hash_contains_ip(ip, op->func_hash)) 2307 return op; 2308 } while_for_each_ftrace_op(op); 2309 2310 return NULL; 2311 } 2312 2313 static struct ftrace_ops * 2314 ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude) 2315 { 2316 struct ftrace_ops *op; 2317 unsigned long ip = rec->ip; 2318 2319 do_for_each_ftrace_op(op, ftrace_ops_list) { 2320 2321 if (op == op_exclude || !op->trampoline) 2322 continue; 2323 2324 if (hash_contains_ip(ip, op->func_hash)) 2325 return op; 2326 } while_for_each_ftrace_op(op); 2327 2328 return NULL; 2329 } 2330 2331 static struct ftrace_ops * 2332 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, 2333 struct ftrace_ops *op) 2334 { 2335 unsigned long ip = rec->ip; 2336 2337 while_for_each_ftrace_op(op) { 2338 2339 if (!op->trampoline) 2340 continue; 2341 2342 if (hash_contains_ip(ip, op->func_hash)) 2343 return op; 2344 } 2345 2346 return NULL; 2347 } 2348 2349 static struct ftrace_ops * 2350 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec) 2351 { 2352 struct ftrace_ops *op; 2353 unsigned long ip = rec->ip; 2354 2355 /* 2356 * Need to check removed ops first. 2357 * If they are being removed, and this rec has a tramp, 2358 * and this rec is in the ops list, then it would be the 2359 * one with the tramp. 2360 */ 2361 if (removed_ops) { 2362 if (hash_contains_ip(ip, &removed_ops->old_hash)) 2363 return removed_ops; 2364 } 2365 2366 /* 2367 * Need to find the current trampoline for a rec. 2368 * Now, a trampoline is only attached to a rec if there 2369 * was a single 'ops' attached to it. But this can be called 2370 * when we are adding another op to the rec or removing the 2371 * current one. Thus, if the op is being added, we can 2372 * ignore it because it hasn't attached itself to the rec 2373 * yet. 2374 * 2375 * If an ops is being modified (hooking to different functions) 2376 * then we don't care about the new functions that are being 2377 * added, just the old ones (that are probably being removed). 2378 * 2379 * If we are adding an ops to a function that already is using 2380 * a trampoline, it needs to be removed (trampolines are only 2381 * for single ops connected), then an ops that is not being 2382 * modified also needs to be checked. 2383 */ 2384 do_for_each_ftrace_op(op, ftrace_ops_list) { 2385 2386 if (!op->trampoline) 2387 continue; 2388 2389 /* 2390 * If the ops is being added, it hasn't gotten to 2391 * the point to be removed from this tree yet. 2392 */ 2393 if (op->flags & FTRACE_OPS_FL_ADDING) 2394 continue; 2395 2396 2397 /* 2398 * If the ops is being modified and is in the old 2399 * hash, then it is probably being removed from this 2400 * function. 2401 */ 2402 if ((op->flags & FTRACE_OPS_FL_MODIFYING) && 2403 hash_contains_ip(ip, &op->old_hash)) 2404 return op; 2405 /* 2406 * If the ops is not being added or modified, and it's 2407 * in its normal filter hash, then this must be the one 2408 * we want! 2409 */ 2410 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) && 2411 hash_contains_ip(ip, op->func_hash)) 2412 return op; 2413 2414 } while_for_each_ftrace_op(op); 2415 2416 return NULL; 2417 } 2418 2419 static struct ftrace_ops * 2420 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec) 2421 { 2422 struct ftrace_ops *op; 2423 unsigned long ip = rec->ip; 2424 2425 do_for_each_ftrace_op(op, ftrace_ops_list) { 2426 /* pass rec in as regs to have non-NULL val */ 2427 if (hash_contains_ip(ip, op->func_hash)) 2428 return op; 2429 } while_for_each_ftrace_op(op); 2430 2431 return NULL; 2432 } 2433 2434 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS 2435 /* Protected by rcu_tasks for reading, and direct_mutex for writing */ 2436 static struct ftrace_hash *direct_functions = EMPTY_HASH; 2437 static DEFINE_MUTEX(direct_mutex); 2438 int ftrace_direct_func_count; 2439 2440 /* 2441 * Search the direct_functions hash to see if the given instruction pointer 2442 * has a direct caller attached to it. 2443 */ 2444 unsigned long ftrace_find_rec_direct(unsigned long ip) 2445 { 2446 struct ftrace_func_entry *entry; 2447 2448 entry = __ftrace_lookup_ip(direct_functions, ip); 2449 if (!entry) 2450 return 0; 2451 2452 return entry->direct; 2453 } 2454 2455 static struct ftrace_func_entry* 2456 ftrace_add_rec_direct(unsigned long ip, unsigned long addr, 2457 struct ftrace_hash **free_hash) 2458 { 2459 struct ftrace_func_entry *entry; 2460 2461 if (ftrace_hash_empty(direct_functions) || 2462 direct_functions->count > 2 * (1 << direct_functions->size_bits)) { 2463 struct ftrace_hash *new_hash; 2464 int size = ftrace_hash_empty(direct_functions) ? 0 : 2465 direct_functions->count + 1; 2466 2467 if (size < 32) 2468 size = 32; 2469 2470 new_hash = dup_hash(direct_functions, size); 2471 if (!new_hash) 2472 return NULL; 2473 2474 *free_hash = direct_functions; 2475 direct_functions = new_hash; 2476 } 2477 2478 entry = kmalloc(sizeof(*entry), GFP_KERNEL); 2479 if (!entry) 2480 return NULL; 2481 2482 entry->ip = ip; 2483 entry->direct = addr; 2484 __add_hash_entry(direct_functions, entry); 2485 return entry; 2486 } 2487 2488 static void call_direct_funcs(unsigned long ip, unsigned long pip, 2489 struct ftrace_ops *ops, struct ftrace_regs *fregs) 2490 { 2491 struct pt_regs *regs = ftrace_get_regs(fregs); 2492 unsigned long addr; 2493 2494 addr = ftrace_find_rec_direct(ip); 2495 if (!addr) 2496 return; 2497 2498 arch_ftrace_set_direct_caller(regs, addr); 2499 } 2500 2501 struct ftrace_ops direct_ops = { 2502 .func = call_direct_funcs, 2503 .flags = FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_REGS 2504 | FTRACE_OPS_FL_PERMANENT, 2505 /* 2506 * By declaring the main trampoline as this trampoline 2507 * it will never have one allocated for it. Allocated 2508 * trampolines should not call direct functions. 2509 * The direct_ops should only be called by the builtin 2510 * ftrace_regs_caller trampoline. 2511 */ 2512 .trampoline = FTRACE_REGS_ADDR, 2513 }; 2514 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */ 2515 2516 /** 2517 * ftrace_get_addr_new - Get the call address to set to 2518 * @rec: The ftrace record descriptor 2519 * 2520 * If the record has the FTRACE_FL_REGS set, that means that it 2521 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS 2522 * is not set, then it wants to convert to the normal callback. 2523 * 2524 * Returns the address of the trampoline to set to 2525 */ 2526 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec) 2527 { 2528 struct ftrace_ops *ops; 2529 unsigned long addr; 2530 2531 if ((rec->flags & FTRACE_FL_DIRECT) && 2532 (ftrace_rec_count(rec) == 1)) { 2533 addr = ftrace_find_rec_direct(rec->ip); 2534 if (addr) 2535 return addr; 2536 WARN_ON_ONCE(1); 2537 } 2538 2539 /* Trampolines take precedence over regs */ 2540 if (rec->flags & FTRACE_FL_TRAMP) { 2541 ops = ftrace_find_tramp_ops_new(rec); 2542 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) { 2543 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n", 2544 (void *)rec->ip, (void *)rec->ip, rec->flags); 2545 /* Ftrace is shutting down, return anything */ 2546 return (unsigned long)FTRACE_ADDR; 2547 } 2548 return ops->trampoline; 2549 } 2550 2551 if (rec->flags & FTRACE_FL_REGS) 2552 return (unsigned long)FTRACE_REGS_ADDR; 2553 else 2554 return (unsigned long)FTRACE_ADDR; 2555 } 2556 2557 /** 2558 * ftrace_get_addr_curr - Get the call address that is already there 2559 * @rec: The ftrace record descriptor 2560 * 2561 * The FTRACE_FL_REGS_EN is set when the record already points to 2562 * a function that saves all the regs. Basically the '_EN' version 2563 * represents the current state of the function. 2564 * 2565 * Returns the address of the trampoline that is currently being called 2566 */ 2567 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec) 2568 { 2569 struct ftrace_ops *ops; 2570 unsigned long addr; 2571 2572 /* Direct calls take precedence over trampolines */ 2573 if (rec->flags & FTRACE_FL_DIRECT_EN) { 2574 addr = ftrace_find_rec_direct(rec->ip); 2575 if (addr) 2576 return addr; 2577 WARN_ON_ONCE(1); 2578 } 2579 2580 /* Trampolines take precedence over regs */ 2581 if (rec->flags & FTRACE_FL_TRAMP_EN) { 2582 ops = ftrace_find_tramp_ops_curr(rec); 2583 if (FTRACE_WARN_ON(!ops)) { 2584 pr_warn("Bad trampoline accounting at: %p (%pS)\n", 2585 (void *)rec->ip, (void *)rec->ip); 2586 /* Ftrace is shutting down, return anything */ 2587 return (unsigned long)FTRACE_ADDR; 2588 } 2589 return ops->trampoline; 2590 } 2591 2592 if (rec->flags & FTRACE_FL_REGS_EN) 2593 return (unsigned long)FTRACE_REGS_ADDR; 2594 else 2595 return (unsigned long)FTRACE_ADDR; 2596 } 2597 2598 static int 2599 __ftrace_replace_code(struct dyn_ftrace *rec, bool enable) 2600 { 2601 unsigned long ftrace_old_addr; 2602 unsigned long ftrace_addr; 2603 int ret; 2604 2605 ftrace_addr = ftrace_get_addr_new(rec); 2606 2607 /* This needs to be done before we call ftrace_update_record */ 2608 ftrace_old_addr = ftrace_get_addr_curr(rec); 2609 2610 ret = ftrace_update_record(rec, enable); 2611 2612 ftrace_bug_type = FTRACE_BUG_UNKNOWN; 2613 2614 switch (ret) { 2615 case FTRACE_UPDATE_IGNORE: 2616 return 0; 2617 2618 case FTRACE_UPDATE_MAKE_CALL: 2619 ftrace_bug_type = FTRACE_BUG_CALL; 2620 return ftrace_make_call(rec, ftrace_addr); 2621 2622 case FTRACE_UPDATE_MAKE_NOP: 2623 ftrace_bug_type = FTRACE_BUG_NOP; 2624 return ftrace_make_nop(NULL, rec, ftrace_old_addr); 2625 2626 case FTRACE_UPDATE_MODIFY_CALL: 2627 ftrace_bug_type = FTRACE_BUG_UPDATE; 2628 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr); 2629 } 2630 2631 return -1; /* unknown ftrace bug */ 2632 } 2633 2634 void __weak ftrace_replace_code(int mod_flags) 2635 { 2636 struct dyn_ftrace *rec; 2637 struct ftrace_page *pg; 2638 bool enable = mod_flags & FTRACE_MODIFY_ENABLE_FL; 2639 int schedulable = mod_flags & FTRACE_MODIFY_MAY_SLEEP_FL; 2640 int failed; 2641 2642 if (unlikely(ftrace_disabled)) 2643 return; 2644 2645 do_for_each_ftrace_rec(pg, rec) { 2646 2647 if (skip_record(rec)) 2648 continue; 2649 2650 failed = __ftrace_replace_code(rec, enable); 2651 if (failed) { 2652 ftrace_bug(failed, rec); 2653 /* Stop processing */ 2654 return; 2655 } 2656 if (schedulable) 2657 cond_resched(); 2658 } while_for_each_ftrace_rec(); 2659 } 2660 2661 struct ftrace_rec_iter { 2662 struct ftrace_page *pg; 2663 int index; 2664 }; 2665 2666 /** 2667 * ftrace_rec_iter_start - start up iterating over traced functions 2668 * 2669 * Returns an iterator handle that is used to iterate over all 2670 * the records that represent address locations where functions 2671 * are traced. 2672 * 2673 * May return NULL if no records are available. 2674 */ 2675 struct ftrace_rec_iter *ftrace_rec_iter_start(void) 2676 { 2677 /* 2678 * We only use a single iterator. 2679 * Protected by the ftrace_lock mutex. 2680 */ 2681 static struct ftrace_rec_iter ftrace_rec_iter; 2682 struct ftrace_rec_iter *iter = &ftrace_rec_iter; 2683 2684 iter->pg = ftrace_pages_start; 2685 iter->index = 0; 2686 2687 /* Could have empty pages */ 2688 while (iter->pg && !iter->pg->index) 2689 iter->pg = iter->pg->next; 2690 2691 if (!iter->pg) 2692 return NULL; 2693 2694 return iter; 2695 } 2696 2697 /** 2698 * ftrace_rec_iter_next - get the next record to process. 2699 * @iter: The handle to the iterator. 2700 * 2701 * Returns the next iterator after the given iterator @iter. 2702 */ 2703 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter) 2704 { 2705 iter->index++; 2706 2707 if (iter->index >= iter->pg->index) { 2708 iter->pg = iter->pg->next; 2709 iter->index = 0; 2710 2711 /* Could have empty pages */ 2712 while (iter->pg && !iter->pg->index) 2713 iter->pg = iter->pg->next; 2714 } 2715 2716 if (!iter->pg) 2717 return NULL; 2718 2719 return iter; 2720 } 2721 2722 /** 2723 * ftrace_rec_iter_record - get the record at the iterator location 2724 * @iter: The current iterator location 2725 * 2726 * Returns the record that the current @iter is at. 2727 */ 2728 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter) 2729 { 2730 return &iter->pg->records[iter->index]; 2731 } 2732 2733 static int 2734 ftrace_nop_initialize(struct module *mod, struct dyn_ftrace *rec) 2735 { 2736 int ret; 2737 2738 if (unlikely(ftrace_disabled)) 2739 return 0; 2740 2741 ret = ftrace_init_nop(mod, rec); 2742 if (ret) { 2743 ftrace_bug_type = FTRACE_BUG_INIT; 2744 ftrace_bug(ret, rec); 2745 return 0; 2746 } 2747 return 1; 2748 } 2749 2750 /* 2751 * archs can override this function if they must do something 2752 * before the modifying code is performed. 2753 */ 2754 void __weak ftrace_arch_code_modify_prepare(void) 2755 { 2756 } 2757 2758 /* 2759 * archs can override this function if they must do something 2760 * after the modifying code is performed. 2761 */ 2762 void __weak ftrace_arch_code_modify_post_process(void) 2763 { 2764 } 2765 2766 static int update_ftrace_func(ftrace_func_t func) 2767 { 2768 static ftrace_func_t save_func; 2769 2770 /* Avoid updating if it hasn't changed */ 2771 if (func == save_func) 2772 return 0; 2773 2774 save_func = func; 2775 2776 return ftrace_update_ftrace_func(func); 2777 } 2778 2779 void ftrace_modify_all_code(int command) 2780 { 2781 int update = command & FTRACE_UPDATE_TRACE_FUNC; 2782 int mod_flags = 0; 2783 int err = 0; 2784 2785 if (command & FTRACE_MAY_SLEEP) 2786 mod_flags = FTRACE_MODIFY_MAY_SLEEP_FL; 2787 2788 /* 2789 * If the ftrace_caller calls a ftrace_ops func directly, 2790 * we need to make sure that it only traces functions it 2791 * expects to trace. When doing the switch of functions, 2792 * we need to update to the ftrace_ops_list_func first 2793 * before the transition between old and new calls are set, 2794 * as the ftrace_ops_list_func will check the ops hashes 2795 * to make sure the ops are having the right functions 2796 * traced. 2797 */ 2798 if (update) { 2799 err = update_ftrace_func(ftrace_ops_list_func); 2800 if (FTRACE_WARN_ON(err)) 2801 return; 2802 } 2803 2804 if (command & FTRACE_UPDATE_CALLS) 2805 ftrace_replace_code(mod_flags | FTRACE_MODIFY_ENABLE_FL); 2806 else if (command & FTRACE_DISABLE_CALLS) 2807 ftrace_replace_code(mod_flags); 2808 2809 if (update && ftrace_trace_function != ftrace_ops_list_func) { 2810 function_trace_op = set_function_trace_op; 2811 smp_wmb(); 2812 /* If irqs are disabled, we are in stop machine */ 2813 if (!irqs_disabled()) 2814 smp_call_function(ftrace_sync_ipi, NULL, 1); 2815 err = update_ftrace_func(ftrace_trace_function); 2816 if (FTRACE_WARN_ON(err)) 2817 return; 2818 } 2819 2820 if (command & FTRACE_START_FUNC_RET) 2821 err = ftrace_enable_ftrace_graph_caller(); 2822 else if (command & FTRACE_STOP_FUNC_RET) 2823 err = ftrace_disable_ftrace_graph_caller(); 2824 FTRACE_WARN_ON(err); 2825 } 2826 2827 static int __ftrace_modify_code(void *data) 2828 { 2829 int *command = data; 2830 2831 ftrace_modify_all_code(*command); 2832 2833 return 0; 2834 } 2835 2836 /** 2837 * ftrace_run_stop_machine - go back to the stop machine method 2838 * @command: The command to tell ftrace what to do 2839 * 2840 * If an arch needs to fall back to the stop machine method, the 2841 * it can call this function. 2842 */ 2843 void ftrace_run_stop_machine(int command) 2844 { 2845 stop_machine(__ftrace_modify_code, &command, NULL); 2846 } 2847 2848 /** 2849 * arch_ftrace_update_code - modify the code to trace or not trace 2850 * @command: The command that needs to be done 2851 * 2852 * Archs can override this function if it does not need to 2853 * run stop_machine() to modify code. 2854 */ 2855 void __weak arch_ftrace_update_code(int command) 2856 { 2857 ftrace_run_stop_machine(command); 2858 } 2859 2860 static void ftrace_run_update_code(int command) 2861 { 2862 ftrace_arch_code_modify_prepare(); 2863 2864 /* 2865 * By default we use stop_machine() to modify the code. 2866 * But archs can do what ever they want as long as it 2867 * is safe. The stop_machine() is the safest, but also 2868 * produces the most overhead. 2869 */ 2870 arch_ftrace_update_code(command); 2871 2872 ftrace_arch_code_modify_post_process(); 2873 } 2874 2875 static void ftrace_run_modify_code(struct ftrace_ops *ops, int command, 2876 struct ftrace_ops_hash *old_hash) 2877 { 2878 ops->flags |= FTRACE_OPS_FL_MODIFYING; 2879 ops->old_hash.filter_hash = old_hash->filter_hash; 2880 ops->old_hash.notrace_hash = old_hash->notrace_hash; 2881 ftrace_run_update_code(command); 2882 ops->old_hash.filter_hash = NULL; 2883 ops->old_hash.notrace_hash = NULL; 2884 ops->flags &= ~FTRACE_OPS_FL_MODIFYING; 2885 } 2886 2887 static ftrace_func_t saved_ftrace_func; 2888 static int ftrace_start_up; 2889 2890 void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops) 2891 { 2892 } 2893 2894 /* List of trace_ops that have allocated trampolines */ 2895 static LIST_HEAD(ftrace_ops_trampoline_list); 2896 2897 static void ftrace_add_trampoline_to_kallsyms(struct ftrace_ops *ops) 2898 { 2899 lockdep_assert_held(&ftrace_lock); 2900 list_add_rcu(&ops->list, &ftrace_ops_trampoline_list); 2901 } 2902 2903 static void ftrace_remove_trampoline_from_kallsyms(struct ftrace_ops *ops) 2904 { 2905 lockdep_assert_held(&ftrace_lock); 2906 list_del_rcu(&ops->list); 2907 synchronize_rcu(); 2908 } 2909 2910 /* 2911 * "__builtin__ftrace" is used as a module name in /proc/kallsyms for symbols 2912 * for pages allocated for ftrace purposes, even though "__builtin__ftrace" is 2913 * not a module. 2914 */ 2915 #define FTRACE_TRAMPOLINE_MOD "__builtin__ftrace" 2916 #define FTRACE_TRAMPOLINE_SYM "ftrace_trampoline" 2917 2918 static void ftrace_trampoline_free(struct ftrace_ops *ops) 2919 { 2920 if (ops && (ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP) && 2921 ops->trampoline) { 2922 /* 2923 * Record the text poke event before the ksymbol unregister 2924 * event. 2925 */ 2926 perf_event_text_poke((void *)ops->trampoline, 2927 (void *)ops->trampoline, 2928 ops->trampoline_size, NULL, 0); 2929 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL, 2930 ops->trampoline, ops->trampoline_size, 2931 true, FTRACE_TRAMPOLINE_SYM); 2932 /* Remove from kallsyms after the perf events */ 2933 ftrace_remove_trampoline_from_kallsyms(ops); 2934 } 2935 2936 arch_ftrace_trampoline_free(ops); 2937 } 2938 2939 static void ftrace_startup_enable(int command) 2940 { 2941 if (saved_ftrace_func != ftrace_trace_function) { 2942 saved_ftrace_func = ftrace_trace_function; 2943 command |= FTRACE_UPDATE_TRACE_FUNC; 2944 } 2945 2946 if (!command || !ftrace_enabled) 2947 return; 2948 2949 ftrace_run_update_code(command); 2950 } 2951 2952 static void ftrace_startup_all(int command) 2953 { 2954 update_all_ops = true; 2955 ftrace_startup_enable(command); 2956 update_all_ops = false; 2957 } 2958 2959 int ftrace_startup(struct ftrace_ops *ops, int command) 2960 { 2961 int ret; 2962 2963 if (unlikely(ftrace_disabled)) 2964 return -ENODEV; 2965 2966 ret = __register_ftrace_function(ops); 2967 if (ret) 2968 return ret; 2969 2970 ftrace_start_up++; 2971 2972 /* 2973 * Note that ftrace probes uses this to start up 2974 * and modify functions it will probe. But we still 2975 * set the ADDING flag for modification, as probes 2976 * do not have trampolines. If they add them in the 2977 * future, then the probes will need to distinguish 2978 * between adding and updating probes. 2979 */ 2980 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING; 2981 2982 ret = ftrace_hash_ipmodify_enable(ops); 2983 if (ret < 0) { 2984 /* Rollback registration process */ 2985 __unregister_ftrace_function(ops); 2986 ftrace_start_up--; 2987 ops->flags &= ~FTRACE_OPS_FL_ENABLED; 2988 if (ops->flags & FTRACE_OPS_FL_DYNAMIC) 2989 ftrace_trampoline_free(ops); 2990 return ret; 2991 } 2992 2993 if (ftrace_hash_rec_enable(ops, 1)) 2994 command |= FTRACE_UPDATE_CALLS; 2995 2996 ftrace_startup_enable(command); 2997 2998 /* 2999 * If ftrace is in an undefined state, we just remove ops from list 3000 * to prevent the NULL pointer, instead of totally rolling it back and 3001 * free trampoline, because those actions could cause further damage. 3002 */ 3003 if (unlikely(ftrace_disabled)) { 3004 __unregister_ftrace_function(ops); 3005 return -ENODEV; 3006 } 3007 3008 ops->flags &= ~FTRACE_OPS_FL_ADDING; 3009 3010 return 0; 3011 } 3012 3013 int ftrace_shutdown(struct ftrace_ops *ops, int command) 3014 { 3015 int ret; 3016 3017 if (unlikely(ftrace_disabled)) 3018 return -ENODEV; 3019 3020 ret = __unregister_ftrace_function(ops); 3021 if (ret) 3022 return ret; 3023 3024 ftrace_start_up--; 3025 /* 3026 * Just warn in case of unbalance, no need to kill ftrace, it's not 3027 * critical but the ftrace_call callers may be never nopped again after 3028 * further ftrace uses. 3029 */ 3030 WARN_ON_ONCE(ftrace_start_up < 0); 3031 3032 /* Disabling ipmodify never fails */ 3033 ftrace_hash_ipmodify_disable(ops); 3034 3035 if (ftrace_hash_rec_disable(ops, 1)) 3036 command |= FTRACE_UPDATE_CALLS; 3037 3038 ops->flags &= ~FTRACE_OPS_FL_ENABLED; 3039 3040 if (saved_ftrace_func != ftrace_trace_function) { 3041 saved_ftrace_func = ftrace_trace_function; 3042 command |= FTRACE_UPDATE_TRACE_FUNC; 3043 } 3044 3045 if (!command || !ftrace_enabled) 3046 goto out; 3047 3048 /* 3049 * If the ops uses a trampoline, then it needs to be 3050 * tested first on update. 3051 */ 3052 ops->flags |= FTRACE_OPS_FL_REMOVING; 3053 removed_ops = ops; 3054 3055 /* The trampoline logic checks the old hashes */ 3056 ops->old_hash.filter_hash = ops->func_hash->filter_hash; 3057 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash; 3058 3059 ftrace_run_update_code(command); 3060 3061 /* 3062 * If there's no more ops registered with ftrace, run a 3063 * sanity check to make sure all rec flags are cleared. 3064 */ 3065 if (rcu_dereference_protected(ftrace_ops_list, 3066 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) { 3067 struct ftrace_page *pg; 3068 struct dyn_ftrace *rec; 3069 3070 do_for_each_ftrace_rec(pg, rec) { 3071 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED)) 3072 pr_warn(" %pS flags:%lx\n", 3073 (void *)rec->ip, rec->flags); 3074 } while_for_each_ftrace_rec(); 3075 } 3076 3077 ops->old_hash.filter_hash = NULL; 3078 ops->old_hash.notrace_hash = NULL; 3079 3080 removed_ops = NULL; 3081 ops->flags &= ~FTRACE_OPS_FL_REMOVING; 3082 3083 out: 3084 /* 3085 * Dynamic ops may be freed, we must make sure that all 3086 * callers are done before leaving this function. 3087 */ 3088 if (ops->flags & FTRACE_OPS_FL_DYNAMIC) { 3089 /* 3090 * We need to do a hard force of sched synchronization. 3091 * This is because we use preempt_disable() to do RCU, but 3092 * the function tracers can be called where RCU is not watching 3093 * (like before user_exit()). We can not rely on the RCU 3094 * infrastructure to do the synchronization, thus we must do it 3095 * ourselves. 3096 */ 3097 synchronize_rcu_tasks_rude(); 3098 3099 /* 3100 * When the kernel is preemptive, tasks can be preempted 3101 * while on a ftrace trampoline. Just scheduling a task on 3102 * a CPU is not good enough to flush them. Calling 3103 * synchronize_rcu_tasks() will wait for those tasks to 3104 * execute and either schedule voluntarily or enter user space. 3105 */ 3106 if (IS_ENABLED(CONFIG_PREEMPTION)) 3107 synchronize_rcu_tasks(); 3108 3109 ftrace_trampoline_free(ops); 3110 } 3111 3112 return 0; 3113 } 3114 3115 static u64 ftrace_update_time; 3116 unsigned long ftrace_update_tot_cnt; 3117 unsigned long ftrace_number_of_pages; 3118 unsigned long ftrace_number_of_groups; 3119 3120 static inline int ops_traces_mod(struct ftrace_ops *ops) 3121 { 3122 /* 3123 * Filter_hash being empty will default to trace module. 3124 * But notrace hash requires a test of individual module functions. 3125 */ 3126 return ftrace_hash_empty(ops->func_hash->filter_hash) && 3127 ftrace_hash_empty(ops->func_hash->notrace_hash); 3128 } 3129 3130 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs) 3131 { 3132 bool init_nop = ftrace_need_init_nop(); 3133 struct ftrace_page *pg; 3134 struct dyn_ftrace *p; 3135 u64 start, stop; 3136 unsigned long update_cnt = 0; 3137 unsigned long rec_flags = 0; 3138 int i; 3139 3140 start = ftrace_now(raw_smp_processor_id()); 3141 3142 /* 3143 * When a module is loaded, this function is called to convert 3144 * the calls to mcount in its text to nops, and also to create 3145 * an entry in the ftrace data. Now, if ftrace is activated 3146 * after this call, but before the module sets its text to 3147 * read-only, the modification of enabling ftrace can fail if 3148 * the read-only is done while ftrace is converting the calls. 3149 * To prevent this, the module's records are set as disabled 3150 * and will be enabled after the call to set the module's text 3151 * to read-only. 3152 */ 3153 if (mod) 3154 rec_flags |= FTRACE_FL_DISABLED; 3155 3156 for (pg = new_pgs; pg; pg = pg->next) { 3157 3158 for (i = 0; i < pg->index; i++) { 3159 3160 /* If something went wrong, bail without enabling anything */ 3161 if (unlikely(ftrace_disabled)) 3162 return -1; 3163 3164 p = &pg->records[i]; 3165 p->flags = rec_flags; 3166 3167 /* 3168 * Do the initial record conversion from mcount jump 3169 * to the NOP instructions. 3170 */ 3171 if (init_nop && !ftrace_nop_initialize(mod, p)) 3172 break; 3173 3174 update_cnt++; 3175 } 3176 } 3177 3178 stop = ftrace_now(raw_smp_processor_id()); 3179 ftrace_update_time = stop - start; 3180 ftrace_update_tot_cnt += update_cnt; 3181 3182 return 0; 3183 } 3184 3185 static int ftrace_allocate_records(struct ftrace_page *pg, int count) 3186 { 3187 int order; 3188 int pages; 3189 int cnt; 3190 3191 if (WARN_ON(!count)) 3192 return -EINVAL; 3193 3194 /* We want to fill as much as possible, with no empty pages */ 3195 pages = DIV_ROUND_UP(count, ENTRIES_PER_PAGE); 3196 order = fls(pages) - 1; 3197 3198 again: 3199 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order); 3200 3201 if (!pg->records) { 3202 /* if we can't allocate this size, try something smaller */ 3203 if (!order) 3204 return -ENOMEM; 3205 order--; 3206 goto again; 3207 } 3208 3209 ftrace_number_of_pages += 1 << order; 3210 ftrace_number_of_groups++; 3211 3212 cnt = (PAGE_SIZE << order) / ENTRY_SIZE; 3213 pg->order = order; 3214 3215 if (cnt > count) 3216 cnt = count; 3217 3218 return cnt; 3219 } 3220 3221 static struct ftrace_page * 3222 ftrace_allocate_pages(unsigned long num_to_init) 3223 { 3224 struct ftrace_page *start_pg; 3225 struct ftrace_page *pg; 3226 int cnt; 3227 3228 if (!num_to_init) 3229 return NULL; 3230 3231 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL); 3232 if (!pg) 3233 return NULL; 3234 3235 /* 3236 * Try to allocate as much as possible in one continues 3237 * location that fills in all of the space. We want to 3238 * waste as little space as possible. 3239 */ 3240 for (;;) { 3241 cnt = ftrace_allocate_records(pg, num_to_init); 3242 if (cnt < 0) 3243 goto free_pages; 3244 3245 num_to_init -= cnt; 3246 if (!num_to_init) 3247 break; 3248 3249 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL); 3250 if (!pg->next) 3251 goto free_pages; 3252 3253 pg = pg->next; 3254 } 3255 3256 return start_pg; 3257 3258 free_pages: 3259 pg = start_pg; 3260 while (pg) { 3261 if (pg->records) { 3262 free_pages((unsigned long)pg->records, pg->order); 3263 ftrace_number_of_pages -= 1 << pg->order; 3264 } 3265 start_pg = pg->next; 3266 kfree(pg); 3267 pg = start_pg; 3268 ftrace_number_of_groups--; 3269 } 3270 pr_info("ftrace: FAILED to allocate memory for functions\n"); 3271 return NULL; 3272 } 3273 3274 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */ 3275 3276 struct ftrace_iterator { 3277 loff_t pos; 3278 loff_t func_pos; 3279 loff_t mod_pos; 3280 struct ftrace_page *pg; 3281 struct dyn_ftrace *func; 3282 struct ftrace_func_probe *probe; 3283 struct ftrace_func_entry *probe_entry; 3284 struct trace_parser parser; 3285 struct ftrace_hash *hash; 3286 struct ftrace_ops *ops; 3287 struct trace_array *tr; 3288 struct list_head *mod_list; 3289 int pidx; 3290 int idx; 3291 unsigned flags; 3292 }; 3293 3294 static void * 3295 t_probe_next(struct seq_file *m, loff_t *pos) 3296 { 3297 struct ftrace_iterator *iter = m->private; 3298 struct trace_array *tr = iter->ops->private; 3299 struct list_head *func_probes; 3300 struct ftrace_hash *hash; 3301 struct list_head *next; 3302 struct hlist_node *hnd = NULL; 3303 struct hlist_head *hhd; 3304 int size; 3305 3306 (*pos)++; 3307 iter->pos = *pos; 3308 3309 if (!tr) 3310 return NULL; 3311 3312 func_probes = &tr->func_probes; 3313 if (list_empty(func_probes)) 3314 return NULL; 3315 3316 if (!iter->probe) { 3317 next = func_probes->next; 3318 iter->probe = list_entry(next, struct ftrace_func_probe, list); 3319 } 3320 3321 if (iter->probe_entry) 3322 hnd = &iter->probe_entry->hlist; 3323 3324 hash = iter->probe->ops.func_hash->filter_hash; 3325 3326 /* 3327 * A probe being registered may temporarily have an empty hash 3328 * and it's at the end of the func_probes list. 3329 */ 3330 if (!hash || hash == EMPTY_HASH) 3331 return NULL; 3332 3333 size = 1 << hash->size_bits; 3334 3335 retry: 3336 if (iter->pidx >= size) { 3337 if (iter->probe->list.next == func_probes) 3338 return NULL; 3339 next = iter->probe->list.next; 3340 iter->probe = list_entry(next, struct ftrace_func_probe, list); 3341 hash = iter->probe->ops.func_hash->filter_hash; 3342 size = 1 << hash->size_bits; 3343 iter->pidx = 0; 3344 } 3345 3346 hhd = &hash->buckets[iter->pidx]; 3347 3348 if (hlist_empty(hhd)) { 3349 iter->pidx++; 3350 hnd = NULL; 3351 goto retry; 3352 } 3353 3354 if (!hnd) 3355 hnd = hhd->first; 3356 else { 3357 hnd = hnd->next; 3358 if (!hnd) { 3359 iter->pidx++; 3360 goto retry; 3361 } 3362 } 3363 3364 if (WARN_ON_ONCE(!hnd)) 3365 return NULL; 3366 3367 iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist); 3368 3369 return iter; 3370 } 3371 3372 static void *t_probe_start(struct seq_file *m, loff_t *pos) 3373 { 3374 struct ftrace_iterator *iter = m->private; 3375 void *p = NULL; 3376 loff_t l; 3377 3378 if (!(iter->flags & FTRACE_ITER_DO_PROBES)) 3379 return NULL; 3380 3381 if (iter->mod_pos > *pos) 3382 return NULL; 3383 3384 iter->probe = NULL; 3385 iter->probe_entry = NULL; 3386 iter->pidx = 0; 3387 for (l = 0; l <= (*pos - iter->mod_pos); ) { 3388 p = t_probe_next(m, &l); 3389 if (!p) 3390 break; 3391 } 3392 if (!p) 3393 return NULL; 3394 3395 /* Only set this if we have an item */ 3396 iter->flags |= FTRACE_ITER_PROBE; 3397 3398 return iter; 3399 } 3400 3401 static int 3402 t_probe_show(struct seq_file *m, struct ftrace_iterator *iter) 3403 { 3404 struct ftrace_func_entry *probe_entry; 3405 struct ftrace_probe_ops *probe_ops; 3406 struct ftrace_func_probe *probe; 3407 3408 probe = iter->probe; 3409 probe_entry = iter->probe_entry; 3410 3411 if (WARN_ON_ONCE(!probe || !probe_entry)) 3412 return -EIO; 3413 3414 probe_ops = probe->probe_ops; 3415 3416 if (probe_ops->print) 3417 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data); 3418 3419 seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip, 3420 (void *)probe_ops->func); 3421 3422 return 0; 3423 } 3424 3425 static void * 3426 t_mod_next(struct seq_file *m, loff_t *pos) 3427 { 3428 struct ftrace_iterator *iter = m->private; 3429 struct trace_array *tr = iter->tr; 3430 3431 (*pos)++; 3432 iter->pos = *pos; 3433 3434 iter->mod_list = iter->mod_list->next; 3435 3436 if (iter->mod_list == &tr->mod_trace || 3437 iter->mod_list == &tr->mod_notrace) { 3438 iter->flags &= ~FTRACE_ITER_MOD; 3439 return NULL; 3440 } 3441 3442 iter->mod_pos = *pos; 3443 3444 return iter; 3445 } 3446 3447 static void *t_mod_start(struct seq_file *m, loff_t *pos) 3448 { 3449 struct ftrace_iterator *iter = m->private; 3450 void *p = NULL; 3451 loff_t l; 3452 3453 if (iter->func_pos > *pos) 3454 return NULL; 3455 3456 iter->mod_pos = iter->func_pos; 3457 3458 /* probes are only available if tr is set */ 3459 if (!iter->tr) 3460 return NULL; 3461 3462 for (l = 0; l <= (*pos - iter->func_pos); ) { 3463 p = t_mod_next(m, &l); 3464 if (!p) 3465 break; 3466 } 3467 if (!p) { 3468 iter->flags &= ~FTRACE_ITER_MOD; 3469 return t_probe_start(m, pos); 3470 } 3471 3472 /* Only set this if we have an item */ 3473 iter->flags |= FTRACE_ITER_MOD; 3474 3475 return iter; 3476 } 3477 3478 static int 3479 t_mod_show(struct seq_file *m, struct ftrace_iterator *iter) 3480 { 3481 struct ftrace_mod_load *ftrace_mod; 3482 struct trace_array *tr = iter->tr; 3483 3484 if (WARN_ON_ONCE(!iter->mod_list) || 3485 iter->mod_list == &tr->mod_trace || 3486 iter->mod_list == &tr->mod_notrace) 3487 return -EIO; 3488 3489 ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list); 3490 3491 if (ftrace_mod->func) 3492 seq_printf(m, "%s", ftrace_mod->func); 3493 else 3494 seq_putc(m, '*'); 3495 3496 seq_printf(m, ":mod:%s\n", ftrace_mod->module); 3497 3498 return 0; 3499 } 3500 3501 static void * 3502 t_func_next(struct seq_file *m, loff_t *pos) 3503 { 3504 struct ftrace_iterator *iter = m->private; 3505 struct dyn_ftrace *rec = NULL; 3506 3507 (*pos)++; 3508 3509 retry: 3510 if (iter->idx >= iter->pg->index) { 3511 if (iter->pg->next) { 3512 iter->pg = iter->pg->next; 3513 iter->idx = 0; 3514 goto retry; 3515 } 3516 } else { 3517 rec = &iter->pg->records[iter->idx++]; 3518 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) && 3519 !ftrace_lookup_ip(iter->hash, rec->ip)) || 3520 3521 ((iter->flags & FTRACE_ITER_ENABLED) && 3522 !(rec->flags & FTRACE_FL_ENABLED))) { 3523 3524 rec = NULL; 3525 goto retry; 3526 } 3527 } 3528 3529 if (!rec) 3530 return NULL; 3531 3532 iter->pos = iter->func_pos = *pos; 3533 iter->func = rec; 3534 3535 return iter; 3536 } 3537 3538 static void * 3539 t_next(struct seq_file *m, void *v, loff_t *pos) 3540 { 3541 struct ftrace_iterator *iter = m->private; 3542 loff_t l = *pos; /* t_probe_start() must use original pos */ 3543 void *ret; 3544 3545 if (unlikely(ftrace_disabled)) 3546 return NULL; 3547 3548 if (iter->flags & FTRACE_ITER_PROBE) 3549 return t_probe_next(m, pos); 3550 3551 if (iter->flags & FTRACE_ITER_MOD) 3552 return t_mod_next(m, pos); 3553 3554 if (iter->flags & FTRACE_ITER_PRINTALL) { 3555 /* next must increment pos, and t_probe_start does not */ 3556 (*pos)++; 3557 return t_mod_start(m, &l); 3558 } 3559 3560 ret = t_func_next(m, pos); 3561 3562 if (!ret) 3563 return t_mod_start(m, &l); 3564 3565 return ret; 3566 } 3567 3568 static void reset_iter_read(struct ftrace_iterator *iter) 3569 { 3570 iter->pos = 0; 3571 iter->func_pos = 0; 3572 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD); 3573 } 3574 3575 static void *t_start(struct seq_file *m, loff_t *pos) 3576 { 3577 struct ftrace_iterator *iter = m->private; 3578 void *p = NULL; 3579 loff_t l; 3580 3581 mutex_lock(&ftrace_lock); 3582 3583 if (unlikely(ftrace_disabled)) 3584 return NULL; 3585 3586 /* 3587 * If an lseek was done, then reset and start from beginning. 3588 */ 3589 if (*pos < iter->pos) 3590 reset_iter_read(iter); 3591 3592 /* 3593 * For set_ftrace_filter reading, if we have the filter 3594 * off, we can short cut and just print out that all 3595 * functions are enabled. 3596 */ 3597 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) && 3598 ftrace_hash_empty(iter->hash)) { 3599 iter->func_pos = 1; /* Account for the message */ 3600 if (*pos > 0) 3601 return t_mod_start(m, pos); 3602 iter->flags |= FTRACE_ITER_PRINTALL; 3603 /* reset in case of seek/pread */ 3604 iter->flags &= ~FTRACE_ITER_PROBE; 3605 return iter; 3606 } 3607 3608 if (iter->flags & FTRACE_ITER_MOD) 3609 return t_mod_start(m, pos); 3610 3611 /* 3612 * Unfortunately, we need to restart at ftrace_pages_start 3613 * every time we let go of the ftrace_mutex. This is because 3614 * those pointers can change without the lock. 3615 */ 3616 iter->pg = ftrace_pages_start; 3617 iter->idx = 0; 3618 for (l = 0; l <= *pos; ) { 3619 p = t_func_next(m, &l); 3620 if (!p) 3621 break; 3622 } 3623 3624 if (!p) 3625 return t_mod_start(m, pos); 3626 3627 return iter; 3628 } 3629 3630 static void t_stop(struct seq_file *m, void *p) 3631 { 3632 mutex_unlock(&ftrace_lock); 3633 } 3634 3635 void * __weak 3636 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec) 3637 { 3638 return NULL; 3639 } 3640 3641 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops, 3642 struct dyn_ftrace *rec) 3643 { 3644 void *ptr; 3645 3646 ptr = arch_ftrace_trampoline_func(ops, rec); 3647 if (ptr) 3648 seq_printf(m, " ->%pS", ptr); 3649 } 3650 3651 #ifdef FTRACE_MCOUNT_MAX_OFFSET 3652 /* 3653 * Weak functions can still have an mcount/fentry that is saved in 3654 * the __mcount_loc section. These can be detected by having a 3655 * symbol offset of greater than FTRACE_MCOUNT_MAX_OFFSET, as the 3656 * symbol found by kallsyms is not the function that the mcount/fentry 3657 * is part of. The offset is much greater in these cases. 3658 * 3659 * Test the record to make sure that the ip points to a valid kallsyms 3660 * and if not, mark it disabled. 3661 */ 3662 static int test_for_valid_rec(struct dyn_ftrace *rec) 3663 { 3664 char str[KSYM_SYMBOL_LEN]; 3665 unsigned long offset; 3666 const char *ret; 3667 3668 ret = kallsyms_lookup(rec->ip, NULL, &offset, NULL, str); 3669 3670 /* Weak functions can cause invalid addresses */ 3671 if (!ret || offset > FTRACE_MCOUNT_MAX_OFFSET) { 3672 rec->flags |= FTRACE_FL_DISABLED; 3673 return 0; 3674 } 3675 return 1; 3676 } 3677 3678 static struct workqueue_struct *ftrace_check_wq __initdata; 3679 static struct work_struct ftrace_check_work __initdata; 3680 3681 /* 3682 * Scan all the mcount/fentry entries to make sure they are valid. 3683 */ 3684 static __init void ftrace_check_work_func(struct work_struct *work) 3685 { 3686 struct ftrace_page *pg; 3687 struct dyn_ftrace *rec; 3688 3689 mutex_lock(&ftrace_lock); 3690 do_for_each_ftrace_rec(pg, rec) { 3691 test_for_valid_rec(rec); 3692 } while_for_each_ftrace_rec(); 3693 mutex_unlock(&ftrace_lock); 3694 } 3695 3696 static int __init ftrace_check_for_weak_functions(void) 3697 { 3698 INIT_WORK(&ftrace_check_work, ftrace_check_work_func); 3699 3700 ftrace_check_wq = alloc_workqueue("ftrace_check_wq", WQ_UNBOUND, 0); 3701 3702 queue_work(ftrace_check_wq, &ftrace_check_work); 3703 return 0; 3704 } 3705 3706 static int __init ftrace_check_sync(void) 3707 { 3708 /* Make sure the ftrace_check updates are finished */ 3709 if (ftrace_check_wq) 3710 destroy_workqueue(ftrace_check_wq); 3711 return 0; 3712 } 3713 3714 late_initcall_sync(ftrace_check_sync); 3715 subsys_initcall(ftrace_check_for_weak_functions); 3716 3717 static int print_rec(struct seq_file *m, unsigned long ip) 3718 { 3719 unsigned long offset; 3720 char str[KSYM_SYMBOL_LEN]; 3721 char *modname; 3722 const char *ret; 3723 3724 ret = kallsyms_lookup(ip, NULL, &offset, &modname, str); 3725 /* Weak functions can cause invalid addresses */ 3726 if (!ret || offset > FTRACE_MCOUNT_MAX_OFFSET) { 3727 snprintf(str, KSYM_SYMBOL_LEN, "%s_%ld", 3728 FTRACE_INVALID_FUNCTION, offset); 3729 ret = NULL; 3730 } 3731 3732 seq_puts(m, str); 3733 if (modname) 3734 seq_printf(m, " [%s]", modname); 3735 return ret == NULL ? -1 : 0; 3736 } 3737 #else 3738 static inline int test_for_valid_rec(struct dyn_ftrace *rec) 3739 { 3740 return 1; 3741 } 3742 3743 static inline int print_rec(struct seq_file *m, unsigned long ip) 3744 { 3745 seq_printf(m, "%ps", (void *)ip); 3746 return 0; 3747 } 3748 #endif 3749 3750 static int t_show(struct seq_file *m, void *v) 3751 { 3752 struct ftrace_iterator *iter = m->private; 3753 struct dyn_ftrace *rec; 3754 3755 if (iter->flags & FTRACE_ITER_PROBE) 3756 return t_probe_show(m, iter); 3757 3758 if (iter->flags & FTRACE_ITER_MOD) 3759 return t_mod_show(m, iter); 3760 3761 if (iter->flags & FTRACE_ITER_PRINTALL) { 3762 if (iter->flags & FTRACE_ITER_NOTRACE) 3763 seq_puts(m, "#### no functions disabled ####\n"); 3764 else 3765 seq_puts(m, "#### all functions enabled ####\n"); 3766 return 0; 3767 } 3768 3769 rec = iter->func; 3770 3771 if (!rec) 3772 return 0; 3773 3774 if (print_rec(m, rec->ip)) { 3775 /* This should only happen when a rec is disabled */ 3776 WARN_ON_ONCE(!(rec->flags & FTRACE_FL_DISABLED)); 3777 seq_putc(m, '\n'); 3778 return 0; 3779 } 3780 3781 if (iter->flags & FTRACE_ITER_ENABLED) { 3782 struct ftrace_ops *ops; 3783 3784 seq_printf(m, " (%ld)%s%s%s", 3785 ftrace_rec_count(rec), 3786 rec->flags & FTRACE_FL_REGS ? " R" : " ", 3787 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ", 3788 rec->flags & FTRACE_FL_DIRECT ? " D" : " "); 3789 if (rec->flags & FTRACE_FL_TRAMP_EN) { 3790 ops = ftrace_find_tramp_ops_any(rec); 3791 if (ops) { 3792 do { 3793 seq_printf(m, "\ttramp: %pS (%pS)", 3794 (void *)ops->trampoline, 3795 (void *)ops->func); 3796 add_trampoline_func(m, ops, rec); 3797 ops = ftrace_find_tramp_ops_next(rec, ops); 3798 } while (ops); 3799 } else 3800 seq_puts(m, "\ttramp: ERROR!"); 3801 } else { 3802 add_trampoline_func(m, NULL, rec); 3803 } 3804 if (rec->flags & FTRACE_FL_DIRECT) { 3805 unsigned long direct; 3806 3807 direct = ftrace_find_rec_direct(rec->ip); 3808 if (direct) 3809 seq_printf(m, "\n\tdirect-->%pS", (void *)direct); 3810 } 3811 } 3812 3813 seq_putc(m, '\n'); 3814 3815 return 0; 3816 } 3817 3818 static const struct seq_operations show_ftrace_seq_ops = { 3819 .start = t_start, 3820 .next = t_next, 3821 .stop = t_stop, 3822 .show = t_show, 3823 }; 3824 3825 static int 3826 ftrace_avail_open(struct inode *inode, struct file *file) 3827 { 3828 struct ftrace_iterator *iter; 3829 int ret; 3830 3831 ret = security_locked_down(LOCKDOWN_TRACEFS); 3832 if (ret) 3833 return ret; 3834 3835 if (unlikely(ftrace_disabled)) 3836 return -ENODEV; 3837 3838 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter)); 3839 if (!iter) 3840 return -ENOMEM; 3841 3842 iter->pg = ftrace_pages_start; 3843 iter->ops = &global_ops; 3844 3845 return 0; 3846 } 3847 3848 static int 3849 ftrace_enabled_open(struct inode *inode, struct file *file) 3850 { 3851 struct ftrace_iterator *iter; 3852 3853 /* 3854 * This shows us what functions are currently being 3855 * traced and by what. Not sure if we want lockdown 3856 * to hide such critical information for an admin. 3857 * Although, perhaps it can show information we don't 3858 * want people to see, but if something is tracing 3859 * something, we probably want to know about it. 3860 */ 3861 3862 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter)); 3863 if (!iter) 3864 return -ENOMEM; 3865 3866 iter->pg = ftrace_pages_start; 3867 iter->flags = FTRACE_ITER_ENABLED; 3868 iter->ops = &global_ops; 3869 3870 return 0; 3871 } 3872 3873 /** 3874 * ftrace_regex_open - initialize function tracer filter files 3875 * @ops: The ftrace_ops that hold the hash filters 3876 * @flag: The type of filter to process 3877 * @inode: The inode, usually passed in to your open routine 3878 * @file: The file, usually passed in to your open routine 3879 * 3880 * ftrace_regex_open() initializes the filter files for the 3881 * @ops. Depending on @flag it may process the filter hash or 3882 * the notrace hash of @ops. With this called from the open 3883 * routine, you can use ftrace_filter_write() for the write 3884 * routine if @flag has FTRACE_ITER_FILTER set, or 3885 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set. 3886 * tracing_lseek() should be used as the lseek routine, and 3887 * release must call ftrace_regex_release(). 3888 */ 3889 int 3890 ftrace_regex_open(struct ftrace_ops *ops, int flag, 3891 struct inode *inode, struct file *file) 3892 { 3893 struct ftrace_iterator *iter; 3894 struct ftrace_hash *hash; 3895 struct list_head *mod_head; 3896 struct trace_array *tr = ops->private; 3897 int ret = -ENOMEM; 3898 3899 ftrace_ops_init(ops); 3900 3901 if (unlikely(ftrace_disabled)) 3902 return -ENODEV; 3903 3904 if (tracing_check_open_get_tr(tr)) 3905 return -ENODEV; 3906 3907 iter = kzalloc(sizeof(*iter), GFP_KERNEL); 3908 if (!iter) 3909 goto out; 3910 3911 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) 3912 goto out; 3913 3914 iter->ops = ops; 3915 iter->flags = flag; 3916 iter->tr = tr; 3917 3918 mutex_lock(&ops->func_hash->regex_lock); 3919 3920 if (flag & FTRACE_ITER_NOTRACE) { 3921 hash = ops->func_hash->notrace_hash; 3922 mod_head = tr ? &tr->mod_notrace : NULL; 3923 } else { 3924 hash = ops->func_hash->filter_hash; 3925 mod_head = tr ? &tr->mod_trace : NULL; 3926 } 3927 3928 iter->mod_list = mod_head; 3929 3930 if (file->f_mode & FMODE_WRITE) { 3931 const int size_bits = FTRACE_HASH_DEFAULT_BITS; 3932 3933 if (file->f_flags & O_TRUNC) { 3934 iter->hash = alloc_ftrace_hash(size_bits); 3935 clear_ftrace_mod_list(mod_head); 3936 } else { 3937 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash); 3938 } 3939 3940 if (!iter->hash) { 3941 trace_parser_put(&iter->parser); 3942 goto out_unlock; 3943 } 3944 } else 3945 iter->hash = hash; 3946 3947 ret = 0; 3948 3949 if (file->f_mode & FMODE_READ) { 3950 iter->pg = ftrace_pages_start; 3951 3952 ret = seq_open(file, &show_ftrace_seq_ops); 3953 if (!ret) { 3954 struct seq_file *m = file->private_data; 3955 m->private = iter; 3956 } else { 3957 /* Failed */ 3958 free_ftrace_hash(iter->hash); 3959 trace_parser_put(&iter->parser); 3960 } 3961 } else 3962 file->private_data = iter; 3963 3964 out_unlock: 3965 mutex_unlock(&ops->func_hash->regex_lock); 3966 3967 out: 3968 if (ret) { 3969 kfree(iter); 3970 if (tr) 3971 trace_array_put(tr); 3972 } 3973 3974 return ret; 3975 } 3976 3977 static int 3978 ftrace_filter_open(struct inode *inode, struct file *file) 3979 { 3980 struct ftrace_ops *ops = inode->i_private; 3981 3982 /* Checks for tracefs lockdown */ 3983 return ftrace_regex_open(ops, 3984 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES, 3985 inode, file); 3986 } 3987 3988 static int 3989 ftrace_notrace_open(struct inode *inode, struct file *file) 3990 { 3991 struct ftrace_ops *ops = inode->i_private; 3992 3993 /* Checks for tracefs lockdown */ 3994 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE, 3995 inode, file); 3996 } 3997 3998 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */ 3999 struct ftrace_glob { 4000 char *search; 4001 unsigned len; 4002 int type; 4003 }; 4004 4005 /* 4006 * If symbols in an architecture don't correspond exactly to the user-visible 4007 * name of what they represent, it is possible to define this function to 4008 * perform the necessary adjustments. 4009 */ 4010 char * __weak arch_ftrace_match_adjust(char *str, const char *search) 4011 { 4012 return str; 4013 } 4014 4015 static int ftrace_match(char *str, struct ftrace_glob *g) 4016 { 4017 int matched = 0; 4018 int slen; 4019 4020 str = arch_ftrace_match_adjust(str, g->search); 4021 4022 switch (g->type) { 4023 case MATCH_FULL: 4024 if (strcmp(str, g->search) == 0) 4025 matched = 1; 4026 break; 4027 case MATCH_FRONT_ONLY: 4028 if (strncmp(str, g->search, g->len) == 0) 4029 matched = 1; 4030 break; 4031 case MATCH_MIDDLE_ONLY: 4032 if (strstr(str, g->search)) 4033 matched = 1; 4034 break; 4035 case MATCH_END_ONLY: 4036 slen = strlen(str); 4037 if (slen >= g->len && 4038 memcmp(str + slen - g->len, g->search, g->len) == 0) 4039 matched = 1; 4040 break; 4041 case MATCH_GLOB: 4042 if (glob_match(g->search, str)) 4043 matched = 1; 4044 break; 4045 } 4046 4047 return matched; 4048 } 4049 4050 static int 4051 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter) 4052 { 4053 struct ftrace_func_entry *entry; 4054 int ret = 0; 4055 4056 entry = ftrace_lookup_ip(hash, rec->ip); 4057 if (clear_filter) { 4058 /* Do nothing if it doesn't exist */ 4059 if (!entry) 4060 return 0; 4061 4062 free_hash_entry(hash, entry); 4063 } else { 4064 /* Do nothing if it exists */ 4065 if (entry) 4066 return 0; 4067 4068 ret = add_hash_entry(hash, rec->ip); 4069 } 4070 return ret; 4071 } 4072 4073 static int 4074 add_rec_by_index(struct ftrace_hash *hash, struct ftrace_glob *func_g, 4075 int clear_filter) 4076 { 4077 long index = simple_strtoul(func_g->search, NULL, 0); 4078 struct ftrace_page *pg; 4079 struct dyn_ftrace *rec; 4080 4081 /* The index starts at 1 */ 4082 if (--index < 0) 4083 return 0; 4084 4085 do_for_each_ftrace_rec(pg, rec) { 4086 if (pg->index <= index) { 4087 index -= pg->index; 4088 /* this is a double loop, break goes to the next page */ 4089 break; 4090 } 4091 rec = &pg->records[index]; 4092 enter_record(hash, rec, clear_filter); 4093 return 1; 4094 } while_for_each_ftrace_rec(); 4095 return 0; 4096 } 4097 4098 #ifdef FTRACE_MCOUNT_MAX_OFFSET 4099 static int lookup_ip(unsigned long ip, char **modname, char *str) 4100 { 4101 unsigned long offset; 4102 4103 kallsyms_lookup(ip, NULL, &offset, modname, str); 4104 if (offset > FTRACE_MCOUNT_MAX_OFFSET) 4105 return -1; 4106 return 0; 4107 } 4108 #else 4109 static int lookup_ip(unsigned long ip, char **modname, char *str) 4110 { 4111 kallsyms_lookup(ip, NULL, NULL, modname, str); 4112 return 0; 4113 } 4114 #endif 4115 4116 static int 4117 ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g, 4118 struct ftrace_glob *mod_g, int exclude_mod) 4119 { 4120 char str[KSYM_SYMBOL_LEN]; 4121 char *modname; 4122 4123 if (lookup_ip(rec->ip, &modname, str)) { 4124 /* This should only happen when a rec is disabled */ 4125 WARN_ON_ONCE(system_state == SYSTEM_RUNNING && 4126 !(rec->flags & FTRACE_FL_DISABLED)); 4127 return 0; 4128 } 4129 4130 if (mod_g) { 4131 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0; 4132 4133 /* blank module name to match all modules */ 4134 if (!mod_g->len) { 4135 /* blank module globbing: modname xor exclude_mod */ 4136 if (!exclude_mod != !modname) 4137 goto func_match; 4138 return 0; 4139 } 4140 4141 /* 4142 * exclude_mod is set to trace everything but the given 4143 * module. If it is set and the module matches, then 4144 * return 0. If it is not set, and the module doesn't match 4145 * also return 0. Otherwise, check the function to see if 4146 * that matches. 4147 */ 4148 if (!mod_matches == !exclude_mod) 4149 return 0; 4150 func_match: 4151 /* blank search means to match all funcs in the mod */ 4152 if (!func_g->len) 4153 return 1; 4154 } 4155 4156 return ftrace_match(str, func_g); 4157 } 4158 4159 static int 4160 match_records(struct ftrace_hash *hash, char *func, int len, char *mod) 4161 { 4162 struct ftrace_page *pg; 4163 struct dyn_ftrace *rec; 4164 struct ftrace_glob func_g = { .type = MATCH_FULL }; 4165 struct ftrace_glob mod_g = { .type = MATCH_FULL }; 4166 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL; 4167 int exclude_mod = 0; 4168 int found = 0; 4169 int ret; 4170 int clear_filter = 0; 4171 4172 if (func) { 4173 func_g.type = filter_parse_regex(func, len, &func_g.search, 4174 &clear_filter); 4175 func_g.len = strlen(func_g.search); 4176 } 4177 4178 if (mod) { 4179 mod_g.type = filter_parse_regex(mod, strlen(mod), 4180 &mod_g.search, &exclude_mod); 4181 mod_g.len = strlen(mod_g.search); 4182 } 4183 4184 mutex_lock(&ftrace_lock); 4185 4186 if (unlikely(ftrace_disabled)) 4187 goto out_unlock; 4188 4189 if (func_g.type == MATCH_INDEX) { 4190 found = add_rec_by_index(hash, &func_g, clear_filter); 4191 goto out_unlock; 4192 } 4193 4194 do_for_each_ftrace_rec(pg, rec) { 4195 4196 if (rec->flags & FTRACE_FL_DISABLED) 4197 continue; 4198 4199 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) { 4200 ret = enter_record(hash, rec, clear_filter); 4201 if (ret < 0) { 4202 found = ret; 4203 goto out_unlock; 4204 } 4205 found = 1; 4206 } 4207 } while_for_each_ftrace_rec(); 4208 out_unlock: 4209 mutex_unlock(&ftrace_lock); 4210 4211 return found; 4212 } 4213 4214 static int 4215 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len) 4216 { 4217 return match_records(hash, buff, len, NULL); 4218 } 4219 4220 static void ftrace_ops_update_code(struct ftrace_ops *ops, 4221 struct ftrace_ops_hash *old_hash) 4222 { 4223 struct ftrace_ops *op; 4224 4225 if (!ftrace_enabled) 4226 return; 4227 4228 if (ops->flags & FTRACE_OPS_FL_ENABLED) { 4229 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash); 4230 return; 4231 } 4232 4233 /* 4234 * If this is the shared global_ops filter, then we need to 4235 * check if there is another ops that shares it, is enabled. 4236 * If so, we still need to run the modify code. 4237 */ 4238 if (ops->func_hash != &global_ops.local_hash) 4239 return; 4240 4241 do_for_each_ftrace_op(op, ftrace_ops_list) { 4242 if (op->func_hash == &global_ops.local_hash && 4243 op->flags & FTRACE_OPS_FL_ENABLED) { 4244 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash); 4245 /* Only need to do this once */ 4246 return; 4247 } 4248 } while_for_each_ftrace_op(op); 4249 } 4250 4251 static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops, 4252 struct ftrace_hash **orig_hash, 4253 struct ftrace_hash *hash, 4254 int enable) 4255 { 4256 struct ftrace_ops_hash old_hash_ops; 4257 struct ftrace_hash *old_hash; 4258 int ret; 4259 4260 old_hash = *orig_hash; 4261 old_hash_ops.filter_hash = ops->func_hash->filter_hash; 4262 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash; 4263 ret = ftrace_hash_move(ops, enable, orig_hash, hash); 4264 if (!ret) { 4265 ftrace_ops_update_code(ops, &old_hash_ops); 4266 free_ftrace_hash_rcu(old_hash); 4267 } 4268 return ret; 4269 } 4270 4271 static bool module_exists(const char *module) 4272 { 4273 /* All modules have the symbol __this_module */ 4274 static const char this_mod[] = "__this_module"; 4275 char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2]; 4276 unsigned long val; 4277 int n; 4278 4279 n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod); 4280 4281 if (n > sizeof(modname) - 1) 4282 return false; 4283 4284 val = module_kallsyms_lookup_name(modname); 4285 return val != 0; 4286 } 4287 4288 static int cache_mod(struct trace_array *tr, 4289 const char *func, char *module, int enable) 4290 { 4291 struct ftrace_mod_load *ftrace_mod, *n; 4292 struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace; 4293 int ret; 4294 4295 mutex_lock(&ftrace_lock); 4296 4297 /* We do not cache inverse filters */ 4298 if (func[0] == '!') { 4299 func++; 4300 ret = -EINVAL; 4301 4302 /* Look to remove this hash */ 4303 list_for_each_entry_safe(ftrace_mod, n, head, list) { 4304 if (strcmp(ftrace_mod->module, module) != 0) 4305 continue; 4306 4307 /* no func matches all */ 4308 if (strcmp(func, "*") == 0 || 4309 (ftrace_mod->func && 4310 strcmp(ftrace_mod->func, func) == 0)) { 4311 ret = 0; 4312 free_ftrace_mod(ftrace_mod); 4313 continue; 4314 } 4315 } 4316 goto out; 4317 } 4318 4319 ret = -EINVAL; 4320 /* We only care about modules that have not been loaded yet */ 4321 if (module_exists(module)) 4322 goto out; 4323 4324 /* Save this string off, and execute it when the module is loaded */ 4325 ret = ftrace_add_mod(tr, func, module, enable); 4326 out: 4327 mutex_unlock(&ftrace_lock); 4328 4329 return ret; 4330 } 4331 4332 static int 4333 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len, 4334 int reset, int enable); 4335 4336 #ifdef CONFIG_MODULES 4337 static void process_mod_list(struct list_head *head, struct ftrace_ops *ops, 4338 char *mod, bool enable) 4339 { 4340 struct ftrace_mod_load *ftrace_mod, *n; 4341 struct ftrace_hash **orig_hash, *new_hash; 4342 LIST_HEAD(process_mods); 4343 char *func; 4344 4345 mutex_lock(&ops->func_hash->regex_lock); 4346 4347 if (enable) 4348 orig_hash = &ops->func_hash->filter_hash; 4349 else 4350 orig_hash = &ops->func_hash->notrace_hash; 4351 4352 new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, 4353 *orig_hash); 4354 if (!new_hash) 4355 goto out; /* warn? */ 4356 4357 mutex_lock(&ftrace_lock); 4358 4359 list_for_each_entry_safe(ftrace_mod, n, head, list) { 4360 4361 if (strcmp(ftrace_mod->module, mod) != 0) 4362 continue; 4363 4364 if (ftrace_mod->func) 4365 func = kstrdup(ftrace_mod->func, GFP_KERNEL); 4366 else 4367 func = kstrdup("*", GFP_KERNEL); 4368 4369 if (!func) /* warn? */ 4370 continue; 4371 4372 list_move(&ftrace_mod->list, &process_mods); 4373 4374 /* Use the newly allocated func, as it may be "*" */ 4375 kfree(ftrace_mod->func); 4376 ftrace_mod->func = func; 4377 } 4378 4379 mutex_unlock(&ftrace_lock); 4380 4381 list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) { 4382 4383 func = ftrace_mod->func; 4384 4385 /* Grabs ftrace_lock, which is why we have this extra step */ 4386 match_records(new_hash, func, strlen(func), mod); 4387 free_ftrace_mod(ftrace_mod); 4388 } 4389 4390 if (enable && list_empty(head)) 4391 new_hash->flags &= ~FTRACE_HASH_FL_MOD; 4392 4393 mutex_lock(&ftrace_lock); 4394 4395 ftrace_hash_move_and_update_ops(ops, orig_hash, 4396 new_hash, enable); 4397 mutex_unlock(&ftrace_lock); 4398 4399 out: 4400 mutex_unlock(&ops->func_hash->regex_lock); 4401 4402 free_ftrace_hash(new_hash); 4403 } 4404 4405 static void process_cached_mods(const char *mod_name) 4406 { 4407 struct trace_array *tr; 4408 char *mod; 4409 4410 mod = kstrdup(mod_name, GFP_KERNEL); 4411 if (!mod) 4412 return; 4413 4414 mutex_lock(&trace_types_lock); 4415 list_for_each_entry(tr, &ftrace_trace_arrays, list) { 4416 if (!list_empty(&tr->mod_trace)) 4417 process_mod_list(&tr->mod_trace, tr->ops, mod, true); 4418 if (!list_empty(&tr->mod_notrace)) 4419 process_mod_list(&tr->mod_notrace, tr->ops, mod, false); 4420 } 4421 mutex_unlock(&trace_types_lock); 4422 4423 kfree(mod); 4424 } 4425 #endif 4426 4427 /* 4428 * We register the module command as a template to show others how 4429 * to register the a command as well. 4430 */ 4431 4432 static int 4433 ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash, 4434 char *func_orig, char *cmd, char *module, int enable) 4435 { 4436 char *func; 4437 int ret; 4438 4439 /* match_records() modifies func, and we need the original */ 4440 func = kstrdup(func_orig, GFP_KERNEL); 4441 if (!func) 4442 return -ENOMEM; 4443 4444 /* 4445 * cmd == 'mod' because we only registered this func 4446 * for the 'mod' ftrace_func_command. 4447 * But if you register one func with multiple commands, 4448 * you can tell which command was used by the cmd 4449 * parameter. 4450 */ 4451 ret = match_records(hash, func, strlen(func), module); 4452 kfree(func); 4453 4454 if (!ret) 4455 return cache_mod(tr, func_orig, module, enable); 4456 if (ret < 0) 4457 return ret; 4458 return 0; 4459 } 4460 4461 static struct ftrace_func_command ftrace_mod_cmd = { 4462 .name = "mod", 4463 .func = ftrace_mod_callback, 4464 }; 4465 4466 static int __init ftrace_mod_cmd_init(void) 4467 { 4468 return register_ftrace_command(&ftrace_mod_cmd); 4469 } 4470 core_initcall(ftrace_mod_cmd_init); 4471 4472 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip, 4473 struct ftrace_ops *op, struct ftrace_regs *fregs) 4474 { 4475 struct ftrace_probe_ops *probe_ops; 4476 struct ftrace_func_probe *probe; 4477 4478 probe = container_of(op, struct ftrace_func_probe, ops); 4479 probe_ops = probe->probe_ops; 4480 4481 /* 4482 * Disable preemption for these calls to prevent a RCU grace 4483 * period. This syncs the hash iteration and freeing of items 4484 * on the hash. rcu_read_lock is too dangerous here. 4485 */ 4486 preempt_disable_notrace(); 4487 probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data); 4488 preempt_enable_notrace(); 4489 } 4490 4491 struct ftrace_func_map { 4492 struct ftrace_func_entry entry; 4493 void *data; 4494 }; 4495 4496 struct ftrace_func_mapper { 4497 struct ftrace_hash hash; 4498 }; 4499 4500 /** 4501 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper 4502 * 4503 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data. 4504 */ 4505 struct ftrace_func_mapper *allocate_ftrace_func_mapper(void) 4506 { 4507 struct ftrace_hash *hash; 4508 4509 /* 4510 * The mapper is simply a ftrace_hash, but since the entries 4511 * in the hash are not ftrace_func_entry type, we define it 4512 * as a separate structure. 4513 */ 4514 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS); 4515 return (struct ftrace_func_mapper *)hash; 4516 } 4517 4518 /** 4519 * ftrace_func_mapper_find_ip - Find some data mapped to an ip 4520 * @mapper: The mapper that has the ip maps 4521 * @ip: the instruction pointer to find the data for 4522 * 4523 * Returns the data mapped to @ip if found otherwise NULL. The return 4524 * is actually the address of the mapper data pointer. The address is 4525 * returned for use cases where the data is no bigger than a long, and 4526 * the user can use the data pointer as its data instead of having to 4527 * allocate more memory for the reference. 4528 */ 4529 void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper, 4530 unsigned long ip) 4531 { 4532 struct ftrace_func_entry *entry; 4533 struct ftrace_func_map *map; 4534 4535 entry = ftrace_lookup_ip(&mapper->hash, ip); 4536 if (!entry) 4537 return NULL; 4538 4539 map = (struct ftrace_func_map *)entry; 4540 return &map->data; 4541 } 4542 4543 /** 4544 * ftrace_func_mapper_add_ip - Map some data to an ip 4545 * @mapper: The mapper that has the ip maps 4546 * @ip: The instruction pointer address to map @data to 4547 * @data: The data to map to @ip 4548 * 4549 * Returns 0 on success otherwise an error. 4550 */ 4551 int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper, 4552 unsigned long ip, void *data) 4553 { 4554 struct ftrace_func_entry *entry; 4555 struct ftrace_func_map *map; 4556 4557 entry = ftrace_lookup_ip(&mapper->hash, ip); 4558 if (entry) 4559 return -EBUSY; 4560 4561 map = kmalloc(sizeof(*map), GFP_KERNEL); 4562 if (!map) 4563 return -ENOMEM; 4564 4565 map->entry.ip = ip; 4566 map->data = data; 4567 4568 __add_hash_entry(&mapper->hash, &map->entry); 4569 4570 return 0; 4571 } 4572 4573 /** 4574 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping 4575 * @mapper: The mapper that has the ip maps 4576 * @ip: The instruction pointer address to remove the data from 4577 * 4578 * Returns the data if it is found, otherwise NULL. 4579 * Note, if the data pointer is used as the data itself, (see 4580 * ftrace_func_mapper_find_ip(), then the return value may be meaningless, 4581 * if the data pointer was set to zero. 4582 */ 4583 void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper, 4584 unsigned long ip) 4585 { 4586 struct ftrace_func_entry *entry; 4587 struct ftrace_func_map *map; 4588 void *data; 4589 4590 entry = ftrace_lookup_ip(&mapper->hash, ip); 4591 if (!entry) 4592 return NULL; 4593 4594 map = (struct ftrace_func_map *)entry; 4595 data = map->data; 4596 4597 remove_hash_entry(&mapper->hash, entry); 4598 kfree(entry); 4599 4600 return data; 4601 } 4602 4603 /** 4604 * free_ftrace_func_mapper - free a mapping of ips and data 4605 * @mapper: The mapper that has the ip maps 4606 * @free_func: A function to be called on each data item. 4607 * 4608 * This is used to free the function mapper. The @free_func is optional 4609 * and can be used if the data needs to be freed as well. 4610 */ 4611 void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper, 4612 ftrace_mapper_func free_func) 4613 { 4614 struct ftrace_func_entry *entry; 4615 struct ftrace_func_map *map; 4616 struct hlist_head *hhd; 4617 int size, i; 4618 4619 if (!mapper) 4620 return; 4621 4622 if (free_func && mapper->hash.count) { 4623 size = 1 << mapper->hash.size_bits; 4624 for (i = 0; i < size; i++) { 4625 hhd = &mapper->hash.buckets[i]; 4626 hlist_for_each_entry(entry, hhd, hlist) { 4627 map = (struct ftrace_func_map *)entry; 4628 free_func(map); 4629 } 4630 } 4631 } 4632 free_ftrace_hash(&mapper->hash); 4633 } 4634 4635 static void release_probe(struct ftrace_func_probe *probe) 4636 { 4637 struct ftrace_probe_ops *probe_ops; 4638 4639 mutex_lock(&ftrace_lock); 4640 4641 WARN_ON(probe->ref <= 0); 4642 4643 /* Subtract the ref that was used to protect this instance */ 4644 probe->ref--; 4645 4646 if (!probe->ref) { 4647 probe_ops = probe->probe_ops; 4648 /* 4649 * Sending zero as ip tells probe_ops to free 4650 * the probe->data itself 4651 */ 4652 if (probe_ops->free) 4653 probe_ops->free(probe_ops, probe->tr, 0, probe->data); 4654 list_del(&probe->list); 4655 kfree(probe); 4656 } 4657 mutex_unlock(&ftrace_lock); 4658 } 4659 4660 static void acquire_probe_locked(struct ftrace_func_probe *probe) 4661 { 4662 /* 4663 * Add one ref to keep it from being freed when releasing the 4664 * ftrace_lock mutex. 4665 */ 4666 probe->ref++; 4667 } 4668 4669 int 4670 register_ftrace_function_probe(char *glob, struct trace_array *tr, 4671 struct ftrace_probe_ops *probe_ops, 4672 void *data) 4673 { 4674 struct ftrace_func_probe *probe = NULL, *iter; 4675 struct ftrace_func_entry *entry; 4676 struct ftrace_hash **orig_hash; 4677 struct ftrace_hash *old_hash; 4678 struct ftrace_hash *hash; 4679 int count = 0; 4680 int size; 4681 int ret; 4682 int i; 4683 4684 if (WARN_ON(!tr)) 4685 return -EINVAL; 4686 4687 /* We do not support '!' for function probes */ 4688 if (WARN_ON(glob[0] == '!')) 4689 return -EINVAL; 4690 4691 4692 mutex_lock(&ftrace_lock); 4693 /* Check if the probe_ops is already registered */ 4694 list_for_each_entry(iter, &tr->func_probes, list) { 4695 if (iter->probe_ops == probe_ops) { 4696 probe = iter; 4697 break; 4698 } 4699 } 4700 if (!probe) { 4701 probe = kzalloc(sizeof(*probe), GFP_KERNEL); 4702 if (!probe) { 4703 mutex_unlock(&ftrace_lock); 4704 return -ENOMEM; 4705 } 4706 probe->probe_ops = probe_ops; 4707 probe->ops.func = function_trace_probe_call; 4708 probe->tr = tr; 4709 ftrace_ops_init(&probe->ops); 4710 list_add(&probe->list, &tr->func_probes); 4711 } 4712 4713 acquire_probe_locked(probe); 4714 4715 mutex_unlock(&ftrace_lock); 4716 4717 /* 4718 * Note, there's a small window here that the func_hash->filter_hash 4719 * may be NULL or empty. Need to be careful when reading the loop. 4720 */ 4721 mutex_lock(&probe->ops.func_hash->regex_lock); 4722 4723 orig_hash = &probe->ops.func_hash->filter_hash; 4724 old_hash = *orig_hash; 4725 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash); 4726 4727 if (!hash) { 4728 ret = -ENOMEM; 4729 goto out; 4730 } 4731 4732 ret = ftrace_match_records(hash, glob, strlen(glob)); 4733 4734 /* Nothing found? */ 4735 if (!ret) 4736 ret = -EINVAL; 4737 4738 if (ret < 0) 4739 goto out; 4740 4741 size = 1 << hash->size_bits; 4742 for (i = 0; i < size; i++) { 4743 hlist_for_each_entry(entry, &hash->buckets[i], hlist) { 4744 if (ftrace_lookup_ip(old_hash, entry->ip)) 4745 continue; 4746 /* 4747 * The caller might want to do something special 4748 * for each function we find. We call the callback 4749 * to give the caller an opportunity to do so. 4750 */ 4751 if (probe_ops->init) { 4752 ret = probe_ops->init(probe_ops, tr, 4753 entry->ip, data, 4754 &probe->data); 4755 if (ret < 0) { 4756 if (probe_ops->free && count) 4757 probe_ops->free(probe_ops, tr, 4758 0, probe->data); 4759 probe->data = NULL; 4760 goto out; 4761 } 4762 } 4763 count++; 4764 } 4765 } 4766 4767 mutex_lock(&ftrace_lock); 4768 4769 if (!count) { 4770 /* Nothing was added? */ 4771 ret = -EINVAL; 4772 goto out_unlock; 4773 } 4774 4775 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash, 4776 hash, 1); 4777 if (ret < 0) 4778 goto err_unlock; 4779 4780 /* One ref for each new function traced */ 4781 probe->ref += count; 4782 4783 if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED)) 4784 ret = ftrace_startup(&probe->ops, 0); 4785 4786 out_unlock: 4787 mutex_unlock(&ftrace_lock); 4788 4789 if (!ret) 4790 ret = count; 4791 out: 4792 mutex_unlock(&probe->ops.func_hash->regex_lock); 4793 free_ftrace_hash(hash); 4794 4795 release_probe(probe); 4796 4797 return ret; 4798 4799 err_unlock: 4800 if (!probe_ops->free || !count) 4801 goto out_unlock; 4802 4803 /* Failed to do the move, need to call the free functions */ 4804 for (i = 0; i < size; i++) { 4805 hlist_for_each_entry(entry, &hash->buckets[i], hlist) { 4806 if (ftrace_lookup_ip(old_hash, entry->ip)) 4807 continue; 4808 probe_ops->free(probe_ops, tr, entry->ip, probe->data); 4809 } 4810 } 4811 goto out_unlock; 4812 } 4813 4814 int 4815 unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr, 4816 struct ftrace_probe_ops *probe_ops) 4817 { 4818 struct ftrace_func_probe *probe = NULL, *iter; 4819 struct ftrace_ops_hash old_hash_ops; 4820 struct ftrace_func_entry *entry; 4821 struct ftrace_glob func_g; 4822 struct ftrace_hash **orig_hash; 4823 struct ftrace_hash *old_hash; 4824 struct ftrace_hash *hash = NULL; 4825 struct hlist_node *tmp; 4826 struct hlist_head hhd; 4827 char str[KSYM_SYMBOL_LEN]; 4828 int count = 0; 4829 int i, ret = -ENODEV; 4830 int size; 4831 4832 if (!glob || !strlen(glob) || !strcmp(glob, "*")) 4833 func_g.search = NULL; 4834 else { 4835 int not; 4836 4837 func_g.type = filter_parse_regex(glob, strlen(glob), 4838 &func_g.search, ¬); 4839 func_g.len = strlen(func_g.search); 4840 4841 /* we do not support '!' for function probes */ 4842 if (WARN_ON(not)) 4843 return -EINVAL; 4844 } 4845 4846 mutex_lock(&ftrace_lock); 4847 /* Check if the probe_ops is already registered */ 4848 list_for_each_entry(iter, &tr->func_probes, list) { 4849 if (iter->probe_ops == probe_ops) { 4850 probe = iter; 4851 break; 4852 } 4853 } 4854 if (!probe) 4855 goto err_unlock_ftrace; 4856 4857 ret = -EINVAL; 4858 if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED)) 4859 goto err_unlock_ftrace; 4860 4861 acquire_probe_locked(probe); 4862 4863 mutex_unlock(&ftrace_lock); 4864 4865 mutex_lock(&probe->ops.func_hash->regex_lock); 4866 4867 orig_hash = &probe->ops.func_hash->filter_hash; 4868 old_hash = *orig_hash; 4869 4870 if (ftrace_hash_empty(old_hash)) 4871 goto out_unlock; 4872 4873 old_hash_ops.filter_hash = old_hash; 4874 /* Probes only have filters */ 4875 old_hash_ops.notrace_hash = NULL; 4876 4877 ret = -ENOMEM; 4878 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash); 4879 if (!hash) 4880 goto out_unlock; 4881 4882 INIT_HLIST_HEAD(&hhd); 4883 4884 size = 1 << hash->size_bits; 4885 for (i = 0; i < size; i++) { 4886 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) { 4887 4888 if (func_g.search) { 4889 kallsyms_lookup(entry->ip, NULL, NULL, 4890 NULL, str); 4891 if (!ftrace_match(str, &func_g)) 4892 continue; 4893 } 4894 count++; 4895 remove_hash_entry(hash, entry); 4896 hlist_add_head(&entry->hlist, &hhd); 4897 } 4898 } 4899 4900 /* Nothing found? */ 4901 if (!count) { 4902 ret = -EINVAL; 4903 goto out_unlock; 4904 } 4905 4906 mutex_lock(&ftrace_lock); 4907 4908 WARN_ON(probe->ref < count); 4909 4910 probe->ref -= count; 4911 4912 if (ftrace_hash_empty(hash)) 4913 ftrace_shutdown(&probe->ops, 0); 4914 4915 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash, 4916 hash, 1); 4917 4918 /* still need to update the function call sites */ 4919 if (ftrace_enabled && !ftrace_hash_empty(hash)) 4920 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS, 4921 &old_hash_ops); 4922 synchronize_rcu(); 4923 4924 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) { 4925 hlist_del(&entry->hlist); 4926 if (probe_ops->free) 4927 probe_ops->free(probe_ops, tr, entry->ip, probe->data); 4928 kfree(entry); 4929 } 4930 mutex_unlock(&ftrace_lock); 4931 4932 out_unlock: 4933 mutex_unlock(&probe->ops.func_hash->regex_lock); 4934 free_ftrace_hash(hash); 4935 4936 release_probe(probe); 4937 4938 return ret; 4939 4940 err_unlock_ftrace: 4941 mutex_unlock(&ftrace_lock); 4942 return ret; 4943 } 4944 4945 void clear_ftrace_function_probes(struct trace_array *tr) 4946 { 4947 struct ftrace_func_probe *probe, *n; 4948 4949 list_for_each_entry_safe(probe, n, &tr->func_probes, list) 4950 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops); 4951 } 4952 4953 static LIST_HEAD(ftrace_commands); 4954 static DEFINE_MUTEX(ftrace_cmd_mutex); 4955 4956 /* 4957 * Currently we only register ftrace commands from __init, so mark this 4958 * __init too. 4959 */ 4960 __init int register_ftrace_command(struct ftrace_func_command *cmd) 4961 { 4962 struct ftrace_func_command *p; 4963 int ret = 0; 4964 4965 mutex_lock(&ftrace_cmd_mutex); 4966 list_for_each_entry(p, &ftrace_commands, list) { 4967 if (strcmp(cmd->name, p->name) == 0) { 4968 ret = -EBUSY; 4969 goto out_unlock; 4970 } 4971 } 4972 list_add(&cmd->list, &ftrace_commands); 4973 out_unlock: 4974 mutex_unlock(&ftrace_cmd_mutex); 4975 4976 return ret; 4977 } 4978 4979 /* 4980 * Currently we only unregister ftrace commands from __init, so mark 4981 * this __init too. 4982 */ 4983 __init int unregister_ftrace_command(struct ftrace_func_command *cmd) 4984 { 4985 struct ftrace_func_command *p, *n; 4986 int ret = -ENODEV; 4987 4988 mutex_lock(&ftrace_cmd_mutex); 4989 list_for_each_entry_safe(p, n, &ftrace_commands, list) { 4990 if (strcmp(cmd->name, p->name) == 0) { 4991 ret = 0; 4992 list_del_init(&p->list); 4993 goto out_unlock; 4994 } 4995 } 4996 out_unlock: 4997 mutex_unlock(&ftrace_cmd_mutex); 4998 4999 return ret; 5000 } 5001 5002 static int ftrace_process_regex(struct ftrace_iterator *iter, 5003 char *buff, int len, int enable) 5004 { 5005 struct ftrace_hash *hash = iter->hash; 5006 struct trace_array *tr = iter->ops->private; 5007 char *func, *command, *next = buff; 5008 struct ftrace_func_command *p; 5009 int ret = -EINVAL; 5010 5011 func = strsep(&next, ":"); 5012 5013 if (!next) { 5014 ret = ftrace_match_records(hash, func, len); 5015 if (!ret) 5016 ret = -EINVAL; 5017 if (ret < 0) 5018 return ret; 5019 return 0; 5020 } 5021 5022 /* command found */ 5023 5024 command = strsep(&next, ":"); 5025 5026 mutex_lock(&ftrace_cmd_mutex); 5027 list_for_each_entry(p, &ftrace_commands, list) { 5028 if (strcmp(p->name, command) == 0) { 5029 ret = p->func(tr, hash, func, command, next, enable); 5030 goto out_unlock; 5031 } 5032 } 5033 out_unlock: 5034 mutex_unlock(&ftrace_cmd_mutex); 5035 5036 return ret; 5037 } 5038 5039 static ssize_t 5040 ftrace_regex_write(struct file *file, const char __user *ubuf, 5041 size_t cnt, loff_t *ppos, int enable) 5042 { 5043 struct ftrace_iterator *iter; 5044 struct trace_parser *parser; 5045 ssize_t ret, read; 5046 5047 if (!cnt) 5048 return 0; 5049 5050 if (file->f_mode & FMODE_READ) { 5051 struct seq_file *m = file->private_data; 5052 iter = m->private; 5053 } else 5054 iter = file->private_data; 5055 5056 if (unlikely(ftrace_disabled)) 5057 return -ENODEV; 5058 5059 /* iter->hash is a local copy, so we don't need regex_lock */ 5060 5061 parser = &iter->parser; 5062 read = trace_get_user(parser, ubuf, cnt, ppos); 5063 5064 if (read >= 0 && trace_parser_loaded(parser) && 5065 !trace_parser_cont(parser)) { 5066 ret = ftrace_process_regex(iter, parser->buffer, 5067 parser->idx, enable); 5068 trace_parser_clear(parser); 5069 if (ret < 0) 5070 goto out; 5071 } 5072 5073 ret = read; 5074 out: 5075 return ret; 5076 } 5077 5078 ssize_t 5079 ftrace_filter_write(struct file *file, const char __user *ubuf, 5080 size_t cnt, loff_t *ppos) 5081 { 5082 return ftrace_regex_write(file, ubuf, cnt, ppos, 1); 5083 } 5084 5085 ssize_t 5086 ftrace_notrace_write(struct file *file, const char __user *ubuf, 5087 size_t cnt, loff_t *ppos) 5088 { 5089 return ftrace_regex_write(file, ubuf, cnt, ppos, 0); 5090 } 5091 5092 static int 5093 __ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove) 5094 { 5095 struct ftrace_func_entry *entry; 5096 5097 ip = ftrace_location(ip); 5098 if (!ip) 5099 return -EINVAL; 5100 5101 if (remove) { 5102 entry = ftrace_lookup_ip(hash, ip); 5103 if (!entry) 5104 return -ENOENT; 5105 free_hash_entry(hash, entry); 5106 return 0; 5107 } 5108 5109 return add_hash_entry(hash, ip); 5110 } 5111 5112 static int 5113 ftrace_match_addr(struct ftrace_hash *hash, unsigned long *ips, 5114 unsigned int cnt, int remove) 5115 { 5116 unsigned int i; 5117 int err; 5118 5119 for (i = 0; i < cnt; i++) { 5120 err = __ftrace_match_addr(hash, ips[i], remove); 5121 if (err) { 5122 /* 5123 * This expects the @hash is a temporary hash and if this 5124 * fails the caller must free the @hash. 5125 */ 5126 return err; 5127 } 5128 } 5129 return 0; 5130 } 5131 5132 static int 5133 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len, 5134 unsigned long *ips, unsigned int cnt, 5135 int remove, int reset, int enable) 5136 { 5137 struct ftrace_hash **orig_hash; 5138 struct ftrace_hash *hash; 5139 int ret; 5140 5141 if (unlikely(ftrace_disabled)) 5142 return -ENODEV; 5143 5144 mutex_lock(&ops->func_hash->regex_lock); 5145 5146 if (enable) 5147 orig_hash = &ops->func_hash->filter_hash; 5148 else 5149 orig_hash = &ops->func_hash->notrace_hash; 5150 5151 if (reset) 5152 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS); 5153 else 5154 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash); 5155 5156 if (!hash) { 5157 ret = -ENOMEM; 5158 goto out_regex_unlock; 5159 } 5160 5161 if (buf && !ftrace_match_records(hash, buf, len)) { 5162 ret = -EINVAL; 5163 goto out_regex_unlock; 5164 } 5165 if (ips) { 5166 ret = ftrace_match_addr(hash, ips, cnt, remove); 5167 if (ret < 0) 5168 goto out_regex_unlock; 5169 } 5170 5171 mutex_lock(&ftrace_lock); 5172 ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable); 5173 mutex_unlock(&ftrace_lock); 5174 5175 out_regex_unlock: 5176 mutex_unlock(&ops->func_hash->regex_lock); 5177 5178 free_ftrace_hash(hash); 5179 return ret; 5180 } 5181 5182 static int 5183 ftrace_set_addr(struct ftrace_ops *ops, unsigned long *ips, unsigned int cnt, 5184 int remove, int reset, int enable) 5185 { 5186 return ftrace_set_hash(ops, NULL, 0, ips, cnt, remove, reset, enable); 5187 } 5188 5189 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS 5190 5191 struct ftrace_direct_func { 5192 struct list_head next; 5193 unsigned long addr; 5194 int count; 5195 }; 5196 5197 static LIST_HEAD(ftrace_direct_funcs); 5198 5199 /** 5200 * ftrace_find_direct_func - test an address if it is a registered direct caller 5201 * @addr: The address of a registered direct caller 5202 * 5203 * This searches to see if a ftrace direct caller has been registered 5204 * at a specific address, and if so, it returns a descriptor for it. 5205 * 5206 * This can be used by architecture code to see if an address is 5207 * a direct caller (trampoline) attached to a fentry/mcount location. 5208 * This is useful for the function_graph tracer, as it may need to 5209 * do adjustments if it traced a location that also has a direct 5210 * trampoline attached to it. 5211 */ 5212 struct ftrace_direct_func *ftrace_find_direct_func(unsigned long addr) 5213 { 5214 struct ftrace_direct_func *entry; 5215 bool found = false; 5216 5217 /* May be called by fgraph trampoline (protected by rcu tasks) */ 5218 list_for_each_entry_rcu(entry, &ftrace_direct_funcs, next) { 5219 if (entry->addr == addr) { 5220 found = true; 5221 break; 5222 } 5223 } 5224 if (found) 5225 return entry; 5226 5227 return NULL; 5228 } 5229 5230 static struct ftrace_direct_func *ftrace_alloc_direct_func(unsigned long addr) 5231 { 5232 struct ftrace_direct_func *direct; 5233 5234 direct = kmalloc(sizeof(*direct), GFP_KERNEL); 5235 if (!direct) 5236 return NULL; 5237 direct->addr = addr; 5238 direct->count = 0; 5239 list_add_rcu(&direct->next, &ftrace_direct_funcs); 5240 ftrace_direct_func_count++; 5241 return direct; 5242 } 5243 5244 static int register_ftrace_function_nolock(struct ftrace_ops *ops); 5245 5246 /** 5247 * register_ftrace_direct - Call a custom trampoline directly 5248 * @ip: The address of the nop at the beginning of a function 5249 * @addr: The address of the trampoline to call at @ip 5250 * 5251 * This is used to connect a direct call from the nop location (@ip) 5252 * at the start of ftrace traced functions. The location that it calls 5253 * (@addr) must be able to handle a direct call, and save the parameters 5254 * of the function being traced, and restore them (or inject new ones 5255 * if needed), before returning. 5256 * 5257 * Returns: 5258 * 0 on success 5259 * -EBUSY - Another direct function is already attached (there can be only one) 5260 * -ENODEV - @ip does not point to a ftrace nop location (or not supported) 5261 * -ENOMEM - There was an allocation failure. 5262 */ 5263 int register_ftrace_direct(unsigned long ip, unsigned long addr) 5264 { 5265 struct ftrace_direct_func *direct; 5266 struct ftrace_func_entry *entry; 5267 struct ftrace_hash *free_hash = NULL; 5268 struct dyn_ftrace *rec; 5269 int ret = -ENODEV; 5270 5271 mutex_lock(&direct_mutex); 5272 5273 ip = ftrace_location(ip); 5274 if (!ip) 5275 goto out_unlock; 5276 5277 /* See if there's a direct function at @ip already */ 5278 ret = -EBUSY; 5279 if (ftrace_find_rec_direct(ip)) 5280 goto out_unlock; 5281 5282 ret = -ENODEV; 5283 rec = lookup_rec(ip, ip); 5284 if (!rec) 5285 goto out_unlock; 5286 5287 /* 5288 * Check if the rec says it has a direct call but we didn't 5289 * find one earlier? 5290 */ 5291 if (WARN_ON(rec->flags & FTRACE_FL_DIRECT)) 5292 goto out_unlock; 5293 5294 /* Make sure the ip points to the exact record */ 5295 if (ip != rec->ip) { 5296 ip = rec->ip; 5297 /* Need to check this ip for a direct. */ 5298 if (ftrace_find_rec_direct(ip)) 5299 goto out_unlock; 5300 } 5301 5302 ret = -ENOMEM; 5303 direct = ftrace_find_direct_func(addr); 5304 if (!direct) { 5305 direct = ftrace_alloc_direct_func(addr); 5306 if (!direct) 5307 goto out_unlock; 5308 } 5309 5310 entry = ftrace_add_rec_direct(ip, addr, &free_hash); 5311 if (!entry) 5312 goto out_unlock; 5313 5314 ret = ftrace_set_filter_ip(&direct_ops, ip, 0, 0); 5315 5316 if (!ret && !(direct_ops.flags & FTRACE_OPS_FL_ENABLED)) { 5317 ret = register_ftrace_function_nolock(&direct_ops); 5318 if (ret) 5319 ftrace_set_filter_ip(&direct_ops, ip, 1, 0); 5320 } 5321 5322 if (ret) { 5323 remove_hash_entry(direct_functions, entry); 5324 kfree(entry); 5325 if (!direct->count) { 5326 list_del_rcu(&direct->next); 5327 synchronize_rcu_tasks(); 5328 kfree(direct); 5329 if (free_hash) 5330 free_ftrace_hash(free_hash); 5331 free_hash = NULL; 5332 ftrace_direct_func_count--; 5333 } 5334 } else { 5335 direct->count++; 5336 } 5337 out_unlock: 5338 mutex_unlock(&direct_mutex); 5339 5340 if (free_hash) { 5341 synchronize_rcu_tasks(); 5342 free_ftrace_hash(free_hash); 5343 } 5344 5345 return ret; 5346 } 5347 EXPORT_SYMBOL_GPL(register_ftrace_direct); 5348 5349 static struct ftrace_func_entry *find_direct_entry(unsigned long *ip, 5350 struct dyn_ftrace **recp) 5351 { 5352 struct ftrace_func_entry *entry; 5353 struct dyn_ftrace *rec; 5354 5355 rec = lookup_rec(*ip, *ip); 5356 if (!rec) 5357 return NULL; 5358 5359 entry = __ftrace_lookup_ip(direct_functions, rec->ip); 5360 if (!entry) { 5361 WARN_ON(rec->flags & FTRACE_FL_DIRECT); 5362 return NULL; 5363 } 5364 5365 WARN_ON(!(rec->flags & FTRACE_FL_DIRECT)); 5366 5367 /* Passed in ip just needs to be on the call site */ 5368 *ip = rec->ip; 5369 5370 if (recp) 5371 *recp = rec; 5372 5373 return entry; 5374 } 5375 5376 int unregister_ftrace_direct(unsigned long ip, unsigned long addr) 5377 { 5378 struct ftrace_direct_func *direct; 5379 struct ftrace_func_entry *entry; 5380 struct ftrace_hash *hash; 5381 int ret = -ENODEV; 5382 5383 mutex_lock(&direct_mutex); 5384 5385 ip = ftrace_location(ip); 5386 if (!ip) 5387 goto out_unlock; 5388 5389 entry = find_direct_entry(&ip, NULL); 5390 if (!entry) 5391 goto out_unlock; 5392 5393 hash = direct_ops.func_hash->filter_hash; 5394 if (hash->count == 1) 5395 unregister_ftrace_function(&direct_ops); 5396 5397 ret = ftrace_set_filter_ip(&direct_ops, ip, 1, 0); 5398 5399 WARN_ON(ret); 5400 5401 remove_hash_entry(direct_functions, entry); 5402 5403 direct = ftrace_find_direct_func(addr); 5404 if (!WARN_ON(!direct)) { 5405 /* This is the good path (see the ! before WARN) */ 5406 direct->count--; 5407 WARN_ON(direct->count < 0); 5408 if (!direct->count) { 5409 list_del_rcu(&direct->next); 5410 synchronize_rcu_tasks(); 5411 kfree(direct); 5412 kfree(entry); 5413 ftrace_direct_func_count--; 5414 } 5415 } 5416 out_unlock: 5417 mutex_unlock(&direct_mutex); 5418 5419 return ret; 5420 } 5421 EXPORT_SYMBOL_GPL(unregister_ftrace_direct); 5422 5423 static struct ftrace_ops stub_ops = { 5424 .func = ftrace_stub, 5425 }; 5426 5427 /** 5428 * ftrace_modify_direct_caller - modify ftrace nop directly 5429 * @entry: The ftrace hash entry of the direct helper for @rec 5430 * @rec: The record representing the function site to patch 5431 * @old_addr: The location that the site at @rec->ip currently calls 5432 * @new_addr: The location that the site at @rec->ip should call 5433 * 5434 * An architecture may overwrite this function to optimize the 5435 * changing of the direct callback on an ftrace nop location. 5436 * This is called with the ftrace_lock mutex held, and no other 5437 * ftrace callbacks are on the associated record (@rec). Thus, 5438 * it is safe to modify the ftrace record, where it should be 5439 * currently calling @old_addr directly, to call @new_addr. 5440 * 5441 * This is called with direct_mutex locked. 5442 * 5443 * Safety checks should be made to make sure that the code at 5444 * @rec->ip is currently calling @old_addr. And this must 5445 * also update entry->direct to @new_addr. 5446 */ 5447 int __weak ftrace_modify_direct_caller(struct ftrace_func_entry *entry, 5448 struct dyn_ftrace *rec, 5449 unsigned long old_addr, 5450 unsigned long new_addr) 5451 { 5452 unsigned long ip = rec->ip; 5453 int ret; 5454 5455 lockdep_assert_held(&direct_mutex); 5456 5457 /* 5458 * The ftrace_lock was used to determine if the record 5459 * had more than one registered user to it. If it did, 5460 * we needed to prevent that from changing to do the quick 5461 * switch. But if it did not (only a direct caller was attached) 5462 * then this function is called. But this function can deal 5463 * with attached callers to the rec that we care about, and 5464 * since this function uses standard ftrace calls that take 5465 * the ftrace_lock mutex, we need to release it. 5466 */ 5467 mutex_unlock(&ftrace_lock); 5468 5469 /* 5470 * By setting a stub function at the same address, we force 5471 * the code to call the iterator and the direct_ops helper. 5472 * This means that @ip does not call the direct call, and 5473 * we can simply modify it. 5474 */ 5475 ret = ftrace_set_filter_ip(&stub_ops, ip, 0, 0); 5476 if (ret) 5477 goto out_lock; 5478 5479 ret = register_ftrace_function_nolock(&stub_ops); 5480 if (ret) { 5481 ftrace_set_filter_ip(&stub_ops, ip, 1, 0); 5482 goto out_lock; 5483 } 5484 5485 entry->direct = new_addr; 5486 5487 /* 5488 * By removing the stub, we put back the direct call, calling 5489 * the @new_addr. 5490 */ 5491 unregister_ftrace_function(&stub_ops); 5492 ftrace_set_filter_ip(&stub_ops, ip, 1, 0); 5493 5494 out_lock: 5495 mutex_lock(&ftrace_lock); 5496 5497 return ret; 5498 } 5499 5500 /** 5501 * modify_ftrace_direct - Modify an existing direct call to call something else 5502 * @ip: The instruction pointer to modify 5503 * @old_addr: The address that the current @ip calls directly 5504 * @new_addr: The address that the @ip should call 5505 * 5506 * This modifies a ftrace direct caller at an instruction pointer without 5507 * having to disable it first. The direct call will switch over to the 5508 * @new_addr without missing anything. 5509 * 5510 * Returns: zero on success. Non zero on error, which includes: 5511 * -ENODEV : the @ip given has no direct caller attached 5512 * -EINVAL : the @old_addr does not match the current direct caller 5513 */ 5514 int modify_ftrace_direct(unsigned long ip, 5515 unsigned long old_addr, unsigned long new_addr) 5516 { 5517 struct ftrace_direct_func *direct, *new_direct = NULL; 5518 struct ftrace_func_entry *entry; 5519 struct dyn_ftrace *rec; 5520 int ret = -ENODEV; 5521 5522 mutex_lock(&direct_mutex); 5523 5524 mutex_lock(&ftrace_lock); 5525 5526 ip = ftrace_location(ip); 5527 if (!ip) 5528 goto out_unlock; 5529 5530 entry = find_direct_entry(&ip, &rec); 5531 if (!entry) 5532 goto out_unlock; 5533 5534 ret = -EINVAL; 5535 if (entry->direct != old_addr) 5536 goto out_unlock; 5537 5538 direct = ftrace_find_direct_func(old_addr); 5539 if (WARN_ON(!direct)) 5540 goto out_unlock; 5541 if (direct->count > 1) { 5542 ret = -ENOMEM; 5543 new_direct = ftrace_alloc_direct_func(new_addr); 5544 if (!new_direct) 5545 goto out_unlock; 5546 direct->count--; 5547 new_direct->count++; 5548 } else { 5549 direct->addr = new_addr; 5550 } 5551 5552 /* 5553 * If there's no other ftrace callback on the rec->ip location, 5554 * then it can be changed directly by the architecture. 5555 * If there is another caller, then we just need to change the 5556 * direct caller helper to point to @new_addr. 5557 */ 5558 if (ftrace_rec_count(rec) == 1) { 5559 ret = ftrace_modify_direct_caller(entry, rec, old_addr, new_addr); 5560 } else { 5561 entry->direct = new_addr; 5562 ret = 0; 5563 } 5564 5565 if (unlikely(ret && new_direct)) { 5566 direct->count++; 5567 list_del_rcu(&new_direct->next); 5568 synchronize_rcu_tasks(); 5569 kfree(new_direct); 5570 ftrace_direct_func_count--; 5571 } 5572 5573 out_unlock: 5574 mutex_unlock(&ftrace_lock); 5575 mutex_unlock(&direct_mutex); 5576 return ret; 5577 } 5578 EXPORT_SYMBOL_GPL(modify_ftrace_direct); 5579 5580 #define MULTI_FLAGS (FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_REGS) 5581 5582 static int check_direct_multi(struct ftrace_ops *ops) 5583 { 5584 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) 5585 return -EINVAL; 5586 if ((ops->flags & MULTI_FLAGS) != MULTI_FLAGS) 5587 return -EINVAL; 5588 return 0; 5589 } 5590 5591 static void remove_direct_functions_hash(struct ftrace_hash *hash, unsigned long addr) 5592 { 5593 struct ftrace_func_entry *entry, *del; 5594 int size, i; 5595 5596 size = 1 << hash->size_bits; 5597 for (i = 0; i < size; i++) { 5598 hlist_for_each_entry(entry, &hash->buckets[i], hlist) { 5599 del = __ftrace_lookup_ip(direct_functions, entry->ip); 5600 if (del && del->direct == addr) { 5601 remove_hash_entry(direct_functions, del); 5602 kfree(del); 5603 } 5604 } 5605 } 5606 } 5607 5608 /** 5609 * register_ftrace_direct_multi - Call a custom trampoline directly 5610 * for multiple functions registered in @ops 5611 * @ops: The address of the struct ftrace_ops object 5612 * @addr: The address of the trampoline to call at @ops functions 5613 * 5614 * This is used to connect a direct calls to @addr from the nop locations 5615 * of the functions registered in @ops (with by ftrace_set_filter_ip 5616 * function). 5617 * 5618 * The location that it calls (@addr) must be able to handle a direct call, 5619 * and save the parameters of the function being traced, and restore them 5620 * (or inject new ones if needed), before returning. 5621 * 5622 * Returns: 5623 * 0 on success 5624 * -EINVAL - The @ops object was already registered with this call or 5625 * when there are no functions in @ops object. 5626 * -EBUSY - Another direct function is already attached (there can be only one) 5627 * -ENODEV - @ip does not point to a ftrace nop location (or not supported) 5628 * -ENOMEM - There was an allocation failure. 5629 */ 5630 int register_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr) 5631 { 5632 struct ftrace_hash *hash, *free_hash = NULL; 5633 struct ftrace_func_entry *entry, *new; 5634 int err = -EBUSY, size, i; 5635 5636 if (ops->func || ops->trampoline) 5637 return -EINVAL; 5638 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) 5639 return -EINVAL; 5640 if (ops->flags & FTRACE_OPS_FL_ENABLED) 5641 return -EINVAL; 5642 5643 hash = ops->func_hash->filter_hash; 5644 if (ftrace_hash_empty(hash)) 5645 return -EINVAL; 5646 5647 mutex_lock(&direct_mutex); 5648 5649 /* Make sure requested entries are not already registered.. */ 5650 size = 1 << hash->size_bits; 5651 for (i = 0; i < size; i++) { 5652 hlist_for_each_entry(entry, &hash->buckets[i], hlist) { 5653 if (ftrace_find_rec_direct(entry->ip)) 5654 goto out_unlock; 5655 } 5656 } 5657 5658 /* ... and insert them to direct_functions hash. */ 5659 err = -ENOMEM; 5660 for (i = 0; i < size; i++) { 5661 hlist_for_each_entry(entry, &hash->buckets[i], hlist) { 5662 new = ftrace_add_rec_direct(entry->ip, addr, &free_hash); 5663 if (!new) 5664 goto out_remove; 5665 entry->direct = addr; 5666 } 5667 } 5668 5669 ops->func = call_direct_funcs; 5670 ops->flags = MULTI_FLAGS; 5671 ops->trampoline = FTRACE_REGS_ADDR; 5672 5673 err = register_ftrace_function_nolock(ops); 5674 5675 out_remove: 5676 if (err) 5677 remove_direct_functions_hash(hash, addr); 5678 5679 out_unlock: 5680 mutex_unlock(&direct_mutex); 5681 5682 if (free_hash) { 5683 synchronize_rcu_tasks(); 5684 free_ftrace_hash(free_hash); 5685 } 5686 return err; 5687 } 5688 EXPORT_SYMBOL_GPL(register_ftrace_direct_multi); 5689 5690 /** 5691 * unregister_ftrace_direct_multi - Remove calls to custom trampoline 5692 * previously registered by register_ftrace_direct_multi for @ops object. 5693 * @ops: The address of the struct ftrace_ops object 5694 * 5695 * This is used to remove a direct calls to @addr from the nop locations 5696 * of the functions registered in @ops (with by ftrace_set_filter_ip 5697 * function). 5698 * 5699 * Returns: 5700 * 0 on success 5701 * -EINVAL - The @ops object was not properly registered. 5702 */ 5703 int unregister_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr) 5704 { 5705 struct ftrace_hash *hash = ops->func_hash->filter_hash; 5706 int err; 5707 5708 if (check_direct_multi(ops)) 5709 return -EINVAL; 5710 if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) 5711 return -EINVAL; 5712 5713 mutex_lock(&direct_mutex); 5714 err = unregister_ftrace_function(ops); 5715 remove_direct_functions_hash(hash, addr); 5716 mutex_unlock(&direct_mutex); 5717 5718 /* cleanup for possible another register call */ 5719 ops->func = NULL; 5720 ops->trampoline = 0; 5721 return err; 5722 } 5723 EXPORT_SYMBOL_GPL(unregister_ftrace_direct_multi); 5724 5725 static int 5726 __modify_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr) 5727 { 5728 struct ftrace_hash *hash; 5729 struct ftrace_func_entry *entry, *iter; 5730 static struct ftrace_ops tmp_ops = { 5731 .func = ftrace_stub, 5732 .flags = FTRACE_OPS_FL_STUB, 5733 }; 5734 int i, size; 5735 int err; 5736 5737 lockdep_assert_held_once(&direct_mutex); 5738 5739 /* Enable the tmp_ops to have the same functions as the direct ops */ 5740 ftrace_ops_init(&tmp_ops); 5741 tmp_ops.func_hash = ops->func_hash; 5742 5743 err = register_ftrace_function_nolock(&tmp_ops); 5744 if (err) 5745 return err; 5746 5747 /* 5748 * Now the ftrace_ops_list_func() is called to do the direct callers. 5749 * We can safely change the direct functions attached to each entry. 5750 */ 5751 mutex_lock(&ftrace_lock); 5752 5753 hash = ops->func_hash->filter_hash; 5754 size = 1 << hash->size_bits; 5755 for (i = 0; i < size; i++) { 5756 hlist_for_each_entry(iter, &hash->buckets[i], hlist) { 5757 entry = __ftrace_lookup_ip(direct_functions, iter->ip); 5758 if (!entry) 5759 continue; 5760 entry->direct = addr; 5761 } 5762 } 5763 5764 mutex_unlock(&ftrace_lock); 5765 5766 /* Removing the tmp_ops will add the updated direct callers to the functions */ 5767 unregister_ftrace_function(&tmp_ops); 5768 5769 return err; 5770 } 5771 5772 /** 5773 * modify_ftrace_direct_multi_nolock - Modify an existing direct 'multi' call 5774 * to call something else 5775 * @ops: The address of the struct ftrace_ops object 5776 * @addr: The address of the new trampoline to call at @ops functions 5777 * 5778 * This is used to unregister currently registered direct caller and 5779 * register new one @addr on functions registered in @ops object. 5780 * 5781 * Note there's window between ftrace_shutdown and ftrace_startup calls 5782 * where there will be no callbacks called. 5783 * 5784 * Caller should already have direct_mutex locked, so we don't lock 5785 * direct_mutex here. 5786 * 5787 * Returns: zero on success. Non zero on error, which includes: 5788 * -EINVAL - The @ops object was not properly registered. 5789 */ 5790 int modify_ftrace_direct_multi_nolock(struct ftrace_ops *ops, unsigned long addr) 5791 { 5792 if (check_direct_multi(ops)) 5793 return -EINVAL; 5794 if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) 5795 return -EINVAL; 5796 5797 return __modify_ftrace_direct_multi(ops, addr); 5798 } 5799 EXPORT_SYMBOL_GPL(modify_ftrace_direct_multi_nolock); 5800 5801 /** 5802 * modify_ftrace_direct_multi - Modify an existing direct 'multi' call 5803 * to call something else 5804 * @ops: The address of the struct ftrace_ops object 5805 * @addr: The address of the new trampoline to call at @ops functions 5806 * 5807 * This is used to unregister currently registered direct caller and 5808 * register new one @addr on functions registered in @ops object. 5809 * 5810 * Note there's window between ftrace_shutdown and ftrace_startup calls 5811 * where there will be no callbacks called. 5812 * 5813 * Returns: zero on success. Non zero on error, which includes: 5814 * -EINVAL - The @ops object was not properly registered. 5815 */ 5816 int modify_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr) 5817 { 5818 int err; 5819 5820 if (check_direct_multi(ops)) 5821 return -EINVAL; 5822 if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) 5823 return -EINVAL; 5824 5825 mutex_lock(&direct_mutex); 5826 err = __modify_ftrace_direct_multi(ops, addr); 5827 mutex_unlock(&direct_mutex); 5828 return err; 5829 } 5830 EXPORT_SYMBOL_GPL(modify_ftrace_direct_multi); 5831 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */ 5832 5833 /** 5834 * ftrace_set_filter_ip - set a function to filter on in ftrace by address 5835 * @ops - the ops to set the filter with 5836 * @ip - the address to add to or remove from the filter. 5837 * @remove - non zero to remove the ip from the filter 5838 * @reset - non zero to reset all filters before applying this filter. 5839 * 5840 * Filters denote which functions should be enabled when tracing is enabled 5841 * If @ip is NULL, it fails to update filter. 5842 */ 5843 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip, 5844 int remove, int reset) 5845 { 5846 ftrace_ops_init(ops); 5847 return ftrace_set_addr(ops, &ip, 1, remove, reset, 1); 5848 } 5849 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip); 5850 5851 /** 5852 * ftrace_set_filter_ips - set functions to filter on in ftrace by addresses 5853 * @ops - the ops to set the filter with 5854 * @ips - the array of addresses to add to or remove from the filter. 5855 * @cnt - the number of addresses in @ips 5856 * @remove - non zero to remove ips from the filter 5857 * @reset - non zero to reset all filters before applying this filter. 5858 * 5859 * Filters denote which functions should be enabled when tracing is enabled 5860 * If @ips array or any ip specified within is NULL , it fails to update filter. 5861 */ 5862 int ftrace_set_filter_ips(struct ftrace_ops *ops, unsigned long *ips, 5863 unsigned int cnt, int remove, int reset) 5864 { 5865 ftrace_ops_init(ops); 5866 return ftrace_set_addr(ops, ips, cnt, remove, reset, 1); 5867 } 5868 EXPORT_SYMBOL_GPL(ftrace_set_filter_ips); 5869 5870 /** 5871 * ftrace_ops_set_global_filter - setup ops to use global filters 5872 * @ops - the ops which will use the global filters 5873 * 5874 * ftrace users who need global function trace filtering should call this. 5875 * It can set the global filter only if ops were not initialized before. 5876 */ 5877 void ftrace_ops_set_global_filter(struct ftrace_ops *ops) 5878 { 5879 if (ops->flags & FTRACE_OPS_FL_INITIALIZED) 5880 return; 5881 5882 ftrace_ops_init(ops); 5883 ops->func_hash = &global_ops.local_hash; 5884 } 5885 EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter); 5886 5887 static int 5888 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len, 5889 int reset, int enable) 5890 { 5891 return ftrace_set_hash(ops, buf, len, NULL, 0, 0, reset, enable); 5892 } 5893 5894 /** 5895 * ftrace_set_filter - set a function to filter on in ftrace 5896 * @ops - the ops to set the filter with 5897 * @buf - the string that holds the function filter text. 5898 * @len - the length of the string. 5899 * @reset - non zero to reset all filters before applying this filter. 5900 * 5901 * Filters denote which functions should be enabled when tracing is enabled. 5902 * If @buf is NULL and reset is set, all functions will be enabled for tracing. 5903 */ 5904 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf, 5905 int len, int reset) 5906 { 5907 ftrace_ops_init(ops); 5908 return ftrace_set_regex(ops, buf, len, reset, 1); 5909 } 5910 EXPORT_SYMBOL_GPL(ftrace_set_filter); 5911 5912 /** 5913 * ftrace_set_notrace - set a function to not trace in ftrace 5914 * @ops - the ops to set the notrace filter with 5915 * @buf - the string that holds the function notrace text. 5916 * @len - the length of the string. 5917 * @reset - non zero to reset all filters before applying this filter. 5918 * 5919 * Notrace Filters denote which functions should not be enabled when tracing 5920 * is enabled. If @buf is NULL and reset is set, all functions will be enabled 5921 * for tracing. 5922 */ 5923 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf, 5924 int len, int reset) 5925 { 5926 ftrace_ops_init(ops); 5927 return ftrace_set_regex(ops, buf, len, reset, 0); 5928 } 5929 EXPORT_SYMBOL_GPL(ftrace_set_notrace); 5930 /** 5931 * ftrace_set_global_filter - set a function to filter on with global tracers 5932 * @buf - the string that holds the function filter text. 5933 * @len - the length of the string. 5934 * @reset - non zero to reset all filters before applying this filter. 5935 * 5936 * Filters denote which functions should be enabled when tracing is enabled. 5937 * If @buf is NULL and reset is set, all functions will be enabled for tracing. 5938 */ 5939 void ftrace_set_global_filter(unsigned char *buf, int len, int reset) 5940 { 5941 ftrace_set_regex(&global_ops, buf, len, reset, 1); 5942 } 5943 EXPORT_SYMBOL_GPL(ftrace_set_global_filter); 5944 5945 /** 5946 * ftrace_set_global_notrace - set a function to not trace with global tracers 5947 * @buf - the string that holds the function notrace text. 5948 * @len - the length of the string. 5949 * @reset - non zero to reset all filters before applying this filter. 5950 * 5951 * Notrace Filters denote which functions should not be enabled when tracing 5952 * is enabled. If @buf is NULL and reset is set, all functions will be enabled 5953 * for tracing. 5954 */ 5955 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset) 5956 { 5957 ftrace_set_regex(&global_ops, buf, len, reset, 0); 5958 } 5959 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace); 5960 5961 /* 5962 * command line interface to allow users to set filters on boot up. 5963 */ 5964 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE 5965 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata; 5966 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata; 5967 5968 /* Used by function selftest to not test if filter is set */ 5969 bool ftrace_filter_param __initdata; 5970 5971 static int __init set_ftrace_notrace(char *str) 5972 { 5973 ftrace_filter_param = true; 5974 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE); 5975 return 1; 5976 } 5977 __setup("ftrace_notrace=", set_ftrace_notrace); 5978 5979 static int __init set_ftrace_filter(char *str) 5980 { 5981 ftrace_filter_param = true; 5982 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE); 5983 return 1; 5984 } 5985 __setup("ftrace_filter=", set_ftrace_filter); 5986 5987 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 5988 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata; 5989 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata; 5990 static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer); 5991 5992 static int __init set_graph_function(char *str) 5993 { 5994 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE); 5995 return 1; 5996 } 5997 __setup("ftrace_graph_filter=", set_graph_function); 5998 5999 static int __init set_graph_notrace_function(char *str) 6000 { 6001 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE); 6002 return 1; 6003 } 6004 __setup("ftrace_graph_notrace=", set_graph_notrace_function); 6005 6006 static int __init set_graph_max_depth_function(char *str) 6007 { 6008 if (!str) 6009 return 0; 6010 fgraph_max_depth = simple_strtoul(str, NULL, 0); 6011 return 1; 6012 } 6013 __setup("ftrace_graph_max_depth=", set_graph_max_depth_function); 6014 6015 static void __init set_ftrace_early_graph(char *buf, int enable) 6016 { 6017 int ret; 6018 char *func; 6019 struct ftrace_hash *hash; 6020 6021 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS); 6022 if (MEM_FAIL(!hash, "Failed to allocate hash\n")) 6023 return; 6024 6025 while (buf) { 6026 func = strsep(&buf, ","); 6027 /* we allow only one expression at a time */ 6028 ret = ftrace_graph_set_hash(hash, func); 6029 if (ret) 6030 printk(KERN_DEBUG "ftrace: function %s not " 6031 "traceable\n", func); 6032 } 6033 6034 if (enable) 6035 ftrace_graph_hash = hash; 6036 else 6037 ftrace_graph_notrace_hash = hash; 6038 } 6039 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ 6040 6041 void __init 6042 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable) 6043 { 6044 char *func; 6045 6046 ftrace_ops_init(ops); 6047 6048 while (buf) { 6049 func = strsep(&buf, ","); 6050 ftrace_set_regex(ops, func, strlen(func), 0, enable); 6051 } 6052 } 6053 6054 static void __init set_ftrace_early_filters(void) 6055 { 6056 if (ftrace_filter_buf[0]) 6057 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1); 6058 if (ftrace_notrace_buf[0]) 6059 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0); 6060 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 6061 if (ftrace_graph_buf[0]) 6062 set_ftrace_early_graph(ftrace_graph_buf, 1); 6063 if (ftrace_graph_notrace_buf[0]) 6064 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0); 6065 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ 6066 } 6067 6068 int ftrace_regex_release(struct inode *inode, struct file *file) 6069 { 6070 struct seq_file *m = (struct seq_file *)file->private_data; 6071 struct ftrace_iterator *iter; 6072 struct ftrace_hash **orig_hash; 6073 struct trace_parser *parser; 6074 int filter_hash; 6075 6076 if (file->f_mode & FMODE_READ) { 6077 iter = m->private; 6078 seq_release(inode, file); 6079 } else 6080 iter = file->private_data; 6081 6082 parser = &iter->parser; 6083 if (trace_parser_loaded(parser)) { 6084 int enable = !(iter->flags & FTRACE_ITER_NOTRACE); 6085 6086 ftrace_process_regex(iter, parser->buffer, 6087 parser->idx, enable); 6088 } 6089 6090 trace_parser_put(parser); 6091 6092 mutex_lock(&iter->ops->func_hash->regex_lock); 6093 6094 if (file->f_mode & FMODE_WRITE) { 6095 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER); 6096 6097 if (filter_hash) { 6098 orig_hash = &iter->ops->func_hash->filter_hash; 6099 if (iter->tr) { 6100 if (list_empty(&iter->tr->mod_trace)) 6101 iter->hash->flags &= ~FTRACE_HASH_FL_MOD; 6102 else 6103 iter->hash->flags |= FTRACE_HASH_FL_MOD; 6104 } 6105 } else 6106 orig_hash = &iter->ops->func_hash->notrace_hash; 6107 6108 mutex_lock(&ftrace_lock); 6109 ftrace_hash_move_and_update_ops(iter->ops, orig_hash, 6110 iter->hash, filter_hash); 6111 mutex_unlock(&ftrace_lock); 6112 } else { 6113 /* For read only, the hash is the ops hash */ 6114 iter->hash = NULL; 6115 } 6116 6117 mutex_unlock(&iter->ops->func_hash->regex_lock); 6118 free_ftrace_hash(iter->hash); 6119 if (iter->tr) 6120 trace_array_put(iter->tr); 6121 kfree(iter); 6122 6123 return 0; 6124 } 6125 6126 static const struct file_operations ftrace_avail_fops = { 6127 .open = ftrace_avail_open, 6128 .read = seq_read, 6129 .llseek = seq_lseek, 6130 .release = seq_release_private, 6131 }; 6132 6133 static const struct file_operations ftrace_enabled_fops = { 6134 .open = ftrace_enabled_open, 6135 .read = seq_read, 6136 .llseek = seq_lseek, 6137 .release = seq_release_private, 6138 }; 6139 6140 static const struct file_operations ftrace_filter_fops = { 6141 .open = ftrace_filter_open, 6142 .read = seq_read, 6143 .write = ftrace_filter_write, 6144 .llseek = tracing_lseek, 6145 .release = ftrace_regex_release, 6146 }; 6147 6148 static const struct file_operations ftrace_notrace_fops = { 6149 .open = ftrace_notrace_open, 6150 .read = seq_read, 6151 .write = ftrace_notrace_write, 6152 .llseek = tracing_lseek, 6153 .release = ftrace_regex_release, 6154 }; 6155 6156 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 6157 6158 static DEFINE_MUTEX(graph_lock); 6159 6160 struct ftrace_hash __rcu *ftrace_graph_hash = EMPTY_HASH; 6161 struct ftrace_hash __rcu *ftrace_graph_notrace_hash = EMPTY_HASH; 6162 6163 enum graph_filter_type { 6164 GRAPH_FILTER_NOTRACE = 0, 6165 GRAPH_FILTER_FUNCTION, 6166 }; 6167 6168 #define FTRACE_GRAPH_EMPTY ((void *)1) 6169 6170 struct ftrace_graph_data { 6171 struct ftrace_hash *hash; 6172 struct ftrace_func_entry *entry; 6173 int idx; /* for hash table iteration */ 6174 enum graph_filter_type type; 6175 struct ftrace_hash *new_hash; 6176 const struct seq_operations *seq_ops; 6177 struct trace_parser parser; 6178 }; 6179 6180 static void * 6181 __g_next(struct seq_file *m, loff_t *pos) 6182 { 6183 struct ftrace_graph_data *fgd = m->private; 6184 struct ftrace_func_entry *entry = fgd->entry; 6185 struct hlist_head *head; 6186 int i, idx = fgd->idx; 6187 6188 if (*pos >= fgd->hash->count) 6189 return NULL; 6190 6191 if (entry) { 6192 hlist_for_each_entry_continue(entry, hlist) { 6193 fgd->entry = entry; 6194 return entry; 6195 } 6196 6197 idx++; 6198 } 6199 6200 for (i = idx; i < 1 << fgd->hash->size_bits; i++) { 6201 head = &fgd->hash->buckets[i]; 6202 hlist_for_each_entry(entry, head, hlist) { 6203 fgd->entry = entry; 6204 fgd->idx = i; 6205 return entry; 6206 } 6207 } 6208 return NULL; 6209 } 6210 6211 static void * 6212 g_next(struct seq_file *m, void *v, loff_t *pos) 6213 { 6214 (*pos)++; 6215 return __g_next(m, pos); 6216 } 6217 6218 static void *g_start(struct seq_file *m, loff_t *pos) 6219 { 6220 struct ftrace_graph_data *fgd = m->private; 6221 6222 mutex_lock(&graph_lock); 6223 6224 if (fgd->type == GRAPH_FILTER_FUNCTION) 6225 fgd->hash = rcu_dereference_protected(ftrace_graph_hash, 6226 lockdep_is_held(&graph_lock)); 6227 else 6228 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash, 6229 lockdep_is_held(&graph_lock)); 6230 6231 /* Nothing, tell g_show to print all functions are enabled */ 6232 if (ftrace_hash_empty(fgd->hash) && !*pos) 6233 return FTRACE_GRAPH_EMPTY; 6234 6235 fgd->idx = 0; 6236 fgd->entry = NULL; 6237 return __g_next(m, pos); 6238 } 6239 6240 static void g_stop(struct seq_file *m, void *p) 6241 { 6242 mutex_unlock(&graph_lock); 6243 } 6244 6245 static int g_show(struct seq_file *m, void *v) 6246 { 6247 struct ftrace_func_entry *entry = v; 6248 6249 if (!entry) 6250 return 0; 6251 6252 if (entry == FTRACE_GRAPH_EMPTY) { 6253 struct ftrace_graph_data *fgd = m->private; 6254 6255 if (fgd->type == GRAPH_FILTER_FUNCTION) 6256 seq_puts(m, "#### all functions enabled ####\n"); 6257 else 6258 seq_puts(m, "#### no functions disabled ####\n"); 6259 return 0; 6260 } 6261 6262 seq_printf(m, "%ps\n", (void *)entry->ip); 6263 6264 return 0; 6265 } 6266 6267 static const struct seq_operations ftrace_graph_seq_ops = { 6268 .start = g_start, 6269 .next = g_next, 6270 .stop = g_stop, 6271 .show = g_show, 6272 }; 6273 6274 static int 6275 __ftrace_graph_open(struct inode *inode, struct file *file, 6276 struct ftrace_graph_data *fgd) 6277 { 6278 int ret; 6279 struct ftrace_hash *new_hash = NULL; 6280 6281 ret = security_locked_down(LOCKDOWN_TRACEFS); 6282 if (ret) 6283 return ret; 6284 6285 if (file->f_mode & FMODE_WRITE) { 6286 const int size_bits = FTRACE_HASH_DEFAULT_BITS; 6287 6288 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX)) 6289 return -ENOMEM; 6290 6291 if (file->f_flags & O_TRUNC) 6292 new_hash = alloc_ftrace_hash(size_bits); 6293 else 6294 new_hash = alloc_and_copy_ftrace_hash(size_bits, 6295 fgd->hash); 6296 if (!new_hash) { 6297 ret = -ENOMEM; 6298 goto out; 6299 } 6300 } 6301 6302 if (file->f_mode & FMODE_READ) { 6303 ret = seq_open(file, &ftrace_graph_seq_ops); 6304 if (!ret) { 6305 struct seq_file *m = file->private_data; 6306 m->private = fgd; 6307 } else { 6308 /* Failed */ 6309 free_ftrace_hash(new_hash); 6310 new_hash = NULL; 6311 } 6312 } else 6313 file->private_data = fgd; 6314 6315 out: 6316 if (ret < 0 && file->f_mode & FMODE_WRITE) 6317 trace_parser_put(&fgd->parser); 6318 6319 fgd->new_hash = new_hash; 6320 6321 /* 6322 * All uses of fgd->hash must be taken with the graph_lock 6323 * held. The graph_lock is going to be released, so force 6324 * fgd->hash to be reinitialized when it is taken again. 6325 */ 6326 fgd->hash = NULL; 6327 6328 return ret; 6329 } 6330 6331 static int 6332 ftrace_graph_open(struct inode *inode, struct file *file) 6333 { 6334 struct ftrace_graph_data *fgd; 6335 int ret; 6336 6337 if (unlikely(ftrace_disabled)) 6338 return -ENODEV; 6339 6340 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL); 6341 if (fgd == NULL) 6342 return -ENOMEM; 6343 6344 mutex_lock(&graph_lock); 6345 6346 fgd->hash = rcu_dereference_protected(ftrace_graph_hash, 6347 lockdep_is_held(&graph_lock)); 6348 fgd->type = GRAPH_FILTER_FUNCTION; 6349 fgd->seq_ops = &ftrace_graph_seq_ops; 6350 6351 ret = __ftrace_graph_open(inode, file, fgd); 6352 if (ret < 0) 6353 kfree(fgd); 6354 6355 mutex_unlock(&graph_lock); 6356 return ret; 6357 } 6358 6359 static int 6360 ftrace_graph_notrace_open(struct inode *inode, struct file *file) 6361 { 6362 struct ftrace_graph_data *fgd; 6363 int ret; 6364 6365 if (unlikely(ftrace_disabled)) 6366 return -ENODEV; 6367 6368 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL); 6369 if (fgd == NULL) 6370 return -ENOMEM; 6371 6372 mutex_lock(&graph_lock); 6373 6374 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash, 6375 lockdep_is_held(&graph_lock)); 6376 fgd->type = GRAPH_FILTER_NOTRACE; 6377 fgd->seq_ops = &ftrace_graph_seq_ops; 6378 6379 ret = __ftrace_graph_open(inode, file, fgd); 6380 if (ret < 0) 6381 kfree(fgd); 6382 6383 mutex_unlock(&graph_lock); 6384 return ret; 6385 } 6386 6387 static int 6388 ftrace_graph_release(struct inode *inode, struct file *file) 6389 { 6390 struct ftrace_graph_data *fgd; 6391 struct ftrace_hash *old_hash, *new_hash; 6392 struct trace_parser *parser; 6393 int ret = 0; 6394 6395 if (file->f_mode & FMODE_READ) { 6396 struct seq_file *m = file->private_data; 6397 6398 fgd = m->private; 6399 seq_release(inode, file); 6400 } else { 6401 fgd = file->private_data; 6402 } 6403 6404 6405 if (file->f_mode & FMODE_WRITE) { 6406 6407 parser = &fgd->parser; 6408 6409 if (trace_parser_loaded((parser))) { 6410 ret = ftrace_graph_set_hash(fgd->new_hash, 6411 parser->buffer); 6412 } 6413 6414 trace_parser_put(parser); 6415 6416 new_hash = __ftrace_hash_move(fgd->new_hash); 6417 if (!new_hash) { 6418 ret = -ENOMEM; 6419 goto out; 6420 } 6421 6422 mutex_lock(&graph_lock); 6423 6424 if (fgd->type == GRAPH_FILTER_FUNCTION) { 6425 old_hash = rcu_dereference_protected(ftrace_graph_hash, 6426 lockdep_is_held(&graph_lock)); 6427 rcu_assign_pointer(ftrace_graph_hash, new_hash); 6428 } else { 6429 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash, 6430 lockdep_is_held(&graph_lock)); 6431 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash); 6432 } 6433 6434 mutex_unlock(&graph_lock); 6435 6436 /* 6437 * We need to do a hard force of sched synchronization. 6438 * This is because we use preempt_disable() to do RCU, but 6439 * the function tracers can be called where RCU is not watching 6440 * (like before user_exit()). We can not rely on the RCU 6441 * infrastructure to do the synchronization, thus we must do it 6442 * ourselves. 6443 */ 6444 if (old_hash != EMPTY_HASH) 6445 synchronize_rcu_tasks_rude(); 6446 6447 free_ftrace_hash(old_hash); 6448 } 6449 6450 out: 6451 free_ftrace_hash(fgd->new_hash); 6452 kfree(fgd); 6453 6454 return ret; 6455 } 6456 6457 static int 6458 ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer) 6459 { 6460 struct ftrace_glob func_g; 6461 struct dyn_ftrace *rec; 6462 struct ftrace_page *pg; 6463 struct ftrace_func_entry *entry; 6464 int fail = 1; 6465 int not; 6466 6467 /* decode regex */ 6468 func_g.type = filter_parse_regex(buffer, strlen(buffer), 6469 &func_g.search, ¬); 6470 6471 func_g.len = strlen(func_g.search); 6472 6473 mutex_lock(&ftrace_lock); 6474 6475 if (unlikely(ftrace_disabled)) { 6476 mutex_unlock(&ftrace_lock); 6477 return -ENODEV; 6478 } 6479 6480 do_for_each_ftrace_rec(pg, rec) { 6481 6482 if (rec->flags & FTRACE_FL_DISABLED) 6483 continue; 6484 6485 if (ftrace_match_record(rec, &func_g, NULL, 0)) { 6486 entry = ftrace_lookup_ip(hash, rec->ip); 6487 6488 if (!not) { 6489 fail = 0; 6490 6491 if (entry) 6492 continue; 6493 if (add_hash_entry(hash, rec->ip) < 0) 6494 goto out; 6495 } else { 6496 if (entry) { 6497 free_hash_entry(hash, entry); 6498 fail = 0; 6499 } 6500 } 6501 } 6502 } while_for_each_ftrace_rec(); 6503 out: 6504 mutex_unlock(&ftrace_lock); 6505 6506 if (fail) 6507 return -EINVAL; 6508 6509 return 0; 6510 } 6511 6512 static ssize_t 6513 ftrace_graph_write(struct file *file, const char __user *ubuf, 6514 size_t cnt, loff_t *ppos) 6515 { 6516 ssize_t read, ret = 0; 6517 struct ftrace_graph_data *fgd = file->private_data; 6518 struct trace_parser *parser; 6519 6520 if (!cnt) 6521 return 0; 6522 6523 /* Read mode uses seq functions */ 6524 if (file->f_mode & FMODE_READ) { 6525 struct seq_file *m = file->private_data; 6526 fgd = m->private; 6527 } 6528 6529 parser = &fgd->parser; 6530 6531 read = trace_get_user(parser, ubuf, cnt, ppos); 6532 6533 if (read >= 0 && trace_parser_loaded(parser) && 6534 !trace_parser_cont(parser)) { 6535 6536 ret = ftrace_graph_set_hash(fgd->new_hash, 6537 parser->buffer); 6538 trace_parser_clear(parser); 6539 } 6540 6541 if (!ret) 6542 ret = read; 6543 6544 return ret; 6545 } 6546 6547 static const struct file_operations ftrace_graph_fops = { 6548 .open = ftrace_graph_open, 6549 .read = seq_read, 6550 .write = ftrace_graph_write, 6551 .llseek = tracing_lseek, 6552 .release = ftrace_graph_release, 6553 }; 6554 6555 static const struct file_operations ftrace_graph_notrace_fops = { 6556 .open = ftrace_graph_notrace_open, 6557 .read = seq_read, 6558 .write = ftrace_graph_write, 6559 .llseek = tracing_lseek, 6560 .release = ftrace_graph_release, 6561 }; 6562 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ 6563 6564 void ftrace_create_filter_files(struct ftrace_ops *ops, 6565 struct dentry *parent) 6566 { 6567 6568 trace_create_file("set_ftrace_filter", TRACE_MODE_WRITE, parent, 6569 ops, &ftrace_filter_fops); 6570 6571 trace_create_file("set_ftrace_notrace", TRACE_MODE_WRITE, parent, 6572 ops, &ftrace_notrace_fops); 6573 } 6574 6575 /* 6576 * The name "destroy_filter_files" is really a misnomer. Although 6577 * in the future, it may actually delete the files, but this is 6578 * really intended to make sure the ops passed in are disabled 6579 * and that when this function returns, the caller is free to 6580 * free the ops. 6581 * 6582 * The "destroy" name is only to match the "create" name that this 6583 * should be paired with. 6584 */ 6585 void ftrace_destroy_filter_files(struct ftrace_ops *ops) 6586 { 6587 mutex_lock(&ftrace_lock); 6588 if (ops->flags & FTRACE_OPS_FL_ENABLED) 6589 ftrace_shutdown(ops, 0); 6590 ops->flags |= FTRACE_OPS_FL_DELETED; 6591 ftrace_free_filter(ops); 6592 mutex_unlock(&ftrace_lock); 6593 } 6594 6595 static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer) 6596 { 6597 6598 trace_create_file("available_filter_functions", TRACE_MODE_READ, 6599 d_tracer, NULL, &ftrace_avail_fops); 6600 6601 trace_create_file("enabled_functions", TRACE_MODE_READ, 6602 d_tracer, NULL, &ftrace_enabled_fops); 6603 6604 ftrace_create_filter_files(&global_ops, d_tracer); 6605 6606 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 6607 trace_create_file("set_graph_function", TRACE_MODE_WRITE, d_tracer, 6608 NULL, 6609 &ftrace_graph_fops); 6610 trace_create_file("set_graph_notrace", TRACE_MODE_WRITE, d_tracer, 6611 NULL, 6612 &ftrace_graph_notrace_fops); 6613 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ 6614 6615 return 0; 6616 } 6617 6618 static int ftrace_cmp_ips(const void *a, const void *b) 6619 { 6620 const unsigned long *ipa = a; 6621 const unsigned long *ipb = b; 6622 6623 if (*ipa > *ipb) 6624 return 1; 6625 if (*ipa < *ipb) 6626 return -1; 6627 return 0; 6628 } 6629 6630 #ifdef CONFIG_FTRACE_SORT_STARTUP_TEST 6631 static void test_is_sorted(unsigned long *start, unsigned long count) 6632 { 6633 int i; 6634 6635 for (i = 1; i < count; i++) { 6636 if (WARN(start[i - 1] > start[i], 6637 "[%d] %pS at %lx is not sorted with %pS at %lx\n", i, 6638 (void *)start[i - 1], start[i - 1], 6639 (void *)start[i], start[i])) 6640 break; 6641 } 6642 if (i == count) 6643 pr_info("ftrace section at %px sorted properly\n", start); 6644 } 6645 #else 6646 static void test_is_sorted(unsigned long *start, unsigned long count) 6647 { 6648 } 6649 #endif 6650 6651 static int ftrace_process_locs(struct module *mod, 6652 unsigned long *start, 6653 unsigned long *end) 6654 { 6655 struct ftrace_page *start_pg; 6656 struct ftrace_page *pg; 6657 struct dyn_ftrace *rec; 6658 unsigned long count; 6659 unsigned long *p; 6660 unsigned long addr; 6661 unsigned long flags = 0; /* Shut up gcc */ 6662 int ret = -ENOMEM; 6663 6664 count = end - start; 6665 6666 if (!count) 6667 return 0; 6668 6669 /* 6670 * Sorting mcount in vmlinux at build time depend on 6671 * CONFIG_BUILDTIME_MCOUNT_SORT, while mcount loc in 6672 * modules can not be sorted at build time. 6673 */ 6674 if (!IS_ENABLED(CONFIG_BUILDTIME_MCOUNT_SORT) || mod) { 6675 sort(start, count, sizeof(*start), 6676 ftrace_cmp_ips, NULL); 6677 } else { 6678 test_is_sorted(start, count); 6679 } 6680 6681 start_pg = ftrace_allocate_pages(count); 6682 if (!start_pg) 6683 return -ENOMEM; 6684 6685 mutex_lock(&ftrace_lock); 6686 6687 /* 6688 * Core and each module needs their own pages, as 6689 * modules will free them when they are removed. 6690 * Force a new page to be allocated for modules. 6691 */ 6692 if (!mod) { 6693 WARN_ON(ftrace_pages || ftrace_pages_start); 6694 /* First initialization */ 6695 ftrace_pages = ftrace_pages_start = start_pg; 6696 } else { 6697 if (!ftrace_pages) 6698 goto out; 6699 6700 if (WARN_ON(ftrace_pages->next)) { 6701 /* Hmm, we have free pages? */ 6702 while (ftrace_pages->next) 6703 ftrace_pages = ftrace_pages->next; 6704 } 6705 6706 ftrace_pages->next = start_pg; 6707 } 6708 6709 p = start; 6710 pg = start_pg; 6711 while (p < end) { 6712 unsigned long end_offset; 6713 addr = ftrace_call_adjust(*p++); 6714 /* 6715 * Some architecture linkers will pad between 6716 * the different mcount_loc sections of different 6717 * object files to satisfy alignments. 6718 * Skip any NULL pointers. 6719 */ 6720 if (!addr) 6721 continue; 6722 6723 end_offset = (pg->index+1) * sizeof(pg->records[0]); 6724 if (end_offset > PAGE_SIZE << pg->order) { 6725 /* We should have allocated enough */ 6726 if (WARN_ON(!pg->next)) 6727 break; 6728 pg = pg->next; 6729 } 6730 6731 rec = &pg->records[pg->index++]; 6732 rec->ip = addr; 6733 } 6734 6735 /* We should have used all pages */ 6736 WARN_ON(pg->next); 6737 6738 /* Assign the last page to ftrace_pages */ 6739 ftrace_pages = pg; 6740 6741 /* 6742 * We only need to disable interrupts on start up 6743 * because we are modifying code that an interrupt 6744 * may execute, and the modification is not atomic. 6745 * But for modules, nothing runs the code we modify 6746 * until we are finished with it, and there's no 6747 * reason to cause large interrupt latencies while we do it. 6748 */ 6749 if (!mod) 6750 local_irq_save(flags); 6751 ftrace_update_code(mod, start_pg); 6752 if (!mod) 6753 local_irq_restore(flags); 6754 ret = 0; 6755 out: 6756 mutex_unlock(&ftrace_lock); 6757 6758 return ret; 6759 } 6760 6761 struct ftrace_mod_func { 6762 struct list_head list; 6763 char *name; 6764 unsigned long ip; 6765 unsigned int size; 6766 }; 6767 6768 struct ftrace_mod_map { 6769 struct rcu_head rcu; 6770 struct list_head list; 6771 struct module *mod; 6772 unsigned long start_addr; 6773 unsigned long end_addr; 6774 struct list_head funcs; 6775 unsigned int num_funcs; 6776 }; 6777 6778 static int ftrace_get_trampoline_kallsym(unsigned int symnum, 6779 unsigned long *value, char *type, 6780 char *name, char *module_name, 6781 int *exported) 6782 { 6783 struct ftrace_ops *op; 6784 6785 list_for_each_entry_rcu(op, &ftrace_ops_trampoline_list, list) { 6786 if (!op->trampoline || symnum--) 6787 continue; 6788 *value = op->trampoline; 6789 *type = 't'; 6790 strlcpy(name, FTRACE_TRAMPOLINE_SYM, KSYM_NAME_LEN); 6791 strlcpy(module_name, FTRACE_TRAMPOLINE_MOD, MODULE_NAME_LEN); 6792 *exported = 0; 6793 return 0; 6794 } 6795 6796 return -ERANGE; 6797 } 6798 6799 #if defined(CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS) || defined(CONFIG_MODULES) 6800 /* 6801 * Check if the current ops references the given ip. 6802 * 6803 * If the ops traces all functions, then it was already accounted for. 6804 * If the ops does not trace the current record function, skip it. 6805 * If the ops ignores the function via notrace filter, skip it. 6806 */ 6807 static bool 6808 ops_references_ip(struct ftrace_ops *ops, unsigned long ip) 6809 { 6810 /* If ops isn't enabled, ignore it */ 6811 if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) 6812 return false; 6813 6814 /* If ops traces all then it includes this function */ 6815 if (ops_traces_mod(ops)) 6816 return true; 6817 6818 /* The function must be in the filter */ 6819 if (!ftrace_hash_empty(ops->func_hash->filter_hash) && 6820 !__ftrace_lookup_ip(ops->func_hash->filter_hash, ip)) 6821 return false; 6822 6823 /* If in notrace hash, we ignore it too */ 6824 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, ip)) 6825 return false; 6826 6827 return true; 6828 } 6829 #endif 6830 6831 #ifdef CONFIG_MODULES 6832 6833 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next) 6834 6835 static LIST_HEAD(ftrace_mod_maps); 6836 6837 static int referenced_filters(struct dyn_ftrace *rec) 6838 { 6839 struct ftrace_ops *ops; 6840 int cnt = 0; 6841 6842 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) { 6843 if (ops_references_ip(ops, rec->ip)) { 6844 if (WARN_ON_ONCE(ops->flags & FTRACE_OPS_FL_DIRECT)) 6845 continue; 6846 if (WARN_ON_ONCE(ops->flags & FTRACE_OPS_FL_IPMODIFY)) 6847 continue; 6848 cnt++; 6849 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) 6850 rec->flags |= FTRACE_FL_REGS; 6851 if (cnt == 1 && ops->trampoline) 6852 rec->flags |= FTRACE_FL_TRAMP; 6853 else 6854 rec->flags &= ~FTRACE_FL_TRAMP; 6855 } 6856 } 6857 6858 return cnt; 6859 } 6860 6861 static void 6862 clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash) 6863 { 6864 struct ftrace_func_entry *entry; 6865 struct dyn_ftrace *rec; 6866 int i; 6867 6868 if (ftrace_hash_empty(hash)) 6869 return; 6870 6871 for (i = 0; i < pg->index; i++) { 6872 rec = &pg->records[i]; 6873 entry = __ftrace_lookup_ip(hash, rec->ip); 6874 /* 6875 * Do not allow this rec to match again. 6876 * Yeah, it may waste some memory, but will be removed 6877 * if/when the hash is modified again. 6878 */ 6879 if (entry) 6880 entry->ip = 0; 6881 } 6882 } 6883 6884 /* Clear any records from hashes */ 6885 static void clear_mod_from_hashes(struct ftrace_page *pg) 6886 { 6887 struct trace_array *tr; 6888 6889 mutex_lock(&trace_types_lock); 6890 list_for_each_entry(tr, &ftrace_trace_arrays, list) { 6891 if (!tr->ops || !tr->ops->func_hash) 6892 continue; 6893 mutex_lock(&tr->ops->func_hash->regex_lock); 6894 clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash); 6895 clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash); 6896 mutex_unlock(&tr->ops->func_hash->regex_lock); 6897 } 6898 mutex_unlock(&trace_types_lock); 6899 } 6900 6901 static void ftrace_free_mod_map(struct rcu_head *rcu) 6902 { 6903 struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu); 6904 struct ftrace_mod_func *mod_func; 6905 struct ftrace_mod_func *n; 6906 6907 /* All the contents of mod_map are now not visible to readers */ 6908 list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) { 6909 kfree(mod_func->name); 6910 list_del(&mod_func->list); 6911 kfree(mod_func); 6912 } 6913 6914 kfree(mod_map); 6915 } 6916 6917 void ftrace_release_mod(struct module *mod) 6918 { 6919 struct ftrace_mod_map *mod_map; 6920 struct ftrace_mod_map *n; 6921 struct dyn_ftrace *rec; 6922 struct ftrace_page **last_pg; 6923 struct ftrace_page *tmp_page = NULL; 6924 struct ftrace_page *pg; 6925 6926 mutex_lock(&ftrace_lock); 6927 6928 if (ftrace_disabled) 6929 goto out_unlock; 6930 6931 list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) { 6932 if (mod_map->mod == mod) { 6933 list_del_rcu(&mod_map->list); 6934 call_rcu(&mod_map->rcu, ftrace_free_mod_map); 6935 break; 6936 } 6937 } 6938 6939 /* 6940 * Each module has its own ftrace_pages, remove 6941 * them from the list. 6942 */ 6943 last_pg = &ftrace_pages_start; 6944 for (pg = ftrace_pages_start; pg; pg = *last_pg) { 6945 rec = &pg->records[0]; 6946 if (within_module_core(rec->ip, mod) || 6947 within_module_init(rec->ip, mod)) { 6948 /* 6949 * As core pages are first, the first 6950 * page should never be a module page. 6951 */ 6952 if (WARN_ON(pg == ftrace_pages_start)) 6953 goto out_unlock; 6954 6955 /* Check if we are deleting the last page */ 6956 if (pg == ftrace_pages) 6957 ftrace_pages = next_to_ftrace_page(last_pg); 6958 6959 ftrace_update_tot_cnt -= pg->index; 6960 *last_pg = pg->next; 6961 6962 pg->next = tmp_page; 6963 tmp_page = pg; 6964 } else 6965 last_pg = &pg->next; 6966 } 6967 out_unlock: 6968 mutex_unlock(&ftrace_lock); 6969 6970 for (pg = tmp_page; pg; pg = tmp_page) { 6971 6972 /* Needs to be called outside of ftrace_lock */ 6973 clear_mod_from_hashes(pg); 6974 6975 if (pg->records) { 6976 free_pages((unsigned long)pg->records, pg->order); 6977 ftrace_number_of_pages -= 1 << pg->order; 6978 } 6979 tmp_page = pg->next; 6980 kfree(pg); 6981 ftrace_number_of_groups--; 6982 } 6983 } 6984 6985 void ftrace_module_enable(struct module *mod) 6986 { 6987 struct dyn_ftrace *rec; 6988 struct ftrace_page *pg; 6989 6990 mutex_lock(&ftrace_lock); 6991 6992 if (ftrace_disabled) 6993 goto out_unlock; 6994 6995 /* 6996 * If the tracing is enabled, go ahead and enable the record. 6997 * 6998 * The reason not to enable the record immediately is the 6999 * inherent check of ftrace_make_nop/ftrace_make_call for 7000 * correct previous instructions. Making first the NOP 7001 * conversion puts the module to the correct state, thus 7002 * passing the ftrace_make_call check. 7003 * 7004 * We also delay this to after the module code already set the 7005 * text to read-only, as we now need to set it back to read-write 7006 * so that we can modify the text. 7007 */ 7008 if (ftrace_start_up) 7009 ftrace_arch_code_modify_prepare(); 7010 7011 do_for_each_ftrace_rec(pg, rec) { 7012 int cnt; 7013 /* 7014 * do_for_each_ftrace_rec() is a double loop. 7015 * module text shares the pg. If a record is 7016 * not part of this module, then skip this pg, 7017 * which the "break" will do. 7018 */ 7019 if (!within_module_core(rec->ip, mod) && 7020 !within_module_init(rec->ip, mod)) 7021 break; 7022 7023 /* Weak functions should still be ignored */ 7024 if (!test_for_valid_rec(rec)) { 7025 /* Clear all other flags. Should not be enabled anyway */ 7026 rec->flags = FTRACE_FL_DISABLED; 7027 continue; 7028 } 7029 7030 cnt = 0; 7031 7032 /* 7033 * When adding a module, we need to check if tracers are 7034 * currently enabled and if they are, and can trace this record, 7035 * we need to enable the module functions as well as update the 7036 * reference counts for those function records. 7037 */ 7038 if (ftrace_start_up) 7039 cnt += referenced_filters(rec); 7040 7041 rec->flags &= ~FTRACE_FL_DISABLED; 7042 rec->flags += cnt; 7043 7044 if (ftrace_start_up && cnt) { 7045 int failed = __ftrace_replace_code(rec, 1); 7046 if (failed) { 7047 ftrace_bug(failed, rec); 7048 goto out_loop; 7049 } 7050 } 7051 7052 } while_for_each_ftrace_rec(); 7053 7054 out_loop: 7055 if (ftrace_start_up) 7056 ftrace_arch_code_modify_post_process(); 7057 7058 out_unlock: 7059 mutex_unlock(&ftrace_lock); 7060 7061 process_cached_mods(mod->name); 7062 } 7063 7064 void ftrace_module_init(struct module *mod) 7065 { 7066 int ret; 7067 7068 if (ftrace_disabled || !mod->num_ftrace_callsites) 7069 return; 7070 7071 ret = ftrace_process_locs(mod, mod->ftrace_callsites, 7072 mod->ftrace_callsites + mod->num_ftrace_callsites); 7073 if (ret) 7074 pr_warn("ftrace: failed to allocate entries for module '%s' functions\n", 7075 mod->name); 7076 } 7077 7078 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map, 7079 struct dyn_ftrace *rec) 7080 { 7081 struct ftrace_mod_func *mod_func; 7082 unsigned long symsize; 7083 unsigned long offset; 7084 char str[KSYM_SYMBOL_LEN]; 7085 char *modname; 7086 const char *ret; 7087 7088 ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str); 7089 if (!ret) 7090 return; 7091 7092 mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL); 7093 if (!mod_func) 7094 return; 7095 7096 mod_func->name = kstrdup(str, GFP_KERNEL); 7097 if (!mod_func->name) { 7098 kfree(mod_func); 7099 return; 7100 } 7101 7102 mod_func->ip = rec->ip - offset; 7103 mod_func->size = symsize; 7104 7105 mod_map->num_funcs++; 7106 7107 list_add_rcu(&mod_func->list, &mod_map->funcs); 7108 } 7109 7110 static struct ftrace_mod_map * 7111 allocate_ftrace_mod_map(struct module *mod, 7112 unsigned long start, unsigned long end) 7113 { 7114 struct ftrace_mod_map *mod_map; 7115 7116 mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL); 7117 if (!mod_map) 7118 return NULL; 7119 7120 mod_map->mod = mod; 7121 mod_map->start_addr = start; 7122 mod_map->end_addr = end; 7123 mod_map->num_funcs = 0; 7124 7125 INIT_LIST_HEAD_RCU(&mod_map->funcs); 7126 7127 list_add_rcu(&mod_map->list, &ftrace_mod_maps); 7128 7129 return mod_map; 7130 } 7131 7132 static const char * 7133 ftrace_func_address_lookup(struct ftrace_mod_map *mod_map, 7134 unsigned long addr, unsigned long *size, 7135 unsigned long *off, char *sym) 7136 { 7137 struct ftrace_mod_func *found_func = NULL; 7138 struct ftrace_mod_func *mod_func; 7139 7140 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) { 7141 if (addr >= mod_func->ip && 7142 addr < mod_func->ip + mod_func->size) { 7143 found_func = mod_func; 7144 break; 7145 } 7146 } 7147 7148 if (found_func) { 7149 if (size) 7150 *size = found_func->size; 7151 if (off) 7152 *off = addr - found_func->ip; 7153 if (sym) 7154 strlcpy(sym, found_func->name, KSYM_NAME_LEN); 7155 7156 return found_func->name; 7157 } 7158 7159 return NULL; 7160 } 7161 7162 const char * 7163 ftrace_mod_address_lookup(unsigned long addr, unsigned long *size, 7164 unsigned long *off, char **modname, char *sym) 7165 { 7166 struct ftrace_mod_map *mod_map; 7167 const char *ret = NULL; 7168 7169 /* mod_map is freed via call_rcu() */ 7170 preempt_disable(); 7171 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) { 7172 ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym); 7173 if (ret) { 7174 if (modname) 7175 *modname = mod_map->mod->name; 7176 break; 7177 } 7178 } 7179 preempt_enable(); 7180 7181 return ret; 7182 } 7183 7184 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value, 7185 char *type, char *name, 7186 char *module_name, int *exported) 7187 { 7188 struct ftrace_mod_map *mod_map; 7189 struct ftrace_mod_func *mod_func; 7190 int ret; 7191 7192 preempt_disable(); 7193 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) { 7194 7195 if (symnum >= mod_map->num_funcs) { 7196 symnum -= mod_map->num_funcs; 7197 continue; 7198 } 7199 7200 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) { 7201 if (symnum > 1) { 7202 symnum--; 7203 continue; 7204 } 7205 7206 *value = mod_func->ip; 7207 *type = 'T'; 7208 strlcpy(name, mod_func->name, KSYM_NAME_LEN); 7209 strlcpy(module_name, mod_map->mod->name, MODULE_NAME_LEN); 7210 *exported = 1; 7211 preempt_enable(); 7212 return 0; 7213 } 7214 WARN_ON(1); 7215 break; 7216 } 7217 ret = ftrace_get_trampoline_kallsym(symnum, value, type, name, 7218 module_name, exported); 7219 preempt_enable(); 7220 return ret; 7221 } 7222 7223 #else 7224 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map, 7225 struct dyn_ftrace *rec) { } 7226 static inline struct ftrace_mod_map * 7227 allocate_ftrace_mod_map(struct module *mod, 7228 unsigned long start, unsigned long end) 7229 { 7230 return NULL; 7231 } 7232 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value, 7233 char *type, char *name, char *module_name, 7234 int *exported) 7235 { 7236 int ret; 7237 7238 preempt_disable(); 7239 ret = ftrace_get_trampoline_kallsym(symnum, value, type, name, 7240 module_name, exported); 7241 preempt_enable(); 7242 return ret; 7243 } 7244 #endif /* CONFIG_MODULES */ 7245 7246 struct ftrace_init_func { 7247 struct list_head list; 7248 unsigned long ip; 7249 }; 7250 7251 /* Clear any init ips from hashes */ 7252 static void 7253 clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash) 7254 { 7255 struct ftrace_func_entry *entry; 7256 7257 entry = ftrace_lookup_ip(hash, func->ip); 7258 /* 7259 * Do not allow this rec to match again. 7260 * Yeah, it may waste some memory, but will be removed 7261 * if/when the hash is modified again. 7262 */ 7263 if (entry) 7264 entry->ip = 0; 7265 } 7266 7267 static void 7268 clear_func_from_hashes(struct ftrace_init_func *func) 7269 { 7270 struct trace_array *tr; 7271 7272 mutex_lock(&trace_types_lock); 7273 list_for_each_entry(tr, &ftrace_trace_arrays, list) { 7274 if (!tr->ops || !tr->ops->func_hash) 7275 continue; 7276 mutex_lock(&tr->ops->func_hash->regex_lock); 7277 clear_func_from_hash(func, tr->ops->func_hash->filter_hash); 7278 clear_func_from_hash(func, tr->ops->func_hash->notrace_hash); 7279 mutex_unlock(&tr->ops->func_hash->regex_lock); 7280 } 7281 mutex_unlock(&trace_types_lock); 7282 } 7283 7284 static void add_to_clear_hash_list(struct list_head *clear_list, 7285 struct dyn_ftrace *rec) 7286 { 7287 struct ftrace_init_func *func; 7288 7289 func = kmalloc(sizeof(*func), GFP_KERNEL); 7290 if (!func) { 7291 MEM_FAIL(1, "alloc failure, ftrace filter could be stale\n"); 7292 return; 7293 } 7294 7295 func->ip = rec->ip; 7296 list_add(&func->list, clear_list); 7297 } 7298 7299 void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr) 7300 { 7301 unsigned long start = (unsigned long)(start_ptr); 7302 unsigned long end = (unsigned long)(end_ptr); 7303 struct ftrace_page **last_pg = &ftrace_pages_start; 7304 struct ftrace_page *pg; 7305 struct dyn_ftrace *rec; 7306 struct dyn_ftrace key; 7307 struct ftrace_mod_map *mod_map = NULL; 7308 struct ftrace_init_func *func, *func_next; 7309 struct list_head clear_hash; 7310 7311 INIT_LIST_HEAD(&clear_hash); 7312 7313 key.ip = start; 7314 key.flags = end; /* overload flags, as it is unsigned long */ 7315 7316 mutex_lock(&ftrace_lock); 7317 7318 /* 7319 * If we are freeing module init memory, then check if 7320 * any tracer is active. If so, we need to save a mapping of 7321 * the module functions being freed with the address. 7322 */ 7323 if (mod && ftrace_ops_list != &ftrace_list_end) 7324 mod_map = allocate_ftrace_mod_map(mod, start, end); 7325 7326 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) { 7327 if (end < pg->records[0].ip || 7328 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE)) 7329 continue; 7330 again: 7331 rec = bsearch(&key, pg->records, pg->index, 7332 sizeof(struct dyn_ftrace), 7333 ftrace_cmp_recs); 7334 if (!rec) 7335 continue; 7336 7337 /* rec will be cleared from hashes after ftrace_lock unlock */ 7338 add_to_clear_hash_list(&clear_hash, rec); 7339 7340 if (mod_map) 7341 save_ftrace_mod_rec(mod_map, rec); 7342 7343 pg->index--; 7344 ftrace_update_tot_cnt--; 7345 if (!pg->index) { 7346 *last_pg = pg->next; 7347 if (pg->records) { 7348 free_pages((unsigned long)pg->records, pg->order); 7349 ftrace_number_of_pages -= 1 << pg->order; 7350 } 7351 ftrace_number_of_groups--; 7352 kfree(pg); 7353 pg = container_of(last_pg, struct ftrace_page, next); 7354 if (!(*last_pg)) 7355 ftrace_pages = pg; 7356 continue; 7357 } 7358 memmove(rec, rec + 1, 7359 (pg->index - (rec - pg->records)) * sizeof(*rec)); 7360 /* More than one function may be in this block */ 7361 goto again; 7362 } 7363 mutex_unlock(&ftrace_lock); 7364 7365 list_for_each_entry_safe(func, func_next, &clear_hash, list) { 7366 clear_func_from_hashes(func); 7367 kfree(func); 7368 } 7369 } 7370 7371 void __init ftrace_free_init_mem(void) 7372 { 7373 void *start = (void *)(&__init_begin); 7374 void *end = (void *)(&__init_end); 7375 7376 ftrace_boot_snapshot(); 7377 7378 ftrace_free_mem(NULL, start, end); 7379 } 7380 7381 int __init __weak ftrace_dyn_arch_init(void) 7382 { 7383 return 0; 7384 } 7385 7386 void __init ftrace_init(void) 7387 { 7388 extern unsigned long __start_mcount_loc[]; 7389 extern unsigned long __stop_mcount_loc[]; 7390 unsigned long count, flags; 7391 int ret; 7392 7393 local_irq_save(flags); 7394 ret = ftrace_dyn_arch_init(); 7395 local_irq_restore(flags); 7396 if (ret) 7397 goto failed; 7398 7399 count = __stop_mcount_loc - __start_mcount_loc; 7400 if (!count) { 7401 pr_info("ftrace: No functions to be traced?\n"); 7402 goto failed; 7403 } 7404 7405 pr_info("ftrace: allocating %ld entries in %ld pages\n", 7406 count, DIV_ROUND_UP(count, ENTRIES_PER_PAGE)); 7407 7408 ret = ftrace_process_locs(NULL, 7409 __start_mcount_loc, 7410 __stop_mcount_loc); 7411 if (ret) { 7412 pr_warn("ftrace: failed to allocate entries for functions\n"); 7413 goto failed; 7414 } 7415 7416 pr_info("ftrace: allocated %ld pages with %ld groups\n", 7417 ftrace_number_of_pages, ftrace_number_of_groups); 7418 7419 last_ftrace_enabled = ftrace_enabled = 1; 7420 7421 set_ftrace_early_filters(); 7422 7423 return; 7424 failed: 7425 ftrace_disabled = 1; 7426 } 7427 7428 /* Do nothing if arch does not support this */ 7429 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops) 7430 { 7431 } 7432 7433 static void ftrace_update_trampoline(struct ftrace_ops *ops) 7434 { 7435 unsigned long trampoline = ops->trampoline; 7436 7437 arch_ftrace_update_trampoline(ops); 7438 if (ops->trampoline && ops->trampoline != trampoline && 7439 (ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP)) { 7440 /* Add to kallsyms before the perf events */ 7441 ftrace_add_trampoline_to_kallsyms(ops); 7442 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL, 7443 ops->trampoline, ops->trampoline_size, false, 7444 FTRACE_TRAMPOLINE_SYM); 7445 /* 7446 * Record the perf text poke event after the ksymbol register 7447 * event. 7448 */ 7449 perf_event_text_poke((void *)ops->trampoline, NULL, 0, 7450 (void *)ops->trampoline, 7451 ops->trampoline_size); 7452 } 7453 } 7454 7455 void ftrace_init_trace_array(struct trace_array *tr) 7456 { 7457 INIT_LIST_HEAD(&tr->func_probes); 7458 INIT_LIST_HEAD(&tr->mod_trace); 7459 INIT_LIST_HEAD(&tr->mod_notrace); 7460 } 7461 #else 7462 7463 struct ftrace_ops global_ops = { 7464 .func = ftrace_stub, 7465 .flags = FTRACE_OPS_FL_INITIALIZED | 7466 FTRACE_OPS_FL_PID, 7467 }; 7468 7469 static int __init ftrace_nodyn_init(void) 7470 { 7471 ftrace_enabled = 1; 7472 return 0; 7473 } 7474 core_initcall(ftrace_nodyn_init); 7475 7476 static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; } 7477 static inline void ftrace_startup_all(int command) { } 7478 7479 static void ftrace_update_trampoline(struct ftrace_ops *ops) 7480 { 7481 } 7482 7483 #endif /* CONFIG_DYNAMIC_FTRACE */ 7484 7485 __init void ftrace_init_global_array_ops(struct trace_array *tr) 7486 { 7487 tr->ops = &global_ops; 7488 tr->ops->private = tr; 7489 ftrace_init_trace_array(tr); 7490 } 7491 7492 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func) 7493 { 7494 /* If we filter on pids, update to use the pid function */ 7495 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) { 7496 if (WARN_ON(tr->ops->func != ftrace_stub)) 7497 printk("ftrace ops had %pS for function\n", 7498 tr->ops->func); 7499 } 7500 tr->ops->func = func; 7501 tr->ops->private = tr; 7502 } 7503 7504 void ftrace_reset_array_ops(struct trace_array *tr) 7505 { 7506 tr->ops->func = ftrace_stub; 7507 } 7508 7509 static nokprobe_inline void 7510 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, 7511 struct ftrace_ops *ignored, struct ftrace_regs *fregs) 7512 { 7513 struct pt_regs *regs = ftrace_get_regs(fregs); 7514 struct ftrace_ops *op; 7515 int bit; 7516 7517 /* 7518 * The ftrace_test_and_set_recursion() will disable preemption, 7519 * which is required since some of the ops may be dynamically 7520 * allocated, they must be freed after a synchronize_rcu(). 7521 */ 7522 bit = trace_test_and_set_recursion(ip, parent_ip, TRACE_LIST_START); 7523 if (bit < 0) 7524 return; 7525 7526 do_for_each_ftrace_op(op, ftrace_ops_list) { 7527 /* Stub functions don't need to be called nor tested */ 7528 if (op->flags & FTRACE_OPS_FL_STUB) 7529 continue; 7530 /* 7531 * Check the following for each ops before calling their func: 7532 * if RCU flag is set, then rcu_is_watching() must be true 7533 * Otherwise test if the ip matches the ops filter 7534 * 7535 * If any of the above fails then the op->func() is not executed. 7536 */ 7537 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) && 7538 ftrace_ops_test(op, ip, regs)) { 7539 if (FTRACE_WARN_ON(!op->func)) { 7540 pr_warn("op=%p %pS\n", op, op); 7541 goto out; 7542 } 7543 op->func(ip, parent_ip, op, fregs); 7544 } 7545 } while_for_each_ftrace_op(op); 7546 out: 7547 trace_clear_recursion(bit); 7548 } 7549 7550 /* 7551 * Some archs only support passing ip and parent_ip. Even though 7552 * the list function ignores the op parameter, we do not want any 7553 * C side effects, where a function is called without the caller 7554 * sending a third parameter. 7555 * Archs are to support both the regs and ftrace_ops at the same time. 7556 * If they support ftrace_ops, it is assumed they support regs. 7557 * If call backs want to use regs, they must either check for regs 7558 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS. 7559 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved. 7560 * An architecture can pass partial regs with ftrace_ops and still 7561 * set the ARCH_SUPPORTS_FTRACE_OPS. 7562 * 7563 * In vmlinux.lds.h, ftrace_ops_list_func() is defined to be 7564 * arch_ftrace_ops_list_func. 7565 */ 7566 #if ARCH_SUPPORTS_FTRACE_OPS 7567 void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, 7568 struct ftrace_ops *op, struct ftrace_regs *fregs) 7569 { 7570 __ftrace_ops_list_func(ip, parent_ip, NULL, fregs); 7571 } 7572 #else 7573 void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip) 7574 { 7575 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL); 7576 } 7577 #endif 7578 NOKPROBE_SYMBOL(arch_ftrace_ops_list_func); 7579 7580 /* 7581 * If there's only one function registered but it does not support 7582 * recursion, needs RCU protection, then this function will be called 7583 * by the mcount trampoline. 7584 */ 7585 static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip, 7586 struct ftrace_ops *op, struct ftrace_regs *fregs) 7587 { 7588 int bit; 7589 7590 bit = trace_test_and_set_recursion(ip, parent_ip, TRACE_LIST_START); 7591 if (bit < 0) 7592 return; 7593 7594 if (!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) 7595 op->func(ip, parent_ip, op, fregs); 7596 7597 trace_clear_recursion(bit); 7598 } 7599 NOKPROBE_SYMBOL(ftrace_ops_assist_func); 7600 7601 /** 7602 * ftrace_ops_get_func - get the function a trampoline should call 7603 * @ops: the ops to get the function for 7604 * 7605 * Normally the mcount trampoline will call the ops->func, but there 7606 * are times that it should not. For example, if the ops does not 7607 * have its own recursion protection, then it should call the 7608 * ftrace_ops_assist_func() instead. 7609 * 7610 * Returns the function that the trampoline should call for @ops. 7611 */ 7612 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops) 7613 { 7614 /* 7615 * If the function does not handle recursion or needs to be RCU safe, 7616 * then we need to call the assist handler. 7617 */ 7618 if (ops->flags & (FTRACE_OPS_FL_RECURSION | 7619 FTRACE_OPS_FL_RCU)) 7620 return ftrace_ops_assist_func; 7621 7622 return ops->func; 7623 } 7624 7625 static void 7626 ftrace_filter_pid_sched_switch_probe(void *data, bool preempt, 7627 struct task_struct *prev, 7628 struct task_struct *next, 7629 unsigned int prev_state) 7630 { 7631 struct trace_array *tr = data; 7632 struct trace_pid_list *pid_list; 7633 struct trace_pid_list *no_pid_list; 7634 7635 pid_list = rcu_dereference_sched(tr->function_pids); 7636 no_pid_list = rcu_dereference_sched(tr->function_no_pids); 7637 7638 if (trace_ignore_this_task(pid_list, no_pid_list, next)) 7639 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid, 7640 FTRACE_PID_IGNORE); 7641 else 7642 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid, 7643 next->pid); 7644 } 7645 7646 static void 7647 ftrace_pid_follow_sched_process_fork(void *data, 7648 struct task_struct *self, 7649 struct task_struct *task) 7650 { 7651 struct trace_pid_list *pid_list; 7652 struct trace_array *tr = data; 7653 7654 pid_list = rcu_dereference_sched(tr->function_pids); 7655 trace_filter_add_remove_task(pid_list, self, task); 7656 7657 pid_list = rcu_dereference_sched(tr->function_no_pids); 7658 trace_filter_add_remove_task(pid_list, self, task); 7659 } 7660 7661 static void 7662 ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task) 7663 { 7664 struct trace_pid_list *pid_list; 7665 struct trace_array *tr = data; 7666 7667 pid_list = rcu_dereference_sched(tr->function_pids); 7668 trace_filter_add_remove_task(pid_list, NULL, task); 7669 7670 pid_list = rcu_dereference_sched(tr->function_no_pids); 7671 trace_filter_add_remove_task(pid_list, NULL, task); 7672 } 7673 7674 void ftrace_pid_follow_fork(struct trace_array *tr, bool enable) 7675 { 7676 if (enable) { 7677 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork, 7678 tr); 7679 register_trace_sched_process_free(ftrace_pid_follow_sched_process_exit, 7680 tr); 7681 } else { 7682 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork, 7683 tr); 7684 unregister_trace_sched_process_free(ftrace_pid_follow_sched_process_exit, 7685 tr); 7686 } 7687 } 7688 7689 static void clear_ftrace_pids(struct trace_array *tr, int type) 7690 { 7691 struct trace_pid_list *pid_list; 7692 struct trace_pid_list *no_pid_list; 7693 int cpu; 7694 7695 pid_list = rcu_dereference_protected(tr->function_pids, 7696 lockdep_is_held(&ftrace_lock)); 7697 no_pid_list = rcu_dereference_protected(tr->function_no_pids, 7698 lockdep_is_held(&ftrace_lock)); 7699 7700 /* Make sure there's something to do */ 7701 if (!pid_type_enabled(type, pid_list, no_pid_list)) 7702 return; 7703 7704 /* See if the pids still need to be checked after this */ 7705 if (!still_need_pid_events(type, pid_list, no_pid_list)) { 7706 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr); 7707 for_each_possible_cpu(cpu) 7708 per_cpu_ptr(tr->array_buffer.data, cpu)->ftrace_ignore_pid = FTRACE_PID_TRACE; 7709 } 7710 7711 if (type & TRACE_PIDS) 7712 rcu_assign_pointer(tr->function_pids, NULL); 7713 7714 if (type & TRACE_NO_PIDS) 7715 rcu_assign_pointer(tr->function_no_pids, NULL); 7716 7717 /* Wait till all users are no longer using pid filtering */ 7718 synchronize_rcu(); 7719 7720 if ((type & TRACE_PIDS) && pid_list) 7721 trace_pid_list_free(pid_list); 7722 7723 if ((type & TRACE_NO_PIDS) && no_pid_list) 7724 trace_pid_list_free(no_pid_list); 7725 } 7726 7727 void ftrace_clear_pids(struct trace_array *tr) 7728 { 7729 mutex_lock(&ftrace_lock); 7730 7731 clear_ftrace_pids(tr, TRACE_PIDS | TRACE_NO_PIDS); 7732 7733 mutex_unlock(&ftrace_lock); 7734 } 7735 7736 static void ftrace_pid_reset(struct trace_array *tr, int type) 7737 { 7738 mutex_lock(&ftrace_lock); 7739 clear_ftrace_pids(tr, type); 7740 7741 ftrace_update_pid_func(); 7742 ftrace_startup_all(0); 7743 7744 mutex_unlock(&ftrace_lock); 7745 } 7746 7747 /* Greater than any max PID */ 7748 #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1) 7749 7750 static void *fpid_start(struct seq_file *m, loff_t *pos) 7751 __acquires(RCU) 7752 { 7753 struct trace_pid_list *pid_list; 7754 struct trace_array *tr = m->private; 7755 7756 mutex_lock(&ftrace_lock); 7757 rcu_read_lock_sched(); 7758 7759 pid_list = rcu_dereference_sched(tr->function_pids); 7760 7761 if (!pid_list) 7762 return !(*pos) ? FTRACE_NO_PIDS : NULL; 7763 7764 return trace_pid_start(pid_list, pos); 7765 } 7766 7767 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos) 7768 { 7769 struct trace_array *tr = m->private; 7770 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids); 7771 7772 if (v == FTRACE_NO_PIDS) { 7773 (*pos)++; 7774 return NULL; 7775 } 7776 return trace_pid_next(pid_list, v, pos); 7777 } 7778 7779 static void fpid_stop(struct seq_file *m, void *p) 7780 __releases(RCU) 7781 { 7782 rcu_read_unlock_sched(); 7783 mutex_unlock(&ftrace_lock); 7784 } 7785 7786 static int fpid_show(struct seq_file *m, void *v) 7787 { 7788 if (v == FTRACE_NO_PIDS) { 7789 seq_puts(m, "no pid\n"); 7790 return 0; 7791 } 7792 7793 return trace_pid_show(m, v); 7794 } 7795 7796 static const struct seq_operations ftrace_pid_sops = { 7797 .start = fpid_start, 7798 .next = fpid_next, 7799 .stop = fpid_stop, 7800 .show = fpid_show, 7801 }; 7802 7803 static void *fnpid_start(struct seq_file *m, loff_t *pos) 7804 __acquires(RCU) 7805 { 7806 struct trace_pid_list *pid_list; 7807 struct trace_array *tr = m->private; 7808 7809 mutex_lock(&ftrace_lock); 7810 rcu_read_lock_sched(); 7811 7812 pid_list = rcu_dereference_sched(tr->function_no_pids); 7813 7814 if (!pid_list) 7815 return !(*pos) ? FTRACE_NO_PIDS : NULL; 7816 7817 return trace_pid_start(pid_list, pos); 7818 } 7819 7820 static void *fnpid_next(struct seq_file *m, void *v, loff_t *pos) 7821 { 7822 struct trace_array *tr = m->private; 7823 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_no_pids); 7824 7825 if (v == FTRACE_NO_PIDS) { 7826 (*pos)++; 7827 return NULL; 7828 } 7829 return trace_pid_next(pid_list, v, pos); 7830 } 7831 7832 static const struct seq_operations ftrace_no_pid_sops = { 7833 .start = fnpid_start, 7834 .next = fnpid_next, 7835 .stop = fpid_stop, 7836 .show = fpid_show, 7837 }; 7838 7839 static int pid_open(struct inode *inode, struct file *file, int type) 7840 { 7841 const struct seq_operations *seq_ops; 7842 struct trace_array *tr = inode->i_private; 7843 struct seq_file *m; 7844 int ret = 0; 7845 7846 ret = tracing_check_open_get_tr(tr); 7847 if (ret) 7848 return ret; 7849 7850 if ((file->f_mode & FMODE_WRITE) && 7851 (file->f_flags & O_TRUNC)) 7852 ftrace_pid_reset(tr, type); 7853 7854 switch (type) { 7855 case TRACE_PIDS: 7856 seq_ops = &ftrace_pid_sops; 7857 break; 7858 case TRACE_NO_PIDS: 7859 seq_ops = &ftrace_no_pid_sops; 7860 break; 7861 default: 7862 trace_array_put(tr); 7863 WARN_ON_ONCE(1); 7864 return -EINVAL; 7865 } 7866 7867 ret = seq_open(file, seq_ops); 7868 if (ret < 0) { 7869 trace_array_put(tr); 7870 } else { 7871 m = file->private_data; 7872 /* copy tr over to seq ops */ 7873 m->private = tr; 7874 } 7875 7876 return ret; 7877 } 7878 7879 static int 7880 ftrace_pid_open(struct inode *inode, struct file *file) 7881 { 7882 return pid_open(inode, file, TRACE_PIDS); 7883 } 7884 7885 static int 7886 ftrace_no_pid_open(struct inode *inode, struct file *file) 7887 { 7888 return pid_open(inode, file, TRACE_NO_PIDS); 7889 } 7890 7891 static void ignore_task_cpu(void *data) 7892 { 7893 struct trace_array *tr = data; 7894 struct trace_pid_list *pid_list; 7895 struct trace_pid_list *no_pid_list; 7896 7897 /* 7898 * This function is called by on_each_cpu() while the 7899 * event_mutex is held. 7900 */ 7901 pid_list = rcu_dereference_protected(tr->function_pids, 7902 mutex_is_locked(&ftrace_lock)); 7903 no_pid_list = rcu_dereference_protected(tr->function_no_pids, 7904 mutex_is_locked(&ftrace_lock)); 7905 7906 if (trace_ignore_this_task(pid_list, no_pid_list, current)) 7907 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid, 7908 FTRACE_PID_IGNORE); 7909 else 7910 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid, 7911 current->pid); 7912 } 7913 7914 static ssize_t 7915 pid_write(struct file *filp, const char __user *ubuf, 7916 size_t cnt, loff_t *ppos, int type) 7917 { 7918 struct seq_file *m = filp->private_data; 7919 struct trace_array *tr = m->private; 7920 struct trace_pid_list *filtered_pids; 7921 struct trace_pid_list *other_pids; 7922 struct trace_pid_list *pid_list; 7923 ssize_t ret; 7924 7925 if (!cnt) 7926 return 0; 7927 7928 mutex_lock(&ftrace_lock); 7929 7930 switch (type) { 7931 case TRACE_PIDS: 7932 filtered_pids = rcu_dereference_protected(tr->function_pids, 7933 lockdep_is_held(&ftrace_lock)); 7934 other_pids = rcu_dereference_protected(tr->function_no_pids, 7935 lockdep_is_held(&ftrace_lock)); 7936 break; 7937 case TRACE_NO_PIDS: 7938 filtered_pids = rcu_dereference_protected(tr->function_no_pids, 7939 lockdep_is_held(&ftrace_lock)); 7940 other_pids = rcu_dereference_protected(tr->function_pids, 7941 lockdep_is_held(&ftrace_lock)); 7942 break; 7943 default: 7944 ret = -EINVAL; 7945 WARN_ON_ONCE(1); 7946 goto out; 7947 } 7948 7949 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt); 7950 if (ret < 0) 7951 goto out; 7952 7953 switch (type) { 7954 case TRACE_PIDS: 7955 rcu_assign_pointer(tr->function_pids, pid_list); 7956 break; 7957 case TRACE_NO_PIDS: 7958 rcu_assign_pointer(tr->function_no_pids, pid_list); 7959 break; 7960 } 7961 7962 7963 if (filtered_pids) { 7964 synchronize_rcu(); 7965 trace_pid_list_free(filtered_pids); 7966 } else if (pid_list && !other_pids) { 7967 /* Register a probe to set whether to ignore the tracing of a task */ 7968 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr); 7969 } 7970 7971 /* 7972 * Ignoring of pids is done at task switch. But we have to 7973 * check for those tasks that are currently running. 7974 * Always do this in case a pid was appended or removed. 7975 */ 7976 on_each_cpu(ignore_task_cpu, tr, 1); 7977 7978 ftrace_update_pid_func(); 7979 ftrace_startup_all(0); 7980 out: 7981 mutex_unlock(&ftrace_lock); 7982 7983 if (ret > 0) 7984 *ppos += ret; 7985 7986 return ret; 7987 } 7988 7989 static ssize_t 7990 ftrace_pid_write(struct file *filp, const char __user *ubuf, 7991 size_t cnt, loff_t *ppos) 7992 { 7993 return pid_write(filp, ubuf, cnt, ppos, TRACE_PIDS); 7994 } 7995 7996 static ssize_t 7997 ftrace_no_pid_write(struct file *filp, const char __user *ubuf, 7998 size_t cnt, loff_t *ppos) 7999 { 8000 return pid_write(filp, ubuf, cnt, ppos, TRACE_NO_PIDS); 8001 } 8002 8003 static int 8004 ftrace_pid_release(struct inode *inode, struct file *file) 8005 { 8006 struct trace_array *tr = inode->i_private; 8007 8008 trace_array_put(tr); 8009 8010 return seq_release(inode, file); 8011 } 8012 8013 static const struct file_operations ftrace_pid_fops = { 8014 .open = ftrace_pid_open, 8015 .write = ftrace_pid_write, 8016 .read = seq_read, 8017 .llseek = tracing_lseek, 8018 .release = ftrace_pid_release, 8019 }; 8020 8021 static const struct file_operations ftrace_no_pid_fops = { 8022 .open = ftrace_no_pid_open, 8023 .write = ftrace_no_pid_write, 8024 .read = seq_read, 8025 .llseek = tracing_lseek, 8026 .release = ftrace_pid_release, 8027 }; 8028 8029 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer) 8030 { 8031 trace_create_file("set_ftrace_pid", TRACE_MODE_WRITE, d_tracer, 8032 tr, &ftrace_pid_fops); 8033 trace_create_file("set_ftrace_notrace_pid", TRACE_MODE_WRITE, 8034 d_tracer, tr, &ftrace_no_pid_fops); 8035 } 8036 8037 void __init ftrace_init_tracefs_toplevel(struct trace_array *tr, 8038 struct dentry *d_tracer) 8039 { 8040 /* Only the top level directory has the dyn_tracefs and profile */ 8041 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL)); 8042 8043 ftrace_init_dyn_tracefs(d_tracer); 8044 ftrace_profile_tracefs(d_tracer); 8045 } 8046 8047 /** 8048 * ftrace_kill - kill ftrace 8049 * 8050 * This function should be used by panic code. It stops ftrace 8051 * but in a not so nice way. If you need to simply kill ftrace 8052 * from a non-atomic section, use ftrace_kill. 8053 */ 8054 void ftrace_kill(void) 8055 { 8056 ftrace_disabled = 1; 8057 ftrace_enabled = 0; 8058 ftrace_trace_function = ftrace_stub; 8059 } 8060 8061 /** 8062 * ftrace_is_dead - Test if ftrace is dead or not. 8063 * 8064 * Returns 1 if ftrace is "dead", zero otherwise. 8065 */ 8066 int ftrace_is_dead(void) 8067 { 8068 return ftrace_disabled; 8069 } 8070 8071 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS 8072 /* 8073 * When registering ftrace_ops with IPMODIFY, it is necessary to make sure 8074 * it doesn't conflict with any direct ftrace_ops. If there is existing 8075 * direct ftrace_ops on a kernel function being patched, call 8076 * FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_PEER on it to enable sharing. 8077 * 8078 * @ops: ftrace_ops being registered. 8079 * 8080 * Returns: 8081 * 0 on success; 8082 * Negative on failure. 8083 */ 8084 static int prepare_direct_functions_for_ipmodify(struct ftrace_ops *ops) 8085 { 8086 struct ftrace_func_entry *entry; 8087 struct ftrace_hash *hash; 8088 struct ftrace_ops *op; 8089 int size, i, ret; 8090 8091 lockdep_assert_held_once(&direct_mutex); 8092 8093 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY)) 8094 return 0; 8095 8096 hash = ops->func_hash->filter_hash; 8097 size = 1 << hash->size_bits; 8098 for (i = 0; i < size; i++) { 8099 hlist_for_each_entry(entry, &hash->buckets[i], hlist) { 8100 unsigned long ip = entry->ip; 8101 bool found_op = false; 8102 8103 mutex_lock(&ftrace_lock); 8104 do_for_each_ftrace_op(op, ftrace_ops_list) { 8105 if (!(op->flags & FTRACE_OPS_FL_DIRECT)) 8106 continue; 8107 if (ops_references_ip(op, ip)) { 8108 found_op = true; 8109 break; 8110 } 8111 } while_for_each_ftrace_op(op); 8112 mutex_unlock(&ftrace_lock); 8113 8114 if (found_op) { 8115 if (!op->ops_func) 8116 return -EBUSY; 8117 8118 ret = op->ops_func(op, FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_PEER); 8119 if (ret) 8120 return ret; 8121 } 8122 } 8123 } 8124 8125 return 0; 8126 } 8127 8128 /* 8129 * Similar to prepare_direct_functions_for_ipmodify, clean up after ops 8130 * with IPMODIFY is unregistered. The cleanup is optional for most DIRECT 8131 * ops. 8132 */ 8133 static void cleanup_direct_functions_after_ipmodify(struct ftrace_ops *ops) 8134 { 8135 struct ftrace_func_entry *entry; 8136 struct ftrace_hash *hash; 8137 struct ftrace_ops *op; 8138 int size, i; 8139 8140 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY)) 8141 return; 8142 8143 mutex_lock(&direct_mutex); 8144 8145 hash = ops->func_hash->filter_hash; 8146 size = 1 << hash->size_bits; 8147 for (i = 0; i < size; i++) { 8148 hlist_for_each_entry(entry, &hash->buckets[i], hlist) { 8149 unsigned long ip = entry->ip; 8150 bool found_op = false; 8151 8152 mutex_lock(&ftrace_lock); 8153 do_for_each_ftrace_op(op, ftrace_ops_list) { 8154 if (!(op->flags & FTRACE_OPS_FL_DIRECT)) 8155 continue; 8156 if (ops_references_ip(op, ip)) { 8157 found_op = true; 8158 break; 8159 } 8160 } while_for_each_ftrace_op(op); 8161 mutex_unlock(&ftrace_lock); 8162 8163 /* The cleanup is optional, ignore any errors */ 8164 if (found_op && op->ops_func) 8165 op->ops_func(op, FTRACE_OPS_CMD_DISABLE_SHARE_IPMODIFY_PEER); 8166 } 8167 } 8168 mutex_unlock(&direct_mutex); 8169 } 8170 8171 #define lock_direct_mutex() mutex_lock(&direct_mutex) 8172 #define unlock_direct_mutex() mutex_unlock(&direct_mutex) 8173 8174 #else /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */ 8175 8176 static int prepare_direct_functions_for_ipmodify(struct ftrace_ops *ops) 8177 { 8178 return 0; 8179 } 8180 8181 static void cleanup_direct_functions_after_ipmodify(struct ftrace_ops *ops) 8182 { 8183 } 8184 8185 #define lock_direct_mutex() do { } while (0) 8186 #define unlock_direct_mutex() do { } while (0) 8187 8188 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */ 8189 8190 /* 8191 * Similar to register_ftrace_function, except we don't lock direct_mutex. 8192 */ 8193 static int register_ftrace_function_nolock(struct ftrace_ops *ops) 8194 { 8195 int ret; 8196 8197 ftrace_ops_init(ops); 8198 8199 mutex_lock(&ftrace_lock); 8200 8201 ret = ftrace_startup(ops, 0); 8202 8203 mutex_unlock(&ftrace_lock); 8204 8205 return ret; 8206 } 8207 8208 /** 8209 * register_ftrace_function - register a function for profiling 8210 * @ops: ops structure that holds the function for profiling. 8211 * 8212 * Register a function to be called by all functions in the 8213 * kernel. 8214 * 8215 * Note: @ops->func and all the functions it calls must be labeled 8216 * with "notrace", otherwise it will go into a 8217 * recursive loop. 8218 */ 8219 int register_ftrace_function(struct ftrace_ops *ops) 8220 { 8221 int ret; 8222 8223 lock_direct_mutex(); 8224 ret = prepare_direct_functions_for_ipmodify(ops); 8225 if (ret < 0) 8226 goto out_unlock; 8227 8228 ret = register_ftrace_function_nolock(ops); 8229 8230 out_unlock: 8231 unlock_direct_mutex(); 8232 return ret; 8233 } 8234 EXPORT_SYMBOL_GPL(register_ftrace_function); 8235 8236 /** 8237 * unregister_ftrace_function - unregister a function for profiling. 8238 * @ops: ops structure that holds the function to unregister 8239 * 8240 * Unregister a function that was added to be called by ftrace profiling. 8241 */ 8242 int unregister_ftrace_function(struct ftrace_ops *ops) 8243 { 8244 int ret; 8245 8246 mutex_lock(&ftrace_lock); 8247 ret = ftrace_shutdown(ops, 0); 8248 mutex_unlock(&ftrace_lock); 8249 8250 cleanup_direct_functions_after_ipmodify(ops); 8251 return ret; 8252 } 8253 EXPORT_SYMBOL_GPL(unregister_ftrace_function); 8254 8255 static int symbols_cmp(const void *a, const void *b) 8256 { 8257 const char **str_a = (const char **) a; 8258 const char **str_b = (const char **) b; 8259 8260 return strcmp(*str_a, *str_b); 8261 } 8262 8263 struct kallsyms_data { 8264 unsigned long *addrs; 8265 const char **syms; 8266 size_t cnt; 8267 size_t found; 8268 }; 8269 8270 static int kallsyms_callback(void *data, const char *name, 8271 struct module *mod, unsigned long addr) 8272 { 8273 struct kallsyms_data *args = data; 8274 const char **sym; 8275 int idx; 8276 8277 sym = bsearch(&name, args->syms, args->cnt, sizeof(*args->syms), symbols_cmp); 8278 if (!sym) 8279 return 0; 8280 8281 idx = sym - args->syms; 8282 if (args->addrs[idx]) 8283 return 0; 8284 8285 if (!ftrace_location(addr)) 8286 return 0; 8287 8288 args->addrs[idx] = addr; 8289 args->found++; 8290 return args->found == args->cnt ? 1 : 0; 8291 } 8292 8293 /** 8294 * ftrace_lookup_symbols - Lookup addresses for array of symbols 8295 * 8296 * @sorted_syms: array of symbols pointers symbols to resolve, 8297 * must be alphabetically sorted 8298 * @cnt: number of symbols/addresses in @syms/@addrs arrays 8299 * @addrs: array for storing resulting addresses 8300 * 8301 * This function looks up addresses for array of symbols provided in 8302 * @syms array (must be alphabetically sorted) and stores them in 8303 * @addrs array, which needs to be big enough to store at least @cnt 8304 * addresses. 8305 * 8306 * This function returns 0 if all provided symbols are found, 8307 * -ESRCH otherwise. 8308 */ 8309 int ftrace_lookup_symbols(const char **sorted_syms, size_t cnt, unsigned long *addrs) 8310 { 8311 struct kallsyms_data args; 8312 int err; 8313 8314 memset(addrs, 0, sizeof(*addrs) * cnt); 8315 args.addrs = addrs; 8316 args.syms = sorted_syms; 8317 args.cnt = cnt; 8318 args.found = 0; 8319 err = kallsyms_on_each_symbol(kallsyms_callback, &args); 8320 if (err < 0) 8321 return err; 8322 return args.found == args.cnt ? 0 : -ESRCH; 8323 } 8324 8325 #ifdef CONFIG_SYSCTL 8326 8327 #ifdef CONFIG_DYNAMIC_FTRACE 8328 static void ftrace_startup_sysctl(void) 8329 { 8330 int command; 8331 8332 if (unlikely(ftrace_disabled)) 8333 return; 8334 8335 /* Force update next time */ 8336 saved_ftrace_func = NULL; 8337 /* ftrace_start_up is true if we want ftrace running */ 8338 if (ftrace_start_up) { 8339 command = FTRACE_UPDATE_CALLS; 8340 if (ftrace_graph_active) 8341 command |= FTRACE_START_FUNC_RET; 8342 ftrace_startup_enable(command); 8343 } 8344 } 8345 8346 static void ftrace_shutdown_sysctl(void) 8347 { 8348 int command; 8349 8350 if (unlikely(ftrace_disabled)) 8351 return; 8352 8353 /* ftrace_start_up is true if ftrace is running */ 8354 if (ftrace_start_up) { 8355 command = FTRACE_DISABLE_CALLS; 8356 if (ftrace_graph_active) 8357 command |= FTRACE_STOP_FUNC_RET; 8358 ftrace_run_update_code(command); 8359 } 8360 } 8361 #else 8362 # define ftrace_startup_sysctl() do { } while (0) 8363 # define ftrace_shutdown_sysctl() do { } while (0) 8364 #endif /* CONFIG_DYNAMIC_FTRACE */ 8365 8366 static bool is_permanent_ops_registered(void) 8367 { 8368 struct ftrace_ops *op; 8369 8370 do_for_each_ftrace_op(op, ftrace_ops_list) { 8371 if (op->flags & FTRACE_OPS_FL_PERMANENT) 8372 return true; 8373 } while_for_each_ftrace_op(op); 8374 8375 return false; 8376 } 8377 8378 static int 8379 ftrace_enable_sysctl(struct ctl_table *table, int write, 8380 void *buffer, size_t *lenp, loff_t *ppos) 8381 { 8382 int ret = -ENODEV; 8383 8384 mutex_lock(&ftrace_lock); 8385 8386 if (unlikely(ftrace_disabled)) 8387 goto out; 8388 8389 ret = proc_dointvec(table, write, buffer, lenp, ppos); 8390 8391 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled)) 8392 goto out; 8393 8394 if (ftrace_enabled) { 8395 8396 /* we are starting ftrace again */ 8397 if (rcu_dereference_protected(ftrace_ops_list, 8398 lockdep_is_held(&ftrace_lock)) != &ftrace_list_end) 8399 update_ftrace_function(); 8400 8401 ftrace_startup_sysctl(); 8402 8403 } else { 8404 if (is_permanent_ops_registered()) { 8405 ftrace_enabled = true; 8406 ret = -EBUSY; 8407 goto out; 8408 } 8409 8410 /* stopping ftrace calls (just send to ftrace_stub) */ 8411 ftrace_trace_function = ftrace_stub; 8412 8413 ftrace_shutdown_sysctl(); 8414 } 8415 8416 last_ftrace_enabled = !!ftrace_enabled; 8417 out: 8418 mutex_unlock(&ftrace_lock); 8419 return ret; 8420 } 8421 8422 static struct ctl_table ftrace_sysctls[] = { 8423 { 8424 .procname = "ftrace_enabled", 8425 .data = &ftrace_enabled, 8426 .maxlen = sizeof(int), 8427 .mode = 0644, 8428 .proc_handler = ftrace_enable_sysctl, 8429 }, 8430 {} 8431 }; 8432 8433 static int __init ftrace_sysctl_init(void) 8434 { 8435 register_sysctl_init("kernel", ftrace_sysctls); 8436 return 0; 8437 } 8438 late_initcall(ftrace_sysctl_init); 8439 #endif 8440