From 119138701222d9e999900e11f30b1d0816fc5dd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Attila=20F=C3=BCl=C3=B6p?= Date: Tue, 7 Mar 2023 22:44:11 +0100 Subject: [PATCH] spl: Add cmn_err_once() to log a message only on the first call MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reviewed-by: Richard Yao Reviewed-by: Brian Behlendorf Reviewed-by: Brian Atkinson Signed-off-by: Attila Fülöp Closes #14567 --- include/os/freebsd/spl/sys/cmn_err.h | 33 +++++++++++++++++++++++++++ include/os/linux/spl/sys/cmn_err.h | 17 ++++++++++++++ lib/libspl/include/sys/cmn_err.h | 34 ++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) diff --git a/include/os/freebsd/spl/sys/cmn_err.h b/include/os/freebsd/spl/sys/cmn_err.h index a8f9a8824..dd3da7da2 100644 --- a/include/os/freebsd/spl/sys/cmn_err.h +++ b/include/os/freebsd/spl/sys/cmn_err.h @@ -33,6 +33,7 @@ #if !defined(_ASM) #include +#include #endif #ifdef __cplusplus @@ -73,6 +74,38 @@ extern void vuprintf(const char *, __va_list) extern void panic(const char *, ...) __attribute__((format(printf, 1, 2), __noreturn__)); +#define cmn_err_once(ce, ...) \ +{ \ + static volatile uint32_t printed = 0; \ + if (atomic_cas_32(&printed, 0, 1) == 0) { \ + cmn_err(ce, __VA_ARGS__); \ + } \ +} + +#define vcmn_err_once(ce, fmt, ap) \ +{ \ + static volatile uint32_t printed = 0; \ + if (atomic_cas_32(&printed, 0, 1) == 0) { \ + vcmn_err(ce, fmt, ap); \ + } \ +} + +#define zcmn_err_once(zone, ce, ...) \ +{ \ + static volatile uint32_t printed = 0; \ + if (atomic_cas_32(&printed, 0, 1) == 0) { \ + zcmn_err(zone, ce, __VA_ARGS__); \ + } \ +} + +#define vzcmn_err_once(zone, ce, fmt, ap) \ +{ \ + static volatile uint32_t printed = 0; \ + if (atomic_cas_32(&printed, 0, 1) == 0) { \ + vzcmn_err(zone, ce, fmt, ap); \ + } \ +} + #endif /* !_ASM */ #ifdef __cplusplus diff --git a/include/os/linux/spl/sys/cmn_err.h b/include/os/linux/spl/sys/cmn_err.h index 161bcf9b3..7ca2a86be 100644 --- a/include/os/linux/spl/sys/cmn_err.h +++ b/include/os/linux/spl/sys/cmn_err.h @@ -29,6 +29,7 @@ #else #include #endif +#include #define CE_CONT 0 /* continuation */ #define CE_NOTE 1 /* notice */ @@ -45,4 +46,20 @@ extern void vpanic(const char *, va_list) #define fm_panic panic +#define cmn_err_once(ce, ...) \ +{ \ + static volatile uint32_t printed = 0; \ + if (atomic_cas_32(&printed, 0, 1) == 0) { \ + cmn_err(ce, __VA_ARGS__); \ + } \ +} + +#define vcmn_err_once(ce, fmt, ap) \ +{ \ + static volatile uint32_t printed = 0; \ + if (atomic_cas_32(&printed, 0, 1) == 0) { \ + vcmn_err(ce, fmt, ap); \ + } \ +} + #endif /* SPL_CMN_ERR_H */ diff --git a/lib/libspl/include/sys/cmn_err.h b/lib/libspl/include/sys/cmn_err.h index f4db082c2..6c71dcb8e 100644 --- a/lib/libspl/include/sys/cmn_err.h +++ b/lib/libspl/include/sys/cmn_err.h @@ -27,4 +27,38 @@ #ifndef _LIBSPL_SYS_CMN_ERR_H #define _LIBSPL_SYS_CMN_ERR_H +#include + +#define cmn_err_once(ce, ...) \ +{ \ + static volatile uint32_t printed = 0; \ + if (atomic_cas_32(&printed, 0, 1) == 0) { \ + cmn_err(ce, __VA_ARGS__); \ + } \ +} + +#define vcmn_err_once(ce, fmt, ap) \ +{ \ + static volatile uint32_t printed = 0; \ + if (atomic_cas_32(&printed, 0, 1) == 0) { \ + vcmn_err(ce, fmt, ap); \ + } \ +} + +#define zcmn_err_once(zone, ce, ...) \ +{ \ + static volatile uint32_t printed = 0; \ + if (atomic_cas_32(&printed, 0, 1) == 0) { \ + zcmn_err(zone, ce, __VA_ARGS__); \ + } \ +} + +#define vzcmn_err_once(zone, ce, fmt, ap) \ +{ \ + static volatile uint32_t printed = 0; \ + if (atomic_cas_32(&printed, 0, 1) == 0) { \ + vzcmn_err(zone, ce, fmt, ap); \ + } \ +} + #endif