1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 21da177e4SLinus Torvalds #ifndef _LINUX_KERNEL_H 31da177e4SLinus Torvalds #define _LINUX_KERNEL_H 41da177e4SLinus Torvalds 5c0891ac1SAlexey Dobriyan #include <linux/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> 12*d2a8ebbfSAndy Shevchenko #include <linux/container_of.h> 131da177e4SLinus Torvalds #include <linux/bitops.h> 144c527293SAndy Shevchenko #include <linux/kstrtox.h> 15f0d1b0b3SDavid Howells #include <linux/log2.h> 16aa6159abSAndy Shevchenko #include <linux/math.h> 17b296a6d5SAndy Shevchenko #include <linux/minmax.h> 18e0deaff4SAndrew Morton #include <linux/typecheck.h> 19f39650deSAndy Shevchenko #include <linux/panic.h> 20968ab183SLinus Torvalds #include <linux/printk.h> 21c7acec71SIan Abbott #include <linux/build_bug.h> 22b965f1ddSPeter Zijlstra (Intel) #include <linux/static_call_types.h> 231da177e4SLinus Torvalds #include <asm/byteorder.h> 24aa6159abSAndy Shevchenko 25607ca46eSDavid Howells #include <uapi/linux/kernel.h> 261da177e4SLinus Torvalds 271da177e4SLinus Torvalds #define STACK_MAGIC 0xdeadbeef 281da177e4SLinus Torvalds 29e8c97af0SRandy Dunlap /** 30e8c97af0SRandy Dunlap * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value 31e8c97af0SRandy Dunlap * @x: value to repeat 32e8c97af0SRandy Dunlap * 33e8c97af0SRandy Dunlap * NOTE: @x is not checked for > 0xff; larger values produce odd results. 34e8c97af0SRandy Dunlap */ 3544696908SDavid S. Miller #define REPEAT_BYTE(x) ((~0ul / 0xff) * (x)) 3644696908SDavid S. Miller 37d3849953SChristoph Hellwig /* generic data direction definitions */ 38d3849953SChristoph Hellwig #define READ 0 39d3849953SChristoph Hellwig #define WRITE 1 40d3849953SChristoph Hellwig 41e8c97af0SRandy Dunlap /** 42e8c97af0SRandy Dunlap * ARRAY_SIZE - get the number of elements in array @arr 43e8c97af0SRandy Dunlap * @arr: array to be sized 44e8c97af0SRandy Dunlap */ 45c5e631cfSRusty Russell #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) 46c5e631cfSRusty Russell 470ab1438bSMasahiro Yamada #define PTR_IF(cond, ptr) ((cond) ? (ptr) : NULL) 480ab1438bSMasahiro Yamada 493ed605bcSGustavo Padovan #define u64_to_user_ptr(x) ( \ 503ed605bcSGustavo Padovan { \ 51a0fe2c64SJann Horn typecheck(u64, (x)); \ 52a0fe2c64SJann Horn (void __user *)(uintptr_t)(x); \ 533ed605bcSGustavo Padovan } \ 543ed605bcSGustavo Padovan ) 553ed605bcSGustavo Padovan 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 7503cb4473SJacob Keller /** 7603cb4473SJacob Keller * upper_16_bits - return bits 16-31 of a number 7703cb4473SJacob Keller * @n: the number we're accessing 7803cb4473SJacob Keller */ 7903cb4473SJacob Keller #define upper_16_bits(n) ((u16)((n) >> 16)) 8003cb4473SJacob Keller 8103cb4473SJacob Keller /** 8203cb4473SJacob Keller * lower_16_bits - return bits 0-15 of a number 8303cb4473SJacob Keller * @n: the number we're accessing 8403cb4473SJacob Keller */ 8503cb4473SJacob Keller #define lower_16_bits(n) ((u16)((n) & 0xffff)) 8603cb4473SJacob Keller 871da177e4SLinus Torvalds struct completion; 88df2e71fbS[email protected] struct user; 891da177e4SLinus Torvalds 90070cb065SUwe Kleine-König #ifdef CONFIG_PREEMPT_VOLUNTARY 91b965f1ddSPeter Zijlstra (Intel) 92b965f1ddSPeter Zijlstra (Intel) extern int __cond_resched(void); 93b965f1ddSPeter Zijlstra (Intel) # define might_resched() __cond_resched() 94b965f1ddSPeter Zijlstra (Intel) 95b965f1ddSPeter Zijlstra (Intel) #elif defined(CONFIG_PREEMPT_DYNAMIC) 96b965f1ddSPeter Zijlstra (Intel) 97b965f1ddSPeter Zijlstra (Intel) extern int __cond_resched(void); 98b965f1ddSPeter Zijlstra (Intel) 99b965f1ddSPeter Zijlstra (Intel) DECLARE_STATIC_CALL(might_resched, __cond_resched); 100b965f1ddSPeter Zijlstra (Intel) 101b965f1ddSPeter Zijlstra (Intel) static __always_inline void might_resched(void) 102b965f1ddSPeter Zijlstra (Intel) { 103ef72661eSPeter Zijlstra static_call_mod(might_resched)(); 104b965f1ddSPeter Zijlstra (Intel) } 105b965f1ddSPeter Zijlstra (Intel) 106070cb065SUwe Kleine-König #else 107b965f1ddSPeter Zijlstra (Intel) 108070cb065SUwe Kleine-König # define might_resched() do { } while (0) 109b965f1ddSPeter Zijlstra (Intel) 110b965f1ddSPeter Zijlstra (Intel) #endif /* CONFIG_PREEMPT_* */ 111070cb065SUwe Kleine-König 112d902db1eSFrederic Weisbecker #ifdef CONFIG_DEBUG_ATOMIC_SLEEP 113568f1967SPeter Zijlstra extern void ___might_sleep(const char *file, int line, int preempt_offset); 114568f1967SPeter Zijlstra extern void __might_sleep(const char *file, int line, int preempt_offset); 115568f1967SPeter Zijlstra extern void __cant_sleep(const char *file, int line, int preempt_offset); 11674d862b6SThomas Gleixner extern void __cant_migrate(const char *file, int line); 117568f1967SPeter Zijlstra 1181da177e4SLinus Torvalds /** 1191da177e4SLinus Torvalds * might_sleep - annotation for functions that can sleep 1201da177e4SLinus Torvalds * 1211da177e4SLinus Torvalds * this macro will print a stack trace if it is executed in an atomic 122312364f3SDaniel Vetter * context (spinlock, irq-handler, ...). Additional sections where blocking is 123312364f3SDaniel Vetter * not allowed can be annotated with non_block_start() and non_block_end() 124312364f3SDaniel Vetter * pairs. 1251da177e4SLinus Torvalds * 1261da177e4SLinus Torvalds * This is a useful debugging help to be able to catch problems early and not 127e20ec991SJim Cromie * be bitten later when the calling function happens to sleep when it is not 1281da177e4SLinus Torvalds * supposed to. 1291da177e4SLinus Torvalds */ 130f8cbd99bSIngo Molnar # define might_sleep() \ 131e4aafea2SFrederic Weisbecker do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0) 132568f1967SPeter Zijlstra /** 133568f1967SPeter Zijlstra * cant_sleep - annotation for functions that cannot sleep 134568f1967SPeter Zijlstra * 135568f1967SPeter Zijlstra * this macro will print a stack trace if it is executed with preemption enabled 136568f1967SPeter Zijlstra */ 137568f1967SPeter Zijlstra # define cant_sleep() \ 138568f1967SPeter Zijlstra do { __cant_sleep(__FILE__, __LINE__, 0); } while (0) 13900845eb9SLinus Torvalds # define sched_annotate_sleep() (current->task_state_change = 0) 14074d862b6SThomas Gleixner 14174d862b6SThomas Gleixner /** 14274d862b6SThomas Gleixner * cant_migrate - annotation for functions that cannot migrate 14374d862b6SThomas Gleixner * 14474d862b6SThomas Gleixner * Will print a stack trace if executed in code which is migratable 14574d862b6SThomas Gleixner */ 14674d862b6SThomas Gleixner # define cant_migrate() \ 14774d862b6SThomas Gleixner do { \ 14874d862b6SThomas Gleixner if (IS_ENABLED(CONFIG_SMP)) \ 14974d862b6SThomas Gleixner __cant_migrate(__FILE__, __LINE__); \ 15074d862b6SThomas Gleixner } while (0) 15174d862b6SThomas Gleixner 152312364f3SDaniel Vetter /** 153312364f3SDaniel Vetter * non_block_start - annotate the start of section where sleeping is prohibited 154312364f3SDaniel Vetter * 155312364f3SDaniel Vetter * This is on behalf of the oom reaper, specifically when it is calling the mmu 156312364f3SDaniel Vetter * notifiers. The problem is that if the notifier were to block on, for example, 157312364f3SDaniel Vetter * mutex_lock() and if the process which holds that mutex were to perform a 158312364f3SDaniel Vetter * sleeping memory allocation, the oom reaper is now blocked on completion of 159312364f3SDaniel Vetter * that memory allocation. Other blocking calls like wait_event() pose similar 160312364f3SDaniel Vetter * issues. 161312364f3SDaniel Vetter */ 162312364f3SDaniel Vetter # define non_block_start() (current->non_block_count++) 163312364f3SDaniel Vetter /** 164312364f3SDaniel Vetter * non_block_end - annotate the end of section where sleeping is prohibited 165312364f3SDaniel Vetter * 166312364f3SDaniel Vetter * Closes a section opened by non_block_start(). 167312364f3SDaniel Vetter */ 168312364f3SDaniel Vetter # define non_block_end() WARN_ON(current->non_block_count-- == 0) 169f8cbd99bSIngo Molnar #else 1703427445aSPeter Zijlstra static inline void ___might_sleep(const char *file, int line, 1713427445aSPeter Zijlstra int preempt_offset) { } 172d894837fSSimon Kagstrom static inline void __might_sleep(const char *file, int line, 173d894837fSSimon Kagstrom int preempt_offset) { } 174f8cbd99bSIngo Molnar # define might_sleep() do { might_resched(); } while (0) 175568f1967SPeter Zijlstra # define cant_sleep() do { } while (0) 17674d862b6SThomas Gleixner # define cant_migrate() do { } while (0) 1771029a2b5SPeter Zijlstra # define sched_annotate_sleep() do { } while (0) 178312364f3SDaniel Vetter # define non_block_start() do { } while (0) 179312364f3SDaniel Vetter # define non_block_end() do { } while (0) 180f8cbd99bSIngo Molnar #endif 181f8cbd99bSIngo Molnar 182368a5fa1SHua Zhong #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0) 183f8cbd99bSIngo Molnar 184386e7906SAxel Lin #if defined(CONFIG_MMU) && \ 185386e7906SAxel Lin (defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)) 1869ec23531SDavid Hildenbrand #define might_fault() __might_fault(__FILE__, __LINE__) 1879ec23531SDavid Hildenbrand void __might_fault(const char *file, int line); 1883ee1afa3SNick Piggin #else 189662bbcb2SMichael S. Tsirkin static inline void might_fault(void) { } 1903ee1afa3SNick Piggin #endif 1913ee1afa3SNick Piggin 1929af6528eSPeter Zijlstra void do_exit(long error_code) __noreturn; 1939af6528eSPeter Zijlstra void complete_and_exit(struct completion *, long) __noreturn; 19433ee3b2eSAlexey Dobriyan 195d1be35cbSAndrei Vagin extern int num_to_str(char *buf, int size, 196d1be35cbSAndrei Vagin unsigned long long num, unsigned int width); 1971ac101a5SKAMEZAWA Hiroyuki 19867d0a075SJoe Perches /* lib/printf utilities */ 19967d0a075SJoe Perches 200b9075fa9SJoe Perches extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...); 201b9075fa9SJoe Perches extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list); 202b9075fa9SJoe Perches extern __printf(3, 4) 203b9075fa9SJoe Perches int snprintf(char *buf, size_t size, const char *fmt, ...); 204b9075fa9SJoe Perches extern __printf(3, 0) 205b9075fa9SJoe Perches int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); 206b9075fa9SJoe Perches extern __printf(3, 4) 207b9075fa9SJoe Perches int scnprintf(char *buf, size_t size, const char *fmt, ...); 208b9075fa9SJoe Perches extern __printf(3, 0) 209b9075fa9SJoe Perches int vscnprintf(char *buf, size_t size, const char *fmt, va_list args); 21048a27055SRasmus Villemoes extern __printf(2, 3) __malloc 211b9075fa9SJoe Perches char *kasprintf(gfp_t gfp, const char *fmt, ...); 21248a27055SRasmus Villemoes extern __printf(2, 0) __malloc 2138db14860SNicolas Iooss char *kvasprintf(gfp_t gfp, const char *fmt, va_list args); 2140a9df786SRasmus Villemoes extern __printf(2, 0) 2150a9df786SRasmus Villemoes const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args); 2161da177e4SLinus Torvalds 2176061d949SJoe Perches extern __scanf(2, 3) 2186061d949SJoe Perches int sscanf(const char *, const char *, ...); 2196061d949SJoe Perches extern __scanf(2, 0) 2206061d949SJoe Perches int vsscanf(const char *, const char *, va_list); 2211da177e4SLinus Torvalds 22279270291SStephen Boyd extern int no_hash_pointers_enable(char *str); 22379270291SStephen Boyd 2241da177e4SLinus Torvalds extern int get_option(char **str, int *pint); 2251da177e4SLinus Torvalds extern char *get_options(const char *str, int nints, int *ints); 226d974ae37SJeremy Fitzhardinge extern unsigned long long memparse(const char *ptr, char **retptr); 2276ccc72b8SDave Young extern bool parse_option_str(const char *str, const char *option); 228f51b17c8SBaoquan He extern char *next_arg(char *args, char **param, char **val); 2291da177e4SLinus Torvalds 2305e376613STrent Piepho extern int core_kernel_text(unsigned long addr); 2319fbcc57aSJosh Poimboeuf extern int init_kernel_text(unsigned long addr); 232cdbe61bfSSteven Rostedt extern int core_kernel_data(unsigned long addr); 2331da177e4SLinus Torvalds extern int __kernel_text_address(unsigned long addr); 2341da177e4SLinus Torvalds extern int kernel_text_address(unsigned long addr); 235ab7476cfSArjan van de Ven extern int func_ptr_is_kernel_text(void *ptr); 236ab7476cfSArjan van de Ven 2371da177e4SLinus Torvalds extern void bust_spinlocks(int yes); 2385375b708SHATAYAMA Daisuke 239b920de1bSDavid Howells extern int root_mountflags; 2401da177e4SLinus Torvalds 2412ce802f6STejun Heo extern bool early_boot_irqs_disabled; 2422ce802f6STejun Heo 24369a78ff2SThomas Gleixner /* 24469a78ff2SThomas Gleixner * Values used for system_state. Ordering of the states must not be changed 24569a78ff2SThomas Gleixner * as code checks for <, <=, >, >= STATE. 24669a78ff2SThomas Gleixner */ 2471da177e4SLinus Torvalds extern enum system_states { 2481da177e4SLinus Torvalds SYSTEM_BOOTING, 24969a78ff2SThomas Gleixner SYSTEM_SCHEDULING, 250d2635f20SChristophe Leroy SYSTEM_FREEING_INITMEM, 2511da177e4SLinus Torvalds SYSTEM_RUNNING, 2521da177e4SLinus Torvalds SYSTEM_HALT, 2531da177e4SLinus Torvalds SYSTEM_POWER_OFF, 2541da177e4SLinus Torvalds SYSTEM_RESTART, 255c1a957d1SThomas Gleixner SYSTEM_SUSPEND, 2561da177e4SLinus Torvalds } system_state; 2571da177e4SLinus Torvalds 2583fc95772SHarvey Harrison extern const char hex_asc[]; 2593fc95772SHarvey Harrison #define hex_asc_lo(x) hex_asc[((x) & 0x0f)] 2603fc95772SHarvey Harrison #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4] 2613fc95772SHarvey Harrison 26255036ba7SAndy Shevchenko static inline char *hex_byte_pack(char *buf, u8 byte) 2633fc95772SHarvey Harrison { 2643fc95772SHarvey Harrison *buf++ = hex_asc_hi(byte); 2653fc95772SHarvey Harrison *buf++ = hex_asc_lo(byte); 2663fc95772SHarvey Harrison return buf; 2673fc95772SHarvey Harrison } 26899eaf3c4SRandy Dunlap 269c26d436cSAndre Naujoks extern const char hex_asc_upper[]; 270c26d436cSAndre Naujoks #define hex_asc_upper_lo(x) hex_asc_upper[((x) & 0x0f)] 271c26d436cSAndre Naujoks #define hex_asc_upper_hi(x) hex_asc_upper[((x) & 0xf0) >> 4] 272c26d436cSAndre Naujoks 273c26d436cSAndre Naujoks static inline char *hex_byte_pack_upper(char *buf, u8 byte) 274c26d436cSAndre Naujoks { 275c26d436cSAndre Naujoks *buf++ = hex_asc_upper_hi(byte); 276c26d436cSAndre Naujoks *buf++ = hex_asc_upper_lo(byte); 277c26d436cSAndre Naujoks return buf; 278c26d436cSAndre Naujoks } 279c26d436cSAndre Naujoks 28090378889SAndy Shevchenko extern int hex_to_bin(char ch); 281b7804983SMimi Zohar extern int __must_check hex2bin(u8 *dst, const char *src, size_t count); 28253d91c5cSDavid Howells extern char *bin2hex(char *dst, const void *src, size_t count); 28390378889SAndy Shevchenko 284a69f5edbSJoe Perches bool mac_pton(const char *s, u8 *mac); 2854cd5773aSAndy Shevchenko 2868a64f336SJoe Perches /* 287526211bcSIngo Molnar * General tracing related utility functions - trace_printk(), 2882002c258SSteven Rostedt * tracing_on/tracing_off and tracing_start()/tracing_stop 2892002c258SSteven Rostedt * 2902002c258SSteven Rostedt * Use tracing_on/tracing_off when you want to quickly turn on or off 2912002c258SSteven Rostedt * tracing. It simply enables or disables the recording of the trace events. 292156f5a78SGeunSik Lim * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on 2932002c258SSteven Rostedt * file, which gives a means for the kernel and userspace to interact. 2942002c258SSteven Rostedt * Place a tracing_off() in the kernel where you want tracing to end. 2952002c258SSteven Rostedt * From user space, examine the trace, and then echo 1 > tracing_on 2962002c258SSteven Rostedt * to continue tracing. 2972002c258SSteven Rostedt * 2982002c258SSteven Rostedt * tracing_stop/tracing_start has slightly more overhead. It is used 2992002c258SSteven Rostedt * by things like suspend to ram where disabling the recording of the 3002002c258SSteven Rostedt * trace is not enough, but tracing must actually stop because things 3012002c258SSteven Rostedt * like calling smp_processor_id() may crash the system. 3022002c258SSteven Rostedt * 3032002c258SSteven Rostedt * Most likely, you want to use tracing_on/tracing_off. 304526211bcSIngo Molnar */ 305cecbca96SFrederic Weisbecker 306cecbca96SFrederic Weisbecker enum ftrace_dump_mode { 307cecbca96SFrederic Weisbecker DUMP_NONE, 308cecbca96SFrederic Weisbecker DUMP_ALL, 309cecbca96SFrederic Weisbecker DUMP_ORIG, 310cecbca96SFrederic Weisbecker }; 311cecbca96SFrederic Weisbecker 312526211bcSIngo Molnar #ifdef CONFIG_TRACING 31393d68e52SSteven Rostedt void tracing_on(void); 31493d68e52SSteven Rostedt void tracing_off(void); 31593d68e52SSteven Rostedt int tracing_is_on(void); 316ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot(void); 317ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot_alloc(void); 31893d68e52SSteven Rostedt 319526211bcSIngo Molnar extern void tracing_start(void); 320526211bcSIngo Molnar extern void tracing_stop(void); 321526211bcSIngo Molnar 322b9075fa9SJoe Perches static inline __printf(1, 2) 323b9075fa9SJoe Perches void ____trace_printk_check_format(const char *fmt, ...) 324769b0441SFrederic Weisbecker { 325769b0441SFrederic Weisbecker } 326769b0441SFrederic Weisbecker #define __trace_printk_check_format(fmt, args...) \ 327769b0441SFrederic Weisbecker do { \ 328769b0441SFrederic Weisbecker if (0) \ 329769b0441SFrederic Weisbecker ____trace_printk_check_format(fmt, ##args); \ 330769b0441SFrederic Weisbecker } while (0) 331769b0441SFrederic Weisbecker 332526211bcSIngo Molnar /** 333526211bcSIngo Molnar * trace_printk - printf formatting in the ftrace buffer 334526211bcSIngo Molnar * @fmt: the printf format for printing 335526211bcSIngo Molnar * 336e8c97af0SRandy Dunlap * Note: __trace_printk is an internal function for trace_printk() and 337e8c97af0SRandy Dunlap * the @ip is passed in via the trace_printk() macro. 338526211bcSIngo Molnar * 339526211bcSIngo Molnar * This function allows a kernel developer to debug fast path sections 340526211bcSIngo Molnar * that printk is not appropriate for. By scattering in various 341526211bcSIngo Molnar * printk like tracing in the code, a developer can quickly see 342526211bcSIngo Molnar * where problems are occurring. 343526211bcSIngo Molnar * 344526211bcSIngo Molnar * This is intended as a debugging tool for the developer only. 345526211bcSIngo Molnar * Please refrain from leaving trace_printks scattered around in 34609ae7234SSteven Rostedt (Red Hat) * your code. (Extra memory is used for special buffers that are 347e8c97af0SRandy Dunlap * allocated when trace_printk() is used.) 3489d3c752cSSteven Rostedt (Red Hat) * 3498730662dSWei Wang * A little optimization trick is done here. If there's only one 3509d3c752cSSteven Rostedt (Red Hat) * argument, there's no need to scan the string for printf formats. 3519d3c752cSSteven Rostedt (Red Hat) * The trace_puts() will suffice. But how can we take advantage of 3529d3c752cSSteven Rostedt (Red Hat) * using trace_puts() when trace_printk() has only one argument? 3539d3c752cSSteven Rostedt (Red Hat) * By stringifying the args and checking the size we can tell 3549d3c752cSSteven Rostedt (Red Hat) * whether or not there are args. __stringify((__VA_ARGS__)) will 3559d3c752cSSteven Rostedt (Red Hat) * turn into "()\0" with a size of 3 when there are no args, anything 3569d3c752cSSteven Rostedt (Red Hat) * else will be bigger. All we need to do is define a string to this, 3579d3c752cSSteven Rostedt (Red Hat) * and then take its size and compare to 3. If it's bigger, use 3589d3c752cSSteven Rostedt (Red Hat) * do_trace_printk() otherwise, optimize it to trace_puts(). Then just 3599d3c752cSSteven Rostedt (Red Hat) * let gcc optimize the rest. 360526211bcSIngo Molnar */ 361769b0441SFrederic Weisbecker 3629d3c752cSSteven Rostedt (Red Hat) #define trace_printk(fmt, ...) \ 3639d3c752cSSteven Rostedt (Red Hat) do { \ 3649d3c752cSSteven Rostedt (Red Hat) char _______STR[] = __stringify((__VA_ARGS__)); \ 3659d3c752cSSteven Rostedt (Red Hat) if (sizeof(_______STR) > 3) \ 3669d3c752cSSteven Rostedt (Red Hat) do_trace_printk(fmt, ##__VA_ARGS__); \ 3679d3c752cSSteven Rostedt (Red Hat) else \ 3689d3c752cSSteven Rostedt (Red Hat) trace_puts(fmt); \ 3699d3c752cSSteven Rostedt (Red Hat) } while (0) 3709d3c752cSSteven Rostedt (Red Hat) 3719d3c752cSSteven Rostedt (Red Hat) #define do_trace_printk(fmt, args...) \ 372769b0441SFrederic Weisbecker do { \ 3733debb0a9SSteven Rostedt (Red Hat) static const char *trace_printk_fmt __used \ 37433def849SJoe Perches __section("__trace_printk_fmt") = \ 37548ead020SFrederic Weisbecker __builtin_constant_p(fmt) ? fmt : NULL; \ 37648ead020SFrederic Weisbecker \ 37707d777feSSteven Rostedt __trace_printk_check_format(fmt, ##args); \ 37807d777feSSteven Rostedt \ 37907d777feSSteven Rostedt if (__builtin_constant_p(fmt)) \ 38048ead020SFrederic Weisbecker __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \ 38107d777feSSteven Rostedt else \ 38248ead020SFrederic Weisbecker __trace_printk(_THIS_IP_, fmt, ##args); \ 383769b0441SFrederic Weisbecker } while (0) 384769b0441SFrederic Weisbecker 385b9075fa9SJoe Perches extern __printf(2, 3) 386b9075fa9SJoe Perches int __trace_bprintk(unsigned long ip, const char *fmt, ...); 38748ead020SFrederic Weisbecker 388b9075fa9SJoe Perches extern __printf(2, 3) 389b9075fa9SJoe Perches int __trace_printk(unsigned long ip, const char *fmt, ...); 390769b0441SFrederic Weisbecker 39109ae7234SSteven Rostedt (Red Hat) /** 39209ae7234SSteven Rostedt (Red Hat) * trace_puts - write a string into the ftrace buffer 39309ae7234SSteven Rostedt (Red Hat) * @str: the string to record 39409ae7234SSteven Rostedt (Red Hat) * 39509ae7234SSteven Rostedt (Red Hat) * Note: __trace_bputs is an internal function for trace_puts and 39609ae7234SSteven Rostedt (Red Hat) * the @ip is passed in via the trace_puts macro. 39709ae7234SSteven Rostedt (Red Hat) * 39809ae7234SSteven Rostedt (Red Hat) * This is similar to trace_printk() but is made for those really fast 399e8c97af0SRandy Dunlap * paths that a developer wants the least amount of "Heisenbug" effects, 40009ae7234SSteven Rostedt (Red Hat) * where the processing of the print format is still too much. 40109ae7234SSteven Rostedt (Red Hat) * 40209ae7234SSteven Rostedt (Red Hat) * This function allows a kernel developer to debug fast path sections 40309ae7234SSteven Rostedt (Red Hat) * that printk is not appropriate for. By scattering in various 40409ae7234SSteven Rostedt (Red Hat) * printk like tracing in the code, a developer can quickly see 40509ae7234SSteven Rostedt (Red Hat) * where problems are occurring. 40609ae7234SSteven Rostedt (Red Hat) * 40709ae7234SSteven Rostedt (Red Hat) * This is intended as a debugging tool for the developer only. 40809ae7234SSteven Rostedt (Red Hat) * Please refrain from leaving trace_puts scattered around in 40909ae7234SSteven Rostedt (Red Hat) * your code. (Extra memory is used for special buffers that are 410e8c97af0SRandy Dunlap * allocated when trace_puts() is used.) 41109ae7234SSteven Rostedt (Red Hat) * 41209ae7234SSteven Rostedt (Red Hat) * Returns: 0 if nothing was written, positive # if string was. 41309ae7234SSteven Rostedt (Red Hat) * (1 when __trace_bputs is used, strlen(str) when __trace_puts is used) 41409ae7234SSteven Rostedt (Red Hat) */ 41509ae7234SSteven Rostedt (Red Hat) 41609ae7234SSteven Rostedt (Red Hat) #define trace_puts(str) ({ \ 4173debb0a9SSteven Rostedt (Red Hat) static const char *trace_printk_fmt __used \ 41833def849SJoe Perches __section("__trace_printk_fmt") = \ 41909ae7234SSteven Rostedt (Red Hat) __builtin_constant_p(str) ? str : NULL; \ 42009ae7234SSteven Rostedt (Red Hat) \ 42109ae7234SSteven Rostedt (Red Hat) if (__builtin_constant_p(str)) \ 42209ae7234SSteven Rostedt (Red Hat) __trace_bputs(_THIS_IP_, trace_printk_fmt); \ 42309ae7234SSteven Rostedt (Red Hat) else \ 42409ae7234SSteven Rostedt (Red Hat) __trace_puts(_THIS_IP_, str, strlen(str)); \ 42509ae7234SSteven Rostedt (Red Hat) }) 426bcf312cfSSteven Rostedt extern int __trace_bputs(unsigned long ip, const char *str); 427bcf312cfSSteven Rostedt extern int __trace_puts(unsigned long ip, const char *str, int size); 42809ae7234SSteven Rostedt (Red Hat) 429c142be8eSSteven Rostedt (Red Hat) extern void trace_dump_stack(int skip); 43003889384SSteven Rostedt 43148ead020SFrederic Weisbecker /* 43248ead020SFrederic Weisbecker * The double __builtin_constant_p is because gcc will give us an error 43348ead020SFrederic Weisbecker * if we try to allocate the static variable to fmt if it is not a 43448ead020SFrederic Weisbecker * constant. Even with the outer if statement. 43548ead020SFrederic Weisbecker */ 436769b0441SFrederic Weisbecker #define ftrace_vprintk(fmt, vargs) \ 437769b0441SFrederic Weisbecker do { \ 43848ead020SFrederic Weisbecker if (__builtin_constant_p(fmt)) { \ 4393debb0a9SSteven Rostedt (Red Hat) static const char *trace_printk_fmt __used \ 44033def849SJoe Perches __section("__trace_printk_fmt") = \ 44148ead020SFrederic Weisbecker __builtin_constant_p(fmt) ? fmt : NULL; \ 4427bffc23eSIngo Molnar \ 44348ead020SFrederic Weisbecker __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \ 44448ead020SFrederic Weisbecker } else \ 44548ead020SFrederic Weisbecker __ftrace_vprintk(_THIS_IP_, fmt, vargs); \ 446769b0441SFrederic Weisbecker } while (0) 447769b0441SFrederic Weisbecker 4488db14860SNicolas Iooss extern __printf(2, 0) int 44948ead020SFrederic Weisbecker __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap); 45048ead020SFrederic Weisbecker 4518db14860SNicolas Iooss extern __printf(2, 0) int 452526211bcSIngo Molnar __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap); 453769b0441SFrederic Weisbecker 454cecbca96SFrederic Weisbecker extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode); 455526211bcSIngo Molnar #else 456526211bcSIngo Molnar static inline void tracing_start(void) { } 457526211bcSIngo Molnar static inline void tracing_stop(void) { } 458e67bc51eSDhaval Giani static inline void trace_dump_stack(int skip) { } 45993d68e52SSteven Rostedt 46093d68e52SSteven Rostedt static inline void tracing_on(void) { } 46193d68e52SSteven Rostedt static inline void tracing_off(void) { } 46293d68e52SSteven Rostedt static inline int tracing_is_on(void) { return 0; } 463ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot(void) { } 464ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot_alloc(void) { } 46593d68e52SSteven Rostedt 46660efc15aSMichal Hocko static inline __printf(1, 2) 46760efc15aSMichal Hocko int trace_printk(const char *fmt, ...) 468526211bcSIngo Molnar { 469526211bcSIngo Molnar return 0; 470526211bcSIngo Molnar } 4718db14860SNicolas Iooss static __printf(1, 0) inline int 472526211bcSIngo Molnar ftrace_vprintk(const char *fmt, va_list ap) 473526211bcSIngo Molnar { 474526211bcSIngo Molnar return 0; 475526211bcSIngo Molnar } 476cecbca96SFrederic Weisbecker static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { } 477769b0441SFrederic Weisbecker #endif /* CONFIG_TRACING */ 478526211bcSIngo Molnar 479cf14f27fSAlexei Starovoitov /* This counts to 12. Any more, it will return 13th argument. */ 480cf14f27fSAlexei Starovoitov #define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n 481cf14f27fSAlexei Starovoitov #define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) 482cf14f27fSAlexei Starovoitov 483cf14f27fSAlexei Starovoitov #define __CONCAT(a, b) a ## b 484cf14f27fSAlexei Starovoitov #define CONCATENATE(a, b) __CONCAT(a, b) 485cf14f27fSAlexei Starovoitov 486b9d4f426SArnaud Lacombe /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */ 487b9d4f426SArnaud Lacombe #ifdef CONFIG_FTRACE_MCOUNT_RECORD 488b9d4f426SArnaud Lacombe # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD 489b9d4f426SArnaud Lacombe #endif 4909d00f92fSWANG Cong 49158f86cc8SRusty Russell /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */ 49258f86cc8SRusty Russell #define VERIFY_OCTAL_PERMISSIONS(perms) \ 49358f86cc8SRusty Russell (BUILD_BUG_ON_ZERO((perms) < 0) + \ 49458f86cc8SRusty Russell BUILD_BUG_ON_ZERO((perms) > 0777) + \ 49528b8d0c8SGobinda Charan Maji /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */ \ 49628b8d0c8SGobinda Charan Maji BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) + \ 49728b8d0c8SGobinda Charan Maji BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) + \ 49828b8d0c8SGobinda Charan Maji /* USER_WRITABLE >= GROUP_WRITABLE */ \ 49928b8d0c8SGobinda Charan Maji BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) + \ 50028b8d0c8SGobinda Charan Maji /* OTHER_WRITABLE? Generally considered a bad idea. */ \ 50137549e94SRusty Russell BUILD_BUG_ON_ZERO((perms) & 2) + \ 50258f86cc8SRusty Russell (perms)) 5031da177e4SLinus Torvalds #endif 504