xen/console: introduce domain_console struct
PATCH v9 1/1 xen/console: introduce domain_console struct
From: Denis Mukhin <[email protected]>
Introduce domain_console for grouping data structures used for integrating domain's diagnostic console with Xen's console driver.
Group all pbuf-related data structures under domain_console. Rename the moved fields to plain .buf, .idx and .lock names, since all uses of the fields are touched.
Bump the domain console buffer allocation size to 256. No extra symbol for the value since it is used only once during data structure declaration. All size checks use ARRAY_SIZE().
Allocate domain_console from the heap so that the parent domain struct size stays below PAGE_SIZE boundary to account for more console-related fields added in the future. Ensure accesses to domain_console pointer are valid in console_switch_input() and hvm_print_line().
Finally, update the domain_console allocation and initialization code.
No functional change.
---
Changes since v8:
- ensure accesses to domain_console pointer are valid in console_switch_input() and hvm_print_line()
- updated comment for domain_console field
Link to v8: https://lore.kernel.org/xen-devel/[email protected]/
--- xen/arch/arm/vpl011.c | 2 +- xen/arch/x86/hvm/hvm.c | 22 ++++++++++++++-------- xen/arch/x86/pv/shim.c | 2 +- xen/common/domain.c | 19 +++++++++---------- xen/drivers/char/console.c | 28 ++++++++++++++++------------ xen/include/xen/sched.h | 24 +++++++++++++----------- 6 files changed, 54 insertions(+), 43 deletions(-)
xen/arch/arm/vpl011.c +1 -1
@@ -713,7 +713,7 @@ int domain_vpl011_init(struct domain *d, struct vpl011_init_info *info) }else { - d->console.input_allowed = true; + d->console->input_allowed = true; vpl011->backend_in_domain = false; vpl011->backend.xen = xzalloc(struct vpl011_xen_backend);
xen/arch/x86/hvm/hvm.c +14 -8
@@ -559,7 +559,8 @@ void hvm_do_resume(struct vcpu *v) static int cf_check hvm_print_line(int dir,unsigned int port,unsigned int bytes,uint32_t *val) { -struct domain *cd = current->domain; +struct domain *d = current->domain; +struct domain_console *cons = d->console;char c = *val;ASSERT (bytes ==1 && port ==XEN_HVM_DEBUGCONS_IOPORT );
@@ -571,16 +572,21 @@ static int cf_check hvm_print_line(if ( !is_console_printable (c) )return X86EMUL_OKAY ; -spin_lock (&cd->pbuf_lock); +rcu_lock_domain (d); + +spin_lock (&cons->lock); +ASSERT (cons->idx <ARRAY_SIZE (cons->buf));if ( c !='\n' ) - cd->pbuf[cd->pbuf_idx++] = c; -if ( (cd->pbuf_idx == (DOMAIN_PBUF_SIZE -1 )) || (c =='\n' ) ) + cons->buf[cons->idx++] = c; +if ( (cons->idx == (ARRAY_SIZE (cons->buf) -1 )) || (c =='\n' ) ) { - cd->pbuf[cd->pbuf_idx] ='\0' ; -guest_printk (cd,XENLOG_G_DEBUG "%s\n" , cd->pbuf); - cd->pbuf_idx =0 ; + cons->buf[cons->idx] ='\0' ; +guest_printk (d,XENLOG_G_DEBUG "%s\n" , cons->buf); + cons->idx =0 ; } -spin_unlock (&cd->pbuf_lock); +spin_unlock (&cons->lock); + +rcu_unlock_domain (d);return X86EMUL_OKAY ; }
xen/arch/x86/pv/shim.c +1 -1
@@ -239,7 +239,7 @@ void __init pv_shim_setup_dom(struct domain *d, l4_pgentry_t *l4start,*/ d->max_pages =domain_tot_pages (d); - d->console.input_allowed = true; + d->console->input_allowed = true; }static void write_start_info (struct domain *d)
xen/common/domain.c +9 -10
@@ -669,7 +669,7 @@ static void _domain_destroy(struct domain *d)BUG_ON (!d->is_dying);BUG_ON (atomic_read (&d->refcnt) !=DOMAIN_DESTROYED ); -xfree (d->pbuf); +xvfree (d->console);argo_destroy (d);
@@ -835,8 +835,6 @@ 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. */
@@ -866,8 +864,6 @@ struct domain *domain_create(domid_t domid,spin_lock_init (&d->shutdown_lock); d->shutdown_code =SHUTDOWN_CODE_INVALID ; -spin_lock_init (&d->pbuf_lock); -rwlock_init (&d->vnuma_rwlock);#ifdef CONFIG_HAS_PCI
@@ -877,6 +873,14 @@ struct domain *domain_create(domid_t domid,/* All error paths can depend on the above setup. */ + err = -ENOMEM ; + d->console =xvzalloc (typeof (*d->console)); +if ( !d->console ) + goto fail; + +spin_lock_init (&d->console->lock); + d->console->input_allowed =is_hardware_domain (d); +/* * Allocate d->vcpu[] and set ->max_vcpus up early. Various per-domain * resources want to be sized based on max_vcpus.
@@ -959,11 +963,6 @@ struct domain *domain_create(domid_t domid,if ( (err =argo_init (d)) !=0 ) goto fail; - err = -ENOMEM ; - d->pbuf =xzalloc_array (char,DOMAIN_PBUF_SIZE ); -if ( !d->pbuf ) - goto fail; -if ( (err =sched_init_domain (d, config->cpupool_id)) !=0 ) goto fail;
xen/drivers/char/console.c +16 -12
@@ -523,7 +523,7 @@ struct domain *console_get_domain(void)if ( !d )return NULL ; -if ( d->console.input_allowed ) +if ( d->console->input_allowed )return d;rcu_unlock_domain (d);
@@ -564,10 +564,13 @@ static void console_switch_input(void) d =rcu_lock_domain_by_id (domid);if ( d ) { -rcu_unlock_domain (d); - -if ( !d->console.input_allowed ) +if ( !d->console->input_allowed ) + { +rcu_unlock_domain (d);continue ; + } + +rcu_unlock_domain (d); console_rx = next_rx;printk ("*** Serial input to DOM%u" , domid);
@@ -746,6 +749,7 @@ static long guest_console_write(XEN_GUEST_HANDLE_PARAM(char) buffer,else {char *kin = kbuf, *kout = kbuf, c; +struct domain_console *cons = cd->console;/* Strip non-printable characters */ do
@@ -758,22 +762,22 @@ static long guest_console_write(XEN_GUEST_HANDLE_PARAM(char) buffer, }while ( --kcount >0 ); *kout ='\0' ; -spin_lock (&cd->pbuf_lock); +spin_lock (&cons->lock); kcount = kin - kbuf;if ( c !='\n' && - (cd->pbuf_idx + (kout - kbuf) < (DOMAIN_PBUF_SIZE -1 )) ) + (cons->idx + (kout - kbuf) < (ARRAY_SIZE (cons->buf) -1 )) ) {/* buffer the output until a newline */ -memcpy (cd->pbuf + cd->pbuf_idx, kbuf, kout - kbuf); - cd->pbuf_idx += (kout - kbuf); +memcpy (cons->buf + cons->idx, kbuf, kout - kbuf); + cons->idx += kout - kbuf; }else { - cd->pbuf[cd->pbuf_idx] ='\0' ; -guest_printk (cd,XENLOG_G_DEBUG "%s%s\n" , cd->pbuf, kbuf); - cd->pbuf_idx =0 ; + cons->buf[cons->idx] ='\0' ; +guest_printk (cd,XENLOG_G_DEBUG "%s%s\n" , cons->buf, kbuf); + cons->idx =0 ; } -spin_unlock (&cd->pbuf_lock); +spin_unlock (&cons->lock); }guest_handle_add_offset (buffer, kcount);
xen/include/xen/sched.h +13 -11
@@ -371,6 +371,17 @@ struct evtchn_port_ops;#define MAX_NR_IOREQ_SERVERS 8 +/* Domain console settings. */ +struct domain_console { +/* Permission to take ownership of the physical console input. */ +bool input_allowed; + +/* hvm_print_line() and guest_console_write() logging. */ +unsigned int idx; +spinlock_t lock; +char buf[256 ]; +}; +struct domain {domid_t domain_id;
@@ -562,12 +573,6 @@ struct domain/* Control-plane tools handle for this domain. */ xen_domain_handle_t handle; -/* hvm_print_line() and guest_console_write() logging. */ -#define DOMAIN_PBUF_SIZE 200 -char *pbuf; -unsigned int pbuf_idx; -spinlock_t pbuf_lock; -/* OProfile support. */ struct xenoprof *xenoprof;
@@ -652,11 +657,8 @@ struct domainconst unsigned int *llc_colors;#endif -/* Console settings. */ -struct { -/* Permission to take ownership of the physical console input. */ -bool input_allowed; - } console; +/* Pointer to console settings; NULL for system Xen domains. */ +struct domain_console *console; }__aligned(PAGE_SIZE) ; static inline struct page_list_head *page_to_list(
Signature · 1 line
2.34.1
xen/arch/x86/hvm/hvm.c void hvm_do_resume(struct vcpu *v) · line 562
Quoted 6 lines · On 23.07.2025 02:11, [email protected] wrote:
xen/arch/x86/hvm/hvm.c +1
-1@@ -559,7 +559,8 @@ void hvm_do_resume(struct vcpu *v) static int cf_check hvm_print_line(int dir,unsigned int port,unsigned int bytes,uint32_t *val) { -struct domain *cd = current->domain; +struct domain *d = current->domain;
Why the renaming? (Iff any renaming was needed here, then please to currd.)
xen/arch/x86/hvm/hvm.c static int cf_check hvm_print_line( · line 575
Quoted 10 lines
xen/arch/x86/hvm/hvm.c +2
-1+struct domain_console *cons = d->console;char c = *val;ASSERT (bytes ==1 && port ==XEN_HVM_DEBUGCONS_IOPORT );@@ -571,16 +572,21 @@ static int cf_check hvm_print_line(if ( !is_console_printable (c) )return X86EMUL_OKAY ; -spin_lock (&cd->pbuf_lock); +rcu_lock_domain (d);
Given this is current->domain, it can't go away, and hence I don't think this is needed here.
Jan
Quoted 9 lines · Jan Beulich
xen/arch/x86/hvm/hvm.c void hvm_do_resume(struct vcpu *v) · line 562
Quoted 6 lines · On 23.07.2025 02:11, [email protected] wrote:
xen/arch/x86/hvm/hvm.c +1
-1@@ -559,7 +559,8 @@ void hvm_do_resume(struct vcpu *v) static int cf_check hvm_print_line(int dir,unsigned int port,unsigned int bytes,uint32_t *val) { -struct domain *cd = current->domain; +struct domain *d = current->domain;Why the renaming? (Iff any renaming was needed here, then please to currd.)
It was requested earlier:
https://lore.kernel.org/all/[email protected]/
Quoted 9 lines
Quoted 9 lines
xen/arch/x86/hvm/hvm.c +1
-0+struct domain_console *cons = d->console;char c = *val;ASSERT (bytes ==1 && port ==XEN_HVM_DEBUGCONS_IOPORT );@@ -571,16 +572,21 @@ static int cf_check hvm_print_line(if ( !is_console_printable (c) )return X86EMUL_OKAY ;
Quoted 5 lines
xen/arch/x86/hvm/hvm.c static int cf_check hvm_print_line( · line 575
Quoted 2 lines
xen/arch/x86/hvm/hvm.c +1
-1-spin_lock (&cd->pbuf_lock); +rcu_lock_domain (d);Given this is
current->domain, it can't go away, and hence I don't think this is needed here.
Ack
Quoted 3 lines
Jan
Quoted 11 lines · On Wed, Jul 23, 2025 at 06:24:07PM +0000, [email protected] wrote:
Quoted 9 lines · Jan Beulich
xen/arch/x86/hvm/hvm.c void hvm_do_resume(struct vcpu *v) · line 562
Quoted 6 lines · On 23.07.2025 02:11, [email protected] wrote:
xen/arch/x86/hvm/hvm.c +1
-1@@ -559,7 +559,8 @@ void hvm_do_resume(struct vcpu *v) static int cf_check hvm_print_line(int dir,unsigned int port,unsigned int bytes,uint32_t *val) { -struct domain *cd = current->domain; +struct domain *d = current->domain;Why the renaming? (Iff any renaming was needed here, then please to currd.)
Quoted 3 lines
It was requested earlier:
https://lore.kernel.org/all/[email protected]/
What would be the preferred way to address this?
Add const to domain pointer?s/cd/currd/?s/cd/d/ like in previous review?
Do not touch cd?
It make sense to adjust the code now by at least adding const.
Quoted 20 lines
Quoted 9 lines
Quoted 9 lines
xen/arch/x86/hvm/hvm.c +1
-0+struct domain_console *cons = d->console;char c = *val;ASSERT (bytes ==1 && port ==XEN_HVM_DEBUGCONS_IOPORT );@@ -571,16 +572,21 @@ static int cf_check hvm_print_line(if ( !is_console_printable (c) )return X86EMUL_OKAY ;Quoted 5 lines
xen/arch/x86/hvm/hvm.c static int cf_check hvm_print_line( · line 575
Quoted 2 lines
xen/arch/x86/hvm/hvm.c +1
-1-spin_lock (&cd->pbuf_lock); +rcu_lock_domain (d);Given this is
current->domain, it can't go away, and hence I don't think this is needed here.Ack
Quoted 3 lines
Jan
Quoted 16 lines · On 24.07.2025 04:17, [email protected] wrote:
Quoted 11 lines · On Wed, Jul 23, 2025 at 06:24:07PM +0000, [email protected] wrote:
Quoted 9 lines · Jan Beulich
xen/arch/x86/hvm/hvm.c void hvm_do_resume(struct vcpu *v) · line 562
Quoted 6 lines · On 23.07.2025 02:11, [email protected] wrote:
xen/arch/x86/hvm/hvm.c +1
-1@@ -559,7 +559,8 @@ void hvm_do_resume(struct vcpu *v) static int cf_check hvm_print_line(int dir,unsigned int port,unsigned int bytes,uint32_t *val) { -struct domain *cd = current->domain; +struct domain *d = current->domain;Why the renaming? (Iff any renaming was needed here, then please to currd.)
Quoted 3 lines
It was requested earlier:
https://lore.kernel.org/all/[email protected]/
Quoted 8 lines
What would be the preferred way to address this?
Add
constto domain pointer?s/cd/currd/?s/cd/d/like in previous review?
Do not touchcd?It make sense to adjust the code now by at least adding
const.
Yet only if the line needs touching anyway for the purpose of the patch. Which, as it looks, isn't necessary.
Jan
Quoted 11 lines · On 23.07.2025 20:24, [email protected] wrote:
Quoted 9 lines · Jan Beulich
xen/arch/x86/hvm/hvm.c void hvm_do_resume(struct vcpu *v) · line 562
Quoted 6 lines · On 23.07.2025 02:11, [email protected] wrote:
xen/arch/x86/hvm/hvm.c +1
-1@@ -559,7 +559,8 @@ void hvm_do_resume(struct vcpu *v) static int cf_check hvm_print_line(int dir,unsigned int port,unsigned int bytes,uint32_t *val) { -struct domain *cd = current->domain; +struct domain *d = current->domain;Why the renaming? (Iff any renaming was needed here, then please to currd.)
Quoted 2 lines
It was requested earlier:
https://lore.kernel.org/all/[email protected]/
But there the decl was moved, which isn't the case anymore. And yes, I should have asked for it becoming "currd" there, not "d".
Jan