Commit 5f97a5a8799b8d7d0afdb9d68a50a4e0e8298a05

Authored by Dave Young
Committed by Linus Torvalds
1 parent 762873c251

isolate ratelimit from printk.c for other use

Due to the rcupreempt.h WARN_ON trigged, I got 2G syslog file.  For some
serious complaining of kernel, we need repeat the warnings, so here I isolate
the ratelimit part of printk.c to a standalone file.

Signed-off-by: Dave Young <hidave.darkstar@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Tested-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Showing 4 changed files with 54 additions and 26 deletions Side-by-side Diff

include/linux/kernel.h
... ... @@ -188,6 +188,7 @@
188 188 extern int printk_ratelimit_jiffies;
189 189 extern int printk_ratelimit_burst;
190 190 extern int printk_ratelimit(void);
  191 +extern int __ratelimit(int ratelimit_jiffies, int ratelimit_burst);
191 192 extern int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst);
192 193 extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
193 194 unsigned int interval_msec);
... ... @@ -1287,31 +1287,7 @@
1287 1287 */
1288 1288 int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst)
1289 1289 {
1290   - static DEFINE_SPINLOCK(ratelimit_lock);
1291   - static unsigned toks = 10 * 5 * HZ;
1292   - static unsigned long last_msg;
1293   - static int missed;
1294   - unsigned long flags;
1295   - unsigned long now = jiffies;
1296   -
1297   - spin_lock_irqsave(&ratelimit_lock, flags);
1298   - toks += now - last_msg;
1299   - last_msg = now;
1300   - if (toks > (ratelimit_burst * ratelimit_jiffies))
1301   - toks = ratelimit_burst * ratelimit_jiffies;
1302   - if (toks >= ratelimit_jiffies) {
1303   - int lost = missed;
1304   -
1305   - missed = 0;
1306   - toks -= ratelimit_jiffies;
1307   - spin_unlock_irqrestore(&ratelimit_lock, flags);
1308   - if (lost)
1309   - printk(KERN_WARNING "printk: %d messages suppressed.\n", lost);
1310   - return 1;
1311   - }
1312   - missed++;
1313   - spin_unlock_irqrestore(&ratelimit_lock, flags);
1314   - return 0;
  1290 + return __ratelimit(ratelimit_jiffies, ratelimit_burst);
1315 1291 }
1316 1292 EXPORT_SYMBOL(__printk_ratelimit);
1317 1293  
... ... @@ -6,7 +6,7 @@
6 6 rbtree.o radix-tree.o dump_stack.o \
7 7 idr.o int_sqrt.o extable.o prio_tree.o \
8 8 sha1.o irq_regs.o reciprocal_div.o argv_split.o \
9   - proportions.o prio_heap.o
  9 + proportions.o prio_heap.o ratelimit.o
10 10  
11 11 lib-$(CONFIG_MMU) += ioremap.o
12 12 lib-$(CONFIG_SMP) += cpumask.o
  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 + * This file is released under the GPLv2.
  7 + *
  8 + */
  9 +
  10 +#include <linux/kernel.h>
  11 +#include <linux/jiffies.h>
  12 +#include <linux/module.h>
  13 +
  14 +/*
  15 + * __ratelimit - rate limiting
  16 + * @ratelimit_jiffies: minimum time in jiffies between two callbacks
  17 + * @ratelimit_burst: number of callbacks we do before ratelimiting
  18 + *
  19 + * This enforces a rate limit: not more than @ratelimit_burst callbacks
  20 + * in every ratelimit_jiffies
  21 + */
  22 +int __ratelimit(int ratelimit_jiffies, int ratelimit_burst)
  23 +{
  24 + static DEFINE_SPINLOCK(ratelimit_lock);
  25 + static unsigned toks = 10 * 5 * HZ;
  26 + static unsigned long last_msg;
  27 + static int missed;
  28 + unsigned long flags;
  29 + unsigned long now = jiffies;
  30 +
  31 + spin_lock_irqsave(&ratelimit_lock, flags);
  32 + toks += now - last_msg;
  33 + last_msg = now;
  34 + if (toks > (ratelimit_burst * ratelimit_jiffies))
  35 + toks = ratelimit_burst * ratelimit_jiffies;
  36 + if (toks >= ratelimit_jiffies) {
  37 + int lost = missed;
  38 +
  39 + missed = 0;
  40 + toks -= ratelimit_jiffies;
  41 + spin_unlock_irqrestore(&ratelimit_lock, flags);
  42 + if (lost)
  43 + printk(KERN_WARNING "%s: %d messages suppressed\n",
  44 + __func__, lost);
  45 + return 1;
  46 + }
  47 + missed++;
  48 + spin_unlock_irqrestore(&ratelimit_lock, flags);
  49 + return 0;
  50 +}
  51 +EXPORT_SYMBOL(__ratelimit);