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