]> git.sesse.net Git - bcachefs-tools-debian/blob - linux/ratelimit.c
Disable pristine-tar option in gbp.conf, since there is no pristine-tar branch.
[bcachefs-tools-debian] / linux / ratelimit.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * ratelimit.c - Do something with rate limit.
4  *
5  * Isolated from kernel/printk.c by Dave Young <hidave.darkstar@gmail.com>
6  *
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.
9  */
10
11 #include <linux/ratelimit.h>
12 #include <linux/jiffies.h>
13 #include <linux/export.h>
14
15 /*
16  * __ratelimit - rate limiting
17  * @rs: ratelimit_state data
18  * @func: name of calling function
19  *
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.
26  */
27 int ___ratelimit(struct ratelimit_state *rs, const char *func)
28 {
29         int ret;
30
31         if (!rs->interval)
32                 return 1;
33
34         /*
35          * If we contend on this state's lock then almost
36          * by definition we are too busy to print a message,
37          * in addition to the one that will be printed by
38          * the entity that is holding the lock already:
39          */
40         if (!raw_spin_trylock(&rs->lock))
41                 return 0;
42
43         if (!rs->begin)
44                 rs->begin = jiffies;
45
46         if (time_is_before_jiffies(rs->begin + rs->interval)) {
47                 if (rs->missed) {
48                         if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) {
49                                 printk(KERN_WARNING
50                                        "%s: %d callbacks suppressed\n",
51                                        func, rs->missed);
52                                 rs->missed = 0;
53                         }
54                 }
55                 rs->begin   = jiffies;
56                 rs->printed = 0;
57         }
58         if (rs->burst && rs->burst > rs->printed) {
59                 rs->printed++;
60                 ret = 1;
61         } else {
62                 rs->missed++;
63                 ret = 0;
64         }
65         raw_spin_unlock(&rs->lock);
66
67         return ret;
68 }
69 EXPORT_SYMBOL(___ratelimit);