Blame view
lib/ratelimit.c
1.61 KB
55716d264 treewide: Replace... |
1 |
// SPDX-License-Identifier: GPL-2.0-only |
5f97a5a87 isolate ratelimit... |
2 3 4 5 6 |
/* * ratelimit.c - Do something with rate limit. * * Isolated from kernel/printk.c by Dave Young <hidave.darkstar@gmail.com> * |
717115e1a printk ratelimiti... |
7 8 |
* 2008-05-01 rewrite the function and use a ratelimit_state data struct as * parameter. Now every user can use their own standalone ratelimit_state. |
5f97a5a87 isolate ratelimit... |
9 |
*/ |
3fff4c42b printk: Remove ra... |
10 |
#include <linux/ratelimit.h> |
5f97a5a87 isolate ratelimit... |
11 |
#include <linux/jiffies.h> |
8bc3bcc93 lib: reduce the u... |
12 |
#include <linux/export.h> |
5f97a5a87 isolate ratelimit... |
13 14 15 |
/* * __ratelimit - rate limiting |
717115e1a printk ratelimiti... |
16 |
* @rs: ratelimit_state data |
2a7268abc ratelimit: annota... |
17 |
* @func: name of calling function |
5f97a5a87 isolate ratelimit... |
18 |
* |
2a7268abc ratelimit: annota... |
19 20 21 22 23 24 |
* This enforces a rate limit: not more than @rs->burst callbacks * in every @rs->interval * * RETURNS: * 0 means callbacks will be suppressed. * 1 means go ahead and do it. |
5f97a5a87 isolate ratelimit... |
25 |
*/ |
5c8287133 ratelimit: Make s... |
26 |
int ___ratelimit(struct ratelimit_state *rs, const char *func) |
5f97a5a87 isolate ratelimit... |
27 |
{ |
4d9c377c8 __ratelimit() cpu... |
28 |
unsigned long flags; |
979f693de ratelimit: Use pe... |
29 |
int ret; |
4d9c377c8 __ratelimit() cpu... |
30 |
|
717115e1a printk ratelimiti... |
31 32 |
if (!rs->interval) return 1; |
5f97a5a87 isolate ratelimit... |
33 |
|
edaac8e31 ratelimit: Fix/al... |
34 35 36 37 38 39 |
/* * If we contend on this state's lock then almost * by definition we are too busy to print a message, * in addition to the one that will be printed by * the entity that is holding the lock already: */ |
07354eb1a locking, printk: ... |
40 |
if (!raw_spin_trylock_irqsave(&rs->lock, flags)) |
57119c34e ratelimit: fix th... |
41 |
return 0; |
edaac8e31 ratelimit: Fix/al... |
42 |
|
717115e1a printk ratelimiti... |
43 44 |
if (!rs->begin) rs->begin = jiffies; |
5f97a5a87 isolate ratelimit... |
45 |
|
717115e1a printk ratelimiti... |
46 |
if (time_is_before_jiffies(rs->begin + rs->interval)) { |
6b1d174b0 ratelimit: extend... |
47 48 |
if (rs->missed) { if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) { |
656d61ce9 lib/ratelimit.c: ... |
49 50 51 52 |
printk_deferred(KERN_WARNING "%s: %d callbacks suppressed ", func, rs->missed); |
6b1d174b0 ratelimit: extend... |
53 54 55 |
rs->missed = 0; } } |
c2594bc37 ratelimit: fix bu... |
56 |
rs->begin = jiffies; |
717115e1a printk ratelimiti... |
57 |
rs->printed = 0; |
5f97a5a87 isolate ratelimit... |
58 |
} |
979f693de ratelimit: Use pe... |
59 60 61 62 63 64 65 |
if (rs->burst && rs->burst > rs->printed) { rs->printed++; ret = 1; } else { rs->missed++; ret = 0; } |
07354eb1a locking, printk: ... |
66 |
raw_spin_unlock_irqrestore(&rs->lock, flags); |
717115e1a printk ratelimiti... |
67 |
|
979f693de ratelimit: Use pe... |
68 |
return ret; |
5f97a5a87 isolate ratelimit... |
69 |
} |
5c8287133 ratelimit: Make s... |
70 |
EXPORT_SYMBOL(___ratelimit); |