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 | { |
Kuniyuki Iwashima | 509c21e | 2022-08-23 10:46:48 -0700 | [diff] [blame] | 30 | /* Paired with WRITE_ONCE() in .proc_handler(). |
| 31 | * Changing two values seperately could be inconsistent |
| 32 | * and some message could be lost. (See: net_ratelimit_state). |
| 33 | */ |
| 34 | int interval = READ_ONCE(rs->interval); |
| 35 | int burst = READ_ONCE(rs->burst); |
Alexey Dobriyan | 4d9c377 | 2008-07-28 15:46:21 -0700 | [diff] [blame] | 36 | unsigned long flags; |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 37 | int ret; |
Alexey Dobriyan | 4d9c377 | 2008-07-28 15:46:21 -0700 | [diff] [blame] | 38 | |
Kuniyuki Iwashima | 509c21e | 2022-08-23 10:46:48 -0700 | [diff] [blame] | 39 | if (!interval) |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 40 | return 1; |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 41 | |
Ingo Molnar | edaac8e | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 42 | /* |
| 43 | * If we contend on this state's lock then almost |
| 44 | * by definition we are too busy to print a message, |
| 45 | * in addition to the one that will be printed by |
| 46 | * the entity that is holding the lock already: |
| 47 | */ |
Thomas Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 48 | if (!raw_spin_trylock_irqsave(&rs->lock, flags)) |
Yong Zhang | 57119c3 | 2010-04-06 14:35:03 -0700 | [diff] [blame] | 49 | return 0; |
Ingo Molnar | edaac8e | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 50 | |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 51 | if (!rs->begin) |
| 52 | rs->begin = jiffies; |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 53 | |
Kuniyuki Iwashima | 509c21e | 2022-08-23 10:46:48 -0700 | [diff] [blame] | 54 | if (time_is_before_jiffies(rs->begin + interval)) { |
Borislav Petkov | 6b1d174 | 2016-08-02 14:04:04 -0700 | [diff] [blame] | 55 | if (rs->missed) { |
| 56 | if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) { |
Sergey Senozhatsky | 1c08912 | 2017-10-03 16:16:45 -0700 | [diff] [blame] | 57 | printk_deferred(KERN_WARNING |
| 58 | "%s: %d callbacks suppressed\n", |
| 59 | func, rs->missed); |
Borislav Petkov | 6b1d174 | 2016-08-02 14:04:04 -0700 | [diff] [blame] | 60 | rs->missed = 0; |
| 61 | } |
| 62 | } |
Jaewon Kim | c2594bc | 2016-01-21 16:55:07 -0800 | [diff] [blame] | 63 | rs->begin = jiffies; |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 64 | rs->printed = 0; |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 65 | } |
Kuniyuki Iwashima | 509c21e | 2022-08-23 10:46:48 -0700 | [diff] [blame] | 66 | if (burst && burst > rs->printed) { |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 67 | rs->printed++; |
| 68 | ret = 1; |
| 69 | } else { |
| 70 | rs->missed++; |
| 71 | ret = 0; |
| 72 | } |
Thomas Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 73 | raw_spin_unlock_irqrestore(&rs->lock, flags); |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 74 | |
Ingo Molnar | 979f693 | 2009-09-22 14:44:11 +0200 | [diff] [blame] | 75 | return ret; |
Dave Young | 5f97a5a | 2008-04-29 00:59:43 -0700 | [diff] [blame] | 76 | } |
Christian Borntraeger | 5c82871 | 2009-10-23 14:58:11 +0200 | [diff] [blame] | 77 | EXPORT_SYMBOL(___ratelimit); |