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