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