Thomas Gleixner | 55716d2 | 2019-06-01 10:08:42 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 2 | /* |
| 3 | * ratelimit.c - Do something with rate limit. |
| 4 | * |
| 5 | * Isolated from kernel/printk.c by Dave Young <hidave.darkstar@gmail.com> |
| 6 | * |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 7 | * 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 Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
Ingo Molnar | 3fff4c4 | 2009-09-22 16:18:09 +0200 | [diff] [blame] | 11 | #include <linux/ratelimit.h> |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 12 | #include <linux/jiffies.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 13 | #include <linux/export.h> |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 14 | |
| 15 | /* |
| 16 | * __ratelimit - rate limiting |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 17 | * @rs: ratelimit_state data |
Yong Zhang | 2a7268a | 2010-04-06 14:35:01 -0700 | [diff] [blame] | 18 | * @func: name of calling function |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 19 | * |
Yong Zhang | 2a7268a | 2010-04-06 14:35:01 -0700 | [diff] [blame] | 20 | * 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 Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 26 | */ |
Christian Borntraeger | 5c82871 | 2009-10-23 14:58:11 +0200 | [diff] [blame] | 27 | int ___ratelimit(struct ratelimit_state *rs, const char *func) |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 28 | { |
Alexey Dobriyan | 4d9c377 | 2008-07-28 15:46:21 -0700 | [diff] [blame] | 29 | unsigned long flags; |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 30 | int ret; |
Alexey Dobriyan | 4d9c377 | 2008-07-28 15:46:21 -0700 | [diff] [blame] | 31 | |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 32 | if (!rs->interval) |
| 33 | return 1; |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 34 | |
Ingo Molnar | edaac8e | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 35 | /* |
| 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 Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 41 | if (!raw_spin_trylock_irqsave(&rs->lock, flags)) |
Yong Zhang | 57119c3 | 2010-04-06 14:35:03 -0700 | [diff] [blame] | 42 | return 0; |
Ingo Molnar | edaac8e | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 43 | |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 44 | if (!rs->begin) |
| 45 | rs->begin = jiffies; |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 46 | |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 47 | if (time_is_before_jiffies(rs->begin + rs->interval)) { |
Borislav Petkov | 6b1d174 | 2016-08-02 14:04:04 -0700 | [diff] [blame] | 48 | if (rs->missed) { |
| 49 | if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) { |
Sergey Senozhatsky | 656d61c | 2017-10-03 16:16:45 -0700 | [diff] [blame] | 50 | printk_deferred(KERN_WARNING |
| 51 | "%s: %d callbacks suppressed\n", |
| 52 | func, rs->missed); |
Borislav Petkov | 6b1d174 | 2016-08-02 14:04:04 -0700 | [diff] [blame] | 53 | rs->missed = 0; |
| 54 | } |
| 55 | } |
Jaewon Kim | c2594bc | 2016-01-21 16:55:07 -0800 | [diff] [blame] | 56 | rs->begin = jiffies; |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 57 | rs->printed = 0; |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 58 | } |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 59 | if (rs->burst && rs->burst > rs->printed) { |
| 60 | rs->printed++; |
| 61 | ret = 1; |
| 62 | } else { |
| 63 | rs->missed++; |
| 64 | ret = 0; |
| 65 | } |
Thomas Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 66 | raw_spin_unlock_irqrestore(&rs->lock, flags); |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 67 | |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 68 | return ret; |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 69 | } |
Christian Borntraeger | 5c82871 | 2009-10-23 14:58:11 +0200 | [diff] [blame] | 70 | EXPORT_SYMBOL(___ratelimit); |