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