blob: b17e0cd0a30cfb9fc15c23bad511ef82007f7414 [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
Herbert Xub4a461e2020-07-21 16:22:48 +10005#include <linux/ratelimit_types.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
OGAWA Hirofumif40c3962010-05-24 14:33:11 -07009static inline void ratelimit_state_init(struct ratelimit_state *rs,
10 int interval, int burst)
11{
Borislav Petkov6b1d1742016-08-02 14:04:04 -070012 memset(rs, 0, sizeof(*rs));
13
Thomas Gleixner07354eb2009-07-25 17:50:36 +020014 raw_spin_lock_init(&rs->lock);
Borislav Petkov6b1d1742016-08-02 14:04:04 -070015 rs->interval = interval;
16 rs->burst = burst;
17}
18
19static 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
25static 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
37static inline void
38ratelimit_set_flags(struct ratelimit_state *rs, unsigned long flags)
39{
40 rs->flags = flags;
OGAWA Hirofumif40c3962010-05-24 14:33:11 -070041}
42
Namhyung Kimf5d87d82010-10-26 14:22:49 -070043extern struct ratelimit_state printk_ratelimit_state;
44
David S. Miller86e4ca62011-05-26 15:00:31 -040045#ifdef CONFIG_PRINTK
46
Jiri Slaby1b011e22016-12-19 16:23:12 -080047#define WARN_ON_RATELIMIT(condition, state) ({ \
48 bool __rtn_cond = !!(condition); \
49 WARN_ON(__rtn_cond && __ratelimit(state)); \
50 __rtn_cond; \
51})
David S. Miller86e4ca62011-05-26 15:00:31 -040052
Markus Trippelsdorf2351a6c2012-10-05 14:57:17 +020053#define WARN_RATELIMIT(condition, format, ...) \
David S. Miller86e4ca62011-05-26 15:00:31 -040054({ \
55 static DEFINE_RATELIMIT_STATE(_rs, \
56 DEFAULT_RATELIMIT_INTERVAL, \
57 DEFAULT_RATELIMIT_BURST); \
Markus Trippelsdorf2351a6c2012-10-05 14:57:17 +020058 int rtn = !!(condition); \
59 \
60 if (unlikely(rtn && __ratelimit(&_rs))) \
61 WARN(rtn, format, ##__VA_ARGS__); \
62 \
63 rtn; \
David S. Miller86e4ca62011-05-26 15:00:31 -040064})
65
66#else
67
68#define WARN_ON_RATELIMIT(condition, state) \
69 WARN_ON(condition)
70
Markus Trippelsdorf2351a6c2012-10-05 14:57:17 +020071#define WARN_RATELIMIT(condition, format, ...) \
David S. Miller86e4ca62011-05-26 15:00:31 -040072({ \
Markus Trippelsdorf2351a6c2012-10-05 14:57:17 +020073 int rtn = WARN(condition, format, ##__VA_ARGS__); \
David S. Miller86e4ca62011-05-26 15:00:31 -040074 rtn; \
75})
76
77#endif
78
Ingo Molnar979f6932009-09-22 14:44:11 +020079#endif /* _LINUX_RATELIMIT_H */