blob: c96d500577de57d5d87ffa293d1d630c318b8b92 [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>
14#include <linux/module.h>
15
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)) {
49 if (rs->missed)
50 printk(KERN_WARNING "%s: %d callbacks suppressed\n",
Christian Borntraeger5c828712009-10-23 14:58:11 +020051 func, rs->missed);
Ingo Molnar979f6932009-09-22 14:44:11 +020052 rs->begin = 0;
Dave Young717115e2008-07-25 01:45:58 -070053 rs->printed = 0;
Ingo Molnar979f6932009-09-22 14:44:11 +020054 rs->missed = 0;
Dave Young5f97a5a2008-04-29 00:59:43 -070055 }
Ingo Molnar979f6932009-09-22 14:44:11 +020056 if (rs->burst && rs->burst > rs->printed) {
57 rs->printed++;
58 ret = 1;
59 } else {
60 rs->missed++;
61 ret = 0;
62 }
Thomas Gleixner07354eb2009-07-25 17:50:36 +020063 raw_spin_unlock_irqrestore(&rs->lock, flags);
Dave Young717115e2008-07-25 01:45:58 -070064
Ingo Molnar979f6932009-09-22 14:44:11 +020065 return ret;
Dave Young5f97a5a2008-04-29 00:59:43 -070066}
Christian Borntraeger5c828712009-10-23 14:58:11 +020067EXPORT_SYMBOL(___ratelimit);