kernel.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * NOTE:
  4. *
  5. * This header has combined a lot of unrelated to each other stuff.
  6. * The process of splitting its content is in progress while keeping
  7. * backward compatibility. That's why it's highly recommended NOT to
  8. * include this header inside another header file, especially under
  9. * generic or architectural include/ directory.
  10. */
  11. #ifndef _LINUX_KERNEL_H
  12. #define _LINUX_KERNEL_H
  13. #include <linux/stdarg.h>
  14. #include <linux/align.h>
  15. #include <linux/limits.h>
  16. #include <linux/linkage.h>
  17. #include <linux/stddef.h>
  18. #include <linux/types.h>
  19. #include <linux/compiler.h>
  20. #include <linux/container_of.h>
  21. #include <linux/bitops.h>
  22. #include <linux/kstrtox.h>
  23. #include <linux/log2.h>
  24. #include <linux/math.h>
  25. #include <linux/minmax.h>
  26. #include <linux/typecheck.h>
  27. #include <linux/panic.h>
  28. #include <linux/printk.h>
  29. #include <linux/build_bug.h>
  30. #include <linux/static_call_types.h>
  31. #include <linux/instruction_pointer.h>
  32. #include <asm/byteorder.h>
  33. #include <uapi/linux/kernel.h>
  34. #define STACK_MAGIC 0xdeadbeef
  35. /**
  36. * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value
  37. * @x: value to repeat
  38. *
  39. * NOTE: @x is not checked for > 0xff; larger values produce odd results.
  40. */
  41. #define REPEAT_BYTE(x) ((~0ul / 0xff) * (x))
  42. /* generic data direction definitions */
  43. #define READ 0
  44. #define WRITE 1
  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. #define PTR_IF(cond, ptr) ((cond) ? (ptr) : NULL)
  51. #define u64_to_user_ptr(x) ( \
  52. { \
  53. typecheck(u64, (x)); \
  54. (void __user *)(uintptr_t)(x); \
  55. } \
  56. )
  57. /**
  58. * upper_32_bits - return bits 32-63 of a number
  59. * @n: the number we're accessing
  60. *
  61. * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
  62. * the "right shift count >= width of type" warning when that quantity is
  63. * 32-bits.
  64. */
  65. #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
  66. /**
  67. * lower_32_bits - return bits 0-31 of a number
  68. * @n: the number we're accessing
  69. */
  70. #define lower_32_bits(n) ((u32)((n) & 0xffffffff))
  71. /**
  72. * upper_16_bits - return bits 16-31 of a number
  73. * @n: the number we're accessing
  74. */
  75. #define upper_16_bits(n) ((u16)((n) >> 16))
  76. /**
  77. * lower_16_bits - return bits 0-15 of a number
  78. * @n: the number we're accessing
  79. */
  80. #define lower_16_bits(n) ((u16)((n) & 0xffff))
  81. struct completion;
  82. struct user;
  83. #ifdef CONFIG_PREEMPT_VOLUNTARY_BUILD
  84. extern int __cond_resched(void);
  85. # define might_resched() __cond_resched()
  86. #elif defined(CONFIG_PREEMPT_DYNAMIC) && defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL)
  87. extern int __cond_resched(void);
  88. DECLARE_STATIC_CALL(might_resched, __cond_resched);
  89. static __always_inline void might_resched(void)
  90. {
  91. static_call_mod(might_resched)();
  92. }
  93. #elif defined(CONFIG_PREEMPT_DYNAMIC) && defined(CONFIG_HAVE_PREEMPT_DYNAMIC_KEY)
  94. extern int dynamic_might_resched(void);
  95. # define might_resched() dynamic_might_resched()
  96. #else
  97. # define might_resched() do { } while (0)
  98. #endif /* CONFIG_PREEMPT_* */
  99. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  100. extern void __might_resched(const char *file, int line, unsigned int offsets);
  101. extern void __might_sleep(const char *file, int line);
  102. extern void __cant_sleep(const char *file, int line, int preempt_offset);
  103. extern void __cant_migrate(const char *file, int line);
  104. /**
  105. * might_sleep - annotation for functions that can sleep
  106. *
  107. * this macro will print a stack trace if it is executed in an atomic
  108. * context (spinlock, irq-handler, ...). Additional sections where blocking is
  109. * not allowed can be annotated with non_block_start() and non_block_end()
  110. * pairs.
  111. *
  112. * This is a useful debugging help to be able to catch problems early and not
  113. * be bitten later when the calling function happens to sleep when it is not
  114. * supposed to.
  115. */
  116. # define might_sleep() \
  117. do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0)
  118. /**
  119. * cant_sleep - annotation for functions that cannot sleep
  120. *
  121. * this macro will print a stack trace if it is executed with preemption enabled
  122. */
  123. # define cant_sleep() \
  124. do { __cant_sleep(__FILE__, __LINE__, 0); } while (0)
  125. # define sched_annotate_sleep() (current->task_state_change = 0)
  126. /**
  127. * cant_migrate - annotation for functions that cannot migrate
  128. *
  129. * Will print a stack trace if executed in code which is migratable
  130. */
  131. # define cant_migrate() \
  132. do { \
  133. if (IS_ENABLED(CONFIG_SMP)) \
  134. __cant_migrate(__FILE__, __LINE__); \
  135. } while (0)
  136. /**
  137. * non_block_start - annotate the start of section where sleeping is prohibited
  138. *
  139. * This is on behalf of the oom reaper, specifically when it is calling the mmu
  140. * notifiers. The problem is that if the notifier were to block on, for example,
  141. * mutex_lock() and if the process which holds that mutex were to perform a
  142. * sleeping memory allocation, the oom reaper is now blocked on completion of
  143. * that memory allocation. Other blocking calls like wait_event() pose similar
  144. * issues.
  145. */
  146. # define non_block_start() (current->non_block_count++)
  147. /**
  148. * non_block_end - annotate the end of section where sleeping is prohibited
  149. *
  150. * Closes a section opened by non_block_start().
  151. */
  152. # define non_block_end() WARN_ON(current->non_block_count-- == 0)
  153. #else
  154. static inline void __might_resched(const char *file, int line,
  155. unsigned int offsets) { }
  156. static inline void __might_sleep(const char *file, int line) { }
  157. # define might_sleep() do { might_resched(); } while (0)
  158. # define cant_sleep() do { } while (0)
  159. # define cant_migrate() do { } while (0)
  160. # define sched_annotate_sleep() do { } while (0)
  161. # define non_block_start() do { } while (0)
  162. # define non_block_end() do { } while (0)
  163. #endif
  164. #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
  165. #if defined(CONFIG_MMU) && \
  166. (defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP))
  167. #define might_fault() __might_fault(__FILE__, __LINE__)
  168. void __might_fault(const char *file, int line);
  169. #else
  170. static inline void might_fault(void) { }
  171. #endif
  172. void do_exit(long error_code) __noreturn;
  173. extern int num_to_str(char *buf, int size,
  174. unsigned long long num, unsigned int width);
  175. /* lib/printf utilities */
  176. extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...);
  177. extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list);
  178. extern __printf(3, 4)
  179. int snprintf(char *buf, size_t size, const char *fmt, ...);
  180. extern __printf(3, 0)
  181. int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
  182. extern __printf(3, 4)
  183. int scnprintf(char *buf, size_t size, const char *fmt, ...);
  184. extern __printf(3, 0)
  185. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
  186. extern __printf(2, 3) __malloc
  187. char *kasprintf(gfp_t gfp, const char *fmt, ...);
  188. extern __printf(2, 0) __malloc
  189. char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
  190. extern __printf(2, 0)
  191. const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args);
  192. extern __scanf(2, 3)
  193. int sscanf(const char *, const char *, ...);
  194. extern __scanf(2, 0)
  195. int vsscanf(const char *, const char *, va_list);
  196. extern int no_hash_pointers_enable(char *str);
  197. extern int get_option(char **str, int *pint);
  198. extern char *get_options(const char *str, int nints, int *ints);
  199. extern unsigned long long memparse(const char *ptr, char **retptr);
  200. extern bool parse_option_str(const char *str, const char *option);
  201. extern char *next_arg(char *args, char **param, char **val);
  202. extern int core_kernel_text(unsigned long addr);
  203. extern int __kernel_text_address(unsigned long addr);
  204. extern int kernel_text_address(unsigned long addr);
  205. extern int func_ptr_is_kernel_text(void *ptr);
  206. extern void bust_spinlocks(int yes);
  207. extern int root_mountflags;
  208. extern bool early_boot_irqs_disabled;
  209. /*
  210. * Values used for system_state. Ordering of the states must not be changed
  211. * as code checks for <, <=, >, >= STATE.
  212. */
  213. extern enum system_states {
  214. SYSTEM_BOOTING,
  215. SYSTEM_SCHEDULING,
  216. SYSTEM_FREEING_INITMEM,
  217. SYSTEM_RUNNING,
  218. SYSTEM_HALT,
  219. SYSTEM_POWER_OFF,
  220. SYSTEM_RESTART,
  221. SYSTEM_SUSPEND,
  222. } system_state;
  223. extern const char hex_asc[];
  224. #define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
  225. #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
  226. static inline char *hex_byte_pack(char *buf, u8 byte)
  227. {
  228. *buf++ = hex_asc_hi(byte);
  229. *buf++ = hex_asc_lo(byte);
  230. return buf;
  231. }
  232. extern const char hex_asc_upper[];
  233. #define hex_asc_upper_lo(x) hex_asc_upper[((x) & 0x0f)]
  234. #define hex_asc_upper_hi(x) hex_asc_upper[((x) & 0xf0) >> 4]
  235. static inline char *hex_byte_pack_upper(char *buf, u8 byte)
  236. {
  237. *buf++ = hex_asc_upper_hi(byte);
  238. *buf++ = hex_asc_upper_lo(byte);
  239. return buf;
  240. }
  241. extern int hex_to_bin(unsigned char ch);
  242. extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);
  243. extern char *bin2hex(char *dst, const void *src, size_t count);
  244. bool mac_pton(const char *s, u8 *mac);
  245. /*
  246. * General tracing related utility functions - trace_printk(),
  247. * tracing_on/tracing_off and tracing_start()/tracing_stop
  248. *
  249. * Use tracing_on/tracing_off when you want to quickly turn on or off
  250. * tracing. It simply enables or disables the recording of the trace events.
  251. * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on
  252. * file, which gives a means for the kernel and userspace to interact.
  253. * Place a tracing_off() in the kernel where you want tracing to end.
  254. * From user space, examine the trace, and then echo 1 > tracing_on
  255. * to continue tracing.
  256. *
  257. * tracing_stop/tracing_start has slightly more overhead. It is used
  258. * by things like suspend to ram where disabling the recording of the
  259. * trace is not enough, but tracing must actually stop because things
  260. * like calling smp_processor_id() may crash the system.
  261. *
  262. * Most likely, you want to use tracing_on/tracing_off.
  263. */
  264. enum ftrace_dump_mode {
  265. DUMP_NONE,
  266. DUMP_ALL,
  267. DUMP_ORIG,
  268. };
  269. #ifdef CONFIG_TRACING
  270. void tracing_on(void);
  271. void tracing_off(void);
  272. int tracing_is_on(void);
  273. void tracing_snapshot(void);
  274. void tracing_snapshot_alloc(void);
  275. extern void tracing_start(void);
  276. extern void tracing_stop(void);
  277. static inline __printf(1, 2)
  278. void ____trace_printk_check_format(const char *fmt, ...)
  279. {
  280. }
  281. #define __trace_printk_check_format(fmt, args...) \
  282. do { \
  283. if (0) \
  284. ____trace_printk_check_format(fmt, ##args); \
  285. } while (0)
  286. /**
  287. * trace_printk - printf formatting in the ftrace buffer
  288. * @fmt: the printf format for printing
  289. *
  290. * Note: __trace_printk is an internal function for trace_printk() and
  291. * the @ip is passed in via the trace_printk() macro.
  292. *
  293. * This function allows a kernel developer to debug fast path sections
  294. * that printk is not appropriate for. By scattering in various
  295. * printk like tracing in the code, a developer can quickly see
  296. * where problems are occurring.
  297. *
  298. * This is intended as a debugging tool for the developer only.
  299. * Please refrain from leaving trace_printks scattered around in
  300. * your code. (Extra memory is used for special buffers that are
  301. * allocated when trace_printk() is used.)
  302. *
  303. * A little optimization trick is done here. If there's only one
  304. * argument, there's no need to scan the string for printf formats.
  305. * The trace_puts() will suffice. But how can we take advantage of
  306. * using trace_puts() when trace_printk() has only one argument?
  307. * By stringifying the args and checking the size we can tell
  308. * whether or not there are args. __stringify((__VA_ARGS__)) will
  309. * turn into "()\0" with a size of 3 when there are no args, anything
  310. * else will be bigger. All we need to do is define a string to this,
  311. * and then take its size and compare to 3. If it's bigger, use
  312. * do_trace_printk() otherwise, optimize it to trace_puts(). Then just
  313. * let gcc optimize the rest.
  314. */
  315. #define trace_printk(fmt, ...) \
  316. do { \
  317. char _______STR[] = __stringify((__VA_ARGS__)); \
  318. if (sizeof(_______STR) > 3) \
  319. do_trace_printk(fmt, ##__VA_ARGS__); \
  320. else \
  321. trace_puts(fmt); \
  322. } while (0)
  323. #define do_trace_printk(fmt, args...) \
  324. do { \
  325. static const char *trace_printk_fmt __used \
  326. __section("__trace_printk_fmt") = \
  327. __builtin_constant_p(fmt) ? fmt : NULL; \
  328. \
  329. __trace_printk_check_format(fmt, ##args); \
  330. \
  331. if (__builtin_constant_p(fmt)) \
  332. __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \
  333. else \
  334. __trace_printk(_THIS_IP_, fmt, ##args); \
  335. } while (0)
  336. extern __printf(2, 3)
  337. int __trace_bprintk(unsigned long ip, const char *fmt, ...);
  338. extern __printf(2, 3)
  339. int __trace_printk(unsigned long ip, const char *fmt, ...);
  340. /**
  341. * trace_puts - write a string into the ftrace buffer
  342. * @str: the string to record
  343. *
  344. * Note: __trace_bputs is an internal function for trace_puts and
  345. * the @ip is passed in via the trace_puts macro.
  346. *
  347. * This is similar to trace_printk() but is made for those really fast
  348. * paths that a developer wants the least amount of "Heisenbug" effects,
  349. * where the processing of the print format is still too much.
  350. *
  351. * This function allows a kernel developer to debug fast path sections
  352. * that printk is not appropriate for. By scattering in various
  353. * printk like tracing in the code, a developer can quickly see
  354. * where problems are occurring.
  355. *
  356. * This is intended as a debugging tool for the developer only.
  357. * Please refrain from leaving trace_puts scattered around in
  358. * your code. (Extra memory is used for special buffers that are
  359. * allocated when trace_puts() is used.)
  360. *
  361. * Returns: 0 if nothing was written, positive # if string was.
  362. * (1 when __trace_bputs is used, strlen(str) when __trace_puts is used)
  363. */
  364. #define trace_puts(str) ({ \
  365. static const char *trace_printk_fmt __used \
  366. __section("__trace_printk_fmt") = \
  367. __builtin_constant_p(str) ? str : NULL; \
  368. \
  369. if (__builtin_constant_p(str)) \
  370. __trace_bputs(_THIS_IP_, trace_printk_fmt); \
  371. else \
  372. __trace_puts(_THIS_IP_, str, strlen(str)); \
  373. })
  374. extern int __trace_bputs(unsigned long ip, const char *str);
  375. extern int __trace_puts(unsigned long ip, const char *str, int size);
  376. extern void trace_dump_stack(int skip);
  377. /*
  378. * The double __builtin_constant_p is because gcc will give us an error
  379. * if we try to allocate the static variable to fmt if it is not a
  380. * constant. Even with the outer if statement.
  381. */
  382. #define ftrace_vprintk(fmt, vargs) \
  383. do { \
  384. if (__builtin_constant_p(fmt)) { \
  385. static const char *trace_printk_fmt __used \
  386. __section("__trace_printk_fmt") = \
  387. __builtin_constant_p(fmt) ? fmt : NULL; \
  388. \
  389. __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \
  390. } else \
  391. __ftrace_vprintk(_THIS_IP_, fmt, vargs); \
  392. } while (0)
  393. extern __printf(2, 0) int
  394. __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
  395. extern __printf(2, 0) int
  396. __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
  397. extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
  398. #else
  399. static inline void tracing_start(void) { }
  400. static inline void tracing_stop(void) { }
  401. static inline void trace_dump_stack(int skip) { }
  402. static inline void tracing_on(void) { }
  403. static inline void tracing_off(void) { }
  404. static inline int tracing_is_on(void) { return 0; }
  405. static inline void tracing_snapshot(void) { }
  406. static inline void tracing_snapshot_alloc(void) { }
  407. static inline __printf(1, 2)
  408. int trace_printk(const char *fmt, ...)
  409. {
  410. return 0;
  411. }
  412. static __printf(1, 0) inline int
  413. ftrace_vprintk(const char *fmt, va_list ap)
  414. {
  415. return 0;
  416. }
  417. static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
  418. #endif /* CONFIG_TRACING */
  419. /* This counts to 12. Any more, it will return 13th argument. */
  420. #define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n
  421. #define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
  422. #define __CONCAT(a, b) a ## b
  423. #define CONCATENATE(a, b) __CONCAT(a, b)
  424. /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
  425. #ifdef CONFIG_FTRACE_MCOUNT_RECORD
  426. # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
  427. #endif
  428. /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */
  429. #define VERIFY_OCTAL_PERMISSIONS(perms) \
  430. (BUILD_BUG_ON_ZERO((perms) < 0) + \
  431. BUILD_BUG_ON_ZERO((perms) > 0777) + \
  432. /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */ \
  433. BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) + \
  434. BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) + \
  435. /* USER_WRITABLE >= GROUP_WRITABLE */ \
  436. BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) + \
  437. /* OTHER_WRITABLE? Generally considered a bad idea. */ \
  438. BUILD_BUG_ON_ZERO((perms) & 2) + \
  439. (perms))
  440. #endif