2019-06-01 10:08:42 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2008-04-29 00:59:43 -07:00
|
|
|
/*
|
|
|
|
|
* ratelimit.c - Do something with rate limit.
|
|
|
|
|
*
|
|
|
|
|
* Isolated from kernel/printk.c by Dave Young <hidave.darkstar@gmail.com>
|
|
|
|
|
*
|
2008-07-25 01:45:58 -07: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 00:59:43 -07:00
|
|
|
*/
|
|
|
|
|
|
2009-09-22 16:18:09 +02:00
|
|
|
#include <linux/ratelimit.h>
|
2008-04-29 00:59:43 -07:00
|
|
|
#include <linux/jiffies.h>
|
2011-11-16 21:29:17 -05:00
|
|
|
#include <linux/export.h>
|
2008-04-29 00:59:43 -07:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* __ratelimit - rate limiting
|
2008-07-25 01:45:58 -07:00
|
|
|
* @rs: ratelimit_state data
|
2010-04-06 14:35:01 -07:00
|
|
|
* @func: name of calling function
|
2008-04-29 00:59:43 -07:00
|
|
|
*
|
2010-04-06 14:35:01 -07: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 00:59:43 -07:00
|
|
|
*/
|
2009-10-23 14:58:11 +02:00
|
|
|
int ___ratelimit(struct ratelimit_state *rs, const char *func)
|
2008-04-29 00:59:43 -07:00
|
|
|
{
|
2022-08-23 10:46:48 -07:00
|
|
|
/* Paired with WRITE_ONCE() in .proc_handler().
|
2025-11-19 18:11:44 +08:00
|
|
|
* Changing two values separately could be inconsistent
|
2022-08-23 10:46:48 -07:00
|
|
|
* 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 15:46:21 -07:00
|
|
|
unsigned long flags;
|
2025-04-24 12:00:10 -07:00
|
|
|
int ret = 0;
|
2008-07-28 15:46:21 -07:00
|
|
|
|
2025-04-23 12:03:59 -07:00
|
|
|
/*
|
|
|
|
|
* Zero interval says never limit, otherwise, non-positive burst
|
|
|
|
|
* says always limit.
|
|
|
|
|
*/
|
2025-04-09 16:58:53 -07:00
|
|
|
if (interval <= 0 || burst <= 0) {
|
2025-04-24 11:19:13 -07:00
|
|
|
WARN_ONCE(interval < 0 || burst < 0, "Negative interval (%d) or burst (%d): Uninitialized ratelimit_state structure?\n", interval, burst);
|
2025-04-09 16:58:53 -07:00
|
|
|
ret = interval == 0 || burst > 0;
|
2025-04-23 12:03:59 -07:00
|
|
|
if (!(READ_ONCE(rs->flags) & RATELIMIT_INITIALIZED) || (!interval && !burst) ||
|
2025-04-24 12:06:43 -07:00
|
|
|
!raw_spin_trylock_irqsave(&rs->lock, flags))
|
|
|
|
|
goto nolock_ret;
|
2025-04-23 12:03:59 -07:00
|
|
|
|
|
|
|
|
/* Force re-initialization once re-enabled. */
|
|
|
|
|
rs->flags &= ~RATELIMIT_INITIALIZED;
|
|
|
|
|
goto unlock_ret;
|
2025-04-09 16:58:53 -07:00
|
|
|
}
|
2008-04-29 00:59:43 -07:00
|
|
|
|
2009-09-22 14:44:11 +02:00
|
|
|
/*
|
2025-04-09 16:54:33 -07: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 14:44:11 +02:00
|
|
|
*/
|
2025-03-14 11:26:59 -07:00
|
|
|
if (!raw_spin_trylock_irqsave(&rs->lock, flags)) {
|
2025-04-24 12:36:19 -07:00
|
|
|
if (READ_ONCE(rs->flags) & RATELIMIT_INITIALIZED &&
|
2025-04-24 12:16:21 -07:00
|
|
|
atomic_read(&rs->rs_n_left) > 0 && atomic_dec_return(&rs->rs_n_left) >= 0)
|
|
|
|
|
ret = 1;
|
|
|
|
|
goto nolock_ret;
|
2025-03-14 11:26:59 -07:00
|
|
|
}
|
2009-09-22 14:44:11 +02:00
|
|
|
|
2025-03-25 15:32:54 -07:00
|
|
|
if (!(rs->flags & RATELIMIT_INITIALIZED)) {
|
2008-07-25 01:45:58 -07:00
|
|
|
rs->begin = jiffies;
|
2025-03-25 15:32:54 -07:00
|
|
|
rs->flags |= RATELIMIT_INITIALIZED;
|
2025-04-09 16:54:33 -07:00
|
|
|
atomic_set(&rs->rs_n_left, rs->burst);
|
2025-03-25 15:32:54 -07:00
|
|
|
}
|
2008-04-29 00:59:43 -07:00
|
|
|
|
2022-08-23 10:46:48 -07:00
|
|
|
if (time_is_before_jiffies(rs->begin + interval)) {
|
2025-04-09 16:54:33 -07:00
|
|
|
int m;
|
2025-03-13 15:31:50 -07:00
|
|
|
|
2025-04-09 16:54:33 -07:00
|
|
|
/*
|
|
|
|
|
* 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-04-10 14:37:31 -07:00
|
|
|
if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) {
|
|
|
|
|
m = ratelimit_state_reset_miss(rs);
|
|
|
|
|
if (m) {
|
2017-10-03 16:16:45 -07:00
|
|
|
printk_deferred(KERN_WARNING
|
2025-03-13 15:31:50 -07:00
|
|
|
"%s: %d callbacks suppressed\n", func, m);
|
2016-08-02 14:04:04 -07:00
|
|
|
}
|
|
|
|
|
}
|
2008-04-29 00:59:43 -07:00
|
|
|
}
|
2025-04-09 16:54:33 -07:00
|
|
|
|
2025-04-24 12:28:24 -07:00
|
|
|
/* Note that the burst might be taken by a parallel call. */
|
2025-04-24 12:36:19 -07:00
|
|
|
if (atomic_read(&rs->rs_n_left) > 0 && atomic_dec_return(&rs->rs_n_left) >= 0)
|
2025-04-24 12:28:24 -07:00
|
|
|
ret = 1;
|
2025-04-09 16:54:33 -07:00
|
|
|
|
|
|
|
|
unlock_ret:
|
2009-07-25 17:50:36 +02:00
|
|
|
raw_spin_unlock_irqrestore(&rs->lock, flags);
|
2008-07-25 01:45:58 -07:00
|
|
|
|
2025-04-24 12:06:43 -07:00
|
|
|
nolock_ret:
|
2025-04-24 12:00:10 -07:00
|
|
|
if (!ret)
|
|
|
|
|
ratelimit_state_inc_miss(rs);
|
|
|
|
|
|
2009-09-22 14:44:11 +02:00
|
|
|
return ret;
|
2008-04-29 00:59:43 -07:00
|
|
|
}
|
2009-10-23 14:58:11 +02:00
|
|
|
EXPORT_SYMBOL(___ratelimit);
|