xref: /linux/include/linux/ftrace.h (revision 87bfa22937000db29a9a09ec826ab7227eaf9e8c)
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_TRACER_SNAPSHOT
35 extern void ftrace_boot_snapshot(void);
36 #else
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
79 static inline void 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
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
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  */
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 
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 *
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 *
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 *
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  */
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
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 void add_ftrace_hash_entry(struct ftrace_hash *hash, struct ftrace_func_entry *entry);
419 struct ftrace_func_entry *ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip);
420 void ftrace_hash_remove(struct ftrace_hash *hash);
421 
422 /* The hash used to know what functions callbacks trace */
423 struct ftrace_ops_hash {
424 	struct ftrace_hash __rcu	*notrace_hash;
425 	struct ftrace_hash __rcu	*filter_hash;
426 	struct mutex			regex_lock;
427 };
428 
429 void ftrace_free_init_mem(void);
430 void ftrace_free_mem(struct module *mod, void *start, void *end);
431 #else
432 static inline void ftrace_free_init_mem(void)
433 {
434 	ftrace_boot_snapshot();
435 }
436 static inline void ftrace_free_mem(struct module *mod, void *start, void *end) { }
437 #endif
438 
439 /*
440  * Note, ftrace_ops can be referenced outside of RCU protection, unless
441  * the RCU flag is set. If ftrace_ops is allocated and not part of kernel
442  * core data, the unregistering of it will perform a scheduling on all CPUs
443  * to make sure that there are no more users. Depending on the load of the
444  * system that may take a bit of time.
445  *
446  * Any private data added must also take care not to be freed and if private
447  * data is added to a ftrace_ops that is in core code, the user of the
448  * ftrace_ops must perform a schedule_on_each_cpu() before freeing it.
449  */
450 struct ftrace_ops {
451 	ftrace_func_t			func;
452 	struct ftrace_ops __rcu		*next;
453 	unsigned long			flags;
454 	void				*private;
455 	ftrace_func_t			saved_func;
456 #ifdef CONFIG_DYNAMIC_FTRACE
457 	struct ftrace_ops_hash		local_hash;
458 	struct ftrace_ops_hash		*func_hash;
459 	struct ftrace_ops_hash		old_hash;
460 	unsigned long			trampoline;
461 	unsigned long			trampoline_size;
462 	struct list_head		list;
463 	struct list_head		subop_list;
464 	ftrace_ops_func_t		ops_func;
465 	struct ftrace_ops		*managed;
466 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
467 	unsigned long			direct_call;
468 #endif
469 #endif
470 };
471 
472 extern struct ftrace_ops __rcu *ftrace_ops_list;
473 extern struct ftrace_ops ftrace_list_end;
474 
475 /*
476  * Traverse the ftrace_ops_list, invoking all entries.  The reason that we
477  * can use rcu_dereference_raw_check() is that elements removed from this list
478  * are simply leaked, so there is no need to interact with a grace-period
479  * mechanism.  The rcu_dereference_raw_check() calls are needed to handle
480  * concurrent insertions into the ftrace_ops_list.
481  *
482  * Silly Alpha and silly pointer-speculation compiler optimizations!
483  */
484 #define do_for_each_ftrace_op(op, list)			\
485 	op = rcu_dereference_raw_check(list);			\
486 	do
487 
488 /*
489  * Optimized for just a single item in the list (as that is the normal case).
490  */
491 #define while_for_each_ftrace_op(op)				\
492 	while (likely(op = rcu_dereference_raw_check((op)->next)) &&	\
493 	       unlikely((op) != &ftrace_list_end))
494 
495 /*
496  * Type of the current tracing.
497  */
498 enum ftrace_tracing_type_t {
499 	FTRACE_TYPE_ENTER = 0, /* Hook the call of the function */
500 	FTRACE_TYPE_RETURN,	/* Hook the return of the function */
501 };
502 
503 /* Current tracing type, default is FTRACE_TYPE_ENTER */
504 extern enum ftrace_tracing_type_t ftrace_tracing_type;
505 
506 /*
507  * The ftrace_ops must be a static and should also
508  * be read_mostly.  These functions do modify read_mostly variables
509  * so use them sparely. Never free an ftrace_op or modify the
510  * next pointer after it has been registered. Even after unregistering
511  * it, the next pointer may still be used internally.
512  */
513 int register_ftrace_function(struct ftrace_ops *ops);
514 int unregister_ftrace_function(struct ftrace_ops *ops);
515 
516 extern void ftrace_stub(unsigned long a0, unsigned long a1,
517 			struct ftrace_ops *op, struct ftrace_regs *fregs);
518 
519 
520 int ftrace_lookup_symbols(const char **sorted_syms, size_t cnt, unsigned long *addrs);
521 #else /* !CONFIG_FUNCTION_TRACER */
522 /*
523  * (un)register_ftrace_function must be a macro since the ops parameter
524  * must not be evaluated.
525  */
526 #define register_ftrace_function(ops) ({ 0; })
527 #define unregister_ftrace_function(ops) ({ 0; })
528 static inline void ftrace_kill(void) { }
529 static inline void ftrace_free_init_mem(void) { }
530 static inline void ftrace_free_mem(struct module *mod, void *start, void *end) { }
531 static inline int ftrace_lookup_symbols(const char **sorted_syms, size_t cnt, unsigned long *addrs)
532 {
533 	return -EOPNOTSUPP;
534 }
535 #endif /* CONFIG_FUNCTION_TRACER */
536 
537 struct ftrace_func_entry {
538 	struct hlist_node hlist;
539 	unsigned long ip;
540 	unsigned long direct; /* for direct lookup only */
541 };
542 
543 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
544 unsigned long ftrace_find_rec_direct(unsigned long ip);
545 int register_ftrace_direct(struct ftrace_ops *ops, unsigned long addr);
546 int unregister_ftrace_direct(struct ftrace_ops *ops, unsigned long addr,
547 			     bool free_filters);
548 int modify_ftrace_direct(struct ftrace_ops *ops, unsigned long addr);
549 int modify_ftrace_direct_nolock(struct ftrace_ops *ops, unsigned long addr);
550 
551 int update_ftrace_direct_add(struct ftrace_ops *ops, struct ftrace_hash *hash);
552 int update_ftrace_direct_del(struct ftrace_ops *ops, struct ftrace_hash *hash);
553 int update_ftrace_direct_mod(struct ftrace_ops *ops, struct ftrace_hash *hash, bool do_direct_lock);
554 
555 void ftrace_stub_direct_tramp(void);
556 
557 unsigned long ftrace_hash_count(struct ftrace_hash *hash);
558 
559 #else
560 struct ftrace_ops;
561 static inline unsigned long ftrace_find_rec_direct(unsigned long ip)
562 {
563 	return 0;
564 }
565 static inline int register_ftrace_direct(struct ftrace_ops *ops, unsigned long addr)
566 {
567 	return -ENODEV;
568 }
569 static inline int unregister_ftrace_direct(struct ftrace_ops *ops, unsigned long addr,
570 					   bool free_filters)
571 {
572 	return -ENODEV;
573 }
574 static inline int modify_ftrace_direct(struct ftrace_ops *ops, unsigned long addr)
575 {
576 	return -ENODEV;
577 }
578 static inline int modify_ftrace_direct_nolock(struct ftrace_ops *ops, unsigned long addr)
579 {
580 	return -ENODEV;
581 }
582 
583 static inline int update_ftrace_direct_add(struct ftrace_ops *ops, struct ftrace_hash *hash)
584 {
585 	return -ENODEV;
586 }
587 
588 static inline int update_ftrace_direct_del(struct ftrace_ops *ops, struct ftrace_hash *hash)
589 {
590 	return -ENODEV;
591 }
592 
593 static inline int update_ftrace_direct_mod(struct ftrace_ops *ops, struct ftrace_hash *hash, bool do_direct_lock)
594 {
595 	return -ENODEV;
596 }
597 
598 static inline unsigned long ftrace_hash_count(struct ftrace_hash *hash)
599 {
600 	return 0;
601 }
602 
603 /*
604  * This must be implemented by the architecture.
605  * It is the way the ftrace direct_ops helper, when called
606  * via ftrace (because there's other callbacks besides the
607  * direct call), can inform the architecture's trampoline that this
608  * routine has a direct caller, and what the caller is.
609  *
610  * For example, in x86, it returns the direct caller
611  * callback function via the regs->orig_ax parameter.
612  * Then in the ftrace trampoline, if this is set, it makes
613  * the return from the trampoline jump to the direct caller
614  * instead of going back to the function it just traced.
615  */
616 static inline void arch_ftrace_set_direct_caller(struct ftrace_regs *fregs,
617 						 unsigned long addr) { }
618 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
619 
620 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_JMP
621 static inline bool ftrace_is_jmp(unsigned long addr)
622 {
623 	return addr & 1;
624 }
625 
626 static inline unsigned long ftrace_jmp_set(unsigned long addr)
627 {
628 	return addr | 1UL;
629 }
630 
631 static inline unsigned long ftrace_jmp_get(unsigned long addr)
632 {
633 	return addr & ~1UL;
634 }
635 #else
636 static inline bool ftrace_is_jmp(unsigned long addr)
637 {
638 	return false;
639 }
640 
641 static inline unsigned long ftrace_jmp_set(unsigned long addr)
642 {
643 	return addr;
644 }
645 
646 static inline unsigned long ftrace_jmp_get(unsigned long addr)
647 {
648 	return addr;
649 }
650 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_JMP */
651 
652 #ifdef CONFIG_STACK_TRACER
653 
654 int stack_trace_sysctl(const struct ctl_table *table, int write, void *buffer,
655 		       size_t *lenp, loff_t *ppos);
656 
657 /* DO NOT MODIFY THIS VARIABLE DIRECTLY! */
658 DECLARE_PER_CPU(int, disable_stack_tracer);
659 
660 /**
661  * stack_tracer_disable - temporarily disable the stack tracer
662  *
663  * There's a few locations (namely in RCU) where stack tracing
664  * cannot be executed. This function is used to disable stack
665  * tracing during those critical sections.
666  *
667  * This function must be called with preemption or interrupts
668  * disabled and stack_tracer_enable() must be called shortly after
669  * while preemption or interrupts are still disabled.
670  */
671 static inline void stack_tracer_disable(void)
672 {
673 	/* Preemption or interrupts must be disabled */
674 	if (IS_ENABLED(CONFIG_DEBUG_PREEMPT))
675 		WARN_ON_ONCE(!preempt_count() || !irqs_disabled());
676 	this_cpu_inc(disable_stack_tracer);
677 }
678 
679 /**
680  * stack_tracer_enable - re-enable the stack tracer
681  *
682  * After stack_tracer_disable() is called, stack_tracer_enable()
683  * must be called shortly afterward.
684  */
685 static inline void stack_tracer_enable(void)
686 {
687 	if (IS_ENABLED(CONFIG_DEBUG_PREEMPT))
688 		WARN_ON_ONCE(!preempt_count() || !irqs_disabled());
689 	this_cpu_dec(disable_stack_tracer);
690 }
691 #else
692 static inline void stack_tracer_disable(void) { }
693 static inline void stack_tracer_enable(void) { }
694 #endif
695 
696 enum {
697 	FTRACE_UPDATE_CALLS		= (1 << 0),
698 	FTRACE_DISABLE_CALLS		= (1 << 1),
699 	FTRACE_UPDATE_TRACE_FUNC	= (1 << 2),
700 	FTRACE_START_FUNC_RET		= (1 << 3),
701 	FTRACE_STOP_FUNC_RET		= (1 << 4),
702 	FTRACE_MAY_SLEEP		= (1 << 5),
703 };
704 
705 /* Arches can override ftrace_get_symaddr() to convert fentry_ip to symaddr. */
706 #ifndef ftrace_get_symaddr
707 /**
708  * ftrace_get_symaddr - return the symbol address from fentry_ip
709  * @fentry_ip: the address of ftrace location
710  *
711  * Get the symbol address from @fentry_ip (fast path). If there is no fast
712  * search path, this returns 0.
713  * User may need to use kallsyms API to find the symbol address.
714  */
715 #define ftrace_get_symaddr(fentry_ip) (0)
716 #endif
717 
718 void ftrace_sync_ipi(void *data);
719 
720 #ifdef CONFIG_DYNAMIC_FTRACE
721 
722 void ftrace_arch_code_modify_prepare(void);
723 void ftrace_arch_code_modify_post_process(void);
724 
725 enum ftrace_bug_type {
726 	FTRACE_BUG_UNKNOWN,
727 	FTRACE_BUG_INIT,
728 	FTRACE_BUG_NOP,
729 	FTRACE_BUG_CALL,
730 	FTRACE_BUG_UPDATE,
731 };
732 extern enum ftrace_bug_type ftrace_bug_type;
733 
734 /*
735  * Archs can set this to point to a variable that holds the value that was
736  * expected at the call site before calling ftrace_bug().
737  */
738 extern const void *ftrace_expected;
739 
740 void ftrace_bug(int err, struct dyn_ftrace *rec);
741 
742 struct seq_file;
743 
744 extern int ftrace_text_reserved(const void *start, const void *end);
745 
746 struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr);
747 
748 bool is_ftrace_trampoline(unsigned long addr);
749 
750 /*
751  * The dyn_ftrace record's flags field is split into two parts.
752  * the first part which is '0-FTRACE_REF_MAX' is a counter of
753  * the number of callbacks that have registered the function that
754  * the dyn_ftrace descriptor represents.
755  *
756  * The second part is a mask:
757  *  ENABLED - the function is being traced
758  *  REGS    - the record wants the function to save regs
759  *  REGS_EN - the function is set up to save regs.
760  *  IPMODIFY - the record allows for the IP address to be changed.
761  *  DISABLED - the record is not ready to be touched yet
762  *  DIRECT   - there is a direct function to call
763  *  CALL_OPS - the record can use callsite-specific ops
764  *  CALL_OPS_EN - the function is set up to use callsite-specific ops
765  *  TOUCHED  - A callback was added since boot up
766  *  MODIFIED - The function had IPMODIFY or DIRECT attached to it
767  *
768  * When a new ftrace_ops is registered and wants a function to save
769  * pt_regs, the rec->flags REGS is set. When the function has been
770  * set up to save regs, the REG_EN flag is set. Once a function
771  * starts saving regs it will do so until all ftrace_ops are removed
772  * from tracing that function.
773  */
774 enum {
775 	FTRACE_FL_ENABLED	= (1UL << 31),
776 	FTRACE_FL_REGS		= (1UL << 30),
777 	FTRACE_FL_REGS_EN	= (1UL << 29),
778 	FTRACE_FL_TRAMP		= (1UL << 28),
779 	FTRACE_FL_TRAMP_EN	= (1UL << 27),
780 	FTRACE_FL_IPMODIFY	= (1UL << 26),
781 	FTRACE_FL_DISABLED	= (1UL << 25),
782 	FTRACE_FL_DIRECT	= (1UL << 24),
783 	FTRACE_FL_DIRECT_EN	= (1UL << 23),
784 	FTRACE_FL_CALL_OPS	= (1UL << 22),
785 	FTRACE_FL_CALL_OPS_EN	= (1UL << 21),
786 	FTRACE_FL_TOUCHED	= (1UL << 20),
787 	FTRACE_FL_MODIFIED	= (1UL << 19),
788 };
789 
790 #define FTRACE_REF_MAX_SHIFT	19
791 #define FTRACE_REF_MAX		((1UL << FTRACE_REF_MAX_SHIFT) - 1)
792 
793 #define ftrace_rec_count(rec)	((rec)->flags & FTRACE_REF_MAX)
794 
795 struct dyn_ftrace {
796 	unsigned long		ip; /* address of mcount call-site */
797 	unsigned long		flags;
798 	struct dyn_arch_ftrace	arch;
799 };
800 
801 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
802 			 int remove, int reset);
803 int ftrace_set_filter_ips(struct ftrace_ops *ops, unsigned long *ips,
804 			  unsigned int cnt, int remove, int reset);
805 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
806 		       int len, int reset);
807 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
808 			int len, int reset);
809 void ftrace_set_global_filter(unsigned char *buf, int len, int reset);
810 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset);
811 void ftrace_free_filter(struct ftrace_ops *ops);
812 void ftrace_ops_set_global_filter(struct ftrace_ops *ops);
813 
814 /*
815  * The FTRACE_UPDATE_* enum is used to pass information back
816  * from the ftrace_update_record() and ftrace_test_record()
817  * functions. These are called by the code update routines
818  * to find out what is to be done for a given function.
819  *
820  *  IGNORE           - The function is already what we want it to be
821  *  MAKE_CALL        - Start tracing the function
822  *  MODIFY_CALL      - Stop saving regs for the function
823  *  MAKE_NOP         - Stop tracing the function
824  */
825 enum {
826 	FTRACE_UPDATE_IGNORE,
827 	FTRACE_UPDATE_MAKE_CALL,
828 	FTRACE_UPDATE_MODIFY_CALL,
829 	FTRACE_UPDATE_MAKE_NOP,
830 };
831 
832 enum {
833 	FTRACE_ITER_FILTER	= (1 << 0),
834 	FTRACE_ITER_NOTRACE	= (1 << 1),
835 	FTRACE_ITER_PRINTALL	= (1 << 2),
836 	FTRACE_ITER_DO_PROBES	= (1 << 3),
837 	FTRACE_ITER_PROBE	= (1 << 4),
838 	FTRACE_ITER_MOD		= (1 << 5),
839 	FTRACE_ITER_ENABLED	= (1 << 6),
840 	FTRACE_ITER_TOUCHED	= (1 << 7),
841 	FTRACE_ITER_ADDRS	= (1 << 8),
842 };
843 
844 void arch_ftrace_update_code(int command);
845 void arch_ftrace_update_trampoline(struct ftrace_ops *ops);
846 void *arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec);
847 void arch_ftrace_trampoline_free(struct ftrace_ops *ops);
848 
849 struct ftrace_rec_iter;
850 
851 struct ftrace_rec_iter *ftrace_rec_iter_start(void);
852 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter);
853 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter);
854 
855 #define for_ftrace_rec_iter(iter)		\
856 	for (iter = ftrace_rec_iter_start();	\
857 	     iter;				\
858 	     iter = ftrace_rec_iter_next(iter))
859 
860 
861 int ftrace_update_record(struct dyn_ftrace *rec, bool enable);
862 int ftrace_test_record(struct dyn_ftrace *rec, bool enable);
863 void ftrace_run_stop_machine(int command);
864 unsigned long ftrace_location(unsigned long ip);
865 unsigned long ftrace_location_range(unsigned long start, unsigned long end);
866 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec);
867 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec);
868 
869 extern ftrace_func_t ftrace_trace_function;
870 struct trace_array;
871 
872 int ftrace_regex_open(struct trace_array *tr, struct ftrace_ops *ops, int flag,
873 		  struct inode *inode, struct file *file);
874 ssize_t ftrace_filter_write(struct file *file, const char __user *ubuf,
875 			    size_t cnt, loff_t *ppos);
876 ssize_t ftrace_notrace_write(struct file *file, const char __user *ubuf,
877 			     size_t cnt, loff_t *ppos);
878 int ftrace_regex_release(struct inode *inode, struct file *file);
879 
880 void __init
881 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable);
882 
883 /* defined in arch */
884 extern int ftrace_dyn_arch_init(void);
885 extern void ftrace_replace_code(int enable);
886 extern int ftrace_update_ftrace_func(ftrace_func_t func);
887 extern void ftrace_caller(void);
888 extern void ftrace_regs_caller(void);
889 extern void ftrace_call(void);
890 extern void ftrace_regs_call(void);
891 extern void mcount_call(void);
892 
893 void ftrace_modify_all_code(int command);
894 
895 #ifndef FTRACE_ADDR
896 #define FTRACE_ADDR ((unsigned long)ftrace_caller)
897 #endif
898 
899 #ifndef FTRACE_GRAPH_ADDR
900 #define FTRACE_GRAPH_ADDR ((unsigned long)ftrace_graph_caller)
901 #endif
902 
903 #ifndef FTRACE_REGS_ADDR
904 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
905 # define FTRACE_REGS_ADDR ((unsigned long)ftrace_regs_caller)
906 #else
907 # define FTRACE_REGS_ADDR FTRACE_ADDR
908 #endif
909 #endif
910 
911 /*
912  * If an arch would like functions that are only traced
913  * by the function graph tracer to jump directly to its own
914  * trampoline, then they can define FTRACE_GRAPH_TRAMP_ADDR
915  * to be that address to jump to.
916  */
917 #ifndef FTRACE_GRAPH_TRAMP_ADDR
918 #define FTRACE_GRAPH_TRAMP_ADDR ((unsigned long) 0)
919 #endif
920 
921 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
922 extern void ftrace_graph_caller(void);
923 extern int ftrace_enable_ftrace_graph_caller(void);
924 extern int ftrace_disable_ftrace_graph_caller(void);
925 #else
926 static inline int ftrace_enable_ftrace_graph_caller(void) { return 0; }
927 static inline int ftrace_disable_ftrace_graph_caller(void) { return 0; }
928 #endif
929 
930 /**
931  * ftrace_make_nop - convert code into nop
932  * @mod: module structure if called by module load initialization
933  * @rec: the call site record (e.g. mcount/fentry)
934  * @addr: the address that the call site should be calling
935  *
936  * This is a very sensitive operation and great care needs
937  * to be taken by the arch.  The operation should carefully
938  * read the location, check to see if what is read is indeed
939  * what we expect it to be, and then on success of the compare,
940  * it should write to the location.
941  *
942  * The code segment at @rec->ip should be a caller to @addr
943  *
944  * Return must be:
945  *  0 on success
946  *  -EFAULT on error reading the location
947  *  -EINVAL on a failed compare of the contents
948  *  -EPERM  on error writing to the location
949  * Any other value will be considered a failure.
950  */
951 extern int ftrace_make_nop(struct module *mod,
952 			   struct dyn_ftrace *rec, unsigned long addr);
953 
954 /**
955  * ftrace_need_init_nop - return whether nop call sites should be initialized
956  *
957  * Normally the compiler's -mnop-mcount generates suitable nops, so we don't
958  * need to call ftrace_init_nop() if the code is built with that flag.
959  * Architectures where this is not always the case may define their own
960  * condition.
961  *
962  * Return must be:
963  *  0	    if ftrace_init_nop() should be called
964  *  Nonzero if ftrace_init_nop() should not be called
965  */
966 
967 #ifndef ftrace_need_init_nop
968 #define ftrace_need_init_nop() (!__is_defined(CC_USING_NOP_MCOUNT))
969 #endif
970 
971 /**
972  * ftrace_init_nop - initialize a nop call site
973  * @mod: module structure if called by module load initialization
974  * @rec: the call site record (e.g. mcount/fentry)
975  *
976  * This is a very sensitive operation and great care needs
977  * to be taken by the arch.  The operation should carefully
978  * read the location, check to see if what is read is indeed
979  * what we expect it to be, and then on success of the compare,
980  * it should write to the location.
981  *
982  * The code segment at @rec->ip should contain the contents created by
983  * the compiler
984  *
985  * Return must be:
986  *  0 on success
987  *  -EFAULT on error reading the location
988  *  -EINVAL on a failed compare of the contents
989  *  -EPERM  on error writing to the location
990  * Any other value will be considered a failure.
991  */
992 #ifndef ftrace_init_nop
993 static inline int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)
994 {
995 	return ftrace_make_nop(mod, rec, MCOUNT_ADDR);
996 }
997 #endif
998 
999 /**
1000  * ftrace_make_call - convert a nop call site into a call to addr
1001  * @rec: the call site record (e.g. mcount/fentry)
1002  * @addr: the address that the call site should call
1003  *
1004  * This is a very sensitive operation and great care needs
1005  * to be taken by the arch.  The operation should carefully
1006  * read the location, check to see if what is read is indeed
1007  * what we expect it to be, and then on success of the compare,
1008  * it should write to the location.
1009  *
1010  * The code segment at @rec->ip should be a nop
1011  *
1012  * Return must be:
1013  *  0 on success
1014  *  -EFAULT on error reading the location
1015  *  -EINVAL on a failed compare of the contents
1016  *  -EPERM  on error writing to the location
1017  * Any other value will be considered a failure.
1018  */
1019 extern int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr);
1020 
1021 #if defined(CONFIG_DYNAMIC_FTRACE_WITH_REGS) || \
1022 	defined(CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS) || \
1023 	defined(CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS)
1024 /**
1025  * ftrace_modify_call - convert from one addr to another (no nop)
1026  * @rec: the call site record (e.g. mcount/fentry)
1027  * @old_addr: the address expected to be currently called to
1028  * @addr: the address to change to
1029  *
1030  * This is a very sensitive operation and great care needs
1031  * to be taken by the arch.  The operation should carefully
1032  * read the location, check to see if what is read is indeed
1033  * what we expect it to be, and then on success of the compare,
1034  * it should write to the location.
1035  *
1036  * When using call ops, this is called when the associated ops change, even
1037  * when (addr == old_addr).
1038  *
1039  * The code segment at @rec->ip should be a caller to @old_addr
1040  *
1041  * Return must be:
1042  *  0 on success
1043  *  -EFAULT on error reading the location
1044  *  -EINVAL on a failed compare of the contents
1045  *  -EPERM  on error writing to the location
1046  * Any other value will be considered a failure.
1047  */
1048 extern int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
1049 			      unsigned long addr);
1050 #else
1051 /* Should never be called */
1052 static inline int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
1053 				     unsigned long addr)
1054 {
1055 	return -EINVAL;
1056 }
1057 #endif
1058 
1059 extern int skip_trace(unsigned long ip);
1060 extern void ftrace_module_init(struct module *mod);
1061 extern void ftrace_module_enable(struct module *mod);
1062 extern void ftrace_release_mod(struct module *mod);
1063 #else /* CONFIG_DYNAMIC_FTRACE */
1064 static inline int skip_trace(unsigned long ip) { return 0; }
1065 static inline void ftrace_module_init(struct module *mod) { }
1066 static inline void ftrace_module_enable(struct module *mod) { }
1067 static inline void ftrace_release_mod(struct module *mod) { }
1068 static inline int ftrace_text_reserved(const void *start, const void *end)
1069 {
1070 	return 0;
1071 }
1072 static inline unsigned long ftrace_location(unsigned long ip)
1073 {
1074 	return 0;
1075 }
1076 
1077 /*
1078  * Again users of functions that have ftrace_ops may not
1079  * have them defined when ftrace is not enabled, but these
1080  * functions may still be called. Use a macro instead of inline.
1081  */
1082 #define ftrace_regex_open(tr, ops, flag, inode, file) ({ -ENODEV; })
1083 #define ftrace_set_early_filter(ops, buf, enable) do { } while (0)
1084 #define ftrace_set_filter_ip(ops, ip, remove, reset) ({ -ENODEV; })
1085 #define ftrace_set_filter_ips(ops, ips, cnt, remove, reset) ({ -ENODEV; })
1086 #define ftrace_set_filter(ops, buf, len, reset) ({ -ENODEV; })
1087 #define ftrace_set_notrace(ops, buf, len, reset) ({ -ENODEV; })
1088 #define ftrace_free_filter(ops) do { } while (0)
1089 #define ftrace_ops_set_global_filter(ops) do { } while (0)
1090 
1091 static inline ssize_t ftrace_filter_write(struct file *file, const char __user *ubuf,
1092 			    size_t cnt, loff_t *ppos) { return -ENODEV; }
1093 static inline ssize_t ftrace_notrace_write(struct file *file, const char __user *ubuf,
1094 			     size_t cnt, loff_t *ppos) { return -ENODEV; }
1095 static inline int
1096 ftrace_regex_release(struct inode *inode, struct file *file) { return -ENODEV; }
1097 
1098 static inline bool is_ftrace_trampoline(unsigned long addr)
1099 {
1100 	return false;
1101 }
1102 #endif /* CONFIG_DYNAMIC_FTRACE */
1103 
1104 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1105 #ifndef ftrace_graph_func
1106 # define ftrace_graph_func ftrace_stub
1107 # define FTRACE_OPS_GRAPH_STUB FTRACE_OPS_FL_STUB
1108 /*
1109  * The function graph is called every time the function tracer is called.
1110  * It must always test the ops hash and cannot just directly call
1111  * the handler.
1112  */
1113 # define FGRAPH_NO_DIRECT	1
1114 #else
1115 # define FTRACE_OPS_GRAPH_STUB	0
1116 # define FGRAPH_NO_DIRECT	0
1117 #endif
1118 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1119 
1120 /* totally disable ftrace - can not re-enable after this */
1121 void ftrace_kill(void);
1122 
1123 static inline void tracer_disable(void)
1124 {
1125 #ifdef CONFIG_FUNCTION_TRACER
1126 	ftrace_enabled = 0;
1127 #endif
1128 }
1129 
1130 /*
1131  * Ftrace disable/restore without lock. Some synchronization mechanism
1132  * must be used to prevent ftrace_enabled to be changed between
1133  * disable/restore.
1134  */
1135 static inline int __ftrace_enabled_save(void)
1136 {
1137 #ifdef CONFIG_FUNCTION_TRACER
1138 	int saved_ftrace_enabled = ftrace_enabled;
1139 	ftrace_enabled = 0;
1140 	return saved_ftrace_enabled;
1141 #else
1142 	return 0;
1143 #endif
1144 }
1145 
1146 static inline void __ftrace_enabled_restore(int enabled)
1147 {
1148 #ifdef CONFIG_FUNCTION_TRACER
1149 	ftrace_enabled = enabled;
1150 #endif
1151 }
1152 
1153 /* All archs should have this, but we define it for consistency */
1154 #ifndef ftrace_return_address0
1155 # define ftrace_return_address0 __builtin_return_address(0)
1156 #endif
1157 
1158 /* Archs may use other ways for ADDR1 and beyond */
1159 #ifndef ftrace_return_address
1160 # ifdef CONFIG_FRAME_POINTER
1161 #  define ftrace_return_address(n) __builtin_return_address(n)
1162 # else
1163 #  define ftrace_return_address(n) 0UL
1164 # endif
1165 #endif
1166 
1167 #define CALLER_ADDR0 ((unsigned long)ftrace_return_address0)
1168 #define CALLER_ADDR1 ((unsigned long)ftrace_return_address(1))
1169 #define CALLER_ADDR2 ((unsigned long)ftrace_return_address(2))
1170 #define CALLER_ADDR3 ((unsigned long)ftrace_return_address(3))
1171 #define CALLER_ADDR4 ((unsigned long)ftrace_return_address(4))
1172 #define CALLER_ADDR5 ((unsigned long)ftrace_return_address(5))
1173 #define CALLER_ADDR6 ((unsigned long)ftrace_return_address(6))
1174 
1175 static __always_inline unsigned long get_lock_parent_ip(void)
1176 {
1177 	unsigned long addr = CALLER_ADDR0;
1178 
1179 	if (!in_lock_functions(addr))
1180 		return addr;
1181 	addr = CALLER_ADDR1;
1182 	if (!in_lock_functions(addr))
1183 		return addr;
1184 	return CALLER_ADDR2;
1185 }
1186 
1187 #ifdef CONFIG_TRACE_PREEMPT_TOGGLE
1188   extern void trace_preempt_on(unsigned long a0, unsigned long a1);
1189   extern void trace_preempt_off(unsigned long a0, unsigned long a1);
1190 #else
1191 /*
1192  * Use defines instead of static inlines because some arches will make code out
1193  * of the CALLER_ADDR, when we really want these to be a real nop.
1194  */
1195 # define trace_preempt_on(a0, a1) do { } while (0)
1196 # define trace_preempt_off(a0, a1) do { } while (0)
1197 #endif
1198 
1199 #ifdef CONFIG_DYNAMIC_FTRACE
1200 extern void ftrace_init(void);
1201 #ifdef CC_USING_PATCHABLE_FUNCTION_ENTRY
1202 #define FTRACE_CALLSITE_SECTION	"__patchable_function_entries"
1203 #else
1204 #define FTRACE_CALLSITE_SECTION	"__mcount_loc"
1205 #endif
1206 #else
1207 static inline void ftrace_init(void) { }
1208 #endif
1209 
1210 /*
1211  * Structure that defines an entry function trace.
1212  * It's already packed but the attribute "packed" is needed
1213  * to remove extra padding at the end.
1214  */
1215 struct ftrace_graph_ent {
1216 	unsigned long func; /* Current function */
1217 	long depth; /* signed to check for less than zero */
1218 } __packed;
1219 
1220 /*
1221  * Structure that defines an entry function trace with retaddr.
1222  */
1223 struct fgraph_retaddr_ent {
1224 	struct ftrace_graph_ent ent;
1225 	unsigned long retaddr;  /* Return address */
1226 } __packed;
1227 
1228 /*
1229  * Structure that defines a return function trace.
1230  * It's already packed but the attribute "packed" is needed
1231  * to remove extra padding at the end.
1232  */
1233 struct ftrace_graph_ret {
1234 	unsigned long func; /* Current function */
1235 #ifdef CONFIG_FUNCTION_GRAPH_RETVAL
1236 	unsigned long retval;
1237 #endif
1238 	int depth;
1239 	/* Number of functions that overran the depth limit for current task */
1240 	unsigned int overrun;
1241 } __packed;
1242 
1243 struct fgraph_ops;
1244 
1245 /* Type of the callback handlers for tracing function graph*/
1246 typedef void (*trace_func_graph_ret_t)(struct ftrace_graph_ret *,
1247 				       struct fgraph_ops *,
1248 				       struct ftrace_regs *); /* return */
1249 typedef int (*trace_func_graph_ent_t)(struct ftrace_graph_ent *,
1250 				      struct fgraph_ops *,
1251 				      struct ftrace_regs *); /* entry */
1252 
1253 extern int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace,
1254 				   struct fgraph_ops *gops,
1255 				   struct ftrace_regs *fregs);
1256 bool ftrace_pids_enabled(struct ftrace_ops *ops);
1257 
1258 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1259 
1260 struct fgraph_ops {
1261 	trace_func_graph_ent_t		entryfunc;
1262 	trace_func_graph_ret_t		retfunc;
1263 	struct ftrace_ops		ops; /* for the hash lists */
1264 	void				*private;
1265 	trace_func_graph_ent_t		saved_func;
1266 	int				idx;
1267 };
1268 
1269 void *fgraph_reserve_data(int idx, int size_bytes);
1270 void *fgraph_retrieve_data(int idx, int *size_bytes);
1271 void *fgraph_retrieve_parent_data(int idx, int *size_bytes, int depth);
1272 
1273 /*
1274  * Stack of return addresses for functions
1275  * of a thread.
1276  * Used in struct thread_info
1277  */
1278 struct ftrace_ret_stack {
1279 	unsigned long ret;
1280 	unsigned long func;
1281 #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
1282 	unsigned long fp;
1283 #endif
1284 	unsigned long *retp;
1285 };
1286 
1287 /*
1288  * Primary handler of a function return.
1289  * It relays on ftrace_return_to_handler.
1290  * Defined in entry_32/64.S
1291  */
1292 extern void return_to_handler(void);
1293 
1294 extern int
1295 function_graph_enter_regs(unsigned long ret, unsigned long func,
1296 			  unsigned long frame_pointer, unsigned long *retp,
1297 			  struct ftrace_regs *fregs);
1298 
1299 static inline int function_graph_enter(unsigned long ret, unsigned long func,
1300 				       unsigned long fp, unsigned long *retp)
1301 {
1302 	return function_graph_enter_regs(ret, func, fp, retp, NULL);
1303 }
1304 
1305 struct ftrace_ret_stack *
1306 ftrace_graph_get_ret_stack(struct task_struct *task, int skip);
1307 unsigned long ftrace_graph_top_ret_addr(struct task_struct *task);
1308 
1309 unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
1310 				    unsigned long ret, unsigned long *retp);
1311 unsigned long *fgraph_get_task_var(struct fgraph_ops *gops);
1312 
1313 /*
1314  * Sometimes we don't want to trace a function with the function
1315  * graph tracer but we want them to keep traced by the usual function
1316  * tracer if the function graph tracer is not configured.
1317  */
1318 #define __notrace_funcgraph		notrace
1319 
1320 #define FTRACE_RETFUNC_DEPTH 50
1321 #define FTRACE_RETSTACK_ALLOC_SIZE 32
1322 
1323 extern int register_ftrace_graph(struct fgraph_ops *ops);
1324 extern void unregister_ftrace_graph(struct fgraph_ops *ops);
1325 
1326 /**
1327  * ftrace_graph_is_dead - returns true if ftrace_graph_stop() was called
1328  *
1329  * ftrace_graph_stop() is called when a severe error is detected in
1330  * the function graph tracing. This function is called by the critical
1331  * paths of function graph to keep those paths from doing any more harm.
1332  */
1333 DECLARE_STATIC_KEY_FALSE(kill_ftrace_graph);
1334 
1335 static inline bool ftrace_graph_is_dead(void)
1336 {
1337 	return static_branch_unlikely(&kill_ftrace_graph);
1338 }
1339 
1340 extern void ftrace_graph_stop(void);
1341 
1342 /* The current handlers in use */
1343 extern trace_func_graph_ret_t ftrace_graph_return;
1344 extern trace_func_graph_ent_t ftrace_graph_entry;
1345 
1346 extern void ftrace_graph_init_task(struct task_struct *t);
1347 extern void ftrace_graph_exit_task(struct task_struct *t);
1348 extern void ftrace_graph_init_idle_task(struct task_struct *t, int cpu);
1349 
1350 /* Used by assembly, but to quiet sparse warnings */
1351 extern struct ftrace_ops *function_trace_op;
1352 
1353 static inline void pause_graph_tracing(void)
1354 {
1355 	atomic_inc(&current->tracing_graph_pause);
1356 }
1357 
1358 static inline void unpause_graph_tracing(void)
1359 {
1360 	atomic_dec(&current->tracing_graph_pause);
1361 }
1362 #else /* !CONFIG_FUNCTION_GRAPH_TRACER */
1363 
1364 #define __notrace_funcgraph
1365 
1366 static inline void ftrace_graph_init_task(struct task_struct *t) { }
1367 static inline void ftrace_graph_exit_task(struct task_struct *t) { }
1368 static inline void ftrace_graph_init_idle_task(struct task_struct *t, int cpu) { }
1369 
1370 /* Define as macros as fgraph_ops may not be defined */
1371 #define register_ftrace_graph(ops) ({ -1; })
1372 #define unregister_ftrace_graph(ops) do { } while (0)
1373 
1374 static inline unsigned long
1375 ftrace_graph_ret_addr(struct task_struct *task, int *idx, unsigned long ret,
1376 		      unsigned long *retp)
1377 {
1378 	return ret;
1379 }
1380 
1381 static inline void pause_graph_tracing(void) { }
1382 static inline void unpause_graph_tracing(void) { }
1383 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1384 
1385 #ifdef CONFIG_TRACING
1386 enum ftrace_dump_mode;
1387 
1388 extern int ftrace_dump_on_oops_enabled(void);
1389 
1390 extern void disable_trace_on_warning(void);
1391 
1392 #else /* CONFIG_TRACING */
1393 static inline void  disable_trace_on_warning(void) { }
1394 #endif /* CONFIG_TRACING */
1395 
1396 #ifdef CONFIG_FTRACE_SYSCALLS
1397 
1398 unsigned long arch_syscall_addr(int nr);
1399 
1400 #endif /* CONFIG_FTRACE_SYSCALLS */
1401 
1402 #endif /* _LINUX_FTRACE_H */
1403