blob: ce945c17980b9b8addd4c044e1080f73f21a4f3d [file] [log] [blame]
Thomas Gleixner55716d22019-06-01 10:08:42 +02001// SPDX-License-Identifier: GPL-2.0-only
Dave Young5f97a5a2008-04-29 00:59:43 -07002/*
3 * ratelimit.c - Do something with rate limit.
4 *
5 * Isolated from kernel/printk.c by Dave Young <hidave.darkstar@gmail.com>
6 *
Dave Young717115e2008-07-25 01:45:58 -07007 * 2008-05-01 rewrite the function and use a ratelimit_state data struct as
8 * parameter. Now every user can use their own standalone ratelimit_state.
Dave Young5f97a5a2008-04-29 00:59:43 -07009 */
10
Ingo Molnar3fff4c42009-09-22 16:18:09 +020011#include <linux/ratelimit.h>
Dave Young5f97a5a2008-04-29 00:59:43 -070012#include <linux/jiffies.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050013#include <linux/export.h>
Dave Young5f97a5a2008-04-29 00:59:43 -070014
15/*
16 * __ratelimit - rate limiting
Dave Young717115e2008-07-25 01:45:58 -070017 * @rs: ratelimit_state data
Yong Zhang2a7268a2010-04-06 14:35:01 -070018 * @func: name of calling function
Dave Young5f97a5a2008-04-29 00:59:43 -070019 *
Yong Zhang2a7268a2010-04-06 14:35:01 -070020 * This enforces a rate limit: not more than @rs->burst callbacks
21 * in every @rs->interval
22 *
23 * RETURNS:
24 * 0 means callbacks will be suppressed.
25 * 1 means go ahead and do it.
Dave Young5f97a5a2008-04-29 00:59:43 -070026 */
Christian Borntraeger5c828712009-10-23 14:58:11 +020027int ___ratelimit(struct ratelimit_state *rs, const char *func)
Dave Young5f97a5a2008-04-29 00:59:43 -070028{
Kuniyuki Iwashima6bae8ceb2022-08-23 10:46:48 -070029 /* Paired with WRITE_ONCE() in .proc_handler().
30 * Changing two values seperately could be inconsistent
31 * and some message could be lost. (See: net_ratelimit_state).
32 */
33 int interval = READ_ONCE(rs->interval);
34 int burst = READ_ONCE(rs->burst);
Alexey Dobriyan4d9c3772008-07-28 15:46:21 -070035 unsigned long flags;
Ingo Molnar979f6932009-09-22 14:44:11 +020036 int ret;
Alexey Dobriyan4d9c3772008-07-28 15:46:21 -070037
Kuniyuki Iwashima6bae8ceb2022-08-23 10:46:48 -070038 if (!interval)
Dave Young717115e2008-07-25 01:45:58 -070039 return 1;
Dave Young5f97a5a2008-04-29 00:59:43 -070040
Ingo Molnaredaac8e2009-09-22 14:44:11 +020041 /*
42 * If we contend on this state's lock then almost
43 * by definition we are too busy to print a message,
44 * in addition to the one that will be printed by
45 * the entity that is holding the lock already:
46 */
Thomas Gleixner07354eb2009-07-25 17:50:36 +020047 if (!raw_spin_trylock_irqsave(&rs->lock, flags))
Yong Zhang57119c32010-04-06 14:35:03 -070048 return 0;
Ingo Molnaredaac8e2009-09-22 14:44:11 +020049
Dave Young717115e2008-07-25 01:45:58 -070050 if (!rs->begin)
51 rs->begin = jiffies;
Dave Young5f97a5a2008-04-29 00:59:43 -070052
Kuniyuki Iwashima6bae8ceb2022-08-23 10:46:48 -070053 if (time_is_before_jiffies(rs->begin + interval)) {
Borislav Petkov6b1d1742016-08-02 14:04:04 -070054 if (rs->missed) {
55 if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) {
Sergey Senozhatsky656d61c2017-10-03 16:16:45 -070056 printk_deferred(KERN_WARNING
57 "%s: %d callbacks suppressed\n",
58 func, rs->missed);
Borislav Petkov6b1d1742016-08-02 14:04:04 -070059 rs->missed = 0;
60 }
61 }
Jaewon Kimc2594bc2016-01-21 16:55:07 -080062 rs->begin = jiffies;
Dave Young717115e2008-07-25 01:45:58 -070063 rs->printed = 0;
Dave Young5f97a5a2008-04-29 00:59:43 -070064 }
Kuniyuki Iwashima6bae8ceb2022-08-23 10:46:48 -070065 if (burst && burst > rs->printed) {
Ingo Molnar979f6932009-09-22 14:44:11 +020066 rs->printed++;
67 ret = 1;
68 } else {
69 rs->missed++;
70 ret = 0;
71 }
Thomas Gleixner07354eb2009-07-25 17:50:36 +020072 raw_spin_unlock_irqrestore(&rs->lock, flags);
Dave Young717115e2008-07-25 01:45:58 -070073
Ingo Molnar979f6932009-09-22 14:44:11 +020074 return ret;
Dave Young5f97a5a2008-04-29 00:59:43 -070075}
Christian Borntraeger5c828712009-10-23 14:58:11 +020076EXPORT_SYMBOL(___ratelimit);