1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_RATELIMIT_H
3 #define _LINUX_RATELIMIT_H
4 
5 #include <linux/param.h>
6 #include <linux/sched.h>
7 #include <linux/spinlock.h>
8 
9 #define DEFAULT_RATELIMIT_INTERVAL	(5 * HZ)
10 #define DEFAULT_RATELIMIT_BURST		10
11 
12 /* issue num suppressed message on exit */
13 #define RATELIMIT_MSG_ON_RELEASE	BIT(0)
14 
15 struct ratelimit_state {
16 	raw_spinlock_t	lock;		/* protect the state */
17 
18 	int		interval;
19 	int		burst;
20 	int		printed;
21 	int		missed;
22 	unsigned long	begin;
23 	unsigned long	flags;
24 };
25 
26 #define RATELIMIT_STATE_INIT_FLAGS(name, interval_init, burst_init, flags_init) { \
27 		.lock		= __RAW_SPIN_LOCK_UNLOCKED(name.lock),		  \
28 		.interval	= interval_init,				  \
29 		.burst		= burst_init,					  \
30 		.flags		= flags_init,					  \
31 	}
32 
33 #define RATELIMIT_STATE_INIT(name, interval_init, burst_init) \
34 	RATELIMIT_STATE_INIT_FLAGS(name, interval_init, burst_init, 0)
35 
36 #define RATELIMIT_STATE_INIT_DISABLED					\
37 	RATELIMIT_STATE_INIT(ratelimit_state, 0, DEFAULT_RATELIMIT_BURST)
38 
39 #define DEFINE_RATELIMIT_STATE(name, interval_init, burst_init)		\
40 									\
41 	struct ratelimit_state name =					\
42 		RATELIMIT_STATE_INIT(name, interval_init, burst_init)	\
43 
ratelimit_state_init(struct ratelimit_state * rs,int interval,int burst)44 static inline void ratelimit_state_init(struct ratelimit_state *rs,
45 					int interval, int burst)
46 {
47 	memset(rs, 0, sizeof(*rs));
48 
49 	raw_spin_lock_init(&rs->lock);
50 	rs->interval	= interval;
51 	rs->burst	= burst;
52 }
53 
ratelimit_default_init(struct ratelimit_state * rs)54 static inline void ratelimit_default_init(struct ratelimit_state *rs)
55 {
56 	return ratelimit_state_init(rs, DEFAULT_RATELIMIT_INTERVAL,
57 					DEFAULT_RATELIMIT_BURST);
58 }
59 
ratelimit_state_exit(struct ratelimit_state * rs)60 static inline void ratelimit_state_exit(struct ratelimit_state *rs)
61 {
62 	if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE))
63 		return;
64 
65 	if (rs->missed) {
66 		pr_warn("%s: %d output lines suppressed due to ratelimiting\n",
67 			current->comm, rs->missed);
68 		rs->missed = 0;
69 	}
70 }
71 
72 static inline void
ratelimit_set_flags(struct ratelimit_state * rs,unsigned long flags)73 ratelimit_set_flags(struct ratelimit_state *rs, unsigned long flags)
74 {
75 	rs->flags = flags;
76 }
77 
78 extern struct ratelimit_state printk_ratelimit_state;
79 
80 extern int ___ratelimit(struct ratelimit_state *rs, const char *func);
81 #define __ratelimit(state) ___ratelimit(state, __func__)
82 
83 #ifdef CONFIG_PRINTK
84 
85 #define WARN_ON_RATELIMIT(condition, state)	({		\
86 	bool __rtn_cond = !!(condition);			\
87 	WARN_ON(__rtn_cond && __ratelimit(state));		\
88 	__rtn_cond;						\
89 })
90 
91 #define WARN_RATELIMIT(condition, format, ...)			\
92 ({								\
93 	static DEFINE_RATELIMIT_STATE(_rs,			\
94 				      DEFAULT_RATELIMIT_INTERVAL,	\
95 				      DEFAULT_RATELIMIT_BURST);	\
96 	int rtn = !!(condition);				\
97 								\
98 	if (unlikely(rtn && __ratelimit(&_rs)))			\
99 		WARN(rtn, format, ##__VA_ARGS__);		\
100 								\
101 	rtn;							\
102 })
103 
104 #else
105 
106 #define WARN_ON_RATELIMIT(condition, state)			\
107 	WARN_ON(condition)
108 
109 #define WARN_RATELIMIT(condition, format, ...)			\
110 ({								\
111 	int rtn = WARN(condition, format, ##__VA_ARGS__);	\
112 	rtn;							\
113 })
114 
115 #endif
116 
117 #endif /* _LINUX_RATELIMIT_H */
118