All of lore.kernel.org
 help / color / mirror / Atom feed
From: dmukhin@ford.com
To: xen-devel@lists.xenproject.org
Cc: andrew.cooper3@citrix.com, anthony.perard@vates.tech,
	jbeulich@suse.com, julien@xen.org, michal.orzel@amd.com,
	roger.pau@citrix.com, sstabellini@kernel.org, dmukhin@ford.com
Subject: [PATCH v4 1/2] xen/console: correct leaky-bucket rate limiter
Date: Wed, 29 Jul 2026 00:25:19 -0700	[thread overview]
Message-ID: <20260729072520.1556970-2-dmukhin@ford.com> (raw)
In-Reply-To: <20260729072520.1556970-1-dmukhin@ford.com>

From: Denis Mukhin <dmukhin@ford.com> 

Use existing 'ratelimit_ms' and 'ratelimit_burst' variables in
do_printk_ratelimit() instead of hardcoded values 5000 and 10 respectively.

Ensure rate limiter is disabled if either 'ratelimit_ms' or 'ratelimit_burst'
is 0.

Account for integer overflow in the rate-limiter logic.

Signed-off-by: Denis Mukhin <dmukhin@ford.com>
---
Changes since v3:
- fixed types
- fixed integer division logic - I used DIM_MUL2() from xvmalloc.h
  I hope this is fine given another pending patch which will include xvmalloc.h
  for heap allocations
- fixed potential problem w/ overflow of toks (introduced elapsed)
- fixed potential problem with toks == 0 which is also "uninitialized"
  state.
---
 xen/drivers/char/console.c | 37 ++++++++++++++++++++++++++++++-------
 1 file changed, 30 insertions(+), 7 deletions(-)

diff --git a/xen/drivers/char/console.c b/xen/drivers/char/console.c
index ea4e3ff34178..76a1681670c1 100644
--- a/xen/drivers/char/console.c
+++ b/xen/drivers/char/console.c
@@ -33,6 +33,7 @@
 #include <asm/setup.h>
 #include <xen/sections.h>
 #include <xen/consoled.h>
+#include <xen/xvmalloc.h>
 
 #ifdef CONFIG_X86
 #include <asm/guest.h>
@@ -1286,21 +1287,43 @@ bool __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;
+    static bool initialized;
+    unsigned long limit;
     unsigned long flags;
-    unsigned long long now = NOW(); /* ns */
     unsigned long ms;
+    s_time_t now;
 
-    do_div(now, 1000000);
-    ms = (unsigned long)now;
+    if ( !ratelimit_ms || !ratelimit_burst )
+        return true;
+
+    limit = DIM_MUL2(ratelimit_burst, ratelimit_ms);
+
+    now = NOW(); /* ns */
+    do_div(now, MILLISECS(1));
+    ms = now;
 
     spin_lock_irqsave(&ratelimit_lock, flags);
-    toks += ms - last_msg;
+
+    if ( initialized )
+    {
+        unsigned long elapsed = ms - last_msg;
+
+        if ( toks >= limit || elapsed >= limit - toks )
+            toks = limit;
+        else
+            toks += elapsed;
+    }
+    else
+    {
+        toks = limit;
+        initialized = true;
+    }
+
     last_msg = ms;
-    if ( toks > (ratelimit_burst * ratelimit_ms))
-        toks = ratelimit_burst * ratelimit_ms;
+
     if ( toks >= ratelimit_ms )
     {
         unsigned int lost = missed;
-- 
2.54.0



  reply	other threads:[~2026-07-29  7:25 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-29  7:25 [PATCH v4 0/2] xen/console: updates to rate-limiting dmukhin
2026-07-29  7:25 ` dmukhin [this message]
     [not found]   ` <annJ3WK35xsv56Xn@macbook.local>
2026-08-10 13:05     ` [PATCH v4 1/2] xen/console: correct leaky-bucket rate limiter Roger Pau Monné
2026-07-29  7:25 ` [PATCH v4 2/2] xen/console: add compile-time rate-limiting controls dmukhin
     [not found]   ` <annMNjOodiS3hHp7@macbook.local>
2026-08-10 13:06     ` Roger Pau Monné
2026-08-10 17:56       ` dmukhin
2026-08-11  7:48         ` Roger Pau Monné

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260729072520.1556970-2-dmukhin@ford.com \
    --to=dmukhin@ford.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=anthony.perard@vates.tech \
    --cc=jbeulich@suse.com \
    --cc=julien@xen.org \
    --cc=michal.orzel@amd.com \
    --cc=roger.pau@citrix.com \
    --cc=sstabellini@kernel.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.