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

TOMOYO Linux Cross Reference
Linux/lib/ratelimit.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ 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.9 ] ~ [ 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         /* Paired with WRITE_ONCE() in .proc_handler().
 30          * Changing two values seperately could be inconsistent
 31          * and some message could be lost.  (See: net_ratelimit_state).
 32          */
 33         int interval = READ_ONCE(rs->interval);
 34         int burst = READ_ONCE(rs->burst);
 35         unsigned long flags;
 36         int ret;
 37 
 38         if (!interval)
 39                 return 1;
 40 
 41         /*
 42          * If we contend on this state's lock then almost
 43          * by definition we are too busy to print a message,
 44          * in addition to the one that will be printed by
 45          * the entity that is holding the lock already:
 46          */
 47         if (!raw_spin_trylock_irqsave(&rs->lock, flags))
 48                 return 0;
 49 
 50         if (!rs->begin)
 51                 rs->begin = jiffies;
 52 
 53         if (time_is_before_jiffies(rs->begin + interval)) {
 54                 if (rs->missed) {
 55                         if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) {
 56                                 printk_deferred(KERN_WARNING
 57                                                 "%s: %d callbacks suppressed\n",
 58                                                 func, rs->missed);
 59                                 rs->missed = 0;
 60                         }
 61                 }
 62                 rs->begin   = jiffies;
 63                 rs->printed = 0;
 64         }
 65         if (burst && burst > rs->printed) {
 66                 rs->printed++;
 67                 ret = 1;
 68         } else {
 69                 rs->missed++;
 70                 ret = 0;
 71         }
 72         raw_spin_unlock_irqrestore(&rs->lock, flags);
 73 
 74         return ret;
 75 }
 76 EXPORT_SYMBOL(___ratelimit);
 77 

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

kernel.org | git.kernel.org | LWN.net | Project Home | 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.

sflogo.php