blob: 8ddf79e9207a9db19a91fd09081f8df3c18eea22 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Dave Young717115e2008-07-25 01:45:58 -07002#ifndef _LINUX_RATELIMIT_H
3#define _LINUX_RATELIMIT_H
Dave Young717115e2008-07-25 01:45:58 -07004
Ingo Molnar979f6932009-09-22 14:44:11 +02005#include <linux/param.h>
Borislav Petkov6b1d1742016-08-02 14:04:04 -07006#include <linux/sched.h>
OGAWA Hirofumif40c3962010-05-24 14:33:11 -07007#include <linux/spinlock.h>
Ingo Molnar979f6932009-09-22 14:44:11 +02008
9#define DEFAULT_RATELIMIT_INTERVAL (5 * HZ)
10#define DEFAULT_RATELIMIT_BURST 10
Dave Young717115e2008-07-25 01:45:58 -070011
Borislav Petkov6b1d1742016-08-02 14:04:04 -070012/* issue num suppressed message on exit */
13#define RATELIMIT_MSG_ON_RELEASE BIT(0)
14
Dave Young717115e2008-07-25 01:45:58 -070015struct ratelimit_state {
Thomas Gleixner07354eb2009-07-25 17:50:36 +020016 raw_spinlock_t lock; /* protect the state */
Ingo Molnar979f6932009-09-22 14:44:11 +020017
18 int interval;
19 int burst;
20 int printed;
21 int missed;
22 unsigned long begin;
Borislav Petkov6b1d1742016-08-02 14:04:04 -070023 unsigned long flags;
Dave Young717115e2008-07-25 01:45:58 -070024};
25
Dmitry Monakhov89e3f902014-12-12 16:57:57 -080026#define RATELIMIT_STATE_INIT(name, interval_init, burst_init) { \
Thomas Gleixner07354eb2009-07-25 17:50:36 +020027 .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \
Ingo Molnar979f6932009-09-22 14:44:11 +020028 .interval = interval_init, \
29 .burst = burst_init, \
30 }
Dave Young717115e2008-07-25 01:45:58 -070031
Dmitry Monakhov89e3f902014-12-12 16:57:57 -080032#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 Hirofumif40c3962010-05-24 14:33:11 -070040static inline void ratelimit_state_init(struct ratelimit_state *rs,
41 int interval, int burst)
42{
Borislav Petkov6b1d1742016-08-02 14:04:04 -070043 memset(rs, 0, sizeof(*rs));
44
Thomas Gleixner07354eb2009-07-25 17:50:36 +020045 raw_spin_lock_init(&rs->lock);
Borislav Petkov6b1d1742016-08-02 14:04:04 -070046 rs->interval = interval;
47 rs->burst = burst;
48}
49
50static 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
56static 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
68static inline void
69ratelimit_set_flags(struct ratelimit_state *rs, unsigned long flags)
70{
71 rs->flags = flags;
OGAWA Hirofumif40c3962010-05-24 14:33:11 -070072}
73
Namhyung Kimf5d87d82010-10-26 14:22:49 -070074extern struct ratelimit_state printk_ratelimit_state;
75
Christian Borntraeger5c828712009-10-23 14:58:11 +020076extern int ___ratelimit(struct ratelimit_state *rs, const char *func);
77#define __ratelimit(state) ___ratelimit(state, __func__)
Ingo Molnar979f6932009-09-22 14:44:11 +020078
David S. Miller86e4ca62011-05-26 15:00:31 -040079#ifdef CONFIG_PRINTK
80
Jiri Slaby1b011e22016-12-19 16:23:12 -080081#define WARN_ON_RATELIMIT(condition, state) ({ \
82 bool __rtn_cond = !!(condition); \
83 WARN_ON(__rtn_cond && __ratelimit(state)); \
84 __rtn_cond; \
85})
David S. Miller86e4ca62011-05-26 15:00:31 -040086
Markus Trippelsdorf2351a6c2012-10-05 14:57:17 +020087#define WARN_RATELIMIT(condition, format, ...) \
David S. Miller86e4ca62011-05-26 15:00:31 -040088({ \
89 static DEFINE_RATELIMIT_STATE(_rs, \
90 DEFAULT_RATELIMIT_INTERVAL, \
91 DEFAULT_RATELIMIT_BURST); \
Markus Trippelsdorf2351a6c2012-10-05 14:57:17 +020092 int rtn = !!(condition); \
93 \
94 if (unlikely(rtn && __ratelimit(&_rs))) \
95 WARN(rtn, format, ##__VA_ARGS__); \
96 \
97 rtn; \
David S. Miller86e4ca62011-05-26 15:00:31 -040098})
99
100#else
101
102#define WARN_ON_RATELIMIT(condition, state) \
103 WARN_ON(condition)
104
Markus Trippelsdorf2351a6c2012-10-05 14:57:17 +0200105#define WARN_RATELIMIT(condition, format, ...) \
David S. Miller86e4ca62011-05-26 15:00:31 -0400106({ \
Markus Trippelsdorf2351a6c2012-10-05 14:57:17 +0200107 int rtn = WARN(condition, format, ##__VA_ARGS__); \
David S. Miller86e4ca62011-05-26 15:00:31 -0400108 rtn; \
109})
110
111#endif
112
Ingo Molnar979f6932009-09-22 14:44:11 +0200113#endif /* _LINUX_RATELIMIT_H */