Skip to contentSkip to conversation

xen/console: remove __printk_ratelimit()

PATCH In review [email protected] 4 participants 11 messages Jul 25, 2025 – Aug 6, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/console: remove __printk_ratelimit()

From: Denis Mukhin <[email protected]>

__printk_ratelimit() is never used outside of the console driver. Remove it from the lib.h and merge with the public printk_ratelimit().

Not a functional change.

---
 xen/drivers/char/console.c | 29 ++++++++++++-----------------
 xen/include/xen/lib.h      |  1 -
 2 files changed, 12 insertions(+), 18 deletions(-)
xen/drivers/char/console.c +12 -17
index 75fa033ce74d..80f8f2ed1bae 100644
@@ -979,7 +979,7 @@ static void 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 printk_ratelimit(). */
     local_irq_save(flags);
     rspin_lock(&console_lock);
     state = &this_cpu(state);
@@ -1266,13 +1266,19 @@ void console_end_sync(void)
     atomic_dec(&print_everything);
 }
 
+/* minimum time in ms between messages */
+static int __read_mostly printk_ratelimit_ms = 5 * 1000;
+
+/* number of messages we send before ratelimiting */
+static int __read_mostly printk_ratelimit_burst = 10;
+
 /*
  * printk rate limiting, lifted from Linux.
  *
  * This enforces a rate limit: not more than one kernel message
  * every printk_ratelimit_ms (millisecs).
  */
-int __printk_ratelimit(int ratelimit_ms, int ratelimit_burst)
+int printk_ratelimit(void)
 {
     static DEFINE_SPINLOCK(ratelimit_lock);
     static unsigned long toks = 10 * 5 * 1000;
@@ -1288,13 +1294,13 @@ int __printk_ratelimit(int ratelimit_ms, int ratelimit_burst)
     spin_lock_irqsave(&ratelimit_lock, flags);
     toks += ms - last_msg;
     last_msg = ms;
-    if ( toks > (ratelimit_burst * ratelimit_ms))
-        toks = ratelimit_burst * ratelimit_ms;
-    if ( toks >= ratelimit_ms )
+    if ( toks > (printk_ratelimit_burst * printk_ratelimit_ms) )
+        toks = printk_ratelimit_burst * printk_ratelimit_ms;
+    if ( toks >= printk_ratelimit_ms )
     {
         int lost = missed;
         missed = 0;
-        toks -= ratelimit_ms;
+        toks -= printk_ratelimit_ms;
         spin_unlock(&ratelimit_lock);
         if ( lost )
         {
@@ -1316,17 +1322,6 @@ int __printk_ratelimit(int ratelimit_ms, int ratelimit_burst)
     return 0;
 }
 
-/* minimum time in ms between messages */
-static int __read_mostly printk_ratelimit_ms = 5 * 1000;
-
-/* number of messages we send before ratelimiting */
-static int __read_mostly printk_ratelimit_burst = 10;
-
-int printk_ratelimit(void)
-{
-    return __printk_ratelimit(printk_ratelimit_ms, printk_ratelimit_burst);
-}
-
 /*
  * **************************************************************
  * ********************** Error-report **************************
xen/include/xen/lib.h +0 -1
index e63ec5039f92..a33c25dd1610 100644
@@ -80,7 +80,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(int ratelimit_ms, int ratelimit_burst);
 extern int printk_ratelimit(void);
 
 #define gprintk(lvl, fmt, args...) \
Signature · 1 line
2.34.1
#1 lore raw

Hi Denis,

Quoted 4 lines · On 25/07/2025 22:24, [email protected] wrote:

From: Denis Mukhin <[email protected]>

__printk_ratelimit() is never used outside of the console driver. Remove it from the lib.h and merge with the public printk_ratelimit().

Is this solving any sort of violation? Asking because even if the function is only used by one caller, I could see a benefit to be able to use different value for the ratelimit. So I leaning towards keep the code as-is.

Cheers,

Signature · 1 line
Julien Grall
#2 lore raw

On Sat, Jul 26, 2025 at 10:20:58AM +0100, Julien Grall wrote:

Hi Julien,

Thanks for your feedback!

I imagined that kind of a change may raise a question about importance of doing it.

Quoted 5 lines

Hi Denis,

Quoted 2 lines · On 25/07/2025 22:24, [email protected] wrote:

From: Denis Mukhin <[email protected]>

Quoted 7 lines
Quoted 2 lines

__printk_ratelimit() is never used outside of the console driver. Remove it from the lib.h and merge with the public printk_ratelimit().

Is this solving any sort of violation? Asking because even if the function is only used by one caller, I could see a benefit to be able to use different value for the ratelimit. So I leaning towards keep the code as-is.

The change is not solving any sort of violation, but simplifies a code path slightly since fine-grain rate-limit controls are not exported/used anywhere outside of the console driver. At this time, the purpose is a tiny cleanup.

TL;DR

I stepped over that code during addressing feedback for another change for domain prefixes, where I'm experimenting with re-using printk-facilities from guest_console_write(). This code showed up wrt rate-limiting for dom0's messages. Just in case, experimental branch is here:
https://gitlab.com/xen-project/people/dmukhin/xen/-/tree/console-fixes

I cannot anticipate not exposing rate-limiting timing controls in embedded certifiable setup, perhaps there will be a legit use case in production deployment.

But currently rate-limiting configuration happens at boot time only. The fact that rate-limiting has not been touched since its introduction in 2006

    26cf03554a75 ("[XEN] Implement rate-limited logging.")

made me think that there's no intent to plumb any rate-limiting controls at the run-time e.g. via keyhandler or a even hypercall.

Which resulted in this submission.

Quoted 6 lines

Cheers,

--
Julien Grall

Thanks,
Denis

#3 lore raw
In reply to Julien Grall
Quoted 10 lines · Julien Grall
Quoted 4 lines · On 25/07/2025 22:24, [email protected] wrote:

From: Denis Mukhin <[email protected]>

__printk_ratelimit() is never used outside of the console driver. Remove it from the lib.h and merge with the public printk_ratelimit().

Is this solving any sort of violation? Asking because even if the function is only used by one caller, I could see a benefit to be able to use different value for the ratelimit. So I leaning towards keep the code as-is.

+1

In fact I'm surprised (or maybe not) that we still don't make better use the rate limiting functionality.

Jan

#4 lore raw
Quoted 12 lines · Jan Beulich
Quoted 10 lines · Julien Grall
Quoted 4 lines · On 25/07/2025 22:24, [email protected] wrote:

From: Denis Mukhin <[email protected]>

__printk_ratelimit() is never used outside of the console driver. Remove it from the lib.h and merge with the public printk_ratelimit().

Is this solving any sort of violation? Asking because even if the function is only used by one caller, I could see a benefit to be able to use different value for the ratelimit. So I leaning towards keep the code as-is.

Quoted 4 lines

+1

In fact I'm surprised (or maybe not) that we still don't make better use the rate limiting functionality.

Out of curiosity, do you have any ideas re: make better use of the rate limiting functionality?

Build-time parameterization?

Quoted 3 lines

Jan

#5 lore raw
Quoted 13 lines · On 30.07.2025 00:18, [email protected] wrote:
Quoted 12 lines · Jan Beulich
Quoted 10 lines · Julien Grall
Quoted 4 lines · On 25/07/2025 22:24, [email protected] wrote:

From: Denis Mukhin <[email protected]>

__printk_ratelimit() is never used outside of the console driver. Remove it from the lib.h and merge with the public printk_ratelimit().

Is this solving any sort of violation? Asking because even if the function is only used by one caller, I could see a benefit to be able to use different value for the ratelimit. So I leaning towards keep the code as-is.

Quoted 7 lines
Quoted 4 lines

+1

In fact I'm surprised (or maybe not) that we still don't make better use the rate limiting functionality.

Out of curiosity, do you have any ideas re: make better use of the rate limiting functionality?

No concrete ones; thinking about this has been way too long ago.

Quoted 1 line

Build-time parameterization?

That and/or command line controls.

Jan

#6 lore raw
Quoted 19 lines · Jan Beulich
Quoted 18 lines · On 30.07.2025 00:18, [email protected] wrote:
Quoted 16 lines · Jan Beulich
Quoted 10 lines · Julien Grall
Quoted 4 lines · On 25/07/2025 22:24, [email protected] wrote:

From: Denis Mukhin <[email protected]>

__printk_ratelimit() is never used outside of the console driver. Remove it from the lib.h and merge with the public printk_ratelimit().

Is this solving any sort of violation? Asking because even if the function is only used by one caller, I could see a benefit to be able to use different value for the ratelimit. So I leaning towards keep the code as-is.

+1

In fact I'm surprised (or maybe not) that we still don't make better use the rate limiting functionality.

Quoted 8 lines
Quoted 2 lines

Out of curiosity, do you have any ideas re: make better use of the rate limiting functionality?

No concrete ones; thinking about this has been way too long ago.

Quoted 1 line

Build-time parameterization?

That and/or command line controls.

Got it.

Can you please explain why exporting __printk_ratelimit() is still required for implementation of build/command line settings in console.c?

Quoted 2 lines

Jan

#7 lore raw
Quoted 25 lines · On 30.07.2025 20:06, [email protected] wrote:
Quoted 24 lines · Jan Beulich
Quoted 13 lines · On 30.07.2025 00:18, [email protected] wrote:
Quoted 12 lines · Jan Beulich
Quoted 10 lines · Julien Grall
Quoted 4 lines · On 25/07/2025 22:24, [email protected] wrote:

From: Denis Mukhin <[email protected]>

__printk_ratelimit() is never used outside of the console driver. Remove it from the lib.h and merge with the public printk_ratelimit().

Is this solving any sort of violation? Asking because even if the function is only used by one caller, I could see a benefit to be able to use different value for the ratelimit. So I leaning towards keep the code as-is.

Quoted 7 lines
Quoted 4 lines

+1

In fact I'm surprised (or maybe not) that we still don't make better use the rate limiting functionality.

Out of curiosity, do you have any ideas re: make better use of the rate limiting functionality?

No concrete ones; thinking about this has been way too long ago.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Build-time parameterization?

That and/or command line controls.

Got it.

Can you please explain why exporting __printk_ratelimit() is still required for implementation of build/command line settings in console.c?

I didn't say console.c, did I? Whatever subsystem wanted to do proper rate limiting would need to gain some way of controlling this (as said, build time or cmdline driven), and it'll then need that function: How would it effect the behavior with custom ms and/or burst values, without having that function to call? (It is another thing that the function, using static variables rather than per-caller state, may not be quite ready for that kind of use. Also the arbitrary and hard-coded 10 * 5 * 1000 there would probably also want to be customizable.)

What you may want to do for Misra's sake is make the function static for the time being. The compiler will then fold it into its sole caller, until some new user appears. (At that occasion dropping one of the underscores may also be reasonable.)

Jan

#8 lore raw

Hi Jan,

Thanks for the feedback.

Quoted 28 lines · Jan Beulich
Quoted 27 lines · On 30.07.2025 20:06, [email protected] wrote:
Quoted 26 lines · Jan Beulich
Quoted 13 lines · On 30.07.2025 00:18, [email protected] wrote:
Quoted 12 lines · Jan Beulich
Quoted 10 lines · Julien Grall
Quoted 4 lines · On 25/07/2025 22:24, [email protected] wrote:

From: Denis Mukhin <[email protected]>

__printk_ratelimit() is never used outside of the console driver. Remove it from the lib.h and merge with the public printk_ratelimit().

Is this solving any sort of violation? Asking because even if the function is only used by one caller, I could see a benefit to be able to use different value for the ratelimit. So I leaning towards keep the code as-is.

Quoted 7 lines
Quoted 4 lines

+1

In fact I'm surprised (or maybe not) that we still don't make better use the rate limiting functionality.

Out of curiosity, do you have any ideas re: make better use of the rate limiting functionality?

No concrete ones; thinking about this has been way too long ago.

Quoted 1 line

Build-time parameterization?

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

That and/or command line controls.

Got it.

Can you please explain why exporting __printk_ratelimit() is still required for implementation of build/command line settings in console.c?

I didn't say console.c, did I? Whatever subsystem wanted to do proper rate

But you also did not say anything about idea of having per-subsystem rate limiting.

Quoted 8 lines

limiting would need to gain some way of controlling this (as said, build time or cmdline driven), and it'll then need that function: How would it effect the behavior with custom ms and/or burst values, without having that function to call? (It is another thing that the function, using static variables rather than per-caller state, may not be quite ready for that kind of use. Also the arbitrary and hard-coded 10 * 5 * 1000 there would probably also want to be customizable.)

Quoted 4 lines

What you may want to do for Misra's sake is make the function static for the time being. The compiler will then fold it into its sole caller, until some new user appears. (At that occasion dropping one of the underscores may also be reasonable.)

Do I understand it correctly that you will accept the following submission:
1) make __printk_ratelimit() static
2) drop one underscore from the name
3) keep the only call __printk_ratelimit() in a hope of there will be per-subsystem rate limiting in the future?

--
Denis

Quoted 3 lines

Jan

#9 lore raw
Quoted 49 lines · On 31.07.2025 23:28, [email protected] wrote:
Quoted 28 lines · Jan Beulich
Quoted 27 lines · On 30.07.2025 20:06, [email protected] wrote:
Quoted 26 lines · Jan Beulich
Quoted 13 lines · On 30.07.2025 00:18, [email protected] wrote:
Quoted 12 lines · Jan Beulich
Quoted 10 lines · Julien Grall
Quoted 4 lines · On 25/07/2025 22:24, [email protected] wrote:

From: Denis Mukhin <[email protected]>

__printk_ratelimit() is never used outside of the console driver. Remove it from the lib.h and merge with the public printk_ratelimit().

Is this solving any sort of violation? Asking because even if the function is only used by one caller, I could see a benefit to be able to use different value for the ratelimit. So I leaning towards keep the code as-is.

Quoted 7 lines
Quoted 4 lines

+1

In fact I'm surprised (or maybe not) that we still don't make better use the rate limiting functionality.

Out of curiosity, do you have any ideas re: make better use of the rate limiting functionality?

No concrete ones; thinking about this has been way too long ago.

Quoted 1 line

Build-time parameterization?

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

That and/or command line controls.

Got it.

Can you please explain why exporting __printk_ratelimit() is still required for implementation of build/command line settings in console.c?

I didn't say console.c, did I? Whatever subsystem wanted to do proper rate

But you also did not say anything about idea of having per-subsystem rate limiting.

Quoted 8 lines

limiting would need to gain some way of controlling this (as said, build time or cmdline driven), and it'll then need that function: How would it effect the behavior with custom ms and/or burst values, without having that function to call? (It is another thing that the function, using static variables rather than per-caller state, may not be quite ready for that kind of use. Also the arbitrary and hard-coded 10 * 5 * 1000 there would probably also want to be customizable.)

Quoted 8 lines
Quoted 4 lines

What you may want to do for Misra's sake is make the function static for the time being. The compiler will then fold it into its sole caller, until some new user appears. (At that occasion dropping one of the underscores may also be reasonable.)

Do I understand it correctly that you will accept the following submission:
1) make __printk_ratelimit() static
2) drop one underscore from the name

Yes, if you really think that's worth it.

Jan

Quoted 10 lines

3) keep the only call __printk_ratelimit() in a hope of there will be per-subsystem rate limiting in the future?

--
Denis

Quoted 3 lines

Jan

#10 lore raw
In reply to [email protected]
Quoted 27 lines · On Fri, Jul 25, 2025 at 09:24:48PM +0000, [email protected] wrote:

From: Denis Mukhin <[email protected]>

__printk_ratelimit() is never used outside of the console driver. Remove it from the lib.h and merge with the public printk_ratelimit().

Not a functional change.

---
 xen/drivers/char/console.c | 29 ++++++++++++-----------------
 xen/include/xen/lib.h      |  1 -
 2 files changed, 12 insertions(+), 18 deletions(-)
xen/drivers/char/console.c +1 -1
index 75fa033ce74d..80f8f2ed1bae 100644
@@ -979,7 +979,7 @@ static void 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 printk_ratelimit(). */
     local_irq_save(flags);
     rspin_lock(&console_lock);
     state = &this_cpu(state);

xen/drivers/char/console.c void console_end_sync(void) · line 1273

Quoted 8 lines
xen/drivers/char/console.c +5 -0
@@ -1266,13 +1266,19 @@ void console_end_sync(void)
     atomic_dec(&print_everything);
 }
 
+/* minimum time in ms between messages */
+static int __read_mostly printk_ratelimit_ms = 5 * 1000;
+
+/* number of messages we send before ratelimiting */
+static int __read_mostly printk_ratelimit_burst = 10;

You possibly want to make them const for the time being?

It's unclear whether we would like to dynamically change those values in the future. Given the current usage they would better be const.

xen/drivers/char/console.c void console_end_sync(void) · line 1281

Quoted 9 lines
xen/drivers/char/console.c +2 -1
+
 /*
  * printk rate limiting, lifted from Linux.
  *
  * This enforces a rate limit: not more than one kernel message
  * every printk_ratelimit_ms (millisecs).
  */
-int __printk_ratelimit(int ratelimit_ms, int ratelimit_burst)
+int printk_ratelimit(void)

As suggested by Jan, I would just make this static and remove the prototype from lib.h. That results in less changes, and allows to more easily export the fine grained function in the future if we had a need to.

Thanks, Roger.