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