~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/lib/ratelimit.c

Version: ~ [ linux-5.9-rc6 ] ~ [ linux-5.8.10 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.66 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.146 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.198 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.236 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.236 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-only
  2 /*
  3  * ratelimit.c - Do something with rate limit.
  4  *
  5  * Isolated from kernel/printk.c by Dave Young <hidave.darkstar@gmail.com>
  6  *
  7  * 2008-05-01 rewrite the function and use a ratelimit_state data struct as
  8  * parameter. Now every user can use their own standalone ratelimit_state.
  9  */
 10 
 11 #include <linux/ratelimit.h>
 12 #include <linux/jiffies.h>
 13 #include <linux/export.h>
 14 
 15 /*
 16  * __ratelimit - rate limiting
 17  * @rs: ratelimit_state data
 18  * @func: name of calling function
 19  *
 20  * This enforces a rate limit: not more than @rs->burst callbacks
 21  * in every @rs->interval
 22  *
 23  * RETURNS:
 24  * 0 means callbacks will be suppressed.
 25  * 1 means go ahead and do it.
 26  */
 27 int ___ratelimit(struct ratelimit_state *rs, const char *func)
 28 {
 29         unsigned long flags;
 30         int ret;
 31 
 32         if (!rs->interval)
 33                 return 1;
 34 
 35         /*
 36          * If we contend on this state's lock then almost
 37          * by definition we are too busy to print a message,
 38          * in addition to the one that will be printed by
 39          * the entity that is holding the lock already:
 40          */
 41         if (!raw_spin_trylock_irqsave(&rs->lock, flags))
 42                 return 0;
 43 
 44         if (!rs->begin)
 45                 rs->begin = jiffies;
 46 
 47         if (time_is_before_jiffies(rs->begin + rs->interval)) {
 48                 if (rs->missed) {
 49                         if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) {
 50                                 printk_deferred(KERN_WARNING
 51                                                 "%s: %d callbacks suppressed\n",
 52                                                 func, rs->missed);
 53                                 rs->missed = 0;
 54                         }
 55                 }
 56                 rs->begin   = jiffies;
 57                 rs->printed = 0;
 58         }
 59         if (rs->burst && rs->burst > rs->printed) {
 60                 rs->printed++;
 61                 ret = 1;
 62         } else {
 63                 rs->missed++;
 64                 ret = 0;
 65         }
 66         raw_spin_unlock_irqrestore(&rs->lock, flags);
 67 
 68         return ret;
 69 }
 70 EXPORT_SYMBOL(___ratelimit);
 71 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp