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