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