xen/console: introduce domain_console struct
PATCH v2 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 size to the closest power of 2 (256) and rename the symbol to DOMAIN_CONSOLE_BUF_SIZE.
Move d->console->buf management under CONFIG_VERBOSE_DEBUG when the HYPERCALL_console_io handler is enabled.
Finally, update the domain_console allocation code.
No functional change.
---
Changes since v1:
- added new struct domain_console
- updated allocation code for domain's console data structure
- bumped the size of the domain's output buffer to 256
- put domain's console buffer treatment under CONFIG_VERBOSE_DEBUG
I addressed most of the v1 feedback here, since all fields of the domain's console data structures are touched.
Domain's console input buffer question is not addressed because it looks like a separate (big) patch.
Link to v1: https://lore.kernel.org/xen-devel/[email protected]/
Link to CI: https://gitlab.com/xen-project/people/dmukhin/xen/-/pipelines/1870179848
--- xen/arch/arm/vpl011.c | 2 +- xen/arch/x86/hvm/hvm.c | 26 ++++++++++++++++---------- xen/arch/x86/pv/shim.c | 2 +- xen/common/domain.c | 18 ++++++++++-------- xen/drivers/char/console.c | 24 ++++++++++++++---------- xen/include/xen/sched.h | 27 +++++++++++++++++---------- 6 files changed, 59 insertions(+), 40 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 +16 -10
@@ -559,7 +559,6 @@ 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;char c = *val;ASSERT (bytes ==1 && port ==XEN_HVM_DEBUGCONS_IOPORT );
@@ -570,17 +569,24 @@ static int cf_check hvm_print_line(if ( !is_console_printable (c) )return X86EMUL_OKAY ; - -spin_lock (&cd->pbuf_lock); -if ( c !='\n' ) - cd->pbuf[cd->pbuf_idx++] = c; -if ( (cd->pbuf_idx == (DOMAIN_PBUF_SIZE -1 )) || (c =='\n' ) ) +#ifdef CONFIG_VERBOSE_DEBUG + else { - cd->pbuf[cd->pbuf_idx] ='\0' ; -guest_printk (cd,XENLOG_G_DEBUG "%s\n" , cd->pbuf); - cd->pbuf_idx =0 ; +struct domain *cd = current->domain; +struct domain_console *cons = cd->console; + +spin_lock (&cons->lock); +if ( c !='\n' ) + cons->buf[cons->idx++] = c; +if ( (cons->idx == (DOMAIN_CONSOLE_BUF_SIZE -1 )) || (c =='\n' ) ) + { + cons->buf[cons->idx] ='\0' ; +guest_printk (cd,XENLOG_G_DEBUG "%s\n" , cons->buf); + cons->idx =0 ; + } +spin_unlock (&cons->lock); } -spin_unlock (&cd->pbuf_lock); +#endif 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 +10 -8
@@ -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); +xfree (d->console);argo_destroy (d);
@@ -800,6 +800,11 @@ struct domain *domain_create(domid_t domid,if ( (d =alloc_domain_struct ()) ==NULL )return ERR_PTR (-ENOMEM ); + err = -ENOMEM ; + d->console =xvzalloc (typeof (*d->console)); +if ( !d->console ) + goto fail; +/* Sort out our idea of is_system_domain(). */ d->domain_id = domid; d->unique_id =get_unique_id ();
@@ -832,7 +837,7 @@ struct domain *domain_create(domid_t domid,if ( old_hwdom ) old_hwdom->cdf &= ~CDF_hardware; - d->console.input_allowed = true; + d->console->input_allowed = true; }/* Holding CDF_* internal flags. */
@@ -862,7 +867,9 @@ 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); +#ifdef CONFIG_VERBOSE_DEBUG +spin_lock_init (&d->console->lock); +#endif rwlock_init (&d->vnuma_rwlock);
@@ -955,11 +962,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 +14 -10
@@ -524,7 +524,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);
@@ -567,7 +567,7 @@ static void console_switch_input(void) {rcu_unlock_domain (d); -if ( !d->console.input_allowed ) +if ( !d->console->input_allowed )continue ; console_rx = next_rx;
@@ -754,9 +754,11 @@ static long guest_console_write(XEN_GUEST_HANDLE_PARAM(char) buffer,console_send (kbuf, kcount, flags);nrspin_unlock_irq (&console_lock); } +#ifdef CONFIG_VERBOSE_DEBUG else {char *kin = kbuf, *kout = kbuf, c; +struct domain_console *cons = cd->console;/* Strip non-printable characters */ do
@@ -769,23 +771,25 @@ 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 <DOMAIN_CONSOLE_BUF_SIZE -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); } +#endif guest_handle_add_offset (buffer, kcount); count -= kcount;
xen/include/xen/sched.h +17 -10
@@ -371,6 +371,22 @@ struct evtchn_port_ops;#define MAX_NR_IOREQ_SERVERS 8 +/* Arbitrary value; must be a multiple of the cacheline size. */ +#define DOMAIN_CONSOLE_BUF_SIZE 256 + +/* Domain console settings. */ +struct domain_console { +#ifdef CONFIG_VERBOSE_DEBUG +/* hvm_print_line() and guest_console_write() logging. */ +char *buf; +unsigned int idx; +spinlock_t lock; +#endif /* CONFIG_VERBOSE_DEBUG */ + +/* Permission to take ownership of the physical console input. */ +bool input_allowed; +}; +struct domain {domid_t domain_id;
@@ -562,12 +578,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;
@@ -653,10 +663,7 @@ struct domain#endif /* Console settings. */ -struct { -/* Permission to take ownership of the physical console input. */ -bool input_allowed; - } console; +struct domain_console *console; }__aligned(PAGE_SIZE) ; static inline struct page_list_head *page_to_list(
Signature · 1 line
2.34.1
Quoted 9 lines · On 17.06.2025 03:27, [email protected] wrote:
From: Denis Mukhin <[email protected]>
Introduce
domain_consolefor 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.
Quoted 5 lines
Bump the domain console buffer size to the closest power of 2 (256) and rename the symbol to
DOMAIN_CONSOLE_BUF_SIZE.Move
d->console->bufmanagement underCONFIG_VERBOSE_DEBUGwhen theHYPERCALL_console_iohandler is enabled.
This, if at all, needs to be a separate change (with its own justification). I for one don't think VERBOSE_DEBUG is intended to control any kind of guest output.
Quoted 11 lines
xen/arch/x86/hvm/hvm.c +0
-1@@ -559,7 +559,6 @@ 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;char c = *val;ASSERT (bytes ==1 && port ==XEN_HVM_DEBUGCONS_IOPORT );
xen/arch/x86/hvm/hvm.c static int cf_check hvm_print_line( · line 571
Quoted 3 lines
xen/arch/x86/hvm/hvm.c +0
-0@@ -570,17 +569,24 @@ static int cf_check hvm_print_line(if ( !is_console_printable (c) )return X86EMUL_OKAY ;
After this "return" ...
xen/arch/x86/hvm/hvm.c static int cf_check hvm_print_line( · line 573
Quoted 7 lines
xen/arch/x86/hvm/hvm.c +2
-5- -spin_lock (&cd->pbuf_lock); -if ( c !='\n' ) - cd->pbuf[cd->pbuf_idx++] = c; -if ( (cd->pbuf_idx == (DOMAIN_PBUF_SIZE -1 )) || (c =='\n' ) ) +#ifdef CONFIG_VERBOSE_DEBUG + else
... there's no point to have "else" here.
xen/arch/x86/hvm/hvm.c static int cf_check hvm_print_line( · line 575
Quoted 5 lines
xen/arch/x86/hvm/hvm.c +1
-3{ - cd->pbuf[cd->pbuf_idx] ='\0' ; -guest_printk (cd,XENLOG_G_DEBUG "%s\n" , cd->pbuf); - cd->pbuf_idx =0 ; +struct domain *cd = current->domain;
We normally name such a variable d. It also looks as if this could be pointer- to-const.
Quoted 5 lines
xen/arch/x86/hvm/hvm.c +5
-0+struct domain_console *cons = cd->console; + +spin_lock (&cons->lock); +if ( c !='\n' ) + cons->buf[cons->idx++] = c;
xen/arch/x86/hvm/hvm.c static int cf_check hvm_print_line( · line 589
Quoted 10 lines
xen/arch/x86/hvm/hvm.c +8
-1+if ( (cons->idx == (DOMAIN_CONSOLE_BUF_SIZE -1 )) || (c =='\n' ) ) + { + cons->buf[cons->idx] ='\0' ; +guest_printk (cd,XENLOG_G_DEBUG "%s\n" , cons->buf); + cons->idx =0 ; + } + spin_unlock(&cons->lock); } -spin_unlock (&cd->pbuf_lock); +#endif
None of the re-indentation is really warranted here (and will likely go away anyway once the #ifdef is dropped).
Quoted 12 lines
xen/common/domain.c +1
-1@@ -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); +xfree (d->console);argo_destroy (d);
xen/common/domain.c struct domain *domain_create(domid_t domid, · line 806
Quoted 7 lines
xen/common/domain.c +4
-0@@ -800,6 +800,11 @@ struct domain *domain_create(domid_t domid,if ( (d =alloc_domain_struct ()) ==NULL )return ERR_PTR (-ENOMEM ); + err = -ENOMEM ; + d->console =xvzalloc (typeof (*d->console)); +if ( !d->console ) + goto fail;
This definitely need to move down some, at least ...
xen/common/domain.c struct domain *domain_create(domid_t domid, · line 810
Quoted 3 lines
xen/common/domain.c +0
-0/* Sort out our idea of is_system_domain(). */ d->domain_id = domid; d->unique_id =get_unique_id ();
... past here. There absolutely must not be struct domain instances be passed around (see e.g. the call to sched_destroy_domain()) without the domain ID set. It's hard to see ...
xen/common/domain.c struct domain *domain_create(domid_t domid, · line 872
Quoted 8 lines
xen/common/domain.c +3
-1@@ -862,7 +867,9 @@ 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); +#ifdef CONFIG_VERBOSE_DEBUG +spin_lock_init (&d->console->lock); +#endif
.. why here or ...
xen/common/domain.c struct domain *domain_create(domid_t domid, · line 961
Quoted 8 lines
xen/common/domain.c +0
-4@@ -955,11 +962,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;
... even here wouldn't be early enough anyway.
And btw - where did this buffer allocation move? I don't see anywhere that d->console->buf would now be initialized. (However, see below.)
xen/include/xen/sched.h struct evtchn_port_ops; · line 375
Quoted 8 lines
xen/include/xen/sched.h +2
-0@@ -371,6 +371,22 @@ struct evtchn_port_ops;#define MAX_NR_IOREQ_SERVERS 8 +/* Arbitrary value; must be a multiple of the cacheline size. */ +#define DOMAIN_CONSOLE_BUF_SIZE 256
Where does the relationship with cache line size come from? What if Xen was to run on hardware (whichever arch) with 512-byte cache lines?
xen/include/xen/sched.h struct evtchn_port_ops; · line 381
Quoted 5 lines
xen/include/xen/sched.h +5
-0+/* Domain console settings. */ +struct domain_console { +#ifdef CONFIG_VERBOSE_DEBUG +/* hvm_print_line() and guest_console_write() logging. */ +char *buf;
To avoid the need to do yet another separate allocation, how about ...
xen/include/xen/sched.h struct evtchn_port_ops; · line 387
Quoted 6 lines
xen/include/xen/sched.h +6
-0+unsigned int idx; +spinlock_t lock; +#endif /* CONFIG_VERBOSE_DEBUG */ + +/* Permission to take ownership of the physical console input. */ +bool input_allowed;
char buf[DOMAIN_CONSOLE_BUF_SIZE];
Ultimately this would allow the buffer size to be e.g. command line controlled (if so desired), by then simply converting to a flexible array member.
Jan
Quoted 13 lines · Jan Beulich
Quoted 12 lines · On 17.06.2025 03:27, [email protected] wrote:
From: Denis Mukhin <[email protected]>
Introduce
domain_consolefor 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 size to the closest power of 2 (256) and rename the symbol to
DOMAIN_CONSOLE_BUF_SIZE.
Quoted 6 lines
Quoted 2 lines
Move
d->console->bufmanagement underCONFIG_VERBOSE_DEBUGwhen theHYPERCALL_console_iohandler is enabled.This, if at all, needs to be a separate change (with its own justification). I for one don't think
VERBOSE_DEBUGis intended to control any kind of guest output.
Hmm, the option's help text indeed says it mixes two unrelated things. Maybe this wants untangling. With a behavioral change like the one you suggest, that help text would need updating in any event, though.
Jan
Quoted 159 lines · Jan Beulich
Quoted 9 lines · On 17.06.2025 03:27, [email protected] wrote:
From: Denis Mukhin <[email protected]>
Introduce
domain_consolefor 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.Quoted 5 lines
Bump the domain console buffer size to the closest power of 2 (256) and rename the symbol to
DOMAIN_CONSOLE_BUF_SIZE.Move
d->console->bufmanagement underCONFIG_VERBOSE_DEBUGwhen theHYPERCALL_console_iohandler is enabled.This, if at all, needs to be a separate change (with its own justification). I for one don't think
VERBOSE_DEBUGis intended to control any kind of guest output.Quoted 11 lines
xen/arch/x86/hvm/hvm.c +0
-1@@ -559,7 +559,6 @@ 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;char c = *val;ASSERT (bytes ==1 && port ==XEN_HVM_DEBUGCONS_IOPORT );xen/arch/x86/hvm/hvm.c static int cf_check hvm_print_line( · line 571
Quoted 3 lines
xen/arch/x86/hvm/hvm.c +0
-0@@ -570,17 +569,24 @@ static int cf_check hvm_print_line(if ( !is_console_printable (c) )return X86EMUL_OKAY ;After this "return" ...
xen/arch/x86/hvm/hvm.c static int cf_check hvm_print_line( · line 573
Quoted 7 lines
xen/arch/x86/hvm/hvm.c +2
-5- -spin_lock (&cd->pbuf_lock); -if ( c !='\n' ) - cd->pbuf[cd->pbuf_idx++] = c; -if ( (cd->pbuf_idx == (DOMAIN_PBUF_SIZE -1 )) || (c =='\n' ) ) +#ifdef CONFIG_VERBOSE_DEBUG + else... there's no point to have "else" here.
xen/arch/x86/hvm/hvm.c static int cf_check hvm_print_line( · line 575
Quoted 5 lines
xen/arch/x86/hvm/hvm.c +1
-3{ - cd->pbuf[cd->pbuf_idx] ='\0' ; -guest_printk (cd,XENLOG_G_DEBUG "%s\n" , cd->pbuf); - cd->pbuf_idx =0 ; +struct domain *cd = current->domain;We normally name such a variable d. It also looks as if this could be pointer- to-const.
Quoted 5 lines
xen/arch/x86/hvm/hvm.c +5
-0+struct domain_console *cons = cd->console; + +spin_lock (&cons->lock); +if ( c !='\n' ) + cons->buf[cons->idx++] = c;xen/arch/x86/hvm/hvm.c static int cf_check hvm_print_line( · line 589
Quoted 10 lines
xen/arch/x86/hvm/hvm.c +8
-1+if ( (cons->idx == (DOMAIN_CONSOLE_BUF_SIZE -1 )) || (c =='\n' ) ) + { + cons->buf[cons->idx] ='\0' ; +guest_printk (cd,XENLOG_G_DEBUG "%s\n" , cons->buf); + cons->idx =0 ; + } + spin_unlock(&cons->lock); } -spin_unlock (&cd->pbuf_lock); +#endif None of the re-indentation is really warranted here (and will likely go away anyway once the #ifdef is dropped).
Quoted 12 lines
xen/common/domain.c +1
-1@@ -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); +xfree (d->console);argo_destroy (d);xen/common/domain.c struct domain *domain_create(domid_t domid, · line 806
Quoted 7 lines
xen/common/domain.c +4
-0@@ -800,6 +800,11 @@ struct domain *domain_create(domid_t domid,if ( (d =alloc_domain_struct ()) ==NULL )return ERR_PTR (-ENOMEM ); + err = -ENOMEM ; + d->console =xvzalloc (typeof (*d->console)); +if ( !d->console ) + goto fail;This definitely need to move down some, at least ...
xen/common/domain.c struct domain *domain_create(domid_t domid, · line 810
Quoted 3 lines
xen/common/domain.c +0
-0/* Sort out our idea of is_system_domain(). */ d->domain_id = domid; d->unique_id =get_unique_id ();... past here. There absolutely must not be struct domain instances be passed around (see e.g. the call to
sched_destroy_domain()) without the domain ID set. It's hard to see ...xen/common/domain.c struct domain *domain_create(domid_t domid, · line 872
Quoted 8 lines
xen/common/domain.c +3
-1@@ -862,7 +867,9 @@ 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); +#ifdef CONFIG_VERBOSE_DEBUG +spin_lock_init (&d->console->lock); +#endif .. why here or ...
xen/common/domain.c struct domain *domain_create(domid_t domid, · line 961
Quoted 8 lines
xen/common/domain.c +0
-4@@ -955,11 +962,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;... even here wouldn't be early enough anyway.
And btw - where did this buffer allocation move? I don't see anywhere that
d->console->bufwould now be initialized. (However, see below.)xen/include/xen/sched.h struct evtchn_port_ops; · line 375
Quoted 8 lines
xen/include/xen/sched.h +2
-0@@ -371,6 +371,22 @@ struct evtchn_port_ops;#define MAX_NR_IOREQ_SERVERS 8 +/* Arbitrary value; must be a multiple of the cacheline size. */ +#define DOMAIN_CONSOLE_BUF_SIZE 256Where does the relationship with cache line size come from? What if Xen was to run on hardware (whichever arch) with 512-byte cache lines?
xen/include/xen/sched.h struct evtchn_port_ops; · line 381
Quoted 5 lines
xen/include/xen/sched.h +5
-0+/* Domain console settings. */ +struct domain_console { +#ifdef CONFIG_VERBOSE_DEBUG +/* hvm_print_line() and guest_console_write() logging. */ +char *buf;To avoid the need to do yet another separate allocation, how about ...
xen/include/xen/sched.h struct evtchn_port_ops; · line 387
Quoted 6 lines
xen/include/xen/sched.h +6
-0+unsigned int idx; +spinlock_t lock; +#endif /* CONFIG_VERBOSE_DEBUG */ + +/* Permission to take ownership of the physical console input. */ +bool input_allowed;char buf[DOMAIN_CONSOLE_BUF_SIZE];
Quoted 3 lines
Ultimately this would allow the buffer size to be e.g. command line controlled (if so desired), by then simply converting to a flexible array member.
Whoops, apologies for the bogus change.
Looks like I posted from the wrong branch.
Quoted 2 lines
Jan