Blame view

lib/ratelimit.c 1.42 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
5f97a5a87   Dave Young   isolate ratelimit...
18
   *
717115e1a   Dave Young   printk ratelimiti...
19
20
   * This enforces a rate limit: not more than @rs->ratelimit_burst callbacks
   * in every @rs->ratelimit_jiffies
5f97a5a87   Dave Young   isolate ratelimit...
21
   */
5c8287133   Christian Borntraeger   ratelimit: Make s...
22
  int ___ratelimit(struct ratelimit_state *rs, const char *func)
5f97a5a87   Dave Young   isolate ratelimit...
23
  {
4d9c377c8   Alexey Dobriyan   __ratelimit() cpu...
24
  	unsigned long flags;
979f693de   Ingo Molnar   ratelimit: Use pe...
25
  	int ret;
4d9c377c8   Alexey Dobriyan   __ratelimit() cpu...
26

717115e1a   Dave Young   printk ratelimiti...
27
28
  	if (!rs->interval)
  		return 1;
5f97a5a87   Dave Young   isolate ratelimit...
29

edaac8e31   Ingo Molnar   ratelimit: Fix/al...
30
31
32
33
34
35
36
37
  	/*
  	 * 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:
  	 */
  	if (!spin_trylock_irqsave(&rs->lock, flags))
  		return 1;
717115e1a   Dave Young   printk ratelimiti...
38
39
  	if (!rs->begin)
  		rs->begin = jiffies;
5f97a5a87   Dave Young   isolate ratelimit...
40

717115e1a   Dave Young   printk ratelimiti...
41
42
43
44
  	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...
45
  				func, rs->missed);
979f693de   Ingo Molnar   ratelimit: Use pe...
46
  		rs->begin   = 0;
717115e1a   Dave Young   printk ratelimiti...
47
  		rs->printed = 0;
979f693de   Ingo Molnar   ratelimit: Use pe...
48
  		rs->missed  = 0;
5f97a5a87   Dave Young   isolate ratelimit...
49
  	}
979f693de   Ingo Molnar   ratelimit: Use pe...
50
51
52
53
54
55
56
57
  	if (rs->burst && rs->burst > rs->printed) {
  		rs->printed++;
  		ret = 1;
  	} else {
  		rs->missed++;
  		ret = 0;
  	}
  	spin_unlock_irqrestore(&rs->lock, flags);
717115e1a   Dave Young   printk ratelimiti...
58

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