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