kernel.h 16.2 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0 */
2 3 4 5 6 7 8 9 10
/*
 * NOTE:
 *
 * This header has combined a lot of unrelated to each other stuff.
 * The process of splitting its content is in progress while keeping
 * backward compatibility. That's why it's highly recommended NOT to
 * include this header inside another header file, especially under
 * generic or architectural include/ directory.
 */
L
Linus Torvalds 已提交
11 12 13
#ifndef _LINUX_KERNEL_H
#define _LINUX_KERNEL_H

A
Alexey Dobriyan 已提交
14
#include <linux/stdarg.h>
15
#include <linux/align.h>
16
#include <linux/limits.h>
L
Linus Torvalds 已提交
17 18 19 20
#include <linux/linkage.h>
#include <linux/stddef.h>
#include <linux/types.h>
#include <linux/compiler.h>
21
#include <linux/container_of.h>
L
Linus Torvalds 已提交
22
#include <linux/bitops.h>
23
#include <linux/kstrtox.h>
24
#include <linux/log2.h>
25
#include <linux/math.h>
26
#include <linux/minmax.h>
27
#include <linux/typecheck.h>
28
#include <linux/panic.h>
29
#include <linux/printk.h>
30
#include <linux/build_bug.h>
31
#include <linux/static_call_types.h>
32
#include <linux/instruction_pointer.h>
L
Linus Torvalds 已提交
33
#include <asm/byteorder.h>
34

35
#include <uapi/linux/kernel.h>
L
Linus Torvalds 已提交
36 37 38

#define STACK_MAGIC	0xdeadbeef

39 40 41 42 43 44
/**
 * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value
 * @x: value to repeat
 *
 * NOTE: @x is not checked for > 0xff; larger values produce odd results.
 */
45 46
#define REPEAT_BYTE(x)	((~0ul / 0xff) * (x))

47 48 49 50
/* generic data direction definitions */
#define READ			0
#define WRITE			1

51 52 53 54
/**
 * ARRAY_SIZE - get the number of elements in array @arr
 * @arr: array to be sized
 */
R
Rusty Russell 已提交
55 56
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))

57 58
#define PTR_IF(cond, ptr)	((cond) ? (ptr) : NULL)

59 60
#define u64_to_user_ptr(x) (		\
{					\
61 62
	typecheck(u64, (x));		\
	(void __user *)(uintptr_t)(x);	\
63 64 65
}					\
)

66 67 68 69 70 71 72 73 74
/**
 * lower_48_bits() - return bits 0-47 of a number
 * @n: the number we're accessing
 */
static inline u64 lower_48_bits(u64 n)
{
	return n & ((1ull << 48) - 1);
}

A
Andrew Morton 已提交
75 76 77 78 79 80 81 82 83 84
/**
 * upper_32_bits - return bits 32-63 of a number
 * @n: the number we're accessing
 *
 * A basic shift-right of a 64- or 32-bit quantity.  Use this to suppress
 * the "right shift count >= width of type" warning when that quantity is
 * 32-bits.
 */
#define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))

J
Joerg Roedel 已提交
85 86 87 88
/**
 * lower_32_bits - return bits 0-31 of a number
 * @n: the number we're accessing
 */
89
#define lower_32_bits(n) ((u32)((n) & 0xffffffff))
J
Joerg Roedel 已提交
90

91 92 93 94 95 96 97 98 99 100 101 102
/**
 * upper_16_bits - return bits 16-31 of a number
 * @n: the number we're accessing
 */
#define upper_16_bits(n) ((u16)((n) >> 16))

/**
 * lower_16_bits - return bits 0-15 of a number
 * @n: the number we're accessing
 */
#define lower_16_bits(n) ((u16)((n) & 0xffff))

L
Linus Torvalds 已提交
103
struct completion;
A
akpm@osdl.org 已提交
104
struct user;
L
Linus Torvalds 已提交
105

106
#ifdef CONFIG_PREEMPT_VOLUNTARY_BUILD
107 108 109 110 111 112 113 114 115 116 117 118

extern int __cond_resched(void);
# define might_resched() __cond_resched()

#elif defined(CONFIG_PREEMPT_DYNAMIC)

extern int __cond_resched(void);

DECLARE_STATIC_CALL(might_resched, __cond_resched);

static __always_inline void might_resched(void)
{
P
Peter Zijlstra 已提交
119
	static_call_mod(might_resched)();
120 121
}

122
#else
123

124
# define might_resched() do { } while (0)
125 126

#endif /* CONFIG_PREEMPT_* */
127

128
#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
129
extern void __might_resched(const char *file, int line, unsigned int offsets);
130
extern void __might_sleep(const char *file, int line);
131
extern void __cant_sleep(const char *file, int line, int preempt_offset);
132
extern void __cant_migrate(const char *file, int line);
133

L
Linus Torvalds 已提交
134 135 136 137
/**
 * might_sleep - annotation for functions that can sleep
 *
 * this macro will print a stack trace if it is executed in an atomic
138 139 140
 * context (spinlock, irq-handler, ...). Additional sections where blocking is
 * not allowed can be annotated with non_block_start() and non_block_end()
 * pairs.
L
Linus Torvalds 已提交
141 142
 *
 * This is a useful debugging help to be able to catch problems early and not
143
 * be bitten later when the calling function happens to sleep when it is not
L
Linus Torvalds 已提交
144 145
 * supposed to.
 */
146
# define might_sleep() \
147
	do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0)
148 149 150 151 152 153 154
/**
 * cant_sleep - annotation for functions that cannot sleep
 *
 * this macro will print a stack trace if it is executed with preemption enabled
 */
# define cant_sleep() \
	do { __cant_sleep(__FILE__, __LINE__, 0); } while (0)
155
# define sched_annotate_sleep()	(current->task_state_change = 0)
156 157 158 159 160 161 162 163 164 165 166 167

/**
 * cant_migrate - annotation for functions that cannot migrate
 *
 * Will print a stack trace if executed in code which is migratable
 */
# define cant_migrate()							\
	do {								\
		if (IS_ENABLED(CONFIG_SMP))				\
			__cant_migrate(__FILE__, __LINE__);		\
	} while (0)

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
/**
 * non_block_start - annotate the start of section where sleeping is prohibited
 *
 * This is on behalf of the oom reaper, specifically when it is calling the mmu
 * notifiers. The problem is that if the notifier were to block on, for example,
 * mutex_lock() and if the process which holds that mutex were to perform a
 * sleeping memory allocation, the oom reaper is now blocked on completion of
 * that memory allocation. Other blocking calls like wait_event() pose similar
 * issues.
 */
# define non_block_start() (current->non_block_count++)
/**
 * non_block_end - annotate the end of section where sleeping is prohibited
 *
 * Closes a section opened by non_block_start().
 */
# define non_block_end() WARN_ON(current->non_block_count-- == 0)
L
Linus Torvalds 已提交
185
#else
186
  static inline void __might_resched(const char *file, int line,
187
				     unsigned int offsets) { }
188
static inline void __might_sleep(const char *file, int line) { }
189
# define might_sleep() do { might_resched(); } while (0)
190
# define cant_sleep() do { } while (0)
191
# define cant_migrate()		do { } while (0)
192
# define sched_annotate_sleep() do { } while (0)
193 194
# define non_block_start() do { } while (0)
# define non_block_end() do { } while (0)
L
Linus Torvalds 已提交
195 196
#endif

197
#define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
198

199 200
#if defined(CONFIG_MMU) && \
	(defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP))
201 202
#define might_fault() __might_fault(__FILE__, __LINE__)
void __might_fault(const char *file, int line);
203
#else
204
static inline void might_fault(void) { }
205 206
#endif

207
void do_exit(long error_code) __noreturn;
208

209 210
extern int num_to_str(char *buf, int size,
		      unsigned long long num, unsigned int width);
211

212 213
/* lib/printf utilities */

214 215 216 217 218 219 220 221 222 223
extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...);
extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list);
extern __printf(3, 4)
int snprintf(char *buf, size_t size, const char *fmt, ...);
extern __printf(3, 0)
int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
extern __printf(3, 4)
int scnprintf(char *buf, size_t size, const char *fmt, ...);
extern __printf(3, 0)
int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
224
extern __printf(2, 3) __malloc
225
char *kasprintf(gfp_t gfp, const char *fmt, ...);
226
extern __printf(2, 0) __malloc
227
char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
228 229
extern __printf(2, 0)
const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args);
L
Linus Torvalds 已提交
230

231 232 233 234
extern __scanf(2, 3)
int sscanf(const char *, const char *, ...);
extern __scanf(2, 0)
int vsscanf(const char *, const char *, va_list);
L
Linus Torvalds 已提交
235

236 237
extern int no_hash_pointers_enable(char *str);

L
Linus Torvalds 已提交
238 239
extern int get_option(char **str, int *pint);
extern char *get_options(const char *str, int nints, int *ints);
240
extern unsigned long long memparse(const char *ptr, char **retptr);
241
extern bool parse_option_str(const char *str, const char *option);
242
extern char *next_arg(char *args, char **param, char **val);
L
Linus Torvalds 已提交
243

244
extern int core_kernel_text(unsigned long addr);
L
Linus Torvalds 已提交
245 246
extern int __kernel_text_address(unsigned long addr);
extern int kernel_text_address(unsigned long addr);
247 248
extern int func_ptr_is_kernel_text(void *ptr);

L
Linus Torvalds 已提交
249
extern void bust_spinlocks(int yes);
250

251
extern int root_mountflags;
L
Linus Torvalds 已提交
252

253 254
extern bool early_boot_irqs_disabled;

255 256 257 258
/*
 * Values used for system_state. Ordering of the states must not be changed
 * as code checks for <, <=, >, >= STATE.
 */
L
Linus Torvalds 已提交
259 260
extern enum system_states {
	SYSTEM_BOOTING,
261
	SYSTEM_SCHEDULING,
262
	SYSTEM_FREEING_INITMEM,
L
Linus Torvalds 已提交
263 264 265 266
	SYSTEM_RUNNING,
	SYSTEM_HALT,
	SYSTEM_POWER_OFF,
	SYSTEM_RESTART,
267
	SYSTEM_SUSPEND,
L
Linus Torvalds 已提交
268 269
} system_state;

270 271 272 273
extern const char hex_asc[];
#define hex_asc_lo(x)	hex_asc[((x) & 0x0f)]
#define hex_asc_hi(x)	hex_asc[((x) & 0xf0) >> 4]

274
static inline char *hex_byte_pack(char *buf, u8 byte)
275 276 277 278 279
{
	*buf++ = hex_asc_hi(byte);
	*buf++ = hex_asc_lo(byte);
	return buf;
}
R
Randy Dunlap 已提交
280

281 282 283 284 285 286 287 288 289 290 291
extern const char hex_asc_upper[];
#define hex_asc_upper_lo(x)	hex_asc_upper[((x) & 0x0f)]
#define hex_asc_upper_hi(x)	hex_asc_upper[((x) & 0xf0) >> 4]

static inline char *hex_byte_pack_upper(char *buf, u8 byte)
{
	*buf++ = hex_asc_upper_hi(byte);
	*buf++ = hex_asc_upper_lo(byte);
	return buf;
}

292
extern int hex_to_bin(char ch);
M
Mimi Zohar 已提交
293
extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);
294
extern char *bin2hex(char *dst, const void *src, size_t count);
295

J
Joe Perches 已提交
296
bool mac_pton(const char *s, u8 *mac);
297

298 299
/*
 * General tracing related utility functions - trace_printk(),
300 301 302 303
 * tracing_on/tracing_off and tracing_start()/tracing_stop
 *
 * Use tracing_on/tracing_off when you want to quickly turn on or off
 * tracing. It simply enables or disables the recording of the trace events.
304
 * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on
305 306 307 308 309 310 311 312 313 314 315
 * file, which gives a means for the kernel and userspace to interact.
 * Place a tracing_off() in the kernel where you want tracing to end.
 * From user space, examine the trace, and then echo 1 > tracing_on
 * to continue tracing.
 *
 * tracing_stop/tracing_start has slightly more overhead. It is used
 * by things like suspend to ram where disabling the recording of the
 * trace is not enough, but tracing must actually stop because things
 * like calling smp_processor_id() may crash the system.
 *
 * Most likely, you want to use tracing_on/tracing_off.
316
 */
317 318 319 320 321 322 323

enum ftrace_dump_mode {
	DUMP_NONE,
	DUMP_ALL,
	DUMP_ORIG,
};

324
#ifdef CONFIG_TRACING
325 326 327
void tracing_on(void);
void tracing_off(void);
int tracing_is_on(void);
328 329
void tracing_snapshot(void);
void tracing_snapshot_alloc(void);
330

331 332 333
extern void tracing_start(void);
extern void tracing_stop(void);

334 335
static inline __printf(1, 2)
void ____trace_printk_check_format(const char *fmt, ...)
336 337 338 339 340 341 342 343
{
}
#define __trace_printk_check_format(fmt, args...)			\
do {									\
	if (0)								\
		____trace_printk_check_format(fmt, ##args);		\
} while (0)

344 345 346 347
/**
 * trace_printk - printf formatting in the ftrace buffer
 * @fmt: the printf format for printing
 *
348 349
 * Note: __trace_printk is an internal function for trace_printk() and
 *       the @ip is passed in via the trace_printk() macro.
350 351 352 353 354 355 356 357
 *
 * This function allows a kernel developer to debug fast path sections
 * that printk is not appropriate for. By scattering in various
 * printk like tracing in the code, a developer can quickly see
 * where problems are occurring.
 *
 * This is intended as a debugging tool for the developer only.
 * Please refrain from leaving trace_printks scattered around in
358
 * your code. (Extra memory is used for special buffers that are
359
 * allocated when trace_printk() is used.)
360
 *
W
Wei Wang 已提交
361
 * A little optimization trick is done here. If there's only one
362 363 364 365 366 367 368 369 370 371
 * argument, there's no need to scan the string for printf formats.
 * The trace_puts() will suffice. But how can we take advantage of
 * using trace_puts() when trace_printk() has only one argument?
 * By stringifying the args and checking the size we can tell
 * whether or not there are args. __stringify((__VA_ARGS__)) will
 * turn into "()\0" with a size of 3 when there are no args, anything
 * else will be bigger. All we need to do is define a string to this,
 * and then take its size and compare to 3. If it's bigger, use
 * do_trace_printk() otherwise, optimize it to trace_puts(). Then just
 * let gcc optimize the rest.
372
 */
373

374 375 376 377 378 379 380 381 382 383
#define trace_printk(fmt, ...)				\
do {							\
	char _______STR[] = __stringify((__VA_ARGS__));	\
	if (sizeof(_______STR) > 3)			\
		do_trace_printk(fmt, ##__VA_ARGS__);	\
	else						\
		trace_puts(fmt);			\
} while (0)

#define do_trace_printk(fmt, args...)					\
384
do {									\
385
	static const char *trace_printk_fmt __used			\
386
		__section("__trace_printk_fmt") =			\
387 388
		__builtin_constant_p(fmt) ? fmt : NULL;			\
									\
389
	__trace_printk_check_format(fmt, ##args);			\
390
									\
391
	if (__builtin_constant_p(fmt))					\
392
		__trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args);	\
393 394
	else								\
		__trace_printk(_THIS_IP_, fmt, ##args);			\
395 396
} while (0)

397 398
extern __printf(2, 3)
int __trace_bprintk(unsigned long ip, const char *fmt, ...);
399

400 401
extern __printf(2, 3)
int __trace_printk(unsigned long ip, const char *fmt, ...);
402

403 404 405 406 407 408 409 410
/**
 * trace_puts - write a string into the ftrace buffer
 * @str: the string to record
 *
 * Note: __trace_bputs is an internal function for trace_puts and
 *       the @ip is passed in via the trace_puts macro.
 *
 * This is similar to trace_printk() but is made for those really fast
411
 * paths that a developer wants the least amount of "Heisenbug" effects,
412 413 414 415 416 417 418 419 420 421
 * where the processing of the print format is still too much.
 *
 * This function allows a kernel developer to debug fast path sections
 * that printk is not appropriate for. By scattering in various
 * printk like tracing in the code, a developer can quickly see
 * where problems are occurring.
 *
 * This is intended as a debugging tool for the developer only.
 * Please refrain from leaving trace_puts scattered around in
 * your code. (Extra memory is used for special buffers that are
422
 * allocated when trace_puts() is used.)
423 424 425 426 427 428
 *
 * Returns: 0 if nothing was written, positive # if string was.
 *  (1 when __trace_bputs is used, strlen(str) when __trace_puts is used)
 */

#define trace_puts(str) ({						\
429
	static const char *trace_printk_fmt __used			\
430
		__section("__trace_printk_fmt") =			\
431 432 433 434 435 436 437
		__builtin_constant_p(str) ? str : NULL;			\
									\
	if (__builtin_constant_p(str))					\
		__trace_bputs(_THIS_IP_, trace_printk_fmt);		\
	else								\
		__trace_puts(_THIS_IP_, str, strlen(str));		\
})
438 439
extern int __trace_bputs(unsigned long ip, const char *str);
extern int __trace_puts(unsigned long ip, const char *str, int size);
440

441
extern void trace_dump_stack(int skip);
S
Steven Rostedt 已提交
442

443 444 445 446 447
/*
 * The double __builtin_constant_p is because gcc will give us an error
 * if we try to allocate the static variable to fmt if it is not a
 * constant. Even with the outer if statement.
 */
448 449
#define ftrace_vprintk(fmt, vargs)					\
do {									\
450
	if (__builtin_constant_p(fmt)) {				\
451
		static const char *trace_printk_fmt __used		\
452
		  __section("__trace_printk_fmt") =			\
453
			__builtin_constant_p(fmt) ? fmt : NULL;		\
I
Ingo Molnar 已提交
454
									\
455 456 457
		__ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs);	\
	} else								\
		__ftrace_vprintk(_THIS_IP_, fmt, vargs);		\
458 459
} while (0)

460
extern __printf(2, 0) int
461 462
__ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);

463
extern __printf(2, 0) int
464
__ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
465

466
extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
467 468 469
#else
static inline void tracing_start(void) { }
static inline void tracing_stop(void) { }
470
static inline void trace_dump_stack(int skip) { }
471 472 473 474

static inline void tracing_on(void) { }
static inline void tracing_off(void) { }
static inline int tracing_is_on(void) { return 0; }
475 476
static inline void tracing_snapshot(void) { }
static inline void tracing_snapshot_alloc(void) { }
477

478 479
static inline __printf(1, 2)
int trace_printk(const char *fmt, ...)
480 481 482
{
	return 0;
}
483
static __printf(1, 0) inline int
484 485 486 487
ftrace_vprintk(const char *fmt, va_list ap)
{
	return 0;
}
488
static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
489
#endif /* CONFIG_TRACING */
490

491 492 493 494 495 496 497
/* This counts to 12. Any more, it will return 13th argument. */
#define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n
#define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)

#define __CONCAT(a, b) a ## b
#define CONCATENATE(a, b) __CONCAT(a, b)

498 499 500 501
/* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
#ifdef CONFIG_FTRACE_MCOUNT_RECORD
# define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
#endif
502

503
/* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */
504 505 506 507 508 509 510 511 512 513
#define VERIFY_OCTAL_PERMISSIONS(perms)						\
	(BUILD_BUG_ON_ZERO((perms) < 0) +					\
	 BUILD_BUG_ON_ZERO((perms) > 0777) +					\
	 /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */		\
	 BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) +	\
	 BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) +		\
	 /* USER_WRITABLE >= GROUP_WRITABLE */					\
	 BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) +	\
	 /* OTHER_WRITABLE?  Generally considered a bad idea. */		\
	 BUILD_BUG_ON_ZERO((perms) & 2) +					\
514
	 (perms))
L
Linus Torvalds 已提交
515
#endif