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