blob: 57c9e0622a38dbd8a742015cd9742bcd52abf226 [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>
Borislav Petkov6b1d1742016-08-02 14:04:04 -07005#include <linux/sched.h>
OGAWA Hirofumif40c3962010-05-24 14:33:11 -07006#include <linux/spinlock.h>
Ingo Molnar979f6932009-09-22 14:44:11 +02007
8#define DEFAULT_RATELIMIT_INTERVAL (5 * HZ)
9#define DEFAULT_RATELIMIT_BURST 10
Dave Young717115e2008-07-25 01:45:58 -070010
Borislav Petkov6b1d1742016-08-02 14:04:04 -070011/* issue num suppressed message on exit */
12#define RATELIMIT_MSG_ON_RELEASE BIT(0)
13
Dave Young717115e2008-07-25 01:45:58 -070014struct ratelimit_state {
Thomas Gleixner07354eb2009-07-25 17:50:36 +020015 raw_spinlock_t lock; /* protect the state */
Ingo Molnar979f6932009-09-22 14:44:11 +020016
17 int interval;
18 int burst;
19 int printed;
20 int missed;
21 unsigned long begin;
Borislav Petkov6b1d1742016-08-02 14:04:04 -070022 unsigned long flags;
Dave Young717115e2008-07-25 01:45:58 -070023};
24
Dmitry Monakhov89e3f902014-12-12 16:57:57 -080025#define RATELIMIT_STATE_INIT(name, interval_init, burst_init) { \
Thomas Gleixner07354eb2009-07-25 17:50:36 +020026 .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \
Ingo Molnar979f6932009-09-22 14:44:11 +020027 .interval = interval_init, \
28 .burst = burst_init, \
29 }
Dave Young717115e2008-07-25 01:45:58 -070030
Dmitry Monakhov89e3f902014-12-12 16:57:57 -080031#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 Hirofumif40c3962010-05-24 14:33:11 -070039static inline void ratelimit_state_init(struct ratelimit_state *rs,
40 int interval, int burst)
41{
Borislav Petkov6b1d1742016-08-02 14:04:04 -070042 memset(rs, 0, sizeof(*rs));
43
Thomas Gleixner07354eb2009-07-25 17:50:36 +020044 raw_spin_lock_init(&rs->lock);
Borislav Petkov6b1d1742016-08-02 14:04:04 -070045 rs->interval = interval;
46 rs->burst = burst;
47}
48
49static 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
55static 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
67static inline void
68ratelimit_set_flags(struct ratelimit_state *rs, unsigned long flags)
69{
70 rs->flags = flags;
OGAWA Hirofumif40c3962010-05-24 14:33:11 -070071}
72
Namhyung Kimf5d87d82010-10-26 14:22:49 -070073extern struct ratelimit_state printk_ratelimit_state;
74
Christian Borntraeger5c828712009-10-23 14:58:11 +020075extern int ___ratelimit(struct ratelimit_state *rs, const char *func);
76#define __ratelimit(state) ___ratelimit(state, __func__)
Ingo Molnar979f6932009-09-22 14:44:11 +020077
David S. Miller86e4ca62011-05-26 15:00:31 -040078#ifdef CONFIG_PRINTK
79
80#define WARN_ON_RATELIMIT(condition, state) \
81 WARN_ON((condition) && __ratelimit(state))
82
Markus Trippelsdorf2351a6c2012-10-05 14:57:17 +020083#define WARN_RATELIMIT(condition, format, ...) \
David S. Miller86e4ca62011-05-26 15:00:31 -040084({ \
85 static DEFINE_RATELIMIT_STATE(_rs, \
86 DEFAULT_RATELIMIT_INTERVAL, \
87 DEFAULT_RATELIMIT_BURST); \
Markus Trippelsdorf2351a6c2012-10-05 14:57:17 +020088 int rtn = !!(condition); \
89 \
90 if (unlikely(rtn && __ratelimit(&_rs))) \
91 WARN(rtn, format, ##__VA_ARGS__); \
92 \
93 rtn; \
David S. Miller86e4ca62011-05-26 15:00:31 -040094})
95
96#else
97
98#define WARN_ON_RATELIMIT(condition, state) \
99 WARN_ON(condition)
100
Markus Trippelsdorf2351a6c2012-10-05 14:57:17 +0200101#define WARN_RATELIMIT(condition, format, ...) \
David S. Miller86e4ca62011-05-26 15:00:31 -0400102({ \
Markus Trippelsdorf2351a6c2012-10-05 14:57:17 +0200103 int rtn = WARN(condition, format, ##__VA_ARGS__); \
David S. Miller86e4ca62011-05-26 15:00:31 -0400104 rtn; \
105})
106
107#endif
108
Ingo Molnar979f6932009-09-22 14:44:11 +0200109#endif /* _LINUX_RATELIMIT_H */