diff --git a/sys/compat/linuxkpi/common/include/linux/device.h b/sys/compat/linuxkpi/common/include/linux/device.h index 3b4f4c618f29..fbd8834629ae 100644 --- a/sys/compat/linuxkpi/common/include/linux/device.h +++ b/sys/compat/linuxkpi/common/include/linux/device.h @@ -144,6 +144,18 @@ show_class_attr_string(struct class *class, #define dev_printk(lvl, dev, fmt, ...) \ device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) +#define dev_err_ratelimited(dev, ...) do { \ + static time_t __ratelimited; \ + if (linux_ratelimited(&__ratelimited)) \ + dev_err(dev, __VA_ARGS__); \ +} while (0) + +#define dev_warn_ratelimited(dev, ...) do { \ + static time_t __ratelimited; \ + if (linux_ratelimited(&__ratelimited)) \ + dev_warn(dev, __VA_ARGS__); \ +} while (0) + static inline void * dev_get_drvdata(const struct device *dev) { diff --git a/sys/compat/linuxkpi/common/include/linux/kernel.h b/sys/compat/linuxkpi/common/include/linux/kernel.h index fabdba5df517..43a4dbd928b8 100644 --- a/sys/compat/linuxkpi/common/include/linux/kernel.h +++ b/sys/compat/linuxkpi/common/include/linux/kernel.h @@ -224,6 +224,11 @@ scnprintf(char *buf, size_t size, const char *fmt, ...) log_once(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__) #define pr_cont(fmt, ...) \ printk(KERN_CONT fmt, ##__VA_ARGS__) +#define pr_warn_ratelimited(...) do { \ + static time_t __ratelimited; \ + if (linux_ratelimited(&__ratelimited)) \ + pr_warning(__VA_ARGS__); \ +} while (0) #ifndef WARN #define WARN(condition, ...) ({ \ @@ -331,4 +336,6 @@ abs64(int64_t x) return (x < 0 ? -x : x); } +extern bool linux_ratelimited(time_t *); + #endif /* _LINUX_KERNEL_H_ */ diff --git a/sys/compat/linuxkpi/common/src/linux_compat.c b/sys/compat/linuxkpi/common/src/linux_compat.c index 37f607cf8861..62682f087665 100644 --- a/sys/compat/linuxkpi/common/src/linux_compat.c +++ b/sys/compat/linuxkpi/common/src/linux_compat.c @@ -1484,6 +1484,20 @@ __unregister_chrdev(unsigned int major, unsigned int baseminor, bool linux_cpu_has_clflush; #endif +bool +linux_ratelimited(time_t *ptime) +{ + /* make sure uptime is not zero by OR'ing bit 31 */ + time_t curr = time_uptime | (1U << 31); + + /* check if one or more seconds have passed */ + if (*ptime != curr) { + *ptime = curr; + return (1); + } + return (0); +} + static void linux_compat_init(void *arg) {