blob: b805702de84ddbc4634f24dc0e805b71214d089b [file] [log] [blame]
Dave Young5f97a5a2008-04-29 00:59:43 -07001/*
2 * ratelimit.c - Do something with rate limit.
3 *
4 * Isolated from kernel/printk.c by Dave Young <hidave.darkstar@gmail.com>
5 *
Dave Young717115e2008-07-25 01:45:58 -07006 * 2008-05-01 rewrite the function and use a ratelimit_state data struct as
7 * parameter. Now every user can use their own standalone ratelimit_state.
8 *
Dave Young5f97a5a2008-04-29 00:59:43 -07009 * This file is released under the GPLv2.
Dave Young5f97a5a2008-04-29 00:59:43 -070010 */
11
Ingo Molnar3fff4c42009-09-22 16:18:09 +020012#include <linux/ratelimit.h>
Dave Young5f97a5a2008-04-29 00:59:43 -070013#include <linux/jiffies.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050014#include <linux/export.h>
Dave Young5f97a5a2008-04-29 00:59:43 -070015
16/*
17 * __ratelimit - rate limiting
Dave Young717115e2008-07-25 01:45:58 -070018 * @rs: ratelimit_state data
Yong Zhang2a7268a2010-04-06 14:35:01 -070019 * @func: name of calling function
Dave Young5f97a5a2008-04-29 00:59:43 -070020 *
Yong Zhang2a7268a2010-04-06 14:35:01 -070021 * This enforces a rate limit: not more than @rs->burst callbacks
22 * in every @rs->interval
23 *
24 * RETURNS:
25 * 0 means callbacks will be suppressed.
26 * 1 means go ahead and do it.
Dave Young5f97a5a2008-04-29 00:59:43 -070027 */
Christian Borntraeger5c828712009-10-23 14:58:11 +020028int ___ratelimit(struct ratelimit_state *rs, const char *func)
Dave Young5f97a5a2008-04-29 00:59:43 -070029{
Kuniyuki Iwashima509c21e2022-08-23 10:46:48 -070030 /* Paired with WRITE_ONCE() in .proc_handler().
31 * Changing two values seperately could be inconsistent
32 * and some message could be lost. (See: net_ratelimit_state).
33 */
34 int interval = READ_ONCE(rs->interval);
35 int burst = READ_ONCE(rs->burst);
Alexey Dobriyan4d9c3772008-07-28 15:46:21 -070036 unsigned long flags;
Ingo Molnar979f6932009-09-22 14:44:11 +020037 int ret;
Alexey Dobriyan4d9c3772008-07-28 15:46:21 -070038
Kuniyuki Iwashima509c21e2022-08-23 10:46:48 -070039 if (!interval)
Dave Young717115e2008-07-25 01:45:58 -070040 return 1;
Dave Young5f97a5a2008-04-29 00:59:43 -070041
Ingo Molnaredaac8e2009-09-22 14:44:11 +020042 /*
43 * If we contend on this state's lock then almost
44 * by definition we are too busy to print a message,
45 * in addition to the one that will be printed by
46 * the entity that is holding the lock already:
47 */
Thomas Gleixner07354eb2009-07-25 17:50:36 +020048 if (!raw_spin_trylock_irqsave(&rs->lock, flags))
Yong Zhang57119c32010-04-06 14:35:03 -070049 return 0;
Ingo Molnaredaac8e2009-09-22 14:44:11 +020050
Dave Young717115e2008-07-25 01:45:58 -070051 if (!rs->begin)
52 rs->begin = jiffies;
Dave Young5f97a5a2008-04-29 00:59:43 -070053
Kuniyuki Iwashima509c21e2022-08-23 10:46:48 -070054 if (time_is_before_jiffies(rs->begin + interval)) {
Borislav Petkov6b1d1742016-08-02 14:04:04 -070055 if (rs->missed) {
56 if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) {
Sergey Senozhatsky1c089122017-10-03 16:16:45 -070057 printk_deferred(KERN_WARNING
58 "%s: %d callbacks suppressed\n",
59 func, rs->missed);
Borislav Petkov6b1d1742016-08-02 14:04:04 -070060 rs->missed = 0;
61 }
62 }
Jaewon Kimc2594bc2016-01-21 16:55:07 -080063 rs->begin = jiffies;
Dave Young717115e2008-07-25 01:45:58 -070064 rs->printed = 0;
Dave Young5f97a5a2008-04-29 00:59:43 -070065 }
Kuniyuki Iwashima509c21e2022-08-23 10:46:48 -070066 if (burst && burst > rs->printed) {
Ingo Molnar979f6932009-09-22 14:44:11 +020067 rs->printed++;
68 ret = 1;
69 } else {
70 rs->missed++;
71 ret = 0;
72 }
Thomas Gleixner07354eb2009-07-25 17:50:36 +020073 raw_spin_unlock_irqrestore(&rs->lock, flags);
Dave Young717115e2008-07-25 01:45:58 -070074
Ingo Molnar979f6932009-09-22 14:44:11 +020075 return ret;
Dave Young5f97a5a2008-04-29 00:59:43 -070076}
Christian Borntraeger5c828712009-10-23 14:58:11 +020077EXPORT_SYMBOL(___ratelimit);