1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 21da177e4SLinus Torvalds #ifndef _LINUX_KERNEL_H 31da177e4SLinus Torvalds #define _LINUX_KERNEL_H 41da177e4SLinus Torvalds 51da177e4SLinus Torvalds 61da177e4SLinus Torvalds #include <stdarg.h> 754d50897SMasahiro Yamada #include <linux/limits.h> 81da177e4SLinus Torvalds #include <linux/linkage.h> 91da177e4SLinus Torvalds #include <linux/stddef.h> 101da177e4SLinus Torvalds #include <linux/types.h> 111da177e4SLinus Torvalds #include <linux/compiler.h> 121da177e4SLinus Torvalds #include <linux/bitops.h> 13f0d1b0b3SDavid Howells #include <linux/log2.h> 14*b296a6d5SAndy Shevchenko #include <linux/minmax.h> 15e0deaff4SAndrew Morton #include <linux/typecheck.h> 16968ab183SLinus Torvalds #include <linux/printk.h> 17c7acec71SIan Abbott #include <linux/build_bug.h> 181da177e4SLinus Torvalds #include <asm/byteorder.h> 19c461aed3SJani Nikula #include <asm/div64.h> 20607ca46eSDavid Howells #include <uapi/linux/kernel.h> 211da177e4SLinus Torvalds 221da177e4SLinus Torvalds #define STACK_MAGIC 0xdeadbeef 231da177e4SLinus Torvalds 24e8c97af0SRandy Dunlap /** 25e8c97af0SRandy Dunlap * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value 26e8c97af0SRandy Dunlap * @x: value to repeat 27e8c97af0SRandy Dunlap * 28e8c97af0SRandy Dunlap * NOTE: @x is not checked for > 0xff; larger values produce odd results. 29e8c97af0SRandy Dunlap */ 3044696908SDavid S. Miller #define REPEAT_BYTE(x) ((~0ul / 0xff) * (x)) 3144696908SDavid S. Miller 323ca45a46Szijun_hu /* @a is a power of 2 value */ 33a79ff731SAlexey Dobriyan #define ALIGN(x, a) __ALIGN_KERNEL((x), (a)) 34ed067d4aSKrzysztof Kozlowski #define ALIGN_DOWN(x, a) __ALIGN_KERNEL((x) - ((a) - 1), (a)) 359f93ff5bSAlexey Dobriyan #define __ALIGN_MASK(x, mask) __ALIGN_KERNEL_MASK((x), (mask)) 36a83308e6SMatthew Wilcox #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a))) 37229f5879SKishon Vijay Abraham I #define PTR_ALIGN_DOWN(p, a) ((typeof(p))ALIGN_DOWN((unsigned long)(p), (a))) 38f10db627SHerbert Xu #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0) 392ea58144SLinus Torvalds 40d3849953SChristoph Hellwig /* generic data direction definitions */ 41d3849953SChristoph Hellwig #define READ 0 42d3849953SChristoph Hellwig #define WRITE 1 43d3849953SChristoph Hellwig 44e8c97af0SRandy Dunlap /** 45e8c97af0SRandy Dunlap * ARRAY_SIZE - get the number of elements in array @arr 46e8c97af0SRandy Dunlap * @arr: array to be sized 47e8c97af0SRandy Dunlap */ 48c5e631cfSRusty Russell #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) 49c5e631cfSRusty Russell 503ed605bcSGustavo Padovan #define u64_to_user_ptr(x) ( \ 513ed605bcSGustavo Padovan { \ 52a0fe2c64SJann Horn typecheck(u64, (x)); \ 53a0fe2c64SJann Horn (void __user *)(uintptr_t)(x); \ 543ed605bcSGustavo Padovan } \ 553ed605bcSGustavo Padovan ) 563ed605bcSGustavo Padovan 579b3be9f9SYinghai Lu /* 589b3be9f9SYinghai Lu * This looks more complex than it should be. But we need to 599b3be9f9SYinghai Lu * get the type for the ~ right in round_down (it needs to be 609b3be9f9SYinghai Lu * as wide as the result!), and we want to evaluate the macro 619b3be9f9SYinghai Lu * arguments just once each. 629b3be9f9SYinghai Lu */ 639b3be9f9SYinghai Lu #define __round_mask(x, y) ((__typeof__(x))((y)-1)) 64cedc5b6aSKees Cook /** 65cedc5b6aSKees Cook * round_up - round up to next specified power of 2 66cedc5b6aSKees Cook * @x: the value to round 67cedc5b6aSKees Cook * @y: multiple to round up to (must be a power of 2) 68cedc5b6aSKees Cook * 69cedc5b6aSKees Cook * Rounds @x up to next multiple of @y (which must be a power of 2). 70cedc5b6aSKees Cook * To perform arbitrary rounding up, use roundup() below. 71cedc5b6aSKees Cook */ 729b3be9f9SYinghai Lu #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) 73cedc5b6aSKees Cook /** 74cedc5b6aSKees Cook * round_down - round down to next specified power of 2 75cedc5b6aSKees Cook * @x: the value to round 76cedc5b6aSKees Cook * @y: multiple to round down to (must be a power of 2) 77cedc5b6aSKees Cook * 78cedc5b6aSKees Cook * Rounds @x down to next multiple of @y (which must be a power of 2). 79cedc5b6aSKees Cook * To perform arbitrary rounding down, use rounddown() below. 80cedc5b6aSKees Cook */ 819b3be9f9SYinghai Lu #define round_down(x, y) ((x) & ~__round_mask(x, y)) 829b3be9f9SYinghai Lu 83ce251e0eSAlexey Dobriyan #define typeof_member(T, m) typeof(((T*)0)->m) 84ce251e0eSAlexey Dobriyan 85b5d3755aSNicolas Dichtel #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP 86604df322SMasahiro Yamada 87604df322SMasahiro Yamada #define DIV_ROUND_DOWN_ULL(ll, d) \ 88604df322SMasahiro Yamada ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; }) 89604df322SMasahiro Yamada 908f9fab48SVinod Koul #define DIV_ROUND_UP_ULL(ll, d) \ 918f9fab48SVinod Koul DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d)) 9236a26c69SNicholas Bellinger 9336a26c69SNicholas Bellinger #if BITS_PER_LONG == 32 9436a26c69SNicholas Bellinger # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d) 9536a26c69SNicholas Bellinger #else 9636a26c69SNicholas Bellinger # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d) 9736a26c69SNicholas Bellinger #endif 98074e61ecSJames Morris 99cedc5b6aSKees Cook /** 100cedc5b6aSKees Cook * roundup - round up to the next specified multiple 101cedc5b6aSKees Cook * @x: the value to up 102cedc5b6aSKees Cook * @y: multiple to round up to 103cedc5b6aSKees Cook * 104cedc5b6aSKees Cook * Rounds @x up to next multiple of @y. If @y will always be a power 105cedc5b6aSKees Cook * of 2, consider using the faster round_up(). 106cedc5b6aSKees Cook */ 107b28efd54SEric Paris #define roundup(x, y) ( \ 108b28efd54SEric Paris { \ 109b95c4d18SRandy Dunlap typeof(y) __y = y; \ 110b28efd54SEric Paris (((x) + (__y - 1)) / __y) * __y; \ 111b28efd54SEric Paris } \ 112b28efd54SEric Paris ) 113cedc5b6aSKees Cook /** 114cedc5b6aSKees Cook * rounddown - round down to next specified multiple 115cedc5b6aSKees Cook * @x: the value to round 116cedc5b6aSKees Cook * @y: multiple to round down to 117cedc5b6aSKees Cook * 118cedc5b6aSKees Cook * Rounds @x down to next multiple of @y. If @y will always be a power 119cedc5b6aSKees Cook * of 2, consider using the faster round_down(). 120cedc5b6aSKees Cook */ 121686a0f3dSEric Paris #define rounddown(x, y) ( \ 122686a0f3dSEric Paris { \ 123686a0f3dSEric Paris typeof(x) __x = (x); \ 124686a0f3dSEric Paris __x - (__x % (y)); \ 125686a0f3dSEric Paris } \ 126686a0f3dSEric Paris ) 127b6d86d3dSGuenter Roeck 128b6d86d3dSGuenter Roeck /* 1294f5901f5SNiklas Söderlund * Divide positive or negative dividend by positive or negative divisor 1304f5901f5SNiklas Söderlund * and round to closest integer. Result is undefined for negative 131e8c97af0SRandy Dunlap * divisors if the dividend variable type is unsigned and for negative 1324f5901f5SNiklas Söderlund * dividends if the divisor variable type is unsigned. 133b6d86d3dSGuenter Roeck */ 1349fe06081SDarrick J. Wong #define DIV_ROUND_CLOSEST(x, divisor)( \ 1359fe06081SDarrick J. Wong { \ 136b6d86d3dSGuenter Roeck typeof(x) __x = x; \ 137b6d86d3dSGuenter Roeck typeof(divisor) __d = divisor; \ 138c4e18497SGuenter Roeck (((typeof(x))-1) > 0 || \ 1394f5901f5SNiklas Söderlund ((typeof(divisor))-1) > 0 || \ 1404f5901f5SNiklas Söderlund (((__x) > 0) == ((__d) > 0))) ? \ 141b6d86d3dSGuenter Roeck (((__x) + ((__d) / 2)) / (__d)) : \ 142b6d86d3dSGuenter Roeck (((__x) - ((__d) / 2)) / (__d)); \ 1439fe06081SDarrick J. Wong } \ 1449fe06081SDarrick J. Wong ) 145f766093eSJavi Merino /* 146f766093eSJavi Merino * Same as above but for u64 dividends. divisor must be a 32-bit 147f766093eSJavi Merino * number. 148f766093eSJavi Merino */ 149f766093eSJavi Merino #define DIV_ROUND_CLOSEST_ULL(x, divisor)( \ 150f766093eSJavi Merino { \ 151f766093eSJavi Merino typeof(divisor) __d = divisor; \ 152f766093eSJavi Merino unsigned long long _tmp = (x) + (__d) / 2; \ 153f766093eSJavi Merino do_div(_tmp, __d); \ 154f766093eSJavi Merino _tmp; \ 155f766093eSJavi Merino } \ 156f766093eSJavi Merino ) 1571da177e4SLinus Torvalds 1589993bc63SSalman Qazi /* 1599993bc63SSalman Qazi * Multiplies an integer by a fraction, while avoiding unnecessary 1609993bc63SSalman Qazi * overflow or loss of precision. 1619993bc63SSalman Qazi */ 1629993bc63SSalman Qazi #define mult_frac(x, numer, denom)( \ 1639993bc63SSalman Qazi { \ 1649993bc63SSalman Qazi typeof(x) quot = (x) / (denom); \ 1659993bc63SSalman Qazi typeof(x) rem = (x) % (denom); \ 1669993bc63SSalman Qazi (quot * (numer)) + ((rem * (numer)) / (denom)); \ 1679993bc63SSalman Qazi } \ 1689993bc63SSalman Qazi ) 1699993bc63SSalman Qazi 1709993bc63SSalman Qazi 171ca31e146SEduard - Gabriel Munteanu #define _RET_IP_ (unsigned long)__builtin_return_address(0) 172ca31e146SEduard - Gabriel Munteanu #define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; }) 173ca31e146SEduard - Gabriel Munteanu 1742da96acdSJens Axboe #define sector_div(a, b) do_div(a, b) 1752da96acdSJens Axboe 176218e180eSAndrew Morton /** 177218e180eSAndrew Morton * upper_32_bits - return bits 32-63 of a number 178218e180eSAndrew Morton * @n: the number we're accessing 179218e180eSAndrew Morton * 180218e180eSAndrew Morton * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress 181218e180eSAndrew Morton * the "right shift count >= width of type" warning when that quantity is 182218e180eSAndrew Morton * 32-bits. 183218e180eSAndrew Morton */ 184218e180eSAndrew Morton #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16)) 185218e180eSAndrew Morton 186204b885eSJoerg Roedel /** 187204b885eSJoerg Roedel * lower_32_bits - return bits 0-31 of a number 188204b885eSJoerg Roedel * @n: the number we're accessing 189204b885eSJoerg Roedel */ 190ef91bb19SHerbert Xu #define lower_32_bits(n) ((u32)((n) & 0xffffffff)) 191204b885eSJoerg Roedel 1921da177e4SLinus Torvalds struct completion; 193df2e71fbS[email protected] struct pt_regs; 194df2e71fbS[email protected] struct user; 1951da177e4SLinus Torvalds 196070cb065SUwe Kleine-König #ifdef CONFIG_PREEMPT_VOLUNTARY 197070cb065SUwe Kleine-König extern int _cond_resched(void); 198070cb065SUwe Kleine-König # define might_resched() _cond_resched() 199070cb065SUwe Kleine-König #else 200070cb065SUwe Kleine-König # define might_resched() do { } while (0) 201070cb065SUwe Kleine-König #endif 202070cb065SUwe Kleine-König 203d902db1eSFrederic Weisbecker #ifdef CONFIG_DEBUG_ATOMIC_SLEEP 204568f1967SPeter Zijlstra extern void ___might_sleep(const char *file, int line, int preempt_offset); 205568f1967SPeter Zijlstra extern void __might_sleep(const char *file, int line, int preempt_offset); 206568f1967SPeter Zijlstra extern void __cant_sleep(const char *file, int line, int preempt_offset); 207568f1967SPeter Zijlstra 2081da177e4SLinus Torvalds /** 2091da177e4SLinus Torvalds * might_sleep - annotation for functions that can sleep 2101da177e4SLinus Torvalds * 2111da177e4SLinus Torvalds * this macro will print a stack trace if it is executed in an atomic 212312364f3SDaniel Vetter * context (spinlock, irq-handler, ...). Additional sections where blocking is 213312364f3SDaniel Vetter * not allowed can be annotated with non_block_start() and non_block_end() 214312364f3SDaniel Vetter * pairs. 2151da177e4SLinus Torvalds * 2161da177e4SLinus Torvalds * This is a useful debugging help to be able to catch problems early and not 217e20ec991SJim Cromie * be bitten later when the calling function happens to sleep when it is not 2181da177e4SLinus Torvalds * supposed to. 2191da177e4SLinus Torvalds */ 220f8cbd99bSIngo Molnar # define might_sleep() \ 221e4aafea2SFrederic Weisbecker do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0) 222568f1967SPeter Zijlstra /** 223568f1967SPeter Zijlstra * cant_sleep - annotation for functions that cannot sleep 224568f1967SPeter Zijlstra * 225568f1967SPeter Zijlstra * this macro will print a stack trace if it is executed with preemption enabled 226568f1967SPeter Zijlstra */ 227568f1967SPeter Zijlstra # define cant_sleep() \ 228568f1967SPeter Zijlstra do { __cant_sleep(__FILE__, __LINE__, 0); } while (0) 22900845eb9SLinus Torvalds # define sched_annotate_sleep() (current->task_state_change = 0) 230312364f3SDaniel Vetter /** 231312364f3SDaniel Vetter * non_block_start - annotate the start of section where sleeping is prohibited 232312364f3SDaniel Vetter * 233312364f3SDaniel Vetter * This is on behalf of the oom reaper, specifically when it is calling the mmu 234312364f3SDaniel Vetter * notifiers. The problem is that if the notifier were to block on, for example, 235312364f3SDaniel Vetter * mutex_lock() and if the process which holds that mutex were to perform a 236312364f3SDaniel Vetter * sleeping memory allocation, the oom reaper is now blocked on completion of 237312364f3SDaniel Vetter * that memory allocation. Other blocking calls like wait_event() pose similar 238312364f3SDaniel Vetter * issues. 239312364f3SDaniel Vetter */ 240312364f3SDaniel Vetter # define non_block_start() (current->non_block_count++) 241312364f3SDaniel Vetter /** 242312364f3SDaniel Vetter * non_block_end - annotate the end of section where sleeping is prohibited 243312364f3SDaniel Vetter * 244312364f3SDaniel Vetter * Closes a section opened by non_block_start(). 245312364f3SDaniel Vetter */ 246312364f3SDaniel Vetter # define non_block_end() WARN_ON(current->non_block_count-- == 0) 247f8cbd99bSIngo Molnar #else 2483427445aSPeter Zijlstra static inline void ___might_sleep(const char *file, int line, 2493427445aSPeter Zijlstra int preempt_offset) { } 250d894837fSSimon Kagstrom static inline void __might_sleep(const char *file, int line, 251d894837fSSimon Kagstrom int preempt_offset) { } 252f8cbd99bSIngo Molnar # define might_sleep() do { might_resched(); } while (0) 253568f1967SPeter Zijlstra # define cant_sleep() do { } while (0) 2541029a2b5SPeter Zijlstra # define sched_annotate_sleep() do { } while (0) 255312364f3SDaniel Vetter # define non_block_start() do { } while (0) 256312364f3SDaniel Vetter # define non_block_end() do { } while (0) 257f8cbd99bSIngo Molnar #endif 258f8cbd99bSIngo Molnar 259368a5fa1SHua Zhong #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0) 260f8cbd99bSIngo Molnar 2614e139c77SThomas Gleixner #ifndef CONFIG_PREEMPT_RT 2624e139c77SThomas Gleixner # define cant_migrate() cant_sleep() 2634e139c77SThomas Gleixner #else 2644e139c77SThomas Gleixner /* Placeholder for now */ 2654e139c77SThomas Gleixner # define cant_migrate() do { } while (0) 2664e139c77SThomas Gleixner #endif 2674e139c77SThomas Gleixner 268c8299cb6SMichal Nazarewicz /** 269c8299cb6SMichal Nazarewicz * abs - return absolute value of an argument 2708f57e4d9SMichal Nazarewicz * @x: the value. If it is unsigned type, it is converted to signed type first. 2718f57e4d9SMichal Nazarewicz * char is treated as if it was signed (regardless of whether it really is) 2728f57e4d9SMichal Nazarewicz * but the macro's return type is preserved as char. 273c8299cb6SMichal Nazarewicz * 2748f57e4d9SMichal Nazarewicz * Return: an absolute value of x. 27571a90484SAndrew Morton */ 2768f57e4d9SMichal Nazarewicz #define abs(x) __abs_choose_expr(x, long long, \ 2778f57e4d9SMichal Nazarewicz __abs_choose_expr(x, long, \ 2788f57e4d9SMichal Nazarewicz __abs_choose_expr(x, int, \ 2798f57e4d9SMichal Nazarewicz __abs_choose_expr(x, short, \ 2808f57e4d9SMichal Nazarewicz __abs_choose_expr(x, char, \ 2818f57e4d9SMichal Nazarewicz __builtin_choose_expr( \ 2828f57e4d9SMichal Nazarewicz __builtin_types_compatible_p(typeof(x), char), \ 2838f57e4d9SMichal Nazarewicz (char)({ signed char __x = (x); __x<0?-__x:__x; }), \ 2848f57e4d9SMichal Nazarewicz ((void)0))))))) 2858f57e4d9SMichal Nazarewicz 2868f57e4d9SMichal Nazarewicz #define __abs_choose_expr(x, type, other) __builtin_choose_expr( \ 2878f57e4d9SMichal Nazarewicz __builtin_types_compatible_p(typeof(x), signed type) || \ 2888f57e4d9SMichal Nazarewicz __builtin_types_compatible_p(typeof(x), unsigned type), \ 2898f57e4d9SMichal Nazarewicz ({ signed type __x = (x); __x < 0 ? -__x : __x; }), other) 2901da177e4SLinus Torvalds 29189770b0aSDaniel Borkmann /** 29289770b0aSDaniel Borkmann * reciprocal_scale - "scale" a value into range [0, ep_ro) 29389770b0aSDaniel Borkmann * @val: value 29489770b0aSDaniel Borkmann * @ep_ro: right open interval endpoint 29589770b0aSDaniel Borkmann * 29689770b0aSDaniel Borkmann * Perform a "reciprocal multiplication" in order to "scale" a value into 297e8c97af0SRandy Dunlap * range [0, @ep_ro), where the upper interval endpoint is right-open. 29889770b0aSDaniel Borkmann * This is useful, e.g. for accessing a index of an array containing 299e8c97af0SRandy Dunlap * @ep_ro elements, for example. Think of it as sort of modulus, only that 30089770b0aSDaniel Borkmann * the result isn't that of modulo. ;) Note that if initial input is a 30189770b0aSDaniel Borkmann * small value, then result will return 0. 30289770b0aSDaniel Borkmann * 303e8c97af0SRandy Dunlap * Return: a result based on @val in interval [0, @ep_ro). 30489770b0aSDaniel Borkmann */ 30589770b0aSDaniel Borkmann static inline u32 reciprocal_scale(u32 val, u32 ep_ro) 30689770b0aSDaniel Borkmann { 30789770b0aSDaniel Borkmann return (u32)(((u64) val * ep_ro) >> 32); 30889770b0aSDaniel Borkmann } 30989770b0aSDaniel Borkmann 310386e7906SAxel Lin #if defined(CONFIG_MMU) && \ 311386e7906SAxel Lin (defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)) 3129ec23531SDavid Hildenbrand #define might_fault() __might_fault(__FILE__, __LINE__) 3139ec23531SDavid Hildenbrand void __might_fault(const char *file, int line); 3143ee1afa3SNick Piggin #else 315662bbcb2SMichael S. Tsirkin static inline void might_fault(void) { } 3163ee1afa3SNick Piggin #endif 3173ee1afa3SNick Piggin 318e041c683SAlan Stern extern struct atomic_notifier_head panic_notifier_list; 319c7ff0d9cSTAMUKI Shoichi extern long (*panic_blink)(int state); 3209402c95fSJoe Perches __printf(1, 2) 3219af6528eSPeter Zijlstra void panic(const char *fmt, ...) __noreturn __cold; 322ebc41f20SHidehiro Kawai void nmi_panic(struct pt_regs *regs, const char *msg); 323dd287796SAndrew Morton extern void oops_enter(void); 324dd287796SAndrew Morton extern void oops_exit(void); 32579076e12STiezhu Yang extern bool oops_may_print(void); 3269af6528eSPeter Zijlstra void do_exit(long error_code) __noreturn; 3279af6528eSPeter Zijlstra void complete_and_exit(struct completion *, long) __noreturn; 32833ee3b2eSAlexey Dobriyan 32933ee3b2eSAlexey Dobriyan /* Internal, do not use. */ 33033ee3b2eSAlexey Dobriyan int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res); 33133ee3b2eSAlexey Dobriyan int __must_check _kstrtol(const char *s, unsigned int base, long *res); 33233ee3b2eSAlexey Dobriyan 33333ee3b2eSAlexey Dobriyan int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res); 33433ee3b2eSAlexey Dobriyan int __must_check kstrtoll(const char *s, unsigned int base, long long *res); 3354c925d60SEldad Zack 3364c925d60SEldad Zack /** 3374c925d60SEldad Zack * kstrtoul - convert a string to an unsigned long 3384c925d60SEldad Zack * @s: The start of the string. The string must be null-terminated, and may also 3394c925d60SEldad Zack * include a single newline before its terminating null. The first character 3404c925d60SEldad Zack * may also be a plus sign, but not a minus sign. 3414c925d60SEldad Zack * @base: The number base to use. The maximum supported base is 16. If base is 3424c925d60SEldad Zack * given as 0, then the base of the string is automatically detected with the 3434c925d60SEldad Zack * conventional semantics - If it begins with 0x the number will be parsed as a 3444c925d60SEldad Zack * hexadecimal (case insensitive), if it otherwise begins with 0, it will be 3454c925d60SEldad Zack * parsed as an octal number. Otherwise it will be parsed as a decimal. 3464c925d60SEldad Zack * @res: Where to write the result of the conversion on success. 3474c925d60SEldad Zack * 3484c925d60SEldad Zack * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. 349ef0f2685SKars Mulder * Preferred over simple_strtoul(). Return code must be checked. 3504c925d60SEldad Zack */ 35133ee3b2eSAlexey Dobriyan static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res) 35233ee3b2eSAlexey Dobriyan { 35333ee3b2eSAlexey Dobriyan /* 35433ee3b2eSAlexey Dobriyan * We want to shortcut function call, but 35533ee3b2eSAlexey Dobriyan * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0. 35633ee3b2eSAlexey Dobriyan */ 35733ee3b2eSAlexey Dobriyan if (sizeof(unsigned long) == sizeof(unsigned long long) && 35833ee3b2eSAlexey Dobriyan __alignof__(unsigned long) == __alignof__(unsigned long long)) 35933ee3b2eSAlexey Dobriyan return kstrtoull(s, base, (unsigned long long *)res); 36033ee3b2eSAlexey Dobriyan else 36133ee3b2eSAlexey Dobriyan return _kstrtoul(s, base, res); 36233ee3b2eSAlexey Dobriyan } 36333ee3b2eSAlexey Dobriyan 3644c925d60SEldad Zack /** 3654c925d60SEldad Zack * kstrtol - convert a string to a long 3664c925d60SEldad Zack * @s: The start of the string. The string must be null-terminated, and may also 3674c925d60SEldad Zack * include a single newline before its terminating null. The first character 3684c925d60SEldad Zack * may also be a plus sign or a minus sign. 3694c925d60SEldad Zack * @base: The number base to use. The maximum supported base is 16. If base is 3704c925d60SEldad Zack * given as 0, then the base of the string is automatically detected with the 3714c925d60SEldad Zack * conventional semantics - If it begins with 0x the number will be parsed as a 3724c925d60SEldad Zack * hexadecimal (case insensitive), if it otherwise begins with 0, it will be 3734c925d60SEldad Zack * parsed as an octal number. Otherwise it will be parsed as a decimal. 3744c925d60SEldad Zack * @res: Where to write the result of the conversion on success. 3754c925d60SEldad Zack * 3764c925d60SEldad Zack * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. 377ef0f2685SKars Mulder * Preferred over simple_strtol(). Return code must be checked. 3784c925d60SEldad Zack */ 37933ee3b2eSAlexey Dobriyan static inline int __must_check kstrtol(const char *s, unsigned int base, long *res) 38033ee3b2eSAlexey Dobriyan { 38133ee3b2eSAlexey Dobriyan /* 38233ee3b2eSAlexey Dobriyan * We want to shortcut function call, but 38333ee3b2eSAlexey Dobriyan * __builtin_types_compatible_p(long, long long) = 0. 38433ee3b2eSAlexey Dobriyan */ 38533ee3b2eSAlexey Dobriyan if (sizeof(long) == sizeof(long long) && 38633ee3b2eSAlexey Dobriyan __alignof__(long) == __alignof__(long long)) 38733ee3b2eSAlexey Dobriyan return kstrtoll(s, base, (long long *)res); 38833ee3b2eSAlexey Dobriyan else 38933ee3b2eSAlexey Dobriyan return _kstrtol(s, base, res); 39033ee3b2eSAlexey Dobriyan } 39133ee3b2eSAlexey Dobriyan 39233ee3b2eSAlexey Dobriyan int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res); 39333ee3b2eSAlexey Dobriyan int __must_check kstrtoint(const char *s, unsigned int base, int *res); 39433ee3b2eSAlexey Dobriyan 39533ee3b2eSAlexey Dobriyan static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res) 39633ee3b2eSAlexey Dobriyan { 39733ee3b2eSAlexey Dobriyan return kstrtoull(s, base, res); 39833ee3b2eSAlexey Dobriyan } 39933ee3b2eSAlexey Dobriyan 40033ee3b2eSAlexey Dobriyan static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res) 40133ee3b2eSAlexey Dobriyan { 40233ee3b2eSAlexey Dobriyan return kstrtoll(s, base, res); 40333ee3b2eSAlexey Dobriyan } 40433ee3b2eSAlexey Dobriyan 40533ee3b2eSAlexey Dobriyan static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res) 40633ee3b2eSAlexey Dobriyan { 40733ee3b2eSAlexey Dobriyan return kstrtouint(s, base, res); 40833ee3b2eSAlexey Dobriyan } 40933ee3b2eSAlexey Dobriyan 41033ee3b2eSAlexey Dobriyan static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res) 41133ee3b2eSAlexey Dobriyan { 41233ee3b2eSAlexey Dobriyan return kstrtoint(s, base, res); 41333ee3b2eSAlexey Dobriyan } 41433ee3b2eSAlexey Dobriyan 41533ee3b2eSAlexey Dobriyan int __must_check kstrtou16(const char *s, unsigned int base, u16 *res); 41633ee3b2eSAlexey Dobriyan int __must_check kstrtos16(const char *s, unsigned int base, s16 *res); 41733ee3b2eSAlexey Dobriyan int __must_check kstrtou8(const char *s, unsigned int base, u8 *res); 41833ee3b2eSAlexey Dobriyan int __must_check kstrtos8(const char *s, unsigned int base, s8 *res); 419ef951599SKees Cook int __must_check kstrtobool(const char *s, bool *res); 42033ee3b2eSAlexey Dobriyan 421c196e32aSAlexey Dobriyan int __must_check kstrtoull_from_user(const char __user *s, size_t count, unsigned int base, unsigned long long *res); 422c196e32aSAlexey Dobriyan int __must_check kstrtoll_from_user(const char __user *s, size_t count, unsigned int base, long long *res); 423c196e32aSAlexey Dobriyan int __must_check kstrtoul_from_user(const char __user *s, size_t count, unsigned int base, unsigned long *res); 424c196e32aSAlexey Dobriyan int __must_check kstrtol_from_user(const char __user *s, size_t count, unsigned int base, long *res); 425c196e32aSAlexey Dobriyan int __must_check kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, unsigned int *res); 426c196e32aSAlexey Dobriyan int __must_check kstrtoint_from_user(const char __user *s, size_t count, unsigned int base, int *res); 427c196e32aSAlexey Dobriyan int __must_check kstrtou16_from_user(const char __user *s, size_t count, unsigned int base, u16 *res); 428c196e32aSAlexey Dobriyan int __must_check kstrtos16_from_user(const char __user *s, size_t count, unsigned int base, s16 *res); 429c196e32aSAlexey Dobriyan int __must_check kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, u8 *res); 430c196e32aSAlexey Dobriyan int __must_check kstrtos8_from_user(const char __user *s, size_t count, unsigned int base, s8 *res); 431ef951599SKees Cook int __must_check kstrtobool_from_user(const char __user *s, size_t count, bool *res); 432c196e32aSAlexey Dobriyan 433c196e32aSAlexey Dobriyan static inline int __must_check kstrtou64_from_user(const char __user *s, size_t count, unsigned int base, u64 *res) 434c196e32aSAlexey Dobriyan { 435c196e32aSAlexey Dobriyan return kstrtoull_from_user(s, count, base, res); 436c196e32aSAlexey Dobriyan } 437c196e32aSAlexey Dobriyan 438c196e32aSAlexey Dobriyan static inline int __must_check kstrtos64_from_user(const char __user *s, size_t count, unsigned int base, s64 *res) 439c196e32aSAlexey Dobriyan { 440c196e32aSAlexey Dobriyan return kstrtoll_from_user(s, count, base, res); 441c196e32aSAlexey Dobriyan } 442c196e32aSAlexey Dobriyan 443c196e32aSAlexey Dobriyan static inline int __must_check kstrtou32_from_user(const char __user *s, size_t count, unsigned int base, u32 *res) 444c196e32aSAlexey Dobriyan { 445c196e32aSAlexey Dobriyan return kstrtouint_from_user(s, count, base, res); 446c196e32aSAlexey Dobriyan } 447c196e32aSAlexey Dobriyan 448c196e32aSAlexey Dobriyan static inline int __must_check kstrtos32_from_user(const char __user *s, size_t count, unsigned int base, s32 *res) 449c196e32aSAlexey Dobriyan { 450c196e32aSAlexey Dobriyan return kstrtoint_from_user(s, count, base, res); 451c196e32aSAlexey Dobriyan } 452c196e32aSAlexey Dobriyan 453885e68e8SAndy Shevchenko /* 454885e68e8SAndy Shevchenko * Use kstrto<foo> instead. 455885e68e8SAndy Shevchenko * 456885e68e8SAndy Shevchenko * NOTE: simple_strto<foo> does not check for the range overflow and, 457885e68e8SAndy Shevchenko * depending on the input, may give interesting results. 458885e68e8SAndy Shevchenko * 459885e68e8SAndy Shevchenko * Use these functions if and only if you cannot use kstrto<foo>, because 460885e68e8SAndy Shevchenko * the conversion ends on the first non-digit character, which may be far 461885e68e8SAndy Shevchenko * beyond the supported range. It might be useful to parse the strings like 462885e68e8SAndy Shevchenko * 10x50 or 12:21 without altering original string or temporary buffer in use. 463885e68e8SAndy Shevchenko * Keep in mind above caveat. 464885e68e8SAndy Shevchenko */ 46567d0a075SJoe Perches 4661da177e4SLinus Torvalds extern unsigned long simple_strtoul(const char *,char **,unsigned int); 4671da177e4SLinus Torvalds extern long simple_strtol(const char *,char **,unsigned int); 4681da177e4SLinus Torvalds extern unsigned long long simple_strtoull(const char *,char **,unsigned int); 4691da177e4SLinus Torvalds extern long long simple_strtoll(const char *,char **,unsigned int); 47033ee3b2eSAlexey Dobriyan 471d1be35cbSAndrei Vagin extern int num_to_str(char *buf, int size, 472d1be35cbSAndrei Vagin unsigned long long num, unsigned int width); 4731ac101a5SKAMEZAWA Hiroyuki 47467d0a075SJoe Perches /* lib/printf utilities */ 47567d0a075SJoe Perches 476b9075fa9SJoe Perches extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...); 477b9075fa9SJoe Perches extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list); 478b9075fa9SJoe Perches extern __printf(3, 4) 479b9075fa9SJoe Perches int snprintf(char *buf, size_t size, const char *fmt, ...); 480b9075fa9SJoe Perches extern __printf(3, 0) 481b9075fa9SJoe Perches int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); 482b9075fa9SJoe Perches extern __printf(3, 4) 483b9075fa9SJoe Perches int scnprintf(char *buf, size_t size, const char *fmt, ...); 484b9075fa9SJoe Perches extern __printf(3, 0) 485b9075fa9SJoe Perches int vscnprintf(char *buf, size_t size, const char *fmt, va_list args); 48648a27055SRasmus Villemoes extern __printf(2, 3) __malloc 487b9075fa9SJoe Perches char *kasprintf(gfp_t gfp, const char *fmt, ...); 48848a27055SRasmus Villemoes extern __printf(2, 0) __malloc 4898db14860SNicolas Iooss char *kvasprintf(gfp_t gfp, const char *fmt, va_list args); 4900a9df786SRasmus Villemoes extern __printf(2, 0) 4910a9df786SRasmus Villemoes const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args); 4921da177e4SLinus Torvalds 4936061d949SJoe Perches extern __scanf(2, 3) 4946061d949SJoe Perches int sscanf(const char *, const char *, ...); 4956061d949SJoe Perches extern __scanf(2, 0) 4966061d949SJoe Perches int vsscanf(const char *, const char *, va_list); 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds extern int get_option(char **str, int *pint); 4991da177e4SLinus Torvalds extern char *get_options(const char *str, int nints, int *ints); 500d974ae37SJeremy Fitzhardinge extern unsigned long long memparse(const char *ptr, char **retptr); 5016ccc72b8SDave Young extern bool parse_option_str(const char *str, const char *option); 502f51b17c8SBaoquan He extern char *next_arg(char *args, char **param, char **val); 5031da177e4SLinus Torvalds 5045e376613STrent Piepho extern int core_kernel_text(unsigned long addr); 5059fbcc57aSJosh Poimboeuf extern int init_kernel_text(unsigned long addr); 506cdbe61bfSSteven Rostedt extern int core_kernel_data(unsigned long addr); 5071da177e4SLinus Torvalds extern int __kernel_text_address(unsigned long addr); 5081da177e4SLinus Torvalds extern int kernel_text_address(unsigned long addr); 509ab7476cfSArjan van de Ven extern int func_ptr_is_kernel_text(void *ptr); 510ab7476cfSArjan van de Ven 5119f615894SAndy Shevchenko u64 int_pow(u64 base, unsigned int exp); 5121da177e4SLinus Torvalds unsigned long int_sqrt(unsigned long); 5131da177e4SLinus Torvalds 51447a36163SCrt Mori #if BITS_PER_LONG < 64 51547a36163SCrt Mori u32 int_sqrt64(u64 x); 51647a36163SCrt Mori #else 51747a36163SCrt Mori static inline u32 int_sqrt64(u64 x) 51847a36163SCrt Mori { 51947a36163SCrt Mori return (u32)int_sqrt(x); 52047a36163SCrt Mori } 52147a36163SCrt Mori #endif 52247a36163SCrt Mori 52360c958d8SGuilherme G. Piccoli #ifdef CONFIG_SMP 52460c958d8SGuilherme G. Piccoli extern unsigned int sysctl_oops_all_cpu_backtrace; 52560c958d8SGuilherme G. Piccoli #else 52660c958d8SGuilherme G. Piccoli #define sysctl_oops_all_cpu_backtrace 0 52760c958d8SGuilherme G. Piccoli #endif /* CONFIG_SMP */ 52860c958d8SGuilherme G. Piccoli 5291da177e4SLinus Torvalds extern void bust_spinlocks(int yes); 530aa727107SAdrian Bunk extern int panic_timeout; 53181c9d43fSFeng Tang extern unsigned long panic_print; 5321da177e4SLinus Torvalds extern int panic_on_oops; 5338da5addaSDon Zickus extern int panic_on_unrecovered_nmi; 5345211a242SKurt Garloff extern int panic_on_io_nmi; 5359e3961a0SPrarit Bhargava extern int panic_on_warn; 536db38d5c1SRafael Aquini extern unsigned long panic_on_taint; 537db38d5c1SRafael Aquini extern bool panic_on_taint_nousertaint; 538088e9d25SDaniel Bristot de Oliveira extern int sysctl_panic_on_rcu_stall; 53955af7796SMitsuo Hayasaka extern int sysctl_panic_on_stackoverflow; 5405375b708SHATAYAMA Daisuke 5415375b708SHATAYAMA Daisuke extern bool crash_kexec_post_notifiers; 5425375b708SHATAYAMA Daisuke 5435800dc3cSJason Baron /* 5441717f209SHidehiro Kawai * panic_cpu is used for synchronizing panic() and crash_kexec() execution. It 5451717f209SHidehiro Kawai * holds a CPU number which is executing panic() currently. A value of 5461717f209SHidehiro Kawai * PANIC_CPU_INVALID means no CPU has entered panic() or crash_kexec(). 5471717f209SHidehiro Kawai */ 5481717f209SHidehiro Kawai extern atomic_t panic_cpu; 5491717f209SHidehiro Kawai #define PANIC_CPU_INVALID -1 5501717f209SHidehiro Kawai 5511717f209SHidehiro Kawai /* 5525800dc3cSJason Baron * Only to be used by arch init code. If the user over-wrote the default 5535800dc3cSJason Baron * CONFIG_PANIC_TIMEOUT, honor it. 5545800dc3cSJason Baron */ 5555800dc3cSJason Baron static inline void set_arch_panic_timeout(int timeout, int arch_default_timeout) 5565800dc3cSJason Baron { 5575800dc3cSJason Baron if (panic_timeout == arch_default_timeout) 5585800dc3cSJason Baron panic_timeout = timeout; 5595800dc3cSJason Baron } 5601da177e4SLinus Torvalds extern const char *print_tainted(void); 561373d4d09SRusty Russell enum lockdep_ok { 562373d4d09SRusty Russell LOCKDEP_STILL_OK, 563373d4d09SRusty Russell LOCKDEP_NOW_UNRELIABLE 564373d4d09SRusty Russell }; 565373d4d09SRusty Russell extern void add_taint(unsigned flag, enum lockdep_ok); 56625ddbb18SAndi Kleen extern int test_taint(unsigned flag); 56725ddbb18SAndi Kleen extern unsigned long get_taint(void); 568b920de1bSDavid Howells extern int root_mountflags; 5691da177e4SLinus Torvalds 5702ce802f6STejun Heo extern bool early_boot_irqs_disabled; 5712ce802f6STejun Heo 57269a78ff2SThomas Gleixner /* 57369a78ff2SThomas Gleixner * Values used for system_state. Ordering of the states must not be changed 57469a78ff2SThomas Gleixner * as code checks for <, <=, >, >= STATE. 57569a78ff2SThomas Gleixner */ 5761da177e4SLinus Torvalds extern enum system_states { 5771da177e4SLinus Torvalds SYSTEM_BOOTING, 57869a78ff2SThomas Gleixner SYSTEM_SCHEDULING, 5791da177e4SLinus Torvalds SYSTEM_RUNNING, 5801da177e4SLinus Torvalds SYSTEM_HALT, 5811da177e4SLinus Torvalds SYSTEM_POWER_OFF, 5821da177e4SLinus Torvalds SYSTEM_RESTART, 583c1a957d1SThomas Gleixner SYSTEM_SUSPEND, 5841da177e4SLinus Torvalds } system_state; 5851da177e4SLinus Torvalds 58647d4b263SKees Cook /* This cannot be an enum because some may be used in assembly source. */ 58725ddbb18SAndi Kleen #define TAINT_PROPRIETARY_MODULE 0 58825ddbb18SAndi Kleen #define TAINT_FORCED_MODULE 1 5898c90487cSDave Jones #define TAINT_CPU_OUT_OF_SPEC 2 59025ddbb18SAndi Kleen #define TAINT_FORCED_RMMOD 3 59125ddbb18SAndi Kleen #define TAINT_MACHINE_CHECK 4 59225ddbb18SAndi Kleen #define TAINT_BAD_PAGE 5 59325ddbb18SAndi Kleen #define TAINT_USER 6 59425ddbb18SAndi Kleen #define TAINT_DIE 7 59525ddbb18SAndi Kleen #define TAINT_OVERRIDDEN_ACPI_TABLE 8 59625ddbb18SAndi Kleen #define TAINT_WARN 9 59726e9a397SLinus Torvalds #define TAINT_CRAP 10 59892946bc7SBen Hutchings #define TAINT_FIRMWARE_WORKAROUND 11 5992449b8baSBen Hutchings #define TAINT_OOT_MODULE 12 60066cc69e3SMathieu Desnoyers #define TAINT_UNSIGNED_MODULE 13 60169361eefSJosh Hunt #define TAINT_SOFTLOCKUP 14 602c5f45465SSeth Jennings #define TAINT_LIVEPATCH 15 6034efb442cSBorislav Petkov #define TAINT_AUX 16 604bc4f2f54SKees Cook #define TAINT_RANDSTRUCT 17 605bc4f2f54SKees Cook #define TAINT_FLAGS_COUNT 18 606db38d5c1SRafael Aquini #define TAINT_FLAGS_MAX ((1UL << TAINT_FLAGS_COUNT) - 1) 6077fd8329bSPetr Mladek 6087fd8329bSPetr Mladek struct taint_flag { 6095eb7c0d0SLarry Finger char c_true; /* character printed when tainted */ 6105eb7c0d0SLarry Finger char c_false; /* character printed when not tainted */ 6117fd8329bSPetr Mladek bool module; /* also show as a per-module taint flag */ 6127fd8329bSPetr Mladek }; 6137fd8329bSPetr Mladek 6147fd8329bSPetr Mladek extern const struct taint_flag taint_flags[TAINT_FLAGS_COUNT]; 6151da177e4SLinus Torvalds 6163fc95772SHarvey Harrison extern const char hex_asc[]; 6173fc95772SHarvey Harrison #define hex_asc_lo(x) hex_asc[((x) & 0x0f)] 6183fc95772SHarvey Harrison #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4] 6193fc95772SHarvey Harrison 62055036ba7SAndy Shevchenko static inline char *hex_byte_pack(char *buf, u8 byte) 6213fc95772SHarvey Harrison { 6223fc95772SHarvey Harrison *buf++ = hex_asc_hi(byte); 6233fc95772SHarvey Harrison *buf++ = hex_asc_lo(byte); 6243fc95772SHarvey Harrison return buf; 6253fc95772SHarvey Harrison } 62699eaf3c4SRandy Dunlap 627c26d436cSAndre Naujoks extern const char hex_asc_upper[]; 628c26d436cSAndre Naujoks #define hex_asc_upper_lo(x) hex_asc_upper[((x) & 0x0f)] 629c26d436cSAndre Naujoks #define hex_asc_upper_hi(x) hex_asc_upper[((x) & 0xf0) >> 4] 630c26d436cSAndre Naujoks 631c26d436cSAndre Naujoks static inline char *hex_byte_pack_upper(char *buf, u8 byte) 632c26d436cSAndre Naujoks { 633c26d436cSAndre Naujoks *buf++ = hex_asc_upper_hi(byte); 634c26d436cSAndre Naujoks *buf++ = hex_asc_upper_lo(byte); 635c26d436cSAndre Naujoks return buf; 636c26d436cSAndre Naujoks } 637c26d436cSAndre Naujoks 63890378889SAndy Shevchenko extern int hex_to_bin(char ch); 639b7804983SMimi Zohar extern int __must_check hex2bin(u8 *dst, const char *src, size_t count); 64053d91c5cSDavid Howells extern char *bin2hex(char *dst, const void *src, size_t count); 64190378889SAndy Shevchenko 642a69f5edbSJoe Perches bool mac_pton(const char *s, u8 *mac); 6434cd5773aSAndy Shevchenko 6448a64f336SJoe Perches /* 645526211bcSIngo Molnar * General tracing related utility functions - trace_printk(), 6462002c258SSteven Rostedt * tracing_on/tracing_off and tracing_start()/tracing_stop 6472002c258SSteven Rostedt * 6482002c258SSteven Rostedt * Use tracing_on/tracing_off when you want to quickly turn on or off 6492002c258SSteven Rostedt * tracing. It simply enables or disables the recording of the trace events. 650156f5a78SGeunSik Lim * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on 6512002c258SSteven Rostedt * file, which gives a means for the kernel and userspace to interact. 6522002c258SSteven Rostedt * Place a tracing_off() in the kernel where you want tracing to end. 6532002c258SSteven Rostedt * From user space, examine the trace, and then echo 1 > tracing_on 6542002c258SSteven Rostedt * to continue tracing. 6552002c258SSteven Rostedt * 6562002c258SSteven Rostedt * tracing_stop/tracing_start has slightly more overhead. It is used 6572002c258SSteven Rostedt * by things like suspend to ram where disabling the recording of the 6582002c258SSteven Rostedt * trace is not enough, but tracing must actually stop because things 6592002c258SSteven Rostedt * like calling smp_processor_id() may crash the system. 6602002c258SSteven Rostedt * 6612002c258SSteven Rostedt * Most likely, you want to use tracing_on/tracing_off. 662526211bcSIngo Molnar */ 663cecbca96SFrederic Weisbecker 664cecbca96SFrederic Weisbecker enum ftrace_dump_mode { 665cecbca96SFrederic Weisbecker DUMP_NONE, 666cecbca96SFrederic Weisbecker DUMP_ALL, 667cecbca96SFrederic Weisbecker DUMP_ORIG, 668cecbca96SFrederic Weisbecker }; 669cecbca96SFrederic Weisbecker 670526211bcSIngo Molnar #ifdef CONFIG_TRACING 67193d68e52SSteven Rostedt void tracing_on(void); 67293d68e52SSteven Rostedt void tracing_off(void); 67393d68e52SSteven Rostedt int tracing_is_on(void); 674ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot(void); 675ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot_alloc(void); 67693d68e52SSteven Rostedt 677526211bcSIngo Molnar extern void tracing_start(void); 678526211bcSIngo Molnar extern void tracing_stop(void); 679526211bcSIngo Molnar 680b9075fa9SJoe Perches static inline __printf(1, 2) 681b9075fa9SJoe Perches void ____trace_printk_check_format(const char *fmt, ...) 682769b0441SFrederic Weisbecker { 683769b0441SFrederic Weisbecker } 684769b0441SFrederic Weisbecker #define __trace_printk_check_format(fmt, args...) \ 685769b0441SFrederic Weisbecker do { \ 686769b0441SFrederic Weisbecker if (0) \ 687769b0441SFrederic Weisbecker ____trace_printk_check_format(fmt, ##args); \ 688769b0441SFrederic Weisbecker } while (0) 689769b0441SFrederic Weisbecker 690526211bcSIngo Molnar /** 691526211bcSIngo Molnar * trace_printk - printf formatting in the ftrace buffer 692526211bcSIngo Molnar * @fmt: the printf format for printing 693526211bcSIngo Molnar * 694e8c97af0SRandy Dunlap * Note: __trace_printk is an internal function for trace_printk() and 695e8c97af0SRandy Dunlap * the @ip is passed in via the trace_printk() macro. 696526211bcSIngo Molnar * 697526211bcSIngo Molnar * This function allows a kernel developer to debug fast path sections 698526211bcSIngo Molnar * that printk is not appropriate for. By scattering in various 699526211bcSIngo Molnar * printk like tracing in the code, a developer can quickly see 700526211bcSIngo Molnar * where problems are occurring. 701526211bcSIngo Molnar * 702526211bcSIngo Molnar * This is intended as a debugging tool for the developer only. 703526211bcSIngo Molnar * Please refrain from leaving trace_printks scattered around in 70409ae7234SSteven Rostedt (Red Hat) * your code. (Extra memory is used for special buffers that are 705e8c97af0SRandy Dunlap * allocated when trace_printk() is used.) 7069d3c752cSSteven Rostedt (Red Hat) * 7078730662dSWei Wang * A little optimization trick is done here. If there's only one 7089d3c752cSSteven Rostedt (Red Hat) * argument, there's no need to scan the string for printf formats. 7099d3c752cSSteven Rostedt (Red Hat) * The trace_puts() will suffice. But how can we take advantage of 7109d3c752cSSteven Rostedt (Red Hat) * using trace_puts() when trace_printk() has only one argument? 7119d3c752cSSteven Rostedt (Red Hat) * By stringifying the args and checking the size we can tell 7129d3c752cSSteven Rostedt (Red Hat) * whether or not there are args. __stringify((__VA_ARGS__)) will 7139d3c752cSSteven Rostedt (Red Hat) * turn into "()\0" with a size of 3 when there are no args, anything 7149d3c752cSSteven Rostedt (Red Hat) * else will be bigger. All we need to do is define a string to this, 7159d3c752cSSteven Rostedt (Red Hat) * and then take its size and compare to 3. If it's bigger, use 7169d3c752cSSteven Rostedt (Red Hat) * do_trace_printk() otherwise, optimize it to trace_puts(). Then just 7179d3c752cSSteven Rostedt (Red Hat) * let gcc optimize the rest. 718526211bcSIngo Molnar */ 719769b0441SFrederic Weisbecker 7209d3c752cSSteven Rostedt (Red Hat) #define trace_printk(fmt, ...) \ 7219d3c752cSSteven Rostedt (Red Hat) do { \ 7229d3c752cSSteven Rostedt (Red Hat) char _______STR[] = __stringify((__VA_ARGS__)); \ 7239d3c752cSSteven Rostedt (Red Hat) if (sizeof(_______STR) > 3) \ 7249d3c752cSSteven Rostedt (Red Hat) do_trace_printk(fmt, ##__VA_ARGS__); \ 7259d3c752cSSteven Rostedt (Red Hat) else \ 7269d3c752cSSteven Rostedt (Red Hat) trace_puts(fmt); \ 7279d3c752cSSteven Rostedt (Red Hat) } while (0) 7289d3c752cSSteven Rostedt (Red Hat) 7299d3c752cSSteven Rostedt (Red Hat) #define do_trace_printk(fmt, args...) \ 730769b0441SFrederic Weisbecker do { \ 7313debb0a9SSteven Rostedt (Red Hat) static const char *trace_printk_fmt __used \ 73248ead020SFrederic Weisbecker __attribute__((section("__trace_printk_fmt"))) = \ 73348ead020SFrederic Weisbecker __builtin_constant_p(fmt) ? fmt : NULL; \ 73448ead020SFrederic Weisbecker \ 73507d777feSSteven Rostedt __trace_printk_check_format(fmt, ##args); \ 73607d777feSSteven Rostedt \ 73707d777feSSteven Rostedt if (__builtin_constant_p(fmt)) \ 73848ead020SFrederic Weisbecker __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \ 73907d777feSSteven Rostedt else \ 74048ead020SFrederic Weisbecker __trace_printk(_THIS_IP_, fmt, ##args); \ 741769b0441SFrederic Weisbecker } while (0) 742769b0441SFrederic Weisbecker 743b9075fa9SJoe Perches extern __printf(2, 3) 744b9075fa9SJoe Perches int __trace_bprintk(unsigned long ip, const char *fmt, ...); 74548ead020SFrederic Weisbecker 746b9075fa9SJoe Perches extern __printf(2, 3) 747b9075fa9SJoe Perches int __trace_printk(unsigned long ip, const char *fmt, ...); 748769b0441SFrederic Weisbecker 74909ae7234SSteven Rostedt (Red Hat) /** 75009ae7234SSteven Rostedt (Red Hat) * trace_puts - write a string into the ftrace buffer 75109ae7234SSteven Rostedt (Red Hat) * @str: the string to record 75209ae7234SSteven Rostedt (Red Hat) * 75309ae7234SSteven Rostedt (Red Hat) * Note: __trace_bputs is an internal function for trace_puts and 75409ae7234SSteven Rostedt (Red Hat) * the @ip is passed in via the trace_puts macro. 75509ae7234SSteven Rostedt (Red Hat) * 75609ae7234SSteven Rostedt (Red Hat) * This is similar to trace_printk() but is made for those really fast 757e8c97af0SRandy Dunlap * paths that a developer wants the least amount of "Heisenbug" effects, 75809ae7234SSteven Rostedt (Red Hat) * where the processing of the print format is still too much. 75909ae7234SSteven Rostedt (Red Hat) * 76009ae7234SSteven Rostedt (Red Hat) * This function allows a kernel developer to debug fast path sections 76109ae7234SSteven Rostedt (Red Hat) * that printk is not appropriate for. By scattering in various 76209ae7234SSteven Rostedt (Red Hat) * printk like tracing in the code, a developer can quickly see 76309ae7234SSteven Rostedt (Red Hat) * where problems are occurring. 76409ae7234SSteven Rostedt (Red Hat) * 76509ae7234SSteven Rostedt (Red Hat) * This is intended as a debugging tool for the developer only. 76609ae7234SSteven Rostedt (Red Hat) * Please refrain from leaving trace_puts scattered around in 76709ae7234SSteven Rostedt (Red Hat) * your code. (Extra memory is used for special buffers that are 768e8c97af0SRandy Dunlap * allocated when trace_puts() is used.) 76909ae7234SSteven Rostedt (Red Hat) * 77009ae7234SSteven Rostedt (Red Hat) * Returns: 0 if nothing was written, positive # if string was. 77109ae7234SSteven Rostedt (Red Hat) * (1 when __trace_bputs is used, strlen(str) when __trace_puts is used) 77209ae7234SSteven Rostedt (Red Hat) */ 77309ae7234SSteven Rostedt (Red Hat) 77409ae7234SSteven Rostedt (Red Hat) #define trace_puts(str) ({ \ 7753debb0a9SSteven Rostedt (Red Hat) static const char *trace_printk_fmt __used \ 77609ae7234SSteven Rostedt (Red Hat) __attribute__((section("__trace_printk_fmt"))) = \ 77709ae7234SSteven Rostedt (Red Hat) __builtin_constant_p(str) ? str : NULL; \ 77809ae7234SSteven Rostedt (Red Hat) \ 77909ae7234SSteven Rostedt (Red Hat) if (__builtin_constant_p(str)) \ 78009ae7234SSteven Rostedt (Red Hat) __trace_bputs(_THIS_IP_, trace_printk_fmt); \ 78109ae7234SSteven Rostedt (Red Hat) else \ 78209ae7234SSteven Rostedt (Red Hat) __trace_puts(_THIS_IP_, str, strlen(str)); \ 78309ae7234SSteven Rostedt (Red Hat) }) 784bcf312cfSSteven Rostedt extern int __trace_bputs(unsigned long ip, const char *str); 785bcf312cfSSteven Rostedt extern int __trace_puts(unsigned long ip, const char *str, int size); 78609ae7234SSteven Rostedt (Red Hat) 787c142be8eSSteven Rostedt (Red Hat) extern void trace_dump_stack(int skip); 78803889384SSteven Rostedt 78948ead020SFrederic Weisbecker /* 79048ead020SFrederic Weisbecker * The double __builtin_constant_p is because gcc will give us an error 79148ead020SFrederic Weisbecker * if we try to allocate the static variable to fmt if it is not a 79248ead020SFrederic Weisbecker * constant. Even with the outer if statement. 79348ead020SFrederic Weisbecker */ 794769b0441SFrederic Weisbecker #define ftrace_vprintk(fmt, vargs) \ 795769b0441SFrederic Weisbecker do { \ 79648ead020SFrederic Weisbecker if (__builtin_constant_p(fmt)) { \ 7973debb0a9SSteven Rostedt (Red Hat) static const char *trace_printk_fmt __used \ 79848ead020SFrederic Weisbecker __attribute__((section("__trace_printk_fmt"))) = \ 79948ead020SFrederic Weisbecker __builtin_constant_p(fmt) ? fmt : NULL; \ 8007bffc23eSIngo Molnar \ 80148ead020SFrederic Weisbecker __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \ 80248ead020SFrederic Weisbecker } else \ 80348ead020SFrederic Weisbecker __ftrace_vprintk(_THIS_IP_, fmt, vargs); \ 804769b0441SFrederic Weisbecker } while (0) 805769b0441SFrederic Weisbecker 8068db14860SNicolas Iooss extern __printf(2, 0) int 80748ead020SFrederic Weisbecker __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap); 80848ead020SFrederic Weisbecker 8098db14860SNicolas Iooss extern __printf(2, 0) int 810526211bcSIngo Molnar __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap); 811769b0441SFrederic Weisbecker 812cecbca96SFrederic Weisbecker extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode); 813526211bcSIngo Molnar #else 814526211bcSIngo Molnar static inline void tracing_start(void) { } 815526211bcSIngo Molnar static inline void tracing_stop(void) { } 816e67bc51eSDhaval Giani static inline void trace_dump_stack(int skip) { } 81793d68e52SSteven Rostedt 81893d68e52SSteven Rostedt static inline void tracing_on(void) { } 81993d68e52SSteven Rostedt static inline void tracing_off(void) { } 82093d68e52SSteven Rostedt static inline int tracing_is_on(void) { return 0; } 821ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot(void) { } 822ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot_alloc(void) { } 82393d68e52SSteven Rostedt 82460efc15aSMichal Hocko static inline __printf(1, 2) 82560efc15aSMichal Hocko int trace_printk(const char *fmt, ...) 826526211bcSIngo Molnar { 827526211bcSIngo Molnar return 0; 828526211bcSIngo Molnar } 8298db14860SNicolas Iooss static __printf(1, 0) inline int 830526211bcSIngo Molnar ftrace_vprintk(const char *fmt, va_list ap) 831526211bcSIngo Molnar { 832526211bcSIngo Molnar return 0; 833526211bcSIngo Molnar } 834cecbca96SFrederic Weisbecker static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { } 835769b0441SFrederic Weisbecker #endif /* CONFIG_TRACING */ 836526211bcSIngo Molnar 837cf14f27fSAlexei Starovoitov /* This counts to 12. Any more, it will return 13th argument. */ 838cf14f27fSAlexei Starovoitov #define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n 839cf14f27fSAlexei Starovoitov #define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) 840cf14f27fSAlexei Starovoitov 841cf14f27fSAlexei Starovoitov #define __CONCAT(a, b) a ## b 842cf14f27fSAlexei Starovoitov #define CONCATENATE(a, b) __CONCAT(a, b) 843cf14f27fSAlexei Starovoitov 8441da177e4SLinus Torvalds /** 8451da177e4SLinus Torvalds * container_of - cast a member of a structure out to the containing structure 8461da177e4SLinus Torvalds * @ptr: the pointer to the member. 8471da177e4SLinus Torvalds * @type: the type of the container struct this is embedded in. 8481da177e4SLinus Torvalds * @member: the name of the member within the struct. 8491da177e4SLinus Torvalds * 8501da177e4SLinus Torvalds */ 8511da177e4SLinus Torvalds #define container_of(ptr, type, member) ({ \ 852c7acec71SIan Abbott void *__mptr = (void *)(ptr); \ 853c7acec71SIan Abbott BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) && \ 854c7acec71SIan Abbott !__same_type(*(ptr), void), \ 855c7acec71SIan Abbott "pointer type mismatch in container_of()"); \ 856c7acec71SIan Abbott ((type *)(__mptr - offsetof(type, member))); }) 8571da177e4SLinus Torvalds 85805e6557bSNeilBrown /** 85905e6557bSNeilBrown * container_of_safe - cast a member of a structure out to the containing structure 86005e6557bSNeilBrown * @ptr: the pointer to the member. 86105e6557bSNeilBrown * @type: the type of the container struct this is embedded in. 86205e6557bSNeilBrown * @member: the name of the member within the struct. 86305e6557bSNeilBrown * 86405e6557bSNeilBrown * If IS_ERR_OR_NULL(ptr), ptr is returned unchanged. 86505e6557bSNeilBrown */ 86605e6557bSNeilBrown #define container_of_safe(ptr, type, member) ({ \ 86705e6557bSNeilBrown void *__mptr = (void *)(ptr); \ 86805e6557bSNeilBrown BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) && \ 86905e6557bSNeilBrown !__same_type(*(ptr), void), \ 87005e6557bSNeilBrown "pointer type mismatch in container_of()"); \ 871227abcc6SDan Carpenter IS_ERR_OR_NULL(__mptr) ? ERR_CAST(__mptr) : \ 87205e6557bSNeilBrown ((type *)(__mptr - offsetof(type, member))); }) 87305e6557bSNeilBrown 874b9d4f426SArnaud Lacombe /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */ 875b9d4f426SArnaud Lacombe #ifdef CONFIG_FTRACE_MCOUNT_RECORD 876b9d4f426SArnaud Lacombe # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD 877b9d4f426SArnaud Lacombe #endif 8789d00f92fSWANG Cong 87958f86cc8SRusty Russell /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */ 88058f86cc8SRusty Russell #define VERIFY_OCTAL_PERMISSIONS(perms) \ 88158f86cc8SRusty Russell (BUILD_BUG_ON_ZERO((perms) < 0) + \ 88258f86cc8SRusty Russell BUILD_BUG_ON_ZERO((perms) > 0777) + \ 88328b8d0c8SGobinda Charan Maji /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */ \ 88428b8d0c8SGobinda Charan Maji BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) + \ 88528b8d0c8SGobinda Charan Maji BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) + \ 88628b8d0c8SGobinda Charan Maji /* USER_WRITABLE >= GROUP_WRITABLE */ \ 88728b8d0c8SGobinda Charan Maji BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) + \ 88828b8d0c8SGobinda Charan Maji /* OTHER_WRITABLE? Generally considered a bad idea. */ \ 88937549e94SRusty Russell BUILD_BUG_ON_ZERO((perms) & 2) + \ 89058f86cc8SRusty Russell (perms)) 8911da177e4SLinus Torvalds #endif 892