blob: d01f4713523904b6c1da5022eb273f227b129095 [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{
Alexey Dobriyan4d9c3772008-07-28 15:46:21 -070030 unsigned long flags;
Ingo Molnar979f6932009-09-22 14:44:11 +020031 int ret;
Alexey Dobriyan4d9c3772008-07-28 15:46:21 -070032
Dave Young717115e2008-07-25 01:45:58 -070033 if (!rs->interval)
34 return 1;
Dave Young5f97a5a2008-04-29 00:59:43 -070035
Ingo Molnaredaac8e2009-09-22 14:44:11 +020036 /*
37 * If we contend on this state's lock then almost
38 * by definition we are too busy to print a message,
39 * in addition to the one that will be printed by
40 * the entity that is holding the lock already:
41 */
Thomas Gleixner07354eb2009-07-25 17:50:36 +020042 if (!raw_spin_trylock_irqsave(&rs->lock, flags))
Yong Zhang57119c32010-04-06 14:35:03 -070043 return 0;
Ingo Molnaredaac8e2009-09-22 14:44:11 +020044
Dave Young717115e2008-07-25 01:45:58 -070045 if (!rs->begin)
46 rs->begin = jiffies;
Dave Young5f97a5a2008-04-29 00:59:43 -070047
Dave Young717115e2008-07-25 01:45:58 -070048 if (time_is_before_jiffies(rs->begin + rs->interval)) {
Borislav Petkov6b1d1742016-08-02 14:04:04 -070049 if (rs->missed) {
50 if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) {
Sergey Senozhatsky1c089122017-10-03 16:16:45 -070051 printk_deferred(KERN_WARNING
52 "%s: %d callbacks suppressed\n",
53 func, rs->missed);
Borislav Petkov6b1d1742016-08-02 14:04:04 -070054 rs->missed = 0;
55 }
56 }
Jaewon Kimc2594bc2016-01-21 16:55:07 -080057 rs->begin = jiffies;
Dave Young717115e2008-07-25 01:45:58 -070058 rs->printed = 0;
Dave Young5f97a5a2008-04-29 00:59:43 -070059 }
Ingo Molnar979f6932009-09-22 14:44:11 +020060 if (rs->burst && rs->burst > rs->printed) {
61 rs->printed++;
62 ret = 1;
63 } else {
64 rs->missed++;
65 ret = 0;
66 }
Thomas Gleixner07354eb2009-07-25 17:50:36 +020067 raw_spin_unlock_irqrestore(&rs->lock, flags);
Dave Young717115e2008-07-25 01:45:58 -070068
Ingo Molnar979f6932009-09-22 14:44:11 +020069 return ret;
Dave Young5f97a5a2008-04-29 00:59:43 -070070}
Christian Borntraeger5c828712009-10-23 14:58:11 +020071EXPORT_SYMBOL(___ratelimit);