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