1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 2*40cbf09fSAndy Shevchenko /* 3*40cbf09fSAndy Shevchenko * NOTE: 4*40cbf09fSAndy Shevchenko * 5*40cbf09fSAndy Shevchenko * This header has combined a lot of unrelated to each other stuff. 6*40cbf09fSAndy Shevchenko * The process of splitting its content is in progress while keeping 7*40cbf09fSAndy Shevchenko * backward compatibility. That's why it's highly recommended NOT to 8*40cbf09fSAndy Shevchenko * include this header inside another header file, especially under 9*40cbf09fSAndy Shevchenko * generic or architectural include/ directory. 10*40cbf09fSAndy Shevchenko */ 111da177e4SLinus Torvalds #ifndef _LINUX_KERNEL_H 121da177e4SLinus Torvalds #define _LINUX_KERNEL_H 131da177e4SLinus Torvalds 14c0891ac1SAlexey Dobriyan #include <linux/stdarg.h> 1508c5188eSAndy Shevchenko #include <linux/align.h> 1654d50897SMasahiro Yamada #include <linux/limits.h> 171da177e4SLinus Torvalds #include <linux/linkage.h> 181da177e4SLinus Torvalds #include <linux/stddef.h> 191da177e4SLinus Torvalds #include <linux/types.h> 201da177e4SLinus Torvalds #include <linux/compiler.h> 21d2a8ebbfSAndy Shevchenko #include <linux/container_of.h> 221da177e4SLinus Torvalds #include <linux/bitops.h> 234c527293SAndy Shevchenko #include <linux/kstrtox.h> 24f0d1b0b3SDavid Howells #include <linux/log2.h> 25aa6159abSAndy Shevchenko #include <linux/math.h> 26b296a6d5SAndy Shevchenko #include <linux/minmax.h> 27e0deaff4SAndrew Morton #include <linux/typecheck.h> 28f39650deSAndy Shevchenko #include <linux/panic.h> 29968ab183SLinus Torvalds #include <linux/printk.h> 30c7acec71SIan Abbott #include <linux/build_bug.h> 31b965f1ddSPeter Zijlstra (Intel) #include <linux/static_call_types.h> 32e52340deSStephen Rothwell #include <linux/instruction_pointer.h> 331da177e4SLinus Torvalds #include <asm/byteorder.h> 34aa6159abSAndy Shevchenko 35607ca46eSDavid Howells #include <uapi/linux/kernel.h> 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds #define STACK_MAGIC 0xdeadbeef 381da177e4SLinus Torvalds 39e8c97af0SRandy Dunlap /** 40e8c97af0SRandy Dunlap * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value 41e8c97af0SRandy Dunlap * @x: value to repeat 42e8c97af0SRandy Dunlap * 43e8c97af0SRandy Dunlap * NOTE: @x is not checked for > 0xff; larger values produce odd results. 44e8c97af0SRandy Dunlap */ 4544696908SDavid S. Miller #define REPEAT_BYTE(x) ((~0ul / 0xff) * (x)) 4644696908SDavid S. Miller 47d3849953SChristoph Hellwig /* generic data direction definitions */ 48d3849953SChristoph Hellwig #define READ 0 49d3849953SChristoph Hellwig #define WRITE 1 50d3849953SChristoph Hellwig 51e8c97af0SRandy Dunlap /** 52e8c97af0SRandy Dunlap * ARRAY_SIZE - get the number of elements in array @arr 53e8c97af0SRandy Dunlap * @arr: array to be sized 54e8c97af0SRandy Dunlap */ 55c5e631cfSRusty Russell #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) 56c5e631cfSRusty Russell 570ab1438bSMasahiro Yamada #define PTR_IF(cond, ptr) ((cond) ? (ptr) : NULL) 580ab1438bSMasahiro Yamada 593ed605bcSGustavo Padovan #define u64_to_user_ptr(x) ( \ 603ed605bcSGustavo Padovan { \ 61a0fe2c64SJann Horn typecheck(u64, (x)); \ 62a0fe2c64SJann Horn (void __user *)(uintptr_t)(x); \ 633ed605bcSGustavo Padovan } \ 643ed605bcSGustavo Padovan ) 653ed605bcSGustavo Padovan 66218e180eSAndrew Morton /** 67218e180eSAndrew Morton * upper_32_bits - return bits 32-63 of a number 68218e180eSAndrew Morton * @n: the number we're accessing 69218e180eSAndrew Morton * 70218e180eSAndrew Morton * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress 71218e180eSAndrew Morton * the "right shift count >= width of type" warning when that quantity is 72218e180eSAndrew Morton * 32-bits. 73218e180eSAndrew Morton */ 74218e180eSAndrew Morton #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16)) 75218e180eSAndrew Morton 76204b885eSJoerg Roedel /** 77204b885eSJoerg Roedel * lower_32_bits - return bits 0-31 of a number 78204b885eSJoerg Roedel * @n: the number we're accessing 79204b885eSJoerg Roedel */ 80ef91bb19SHerbert Xu #define lower_32_bits(n) ((u32)((n) & 0xffffffff)) 81204b885eSJoerg Roedel 8203cb4473SJacob Keller /** 8303cb4473SJacob Keller * upper_16_bits - return bits 16-31 of a number 8403cb4473SJacob Keller * @n: the number we're accessing 8503cb4473SJacob Keller */ 8603cb4473SJacob Keller #define upper_16_bits(n) ((u16)((n) >> 16)) 8703cb4473SJacob Keller 8803cb4473SJacob Keller /** 8903cb4473SJacob Keller * lower_16_bits - return bits 0-15 of a number 9003cb4473SJacob Keller * @n: the number we're accessing 9103cb4473SJacob Keller */ 9203cb4473SJacob Keller #define lower_16_bits(n) ((u16)((n) & 0xffff)) 9303cb4473SJacob Keller 941da177e4SLinus Torvalds struct completion; 95df2e71fbS[email protected] struct user; 961da177e4SLinus Torvalds 97a8b76910SValentin Schneider #ifdef CONFIG_PREEMPT_VOLUNTARY_BUILD 98b965f1ddSPeter Zijlstra (Intel) 99b965f1ddSPeter Zijlstra (Intel) extern int __cond_resched(void); 100b965f1ddSPeter Zijlstra (Intel) # define might_resched() __cond_resched() 101b965f1ddSPeter Zijlstra (Intel) 102b965f1ddSPeter Zijlstra (Intel) #elif defined(CONFIG_PREEMPT_DYNAMIC) 103b965f1ddSPeter Zijlstra (Intel) 104b965f1ddSPeter Zijlstra (Intel) extern int __cond_resched(void); 105b965f1ddSPeter Zijlstra (Intel) 106b965f1ddSPeter Zijlstra (Intel) DECLARE_STATIC_CALL(might_resched, __cond_resched); 107b965f1ddSPeter Zijlstra (Intel) 108b965f1ddSPeter Zijlstra (Intel) static __always_inline void might_resched(void) 109b965f1ddSPeter Zijlstra (Intel) { 110ef72661eSPeter Zijlstra static_call_mod(might_resched)(); 111b965f1ddSPeter Zijlstra (Intel) } 112b965f1ddSPeter Zijlstra (Intel) 113070cb065SUwe Kleine-König #else 114b965f1ddSPeter Zijlstra (Intel) 115070cb065SUwe Kleine-König # define might_resched() do { } while (0) 116b965f1ddSPeter Zijlstra (Intel) 117b965f1ddSPeter Zijlstra (Intel) #endif /* CONFIG_PREEMPT_* */ 118070cb065SUwe Kleine-König 119d902db1eSFrederic Weisbecker #ifdef CONFIG_DEBUG_ATOMIC_SLEEP 12050e081b9SThomas Gleixner extern void __might_resched(const char *file, int line, unsigned int offsets); 12142a38756SThomas Gleixner extern void __might_sleep(const char *file, int line); 122568f1967SPeter Zijlstra extern void __cant_sleep(const char *file, int line, int preempt_offset); 12374d862b6SThomas Gleixner extern void __cant_migrate(const char *file, int line); 124568f1967SPeter Zijlstra 1251da177e4SLinus Torvalds /** 1261da177e4SLinus Torvalds * might_sleep - annotation for functions that can sleep 1271da177e4SLinus Torvalds * 1281da177e4SLinus Torvalds * this macro will print a stack trace if it is executed in an atomic 129312364f3SDaniel Vetter * context (spinlock, irq-handler, ...). Additional sections where blocking is 130312364f3SDaniel Vetter * not allowed can be annotated with non_block_start() and non_block_end() 131312364f3SDaniel Vetter * pairs. 1321da177e4SLinus Torvalds * 1331da177e4SLinus Torvalds * This is a useful debugging help to be able to catch problems early and not 134e20ec991SJim Cromie * be bitten later when the calling function happens to sleep when it is not 1351da177e4SLinus Torvalds * supposed to. 1361da177e4SLinus Torvalds */ 137f8cbd99bSIngo Molnar # define might_sleep() \ 13842a38756SThomas Gleixner do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0) 139568f1967SPeter Zijlstra /** 140568f1967SPeter Zijlstra * cant_sleep - annotation for functions that cannot sleep 141568f1967SPeter Zijlstra * 142568f1967SPeter Zijlstra * this macro will print a stack trace if it is executed with preemption enabled 143568f1967SPeter Zijlstra */ 144568f1967SPeter Zijlstra # define cant_sleep() \ 145568f1967SPeter Zijlstra do { __cant_sleep(__FILE__, __LINE__, 0); } while (0) 14600845eb9SLinus Torvalds # define sched_annotate_sleep() (current->task_state_change = 0) 14774d862b6SThomas Gleixner 14874d862b6SThomas Gleixner /** 14974d862b6SThomas Gleixner * cant_migrate - annotation for functions that cannot migrate 15074d862b6SThomas Gleixner * 15174d862b6SThomas Gleixner * Will print a stack trace if executed in code which is migratable 15274d862b6SThomas Gleixner */ 15374d862b6SThomas Gleixner # define cant_migrate() \ 15474d862b6SThomas Gleixner do { \ 15574d862b6SThomas Gleixner if (IS_ENABLED(CONFIG_SMP)) \ 15674d862b6SThomas Gleixner __cant_migrate(__FILE__, __LINE__); \ 15774d862b6SThomas Gleixner } while (0) 15874d862b6SThomas Gleixner 159312364f3SDaniel Vetter /** 160312364f3SDaniel Vetter * non_block_start - annotate the start of section where sleeping is prohibited 161312364f3SDaniel Vetter * 162312364f3SDaniel Vetter * This is on behalf of the oom reaper, specifically when it is calling the mmu 163312364f3SDaniel Vetter * notifiers. The problem is that if the notifier were to block on, for example, 164312364f3SDaniel Vetter * mutex_lock() and if the process which holds that mutex were to perform a 165312364f3SDaniel Vetter * sleeping memory allocation, the oom reaper is now blocked on completion of 166312364f3SDaniel Vetter * that memory allocation. Other blocking calls like wait_event() pose similar 167312364f3SDaniel Vetter * issues. 168312364f3SDaniel Vetter */ 169312364f3SDaniel Vetter # define non_block_start() (current->non_block_count++) 170312364f3SDaniel Vetter /** 171312364f3SDaniel Vetter * non_block_end - annotate the end of section where sleeping is prohibited 172312364f3SDaniel Vetter * 173312364f3SDaniel Vetter * Closes a section opened by non_block_start(). 174312364f3SDaniel Vetter */ 175312364f3SDaniel Vetter # define non_block_end() WARN_ON(current->non_block_count-- == 0) 176f8cbd99bSIngo Molnar #else 177874f670eSThomas Gleixner static inline void __might_resched(const char *file, int line, 17850e081b9SThomas Gleixner unsigned int offsets) { } 17942a38756SThomas Gleixner static inline void __might_sleep(const char *file, int line) { } 180f8cbd99bSIngo Molnar # define might_sleep() do { might_resched(); } while (0) 181568f1967SPeter Zijlstra # define cant_sleep() do { } while (0) 18274d862b6SThomas Gleixner # define cant_migrate() do { } while (0) 1831029a2b5SPeter Zijlstra # define sched_annotate_sleep() do { } while (0) 184312364f3SDaniel Vetter # define non_block_start() do { } while (0) 185312364f3SDaniel Vetter # define non_block_end() do { } while (0) 186f8cbd99bSIngo Molnar #endif 187f8cbd99bSIngo Molnar 188368a5fa1SHua Zhong #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0) 189f8cbd99bSIngo Molnar 190386e7906SAxel Lin #if defined(CONFIG_MMU) && \ 191386e7906SAxel Lin (defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)) 1929ec23531SDavid Hildenbrand #define might_fault() __might_fault(__FILE__, __LINE__) 1939ec23531SDavid Hildenbrand void __might_fault(const char *file, int line); 1943ee1afa3SNick Piggin #else 195662bbcb2SMichael S. Tsirkin static inline void might_fault(void) { } 1963ee1afa3SNick Piggin #endif 1973ee1afa3SNick Piggin 1989af6528eSPeter Zijlstra void do_exit(long error_code) __noreturn; 1999af6528eSPeter Zijlstra void complete_and_exit(struct completion *, long) __noreturn; 20033ee3b2eSAlexey Dobriyan 201d1be35cbSAndrei Vagin extern int num_to_str(char *buf, int size, 202d1be35cbSAndrei Vagin unsigned long long num, unsigned int width); 2031ac101a5SKAMEZAWA Hiroyuki 20467d0a075SJoe Perches /* lib/printf utilities */ 20567d0a075SJoe Perches 206b9075fa9SJoe Perches extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...); 207b9075fa9SJoe Perches extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list); 208b9075fa9SJoe Perches extern __printf(3, 4) 209b9075fa9SJoe Perches int snprintf(char *buf, size_t size, const char *fmt, ...); 210b9075fa9SJoe Perches extern __printf(3, 0) 211b9075fa9SJoe Perches int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); 212b9075fa9SJoe Perches extern __printf(3, 4) 213b9075fa9SJoe Perches int scnprintf(char *buf, size_t size, const char *fmt, ...); 214b9075fa9SJoe Perches extern __printf(3, 0) 215b9075fa9SJoe Perches int vscnprintf(char *buf, size_t size, const char *fmt, va_list args); 21648a27055SRasmus Villemoes extern __printf(2, 3) __malloc 217b9075fa9SJoe Perches char *kasprintf(gfp_t gfp, const char *fmt, ...); 21848a27055SRasmus Villemoes extern __printf(2, 0) __malloc 2198db14860SNicolas Iooss char *kvasprintf(gfp_t gfp, const char *fmt, va_list args); 2200a9df786SRasmus Villemoes extern __printf(2, 0) 2210a9df786SRasmus Villemoes const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args); 2221da177e4SLinus Torvalds 2236061d949SJoe Perches extern __scanf(2, 3) 2246061d949SJoe Perches int sscanf(const char *, const char *, ...); 2256061d949SJoe Perches extern __scanf(2, 0) 2266061d949SJoe Perches int vsscanf(const char *, const char *, va_list); 2271da177e4SLinus Torvalds 22879270291SStephen Boyd extern int no_hash_pointers_enable(char *str); 22979270291SStephen Boyd 2301da177e4SLinus Torvalds extern int get_option(char **str, int *pint); 2311da177e4SLinus Torvalds extern char *get_options(const char *str, int nints, int *ints); 232d974ae37SJeremy Fitzhardinge extern unsigned long long memparse(const char *ptr, char **retptr); 2336ccc72b8SDave Young extern bool parse_option_str(const char *str, const char *option); 234f51b17c8SBaoquan He extern char *next_arg(char *args, char **param, char **val); 2351da177e4SLinus Torvalds 2365e376613STrent Piepho extern int core_kernel_text(unsigned long addr); 2371da177e4SLinus Torvalds extern int __kernel_text_address(unsigned long addr); 2381da177e4SLinus Torvalds extern int kernel_text_address(unsigned long addr); 239ab7476cfSArjan van de Ven extern int func_ptr_is_kernel_text(void *ptr); 240ab7476cfSArjan van de Ven 2411da177e4SLinus Torvalds extern void bust_spinlocks(int yes); 2425375b708SHATAYAMA Daisuke 243b920de1bSDavid Howells extern int root_mountflags; 2441da177e4SLinus Torvalds 2452ce802f6STejun Heo extern bool early_boot_irqs_disabled; 2462ce802f6STejun Heo 24769a78ff2SThomas Gleixner /* 24869a78ff2SThomas Gleixner * Values used for system_state. Ordering of the states must not be changed 24969a78ff2SThomas Gleixner * as code checks for <, <=, >, >= STATE. 25069a78ff2SThomas Gleixner */ 2511da177e4SLinus Torvalds extern enum system_states { 2521da177e4SLinus Torvalds SYSTEM_BOOTING, 25369a78ff2SThomas Gleixner SYSTEM_SCHEDULING, 254d2635f20SChristophe Leroy SYSTEM_FREEING_INITMEM, 2551da177e4SLinus Torvalds SYSTEM_RUNNING, 2561da177e4SLinus Torvalds SYSTEM_HALT, 2571da177e4SLinus Torvalds SYSTEM_POWER_OFF, 2581da177e4SLinus Torvalds SYSTEM_RESTART, 259c1a957d1SThomas Gleixner SYSTEM_SUSPEND, 2601da177e4SLinus Torvalds } system_state; 2611da177e4SLinus Torvalds 2623fc95772SHarvey Harrison extern const char hex_asc[]; 2633fc95772SHarvey Harrison #define hex_asc_lo(x) hex_asc[((x) & 0x0f)] 2643fc95772SHarvey Harrison #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4] 2653fc95772SHarvey Harrison 26655036ba7SAndy Shevchenko static inline char *hex_byte_pack(char *buf, u8 byte) 2673fc95772SHarvey Harrison { 2683fc95772SHarvey Harrison *buf++ = hex_asc_hi(byte); 2693fc95772SHarvey Harrison *buf++ = hex_asc_lo(byte); 2703fc95772SHarvey Harrison return buf; 2713fc95772SHarvey Harrison } 27299eaf3c4SRandy Dunlap 273c26d436cSAndre Naujoks extern const char hex_asc_upper[]; 274c26d436cSAndre Naujoks #define hex_asc_upper_lo(x) hex_asc_upper[((x) & 0x0f)] 275c26d436cSAndre Naujoks #define hex_asc_upper_hi(x) hex_asc_upper[((x) & 0xf0) >> 4] 276c26d436cSAndre Naujoks 277c26d436cSAndre Naujoks static inline char *hex_byte_pack_upper(char *buf, u8 byte) 278c26d436cSAndre Naujoks { 279c26d436cSAndre Naujoks *buf++ = hex_asc_upper_hi(byte); 280c26d436cSAndre Naujoks *buf++ = hex_asc_upper_lo(byte); 281c26d436cSAndre Naujoks return buf; 282c26d436cSAndre Naujoks } 283c26d436cSAndre Naujoks 28490378889SAndy Shevchenko extern int hex_to_bin(char ch); 285b7804983SMimi Zohar extern int __must_check hex2bin(u8 *dst, const char *src, size_t count); 28653d91c5cSDavid Howells extern char *bin2hex(char *dst, const void *src, size_t count); 28790378889SAndy Shevchenko 288a69f5edbSJoe Perches bool mac_pton(const char *s, u8 *mac); 2894cd5773aSAndy Shevchenko 2908a64f336SJoe Perches /* 291526211bcSIngo Molnar * General tracing related utility functions - trace_printk(), 2922002c258SSteven Rostedt * tracing_on/tracing_off and tracing_start()/tracing_stop 2932002c258SSteven Rostedt * 2942002c258SSteven Rostedt * Use tracing_on/tracing_off when you want to quickly turn on or off 2952002c258SSteven Rostedt * tracing. It simply enables or disables the recording of the trace events. 296156f5a78SGeunSik Lim * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on 2972002c258SSteven Rostedt * file, which gives a means for the kernel and userspace to interact. 2982002c258SSteven Rostedt * Place a tracing_off() in the kernel where you want tracing to end. 2992002c258SSteven Rostedt * From user space, examine the trace, and then echo 1 > tracing_on 3002002c258SSteven Rostedt * to continue tracing. 3012002c258SSteven Rostedt * 3022002c258SSteven Rostedt * tracing_stop/tracing_start has slightly more overhead. It is used 3032002c258SSteven Rostedt * by things like suspend to ram where disabling the recording of the 3042002c258SSteven Rostedt * trace is not enough, but tracing must actually stop because things 3052002c258SSteven Rostedt * like calling smp_processor_id() may crash the system. 3062002c258SSteven Rostedt * 3072002c258SSteven Rostedt * Most likely, you want to use tracing_on/tracing_off. 308526211bcSIngo Molnar */ 309cecbca96SFrederic Weisbecker 310cecbca96SFrederic Weisbecker enum ftrace_dump_mode { 311cecbca96SFrederic Weisbecker DUMP_NONE, 312cecbca96SFrederic Weisbecker DUMP_ALL, 313cecbca96SFrederic Weisbecker DUMP_ORIG, 314cecbca96SFrederic Weisbecker }; 315cecbca96SFrederic Weisbecker 316526211bcSIngo Molnar #ifdef CONFIG_TRACING 31793d68e52SSteven Rostedt void tracing_on(void); 31893d68e52SSteven Rostedt void tracing_off(void); 31993d68e52SSteven Rostedt int tracing_is_on(void); 320ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot(void); 321ad909e21SSteven Rostedt (Red Hat) void tracing_snapshot_alloc(void); 32293d68e52SSteven Rostedt 323526211bcSIngo Molnar extern void tracing_start(void); 324526211bcSIngo Molnar extern void tracing_stop(void); 325526211bcSIngo Molnar 326b9075fa9SJoe Perches static inline __printf(1, 2) 327b9075fa9SJoe Perches void ____trace_printk_check_format(const char *fmt, ...) 328769b0441SFrederic Weisbecker { 329769b0441SFrederic Weisbecker } 330769b0441SFrederic Weisbecker #define __trace_printk_check_format(fmt, args...) \ 331769b0441SFrederic Weisbecker do { \ 332769b0441SFrederic Weisbecker if (0) \ 333769b0441SFrederic Weisbecker ____trace_printk_check_format(fmt, ##args); \ 334769b0441SFrederic Weisbecker } while (0) 335769b0441SFrederic Weisbecker 336526211bcSIngo Molnar /** 337526211bcSIngo Molnar * trace_printk - printf formatting in the ftrace buffer 338526211bcSIngo Molnar * @fmt: the printf format for printing 339526211bcSIngo Molnar * 340e8c97af0SRandy Dunlap * Note: __trace_printk is an internal function for trace_printk() and 341e8c97af0SRandy Dunlap * the @ip is passed in via the trace_printk() macro. 342526211bcSIngo Molnar * 343526211bcSIngo Molnar * This function allows a kernel developer to debug fast path sections 344526211bcSIngo Molnar * that printk is not appropriate for. By scattering in various 345526211bcSIngo Molnar * printk like tracing in the code, a developer can quickly see 346526211bcSIngo Molnar * where problems are occurring. 347526211bcSIngo Molnar * 348526211bcSIngo Molnar * This is intended as a debugging tool for the developer only. 349526211bcSIngo Molnar * Please refrain from leaving trace_printks scattered around in 35009ae7234SSteven Rostedt (Red Hat) * your code. (Extra memory is used for special buffers that are 351e8c97af0SRandy Dunlap * allocated when trace_printk() is used.) 3529d3c752cSSteven Rostedt (Red Hat) * 3538730662dSWei Wang * A little optimization trick is done here. If there's only one 3549d3c752cSSteven Rostedt (Red Hat) * argument, there's no need to scan the string for printf formats. 3559d3c752cSSteven Rostedt (Red Hat) * The trace_puts() will suffice. But how can we take advantage of 3569d3c752cSSteven Rostedt (Red Hat) * using trace_puts() when trace_printk() has only one argument? 3579d3c752cSSteven Rostedt (Red Hat) * By stringifying the args and checking the size we can tell 3589d3c752cSSteven Rostedt (Red Hat) * whether or not there are args. __stringify((__VA_ARGS__)) will 3599d3c752cSSteven Rostedt (Red Hat) * turn into "()\0" with a size of 3 when there are no args, anything 3609d3c752cSSteven Rostedt (Red Hat) * else will be bigger. All we need to do is define a string to this, 3619d3c752cSSteven Rostedt (Red Hat) * and then take its size and compare to 3. If it's bigger, use 3629d3c752cSSteven Rostedt (Red Hat) * do_trace_printk() otherwise, optimize it to trace_puts(). Then just 3639d3c752cSSteven Rostedt (Red Hat) * let gcc optimize the rest. 364526211bcSIngo Molnar */ 365769b0441SFrederic Weisbecker 3669d3c752cSSteven Rostedt (Red Hat) #define trace_printk(fmt, ...) \ 3679d3c752cSSteven Rostedt (Red Hat) do { \ 3689d3c752cSSteven Rostedt (Red Hat) char _______STR[] = __stringify((__VA_ARGS__)); \ 3699d3c752cSSteven Rostedt (Red Hat) if (sizeof(_______STR) > 3) \ 3709d3c752cSSteven Rostedt (Red Hat) do_trace_printk(fmt, ##__VA_ARGS__); \ 3719d3c752cSSteven Rostedt (Red Hat) else \ 3729d3c752cSSteven Rostedt (Red Hat) trace_puts(fmt); \ 3739d3c752cSSteven Rostedt (Red Hat) } while (0) 3749d3c752cSSteven Rostedt (Red Hat) 3759d3c752cSSteven Rostedt (Red Hat) #define do_trace_printk(fmt, args...) \ 376769b0441SFrederic Weisbecker do { \ 3773debb0a9SSteven Rostedt (Red Hat) static const char *trace_printk_fmt __used \ 37833def849SJoe Perches __section("__trace_printk_fmt") = \ 37948ead020SFrederic Weisbecker __builtin_constant_p(fmt) ? fmt : NULL; \ 38048ead020SFrederic Weisbecker \ 38107d777feSSteven Rostedt __trace_printk_check_format(fmt, ##args); \ 38207d777feSSteven Rostedt \ 38307d777feSSteven Rostedt if (__builtin_constant_p(fmt)) \ 38448ead020SFrederic Weisbecker __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \ 38507d777feSSteven Rostedt else \ 38648ead020SFrederic Weisbecker __trace_printk(_THIS_IP_, fmt, ##args); \ 387769b0441SFrederic Weisbecker } while (0) 388769b0441SFrederic Weisbecker 389b9075fa9SJoe Perches extern __printf(2, 3) 390b9075fa9SJoe Perches int __trace_bprintk(unsigned long ip, const char *fmt, ...); 39148ead020SFrederic Weisbecker 392b9075fa9SJoe Perches extern __printf(2, 3) 393b9075fa9SJoe Perches int __trace_printk(unsigned long ip, const char *fmt, ...); 394769b0441SFrederic Weisbecker 39509ae7234SSteven Rostedt (Red Hat) /** 39609ae7234SSteven Rostedt (Red Hat) * trace_puts - write a string into the ftrace buffer 39709ae7234SSteven Rostedt (Red Hat) * @str: the string to record 39809ae7234SSteven Rostedt (Red Hat) * 39909ae7234SSteven Rostedt (Red Hat) * Note: __trace_bputs is an internal function for trace_puts and 40009ae7234SSteven Rostedt (Red Hat) * the @ip is passed in via the trace_puts macro. 40109ae7234SSteven Rostedt (Red Hat) * 40209ae7234SSteven Rostedt (Red Hat) * This is similar to trace_printk() but is made for those really fast 403e8c97af0SRandy Dunlap * paths that a developer wants the least amount of "Heisenbug" effects, 40409ae7234SSteven Rostedt (Red Hat) * where the processing of the print format is still too much. 40509ae7234SSteven Rostedt (Red Hat) * 40609ae7234SSteven Rostedt (Red Hat) * This function allows a kernel developer to debug fast path sections 40709ae7234SSteven Rostedt (Red Hat) * that printk is not appropriate for. By scattering in various 40809ae7234SSteven Rostedt (Red Hat) * printk like tracing in the code, a developer can quickly see 40909ae7234SSteven Rostedt (Red Hat) * where problems are occurring. 41009ae7234SSteven Rostedt (Red Hat) * 41109ae7234SSteven Rostedt (Red Hat) * This is intended as a debugging tool for the developer only. 41209ae7234SSteven Rostedt (Red Hat) * Please refrain from leaving trace_puts scattered around in 41309ae7234SSteven Rostedt (Red Hat) * your code. (Extra memory is used for special buffers that are 414e8c97af0SRandy Dunlap * allocated when trace_puts() is used.) 41509ae7234SSteven Rostedt (Red Hat) * 41609ae7234SSteven Rostedt (Red Hat) * Returns: 0 if nothing was written, positive # if string was. 41709ae7234SSteven Rostedt (Red Hat) * (1 when __trace_bputs is used, strlen(str) when __trace_puts is used) 41809ae7234SSteven Rostedt (Red Hat) */ 41909ae7234SSteven Rostedt (Red Hat) 42009ae7234SSteven Rostedt (Red Hat) #define trace_puts(str) ({ \ 4213debb0a9SSteven Rostedt (Red Hat) static const char *trace_printk_fmt __used \ 42233def849SJoe Perches __section("__trace_printk_fmt") = \ 42309ae7234SSteven Rostedt (Red Hat) __builtin_constant_p(str) ? str : NULL; \ 42409ae7234SSteven Rostedt (Red Hat) \ 42509ae7234SSteven Rostedt (Red Hat) if (__builtin_constant_p(str)) \ 42609ae7234SSteven Rostedt (Red Hat) __trace_bputs(_THIS_IP_, trace_printk_fmt); \ 42709ae7234SSteven Rostedt (Red Hat) else \ 42809ae7234SSteven Rostedt (Red Hat) __trace_puts(_THIS_IP_, str, strlen(str)); \ 42909ae7234SSteven Rostedt (Red Hat) }) 430bcf312cfSSteven Rostedt extern int __trace_bputs(unsigned long ip, const char *str); 431bcf312cfSSteven Rostedt extern int __trace_puts(unsigned long ip, const char *str, int size); 43209ae7234SSteven Rostedt (Red Hat) 433c142be8eSSteven Rostedt (Red Hat) extern void trace_dump_stack(int skip); 43403889384SSteven Rostedt 43548ead020SFrederic Weisbecker /* 43648ead020SFrederic Weisbecker * The double __builtin_constant_p is because gcc will give us an error 43748ead020SFrederic Weisbecker * if we try to allocate the static variable to fmt if it is not a 43848ead020SFrederic Weisbecker * constant. Even with the outer if statement. 43948ead020SFrederic Weisbecker */ 440769b0441SFrederic Weisbecker #define ftrace_vprintk(fmt, vargs) \ 441769b0441SFrederic Weisbecker do { \ 44248ead020SFrederic Weisbecker if (__builtin_constant_p(fmt)) { \ 4433debb0a9SSteven Rostedt (Red Hat) static const char *trace_printk_fmt __used \ 44433def849SJoe Perches __section("__trace_printk_fmt") = \ 44548ead020SFrederic Weisbecker __builtin_constant_p(fmt) ? fmt : NULL; \ 4467bffc23eSIngo Molnar \ 44748ead020SFrederic Weisbecker __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \ 44848ead020SFrederic Weisbecker } else \ 44948ead020SFrederic Weisbecker __ftrace_vprintk(_THIS_IP_, fmt, vargs); \ 450769b0441SFrederic Weisbecker } while (0) 451769b0441SFrederic Weisbecker 4528db14860SNicolas Iooss extern __printf(2, 0) int 45348ead020SFrederic Weisbecker __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap); 45448ead020SFrederic Weisbecker 4558db14860SNicolas Iooss extern __printf(2, 0) int 456526211bcSIngo Molnar __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap); 457769b0441SFrederic Weisbecker 458cecbca96SFrederic Weisbecker extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode); 459526211bcSIngo Molnar #else 460526211bcSIngo Molnar static inline void tracing_start(void) { } 461526211bcSIngo Molnar static inline void tracing_stop(void) { } 462e67bc51eSDhaval Giani static inline void trace_dump_stack(int skip) { } 46393d68e52SSteven Rostedt 46493d68e52SSteven Rostedt static inline void tracing_on(void) { } 46593d68e52SSteven Rostedt static inline void tracing_off(void) { } 46693d68e52SSteven Rostedt static inline int tracing_is_on(void) { return 0; } 467ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot(void) { } 468ad909e21SSteven Rostedt (Red Hat) static inline void tracing_snapshot_alloc(void) { } 46993d68e52SSteven Rostedt 47060efc15aSMichal Hocko static inline __printf(1, 2) 47160efc15aSMichal Hocko int trace_printk(const char *fmt, ...) 472526211bcSIngo Molnar { 473526211bcSIngo Molnar return 0; 474526211bcSIngo Molnar } 4758db14860SNicolas Iooss static __printf(1, 0) inline int 476526211bcSIngo Molnar ftrace_vprintk(const char *fmt, va_list ap) 477526211bcSIngo Molnar { 478526211bcSIngo Molnar return 0; 479526211bcSIngo Molnar } 480cecbca96SFrederic Weisbecker static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { } 481769b0441SFrederic Weisbecker #endif /* CONFIG_TRACING */ 482526211bcSIngo Molnar 483cf14f27fSAlexei Starovoitov /* This counts to 12. Any more, it will return 13th argument. */ 484cf14f27fSAlexei Starovoitov #define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n 485cf14f27fSAlexei Starovoitov #define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) 486cf14f27fSAlexei Starovoitov 487cf14f27fSAlexei Starovoitov #define __CONCAT(a, b) a ## b 488cf14f27fSAlexei Starovoitov #define CONCATENATE(a, b) __CONCAT(a, b) 489cf14f27fSAlexei Starovoitov 490b9d4f426SArnaud Lacombe /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */ 491b9d4f426SArnaud Lacombe #ifdef CONFIG_FTRACE_MCOUNT_RECORD 492b9d4f426SArnaud Lacombe # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD 493b9d4f426SArnaud Lacombe #endif 4949d00f92fSWANG Cong 49558f86cc8SRusty Russell /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */ 49658f86cc8SRusty Russell #define VERIFY_OCTAL_PERMISSIONS(perms) \ 49758f86cc8SRusty Russell (BUILD_BUG_ON_ZERO((perms) < 0) + \ 49858f86cc8SRusty Russell BUILD_BUG_ON_ZERO((perms) > 0777) + \ 49928b8d0c8SGobinda Charan Maji /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */ \ 50028b8d0c8SGobinda Charan Maji BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) + \ 50128b8d0c8SGobinda Charan Maji BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) + \ 50228b8d0c8SGobinda Charan Maji /* USER_WRITABLE >= GROUP_WRITABLE */ \ 50328b8d0c8SGobinda Charan Maji BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) + \ 50428b8d0c8SGobinda Charan Maji /* OTHER_WRITABLE? Generally considered a bad idea. */ \ 50537549e94SRusty Russell BUILD_BUG_ON_ZERO((perms) & 2) + \ 50658f86cc8SRusty Russell (perms)) 5071da177e4SLinus Torvalds #endif 508