Skip to contentSkip to conversation

xen/console: few cleanups in console driver

PATCH Superseded [email protected] 3 participants 7 messages May 4, 2025 – May 16, 2025

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover xen/console: few cleanups in console driver

The patch series introduces a few cleanups aimed at reducing code duplication in the console driver.

Originally, patches 2 and 3 were part of NS16550 emulator v3 series [1].

Patch 1 removes some code duplication for logging via conring facility.

Patch 2 (see [2]) removes code duplication between __putstr() and the rest of the driver. It also introduces private flags to select console devices for printout which simplifies some code paths.

Patch 3 (see [3]) adds conring_flush() to send contents of conring to all currently available console devices.

[1] https://lore.kernel.org/xen-devel/[email protected]/
[2] https://lore.kernel.org/xen-devel/[email protected]/
[3] https://lore.kernel.org/xen-devel/[email protected]/
[4] Link to v2: https://lore.kernel.org/xen-devel/[email protected]/
[5] Link to CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/1799484502

Denis Mukhin (3):
xen/console: cleanup conring management
xen/console: introduce console_puts()
xen/console: introduce conring_flush()

 xen/drivers/char/console.c | 186 +++++++++++++++++++++++--------------
 1 file changed, 115 insertions(+), 71 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v3 1/3 xen/console: cleanup conring management

From: Denis Mukhin <[email protected]>

Add new parameter to conring_puts() to trigger conring VIRQ when needed.

That cleans up the conring management because it removes VIRQ tasklet code from few places.

Move conring tasklet code close to conring definitions in the console driver to enable symbols use in conring_puts(). While doing it, rename conring tasklet variables to match the code.

No functional change.

---
Changes since v2:
- renamed notify_dom0_con_ring_ prefix to conring_ prefix for readability
- renamed console_locks_busted to conring_no_notify for readability
- added extra parameter to conring_puts() to trigger VIRQ_CON_RING

---
 xen/drivers/char/console.c | 36 +++++++++++++++++-------------------
 1 file changed, 17 insertions(+), 19 deletions(-)
xen/drivers/char/console.c +17 -19
index c3150fbdb7..20504060cb 100644
@@ -325,7 +325,17 @@ static void cf_check do_dec_thresh(unsigned char key, bool unused)
  * ********************************************************
  */
 
-static void conring_puts(const char *str, size_t len)
+static void cf_check conring_notify(void *unused)
+{
+    send_global_virq(VIRQ_CON_RING);
+}
+
+static DECLARE_SOFTIRQ_TASKLET(conring_tasklet, conring_notify, NULL);
+
+/* NB: Do not send conring VIRQs during panic. */
+static bool conring_no_notify;
+
+static void conring_puts(const char *str, size_t len, bool notify)
 {
     ASSERT(rspin_is_locked(&console_lock));
 
@@ -334,6 +344,9 @@ static void conring_puts(const char *str, size_t len)
 
     if ( conringp - conringc > conring_size )
         conringc = conringp - conring_size;
+
+    if ( notify )
+        tasklet_schedule(&conring_tasklet);
 }
 
 long read_console_ring(struct xen_sysctl_readconsole *op)
@@ -594,13 +607,6 @@ static void cf_check serial_rx(char c)
     __serial_rx(c);
 }
 
-static void cf_check notify_dom0_con_ring(void *unused)
-{
-    send_global_virq(VIRQ_CON_RING);
-}
-static DECLARE_SOFTIRQ_TASKLET(notify_dom0_con_ring_tasklet,
-                               notify_dom0_con_ring, NULL);
-
 #ifdef CONFIG_X86
 static inline void xen_console_write_debug_port(const char *buf, size_t len)
 {
@@ -648,10 +654,7 @@ static long guest_console_write(XEN_GUEST_HANDLE_PARAM(char) buffer,
 #endif
 
             if ( opt_console_to_ring )
-            {
-                conring_puts(kbuf, kcount);
-                tasklet_schedule(&notify_dom0_con_ring_tasklet);
-            }
+                conring_puts(kbuf, kcount, true);
 
             nrspin_unlock_irq(&console_lock);
         }
@@ -753,8 +756,6 @@ long do_console_io(
  * *****************************************************
  */
 
-static bool console_locks_busted;
-
 static void __putstr(const char *str)
 {
     size_t len = strlen(str);
@@ -774,10 +775,7 @@ static void __putstr(const char *str)
     }
 #endif
 
-    conring_puts(str, len);
-
-    if ( !console_locks_busted )
-        tasklet_schedule(&notify_dom0_con_ring_tasklet);
+    conring_puts(str, len, !conring_no_notify);
 }
 
 static int printk_prefix_check(char *p, char **pp)
@@ -1171,7 +1169,7 @@ void console_force_unlock(void)
     spin_debug_disable();
     rspin_lock_init(&console_lock);
     serial_force_unlock(sercon_handle);
-    console_locks_busted = 1;
+    conring_no_notify = true;
     console_start_sync();
 }
 
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v3 1/3] xen/console: cleanup conring management

xen/drivers/char/console.c static void conring_puts(const char *str, size_t len) · line 350

Quoted 8 lines · On 04.05.2025 20:14, [email protected] wrote:
xen/drivers/char/console.c +3 -0
@@ -334,6 +344,9 @@ static void conring_puts(const char *str, size_t len)
 
     if ( conringp - conringc > conring_size )
         conringc = conringp - conring_size;
+
+    if ( notify )
+        tasklet_schedule(&conring_tasklet);
 }

Just to re-state my earlier concern: I'm not convinced this belongs in a function named "puts".

Jan

#3 lore raw

PATCH v3 2/3 xen/console: introduce console_puts()

In reply to [email protected]

From: Denis Mukhin <[email protected]>

guest_console_write() duplicates the code from __putstr(), eliminate code duplication.

Introduce console_puts() for writing a buffer to console devices.

Also, introduce internal console flags to control which console devices should be used.

No functional change intended.

---
Changes since v2:
- added CONSOLE_RING_VIRQ flag
- dropped locking changes

---
 xen/drivers/char/console.c | 123 ++++++++++++++++++++++++-------------
 1 file changed, 79 insertions(+), 44 deletions(-)
xen/drivers/char/console.c +79 -44
index 20504060cb..b788427aa5 100644
@@ -41,6 +41,28 @@
 #include <asm/vpl011.h>
 #endif
 
+/* Internal console flags. */
+enum {
+    CONSOLE_SERIAL          = BIT(0, U),    /* Use serial device. */
+    CONSOLE_PV              = BIT(1, U),    /* Use PV console. */
+    CONSOLE_VIDEO           = BIT(2, U),    /* Use video device. */
+    CONSOLE_DEBUG           = BIT(3, U),    /* Use debug device. */
+    CONSOLE_RING            = BIT(4, U),    /* Use console ring. */
+    CONSOLE_RING_VIRQ       = BIT(5, U),    /* Use console ring VIRQ. */
+
+    /* Default console flags. */
+    CONSOLE_DEFAULT         = CONSOLE_SERIAL |
+                              CONSOLE_PV |
+                              CONSOLE_VIDEO |
+                              CONSOLE_RING_VIRQ |
+                              CONSOLE_DEBUG,
+
+    /* Use all known console devices. */
+    CONSOLE_ALL             = CONSOLE_DEFAULT | CONSOLE_RING,
+};
+
+static void console_puts(const char *str, size_t len, unsigned int flags);
+
 /* console: comma-separated list of console outputs. */
 static char __initdata opt_console[30] = OPT_CONSOLE_STR;
 string_param("console", opt_console);
@@ -431,9 +453,6 @@ void console_serial_puts(const char *s, size_t nr)
         serial_steal_fn(s, nr);
     else
         serial_puts(sercon_handle, s, nr);
-
-    /* Copy all serial output into PV console */
-    pv_console_puts(s, nr);
 }
 
 static void cf_check dump_console_ring_key(unsigned char key)
@@ -467,8 +486,7 @@ static void cf_check dump_console_ring_key(unsigned char key)
         c += len;
     }
 
-    console_serial_puts(buf, sofar);
-    video_puts(buf, sofar);
+    console_puts(buf, sofar, CONSOLE_SERIAL | CONSOLE_VIDEO | CONSOLE_PV);
 
     free_xenheap_pages(buf, order);
 }
@@ -617,11 +635,66 @@ static inline void xen_console_write_debug_port(const char *buf, size_t len)
 }
 #endif
 
+static inline void console_debug_puts(const char *str, size_t len)
+{
+#ifdef CONFIG_X86
+    if ( opt_console_xen )
+    {
+        if ( xen_guest )
+            xen_hypercall_console_write(str, len);
+        else
+            xen_console_write_debug_port(str, len);
+    }
+#endif
+}
+
+/*
+ * Write buffer to all enabled console devices.
+ *
+ * That will handle all possible scenarios working w/ console
+ * - physical console (serial console, VGA console (x86 only));
+ * - PV console;
+ * - debug console (x86 only): debug I/O port or __HYPERVISOR_console_io
+ *   hypercall;
+ * - console ring.
+ */
+static void console_puts(const char *str, size_t len, unsigned int flags)
+{
+    if ( flags & CONSOLE_SERIAL )
+        console_serial_puts(str, len);
+
+    if ( flags & CONSOLE_PV )
+        pv_console_puts(str, len);
+
+    if ( flags & CONSOLE_VIDEO )
+        video_puts(str, len);
+
+    if ( flags & CONSOLE_DEBUG )
+        console_debug_puts(str, len);
+
+    if ( flags & CONSOLE_RING )
+        conring_puts(str, len, !!(flags & CONSOLE_RING_VIRQ) );
+}
+
+static inline void __putstr(const char *str)
+{
+    unsigned int flags = CONSOLE_ALL;
+
+    ASSERT(rspin_is_locked(&console_lock));
+
+    if ( conring_no_notify )
+        flags &= ~CONSOLE_RING_VIRQ;
+
+    console_puts(str, strlen(str), flags);
+}
+
 static long guest_console_write(XEN_GUEST_HANDLE_PARAM(char) buffer,
                                 unsigned int count)
 {
     char kbuf[128];
     unsigned int kcount = 0;
+    unsigned int flags = opt_console_to_ring
+                         ? CONSOLE_ALL : CONSOLE_DEFAULT;
     struct domain *cd = current->domain;
 
     while ( count > 0 )
@@ -639,23 +712,7 @@ static long guest_console_write(XEN_GUEST_HANDLE_PARAM(char) buffer,
         {
             /* Use direct console output as it could be interactive */
             nrspin_lock_irq(&console_lock);
-
-            console_serial_puts(kbuf, kcount);
-            video_puts(kbuf, kcount);
-
-#ifdef CONFIG_X86
-            if ( opt_console_xen )
-            {
-                if ( xen_guest )
-                    xen_hypercall_console_write(kbuf, kcount);
-                else
-                    xen_console_write_debug_port(kbuf, kcount);
-            }
-#endif
-
-            if ( opt_console_to_ring )
-                conring_puts(kbuf, kcount, true);
-
+            console_puts(kbuf, kcount, flags);
             nrspin_unlock_irq(&console_lock);
         }
         else
@@ -756,28 +813,6 @@ long do_console_io(
  * *****************************************************
  */
 
-static void __putstr(const char *str)
-{
-    size_t len = strlen(str);
-
-    ASSERT(rspin_is_locked(&console_lock));
-
-    console_serial_puts(str, len);
-    video_puts(str, len);
-
-#ifdef CONFIG_X86
-    if ( opt_console_xen )
-    {
-        if ( xen_guest )
-            xen_hypercall_console_write(str, len);
-        else
-            xen_console_write_debug_port(str, len);
-    }
-#endif
-
-    conring_puts(str, len, !conring_no_notify);
-}
-
 static int printk_prefix_check(char *p, char **pp)
 {
     int loglvl = -1;
Signature · 1 line
2.34.1
Reviewed-by on v3 2/3
#4 lore raw

Re: [PATCH v3 2/3] xen/console: introduce console_puts()

Quoted 5 lines · On Sun, 4 May 2025, [email protected] wrote:

From: Denis Mukhin <[email protected]>

guest_console_write() duplicates the code from __putstr(), eliminate code duplication.

Quoted 8 lines

Introduce console_puts() for writing a buffer to console devices.

Also, introduce internal console flags to control which console devices should be used.

No functional change intended.

Quoted 186 lines

---
Changes since v2:
- added CONSOLE_RING_VIRQ flag
- dropped locking changes

---
 xen/drivers/char/console.c | 123 ++++++++++++++++++++++++-------------
 1 file changed, 79 insertions(+), 44 deletions(-)
xen/drivers/char/console.c +79 -44
index 20504060cb..b788427aa5 100644
@@ -41,6 +41,28 @@
 #include <asm/vpl011.h>
 #endif
 
+/* Internal console flags. */
+enum {
+    CONSOLE_SERIAL          = BIT(0, U),    /* Use serial device. */
+    CONSOLE_PV              = BIT(1, U),    /* Use PV console. */
+    CONSOLE_VIDEO           = BIT(2, U),    /* Use video device. */
+    CONSOLE_DEBUG           = BIT(3, U),    /* Use debug device. */
+    CONSOLE_RING            = BIT(4, U),    /* Use console ring. */
+    CONSOLE_RING_VIRQ       = BIT(5, U),    /* Use console ring VIRQ. */
+
+    /* Default console flags. */
+    CONSOLE_DEFAULT         = CONSOLE_SERIAL |
+                              CONSOLE_PV |
+                              CONSOLE_VIDEO |
+                              CONSOLE_RING_VIRQ |
+                              CONSOLE_DEBUG,
+
+    /* Use all known console devices. */
+    CONSOLE_ALL             = CONSOLE_DEFAULT | CONSOLE_RING,
+};
+
+static void console_puts(const char *str, size_t len, unsigned int flags);
+
 /* console: comma-separated list of console outputs. */
 static char __initdata opt_console[30] = OPT_CONSOLE_STR;
 string_param("console", opt_console);
@@ -431,9 +453,6 @@ void console_serial_puts(const char *s, size_t nr)
         serial_steal_fn(s, nr);
     else
         serial_puts(sercon_handle, s, nr);
-
-    /* Copy all serial output into PV console */
-    pv_console_puts(s, nr);
 }
 
 static void cf_check dump_console_ring_key(unsigned char key)
@@ -467,8 +486,7 @@ static void cf_check dump_console_ring_key(unsigned char key)
         c += len;
     }
 
-    console_serial_puts(buf, sofar);
-    video_puts(buf, sofar);
+    console_puts(buf, sofar, CONSOLE_SERIAL | CONSOLE_VIDEO | CONSOLE_PV);
 
     free_xenheap_pages(buf, order);
 }
@@ -617,11 +635,66 @@ static inline void xen_console_write_debug_port(const char *buf, size_t len)
 }
 #endif
 
+static inline void console_debug_puts(const char *str, size_t len)
+{
+#ifdef CONFIG_X86
+    if ( opt_console_xen )
+    {
+        if ( xen_guest )
+            xen_hypercall_console_write(str, len);
+        else
+            xen_console_write_debug_port(str, len);
+    }
+#endif
+}
+
+/*
+ * Write buffer to all enabled console devices.
+ *
+ * That will handle all possible scenarios working w/ console
+ * - physical console (serial console, VGA console (x86 only));
+ * - PV console;
+ * - debug console (x86 only): debug I/O port or __HYPERVISOR_console_io
+ *   hypercall;
+ * - console ring.
+ */
+static void console_puts(const char *str, size_t len, unsigned int flags)
+{
+    if ( flags & CONSOLE_SERIAL )
+        console_serial_puts(str, len);
+
+    if ( flags & CONSOLE_PV )
+        pv_console_puts(str, len);
+
+    if ( flags & CONSOLE_VIDEO )
+        video_puts(str, len);
+
+    if ( flags & CONSOLE_DEBUG )
+        console_debug_puts(str, len);
+
+    if ( flags & CONSOLE_RING )
+        conring_puts(str, len, !!(flags & CONSOLE_RING_VIRQ) );
+}
+
+static inline void __putstr(const char *str)
+{
+    unsigned int flags = CONSOLE_ALL;
+
+    ASSERT(rspin_is_locked(&console_lock));
+
+    if ( conring_no_notify )
+        flags &= ~CONSOLE_RING_VIRQ;
+
+    console_puts(str, strlen(str), flags);
+}
+
 static long guest_console_write(XEN_GUEST_HANDLE_PARAM(char) buffer,
                                 unsigned int count)
 {
     char kbuf[128];
     unsigned int kcount = 0;
+    unsigned int flags = opt_console_to_ring
+                         ? CONSOLE_ALL : CONSOLE_DEFAULT;
     struct domain *cd = current->domain;
 
     while ( count > 0 )
@@ -639,23 +712,7 @@ static long guest_console_write(XEN_GUEST_HANDLE_PARAM(char) buffer,
         {
             /* Use direct console output as it could be interactive */
             nrspin_lock_irq(&console_lock);
-
-            console_serial_puts(kbuf, kcount);
-            video_puts(kbuf, kcount);
-
-#ifdef CONFIG_X86
-            if ( opt_console_xen )
-            {
-                if ( xen_guest )
-                    xen_hypercall_console_write(kbuf, kcount);
-                else
-                    xen_console_write_debug_port(kbuf, kcount);
-            }
-#endif
-
-            if ( opt_console_to_ring )
-                conring_puts(kbuf, kcount, true);
-
+            console_puts(kbuf, kcount, flags);
             nrspin_unlock_irq(&console_lock);
         }
         else
@@ -756,28 +813,6 @@ long do_console_io(
  * *****************************************************
  */
 
-static void __putstr(const char *str)
-{
-    size_t len = strlen(str);
-
-    ASSERT(rspin_is_locked(&console_lock));
-
-    console_serial_puts(str, len);
-    video_puts(str, len);
-
-#ifdef CONFIG_X86
-    if ( opt_console_xen )
-    {
-        if ( xen_guest )
-            xen_hypercall_console_write(str, len);
-        else
-            xen_console_write_debug_port(str, len);
-    }
-#endif
-
-    conring_puts(str, len, !conring_no_notify);
-}
-
 static int printk_prefix_check(char *p, char **pp)
 {
     int loglvl = -1;
Signature · 1 line
2.34.1
#5 lore raw

Re: [PATCH v3 2/3] xen/console: introduce console_puts()

Quoted 8 lines · Stefano Stabellini
Quoted 7 lines · On Sun, 4 May 2025, [email protected] wrote:

From: Denis Mukhin <[email protected]>

guest_console_write() duplicates the code from __putstr(), eliminate code duplication.

Introduce console_puts() for writing a buffer to console devices.

Quoted 8 lines
Quoted 6 lines

Also, introduce internal console flags to control which console devices should be used.

No functional change intended.

Sorry I replied to the wrong version, I meant to ack v4.

#6 lore raw

PATCH v3 3/3 xen/console: introduce conring_flush()

In reply to [email protected]

From: Denis Mukhin <[email protected]>

Introduce conring_flush() to ensure all messages kept in the internal console ring are sent to all physical consoles (serial, VGA (x86)) after their initialization is completed.

Rename dump_console_ring_key to conring_dump_keyhandler to match the notation for conring management symbols.

Resolves: https://gitlab.com/xen-project/xen/-/issues/184

---
Changes since v2:
- Kept Stefano's R-b

---
 xen/drivers/char/console.c | 31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)
xen/drivers/char/console.c +21 -10
index b788427aa5..a089d6e292 100644
@@ -455,23 +455,19 @@ void console_serial_puts(const char *s, size_t nr)
         serial_puts(sercon_handle, s, nr);
 }
 
-static void cf_check dump_console_ring_key(unsigned char key)
+/*
+ * Flush contents of the conring to the physical console devices.
+ */
+static int conring_flush(void)
 {
     uint32_t idx, len, sofar, c;
     unsigned int order;
     char *buf;
 
-    printk("'%c' pressed -> dumping console ring buffer (dmesg)\n", key);
-
-    /* create a buffer in which we'll copy the ring in the correct
-       order and NUL terminate */
     order = get_order_from_bytes(conring_size + 1);
     buf = alloc_xenheap_pages(order, 0);
     if ( buf == NULL )
-    {
-        printk("unable to allocate memory!\n");
-        return;
-    }
+        return -ENOMEM;
 
     c = conringc;
     sofar = 0;
@@ -489,6 +485,18 @@ static void cf_check dump_console_ring_key(unsigned char key)
     console_puts(buf, sofar, CONSOLE_SERIAL | CONSOLE_VIDEO | CONSOLE_PV);
 
     free_xenheap_pages(buf, order);
+
+    return 0;
+}
+
+static void cf_check conring_dump_keyhandler(unsigned char key)
+{
+    int rc;
+
+    printk("'%c' pressed -> dumping console ring buffer (dmesg)\n", key);
+    rc = conring_flush();
+    if ( rc )
+        printk("failed to dump console ring buffer: %d\n", rc);
 }
 
 /*
@@ -1058,6 +1066,9 @@ void __init console_init_preirq(void)
     serial_set_rx_handler(sercon_handle, serial_rx);
     pv_console_set_rx_handler(serial_rx);
 
+    /* NB: send conring contents to all enabled physical consoles, if any */
+    conring_flush();
+
     /* HELLO WORLD --- start-of-day banner text. */
     nrspin_lock(&console_lock);
     __putstr(xen_banner());
@@ -1148,7 +1159,7 @@ void __init console_endboot(void)
     if ( opt_conswitch[1] == 'x' )
         console_rx = max_console_rx;
 
-    register_keyhandler('w', dump_console_ring_key,
+    register_keyhandler('w', conring_dump_keyhandler,
                         "synchronously dump console ring buffer (dmesg)", 0);
     register_irq_keyhandler('+', &do_inc_thresh,
                             "increase log level threshold", 0);
Signature · 1 line
2.34.1