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