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> 14e9d376f0SJason Baron #include <linux/dynamic_debug.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 49a79ff731SAlexey Dobriyan #define ALIGN(x, a) __ALIGN_KERNEL((x), (a)) 509f93ff5bSAlexey Dobriyan #define __ALIGN_MASK(x, mask) __ALIGN_KERNEL_MASK((x), (mask)) 51a83308e6SMatthew Wilcox #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a))) 52f10db627SHerbert Xu #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0) 532ea58144SLinus Torvalds 54c5e631cfSRusty Russell #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) 55c5e631cfSRusty Russell 569b3be9f9SYinghai Lu /* 579b3be9f9SYinghai Lu * This looks more complex than it should be. But we need to 589b3be9f9SYinghai Lu * get the type for the ~ right in round_down (it needs to be 599b3be9f9SYinghai Lu * as wide as the result!), and we want to evaluate the macro 609b3be9f9SYinghai Lu * arguments just once each. 619b3be9f9SYinghai Lu */ 629b3be9f9SYinghai Lu #define __round_mask(x, y) ((__typeof__(x))((y)-1)) 639b3be9f9SYinghai Lu #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) 649b3be9f9SYinghai Lu #define round_down(x, y) ((x) & ~__round_mask(x, y)) 659b3be9f9SYinghai Lu 664552d5dcSJan Beulich #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) 67930631edSSteven Whitehouse #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) 6836a26c69SNicholas Bellinger #define DIV_ROUND_UP_ULL(ll,d) \ 6936a26c69SNicholas Bellinger ({ unsigned long long _tmp = (ll)+(d)-1; do_div(_tmp, d); _tmp; }) 7036a26c69SNicholas Bellinger 7136a26c69SNicholas Bellinger #if BITS_PER_LONG == 32 7236a26c69SNicholas Bellinger # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d) 7336a26c69SNicholas Bellinger #else 7436a26c69SNicholas Bellinger # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d) 7536a26c69SNicholas Bellinger #endif 76074e61ecSJames Morris 77074e61ecSJames Morris /* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */ 78b28efd54SEric Paris #define roundup(x, y) ( \ 79b28efd54SEric Paris { \ 806070bf35STetsuo Handa const typeof(y) __y = y; \ 81b28efd54SEric Paris (((x) + (__y - 1)) / __y) * __y; \ 82b28efd54SEric Paris } \ 83b28efd54SEric Paris ) 84686a0f3dSEric Paris #define rounddown(x, y) ( \ 85686a0f3dSEric Paris { \ 86686a0f3dSEric Paris typeof(x) __x = (x); \ 87686a0f3dSEric Paris __x - (__x % (y)); \ 88686a0f3dSEric Paris } \ 89686a0f3dSEric Paris ) 90b6d86d3dSGuenter Roeck 91b6d86d3dSGuenter Roeck /* 92b6d86d3dSGuenter Roeck * Divide positive or negative dividend by positive divisor and round 93c4e18497SGuenter Roeck * to closest integer. Result is undefined for negative divisors and 94c4e18497SGuenter Roeck * for negative dividends if the divisor variable type is unsigned. 95b6d86d3dSGuenter Roeck */ 969fe06081SDarrick J. Wong #define DIV_ROUND_CLOSEST(x, divisor)( \ 979fe06081SDarrick J. Wong { \ 98b6d86d3dSGuenter Roeck typeof(x) __x = x; \ 99b6d86d3dSGuenter Roeck typeof(divisor) __d = divisor; \ 100c4e18497SGuenter Roeck (((typeof(x))-1) > 0 || \ 101c4e18497SGuenter Roeck ((typeof(divisor))-1) > 0 || (__x) > 0) ? \ 102b6d86d3dSGuenter Roeck (((__x) + ((__d) / 2)) / (__d)) : \ 103b6d86d3dSGuenter Roeck (((__x) - ((__d) / 2)) / (__d)); \ 1049fe06081SDarrick J. Wong } \ 1059fe06081SDarrick J. Wong ) 1061da177e4SLinus Torvalds 1079993bc63SSalman Qazi /* 1089993bc63SSalman Qazi * Multiplies an integer by a fraction, while avoiding unnecessary 1099993bc63SSalman Qazi * overflow or loss of precision. 1109993bc63SSalman Qazi */ 1119993bc63SSalman Qazi #define mult_frac(x, numer, denom)( \ 1129993bc63SSalman Qazi { \ 1139993bc63SSalman Qazi typeof(x) quot = (x) / (denom); \ 1149993bc63SSalman Qazi typeof(x) rem = (x) % (denom); \ 1159993bc63SSalman Qazi (quot * (numer)) + ((rem * (numer)) / (denom)); \ 1169993bc63SSalman Qazi } \ 1179993bc63SSalman Qazi ) 1189993bc63SSalman Qazi 1199993bc63SSalman Qazi 120ca31e146SEduard - Gabriel Munteanu #define _RET_IP_ (unsigned long)__builtin_return_address(0) 121ca31e146SEduard - Gabriel Munteanu #define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; }) 122ca31e146SEduard - Gabriel Munteanu 12390c699a9SBartlomiej Zolnierkiewicz #ifdef CONFIG_LBDAF 1242da96acdSJens Axboe # include <asm/div64.h> 1252da96acdSJens Axboe # define sector_div(a, b) do_div(a, b) 1262da96acdSJens Axboe #else 1272da96acdSJens Axboe # define sector_div(n, b)( \ 1282da96acdSJens Axboe { \ 1292da96acdSJens Axboe int _res; \ 1302da96acdSJens Axboe _res = (n) % (b); \ 1312da96acdSJens Axboe (n) /= (b); \ 1322da96acdSJens Axboe _res; \ 1332da96acdSJens Axboe } \ 1342da96acdSJens Axboe ) 1352da96acdSJens Axboe #endif 1362da96acdSJens Axboe 137218e180eSAndrew Morton /** 138218e180eSAndrew Morton * upper_32_bits - return bits 32-63 of a number 139218e180eSAndrew Morton * @n: the number we're accessing 140218e180eSAndrew Morton * 141218e180eSAndrew Morton * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress 142218e180eSAndrew Morton * the "right shift count >= width of type" warning when that quantity is 143218e180eSAndrew Morton * 32-bits. 144218e180eSAndrew Morton */ 145218e180eSAndrew Morton #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16)) 146218e180eSAndrew Morton 147204b885eSJoerg Roedel /** 148204b885eSJoerg Roedel * lower_32_bits - return bits 0-31 of a number 149204b885eSJoerg Roedel * @n: the number we're accessing 150204b885eSJoerg Roedel */ 151204b885eSJoerg Roedel #define lower_32_bits(n) ((u32)(n)) 152204b885eSJoerg Roedel 1531da177e4SLinus Torvalds struct completion; 154df2e71fbS[email protected] struct pt_regs; 155df2e71fbS[email protected] struct user; 1561da177e4SLinus Torvalds 157070cb065SUwe Kleine-König #ifdef CONFIG_PREEMPT_VOLUNTARY 158070cb065SUwe Kleine-König extern int _cond_resched(void); 159070cb065SUwe Kleine-König # define might_resched() _cond_resched() 160070cb065SUwe Kleine-König #else 161070cb065SUwe Kleine-König # define might_resched() do { } while (0) 162070cb065SUwe Kleine-König #endif 163070cb065SUwe Kleine-König 164d902db1eSFrederic Weisbecker #ifdef CONFIG_DEBUG_ATOMIC_SLEEP 165*3427445aSPeter Zijlstra void ___might_sleep(const char *file, int line, int preempt_offset); 166d894837fSSimon Kagstrom void __might_sleep(const char *file, int line, int preempt_offset); 1671da177e4SLinus Torvalds /** 1681da177e4SLinus Torvalds * might_sleep - annotation for functions that can sleep 1691da177e4SLinus Torvalds * 1701da177e4SLinus Torvalds * this macro will print a stack trace if it is executed in an atomic 1711da177e4SLinus Torvalds * context (spinlock, irq-handler, ...). 1721da177e4SLinus Torvalds * 1731da177e4SLinus Torvalds * This is a useful debugging help to be able to catch problems early and not 174e20ec991SJim Cromie * be bitten later when the calling function happens to sleep when it is not 1751da177e4SLinus Torvalds * supposed to. 1761da177e4SLinus Torvalds */ 177f8cbd99bSIngo Molnar # define might_sleep() \ 178e4aafea2SFrederic Weisbecker do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0) 1791029a2b5SPeter Zijlstra # define sched_annotate_sleep() __set_current_state(TASK_RUNNING) 180f8cbd99bSIngo Molnar #else 181*3427445aSPeter Zijlstra static inline void ___might_sleep(const char *file, int line, 182*3427445aSPeter Zijlstra int preempt_offset) { } 183d894837fSSimon Kagstrom static inline void __might_sleep(const char *file, int line, 184d894837fSSimon Kagstrom int preempt_offset) { } 185f8cbd99bSIngo Molnar # define might_sleep() do { might_resched(); } while (0) 1861029a2b5SPeter Zijlstra # define sched_annotate_sleep() do { } while (0) 187f8cbd99bSIngo Molnar #endif 188f8cbd99bSIngo Molnar 189368a5fa1SHua Zhong #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0) 190f8cbd99bSIngo Molnar 19171a90484SAndrew Morton /* 19271a90484SAndrew Morton * abs() handles unsigned and signed longs, ints, shorts and chars. For all 19371a90484SAndrew Morton * input types abs() returns a signed long. 19471a90484SAndrew Morton * abs() should not be used for 64-bit types (s64, u64, long long) - use abs64() 19571a90484SAndrew Morton * for those. 19671a90484SAndrew Morton */ 1971da177e4SLinus Torvalds #define abs(x) ({ \ 19871a90484SAndrew Morton long ret; \ 19971a90484SAndrew Morton if (sizeof(x) == sizeof(long)) { \ 200a49c59c0SRolf Eike Beer long __x = (x); \ 20171a90484SAndrew Morton ret = (__x < 0) ? -__x : __x; \ 20271a90484SAndrew Morton } else { \ 20371a90484SAndrew Morton int __x = (x); \ 20471a90484SAndrew Morton ret = (__x < 0) ? -__x : __x; \ 20571a90484SAndrew Morton } \ 20671a90484SAndrew Morton ret; \ 2071da177e4SLinus Torvalds }) 2081da177e4SLinus Torvalds 209658716d1SBrian Behlendorf #define abs64(x) ({ \ 210658716d1SBrian Behlendorf s64 __x = (x); \ 211658716d1SBrian Behlendorf (__x < 0) ? -__x : __x; \ 212658716d1SBrian Behlendorf }) 213658716d1SBrian Behlendorf 21489770b0aSDaniel Borkmann /** 21589770b0aSDaniel Borkmann * reciprocal_scale - "scale" a value into range [0, ep_ro) 21689770b0aSDaniel Borkmann * @val: value 21789770b0aSDaniel Borkmann * @ep_ro: right open interval endpoint 21889770b0aSDaniel Borkmann * 21989770b0aSDaniel Borkmann * Perform a "reciprocal multiplication" in order to "scale" a value into 22089770b0aSDaniel Borkmann * range [0, ep_ro), where the upper interval endpoint is right-open. 22189770b0aSDaniel Borkmann * This is useful, e.g. for accessing a index of an array containing 22289770b0aSDaniel Borkmann * ep_ro elements, for example. Think of it as sort of modulus, only that 22389770b0aSDaniel Borkmann * the result isn't that of modulo. ;) Note that if initial input is a 22489770b0aSDaniel Borkmann * small value, then result will return 0. 22589770b0aSDaniel Borkmann * 22689770b0aSDaniel Borkmann * Return: a result based on val in interval [0, ep_ro). 22789770b0aSDaniel Borkmann */ 22889770b0aSDaniel Borkmann static inline u32 reciprocal_scale(u32 val, u32 ep_ro) 22989770b0aSDaniel Borkmann { 23089770b0aSDaniel Borkmann return (u32)(((u64) val * ep_ro) >> 32); 23189770b0aSDaniel Borkmann } 23289770b0aSDaniel Borkmann 233386e7906SAxel Lin #if defined(CONFIG_MMU) && \ 234386e7906SAxel Lin (defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)) 2353ee1afa3SNick Piggin void might_fault(void); 2363ee1afa3SNick Piggin #else 237662bbcb2SMichael S. Tsirkin static inline void might_fault(void) { } 2383ee1afa3SNick Piggin #endif 2393ee1afa3SNick Piggin 240e041c683SAlan Stern extern struct atomic_notifier_head panic_notifier_list; 241c7ff0d9cSTAMUKI Shoichi extern long (*panic_blink)(int state); 2429402c95fSJoe Perches __printf(1, 2) 2434da47859SJoe Perches void panic(const char *fmt, ...) 244ff2d8b19SJoe Perches __noreturn __cold; 245dd287796SAndrew Morton extern void oops_enter(void); 246dd287796SAndrew Morton extern void oops_exit(void); 247863a6049SAnton Blanchard void print_oops_end_marker(void); 248dd287796SAndrew Morton extern int oops_may_print(void); 2499402c95fSJoe Perches void do_exit(long error_code) 250ff2d8b19SJoe Perches __noreturn; 2519402c95fSJoe Perches void complete_and_exit(struct completion *, long) 252ff2d8b19SJoe Perches __noreturn; 25333ee3b2eSAlexey Dobriyan 25433ee3b2eSAlexey Dobriyan /* Internal, do not use. */ 25533ee3b2eSAlexey Dobriyan int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res); 25633ee3b2eSAlexey Dobriyan int __must_check _kstrtol(const char *s, unsigned int base, long *res); 25733ee3b2eSAlexey Dobriyan 25833ee3b2eSAlexey Dobriyan int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res); 25933ee3b2eSAlexey Dobriyan int __must_check kstrtoll(const char *s, unsigned int base, long long *res); 2604c925d60SEldad Zack 2614c925d60SEldad Zack /** 2624c925d60SEldad Zack * kstrtoul - convert a string to an unsigned long 2634c925d60SEldad Zack * @s: The start of the string. The string must be null-terminated, and may also 2644c925d60SEldad Zack * include a single newline before its terminating null. The first character 2654c925d60SEldad Zack * may also be a plus sign, but not a minus sign. 2664c925d60SEldad Zack * @base: The number base to use. The maximum supported base is 16. If base is 2674c925d60SEldad Zack * given as 0, then the base of the string is automatically detected with the 2684c925d60SEldad Zack * conventional semantics - If it begins with 0x the number will be parsed as a 2694c925d60SEldad Zack * hexadecimal (case insensitive), if it otherwise begins with 0, it will be 2704c925d60SEldad Zack * parsed as an octal number. Otherwise it will be parsed as a decimal. 2714c925d60SEldad Zack * @res: Where to write the result of the conversion on success. 2724c925d60SEldad Zack * 2734c925d60SEldad Zack * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. 2744c925d60SEldad Zack * Used as a replacement for the obsolete simple_strtoull. Return code must 2754c925d60SEldad Zack * be checked. 2764c925d60SEldad Zack */ 27733ee3b2eSAlexey Dobriyan static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res) 27833ee3b2eSAlexey Dobriyan { 27933ee3b2eSAlexey Dobriyan /* 28033ee3b2eSAlexey Dobriyan * We want to shortcut function call, but 28133ee3b2eSAlexey Dobriyan * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0. 28233ee3b2eSAlexey Dobriyan */ 28333ee3b2eSAlexey Dobriyan if (sizeof(unsigned long) == sizeof(unsigned long long) && 28433ee3b2eSAlexey Dobriyan __alignof__(unsigned long) == __alignof__(unsigned long long)) 28533ee3b2eSAlexey Dobriyan return kstrtoull(s, base, (unsigned long long *)res); 28633ee3b2eSAlexey Dobriyan else 28733ee3b2eSAlexey Dobriyan return _kstrtoul(s, base, res); 28833ee3b2eSAlexey Dobriyan } 28933ee3b2eSAlexey Dobriyan 2904c925d60SEldad Zack /** 2914c925d60SEldad Zack * kstrtol - convert a string to a long 2924c925d60SEldad Zack * @s: The start of the string. The string must be null-terminated, and may also 2934c925d60SEldad Zack * include a single newline before its terminating null. The first character 2944c925d60SEldad Zack * may also be a plus sign or a minus sign. 2954c925d60SEldad Zack * @base: The number base to use. The maximum supported base is 16. If base is 2964c925d60SEldad Zack * given as 0, then the base of the string is automatically detected with the 2974c925d60SEldad Zack * conventional semantics - If it begins with 0x the number will be parsed as a 2984c925d60SEldad Zack * hexadecimal (case insensitive), if it otherwise begins with 0, it will be 2994c925d60SEldad Zack * parsed as an octal number. Otherwise it will be parsed as a decimal. 3004c925d60SEldad Zack * @res: Where to write the result of the conversion on success. 3014c925d60SEldad Zack * 3024c925d60SEldad Zack * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. 3034c925d60SEldad Zack * Used as a replacement for the obsolete simple_strtoull. Return code must 3044c925d60SEldad Zack * be checked. 3054c925d60SEldad Zack */ 30633ee3b2eSAlexey Dobriyan static inline int __must_check kstrtol(const char *s, unsigned int base, long *res) 30733ee3b2eSAlexey Dobriyan { 30833ee3b2eSAlexey Dobriyan /* 30933ee3b2eSAlexey Dobriyan * We want to shortcut function call, but 31033ee3b2eSAlexey Dobriyan * __builtin_types_compatible_p(long, long long) = 0. 31133ee3b2eSAlexey Dobriyan */ 31233ee3b2eSAlexey Dobriyan if (sizeof(long) == sizeof(long long) && 31333ee3b2eSAlexey Dobriyan __alignof__(long) == __alignof__(long long)) 31433ee3b2eSAlexey Dobriyan return kstrtoll(s, base, (long long *)res); 31533ee3b2eSAlexey Dobriyan else 31633ee3b2eSAlexey Dobriyan return _kstrtol(s, base, res); 31733ee3b2eSAlexey Dobriyan } 31833ee3b2eSAlexey Dobriyan 31933ee3b2eSAlexey Dobriyan int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res); 32033ee3b2eSAlexey Dobriyan int __must_check kstrtoint(const char *s, unsigned int base, int *res); 32133ee3b2eSAlexey Dobriyan 32233ee3b2eSAlexey Dobriyan static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res) 32333ee3b2eSAlexey Dobriyan { 32433ee3b2eSAlexey Dobriyan return kstrtoull(s, base, res); 32533ee3b2eSAlexey Dobriyan } 32633ee3b2eSAlexey Dobriyan 32733ee3b2eSAlexey Dobriyan static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res) 32833ee3b2eSAlexey Dobriyan { 32933ee3b2eSAlexey Dobriyan return kstrtoll(s, base, res); 33033ee3b2eSAlexey Dobriyan } 33133ee3b2eSAlexey Dobriyan 33233ee3b2eSAlexey Dobriyan static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res) 33333ee3b2eSAlexey Dobriyan { 33433ee3b2eSAlexey Dobriyan return kstrtouint(s, base, res); 33533ee3b2eSAlexey Dobriyan } 33633ee3b2eSAlexey Dobriyan 33733ee3b2eSAlexey Dobriyan static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res) 33833ee3b2eSAlexey Dobriyan { 33933ee3b2eSAlexey Dobriyan return kstrtoint(s, base, res); 34033ee3b2eSAlexey Dobriyan } 34133ee3b2eSAlexey Dobriyan 34233ee3b2eSAlexey Dobriyan int __must_check kstrtou16(const char *s, unsigned int base, u16 *res); 34333ee3b2eSAlexey Dobriyan int __must_check kstrtos16(const char *s, unsigned int base, s16 *res); 34433ee3b2eSAlexey Dobriyan int __must_check kstrtou8(const char *s, unsigned int base, u8 *res); 34533ee3b2eSAlexey Dobriyan int __must_check kstrtos8(const char *s, unsigned int base, s8 *res); 34633ee3b2eSAlexey Dobriyan 347c196e32aSAlexey Dobriyan int __must_check kstrtoull_from_user(const char __user *s, size_t count, unsigned int base, unsigned long long *res); 348c196e32aSAlexey Dobriyan int __must_check kstrtoll_from_user(const char __user *s, size_t count, unsigned int base, long long *res); 349c196e32aSAlexey Dobriyan int __must_check kstrtoul_from_user(const char __user *s, size_t count, unsigned int base, unsigned long *res); 350c196e32aSAlexey Dobriyan int __must_check kstrtol_from_user(const char __user *s, size_t count, unsigned int base, long *res); 351c196e32aSAlexey Dobriyan int __must_check kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, unsigned int *res); 352c196e32aSAlexey Dobriyan int __must_check kstrtoint_from_user(const char __user *s, size_t count, unsigned int base, int *res); 353c196e32aSAlexey Dobriyan int __must_check kstrtou16_from_user(const char __user *s, size_t count, unsigned int base, u16 *res); 354c196e32aSAlexey Dobriyan int __must_check kstrtos16_from_user(const char __user *s, size_t count, unsigned int base, s16 *res); 355c196e32aSAlexey Dobriyan int __must_check kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, u8 *res); 356c196e32aSAlexey Dobriyan int __must_check kstrtos8_from_user(const char __user *s, size_t count, unsigned int base, s8 *res); 357c196e32aSAlexey Dobriyan 358c196e32aSAlexey Dobriyan static inline int __must_check kstrtou64_from_user(const char __user *s, size_t count, unsigned int base, u64 *res) 359c196e32aSAlexey Dobriyan { 360c196e32aSAlexey Dobriyan return kstrtoull_from_user(s, count, base, res); 361c196e32aSAlexey Dobriyan } 362c196e32aSAlexey Dobriyan 363c196e32aSAlexey Dobriyan static inline int __must_check kstrtos64_from_user(const char __user *s, size_t count, unsigned int base, s64 *res) 364c196e32aSAlexey Dobriyan { 365c196e32aSAlexey Dobriyan return kstrtoll_from_user(s, count, base, res); 366c196e32aSAlexey Dobriyan } 367c196e32aSAlexey Dobriyan 368c196e32aSAlexey Dobriyan static inline int __must_check kstrtou32_from_user(const char __user *s, size_t count, unsigned int base, u32 *res) 369c196e32aSAlexey Dobriyan { 370c196e32aSAlexey Dobriyan return kstrtouint_from_user(s, count, base, res); 371c196e32aSAlexey Dobriyan } 372c196e32aSAlexey Dobriyan 373c196e32aSAlexey Dobriyan static inline int __must_check kstrtos32_from_user(const char __user *s, size_t count, unsigned int base, s32 *res) 374c196e32aSAlexey Dobriyan { 375c196e32aSAlexey Dobriyan return kstrtoint_from_user(s, count, base, res); 376c196e32aSAlexey Dobriyan } 377c196e32aSAlexey Dobriyan 37867d0a075SJoe Perches /* Obsolete, do not use. Use kstrto<foo> instead */ 37967d0a075SJoe Perches 3801da177e4SLinus Torvalds extern unsigned long simple_strtoul(const char *,char **,unsigned int); 3811da177e4SLinus Torvalds extern long simple_strtol(const char *,char **,unsigned int); 3821da177e4SLinus Torvalds extern unsigned long long simple_strtoull(const char *,char **,unsigned int); 3831da177e4SLinus Torvalds extern long long simple_strtoll(const char *,char **,unsigned int); 38433ee3b2eSAlexey Dobriyan 3851ac101a5SKAMEZAWA Hiroyuki extern int num_to_str(char *buf, int size, unsigned long long num); 3861ac101a5SKAMEZAWA Hiroyuki 38767d0a075SJoe Perches /* lib/printf utilities */ 38867d0a075SJoe Perches 389b9075fa9SJoe Perches extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...); 390b9075fa9SJoe Perches extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list); 391b9075fa9SJoe Perches extern __printf(3, 4) 392b9075fa9SJoe Perches int snprintf(char *buf, size_t size, const char *fmt, ...); 393b9075fa9SJoe Perches extern __printf(3, 0) 394b9075fa9SJoe Perches int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); 395b9075fa9SJoe Perches extern __printf(3, 4) 396b9075fa9SJoe Perches int scnprintf(char *buf, size_t size, const char *fmt, ...); 397b9075fa9SJoe Perches extern __printf(3, 0) 398b9075fa9SJoe Perches int vscnprintf(char *buf, size_t size, const char *fmt, va_list args); 399b9075fa9SJoe Perches extern __printf(2, 3) 400b9075fa9SJoe Perches char *kasprintf(gfp_t gfp, const char *fmt, ...); 40111443ec7SJeremy Fitzhardinge extern char *kvasprintf(gfp_t gfp, const char *fmt, va_list args); 4021da177e4SLinus Torvalds 4036061d949SJoe Perches extern __scanf(2, 3) 4046061d949SJoe Perches int sscanf(const char *, const char *, ...); 4056061d949SJoe Perches extern __scanf(2, 0) 4066061d949SJoe Perches int vsscanf(const char *, const char *, va_list); 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds extern int get_option(char **str, int *pint); 4091da177e4SLinus Torvalds extern char *get_options(const char *str, int nints, int *ints); 410d974ae37SJeremy Fitzhardinge extern unsigned long long memparse(const char *ptr, char **retptr); 4116ccc72b8SDave Young extern bool parse_option_str(const char *str, const char *option); 4121da177e4SLinus Torvalds 4135e376613STrent Piepho extern int core_kernel_text(unsigned long addr); 414cdbe61bfSSteven Rostedt extern int core_kernel_data(unsigned long addr); 4151da177e4SLinus Torvalds extern int __kernel_text_address(unsigned long addr); 4161da177e4SLinus Torvalds extern int kernel_text_address(unsigned long addr); 417ab7476cfSArjan van de Ven extern int func_ptr_is_kernel_text(void *ptr); 418ab7476cfSArjan van de Ven 41904a2e6a5SEric W. Biederman struct pid; 42004a2e6a5SEric W. Biederman extern struct pid *session_of_pgrp(struct pid *pgrp); 4211da177e4SLinus Torvalds 4221da177e4SLinus Torvalds unsigned long int_sqrt(unsigned long); 4231da177e4SLinus Torvalds 4241da177e4SLinus Torvalds extern void bust_spinlocks(int yes); 4251da177e4SLinus Torvalds extern int oops_in_progress; /* If set, an oops, panic(), BUG() or die() is in progress */ 426aa727107SAdrian Bunk extern int panic_timeout; 4271da177e4SLinus Torvalds extern int panic_on_oops; 4288da5addaSDon Zickus extern int panic_on_unrecovered_nmi; 4295211a242SKurt Garloff extern int panic_on_io_nmi; 43055af7796SMitsuo Hayasaka extern int sysctl_panic_on_stackoverflow; 4315800dc3cSJason Baron /* 4325800dc3cSJason Baron * Only to be used by arch init code. If the user over-wrote the default 4335800dc3cSJason Baron * CONFIG_PANIC_TIMEOUT, honor it. 4345800dc3cSJason Baron */ 4355800dc3cSJason Baron static inline void set_arch_panic_timeout(int timeout, int arch_default_timeout) 4365800dc3cSJason Baron { 4375800dc3cSJason Baron if (panic_timeout == arch_default_timeout) 4385800dc3cSJason Baron panic_timeout = timeout; 4395800dc3cSJason Baron } 4401da177e4SLinus Torvalds extern const char *print_tainted(void); 441373d4d09SRusty Russell enum lockdep_ok { 442373d4d09SRusty Russell LOCKDEP_STILL_OK, 443373d4d09SRusty Russell LOCKDEP_NOW_UNRELIABLE 444373d4d09SRusty Russell }; 445373d4d09SRusty Russell extern void add_taint(unsigned flag, enum lockdep_ok); 44625ddbb18SAndi Kleen extern int test_taint(unsigned flag); 44725ddbb18SAndi Kleen extern unsigned long get_taint(void); 448b920de1bSDavid Howells extern int root_mountflags; 4491da177e4SLinus Torvalds 4502ce802f6STejun Heo extern bool early_boot_irqs_disabled; 4512ce802f6STejun Heo 4521da177e4SLinus Torvalds /* Values used for system_state */ 4531da177e4SLinus Torvalds extern enum system_states { 4541da177e4SLinus Torvalds SYSTEM_BOOTING, 4551da177e4SLinus Torvalds SYSTEM_RUNNING, 4561da177e4SLinus Torvalds SYSTEM_HALT, 4571da177e4SLinus Torvalds SYSTEM_POWER_OFF, 4581da177e4SLinus Torvalds SYSTEM_RESTART, 4591da177e4SLinus Torvalds } system_state; 4601da177e4SLinus Torvalds 46125ddbb18SAndi Kleen #define TAINT_PROPRIETARY_MODULE 0 46225ddbb18SAndi Kleen #define TAINT_FORCED_MODULE 1 4638c90487cSDave Jones #define TAINT_CPU_OUT_OF_SPEC 2 46425ddbb18SAndi Kleen #define TAINT_FORCED_RMMOD 3 46525ddbb18SAndi Kleen #define TAINT_MACHINE_CHECK 4 46625ddbb18SAndi Kleen #define TAINT_BAD_PAGE 5 46725ddbb18SAndi Kleen #define TAINT_USER 6 46825ddbb18SAndi Kleen #define TAINT_DIE 7 46925ddbb18SAndi Kleen #define TAINT_OVERRIDDEN_ACPI_TABLE 8 47025ddbb18SAndi Kleen #define TAINT_WARN 9 47126e9a397SLinus Torvalds #define TAINT_CRAP 10 47292946bc7SBen Hutchings #define TAINT_FIRMWARE_WORKAROUND 11 4732449b8baSBen Hutchings #define TAINT_OOT_MODULE 12 47466cc69e3SMathieu Desnoyers #define TAINT_UNSIGNED_MODULE 13 47569361eefSJosh Hunt #define TAINT_SOFTLOCKUP 14 4761da177e4SLinus Torvalds 4773fc95772SHarvey Harrison extern const char hex_asc[]; 4783fc95772SHarvey Harrison #define hex_asc_lo(x) hex_asc[((x) & 0x0f)] 4793fc95772SHarvey Harrison #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4] 4803fc95772SHarvey Harrison 48155036ba7SAndy Shevchenko static inline char *hex_byte_pack(char *buf, u8 byte) 4823fc95772SHarvey Harrison { 4833fc95772SHarvey Harrison *buf++ = hex_asc_hi(byte); 4843fc95772SHarvey Harrison *buf++ = hex_asc_lo(byte); 4853fc95772SHarvey Harrison return buf; 4863fc95772SHarvey Harrison } 48799eaf3c4SRandy Dunlap 488c26d436cSAndre Naujoks extern const char hex_asc_upper[]; 489c26d436cSAndre Naujoks #define hex_asc_upper_lo(x) hex_asc_upper[((x) & 0x0f)] 490c26d436cSAndre Naujoks #define hex_asc_upper_hi(x) hex_asc_upper[((x) & 0xf0) >> 4] 491c26d436cSAndre Naujoks 492c26d436cSAndre Naujoks static inline char *hex_byte_pack_upper(char *buf, u8 byte) 493c26d436cSAndre Naujoks { 494c26d436cSAndre Naujoks *buf++ = hex_asc_upper_hi(byte); 495c26d436cSAndre Naujoks *buf++ = hex_asc_upper_lo(byte); 496c26d436cSAndre Naujoks return buf; 497c26d436cSAndre Naujoks } 498c26d436cSAndre Naujoks 49990378889SAndy Shevchenko extern int hex_to_bin(char ch); 500b7804983SMimi Zohar extern int __must_check hex2bin(u8 *dst, const char *src, size_t count); 50153d91c5cSDavid Howells extern char *bin2hex(char *dst, const void *src, size_t count); 50290378889SAndy Shevchenko 503a69f5edbSJoe Perches bool mac_pton(const char *s, u8 *mac); 5044cd5773aSAndy Shevchenko 5058a64f336SJoe Perches /* 506526211bcSIngo Molnar * General tracing related utility functions - trace_printk(), 5072002c258SSteven Rostedt * tracing_on/tracing_off and tracing_start()/tracing_stop 5082002c258SSteven Rostedt * 5092002c258SSteven Rostedt * Use tracing_on/tracing_off when you want to quickly turn on or off 5102002c258SSteven Rostedt * tracing. It simply enables or disables the recording of the trace events. 511156f5a78SGeunSik Lim * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on 5122002c258SSteven Rostedt * file, which gives a means for the kernel and userspace to interact. 5132002c258SSteven Rostedt * Place a tracing_off() in the kernel where you want tracing to end. 5142002c258SSteven Rostedt * From user space, examine the trace, and then echo 1 > tracing_on 5152002c258SSteven Rostedt * to continue tracing. 5162002c258SSteven Rostedt * 5172002c258SSteven Rostedt * tracing_stop/tracing_start has slightly more overhead. It is used 5182002c258SSteven Rostedt * by things like suspend to ram where disabling the recording of the 5192002c258SSteven Rostedt * trace is not enough, but tracing must actually stop because things 5202002c258SSteven Rostedt * like calling smp_processor_id() may crash the system. 5212002c258SSteven Rostedt * 5222002c258SSteven Rostedt * Most likely, you want to use tracing_on/tracing_off. 523526211bcSIngo Molnar */ 5242002c258SSteven Rostedt #ifdef CONFIG_RING_BUFFER 5252002c258SSteven Rostedt /* trace_off_permanent stops recording with no way to bring it back */ 5262002c258SSteven Rostedt void tracing_off_permanent(void); 5272002c258SSteven Rostedt #else 5282002c258SSteven Rostedt static inline void tracing_off_permanent(void) { } 5292002c258SSteven Rostedt #endif 530cecbca96SFrederic Weisbecker 531cecbca96SFrederic Weisbecker enum ftrace_dump_mode { 532cecbca96SFrederic Weisbecker DUMP_NONE, 533cecbca96SFrederic Weisbecker DUMP_ALL, 534cecbca96SFrederic Weisbecker DUMP_ORIG, 535cecbca96SFrederic Weisbecker }; 536cecbca96SFrederic Weisbecker 537526211bcSIngo Molnar #ifdef CONFIG_TRACING 53893d68e52SSteven Rostedt void tracing_on(void); 53993d68e52SSteven Rostedt void tracing_off(void); 54093d68e52SSteven Rostedt int tracing_is_on(void); 541ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot(void); 542ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot_alloc(void); 54393d68e52SSteven Rostedt 544526211bcSIngo Molnar extern void tracing_start(void); 545526211bcSIngo Molnar extern void tracing_stop(void); 546526211bcSIngo Molnar 547b9075fa9SJoe Perches static inline __printf(1, 2) 548b9075fa9SJoe Perches void ____trace_printk_check_format(const char *fmt, ...) 549769b0441SFrederic Weisbecker { 550769b0441SFrederic Weisbecker } 551769b0441SFrederic Weisbecker #define __trace_printk_check_format(fmt, args...) \ 552769b0441SFrederic Weisbecker do { \ 553769b0441SFrederic Weisbecker if (0) \ 554769b0441SFrederic Weisbecker ____trace_printk_check_format(fmt, ##args); \ 555769b0441SFrederic Weisbecker } while (0) 556769b0441SFrederic Weisbecker 557526211bcSIngo Molnar /** 558526211bcSIngo Molnar * trace_printk - printf formatting in the ftrace buffer 559526211bcSIngo Molnar * @fmt: the printf format for printing 560526211bcSIngo Molnar * 561526211bcSIngo Molnar * Note: __trace_printk is an internal function for trace_printk and 562526211bcSIngo Molnar * the @ip is passed in via the trace_printk macro. 563526211bcSIngo Molnar * 564526211bcSIngo Molnar * This function allows a kernel developer to debug fast path sections 565526211bcSIngo Molnar * that printk is not appropriate for. By scattering in various 566526211bcSIngo Molnar * printk like tracing in the code, a developer can quickly see 567526211bcSIngo Molnar * where problems are occurring. 568526211bcSIngo Molnar * 569526211bcSIngo Molnar * This is intended as a debugging tool for the developer only. 570526211bcSIngo Molnar * Please refrain from leaving trace_printks scattered around in 57109ae7234SSteven Rostedt (Red Hat) * your code. (Extra memory is used for special buffers that are 57209ae7234SSteven Rostedt (Red Hat) * allocated when trace_printk() is used) 5739d3c752cSSteven Rostedt (Red Hat) * 5749d3c752cSSteven Rostedt (Red Hat) * A little optization trick is done here. If there's only one 5759d3c752cSSteven Rostedt (Red Hat) * argument, there's no need to scan the string for printf formats. 5769d3c752cSSteven Rostedt (Red Hat) * The trace_puts() will suffice. But how can we take advantage of 5779d3c752cSSteven Rostedt (Red Hat) * using trace_puts() when trace_printk() has only one argument? 5789d3c752cSSteven Rostedt (Red Hat) * By stringifying the args and checking the size we can tell 5799d3c752cSSteven Rostedt (Red Hat) * whether or not there are args. __stringify((__VA_ARGS__)) will 5809d3c752cSSteven Rostedt (Red Hat) * turn into "()\0" with a size of 3 when there are no args, anything 5819d3c752cSSteven Rostedt (Red Hat) * else will be bigger. All we need to do is define a string to this, 5829d3c752cSSteven Rostedt (Red Hat) * and then take its size and compare to 3. If it's bigger, use 5839d3c752cSSteven Rostedt (Red Hat) * do_trace_printk() otherwise, optimize it to trace_puts(). Then just 5849d3c752cSSteven Rostedt (Red Hat) * let gcc optimize the rest. 585526211bcSIngo Molnar */ 586769b0441SFrederic Weisbecker 5879d3c752cSSteven Rostedt (Red Hat) #define trace_printk(fmt, ...) \ 5889d3c752cSSteven Rostedt (Red Hat) do { \ 5899d3c752cSSteven Rostedt (Red Hat) char _______STR[] = __stringify((__VA_ARGS__)); \ 5909d3c752cSSteven Rostedt (Red Hat) if (sizeof(_______STR) > 3) \ 5919d3c752cSSteven Rostedt (Red Hat) do_trace_printk(fmt, ##__VA_ARGS__); \ 5929d3c752cSSteven Rostedt (Red Hat) else \ 5939d3c752cSSteven Rostedt (Red Hat) trace_puts(fmt); \ 5949d3c752cSSteven Rostedt (Red Hat) } while (0) 5959d3c752cSSteven Rostedt (Red Hat) 5969d3c752cSSteven Rostedt (Red Hat) #define do_trace_printk(fmt, args...) \ 597769b0441SFrederic Weisbecker do { \ 59848ead020SFrederic Weisbecker static const char *trace_printk_fmt \ 59948ead020SFrederic Weisbecker __attribute__((section("__trace_printk_fmt"))) = \ 60048ead020SFrederic Weisbecker __builtin_constant_p(fmt) ? fmt : NULL; \ 60148ead020SFrederic Weisbecker \ 60207d777feSSteven Rostedt __trace_printk_check_format(fmt, ##args); \ 60307d777feSSteven Rostedt \ 60407d777feSSteven Rostedt if (__builtin_constant_p(fmt)) \ 60548ead020SFrederic Weisbecker __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \ 60607d777feSSteven Rostedt else \ 60748ead020SFrederic Weisbecker __trace_printk(_THIS_IP_, fmt, ##args); \ 608769b0441SFrederic Weisbecker } while (0) 609769b0441SFrederic Weisbecker 610b9075fa9SJoe Perches extern __printf(2, 3) 611b9075fa9SJoe Perches int __trace_bprintk(unsigned long ip, const char *fmt, ...); 61248ead020SFrederic Weisbecker 613b9075fa9SJoe Perches extern __printf(2, 3) 614b9075fa9SJoe Perches int __trace_printk(unsigned long ip, const char *fmt, ...); 615769b0441SFrederic Weisbecker 61609ae7234SSteven Rostedt (Red Hat) /** 61709ae7234SSteven Rostedt (Red Hat) * trace_puts - write a string into the ftrace buffer 61809ae7234SSteven Rostedt (Red Hat) * @str: the string to record 61909ae7234SSteven Rostedt (Red Hat) * 62009ae7234SSteven Rostedt (Red Hat) * Note: __trace_bputs is an internal function for trace_puts and 62109ae7234SSteven Rostedt (Red Hat) * the @ip is passed in via the trace_puts macro. 62209ae7234SSteven Rostedt (Red Hat) * 62309ae7234SSteven Rostedt (Red Hat) * This is similar to trace_printk() but is made for those really fast 62409ae7234SSteven Rostedt (Red Hat) * paths that a developer wants the least amount of "Heisenbug" affects, 62509ae7234SSteven Rostedt (Red Hat) * where the processing of the print format is still too much. 62609ae7234SSteven Rostedt (Red Hat) * 62709ae7234SSteven Rostedt (Red Hat) * This function allows a kernel developer to debug fast path sections 62809ae7234SSteven Rostedt (Red Hat) * that printk is not appropriate for. By scattering in various 62909ae7234SSteven Rostedt (Red Hat) * printk like tracing in the code, a developer can quickly see 63009ae7234SSteven Rostedt (Red Hat) * where problems are occurring. 63109ae7234SSteven Rostedt (Red Hat) * 63209ae7234SSteven Rostedt (Red Hat) * This is intended as a debugging tool for the developer only. 63309ae7234SSteven Rostedt (Red Hat) * Please refrain from leaving trace_puts scattered around in 63409ae7234SSteven Rostedt (Red Hat) * your code. (Extra memory is used for special buffers that are 63509ae7234SSteven Rostedt (Red Hat) * allocated when trace_puts() is used) 63609ae7234SSteven Rostedt (Red Hat) * 63709ae7234SSteven Rostedt (Red Hat) * Returns: 0 if nothing was written, positive # if string was. 63809ae7234SSteven Rostedt (Red Hat) * (1 when __trace_bputs is used, strlen(str) when __trace_puts is used) 63909ae7234SSteven Rostedt (Red Hat) */ 64009ae7234SSteven Rostedt (Red Hat) 64109ae7234SSteven Rostedt (Red Hat) #define trace_puts(str) ({ \ 64209ae7234SSteven Rostedt (Red Hat) static const char *trace_printk_fmt \ 64309ae7234SSteven Rostedt (Red Hat) __attribute__((section("__trace_printk_fmt"))) = \ 64409ae7234SSteven Rostedt (Red Hat) __builtin_constant_p(str) ? str : NULL; \ 64509ae7234SSteven Rostedt (Red Hat) \ 64609ae7234SSteven Rostedt (Red Hat) if (__builtin_constant_p(str)) \ 64709ae7234SSteven Rostedt (Red Hat) __trace_bputs(_THIS_IP_, trace_printk_fmt); \ 64809ae7234SSteven Rostedt (Red Hat) else \ 64909ae7234SSteven Rostedt (Red Hat) __trace_puts(_THIS_IP_, str, strlen(str)); \ 65009ae7234SSteven Rostedt (Red Hat) }) 651bcf312cfSSteven Rostedt extern int __trace_bputs(unsigned long ip, const char *str); 652bcf312cfSSteven Rostedt extern int __trace_puts(unsigned long ip, const char *str, int size); 65309ae7234SSteven Rostedt (Red Hat) 654c142be8eSSteven Rostedt (Red Hat) extern void trace_dump_stack(int skip); 65503889384SSteven Rostedt 65648ead020SFrederic Weisbecker /* 65748ead020SFrederic Weisbecker * The double __builtin_constant_p is because gcc will give us an error 65848ead020SFrederic Weisbecker * if we try to allocate the static variable to fmt if it is not a 65948ead020SFrederic Weisbecker * constant. Even with the outer if statement. 66048ead020SFrederic Weisbecker */ 661769b0441SFrederic Weisbecker #define ftrace_vprintk(fmt, vargs) \ 662769b0441SFrederic Weisbecker do { \ 66348ead020SFrederic Weisbecker if (__builtin_constant_p(fmt)) { \ 664769b0441SFrederic Weisbecker static const char *trace_printk_fmt \ 66548ead020SFrederic Weisbecker __attribute__((section("__trace_printk_fmt"))) = \ 66648ead020SFrederic Weisbecker __builtin_constant_p(fmt) ? fmt : NULL; \ 6677bffc23eSIngo Molnar \ 66848ead020SFrederic Weisbecker __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \ 66948ead020SFrederic Weisbecker } else \ 67048ead020SFrederic Weisbecker __ftrace_vprintk(_THIS_IP_, fmt, vargs); \ 671769b0441SFrederic Weisbecker } while (0) 672769b0441SFrederic Weisbecker 673526211bcSIngo Molnar extern int 67448ead020SFrederic Weisbecker __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap); 67548ead020SFrederic Weisbecker 67648ead020SFrederic Weisbecker extern int 677526211bcSIngo Molnar __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap); 678769b0441SFrederic Weisbecker 679cecbca96SFrederic Weisbecker extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode); 680526211bcSIngo Molnar #else 681526211bcSIngo Molnar static inline void tracing_start(void) { } 682526211bcSIngo Molnar static inline void tracing_stop(void) { } 683e67bc51eSDhaval Giani static inline void trace_dump_stack(int skip) { } 68493d68e52SSteven Rostedt 68593d68e52SSteven Rostedt static inline void tracing_on(void) { } 68693d68e52SSteven Rostedt static inline void tracing_off(void) { } 68793d68e52SSteven Rostedt static inline int tracing_is_on(void) { return 0; } 688ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot(void) { } 689ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot_alloc(void) { } 69093d68e52SSteven Rostedt 69160efc15aSMichal Hocko static inline __printf(1, 2) 69260efc15aSMichal Hocko int trace_printk(const char *fmt, ...) 693526211bcSIngo Molnar { 694526211bcSIngo Molnar return 0; 695526211bcSIngo Molnar } 696526211bcSIngo Molnar static inline int 697526211bcSIngo Molnar ftrace_vprintk(const char *fmt, va_list ap) 698526211bcSIngo Molnar { 699526211bcSIngo Molnar return 0; 700526211bcSIngo Molnar } 701cecbca96SFrederic Weisbecker static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { } 702769b0441SFrederic Weisbecker #endif /* CONFIG_TRACING */ 703526211bcSIngo Molnar 704526211bcSIngo Molnar /* 705bdf4bbaaSHarvey Harrison * min()/max()/clamp() macros that also do 7061da177e4SLinus Torvalds * strict type-checking.. See the 7071da177e4SLinus Torvalds * "unnecessary" pointer comparison. 7081da177e4SLinus Torvalds */ 7091da177e4SLinus Torvalds #define min(x, y) ({ \ 710bdf4bbaaSHarvey Harrison typeof(x) _min1 = (x); \ 711bdf4bbaaSHarvey Harrison typeof(y) _min2 = (y); \ 712bdf4bbaaSHarvey Harrison (void) (&_min1 == &_min2); \ 713bdf4bbaaSHarvey Harrison _min1 < _min2 ? _min1 : _min2; }) 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds #define max(x, y) ({ \ 716bdf4bbaaSHarvey Harrison typeof(x) _max1 = (x); \ 717bdf4bbaaSHarvey Harrison typeof(y) _max2 = (y); \ 718bdf4bbaaSHarvey Harrison (void) (&_max1 == &_max2); \ 719bdf4bbaaSHarvey Harrison _max1 > _max2 ? _max1 : _max2; }) 720bdf4bbaaSHarvey Harrison 7212e1d06e1SMichal Nazarewicz #define min3(x, y, z) min((typeof(x))min(x, y), z) 7222e1d06e1SMichal Nazarewicz #define max3(x, y, z) max((typeof(x))max(x, y), z) 723f27c85c5SHagen Paul Pfeifer 724bdf4bbaaSHarvey Harrison /** 725c8bf1336SMartin K. Petersen * min_not_zero - return the minimum that is _not_ zero, unless both are zero 726c8bf1336SMartin K. Petersen * @x: value1 727c8bf1336SMartin K. Petersen * @y: value2 728c8bf1336SMartin K. Petersen */ 729c8bf1336SMartin K. Petersen #define min_not_zero(x, y) ({ \ 730c8bf1336SMartin K. Petersen typeof(x) __x = (x); \ 731c8bf1336SMartin K. Petersen typeof(y) __y = (y); \ 732c8bf1336SMartin K. Petersen __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); }) 733c8bf1336SMartin K. Petersen 734c8bf1336SMartin K. Petersen /** 735bdf4bbaaSHarvey Harrison * clamp - return a value clamped to a given range with strict typechecking 736bdf4bbaaSHarvey Harrison * @val: current value 7372e1d06e1SMichal Nazarewicz * @lo: lowest allowable value 7382e1d06e1SMichal Nazarewicz * @hi: highest allowable value 739bdf4bbaaSHarvey Harrison * 740c185b07fSMichal Nazarewicz * This macro does strict typechecking of lo/hi to make sure they are of the 741bdf4bbaaSHarvey Harrison * same type as val. See the unnecessary pointer comparisons. 742bdf4bbaaSHarvey Harrison */ 7432e1d06e1SMichal Nazarewicz #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi) 7441da177e4SLinus Torvalds 7451da177e4SLinus Torvalds /* 7461da177e4SLinus Torvalds * ..and if you can't take the strict 7471da177e4SLinus Torvalds * types, you can specify one yourself. 7481da177e4SLinus Torvalds * 749bdf4bbaaSHarvey Harrison * Or not use min/max/clamp at all, of course. 7501da177e4SLinus Torvalds */ 751bdf4bbaaSHarvey Harrison #define min_t(type, x, y) ({ \ 752bdf4bbaaSHarvey Harrison type __min1 = (x); \ 753bdf4bbaaSHarvey Harrison type __min2 = (y); \ 754bdf4bbaaSHarvey Harrison __min1 < __min2 ? __min1: __min2; }) 7551da177e4SLinus Torvalds 756bdf4bbaaSHarvey Harrison #define max_t(type, x, y) ({ \ 757bdf4bbaaSHarvey Harrison type __max1 = (x); \ 758bdf4bbaaSHarvey Harrison type __max2 = (y); \ 759bdf4bbaaSHarvey Harrison __max1 > __max2 ? __max1: __max2; }) 760bdf4bbaaSHarvey Harrison 761bdf4bbaaSHarvey Harrison /** 762bdf4bbaaSHarvey Harrison * clamp_t - return a value clamped to a given range using a given type 763bdf4bbaaSHarvey Harrison * @type: the type of variable to use 764bdf4bbaaSHarvey Harrison * @val: current value 765c185b07fSMichal Nazarewicz * @lo: minimum allowable value 766c185b07fSMichal Nazarewicz * @hi: maximum allowable value 767bdf4bbaaSHarvey Harrison * 768bdf4bbaaSHarvey Harrison * This macro does no typechecking and uses temporary variables of type 769bdf4bbaaSHarvey Harrison * 'type' to make all the comparisons. 770bdf4bbaaSHarvey Harrison */ 771c185b07fSMichal Nazarewicz #define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi) 772bdf4bbaaSHarvey Harrison 773bdf4bbaaSHarvey Harrison /** 774bdf4bbaaSHarvey Harrison * clamp_val - return a value clamped to a given range using val's type 775bdf4bbaaSHarvey Harrison * @val: current value 776c185b07fSMichal Nazarewicz * @lo: minimum allowable value 777c185b07fSMichal Nazarewicz * @hi: maximum allowable value 778bdf4bbaaSHarvey Harrison * 779bdf4bbaaSHarvey Harrison * This macro does no typechecking and uses temporary variables of whatever 780bdf4bbaaSHarvey Harrison * type the input argument 'val' is. This is useful when val is an unsigned 781bdf4bbaaSHarvey Harrison * type and min and max are literals that will otherwise be assigned a signed 782bdf4bbaaSHarvey Harrison * integer type. 783bdf4bbaaSHarvey Harrison */ 784c185b07fSMichal Nazarewicz #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) 7851da177e4SLinus Torvalds 78691f68b73SWu Fengguang 78791f68b73SWu Fengguang /* 78891f68b73SWu Fengguang * swap - swap value of @a and @b 78991f68b73SWu Fengguang */ 790ac7b9004SPeter Zijlstra #define swap(a, b) \ 791ac7b9004SPeter Zijlstra do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0) 79291f68b73SWu Fengguang 7931da177e4SLinus Torvalds /** 7941da177e4SLinus Torvalds * container_of - cast a member of a structure out to the containing structure 7951da177e4SLinus Torvalds * @ptr: the pointer to the member. 7961da177e4SLinus Torvalds * @type: the type of the container struct this is embedded in. 7971da177e4SLinus Torvalds * @member: the name of the member within the struct. 7981da177e4SLinus Torvalds * 7991da177e4SLinus Torvalds */ 8001da177e4SLinus Torvalds #define container_of(ptr, type, member) ({ \ 8011da177e4SLinus Torvalds const typeof( ((type *)0)->member ) *__mptr = (ptr); \ 8021da177e4SLinus Torvalds (type *)( (char *)__mptr - offsetof(type,member) );}) 8031da177e4SLinus Torvalds 804b9d4f426SArnaud Lacombe /* Trap pasters of __FUNCTION__ at compile-time */ 805b9d4f426SArnaud Lacombe #define __FUNCTION__ (__func__) 806b9d4f426SArnaud Lacombe 807b9d4f426SArnaud Lacombe /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */ 808b9d4f426SArnaud Lacombe #ifdef CONFIG_FTRACE_MCOUNT_RECORD 809b9d4f426SArnaud Lacombe # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD 810b9d4f426SArnaud Lacombe #endif 8119d00f92fSWANG Cong 81258f86cc8SRusty Russell /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */ 81358f86cc8SRusty Russell #define VERIFY_OCTAL_PERMISSIONS(perms) \ 81458f86cc8SRusty Russell (BUILD_BUG_ON_ZERO((perms) < 0) + \ 81558f86cc8SRusty Russell BUILD_BUG_ON_ZERO((perms) > 0777) + \ 81658f86cc8SRusty Russell /* User perms >= group perms >= other perms */ \ 81758f86cc8SRusty Russell BUILD_BUG_ON_ZERO(((perms) >> 6) < (((perms) >> 3) & 7)) + \ 81858f86cc8SRusty Russell BUILD_BUG_ON_ZERO((((perms) >> 3) & 7) < ((perms) & 7)) + \ 81937549e94SRusty Russell /* Other writable? Generally considered a bad idea. */ \ 82037549e94SRusty Russell BUILD_BUG_ON_ZERO((perms) & 2) + \ 82158f86cc8SRusty Russell (perms)) 8221da177e4SLinus Torvalds #endif 823