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