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