mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf.git
synced 2026-04-03 23:38:12 -04:00
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/20250423115409.3425-1-spasswolf@web.de/ Signed-off-by: Paul E. McKenney <paulmck@kernel.org> Reviewed-by: Petr Mladek <pmladek@suse.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Kuniyuki Iwashima <kuniyu@amazon.com> Cc: Mateusz Guzik <mjguzik@gmail.com> Cc: Steven Rostedt <rostedt@goodmis.org> Cc: John Ogness <john.ogness@linutronix.de> Cc: Sergey Senozhatsky <senozhatsky@chromium.org>
This commit is contained in:
@@ -22,16 +22,46 @@ static inline void ratelimit_default_init(struct ratelimit_state *rs)
|
||||
DEFAULT_RATELIMIT_BURST);
|
||||
}
|
||||
|
||||
static inline void ratelimit_state_inc_miss(struct ratelimit_state *rs)
|
||||
{
|
||||
rs->missed++;
|
||||
}
|
||||
|
||||
static inline int ratelimit_state_get_miss(struct ratelimit_state *rs)
|
||||
{
|
||||
return rs->missed;
|
||||
}
|
||||
|
||||
static inline int ratelimit_state_reset_miss(struct ratelimit_state *rs)
|
||||
{
|
||||
int ret = rs->missed;
|
||||
|
||||
rs->missed = 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline void ratelimit_state_reset_interval(struct ratelimit_state *rs, int interval_init)
|
||||
{
|
||||
unsigned long flags;
|
||||
|
||||
raw_spin_lock_irqsave(&rs->lock, flags);
|
||||
rs->interval = interval_init;
|
||||
rs->begin = 0;
|
||||
rs->printed = 0;
|
||||
ratelimit_state_reset_miss(rs);
|
||||
raw_spin_unlock_irqrestore(&rs->lock, flags);
|
||||
}
|
||||
|
||||
static inline void ratelimit_state_exit(struct ratelimit_state *rs)
|
||||
{
|
||||
int m;
|
||||
|
||||
if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE))
|
||||
return;
|
||||
|
||||
if (rs->missed) {
|
||||
pr_warn("%s: %d output lines suppressed due to ratelimiting\n",
|
||||
current->comm, rs->missed);
|
||||
rs->missed = 0;
|
||||
}
|
||||
m = ratelimit_state_reset_miss(rs);
|
||||
if (m)
|
||||
pr_warn("%s: %d output lines suppressed due to ratelimiting\n", current->comm, m);
|
||||
}
|
||||
|
||||
static inline void
|
||||
|
||||
@@ -51,12 +51,12 @@ int ___ratelimit(struct ratelimit_state *rs, const char *func)
|
||||
rs->begin = jiffies;
|
||||
|
||||
if (time_is_before_jiffies(rs->begin + interval)) {
|
||||
if (rs->missed) {
|
||||
int m = ratelimit_state_reset_miss(rs);
|
||||
|
||||
if (m) {
|
||||
if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) {
|
||||
printk_deferred(KERN_WARNING
|
||||
"%s: %d callbacks suppressed\n",
|
||||
func, rs->missed);
|
||||
rs->missed = 0;
|
||||
"%s: %d callbacks suppressed\n", func, m);
|
||||
}
|
||||
}
|
||||
rs->begin = jiffies;
|
||||
|
||||
Reference in New Issue
Block a user