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