Skip to content

Commit 6b1d174

Browse files
suryasaimadhutorvalds
authored andcommitted
ratelimit: extend to print suppressed messages on release
Extend the ratelimiting facility to print the amount of suppressed lines when it is being released. This use case is aimed at short-termed, burst-like users for which we want to output the suppressed lines stats only once, after it has been disposed of. For an example, see /dev/kmsg usage in a follow-on patch. Also, change the printk() line we issue on release to not use "callbacks" as it is misleading: we're not suppressing callbacks but printk() calls. This has been separated from a previous patch by Linus. Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Borislav Petkov <[email protected]> Cc: Dave Young <[email protected]> Cc: Franck Bui <[email protected]> Cc: Greg Kroah-Hartman <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Steven Rostedt <[email protected]> Cc: Uwe Kleine-König <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent b5644a1 commit 6b1d174

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

include/linux/ratelimit.h

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
#define _LINUX_RATELIMIT_H
33

44
#include <linux/param.h>
5+
#include <linux/sched.h>
56
#include <linux/spinlock.h>
67

78
#define DEFAULT_RATELIMIT_INTERVAL (5 * HZ)
89
#define DEFAULT_RATELIMIT_BURST 10
910

11+
/* issue num suppressed message on exit */
12+
#define RATELIMIT_MSG_ON_RELEASE BIT(0)
13+
1014
struct ratelimit_state {
1115
raw_spinlock_t lock; /* protect the state */
1216

@@ -15,6 +19,7 @@ struct ratelimit_state {
1519
int printed;
1620
int missed;
1721
unsigned long begin;
22+
unsigned long flags;
1823
};
1924

2025
#define RATELIMIT_STATE_INIT(name, interval_init, burst_init) { \
@@ -34,12 +39,35 @@ struct ratelimit_state {
3439
static inline void ratelimit_state_init(struct ratelimit_state *rs,
3540
int interval, int burst)
3641
{
42+
memset(rs, 0, sizeof(*rs));
43+
3744
raw_spin_lock_init(&rs->lock);
38-
rs->interval = interval;
39-
rs->burst = burst;
40-
rs->printed = 0;
41-
rs->missed = 0;
42-
rs->begin = 0;
45+
rs->interval = interval;
46+
rs->burst = burst;
47+
}
48+
49+
static inline void ratelimit_default_init(struct ratelimit_state *rs)
50+
{
51+
return ratelimit_state_init(rs, DEFAULT_RATELIMIT_INTERVAL,
52+
DEFAULT_RATELIMIT_BURST);
53+
}
54+
55+
static inline void ratelimit_state_exit(struct ratelimit_state *rs)
56+
{
57+
if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE))
58+
return;
59+
60+
if (rs->missed) {
61+
pr_warn("%s: %d output lines suppressed due to ratelimiting\n",
62+
current->comm, rs->missed);
63+
rs->missed = 0;
64+
}
65+
}
66+
67+
static inline void
68+
ratelimit_set_flags(struct ratelimit_state *rs, unsigned long flags)
69+
{
70+
rs->flags = flags;
4371
}
4472

4573
extern struct ratelimit_state printk_ratelimit_state;

lib/ratelimit.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ int ___ratelimit(struct ratelimit_state *rs, const char *func)
4646
rs->begin = jiffies;
4747

4848
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);
49+
if (rs->missed) {
50+
if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) {
51+
pr_warn("%s: %d callbacks suppressed\n", func, rs->missed);
52+
rs->missed = 0;
53+
}
54+
}
5255
rs->begin = jiffies;
5356
rs->printed = 0;
54-
rs->missed = 0;
5557
}
5658
if (rs->burst && rs->burst > rs->printed) {
5759
rs->printed++;

0 commit comments

Comments
 (0)