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