1 // SPDX-License-Identifier: GPL-2.0-only
3 * ratelimit.c - Do something with rate limit.
5 * Isolated from kernel/printk.c by Dave Young <hidave.darkstar@gmail.com>
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.
11 #include <linux/ratelimit.h>
12 #include <linux/jiffies.h>
13 #include <linux/export.h>
16 * __ratelimit - rate limiting
17 * @rs: ratelimit_state data
18 * @func: name of calling function
20 * This enforces a rate limit: not more than @rs->burst callbacks
21 * in every @rs->interval
24 * 0 means callbacks will be suppressed.
25 * 1 means go ahead and do it.
27 int ___ratelimit(struct ratelimit_state
*rs
, const char *func
)
29 /* Paired with WRITE_ONCE() in .proc_handler().
30 * Changing two values seperately could be inconsistent
31 * and some message could be lost. (See: net_ratelimit_state).
33 int interval
= READ_ONCE(rs
->interval
);
34 int burst
= READ_ONCE(rs
->burst
);
42 * If we contend on this state's lock then almost
43 * by definition we are too busy to print a message,
44 * in addition to the one that will be printed by
45 * the entity that is holding the lock already:
47 if (!raw_spin_trylock_irqsave(&rs
->lock
, flags
))
53 if (time_is_before_jiffies(rs
->begin
+ interval
)) {
55 if (!(rs
->flags
& RATELIMIT_MSG_ON_RELEASE
)) {
56 printk_deferred(KERN_WARNING
57 "%s: %d callbacks suppressed\n",
65 if (burst
&& burst
> rs
->printed
) {
72 raw_spin_unlock_irqrestore(&rs
->lock
, flags
);
76 EXPORT_SYMBOL(___ratelimit
);