at v5.11-rc3 26 kB view raw
1/* SPDX-License-Identifier: GPL-2.0 */ 2#ifndef _LINUX_KERNEL_H 3#define _LINUX_KERNEL_H 4 5#include <stdarg.h> 6#include <linux/limits.h> 7#include <linux/linkage.h> 8#include <linux/stddef.h> 9#include <linux/types.h> 10#include <linux/compiler.h> 11#include <linux/bitops.h> 12#include <linux/log2.h> 13#include <linux/math.h> 14#include <linux/minmax.h> 15#include <linux/typecheck.h> 16#include <linux/printk.h> 17#include <linux/build_bug.h> 18 19#include <asm/byteorder.h> 20 21#include <uapi/linux/kernel.h> 22 23#define STACK_MAGIC 0xdeadbeef 24 25/** 26 * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value 27 * @x: value to repeat 28 * 29 * NOTE: @x is not checked for > 0xff; larger values produce odd results. 30 */ 31#define REPEAT_BYTE(x) ((~0ul / 0xff) * (x)) 32 33/* @a is a power of 2 value */ 34#define ALIGN(x, a) __ALIGN_KERNEL((x), (a)) 35#define ALIGN_DOWN(x, a) __ALIGN_KERNEL((x) - ((a) - 1), (a)) 36#define __ALIGN_MASK(x, mask) __ALIGN_KERNEL_MASK((x), (mask)) 37#define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a))) 38#define PTR_ALIGN_DOWN(p, a) ((typeof(p))ALIGN_DOWN((unsigned long)(p), (a))) 39#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0) 40 41/* generic data direction definitions */ 42#define READ 0 43#define WRITE 1 44 45/** 46 * ARRAY_SIZE - get the number of elements in array @arr 47 * @arr: array to be sized 48 */ 49#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) 50 51#define u64_to_user_ptr(x) ( \ 52{ \ 53 typecheck(u64, (x)); \ 54 (void __user *)(uintptr_t)(x); \ 55} \ 56) 57 58#define typeof_member(T, m) typeof(((T*)0)->m) 59 60#define _RET_IP_ (unsigned long)__builtin_return_address(0) 61#define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; }) 62 63/** 64 * upper_32_bits - return bits 32-63 of a number 65 * @n: the number we're accessing 66 * 67 * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress 68 * the "right shift count >= width of type" warning when that quantity is 69 * 32-bits. 70 */ 71#define upper_32_bits(n) ((u32)(((n) >> 16) >> 16)) 72 73/** 74 * lower_32_bits - return bits 0-31 of a number 75 * @n: the number we're accessing 76 */ 77#define lower_32_bits(n) ((u32)((n) & 0xffffffff)) 78 79struct completion; 80struct pt_regs; 81struct user; 82 83#ifdef CONFIG_PREEMPT_VOLUNTARY 84extern int _cond_resched(void); 85# define might_resched() _cond_resched() 86#else 87# define might_resched() do { } while (0) 88#endif 89 90#ifdef CONFIG_DEBUG_ATOMIC_SLEEP 91extern void ___might_sleep(const char *file, int line, int preempt_offset); 92extern void __might_sleep(const char *file, int line, int preempt_offset); 93extern void __cant_sleep(const char *file, int line, int preempt_offset); 94extern void __cant_migrate(const char *file, int line); 95 96/** 97 * might_sleep - annotation for functions that can sleep 98 * 99 * this macro will print a stack trace if it is executed in an atomic 100 * context (spinlock, irq-handler, ...). Additional sections where blocking is 101 * not allowed can be annotated with non_block_start() and non_block_end() 102 * pairs. 103 * 104 * This is a useful debugging help to be able to catch problems early and not 105 * be bitten later when the calling function happens to sleep when it is not 106 * supposed to. 107 */ 108# define might_sleep() \ 109 do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0) 110/** 111 * cant_sleep - annotation for functions that cannot sleep 112 * 113 * this macro will print a stack trace if it is executed with preemption enabled 114 */ 115# define cant_sleep() \ 116 do { __cant_sleep(__FILE__, __LINE__, 0); } while (0) 117# define sched_annotate_sleep() (current->task_state_change = 0) 118 119/** 120 * cant_migrate - annotation for functions that cannot migrate 121 * 122 * Will print a stack trace if executed in code which is migratable 123 */ 124# define cant_migrate() \ 125 do { \ 126 if (IS_ENABLED(CONFIG_SMP)) \ 127 __cant_migrate(__FILE__, __LINE__); \ 128 } while (0) 129 130/** 131 * non_block_start - annotate the start of section where sleeping is prohibited 132 * 133 * This is on behalf of the oom reaper, specifically when it is calling the mmu 134 * notifiers. The problem is that if the notifier were to block on, for example, 135 * mutex_lock() and if the process which holds that mutex were to perform a 136 * sleeping memory allocation, the oom reaper is now blocked on completion of 137 * that memory allocation. Other blocking calls like wait_event() pose similar 138 * issues. 139 */ 140# define non_block_start() (current->non_block_count++) 141/** 142 * non_block_end - annotate the end of section where sleeping is prohibited 143 * 144 * Closes a section opened by non_block_start(). 145 */ 146# define non_block_end() WARN_ON(current->non_block_count-- == 0) 147#else 148 static inline void ___might_sleep(const char *file, int line, 149 int preempt_offset) { } 150 static inline void __might_sleep(const char *file, int line, 151 int preempt_offset) { } 152# define might_sleep() do { might_resched(); } while (0) 153# define cant_sleep() do { } while (0) 154# define cant_migrate() do { } while (0) 155# define sched_annotate_sleep() do { } while (0) 156# define non_block_start() do { } while (0) 157# define non_block_end() do { } while (0) 158#endif 159 160#define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0) 161 162#if defined(CONFIG_MMU) && \ 163 (defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)) 164#define might_fault() __might_fault(__FILE__, __LINE__) 165void __might_fault(const char *file, int line); 166#else 167static inline void might_fault(void) { } 168#endif 169 170extern struct atomic_notifier_head panic_notifier_list; 171extern long (*panic_blink)(int state); 172__printf(1, 2) 173void panic(const char *fmt, ...) __noreturn __cold; 174void nmi_panic(struct pt_regs *regs, const char *msg); 175extern void oops_enter(void); 176extern void oops_exit(void); 177extern bool oops_may_print(void); 178void do_exit(long error_code) __noreturn; 179void complete_and_exit(struct completion *, long) __noreturn; 180 181/* Internal, do not use. */ 182int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res); 183int __must_check _kstrtol(const char *s, unsigned int base, long *res); 184 185int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res); 186int __must_check kstrtoll(const char *s, unsigned int base, long long *res); 187 188/** 189 * kstrtoul - convert a string to an unsigned long 190 * @s: The start of the string. The string must be null-terminated, and may also 191 * include a single newline before its terminating null. The first character 192 * may also be a plus sign, but not a minus sign. 193 * @base: The number base to use. The maximum supported base is 16. If base is 194 * given as 0, then the base of the string is automatically detected with the 195 * conventional semantics - If it begins with 0x the number will be parsed as a 196 * hexadecimal (case insensitive), if it otherwise begins with 0, it will be 197 * parsed as an octal number. Otherwise it will be parsed as a decimal. 198 * @res: Where to write the result of the conversion on success. 199 * 200 * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. 201 * Preferred over simple_strtoul(). Return code must be checked. 202*/ 203static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res) 204{ 205 /* 206 * We want to shortcut function call, but 207 * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0. 208 */ 209 if (sizeof(unsigned long) == sizeof(unsigned long long) && 210 __alignof__(unsigned long) == __alignof__(unsigned long long)) 211 return kstrtoull(s, base, (unsigned long long *)res); 212 else 213 return _kstrtoul(s, base, res); 214} 215 216/** 217 * kstrtol - convert a string to a long 218 * @s: The start of the string. The string must be null-terminated, and may also 219 * include a single newline before its terminating null. The first character 220 * may also be a plus sign or a minus sign. 221 * @base: The number base to use. The maximum supported base is 16. If base is 222 * given as 0, then the base of the string is automatically detected with the 223 * conventional semantics - If it begins with 0x the number will be parsed as a 224 * hexadecimal (case insensitive), if it otherwise begins with 0, it will be 225 * parsed as an octal number. Otherwise it will be parsed as a decimal. 226 * @res: Where to write the result of the conversion on success. 227 * 228 * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. 229 * Preferred over simple_strtol(). Return code must be checked. 230 */ 231static inline int __must_check kstrtol(const char *s, unsigned int base, long *res) 232{ 233 /* 234 * We want to shortcut function call, but 235 * __builtin_types_compatible_p(long, long long) = 0. 236 */ 237 if (sizeof(long) == sizeof(long long) && 238 __alignof__(long) == __alignof__(long long)) 239 return kstrtoll(s, base, (long long *)res); 240 else 241 return _kstrtol(s, base, res); 242} 243 244int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res); 245int __must_check kstrtoint(const char *s, unsigned int base, int *res); 246 247static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res) 248{ 249 return kstrtoull(s, base, res); 250} 251 252static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res) 253{ 254 return kstrtoll(s, base, res); 255} 256 257static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res) 258{ 259 return kstrtouint(s, base, res); 260} 261 262static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res) 263{ 264 return kstrtoint(s, base, res); 265} 266 267int __must_check kstrtou16(const char *s, unsigned int base, u16 *res); 268int __must_check kstrtos16(const char *s, unsigned int base, s16 *res); 269int __must_check kstrtou8(const char *s, unsigned int base, u8 *res); 270int __must_check kstrtos8(const char *s, unsigned int base, s8 *res); 271int __must_check kstrtobool(const char *s, bool *res); 272 273int __must_check kstrtoull_from_user(const char __user *s, size_t count, unsigned int base, unsigned long long *res); 274int __must_check kstrtoll_from_user(const char __user *s, size_t count, unsigned int base, long long *res); 275int __must_check kstrtoul_from_user(const char __user *s, size_t count, unsigned int base, unsigned long *res); 276int __must_check kstrtol_from_user(const char __user *s, size_t count, unsigned int base, long *res); 277int __must_check kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, unsigned int *res); 278int __must_check kstrtoint_from_user(const char __user *s, size_t count, unsigned int base, int *res); 279int __must_check kstrtou16_from_user(const char __user *s, size_t count, unsigned int base, u16 *res); 280int __must_check kstrtos16_from_user(const char __user *s, size_t count, unsigned int base, s16 *res); 281int __must_check kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, u8 *res); 282int __must_check kstrtos8_from_user(const char __user *s, size_t count, unsigned int base, s8 *res); 283int __must_check kstrtobool_from_user(const char __user *s, size_t count, bool *res); 284 285static inline int __must_check kstrtou64_from_user(const char __user *s, size_t count, unsigned int base, u64 *res) 286{ 287 return kstrtoull_from_user(s, count, base, res); 288} 289 290static inline int __must_check kstrtos64_from_user(const char __user *s, size_t count, unsigned int base, s64 *res) 291{ 292 return kstrtoll_from_user(s, count, base, res); 293} 294 295static inline int __must_check kstrtou32_from_user(const char __user *s, size_t count, unsigned int base, u32 *res) 296{ 297 return kstrtouint_from_user(s, count, base, res); 298} 299 300static inline int __must_check kstrtos32_from_user(const char __user *s, size_t count, unsigned int base, s32 *res) 301{ 302 return kstrtoint_from_user(s, count, base, res); 303} 304 305/* 306 * Use kstrto<foo> instead. 307 * 308 * NOTE: simple_strto<foo> does not check for the range overflow and, 309 * depending on the input, may give interesting results. 310 * 311 * Use these functions if and only if you cannot use kstrto<foo>, because 312 * the conversion ends on the first non-digit character, which may be far 313 * beyond the supported range. It might be useful to parse the strings like 314 * 10x50 or 12:21 without altering original string or temporary buffer in use. 315 * Keep in mind above caveat. 316 */ 317 318extern unsigned long simple_strtoul(const char *,char **,unsigned int); 319extern long simple_strtol(const char *,char **,unsigned int); 320extern unsigned long long simple_strtoull(const char *,char **,unsigned int); 321extern long long simple_strtoll(const char *,char **,unsigned int); 322 323extern int num_to_str(char *buf, int size, 324 unsigned long long num, unsigned int width); 325 326/* lib/printf utilities */ 327 328extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...); 329extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list); 330extern __printf(3, 4) 331int snprintf(char *buf, size_t size, const char *fmt, ...); 332extern __printf(3, 0) 333int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); 334extern __printf(3, 4) 335int scnprintf(char *buf, size_t size, const char *fmt, ...); 336extern __printf(3, 0) 337int vscnprintf(char *buf, size_t size, const char *fmt, va_list args); 338extern __printf(2, 3) __malloc 339char *kasprintf(gfp_t gfp, const char *fmt, ...); 340extern __printf(2, 0) __malloc 341char *kvasprintf(gfp_t gfp, const char *fmt, va_list args); 342extern __printf(2, 0) 343const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args); 344 345extern __scanf(2, 3) 346int sscanf(const char *, const char *, ...); 347extern __scanf(2, 0) 348int vsscanf(const char *, const char *, va_list); 349 350extern int get_option(char **str, int *pint); 351extern char *get_options(const char *str, int nints, int *ints); 352extern unsigned long long memparse(const char *ptr, char **retptr); 353extern bool parse_option_str(const char *str, const char *option); 354extern char *next_arg(char *args, char **param, char **val); 355 356extern int core_kernel_text(unsigned long addr); 357extern int init_kernel_text(unsigned long addr); 358extern int core_kernel_data(unsigned long addr); 359extern int __kernel_text_address(unsigned long addr); 360extern int kernel_text_address(unsigned long addr); 361extern int func_ptr_is_kernel_text(void *ptr); 362 363#ifdef CONFIG_SMP 364extern unsigned int sysctl_oops_all_cpu_backtrace; 365#else 366#define sysctl_oops_all_cpu_backtrace 0 367#endif /* CONFIG_SMP */ 368 369extern void bust_spinlocks(int yes); 370extern int panic_timeout; 371extern unsigned long panic_print; 372extern int panic_on_oops; 373extern int panic_on_unrecovered_nmi; 374extern int panic_on_io_nmi; 375extern int panic_on_warn; 376extern unsigned long panic_on_taint; 377extern bool panic_on_taint_nousertaint; 378extern int sysctl_panic_on_rcu_stall; 379extern int sysctl_max_rcu_stall_to_panic; 380extern int sysctl_panic_on_stackoverflow; 381 382extern bool crash_kexec_post_notifiers; 383 384/* 385 * panic_cpu is used for synchronizing panic() and crash_kexec() execution. It 386 * holds a CPU number which is executing panic() currently. A value of 387 * PANIC_CPU_INVALID means no CPU has entered panic() or crash_kexec(). 388 */ 389extern atomic_t panic_cpu; 390#define PANIC_CPU_INVALID -1 391 392/* 393 * Only to be used by arch init code. If the user over-wrote the default 394 * CONFIG_PANIC_TIMEOUT, honor it. 395 */ 396static inline void set_arch_panic_timeout(int timeout, int arch_default_timeout) 397{ 398 if (panic_timeout == arch_default_timeout) 399 panic_timeout = timeout; 400} 401extern const char *print_tainted(void); 402enum lockdep_ok { 403 LOCKDEP_STILL_OK, 404 LOCKDEP_NOW_UNRELIABLE 405}; 406extern void add_taint(unsigned flag, enum lockdep_ok); 407extern int test_taint(unsigned flag); 408extern unsigned long get_taint(void); 409extern int root_mountflags; 410 411extern bool early_boot_irqs_disabled; 412 413/* 414 * Values used for system_state. Ordering of the states must not be changed 415 * as code checks for <, <=, >, >= STATE. 416 */ 417extern enum system_states { 418 SYSTEM_BOOTING, 419 SYSTEM_SCHEDULING, 420 SYSTEM_RUNNING, 421 SYSTEM_HALT, 422 SYSTEM_POWER_OFF, 423 SYSTEM_RESTART, 424 SYSTEM_SUSPEND, 425} system_state; 426 427/* This cannot be an enum because some may be used in assembly source. */ 428#define TAINT_PROPRIETARY_MODULE 0 429#define TAINT_FORCED_MODULE 1 430#define TAINT_CPU_OUT_OF_SPEC 2 431#define TAINT_FORCED_RMMOD 3 432#define TAINT_MACHINE_CHECK 4 433#define TAINT_BAD_PAGE 5 434#define TAINT_USER 6 435#define TAINT_DIE 7 436#define TAINT_OVERRIDDEN_ACPI_TABLE 8 437#define TAINT_WARN 9 438#define TAINT_CRAP 10 439#define TAINT_FIRMWARE_WORKAROUND 11 440#define TAINT_OOT_MODULE 12 441#define TAINT_UNSIGNED_MODULE 13 442#define TAINT_SOFTLOCKUP 14 443#define TAINT_LIVEPATCH 15 444#define TAINT_AUX 16 445#define TAINT_RANDSTRUCT 17 446#define TAINT_FLAGS_COUNT 18 447#define TAINT_FLAGS_MAX ((1UL << TAINT_FLAGS_COUNT) - 1) 448 449struct taint_flag { 450 char c_true; /* character printed when tainted */ 451 char c_false; /* character printed when not tainted */ 452 bool module; /* also show as a per-module taint flag */ 453}; 454 455extern const struct taint_flag taint_flags[TAINT_FLAGS_COUNT]; 456 457extern const char hex_asc[]; 458#define hex_asc_lo(x) hex_asc[((x) & 0x0f)] 459#define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4] 460 461static inline char *hex_byte_pack(char *buf, u8 byte) 462{ 463 *buf++ = hex_asc_hi(byte); 464 *buf++ = hex_asc_lo(byte); 465 return buf; 466} 467 468extern const char hex_asc_upper[]; 469#define hex_asc_upper_lo(x) hex_asc_upper[((x) & 0x0f)] 470#define hex_asc_upper_hi(x) hex_asc_upper[((x) & 0xf0) >> 4] 471 472static inline char *hex_byte_pack_upper(char *buf, u8 byte) 473{ 474 *buf++ = hex_asc_upper_hi(byte); 475 *buf++ = hex_asc_upper_lo(byte); 476 return buf; 477} 478 479extern int hex_to_bin(char ch); 480extern int __must_check hex2bin(u8 *dst, const char *src, size_t count); 481extern char *bin2hex(char *dst, const void *src, size_t count); 482 483bool mac_pton(const char *s, u8 *mac); 484 485/* 486 * General tracing related utility functions - trace_printk(), 487 * tracing_on/tracing_off and tracing_start()/tracing_stop 488 * 489 * Use tracing_on/tracing_off when you want to quickly turn on or off 490 * tracing. It simply enables or disables the recording of the trace events. 491 * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on 492 * file, which gives a means for the kernel and userspace to interact. 493 * Place a tracing_off() in the kernel where you want tracing to end. 494 * From user space, examine the trace, and then echo 1 > tracing_on 495 * to continue tracing. 496 * 497 * tracing_stop/tracing_start has slightly more overhead. It is used 498 * by things like suspend to ram where disabling the recording of the 499 * trace is not enough, but tracing must actually stop because things 500 * like calling smp_processor_id() may crash the system. 501 * 502 * Most likely, you want to use tracing_on/tracing_off. 503 */ 504 505enum ftrace_dump_mode { 506 DUMP_NONE, 507 DUMP_ALL, 508 DUMP_ORIG, 509}; 510 511#ifdef CONFIG_TRACING 512void tracing_on(void); 513void tracing_off(void); 514int tracing_is_on(void); 515void tracing_snapshot(void); 516void tracing_snapshot_alloc(void); 517 518extern void tracing_start(void); 519extern void tracing_stop(void); 520 521static inline __printf(1, 2) 522void ____trace_printk_check_format(const char *fmt, ...) 523{ 524} 525#define __trace_printk_check_format(fmt, args...) \ 526do { \ 527 if (0) \ 528 ____trace_printk_check_format(fmt, ##args); \ 529} while (0) 530 531/** 532 * trace_printk - printf formatting in the ftrace buffer 533 * @fmt: the printf format for printing 534 * 535 * Note: __trace_printk is an internal function for trace_printk() and 536 * the @ip is passed in via the trace_printk() macro. 537 * 538 * This function allows a kernel developer to debug fast path sections 539 * that printk is not appropriate for. By scattering in various 540 * printk like tracing in the code, a developer can quickly see 541 * where problems are occurring. 542 * 543 * This is intended as a debugging tool for the developer only. 544 * Please refrain from leaving trace_printks scattered around in 545 * your code. (Extra memory is used for special buffers that are 546 * allocated when trace_printk() is used.) 547 * 548 * A little optimization trick is done here. If there's only one 549 * argument, there's no need to scan the string for printf formats. 550 * The trace_puts() will suffice. But how can we take advantage of 551 * using trace_puts() when trace_printk() has only one argument? 552 * By stringifying the args and checking the size we can tell 553 * whether or not there are args. __stringify((__VA_ARGS__)) will 554 * turn into "()\0" with a size of 3 when there are no args, anything 555 * else will be bigger. All we need to do is define a string to this, 556 * and then take its size and compare to 3. If it's bigger, use 557 * do_trace_printk() otherwise, optimize it to trace_puts(). Then just 558 * let gcc optimize the rest. 559 */ 560 561#define trace_printk(fmt, ...) \ 562do { \ 563 char _______STR[] = __stringify((__VA_ARGS__)); \ 564 if (sizeof(_______STR) > 3) \ 565 do_trace_printk(fmt, ##__VA_ARGS__); \ 566 else \ 567 trace_puts(fmt); \ 568} while (0) 569 570#define do_trace_printk(fmt, args...) \ 571do { \ 572 static const char *trace_printk_fmt __used \ 573 __section("__trace_printk_fmt") = \ 574 __builtin_constant_p(fmt) ? fmt : NULL; \ 575 \ 576 __trace_printk_check_format(fmt, ##args); \ 577 \ 578 if (__builtin_constant_p(fmt)) \ 579 __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \ 580 else \ 581 __trace_printk(_THIS_IP_, fmt, ##args); \ 582} while (0) 583 584extern __printf(2, 3) 585int __trace_bprintk(unsigned long ip, const char *fmt, ...); 586 587extern __printf(2, 3) 588int __trace_printk(unsigned long ip, const char *fmt, ...); 589 590/** 591 * trace_puts - write a string into the ftrace buffer 592 * @str: the string to record 593 * 594 * Note: __trace_bputs is an internal function for trace_puts and 595 * the @ip is passed in via the trace_puts macro. 596 * 597 * This is similar to trace_printk() but is made for those really fast 598 * paths that a developer wants the least amount of "Heisenbug" effects, 599 * where the processing of the print format is still too much. 600 * 601 * This function allows a kernel developer to debug fast path sections 602 * that printk is not appropriate for. By scattering in various 603 * printk like tracing in the code, a developer can quickly see 604 * where problems are occurring. 605 * 606 * This is intended as a debugging tool for the developer only. 607 * Please refrain from leaving trace_puts scattered around in 608 * your code. (Extra memory is used for special buffers that are 609 * allocated when trace_puts() is used.) 610 * 611 * Returns: 0 if nothing was written, positive # if string was. 612 * (1 when __trace_bputs is used, strlen(str) when __trace_puts is used) 613 */ 614 615#define trace_puts(str) ({ \ 616 static const char *trace_printk_fmt __used \ 617 __section("__trace_printk_fmt") = \ 618 __builtin_constant_p(str) ? str : NULL; \ 619 \ 620 if (__builtin_constant_p(str)) \ 621 __trace_bputs(_THIS_IP_, trace_printk_fmt); \ 622 else \ 623 __trace_puts(_THIS_IP_, str, strlen(str)); \ 624}) 625extern int __trace_bputs(unsigned long ip, const char *str); 626extern int __trace_puts(unsigned long ip, const char *str, int size); 627 628extern void trace_dump_stack(int skip); 629 630/* 631 * The double __builtin_constant_p is because gcc will give us an error 632 * if we try to allocate the static variable to fmt if it is not a 633 * constant. Even with the outer if statement. 634 */ 635#define ftrace_vprintk(fmt, vargs) \ 636do { \ 637 if (__builtin_constant_p(fmt)) { \ 638 static const char *trace_printk_fmt __used \ 639 __section("__trace_printk_fmt") = \ 640 __builtin_constant_p(fmt) ? fmt : NULL; \ 641 \ 642 __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \ 643 } else \ 644 __ftrace_vprintk(_THIS_IP_, fmt, vargs); \ 645} while (0) 646 647extern __printf(2, 0) int 648__ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap); 649 650extern __printf(2, 0) int 651__ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap); 652 653extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode); 654#else 655static inline void tracing_start(void) { } 656static inline void tracing_stop(void) { } 657static inline void trace_dump_stack(int skip) { } 658 659static inline void tracing_on(void) { } 660static inline void tracing_off(void) { } 661static inline int tracing_is_on(void) { return 0; } 662static inline void tracing_snapshot(void) { } 663static inline void tracing_snapshot_alloc(void) { } 664 665static inline __printf(1, 2) 666int trace_printk(const char *fmt, ...) 667{ 668 return 0; 669} 670static __printf(1, 0) inline int 671ftrace_vprintk(const char *fmt, va_list ap) 672{ 673 return 0; 674} 675static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { } 676#endif /* CONFIG_TRACING */ 677 678/* This counts to 12. Any more, it will return 13th argument. */ 679#define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n 680#define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) 681 682#define __CONCAT(a, b) a ## b 683#define CONCATENATE(a, b) __CONCAT(a, b) 684 685/** 686 * container_of - cast a member of a structure out to the containing structure 687 * @ptr: the pointer to the member. 688 * @type: the type of the container struct this is embedded in. 689 * @member: the name of the member within the struct. 690 * 691 */ 692#define container_of(ptr, type, member) ({ \ 693 void *__mptr = (void *)(ptr); \ 694 BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) && \ 695 !__same_type(*(ptr), void), \ 696 "pointer type mismatch in container_of()"); \ 697 ((type *)(__mptr - offsetof(type, member))); }) 698 699/** 700 * container_of_safe - cast a member of a structure out to the containing structure 701 * @ptr: the pointer to the member. 702 * @type: the type of the container struct this is embedded in. 703 * @member: the name of the member within the struct. 704 * 705 * If IS_ERR_OR_NULL(ptr), ptr is returned unchanged. 706 */ 707#define container_of_safe(ptr, type, member) ({ \ 708 void *__mptr = (void *)(ptr); \ 709 BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) && \ 710 !__same_type(*(ptr), void), \ 711 "pointer type mismatch in container_of()"); \ 712 IS_ERR_OR_NULL(__mptr) ? ERR_CAST(__mptr) : \ 713 ((type *)(__mptr - offsetof(type, member))); }) 714 715/* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */ 716#ifdef CONFIG_FTRACE_MCOUNT_RECORD 717# define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD 718#endif 719 720/* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */ 721#define VERIFY_OCTAL_PERMISSIONS(perms) \ 722 (BUILD_BUG_ON_ZERO((perms) < 0) + \ 723 BUILD_BUG_ON_ZERO((perms) > 0777) + \ 724 /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */ \ 725 BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) + \ 726 BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) + \ 727 /* USER_WRITABLE >= GROUP_WRITABLE */ \ 728 BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) + \ 729 /* OTHER_WRITABLE? Generally considered a bad idea. */ \ 730 BUILD_BUG_ON_ZERO((perms) & 2) + \ 731 (perms)) 732#endif