ALSA: memalloc: Drop snd_dma_pci_data() macro
[linux-2.6-block.git] / lib / ratelimit.c
CommitLineData
55716d26 1// SPDX-License-Identifier: GPL-2.0-only
5f97a5a8
DY
2/*
3 * ratelimit.c - Do something with rate limit.
4 *
5 * Isolated from kernel/printk.c by Dave Young <hidave.darkstar@gmail.com>
6 *
717115e1
DY
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.
5f97a5a8
DY
9 */
10
3fff4c42 11#include <linux/ratelimit.h>
5f97a5a8 12#include <linux/jiffies.h>
8bc3bcc9 13#include <linux/export.h>
5f97a5a8
DY
14
15/*
16 * __ratelimit - rate limiting
717115e1 17 * @rs: ratelimit_state data
2a7268ab 18 * @func: name of calling function
5f97a5a8 19 *
2a7268ab
YZ
20 * This enforces a rate limit: not more than @rs->burst callbacks
21 * in every @rs->interval
22 *
23 * RETURNS:
24 * 0 means callbacks will be suppressed.
25 * 1 means go ahead and do it.
5f97a5a8 26 */
5c828713 27int ___ratelimit(struct ratelimit_state *rs, const char *func)
5f97a5a8 28{
4d9c377c 29 unsigned long flags;
979f693d 30 int ret;
4d9c377c 31
717115e1
DY
32 if (!rs->interval)
33 return 1;
5f97a5a8 34
edaac8e3
IM
35 /*
36 * If we contend on this state's lock then almost
37 * by definition we are too busy to print a message,
38 * in addition to the one that will be printed by
39 * the entity that is holding the lock already:
40 */
07354eb1 41 if (!raw_spin_trylock_irqsave(&rs->lock, flags))
57119c34 42 return 0;
edaac8e3 43
717115e1
DY
44 if (!rs->begin)
45 rs->begin = jiffies;
5f97a5a8 46
717115e1 47 if (time_is_before_jiffies(rs->begin + rs->interval)) {
6b1d174b
BP
48 if (rs->missed) {
49 if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) {
656d61ce
SS
50 printk_deferred(KERN_WARNING
51 "%s: %d callbacks suppressed\n",
52 func, rs->missed);
6b1d174b
BP
53 rs->missed = 0;
54 }
55 }
c2594bc3 56 rs->begin = jiffies;
717115e1 57 rs->printed = 0;
5f97a5a8 58 }
979f693d
IM
59 if (rs->burst && rs->burst > rs->printed) {
60 rs->printed++;
61 ret = 1;
62 } else {
63 rs->missed++;
64 ret = 0;
65 }
07354eb1 66 raw_spin_unlock_irqrestore(&rs->lock, flags);
717115e1 67
979f693d 68 return ret;
5f97a5a8 69}
5c828713 70EXPORT_SYMBOL(___ratelimit);