Skip to contentSkip to conversation

console: make printk_ratelimit_{burst,ms} const

PATCH In review Jan Beulich 3 participants 5 messages Aug 1, 2025 – Aug 5, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 console: make printk_ratelimit_{burst,ms} const

Them not being altered by any means, their __read_mostly attribute is actually counter-productive: It causes the compiler to instantiate the variables, when already with just the attributes dropped the compiler can constant-propagate the values into the sole use site. Make the situation yet more explicit by adding const.

Also switch the variables away from being plain int, and have the parameters of __printk_ratelimit() follow suit. While there also similarly adjust the type of "missed" and "lost", and - while touching the adjacent line - increase lost_str[] to accommodate any unsigned 32-bit number.

Fixes a8b1845a7845 ("Miscellaneous data placement adjustments")

---
In principle {__,}printk_ratelimit() may also want to have their return type changed to bool, but I think doing so would go too far here: This would have knock-on effects elsewhere, and it would want considering to actually flip polarity.

Despite the Fixes: tag I wouldn't consider this for backport.

xen/drivers/char/console.c +11 -9
@@ -1268,12 +1268,12 @@ void console_end_sync(void)
  * 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(unsigned int ratelimit_ms, unsigned int ratelimit_burst)
 {
     static DEFINE_SPINLOCK(ratelimit_lock);
     static unsigned long toks = 10 * 5 * 1000;
     static unsigned long last_msg;
-    static int missed;
+    static unsigned int missed;
     unsigned long flags;
     unsigned long long now = NOW(); /* ns */
     unsigned long ms;
@@ -1288,14 +1288,16 @@ int __printk_ratelimit(int ratelimit_ms,
         toks = ratelimit_burst * ratelimit_ms;
     if ( toks >= ratelimit_ms )
     {
-        int lost = missed;
+        unsigned int lost = missed;
+
         missed = 0;
         toks -= ratelimit_ms;
         spin_unlock(&ratelimit_lock);
         if ( lost )
         {
-            char lost_str[8];
-            snprintf(lost_str, sizeof(lost_str), "%d", lost);
+            char lost_str[10];
+
+            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);
@@ -1312,11 +1314,11 @@ int __printk_ratelimit(int ratelimit_ms,
     return 0;
 }
 
-/* minimum time in ms between messages */
-static int __read_mostly printk_ratelimit_ms = 5 * 1000;
+/* Minimum time in ms between messages */
+static const unsigned int printk_ratelimit_ms = 5 * 1000;
 
-/* number of messages we send before ratelimiting */
-static int __read_mostly printk_ratelimit_burst = 10;
+/* Number of messages we send before ratelimiting */
+static const unsigned int printk_ratelimit_burst = 10;
 
 int printk_ratelimit(void)
 {
xen/include/xen/lib.h +2 -1
@@ -80,7 +80,8 @@ extern void guest_printk(const struct do
     __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(unsigned int ratelimit_ms,
+                              unsigned int ratelimit_burst);
 extern int printk_ratelimit(void);
 
 #define gprintk(lvl, fmt, args...) \
#1 lore raw
Quoted 46 lines · Jan Beulich

Them not being altered by any means, their __read_mostly attribute is actually counter-productive: It causes the compiler to instantiate the variables, when already with just the attributes dropped the compiler can constant-propagate the values into the sole use site. Make the situation yet more explicit by adding const.

Also switch the variables away from being plain int, and have the parameters of __printk_ratelimit() follow suit. While there also similarly adjust the type of "missed" and "lost", and - while touching the adjacent line - increase lost_str[] to accommodate any unsigned 32-bit number.

Fixes a8b1845a7845 ("Miscellaneous data placement adjustments")

---
In principle {__,}printk_ratelimit() may also want to have their return type changed to bool, but I think doing so would go too far here: This would have knock-on effects elsewhere, and it would want considering to actually flip polarity.

Despite the Fixes: tag I wouldn't consider this for backport.

xen/drivers/char/console.c +4 -3
@@ -1268,12 +1268,12 @@ void console_end_sync(void)
  * 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(unsigned int ratelimit_ms, unsigned int ratelimit_burst)
 {
     static DEFINE_SPINLOCK(ratelimit_lock);
     static unsigned long toks = 10 * 5 * 1000;
     static unsigned long last_msg;
-    static int missed;
+    static unsigned int missed;
     unsigned long flags;
     unsigned long long now = NOW(); /* ns */
     unsigned long ms;
@@ -1288,14 +1288,16 @@ int __printk_ratelimit(int ratelimit_ms,
         toks = ratelimit_burst * ratelimit_ms;
     if ( toks >= ratelimit_ms )
     {
-        int lost = missed;
+        unsigned int lost = missed;
+

xen/drivers/char/console.c int __printk_ratelimit(int ratelimit_ms, · line 1300

Quoted 10 lines
xen/drivers/char/console.c +3 -2
         missed = 0;
         toks -= ratelimit_ms;
         spin_unlock(&ratelimit_lock);
         if ( lost )
         {
-            char lost_str[8];
-            snprintf(lost_str, sizeof(lost_str), "%d", lost);
+            char lost_str[10];
+
+            snprintf(lost_str, sizeof(lost_str), "%u", lost);

Since this code is touched, I would also simplify the entire if ( lost ) block (I have it done in another experiment):

            char lost_str[64];
            size_t lost_len = snprintf(lost_str, sizeof(lost_str),
                                       "printk: %d messages suppressed.\n",
                                       lost_str);

            /* console_lock may already be acquired by printk(). */
            rspin_lock(&console_lock);
            printk_start_of_line(CONSOLE_PREFIX, cflags);
            __putstr(lost_str, lost_len);
            ...

What do you think?

--
Denis

#2 lore raw
Quoted 72 lines · On 01.08.2025 20:58, [email protected] wrote:
Quoted 46 lines · Jan Beulich

Them not being altered by any means, their __read_mostly attribute is actually counter-productive: It causes the compiler to instantiate the variables, when already with just the attributes dropped the compiler can constant-propagate the values into the sole use site. Make the situation yet more explicit by adding const.

Also switch the variables away from being plain int, and have the parameters of __printk_ratelimit() follow suit. While there also similarly adjust the type of "missed" and "lost", and - while touching the adjacent line - increase lost_str[] to accommodate any unsigned 32-bit number.

Fixes a8b1845a7845 ("Miscellaneous data placement adjustments")

---
In principle {__,}printk_ratelimit() may also want to have their return type changed to bool, but I think doing so would go too far here: This would have knock-on effects elsewhere, and it would want considering to actually flip polarity.

Despite the Fixes: tag I wouldn't consider this for backport.

xen/drivers/char/console.c +4 -3
@@ -1268,12 +1268,12 @@ void console_end_sync(void)
  * 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(unsigned int ratelimit_ms, unsigned int ratelimit_burst)
 {
     static DEFINE_SPINLOCK(ratelimit_lock);
     static unsigned long toks = 10 * 5 * 1000;
     static unsigned long last_msg;
-    static int missed;
+    static unsigned int missed;
     unsigned long flags;
     unsigned long long now = NOW(); /* ns */
     unsigned long ms;
@@ -1288,14 +1288,16 @@ int __printk_ratelimit(int ratelimit_ms,
         toks = ratelimit_burst * ratelimit_ms;
     if ( toks >= ratelimit_ms )
     {
-        int lost = missed;
+        unsigned int lost = missed;
+

xen/drivers/char/console.c int __printk_ratelimit(int ratelimit_ms, · line 1300

Quoted 10 lines
xen/drivers/char/console.c +3 -2
         missed = 0;
         toks -= ratelimit_ms;
         spin_unlock(&ratelimit_lock);
         if ( lost )
         {
-            char lost_str[8];
-            snprintf(lost_str, sizeof(lost_str), "%d", lost);
+            char lost_str[10];
+
+            snprintf(lost_str, sizeof(lost_str), "%u", lost);

Since this code is touched, I would also simplify the entire if ( lost ) block (I have it done in another experiment):

            char lost_str[64];
            size_t lost_len = snprintf(lost_str, sizeof(lost_str),
                                       "printk: %d messages suppressed.\n",
                                       lost_str);

            /* console_lock may already be acquired by printk(). */
            rspin_lock(&console_lock);
            printk_start_of_line(CONSOLE_PREFIX, cflags);
            __putstr(lost_str, lost_len);
            ...
Quoted 1 line

What do you think?

Maybe, but definitely not right in this patch.

Jan

Reviewed-by Denis Mukhin
#3 lore raw
Quoted 73 lines · Jan Beulich
Quoted 72 lines · On 01.08.2025 20:58, [email protected] wrote:
Quoted 46 lines · Jan Beulich

Them not being altered by any means, their __read_mostly attribute is actually counter-productive: It causes the compiler to instantiate the variables, when already with just the attributes dropped the compiler can constant-propagate the values into the sole use site. Make the situation yet more explicit by adding const.

Also switch the variables away from being plain int, and have the parameters of __printk_ratelimit() follow suit. While there also similarly adjust the type of "missed" and "lost", and - while touching the adjacent line - increase lost_str[] to accommodate any unsigned 32-bit number.

Fixes a8b1845a7845 ("Miscellaneous data placement adjustments")

---
In principle {__,}printk_ratelimit() may also want to have their return type changed to bool, but I think doing so would go too far here: This would have knock-on effects elsewhere, and it would want considering to actually flip polarity.

Despite the Fixes: tag I wouldn't consider this for backport.

xen/drivers/char/console.c +4 -3
@@ -1268,12 +1268,12 @@ void console_end_sync(void)
  * 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(unsigned int ratelimit_ms, unsigned int ratelimit_burst)
 {
     static DEFINE_SPINLOCK(ratelimit_lock);
     static unsigned long toks = 10 * 5 * 1000;
     static unsigned long last_msg;
-    static int missed;
+    static unsigned int missed;
     unsigned long flags;
     unsigned long long now = NOW(); /* ns */
     unsigned long ms;
@@ -1288,14 +1288,16 @@ int __printk_ratelimit(int ratelimit_ms,
         toks = ratelimit_burst * ratelimit_ms;
     if ( toks >= ratelimit_ms )
     {
-        int lost = missed;
+        unsigned int lost = missed;
+

xen/drivers/char/console.c int __printk_ratelimit(int ratelimit_ms, · line 1300

Quoted 10 lines
xen/drivers/char/console.c +3 -2
         missed = 0;
         toks -= ratelimit_ms;
         spin_unlock(&ratelimit_lock);
         if ( lost )
         {
-            char lost_str[8];
-            snprintf(lost_str, sizeof(lost_str), "%d", lost);
+            char lost_str[10];
+
+            snprintf(lost_str, sizeof(lost_str), "%u", lost);

Since this code is touched, I would also simplify the entire if ( lost ) block (I have it done in another experiment):

            char lost_str[64];
            size_t lost_len = snprintf(lost_str, sizeof(lost_str),
                                       "printk: %d messages suppressed.\n",
                                       lost_str);

            /* console_lock may already be acquired by printk(). */
            rspin_lock(&console_lock);
            printk_start_of_line(CONSOLE_PREFIX, cflags);
            __putstr(lost_str, lost_len);
            ...
Quoted 3 lines
Quoted 1 line

What do you think?

Maybe, but definitely not right in this patch.

Sure, please consider

Reviewed-by on v1 1/1
#4 lore raw
In reply to Jan Beulich
Quoted 6 lines · Jan Beulich

Them not being altered by any means, their __read_mostly attribute is actually counter-productive: It causes the compiler to instantiate the variables, when already with just the attributes dropped the compiler can constant-propagate the values into the sole use site. Make the situation yet more explicit by adding const.

Quoted 8 lines

Also switch the variables away from being plain int, and have the parameters of __printk_ratelimit() follow suit. While there also similarly adjust the type of "missed" and "lost", and - while touching the adjacent line - increase lost_str[] to accommodate any unsigned 32-bit number.

Fixes a8b1845a7845 ("Miscellaneous data placement adjustments")