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