Skip to contentSkip to conversation

xen/console: updates to rate-limiting

PATCH Superseded [email protected] 2 participants 15 messages Feb 6 – Feb 19

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover xen/console: updates to rate-limiting

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

Patches 1-2, 6-7 are tiny cleanups.
Patch 3 introduces build-time rate-limiting controls. Patch 4 introduces run-time rate-limiting controls.
Patch 5 is a fixup for the rate-limiter to adjust to new user-defined rate-limiting parameters.

[1] Link to v1: https://lore.kernel.org/xen-devel/[email protected]/
[2] CI run: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/2311064179

Denis Mukhin (7):
xen/console: remove __printk_ratelimit() from lib.h xen/console: use bool as return value from printk_ratelimit() xen/console: add build-time rate-limiting controls xen/console: add run-time rate-limiting controls xen/console: correct leaky-bucket rate limiter xen/console: use s_time_t for time accounting in do_printk_ratelimit() xen/console: simplify printouts in do_printk_ratelimit()

 docs/misc/xen-command-line.pandoc | 27 ++++++++++++++
 xen/drivers/char/Kconfig          | 25 +++++++++++++
 xen/drivers/char/console.c        | 62 ++++++++++++++++++-------------
 xen/include/xen/lib.h             |  4 +-
 4 files changed, 90 insertions(+), 28 deletions(-)
Signature · 1 line
2.52.0
#1 lore raw

PATCH v2 1/7 xen/console: remove __printk_ratelimit() from lib.h

From: Denis Mukhin <[email protected]>

There's no users of __printk_ratelimit() outside console driver.

Remove the declaration from lib.h header and make the function local to console driver.

While doing it, drop double underscores to satisfy MISRA; rename function to do_printk_ratelimit().

Not a functional change.

---
Changes since v1:
- made __printk_ratelimit() static to console driver

---
 xen/drivers/char/console.c | 7 ++++---
 xen/include/xen/lib.h      | 2 --
 2 files changed, 4 insertions(+), 5 deletions(-)
xen/drivers/char/console.c +4 -3
index 2bdb4d5fb417..af9ed38da553 100644
@@ -981,7 +981,7 @@ vprintk_common(const char *fmt, va_list args, const char *prefix)
     char         *p, *q;
     unsigned long flags;
 
-    /* console_lock can be acquired recursively from __printk_ratelimit(). */
+    /* console_lock can be acquired recursively from do_printk_ratelimit(). */
     local_irq_save(flags);
     rspin_lock(&console_lock);
     state = &this_cpu(state);
@@ -1273,7 +1273,8 @@ void console_end_sync(void)
  * This enforces a rate limit: not more than one kernel message
  * every printk_ratelimit_ms (millisecs).
  */
-int __printk_ratelimit(unsigned int ratelimit_ms, unsigned int ratelimit_burst)
+static int do_printk_ratelimit(unsigned int ratelimit_ms,
+                               unsigned int ratelimit_burst)
 {
     static DEFINE_SPINLOCK(ratelimit_lock);
     static unsigned long toks = 10 * 5 * 1000;
@@ -1327,7 +1328,7 @@ static const unsigned int printk_ratelimit_burst = 10;
 
 int printk_ratelimit(void)
 {
-    return __printk_ratelimit(printk_ratelimit_ms, printk_ratelimit_burst);
+    return do_printk_ratelimit(printk_ratelimit_ms, printk_ratelimit_burst);
 }
 
 /*
xen/include/xen/lib.h +0 -2
index bb0fd446b484..f516215ca8e4 100644
@@ -82,8 +82,6 @@ extern void guest_printk(const struct domain *d, const char *fmt, ...)
     __attribute__ ((format (printf, 2, 3)));
 extern void noreturn panic(const char *fmt, ...)
     __attribute__ ((format (printf, 1, 2)));
-extern int __printk_ratelimit(unsigned int ratelimit_ms,
-                              unsigned int ratelimit_burst);
 extern int printk_ratelimit(void);
 
 #define gprintk(lvl, fmt, args...) \
Signature · 1 line
2.52.0
#2 lore raw

PATCH v2 2/7 xen/console: use bool as return value from printk_ratelimit()

In reply to [email protected]

From: Denis Mukhin <[email protected]>

{do_,}printk_ratelimit() are predicates. Make that pronounced by updating the return value to boolean.

Not a functional change.

---
Changes since v1:
- new patch

---
 xen/drivers/char/console.c | 10 +++++-----
 xen/include/xen/lib.h      |  2 +-
 2 files changed, 6 insertions(+), 6 deletions(-)
xen/drivers/char/console.c +5 -5
index af9ed38da553..c4c92e3efa39 100644
@@ -1273,8 +1273,8 @@ void console_end_sync(void)
  * This enforces a rate limit: not more than one kernel message
  * every printk_ratelimit_ms (millisecs).
  */
-static int do_printk_ratelimit(unsigned int ratelimit_ms,
-                               unsigned int ratelimit_burst)
+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;
@@ -1313,11 +1313,11 @@ static int do_printk_ratelimit(unsigned int ratelimit_ms,
             rspin_unlock(&console_lock);
         }
         local_irq_restore(flags);
-        return 1;
+        return true;
     }
     missed++;
     spin_unlock_irqrestore(&ratelimit_lock, flags);
-    return 0;
+    return false;
 }
 
 /* Minimum time in ms between messages */
@@ -1326,7 +1326,7 @@ static const unsigned int printk_ratelimit_ms = 5 * 1000;
 /* Number of messages we send before ratelimiting */
 static const unsigned int printk_ratelimit_burst = 10;
 
-int printk_ratelimit(void)
+bool printk_ratelimit(void)
 {
     return do_printk_ratelimit(printk_ratelimit_ms, printk_ratelimit_burst);
 }
xen/include/xen/lib.h +1 -1
index f516215ca8e4..e39fa9200c82 100644
@@ -82,7 +82,7 @@ extern void guest_printk(const struct domain *d, const char *fmt, ...)
     __attribute__ ((format (printf, 2, 3)));
 extern void noreturn panic(const char *fmt, ...)
     __attribute__ ((format (printf, 1, 2)));
-extern int printk_ratelimit(void);
+extern bool printk_ratelimit(void);
 
 #define gprintk(lvl, fmt, args...) \
     printk(XENLOG_GUEST lvl "%pv " fmt, current, ## args)
Signature · 1 line
2.52.0
Acked-by on v2 2/7
#3 lore raw

Re: [PATCH v2 2/7] xen/console: use bool as return value from printk_ratelimit()

Quoted 8 lines · On 06.02.2026 21:24, [email protected] wrote:

From: Denis Mukhin <[email protected]>

{do_,}printk_ratelimit() are predicates. Make that pronounced by updating the return value to boolean.

Not a functional change.

Yet to make it explicit: I'm not quite sure I want to ack patch 1.

Jan

#4 lore raw

PATCH v2 3/7 xen/console: add build-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 v1:
- new patch

---
 xen/drivers/char/Kconfig   | 25 +++++++++++++++++++++++++
 xen/drivers/char/console.c |  6 ++++--
 2 files changed, 29 insertions(+), 2 deletions(-)
xen/drivers/char/Kconfig +25 -0
index 8e49a52c735b..98b117762a0c 100644
@@ -103,3 +103,28 @@ config XHCI
 	  Enabling this option makes Xen use extra ~230KiB memory, even if XHCI UART
 	  is not selected.
 	  If you have an x86 based system with USB3, say Y.
+
+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-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.
+
+	  Rate-limited messages are those controlled by the `loglvl` and
+	  `guest_loglvl` command-line parameters.
xen/drivers/char/console.c +4 -2
index c4c92e3efa39..ec87ecb3e5a0 100644
@@ -1321,10 +1321,12 @@ static bool do_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.52.0
#5 lore raw

Re: [PATCH v2 3/7] xen/console: add build-time rate-limiting controls

Quoted 8 lines · On 06.02.2026 21:24, [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.

I'm not sure this is a direction we want to move into, so just one formal remark below.

Quoted 21 lines
xen/drivers/char/Kconfig +15 -0
@@ -103,3 +103,28 @@ config XHCI
 	  Enabling this option makes Xen use extra ~230KiB memory, even if XHCI UART
 	  is not selected.
 	  If you have an x86 based system with USB3, say Y.
+
+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-limited messages are those controlled by the `loglvl` and
+	  `guest_loglvl` command-line parameters.
+
+config PRINTK_RATELIMIT_BURST

xen/drivers/char/Kconfig config XHCI · line 130

Quoted 10 lines
xen/drivers/char/Kconfig +10 -0
+	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-limited messages are those controlled by the `loglvl` and
+	  `guest_loglvl` command-line parameters.

printk() is implemented under drivers/char/, but I'm unconvinced of placing such pretty much global controls here (rather than in common/Kconfig).

Jan

#6 lore raw

PATCH v2 4/7 xen/console: add run-time rate-limiting controls

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Add new "printk_ratelimit_ms" and "printk_ratelimit_burst" command line parameters for global rate-limiting controls.

Drop const and add __read_mostly for both global parameters printk_ratelimit_{ms,burst}.

Update command line documentation.

---
Changes since v1:
- new patch

---
 docs/misc/xen-command-line.pandoc | 27 +++++++++++++++++++++++++++
 xen/drivers/char/console.c        | 22 ++++++++++++++--------
 2 files changed, 41 insertions(+), 8 deletions(-)
docs/misc/xen-command-line.pandoc +27 -0
index c1f2def9f99c..5505644d1c0f 100644
@@ -2077,6 +2077,33 @@ compression is selected at build time from Kconfig.
 This is a mask of C-states which are to be used preferably.  This option is
 applicable only on hardware were certain C-states are exclusive of one another.
 
+### printk_ratelimit_ms
+> `= <integer>`
+
+> Default: `CONFIG_PRINTK_RATELIMIT_MS`
+
+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-limited messages are those controlled by the `loglvl` and `guest_loglvl`
+command-line parameters.
+
+### printk_ratelimit_burst
+> `= <integer>`
+
+> Default: `CONFIG_PRINTK_RATELIMIT_BURST`
+
+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-limited messages are those controlled by the `loglvl` and `guest_loglvl`
+command-line parameters.
+
 ### probe-port-aliases (x86)
 > `= <boolean>`
 
xen/drivers/char/console.c +14 -8
index ec87ecb3e5a0..f607e8f84d7a 100644
@@ -344,6 +344,20 @@ static void cf_check do_dec_thresh(unsigned char key, bool unused)
     do_adj_thresh(key);
 }
 
+/*
+ * Global rate-limiting controls for printk().
+ */
+
+/* Minimum time in ms between messages */
+static unsigned int __read_mostly printk_ratelimit_ms =
+    CONFIG_PRINTK_RATELIMIT_MS;
+integer_param("printk_ratelimit_ms", printk_ratelimit_ms);
+
+/* Number of messages we send before ratelimiting */
+static unsigned int __read_mostly printk_ratelimit_burst =
+    CONFIG_PRINTK_RATELIMIT_BURST;
+integer_param("printk_ratelimit_burst", printk_ratelimit_burst);
+
 /*
  * ********************************************************
  * *************** ACCESS TO CONSOLE RING *****************
@@ -1320,14 +1334,6 @@ static bool do_printk_ratelimit(unsigned int ratelimit_ms,
     return false;
 }
 
-/* Minimum time in ms between messages */
-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 =
-    CONFIG_PRINTK_RATELIMIT_BURST;
-
 bool printk_ratelimit(void)
 {
     return do_printk_ratelimit(printk_ratelimit_ms, printk_ratelimit_burst);
Signature · 1 line
2.52.0
#7 lore raw

Re: [PATCH v2 4/7] xen/console: add run-time rate-limiting controls

Quoted 11 lines · On 06.02.2026 21:24, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Add new "printk_ratelimit_ms" and "printk_ratelimit_burst" command line parameters for global rate-limiting controls.

Drop const and add __read_mostly for both global parameters printk_ratelimit_{ms,burst}.

Update command line documentation.

Like for the previous patch, I'm not sure we want/need this, so again only two formal things:

docs/misc/xen-command-line.pandoc compression is selected at build time from Kconfig. · line 2080

Quoted 7 lines
docs/misc/xen-command-line.pandoc +1 -0
@@ -2077,6 +2077,33 @@ compression is selected at build time from Kconfig.
 This is a mask of C-states which are to be used preferably.  This option is
 applicable only on hardware were certain C-states are exclusive of one another.
 
+### printk_ratelimit_ms

New options want to use dashes in favor of underscores.

Quoted 28 lines
xen/drivers/char/console.c +14 -1
@@ -344,6 +344,20 @@ static void cf_check do_dec_thresh(unsigned char key, bool unused)
     do_adj_thresh(key);
 }
 
+/*
+ * Global rate-limiting controls for printk().
+ */
+
+/* Minimum time in ms between messages */
+static unsigned int __read_mostly printk_ratelimit_ms =
+    CONFIG_PRINTK_RATELIMIT_MS;
+integer_param("printk_ratelimit_ms", printk_ratelimit_ms);
+
+/* Number of messages we send before ratelimiting */
+static unsigned int __read_mostly printk_ratelimit_burst =
+    CONFIG_PRINTK_RATELIMIT_BURST;
+integer_param("printk_ratelimit_burst", printk_ratelimit_burst);
+
 /*
  * ********************************************************
  * *************** ACCESS TO CONSOLE RING *****************
@@ -1320,14 +1334,6 @@ static bool do_printk_ratelimit(unsigned int ratelimit_ms,
     return false;
 }
 
-/* Minimum time in ms between messages */

xen/drivers/char/console.c static bool do_printk_ratelimit(unsigned int ratelimit_ms, · line 1339

Quoted 10 lines
xen/drivers/char/console.c +0 -7
-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 =
-    CONFIG_PRINTK_RATELIMIT_BURST;
-
 bool printk_ratelimit(void)
 {
     return do_printk_ratelimit(printk_ratelimit_ms, printk_ratelimit_burst);

Do things really need moving around?

Jan

#8 lore raw

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

In reply to [email protected]

From: Denis Mukhin <[email protected]>

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().

---
Changes since v1:
- new patch

---
 xen/drivers/char/console.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
xen/drivers/char/console.c +10 -4
index f607e8f84d7a..e3962512d282 100644
@@ -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;
Signature · 1 line
2.52.0
#9 lore raw

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

Quoted 24 lines · On 06.02.2026 21:24, [email protected] wrote:

From: Denis Mukhin <[email protected]>

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().

---
Changes since v1:
- new patch

---
 xen/drivers/char/console.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
xen/drivers/char/console.c +0 -0
index f607e8f84d7a..e3962512d282 100644

xen/drivers/char/console.c static bool do_printk_ratelimit(unsigned int ratelimit_ms, · line 1294

Quoted 4 lines
xen/drivers/char/console.c +0 -1
@@ -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;

Shouldn't this have been adjusted already in patches 3 and 4?

xen/drivers/char/console.c static bool do_printk_ratelimit(unsigned int ratelimit_ms, · line 1305

Quoted 13 lines
xen/drivers/char/console.c +7 -1
+    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;

This multiplication can overflow (which is avoidable on 64-bit arch-es, but needs checking for on 32-bit ones in any event).

xen/drivers/char/console.c static bool do_printk_ratelimit(unsigned int ratelimit_ms, · line 1314

Quoted 10 lines
xen/drivers/char/console.c +2 -2
+    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 you need the product anyway, calculate it once (correctly) and then you won't need a cast here either.

Jan

#10 lore raw

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

In reply to [email protected]
Quoted 10 lines · On 06.02.2026 21:24, [email protected] wrote:
xen/drivers/char/console.c +1 -1
@@ -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;

xen/drivers/char/console.c static bool do_printk_ratelimit(unsigned int ratelimit_ms, · line 1305

Quoted 10 lines
xen/drivers/char/console.c +6 -1
     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;

Btw, instead of this I wonder whether it wouldn't be neater to use the nested functions extension of the compiler:

    void __constructor __init init(void)
    {
        toks = printk_ratelimit_burst * printk_ratelimit_ms;
    }

If we wanted this, we'd need to record the extension use in docs/misra/C-language-toolchain.rst, though. Plus init_constructors() would need invoking _far_ earlier (perhaps right after command line parsing).

Jan

#11 lore raw

PATCH v2 6/7 xen/console: use s_time_t for time accounting in do_printk_ratelimit()

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Use appropriate s_time_t for time window calculation in the rate limiting algorithm.

---
Changes since v1:
- new patch

---
 xen/drivers/char/console.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
xen/drivers/char/console.c +4 -7
index e3962512d282..536b883d5dcf 100644
@@ -1291,12 +1291,10 @@ static bool do_printk_ratelimit(unsigned int ratelimit_ms,
                                 unsigned int ratelimit_burst)
 {
     static DEFINE_SPINLOCK(ratelimit_lock);
-    static unsigned long toks;
-    static unsigned long last_msg;
+    static s_time_t toks, last_msg;
+    s_time_t now, ms;
     static unsigned int missed;
     unsigned long flags;
-    unsigned long long now;
-    unsigned long ms;
 
     if ( !printk_ratelimit_burst || !printk_ratelimit_burst )
         return true;
@@ -1305,13 +1303,12 @@ static bool do_printk_ratelimit(unsigned int ratelimit_ms,
         toks = printk_ratelimit_burst * printk_ratelimit_ms;
 
     now = NOW(); /* ns */
-    do_div(now, 1000000);
-    ms = (unsigned long)now;
+    ms = do_div(now, MILLISECS(1));
 
     spin_lock_irqsave(&ratelimit_lock, flags);
     toks += ms - last_msg;
     last_msg = ms;
-    toks = min(toks, (unsigned long)(ratelimit_burst * ratelimit_ms));
+    toks = min(toks, (s_time_t)(ratelimit_burst * ratelimit_ms));
     if ( toks >= ratelimit_ms )
     {
         unsigned int lost = missed;
Signature · 1 line
2.52.0
#12 lore raw

Re: [PATCH v2 6/7] xen/console: use s_time_t for time accounting in do_printk_ratelimit()

xen/drivers/char/console.c static bool do_printk_ratelimit(unsigned int ratelimit_ms, · line 1295

Quoted 10 lines · On 06.02.2026 21:24, [email protected] wrote:
xen/drivers/char/console.c +2 -2
@@ -1291,12 +1291,10 @@ static bool do_printk_ratelimit(unsigned int ratelimit_ms,
                                 unsigned int ratelimit_burst)
 {
     static DEFINE_SPINLOCK(ratelimit_lock);
-    static unsigned long toks;
-    static unsigned long last_msg;
+    static s_time_t toks, last_msg;
+    s_time_t now, ms;

For now (i.e. return values of NOW()) s_time_t is appropriate to use. For millisecond values and alike it isn't, though.

Jan

#13 lore raw

PATCH v2 7/7 xen/console: simplify printouts in do_printk_ratelimit()

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Simplify the code around notification of how many messages have been rate-limited.

Not a functional change.

---
Changes since v1:
- new patch

---
 xen/drivers/char/console.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
xen/drivers/char/console.c +5 -5
index 536b883d5dcf..a60a7c213555 100644
@@ -1318,15 +1318,15 @@ static bool do_printk_ratelimit(unsigned int ratelimit_ms,
         spin_unlock(&ratelimit_lock);
         if ( lost )
         {
-            char lost_str[10];
+            char msg[40];
+
+            snprintf(msg, sizeof(msg),
+                     "printk: %u messages suppressed\n", lost);
 
-            snprintf(lost_str, sizeof(lost_str), "%u", lost);
             /* console_lock may already be acquired by printk(). */
             rspin_lock(&console_lock);
             printk_start_of_line(CONSOLE_PREFIX);
-            __putstr("printk: ");
-            __putstr(lost_str);
-            __putstr(" messages suppressed.\n");
+            __putstr(msg);
             rspin_unlock(&console_lock);
         }
         local_irq_restore(flags);
Signature · 1 line
2.52.0
#14 lore raw

Re: [PATCH v2 7/7] xen/console: simplify printouts in do_printk_ratelimit()

Quoted 4 lines · On 06.02.2026 21:24, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Simplify the code around notification of how many messages have been rate-limited.

"Simplify" in what dimension?

Quoted 3 lines
---
 xen/drivers/char/console.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

Diffstat says there's no difference.

xen/drivers/char/console.c static bool do_printk_ratelimit(unsigned int ratelimit_ms, · line 1324

Quoted 11 lines
xen/drivers/char/console.c +4 -1
@@ -1318,15 +1318,15 @@ static bool do_printk_ratelimit(unsigned int ratelimit_ms,
         spin_unlock(&ratelimit_lock);
         if ( lost )
         {
-            char lost_str[10];
+            char msg[40];
+
+            snprintf(msg, sizeof(msg),
+                     "printk: %u messages suppressed\n", lost);

Having snprintf() process a longer string means there's more runtime overhead.

xen/drivers/char/console.c static bool do_printk_ratelimit(unsigned int ratelimit_ms, · line 1332

Quoted 11 lines
xen/drivers/char/console.c +1 -4
-            snprintf(lost_str, sizeof(lost_str), "%u", lost);
             /* console_lock may already be acquired by printk(). */
             rspin_lock(&console_lock);
             printk_start_of_line(CONSOLE_PREFIX);
-            __putstr("printk: ");
-            __putstr(lost_str);
-            __putstr(" messages suppressed.\n");
+            __putstr(msg);
             rspin_unlock(&console_lock);
         }
         local_irq_restore(flags);

The net win therefore looks to be one __putstr() where previously we had three. While it's less obvious then that the array size is actually large enough.

Jan