ratelimit.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 <[email protected]>
  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. #include <linux/ratelimit.h>
  11. #include <linux/jiffies.h>
  12. #include <linux/export.h>
  13. /*
  14. * __ratelimit - rate limiting
  15. * @rs: ratelimit_state data
  16. * @func: name of calling function
  17. *
  18. * This enforces a rate limit: not more than @rs->burst callbacks
  19. * in every @rs->interval
  20. *
  21. * RETURNS:
  22. * 0 means callbacks will be suppressed.
  23. * 1 means go ahead and do it.
  24. */
  25. int ___ratelimit(struct ratelimit_state *rs, const char *func)
  26. {
  27. /* Paired with WRITE_ONCE() in .proc_handler().
  28. * Changing two values seperately could be inconsistent
  29. * and some message could be lost. (See: net_ratelimit_state).
  30. */
  31. int interval = READ_ONCE(rs->interval);
  32. int burst = READ_ONCE(rs->burst);
  33. unsigned long flags;
  34. int ret;
  35. if (!interval)
  36. return 1;
  37. /*
  38. * If we contend on this state's lock then almost
  39. * by definition we are too busy to print a message,
  40. * in addition to the one that will be printed by
  41. * the entity that is holding the lock already:
  42. */
  43. if (!raw_spin_trylock_irqsave(&rs->lock, flags))
  44. return 0;
  45. if (!rs->begin)
  46. rs->begin = jiffies;
  47. if (time_is_before_jiffies(rs->begin + 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 (burst && 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. return ret;
  68. }
  69. EXPORT_SYMBOL(___ratelimit);