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