Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 1 | #ifndef _LINUX_RATELIMIT_H |
| 2 | #define _LINUX_RATELIMIT_H |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 3 | |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 4 | #include <linux/param.h> |
OGAWA Hirofumi | f40c396 | 2010-05-24 14:33:11 -0700 | [diff] [blame] | 5 | #include <linux/spinlock.h> |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 6 | |
| 7 | #define DEFAULT_RATELIMIT_INTERVAL (5 * HZ) |
| 8 | #define DEFAULT_RATELIMIT_BURST 10 |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 9 | |
| 10 | struct ratelimit_state { |
Thomas Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 11 | raw_spinlock_t lock; /* protect the state */ |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 12 | |
| 13 | int interval; |
| 14 | int burst; |
| 15 | int printed; |
| 16 | int missed; |
| 17 | unsigned long begin; |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 18 | }; |
| 19 | |
Dmitry Monakhov | 89e3f90 | 2014-12-12 16:57:57 -0800 | [diff] [blame] | 20 | #define RATELIMIT_STATE_INIT(name, interval_init, burst_init) { \ |
Thomas Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 21 | .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \ |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 22 | .interval = interval_init, \ |
| 23 | .burst = burst_init, \ |
| 24 | } |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 25 | |
Dmitry Monakhov | 89e3f90 | 2014-12-12 16:57:57 -0800 | [diff] [blame] | 26 | #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 Hirofumi | f40c396 | 2010-05-24 14:33:11 -0700 | [diff] [blame] | 34 | static inline void ratelimit_state_init(struct ratelimit_state *rs, |
| 35 | int interval, int burst) |
| 36 | { |
Thomas Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 37 | raw_spin_lock_init(&rs->lock); |
OGAWA Hirofumi | f40c396 | 2010-05-24 14:33:11 -0700 | [diff] [blame] | 38 | rs->interval = interval; |
| 39 | rs->burst = burst; |
| 40 | rs->printed = 0; |
| 41 | rs->missed = 0; |
| 42 | rs->begin = 0; |
| 43 | } |
| 44 | |
Namhyung Kim | f5d87d8 | 2010-10-26 14:22:49 -0700 | [diff] [blame] | 45 | extern struct ratelimit_state printk_ratelimit_state; |
| 46 | |
Christian Borntraeger | 5c82871 | 2009-10-23 14:58:11 +0200 | [diff] [blame] | 47 | extern int ___ratelimit(struct ratelimit_state *rs, const char *func); |
| 48 | #define __ratelimit(state) ___ratelimit(state, __func__) |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 49 | |
David S. Miller | 86e4ca6 | 2011-05-26 15:00:31 -0400 | [diff] [blame] | 50 | #ifdef CONFIG_PRINTK |
| 51 | |
| 52 | #define WARN_ON_RATELIMIT(condition, state) \ |
| 53 | WARN_ON((condition) && __ratelimit(state)) |
| 54 | |
Markus Trippelsdorf | 2351a6c | 2012-10-05 14:57:17 +0200 | [diff] [blame] | 55 | #define WARN_RATELIMIT(condition, format, ...) \ |
David S. Miller | 86e4ca6 | 2011-05-26 15:00:31 -0400 | [diff] [blame] | 56 | ({ \ |
| 57 | static DEFINE_RATELIMIT_STATE(_rs, \ |
| 58 | DEFAULT_RATELIMIT_INTERVAL, \ |
| 59 | DEFAULT_RATELIMIT_BURST); \ |
Markus Trippelsdorf | 2351a6c | 2012-10-05 14:57:17 +0200 | [diff] [blame] | 60 | int rtn = !!(condition); \ |
| 61 | \ |
| 62 | if (unlikely(rtn && __ratelimit(&_rs))) \ |
| 63 | WARN(rtn, format, ##__VA_ARGS__); \ |
| 64 | \ |
| 65 | rtn; \ |
David S. Miller | 86e4ca6 | 2011-05-26 15:00:31 -0400 | [diff] [blame] | 66 | }) |
| 67 | |
| 68 | #else |
| 69 | |
| 70 | #define WARN_ON_RATELIMIT(condition, state) \ |
| 71 | WARN_ON(condition) |
| 72 | |
Markus Trippelsdorf | 2351a6c | 2012-10-05 14:57:17 +0200 | [diff] [blame] | 73 | #define WARN_RATELIMIT(condition, format, ...) \ |
David S. Miller | 86e4ca6 | 2011-05-26 15:00:31 -0400 | [diff] [blame] | 74 | ({ \ |
Markus Trippelsdorf | 2351a6c | 2012-10-05 14:57:17 +0200 | [diff] [blame] | 75 | int rtn = WARN(condition, format, ##__VA_ARGS__); \ |
David S. Miller | 86e4ca6 | 2011-05-26 15:00:31 -0400 | [diff] [blame] | 76 | rtn; \ |
| 77 | }) |
| 78 | |
| 79 | #endif |
| 80 | |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 81 | #endif /* _LINUX_RATELIMIT_H */ |