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 |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 19 | * |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 20 | * This enforces a rate limit: not more than @rs->ratelimit_burst callbacks |
| 21 | * in every @rs->ratelimit_jiffies |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 22 | */ |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 23 | int __ratelimit(struct ratelimit_state *rs) |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 24 | { |
Alexey Dobriyan | 4d9c377 | 2008-07-28 15:46:21 -0700 | [diff] [blame] | 25 | unsigned long flags; |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 26 | int ret; |
Alexey Dobriyan | 4d9c377 | 2008-07-28 15:46:21 -0700 | [diff] [blame] | 27 | |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 28 | if (!rs->interval) |
| 29 | return 1; |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 30 | |
Ingo Molnar | edaac8e | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 31 | /* |
| 32 | * If we contend on this state's lock then almost |
| 33 | * by definition we are too busy to print a message, |
| 34 | * in addition to the one that will be printed by |
| 35 | * the entity that is holding the lock already: |
| 36 | */ |
| 37 | if (!spin_trylock_irqsave(&rs->lock, flags)) |
| 38 | return 1; |
| 39 | |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 40 | if (!rs->begin) |
| 41 | rs->begin = jiffies; |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 42 | |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 43 | if (time_is_before_jiffies(rs->begin + rs->interval)) { |
| 44 | if (rs->missed) |
| 45 | printk(KERN_WARNING "%s: %d callbacks suppressed\n", |
| 46 | __func__, rs->missed); |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 47 | rs->begin = 0; |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 48 | rs->printed = 0; |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 49 | rs->missed = 0; |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 50 | } |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 51 | if (rs->burst && rs->burst > rs->printed) { |
| 52 | rs->printed++; |
| 53 | ret = 1; |
| 54 | } else { |
| 55 | rs->missed++; |
| 56 | ret = 0; |
| 57 | } |
| 58 | spin_unlock_irqrestore(&rs->lock, flags); |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 59 | |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 60 | return ret; |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 61 | } |
| 62 | EXPORT_SYMBOL(__ratelimit); |