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