Skip to content

Commit 56a7b9f

Browse files
committed
ratelimit: Create functions to handle ratelimit_state internals
A number of ratelimit use cases do open-coded access to the ratelimit_state structure's ->missed field. This works, but is a bit messy and makes it more annoying to make changes to this field. Therefore, provide a ratelimit_state_inc_miss() function that increments the ->missed field, a ratelimit_state_get_miss() function that reads out the ->missed field, and a ratelimit_state_reset_miss() function that reads out that field, but that also resets its value to zero. These functions will replace client-code open-coded uses of ->missed. In addition, a new ratelimit_state_reset_interval() function encapsulates what was previously open-coded lock acquisition and direct field updates. [ paulmck: Apply kernel test robot feedback. ] Link: https://lore.kernel.org/all/fbe93a52-365e-47fe-93a4-44a44547d601@paulmck-laptop/ Link: https://lore.kernel.org/all/[email protected]/ Signed-off-by: Paul E. McKenney <[email protected]> Reviewed-by: Petr Mladek <[email protected]> Cc: Andrew Morton <[email protected]> Cc: Kuniyuki Iwashima <[email protected]> Cc: Mateusz Guzik <[email protected]> Cc: Steven Rostedt <[email protected]> Cc: John Ogness <[email protected]> Cc: Sergey Senozhatsky <[email protected]>
1 parent 0af2f6b commit 56a7b9f

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

include/linux/ratelimit.h

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,46 @@ static inline void ratelimit_default_init(struct ratelimit_state *rs)
2222
DEFAULT_RATELIMIT_BURST);
2323
}
2424

25+
static inline void ratelimit_state_inc_miss(struct ratelimit_state *rs)
26+
{
27+
rs->missed++;
28+
}
29+
30+
static inline int ratelimit_state_get_miss(struct ratelimit_state *rs)
31+
{
32+
return rs->missed;
33+
}
34+
35+
static inline int ratelimit_state_reset_miss(struct ratelimit_state *rs)
36+
{
37+
int ret = rs->missed;
38+
39+
rs->missed = 0;
40+
return ret;
41+
}
42+
43+
static inline void ratelimit_state_reset_interval(struct ratelimit_state *rs, int interval_init)
44+
{
45+
unsigned long flags;
46+
47+
raw_spin_lock_irqsave(&rs->lock, flags);
48+
rs->interval = interval_init;
49+
rs->begin = 0;
50+
rs->printed = 0;
51+
ratelimit_state_reset_miss(rs);
52+
raw_spin_unlock_irqrestore(&rs->lock, flags);
53+
}
54+
2555
static inline void ratelimit_state_exit(struct ratelimit_state *rs)
2656
{
57+
int m;
58+
2759
if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE))
2860
return;
2961

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-
}
62+
m = ratelimit_state_reset_miss(rs);
63+
if (m)
64+
pr_warn("%s: %d output lines suppressed due to ratelimiting\n", current->comm, m);
3565
}
3666

3767
static inline void

lib/ratelimit.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ int ___ratelimit(struct ratelimit_state *rs, const char *func)
5151
rs->begin = jiffies;
5252

5353
if (time_is_before_jiffies(rs->begin + interval)) {
54-
if (rs->missed) {
54+
int m = ratelimit_state_reset_miss(rs);
55+
56+
if (m) {
5557
if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) {
5658
printk_deferred(KERN_WARNING
57-
"%s: %d callbacks suppressed\n",
58-
func, rs->missed);
59-
rs->missed = 0;
59+
"%s: %d callbacks suppressed\n", func, m);
6060
}
6161
}
6262
rs->begin = jiffies;

0 commit comments

Comments
 (0)