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