blob: e01a93f46f833483680b876fe9df7573bf4c2281 [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{
Alexey Dobriyan4d9c3772008-07-28 15:46:21 -070029 unsigned long flags;
Ingo Molnar979f6932009-09-22 14:44:11 +020030 int ret;
Alexey Dobriyan4d9c3772008-07-28 15:46:21 -070031
Dave Young717115e2008-07-25 01:45:58 -070032 if (!rs->interval)
33 return 1;
Dave Young5f97a5a2008-04-29 00:59:43 -070034
Ingo Molnaredaac8e2009-09-22 14:44:11 +020035 /*
36 * If we contend on this state's lock then almost
37 * by definition we are too busy to print a message,
38 * in addition to the one that will be printed by
39 * the entity that is holding the lock already:
40 */
Thomas Gleixner07354eb2009-07-25 17:50:36 +020041 if (!raw_spin_trylock_irqsave(&rs->lock, flags))
Yong Zhang57119c32010-04-06 14:35:03 -070042 return 0;
Ingo Molnaredaac8e2009-09-22 14:44:11 +020043
Dave Young717115e2008-07-25 01:45:58 -070044 if (!rs->begin)
45 rs->begin = jiffies;
Dave Young5f97a5a2008-04-29 00:59:43 -070046
Dave Young717115e2008-07-25 01:45:58 -070047 if (time_is_before_jiffies(rs->begin + rs->interval)) {
Borislav Petkov6b1d1742016-08-02 14:04:04 -070048 if (rs->missed) {
49 if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) {
Sergey Senozhatsky656d61c2017-10-03 16:16:45 -070050 printk_deferred(KERN_WARNING
51 "%s: %d callbacks suppressed\n",
52 func, rs->missed);
Borislav Petkov6b1d1742016-08-02 14:04:04 -070053 rs->missed = 0;
54 }
55 }
Jaewon Kimc2594bc2016-01-21 16:55:07 -080056 rs->begin = jiffies;
Dave Young717115e2008-07-25 01:45:58 -070057 rs->printed = 0;
Dave Young5f97a5a2008-04-29 00:59:43 -070058 }
Ingo Molnar979f6932009-09-22 14:44:11 +020059 if (rs->burst && rs->burst > rs->printed) {
60 rs->printed++;
61 ret = 1;
62 } else {
63 rs->missed++;
64 ret = 0;
65 }
Thomas Gleixner07354eb2009-07-25 17:50:36 +020066 raw_spin_unlock_irqrestore(&rs->lock, flags);
Dave Young717115e2008-07-25 01:45:58 -070067
Ingo Molnar979f6932009-09-22 14:44:11 +020068 return ret;
Dave Young5f97a5a2008-04-29 00:59:43 -070069}
Christian Borntraeger5c828712009-10-23 14:58:11 +020070EXPORT_SYMBOL(___ratelimit);