xref: /linux-6.15/include/linux/kernel.h (revision a0fe2c64)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef _LINUX_KERNEL_H
31da177e4SLinus Torvalds #define _LINUX_KERNEL_H
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds #include <stdarg.h>
754d50897SMasahiro Yamada #include <linux/limits.h>
81da177e4SLinus Torvalds #include <linux/linkage.h>
91da177e4SLinus Torvalds #include <linux/stddef.h>
101da177e4SLinus Torvalds #include <linux/types.h>
111da177e4SLinus Torvalds #include <linux/compiler.h>
121da177e4SLinus Torvalds #include <linux/bitops.h>
13f0d1b0b3SDavid Howells #include <linux/log2.h>
14e0deaff4SAndrew Morton #include <linux/typecheck.h>
15968ab183SLinus Torvalds #include <linux/printk.h>
16c7acec71SIan Abbott #include <linux/build_bug.h>
171da177e4SLinus Torvalds #include <asm/byteorder.h>
18c461aed3SJani Nikula #include <asm/div64.h>
19607ca46eSDavid Howells #include <uapi/linux/kernel.h>
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds #define STACK_MAGIC	0xdeadbeef
221da177e4SLinus Torvalds 
23e8c97af0SRandy Dunlap /**
24e8c97af0SRandy Dunlap  * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value
25e8c97af0SRandy Dunlap  * @x: value to repeat
26e8c97af0SRandy Dunlap  *
27e8c97af0SRandy Dunlap  * NOTE: @x is not checked for > 0xff; larger values produce odd results.
28e8c97af0SRandy Dunlap  */
2944696908SDavid S. Miller #define REPEAT_BYTE(x)	((~0ul / 0xff) * (x))
3044696908SDavid S. Miller 
313ca45a46Szijun_hu /* @a is a power of 2 value */
32a79ff731SAlexey Dobriyan #define ALIGN(x, a)		__ALIGN_KERNEL((x), (a))
33ed067d4aSKrzysztof Kozlowski #define ALIGN_DOWN(x, a)	__ALIGN_KERNEL((x) - ((a) - 1), (a))
349f93ff5bSAlexey Dobriyan #define __ALIGN_MASK(x, mask)	__ALIGN_KERNEL_MASK((x), (mask))
35a83308e6SMatthew Wilcox #define PTR_ALIGN(p, a)		((typeof(p))ALIGN((unsigned long)(p), (a)))
36f10db627SHerbert Xu #define IS_ALIGNED(x, a)		(((x) & ((typeof(x))(a) - 1)) == 0)
372ea58144SLinus Torvalds 
38d3849953SChristoph Hellwig /* generic data direction definitions */
39d3849953SChristoph Hellwig #define READ			0
40d3849953SChristoph Hellwig #define WRITE			1
41d3849953SChristoph Hellwig 
42e8c97af0SRandy Dunlap /**
43e8c97af0SRandy Dunlap  * ARRAY_SIZE - get the number of elements in array @arr
44e8c97af0SRandy Dunlap  * @arr: array to be sized
45e8c97af0SRandy Dunlap  */
46c5e631cfSRusty Russell #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
47c5e631cfSRusty Russell 
483ed605bcSGustavo Padovan #define u64_to_user_ptr(x) (		\
493ed605bcSGustavo Padovan {					\
50*a0fe2c64SJann Horn 	typecheck(u64, (x));		\
51*a0fe2c64SJann Horn 	(void __user *)(uintptr_t)(x);	\
523ed605bcSGustavo Padovan }					\
533ed605bcSGustavo Padovan )
543ed605bcSGustavo Padovan 
559b3be9f9SYinghai Lu /*
569b3be9f9SYinghai Lu  * This looks more complex than it should be. But we need to
579b3be9f9SYinghai Lu  * get the type for the ~ right in round_down (it needs to be
589b3be9f9SYinghai Lu  * as wide as the result!), and we want to evaluate the macro
599b3be9f9SYinghai Lu  * arguments just once each.
609b3be9f9SYinghai Lu  */
619b3be9f9SYinghai Lu #define __round_mask(x, y) ((__typeof__(x))((y)-1))
62cedc5b6aSKees Cook /**
63cedc5b6aSKees Cook  * round_up - round up to next specified power of 2
64cedc5b6aSKees Cook  * @x: the value to round
65cedc5b6aSKees Cook  * @y: multiple to round up to (must be a power of 2)
66cedc5b6aSKees Cook  *
67cedc5b6aSKees Cook  * Rounds @x up to next multiple of @y (which must be a power of 2).
68cedc5b6aSKees Cook  * To perform arbitrary rounding up, use roundup() below.
69cedc5b6aSKees Cook  */
709b3be9f9SYinghai Lu #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
71cedc5b6aSKees Cook /**
72cedc5b6aSKees Cook  * round_down - round down to next specified power of 2
73cedc5b6aSKees Cook  * @x: the value to round
74cedc5b6aSKees Cook  * @y: multiple to round down to (must be a power of 2)
75cedc5b6aSKees Cook  *
76cedc5b6aSKees Cook  * Rounds @x down to next multiple of @y (which must be a power of 2).
77cedc5b6aSKees Cook  * To perform arbitrary rounding down, use rounddown() below.
78cedc5b6aSKees Cook  */
799b3be9f9SYinghai Lu #define round_down(x, y) ((x) & ~__round_mask(x, y))
809b3be9f9SYinghai Lu 
81e8c97af0SRandy Dunlap /**
82e8c97af0SRandy Dunlap  * FIELD_SIZEOF - get the size of a struct's field
83e8c97af0SRandy Dunlap  * @t: the target struct
84e8c97af0SRandy Dunlap  * @f: the target struct's field
85e8c97af0SRandy Dunlap  * Return: the size of @f in the struct definition without having a
86e8c97af0SRandy Dunlap  * declared instance of @t.
87e8c97af0SRandy Dunlap  */
884552d5dcSJan Beulich #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
89e8c97af0SRandy Dunlap 
90b5d3755aSNicolas Dichtel #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP
91604df322SMasahiro Yamada 
92604df322SMasahiro Yamada #define DIV_ROUND_DOWN_ULL(ll, d) \
93604df322SMasahiro Yamada 	({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
94604df322SMasahiro Yamada 
95604df322SMasahiro Yamada #define DIV_ROUND_UP_ULL(ll, d)		DIV_ROUND_DOWN_ULL((ll) + (d) - 1, (d))
9636a26c69SNicholas Bellinger 
9736a26c69SNicholas Bellinger #if BITS_PER_LONG == 32
9836a26c69SNicholas Bellinger # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d)
9936a26c69SNicholas Bellinger #else
10036a26c69SNicholas Bellinger # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d)
10136a26c69SNicholas Bellinger #endif
102074e61ecSJames Morris 
103cedc5b6aSKees Cook /**
104cedc5b6aSKees Cook  * roundup - round up to the next specified multiple
105cedc5b6aSKees Cook  * @x: the value to up
106cedc5b6aSKees Cook  * @y: multiple to round up to
107cedc5b6aSKees Cook  *
108cedc5b6aSKees Cook  * Rounds @x up to next multiple of @y. If @y will always be a power
109cedc5b6aSKees Cook  * of 2, consider using the faster round_up().
110cedc5b6aSKees Cook  */
111b28efd54SEric Paris #define roundup(x, y) (					\
112b28efd54SEric Paris {							\
113b95c4d18SRandy Dunlap 	typeof(y) __y = y;				\
114b28efd54SEric Paris 	(((x) + (__y - 1)) / __y) * __y;		\
115b28efd54SEric Paris }							\
116b28efd54SEric Paris )
117cedc5b6aSKees Cook /**
118cedc5b6aSKees Cook  * rounddown - round down to next specified multiple
119cedc5b6aSKees Cook  * @x: the value to round
120cedc5b6aSKees Cook  * @y: multiple to round down to
121cedc5b6aSKees Cook  *
122cedc5b6aSKees Cook  * Rounds @x down to next multiple of @y. If @y will always be a power
123cedc5b6aSKees Cook  * of 2, consider using the faster round_down().
124cedc5b6aSKees Cook  */
125686a0f3dSEric Paris #define rounddown(x, y) (				\
126686a0f3dSEric Paris {							\
127686a0f3dSEric Paris 	typeof(x) __x = (x);				\
128686a0f3dSEric Paris 	__x - (__x % (y));				\
129686a0f3dSEric Paris }							\
130686a0f3dSEric Paris )
131b6d86d3dSGuenter Roeck 
132b6d86d3dSGuenter Roeck /*
1334f5901f5SNiklas Söderlund  * Divide positive or negative dividend by positive or negative divisor
1344f5901f5SNiklas Söderlund  * and round to closest integer. Result is undefined for negative
135e8c97af0SRandy Dunlap  * divisors if the dividend variable type is unsigned and for negative
1364f5901f5SNiklas Söderlund  * dividends if the divisor variable type is unsigned.
137b6d86d3dSGuenter Roeck  */
1389fe06081SDarrick J. Wong #define DIV_ROUND_CLOSEST(x, divisor)(			\
1399fe06081SDarrick J. Wong {							\
140b6d86d3dSGuenter Roeck 	typeof(x) __x = x;				\
141b6d86d3dSGuenter Roeck 	typeof(divisor) __d = divisor;			\
142c4e18497SGuenter Roeck 	(((typeof(x))-1) > 0 ||				\
1434f5901f5SNiklas Söderlund 	 ((typeof(divisor))-1) > 0 ||			\
1444f5901f5SNiklas Söderlund 	 (((__x) > 0) == ((__d) > 0))) ?		\
145b6d86d3dSGuenter Roeck 		(((__x) + ((__d) / 2)) / (__d)) :	\
146b6d86d3dSGuenter Roeck 		(((__x) - ((__d) / 2)) / (__d));	\
1479fe06081SDarrick J. Wong }							\
1489fe06081SDarrick J. Wong )
149f766093eSJavi Merino /*
150f766093eSJavi Merino  * Same as above but for u64 dividends. divisor must be a 32-bit
151f766093eSJavi Merino  * number.
152f766093eSJavi Merino  */
153f766093eSJavi Merino #define DIV_ROUND_CLOSEST_ULL(x, divisor)(		\
154f766093eSJavi Merino {							\
155f766093eSJavi Merino 	typeof(divisor) __d = divisor;			\
156f766093eSJavi Merino 	unsigned long long _tmp = (x) + (__d) / 2;	\
157f766093eSJavi Merino 	do_div(_tmp, __d);				\
158f766093eSJavi Merino 	_tmp;						\
159f766093eSJavi Merino }							\
160f766093eSJavi Merino )
1611da177e4SLinus Torvalds 
1629993bc63SSalman Qazi /*
1639993bc63SSalman Qazi  * Multiplies an integer by a fraction, while avoiding unnecessary
1649993bc63SSalman Qazi  * overflow or loss of precision.
1659993bc63SSalman Qazi  */
1669993bc63SSalman Qazi #define mult_frac(x, numer, denom)(			\
1679993bc63SSalman Qazi {							\
1689993bc63SSalman Qazi 	typeof(x) quot = (x) / (denom);			\
1699993bc63SSalman Qazi 	typeof(x) rem  = (x) % (denom);			\
1709993bc63SSalman Qazi 	(quot * (numer)) + ((rem * (numer)) / (denom));	\
1719993bc63SSalman Qazi }							\
1729993bc63SSalman Qazi )
1739993bc63SSalman Qazi 
1749993bc63SSalman Qazi 
175ca31e146SEduard - Gabriel Munteanu #define _RET_IP_		(unsigned long)__builtin_return_address(0)
176ca31e146SEduard - Gabriel Munteanu #define _THIS_IP_  ({ __label__ __here; __here: (unsigned long)&&__here; })
177ca31e146SEduard - Gabriel Munteanu 
17890c699a9SBartlomiej Zolnierkiewicz #ifdef CONFIG_LBDAF
1792da96acdSJens Axboe # define sector_div(a, b) do_div(a, b)
1802da96acdSJens Axboe #else
1812da96acdSJens Axboe # define sector_div(n, b)( \
1822da96acdSJens Axboe { \
1832da96acdSJens Axboe 	int _res; \
1842da96acdSJens Axboe 	_res = (n) % (b); \
1852da96acdSJens Axboe 	(n) /= (b); \
1862da96acdSJens Axboe 	_res; \
1872da96acdSJens Axboe } \
1882da96acdSJens Axboe )
1892da96acdSJens Axboe #endif
1902da96acdSJens Axboe 
191218e180eSAndrew Morton /**
192218e180eSAndrew Morton  * upper_32_bits - return bits 32-63 of a number
193218e180eSAndrew Morton  * @n: the number we're accessing
194218e180eSAndrew Morton  *
195218e180eSAndrew Morton  * A basic shift-right of a 64- or 32-bit quantity.  Use this to suppress
196218e180eSAndrew Morton  * the "right shift count >= width of type" warning when that quantity is
197218e180eSAndrew Morton  * 32-bits.
198218e180eSAndrew Morton  */
199218e180eSAndrew Morton #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
200218e180eSAndrew Morton 
201204b885eSJoerg Roedel /**
202204b885eSJoerg Roedel  * lower_32_bits - return bits 0-31 of a number
203204b885eSJoerg Roedel  * @n: the number we're accessing
204204b885eSJoerg Roedel  */
205204b885eSJoerg Roedel #define lower_32_bits(n) ((u32)(n))
206204b885eSJoerg Roedel 
2071da177e4SLinus Torvalds struct completion;
208df2e71fbS[email protected] struct pt_regs;
209df2e71fbS[email protected] struct user;
2101da177e4SLinus Torvalds 
211070cb065SUwe Kleine-König #ifdef CONFIG_PREEMPT_VOLUNTARY
212070cb065SUwe Kleine-König extern int _cond_resched(void);
213070cb065SUwe Kleine-König # define might_resched() _cond_resched()
214070cb065SUwe Kleine-König #else
215070cb065SUwe Kleine-König # define might_resched() do { } while (0)
216070cb065SUwe Kleine-König #endif
217070cb065SUwe Kleine-König 
218d902db1eSFrederic Weisbecker #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
219568f1967SPeter Zijlstra extern void ___might_sleep(const char *file, int line, int preempt_offset);
220568f1967SPeter Zijlstra extern void __might_sleep(const char *file, int line, int preempt_offset);
221568f1967SPeter Zijlstra extern void __cant_sleep(const char *file, int line, int preempt_offset);
222568f1967SPeter Zijlstra 
2231da177e4SLinus Torvalds /**
2241da177e4SLinus Torvalds  * might_sleep - annotation for functions that can sleep
2251da177e4SLinus Torvalds  *
2261da177e4SLinus Torvalds  * this macro will print a stack trace if it is executed in an atomic
2271da177e4SLinus Torvalds  * context (spinlock, irq-handler, ...).
2281da177e4SLinus Torvalds  *
2291da177e4SLinus Torvalds  * This is a useful debugging help to be able to catch problems early and not
230e20ec991SJim Cromie  * be bitten later when the calling function happens to sleep when it is not
2311da177e4SLinus Torvalds  * supposed to.
2321da177e4SLinus Torvalds  */
233f8cbd99bSIngo Molnar # define might_sleep() \
234e4aafea2SFrederic Weisbecker 	do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
235568f1967SPeter Zijlstra /**
236568f1967SPeter Zijlstra  * cant_sleep - annotation for functions that cannot sleep
237568f1967SPeter Zijlstra  *
238568f1967SPeter Zijlstra  * this macro will print a stack trace if it is executed with preemption enabled
239568f1967SPeter Zijlstra  */
240568f1967SPeter Zijlstra # define cant_sleep() \
241568f1967SPeter Zijlstra 	do { __cant_sleep(__FILE__, __LINE__, 0); } while (0)
24200845eb9SLinus Torvalds # define sched_annotate_sleep()	(current->task_state_change = 0)
243f8cbd99bSIngo Molnar #else
2443427445aSPeter Zijlstra   static inline void ___might_sleep(const char *file, int line,
2453427445aSPeter Zijlstra 				   int preempt_offset) { }
246d894837fSSimon Kagstrom   static inline void __might_sleep(const char *file, int line,
247d894837fSSimon Kagstrom 				   int preempt_offset) { }
248f8cbd99bSIngo Molnar # define might_sleep() do { might_resched(); } while (0)
249568f1967SPeter Zijlstra # define cant_sleep() do { } while (0)
2501029a2b5SPeter Zijlstra # define sched_annotate_sleep() do { } while (0)
251f8cbd99bSIngo Molnar #endif
252f8cbd99bSIngo Molnar 
253368a5fa1SHua Zhong #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
254f8cbd99bSIngo Molnar 
255c8299cb6SMichal Nazarewicz /**
256c8299cb6SMichal Nazarewicz  * abs - return absolute value of an argument
2578f57e4d9SMichal Nazarewicz  * @x: the value.  If it is unsigned type, it is converted to signed type first.
2588f57e4d9SMichal Nazarewicz  *     char is treated as if it was signed (regardless of whether it really is)
2598f57e4d9SMichal Nazarewicz  *     but the macro's return type is preserved as char.
260c8299cb6SMichal Nazarewicz  *
2618f57e4d9SMichal Nazarewicz  * Return: an absolute value of x.
26271a90484SAndrew Morton  */
2638f57e4d9SMichal Nazarewicz #define abs(x)	__abs_choose_expr(x, long long,				\
2648f57e4d9SMichal Nazarewicz 		__abs_choose_expr(x, long,				\
2658f57e4d9SMichal Nazarewicz 		__abs_choose_expr(x, int,				\
2668f57e4d9SMichal Nazarewicz 		__abs_choose_expr(x, short,				\
2678f57e4d9SMichal Nazarewicz 		__abs_choose_expr(x, char,				\
2688f57e4d9SMichal Nazarewicz 		__builtin_choose_expr(					\
2698f57e4d9SMichal Nazarewicz 			__builtin_types_compatible_p(typeof(x), char),	\
2708f57e4d9SMichal Nazarewicz 			(char)({ signed char __x = (x); __x<0?-__x:__x; }), \
2718f57e4d9SMichal Nazarewicz 			((void)0)))))))
2728f57e4d9SMichal Nazarewicz 
2738f57e4d9SMichal Nazarewicz #define __abs_choose_expr(x, type, other) __builtin_choose_expr(	\
2748f57e4d9SMichal Nazarewicz 	__builtin_types_compatible_p(typeof(x),   signed type) ||	\
2758f57e4d9SMichal Nazarewicz 	__builtin_types_compatible_p(typeof(x), unsigned type),		\
2768f57e4d9SMichal Nazarewicz 	({ signed type __x = (x); __x < 0 ? -__x : __x; }), other)
2771da177e4SLinus Torvalds 
27889770b0aSDaniel Borkmann /**
27989770b0aSDaniel Borkmann  * reciprocal_scale - "scale" a value into range [0, ep_ro)
28089770b0aSDaniel Borkmann  * @val: value
28189770b0aSDaniel Borkmann  * @ep_ro: right open interval endpoint
28289770b0aSDaniel Borkmann  *
28389770b0aSDaniel Borkmann  * Perform a "reciprocal multiplication" in order to "scale" a value into
284e8c97af0SRandy Dunlap  * range [0, @ep_ro), where the upper interval endpoint is right-open.
28589770b0aSDaniel Borkmann  * This is useful, e.g. for accessing a index of an array containing
286e8c97af0SRandy Dunlap  * @ep_ro elements, for example. Think of it as sort of modulus, only that
28789770b0aSDaniel Borkmann  * the result isn't that of modulo. ;) Note that if initial input is a
28889770b0aSDaniel Borkmann  * small value, then result will return 0.
28989770b0aSDaniel Borkmann  *
290e8c97af0SRandy Dunlap  * Return: a result based on @val in interval [0, @ep_ro).
29189770b0aSDaniel Borkmann  */
29289770b0aSDaniel Borkmann static inline u32 reciprocal_scale(u32 val, u32 ep_ro)
29389770b0aSDaniel Borkmann {
29489770b0aSDaniel Borkmann 	return (u32)(((u64) val * ep_ro) >> 32);
29589770b0aSDaniel Borkmann }
29689770b0aSDaniel Borkmann 
297386e7906SAxel Lin #if defined(CONFIG_MMU) && \
298386e7906SAxel Lin 	(defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP))
2999ec23531SDavid Hildenbrand #define might_fault() __might_fault(__FILE__, __LINE__)
3009ec23531SDavid Hildenbrand void __might_fault(const char *file, int line);
3013ee1afa3SNick Piggin #else
302662bbcb2SMichael S. Tsirkin static inline void might_fault(void) { }
3033ee1afa3SNick Piggin #endif
3043ee1afa3SNick Piggin 
305e041c683SAlan Stern extern struct atomic_notifier_head panic_notifier_list;
306c7ff0d9cSTAMUKI Shoichi extern long (*panic_blink)(int state);
3079402c95fSJoe Perches __printf(1, 2)
3089af6528eSPeter Zijlstra void panic(const char *fmt, ...) __noreturn __cold;
309ebc41f20SHidehiro Kawai void nmi_panic(struct pt_regs *regs, const char *msg);
310dd287796SAndrew Morton extern void oops_enter(void);
311dd287796SAndrew Morton extern void oops_exit(void);
312863a6049SAnton Blanchard void print_oops_end_marker(void);
313dd287796SAndrew Morton extern int oops_may_print(void);
3149af6528eSPeter Zijlstra void do_exit(long error_code) __noreturn;
3159af6528eSPeter Zijlstra void complete_and_exit(struct completion *, long) __noreturn;
31633ee3b2eSAlexey Dobriyan 
3177a46ec0eSKees Cook #ifdef CONFIG_ARCH_HAS_REFCOUNT
3187a46ec0eSKees Cook void refcount_error_report(struct pt_regs *regs, const char *err);
3197a46ec0eSKees Cook #else
3207a46ec0eSKees Cook static inline void refcount_error_report(struct pt_regs *regs, const char *err)
3217a46ec0eSKees Cook { }
3227a46ec0eSKees Cook #endif
3237a46ec0eSKees Cook 
32433ee3b2eSAlexey Dobriyan /* Internal, do not use. */
32533ee3b2eSAlexey Dobriyan int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res);
32633ee3b2eSAlexey Dobriyan int __must_check _kstrtol(const char *s, unsigned int base, long *res);
32733ee3b2eSAlexey Dobriyan 
32833ee3b2eSAlexey Dobriyan int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res);
32933ee3b2eSAlexey Dobriyan int __must_check kstrtoll(const char *s, unsigned int base, long long *res);
3304c925d60SEldad Zack 
3314c925d60SEldad Zack /**
3324c925d60SEldad Zack  * kstrtoul - convert a string to an unsigned long
3334c925d60SEldad Zack  * @s: The start of the string. The string must be null-terminated, and may also
3344c925d60SEldad Zack  *  include a single newline before its terminating null. The first character
3354c925d60SEldad Zack  *  may also be a plus sign, but not a minus sign.
3364c925d60SEldad Zack  * @base: The number base to use. The maximum supported base is 16. If base is
3374c925d60SEldad Zack  *  given as 0, then the base of the string is automatically detected with the
3384c925d60SEldad Zack  *  conventional semantics - If it begins with 0x the number will be parsed as a
3394c925d60SEldad Zack  *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
3404c925d60SEldad Zack  *  parsed as an octal number. Otherwise it will be parsed as a decimal.
3414c925d60SEldad Zack  * @res: Where to write the result of the conversion on success.
3424c925d60SEldad Zack  *
3434c925d60SEldad Zack  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
3444c925d60SEldad Zack  * Used as a replacement for the obsolete simple_strtoull. Return code must
3454c925d60SEldad Zack  * be checked.
3464c925d60SEldad Zack */
34733ee3b2eSAlexey Dobriyan static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res)
34833ee3b2eSAlexey Dobriyan {
34933ee3b2eSAlexey Dobriyan 	/*
35033ee3b2eSAlexey Dobriyan 	 * We want to shortcut function call, but
35133ee3b2eSAlexey Dobriyan 	 * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0.
35233ee3b2eSAlexey Dobriyan 	 */
35333ee3b2eSAlexey Dobriyan 	if (sizeof(unsigned long) == sizeof(unsigned long long) &&
35433ee3b2eSAlexey Dobriyan 	    __alignof__(unsigned long) == __alignof__(unsigned long long))
35533ee3b2eSAlexey Dobriyan 		return kstrtoull(s, base, (unsigned long long *)res);
35633ee3b2eSAlexey Dobriyan 	else
35733ee3b2eSAlexey Dobriyan 		return _kstrtoul(s, base, res);
35833ee3b2eSAlexey Dobriyan }
35933ee3b2eSAlexey Dobriyan 
3604c925d60SEldad Zack /**
3614c925d60SEldad Zack  * kstrtol - convert a string to a long
3624c925d60SEldad Zack  * @s: The start of the string. The string must be null-terminated, and may also
3634c925d60SEldad Zack  *  include a single newline before its terminating null. The first character
3644c925d60SEldad Zack  *  may also be a plus sign or a minus sign.
3654c925d60SEldad Zack  * @base: The number base to use. The maximum supported base is 16. If base is
3664c925d60SEldad Zack  *  given as 0, then the base of the string is automatically detected with the
3674c925d60SEldad Zack  *  conventional semantics - If it begins with 0x the number will be parsed as a
3684c925d60SEldad Zack  *  hexadecimal (case insensitive), if it otherwise begins with 0, it will be
3694c925d60SEldad Zack  *  parsed as an octal number. Otherwise it will be parsed as a decimal.
3704c925d60SEldad Zack  * @res: Where to write the result of the conversion on success.
3714c925d60SEldad Zack  *
3724c925d60SEldad Zack  * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
3734c925d60SEldad Zack  * Used as a replacement for the obsolete simple_strtoull. Return code must
3744c925d60SEldad Zack  * be checked.
3754c925d60SEldad Zack  */
37633ee3b2eSAlexey Dobriyan static inline int __must_check kstrtol(const char *s, unsigned int base, long *res)
37733ee3b2eSAlexey Dobriyan {
37833ee3b2eSAlexey Dobriyan 	/*
37933ee3b2eSAlexey Dobriyan 	 * We want to shortcut function call, but
38033ee3b2eSAlexey Dobriyan 	 * __builtin_types_compatible_p(long, long long) = 0.
38133ee3b2eSAlexey Dobriyan 	 */
38233ee3b2eSAlexey Dobriyan 	if (sizeof(long) == sizeof(long long) &&
38333ee3b2eSAlexey Dobriyan 	    __alignof__(long) == __alignof__(long long))
38433ee3b2eSAlexey Dobriyan 		return kstrtoll(s, base, (long long *)res);
38533ee3b2eSAlexey Dobriyan 	else
38633ee3b2eSAlexey Dobriyan 		return _kstrtol(s, base, res);
38733ee3b2eSAlexey Dobriyan }
38833ee3b2eSAlexey Dobriyan 
38933ee3b2eSAlexey Dobriyan int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res);
39033ee3b2eSAlexey Dobriyan int __must_check kstrtoint(const char *s, unsigned int base, int *res);
39133ee3b2eSAlexey Dobriyan 
39233ee3b2eSAlexey Dobriyan static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res)
39333ee3b2eSAlexey Dobriyan {
39433ee3b2eSAlexey Dobriyan 	return kstrtoull(s, base, res);
39533ee3b2eSAlexey Dobriyan }
39633ee3b2eSAlexey Dobriyan 
39733ee3b2eSAlexey Dobriyan static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res)
39833ee3b2eSAlexey Dobriyan {
39933ee3b2eSAlexey Dobriyan 	return kstrtoll(s, base, res);
40033ee3b2eSAlexey Dobriyan }
40133ee3b2eSAlexey Dobriyan 
40233ee3b2eSAlexey Dobriyan static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res)
40333ee3b2eSAlexey Dobriyan {
40433ee3b2eSAlexey Dobriyan 	return kstrtouint(s, base, res);
40533ee3b2eSAlexey Dobriyan }
40633ee3b2eSAlexey Dobriyan 
40733ee3b2eSAlexey Dobriyan static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res)
40833ee3b2eSAlexey Dobriyan {
40933ee3b2eSAlexey Dobriyan 	return kstrtoint(s, base, res);
41033ee3b2eSAlexey Dobriyan }
41133ee3b2eSAlexey Dobriyan 
41233ee3b2eSAlexey Dobriyan int __must_check kstrtou16(const char *s, unsigned int base, u16 *res);
41333ee3b2eSAlexey Dobriyan int __must_check kstrtos16(const char *s, unsigned int base, s16 *res);
41433ee3b2eSAlexey Dobriyan int __must_check kstrtou8(const char *s, unsigned int base, u8 *res);
41533ee3b2eSAlexey Dobriyan int __must_check kstrtos8(const char *s, unsigned int base, s8 *res);
416ef951599SKees Cook int __must_check kstrtobool(const char *s, bool *res);
41733ee3b2eSAlexey Dobriyan 
418c196e32aSAlexey Dobriyan int __must_check kstrtoull_from_user(const char __user *s, size_t count, unsigned int base, unsigned long long *res);
419c196e32aSAlexey Dobriyan int __must_check kstrtoll_from_user(const char __user *s, size_t count, unsigned int base, long long *res);
420c196e32aSAlexey Dobriyan int __must_check kstrtoul_from_user(const char __user *s, size_t count, unsigned int base, unsigned long *res);
421c196e32aSAlexey Dobriyan int __must_check kstrtol_from_user(const char __user *s, size_t count, unsigned int base, long *res);
422c196e32aSAlexey Dobriyan int __must_check kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, unsigned int *res);
423c196e32aSAlexey Dobriyan int __must_check kstrtoint_from_user(const char __user *s, size_t count, unsigned int base, int *res);
424c196e32aSAlexey Dobriyan int __must_check kstrtou16_from_user(const char __user *s, size_t count, unsigned int base, u16 *res);
425c196e32aSAlexey Dobriyan int __must_check kstrtos16_from_user(const char __user *s, size_t count, unsigned int base, s16 *res);
426c196e32aSAlexey Dobriyan int __must_check kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, u8 *res);
427c196e32aSAlexey Dobriyan int __must_check kstrtos8_from_user(const char __user *s, size_t count, unsigned int base, s8 *res);
428ef951599SKees Cook int __must_check kstrtobool_from_user(const char __user *s, size_t count, bool *res);
429c196e32aSAlexey Dobriyan 
430c196e32aSAlexey Dobriyan static inline int __must_check kstrtou64_from_user(const char __user *s, size_t count, unsigned int base, u64 *res)
431c196e32aSAlexey Dobriyan {
432c196e32aSAlexey Dobriyan 	return kstrtoull_from_user(s, count, base, res);
433c196e32aSAlexey Dobriyan }
434c196e32aSAlexey Dobriyan 
435c196e32aSAlexey Dobriyan static inline int __must_check kstrtos64_from_user(const char __user *s, size_t count, unsigned int base, s64 *res)
436c196e32aSAlexey Dobriyan {
437c196e32aSAlexey Dobriyan 	return kstrtoll_from_user(s, count, base, res);
438c196e32aSAlexey Dobriyan }
439c196e32aSAlexey Dobriyan 
440c196e32aSAlexey Dobriyan static inline int __must_check kstrtou32_from_user(const char __user *s, size_t count, unsigned int base, u32 *res)
441c196e32aSAlexey Dobriyan {
442c196e32aSAlexey Dobriyan 	return kstrtouint_from_user(s, count, base, res);
443c196e32aSAlexey Dobriyan }
444c196e32aSAlexey Dobriyan 
445c196e32aSAlexey Dobriyan static inline int __must_check kstrtos32_from_user(const char __user *s, size_t count, unsigned int base, s32 *res)
446c196e32aSAlexey Dobriyan {
447c196e32aSAlexey Dobriyan 	return kstrtoint_from_user(s, count, base, res);
448c196e32aSAlexey Dobriyan }
449c196e32aSAlexey Dobriyan 
45067d0a075SJoe Perches /* Obsolete, do not use.  Use kstrto<foo> instead */
45167d0a075SJoe Perches 
4521da177e4SLinus Torvalds extern unsigned long simple_strtoul(const char *,char **,unsigned int);
4531da177e4SLinus Torvalds extern long simple_strtol(const char *,char **,unsigned int);
4541da177e4SLinus Torvalds extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
4551da177e4SLinus Torvalds extern long long simple_strtoll(const char *,char **,unsigned int);
45633ee3b2eSAlexey Dobriyan 
457d1be35cbSAndrei Vagin extern int num_to_str(char *buf, int size,
458d1be35cbSAndrei Vagin 		      unsigned long long num, unsigned int width);
4591ac101a5SKAMEZAWA Hiroyuki 
46067d0a075SJoe Perches /* lib/printf utilities */
46167d0a075SJoe Perches 
462b9075fa9SJoe Perches extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...);
463b9075fa9SJoe Perches extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list);
464b9075fa9SJoe Perches extern __printf(3, 4)
465b9075fa9SJoe Perches int snprintf(char *buf, size_t size, const char *fmt, ...);
466b9075fa9SJoe Perches extern __printf(3, 0)
467b9075fa9SJoe Perches int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
468b9075fa9SJoe Perches extern __printf(3, 4)
469b9075fa9SJoe Perches int scnprintf(char *buf, size_t size, const char *fmt, ...);
470b9075fa9SJoe Perches extern __printf(3, 0)
471b9075fa9SJoe Perches int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
47248a27055SRasmus Villemoes extern __printf(2, 3) __malloc
473b9075fa9SJoe Perches char *kasprintf(gfp_t gfp, const char *fmt, ...);
47448a27055SRasmus Villemoes extern __printf(2, 0) __malloc
4758db14860SNicolas Iooss char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
4760a9df786SRasmus Villemoes extern __printf(2, 0)
4770a9df786SRasmus Villemoes const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args);
4781da177e4SLinus Torvalds 
4796061d949SJoe Perches extern __scanf(2, 3)
4806061d949SJoe Perches int sscanf(const char *, const char *, ...);
4816061d949SJoe Perches extern __scanf(2, 0)
4826061d949SJoe Perches int vsscanf(const char *, const char *, va_list);
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds extern int get_option(char **str, int *pint);
4851da177e4SLinus Torvalds extern char *get_options(const char *str, int nints, int *ints);
486d974ae37SJeremy Fitzhardinge extern unsigned long long memparse(const char *ptr, char **retptr);
4876ccc72b8SDave Young extern bool parse_option_str(const char *str, const char *option);
488f51b17c8SBaoquan He extern char *next_arg(char *args, char **param, char **val);
4891da177e4SLinus Torvalds 
4905e376613STrent Piepho extern int core_kernel_text(unsigned long addr);
4919fbcc57aSJosh Poimboeuf extern int init_kernel_text(unsigned long addr);
492cdbe61bfSSteven Rostedt extern int core_kernel_data(unsigned long addr);
4931da177e4SLinus Torvalds extern int __kernel_text_address(unsigned long addr);
4941da177e4SLinus Torvalds extern int kernel_text_address(unsigned long addr);
495ab7476cfSArjan van de Ven extern int func_ptr_is_kernel_text(void *ptr);
496ab7476cfSArjan van de Ven 
4971da177e4SLinus Torvalds unsigned long int_sqrt(unsigned long);
4981da177e4SLinus Torvalds 
49947a36163SCrt Mori #if BITS_PER_LONG < 64
50047a36163SCrt Mori u32 int_sqrt64(u64 x);
50147a36163SCrt Mori #else
50247a36163SCrt Mori static inline u32 int_sqrt64(u64 x)
50347a36163SCrt Mori {
50447a36163SCrt Mori 	return (u32)int_sqrt(x);
50547a36163SCrt Mori }
50647a36163SCrt Mori #endif
50747a36163SCrt Mori 
5081da177e4SLinus Torvalds extern void bust_spinlocks(int yes);
5091da177e4SLinus Torvalds extern int oops_in_progress;		/* If set, an oops, panic(), BUG() or die() is in progress */
510aa727107SAdrian Bunk extern int panic_timeout;
51181c9d43fSFeng Tang extern unsigned long panic_print;
5121da177e4SLinus Torvalds extern int panic_on_oops;
5138da5addaSDon Zickus extern int panic_on_unrecovered_nmi;
5145211a242SKurt Garloff extern int panic_on_io_nmi;
5159e3961a0SPrarit Bhargava extern int panic_on_warn;
516088e9d25SDaniel Bristot de Oliveira extern int sysctl_panic_on_rcu_stall;
51755af7796SMitsuo Hayasaka extern int sysctl_panic_on_stackoverflow;
5185375b708SHATAYAMA Daisuke 
5195375b708SHATAYAMA Daisuke extern bool crash_kexec_post_notifiers;
5205375b708SHATAYAMA Daisuke 
5215800dc3cSJason Baron /*
5221717f209SHidehiro Kawai  * panic_cpu is used for synchronizing panic() and crash_kexec() execution. It
5231717f209SHidehiro Kawai  * holds a CPU number which is executing panic() currently. A value of
5241717f209SHidehiro Kawai  * PANIC_CPU_INVALID means no CPU has entered panic() or crash_kexec().
5251717f209SHidehiro Kawai  */
5261717f209SHidehiro Kawai extern atomic_t panic_cpu;
5271717f209SHidehiro Kawai #define PANIC_CPU_INVALID	-1
5281717f209SHidehiro Kawai 
5291717f209SHidehiro Kawai /*
5305800dc3cSJason Baron  * Only to be used by arch init code. If the user over-wrote the default
5315800dc3cSJason Baron  * CONFIG_PANIC_TIMEOUT, honor it.
5325800dc3cSJason Baron  */
5335800dc3cSJason Baron static inline void set_arch_panic_timeout(int timeout, int arch_default_timeout)
5345800dc3cSJason Baron {
5355800dc3cSJason Baron 	if (panic_timeout == arch_default_timeout)
5365800dc3cSJason Baron 		panic_timeout = timeout;
5375800dc3cSJason Baron }
5381da177e4SLinus Torvalds extern const char *print_tainted(void);
539373d4d09SRusty Russell enum lockdep_ok {
540373d4d09SRusty Russell 	LOCKDEP_STILL_OK,
541373d4d09SRusty Russell 	LOCKDEP_NOW_UNRELIABLE
542373d4d09SRusty Russell };
543373d4d09SRusty Russell extern void add_taint(unsigned flag, enum lockdep_ok);
54425ddbb18SAndi Kleen extern int test_taint(unsigned flag);
54525ddbb18SAndi Kleen extern unsigned long get_taint(void);
546b920de1bSDavid Howells extern int root_mountflags;
5471da177e4SLinus Torvalds 
5482ce802f6STejun Heo extern bool early_boot_irqs_disabled;
5492ce802f6STejun Heo 
55069a78ff2SThomas Gleixner /*
55169a78ff2SThomas Gleixner  * Values used for system_state. Ordering of the states must not be changed
55269a78ff2SThomas Gleixner  * as code checks for <, <=, >, >= STATE.
55369a78ff2SThomas Gleixner  */
5541da177e4SLinus Torvalds extern enum system_states {
5551da177e4SLinus Torvalds 	SYSTEM_BOOTING,
55669a78ff2SThomas Gleixner 	SYSTEM_SCHEDULING,
5571da177e4SLinus Torvalds 	SYSTEM_RUNNING,
5581da177e4SLinus Torvalds 	SYSTEM_HALT,
5591da177e4SLinus Torvalds 	SYSTEM_POWER_OFF,
5601da177e4SLinus Torvalds 	SYSTEM_RESTART,
561c1a957d1SThomas Gleixner 	SYSTEM_SUSPEND,
5621da177e4SLinus Torvalds } system_state;
5631da177e4SLinus Torvalds 
56447d4b263SKees Cook /* This cannot be an enum because some may be used in assembly source. */
56525ddbb18SAndi Kleen #define TAINT_PROPRIETARY_MODULE	0
56625ddbb18SAndi Kleen #define TAINT_FORCED_MODULE		1
5678c90487cSDave Jones #define TAINT_CPU_OUT_OF_SPEC		2
56825ddbb18SAndi Kleen #define TAINT_FORCED_RMMOD		3
56925ddbb18SAndi Kleen #define TAINT_MACHINE_CHECK		4
57025ddbb18SAndi Kleen #define TAINT_BAD_PAGE			5
57125ddbb18SAndi Kleen #define TAINT_USER			6
57225ddbb18SAndi Kleen #define TAINT_DIE			7
57325ddbb18SAndi Kleen #define TAINT_OVERRIDDEN_ACPI_TABLE	8
57425ddbb18SAndi Kleen #define TAINT_WARN			9
57526e9a397SLinus Torvalds #define TAINT_CRAP			10
57692946bc7SBen Hutchings #define TAINT_FIRMWARE_WORKAROUND	11
5772449b8baSBen Hutchings #define TAINT_OOT_MODULE		12
57866cc69e3SMathieu Desnoyers #define TAINT_UNSIGNED_MODULE		13
57969361eefSJosh Hunt #define TAINT_SOFTLOCKUP		14
580c5f45465SSeth Jennings #define TAINT_LIVEPATCH			15
5814efb442cSBorislav Petkov #define TAINT_AUX			16
582bc4f2f54SKees Cook #define TAINT_RANDSTRUCT		17
583bc4f2f54SKees Cook #define TAINT_FLAGS_COUNT		18
5847fd8329bSPetr Mladek 
5857fd8329bSPetr Mladek struct taint_flag {
5865eb7c0d0SLarry Finger 	char c_true;	/* character printed when tainted */
5875eb7c0d0SLarry Finger 	char c_false;	/* character printed when not tainted */
5887fd8329bSPetr Mladek 	bool module;	/* also show as a per-module taint flag */
5897fd8329bSPetr Mladek };
5907fd8329bSPetr Mladek 
5917fd8329bSPetr Mladek extern const struct taint_flag taint_flags[TAINT_FLAGS_COUNT];
5921da177e4SLinus Torvalds 
5933fc95772SHarvey Harrison extern const char hex_asc[];
5943fc95772SHarvey Harrison #define hex_asc_lo(x)	hex_asc[((x) & 0x0f)]
5953fc95772SHarvey Harrison #define hex_asc_hi(x)	hex_asc[((x) & 0xf0) >> 4]
5963fc95772SHarvey Harrison 
59755036ba7SAndy Shevchenko static inline char *hex_byte_pack(char *buf, u8 byte)
5983fc95772SHarvey Harrison {
5993fc95772SHarvey Harrison 	*buf++ = hex_asc_hi(byte);
6003fc95772SHarvey Harrison 	*buf++ = hex_asc_lo(byte);
6013fc95772SHarvey Harrison 	return buf;
6023fc95772SHarvey Harrison }
60399eaf3c4SRandy Dunlap 
604c26d436cSAndre Naujoks extern const char hex_asc_upper[];
605c26d436cSAndre Naujoks #define hex_asc_upper_lo(x)	hex_asc_upper[((x) & 0x0f)]
606c26d436cSAndre Naujoks #define hex_asc_upper_hi(x)	hex_asc_upper[((x) & 0xf0) >> 4]
607c26d436cSAndre Naujoks 
608c26d436cSAndre Naujoks static inline char *hex_byte_pack_upper(char *buf, u8 byte)
609c26d436cSAndre Naujoks {
610c26d436cSAndre Naujoks 	*buf++ = hex_asc_upper_hi(byte);
611c26d436cSAndre Naujoks 	*buf++ = hex_asc_upper_lo(byte);
612c26d436cSAndre Naujoks 	return buf;
613c26d436cSAndre Naujoks }
614c26d436cSAndre Naujoks 
61590378889SAndy Shevchenko extern int hex_to_bin(char ch);
616b7804983SMimi Zohar extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);
61753d91c5cSDavid Howells extern char *bin2hex(char *dst, const void *src, size_t count);
61890378889SAndy Shevchenko 
619a69f5edbSJoe Perches bool mac_pton(const char *s, u8 *mac);
6204cd5773aSAndy Shevchenko 
6218a64f336SJoe Perches /*
622526211bcSIngo Molnar  * General tracing related utility functions - trace_printk(),
6232002c258SSteven Rostedt  * tracing_on/tracing_off and tracing_start()/tracing_stop
6242002c258SSteven Rostedt  *
6252002c258SSteven Rostedt  * Use tracing_on/tracing_off when you want to quickly turn on or off
6262002c258SSteven Rostedt  * tracing. It simply enables or disables the recording of the trace events.
627156f5a78SGeunSik Lim  * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on
6282002c258SSteven Rostedt  * file, which gives a means for the kernel and userspace to interact.
6292002c258SSteven Rostedt  * Place a tracing_off() in the kernel where you want tracing to end.
6302002c258SSteven Rostedt  * From user space, examine the trace, and then echo 1 > tracing_on
6312002c258SSteven Rostedt  * to continue tracing.
6322002c258SSteven Rostedt  *
6332002c258SSteven Rostedt  * tracing_stop/tracing_start has slightly more overhead. It is used
6342002c258SSteven Rostedt  * by things like suspend to ram where disabling the recording of the
6352002c258SSteven Rostedt  * trace is not enough, but tracing must actually stop because things
6362002c258SSteven Rostedt  * like calling smp_processor_id() may crash the system.
6372002c258SSteven Rostedt  *
6382002c258SSteven Rostedt  * Most likely, you want to use tracing_on/tracing_off.
639526211bcSIngo Molnar  */
640cecbca96SFrederic Weisbecker 
641cecbca96SFrederic Weisbecker enum ftrace_dump_mode {
642cecbca96SFrederic Weisbecker 	DUMP_NONE,
643cecbca96SFrederic Weisbecker 	DUMP_ALL,
644cecbca96SFrederic Weisbecker 	DUMP_ORIG,
645cecbca96SFrederic Weisbecker };
646cecbca96SFrederic Weisbecker 
647526211bcSIngo Molnar #ifdef CONFIG_TRACING
64893d68e52SSteven Rostedt void tracing_on(void);
64993d68e52SSteven Rostedt void tracing_off(void);
65093d68e52SSteven Rostedt int tracing_is_on(void);
651ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot(void);
652ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot_alloc(void);
65393d68e52SSteven Rostedt 
654526211bcSIngo Molnar extern void tracing_start(void);
655526211bcSIngo Molnar extern void tracing_stop(void);
656526211bcSIngo Molnar 
657b9075fa9SJoe Perches static inline __printf(1, 2)
658b9075fa9SJoe Perches void ____trace_printk_check_format(const char *fmt, ...)
659769b0441SFrederic Weisbecker {
660769b0441SFrederic Weisbecker }
661769b0441SFrederic Weisbecker #define __trace_printk_check_format(fmt, args...)			\
662769b0441SFrederic Weisbecker do {									\
663769b0441SFrederic Weisbecker 	if (0)								\
664769b0441SFrederic Weisbecker 		____trace_printk_check_format(fmt, ##args);		\
665769b0441SFrederic Weisbecker } while (0)
666769b0441SFrederic Weisbecker 
667526211bcSIngo Molnar /**
668526211bcSIngo Molnar  * trace_printk - printf formatting in the ftrace buffer
669526211bcSIngo Molnar  * @fmt: the printf format for printing
670526211bcSIngo Molnar  *
671e8c97af0SRandy Dunlap  * Note: __trace_printk is an internal function for trace_printk() and
672e8c97af0SRandy Dunlap  *       the @ip is passed in via the trace_printk() macro.
673526211bcSIngo Molnar  *
674526211bcSIngo Molnar  * This function allows a kernel developer to debug fast path sections
675526211bcSIngo Molnar  * that printk is not appropriate for. By scattering in various
676526211bcSIngo Molnar  * printk like tracing in the code, a developer can quickly see
677526211bcSIngo Molnar  * where problems are occurring.
678526211bcSIngo Molnar  *
679526211bcSIngo Molnar  * This is intended as a debugging tool for the developer only.
680526211bcSIngo Molnar  * Please refrain from leaving trace_printks scattered around in
68109ae7234SSteven Rostedt (Red Hat)  * your code. (Extra memory is used for special buffers that are
682e8c97af0SRandy Dunlap  * allocated when trace_printk() is used.)
6839d3c752cSSteven Rostedt (Red Hat)  *
6848730662dSWei Wang  * A little optimization trick is done here. If there's only one
6859d3c752cSSteven Rostedt (Red Hat)  * argument, there's no need to scan the string for printf formats.
6869d3c752cSSteven Rostedt (Red Hat)  * The trace_puts() will suffice. But how can we take advantage of
6879d3c752cSSteven Rostedt (Red Hat)  * using trace_puts() when trace_printk() has only one argument?
6889d3c752cSSteven Rostedt (Red Hat)  * By stringifying the args and checking the size we can tell
6899d3c752cSSteven Rostedt (Red Hat)  * whether or not there are args. __stringify((__VA_ARGS__)) will
6909d3c752cSSteven Rostedt (Red Hat)  * turn into "()\0" with a size of 3 when there are no args, anything
6919d3c752cSSteven Rostedt (Red Hat)  * else will be bigger. All we need to do is define a string to this,
6929d3c752cSSteven Rostedt (Red Hat)  * and then take its size and compare to 3. If it's bigger, use
6939d3c752cSSteven Rostedt (Red Hat)  * do_trace_printk() otherwise, optimize it to trace_puts(). Then just
6949d3c752cSSteven Rostedt (Red Hat)  * let gcc optimize the rest.
695526211bcSIngo Molnar  */
696769b0441SFrederic Weisbecker 
6979d3c752cSSteven Rostedt (Red Hat) #define trace_printk(fmt, ...)				\
6989d3c752cSSteven Rostedt (Red Hat) do {							\
6999d3c752cSSteven Rostedt (Red Hat) 	char _______STR[] = __stringify((__VA_ARGS__));	\
7009d3c752cSSteven Rostedt (Red Hat) 	if (sizeof(_______STR) > 3)			\
7019d3c752cSSteven Rostedt (Red Hat) 		do_trace_printk(fmt, ##__VA_ARGS__);	\
7029d3c752cSSteven Rostedt (Red Hat) 	else						\
7039d3c752cSSteven Rostedt (Red Hat) 		trace_puts(fmt);			\
7049d3c752cSSteven Rostedt (Red Hat) } while (0)
7059d3c752cSSteven Rostedt (Red Hat) 
7069d3c752cSSteven Rostedt (Red Hat) #define do_trace_printk(fmt, args...)					\
707769b0441SFrederic Weisbecker do {									\
7083debb0a9SSteven Rostedt (Red Hat) 	static const char *trace_printk_fmt __used			\
70948ead020SFrederic Weisbecker 		__attribute__((section("__trace_printk_fmt"))) =	\
71048ead020SFrederic Weisbecker 		__builtin_constant_p(fmt) ? fmt : NULL;			\
71148ead020SFrederic Weisbecker 									\
71207d777feSSteven Rostedt 	__trace_printk_check_format(fmt, ##args);			\
71307d777feSSteven Rostedt 									\
71407d777feSSteven Rostedt 	if (__builtin_constant_p(fmt))					\
71548ead020SFrederic Weisbecker 		__trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args);	\
71607d777feSSteven Rostedt 	else								\
71748ead020SFrederic Weisbecker 		__trace_printk(_THIS_IP_, fmt, ##args);			\
718769b0441SFrederic Weisbecker } while (0)
719769b0441SFrederic Weisbecker 
720b9075fa9SJoe Perches extern __printf(2, 3)
721b9075fa9SJoe Perches int __trace_bprintk(unsigned long ip, const char *fmt, ...);
72248ead020SFrederic Weisbecker 
723b9075fa9SJoe Perches extern __printf(2, 3)
724b9075fa9SJoe Perches int __trace_printk(unsigned long ip, const char *fmt, ...);
725769b0441SFrederic Weisbecker 
72609ae7234SSteven Rostedt (Red Hat) /**
72709ae7234SSteven Rostedt (Red Hat)  * trace_puts - write a string into the ftrace buffer
72809ae7234SSteven Rostedt (Red Hat)  * @str: the string to record
72909ae7234SSteven Rostedt (Red Hat)  *
73009ae7234SSteven Rostedt (Red Hat)  * Note: __trace_bputs is an internal function for trace_puts and
73109ae7234SSteven Rostedt (Red Hat)  *       the @ip is passed in via the trace_puts macro.
73209ae7234SSteven Rostedt (Red Hat)  *
73309ae7234SSteven Rostedt (Red Hat)  * This is similar to trace_printk() but is made for those really fast
734e8c97af0SRandy Dunlap  * paths that a developer wants the least amount of "Heisenbug" effects,
73509ae7234SSteven Rostedt (Red Hat)  * where the processing of the print format is still too much.
73609ae7234SSteven Rostedt (Red Hat)  *
73709ae7234SSteven Rostedt (Red Hat)  * This function allows a kernel developer to debug fast path sections
73809ae7234SSteven Rostedt (Red Hat)  * that printk is not appropriate for. By scattering in various
73909ae7234SSteven Rostedt (Red Hat)  * printk like tracing in the code, a developer can quickly see
74009ae7234SSteven Rostedt (Red Hat)  * where problems are occurring.
74109ae7234SSteven Rostedt (Red Hat)  *
74209ae7234SSteven Rostedt (Red Hat)  * This is intended as a debugging tool for the developer only.
74309ae7234SSteven Rostedt (Red Hat)  * Please refrain from leaving trace_puts scattered around in
74409ae7234SSteven Rostedt (Red Hat)  * your code. (Extra memory is used for special buffers that are
745e8c97af0SRandy Dunlap  * allocated when trace_puts() is used.)
74609ae7234SSteven Rostedt (Red Hat)  *
74709ae7234SSteven Rostedt (Red Hat)  * Returns: 0 if nothing was written, positive # if string was.
74809ae7234SSteven Rostedt (Red Hat)  *  (1 when __trace_bputs is used, strlen(str) when __trace_puts is used)
74909ae7234SSteven Rostedt (Red Hat)  */
75009ae7234SSteven Rostedt (Red Hat) 
75109ae7234SSteven Rostedt (Red Hat) #define trace_puts(str) ({						\
7523debb0a9SSteven Rostedt (Red Hat) 	static const char *trace_printk_fmt __used			\
75309ae7234SSteven Rostedt (Red Hat) 		__attribute__((section("__trace_printk_fmt"))) =	\
75409ae7234SSteven Rostedt (Red Hat) 		__builtin_constant_p(str) ? str : NULL;			\
75509ae7234SSteven Rostedt (Red Hat) 									\
75609ae7234SSteven Rostedt (Red Hat) 	if (__builtin_constant_p(str))					\
75709ae7234SSteven Rostedt (Red Hat) 		__trace_bputs(_THIS_IP_, trace_printk_fmt);		\
75809ae7234SSteven Rostedt (Red Hat) 	else								\
75909ae7234SSteven Rostedt (Red Hat) 		__trace_puts(_THIS_IP_, str, strlen(str));		\
76009ae7234SSteven Rostedt (Red Hat) })
761bcf312cfSSteven Rostedt extern int __trace_bputs(unsigned long ip, const char *str);
762bcf312cfSSteven Rostedt extern int __trace_puts(unsigned long ip, const char *str, int size);
76309ae7234SSteven Rostedt (Red Hat) 
764c142be8eSSteven Rostedt (Red Hat) extern void trace_dump_stack(int skip);
76503889384SSteven Rostedt 
76648ead020SFrederic Weisbecker /*
76748ead020SFrederic Weisbecker  * The double __builtin_constant_p is because gcc will give us an error
76848ead020SFrederic Weisbecker  * if we try to allocate the static variable to fmt if it is not a
76948ead020SFrederic Weisbecker  * constant. Even with the outer if statement.
77048ead020SFrederic Weisbecker  */
771769b0441SFrederic Weisbecker #define ftrace_vprintk(fmt, vargs)					\
772769b0441SFrederic Weisbecker do {									\
77348ead020SFrederic Weisbecker 	if (__builtin_constant_p(fmt)) {				\
7743debb0a9SSteven Rostedt (Red Hat) 		static const char *trace_printk_fmt __used		\
77548ead020SFrederic Weisbecker 		  __attribute__((section("__trace_printk_fmt"))) =	\
77648ead020SFrederic Weisbecker 			__builtin_constant_p(fmt) ? fmt : NULL;		\
7777bffc23eSIngo Molnar 									\
77848ead020SFrederic Weisbecker 		__ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs);	\
77948ead020SFrederic Weisbecker 	} else								\
78048ead020SFrederic Weisbecker 		__ftrace_vprintk(_THIS_IP_, fmt, vargs);		\
781769b0441SFrederic Weisbecker } while (0)
782769b0441SFrederic Weisbecker 
7838db14860SNicolas Iooss extern __printf(2, 0) int
78448ead020SFrederic Weisbecker __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
78548ead020SFrederic Weisbecker 
7868db14860SNicolas Iooss extern __printf(2, 0) int
787526211bcSIngo Molnar __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
788769b0441SFrederic Weisbecker 
789cecbca96SFrederic Weisbecker extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
790526211bcSIngo Molnar #else
791526211bcSIngo Molnar static inline void tracing_start(void) { }
792526211bcSIngo Molnar static inline void tracing_stop(void) { }
793e67bc51eSDhaval Giani static inline void trace_dump_stack(int skip) { }
79493d68e52SSteven Rostedt 
79593d68e52SSteven Rostedt static inline void tracing_on(void) { }
79693d68e52SSteven Rostedt static inline void tracing_off(void) { }
79793d68e52SSteven Rostedt static inline int tracing_is_on(void) { return 0; }
798ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot(void) { }
799ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot_alloc(void) { }
80093d68e52SSteven Rostedt 
80160efc15aSMichal Hocko static inline __printf(1, 2)
80260efc15aSMichal Hocko int trace_printk(const char *fmt, ...)
803526211bcSIngo Molnar {
804526211bcSIngo Molnar 	return 0;
805526211bcSIngo Molnar }
8068db14860SNicolas Iooss static __printf(1, 0) inline int
807526211bcSIngo Molnar ftrace_vprintk(const char *fmt, va_list ap)
808526211bcSIngo Molnar {
809526211bcSIngo Molnar 	return 0;
810526211bcSIngo Molnar }
811cecbca96SFrederic Weisbecker static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
812769b0441SFrederic Weisbecker #endif /* CONFIG_TRACING */
813526211bcSIngo Molnar 
814526211bcSIngo Molnar /*
8153c8ba0d6SKees Cook  * min()/max()/clamp() macros must accomplish three things:
8163c8ba0d6SKees Cook  *
8173c8ba0d6SKees Cook  * - avoid multiple evaluations of the arguments (so side-effects like
8183c8ba0d6SKees Cook  *   "x++" happen only once) when non-constant.
8193c8ba0d6SKees Cook  * - perform strict type-checking (to generate warnings instead of
8203c8ba0d6SKees Cook  *   nasty runtime surprises). See the "unnecessary" pointer comparison
8213c8ba0d6SKees Cook  *   in __typecheck().
8223c8ba0d6SKees Cook  * - retain result as a constant expressions when called with only
8233c8ba0d6SKees Cook  *   constant expressions (to avoid tripping VLA warnings in stack
8243c8ba0d6SKees Cook  *   allocation usage).
8251da177e4SLinus Torvalds  */
8263c8ba0d6SKees Cook #define __typecheck(x, y) \
8273c8ba0d6SKees Cook 		(!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
8283c8ba0d6SKees Cook 
8293c8ba0d6SKees Cook /*
8303c8ba0d6SKees Cook  * This returns a constant expression while determining if an argument is
8313c8ba0d6SKees Cook  * a constant expression, most importantly without evaluating the argument.
8323c8ba0d6SKees Cook  * Glory to Martin Uecker <[email protected]>
8333c8ba0d6SKees Cook  */
8343c8ba0d6SKees Cook #define __is_constexpr(x) \
8353c8ba0d6SKees Cook 	(sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))
8363c8ba0d6SKees Cook 
8373c8ba0d6SKees Cook #define __no_side_effects(x, y) \
8383c8ba0d6SKees Cook 		(__is_constexpr(x) && __is_constexpr(y))
8393c8ba0d6SKees Cook 
8403c8ba0d6SKees Cook #define __safe_cmp(x, y) \
8413c8ba0d6SKees Cook 		(__typecheck(x, y) && __no_side_effects(x, y))
8423c8ba0d6SKees Cook 
8433c8ba0d6SKees Cook #define __cmp(x, y, op)	((x) op (y) ? (x) : (y))
8443c8ba0d6SKees Cook 
845e9092d0dSLinus Torvalds #define __cmp_once(x, y, unique_x, unique_y, op) ({	\
846e9092d0dSLinus Torvalds 		typeof(x) unique_x = (x);		\
847e9092d0dSLinus Torvalds 		typeof(y) unique_y = (y);		\
848e9092d0dSLinus Torvalds 		__cmp(unique_x, unique_y, op); })
8493c8ba0d6SKees Cook 
8503c8ba0d6SKees Cook #define __careful_cmp(x, y, op) \
8513c8ba0d6SKees Cook 	__builtin_choose_expr(__safe_cmp(x, y), \
852e9092d0dSLinus Torvalds 		__cmp(x, y, op), \
853e9092d0dSLinus Torvalds 		__cmp_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y), op))
854e8c97af0SRandy Dunlap 
855e8c97af0SRandy Dunlap /**
856e8c97af0SRandy Dunlap  * min - return minimum of two values of the same or compatible types
857e8c97af0SRandy Dunlap  * @x: first value
858e8c97af0SRandy Dunlap  * @y: second value
859e8c97af0SRandy Dunlap  */
8603c8ba0d6SKees Cook #define min(x, y)	__careful_cmp(x, y, <)
861e8c97af0SRandy Dunlap 
862e8c97af0SRandy Dunlap /**
863e8c97af0SRandy Dunlap  * max - return maximum of two values of the same or compatible types
864e8c97af0SRandy Dunlap  * @x: first value
865e8c97af0SRandy Dunlap  * @y: second value
866e8c97af0SRandy Dunlap  */
8673c8ba0d6SKees Cook #define max(x, y)	__careful_cmp(x, y, >)
868bdf4bbaaSHarvey Harrison 
869e8c97af0SRandy Dunlap /**
870e8c97af0SRandy Dunlap  * min3 - return minimum of three values
871e8c97af0SRandy Dunlap  * @x: first value
872e8c97af0SRandy Dunlap  * @y: second value
873e8c97af0SRandy Dunlap  * @z: third value
874e8c97af0SRandy Dunlap  */
8752e1d06e1SMichal Nazarewicz #define min3(x, y, z) min((typeof(x))min(x, y), z)
876e8c97af0SRandy Dunlap 
877e8c97af0SRandy Dunlap /**
878e8c97af0SRandy Dunlap  * max3 - return maximum of three values
879e8c97af0SRandy Dunlap  * @x: first value
880e8c97af0SRandy Dunlap  * @y: second value
881e8c97af0SRandy Dunlap  * @z: third value
882e8c97af0SRandy Dunlap  */
8832e1d06e1SMichal Nazarewicz #define max3(x, y, z) max((typeof(x))max(x, y), z)
884f27c85c5SHagen Paul Pfeifer 
885bdf4bbaaSHarvey Harrison /**
886c8bf1336SMartin K. Petersen  * min_not_zero - return the minimum that is _not_ zero, unless both are zero
887c8bf1336SMartin K. Petersen  * @x: value1
888c8bf1336SMartin K. Petersen  * @y: value2
889c8bf1336SMartin K. Petersen  */
890c8bf1336SMartin K. Petersen #define min_not_zero(x, y) ({			\
891c8bf1336SMartin K. Petersen 	typeof(x) __x = (x);			\
892c8bf1336SMartin K. Petersen 	typeof(y) __y = (y);			\
893c8bf1336SMartin K. Petersen 	__x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
894c8bf1336SMartin K. Petersen 
895c8bf1336SMartin K. Petersen /**
896bdf4bbaaSHarvey Harrison  * clamp - return a value clamped to a given range with strict typechecking
897bdf4bbaaSHarvey Harrison  * @val: current value
8982e1d06e1SMichal Nazarewicz  * @lo: lowest allowable value
8992e1d06e1SMichal Nazarewicz  * @hi: highest allowable value
900bdf4bbaaSHarvey Harrison  *
901e8c97af0SRandy Dunlap  * This macro does strict typechecking of @lo/@hi to make sure they are of the
902e8c97af0SRandy Dunlap  * same type as @val.  See the unnecessary pointer comparisons.
903bdf4bbaaSHarvey Harrison  */
9042e1d06e1SMichal Nazarewicz #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds /*
9071da177e4SLinus Torvalds  * ..and if you can't take the strict
9081da177e4SLinus Torvalds  * types, you can specify one yourself.
9091da177e4SLinus Torvalds  *
910bdf4bbaaSHarvey Harrison  * Or not use min/max/clamp at all, of course.
9111da177e4SLinus Torvalds  */
912e8c97af0SRandy Dunlap 
913e8c97af0SRandy Dunlap /**
914e8c97af0SRandy Dunlap  * min_t - return minimum of two values, using the specified type
915e8c97af0SRandy Dunlap  * @type: data type to use
916e8c97af0SRandy Dunlap  * @x: first value
917e8c97af0SRandy Dunlap  * @y: second value
918e8c97af0SRandy Dunlap  */
9193c8ba0d6SKees Cook #define min_t(type, x, y)	__careful_cmp((type)(x), (type)(y), <)
9201da177e4SLinus Torvalds 
921e8c97af0SRandy Dunlap /**
922e8c97af0SRandy Dunlap  * max_t - return maximum of two values, using the specified type
923e8c97af0SRandy Dunlap  * @type: data type to use
924e8c97af0SRandy Dunlap  * @x: first value
925e8c97af0SRandy Dunlap  * @y: second value
926e8c97af0SRandy Dunlap  */
9273c8ba0d6SKees Cook #define max_t(type, x, y)	__careful_cmp((type)(x), (type)(y), >)
928bdf4bbaaSHarvey Harrison 
929bdf4bbaaSHarvey Harrison /**
930bdf4bbaaSHarvey Harrison  * clamp_t - return a value clamped to a given range using a given type
931bdf4bbaaSHarvey Harrison  * @type: the type of variable to use
932bdf4bbaaSHarvey Harrison  * @val: current value
933c185b07fSMichal Nazarewicz  * @lo: minimum allowable value
934c185b07fSMichal Nazarewicz  * @hi: maximum allowable value
935bdf4bbaaSHarvey Harrison  *
936bdf4bbaaSHarvey Harrison  * This macro does no typechecking and uses temporary variables of type
937e8c97af0SRandy Dunlap  * @type to make all the comparisons.
938bdf4bbaaSHarvey Harrison  */
939c185b07fSMichal Nazarewicz #define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
940bdf4bbaaSHarvey Harrison 
941bdf4bbaaSHarvey Harrison /**
942bdf4bbaaSHarvey Harrison  * clamp_val - return a value clamped to a given range using val's type
943bdf4bbaaSHarvey Harrison  * @val: current value
944c185b07fSMichal Nazarewicz  * @lo: minimum allowable value
945c185b07fSMichal Nazarewicz  * @hi: maximum allowable value
946bdf4bbaaSHarvey Harrison  *
947bdf4bbaaSHarvey Harrison  * This macro does no typechecking and uses temporary variables of whatever
948e8c97af0SRandy Dunlap  * type the input argument @val is.  This is useful when @val is an unsigned
949e8c97af0SRandy Dunlap  * type and @lo and @hi are literals that will otherwise be assigned a signed
950bdf4bbaaSHarvey Harrison  * integer type.
951bdf4bbaaSHarvey Harrison  */
952c185b07fSMichal Nazarewicz #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
9531da177e4SLinus Torvalds 
95491f68b73SWu Fengguang 
955e8c97af0SRandy Dunlap /**
956e8c97af0SRandy Dunlap  * swap - swap values of @a and @b
957e8c97af0SRandy Dunlap  * @a: first value
958e8c97af0SRandy Dunlap  * @b: second value
95991f68b73SWu Fengguang  */
960ac7b9004SPeter Zijlstra #define swap(a, b) \
961ac7b9004SPeter Zijlstra 	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
96291f68b73SWu Fengguang 
963cf14f27fSAlexei Starovoitov /* This counts to 12. Any more, it will return 13th argument. */
964cf14f27fSAlexei Starovoitov #define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n
965cf14f27fSAlexei Starovoitov #define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
966cf14f27fSAlexei Starovoitov 
967cf14f27fSAlexei Starovoitov #define __CONCAT(a, b) a ## b
968cf14f27fSAlexei Starovoitov #define CONCATENATE(a, b) __CONCAT(a, b)
969cf14f27fSAlexei Starovoitov 
9701da177e4SLinus Torvalds /**
9711da177e4SLinus Torvalds  * container_of - cast a member of a structure out to the containing structure
9721da177e4SLinus Torvalds  * @ptr:	the pointer to the member.
9731da177e4SLinus Torvalds  * @type:	the type of the container struct this is embedded in.
9741da177e4SLinus Torvalds  * @member:	the name of the member within the struct.
9751da177e4SLinus Torvalds  *
9761da177e4SLinus Torvalds  */
9771da177e4SLinus Torvalds #define container_of(ptr, type, member) ({				\
978c7acec71SIan Abbott 	void *__mptr = (void *)(ptr);					\
979c7acec71SIan Abbott 	BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) &&	\
980c7acec71SIan Abbott 			 !__same_type(*(ptr), void),			\
981c7acec71SIan Abbott 			 "pointer type mismatch in container_of()");	\
982c7acec71SIan Abbott 	((type *)(__mptr - offsetof(type, member))); })
9831da177e4SLinus Torvalds 
98405e6557bSNeilBrown /**
98505e6557bSNeilBrown  * container_of_safe - cast a member of a structure out to the containing structure
98605e6557bSNeilBrown  * @ptr:	the pointer to the member.
98705e6557bSNeilBrown  * @type:	the type of the container struct this is embedded in.
98805e6557bSNeilBrown  * @member:	the name of the member within the struct.
98905e6557bSNeilBrown  *
99005e6557bSNeilBrown  * If IS_ERR_OR_NULL(ptr), ptr is returned unchanged.
99105e6557bSNeilBrown  */
99205e6557bSNeilBrown #define container_of_safe(ptr, type, member) ({				\
99305e6557bSNeilBrown 	void *__mptr = (void *)(ptr);					\
99405e6557bSNeilBrown 	BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) &&	\
99505e6557bSNeilBrown 			 !__same_type(*(ptr), void),			\
99605e6557bSNeilBrown 			 "pointer type mismatch in container_of()");	\
997227abcc6SDan Carpenter 	IS_ERR_OR_NULL(__mptr) ? ERR_CAST(__mptr) :			\
99805e6557bSNeilBrown 		((type *)(__mptr - offsetof(type, member))); })
99905e6557bSNeilBrown 
1000b9d4f426SArnaud Lacombe /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
1001b9d4f426SArnaud Lacombe #ifdef CONFIG_FTRACE_MCOUNT_RECORD
1002b9d4f426SArnaud Lacombe # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
1003b9d4f426SArnaud Lacombe #endif
10049d00f92fSWANG Cong 
100558f86cc8SRusty Russell /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */
100658f86cc8SRusty Russell #define VERIFY_OCTAL_PERMISSIONS(perms)						\
100758f86cc8SRusty Russell 	(BUILD_BUG_ON_ZERO((perms) < 0) +					\
100858f86cc8SRusty Russell 	 BUILD_BUG_ON_ZERO((perms) > 0777) +					\
100928b8d0c8SGobinda Charan Maji 	 /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */		\
101028b8d0c8SGobinda Charan Maji 	 BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) +	\
101128b8d0c8SGobinda Charan Maji 	 BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) +		\
101228b8d0c8SGobinda Charan Maji 	 /* USER_WRITABLE >= GROUP_WRITABLE */					\
101328b8d0c8SGobinda Charan Maji 	 BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) +	\
101428b8d0c8SGobinda Charan Maji 	 /* OTHER_WRITABLE?  Generally considered a bad idea. */		\
101537549e94SRusty Russell 	 BUILD_BUG_ON_ZERO((perms) & 2) +					\
101658f86cc8SRusty Russell 	 (perms))
10171da177e4SLinus Torvalds #endif
1018