Skip to contentSkip to conversation

xen/console: updates to rate-limiting

PATCH Superseded [email protected] 2 participants 7 messages Jul 29 – Aug 11 2 messages not in the archive

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover xen/console: updates to rate-limiting

The series introduces compile-configuration for diagnostic messages rate-limiting.

Patch 1 is a fixup for the rate-limiter to adjust to user-defined rate-limiting parameters.

Patch 2 introduces compile-time rate-limiting controls.

[1] v3: https://lore.kernel.org/xen-devel/[email protected]/
[2] CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/2714300378

Denis Mukhin (2):
xen/console: correct leaky-bucket rate limiter xen/console: add build-time rate-limiting controls

 xen/common/Kconfig         | 36 +++++++++++++++++++++++++++++++
 xen/drivers/char/console.c | 43 ++++++++++++++++++++++++++++++--------
 2 files changed, 70 insertions(+), 9 deletions(-)
Signature · 1 line
2.54.0
#1 lore raw

PATCH v4 1/2 xen/console: correct leaky-bucket rate limiter

From: Denis Mukhin <[email protected]>

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.

---
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(-)
xen/drivers/char/console.c +30 -7
index ea4e3ff34178..76a1681670c1 100644
@@ -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;
Signature · 1 line
2.54.0

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw

Re: [PATCH v4 1/2] xen/console: correct leaky-bucket rate limiter

You mention "correct" in the subject, but there's no fixes tag, and it's not clear exactly what this patch corrects.

Quoted 69 lines · On Wed, Jul 29, 2026 at 12:25:19AM -0700, [email protected] wrote:

From: Denis Mukhin <[email protected]>

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.

---
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(-)
xen/drivers/char/console.c +18 -5
index ea4e3ff34178..76a1681670c1 100644
@@ -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;
+

xen/drivers/char/console.c bool __printk_ratelimit(unsigned int ratelimit_ms, · line 1323

Quoted 10 lines
xen/drivers/char/console.c +10 -0
+        if ( toks >= limit || elapsed >= limit - toks )
+            toks = limit;
+        else
+            toks += elapsed;
+    }
+    else
+    {
+        toks = limit;
+        initialized = true;
+    }

I'm not sure you need the initialized static variable. You could set the initial value of toks = ~0, and then if the limit is set to a lower value it would already get adjusted as part of the toks >= limit check?

Thanks, Roger.

#4 lore raw

PATCH v4 2/2 xen/console: add compile-time rate-limiting controls

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Introduce CONFIG_PRINTK_RATELIMIT_MS and CONFIG_PRINTK_RATELIMIT_BURST for configuring rate-limiting policy at the compile time.

Use symbols for global rate-limiting initialization in the console driver.

---
Changes since v3:
- added note on security support for non-standard configurations
- gated menu with EXPERT

I kept both settings for now.

---
 xen/common/Kconfig         | 36 ++++++++++++++++++++++++++++++++++++
 xen/drivers/char/console.c |  6 ++++--
 2 files changed, 40 insertions(+), 2 deletions(-)
xen/common/Kconfig +36 -0
index da80fdba8469..749d3bfb08e0 100644
@@ -672,4 +672,40 @@ config PM_STATS
 	  Enable collection of performance management statistics to aid in
 	  analyzing and tuning power/performance characteristics of the system
 
+menu "Console rate-limiting"
+	visible if EXPERT
+
+config PRINTK_RATELIMIT_MS
+	int "printk rate-limiting time window (milliseconds)"
+	default 5000
+	help
+	  Specifies the time window, in milliseconds, for rate-limited [*] printk
+	  messages. No more than `CONFIG_PRINTK_RATELIMIT_BURST` messages will be
+	  printed within this window.
+
+	  Setting this value to 0 disables rate-limiting entirely.
+
+	  Configurations using a value other than the default of 5000 are not
+	  security supported.
+
+	  [*] Rate-limited messages are those controlled by the `loglvl` and
+	  `guest_loglvl` command-line parameters.
+
+config PRINTK_RATELIMIT_BURST
+	int "printk rate-limited message burst size"
+	default 10
+	help
+	  Defines the maximum number of rate-limited [*] printk messages that may
+	  be printed within each `CONFIG_PRINTK_RATELIMIT_MS` time window.
+
+	  Setting this value to 0 disables rate-limiting entirely.
+
+	  Configurations using a value other than the default of 10 are not
+	  security supported.
+
+	  [*] Rate-limited messages are those controlled by the `loglvl` and
+	  `guest_loglvl` command-line parameters.
+
+endmenu
+
 endmenu
xen/drivers/char/console.c +4 -2
index 76a1681670c1..2c7be1e61f3e 100644
@@ -1353,10 +1353,12 @@ bool __printk_ratelimit(unsigned int ratelimit_ms,
 }
 
 /* Minimum time in ms between messages */
-static const unsigned int printk_ratelimit_ms = 5 * 1000;
+static const unsigned int printk_ratelimit_ms =
+    CONFIG_PRINTK_RATELIMIT_MS;
 
 /* Number of messages we send before ratelimiting */
-static const unsigned int printk_ratelimit_burst = 10;
+static const unsigned int printk_ratelimit_burst =
+    CONFIG_PRINTK_RATELIMIT_BURST;
 
 bool printk_ratelimit(void)
 {
Signature · 1 line
2.54.0

A message that is not in the archive (sent off-list or not yet received): [email protected]

#6 lore raw

Re: [PATCH v4 2/2] xen/console: add compile-time rate-limiting controls

Quoted 24 lines · On Wed, Jul 29, 2026 at 12:25:20AM -0700, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Introduce CONFIG_PRINTK_RATELIMIT_MS and CONFIG_PRINTK_RATELIMIT_BURST for configuring rate-limiting policy at the compile time.

Use symbols for global rate-limiting initialization in the console driver.

---
Changes since v3:
- added note on security support for non-standard configurations
- gated menu with EXPERT

I kept both settings for now.

---
 xen/common/Kconfig         | 36 ++++++++++++++++++++++++++++++++++++
 xen/drivers/char/console.c |  6 ++++--
 2 files changed, 40 insertions(+), 2 deletions(-)
xen/common/Kconfig +0 -0
index da80fdba8469..749d3bfb08e0 100644

xen/common/Kconfig config PM_STATS · line 676

Quoted 5 lines
xen/common/Kconfig +2 -0
@@ -672,4 +672,40 @@ config PM_STATS
 	  Enable collection of performance management statistics to aid in
 	  analyzing and tuning power/performance characteristics of the system
 
+menu "Console rate-limiting"
+	visible if EXPERT

No strong opinion, but there's a drivers/char/Kconfig which might be a more natural place for those option to live, and then there's no reason for the extra menu?

Quoted 21 lines
xen/common/Kconfig +21 -0
+
+config PRINTK_RATELIMIT_MS
+	int "printk rate-limiting time window (milliseconds)"
+	default 5000
+	help
+	  Specifies the time window, in milliseconds, for rate-limited [*] printk
+	  messages. No more than `CONFIG_PRINTK_RATELIMIT_BURST` messages will be
+	  printed within this window.
+
+	  Setting this value to 0 disables rate-limiting entirely.
+
+	  Configurations using a value other than the default of 5000 are not
+	  security supported.
+
+	  [*] Rate-limited messages are those controlled by the `loglvl` and
+	  `guest_loglvl` command-line parameters.
+
+config PRINTK_RATELIMIT_BURST
+	int "printk rate-limited message burst size"
+	default 10
+	help

xen/common/Kconfig config PM_STATS · line 707

Quoted 10 lines
xen/common/Kconfig +10 -0
+	  Defines the maximum number of rate-limited [*] printk messages that may
+	  be printed within each `CONFIG_PRINTK_RATELIMIT_MS` time window.
+
+	  Setting this value to 0 disables rate-limiting entirely.
+
+	  Configurations using a value other than the default of 10 are not
+	  security supported.
+
+	  [*] Rate-limited messages are those controlled by the `loglvl` and
+	  `guest_loglvl` command-line parameters.

Is it common to use footnotes in Kconfig options? It seems a bit weird to me, I would probably just expand inside parenthesis if needed.

Also, I'm a bit confused by the mention of loglvl and guest_loglvl explicitly here: messages outside of the selected level are just discarded, and hence it's kind of obvious that just messages inside the selected level are controlled by this rate-limiting.

#7 lore raw

Re: [PATCH v4 2/2] xen/console: add compile-time rate-limiting controls

Quoted 28 lines · Roger Pau Monné
Quoted 27 lines · On Wed, Jul 29, 2026 at 12:25:20AM -0700, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Introduce CONFIG_PRINTK_RATELIMIT_MS and CONFIG_PRINTK_RATELIMIT_BURST for configuring rate-limiting policy at the compile time.

Use symbols for global rate-limiting initialization in the console driver.

---
Changes since v3:
- added note on security support for non-standard configurations
- gated menu with EXPERT

I kept both settings for now.

---
 xen/common/Kconfig         | 36 ++++++++++++++++++++++++++++++++++++
 xen/drivers/char/console.c |  6 ++++--
 2 files changed, 40 insertions(+), 2 deletions(-)
xen/common/Kconfig +0 -0
index da80fdba8469..749d3bfb08e0 100644
@@ -672,4 +672,40 @@ config PM_STATS
 	  Enable collection of performance management statistics to aid in
 	  analyzing and tuning power/performance characteristics of the system
 
Quoted 6 lines

xen/common/Kconfig config PM_STATS · line 676

Quoted 2 lines
xen/common/Kconfig +2 -0
+menu "Console rate-limiting"
+	visible if EXPERT

No strong opinion, but there's a drivers/char/Kconfig which might be a more natural place for those option to live, and then there's no reason for the extra menu?

I had the knob initially in drivers/char/Kconfig, but moved to common/Kconfig to address Jan's feedback:

https://lore.kernel.org/xen-devel/[email protected]/

Quoted 28 lines
Quoted 28 lines
xen/common/Kconfig +27 -0
+
+config PRINTK_RATELIMIT_MS
+	int "printk rate-limiting time window (milliseconds)"
+	default 5000
+	help
+	  Specifies the time window, in milliseconds, for rate-limited [*] printk
+	  messages. No more than `CONFIG_PRINTK_RATELIMIT_BURST` messages will be
+	  printed within this window.
+
+	  Setting this value to 0 disables rate-limiting entirely.
+
+	  Configurations using a value other than the default of 5000 are not
+	  security supported.
+
+	  [*] Rate-limited messages are those controlled by the `loglvl` and
+	  `guest_loglvl` command-line parameters.
+
+config PRINTK_RATELIMIT_BURST
+	int "printk rate-limited message burst size"
+	default 10
+	help
+	  Defines the maximum number of rate-limited [*] printk messages that may
+	  be printed within each `CONFIG_PRINTK_RATELIMIT_MS` time window.
+
+	  Setting this value to 0 disables rate-limiting entirely.
+
+	  Configurations using a value other than the default of 10 are not
Quoted 8 lines

xen/common/Kconfig config PM_STATS · line 692

Quoted 4 lines
xen/common/Kconfig +4 -0
+	  security supported.
+
+	  [*] Rate-limited messages are those controlled by the `loglvl` and
+	  `guest_loglvl` command-line parameters.

Is it common to use footnotes in Kconfig options? It seems a bit weird to me, I would probably just expand inside parenthesis if needed.

I'll just drop extra text.

Quoted 5 lines

Also, I'm a bit confused by the mention of loglvl and guest_loglvl explicitly here: messages outside of the selected level are just discarded, and hence it's kind of obvious that just messages inside the selected level are controlled by this rate-limiting.

#8 lore raw

Re: [PATCH v4 2/2] xen/console: add compile-time rate-limiting controls

Quoted 36 lines · On Mon, Aug 10, 2026 at 10:56:15AM -0700, [email protected] wrote:
Quoted 28 lines · Roger Pau Monné
Quoted 27 lines · On Wed, Jul 29, 2026 at 12:25:20AM -0700, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Introduce CONFIG_PRINTK_RATELIMIT_MS and CONFIG_PRINTK_RATELIMIT_BURST for configuring rate-limiting policy at the compile time.

Use symbols for global rate-limiting initialization in the console driver.

---
Changes since v3:
- added note on security support for non-standard configurations
- gated menu with EXPERT

I kept both settings for now.

---
 xen/common/Kconfig         | 36 ++++++++++++++++++++++++++++++++++++
 xen/drivers/char/console.c |  6 ++++--
 2 files changed, 40 insertions(+), 2 deletions(-)
xen/common/Kconfig +0 -0
index da80fdba8469..749d3bfb08e0 100644
@@ -672,4 +672,40 @@ config PM_STATS
 	  Enable collection of performance management statistics to aid in
 	  analyzing and tuning power/performance characteristics of the system
 
Quoted 6 lines

xen/common/Kconfig config PM_STATS · line 676

Quoted 2 lines
xen/common/Kconfig +2 -0
+menu "Console rate-limiting"
+	visible if EXPERT

No strong opinion, but there's a drivers/char/Kconfig which might be a more natural place for those option to live, and then there's no reason for the extra menu?

Quoted 4 lines

I had the knob initially in drivers/char/Kconfig, but moved to common/Kconfig to address Jan's feedback:

https://lore.kernel.org/xen-devel/[email protected]/

OK, as I said, I don't have a strong opinion. I think we want to keep drivers/char/Kconfig for console driver specific options, but not generic console related parameters.

Thanks, Roger.