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