[PATCH master v2 1/3] include: linux/printk: implement pr_*_once macros

Ahmad Fatoum a.fatoum at pengutronix.de
Thu Feb 2 05:34:13 PST 2023


These are useful as a less verbose alternative to WARN_ONCE, which also
prints a stack trace if possible.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
v1 -> v2:
  unchanged
---
 include/linux/printk.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/include/linux/printk.h b/include/linux/printk.h
index 2fd6551e0ccb..a9d1b05f6f0f 100644
--- a/include/linux/printk.h
+++ b/include/linux/printk.h
@@ -94,6 +94,15 @@ static inline int dev_err_probe(struct device *dev, int err, const char *fmt,
 		(level) <= LOGLEVEL ? pr_print((level), (format), ##args) : 0; \
 	 })
 
+#define __pr_printk_once(level, format, args...) do {	\
+	static bool __print_once;			\
+							\
+	if (!__print_once && (level) <= LOGLEVEL) {	\
+		__print_once = true;			\
+		pr_print((level), (format), ##args);	\
+	}						\
+} while (0)
+
 #ifndef pr_fmt
 #define pr_fmt(fmt) fmt
 #endif
@@ -110,7 +119,18 @@ static inline int dev_err_probe(struct device *dev, int err, const char *fmt,
 #define pr_vdebug(fmt, arg...)	__pr_printk(8, pr_fmt(fmt), ##arg)
 #define pr_cont(fmt, arg...)	__pr_printk(-1, fmt, ##arg)
 
+#define pr_emerg_once(fmt, arg...)	__pr_printk_once(0, pr_fmt(fmt), ##arg)
+#define pr_alert_once(fmt, arg...)	__pr_printk_once(1, pr_fmt(fmt), ##arg)
+#define pr_crit_once(fmt, arg...)	__pr_printk_once(2, pr_fmt(fmt), ##arg)
+#define pr_err_once(fmt, arg...)	__pr_printk_once(3, pr_fmt(fmt), ##arg)
+#define pr_warning_once(fmt, arg...)	__pr_printk_once(4, pr_fmt(fmt), ##arg)
+#define pr_notice_once(fmt, arg...)	__pr_printk_once(5, pr_fmt(fmt), ##arg)
+#define pr_info_once(fmt, arg...)	__pr_printk_once(6, pr_fmt(fmt), ##arg)
+#define pr_debug_once(fmt, arg...)	__pr_printk_once(7, pr_fmt(fmt), ##arg)
+#define pr_vdebug_once(fmt, arg...)	__pr_printk_once(8, pr_fmt(fmt), ##arg)
+
 #define pr_warn			pr_warning
+#define pr_warn_once		pr_warning_once
 
 int memory_display(const void *addr, loff_t offs, unsigned nbytes, int size,
 		   int swab);
-- 
2.30.2




More information about the barebox mailing list