common/sysctl: Introduce hypercall to query the console ring size
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH v4 1/1 common/sysctl: Introduce hypercall to query the console ring size
--
Changes since v2:
* Rebase on top of coverage patches.
* Reword hypercall comments.
xen/common/sysctl.c +4 -0
@@ -358,6 +358,10 @@ long do_sysctl(XEN_GUEST_HANDLE_PARAM(xe }break ; +case XEN_SYSCTL_consoleringsize: + ret =console_ring_size (&op->u.consoleringsize); +break ; +#ifdef TEST_COVERAGE case XEN_SYSCTL_coverage_op: ret =sysctl_coverage_op (&op->u.coverage_op);
xen/drivers/char/console.c +6 -0
@@ -226,6 +226,12 @@ long read_console_ring(struct xen_sysctlreturn 0 ; } +long console_ring_size (struct xen_sysctl_consoleringsize * op) +{ + op->size = conring_size; +return 0 ; +} +/* * *******************************************************
xen/include/public/sysctl.h +10 -0
@@ -632,6 +632,14 @@ typedef struct xen_sysctl_coverage_op xeDEFINE_XEN_GUEST_HANDLE (xen_sysctl_coverage_op_t); +/* XEN_SYSCTL_consoleringsize */ +/* Get the size of the hypervisor console ring in bytes. */ +struct xen_sysctl_consoleringsize { +uint64_t size;/* OUT */ +}; +typedef struct xen_sysctl_consoleringsize xen_sysctl_consoleringsize_t ; +DEFINE_XEN_GUEST_HANDLE (xen_sysctl_consoleringsize_t); +struct xen_sysctl {uint32_t cmd;#define XEN_SYSCTL_readconsole 1
@@ -653,6 +661,7 @@ struct xen_sysctl {#define XEN_SYSCTL_cpupool_op 18#define XEN_SYSCTL_scheduler_op 19#define XEN_SYSCTL_coverage_op 20 +#define XEN_SYSCTL_consoleringsize 21uint32_t interface_version;/* XEN_SYSCTL_INTERFACE_VERSION */ union {struct xen_sysctl_readconsole readconsole;
@@ -674,6 +683,7 @@ struct xen_sysctl {struct xen_sysctl_cpupool_op cpupool_op;struct xen_sysctl_scheduler_op scheduler_op;struct xen_sysctl_coverage_op coverage_op; +struct xen_sysctl_consoleringsize consoleringsize;uint8_t pad[128 ]; } u; };
xen/include/xen/console.h +2 -0
@@ -12,6 +12,8 @@struct xen_sysctl_readconsole ;long read_console_ring (struct xen_sysctl_readconsole *op); +struct xen_sysctl_consoleringsize ; +long console_ring_size (struct xen_sysctl_consoleringsize *op);void console_init_preirq (void );void console_init_postirq (void );
PATCH v4 1/1 tools/libxc: Helper function for XEN_SYSCTL_consoleringsize
In reply to a message not in the archive---
Changes since v2:
* Tweak style and errno in case of invalid pointer.
tools/libxc/xc_misc.c +20 -0
@@ -100,6 +100,26 @@ int xc_readconsolering(xc_interface *xchreturn ret; } +int xc_consoleringsize (xc_interface *xch,uint64_t *psize) +{ +int ret =-1 ; +DECLARE_SYSCTL ; + +if ( !psize ) + { + errno =EINVAL ; +return ret; + } + + sysctl.cmd = XEN_SYSCTL_consoleringsize; + ret =do_sysctl (xch, &sysctl); + +if ( !ret ) + *psize = sysctl.u.consoleringsize.size; + +return ret; +} +int xc_send_debug_keys (xc_interface *xch,char *keys) {int ret, len =strlen (keys);
tools/libxc/xenctrl.h +1 -0
@@ -998,6 +998,7 @@ int xc_readconsolering(xc_interface *xchchar *buffer, unsigned int *pnr_chars, int clear, int incremental, uint32_t *pindex); +int xc_consoleringsize (xc_interface *xch,uint64_t *psize);int xc_send_debug_keys (xc_interface *xch,char *keys);
PATCH v4 1/1 tools/libxc: Implement xc_readconsolering_buffer()
In reply to a message not in the archiveFunctions identically to xc_readconsolering(), but uses a user-provided xc_hypercall_buffer_t to save using a bounce buffer.
--
Changes since v2:
* Document xc_readconsolering() and xc_readconsolering_buffer() functions
Changes since v1:
* Reduce xc_readconsolering() to use xc_readconsolering_buffer()
tools/libxc/xc_misc.c +19 -5
@@ -70,13 +70,29 @@ int xc_readconsolering(xc_interface *xch int clear, int incremental, uint32_t *pindex) { int ret; -unsigned int nr_chars = *pnr_chars; -DECLARE_SYSCTL ; -DECLARE_HYPERCALL_BOUNCE (buffer, nr_chars,XC_HYPERCALL_BUFFER_BOUNCE_OUT ); +DECLARE_HYPERCALL_BOUNCE (buffer, *pnr_chars,XC_HYPERCALL_BUFFER_BOUNCE_OUT );if (xc_hypercall_bounce_pre (xch, buffer) )return -1 ; + ret =xc_readconsolering_buffer (xch,HYPERCALL_BUFFER (buffer), + pnr_chars, clear, incremental, pindex); + +xc_hypercall_bounce_post (xch, buffer); + +return ret; +} + +int xc_readconsolering_buffer (xc_interface *xch, +xc_hypercall_buffer_t *buffer, +unsigned int *pnr_chars, +int clear,int incremental,uint32_t *pindex) +{ +int ret; +unsigned int nr_chars = *pnr_chars; +DECLARE_SYSCTL ; +DECLARE_HYPERCALL_BUFFER_ARGUMENT (buffer); + sysctl.cmd = XEN_SYSCTL_readconsole;set_xen_guest_handle (sysctl.u.readconsole.buffer, buffer); sysctl.u.readconsole.count = nr_chars;
@@ -95,8 +111,6 @@ int xc_readconsolering(xc_interface *xch *pindex = sysctl.u.readconsole.index; } -xc_hypercall_bounce_post (xch, buffer); -return ret; }
tools/libxc/xenctrl.h +16 -0
@@ -994,10 +994,26 @@ int xc_physdev_pci_access_modify(xc_inteint func, int enable); +/* +* For both readconsolering functions, *pnr_chars is both an input and +* output. As an input, it specifies the size of *buffer, and as an output +* indicates now many character Xen wrote into *buffer. +* +* The 'clear' parameter indicates whether Xen should clear the buffer or not. +* If incremental is set, *pindex is an input and output parameter to aid with +* sequential small reads of the console ring. +* +* xc_readconsolering_buffer is preferred to avoid an extra copy of the console +* ring buffer. +*/ int xc_readconsolering (xc_interface *xch,char *buffer,unsigned int *pnr_chars,int clear,int incremental,uint32_t *pindex); +int xc_readconsolering_buffer (xc_interface *xch, +xc_hypercall_buffer_t *buffer, +unsigned int *pnr_chars, +int clear,int incremental,uint32_t *pindex);int xc_consoleringsize (xc_interface *xch,uint64_t *psize);int xc_send_debug_keys (xc_interface *xch,char *keys);
PATCH v4 1/1 tools/ocaml: libxc bindings: Fix stub_xc_readconsolering()
In reply to a message not in the archiveThere are two problems with this function:
* The caml_{enter,leave}_blocking_section() calls mean that two threads can
compete for use of the static ring[] buffer.
* It fails to retrieve the entire buffer if the hypervisor has used 32K or more of its console ring.
Rewrite the function using the new xc_consoleringsize() and xc_readconsolering_buffer() functions to efficiently grab the entire console ring.
--
Changes since v3:
* Use caml_alloc_string/memcpy in preference to caml_copy_string to allow for the possibility of NULL characters in the middle of the conring
Changes since v2:
* Tweak style and remove redundant variables
Changes since v1:
* Convert conring_size to being static to avoid needless hypercalls
* Fix memory due to ordering of failwith_xc() and xc_hypercall_buffer_free()
* Remove useless CAMLreturns()
tools/ocaml/libs/xc/xenctrl_stubs.c +29 -9
@@ -523,27 +523,47 @@ CAMLprim value stub_xc_evtchn_reset(valuCAMLreturn (Val_unit); } - -#define RING_SIZE 32768 -static char ring[RING_SIZE ]; - CAMLprim value stub_xc_readconsolering(value xch) { -unsigned int size =RING_SIZE -1 ; -char *ring_ptr = ring; +static uint64_t conring_size =0 ; +unsigned int nr_chars; +DECLARE_HYPERCALL_BUFFER (char, ring);int retval;CAMLparam1 (xch); +CAMLlocal1 (conring); + +if (!conring_size) + { +if (xc_consoleringsize (_H (xch), &conring_size)) +failwith_xc (_H (xch)); + +/* Round conring_size up to the next page, for NULL terminator +and slop from a race with printk() in the hypervisor. */ + conring_size = (conring_size +XC_PAGE_SIZE ) &XC_PAGE_MASK ; + } + + nr_chars = conring_size -1 ; + ring =xc_hypercall_buffer_alloc (_H (xch), ring, conring_size); + +if (!ring) +caml_raise_out_of_memory ();caml_enter_blocking_section (); - retval =xc_readconsolering (_H (xch), ring_ptr, &size,0 ,0 ,NULL ); + retval =xc_readconsolering_buffer (_H (xch),HYPERCALL_BUFFER (ring), + &nr_chars,0 ,0 ,NULL );caml_leave_blocking_section ();if (retval) + { +xc_hypercall_buffer_free (_H (xch), ring);failwith_xc (_H (xch)); + } - ring[size] ='\0' ; -CAMLreturn (caml_copy_string (ring)); + conring =caml_alloc_string (nr_chars); +memcpy (String_val (conring), ring, nr_chars); +xc_hypercall_buffer_free (_H (xch), ring); +CAMLreturn (conring); } CAMLprim value stub_xc_send_debug_keys(value xch, value keys)
PATCH v4 1/1 tools/ocaml: libxc bindings: Fix failwith_xc()
In reply to a message not in the archiveThe static error_str[] buffer is not thread-safe, and 1024 bytes is unreasonably large. Reduce to 256 bytes (which is still much larger than any current use), and move it to being a stack variable.
--
Changes since v1:
* Mark as Noreturn, due to unconditional use of caml_raise_with_string()
tools/ocaml/libs/xc/xenctrl_stubs.c +8 -7
@@ -51,21 +51,22 @@ i1 = (uint32_t )Int64_val (Field (input,0 )); \ i2 = ((Field (input,1 ) == Val_none) ?0xffffffff : (uint32_t )Int64_val (Field (Field (input,1 ),0 ))); -#define ERROR_STRLEN 1024 -void failwith_xc (xc_interface *xch) +static void Noreturn failwith_xc(xc_interface *xch) { -static char error_str[ERROR_STRLEN ]; +char error_str[256 ];if (xch) {const xc_error *error =xc_get_last_error (xch);if (error->code ==XC_ERROR_NONE ) -snprintf (error_str,ERROR_STRLEN ,"%d: %s" , errno,strerror (errno)); +snprintf (error_str,sizeof (error_str), +"%d: %s" , errno,strerror (errno));else -snprintf (error_str,ERROR_STRLEN ,"%d: %s: %s" , - error->code, +snprintf (error_str,sizeof (error_str), +"%d: %s: %s" , error->code,xc_error_code_to_desc (error->code), error->message); }else { -snprintf (error_str,ERROR_STRLEN ,"Unable to open XC interface" ); +snprintf (error_str,sizeof (error_str), +"Unable to open XC interface" ); }caml_raise_with_string (*caml_named_value ("xc.error" ), error_str); }
A message that is not in the archive (sent off-list or not yet received): [email protected]
Dave Scott
Acked-byRe: [PATCH 4 of 5 v4] tools/ocaml: libxc bindings: Fix stub_xc_readconsolering()
This looks good to me.
Quoted 102 lines
-----Original Message-----
From: [email protected] [mailto:xen-devel- [email protected]] On Behalf Of Andrew Cooper
Sent: 13 March 2013 12:52 PM
To: [email protected]
Cc: Ian Jackson; Keir (Xen.org); Ian Campbell; Jan Beulich; xen- [email protected]
Subject: [Xen-devel] [PATCH 4 of 5 v4]tools/ocaml: libxc bindings: Fixstub_xc_readconsolering()There are two problems with this function:
* The caml_{enter,leave}_blocking_section() calls mean that two threads
cancompete for use of the static ring[] buffer.* It fails to retrieve the entire buffer if the hypervisor has used 32K or more of its console ring.
Rewrite the function using the new
xc_consoleringsize()andxc_readconsolering_buffer()functions to efficiently grab the entire console ring.Signed-off-by Andrew Cooper <[email protected]>--
Changes since v3:
* Use caml_alloc_string/memcpy in preference tocaml_copy_stringto
allow
for the possibility of NULL characters in the middle of the conring Changes
since v2:
* Tweak style and remove redundant variables Changes since v1:
* Convertconring_sizeto being static to avoid needless hypercalls
* Fix memory due to ordering offailwith_xc()andxc_hypercall_buffer_free()
* Remove uselessCAMLreturns()diff -r
c7b82dfbec34-r5bd08f35b362tools/ocaml/libs/xc/xenctrl_stubs.ctools/ocaml/libs/xc/xenctrl_stubs.c +11
-6@@ -523,27 +523,47 @@ CAMLprim value stub_xc_evtchn_reset(valuCAMLreturn (Val_unit); } - -#define RING_SIZE 32768 -static char ring[RING_SIZE ]; - CAMLprim value stub_xc_readconsolering(value xch) { -unsigned int size =RING_SIZE -1 ; -char *ring_ptr = ring; +static uint64_t conring_size =0 ; +unsigned int nr_chars; +DECLARE_HYPERCALL_BUFFER (char, ring);int retval;CAMLparam1 (xch); +CAMLlocal1 (conring); + +if (!conring_size) + { +if (xc_consoleringsize (_H (xch), &conring_size)) +failwith_xc (_H (xch)); + +/* Round conring_size up to the next page, for NULL terminator
tools/ocaml/libs/xc/xenctrl_stubs.c +2
-0+and slop from a race with printk() in the hypervisor. */ + conring_size = (conring_size + XC_PAGE_SIZE) &
XC_PAGE_MASK;tools/ocaml/libs/xc/xenctrl_stubs.c +8
-1+ } + + nr_chars = conring_size -1 ; + ring =xc_hypercall_buffer_alloc (_H (xch), ring, conring_size); + +if (!ring) +caml_raise_out_of_memory ();caml_enter_blocking_section (); - retval =xc_readconsolering (_H (xch), ring_ptr, &size,0 ,0 ,NULL ); + retval = xc_readconsolering_buffer(_H(xch),
HYPERCALL_BUFFER(ring),tools/ocaml/libs/xc/xenctrl_stubs.c +8
-2+ &nr_chars, 0, 0, NULL);caml_leave_blocking_section ();if (retval) + { +xc_hypercall_buffer_free (_H (xch), ring);failwith_xc (_H (xch)); + } - ring[size] ='\0' ; -CAMLreturn (caml_copy_string (ring)); + conring =caml_alloc_string (nr_chars); +memcpy (String_val (conring), ring, nr_chars); +xc_hypercall_buffer_free (_H (xch), ring); +CAMLreturn (conring); } CAMLprim value stub_xc_send_debug_keys(value xch, value keys)_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [PATCH 2 of 5 v4] tools/libxc: Helper function for XEN_SYSCTL_consoleringsize
Quoted 1 line · Andrew Cooper
Signed-off-by Andrew Cooper <[email protected]>
Keir/Jan: Ping? This is a hypervisor change.
Quoted 51 lines
---
Changes since v2:
* Tweak style and errno in case of invalid pointer.diff -r
89f3c6846f6b-r0bd32c19873etools/libxc/xc_misc.ctools/libxc/xc_misc.c +20
-0@@ -100,6 +100,26 @@ int xc_readconsolering(xc_interface *xchreturn ret; } +int xc_consoleringsize (xc_interface *xch,uint64_t *psize) +{ +int ret =-1 ; +DECLARE_SYSCTL ; + +if ( !psize ) + { + errno =EINVAL ; +return ret; + } + + sysctl.cmd = XEN_SYSCTL_consoleringsize; + ret =do_sysctl (xch, &sysctl); + +if ( !ret ) + *psize = sysctl.u.consoleringsize.size; + +return ret; +} +int xc_send_debug_keys (xc_interface *xch,char *keys) {int ret, len =strlen (keys);diff -r
89f3c6846f6b-r0bd32c19873etools/libxc/xenctrl.htools/libxc/xenctrl.h +1
-0@@ -998,6 +998,7 @@ int xc_readconsolering(xc_interface *xchchar *buffer, unsigned int *pnr_chars, int clear, int incremental, uint32_t *pindex); +int xc_consoleringsize (xc_interface *xch,uint64_t *psize);int xc_send_debug_keys (xc_interface *xch,char *keys);_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [PATCH 2 of 5 v4] tools/libxc: Helper function for XEN_SYSCTL_consoleringsize
Quoted 3 lines · Andrew Cooper
Quoted 1 line · Andrew Cooper
Signed-off-by Andrew Cooper <[email protected]>Keir/Jan: Ping? This is a hypervisor change.
Apologies - this is actually a tools change. Ian/Ian?
Quoted 56 lines
Quoted 47 lines
---
Changes since v2:
* Tweak style and errno in case of invalid pointer.diff -r
89f3c6846f6b-r0bd32c19873etools/libxc/xc_misc.ctools/libxc/xc_misc.c +20
-0@@ -100,6 +100,26 @@ int xc_readconsolering(xc_interface *xchreturn ret; } +int xc_consoleringsize (xc_interface *xch,uint64_t *psize) +{ +int ret =-1 ; +DECLARE_SYSCTL ; + +if ( !psize ) + { + errno =EINVAL ; +return ret; + } + + sysctl.cmd = XEN_SYSCTL_consoleringsize; + ret =do_sysctl (xch, &sysctl); + +if ( !ret ) + *psize = sysctl.u.consoleringsize.size; + +return ret; +} +int xc_send_debug_keys (xc_interface *xch,char *keys) {int ret, len =strlen (keys);diff -r
89f3c6846f6b-r0bd32c19873etools/libxc/xenctrl.htools/libxc/xenctrl.h +1
-0@@ -998,6 +998,7 @@ int xc_readconsolering(xc_interface *xchchar *buffer, unsigned int *pnr_chars, int clear, int incremental, uint32_t *pindex); +int xc_consoleringsize (xc_interface *xch,uint64_t *psize);int xc_send_debug_keys (xc_interface *xch,char *keys);Quoted 4 lines
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Ian Campbell
Acked-byRe: [PATCH 2 of 5 v4] tools/libxc: Helper function for XEN_SYSCTL_consoleringsize
Quoted 6 lines · Andrew Cooper
Quoted 3 lines · Andrew Cooper
Quoted 1 line · Andrew Cooper
Signed-off-by Andrew Cooper <[email protected]>Keir/Jan: Ping? This is a hypervisor change.
Apologies - this is actually a tools change. Ian/Ian?
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 1 line · Andrew Cooper
Signed-off-by Andrew Cooper <[email protected]>
Keir/Jan: Ping - this is a hypervisor change.
~Andrew
Quoted 87 lines
--
Changes since v2:
* Rebase on top of coverage patches.
* Reword hypercall comments.diff -r
a6b81234b189-r89f3c6846f6bxen/common/sysctl.cxen/common/sysctl.c +4
-0@@ -358,6 +358,10 @@ long do_sysctl(XEN_GUEST_HANDLE_PARAM(xe }break ; +case XEN_SYSCTL_consoleringsize: + ret =console_ring_size (&op->u.consoleringsize); +break ; +#ifdef TEST_COVERAGE case XEN_SYSCTL_coverage_op: ret =sysctl_coverage_op (&op->u.coverage_op);diff -r
a6b81234b189-r89f3c6846f6bxen/drivers/char/console.cxen/drivers/char/console.c +6
-0@@ -226,6 +226,12 @@ long read_console_ring(struct xen_sysctlreturn 0 ; } +long console_ring_size (struct xen_sysctl_consoleringsize * op) +{ + op->size = conring_size; +return 0 ; +} +/* * ******************************************************* diff -r
a6b81234b189-r89f3c6846f6bxen/include/public/sysctl.hxen/include/public/sysctl.h +10
-0@@ -632,6 +632,14 @@ typedef struct xen_sysctl_coverage_op xeDEFINE_XEN_GUEST_HANDLE (xen_sysctl_coverage_op_t); +/* XEN_SYSCTL_consoleringsize */ +/* Get the size of the hypervisor console ring in bytes. */ +struct xen_sysctl_consoleringsize { +uint64_t size;/* OUT */ +}; +typedef struct xen_sysctl_consoleringsize xen_sysctl_consoleringsize_t ; +DEFINE_XEN_GUEST_HANDLE (xen_sysctl_consoleringsize_t); +struct xen_sysctl {uint32_t cmd;#define XEN_SYSCTL_readconsole 1@@ -653,6 +661,7 @@ struct xen_sysctl {#define XEN_SYSCTL_cpupool_op 18#define XEN_SYSCTL_scheduler_op 19#define XEN_SYSCTL_coverage_op 20 +#define XEN_SYSCTL_consoleringsize 21uint32_t interface_version;/* XEN_SYSCTL_INTERFACE_VERSION */ union {struct xen_sysctl_readconsole readconsole;@@ -674,6 +683,7 @@ struct xen_sysctl {struct xen_sysctl_cpupool_op cpupool_op;struct xen_sysctl_scheduler_op scheduler_op;struct xen_sysctl_coverage_op coverage_op; +struct xen_sysctl_consoleringsize consoleringsize;uint8_t pad[128 ]; } u; };diff -r
a6b81234b189-r89f3c6846f6bxen/include/xen/console.hxen/include/xen/console.h +2
-0@@ -12,6 +12,8 @@struct xen_sysctl_readconsole ;long read_console_ring (struct xen_sysctl_readconsole *op); +struct xen_sysctl_consoleringsize ; +long console_ring_size (struct xen_sysctl_consoleringsize *op);void console_init_preirq (void );void console_init_postirq (void );_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 4 lines · Andrew Cooper
Quoted 1 line · Andrew Cooper
Signed-off-by Andrew Cooper <[email protected]>Keir/Jan: Ping - this is a hypervisor change.
Anyone? This hypervisor side portion of this series seems pretty trivial to me.
Ian.
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [PATCH 3 of 5 v4] tools/libxc: Implement xc_readconsolering_buffer()
Quoted 6 lines · Andrew Cooper
+/*
+ * For both readconsolering functions,*pnr_charsis both an input and
+ * output. As an input, it specifies the size of *buffer, and as an output
+ * indicates now many character Xen wrote into *buffer.
+ *
+ * The 'clear' parameter indicates whether Xen should clear the buffer or not.
Buffer here refers to Xen's internal ring, not the char *buffer argument to this function, right? If so the wording (at least in the context) is a bit confusing!
The actual code looks good to me.
Ian.