xen/console: cleanup console input switch logic
v3 cover xen/console: cleanup console input switch logic
The patch series originates from the NS16550 UART emulator series [1] (x86) which requires ability to switch physical console input to a PVH/HVM domain with an emulated UART.
Currently, on x86, console input can be rotated in round-robin manner only between dom0, PV shim, and Xen itself. On Arm the input rotation can include domUs with vpl011.
The main idea of this series is introducing a per-domain permission flag that is set during domain initialization and used by the console driver to switch the input across permitted domains.
Patch 1 performs rename of switch_serial_input() to fit the console driver notation.
Patch 2 simplifies the existing vUART code by using newly introduced API.
Patch 3 introduces a new domain permission flag to mark ownership of the console input for the console driver.
Patch 4 cleans up the console input switch logic by removing dependency on max_init_domid. Depends on patches 1, 3 and [2].
Patch 5 performs rename of console_rx to console_domid to match the usage of the symbol in the code.
[1] https://lore.kernel.org/xen-devel/[email protected]/
[2] https://lore.kernel.org/xen-devel/[email protected]/
[3] Link to v2: https://lore.kernel.org/xen-devel/[email protected]/
[4] Link to CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/1825448327
Denis Mukhin (5):
xen/console: rename switch_serial_input() to console_switch_input() xen/console: introduce console_get_domid()
xen/console: introduce console input permission xen/console: remove max_init_domid dependency xen/console: rename console_rx to console_domid
xen/arch/arm/include/asm/setup.h | 2 - xen/arch/arm/setup.c | 2 - xen/arch/arm/vpl011.c | 7 +- xen/arch/ppc/include/asm/setup.h | 2 - xen/arch/riscv/include/asm/setup.h | 2 - xen/arch/x86/include/asm/setup.h | 2 - xen/arch/x86/pv/shim.c | 2 + xen/common/device-tree/dom0less-build.c | 2 - xen/common/domain.c | 31 +++++++ xen/drivers/char/console.c | 105 +++++++++++------------- xen/include/xen/console.h | 3 +- xen/include/xen/domain.h | 1 + xen/include/xen/sched.h | 8 +- 13 files changed, 94 insertions(+), 75 deletions(-)
Signature · 1 line
2.34.1
PATCH v3 1/5 xen/console: rename switch_serial_input() to console_switch_input()
From: Denis Mukhin <[email protected]>
Update the function name as per naming notation in the console driver.
No functional change.
---
Changes since v2:
- rebased
- Link to v2: https://lore.kernel.org/xen-devel/[email protected]/
--- xen/drivers/char/console.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
xen/drivers/char/console.c +3 -3
@@ -487,7 +487,7 @@ void console_put_domain(struct domain *d)rcu_unlock_domain (d); } -static void switch_serial_input (void ) +static void console_switch_input (void ) {unsigned int next_rx = console_rx;
@@ -581,7 +581,7 @@ static void cf_check serial_rx(char c)/* We eat CTRL-<switch_char> in groups of 3 to switch console input. */ if ( ++switch_code_count ==3 ) { -switch_serial_input (); +console_switch_input (); switch_code_count =0 ; }return ;
@@ -1125,7 +1125,7 @@ void __init console_endboot(void) "toggle host/guest log level adjustment", 0);/* Serial input is directed to DOM0 by default. */ -switch_serial_input (); +console_switch_input (); }int __init console_has (const char *device)
Signature · 1 line
2.34.1
Re: [PATCH v3 1/5] xen/console: rename switch_serial_input() to console_switch_input()
Quoted 7 lines · On 19.05.2025 22:12, [email protected] wrote:
From: Denis Mukhin <[email protected]>
Update the function name as per naming notation in the console driver.
No functional change.
Signed-off-by Denis Mukhin <[email protected]>
PATCH v3 2/5 xen/console: introduce console_get_domid()
In reply to [email protected]From: Denis Mukhin <[email protected]>
Add console_get_domid() to the console driver to retrieve the current console owner domain ID.
Use the function in vpl011 emulator which leads to simpler code.
Make console_{get,put}_domain() private to the console driver.
No functional change intended.
---
Changes since v2:
- changed commit subject line
- Link to v2: https://lore.kernel.org/xen-devel/[email protected]/
--- xen/arch/arm/vpl011.c | 5 +---- xen/drivers/char/console.c | 11 +++++++++-- xen/include/xen/console.h | 3 +-- 3 files changed, 11 insertions(+), 8 deletions(-)
xen/arch/arm/vpl011.c +1 -4
@@ -78,12 +78,11 @@ static void vpl011_write_data_xen(struct domain *d, uint8_t data)unsigned long flags;struct vpl011 *vpl011 = &d->arch.vpl011;struct vpl011_xen_backend *intf = vpl011->backend.xen; -struct domain *input =console_get_domain ();VPL011_LOCK (d, flags); intf->out[intf->out_prod++] = data; -if ( d == input ) +if ( d->domain_id ==console_get_domid () ) {if ( intf->out_prod ==1 ) {
@@ -123,8 +122,6 @@ static void vpl011_write_data_xen(struct domain *d, uint8_t data)vpl011_update_interrupt_status (d);VPL011_UNLOCK (d, flags); - -console_put_domain (input); }/*
xen/drivers/char/console.c +9 -2
@@ -474,19 +474,26 @@ static unsigned int __read_mostly console_rx = 0;#define max_console_rx (max_init_domid + 1) -struct domain *console_get_domain (void ) +static struct domain *console_get_domain (void ) {if ( console_rx ==0 )return NULL ;return rcu_lock_domain_by_id (console_rx -1 ); } -void console_put_domain (struct domain *d) +static void console_put_domain (struct domain *d) {if ( d )rcu_unlock_domain (d); } +domid_t console_get_domid (void ) +{ +if ( console_rx ==0 ) +return DOMID_XEN ; +return console_rx -1 ; +} +static void console_switch_input (void ) {unsigned int next_rx = console_rx;
xen/include/xen/console.h +1 -2
@@ -32,8 +32,7 @@ void console_end_sync(void);void console_start_log_everything (void );void console_end_log_everything (void ); -struct domain *console_get_domain (void ); -void console_put_domain (struct domain *d); +domid_t console_get_domid (void );/* * Steal output from the console. Returns +ve identifier, else -ve error.
Signature · 1 line
2.34.1
Re: [PATCH v3 2/5] xen/console: introduce console_get_domid()
xen/arch/arm/vpl011.c static void vpl011_write_data_xen(struct domain *d, uint8_t data) · line 85
Quoted 13 lines · On 19.05.2025 22:12, [email protected] wrote:
xen/arch/arm/vpl011.c +1
-2@@ -78,12 +78,11 @@ static void vpl011_write_data_xen(struct domain *d, uint8_t data)unsigned long flags;struct vpl011 *vpl011 = &d->arch.vpl011;struct vpl011_xen_backend *intf = vpl011->backend.xen; -struct domain *input =console_get_domain ();VPL011_LOCK (d, flags); intf->out[intf->out_prod++] = data; -if ( d == input ) +if ( d->domain_id ==console_get_domid () )
How do you know d isn't a domain different from the one that was the console "owner" prior to being destroyed? Original code guaranteed this up to ...
xen/arch/arm/vpl011.c static void vpl011_write_data_xen(struct domain *d, uint8_t data) · line 127
Quoted 6 lines
xen/arch/arm/vpl011.c +0
-2@@ -123,8 +122,6 @@ static void vpl011_write_data_xen(struct domain *d, uint8_t data)vpl011_update_interrupt_status (d);VPL011_UNLOCK (d, flags); - -console_put_domain (input);
... here.
Jan
Re: [PATCH v3 2/5] xen/console: introduce console_get_domid()
Quoted 19 lines · Jan Beulich
xen/arch/arm/vpl011.c static void vpl011_write_data_xen(struct domain *d, uint8_t data) · line 85
Quoted 13 lines · On 19.05.2025 22:12, [email protected] wrote:
xen/arch/arm/vpl011.c +1
-2@@ -78,12 +78,11 @@ static void vpl011_write_data_xen(struct domain *d, uint8_t data)unsigned long flags;struct vpl011 *vpl011 = &d->arch.vpl011;struct vpl011_xen_backend *intf = vpl011->backend.xen; -struct domain *input =console_get_domain ();VPL011_LOCK (d, flags); intf->out[intf->out_prod++] = data; -if ( d == input ) +if ( d->domain_id ==console_get_domid () )How do you know d isn't a domain different from the one that was the console "owner" prior to being destroyed? Original code guaranteed this up to ...
Quoted 8 lines
xen/arch/arm/vpl011.c static void vpl011_write_data_xen(struct domain *d, uint8_t data) · line 127
Quoted 6 lines
xen/arch/arm/vpl011.c +0
-2@@ -123,8 +122,6 @@ static void vpl011_write_data_xen(struct domain *d, uint8_t data)vpl011_update_interrupt_status (d);VPL011_UNLOCK (d, flags); - -console_put_domain (input);... here.
Agreed; I will drop the code change in the next iteration. Thanks.
Quoted 2 lines
Jan
PATCH v3 3/5 xen/console: introduce console input permission
In reply to [email protected]From: Denis Mukhin <[email protected]>
Add new flag to domain structure for marking permission to intercept the physical console input by the domain.
Update console input switch logic accordingly.
No functional change intended.
---
Changes since v2:
- rebased
- Link to v2: https://lore.kernel.org/xen-devel/[email protected]/
--- xen/arch/arm/vpl011.c | 2 ++ xen/arch/x86/pv/shim.c | 2 ++ xen/common/domain.c | 2 ++ xen/drivers/char/console.c | 18 +++++++++++++++++- xen/include/xen/sched.h | 8 +++++++- 5 files changed, 30 insertions(+), 2 deletions(-)
xen/arch/arm/vpl011.c +2 -0
@@ -734,6 +734,8 @@ int domain_vpl011_init(struct domain *d, struct vpl011_init_info *info)register_mmio_handler (d, &vpl011_mmio_handler, vpl011->base_addr,GUEST_PL011_SIZE ,NULL ); + d->console.input_allowed = true; +return 0 ; out1:
xen/arch/x86/pv/shim.c +2 -0
@@ -238,6 +238,8 @@ void __init pv_shim_setup_dom(struct domain *d, l4_pgentry_t *l4start,* guest from depleting the shim memory pool. */ d->max_pages =domain_tot_pages (d); + + d->console.input_allowed = true; }static void write_start_info (struct domain *d)
xen/common/domain.c +2 -0
@@ -836,6 +836,8 @@ struct domain *domain_create(domid_t domid, flags |= CDF_hardware;if ( old_hwdom ) old_hwdom->cdf &= ~CDF_hardware; + + d->console.input_allowed = true; }/* Holding CDF_* internal flags. */
xen/drivers/char/console.c +17 -1
@@ -476,9 +476,21 @@ static unsigned int __read_mostly console_rx = 0;static struct domain *console_get_domain (void ) { +struct domain *d; +if ( console_rx ==0 )return NULL ; -return rcu_lock_domain_by_id (console_rx -1 ); + + d =rcu_lock_domain_by_id (console_rx -1 ); +if ( !d ) +return NULL ; + +if ( d->console.input_allowed ) +return d; + +rcu_unlock_domain (d); + +return NULL ; }static void console_put_domain (struct domain *d)
@@ -522,6 +534,10 @@ static void console_switch_input(void)if ( d ) {rcu_unlock_domain (d); + +if ( !d->console.input_allowed ) +break ; + console_rx = next_rx;printk ("*** Serial input to DOM%u" , domid);break ;
xen/include/xen/sched.h +7 -1
@@ -512,7 +512,7 @@ struct domainbool auto_node_affinity;/* Is this guest fully privileged (aka dom0)? */ bool is_privileged; -/* Can this guest access the Xen console? */ +/* XSM: permission to use HYPERCALL_console_io hypercall */ bool is_console;/* Is this guest being debugged by dom0? */ bool debugger_attached;
@@ -651,6 +651,12 @@ struct domainunsigned int num_llc_colors;const unsigned int *llc_colors;#endif + +/* Console settings. */ +struct { +/* Permission to take ownership of the physical console input. */ +bool input_allowed; + } console; }__aligned(PAGE_SIZE) ; static inline struct page_list_head *page_to_list(
Signature · 1 line
2.34.1
PATCH v3 4/5 xen/console: remove max_init_domid dependency
In reply to [email protected]From: Denis Mukhin <[email protected]>
The physical console input rotation depends on max_init_domid symbol, which is managed differently across architectures.
Instead of trying to manage max_init_domid in the arch-common code the console input rotation code can be reworked by removing dependency on max_init_domid entirely.
To do that, introduce domid_find_with_input_allowed() in arch-independent location to find the ID of the next possible console owner domain. The IDs are rotated across non-system domain IDs and DOMID_XEN.
Also, introduce helper console_set_domid() for updating identifier of the current console input owner (points to Xen or domain).
Use domid_find_with_input_allowed() and console_set_domid() in console_switch_input().
Remove uses of max_init_domid in the code.
---
Changes since v2:
- reworked to traversing domain list directly
- Link to v2: https://lore.kernel.org/xen-devel/[email protected]/
--- xen/arch/arm/include/asm/setup.h | 2 - xen/arch/arm/setup.c | 2 - xen/arch/ppc/include/asm/setup.h | 2 - xen/arch/riscv/include/asm/setup.h | 2 - xen/arch/x86/include/asm/setup.h | 2 - xen/common/device-tree/dom0less-build.c | 2 - xen/common/domain.c | 29 +++++++ xen/drivers/char/console.c | 102 +++++++++--------------- xen/include/xen/domain.h | 1 + 9 files changed, 66 insertions(+), 78 deletions(-)
xen/arch/arm/include/asm/setup.h +0 -2
@@ -25,8 +25,6 @@ struct map_range_datastruct rangeset *irq_ranges; }; -extern domid_t max_init_domid; -void copy_from_paddr (void *dst,paddr_t paddr,unsigned long len);size_t estimate_efi_size (unsigned int mem_nr_banks);
xen/arch/arm/setup.c +0 -2
@@ -61,8 +61,6 @@ struct cpuinfo_arm __read_mostly system_cpuinfo;bool __read_mostly acpi_disabled;#endif -domid_t __read_mostly max_init_domid; -static __used void init_done (void ) {int rc;
xen/arch/ppc/include/asm/setup.h +0 -2
@@ -1,6 +1,4 @@#ifndef __ASM_PPC_SETUP_H__#define __ASM_PPC_SETUP_H__ -#define max_init_domid (0) -#endif /* __ASM_PPC_SETUP_H__ */
xen/arch/riscv/include/asm/setup.h +0 -2
@@ -5,8 +5,6 @@#include <xen/types.h> -#define max_init_domid (0) -void setup_mm (void );void copy_from_paddr (void *dst,paddr_t paddr,unsigned long len);
xen/arch/x86/include/asm/setup.h +0 -2
@@ -69,6 +69,4 @@ extern bool opt_dom0_verbose;extern bool opt_dom0_cpuid_faulting;extern bool opt_dom0_msr_relaxed; -#define max_init_domid (0) -#endif
xen/common/device-tree/dom0less-build.c +0 -2
@@ -977,8 +977,6 @@ void __init create_domUs(void) domid =domid_alloc (DOMID_INVALID );if ( domid ==DOMID_INVALID )panic ("Error allocating ID for domain %s\n" ,dt_node_name (node)); -if ( max_init_domid < domid ) - max_init_domid = domid; d =domain_create (domid, &d_cfg, flags);if (IS_ERR (d) )
xen/common/domain.c +29 -0
@@ -2460,6 +2460,35 @@ void domid_free(domid_t domid)spin_unlock (&domid_lock); } +/* +* Find the ID of the next possible console owner domain. +* +* @return Domain ID: DOMID_XEN or non-system domain IDs within +* the range of [0..CONFIG_MAX_DOMID-1]. +*/ +domid_t domid_find_with_input_allowed (domid_t hint) +{ +struct domain *d; +domid_t domid =DOMID_XEN ; + +spin_lock (&domlist_update_lock); + +for ( d =domid_to_domain (hint); + d &&likely (get_domain (d)) && (d->domain_id <CONFIG_MAX_DOMID ); + d = d->next_in_list ) + { +if ( d->console.input_allowed ) + { + domid = d->domain_id; +break ; + } + } + +spin_unlock (&domlist_update_lock); + +return domid; +} +/* * Local variables: * mode: C
xen/drivers/char/console.c +36 -66
146 lines not on this page · raw message
xen/include/xen/domain.h +1 -0
@@ -36,6 +36,7 @@ void getdomaininfo(struct domain *d, struct xen_domctl_getdomaininfo *info);void arch_get_domain_info (const struct domain *d,struct xen_domctl_getdomaininfo *info); +domid_t domid_find_with_input_allowed (domid_t hint);domid_t get_initial_domain_id (void );domid_t domid_alloc (domid_t domid);
Signature · 1 line
2.34.1
Re: [PATCH v3 4/5] xen/console: remove max_init_domid dependency
Quoted 8 lines · On 19.05.2025 22:12, [email protected] wrote:
From: Denis Mukhin <[email protected]>
The physical console input rotation depends on
max_init_domidsymbol, which is managed differently across architectures.Instead of trying to manage
max_init_domidin the arch-common code the console input rotation code can be reworked by removing dependency onmax_init_domidentirely.
... at the expense of doing (worst case) 32k iterations just to find nothing (else). Iirc it was to avoid this why max_init_domid was introduced.
Quoted 5 lines
xen/common/domain.c +0
-0@@ -2460,6 +2460,35 @@ void domid_free(domid_t domid)spin_unlock (&domid_lock); }
xen/common/domain.c void domid_free(domid_t domid) · line 2471
Quoted 10 lines
xen/common/domain.c +9
-0+/* +* Find the ID of the next possible console owner domain. +* +* @return Domain ID: DOMID_XEN or non-system domain IDs within +* the range of [0..CONFIG_MAX_DOMID-1]. +*/ +domid_t domid_find_with_input_allowed (domid_t hint) +{ +struct domain *d;
const?
xen/common/domain.c void domid_free(domid_t domid) · line 2474
Quoted 3 lines
xen/common/domain.c +3
-0+domid_t domid =DOMID_XEN ; + +spin_lock (&domlist_update_lock);
Why this heavy lock? Other functions iterating the list just use the RCU read lock.
Jan
PATCH v3 5/5 xen/console: rename console_rx to console_domid
In reply to [email protected]From: Denis Mukhin <[email protected]>
Update the symbol name to match the code better.
No functional change.
---
Changes since v2:
- new patch
--- xen/drivers/char/console.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
xen/drivers/char/console.c +7 -7
@@ -467,11 +467,11 @@ static void cf_check dump_console_ring_key(unsigned char key)#define switch_code (opt_conswitch[0]-'a'+1)/* Console owner domain identifier. */ -static domid_t __read_mostly console_rx =DOMID_XEN ; +static domid_t __read_mostly console_domid =DOMID_XEN ;static struct domain *console_get_domain (void ) { -struct domain *d =rcu_lock_domain_by_id (console_rx); +struct domain *d =rcu_lock_domain_by_id (console_domid);if ( !d )return NULL ;
@@ -497,7 +497,7 @@ static void console_set_domid(domid_t domid)else printk ("*** Serial input to DOM%u" , domid); - console_rx = domid; + console_domid = domid;if ( switch_code ) printk(" (type 'CTRL-%c' three times to switch input)",
@@ -507,7 +507,7 @@ static void console_set_domid(domid_t domid)domid_t console_get_domid (void ) { -return console_rx; +return console_domid; }/*
@@ -518,10 +518,10 @@ static void console_switch_input(void) {domid_t hint; -if ( console_rx ==DOMID_XEN ) +if ( console_domid ==DOMID_XEN ) hint =get_initial_domain_id ();else - hint = console_rx +1 ; + hint = console_domid +1 ; hint =domid_find_with_input_allowed (hint);
@@ -533,7 +533,7 @@ static void __serial_rx(char c)struct domain *d;int rc =0 ; -if ( console_rx ==DOMID_XEN ) +if ( console_domid ==DOMID_XEN )return handle_keypress (c, false); d =console_get_domain ();
Signature · 1 line
2.34.1