1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_KERNEL_H
3 #define _LINUX_KERNEL_H
4
5
6 #include <stdarg.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/kstrtox.h>
14 #include <linux/log2.h>
15 #include <linux/minmax.h>
16 #include <linux/typecheck.h>
17 #include <linux/printk.h>
18 #include <linux/build_bug.h>
19 #include <asm/byteorder.h>
20 #include <asm/div64.h>
21 #include <uapi/linux/kernel.h>
22
23 #define STACK_MAGIC 0xdeadbeef
24
25 /**
26 * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value
27 * @x: value to repeat
28 *
29 * NOTE: @x is not checked for > 0xff; larger values produce odd results.
30 */
31 #define REPEAT_BYTE(x) ((~0ul / 0xff) * (x))
32
33 /* @a is a power of 2 value */
34 #define ALIGN(x, a) __ALIGN_KERNEL((x), (a))
35 #define ALIGN_DOWN(x, a) __ALIGN_KERNEL((x) - ((a) - 1), (a))
36 #define __ALIGN_MASK(x, mask) __ALIGN_KERNEL_MASK((x), (mask))
37 #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
38 #define PTR_ALIGN_DOWN(p, a) ((typeof(p))ALIGN_DOWN((unsigned long)(p), (a)))
39 #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
40
41 /* generic data direction definitions */
42 #define READ 0
43 #define WRITE 1
44
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
51 #define PTR_IF(cond, ptr) ((cond) ? (ptr) : NULL)
52
53 #define u64_to_user_ptr(x) ( \
54 { \
55 typecheck(u64, (x)); \
56 (void __user *)(uintptr_t)(x); \
57 } \
58 )
59
60 /*
61 * This looks more complex than it should be. But we need to
62 * get the type for the ~ right in round_down (it needs to be
63 * as wide as the result!), and we want to evaluate the macro
64 * arguments just once each.
65 */
66 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
67 /**
68 * round_up - round up to next specified power of 2
69 * @x: the value to round
70 * @y: multiple to round up to (must be a power of 2)
71 *
72 * Rounds @x up to next multiple of @y (which must be a power of 2).
73 * To perform arbitrary rounding up, use roundup() below.
74 */
75 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
76 /**
77 * round_down - round down to next specified power of 2
78 * @x: the value to round
79 * @y: multiple to round down to (must be a power of 2)
80 *
81 * Rounds @x down to next multiple of @y (which must be a power of 2).
82 * To perform arbitrary rounding down, use rounddown() below.
83 */
84 #define round_down(x, y) ((x) & ~__round_mask(x, y))
85
86 #define typeof_member(T, m) typeof(((T*)0)->m)
87
88 #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP
89
90 #define DIV_ROUND_DOWN_ULL(ll, d) \
91 ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
92
93 #define DIV_ROUND_UP_ULL(ll, d) \
94 DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d))
95
96 #if BITS_PER_LONG == 32
97 # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d)
98 #else
99 # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d)
100 #endif
101
102 /**
103 * roundup - round up to the next specified multiple
104 * @x: the value to up
105 * @y: multiple to round up to
106 *
107 * Rounds @x up to next multiple of @y. If @y will always be a power
108 * of 2, consider using the faster round_up().
109 */
110 #define roundup(x, y) ( \
111 { \
112 typeof(y) __y = y; \
113 (((x) + (__y - 1)) / __y) * __y; \
114 } \
115 )
116 /**
117 * rounddown - round down to next specified multiple
118 * @x: the value to round
119 * @y: multiple to round down to
120 *
121 * Rounds @x down to next multiple of @y. If @y will always be a power
122 * of 2, consider using the faster round_down().
123 */
124 #define rounddown(x, y) ( \
125 { \
126 typeof(x) __x = (x); \
127 __x - (__x % (y)); \
128 } \
129 )
130
131 /*
132 * Divide positive or negative dividend by positive or negative divisor
133 * and round to closest integer. Result is undefined for negative
134 * divisors if the dividend variable type is unsigned and for negative
135 * dividends if the divisor variable type is unsigned.
136 */
137 #define DIV_ROUND_CLOSEST(x, divisor)( \
138 { \
139 typeof(x) __x = x; \
140 typeof(divisor) __d = divisor; \
141 (((typeof(x))-1) > 0 || \
142 ((typeof(divisor))-1) > 0 || \
143 (((__x) > 0) == ((__d) > 0))) ? \
144 (((__x) + ((__d) / 2)) / (__d)) : \
145 (((__x) - ((__d) / 2)) / (__d)); \
146 } \
147 )
148 /*
149 * Same as above but for u64 dividends. divisor must be a 32-bit
150 * number.
151 */
152 #define DIV_ROUND_CLOSEST_ULL(x, divisor)( \
153 { \
154 typeof(divisor) __d = divisor; \
155 unsigned long long _tmp = (x) + (__d) / 2; \
156 do_div(_tmp, __d); \
157 _tmp; \
158 } \
159 )
160
161 /*
162 * Multiplies an integer by a fraction, while avoiding unnecessary
163 * overflow or loss of precision.
164 */
165 #define mult_frac(x, numer, denom)( \
166 { \
167 typeof(x) quot = (x) / (denom); \
168 typeof(x) rem = (x) % (denom); \
169 (quot * (numer)) + ((rem * (numer)) / (denom)); \
170 } \
171 )
172
173
174 #define _RET_IP_ (unsigned long)__builtin_return_address(0)
175 #define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; })
176
177 #define sector_div(a, b) do_div(a, b)
178
179 /**
180 * upper_32_bits - return bits 32-63 of a number
181 * @n: the number we're accessing
182 *
183 * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
184 * the "right shift count >= width of type" warning when that quantity is
185 * 32-bits.
186 */
187 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
188
189 /**
190 * lower_32_bits - return bits 0-31 of a number
191 * @n: the number we're accessing
192 */
193 #define lower_32_bits(n) ((u32)((n) & 0xffffffff))
194
195 struct completion;
196 struct pt_regs;
197 struct user;
198
199 #ifdef CONFIG_PREEMPT_VOLUNTARY
200 extern int _cond_resched(void);
201 # define might_resched() _cond_resched()
202 #else
203 # define might_resched() do { } while (0)
204 #endif
205
206 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
207 extern void ___might_sleep(const char *file, int line, int preempt_offset);
208 extern void __might_sleep(const char *file, int line, int preempt_offset);
209 extern void __cant_sleep(const char *file, int line, int preempt_offset);
210
211 /**
212 * might_sleep - annotation for functions that can sleep
213 *
214 * this macro will print a stack trace if it is executed in an atomic
215 * context (spinlock, irq-handler, ...). Additional sections where blocking is
216 * not allowed can be annotated with non_block_start() and non_block_end()
217 * pairs.
218 *
219 * This is a useful debugging help to be able to catch problems early and not
220 * be bitten later when the calling function happens to sleep when it is not
221 * supposed to.
222 */
223 # define might_sleep() \
224 do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
225 /**
226 * cant_sleep - annotation for functions that cannot sleep
227 *
228 * this macro will print a stack trace if it is executed with preemption enabled
229 */
230 # define cant_sleep() \
231 do { __cant_sleep(__FILE__, __LINE__, 0); } while (0)
232 # define sched_annotate_sleep() (current->task_state_change = 0)
233 /**
234 * non_block_start - annotate the start of section where sleeping is prohibited
235 *
236 * This is on behalf of the oom reaper, specifically when it is calling the mmu
237 * notifiers. The problem is that if the notifier were to block on, for example,
238 * mutex_lock() and if the process which holds that mutex were to perform a
239 * sleeping memory allocation, the oom reaper is now blocked on completion of
240 * that memory allocation. Other blocking calls like wait_event() pose similar
241 * issues.
242 */
243 # define non_block_start() (current->non_block_count++)
244 /**
245 * non_block_end - annotate the end of section where sleeping is prohibited
246 *
247 * Closes a section opened by non_block_start().
248 */
249 # define non_block_end() WARN_ON(current->non_block_count-- == 0)
250 #else
___might_sleep(const char * file,int line,int preempt_offset)251 static inline void ___might_sleep(const char *file, int line,
252 int preempt_offset) { }
__might_sleep(const char * file,int line,int preempt_offset)253 static inline void __might_sleep(const char *file, int line,
254 int preempt_offset) { }
255 # define might_sleep() do { might_resched(); } while (0)
256 # define cant_sleep() do { } while (0)
257 # define sched_annotate_sleep() do { } while (0)
258 # define non_block_start() do { } while (0)
259 # define non_block_end() do { } while (0)
260 #endif
261
262 #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
263
264 #ifndef CONFIG_PREEMPT_RT
265 # define cant_migrate() cant_sleep()
266 #else
267 /* Placeholder for now */
268 # define cant_migrate() do { } while (0)
269 #endif
270
271 /**
272 * abs - return absolute value of an argument
273 * @x: the value. If it is unsigned type, it is converted to signed type first.
274 * char is treated as if it was signed (regardless of whether it really is)
275 * but the macro's return type is preserved as char.
276 *
277 * Return: an absolute value of x.
278 */
279 #define abs(x) __abs_choose_expr(x, long long, \
280 __abs_choose_expr(x, long, \
281 __abs_choose_expr(x, int, \
282 __abs_choose_expr(x, short, \
283 __abs_choose_expr(x, char, \
284 __builtin_choose_expr( \
285 __builtin_types_compatible_p(typeof(x), char), \
286 (char)({ signed char __x = (x); __x<0?-__x:__x; }), \
287 ((void)0)))))))
288
289 #define __abs_choose_expr(x, type, other) __builtin_choose_expr( \
290 __builtin_types_compatible_p(typeof(x), signed type) || \
291 __builtin_types_compatible_p(typeof(x), unsigned type), \
292 ({ signed type __x = (x); __x < 0 ? -__x : __x; }), other)
293
294 /**
295 * reciprocal_scale - "scale" a value into range [0, ep_ro)
296 * @val: value
297 * @ep_ro: right open interval endpoint
298 *
299 * Perform a "reciprocal multiplication" in order to "scale" a value into
300 * range [0, @ep_ro), where the upper interval endpoint is right-open.
301 * This is useful, e.g. for accessing a index of an array containing
302 * @ep_ro elements, for example. Think of it as sort of modulus, only that
303 * the result isn't that of modulo. ;) Note that if initial input is a
304 * small value, then result will return 0.
305 *
306 * Return: a result based on @val in interval [0, @ep_ro).
307 */
reciprocal_scale(u32 val,u32 ep_ro)308 static inline u32 reciprocal_scale(u32 val, u32 ep_ro)
309 {
310 return (u32)(((u64) val * ep_ro) >> 32);
311 }
312
313 #if defined(CONFIG_MMU) && \
314 (defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP))
315 #define might_fault() __might_fault(__FILE__, __LINE__)
316 void __might_fault(const char *file, int line);
317 #else
might_fault(void)318 static inline void might_fault(void) { }
319 #endif
320
321 extern struct atomic_notifier_head panic_notifier_list;
322 extern long (*panic_blink)(int state);
323 __printf(1, 2)
324 void panic(const char *fmt, ...) __noreturn __cold;
325 void nmi_panic(struct pt_regs *regs, const char *msg);
326 void check_panic_on_warn(const char *origin);
327 extern void oops_enter(void);
328 extern void oops_exit(void);
329 extern bool oops_may_print(void);
330 void do_exit(long error_code) __noreturn;
331 void complete_and_exit(struct completion *, long) __noreturn;
332
333 extern int num_to_str(char *buf, int size,
334 unsigned long long num, unsigned int width);
335
336 /* lib/printf utilities */
337
338 extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...);
339 extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list);
340 extern __printf(3, 4)
341 int snprintf(char *buf, size_t size, const char *fmt, ...);
342 extern __printf(3, 0)
343 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
344 extern __printf(3, 4)
345 int scnprintf(char *buf, size_t size, const char *fmt, ...);
346 extern __printf(3, 0)
347 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
348 extern __printf(2, 3) __malloc
349 char *kasprintf(gfp_t gfp, const char *fmt, ...);
350 extern __printf(2, 0) __malloc
351 char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
352 extern __printf(2, 0)
353 const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args);
354
355 extern __scanf(2, 3)
356 int sscanf(const char *, const char *, ...);
357 extern __scanf(2, 0)
358 int vsscanf(const char *, const char *, va_list);
359
360 extern int get_option(char **str, int *pint);
361 extern char *get_options(const char *str, int nints, int *ints);
362 extern unsigned long long memparse(const char *ptr, char **retptr);
363 extern bool parse_option_str(const char *str, const char *option);
364 extern char *next_arg(char *args, char **param, char **val);
365
366 extern int core_kernel_text(unsigned long addr);
367 extern int init_kernel_text(unsigned long addr);
368 extern int core_kernel_data(unsigned long addr);
369 extern int __kernel_text_address(unsigned long addr);
370 extern int kernel_text_address(unsigned long addr);
371 extern int func_ptr_is_kernel_text(void *ptr);
372
373 u64 int_pow(u64 base, unsigned int exp);
374 unsigned long int_sqrt(unsigned long);
375
376 #if BITS_PER_LONG < 64
377 u32 int_sqrt64(u64 x);
378 #else
int_sqrt64(u64 x)379 static inline u32 int_sqrt64(u64 x)
380 {
381 return (u32)int_sqrt(x);
382 }
383 #endif
384
385 extern void bust_spinlocks(int yes);
386 extern int panic_timeout;
387 extern unsigned long panic_print;
388 extern int panic_on_oops;
389 extern int panic_on_unrecovered_nmi;
390 extern int panic_on_io_nmi;
391 extern int panic_on_warn;
392 extern unsigned long panic_on_taint;
393 extern bool panic_on_taint_nousertaint;
394 extern int sysctl_panic_on_rcu_stall;
395 extern int sysctl_panic_on_stackoverflow;
396
397 extern bool crash_kexec_post_notifiers;
398
399 /*
400 * panic_cpu is used for synchronizing panic() and crash_kexec() execution. It
401 * holds a CPU number which is executing panic() currently. A value of
402 * PANIC_CPU_INVALID means no CPU has entered panic() or crash_kexec().
403 */
404 extern atomic_t panic_cpu;
405 #define PANIC_CPU_INVALID -1
406
407 /*
408 * Only to be used by arch init code. If the user over-wrote the default
409 * CONFIG_PANIC_TIMEOUT, honor it.
410 */
set_arch_panic_timeout(int timeout,int arch_default_timeout)411 static inline void set_arch_panic_timeout(int timeout, int arch_default_timeout)
412 {
413 if (panic_timeout == arch_default_timeout)
414 panic_timeout = timeout;
415 }
416 extern const char *print_tainted(void);
417 enum lockdep_ok {
418 LOCKDEP_STILL_OK,
419 LOCKDEP_NOW_UNRELIABLE
420 };
421 extern void add_taint(unsigned flag, enum lockdep_ok);
422 extern int test_taint(unsigned flag);
423 extern unsigned long get_taint(void);
424 extern int root_mountflags;
425
426 extern bool early_boot_irqs_disabled;
427
428 /*
429 * Values used for system_state. Ordering of the states must not be changed
430 * as code checks for <, <=, >, >= STATE.
431 */
432 extern enum system_states {
433 SYSTEM_BOOTING,
434 SYSTEM_SCHEDULING,
435 SYSTEM_RUNNING,
436 SYSTEM_HALT,
437 SYSTEM_POWER_OFF,
438 SYSTEM_RESTART,
439 SYSTEM_SUSPEND,
440 } system_state;
441
442 /* This cannot be an enum because some may be used in assembly source. */
443 #define TAINT_PROPRIETARY_MODULE 0
444 #define TAINT_FORCED_MODULE 1
445 #define TAINT_CPU_OUT_OF_SPEC 2
446 #define TAINT_FORCED_RMMOD 3
447 #define TAINT_MACHINE_CHECK 4
448 #define TAINT_BAD_PAGE 5
449 #define TAINT_USER 6
450 #define TAINT_DIE 7
451 #define TAINT_OVERRIDDEN_ACPI_TABLE 8
452 #define TAINT_WARN 9
453 #define TAINT_CRAP 10
454 #define TAINT_FIRMWARE_WORKAROUND 11
455 #define TAINT_OOT_MODULE 12
456 #define TAINT_UNSIGNED_MODULE 13
457 #define TAINT_SOFTLOCKUP 14
458 #define TAINT_LIVEPATCH 15
459 #define TAINT_AUX 16
460 #define TAINT_RANDSTRUCT 17
461 #define TAINT_FLAGS_COUNT 18
462 #define TAINT_FLAGS_MAX ((1UL << TAINT_FLAGS_COUNT) - 1)
463
464 struct taint_flag {
465 char c_true; /* character printed when tainted */
466 char c_false; /* character printed when not tainted */
467 bool module; /* also show as a per-module taint flag */
468 };
469
470 extern const struct taint_flag taint_flags[TAINT_FLAGS_COUNT];
471
472 extern const char hex_asc[];
473 #define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
474 #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
475
hex_byte_pack(char * buf,u8 byte)476 static inline char *hex_byte_pack(char *buf, u8 byte)
477 {
478 *buf++ = hex_asc_hi(byte);
479 *buf++ = hex_asc_lo(byte);
480 return buf;
481 }
482
483 extern const char hex_asc_upper[];
484 #define hex_asc_upper_lo(x) hex_asc_upper[((x) & 0x0f)]
485 #define hex_asc_upper_hi(x) hex_asc_upper[((x) & 0xf0) >> 4]
486
hex_byte_pack_upper(char * buf,u8 byte)487 static inline char *hex_byte_pack_upper(char *buf, u8 byte)
488 {
489 *buf++ = hex_asc_upper_hi(byte);
490 *buf++ = hex_asc_upper_lo(byte);
491 return buf;
492 }
493
494 extern int hex_to_bin(unsigned char ch);
495 extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);
496 extern char *bin2hex(char *dst, const void *src, size_t count);
497
498 bool mac_pton(const char *s, u8 *mac);
499
500 /*
501 * General tracing related utility functions - trace_printk(),
502 * tracing_on/tracing_off and tracing_start()/tracing_stop
503 *
504 * Use tracing_on/tracing_off when you want to quickly turn on or off
505 * tracing. It simply enables or disables the recording of the trace events.
506 * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on
507 * file, which gives a means for the kernel and userspace to interact.
508 * Place a tracing_off() in the kernel where you want tracing to end.
509 * From user space, examine the trace, and then echo 1 > tracing_on
510 * to continue tracing.
511 *
512 * tracing_stop/tracing_start has slightly more overhead. It is used
513 * by things like suspend to ram where disabling the recording of the
514 * trace is not enough, but tracing must actually stop because things
515 * like calling smp_processor_id() may crash the system.
516 *
517 * Most likely, you want to use tracing_on/tracing_off.
518 */
519
520 enum ftrace_dump_mode {
521 DUMP_NONE,
522 DUMP_ALL,
523 DUMP_ORIG,
524 };
525
526 #ifdef CONFIG_TRACING
527 void tracing_on(void);
528 void tracing_off(void);
529 int tracing_is_on(void);
530 void tracing_snapshot(void);
531 void tracing_snapshot_alloc(void);
532
533 extern void tracing_start(void);
534 extern void tracing_stop(void);
535
536 static inline __printf(1, 2)
____trace_printk_check_format(const char * fmt,...)537 void ____trace_printk_check_format(const char *fmt, ...)
538 {
539 }
540 #define __trace_printk_check_format(fmt, args...) \
541 do { \
542 if (0) \
543 ____trace_printk_check_format(fmt, ##args); \
544 } while (0)
545
546 /**
547 * trace_printk - printf formatting in the ftrace buffer
548 * @fmt: the printf format for printing
549 *
550 * Note: __trace_printk is an internal function for trace_printk() and
551 * the @ip is passed in via the trace_printk() macro.
552 *
553 * This function allows a kernel developer to debug fast path sections
554 * that printk is not appropriate for. By scattering in various
555 * printk like tracing in the code, a developer can quickly see
556 * where problems are occurring.
557 *
558 * This is intended as a debugging tool for the developer only.
559 * Please refrain from leaving trace_printks scattered around in
560 * your code. (Extra memory is used for special buffers that are
561 * allocated when trace_printk() is used.)
562 *
563 * A little optimization trick is done here. If there's only one
564 * argument, there's no need to scan the string for printf formats.
565 * The trace_puts() will suffice. But how can we take advantage of
566 * using trace_puts() when trace_printk() has only one argument?
567 * By stringifying the args and checking the size we can tell
568 * whether or not there are args. __stringify((__VA_ARGS__)) will
569 * turn into "()\0" with a size of 3 when there are no args, anything
570 * else will be bigger. All we need to do is define a string to this,
571 * and then take its size and compare to 3. If it's bigger, use
572 * do_trace_printk() otherwise, optimize it to trace_puts(). Then just
573 * let gcc optimize the rest.
574 */
575
576 #define trace_printk(fmt, ...) \
577 do { \
578 char _______STR[] = __stringify((__VA_ARGS__)); \
579 if (sizeof(_______STR) > 3) \
580 do_trace_printk(fmt, ##__VA_ARGS__); \
581 else \
582 trace_puts(fmt); \
583 } while (0)
584
585 #define do_trace_printk(fmt, args...) \
586 do { \
587 static const char *trace_printk_fmt __used \
588 __section("__trace_printk_fmt") = \
589 __builtin_constant_p(fmt) ? fmt : NULL; \
590 \
591 __trace_printk_check_format(fmt, ##args); \
592 \
593 if (__builtin_constant_p(fmt)) \
594 __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \
595 else \
596 __trace_printk(_THIS_IP_, fmt, ##args); \
597 } while (0)
598
599 extern __printf(2, 3)
600 int __trace_bprintk(unsigned long ip, const char *fmt, ...);
601
602 extern __printf(2, 3)
603 int __trace_printk(unsigned long ip, const char *fmt, ...);
604
605 /**
606 * trace_puts - write a string into the ftrace buffer
607 * @str: the string to record
608 *
609 * Note: __trace_bputs is an internal function for trace_puts and
610 * the @ip is passed in via the trace_puts macro.
611 *
612 * This is similar to trace_printk() but is made for those really fast
613 * paths that a developer wants the least amount of "Heisenbug" effects,
614 * where the processing of the print format is still too much.
615 *
616 * This function allows a kernel developer to debug fast path sections
617 * that printk is not appropriate for. By scattering in various
618 * printk like tracing in the code, a developer can quickly see
619 * where problems are occurring.
620 *
621 * This is intended as a debugging tool for the developer only.
622 * Please refrain from leaving trace_puts scattered around in
623 * your code. (Extra memory is used for special buffers that are
624 * allocated when trace_puts() is used.)
625 *
626 * Returns: 0 if nothing was written, positive # if string was.
627 * (1 when __trace_bputs is used, strlen(str) when __trace_puts is used)
628 */
629
630 #define trace_puts(str) ({ \
631 static const char *trace_printk_fmt __used \
632 __section("__trace_printk_fmt") = \
633 __builtin_constant_p(str) ? str : NULL; \
634 \
635 if (__builtin_constant_p(str)) \
636 __trace_bputs(_THIS_IP_, trace_printk_fmt); \
637 else \
638 __trace_puts(_THIS_IP_, str, strlen(str)); \
639 })
640 extern int __trace_bputs(unsigned long ip, const char *str);
641 extern int __trace_puts(unsigned long ip, const char *str, int size);
642
643 extern void trace_dump_stack(int skip);
644
645 /*
646 * The double __builtin_constant_p is because gcc will give us an error
647 * if we try to allocate the static variable to fmt if it is not a
648 * constant. Even with the outer if statement.
649 */
650 #define ftrace_vprintk(fmt, vargs) \
651 do { \
652 if (__builtin_constant_p(fmt)) { \
653 static const char *trace_printk_fmt __used \
654 __section("__trace_printk_fmt") = \
655 __builtin_constant_p(fmt) ? fmt : NULL; \
656 \
657 __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \
658 } else \
659 __ftrace_vprintk(_THIS_IP_, fmt, vargs); \
660 } while (0)
661
662 extern __printf(2, 0) int
663 __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
664
665 extern __printf(2, 0) int
666 __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
667
668 extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
669 #else
tracing_start(void)670 static inline void tracing_start(void) { }
tracing_stop(void)671 static inline void tracing_stop(void) { }
trace_dump_stack(int skip)672 static inline void trace_dump_stack(int skip) { }
673
tracing_on(void)674 static inline void tracing_on(void) { }
tracing_off(void)675 static inline void tracing_off(void) { }
tracing_is_on(void)676 static inline int tracing_is_on(void) { return 0; }
tracing_snapshot(void)677 static inline void tracing_snapshot(void) { }
tracing_snapshot_alloc(void)678 static inline void tracing_snapshot_alloc(void) { }
679
680 static inline __printf(1, 2)
trace_printk(const char * fmt,...)681 int trace_printk(const char *fmt, ...)
682 {
683 return 0;
684 }
685 static __printf(1, 0) inline int
ftrace_vprintk(const char * fmt,va_list ap)686 ftrace_vprintk(const char *fmt, va_list ap)
687 {
688 return 0;
689 }
ftrace_dump(enum ftrace_dump_mode oops_dump_mode)690 static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
691 #endif /* CONFIG_TRACING */
692
693 /* This counts to 12. Any more, it will return 13th argument. */
694 #define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n
695 #define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
696
697 #define __CONCAT(a, b) a ## b
698 #define CONCATENATE(a, b) __CONCAT(a, b)
699
700 /**
701 * container_of - cast a member of a structure out to the containing structure
702 * @ptr: the pointer to the member.
703 * @type: the type of the container struct this is embedded in.
704 * @member: the name of the member within the struct.
705 *
706 */
707 #define container_of(ptr, type, member) ({ \
708 void *__mptr = (void *)(ptr); \
709 BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) && \
710 !__same_type(*(ptr), void), \
711 "pointer type mismatch in container_of()"); \
712 ((type *)(__mptr - offsetof(type, member))); })
713
714 /**
715 * container_of_safe - cast a member of a structure out to the containing structure
716 * @ptr: the pointer to the member.
717 * @type: the type of the container struct this is embedded in.
718 * @member: the name of the member within the struct.
719 *
720 * If IS_ERR_OR_NULL(ptr), ptr is returned unchanged.
721 */
722 #define container_of_safe(ptr, type, member) ({ \
723 void *__mptr = (void *)(ptr); \
724 BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) && \
725 !__same_type(*(ptr), void), \
726 "pointer type mismatch in container_of()"); \
727 IS_ERR_OR_NULL(__mptr) ? ERR_CAST(__mptr) : \
728 ((type *)(__mptr - offsetof(type, member))); })
729
730 /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
731 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
732 # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
733 #endif
734
735 /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */
736 #define VERIFY_OCTAL_PERMISSIONS(perms) \
737 (BUILD_BUG_ON_ZERO((perms) < 0) + \
738 BUILD_BUG_ON_ZERO((perms) > 0777) + \
739 /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */ \
740 BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) + \
741 BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) + \
742 /* USER_WRITABLE >= GROUP_WRITABLE */ \
743 BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) + \
744 /* OTHER_WRITABLE? Generally considered a bad idea. */ \
745 BUILD_BUG_ON_ZERO((perms) & 2) + \
746 (perms))
747 #endif
748