11da177e4SLinus Torvalds #ifndef _LINUX_KERNEL_H 21da177e4SLinus Torvalds #define _LINUX_KERNEL_H 31da177e4SLinus Torvalds 41da177e4SLinus Torvalds 51da177e4SLinus Torvalds #include <stdarg.h> 61da177e4SLinus Torvalds #include <linux/linkage.h> 71da177e4SLinus Torvalds #include <linux/stddef.h> 81da177e4SLinus Torvalds #include <linux/types.h> 91da177e4SLinus Torvalds #include <linux/compiler.h> 101da177e4SLinus Torvalds #include <linux/bitops.h> 11f0d1b0b3SDavid Howells #include <linux/log2.h> 12e0deaff4SAndrew Morton #include <linux/typecheck.h> 13968ab183SLinus Torvalds #include <linux/printk.h> 14c7acec71SIan Abbott #include <linux/build_bug.h> 151da177e4SLinus Torvalds #include <asm/byteorder.h> 16607ca46eSDavid Howells #include <uapi/linux/kernel.h> 171da177e4SLinus Torvalds 184be929beSAlexey Dobriyan #define USHRT_MAX ((u16)(~0U)) 194be929beSAlexey Dobriyan #define SHRT_MAX ((s16)(USHRT_MAX>>1)) 204be929beSAlexey Dobriyan #define SHRT_MIN ((s16)(-SHRT_MAX - 1)) 211da177e4SLinus Torvalds #define INT_MAX ((int)(~0U>>1)) 221da177e4SLinus Torvalds #define INT_MIN (-INT_MAX - 1) 231da177e4SLinus Torvalds #define UINT_MAX (~0U) 241da177e4SLinus Torvalds #define LONG_MAX ((long)(~0UL>>1)) 251da177e4SLinus Torvalds #define LONG_MIN (-LONG_MAX - 1) 261da177e4SLinus Torvalds #define ULONG_MAX (~0UL) 27111ebb6eSOGAWA Hirofumi #define LLONG_MAX ((long long)(~0ULL>>1)) 28111ebb6eSOGAWA Hirofumi #define LLONG_MIN (-LLONG_MAX - 1) 29111ebb6eSOGAWA Hirofumi #define ULLONG_MAX (~0ULL) 30a3860c1cSXi Wang #define SIZE_MAX (~(size_t)0) 311da177e4SLinus Torvalds 3289a07141SAlex Elder #define U8_MAX ((u8)~0U) 3389a07141SAlex Elder #define S8_MAX ((s8)(U8_MAX>>1)) 3489a07141SAlex Elder #define S8_MIN ((s8)(-S8_MAX - 1)) 3589a07141SAlex Elder #define U16_MAX ((u16)~0U) 3689a07141SAlex Elder #define S16_MAX ((s16)(U16_MAX>>1)) 3789a07141SAlex Elder #define S16_MIN ((s16)(-S16_MAX - 1)) 3889a07141SAlex Elder #define U32_MAX ((u32)~0U) 3989a07141SAlex Elder #define S32_MAX ((s32)(U32_MAX>>1)) 4089a07141SAlex Elder #define S32_MIN ((s32)(-S32_MAX - 1)) 4189a07141SAlex Elder #define U64_MAX ((u64)~0ULL) 4289a07141SAlex Elder #define S64_MAX ((s64)(U64_MAX>>1)) 4389a07141SAlex Elder #define S64_MIN ((s64)(-S64_MAX - 1)) 4489a07141SAlex Elder 451da177e4SLinus Torvalds #define STACK_MAGIC 0xdeadbeef 461da177e4SLinus Torvalds 4744696908SDavid S. Miller #define REPEAT_BYTE(x) ((~0ul / 0xff) * (x)) 4844696908SDavid S. Miller 493ca45a46Szijun_hu /* @a is a power of 2 value */ 50a79ff731SAlexey Dobriyan #define ALIGN(x, a) __ALIGN_KERNEL((x), (a)) 51ed067d4aSKrzysztof Kozlowski #define ALIGN_DOWN(x, a) __ALIGN_KERNEL((x) - ((a) - 1), (a)) 529f93ff5bSAlexey Dobriyan #define __ALIGN_MASK(x, mask) __ALIGN_KERNEL_MASK((x), (mask)) 53a83308e6SMatthew Wilcox #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a))) 54f10db627SHerbert Xu #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0) 552ea58144SLinus Torvalds 56d3849953SChristoph Hellwig /* generic data direction definitions */ 57d3849953SChristoph Hellwig #define READ 0 58d3849953SChristoph Hellwig #define WRITE 1 59d3849953SChristoph Hellwig 60c5e631cfSRusty Russell #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) 61c5e631cfSRusty Russell 623ed605bcSGustavo Padovan #define u64_to_user_ptr(x) ( \ 633ed605bcSGustavo Padovan { \ 643ed605bcSGustavo Padovan typecheck(u64, x); \ 653ed605bcSGustavo Padovan (void __user *)(uintptr_t)x; \ 663ed605bcSGustavo Padovan } \ 673ed605bcSGustavo Padovan ) 683ed605bcSGustavo Padovan 699b3be9f9SYinghai Lu /* 709b3be9f9SYinghai Lu * This looks more complex than it should be. But we need to 719b3be9f9SYinghai Lu * get the type for the ~ right in round_down (it needs to be 729b3be9f9SYinghai Lu * as wide as the result!), and we want to evaluate the macro 739b3be9f9SYinghai Lu * arguments just once each. 749b3be9f9SYinghai Lu */ 759b3be9f9SYinghai Lu #define __round_mask(x, y) ((__typeof__(x))((y)-1)) 769b3be9f9SYinghai Lu #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) 779b3be9f9SYinghai Lu #define round_down(x, y) ((x) & ~__round_mask(x, y)) 789b3be9f9SYinghai Lu 794552d5dcSJan Beulich #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) 80b5d3755aSNicolas Dichtel #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP 81*604df322SMasahiro Yamada 82*604df322SMasahiro Yamada #define DIV_ROUND_DOWN_ULL(ll, d) \ 83*604df322SMasahiro Yamada ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; }) 84*604df322SMasahiro Yamada 85*604df322SMasahiro Yamada #define DIV_ROUND_UP_ULL(ll, d) DIV_ROUND_DOWN_ULL((ll) + (d) - 1, (d)) 8636a26c69SNicholas Bellinger 8736a26c69SNicholas Bellinger #if BITS_PER_LONG == 32 8836a26c69SNicholas Bellinger # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d) 8936a26c69SNicholas Bellinger #else 9036a26c69SNicholas Bellinger # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d) 9136a26c69SNicholas Bellinger #endif 92074e61ecSJames Morris 93074e61ecSJames Morris /* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */ 94b28efd54SEric Paris #define roundup(x, y) ( \ 95b28efd54SEric Paris { \ 966070bf35STetsuo Handa const typeof(y) __y = y; \ 97b28efd54SEric Paris (((x) + (__y - 1)) / __y) * __y; \ 98b28efd54SEric Paris } \ 99b28efd54SEric Paris ) 100686a0f3dSEric Paris #define rounddown(x, y) ( \ 101686a0f3dSEric Paris { \ 102686a0f3dSEric Paris typeof(x) __x = (x); \ 103686a0f3dSEric Paris __x - (__x % (y)); \ 104686a0f3dSEric Paris } \ 105686a0f3dSEric Paris ) 106b6d86d3dSGuenter Roeck 107b6d86d3dSGuenter Roeck /* 1084f5901f5SNiklas Söderlund * Divide positive or negative dividend by positive or negative divisor 1094f5901f5SNiklas Söderlund * and round to closest integer. Result is undefined for negative 1104f5901f5SNiklas Söderlund * divisors if he dividend variable type is unsigned and for negative 1114f5901f5SNiklas Söderlund * dividends if the divisor variable type is unsigned. 112b6d86d3dSGuenter Roeck */ 1139fe06081SDarrick J. Wong #define DIV_ROUND_CLOSEST(x, divisor)( \ 1149fe06081SDarrick J. Wong { \ 115b6d86d3dSGuenter Roeck typeof(x) __x = x; \ 116b6d86d3dSGuenter Roeck typeof(divisor) __d = divisor; \ 117c4e18497SGuenter Roeck (((typeof(x))-1) > 0 || \ 1184f5901f5SNiklas Söderlund ((typeof(divisor))-1) > 0 || \ 1194f5901f5SNiklas Söderlund (((__x) > 0) == ((__d) > 0))) ? \ 120b6d86d3dSGuenter Roeck (((__x) + ((__d) / 2)) / (__d)) : \ 121b6d86d3dSGuenter Roeck (((__x) - ((__d) / 2)) / (__d)); \ 1229fe06081SDarrick J. Wong } \ 1239fe06081SDarrick J. Wong ) 124f766093eSJavi Merino /* 125f766093eSJavi Merino * Same as above but for u64 dividends. divisor must be a 32-bit 126f766093eSJavi Merino * number. 127f766093eSJavi Merino */ 128f766093eSJavi Merino #define DIV_ROUND_CLOSEST_ULL(x, divisor)( \ 129f766093eSJavi Merino { \ 130f766093eSJavi Merino typeof(divisor) __d = divisor; \ 131f766093eSJavi Merino unsigned long long _tmp = (x) + (__d) / 2; \ 132f766093eSJavi Merino do_div(_tmp, __d); \ 133f766093eSJavi Merino _tmp; \ 134f766093eSJavi Merino } \ 135f766093eSJavi Merino ) 1361da177e4SLinus Torvalds 1379993bc63SSalman Qazi /* 1389993bc63SSalman Qazi * Multiplies an integer by a fraction, while avoiding unnecessary 1399993bc63SSalman Qazi * overflow or loss of precision. 1409993bc63SSalman Qazi */ 1419993bc63SSalman Qazi #define mult_frac(x, numer, denom)( \ 1429993bc63SSalman Qazi { \ 1439993bc63SSalman Qazi typeof(x) quot = (x) / (denom); \ 1449993bc63SSalman Qazi typeof(x) rem = (x) % (denom); \ 1459993bc63SSalman Qazi (quot * (numer)) + ((rem * (numer)) / (denom)); \ 1469993bc63SSalman Qazi } \ 1479993bc63SSalman Qazi ) 1489993bc63SSalman Qazi 1499993bc63SSalman Qazi 150ca31e146SEduard - Gabriel Munteanu #define _RET_IP_ (unsigned long)__builtin_return_address(0) 151ca31e146SEduard - Gabriel Munteanu #define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; }) 152ca31e146SEduard - Gabriel Munteanu 15390c699a9SBartlomiej Zolnierkiewicz #ifdef CONFIG_LBDAF 1542da96acdSJens Axboe # include <asm/div64.h> 1552da96acdSJens Axboe # define sector_div(a, b) do_div(a, b) 1562da96acdSJens Axboe #else 1572da96acdSJens Axboe # define sector_div(n, b)( \ 1582da96acdSJens Axboe { \ 1592da96acdSJens Axboe int _res; \ 1602da96acdSJens Axboe _res = (n) % (b); \ 1612da96acdSJens Axboe (n) /= (b); \ 1622da96acdSJens Axboe _res; \ 1632da96acdSJens Axboe } \ 1642da96acdSJens Axboe ) 1652da96acdSJens Axboe #endif 1662da96acdSJens Axboe 167218e180eSAndrew Morton /** 168218e180eSAndrew Morton * upper_32_bits - return bits 32-63 of a number 169218e180eSAndrew Morton * @n: the number we're accessing 170218e180eSAndrew Morton * 171218e180eSAndrew Morton * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress 172218e180eSAndrew Morton * the "right shift count >= width of type" warning when that quantity is 173218e180eSAndrew Morton * 32-bits. 174218e180eSAndrew Morton */ 175218e180eSAndrew Morton #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16)) 176218e180eSAndrew Morton 177204b885eSJoerg Roedel /** 178204b885eSJoerg Roedel * lower_32_bits - return bits 0-31 of a number 179204b885eSJoerg Roedel * @n: the number we're accessing 180204b885eSJoerg Roedel */ 181204b885eSJoerg Roedel #define lower_32_bits(n) ((u32)(n)) 182204b885eSJoerg Roedel 1831da177e4SLinus Torvalds struct completion; 184df2e71fbS[email protected] struct pt_regs; 185df2e71fbS[email protected] struct user; 1861da177e4SLinus Torvalds 187070cb065SUwe Kleine-König #ifdef CONFIG_PREEMPT_VOLUNTARY 188070cb065SUwe Kleine-König extern int _cond_resched(void); 189070cb065SUwe Kleine-König # define might_resched() _cond_resched() 190070cb065SUwe Kleine-König #else 191070cb065SUwe Kleine-König # define might_resched() do { } while (0) 192070cb065SUwe Kleine-König #endif 193070cb065SUwe Kleine-König 194d902db1eSFrederic Weisbecker #ifdef CONFIG_DEBUG_ATOMIC_SLEEP 1953427445aSPeter Zijlstra void ___might_sleep(const char *file, int line, int preempt_offset); 196d894837fSSimon Kagstrom void __might_sleep(const char *file, int line, int preempt_offset); 1971da177e4SLinus Torvalds /** 1981da177e4SLinus Torvalds * might_sleep - annotation for functions that can sleep 1991da177e4SLinus Torvalds * 2001da177e4SLinus Torvalds * this macro will print a stack trace if it is executed in an atomic 2011da177e4SLinus Torvalds * context (spinlock, irq-handler, ...). 2021da177e4SLinus Torvalds * 2031da177e4SLinus Torvalds * This is a useful debugging help to be able to catch problems early and not 204e20ec991SJim Cromie * be bitten later when the calling function happens to sleep when it is not 2051da177e4SLinus Torvalds * supposed to. 2061da177e4SLinus Torvalds */ 207f8cbd99bSIngo Molnar # define might_sleep() \ 208e4aafea2SFrederic Weisbecker do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0) 20900845eb9SLinus Torvalds # define sched_annotate_sleep() (current->task_state_change = 0) 210f8cbd99bSIngo Molnar #else 2113427445aSPeter Zijlstra static inline void ___might_sleep(const char *file, int line, 2123427445aSPeter Zijlstra int preempt_offset) { } 213d894837fSSimon Kagstrom static inline void __might_sleep(const char *file, int line, 214d894837fSSimon Kagstrom int preempt_offset) { } 215f8cbd99bSIngo Molnar # define might_sleep() do { might_resched(); } while (0) 2161029a2b5SPeter Zijlstra # define sched_annotate_sleep() do { } while (0) 217f8cbd99bSIngo Molnar #endif 218f8cbd99bSIngo Molnar 219368a5fa1SHua Zhong #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0) 220f8cbd99bSIngo Molnar 221c8299cb6SMichal Nazarewicz /** 222c8299cb6SMichal Nazarewicz * abs - return absolute value of an argument 2238f57e4d9SMichal Nazarewicz * @x: the value. If it is unsigned type, it is converted to signed type first. 2248f57e4d9SMichal Nazarewicz * char is treated as if it was signed (regardless of whether it really is) 2258f57e4d9SMichal Nazarewicz * but the macro's return type is preserved as char. 226c8299cb6SMichal Nazarewicz * 2278f57e4d9SMichal Nazarewicz * Return: an absolute value of x. 22871a90484SAndrew Morton */ 2298f57e4d9SMichal Nazarewicz #define abs(x) __abs_choose_expr(x, long long, \ 2308f57e4d9SMichal Nazarewicz __abs_choose_expr(x, long, \ 2318f57e4d9SMichal Nazarewicz __abs_choose_expr(x, int, \ 2328f57e4d9SMichal Nazarewicz __abs_choose_expr(x, short, \ 2338f57e4d9SMichal Nazarewicz __abs_choose_expr(x, char, \ 2348f57e4d9SMichal Nazarewicz __builtin_choose_expr( \ 2358f57e4d9SMichal Nazarewicz __builtin_types_compatible_p(typeof(x), char), \ 2368f57e4d9SMichal Nazarewicz (char)({ signed char __x = (x); __x<0?-__x:__x; }), \ 2378f57e4d9SMichal Nazarewicz ((void)0))))))) 2388f57e4d9SMichal Nazarewicz 2398f57e4d9SMichal Nazarewicz #define __abs_choose_expr(x, type, other) __builtin_choose_expr( \ 2408f57e4d9SMichal Nazarewicz __builtin_types_compatible_p(typeof(x), signed type) || \ 2418f57e4d9SMichal Nazarewicz __builtin_types_compatible_p(typeof(x), unsigned type), \ 2428f57e4d9SMichal Nazarewicz ({ signed type __x = (x); __x < 0 ? -__x : __x; }), other) 2431da177e4SLinus Torvalds 24489770b0aSDaniel Borkmann /** 24589770b0aSDaniel Borkmann * reciprocal_scale - "scale" a value into range [0, ep_ro) 24689770b0aSDaniel Borkmann * @val: value 24789770b0aSDaniel Borkmann * @ep_ro: right open interval endpoint 24889770b0aSDaniel Borkmann * 24989770b0aSDaniel Borkmann * Perform a "reciprocal multiplication" in order to "scale" a value into 25089770b0aSDaniel Borkmann * range [0, ep_ro), where the upper interval endpoint is right-open. 25189770b0aSDaniel Borkmann * This is useful, e.g. for accessing a index of an array containing 25289770b0aSDaniel Borkmann * ep_ro elements, for example. Think of it as sort of modulus, only that 25389770b0aSDaniel Borkmann * the result isn't that of modulo. ;) Note that if initial input is a 25489770b0aSDaniel Borkmann * small value, then result will return 0. 25589770b0aSDaniel Borkmann * 25689770b0aSDaniel Borkmann * Return: a result based on val in interval [0, ep_ro). 25789770b0aSDaniel Borkmann */ 25889770b0aSDaniel Borkmann static inline u32 reciprocal_scale(u32 val, u32 ep_ro) 25989770b0aSDaniel Borkmann { 26089770b0aSDaniel Borkmann return (u32)(((u64) val * ep_ro) >> 32); 26189770b0aSDaniel Borkmann } 26289770b0aSDaniel Borkmann 263386e7906SAxel Lin #if defined(CONFIG_MMU) && \ 264386e7906SAxel Lin (defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)) 2659ec23531SDavid Hildenbrand #define might_fault() __might_fault(__FILE__, __LINE__) 2669ec23531SDavid Hildenbrand void __might_fault(const char *file, int line); 2673ee1afa3SNick Piggin #else 268662bbcb2SMichael S. Tsirkin static inline void might_fault(void) { } 2693ee1afa3SNick Piggin #endif 2703ee1afa3SNick Piggin 271e041c683SAlan Stern extern struct atomic_notifier_head panic_notifier_list; 272c7ff0d9cSTAMUKI Shoichi extern long (*panic_blink)(int state); 2739402c95fSJoe Perches __printf(1, 2) 2749af6528eSPeter Zijlstra void panic(const char *fmt, ...) __noreturn __cold; 275ebc41f20SHidehiro Kawai void nmi_panic(struct pt_regs *regs, const char *msg); 276dd287796SAndrew Morton extern void oops_enter(void); 277dd287796SAndrew Morton extern void oops_exit(void); 278863a6049SAnton Blanchard void print_oops_end_marker(void); 279dd287796SAndrew Morton extern int oops_may_print(void); 2809af6528eSPeter Zijlstra void do_exit(long error_code) __noreturn; 2819af6528eSPeter Zijlstra void complete_and_exit(struct completion *, long) __noreturn; 28233ee3b2eSAlexey Dobriyan 2837a46ec0eSKees Cook #ifdef CONFIG_ARCH_HAS_REFCOUNT 2847a46ec0eSKees Cook void refcount_error_report(struct pt_regs *regs, const char *err); 2857a46ec0eSKees Cook #else 2867a46ec0eSKees Cook static inline void refcount_error_report(struct pt_regs *regs, const char *err) 2877a46ec0eSKees Cook { } 2887a46ec0eSKees Cook #endif 2897a46ec0eSKees Cook 29033ee3b2eSAlexey Dobriyan /* Internal, do not use. */ 29133ee3b2eSAlexey Dobriyan int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res); 29233ee3b2eSAlexey Dobriyan int __must_check _kstrtol(const char *s, unsigned int base, long *res); 29333ee3b2eSAlexey Dobriyan 29433ee3b2eSAlexey Dobriyan int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res); 29533ee3b2eSAlexey Dobriyan int __must_check kstrtoll(const char *s, unsigned int base, long long *res); 2964c925d60SEldad Zack 2974c925d60SEldad Zack /** 2984c925d60SEldad Zack * kstrtoul - convert a string to an unsigned long 2994c925d60SEldad Zack * @s: The start of the string. The string must be null-terminated, and may also 3004c925d60SEldad Zack * include a single newline before its terminating null. The first character 3014c925d60SEldad Zack * may also be a plus sign, but not a minus sign. 3024c925d60SEldad Zack * @base: The number base to use. The maximum supported base is 16. If base is 3034c925d60SEldad Zack * given as 0, then the base of the string is automatically detected with the 3044c925d60SEldad Zack * conventional semantics - If it begins with 0x the number will be parsed as a 3054c925d60SEldad Zack * hexadecimal (case insensitive), if it otherwise begins with 0, it will be 3064c925d60SEldad Zack * parsed as an octal number. Otherwise it will be parsed as a decimal. 3074c925d60SEldad Zack * @res: Where to write the result of the conversion on success. 3084c925d60SEldad Zack * 3094c925d60SEldad Zack * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. 3104c925d60SEldad Zack * Used as a replacement for the obsolete simple_strtoull. Return code must 3114c925d60SEldad Zack * be checked. 3124c925d60SEldad Zack */ 31333ee3b2eSAlexey Dobriyan static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res) 31433ee3b2eSAlexey Dobriyan { 31533ee3b2eSAlexey Dobriyan /* 31633ee3b2eSAlexey Dobriyan * We want to shortcut function call, but 31733ee3b2eSAlexey Dobriyan * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0. 31833ee3b2eSAlexey Dobriyan */ 31933ee3b2eSAlexey Dobriyan if (sizeof(unsigned long) == sizeof(unsigned long long) && 32033ee3b2eSAlexey Dobriyan __alignof__(unsigned long) == __alignof__(unsigned long long)) 32133ee3b2eSAlexey Dobriyan return kstrtoull(s, base, (unsigned long long *)res); 32233ee3b2eSAlexey Dobriyan else 32333ee3b2eSAlexey Dobriyan return _kstrtoul(s, base, res); 32433ee3b2eSAlexey Dobriyan } 32533ee3b2eSAlexey Dobriyan 3264c925d60SEldad Zack /** 3274c925d60SEldad Zack * kstrtol - convert a string to a long 3284c925d60SEldad Zack * @s: The start of the string. The string must be null-terminated, and may also 3294c925d60SEldad Zack * include a single newline before its terminating null. The first character 3304c925d60SEldad Zack * may also be a plus sign or a minus sign. 3314c925d60SEldad Zack * @base: The number base to use. The maximum supported base is 16. If base is 3324c925d60SEldad Zack * given as 0, then the base of the string is automatically detected with the 3334c925d60SEldad Zack * conventional semantics - If it begins with 0x the number will be parsed as a 3344c925d60SEldad Zack * hexadecimal (case insensitive), if it otherwise begins with 0, it will be 3354c925d60SEldad Zack * parsed as an octal number. Otherwise it will be parsed as a decimal. 3364c925d60SEldad Zack * @res: Where to write the result of the conversion on success. 3374c925d60SEldad Zack * 3384c925d60SEldad Zack * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. 3394c925d60SEldad Zack * Used as a replacement for the obsolete simple_strtoull. Return code must 3404c925d60SEldad Zack * be checked. 3414c925d60SEldad Zack */ 34233ee3b2eSAlexey Dobriyan static inline int __must_check kstrtol(const char *s, unsigned int base, long *res) 34333ee3b2eSAlexey Dobriyan { 34433ee3b2eSAlexey Dobriyan /* 34533ee3b2eSAlexey Dobriyan * We want to shortcut function call, but 34633ee3b2eSAlexey Dobriyan * __builtin_types_compatible_p(long, long long) = 0. 34733ee3b2eSAlexey Dobriyan */ 34833ee3b2eSAlexey Dobriyan if (sizeof(long) == sizeof(long long) && 34933ee3b2eSAlexey Dobriyan __alignof__(long) == __alignof__(long long)) 35033ee3b2eSAlexey Dobriyan return kstrtoll(s, base, (long long *)res); 35133ee3b2eSAlexey Dobriyan else 35233ee3b2eSAlexey Dobriyan return _kstrtol(s, base, res); 35333ee3b2eSAlexey Dobriyan } 35433ee3b2eSAlexey Dobriyan 35533ee3b2eSAlexey Dobriyan int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res); 35633ee3b2eSAlexey Dobriyan int __must_check kstrtoint(const char *s, unsigned int base, int *res); 35733ee3b2eSAlexey Dobriyan 35833ee3b2eSAlexey Dobriyan static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res) 35933ee3b2eSAlexey Dobriyan { 36033ee3b2eSAlexey Dobriyan return kstrtoull(s, base, res); 36133ee3b2eSAlexey Dobriyan } 36233ee3b2eSAlexey Dobriyan 36333ee3b2eSAlexey Dobriyan static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res) 36433ee3b2eSAlexey Dobriyan { 36533ee3b2eSAlexey Dobriyan return kstrtoll(s, base, res); 36633ee3b2eSAlexey Dobriyan } 36733ee3b2eSAlexey Dobriyan 36833ee3b2eSAlexey Dobriyan static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res) 36933ee3b2eSAlexey Dobriyan { 37033ee3b2eSAlexey Dobriyan return kstrtouint(s, base, res); 37133ee3b2eSAlexey Dobriyan } 37233ee3b2eSAlexey Dobriyan 37333ee3b2eSAlexey Dobriyan static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res) 37433ee3b2eSAlexey Dobriyan { 37533ee3b2eSAlexey Dobriyan return kstrtoint(s, base, res); 37633ee3b2eSAlexey Dobriyan } 37733ee3b2eSAlexey Dobriyan 37833ee3b2eSAlexey Dobriyan int __must_check kstrtou16(const char *s, unsigned int base, u16 *res); 37933ee3b2eSAlexey Dobriyan int __must_check kstrtos16(const char *s, unsigned int base, s16 *res); 38033ee3b2eSAlexey Dobriyan int __must_check kstrtou8(const char *s, unsigned int base, u8 *res); 38133ee3b2eSAlexey Dobriyan int __must_check kstrtos8(const char *s, unsigned int base, s8 *res); 382ef951599SKees Cook int __must_check kstrtobool(const char *s, bool *res); 38333ee3b2eSAlexey Dobriyan 384c196e32aSAlexey Dobriyan int __must_check kstrtoull_from_user(const char __user *s, size_t count, unsigned int base, unsigned long long *res); 385c196e32aSAlexey Dobriyan int __must_check kstrtoll_from_user(const char __user *s, size_t count, unsigned int base, long long *res); 386c196e32aSAlexey Dobriyan int __must_check kstrtoul_from_user(const char __user *s, size_t count, unsigned int base, unsigned long *res); 387c196e32aSAlexey Dobriyan int __must_check kstrtol_from_user(const char __user *s, size_t count, unsigned int base, long *res); 388c196e32aSAlexey Dobriyan int __must_check kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, unsigned int *res); 389c196e32aSAlexey Dobriyan int __must_check kstrtoint_from_user(const char __user *s, size_t count, unsigned int base, int *res); 390c196e32aSAlexey Dobriyan int __must_check kstrtou16_from_user(const char __user *s, size_t count, unsigned int base, u16 *res); 391c196e32aSAlexey Dobriyan int __must_check kstrtos16_from_user(const char __user *s, size_t count, unsigned int base, s16 *res); 392c196e32aSAlexey Dobriyan int __must_check kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, u8 *res); 393c196e32aSAlexey Dobriyan int __must_check kstrtos8_from_user(const char __user *s, size_t count, unsigned int base, s8 *res); 394ef951599SKees Cook int __must_check kstrtobool_from_user(const char __user *s, size_t count, bool *res); 395c196e32aSAlexey Dobriyan 396c196e32aSAlexey Dobriyan static inline int __must_check kstrtou64_from_user(const char __user *s, size_t count, unsigned int base, u64 *res) 397c196e32aSAlexey Dobriyan { 398c196e32aSAlexey Dobriyan return kstrtoull_from_user(s, count, base, res); 399c196e32aSAlexey Dobriyan } 400c196e32aSAlexey Dobriyan 401c196e32aSAlexey Dobriyan static inline int __must_check kstrtos64_from_user(const char __user *s, size_t count, unsigned int base, s64 *res) 402c196e32aSAlexey Dobriyan { 403c196e32aSAlexey Dobriyan return kstrtoll_from_user(s, count, base, res); 404c196e32aSAlexey Dobriyan } 405c196e32aSAlexey Dobriyan 406c196e32aSAlexey Dobriyan static inline int __must_check kstrtou32_from_user(const char __user *s, size_t count, unsigned int base, u32 *res) 407c196e32aSAlexey Dobriyan { 408c196e32aSAlexey Dobriyan return kstrtouint_from_user(s, count, base, res); 409c196e32aSAlexey Dobriyan } 410c196e32aSAlexey Dobriyan 411c196e32aSAlexey Dobriyan static inline int __must_check kstrtos32_from_user(const char __user *s, size_t count, unsigned int base, s32 *res) 412c196e32aSAlexey Dobriyan { 413c196e32aSAlexey Dobriyan return kstrtoint_from_user(s, count, base, res); 414c196e32aSAlexey Dobriyan } 415c196e32aSAlexey Dobriyan 41667d0a075SJoe Perches /* Obsolete, do not use. Use kstrto<foo> instead */ 41767d0a075SJoe Perches 4181da177e4SLinus Torvalds extern unsigned long simple_strtoul(const char *,char **,unsigned int); 4191da177e4SLinus Torvalds extern long simple_strtol(const char *,char **,unsigned int); 4201da177e4SLinus Torvalds extern unsigned long long simple_strtoull(const char *,char **,unsigned int); 4211da177e4SLinus Torvalds extern long long simple_strtoll(const char *,char **,unsigned int); 42233ee3b2eSAlexey Dobriyan 4231ac101a5SKAMEZAWA Hiroyuki extern int num_to_str(char *buf, int size, unsigned long long num); 4241ac101a5SKAMEZAWA Hiroyuki 42567d0a075SJoe Perches /* lib/printf utilities */ 42667d0a075SJoe Perches 427b9075fa9SJoe Perches extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...); 428b9075fa9SJoe Perches extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list); 429b9075fa9SJoe Perches extern __printf(3, 4) 430b9075fa9SJoe Perches int snprintf(char *buf, size_t size, const char *fmt, ...); 431b9075fa9SJoe Perches extern __printf(3, 0) 432b9075fa9SJoe Perches int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); 433b9075fa9SJoe Perches extern __printf(3, 4) 434b9075fa9SJoe Perches int scnprintf(char *buf, size_t size, const char *fmt, ...); 435b9075fa9SJoe Perches extern __printf(3, 0) 436b9075fa9SJoe Perches int vscnprintf(char *buf, size_t size, const char *fmt, va_list args); 43748a27055SRasmus Villemoes extern __printf(2, 3) __malloc 438b9075fa9SJoe Perches char *kasprintf(gfp_t gfp, const char *fmt, ...); 43948a27055SRasmus Villemoes extern __printf(2, 0) __malloc 4408db14860SNicolas Iooss char *kvasprintf(gfp_t gfp, const char *fmt, va_list args); 4410a9df786SRasmus Villemoes extern __printf(2, 0) 4420a9df786SRasmus Villemoes const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args); 4431da177e4SLinus Torvalds 4446061d949SJoe Perches extern __scanf(2, 3) 4456061d949SJoe Perches int sscanf(const char *, const char *, ...); 4466061d949SJoe Perches extern __scanf(2, 0) 4476061d949SJoe Perches int vsscanf(const char *, const char *, va_list); 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds extern int get_option(char **str, int *pint); 4501da177e4SLinus Torvalds extern char *get_options(const char *str, int nints, int *ints); 451d974ae37SJeremy Fitzhardinge extern unsigned long long memparse(const char *ptr, char **retptr); 4526ccc72b8SDave Young extern bool parse_option_str(const char *str, const char *option); 453f51b17c8SBaoquan He extern char *next_arg(char *args, char **param, char **val); 4541da177e4SLinus Torvalds 4555e376613STrent Piepho extern int core_kernel_text(unsigned long addr); 456cdbe61bfSSteven Rostedt extern int core_kernel_data(unsigned long addr); 4571da177e4SLinus Torvalds extern int __kernel_text_address(unsigned long addr); 4581da177e4SLinus Torvalds extern int kernel_text_address(unsigned long addr); 459ab7476cfSArjan van de Ven extern int func_ptr_is_kernel_text(void *ptr); 460ab7476cfSArjan van de Ven 4611da177e4SLinus Torvalds unsigned long int_sqrt(unsigned long); 4621da177e4SLinus Torvalds 4631da177e4SLinus Torvalds extern void bust_spinlocks(int yes); 4641da177e4SLinus Torvalds extern int oops_in_progress; /* If set, an oops, panic(), BUG() or die() is in progress */ 465aa727107SAdrian Bunk extern int panic_timeout; 4661da177e4SLinus Torvalds extern int panic_on_oops; 4678da5addaSDon Zickus extern int panic_on_unrecovered_nmi; 4685211a242SKurt Garloff extern int panic_on_io_nmi; 4699e3961a0SPrarit Bhargava extern int panic_on_warn; 470088e9d25SDaniel Bristot de Oliveira extern int sysctl_panic_on_rcu_stall; 47155af7796SMitsuo Hayasaka extern int sysctl_panic_on_stackoverflow; 4725375b708SHATAYAMA Daisuke 4735375b708SHATAYAMA Daisuke extern bool crash_kexec_post_notifiers; 4745375b708SHATAYAMA Daisuke 4755800dc3cSJason Baron /* 4761717f209SHidehiro Kawai * panic_cpu is used for synchronizing panic() and crash_kexec() execution. It 4771717f209SHidehiro Kawai * holds a CPU number which is executing panic() currently. A value of 4781717f209SHidehiro Kawai * PANIC_CPU_INVALID means no CPU has entered panic() or crash_kexec(). 4791717f209SHidehiro Kawai */ 4801717f209SHidehiro Kawai extern atomic_t panic_cpu; 4811717f209SHidehiro Kawai #define PANIC_CPU_INVALID -1 4821717f209SHidehiro Kawai 4831717f209SHidehiro Kawai /* 4845800dc3cSJason Baron * Only to be used by arch init code. If the user over-wrote the default 4855800dc3cSJason Baron * CONFIG_PANIC_TIMEOUT, honor it. 4865800dc3cSJason Baron */ 4875800dc3cSJason Baron static inline void set_arch_panic_timeout(int timeout, int arch_default_timeout) 4885800dc3cSJason Baron { 4895800dc3cSJason Baron if (panic_timeout == arch_default_timeout) 4905800dc3cSJason Baron panic_timeout = timeout; 4915800dc3cSJason Baron } 4921da177e4SLinus Torvalds extern const char *print_tainted(void); 493373d4d09SRusty Russell enum lockdep_ok { 494373d4d09SRusty Russell LOCKDEP_STILL_OK, 495373d4d09SRusty Russell LOCKDEP_NOW_UNRELIABLE 496373d4d09SRusty Russell }; 497373d4d09SRusty Russell extern void add_taint(unsigned flag, enum lockdep_ok); 49825ddbb18SAndi Kleen extern int test_taint(unsigned flag); 49925ddbb18SAndi Kleen extern unsigned long get_taint(void); 500b920de1bSDavid Howells extern int root_mountflags; 5011da177e4SLinus Torvalds 5022ce802f6STejun Heo extern bool early_boot_irqs_disabled; 5032ce802f6STejun Heo 50469a78ff2SThomas Gleixner /* 50569a78ff2SThomas Gleixner * Values used for system_state. Ordering of the states must not be changed 50669a78ff2SThomas Gleixner * as code checks for <, <=, >, >= STATE. 50769a78ff2SThomas Gleixner */ 5081da177e4SLinus Torvalds extern enum system_states { 5091da177e4SLinus Torvalds SYSTEM_BOOTING, 51069a78ff2SThomas Gleixner SYSTEM_SCHEDULING, 5111da177e4SLinus Torvalds SYSTEM_RUNNING, 5121da177e4SLinus Torvalds SYSTEM_HALT, 5131da177e4SLinus Torvalds SYSTEM_POWER_OFF, 5141da177e4SLinus Torvalds SYSTEM_RESTART, 5151da177e4SLinus Torvalds } system_state; 5161da177e4SLinus Torvalds 51725ddbb18SAndi Kleen #define TAINT_PROPRIETARY_MODULE 0 51825ddbb18SAndi Kleen #define TAINT_FORCED_MODULE 1 5198c90487cSDave Jones #define TAINT_CPU_OUT_OF_SPEC 2 52025ddbb18SAndi Kleen #define TAINT_FORCED_RMMOD 3 52125ddbb18SAndi Kleen #define TAINT_MACHINE_CHECK 4 52225ddbb18SAndi Kleen #define TAINT_BAD_PAGE 5 52325ddbb18SAndi Kleen #define TAINT_USER 6 52425ddbb18SAndi Kleen #define TAINT_DIE 7 52525ddbb18SAndi Kleen #define TAINT_OVERRIDDEN_ACPI_TABLE 8 52625ddbb18SAndi Kleen #define TAINT_WARN 9 52726e9a397SLinus Torvalds #define TAINT_CRAP 10 52892946bc7SBen Hutchings #define TAINT_FIRMWARE_WORKAROUND 11 5292449b8baSBen Hutchings #define TAINT_OOT_MODULE 12 53066cc69e3SMathieu Desnoyers #define TAINT_UNSIGNED_MODULE 13 53169361eefSJosh Hunt #define TAINT_SOFTLOCKUP 14 532c5f45465SSeth Jennings #define TAINT_LIVEPATCH 15 5337fd8329bSPetr Mladek #define TAINT_FLAGS_COUNT 16 5347fd8329bSPetr Mladek 5357fd8329bSPetr Mladek struct taint_flag { 5365eb7c0d0SLarry Finger char c_true; /* character printed when tainted */ 5375eb7c0d0SLarry Finger char c_false; /* character printed when not tainted */ 5387fd8329bSPetr Mladek bool module; /* also show as a per-module taint flag */ 5397fd8329bSPetr Mladek }; 5407fd8329bSPetr Mladek 5417fd8329bSPetr Mladek extern const struct taint_flag taint_flags[TAINT_FLAGS_COUNT]; 5421da177e4SLinus Torvalds 5433fc95772SHarvey Harrison extern const char hex_asc[]; 5443fc95772SHarvey Harrison #define hex_asc_lo(x) hex_asc[((x) & 0x0f)] 5453fc95772SHarvey Harrison #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4] 5463fc95772SHarvey Harrison 54755036ba7SAndy Shevchenko static inline char *hex_byte_pack(char *buf, u8 byte) 5483fc95772SHarvey Harrison { 5493fc95772SHarvey Harrison *buf++ = hex_asc_hi(byte); 5503fc95772SHarvey Harrison *buf++ = hex_asc_lo(byte); 5513fc95772SHarvey Harrison return buf; 5523fc95772SHarvey Harrison } 55399eaf3c4SRandy Dunlap 554c26d436cSAndre Naujoks extern const char hex_asc_upper[]; 555c26d436cSAndre Naujoks #define hex_asc_upper_lo(x) hex_asc_upper[((x) & 0x0f)] 556c26d436cSAndre Naujoks #define hex_asc_upper_hi(x) hex_asc_upper[((x) & 0xf0) >> 4] 557c26d436cSAndre Naujoks 558c26d436cSAndre Naujoks static inline char *hex_byte_pack_upper(char *buf, u8 byte) 559c26d436cSAndre Naujoks { 560c26d436cSAndre Naujoks *buf++ = hex_asc_upper_hi(byte); 561c26d436cSAndre Naujoks *buf++ = hex_asc_upper_lo(byte); 562c26d436cSAndre Naujoks return buf; 563c26d436cSAndre Naujoks } 564c26d436cSAndre Naujoks 56590378889SAndy Shevchenko extern int hex_to_bin(char ch); 566b7804983SMimi Zohar extern int __must_check hex2bin(u8 *dst, const char *src, size_t count); 56753d91c5cSDavid Howells extern char *bin2hex(char *dst, const void *src, size_t count); 56890378889SAndy Shevchenko 569a69f5edbSJoe Perches bool mac_pton(const char *s, u8 *mac); 5704cd5773aSAndy Shevchenko 5718a64f336SJoe Perches /* 572526211bcSIngo Molnar * General tracing related utility functions - trace_printk(), 5732002c258SSteven Rostedt * tracing_on/tracing_off and tracing_start()/tracing_stop 5742002c258SSteven Rostedt * 5752002c258SSteven Rostedt * Use tracing_on/tracing_off when you want to quickly turn on or off 5762002c258SSteven Rostedt * tracing. It simply enables or disables the recording of the trace events. 577156f5a78SGeunSik Lim * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on 5782002c258SSteven Rostedt * file, which gives a means for the kernel and userspace to interact. 5792002c258SSteven Rostedt * Place a tracing_off() in the kernel where you want tracing to end. 5802002c258SSteven Rostedt * From user space, examine the trace, and then echo 1 > tracing_on 5812002c258SSteven Rostedt * to continue tracing. 5822002c258SSteven Rostedt * 5832002c258SSteven Rostedt * tracing_stop/tracing_start has slightly more overhead. It is used 5842002c258SSteven Rostedt * by things like suspend to ram where disabling the recording of the 5852002c258SSteven Rostedt * trace is not enough, but tracing must actually stop because things 5862002c258SSteven Rostedt * like calling smp_processor_id() may crash the system. 5872002c258SSteven Rostedt * 5882002c258SSteven Rostedt * Most likely, you want to use tracing_on/tracing_off. 589526211bcSIngo Molnar */ 590cecbca96SFrederic Weisbecker 591cecbca96SFrederic Weisbecker enum ftrace_dump_mode { 592cecbca96SFrederic Weisbecker DUMP_NONE, 593cecbca96SFrederic Weisbecker DUMP_ALL, 594cecbca96SFrederic Weisbecker DUMP_ORIG, 595cecbca96SFrederic Weisbecker }; 596cecbca96SFrederic Weisbecker 597526211bcSIngo Molnar #ifdef CONFIG_TRACING 59893d68e52SSteven Rostedt void tracing_on(void); 59993d68e52SSteven Rostedt void tracing_off(void); 60093d68e52SSteven Rostedt int tracing_is_on(void); 601ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot(void); 602ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot_alloc(void); 60393d68e52SSteven Rostedt 604526211bcSIngo Molnar extern void tracing_start(void); 605526211bcSIngo Molnar extern void tracing_stop(void); 606526211bcSIngo Molnar 607b9075fa9SJoe Perches static inline __printf(1, 2) 608b9075fa9SJoe Perches void ____trace_printk_check_format(const char *fmt, ...) 609769b0441SFrederic Weisbecker { 610769b0441SFrederic Weisbecker } 611769b0441SFrederic Weisbecker #define __trace_printk_check_format(fmt, args...) \ 612769b0441SFrederic Weisbecker do { \ 613769b0441SFrederic Weisbecker if (0) \ 614769b0441SFrederic Weisbecker ____trace_printk_check_format(fmt, ##args); \ 615769b0441SFrederic Weisbecker } while (0) 616769b0441SFrederic Weisbecker 617526211bcSIngo Molnar /** 618526211bcSIngo Molnar * trace_printk - printf formatting in the ftrace buffer 619526211bcSIngo Molnar * @fmt: the printf format for printing 620526211bcSIngo Molnar * 621526211bcSIngo Molnar * Note: __trace_printk is an internal function for trace_printk and 622526211bcSIngo Molnar * the @ip is passed in via the trace_printk macro. 623526211bcSIngo Molnar * 624526211bcSIngo Molnar * This function allows a kernel developer to debug fast path sections 625526211bcSIngo Molnar * that printk is not appropriate for. By scattering in various 626526211bcSIngo Molnar * printk like tracing in the code, a developer can quickly see 627526211bcSIngo Molnar * where problems are occurring. 628526211bcSIngo Molnar * 629526211bcSIngo Molnar * This is intended as a debugging tool for the developer only. 630526211bcSIngo Molnar * Please refrain from leaving trace_printks scattered around in 63109ae7234SSteven Rostedt (Red Hat) * your code. (Extra memory is used for special buffers that are 63209ae7234SSteven Rostedt (Red Hat) * allocated when trace_printk() is used) 6339d3c752cSSteven Rostedt (Red Hat) * 6349d3c752cSSteven Rostedt (Red Hat) * A little optization trick is done here. If there's only one 6359d3c752cSSteven Rostedt (Red Hat) * argument, there's no need to scan the string for printf formats. 6369d3c752cSSteven Rostedt (Red Hat) * The trace_puts() will suffice. But how can we take advantage of 6379d3c752cSSteven Rostedt (Red Hat) * using trace_puts() when trace_printk() has only one argument? 6389d3c752cSSteven Rostedt (Red Hat) * By stringifying the args and checking the size we can tell 6399d3c752cSSteven Rostedt (Red Hat) * whether or not there are args. __stringify((__VA_ARGS__)) will 6409d3c752cSSteven Rostedt (Red Hat) * turn into "()\0" with a size of 3 when there are no args, anything 6419d3c752cSSteven Rostedt (Red Hat) * else will be bigger. All we need to do is define a string to this, 6429d3c752cSSteven Rostedt (Red Hat) * and then take its size and compare to 3. If it's bigger, use 6439d3c752cSSteven Rostedt (Red Hat) * do_trace_printk() otherwise, optimize it to trace_puts(). Then just 6449d3c752cSSteven Rostedt (Red Hat) * let gcc optimize the rest. 645526211bcSIngo Molnar */ 646769b0441SFrederic Weisbecker 6479d3c752cSSteven Rostedt (Red Hat) #define trace_printk(fmt, ...) \ 6489d3c752cSSteven Rostedt (Red Hat) do { \ 6499d3c752cSSteven Rostedt (Red Hat) char _______STR[] = __stringify((__VA_ARGS__)); \ 6509d3c752cSSteven Rostedt (Red Hat) if (sizeof(_______STR) > 3) \ 6519d3c752cSSteven Rostedt (Red Hat) do_trace_printk(fmt, ##__VA_ARGS__); \ 6529d3c752cSSteven Rostedt (Red Hat) else \ 6539d3c752cSSteven Rostedt (Red Hat) trace_puts(fmt); \ 6549d3c752cSSteven Rostedt (Red Hat) } while (0) 6559d3c752cSSteven Rostedt (Red Hat) 6569d3c752cSSteven Rostedt (Red Hat) #define do_trace_printk(fmt, args...) \ 657769b0441SFrederic Weisbecker do { \ 6583debb0a9SSteven Rostedt (Red Hat) static const char *trace_printk_fmt __used \ 65948ead020SFrederic Weisbecker __attribute__((section("__trace_printk_fmt"))) = \ 66048ead020SFrederic Weisbecker __builtin_constant_p(fmt) ? fmt : NULL; \ 66148ead020SFrederic Weisbecker \ 66207d777feSSteven Rostedt __trace_printk_check_format(fmt, ##args); \ 66307d777feSSteven Rostedt \ 66407d777feSSteven Rostedt if (__builtin_constant_p(fmt)) \ 66548ead020SFrederic Weisbecker __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \ 66607d777feSSteven Rostedt else \ 66748ead020SFrederic Weisbecker __trace_printk(_THIS_IP_, fmt, ##args); \ 668769b0441SFrederic Weisbecker } while (0) 669769b0441SFrederic Weisbecker 670b9075fa9SJoe Perches extern __printf(2, 3) 671b9075fa9SJoe Perches int __trace_bprintk(unsigned long ip, const char *fmt, ...); 67248ead020SFrederic Weisbecker 673b9075fa9SJoe Perches extern __printf(2, 3) 674b9075fa9SJoe Perches int __trace_printk(unsigned long ip, const char *fmt, ...); 675769b0441SFrederic Weisbecker 67609ae7234SSteven Rostedt (Red Hat) /** 67709ae7234SSteven Rostedt (Red Hat) * trace_puts - write a string into the ftrace buffer 67809ae7234SSteven Rostedt (Red Hat) * @str: the string to record 67909ae7234SSteven Rostedt (Red Hat) * 68009ae7234SSteven Rostedt (Red Hat) * Note: __trace_bputs is an internal function for trace_puts and 68109ae7234SSteven Rostedt (Red Hat) * the @ip is passed in via the trace_puts macro. 68209ae7234SSteven Rostedt (Red Hat) * 68309ae7234SSteven Rostedt (Red Hat) * This is similar to trace_printk() but is made for those really fast 68409ae7234SSteven Rostedt (Red Hat) * paths that a developer wants the least amount of "Heisenbug" affects, 68509ae7234SSteven Rostedt (Red Hat) * where the processing of the print format is still too much. 68609ae7234SSteven Rostedt (Red Hat) * 68709ae7234SSteven Rostedt (Red Hat) * This function allows a kernel developer to debug fast path sections 68809ae7234SSteven Rostedt (Red Hat) * that printk is not appropriate for. By scattering in various 68909ae7234SSteven Rostedt (Red Hat) * printk like tracing in the code, a developer can quickly see 69009ae7234SSteven Rostedt (Red Hat) * where problems are occurring. 69109ae7234SSteven Rostedt (Red Hat) * 69209ae7234SSteven Rostedt (Red Hat) * This is intended as a debugging tool for the developer only. 69309ae7234SSteven Rostedt (Red Hat) * Please refrain from leaving trace_puts scattered around in 69409ae7234SSteven Rostedt (Red Hat) * your code. (Extra memory is used for special buffers that are 69509ae7234SSteven Rostedt (Red Hat) * allocated when trace_puts() is used) 69609ae7234SSteven Rostedt (Red Hat) * 69709ae7234SSteven Rostedt (Red Hat) * Returns: 0 if nothing was written, positive # if string was. 69809ae7234SSteven Rostedt (Red Hat) * (1 when __trace_bputs is used, strlen(str) when __trace_puts is used) 69909ae7234SSteven Rostedt (Red Hat) */ 70009ae7234SSteven Rostedt (Red Hat) 70109ae7234SSteven Rostedt (Red Hat) #define trace_puts(str) ({ \ 7023debb0a9SSteven Rostedt (Red Hat) static const char *trace_printk_fmt __used \ 70309ae7234SSteven Rostedt (Red Hat) __attribute__((section("__trace_printk_fmt"))) = \ 70409ae7234SSteven Rostedt (Red Hat) __builtin_constant_p(str) ? str : NULL; \ 70509ae7234SSteven Rostedt (Red Hat) \ 70609ae7234SSteven Rostedt (Red Hat) if (__builtin_constant_p(str)) \ 70709ae7234SSteven Rostedt (Red Hat) __trace_bputs(_THIS_IP_, trace_printk_fmt); \ 70809ae7234SSteven Rostedt (Red Hat) else \ 70909ae7234SSteven Rostedt (Red Hat) __trace_puts(_THIS_IP_, str, strlen(str)); \ 71009ae7234SSteven Rostedt (Red Hat) }) 711bcf312cfSSteven Rostedt extern int __trace_bputs(unsigned long ip, const char *str); 712bcf312cfSSteven Rostedt extern int __trace_puts(unsigned long ip, const char *str, int size); 71309ae7234SSteven Rostedt (Red Hat) 714c142be8eSSteven Rostedt (Red Hat) extern void trace_dump_stack(int skip); 71503889384SSteven Rostedt 71648ead020SFrederic Weisbecker /* 71748ead020SFrederic Weisbecker * The double __builtin_constant_p is because gcc will give us an error 71848ead020SFrederic Weisbecker * if we try to allocate the static variable to fmt if it is not a 71948ead020SFrederic Weisbecker * constant. Even with the outer if statement. 72048ead020SFrederic Weisbecker */ 721769b0441SFrederic Weisbecker #define ftrace_vprintk(fmt, vargs) \ 722769b0441SFrederic Weisbecker do { \ 72348ead020SFrederic Weisbecker if (__builtin_constant_p(fmt)) { \ 7243debb0a9SSteven Rostedt (Red Hat) static const char *trace_printk_fmt __used \ 72548ead020SFrederic Weisbecker __attribute__((section("__trace_printk_fmt"))) = \ 72648ead020SFrederic Weisbecker __builtin_constant_p(fmt) ? fmt : NULL; \ 7277bffc23eSIngo Molnar \ 72848ead020SFrederic Weisbecker __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \ 72948ead020SFrederic Weisbecker } else \ 73048ead020SFrederic Weisbecker __ftrace_vprintk(_THIS_IP_, fmt, vargs); \ 731769b0441SFrederic Weisbecker } while (0) 732769b0441SFrederic Weisbecker 7338db14860SNicolas Iooss extern __printf(2, 0) int 73448ead020SFrederic Weisbecker __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap); 73548ead020SFrederic Weisbecker 7368db14860SNicolas Iooss extern __printf(2, 0) int 737526211bcSIngo Molnar __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap); 738769b0441SFrederic Weisbecker 739cecbca96SFrederic Weisbecker extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode); 740526211bcSIngo Molnar #else 741526211bcSIngo Molnar static inline void tracing_start(void) { } 742526211bcSIngo Molnar static inline void tracing_stop(void) { } 743e67bc51eSDhaval Giani static inline void trace_dump_stack(int skip) { } 74493d68e52SSteven Rostedt 74593d68e52SSteven Rostedt static inline void tracing_on(void) { } 74693d68e52SSteven Rostedt static inline void tracing_off(void) { } 74793d68e52SSteven Rostedt static inline int tracing_is_on(void) { return 0; } 748ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot(void) { } 749ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot_alloc(void) { } 75093d68e52SSteven Rostedt 75160efc15aSMichal Hocko static inline __printf(1, 2) 75260efc15aSMichal Hocko int trace_printk(const char *fmt, ...) 753526211bcSIngo Molnar { 754526211bcSIngo Molnar return 0; 755526211bcSIngo Molnar } 7568db14860SNicolas Iooss static __printf(1, 0) inline int 757526211bcSIngo Molnar ftrace_vprintk(const char *fmt, va_list ap) 758526211bcSIngo Molnar { 759526211bcSIngo Molnar return 0; 760526211bcSIngo Molnar } 761cecbca96SFrederic Weisbecker static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { } 762769b0441SFrederic Weisbecker #endif /* CONFIG_TRACING */ 763526211bcSIngo Molnar 764526211bcSIngo Molnar /* 765bdf4bbaaSHarvey Harrison * min()/max()/clamp() macros that also do 7661da177e4SLinus Torvalds * strict type-checking.. See the 7671da177e4SLinus Torvalds * "unnecessary" pointer comparison. 7681da177e4SLinus Torvalds */ 769589a9785SJohannes Berg #define __min(t1, t2, min1, min2, x, y) ({ \ 770589a9785SJohannes Berg t1 min1 = (x); \ 771589a9785SJohannes Berg t2 min2 = (y); \ 772589a9785SJohannes Berg (void) (&min1 == &min2); \ 773589a9785SJohannes Berg min1 < min2 ? min1 : min2; }) 774589a9785SJohannes Berg #define min(x, y) \ 775589a9785SJohannes Berg __min(typeof(x), typeof(y), \ 776589a9785SJohannes Berg __UNIQUE_ID(min1_), __UNIQUE_ID(min2_), \ 777589a9785SJohannes Berg x, y) 7781da177e4SLinus Torvalds 779589a9785SJohannes Berg #define __max(t1, t2, max1, max2, x, y) ({ \ 780589a9785SJohannes Berg t1 max1 = (x); \ 781589a9785SJohannes Berg t2 max2 = (y); \ 782589a9785SJohannes Berg (void) (&max1 == &max2); \ 783589a9785SJohannes Berg max1 > max2 ? max1 : max2; }) 784589a9785SJohannes Berg #define max(x, y) \ 785589a9785SJohannes Berg __max(typeof(x), typeof(y), \ 786589a9785SJohannes Berg __UNIQUE_ID(max1_), __UNIQUE_ID(max2_), \ 787589a9785SJohannes Berg x, y) 788bdf4bbaaSHarvey Harrison 7892e1d06e1SMichal Nazarewicz #define min3(x, y, z) min((typeof(x))min(x, y), z) 7902e1d06e1SMichal Nazarewicz #define max3(x, y, z) max((typeof(x))max(x, y), z) 791f27c85c5SHagen Paul Pfeifer 792bdf4bbaaSHarvey Harrison /** 793c8bf1336SMartin K. Petersen * min_not_zero - return the minimum that is _not_ zero, unless both are zero 794c8bf1336SMartin K. Petersen * @x: value1 795c8bf1336SMartin K. Petersen * @y: value2 796c8bf1336SMartin K. Petersen */ 797c8bf1336SMartin K. Petersen #define min_not_zero(x, y) ({ \ 798c8bf1336SMartin K. Petersen typeof(x) __x = (x); \ 799c8bf1336SMartin K. Petersen typeof(y) __y = (y); \ 800c8bf1336SMartin K. Petersen __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); }) 801c8bf1336SMartin K. Petersen 802c8bf1336SMartin K. Petersen /** 803bdf4bbaaSHarvey Harrison * clamp - return a value clamped to a given range with strict typechecking 804bdf4bbaaSHarvey Harrison * @val: current value 8052e1d06e1SMichal Nazarewicz * @lo: lowest allowable value 8062e1d06e1SMichal Nazarewicz * @hi: highest allowable value 807bdf4bbaaSHarvey Harrison * 808c185b07fSMichal Nazarewicz * This macro does strict typechecking of lo/hi to make sure they are of the 809bdf4bbaaSHarvey Harrison * same type as val. See the unnecessary pointer comparisons. 810bdf4bbaaSHarvey Harrison */ 8112e1d06e1SMichal Nazarewicz #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi) 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds /* 8141da177e4SLinus Torvalds * ..and if you can't take the strict 8151da177e4SLinus Torvalds * types, you can specify one yourself. 8161da177e4SLinus Torvalds * 817bdf4bbaaSHarvey Harrison * Or not use min/max/clamp at all, of course. 8181da177e4SLinus Torvalds */ 819589a9785SJohannes Berg #define min_t(type, x, y) \ 820589a9785SJohannes Berg __min(type, type, \ 821589a9785SJohannes Berg __UNIQUE_ID(min1_), __UNIQUE_ID(min2_), \ 822589a9785SJohannes Berg x, y) 8231da177e4SLinus Torvalds 824589a9785SJohannes Berg #define max_t(type, x, y) \ 825589a9785SJohannes Berg __max(type, type, \ 826589a9785SJohannes Berg __UNIQUE_ID(min1_), __UNIQUE_ID(min2_), \ 827589a9785SJohannes Berg x, y) 828bdf4bbaaSHarvey Harrison 829bdf4bbaaSHarvey Harrison /** 830bdf4bbaaSHarvey Harrison * clamp_t - return a value clamped to a given range using a given type 831bdf4bbaaSHarvey Harrison * @type: the type of variable to use 832bdf4bbaaSHarvey Harrison * @val: current value 833c185b07fSMichal Nazarewicz * @lo: minimum allowable value 834c185b07fSMichal Nazarewicz * @hi: maximum allowable value 835bdf4bbaaSHarvey Harrison * 836bdf4bbaaSHarvey Harrison * This macro does no typechecking and uses temporary variables of type 837bdf4bbaaSHarvey Harrison * 'type' to make all the comparisons. 838bdf4bbaaSHarvey Harrison */ 839c185b07fSMichal Nazarewicz #define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi) 840bdf4bbaaSHarvey Harrison 841bdf4bbaaSHarvey Harrison /** 842bdf4bbaaSHarvey Harrison * clamp_val - return a value clamped to a given range using val's type 843bdf4bbaaSHarvey Harrison * @val: current value 844c185b07fSMichal Nazarewicz * @lo: minimum allowable value 845c185b07fSMichal Nazarewicz * @hi: maximum allowable value 846bdf4bbaaSHarvey Harrison * 847bdf4bbaaSHarvey Harrison * This macro does no typechecking and uses temporary variables of whatever 848bdf4bbaaSHarvey Harrison * type the input argument 'val' is. This is useful when val is an unsigned 849bdf4bbaaSHarvey Harrison * type and min and max are literals that will otherwise be assigned a signed 850bdf4bbaaSHarvey Harrison * integer type. 851bdf4bbaaSHarvey Harrison */ 852c185b07fSMichal Nazarewicz #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) 8531da177e4SLinus Torvalds 85491f68b73SWu Fengguang 85591f68b73SWu Fengguang /* 85691f68b73SWu Fengguang * swap - swap value of @a and @b 85791f68b73SWu Fengguang */ 858ac7b9004SPeter Zijlstra #define swap(a, b) \ 859ac7b9004SPeter Zijlstra do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0) 86091f68b73SWu Fengguang 8611da177e4SLinus Torvalds /** 8621da177e4SLinus Torvalds * container_of - cast a member of a structure out to the containing structure 8631da177e4SLinus Torvalds * @ptr: the pointer to the member. 8641da177e4SLinus Torvalds * @type: the type of the container struct this is embedded in. 8651da177e4SLinus Torvalds * @member: the name of the member within the struct. 8661da177e4SLinus Torvalds * 8671da177e4SLinus Torvalds */ 8681da177e4SLinus Torvalds #define container_of(ptr, type, member) ({ \ 869c7acec71SIan Abbott void *__mptr = (void *)(ptr); \ 870c7acec71SIan Abbott BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) && \ 871c7acec71SIan Abbott !__same_type(*(ptr), void), \ 872c7acec71SIan Abbott "pointer type mismatch in container_of()"); \ 873c7acec71SIan Abbott ((type *)(__mptr - offsetof(type, member))); }) 8741da177e4SLinus Torvalds 875b9d4f426SArnaud Lacombe /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */ 876b9d4f426SArnaud Lacombe #ifdef CONFIG_FTRACE_MCOUNT_RECORD 877b9d4f426SArnaud Lacombe # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD 878b9d4f426SArnaud Lacombe #endif 8799d00f92fSWANG Cong 88058f86cc8SRusty Russell /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */ 88158f86cc8SRusty Russell #define VERIFY_OCTAL_PERMISSIONS(perms) \ 88258f86cc8SRusty Russell (BUILD_BUG_ON_ZERO((perms) < 0) + \ 88358f86cc8SRusty Russell BUILD_BUG_ON_ZERO((perms) > 0777) + \ 88428b8d0c8SGobinda Charan Maji /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */ \ 88528b8d0c8SGobinda Charan Maji BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) + \ 88628b8d0c8SGobinda Charan Maji BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) + \ 88728b8d0c8SGobinda Charan Maji /* USER_WRITABLE >= GROUP_WRITABLE */ \ 88828b8d0c8SGobinda Charan Maji BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) + \ 88928b8d0c8SGobinda Charan Maji /* OTHER_WRITABLE? Generally considered a bad idea. */ \ 89037549e94SRusty Russell BUILD_BUG_ON_ZERO((perms) & 2) + \ 89158f86cc8SRusty Russell (perms)) 8921da177e4SLinus Torvalds #endif 893