1 #ifndef _LINUX_RATELIMIT_H
2 #define _LINUX_RATELIMIT_H
4 #include <linux/param.h>
5 #include <linux/spinlock.h>
7 #define DEFAULT_RATELIMIT_INTERVAL (5 * HZ)
8 #define DEFAULT_RATELIMIT_BURST 10
10 struct ratelimit_state {
11 raw_spinlock_t lock; /* protect the state */
20 #define RATELIMIT_STATE_INIT(name, interval_init, burst_init) { \
21 .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \
22 .interval = interval_init, \
23 .burst = burst_init, \
26 #define RATELIMIT_STATE_INIT_DISABLED \
27 RATELIMIT_STATE_INIT(ratelimit_state, 0, DEFAULT_RATELIMIT_BURST)
29 #define DEFINE_RATELIMIT_STATE(name, interval_init, burst_init) \
31 struct ratelimit_state name = \
32 RATELIMIT_STATE_INIT(name, interval_init, burst_init) \
34 static inline void ratelimit_state_init(struct ratelimit_state *rs,
35 int interval, int burst)
37 raw_spin_lock_init(&rs->lock);
38 rs->interval = interval;
45 extern struct ratelimit_state printk_ratelimit_state;
47 extern int ___ratelimit(struct ratelimit_state *rs, const char *func);
48 #define __ratelimit(state) ___ratelimit(state, __func__)
52 #define WARN_ON_RATELIMIT(condition, state) \
53 WARN_ON((condition) && __ratelimit(state))
55 #define WARN_RATELIMIT(condition, format, ...) \
57 static DEFINE_RATELIMIT_STATE(_rs, \
58 DEFAULT_RATELIMIT_INTERVAL, \
59 DEFAULT_RATELIMIT_BURST); \
60 int rtn = !!(condition); \
62 if (unlikely(rtn && __ratelimit(&_rs))) \
63 WARN(rtn, format, ##__VA_ARGS__); \
70 #define WARN_ON_RATELIMIT(condition, state) \
73 #define WARN_RATELIMIT(condition, format, ...) \
75 int rtn = WARN(condition, format, ##__VA_ARGS__); \
81 #endif /* _LINUX_RATELIMIT_H */