1/* 2 * Infrastructure for profiling code inserted by 'gcc -pg'. 3 * 4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com> 5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com> 6 * 7 * Originally ported from the -rt patch by: 8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com> 9 * 10 * Based on code in the latency_tracer, that is: 11 * 12 * Copyright (C) 2004-2006 Ingo Molnar 13 * Copyright (C) 2004 Nadia Yvette Chambers 14 */ 15 16#include <linux/stop_machine.h> 17#include <linux/clocksource.h> 18#include <linux/kallsyms.h> 19#include <linux/seq_file.h> 20#include <linux/suspend.h> 21#include <linux/tracefs.h> 22#include <linux/hardirq.h> 23#include <linux/kthread.h> 24#include <linux/uaccess.h> 25#include <linux/bsearch.h> 26#include <linux/module.h> 27#include <linux/ftrace.h> 28#include <linux/sysctl.h> 29#include <linux/slab.h> 30#include <linux/ctype.h> 31#include <linux/sort.h> 32#include <linux/list.h> 33#include <linux/hash.h> 34#include <linux/rcupdate.h> 35 36#include <trace/events/sched.h> 37 38#include <asm/setup.h> 39 40#include "trace_output.h" 41#include "trace_stat.h" 42 43#define FTRACE_WARN_ON(cond) \ 44 ({ \ 45 int ___r = cond; \ 46 if (WARN_ON(___r)) \ 47 ftrace_kill(); \ 48 ___r; \ 49 }) 50 51#define FTRACE_WARN_ON_ONCE(cond) \ 52 ({ \ 53 int ___r = cond; \ 54 if (WARN_ON_ONCE(___r)) \ 55 ftrace_kill(); \ 56 ___r; \ 57 }) 58 59/* hash bits for specific function selection */ 60#define FTRACE_HASH_BITS 7 61#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS) 62#define FTRACE_HASH_DEFAULT_BITS 10 63#define FTRACE_HASH_MAX_BITS 12 64 65#define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_CONTROL) 66 67#ifdef CONFIG_DYNAMIC_FTRACE 68#define INIT_OPS_HASH(opsname) \ 69 .func_hash = &opsname.local_hash, \ 70 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock), 71#define ASSIGN_OPS_HASH(opsname, val) \ 72 .func_hash = val, \ 73 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock), 74#else 75#define INIT_OPS_HASH(opsname) 76#define ASSIGN_OPS_HASH(opsname, val) 77#endif 78 79static struct ftrace_ops ftrace_list_end __read_mostly = { 80 .func = ftrace_stub, 81 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB, 82 INIT_OPS_HASH(ftrace_list_end) 83}; 84 85/* ftrace_enabled is a method to turn ftrace on or off */ 86int ftrace_enabled __read_mostly; 87static int last_ftrace_enabled; 88 89/* Current function tracing op */ 90struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end; 91/* What to set function_trace_op to */ 92static struct ftrace_ops *set_function_trace_op; 93 94/* List for set_ftrace_pid's pids. */ 95LIST_HEAD(ftrace_pids); 96struct ftrace_pid { 97 struct list_head list; 98 struct pid *pid; 99}; 100 101static bool ftrace_pids_enabled(void) 102{ 103 return !list_empty(&ftrace_pids); 104} 105 106static void ftrace_update_trampoline(struct ftrace_ops *ops); 107 108/* 109 * ftrace_disabled is set when an anomaly is discovered. 110 * ftrace_disabled is much stronger than ftrace_enabled. 111 */ 112static int ftrace_disabled __read_mostly; 113 114static DEFINE_MUTEX(ftrace_lock); 115 116static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end; 117static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end; 118ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub; 119static struct ftrace_ops global_ops; 120static struct ftrace_ops control_ops; 121 122static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip, 123 struct ftrace_ops *op, struct pt_regs *regs); 124 125#if ARCH_SUPPORTS_FTRACE_OPS 126static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, 127 struct ftrace_ops *op, struct pt_regs *regs); 128#else 129/* See comment below, where ftrace_ops_list_func is defined */ 130static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip); 131#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops) 132#endif 133 134/* 135 * Traverse the ftrace_global_list, invoking all entries. The reason that we 136 * can use rcu_dereference_raw_notrace() is that elements removed from this list 137 * are simply leaked, so there is no need to interact with a grace-period 138 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle 139 * concurrent insertions into the ftrace_global_list. 140 * 141 * Silly Alpha and silly pointer-speculation compiler optimizations! 142 */ 143#define do_for_each_ftrace_op(op, list) \ 144 op = rcu_dereference_raw_notrace(list); \ 145 do 146 147/* 148 * Optimized for just a single item in the list (as that is the normal case). 149 */ 150#define while_for_each_ftrace_op(op) \ 151 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \ 152 unlikely((op) != &ftrace_list_end)) 153 154static inline void ftrace_ops_init(struct ftrace_ops *ops) 155{ 156#ifdef CONFIG_DYNAMIC_FTRACE 157 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) { 158 mutex_init(&ops->local_hash.regex_lock); 159 ops->func_hash = &ops->local_hash; 160 ops->flags |= FTRACE_OPS_FL_INITIALIZED; 161 } 162#endif 163} 164 165/** 166 * ftrace_nr_registered_ops - return number of ops registered 167 * 168 * Returns the number of ftrace_ops registered and tracing functions 169 */ 170int ftrace_nr_registered_ops(void) 171{ 172 struct ftrace_ops *ops; 173 int cnt = 0; 174 175 mutex_lock(&ftrace_lock); 176 177 for (ops = ftrace_ops_list; 178 ops != &ftrace_list_end; ops = ops->next) 179 cnt++; 180 181 mutex_unlock(&ftrace_lock); 182 183 return cnt; 184} 185 186static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip, 187 struct ftrace_ops *op, struct pt_regs *regs) 188{ 189 if (!test_tsk_trace_trace(current)) 190 return; 191 192 op->saved_func(ip, parent_ip, op, regs); 193} 194 195/** 196 * clear_ftrace_function - reset the ftrace function 197 * 198 * This NULLs the ftrace function and in essence stops 199 * tracing. There may be lag 200 */ 201void clear_ftrace_function(void) 202{ 203 ftrace_trace_function = ftrace_stub; 204} 205 206static void control_ops_disable_all(struct ftrace_ops *ops) 207{ 208 int cpu; 209 210 for_each_possible_cpu(cpu) 211 *per_cpu_ptr(ops->disabled, cpu) = 1; 212} 213 214static int control_ops_alloc(struct ftrace_ops *ops) 215{ 216 int __percpu *disabled; 217 218 disabled = alloc_percpu(int); 219 if (!disabled) 220 return -ENOMEM; 221 222 ops->disabled = disabled; 223 control_ops_disable_all(ops); 224 return 0; 225} 226 227static void ftrace_sync(struct work_struct *work) 228{ 229 /* 230 * This function is just a stub to implement a hard force 231 * of synchronize_sched(). This requires synchronizing 232 * tasks even in userspace and idle. 233 * 234 * Yes, function tracing is rude. 235 */ 236} 237 238static void ftrace_sync_ipi(void *data) 239{ 240 /* Probably not needed, but do it anyway */ 241 smp_rmb(); 242} 243 244#ifdef CONFIG_FUNCTION_GRAPH_TRACER 245static void update_function_graph_func(void); 246#else 247static inline void update_function_graph_func(void) { } 248#endif 249 250 251static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops) 252{ 253 /* 254 * If this is a dynamic ops or we force list func, 255 * then it needs to call the list anyway. 256 */ 257 if (ops->flags & FTRACE_OPS_FL_DYNAMIC || FTRACE_FORCE_LIST_FUNC) 258 return ftrace_ops_list_func; 259 260 return ftrace_ops_get_func(ops); 261} 262 263static void update_ftrace_function(void) 264{ 265 ftrace_func_t func; 266 267 /* 268 * Prepare the ftrace_ops that the arch callback will use. 269 * If there's only one ftrace_ops registered, the ftrace_ops_list 270 * will point to the ops we want. 271 */ 272 set_function_trace_op = ftrace_ops_list; 273 274 /* If there's no ftrace_ops registered, just call the stub function */ 275 if (ftrace_ops_list == &ftrace_list_end) { 276 func = ftrace_stub; 277 278 /* 279 * If we are at the end of the list and this ops is 280 * recursion safe and not dynamic and the arch supports passing ops, 281 * then have the mcount trampoline call the function directly. 282 */ 283 } else if (ftrace_ops_list->next == &ftrace_list_end) { 284 func = ftrace_ops_get_list_func(ftrace_ops_list); 285 286 } else { 287 /* Just use the default ftrace_ops */ 288 set_function_trace_op = &ftrace_list_end; 289 func = ftrace_ops_list_func; 290 } 291 292 update_function_graph_func(); 293 294 /* If there's no change, then do nothing more here */ 295 if (ftrace_trace_function == func) 296 return; 297 298 /* 299 * If we are using the list function, it doesn't care 300 * about the function_trace_ops. 301 */ 302 if (func == ftrace_ops_list_func) { 303 ftrace_trace_function = func; 304 /* 305 * Don't even bother setting function_trace_ops, 306 * it would be racy to do so anyway. 307 */ 308 return; 309 } 310 311#ifndef CONFIG_DYNAMIC_FTRACE 312 /* 313 * For static tracing, we need to be a bit more careful. 314 * The function change takes affect immediately. Thus, 315 * we need to coorditate the setting of the function_trace_ops 316 * with the setting of the ftrace_trace_function. 317 * 318 * Set the function to the list ops, which will call the 319 * function we want, albeit indirectly, but it handles the 320 * ftrace_ops and doesn't depend on function_trace_op. 321 */ 322 ftrace_trace_function = ftrace_ops_list_func; 323 /* 324 * Make sure all CPUs see this. Yes this is slow, but static 325 * tracing is slow and nasty to have enabled. 326 */ 327 schedule_on_each_cpu(ftrace_sync); 328 /* Now all cpus are using the list ops. */ 329 function_trace_op = set_function_trace_op; 330 /* Make sure the function_trace_op is visible on all CPUs */ 331 smp_wmb(); 332 /* Nasty way to force a rmb on all cpus */ 333 smp_call_function(ftrace_sync_ipi, NULL, 1); 334 /* OK, we are all set to update the ftrace_trace_function now! */ 335#endif /* !CONFIG_DYNAMIC_FTRACE */ 336 337 ftrace_trace_function = func; 338} 339 340int using_ftrace_ops_list_func(void) 341{ 342 return ftrace_trace_function == ftrace_ops_list_func; 343} 344 345static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops) 346{ 347 ops->next = *list; 348 /* 349 * We are entering ops into the list but another 350 * CPU might be walking that list. We need to make sure 351 * the ops->next pointer is valid before another CPU sees 352 * the ops pointer included into the list. 353 */ 354 rcu_assign_pointer(*list, ops); 355} 356 357static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops) 358{ 359 struct ftrace_ops **p; 360 361 /* 362 * If we are removing the last function, then simply point 363 * to the ftrace_stub. 364 */ 365 if (*list == ops && ops->next == &ftrace_list_end) { 366 *list = &ftrace_list_end; 367 return 0; 368 } 369 370 for (p = list; *p != &ftrace_list_end; p = &(*p)->next) 371 if (*p == ops) 372 break; 373 374 if (*p != ops) 375 return -1; 376 377 *p = (*p)->next; 378 return 0; 379} 380 381static void add_ftrace_list_ops(struct ftrace_ops **list, 382 struct ftrace_ops *main_ops, 383 struct ftrace_ops *ops) 384{ 385 int first = *list == &ftrace_list_end; 386 add_ftrace_ops(list, ops); 387 if (first) 388 add_ftrace_ops(&ftrace_ops_list, main_ops); 389} 390 391static int remove_ftrace_list_ops(struct ftrace_ops **list, 392 struct ftrace_ops *main_ops, 393 struct ftrace_ops *ops) 394{ 395 int ret = remove_ftrace_ops(list, ops); 396 if (!ret && *list == &ftrace_list_end) 397 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops); 398 return ret; 399} 400 401static void ftrace_update_trampoline(struct ftrace_ops *ops); 402 403static int __register_ftrace_function(struct ftrace_ops *ops) 404{ 405 if (ops->flags & FTRACE_OPS_FL_DELETED) 406 return -EINVAL; 407 408 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED)) 409 return -EBUSY; 410 411#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS 412 /* 413 * If the ftrace_ops specifies SAVE_REGS, then it only can be used 414 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set. 415 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant. 416 */ 417 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS && 418 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)) 419 return -EINVAL; 420 421 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED) 422 ops->flags |= FTRACE_OPS_FL_SAVE_REGS; 423#endif 424 425 if (!core_kernel_data((unsigned long)ops)) 426 ops->flags |= FTRACE_OPS_FL_DYNAMIC; 427 428 if (ops->flags & FTRACE_OPS_FL_CONTROL) { 429 if (control_ops_alloc(ops)) 430 return -ENOMEM; 431 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops); 432 /* The control_ops needs the trampoline update */ 433 ops = &control_ops; 434 } else 435 add_ftrace_ops(&ftrace_ops_list, ops); 436 437 /* Always save the function, and reset at unregistering */ 438 ops->saved_func = ops->func; 439 440 if (ops->flags & FTRACE_OPS_FL_PID && ftrace_pids_enabled()) 441 ops->func = ftrace_pid_func; 442 443 ftrace_update_trampoline(ops); 444 445 if (ftrace_enabled) 446 update_ftrace_function(); 447 448 return 0; 449} 450 451static int __unregister_ftrace_function(struct ftrace_ops *ops) 452{ 453 int ret; 454 455 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED))) 456 return -EBUSY; 457 458 if (ops->flags & FTRACE_OPS_FL_CONTROL) { 459 ret = remove_ftrace_list_ops(&ftrace_control_list, 460 &control_ops, ops); 461 } else 462 ret = remove_ftrace_ops(&ftrace_ops_list, ops); 463 464 if (ret < 0) 465 return ret; 466 467 if (ftrace_enabled) 468 update_ftrace_function(); 469 470 ops->func = ops->saved_func; 471 472 return 0; 473} 474 475static void ftrace_update_pid_func(void) 476{ 477 bool enabled = ftrace_pids_enabled(); 478 struct ftrace_ops *op; 479 480 /* Only do something if we are tracing something */ 481 if (ftrace_trace_function == ftrace_stub) 482 return; 483 484 do_for_each_ftrace_op(op, ftrace_ops_list) { 485 if (op->flags & FTRACE_OPS_FL_PID) { 486 op->func = enabled ? ftrace_pid_func : 487 op->saved_func; 488 ftrace_update_trampoline(op); 489 } 490 } while_for_each_ftrace_op(op); 491 492 update_ftrace_function(); 493} 494 495#ifdef CONFIG_FUNCTION_PROFILER 496struct ftrace_profile { 497 struct hlist_node node; 498 unsigned long ip; 499 unsigned long counter; 500#ifdef CONFIG_FUNCTION_GRAPH_TRACER 501 unsigned long long time; 502 unsigned long long time_squared; 503#endif 504}; 505 506struct ftrace_profile_page { 507 struct ftrace_profile_page *next; 508 unsigned long index; 509 struct ftrace_profile records[]; 510}; 511 512struct ftrace_profile_stat { 513 atomic_t disabled; 514 struct hlist_head *hash; 515 struct ftrace_profile_page *pages; 516 struct ftrace_profile_page *start; 517 struct tracer_stat stat; 518}; 519 520#define PROFILE_RECORDS_SIZE \ 521 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records)) 522 523#define PROFILES_PER_PAGE \ 524 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile)) 525 526static int ftrace_profile_enabled __read_mostly; 527 528/* ftrace_profile_lock - synchronize the enable and disable of the profiler */ 529static DEFINE_MUTEX(ftrace_profile_lock); 530 531static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats); 532 533#define FTRACE_PROFILE_HASH_BITS 10 534#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS) 535 536static void * 537function_stat_next(void *v, int idx) 538{ 539 struct ftrace_profile *rec = v; 540 struct ftrace_profile_page *pg; 541 542 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK); 543 544 again: 545 if (idx != 0) 546 rec++; 547 548 if ((void *)rec >= (void *)&pg->records[pg->index]) { 549 pg = pg->next; 550 if (!pg) 551 return NULL; 552 rec = &pg->records[0]; 553 if (!rec->counter) 554 goto again; 555 } 556 557 return rec; 558} 559 560static void *function_stat_start(struct tracer_stat *trace) 561{ 562 struct ftrace_profile_stat *stat = 563 container_of(trace, struct ftrace_profile_stat, stat); 564 565 if (!stat || !stat->start) 566 return NULL; 567 568 return function_stat_next(&stat->start->records[0], 0); 569} 570 571#ifdef CONFIG_FUNCTION_GRAPH_TRACER 572/* function graph compares on total time */ 573static int function_stat_cmp(void *p1, void *p2) 574{ 575 struct ftrace_profile *a = p1; 576 struct ftrace_profile *b = p2; 577 578 if (a->time < b->time) 579 return -1; 580 if (a->time > b->time) 581 return 1; 582 else 583 return 0; 584} 585#else 586/* not function graph compares against hits */ 587static int function_stat_cmp(void *p1, void *p2) 588{ 589 struct ftrace_profile *a = p1; 590 struct ftrace_profile *b = p2; 591 592 if (a->counter < b->counter) 593 return -1; 594 if (a->counter > b->counter) 595 return 1; 596 else 597 return 0; 598} 599#endif 600 601static int function_stat_headers(struct seq_file *m) 602{ 603#ifdef CONFIG_FUNCTION_GRAPH_TRACER 604 seq_puts(m, " Function " 605 "Hit Time Avg s^2\n" 606 " -------- " 607 "--- ---- --- ---\n"); 608#else 609 seq_puts(m, " Function Hit\n" 610 " -------- ---\n"); 611#endif 612 return 0; 613} 614 615static int function_stat_show(struct seq_file *m, void *v) 616{ 617 struct ftrace_profile *rec = v; 618 char str[KSYM_SYMBOL_LEN]; 619 int ret = 0; 620#ifdef CONFIG_FUNCTION_GRAPH_TRACER 621 static struct trace_seq s; 622 unsigned long long avg; 623 unsigned long long stddev; 624#endif 625 mutex_lock(&ftrace_profile_lock); 626 627 /* we raced with function_profile_reset() */ 628 if (unlikely(rec->counter == 0)) { 629 ret = -EBUSY; 630 goto out; 631 } 632 633 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str); 634 seq_printf(m, " %-30.30s %10lu", str, rec->counter); 635 636#ifdef CONFIG_FUNCTION_GRAPH_TRACER 637 seq_puts(m, " "); 638 avg = rec->time; 639 do_div(avg, rec->counter); 640 641 /* Sample standard deviation (s^2) */ 642 if (rec->counter <= 1) 643 stddev = 0; 644 else { 645 /* 646 * Apply Welford's method: 647 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2) 648 */ 649 stddev = rec->counter * rec->time_squared - 650 rec->time * rec->time; 651 652 /* 653 * Divide only 1000 for ns^2 -> us^2 conversion. 654 * trace_print_graph_duration will divide 1000 again. 655 */ 656 do_div(stddev, rec->counter * (rec->counter - 1) * 1000); 657 } 658 659 trace_seq_init(&s); 660 trace_print_graph_duration(rec->time, &s); 661 trace_seq_puts(&s, " "); 662 trace_print_graph_duration(avg, &s); 663 trace_seq_puts(&s, " "); 664 trace_print_graph_duration(stddev, &s); 665 trace_print_seq(m, &s); 666#endif 667 seq_putc(m, '\n'); 668out: 669 mutex_unlock(&ftrace_profile_lock); 670 671 return ret; 672} 673 674static void ftrace_profile_reset(struct ftrace_profile_stat *stat) 675{ 676 struct ftrace_profile_page *pg; 677 678 pg = stat->pages = stat->start; 679 680 while (pg) { 681 memset(pg->records, 0, PROFILE_RECORDS_SIZE); 682 pg->index = 0; 683 pg = pg->next; 684 } 685 686 memset(stat->hash, 0, 687 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head)); 688} 689 690int ftrace_profile_pages_init(struct ftrace_profile_stat *stat) 691{ 692 struct ftrace_profile_page *pg; 693 int functions; 694 int pages; 695 int i; 696 697 /* If we already allocated, do nothing */ 698 if (stat->pages) 699 return 0; 700 701 stat->pages = (void *)get_zeroed_page(GFP_KERNEL); 702 if (!stat->pages) 703 return -ENOMEM; 704 705#ifdef CONFIG_DYNAMIC_FTRACE 706 functions = ftrace_update_tot_cnt; 707#else 708 /* 709 * We do not know the number of functions that exist because 710 * dynamic tracing is what counts them. With past experience 711 * we have around 20K functions. That should be more than enough. 712 * It is highly unlikely we will execute every function in 713 * the kernel. 714 */ 715 functions = 20000; 716#endif 717 718 pg = stat->start = stat->pages; 719 720 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE); 721 722 for (i = 1; i < pages; i++) { 723 pg->next = (void *)get_zeroed_page(GFP_KERNEL); 724 if (!pg->next) 725 goto out_free; 726 pg = pg->next; 727 } 728 729 return 0; 730 731 out_free: 732 pg = stat->start; 733 while (pg) { 734 unsigned long tmp = (unsigned long)pg; 735 736 pg = pg->next; 737 free_page(tmp); 738 } 739 740 stat->pages = NULL; 741 stat->start = NULL; 742 743 return -ENOMEM; 744} 745 746static int ftrace_profile_init_cpu(int cpu) 747{ 748 struct ftrace_profile_stat *stat; 749 int size; 750 751 stat = &per_cpu(ftrace_profile_stats, cpu); 752 753 if (stat->hash) { 754 /* If the profile is already created, simply reset it */ 755 ftrace_profile_reset(stat); 756 return 0; 757 } 758 759 /* 760 * We are profiling all functions, but usually only a few thousand 761 * functions are hit. We'll make a hash of 1024 items. 762 */ 763 size = FTRACE_PROFILE_HASH_SIZE; 764 765 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL); 766 767 if (!stat->hash) 768 return -ENOMEM; 769 770 /* Preallocate the function profiling pages */ 771 if (ftrace_profile_pages_init(stat) < 0) { 772 kfree(stat->hash); 773 stat->hash = NULL; 774 return -ENOMEM; 775 } 776 777 return 0; 778} 779 780static int ftrace_profile_init(void) 781{ 782 int cpu; 783 int ret = 0; 784 785 for_each_possible_cpu(cpu) { 786 ret = ftrace_profile_init_cpu(cpu); 787 if (ret) 788 break; 789 } 790 791 return ret; 792} 793 794/* interrupts must be disabled */ 795static struct ftrace_profile * 796ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip) 797{ 798 struct ftrace_profile *rec; 799 struct hlist_head *hhd; 800 unsigned long key; 801 802 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS); 803 hhd = &stat->hash[key]; 804 805 if (hlist_empty(hhd)) 806 return NULL; 807 808 hlist_for_each_entry_rcu_notrace(rec, hhd, node) { 809 if (rec->ip == ip) 810 return rec; 811 } 812 813 return NULL; 814} 815 816static void ftrace_add_profile(struct ftrace_profile_stat *stat, 817 struct ftrace_profile *rec) 818{ 819 unsigned long key; 820 821 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS); 822 hlist_add_head_rcu(&rec->node, &stat->hash[key]); 823} 824 825/* 826 * The memory is already allocated, this simply finds a new record to use. 827 */ 828static struct ftrace_profile * 829ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip) 830{ 831 struct ftrace_profile *rec = NULL; 832 833 /* prevent recursion (from NMIs) */ 834 if (atomic_inc_return(&stat->disabled) != 1) 835 goto out; 836 837 /* 838 * Try to find the function again since an NMI 839 * could have added it 840 */ 841 rec = ftrace_find_profiled_func(stat, ip); 842 if (rec) 843 goto out; 844 845 if (stat->pages->index == PROFILES_PER_PAGE) { 846 if (!stat->pages->next) 847 goto out; 848 stat->pages = stat->pages->next; 849 } 850 851 rec = &stat->pages->records[stat->pages->index++]; 852 rec->ip = ip; 853 ftrace_add_profile(stat, rec); 854 855 out: 856 atomic_dec(&stat->disabled); 857 858 return rec; 859} 860 861static void 862function_profile_call(unsigned long ip, unsigned long parent_ip, 863 struct ftrace_ops *ops, struct pt_regs *regs) 864{ 865 struct ftrace_profile_stat *stat; 866 struct ftrace_profile *rec; 867 unsigned long flags; 868 869 if (!ftrace_profile_enabled) 870 return; 871 872 local_irq_save(flags); 873 874 stat = this_cpu_ptr(&ftrace_profile_stats); 875 if (!stat->hash || !ftrace_profile_enabled) 876 goto out; 877 878 rec = ftrace_find_profiled_func(stat, ip); 879 if (!rec) { 880 rec = ftrace_profile_alloc(stat, ip); 881 if (!rec) 882 goto out; 883 } 884 885 rec->counter++; 886 out: 887 local_irq_restore(flags); 888} 889 890#ifdef CONFIG_FUNCTION_GRAPH_TRACER 891static int profile_graph_entry(struct ftrace_graph_ent *trace) 892{ 893 function_profile_call(trace->func, 0, NULL, NULL); 894 return 1; 895} 896 897static void profile_graph_return(struct ftrace_graph_ret *trace) 898{ 899 struct ftrace_profile_stat *stat; 900 unsigned long long calltime; 901 struct ftrace_profile *rec; 902 unsigned long flags; 903 904 local_irq_save(flags); 905 stat = this_cpu_ptr(&ftrace_profile_stats); 906 if (!stat->hash || !ftrace_profile_enabled) 907 goto out; 908 909 /* If the calltime was zero'd ignore it */ 910 if (!trace->calltime) 911 goto out; 912 913 calltime = trace->rettime - trace->calltime; 914 915 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) { 916 int index; 917 918 index = trace->depth; 919 920 /* Append this call time to the parent time to subtract */ 921 if (index) 922 current->ret_stack[index - 1].subtime += calltime; 923 924 if (current->ret_stack[index].subtime < calltime) 925 calltime -= current->ret_stack[index].subtime; 926 else 927 calltime = 0; 928 } 929 930 rec = ftrace_find_profiled_func(stat, trace->func); 931 if (rec) { 932 rec->time += calltime; 933 rec->time_squared += calltime * calltime; 934 } 935 936 out: 937 local_irq_restore(flags); 938} 939 940static int register_ftrace_profiler(void) 941{ 942 return register_ftrace_graph(&profile_graph_return, 943 &profile_graph_entry); 944} 945 946static void unregister_ftrace_profiler(void) 947{ 948 unregister_ftrace_graph(); 949} 950#else 951static struct ftrace_ops ftrace_profile_ops __read_mostly = { 952 .func = function_profile_call, 953 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED, 954 INIT_OPS_HASH(ftrace_profile_ops) 955}; 956 957static int register_ftrace_profiler(void) 958{ 959 return register_ftrace_function(&ftrace_profile_ops); 960} 961 962static void unregister_ftrace_profiler(void) 963{ 964 unregister_ftrace_function(&ftrace_profile_ops); 965} 966#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ 967 968static ssize_t 969ftrace_profile_write(struct file *filp, const char __user *ubuf, 970 size_t cnt, loff_t *ppos) 971{ 972 unsigned long val; 973 int ret; 974 975 ret = kstrtoul_from_user(ubuf, cnt, 10, &val); 976 if (ret) 977 return ret; 978 979 val = !!val; 980 981 mutex_lock(&ftrace_profile_lock); 982 if (ftrace_profile_enabled ^ val) { 983 if (val) { 984 ret = ftrace_profile_init(); 985 if (ret < 0) { 986 cnt = ret; 987 goto out; 988 } 989 990 ret = register_ftrace_profiler(); 991 if (ret < 0) { 992 cnt = ret; 993 goto out; 994 } 995 ftrace_profile_enabled = 1; 996 } else { 997 ftrace_profile_enabled = 0; 998 /* 999 * unregister_ftrace_profiler calls stop_machine 1000 * so this acts like an synchronize_sched. 1001 */ 1002 unregister_ftrace_profiler(); 1003 } 1004 } 1005 out: 1006 mutex_unlock(&ftrace_profile_lock); 1007 1008 *ppos += cnt; 1009 1010 return cnt; 1011} 1012 1013static ssize_t 1014ftrace_profile_read(struct file *filp, char __user *ubuf, 1015 size_t cnt, loff_t *ppos) 1016{ 1017 char buf[64]; /* big enough to hold a number */ 1018 int r; 1019 1020 r = sprintf(buf, "%u\n", ftrace_profile_enabled); 1021 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); 1022} 1023 1024static const struct file_operations ftrace_profile_fops = { 1025 .open = tracing_open_generic, 1026 .read = ftrace_profile_read, 1027 .write = ftrace_profile_write, 1028 .llseek = default_llseek, 1029}; 1030 1031/* used to initialize the real stat files */ 1032static struct tracer_stat function_stats __initdata = { 1033 .name = "functions", 1034 .stat_start = function_stat_start, 1035 .stat_next = function_stat_next, 1036 .stat_cmp = function_stat_cmp, 1037 .stat_headers = function_stat_headers, 1038 .stat_show = function_stat_show 1039}; 1040 1041static __init void ftrace_profile_tracefs(struct dentry *d_tracer) 1042{ 1043 struct ftrace_profile_stat *stat; 1044 struct dentry *entry; 1045 char *name; 1046 int ret; 1047 int cpu; 1048 1049 for_each_possible_cpu(cpu) { 1050 stat = &per_cpu(ftrace_profile_stats, cpu); 1051 1052 /* allocate enough for function name + cpu number */ 1053 name = kmalloc(32, GFP_KERNEL); 1054 if (!name) { 1055 /* 1056 * The files created are permanent, if something happens 1057 * we still do not free memory. 1058 */ 1059 WARN(1, 1060 "Could not allocate stat file for cpu %d\n", 1061 cpu); 1062 return; 1063 } 1064 stat->stat = function_stats; 1065 snprintf(name, 32, "function%d", cpu); 1066 stat->stat.name = name; 1067 ret = register_stat_tracer(&stat->stat); 1068 if (ret) { 1069 WARN(1, 1070 "Could not register function stat for cpu %d\n", 1071 cpu); 1072 kfree(name); 1073 return; 1074 } 1075 } 1076 1077 entry = tracefs_create_file("function_profile_enabled", 0644, 1078 d_tracer, NULL, &ftrace_profile_fops); 1079 if (!entry) 1080 pr_warning("Could not create tracefs " 1081 "'function_profile_enabled' entry\n"); 1082} 1083 1084#else /* CONFIG_FUNCTION_PROFILER */ 1085static __init void ftrace_profile_tracefs(struct dentry *d_tracer) 1086{ 1087} 1088#endif /* CONFIG_FUNCTION_PROFILER */ 1089 1090static struct pid * const ftrace_swapper_pid = &init_struct_pid; 1091 1092#ifdef CONFIG_FUNCTION_GRAPH_TRACER 1093static int ftrace_graph_active; 1094#else 1095# define ftrace_graph_active 0 1096#endif 1097 1098#ifdef CONFIG_DYNAMIC_FTRACE 1099 1100static struct ftrace_ops *removed_ops; 1101 1102/* 1103 * Set when doing a global update, like enabling all recs or disabling them. 1104 * It is not set when just updating a single ftrace_ops. 1105 */ 1106static bool update_all_ops; 1107 1108#ifndef CONFIG_FTRACE_MCOUNT_RECORD 1109# error Dynamic ftrace depends on MCOUNT_RECORD 1110#endif 1111 1112static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly; 1113 1114struct ftrace_func_probe { 1115 struct hlist_node node; 1116 struct ftrace_probe_ops *ops; 1117 unsigned long flags; 1118 unsigned long ip; 1119 void *data; 1120 struct list_head free_list; 1121}; 1122 1123struct ftrace_func_entry { 1124 struct hlist_node hlist; 1125 unsigned long ip; 1126}; 1127 1128struct ftrace_hash { 1129 unsigned long size_bits; 1130 struct hlist_head *buckets; 1131 unsigned long count; 1132 struct rcu_head rcu; 1133}; 1134 1135/* 1136 * We make these constant because no one should touch them, 1137 * but they are used as the default "empty hash", to avoid allocating 1138 * it all the time. These are in a read only section such that if 1139 * anyone does try to modify it, it will cause an exception. 1140 */ 1141static const struct hlist_head empty_buckets[1]; 1142static const struct ftrace_hash empty_hash = { 1143 .buckets = (struct hlist_head *)empty_buckets, 1144}; 1145#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash) 1146 1147static struct ftrace_ops global_ops = { 1148 .func = ftrace_stub, 1149 .local_hash.notrace_hash = EMPTY_HASH, 1150 .local_hash.filter_hash = EMPTY_HASH, 1151 INIT_OPS_HASH(global_ops) 1152 .flags = FTRACE_OPS_FL_RECURSION_SAFE | 1153 FTRACE_OPS_FL_INITIALIZED | 1154 FTRACE_OPS_FL_PID, 1155}; 1156 1157/* 1158 * This is used by __kernel_text_address() to return true if the 1159 * address is on a dynamically allocated trampoline that would 1160 * not return true for either core_kernel_text() or 1161 * is_module_text_address(). 1162 */ 1163bool is_ftrace_trampoline(unsigned long addr) 1164{ 1165 struct ftrace_ops *op; 1166 bool ret = false; 1167 1168 /* 1169 * Some of the ops may be dynamically allocated, 1170 * they are freed after a synchronize_sched(). 1171 */ 1172 preempt_disable_notrace(); 1173 1174 do_for_each_ftrace_op(op, ftrace_ops_list) { 1175 /* 1176 * This is to check for dynamically allocated trampolines. 1177 * Trampolines that are in kernel text will have 1178 * core_kernel_text() return true. 1179 */ 1180 if (op->trampoline && op->trampoline_size) 1181 if (addr >= op->trampoline && 1182 addr < op->trampoline + op->trampoline_size) { 1183 ret = true; 1184 goto out; 1185 } 1186 } while_for_each_ftrace_op(op); 1187 1188 out: 1189 preempt_enable_notrace(); 1190 1191 return ret; 1192} 1193 1194struct ftrace_page { 1195 struct ftrace_page *next; 1196 struct dyn_ftrace *records; 1197 int index; 1198 int size; 1199}; 1200 1201#define ENTRY_SIZE sizeof(struct dyn_ftrace) 1202#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE) 1203 1204/* estimate from running different kernels */ 1205#define NR_TO_INIT 10000 1206 1207static struct ftrace_page *ftrace_pages_start; 1208static struct ftrace_page *ftrace_pages; 1209 1210static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash) 1211{ 1212 return !hash || !hash->count; 1213} 1214 1215static struct ftrace_func_entry * 1216ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip) 1217{ 1218 unsigned long key; 1219 struct ftrace_func_entry *entry; 1220 struct hlist_head *hhd; 1221 1222 if (ftrace_hash_empty(hash)) 1223 return NULL; 1224 1225 if (hash->size_bits > 0) 1226 key = hash_long(ip, hash->size_bits); 1227 else 1228 key = 0; 1229 1230 hhd = &hash->buckets[key]; 1231 1232 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) { 1233 if (entry->ip == ip) 1234 return entry; 1235 } 1236 return NULL; 1237} 1238 1239static void __add_hash_entry(struct ftrace_hash *hash, 1240 struct ftrace_func_entry *entry) 1241{ 1242 struct hlist_head *hhd; 1243 unsigned long key; 1244 1245 if (hash->size_bits) 1246 key = hash_long(entry->ip, hash->size_bits); 1247 else 1248 key = 0; 1249 1250 hhd = &hash->buckets[key]; 1251 hlist_add_head(&entry->hlist, hhd); 1252 hash->count++; 1253} 1254 1255static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip) 1256{ 1257 struct ftrace_func_entry *entry; 1258 1259 entry = kmalloc(sizeof(*entry), GFP_KERNEL); 1260 if (!entry) 1261 return -ENOMEM; 1262 1263 entry->ip = ip; 1264 __add_hash_entry(hash, entry); 1265 1266 return 0; 1267} 1268 1269static void 1270free_hash_entry(struct ftrace_hash *hash, 1271 struct ftrace_func_entry *entry) 1272{ 1273 hlist_del(&entry->hlist); 1274 kfree(entry); 1275 hash->count--; 1276} 1277 1278static void 1279remove_hash_entry(struct ftrace_hash *hash, 1280 struct ftrace_func_entry *entry) 1281{ 1282 hlist_del(&entry->hlist); 1283 hash->count--; 1284} 1285 1286static void ftrace_hash_clear(struct ftrace_hash *hash) 1287{ 1288 struct hlist_head *hhd; 1289 struct hlist_node *tn; 1290 struct ftrace_func_entry *entry; 1291 int size = 1 << hash->size_bits; 1292 int i; 1293 1294 if (!hash->count) 1295 return; 1296 1297 for (i = 0; i < size; i++) { 1298 hhd = &hash->buckets[i]; 1299 hlist_for_each_entry_safe(entry, tn, hhd, hlist) 1300 free_hash_entry(hash, entry); 1301 } 1302 FTRACE_WARN_ON(hash->count); 1303} 1304 1305static void free_ftrace_hash(struct ftrace_hash *hash) 1306{ 1307 if (!hash || hash == EMPTY_HASH) 1308 return; 1309 ftrace_hash_clear(hash); 1310 kfree(hash->buckets); 1311 kfree(hash); 1312} 1313 1314static void __free_ftrace_hash_rcu(struct rcu_head *rcu) 1315{ 1316 struct ftrace_hash *hash; 1317 1318 hash = container_of(rcu, struct ftrace_hash, rcu); 1319 free_ftrace_hash(hash); 1320} 1321 1322static void free_ftrace_hash_rcu(struct ftrace_hash *hash) 1323{ 1324 if (!hash || hash == EMPTY_HASH) 1325 return; 1326 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu); 1327} 1328 1329void ftrace_free_filter(struct ftrace_ops *ops) 1330{ 1331 ftrace_ops_init(ops); 1332 free_ftrace_hash(ops->func_hash->filter_hash); 1333 free_ftrace_hash(ops->func_hash->notrace_hash); 1334} 1335 1336static struct ftrace_hash *alloc_ftrace_hash(int size_bits) 1337{ 1338 struct ftrace_hash *hash; 1339 int size; 1340 1341 hash = kzalloc(sizeof(*hash), GFP_KERNEL); 1342 if (!hash) 1343 return NULL; 1344 1345 size = 1 << size_bits; 1346 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL); 1347 1348 if (!hash->buckets) { 1349 kfree(hash); 1350 return NULL; 1351 } 1352 1353 hash->size_bits = size_bits; 1354 1355 return hash; 1356} 1357 1358static struct ftrace_hash * 1359alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash) 1360{ 1361 struct ftrace_func_entry *entry; 1362 struct ftrace_hash *new_hash; 1363 int size; 1364 int ret; 1365 int i; 1366 1367 new_hash = alloc_ftrace_hash(size_bits); 1368 if (!new_hash) 1369 return NULL; 1370 1371 /* Empty hash? */ 1372 if (ftrace_hash_empty(hash)) 1373 return new_hash; 1374 1375 size = 1 << hash->size_bits; 1376 for (i = 0; i < size; i++) { 1377 hlist_for_each_entry(entry, &hash->buckets[i], hlist) { 1378 ret = add_hash_entry(new_hash, entry->ip); 1379 if (ret < 0) 1380 goto free_hash; 1381 } 1382 } 1383 1384 FTRACE_WARN_ON(new_hash->count != hash->count); 1385 1386 return new_hash; 1387 1388 free_hash: 1389 free_ftrace_hash(new_hash); 1390 return NULL; 1391} 1392 1393static void 1394ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash); 1395static void 1396ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash); 1397 1398static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops, 1399 struct ftrace_hash *new_hash); 1400 1401static int 1402ftrace_hash_move(struct ftrace_ops *ops, int enable, 1403 struct ftrace_hash **dst, struct ftrace_hash *src) 1404{ 1405 struct ftrace_func_entry *entry; 1406 struct hlist_node *tn; 1407 struct hlist_head *hhd; 1408 struct ftrace_hash *new_hash; 1409 int size = src->count; 1410 int bits = 0; 1411 int ret; 1412 int i; 1413 1414 /* Reject setting notrace hash on IPMODIFY ftrace_ops */ 1415 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable) 1416 return -EINVAL; 1417 1418 /* 1419 * If the new source is empty, just free dst and assign it 1420 * the empty_hash. 1421 */ 1422 if (!src->count) { 1423 new_hash = EMPTY_HASH; 1424 goto update; 1425 } 1426 1427 /* 1428 * Make the hash size about 1/2 the # found 1429 */ 1430 for (size /= 2; size; size >>= 1) 1431 bits++; 1432 1433 /* Don't allocate too much */ 1434 if (bits > FTRACE_HASH_MAX_BITS) 1435 bits = FTRACE_HASH_MAX_BITS; 1436 1437 new_hash = alloc_ftrace_hash(bits); 1438 if (!new_hash) 1439 return -ENOMEM; 1440 1441 size = 1 << src->size_bits; 1442 for (i = 0; i < size; i++) { 1443 hhd = &src->buckets[i]; 1444 hlist_for_each_entry_safe(entry, tn, hhd, hlist) { 1445 remove_hash_entry(src, entry); 1446 __add_hash_entry(new_hash, entry); 1447 } 1448 } 1449 1450update: 1451 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */ 1452 if (enable) { 1453 /* IPMODIFY should be updated only when filter_hash updating */ 1454 ret = ftrace_hash_ipmodify_update(ops, new_hash); 1455 if (ret < 0) { 1456 free_ftrace_hash(new_hash); 1457 return ret; 1458 } 1459 } 1460 1461 /* 1462 * Remove the current set, update the hash and add 1463 * them back. 1464 */ 1465 ftrace_hash_rec_disable_modify(ops, enable); 1466 1467 rcu_assign_pointer(*dst, new_hash); 1468 1469 ftrace_hash_rec_enable_modify(ops, enable); 1470 1471 return 0; 1472} 1473 1474static bool hash_contains_ip(unsigned long ip, 1475 struct ftrace_ops_hash *hash) 1476{ 1477 /* 1478 * The function record is a match if it exists in the filter 1479 * hash and not in the notrace hash. Note, an emty hash is 1480 * considered a match for the filter hash, but an empty 1481 * notrace hash is considered not in the notrace hash. 1482 */ 1483 return (ftrace_hash_empty(hash->filter_hash) || 1484 ftrace_lookup_ip(hash->filter_hash, ip)) && 1485 (ftrace_hash_empty(hash->notrace_hash) || 1486 !ftrace_lookup_ip(hash->notrace_hash, ip)); 1487} 1488 1489/* 1490 * Test the hashes for this ops to see if we want to call 1491 * the ops->func or not. 1492 * 1493 * It's a match if the ip is in the ops->filter_hash or 1494 * the filter_hash does not exist or is empty, 1495 * AND 1496 * the ip is not in the ops->notrace_hash. 1497 * 1498 * This needs to be called with preemption disabled as 1499 * the hashes are freed with call_rcu_sched(). 1500 */ 1501static int 1502ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs) 1503{ 1504 struct ftrace_ops_hash hash; 1505 int ret; 1506 1507#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS 1508 /* 1509 * There's a small race when adding ops that the ftrace handler 1510 * that wants regs, may be called without them. We can not 1511 * allow that handler to be called if regs is NULL. 1512 */ 1513 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS)) 1514 return 0; 1515#endif 1516 1517 hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash); 1518 hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash); 1519 1520 if (hash_contains_ip(ip, &hash)) 1521 ret = 1; 1522 else 1523 ret = 0; 1524 1525 return ret; 1526} 1527 1528/* 1529 * This is a double for. Do not use 'break' to break out of the loop, 1530 * you must use a goto. 1531 */ 1532#define do_for_each_ftrace_rec(pg, rec) \ 1533 for (pg = ftrace_pages_start; pg; pg = pg->next) { \ 1534 int _____i; \ 1535 for (_____i = 0; _____i < pg->index; _____i++) { \ 1536 rec = &pg->records[_____i]; 1537 1538#define while_for_each_ftrace_rec() \ 1539 } \ 1540 } 1541 1542 1543static int ftrace_cmp_recs(const void *a, const void *b) 1544{ 1545 const struct dyn_ftrace *key = a; 1546 const struct dyn_ftrace *rec = b; 1547 1548 if (key->flags < rec->ip) 1549 return -1; 1550 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE) 1551 return 1; 1552 return 0; 1553} 1554 1555static unsigned long ftrace_location_range(unsigned long start, unsigned long end) 1556{ 1557 struct ftrace_page *pg; 1558 struct dyn_ftrace *rec; 1559 struct dyn_ftrace key; 1560 1561 key.ip = start; 1562 key.flags = end; /* overload flags, as it is unsigned long */ 1563 1564 for (pg = ftrace_pages_start; pg; pg = pg->next) { 1565 if (end < pg->records[0].ip || 1566 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE)) 1567 continue; 1568 rec = bsearch(&key, pg->records, pg->index, 1569 sizeof(struct dyn_ftrace), 1570 ftrace_cmp_recs); 1571 if (rec) 1572 return rec->ip; 1573 } 1574 1575 return 0; 1576} 1577 1578/** 1579 * ftrace_location - return true if the ip giving is a traced location 1580 * @ip: the instruction pointer to check 1581 * 1582 * Returns rec->ip if @ip given is a pointer to a ftrace location. 1583 * That is, the instruction that is either a NOP or call to 1584 * the function tracer. It checks the ftrace internal tables to 1585 * determine if the address belongs or not. 1586 */ 1587unsigned long ftrace_location(unsigned long ip) 1588{ 1589 return ftrace_location_range(ip, ip); 1590} 1591 1592/** 1593 * ftrace_text_reserved - return true if range contains an ftrace location 1594 * @start: start of range to search 1595 * @end: end of range to search (inclusive). @end points to the last byte to check. 1596 * 1597 * Returns 1 if @start and @end contains a ftrace location. 1598 * That is, the instruction that is either a NOP or call to 1599 * the function tracer. It checks the ftrace internal tables to 1600 * determine if the address belongs or not. 1601 */ 1602int ftrace_text_reserved(const void *start, const void *end) 1603{ 1604 unsigned long ret; 1605 1606 ret = ftrace_location_range((unsigned long)start, 1607 (unsigned long)end); 1608 1609 return (int)!!ret; 1610} 1611 1612/* Test if ops registered to this rec needs regs */ 1613static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec) 1614{ 1615 struct ftrace_ops *ops; 1616 bool keep_regs = false; 1617 1618 for (ops = ftrace_ops_list; 1619 ops != &ftrace_list_end; ops = ops->next) { 1620 /* pass rec in as regs to have non-NULL val */ 1621 if (ftrace_ops_test(ops, rec->ip, rec)) { 1622 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) { 1623 keep_regs = true; 1624 break; 1625 } 1626 } 1627 } 1628 1629 return keep_regs; 1630} 1631 1632static void __ftrace_hash_rec_update(struct ftrace_ops *ops, 1633 int filter_hash, 1634 bool inc) 1635{ 1636 struct ftrace_hash *hash; 1637 struct ftrace_hash *other_hash; 1638 struct ftrace_page *pg; 1639 struct dyn_ftrace *rec; 1640 int count = 0; 1641 int all = 0; 1642 1643 /* Only update if the ops has been registered */ 1644 if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) 1645 return; 1646 1647 /* 1648 * In the filter_hash case: 1649 * If the count is zero, we update all records. 1650 * Otherwise we just update the items in the hash. 1651 * 1652 * In the notrace_hash case: 1653 * We enable the update in the hash. 1654 * As disabling notrace means enabling the tracing, 1655 * and enabling notrace means disabling, the inc variable 1656 * gets inversed. 1657 */ 1658 if (filter_hash) { 1659 hash = ops->func_hash->filter_hash; 1660 other_hash = ops->func_hash->notrace_hash; 1661 if (ftrace_hash_empty(hash)) 1662 all = 1; 1663 } else { 1664 inc = !inc; 1665 hash = ops->func_hash->notrace_hash; 1666 other_hash = ops->func_hash->filter_hash; 1667 /* 1668 * If the notrace hash has no items, 1669 * then there's nothing to do. 1670 */ 1671 if (ftrace_hash_empty(hash)) 1672 return; 1673 } 1674 1675 do_for_each_ftrace_rec(pg, rec) { 1676 int in_other_hash = 0; 1677 int in_hash = 0; 1678 int match = 0; 1679 1680 if (all) { 1681 /* 1682 * Only the filter_hash affects all records. 1683 * Update if the record is not in the notrace hash. 1684 */ 1685 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip)) 1686 match = 1; 1687 } else { 1688 in_hash = !!ftrace_lookup_ip(hash, rec->ip); 1689 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip); 1690 1691 /* 1692 * If filter_hash is set, we want to match all functions 1693 * that are in the hash but not in the other hash. 1694 * 1695 * If filter_hash is not set, then we are decrementing. 1696 * That means we match anything that is in the hash 1697 * and also in the other_hash. That is, we need to turn 1698 * off functions in the other hash because they are disabled 1699 * by this hash. 1700 */ 1701 if (filter_hash && in_hash && !in_other_hash) 1702 match = 1; 1703 else if (!filter_hash && in_hash && 1704 (in_other_hash || ftrace_hash_empty(other_hash))) 1705 match = 1; 1706 } 1707 if (!match) 1708 continue; 1709 1710 if (inc) { 1711 rec->flags++; 1712 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX)) 1713 return; 1714 1715 /* 1716 * If there's only a single callback registered to a 1717 * function, and the ops has a trampoline registered 1718 * for it, then we can call it directly. 1719 */ 1720 if (ftrace_rec_count(rec) == 1 && ops->trampoline) 1721 rec->flags |= FTRACE_FL_TRAMP; 1722 else 1723 /* 1724 * If we are adding another function callback 1725 * to this function, and the previous had a 1726 * custom trampoline in use, then we need to go 1727 * back to the default trampoline. 1728 */ 1729 rec->flags &= ~FTRACE_FL_TRAMP; 1730 1731 /* 1732 * If any ops wants regs saved for this function 1733 * then all ops will get saved regs. 1734 */ 1735 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) 1736 rec->flags |= FTRACE_FL_REGS; 1737 } else { 1738 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0)) 1739 return; 1740 rec->flags--; 1741 1742 /* 1743 * If the rec had REGS enabled and the ops that is 1744 * being removed had REGS set, then see if there is 1745 * still any ops for this record that wants regs. 1746 * If not, we can stop recording them. 1747 */ 1748 if (ftrace_rec_count(rec) > 0 && 1749 rec->flags & FTRACE_FL_REGS && 1750 ops->flags & FTRACE_OPS_FL_SAVE_REGS) { 1751 if (!test_rec_ops_needs_regs(rec)) 1752 rec->flags &= ~FTRACE_FL_REGS; 1753 } 1754 1755 /* 1756 * If the rec had TRAMP enabled, then it needs to 1757 * be cleared. As TRAMP can only be enabled iff 1758 * there is only a single ops attached to it. 1759 * In otherwords, always disable it on decrementing. 1760 * In the future, we may set it if rec count is 1761 * decremented to one, and the ops that is left 1762 * has a trampoline. 1763 */ 1764 rec->flags &= ~FTRACE_FL_TRAMP; 1765 1766 /* 1767 * flags will be cleared in ftrace_check_record() 1768 * if rec count is zero. 1769 */ 1770 } 1771 count++; 1772 /* Shortcut, if we handled all records, we are done. */ 1773 if (!all && count == hash->count) 1774 return; 1775 } while_for_each_ftrace_rec(); 1776} 1777 1778static void ftrace_hash_rec_disable(struct ftrace_ops *ops, 1779 int filter_hash) 1780{ 1781 __ftrace_hash_rec_update(ops, filter_hash, 0); 1782} 1783 1784static void ftrace_hash_rec_enable(struct ftrace_ops *ops, 1785 int filter_hash) 1786{ 1787 __ftrace_hash_rec_update(ops, filter_hash, 1); 1788} 1789 1790static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops, 1791 int filter_hash, int inc) 1792{ 1793 struct ftrace_ops *op; 1794 1795 __ftrace_hash_rec_update(ops, filter_hash, inc); 1796 1797 if (ops->func_hash != &global_ops.local_hash) 1798 return; 1799 1800 /* 1801 * If the ops shares the global_ops hash, then we need to update 1802 * all ops that are enabled and use this hash. 1803 */ 1804 do_for_each_ftrace_op(op, ftrace_ops_list) { 1805 /* Already done */ 1806 if (op == ops) 1807 continue; 1808 if (op->func_hash == &global_ops.local_hash) 1809 __ftrace_hash_rec_update(op, filter_hash, inc); 1810 } while_for_each_ftrace_op(op); 1811} 1812 1813static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, 1814 int filter_hash) 1815{ 1816 ftrace_hash_rec_update_modify(ops, filter_hash, 0); 1817} 1818 1819static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, 1820 int filter_hash) 1821{ 1822 ftrace_hash_rec_update_modify(ops, filter_hash, 1); 1823} 1824 1825/* 1826 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK 1827 * or no-needed to update, -EBUSY if it detects a conflict of the flag 1828 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs. 1829 * Note that old_hash and new_hash has below meanings 1830 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected) 1831 * - If the hash is EMPTY_HASH, it hits nothing 1832 * - Anything else hits the recs which match the hash entries. 1833 */ 1834static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops, 1835 struct ftrace_hash *old_hash, 1836 struct ftrace_hash *new_hash) 1837{ 1838 struct ftrace_page *pg; 1839 struct dyn_ftrace *rec, *end = NULL; 1840 int in_old, in_new; 1841 1842 /* Only update if the ops has been registered */ 1843 if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) 1844 return 0; 1845 1846 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY)) 1847 return 0; 1848 1849 /* 1850 * Since the IPMODIFY is a very address sensitive action, we do not 1851 * allow ftrace_ops to set all functions to new hash. 1852 */ 1853 if (!new_hash || !old_hash) 1854 return -EINVAL; 1855 1856 /* Update rec->flags */ 1857 do_for_each_ftrace_rec(pg, rec) { 1858 /* We need to update only differences of filter_hash */ 1859 in_old = !!ftrace_lookup_ip(old_hash, rec->ip); 1860 in_new = !!ftrace_lookup_ip(new_hash, rec->ip); 1861 if (in_old == in_new) 1862 continue; 1863 1864 if (in_new) { 1865 /* New entries must ensure no others are using it */ 1866 if (rec->flags & FTRACE_FL_IPMODIFY) 1867 goto rollback; 1868 rec->flags |= FTRACE_FL_IPMODIFY; 1869 } else /* Removed entry */ 1870 rec->flags &= ~FTRACE_FL_IPMODIFY; 1871 } while_for_each_ftrace_rec(); 1872 1873 return 0; 1874 1875rollback: 1876 end = rec; 1877 1878 /* Roll back what we did above */ 1879 do_for_each_ftrace_rec(pg, rec) { 1880 if (rec == end) 1881 goto err_out; 1882 1883 in_old = !!ftrace_lookup_ip(old_hash, rec->ip); 1884 in_new = !!ftrace_lookup_ip(new_hash, rec->ip); 1885 if (in_old == in_new) 1886 continue; 1887 1888 if (in_new) 1889 rec->flags &= ~FTRACE_FL_IPMODIFY; 1890 else 1891 rec->flags |= FTRACE_FL_IPMODIFY; 1892 } while_for_each_ftrace_rec(); 1893 1894err_out: 1895 return -EBUSY; 1896} 1897 1898static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops) 1899{ 1900 struct ftrace_hash *hash = ops->func_hash->filter_hash; 1901 1902 if (ftrace_hash_empty(hash)) 1903 hash = NULL; 1904 1905 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash); 1906} 1907 1908/* Disabling always succeeds */ 1909static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops) 1910{ 1911 struct ftrace_hash *hash = ops->func_hash->filter_hash; 1912 1913 if (ftrace_hash_empty(hash)) 1914 hash = NULL; 1915 1916 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH); 1917} 1918 1919static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops, 1920 struct ftrace_hash *new_hash) 1921{ 1922 struct ftrace_hash *old_hash = ops->func_hash->filter_hash; 1923 1924 if (ftrace_hash_empty(old_hash)) 1925 old_hash = NULL; 1926 1927 if (ftrace_hash_empty(new_hash)) 1928 new_hash = NULL; 1929 1930 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash); 1931} 1932 1933static void print_ip_ins(const char *fmt, unsigned char *p) 1934{ 1935 int i; 1936 1937 printk(KERN_CONT "%s", fmt); 1938 1939 for (i = 0; i < MCOUNT_INSN_SIZE; i++) 1940 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]); 1941} 1942 1943static struct ftrace_ops * 1944ftrace_find_tramp_ops_any(struct dyn_ftrace *rec); 1945 1946/** 1947 * ftrace_bug - report and shutdown function tracer 1948 * @failed: The failed type (EFAULT, EINVAL, EPERM) 1949 * @rec: The record that failed 1950 * 1951 * The arch code that enables or disables the function tracing 1952 * can call ftrace_bug() when it has detected a problem in 1953 * modifying the code. @failed should be one of either: 1954 * EFAULT - if the problem happens on reading the @ip address 1955 * EINVAL - if what is read at @ip is not what was expected 1956 * EPERM - if the problem happens on writting to the @ip address 1957 */ 1958void ftrace_bug(int failed, struct dyn_ftrace *rec) 1959{ 1960 unsigned long ip = rec ? rec->ip : 0; 1961 1962 switch (failed) { 1963 case -EFAULT: 1964 FTRACE_WARN_ON_ONCE(1); 1965 pr_info("ftrace faulted on modifying "); 1966 print_ip_sym(ip); 1967 break; 1968 case -EINVAL: 1969 FTRACE_WARN_ON_ONCE(1); 1970 pr_info("ftrace failed to modify "); 1971 print_ip_sym(ip); 1972 print_ip_ins(" actual: ", (unsigned char *)ip); 1973 pr_cont("\n"); 1974 break; 1975 case -EPERM: 1976 FTRACE_WARN_ON_ONCE(1); 1977 pr_info("ftrace faulted on writing "); 1978 print_ip_sym(ip); 1979 break; 1980 default: 1981 FTRACE_WARN_ON_ONCE(1); 1982 pr_info("ftrace faulted on unknown error "); 1983 print_ip_sym(ip); 1984 } 1985 if (rec) { 1986 struct ftrace_ops *ops = NULL; 1987 1988 pr_info("ftrace record flags: %lx\n", rec->flags); 1989 pr_cont(" (%ld)%s", ftrace_rec_count(rec), 1990 rec->flags & FTRACE_FL_REGS ? " R" : " "); 1991 if (rec->flags & FTRACE_FL_TRAMP_EN) { 1992 ops = ftrace_find_tramp_ops_any(rec); 1993 if (ops) 1994 pr_cont("\ttramp: %pS", 1995 (void *)ops->trampoline); 1996 else 1997 pr_cont("\ttramp: ERROR!"); 1998 1999 } 2000 ip = ftrace_get_addr_curr(rec); 2001 pr_cont(" expected tramp: %lx\n", ip); 2002 } 2003} 2004 2005static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update) 2006{ 2007 unsigned long flag = 0UL; 2008 2009 /* 2010 * If we are updating calls: 2011 * 2012 * If the record has a ref count, then we need to enable it 2013 * because someone is using it. 2014 * 2015 * Otherwise we make sure its disabled. 2016 * 2017 * If we are disabling calls, then disable all records that 2018 * are enabled. 2019 */ 2020 if (enable && ftrace_rec_count(rec)) 2021 flag = FTRACE_FL_ENABLED; 2022 2023 /* 2024 * If enabling and the REGS flag does not match the REGS_EN, or 2025 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore 2026 * this record. Set flags to fail the compare against ENABLED. 2027 */ 2028 if (flag) { 2029 if (!(rec->flags & FTRACE_FL_REGS) != 2030 !(rec->flags & FTRACE_FL_REGS_EN)) 2031 flag |= FTRACE_FL_REGS; 2032 2033 if (!(rec->flags & FTRACE_FL_TRAMP) != 2034 !(rec->flags & FTRACE_FL_TRAMP_EN)) 2035 flag |= FTRACE_FL_TRAMP; 2036 } 2037 2038 /* If the state of this record hasn't changed, then do nothing */ 2039 if ((rec->flags & FTRACE_FL_ENABLED) == flag) 2040 return FTRACE_UPDATE_IGNORE; 2041 2042 if (flag) { 2043 /* Save off if rec is being enabled (for return value) */ 2044 flag ^= rec->flags & FTRACE_FL_ENABLED; 2045 2046 if (update) { 2047 rec->flags |= FTRACE_FL_ENABLED; 2048 if (flag & FTRACE_FL_REGS) { 2049 if (rec->flags & FTRACE_FL_REGS) 2050 rec->flags |= FTRACE_FL_REGS_EN; 2051 else 2052 rec->flags &= ~FTRACE_FL_REGS_EN; 2053 } 2054 if (flag & FTRACE_FL_TRAMP) { 2055 if (rec->flags & FTRACE_FL_TRAMP) 2056 rec->flags |= FTRACE_FL_TRAMP_EN; 2057 else 2058 rec->flags &= ~FTRACE_FL_TRAMP_EN; 2059 } 2060 } 2061 2062 /* 2063 * If this record is being updated from a nop, then 2064 * return UPDATE_MAKE_CALL. 2065 * Otherwise, 2066 * return UPDATE_MODIFY_CALL to tell the caller to convert 2067 * from the save regs, to a non-save regs function or 2068 * vice versa, or from a trampoline call. 2069 */ 2070 if (flag & FTRACE_FL_ENABLED) 2071 return FTRACE_UPDATE_MAKE_CALL; 2072 2073 return FTRACE_UPDATE_MODIFY_CALL; 2074 } 2075 2076 if (update) { 2077 /* If there's no more users, clear all flags */ 2078 if (!ftrace_rec_count(rec)) 2079 rec->flags = 0; 2080 else 2081 /* 2082 * Just disable the record, but keep the ops TRAMP 2083 * and REGS states. The _EN flags must be disabled though. 2084 */ 2085 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN | 2086 FTRACE_FL_REGS_EN); 2087 } 2088 2089 return FTRACE_UPDATE_MAKE_NOP; 2090} 2091 2092/** 2093 * ftrace_update_record, set a record that now is tracing or not 2094 * @rec: the record to update 2095 * @enable: set to 1 if the record is tracing, zero to force disable 2096 * 2097 * The records that represent all functions that can be traced need 2098 * to be updated when tracing has been enabled. 2099 */ 2100int ftrace_update_record(struct dyn_ftrace *rec, int enable) 2101{ 2102 return ftrace_check_record(rec, enable, 1); 2103} 2104 2105/** 2106 * ftrace_test_record, check if the record has been enabled or not 2107 * @rec: the record to test 2108 * @enable: set to 1 to check if enabled, 0 if it is disabled 2109 * 2110 * The arch code may need to test if a record is already set to 2111 * tracing to determine how to modify the function code that it 2112 * represents. 2113 */ 2114int ftrace_test_record(struct dyn_ftrace *rec, int enable) 2115{ 2116 return ftrace_check_record(rec, enable, 0); 2117} 2118 2119static struct ftrace_ops * 2120ftrace_find_tramp_ops_any(struct dyn_ftrace *rec) 2121{ 2122 struct ftrace_ops *op; 2123 unsigned long ip = rec->ip; 2124 2125 do_for_each_ftrace_op(op, ftrace_ops_list) { 2126 2127 if (!op->trampoline) 2128 continue; 2129 2130 if (hash_contains_ip(ip, op->func_hash)) 2131 return op; 2132 } while_for_each_ftrace_op(op); 2133 2134 return NULL; 2135} 2136 2137static struct ftrace_ops * 2138ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec) 2139{ 2140 struct ftrace_ops *op; 2141 unsigned long ip = rec->ip; 2142 2143 /* 2144 * Need to check removed ops first. 2145 * If they are being removed, and this rec has a tramp, 2146 * and this rec is in the ops list, then it would be the 2147 * one with the tramp. 2148 */ 2149 if (removed_ops) { 2150 if (hash_contains_ip(ip, &removed_ops->old_hash)) 2151 return removed_ops; 2152 } 2153 2154 /* 2155 * Need to find the current trampoline for a rec. 2156 * Now, a trampoline is only attached to a rec if there 2157 * was a single 'ops' attached to it. But this can be called 2158 * when we are adding another op to the rec or removing the 2159 * current one. Thus, if the op is being added, we can 2160 * ignore it because it hasn't attached itself to the rec 2161 * yet. 2162 * 2163 * If an ops is being modified (hooking to different functions) 2164 * then we don't care about the new functions that are being 2165 * added, just the old ones (that are probably being removed). 2166 * 2167 * If we are adding an ops to a function that already is using 2168 * a trampoline, it needs to be removed (trampolines are only 2169 * for single ops connected), then an ops that is not being 2170 * modified also needs to be checked. 2171 */ 2172 do_for_each_ftrace_op(op, ftrace_ops_list) { 2173 2174 if (!op->trampoline) 2175 continue; 2176 2177 /* 2178 * If the ops is being added, it hasn't gotten to 2179 * the point to be removed from this tree yet. 2180 */ 2181 if (op->flags & FTRACE_OPS_FL_ADDING) 2182 continue; 2183 2184 2185 /* 2186 * If the ops is being modified and is in the old 2187 * hash, then it is probably being removed from this 2188 * function. 2189 */ 2190 if ((op->flags & FTRACE_OPS_FL_MODIFYING) && 2191 hash_contains_ip(ip, &op->old_hash)) 2192 return op; 2193 /* 2194 * If the ops is not being added or modified, and it's 2195 * in its normal filter hash, then this must be the one 2196 * we want! 2197 */ 2198 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) && 2199 hash_contains_ip(ip, op->func_hash)) 2200 return op; 2201 2202 } while_for_each_ftrace_op(op); 2203 2204 return NULL; 2205} 2206 2207static struct ftrace_ops * 2208ftrace_find_tramp_ops_new(struct dyn_ftrace *rec) 2209{ 2210 struct ftrace_ops *op; 2211 unsigned long ip = rec->ip; 2212 2213 do_for_each_ftrace_op(op, ftrace_ops_list) { 2214 /* pass rec in as regs to have non-NULL val */ 2215 if (hash_contains_ip(ip, op->func_hash)) 2216 return op; 2217 } while_for_each_ftrace_op(op); 2218 2219 return NULL; 2220} 2221 2222/** 2223 * ftrace_get_addr_new - Get the call address to set to 2224 * @rec: The ftrace record descriptor 2225 * 2226 * If the record has the FTRACE_FL_REGS set, that means that it 2227 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS 2228 * is not not set, then it wants to convert to the normal callback. 2229 * 2230 * Returns the address of the trampoline to set to 2231 */ 2232unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec) 2233{ 2234 struct ftrace_ops *ops; 2235 2236 /* Trampolines take precedence over regs */ 2237 if (rec->flags & FTRACE_FL_TRAMP) { 2238 ops = ftrace_find_tramp_ops_new(rec); 2239 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) { 2240 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n", 2241 (void *)rec->ip, (void *)rec->ip, rec->flags); 2242 /* Ftrace is shutting down, return anything */ 2243 return (unsigned long)FTRACE_ADDR; 2244 } 2245 return ops->trampoline; 2246 } 2247 2248 if (rec->flags & FTRACE_FL_REGS) 2249 return (unsigned long)FTRACE_REGS_ADDR; 2250 else 2251 return (unsigned long)FTRACE_ADDR; 2252} 2253 2254/** 2255 * ftrace_get_addr_curr - Get the call address that is already there 2256 * @rec: The ftrace record descriptor 2257 * 2258 * The FTRACE_FL_REGS_EN is set when the record already points to 2259 * a function that saves all the regs. Basically the '_EN' version 2260 * represents the current state of the function. 2261 * 2262 * Returns the address of the trampoline that is currently being called 2263 */ 2264unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec) 2265{ 2266 struct ftrace_ops *ops; 2267 2268 /* Trampolines take precedence over regs */ 2269 if (rec->flags & FTRACE_FL_TRAMP_EN) { 2270 ops = ftrace_find_tramp_ops_curr(rec); 2271 if (FTRACE_WARN_ON(!ops)) { 2272 pr_warning("Bad trampoline accounting at: %p (%pS)\n", 2273 (void *)rec->ip, (void *)rec->ip); 2274 /* Ftrace is shutting down, return anything */ 2275 return (unsigned long)FTRACE_ADDR; 2276 } 2277 return ops->trampoline; 2278 } 2279 2280 if (rec->flags & FTRACE_FL_REGS_EN) 2281 return (unsigned long)FTRACE_REGS_ADDR; 2282 else 2283 return (unsigned long)FTRACE_ADDR; 2284} 2285 2286static int 2287__ftrace_replace_code(struct dyn_ftrace *rec, int enable) 2288{ 2289 unsigned long ftrace_old_addr; 2290 unsigned long ftrace_addr; 2291 int ret; 2292 2293 ftrace_addr = ftrace_get_addr_new(rec); 2294 2295 /* This needs to be done before we call ftrace_update_record */ 2296 ftrace_old_addr = ftrace_get_addr_curr(rec); 2297 2298 ret = ftrace_update_record(rec, enable); 2299 2300 switch (ret) { 2301 case FTRACE_UPDATE_IGNORE: 2302 return 0; 2303 2304 case FTRACE_UPDATE_MAKE_CALL: 2305 return ftrace_make_call(rec, ftrace_addr); 2306 2307 case FTRACE_UPDATE_MAKE_NOP: 2308 return ftrace_make_nop(NULL, rec, ftrace_old_addr); 2309 2310 case FTRACE_UPDATE_MODIFY_CALL: 2311 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr); 2312 } 2313 2314 return -1; /* unknow ftrace bug */ 2315} 2316 2317void __weak ftrace_replace_code(int enable) 2318{ 2319 struct dyn_ftrace *rec; 2320 struct ftrace_page *pg; 2321 int failed; 2322 2323 if (unlikely(ftrace_disabled)) 2324 return; 2325 2326 do_for_each_ftrace_rec(pg, rec) { 2327 failed = __ftrace_replace_code(rec, enable); 2328 if (failed) { 2329 ftrace_bug(failed, rec); 2330 /* Stop processing */ 2331 return; 2332 } 2333 } while_for_each_ftrace_rec(); 2334} 2335 2336struct ftrace_rec_iter { 2337 struct ftrace_page *pg; 2338 int index; 2339}; 2340 2341/** 2342 * ftrace_rec_iter_start, start up iterating over traced functions 2343 * 2344 * Returns an iterator handle that is used to iterate over all 2345 * the records that represent address locations where functions 2346 * are traced. 2347 * 2348 * May return NULL if no records are available. 2349 */ 2350struct ftrace_rec_iter *ftrace_rec_iter_start(void) 2351{ 2352 /* 2353 * We only use a single iterator. 2354 * Protected by the ftrace_lock mutex. 2355 */ 2356 static struct ftrace_rec_iter ftrace_rec_iter; 2357 struct ftrace_rec_iter *iter = &ftrace_rec_iter; 2358 2359 iter->pg = ftrace_pages_start; 2360 iter->index = 0; 2361 2362 /* Could have empty pages */ 2363 while (iter->pg && !iter->pg->index) 2364 iter->pg = iter->pg->next; 2365 2366 if (!iter->pg) 2367 return NULL; 2368 2369 return iter; 2370} 2371 2372/** 2373 * ftrace_rec_iter_next, get the next record to process. 2374 * @iter: The handle to the iterator. 2375 * 2376 * Returns the next iterator after the given iterator @iter. 2377 */ 2378struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter) 2379{ 2380 iter->index++; 2381 2382 if (iter->index >= iter->pg->index) { 2383 iter->pg = iter->pg->next; 2384 iter->index = 0; 2385 2386 /* Could have empty pages */ 2387 while (iter->pg && !iter->pg->index) 2388 iter->pg = iter->pg->next; 2389 } 2390 2391 if (!iter->pg) 2392 return NULL; 2393 2394 return iter; 2395} 2396 2397/** 2398 * ftrace_rec_iter_record, get the record at the iterator location 2399 * @iter: The current iterator location 2400 * 2401 * Returns the record that the current @iter is at. 2402 */ 2403struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter) 2404{ 2405 return &iter->pg->records[iter->index]; 2406} 2407 2408static int 2409ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec) 2410{ 2411 int ret; 2412 2413 if (unlikely(ftrace_disabled)) 2414 return 0; 2415 2416 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR); 2417 if (ret) { 2418 ftrace_bug(ret, rec); 2419 return 0; 2420 } 2421 return 1; 2422} 2423 2424/* 2425 * archs can override this function if they must do something 2426 * before the modifying code is performed. 2427 */ 2428int __weak ftrace_arch_code_modify_prepare(void) 2429{ 2430 return 0; 2431} 2432 2433/* 2434 * archs can override this function if they must do something 2435 * after the modifying code is performed. 2436 */ 2437int __weak ftrace_arch_code_modify_post_process(void) 2438{ 2439 return 0; 2440} 2441 2442void ftrace_modify_all_code(int command) 2443{ 2444 int update = command & FTRACE_UPDATE_TRACE_FUNC; 2445 int err = 0; 2446 2447 /* 2448 * If the ftrace_caller calls a ftrace_ops func directly, 2449 * we need to make sure that it only traces functions it 2450 * expects to trace. When doing the switch of functions, 2451 * we need to update to the ftrace_ops_list_func first 2452 * before the transition between old and new calls are set, 2453 * as the ftrace_ops_list_func will check the ops hashes 2454 * to make sure the ops are having the right functions 2455 * traced. 2456 */ 2457 if (update) { 2458 err = ftrace_update_ftrace_func(ftrace_ops_list_func); 2459 if (FTRACE_WARN_ON(err)) 2460 return; 2461 } 2462 2463 if (command & FTRACE_UPDATE_CALLS) 2464 ftrace_replace_code(1); 2465 else if (command & FTRACE_DISABLE_CALLS) 2466 ftrace_replace_code(0); 2467 2468 if (update && ftrace_trace_function != ftrace_ops_list_func) { 2469 function_trace_op = set_function_trace_op; 2470 smp_wmb(); 2471 /* If irqs are disabled, we are in stop machine */ 2472 if (!irqs_disabled()) 2473 smp_call_function(ftrace_sync_ipi, NULL, 1); 2474 err = ftrace_update_ftrace_func(ftrace_trace_function); 2475 if (FTRACE_WARN_ON(err)) 2476 return; 2477 } 2478 2479 if (command & FTRACE_START_FUNC_RET) 2480 err = ftrace_enable_ftrace_graph_caller(); 2481 else if (command & FTRACE_STOP_FUNC_RET) 2482 err = ftrace_disable_ftrace_graph_caller(); 2483 FTRACE_WARN_ON(err); 2484} 2485 2486static int __ftrace_modify_code(void *data) 2487{ 2488 int *command = data; 2489 2490 ftrace_modify_all_code(*command); 2491 2492 return 0; 2493} 2494 2495/** 2496 * ftrace_run_stop_machine, go back to the stop machine method 2497 * @command: The command to tell ftrace what to do 2498 * 2499 * If an arch needs to fall back to the stop machine method, the 2500 * it can call this function. 2501 */ 2502void ftrace_run_stop_machine(int command) 2503{ 2504 stop_machine(__ftrace_modify_code, &command, NULL); 2505} 2506 2507/** 2508 * arch_ftrace_update_code, modify the code to trace or not trace 2509 * @command: The command that needs to be done 2510 * 2511 * Archs can override this function if it does not need to 2512 * run stop_machine() to modify code. 2513 */ 2514void __weak arch_ftrace_update_code(int command) 2515{ 2516 ftrace_run_stop_machine(command); 2517} 2518 2519static void ftrace_run_update_code(int command) 2520{ 2521 int ret; 2522 2523 ret = ftrace_arch_code_modify_prepare(); 2524 FTRACE_WARN_ON(ret); 2525 if (ret) 2526 return; 2527 2528 /* 2529 * By default we use stop_machine() to modify the code. 2530 * But archs can do what ever they want as long as it 2531 * is safe. The stop_machine() is the safest, but also 2532 * produces the most overhead. 2533 */ 2534 arch_ftrace_update_code(command); 2535 2536 ret = ftrace_arch_code_modify_post_process(); 2537 FTRACE_WARN_ON(ret); 2538} 2539 2540static void ftrace_run_modify_code(struct ftrace_ops *ops, int command, 2541 struct ftrace_ops_hash *old_hash) 2542{ 2543 ops->flags |= FTRACE_OPS_FL_MODIFYING; 2544 ops->old_hash.filter_hash = old_hash->filter_hash; 2545 ops->old_hash.notrace_hash = old_hash->notrace_hash; 2546 ftrace_run_update_code(command); 2547 ops->old_hash.filter_hash = NULL; 2548 ops->old_hash.notrace_hash = NULL; 2549 ops->flags &= ~FTRACE_OPS_FL_MODIFYING; 2550} 2551 2552static ftrace_func_t saved_ftrace_func; 2553static int ftrace_start_up; 2554 2555void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops) 2556{ 2557} 2558 2559static void control_ops_free(struct ftrace_ops *ops) 2560{ 2561 free_percpu(ops->disabled); 2562} 2563 2564static void ftrace_startup_enable(int command) 2565{ 2566 if (saved_ftrace_func != ftrace_trace_function) { 2567 saved_ftrace_func = ftrace_trace_function; 2568 command |= FTRACE_UPDATE_TRACE_FUNC; 2569 } 2570 2571 if (!command || !ftrace_enabled) 2572 return; 2573 2574 ftrace_run_update_code(command); 2575} 2576 2577static void ftrace_startup_all(int command) 2578{ 2579 update_all_ops = true; 2580 ftrace_startup_enable(command); 2581 update_all_ops = false; 2582} 2583 2584static int ftrace_startup(struct ftrace_ops *ops, int command) 2585{ 2586 int ret; 2587 2588 if (unlikely(ftrace_disabled)) 2589 return -ENODEV; 2590 2591 ret = __register_ftrace_function(ops); 2592 if (ret) 2593 return ret; 2594 2595 ftrace_start_up++; 2596 command |= FTRACE_UPDATE_CALLS; 2597 2598 /* 2599 * Note that ftrace probes uses this to start up 2600 * and modify functions it will probe. But we still 2601 * set the ADDING flag for modification, as probes 2602 * do not have trampolines. If they add them in the 2603 * future, then the probes will need to distinguish 2604 * between adding and updating probes. 2605 */ 2606 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING; 2607 2608 ret = ftrace_hash_ipmodify_enable(ops); 2609 if (ret < 0) { 2610 /* Rollback registration process */ 2611 __unregister_ftrace_function(ops); 2612 ftrace_start_up--; 2613 ops->flags &= ~FTRACE_OPS_FL_ENABLED; 2614 return ret; 2615 } 2616 2617 ftrace_hash_rec_enable(ops, 1); 2618 2619 ftrace_startup_enable(command); 2620 2621 ops->flags &= ~FTRACE_OPS_FL_ADDING; 2622 2623 return 0; 2624} 2625 2626static int ftrace_shutdown(struct ftrace_ops *ops, int command) 2627{ 2628 int ret; 2629 2630 if (unlikely(ftrace_disabled)) 2631 return -ENODEV; 2632 2633 ret = __unregister_ftrace_function(ops); 2634 if (ret) 2635 return ret; 2636 2637 ftrace_start_up--; 2638 /* 2639 * Just warn in case of unbalance, no need to kill ftrace, it's not 2640 * critical but the ftrace_call callers may be never nopped again after 2641 * further ftrace uses. 2642 */ 2643 WARN_ON_ONCE(ftrace_start_up < 0); 2644 2645 /* Disabling ipmodify never fails */ 2646 ftrace_hash_ipmodify_disable(ops); 2647 ftrace_hash_rec_disable(ops, 1); 2648 2649 ops->flags &= ~FTRACE_OPS_FL_ENABLED; 2650 2651 command |= FTRACE_UPDATE_CALLS; 2652 2653 if (saved_ftrace_func != ftrace_trace_function) { 2654 saved_ftrace_func = ftrace_trace_function; 2655 command |= FTRACE_UPDATE_TRACE_FUNC; 2656 } 2657 2658 if (!command || !ftrace_enabled) { 2659 /* 2660 * If these are control ops, they still need their 2661 * per_cpu field freed. Since, function tracing is 2662 * not currently active, we can just free them 2663 * without synchronizing all CPUs. 2664 */ 2665 if (ops->flags & FTRACE_OPS_FL_CONTROL) 2666 control_ops_free(ops); 2667 return 0; 2668 } 2669 2670 /* 2671 * If the ops uses a trampoline, then it needs to be 2672 * tested first on update. 2673 */ 2674 ops->flags |= FTRACE_OPS_FL_REMOVING; 2675 removed_ops = ops; 2676 2677 /* The trampoline logic checks the old hashes */ 2678 ops->old_hash.filter_hash = ops->func_hash->filter_hash; 2679 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash; 2680 2681 ftrace_run_update_code(command); 2682 2683 /* 2684 * If there's no more ops registered with ftrace, run a 2685 * sanity check to make sure all rec flags are cleared. 2686 */ 2687 if (ftrace_ops_list == &ftrace_list_end) { 2688 struct ftrace_page *pg; 2689 struct dyn_ftrace *rec; 2690 2691 do_for_each_ftrace_rec(pg, rec) { 2692 if (FTRACE_WARN_ON_ONCE(rec->flags)) 2693 pr_warn(" %pS flags:%lx\n", 2694 (void *)rec->ip, rec->flags); 2695 } while_for_each_ftrace_rec(); 2696 } 2697 2698 ops->old_hash.filter_hash = NULL; 2699 ops->old_hash.notrace_hash = NULL; 2700 2701 removed_ops = NULL; 2702 ops->flags &= ~FTRACE_OPS_FL_REMOVING; 2703 2704 /* 2705 * Dynamic ops may be freed, we must make sure that all 2706 * callers are done before leaving this function. 2707 * The same goes for freeing the per_cpu data of the control 2708 * ops. 2709 * 2710 * Again, normal synchronize_sched() is not good enough. 2711 * We need to do a hard force of sched synchronization. 2712 * This is because we use preempt_disable() to do RCU, but 2713 * the function tracers can be called where RCU is not watching 2714 * (like before user_exit()). We can not rely on the RCU 2715 * infrastructure to do the synchronization, thus we must do it 2716 * ourselves. 2717 */ 2718 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) { 2719 schedule_on_each_cpu(ftrace_sync); 2720 2721 arch_ftrace_trampoline_free(ops); 2722 2723 if (ops->flags & FTRACE_OPS_FL_CONTROL) 2724 control_ops_free(ops); 2725 } 2726 2727 return 0; 2728} 2729 2730static void ftrace_startup_sysctl(void) 2731{ 2732 int command; 2733 2734 if (unlikely(ftrace_disabled)) 2735 return; 2736 2737 /* Force update next time */ 2738 saved_ftrace_func = NULL; 2739 /* ftrace_start_up is true if we want ftrace running */ 2740 if (ftrace_start_up) { 2741 command = FTRACE_UPDATE_CALLS; 2742 if (ftrace_graph_active) 2743 command |= FTRACE_START_FUNC_RET; 2744 ftrace_startup_enable(command); 2745 } 2746} 2747 2748static void ftrace_shutdown_sysctl(void) 2749{ 2750 int command; 2751 2752 if (unlikely(ftrace_disabled)) 2753 return; 2754 2755 /* ftrace_start_up is true if ftrace is running */ 2756 if (ftrace_start_up) { 2757 command = FTRACE_DISABLE_CALLS; 2758 if (ftrace_graph_active) 2759 command |= FTRACE_STOP_FUNC_RET; 2760 ftrace_run_update_code(command); 2761 } 2762} 2763 2764static cycle_t ftrace_update_time; 2765unsigned long ftrace_update_tot_cnt; 2766 2767static inline int ops_traces_mod(struct ftrace_ops *ops) 2768{ 2769 /* 2770 * Filter_hash being empty will default to trace module. 2771 * But notrace hash requires a test of individual module functions. 2772 */ 2773 return ftrace_hash_empty(ops->func_hash->filter_hash) && 2774 ftrace_hash_empty(ops->func_hash->notrace_hash); 2775} 2776 2777/* 2778 * Check if the current ops references the record. 2779 * 2780 * If the ops traces all functions, then it was already accounted for. 2781 * If the ops does not trace the current record function, skip it. 2782 * If the ops ignores the function via notrace filter, skip it. 2783 */ 2784static inline bool 2785ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec) 2786{ 2787 /* If ops isn't enabled, ignore it */ 2788 if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) 2789 return 0; 2790 2791 /* If ops traces all mods, we already accounted for it */ 2792 if (ops_traces_mod(ops)) 2793 return 0; 2794 2795 /* The function must be in the filter */ 2796 if (!ftrace_hash_empty(ops->func_hash->filter_hash) && 2797 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip)) 2798 return 0; 2799 2800 /* If in notrace hash, we ignore it too */ 2801 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) 2802 return 0; 2803 2804 return 1; 2805} 2806 2807static int referenced_filters(struct dyn_ftrace *rec) 2808{ 2809 struct ftrace_ops *ops; 2810 int cnt = 0; 2811 2812 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) { 2813 if (ops_references_rec(ops, rec)) 2814 cnt++; 2815 } 2816 2817 return cnt; 2818} 2819 2820static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs) 2821{ 2822 struct ftrace_page *pg; 2823 struct dyn_ftrace *p; 2824 cycle_t start, stop; 2825 unsigned long update_cnt = 0; 2826 unsigned long ref = 0; 2827 bool test = false; 2828 int i; 2829 2830 /* 2831 * When adding a module, we need to check if tracers are 2832 * currently enabled and if they are set to trace all functions. 2833 * If they are, we need to enable the module functions as well 2834 * as update the reference counts for those function records. 2835 */ 2836 if (mod) { 2837 struct ftrace_ops *ops; 2838 2839 for (ops = ftrace_ops_list; 2840 ops != &ftrace_list_end; ops = ops->next) { 2841 if (ops->flags & FTRACE_OPS_FL_ENABLED) { 2842 if (ops_traces_mod(ops)) 2843 ref++; 2844 else 2845 test = true; 2846 } 2847 } 2848 } 2849 2850 start = ftrace_now(raw_smp_processor_id()); 2851 2852 for (pg = new_pgs; pg; pg = pg->next) { 2853 2854 for (i = 0; i < pg->index; i++) { 2855 int cnt = ref; 2856 2857 /* If something went wrong, bail without enabling anything */ 2858 if (unlikely(ftrace_disabled)) 2859 return -1; 2860 2861 p = &pg->records[i]; 2862 if (test) 2863 cnt += referenced_filters(p); 2864 p->flags = cnt; 2865 2866 /* 2867 * Do the initial record conversion from mcount jump 2868 * to the NOP instructions. 2869 */ 2870 if (!ftrace_code_disable(mod, p)) 2871 break; 2872 2873 update_cnt++; 2874 2875 /* 2876 * If the tracing is enabled, go ahead and enable the record. 2877 * 2878 * The reason not to enable the record immediatelly is the 2879 * inherent check of ftrace_make_nop/ftrace_make_call for 2880 * correct previous instructions. Making first the NOP 2881 * conversion puts the module to the correct state, thus 2882 * passing the ftrace_make_call check. 2883 */ 2884 if (ftrace_start_up && cnt) { 2885 int failed = __ftrace_replace_code(p, 1); 2886 if (failed) 2887 ftrace_bug(failed, p); 2888 } 2889 } 2890 } 2891 2892 stop = ftrace_now(raw_smp_processor_id()); 2893 ftrace_update_time = stop - start; 2894 ftrace_update_tot_cnt += update_cnt; 2895 2896 return 0; 2897} 2898 2899static int ftrace_allocate_records(struct ftrace_page *pg, int count) 2900{ 2901 int order; 2902 int cnt; 2903 2904 if (WARN_ON(!count)) 2905 return -EINVAL; 2906 2907 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE)); 2908 2909 /* 2910 * We want to fill as much as possible. No more than a page 2911 * may be empty. 2912 */ 2913 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE) 2914 order--; 2915 2916 again: 2917 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order); 2918 2919 if (!pg->records) { 2920 /* if we can't allocate this size, try something smaller */ 2921 if (!order) 2922 return -ENOMEM; 2923 order >>= 1; 2924 goto again; 2925 } 2926 2927 cnt = (PAGE_SIZE << order) / ENTRY_SIZE; 2928 pg->size = cnt; 2929 2930 if (cnt > count) 2931 cnt = count; 2932 2933 return cnt; 2934} 2935 2936static struct ftrace_page * 2937ftrace_allocate_pages(unsigned long num_to_init) 2938{ 2939 struct ftrace_page *start_pg; 2940 struct ftrace_page *pg; 2941 int order; 2942 int cnt; 2943 2944 if (!num_to_init) 2945 return 0; 2946 2947 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL); 2948 if (!pg) 2949 return NULL; 2950 2951 /* 2952 * Try to allocate as much as possible in one continues 2953 * location that fills in all of the space. We want to 2954 * waste as little space as possible. 2955 */ 2956 for (;;) { 2957 cnt = ftrace_allocate_records(pg, num_to_init); 2958 if (cnt < 0) 2959 goto free_pages; 2960 2961 num_to_init -= cnt; 2962 if (!num_to_init) 2963 break; 2964 2965 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL); 2966 if (!pg->next) 2967 goto free_pages; 2968 2969 pg = pg->next; 2970 } 2971 2972 return start_pg; 2973 2974 free_pages: 2975 pg = start_pg; 2976 while (pg) { 2977 order = get_count_order(pg->size / ENTRIES_PER_PAGE); 2978 free_pages((unsigned long)pg->records, order); 2979 start_pg = pg->next; 2980 kfree(pg); 2981 pg = start_pg; 2982 } 2983 pr_info("ftrace: FAILED to allocate memory for functions\n"); 2984 return NULL; 2985} 2986 2987#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */ 2988 2989struct ftrace_iterator { 2990 loff_t pos; 2991 loff_t func_pos; 2992 struct ftrace_page *pg; 2993 struct dyn_ftrace *func; 2994 struct ftrace_func_probe *probe; 2995 struct trace_parser parser; 2996 struct ftrace_hash *hash; 2997 struct ftrace_ops *ops; 2998 int hidx; 2999 int idx; 3000 unsigned flags; 3001}; 3002 3003static void * 3004t_hash_next(struct seq_file *m, loff_t *pos) 3005{ 3006 struct ftrace_iterator *iter = m->private; 3007 struct hlist_node *hnd = NULL; 3008 struct hlist_head *hhd; 3009 3010 (*pos)++; 3011 iter->pos = *pos; 3012 3013 if (iter->probe) 3014 hnd = &iter->probe->node; 3015 retry: 3016 if (iter->hidx >= FTRACE_FUNC_HASHSIZE) 3017 return NULL; 3018 3019 hhd = &ftrace_func_hash[iter->hidx]; 3020 3021 if (hlist_empty(hhd)) { 3022 iter->hidx++; 3023 hnd = NULL; 3024 goto retry; 3025 } 3026 3027 if (!hnd) 3028 hnd = hhd->first; 3029 else { 3030 hnd = hnd->next; 3031 if (!hnd) { 3032 iter->hidx++; 3033 goto retry; 3034 } 3035 } 3036 3037 if (WARN_ON_ONCE(!hnd)) 3038 return NULL; 3039 3040 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node); 3041 3042 return iter; 3043} 3044 3045static void *t_hash_start(struct seq_file *m, loff_t *pos) 3046{ 3047 struct ftrace_iterator *iter = m->private; 3048 void *p = NULL; 3049 loff_t l; 3050 3051 if (!(iter->flags & FTRACE_ITER_DO_HASH)) 3052 return NULL; 3053 3054 if (iter->func_pos > *pos) 3055 return NULL; 3056 3057 iter->hidx = 0; 3058 for (l = 0; l <= (*pos - iter->func_pos); ) { 3059 p = t_hash_next(m, &l); 3060 if (!p) 3061 break; 3062 } 3063 if (!p) 3064 return NULL; 3065 3066 /* Only set this if we have an item */ 3067 iter->flags |= FTRACE_ITER_HASH; 3068 3069 return iter; 3070} 3071 3072static int 3073t_hash_show(struct seq_file *m, struct ftrace_iterator *iter) 3074{ 3075 struct ftrace_func_probe *rec; 3076 3077 rec = iter->probe; 3078 if (WARN_ON_ONCE(!rec)) 3079 return -EIO; 3080 3081 if (rec->ops->print) 3082 return rec->ops->print(m, rec->ip, rec->ops, rec->data); 3083 3084 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func); 3085 3086 if (rec->data) 3087 seq_printf(m, ":%p", rec->data); 3088 seq_putc(m, '\n'); 3089 3090 return 0; 3091} 3092 3093static void * 3094t_next(struct seq_file *m, void *v, loff_t *pos) 3095{ 3096 struct ftrace_iterator *iter = m->private; 3097 struct ftrace_ops *ops = iter->ops; 3098 struct dyn_ftrace *rec = NULL; 3099 3100 if (unlikely(ftrace_disabled)) 3101 return NULL; 3102 3103 if (iter->flags & FTRACE_ITER_HASH) 3104 return t_hash_next(m, pos); 3105 3106 (*pos)++; 3107 iter->pos = iter->func_pos = *pos; 3108 3109 if (iter->flags & FTRACE_ITER_PRINTALL) 3110 return t_hash_start(m, pos); 3111 3112 retry: 3113 if (iter->idx >= iter->pg->index) { 3114 if (iter->pg->next) { 3115 iter->pg = iter->pg->next; 3116 iter->idx = 0; 3117 goto retry; 3118 } 3119 } else { 3120 rec = &iter->pg->records[iter->idx++]; 3121 if (((iter->flags & FTRACE_ITER_FILTER) && 3122 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) || 3123 3124 ((iter->flags & FTRACE_ITER_NOTRACE) && 3125 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) || 3126 3127 ((iter->flags & FTRACE_ITER_ENABLED) && 3128 !(rec->flags & FTRACE_FL_ENABLED))) { 3129 3130 rec = NULL; 3131 goto retry; 3132 } 3133 } 3134 3135 if (!rec) 3136 return t_hash_start(m, pos); 3137 3138 iter->func = rec; 3139 3140 return iter; 3141} 3142 3143static void reset_iter_read(struct ftrace_iterator *iter) 3144{ 3145 iter->pos = 0; 3146 iter->func_pos = 0; 3147 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH); 3148} 3149 3150static void *t_start(struct seq_file *m, loff_t *pos) 3151{ 3152 struct ftrace_iterator *iter = m->private; 3153 struct ftrace_ops *ops = iter->ops; 3154 void *p = NULL; 3155 loff_t l; 3156 3157 mutex_lock(&ftrace_lock); 3158 3159 if (unlikely(ftrace_disabled)) 3160 return NULL; 3161 3162 /* 3163 * If an lseek was done, then reset and start from beginning. 3164 */ 3165 if (*pos < iter->pos) 3166 reset_iter_read(iter); 3167 3168 /* 3169 * For set_ftrace_filter reading, if we have the filter 3170 * off, we can short cut and just print out that all 3171 * functions are enabled. 3172 */ 3173 if ((iter->flags & FTRACE_ITER_FILTER && 3174 ftrace_hash_empty(ops->func_hash->filter_hash)) || 3175 (iter->flags & FTRACE_ITER_NOTRACE && 3176 ftrace_hash_empty(ops->func_hash->notrace_hash))) { 3177 if (*pos > 0) 3178 return t_hash_start(m, pos); 3179 iter->flags |= FTRACE_ITER_PRINTALL; 3180 /* reset in case of seek/pread */ 3181 iter->flags &= ~FTRACE_ITER_HASH; 3182 return iter; 3183 } 3184 3185 if (iter->flags & FTRACE_ITER_HASH) 3186 return t_hash_start(m, pos); 3187 3188 /* 3189 * Unfortunately, we need to restart at ftrace_pages_start 3190 * every time we let go of the ftrace_mutex. This is because 3191 * those pointers can change without the lock. 3192 */ 3193 iter->pg = ftrace_pages_start; 3194 iter->idx = 0; 3195 for (l = 0; l <= *pos; ) { 3196 p = t_next(m, p, &l); 3197 if (!p) 3198 break; 3199 } 3200 3201 if (!p) 3202 return t_hash_start(m, pos); 3203 3204 return iter; 3205} 3206 3207static void t_stop(struct seq_file *m, void *p) 3208{ 3209 mutex_unlock(&ftrace_lock); 3210} 3211 3212void * __weak 3213arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec) 3214{ 3215 return NULL; 3216} 3217 3218static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops, 3219 struct dyn_ftrace *rec) 3220{ 3221 void *ptr; 3222 3223 ptr = arch_ftrace_trampoline_func(ops, rec); 3224 if (ptr) 3225 seq_printf(m, " ->%pS", ptr); 3226} 3227 3228static int t_show(struct seq_file *m, void *v) 3229{ 3230 struct ftrace_iterator *iter = m->private; 3231 struct dyn_ftrace *rec; 3232 3233 if (iter->flags & FTRACE_ITER_HASH) 3234 return t_hash_show(m, iter); 3235 3236 if (iter->flags & FTRACE_ITER_PRINTALL) { 3237 if (iter->flags & FTRACE_ITER_NOTRACE) 3238 seq_puts(m, "#### no functions disabled ####\n"); 3239 else 3240 seq_puts(m, "#### all functions enabled ####\n"); 3241 return 0; 3242 } 3243 3244 rec = iter->func; 3245 3246 if (!rec) 3247 return 0; 3248 3249 seq_printf(m, "%ps", (void *)rec->ip); 3250 if (iter->flags & FTRACE_ITER_ENABLED) { 3251 struct ftrace_ops *ops = NULL; 3252 3253 seq_printf(m, " (%ld)%s%s", 3254 ftrace_rec_count(rec), 3255 rec->flags & FTRACE_FL_REGS ? " R" : " ", 3256 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " "); 3257 if (rec->flags & FTRACE_FL_TRAMP_EN) { 3258 ops = ftrace_find_tramp_ops_any(rec); 3259 if (ops) 3260 seq_printf(m, "\ttramp: %pS", 3261 (void *)ops->trampoline); 3262 else 3263 seq_puts(m, "\ttramp: ERROR!"); 3264 3265 } 3266 add_trampoline_func(m, ops, rec); 3267 } 3268 3269 seq_putc(m, '\n'); 3270 3271 return 0; 3272} 3273 3274static const struct seq_operations show_ftrace_seq_ops = { 3275 .start = t_start, 3276 .next = t_next, 3277 .stop = t_stop, 3278 .show = t_show, 3279}; 3280 3281static int 3282ftrace_avail_open(struct inode *inode, struct file *file) 3283{ 3284 struct ftrace_iterator *iter; 3285 3286 if (unlikely(ftrace_disabled)) 3287 return -ENODEV; 3288 3289 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter)); 3290 if (iter) { 3291 iter->pg = ftrace_pages_start; 3292 iter->ops = &global_ops; 3293 } 3294 3295 return iter ? 0 : -ENOMEM; 3296} 3297 3298static int 3299ftrace_enabled_open(struct inode *inode, struct file *file) 3300{ 3301 struct ftrace_iterator *iter; 3302 3303 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter)); 3304 if (iter) { 3305 iter->pg = ftrace_pages_start; 3306 iter->flags = FTRACE_ITER_ENABLED; 3307 iter->ops = &global_ops; 3308 } 3309 3310 return iter ? 0 : -ENOMEM; 3311} 3312 3313/** 3314 * ftrace_regex_open - initialize function tracer filter files 3315 * @ops: The ftrace_ops that hold the hash filters 3316 * @flag: The type of filter to process 3317 * @inode: The inode, usually passed in to your open routine 3318 * @file: The file, usually passed in to your open routine 3319 * 3320 * ftrace_regex_open() initializes the filter files for the 3321 * @ops. Depending on @flag it may process the filter hash or 3322 * the notrace hash of @ops. With this called from the open 3323 * routine, you can use ftrace_filter_write() for the write 3324 * routine if @flag has FTRACE_ITER_FILTER set, or 3325 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set. 3326 * tracing_lseek() should be used as the lseek routine, and 3327 * release must call ftrace_regex_release(). 3328 */ 3329int 3330ftrace_regex_open(struct ftrace_ops *ops, int flag, 3331 struct inode *inode, struct file *file) 3332{ 3333 struct ftrace_iterator *iter; 3334 struct ftrace_hash *hash; 3335 int ret = 0; 3336 3337 ftrace_ops_init(ops); 3338 3339 if (unlikely(ftrace_disabled)) 3340 return -ENODEV; 3341 3342 iter = kzalloc(sizeof(*iter), GFP_KERNEL); 3343 if (!iter) 3344 return -ENOMEM; 3345 3346 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) { 3347 kfree(iter); 3348 return -ENOMEM; 3349 } 3350 3351 iter->ops = ops; 3352 iter->flags = flag; 3353 3354 mutex_lock(&ops->func_hash->regex_lock); 3355 3356 if (flag & FTRACE_ITER_NOTRACE) 3357 hash = ops->func_hash->notrace_hash; 3358 else 3359 hash = ops->func_hash->filter_hash; 3360 3361 if (file->f_mode & FMODE_WRITE) { 3362 const int size_bits = FTRACE_HASH_DEFAULT_BITS; 3363 3364 if (file->f_flags & O_TRUNC) 3365 iter->hash = alloc_ftrace_hash(size_bits); 3366 else 3367 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash); 3368 3369 if (!iter->hash) { 3370 trace_parser_put(&iter->parser); 3371 kfree(iter); 3372 ret = -ENOMEM; 3373 goto out_unlock; 3374 } 3375 } 3376 3377 if (file->f_mode & FMODE_READ) { 3378 iter->pg = ftrace_pages_start; 3379 3380 ret = seq_open(file, &show_ftrace_seq_ops); 3381 if (!ret) { 3382 struct seq_file *m = file->private_data; 3383 m->private = iter; 3384 } else { 3385 /* Failed */ 3386 free_ftrace_hash(iter->hash); 3387 trace_parser_put(&iter->parser); 3388 kfree(iter); 3389 } 3390 } else 3391 file->private_data = iter; 3392 3393 out_unlock: 3394 mutex_unlock(&ops->func_hash->regex_lock); 3395 3396 return ret; 3397} 3398 3399static int 3400ftrace_filter_open(struct inode *inode, struct file *file) 3401{ 3402 struct ftrace_ops *ops = inode->i_private; 3403 3404 return ftrace_regex_open(ops, 3405 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH, 3406 inode, file); 3407} 3408 3409static int 3410ftrace_notrace_open(struct inode *inode, struct file *file) 3411{ 3412 struct ftrace_ops *ops = inode->i_private; 3413 3414 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE, 3415 inode, file); 3416} 3417 3418static int ftrace_match(char *str, char *regex, int len, int type) 3419{ 3420 int matched = 0; 3421 int slen; 3422 3423 switch (type) { 3424 case MATCH_FULL: 3425 if (strcmp(str, regex) == 0) 3426 matched = 1; 3427 break; 3428 case MATCH_FRONT_ONLY: 3429 if (strncmp(str, regex, len) == 0) 3430 matched = 1; 3431 break; 3432 case MATCH_MIDDLE_ONLY: 3433 if (strstr(str, regex)) 3434 matched = 1; 3435 break; 3436 case MATCH_END_ONLY: 3437 slen = strlen(str); 3438 if (slen >= len && memcmp(str + slen - len, regex, len) == 0) 3439 matched = 1; 3440 break; 3441 } 3442 3443 return matched; 3444} 3445 3446static int 3447enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not) 3448{ 3449 struct ftrace_func_entry *entry; 3450 int ret = 0; 3451 3452 entry = ftrace_lookup_ip(hash, rec->ip); 3453 if (not) { 3454 /* Do nothing if it doesn't exist */ 3455 if (!entry) 3456 return 0; 3457 3458 free_hash_entry(hash, entry); 3459 } else { 3460 /* Do nothing if it exists */ 3461 if (entry) 3462 return 0; 3463 3464 ret = add_hash_entry(hash, rec->ip); 3465 } 3466 return ret; 3467} 3468 3469static int 3470ftrace_match_record(struct dyn_ftrace *rec, char *mod, 3471 char *regex, int len, int type) 3472{ 3473 char str[KSYM_SYMBOL_LEN]; 3474 char *modname; 3475 3476 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str); 3477 3478 if (mod) { 3479 /* module lookup requires matching the module */ 3480 if (!modname || strcmp(modname, mod)) 3481 return 0; 3482 3483 /* blank search means to match all funcs in the mod */ 3484 if (!len) 3485 return 1; 3486 } 3487 3488 return ftrace_match(str, regex, len, type); 3489} 3490 3491static int 3492match_records(struct ftrace_hash *hash, char *buff, 3493 int len, char *mod, int not) 3494{ 3495 unsigned search_len = 0; 3496 struct ftrace_page *pg; 3497 struct dyn_ftrace *rec; 3498 int type = MATCH_FULL; 3499 char *search = buff; 3500 int found = 0; 3501 int ret; 3502 3503 if (len) { 3504 type = filter_parse_regex(buff, len, &search, ¬); 3505 search_len = strlen(search); 3506 } 3507 3508 mutex_lock(&ftrace_lock); 3509 3510 if (unlikely(ftrace_disabled)) 3511 goto out_unlock; 3512 3513 do_for_each_ftrace_rec(pg, rec) { 3514 if (ftrace_match_record(rec, mod, search, search_len, type)) { 3515 ret = enter_record(hash, rec, not); 3516 if (ret < 0) { 3517 found = ret; 3518 goto out_unlock; 3519 } 3520 found = 1; 3521 } 3522 } while_for_each_ftrace_rec(); 3523 out_unlock: 3524 mutex_unlock(&ftrace_lock); 3525 3526 return found; 3527} 3528 3529static int 3530ftrace_match_records(struct ftrace_hash *hash, char *buff, int len) 3531{ 3532 return match_records(hash, buff, len, NULL, 0); 3533} 3534 3535static int 3536ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod) 3537{ 3538 int not = 0; 3539 3540 /* blank or '*' mean the same */ 3541 if (strcmp(buff, "*") == 0) 3542 buff[0] = 0; 3543 3544 /* handle the case of 'dont filter this module' */ 3545 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) { 3546 buff[0] = 0; 3547 not = 1; 3548 } 3549 3550 return match_records(hash, buff, strlen(buff), mod, not); 3551} 3552 3553/* 3554 * We register the module command as a template to show others how 3555 * to register the a command as well. 3556 */ 3557 3558static int 3559ftrace_mod_callback(struct ftrace_hash *hash, 3560 char *func, char *cmd, char *param, int enable) 3561{ 3562 char *mod; 3563 int ret = -EINVAL; 3564 3565 /* 3566 * cmd == 'mod' because we only registered this func 3567 * for the 'mod' ftrace_func_command. 3568 * But if you register one func with multiple commands, 3569 * you can tell which command was used by the cmd 3570 * parameter. 3571 */ 3572 3573 /* we must have a module name */ 3574 if (!param) 3575 return ret; 3576 3577 mod = strsep(¶m, ":"); 3578 if (!strlen(mod)) 3579 return ret; 3580 3581 ret = ftrace_match_module_records(hash, func, mod); 3582 if (!ret) 3583 ret = -EINVAL; 3584 if (ret < 0) 3585 return ret; 3586 3587 return 0; 3588} 3589 3590static struct ftrace_func_command ftrace_mod_cmd = { 3591 .name = "mod", 3592 .func = ftrace_mod_callback, 3593}; 3594 3595static int __init ftrace_mod_cmd_init(void) 3596{ 3597 return register_ftrace_command(&ftrace_mod_cmd); 3598} 3599core_initcall(ftrace_mod_cmd_init); 3600 3601static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip, 3602 struct ftrace_ops *op, struct pt_regs *pt_regs) 3603{ 3604 struct ftrace_func_probe *entry; 3605 struct hlist_head *hhd; 3606 unsigned long key; 3607 3608 key = hash_long(ip, FTRACE_HASH_BITS); 3609 3610 hhd = &ftrace_func_hash[key]; 3611 3612 if (hlist_empty(hhd)) 3613 return; 3614 3615 /* 3616 * Disable preemption for these calls to prevent a RCU grace 3617 * period. This syncs the hash iteration and freeing of items 3618 * on the hash. rcu_read_lock is too dangerous here. 3619 */ 3620 preempt_disable_notrace(); 3621 hlist_for_each_entry_rcu_notrace(entry, hhd, node) { 3622 if (entry->ip == ip) 3623 entry->ops->func(ip, parent_ip, &entry->data); 3624 } 3625 preempt_enable_notrace(); 3626} 3627 3628static struct ftrace_ops trace_probe_ops __read_mostly = 3629{ 3630 .func = function_trace_probe_call, 3631 .flags = FTRACE_OPS_FL_INITIALIZED, 3632 INIT_OPS_HASH(trace_probe_ops) 3633}; 3634 3635static int ftrace_probe_registered; 3636 3637static void __enable_ftrace_function_probe(struct ftrace_ops_hash *old_hash) 3638{ 3639 int ret; 3640 int i; 3641 3642 if (ftrace_probe_registered) { 3643 /* still need to update the function call sites */ 3644 if (ftrace_enabled) 3645 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS, 3646 old_hash); 3647 return; 3648 } 3649 3650 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { 3651 struct hlist_head *hhd = &ftrace_func_hash[i]; 3652 if (hhd->first) 3653 break; 3654 } 3655 /* Nothing registered? */ 3656 if (i == FTRACE_FUNC_HASHSIZE) 3657 return; 3658 3659 ret = ftrace_startup(&trace_probe_ops, 0); 3660 3661 ftrace_probe_registered = 1; 3662} 3663 3664static void __disable_ftrace_function_probe(void) 3665{ 3666 int i; 3667 3668 if (!ftrace_probe_registered) 3669 return; 3670 3671 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { 3672 struct hlist_head *hhd = &ftrace_func_hash[i]; 3673 if (hhd->first) 3674 return; 3675 } 3676 3677 /* no more funcs left */ 3678 ftrace_shutdown(&trace_probe_ops, 0); 3679 3680 ftrace_probe_registered = 0; 3681} 3682 3683 3684static void ftrace_free_entry(struct ftrace_func_probe *entry) 3685{ 3686 if (entry->ops->free) 3687 entry->ops->free(entry->ops, entry->ip, &entry->data); 3688 kfree(entry); 3689} 3690 3691int 3692register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, 3693 void *data) 3694{ 3695 struct ftrace_ops_hash old_hash_ops; 3696 struct ftrace_func_probe *entry; 3697 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash; 3698 struct ftrace_hash *old_hash = *orig_hash; 3699 struct ftrace_hash *hash; 3700 struct ftrace_page *pg; 3701 struct dyn_ftrace *rec; 3702 int type, len, not; 3703 unsigned long key; 3704 int count = 0; 3705 char *search; 3706 int ret; 3707 3708 type = filter_parse_regex(glob, strlen(glob), &search, ¬); 3709 len = strlen(search); 3710 3711 /* we do not support '!' for function probes */ 3712 if (WARN_ON(not)) 3713 return -EINVAL; 3714 3715 mutex_lock(&trace_probe_ops.func_hash->regex_lock); 3716 3717 old_hash_ops.filter_hash = old_hash; 3718 /* Probes only have filters */ 3719 old_hash_ops.notrace_hash = NULL; 3720 3721 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash); 3722 if (!hash) { 3723 count = -ENOMEM; 3724 goto out; 3725 } 3726 3727 if (unlikely(ftrace_disabled)) { 3728 count = -ENODEV; 3729 goto out; 3730 } 3731 3732 mutex_lock(&ftrace_lock); 3733 3734 do_for_each_ftrace_rec(pg, rec) { 3735 3736 if (!ftrace_match_record(rec, NULL, search, len, type)) 3737 continue; 3738 3739 entry = kmalloc(sizeof(*entry), GFP_KERNEL); 3740 if (!entry) { 3741 /* If we did not process any, then return error */ 3742 if (!count) 3743 count = -ENOMEM; 3744 goto out_unlock; 3745 } 3746 3747 count++; 3748 3749 entry->data = data; 3750 3751 /* 3752 * The caller might want to do something special 3753 * for each function we find. We call the callback 3754 * to give the caller an opportunity to do so. 3755 */ 3756 if (ops->init) { 3757 if (ops->init(ops, rec->ip, &entry->data) < 0) { 3758 /* caller does not like this func */ 3759 kfree(entry); 3760 continue; 3761 } 3762 } 3763 3764 ret = enter_record(hash, rec, 0); 3765 if (ret < 0) { 3766 kfree(entry); 3767 count = ret; 3768 goto out_unlock; 3769 } 3770 3771 entry->ops = ops; 3772 entry->ip = rec->ip; 3773 3774 key = hash_long(entry->ip, FTRACE_HASH_BITS); 3775 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]); 3776 3777 } while_for_each_ftrace_rec(); 3778 3779 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash); 3780 3781 __enable_ftrace_function_probe(&old_hash_ops); 3782 3783 if (!ret) 3784 free_ftrace_hash_rcu(old_hash); 3785 else 3786 count = ret; 3787 3788 out_unlock: 3789 mutex_unlock(&ftrace_lock); 3790 out: 3791 mutex_unlock(&trace_probe_ops.func_hash->regex_lock); 3792 free_ftrace_hash(hash); 3793 3794 return count; 3795} 3796 3797enum { 3798 PROBE_TEST_FUNC = 1, 3799 PROBE_TEST_DATA = 2 3800}; 3801 3802static void 3803__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, 3804 void *data, int flags) 3805{ 3806 struct ftrace_func_entry *rec_entry; 3807 struct ftrace_func_probe *entry; 3808 struct ftrace_func_probe *p; 3809 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash; 3810 struct ftrace_hash *old_hash = *orig_hash; 3811 struct list_head free_list; 3812 struct ftrace_hash *hash; 3813 struct hlist_node *tmp; 3814 char str[KSYM_SYMBOL_LEN]; 3815 int type = MATCH_FULL; 3816 int i, len = 0; 3817 char *search; 3818 int ret; 3819 3820 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob))) 3821 glob = NULL; 3822 else if (glob) { 3823 int not; 3824 3825 type = filter_parse_regex(glob, strlen(glob), &search, ¬); 3826 len = strlen(search); 3827 3828 /* we do not support '!' for function probes */ 3829 if (WARN_ON(not)) 3830 return; 3831 } 3832 3833 mutex_lock(&trace_probe_ops.func_hash->regex_lock); 3834 3835 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash); 3836 if (!hash) 3837 /* Hmm, should report this somehow */ 3838 goto out_unlock; 3839 3840 INIT_LIST_HEAD(&free_list); 3841 3842 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { 3843 struct hlist_head *hhd = &ftrace_func_hash[i]; 3844 3845 hlist_for_each_entry_safe(entry, tmp, hhd, node) { 3846 3847 /* break up if statements for readability */ 3848 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops) 3849 continue; 3850 3851 if ((flags & PROBE_TEST_DATA) && entry->data != data) 3852 continue; 3853 3854 /* do this last, since it is the most expensive */ 3855 if (glob) { 3856 kallsyms_lookup(entry->ip, NULL, NULL, 3857 NULL, str); 3858 if (!ftrace_match(str, glob, len, type)) 3859 continue; 3860 } 3861 3862 rec_entry = ftrace_lookup_ip(hash, entry->ip); 3863 /* It is possible more than one entry had this ip */ 3864 if (rec_entry) 3865 free_hash_entry(hash, rec_entry); 3866 3867 hlist_del_rcu(&entry->node); 3868 list_add(&entry->free_list, &free_list); 3869 } 3870 } 3871 mutex_lock(&ftrace_lock); 3872 __disable_ftrace_function_probe(); 3873 /* 3874 * Remove after the disable is called. Otherwise, if the last 3875 * probe is removed, a null hash means *all enabled*. 3876 */ 3877 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash); 3878 synchronize_sched(); 3879 if (!ret) 3880 free_ftrace_hash_rcu(old_hash); 3881 3882 list_for_each_entry_safe(entry, p, &free_list, free_list) { 3883 list_del(&entry->free_list); 3884 ftrace_free_entry(entry); 3885 } 3886 mutex_unlock(&ftrace_lock); 3887 3888 out_unlock: 3889 mutex_unlock(&trace_probe_ops.func_hash->regex_lock); 3890 free_ftrace_hash(hash); 3891} 3892 3893void 3894unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, 3895 void *data) 3896{ 3897 __unregister_ftrace_function_probe(glob, ops, data, 3898 PROBE_TEST_FUNC | PROBE_TEST_DATA); 3899} 3900 3901void 3902unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops) 3903{ 3904 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC); 3905} 3906 3907void unregister_ftrace_function_probe_all(char *glob) 3908{ 3909 __unregister_ftrace_function_probe(glob, NULL, NULL, 0); 3910} 3911 3912static LIST_HEAD(ftrace_commands); 3913static DEFINE_MUTEX(ftrace_cmd_mutex); 3914 3915/* 3916 * Currently we only register ftrace commands from __init, so mark this 3917 * __init too. 3918 */ 3919__init int register_ftrace_command(struct ftrace_func_command *cmd) 3920{ 3921 struct ftrace_func_command *p; 3922 int ret = 0; 3923 3924 mutex_lock(&ftrace_cmd_mutex); 3925 list_for_each_entry(p, &ftrace_commands, list) { 3926 if (strcmp(cmd->name, p->name) == 0) { 3927 ret = -EBUSY; 3928 goto out_unlock; 3929 } 3930 } 3931 list_add(&cmd->list, &ftrace_commands); 3932 out_unlock: 3933 mutex_unlock(&ftrace_cmd_mutex); 3934 3935 return ret; 3936} 3937 3938/* 3939 * Currently we only unregister ftrace commands from __init, so mark 3940 * this __init too. 3941 */ 3942__init int unregister_ftrace_command(struct ftrace_func_command *cmd) 3943{ 3944 struct ftrace_func_command *p, *n; 3945 int ret = -ENODEV; 3946 3947 mutex_lock(&ftrace_cmd_mutex); 3948 list_for_each_entry_safe(p, n, &ftrace_commands, list) { 3949 if (strcmp(cmd->name, p->name) == 0) { 3950 ret = 0; 3951 list_del_init(&p->list); 3952 goto out_unlock; 3953 } 3954 } 3955 out_unlock: 3956 mutex_unlock(&ftrace_cmd_mutex); 3957 3958 return ret; 3959} 3960 3961static int ftrace_process_regex(struct ftrace_hash *hash, 3962 char *buff, int len, int enable) 3963{ 3964 char *func, *command, *next = buff; 3965 struct ftrace_func_command *p; 3966 int ret = -EINVAL; 3967 3968 func = strsep(&next, ":"); 3969 3970 if (!next) { 3971 ret = ftrace_match_records(hash, func, len); 3972 if (!ret) 3973 ret = -EINVAL; 3974 if (ret < 0) 3975 return ret; 3976 return 0; 3977 } 3978 3979 /* command found */ 3980 3981 command = strsep(&next, ":"); 3982 3983 mutex_lock(&ftrace_cmd_mutex); 3984 list_for_each_entry(p, &ftrace_commands, list) { 3985 if (strcmp(p->name, command) == 0) { 3986 ret = p->func(hash, func, command, next, enable); 3987 goto out_unlock; 3988 } 3989 } 3990 out_unlock: 3991 mutex_unlock(&ftrace_cmd_mutex); 3992 3993 return ret; 3994} 3995 3996static ssize_t 3997ftrace_regex_write(struct file *file, const char __user *ubuf, 3998 size_t cnt, loff_t *ppos, int enable) 3999{ 4000 struct ftrace_iterator *iter; 4001 struct trace_parser *parser; 4002 ssize_t ret, read; 4003 4004 if (!cnt) 4005 return 0; 4006 4007 if (file->f_mode & FMODE_READ) { 4008 struct seq_file *m = file->private_data; 4009 iter = m->private; 4010 } else 4011 iter = file->private_data; 4012 4013 if (unlikely(ftrace_disabled)) 4014 return -ENODEV; 4015 4016 /* iter->hash is a local copy, so we don't need regex_lock */ 4017 4018 parser = &iter->parser; 4019 read = trace_get_user(parser, ubuf, cnt, ppos); 4020 4021 if (read >= 0 && trace_parser_loaded(parser) && 4022 !trace_parser_cont(parser)) { 4023 ret = ftrace_process_regex(iter->hash, parser->buffer, 4024 parser->idx, enable); 4025 trace_parser_clear(parser); 4026 if (ret < 0) 4027 goto out; 4028 } 4029 4030 ret = read; 4031 out: 4032 return ret; 4033} 4034 4035ssize_t 4036ftrace_filter_write(struct file *file, const char __user *ubuf, 4037 size_t cnt, loff_t *ppos) 4038{ 4039 return ftrace_regex_write(file, ubuf, cnt, ppos, 1); 4040} 4041 4042ssize_t 4043ftrace_notrace_write(struct file *file, const char __user *ubuf, 4044 size_t cnt, loff_t *ppos) 4045{ 4046 return ftrace_regex_write(file, ubuf, cnt, ppos, 0); 4047} 4048 4049static int 4050ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove) 4051{ 4052 struct ftrace_func_entry *entry; 4053 4054 if (!ftrace_location(ip)) 4055 return -EINVAL; 4056 4057 if (remove) { 4058 entry = ftrace_lookup_ip(hash, ip); 4059 if (!entry) 4060 return -ENOENT; 4061 free_hash_entry(hash, entry); 4062 return 0; 4063 } 4064 4065 return add_hash_entry(hash, ip); 4066} 4067 4068static void ftrace_ops_update_code(struct ftrace_ops *ops, 4069 struct ftrace_ops_hash *old_hash) 4070{ 4071 struct ftrace_ops *op; 4072 4073 if (!ftrace_enabled) 4074 return; 4075 4076 if (ops->flags & FTRACE_OPS_FL_ENABLED) { 4077 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash); 4078 return; 4079 } 4080 4081 /* 4082 * If this is the shared global_ops filter, then we need to 4083 * check if there is another ops that shares it, is enabled. 4084 * If so, we still need to run the modify code. 4085 */ 4086 if (ops->func_hash != &global_ops.local_hash) 4087 return; 4088 4089 do_for_each_ftrace_op(op, ftrace_ops_list) { 4090 if (op->func_hash == &global_ops.local_hash && 4091 op->flags & FTRACE_OPS_FL_ENABLED) { 4092 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash); 4093 /* Only need to do this once */ 4094 return; 4095 } 4096 } while_for_each_ftrace_op(op); 4097} 4098 4099static int 4100ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len, 4101 unsigned long ip, int remove, int reset, int enable) 4102{ 4103 struct ftrace_hash **orig_hash; 4104 struct ftrace_ops_hash old_hash_ops; 4105 struct ftrace_hash *old_hash; 4106 struct ftrace_hash *hash; 4107 int ret; 4108 4109 if (unlikely(ftrace_disabled)) 4110 return -ENODEV; 4111 4112 mutex_lock(&ops->func_hash->regex_lock); 4113 4114 if (enable) 4115 orig_hash = &ops->func_hash->filter_hash; 4116 else 4117 orig_hash = &ops->func_hash->notrace_hash; 4118 4119 if (reset) 4120 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS); 4121 else 4122 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash); 4123 4124 if (!hash) { 4125 ret = -ENOMEM; 4126 goto out_regex_unlock; 4127 } 4128 4129 if (buf && !ftrace_match_records(hash, buf, len)) { 4130 ret = -EINVAL; 4131 goto out_regex_unlock; 4132 } 4133 if (ip) { 4134 ret = ftrace_match_addr(hash, ip, remove); 4135 if (ret < 0) 4136 goto out_regex_unlock; 4137 } 4138 4139 mutex_lock(&ftrace_lock); 4140 old_hash = *orig_hash; 4141 old_hash_ops.filter_hash = ops->func_hash->filter_hash; 4142 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash; 4143 ret = ftrace_hash_move(ops, enable, orig_hash, hash); 4144 if (!ret) { 4145 ftrace_ops_update_code(ops, &old_hash_ops); 4146 free_ftrace_hash_rcu(old_hash); 4147 } 4148 mutex_unlock(&ftrace_lock); 4149 4150 out_regex_unlock: 4151 mutex_unlock(&ops->func_hash->regex_lock); 4152 4153 free_ftrace_hash(hash); 4154 return ret; 4155} 4156 4157static int 4158ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove, 4159 int reset, int enable) 4160{ 4161 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable); 4162} 4163 4164/** 4165 * ftrace_set_filter_ip - set a function to filter on in ftrace by address 4166 * @ops - the ops to set the filter with 4167 * @ip - the address to add to or remove from the filter. 4168 * @remove - non zero to remove the ip from the filter 4169 * @reset - non zero to reset all filters before applying this filter. 4170 * 4171 * Filters denote which functions should be enabled when tracing is enabled 4172 * If @ip is NULL, it failes to update filter. 4173 */ 4174int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip, 4175 int remove, int reset) 4176{ 4177 ftrace_ops_init(ops); 4178 return ftrace_set_addr(ops, ip, remove, reset, 1); 4179} 4180EXPORT_SYMBOL_GPL(ftrace_set_filter_ip); 4181 4182static int 4183ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len, 4184 int reset, int enable) 4185{ 4186 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable); 4187} 4188 4189/** 4190 * ftrace_set_filter - set a function to filter on in ftrace 4191 * @ops - the ops to set the filter with 4192 * @buf - the string that holds the function filter text. 4193 * @len - the length of the string. 4194 * @reset - non zero to reset all filters before applying this filter. 4195 * 4196 * Filters denote which functions should be enabled when tracing is enabled. 4197 * If @buf is NULL and reset is set, all functions will be enabled for tracing. 4198 */ 4199int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf, 4200 int len, int reset) 4201{ 4202 ftrace_ops_init(ops); 4203 return ftrace_set_regex(ops, buf, len, reset, 1); 4204} 4205EXPORT_SYMBOL_GPL(ftrace_set_filter); 4206 4207/** 4208 * ftrace_set_notrace - set a function to not trace in ftrace 4209 * @ops - the ops to set the notrace filter with 4210 * @buf - the string that holds the function notrace text. 4211 * @len - the length of the string. 4212 * @reset - non zero to reset all filters before applying this filter. 4213 * 4214 * Notrace Filters denote which functions should not be enabled when tracing 4215 * is enabled. If @buf is NULL and reset is set, all functions will be enabled 4216 * for tracing. 4217 */ 4218int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf, 4219 int len, int reset) 4220{ 4221 ftrace_ops_init(ops); 4222 return ftrace_set_regex(ops, buf, len, reset, 0); 4223} 4224EXPORT_SYMBOL_GPL(ftrace_set_notrace); 4225/** 4226 * ftrace_set_global_filter - set a function to filter on with global tracers 4227 * @buf - the string that holds the function filter text. 4228 * @len - the length of the string. 4229 * @reset - non zero to reset all filters before applying this filter. 4230 * 4231 * Filters denote which functions should be enabled when tracing is enabled. 4232 * If @buf is NULL and reset is set, all functions will be enabled for tracing. 4233 */ 4234void ftrace_set_global_filter(unsigned char *buf, int len, int reset) 4235{ 4236 ftrace_set_regex(&global_ops, buf, len, reset, 1); 4237} 4238EXPORT_SYMBOL_GPL(ftrace_set_global_filter); 4239 4240/** 4241 * ftrace_set_global_notrace - set a function to not trace with global tracers 4242 * @buf - the string that holds the function notrace text. 4243 * @len - the length of the string. 4244 * @reset - non zero to reset all filters before applying this filter. 4245 * 4246 * Notrace Filters denote which functions should not be enabled when tracing 4247 * is enabled. If @buf is NULL and reset is set, all functions will be enabled 4248 * for tracing. 4249 */ 4250void ftrace_set_global_notrace(unsigned char *buf, int len, int reset) 4251{ 4252 ftrace_set_regex(&global_ops, buf, len, reset, 0); 4253} 4254EXPORT_SYMBOL_GPL(ftrace_set_global_notrace); 4255 4256/* 4257 * command line interface to allow users to set filters on boot up. 4258 */ 4259#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE 4260static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata; 4261static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata; 4262 4263/* Used by function selftest to not test if filter is set */ 4264bool ftrace_filter_param __initdata; 4265 4266static int __init set_ftrace_notrace(char *str) 4267{ 4268 ftrace_filter_param = true; 4269 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE); 4270 return 1; 4271} 4272__setup("ftrace_notrace=", set_ftrace_notrace); 4273 4274static int __init set_ftrace_filter(char *str) 4275{ 4276 ftrace_filter_param = true; 4277 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE); 4278 return 1; 4279} 4280__setup("ftrace_filter=", set_ftrace_filter); 4281 4282#ifdef CONFIG_FUNCTION_GRAPH_TRACER 4283static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata; 4284static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata; 4285static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer); 4286 4287static unsigned long save_global_trampoline; 4288static unsigned long save_global_flags; 4289 4290static int __init set_graph_function(char *str) 4291{ 4292 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE); 4293 return 1; 4294} 4295__setup("ftrace_graph_filter=", set_graph_function); 4296 4297static int __init set_graph_notrace_function(char *str) 4298{ 4299 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE); 4300 return 1; 4301} 4302__setup("ftrace_graph_notrace=", set_graph_notrace_function); 4303 4304static void __init set_ftrace_early_graph(char *buf, int enable) 4305{ 4306 int ret; 4307 char *func; 4308 unsigned long *table = ftrace_graph_funcs; 4309 int *count = &ftrace_graph_count; 4310 4311 if (!enable) { 4312 table = ftrace_graph_notrace_funcs; 4313 count = &ftrace_graph_notrace_count; 4314 } 4315 4316 while (buf) { 4317 func = strsep(&buf, ","); 4318 /* we allow only one expression at a time */ 4319 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func); 4320 if (ret) 4321 printk(KERN_DEBUG "ftrace: function %s not " 4322 "traceable\n", func); 4323 } 4324} 4325#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ 4326 4327void __init 4328ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable) 4329{ 4330 char *func; 4331 4332 ftrace_ops_init(ops); 4333 4334 while (buf) { 4335 func = strsep(&buf, ","); 4336 ftrace_set_regex(ops, func, strlen(func), 0, enable); 4337 } 4338} 4339 4340static void __init set_ftrace_early_filters(void) 4341{ 4342 if (ftrace_filter_buf[0]) 4343 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1); 4344 if (ftrace_notrace_buf[0]) 4345 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0); 4346#ifdef CONFIG_FUNCTION_GRAPH_TRACER 4347 if (ftrace_graph_buf[0]) 4348 set_ftrace_early_graph(ftrace_graph_buf, 1); 4349 if (ftrace_graph_notrace_buf[0]) 4350 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0); 4351#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ 4352} 4353 4354int ftrace_regex_release(struct inode *inode, struct file *file) 4355{ 4356 struct seq_file *m = (struct seq_file *)file->private_data; 4357 struct ftrace_ops_hash old_hash_ops; 4358 struct ftrace_iterator *iter; 4359 struct ftrace_hash **orig_hash; 4360 struct ftrace_hash *old_hash; 4361 struct trace_parser *parser; 4362 int filter_hash; 4363 int ret; 4364 4365 if (file->f_mode & FMODE_READ) { 4366 iter = m->private; 4367 seq_release(inode, file); 4368 } else 4369 iter = file->private_data; 4370 4371 parser = &iter->parser; 4372 if (trace_parser_loaded(parser)) { 4373 parser->buffer[parser->idx] = 0; 4374 ftrace_match_records(iter->hash, parser->buffer, parser->idx); 4375 } 4376 4377 trace_parser_put(parser); 4378 4379 mutex_lock(&iter->ops->func_hash->regex_lock); 4380 4381 if (file->f_mode & FMODE_WRITE) { 4382 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER); 4383 4384 if (filter_hash) 4385 orig_hash = &iter->ops->func_hash->filter_hash; 4386 else 4387 orig_hash = &iter->ops->func_hash->notrace_hash; 4388 4389 mutex_lock(&ftrace_lock); 4390 old_hash = *orig_hash; 4391 old_hash_ops.filter_hash = iter->ops->func_hash->filter_hash; 4392 old_hash_ops.notrace_hash = iter->ops->func_hash->notrace_hash; 4393 ret = ftrace_hash_move(iter->ops, filter_hash, 4394 orig_hash, iter->hash); 4395 if (!ret) { 4396 ftrace_ops_update_code(iter->ops, &old_hash_ops); 4397 free_ftrace_hash_rcu(old_hash); 4398 } 4399 mutex_unlock(&ftrace_lock); 4400 } 4401 4402 mutex_unlock(&iter->ops->func_hash->regex_lock); 4403 free_ftrace_hash(iter->hash); 4404 kfree(iter); 4405 4406 return 0; 4407} 4408 4409static const struct file_operations ftrace_avail_fops = { 4410 .open = ftrace_avail_open, 4411 .read = seq_read, 4412 .llseek = seq_lseek, 4413 .release = seq_release_private, 4414}; 4415 4416static const struct file_operations ftrace_enabled_fops = { 4417 .open = ftrace_enabled_open, 4418 .read = seq_read, 4419 .llseek = seq_lseek, 4420 .release = seq_release_private, 4421}; 4422 4423static const struct file_operations ftrace_filter_fops = { 4424 .open = ftrace_filter_open, 4425 .read = seq_read, 4426 .write = ftrace_filter_write, 4427 .llseek = tracing_lseek, 4428 .release = ftrace_regex_release, 4429}; 4430 4431static const struct file_operations ftrace_notrace_fops = { 4432 .open = ftrace_notrace_open, 4433 .read = seq_read, 4434 .write = ftrace_notrace_write, 4435 .llseek = tracing_lseek, 4436 .release = ftrace_regex_release, 4437}; 4438 4439#ifdef CONFIG_FUNCTION_GRAPH_TRACER 4440 4441static DEFINE_MUTEX(graph_lock); 4442 4443int ftrace_graph_count; 4444int ftrace_graph_notrace_count; 4445unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly; 4446unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly; 4447 4448struct ftrace_graph_data { 4449 unsigned long *table; 4450 size_t size; 4451 int *count; 4452 const struct seq_operations *seq_ops; 4453}; 4454 4455static void * 4456__g_next(struct seq_file *m, loff_t *pos) 4457{ 4458 struct ftrace_graph_data *fgd = m->private; 4459 4460 if (*pos >= *fgd->count) 4461 return NULL; 4462 return &fgd->table[*pos]; 4463} 4464 4465static void * 4466g_next(struct seq_file *m, void *v, loff_t *pos) 4467{ 4468 (*pos)++; 4469 return __g_next(m, pos); 4470} 4471 4472static void *g_start(struct seq_file *m, loff_t *pos) 4473{ 4474 struct ftrace_graph_data *fgd = m->private; 4475 4476 mutex_lock(&graph_lock); 4477 4478 /* Nothing, tell g_show to print all functions are enabled */ 4479 if (!*fgd->count && !*pos) 4480 return (void *)1; 4481 4482 return __g_next(m, pos); 4483} 4484 4485static void g_stop(struct seq_file *m, void *p) 4486{ 4487 mutex_unlock(&graph_lock); 4488} 4489 4490static int g_show(struct seq_file *m, void *v) 4491{ 4492 unsigned long *ptr = v; 4493 4494 if (!ptr) 4495 return 0; 4496 4497 if (ptr == (unsigned long *)1) { 4498 struct ftrace_graph_data *fgd = m->private; 4499 4500 if (fgd->table == ftrace_graph_funcs) 4501 seq_puts(m, "#### all functions enabled ####\n"); 4502 else 4503 seq_puts(m, "#### no functions disabled ####\n"); 4504 return 0; 4505 } 4506 4507 seq_printf(m, "%ps\n", (void *)*ptr); 4508 4509 return 0; 4510} 4511 4512static const struct seq_operations ftrace_graph_seq_ops = { 4513 .start = g_start, 4514 .next = g_next, 4515 .stop = g_stop, 4516 .show = g_show, 4517}; 4518 4519static int 4520__ftrace_graph_open(struct inode *inode, struct file *file, 4521 struct ftrace_graph_data *fgd) 4522{ 4523 int ret = 0; 4524 4525 mutex_lock(&graph_lock); 4526 if ((file->f_mode & FMODE_WRITE) && 4527 (file->f_flags & O_TRUNC)) { 4528 *fgd->count = 0; 4529 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table)); 4530 } 4531 mutex_unlock(&graph_lock); 4532 4533 if (file->f_mode & FMODE_READ) { 4534 ret = seq_open(file, fgd->seq_ops); 4535 if (!ret) { 4536 struct seq_file *m = file->private_data; 4537 m->private = fgd; 4538 } 4539 } else 4540 file->private_data = fgd; 4541 4542 return ret; 4543} 4544 4545static int 4546ftrace_graph_open(struct inode *inode, struct file *file) 4547{ 4548 struct ftrace_graph_data *fgd; 4549 4550 if (unlikely(ftrace_disabled)) 4551 return -ENODEV; 4552 4553 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL); 4554 if (fgd == NULL) 4555 return -ENOMEM; 4556 4557 fgd->table = ftrace_graph_funcs; 4558 fgd->size = FTRACE_GRAPH_MAX_FUNCS; 4559 fgd->count = &ftrace_graph_count; 4560 fgd->seq_ops = &ftrace_graph_seq_ops; 4561 4562 return __ftrace_graph_open(inode, file, fgd); 4563} 4564 4565static int 4566ftrace_graph_notrace_open(struct inode *inode, struct file *file) 4567{ 4568 struct ftrace_graph_data *fgd; 4569 4570 if (unlikely(ftrace_disabled)) 4571 return -ENODEV; 4572 4573 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL); 4574 if (fgd == NULL) 4575 return -ENOMEM; 4576 4577 fgd->table = ftrace_graph_notrace_funcs; 4578 fgd->size = FTRACE_GRAPH_MAX_FUNCS; 4579 fgd->count = &ftrace_graph_notrace_count; 4580 fgd->seq_ops = &ftrace_graph_seq_ops; 4581 4582 return __ftrace_graph_open(inode, file, fgd); 4583} 4584 4585static int 4586ftrace_graph_release(struct inode *inode, struct file *file) 4587{ 4588 if (file->f_mode & FMODE_READ) { 4589 struct seq_file *m = file->private_data; 4590 4591 kfree(m->private); 4592 seq_release(inode, file); 4593 } else { 4594 kfree(file->private_data); 4595 } 4596 4597 return 0; 4598} 4599 4600static int 4601ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer) 4602{ 4603 struct dyn_ftrace *rec; 4604 struct ftrace_page *pg; 4605 int search_len; 4606 int fail = 1; 4607 int type, not; 4608 char *search; 4609 bool exists; 4610 int i; 4611 4612 /* decode regex */ 4613 type = filter_parse_regex(buffer, strlen(buffer), &search, ¬); 4614 if (!not && *idx >= size) 4615 return -EBUSY; 4616 4617 search_len = strlen(search); 4618 4619 mutex_lock(&ftrace_lock); 4620 4621 if (unlikely(ftrace_disabled)) { 4622 mutex_unlock(&ftrace_lock); 4623 return -ENODEV; 4624 } 4625 4626 do_for_each_ftrace_rec(pg, rec) { 4627 4628 if (ftrace_match_record(rec, NULL, search, search_len, type)) { 4629 /* if it is in the array */ 4630 exists = false; 4631 for (i = 0; i < *idx; i++) { 4632 if (array[i] == rec->ip) { 4633 exists = true; 4634 break; 4635 } 4636 } 4637 4638 if (!not) { 4639 fail = 0; 4640 if (!exists) { 4641 array[(*idx)++] = rec->ip; 4642 if (*idx >= size) 4643 goto out; 4644 } 4645 } else { 4646 if (exists) { 4647 array[i] = array[--(*idx)]; 4648 array[*idx] = 0; 4649 fail = 0; 4650 } 4651 } 4652 } 4653 } while_for_each_ftrace_rec(); 4654out: 4655 mutex_unlock(&ftrace_lock); 4656 4657 if (fail) 4658 return -EINVAL; 4659 4660 return 0; 4661} 4662 4663static ssize_t 4664ftrace_graph_write(struct file *file, const char __user *ubuf, 4665 size_t cnt, loff_t *ppos) 4666{ 4667 struct trace_parser parser; 4668 ssize_t read, ret = 0; 4669 struct ftrace_graph_data *fgd = file->private_data; 4670 4671 if (!cnt) 4672 return 0; 4673 4674 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) 4675 return -ENOMEM; 4676 4677 read = trace_get_user(&parser, ubuf, cnt, ppos); 4678 4679 if (read >= 0 && trace_parser_loaded((&parser))) { 4680 parser.buffer[parser.idx] = 0; 4681 4682 mutex_lock(&graph_lock); 4683 4684 /* we allow only one expression at a time */ 4685 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size, 4686 parser.buffer); 4687 4688 mutex_unlock(&graph_lock); 4689 } 4690 4691 if (!ret) 4692 ret = read; 4693 4694 trace_parser_put(&parser); 4695 4696 return ret; 4697} 4698 4699static const struct file_operations ftrace_graph_fops = { 4700 .open = ftrace_graph_open, 4701 .read = seq_read, 4702 .write = ftrace_graph_write, 4703 .llseek = tracing_lseek, 4704 .release = ftrace_graph_release, 4705}; 4706 4707static const struct file_operations ftrace_graph_notrace_fops = { 4708 .open = ftrace_graph_notrace_open, 4709 .read = seq_read, 4710 .write = ftrace_graph_write, 4711 .llseek = tracing_lseek, 4712 .release = ftrace_graph_release, 4713}; 4714#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ 4715 4716void ftrace_create_filter_files(struct ftrace_ops *ops, 4717 struct dentry *parent) 4718{ 4719 4720 trace_create_file("set_ftrace_filter", 0644, parent, 4721 ops, &ftrace_filter_fops); 4722 4723 trace_create_file("set_ftrace_notrace", 0644, parent, 4724 ops, &ftrace_notrace_fops); 4725} 4726 4727/* 4728 * The name "destroy_filter_files" is really a misnomer. Although 4729 * in the future, it may actualy delete the files, but this is 4730 * really intended to make sure the ops passed in are disabled 4731 * and that when this function returns, the caller is free to 4732 * free the ops. 4733 * 4734 * The "destroy" name is only to match the "create" name that this 4735 * should be paired with. 4736 */ 4737void ftrace_destroy_filter_files(struct ftrace_ops *ops) 4738{ 4739 mutex_lock(&ftrace_lock); 4740 if (ops->flags & FTRACE_OPS_FL_ENABLED) 4741 ftrace_shutdown(ops, 0); 4742 ops->flags |= FTRACE_OPS_FL_DELETED; 4743 mutex_unlock(&ftrace_lock); 4744} 4745 4746static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer) 4747{ 4748 4749 trace_create_file("available_filter_functions", 0444, 4750 d_tracer, NULL, &ftrace_avail_fops); 4751 4752 trace_create_file("enabled_functions", 0444, 4753 d_tracer, NULL, &ftrace_enabled_fops); 4754 4755 ftrace_create_filter_files(&global_ops, d_tracer); 4756 4757#ifdef CONFIG_FUNCTION_GRAPH_TRACER 4758 trace_create_file("set_graph_function", 0444, d_tracer, 4759 NULL, 4760 &ftrace_graph_fops); 4761 trace_create_file("set_graph_notrace", 0444, d_tracer, 4762 NULL, 4763 &ftrace_graph_notrace_fops); 4764#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ 4765 4766 return 0; 4767} 4768 4769static int ftrace_cmp_ips(const void *a, const void *b) 4770{ 4771 const unsigned long *ipa = a; 4772 const unsigned long *ipb = b; 4773 4774 if (*ipa > *ipb) 4775 return 1; 4776 if (*ipa < *ipb) 4777 return -1; 4778 return 0; 4779} 4780 4781static void ftrace_swap_ips(void *a, void *b, int size) 4782{ 4783 unsigned long *ipa = a; 4784 unsigned long *ipb = b; 4785 unsigned long t; 4786 4787 t = *ipa; 4788 *ipa = *ipb; 4789 *ipb = t; 4790} 4791 4792static int ftrace_process_locs(struct module *mod, 4793 unsigned long *start, 4794 unsigned long *end) 4795{ 4796 struct ftrace_page *start_pg; 4797 struct ftrace_page *pg; 4798 struct dyn_ftrace *rec; 4799 unsigned long count; 4800 unsigned long *p; 4801 unsigned long addr; 4802 unsigned long flags = 0; /* Shut up gcc */ 4803 int ret = -ENOMEM; 4804 4805 count = end - start; 4806 4807 if (!count) 4808 return 0; 4809 4810 sort(start, count, sizeof(*start), 4811 ftrace_cmp_ips, ftrace_swap_ips); 4812 4813 start_pg = ftrace_allocate_pages(count); 4814 if (!start_pg) 4815 return -ENOMEM; 4816 4817 mutex_lock(&ftrace_lock); 4818 4819 /* 4820 * Core and each module needs their own pages, as 4821 * modules will free them when they are removed. 4822 * Force a new page to be allocated for modules. 4823 */ 4824 if (!mod) { 4825 WARN_ON(ftrace_pages || ftrace_pages_start); 4826 /* First initialization */ 4827 ftrace_pages = ftrace_pages_start = start_pg; 4828 } else { 4829 if (!ftrace_pages) 4830 goto out; 4831 4832 if (WARN_ON(ftrace_pages->next)) { 4833 /* Hmm, we have free pages? */ 4834 while (ftrace_pages->next) 4835 ftrace_pages = ftrace_pages->next; 4836 } 4837 4838 ftrace_pages->next = start_pg; 4839 } 4840 4841 p = start; 4842 pg = start_pg; 4843 while (p < end) { 4844 addr = ftrace_call_adjust(*p++); 4845 /* 4846 * Some architecture linkers will pad between 4847 * the different mcount_loc sections of different 4848 * object files to satisfy alignments. 4849 * Skip any NULL pointers. 4850 */ 4851 if (!addr) 4852 continue; 4853 4854 if (pg->index == pg->size) { 4855 /* We should have allocated enough */ 4856 if (WARN_ON(!pg->next)) 4857 break; 4858 pg = pg->next; 4859 } 4860 4861 rec = &pg->records[pg->index++]; 4862 rec->ip = addr; 4863 } 4864 4865 /* We should have used all pages */ 4866 WARN_ON(pg->next); 4867 4868 /* Assign the last page to ftrace_pages */ 4869 ftrace_pages = pg; 4870 4871 /* 4872 * We only need to disable interrupts on start up 4873 * because we are modifying code that an interrupt 4874 * may execute, and the modification is not atomic. 4875 * But for modules, nothing runs the code we modify 4876 * until we are finished with it, and there's no 4877 * reason to cause large interrupt latencies while we do it. 4878 */ 4879 if (!mod) 4880 local_irq_save(flags); 4881 ftrace_update_code(mod, start_pg); 4882 if (!mod) 4883 local_irq_restore(flags); 4884 ret = 0; 4885 out: 4886 mutex_unlock(&ftrace_lock); 4887 4888 return ret; 4889} 4890 4891#ifdef CONFIG_MODULES 4892 4893#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next) 4894 4895void ftrace_release_mod(struct module *mod) 4896{ 4897 struct dyn_ftrace *rec; 4898 struct ftrace_page **last_pg; 4899 struct ftrace_page *pg; 4900 int order; 4901 4902 mutex_lock(&ftrace_lock); 4903 4904 if (ftrace_disabled) 4905 goto out_unlock; 4906 4907 /* 4908 * Each module has its own ftrace_pages, remove 4909 * them from the list. 4910 */ 4911 last_pg = &ftrace_pages_start; 4912 for (pg = ftrace_pages_start; pg; pg = *last_pg) { 4913 rec = &pg->records[0]; 4914 if (within_module_core(rec->ip, mod)) { 4915 /* 4916 * As core pages are first, the first 4917 * page should never be a module page. 4918 */ 4919 if (WARN_ON(pg == ftrace_pages_start)) 4920 goto out_unlock; 4921 4922 /* Check if we are deleting the last page */ 4923 if (pg == ftrace_pages) 4924 ftrace_pages = next_to_ftrace_page(last_pg); 4925 4926 *last_pg = pg->next; 4927 order = get_count_order(pg->size / ENTRIES_PER_PAGE); 4928 free_pages((unsigned long)pg->records, order); 4929 kfree(pg); 4930 } else 4931 last_pg = &pg->next; 4932 } 4933 out_unlock: 4934 mutex_unlock(&ftrace_lock); 4935} 4936 4937static void ftrace_init_module(struct module *mod, 4938 unsigned long *start, unsigned long *end) 4939{ 4940 if (ftrace_disabled || start == end) 4941 return; 4942 ftrace_process_locs(mod, start, end); 4943} 4944 4945void ftrace_module_init(struct module *mod) 4946{ 4947 ftrace_init_module(mod, mod->ftrace_callsites, 4948 mod->ftrace_callsites + 4949 mod->num_ftrace_callsites); 4950} 4951 4952static int ftrace_module_notify_exit(struct notifier_block *self, 4953 unsigned long val, void *data) 4954{ 4955 struct module *mod = data; 4956 4957 if (val == MODULE_STATE_GOING) 4958 ftrace_release_mod(mod); 4959 4960 return 0; 4961} 4962#else 4963static int ftrace_module_notify_exit(struct notifier_block *self, 4964 unsigned long val, void *data) 4965{ 4966 return 0; 4967} 4968#endif /* CONFIG_MODULES */ 4969 4970struct notifier_block ftrace_module_exit_nb = { 4971 .notifier_call = ftrace_module_notify_exit, 4972 .priority = INT_MIN, /* Run after anything that can remove kprobes */ 4973}; 4974 4975void __init ftrace_init(void) 4976{ 4977 extern unsigned long __start_mcount_loc[]; 4978 extern unsigned long __stop_mcount_loc[]; 4979 unsigned long count, flags; 4980 int ret; 4981 4982 local_irq_save(flags); 4983 ret = ftrace_dyn_arch_init(); 4984 local_irq_restore(flags); 4985 if (ret) 4986 goto failed; 4987 4988 count = __stop_mcount_loc - __start_mcount_loc; 4989 if (!count) { 4990 pr_info("ftrace: No functions to be traced?\n"); 4991 goto failed; 4992 } 4993 4994 pr_info("ftrace: allocating %ld entries in %ld pages\n", 4995 count, count / ENTRIES_PER_PAGE + 1); 4996 4997 last_ftrace_enabled = ftrace_enabled = 1; 4998 4999 ret = ftrace_process_locs(NULL, 5000 __start_mcount_loc, 5001 __stop_mcount_loc); 5002 5003 ret = register_module_notifier(&ftrace_module_exit_nb); 5004 if (ret) 5005 pr_warning("Failed to register trace ftrace module exit notifier\n"); 5006 5007 set_ftrace_early_filters(); 5008 5009 return; 5010 failed: 5011 ftrace_disabled = 1; 5012} 5013 5014/* Do nothing if arch does not support this */ 5015void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops) 5016{ 5017} 5018 5019static void ftrace_update_trampoline(struct ftrace_ops *ops) 5020{ 5021 5022/* 5023 * Currently there's no safe way to free a trampoline when the kernel 5024 * is configured with PREEMPT. That is because a task could be preempted 5025 * when it jumped to the trampoline, it may be preempted for a long time 5026 * depending on the system load, and currently there's no way to know 5027 * when it will be off the trampoline. If the trampoline is freed 5028 * too early, when the task runs again, it will be executing on freed 5029 * memory and crash. 5030 */ 5031#ifdef CONFIG_PREEMPT 5032 /* Currently, only non dynamic ops can have a trampoline */ 5033 if (ops->flags & FTRACE_OPS_FL_DYNAMIC) 5034 return; 5035#endif 5036 5037 arch_ftrace_update_trampoline(ops); 5038} 5039 5040#else 5041 5042static struct ftrace_ops global_ops = { 5043 .func = ftrace_stub, 5044 .flags = FTRACE_OPS_FL_RECURSION_SAFE | 5045 FTRACE_OPS_FL_INITIALIZED | 5046 FTRACE_OPS_FL_PID, 5047}; 5048 5049static int __init ftrace_nodyn_init(void) 5050{ 5051 ftrace_enabled = 1; 5052 return 0; 5053} 5054core_initcall(ftrace_nodyn_init); 5055 5056static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; } 5057static inline void ftrace_startup_enable(int command) { } 5058static inline void ftrace_startup_all(int command) { } 5059/* Keep as macros so we do not need to define the commands */ 5060# define ftrace_startup(ops, command) \ 5061 ({ \ 5062 int ___ret = __register_ftrace_function(ops); \ 5063 if (!___ret) \ 5064 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \ 5065 ___ret; \ 5066 }) 5067# define ftrace_shutdown(ops, command) \ 5068 ({ \ 5069 int ___ret = __unregister_ftrace_function(ops); \ 5070 if (!___ret) \ 5071 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \ 5072 ___ret; \ 5073 }) 5074 5075# define ftrace_startup_sysctl() do { } while (0) 5076# define ftrace_shutdown_sysctl() do { } while (0) 5077 5078static inline int 5079ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs) 5080{ 5081 return 1; 5082} 5083 5084static void ftrace_update_trampoline(struct ftrace_ops *ops) 5085{ 5086} 5087 5088#endif /* CONFIG_DYNAMIC_FTRACE */ 5089 5090__init void ftrace_init_global_array_ops(struct trace_array *tr) 5091{ 5092 tr->ops = &global_ops; 5093 tr->ops->private = tr; 5094} 5095 5096void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func) 5097{ 5098 /* If we filter on pids, update to use the pid function */ 5099 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) { 5100 if (WARN_ON(tr->ops->func != ftrace_stub)) 5101 printk("ftrace ops had %pS for function\n", 5102 tr->ops->func); 5103 } 5104 tr->ops->func = func; 5105 tr->ops->private = tr; 5106} 5107 5108void ftrace_reset_array_ops(struct trace_array *tr) 5109{ 5110 tr->ops->func = ftrace_stub; 5111} 5112 5113static void 5114ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip, 5115 struct ftrace_ops *op, struct pt_regs *regs) 5116{ 5117 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT))) 5118 return; 5119 5120 /* 5121 * Some of the ops may be dynamically allocated, 5122 * they must be freed after a synchronize_sched(). 5123 */ 5124 preempt_disable_notrace(); 5125 trace_recursion_set(TRACE_CONTROL_BIT); 5126 5127 /* 5128 * Control funcs (perf) uses RCU. Only trace if 5129 * RCU is currently active. 5130 */ 5131 if (!rcu_is_watching()) 5132 goto out; 5133 5134 do_for_each_ftrace_op(op, ftrace_control_list) { 5135 if (!(op->flags & FTRACE_OPS_FL_STUB) && 5136 !ftrace_function_local_disabled(op) && 5137 ftrace_ops_test(op, ip, regs)) 5138 op->func(ip, parent_ip, op, regs); 5139 } while_for_each_ftrace_op(op); 5140 out: 5141 trace_recursion_clear(TRACE_CONTROL_BIT); 5142 preempt_enable_notrace(); 5143} 5144 5145static struct ftrace_ops control_ops = { 5146 .func = ftrace_ops_control_func, 5147 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED, 5148 INIT_OPS_HASH(control_ops) 5149}; 5150 5151static inline void 5152__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, 5153 struct ftrace_ops *ignored, struct pt_regs *regs) 5154{ 5155 struct ftrace_ops *op; 5156 int bit; 5157 5158 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX); 5159 if (bit < 0) 5160 return; 5161 5162 /* 5163 * Some of the ops may be dynamically allocated, 5164 * they must be freed after a synchronize_sched(). 5165 */ 5166 preempt_disable_notrace(); 5167 do_for_each_ftrace_op(op, ftrace_ops_list) { 5168 if (ftrace_ops_test(op, ip, regs)) { 5169 if (FTRACE_WARN_ON(!op->func)) { 5170 pr_warn("op=%p %pS\n", op, op); 5171 goto out; 5172 } 5173 op->func(ip, parent_ip, op, regs); 5174 } 5175 } while_for_each_ftrace_op(op); 5176out: 5177 preempt_enable_notrace(); 5178 trace_clear_recursion(bit); 5179} 5180 5181/* 5182 * Some archs only support passing ip and parent_ip. Even though 5183 * the list function ignores the op parameter, we do not want any 5184 * C side effects, where a function is called without the caller 5185 * sending a third parameter. 5186 * Archs are to support both the regs and ftrace_ops at the same time. 5187 * If they support ftrace_ops, it is assumed they support regs. 5188 * If call backs want to use regs, they must either check for regs 5189 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS. 5190 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved. 5191 * An architecture can pass partial regs with ftrace_ops and still 5192 * set the ARCH_SUPPORT_FTARCE_OPS. 5193 */ 5194#if ARCH_SUPPORTS_FTRACE_OPS 5195static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, 5196 struct ftrace_ops *op, struct pt_regs *regs) 5197{ 5198 __ftrace_ops_list_func(ip, parent_ip, NULL, regs); 5199} 5200#else 5201static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip) 5202{ 5203 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL); 5204} 5205#endif 5206 5207/* 5208 * If there's only one function registered but it does not support 5209 * recursion, this function will be called by the mcount trampoline. 5210 * This function will handle recursion protection. 5211 */ 5212static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip, 5213 struct ftrace_ops *op, struct pt_regs *regs) 5214{ 5215 int bit; 5216 5217 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX); 5218 if (bit < 0) 5219 return; 5220 5221 op->func(ip, parent_ip, op, regs); 5222 5223 trace_clear_recursion(bit); 5224} 5225 5226/** 5227 * ftrace_ops_get_func - get the function a trampoline should call 5228 * @ops: the ops to get the function for 5229 * 5230 * Normally the mcount trampoline will call the ops->func, but there 5231 * are times that it should not. For example, if the ops does not 5232 * have its own recursion protection, then it should call the 5233 * ftrace_ops_recurs_func() instead. 5234 * 5235 * Returns the function that the trampoline should call for @ops. 5236 */ 5237ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops) 5238{ 5239 /* 5240 * If the func handles its own recursion, call it directly. 5241 * Otherwise call the recursion protected function that 5242 * will call the ftrace ops function. 5243 */ 5244 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE)) 5245 return ftrace_ops_recurs_func; 5246 5247 return ops->func; 5248} 5249 5250static void clear_ftrace_swapper(void) 5251{ 5252 struct task_struct *p; 5253 int cpu; 5254 5255 get_online_cpus(); 5256 for_each_online_cpu(cpu) { 5257 p = idle_task(cpu); 5258 clear_tsk_trace_trace(p); 5259 } 5260 put_online_cpus(); 5261} 5262 5263static void set_ftrace_swapper(void) 5264{ 5265 struct task_struct *p; 5266 int cpu; 5267 5268 get_online_cpus(); 5269 for_each_online_cpu(cpu) { 5270 p = idle_task(cpu); 5271 set_tsk_trace_trace(p); 5272 } 5273 put_online_cpus(); 5274} 5275 5276static void clear_ftrace_pid(struct pid *pid) 5277{ 5278 struct task_struct *p; 5279 5280 rcu_read_lock(); 5281 do_each_pid_task(pid, PIDTYPE_PID, p) { 5282 clear_tsk_trace_trace(p); 5283 } while_each_pid_task(pid, PIDTYPE_PID, p); 5284 rcu_read_unlock(); 5285 5286 put_pid(pid); 5287} 5288 5289static void set_ftrace_pid(struct pid *pid) 5290{ 5291 struct task_struct *p; 5292 5293 rcu_read_lock(); 5294 do_each_pid_task(pid, PIDTYPE_PID, p) { 5295 set_tsk_trace_trace(p); 5296 } while_each_pid_task(pid, PIDTYPE_PID, p); 5297 rcu_read_unlock(); 5298} 5299 5300static void clear_ftrace_pid_task(struct pid *pid) 5301{ 5302 if (pid == ftrace_swapper_pid) 5303 clear_ftrace_swapper(); 5304 else 5305 clear_ftrace_pid(pid); 5306} 5307 5308static void set_ftrace_pid_task(struct pid *pid) 5309{ 5310 if (pid == ftrace_swapper_pid) 5311 set_ftrace_swapper(); 5312 else 5313 set_ftrace_pid(pid); 5314} 5315 5316static int ftrace_pid_add(int p) 5317{ 5318 struct pid *pid; 5319 struct ftrace_pid *fpid; 5320 int ret = -EINVAL; 5321 5322 mutex_lock(&ftrace_lock); 5323 5324 if (!p) 5325 pid = ftrace_swapper_pid; 5326 else 5327 pid = find_get_pid(p); 5328 5329 if (!pid) 5330 goto out; 5331 5332 ret = 0; 5333 5334 list_for_each_entry(fpid, &ftrace_pids, list) 5335 if (fpid->pid == pid) 5336 goto out_put; 5337 5338 ret = -ENOMEM; 5339 5340 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL); 5341 if (!fpid) 5342 goto out_put; 5343 5344 list_add(&fpid->list, &ftrace_pids); 5345 fpid->pid = pid; 5346 5347 set_ftrace_pid_task(pid); 5348 5349 ftrace_update_pid_func(); 5350 5351 ftrace_startup_all(0); 5352 5353 mutex_unlock(&ftrace_lock); 5354 return 0; 5355 5356out_put: 5357 if (pid != ftrace_swapper_pid) 5358 put_pid(pid); 5359 5360out: 5361 mutex_unlock(&ftrace_lock); 5362 return ret; 5363} 5364 5365static void ftrace_pid_reset(void) 5366{ 5367 struct ftrace_pid *fpid, *safe; 5368 5369 mutex_lock(&ftrace_lock); 5370 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) { 5371 struct pid *pid = fpid->pid; 5372 5373 clear_ftrace_pid_task(pid); 5374 5375 list_del(&fpid->list); 5376 kfree(fpid); 5377 } 5378 5379 ftrace_update_pid_func(); 5380 ftrace_startup_all(0); 5381 5382 mutex_unlock(&ftrace_lock); 5383} 5384 5385static void *fpid_start(struct seq_file *m, loff_t *pos) 5386{ 5387 mutex_lock(&ftrace_lock); 5388 5389 if (!ftrace_pids_enabled() && (!*pos)) 5390 return (void *) 1; 5391 5392 return seq_list_start(&ftrace_pids, *pos); 5393} 5394 5395static void *fpid_next(struct seq_file *m, void *v, loff_t *pos) 5396{ 5397 if (v == (void *)1) 5398 return NULL; 5399 5400 return seq_list_next(v, &ftrace_pids, pos); 5401} 5402 5403static void fpid_stop(struct seq_file *m, void *p) 5404{ 5405 mutex_unlock(&ftrace_lock); 5406} 5407 5408static int fpid_show(struct seq_file *m, void *v) 5409{ 5410 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list); 5411 5412 if (v == (void *)1) { 5413 seq_puts(m, "no pid\n"); 5414 return 0; 5415 } 5416 5417 if (fpid->pid == ftrace_swapper_pid) 5418 seq_puts(m, "swapper tasks\n"); 5419 else 5420 seq_printf(m, "%u\n", pid_vnr(fpid->pid)); 5421 5422 return 0; 5423} 5424 5425static const struct seq_operations ftrace_pid_sops = { 5426 .start = fpid_start, 5427 .next = fpid_next, 5428 .stop = fpid_stop, 5429 .show = fpid_show, 5430}; 5431 5432static int 5433ftrace_pid_open(struct inode *inode, struct file *file) 5434{ 5435 int ret = 0; 5436 5437 if ((file->f_mode & FMODE_WRITE) && 5438 (file->f_flags & O_TRUNC)) 5439 ftrace_pid_reset(); 5440 5441 if (file->f_mode & FMODE_READ) 5442 ret = seq_open(file, &ftrace_pid_sops); 5443 5444 return ret; 5445} 5446 5447static ssize_t 5448ftrace_pid_write(struct file *filp, const char __user *ubuf, 5449 size_t cnt, loff_t *ppos) 5450{ 5451 char buf[64], *tmp; 5452 long val; 5453 int ret; 5454 5455 if (cnt >= sizeof(buf)) 5456 return -EINVAL; 5457 5458 if (copy_from_user(&buf, ubuf, cnt)) 5459 return -EFAULT; 5460 5461 buf[cnt] = 0; 5462 5463 /* 5464 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid" 5465 * to clean the filter quietly. 5466 */ 5467 tmp = strstrip(buf); 5468 if (strlen(tmp) == 0) 5469 return 1; 5470 5471 ret = kstrtol(tmp, 10, &val); 5472 if (ret < 0) 5473 return ret; 5474 5475 ret = ftrace_pid_add(val); 5476 5477 return ret ? ret : cnt; 5478} 5479 5480static int 5481ftrace_pid_release(struct inode *inode, struct file *file) 5482{ 5483 if (file->f_mode & FMODE_READ) 5484 seq_release(inode, file); 5485 5486 return 0; 5487} 5488 5489static const struct file_operations ftrace_pid_fops = { 5490 .open = ftrace_pid_open, 5491 .write = ftrace_pid_write, 5492 .read = seq_read, 5493 .llseek = tracing_lseek, 5494 .release = ftrace_pid_release, 5495}; 5496 5497static __init int ftrace_init_tracefs(void) 5498{ 5499 struct dentry *d_tracer; 5500 5501 d_tracer = tracing_init_dentry(); 5502 if (IS_ERR(d_tracer)) 5503 return 0; 5504 5505 ftrace_init_dyn_tracefs(d_tracer); 5506 5507 trace_create_file("set_ftrace_pid", 0644, d_tracer, 5508 NULL, &ftrace_pid_fops); 5509 5510 ftrace_profile_tracefs(d_tracer); 5511 5512 return 0; 5513} 5514fs_initcall(ftrace_init_tracefs); 5515 5516/** 5517 * ftrace_kill - kill ftrace 5518 * 5519 * This function should be used by panic code. It stops ftrace 5520 * but in a not so nice way. If you need to simply kill ftrace 5521 * from a non-atomic section, use ftrace_kill. 5522 */ 5523void ftrace_kill(void) 5524{ 5525 ftrace_disabled = 1; 5526 ftrace_enabled = 0; 5527 clear_ftrace_function(); 5528} 5529 5530/** 5531 * Test if ftrace is dead or not. 5532 */ 5533int ftrace_is_dead(void) 5534{ 5535 return ftrace_disabled; 5536} 5537 5538/** 5539 * register_ftrace_function - register a function for profiling 5540 * @ops - ops structure that holds the function for profiling. 5541 * 5542 * Register a function to be called by all functions in the 5543 * kernel. 5544 * 5545 * Note: @ops->func and all the functions it calls must be labeled 5546 * with "notrace", otherwise it will go into a 5547 * recursive loop. 5548 */ 5549int register_ftrace_function(struct ftrace_ops *ops) 5550{ 5551 int ret = -1; 5552 5553 ftrace_ops_init(ops); 5554 5555 mutex_lock(&ftrace_lock); 5556 5557 ret = ftrace_startup(ops, 0); 5558 5559 mutex_unlock(&ftrace_lock); 5560 5561 return ret; 5562} 5563EXPORT_SYMBOL_GPL(register_ftrace_function); 5564 5565/** 5566 * unregister_ftrace_function - unregister a function for profiling. 5567 * @ops - ops structure that holds the function to unregister 5568 * 5569 * Unregister a function that was added to be called by ftrace profiling. 5570 */ 5571int unregister_ftrace_function(struct ftrace_ops *ops) 5572{ 5573 int ret; 5574 5575 mutex_lock(&ftrace_lock); 5576 ret = ftrace_shutdown(ops, 0); 5577 mutex_unlock(&ftrace_lock); 5578 5579 return ret; 5580} 5581EXPORT_SYMBOL_GPL(unregister_ftrace_function); 5582 5583int 5584ftrace_enable_sysctl(struct ctl_table *table, int write, 5585 void __user *buffer, size_t *lenp, 5586 loff_t *ppos) 5587{ 5588 int ret = -ENODEV; 5589 5590 mutex_lock(&ftrace_lock); 5591 5592 if (unlikely(ftrace_disabled)) 5593 goto out; 5594 5595 ret = proc_dointvec(table, write, buffer, lenp, ppos); 5596 5597 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled)) 5598 goto out; 5599 5600 last_ftrace_enabled = !!ftrace_enabled; 5601 5602 if (ftrace_enabled) { 5603 5604 /* we are starting ftrace again */ 5605 if (ftrace_ops_list != &ftrace_list_end) 5606 update_ftrace_function(); 5607 5608 ftrace_startup_sysctl(); 5609 5610 } else { 5611 /* stopping ftrace calls (just send to ftrace_stub) */ 5612 ftrace_trace_function = ftrace_stub; 5613 5614 ftrace_shutdown_sysctl(); 5615 } 5616 5617 out: 5618 mutex_unlock(&ftrace_lock); 5619 return ret; 5620} 5621 5622#ifdef CONFIG_FUNCTION_GRAPH_TRACER 5623 5624static struct ftrace_ops graph_ops = { 5625 .func = ftrace_stub, 5626 .flags = FTRACE_OPS_FL_RECURSION_SAFE | 5627 FTRACE_OPS_FL_INITIALIZED | 5628 FTRACE_OPS_FL_PID | 5629 FTRACE_OPS_FL_STUB, 5630#ifdef FTRACE_GRAPH_TRAMP_ADDR 5631 .trampoline = FTRACE_GRAPH_TRAMP_ADDR, 5632 /* trampoline_size is only needed for dynamically allocated tramps */ 5633#endif 5634 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash) 5635}; 5636 5637int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace) 5638{ 5639 return 0; 5640} 5641 5642/* The callbacks that hook a function */ 5643trace_func_graph_ret_t ftrace_graph_return = 5644 (trace_func_graph_ret_t)ftrace_stub; 5645trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub; 5646static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub; 5647 5648/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */ 5649static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list) 5650{ 5651 int i; 5652 int ret = 0; 5653 unsigned long flags; 5654 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE; 5655 struct task_struct *g, *t; 5656 5657 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) { 5658 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH 5659 * sizeof(struct ftrace_ret_stack), 5660 GFP_KERNEL); 5661 if (!ret_stack_list[i]) { 5662 start = 0; 5663 end = i; 5664 ret = -ENOMEM; 5665 goto free; 5666 } 5667 } 5668 5669 read_lock_irqsave(&tasklist_lock, flags); 5670 do_each_thread(g, t) { 5671 if (start == end) { 5672 ret = -EAGAIN; 5673 goto unlock; 5674 } 5675 5676 if (t->ret_stack == NULL) { 5677 atomic_set(&t->tracing_graph_pause, 0); 5678 atomic_set(&t->trace_overrun, 0); 5679 t->curr_ret_stack = -1; 5680 /* Make sure the tasks see the -1 first: */ 5681 smp_wmb(); 5682 t->ret_stack = ret_stack_list[start++]; 5683 } 5684 } while_each_thread(g, t); 5685 5686unlock: 5687 read_unlock_irqrestore(&tasklist_lock, flags); 5688free: 5689 for (i = start; i < end; i++) 5690 kfree(ret_stack_list[i]); 5691 return ret; 5692} 5693 5694static void 5695ftrace_graph_probe_sched_switch(void *ignore, 5696 struct task_struct *prev, struct task_struct *next) 5697{ 5698 unsigned long long timestamp; 5699 int index; 5700 5701 /* 5702 * Does the user want to count the time a function was asleep. 5703 * If so, do not update the time stamps. 5704 */ 5705 if (trace_flags & TRACE_ITER_SLEEP_TIME) 5706 return; 5707 5708 timestamp = trace_clock_local(); 5709 5710 prev->ftrace_timestamp = timestamp; 5711 5712 /* only process tasks that we timestamped */ 5713 if (!next->ftrace_timestamp) 5714 return; 5715 5716 /* 5717 * Update all the counters in next to make up for the 5718 * time next was sleeping. 5719 */ 5720 timestamp -= next->ftrace_timestamp; 5721 5722 for (index = next->curr_ret_stack; index >= 0; index--) 5723 next->ret_stack[index].calltime += timestamp; 5724} 5725 5726/* Allocate a return stack for each task */ 5727static int start_graph_tracing(void) 5728{ 5729 struct ftrace_ret_stack **ret_stack_list; 5730 int ret, cpu; 5731 5732 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE * 5733 sizeof(struct ftrace_ret_stack *), 5734 GFP_KERNEL); 5735 5736 if (!ret_stack_list) 5737 return -ENOMEM; 5738 5739 /* The cpu_boot init_task->ret_stack will never be freed */ 5740 for_each_online_cpu(cpu) { 5741 if (!idle_task(cpu)->ret_stack) 5742 ftrace_graph_init_idle_task(idle_task(cpu), cpu); 5743 } 5744 5745 do { 5746 ret = alloc_retstack_tasklist(ret_stack_list); 5747 } while (ret == -EAGAIN); 5748 5749 if (!ret) { 5750 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL); 5751 if (ret) 5752 pr_info("ftrace_graph: Couldn't activate tracepoint" 5753 " probe to kernel_sched_switch\n"); 5754 } 5755 5756 kfree(ret_stack_list); 5757 return ret; 5758} 5759 5760/* 5761 * Hibernation protection. 5762 * The state of the current task is too much unstable during 5763 * suspend/restore to disk. We want to protect against that. 5764 */ 5765static int 5766ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state, 5767 void *unused) 5768{ 5769 switch (state) { 5770 case PM_HIBERNATION_PREPARE: 5771 pause_graph_tracing(); 5772 break; 5773 5774 case PM_POST_HIBERNATION: 5775 unpause_graph_tracing(); 5776 break; 5777 } 5778 return NOTIFY_DONE; 5779} 5780 5781static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace) 5782{ 5783 if (!ftrace_ops_test(&global_ops, trace->func, NULL)) 5784 return 0; 5785 return __ftrace_graph_entry(trace); 5786} 5787 5788/* 5789 * The function graph tracer should only trace the functions defined 5790 * by set_ftrace_filter and set_ftrace_notrace. If another function 5791 * tracer ops is registered, the graph tracer requires testing the 5792 * function against the global ops, and not just trace any function 5793 * that any ftrace_ops registered. 5794 */ 5795static void update_function_graph_func(void) 5796{ 5797 struct ftrace_ops *op; 5798 bool do_test = false; 5799 5800 /* 5801 * The graph and global ops share the same set of functions 5802 * to test. If any other ops is on the list, then 5803 * the graph tracing needs to test if its the function 5804 * it should call. 5805 */ 5806 do_for_each_ftrace_op(op, ftrace_ops_list) { 5807 if (op != &global_ops && op != &graph_ops && 5808 op != &ftrace_list_end) { 5809 do_test = true; 5810 /* in double loop, break out with goto */ 5811 goto out; 5812 } 5813 } while_for_each_ftrace_op(op); 5814 out: 5815 if (do_test) 5816 ftrace_graph_entry = ftrace_graph_entry_test; 5817 else 5818 ftrace_graph_entry = __ftrace_graph_entry; 5819} 5820 5821static struct notifier_block ftrace_suspend_notifier = { 5822 .notifier_call = ftrace_suspend_notifier_call, 5823}; 5824 5825int register_ftrace_graph(trace_func_graph_ret_t retfunc, 5826 trace_func_graph_ent_t entryfunc) 5827{ 5828 int ret = 0; 5829 5830 mutex_lock(&ftrace_lock); 5831 5832 /* we currently allow only one tracer registered at a time */ 5833 if (ftrace_graph_active) { 5834 ret = -EBUSY; 5835 goto out; 5836 } 5837 5838 register_pm_notifier(&ftrace_suspend_notifier); 5839 5840 ftrace_graph_active++; 5841 ret = start_graph_tracing(); 5842 if (ret) { 5843 ftrace_graph_active--; 5844 goto out; 5845 } 5846 5847 ftrace_graph_return = retfunc; 5848 5849 /* 5850 * Update the indirect function to the entryfunc, and the 5851 * function that gets called to the entry_test first. Then 5852 * call the update fgraph entry function to determine if 5853 * the entryfunc should be called directly or not. 5854 */ 5855 __ftrace_graph_entry = entryfunc; 5856 ftrace_graph_entry = ftrace_graph_entry_test; 5857 update_function_graph_func(); 5858 5859 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET); 5860out: 5861 mutex_unlock(&ftrace_lock); 5862 return ret; 5863} 5864 5865void unregister_ftrace_graph(void) 5866{ 5867 mutex_lock(&ftrace_lock); 5868 5869 if (unlikely(!ftrace_graph_active)) 5870 goto out; 5871 5872 ftrace_graph_active--; 5873 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub; 5874 ftrace_graph_entry = ftrace_graph_entry_stub; 5875 __ftrace_graph_entry = ftrace_graph_entry_stub; 5876 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET); 5877 unregister_pm_notifier(&ftrace_suspend_notifier); 5878 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL); 5879 5880#ifdef CONFIG_DYNAMIC_FTRACE 5881 /* 5882 * Function graph does not allocate the trampoline, but 5883 * other global_ops do. We need to reset the ALLOC_TRAMP flag 5884 * if one was used. 5885 */ 5886 global_ops.trampoline = save_global_trampoline; 5887 if (save_global_flags & FTRACE_OPS_FL_ALLOC_TRAMP) 5888 global_ops.flags |= FTRACE_OPS_FL_ALLOC_TRAMP; 5889#endif 5890 5891 out: 5892 mutex_unlock(&ftrace_lock); 5893} 5894 5895static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack); 5896 5897static void 5898graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack) 5899{ 5900 atomic_set(&t->tracing_graph_pause, 0); 5901 atomic_set(&t->trace_overrun, 0); 5902 t->ftrace_timestamp = 0; 5903 /* make curr_ret_stack visible before we add the ret_stack */ 5904 smp_wmb(); 5905 t->ret_stack = ret_stack; 5906} 5907 5908/* 5909 * Allocate a return stack for the idle task. May be the first 5910 * time through, or it may be done by CPU hotplug online. 5911 */ 5912void ftrace_graph_init_idle_task(struct task_struct *t, int cpu) 5913{ 5914 t->curr_ret_stack = -1; 5915 /* 5916 * The idle task has no parent, it either has its own 5917 * stack or no stack at all. 5918 */ 5919 if (t->ret_stack) 5920 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu)); 5921 5922 if (ftrace_graph_active) { 5923 struct ftrace_ret_stack *ret_stack; 5924 5925 ret_stack = per_cpu(idle_ret_stack, cpu); 5926 if (!ret_stack) { 5927 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH 5928 * sizeof(struct ftrace_ret_stack), 5929 GFP_KERNEL); 5930 if (!ret_stack) 5931 return; 5932 per_cpu(idle_ret_stack, cpu) = ret_stack; 5933 } 5934 graph_init_task(t, ret_stack); 5935 } 5936} 5937 5938/* Allocate a return stack for newly created task */ 5939void ftrace_graph_init_task(struct task_struct *t) 5940{ 5941 /* Make sure we do not use the parent ret_stack */ 5942 t->ret_stack = NULL; 5943 t->curr_ret_stack = -1; 5944 5945 if (ftrace_graph_active) { 5946 struct ftrace_ret_stack *ret_stack; 5947 5948 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH 5949 * sizeof(struct ftrace_ret_stack), 5950 GFP_KERNEL); 5951 if (!ret_stack) 5952 return; 5953 graph_init_task(t, ret_stack); 5954 } 5955} 5956 5957void ftrace_graph_exit_task(struct task_struct *t) 5958{ 5959 struct ftrace_ret_stack *ret_stack = t->ret_stack; 5960 5961 t->ret_stack = NULL; 5962 /* NULL must become visible to IRQs before we free it: */ 5963 barrier(); 5964 5965 kfree(ret_stack); 5966} 5967#endif 5968