Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * NOTE:
0004  *
0005  * This header has combined a lot of unrelated to each other stuff.
0006  * The process of splitting its content is in progress while keeping
0007  * backward compatibility. That's why it's highly recommended NOT to
0008  * include this header inside another header file, especially under
0009  * generic or architectural include/ directory.
0010  */
0011 #ifndef _LINUX_KERNEL_H
0012 #define _LINUX_KERNEL_H
0013 
0014 #include <linux/stdarg.h>
0015 #include <linux/align.h>
0016 #include <linux/limits.h>
0017 #include <linux/linkage.h>
0018 #include <linux/stddef.h>
0019 #include <linux/types.h>
0020 #include <linux/compiler.h>
0021 #include <linux/container_of.h>
0022 #include <linux/bitops.h>
0023 #include <linux/kstrtox.h>
0024 #include <linux/log2.h>
0025 #include <linux/math.h>
0026 #include <linux/minmax.h>
0027 #include <linux/typecheck.h>
0028 #include <linux/panic.h>
0029 #include <linux/printk.h>
0030 #include <linux/build_bug.h>
0031 #include <linux/static_call_types.h>
0032 #include <linux/instruction_pointer.h>
0033 #include <asm/byteorder.h>
0034 
0035 #include <uapi/linux/kernel.h>
0036 
0037 #define STACK_MAGIC 0xdeadbeef
0038 
0039 /**
0040  * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value
0041  * @x: value to repeat
0042  *
0043  * NOTE: @x is not checked for > 0xff; larger values produce odd results.
0044  */
0045 #define REPEAT_BYTE(x)  ((~0ul / 0xff) * (x))
0046 
0047 /* generic data direction definitions */
0048 #define READ            0
0049 #define WRITE           1
0050 
0051 /**
0052  * ARRAY_SIZE - get the number of elements in array @arr
0053  * @arr: array to be sized
0054  */
0055 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
0056 
0057 #define PTR_IF(cond, ptr)   ((cond) ? (ptr) : NULL)
0058 
0059 #define u64_to_user_ptr(x) (        \
0060 {                   \
0061     typecheck(u64, (x));        \
0062     (void __user *)(uintptr_t)(x);  \
0063 }                   \
0064 )
0065 
0066 /**
0067  * upper_32_bits - return bits 32-63 of a number
0068  * @n: the number we're accessing
0069  *
0070  * A basic shift-right of a 64- or 32-bit quantity.  Use this to suppress
0071  * the "right shift count >= width of type" warning when that quantity is
0072  * 32-bits.
0073  */
0074 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
0075 
0076 /**
0077  * lower_32_bits - return bits 0-31 of a number
0078  * @n: the number we're accessing
0079  */
0080 #define lower_32_bits(n) ((u32)((n) & 0xffffffff))
0081 
0082 /**
0083  * upper_16_bits - return bits 16-31 of a number
0084  * @n: the number we're accessing
0085  */
0086 #define upper_16_bits(n) ((u16)((n) >> 16))
0087 
0088 /**
0089  * lower_16_bits - return bits 0-15 of a number
0090  * @n: the number we're accessing
0091  */
0092 #define lower_16_bits(n) ((u16)((n) & 0xffff))
0093 
0094 struct completion;
0095 struct user;
0096 
0097 #ifdef CONFIG_PREEMPT_VOLUNTARY_BUILD
0098 
0099 extern int __cond_resched(void);
0100 # define might_resched() __cond_resched()
0101 
0102 #elif defined(CONFIG_PREEMPT_DYNAMIC) && defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL)
0103 
0104 extern int __cond_resched(void);
0105 
0106 DECLARE_STATIC_CALL(might_resched, __cond_resched);
0107 
0108 static __always_inline void might_resched(void)
0109 {
0110     static_call_mod(might_resched)();
0111 }
0112 
0113 #elif defined(CONFIG_PREEMPT_DYNAMIC) && defined(CONFIG_HAVE_PREEMPT_DYNAMIC_KEY)
0114 
0115 extern int dynamic_might_resched(void);
0116 # define might_resched() dynamic_might_resched()
0117 
0118 #else
0119 
0120 # define might_resched() do { } while (0)
0121 
0122 #endif /* CONFIG_PREEMPT_* */
0123 
0124 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
0125 extern void __might_resched(const char *file, int line, unsigned int offsets);
0126 extern void __might_sleep(const char *file, int line);
0127 extern void __cant_sleep(const char *file, int line, int preempt_offset);
0128 extern void __cant_migrate(const char *file, int line);
0129 
0130 /**
0131  * might_sleep - annotation for functions that can sleep
0132  *
0133  * this macro will print a stack trace if it is executed in an atomic
0134  * context (spinlock, irq-handler, ...). Additional sections where blocking is
0135  * not allowed can be annotated with non_block_start() and non_block_end()
0136  * pairs.
0137  *
0138  * This is a useful debugging help to be able to catch problems early and not
0139  * be bitten later when the calling function happens to sleep when it is not
0140  * supposed to.
0141  */
0142 # define might_sleep() \
0143     do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0)
0144 /**
0145  * cant_sleep - annotation for functions that cannot sleep
0146  *
0147  * this macro will print a stack trace if it is executed with preemption enabled
0148  */
0149 # define cant_sleep() \
0150     do { __cant_sleep(__FILE__, __LINE__, 0); } while (0)
0151 # define sched_annotate_sleep() (current->task_state_change = 0)
0152 
0153 /**
0154  * cant_migrate - annotation for functions that cannot migrate
0155  *
0156  * Will print a stack trace if executed in code which is migratable
0157  */
0158 # define cant_migrate()                         \
0159     do {                                \
0160         if (IS_ENABLED(CONFIG_SMP))             \
0161             __cant_migrate(__FILE__, __LINE__);     \
0162     } while (0)
0163 
0164 /**
0165  * non_block_start - annotate the start of section where sleeping is prohibited
0166  *
0167  * This is on behalf of the oom reaper, specifically when it is calling the mmu
0168  * notifiers. The problem is that if the notifier were to block on, for example,
0169  * mutex_lock() and if the process which holds that mutex were to perform a
0170  * sleeping memory allocation, the oom reaper is now blocked on completion of
0171  * that memory allocation. Other blocking calls like wait_event() pose similar
0172  * issues.
0173  */
0174 # define non_block_start() (current->non_block_count++)
0175 /**
0176  * non_block_end - annotate the end of section where sleeping is prohibited
0177  *
0178  * Closes a section opened by non_block_start().
0179  */
0180 # define non_block_end() WARN_ON(current->non_block_count-- == 0)
0181 #else
0182   static inline void __might_resched(const char *file, int line,
0183                      unsigned int offsets) { }
0184 static inline void __might_sleep(const char *file, int line) { }
0185 # define might_sleep() do { might_resched(); } while (0)
0186 # define cant_sleep() do { } while (0)
0187 # define cant_migrate()     do { } while (0)
0188 # define sched_annotate_sleep() do { } while (0)
0189 # define non_block_start() do { } while (0)
0190 # define non_block_end() do { } while (0)
0191 #endif
0192 
0193 #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
0194 
0195 #if defined(CONFIG_MMU) && \
0196     (defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP))
0197 #define might_fault() __might_fault(__FILE__, __LINE__)
0198 void __might_fault(const char *file, int line);
0199 #else
0200 static inline void might_fault(void) { }
0201 #endif
0202 
0203 void do_exit(long error_code) __noreturn;
0204 
0205 extern int num_to_str(char *buf, int size,
0206               unsigned long long num, unsigned int width);
0207 
0208 /* lib/printf utilities */
0209 
0210 extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...);
0211 extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list);
0212 extern __printf(3, 4)
0213 int snprintf(char *buf, size_t size, const char *fmt, ...);
0214 extern __printf(3, 0)
0215 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
0216 extern __printf(3, 4)
0217 int scnprintf(char *buf, size_t size, const char *fmt, ...);
0218 extern __printf(3, 0)
0219 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
0220 extern __printf(2, 3) __malloc
0221 char *kasprintf(gfp_t gfp, const char *fmt, ...);
0222 extern __printf(2, 0) __malloc
0223 char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
0224 extern __printf(2, 0)
0225 const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args);
0226 
0227 extern __scanf(2, 3)
0228 int sscanf(const char *, const char *, ...);
0229 extern __scanf(2, 0)
0230 int vsscanf(const char *, const char *, va_list);
0231 
0232 extern int no_hash_pointers_enable(char *str);
0233 
0234 extern int get_option(char **str, int *pint);
0235 extern char *get_options(const char *str, int nints, int *ints);
0236 extern unsigned long long memparse(const char *ptr, char **retptr);
0237 extern bool parse_option_str(const char *str, const char *option);
0238 extern char *next_arg(char *args, char **param, char **val);
0239 
0240 extern int core_kernel_text(unsigned long addr);
0241 extern int __kernel_text_address(unsigned long addr);
0242 extern int kernel_text_address(unsigned long addr);
0243 extern int func_ptr_is_kernel_text(void *ptr);
0244 
0245 extern void bust_spinlocks(int yes);
0246 
0247 extern int root_mountflags;
0248 
0249 extern bool early_boot_irqs_disabled;
0250 
0251 /*
0252  * Values used for system_state. Ordering of the states must not be changed
0253  * as code checks for <, <=, >, >= STATE.
0254  */
0255 extern enum system_states {
0256     SYSTEM_BOOTING,
0257     SYSTEM_SCHEDULING,
0258     SYSTEM_FREEING_INITMEM,
0259     SYSTEM_RUNNING,
0260     SYSTEM_HALT,
0261     SYSTEM_POWER_OFF,
0262     SYSTEM_RESTART,
0263     SYSTEM_SUSPEND,
0264 } system_state;
0265 
0266 extern const char hex_asc[];
0267 #define hex_asc_lo(x)   hex_asc[((x) & 0x0f)]
0268 #define hex_asc_hi(x)   hex_asc[((x) & 0xf0) >> 4]
0269 
0270 static inline char *hex_byte_pack(char *buf, u8 byte)
0271 {
0272     *buf++ = hex_asc_hi(byte);
0273     *buf++ = hex_asc_lo(byte);
0274     return buf;
0275 }
0276 
0277 extern const char hex_asc_upper[];
0278 #define hex_asc_upper_lo(x) hex_asc_upper[((x) & 0x0f)]
0279 #define hex_asc_upper_hi(x) hex_asc_upper[((x) & 0xf0) >> 4]
0280 
0281 static inline char *hex_byte_pack_upper(char *buf, u8 byte)
0282 {
0283     *buf++ = hex_asc_upper_hi(byte);
0284     *buf++ = hex_asc_upper_lo(byte);
0285     return buf;
0286 }
0287 
0288 extern int hex_to_bin(unsigned char ch);
0289 extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);
0290 extern char *bin2hex(char *dst, const void *src, size_t count);
0291 
0292 bool mac_pton(const char *s, u8 *mac);
0293 
0294 /*
0295  * General tracing related utility functions - trace_printk(),
0296  * tracing_on/tracing_off and tracing_start()/tracing_stop
0297  *
0298  * Use tracing_on/tracing_off when you want to quickly turn on or off
0299  * tracing. It simply enables or disables the recording of the trace events.
0300  * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on
0301  * file, which gives a means for the kernel and userspace to interact.
0302  * Place a tracing_off() in the kernel where you want tracing to end.
0303  * From user space, examine the trace, and then echo 1 > tracing_on
0304  * to continue tracing.
0305  *
0306  * tracing_stop/tracing_start has slightly more overhead. It is used
0307  * by things like suspend to ram where disabling the recording of the
0308  * trace is not enough, but tracing must actually stop because things
0309  * like calling smp_processor_id() may crash the system.
0310  *
0311  * Most likely, you want to use tracing_on/tracing_off.
0312  */
0313 
0314 enum ftrace_dump_mode {
0315     DUMP_NONE,
0316     DUMP_ALL,
0317     DUMP_ORIG,
0318 };
0319 
0320 #ifdef CONFIG_TRACING
0321 void tracing_on(void);
0322 void tracing_off(void);
0323 int tracing_is_on(void);
0324 void tracing_snapshot(void);
0325 void tracing_snapshot_alloc(void);
0326 
0327 extern void tracing_start(void);
0328 extern void tracing_stop(void);
0329 
0330 static inline __printf(1, 2)
0331 void ____trace_printk_check_format(const char *fmt, ...)
0332 {
0333 }
0334 #define __trace_printk_check_format(fmt, args...)           \
0335 do {                                    \
0336     if (0)                              \
0337         ____trace_printk_check_format(fmt, ##args);     \
0338 } while (0)
0339 
0340 /**
0341  * trace_printk - printf formatting in the ftrace buffer
0342  * @fmt: the printf format for printing
0343  *
0344  * Note: __trace_printk is an internal function for trace_printk() and
0345  *       the @ip is passed in via the trace_printk() macro.
0346  *
0347  * This function allows a kernel developer to debug fast path sections
0348  * that printk is not appropriate for. By scattering in various
0349  * printk like tracing in the code, a developer can quickly see
0350  * where problems are occurring.
0351  *
0352  * This is intended as a debugging tool for the developer only.
0353  * Please refrain from leaving trace_printks scattered around in
0354  * your code. (Extra memory is used for special buffers that are
0355  * allocated when trace_printk() is used.)
0356  *
0357  * A little optimization trick is done here. If there's only one
0358  * argument, there's no need to scan the string for printf formats.
0359  * The trace_puts() will suffice. But how can we take advantage of
0360  * using trace_puts() when trace_printk() has only one argument?
0361  * By stringifying the args and checking the size we can tell
0362  * whether or not there are args. __stringify((__VA_ARGS__)) will
0363  * turn into "()\0" with a size of 3 when there are no args, anything
0364  * else will be bigger. All we need to do is define a string to this,
0365  * and then take its size and compare to 3. If it's bigger, use
0366  * do_trace_printk() otherwise, optimize it to trace_puts(). Then just
0367  * let gcc optimize the rest.
0368  */
0369 
0370 #define trace_printk(fmt, ...)              \
0371 do {                            \
0372     char _______STR[] = __stringify((__VA_ARGS__)); \
0373     if (sizeof(_______STR) > 3)         \
0374         do_trace_printk(fmt, ##__VA_ARGS__);    \
0375     else                        \
0376         trace_puts(fmt);            \
0377 } while (0)
0378 
0379 #define do_trace_printk(fmt, args...)                   \
0380 do {                                    \
0381     static const char *trace_printk_fmt __used          \
0382         __section("__trace_printk_fmt") =           \
0383         __builtin_constant_p(fmt) ? fmt : NULL;         \
0384                                     \
0385     __trace_printk_check_format(fmt, ##args);           \
0386                                     \
0387     if (__builtin_constant_p(fmt))                  \
0388         __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args);   \
0389     else                                \
0390         __trace_printk(_THIS_IP_, fmt, ##args);         \
0391 } while (0)
0392 
0393 extern __printf(2, 3)
0394 int __trace_bprintk(unsigned long ip, const char *fmt, ...);
0395 
0396 extern __printf(2, 3)
0397 int __trace_printk(unsigned long ip, const char *fmt, ...);
0398 
0399 /**
0400  * trace_puts - write a string into the ftrace buffer
0401  * @str: the string to record
0402  *
0403  * Note: __trace_bputs is an internal function for trace_puts and
0404  *       the @ip is passed in via the trace_puts macro.
0405  *
0406  * This is similar to trace_printk() but is made for those really fast
0407  * paths that a developer wants the least amount of "Heisenbug" effects,
0408  * where the processing of the print format is still too much.
0409  *
0410  * This function allows a kernel developer to debug fast path sections
0411  * that printk is not appropriate for. By scattering in various
0412  * printk like tracing in the code, a developer can quickly see
0413  * where problems are occurring.
0414  *
0415  * This is intended as a debugging tool for the developer only.
0416  * Please refrain from leaving trace_puts scattered around in
0417  * your code. (Extra memory is used for special buffers that are
0418  * allocated when trace_puts() is used.)
0419  *
0420  * Returns: 0 if nothing was written, positive # if string was.
0421  *  (1 when __trace_bputs is used, strlen(str) when __trace_puts is used)
0422  */
0423 
0424 #define trace_puts(str) ({                      \
0425     static const char *trace_printk_fmt __used          \
0426         __section("__trace_printk_fmt") =           \
0427         __builtin_constant_p(str) ? str : NULL;         \
0428                                     \
0429     if (__builtin_constant_p(str))                  \
0430         __trace_bputs(_THIS_IP_, trace_printk_fmt);     \
0431     else                                \
0432         __trace_puts(_THIS_IP_, str, strlen(str));      \
0433 })
0434 extern int __trace_bputs(unsigned long ip, const char *str);
0435 extern int __trace_puts(unsigned long ip, const char *str, int size);
0436 
0437 extern void trace_dump_stack(int skip);
0438 
0439 /*
0440  * The double __builtin_constant_p is because gcc will give us an error
0441  * if we try to allocate the static variable to fmt if it is not a
0442  * constant. Even with the outer if statement.
0443  */
0444 #define ftrace_vprintk(fmt, vargs)                  \
0445 do {                                    \
0446     if (__builtin_constant_p(fmt)) {                \
0447         static const char *trace_printk_fmt __used      \
0448           __section("__trace_printk_fmt") =         \
0449             __builtin_constant_p(fmt) ? fmt : NULL;     \
0450                                     \
0451         __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs);  \
0452     } else                              \
0453         __ftrace_vprintk(_THIS_IP_, fmt, vargs);        \
0454 } while (0)
0455 
0456 extern __printf(2, 0) int
0457 __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
0458 
0459 extern __printf(2, 0) int
0460 __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
0461 
0462 extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
0463 #else
0464 static inline void tracing_start(void) { }
0465 static inline void tracing_stop(void) { }
0466 static inline void trace_dump_stack(int skip) { }
0467 
0468 static inline void tracing_on(void) { }
0469 static inline void tracing_off(void) { }
0470 static inline int tracing_is_on(void) { return 0; }
0471 static inline void tracing_snapshot(void) { }
0472 static inline void tracing_snapshot_alloc(void) { }
0473 
0474 static inline __printf(1, 2)
0475 int trace_printk(const char *fmt, ...)
0476 {
0477     return 0;
0478 }
0479 static __printf(1, 0) inline int
0480 ftrace_vprintk(const char *fmt, va_list ap)
0481 {
0482     return 0;
0483 }
0484 static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
0485 #endif /* CONFIG_TRACING */
0486 
0487 /* This counts to 12. Any more, it will return 13th argument. */
0488 #define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n
0489 #define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
0490 
0491 #define __CONCAT(a, b) a ## b
0492 #define CONCATENATE(a, b) __CONCAT(a, b)
0493 
0494 /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
0495 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
0496 # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
0497 #endif
0498 
0499 /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */
0500 #define VERIFY_OCTAL_PERMISSIONS(perms)                     \
0501     (BUILD_BUG_ON_ZERO((perms) < 0) +                   \
0502      BUILD_BUG_ON_ZERO((perms) > 0777) +                    \
0503      /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */        \
0504      BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) +   \
0505      BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) +      \
0506      /* USER_WRITABLE >= GROUP_WRITABLE */                  \
0507      BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) +   \
0508      /* OTHER_WRITABLE?  Generally considered a bad idea. */        \
0509      BUILD_BUG_ON_ZERO((perms) & 2) +                   \
0510      (perms))
0511 #endif