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