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