Linux Audio

Check our new training course

Loading...
v3.15
 
 1/*
 2 * ratelimit.c - Do something with rate limit.
 3 *
 4 * Isolated from kernel/printk.c by Dave Young <hidave.darkstar@gmail.com>
 5 *
 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.
 8 *
 9 * This file is released under the GPLv2.
10 */
11
12#include <linux/ratelimit.h>
13#include <linux/jiffies.h>
14#include <linux/export.h>
15
16/*
17 * __ratelimit - rate limiting
18 * @rs: ratelimit_state data
19 * @func: name of calling function
20 *
21 * This enforces a rate limit: not more than @rs->burst callbacks
22 * in every @rs->interval
23 *
24 * RETURNS:
25 * 0 means callbacks will be suppressed.
26 * 1 means go ahead and do it.
27 */
28int ___ratelimit(struct ratelimit_state *rs, const char *func)
29{
 
 
 
 
 
 
30	unsigned long flags;
31	int ret;
32
33	if (!rs->interval)
34		return 1;
35
36	/*
37	 * If we contend on this state's lock then almost
38	 * by definition we are too busy to print a message,
39	 * in addition to the one that will be printed by
40	 * the entity that is holding the lock already:
41	 */
42	if (!raw_spin_trylock_irqsave(&rs->lock, flags))
43		return 0;
44
45	if (!rs->begin)
46		rs->begin = jiffies;
47
48	if (time_is_before_jiffies(rs->begin + rs->interval)) {
49		if (rs->missed)
50			printk(KERN_WARNING "%s: %d callbacks suppressed\n",
51				func, rs->missed);
52		rs->begin   = 0;
 
 
 
 
 
53		rs->printed = 0;
54		rs->missed  = 0;
55	}
56	if (rs->burst && rs->burst > rs->printed) {
57		rs->printed++;
58		ret = 1;
59	} else {
60		rs->missed++;
61		ret = 0;
62	}
63	raw_spin_unlock_irqrestore(&rs->lock, flags);
64
65	return ret;
66}
67EXPORT_SYMBOL(___ratelimit);
v6.13.7
 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 */
27int ___ratelimit(struct ratelimit_state *rs, const char *func)
28{
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).
32	 */
33	int interval = READ_ONCE(rs->interval);
34	int burst = READ_ONCE(rs->burst);
35	unsigned long flags;
36	int ret;
37
38	if (!interval)
39		return 1;
40
41	/*
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:
46	 */
47	if (!raw_spin_trylock_irqsave(&rs->lock, flags))
48		return 0;
49
50	if (!rs->begin)
51		rs->begin = jiffies;
52
53	if (time_is_before_jiffies(rs->begin + interval)) {
54		if (rs->missed) {
55			if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) {
56				printk_deferred(KERN_WARNING
57						"%s: %d callbacks suppressed\n",
58						func, rs->missed);
59				rs->missed = 0;
60			}
61		}
62		rs->begin   = jiffies;
63		rs->printed = 0;
 
64	}
65	if (burst && burst > rs->printed) {
66		rs->printed++;
67		ret = 1;
68	} else {
69		rs->missed++;
70		ret = 0;
71	}
72	raw_spin_unlock_irqrestore(&rs->lock, flags);
73
74	return ret;
75}
76EXPORT_SYMBOL(___ratelimit);