Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ratelimit.c - Do something with rate limit. |
| 3 | * |
| 4 | * Isolated from kernel/printk.c by Dave Young <hidave.darkstar@gmail.com> |
| 5 | * |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 6 | * 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 Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 9 | * This file is released under the GPLv2. |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
Ingo Molnar | 3fff4c4 | 2009-09-22 16:18:09 +0200 | [diff] [blame] | 12 | #include <linux/ratelimit.h> |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 13 | #include <linux/jiffies.h> |
| 14 | #include <linux/module.h> |
| 15 | |
| 16 | /* |
| 17 | * __ratelimit - rate limiting |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 18 | * @rs: ratelimit_state data |
Yong Zhang | 2a7268a | 2010-04-06 14:35:01 -0700 | [diff] [blame] | 19 | * @func: name of calling function |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 20 | * |
Yong Zhang | 2a7268a | 2010-04-06 14:35:01 -0700 | [diff] [blame] | 21 | * 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 Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 27 | */ |
Christian Borntraeger | 5c82871 | 2009-10-23 14:58:11 +0200 | [diff] [blame] | 28 | int ___ratelimit(struct ratelimit_state *rs, const char *func) |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 29 | { |
Alexey Dobriyan | 4d9c377 | 2008-07-28 15:46:21 -0700 | [diff] [blame] | 30 | unsigned long flags; |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 31 | int ret; |
Alexey Dobriyan | 4d9c377 | 2008-07-28 15:46:21 -0700 | [diff] [blame] | 32 | |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 33 | if (!rs->interval) |
| 34 | return 1; |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 35 | |
Ingo Molnar | edaac8e | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 36 | /* |
| 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 | */ |
| 42 | if (!spin_trylock_irqsave(&rs->lock, flags)) |
Yong Zhang | 57119c3 | 2010-04-06 14:35:03 -0700 | [diff] [blame^] | 43 | return 0; |
Ingo Molnar | edaac8e | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 44 | |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 45 | if (!rs->begin) |
| 46 | rs->begin = jiffies; |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 47 | |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 48 | if (time_is_before_jiffies(rs->begin + rs->interval)) { |
| 49 | if (rs->missed) |
| 50 | printk(KERN_WARNING "%s: %d callbacks suppressed\n", |
Christian Borntraeger | 5c82871 | 2009-10-23 14:58:11 +0200 | [diff] [blame] | 51 | func, rs->missed); |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 52 | rs->begin = 0; |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 53 | rs->printed = 0; |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 54 | rs->missed = 0; |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 55 | } |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 56 | if (rs->burst && rs->burst > rs->printed) { |
| 57 | rs->printed++; |
| 58 | ret = 1; |
| 59 | } else { |
| 60 | rs->missed++; |
| 61 | ret = 0; |
| 62 | } |
| 63 | spin_unlock_irqrestore(&rs->lock, flags); |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 64 | |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 65 | return ret; |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 66 | } |
Christian Borntraeger | 5c82871 | 2009-10-23 14:58:11 +0200 | [diff] [blame] | 67 | EXPORT_SYMBOL(___ratelimit); |