ratelimit.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #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 separately 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 = 0;
  35. /*
  36. * Zero interval says never limit, otherwise, non-positive burst
  37. * says always limit.
  38. */
  39. if (interval <= 0 || burst <= 0) {
  40. WARN_ONCE(interval < 0 || burst < 0, "Negative interval (%d) or burst (%d): Uninitialized ratelimit_state structure?\n", interval, burst);
  41. ret = interval == 0 || burst > 0;
  42. if (!(READ_ONCE(rs->flags) & RATELIMIT_INITIALIZED) || (!interval && !burst) ||
  43. !raw_spin_trylock_irqsave(&rs->lock, flags))
  44. goto nolock_ret;
  45. /* Force re-initialization once re-enabled. */
  46. rs->flags &= ~RATELIMIT_INITIALIZED;
  47. goto unlock_ret;
  48. }
  49. /*
  50. * If we contend on this state's lock then just check if
  51. * the current burst is used or not. It might cause
  52. * false positive when we are past the interval and
  53. * the current lock owner is just about to reset it.
  54. */
  55. if (!raw_spin_trylock_irqsave(&rs->lock, flags)) {
  56. if (READ_ONCE(rs->flags) & RATELIMIT_INITIALIZED &&
  57. atomic_read(&rs->rs_n_left) > 0 && atomic_dec_return(&rs->rs_n_left) >= 0)
  58. ret = 1;
  59. goto nolock_ret;
  60. }
  61. if (!(rs->flags & RATELIMIT_INITIALIZED)) {
  62. rs->begin = jiffies;
  63. rs->flags |= RATELIMIT_INITIALIZED;
  64. atomic_set(&rs->rs_n_left, rs->burst);
  65. }
  66. if (time_is_before_jiffies(rs->begin + interval)) {
  67. int m;
  68. /*
  69. * Reset rs_n_left ASAP to reduce false positives
  70. * in parallel calls, see above.
  71. */
  72. atomic_set(&rs->rs_n_left, rs->burst);
  73. rs->begin = jiffies;
  74. if (!(rs->flags & RATELIMIT_MSG_ON_RELEASE)) {
  75. m = ratelimit_state_reset_miss(rs);
  76. if (m) {
  77. printk_deferred(KERN_WARNING
  78. "%s: %d callbacks suppressed\n", func, m);
  79. }
  80. }
  81. }
  82. /* Note that the burst might be taken by a parallel call. */
  83. if (atomic_read(&rs->rs_n_left) > 0 && atomic_dec_return(&rs->rs_n_left) >= 0)
  84. ret = 1;
  85. unlock_ret:
  86. raw_spin_unlock_irqrestore(&rs->lock, flags);
  87. nolock_ret:
  88. if (!ret)
  89. ratelimit_state_inc_miss(rs);
  90. return ret;
  91. }
  92. EXPORT_SYMBOL(___ratelimit);