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> 12d2a8ebbfSAndy 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> 23*e52340deSStephen Rothwell #include <linux/instruction_pointer.h> 241da177e4SLinus Torvalds #include <asm/byteorder.h> 25aa6159abSAndy Shevchenko 26607ca46eSDavid Howells #include <uapi/linux/kernel.h> 271da177e4SLinus Torvalds 281da177e4SLinus Torvalds #define STACK_MAGIC 0xdeadbeef 291da177e4SLinus Torvalds 30e8c97af0SRandy Dunlap /** 31e8c97af0SRandy Dunlap * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value 32e8c97af0SRandy Dunlap * @x: value to repeat 33e8c97af0SRandy Dunlap * 34e8c97af0SRandy Dunlap * NOTE: @x is not checked for > 0xff; larger values produce odd results. 35e8c97af0SRandy Dunlap */ 3644696908SDavid S. Miller #define REPEAT_BYTE(x) ((~0ul / 0xff) * (x)) 3744696908SDavid S. Miller 38d3849953SChristoph Hellwig /* generic data direction definitions */ 39d3849953SChristoph Hellwig #define READ 0 40d3849953SChristoph Hellwig #define WRITE 1 41d3849953SChristoph Hellwig 42e8c97af0SRandy Dunlap /** 43e8c97af0SRandy Dunlap * ARRAY_SIZE - get the number of elements in array @arr 44e8c97af0SRandy Dunlap * @arr: array to be sized 45e8c97af0SRandy Dunlap */ 46c5e631cfSRusty Russell #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) 47c5e631cfSRusty Russell 480ab1438bSMasahiro Yamada #define PTR_IF(cond, ptr) ((cond) ? (ptr) : NULL) 490ab1438bSMasahiro Yamada 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 57218e180eSAndrew Morton /** 58218e180eSAndrew Morton * upper_32_bits - return bits 32-63 of a number 59218e180eSAndrew Morton * @n: the number we're accessing 60218e180eSAndrew Morton * 61218e180eSAndrew Morton * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress 62218e180eSAndrew Morton * the "right shift count >= width of type" warning when that quantity is 63218e180eSAndrew Morton * 32-bits. 64218e180eSAndrew Morton */ 65218e180eSAndrew Morton #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16)) 66218e180eSAndrew Morton 67204b885eSJoerg Roedel /** 68204b885eSJoerg Roedel * lower_32_bits - return bits 0-31 of a number 69204b885eSJoerg Roedel * @n: the number we're accessing 70204b885eSJoerg Roedel */ 71ef91bb19SHerbert Xu #define lower_32_bits(n) ((u32)((n) & 0xffffffff)) 72204b885eSJoerg Roedel 7303cb4473SJacob Keller /** 7403cb4473SJacob Keller * upper_16_bits - return bits 16-31 of a number 7503cb4473SJacob Keller * @n: the number we're accessing 7603cb4473SJacob Keller */ 7703cb4473SJacob Keller #define upper_16_bits(n) ((u16)((n) >> 16)) 7803cb4473SJacob Keller 7903cb4473SJacob Keller /** 8003cb4473SJacob Keller * lower_16_bits - return bits 0-15 of a number 8103cb4473SJacob Keller * @n: the number we're accessing 8203cb4473SJacob Keller */ 8303cb4473SJacob Keller #define lower_16_bits(n) ((u16)((n) & 0xffff)) 8403cb4473SJacob Keller 851da177e4SLinus Torvalds struct completion; 86df2e71fbS[email protected] struct user; 871da177e4SLinus Torvalds 88070cb065SUwe Kleine-König #ifdef CONFIG_PREEMPT_VOLUNTARY 89b965f1ddSPeter Zijlstra (Intel) 90b965f1ddSPeter Zijlstra (Intel) extern int __cond_resched(void); 91b965f1ddSPeter Zijlstra (Intel) # define might_resched() __cond_resched() 92b965f1ddSPeter Zijlstra (Intel) 93b965f1ddSPeter Zijlstra (Intel) #elif defined(CONFIG_PREEMPT_DYNAMIC) 94b965f1ddSPeter Zijlstra (Intel) 95b965f1ddSPeter Zijlstra (Intel) extern int __cond_resched(void); 96b965f1ddSPeter Zijlstra (Intel) 97b965f1ddSPeter Zijlstra (Intel) DECLARE_STATIC_CALL(might_resched, __cond_resched); 98b965f1ddSPeter Zijlstra (Intel) 99b965f1ddSPeter Zijlstra (Intel) static __always_inline void might_resched(void) 100b965f1ddSPeter Zijlstra (Intel) { 101ef72661eSPeter Zijlstra static_call_mod(might_resched)(); 102b965f1ddSPeter Zijlstra (Intel) } 103b965f1ddSPeter Zijlstra (Intel) 104070cb065SUwe Kleine-König #else 105b965f1ddSPeter Zijlstra (Intel) 106070cb065SUwe Kleine-König # define might_resched() do { } while (0) 107b965f1ddSPeter Zijlstra (Intel) 108b965f1ddSPeter Zijlstra (Intel) #endif /* CONFIG_PREEMPT_* */ 109070cb065SUwe Kleine-König 110d902db1eSFrederic Weisbecker #ifdef CONFIG_DEBUG_ATOMIC_SLEEP 111568f1967SPeter Zijlstra extern void ___might_sleep(const char *file, int line, int preempt_offset); 112568f1967SPeter Zijlstra extern void __might_sleep(const char *file, int line, int preempt_offset); 113568f1967SPeter Zijlstra extern void __cant_sleep(const char *file, int line, int preempt_offset); 11474d862b6SThomas Gleixner extern void __cant_migrate(const char *file, int line); 115568f1967SPeter Zijlstra 1161da177e4SLinus Torvalds /** 1171da177e4SLinus Torvalds * might_sleep - annotation for functions that can sleep 1181da177e4SLinus Torvalds * 1191da177e4SLinus Torvalds * this macro will print a stack trace if it is executed in an atomic 120312364f3SDaniel Vetter * context (spinlock, irq-handler, ...). Additional sections where blocking is 121312364f3SDaniel Vetter * not allowed can be annotated with non_block_start() and non_block_end() 122312364f3SDaniel Vetter * pairs. 1231da177e4SLinus Torvalds * 1241da177e4SLinus Torvalds * This is a useful debugging help to be able to catch problems early and not 125e20ec991SJim Cromie * be bitten later when the calling function happens to sleep when it is not 1261da177e4SLinus Torvalds * supposed to. 1271da177e4SLinus Torvalds */ 128f8cbd99bSIngo Molnar # define might_sleep() \ 129e4aafea2SFrederic Weisbecker do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0) 130568f1967SPeter Zijlstra /** 131568f1967SPeter Zijlstra * cant_sleep - annotation for functions that cannot sleep 132568f1967SPeter Zijlstra * 133568f1967SPeter Zijlstra * this macro will print a stack trace if it is executed with preemption enabled 134568f1967SPeter Zijlstra */ 135568f1967SPeter Zijlstra # define cant_sleep() \ 136568f1967SPeter Zijlstra do { __cant_sleep(__FILE__, __LINE__, 0); } while (0) 13700845eb9SLinus Torvalds # define sched_annotate_sleep() (current->task_state_change = 0) 13874d862b6SThomas Gleixner 13974d862b6SThomas Gleixner /** 14074d862b6SThomas Gleixner * cant_migrate - annotation for functions that cannot migrate 14174d862b6SThomas Gleixner * 14274d862b6SThomas Gleixner * Will print a stack trace if executed in code which is migratable 14374d862b6SThomas Gleixner */ 14474d862b6SThomas Gleixner # define cant_migrate() \ 14574d862b6SThomas Gleixner do { \ 14674d862b6SThomas Gleixner if (IS_ENABLED(CONFIG_SMP)) \ 14774d862b6SThomas Gleixner __cant_migrate(__FILE__, __LINE__); \ 14874d862b6SThomas Gleixner } while (0) 14974d862b6SThomas Gleixner 150312364f3SDaniel Vetter /** 151312364f3SDaniel Vetter * non_block_start - annotate the start of section where sleeping is prohibited 152312364f3SDaniel Vetter * 153312364f3SDaniel Vetter * This is on behalf of the oom reaper, specifically when it is calling the mmu 154312364f3SDaniel Vetter * notifiers. The problem is that if the notifier were to block on, for example, 155312364f3SDaniel Vetter * mutex_lock() and if the process which holds that mutex were to perform a 156312364f3SDaniel Vetter * sleeping memory allocation, the oom reaper is now blocked on completion of 157312364f3SDaniel Vetter * that memory allocation. Other blocking calls like wait_event() pose similar 158312364f3SDaniel Vetter * issues. 159312364f3SDaniel Vetter */ 160312364f3SDaniel Vetter # define non_block_start() (current->non_block_count++) 161312364f3SDaniel Vetter /** 162312364f3SDaniel Vetter * non_block_end - annotate the end of section where sleeping is prohibited 163312364f3SDaniel Vetter * 164312364f3SDaniel Vetter * Closes a section opened by non_block_start(). 165312364f3SDaniel Vetter */ 166312364f3SDaniel Vetter # define non_block_end() WARN_ON(current->non_block_count-- == 0) 167f8cbd99bSIngo Molnar #else 1683427445aSPeter Zijlstra static inline void ___might_sleep(const char *file, int line, 1693427445aSPeter Zijlstra int preempt_offset) { } 170d894837fSSimon Kagstrom static inline void __might_sleep(const char *file, int line, 171d894837fSSimon Kagstrom int preempt_offset) { } 172f8cbd99bSIngo Molnar # define might_sleep() do { might_resched(); } while (0) 173568f1967SPeter Zijlstra # define cant_sleep() do { } while (0) 17474d862b6SThomas Gleixner # define cant_migrate() do { } while (0) 1751029a2b5SPeter Zijlstra # define sched_annotate_sleep() do { } while (0) 176312364f3SDaniel Vetter # define non_block_start() do { } while (0) 177312364f3SDaniel Vetter # define non_block_end() do { } while (0) 178f8cbd99bSIngo Molnar #endif 179f8cbd99bSIngo Molnar 180368a5fa1SHua Zhong #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0) 181f8cbd99bSIngo Molnar 182386e7906SAxel Lin #if defined(CONFIG_MMU) && \ 183386e7906SAxel Lin (defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)) 1849ec23531SDavid Hildenbrand #define might_fault() __might_fault(__FILE__, __LINE__) 1859ec23531SDavid Hildenbrand void __might_fault(const char *file, int line); 1863ee1afa3SNick Piggin #else 187662bbcb2SMichael S. Tsirkin static inline void might_fault(void) { } 1883ee1afa3SNick Piggin #endif 1893ee1afa3SNick Piggin 1909af6528eSPeter Zijlstra void do_exit(long error_code) __noreturn; 1919af6528eSPeter Zijlstra void complete_and_exit(struct completion *, long) __noreturn; 19233ee3b2eSAlexey Dobriyan 193d1be35cbSAndrei Vagin extern int num_to_str(char *buf, int size, 194d1be35cbSAndrei Vagin unsigned long long num, unsigned int width); 1951ac101a5SKAMEZAWA Hiroyuki 19667d0a075SJoe Perches /* lib/printf utilities */ 19767d0a075SJoe Perches 198b9075fa9SJoe Perches extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...); 199b9075fa9SJoe Perches extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list); 200b9075fa9SJoe Perches extern __printf(3, 4) 201b9075fa9SJoe Perches int snprintf(char *buf, size_t size, const char *fmt, ...); 202b9075fa9SJoe Perches extern __printf(3, 0) 203b9075fa9SJoe Perches int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); 204b9075fa9SJoe Perches extern __printf(3, 4) 205b9075fa9SJoe Perches int scnprintf(char *buf, size_t size, const char *fmt, ...); 206b9075fa9SJoe Perches extern __printf(3, 0) 207b9075fa9SJoe Perches int vscnprintf(char *buf, size_t size, const char *fmt, va_list args); 20848a27055SRasmus Villemoes extern __printf(2, 3) __malloc 209b9075fa9SJoe Perches char *kasprintf(gfp_t gfp, const char *fmt, ...); 21048a27055SRasmus Villemoes extern __printf(2, 0) __malloc 2118db14860SNicolas Iooss char *kvasprintf(gfp_t gfp, const char *fmt, va_list args); 2120a9df786SRasmus Villemoes extern __printf(2, 0) 2130a9df786SRasmus Villemoes const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args); 2141da177e4SLinus Torvalds 2156061d949SJoe Perches extern __scanf(2, 3) 2166061d949SJoe Perches int sscanf(const char *, const char *, ...); 2176061d949SJoe Perches extern __scanf(2, 0) 2186061d949SJoe Perches int vsscanf(const char *, const char *, va_list); 2191da177e4SLinus Torvalds 22079270291SStephen Boyd extern int no_hash_pointers_enable(char *str); 22179270291SStephen Boyd 2221da177e4SLinus Torvalds extern int get_option(char **str, int *pint); 2231da177e4SLinus Torvalds extern char *get_options(const char *str, int nints, int *ints); 224d974ae37SJeremy Fitzhardinge extern unsigned long long memparse(const char *ptr, char **retptr); 2256ccc72b8SDave Young extern bool parse_option_str(const char *str, const char *option); 226f51b17c8SBaoquan He extern char *next_arg(char *args, char **param, char **val); 2271da177e4SLinus Torvalds 2285e376613STrent Piepho extern int core_kernel_text(unsigned long addr); 2299fbcc57aSJosh Poimboeuf extern int init_kernel_text(unsigned long addr); 230cdbe61bfSSteven Rostedt extern int core_kernel_data(unsigned long addr); 2311da177e4SLinus Torvalds extern int __kernel_text_address(unsigned long addr); 2321da177e4SLinus Torvalds extern int kernel_text_address(unsigned long addr); 233ab7476cfSArjan van de Ven extern int func_ptr_is_kernel_text(void *ptr); 234ab7476cfSArjan van de Ven 2351da177e4SLinus Torvalds extern void bust_spinlocks(int yes); 2365375b708SHATAYAMA Daisuke 237b920de1bSDavid Howells extern int root_mountflags; 2381da177e4SLinus Torvalds 2392ce802f6STejun Heo extern bool early_boot_irqs_disabled; 2402ce802f6STejun Heo 24169a78ff2SThomas Gleixner /* 24269a78ff2SThomas Gleixner * Values used for system_state. Ordering of the states must not be changed 24369a78ff2SThomas Gleixner * as code checks for <, <=, >, >= STATE. 24469a78ff2SThomas Gleixner */ 2451da177e4SLinus Torvalds extern enum system_states { 2461da177e4SLinus Torvalds SYSTEM_BOOTING, 24769a78ff2SThomas Gleixner SYSTEM_SCHEDULING, 248d2635f20SChristophe Leroy SYSTEM_FREEING_INITMEM, 2491da177e4SLinus Torvalds SYSTEM_RUNNING, 2501da177e4SLinus Torvalds SYSTEM_HALT, 2511da177e4SLinus Torvalds SYSTEM_POWER_OFF, 2521da177e4SLinus Torvalds SYSTEM_RESTART, 253c1a957d1SThomas Gleixner SYSTEM_SUSPEND, 2541da177e4SLinus Torvalds } system_state; 2551da177e4SLinus Torvalds 2563fc95772SHarvey Harrison extern const char hex_asc[]; 2573fc95772SHarvey Harrison #define hex_asc_lo(x) hex_asc[((x) & 0x0f)] 2583fc95772SHarvey Harrison #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4] 2593fc95772SHarvey Harrison 26055036ba7SAndy Shevchenko static inline char *hex_byte_pack(char *buf, u8 byte) 2613fc95772SHarvey Harrison { 2623fc95772SHarvey Harrison *buf++ = hex_asc_hi(byte); 2633fc95772SHarvey Harrison *buf++ = hex_asc_lo(byte); 2643fc95772SHarvey Harrison return buf; 2653fc95772SHarvey Harrison } 26699eaf3c4SRandy Dunlap 267c26d436cSAndre Naujoks extern const char hex_asc_upper[]; 268c26d436cSAndre Naujoks #define hex_asc_upper_lo(x) hex_asc_upper[((x) & 0x0f)] 269c26d436cSAndre Naujoks #define hex_asc_upper_hi(x) hex_asc_upper[((x) & 0xf0) >> 4] 270c26d436cSAndre Naujoks 271c26d436cSAndre Naujoks static inline char *hex_byte_pack_upper(char *buf, u8 byte) 272c26d436cSAndre Naujoks { 273c26d436cSAndre Naujoks *buf++ = hex_asc_upper_hi(byte); 274c26d436cSAndre Naujoks *buf++ = hex_asc_upper_lo(byte); 275c26d436cSAndre Naujoks return buf; 276c26d436cSAndre Naujoks } 277c26d436cSAndre Naujoks 27890378889SAndy Shevchenko extern int hex_to_bin(char ch); 279b7804983SMimi Zohar extern int __must_check hex2bin(u8 *dst, const char *src, size_t count); 28053d91c5cSDavid Howells extern char *bin2hex(char *dst, const void *src, size_t count); 28190378889SAndy Shevchenko 282a69f5edbSJoe Perches bool mac_pton(const char *s, u8 *mac); 2834cd5773aSAndy Shevchenko 2848a64f336SJoe Perches /* 285526211bcSIngo Molnar * General tracing related utility functions - trace_printk(), 2862002c258SSteven Rostedt * tracing_on/tracing_off and tracing_start()/tracing_stop 2872002c258SSteven Rostedt * 2882002c258SSteven Rostedt * Use tracing_on/tracing_off when you want to quickly turn on or off 2892002c258SSteven Rostedt * tracing. It simply enables or disables the recording of the trace events. 290156f5a78SGeunSik Lim * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on 2912002c258SSteven Rostedt * file, which gives a means for the kernel and userspace to interact. 2922002c258SSteven Rostedt * Place a tracing_off() in the kernel where you want tracing to end. 2932002c258SSteven Rostedt * From user space, examine the trace, and then echo 1 > tracing_on 2942002c258SSteven Rostedt * to continue tracing. 2952002c258SSteven Rostedt * 2962002c258SSteven Rostedt * tracing_stop/tracing_start has slightly more overhead. It is used 2972002c258SSteven Rostedt * by things like suspend to ram where disabling the recording of the 2982002c258SSteven Rostedt * trace is not enough, but tracing must actually stop because things 2992002c258SSteven Rostedt * like calling smp_processor_id() may crash the system. 3002002c258SSteven Rostedt * 3012002c258SSteven Rostedt * Most likely, you want to use tracing_on/tracing_off. 302526211bcSIngo Molnar */ 303cecbca96SFrederic Weisbecker 304cecbca96SFrederic Weisbecker enum ftrace_dump_mode { 305cecbca96SFrederic Weisbecker DUMP_NONE, 306cecbca96SFrederic Weisbecker DUMP_ALL, 307cecbca96SFrederic Weisbecker DUMP_ORIG, 308cecbca96SFrederic Weisbecker }; 309cecbca96SFrederic Weisbecker 310526211bcSIngo Molnar #ifdef CONFIG_TRACING 31193d68e52SSteven Rostedt void tracing_on(void); 31293d68e52SSteven Rostedt void tracing_off(void); 31393d68e52SSteven Rostedt int tracing_is_on(void); 314ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot(void); 315ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot_alloc(void); 31693d68e52SSteven Rostedt 317526211bcSIngo Molnar extern void tracing_start(void); 318526211bcSIngo Molnar extern void tracing_stop(void); 319526211bcSIngo Molnar 320b9075fa9SJoe Perches static inline __printf(1, 2) 321b9075fa9SJoe Perches void ____trace_printk_check_format(const char *fmt, ...) 322769b0441SFrederic Weisbecker { 323769b0441SFrederic Weisbecker } 324769b0441SFrederic Weisbecker #define __trace_printk_check_format(fmt, args...) \ 325769b0441SFrederic Weisbecker do { \ 326769b0441SFrederic Weisbecker if (0) \ 327769b0441SFrederic Weisbecker ____trace_printk_check_format(fmt, ##args); \ 328769b0441SFrederic Weisbecker } while (0) 329769b0441SFrederic Weisbecker 330526211bcSIngo Molnar /** 331526211bcSIngo Molnar * trace_printk - printf formatting in the ftrace buffer 332526211bcSIngo Molnar * @fmt: the printf format for printing 333526211bcSIngo Molnar * 334e8c97af0SRandy Dunlap * Note: __trace_printk is an internal function for trace_printk() and 335e8c97af0SRandy Dunlap * the @ip is passed in via the trace_printk() macro. 336526211bcSIngo Molnar * 337526211bcSIngo Molnar * This function allows a kernel developer to debug fast path sections 338526211bcSIngo Molnar * that printk is not appropriate for. By scattering in various 339526211bcSIngo Molnar * printk like tracing in the code, a developer can quickly see 340526211bcSIngo Molnar * where problems are occurring. 341526211bcSIngo Molnar * 342526211bcSIngo Molnar * This is intended as a debugging tool for the developer only. 343526211bcSIngo Molnar * Please refrain from leaving trace_printks scattered around in 34409ae7234SSteven Rostedt (Red Hat) * your code. (Extra memory is used for special buffers that are 345e8c97af0SRandy Dunlap * allocated when trace_printk() is used.) 3469d3c752cSSteven Rostedt (Red Hat) * 3478730662dSWei Wang * A little optimization trick is done here. If there's only one 3489d3c752cSSteven Rostedt (Red Hat) * argument, there's no need to scan the string for printf formats. 3499d3c752cSSteven Rostedt (Red Hat) * The trace_puts() will suffice. But how can we take advantage of 3509d3c752cSSteven Rostedt (Red Hat) * using trace_puts() when trace_printk() has only one argument? 3519d3c752cSSteven Rostedt (Red Hat) * By stringifying the args and checking the size we can tell 3529d3c752cSSteven Rostedt (Red Hat) * whether or not there are args. __stringify((__VA_ARGS__)) will 3539d3c752cSSteven Rostedt (Red Hat) * turn into "()\0" with a size of 3 when there are no args, anything 3549d3c752cSSteven Rostedt (Red Hat) * else will be bigger. All we need to do is define a string to this, 3559d3c752cSSteven Rostedt (Red Hat) * and then take its size and compare to 3. If it's bigger, use 3569d3c752cSSteven Rostedt (Red Hat) * do_trace_printk() otherwise, optimize it to trace_puts(). Then just 3579d3c752cSSteven Rostedt (Red Hat) * let gcc optimize the rest. 358526211bcSIngo Molnar */ 359769b0441SFrederic Weisbecker 3609d3c752cSSteven Rostedt (Red Hat) #define trace_printk(fmt, ...) \ 3619d3c752cSSteven Rostedt (Red Hat) do { \ 3629d3c752cSSteven Rostedt (Red Hat) char _______STR[] = __stringify((__VA_ARGS__)); \ 3639d3c752cSSteven Rostedt (Red Hat) if (sizeof(_______STR) > 3) \ 3649d3c752cSSteven Rostedt (Red Hat) do_trace_printk(fmt, ##__VA_ARGS__); \ 3659d3c752cSSteven Rostedt (Red Hat) else \ 3669d3c752cSSteven Rostedt (Red Hat) trace_puts(fmt); \ 3679d3c752cSSteven Rostedt (Red Hat) } while (0) 3689d3c752cSSteven Rostedt (Red Hat) 3699d3c752cSSteven Rostedt (Red Hat) #define do_trace_printk(fmt, args...) \ 370769b0441SFrederic Weisbecker do { \ 3713debb0a9SSteven Rostedt (Red Hat) static const char *trace_printk_fmt __used \ 37233def849SJoe Perches __section("__trace_printk_fmt") = \ 37348ead020SFrederic Weisbecker __builtin_constant_p(fmt) ? fmt : NULL; \ 37448ead020SFrederic Weisbecker \ 37507d777feSSteven Rostedt __trace_printk_check_format(fmt, ##args); \ 37607d777feSSteven Rostedt \ 37707d777feSSteven Rostedt if (__builtin_constant_p(fmt)) \ 37848ead020SFrederic Weisbecker __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \ 37907d777feSSteven Rostedt else \ 38048ead020SFrederic Weisbecker __trace_printk(_THIS_IP_, fmt, ##args); \ 381769b0441SFrederic Weisbecker } while (0) 382769b0441SFrederic Weisbecker 383b9075fa9SJoe Perches extern __printf(2, 3) 384b9075fa9SJoe Perches int __trace_bprintk(unsigned long ip, const char *fmt, ...); 38548ead020SFrederic Weisbecker 386b9075fa9SJoe Perches extern __printf(2, 3) 387b9075fa9SJoe Perches int __trace_printk(unsigned long ip, const char *fmt, ...); 388769b0441SFrederic Weisbecker 38909ae7234SSteven Rostedt (Red Hat) /** 39009ae7234SSteven Rostedt (Red Hat) * trace_puts - write a string into the ftrace buffer 39109ae7234SSteven Rostedt (Red Hat) * @str: the string to record 39209ae7234SSteven Rostedt (Red Hat) * 39309ae7234SSteven Rostedt (Red Hat) * Note: __trace_bputs is an internal function for trace_puts and 39409ae7234SSteven Rostedt (Red Hat) * the @ip is passed in via the trace_puts macro. 39509ae7234SSteven Rostedt (Red Hat) * 39609ae7234SSteven Rostedt (Red Hat) * This is similar to trace_printk() but is made for those really fast 397e8c97af0SRandy Dunlap * paths that a developer wants the least amount of "Heisenbug" effects, 39809ae7234SSteven Rostedt (Red Hat) * where the processing of the print format is still too much. 39909ae7234SSteven Rostedt (Red Hat) * 40009ae7234SSteven Rostedt (Red Hat) * This function allows a kernel developer to debug fast path sections 40109ae7234SSteven Rostedt (Red Hat) * that printk is not appropriate for. By scattering in various 40209ae7234SSteven Rostedt (Red Hat) * printk like tracing in the code, a developer can quickly see 40309ae7234SSteven Rostedt (Red Hat) * where problems are occurring. 40409ae7234SSteven Rostedt (Red Hat) * 40509ae7234SSteven Rostedt (Red Hat) * This is intended as a debugging tool for the developer only. 40609ae7234SSteven Rostedt (Red Hat) * Please refrain from leaving trace_puts scattered around in 40709ae7234SSteven Rostedt (Red Hat) * your code. (Extra memory is used for special buffers that are 408e8c97af0SRandy Dunlap * allocated when trace_puts() is used.) 40909ae7234SSteven Rostedt (Red Hat) * 41009ae7234SSteven Rostedt (Red Hat) * Returns: 0 if nothing was written, positive # if string was. 41109ae7234SSteven Rostedt (Red Hat) * (1 when __trace_bputs is used, strlen(str) when __trace_puts is used) 41209ae7234SSteven Rostedt (Red Hat) */ 41309ae7234SSteven Rostedt (Red Hat) 41409ae7234SSteven Rostedt (Red Hat) #define trace_puts(str) ({ \ 4153debb0a9SSteven Rostedt (Red Hat) static const char *trace_printk_fmt __used \ 41633def849SJoe Perches __section("__trace_printk_fmt") = \ 41709ae7234SSteven Rostedt (Red Hat) __builtin_constant_p(str) ? str : NULL; \ 41809ae7234SSteven Rostedt (Red Hat) \ 41909ae7234SSteven Rostedt (Red Hat) if (__builtin_constant_p(str)) \ 42009ae7234SSteven Rostedt (Red Hat) __trace_bputs(_THIS_IP_, trace_printk_fmt); \ 42109ae7234SSteven Rostedt (Red Hat) else \ 42209ae7234SSteven Rostedt (Red Hat) __trace_puts(_THIS_IP_, str, strlen(str)); \ 42309ae7234SSteven Rostedt (Red Hat) }) 424bcf312cfSSteven Rostedt extern int __trace_bputs(unsigned long ip, const char *str); 425bcf312cfSSteven Rostedt extern int __trace_puts(unsigned long ip, const char *str, int size); 42609ae7234SSteven Rostedt (Red Hat) 427c142be8eSSteven Rostedt (Red Hat) extern void trace_dump_stack(int skip); 42803889384SSteven Rostedt 42948ead020SFrederic Weisbecker /* 43048ead020SFrederic Weisbecker * The double __builtin_constant_p is because gcc will give us an error 43148ead020SFrederic Weisbecker * if we try to allocate the static variable to fmt if it is not a 43248ead020SFrederic Weisbecker * constant. Even with the outer if statement. 43348ead020SFrederic Weisbecker */ 434769b0441SFrederic Weisbecker #define ftrace_vprintk(fmt, vargs) \ 435769b0441SFrederic Weisbecker do { \ 43648ead020SFrederic Weisbecker if (__builtin_constant_p(fmt)) { \ 4373debb0a9SSteven Rostedt (Red Hat) static const char *trace_printk_fmt __used \ 43833def849SJoe Perches __section("__trace_printk_fmt") = \ 43948ead020SFrederic Weisbecker __builtin_constant_p(fmt) ? fmt : NULL; \ 4407bffc23eSIngo Molnar \ 44148ead020SFrederic Weisbecker __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \ 44248ead020SFrederic Weisbecker } else \ 44348ead020SFrederic Weisbecker __ftrace_vprintk(_THIS_IP_, fmt, vargs); \ 444769b0441SFrederic Weisbecker } while (0) 445769b0441SFrederic Weisbecker 4468db14860SNicolas Iooss extern __printf(2, 0) int 44748ead020SFrederic Weisbecker __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap); 44848ead020SFrederic Weisbecker 4498db14860SNicolas Iooss extern __printf(2, 0) int 450526211bcSIngo Molnar __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap); 451769b0441SFrederic Weisbecker 452cecbca96SFrederic Weisbecker extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode); 453526211bcSIngo Molnar #else 454526211bcSIngo Molnar static inline void tracing_start(void) { } 455526211bcSIngo Molnar static inline void tracing_stop(void) { } 456e67bc51eSDhaval Giani static inline void trace_dump_stack(int skip) { } 45793d68e52SSteven Rostedt 45893d68e52SSteven Rostedt static inline void tracing_on(void) { } 45993d68e52SSteven Rostedt static inline void tracing_off(void) { } 46093d68e52SSteven Rostedt static inline int tracing_is_on(void) { return 0; } 461ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot(void) { } 462ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot_alloc(void) { } 46393d68e52SSteven Rostedt 46460efc15aSMichal Hocko static inline __printf(1, 2) 46560efc15aSMichal Hocko int trace_printk(const char *fmt, ...) 466526211bcSIngo Molnar { 467526211bcSIngo Molnar return 0; 468526211bcSIngo Molnar } 4698db14860SNicolas Iooss static __printf(1, 0) inline int 470526211bcSIngo Molnar ftrace_vprintk(const char *fmt, va_list ap) 471526211bcSIngo Molnar { 472526211bcSIngo Molnar return 0; 473526211bcSIngo Molnar } 474cecbca96SFrederic Weisbecker static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { } 475769b0441SFrederic Weisbecker #endif /* CONFIG_TRACING */ 476526211bcSIngo Molnar 477cf14f27fSAlexei Starovoitov /* This counts to 12. Any more, it will return 13th argument. */ 478cf14f27fSAlexei Starovoitov #define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n 479cf14f27fSAlexei Starovoitov #define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) 480cf14f27fSAlexei Starovoitov 481cf14f27fSAlexei Starovoitov #define __CONCAT(a, b) a ## b 482cf14f27fSAlexei Starovoitov #define CONCATENATE(a, b) __CONCAT(a, b) 483cf14f27fSAlexei Starovoitov 484b9d4f426SArnaud Lacombe /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */ 485b9d4f426SArnaud Lacombe #ifdef CONFIG_FTRACE_MCOUNT_RECORD 486b9d4f426SArnaud Lacombe # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD 487b9d4f426SArnaud Lacombe #endif 4889d00f92fSWANG Cong 48958f86cc8SRusty Russell /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */ 49058f86cc8SRusty Russell #define VERIFY_OCTAL_PERMISSIONS(perms) \ 49158f86cc8SRusty Russell (BUILD_BUG_ON_ZERO((perms) < 0) + \ 49258f86cc8SRusty Russell BUILD_BUG_ON_ZERO((perms) > 0777) + \ 49328b8d0c8SGobinda Charan Maji /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */ \ 49428b8d0c8SGobinda Charan Maji BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) + \ 49528b8d0c8SGobinda Charan Maji BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) + \ 49628b8d0c8SGobinda Charan Maji /* USER_WRITABLE >= GROUP_WRITABLE */ \ 49728b8d0c8SGobinda Charan Maji BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) + \ 49828b8d0c8SGobinda Charan Maji /* OTHER_WRITABLE? Generally considered a bad idea. */ \ 49937549e94SRusty Russell BUILD_BUG_ON_ZERO((perms) & 2) + \ 50058f86cc8SRusty Russell (perms)) 5011da177e4SLinus Torvalds #endif 502