Blame view

lib/ratelimit.c 1.61 KB
5f97a5a87   Dave Young   isolate ratelimit...
1
2
3
4
5
  /*
   * ratelimit.c - Do something with rate limit.
   *
   * Isolated from kernel/printk.c by Dave Young <hidave.darkstar@gmail.com>
   *
717115e1a   Dave Young   printk ratelimiti...
6
7
8
   * 2008-05-01 rewrite the function and use a ratelimit_state data struct as
   * parameter. Now every user can use their own standalone ratelimit_state.
   *
5f97a5a87   Dave Young   isolate ratelimit...
9
   * This file is released under the GPLv2.
5f97a5a87   Dave Young   isolate ratelimit...
10
   */
3fff4c42b   Ingo Molnar   printk: Remove ra...
11
  #include <linux/ratelimit.h>
5f97a5a87   Dave Young   isolate ratelimit...
12
  #include <linux/jiffies.h>
8bc3bcc93   Paul Gortmaker   lib: reduce the u...
13
  #include <linux/export.h>
5f97a5a87   Dave Young   isolate ratelimit...
14
15
16
  
  /*
   * __ratelimit - rate limiting
717115e1a   Dave Young   printk ratelimiti...
17
   * @rs: ratelimit_state data
2a7268abc   Yong Zhang   ratelimit: annota...
18
   * @func: name of calling function
5f97a5a87   Dave Young   isolate ratelimit...
19
   *
2a7268abc   Yong Zhang   ratelimit: annota...
20
21
22
23
24
25
   * This enforces a rate limit: not more than @rs->burst callbacks
   * in every @rs->interval
   *
   * RETURNS:
   * 0 means callbacks will be suppressed.
   * 1 means go ahead and do it.
5f97a5a87   Dave Young   isolate ratelimit...
26
   */
5c8287133   Christian Borntraeger   ratelimit: Make s...
27
  int ___ratelimit(struct ratelimit_state *rs, const char *func)
5f97a5a87   Dave Young   isolate ratelimit...
28
  {
4d9c377c8   Alexey Dobriyan   __ratelimit() cpu...
29
  	unsigned long flags;
979f693de   Ingo Molnar   ratelimit: Use pe...
30
  	int ret;
4d9c377c8   Alexey Dobriyan   __ratelimit() cpu...
31

717115e1a   Dave Young   printk ratelimiti...
32
33
  	if (!rs->interval)
  		return 1;
5f97a5a87   Dave Young   isolate ratelimit...
34

edaac8e31   Ingo Molnar   ratelimit: Fix/al...
35
36
37
38
39
40
  	/*
  	 * If we contend on this state's lock then almost
  	 * by definition we are too busy to print a message,
  	 * in addition to the one that will be printed by
  	 * the entity that is holding the lock already:
  	 */
07354eb1a   Thomas Gleixner   locking, printk: ...
41
  	if (!raw_spin_trylock_irqsave(&rs->lock, flags))
57119c34e   Yong Zhang   ratelimit: fix th...
42
  		return 0;
edaac8e31   Ingo Molnar   ratelimit: Fix/al...
43

717115e1a   Dave Young   printk ratelimiti...
44
45
  	if (!rs->begin)
  		rs->begin = jiffies;
5f97a5a87   Dave Young   isolate ratelimit...
46

717115e1a   Dave Young   printk ratelimiti...
47
  	if (time_is_before_jiffies(rs->begin + rs->interval)) {
6b1d174b0   Borislav Petkov   ratelimit: extend...
48
49
  		if (rs->missed) {
  			if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) {
656d61ce9   Sergey Senozhatsky   lib/ratelimit.c: ...
50
51
52
53
  				printk_deferred(KERN_WARNING
  						"%s: %d callbacks suppressed
  ",
  						func, rs->missed);
6b1d174b0   Borislav Petkov   ratelimit: extend...
54
55
56
  				rs->missed = 0;
  			}
  		}
c2594bc37   Jaewon Kim   ratelimit: fix bu...
57
  		rs->begin   = jiffies;
717115e1a   Dave Young   printk ratelimiti...
58
  		rs->printed = 0;
5f97a5a87   Dave Young   isolate ratelimit...
59
  	}
979f693de   Ingo Molnar   ratelimit: Use pe...
60
61
62
63
64
65
66
  	if (rs->burst && rs->burst > rs->printed) {
  		rs->printed++;
  		ret = 1;
  	} else {
  		rs->missed++;
  		ret = 0;
  	}
07354eb1a   Thomas Gleixner   locking, printk: ...
67
  	raw_spin_unlock_irqrestore(&rs->lock, flags);
717115e1a   Dave Young   printk ratelimiti...
68

979f693de   Ingo Molnar   ratelimit: Use pe...
69
  	return ret;
5f97a5a87   Dave Young   isolate ratelimit...
70
  }
5c8287133   Christian Borntraeger   ratelimit: Make s...
71
  EXPORT_SYMBOL(___ratelimit);