[PATCH v2 5/7] xen/console: correct leaky-bucket rate limiter

dmukhin@ford.com posted 7 patches 3 days, 12 hours ago
[PATCH v2 5/7] xen/console: correct leaky-bucket rate limiter
Posted by dmukhin@ford.com 3 days, 12 hours ago
From: Denis Mukhin <dmukhin@ford.com> 

Use existing printk_ratelimit_ms and printk_ratelimit_burst variables in
do_printk_ratelimit() instead of hardcoded values 5000 and 10 respectively.

Ensure rate limiter is disabled if either printk_ratelimit_ms or
printk_ratelimit_burst is 0. Make sure no unnecessary initialization is done
in the corner case.

Also, simplify the limiter code by using min().

Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
Changes since v1:
- new patch
---
 xen/drivers/char/console.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/xen/drivers/char/console.c b/xen/drivers/char/console.c
index f607e8f84d7a..e3962512d282 100644
--- a/xen/drivers/char/console.c
+++ b/xen/drivers/char/console.c
@@ -1291,21 +1291,27 @@ static bool do_printk_ratelimit(unsigned int ratelimit_ms,
                                 unsigned int ratelimit_burst)
 {
     static DEFINE_SPINLOCK(ratelimit_lock);
-    static unsigned long toks = 10 * 5 * 1000;
+    static unsigned long toks;
     static unsigned long last_msg;
     static unsigned int missed;
     unsigned long flags;
-    unsigned long long now = NOW(); /* ns */
+    unsigned long long now;
     unsigned long ms;
 
+    if ( !printk_ratelimit_burst || !printk_ratelimit_burst )
+        return true;
+
+    if ( !toks )
+        toks = printk_ratelimit_burst * printk_ratelimit_ms;
+
+    now = NOW(); /* ns */
     do_div(now, 1000000);
     ms = (unsigned long)now;
 
     spin_lock_irqsave(&ratelimit_lock, flags);
     toks += ms - last_msg;
     last_msg = ms;
-    if ( toks > (ratelimit_burst * ratelimit_ms))
-        toks = ratelimit_burst * ratelimit_ms;
+    toks = min(toks, (unsigned long)(ratelimit_burst * ratelimit_ms));
     if ( toks >= ratelimit_ms )
     {
         unsigned int lost = missed;
-- 
2.52.0