xref: /linux-6.15/include/linux/kernel.h (revision 3cd39bc3)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
240cbf09fSAndy Shevchenko /*
340cbf09fSAndy Shevchenko  * NOTE:
440cbf09fSAndy Shevchenko  *
540cbf09fSAndy Shevchenko  * This header has combined a lot of unrelated to each other stuff.
640cbf09fSAndy Shevchenko  * The process of splitting its content is in progress while keeping
740cbf09fSAndy Shevchenko  * backward compatibility. That's why it's highly recommended NOT to
840cbf09fSAndy Shevchenko  * include this header inside another header file, especially under
940cbf09fSAndy Shevchenko  * generic or architectural include/ directory.
1040cbf09fSAndy Shevchenko  */
111da177e4SLinus Torvalds #ifndef _LINUX_KERNEL_H
121da177e4SLinus Torvalds #define _LINUX_KERNEL_H
131da177e4SLinus Torvalds 
14c0891ac1SAlexey Dobriyan #include <linux/stdarg.h>
1508c5188eSAndy Shevchenko #include <linux/align.h>
16*3cd39bc3SAlejandro Colomar #include <linux/array_size.h>
1754d50897SMasahiro Yamada #include <linux/limits.h>
181da177e4SLinus Torvalds #include <linux/linkage.h>
191da177e4SLinus Torvalds #include <linux/stddef.h>
201da177e4SLinus Torvalds #include <linux/types.h>
211da177e4SLinus Torvalds #include <linux/compiler.h>
22d2a8ebbfSAndy Shevchenko #include <linux/container_of.h>
231da177e4SLinus Torvalds #include <linux/bitops.h>
24890a3ee3SAndy Shevchenko #include <linux/hex.h>
254c527293SAndy Shevchenko #include <linux/kstrtox.h>
26f0d1b0b3SDavid Howells #include <linux/log2.h>
27aa6159abSAndy Shevchenko #include <linux/math.h>
28b296a6d5SAndy Shevchenko #include <linux/minmax.h>
29e0deaff4SAndrew Morton #include <linux/typecheck.h>
30f39650deSAndy Shevchenko #include <linux/panic.h>
31968ab183SLinus Torvalds #include <linux/printk.h>
32c7acec71SIan Abbott #include <linux/build_bug.h>
3339ced19bSAndy Shevchenko #include <linux/sprintf.h>
34b965f1ddSPeter Zijlstra (Intel) #include <linux/static_call_types.h>
35e52340deSStephen Rothwell #include <linux/instruction_pointer.h>
361da177e4SLinus Torvalds #include <asm/byteorder.h>
37aa6159abSAndy Shevchenko 
38607ca46eSDavid Howells #include <uapi/linux/kernel.h>
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds #define STACK_MAGIC	0xdeadbeef
411da177e4SLinus Torvalds 
42e8c97af0SRandy Dunlap /**
43e8c97af0SRandy Dunlap  * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value
44e8c97af0SRandy Dunlap  * @x: value to repeat
45e8c97af0SRandy Dunlap  *
46e8c97af0SRandy Dunlap  * NOTE: @x is not checked for > 0xff; larger values produce odd results.
47e8c97af0SRandy Dunlap  */
4844696908SDavid S. Miller #define REPEAT_BYTE(x)	((~0ul / 0xff) * (x))
4944696908SDavid S. Miller 
50d3849953SChristoph Hellwig /* generic data direction definitions */
51d3849953SChristoph Hellwig #define READ			0
52d3849953SChristoph Hellwig #define WRITE			1
53d3849953SChristoph Hellwig 
540ab1438bSMasahiro Yamada #define PTR_IF(cond, ptr)	((cond) ? (ptr) : NULL)
550ab1438bSMasahiro Yamada 
563ed605bcSGustavo Padovan #define u64_to_user_ptr(x) (		\
573ed605bcSGustavo Padovan {					\
58a0fe2c64SJann Horn 	typecheck(u64, (x));		\
59a0fe2c64SJann Horn 	(void __user *)(uintptr_t)(x);	\
603ed605bcSGustavo Padovan }					\
613ed605bcSGustavo Padovan )
623ed605bcSGustavo Padovan 
63218e180eSAndrew Morton /**
64218e180eSAndrew Morton  * upper_32_bits - return bits 32-63 of a number
65218e180eSAndrew Morton  * @n: the number we're accessing
66218e180eSAndrew Morton  *
67218e180eSAndrew Morton  * A basic shift-right of a 64- or 32-bit quantity.  Use this to suppress
68218e180eSAndrew Morton  * the "right shift count >= width of type" warning when that quantity is
69218e180eSAndrew Morton  * 32-bits.
70218e180eSAndrew Morton  */
71218e180eSAndrew Morton #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
72218e180eSAndrew Morton 
73204b885eSJoerg Roedel /**
74204b885eSJoerg Roedel  * lower_32_bits - return bits 0-31 of a number
75204b885eSJoerg Roedel  * @n: the number we're accessing
76204b885eSJoerg Roedel  */
77ef91bb19SHerbert Xu #define lower_32_bits(n) ((u32)((n) & 0xffffffff))
78204b885eSJoerg Roedel 
7903cb4473SJacob Keller /**
8003cb4473SJacob Keller  * upper_16_bits - return bits 16-31 of a number
8103cb4473SJacob Keller  * @n: the number we're accessing
8203cb4473SJacob Keller  */
8303cb4473SJacob Keller #define upper_16_bits(n) ((u16)((n) >> 16))
8403cb4473SJacob Keller 
8503cb4473SJacob Keller /**
8603cb4473SJacob Keller  * lower_16_bits - return bits 0-15 of a number
8703cb4473SJacob Keller  * @n: the number we're accessing
8803cb4473SJacob Keller  */
8903cb4473SJacob Keller #define lower_16_bits(n) ((u16)((n) & 0xffff))
9003cb4473SJacob Keller 
911da177e4SLinus Torvalds struct completion;
92df2e71fbS[email protected] struct user;
931da177e4SLinus Torvalds 
94a8b76910SValentin Schneider #ifdef CONFIG_PREEMPT_VOLUNTARY_BUILD
95b965f1ddSPeter Zijlstra (Intel) 
96b965f1ddSPeter Zijlstra (Intel) extern int __cond_resched(void);
97b965f1ddSPeter Zijlstra (Intel) # define might_resched() __cond_resched()
98b965f1ddSPeter Zijlstra (Intel) 
9999cf983cSMark Rutland #elif defined(CONFIG_PREEMPT_DYNAMIC) && defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL)
100b965f1ddSPeter Zijlstra (Intel) 
101b965f1ddSPeter Zijlstra (Intel) extern int __cond_resched(void);
102b965f1ddSPeter Zijlstra (Intel) 
103b965f1ddSPeter Zijlstra (Intel) DECLARE_STATIC_CALL(might_resched, __cond_resched);
104b965f1ddSPeter Zijlstra (Intel) 
105b965f1ddSPeter Zijlstra (Intel) static __always_inline void might_resched(void)
106b965f1ddSPeter Zijlstra (Intel) {
107ef72661eSPeter Zijlstra 	static_call_mod(might_resched)();
108b965f1ddSPeter Zijlstra (Intel) }
109b965f1ddSPeter Zijlstra (Intel) 
11099cf983cSMark Rutland #elif defined(CONFIG_PREEMPT_DYNAMIC) && defined(CONFIG_HAVE_PREEMPT_DYNAMIC_KEY)
11199cf983cSMark Rutland 
11299cf983cSMark Rutland extern int dynamic_might_resched(void);
11399cf983cSMark Rutland # define might_resched() dynamic_might_resched()
11499cf983cSMark Rutland 
115070cb065SUwe Kleine-König #else
116b965f1ddSPeter Zijlstra (Intel) 
117070cb065SUwe Kleine-König # define might_resched() do { } while (0)
118b965f1ddSPeter Zijlstra (Intel) 
119b965f1ddSPeter Zijlstra (Intel) #endif /* CONFIG_PREEMPT_* */
120070cb065SUwe Kleine-König 
121d902db1eSFrederic Weisbecker #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
12250e081b9SThomas Gleixner extern void __might_resched(const char *file, int line, unsigned int offsets);
12342a38756SThomas Gleixner extern void __might_sleep(const char *file, int line);
124568f1967SPeter Zijlstra extern void __cant_sleep(const char *file, int line, int preempt_offset);
12574d862b6SThomas Gleixner extern void __cant_migrate(const char *file, int line);
126568f1967SPeter Zijlstra 
1271da177e4SLinus Torvalds /**
1281da177e4SLinus Torvalds  * might_sleep - annotation for functions that can sleep
1291da177e4SLinus Torvalds  *
1301da177e4SLinus Torvalds  * this macro will print a stack trace if it is executed in an atomic
131312364f3SDaniel Vetter  * context (spinlock, irq-handler, ...). Additional sections where blocking is
132312364f3SDaniel Vetter  * not allowed can be annotated with non_block_start() and non_block_end()
133312364f3SDaniel Vetter  * pairs.
1341da177e4SLinus Torvalds  *
1351da177e4SLinus Torvalds  * This is a useful debugging help to be able to catch problems early and not
136e20ec991SJim Cromie  * be bitten later when the calling function happens to sleep when it is not
1371da177e4SLinus Torvalds  * supposed to.
1381da177e4SLinus Torvalds  */
139f8cbd99bSIngo Molnar # define might_sleep() \
14042a38756SThomas Gleixner 	do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0)
141568f1967SPeter Zijlstra /**
142568f1967SPeter Zijlstra  * cant_sleep - annotation for functions that cannot sleep
143568f1967SPeter Zijlstra  *
144568f1967SPeter Zijlstra  * this macro will print a stack trace if it is executed with preemption enabled
145568f1967SPeter Zijlstra  */
146568f1967SPeter Zijlstra # define cant_sleep() \
147568f1967SPeter Zijlstra 	do { __cant_sleep(__FILE__, __LINE__, 0); } while (0)
14800845eb9SLinus Torvalds # define sched_annotate_sleep()	(current->task_state_change = 0)
14974d862b6SThomas Gleixner 
15074d862b6SThomas Gleixner /**
15174d862b6SThomas Gleixner  * cant_migrate - annotation for functions that cannot migrate
15274d862b6SThomas Gleixner  *
15374d862b6SThomas Gleixner  * Will print a stack trace if executed in code which is migratable
15474d862b6SThomas Gleixner  */
15574d862b6SThomas Gleixner # define cant_migrate()							\
15674d862b6SThomas Gleixner 	do {								\
15774d862b6SThomas Gleixner 		if (IS_ENABLED(CONFIG_SMP))				\
15874d862b6SThomas Gleixner 			__cant_migrate(__FILE__, __LINE__);		\
15974d862b6SThomas Gleixner 	} while (0)
16074d862b6SThomas Gleixner 
161312364f3SDaniel Vetter /**
162312364f3SDaniel Vetter  * non_block_start - annotate the start of section where sleeping is prohibited
163312364f3SDaniel Vetter  *
164312364f3SDaniel Vetter  * This is on behalf of the oom reaper, specifically when it is calling the mmu
165312364f3SDaniel Vetter  * notifiers. The problem is that if the notifier were to block on, for example,
166312364f3SDaniel Vetter  * mutex_lock() and if the process which holds that mutex were to perform a
167312364f3SDaniel Vetter  * sleeping memory allocation, the oom reaper is now blocked on completion of
168312364f3SDaniel Vetter  * that memory allocation. Other blocking calls like wait_event() pose similar
169312364f3SDaniel Vetter  * issues.
170312364f3SDaniel Vetter  */
171312364f3SDaniel Vetter # define non_block_start() (current->non_block_count++)
172312364f3SDaniel Vetter /**
173312364f3SDaniel Vetter  * non_block_end - annotate the end of section where sleeping is prohibited
174312364f3SDaniel Vetter  *
175312364f3SDaniel Vetter  * Closes a section opened by non_block_start().
176312364f3SDaniel Vetter  */
177312364f3SDaniel Vetter # define non_block_end() WARN_ON(current->non_block_count-- == 0)
178f8cbd99bSIngo Molnar #else
179874f670eSThomas Gleixner   static inline void __might_resched(const char *file, int line,
18050e081b9SThomas Gleixner 				     unsigned int offsets) { }
18142a38756SThomas Gleixner static inline void __might_sleep(const char *file, int line) { }
182f8cbd99bSIngo Molnar # define might_sleep() do { might_resched(); } while (0)
183568f1967SPeter Zijlstra # define cant_sleep() do { } while (0)
18474d862b6SThomas Gleixner # define cant_migrate()		do { } while (0)
1851029a2b5SPeter Zijlstra # define sched_annotate_sleep() do { } while (0)
186312364f3SDaniel Vetter # define non_block_start() do { } while (0)
187312364f3SDaniel Vetter # define non_block_end() do { } while (0)
188f8cbd99bSIngo Molnar #endif
189f8cbd99bSIngo Molnar 
190368a5fa1SHua Zhong #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
191f8cbd99bSIngo Molnar 
192386e7906SAxel Lin #if defined(CONFIG_MMU) && \
193386e7906SAxel Lin 	(defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP))
1949ec23531SDavid Hildenbrand #define might_fault() __might_fault(__FILE__, __LINE__)
1959ec23531SDavid Hildenbrand void __might_fault(const char *file, int line);
1963ee1afa3SNick Piggin #else
197662bbcb2SMichael S. Tsirkin static inline void might_fault(void) { }
1983ee1afa3SNick Piggin #endif
1993ee1afa3SNick Piggin 
2009af6528eSPeter Zijlstra void do_exit(long error_code) __noreturn;
20133ee3b2eSAlexey Dobriyan 
2021da177e4SLinus Torvalds extern int get_option(char **str, int *pint);
2031da177e4SLinus Torvalds extern char *get_options(const char *str, int nints, int *ints);
204d974ae37SJeremy Fitzhardinge extern unsigned long long memparse(const char *ptr, char **retptr);
2056ccc72b8SDave Young extern bool parse_option_str(const char *str, const char *option);
206f51b17c8SBaoquan He extern char *next_arg(char *args, char **param, char **val);
2071da177e4SLinus Torvalds 
2085e376613STrent Piepho extern int core_kernel_text(unsigned long addr);
2091da177e4SLinus Torvalds extern int __kernel_text_address(unsigned long addr);
2101da177e4SLinus Torvalds extern int kernel_text_address(unsigned long addr);
211ab7476cfSArjan van de Ven extern int func_ptr_is_kernel_text(void *ptr);
212ab7476cfSArjan van de Ven 
2131da177e4SLinus Torvalds extern void bust_spinlocks(int yes);
2145375b708SHATAYAMA Daisuke 
215b920de1bSDavid Howells extern int root_mountflags;
2161da177e4SLinus Torvalds 
2172ce802f6STejun Heo extern bool early_boot_irqs_disabled;
2182ce802f6STejun Heo 
21969a78ff2SThomas Gleixner /*
22069a78ff2SThomas Gleixner  * Values used for system_state. Ordering of the states must not be changed
22169a78ff2SThomas Gleixner  * as code checks for <, <=, >, >= STATE.
22269a78ff2SThomas Gleixner  */
2231da177e4SLinus Torvalds extern enum system_states {
2241da177e4SLinus Torvalds 	SYSTEM_BOOTING,
22569a78ff2SThomas Gleixner 	SYSTEM_SCHEDULING,
226d2635f20SChristophe Leroy 	SYSTEM_FREEING_INITMEM,
2271da177e4SLinus Torvalds 	SYSTEM_RUNNING,
2281da177e4SLinus Torvalds 	SYSTEM_HALT,
2291da177e4SLinus Torvalds 	SYSTEM_POWER_OFF,
2301da177e4SLinus Torvalds 	SYSTEM_RESTART,
231c1a957d1SThomas Gleixner 	SYSTEM_SUSPEND,
2321da177e4SLinus Torvalds } system_state;
2331da177e4SLinus Torvalds 
2348a64f336SJoe Perches /*
235526211bcSIngo Molnar  * General tracing related utility functions - trace_printk(),
2362002c258SSteven Rostedt  * tracing_on/tracing_off and tracing_start()/tracing_stop
2372002c258SSteven Rostedt  *
2382002c258SSteven Rostedt  * Use tracing_on/tracing_off when you want to quickly turn on or off
2392002c258SSteven Rostedt  * tracing. It simply enables or disables the recording of the trace events.
2402455f0e1SRoss Zwisler  * This also corresponds to the user space /sys/kernel/tracing/tracing_on
2412002c258SSteven Rostedt  * file, which gives a means for the kernel and userspace to interact.
2422002c258SSteven Rostedt  * Place a tracing_off() in the kernel where you want tracing to end.
2432002c258SSteven Rostedt  * From user space, examine the trace, and then echo 1 > tracing_on
2442002c258SSteven Rostedt  * to continue tracing.
2452002c258SSteven Rostedt  *
2462002c258SSteven Rostedt  * tracing_stop/tracing_start has slightly more overhead. It is used
2472002c258SSteven Rostedt  * by things like suspend to ram where disabling the recording of the
2482002c258SSteven Rostedt  * trace is not enough, but tracing must actually stop because things
2492002c258SSteven Rostedt  * like calling smp_processor_id() may crash the system.
2502002c258SSteven Rostedt  *
2512002c258SSteven Rostedt  * Most likely, you want to use tracing_on/tracing_off.
252526211bcSIngo Molnar  */
253cecbca96SFrederic Weisbecker 
254cecbca96SFrederic Weisbecker enum ftrace_dump_mode {
255cecbca96SFrederic Weisbecker 	DUMP_NONE,
256cecbca96SFrederic Weisbecker 	DUMP_ALL,
257cecbca96SFrederic Weisbecker 	DUMP_ORIG,
258cecbca96SFrederic Weisbecker };
259cecbca96SFrederic Weisbecker 
260526211bcSIngo Molnar #ifdef CONFIG_TRACING
26193d68e52SSteven Rostedt void tracing_on(void);
26293d68e52SSteven Rostedt void tracing_off(void);
26393d68e52SSteven Rostedt int tracing_is_on(void);
264ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot(void);
265ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot_alloc(void);
26693d68e52SSteven Rostedt 
267526211bcSIngo Molnar extern void tracing_start(void);
268526211bcSIngo Molnar extern void tracing_stop(void);
269526211bcSIngo Molnar 
270b9075fa9SJoe Perches static inline __printf(1, 2)
271b9075fa9SJoe Perches void ____trace_printk_check_format(const char *fmt, ...)
272769b0441SFrederic Weisbecker {
273769b0441SFrederic Weisbecker }
274769b0441SFrederic Weisbecker #define __trace_printk_check_format(fmt, args...)			\
275769b0441SFrederic Weisbecker do {									\
276769b0441SFrederic Weisbecker 	if (0)								\
277769b0441SFrederic Weisbecker 		____trace_printk_check_format(fmt, ##args);		\
278769b0441SFrederic Weisbecker } while (0)
279769b0441SFrederic Weisbecker 
280526211bcSIngo Molnar /**
281526211bcSIngo Molnar  * trace_printk - printf formatting in the ftrace buffer
282526211bcSIngo Molnar  * @fmt: the printf format for printing
283526211bcSIngo Molnar  *
284e8c97af0SRandy Dunlap  * Note: __trace_printk is an internal function for trace_printk() and
285e8c97af0SRandy Dunlap  *       the @ip is passed in via the trace_printk() macro.
286526211bcSIngo Molnar  *
287526211bcSIngo Molnar  * This function allows a kernel developer to debug fast path sections
288526211bcSIngo Molnar  * that printk is not appropriate for. By scattering in various
289526211bcSIngo Molnar  * printk like tracing in the code, a developer can quickly see
290526211bcSIngo Molnar  * where problems are occurring.
291526211bcSIngo Molnar  *
292526211bcSIngo Molnar  * This is intended as a debugging tool for the developer only.
293526211bcSIngo Molnar  * Please refrain from leaving trace_printks scattered around in
29409ae7234SSteven Rostedt (Red Hat)  * your code. (Extra memory is used for special buffers that are
295e8c97af0SRandy Dunlap  * allocated when trace_printk() is used.)
2969d3c752cSSteven Rostedt (Red Hat)  *
2978730662dSWei Wang  * A little optimization trick is done here. If there's only one
2989d3c752cSSteven Rostedt (Red Hat)  * argument, there's no need to scan the string for printf formats.
2999d3c752cSSteven Rostedt (Red Hat)  * The trace_puts() will suffice. But how can we take advantage of
3009d3c752cSSteven Rostedt (Red Hat)  * using trace_puts() when trace_printk() has only one argument?
3019d3c752cSSteven Rostedt (Red Hat)  * By stringifying the args and checking the size we can tell
3029d3c752cSSteven Rostedt (Red Hat)  * whether or not there are args. __stringify((__VA_ARGS__)) will
3039d3c752cSSteven Rostedt (Red Hat)  * turn into "()\0" with a size of 3 when there are no args, anything
3049d3c752cSSteven Rostedt (Red Hat)  * else will be bigger. All we need to do is define a string to this,
3059d3c752cSSteven Rostedt (Red Hat)  * and then take its size and compare to 3. If it's bigger, use
3069d3c752cSSteven Rostedt (Red Hat)  * do_trace_printk() otherwise, optimize it to trace_puts(). Then just
3079d3c752cSSteven Rostedt (Red Hat)  * let gcc optimize the rest.
308526211bcSIngo Molnar  */
309769b0441SFrederic Weisbecker 
3109d3c752cSSteven Rostedt (Red Hat) #define trace_printk(fmt, ...)				\
3119d3c752cSSteven Rostedt (Red Hat) do {							\
3129d3c752cSSteven Rostedt (Red Hat) 	char _______STR[] = __stringify((__VA_ARGS__));	\
3139d3c752cSSteven Rostedt (Red Hat) 	if (sizeof(_______STR) > 3)			\
3149d3c752cSSteven Rostedt (Red Hat) 		do_trace_printk(fmt, ##__VA_ARGS__);	\
3159d3c752cSSteven Rostedt (Red Hat) 	else						\
3169d3c752cSSteven Rostedt (Red Hat) 		trace_puts(fmt);			\
3179d3c752cSSteven Rostedt (Red Hat) } while (0)
3189d3c752cSSteven Rostedt (Red Hat) 
3199d3c752cSSteven Rostedt (Red Hat) #define do_trace_printk(fmt, args...)					\
320769b0441SFrederic Weisbecker do {									\
3213debb0a9SSteven Rostedt (Red Hat) 	static const char *trace_printk_fmt __used			\
32233def849SJoe Perches 		__section("__trace_printk_fmt") =			\
32348ead020SFrederic Weisbecker 		__builtin_constant_p(fmt) ? fmt : NULL;			\
32448ead020SFrederic Weisbecker 									\
32507d777feSSteven Rostedt 	__trace_printk_check_format(fmt, ##args);			\
32607d777feSSteven Rostedt 									\
32707d777feSSteven Rostedt 	if (__builtin_constant_p(fmt))					\
32848ead020SFrederic Weisbecker 		__trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args);	\
32907d777feSSteven Rostedt 	else								\
33048ead020SFrederic Weisbecker 		__trace_printk(_THIS_IP_, fmt, ##args);			\
331769b0441SFrederic Weisbecker } while (0)
332769b0441SFrederic Weisbecker 
333b9075fa9SJoe Perches extern __printf(2, 3)
334b9075fa9SJoe Perches int __trace_bprintk(unsigned long ip, const char *fmt, ...);
33548ead020SFrederic Weisbecker 
336b9075fa9SJoe Perches extern __printf(2, 3)
337b9075fa9SJoe Perches int __trace_printk(unsigned long ip, const char *fmt, ...);
338769b0441SFrederic Weisbecker 
33909ae7234SSteven Rostedt (Red Hat) /**
34009ae7234SSteven Rostedt (Red Hat)  * trace_puts - write a string into the ftrace buffer
34109ae7234SSteven Rostedt (Red Hat)  * @str: the string to record
34209ae7234SSteven Rostedt (Red Hat)  *
34309ae7234SSteven Rostedt (Red Hat)  * Note: __trace_bputs is an internal function for trace_puts and
34409ae7234SSteven Rostedt (Red Hat)  *       the @ip is passed in via the trace_puts macro.
34509ae7234SSteven Rostedt (Red Hat)  *
34609ae7234SSteven Rostedt (Red Hat)  * This is similar to trace_printk() but is made for those really fast
347e8c97af0SRandy Dunlap  * paths that a developer wants the least amount of "Heisenbug" effects,
34809ae7234SSteven Rostedt (Red Hat)  * where the processing of the print format is still too much.
34909ae7234SSteven Rostedt (Red Hat)  *
35009ae7234SSteven Rostedt (Red Hat)  * This function allows a kernel developer to debug fast path sections
35109ae7234SSteven Rostedt (Red Hat)  * that printk is not appropriate for. By scattering in various
35209ae7234SSteven Rostedt (Red Hat)  * printk like tracing in the code, a developer can quickly see
35309ae7234SSteven Rostedt (Red Hat)  * where problems are occurring.
35409ae7234SSteven Rostedt (Red Hat)  *
35509ae7234SSteven Rostedt (Red Hat)  * This is intended as a debugging tool for the developer only.
35609ae7234SSteven Rostedt (Red Hat)  * Please refrain from leaving trace_puts scattered around in
35709ae7234SSteven Rostedt (Red Hat)  * your code. (Extra memory is used for special buffers that are
358e8c97af0SRandy Dunlap  * allocated when trace_puts() is used.)
35909ae7234SSteven Rostedt (Red Hat)  *
36009ae7234SSteven Rostedt (Red Hat)  * Returns: 0 if nothing was written, positive # if string was.
36109ae7234SSteven Rostedt (Red Hat)  *  (1 when __trace_bputs is used, strlen(str) when __trace_puts is used)
36209ae7234SSteven Rostedt (Red Hat)  */
36309ae7234SSteven Rostedt (Red Hat) 
36409ae7234SSteven Rostedt (Red Hat) #define trace_puts(str) ({						\
3653debb0a9SSteven Rostedt (Red Hat) 	static const char *trace_printk_fmt __used			\
36633def849SJoe Perches 		__section("__trace_printk_fmt") =			\
36709ae7234SSteven Rostedt (Red Hat) 		__builtin_constant_p(str) ? str : NULL;			\
36809ae7234SSteven Rostedt (Red Hat) 									\
36909ae7234SSteven Rostedt (Red Hat) 	if (__builtin_constant_p(str))					\
37009ae7234SSteven Rostedt (Red Hat) 		__trace_bputs(_THIS_IP_, trace_printk_fmt);		\
37109ae7234SSteven Rostedt (Red Hat) 	else								\
37209ae7234SSteven Rostedt (Red Hat) 		__trace_puts(_THIS_IP_, str, strlen(str));		\
37309ae7234SSteven Rostedt (Red Hat) })
374bcf312cfSSteven Rostedt extern int __trace_bputs(unsigned long ip, const char *str);
375bcf312cfSSteven Rostedt extern int __trace_puts(unsigned long ip, const char *str, int size);
37609ae7234SSteven Rostedt (Red Hat) 
377c142be8eSSteven Rostedt (Red Hat) extern void trace_dump_stack(int skip);
37803889384SSteven Rostedt 
37948ead020SFrederic Weisbecker /*
38048ead020SFrederic Weisbecker  * The double __builtin_constant_p is because gcc will give us an error
38148ead020SFrederic Weisbecker  * if we try to allocate the static variable to fmt if it is not a
38248ead020SFrederic Weisbecker  * constant. Even with the outer if statement.
38348ead020SFrederic Weisbecker  */
384769b0441SFrederic Weisbecker #define ftrace_vprintk(fmt, vargs)					\
385769b0441SFrederic Weisbecker do {									\
38648ead020SFrederic Weisbecker 	if (__builtin_constant_p(fmt)) {				\
3873debb0a9SSteven Rostedt (Red Hat) 		static const char *trace_printk_fmt __used		\
38833def849SJoe Perches 		  __section("__trace_printk_fmt") =			\
38948ead020SFrederic Weisbecker 			__builtin_constant_p(fmt) ? fmt : NULL;		\
3907bffc23eSIngo Molnar 									\
39148ead020SFrederic Weisbecker 		__ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs);	\
39248ead020SFrederic Weisbecker 	} else								\
39348ead020SFrederic Weisbecker 		__ftrace_vprintk(_THIS_IP_, fmt, vargs);		\
394769b0441SFrederic Weisbecker } while (0)
395769b0441SFrederic Weisbecker 
3968db14860SNicolas Iooss extern __printf(2, 0) int
39748ead020SFrederic Weisbecker __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
39848ead020SFrederic Weisbecker 
3998db14860SNicolas Iooss extern __printf(2, 0) int
400526211bcSIngo Molnar __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
401769b0441SFrederic Weisbecker 
402cecbca96SFrederic Weisbecker extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
403526211bcSIngo Molnar #else
404526211bcSIngo Molnar static inline void tracing_start(void) { }
405526211bcSIngo Molnar static inline void tracing_stop(void) { }
406e67bc51eSDhaval Giani static inline void trace_dump_stack(int skip) { }
40793d68e52SSteven Rostedt 
40893d68e52SSteven Rostedt static inline void tracing_on(void) { }
40993d68e52SSteven Rostedt static inline void tracing_off(void) { }
41093d68e52SSteven Rostedt static inline int tracing_is_on(void) { return 0; }
411ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot(void) { }
412ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot_alloc(void) { }
41393d68e52SSteven Rostedt 
41460efc15aSMichal Hocko static inline __printf(1, 2)
41560efc15aSMichal Hocko int trace_printk(const char *fmt, ...)
416526211bcSIngo Molnar {
417526211bcSIngo Molnar 	return 0;
418526211bcSIngo Molnar }
4198db14860SNicolas Iooss static __printf(1, 0) inline int
420526211bcSIngo Molnar ftrace_vprintk(const char *fmt, va_list ap)
421526211bcSIngo Molnar {
422526211bcSIngo Molnar 	return 0;
423526211bcSIngo Molnar }
424cecbca96SFrederic Weisbecker static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
425769b0441SFrederic Weisbecker #endif /* CONFIG_TRACING */
426526211bcSIngo Molnar 
427b9d4f426SArnaud Lacombe /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
428b9d4f426SArnaud Lacombe #ifdef CONFIG_FTRACE_MCOUNT_RECORD
429b9d4f426SArnaud Lacombe # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
430b9d4f426SArnaud Lacombe #endif
4319d00f92fSWANG Cong 
43258f86cc8SRusty Russell /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */
43358f86cc8SRusty Russell #define VERIFY_OCTAL_PERMISSIONS(perms)						\
43458f86cc8SRusty Russell 	(BUILD_BUG_ON_ZERO((perms) < 0) +					\
43558f86cc8SRusty Russell 	 BUILD_BUG_ON_ZERO((perms) > 0777) +					\
43628b8d0c8SGobinda Charan Maji 	 /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */		\
43728b8d0c8SGobinda Charan Maji 	 BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) +	\
43828b8d0c8SGobinda Charan Maji 	 BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) +		\
43928b8d0c8SGobinda Charan Maji 	 /* USER_WRITABLE >= GROUP_WRITABLE */					\
44028b8d0c8SGobinda Charan Maji 	 BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) +	\
44128b8d0c8SGobinda Charan Maji 	 /* OTHER_WRITABLE?  Generally considered a bad idea. */		\
44237549e94SRusty Russell 	 BUILD_BUG_ON_ZERO((perms) & 2) +					\
44358f86cc8SRusty Russell 	 (perms))
4441da177e4SLinus Torvalds #endif
445