tizen: Use unique directory prefix for baselibs packages
[platform/kernel/linux-rpi.git] / include / linux / printk.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __KERNEL_PRINTK__
3 #define __KERNEL_PRINTK__
4
5 #include <linux/stdarg.h>
6 #include <linux/init.h>
7 #include <linux/kern_levels.h>
8 #include <linux/linkage.h>
9 #include <linux/ratelimit_types.h>
10 #include <linux/once_lite.h>
11
12 struct uart_port;
13
14 extern const char linux_banner[];
15 extern const char linux_proc_banner[];
16
17 extern int oops_in_progress;    /* If set, an oops, panic(), BUG() or die() is in progress */
18
19 #define PRINTK_MAX_SINGLE_HEADER_LEN 2
20
21 static inline int printk_get_level(const char *buffer)
22 {
23         if (buffer[0] == KERN_SOH_ASCII && buffer[1]) {
24                 switch (buffer[1]) {
25                 case '0' ... '7':
26                 case 'c':       /* KERN_CONT */
27                         return buffer[1];
28                 }
29         }
30         return 0;
31 }
32
33 static inline const char *printk_skip_level(const char *buffer)
34 {
35         if (printk_get_level(buffer))
36                 return buffer + 2;
37
38         return buffer;
39 }
40
41 static inline const char *printk_skip_headers(const char *buffer)
42 {
43         while (printk_get_level(buffer))
44                 buffer = printk_skip_level(buffer);
45
46         return buffer;
47 }
48
49 /* printk's without a loglevel use this.. */
50 #define MESSAGE_LOGLEVEL_DEFAULT CONFIG_MESSAGE_LOGLEVEL_DEFAULT
51
52 /* We show everything that is MORE important than this.. */
53 #define CONSOLE_LOGLEVEL_SILENT  0 /* Mum's the word */
54 #define CONSOLE_LOGLEVEL_MIN     1 /* Minimum loglevel we let people use */
55 #define CONSOLE_LOGLEVEL_DEBUG  10 /* issue debug messages */
56 #define CONSOLE_LOGLEVEL_MOTORMOUTH 15  /* You can't shut this one up */
57
58 /*
59  * Default used to be hard-coded at 7, quiet used to be hardcoded at 4,
60  * we're now allowing both to be set from kernel config.
61  */
62 #define CONSOLE_LOGLEVEL_DEFAULT CONFIG_CONSOLE_LOGLEVEL_DEFAULT
63 #define CONSOLE_LOGLEVEL_QUIET   CONFIG_CONSOLE_LOGLEVEL_QUIET
64
65 extern int console_printk[];
66
67 #define console_loglevel (console_printk[0])
68 #define default_message_loglevel (console_printk[1])
69 #define minimum_console_loglevel (console_printk[2])
70 #define default_console_loglevel (console_printk[3])
71
72 extern void console_verbose(void);
73
74 /* strlen("ratelimit") + 1 */
75 #define DEVKMSG_STR_MAX_SIZE 10
76 extern char devkmsg_log_str[];
77 struct ctl_table;
78
79 extern int suppress_printk;
80
81 struct va_format {
82         const char *fmt;
83         va_list *va;
84 };
85
86 /*
87  * FW_BUG
88  * Add this to a message where you are sure the firmware is buggy or behaves
89  * really stupid or out of spec. Be aware that the responsible BIOS developer
90  * should be able to fix this issue or at least get a concrete idea of the
91  * problem by reading your message without the need of looking at the kernel
92  * code.
93  *
94  * Use it for definite and high priority BIOS bugs.
95  *
96  * FW_WARN
97  * Use it for not that clear (e.g. could the kernel messed up things already?)
98  * and medium priority BIOS bugs.
99  *
100  * FW_INFO
101  * Use this one if you want to tell the user or vendor about something
102  * suspicious, but generally harmless related to the firmware.
103  *
104  * Use it for information or very low priority BIOS bugs.
105  */
106 #define FW_BUG          "[Firmware Bug]: "
107 #define FW_WARN         "[Firmware Warn]: "
108 #define FW_INFO         "[Firmware Info]: "
109
110 /*
111  * HW_ERR
112  * Add this to a message for hardware errors, so that user can report
113  * it to hardware vendor instead of LKML or software vendor.
114  */
115 #define HW_ERR          "[Hardware Error]: "
116
117 /*
118  * DEPRECATED
119  * Add this to a message whenever you want to warn user space about the use
120  * of a deprecated aspect of an API so they can stop using it
121  */
122 #define DEPRECATED      "[Deprecated]: "
123
124 /*
125  * Dummy printk for disabled debugging statements to use whilst maintaining
126  * gcc's format checking.
127  */
128 #define no_printk(fmt, ...)                             \
129 ({                                                      \
130         if (0)                                          \
131                 printk(fmt, ##__VA_ARGS__);             \
132         0;                                              \
133 })
134
135 #ifdef CONFIG_EARLY_PRINTK
136 extern asmlinkage __printf(1, 2)
137 void early_printk(const char *fmt, ...);
138 #else
139 static inline __printf(1, 2) __cold
140 void early_printk(const char *s, ...) { }
141 #endif
142
143 struct dev_printk_info;
144
145 #ifdef CONFIG_PRINTK
146 asmlinkage __printf(4, 0)
147 int vprintk_emit(int facility, int level,
148                  const struct dev_printk_info *dev_info,
149                  const char *fmt, va_list args);
150
151 asmlinkage __printf(1, 0)
152 int vprintk(const char *fmt, va_list args);
153
154 asmlinkage __printf(1, 2) __cold
155 int _printk(const char *fmt, ...);
156
157 /*
158  * Special printk facility for scheduler/timekeeping use only, _DO_NOT_USE_ !
159  */
160 __printf(1, 2) __cold int _printk_deferred(const char *fmt, ...);
161
162 extern void __printk_safe_enter(void);
163 extern void __printk_safe_exit(void);
164 extern void __printk_deferred_enter(void);
165 extern void __printk_deferred_exit(void);
166
167 /*
168  * The printk_deferred_enter/exit macros are available only as a hack for
169  * some code paths that need to defer all printk console printing. Interrupts
170  * must be disabled for the deferred duration.
171  */
172 #define printk_deferred_enter() __printk_deferred_enter()
173 #define printk_deferred_exit() __printk_deferred_exit()
174
175 /*
176  * Please don't use printk_ratelimit(), because it shares ratelimiting state
177  * with all other unrelated printk_ratelimit() callsites.  Instead use
178  * printk_ratelimited() or plain old __ratelimit().
179  */
180 extern int __printk_ratelimit(const char *func);
181 #define printk_ratelimit() __printk_ratelimit(__func__)
182 extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
183                                    unsigned int interval_msec);
184
185 extern int printk_delay_msec;
186 extern int dmesg_restrict;
187
188 extern void wake_up_klogd(void);
189
190 char *log_buf_addr_get(void);
191 u32 log_buf_len_get(void);
192 void log_buf_vmcoreinfo_setup(void);
193 void __init setup_log_buf(int early);
194 __printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...);
195 void dump_stack_print_info(const char *log_lvl);
196 void show_regs_print_info(const char *log_lvl);
197 extern asmlinkage void dump_stack_lvl(const char *log_lvl) __cold;
198 extern asmlinkage void dump_stack(void) __cold;
199 void printk_trigger_flush(void);
200 void printk_legacy_allow_panic_sync(void);
201 extern void nbcon_acquire(struct uart_port *up);
202 extern void nbcon_release(struct uart_port *up);
203 void nbcon_atomic_flush_unsafe(void);
204 #else
205 static inline __printf(1, 0)
206 int vprintk(const char *s, va_list args)
207 {
208         return 0;
209 }
210 static inline __printf(1, 2) __cold
211 int _printk(const char *s, ...)
212 {
213         return 0;
214 }
215 static inline __printf(1, 2) __cold
216 int _printk_deferred(const char *s, ...)
217 {
218         return 0;
219 }
220
221 static inline void printk_deferred_enter(void)
222 {
223 }
224
225 static inline void printk_deferred_exit(void)
226 {
227 }
228
229 static inline int printk_ratelimit(void)
230 {
231         return 0;
232 }
233 static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies,
234                                           unsigned int interval_msec)
235 {
236         return false;
237 }
238
239 static inline void wake_up_klogd(void)
240 {
241 }
242
243 static inline char *log_buf_addr_get(void)
244 {
245         return NULL;
246 }
247
248 static inline u32 log_buf_len_get(void)
249 {
250         return 0;
251 }
252
253 static inline void log_buf_vmcoreinfo_setup(void)
254 {
255 }
256
257 static inline void setup_log_buf(int early)
258 {
259 }
260
261 static inline __printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...)
262 {
263 }
264
265 static inline void dump_stack_print_info(const char *log_lvl)
266 {
267 }
268
269 static inline void show_regs_print_info(const char *log_lvl)
270 {
271 }
272
273 static inline void dump_stack_lvl(const char *log_lvl)
274 {
275 }
276
277 static inline void dump_stack(void)
278 {
279 }
280 static inline void printk_trigger_flush(void)
281 {
282 }
283
284 static inline void printk_legacy_allow_panic_sync(void)
285 {
286 }
287
288 static inline void nbcon_acquire(struct uart_port *up)
289 {
290 }
291
292 static inline void nbcon_release(struct uart_port *up)
293 {
294 }
295
296 static inline void nbcon_atomic_flush_unsafe(void)
297 {
298 }
299
300 #endif
301
302 #ifdef CONFIG_SMP
303 extern int __printk_cpu_sync_try_get(void);
304 extern void __printk_cpu_sync_wait(void);
305 extern void __printk_cpu_sync_put(void);
306
307 #else
308
309 #define __printk_cpu_sync_try_get() true
310 #define __printk_cpu_sync_wait()
311 #define __printk_cpu_sync_put()
312 #endif /* CONFIG_SMP */
313
314 /**
315  * printk_cpu_sync_get_irqsave() - Disable interrupts and acquire the printk
316  *                                 cpu-reentrant spinning lock.
317  * @flags: Stack-allocated storage for saving local interrupt state,
318  *         to be passed to printk_cpu_sync_put_irqrestore().
319  *
320  * If the lock is owned by another CPU, spin until it becomes available.
321  * Interrupts are restored while spinning.
322  *
323  * CAUTION: This function must be used carefully. It does not behave like a
324  * typical lock. Here are important things to watch out for...
325  *
326  *     * This function is reentrant on the same CPU. Therefore the calling
327  *       code must not assume exclusive access to data if code accessing the
328  *       data can run reentrant or within NMI context on the same CPU.
329  *
330  *     * If there exists usage of this function from NMI context, it becomes
331  *       unsafe to perform any type of locking or spinning to wait for other
332  *       CPUs after calling this function from any context. This includes
333  *       using spinlocks or any other busy-waiting synchronization methods.
334  */
335 #define printk_cpu_sync_get_irqsave(flags)              \
336         for (;;) {                                      \
337                 local_irq_save(flags);                  \
338                 if (__printk_cpu_sync_try_get())        \
339                         break;                          \
340                 local_irq_restore(flags);               \
341                 __printk_cpu_sync_wait();               \
342         }
343
344 /**
345  * printk_cpu_sync_put_irqrestore() - Release the printk cpu-reentrant spinning
346  *                                    lock and restore interrupts.
347  * @flags: Caller's saved interrupt state, from printk_cpu_sync_get_irqsave().
348  */
349 #define printk_cpu_sync_put_irqrestore(flags)   \
350         do {                                    \
351                 __printk_cpu_sync_put();        \
352                 local_irq_restore(flags);       \
353         } while (0)
354
355 extern int kptr_restrict;
356
357 /**
358  * pr_fmt - used by the pr_*() macros to generate the printk format string
359  * @fmt: format string passed from a pr_*() macro
360  *
361  * This macro can be used to generate a unified format string for pr_*()
362  * macros. A common use is to prefix all pr_*() messages in a file with a common
363  * string. For example, defining this at the top of a source file:
364  *
365  *        #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
366  *
367  * would prefix all pr_info, pr_emerg... messages in the file with the module
368  * name.
369  */
370 #ifndef pr_fmt
371 #define pr_fmt(fmt) fmt
372 #endif
373
374 struct module;
375
376 #ifdef CONFIG_PRINTK_INDEX
377 struct pi_entry {
378         const char *fmt;
379         const char *func;
380         const char *file;
381         unsigned int line;
382
383         /*
384          * While printk and pr_* have the level stored in the string at compile
385          * time, some subsystems dynamically add it at runtime through the
386          * format string. For these dynamic cases, we allow the subsystem to
387          * tell us the level at compile time.
388          *
389          * NULL indicates that the level, if any, is stored in fmt.
390          */
391         const char *level;
392
393         /*
394          * The format string used by various subsystem specific printk()
395          * wrappers to prefix the message.
396          *
397          * Note that the static prefix defined by the pr_fmt() macro is stored
398          * directly in the message format (@fmt), not here.
399          */
400         const char *subsys_fmt_prefix;
401 } __packed;
402
403 #define __printk_index_emit(_fmt, _level, _subsys_fmt_prefix)           \
404         do {                                                            \
405                 if (__builtin_constant_p(_fmt) && __builtin_constant_p(_level)) { \
406                         /*
407                          * We check __builtin_constant_p multiple times here
408                          * for the same input because GCC will produce an error
409                          * if we try to assign a static variable to fmt if it
410                          * is not a constant, even with the outer if statement.
411                          */                                             \
412                         static const struct pi_entry _entry             \
413                         __used = {                                      \
414                                 .fmt = __builtin_constant_p(_fmt) ? (_fmt) : NULL, \
415                                 .func = __func__,                       \
416                                 .file = __FILE__,                       \
417                                 .line = __LINE__,                       \
418                                 .level = __builtin_constant_p(_level) ? (_level) : NULL, \
419                                 .subsys_fmt_prefix = _subsys_fmt_prefix,\
420                         };                                              \
421                         static const struct pi_entry *_entry_ptr        \
422                         __used __section(".printk_index") = &_entry;    \
423                 }                                                       \
424         } while (0)
425
426 #else /* !CONFIG_PRINTK_INDEX */
427 #define __printk_index_emit(...) do {} while (0)
428 #endif /* CONFIG_PRINTK_INDEX */
429
430 /*
431  * Some subsystems have their own custom printk that applies a va_format to a
432  * generic format, for example, to include a device number or other metadata
433  * alongside the format supplied by the caller.
434  *
435  * In order to store these in the way they would be emitted by the printk
436  * infrastructure, the subsystem provides us with the start, fixed string, and
437  * any subsequent text in the format string.
438  *
439  * We take a variable argument list as pr_fmt/dev_fmt/etc are sometimes passed
440  * as multiple arguments (eg: `"%s: ", "blah"`), and we must only take the
441  * first one.
442  *
443  * subsys_fmt_prefix must be known at compile time, or compilation will fail
444  * (since this is a mistake). If fmt or level is not known at compile time, no
445  * index entry will be made (since this can legitimately happen).
446  */
447 #define printk_index_subsys_emit(subsys_fmt_prefix, level, fmt, ...) \
448         __printk_index_emit(fmt, level, subsys_fmt_prefix)
449
450 #define printk_index_wrap(_p_func, _fmt, ...)                           \
451         ({                                                              \
452                 __printk_index_emit(_fmt, NULL, NULL);                  \
453                 _p_func(_fmt, ##__VA_ARGS__);                           \
454         })
455
456
457 /**
458  * printk - print a kernel message
459  * @fmt: format string
460  *
461  * This is printk(). It can be called from any context. We want it to work.
462  *
463  * If printk indexing is enabled, _printk() is called from printk_index_wrap.
464  * Otherwise, printk is simply #defined to _printk.
465  *
466  * We try to grab the console_lock. If we succeed, it's easy - we log the
467  * output and call the console drivers.  If we fail to get the semaphore, we
468  * place the output into the log buffer and return. The current holder of
469  * the console_sem will notice the new output in console_unlock(); and will
470  * send it to the consoles before releasing the lock.
471  *
472  * One effect of this deferred printing is that code which calls printk() and
473  * then changes console_loglevel may break. This is because console_loglevel
474  * is inspected when the actual printing occurs.
475  *
476  * See also:
477  * printf(3)
478  *
479  * See the vsnprintf() documentation for format string extensions over C99.
480  */
481 #define printk(fmt, ...) printk_index_wrap(_printk, fmt, ##__VA_ARGS__)
482 #define printk_deferred(fmt, ...)                                       \
483         printk_index_wrap(_printk_deferred, fmt, ##__VA_ARGS__)
484
485 /**
486  * pr_emerg - Print an emergency-level message
487  * @fmt: format string
488  * @...: arguments for the format string
489  *
490  * This macro expands to a printk with KERN_EMERG loglevel. It uses pr_fmt() to
491  * generate the format string.
492  */
493 #define pr_emerg(fmt, ...) \
494         printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
495 /**
496  * pr_alert - Print an alert-level message
497  * @fmt: format string
498  * @...: arguments for the format string
499  *
500  * This macro expands to a printk with KERN_ALERT loglevel. It uses pr_fmt() to
501  * generate the format string.
502  */
503 #define pr_alert(fmt, ...) \
504         printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
505 /**
506  * pr_crit - Print a critical-level message
507  * @fmt: format string
508  * @...: arguments for the format string
509  *
510  * This macro expands to a printk with KERN_CRIT loglevel. It uses pr_fmt() to
511  * generate the format string.
512  */
513 #define pr_crit(fmt, ...) \
514         printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
515 /**
516  * pr_err - Print an error-level message
517  * @fmt: format string
518  * @...: arguments for the format string
519  *
520  * This macro expands to a printk with KERN_ERR loglevel. It uses pr_fmt() to
521  * generate the format string.
522  */
523 #define pr_err(fmt, ...) \
524         printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
525 /**
526  * pr_warn - Print a warning-level message
527  * @fmt: format string
528  * @...: arguments for the format string
529  *
530  * This macro expands to a printk with KERN_WARNING loglevel. It uses pr_fmt()
531  * to generate the format string.
532  */
533 #define pr_warn(fmt, ...) \
534         printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
535 /**
536  * pr_notice - Print a notice-level message
537  * @fmt: format string
538  * @...: arguments for the format string
539  *
540  * This macro expands to a printk with KERN_NOTICE loglevel. It uses pr_fmt() to
541  * generate the format string.
542  */
543 #define pr_notice(fmt, ...) \
544         printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
545 /**
546  * pr_info - Print an info-level message
547  * @fmt: format string
548  * @...: arguments for the format string
549  *
550  * This macro expands to a printk with KERN_INFO loglevel. It uses pr_fmt() to
551  * generate the format string.
552  */
553 #define pr_info(fmt, ...) \
554         printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
555
556 /**
557  * pr_cont - Continues a previous log message in the same line.
558  * @fmt: format string
559  * @...: arguments for the format string
560  *
561  * This macro expands to a printk with KERN_CONT loglevel. It should only be
562  * used when continuing a log message with no newline ('\n') enclosed. Otherwise
563  * it defaults back to KERN_DEFAULT loglevel.
564  */
565 #define pr_cont(fmt, ...) \
566         printk(KERN_CONT fmt, ##__VA_ARGS__)
567
568 /**
569  * pr_devel - Print a debug-level message conditionally
570  * @fmt: format string
571  * @...: arguments for the format string
572  *
573  * This macro expands to a printk with KERN_DEBUG loglevel if DEBUG is
574  * defined. Otherwise it does nothing.
575  *
576  * It uses pr_fmt() to generate the format string.
577  */
578 #ifdef DEBUG
579 #define pr_devel(fmt, ...) \
580         printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
581 #else
582 #define pr_devel(fmt, ...) \
583         no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
584 #endif
585
586
587 /* If you are writing a driver, please use dev_dbg instead */
588 #if defined(CONFIG_DYNAMIC_DEBUG) || \
589         (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
590 #include <linux/dynamic_debug.h>
591
592 /**
593  * pr_debug - Print a debug-level message conditionally
594  * @fmt: format string
595  * @...: arguments for the format string
596  *
597  * This macro expands to dynamic_pr_debug() if CONFIG_DYNAMIC_DEBUG is
598  * set. Otherwise, if DEBUG is defined, it's equivalent to a printk with
599  * KERN_DEBUG loglevel. If DEBUG is not defined it does nothing.
600  *
601  * It uses pr_fmt() to generate the format string (dynamic_pr_debug() uses
602  * pr_fmt() internally).
603  */
604 #define pr_debug(fmt, ...)                      \
605         dynamic_pr_debug(fmt, ##__VA_ARGS__)
606 #elif defined(DEBUG)
607 #define pr_debug(fmt, ...) \
608         printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
609 #else
610 #define pr_debug(fmt, ...) \
611         no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
612 #endif
613
614 /*
615  * Print a one-time message (analogous to WARN_ONCE() et al):
616  */
617
618 #ifdef CONFIG_PRINTK
619 #define printk_once(fmt, ...)                                   \
620         DO_ONCE_LITE(printk, fmt, ##__VA_ARGS__)
621 #define printk_deferred_once(fmt, ...)                          \
622         DO_ONCE_LITE(printk_deferred, fmt, ##__VA_ARGS__)
623 #else
624 #define printk_once(fmt, ...)                                   \
625         no_printk(fmt, ##__VA_ARGS__)
626 #define printk_deferred_once(fmt, ...)                          \
627         no_printk(fmt, ##__VA_ARGS__)
628 #endif
629
630 #define pr_emerg_once(fmt, ...)                                 \
631         printk_once(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
632 #define pr_alert_once(fmt, ...)                                 \
633         printk_once(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
634 #define pr_crit_once(fmt, ...)                                  \
635         printk_once(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
636 #define pr_err_once(fmt, ...)                                   \
637         printk_once(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
638 #define pr_warn_once(fmt, ...)                                  \
639         printk_once(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
640 #define pr_notice_once(fmt, ...)                                \
641         printk_once(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
642 #define pr_info_once(fmt, ...)                                  \
643         printk_once(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
644 /* no pr_cont_once, don't do that... */
645
646 #if defined(DEBUG)
647 #define pr_devel_once(fmt, ...)                                 \
648         printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
649 #else
650 #define pr_devel_once(fmt, ...)                                 \
651         no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
652 #endif
653
654 /* If you are writing a driver, please use dev_dbg instead */
655 #if defined(DEBUG)
656 #define pr_debug_once(fmt, ...)                                 \
657         printk_once(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
658 #else
659 #define pr_debug_once(fmt, ...)                                 \
660         no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
661 #endif
662
663 /*
664  * ratelimited messages with local ratelimit_state,
665  * no local ratelimit_state used in the !PRINTK case
666  */
667 #ifdef CONFIG_PRINTK
668 #define printk_ratelimited(fmt, ...)                                    \
669 ({                                                                      \
670         static DEFINE_RATELIMIT_STATE(_rs,                              \
671                                       DEFAULT_RATELIMIT_INTERVAL,       \
672                                       DEFAULT_RATELIMIT_BURST);         \
673                                                                         \
674         if (__ratelimit(&_rs))                                          \
675                 printk(fmt, ##__VA_ARGS__);                             \
676 })
677 #else
678 #define printk_ratelimited(fmt, ...)                                    \
679         no_printk(fmt, ##__VA_ARGS__)
680 #endif
681
682 #define pr_emerg_ratelimited(fmt, ...)                                  \
683         printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
684 #define pr_alert_ratelimited(fmt, ...)                                  \
685         printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
686 #define pr_crit_ratelimited(fmt, ...)                                   \
687         printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
688 #define pr_err_ratelimited(fmt, ...)                                    \
689         printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
690 #define pr_warn_ratelimited(fmt, ...)                                   \
691         printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
692 #define pr_notice_ratelimited(fmt, ...)                                 \
693         printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
694 #define pr_info_ratelimited(fmt, ...)                                   \
695         printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
696 /* no pr_cont_ratelimited, don't do that... */
697
698 #if defined(DEBUG)
699 #define pr_devel_ratelimited(fmt, ...)                                  \
700         printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
701 #else
702 #define pr_devel_ratelimited(fmt, ...)                                  \
703         no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
704 #endif
705
706 /* If you are writing a driver, please use dev_dbg instead */
707 #if defined(CONFIG_DYNAMIC_DEBUG) || \
708         (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
709 /* descriptor check is first to prevent flooding with "callbacks suppressed" */
710 #define pr_debug_ratelimited(fmt, ...)                                  \
711 do {                                                                    \
712         static DEFINE_RATELIMIT_STATE(_rs,                              \
713                                       DEFAULT_RATELIMIT_INTERVAL,       \
714                                       DEFAULT_RATELIMIT_BURST);         \
715         DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, pr_fmt(fmt));         \
716         if (DYNAMIC_DEBUG_BRANCH(descriptor) &&                         \
717             __ratelimit(&_rs))                                          \
718                 __dynamic_pr_debug(&descriptor, pr_fmt(fmt), ##__VA_ARGS__);    \
719 } while (0)
720 #elif defined(DEBUG)
721 #define pr_debug_ratelimited(fmt, ...)                                  \
722         printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
723 #else
724 #define pr_debug_ratelimited(fmt, ...) \
725         no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
726 #endif
727
728 extern const struct file_operations kmsg_fops;
729
730 enum {
731         DUMP_PREFIX_NONE,
732         DUMP_PREFIX_ADDRESS,
733         DUMP_PREFIX_OFFSET
734 };
735 extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
736                               int groupsize, char *linebuf, size_t linebuflen,
737                               bool ascii);
738 #ifdef CONFIG_PRINTK
739 extern void print_hex_dump(const char *level, const char *prefix_str,
740                            int prefix_type, int rowsize, int groupsize,
741                            const void *buf, size_t len, bool ascii);
742 #else
743 static inline void print_hex_dump(const char *level, const char *prefix_str,
744                                   int prefix_type, int rowsize, int groupsize,
745                                   const void *buf, size_t len, bool ascii)
746 {
747 }
748 static inline void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
749                                         const void *buf, size_t len)
750 {
751 }
752
753 #endif
754
755 #if defined(CONFIG_DYNAMIC_DEBUG) || \
756         (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
757 #define print_hex_dump_debug(prefix_str, prefix_type, rowsize,  \
758                              groupsize, buf, len, ascii)        \
759         dynamic_hex_dump(prefix_str, prefix_type, rowsize,      \
760                          groupsize, buf, len, ascii)
761 #elif defined(DEBUG)
762 #define print_hex_dump_debug(prefix_str, prefix_type, rowsize,          \
763                              groupsize, buf, len, ascii)                \
764         print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, rowsize,    \
765                        groupsize, buf, len, ascii)
766 #else
767 static inline void print_hex_dump_debug(const char *prefix_str, int prefix_type,
768                                         int rowsize, int groupsize,
769                                         const void *buf, size_t len, bool ascii)
770 {
771 }
772 #endif
773
774 /**
775  * print_hex_dump_bytes - shorthand form of print_hex_dump() with default params
776  * @prefix_str: string to prefix each line with;
777  *  caller supplies trailing spaces for alignment if desired
778  * @prefix_type: controls whether prefix of an offset, address, or none
779  *  is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
780  * @buf: data blob to dump
781  * @len: number of bytes in the @buf
782  *
783  * Calls print_hex_dump(), with log level of KERN_DEBUG,
784  * rowsize of 16, groupsize of 1, and ASCII output included.
785  */
786 #define print_hex_dump_bytes(prefix_str, prefix_type, buf, len) \
787         print_hex_dump_debug(prefix_str, prefix_type, 16, 1, buf, len, true)
788
789 #endif