2019-06-01 08:08:42 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2008-04-29 07:59:43 +00:00
|
|
|
/*
|
|
|
|
|
* ratelimit.c - Do something with rate limit.
|
|
|
|
|
*
|
|
|
|
|
* Isolated from kernel/printk.c by Dave Young <hidave.darkstar@gmail.com>
|
|
|
|
|
*
|
2008-07-25 08:45:58 +00:00
|
|
|
* 2008-05-01 rewrite the function and use a ratelimit_state data struct as
|
|
|
|
|
* parameter. Now every user can use their own standalone ratelimit_state.
|
2008-04-29 07:59:43 +00:00
|
|
|
*/
|
|
|
|
|
|
2009-09-22 14:18:09 +00:00
|
|
|
#include <linux/ratelimit.h>
|
2008-04-29 07:59:43 +00:00
|
|
|
#include <linux/jiffies.h>
|
2011-11-17 02:29:17 +00:00
|
|
|
#include <linux/export.h>
|
2008-04-29 07:59:43 +00:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* __ratelimit - rate limiting
|
2008-07-25 08:45:58 +00:00
|
|
|
* @rs: ratelimit_state data
|
2010-04-06 21:35:01 +00:00
|
|
|
* @func: name of calling function
|
2008-04-29 07:59:43 +00:00
|
|
|
*
|
2010-04-06 21:35:01 +00:00
|
|
|
* This enforces a rate limit: not more than @rs->burst callbacks
|
|
|
|
|
* in every @rs->interval
|
|
|
|
|
*
|
|
|
|
|
* RETURNS:
|
|
|
|
|
* 0 means callbacks will be suppressed.
|
|
|
|
|
* 1 means go ahead and do it.
|
2008-04-29 07:59:43 +00:00
|
|
|
*/
|
2009-10-23 12:58:11 +00:00
|
|
|
int ___ratelimit(struct ratelimit_state *rs, const char *func)
|
2008-04-29 07:59:43 +00:00
|
|
|
{
|
2022-08-23 17:46:48 +00:00
|
|
|
/* Paired with WRITE_ONCE() in .proc_handler().
|
|
|
|
|
* Changing two values seperately could be inconsistent
|
|
|
|
|
* and some message could be lost. (See: net_ratelimit_state).
|
|
|
|
|
*/
|
|
|
|
|
int interval = READ_ONCE(rs->interval);
|
|
|
|
|
int burst = READ_ONCE(rs->burst);
|
2008-07-28 22:46:21 +00:00
|
|
|
unsigned long flags;
|
2025-04-24 19:00:10 +00:00
|
|
|
int ret = 0;
|
2008-07-28 22:46:21 +00:00
|
|
|
|
2025-04-23 19:03:59 +00:00
|
|
|
/*
|
|
|
|
|
* Zero interval says never limit, otherwise, non-positive burst
|
|
|
|
|
* says always limit.
|
|
|
|
|
*/
|
2025-04-09 23:58:53 +00:00
|
|
|
if (interval <= 0 || burst <= 0) {
|
2025-04-24 18:19:13 +00:00
|
|
|
WARN_ONCE(interval < 0 || burst < 0, "Negative interval (%d) or burst (%d): Uninitialized ratelimit_state structure?\n", interval, burst);
|
2025-04-09 23:58:53 +00:00
|
|
|
ret = interval == 0 || burst > 0;
|
2025-04-23 19:03:59 +00:00
|
|
|
if (!(READ_ONCE(rs->flags) & RATELIMIT_INITIALIZED) || (!interval && !burst) ||
|
2025-04-24 19:06:43 +00:00
|
|
|
!raw_spin_trylock_irqsave(&rs->lock, flags))
|
|
|
|
|
goto nolock_ret;
|
2025-04-23 19:03:59 +00:00
|
|
|
|
|
|
|
|
/* Force re-initialization once re-enabled. */
|
|
|
|
|
rs->flags &= ~RATELIMIT_INITIALIZED;
|
|
|
|
|
goto unlock_ret;
|
2025-04-09 23:58:53 +00:00
|
|
|
}
|
2008-04-29 07:59:43 +00:00
|
|
|
|
2009-09-22 12:44:11 +00:00
|
|
|
/*
|
2025-04-09 23:54:33 +00:00
|
|
|
* If we contend on this state's lock then just check if
|
|
|
|
|
* the current burst is used or not. It might cause
|
|
|
|
|
* false positive when we are past the interval and
|
|
|
|
|
* the current lock owner is just about to reset it.
|
2009-09-22 12:44:11 +00:00
|
|
|
*/
|
2025-03-14 18:26:59 +00:00
|
|
|
if (!raw_spin_trylock_irqsave(&rs->lock, flags)) {
|
2025-04-24 19:36:19 +00:00
|
|
|
if (READ_ONCE(rs->flags) & RATELIMIT_INITIALIZED &&
|
2025-04-24 19:16:21 +00:00
|
|
|
atomic_read(&rs->rs_n_left) > 0 && atomic_dec_return(&rs->rs_n_left) >= 0)
|
|
|
|
|
ret = 1;
|
|
|
|
|
goto nolock_ret;
|
2025-03-14 18:26:59 +00:00
|
|
|
}
|
2009-09-22 12:44:11 +00:00
|
|
|
|
2025-03-25 22:32:54 +00:00
|
|
|
if (!(rs->flags & RATELIMIT_INITIALIZED)) {
|
2008-07-25 08:45:58 +00:00
|
|
|
rs->begin = jiffies;
|
2025-03-25 22:32:54 +00:00
|
|
|
rs->flags |= RATELIMIT_INITIALIZED;
|
2025-04-09 23:54:33 +00:00
|
|
|
atomic_set(&rs->rs_n_left, rs->burst);
|
2025-03-25 22:32:54 +00:00
|
|
|
}
|
2008-04-29 07:59:43 +00:00
|
|
|
|
2022-08-23 17:46:48 +00:00
|
|
|
if (time_is_before_jiffies(rs->begin + interval)) {
|
2025-04-09 23:54:33 +00:00
|
|
|
int m;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Reset rs_n_left ASAP to reduce false positives
|
|
|
|
|
* in parallel calls, see above.
|
|
|
|
|
*/
|
|
|
|
|
atomic_set(&rs->rs_n_left, rs->burst);
|
|
|
|
|
rs->begin = jiffies;
|
2025-03-13 22:31:50 +00:00
|
|
|
|
2025-04-10 21:37:31 +00:00
|
|
|
if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) {
|
|
|
|
|
m = ratelimit_state_reset_miss(rs);
|
|
|
|
|
if (m) {
|
2017-10-03 23:16:45 +00:00
|
|
|
printk_deferred(KERN_WARNING
|
2025-03-13 22:31:50 +00:00
|
|
|
"%s: %d callbacks suppressed\n", func, m);
|
2016-08-02 21:04:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
2008-04-29 07:59:43 +00:00
|
|
|
}
|
2025-04-09 23:54:33 +00:00
|
|
|
|
2025-04-24 19:28:24 +00:00
|
|
|
/* Note that the burst might be taken by a parallel call. */
|
2025-04-24 19:36:19 +00:00
|
|
|
if (atomic_read(&rs->rs_n_left) > 0 && atomic_dec_return(&rs->rs_n_left) >= 0)
|
2025-04-24 19:28:24 +00:00
|
|
|
ret = 1;
|
2025-04-09 23:54:33 +00:00
|
|
|
|
|
|
|
|
unlock_ret:
|
2009-07-25 15:50:36 +00:00
|
|
|
raw_spin_unlock_irqrestore(&rs->lock, flags);
|
2008-07-25 08:45:58 +00:00
|
|
|
|
2025-04-24 19:06:43 +00:00
|
|
|
nolock_ret:
|
2025-04-24 19:00:10 +00:00
|
|
|
if (!ret)
|
|
|
|
|
ratelimit_state_inc_miss(rs);
|
|
|
|
|
|
2009-09-22 12:44:11 +00:00
|
|
|
return ret;
|
2008-04-29 07:59:43 +00:00
|
|
|
}
|
2009-10-23 12:58:11 +00:00
|
|
|
EXPORT_SYMBOL(___ratelimit);
|