Skip to contentSkip to conversation

xen/console: updates to rate-limiting

PATCH In review [email protected] 2 participants 6 messages Nov 12 – Nov 16

lore
Share & tools
lore
#0 lore raw

v6 cover xen/console: updates to rate-limiting

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

Patch 1 introduces compile-time rate-limiting controls.

Patch 2 is a fixup for the rate-limiter to adjust to new compile-time rate-limiting parameters.

v5: https://lore.kernel.org/xen-devel/[email protected]/
CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/3057631284

Denis Mukhin (2):
xen/console: add build-time rate-limiting controls xen/console: re-calibrate rate-limiter based on compile-time options

 xen/common/Kconfig         | 30 ++++++++++++++++++++++++++++++
 xen/drivers/char/console.c | 34 +++++++++++++++++++++++++---------
 2 files changed, 55 insertions(+), 9 deletions(-)
Signature · 1 line
2.56.0
#1 lore raw

PATCH v6 1/2 xen/console: add build-time rate-limiting controls

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 v5:
- updated notes on security supportable rate-limiting configurations

---
 xen/common/Kconfig         | 30 ++++++++++++++++++++++++++++++
 xen/drivers/char/console.c |  6 ++++--
 2 files changed, 34 insertions(+), 2 deletions(-)
xen/common/Kconfig +30 -0
index 5b289e444fa5..539cccf21029 100644
@@ -675,4 +675,34 @@ 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.
+
+	  Rate-limiting configurations within [0..5000) milliseconds are not
+	  security supported.
+
+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.
+
+	  Rate-limiting configurations with burst size larger than default 10
+	  are not security supported.
+
+endmenu
+
 endmenu
xen/drivers/char/console.c +4 -2
index e4df0bbc2eec..1a8beb6665c1 100644
@@ -1330,10 +1330,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.56.0
#2 lore raw
In reply to [email protected]
Quoted 8 lines · On 12.11.2026 19:20, [email protected] wrote:

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

Patch 1 introduces compile-time rate-limiting controls.

Patch 2 is a fixup for the rate-limiter to adjust to new compile-time rate-limiting parameters.

Quoted 6 lines

v5: https://lore.kernel.org/xen-devel/[email protected]/
CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/3057631284

Denis Mukhin (2):
xen/console: add build-time rate-limiting controls xen/console: re-calibrate rate-limiter based on compile-time options

Not just as per my inbox, but also as per [1], only patch 1 has made it.

Jan

[1] https://lists.xen.org/archives/html/xen-devel/2026-11/threads.html

#3 lore raw

PATCH v6 2/2 xen/console: re-calibrate rate-limiter based on compile-time options

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Current printk_ratelimit() relies on hardcoded values 5000 and 10 respectively to program leaky-bucket message limiting. Now that there's compile-time rate-limiting settings, the algorithm needs adjustment.

Use 'ratelimit_ms' and 'ratelimit_burst' variables to re-calibrate limiter.

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

---
Changes since v5:
- dropped "Fixes:" tag
- updated commit message
- dropped use of DIM_MUL2() in favor of open-coded __builtin_umul_overflow
- re-aligned use of integer arithmetics across 32- and 64-bit hosts

---
 xen/drivers/char/console.c | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)
xen/drivers/char/console.c +21 -7
index 1a8beb6665c1..e2310b9d3c9a 100644
@@ -1286,21 +1286,35 @@ 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 int toks = UINT_MAX;
     static unsigned long last_msg;
     static unsigned int missed;
+    unsigned int limit;
+    unsigned int elapsed;
     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;
+
+    if ( __builtin_umul_overflow(ratelimit_burst, ratelimit_ms, &limit) )
+        limit = UINT_MAX;
+
+    now = NOW(); /* ns */
+    do_div(now, MILLISECS(1));
+    ms = now;
 
     spin_lock_irqsave(&ratelimit_lock, flags);
-    toks += ms - last_msg;
+
+    elapsed = ms - last_msg;
+    if ( toks >= limit || elapsed >= limit - toks )
+        toks = limit;
+    else
+        toks += elapsed;
+
     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.56.0
#4 lore raw

Re: [PATCH v6 2/2] xen/console: re-calibrate rate-limiter based on compile-time options

Quoted 14 lines · On 12.11.2026 19:20, [email protected] wrote:
xen/drivers/char/console.c +3 -2
@@ -1286,21 +1286,35 @@ 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 int toks = UINT_MAX;
     static unsigned long last_msg;
     static unsigned int missed;
+    unsigned int limit;
+    unsigned int elapsed;
     unsigned long flags;
-    unsigned long long now = NOW(); /* ns */

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

Quoted 10 lines
xen/drivers/char/console.c +6 -2
     unsigned long ms;
+    s_time_t now;
 
-    do_div(now, 1000000);
-    ms = (unsigned long)now;
+    if ( !ratelimit_ms || !ratelimit_burst )
+        return true;
+
+    if ( __builtin_umul_overflow(ratelimit_burst, ratelimit_ms, &limit) )
+        limit = UINT_MAX;

It's only a multiplication, so likely okay to do on every call. Strictly speaking limit could be static __read_mostly, and would need setting only once.

Jan

Acked-by on the v6 series
#5 lore raw
In reply to [email protected]
Quoted 8 lines · On 12.11.2026 19:20, [email protected] wrote:

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

Patch 1 introduces compile-time rate-limiting controls.

Patch 2 is a fixup for the rate-limiter to adjust to new compile-time rate-limiting parameters.

Quoted 6 lines

v5: https://lore.kernel.org/xen-devel/[email protected]/
CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/3057631284

Denis Mukhin (2):
xen/console: add build-time rate-limiting controls xen/console: re-calibrate rate-limiter based on compile-time options

I'll send one remark on patch 2 separately.

Jan