Commit 979f693def9084a452846365dfde5dcb28366333

Authored by Ingo Molnar
1 parent b8c7f1dc5c

ratelimit: Use per ratelimit context locking

I'd like to use printk_ratelimit() in atomic context, but that's
not possible right now due to the spinlock usage this commit
introduced more than a year ago:

  717115e: printk ratelimiting rewrite

As a first step push the lock into the ratelimit state structure.
This allows us to deal with locking failures to be considered as an
event related to that state being too busy.

Also clean up the code a bit (without changing functionality):

 - tidy up the definitions

 - clean up the code flow

This also shrinks the code a tiny bit:

   text	   data	    bss	    dec	    hex	filename
    264	      0	      4	    268	    10c	ratelimit.o.before
    255	      0	      0	    255	     ff	ratelimit.o.after

( Whole-kernel data size got a bit larger, because we have
  two ratelimit-state data structures right now. )

Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: David S. Miller <davem@davemloft.net>
LKML-Reference: <new-submission>
Signed-off-by: Ingo Molnar <mingo@elte.hu>

Showing 2 changed files with 33 additions and 26 deletions Side-by-side Diff

include/linux/ratelimit.h
1 1 #ifndef _LINUX_RATELIMIT_H
2 2 #define _LINUX_RATELIMIT_H
  3 +
3 4 #include <linux/param.h>
  5 +#include <linux/spinlock_types.h>
4 6  
5   -#define DEFAULT_RATELIMIT_INTERVAL (5 * HZ)
6   -#define DEFAULT_RATELIMIT_BURST 10
  7 +#define DEFAULT_RATELIMIT_INTERVAL (5 * HZ)
  8 +#define DEFAULT_RATELIMIT_BURST 10
7 9  
8 10 struct ratelimit_state {
9   - int interval;
10   - int burst;
11   - int printed;
12   - int missed;
13   - unsigned long begin;
  11 + spinlock_t lock; /* protect the state */
  12 +
  13 + int interval;
  14 + int burst;
  15 + int printed;
  16 + int missed;
  17 + unsigned long begin;
14 18 };
15 19  
16   -#define DEFINE_RATELIMIT_STATE(name, interval, burst) \
17   - struct ratelimit_state name = {interval, burst,}
  20 +#define DEFINE_RATELIMIT_STATE(name, interval_init, burst_init) \
  21 + \
  22 + struct ratelimit_state name = { \
  23 + .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
  24 + .interval = interval_init, \
  25 + .burst = burst_init, \
  26 + }
18 27  
19 28 extern int __ratelimit(struct ratelimit_state *rs);
20   -#endif
  29 +
  30 +#endif /* _LINUX_RATELIMIT_H */
... ... @@ -7,15 +7,12 @@
7 7 * parameter. Now every user can use their own standalone ratelimit_state.
8 8 *
9 9 * This file is released under the GPLv2.
10   - *
11 10 */
12 11  
13 12 #include <linux/kernel.h>
14 13 #include <linux/jiffies.h>
15 14 #include <linux/module.h>
16 15  
17   -static DEFINE_SPINLOCK(ratelimit_lock);
18   -
19 16 /*
20 17 * __ratelimit - rate limiting
21 18 * @rs: ratelimit_state data
22 19  
... ... @@ -26,11 +23,12 @@
26 23 int __ratelimit(struct ratelimit_state *rs)
27 24 {
28 25 unsigned long flags;
  26 + int ret;
29 27  
30 28 if (!rs->interval)
31 29 return 1;
32 30  
33   - spin_lock_irqsave(&ratelimit_lock, flags);
  31 + spin_lock_irqsave(&rs->lock, flags);
34 32 if (!rs->begin)
35 33 rs->begin = jiffies;
36 34  
37 35  
38 36  
39 37  
... ... @@ -38,21 +36,20 @@
38 36 if (rs->missed)
39 37 printk(KERN_WARNING "%s: %d callbacks suppressed\n",
40 38 __func__, rs->missed);
41   - rs->begin = 0;
  39 + rs->begin = 0;
42 40 rs->printed = 0;
43   - rs->missed = 0;
  41 + rs->missed = 0;
44 42 }
45   - if (rs->burst && rs->burst > rs->printed)
46   - goto print;
  43 + if (rs->burst && rs->burst > rs->printed) {
  44 + rs->printed++;
  45 + ret = 1;
  46 + } else {
  47 + rs->missed++;
  48 + ret = 0;
  49 + }
  50 + spin_unlock_irqrestore(&rs->lock, flags);
47 51  
48   - rs->missed++;
49   - spin_unlock_irqrestore(&ratelimit_lock, flags);
50   - return 0;
51   -
52   -print:
53   - rs->printed++;
54   - spin_unlock_irqrestore(&ratelimit_lock, flags);
55   - return 1;
  52 + return ret;
56 53 }
57 54 EXPORT_SYMBOL(__ratelimit);