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