blob: 18102529254eeb8fe9fe112c9b455ca7c4e8650f [file] [log] [blame]
Dave Young717115e2008-07-25 01:45:58 -07001#ifndef _LINUX_RATELIMIT_H
2#define _LINUX_RATELIMIT_H
Dave Young717115e2008-07-25 01:45:58 -07003
Ingo Molnar979f6932009-09-22 14:44:11 +02004#include <linux/param.h>
OGAWA Hirofumif40c3962010-05-24 14:33:11 -07005#include <linux/spinlock.h>
Ingo Molnar979f6932009-09-22 14:44:11 +02006
7#define DEFAULT_RATELIMIT_INTERVAL (5 * HZ)
8#define DEFAULT_RATELIMIT_BURST 10
Dave Young717115e2008-07-25 01:45:58 -07009
10struct ratelimit_state {
Thomas Gleixner07354eb2009-07-25 17:50:36 +020011 raw_spinlock_t lock; /* protect the state */
Ingo Molnar979f6932009-09-22 14:44:11 +020012
13 int interval;
14 int burst;
15 int printed;
16 int missed;
17 unsigned long begin;
Dave Young717115e2008-07-25 01:45:58 -070018};
19
Dmitry Monakhov89e3f902014-12-12 16:57:57 -080020#define RATELIMIT_STATE_INIT(name, interval_init, burst_init) { \
Thomas Gleixner07354eb2009-07-25 17:50:36 +020021 .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \
Ingo Molnar979f6932009-09-22 14:44:11 +020022 .interval = interval_init, \
23 .burst = burst_init, \
24 }
Dave Young717115e2008-07-25 01:45:58 -070025
Dmitry Monakhov89e3f902014-12-12 16:57:57 -080026#define RATELIMIT_STATE_INIT_DISABLED \
27 RATELIMIT_STATE_INIT(ratelimit_state, 0, DEFAULT_RATELIMIT_BURST)
28
29#define DEFINE_RATELIMIT_STATE(name, interval_init, burst_init) \
30 \
31 struct ratelimit_state name = \
32 RATELIMIT_STATE_INIT(name, interval_init, burst_init) \
33
OGAWA Hirofumif40c3962010-05-24 14:33:11 -070034static inline void ratelimit_state_init(struct ratelimit_state *rs,
35 int interval, int burst)
36{
Thomas Gleixner07354eb2009-07-25 17:50:36 +020037 raw_spin_lock_init(&rs->lock);
OGAWA Hirofumif40c3962010-05-24 14:33:11 -070038 rs->interval = interval;
39 rs->burst = burst;
40 rs->printed = 0;
41 rs->missed = 0;
42 rs->begin = 0;
43}
44
Namhyung Kimf5d87d82010-10-26 14:22:49 -070045extern struct ratelimit_state printk_ratelimit_state;
46
Christian Borntraeger5c828712009-10-23 14:58:11 +020047extern int ___ratelimit(struct ratelimit_state *rs, const char *func);
48#define __ratelimit(state) ___ratelimit(state, __func__)
Ingo Molnar979f6932009-09-22 14:44:11 +020049
David S. Miller86e4ca62011-05-26 15:00:31 -040050#ifdef CONFIG_PRINTK
51
52#define WARN_ON_RATELIMIT(condition, state) \
53 WARN_ON((condition) && __ratelimit(state))
54
Markus Trippelsdorf2351a6c2012-10-05 14:57:17 +020055#define WARN_RATELIMIT(condition, format, ...) \
David S. Miller86e4ca62011-05-26 15:00:31 -040056({ \
57 static DEFINE_RATELIMIT_STATE(_rs, \
58 DEFAULT_RATELIMIT_INTERVAL, \
59 DEFAULT_RATELIMIT_BURST); \
Markus Trippelsdorf2351a6c2012-10-05 14:57:17 +020060 int rtn = !!(condition); \
61 \
62 if (unlikely(rtn && __ratelimit(&_rs))) \
63 WARN(rtn, format, ##__VA_ARGS__); \
64 \
65 rtn; \
David S. Miller86e4ca62011-05-26 15:00:31 -040066})
67
68#else
69
70#define WARN_ON_RATELIMIT(condition, state) \
71 WARN_ON(condition)
72
Markus Trippelsdorf2351a6c2012-10-05 14:57:17 +020073#define WARN_RATELIMIT(condition, format, ...) \
David S. Miller86e4ca62011-05-26 15:00:31 -040074({ \
Markus Trippelsdorf2351a6c2012-10-05 14:57:17 +020075 int rtn = WARN(condition, format, ##__VA_ARGS__); \
David S. Miller86e4ca62011-05-26 15:00:31 -040076 rtn; \
77})
78
79#endif
80
Ingo Molnar979f6932009-09-22 14:44:11 +020081#endif /* _LINUX_RATELIMIT_H */