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 #include <stdarg.h> 608c5188eSAndy Shevchenko #include <linux/align.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> 14aa6159abSAndy Shevchenko #include <linux/math.h> 15b296a6d5SAndy Shevchenko #include <linux/minmax.h> 16e0deaff4SAndrew Morton #include <linux/typecheck.h> 17*f39650deSAndy Shevchenko #include <linux/panic.h> 18968ab183SLinus Torvalds #include <linux/printk.h> 19c7acec71SIan Abbott #include <linux/build_bug.h> 20b965f1ddSPeter Zijlstra (Intel) #include <linux/static_call_types.h> 211da177e4SLinus Torvalds #include <asm/byteorder.h> 22aa6159abSAndy Shevchenko 23607ca46eSDavid Howells #include <uapi/linux/kernel.h> 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds #define STACK_MAGIC 0xdeadbeef 261da177e4SLinus Torvalds 27e8c97af0SRandy Dunlap /** 28e8c97af0SRandy Dunlap * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value 29e8c97af0SRandy Dunlap * @x: value to repeat 30e8c97af0SRandy Dunlap * 31e8c97af0SRandy Dunlap * NOTE: @x is not checked for > 0xff; larger values produce odd results. 32e8c97af0SRandy Dunlap */ 3344696908SDavid S. Miller #define REPEAT_BYTE(x) ((~0ul / 0xff) * (x)) 3444696908SDavid S. Miller 35d3849953SChristoph Hellwig /* generic data direction definitions */ 36d3849953SChristoph Hellwig #define READ 0 37d3849953SChristoph Hellwig #define WRITE 1 38d3849953SChristoph Hellwig 39e8c97af0SRandy Dunlap /** 40e8c97af0SRandy Dunlap * ARRAY_SIZE - get the number of elements in array @arr 41e8c97af0SRandy Dunlap * @arr: array to be sized 42e8c97af0SRandy Dunlap */ 43c5e631cfSRusty Russell #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) 44c5e631cfSRusty Russell 450ab1438bSMasahiro Yamada #define PTR_IF(cond, ptr) ((cond) ? (ptr) : NULL) 460ab1438bSMasahiro Yamada 473ed605bcSGustavo Padovan #define u64_to_user_ptr(x) ( \ 483ed605bcSGustavo Padovan { \ 49a0fe2c64SJann Horn typecheck(u64, (x)); \ 50a0fe2c64SJann Horn (void __user *)(uintptr_t)(x); \ 513ed605bcSGustavo Padovan } \ 523ed605bcSGustavo Padovan ) 533ed605bcSGustavo Padovan 54ce251e0eSAlexey Dobriyan #define typeof_member(T, m) typeof(((T*)0)->m) 55ce251e0eSAlexey Dobriyan 56ca31e146SEduard - Gabriel Munteanu #define _RET_IP_ (unsigned long)__builtin_return_address(0) 57ca31e146SEduard - Gabriel Munteanu #define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; }) 58ca31e146SEduard - Gabriel Munteanu 59218e180eSAndrew Morton /** 60218e180eSAndrew Morton * upper_32_bits - return bits 32-63 of a number 61218e180eSAndrew Morton * @n: the number we're accessing 62218e180eSAndrew Morton * 63218e180eSAndrew Morton * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress 64218e180eSAndrew Morton * the "right shift count >= width of type" warning when that quantity is 65218e180eSAndrew Morton * 32-bits. 66218e180eSAndrew Morton */ 67218e180eSAndrew Morton #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16)) 68218e180eSAndrew Morton 69204b885eSJoerg Roedel /** 70204b885eSJoerg Roedel * lower_32_bits - return bits 0-31 of a number 71204b885eSJoerg Roedel * @n: the number we're accessing 72204b885eSJoerg Roedel */ 73ef91bb19SHerbert Xu #define lower_32_bits(n) ((u32)((n) & 0xffffffff)) 74204b885eSJoerg Roedel 751da177e4SLinus Torvalds struct completion; 76df2e71fbS[email protected] struct user; 771da177e4SLinus Torvalds 78070cb065SUwe Kleine-König #ifdef CONFIG_PREEMPT_VOLUNTARY 79b965f1ddSPeter Zijlstra (Intel) 80b965f1ddSPeter Zijlstra (Intel) extern int __cond_resched(void); 81b965f1ddSPeter Zijlstra (Intel) # define might_resched() __cond_resched() 82b965f1ddSPeter Zijlstra (Intel) 83b965f1ddSPeter Zijlstra (Intel) #elif defined(CONFIG_PREEMPT_DYNAMIC) 84b965f1ddSPeter Zijlstra (Intel) 85b965f1ddSPeter Zijlstra (Intel) extern int __cond_resched(void); 86b965f1ddSPeter Zijlstra (Intel) 87b965f1ddSPeter Zijlstra (Intel) DECLARE_STATIC_CALL(might_resched, __cond_resched); 88b965f1ddSPeter Zijlstra (Intel) 89b965f1ddSPeter Zijlstra (Intel) static __always_inline void might_resched(void) 90b965f1ddSPeter Zijlstra (Intel) { 91ef72661eSPeter Zijlstra static_call_mod(might_resched)(); 92b965f1ddSPeter Zijlstra (Intel) } 93b965f1ddSPeter Zijlstra (Intel) 94070cb065SUwe Kleine-König #else 95b965f1ddSPeter Zijlstra (Intel) 96070cb065SUwe Kleine-König # define might_resched() do { } while (0) 97b965f1ddSPeter Zijlstra (Intel) 98b965f1ddSPeter Zijlstra (Intel) #endif /* CONFIG_PREEMPT_* */ 99070cb065SUwe Kleine-König 100d902db1eSFrederic Weisbecker #ifdef CONFIG_DEBUG_ATOMIC_SLEEP 101568f1967SPeter Zijlstra extern void ___might_sleep(const char *file, int line, int preempt_offset); 102568f1967SPeter Zijlstra extern void __might_sleep(const char *file, int line, int preempt_offset); 103568f1967SPeter Zijlstra extern void __cant_sleep(const char *file, int line, int preempt_offset); 10474d862b6SThomas Gleixner extern void __cant_migrate(const char *file, int line); 105568f1967SPeter Zijlstra 1061da177e4SLinus Torvalds /** 1071da177e4SLinus Torvalds * might_sleep - annotation for functions that can sleep 1081da177e4SLinus Torvalds * 1091da177e4SLinus Torvalds * this macro will print a stack trace if it is executed in an atomic 110312364f3SDaniel Vetter * context (spinlock, irq-handler, ...). Additional sections where blocking is 111312364f3SDaniel Vetter * not allowed can be annotated with non_block_start() and non_block_end() 112312364f3SDaniel Vetter * pairs. 1131da177e4SLinus Torvalds * 1141da177e4SLinus Torvalds * This is a useful debugging help to be able to catch problems early and not 115e20ec991SJim Cromie * be bitten later when the calling function happens to sleep when it is not 1161da177e4SLinus Torvalds * supposed to. 1171da177e4SLinus Torvalds */ 118f8cbd99bSIngo Molnar # define might_sleep() \ 119e4aafea2SFrederic Weisbecker do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0) 120568f1967SPeter Zijlstra /** 121568f1967SPeter Zijlstra * cant_sleep - annotation for functions that cannot sleep 122568f1967SPeter Zijlstra * 123568f1967SPeter Zijlstra * this macro will print a stack trace if it is executed with preemption enabled 124568f1967SPeter Zijlstra */ 125568f1967SPeter Zijlstra # define cant_sleep() \ 126568f1967SPeter Zijlstra do { __cant_sleep(__FILE__, __LINE__, 0); } while (0) 12700845eb9SLinus Torvalds # define sched_annotate_sleep() (current->task_state_change = 0) 12874d862b6SThomas Gleixner 12974d862b6SThomas Gleixner /** 13074d862b6SThomas Gleixner * cant_migrate - annotation for functions that cannot migrate 13174d862b6SThomas Gleixner * 13274d862b6SThomas Gleixner * Will print a stack trace if executed in code which is migratable 13374d862b6SThomas Gleixner */ 13474d862b6SThomas Gleixner # define cant_migrate() \ 13574d862b6SThomas Gleixner do { \ 13674d862b6SThomas Gleixner if (IS_ENABLED(CONFIG_SMP)) \ 13774d862b6SThomas Gleixner __cant_migrate(__FILE__, __LINE__); \ 13874d862b6SThomas Gleixner } while (0) 13974d862b6SThomas Gleixner 140312364f3SDaniel Vetter /** 141312364f3SDaniel Vetter * non_block_start - annotate the start of section where sleeping is prohibited 142312364f3SDaniel Vetter * 143312364f3SDaniel Vetter * This is on behalf of the oom reaper, specifically when it is calling the mmu 144312364f3SDaniel Vetter * notifiers. The problem is that if the notifier were to block on, for example, 145312364f3SDaniel Vetter * mutex_lock() and if the process which holds that mutex were to perform a 146312364f3SDaniel Vetter * sleeping memory allocation, the oom reaper is now blocked on completion of 147312364f3SDaniel Vetter * that memory allocation. Other blocking calls like wait_event() pose similar 148312364f3SDaniel Vetter * issues. 149312364f3SDaniel Vetter */ 150312364f3SDaniel Vetter # define non_block_start() (current->non_block_count++) 151312364f3SDaniel Vetter /** 152312364f3SDaniel Vetter * non_block_end - annotate the end of section where sleeping is prohibited 153312364f3SDaniel Vetter * 154312364f3SDaniel Vetter * Closes a section opened by non_block_start(). 155312364f3SDaniel Vetter */ 156312364f3SDaniel Vetter # define non_block_end() WARN_ON(current->non_block_count-- == 0) 157f8cbd99bSIngo Molnar #else 1583427445aSPeter Zijlstra static inline void ___might_sleep(const char *file, int line, 1593427445aSPeter Zijlstra int preempt_offset) { } 160d894837fSSimon Kagstrom static inline void __might_sleep(const char *file, int line, 161d894837fSSimon Kagstrom int preempt_offset) { } 162f8cbd99bSIngo Molnar # define might_sleep() do { might_resched(); } while (0) 163568f1967SPeter Zijlstra # define cant_sleep() do { } while (0) 16474d862b6SThomas Gleixner # define cant_migrate() do { } while (0) 1651029a2b5SPeter Zijlstra # define sched_annotate_sleep() do { } while (0) 166312364f3SDaniel Vetter # define non_block_start() do { } while (0) 167312364f3SDaniel Vetter # define non_block_end() do { } while (0) 168f8cbd99bSIngo Molnar #endif 169f8cbd99bSIngo Molnar 170368a5fa1SHua Zhong #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0) 171f8cbd99bSIngo Molnar 172386e7906SAxel Lin #if defined(CONFIG_MMU) && \ 173386e7906SAxel Lin (defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)) 1749ec23531SDavid Hildenbrand #define might_fault() __might_fault(__FILE__, __LINE__) 1759ec23531SDavid Hildenbrand void __might_fault(const char *file, int line); 1763ee1afa3SNick Piggin #else 177662bbcb2SMichael S. Tsirkin static inline void might_fault(void) { } 1783ee1afa3SNick Piggin #endif 1793ee1afa3SNick Piggin 1809af6528eSPeter Zijlstra void do_exit(long error_code) __noreturn; 1819af6528eSPeter Zijlstra void complete_and_exit(struct completion *, long) __noreturn; 18233ee3b2eSAlexey Dobriyan 18333ee3b2eSAlexey Dobriyan /* Internal, do not use. */ 18433ee3b2eSAlexey Dobriyan int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res); 18533ee3b2eSAlexey Dobriyan int __must_check _kstrtol(const char *s, unsigned int base, long *res); 18633ee3b2eSAlexey Dobriyan 18733ee3b2eSAlexey Dobriyan int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res); 18833ee3b2eSAlexey Dobriyan int __must_check kstrtoll(const char *s, unsigned int base, long long *res); 1894c925d60SEldad Zack 1904c925d60SEldad Zack /** 1914c925d60SEldad Zack * kstrtoul - convert a string to an unsigned long 1924c925d60SEldad Zack * @s: The start of the string. The string must be null-terminated, and may also 1934c925d60SEldad Zack * include a single newline before its terminating null. The first character 1944c925d60SEldad Zack * may also be a plus sign, but not a minus sign. 1954c925d60SEldad Zack * @base: The number base to use. The maximum supported base is 16. If base is 1964c925d60SEldad Zack * given as 0, then the base of the string is automatically detected with the 1974c925d60SEldad Zack * conventional semantics - If it begins with 0x the number will be parsed as a 1984c925d60SEldad Zack * hexadecimal (case insensitive), if it otherwise begins with 0, it will be 1994c925d60SEldad Zack * parsed as an octal number. Otherwise it will be parsed as a decimal. 2004c925d60SEldad Zack * @res: Where to write the result of the conversion on success. 2014c925d60SEldad Zack * 2024c925d60SEldad Zack * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. 203ef0f2685SKars Mulder * Preferred over simple_strtoul(). Return code must be checked. 2044c925d60SEldad Zack */ 20533ee3b2eSAlexey Dobriyan static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res) 20633ee3b2eSAlexey Dobriyan { 20733ee3b2eSAlexey Dobriyan /* 20833ee3b2eSAlexey Dobriyan * We want to shortcut function call, but 20933ee3b2eSAlexey Dobriyan * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0. 21033ee3b2eSAlexey Dobriyan */ 21133ee3b2eSAlexey Dobriyan if (sizeof(unsigned long) == sizeof(unsigned long long) && 21233ee3b2eSAlexey Dobriyan __alignof__(unsigned long) == __alignof__(unsigned long long)) 21333ee3b2eSAlexey Dobriyan return kstrtoull(s, base, (unsigned long long *)res); 21433ee3b2eSAlexey Dobriyan else 21533ee3b2eSAlexey Dobriyan return _kstrtoul(s, base, res); 21633ee3b2eSAlexey Dobriyan } 21733ee3b2eSAlexey Dobriyan 2184c925d60SEldad Zack /** 2194c925d60SEldad Zack * kstrtol - convert a string to a long 2204c925d60SEldad Zack * @s: The start of the string. The string must be null-terminated, and may also 2214c925d60SEldad Zack * include a single newline before its terminating null. The first character 2224c925d60SEldad Zack * may also be a plus sign or a minus sign. 2234c925d60SEldad Zack * @base: The number base to use. The maximum supported base is 16. If base is 2244c925d60SEldad Zack * given as 0, then the base of the string is automatically detected with the 2254c925d60SEldad Zack * conventional semantics - If it begins with 0x the number will be parsed as a 2264c925d60SEldad Zack * hexadecimal (case insensitive), if it otherwise begins with 0, it will be 2274c925d60SEldad Zack * parsed as an octal number. Otherwise it will be parsed as a decimal. 2284c925d60SEldad Zack * @res: Where to write the result of the conversion on success. 2294c925d60SEldad Zack * 2304c925d60SEldad Zack * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. 231ef0f2685SKars Mulder * Preferred over simple_strtol(). Return code must be checked. 2324c925d60SEldad Zack */ 23333ee3b2eSAlexey Dobriyan static inline int __must_check kstrtol(const char *s, unsigned int base, long *res) 23433ee3b2eSAlexey Dobriyan { 23533ee3b2eSAlexey Dobriyan /* 23633ee3b2eSAlexey Dobriyan * We want to shortcut function call, but 23733ee3b2eSAlexey Dobriyan * __builtin_types_compatible_p(long, long long) = 0. 23833ee3b2eSAlexey Dobriyan */ 23933ee3b2eSAlexey Dobriyan if (sizeof(long) == sizeof(long long) && 24033ee3b2eSAlexey Dobriyan __alignof__(long) == __alignof__(long long)) 24133ee3b2eSAlexey Dobriyan return kstrtoll(s, base, (long long *)res); 24233ee3b2eSAlexey Dobriyan else 24333ee3b2eSAlexey Dobriyan return _kstrtol(s, base, res); 24433ee3b2eSAlexey Dobriyan } 24533ee3b2eSAlexey Dobriyan 24633ee3b2eSAlexey Dobriyan int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res); 24733ee3b2eSAlexey Dobriyan int __must_check kstrtoint(const char *s, unsigned int base, int *res); 24833ee3b2eSAlexey Dobriyan 24933ee3b2eSAlexey Dobriyan static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res) 25033ee3b2eSAlexey Dobriyan { 25133ee3b2eSAlexey Dobriyan return kstrtoull(s, base, res); 25233ee3b2eSAlexey Dobriyan } 25333ee3b2eSAlexey Dobriyan 25433ee3b2eSAlexey Dobriyan static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res) 25533ee3b2eSAlexey Dobriyan { 25633ee3b2eSAlexey Dobriyan return kstrtoll(s, base, res); 25733ee3b2eSAlexey Dobriyan } 25833ee3b2eSAlexey Dobriyan 25933ee3b2eSAlexey Dobriyan static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res) 26033ee3b2eSAlexey Dobriyan { 26133ee3b2eSAlexey Dobriyan return kstrtouint(s, base, res); 26233ee3b2eSAlexey Dobriyan } 26333ee3b2eSAlexey Dobriyan 26433ee3b2eSAlexey Dobriyan static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res) 26533ee3b2eSAlexey Dobriyan { 26633ee3b2eSAlexey Dobriyan return kstrtoint(s, base, res); 26733ee3b2eSAlexey Dobriyan } 26833ee3b2eSAlexey Dobriyan 26933ee3b2eSAlexey Dobriyan int __must_check kstrtou16(const char *s, unsigned int base, u16 *res); 27033ee3b2eSAlexey Dobriyan int __must_check kstrtos16(const char *s, unsigned int base, s16 *res); 27133ee3b2eSAlexey Dobriyan int __must_check kstrtou8(const char *s, unsigned int base, u8 *res); 27233ee3b2eSAlexey Dobriyan int __must_check kstrtos8(const char *s, unsigned int base, s8 *res); 273ef951599SKees Cook int __must_check kstrtobool(const char *s, bool *res); 27433ee3b2eSAlexey Dobriyan 275c196e32aSAlexey Dobriyan int __must_check kstrtoull_from_user(const char __user *s, size_t count, unsigned int base, unsigned long long *res); 276c196e32aSAlexey Dobriyan int __must_check kstrtoll_from_user(const char __user *s, size_t count, unsigned int base, long long *res); 277c196e32aSAlexey Dobriyan int __must_check kstrtoul_from_user(const char __user *s, size_t count, unsigned int base, unsigned long *res); 278c196e32aSAlexey Dobriyan int __must_check kstrtol_from_user(const char __user *s, size_t count, unsigned int base, long *res); 279c196e32aSAlexey Dobriyan int __must_check kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, unsigned int *res); 280c196e32aSAlexey Dobriyan int __must_check kstrtoint_from_user(const char __user *s, size_t count, unsigned int base, int *res); 281c196e32aSAlexey Dobriyan int __must_check kstrtou16_from_user(const char __user *s, size_t count, unsigned int base, u16 *res); 282c196e32aSAlexey Dobriyan int __must_check kstrtos16_from_user(const char __user *s, size_t count, unsigned int base, s16 *res); 283c196e32aSAlexey Dobriyan int __must_check kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, u8 *res); 284c196e32aSAlexey Dobriyan int __must_check kstrtos8_from_user(const char __user *s, size_t count, unsigned int base, s8 *res); 285ef951599SKees Cook int __must_check kstrtobool_from_user(const char __user *s, size_t count, bool *res); 286c196e32aSAlexey Dobriyan 287c196e32aSAlexey Dobriyan static inline int __must_check kstrtou64_from_user(const char __user *s, size_t count, unsigned int base, u64 *res) 288c196e32aSAlexey Dobriyan { 289c196e32aSAlexey Dobriyan return kstrtoull_from_user(s, count, base, res); 290c196e32aSAlexey Dobriyan } 291c196e32aSAlexey Dobriyan 292c196e32aSAlexey Dobriyan static inline int __must_check kstrtos64_from_user(const char __user *s, size_t count, unsigned int base, s64 *res) 293c196e32aSAlexey Dobriyan { 294c196e32aSAlexey Dobriyan return kstrtoll_from_user(s, count, base, res); 295c196e32aSAlexey Dobriyan } 296c196e32aSAlexey Dobriyan 297c196e32aSAlexey Dobriyan static inline int __must_check kstrtou32_from_user(const char __user *s, size_t count, unsigned int base, u32 *res) 298c196e32aSAlexey Dobriyan { 299c196e32aSAlexey Dobriyan return kstrtouint_from_user(s, count, base, res); 300c196e32aSAlexey Dobriyan } 301c196e32aSAlexey Dobriyan 302c196e32aSAlexey Dobriyan static inline int __must_check kstrtos32_from_user(const char __user *s, size_t count, unsigned int base, s32 *res) 303c196e32aSAlexey Dobriyan { 304c196e32aSAlexey Dobriyan return kstrtoint_from_user(s, count, base, res); 305c196e32aSAlexey Dobriyan } 306c196e32aSAlexey Dobriyan 307885e68e8SAndy Shevchenko /* 308885e68e8SAndy Shevchenko * Use kstrto<foo> instead. 309885e68e8SAndy Shevchenko * 310885e68e8SAndy Shevchenko * NOTE: simple_strto<foo> does not check for the range overflow and, 311885e68e8SAndy Shevchenko * depending on the input, may give interesting results. 312885e68e8SAndy Shevchenko * 313885e68e8SAndy Shevchenko * Use these functions if and only if you cannot use kstrto<foo>, because 314885e68e8SAndy Shevchenko * the conversion ends on the first non-digit character, which may be far 315885e68e8SAndy Shevchenko * beyond the supported range. It might be useful to parse the strings like 316885e68e8SAndy Shevchenko * 10x50 or 12:21 without altering original string or temporary buffer in use. 317885e68e8SAndy Shevchenko * Keep in mind above caveat. 318885e68e8SAndy Shevchenko */ 31967d0a075SJoe Perches 3201da177e4SLinus Torvalds extern unsigned long simple_strtoul(const char *,char **,unsigned int); 3211da177e4SLinus Torvalds extern long simple_strtol(const char *,char **,unsigned int); 3221da177e4SLinus Torvalds extern unsigned long long simple_strtoull(const char *,char **,unsigned int); 3231da177e4SLinus Torvalds extern long long simple_strtoll(const char *,char **,unsigned int); 32433ee3b2eSAlexey Dobriyan 325d1be35cbSAndrei Vagin extern int num_to_str(char *buf, int size, 326d1be35cbSAndrei Vagin unsigned long long num, unsigned int width); 3271ac101a5SKAMEZAWA Hiroyuki 32867d0a075SJoe Perches /* lib/printf utilities */ 32967d0a075SJoe Perches 330b9075fa9SJoe Perches extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...); 331b9075fa9SJoe Perches extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list); 332b9075fa9SJoe Perches extern __printf(3, 4) 333b9075fa9SJoe Perches int snprintf(char *buf, size_t size, const char *fmt, ...); 334b9075fa9SJoe Perches extern __printf(3, 0) 335b9075fa9SJoe Perches int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); 336b9075fa9SJoe Perches extern __printf(3, 4) 337b9075fa9SJoe Perches int scnprintf(char *buf, size_t size, const char *fmt, ...); 338b9075fa9SJoe Perches extern __printf(3, 0) 339b9075fa9SJoe Perches int vscnprintf(char *buf, size_t size, const char *fmt, va_list args); 34048a27055SRasmus Villemoes extern __printf(2, 3) __malloc 341b9075fa9SJoe Perches char *kasprintf(gfp_t gfp, const char *fmt, ...); 34248a27055SRasmus Villemoes extern __printf(2, 0) __malloc 3438db14860SNicolas Iooss char *kvasprintf(gfp_t gfp, const char *fmt, va_list args); 3440a9df786SRasmus Villemoes extern __printf(2, 0) 3450a9df786SRasmus Villemoes const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args); 3461da177e4SLinus Torvalds 3476061d949SJoe Perches extern __scanf(2, 3) 3486061d949SJoe Perches int sscanf(const char *, const char *, ...); 3496061d949SJoe Perches extern __scanf(2, 0) 3506061d949SJoe Perches int vsscanf(const char *, const char *, va_list); 3511da177e4SLinus Torvalds 35279270291SStephen Boyd extern int no_hash_pointers_enable(char *str); 35379270291SStephen Boyd 3541da177e4SLinus Torvalds extern int get_option(char **str, int *pint); 3551da177e4SLinus Torvalds extern char *get_options(const char *str, int nints, int *ints); 356d974ae37SJeremy Fitzhardinge extern unsigned long long memparse(const char *ptr, char **retptr); 3576ccc72b8SDave Young extern bool parse_option_str(const char *str, const char *option); 358f51b17c8SBaoquan He extern char *next_arg(char *args, char **param, char **val); 3591da177e4SLinus Torvalds 3605e376613STrent Piepho extern int core_kernel_text(unsigned long addr); 3619fbcc57aSJosh Poimboeuf extern int init_kernel_text(unsigned long addr); 362cdbe61bfSSteven Rostedt extern int core_kernel_data(unsigned long addr); 3631da177e4SLinus Torvalds extern int __kernel_text_address(unsigned long addr); 3641da177e4SLinus Torvalds extern int kernel_text_address(unsigned long addr); 365ab7476cfSArjan van de Ven extern int func_ptr_is_kernel_text(void *ptr); 366ab7476cfSArjan van de Ven 3671da177e4SLinus Torvalds extern void bust_spinlocks(int yes); 3685375b708SHATAYAMA Daisuke 369b920de1bSDavid Howells extern int root_mountflags; 3701da177e4SLinus Torvalds 3712ce802f6STejun Heo extern bool early_boot_irqs_disabled; 3722ce802f6STejun Heo 37369a78ff2SThomas Gleixner /* 37469a78ff2SThomas Gleixner * Values used for system_state. Ordering of the states must not be changed 37569a78ff2SThomas Gleixner * as code checks for <, <=, >, >= STATE. 37669a78ff2SThomas Gleixner */ 3771da177e4SLinus Torvalds extern enum system_states { 3781da177e4SLinus Torvalds SYSTEM_BOOTING, 37969a78ff2SThomas Gleixner SYSTEM_SCHEDULING, 3801da177e4SLinus Torvalds SYSTEM_RUNNING, 3811da177e4SLinus Torvalds SYSTEM_HALT, 3821da177e4SLinus Torvalds SYSTEM_POWER_OFF, 3831da177e4SLinus Torvalds SYSTEM_RESTART, 384c1a957d1SThomas Gleixner SYSTEM_SUSPEND, 3851da177e4SLinus Torvalds } system_state; 3861da177e4SLinus Torvalds 3873fc95772SHarvey Harrison extern const char hex_asc[]; 3883fc95772SHarvey Harrison #define hex_asc_lo(x) hex_asc[((x) & 0x0f)] 3893fc95772SHarvey Harrison #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4] 3903fc95772SHarvey Harrison 39155036ba7SAndy Shevchenko static inline char *hex_byte_pack(char *buf, u8 byte) 3923fc95772SHarvey Harrison { 3933fc95772SHarvey Harrison *buf++ = hex_asc_hi(byte); 3943fc95772SHarvey Harrison *buf++ = hex_asc_lo(byte); 3953fc95772SHarvey Harrison return buf; 3963fc95772SHarvey Harrison } 39799eaf3c4SRandy Dunlap 398c26d436cSAndre Naujoks extern const char hex_asc_upper[]; 399c26d436cSAndre Naujoks #define hex_asc_upper_lo(x) hex_asc_upper[((x) & 0x0f)] 400c26d436cSAndre Naujoks #define hex_asc_upper_hi(x) hex_asc_upper[((x) & 0xf0) >> 4] 401c26d436cSAndre Naujoks 402c26d436cSAndre Naujoks static inline char *hex_byte_pack_upper(char *buf, u8 byte) 403c26d436cSAndre Naujoks { 404c26d436cSAndre Naujoks *buf++ = hex_asc_upper_hi(byte); 405c26d436cSAndre Naujoks *buf++ = hex_asc_upper_lo(byte); 406c26d436cSAndre Naujoks return buf; 407c26d436cSAndre Naujoks } 408c26d436cSAndre Naujoks 40990378889SAndy Shevchenko extern int hex_to_bin(char ch); 410b7804983SMimi Zohar extern int __must_check hex2bin(u8 *dst, const char *src, size_t count); 41153d91c5cSDavid Howells extern char *bin2hex(char *dst, const void *src, size_t count); 41290378889SAndy Shevchenko 413a69f5edbSJoe Perches bool mac_pton(const char *s, u8 *mac); 4144cd5773aSAndy Shevchenko 4158a64f336SJoe Perches /* 416526211bcSIngo Molnar * General tracing related utility functions - trace_printk(), 4172002c258SSteven Rostedt * tracing_on/tracing_off and tracing_start()/tracing_stop 4182002c258SSteven Rostedt * 4192002c258SSteven Rostedt * Use tracing_on/tracing_off when you want to quickly turn on or off 4202002c258SSteven Rostedt * tracing. It simply enables or disables the recording of the trace events. 421156f5a78SGeunSik Lim * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on 4222002c258SSteven Rostedt * file, which gives a means for the kernel and userspace to interact. 4232002c258SSteven Rostedt * Place a tracing_off() in the kernel where you want tracing to end. 4242002c258SSteven Rostedt * From user space, examine the trace, and then echo 1 > tracing_on 4252002c258SSteven Rostedt * to continue tracing. 4262002c258SSteven Rostedt * 4272002c258SSteven Rostedt * tracing_stop/tracing_start has slightly more overhead. It is used 4282002c258SSteven Rostedt * by things like suspend to ram where disabling the recording of the 4292002c258SSteven Rostedt * trace is not enough, but tracing must actually stop because things 4302002c258SSteven Rostedt * like calling smp_processor_id() may crash the system. 4312002c258SSteven Rostedt * 4322002c258SSteven Rostedt * Most likely, you want to use tracing_on/tracing_off. 433526211bcSIngo Molnar */ 434cecbca96SFrederic Weisbecker 435cecbca96SFrederic Weisbecker enum ftrace_dump_mode { 436cecbca96SFrederic Weisbecker DUMP_NONE, 437cecbca96SFrederic Weisbecker DUMP_ALL, 438cecbca96SFrederic Weisbecker DUMP_ORIG, 439cecbca96SFrederic Weisbecker }; 440cecbca96SFrederic Weisbecker 441526211bcSIngo Molnar #ifdef CONFIG_TRACING 44293d68e52SSteven Rostedt void tracing_on(void); 44393d68e52SSteven Rostedt void tracing_off(void); 44493d68e52SSteven Rostedt int tracing_is_on(void); 445ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot(void); 446ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot_alloc(void); 44793d68e52SSteven Rostedt 448526211bcSIngo Molnar extern void tracing_start(void); 449526211bcSIngo Molnar extern void tracing_stop(void); 450526211bcSIngo Molnar 451b9075fa9SJoe Perches static inline __printf(1, 2) 452b9075fa9SJoe Perches void ____trace_printk_check_format(const char *fmt, ...) 453769b0441SFrederic Weisbecker { 454769b0441SFrederic Weisbecker } 455769b0441SFrederic Weisbecker #define __trace_printk_check_format(fmt, args...) \ 456769b0441SFrederic Weisbecker do { \ 457769b0441SFrederic Weisbecker if (0) \ 458769b0441SFrederic Weisbecker ____trace_printk_check_format(fmt, ##args); \ 459769b0441SFrederic Weisbecker } while (0) 460769b0441SFrederic Weisbecker 461526211bcSIngo Molnar /** 462526211bcSIngo Molnar * trace_printk - printf formatting in the ftrace buffer 463526211bcSIngo Molnar * @fmt: the printf format for printing 464526211bcSIngo Molnar * 465e8c97af0SRandy Dunlap * Note: __trace_printk is an internal function for trace_printk() and 466e8c97af0SRandy Dunlap * the @ip is passed in via the trace_printk() macro. 467526211bcSIngo Molnar * 468526211bcSIngo Molnar * This function allows a kernel developer to debug fast path sections 469526211bcSIngo Molnar * that printk is not appropriate for. By scattering in various 470526211bcSIngo Molnar * printk like tracing in the code, a developer can quickly see 471526211bcSIngo Molnar * where problems are occurring. 472526211bcSIngo Molnar * 473526211bcSIngo Molnar * This is intended as a debugging tool for the developer only. 474526211bcSIngo Molnar * Please refrain from leaving trace_printks scattered around in 47509ae7234SSteven Rostedt (Red Hat) * your code. (Extra memory is used for special buffers that are 476e8c97af0SRandy Dunlap * allocated when trace_printk() is used.) 4779d3c752cSSteven Rostedt (Red Hat) * 4788730662dSWei Wang * A little optimization trick is done here. If there's only one 4799d3c752cSSteven Rostedt (Red Hat) * argument, there's no need to scan the string for printf formats. 4809d3c752cSSteven Rostedt (Red Hat) * The trace_puts() will suffice. But how can we take advantage of 4819d3c752cSSteven Rostedt (Red Hat) * using trace_puts() when trace_printk() has only one argument? 4829d3c752cSSteven Rostedt (Red Hat) * By stringifying the args and checking the size we can tell 4839d3c752cSSteven Rostedt (Red Hat) * whether or not there are args. __stringify((__VA_ARGS__)) will 4849d3c752cSSteven Rostedt (Red Hat) * turn into "()\0" with a size of 3 when there are no args, anything 4859d3c752cSSteven Rostedt (Red Hat) * else will be bigger. All we need to do is define a string to this, 4869d3c752cSSteven Rostedt (Red Hat) * and then take its size and compare to 3. If it's bigger, use 4879d3c752cSSteven Rostedt (Red Hat) * do_trace_printk() otherwise, optimize it to trace_puts(). Then just 4889d3c752cSSteven Rostedt (Red Hat) * let gcc optimize the rest. 489526211bcSIngo Molnar */ 490769b0441SFrederic Weisbecker 4919d3c752cSSteven Rostedt (Red Hat) #define trace_printk(fmt, ...) \ 4929d3c752cSSteven Rostedt (Red Hat) do { \ 4939d3c752cSSteven Rostedt (Red Hat) char _______STR[] = __stringify((__VA_ARGS__)); \ 4949d3c752cSSteven Rostedt (Red Hat) if (sizeof(_______STR) > 3) \ 4959d3c752cSSteven Rostedt (Red Hat) do_trace_printk(fmt, ##__VA_ARGS__); \ 4969d3c752cSSteven Rostedt (Red Hat) else \ 4979d3c752cSSteven Rostedt (Red Hat) trace_puts(fmt); \ 4989d3c752cSSteven Rostedt (Red Hat) } while (0) 4999d3c752cSSteven Rostedt (Red Hat) 5009d3c752cSSteven Rostedt (Red Hat) #define do_trace_printk(fmt, args...) \ 501769b0441SFrederic Weisbecker do { \ 5023debb0a9SSteven Rostedt (Red Hat) static const char *trace_printk_fmt __used \ 50333def849SJoe Perches __section("__trace_printk_fmt") = \ 50448ead020SFrederic Weisbecker __builtin_constant_p(fmt) ? fmt : NULL; \ 50548ead020SFrederic Weisbecker \ 50607d777feSSteven Rostedt __trace_printk_check_format(fmt, ##args); \ 50707d777feSSteven Rostedt \ 50807d777feSSteven Rostedt if (__builtin_constant_p(fmt)) \ 50948ead020SFrederic Weisbecker __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \ 51007d777feSSteven Rostedt else \ 51148ead020SFrederic Weisbecker __trace_printk(_THIS_IP_, fmt, ##args); \ 512769b0441SFrederic Weisbecker } while (0) 513769b0441SFrederic Weisbecker 514b9075fa9SJoe Perches extern __printf(2, 3) 515b9075fa9SJoe Perches int __trace_bprintk(unsigned long ip, const char *fmt, ...); 51648ead020SFrederic Weisbecker 517b9075fa9SJoe Perches extern __printf(2, 3) 518b9075fa9SJoe Perches int __trace_printk(unsigned long ip, const char *fmt, ...); 519769b0441SFrederic Weisbecker 52009ae7234SSteven Rostedt (Red Hat) /** 52109ae7234SSteven Rostedt (Red Hat) * trace_puts - write a string into the ftrace buffer 52209ae7234SSteven Rostedt (Red Hat) * @str: the string to record 52309ae7234SSteven Rostedt (Red Hat) * 52409ae7234SSteven Rostedt (Red Hat) * Note: __trace_bputs is an internal function for trace_puts and 52509ae7234SSteven Rostedt (Red Hat) * the @ip is passed in via the trace_puts macro. 52609ae7234SSteven Rostedt (Red Hat) * 52709ae7234SSteven Rostedt (Red Hat) * This is similar to trace_printk() but is made for those really fast 528e8c97af0SRandy Dunlap * paths that a developer wants the least amount of "Heisenbug" effects, 52909ae7234SSteven Rostedt (Red Hat) * where the processing of the print format is still too much. 53009ae7234SSteven Rostedt (Red Hat) * 53109ae7234SSteven Rostedt (Red Hat) * This function allows a kernel developer to debug fast path sections 53209ae7234SSteven Rostedt (Red Hat) * that printk is not appropriate for. By scattering in various 53309ae7234SSteven Rostedt (Red Hat) * printk like tracing in the code, a developer can quickly see 53409ae7234SSteven Rostedt (Red Hat) * where problems are occurring. 53509ae7234SSteven Rostedt (Red Hat) * 53609ae7234SSteven Rostedt (Red Hat) * This is intended as a debugging tool for the developer only. 53709ae7234SSteven Rostedt (Red Hat) * Please refrain from leaving trace_puts scattered around in 53809ae7234SSteven Rostedt (Red Hat) * your code. (Extra memory is used for special buffers that are 539e8c97af0SRandy Dunlap * allocated when trace_puts() is used.) 54009ae7234SSteven Rostedt (Red Hat) * 54109ae7234SSteven Rostedt (Red Hat) * Returns: 0 if nothing was written, positive # if string was. 54209ae7234SSteven Rostedt (Red Hat) * (1 when __trace_bputs is used, strlen(str) when __trace_puts is used) 54309ae7234SSteven Rostedt (Red Hat) */ 54409ae7234SSteven Rostedt (Red Hat) 54509ae7234SSteven Rostedt (Red Hat) #define trace_puts(str) ({ \ 5463debb0a9SSteven Rostedt (Red Hat) static const char *trace_printk_fmt __used \ 54733def849SJoe Perches __section("__trace_printk_fmt") = \ 54809ae7234SSteven Rostedt (Red Hat) __builtin_constant_p(str) ? str : NULL; \ 54909ae7234SSteven Rostedt (Red Hat) \ 55009ae7234SSteven Rostedt (Red Hat) if (__builtin_constant_p(str)) \ 55109ae7234SSteven Rostedt (Red Hat) __trace_bputs(_THIS_IP_, trace_printk_fmt); \ 55209ae7234SSteven Rostedt (Red Hat) else \ 55309ae7234SSteven Rostedt (Red Hat) __trace_puts(_THIS_IP_, str, strlen(str)); \ 55409ae7234SSteven Rostedt (Red Hat) }) 555bcf312cfSSteven Rostedt extern int __trace_bputs(unsigned long ip, const char *str); 556bcf312cfSSteven Rostedt extern int __trace_puts(unsigned long ip, const char *str, int size); 55709ae7234SSteven Rostedt (Red Hat) 558c142be8eSSteven Rostedt (Red Hat) extern void trace_dump_stack(int skip); 55903889384SSteven Rostedt 56048ead020SFrederic Weisbecker /* 56148ead020SFrederic Weisbecker * The double __builtin_constant_p is because gcc will give us an error 56248ead020SFrederic Weisbecker * if we try to allocate the static variable to fmt if it is not a 56348ead020SFrederic Weisbecker * constant. Even with the outer if statement. 56448ead020SFrederic Weisbecker */ 565769b0441SFrederic Weisbecker #define ftrace_vprintk(fmt, vargs) \ 566769b0441SFrederic Weisbecker do { \ 56748ead020SFrederic Weisbecker if (__builtin_constant_p(fmt)) { \ 5683debb0a9SSteven Rostedt (Red Hat) static const char *trace_printk_fmt __used \ 56933def849SJoe Perches __section("__trace_printk_fmt") = \ 57048ead020SFrederic Weisbecker __builtin_constant_p(fmt) ? fmt : NULL; \ 5717bffc23eSIngo Molnar \ 57248ead020SFrederic Weisbecker __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \ 57348ead020SFrederic Weisbecker } else \ 57448ead020SFrederic Weisbecker __ftrace_vprintk(_THIS_IP_, fmt, vargs); \ 575769b0441SFrederic Weisbecker } while (0) 576769b0441SFrederic Weisbecker 5778db14860SNicolas Iooss extern __printf(2, 0) int 57848ead020SFrederic Weisbecker __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap); 57948ead020SFrederic Weisbecker 5808db14860SNicolas Iooss extern __printf(2, 0) int 581526211bcSIngo Molnar __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap); 582769b0441SFrederic Weisbecker 583cecbca96SFrederic Weisbecker extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode); 584526211bcSIngo Molnar #else 585526211bcSIngo Molnar static inline void tracing_start(void) { } 586526211bcSIngo Molnar static inline void tracing_stop(void) { } 587e67bc51eSDhaval Giani static inline void trace_dump_stack(int skip) { } 58893d68e52SSteven Rostedt 58993d68e52SSteven Rostedt static inline void tracing_on(void) { } 59093d68e52SSteven Rostedt static inline void tracing_off(void) { } 59193d68e52SSteven Rostedt static inline int tracing_is_on(void) { return 0; } 592ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot(void) { } 593ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot_alloc(void) { } 59493d68e52SSteven Rostedt 59560efc15aSMichal Hocko static inline __printf(1, 2) 59660efc15aSMichal Hocko int trace_printk(const char *fmt, ...) 597526211bcSIngo Molnar { 598526211bcSIngo Molnar return 0; 599526211bcSIngo Molnar } 6008db14860SNicolas Iooss static __printf(1, 0) inline int 601526211bcSIngo Molnar ftrace_vprintk(const char *fmt, va_list ap) 602526211bcSIngo Molnar { 603526211bcSIngo Molnar return 0; 604526211bcSIngo Molnar } 605cecbca96SFrederic Weisbecker static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { } 606769b0441SFrederic Weisbecker #endif /* CONFIG_TRACING */ 607526211bcSIngo Molnar 608cf14f27fSAlexei Starovoitov /* This counts to 12. Any more, it will return 13th argument. */ 609cf14f27fSAlexei Starovoitov #define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n 610cf14f27fSAlexei Starovoitov #define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) 611cf14f27fSAlexei Starovoitov 612cf14f27fSAlexei Starovoitov #define __CONCAT(a, b) a ## b 613cf14f27fSAlexei Starovoitov #define CONCATENATE(a, b) __CONCAT(a, b) 614cf14f27fSAlexei Starovoitov 6151da177e4SLinus Torvalds /** 6161da177e4SLinus Torvalds * container_of - cast a member of a structure out to the containing structure 6171da177e4SLinus Torvalds * @ptr: the pointer to the member. 6181da177e4SLinus Torvalds * @type: the type of the container struct this is embedded in. 6191da177e4SLinus Torvalds * @member: the name of the member within the struct. 6201da177e4SLinus Torvalds * 6211da177e4SLinus Torvalds */ 6221da177e4SLinus Torvalds #define container_of(ptr, type, member) ({ \ 623c7acec71SIan Abbott void *__mptr = (void *)(ptr); \ 624c7acec71SIan Abbott BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) && \ 625c7acec71SIan Abbott !__same_type(*(ptr), void), \ 626c7acec71SIan Abbott "pointer type mismatch in container_of()"); \ 627c7acec71SIan Abbott ((type *)(__mptr - offsetof(type, member))); }) 6281da177e4SLinus Torvalds 62905e6557bSNeilBrown /** 63005e6557bSNeilBrown * container_of_safe - cast a member of a structure out to the containing structure 63105e6557bSNeilBrown * @ptr: the pointer to the member. 63205e6557bSNeilBrown * @type: the type of the container struct this is embedded in. 63305e6557bSNeilBrown * @member: the name of the member within the struct. 63405e6557bSNeilBrown * 63505e6557bSNeilBrown * If IS_ERR_OR_NULL(ptr), ptr is returned unchanged. 63605e6557bSNeilBrown */ 63705e6557bSNeilBrown #define container_of_safe(ptr, type, member) ({ \ 63805e6557bSNeilBrown void *__mptr = (void *)(ptr); \ 63905e6557bSNeilBrown BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) && \ 64005e6557bSNeilBrown !__same_type(*(ptr), void), \ 64105e6557bSNeilBrown "pointer type mismatch in container_of()"); \ 642227abcc6SDan Carpenter IS_ERR_OR_NULL(__mptr) ? ERR_CAST(__mptr) : \ 64305e6557bSNeilBrown ((type *)(__mptr - offsetof(type, member))); }) 64405e6557bSNeilBrown 645b9d4f426SArnaud Lacombe /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */ 646b9d4f426SArnaud Lacombe #ifdef CONFIG_FTRACE_MCOUNT_RECORD 647b9d4f426SArnaud Lacombe # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD 648b9d4f426SArnaud Lacombe #endif 6499d00f92fSWANG Cong 65058f86cc8SRusty Russell /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */ 65158f86cc8SRusty Russell #define VERIFY_OCTAL_PERMISSIONS(perms) \ 65258f86cc8SRusty Russell (BUILD_BUG_ON_ZERO((perms) < 0) + \ 65358f86cc8SRusty Russell BUILD_BUG_ON_ZERO((perms) > 0777) + \ 65428b8d0c8SGobinda Charan Maji /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */ \ 65528b8d0c8SGobinda Charan Maji BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) + \ 65628b8d0c8SGobinda Charan Maji BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) + \ 65728b8d0c8SGobinda Charan Maji /* USER_WRITABLE >= GROUP_WRITABLE */ \ 65828b8d0c8SGobinda Charan Maji BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) + \ 65928b8d0c8SGobinda Charan Maji /* OTHER_WRITABLE? Generally considered a bad idea. */ \ 66037549e94SRusty Russell BUILD_BUG_ON_ZERO((perms) & 2) + \ 66158f86cc8SRusty Russell (perms)) 6621da177e4SLinus Torvalds #endif 663