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> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 14 | #include <linux/export.h> |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 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 | */ |
Thomas Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 42 | if (!raw_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)) { |
Borislav Petkov | 6b1d174 | 2016-08-02 14:04:04 -0700 | [diff] [blame] | 49 | if (rs->missed) { |
| 50 | if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) { |
Sergey Senozhatsky | 656d61c | 2017-10-03 16:16:45 -0700 | [diff] [blame^] | 51 | printk_deferred(KERN_WARNING |
| 52 | "%s: %d callbacks suppressed\n", |
| 53 | func, rs->missed); |
Borislav Petkov | 6b1d174 | 2016-08-02 14:04:04 -0700 | [diff] [blame] | 54 | rs->missed = 0; |
| 55 | } |
| 56 | } |
Jaewon Kim | c2594bc | 2016-01-21 16:55:07 -0800 | [diff] [blame] | 57 | rs->begin = jiffies; |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 58 | rs->printed = 0; |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 59 | } |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 60 | if (rs->burst && rs->burst > rs->printed) { |
| 61 | rs->printed++; |
| 62 | ret = 1; |
| 63 | } else { |
| 64 | rs->missed++; |
| 65 | ret = 0; |
| 66 | } |
Thomas Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 67 | raw_spin_unlock_irqrestore(&rs->lock, flags); |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 68 | |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 69 | return ret; |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 70 | } |
Christian Borntraeger | 5c82871 | 2009-10-23 14:58:11 +0200 | [diff] [blame] | 71 | EXPORT_SYMBOL(___ratelimit); |