at v6.10 40 kB view raw
1/* SPDX-License-Identifier: GPL-2.0 */ 2/* 3 * Ftrace header. For implementation details beyond the random comments 4 * scattered below, see: Documentation/trace/ftrace-design.rst 5 */ 6 7#ifndef _LINUX_FTRACE_H 8#define _LINUX_FTRACE_H 9 10#include <linux/trace_recursion.h> 11#include <linux/trace_clock.h> 12#include <linux/jump_label.h> 13#include <linux/kallsyms.h> 14#include <linux/linkage.h> 15#include <linux/bitops.h> 16#include <linux/ptrace.h> 17#include <linux/ktime.h> 18#include <linux/sched.h> 19#include <linux/types.h> 20#include <linux/init.h> 21#include <linux/fs.h> 22 23#include <asm/ftrace.h> 24 25/* 26 * If the arch supports passing the variable contents of 27 * function_trace_op as the third parameter back from the 28 * mcount call, then the arch should define this as 1. 29 */ 30#ifndef ARCH_SUPPORTS_FTRACE_OPS 31#define ARCH_SUPPORTS_FTRACE_OPS 0 32#endif 33 34#ifdef CONFIG_TRACING 35extern void ftrace_boot_snapshot(void); 36#else 37static inline void ftrace_boot_snapshot(void) { } 38#endif 39 40struct ftrace_ops; 41struct ftrace_regs; 42struct dyn_ftrace; 43 44char *arch_ftrace_match_adjust(char *str, const char *search); 45 46#ifdef CONFIG_HAVE_FUNCTION_GRAPH_RETVAL 47struct fgraph_ret_regs; 48unsigned long ftrace_return_to_handler(struct fgraph_ret_regs *ret_regs); 49#else 50unsigned long ftrace_return_to_handler(unsigned long frame_pointer); 51#endif 52 53#ifdef CONFIG_FUNCTION_TRACER 54/* 55 * If the arch's mcount caller does not support all of ftrace's 56 * features, then it must call an indirect function that 57 * does. Or at least does enough to prevent any unwelcome side effects. 58 * 59 * Also define the function prototype that these architectures use 60 * to call the ftrace_ops_list_func(). 61 */ 62#if !ARCH_SUPPORTS_FTRACE_OPS 63# define FTRACE_FORCE_LIST_FUNC 1 64void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip); 65#else 66# define FTRACE_FORCE_LIST_FUNC 0 67void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, 68 struct ftrace_ops *op, struct ftrace_regs *fregs); 69#endif 70extern const struct ftrace_ops ftrace_nop_ops; 71extern const struct ftrace_ops ftrace_list_ops; 72struct ftrace_ops *ftrace_find_unique_ops(struct dyn_ftrace *rec); 73#endif /* CONFIG_FUNCTION_TRACER */ 74 75/* Main tracing buffer and events set up */ 76#ifdef CONFIG_TRACING 77void trace_init(void); 78void early_trace_init(void); 79#else 80static inline void trace_init(void) { } 81static inline void early_trace_init(void) { } 82#endif 83 84struct module; 85struct ftrace_hash; 86 87#if defined(CONFIG_FUNCTION_TRACER) && defined(CONFIG_MODULES) && \ 88 defined(CONFIG_DYNAMIC_FTRACE) 89int 90ftrace_mod_address_lookup(unsigned long addr, unsigned long *size, 91 unsigned long *off, char **modname, char *sym); 92#else 93static inline int 94ftrace_mod_address_lookup(unsigned long addr, unsigned long *size, 95 unsigned long *off, char **modname, char *sym) 96{ 97 return 0; 98} 99#endif 100 101#if defined(CONFIG_FUNCTION_TRACER) && defined(CONFIG_DYNAMIC_FTRACE) 102int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value, 103 char *type, char *name, 104 char *module_name, int *exported); 105#else 106static inline int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value, 107 char *type, char *name, 108 char *module_name, int *exported) 109{ 110 return -1; 111} 112#endif 113 114#ifdef CONFIG_FUNCTION_TRACER 115 116extern int ftrace_enabled; 117 118#ifndef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS 119 120struct ftrace_regs { 121 struct pt_regs regs; 122}; 123#define arch_ftrace_get_regs(fregs) (&(fregs)->regs) 124 125/* 126 * ftrace_regs_set_instruction_pointer() is to be defined by the architecture 127 * if to allow setting of the instruction pointer from the ftrace_regs when 128 * HAVE_DYNAMIC_FTRACE_WITH_ARGS is set and it supports live kernel patching. 129 */ 130#define ftrace_regs_set_instruction_pointer(fregs, ip) do { } while (0) 131#endif /* CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS */ 132 133static __always_inline struct pt_regs *ftrace_get_regs(struct ftrace_regs *fregs) 134{ 135 if (!fregs) 136 return NULL; 137 138 return arch_ftrace_get_regs(fregs); 139} 140 141/* 142 * When true, the ftrace_regs_{get,set}_*() functions may be used on fregs. 143 * Note: this can be true even when ftrace_get_regs() cannot provide a pt_regs. 144 */ 145static __always_inline bool ftrace_regs_has_args(struct ftrace_regs *fregs) 146{ 147 if (IS_ENABLED(CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS)) 148 return true; 149 150 return ftrace_get_regs(fregs) != NULL; 151} 152 153#ifndef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS 154#define ftrace_regs_get_instruction_pointer(fregs) \ 155 instruction_pointer(ftrace_get_regs(fregs)) 156#define ftrace_regs_get_argument(fregs, n) \ 157 regs_get_kernel_argument(ftrace_get_regs(fregs), n) 158#define ftrace_regs_get_stack_pointer(fregs) \ 159 kernel_stack_pointer(ftrace_get_regs(fregs)) 160#define ftrace_regs_return_value(fregs) \ 161 regs_return_value(ftrace_get_regs(fregs)) 162#define ftrace_regs_set_return_value(fregs, ret) \ 163 regs_set_return_value(ftrace_get_regs(fregs), ret) 164#define ftrace_override_function_with_return(fregs) \ 165 override_function_with_return(ftrace_get_regs(fregs)) 166#define ftrace_regs_query_register_offset(name) \ 167 regs_query_register_offset(name) 168#endif 169 170typedef void (*ftrace_func_t)(unsigned long ip, unsigned long parent_ip, 171 struct ftrace_ops *op, struct ftrace_regs *fregs); 172 173ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops); 174 175/* 176 * FTRACE_OPS_FL_* bits denote the state of ftrace_ops struct and are 177 * set in the flags member. 178 * CONTROL, SAVE_REGS, SAVE_REGS_IF_SUPPORTED, RECURSION, STUB and 179 * IPMODIFY are a kind of attribute flags which can be set only before 180 * registering the ftrace_ops, and can not be modified while registered. 181 * Changing those attribute flags after registering ftrace_ops will 182 * cause unexpected results. 183 * 184 * ENABLED - set/unset when ftrace_ops is registered/unregistered 185 * DYNAMIC - set when ftrace_ops is registered to denote dynamically 186 * allocated ftrace_ops which need special care 187 * SAVE_REGS - The ftrace_ops wants regs saved at each function called 188 * and passed to the callback. If this flag is set, but the 189 * architecture does not support passing regs 190 * (CONFIG_DYNAMIC_FTRACE_WITH_REGS is not defined), then the 191 * ftrace_ops will fail to register, unless the next flag 192 * is set. 193 * SAVE_REGS_IF_SUPPORTED - This is the same as SAVE_REGS, but if the 194 * handler can handle an arch that does not save regs 195 * (the handler tests if regs == NULL), then it can set 196 * this flag instead. It will not fail registering the ftrace_ops 197 * but, the regs field will be NULL if the arch does not support 198 * passing regs to the handler. 199 * Note, if this flag is set, the SAVE_REGS flag will automatically 200 * get set upon registering the ftrace_ops, if the arch supports it. 201 * RECURSION - The ftrace_ops can set this to tell the ftrace infrastructure 202 * that the call back needs recursion protection. If it does 203 * not set this, then the ftrace infrastructure will assume 204 * that the callback can handle recursion on its own. 205 * STUB - The ftrace_ops is just a place holder. 206 * INITIALIZED - The ftrace_ops has already been initialized (first use time 207 * register_ftrace_function() is called, it will initialized the ops) 208 * DELETED - The ops are being deleted, do not let them be registered again. 209 * ADDING - The ops is in the process of being added. 210 * REMOVING - The ops is in the process of being removed. 211 * MODIFYING - The ops is in the process of changing its filter functions. 212 * ALLOC_TRAMP - A dynamic trampoline was allocated by the core code. 213 * The arch specific code sets this flag when it allocated a 214 * trampoline. This lets the arch know that it can update the 215 * trampoline in case the callback function changes. 216 * The ftrace_ops trampoline can be set by the ftrace users, and 217 * in such cases the arch must not modify it. Only the arch ftrace 218 * core code should set this flag. 219 * IPMODIFY - The ops can modify the IP register. This can only be set with 220 * SAVE_REGS. If another ops with this flag set is already registered 221 * for any of the functions that this ops will be registered for, then 222 * this ops will fail to register or set_filter_ip. 223 * PID - Is affected by set_ftrace_pid (allows filtering on those pids) 224 * RCU - Set when the ops can only be called when RCU is watching. 225 * TRACE_ARRAY - The ops->private points to a trace_array descriptor. 226 * PERMANENT - Set when the ops is permanent and should not be affected by 227 * ftrace_enabled. 228 * DIRECT - Used by the direct ftrace_ops helper for direct functions 229 * (internal ftrace only, should not be used by others) 230 */ 231enum { 232 FTRACE_OPS_FL_ENABLED = BIT(0), 233 FTRACE_OPS_FL_DYNAMIC = BIT(1), 234 FTRACE_OPS_FL_SAVE_REGS = BIT(2), 235 FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED = BIT(3), 236 FTRACE_OPS_FL_RECURSION = BIT(4), 237 FTRACE_OPS_FL_STUB = BIT(5), 238 FTRACE_OPS_FL_INITIALIZED = BIT(6), 239 FTRACE_OPS_FL_DELETED = BIT(7), 240 FTRACE_OPS_FL_ADDING = BIT(8), 241 FTRACE_OPS_FL_REMOVING = BIT(9), 242 FTRACE_OPS_FL_MODIFYING = BIT(10), 243 FTRACE_OPS_FL_ALLOC_TRAMP = BIT(11), 244 FTRACE_OPS_FL_IPMODIFY = BIT(12), 245 FTRACE_OPS_FL_PID = BIT(13), 246 FTRACE_OPS_FL_RCU = BIT(14), 247 FTRACE_OPS_FL_TRACE_ARRAY = BIT(15), 248 FTRACE_OPS_FL_PERMANENT = BIT(16), 249 FTRACE_OPS_FL_DIRECT = BIT(17), 250}; 251 252#ifndef CONFIG_DYNAMIC_FTRACE_WITH_ARGS 253#define FTRACE_OPS_FL_SAVE_ARGS FTRACE_OPS_FL_SAVE_REGS 254#else 255#define FTRACE_OPS_FL_SAVE_ARGS 0 256#endif 257 258/* 259 * FTRACE_OPS_CMD_* commands allow the ftrace core logic to request changes 260 * to a ftrace_ops. Note, the requests may fail. 261 * 262 * ENABLE_SHARE_IPMODIFY_SELF - enable a DIRECT ops to work on the same 263 * function as an ops with IPMODIFY. Called 264 * when the DIRECT ops is being registered. 265 * This is called with both direct_mutex and 266 * ftrace_lock are locked. 267 * 268 * ENABLE_SHARE_IPMODIFY_PEER - enable a DIRECT ops to work on the same 269 * function as an ops with IPMODIFY. Called 270 * when the other ops (the one with IPMODIFY) 271 * is being registered. 272 * This is called with direct_mutex locked. 273 * 274 * DISABLE_SHARE_IPMODIFY_PEER - disable a DIRECT ops to work on the same 275 * function as an ops with IPMODIFY. Called 276 * when the other ops (the one with IPMODIFY) 277 * is being unregistered. 278 * This is called with direct_mutex locked. 279 */ 280enum ftrace_ops_cmd { 281 FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_SELF, 282 FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_PEER, 283 FTRACE_OPS_CMD_DISABLE_SHARE_IPMODIFY_PEER, 284}; 285 286/* 287 * For most ftrace_ops_cmd, 288 * Returns: 289 * 0 - Success. 290 * Negative on failure. The return value is dependent on the 291 * callback. 292 */ 293typedef int (*ftrace_ops_func_t)(struct ftrace_ops *op, enum ftrace_ops_cmd cmd); 294 295#ifdef CONFIG_DYNAMIC_FTRACE 296/* The hash used to know what functions callbacks trace */ 297struct ftrace_ops_hash { 298 struct ftrace_hash __rcu *notrace_hash; 299 struct ftrace_hash __rcu *filter_hash; 300 struct mutex regex_lock; 301}; 302 303void ftrace_free_init_mem(void); 304void ftrace_free_mem(struct module *mod, void *start, void *end); 305#else 306static inline void ftrace_free_init_mem(void) 307{ 308 ftrace_boot_snapshot(); 309} 310static inline void ftrace_free_mem(struct module *mod, void *start, void *end) { } 311#endif 312 313/* 314 * Note, ftrace_ops can be referenced outside of RCU protection, unless 315 * the RCU flag is set. If ftrace_ops is allocated and not part of kernel 316 * core data, the unregistering of it will perform a scheduling on all CPUs 317 * to make sure that there are no more users. Depending on the load of the 318 * system that may take a bit of time. 319 * 320 * Any private data added must also take care not to be freed and if private 321 * data is added to a ftrace_ops that is in core code, the user of the 322 * ftrace_ops must perform a schedule_on_each_cpu() before freeing it. 323 */ 324struct ftrace_ops { 325 ftrace_func_t func; 326 struct ftrace_ops __rcu *next; 327 unsigned long flags; 328 void *private; 329 ftrace_func_t saved_func; 330#ifdef CONFIG_DYNAMIC_FTRACE 331 struct ftrace_ops_hash local_hash; 332 struct ftrace_ops_hash *func_hash; 333 struct ftrace_ops_hash old_hash; 334 unsigned long trampoline; 335 unsigned long trampoline_size; 336 struct list_head list; 337 ftrace_ops_func_t ops_func; 338#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS 339 unsigned long direct_call; 340#endif 341#endif 342}; 343 344extern struct ftrace_ops __rcu *ftrace_ops_list; 345extern struct ftrace_ops ftrace_list_end; 346 347/* 348 * Traverse the ftrace_ops_list, invoking all entries. The reason that we 349 * can use rcu_dereference_raw_check() is that elements removed from this list 350 * are simply leaked, so there is no need to interact with a grace-period 351 * mechanism. The rcu_dereference_raw_check() calls are needed to handle 352 * concurrent insertions into the ftrace_ops_list. 353 * 354 * Silly Alpha and silly pointer-speculation compiler optimizations! 355 */ 356#define do_for_each_ftrace_op(op, list) \ 357 op = rcu_dereference_raw_check(list); \ 358 do 359 360/* 361 * Optimized for just a single item in the list (as that is the normal case). 362 */ 363#define while_for_each_ftrace_op(op) \ 364 while (likely(op = rcu_dereference_raw_check((op)->next)) && \ 365 unlikely((op) != &ftrace_list_end)) 366 367/* 368 * Type of the current tracing. 369 */ 370enum ftrace_tracing_type_t { 371 FTRACE_TYPE_ENTER = 0, /* Hook the call of the function */ 372 FTRACE_TYPE_RETURN, /* Hook the return of the function */ 373}; 374 375/* Current tracing type, default is FTRACE_TYPE_ENTER */ 376extern enum ftrace_tracing_type_t ftrace_tracing_type; 377 378/* 379 * The ftrace_ops must be a static and should also 380 * be read_mostly. These functions do modify read_mostly variables 381 * so use them sparely. Never free an ftrace_op or modify the 382 * next pointer after it has been registered. Even after unregistering 383 * it, the next pointer may still be used internally. 384 */ 385int register_ftrace_function(struct ftrace_ops *ops); 386int unregister_ftrace_function(struct ftrace_ops *ops); 387 388extern void ftrace_stub(unsigned long a0, unsigned long a1, 389 struct ftrace_ops *op, struct ftrace_regs *fregs); 390 391 392int ftrace_lookup_symbols(const char **sorted_syms, size_t cnt, unsigned long *addrs); 393#else /* !CONFIG_FUNCTION_TRACER */ 394/* 395 * (un)register_ftrace_function must be a macro since the ops parameter 396 * must not be evaluated. 397 */ 398#define register_ftrace_function(ops) ({ 0; }) 399#define unregister_ftrace_function(ops) ({ 0; }) 400static inline void ftrace_kill(void) { } 401static inline void ftrace_free_init_mem(void) { } 402static inline void ftrace_free_mem(struct module *mod, void *start, void *end) { } 403static inline int ftrace_lookup_symbols(const char **sorted_syms, size_t cnt, unsigned long *addrs) 404{ 405 return -EOPNOTSUPP; 406} 407#endif /* CONFIG_FUNCTION_TRACER */ 408 409struct ftrace_func_entry { 410 struct hlist_node hlist; 411 unsigned long ip; 412 unsigned long direct; /* for direct lookup only */ 413}; 414 415#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS 416unsigned long ftrace_find_rec_direct(unsigned long ip); 417int register_ftrace_direct(struct ftrace_ops *ops, unsigned long addr); 418int unregister_ftrace_direct(struct ftrace_ops *ops, unsigned long addr, 419 bool free_filters); 420int modify_ftrace_direct(struct ftrace_ops *ops, unsigned long addr); 421int modify_ftrace_direct_nolock(struct ftrace_ops *ops, unsigned long addr); 422 423void ftrace_stub_direct_tramp(void); 424 425#else 426struct ftrace_ops; 427static inline unsigned long ftrace_find_rec_direct(unsigned long ip) 428{ 429 return 0; 430} 431static inline int register_ftrace_direct(struct ftrace_ops *ops, unsigned long addr) 432{ 433 return -ENODEV; 434} 435static inline int unregister_ftrace_direct(struct ftrace_ops *ops, unsigned long addr, 436 bool free_filters) 437{ 438 return -ENODEV; 439} 440static inline int modify_ftrace_direct(struct ftrace_ops *ops, unsigned long addr) 441{ 442 return -ENODEV; 443} 444static inline int modify_ftrace_direct_nolock(struct ftrace_ops *ops, unsigned long addr) 445{ 446 return -ENODEV; 447} 448 449/* 450 * This must be implemented by the architecture. 451 * It is the way the ftrace direct_ops helper, when called 452 * via ftrace (because there's other callbacks besides the 453 * direct call), can inform the architecture's trampoline that this 454 * routine has a direct caller, and what the caller is. 455 * 456 * For example, in x86, it returns the direct caller 457 * callback function via the regs->orig_ax parameter. 458 * Then in the ftrace trampoline, if this is set, it makes 459 * the return from the trampoline jump to the direct caller 460 * instead of going back to the function it just traced. 461 */ 462static inline void arch_ftrace_set_direct_caller(struct ftrace_regs *fregs, 463 unsigned long addr) { } 464#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */ 465 466#ifdef CONFIG_STACK_TRACER 467 468extern int stack_tracer_enabled; 469 470int stack_trace_sysctl(struct ctl_table *table, int write, void *buffer, 471 size_t *lenp, loff_t *ppos); 472 473/* DO NOT MODIFY THIS VARIABLE DIRECTLY! */ 474DECLARE_PER_CPU(int, disable_stack_tracer); 475 476/** 477 * stack_tracer_disable - temporarily disable the stack tracer 478 * 479 * There's a few locations (namely in RCU) where stack tracing 480 * cannot be executed. This function is used to disable stack 481 * tracing during those critical sections. 482 * 483 * This function must be called with preemption or interrupts 484 * disabled and stack_tracer_enable() must be called shortly after 485 * while preemption or interrupts are still disabled. 486 */ 487static inline void stack_tracer_disable(void) 488{ 489 /* Preemption or interrupts must be disabled */ 490 if (IS_ENABLED(CONFIG_DEBUG_PREEMPT)) 491 WARN_ON_ONCE(!preempt_count() || !irqs_disabled()); 492 this_cpu_inc(disable_stack_tracer); 493} 494 495/** 496 * stack_tracer_enable - re-enable the stack tracer 497 * 498 * After stack_tracer_disable() is called, stack_tracer_enable() 499 * must be called shortly afterward. 500 */ 501static inline void stack_tracer_enable(void) 502{ 503 if (IS_ENABLED(CONFIG_DEBUG_PREEMPT)) 504 WARN_ON_ONCE(!preempt_count() || !irqs_disabled()); 505 this_cpu_dec(disable_stack_tracer); 506} 507#else 508static inline void stack_tracer_disable(void) { } 509static inline void stack_tracer_enable(void) { } 510#endif 511 512#ifdef CONFIG_DYNAMIC_FTRACE 513 514void ftrace_arch_code_modify_prepare(void); 515void ftrace_arch_code_modify_post_process(void); 516 517enum ftrace_bug_type { 518 FTRACE_BUG_UNKNOWN, 519 FTRACE_BUG_INIT, 520 FTRACE_BUG_NOP, 521 FTRACE_BUG_CALL, 522 FTRACE_BUG_UPDATE, 523}; 524extern enum ftrace_bug_type ftrace_bug_type; 525 526/* 527 * Archs can set this to point to a variable that holds the value that was 528 * expected at the call site before calling ftrace_bug(). 529 */ 530extern const void *ftrace_expected; 531 532void ftrace_bug(int err, struct dyn_ftrace *rec); 533 534struct seq_file; 535 536extern int ftrace_text_reserved(const void *start, const void *end); 537 538struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr); 539 540bool is_ftrace_trampoline(unsigned long addr); 541 542/* 543 * The dyn_ftrace record's flags field is split into two parts. 544 * the first part which is '0-FTRACE_REF_MAX' is a counter of 545 * the number of callbacks that have registered the function that 546 * the dyn_ftrace descriptor represents. 547 * 548 * The second part is a mask: 549 * ENABLED - the function is being traced 550 * REGS - the record wants the function to save regs 551 * REGS_EN - the function is set up to save regs. 552 * IPMODIFY - the record allows for the IP address to be changed. 553 * DISABLED - the record is not ready to be touched yet 554 * DIRECT - there is a direct function to call 555 * CALL_OPS - the record can use callsite-specific ops 556 * CALL_OPS_EN - the function is set up to use callsite-specific ops 557 * TOUCHED - A callback was added since boot up 558 * MODIFIED - The function had IPMODIFY or DIRECT attached to it 559 * 560 * When a new ftrace_ops is registered and wants a function to save 561 * pt_regs, the rec->flags REGS is set. When the function has been 562 * set up to save regs, the REG_EN flag is set. Once a function 563 * starts saving regs it will do so until all ftrace_ops are removed 564 * from tracing that function. 565 */ 566enum { 567 FTRACE_FL_ENABLED = (1UL << 31), 568 FTRACE_FL_REGS = (1UL << 30), 569 FTRACE_FL_REGS_EN = (1UL << 29), 570 FTRACE_FL_TRAMP = (1UL << 28), 571 FTRACE_FL_TRAMP_EN = (1UL << 27), 572 FTRACE_FL_IPMODIFY = (1UL << 26), 573 FTRACE_FL_DISABLED = (1UL << 25), 574 FTRACE_FL_DIRECT = (1UL << 24), 575 FTRACE_FL_DIRECT_EN = (1UL << 23), 576 FTRACE_FL_CALL_OPS = (1UL << 22), 577 FTRACE_FL_CALL_OPS_EN = (1UL << 21), 578 FTRACE_FL_TOUCHED = (1UL << 20), 579 FTRACE_FL_MODIFIED = (1UL << 19), 580}; 581 582#define FTRACE_REF_MAX_SHIFT 19 583#define FTRACE_REF_MAX ((1UL << FTRACE_REF_MAX_SHIFT) - 1) 584 585#define ftrace_rec_count(rec) ((rec)->flags & FTRACE_REF_MAX) 586 587struct dyn_ftrace { 588 unsigned long ip; /* address of mcount call-site */ 589 unsigned long flags; 590 struct dyn_arch_ftrace arch; 591}; 592 593int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip, 594 int remove, int reset); 595int ftrace_set_filter_ips(struct ftrace_ops *ops, unsigned long *ips, 596 unsigned int cnt, int remove, int reset); 597int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf, 598 int len, int reset); 599int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf, 600 int len, int reset); 601void ftrace_set_global_filter(unsigned char *buf, int len, int reset); 602void ftrace_set_global_notrace(unsigned char *buf, int len, int reset); 603void ftrace_free_filter(struct ftrace_ops *ops); 604void ftrace_ops_set_global_filter(struct ftrace_ops *ops); 605 606enum { 607 FTRACE_UPDATE_CALLS = (1 << 0), 608 FTRACE_DISABLE_CALLS = (1 << 1), 609 FTRACE_UPDATE_TRACE_FUNC = (1 << 2), 610 FTRACE_START_FUNC_RET = (1 << 3), 611 FTRACE_STOP_FUNC_RET = (1 << 4), 612 FTRACE_MAY_SLEEP = (1 << 5), 613}; 614 615/* 616 * The FTRACE_UPDATE_* enum is used to pass information back 617 * from the ftrace_update_record() and ftrace_test_record() 618 * functions. These are called by the code update routines 619 * to find out what is to be done for a given function. 620 * 621 * IGNORE - The function is already what we want it to be 622 * MAKE_CALL - Start tracing the function 623 * MODIFY_CALL - Stop saving regs for the function 624 * MAKE_NOP - Stop tracing the function 625 */ 626enum { 627 FTRACE_UPDATE_IGNORE, 628 FTRACE_UPDATE_MAKE_CALL, 629 FTRACE_UPDATE_MODIFY_CALL, 630 FTRACE_UPDATE_MAKE_NOP, 631}; 632 633enum { 634 FTRACE_ITER_FILTER = (1 << 0), 635 FTRACE_ITER_NOTRACE = (1 << 1), 636 FTRACE_ITER_PRINTALL = (1 << 2), 637 FTRACE_ITER_DO_PROBES = (1 << 3), 638 FTRACE_ITER_PROBE = (1 << 4), 639 FTRACE_ITER_MOD = (1 << 5), 640 FTRACE_ITER_ENABLED = (1 << 6), 641 FTRACE_ITER_TOUCHED = (1 << 7), 642 FTRACE_ITER_ADDRS = (1 << 8), 643}; 644 645void arch_ftrace_update_code(int command); 646void arch_ftrace_update_trampoline(struct ftrace_ops *ops); 647void *arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec); 648void arch_ftrace_trampoline_free(struct ftrace_ops *ops); 649 650struct ftrace_rec_iter; 651 652struct ftrace_rec_iter *ftrace_rec_iter_start(void); 653struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter); 654struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter); 655 656#define for_ftrace_rec_iter(iter) \ 657 for (iter = ftrace_rec_iter_start(); \ 658 iter; \ 659 iter = ftrace_rec_iter_next(iter)) 660 661 662int ftrace_update_record(struct dyn_ftrace *rec, bool enable); 663int ftrace_test_record(struct dyn_ftrace *rec, bool enable); 664void ftrace_run_stop_machine(int command); 665unsigned long ftrace_location(unsigned long ip); 666unsigned long ftrace_location_range(unsigned long start, unsigned long end); 667unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec); 668unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec); 669 670extern ftrace_func_t ftrace_trace_function; 671 672int ftrace_regex_open(struct ftrace_ops *ops, int flag, 673 struct inode *inode, struct file *file); 674ssize_t ftrace_filter_write(struct file *file, const char __user *ubuf, 675 size_t cnt, loff_t *ppos); 676ssize_t ftrace_notrace_write(struct file *file, const char __user *ubuf, 677 size_t cnt, loff_t *ppos); 678int ftrace_regex_release(struct inode *inode, struct file *file); 679 680void __init 681ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable); 682 683/* defined in arch */ 684extern int ftrace_dyn_arch_init(void); 685extern void ftrace_replace_code(int enable); 686extern int ftrace_update_ftrace_func(ftrace_func_t func); 687extern void ftrace_caller(void); 688extern void ftrace_regs_caller(void); 689extern void ftrace_call(void); 690extern void ftrace_regs_call(void); 691extern void mcount_call(void); 692 693void ftrace_modify_all_code(int command); 694 695#ifndef FTRACE_ADDR 696#define FTRACE_ADDR ((unsigned long)ftrace_caller) 697#endif 698 699#ifndef FTRACE_GRAPH_ADDR 700#define FTRACE_GRAPH_ADDR ((unsigned long)ftrace_graph_caller) 701#endif 702 703#ifndef FTRACE_REGS_ADDR 704#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS 705# define FTRACE_REGS_ADDR ((unsigned long)ftrace_regs_caller) 706#else 707# define FTRACE_REGS_ADDR FTRACE_ADDR 708#endif 709#endif 710 711/* 712 * If an arch would like functions that are only traced 713 * by the function graph tracer to jump directly to its own 714 * trampoline, then they can define FTRACE_GRAPH_TRAMP_ADDR 715 * to be that address to jump to. 716 */ 717#ifndef FTRACE_GRAPH_TRAMP_ADDR 718#define FTRACE_GRAPH_TRAMP_ADDR ((unsigned long) 0) 719#endif 720 721#ifdef CONFIG_FUNCTION_GRAPH_TRACER 722extern void ftrace_graph_caller(void); 723extern int ftrace_enable_ftrace_graph_caller(void); 724extern int ftrace_disable_ftrace_graph_caller(void); 725#else 726static inline int ftrace_enable_ftrace_graph_caller(void) { return 0; } 727static inline int ftrace_disable_ftrace_graph_caller(void) { return 0; } 728#endif 729 730/** 731 * ftrace_make_nop - convert code into nop 732 * @mod: module structure if called by module load initialization 733 * @rec: the call site record (e.g. mcount/fentry) 734 * @addr: the address that the call site should be calling 735 * 736 * This is a very sensitive operation and great care needs 737 * to be taken by the arch. The operation should carefully 738 * read the location, check to see if what is read is indeed 739 * what we expect it to be, and then on success of the compare, 740 * it should write to the location. 741 * 742 * The code segment at @rec->ip should be a caller to @addr 743 * 744 * Return must be: 745 * 0 on success 746 * -EFAULT on error reading the location 747 * -EINVAL on a failed compare of the contents 748 * -EPERM on error writing to the location 749 * Any other value will be considered a failure. 750 */ 751extern int ftrace_make_nop(struct module *mod, 752 struct dyn_ftrace *rec, unsigned long addr); 753 754/** 755 * ftrace_need_init_nop - return whether nop call sites should be initialized 756 * 757 * Normally the compiler's -mnop-mcount generates suitable nops, so we don't 758 * need to call ftrace_init_nop() if the code is built with that flag. 759 * Architectures where this is not always the case may define their own 760 * condition. 761 * 762 * Return must be: 763 * 0 if ftrace_init_nop() should be called 764 * Nonzero if ftrace_init_nop() should not be called 765 */ 766 767#ifndef ftrace_need_init_nop 768#define ftrace_need_init_nop() (!__is_defined(CC_USING_NOP_MCOUNT)) 769#endif 770 771/** 772 * ftrace_init_nop - initialize a nop call site 773 * @mod: module structure if called by module load initialization 774 * @rec: the call site record (e.g. mcount/fentry) 775 * 776 * This is a very sensitive operation and great care needs 777 * to be taken by the arch. The operation should carefully 778 * read the location, check to see if what is read is indeed 779 * what we expect it to be, and then on success of the compare, 780 * it should write to the location. 781 * 782 * The code segment at @rec->ip should contain the contents created by 783 * the compiler 784 * 785 * Return must be: 786 * 0 on success 787 * -EFAULT on error reading the location 788 * -EINVAL on a failed compare of the contents 789 * -EPERM on error writing to the location 790 * Any other value will be considered a failure. 791 */ 792#ifndef ftrace_init_nop 793static inline int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec) 794{ 795 return ftrace_make_nop(mod, rec, MCOUNT_ADDR); 796} 797#endif 798 799/** 800 * ftrace_make_call - convert a nop call site into a call to addr 801 * @rec: the call site record (e.g. mcount/fentry) 802 * @addr: the address that the call site should call 803 * 804 * This is a very sensitive operation and great care needs 805 * to be taken by the arch. The operation should carefully 806 * read the location, check to see if what is read is indeed 807 * what we expect it to be, and then on success of the compare, 808 * it should write to the location. 809 * 810 * The code segment at @rec->ip should be a nop 811 * 812 * Return must be: 813 * 0 on success 814 * -EFAULT on error reading the location 815 * -EINVAL on a failed compare of the contents 816 * -EPERM on error writing to the location 817 * Any other value will be considered a failure. 818 */ 819extern int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr); 820 821#if defined(CONFIG_DYNAMIC_FTRACE_WITH_REGS) || \ 822 defined(CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS) || \ 823 defined(CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS) 824/** 825 * ftrace_modify_call - convert from one addr to another (no nop) 826 * @rec: the call site record (e.g. mcount/fentry) 827 * @old_addr: the address expected to be currently called to 828 * @addr: the address to change to 829 * 830 * This is a very sensitive operation and great care needs 831 * to be taken by the arch. The operation should carefully 832 * read the location, check to see if what is read is indeed 833 * what we expect it to be, and then on success of the compare, 834 * it should write to the location. 835 * 836 * When using call ops, this is called when the associated ops change, even 837 * when (addr == old_addr). 838 * 839 * The code segment at @rec->ip should be a caller to @old_addr 840 * 841 * Return must be: 842 * 0 on success 843 * -EFAULT on error reading the location 844 * -EINVAL on a failed compare of the contents 845 * -EPERM on error writing to the location 846 * Any other value will be considered a failure. 847 */ 848extern int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr, 849 unsigned long addr); 850#else 851/* Should never be called */ 852static inline int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr, 853 unsigned long addr) 854{ 855 return -EINVAL; 856} 857#endif 858 859extern int skip_trace(unsigned long ip); 860extern void ftrace_module_init(struct module *mod); 861extern void ftrace_module_enable(struct module *mod); 862extern void ftrace_release_mod(struct module *mod); 863#else /* CONFIG_DYNAMIC_FTRACE */ 864static inline int skip_trace(unsigned long ip) { return 0; } 865static inline void ftrace_module_init(struct module *mod) { } 866static inline void ftrace_module_enable(struct module *mod) { } 867static inline void ftrace_release_mod(struct module *mod) { } 868static inline int ftrace_text_reserved(const void *start, const void *end) 869{ 870 return 0; 871} 872static inline unsigned long ftrace_location(unsigned long ip) 873{ 874 return 0; 875} 876 877/* 878 * Again users of functions that have ftrace_ops may not 879 * have them defined when ftrace is not enabled, but these 880 * functions may still be called. Use a macro instead of inline. 881 */ 882#define ftrace_regex_open(ops, flag, inod, file) ({ -ENODEV; }) 883#define ftrace_set_early_filter(ops, buf, enable) do { } while (0) 884#define ftrace_set_filter_ip(ops, ip, remove, reset) ({ -ENODEV; }) 885#define ftrace_set_filter_ips(ops, ips, cnt, remove, reset) ({ -ENODEV; }) 886#define ftrace_set_filter(ops, buf, len, reset) ({ -ENODEV; }) 887#define ftrace_set_notrace(ops, buf, len, reset) ({ -ENODEV; }) 888#define ftrace_free_filter(ops) do { } while (0) 889#define ftrace_ops_set_global_filter(ops) do { } while (0) 890 891static inline ssize_t ftrace_filter_write(struct file *file, const char __user *ubuf, 892 size_t cnt, loff_t *ppos) { return -ENODEV; } 893static inline ssize_t ftrace_notrace_write(struct file *file, const char __user *ubuf, 894 size_t cnt, loff_t *ppos) { return -ENODEV; } 895static inline int 896ftrace_regex_release(struct inode *inode, struct file *file) { return -ENODEV; } 897 898static inline bool is_ftrace_trampoline(unsigned long addr) 899{ 900 return false; 901} 902#endif /* CONFIG_DYNAMIC_FTRACE */ 903 904#ifdef CONFIG_FUNCTION_GRAPH_TRACER 905#ifndef ftrace_graph_func 906#define ftrace_graph_func ftrace_stub 907#define FTRACE_OPS_GRAPH_STUB FTRACE_OPS_FL_STUB 908#else 909#define FTRACE_OPS_GRAPH_STUB 0 910#endif 911#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ 912 913/* totally disable ftrace - can not re-enable after this */ 914void ftrace_kill(void); 915 916static inline void tracer_disable(void) 917{ 918#ifdef CONFIG_FUNCTION_TRACER 919 ftrace_enabled = 0; 920#endif 921} 922 923/* 924 * Ftrace disable/restore without lock. Some synchronization mechanism 925 * must be used to prevent ftrace_enabled to be changed between 926 * disable/restore. 927 */ 928static inline int __ftrace_enabled_save(void) 929{ 930#ifdef CONFIG_FUNCTION_TRACER 931 int saved_ftrace_enabled = ftrace_enabled; 932 ftrace_enabled = 0; 933 return saved_ftrace_enabled; 934#else 935 return 0; 936#endif 937} 938 939static inline void __ftrace_enabled_restore(int enabled) 940{ 941#ifdef CONFIG_FUNCTION_TRACER 942 ftrace_enabled = enabled; 943#endif 944} 945 946/* All archs should have this, but we define it for consistency */ 947#ifndef ftrace_return_address0 948# define ftrace_return_address0 __builtin_return_address(0) 949#endif 950 951/* Archs may use other ways for ADDR1 and beyond */ 952#ifndef ftrace_return_address 953# ifdef CONFIG_FRAME_POINTER 954# define ftrace_return_address(n) __builtin_return_address(n) 955# else 956# define ftrace_return_address(n) 0UL 957# endif 958#endif 959 960#define CALLER_ADDR0 ((unsigned long)ftrace_return_address0) 961#define CALLER_ADDR1 ((unsigned long)ftrace_return_address(1)) 962#define CALLER_ADDR2 ((unsigned long)ftrace_return_address(2)) 963#define CALLER_ADDR3 ((unsigned long)ftrace_return_address(3)) 964#define CALLER_ADDR4 ((unsigned long)ftrace_return_address(4)) 965#define CALLER_ADDR5 ((unsigned long)ftrace_return_address(5)) 966#define CALLER_ADDR6 ((unsigned long)ftrace_return_address(6)) 967 968static __always_inline unsigned long get_lock_parent_ip(void) 969{ 970 unsigned long addr = CALLER_ADDR0; 971 972 if (!in_lock_functions(addr)) 973 return addr; 974 addr = CALLER_ADDR1; 975 if (!in_lock_functions(addr)) 976 return addr; 977 return CALLER_ADDR2; 978} 979 980#ifdef CONFIG_TRACE_PREEMPT_TOGGLE 981 extern void trace_preempt_on(unsigned long a0, unsigned long a1); 982 extern void trace_preempt_off(unsigned long a0, unsigned long a1); 983#else 984/* 985 * Use defines instead of static inlines because some arches will make code out 986 * of the CALLER_ADDR, when we really want these to be a real nop. 987 */ 988# define trace_preempt_on(a0, a1) do { } while (0) 989# define trace_preempt_off(a0, a1) do { } while (0) 990#endif 991 992#ifdef CONFIG_FTRACE_MCOUNT_RECORD 993extern void ftrace_init(void); 994#ifdef CC_USING_PATCHABLE_FUNCTION_ENTRY 995#define FTRACE_CALLSITE_SECTION "__patchable_function_entries" 996#else 997#define FTRACE_CALLSITE_SECTION "__mcount_loc" 998#endif 999#else 1000static inline void ftrace_init(void) { } 1001#endif 1002 1003/* 1004 * Structure that defines an entry function trace. 1005 * It's already packed but the attribute "packed" is needed 1006 * to remove extra padding at the end. 1007 */ 1008struct ftrace_graph_ent { 1009 unsigned long func; /* Current function */ 1010 int depth; 1011} __packed; 1012 1013/* 1014 * Structure that defines a return function trace. 1015 * It's already packed but the attribute "packed" is needed 1016 * to remove extra padding at the end. 1017 */ 1018struct ftrace_graph_ret { 1019 unsigned long func; /* Current function */ 1020#ifdef CONFIG_FUNCTION_GRAPH_RETVAL 1021 unsigned long retval; 1022#endif 1023 int depth; 1024 /* Number of functions that overran the depth limit for current task */ 1025 unsigned int overrun; 1026 unsigned long long calltime; 1027 unsigned long long rettime; 1028} __packed; 1029 1030/* Type of the callback handlers for tracing function graph*/ 1031typedef void (*trace_func_graph_ret_t)(struct ftrace_graph_ret *); /* return */ 1032typedef int (*trace_func_graph_ent_t)(struct ftrace_graph_ent *); /* entry */ 1033 1034extern int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace); 1035 1036#ifdef CONFIG_FUNCTION_GRAPH_TRACER 1037 1038struct fgraph_ops { 1039 trace_func_graph_ent_t entryfunc; 1040 trace_func_graph_ret_t retfunc; 1041}; 1042 1043/* 1044 * Stack of return addresses for functions 1045 * of a thread. 1046 * Used in struct thread_info 1047 */ 1048struct ftrace_ret_stack { 1049 unsigned long ret; 1050 unsigned long func; 1051 unsigned long long calltime; 1052#ifdef CONFIG_FUNCTION_PROFILER 1053 unsigned long long subtime; 1054#endif 1055#ifdef HAVE_FUNCTION_GRAPH_FP_TEST 1056 unsigned long fp; 1057#endif 1058#ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR 1059 unsigned long *retp; 1060#endif 1061}; 1062 1063/* 1064 * Primary handler of a function return. 1065 * It relays on ftrace_return_to_handler. 1066 * Defined in entry_32/64.S 1067 */ 1068extern void return_to_handler(void); 1069 1070extern int 1071function_graph_enter(unsigned long ret, unsigned long func, 1072 unsigned long frame_pointer, unsigned long *retp); 1073 1074struct ftrace_ret_stack * 1075ftrace_graph_get_ret_stack(struct task_struct *task, int idx); 1076 1077unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx, 1078 unsigned long ret, unsigned long *retp); 1079 1080/* 1081 * Sometimes we don't want to trace a function with the function 1082 * graph tracer but we want them to keep traced by the usual function 1083 * tracer if the function graph tracer is not configured. 1084 */ 1085#define __notrace_funcgraph notrace 1086 1087#define FTRACE_RETFUNC_DEPTH 50 1088#define FTRACE_RETSTACK_ALLOC_SIZE 32 1089 1090extern int register_ftrace_graph(struct fgraph_ops *ops); 1091extern void unregister_ftrace_graph(struct fgraph_ops *ops); 1092 1093/** 1094 * ftrace_graph_is_dead - returns true if ftrace_graph_stop() was called 1095 * 1096 * ftrace_graph_stop() is called when a severe error is detected in 1097 * the function graph tracing. This function is called by the critical 1098 * paths of function graph to keep those paths from doing any more harm. 1099 */ 1100DECLARE_STATIC_KEY_FALSE(kill_ftrace_graph); 1101 1102static inline bool ftrace_graph_is_dead(void) 1103{ 1104 return static_branch_unlikely(&kill_ftrace_graph); 1105} 1106 1107extern void ftrace_graph_stop(void); 1108 1109/* The current handlers in use */ 1110extern trace_func_graph_ret_t ftrace_graph_return; 1111extern trace_func_graph_ent_t ftrace_graph_entry; 1112 1113extern void ftrace_graph_init_task(struct task_struct *t); 1114extern void ftrace_graph_exit_task(struct task_struct *t); 1115extern void ftrace_graph_init_idle_task(struct task_struct *t, int cpu); 1116 1117static inline void pause_graph_tracing(void) 1118{ 1119 atomic_inc(&current->tracing_graph_pause); 1120} 1121 1122static inline void unpause_graph_tracing(void) 1123{ 1124 atomic_dec(&current->tracing_graph_pause); 1125} 1126#else /* !CONFIG_FUNCTION_GRAPH_TRACER */ 1127 1128#define __notrace_funcgraph 1129 1130static inline void ftrace_graph_init_task(struct task_struct *t) { } 1131static inline void ftrace_graph_exit_task(struct task_struct *t) { } 1132static inline void ftrace_graph_init_idle_task(struct task_struct *t, int cpu) { } 1133 1134/* Define as macros as fgraph_ops may not be defined */ 1135#define register_ftrace_graph(ops) ({ -1; }) 1136#define unregister_ftrace_graph(ops) do { } while (0) 1137 1138static inline unsigned long 1139ftrace_graph_ret_addr(struct task_struct *task, int *idx, unsigned long ret, 1140 unsigned long *retp) 1141{ 1142 return ret; 1143} 1144 1145static inline void pause_graph_tracing(void) { } 1146static inline void unpause_graph_tracing(void) { } 1147#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ 1148 1149#ifdef CONFIG_TRACING 1150enum ftrace_dump_mode; 1151 1152#define MAX_TRACER_SIZE 100 1153extern char ftrace_dump_on_oops[]; 1154extern int ftrace_dump_on_oops_enabled(void); 1155extern int tracepoint_printk; 1156 1157extern void disable_trace_on_warning(void); 1158extern int __disable_trace_on_warning; 1159 1160int tracepoint_printk_sysctl(struct ctl_table *table, int write, 1161 void *buffer, size_t *lenp, loff_t *ppos); 1162 1163#else /* CONFIG_TRACING */ 1164static inline void disable_trace_on_warning(void) { } 1165#endif /* CONFIG_TRACING */ 1166 1167#ifdef CONFIG_FTRACE_SYSCALLS 1168 1169unsigned long arch_syscall_addr(int nr); 1170 1171#endif /* CONFIG_FTRACE_SYSCALLS */ 1172 1173#endif /* _LINUX_FTRACE_H */