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