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 | |
Herbert Xu | b4a461e | 2020-07-21 16:22:48 +1000 | [diff] [blame] | 5 | #include <linux/ratelimit_types.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 | |
OGAWA Hirofumi | f40c396 | 2010-05-24 14:33:11 -0700 | [diff] [blame] | 9 | static inline void ratelimit_state_init(struct ratelimit_state *rs, |
| 10 | int interval, int burst) |
| 11 | { |
Borislav Petkov | 6b1d174 | 2016-08-02 14:04:04 -0700 | [diff] [blame] | 12 | memset(rs, 0, sizeof(*rs)); |
| 13 | |
Thomas Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 14 | raw_spin_lock_init(&rs->lock); |
Borislav Petkov | 6b1d174 | 2016-08-02 14:04:04 -0700 | [diff] [blame] | 15 | rs->interval = interval; |
| 16 | rs->burst = burst; |
| 17 | } |
| 18 | |
| 19 | static inline void ratelimit_default_init(struct ratelimit_state *rs) |
| 20 | { |
| 21 | return ratelimit_state_init(rs, DEFAULT_RATELIMIT_INTERVAL, |
| 22 | DEFAULT_RATELIMIT_BURST); |
| 23 | } |
| 24 | |
| 25 | static inline void ratelimit_state_exit(struct ratelimit_state *rs) |
| 26 | { |
| 27 | if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) |
| 28 | return; |
| 29 | |
| 30 | if (rs->missed) { |
| 31 | pr_warn("%s: %d output lines suppressed due to ratelimiting\n", |
| 32 | current->comm, rs->missed); |
| 33 | rs->missed = 0; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | static inline void |
| 38 | ratelimit_set_flags(struct ratelimit_state *rs, unsigned long flags) |
| 39 | { |
| 40 | rs->flags = flags; |
OGAWA Hirofumi | f40c396 | 2010-05-24 14:33:11 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Namhyung Kim | f5d87d8 | 2010-10-26 14:22:49 -0700 | [diff] [blame] | 43 | extern struct ratelimit_state printk_ratelimit_state; |
| 44 | |
David S. Miller | 86e4ca6 | 2011-05-26 15:00:31 -0400 | [diff] [blame] | 45 | #ifdef CONFIG_PRINTK |
| 46 | |
Jiri Slaby | 1b011e2 | 2016-12-19 16:23:12 -0800 | [diff] [blame] | 47 | #define WARN_ON_RATELIMIT(condition, state) ({ \ |
| 48 | bool __rtn_cond = !!(condition); \ |
| 49 | WARN_ON(__rtn_cond && __ratelimit(state)); \ |
| 50 | __rtn_cond; \ |
| 51 | }) |
David S. Miller | 86e4ca6 | 2011-05-26 15:00:31 -0400 | [diff] [blame] | 52 | |
Markus Trippelsdorf | 2351a6c | 2012-10-05 14:57:17 +0200 | [diff] [blame] | 53 | #define WARN_RATELIMIT(condition, format, ...) \ |
David S. Miller | 86e4ca6 | 2011-05-26 15:00:31 -0400 | [diff] [blame] | 54 | ({ \ |
| 55 | static DEFINE_RATELIMIT_STATE(_rs, \ |
| 56 | DEFAULT_RATELIMIT_INTERVAL, \ |
| 57 | DEFAULT_RATELIMIT_BURST); \ |
Markus Trippelsdorf | 2351a6c | 2012-10-05 14:57:17 +0200 | [diff] [blame] | 58 | int rtn = !!(condition); \ |
| 59 | \ |
| 60 | if (unlikely(rtn && __ratelimit(&_rs))) \ |
| 61 | WARN(rtn, format, ##__VA_ARGS__); \ |
| 62 | \ |
| 63 | rtn; \ |
David S. Miller | 86e4ca6 | 2011-05-26 15:00:31 -0400 | [diff] [blame] | 64 | }) |
| 65 | |
| 66 | #else |
| 67 | |
| 68 | #define WARN_ON_RATELIMIT(condition, state) \ |
| 69 | WARN_ON(condition) |
| 70 | |
Markus Trippelsdorf | 2351a6c | 2012-10-05 14:57:17 +0200 | [diff] [blame] | 71 | #define WARN_RATELIMIT(condition, format, ...) \ |
David S. Miller | 86e4ca6 | 2011-05-26 15:00:31 -0400 | [diff] [blame] | 72 | ({ \ |
Markus Trippelsdorf | 2351a6c | 2012-10-05 14:57:17 +0200 | [diff] [blame] | 73 | int rtn = WARN(condition, format, ##__VA_ARGS__); \ |
David S. Miller | 86e4ca6 | 2011-05-26 15:00:31 -0400 | [diff] [blame] | 74 | rtn; \ |
| 75 | }) |
| 76 | |
| 77 | #endif |
| 78 | |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 79 | #endif /* _LINUX_RATELIMIT_H */ |