1 #ifndef _LINUX_RATELIMIT_H
2 #define _LINUX_RATELIMIT_H
4 #include <linux/param.h>
5 #include <linux/sched.h>
6 #include <linux/spinlock.h>
8 #define DEFAULT_RATELIMIT_INTERVAL (5 * HZ)
9 #define DEFAULT_RATELIMIT_BURST 10
11 /* issue num suppressed message on exit */
12 #define RATELIMIT_MSG_ON_RELEASE BIT(0)
14 struct ratelimit_state
{
15 raw_spinlock_t lock
; /* protect the state */
25 #define RATELIMIT_STATE_INIT(name, interval_init, burst_init) { \
26 .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \
27 .interval = interval_init, \
28 .burst = burst_init, \
31 #define RATELIMIT_STATE_INIT_DISABLED \
32 RATELIMIT_STATE_INIT(ratelimit_state, 0, DEFAULT_RATELIMIT_BURST)
34 #define DEFINE_RATELIMIT_STATE(name, interval_init, burst_init) \
36 struct ratelimit_state name = \
37 RATELIMIT_STATE_INIT(name, interval_init, burst_init) \
39 static inline void ratelimit_state_init(struct ratelimit_state *rs,
40 int interval
, int burst
)
42 memset(rs
, 0, sizeof(*rs
));
44 raw_spin_lock_init(&rs
->lock
);
45 rs
->interval
= interval
;
49 static inline void ratelimit_default_init(struct ratelimit_state
*rs
)
51 return ratelimit_state_init(rs
, DEFAULT_RATELIMIT_INTERVAL
,
52 DEFAULT_RATELIMIT_BURST
);
55 static inline void ratelimit_state_exit(struct ratelimit_state
*rs
)
57 if (!(rs
->flags
& RATELIMIT_MSG_ON_RELEASE
))
61 pr_warn("%s: %d output lines suppressed due to ratelimiting\n",
62 current
->comm
, rs
->missed
);
68 ratelimit_set_flags(struct ratelimit_state
*rs
, unsigned long flags
)
73 extern struct ratelimit_state printk_ratelimit_state
;
75 extern int ___ratelimit(struct ratelimit_state
*rs
, const char *func
);
76 #define __ratelimit(state) ___ratelimit(state, __func__)
80 #define WARN_ON_RATELIMIT(condition, state) \
81 WARN_ON((condition) && __ratelimit(state))
83 #define WARN_RATELIMIT(condition, format, ...) \
85 static DEFINE_RATELIMIT_STATE(_rs, \
86 DEFAULT_RATELIMIT_INTERVAL, \
87 DEFAULT_RATELIMIT_BURST); \
88 int rtn = !!(condition); \
90 if (unlikely(rtn && __ratelimit(&_rs))) \
91 WARN(rtn, format, ##__VA_ARGS__); \
98 #define WARN_ON_RATELIMIT(condition, state) \
101 #define WARN_RATELIMIT(condition, format, ...) \
103 int rtn = WARN(condition, format, ##__VA_ARGS__); \
109 #endif /* _LINUX_RATELIMIT_H */