Blame view

lib/ratelimit.c 1.53 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
13
14
15
16
  #include <linux/jiffies.h>
  #include <linux/module.h>
  
  /*
   * __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
48
49
50
  	if (time_is_before_jiffies(rs->begin + rs->interval)) {
  		if (rs->missed)
  			printk(KERN_WARNING "%s: %d callbacks suppressed
  ",
5c8287133   Christian Borntraeger   ratelimit: Make s...
51
  				func, rs->missed);
979f693de   Ingo Molnar   ratelimit: Use pe...
52
  		rs->begin   = 0;
717115e1a   Dave Young   printk ratelimiti...
53
  		rs->printed = 0;
979f693de   Ingo Molnar   ratelimit: Use pe...
54
  		rs->missed  = 0;
5f97a5a87   Dave Young   isolate ratelimit...
55
  	}
979f693de   Ingo Molnar   ratelimit: Use pe...
56
57
58
59
60
61
62
  	if (rs->burst && rs->burst > rs->printed) {
  		rs->printed++;
  		ret = 1;
  	} else {
  		rs->missed++;
  		ret = 0;
  	}
07354eb1a   Thomas Gleixner   locking, printk: ...
63
  	raw_spin_unlock_irqrestore(&rs->lock, flags);
717115e1a   Dave Young   printk ratelimiti...
64

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