Skip to contentSkip to conversation

common/sysctl: Introduce hypercall to query the console ring size

PATCH Superseded Andrew Cooper 2 participants 12 messages Feb 21, 2013 – Mar 12, 2013 6 messages not in the archive

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#1 lore raw

PATCH v2 1/1 common/sysctl: Introduce hypercall to query the console ring size

In reply to a message not in the archive
xen/common/sysctl.c +3 -0
diff -r 66f563be41d9 -r 1306e69a7018 xen/common/sysctl.c
@@ -357,6 +357,9 @@ long do_sysctl(XEN_GUEST_HANDLE_PARAM(xe
     }
     break;
 
+    case XEN_SYSCTL_consoleringsize:
+        ret = console_ring_size(&op->u.consoleringsize);
+        break;
 
     default:
         ret = arch_do_sysctl(op, u_sysctl);
xen/drivers/char/console.c +6 -0
diff -r 66f563be41d9 -r 1306e69a7018 xen/drivers/char/console.c
@@ -226,6 +226,12 @@ long read_console_ring(struct xen_sysctl
     return 0;
 }
 
+long console_ring_size(struct xen_sysctl_consoleringsize * op)
+{
+    op->size = conring_size;
+    return 0;
+}
+
 
 /*
  * *******************************************************
xen/include/public/sysctl.h +10 -0
diff -r 66f563be41d9 -r 1306e69a7018 xen/include/public/sysctl.h
@@ -596,6 +596,14 @@ struct xen_sysctl_scheduler_op {
 typedef struct xen_sysctl_scheduler_op xen_sysctl_scheduler_op_t;
 DEFINE_XEN_GUEST_HANDLE(xen_sysctl_scheduler_op_t);
 
+/* XEN_SYSCTL_consoleringsize */
+/* Get the size of the hypervisor console ring. */
+struct xen_sysctl_consoleringsize {
+    uint64_t size; /* Written by Xen. */
+};
+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
@@ -616,6 +624,7 @@ struct xen_sysctl {
 #define XEN_SYSCTL_numainfo                      17
 #define XEN_SYSCTL_cpupool_op                    18
 #define XEN_SYSCTL_scheduler_op                  19
+#define XEN_SYSCTL_consoleringsize               20
     uint32_t interface_version; /* XEN_SYSCTL_INTERFACE_VERSION */
     union {
         struct xen_sysctl_readconsole       readconsole;
@@ -636,6 +645,7 @@ struct xen_sysctl {
         struct xen_sysctl_lockprof_op       lockprof_op;
         struct xen_sysctl_cpupool_op        cpupool_op;
         struct xen_sysctl_scheduler_op      scheduler_op;
+        struct xen_sysctl_consoleringsize   consoleringsize;
         uint8_t                             pad[128];
     } u;
 };
xen/include/xen/console.h +2 -0
diff -r 66f563be41d9 -r 1306e69a7018 xen/include/xen/console.h
@@ -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);
#2 lore raw

PATCH v2 1/1 tools/libxc: Helper function for XEN_SYSCTL_consoleringsize

In reply to a message not in the archive
tools/libxc/xc_misc.c +20 -0
diff -r 1306e69a7018 -r 1ffce9bf90f5 tools/libxc/xc_misc.c
@@ -100,6 +100,26 @@ int xc_readconsolering(xc_interface *xch
     return ret;
 }
 
+int xc_consoleringsize(xc_interface *xch, uint64_t * psize)
+{
+    int ret = -1;
+    DECLARE_SYSCTL;
+
+    if ( ! psize )
+    {
+        errno = EFAULT;
+        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
diff -r 1306e69a7018 -r 1ffce9bf90f5 tools/libxc/xenctrl.h
@@ -998,6 +998,7 @@ int xc_readconsolering(xc_interface *xch
                        char *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);
#3 lore raw

PATCH v2 1/1 tools/libxc: Implement xc_readconsolering_buffer()

In reply to a message not in the archive

Functions identically to xc_readconsolering(), but uses a user-provided xc_hypercall_buffer_t to save using a bounce buffer.

--
Changes since v1:
* Reduce xc_readconsolering() to use xc_readconsolering_buffer()

tools/libxc/xc_misc.c +19 -5
diff -r 1ffce9bf90f5 -r 1e6c7f7cec6f tools/libxc/xc_misc.c
@@ -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 +4 -0
diff -r 1ffce9bf90f5 -r 1e6c7f7cec6f tools/libxc/xenctrl.h
@@ -998,6 +998,10 @@ 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);
#4 lore raw

PATCH v2 1/1 tools/ocaml: libxc bindings: Fix stub_xc_readconsolering()

In reply to a message not in the archive

There 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 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 +34 -11
diff -r 1e6c7f7cec6f -r 1774a72fde4a tools/ocaml/libs/xc/xenctrl_stubs.c
@@ -523,27 +523,50 @@ CAMLprim value stub_xc_evtchn_reset(valu
 	CAMLreturn(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;
-	int retval;
+	static int have_conring_size = 0;
+	static uint64_t conring_size;
+
+	unsigned int nr_chars;
+	DECLARE_HYPERCALL_BUFFER(char, ring);
+	int r;
 
 	CAMLparam1(xch);
+	CAMLlocal1(conring);
+
+	if ( ! have_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;
+		have_conring_size = 0;
+	}
+
+	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);
+	r = xc_readconsolering_buffer(_H(xch), HYPERCALL_BUFFER(ring),
+				      &nr_chars, 0, 0, NULL);
 	caml_leave_blocking_section();
 
-	if (retval)
+	if ( r )
+	{
+		xc_hypercall_buffer_free(_H(xch), ring);
 		failwith_xc(_H(xch));
+	}
 
-	ring[size] = '\0';
-	CAMLreturn(caml_copy_string(ring));
+	ring[nr_chars] = '\0';
+	conring = caml_copy_string(ring);
+	xc_hypercall_buffer_free(_H(xch), ring);
+	CAMLreturn(conring);
 }
 
 CAMLprim value stub_xc_send_debug_keys(value xch, value keys)
#5 lore raw

PATCH v2 1/1 tools/ocaml: libxc bindings: Fix failwith_xc()

In reply to a message not in the archive

The 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
diff -r 1774a72fde4a -r 3300e9cad49d tools/ocaml/libs/xc/xenctrl_stubs.c
@@ -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);
 }
#6 lore raw

Re: [PATCH 0 of 5 v2] Tools: Console ring improvements

In reply to a message not in the archive

Ping?

Quoted 30 lines · Andrew Cooper

Hello,

The following set of patches came about when trying to fix the 32K limitation with stub_xc_readconsolering() in the ocaml bindings.

Patch 1 is a hypervisor patch adding a new SYSCTL hypercall to query the size of the console ring, which is fixed but otherwise inaccessible after boot.

Patches 2 thru 5 are tools patches:
* Patch 2 adds a libxc function to use the new SYSCTL hypercall.
* Patch 3 implements an alternative xc_readconsolering() to prevent bounce buffering.
* Patch 4 uses the preceding patches to fix the implementation of stub_xc_readconsolering() in the ocaml bindings.
* Patch 5 is a misc threading fix in the ocaml bindings, discovered when trying to fix the console functionality.

CC'd is [email protected] as well, for comment on the ocaml patches.

This patch set has been compile-tested on unstable, and functionally tested via backport to 4.2 (which was only one whitespace fuzz issue and no code change).

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#8 lore raw
In reply to a message not in the archive
Quoted 1 line · Andrew Cooper

Seems pretty obvious to me, any (n)ack from a hypervisor hat wearing person?

Quoted 34 lines

diff -r 66f563be41d9 -r 1306e69a7018 xen/common/sysctl.c

xen/common/sysctl.c +3 -0
@@ -357,6 +357,9 @@ long do_sysctl(XEN_GUEST_HANDLE_PARAM(xe
     }
     break;
 
+    case XEN_SYSCTL_consoleringsize:
+        ret = console_ring_size(&op->u.consoleringsize);
+        break;
 
     default:
         ret = arch_do_sysctl(op, u_sysctl);

diff -r 66f563be41d9 -r 1306e69a7018 xen/drivers/char/console.c

xen/drivers/char/console.c +6 -0
@@ -226,6 +226,12 @@ long read_console_ring(struct xen_sysctl
     return 0;
 }
 
+long console_ring_size(struct xen_sysctl_consoleringsize * op)
+{
+    op->size = conring_size;
+    return 0;
+}
+
 
 /*
  * *******************************************************

diff -r 66f563be41d9 -r 1306e69a7018 xen/include/public/sysctl.h

xen/include/public/sysctl.h struct xen_sysctl_scheduler_op { · line 602

Quoted 7 lines
xen/include/public/sysctl.h +4 -0
@@ -596,6 +596,14 @@ struct xen_sysctl_scheduler_op {
 typedef struct xen_sysctl_scheduler_op xen_sysctl_scheduler_op_t;
 DEFINE_XEN_GUEST_HANDLE(xen_sysctl_scheduler_op_t);
 
+/* XEN_SYSCTL_consoleringsize */
+/* Get the size of the hypervisor console ring. */
+struct xen_sysctl_consoleringsize {
+    uint64_t size;

Is this bytes? Because even 2^32 is a hell of a lot of logging... I suppose you could say it was future-proof ;-)

xen/include/public/sysctl.h struct xen_sysctl_scheduler_op { · line 603

Quoted 1 line
xen/include/public/sysctl.h +0 -0
 /* Written by Xen. */

The norm in the Xen headers seems to be /* OUT */ above the output variables.

Quoted 35 lines
xen/include/public/sysctl.h +4 -0
+};
+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

xen/include/public/sysctl.h +2 -0
@@ -616,6 +624,7 @@ struct xen_sysctl {
 #define XEN_SYSCTL_numainfo                      17
 #define XEN_SYSCTL_cpupool_op                    18
 #define XEN_SYSCTL_scheduler_op                  19
+#define XEN_SYSCTL_consoleringsize               20
     uint32_t interface_version; /* XEN_SYSCTL_INTERFACE_VERSION */
     union {
         struct xen_sysctl_readconsole       readconsole;
@@ -636,6 +645,7 @@ struct xen_sysctl {
         struct xen_sysctl_lockprof_op       lockprof_op;
         struct xen_sysctl_cpupool_op        cpupool_op;
         struct xen_sysctl_scheduler_op      scheduler_op;
+        struct xen_sysctl_consoleringsize   consoleringsize;
         uint8_t                             pad[128];
     } u;
 };

diff -r 66f563be41d9 -r 1306e69a7018 xen/include/xen/console.h

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);
#12 lore raw

Re: [PATCH 2 of 5 v2] tools/libxc: Helper function for XEN_SYSCTL_consoleringsize

In reply to a message not in the archive
Quoted 7 lines · Andrew Cooper

diff -r 1306e69a7018 -r 1ffce9bf90f5 tools/libxc/xc_misc.c

tools/libxc/xc_misc.c +0 -0
@@ -100,6 +100,26 @@ int xc_readconsolering(xc_interface *xch
     return ret;

tools/libxc/xc_misc.c int xc_readconsolering(xc_interface *xch · line 110

Quoted 10 lines
tools/libxc/xc_misc.c +8 -0
 }
 
+int xc_consoleringsize(xc_interface *xch, uint64_t * psize)
+{
+    int ret = -1;
+    DECLARE_SYSCTL;
+
+    if ( ! psize )
+    {
+        errno = EFAULT;

Strictly I think this is an EINVAL event. EFAULT would be when it is non-NULL but dereferencing it faults.

#10 lore raw
In reply to a message not in the archive
Quoted 48 lines · Ian Campbell
Quoted 1 line · Andrew Cooper

Seems pretty obvious to me, any (n)ack from a hypervisor hat wearing person?

Quoted 33 lines

diff -r 66f563be41d9 -r 1306e69a7018 xen/common/sysctl.c

xen/common/sysctl.c +3 -0
@@ -357,6 +357,9 @@ long do_sysctl(XEN_GUEST_HANDLE_PARAM(xe
     }
     break;
 
+    case XEN_SYSCTL_consoleringsize:
+        ret = console_ring_size(&op->u.consoleringsize);
+        break;
 
     default:
         ret = arch_do_sysctl(op, u_sysctl);

diff -r 66f563be41d9 -r 1306e69a7018 xen/drivers/char/console.c

xen/drivers/char/console.c +6 -0
@@ -226,6 +226,12 @@ long read_console_ring(struct xen_sysctl
     return 0;
 }
 
+long console_ring_size(struct xen_sysctl_consoleringsize * op)
+{
+    op->size = conring_size;
+    return 0;
+}
+
 
 /*
  * *******************************************************

diff -r 66f563be41d9 -r 1306e69a7018 xen/include/public/sysctl.h

xen/include/public/sysctl.h struct xen_sysctl_scheduler_op { · line 602

Quoted 7 lines
xen/include/public/sysctl.h +4 -0
@@ -596,6 +596,14 @@ struct xen_sysctl_scheduler_op {
 typedef struct xen_sysctl_scheduler_op xen_sysctl_scheduler_op_t;
 DEFINE_XEN_GUEST_HANDLE(xen_sysctl_scheduler_op_t);
 
+/* XEN_SYSCTL_consoleringsize */
+/* Get the size of the hypervisor console ring. */
+struct xen_sysctl_consoleringsize {
+    uint64_t size;

Is this bytes? Because even 2^32 is a hell of a lot of logging... I suppose you could say it was future-proof ;-)

Quoted 3 lines

xen/include/public/sysctl.h struct xen_sysctl_scheduler_op { · line 603

Quoted 1 line
xen/include/public/sysctl.h +0 -0
 /* Written by Xen. */

The norm in the Xen headers seems to be /* OUT */ above the output variables.

Ok - I will make these changes. I am going to have to rebase this series back on top of unstable anyway given the latest churn.

~Andrew

Quoted 37 lines
Quoted 36 lines
xen/include/public/sysctl.h +4 -0
+};
+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

xen/include/public/sysctl.h +2 -0
@@ -616,6 +624,7 @@ struct xen_sysctl {
 #define XEN_SYSCTL_numainfo                      17
 #define XEN_SYSCTL_cpupool_op                    18
 #define XEN_SYSCTL_scheduler_op                  19
+#define XEN_SYSCTL_consoleringsize               20
     uint32_t interface_version; /* XEN_SYSCTL_INTERFACE_VERSION */
     union {
         struct xen_sysctl_readconsole       readconsole;
@@ -636,6 +645,7 @@ struct xen_sysctl {
         struct xen_sysctl_lockprof_op       lockprof_op;
         struct xen_sysctl_cpupool_op        cpupool_op;
         struct xen_sysctl_scheduler_op      scheduler_op;
+        struct xen_sysctl_consoleringsize   consoleringsize;
         uint8_t                             pad[128];
     } u;
 };

diff -r 66f563be41d9 -r 1306e69a7018 xen/include/xen/console.h

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);
#14 lore raw

Re: [PATCH 3 of 5 v2] tools/libxc: Implement xc_readconsolering_buffer()

In reply to a message not in the archive
Quoted 13 lines · Andrew Cooper

Functions identically to xc_readconsolering(), but uses a user-provided xc_hypercall_buffer_t to save using a bounce buffer.

--
Changes since v1:
* Reduce xc_readconsolering() to use xc_readconsolering_buffer()

diff -r 1ffce9bf90f5 -r 1e6c7f7cec6f tools/libxc/xc_misc.c

tools/libxc/xc_misc.c int xc_readconsolering(xc_interface *xch · line 73

Quoted 7 lines
tools/libxc/xc_misc.c +1 -3
@@ -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);

While you are hear this could really use a comment (in the header) that *pnr_chars is both an input (buffer size) and an output. Same is true of the new function I think?
[...]

Quoted 14 lines

diff -r 1ffce9bf90f5 -r 1e6c7f7cec6f tools/libxc/xenctrl.h

tools/libxc/xenctrl.h +4 -0
@@ -998,6 +998,10 @@ 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);
#16 lore raw

Re: [PATCH 4 of 5 v2] tools/ocaml: libxc bindings: Fix stub_xc_readconsolering()

In reply to a message not in the archive
Quoted 26 lines · Andrew Cooper

There 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 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()

diff -r 1e6c7f7cec6f -r 1774a72fde4a tools/ocaml/libs/xc/xenctrl_stubs.c

tools/ocaml/libs/xc/xenctrl_stubs.c +0 -1
@@ -523,27 +523,50 @@ CAMLprim value stub_xc_evtchn_reset(valu
 	CAMLreturn(Val_unit);
 }
 
-

tools/ocaml/libs/xc/xenctrl_stubs.c CAMLprim value stub_xc_evtchn_reset(valu · line 529

Quoted 10 lines
tools/ocaml/libs/xc/xenctrl_stubs.c +2 -6
-#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;
-	int retval;
+	static int have_conring_size = 0;
+	static uint64_t conring_size;

Checking if conring_size == 0 (or ~0 if you prefer) is functionally equivalent to the additional variable I think.

tools/ocaml/libs/xc/xenctrl_stubs.c CAMLprim value stub_xc_evtchn_reset(valu · line 538

Quoted 9 lines
tools/ocaml/libs/xc/xenctrl_stubs.c +7 -0
+
+	unsigned int nr_chars;
+	DECLARE_HYPERCALL_BUFFER(char, ring);
+	int r;
 
 	CAMLparam1(xch);
+	CAMLlocal1(conring);
+
+	if ( ! have_conring_size )

This overuse of spaces is neither Xen style nor the prevailing style in this file AFAICT. The prevailing style appears to be Linux-ish, I suggest to stick with that.

tools/ocaml/libs/xc/xenctrl_stubs.c CAMLprim value stub_xc_evtchn_reset(valu · line 546

Quoted 8 lines
tools/ocaml/libs/xc/xenctrl_stubs.c +8 -0
+	{
+		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;
+		have_conring_size = 0;

If you do want to do things this way then surely this has to be = 1 ?

Quoted 6 lines
tools/ocaml/libs/xc/xenctrl_stubs.c +6 -0
+	}
+
+	nr_chars = conring_size-1;
+	ring = xc_hypercall_buffer_alloc(_H(xch), ring, conring_size);
+
+	if ( ! ring )

tools/ocaml/libs/xc/xenctrl_stubs.c CAMLprim value stub_xc_evtchn_reset(valu · line 560

Quoted 10 lines
tools/ocaml/libs/xc/xenctrl_stubs.c +4 -2
+		caml_raise_out_of_memory();
 
 	caml_enter_blocking_section();
-	retval = xc_readconsolering(_H(xch), ring_ptr, &size, 0, 0, NULL);
+	r = xc_readconsolering_buffer(_H(xch), HYPERCALL_BUFFER(ring),
+				      &nr_chars, 0, 0, NULL);
 	caml_leave_blocking_section();
 
-	if (retval)
+	if ( r )

The s/retval/r/ seems a bit unneeded.

Quoted 14 lines
tools/ocaml/libs/xc/xenctrl_stubs.c +7 -2
+	{
+		xc_hypercall_buffer_free(_H(xch), ring);
 		failwith_xc(_H(xch));
+	}
 
-	ring[size] = '\0';
-	CAMLreturn(caml_copy_string(ring));
+	ring[nr_chars] = '\0';
+	conring = caml_copy_string(ring);
+	xc_hypercall_buffer_free(_H(xch), ring);
+	CAMLreturn(conring);
 }
 
 CAMLprim value stub_xc_send_debug_keys(value xch, value keys)
#17 lore raw

Re: [PATCH 0 of 5 v2] Tools: Console ring improvements

In reply to a message not in the archive
Quoted 4 lines · Andrew Cooper

Hello,

The following set of patches came about when trying to fix the 32K limitation with stub_xc_readconsolering() in the ocaml bindings.

Anyone on xen-api@ want to comment on the ocaml binding aspects of this series?

[...]

Quoted 1 line

CC'd is [email protected] as well, for comment on the ocaml patches.

Ian.