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 { 3225 struct ftrace_func_entry *entry; 3226 int size; 3227 int i; 3228 3229 /* An empty hash does everything */ 3230 if (ftrace_hash_empty(*hash)) 3231 return 0; 3232 3233 /* If new_hash has everything make hash have everything */ 3234 if (ftrace_hash_empty(new_hash)) { 3235 free_ftrace_hash(*hash); 3236 *hash = EMPTY_HASH; 3237 return 0; 3238 } 3239 3240 size = 1 << new_hash->size_bits; 3241 for (i = 0; i < size; i++) { 3242 hlist_for_each_entry(entry, &new_hash->buckets[i], hlist) { 3243 /* Only add if not already in hash */ 3244 if (!__ftrace_lookup_ip(*hash, entry->ip) && 3245 add_hash_entry(*hash, entry->ip) == NULL) 3246 return -ENOMEM; 3247 } 3248 } 3249 return 0; 3250 } 3251 3252 /* 3253 * Add to @hash only those that are in both @new_hash1 and @new_hash2 3254 * 3255 * The notrace_hash updates uses just the intersect_hash() function 3256 * and the filter_hash does not. 3257 */ 3258 static int intersect_hash(struct ftrace_hash **hash, struct ftrace_hash *new_hash1, 3259 struct ftrace_hash *new_hash2) 3260 { 3261 struct ftrace_func_entry *entry; 3262 int size; 3263 int i; 3264 3265 /* 3266 * If new_hash1 or new_hash2 is the EMPTY_HASH then make the hash 3267 * empty as well as empty for notrace means none are notraced. 3268 */ 3269 if (ftrace_hash_empty(new_hash1) || ftrace_hash_empty(new_hash2)) { 3270 free_ftrace_hash(*hash); 3271 *hash = EMPTY_HASH; 3272 return 0; 3273 } 3274 3275 size = 1 << new_hash1->size_bits; 3276 for (i = 0; i < size; i++) { 3277 hlist_for_each_entry(entry, &new_hash1->buckets[i], hlist) { 3278 /* Only add if in both @new_hash1 and @new_hash2 */ 3279 if (__ftrace_lookup_ip(new_hash2, entry->ip) && 3280 add_hash_entry(*hash, entry->ip) == NULL) 3281 return -ENOMEM; 3282 } 3283 } 3284 /* If nothing intersects, make it the empty set */ 3285 if (ftrace_hash_empty(*hash)) { 3286 free_ftrace_hash(*hash); 3287 *hash = EMPTY_HASH; 3288 } 3289 return 0; 3290 } 3291 3292 /* Return a new hash that has a union of all @ops->filter_hash entries */ 3293 static struct ftrace_hash *append_hashes(struct ftrace_ops *ops) 3294 { 3295 struct ftrace_hash *new_hash; 3296 struct ftrace_ops *subops; 3297 int ret; 3298 3299 new_hash = alloc_ftrace_hash(ops->func_hash->filter_hash->size_bits); 3300 if (!new_hash) 3301 return NULL; 3302 3303 list_for_each_entry(subops, &ops->subop_list, list) { 3304 ret = append_hash(&new_hash, subops->func_hash->filter_hash); 3305 if (ret < 0) { 3306 free_ftrace_hash(new_hash); 3307 return NULL; 3308 } 3309 /* Nothing more to do if new_hash is empty */ 3310 if (ftrace_hash_empty(new_hash)) 3311 break; 3312 } 3313 return new_hash; 3314 } 3315 3316 /* Make @ops trace evenything except what all its subops do not trace */ 3317 static struct ftrace_hash *intersect_hashes(struct ftrace_ops *ops) 3318 { 3319 struct ftrace_hash *new_hash = NULL; 3320 struct ftrace_ops *subops; 3321 int size_bits; 3322 int ret; 3323 3324 list_for_each_entry(subops, &ops->subop_list, list) { 3325 struct ftrace_hash *next_hash; 3326 3327 if (!new_hash) { 3328 size_bits = subops->func_hash->notrace_hash->size_bits; 3329 new_hash = alloc_and_copy_ftrace_hash(size_bits, ops->func_hash->notrace_hash); 3330 if (!new_hash) 3331 return NULL; 3332 continue; 3333 } 3334 size_bits = new_hash->size_bits; 3335 next_hash = new_hash; 3336 new_hash = alloc_ftrace_hash(size_bits); 3337 ret = intersect_hash(&new_hash, next_hash, subops->func_hash->notrace_hash); 3338 free_ftrace_hash(next_hash); 3339 if (ret < 0) { 3340 free_ftrace_hash(new_hash); 3341 return NULL; 3342 } 3343 /* Nothing more to do if new_hash is empty */ 3344 if (ftrace_hash_empty(new_hash)) 3345 break; 3346 } 3347 return new_hash; 3348 } 3349 3350 static bool ops_equal(struct ftrace_hash *A, struct ftrace_hash *B) 3351 { 3352 struct ftrace_func_entry *entry; 3353 int size; 3354 int i; 3355 3356 if (ftrace_hash_empty(A)) 3357 return ftrace_hash_empty(B); 3358 3359 if (ftrace_hash_empty(B)) 3360 return ftrace_hash_empty(A); 3361 3362 if (A->count != B->count) 3363 return false; 3364 3365 size = 1 << A->size_bits; 3366 for (i = 0; i < size; i++) { 3367 hlist_for_each_entry(entry, &A->buckets[i], hlist) { 3368 if (!__ftrace_lookup_ip(B, entry->ip)) 3369 return false; 3370 } 3371 } 3372 3373 return true; 3374 } 3375 3376 static void ftrace_ops_update_code(struct ftrace_ops *ops, 3377 struct ftrace_ops_hash *old_hash); 3378 3379 static int __ftrace_hash_move_and_update_ops(struct ftrace_ops *ops, 3380 struct ftrace_hash **orig_hash, 3381 struct ftrace_hash *hash, 3382 int enable) 3383 { 3384 struct ftrace_ops_hash old_hash_ops; 3385 struct ftrace_hash *old_hash; 3386 int ret; 3387 3388 old_hash = *orig_hash; 3389 old_hash_ops.filter_hash = ops->func_hash->filter_hash; 3390 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash; 3391 ret = ftrace_hash_move(ops, enable, orig_hash, hash); 3392 if (!ret) { 3393 ftrace_ops_update_code(ops, &old_hash_ops); 3394 free_ftrace_hash_rcu(old_hash); 3395 } 3396 return ret; 3397 } 3398 3399 static int ftrace_update_ops(struct ftrace_ops *ops, struct ftrace_hash *filter_hash, 3400 struct ftrace_hash *notrace_hash) 3401 { 3402 int ret; 3403 3404 if (!ops_equal(filter_hash, ops->func_hash->filter_hash)) { 3405 ret = __ftrace_hash_move_and_update_ops(ops, &ops->func_hash->filter_hash, 3406 filter_hash, 1); 3407 if (ret < 0) 3408 return ret; 3409 } 3410 3411 if (!ops_equal(notrace_hash, ops->func_hash->notrace_hash)) { 3412 ret = __ftrace_hash_move_and_update_ops(ops, &ops->func_hash->notrace_hash, 3413 notrace_hash, 0); 3414 if (ret < 0) 3415 return ret; 3416 } 3417 3418 return 0; 3419 } 3420 3421 /** 3422 * ftrace_startup_subops - enable tracing for subops of an ops 3423 * @ops: Manager ops (used to pick all the functions of its subops) 3424 * @subops: A new ops to add to @ops 3425 * @command: Extra commands to use to enable tracing 3426 * 3427 * The @ops is a manager @ops that has the filter that includes all the functions 3428 * that its list of subops are tracing. Adding a new @subops will add the 3429 * functions of @subops to @ops. 3430 */ 3431 int ftrace_startup_subops(struct ftrace_ops *ops, struct ftrace_ops *subops, int command) 3432 { 3433 struct ftrace_hash *filter_hash; 3434 struct ftrace_hash *notrace_hash; 3435 struct ftrace_hash *save_filter_hash; 3436 struct ftrace_hash *save_notrace_hash; 3437 int size_bits; 3438 int ret; 3439 3440 if (unlikely(ftrace_disabled)) 3441 return -ENODEV; 3442 3443 ftrace_ops_init(ops); 3444 ftrace_ops_init(subops); 3445 3446 if (WARN_ON_ONCE(subops->flags & FTRACE_OPS_FL_ENABLED)) 3447 return -EBUSY; 3448 3449 /* Make everything canonical (Just in case!) */ 3450 if (!ops->func_hash->filter_hash) 3451 ops->func_hash->filter_hash = EMPTY_HASH; 3452 if (!ops->func_hash->notrace_hash) 3453 ops->func_hash->notrace_hash = EMPTY_HASH; 3454 if (!subops->func_hash->filter_hash) 3455 subops->func_hash->filter_hash = EMPTY_HASH; 3456 if (!subops->func_hash->notrace_hash) 3457 subops->func_hash->notrace_hash = EMPTY_HASH; 3458 3459 /* For the first subops to ops just enable it normally */ 3460 if (list_empty(&ops->subop_list)) { 3461 /* Just use the subops hashes */ 3462 filter_hash = copy_hash(subops->func_hash->filter_hash); 3463 notrace_hash = copy_hash(subops->func_hash->notrace_hash); 3464 if (!filter_hash || !notrace_hash) { 3465 free_ftrace_hash(filter_hash); 3466 free_ftrace_hash(notrace_hash); 3467 return -ENOMEM; 3468 } 3469 3470 save_filter_hash = ops->func_hash->filter_hash; 3471 save_notrace_hash = ops->func_hash->notrace_hash; 3472 3473 ops->func_hash->filter_hash = filter_hash; 3474 ops->func_hash->notrace_hash = notrace_hash; 3475 list_add(&subops->list, &ops->subop_list); 3476 ret = ftrace_startup(ops, command); 3477 if (ret < 0) { 3478 list_del(&subops->list); 3479 ops->func_hash->filter_hash = save_filter_hash; 3480 ops->func_hash->notrace_hash = save_notrace_hash; 3481 free_ftrace_hash(filter_hash); 3482 free_ftrace_hash(notrace_hash); 3483 } else { 3484 free_ftrace_hash(save_filter_hash); 3485 free_ftrace_hash(save_notrace_hash); 3486 subops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_SUBOP; 3487 subops->managed = ops; 3488 } 3489 return ret; 3490 } 3491 3492 /* 3493 * Here there's already something attached. Here are the rules: 3494 * o If either filter_hash is empty then the final stays empty 3495 * o Otherwise, the final is a superset of both hashes 3496 * o If either notrace_hash is empty then the final stays empty 3497 * o Otherwise, the final is an intersection between the hashes 3498 */ 3499 if (ftrace_hash_empty(ops->func_hash->filter_hash) || 3500 ftrace_hash_empty(subops->func_hash->filter_hash)) { 3501 filter_hash = EMPTY_HASH; 3502 } else { 3503 size_bits = max(ops->func_hash->filter_hash->size_bits, 3504 subops->func_hash->filter_hash->size_bits); 3505 filter_hash = alloc_and_copy_ftrace_hash(size_bits, ops->func_hash->filter_hash); 3506 if (!filter_hash) 3507 return -ENOMEM; 3508 ret = append_hash(&filter_hash, subops->func_hash->filter_hash); 3509 if (ret < 0) { 3510 free_ftrace_hash(filter_hash); 3511 return ret; 3512 } 3513 } 3514 3515 if (ftrace_hash_empty(ops->func_hash->notrace_hash) || 3516 ftrace_hash_empty(subops->func_hash->notrace_hash)) { 3517 notrace_hash = EMPTY_HASH; 3518 } else { 3519 size_bits = max(ops->func_hash->filter_hash->size_bits, 3520 subops->func_hash->filter_hash->size_bits); 3521 notrace_hash = alloc_ftrace_hash(size_bits); 3522 if (!notrace_hash) { 3523 free_ftrace_hash(filter_hash); 3524 return -ENOMEM; 3525 } 3526 3527 ret = intersect_hash(¬race_hash, ops->func_hash->filter_hash, 3528 subops->func_hash->filter_hash); 3529 if (ret < 0) { 3530 free_ftrace_hash(filter_hash); 3531 free_ftrace_hash(notrace_hash); 3532 return ret; 3533 } 3534 } 3535 3536 list_add(&subops->list, &ops->subop_list); 3537 3538 ret = ftrace_update_ops(ops, filter_hash, notrace_hash); 3539 free_ftrace_hash(filter_hash); 3540 free_ftrace_hash(notrace_hash); 3541 if (ret < 0) { 3542 list_del(&subops->list); 3543 } else { 3544 subops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_SUBOP; 3545 subops->managed = ops; 3546 } 3547 return ret; 3548 } 3549 3550 /** 3551 * ftrace_shutdown_subops - Remove a subops from a manager ops 3552 * @ops: A manager ops to remove @subops from 3553 * @subops: The subops to remove from @ops 3554 * @command: Any extra command flags to add to modifying the text 3555 * 3556 * Removes the functions being traced by the @subops from @ops. Note, it 3557 * will not affect functions that are being traced by other subops that 3558 * still exist in @ops. 3559 * 3560 * If the last subops is removed from @ops, then @ops is shutdown normally. 3561 */ 3562 int ftrace_shutdown_subops(struct ftrace_ops *ops, struct ftrace_ops *subops, int command) 3563 { 3564 struct ftrace_hash *filter_hash; 3565 struct ftrace_hash *notrace_hash; 3566 int ret; 3567 3568 if (unlikely(ftrace_disabled)) 3569 return -ENODEV; 3570 3571 if (WARN_ON_ONCE(!(subops->flags & FTRACE_OPS_FL_ENABLED))) 3572 return -EINVAL; 3573 3574 list_del(&subops->list); 3575 3576 if (list_empty(&ops->subop_list)) { 3577 /* Last one, just disable the current ops */ 3578 3579 ret = ftrace_shutdown(ops, command); 3580 if (ret < 0) { 3581 list_add(&subops->list, &ops->subop_list); 3582 return ret; 3583 } 3584 3585 subops->flags &= ~FTRACE_OPS_FL_ENABLED; 3586 3587 free_ftrace_hash(ops->func_hash->filter_hash); 3588 free_ftrace_hash(ops->func_hash->notrace_hash); 3589 ops->func_hash->filter_hash = EMPTY_HASH; 3590 ops->func_hash->notrace_hash = EMPTY_HASH; 3591 subops->flags &= ~(FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_SUBOP); 3592 subops->managed = NULL; 3593 3594 return 0; 3595 } 3596 3597 /* Rebuild the hashes without subops */ 3598 filter_hash = append_hashes(ops); 3599 notrace_hash = intersect_hashes(ops); 3600 if (!filter_hash || !notrace_hash) { 3601 free_ftrace_hash(filter_hash); 3602 free_ftrace_hash(notrace_hash); 3603 list_add(&subops->list, &ops->subop_list); 3604 return -ENOMEM; 3605 } 3606 3607 ret = ftrace_update_ops(ops, filter_hash, notrace_hash); 3608 if (ret < 0) { 3609 list_add(&subops->list, &ops->subop_list); 3610 } else { 3611 subops->flags &= ~(FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_SUBOP); 3612 subops->managed = NULL; 3613 } 3614 free_ftrace_hash(filter_hash); 3615 free_ftrace_hash(notrace_hash); 3616 return ret; 3617 } 3618 3619 static int ftrace_hash_move_and_update_subops(struct ftrace_ops *subops, 3620 struct ftrace_hash **orig_subhash, 3621 struct ftrace_hash *hash, 3622 int enable) 3623 { 3624 struct ftrace_ops *ops = subops->managed; 3625 struct ftrace_hash **orig_hash; 3626 struct ftrace_hash *save_hash; 3627 struct ftrace_hash *new_hash; 3628 int ret; 3629 3630 /* Manager ops can not be subops (yet) */ 3631 if (WARN_ON_ONCE(!ops || ops->flags & FTRACE_OPS_FL_SUBOP)) 3632 return -EINVAL; 3633 3634 /* Move the new hash over to the subops hash */ 3635 save_hash = *orig_subhash; 3636 *orig_subhash = __ftrace_hash_move(hash); 3637 if (!*orig_subhash) { 3638 *orig_subhash = save_hash; 3639 return -ENOMEM; 3640 } 3641 3642 /* Create a new_hash to hold the ops new functions */ 3643 if (enable) { 3644 orig_hash = &ops->func_hash->filter_hash; 3645 new_hash = append_hashes(ops); 3646 } else { 3647 orig_hash = &ops->func_hash->notrace_hash; 3648 new_hash = intersect_hashes(ops); 3649 } 3650 3651 /* Move the hash over to the new hash */ 3652 ret = __ftrace_hash_move_and_update_ops(ops, orig_hash, new_hash, enable); 3653 3654 free_ftrace_hash(new_hash); 3655 3656 if (ret) { 3657 /* Put back the original hash */ 3658 free_ftrace_hash_rcu(*orig_subhash); 3659 *orig_subhash = save_hash; 3660 } else { 3661 free_ftrace_hash_rcu(save_hash); 3662 } 3663 return ret; 3664 } 3665 3666 3667 u64 ftrace_update_time; 3668 u64 ftrace_total_mod_time; 3669 unsigned long ftrace_update_tot_cnt; 3670 unsigned long ftrace_number_of_pages; 3671 unsigned long ftrace_number_of_groups; 3672 3673 static inline int ops_traces_mod(struct ftrace_ops *ops) 3674 { 3675 /* 3676 * Filter_hash being empty will default to trace module. 3677 * But notrace hash requires a test of individual module functions. 3678 */ 3679 return ftrace_hash_empty(ops->func_hash->filter_hash) && 3680 ftrace_hash_empty(ops->func_hash->notrace_hash); 3681 } 3682 3683 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs) 3684 { 3685 bool init_nop = ftrace_need_init_nop(); 3686 struct ftrace_page *pg; 3687 struct dyn_ftrace *p; 3688 u64 start, stop, update_time; 3689 unsigned long update_cnt = 0; 3690 unsigned long rec_flags = 0; 3691 int i; 3692 3693 start = ftrace_now(raw_smp_processor_id()); 3694 3695 /* 3696 * When a module is loaded, this function is called to convert 3697 * the calls to mcount in its text to nops, and also to create 3698 * an entry in the ftrace data. Now, if ftrace is activated 3699 * after this call, but before the module sets its text to 3700 * read-only, the modification of enabling ftrace can fail if 3701 * the read-only is done while ftrace is converting the calls. 3702 * To prevent this, the module's records are set as disabled 3703 * and will be enabled after the call to set the module's text 3704 * to read-only. 3705 */ 3706 if (mod) 3707 rec_flags |= FTRACE_FL_DISABLED; 3708 3709 for (pg = new_pgs; pg; pg = pg->next) { 3710 3711 for (i = 0; i < pg->index; i++) { 3712 3713 /* If something went wrong, bail without enabling anything */ 3714 if (unlikely(ftrace_disabled)) 3715 return -1; 3716 3717 p = &pg->records[i]; 3718 p->flags = rec_flags; 3719 3720 /* 3721 * Do the initial record conversion from mcount jump 3722 * to the NOP instructions. 3723 */ 3724 if (init_nop && !ftrace_nop_initialize(mod, p)) 3725 break; 3726 3727 update_cnt++; 3728 } 3729 } 3730 3731 stop = ftrace_now(raw_smp_processor_id()); 3732 update_time = stop - start; 3733 if (mod) 3734 ftrace_total_mod_time += update_time; 3735 else 3736 ftrace_update_time = update_time; 3737 ftrace_update_tot_cnt += update_cnt; 3738 3739 return 0; 3740 } 3741 3742 static int ftrace_allocate_records(struct ftrace_page *pg, int count) 3743 { 3744 int order; 3745 int pages; 3746 int cnt; 3747 3748 if (WARN_ON(!count)) 3749 return -EINVAL; 3750 3751 /* We want to fill as much as possible, with no empty pages */ 3752 pages = DIV_ROUND_UP(count, ENTRIES_PER_PAGE); 3753 order = fls(pages) - 1; 3754 3755 again: 3756 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order); 3757 3758 if (!pg->records) { 3759 /* if we can't allocate this size, try something smaller */ 3760 if (!order) 3761 return -ENOMEM; 3762 order--; 3763 goto again; 3764 } 3765 3766 ftrace_number_of_pages += 1 << order; 3767 ftrace_number_of_groups++; 3768 3769 cnt = (PAGE_SIZE << order) / ENTRY_SIZE; 3770 pg->order = order; 3771 3772 if (cnt > count) 3773 cnt = count; 3774 3775 return cnt; 3776 } 3777 3778 static void ftrace_free_pages(struct ftrace_page *pages) 3779 { 3780 struct ftrace_page *pg = pages; 3781 3782 while (pg) { 3783 if (pg->records) { 3784 free_pages((unsigned long)pg->records, pg->order); 3785 ftrace_number_of_pages -= 1 << pg->order; 3786 } 3787 pages = pg->next; 3788 kfree(pg); 3789 pg = pages; 3790 ftrace_number_of_groups--; 3791 } 3792 } 3793 3794 static struct ftrace_page * 3795 ftrace_allocate_pages(unsigned long num_to_init) 3796 { 3797 struct ftrace_page *start_pg; 3798 struct ftrace_page *pg; 3799 int cnt; 3800 3801 if (!num_to_init) 3802 return NULL; 3803 3804 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL); 3805 if (!pg) 3806 return NULL; 3807 3808 /* 3809 * Try to allocate as much as possible in one continues 3810 * location that fills in all of the space. We want to 3811 * waste as little space as possible. 3812 */ 3813 for (;;) { 3814 cnt = ftrace_allocate_records(pg, num_to_init); 3815 if (cnt < 0) 3816 goto free_pages; 3817 3818 num_to_init -= cnt; 3819 if (!num_to_init) 3820 break; 3821 3822 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL); 3823 if (!pg->next) 3824 goto free_pages; 3825 3826 pg = pg->next; 3827 } 3828 3829 return start_pg; 3830 3831 free_pages: 3832 ftrace_free_pages(start_pg); 3833 pr_info("ftrace: FAILED to allocate memory for functions\n"); 3834 return NULL; 3835 } 3836 3837 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */ 3838 3839 struct ftrace_iterator { 3840 loff_t pos; 3841 loff_t func_pos; 3842 loff_t mod_pos; 3843 struct ftrace_page *pg; 3844 struct dyn_ftrace *func; 3845 struct ftrace_func_probe *probe; 3846 struct ftrace_func_entry *probe_entry; 3847 struct trace_parser parser; 3848 struct ftrace_hash *hash; 3849 struct ftrace_ops *ops; 3850 struct trace_array *tr; 3851 struct list_head *mod_list; 3852 int pidx; 3853 int idx; 3854 unsigned flags; 3855 }; 3856 3857 static void * 3858 t_probe_next(struct seq_file *m, loff_t *pos) 3859 { 3860 struct ftrace_iterator *iter = m->private; 3861 struct trace_array *tr = iter->ops->private; 3862 struct list_head *func_probes; 3863 struct ftrace_hash *hash; 3864 struct list_head *next; 3865 struct hlist_node *hnd = NULL; 3866 struct hlist_head *hhd; 3867 int size; 3868 3869 (*pos)++; 3870 iter->pos = *pos; 3871 3872 if (!tr) 3873 return NULL; 3874 3875 func_probes = &tr->func_probes; 3876 if (list_empty(func_probes)) 3877 return NULL; 3878 3879 if (!iter->probe) { 3880 next = func_probes->next; 3881 iter->probe = list_entry(next, struct ftrace_func_probe, list); 3882 } 3883 3884 if (iter->probe_entry) 3885 hnd = &iter->probe_entry->hlist; 3886 3887 hash = iter->probe->ops.func_hash->filter_hash; 3888 3889 /* 3890 * A probe being registered may temporarily have an empty hash 3891 * and it's at the end of the func_probes list. 3892 */ 3893 if (!hash || hash == EMPTY_HASH) 3894 return NULL; 3895 3896 size = 1 << hash->size_bits; 3897 3898 retry: 3899 if (iter->pidx >= size) { 3900 if (iter->probe->list.next == func_probes) 3901 return NULL; 3902 next = iter->probe->list.next; 3903 iter->probe = list_entry(next, struct ftrace_func_probe, list); 3904 hash = iter->probe->ops.func_hash->filter_hash; 3905 size = 1 << hash->size_bits; 3906 iter->pidx = 0; 3907 } 3908 3909 hhd = &hash->buckets[iter->pidx]; 3910 3911 if (hlist_empty(hhd)) { 3912 iter->pidx++; 3913 hnd = NULL; 3914 goto retry; 3915 } 3916 3917 if (!hnd) 3918 hnd = hhd->first; 3919 else { 3920 hnd = hnd->next; 3921 if (!hnd) { 3922 iter->pidx++; 3923 goto retry; 3924 } 3925 } 3926 3927 if (WARN_ON_ONCE(!hnd)) 3928 return NULL; 3929 3930 iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist); 3931 3932 return iter; 3933 } 3934 3935 static void *t_probe_start(struct seq_file *m, loff_t *pos) 3936 { 3937 struct ftrace_iterator *iter = m->private; 3938 void *p = NULL; 3939 loff_t l; 3940 3941 if (!(iter->flags & FTRACE_ITER_DO_PROBES)) 3942 return NULL; 3943 3944 if (iter->mod_pos > *pos) 3945 return NULL; 3946 3947 iter->probe = NULL; 3948 iter->probe_entry = NULL; 3949 iter->pidx = 0; 3950 for (l = 0; l <= (*pos - iter->mod_pos); ) { 3951 p = t_probe_next(m, &l); 3952 if (!p) 3953 break; 3954 } 3955 if (!p) 3956 return NULL; 3957 3958 /* Only set this if we have an item */ 3959 iter->flags |= FTRACE_ITER_PROBE; 3960 3961 return iter; 3962 } 3963 3964 static int 3965 t_probe_show(struct seq_file *m, struct ftrace_iterator *iter) 3966 { 3967 struct ftrace_func_entry *probe_entry; 3968 struct ftrace_probe_ops *probe_ops; 3969 struct ftrace_func_probe *probe; 3970 3971 probe = iter->probe; 3972 probe_entry = iter->probe_entry; 3973 3974 if (WARN_ON_ONCE(!probe || !probe_entry)) 3975 return -EIO; 3976 3977 probe_ops = probe->probe_ops; 3978 3979 if (probe_ops->print) 3980 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data); 3981 3982 seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip, 3983 (void *)probe_ops->func); 3984 3985 return 0; 3986 } 3987 3988 static void * 3989 t_mod_next(struct seq_file *m, loff_t *pos) 3990 { 3991 struct ftrace_iterator *iter = m->private; 3992 struct trace_array *tr = iter->tr; 3993 3994 (*pos)++; 3995 iter->pos = *pos; 3996 3997 iter->mod_list = iter->mod_list->next; 3998 3999 if (iter->mod_list == &tr->mod_trace || 4000 iter->mod_list == &tr->mod_notrace) { 4001 iter->flags &= ~FTRACE_ITER_MOD; 4002 return NULL; 4003 } 4004 4005 iter->mod_pos = *pos; 4006 4007 return iter; 4008 } 4009 4010 static void *t_mod_start(struct seq_file *m, loff_t *pos) 4011 { 4012 struct ftrace_iterator *iter = m->private; 4013 void *p = NULL; 4014 loff_t l; 4015 4016 if (iter->func_pos > *pos) 4017 return NULL; 4018 4019 iter->mod_pos = iter->func_pos; 4020 4021 /* probes are only available if tr is set */ 4022 if (!iter->tr) 4023 return NULL; 4024 4025 for (l = 0; l <= (*pos - iter->func_pos); ) { 4026 p = t_mod_next(m, &l); 4027 if (!p) 4028 break; 4029 } 4030 if (!p) { 4031 iter->flags &= ~FTRACE_ITER_MOD; 4032 return t_probe_start(m, pos); 4033 } 4034 4035 /* Only set this if we have an item */ 4036 iter->flags |= FTRACE_ITER_MOD; 4037 4038 return iter; 4039 } 4040 4041 static int 4042 t_mod_show(struct seq_file *m, struct ftrace_iterator *iter) 4043 { 4044 struct ftrace_mod_load *ftrace_mod; 4045 struct trace_array *tr = iter->tr; 4046 4047 if (WARN_ON_ONCE(!iter->mod_list) || 4048 iter->mod_list == &tr->mod_trace || 4049 iter->mod_list == &tr->mod_notrace) 4050 return -EIO; 4051 4052 ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list); 4053 4054 if (ftrace_mod->func) 4055 seq_printf(m, "%s", ftrace_mod->func); 4056 else 4057 seq_putc(m, '*'); 4058 4059 seq_printf(m, ":mod:%s\n", ftrace_mod->module); 4060 4061 return 0; 4062 } 4063 4064 static void * 4065 t_func_next(struct seq_file *m, loff_t *pos) 4066 { 4067 struct ftrace_iterator *iter = m->private; 4068 struct dyn_ftrace *rec = NULL; 4069 4070 (*pos)++; 4071 4072 retry: 4073 if (iter->idx >= iter->pg->index) { 4074 if (iter->pg->next) { 4075 iter->pg = iter->pg->next; 4076 iter->idx = 0; 4077 goto retry; 4078 } 4079 } else { 4080 rec = &iter->pg->records[iter->idx++]; 4081 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) && 4082 !ftrace_lookup_ip(iter->hash, rec->ip)) || 4083 4084 ((iter->flags & FTRACE_ITER_ENABLED) && 4085 !(rec->flags & FTRACE_FL_ENABLED)) || 4086 4087 ((iter->flags & FTRACE_ITER_TOUCHED) && 4088 !(rec->flags & FTRACE_FL_TOUCHED))) { 4089 4090 rec = NULL; 4091 goto retry; 4092 } 4093 } 4094 4095 if (!rec) 4096 return NULL; 4097 4098 iter->pos = iter->func_pos = *pos; 4099 iter->func = rec; 4100 4101 return iter; 4102 } 4103 4104 static void * 4105 t_next(struct seq_file *m, void *v, loff_t *pos) 4106 { 4107 struct ftrace_iterator *iter = m->private; 4108 loff_t l = *pos; /* t_probe_start() must use original pos */ 4109 void *ret; 4110 4111 if (unlikely(ftrace_disabled)) 4112 return NULL; 4113 4114 if (iter->flags & FTRACE_ITER_PROBE) 4115 return t_probe_next(m, pos); 4116 4117 if (iter->flags & FTRACE_ITER_MOD) 4118 return t_mod_next(m, pos); 4119 4120 if (iter->flags & FTRACE_ITER_PRINTALL) { 4121 /* next must increment pos, and t_probe_start does not */ 4122 (*pos)++; 4123 return t_mod_start(m, &l); 4124 } 4125 4126 ret = t_func_next(m, pos); 4127 4128 if (!ret) 4129 return t_mod_start(m, &l); 4130 4131 return ret; 4132 } 4133 4134 static void reset_iter_read(struct ftrace_iterator *iter) 4135 { 4136 iter->pos = 0; 4137 iter->func_pos = 0; 4138 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD); 4139 } 4140 4141 static void *t_start(struct seq_file *m, loff_t *pos) 4142 { 4143 struct ftrace_iterator *iter = m->private; 4144 void *p = NULL; 4145 loff_t l; 4146 4147 mutex_lock(&ftrace_lock); 4148 4149 if (unlikely(ftrace_disabled)) 4150 return NULL; 4151 4152 /* 4153 * If an lseek was done, then reset and start from beginning. 4154 */ 4155 if (*pos < iter->pos) 4156 reset_iter_read(iter); 4157 4158 /* 4159 * For set_ftrace_filter reading, if we have the filter 4160 * off, we can short cut and just print out that all 4161 * functions are enabled. 4162 */ 4163 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) && 4164 ftrace_hash_empty(iter->hash)) { 4165 iter->func_pos = 1; /* Account for the message */ 4166 if (*pos > 0) 4167 return t_mod_start(m, pos); 4168 iter->flags |= FTRACE_ITER_PRINTALL; 4169 /* reset in case of seek/pread */ 4170 iter->flags &= ~FTRACE_ITER_PROBE; 4171 return iter; 4172 } 4173 4174 if (iter->flags & FTRACE_ITER_MOD) 4175 return t_mod_start(m, pos); 4176 4177 /* 4178 * Unfortunately, we need to restart at ftrace_pages_start 4179 * every time we let go of the ftrace_mutex. This is because 4180 * those pointers can change without the lock. 4181 */ 4182 iter->pg = ftrace_pages_start; 4183 iter->idx = 0; 4184 for (l = 0; l <= *pos; ) { 4185 p = t_func_next(m, &l); 4186 if (!p) 4187 break; 4188 } 4189 4190 if (!p) 4191 return t_mod_start(m, pos); 4192 4193 return iter; 4194 } 4195 4196 static void t_stop(struct seq_file *m, void *p) 4197 { 4198 mutex_unlock(&ftrace_lock); 4199 } 4200 4201 void * __weak 4202 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec) 4203 { 4204 return NULL; 4205 } 4206 4207 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops, 4208 struct dyn_ftrace *rec) 4209 { 4210 void *ptr; 4211 4212 ptr = arch_ftrace_trampoline_func(ops, rec); 4213 if (ptr) 4214 seq_printf(m, " ->%pS", ptr); 4215 } 4216 4217 #ifdef FTRACE_MCOUNT_MAX_OFFSET 4218 /* 4219 * Weak functions can still have an mcount/fentry that is saved in 4220 * the __mcount_loc section. These can be detected by having a 4221 * symbol offset of greater than FTRACE_MCOUNT_MAX_OFFSET, as the 4222 * symbol found by kallsyms is not the function that the mcount/fentry 4223 * is part of. The offset is much greater in these cases. 4224 * 4225 * Test the record to make sure that the ip points to a valid kallsyms 4226 * and if not, mark it disabled. 4227 */ 4228 static int test_for_valid_rec(struct dyn_ftrace *rec) 4229 { 4230 char str[KSYM_SYMBOL_LEN]; 4231 unsigned long offset; 4232 const char *ret; 4233 4234 ret = kallsyms_lookup(rec->ip, NULL, &offset, NULL, str); 4235 4236 /* Weak functions can cause invalid addresses */ 4237 if (!ret || offset > FTRACE_MCOUNT_MAX_OFFSET) { 4238 rec->flags |= FTRACE_FL_DISABLED; 4239 return 0; 4240 } 4241 return 1; 4242 } 4243 4244 static struct workqueue_struct *ftrace_check_wq __initdata; 4245 static struct work_struct ftrace_check_work __initdata; 4246 4247 /* 4248 * Scan all the mcount/fentry entries to make sure they are valid. 4249 */ 4250 static __init void ftrace_check_work_func(struct work_struct *work) 4251 { 4252 struct ftrace_page *pg; 4253 struct dyn_ftrace *rec; 4254 4255 mutex_lock(&ftrace_lock); 4256 do_for_each_ftrace_rec(pg, rec) { 4257 test_for_valid_rec(rec); 4258 } while_for_each_ftrace_rec(); 4259 mutex_unlock(&ftrace_lock); 4260 } 4261 4262 static int __init ftrace_check_for_weak_functions(void) 4263 { 4264 INIT_WORK(&ftrace_check_work, ftrace_check_work_func); 4265 4266 ftrace_check_wq = alloc_workqueue("ftrace_check_wq", WQ_UNBOUND, 0); 4267 4268 queue_work(ftrace_check_wq, &ftrace_check_work); 4269 return 0; 4270 } 4271 4272 static int __init ftrace_check_sync(void) 4273 { 4274 /* Make sure the ftrace_check updates are finished */ 4275 if (ftrace_check_wq) 4276 destroy_workqueue(ftrace_check_wq); 4277 return 0; 4278 } 4279 4280 late_initcall_sync(ftrace_check_sync); 4281 subsys_initcall(ftrace_check_for_weak_functions); 4282 4283 static int print_rec(struct seq_file *m, unsigned long ip) 4284 { 4285 unsigned long offset; 4286 char str[KSYM_SYMBOL_LEN]; 4287 char *modname; 4288 const char *ret; 4289 4290 ret = kallsyms_lookup(ip, NULL, &offset, &modname, str); 4291 /* Weak functions can cause invalid addresses */ 4292 if (!ret || offset > FTRACE_MCOUNT_MAX_OFFSET) { 4293 snprintf(str, KSYM_SYMBOL_LEN, "%s_%ld", 4294 FTRACE_INVALID_FUNCTION, offset); 4295 ret = NULL; 4296 } 4297 4298 seq_puts(m, str); 4299 if (modname) 4300 seq_printf(m, " [%s]", modname); 4301 return ret == NULL ? -1 : 0; 4302 } 4303 #else 4304 static inline int test_for_valid_rec(struct dyn_ftrace *rec) 4305 { 4306 return 1; 4307 } 4308 4309 static inline int print_rec(struct seq_file *m, unsigned long ip) 4310 { 4311 seq_printf(m, "%ps", (void *)ip); 4312 return 0; 4313 } 4314 #endif 4315 4316 static int t_show(struct seq_file *m, void *v) 4317 { 4318 struct ftrace_iterator *iter = m->private; 4319 struct dyn_ftrace *rec; 4320 4321 if (iter->flags & FTRACE_ITER_PROBE) 4322 return t_probe_show(m, iter); 4323 4324 if (iter->flags & FTRACE_ITER_MOD) 4325 return t_mod_show(m, iter); 4326 4327 if (iter->flags & FTRACE_ITER_PRINTALL) { 4328 if (iter->flags & FTRACE_ITER_NOTRACE) 4329 seq_puts(m, "#### no functions disabled ####\n"); 4330 else 4331 seq_puts(m, "#### all functions enabled ####\n"); 4332 return 0; 4333 } 4334 4335 rec = iter->func; 4336 4337 if (!rec) 4338 return 0; 4339 4340 if (iter->flags & FTRACE_ITER_ADDRS) 4341 seq_printf(m, "%lx ", rec->ip); 4342 4343 if (print_rec(m, rec->ip)) { 4344 /* This should only happen when a rec is disabled */ 4345 WARN_ON_ONCE(!(rec->flags & FTRACE_FL_DISABLED)); 4346 seq_putc(m, '\n'); 4347 return 0; 4348 } 4349 4350 if (iter->flags & (FTRACE_ITER_ENABLED | FTRACE_ITER_TOUCHED)) { 4351 struct ftrace_ops *ops; 4352 4353 seq_printf(m, " (%ld)%s%s%s%s%s", 4354 ftrace_rec_count(rec), 4355 rec->flags & FTRACE_FL_REGS ? " R" : " ", 4356 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ", 4357 rec->flags & FTRACE_FL_DIRECT ? " D" : " ", 4358 rec->flags & FTRACE_FL_CALL_OPS ? " O" : " ", 4359 rec->flags & FTRACE_FL_MODIFIED ? " M " : " "); 4360 if (rec->flags & FTRACE_FL_TRAMP_EN) { 4361 ops = ftrace_find_tramp_ops_any(rec); 4362 if (ops) { 4363 do { 4364 seq_printf(m, "\ttramp: %pS (%pS)", 4365 (void *)ops->trampoline, 4366 (void *)ops->func); 4367 add_trampoline_func(m, ops, rec); 4368 ops = ftrace_find_tramp_ops_next(rec, ops); 4369 } while (ops); 4370 } else 4371 seq_puts(m, "\ttramp: ERROR!"); 4372 } else { 4373 add_trampoline_func(m, NULL, rec); 4374 } 4375 if (rec->flags & FTRACE_FL_CALL_OPS_EN) { 4376 ops = ftrace_find_unique_ops(rec); 4377 if (ops) { 4378 seq_printf(m, "\tops: %pS (%pS)", 4379 ops, ops->func); 4380 } else { 4381 seq_puts(m, "\tops: ERROR!"); 4382 } 4383 } 4384 if (rec->flags & FTRACE_FL_DIRECT) { 4385 unsigned long direct; 4386 4387 direct = ftrace_find_rec_direct(rec->ip); 4388 if (direct) 4389 seq_printf(m, "\n\tdirect-->%pS", (void *)direct); 4390 } 4391 } 4392 4393 seq_putc(m, '\n'); 4394 4395 return 0; 4396 } 4397 4398 static const struct seq_operations show_ftrace_seq_ops = { 4399 .start = t_start, 4400 .next = t_next, 4401 .stop = t_stop, 4402 .show = t_show, 4403 }; 4404 4405 static int 4406 ftrace_avail_open(struct inode *inode, struct file *file) 4407 { 4408 struct ftrace_iterator *iter; 4409 int ret; 4410 4411 ret = security_locked_down(LOCKDOWN_TRACEFS); 4412 if (ret) 4413 return ret; 4414 4415 if (unlikely(ftrace_disabled)) 4416 return -ENODEV; 4417 4418 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter)); 4419 if (!iter) 4420 return -ENOMEM; 4421 4422 iter->pg = ftrace_pages_start; 4423 iter->ops = &global_ops; 4424 4425 return 0; 4426 } 4427 4428 static int 4429 ftrace_enabled_open(struct inode *inode, struct file *file) 4430 { 4431 struct ftrace_iterator *iter; 4432 4433 /* 4434 * This shows us what functions are currently being 4435 * traced and by what. Not sure if we want lockdown 4436 * to hide such critical information for an admin. 4437 * Although, perhaps it can show information we don't 4438 * want people to see, but if something is tracing 4439 * something, we probably want to know about it. 4440 */ 4441 4442 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter)); 4443 if (!iter) 4444 return -ENOMEM; 4445 4446 iter->pg = ftrace_pages_start; 4447 iter->flags = FTRACE_ITER_ENABLED; 4448 iter->ops = &global_ops; 4449 4450 return 0; 4451 } 4452 4453 static int 4454 ftrace_touched_open(struct inode *inode, struct file *file) 4455 { 4456 struct ftrace_iterator *iter; 4457 4458 /* 4459 * This shows us what functions have ever been enabled 4460 * (traced, direct, patched, etc). Not sure if we want lockdown 4461 * to hide such critical information for an admin. 4462 * Although, perhaps it can show information we don't 4463 * want people to see, but if something had traced 4464 * something, we probably want to know about it. 4465 */ 4466 4467 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter)); 4468 if (!iter) 4469 return -ENOMEM; 4470 4471 iter->pg = ftrace_pages_start; 4472 iter->flags = FTRACE_ITER_TOUCHED; 4473 iter->ops = &global_ops; 4474 4475 return 0; 4476 } 4477 4478 static int 4479 ftrace_avail_addrs_open(struct inode *inode, struct file *file) 4480 { 4481 struct ftrace_iterator *iter; 4482 int ret; 4483 4484 ret = security_locked_down(LOCKDOWN_TRACEFS); 4485 if (ret) 4486 return ret; 4487 4488 if (unlikely(ftrace_disabled)) 4489 return -ENODEV; 4490 4491 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter)); 4492 if (!iter) 4493 return -ENOMEM; 4494 4495 iter->pg = ftrace_pages_start; 4496 iter->flags = FTRACE_ITER_ADDRS; 4497 iter->ops = &global_ops; 4498 4499 return 0; 4500 } 4501 4502 /** 4503 * ftrace_regex_open - initialize function tracer filter files 4504 * @ops: The ftrace_ops that hold the hash filters 4505 * @flag: The type of filter to process 4506 * @inode: The inode, usually passed in to your open routine 4507 * @file: The file, usually passed in to your open routine 4508 * 4509 * ftrace_regex_open() initializes the filter files for the 4510 * @ops. Depending on @flag it may process the filter hash or 4511 * the notrace hash of @ops. With this called from the open 4512 * routine, you can use ftrace_filter_write() for the write 4513 * routine if @flag has FTRACE_ITER_FILTER set, or 4514 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set. 4515 * tracing_lseek() should be used as the lseek routine, and 4516 * release must call ftrace_regex_release(). 4517 * 4518 * Returns: 0 on success or a negative errno value on failure 4519 */ 4520 int 4521 ftrace_regex_open(struct ftrace_ops *ops, int flag, 4522 struct inode *inode, struct file *file) 4523 { 4524 struct ftrace_iterator *iter; 4525 struct ftrace_hash *hash; 4526 struct list_head *mod_head; 4527 struct trace_array *tr = ops->private; 4528 int ret = -ENOMEM; 4529 4530 ftrace_ops_init(ops); 4531 4532 if (unlikely(ftrace_disabled)) 4533 return -ENODEV; 4534 4535 if (tracing_check_open_get_tr(tr)) 4536 return -ENODEV; 4537 4538 iter = kzalloc(sizeof(*iter), GFP_KERNEL); 4539 if (!iter) 4540 goto out; 4541 4542 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) 4543 goto out; 4544 4545 iter->ops = ops; 4546 iter->flags = flag; 4547 iter->tr = tr; 4548 4549 mutex_lock(&ops->func_hash->regex_lock); 4550 4551 if (flag & FTRACE_ITER_NOTRACE) { 4552 hash = ops->func_hash->notrace_hash; 4553 mod_head = tr ? &tr->mod_notrace : NULL; 4554 } else { 4555 hash = ops->func_hash->filter_hash; 4556 mod_head = tr ? &tr->mod_trace : NULL; 4557 } 4558 4559 iter->mod_list = mod_head; 4560 4561 if (file->f_mode & FMODE_WRITE) { 4562 const int size_bits = FTRACE_HASH_DEFAULT_BITS; 4563 4564 if (file->f_flags & O_TRUNC) { 4565 iter->hash = alloc_ftrace_hash(size_bits); 4566 clear_ftrace_mod_list(mod_head); 4567 } else { 4568 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash); 4569 } 4570 4571 if (!iter->hash) { 4572 trace_parser_put(&iter->parser); 4573 goto out_unlock; 4574 } 4575 } else 4576 iter->hash = hash; 4577 4578 ret = 0; 4579 4580 if (file->f_mode & FMODE_READ) { 4581 iter->pg = ftrace_pages_start; 4582 4583 ret = seq_open(file, &show_ftrace_seq_ops); 4584 if (!ret) { 4585 struct seq_file *m = file->private_data; 4586 m->private = iter; 4587 } else { 4588 /* Failed */ 4589 free_ftrace_hash(iter->hash); 4590 trace_parser_put(&iter->parser); 4591 } 4592 } else 4593 file->private_data = iter; 4594 4595 out_unlock: 4596 mutex_unlock(&ops->func_hash->regex_lock); 4597 4598 out: 4599 if (ret) { 4600 kfree(iter); 4601 if (tr) 4602 trace_array_put(tr); 4603 } 4604 4605 return ret; 4606 } 4607 4608 static int 4609 ftrace_filter_open(struct inode *inode, struct file *file) 4610 { 4611 struct ftrace_ops *ops = inode->i_private; 4612 4613 /* Checks for tracefs lockdown */ 4614 return ftrace_regex_open(ops, 4615 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES, 4616 inode, file); 4617 } 4618 4619 static int 4620 ftrace_notrace_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, FTRACE_ITER_NOTRACE, 4626 inode, file); 4627 } 4628 4629 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */ 4630 struct ftrace_glob { 4631 char *search; 4632 unsigned len; 4633 int type; 4634 }; 4635 4636 /* 4637 * If symbols in an architecture don't correspond exactly to the user-visible 4638 * name of what they represent, it is possible to define this function to 4639 * perform the necessary adjustments. 4640 */ 4641 char * __weak arch_ftrace_match_adjust(char *str, const char *search) 4642 { 4643 return str; 4644 } 4645 4646 static int ftrace_match(char *str, struct ftrace_glob *g) 4647 { 4648 int matched = 0; 4649 int slen; 4650 4651 str = arch_ftrace_match_adjust(str, g->search); 4652 4653 switch (g->type) { 4654 case MATCH_FULL: 4655 if (strcmp(str, g->search) == 0) 4656 matched = 1; 4657 break; 4658 case MATCH_FRONT_ONLY: 4659 if (strncmp(str, g->search, g->len) == 0) 4660 matched = 1; 4661 break; 4662 case MATCH_MIDDLE_ONLY: 4663 if (strstr(str, g->search)) 4664 matched = 1; 4665 break; 4666 case MATCH_END_ONLY: 4667 slen = strlen(str); 4668 if (slen >= g->len && 4669 memcmp(str + slen - g->len, g->search, g->len) == 0) 4670 matched = 1; 4671 break; 4672 case MATCH_GLOB: 4673 if (glob_match(g->search, str)) 4674 matched = 1; 4675 break; 4676 } 4677 4678 return matched; 4679 } 4680 4681 static int 4682 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter) 4683 { 4684 struct ftrace_func_entry *entry; 4685 int ret = 0; 4686 4687 entry = ftrace_lookup_ip(hash, rec->ip); 4688 if (clear_filter) { 4689 /* Do nothing if it doesn't exist */ 4690 if (!entry) 4691 return 0; 4692 4693 free_hash_entry(hash, entry); 4694 } else { 4695 /* Do nothing if it exists */ 4696 if (entry) 4697 return 0; 4698 if (add_hash_entry(hash, rec->ip) == NULL) 4699 ret = -ENOMEM; 4700 } 4701 return ret; 4702 } 4703 4704 static int 4705 add_rec_by_index(struct ftrace_hash *hash, struct ftrace_glob *func_g, 4706 int clear_filter) 4707 { 4708 long index; 4709 struct ftrace_page *pg; 4710 struct dyn_ftrace *rec; 4711 4712 /* The index starts at 1 */ 4713 if (kstrtoul(func_g->search, 0, &index) || --index < 0) 4714 return 0; 4715 4716 do_for_each_ftrace_rec(pg, rec) { 4717 if (pg->index <= index) { 4718 index -= pg->index; 4719 /* this is a double loop, break goes to the next page */ 4720 break; 4721 } 4722 rec = &pg->records[index]; 4723 enter_record(hash, rec, clear_filter); 4724 return 1; 4725 } while_for_each_ftrace_rec(); 4726 return 0; 4727 } 4728 4729 #ifdef FTRACE_MCOUNT_MAX_OFFSET 4730 static int lookup_ip(unsigned long ip, char **modname, char *str) 4731 { 4732 unsigned long offset; 4733 4734 kallsyms_lookup(ip, NULL, &offset, modname, str); 4735 if (offset > FTRACE_MCOUNT_MAX_OFFSET) 4736 return -1; 4737 return 0; 4738 } 4739 #else 4740 static int lookup_ip(unsigned long ip, char **modname, char *str) 4741 { 4742 kallsyms_lookup(ip, NULL, NULL, modname, str); 4743 return 0; 4744 } 4745 #endif 4746 4747 static int 4748 ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g, 4749 struct ftrace_glob *mod_g, int exclude_mod) 4750 { 4751 char str[KSYM_SYMBOL_LEN]; 4752 char *modname; 4753 4754 if (lookup_ip(rec->ip, &modname, str)) { 4755 /* This should only happen when a rec is disabled */ 4756 WARN_ON_ONCE(system_state == SYSTEM_RUNNING && 4757 !(rec->flags & FTRACE_FL_DISABLED)); 4758 return 0; 4759 } 4760 4761 if (mod_g) { 4762 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0; 4763 4764 /* blank module name to match all modules */ 4765 if (!mod_g->len) { 4766 /* blank module globbing: modname xor exclude_mod */ 4767 if (!exclude_mod != !modname) 4768 goto func_match; 4769 return 0; 4770 } 4771 4772 /* 4773 * exclude_mod is set to trace everything but the given 4774 * module. If it is set and the module matches, then 4775 * return 0. If it is not set, and the module doesn't match 4776 * also return 0. Otherwise, check the function to see if 4777 * that matches. 4778 */ 4779 if (!mod_matches == !exclude_mod) 4780 return 0; 4781 func_match: 4782 /* blank search means to match all funcs in the mod */ 4783 if (!func_g->len) 4784 return 1; 4785 } 4786 4787 return ftrace_match(str, func_g); 4788 } 4789 4790 static int 4791 match_records(struct ftrace_hash *hash, char *func, int len, char *mod) 4792 { 4793 struct ftrace_page *pg; 4794 struct dyn_ftrace *rec; 4795 struct ftrace_glob func_g = { .type = MATCH_FULL }; 4796 struct ftrace_glob mod_g = { .type = MATCH_FULL }; 4797 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL; 4798 int exclude_mod = 0; 4799 int found = 0; 4800 int ret; 4801 int clear_filter = 0; 4802 4803 if (func) { 4804 func_g.type = filter_parse_regex(func, len, &func_g.search, 4805 &clear_filter); 4806 func_g.len = strlen(func_g.search); 4807 } 4808 4809 if (mod) { 4810 mod_g.type = filter_parse_regex(mod, strlen(mod), 4811 &mod_g.search, &exclude_mod); 4812 mod_g.len = strlen(mod_g.search); 4813 } 4814 4815 guard(mutex)(&ftrace_lock); 4816 4817 if (unlikely(ftrace_disabled)) 4818 return 0; 4819 4820 if (func_g.type == MATCH_INDEX) 4821 return add_rec_by_index(hash, &func_g, clear_filter); 4822 4823 do_for_each_ftrace_rec(pg, rec) { 4824 4825 if (rec->flags & FTRACE_FL_DISABLED) 4826 continue; 4827 4828 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) { 4829 ret = enter_record(hash, rec, clear_filter); 4830 if (ret < 0) 4831 return ret; 4832 found = 1; 4833 } 4834 cond_resched(); 4835 } while_for_each_ftrace_rec(); 4836 4837 return found; 4838 } 4839 4840 static int 4841 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len) 4842 { 4843 return match_records(hash, buff, len, NULL); 4844 } 4845 4846 static void ftrace_ops_update_code(struct ftrace_ops *ops, 4847 struct ftrace_ops_hash *old_hash) 4848 { 4849 struct ftrace_ops *op; 4850 4851 if (!ftrace_enabled) 4852 return; 4853 4854 if (ops->flags & FTRACE_OPS_FL_ENABLED) { 4855 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash); 4856 return; 4857 } 4858 4859 /* 4860 * If this is the shared global_ops filter, then we need to 4861 * check if there is another ops that shares it, is enabled. 4862 * If so, we still need to run the modify code. 4863 */ 4864 if (ops->func_hash != &global_ops.local_hash) 4865 return; 4866 4867 do_for_each_ftrace_op(op, ftrace_ops_list) { 4868 if (op->func_hash == &global_ops.local_hash && 4869 op->flags & FTRACE_OPS_FL_ENABLED) { 4870 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash); 4871 /* Only need to do this once */ 4872 return; 4873 } 4874 } while_for_each_ftrace_op(op); 4875 } 4876 4877 static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops, 4878 struct ftrace_hash **orig_hash, 4879 struct ftrace_hash *hash, 4880 int enable) 4881 { 4882 if (ops->flags & FTRACE_OPS_FL_SUBOP) 4883 return ftrace_hash_move_and_update_subops(ops, orig_hash, hash, enable); 4884 4885 /* 4886 * If this ops is not enabled, it could be sharing its filters 4887 * with a subop. If that's the case, update the subop instead of 4888 * this ops. Shared filters are only allowed to have one ops set 4889 * at a time, and if we update the ops that is not enabled, 4890 * it will not affect subops that share it. 4891 */ 4892 if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) { 4893 struct ftrace_ops *op; 4894 4895 /* Check if any other manager subops maps to this hash */ 4896 do_for_each_ftrace_op(op, ftrace_ops_list) { 4897 struct ftrace_ops *subops; 4898 4899 list_for_each_entry(subops, &op->subop_list, list) { 4900 if ((subops->flags & FTRACE_OPS_FL_ENABLED) && 4901 subops->func_hash == ops->func_hash) { 4902 return ftrace_hash_move_and_update_subops(subops, orig_hash, hash, enable); 4903 } 4904 } 4905 } while_for_each_ftrace_op(op); 4906 } 4907 4908 return __ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable); 4909 } 4910 4911 static int cache_mod(struct trace_array *tr, 4912 const char *func, char *module, int enable) 4913 { 4914 struct ftrace_mod_load *ftrace_mod, *n; 4915 struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace; 4916 4917 guard(mutex)(&ftrace_lock); 4918 4919 /* We do not cache inverse filters */ 4920 if (func[0] == '!') { 4921 int ret = -EINVAL; 4922 4923 func++; 4924 4925 /* Look to remove this hash */ 4926 list_for_each_entry_safe(ftrace_mod, n, head, list) { 4927 if (strcmp(ftrace_mod->module, module) != 0) 4928 continue; 4929 4930 /* no func matches all */ 4931 if (strcmp(func, "*") == 0 || 4932 (ftrace_mod->func && 4933 strcmp(ftrace_mod->func, func) == 0)) { 4934 ret = 0; 4935 free_ftrace_mod(ftrace_mod); 4936 continue; 4937 } 4938 } 4939 return ret; 4940 } 4941 4942 /* We only care about modules that have not been loaded yet */ 4943 if (module_exists(module)) 4944 return -EINVAL; 4945 4946 /* Save this string off, and execute it when the module is loaded */ 4947 return ftrace_add_mod(tr, func, module, enable); 4948 } 4949 4950 #ifdef CONFIG_MODULES 4951 static void process_mod_list(struct list_head *head, struct ftrace_ops *ops, 4952 char *mod, bool enable) 4953 { 4954 struct ftrace_mod_load *ftrace_mod, *n; 4955 struct ftrace_hash **orig_hash, *new_hash; 4956 LIST_HEAD(process_mods); 4957 char *func; 4958 4959 mutex_lock(&ops->func_hash->regex_lock); 4960 4961 if (enable) 4962 orig_hash = &ops->func_hash->filter_hash; 4963 else 4964 orig_hash = &ops->func_hash->notrace_hash; 4965 4966 new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, 4967 *orig_hash); 4968 if (!new_hash) 4969 goto out; /* warn? */ 4970 4971 mutex_lock(&ftrace_lock); 4972 4973 list_for_each_entry_safe(ftrace_mod, n, head, list) { 4974 4975 if (strcmp(ftrace_mod->module, mod) != 0) 4976 continue; 4977 4978 if (ftrace_mod->func) 4979 func = kstrdup(ftrace_mod->func, GFP_KERNEL); 4980 else 4981 func = kstrdup("*", GFP_KERNEL); 4982 4983 if (!func) /* warn? */ 4984 continue; 4985 4986 list_move(&ftrace_mod->list, &process_mods); 4987 4988 /* Use the newly allocated func, as it may be "*" */ 4989 kfree(ftrace_mod->func); 4990 ftrace_mod->func = func; 4991 } 4992 4993 mutex_unlock(&ftrace_lock); 4994 4995 list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) { 4996 4997 func = ftrace_mod->func; 4998 4999 /* Grabs ftrace_lock, which is why we have this extra step */ 5000 match_records(new_hash, func, strlen(func), mod); 5001 free_ftrace_mod(ftrace_mod); 5002 } 5003 5004 if (enable && list_empty(head)) 5005 new_hash->flags &= ~FTRACE_HASH_FL_MOD; 5006 5007 mutex_lock(&ftrace_lock); 5008 5009 ftrace_hash_move_and_update_ops(ops, orig_hash, 5010 new_hash, enable); 5011 mutex_unlock(&ftrace_lock); 5012 5013 out: 5014 mutex_unlock(&ops->func_hash->regex_lock); 5015 5016 free_ftrace_hash(new_hash); 5017 } 5018 5019 static void process_cached_mods(const char *mod_name) 5020 { 5021 struct trace_array *tr; 5022 char *mod; 5023 5024 mod = kstrdup(mod_name, GFP_KERNEL); 5025 if (!mod) 5026 return; 5027 5028 mutex_lock(&trace_types_lock); 5029 list_for_each_entry(tr, &ftrace_trace_arrays, list) { 5030 if (!list_empty(&tr->mod_trace)) 5031 process_mod_list(&tr->mod_trace, tr->ops, mod, true); 5032 if (!list_empty(&tr->mod_notrace)) 5033 process_mod_list(&tr->mod_notrace, tr->ops, mod, false); 5034 } 5035 mutex_unlock(&trace_types_lock); 5036 5037 kfree(mod); 5038 } 5039 #endif 5040 5041 /* 5042 * We register the module command as a template to show others how 5043 * to register the a command as well. 5044 */ 5045 5046 static int 5047 ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash, 5048 char *func_orig, char *cmd, char *module, int enable) 5049 { 5050 char *func; 5051 int ret; 5052 5053 if (!tr) 5054 return -ENODEV; 5055 5056 /* match_records() modifies func, and we need the original */ 5057 func = kstrdup(func_orig, GFP_KERNEL); 5058 if (!func) 5059 return -ENOMEM; 5060 5061 /* 5062 * cmd == 'mod' because we only registered this func 5063 * for the 'mod' ftrace_func_command. 5064 * But if you register one func with multiple commands, 5065 * you can tell which command was used by the cmd 5066 * parameter. 5067 */ 5068 ret = match_records(hash, func, strlen(func), module); 5069 kfree(func); 5070 5071 if (!ret) 5072 return cache_mod(tr, func_orig, module, enable); 5073 if (ret < 0) 5074 return ret; 5075 return 0; 5076 } 5077 5078 static struct ftrace_func_command ftrace_mod_cmd = { 5079 .name = "mod", 5080 .func = ftrace_mod_callback, 5081 }; 5082 5083 static int __init ftrace_mod_cmd_init(void) 5084 { 5085 return register_ftrace_command(&ftrace_mod_cmd); 5086 } 5087 core_initcall(ftrace_mod_cmd_init); 5088 5089 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip, 5090 struct ftrace_ops *op, struct ftrace_regs *fregs) 5091 { 5092 struct ftrace_probe_ops *probe_ops; 5093 struct ftrace_func_probe *probe; 5094 5095 probe = container_of(op, struct ftrace_func_probe, ops); 5096 probe_ops = probe->probe_ops; 5097 5098 /* 5099 * Disable preemption for these calls to prevent a RCU grace 5100 * period. This syncs the hash iteration and freeing of items 5101 * on the hash. rcu_read_lock is too dangerous here. 5102 */ 5103 preempt_disable_notrace(); 5104 probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data); 5105 preempt_enable_notrace(); 5106 } 5107 5108 struct ftrace_func_map { 5109 struct ftrace_func_entry entry; 5110 void *data; 5111 }; 5112 5113 struct ftrace_func_mapper { 5114 struct ftrace_hash hash; 5115 }; 5116 5117 /** 5118 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper 5119 * 5120 * Returns: a ftrace_func_mapper descriptor that can be used to map ips to data. 5121 */ 5122 struct ftrace_func_mapper *allocate_ftrace_func_mapper(void) 5123 { 5124 struct ftrace_hash *hash; 5125 5126 /* 5127 * The mapper is simply a ftrace_hash, but since the entries 5128 * in the hash are not ftrace_func_entry type, we define it 5129 * as a separate structure. 5130 */ 5131 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS); 5132 return (struct ftrace_func_mapper *)hash; 5133 } 5134 5135 /** 5136 * ftrace_func_mapper_find_ip - Find some data mapped to an ip 5137 * @mapper: The mapper that has the ip maps 5138 * @ip: the instruction pointer to find the data for 5139 * 5140 * Returns: the data mapped to @ip if found otherwise NULL. The return 5141 * is actually the address of the mapper data pointer. The address is 5142 * returned for use cases where the data is no bigger than a long, and 5143 * the user can use the data pointer as its data instead of having to 5144 * allocate more memory for the reference. 5145 */ 5146 void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper, 5147 unsigned long ip) 5148 { 5149 struct ftrace_func_entry *entry; 5150 struct ftrace_func_map *map; 5151 5152 entry = ftrace_lookup_ip(&mapper->hash, ip); 5153 if (!entry) 5154 return NULL; 5155 5156 map = (struct ftrace_func_map *)entry; 5157 return &map->data; 5158 } 5159 5160 /** 5161 * ftrace_func_mapper_add_ip - Map some data to an ip 5162 * @mapper: The mapper that has the ip maps 5163 * @ip: The instruction pointer address to map @data to 5164 * @data: The data to map to @ip 5165 * 5166 * Returns: 0 on success otherwise an error. 5167 */ 5168 int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper, 5169 unsigned long ip, void *data) 5170 { 5171 struct ftrace_func_entry *entry; 5172 struct ftrace_func_map *map; 5173 5174 entry = ftrace_lookup_ip(&mapper->hash, ip); 5175 if (entry) 5176 return -EBUSY; 5177 5178 map = kmalloc(sizeof(*map), GFP_KERNEL); 5179 if (!map) 5180 return -ENOMEM; 5181 5182 map->entry.ip = ip; 5183 map->data = data; 5184 5185 __add_hash_entry(&mapper->hash, &map->entry); 5186 5187 return 0; 5188 } 5189 5190 /** 5191 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping 5192 * @mapper: The mapper that has the ip maps 5193 * @ip: The instruction pointer address to remove the data from 5194 * 5195 * Returns: the data if it is found, otherwise NULL. 5196 * Note, if the data pointer is used as the data itself, (see 5197 * ftrace_func_mapper_find_ip(), then the return value may be meaningless, 5198 * if the data pointer was set to zero. 5199 */ 5200 void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper, 5201 unsigned long ip) 5202 { 5203 struct ftrace_func_entry *entry; 5204 struct ftrace_func_map *map; 5205 void *data; 5206 5207 entry = ftrace_lookup_ip(&mapper->hash, ip); 5208 if (!entry) 5209 return NULL; 5210 5211 map = (struct ftrace_func_map *)entry; 5212 data = map->data; 5213 5214 remove_hash_entry(&mapper->hash, entry); 5215 kfree(entry); 5216 5217 return data; 5218 } 5219 5220 /** 5221 * free_ftrace_func_mapper - free a mapping of ips and data 5222 * @mapper: The mapper that has the ip maps 5223 * @free_func: A function to be called on each data item. 5224 * 5225 * This is used to free the function mapper. The @free_func is optional 5226 * and can be used if the data needs to be freed as well. 5227 */ 5228 void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper, 5229 ftrace_mapper_func free_func) 5230 { 5231 struct ftrace_func_entry *entry; 5232 struct ftrace_func_map *map; 5233 struct hlist_head *hhd; 5234 int size, i; 5235 5236 if (!mapper) 5237 return; 5238 5239 if (free_func && mapper->hash.count) { 5240 size = 1 << mapper->hash.size_bits; 5241 for (i = 0; i < size; i++) { 5242 hhd = &mapper->hash.buckets[i]; 5243 hlist_for_each_entry(entry, hhd, hlist) { 5244 map = (struct ftrace_func_map *)entry; 5245 free_func(map); 5246 } 5247 } 5248 } 5249 free_ftrace_hash(&mapper->hash); 5250 } 5251 5252 static void release_probe(struct ftrace_func_probe *probe) 5253 { 5254 struct ftrace_probe_ops *probe_ops; 5255 5256 guard(mutex)(&ftrace_lock); 5257 5258 WARN_ON(probe->ref <= 0); 5259 5260 /* Subtract the ref that was used to protect this instance */ 5261 probe->ref--; 5262 5263 if (!probe->ref) { 5264 probe_ops = probe->probe_ops; 5265 /* 5266 * Sending zero as ip tells probe_ops to free 5267 * the probe->data itself 5268 */ 5269 if (probe_ops->free) 5270 probe_ops->free(probe_ops, probe->tr, 0, probe->data); 5271 list_del(&probe->list); 5272 kfree(probe); 5273 } 5274 } 5275 5276 static void acquire_probe_locked(struct ftrace_func_probe *probe) 5277 { 5278 /* 5279 * Add one ref to keep it from being freed when releasing the 5280 * ftrace_lock mutex. 5281 */ 5282 probe->ref++; 5283 } 5284 5285 int 5286 register_ftrace_function_probe(char *glob, struct trace_array *tr, 5287 struct ftrace_probe_ops *probe_ops, 5288 void *data) 5289 { 5290 struct ftrace_func_probe *probe = NULL, *iter; 5291 struct ftrace_func_entry *entry; 5292 struct ftrace_hash **orig_hash; 5293 struct ftrace_hash *old_hash; 5294 struct ftrace_hash *hash; 5295 int count = 0; 5296 int size; 5297 int ret; 5298 int i; 5299 5300 if (WARN_ON(!tr)) 5301 return -EINVAL; 5302 5303 /* We do not support '!' for function probes */ 5304 if (WARN_ON(glob[0] == '!')) 5305 return -EINVAL; 5306 5307 5308 mutex_lock(&ftrace_lock); 5309 /* Check if the probe_ops is already registered */ 5310 list_for_each_entry(iter, &tr->func_probes, list) { 5311 if (iter->probe_ops == probe_ops) { 5312 probe = iter; 5313 break; 5314 } 5315 } 5316 if (!probe) { 5317 probe = kzalloc(sizeof(*probe), GFP_KERNEL); 5318 if (!probe) { 5319 mutex_unlock(&ftrace_lock); 5320 return -ENOMEM; 5321 } 5322 probe->probe_ops = probe_ops; 5323 probe->ops.func = function_trace_probe_call; 5324 probe->tr = tr; 5325 ftrace_ops_init(&probe->ops); 5326 list_add(&probe->list, &tr->func_probes); 5327 } 5328 5329 acquire_probe_locked(probe); 5330 5331 mutex_unlock(&ftrace_lock); 5332 5333 /* 5334 * Note, there's a small window here that the func_hash->filter_hash 5335 * may be NULL or empty. Need to be careful when reading the loop. 5336 */ 5337 mutex_lock(&probe->ops.func_hash->regex_lock); 5338 5339 orig_hash = &probe->ops.func_hash->filter_hash; 5340 old_hash = *orig_hash; 5341 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash); 5342 5343 if (!hash) { 5344 ret = -ENOMEM; 5345 goto out; 5346 } 5347 5348 ret = ftrace_match_records(hash, glob, strlen(glob)); 5349 5350 /* Nothing found? */ 5351 if (!ret) 5352 ret = -EINVAL; 5353 5354 if (ret < 0) 5355 goto out; 5356 5357 size = 1 << hash->size_bits; 5358 for (i = 0; i < size; i++) { 5359 hlist_for_each_entry(entry, &hash->buckets[i], hlist) { 5360 if (ftrace_lookup_ip(old_hash, entry->ip)) 5361 continue; 5362 /* 5363 * The caller might want to do something special 5364 * for each function we find. We call the callback 5365 * to give the caller an opportunity to do so. 5366 */ 5367 if (probe_ops->init) { 5368 ret = probe_ops->init(probe_ops, tr, 5369 entry->ip, data, 5370 &probe->data); 5371 if (ret < 0) { 5372 if (probe_ops->free && count) 5373 probe_ops->free(probe_ops, tr, 5374 0, probe->data); 5375 probe->data = NULL; 5376 goto out; 5377 } 5378 } 5379 count++; 5380 } 5381 } 5382 5383 mutex_lock(&ftrace_lock); 5384 5385 if (!count) { 5386 /* Nothing was added? */ 5387 ret = -EINVAL; 5388 goto out_unlock; 5389 } 5390 5391 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash, 5392 hash, 1); 5393 if (ret < 0) 5394 goto err_unlock; 5395 5396 /* One ref for each new function traced */ 5397 probe->ref += count; 5398 5399 if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED)) 5400 ret = ftrace_startup(&probe->ops, 0); 5401 5402 out_unlock: 5403 mutex_unlock(&ftrace_lock); 5404 5405 if (!ret) 5406 ret = count; 5407 out: 5408 mutex_unlock(&probe->ops.func_hash->regex_lock); 5409 free_ftrace_hash(hash); 5410 5411 release_probe(probe); 5412 5413 return ret; 5414 5415 err_unlock: 5416 if (!probe_ops->free || !count) 5417 goto out_unlock; 5418 5419 /* Failed to do the move, need to call the free functions */ 5420 for (i = 0; i < size; i++) { 5421 hlist_for_each_entry(entry, &hash->buckets[i], hlist) { 5422 if (ftrace_lookup_ip(old_hash, entry->ip)) 5423 continue; 5424 probe_ops->free(probe_ops, tr, entry->ip, probe->data); 5425 } 5426 } 5427 goto out_unlock; 5428 } 5429 5430 int 5431 unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr, 5432 struct ftrace_probe_ops *probe_ops) 5433 { 5434 struct ftrace_func_probe *probe = NULL, *iter; 5435 struct ftrace_ops_hash old_hash_ops; 5436 struct ftrace_func_entry *entry; 5437 struct ftrace_glob func_g; 5438 struct ftrace_hash **orig_hash; 5439 struct ftrace_hash *old_hash; 5440 struct ftrace_hash *hash = NULL; 5441 struct hlist_node *tmp; 5442 struct hlist_head hhd; 5443 char str[KSYM_SYMBOL_LEN]; 5444 int count = 0; 5445 int i, ret = -ENODEV; 5446 int size; 5447 5448 if (!glob || !strlen(glob) || !strcmp(glob, "*")) 5449 func_g.search = NULL; 5450 else { 5451 int not; 5452 5453 func_g.type = filter_parse_regex(glob, strlen(glob), 5454 &func_g.search, ¬); 5455 func_g.len = strlen(func_g.search); 5456 5457 /* we do not support '!' for function probes */ 5458 if (WARN_ON(not)) 5459 return -EINVAL; 5460 } 5461 5462 mutex_lock(&ftrace_lock); 5463 /* Check if the probe_ops is already registered */ 5464 list_for_each_entry(iter, &tr->func_probes, list) { 5465 if (iter->probe_ops == probe_ops) { 5466 probe = iter; 5467 break; 5468 } 5469 } 5470 if (!probe) 5471 goto err_unlock_ftrace; 5472 5473 ret = -EINVAL; 5474 if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED)) 5475 goto err_unlock_ftrace; 5476 5477 acquire_probe_locked(probe); 5478 5479 mutex_unlock(&ftrace_lock); 5480 5481 mutex_lock(&probe->ops.func_hash->regex_lock); 5482 5483 orig_hash = &probe->ops.func_hash->filter_hash; 5484 old_hash = *orig_hash; 5485 5486 if (ftrace_hash_empty(old_hash)) 5487 goto out_unlock; 5488 5489 old_hash_ops.filter_hash = old_hash; 5490 /* Probes only have filters */ 5491 old_hash_ops.notrace_hash = NULL; 5492 5493 ret = -ENOMEM; 5494 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash); 5495 if (!hash) 5496 goto out_unlock; 5497 5498 INIT_HLIST_HEAD(&hhd); 5499 5500 size = 1 << hash->size_bits; 5501 for (i = 0; i < size; i++) { 5502 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) { 5503 5504 if (func_g.search) { 5505 kallsyms_lookup(entry->ip, NULL, NULL, 5506 NULL, str); 5507 if (!ftrace_match(str, &func_g)) 5508 continue; 5509 } 5510 count++; 5511 remove_hash_entry(hash, entry); 5512 hlist_add_head(&entry->hlist, &hhd); 5513 } 5514 } 5515 5516 /* Nothing found? */ 5517 if (!count) { 5518 ret = -EINVAL; 5519 goto out_unlock; 5520 } 5521 5522 mutex_lock(&ftrace_lock); 5523 5524 WARN_ON(probe->ref < count); 5525 5526 probe->ref -= count; 5527 5528 if (ftrace_hash_empty(hash)) 5529 ftrace_shutdown(&probe->ops, 0); 5530 5531 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash, 5532 hash, 1); 5533 5534 /* still need to update the function call sites */ 5535 if (ftrace_enabled && !ftrace_hash_empty(hash)) 5536 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS, 5537 &old_hash_ops); 5538 synchronize_rcu(); 5539 5540 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) { 5541 hlist_del(&entry->hlist); 5542 if (probe_ops->free) 5543 probe_ops->free(probe_ops, tr, entry->ip, probe->data); 5544 kfree(entry); 5545 } 5546 mutex_unlock(&ftrace_lock); 5547 5548 out_unlock: 5549 mutex_unlock(&probe->ops.func_hash->regex_lock); 5550 free_ftrace_hash(hash); 5551 5552 release_probe(probe); 5553 5554 return ret; 5555 5556 err_unlock_ftrace: 5557 mutex_unlock(&ftrace_lock); 5558 return ret; 5559 } 5560 5561 void clear_ftrace_function_probes(struct trace_array *tr) 5562 { 5563 struct ftrace_func_probe *probe, *n; 5564 5565 list_for_each_entry_safe(probe, n, &tr->func_probes, list) 5566 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops); 5567 } 5568 5569 static LIST_HEAD(ftrace_commands); 5570 static DEFINE_MUTEX(ftrace_cmd_mutex); 5571 5572 /* 5573 * Currently we only register ftrace commands from __init, so mark this 5574 * __init too. 5575 */ 5576 __init int register_ftrace_command(struct ftrace_func_command *cmd) 5577 { 5578 struct ftrace_func_command *p; 5579 5580 guard(mutex)(&ftrace_cmd_mutex); 5581 list_for_each_entry(p, &ftrace_commands, list) { 5582 if (strcmp(cmd->name, p->name) == 0) 5583 return -EBUSY; 5584 } 5585 list_add(&cmd->list, &ftrace_commands); 5586 5587 return 0; 5588 } 5589 5590 /* 5591 * Currently we only unregister ftrace commands from __init, so mark 5592 * this __init too. 5593 */ 5594 __init int unregister_ftrace_command(struct ftrace_func_command *cmd) 5595 { 5596 struct ftrace_func_command *p, *n; 5597 5598 guard(mutex)(&ftrace_cmd_mutex); 5599 5600 list_for_each_entry_safe(p, n, &ftrace_commands, list) { 5601 if (strcmp(cmd->name, p->name) == 0) { 5602 list_del_init(&p->list); 5603 return 0; 5604 } 5605 } 5606 5607 return -ENODEV; 5608 } 5609 5610 static int ftrace_process_regex(struct ftrace_iterator *iter, 5611 char *buff, int len, int enable) 5612 { 5613 struct ftrace_hash *hash = iter->hash; 5614 struct trace_array *tr = iter->ops->private; 5615 char *func, *command, *next = buff; 5616 struct ftrace_func_command *p; 5617 int ret; 5618 5619 func = strsep(&next, ":"); 5620 5621 if (!next) { 5622 ret = ftrace_match_records(hash, func, len); 5623 if (!ret) 5624 ret = -EINVAL; 5625 if (ret < 0) 5626 return ret; 5627 return 0; 5628 } 5629 5630 /* command found */ 5631 5632 command = strsep(&next, ":"); 5633 5634 guard(mutex)(&ftrace_cmd_mutex); 5635 5636 list_for_each_entry(p, &ftrace_commands, list) { 5637 if (strcmp(p->name, command) == 0) 5638 return p->func(tr, hash, func, command, next, enable); 5639 } 5640 5641 return -EINVAL; 5642 } 5643 5644 static ssize_t 5645 ftrace_regex_write(struct file *file, const char __user *ubuf, 5646 size_t cnt, loff_t *ppos, int enable) 5647 { 5648 struct ftrace_iterator *iter; 5649 struct trace_parser *parser; 5650 ssize_t ret, read; 5651 5652 if (!cnt) 5653 return 0; 5654 5655 if (file->f_mode & FMODE_READ) { 5656 struct seq_file *m = file->private_data; 5657 iter = m->private; 5658 } else 5659 iter = file->private_data; 5660 5661 if (unlikely(ftrace_disabled)) 5662 return -ENODEV; 5663 5664 /* iter->hash is a local copy, so we don't need regex_lock */ 5665 5666 parser = &iter->parser; 5667 read = trace_get_user(parser, ubuf, cnt, ppos); 5668 5669 if (read >= 0 && trace_parser_loaded(parser) && 5670 !trace_parser_cont(parser)) { 5671 ret = ftrace_process_regex(iter, parser->buffer, 5672 parser->idx, enable); 5673 trace_parser_clear(parser); 5674 if (ret < 0) 5675 return ret; 5676 } 5677 5678 return read; 5679 } 5680 5681 ssize_t 5682 ftrace_filter_write(struct file *file, const char __user *ubuf, 5683 size_t cnt, loff_t *ppos) 5684 { 5685 return ftrace_regex_write(file, ubuf, cnt, ppos, 1); 5686 } 5687 5688 ssize_t 5689 ftrace_notrace_write(struct file *file, const char __user *ubuf, 5690 size_t cnt, loff_t *ppos) 5691 { 5692 return ftrace_regex_write(file, ubuf, cnt, ppos, 0); 5693 } 5694 5695 static int 5696 __ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove) 5697 { 5698 struct ftrace_func_entry *entry; 5699 5700 ip = ftrace_location(ip); 5701 if (!ip) 5702 return -EINVAL; 5703 5704 if (remove) { 5705 entry = ftrace_lookup_ip(hash, ip); 5706 if (!entry) 5707 return -ENOENT; 5708 free_hash_entry(hash, entry); 5709 return 0; 5710 } 5711 5712 entry = add_hash_entry(hash, ip); 5713 return entry ? 0 : -ENOMEM; 5714 } 5715 5716 static int 5717 ftrace_match_addr(struct ftrace_hash *hash, unsigned long *ips, 5718 unsigned int cnt, int remove) 5719 { 5720 unsigned int i; 5721 int err; 5722 5723 for (i = 0; i < cnt; i++) { 5724 err = __ftrace_match_addr(hash, ips[i], remove); 5725 if (err) { 5726 /* 5727 * This expects the @hash is a temporary hash and if this 5728 * fails the caller must free the @hash. 5729 */ 5730 return err; 5731 } 5732 } 5733 return 0; 5734 } 5735 5736 static int 5737 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len, 5738 unsigned long *ips, unsigned int cnt, 5739 int remove, int reset, int enable, char *mod) 5740 { 5741 struct ftrace_hash **orig_hash; 5742 struct ftrace_hash *hash; 5743 int ret; 5744 5745 if (unlikely(ftrace_disabled)) 5746 return -ENODEV; 5747 5748 mutex_lock(&ops->func_hash->regex_lock); 5749 5750 if (enable) 5751 orig_hash = &ops->func_hash->filter_hash; 5752 else 5753 orig_hash = &ops->func_hash->notrace_hash; 5754 5755 if (reset) 5756 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS); 5757 else 5758 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash); 5759 5760 if (!hash) { 5761 ret = -ENOMEM; 5762 goto out_regex_unlock; 5763 } 5764 5765 if (buf && !match_records(hash, buf, len, mod)) { 5766 /* If this was for a module and nothing was enabled, flag it */ 5767 if (mod) 5768 (*orig_hash)->flags |= FTRACE_HASH_FL_MOD; 5769 5770 /* 5771 * Even if it is a mod, return error to let caller know 5772 * nothing was added 5773 */ 5774 ret = -EINVAL; 5775 goto out_regex_unlock; 5776 } 5777 if (ips) { 5778 ret = ftrace_match_addr(hash, ips, cnt, remove); 5779 if (ret < 0) 5780 goto out_regex_unlock; 5781 } 5782 5783 mutex_lock(&ftrace_lock); 5784 ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable); 5785 mutex_unlock(&ftrace_lock); 5786 5787 out_regex_unlock: 5788 mutex_unlock(&ops->func_hash->regex_lock); 5789 5790 free_ftrace_hash(hash); 5791 return ret; 5792 } 5793 5794 static int 5795 ftrace_set_addr(struct ftrace_ops *ops, unsigned long *ips, unsigned int cnt, 5796 int remove, int reset, int enable) 5797 { 5798 return ftrace_set_hash(ops, NULL, 0, ips, cnt, remove, reset, enable, NULL); 5799 } 5800 5801 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS 5802 5803 static int register_ftrace_function_nolock(struct ftrace_ops *ops); 5804 5805 /* 5806 * If there are multiple ftrace_ops, use SAVE_REGS by default, so that direct 5807 * call will be jumped from ftrace_regs_caller. Only if the architecture does 5808 * not support ftrace_regs_caller but direct_call, use SAVE_ARGS so that it 5809 * jumps from ftrace_caller for multiple ftrace_ops. 5810 */ 5811 #ifndef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_REGS 5812 #define MULTI_FLAGS (FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_ARGS) 5813 #else 5814 #define MULTI_FLAGS (FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_REGS) 5815 #endif 5816 5817 static int check_direct_multi(struct ftrace_ops *ops) 5818 { 5819 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) 5820 return -EINVAL; 5821 if ((ops->flags & MULTI_FLAGS) != MULTI_FLAGS) 5822 return -EINVAL; 5823 return 0; 5824 } 5825 5826 static void remove_direct_functions_hash(struct ftrace_hash *hash, unsigned long addr) 5827 { 5828 struct ftrace_func_entry *entry, *del; 5829 int size, i; 5830 5831 size = 1 << hash->size_bits; 5832 for (i = 0; i < size; i++) { 5833 hlist_for_each_entry(entry, &hash->buckets[i], hlist) { 5834 del = __ftrace_lookup_ip(direct_functions, entry->ip); 5835 if (del && del->direct == addr) { 5836 remove_hash_entry(direct_functions, del); 5837 kfree(del); 5838 } 5839 } 5840 } 5841 } 5842 5843 static void register_ftrace_direct_cb(struct rcu_head *rhp) 5844 { 5845 struct ftrace_hash *fhp = container_of(rhp, struct ftrace_hash, rcu); 5846 5847 free_ftrace_hash(fhp); 5848 } 5849 5850 /** 5851 * register_ftrace_direct - Call a custom trampoline directly 5852 * for multiple functions registered in @ops 5853 * @ops: The address of the struct ftrace_ops object 5854 * @addr: The address of the trampoline to call at @ops functions 5855 * 5856 * This is used to connect a direct calls to @addr from the nop locations 5857 * of the functions registered in @ops (with by ftrace_set_filter_ip 5858 * function). 5859 * 5860 * The location that it calls (@addr) must be able to handle a direct call, 5861 * and save the parameters of the function being traced, and restore them 5862 * (or inject new ones if needed), before returning. 5863 * 5864 * Returns: 5865 * 0 on success 5866 * -EINVAL - The @ops object was already registered with this call or 5867 * when there are no functions in @ops object. 5868 * -EBUSY - Another direct function is already attached (there can be only one) 5869 * -ENODEV - @ip does not point to a ftrace nop location (or not supported) 5870 * -ENOMEM - There was an allocation failure. 5871 */ 5872 int register_ftrace_direct(struct ftrace_ops *ops, unsigned long addr) 5873 { 5874 struct ftrace_hash *hash, *new_hash = NULL, *free_hash = NULL; 5875 struct ftrace_func_entry *entry, *new; 5876 int err = -EBUSY, size, i; 5877 5878 if (ops->func || ops->trampoline) 5879 return -EINVAL; 5880 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) 5881 return -EINVAL; 5882 if (ops->flags & FTRACE_OPS_FL_ENABLED) 5883 return -EINVAL; 5884 5885 hash = ops->func_hash->filter_hash; 5886 if (ftrace_hash_empty(hash)) 5887 return -EINVAL; 5888 5889 mutex_lock(&direct_mutex); 5890 5891 /* Make sure requested entries are not already registered.. */ 5892 size = 1 << hash->size_bits; 5893 for (i = 0; i < size; i++) { 5894 hlist_for_each_entry(entry, &hash->buckets[i], hlist) { 5895 if (ftrace_find_rec_direct(entry->ip)) 5896 goto out_unlock; 5897 } 5898 } 5899 5900 err = -ENOMEM; 5901 5902 /* Make a copy hash to place the new and the old entries in */ 5903 size = hash->count + direct_functions->count; 5904 if (size > 32) 5905 size = 32; 5906 new_hash = alloc_ftrace_hash(fls(size)); 5907 if (!new_hash) 5908 goto out_unlock; 5909 5910 /* Now copy over the existing direct entries */ 5911 size = 1 << direct_functions->size_bits; 5912 for (i = 0; i < size; i++) { 5913 hlist_for_each_entry(entry, &direct_functions->buckets[i], hlist) { 5914 new = add_hash_entry(new_hash, entry->ip); 5915 if (!new) 5916 goto out_unlock; 5917 new->direct = entry->direct; 5918 } 5919 } 5920 5921 /* ... and add the new entries */ 5922 size = 1 << hash->size_bits; 5923 for (i = 0; i < size; i++) { 5924 hlist_for_each_entry(entry, &hash->buckets[i], hlist) { 5925 new = add_hash_entry(new_hash, entry->ip); 5926 if (!new) 5927 goto out_unlock; 5928 /* Update both the copy and the hash entry */ 5929 new->direct = addr; 5930 entry->direct = addr; 5931 } 5932 } 5933 5934 free_hash = direct_functions; 5935 rcu_assign_pointer(direct_functions, new_hash); 5936 new_hash = NULL; 5937 5938 ops->func = call_direct_funcs; 5939 ops->flags = MULTI_FLAGS; 5940 ops->trampoline = FTRACE_REGS_ADDR; 5941 ops->direct_call = addr; 5942 5943 err = register_ftrace_function_nolock(ops); 5944 5945 out_unlock: 5946 mutex_unlock(&direct_mutex); 5947 5948 if (free_hash && free_hash != EMPTY_HASH) 5949 call_rcu_tasks(&free_hash->rcu, register_ftrace_direct_cb); 5950 5951 if (new_hash) 5952 free_ftrace_hash(new_hash); 5953 5954 return err; 5955 } 5956 EXPORT_SYMBOL_GPL(register_ftrace_direct); 5957 5958 /** 5959 * unregister_ftrace_direct - Remove calls to custom trampoline 5960 * previously registered by register_ftrace_direct for @ops object. 5961 * @ops: The address of the struct ftrace_ops object 5962 * @addr: The address of the direct function that is called by the @ops functions 5963 * @free_filters: Set to true to remove all filters for the ftrace_ops, false otherwise 5964 * 5965 * This is used to remove a direct calls to @addr from the nop locations 5966 * of the functions registered in @ops (with by ftrace_set_filter_ip 5967 * function). 5968 * 5969 * Returns: 5970 * 0 on success 5971 * -EINVAL - The @ops object was not properly registered. 5972 */ 5973 int unregister_ftrace_direct(struct ftrace_ops *ops, unsigned long addr, 5974 bool free_filters) 5975 { 5976 struct ftrace_hash *hash = ops->func_hash->filter_hash; 5977 int err; 5978 5979 if (check_direct_multi(ops)) 5980 return -EINVAL; 5981 if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) 5982 return -EINVAL; 5983 5984 mutex_lock(&direct_mutex); 5985 err = unregister_ftrace_function(ops); 5986 remove_direct_functions_hash(hash, addr); 5987 mutex_unlock(&direct_mutex); 5988 5989 /* cleanup for possible another register call */ 5990 ops->func = NULL; 5991 ops->trampoline = 0; 5992 5993 if (free_filters) 5994 ftrace_free_filter(ops); 5995 return err; 5996 } 5997 EXPORT_SYMBOL_GPL(unregister_ftrace_direct); 5998 5999 static int 6000 __modify_ftrace_direct(struct ftrace_ops *ops, unsigned long addr) 6001 { 6002 struct ftrace_hash *hash; 6003 struct ftrace_func_entry *entry, *iter; 6004 static struct ftrace_ops tmp_ops = { 6005 .func = ftrace_stub, 6006 .flags = FTRACE_OPS_FL_STUB, 6007 }; 6008 int i, size; 6009 int err; 6010 6011 lockdep_assert_held_once(&direct_mutex); 6012 6013 /* Enable the tmp_ops to have the same functions as the direct ops */ 6014 ftrace_ops_init(&tmp_ops); 6015 tmp_ops.func_hash = ops->func_hash; 6016 tmp_ops.direct_call = addr; 6017 6018 err = register_ftrace_function_nolock(&tmp_ops); 6019 if (err) 6020 return err; 6021 6022 /* 6023 * Now the ftrace_ops_list_func() is called to do the direct callers. 6024 * We can safely change the direct functions attached to each entry. 6025 */ 6026 mutex_lock(&ftrace_lock); 6027 6028 hash = ops->func_hash->filter_hash; 6029 size = 1 << hash->size_bits; 6030 for (i = 0; i < size; i++) { 6031 hlist_for_each_entry(iter, &hash->buckets[i], hlist) { 6032 entry = __ftrace_lookup_ip(direct_functions, iter->ip); 6033 if (!entry) 6034 continue; 6035 entry->direct = addr; 6036 } 6037 } 6038 /* Prevent store tearing if a trampoline concurrently accesses the value */ 6039 WRITE_ONCE(ops->direct_call, addr); 6040 6041 mutex_unlock(&ftrace_lock); 6042 6043 /* Removing the tmp_ops will add the updated direct callers to the functions */ 6044 unregister_ftrace_function(&tmp_ops); 6045 6046 return err; 6047 } 6048 6049 /** 6050 * modify_ftrace_direct_nolock - Modify an existing direct 'multi' call 6051 * to call something else 6052 * @ops: The address of the struct ftrace_ops object 6053 * @addr: The address of the new trampoline to call at @ops functions 6054 * 6055 * This is used to unregister currently registered direct caller and 6056 * register new one @addr on functions registered in @ops object. 6057 * 6058 * Note there's window between ftrace_shutdown and ftrace_startup calls 6059 * where there will be no callbacks called. 6060 * 6061 * Caller should already have direct_mutex locked, so we don't lock 6062 * direct_mutex here. 6063 * 6064 * Returns: zero on success. Non zero on error, which includes: 6065 * -EINVAL - The @ops object was not properly registered. 6066 */ 6067 int modify_ftrace_direct_nolock(struct ftrace_ops *ops, unsigned long addr) 6068 { 6069 if (check_direct_multi(ops)) 6070 return -EINVAL; 6071 if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) 6072 return -EINVAL; 6073 6074 return __modify_ftrace_direct(ops, addr); 6075 } 6076 EXPORT_SYMBOL_GPL(modify_ftrace_direct_nolock); 6077 6078 /** 6079 * modify_ftrace_direct - Modify an existing direct 'multi' call 6080 * to call something else 6081 * @ops: The address of the struct ftrace_ops object 6082 * @addr: The address of the new trampoline to call at @ops functions 6083 * 6084 * This is used to unregister currently registered direct caller and 6085 * register new one @addr on functions registered in @ops object. 6086 * 6087 * Note there's window between ftrace_shutdown and ftrace_startup calls 6088 * where there will be no callbacks called. 6089 * 6090 * Returns: zero on success. Non zero on error, which includes: 6091 * -EINVAL - The @ops object was not properly registered. 6092 */ 6093 int modify_ftrace_direct(struct ftrace_ops *ops, unsigned long addr) 6094 { 6095 int err; 6096 6097 if (check_direct_multi(ops)) 6098 return -EINVAL; 6099 if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) 6100 return -EINVAL; 6101 6102 mutex_lock(&direct_mutex); 6103 err = __modify_ftrace_direct(ops, addr); 6104 mutex_unlock(&direct_mutex); 6105 return err; 6106 } 6107 EXPORT_SYMBOL_GPL(modify_ftrace_direct); 6108 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */ 6109 6110 /** 6111 * ftrace_set_filter_ip - set a function to filter on in ftrace by address 6112 * @ops: the ops to set the filter with 6113 * @ip: the address to add to or remove from the filter. 6114 * @remove: non zero to remove the ip from the filter 6115 * @reset: non zero to reset all filters before applying this filter. 6116 * 6117 * Filters denote which functions should be enabled when tracing is enabled 6118 * If @ip is NULL, it fails to update filter. 6119 * 6120 * This can allocate memory which must be freed before @ops can be freed, 6121 * either by removing each filtered addr or by using 6122 * ftrace_free_filter(@ops). 6123 */ 6124 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip, 6125 int remove, int reset) 6126 { 6127 ftrace_ops_init(ops); 6128 return ftrace_set_addr(ops, &ip, 1, remove, reset, 1); 6129 } 6130 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip); 6131 6132 /** 6133 * ftrace_set_filter_ips - set functions to filter on in ftrace by addresses 6134 * @ops: the ops to set the filter with 6135 * @ips: the array of addresses to add to or remove from the filter. 6136 * @cnt: the number of addresses in @ips 6137 * @remove: non zero to remove ips from the filter 6138 * @reset: non zero to reset all filters before applying this filter. 6139 * 6140 * Filters denote which functions should be enabled when tracing is enabled 6141 * If @ips array or any ip specified within is NULL , it fails to update filter. 6142 * 6143 * This can allocate memory which must be freed before @ops can be freed, 6144 * either by removing each filtered addr or by using 6145 * ftrace_free_filter(@ops). 6146 */ 6147 int ftrace_set_filter_ips(struct ftrace_ops *ops, unsigned long *ips, 6148 unsigned int cnt, int remove, int reset) 6149 { 6150 ftrace_ops_init(ops); 6151 return ftrace_set_addr(ops, ips, cnt, remove, reset, 1); 6152 } 6153 EXPORT_SYMBOL_GPL(ftrace_set_filter_ips); 6154 6155 /** 6156 * ftrace_ops_set_global_filter - setup ops to use global filters 6157 * @ops: the ops which will use the global filters 6158 * 6159 * ftrace users who need global function trace filtering should call this. 6160 * It can set the global filter only if ops were not initialized before. 6161 */ 6162 void ftrace_ops_set_global_filter(struct ftrace_ops *ops) 6163 { 6164 if (ops->flags & FTRACE_OPS_FL_INITIALIZED) 6165 return; 6166 6167 ftrace_ops_init(ops); 6168 ops->func_hash = &global_ops.local_hash; 6169 } 6170 EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter); 6171 6172 static int 6173 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len, 6174 int reset, int enable) 6175 { 6176 char *mod = NULL, *func, *command, *next = buf; 6177 char *tmp __free(kfree) = NULL; 6178 struct trace_array *tr = ops->private; 6179 int ret; 6180 6181 func = strsep(&next, ":"); 6182 6183 /* This can also handle :mod: parsing */ 6184 if (next) { 6185 if (!tr) 6186 return -EINVAL; 6187 6188 command = strsep(&next, ":"); 6189 if (strcmp(command, "mod") != 0) 6190 return -EINVAL; 6191 6192 mod = next; 6193 len = command - func; 6194 /* Save the original func as ftrace_set_hash() can modify it */ 6195 tmp = kstrdup(func, GFP_KERNEL); 6196 } 6197 6198 ret = ftrace_set_hash(ops, func, len, NULL, 0, 0, reset, enable, mod); 6199 6200 if (tr && mod && ret < 0) { 6201 /* Did tmp fail to allocate? */ 6202 if (!tmp) 6203 return -ENOMEM; 6204 ret = cache_mod(tr, tmp, mod, enable); 6205 } 6206 6207 return ret; 6208 } 6209 6210 /** 6211 * ftrace_set_filter - set a function to filter on in ftrace 6212 * @ops: the ops to set the filter with 6213 * @buf: the string that holds the function filter text. 6214 * @len: the length of the string. 6215 * @reset: non-zero to reset all filters before applying this filter. 6216 * 6217 * Filters denote which functions should be enabled when tracing is enabled. 6218 * If @buf is NULL and reset is set, all functions will be enabled for tracing. 6219 * 6220 * This can allocate memory which must be freed before @ops can be freed, 6221 * either by removing each filtered addr or by using 6222 * ftrace_free_filter(@ops). 6223 */ 6224 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf, 6225 int len, int reset) 6226 { 6227 ftrace_ops_init(ops); 6228 return ftrace_set_regex(ops, buf, len, reset, 1); 6229 } 6230 EXPORT_SYMBOL_GPL(ftrace_set_filter); 6231 6232 /** 6233 * ftrace_set_notrace - set a function to not trace in ftrace 6234 * @ops: the ops to set the notrace filter with 6235 * @buf: the string that holds the function notrace text. 6236 * @len: the length of the string. 6237 * @reset: non-zero to reset all filters before applying this filter. 6238 * 6239 * Notrace Filters denote which functions should not be enabled when tracing 6240 * is enabled. If @buf is NULL and reset is set, all functions will be enabled 6241 * for tracing. 6242 * 6243 * This can allocate memory which must be freed before @ops can be freed, 6244 * either by removing each filtered addr or by using 6245 * ftrace_free_filter(@ops). 6246 */ 6247 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf, 6248 int len, int reset) 6249 { 6250 ftrace_ops_init(ops); 6251 return ftrace_set_regex(ops, buf, len, reset, 0); 6252 } 6253 EXPORT_SYMBOL_GPL(ftrace_set_notrace); 6254 /** 6255 * ftrace_set_global_filter - set a function to filter on with global tracers 6256 * @buf: the string that holds the function filter text. 6257 * @len: the length of the string. 6258 * @reset: non-zero to reset all filters before applying this filter. 6259 * 6260 * Filters denote which functions should be enabled when tracing is enabled. 6261 * If @buf is NULL and reset is set, all functions will be enabled for tracing. 6262 */ 6263 void ftrace_set_global_filter(unsigned char *buf, int len, int reset) 6264 { 6265 ftrace_set_regex(&global_ops, buf, len, reset, 1); 6266 } 6267 EXPORT_SYMBOL_GPL(ftrace_set_global_filter); 6268 6269 /** 6270 * ftrace_set_global_notrace - set a function to not trace with global tracers 6271 * @buf: the string that holds the function notrace text. 6272 * @len: the length of the string. 6273 * @reset: non-zero to reset all filters before applying this filter. 6274 * 6275 * Notrace Filters denote which functions should not be enabled when tracing 6276 * is enabled. If @buf is NULL and reset is set, all functions will be enabled 6277 * for tracing. 6278 */ 6279 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset) 6280 { 6281 ftrace_set_regex(&global_ops, buf, len, reset, 0); 6282 } 6283 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace); 6284 6285 /* 6286 * command line interface to allow users to set filters on boot up. 6287 */ 6288 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE 6289 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata; 6290 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata; 6291 6292 /* Used by function selftest to not test if filter is set */ 6293 bool ftrace_filter_param __initdata; 6294 6295 static int __init set_ftrace_notrace(char *str) 6296 { 6297 ftrace_filter_param = true; 6298 strscpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE); 6299 return 1; 6300 } 6301 __setup("ftrace_notrace=", set_ftrace_notrace); 6302 6303 static int __init set_ftrace_filter(char *str) 6304 { 6305 ftrace_filter_param = true; 6306 strscpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE); 6307 return 1; 6308 } 6309 __setup("ftrace_filter=", set_ftrace_filter); 6310 6311 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 6312 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata; 6313 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata; 6314 static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer); 6315 6316 static int __init set_graph_function(char *str) 6317 { 6318 strscpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE); 6319 return 1; 6320 } 6321 __setup("ftrace_graph_filter=", set_graph_function); 6322 6323 static int __init set_graph_notrace_function(char *str) 6324 { 6325 strscpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE); 6326 return 1; 6327 } 6328 __setup("ftrace_graph_notrace=", set_graph_notrace_function); 6329 6330 static int __init set_graph_max_depth_function(char *str) 6331 { 6332 if (!str || kstrtouint(str, 0, &fgraph_max_depth)) 6333 return 0; 6334 return 1; 6335 } 6336 __setup("ftrace_graph_max_depth=", set_graph_max_depth_function); 6337 6338 static void __init set_ftrace_early_graph(char *buf, int enable) 6339 { 6340 int ret; 6341 char *func; 6342 struct ftrace_hash *hash; 6343 6344 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS); 6345 if (MEM_FAIL(!hash, "Failed to allocate hash\n")) 6346 return; 6347 6348 while (buf) { 6349 func = strsep(&buf, ","); 6350 /* we allow only one expression at a time */ 6351 ret = ftrace_graph_set_hash(hash, func); 6352 if (ret) 6353 printk(KERN_DEBUG "ftrace: function %s not " 6354 "traceable\n", func); 6355 } 6356 6357 if (enable) 6358 ftrace_graph_hash = hash; 6359 else 6360 ftrace_graph_notrace_hash = hash; 6361 } 6362 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ 6363 6364 void __init 6365 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable) 6366 { 6367 char *func; 6368 6369 ftrace_ops_init(ops); 6370 6371 /* The trace_array is needed for caching module function filters */ 6372 if (!ops->private) { 6373 struct trace_array *tr = trace_get_global_array(); 6374 6375 ops->private = tr; 6376 ftrace_init_trace_array(tr); 6377 } 6378 6379 while (buf) { 6380 func = strsep(&buf, ","); 6381 ftrace_set_regex(ops, func, strlen(func), 0, enable); 6382 } 6383 } 6384 6385 static void __init set_ftrace_early_filters(void) 6386 { 6387 if (ftrace_filter_buf[0]) 6388 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1); 6389 if (ftrace_notrace_buf[0]) 6390 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0); 6391 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 6392 if (ftrace_graph_buf[0]) 6393 set_ftrace_early_graph(ftrace_graph_buf, 1); 6394 if (ftrace_graph_notrace_buf[0]) 6395 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0); 6396 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ 6397 } 6398 6399 int ftrace_regex_release(struct inode *inode, struct file *file) 6400 { 6401 struct seq_file *m = (struct seq_file *)file->private_data; 6402 struct ftrace_iterator *iter; 6403 struct ftrace_hash **orig_hash; 6404 struct trace_parser *parser; 6405 int filter_hash; 6406 6407 if (file->f_mode & FMODE_READ) { 6408 iter = m->private; 6409 seq_release(inode, file); 6410 } else 6411 iter = file->private_data; 6412 6413 parser = &iter->parser; 6414 if (trace_parser_loaded(parser)) { 6415 int enable = !(iter->flags & FTRACE_ITER_NOTRACE); 6416 6417 ftrace_process_regex(iter, parser->buffer, 6418 parser->idx, enable); 6419 } 6420 6421 trace_parser_put(parser); 6422 6423 mutex_lock(&iter->ops->func_hash->regex_lock); 6424 6425 if (file->f_mode & FMODE_WRITE) { 6426 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER); 6427 6428 if (filter_hash) { 6429 orig_hash = &iter->ops->func_hash->filter_hash; 6430 if (iter->tr) { 6431 if (list_empty(&iter->tr->mod_trace)) 6432 iter->hash->flags &= ~FTRACE_HASH_FL_MOD; 6433 else 6434 iter->hash->flags |= FTRACE_HASH_FL_MOD; 6435 } 6436 } else 6437 orig_hash = &iter->ops->func_hash->notrace_hash; 6438 6439 mutex_lock(&ftrace_lock); 6440 ftrace_hash_move_and_update_ops(iter->ops, orig_hash, 6441 iter->hash, filter_hash); 6442 mutex_unlock(&ftrace_lock); 6443 } else { 6444 /* For read only, the hash is the ops hash */ 6445 iter->hash = NULL; 6446 } 6447 6448 mutex_unlock(&iter->ops->func_hash->regex_lock); 6449 free_ftrace_hash(iter->hash); 6450 if (iter->tr) 6451 trace_array_put(iter->tr); 6452 kfree(iter); 6453 6454 return 0; 6455 } 6456 6457 static const struct file_operations ftrace_avail_fops = { 6458 .open = ftrace_avail_open, 6459 .read = seq_read, 6460 .llseek = seq_lseek, 6461 .release = seq_release_private, 6462 }; 6463 6464 static const struct file_operations ftrace_enabled_fops = { 6465 .open = ftrace_enabled_open, 6466 .read = seq_read, 6467 .llseek = seq_lseek, 6468 .release = seq_release_private, 6469 }; 6470 6471 static const struct file_operations ftrace_touched_fops = { 6472 .open = ftrace_touched_open, 6473 .read = seq_read, 6474 .llseek = seq_lseek, 6475 .release = seq_release_private, 6476 }; 6477 6478 static const struct file_operations ftrace_avail_addrs_fops = { 6479 .open = ftrace_avail_addrs_open, 6480 .read = seq_read, 6481 .llseek = seq_lseek, 6482 .release = seq_release_private, 6483 }; 6484 6485 static const struct file_operations ftrace_filter_fops = { 6486 .open = ftrace_filter_open, 6487 .read = seq_read, 6488 .write = ftrace_filter_write, 6489 .llseek = tracing_lseek, 6490 .release = ftrace_regex_release, 6491 }; 6492 6493 static const struct file_operations ftrace_notrace_fops = { 6494 .open = ftrace_notrace_open, 6495 .read = seq_read, 6496 .write = ftrace_notrace_write, 6497 .llseek = tracing_lseek, 6498 .release = ftrace_regex_release, 6499 }; 6500 6501 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 6502 6503 static DEFINE_MUTEX(graph_lock); 6504 6505 struct ftrace_hash __rcu *ftrace_graph_hash = EMPTY_HASH; 6506 struct ftrace_hash __rcu *ftrace_graph_notrace_hash = EMPTY_HASH; 6507 6508 enum graph_filter_type { 6509 GRAPH_FILTER_NOTRACE = 0, 6510 GRAPH_FILTER_FUNCTION, 6511 }; 6512 6513 #define FTRACE_GRAPH_EMPTY ((void *)1) 6514 6515 struct ftrace_graph_data { 6516 struct ftrace_hash *hash; 6517 struct ftrace_func_entry *entry; 6518 int idx; /* for hash table iteration */ 6519 enum graph_filter_type type; 6520 struct ftrace_hash *new_hash; 6521 const struct seq_operations *seq_ops; 6522 struct trace_parser parser; 6523 }; 6524 6525 static void * 6526 __g_next(struct seq_file *m, loff_t *pos) 6527 { 6528 struct ftrace_graph_data *fgd = m->private; 6529 struct ftrace_func_entry *entry = fgd->entry; 6530 struct hlist_head *head; 6531 int i, idx = fgd->idx; 6532 6533 if (*pos >= fgd->hash->count) 6534 return NULL; 6535 6536 if (entry) { 6537 hlist_for_each_entry_continue(entry, hlist) { 6538 fgd->entry = entry; 6539 return entry; 6540 } 6541 6542 idx++; 6543 } 6544 6545 for (i = idx; i < 1 << fgd->hash->size_bits; i++) { 6546 head = &fgd->hash->buckets[i]; 6547 hlist_for_each_entry(entry, head, hlist) { 6548 fgd->entry = entry; 6549 fgd->idx = i; 6550 return entry; 6551 } 6552 } 6553 return NULL; 6554 } 6555 6556 static void * 6557 g_next(struct seq_file *m, void *v, loff_t *pos) 6558 { 6559 (*pos)++; 6560 return __g_next(m, pos); 6561 } 6562 6563 static void *g_start(struct seq_file *m, loff_t *pos) 6564 { 6565 struct ftrace_graph_data *fgd = m->private; 6566 6567 mutex_lock(&graph_lock); 6568 6569 if (fgd->type == GRAPH_FILTER_FUNCTION) 6570 fgd->hash = rcu_dereference_protected(ftrace_graph_hash, 6571 lockdep_is_held(&graph_lock)); 6572 else 6573 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash, 6574 lockdep_is_held(&graph_lock)); 6575 6576 /* Nothing, tell g_show to print all functions are enabled */ 6577 if (ftrace_hash_empty(fgd->hash) && !*pos) 6578 return FTRACE_GRAPH_EMPTY; 6579 6580 fgd->idx = 0; 6581 fgd->entry = NULL; 6582 return __g_next(m, pos); 6583 } 6584 6585 static void g_stop(struct seq_file *m, void *p) 6586 { 6587 mutex_unlock(&graph_lock); 6588 } 6589 6590 static int g_show(struct seq_file *m, void *v) 6591 { 6592 struct ftrace_func_entry *entry = v; 6593 6594 if (!entry) 6595 return 0; 6596 6597 if (entry == FTRACE_GRAPH_EMPTY) { 6598 struct ftrace_graph_data *fgd = m->private; 6599 6600 if (fgd->type == GRAPH_FILTER_FUNCTION) 6601 seq_puts(m, "#### all functions enabled ####\n"); 6602 else 6603 seq_puts(m, "#### no functions disabled ####\n"); 6604 return 0; 6605 } 6606 6607 seq_printf(m, "%ps\n", (void *)entry->ip); 6608 6609 return 0; 6610 } 6611 6612 static const struct seq_operations ftrace_graph_seq_ops = { 6613 .start = g_start, 6614 .next = g_next, 6615 .stop = g_stop, 6616 .show = g_show, 6617 }; 6618 6619 static int 6620 __ftrace_graph_open(struct inode *inode, struct file *file, 6621 struct ftrace_graph_data *fgd) 6622 { 6623 int ret; 6624 struct ftrace_hash *new_hash = NULL; 6625 6626 ret = security_locked_down(LOCKDOWN_TRACEFS); 6627 if (ret) 6628 return ret; 6629 6630 if (file->f_mode & FMODE_WRITE) { 6631 const int size_bits = FTRACE_HASH_DEFAULT_BITS; 6632 6633 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX)) 6634 return -ENOMEM; 6635 6636 if (file->f_flags & O_TRUNC) 6637 new_hash = alloc_ftrace_hash(size_bits); 6638 else 6639 new_hash = alloc_and_copy_ftrace_hash(size_bits, 6640 fgd->hash); 6641 if (!new_hash) { 6642 ret = -ENOMEM; 6643 goto out; 6644 } 6645 } 6646 6647 if (file->f_mode & FMODE_READ) { 6648 ret = seq_open(file, &ftrace_graph_seq_ops); 6649 if (!ret) { 6650 struct seq_file *m = file->private_data; 6651 m->private = fgd; 6652 } else { 6653 /* Failed */ 6654 free_ftrace_hash(new_hash); 6655 new_hash = NULL; 6656 } 6657 } else 6658 file->private_data = fgd; 6659 6660 out: 6661 if (ret < 0 && file->f_mode & FMODE_WRITE) 6662 trace_parser_put(&fgd->parser); 6663 6664 fgd->new_hash = new_hash; 6665 6666 /* 6667 * All uses of fgd->hash must be taken with the graph_lock 6668 * held. The graph_lock is going to be released, so force 6669 * fgd->hash to be reinitialized when it is taken again. 6670 */ 6671 fgd->hash = NULL; 6672 6673 return ret; 6674 } 6675 6676 static int 6677 ftrace_graph_open(struct inode *inode, struct file *file) 6678 { 6679 struct ftrace_graph_data *fgd; 6680 int ret; 6681 6682 if (unlikely(ftrace_disabled)) 6683 return -ENODEV; 6684 6685 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL); 6686 if (fgd == NULL) 6687 return -ENOMEM; 6688 6689 mutex_lock(&graph_lock); 6690 6691 fgd->hash = rcu_dereference_protected(ftrace_graph_hash, 6692 lockdep_is_held(&graph_lock)); 6693 fgd->type = GRAPH_FILTER_FUNCTION; 6694 fgd->seq_ops = &ftrace_graph_seq_ops; 6695 6696 ret = __ftrace_graph_open(inode, file, fgd); 6697 if (ret < 0) 6698 kfree(fgd); 6699 6700 mutex_unlock(&graph_lock); 6701 return ret; 6702 } 6703 6704 static int 6705 ftrace_graph_notrace_open(struct inode *inode, struct file *file) 6706 { 6707 struct ftrace_graph_data *fgd; 6708 int ret; 6709 6710 if (unlikely(ftrace_disabled)) 6711 return -ENODEV; 6712 6713 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL); 6714 if (fgd == NULL) 6715 return -ENOMEM; 6716 6717 mutex_lock(&graph_lock); 6718 6719 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash, 6720 lockdep_is_held(&graph_lock)); 6721 fgd->type = GRAPH_FILTER_NOTRACE; 6722 fgd->seq_ops = &ftrace_graph_seq_ops; 6723 6724 ret = __ftrace_graph_open(inode, file, fgd); 6725 if (ret < 0) 6726 kfree(fgd); 6727 6728 mutex_unlock(&graph_lock); 6729 return ret; 6730 } 6731 6732 static int 6733 ftrace_graph_release(struct inode *inode, struct file *file) 6734 { 6735 struct ftrace_graph_data *fgd; 6736 struct ftrace_hash *old_hash, *new_hash; 6737 struct trace_parser *parser; 6738 int ret = 0; 6739 6740 if (file->f_mode & FMODE_READ) { 6741 struct seq_file *m = file->private_data; 6742 6743 fgd = m->private; 6744 seq_release(inode, file); 6745 } else { 6746 fgd = file->private_data; 6747 } 6748 6749 6750 if (file->f_mode & FMODE_WRITE) { 6751 6752 parser = &fgd->parser; 6753 6754 if (trace_parser_loaded((parser))) { 6755 ret = ftrace_graph_set_hash(fgd->new_hash, 6756 parser->buffer); 6757 } 6758 6759 trace_parser_put(parser); 6760 6761 new_hash = __ftrace_hash_move(fgd->new_hash); 6762 if (!new_hash) { 6763 ret = -ENOMEM; 6764 goto out; 6765 } 6766 6767 mutex_lock(&graph_lock); 6768 6769 if (fgd->type == GRAPH_FILTER_FUNCTION) { 6770 old_hash = rcu_dereference_protected(ftrace_graph_hash, 6771 lockdep_is_held(&graph_lock)); 6772 rcu_assign_pointer(ftrace_graph_hash, new_hash); 6773 } else { 6774 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash, 6775 lockdep_is_held(&graph_lock)); 6776 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash); 6777 } 6778 6779 mutex_unlock(&graph_lock); 6780 6781 /* 6782 * We need to do a hard force of sched synchronization. 6783 * This is because we use preempt_disable() to do RCU, but 6784 * the function tracers can be called where RCU is not watching 6785 * (like before user_exit()). We can not rely on the RCU 6786 * infrastructure to do the synchronization, thus we must do it 6787 * ourselves. 6788 */ 6789 if (old_hash != EMPTY_HASH) 6790 synchronize_rcu_tasks_rude(); 6791 6792 free_ftrace_hash(old_hash); 6793 } 6794 6795 out: 6796 free_ftrace_hash(fgd->new_hash); 6797 kfree(fgd); 6798 6799 return ret; 6800 } 6801 6802 static int 6803 ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer) 6804 { 6805 struct ftrace_glob func_g; 6806 struct dyn_ftrace *rec; 6807 struct ftrace_page *pg; 6808 struct ftrace_func_entry *entry; 6809 int fail = 1; 6810 int not; 6811 6812 /* decode regex */ 6813 func_g.type = filter_parse_regex(buffer, strlen(buffer), 6814 &func_g.search, ¬); 6815 6816 func_g.len = strlen(func_g.search); 6817 6818 guard(mutex)(&ftrace_lock); 6819 6820 if (unlikely(ftrace_disabled)) 6821 return -ENODEV; 6822 6823 do_for_each_ftrace_rec(pg, rec) { 6824 6825 if (rec->flags & FTRACE_FL_DISABLED) 6826 continue; 6827 6828 if (ftrace_match_record(rec, &func_g, NULL, 0)) { 6829 entry = ftrace_lookup_ip(hash, rec->ip); 6830 6831 if (!not) { 6832 fail = 0; 6833 6834 if (entry) 6835 continue; 6836 if (add_hash_entry(hash, rec->ip) == NULL) 6837 return 0; 6838 } else { 6839 if (entry) { 6840 free_hash_entry(hash, entry); 6841 fail = 0; 6842 } 6843 } 6844 } 6845 } while_for_each_ftrace_rec(); 6846 6847 return fail ? -EINVAL : 0; 6848 } 6849 6850 static ssize_t 6851 ftrace_graph_write(struct file *file, const char __user *ubuf, 6852 size_t cnt, loff_t *ppos) 6853 { 6854 ssize_t read, ret = 0; 6855 struct ftrace_graph_data *fgd = file->private_data; 6856 struct trace_parser *parser; 6857 6858 if (!cnt) 6859 return 0; 6860 6861 /* Read mode uses seq functions */ 6862 if (file->f_mode & FMODE_READ) { 6863 struct seq_file *m = file->private_data; 6864 fgd = m->private; 6865 } 6866 6867 parser = &fgd->parser; 6868 6869 read = trace_get_user(parser, ubuf, cnt, ppos); 6870 6871 if (read >= 0 && trace_parser_loaded(parser) && 6872 !trace_parser_cont(parser)) { 6873 6874 ret = ftrace_graph_set_hash(fgd->new_hash, 6875 parser->buffer); 6876 trace_parser_clear(parser); 6877 } 6878 6879 if (!ret) 6880 ret = read; 6881 6882 return ret; 6883 } 6884 6885 static const struct file_operations ftrace_graph_fops = { 6886 .open = ftrace_graph_open, 6887 .read = seq_read, 6888 .write = ftrace_graph_write, 6889 .llseek = tracing_lseek, 6890 .release = ftrace_graph_release, 6891 }; 6892 6893 static const struct file_operations ftrace_graph_notrace_fops = { 6894 .open = ftrace_graph_notrace_open, 6895 .read = seq_read, 6896 .write = ftrace_graph_write, 6897 .llseek = tracing_lseek, 6898 .release = ftrace_graph_release, 6899 }; 6900 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ 6901 6902 void ftrace_create_filter_files(struct ftrace_ops *ops, 6903 struct dentry *parent) 6904 { 6905 6906 trace_create_file("set_ftrace_filter", TRACE_MODE_WRITE, parent, 6907 ops, &ftrace_filter_fops); 6908 6909 trace_create_file("set_ftrace_notrace", TRACE_MODE_WRITE, parent, 6910 ops, &ftrace_notrace_fops); 6911 } 6912 6913 /* 6914 * The name "destroy_filter_files" is really a misnomer. Although 6915 * in the future, it may actually delete the files, but this is 6916 * really intended to make sure the ops passed in are disabled 6917 * and that when this function returns, the caller is free to 6918 * free the ops. 6919 * 6920 * The "destroy" name is only to match the "create" name that this 6921 * should be paired with. 6922 */ 6923 void ftrace_destroy_filter_files(struct ftrace_ops *ops) 6924 { 6925 mutex_lock(&ftrace_lock); 6926 if (ops->flags & FTRACE_OPS_FL_ENABLED) 6927 ftrace_shutdown(ops, 0); 6928 ops->flags |= FTRACE_OPS_FL_DELETED; 6929 ftrace_free_filter(ops); 6930 mutex_unlock(&ftrace_lock); 6931 } 6932 6933 static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer) 6934 { 6935 6936 trace_create_file("available_filter_functions", TRACE_MODE_READ, 6937 d_tracer, NULL, &ftrace_avail_fops); 6938 6939 trace_create_file("available_filter_functions_addrs", TRACE_MODE_READ, 6940 d_tracer, NULL, &ftrace_avail_addrs_fops); 6941 6942 trace_create_file("enabled_functions", TRACE_MODE_READ, 6943 d_tracer, NULL, &ftrace_enabled_fops); 6944 6945 trace_create_file("touched_functions", TRACE_MODE_READ, 6946 d_tracer, NULL, &ftrace_touched_fops); 6947 6948 ftrace_create_filter_files(&global_ops, d_tracer); 6949 6950 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 6951 trace_create_file("set_graph_function", TRACE_MODE_WRITE, d_tracer, 6952 NULL, 6953 &ftrace_graph_fops); 6954 trace_create_file("set_graph_notrace", TRACE_MODE_WRITE, d_tracer, 6955 NULL, 6956 &ftrace_graph_notrace_fops); 6957 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ 6958 6959 return 0; 6960 } 6961 6962 static int ftrace_cmp_ips(const void *a, const void *b) 6963 { 6964 const unsigned long *ipa = a; 6965 const unsigned long *ipb = b; 6966 6967 if (*ipa > *ipb) 6968 return 1; 6969 if (*ipa < *ipb) 6970 return -1; 6971 return 0; 6972 } 6973 6974 #ifdef CONFIG_FTRACE_SORT_STARTUP_TEST 6975 static void test_is_sorted(unsigned long *start, unsigned long count) 6976 { 6977 int i; 6978 6979 for (i = 1; i < count; i++) { 6980 if (WARN(start[i - 1] > start[i], 6981 "[%d] %pS at %lx is not sorted with %pS at %lx\n", i, 6982 (void *)start[i - 1], start[i - 1], 6983 (void *)start[i], start[i])) 6984 break; 6985 } 6986 if (i == count) 6987 pr_info("ftrace section at %px sorted properly\n", start); 6988 } 6989 #else 6990 static void test_is_sorted(unsigned long *start, unsigned long count) 6991 { 6992 } 6993 #endif 6994 6995 static int ftrace_process_locs(struct module *mod, 6996 unsigned long *start, 6997 unsigned long *end) 6998 { 6999 struct ftrace_page *pg_unuse = NULL; 7000 struct ftrace_page *start_pg; 7001 struct ftrace_page *pg; 7002 struct dyn_ftrace *rec; 7003 unsigned long skipped = 0; 7004 unsigned long count; 7005 unsigned long *p; 7006 unsigned long addr; 7007 unsigned long flags = 0; /* Shut up gcc */ 7008 int ret = -ENOMEM; 7009 7010 count = end - start; 7011 7012 if (!count) 7013 return 0; 7014 7015 /* 7016 * Sorting mcount in vmlinux at build time depend on 7017 * CONFIG_BUILDTIME_MCOUNT_SORT, while mcount loc in 7018 * modules can not be sorted at build time. 7019 */ 7020 if (!IS_ENABLED(CONFIG_BUILDTIME_MCOUNT_SORT) || mod) { 7021 sort(start, count, sizeof(*start), 7022 ftrace_cmp_ips, NULL); 7023 } else { 7024 test_is_sorted(start, count); 7025 } 7026 7027 start_pg = ftrace_allocate_pages(count); 7028 if (!start_pg) 7029 return -ENOMEM; 7030 7031 mutex_lock(&ftrace_lock); 7032 7033 /* 7034 * Core and each module needs their own pages, as 7035 * modules will free them when they are removed. 7036 * Force a new page to be allocated for modules. 7037 */ 7038 if (!mod) { 7039 WARN_ON(ftrace_pages || ftrace_pages_start); 7040 /* First initialization */ 7041 ftrace_pages = ftrace_pages_start = start_pg; 7042 } else { 7043 if (!ftrace_pages) 7044 goto out; 7045 7046 if (WARN_ON(ftrace_pages->next)) { 7047 /* Hmm, we have free pages? */ 7048 while (ftrace_pages->next) 7049 ftrace_pages = ftrace_pages->next; 7050 } 7051 7052 ftrace_pages->next = start_pg; 7053 } 7054 7055 p = start; 7056 pg = start_pg; 7057 while (p < end) { 7058 unsigned long end_offset; 7059 addr = ftrace_call_adjust(*p++); 7060 /* 7061 * Some architecture linkers will pad between 7062 * the different mcount_loc sections of different 7063 * object files to satisfy alignments. 7064 * Skip any NULL pointers. 7065 */ 7066 if (!addr) { 7067 skipped++; 7068 continue; 7069 } 7070 7071 end_offset = (pg->index+1) * sizeof(pg->records[0]); 7072 if (end_offset > PAGE_SIZE << pg->order) { 7073 /* We should have allocated enough */ 7074 if (WARN_ON(!pg->next)) 7075 break; 7076 pg = pg->next; 7077 } 7078 7079 rec = &pg->records[pg->index++]; 7080 rec->ip = addr; 7081 } 7082 7083 if (pg->next) { 7084 pg_unuse = pg->next; 7085 pg->next = NULL; 7086 } 7087 7088 /* Assign the last page to ftrace_pages */ 7089 ftrace_pages = pg; 7090 7091 /* 7092 * We only need to disable interrupts on start up 7093 * because we are modifying code that an interrupt 7094 * may execute, and the modification is not atomic. 7095 * But for modules, nothing runs the code we modify 7096 * until we are finished with it, and there's no 7097 * reason to cause large interrupt latencies while we do it. 7098 */ 7099 if (!mod) 7100 local_irq_save(flags); 7101 ftrace_update_code(mod, start_pg); 7102 if (!mod) 7103 local_irq_restore(flags); 7104 ret = 0; 7105 out: 7106 mutex_unlock(&ftrace_lock); 7107 7108 /* We should have used all pages unless we skipped some */ 7109 if (pg_unuse) { 7110 WARN_ON(!skipped); 7111 /* Need to synchronize with ftrace_location_range() */ 7112 synchronize_rcu(); 7113 ftrace_free_pages(pg_unuse); 7114 } 7115 return ret; 7116 } 7117 7118 struct ftrace_mod_func { 7119 struct list_head list; 7120 char *name; 7121 unsigned long ip; 7122 unsigned int size; 7123 }; 7124 7125 struct ftrace_mod_map { 7126 struct rcu_head rcu; 7127 struct list_head list; 7128 struct module *mod; 7129 unsigned long start_addr; 7130 unsigned long end_addr; 7131 struct list_head funcs; 7132 unsigned int num_funcs; 7133 }; 7134 7135 static int ftrace_get_trampoline_kallsym(unsigned int symnum, 7136 unsigned long *value, char *type, 7137 char *name, char *module_name, 7138 int *exported) 7139 { 7140 struct ftrace_ops *op; 7141 7142 list_for_each_entry_rcu(op, &ftrace_ops_trampoline_list, list) { 7143 if (!op->trampoline || symnum--) 7144 continue; 7145 *value = op->trampoline; 7146 *type = 't'; 7147 strscpy(name, FTRACE_TRAMPOLINE_SYM, KSYM_NAME_LEN); 7148 strscpy(module_name, FTRACE_TRAMPOLINE_MOD, MODULE_NAME_LEN); 7149 *exported = 0; 7150 return 0; 7151 } 7152 7153 return -ERANGE; 7154 } 7155 7156 #if defined(CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS) || defined(CONFIG_MODULES) 7157 /* 7158 * Check if the current ops references the given ip. 7159 * 7160 * If the ops traces all functions, then it was already accounted for. 7161 * If the ops does not trace the current record function, skip it. 7162 * If the ops ignores the function via notrace filter, skip it. 7163 */ 7164 static bool 7165 ops_references_ip(struct ftrace_ops *ops, unsigned long ip) 7166 { 7167 /* If ops isn't enabled, ignore it */ 7168 if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) 7169 return false; 7170 7171 /* If ops traces all then it includes this function */ 7172 if (ops_traces_mod(ops)) 7173 return true; 7174 7175 /* The function must be in the filter */ 7176 if (!ftrace_hash_empty(ops->func_hash->filter_hash) && 7177 !__ftrace_lookup_ip(ops->func_hash->filter_hash, ip)) 7178 return false; 7179 7180 /* If in notrace hash, we ignore it too */ 7181 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, ip)) 7182 return false; 7183 7184 return true; 7185 } 7186 #endif 7187 7188 #ifdef CONFIG_MODULES 7189 7190 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next) 7191 7192 static LIST_HEAD(ftrace_mod_maps); 7193 7194 static int referenced_filters(struct dyn_ftrace *rec) 7195 { 7196 struct ftrace_ops *ops; 7197 int cnt = 0; 7198 7199 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) { 7200 if (ops_references_ip(ops, rec->ip)) { 7201 if (WARN_ON_ONCE(ops->flags & FTRACE_OPS_FL_DIRECT)) 7202 continue; 7203 if (WARN_ON_ONCE(ops->flags & FTRACE_OPS_FL_IPMODIFY)) 7204 continue; 7205 cnt++; 7206 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) 7207 rec->flags |= FTRACE_FL_REGS; 7208 if (cnt == 1 && ops->trampoline) 7209 rec->flags |= FTRACE_FL_TRAMP; 7210 else 7211 rec->flags &= ~FTRACE_FL_TRAMP; 7212 } 7213 } 7214 7215 return cnt; 7216 } 7217 7218 static void 7219 clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash) 7220 { 7221 struct ftrace_func_entry *entry; 7222 struct dyn_ftrace *rec; 7223 int i; 7224 7225 if (ftrace_hash_empty(hash)) 7226 return; 7227 7228 for (i = 0; i < pg->index; i++) { 7229 rec = &pg->records[i]; 7230 entry = __ftrace_lookup_ip(hash, rec->ip); 7231 /* 7232 * Do not allow this rec to match again. 7233 * Yeah, it may waste some memory, but will be removed 7234 * if/when the hash is modified again. 7235 */ 7236 if (entry) 7237 entry->ip = 0; 7238 } 7239 } 7240 7241 /* Clear any records from hashes */ 7242 static void clear_mod_from_hashes(struct ftrace_page *pg) 7243 { 7244 struct trace_array *tr; 7245 7246 mutex_lock(&trace_types_lock); 7247 list_for_each_entry(tr, &ftrace_trace_arrays, list) { 7248 if (!tr->ops || !tr->ops->func_hash) 7249 continue; 7250 mutex_lock(&tr->ops->func_hash->regex_lock); 7251 clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash); 7252 clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash); 7253 mutex_unlock(&tr->ops->func_hash->regex_lock); 7254 } 7255 mutex_unlock(&trace_types_lock); 7256 } 7257 7258 static void ftrace_free_mod_map(struct rcu_head *rcu) 7259 { 7260 struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu); 7261 struct ftrace_mod_func *mod_func; 7262 struct ftrace_mod_func *n; 7263 7264 /* All the contents of mod_map are now not visible to readers */ 7265 list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) { 7266 kfree(mod_func->name); 7267 list_del(&mod_func->list); 7268 kfree(mod_func); 7269 } 7270 7271 kfree(mod_map); 7272 } 7273 7274 void ftrace_release_mod(struct module *mod) 7275 { 7276 struct ftrace_mod_map *mod_map; 7277 struct ftrace_mod_map *n; 7278 struct dyn_ftrace *rec; 7279 struct ftrace_page **last_pg; 7280 struct ftrace_page *tmp_page = NULL; 7281 struct ftrace_page *pg; 7282 7283 mutex_lock(&ftrace_lock); 7284 7285 if (ftrace_disabled) 7286 goto out_unlock; 7287 7288 list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) { 7289 if (mod_map->mod == mod) { 7290 list_del_rcu(&mod_map->list); 7291 call_rcu(&mod_map->rcu, ftrace_free_mod_map); 7292 break; 7293 } 7294 } 7295 7296 /* 7297 * Each module has its own ftrace_pages, remove 7298 * them from the list. 7299 */ 7300 last_pg = &ftrace_pages_start; 7301 for (pg = ftrace_pages_start; pg; pg = *last_pg) { 7302 rec = &pg->records[0]; 7303 if (within_module(rec->ip, mod)) { 7304 /* 7305 * As core pages are first, the first 7306 * page should never be a module page. 7307 */ 7308 if (WARN_ON(pg == ftrace_pages_start)) 7309 goto out_unlock; 7310 7311 /* Check if we are deleting the last page */ 7312 if (pg == ftrace_pages) 7313 ftrace_pages = next_to_ftrace_page(last_pg); 7314 7315 ftrace_update_tot_cnt -= pg->index; 7316 *last_pg = pg->next; 7317 7318 pg->next = tmp_page; 7319 tmp_page = pg; 7320 } else 7321 last_pg = &pg->next; 7322 } 7323 out_unlock: 7324 mutex_unlock(&ftrace_lock); 7325 7326 /* Need to synchronize with ftrace_location_range() */ 7327 if (tmp_page) 7328 synchronize_rcu(); 7329 for (pg = tmp_page; pg; pg = tmp_page) { 7330 7331 /* Needs to be called outside of ftrace_lock */ 7332 clear_mod_from_hashes(pg); 7333 7334 if (pg->records) { 7335 free_pages((unsigned long)pg->records, pg->order); 7336 ftrace_number_of_pages -= 1 << pg->order; 7337 } 7338 tmp_page = pg->next; 7339 kfree(pg); 7340 ftrace_number_of_groups--; 7341 } 7342 } 7343 7344 void ftrace_module_enable(struct module *mod) 7345 { 7346 struct dyn_ftrace *rec; 7347 struct ftrace_page *pg; 7348 7349 mutex_lock(&ftrace_lock); 7350 7351 if (ftrace_disabled) 7352 goto out_unlock; 7353 7354 /* 7355 * If the tracing is enabled, go ahead and enable the record. 7356 * 7357 * The reason not to enable the record immediately is the 7358 * inherent check of ftrace_make_nop/ftrace_make_call for 7359 * correct previous instructions. Making first the NOP 7360 * conversion puts the module to the correct state, thus 7361 * passing the ftrace_make_call check. 7362 * 7363 * We also delay this to after the module code already set the 7364 * text to read-only, as we now need to set it back to read-write 7365 * so that we can modify the text. 7366 */ 7367 if (ftrace_start_up) 7368 ftrace_arch_code_modify_prepare(); 7369 7370 do_for_each_ftrace_rec(pg, rec) { 7371 int cnt; 7372 /* 7373 * do_for_each_ftrace_rec() is a double loop. 7374 * module text shares the pg. If a record is 7375 * not part of this module, then skip this pg, 7376 * which the "break" will do. 7377 */ 7378 if (!within_module(rec->ip, mod)) 7379 break; 7380 7381 /* Weak functions should still be ignored */ 7382 if (!test_for_valid_rec(rec)) { 7383 /* Clear all other flags. Should not be enabled anyway */ 7384 rec->flags = FTRACE_FL_DISABLED; 7385 continue; 7386 } 7387 7388 cnt = 0; 7389 7390 /* 7391 * When adding a module, we need to check if tracers are 7392 * currently enabled and if they are, and can trace this record, 7393 * we need to enable the module functions as well as update the 7394 * reference counts for those function records. 7395 */ 7396 if (ftrace_start_up) 7397 cnt += referenced_filters(rec); 7398 7399 rec->flags &= ~FTRACE_FL_DISABLED; 7400 rec->flags += cnt; 7401 7402 if (ftrace_start_up && cnt) { 7403 int failed = __ftrace_replace_code(rec, 1); 7404 if (failed) { 7405 ftrace_bug(failed, rec); 7406 goto out_loop; 7407 } 7408 } 7409 7410 } while_for_each_ftrace_rec(); 7411 7412 out_loop: 7413 if (ftrace_start_up) 7414 ftrace_arch_code_modify_post_process(); 7415 7416 out_unlock: 7417 mutex_unlock(&ftrace_lock); 7418 7419 process_cached_mods(mod->name); 7420 } 7421 7422 void ftrace_module_init(struct module *mod) 7423 { 7424 int ret; 7425 7426 if (ftrace_disabled || !mod->num_ftrace_callsites) 7427 return; 7428 7429 ret = ftrace_process_locs(mod, mod->ftrace_callsites, 7430 mod->ftrace_callsites + mod->num_ftrace_callsites); 7431 if (ret) 7432 pr_warn("ftrace: failed to allocate entries for module '%s' functions\n", 7433 mod->name); 7434 } 7435 7436 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map, 7437 struct dyn_ftrace *rec) 7438 { 7439 struct ftrace_mod_func *mod_func; 7440 unsigned long symsize; 7441 unsigned long offset; 7442 char str[KSYM_SYMBOL_LEN]; 7443 char *modname; 7444 const char *ret; 7445 7446 ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str); 7447 if (!ret) 7448 return; 7449 7450 mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL); 7451 if (!mod_func) 7452 return; 7453 7454 mod_func->name = kstrdup(str, GFP_KERNEL); 7455 if (!mod_func->name) { 7456 kfree(mod_func); 7457 return; 7458 } 7459 7460 mod_func->ip = rec->ip - offset; 7461 mod_func->size = symsize; 7462 7463 mod_map->num_funcs++; 7464 7465 list_add_rcu(&mod_func->list, &mod_map->funcs); 7466 } 7467 7468 static struct ftrace_mod_map * 7469 allocate_ftrace_mod_map(struct module *mod, 7470 unsigned long start, unsigned long end) 7471 { 7472 struct ftrace_mod_map *mod_map; 7473 7474 mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL); 7475 if (!mod_map) 7476 return NULL; 7477 7478 mod_map->mod = mod; 7479 mod_map->start_addr = start; 7480 mod_map->end_addr = end; 7481 mod_map->num_funcs = 0; 7482 7483 INIT_LIST_HEAD_RCU(&mod_map->funcs); 7484 7485 list_add_rcu(&mod_map->list, &ftrace_mod_maps); 7486 7487 return mod_map; 7488 } 7489 7490 static int 7491 ftrace_func_address_lookup(struct ftrace_mod_map *mod_map, 7492 unsigned long addr, unsigned long *size, 7493 unsigned long *off, char *sym) 7494 { 7495 struct ftrace_mod_func *found_func = NULL; 7496 struct ftrace_mod_func *mod_func; 7497 7498 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) { 7499 if (addr >= mod_func->ip && 7500 addr < mod_func->ip + mod_func->size) { 7501 found_func = mod_func; 7502 break; 7503 } 7504 } 7505 7506 if (found_func) { 7507 if (size) 7508 *size = found_func->size; 7509 if (off) 7510 *off = addr - found_func->ip; 7511 return strscpy(sym, found_func->name, KSYM_NAME_LEN); 7512 } 7513 7514 return 0; 7515 } 7516 7517 int 7518 ftrace_mod_address_lookup(unsigned long addr, unsigned long *size, 7519 unsigned long *off, char **modname, char *sym) 7520 { 7521 struct ftrace_mod_map *mod_map; 7522 int ret = 0; 7523 7524 /* mod_map is freed via call_rcu() */ 7525 preempt_disable(); 7526 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) { 7527 ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym); 7528 if (ret) { 7529 if (modname) 7530 *modname = mod_map->mod->name; 7531 break; 7532 } 7533 } 7534 preempt_enable(); 7535 7536 return ret; 7537 } 7538 7539 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value, 7540 char *type, char *name, 7541 char *module_name, int *exported) 7542 { 7543 struct ftrace_mod_map *mod_map; 7544 struct ftrace_mod_func *mod_func; 7545 int ret; 7546 7547 preempt_disable(); 7548 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) { 7549 7550 if (symnum >= mod_map->num_funcs) { 7551 symnum -= mod_map->num_funcs; 7552 continue; 7553 } 7554 7555 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) { 7556 if (symnum > 1) { 7557 symnum--; 7558 continue; 7559 } 7560 7561 *value = mod_func->ip; 7562 *type = 'T'; 7563 strscpy(name, mod_func->name, KSYM_NAME_LEN); 7564 strscpy(module_name, mod_map->mod->name, MODULE_NAME_LEN); 7565 *exported = 1; 7566 preempt_enable(); 7567 return 0; 7568 } 7569 WARN_ON(1); 7570 break; 7571 } 7572 ret = ftrace_get_trampoline_kallsym(symnum, value, type, name, 7573 module_name, exported); 7574 preempt_enable(); 7575 return ret; 7576 } 7577 7578 #else 7579 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map, 7580 struct dyn_ftrace *rec) { } 7581 static inline struct ftrace_mod_map * 7582 allocate_ftrace_mod_map(struct module *mod, 7583 unsigned long start, unsigned long end) 7584 { 7585 return NULL; 7586 } 7587 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value, 7588 char *type, char *name, char *module_name, 7589 int *exported) 7590 { 7591 int ret; 7592 7593 preempt_disable(); 7594 ret = ftrace_get_trampoline_kallsym(symnum, value, type, name, 7595 module_name, exported); 7596 preempt_enable(); 7597 return ret; 7598 } 7599 #endif /* CONFIG_MODULES */ 7600 7601 struct ftrace_init_func { 7602 struct list_head list; 7603 unsigned long ip; 7604 }; 7605 7606 /* Clear any init ips from hashes */ 7607 static void 7608 clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash) 7609 { 7610 struct ftrace_func_entry *entry; 7611 7612 entry = ftrace_lookup_ip(hash, func->ip); 7613 /* 7614 * Do not allow this rec to match again. 7615 * Yeah, it may waste some memory, but will be removed 7616 * if/when the hash is modified again. 7617 */ 7618 if (entry) 7619 entry->ip = 0; 7620 } 7621 7622 static void 7623 clear_func_from_hashes(struct ftrace_init_func *func) 7624 { 7625 struct trace_array *tr; 7626 7627 mutex_lock(&trace_types_lock); 7628 list_for_each_entry(tr, &ftrace_trace_arrays, list) { 7629 if (!tr->ops || !tr->ops->func_hash) 7630 continue; 7631 mutex_lock(&tr->ops->func_hash->regex_lock); 7632 clear_func_from_hash(func, tr->ops->func_hash->filter_hash); 7633 clear_func_from_hash(func, tr->ops->func_hash->notrace_hash); 7634 mutex_unlock(&tr->ops->func_hash->regex_lock); 7635 } 7636 mutex_unlock(&trace_types_lock); 7637 } 7638 7639 static void add_to_clear_hash_list(struct list_head *clear_list, 7640 struct dyn_ftrace *rec) 7641 { 7642 struct ftrace_init_func *func; 7643 7644 func = kmalloc(sizeof(*func), GFP_KERNEL); 7645 if (!func) { 7646 MEM_FAIL(1, "alloc failure, ftrace filter could be stale\n"); 7647 return; 7648 } 7649 7650 func->ip = rec->ip; 7651 list_add(&func->list, clear_list); 7652 } 7653 7654 void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr) 7655 { 7656 unsigned long start = (unsigned long)(start_ptr); 7657 unsigned long end = (unsigned long)(end_ptr); 7658 struct ftrace_page **last_pg = &ftrace_pages_start; 7659 struct ftrace_page *tmp_page = NULL; 7660 struct ftrace_page *pg; 7661 struct dyn_ftrace *rec; 7662 struct dyn_ftrace key; 7663 struct ftrace_mod_map *mod_map = NULL; 7664 struct ftrace_init_func *func, *func_next; 7665 LIST_HEAD(clear_hash); 7666 7667 key.ip = start; 7668 key.flags = end; /* overload flags, as it is unsigned long */ 7669 7670 mutex_lock(&ftrace_lock); 7671 7672 /* 7673 * If we are freeing module init memory, then check if 7674 * any tracer is active. If so, we need to save a mapping of 7675 * the module functions being freed with the address. 7676 */ 7677 if (mod && ftrace_ops_list != &ftrace_list_end) 7678 mod_map = allocate_ftrace_mod_map(mod, start, end); 7679 7680 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) { 7681 if (end < pg->records[0].ip || 7682 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE)) 7683 continue; 7684 again: 7685 rec = bsearch(&key, pg->records, pg->index, 7686 sizeof(struct dyn_ftrace), 7687 ftrace_cmp_recs); 7688 if (!rec) 7689 continue; 7690 7691 /* rec will be cleared from hashes after ftrace_lock unlock */ 7692 add_to_clear_hash_list(&clear_hash, rec); 7693 7694 if (mod_map) 7695 save_ftrace_mod_rec(mod_map, rec); 7696 7697 pg->index--; 7698 ftrace_update_tot_cnt--; 7699 if (!pg->index) { 7700 *last_pg = pg->next; 7701 pg->next = tmp_page; 7702 tmp_page = pg; 7703 pg = container_of(last_pg, struct ftrace_page, next); 7704 if (!(*last_pg)) 7705 ftrace_pages = pg; 7706 continue; 7707 } 7708 memmove(rec, rec + 1, 7709 (pg->index - (rec - pg->records)) * sizeof(*rec)); 7710 /* More than one function may be in this block */ 7711 goto again; 7712 } 7713 mutex_unlock(&ftrace_lock); 7714 7715 list_for_each_entry_safe(func, func_next, &clear_hash, list) { 7716 clear_func_from_hashes(func); 7717 kfree(func); 7718 } 7719 /* Need to synchronize with ftrace_location_range() */ 7720 if (tmp_page) { 7721 synchronize_rcu(); 7722 ftrace_free_pages(tmp_page); 7723 } 7724 } 7725 7726 void __init ftrace_free_init_mem(void) 7727 { 7728 void *start = (void *)(&__init_begin); 7729 void *end = (void *)(&__init_end); 7730 7731 ftrace_boot_snapshot(); 7732 7733 ftrace_free_mem(NULL, start, end); 7734 } 7735 7736 int __init __weak ftrace_dyn_arch_init(void) 7737 { 7738 return 0; 7739 } 7740 7741 void __init ftrace_init(void) 7742 { 7743 extern unsigned long __start_mcount_loc[]; 7744 extern unsigned long __stop_mcount_loc[]; 7745 unsigned long count, flags; 7746 int ret; 7747 7748 local_irq_save(flags); 7749 ret = ftrace_dyn_arch_init(); 7750 local_irq_restore(flags); 7751 if (ret) 7752 goto failed; 7753 7754 count = __stop_mcount_loc - __start_mcount_loc; 7755 if (!count) { 7756 pr_info("ftrace: No functions to be traced?\n"); 7757 goto failed; 7758 } 7759 7760 pr_info("ftrace: allocating %ld entries in %ld pages\n", 7761 count, DIV_ROUND_UP(count, ENTRIES_PER_PAGE)); 7762 7763 ret = ftrace_process_locs(NULL, 7764 __start_mcount_loc, 7765 __stop_mcount_loc); 7766 if (ret) { 7767 pr_warn("ftrace: failed to allocate entries for functions\n"); 7768 goto failed; 7769 } 7770 7771 pr_info("ftrace: allocated %ld pages with %ld groups\n", 7772 ftrace_number_of_pages, ftrace_number_of_groups); 7773 7774 last_ftrace_enabled = ftrace_enabled = 1; 7775 7776 set_ftrace_early_filters(); 7777 7778 return; 7779 failed: 7780 ftrace_disabled = 1; 7781 } 7782 7783 /* Do nothing if arch does not support this */ 7784 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops) 7785 { 7786 } 7787 7788 static void ftrace_update_trampoline(struct ftrace_ops *ops) 7789 { 7790 unsigned long trampoline = ops->trampoline; 7791 7792 arch_ftrace_update_trampoline(ops); 7793 if (ops->trampoline && ops->trampoline != trampoline && 7794 (ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP)) { 7795 /* Add to kallsyms before the perf events */ 7796 ftrace_add_trampoline_to_kallsyms(ops); 7797 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL, 7798 ops->trampoline, ops->trampoline_size, false, 7799 FTRACE_TRAMPOLINE_SYM); 7800 /* 7801 * Record the perf text poke event after the ksymbol register 7802 * event. 7803 */ 7804 perf_event_text_poke((void *)ops->trampoline, NULL, 0, 7805 (void *)ops->trampoline, 7806 ops->trampoline_size); 7807 } 7808 } 7809 7810 void ftrace_init_trace_array(struct trace_array *tr) 7811 { 7812 if (tr->flags & TRACE_ARRAY_FL_MOD_INIT) 7813 return; 7814 7815 INIT_LIST_HEAD(&tr->func_probes); 7816 INIT_LIST_HEAD(&tr->mod_trace); 7817 INIT_LIST_HEAD(&tr->mod_notrace); 7818 7819 tr->flags |= TRACE_ARRAY_FL_MOD_INIT; 7820 } 7821 #else 7822 7823 struct ftrace_ops global_ops = { 7824 .func = ftrace_stub, 7825 .flags = FTRACE_OPS_FL_INITIALIZED | 7826 FTRACE_OPS_FL_PID, 7827 }; 7828 7829 static int __init ftrace_nodyn_init(void) 7830 { 7831 ftrace_enabled = 1; 7832 return 0; 7833 } 7834 core_initcall(ftrace_nodyn_init); 7835 7836 static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; } 7837 static inline void ftrace_startup_all(int command) { } 7838 7839 static void ftrace_update_trampoline(struct ftrace_ops *ops) 7840 { 7841 } 7842 7843 #endif /* CONFIG_DYNAMIC_FTRACE */ 7844 7845 __init void ftrace_init_global_array_ops(struct trace_array *tr) 7846 { 7847 tr->ops = &global_ops; 7848 if (!global_ops.private) 7849 global_ops.private = tr; 7850 ftrace_init_trace_array(tr); 7851 init_array_fgraph_ops(tr, tr->ops); 7852 } 7853 7854 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func) 7855 { 7856 /* If we filter on pids, update to use the pid function */ 7857 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) { 7858 if (WARN_ON(tr->ops->func != ftrace_stub)) 7859 printk("ftrace ops had %pS for function\n", 7860 tr->ops->func); 7861 } 7862 tr->ops->func = func; 7863 tr->ops->private = tr; 7864 } 7865 7866 void ftrace_reset_array_ops(struct trace_array *tr) 7867 { 7868 tr->ops->func = ftrace_stub; 7869 } 7870 7871 static nokprobe_inline void 7872 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, 7873 struct ftrace_ops *ignored, struct ftrace_regs *fregs) 7874 { 7875 struct pt_regs *regs = ftrace_get_regs(fregs); 7876 struct ftrace_ops *op; 7877 int bit; 7878 7879 /* 7880 * The ftrace_test_and_set_recursion() will disable preemption, 7881 * which is required since some of the ops may be dynamically 7882 * allocated, they must be freed after a synchronize_rcu(). 7883 */ 7884 bit = trace_test_and_set_recursion(ip, parent_ip, TRACE_LIST_START); 7885 if (bit < 0) 7886 return; 7887 7888 do_for_each_ftrace_op(op, ftrace_ops_list) { 7889 /* Stub functions don't need to be called nor tested */ 7890 if (op->flags & FTRACE_OPS_FL_STUB) 7891 continue; 7892 /* 7893 * Check the following for each ops before calling their func: 7894 * if RCU flag is set, then rcu_is_watching() must be true 7895 * Otherwise test if the ip matches the ops filter 7896 * 7897 * If any of the above fails then the op->func() is not executed. 7898 */ 7899 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) && 7900 ftrace_ops_test(op, ip, regs)) { 7901 if (FTRACE_WARN_ON(!op->func)) { 7902 pr_warn("op=%p %pS\n", op, op); 7903 goto out; 7904 } 7905 op->func(ip, parent_ip, op, fregs); 7906 } 7907 } while_for_each_ftrace_op(op); 7908 out: 7909 trace_clear_recursion(bit); 7910 } 7911 7912 /* 7913 * Some archs only support passing ip and parent_ip. Even though 7914 * the list function ignores the op parameter, we do not want any 7915 * C side effects, where a function is called without the caller 7916 * sending a third parameter. 7917 * Archs are to support both the regs and ftrace_ops at the same time. 7918 * If they support ftrace_ops, it is assumed they support regs. 7919 * If call backs want to use regs, they must either check for regs 7920 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS. 7921 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved. 7922 * An architecture can pass partial regs with ftrace_ops and still 7923 * set the ARCH_SUPPORTS_FTRACE_OPS. 7924 * 7925 * In vmlinux.lds.h, ftrace_ops_list_func() is defined to be 7926 * arch_ftrace_ops_list_func. 7927 */ 7928 #if ARCH_SUPPORTS_FTRACE_OPS 7929 void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, 7930 struct ftrace_ops *op, struct ftrace_regs *fregs) 7931 { 7932 kmsan_unpoison_memory(fregs, ftrace_regs_size()); 7933 __ftrace_ops_list_func(ip, parent_ip, NULL, fregs); 7934 } 7935 #else 7936 void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip) 7937 { 7938 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL); 7939 } 7940 #endif 7941 NOKPROBE_SYMBOL(arch_ftrace_ops_list_func); 7942 7943 /* 7944 * If there's only one function registered but it does not support 7945 * recursion, needs RCU protection, then this function will be called 7946 * by the mcount trampoline. 7947 */ 7948 static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip, 7949 struct ftrace_ops *op, struct ftrace_regs *fregs) 7950 { 7951 int bit; 7952 7953 bit = trace_test_and_set_recursion(ip, parent_ip, TRACE_LIST_START); 7954 if (bit < 0) 7955 return; 7956 7957 if (!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) 7958 op->func(ip, parent_ip, op, fregs); 7959 7960 trace_clear_recursion(bit); 7961 } 7962 NOKPROBE_SYMBOL(ftrace_ops_assist_func); 7963 7964 /** 7965 * ftrace_ops_get_func - get the function a trampoline should call 7966 * @ops: the ops to get the function for 7967 * 7968 * Normally the mcount trampoline will call the ops->func, but there 7969 * are times that it should not. For example, if the ops does not 7970 * have its own recursion protection, then it should call the 7971 * ftrace_ops_assist_func() instead. 7972 * 7973 * Returns: the function that the trampoline should call for @ops. 7974 */ 7975 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops) 7976 { 7977 /* 7978 * If the function does not handle recursion or needs to be RCU safe, 7979 * then we need to call the assist handler. 7980 */ 7981 if (ops->flags & (FTRACE_OPS_FL_RECURSION | 7982 FTRACE_OPS_FL_RCU)) 7983 return ftrace_ops_assist_func; 7984 7985 return ops->func; 7986 } 7987 7988 static void 7989 ftrace_filter_pid_sched_switch_probe(void *data, bool preempt, 7990 struct task_struct *prev, 7991 struct task_struct *next, 7992 unsigned int prev_state) 7993 { 7994 struct trace_array *tr = data; 7995 struct trace_pid_list *pid_list; 7996 struct trace_pid_list *no_pid_list; 7997 7998 pid_list = rcu_dereference_sched(tr->function_pids); 7999 no_pid_list = rcu_dereference_sched(tr->function_no_pids); 8000 8001 if (trace_ignore_this_task(pid_list, no_pid_list, next)) 8002 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid, 8003 FTRACE_PID_IGNORE); 8004 else 8005 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid, 8006 next->pid); 8007 } 8008 8009 static void 8010 ftrace_pid_follow_sched_process_fork(void *data, 8011 struct task_struct *self, 8012 struct task_struct *task) 8013 { 8014 struct trace_pid_list *pid_list; 8015 struct trace_array *tr = data; 8016 8017 pid_list = rcu_dereference_sched(tr->function_pids); 8018 trace_filter_add_remove_task(pid_list, self, task); 8019 8020 pid_list = rcu_dereference_sched(tr->function_no_pids); 8021 trace_filter_add_remove_task(pid_list, self, task); 8022 } 8023 8024 static void 8025 ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task) 8026 { 8027 struct trace_pid_list *pid_list; 8028 struct trace_array *tr = data; 8029 8030 pid_list = rcu_dereference_sched(tr->function_pids); 8031 trace_filter_add_remove_task(pid_list, NULL, task); 8032 8033 pid_list = rcu_dereference_sched(tr->function_no_pids); 8034 trace_filter_add_remove_task(pid_list, NULL, task); 8035 } 8036 8037 void ftrace_pid_follow_fork(struct trace_array *tr, bool enable) 8038 { 8039 if (enable) { 8040 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork, 8041 tr); 8042 register_trace_sched_process_free(ftrace_pid_follow_sched_process_exit, 8043 tr); 8044 } else { 8045 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork, 8046 tr); 8047 unregister_trace_sched_process_free(ftrace_pid_follow_sched_process_exit, 8048 tr); 8049 } 8050 } 8051 8052 static void clear_ftrace_pids(struct trace_array *tr, int type) 8053 { 8054 struct trace_pid_list *pid_list; 8055 struct trace_pid_list *no_pid_list; 8056 int cpu; 8057 8058 pid_list = rcu_dereference_protected(tr->function_pids, 8059 lockdep_is_held(&ftrace_lock)); 8060 no_pid_list = rcu_dereference_protected(tr->function_no_pids, 8061 lockdep_is_held(&ftrace_lock)); 8062 8063 /* Make sure there's something to do */ 8064 if (!pid_type_enabled(type, pid_list, no_pid_list)) 8065 return; 8066 8067 /* See if the pids still need to be checked after this */ 8068 if (!still_need_pid_events(type, pid_list, no_pid_list)) { 8069 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr); 8070 for_each_possible_cpu(cpu) 8071 per_cpu_ptr(tr->array_buffer.data, cpu)->ftrace_ignore_pid = FTRACE_PID_TRACE; 8072 } 8073 8074 if (type & TRACE_PIDS) 8075 rcu_assign_pointer(tr->function_pids, NULL); 8076 8077 if (type & TRACE_NO_PIDS) 8078 rcu_assign_pointer(tr->function_no_pids, NULL); 8079 8080 /* Wait till all users are no longer using pid filtering */ 8081 synchronize_rcu(); 8082 8083 if ((type & TRACE_PIDS) && pid_list) 8084 trace_pid_list_free(pid_list); 8085 8086 if ((type & TRACE_NO_PIDS) && no_pid_list) 8087 trace_pid_list_free(no_pid_list); 8088 } 8089 8090 void ftrace_clear_pids(struct trace_array *tr) 8091 { 8092 mutex_lock(&ftrace_lock); 8093 8094 clear_ftrace_pids(tr, TRACE_PIDS | TRACE_NO_PIDS); 8095 8096 mutex_unlock(&ftrace_lock); 8097 } 8098 8099 static void ftrace_pid_reset(struct trace_array *tr, int type) 8100 { 8101 mutex_lock(&ftrace_lock); 8102 clear_ftrace_pids(tr, type); 8103 8104 ftrace_update_pid_func(); 8105 ftrace_startup_all(0); 8106 8107 mutex_unlock(&ftrace_lock); 8108 } 8109 8110 /* Greater than any max PID */ 8111 #define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1) 8112 8113 static void *fpid_start(struct seq_file *m, loff_t *pos) 8114 __acquires(RCU) 8115 { 8116 struct trace_pid_list *pid_list; 8117 struct trace_array *tr = m->private; 8118 8119 mutex_lock(&ftrace_lock); 8120 rcu_read_lock_sched(); 8121 8122 pid_list = rcu_dereference_sched(tr->function_pids); 8123 8124 if (!pid_list) 8125 return !(*pos) ? FTRACE_NO_PIDS : NULL; 8126 8127 return trace_pid_start(pid_list, pos); 8128 } 8129 8130 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos) 8131 { 8132 struct trace_array *tr = m->private; 8133 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids); 8134 8135 if (v == FTRACE_NO_PIDS) { 8136 (*pos)++; 8137 return NULL; 8138 } 8139 return trace_pid_next(pid_list, v, pos); 8140 } 8141 8142 static void fpid_stop(struct seq_file *m, void *p) 8143 __releases(RCU) 8144 { 8145 rcu_read_unlock_sched(); 8146 mutex_unlock(&ftrace_lock); 8147 } 8148 8149 static int fpid_show(struct seq_file *m, void *v) 8150 { 8151 if (v == FTRACE_NO_PIDS) { 8152 seq_puts(m, "no pid\n"); 8153 return 0; 8154 } 8155 8156 return trace_pid_show(m, v); 8157 } 8158 8159 static const struct seq_operations ftrace_pid_sops = { 8160 .start = fpid_start, 8161 .next = fpid_next, 8162 .stop = fpid_stop, 8163 .show = fpid_show, 8164 }; 8165 8166 static void *fnpid_start(struct seq_file *m, loff_t *pos) 8167 __acquires(RCU) 8168 { 8169 struct trace_pid_list *pid_list; 8170 struct trace_array *tr = m->private; 8171 8172 mutex_lock(&ftrace_lock); 8173 rcu_read_lock_sched(); 8174 8175 pid_list = rcu_dereference_sched(tr->function_no_pids); 8176 8177 if (!pid_list) 8178 return !(*pos) ? FTRACE_NO_PIDS : NULL; 8179 8180 return trace_pid_start(pid_list, pos); 8181 } 8182 8183 static void *fnpid_next(struct seq_file *m, void *v, loff_t *pos) 8184 { 8185 struct trace_array *tr = m->private; 8186 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_no_pids); 8187 8188 if (v == FTRACE_NO_PIDS) { 8189 (*pos)++; 8190 return NULL; 8191 } 8192 return trace_pid_next(pid_list, v, pos); 8193 } 8194 8195 static const struct seq_operations ftrace_no_pid_sops = { 8196 .start = fnpid_start, 8197 .next = fnpid_next, 8198 .stop = fpid_stop, 8199 .show = fpid_show, 8200 }; 8201 8202 static int pid_open(struct inode *inode, struct file *file, int type) 8203 { 8204 const struct seq_operations *seq_ops; 8205 struct trace_array *tr = inode->i_private; 8206 struct seq_file *m; 8207 int ret = 0; 8208 8209 ret = tracing_check_open_get_tr(tr); 8210 if (ret) 8211 return ret; 8212 8213 if ((file->f_mode & FMODE_WRITE) && 8214 (file->f_flags & O_TRUNC)) 8215 ftrace_pid_reset(tr, type); 8216 8217 switch (type) { 8218 case TRACE_PIDS: 8219 seq_ops = &ftrace_pid_sops; 8220 break; 8221 case TRACE_NO_PIDS: 8222 seq_ops = &ftrace_no_pid_sops; 8223 break; 8224 default: 8225 trace_array_put(tr); 8226 WARN_ON_ONCE(1); 8227 return -EINVAL; 8228 } 8229 8230 ret = seq_open(file, seq_ops); 8231 if (ret < 0) { 8232 trace_array_put(tr); 8233 } else { 8234 m = file->private_data; 8235 /* copy tr over to seq ops */ 8236 m->private = tr; 8237 } 8238 8239 return ret; 8240 } 8241 8242 static int 8243 ftrace_pid_open(struct inode *inode, struct file *file) 8244 { 8245 return pid_open(inode, file, TRACE_PIDS); 8246 } 8247 8248 static int 8249 ftrace_no_pid_open(struct inode *inode, struct file *file) 8250 { 8251 return pid_open(inode, file, TRACE_NO_PIDS); 8252 } 8253 8254 static void ignore_task_cpu(void *data) 8255 { 8256 struct trace_array *tr = data; 8257 struct trace_pid_list *pid_list; 8258 struct trace_pid_list *no_pid_list; 8259 8260 /* 8261 * This function is called by on_each_cpu() while the 8262 * event_mutex is held. 8263 */ 8264 pid_list = rcu_dereference_protected(tr->function_pids, 8265 mutex_is_locked(&ftrace_lock)); 8266 no_pid_list = rcu_dereference_protected(tr->function_no_pids, 8267 mutex_is_locked(&ftrace_lock)); 8268 8269 if (trace_ignore_this_task(pid_list, no_pid_list, current)) 8270 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid, 8271 FTRACE_PID_IGNORE); 8272 else 8273 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid, 8274 current->pid); 8275 } 8276 8277 static ssize_t 8278 pid_write(struct file *filp, const char __user *ubuf, 8279 size_t cnt, loff_t *ppos, int type) 8280 { 8281 struct seq_file *m = filp->private_data; 8282 struct trace_array *tr = m->private; 8283 struct trace_pid_list *filtered_pids; 8284 struct trace_pid_list *other_pids; 8285 struct trace_pid_list *pid_list; 8286 ssize_t ret; 8287 8288 if (!cnt) 8289 return 0; 8290 8291 guard(mutex)(&ftrace_lock); 8292 8293 switch (type) { 8294 case TRACE_PIDS: 8295 filtered_pids = rcu_dereference_protected(tr->function_pids, 8296 lockdep_is_held(&ftrace_lock)); 8297 other_pids = rcu_dereference_protected(tr->function_no_pids, 8298 lockdep_is_held(&ftrace_lock)); 8299 break; 8300 case TRACE_NO_PIDS: 8301 filtered_pids = rcu_dereference_protected(tr->function_no_pids, 8302 lockdep_is_held(&ftrace_lock)); 8303 other_pids = rcu_dereference_protected(tr->function_pids, 8304 lockdep_is_held(&ftrace_lock)); 8305 break; 8306 default: 8307 WARN_ON_ONCE(1); 8308 return -EINVAL; 8309 } 8310 8311 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt); 8312 if (ret < 0) 8313 return ret; 8314 8315 switch (type) { 8316 case TRACE_PIDS: 8317 rcu_assign_pointer(tr->function_pids, pid_list); 8318 break; 8319 case TRACE_NO_PIDS: 8320 rcu_assign_pointer(tr->function_no_pids, pid_list); 8321 break; 8322 } 8323 8324 8325 if (filtered_pids) { 8326 synchronize_rcu(); 8327 trace_pid_list_free(filtered_pids); 8328 } else if (pid_list && !other_pids) { 8329 /* Register a probe to set whether to ignore the tracing of a task */ 8330 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr); 8331 } 8332 8333 /* 8334 * Ignoring of pids is done at task switch. But we have to 8335 * check for those tasks that are currently running. 8336 * Always do this in case a pid was appended or removed. 8337 */ 8338 on_each_cpu(ignore_task_cpu, tr, 1); 8339 8340 ftrace_update_pid_func(); 8341 ftrace_startup_all(0); 8342 8343 *ppos += ret; 8344 8345 return ret; 8346 } 8347 8348 static ssize_t 8349 ftrace_pid_write(struct file *filp, const char __user *ubuf, 8350 size_t cnt, loff_t *ppos) 8351 { 8352 return pid_write(filp, ubuf, cnt, ppos, TRACE_PIDS); 8353 } 8354 8355 static ssize_t 8356 ftrace_no_pid_write(struct file *filp, const char __user *ubuf, 8357 size_t cnt, loff_t *ppos) 8358 { 8359 return pid_write(filp, ubuf, cnt, ppos, TRACE_NO_PIDS); 8360 } 8361 8362 static int 8363 ftrace_pid_release(struct inode *inode, struct file *file) 8364 { 8365 struct trace_array *tr = inode->i_private; 8366 8367 trace_array_put(tr); 8368 8369 return seq_release(inode, file); 8370 } 8371 8372 static const struct file_operations ftrace_pid_fops = { 8373 .open = ftrace_pid_open, 8374 .write = ftrace_pid_write, 8375 .read = seq_read, 8376 .llseek = tracing_lseek, 8377 .release = ftrace_pid_release, 8378 }; 8379 8380 static const struct file_operations ftrace_no_pid_fops = { 8381 .open = ftrace_no_pid_open, 8382 .write = ftrace_no_pid_write, 8383 .read = seq_read, 8384 .llseek = tracing_lseek, 8385 .release = ftrace_pid_release, 8386 }; 8387 8388 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer) 8389 { 8390 trace_create_file("set_ftrace_pid", TRACE_MODE_WRITE, d_tracer, 8391 tr, &ftrace_pid_fops); 8392 trace_create_file("set_ftrace_notrace_pid", TRACE_MODE_WRITE, 8393 d_tracer, tr, &ftrace_no_pid_fops); 8394 } 8395 8396 void __init ftrace_init_tracefs_toplevel(struct trace_array *tr, 8397 struct dentry *d_tracer) 8398 { 8399 /* Only the top level directory has the dyn_tracefs and profile */ 8400 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL)); 8401 8402 ftrace_init_dyn_tracefs(d_tracer); 8403 ftrace_profile_tracefs(d_tracer); 8404 } 8405 8406 /** 8407 * ftrace_kill - kill ftrace 8408 * 8409 * This function should be used by panic code. It stops ftrace 8410 * but in a not so nice way. If you need to simply kill ftrace 8411 * from a non-atomic section, use ftrace_kill. 8412 */ 8413 void ftrace_kill(void) 8414 { 8415 ftrace_disabled = 1; 8416 ftrace_enabled = 0; 8417 ftrace_trace_function = ftrace_stub; 8418 kprobe_ftrace_kill(); 8419 } 8420 8421 /** 8422 * ftrace_is_dead - Test if ftrace is dead or not. 8423 * 8424 * Returns: 1 if ftrace is "dead", zero otherwise. 8425 */ 8426 int ftrace_is_dead(void) 8427 { 8428 return ftrace_disabled; 8429 } 8430 8431 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS 8432 /* 8433 * When registering ftrace_ops with IPMODIFY, it is necessary to make sure 8434 * it doesn't conflict with any direct ftrace_ops. If there is existing 8435 * direct ftrace_ops on a kernel function being patched, call 8436 * FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_PEER on it to enable sharing. 8437 * 8438 * @ops: ftrace_ops being registered. 8439 * 8440 * Returns: 8441 * 0 on success; 8442 * Negative on failure. 8443 */ 8444 static int prepare_direct_functions_for_ipmodify(struct ftrace_ops *ops) 8445 { 8446 struct ftrace_func_entry *entry; 8447 struct ftrace_hash *hash; 8448 struct ftrace_ops *op; 8449 int size, i, ret; 8450 8451 lockdep_assert_held_once(&direct_mutex); 8452 8453 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY)) 8454 return 0; 8455 8456 hash = ops->func_hash->filter_hash; 8457 size = 1 << hash->size_bits; 8458 for (i = 0; i < size; i++) { 8459 hlist_for_each_entry(entry, &hash->buckets[i], hlist) { 8460 unsigned long ip = entry->ip; 8461 bool found_op = false; 8462 8463 mutex_lock(&ftrace_lock); 8464 do_for_each_ftrace_op(op, ftrace_ops_list) { 8465 if (!(op->flags & FTRACE_OPS_FL_DIRECT)) 8466 continue; 8467 if (ops_references_ip(op, ip)) { 8468 found_op = true; 8469 break; 8470 } 8471 } while_for_each_ftrace_op(op); 8472 mutex_unlock(&ftrace_lock); 8473 8474 if (found_op) { 8475 if (!op->ops_func) 8476 return -EBUSY; 8477 8478 ret = op->ops_func(op, FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_PEER); 8479 if (ret) 8480 return ret; 8481 } 8482 } 8483 } 8484 8485 return 0; 8486 } 8487 8488 /* 8489 * Similar to prepare_direct_functions_for_ipmodify, clean up after ops 8490 * with IPMODIFY is unregistered. The cleanup is optional for most DIRECT 8491 * ops. 8492 */ 8493 static void cleanup_direct_functions_after_ipmodify(struct ftrace_ops *ops) 8494 { 8495 struct ftrace_func_entry *entry; 8496 struct ftrace_hash *hash; 8497 struct ftrace_ops *op; 8498 int size, i; 8499 8500 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY)) 8501 return; 8502 8503 mutex_lock(&direct_mutex); 8504 8505 hash = ops->func_hash->filter_hash; 8506 size = 1 << hash->size_bits; 8507 for (i = 0; i < size; i++) { 8508 hlist_for_each_entry(entry, &hash->buckets[i], hlist) { 8509 unsigned long ip = entry->ip; 8510 bool found_op = false; 8511 8512 mutex_lock(&ftrace_lock); 8513 do_for_each_ftrace_op(op, ftrace_ops_list) { 8514 if (!(op->flags & FTRACE_OPS_FL_DIRECT)) 8515 continue; 8516 if (ops_references_ip(op, ip)) { 8517 found_op = true; 8518 break; 8519 } 8520 } while_for_each_ftrace_op(op); 8521 mutex_unlock(&ftrace_lock); 8522 8523 /* The cleanup is optional, ignore any errors */ 8524 if (found_op && op->ops_func) 8525 op->ops_func(op, FTRACE_OPS_CMD_DISABLE_SHARE_IPMODIFY_PEER); 8526 } 8527 } 8528 mutex_unlock(&direct_mutex); 8529 } 8530 8531 #define lock_direct_mutex() mutex_lock(&direct_mutex) 8532 #define unlock_direct_mutex() mutex_unlock(&direct_mutex) 8533 8534 #else /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */ 8535 8536 static int prepare_direct_functions_for_ipmodify(struct ftrace_ops *ops) 8537 { 8538 return 0; 8539 } 8540 8541 static void cleanup_direct_functions_after_ipmodify(struct ftrace_ops *ops) 8542 { 8543 } 8544 8545 #define lock_direct_mutex() do { } while (0) 8546 #define unlock_direct_mutex() do { } while (0) 8547 8548 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */ 8549 8550 /* 8551 * Similar to register_ftrace_function, except we don't lock direct_mutex. 8552 */ 8553 static int register_ftrace_function_nolock(struct ftrace_ops *ops) 8554 { 8555 int ret; 8556 8557 ftrace_ops_init(ops); 8558 8559 mutex_lock(&ftrace_lock); 8560 8561 ret = ftrace_startup(ops, 0); 8562 8563 mutex_unlock(&ftrace_lock); 8564 8565 return ret; 8566 } 8567 8568 /** 8569 * register_ftrace_function - register a function for profiling 8570 * @ops: ops structure that holds the function for profiling. 8571 * 8572 * Register a function to be called by all functions in the 8573 * kernel. 8574 * 8575 * Note: @ops->func and all the functions it calls must be labeled 8576 * with "notrace", otherwise it will go into a 8577 * recursive loop. 8578 */ 8579 int register_ftrace_function(struct ftrace_ops *ops) 8580 { 8581 int ret; 8582 8583 lock_direct_mutex(); 8584 ret = prepare_direct_functions_for_ipmodify(ops); 8585 if (ret < 0) 8586 goto out_unlock; 8587 8588 ret = register_ftrace_function_nolock(ops); 8589 8590 out_unlock: 8591 unlock_direct_mutex(); 8592 return ret; 8593 } 8594 EXPORT_SYMBOL_GPL(register_ftrace_function); 8595 8596 /** 8597 * unregister_ftrace_function - unregister a function for profiling. 8598 * @ops: ops structure that holds the function to unregister 8599 * 8600 * Unregister a function that was added to be called by ftrace profiling. 8601 */ 8602 int unregister_ftrace_function(struct ftrace_ops *ops) 8603 { 8604 int ret; 8605 8606 mutex_lock(&ftrace_lock); 8607 ret = ftrace_shutdown(ops, 0); 8608 mutex_unlock(&ftrace_lock); 8609 8610 cleanup_direct_functions_after_ipmodify(ops); 8611 return ret; 8612 } 8613 EXPORT_SYMBOL_GPL(unregister_ftrace_function); 8614 8615 static int symbols_cmp(const void *a, const void *b) 8616 { 8617 const char **str_a = (const char **) a; 8618 const char **str_b = (const char **) b; 8619 8620 return strcmp(*str_a, *str_b); 8621 } 8622 8623 struct kallsyms_data { 8624 unsigned long *addrs; 8625 const char **syms; 8626 size_t cnt; 8627 size_t found; 8628 }; 8629 8630 /* This function gets called for all kernel and module symbols 8631 * and returns 1 in case we resolved all the requested symbols, 8632 * 0 otherwise. 8633 */ 8634 static int kallsyms_callback(void *data, const char *name, unsigned long addr) 8635 { 8636 struct kallsyms_data *args = data; 8637 const char **sym; 8638 int idx; 8639 8640 sym = bsearch(&name, args->syms, args->cnt, sizeof(*args->syms), symbols_cmp); 8641 if (!sym) 8642 return 0; 8643 8644 idx = sym - args->syms; 8645 if (args->addrs[idx]) 8646 return 0; 8647 8648 if (!ftrace_location(addr)) 8649 return 0; 8650 8651 args->addrs[idx] = addr; 8652 args->found++; 8653 return args->found == args->cnt ? 1 : 0; 8654 } 8655 8656 /** 8657 * ftrace_lookup_symbols - Lookup addresses for array of symbols 8658 * 8659 * @sorted_syms: array of symbols pointers symbols to resolve, 8660 * must be alphabetically sorted 8661 * @cnt: number of symbols/addresses in @syms/@addrs arrays 8662 * @addrs: array for storing resulting addresses 8663 * 8664 * This function looks up addresses for array of symbols provided in 8665 * @syms array (must be alphabetically sorted) and stores them in 8666 * @addrs array, which needs to be big enough to store at least @cnt 8667 * addresses. 8668 * 8669 * Returns: 0 if all provided symbols are found, -ESRCH otherwise. 8670 */ 8671 int ftrace_lookup_symbols(const char **sorted_syms, size_t cnt, unsigned long *addrs) 8672 { 8673 struct kallsyms_data args; 8674 int found_all; 8675 8676 memset(addrs, 0, sizeof(*addrs) * cnt); 8677 args.addrs = addrs; 8678 args.syms = sorted_syms; 8679 args.cnt = cnt; 8680 args.found = 0; 8681 8682 found_all = kallsyms_on_each_symbol(kallsyms_callback, &args); 8683 if (found_all) 8684 return 0; 8685 found_all = module_kallsyms_on_each_symbol(NULL, kallsyms_callback, &args); 8686 return found_all ? 0 : -ESRCH; 8687 } 8688 8689 #ifdef CONFIG_SYSCTL 8690 8691 #ifdef CONFIG_DYNAMIC_FTRACE 8692 static void ftrace_startup_sysctl(void) 8693 { 8694 int command; 8695 8696 if (unlikely(ftrace_disabled)) 8697 return; 8698 8699 /* Force update next time */ 8700 saved_ftrace_func = NULL; 8701 /* ftrace_start_up is true if we want ftrace running */ 8702 if (ftrace_start_up) { 8703 command = FTRACE_UPDATE_CALLS; 8704 if (ftrace_graph_active) 8705 command |= FTRACE_START_FUNC_RET; 8706 ftrace_startup_enable(command); 8707 } 8708 } 8709 8710 static void ftrace_shutdown_sysctl(void) 8711 { 8712 int command; 8713 8714 if (unlikely(ftrace_disabled)) 8715 return; 8716 8717 /* ftrace_start_up is true if ftrace is running */ 8718 if (ftrace_start_up) { 8719 command = FTRACE_DISABLE_CALLS; 8720 if (ftrace_graph_active) 8721 command |= FTRACE_STOP_FUNC_RET; 8722 ftrace_run_update_code(command); 8723 } 8724 } 8725 #else 8726 # define ftrace_startup_sysctl() do { } while (0) 8727 # define ftrace_shutdown_sysctl() do { } while (0) 8728 #endif /* CONFIG_DYNAMIC_FTRACE */ 8729 8730 static bool is_permanent_ops_registered(void) 8731 { 8732 struct ftrace_ops *op; 8733 8734 do_for_each_ftrace_op(op, ftrace_ops_list) { 8735 if (op->flags & FTRACE_OPS_FL_PERMANENT) 8736 return true; 8737 } while_for_each_ftrace_op(op); 8738 8739 return false; 8740 } 8741 8742 static int 8743 ftrace_enable_sysctl(const struct ctl_table *table, int write, 8744 void *buffer, size_t *lenp, loff_t *ppos) 8745 { 8746 int ret; 8747 8748 guard(mutex)(&ftrace_lock); 8749 8750 if (unlikely(ftrace_disabled)) 8751 return -ENODEV; 8752 8753 ret = proc_dointvec(table, write, buffer, lenp, ppos); 8754 8755 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled)) 8756 return ret; 8757 8758 if (ftrace_enabled) { 8759 8760 /* we are starting ftrace again */ 8761 if (rcu_dereference_protected(ftrace_ops_list, 8762 lockdep_is_held(&ftrace_lock)) != &ftrace_list_end) 8763 update_ftrace_function(); 8764 8765 ftrace_startup_sysctl(); 8766 8767 } else { 8768 if (is_permanent_ops_registered()) { 8769 ftrace_enabled = true; 8770 return -EBUSY; 8771 } 8772 8773 /* stopping ftrace calls (just send to ftrace_stub) */ 8774 ftrace_trace_function = ftrace_stub; 8775 8776 ftrace_shutdown_sysctl(); 8777 } 8778 8779 last_ftrace_enabled = !!ftrace_enabled; 8780 return 0; 8781 } 8782 8783 static const struct ctl_table ftrace_sysctls[] = { 8784 { 8785 .procname = "ftrace_enabled", 8786 .data = &ftrace_enabled, 8787 .maxlen = sizeof(int), 8788 .mode = 0644, 8789 .proc_handler = ftrace_enable_sysctl, 8790 }, 8791 }; 8792 8793 static int __init ftrace_sysctl_init(void) 8794 { 8795 register_sysctl_init("kernel", ftrace_sysctls); 8796 return 0; 8797 } 8798 late_initcall(ftrace_sysctl_init); 8799 #endif 8800