2 * ratelimit.c - Do something with rate limit.
4 * Isolated from kernel/printk.c by Dave Young <hidave.darkstar@gmail.com>
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.
9 * This file is released under the GPLv2.
13 #include <linux/kernel.h>
14 #include <linux/jiffies.h>
15 #include <linux/module.h>
17 static DEFINE_SPINLOCK(ratelimit_lock);
18 static unsigned long flags;
21 * __ratelimit - rate limiting
22 * @rs: ratelimit_state data
24 * This enforces a rate limit: not more than @rs->ratelimit_burst callbacks
25 * in every @rs->ratelimit_jiffies
27 int __ratelimit(struct ratelimit_state *rs)
32 spin_lock_irqsave(&ratelimit_lock, flags);
36 if (time_is_before_jiffies(rs->begin + rs->interval)) {
38 printk(KERN_WARNING "%s: %d callbacks suppressed\n",
39 __func__, rs->missed);
44 if (rs->burst && rs->burst > rs->printed)
48 spin_unlock_irqrestore(&ratelimit_lock, flags);
53 spin_unlock_irqrestore(&ratelimit_lock, flags);
56 EXPORT_SYMBOL(__ratelimit);