diff options
author | Kent Overstreet <kent.overstreet@gmail.com> | 2022-06-22 22:05:39 -0400 |
---|---|---|
committer | Kent Overstreet <kent.overstreet@gmail.com> | 2022-06-22 22:05:39 -0400 |
commit | 23dc00de78fbfea39b61aae2d261b621fee7853d (patch) | |
tree | 047ca618c7a40ad61fe71470a889d2871a0b0c4a /linux/ratelimit.c | |
parent | ff2ebf3f75814bc45353013782ebeb1556adfc05 (diff) |
Fix printk_ratelimited()
printk_ratelimited was behind an #ifdef CONFIG_PRINTK, which we don't
define, so it was a complete noop - oops.
Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
Diffstat (limited to 'linux/ratelimit.c')
-rw-r--r-- | linux/ratelimit.c | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/linux/ratelimit.c b/linux/ratelimit.c new file mode 100644 index 00000000..21a6d6c8 --- /dev/null +++ b/linux/ratelimit.c @@ -0,0 +1,69 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * ratelimit.c - Do something with rate limit. + * + * Isolated from kernel/printk.c by Dave Young <hidave.darkstar@gmail.com> + * + * 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. + */ + +#include <linux/ratelimit.h> +#include <linux/jiffies.h> +#include <linux/export.h> + +/* + * __ratelimit - rate limiting + * @rs: ratelimit_state data + * @func: name of calling function + * + * 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. + */ +int ___ratelimit(struct ratelimit_state *rs, const char *func) +{ + int ret; + + if (!rs->interval) + return 1; + + /* + * 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: + */ + if (!raw_spin_trylock(&rs->lock)) + return 0; + + if (!rs->begin) + rs->begin = jiffies; + + if (time_is_before_jiffies(rs->begin + rs->interval)) { + if (rs->missed) { + if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) { + printk(KERN_WARNING + "%s: %d callbacks suppressed\n", + func, rs->missed); + rs->missed = 0; + } + } + rs->begin = jiffies; + rs->printed = 0; + } + if (rs->burst && rs->burst > rs->printed) { + rs->printed++; + ret = 1; + } else { + rs->missed++; + ret = 0; + } + raw_spin_unlock(&rs->lock); + + return ret; +} +EXPORT_SYMBOL(___ratelimit); |