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> |
Borislav Petkov | 6b1d174 | 2016-08-02 14:04:04 -0700 | [diff] [blame] | 5 | #include <linux/sched.h> |
OGAWA Hirofumi | f40c396 | 2010-05-24 14:33:11 -0700 | [diff] [blame] | 6 | #include <linux/spinlock.h> |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 7 | |
| 8 | #define DEFAULT_RATELIMIT_INTERVAL (5 * HZ) |
| 9 | #define DEFAULT_RATELIMIT_BURST 10 |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 10 | |
Borislav Petkov | 6b1d174 | 2016-08-02 14:04:04 -0700 | [diff] [blame] | 11 | /* issue num suppressed message on exit */ |
| 12 | #define RATELIMIT_MSG_ON_RELEASE BIT(0) |
| 13 | |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 14 | struct ratelimit_state { |
Thomas Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 15 | raw_spinlock_t lock; /* protect the state */ |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 16 | |
| 17 | int interval; |
| 18 | int burst; |
| 19 | int printed; |
| 20 | int missed; |
| 21 | unsigned long begin; |
Borislav Petkov | 6b1d174 | 2016-08-02 14:04:04 -0700 | [diff] [blame] | 22 | unsigned long flags; |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 23 | }; |
| 24 | |
Dmitry Monakhov | 89e3f90 | 2014-12-12 16:57:57 -0800 | [diff] [blame] | 25 | #define RATELIMIT_STATE_INIT(name, interval_init, burst_init) { \ |
Thomas Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 26 | .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \ |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 27 | .interval = interval_init, \ |
| 28 | .burst = burst_init, \ |
| 29 | } |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 30 | |
Dmitry Monakhov | 89e3f90 | 2014-12-12 16:57:57 -0800 | [diff] [blame] | 31 | #define RATELIMIT_STATE_INIT_DISABLED \ |
| 32 | RATELIMIT_STATE_INIT(ratelimit_state, 0, DEFAULT_RATELIMIT_BURST) |
| 33 | |
| 34 | #define DEFINE_RATELIMIT_STATE(name, interval_init, burst_init) \ |
| 35 | \ |
| 36 | struct ratelimit_state name = \ |
| 37 | RATELIMIT_STATE_INIT(name, interval_init, burst_init) \ |
| 38 | |
OGAWA Hirofumi | f40c396 | 2010-05-24 14:33:11 -0700 | [diff] [blame] | 39 | static inline void ratelimit_state_init(struct ratelimit_state *rs, |
| 40 | int interval, int burst) |
| 41 | { |
Borislav Petkov | 6b1d174 | 2016-08-02 14:04:04 -0700 | [diff] [blame] | 42 | memset(rs, 0, sizeof(*rs)); |
| 43 | |
Thomas Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 44 | raw_spin_lock_init(&rs->lock); |
Borislav Petkov | 6b1d174 | 2016-08-02 14:04:04 -0700 | [diff] [blame] | 45 | rs->interval = interval; |
| 46 | rs->burst = burst; |
| 47 | } |
| 48 | |
| 49 | static inline void ratelimit_default_init(struct ratelimit_state *rs) |
| 50 | { |
| 51 | return ratelimit_state_init(rs, DEFAULT_RATELIMIT_INTERVAL, |
| 52 | DEFAULT_RATELIMIT_BURST); |
| 53 | } |
| 54 | |
| 55 | static inline void ratelimit_state_exit(struct ratelimit_state *rs) |
| 56 | { |
| 57 | if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) |
| 58 | return; |
| 59 | |
| 60 | if (rs->missed) { |
| 61 | pr_warn("%s: %d output lines suppressed due to ratelimiting\n", |
| 62 | current->comm, rs->missed); |
| 63 | rs->missed = 0; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | static inline void |
| 68 | ratelimit_set_flags(struct ratelimit_state *rs, unsigned long flags) |
| 69 | { |
| 70 | rs->flags = flags; |
OGAWA Hirofumi | f40c396 | 2010-05-24 14:33:11 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Namhyung Kim | f5d87d8 | 2010-10-26 14:22:49 -0700 | [diff] [blame] | 73 | extern struct ratelimit_state printk_ratelimit_state; |
| 74 | |
Christian Borntraeger | 5c82871 | 2009-10-23 14:58:11 +0200 | [diff] [blame] | 75 | extern int ___ratelimit(struct ratelimit_state *rs, const char *func); |
| 76 | #define __ratelimit(state) ___ratelimit(state, __func__) |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 77 | |
David S. Miller | 86e4ca6 | 2011-05-26 15:00:31 -0400 | [diff] [blame] | 78 | #ifdef CONFIG_PRINTK |
| 79 | |
| 80 | #define WARN_ON_RATELIMIT(condition, state) \ |
| 81 | WARN_ON((condition) && __ratelimit(state)) |
| 82 | |
Markus Trippelsdorf | 2351a6c | 2012-10-05 14:57:17 +0200 | [diff] [blame] | 83 | #define WARN_RATELIMIT(condition, format, ...) \ |
David S. Miller | 86e4ca6 | 2011-05-26 15:00:31 -0400 | [diff] [blame] | 84 | ({ \ |
| 85 | static DEFINE_RATELIMIT_STATE(_rs, \ |
| 86 | DEFAULT_RATELIMIT_INTERVAL, \ |
| 87 | DEFAULT_RATELIMIT_BURST); \ |
Markus Trippelsdorf | 2351a6c | 2012-10-05 14:57:17 +0200 | [diff] [blame] | 88 | int rtn = !!(condition); \ |
| 89 | \ |
| 90 | if (unlikely(rtn && __ratelimit(&_rs))) \ |
| 91 | WARN(rtn, format, ##__VA_ARGS__); \ |
| 92 | \ |
| 93 | rtn; \ |
David S. Miller | 86e4ca6 | 2011-05-26 15:00:31 -0400 | [diff] [blame] | 94 | }) |
| 95 | |
| 96 | #else |
| 97 | |
| 98 | #define WARN_ON_RATELIMIT(condition, state) \ |
| 99 | WARN_ON(condition) |
| 100 | |
Markus Trippelsdorf | 2351a6c | 2012-10-05 14:57:17 +0200 | [diff] [blame] | 101 | #define WARN_RATELIMIT(condition, format, ...) \ |
David S. Miller | 86e4ca6 | 2011-05-26 15:00:31 -0400 | [diff] [blame] | 102 | ({ \ |
Markus Trippelsdorf | 2351a6c | 2012-10-05 14:57:17 +0200 | [diff] [blame] | 103 | int rtn = WARN(condition, format, ##__VA_ARGS__); \ |
David S. Miller | 86e4ca6 | 2011-05-26 15:00:31 -0400 | [diff] [blame] | 104 | rtn; \ |
| 105 | }) |
| 106 | |
| 107 | #endif |
| 108 | |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 109 | #endif /* _LINUX_RATELIMIT_H */ |