Skip to contentSkip to conversation

xen: optimize xenbus performance

PATCH Superseded Juergen Gross 5 participants 35 messages Jan 6, 2017 – Jan 11, 2017

Subsystems (6)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Juergen Gross

author maintainer
#0 lore raw

cover xen: optimize xenbus performance

The xenbus driver used for communication with Xenstore (all kernel accesses to Xenstore and in case of Xenstore living in another domain all accesses of the local domain to Xenstore) is rather simple especially regarding multiple concurrent accesses: they are just being serialized in spite of Xenstore being capable to handle multiple parallel accesses.

Clean up the external interface(s) of xenbus and optimize its performance by allowing multiple concurrent accesses to Xenstore.

Juergen Gross (3):
xen: clean up xenbus internal headers
xen: modify xenstore watch event interface
xen: optimize xenbus driver for multiple concurrent xenstore accesses

 drivers/block/xen-blkback/xenbus.c         |   6 +-
 drivers/net/xen-netback/xenbus.c           |   8 +-
 drivers/xen/cpu_hotplug.c                  |   5 +-
 drivers/xen/manage.c                       |   6 +-
 drivers/xen/xen-balloon.c                  |   2 +-
 drivers/xen/xen-pciback/xenbus.c           |   2 +-
 drivers/xen/xenbus/xenbus.h                | 134 ++++++++
 drivers/xen/xenbus/xenbus_client.c         |   6 +-
 drivers/xen/xenbus/xenbus_comms.c          | 319 +++++++++++++++--
 drivers/xen/xenbus/xenbus_comms.h          |  51 ---
 drivers/xen/xenbus/xenbus_dev_backend.c    |   2 +-
 drivers/xen/xenbus/xenbus_dev_frontend.c   | 213 +++++++-----
 drivers/xen/xenbus/xenbus_probe.c          |  14 +-
 drivers/xen/xenbus/xenbus_probe.h          |  88 -----
 drivers/xen/xenbus/xenbus_probe_backend.c  |  11 +-
 drivers/xen/xenbus/xenbus_probe_frontend.c |  17 +-
 drivers/xen/xenbus/xenbus_xs.c             | 535 +++++++++++++----------------
 drivers/xen/xenfs/super.c                  |   2 +-
 drivers/xen/xenfs/xenstored.c              |   2 +-
 include/xen/xenbus.h                       |  18 +-
 20 files changed, 830 insertions(+), 611 deletions(-)
 create mode 100644 drivers/xen/xenbus/xenbus.h
 delete mode 100644 drivers/xen/xenbus/xenbus_comms.h
 delete mode 100644 drivers/xen/xenbus/xenbus_probe.h
Signature · 7 lines
2.10.2


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
author maintainer
#5 lore raw

PATCH 2/3 xen: modify xenstore watch event interface

Today a Xenstore watch event is delivered via a callback function declared as:

void (*callback)(struct xenbus_watch *,

                 const char **vec, unsigned int len);

As all watch events only ever come with two parameters (path and token) changing the prototype to:

void (*callback)(struct xenbus_watch *,

                 const char *path, const char *token);

is the natural thing to do.

Apply this change and adapt all users.

---
 drivers/block/xen-blkback/xenbus.c         |  6 +++---
 drivers/net/xen-netback/xenbus.c           |  8 ++++----
 drivers/xen/cpu_hotplug.c                  |  5 ++---
 drivers/xen/manage.c                       |  6 +++---
 drivers/xen/xen-balloon.c                  |  2 +-
 drivers/xen/xen-pciback/xenbus.c           |  2 +-
 drivers/xen/xenbus/xenbus.h                |  6 +++---
 drivers/xen/xenbus/xenbus_client.c         |  4 ++--
 drivers/xen/xenbus/xenbus_dev_frontend.c   | 21 ++++++++-------------
 drivers/xen/xenbus/xenbus_probe.c          | 11 ++++-------
 drivers/xen/xenbus/xenbus_probe_backend.c  |  8 ++++----
 drivers/xen/xenbus/xenbus_probe_frontend.c | 14 +++++++-------
 drivers/xen/xenbus/xenbus_xs.c             | 29 ++++++++++++++---------------
 include/xen/xenbus.h                       |  6 +++---
 14 files changed, 59 insertions(+), 69 deletions(-)
drivers/block/xen-blkback/xenbus.c +3 -3
index 415e79b..8fe61b5 100644
@@ -38,8 +38,8 @@ struct backend_info {
 static struct kmem_cache *xen_blkif_cachep;
 static void connect(struct backend_info *);
 static int connect_ring(struct backend_info *);
-static void backend_changed(struct xenbus_watch *, const char **,
-			    unsigned int);
+static void backend_changed(struct xenbus_watch *, const char *,
+			    const char *);
 static void xen_blkif_free(struct xen_blkif *blkif);
 static void xen_vbd_free(struct xen_vbd *vbd);
 
@@ -661,7 +661,7 @@ static int xen_blkbk_probe(struct xenbus_device *dev,
  * ready, connect.
  */
 static void backend_changed(struct xenbus_watch *watch,
-			    const char **vec, unsigned int len)
+			    const char *path, const char *token)
 {
 	int err;
 	unsigned major;
drivers/net/xen-netback/xenbus.c +4 -4
index 3124eae..d8a40fa 100644
@@ -723,7 +723,7 @@ static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
 }
 
 static void xen_net_rate_changed(struct xenbus_watch *watch,
-				const char **vec, unsigned int len)
+				 const char *path, const char *token)
 {
 	struct xenvif *vif = container_of(watch, struct xenvif, credit_watch);
 	struct xenbus_device *dev = xenvif_to_xenbus_device(vif);
@@ -780,7 +780,7 @@ static void xen_unregister_credit_watch(struct xenvif *vif)
 }
 
 static void xen_mcast_ctrl_changed(struct xenbus_watch *watch,
-				   const char **vec, unsigned int len)
+				   const char *path, const char *token)
 {
 	struct xenvif *vif = container_of(watch, struct xenvif,
 					  mcast_ctrl_watch);
@@ -855,8 +855,8 @@ static void unregister_hotplug_status_watch(struct backend_info *be)
 }
 
 static void hotplug_status_changed(struct xenbus_watch *watch,
-				   const char **vec,
-				   unsigned int vec_size)
+				   const char *path,
+				   const char *token)
 {
 	struct backend_info *be = container_of(watch,
 					       struct backend_info,
drivers/xen/cpu_hotplug.c +2 -3
index 5676aef..7a4daa2 100644
@@ -68,13 +68,12 @@ static void vcpu_hotplug(unsigned int cpu)
 }
 
 static void handle_vcpu_hotplug_event(struct xenbus_watch *watch,
-					const char **vec, unsigned int len)
+				      const char *path, const char *token)
 {
 	unsigned int cpu;
 	char *cpustr;
-	const char *node = vec[XS_WATCH_PATH];
 
-	cpustr = strstr(node, "cpu/");
+	cpustr = strstr(path, "cpu/");
 	if (cpustr != NULL) {
 		sscanf(cpustr, "cpu/%u", &cpu);
 		vcpu_hotplug(cpu);
drivers/xen/manage.c +3 -3
index 26e5e85..ca62c09 100644
@@ -218,7 +218,7 @@ static struct shutdown_handler shutdown_handlers[] = {
 };
 
 static void shutdown_handler(struct xenbus_watch *watch,
-			     const char **vec, unsigned int len)
+			     const char *path, const char *token)
 {
 	char *str;
 	struct xenbus_transaction xbt;
@@ -266,8 +266,8 @@ static void shutdown_handler(struct xenbus_watch *watch,
 }
 
 #ifdef CONFIG_MAGIC_SYSRQ
-static void sysrq_handler(struct xenbus_watch *watch, const char **vec,
-			  unsigned int len)
+static void sysrq_handler(struct xenbus_watch *watch, const char *path,
+			  const char *token)
 {
 	char sysrq_key = '\0';
 	struct xenbus_transaction xbt;
drivers/xen/xen-balloon.c +1 -1
index 79865b8..e7715cb 100644
@@ -55,7 +55,7 @@ static int register_balloon(struct device *dev);
 
 /* React to a change in the target key */
 static void watch_target(struct xenbus_watch *watch,
-			 const char **vec, unsigned int len)
+			 const char *path, const char *token)
 {
 	unsigned long long new_target;
 	int err;
drivers/xen/xen-pciback/xenbus.c +1 -1
index 3f0aee0..3814b44 100644
@@ -652,7 +652,7 @@ static int xen_pcibk_setup_backend(struct xen_pcibk_device *pdev)
 }
 
 static void xen_pcibk_be_watch(struct xenbus_watch *watch,
-			     const char **vec, unsigned int len)
+			       const char *path, const char *token)
 {
 	struct xen_pcibk_device *pdev =
 	    container_of(watch, struct xen_pcibk_device, be_watch);
drivers/xen/xenbus/xenbus.h +3 -3
index 6a80c1e..bd95c21 100644
@@ -39,8 +39,8 @@ struct xen_bus_type {
 	int (*get_bus_id)(char bus_id[XEN_BUS_ID_SIZE], const char *nodename);
 	int (*probe)(struct xen_bus_type *bus, const char *type,
 		     const char *dir);
-	void (*otherend_changed)(struct xenbus_watch *watch, const char **vec,
-				 unsigned int len);
+	void (*otherend_changed)(struct xenbus_watch *watch, const char *path,
+				 const char *token);
 	struct bus_type bus;
 };
 
@@ -83,7 +83,7 @@ int xenbus_dev_resume(struct device *dev);
 int xenbus_dev_cancel(struct device *dev);
 
 void xenbus_otherend_changed(struct xenbus_watch *watch,
-			     const char **vec, unsigned int len,
+			     const char *path, const char *token,
 			     int ignore_on_shutdown);
 
 int xenbus_read_otherend_details(struct xenbus_device *xendev,
drivers/xen/xenbus/xenbus_client.c +2 -2
index 23edf53..9586c24 100644
@@ -115,7 +115,7 @@ EXPORT_SYMBOL_GPL(xenbus_strstate);
 int xenbus_watch_path(struct xenbus_device *dev, const char *path,
 		      struct xenbus_watch *watch,
 		      void (*callback)(struct xenbus_watch *,
-				       const char **, unsigned int))
+				       const char *, const char *))
 {
 	int err;
 
@@ -153,7 +153,7 @@ EXPORT_SYMBOL_GPL(xenbus_watch_path);
 int xenbus_watch_pathfmt(struct xenbus_device *dev,
 			 struct xenbus_watch *watch,
 			 void (*callback)(struct xenbus_watch *,
-					const char **, unsigned int),
+					  const char *, const char *),
 			 const char *pathfmt, ...)
 {
 	int err;
drivers/xen/xenbus/xenbus_dev_frontend.c +8 -13
index e2bc9b3..e4b9847 100644
@@ -258,26 +258,23 @@ static struct watch_adapter *alloc_watch_adapter(const char *path,
 }
 
 static void watch_fired(struct xenbus_watch *watch,
-			const char **vec,
-			unsigned int len)
+			const char *path,
+			const char *token)
 {
 	struct watch_adapter *adap;
 	struct xsd_sockmsg hdr;
-	const char *path, *token;
-	int path_len, tok_len, body_len, data_len = 0;
+	const char *token_caller;
+	int path_len, tok_len, body_len;
 	int ret;
 	LIST_HEAD(staging_q);
 
 	adap = container_of(watch, struct watch_adapter, watch);
 
-	path = vec[XS_WATCH_PATH];
-	token = adap->token;
+	token_caller = adap->token;
 
 	path_len = strlen(path) + 1;
-	tok_len = strlen(token) + 1;
-	if (len > 2)
-		data_len = vec[len] - vec[2] + 1;
-	body_len = path_len + tok_len + data_len;
+	tok_len = strlen(token_caller) + 1;
+	body_len = path_len + tok_len;
 
 	hdr.type = XS_WATCH_EVENT;
 	hdr.len = body_len;
@@ -288,9 +285,7 @@ static void watch_fired(struct xenbus_watch *watch,
 	if (!ret)
 		ret = queue_reply(&staging_q, path, path_len);
 	if (!ret)
-		ret = queue_reply(&staging_q, token, tok_len);
-	if (!ret && len > 2)
-		ret = queue_reply(&staging_q, vec[2], data_len);
+		ret = queue_reply(&staging_q, token_caller, tok_len);
 
 	if (!ret) {
 		/* success: pass reply list onto watcher */
drivers/xen/xenbus/xenbus_probe.c +4 -7
index 6baffbb..74888ca 100644
@@ -169,7 +169,7 @@ int xenbus_read_otherend_details(struct xenbus_device *xendev,
 EXPORT_SYMBOL_GPL(xenbus_read_otherend_details);
 
 void xenbus_otherend_changed(struct xenbus_watch *watch,
-			     const char **vec, unsigned int len,
+			     const char *path, const char *token,
 			     int ignore_on_shutdown)
 {
 	struct xenbus_device *dev =
@@ -180,18 +180,15 @@ void xenbus_otherend_changed(struct xenbus_watch *watch,
 	/* Protect us against watches firing on old details when the otherend
 	   details change, say immediately after a resume. */
 	if (!dev->otherend ||
-	    strncmp(dev->otherend, vec[XS_WATCH_PATH],
-		    strlen(dev->otherend))) {
-		dev_dbg(&dev->dev, "Ignoring watch at %s\n",
-			vec[XS_WATCH_PATH]);
+	    strncmp(dev->otherend, path, strlen(dev->otherend))) {
+		dev_dbg(&dev->dev, "Ignoring watch at %s\n", path);
 		return;
 	}
 
 	state = xenbus_read_driver_state(dev->otherend);
 
 	dev_dbg(&dev->dev, "state is %d, (%s), %s, %s\n",
-		state, xenbus_strstate(state), dev->otherend_watch.node,
-		vec[XS_WATCH_PATH]);
+		state, xenbus_strstate(state), dev->otherend_watch.node, path);
 
 	/*
 	 * Ignore xenbus transitions during shutdown. This prevents us doing
drivers/xen/xenbus/xenbus_probe_backend.c +4 -4
index f46b4dc..b0bed4f 100644
@@ -181,9 +181,9 @@ static int xenbus_probe_backend(struct xen_bus_type *bus, const char *type,
 }
 
 static void frontend_changed(struct xenbus_watch *watch,
-			    const char **vec, unsigned int len)
+			     const char *path, const char *token)
 {
-	xenbus_otherend_changed(watch, vec, len, 0);
+	xenbus_otherend_changed(watch, path, token, 0);
 }
 
 static struct xen_bus_type xenbus_backend = {
@@ -204,11 +204,11 @@ static struct xen_bus_type xenbus_backend = {
 };
 
 static void backend_changed(struct xenbus_watch *watch,
-			    const char **vec, unsigned int len)
+			    const char *path, const char *token)
 {
 	DPRINTK("");
 
-	xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_backend);
+	xenbus_dev_changed(path, &xenbus_backend);
 }
 
 static struct xenbus_watch be_watch = {
drivers/xen/xenbus/xenbus_probe_frontend.c +7 -7
index d7b77a6..19e45ce 100644
@@ -86,9 +86,9 @@ static int xenbus_uevent_frontend(struct device *_dev,
 
 
 static void backend_changed(struct xenbus_watch *watch,
-			    const char **vec, unsigned int len)
+			    const char *path, const char *token)
 {
-	xenbus_otherend_changed(watch, vec, len, 1);
+	xenbus_otherend_changed(watch, path, token, 1);
 }
 
 static void xenbus_frontend_delayed_resume(struct work_struct *w)
@@ -153,11 +153,11 @@ static struct xen_bus_type xenbus_frontend = {
 };
 
 static void frontend_changed(struct xenbus_watch *watch,
-			     const char **vec, unsigned int len)
+			     const char *path, const char *token)
 {
 	DPRINTK("");
 
-	xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
+	xenbus_dev_changed(path, &xenbus_frontend);
 }
 
 
@@ -332,13 +332,13 @@ static DECLARE_WAIT_QUEUE_HEAD(backend_state_wq);
 static int backend_state;
 
 static void xenbus_reset_backend_state_changed(struct xenbus_watch *w,
-					const char **v, unsigned int l)
+					const char *path, const char *token)
 {
-	if (xenbus_scanf(XBT_NIL, v[XS_WATCH_PATH], "", "%i",
+	if (xenbus_scanf(XBT_NIL, path, "", "%i",
 			 &backend_state) != 1)
 		backend_state = XenbusStateUnknown;
 	printk(KERN_DEBUG "XENBUS: backend %s %s\n",
-			v[XS_WATCH_PATH], xenbus_strstate(backend_state));
+	       path, xenbus_strstate(backend_state));
 	wake_up(&backend_state_wq);
 }
 
drivers/xen/xenbus/xenbus_xs.c +14 -15
index 4c49d87..ebc768f 100644
@@ -64,8 +64,8 @@ struct xs_stored_msg {
 		/* Queued watch events. */
 		struct {
 			struct xenbus_watch *handle;
-			char **vec;
-			unsigned int vec_size;
+			const char *path;
+			const char *token;
 		} watch;
 	} u;
 };
@@ -765,7 +765,7 @@ void unregister_xenbus_watch(struct xenbus_watch *watch)
 		if (msg->u.watch.handle != watch)
 			continue;
 		list_del(&msg->list);
-		kfree(msg->u.watch.vec);
+		kfree(msg->u.watch.path);
 		kfree(msg);
 	}
 	spin_unlock(&watch_events_lock);
@@ -833,11 +833,10 @@ static int xenwatch_thread(void *unused)
 
 		if (ent != &watch_events) {
 			msg = list_entry(ent, struct xs_stored_msg, list);
-			msg->u.watch.handle->callback(
-				msg->u.watch.handle,
-				(const char **)msg->u.watch.vec,
-				msg->u.watch.vec_size);
-			kfree(msg->u.watch.vec);
+			msg->u.watch.handle->callback(msg->u.watch.handle,
+						      msg->u.watch.path,
+						      msg->u.watch.token);
+			kfree(msg->u.watch.path);
 			kfree(msg);
 		}
 
@@ -903,24 +902,24 @@ static int process_msg(void)
 	body[msg->hdr.len] = '\0';
 
 	if (msg->hdr.type == XS_WATCH_EVENT) {
-		msg->u.watch.vec = split(body, msg->hdr.len,
-					 &msg->u.watch.vec_size);
-		if (IS_ERR(msg->u.watch.vec)) {
-			err = PTR_ERR(msg->u.watch.vec);
+		if (count_strings(body, msg->hdr.len) != 2) {
+			err = -EINVAL;
 			kfree(msg);
+			kfree(body);
 			goto out;
 		}
+		msg->u.watch.path = (const char *)body;
+		msg->u.watch.token = (const char *)strchr(body, '\0') + 1;
 
 		spin_lock(&watches_lock);
-		msg->u.watch.handle = find_watch(
-			msg->u.watch.vec[XS_WATCH_TOKEN]);
+		msg->u.watch.handle = find_watch(msg->u.watch.token);
 		if (msg->u.watch.handle != NULL) {
 			spin_lock(&watch_events_lock);
 			list_add_tail(&msg->list, &watch_events);
 			wake_up(&watch_events_waitq);
 			spin_unlock(&watch_events_lock);
 		} else {
-			kfree(msg->u.watch.vec);
+			kfree(body);
 			kfree(msg);
 		}
 		spin_unlock(&watches_lock);
include/xen/xenbus.h +3 -3
index 98f73a2..869c816 100644
@@ -61,7 +61,7 @@ struct xenbus_watch
 
 	/* Callback (executed in a process context with no locks held). */
 	void (*callback)(struct xenbus_watch *,
-			 const char **vec, unsigned int len);
+			 const char *path, const char *token);
 };
 
 
@@ -193,11 +193,11 @@ void xenbus_probe(struct work_struct *);
 int xenbus_watch_path(struct xenbus_device *dev, const char *path,
 		      struct xenbus_watch *watch,
 		      void (*callback)(struct xenbus_watch *,
-				       const char **, unsigned int));
+				       const char *, const char *));
 __printf(4, 5)
 int xenbus_watch_pathfmt(struct xenbus_device *dev, struct xenbus_watch *watch,
 			 void (*callback)(struct xenbus_watch *,
-					  const char **, unsigned int),
+					  const char *, const char *),
 			 const char *pathfmt, ...);
 
 int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state new_state);
Signature · 7 lines
2.10.2


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
author maintainer
#1 lore raw

PATCH 1/3 xen: clean up xenbus internal headers

In reply to Juergen Gross

The xenbus driver has an awful mixture of internal and global visible
headers: some of the internal used only stuff is defined in the global header include/xen/xenbus.h while some stuff defined in internal headers is used by other drivers, too.

Clean this up by moving the external used symbols to include/xen/xenbus.h and the symbols used internal only to a new header drivers/xen/xenbus/xenbus.h

---
 drivers/xen/xenbus/{xenbus_probe.h => xenbus.h} | 64 ++++++++++++++-----------
 drivers/xen/xenbus/xenbus_client.c              |  2 +-
 drivers/xen/xenbus/xenbus_comms.c               |  2 +-
 drivers/xen/xenbus/xenbus_comms.h               | 51 --------------------
 drivers/xen/xenbus/xenbus_dev_backend.c         |  2 +-
 drivers/xen/xenbus/xenbus_dev_frontend.c        |  4 +-
 drivers/xen/xenbus/xenbus_probe.c               |  3 +-
 drivers/xen/xenbus/xenbus_probe_backend.c       |  3 +-
 drivers/xen/xenbus/xenbus_probe_frontend.c      |  3 +-
 drivers/xen/xenbus/xenbus_xs.c                  |  3 +-
 drivers/xen/xenfs/super.c                       |  2 +-
 drivers/xen/xenfs/xenstored.c                   |  2 +-
 include/xen/xenbus.h                            | 12 ++---
 13 files changed, 52 insertions(+), 101 deletions(-)
 rename drivers/xen/xenbus/{xenbus_probe.h => xenbus.h} (58%)
 delete mode 100644 drivers/xen/xenbus/xenbus_comms.h
drivers/xen/xenbus/xenbus.h renamed from drivers/xen/xenbus/xenbus_probe.h +36 -28
similarity index 58%
rename from drivers/xen/xenbus/xenbus_probe.h
rename to drivers/xen/xenbus/xenbus.h
index c9ec7ca..6a80c1e 100644
@@ -1,10 +1,7 @@
-/******************************************************************************
- * xenbus_probe.h
- *
- * Talks to Xen Store to figure out what devices we have.
+/*
+ * Private include for xenbus communications.
  *
  * Copyright (C) 2005 Rusty Russell, IBM Corporation
- * Copyright (C) 2005 XenSource Ltd.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License version 2
@@ -31,8 +28,8 @@
  * IN THE SOFTWARE.
  */
 
-#ifndef _XENBUS_PROBE_H
-#define _XENBUS_PROBE_H
+#ifndef _XENBUS_XENBUS_H
+#define _XENBUS_XENBUS_H
 
 #define XEN_BUS_ID_SIZE			20
 
@@ -54,35 +51,46 @@ enum xenstore_init {
 	XS_LOCAL,
 };
 
+extern enum xenstore_init xen_store_domain_type;
 extern const struct attribute_group *xenbus_dev_groups[];
 
-extern int xenbus_match(struct device *_dev, struct device_driver *_drv);
-extern int xenbus_dev_probe(struct device *_dev);
-extern int xenbus_dev_remove(struct device *_dev);
-extern int xenbus_register_driver_common(struct xenbus_driver *drv,
-					 struct xen_bus_type *bus,
-					 struct module *owner,
-					 const char *mod_name);
-extern int xenbus_probe_node(struct xen_bus_type *bus,
-			     const char *type,
-			     const char *nodename);
-extern int xenbus_probe_devices(struct xen_bus_type *bus);
+int xs_init(void);
+int xb_init_comms(void);
+void xb_deinit_comms(void);
+int xb_write(const void *data, unsigned int len);
+int xb_read(void *data, unsigned int len);
+int xb_data_to_read(void);
+int xb_wait_for_data_to_read(void);
 
-extern void xenbus_dev_changed(const char *node, struct xen_bus_type *bus);
+int xenbus_match(struct device *_dev, struct device_driver *_drv);
+int xenbus_dev_probe(struct device *_dev);
+int xenbus_dev_remove(struct device *_dev);
+int xenbus_register_driver_common(struct xenbus_driver *drv,
+				  struct xen_bus_type *bus,
+				  struct module *owner,
+				  const char *mod_name);
+int xenbus_probe_node(struct xen_bus_type *bus,
+		      const char *type,
+		      const char *nodename);
+int xenbus_probe_devices(struct xen_bus_type *bus);
 
-extern void xenbus_dev_shutdown(struct device *_dev);
+void xenbus_dev_changed(const char *node, struct xen_bus_type *bus);
 
-extern int xenbus_dev_suspend(struct device *dev);
-extern int xenbus_dev_resume(struct device *dev);
-extern int xenbus_dev_cancel(struct device *dev);
+void xenbus_dev_shutdown(struct device *_dev);
 
-extern void xenbus_otherend_changed(struct xenbus_watch *watch,
-				    const char **vec, unsigned int len,
-				    int ignore_on_shutdown);
+int xenbus_dev_suspend(struct device *dev);
+int xenbus_dev_resume(struct device *dev);
+int xenbus_dev_cancel(struct device *dev);
 
-extern int xenbus_read_otherend_details(struct xenbus_device *xendev,
-					char *id_node, char *path_node);
+void xenbus_otherend_changed(struct xenbus_watch *watch,
+			     const char **vec, unsigned int len,
+			     int ignore_on_shutdown);
+
+int xenbus_read_otherend_details(struct xenbus_device *xendev,
+				 char *id_node, char *path_node);
 
 void xenbus_ring_ops_init(void);
 
+void *xenbus_dev_request_and_reply(struct xsd_sockmsg *msg);
+
 #endif
drivers/xen/xenbus/xenbus_client.c +1 -1
index 056da6e..23edf53 100644
@@ -47,7 +47,7 @@
 #include <xen/xen.h>
 #include <xen/features.h>
 
-#include "xenbus_probe.h"
+#include "xenbus.h"
 
 #define XENBUS_PAGES(_grants)	(DIV_ROUND_UP(_grants, XEN_PFN_PER_PAGE))
 
drivers/xen/xenbus/xenbus_comms.c +1 -1
index ecdecce..c21ec02 100644
@@ -40,7 +40,7 @@
 #include <asm/xen/hypervisor.h>
 #include <xen/events.h>
 #include <xen/page.h>
-#include "xenbus_comms.h"
+#include "xenbus.h"
 
 static int xenbus_irq;
 
drivers/xen/xenbus/xenbus_comms.h deleted +0 -51

51 lines not on this page · raw message

drivers/xen/xenbus/xenbus_dev_backend.c +1 -1
index 4a41ac9..1126701 100644
@@ -16,7 +16,7 @@
 #include <xen/events.h>
 #include <asm/xen/hypervisor.h>
 
-#include "xenbus_comms.h"
+#include "xenbus.h"
 
 static int xenbus_backend_open(struct inode *inode, struct file *filp)
 {
drivers/xen/xenbus/xenbus_dev_frontend.c +2 -2
index 79130b3..e2bc9b3 100644
@@ -57,12 +57,12 @@
 #include <linux/miscdevice.h>
 #include <linux/init.h>
 
-#include "xenbus_comms.h"
-
 #include <xen/xenbus.h>
 #include <xen/xen.h>
 #include <asm/xen/hypervisor.h>
 
+#include "xenbus.h"
+
 /*
  * An element of a list of outstanding transactions, for which we're
  * still waiting a reply.
drivers/xen/xenbus/xenbus_probe.c +1 -2
index 4bdf654..6baffbb 100644
@@ -62,8 +62,7 @@
 
 #include <xen/hvm.h>
 
-#include "xenbus_comms.h"
-#include "xenbus_probe.h"
+#include "xenbus.h"
 
 
 int xen_store_evtchn;
drivers/xen/xenbus/xenbus_probe_backend.c +1 -2
index 37929df..f46b4dc 100644
@@ -53,8 +53,7 @@
 #include <xen/xenbus.h>
 #include <xen/features.h>
 
-#include "xenbus_comms.h"
-#include "xenbus_probe.h"
+#include "xenbus.h"
 
 /* backend/<type>/<fe-uuid>/<id> => <type>-<fe-domid>-<id> */
 static int backend_bus_id(char bus_id[XEN_BUS_ID_SIZE], const char *nodename)
drivers/xen/xenbus/xenbus_probe_frontend.c +1 -2
index 6d40a97..d7b77a6 100644
@@ -27,8 +27,7 @@
 
 #include <xen/platform_pci.h>
 
-#include "xenbus_comms.h"
-#include "xenbus_probe.h"
+#include "xenbus.h"
 
 
 
drivers/xen/xenbus/xenbus_xs.c +1 -2
index 6afb993..4c49d87 100644
@@ -48,8 +48,7 @@
 #include <asm/xen/hypervisor.h>
 #include <xen/xenbus.h>
 #include <xen/xen.h>
-#include "xenbus_comms.h"
-#include "xenbus_probe.h"
+#include "xenbus.h"
 
 struct xs_stored_msg {
 	struct list_head list;
drivers/xen/xenfs/super.c +1 -1
index 8559a71..328c398 100644
@@ -16,10 +16,10 @@
 #include <linux/magic.h>
 
 #include <xen/xen.h>
+#include <xen/xenbus.h>
 
 #include "xenfs.h"
 #include "../privcmd.h"
-#include "../xenbus/xenbus_comms.h"
 
 #include <asm/xen/hypervisor.h>
 
drivers/xen/xenfs/xenstored.c +1 -1
index fef20db..82fd2a3 100644
@@ -4,9 +4,9 @@
 #include <linux/fs.h>
 
 #include <xen/page.h>
+#include <xen/xenbus.h>
 
 #include "xenfs.h"
-#include "../xenbus/xenbus_comms.h"
 
 static ssize_t xsd_read(struct file *file, char __user *buf,
 			    size_t size, loff_t *off)
include/xen/xenbus.h +5 -7
index 271ba62..98f73a2 100644
@@ -38,6 +38,7 @@
 #include <linux/notifier.h>
 #include <linux/mutex.h>
 #include <linux/export.h>
+#include <linux/fs.h>
 #include <linux/completion.h>
 #include <linux/init.h>
 #include <linux/slab.h>
@@ -175,16 +176,9 @@ void xs_suspend(void);
 void xs_resume(void);
 void xs_suspend_cancel(void);
 
-/* Used by xenbus_dev to borrow kernel's store connection. */
-void *xenbus_dev_request_and_reply(struct xsd_sockmsg *msg);
-
 struct work_struct;
 
-/* Prepare for domain suspend: then resume or cancel the suspend. */
-void xenbus_suspend(void);
-void xenbus_resume(void);
 void xenbus_probe(struct work_struct *);
-void xenbus_suspend_cancel(void);
 
 #define XENBUS_IS_ERR_READ(str) ({			\
 	if (!IS_ERR(str) && strlen(str) == 0) {		\
@@ -235,4 +229,8 @@ const char *xenbus_strstate(enum xenbus_state state);
 int xenbus_dev_is_online(struct xenbus_device *dev);
 int xenbus_frontend_closed(struct xenbus_device *dev);
 
+extern const struct file_operations xen_xenbus_fops;
+extern struct xenstore_domain_interface *xen_store_interface;
+extern int xen_store_evtchn;
+
 #endif /* _XEN_XENBUS_H */
Signature · 7 lines
2.10.2


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
author maintainer
#15 lore raw

PATCH 3/3 xen: optimize xenbus driver for multiple concurrent xenstore accesses

In reply to Juergen Gross

Handling of multiple concurrent Xenstore accesses through xenbus driver either from the kernel or user land is rather lame today: xenbus is capable to have one access active only at one point of time.

Rewrite xenbus to handle multiple requests concurrently by making use of the request id of the Xenstore protocol. This requires to:

- Instead of blocking inside xb_read() when trying to read data from the xenstore ring buffer do so only in the main loop of xenbus_thread().

- Instead of doing writes to the xenstore ring buffer in the context of the caller just queue the request and do the write in the dedicated xenbus thread.

- Instead of just forwarding the request id specified by the caller of xenbus to xenstore use a xenbus internal unique request id. This will allow multiple outstanding requests.

- Modify the locking scheme in order to allow multiple requests being active in parallel.

- Instead of waiting for the reply of a user's xenstore request after writing the request to the xenstore ring buffer return directly to the caller and do the waiting in the read path.

Additionally signal handling was optimized by avoiding waking up the xenbus thread or sending an event to Xenstore in case the addressed entity is known to be running already.

As a result communication with Xenstore is sped up by a factor of up to 5: depending on the request type (read or write) and the amount of data transferred the gain was at least 20% (small reads) and went up to a factor of 5 for large writes.

In the end some more rough edges of xenbus have been smoothed:

- Handling of memory shortage when reading from xenstore ring buffer in the xenbus driver was not optimal: it was busy looping and issuing a warning in each loop.

- In case of xenstore not running in dom0 but in a stubdom we end up with two xenbus threads running as the initialization of xenbus in dom0 expecting a local xenstored will be redone later when connecting to the xenstore domain. Up to now this was no problem as locking would prevent the two xenbus threads interfering with each other, but this was just a waste of kernel resources.

- An out of memory situation while writing to or reading from the xenstore ring buffer no longer will lead to a possible loss of synchronization with xenstore.

- The user read and write part are now interruptible by signals.

---
I'm aware that the changes are quite large. I thought about sending a version split into multiple patches, but a lot of lines would have been touched by more than one patch. I still have the multiple patch variant lying around - this patch is split into 11 smaller ones. While all steps of this larger series is operational some steps are not optimal as they are even slower than the original version of xenbus.

Nevertheless I can send the large series if there are requests for it.

---
 drivers/xen/xenbus/xenbus.h              |  48 ++-
 drivers/xen/xenbus/xenbus_comms.c        | 317 ++++++++++++++++--
 drivers/xen/xenbus/xenbus_dev_frontend.c | 188 +++++++----
 drivers/xen/xenbus/xenbus_xs.c           | 529 ++++++++++++++-----------------
 4 files changed, 680 insertions(+), 402 deletions(-)
drivers/xen/xenbus/xenbus.h +43 -5
index bd95c21..d693b5f 100644
@@ -31,6 +31,10 @@
 #ifndef _XENBUS_XENBUS_H
 #define _XENBUS_XENBUS_H
 
+#include <linux/mutex.h>
+#include <linux/uio.h>
+#include <xen/xenbus.h>
+
 #define XEN_BUS_ID_SIZE			20
 
 struct xen_bus_type {
@@ -51,16 +55,49 @@ enum xenstore_init {
 	XS_LOCAL,
 };
 
+struct xs_watch_event {
+	struct list_head list;
+	unsigned int len;
+	struct xenbus_watch *handle;
+	const char *path;
+	const char *token;
+	char body[];
+};
+
+enum xb_req_state {
+	xb_req_state_queued,
+	xb_req_state_wait_reply,
+	xb_req_state_got_reply,
+	xb_req_state_aborted
+};
+
+struct xb_req_data {
+	struct list_head list;
+	wait_queue_head_t wq;
+	struct xsd_sockmsg msg;
+	enum xsd_sockmsg_type type;
+	char *body;
+	const struct kvec *vec;
+	int num_vecs;
+	int err;
+	enum xb_req_state state;
+	void (*cb)(struct xb_req_data *);
+	void *par;
+};
+
 extern enum xenstore_init xen_store_domain_type;
 extern const struct attribute_group *xenbus_dev_groups[];
+extern struct mutex xs_response_mutex;
+extern struct list_head xs_reply_list;
+extern struct list_head xb_write_list;
+extern wait_queue_head_t xb_waitq;
+extern struct mutex xb_write_mutex;
 
 int xs_init(void);
 int xb_init_comms(void);
 void xb_deinit_comms(void);
-int xb_write(const void *data, unsigned int len);
-int xb_read(void *data, unsigned int len);
-int xb_data_to_read(void);
-int xb_wait_for_data_to_read(void);
+int xs_watch_msg(struct xs_watch_event *event);
+void xs_request_exit(struct xb_req_data *req);
 
 int xenbus_match(struct device *_dev, struct device_driver *_drv);
 int xenbus_dev_probe(struct device *_dev);
@@ -91,6 +128,7 @@ int xenbus_read_otherend_details(struct xenbus_device *xendev,
 
 void xenbus_ring_ops_init(void);
 
-void *xenbus_dev_request_and_reply(struct xsd_sockmsg *msg);
+int xenbus_dev_request_and_reply(struct xsd_sockmsg *msg, void *par);
+void xenbus_dev_queue_reply(struct xb_req_data *req);
 
 #endif
drivers/xen/xenbus/xenbus_comms.c +283 -34

418 lines not on this page · raw message

drivers/xen/xenbus/xenbus_dev_frontend.c +120 -68

247 lines not on this page · raw message

drivers/xen/xenbus/xenbus_xs.c +234 -295

719 lines not on this page · raw message

Signature · 7 lines
2.10.2


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v1 2/3
#6 lore raw

RE: [PATCH 2/3] xen: modify xenstore watch event interface

In reply to Juergen Gross
Quoted 26 lines

-----Original Message-----
From: Juergen Gross [mailto:[email protected]]
Sent: 06 January 2017 15:06
To: [email protected]; [email protected]
Cc: [email protected]; Juergen Gross <[email protected]>; [email protected]; Roger Pau Monne <[email protected]>; Wei Liu <[email protected]>; Paul Durrant <[email protected]>; [email protected]
Subject: [PATCH 2/3] xen: modify xenstore watch event interface

Today a Xenstore watch event is delivered via a callback function declared as:

void (*callback)(struct xenbus_watch *,

                 const char **vec, unsigned int len);

As all watch events only ever come with two parameters (path and token) changing the prototype to:

void (*callback)(struct xenbus_watch *,

                 const char *path, const char *token);

is the natural thing to do.

Apply this change and adapt all users.

Quoted 7 lines

xen-netback changes...

Quoted 482 lines
---
 drivers/block/xen-blkback/xenbus.c         |  6 +++---
 drivers/net/xen-netback/xenbus.c           |  8 ++++----
 drivers/xen/cpu_hotplug.c                  |  5 ++---
 drivers/xen/manage.c                       |  6 +++---
 drivers/xen/xen-balloon.c                  |  2 +-
 drivers/xen/xen-pciback/xenbus.c           |  2 +-
 drivers/xen/xenbus/xenbus.h                |  6 +++---
 drivers/xen/xenbus/xenbus_client.c         |  4 ++--
 drivers/xen/xenbus/xenbus_dev_frontend.c   | 21 ++++++++-------------
 drivers/xen/xenbus/xenbus_probe.c          | 11 ++++-------
 drivers/xen/xenbus/xenbus_probe_backend.c  |  8 ++++----
 drivers/xen/xenbus/xenbus_probe_frontend.c | 14 +++++++-------
 drivers/xen/xenbus/xenbus_xs.c             | 29 ++++++++++++++---------------
 include/xen/xenbus.h                       |  6 +++---
 14 files changed, 59 insertions(+), 69 deletions(-)
drivers/block/xen- renamed from drivers/block/xen-blkback/xenbus.c +0 -0

blkback/xenbus.c
index 415e79b..8fe61b5 100644

drivers/block/xen-blkback/xenbus.c +3 -3
@@ -38,8 +38,8 @@ struct backend_info {
 static struct kmem_cache *xen_blkif_cachep;
 static void connect(struct backend_info *);
 static int connect_ring(struct backend_info *);
-static void backend_changed(struct xenbus_watch *, const char **,
-			    unsigned int);
+static void backend_changed(struct xenbus_watch *, const char *,
+			    const char *);
 static void xen_blkif_free(struct xen_blkif *blkif);
 static void xen_vbd_free(struct xen_vbd *vbd);
 
@@ -661,7 +661,7 @@ static int xen_blkbk_probe(struct xenbus_device
 *dev,
  * ready, connect.
  */
 static void backend_changed(struct xenbus_watch *watch,
-			    const char **vec, unsigned int len)
+			    const char *path, const char *token)
 {
 	int err;
drivers/block/xen-blkback/xenbus.c +0 -0
 	unsigned major;
drivers/net/xen- renamed from drivers/net/xen-netback/xenbus.c +0 -0

netback/xenbus.c
index 3124eae..d8a40fa 100644

drivers/net/xen-netback/xenbus.c +1 -1
@@ -723,7 +723,7 @@ static int xen_net_read_mac(struct xenbus_device
 *dev, u8 mac[])
 }
 
 static void xen_net_rate_changed(struct xenbus_watch *watch,
-				const char **vec, unsigned int len)
+				 const char *path, const char *token)
 {
 	struct xenvif *vif = container_of(watch, struct xenvif, credit_watch);
drivers/net/xen-netback/xenbus.c +1 -1
 	struct xenbus_device *dev = xenvif_to_xenbus_device(vif);
@@ -780,7 +780,7 @@ static void xen_unregister_credit_watch(struct xenvif
 *vif)
 }
 
 static void xen_mcast_ctrl_changed(struct xenbus_watch *watch,
-				   const char **vec, unsigned int len)
+				   const char *path, const char *token)
 {
 	struct xenvif *vif = container_of(watch, struct xenvif,
drivers/net/xen-netback/xenbus.c +2 -2
 					  mcast_ctrl_watch);
@@ -855,8 +855,8 @@ static void unregister_hotplug_status_watch(struct
 backend_info *be)
 }
 
 static void hotplug_status_changed(struct xenbus_watch *watch,
-				   const char **vec,
-				   unsigned int vec_size)
+				   const char *path,
+				   const char *token)
 {
 	struct backend_info *be = container_of(watch,
drivers/net/xen-netback/xenbus.c +0 -0
 					       struct backend_info,
drivers/xen/cpu_hotplug.c +2 -3
index 5676aef..7a4daa2 100644
@@ -68,13 +68,12 @@ static void vcpu_hotplug(unsigned int cpu)
 }
 
 static void handle_vcpu_hotplug_event(struct xenbus_watch *watch,
-					const char **vec, unsigned int len)
+				      const char *path, const char *token)
 {
 	unsigned int cpu;
 	char *cpustr;
-	const char *node = vec[XS_WATCH_PATH];
 
-	cpustr = strstr(node, "cpu/");
+	cpustr = strstr(path, "cpu/");
 	if (cpustr != NULL) {
 		sscanf(cpustr, "cpu/%u", &cpu);
 		vcpu_hotplug(cpu);
drivers/xen/manage.c +1 -1
index 26e5e85..ca62c09 100644
@@ -218,7 +218,7 @@ static struct shutdown_handler shutdown_handlers[]
 = {
 };
 
 static void shutdown_handler(struct xenbus_watch *watch,
-			     const char **vec, unsigned int len)
+			     const char *path, const char *token)
 {
 	char *str;
drivers/xen/manage.c +2 -2
 	struct xenbus_transaction xbt;
@@ -266,8 +266,8 @@ static void shutdown_handler(struct xenbus_watch
 *watch,
 }
 
 #ifdef CONFIG_MAGIC_SYSRQ
-static void sysrq_handler(struct xenbus_watch *watch, const char **vec,
-			  unsigned int len)
+static void sysrq_handler(struct xenbus_watch *watch, const char *path,
+			  const char *token)
 {
 	char sysrq_key = '\0';
drivers/xen/manage.c +0 -0
 	struct xenbus_transaction xbt;
drivers/xen/xen-balloon.c +1 -1
index 79865b8..e7715cb 100644
@@ -55,7 +55,7 @@ static int register_balloon(struct device *dev);
 
 /* React to a change in the target key */
 static void watch_target(struct xenbus_watch *watch,
-			 const char **vec, unsigned int len)
+			 const char *path, const char *token)
 {
 	unsigned long long new_target;
 	int err;
drivers/xen/xen- renamed from drivers/xen/xen-pciback/xenbus.c +0 -0

pciback/xenbus.c
index 3f0aee0..3814b44 100644

drivers/xen/xen-pciback/xenbus.c +1 -1
@@ -652,7 +652,7 @@ static int xen_pcibk_setup_backend(struct
 xen_pcibk_device *pdev)
 }
 
 static void xen_pcibk_be_watch(struct xenbus_watch *watch,
-			     const char **vec, unsigned int len)
+			       const char *path, const char *token)
 {
 	struct xen_pcibk_device *pdev =
drivers/xen/xen-pciback/xenbus.c +0 -0
 	    container_of(watch, struct xen_pcibk_device, be_watch);
drivers/xen/xenbus/xenbus.h +2 -2
index 6a80c1e..bd95c21 100644
@@ -39,8 +39,8 @@ struct xen_bus_type {
 	int (*get_bus_id)(char bus_id[XEN_BUS_ID_SIZE], const char
 *nodename);
 	int (*probe)(struct xen_bus_type *bus, const char *type,
 		     const char *dir);
-	void (*otherend_changed)(struct xenbus_watch *watch, const char
 **vec,
-				 unsigned int len);
+	void (*otherend_changed)(struct xenbus_watch *watch, const char
 *path,
+				 const char *token);
drivers/xen/xenbus/xenbus.h +1 -1
 	struct bus_type bus;
 };
@@ -83,7 +83,7 @@ int xenbus_dev_resume(struct device *dev);
 int xenbus_dev_cancel(struct device *dev);
 
 void xenbus_otherend_changed(struct xenbus_watch *watch,
-			     const char **vec, unsigned int len,
+			     const char *path, const char *token,
 			     int ignore_on_shutdown);
 
 int xenbus_read_otherend_details(struct xenbus_device *xendev,
drivers/xen/xenbus/xenbus_client.c +2 -2
index 23edf53..9586c24 100644
@@ -115,7 +115,7 @@ EXPORT_SYMBOL_GPL(xenbus_strstate);
 int xenbus_watch_path(struct xenbus_device *dev, const char *path,
 		      struct xenbus_watch *watch,
 		      void (*callback)(struct xenbus_watch *,
-				       const char **, unsigned int))
+				       const char *, const char *))
 {
 	int err;
 
@@ -153,7 +153,7 @@ EXPORT_SYMBOL_GPL(xenbus_watch_path);
 int xenbus_watch_pathfmt(struct xenbus_device *dev,
 			 struct xenbus_watch *watch,
 			 void (*callback)(struct xenbus_watch *,
-					const char **, unsigned int),
+					  const char *, const char *),
 			 const char *pathfmt, ...)
 {
 	int err;
drivers/xen/xenbus/xenbus_dev_frontend.c +7 -10
index e2bc9b3..e4b9847 100644
@@ -258,26 +258,23 @@ static struct watch_adapter
 *alloc_watch_adapter(const char *path,
 }
 
 static void watch_fired(struct xenbus_watch *watch,
-			const char **vec,
-			unsigned int len)
+			const char *path,
+			const char *token)
 {
 	struct watch_adapter *adap;
 	struct xsd_sockmsg hdr;
-	const char *path, *token;
-	int path_len, tok_len, body_len, data_len = 0;
+	const char *token_caller;
+	int path_len, tok_len, body_len;
 	int ret;
 	LIST_HEAD(staging_q);
 
 	adap = container_of(watch, struct watch_adapter, watch);
 
-	path = vec[XS_WATCH_PATH];
-	token = adap->token;
+	token_caller = adap->token;
 
 	path_len = strlen(path) + 1;
-	tok_len = strlen(token) + 1;
-	if (len > 2)
-		data_len = vec[len] - vec[2] + 1;
-	body_len = path_len + tok_len + data_len;
+	tok_len = strlen(token_caller) + 1;
+	body_len = path_len + tok_len;
 
 	hdr.type = XS_WATCH_EVENT;
drivers/xen/xenbus/xenbus_dev_frontend.c +1 -3
 	hdr.len = body_len;
@@ -288,9 +285,7 @@ static void watch_fired(struct xenbus_watch *watch,
 	if (!ret)
 		ret = queue_reply(&staging_q, path, path_len);
 	if (!ret)
-		ret = queue_reply(&staging_q, token, tok_len);
-	if (!ret && len > 2)
-		ret = queue_reply(&staging_q, vec[2], data_len);
+		ret = queue_reply(&staging_q, token_caller, tok_len);
 
 	if (!ret) {
 		/* success: pass reply list onto watcher */
drivers/xen/xenbus/xenbus_probe.c +1 -1
index 6baffbb..74888ca 100644
@@ -169,7 +169,7 @@ int xenbus_read_otherend_details(struct
 xenbus_device *xendev,
 EXPORT_SYMBOL_GPL(xenbus_read_otherend_details);
 
 void xenbus_otherend_changed(struct xenbus_watch *watch,
-			     const char **vec, unsigned int len,
+			     const char *path, const char *token,
 			     int ignore_on_shutdown)
 {
drivers/xen/xenbus/xenbus_probe.c +3 -6
 	struct xenbus_device *dev =
@@ -180,18 +180,15 @@ void xenbus_otherend_changed(struct
 xenbus_watch *watch,
 	/* Protect us against watches firing on old details when the otherend
 	   details change, say immediately after a resume. */
 	if (!dev->otherend ||
-	    strncmp(dev->otherend, vec[XS_WATCH_PATH],
-		    strlen(dev->otherend))) {
-		dev_dbg(&dev->dev, "Ignoring watch at %s\n",
-			vec[XS_WATCH_PATH]);
+	    strncmp(dev->otherend, path, strlen(dev->otherend))) {
+		dev_dbg(&dev->dev, "Ignoring watch at %s\n", path);
 		return;
 	}
 
 	state = xenbus_read_driver_state(dev->otherend);
 
 	dev_dbg(&dev->dev, "state is %d, (%s), %s, %s\n",
-		state, xenbus_strstate(state), dev->otherend_watch.node,
-		vec[XS_WATCH_PATH]);
+		state, xenbus_strstate(state), dev->otherend_watch.node,
 path);
 
drivers/xen/xenbus/xenbus_probe.c +0 -0
 	/*
 	 * Ignore xenbus transitions during shutdown. This prevents us doing
drivers/xen/xenbus/xenbus_probe_backend.c +2 -2
index f46b4dc..b0bed4f 100644
@@ -181,9 +181,9 @@ static int xenbus_probe_backend(struct
 xen_bus_type *bus, const char *type,
 }
 
 static void frontend_changed(struct xenbus_watch *watch,
-			    const char **vec, unsigned int len)
+			     const char *path, const char *token)
 {
-	xenbus_otherend_changed(watch, vec, len, 0);
+	xenbus_otherend_changed(watch, path, token, 0);
 }
 

static struct xen_bus_type xenbus_backend = {

drivers/xen/xenbus/xenbus_probe_backend.c +2 -2
@@ -204,11 +204,11 @@ static struct xen_bus_type xenbus_backend = {
 };
 
 static void backend_changed(struct xenbus_watch *watch,
-			    const char **vec, unsigned int len)
+			    const char *path, const char *token)
 {
 	DPRINTK("");
 
-	xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_backend);
+	xenbus_dev_changed(path, &xenbus_backend);
 }
 
 static struct xenbus_watch be_watch = {
drivers/xen/xenbus/xenbus_probe_frontend.c +7 -7
index d7b77a6..19e45ce 100644
@@ -86,9 +86,9 @@ static int xenbus_uevent_frontend(struct device *_dev,
 
 
 static void backend_changed(struct xenbus_watch *watch,
-			    const char **vec, unsigned int len)
+			    const char *path, const char *token)
 {
-	xenbus_otherend_changed(watch, vec, len, 1);
+	xenbus_otherend_changed(watch, path, token, 1);
 }
 
 static void xenbus_frontend_delayed_resume(struct work_struct *w)
@@ -153,11 +153,11 @@ static struct xen_bus_type xenbus_frontend = {
 };
 
 static void frontend_changed(struct xenbus_watch *watch,
-			     const char **vec, unsigned int len)
+			     const char *path, const char *token)
 {
 	DPRINTK("");
 
-	xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
+	xenbus_dev_changed(path, &xenbus_frontend);
 }
 
 
@@ -332,13 +332,13 @@ static
 DECLARE_WAIT_QUEUE_HEAD(backend_state_wq);
 static int backend_state;
 
 static void xenbus_reset_backend_state_changed(struct xenbus_watch *w,
-					const char **v, unsigned int l)
+					const char *path, const char *token)
 {
-	if (xenbus_scanf(XBT_NIL, v[XS_WATCH_PATH], "", "%i",
+	if (xenbus_scanf(XBT_NIL, path, "", "%i",
 			 &backend_state) != 1)
 		backend_state = XenbusStateUnknown;
 	printk(KERN_DEBUG "XENBUS: backend %s %s\n",
-			v[XS_WATCH_PATH],
 xenbus_strstate(backend_state));
+	       path, xenbus_strstate(backend_state));
 	wake_up(&backend_state_wq);

}

drivers/xen/xenbus/xenbus_xs.c +3 -3
index 4c49d87..ebc768f 100644
@@ -64,8 +64,8 @@ struct xs_stored_msg {
 		/* Queued watch events. */
 		struct {
 			struct xenbus_watch *handle;
-			char **vec;
-			unsigned int vec_size;
+			const char *path;
+			const char *token;
 		} watch;
 	} u;
 };
@@ -765,7 +765,7 @@ void unregister_xenbus_watch(struct xenbus_watch
 *watch)
 		if (msg->u.watch.handle != watch)
 			continue;
 		list_del(&msg->list);
-		kfree(msg->u.watch.vec);
+		kfree(msg->u.watch.path);
 		kfree(msg);
 	}
drivers/xen/xenbus/xenbus_xs.c +11 -12
 	spin_unlock(&watch_events_lock);
@@ -833,11 +833,10 @@ static int xenwatch_thread(void *unused)
 
 		if (ent != &watch_events) {
 			msg = list_entry(ent, struct xs_stored_msg, list);
-			msg->u.watch.handle->callback(
-				msg->u.watch.handle,
-				(const char **)msg->u.watch.vec,
-				msg->u.watch.vec_size);
-			kfree(msg->u.watch.vec);
+			msg->u.watch.handle->callback(msg-
 >u.watch.handle,
+						      msg->u.watch.path,
+						      msg->u.watch.token);
+			kfree(msg->u.watch.path);
 			kfree(msg);
 		}
@@ -903,24 +902,24 @@ static int process_msg(void)
 	body[msg->hdr.len] = '\0';
 
 	if (msg->hdr.type == XS_WATCH_EVENT) {
-		msg->u.watch.vec = split(body, msg->hdr.len,
-					 &msg->u.watch.vec_size);
-		if (IS_ERR(msg->u.watch.vec)) {
-			err = PTR_ERR(msg->u.watch.vec);
+		if (count_strings(body, msg->hdr.len) != 2) {
+			err = -EINVAL;
 			kfree(msg);
+			kfree(body);
 			goto out;
 		}
+		msg->u.watch.path = (const char *)body;
+		msg->u.watch.token = (const char *)strchr(body, '\0') + 1;
 
 		spin_lock(&watches_lock);
-		msg->u.watch.handle = find_watch(
-			msg->u.watch.vec[XS_WATCH_TOKEN]);
+		msg->u.watch.handle = find_watch(msg->u.watch.token);
 		if (msg->u.watch.handle != NULL) {
 			spin_lock(&watch_events_lock);
 			list_add_tail(&msg->list, &watch_events);
 			wake_up(&watch_events_waitq);
 			spin_unlock(&watch_events_lock);
 		} else {
-			kfree(msg->u.watch.vec);
+			kfree(body);
 			kfree(msg);
 		}
 		spin_unlock(&watches_lock);
include/xen/xenbus.h +3 -3
index 98f73a2..869c816 100644
@@ -61,7 +61,7 @@ struct xenbus_watch
 
 	/* Callback (executed in a process context with no locks held). */
 	void (*callback)(struct xenbus_watch *,
-			 const char **vec, unsigned int len);
+			 const char *path, const char *token);
 };
 
 
@@ -193,11 +193,11 @@ void xenbus_probe(struct work_struct *);
 int xenbus_watch_path(struct xenbus_device *dev, const char *path,
 		      struct xenbus_watch *watch,
 		      void (*callback)(struct xenbus_watch *,
-				       const char **, unsigned int));
+				       const char *, const char *));
 __printf(4, 5)
 int xenbus_watch_pathfmt(struct xenbus_device *dev, struct
 xenbus_watch *watch,
 			 void (*callback)(struct xenbus_watch *,
-					  const char **, unsigned int),
+					  const char *, const char *),
 			 const char *pathfmt, ...);
 

int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state
new_state);
--
2.10.2

Reviewed-by on v1 2/3
#7 lore raw

Re: [PATCH 2/3] xen: modify xenstore watch event interface

In reply to Juergen Gross
Quoted 26 lines

-----Original Message-----
From: Juergen Gross [mailto:[email protected]]
Sent: 06 January 2017 15:06
To: [email protected]; [email protected]
Cc: [email protected]; Juergen Gross <[email protected]>; [email protected]; Roger Pau Monne <[email protected]>; Wei Liu <[email protected]>; Paul Durrant <[email protected]>; [email protected]
Subject: [PATCH 2/3] xen: modify xenstore watch event interface

Today a Xenstore watch event is delivered via a callback function declared as:

void (*callback)(struct xenbus_watch *,

                 const char **vec, unsigned int len);

As all watch events only ever come with two parameters (path and token) changing the prototype to:

void (*callback)(struct xenbus_watch *,

                 const char *path, const char *token);

is the natural thing to do.

Apply this change and adapt all users.

Quoted 7 lines

xen-netback changes...

Quoted 478 lines
---
 drivers/block/xen-blkback/xenbus.c         |  6 +++---
 drivers/net/xen-netback/xenbus.c           |  8 ++++----
 drivers/xen/cpu_hotplug.c                  |  5 ++---
 drivers/xen/manage.c                       |  6 +++---
 drivers/xen/xen-balloon.c                  |  2 +-
 drivers/xen/xen-pciback/xenbus.c           |  2 +-
 drivers/xen/xenbus/xenbus.h                |  6 +++---
 drivers/xen/xenbus/xenbus_client.c         |  4 ++--
 drivers/xen/xenbus/xenbus_dev_frontend.c   | 21 ++++++++-------------
 drivers/xen/xenbus/xenbus_probe.c          | 11 ++++-------
 drivers/xen/xenbus/xenbus_probe_backend.c  |  8 ++++----
 drivers/xen/xenbus/xenbus_probe_frontend.c | 14 +++++++-------
 drivers/xen/xenbus/xenbus_xs.c             | 29 ++++++++++++++---------------
 include/xen/xenbus.h                       |  6 +++---
 14 files changed, 59 insertions(+), 69 deletions(-)
drivers/block/xen- renamed from drivers/block/xen-blkback/xenbus.c +0 -0

blkback/xenbus.c
index 415e79b..8fe61b5 100644

drivers/block/xen-blkback/xenbus.c +3 -3
@@ -38,8 +38,8 @@ struct backend_info {
 static struct kmem_cache *xen_blkif_cachep;
 static void connect(struct backend_info *);
 static int connect_ring(struct backend_info *);
-static void backend_changed(struct xenbus_watch *, const char **,
-			    unsigned int);
+static void backend_changed(struct xenbus_watch *, const char *,
+			    const char *);
 static void xen_blkif_free(struct xen_blkif *blkif);
 static void xen_vbd_free(struct xen_vbd *vbd);
 
@@ -661,7 +661,7 @@ static int xen_blkbk_probe(struct xenbus_device
 *dev,
  * ready, connect.
  */
 static void backend_changed(struct xenbus_watch *watch,
-			    const char **vec, unsigned int len)
+			    const char *path, const char *token)
 {
 	int err;
drivers/block/xen-blkback/xenbus.c +0 -0
 	unsigned major;
drivers/net/xen- renamed from drivers/net/xen-netback/xenbus.c +0 -0

netback/xenbus.c
index 3124eae..d8a40fa 100644

drivers/net/xen-netback/xenbus.c +1 -1
@@ -723,7 +723,7 @@ static int xen_net_read_mac(struct xenbus_device
 *dev, u8 mac[])
 }
 
 static void xen_net_rate_changed(struct xenbus_watch *watch,
-				const char **vec, unsigned int len)
+				 const char *path, const char *token)
 {
 	struct xenvif *vif = container_of(watch, struct xenvif, credit_watch);
drivers/net/xen-netback/xenbus.c +1 -1
 	struct xenbus_device *dev = xenvif_to_xenbus_device(vif);
@@ -780,7 +780,7 @@ static void xen_unregister_credit_watch(struct xenvif
 *vif)
 }
 
 static void xen_mcast_ctrl_changed(struct xenbus_watch *watch,
-				   const char **vec, unsigned int len)
+				   const char *path, const char *token)
 {
 	struct xenvif *vif = container_of(watch, struct xenvif,
drivers/net/xen-netback/xenbus.c +2 -2
 					  mcast_ctrl_watch);
@@ -855,8 +855,8 @@ static void unregister_hotplug_status_watch(struct
 backend_info *be)
 }
 
 static void hotplug_status_changed(struct xenbus_watch *watch,
-				   const char **vec,
-				   unsigned int vec_size)
+				   const char *path,
+				   const char *token)
 {
 	struct backend_info *be = container_of(watch,
drivers/net/xen-netback/xenbus.c +0 -0
 					       struct backend_info,
drivers/xen/cpu_hotplug.c +2 -3
index 5676aef..7a4daa2 100644
@@ -68,13 +68,12 @@ static void vcpu_hotplug(unsigned int cpu)
 }
 
 static void handle_vcpu_hotplug_event(struct xenbus_watch *watch,
-					const char **vec, unsigned int len)
+				      const char *path, const char *token)
 {
 	unsigned int cpu;
 	char *cpustr;
-	const char *node = vec[XS_WATCH_PATH];
 
-	cpustr = strstr(node, "cpu/");
+	cpustr = strstr(path, "cpu/");
 	if (cpustr != NULL) {
 		sscanf(cpustr, "cpu/%u", &cpu);
 		vcpu_hotplug(cpu);
drivers/xen/manage.c +1 -1
index 26e5e85..ca62c09 100644
@@ -218,7 +218,7 @@ static struct shutdown_handler shutdown_handlers[]
 = {
 };
 
 static void shutdown_handler(struct xenbus_watch *watch,
-			     const char **vec, unsigned int len)
+			     const char *path, const char *token)
 {
 	char *str;
drivers/xen/manage.c +2 -2
 	struct xenbus_transaction xbt;
@@ -266,8 +266,8 @@ static void shutdown_handler(struct xenbus_watch
 *watch,
 }
 
 #ifdef CONFIG_MAGIC_SYSRQ
-static void sysrq_handler(struct xenbus_watch *watch, const char **vec,
-			  unsigned int len)
+static void sysrq_handler(struct xenbus_watch *watch, const char *path,
+			  const char *token)
 {
 	char sysrq_key = '\0';
drivers/xen/manage.c +0 -0
 	struct xenbus_transaction xbt;
drivers/xen/xen-balloon.c +1 -1
index 79865b8..e7715cb 100644
@@ -55,7 +55,7 @@ static int register_balloon(struct device *dev);
 
 /* React to a change in the target key */
 static void watch_target(struct xenbus_watch *watch,
-			 const char **vec, unsigned int len)
+			 const char *path, const char *token)
 {
 	unsigned long long new_target;
 	int err;
drivers/xen/xen- renamed from drivers/xen/xen-pciback/xenbus.c +0 -0

pciback/xenbus.c
index 3f0aee0..3814b44 100644

drivers/xen/xen-pciback/xenbus.c +1 -1
@@ -652,7 +652,7 @@ static int xen_pcibk_setup_backend(struct
 xen_pcibk_device *pdev)
 }
 
 static void xen_pcibk_be_watch(struct xenbus_watch *watch,
-			     const char **vec, unsigned int len)
+			       const char *path, const char *token)
 {
 	struct xen_pcibk_device *pdev =
drivers/xen/xen-pciback/xenbus.c +0 -0
 	    container_of(watch, struct xen_pcibk_device, be_watch);
drivers/xen/xenbus/xenbus.h +2 -2
index 6a80c1e..bd95c21 100644
@@ -39,8 +39,8 @@ struct xen_bus_type {
 	int (*get_bus_id)(char bus_id[XEN_BUS_ID_SIZE], const char
 *nodename);
 	int (*probe)(struct xen_bus_type *bus, const char *type,
 		     const char *dir);
-	void (*otherend_changed)(struct xenbus_watch *watch, const char
 **vec,
-				 unsigned int len);
+	void (*otherend_changed)(struct xenbus_watch *watch, const char
 *path,
+				 const char *token);
drivers/xen/xenbus/xenbus.h +1 -1
 	struct bus_type bus;
 };
@@ -83,7 +83,7 @@ int xenbus_dev_resume(struct device *dev);
 int xenbus_dev_cancel(struct device *dev);
 
 void xenbus_otherend_changed(struct xenbus_watch *watch,
-			     const char **vec, unsigned int len,
+			     const char *path, const char *token,
 			     int ignore_on_shutdown);
 
 int xenbus_read_otherend_details(struct xenbus_device *xendev,
drivers/xen/xenbus/xenbus_client.c +2 -2
index 23edf53..9586c24 100644
@@ -115,7 +115,7 @@ EXPORT_SYMBOL_GPL(xenbus_strstate);
 int xenbus_watch_path(struct xenbus_device *dev, const char *path,
 		      struct xenbus_watch *watch,
 		      void (*callback)(struct xenbus_watch *,
-				       const char **, unsigned int))
+				       const char *, const char *))
 {
 	int err;
 
@@ -153,7 +153,7 @@ EXPORT_SYMBOL_GPL(xenbus_watch_path);
 int xenbus_watch_pathfmt(struct xenbus_device *dev,
 			 struct xenbus_watch *watch,
 			 void (*callback)(struct xenbus_watch *,
-					const char **, unsigned int),
+					  const char *, const char *),
 			 const char *pathfmt, ...)
 {
 	int err;
drivers/xen/xenbus/xenbus_dev_frontend.c +7 -10
index e2bc9b3..e4b9847 100644
@@ -258,26 +258,23 @@ static struct watch_adapter
 *alloc_watch_adapter(const char *path,
 }
 
 static void watch_fired(struct xenbus_watch *watch,
-			const char **vec,
-			unsigned int len)
+			const char *path,
+			const char *token)
 {
 	struct watch_adapter *adap;
 	struct xsd_sockmsg hdr;
-	const char *path, *token;
-	int path_len, tok_len, body_len, data_len = 0;
+	const char *token_caller;
+	int path_len, tok_len, body_len;
 	int ret;
 	LIST_HEAD(staging_q);
 
 	adap = container_of(watch, struct watch_adapter, watch);
 
-	path = vec[XS_WATCH_PATH];
-	token = adap->token;
+	token_caller = adap->token;
 
 	path_len = strlen(path) + 1;
-	tok_len = strlen(token) + 1;
-	if (len > 2)
-		data_len = vec[len] - vec[2] + 1;
-	body_len = path_len + tok_len + data_len;
+	tok_len = strlen(token_caller) + 1;
+	body_len = path_len + tok_len;
 
 	hdr.type = XS_WATCH_EVENT;
drivers/xen/xenbus/xenbus_dev_frontend.c +1 -3
 	hdr.len = body_len;
@@ -288,9 +285,7 @@ static void watch_fired(struct xenbus_watch *watch,
 	if (!ret)
 		ret = queue_reply(&staging_q, path, path_len);
 	if (!ret)
-		ret = queue_reply(&staging_q, token, tok_len);
-	if (!ret && len > 2)
-		ret = queue_reply(&staging_q, vec[2], data_len);
+		ret = queue_reply(&staging_q, token_caller, tok_len);
 
 	if (!ret) {
 		/* success: pass reply list onto watcher */
drivers/xen/xenbus/xenbus_probe.c +1 -1
index 6baffbb..74888ca 100644
@@ -169,7 +169,7 @@ int xenbus_read_otherend_details(struct
 xenbus_device *xendev,
 EXPORT_SYMBOL_GPL(xenbus_read_otherend_details);
 
 void xenbus_otherend_changed(struct xenbus_watch *watch,
-			     const char **vec, unsigned int len,
+			     const char *path, const char *token,
 			     int ignore_on_shutdown)
 {
drivers/xen/xenbus/xenbus_probe.c +3 -6
 	struct xenbus_device *dev =
@@ -180,18 +180,15 @@ void xenbus_otherend_changed(struct
 xenbus_watch *watch,
 	/* Protect us against watches firing on old details when the otherend
 	   details change, say immediately after a resume. */
 	if (!dev->otherend ||
-	    strncmp(dev->otherend, vec[XS_WATCH_PATH],
-		    strlen(dev->otherend))) {
-		dev_dbg(&dev->dev, "Ignoring watch at %s\n",
-			vec[XS_WATCH_PATH]);
+	    strncmp(dev->otherend, path, strlen(dev->otherend))) {
+		dev_dbg(&dev->dev, "Ignoring watch at %s\n", path);
 		return;
 	}
 
 	state = xenbus_read_driver_state(dev->otherend);
 
 	dev_dbg(&dev->dev, "state is %d, (%s), %s, %s\n",
-		state, xenbus_strstate(state), dev->otherend_watch.node,
-		vec[XS_WATCH_PATH]);
+		state, xenbus_strstate(state), dev->otherend_watch.node,
 path);
 
drivers/xen/xenbus/xenbus_probe.c +0 -0
 	/*
 	 * Ignore xenbus transitions during shutdown. This prevents us doing
drivers/xen/xenbus/xenbus_probe_backend.c +2 -2
index f46b4dc..b0bed4f 100644
@@ -181,9 +181,9 @@ static int xenbus_probe_backend(struct
 xen_bus_type *bus, const char *type,
 }
 
 static void frontend_changed(struct xenbus_watch *watch,
-			    const char **vec, unsigned int len)
+			     const char *path, const char *token)
 {
-	xenbus_otherend_changed(watch, vec, len, 0);
+	xenbus_otherend_changed(watch, path, token, 0);
 }
 

static struct xen_bus_type xenbus_backend = {

drivers/xen/xenbus/xenbus_probe_backend.c +2 -2
@@ -204,11 +204,11 @@ static struct xen_bus_type xenbus_backend = {
 };
 
 static void backend_changed(struct xenbus_watch *watch,
-			    const char **vec, unsigned int len)
+			    const char *path, const char *token)
 {
 	DPRINTK("");
 
-	xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_backend);
+	xenbus_dev_changed(path, &xenbus_backend);
 }
 
 static struct xenbus_watch be_watch = {
drivers/xen/xenbus/xenbus_probe_frontend.c +7 -7
index d7b77a6..19e45ce 100644
@@ -86,9 +86,9 @@ static int xenbus_uevent_frontend(struct device *_dev,
 
 
 static void backend_changed(struct xenbus_watch *watch,
-			    const char **vec, unsigned int len)
+			    const char *path, const char *token)
 {
-	xenbus_otherend_changed(watch, vec, len, 1);
+	xenbus_otherend_changed(watch, path, token, 1);
 }
 
 static void xenbus_frontend_delayed_resume(struct work_struct *w)
@@ -153,11 +153,11 @@ static struct xen_bus_type xenbus_frontend = {
 };
 
 static void frontend_changed(struct xenbus_watch *watch,
-			     const char **vec, unsigned int len)
+			     const char *path, const char *token)
 {
 	DPRINTK("");
 
-	xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
+	xenbus_dev_changed(path, &xenbus_frontend);
 }
 
 
@@ -332,13 +332,13 @@ static
 DECLARE_WAIT_QUEUE_HEAD(backend_state_wq);
 static int backend_state;
 
 static void xenbus_reset_backend_state_changed(struct xenbus_watch *w,
-					const char **v, unsigned int l)
+					const char *path, const char *token)
 {
-	if (xenbus_scanf(XBT_NIL, v[XS_WATCH_PATH], "", "%i",
+	if (xenbus_scanf(XBT_NIL, path, "", "%i",
 			 &backend_state) != 1)
 		backend_state = XenbusStateUnknown;
 	printk(KERN_DEBUG "XENBUS: backend %s %s\n",
-			v[XS_WATCH_PATH],
 xenbus_strstate(backend_state));
+	       path, xenbus_strstate(backend_state));
 	wake_up(&backend_state_wq);

}

drivers/xen/xenbus/xenbus_xs.c +3 -3
index 4c49d87..ebc768f 100644
@@ -64,8 +64,8 @@ struct xs_stored_msg {
 		/* Queued watch events. */
 		struct {
 			struct xenbus_watch *handle;
-			char **vec;
-			unsigned int vec_size;
+			const char *path;
+			const char *token;
 		} watch;
 	} u;
 };
@@ -765,7 +765,7 @@ void unregister_xenbus_watch(struct xenbus_watch
 *watch)
 		if (msg->u.watch.handle != watch)
 			continue;
 		list_del(&msg->list);
-		kfree(msg->u.watch.vec);
+		kfree(msg->u.watch.path);
 		kfree(msg);
 	}
drivers/xen/xenbus/xenbus_xs.c +11 -12
 	spin_unlock(&watch_events_lock);
@@ -833,11 +833,10 @@ static int xenwatch_thread(void *unused)
 
 		if (ent != &watch_events) {
 			msg = list_entry(ent, struct xs_stored_msg, list);
-			msg->u.watch.handle->callback(
-				msg->u.watch.handle,
-				(const char **)msg->u.watch.vec,
-				msg->u.watch.vec_size);
-			kfree(msg->u.watch.vec);
+			msg->u.watch.handle->callback(msg-
 >u.watch.handle,
+						      msg->u.watch.path,
+						      msg->u.watch.token);
+			kfree(msg->u.watch.path);
 			kfree(msg);
 		}
@@ -903,24 +902,24 @@ static int process_msg(void)
 	body[msg->hdr.len] = '\0';
 
 	if (msg->hdr.type == XS_WATCH_EVENT) {
-		msg->u.watch.vec = split(body, msg->hdr.len,
-					 &msg->u.watch.vec_size);
-		if (IS_ERR(msg->u.watch.vec)) {
-			err = PTR_ERR(msg->u.watch.vec);
+		if (count_strings(body, msg->hdr.len) != 2) {
+			err = -EINVAL;
 			kfree(msg);
+			kfree(body);
 			goto out;
 		}
+		msg->u.watch.path = (const char *)body;
+		msg->u.watch.token = (const char *)strchr(body, '\0') + 1;
 
 		spin_lock(&watches_lock);
-		msg->u.watch.handle = find_watch(
-			msg->u.watch.vec[XS_WATCH_TOKEN]);
+		msg->u.watch.handle = find_watch(msg->u.watch.token);
 		if (msg->u.watch.handle != NULL) {
 			spin_lock(&watch_events_lock);
 			list_add_tail(&msg->list, &watch_events);
 			wake_up(&watch_events_waitq);
 			spin_unlock(&watch_events_lock);
 		} else {
-			kfree(msg->u.watch.vec);
+			kfree(body);
 			kfree(msg);
 		}
 		spin_unlock(&watches_lock);
include/xen/xenbus.h +3 -3
index 98f73a2..869c816 100644
@@ -61,7 +61,7 @@ struct xenbus_watch
 
 	/* Callback (executed in a process context with no locks held). */
 	void (*callback)(struct xenbus_watch *,
-			 const char **vec, unsigned int len);
+			 const char *path, const char *token);
 };
 
 
@@ -193,11 +193,11 @@ void xenbus_probe(struct work_struct *);
 int xenbus_watch_path(struct xenbus_device *dev, const char *path,
 		      struct xenbus_watch *watch,
 		      void (*callback)(struct xenbus_watch *,
-				       const char **, unsigned int));
+				       const char *, const char *));
 __printf(4, 5)
 int xenbus_watch_pathfmt(struct xenbus_device *dev, struct
 xenbus_watch *watch,
 			 void (*callback)(struct xenbus_watch *,
-					  const char **, unsigned int),
+					  const char *, const char *),
 			 const char *pathfmt, ...);
 
Quoted 4 lines

int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state
new_state);
--
2.10.2

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

Reviewed-by on v1 2/3
#8 lore raw

Re: [PATCH 2/3] xen: modify xenstore watch event interface

In reply to Juergen Gross
Quoted 16 lines · Juergen Gross

Today a Xenstore watch event is delivered via a callback function declared as:

void (*callback)(struct xenbus_watch *,

                 const char **vec, unsigned int len);

As all watch events only ever come with two parameters (path and token) changing the prototype to:

void (*callback)(struct xenbus_watch *,

                 const char *path, const char *token);

is the natural thing to do.

Apply this change and adapt all users.

Quoted 7 lines
Reviewed-by on v1 2/3
#9 lore raw

Re: [PATCH 2/3] xen: modify xenstore watch event interface

In reply to Juergen Gross
Quoted 16 lines · Juergen Gross

Today a Xenstore watch event is delivered via a callback function declared as:

void (*callback)(struct xenbus_watch *,

                 const char **vec, unsigned int len);

As all watch events only ever come with two parameters (path and token) changing the prototype to:

void (*callback)(struct xenbus_watch *,

                 const char *path, const char *token);

is the natural thing to do.

Apply this change and adapt all users.

Quoted 7 lines

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

Reviewed-by on v1 2/3
#10 lore raw

Re: [PATCH 2/3] xen: modify xenstore watch event interface

In reply to Juergen Gross
Quoted 16 lines · Juergen Gross

Today a Xenstore watch event is delivered via a callback function declared as:

void (*callback)(struct xenbus_watch *,

                 const char **vec, unsigned int len);

As all watch events only ever come with two parameters (path and token) changing the prototype to:

void (*callback)(struct xenbus_watch *,

                 const char *path, const char *token);

is the natural thing to do.

Apply this change and adapt all users.

Quoted 7 lines

blkback changes:

Thanks, Roger.

Reviewed-by on v1 2/3
#11 lore raw

Re: [PATCH 2/3] xen: modify xenstore watch event interface

In reply to Juergen Gross
Quoted 16 lines · Juergen Gross

Today a Xenstore watch event is delivered via a callback function declared as:

void (*callback)(struct xenbus_watch *,

                 const char **vec, unsigned int len);

As all watch events only ever come with two parameters (path and token) changing the prototype to:

void (*callback)(struct xenbus_watch *,

                 const char *path, const char *token);

is the natural thing to do.

Apply this change and adapt all users.

Quoted 7 lines

blkback changes:

Thanks, Roger.

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

#2 lore raw

Re: [PATCH 1/3] xen: clean up xenbus internal headers

In reply to Juergen Gross
Quoted 7 lines · Juergen Gross

The xenbus driver has an awful mixture of internal and global visible
headers: some of the internal used only stuff is defined in the global header include/xen/xenbus.h while some stuff defined in internal headers is used by other drivers, too.

Clean this up by moving the external used symbols to include/xen/xenbus.h and the symbols used internal only to a new

I think

s/external/externally/
s/internal/internally/

Quoted 1 line

header drivers/xen/xenbus/xenbus.h

... and remove xenbus_comms.h

Quoted 28 lines
---
 drivers/xen/xenbus/{xenbus_probe.h => xenbus.h} | 64 ++++++++++++++-----------
 drivers/xen/xenbus/xenbus_client.c              |  2 +-
 drivers/xen/xenbus/xenbus_comms.c               |  2 +-
 drivers/xen/xenbus/xenbus_comms.h               | 51 --------------------
 drivers/xen/xenbus/xenbus_dev_backend.c         |  2 +-
 drivers/xen/xenbus/xenbus_dev_frontend.c        |  4 +-
 drivers/xen/xenbus/xenbus_probe.c               |  3 +-
 drivers/xen/xenbus/xenbus_probe_backend.c       |  3 +-
 drivers/xen/xenbus/xenbus_probe_frontend.c      |  3 +-
 drivers/xen/xenbus/xenbus_xs.c                  |  3 +-
 drivers/xen/xenfs/super.c                       |  2 +-
 drivers/xen/xenfs/xenstored.c                   |  2 +-
 include/xen/xenbus.h                            | 12 ++---
 13 files changed, 52 insertions(+), 101 deletions(-)
 rename drivers/xen/xenbus/{xenbus_probe.h => xenbus.h} (58%)
 delete mode 100644 drivers/xen/xenbus/xenbus_comms.h
drivers/xen/xenbus/xenbus.h renamed from drivers/xen/xenbus/xenbus_probe.h +0 -0
similarity index 58%
rename from drivers/xen/xenbus/xenbus_probe.h
rename to drivers/xen/xenbus/xenbus.h
index c9ec7ca..6a80c1e 100644

drivers/xen/xenbus/xenbus.h line 7

Quoted 9 lines
drivers/xen/xenbus/xenbus.h renamed from drivers/xen/xenbus/xenbus_probe.h +2 -5
@@ -1,10 +1,7 @@
-/******************************************************************************
- * xenbus_probe.h
- *
- * Talks to Xen Store to figure out what devices we have.
+/*
+ * Private include for xenbus communications.
  *
  * Copyright (C) 2005 Rusty Russell, IBM Corporation
- * Copyright (C) 2005 XenSource Ltd.

Why is this?

-boris

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

Reviewed-by on v1 2/3
#12 lore raw

Re: [PATCH 2/3] xen: modify xenstore watch event interface

In reply to Juergen Gross
Quoted 27 lines · Juergen Gross

Today a Xenstore watch event is delivered via a callback function declared as:

void (*callback)(struct xenbus_watch *,

                 const char **vec, unsigned int len);

As all watch events only ever come with two parameters (path and token) changing the prototype to:

void (*callback)(struct xenbus_watch *,

                 const char *path, const char *token);

is the natural thing to do.

Apply this change and adapt all users.

drivers/xen/xenbus/xenbus_xs.c static int process_msg(void) · line 906

Quoted 9 lines
drivers/xen/xenbus/xenbus_xs.c +2 -4
@@ -903,24 +902,24 @@ static int process_msg(void)
 	body[msg->hdr.len] = '\0';
 
 	if (msg->hdr.type == XS_WATCH_EVENT) {
-		msg->u.watch.vec = split(body, msg->hdr.len,
-					 &msg->u.watch.vec_size);
-		if (IS_ERR(msg->u.watch.vec)) {
-			err = PTR_ERR(msg->u.watch.vec);
+		if (count_strings(body, msg->hdr.len) != 2) {
+			err = -EINVAL;

xenbus_write_watch() returns -EILSEQ when this type of error is encountered so perhaps for we should return the same error here.

Either way

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

author maintainer
#3 lore raw

Re: [PATCH 1/3] xen: clean up xenbus internal headers

In reply to Boris Ostrovsky
Quoted 6 lines · Boris Ostrovsky
Quoted 5 lines · Juergen Gross

The xenbus driver has an awful mixture of internal and global visible
headers: some of the internal used only stuff is defined in the global header include/xen/xenbus.h while some stuff defined in internal headers is used by other drivers, too.

Quoted 7 lines
Quoted 2 lines

Clean this up by moving the external used symbols to include/xen/xenbus.h and the symbols used internal only to a new

I think

s/external/externally/
s/internal/internally/

Hmm, yes.

Quoted 3 lines
Quoted 1 line

header drivers/xen/xenbus/xenbus.h

... and remove xenbus_comms.h

and xenbus_probe.h.

Quoted 32 lines
Quoted 32 lines
---
 drivers/xen/xenbus/{xenbus_probe.h => xenbus.h} | 64 ++++++++++++++-----------
 drivers/xen/xenbus/xenbus_client.c              |  2 +-
 drivers/xen/xenbus/xenbus_comms.c               |  2 +-
 drivers/xen/xenbus/xenbus_comms.h               | 51 --------------------
 drivers/xen/xenbus/xenbus_dev_backend.c         |  2 +-
 drivers/xen/xenbus/xenbus_dev_frontend.c        |  4 +-
 drivers/xen/xenbus/xenbus_probe.c               |  3 +-
 drivers/xen/xenbus/xenbus_probe_backend.c       |  3 +-
 drivers/xen/xenbus/xenbus_probe_frontend.c      |  3 +-
 drivers/xen/xenbus/xenbus_xs.c                  |  3 +-
 drivers/xen/xenfs/super.c                       |  2 +-
 drivers/xen/xenfs/xenstored.c                   |  2 +-
 include/xen/xenbus.h                            | 12 ++---
 13 files changed, 52 insertions(+), 101 deletions(-)
 rename drivers/xen/xenbus/{xenbus_probe.h => xenbus.h} (58%)
 delete mode 100644 drivers/xen/xenbus/xenbus_comms.h
drivers/xen/xenbus/xenbus.h renamed from drivers/xen/xenbus/xenbus_probe.h +0 -3
similarity index 58%
rename from drivers/xen/xenbus/xenbus_probe.h
rename to drivers/xen/xenbus/xenbus.h
index c9ec7ca..6a80c1e 100644
@@ -1,10 +1,7 @@
-/******************************************************************************
- * xenbus_probe.h
- *
Quoted 8 lines

drivers/xen/xenbus/xenbus.h line 7

Quoted 6 lines
drivers/xen/xenbus/xenbus.h renamed from drivers/xen/xenbus/xenbus_probe.h +2 -2
- * Talks to Xen Store to figure out what devices we have.
+/*
+ * Private include for xenbus communications.
  *
  * Copyright (C) 2005 Rusty Russell, IBM Corporation
- * Copyright (C) 2005 XenSource Ltd.

Why is this?

Result of merging two header files. Will re-add.

Juergen

author maintainer
#4 lore raw

Re: [PATCH 1/3] xen: clean up xenbus internal headers

In reply to Boris Ostrovsky
Quoted 6 lines · Boris Ostrovsky
Quoted 5 lines · Juergen Gross

The xenbus driver has an awful mixture of internal and global visible
headers: some of the internal used only stuff is defined in the global header include/xen/xenbus.h while some stuff defined in internal headers is used by other drivers, too.

Quoted 7 lines
Quoted 2 lines

Clean this up by moving the external used symbols to include/xen/xenbus.h and the symbols used internal only to a new

I think

s/external/externally/
s/internal/internally/

Hmm, yes.

Quoted 3 lines
Quoted 1 line

header drivers/xen/xenbus/xenbus.h

... and remove xenbus_comms.h

and xenbus_probe.h.

Quoted 32 lines
Quoted 32 lines
---
 drivers/xen/xenbus/{xenbus_probe.h => xenbus.h} | 64 ++++++++++++++-----------
 drivers/xen/xenbus/xenbus_client.c              |  2 +-
 drivers/xen/xenbus/xenbus_comms.c               |  2 +-
 drivers/xen/xenbus/xenbus_comms.h               | 51 --------------------
 drivers/xen/xenbus/xenbus_dev_backend.c         |  2 +-
 drivers/xen/xenbus/xenbus_dev_frontend.c        |  4 +-
 drivers/xen/xenbus/xenbus_probe.c               |  3 +-
 drivers/xen/xenbus/xenbus_probe_backend.c       |  3 +-
 drivers/xen/xenbus/xenbus_probe_frontend.c      |  3 +-
 drivers/xen/xenbus/xenbus_xs.c                  |  3 +-
 drivers/xen/xenfs/super.c                       |  2 +-
 drivers/xen/xenfs/xenstored.c                   |  2 +-
 include/xen/xenbus.h                            | 12 ++---
 13 files changed, 52 insertions(+), 101 deletions(-)
 rename drivers/xen/xenbus/{xenbus_probe.h => xenbus.h} (58%)
 delete mode 100644 drivers/xen/xenbus/xenbus_comms.h
drivers/xen/xenbus/xenbus.h renamed from drivers/xen/xenbus/xenbus_probe.h +0 -3
similarity index 58%
rename from drivers/xen/xenbus/xenbus_probe.h
rename to drivers/xen/xenbus/xenbus.h
index c9ec7ca..6a80c1e 100644
@@ -1,10 +1,7 @@
-/******************************************************************************
- * xenbus_probe.h
- *
Quoted 8 lines

drivers/xen/xenbus/xenbus.h line 7

Quoted 6 lines
drivers/xen/xenbus/xenbus.h renamed from drivers/xen/xenbus/xenbus_probe.h +2 -2
- * Talks to Xen Store to figure out what devices we have.
+/*
+ * Private include for xenbus communications.
  *
  * Copyright (C) 2005 Rusty Russell, IBM Corporation
- * Copyright (C) 2005 XenSource Ltd.

Why is this?

Result of merging two header files. Will re-add.

Juergen

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

author maintainer
#13 lore raw

Re: [PATCH 2/3] xen: modify xenstore watch event interface

In reply to Boris Ostrovsky
Quoted 30 lines · Boris Ostrovsky
Quoted 29 lines · Juergen Gross

Today a Xenstore watch event is delivered via a callback function declared as:

void (*callback)(struct xenbus_watch *,

                 const char **vec, unsigned int len);

As all watch events only ever come with two parameters (path and token) changing the prototype to:

void (*callback)(struct xenbus_watch *,

                 const char *path, const char *token);

is the natural thing to do.

Apply this change and adapt all users.

drivers/xen/xenbus/xenbus_xs.c +0 -0
@@ -903,24 +902,24 @@ static int process_msg(void)
 	body[msg->hdr.len] = '\0';
 
Quoted 10 lines

drivers/xen/xenbus/xenbus_xs.c static int process_msg(void) · line 906

Quoted 7 lines
drivers/xen/xenbus/xenbus_xs.c +2 -4
 	if (msg->hdr.type == XS_WATCH_EVENT) {
-		msg->u.watch.vec = split(body, msg->hdr.len,
-					 &msg->u.watch.vec_size);
-		if (IS_ERR(msg->u.watch.vec)) {
-			err = PTR_ERR(msg->u.watch.vec);
+		if (count_strings(body, msg->hdr.len) != 2) {
+			err = -EINVAL;

xenbus_write_watch() returns -EILSEQ when this type of error is encountered so perhaps for we should return the same error here.

Not since 9a6161fe73bdd3ae4a1e18421b0b20cb7141f680. :-)

Quoted 4 lines

Either way

Thanks,

Juergen

author maintainer
#14 lore raw

Re: [PATCH 2/3] xen: modify xenstore watch event interface

In reply to Boris Ostrovsky
Quoted 30 lines · Boris Ostrovsky
Quoted 29 lines · Juergen Gross

Today a Xenstore watch event is delivered via a callback function declared as:

void (*callback)(struct xenbus_watch *,

                 const char **vec, unsigned int len);

As all watch events only ever come with two parameters (path and token) changing the prototype to:

void (*callback)(struct xenbus_watch *,

                 const char *path, const char *token);

is the natural thing to do.

Apply this change and adapt all users.

drivers/xen/xenbus/xenbus_xs.c +0 -0
@@ -903,24 +902,24 @@ static int process_msg(void)
 	body[msg->hdr.len] = '\0';
 
Quoted 10 lines

drivers/xen/xenbus/xenbus_xs.c static int process_msg(void) · line 906

Quoted 7 lines
drivers/xen/xenbus/xenbus_xs.c +2 -4
 	if (msg->hdr.type == XS_WATCH_EVENT) {
-		msg->u.watch.vec = split(body, msg->hdr.len,
-					 &msg->u.watch.vec_size);
-		if (IS_ERR(msg->u.watch.vec)) {
-			err = PTR_ERR(msg->u.watch.vec);
+		if (count_strings(body, msg->hdr.len) != 2) {
+			err = -EINVAL;

xenbus_write_watch() returns -EILSEQ when this type of error is encountered so perhaps for we should return the same error here.

Not since 9a6161fe73bdd3ae4a1e18421b0b20cb7141f680. :-)

Quoted 4 lines

Either way

Thanks,

Juergen

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

#16 lore raw

Re: [PATCH 3/3] xen: optimize xenbus driver for multiple concurrent xenstore accesses

In reply to Juergen Gross
Quoted 63 lines · Juergen Gross

Handling of multiple concurrent Xenstore accesses through xenbus driver either from the kernel or user land is rather lame today: xenbus is capable to have one access active only at one point of time.

Rewrite xenbus to handle multiple requests concurrently by making use of the request id of the Xenstore protocol. This requires to:

- Instead of blocking inside xb_read() when trying to read data from the xenstore ring buffer do so only in the main loop of xenbus_thread().

- Instead of doing writes to the xenstore ring buffer in the context of the caller just queue the request and do the write in the dedicated xenbus thread.

- Instead of just forwarding the request id specified by the caller of xenbus to xenstore use a xenbus internal unique request id. This will allow multiple outstanding requests.

- Modify the locking scheme in order to allow multiple requests being active in parallel.

- Instead of waiting for the reply of a user's xenstore request after writing the request to the xenstore ring buffer return directly to the caller and do the waiting in the read path.

Additionally signal handling was optimized by avoiding waking up the xenbus thread or sending an event to Xenstore in case the addressed entity is known to be running already.

As a result communication with Xenstore is sped up by a factor of up to 5: depending on the request type (read or write) and the amount of data transferred the gain was at least 20% (small reads) and went up to a factor of 5 for large writes.

In the end some more rough edges of xenbus have been smoothed:

- Handling of memory shortage when reading from xenstore ring buffer in the xenbus driver was not optimal: it was busy looping and issuing a warning in each loop.

- In case of xenstore not running in dom0 but in a stubdom we end up with two xenbus threads running as the initialization of xenbus in dom0 expecting a local xenstored will be redone later when connecting to the xenstore domain. Up to now this was no problem as locking would prevent the two xenbus threads interfering with each other, but this was just a waste of kernel resources.

- An out of memory situation while writing to or reading from the xenstore ring buffer no longer will lead to a possible loss of synchronization with xenstore.

- The user read and write part are now interruptible by signals.

---
I'm aware that the changes are quite large. I thought about sending a version split into multiple patches, but a lot of lines would have been touched by more than one patch. I still have the multiple patch variant lying around - this patch is split into 11 smaller ones. While all steps of this larger series is operational some steps are not optimal as they are even slower than the original version of xenbus.

Quoted 1 line

Nevertheless I can send the large series if there are requests for it.

I will comment only on xen_comms changes for now since otherwise I am afraid it may be difficult to keep track of conversation.

Quoted 13 lines
drivers/xen/xenbus/xenbus_comms.c +1 -0
index c21ec02..fa054ca 100644
@@ -34,6 +34,7 @@
 
 #include <linux/wait.h>
 #include <linux/interrupt.h>
+#include <linux/kthread.h>
 #include <linux/sched.h>
 #include <linux/err.h>
 #include <xen/xenbus.h>

drivers/xen/xenbus/xenbus_comms.c line 49

Quoted 7 lines
drivers/xen/xenbus/xenbus_comms.c +4 -0
@@ -42,11 +43,40 @@
 #include <xen/page.h>
 #include "xenbus.h"
 
+struct xs_thread_state_write {
+	struct xb_req_data *req;
+	int idx;
+	unsigned int used;

"written" or "sent"?

drivers/xen/xenbus/xenbus_comms.c line 60

Quoted 11 lines
drivers/xen/xenbus/xenbus_comms.c +11 -0
+};
+
+struct xs_thread_state_read {
+	struct xsd_sockmsg msg;
+	char *body;
+	union {
+		void *alloc;
+		struct xs_watch_event *watch;
+	};
+	bool in_msg;
+	bool in_hdr;

It may be better to keep track of which state we are in using a bitmap. Otherwise it easy to lose track of one or the other.

drivers/xen/xenbus/xenbus_comms.c line 61

Quoted 1 line
drivers/xen/xenbus/xenbus_comms.c +1 -0
+	unsigned int used;

"read" or"received"?

drivers/xen/xenbus/xenbus_comms.c line 62

Quoted 1 line
drivers/xen/xenbus/xenbus_comms.c +1 -0
+};

Both of these are private to process_msg/process_write so perhaps they can be declared in those routines' scopes.

Quoted 72 lines
drivers/xen/xenbus/xenbus_comms.c +24 -16
+
+/* A list of replies. Currently only one will ever be outstanding. */
+LIST_HEAD(xs_reply_list);
+
+/* A list of write requests. */
+LIST_HEAD(xb_write_list);
+DECLARE_WAIT_QUEUE_HEAD(xb_waitq);
+DEFINE_MUTEX(xb_write_mutex);
+
+/* Protect xenbus reader thread against save/restore. */
+DEFINE_MUTEX(xs_response_mutex);
+
 static int xenbus_irq;
+static struct task_struct *xenbus_task;
 
 static DECLARE_WORK(probe_work, xenbus_probe);
 
-static DECLARE_WAIT_QUEUE_HEAD(xb_waitq);
 
 static irqreturn_t wake_waiting(int irq, void *unused)
 {
@@ -84,30 +114,31 @@ static const void *get_input_chunk(XENSTORE_RING_IDX cons,
 	return buf + MASK_XENSTORE_IDX(cons);
 }
 
+static int xb_data_to_write(void)
+{
+	struct xenstore_domain_interface *intf = xen_store_interface;
+
+	return (intf->req_prod - intf->req_cons) != XENSTORE_RING_SIZE &&
+		!list_empty(&xb_write_list);
+}
+
 /**
  * xb_write - low level write
  * @data: buffer to send
  * @len: length of buffer
  *
- * Returns 0 on success, error otherwise.
+ * Returns number of bytes written or -err.
  */
-int xb_write(const void *data, unsigned len)
+static int xb_write(const void *data, unsigned int len)
 {
 	struct xenstore_domain_interface *intf = xen_store_interface;
 	XENSTORE_RING_IDX cons, prod;
-	int rc;
+	unsigned int bytes = 0;
 
 	while (len != 0) {
 		void *dst;
 		unsigned int avail;
 
-		rc = wait_event_interruptible(
-			xb_waitq,
-			(intf->req_prod - intf->req_cons) !=
-			XENSTORE_RING_SIZE);
-		if (rc < 0)
-			return rc;
-
 		/* Read indexes, then verify. */
 		cons = intf->req_cons;
 		prod = intf->req_prod;
@@ -115,59 +146,57 @@ int xb_write(const void *data, unsigned len)
 			intf->req_cons = intf->req_prod = 0;
 			return -EIO;
 		}
-
-		dst = get_output_chunk(cons, prod, intf->req, &avail);
-		if (avail == 0)
-			continue;
-		if (avail > len)

drivers/xen/xenbus/xenbus_comms.c int xb_write(const void *data, unsigned len) · line 157

Quoted 10 lines
drivers/xen/xenbus/xenbus_comms.c +5 -1
-			avail = len;
+		if (!xb_data_to_write())
+			return bytes;
 
 		/* Must write data /after/ reading the consumer index. */
 		virt_mb();
 
+		dst = get_output_chunk(cons, prod, intf->req, &avail);
+		if (avail == 0)
+			continue;

Should we continue the loop here or return? We are waiting for the reader to get stuff off the ring.

Quoted 106 lines
drivers/xen/xenbus/xenbus_comms.c +45 -17
+		if (avail > len)
+			avail = len;
+
 		memcpy(dst, data, avail);
 		data += avail;
 		len -= avail;
+		bytes += avail;
 
 		/* Other side must not see new producer until data is there. */
 		virt_wmb();
 		intf->req_prod += avail;
 
 		/* Implies mb(): other side will see the updated producer. */
-		notify_remote_via_evtchn(xen_store_evtchn);
+		if (prod <= intf->req_cons)
+			notify_remote_via_evtchn(xen_store_evtchn);
 	}
 
-	return 0;
+	return bytes;
 }
 
-int xb_data_to_read(void)
+static int xb_data_to_read(void)
 {
 	struct xenstore_domain_interface *intf = xen_store_interface;
 	return (intf->rsp_cons != intf->rsp_prod);
 }
 
-int xb_wait_for_data_to_read(void)
-{
-	return wait_event_interruptible(xb_waitq, xb_data_to_read());
-}
-
-int xb_read(void *data, unsigned len)
+static int xb_read(void *data, unsigned int len)
 {
 	struct xenstore_domain_interface *intf = xen_store_interface;
 	XENSTORE_RING_IDX cons, prod;
-	int rc;
+	unsigned int bytes = 0;
 
 	while (len != 0) {
 		unsigned int avail;
 		const char *src;
 
-		rc = xb_wait_for_data_to_read();
-		if (rc < 0)
-			return rc;
-
 		/* Read indexes, then verify. */
 		cons = intf->rsp_cons;
 		prod = intf->rsp_prod;
+		if (cons == prod)
+			return bytes;
+
 		if (!check_indexes(cons, prod)) {
 			intf->rsp_cons = intf->rsp_prod = 0;
 			return -EIO;
@@ -185,17 +214,229 @@ int xb_read(void *data, unsigned len)
 		memcpy(data, src, avail);
 		data += avail;
 		len -= avail;
+		bytes += avail;
 
 		/* Other side must not see free space until we've copied out */
 		virt_mb();
 		intf->rsp_cons += avail;
 
-		pr_debug("Finished read of %i bytes (%i to go)\n", avail, len);
-
 		/* Implies mb(): other side will see the updated consumer. */
-		notify_remote_via_evtchn(xen_store_evtchn);
+		if (intf->rsp_prod - cons >= XENSTORE_RING_SIZE)
+			notify_remote_via_evtchn(xen_store_evtchn);
 	}
 
+	return bytes;
+}
+
+static int process_msg(void)
+{
+	static struct xs_thread_state_read state;
+	struct xb_req_data *req;
+	int err;
+	unsigned int len;
+
+	if (!state.in_msg) {
+		state.in_msg = true;
+		state.in_hdr = true;
+		state.used = 0;
+
+		/*
+		 * We must disallow save/restore while reading a message.
+		 * A partial read across s/r leaves us out of sync with
+		 * xenstored.
+		 */
+		mutex_lock(&xs_response_mutex);
+
+		if (!xb_data_to_read()) {
+			/* We raced with save/restore: pending data 'gone'. */
+			mutex_unlock(&xs_response_mutex);
+			state.in_msg = false;
+			return 0;
+		}
+	}

drivers/xen/xenbus/xenbus_comms.c int xb_read(void *data, unsigned len) · line 266

Quoted 10 lines
drivers/xen/xenbus/xenbus_comms.c +10 -0
+
+	if (state.in_hdr) {
+		if (state.used != sizeof(state.msg)) {
+			err = xb_read((void *)&state.msg + state.used,
+				      sizeof(state.msg) - state.used);
+			if (err < 0)
+				goto out;
+			state.used += err;
+			if (state.used != sizeof(state.msg))
+				return 0;

Would it be possible to do locking at the caller? I understand that you are trying to hold the lock across multiple invocations of this function but it feels somewhat counter-intuitive and bug-prone.

If it's not possible then at least please add a comment explaining locking algorithm.

drivers/xen/xenbus/xenbus_comms.c int xb_read(void *data, unsigned len) · line 277

Quoted 11 lines
drivers/xen/xenbus/xenbus_comms.c +11 -0
+			if (state.msg.len > XENSTORE_PAYLOAD_MAX) {
+				err = -EINVAL;
+				goto out;
+			}
+		}
+
+		len = state.msg.len + 1;
+		if (state.msg.type == XS_WATCH_EVENT)
+			len += sizeof(*state.watch);
+
+		state.alloc = kmalloc(len, GFP_NOIO | __GFP_HIGH);

Why can't you kmalloc to state.body only when type!=XS_WATCH_EVENT ?

Quoted 99 lines
drivers/xen/xenbus/xenbus_comms.c +99 -0
+		if (!state.alloc)
+			return -ENOMEM;
+
+		if (state.msg.type == XS_WATCH_EVENT)
+			state.body = state.watch->body;
+		else
+			state.body = state.alloc;
+		state.in_hdr = false;
+		state.used = 0;
+	}
+
+	err = xb_read(state.body + state.used, state.msg.len - state.used);
+	if (err < 0)
+		goto out;
+
+	state.used += err;
+	if (state.used != state.msg.len)
+		return 0;
+
+	state.body[state.msg.len] = '\0';
+
+	if (state.msg.type == XS_WATCH_EVENT) {
+		state.watch->len = state.msg.len;
+		err = xs_watch_msg(state.watch);
+	} else {
+		err = -ENOENT;
+		mutex_lock(&xb_write_mutex);
+		list_for_each_entry(req, &xs_reply_list, list) {
+			if (req->msg.req_id == state.msg.req_id) {
+				if (req->state == xb_req_state_wait_reply) {
+					req->msg.type = state.msg.type;
+					req->msg.len = state.msg.len;
+					req->body = state.body;
+					req->state = xb_req_state_got_reply;
+					list_del(&req->list);
+					req->cb(req);
+				} else {
+					list_del(&req->list);
+					kfree(req);
+				}
+				err = 0;
+				break;
+			}
+		}
+		mutex_unlock(&xb_write_mutex);
+		if (err)
+			goto out;
+	}
+
+	mutex_unlock(&xs_response_mutex);
+
+	state.in_msg = false;
+	state.alloc = NULL;
+	return err;
+
+ out:
+	mutex_unlock(&xs_response_mutex);
+	state.in_msg = false;
+	kfree(state.alloc);
+	state.alloc = NULL;
+	return err;
+}
+
+static int process_writes(void)
+{
+	static struct xs_thread_state_write state;
+	void *base;
+	unsigned int len;
+	int err = 0;
+
+	if (!xb_data_to_write())
+		return 0;
+
+	mutex_lock(&xb_write_mutex);
+
+	if (!state.req) {
+		state.req = list_first_entry(&xb_write_list,
+					     struct xb_req_data, list);
+		state.idx = -1;
+		state.used = 0;
+	}
+
+	if (state.req->state == xb_req_state_aborted)
+		goto out_err;
+
+	while (state.idx < state.req->num_vecs) {
+		if (state.idx < 0) {
+			base = &state.req->msg;
+			len = sizeof(state.req->msg);
+		} else {
+			base = state.req->vec[state.idx].iov_base;
+			len = state.req->vec[state.idx].iov_len;
+		}
+		err = xb_write(base + state.used, len - state.used);
+		if (err < 0)
+			goto out_err;
+		state.used += err;
+		if (state.used != len)
+			goto out;

drivers/xen/xenbus/xenbus_comms.c int xb_read(void *data, unsigned len) · line 386

Quoted 10 lines
drivers/xen/xenbus/xenbus_comms.c +10 -0
+
+		state.idx++;
+		state.used = 0;
+	}
+
+	/*
+	 * You would expect the following to be racy, but as the response is
+	 * being read by our thread there is no risk of req being freed
+	 * under our feet.
+	 */

I don't think I understand this (and it's missing a "so" or something like that between "thread" and "there"). If this is not racy, why are we doing this under xb_write_mutex?

drivers/xen/xenbus/xenbus_comms.c int xb_read(void *data, unsigned len) · line 399

Quoted 13 lines
drivers/xen/xenbus/xenbus_comms.c +13 -0
+	list_del(&state.req->list);
+	state.req->state = xb_req_state_wait_reply;
+	list_add_tail(&state.req->list, &xs_reply_list);
+	state.req = NULL;
+
+ out:
+	mutex_unlock(&xb_write_mutex);
+
+	return 0;
+
+ out_err:
+	state.req->msg.type = XS_ERROR;
+	state.req->err = err;

You don't seem to need this for xb_req_state_aborted since you are freeing state_req. OTOH, why shouldn't aborted requests generate an error reply as well?

Quoted 27 lines
drivers/xen/xenbus/xenbus_comms.c +27 -0
+	list_del(&state.req->list);
+	if (state.req->state == xb_req_state_aborted)
+		kfree(state.req);
+	else {
+		state.req->state = xb_req_state_got_reply;
+		wake_up(&state.req->wq);
+	}
+
+	mutex_unlock(&xb_write_mutex);
+
+	state.req = NULL;
+
+	return err;
+}
+
+static int xb_thread_work(void)
+{
+	return xb_data_to_read() || xb_data_to_write();
+}
+
+static int xenbus_thread(void *unused)
+{
+	int err;
+
+	while (!kthread_should_stop()) {
+		if (wait_event_interruptible(xb_waitq, xb_thread_work()))
+			continue;

drivers/xen/xenbus/xenbus_comms.c int xb_read(void *data, unsigned len) · line 436

Quoted 10 lines
drivers/xen/xenbus/xenbus_comms.c +10 -0
+
+		err = process_msg();
+		if (err == -ENOMEM)
+			schedule();
+		else if (err)
+			pr_warn("error %d while reading message\n", err);
+
+		err = process_writes();
+		if (err)
+			pr_warn("error %d while writing message\n", err);

Is there a chance that errors are persistent and you then spam the log?

-boris

Quoted 23 lines
drivers/xen/xenbus/xenbus_comms.c +4 -0
+	}
+
+	xenbus_task = NULL;
 	return 0;
 }
 
@@ -223,6 +464,7 @@ int xb_init_comms(void)
 		rebind_evtchn_irq(xen_store_evtchn, xenbus_irq);
 	} else {
 		int err;
+
 		err = bind_evtchn_to_irqhandler(xen_store_evtchn, wake_waiting,
 						0, "xenbus", &xb_waitq);
 		if (err < 0) {
@@ -231,6 +473,13 @@ int xb_init_comms(void)
 		}
 
 		xenbus_irq = err;

drivers/xen/xenbus/xenbus_comms.c int xb_init_comms(void) · line 485

Quoted 10 lines
drivers/xen/xenbus/xenbus_comms.c +7 -0
+
+		if (!xenbus_task) {
+			xenbus_task = kthread_run(xenbus_thread, NULL,
+						  "xenbus");
+			if (IS_ERR(xenbus_task))
+				return PTR_ERR(xenbus_task);
+		}
 	}
 
 	return 0;

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

#17 lore raw

Re: [PATCH 3/3] xen: optimize xenbus driver for multiple concurrent xenstore accesses

In reply to Juergen Gross
Quoted 63 lines · Juergen Gross

Handling of multiple concurrent Xenstore accesses through xenbus driver either from the kernel or user land is rather lame today: xenbus is capable to have one access active only at one point of time.

Rewrite xenbus to handle multiple requests concurrently by making use of the request id of the Xenstore protocol. This requires to:

- Instead of blocking inside xb_read() when trying to read data from the xenstore ring buffer do so only in the main loop of xenbus_thread().

- Instead of doing writes to the xenstore ring buffer in the context of the caller just queue the request and do the write in the dedicated xenbus thread.

- Instead of just forwarding the request id specified by the caller of xenbus to xenstore use a xenbus internal unique request id. This will allow multiple outstanding requests.

- Modify the locking scheme in order to allow multiple requests being active in parallel.

- Instead of waiting for the reply of a user's xenstore request after writing the request to the xenstore ring buffer return directly to the caller and do the waiting in the read path.

Additionally signal handling was optimized by avoiding waking up the xenbus thread or sending an event to Xenstore in case the addressed entity is known to be running already.

As a result communication with Xenstore is sped up by a factor of up to 5: depending on the request type (read or write) and the amount of data transferred the gain was at least 20% (small reads) and went up to a factor of 5 for large writes.

In the end some more rough edges of xenbus have been smoothed:

- Handling of memory shortage when reading from xenstore ring buffer in the xenbus driver was not optimal: it was busy looping and issuing a warning in each loop.

- In case of xenstore not running in dom0 but in a stubdom we end up with two xenbus threads running as the initialization of xenbus in dom0 expecting a local xenstored will be redone later when connecting to the xenstore domain. Up to now this was no problem as locking would prevent the two xenbus threads interfering with each other, but this was just a waste of kernel resources.

- An out of memory situation while writing to or reading from the xenstore ring buffer no longer will lead to a possible loss of synchronization with xenstore.

- The user read and write part are now interruptible by signals.

---
I'm aware that the changes are quite large. I thought about sending a version split into multiple patches, but a lot of lines would have been touched by more than one patch. I still have the multiple patch variant lying around - this patch is split into 11 smaller ones. While all steps of this larger series is operational some steps are not optimal as they are even slower than the original version of xenbus.

Quoted 1 line

Nevertheless I can send the large series if there are requests for it.

I will comment only on xen_comms changes for now since otherwise I am afraid it may be difficult to keep track of conversation.

Quoted 13 lines
drivers/xen/xenbus/xenbus_comms.c +1 -0
index c21ec02..fa054ca 100644
@@ -34,6 +34,7 @@
 
 #include <linux/wait.h>
 #include <linux/interrupt.h>
+#include <linux/kthread.h>
 #include <linux/sched.h>
 #include <linux/err.h>
 #include <xen/xenbus.h>

drivers/xen/xenbus/xenbus_comms.c line 49

Quoted 7 lines
drivers/xen/xenbus/xenbus_comms.c +4 -0
@@ -42,11 +43,40 @@
 #include <xen/page.h>
 #include "xenbus.h"
 
+struct xs_thread_state_write {
+	struct xb_req_data *req;
+	int idx;
+	unsigned int used;

"written" or "sent"?

drivers/xen/xenbus/xenbus_comms.c line 60

Quoted 11 lines
drivers/xen/xenbus/xenbus_comms.c +11 -0
+};
+
+struct xs_thread_state_read {
+	struct xsd_sockmsg msg;
+	char *body;
+	union {
+		void *alloc;
+		struct xs_watch_event *watch;
+	};
+	bool in_msg;
+	bool in_hdr;

It may be better to keep track of which state we are in using a bitmap. Otherwise it easy to lose track of one or the other.

drivers/xen/xenbus/xenbus_comms.c line 61

Quoted 1 line
drivers/xen/xenbus/xenbus_comms.c +1 -0
+	unsigned int used;

"read" or"received"?

drivers/xen/xenbus/xenbus_comms.c line 62

Quoted 1 line
drivers/xen/xenbus/xenbus_comms.c +1 -0
+};

Both of these are private to process_msg/process_write so perhaps they can be declared in those routines' scopes.

Quoted 72 lines
drivers/xen/xenbus/xenbus_comms.c +24 -16
+
+/* A list of replies. Currently only one will ever be outstanding. */
+LIST_HEAD(xs_reply_list);
+
+/* A list of write requests. */
+LIST_HEAD(xb_write_list);
+DECLARE_WAIT_QUEUE_HEAD(xb_waitq);
+DEFINE_MUTEX(xb_write_mutex);
+
+/* Protect xenbus reader thread against save/restore. */
+DEFINE_MUTEX(xs_response_mutex);
+
 static int xenbus_irq;
+static struct task_struct *xenbus_task;
 
 static DECLARE_WORK(probe_work, xenbus_probe);
 
-static DECLARE_WAIT_QUEUE_HEAD(xb_waitq);
 
 static irqreturn_t wake_waiting(int irq, void *unused)
 {
@@ -84,30 +114,31 @@ static const void *get_input_chunk(XENSTORE_RING_IDX cons,
 	return buf + MASK_XENSTORE_IDX(cons);
 }
 
+static int xb_data_to_write(void)
+{
+	struct xenstore_domain_interface *intf = xen_store_interface;
+
+	return (intf->req_prod - intf->req_cons) != XENSTORE_RING_SIZE &&
+		!list_empty(&xb_write_list);
+}
+
 /**
  * xb_write - low level write
  * @data: buffer to send
  * @len: length of buffer
  *
- * Returns 0 on success, error otherwise.
+ * Returns number of bytes written or -err.
  */
-int xb_write(const void *data, unsigned len)
+static int xb_write(const void *data, unsigned int len)
 {
 	struct xenstore_domain_interface *intf = xen_store_interface;
 	XENSTORE_RING_IDX cons, prod;
-	int rc;
+	unsigned int bytes = 0;
 
 	while (len != 0) {
 		void *dst;
 		unsigned int avail;
 
-		rc = wait_event_interruptible(
-			xb_waitq,
-			(intf->req_prod - intf->req_cons) !=
-			XENSTORE_RING_SIZE);
-		if (rc < 0)
-			return rc;
-
 		/* Read indexes, then verify. */
 		cons = intf->req_cons;
 		prod = intf->req_prod;
@@ -115,59 +146,57 @@ int xb_write(const void *data, unsigned len)
 			intf->req_cons = intf->req_prod = 0;
 			return -EIO;
 		}
-
-		dst = get_output_chunk(cons, prod, intf->req, &avail);
-		if (avail == 0)
-			continue;
-		if (avail > len)

drivers/xen/xenbus/xenbus_comms.c int xb_write(const void *data, unsigned len) · line 157

Quoted 10 lines
drivers/xen/xenbus/xenbus_comms.c +5 -1
-			avail = len;
+		if (!xb_data_to_write())
+			return bytes;
 
 		/* Must write data /after/ reading the consumer index. */
 		virt_mb();
 
+		dst = get_output_chunk(cons, prod, intf->req, &avail);
+		if (avail == 0)
+			continue;

Should we continue the loop here or return? We are waiting for the reader to get stuff off the ring.

Quoted 106 lines
drivers/xen/xenbus/xenbus_comms.c +45 -17
+		if (avail > len)
+			avail = len;
+
 		memcpy(dst, data, avail);
 		data += avail;
 		len -= avail;
+		bytes += avail;
 
 		/* Other side must not see new producer until data is there. */
 		virt_wmb();
 		intf->req_prod += avail;
 
 		/* Implies mb(): other side will see the updated producer. */
-		notify_remote_via_evtchn(xen_store_evtchn);
+		if (prod <= intf->req_cons)
+			notify_remote_via_evtchn(xen_store_evtchn);
 	}
 
-	return 0;
+	return bytes;
 }
 
-int xb_data_to_read(void)
+static int xb_data_to_read(void)
 {
 	struct xenstore_domain_interface *intf = xen_store_interface;
 	return (intf->rsp_cons != intf->rsp_prod);
 }
 
-int xb_wait_for_data_to_read(void)
-{
-	return wait_event_interruptible(xb_waitq, xb_data_to_read());
-}
-
-int xb_read(void *data, unsigned len)
+static int xb_read(void *data, unsigned int len)
 {
 	struct xenstore_domain_interface *intf = xen_store_interface;
 	XENSTORE_RING_IDX cons, prod;
-	int rc;
+	unsigned int bytes = 0;
 
 	while (len != 0) {
 		unsigned int avail;
 		const char *src;
 
-		rc = xb_wait_for_data_to_read();
-		if (rc < 0)
-			return rc;
-
 		/* Read indexes, then verify. */
 		cons = intf->rsp_cons;
 		prod = intf->rsp_prod;
+		if (cons == prod)
+			return bytes;
+
 		if (!check_indexes(cons, prod)) {
 			intf->rsp_cons = intf->rsp_prod = 0;
 			return -EIO;
@@ -185,17 +214,229 @@ int xb_read(void *data, unsigned len)
 		memcpy(data, src, avail);
 		data += avail;
 		len -= avail;
+		bytes += avail;
 
 		/* Other side must not see free space until we've copied out */
 		virt_mb();
 		intf->rsp_cons += avail;
 
-		pr_debug("Finished read of %i bytes (%i to go)\n", avail, len);
-
 		/* Implies mb(): other side will see the updated consumer. */
-		notify_remote_via_evtchn(xen_store_evtchn);
+		if (intf->rsp_prod - cons >= XENSTORE_RING_SIZE)
+			notify_remote_via_evtchn(xen_store_evtchn);
 	}
 
+	return bytes;
+}
+
+static int process_msg(void)
+{
+	static struct xs_thread_state_read state;
+	struct xb_req_data *req;
+	int err;
+	unsigned int len;
+
+	if (!state.in_msg) {
+		state.in_msg = true;
+		state.in_hdr = true;
+		state.used = 0;
+
+		/*
+		 * We must disallow save/restore while reading a message.
+		 * A partial read across s/r leaves us out of sync with
+		 * xenstored.
+		 */
+		mutex_lock(&xs_response_mutex);
+
+		if (!xb_data_to_read()) {
+			/* We raced with save/restore: pending data 'gone'. */
+			mutex_unlock(&xs_response_mutex);
+			state.in_msg = false;
+			return 0;
+		}
+	}

drivers/xen/xenbus/xenbus_comms.c int xb_read(void *data, unsigned len) · line 266

Quoted 10 lines
drivers/xen/xenbus/xenbus_comms.c +10 -0
+
+	if (state.in_hdr) {
+		if (state.used != sizeof(state.msg)) {
+			err = xb_read((void *)&state.msg + state.used,
+				      sizeof(state.msg) - state.used);
+			if (err < 0)
+				goto out;
+			state.used += err;
+			if (state.used != sizeof(state.msg))
+				return 0;

Would it be possible to do locking at the caller? I understand that you are trying to hold the lock across multiple invocations of this function but it feels somewhat counter-intuitive and bug-prone.

If it's not possible then at least please add a comment explaining locking algorithm.

drivers/xen/xenbus/xenbus_comms.c int xb_read(void *data, unsigned len) · line 277

Quoted 11 lines
drivers/xen/xenbus/xenbus_comms.c +11 -0
+			if (state.msg.len > XENSTORE_PAYLOAD_MAX) {
+				err = -EINVAL;
+				goto out;
+			}
+		}
+
+		len = state.msg.len + 1;
+		if (state.msg.type == XS_WATCH_EVENT)
+			len += sizeof(*state.watch);
+
+		state.alloc = kmalloc(len, GFP_NOIO | __GFP_HIGH);

Why can't you kmalloc to state.body only when type!=XS_WATCH_EVENT ?

Quoted 99 lines
drivers/xen/xenbus/xenbus_comms.c +99 -0
+		if (!state.alloc)
+			return -ENOMEM;
+
+		if (state.msg.type == XS_WATCH_EVENT)
+			state.body = state.watch->body;
+		else
+			state.body = state.alloc;
+		state.in_hdr = false;
+		state.used = 0;
+	}
+
+	err = xb_read(state.body + state.used, state.msg.len - state.used);
+	if (err < 0)
+		goto out;
+
+	state.used += err;
+	if (state.used != state.msg.len)
+		return 0;
+
+	state.body[state.msg.len] = '\0';
+
+	if (state.msg.type == XS_WATCH_EVENT) {
+		state.watch->len = state.msg.len;
+		err = xs_watch_msg(state.watch);
+	} else {
+		err = -ENOENT;
+		mutex_lock(&xb_write_mutex);
+		list_for_each_entry(req, &xs_reply_list, list) {
+			if (req->msg.req_id == state.msg.req_id) {
+				if (req->state == xb_req_state_wait_reply) {
+					req->msg.type = state.msg.type;
+					req->msg.len = state.msg.len;
+					req->body = state.body;
+					req->state = xb_req_state_got_reply;
+					list_del(&req->list);
+					req->cb(req);
+				} else {
+					list_del(&req->list);
+					kfree(req);
+				}
+				err = 0;
+				break;
+			}
+		}
+		mutex_unlock(&xb_write_mutex);
+		if (err)
+			goto out;
+	}
+
+	mutex_unlock(&xs_response_mutex);
+
+	state.in_msg = false;
+	state.alloc = NULL;
+	return err;
+
+ out:
+	mutex_unlock(&xs_response_mutex);
+	state.in_msg = false;
+	kfree(state.alloc);
+	state.alloc = NULL;
+	return err;
+}
+
+static int process_writes(void)
+{
+	static struct xs_thread_state_write state;
+	void *base;
+	unsigned int len;
+	int err = 0;
+
+	if (!xb_data_to_write())
+		return 0;
+
+	mutex_lock(&xb_write_mutex);
+
+	if (!state.req) {
+		state.req = list_first_entry(&xb_write_list,
+					     struct xb_req_data, list);
+		state.idx = -1;
+		state.used = 0;
+	}
+
+	if (state.req->state == xb_req_state_aborted)
+		goto out_err;
+
+	while (state.idx < state.req->num_vecs) {
+		if (state.idx < 0) {
+			base = &state.req->msg;
+			len = sizeof(state.req->msg);
+		} else {
+			base = state.req->vec[state.idx].iov_base;
+			len = state.req->vec[state.idx].iov_len;
+		}
+		err = xb_write(base + state.used, len - state.used);
+		if (err < 0)
+			goto out_err;
+		state.used += err;
+		if (state.used != len)
+			goto out;

drivers/xen/xenbus/xenbus_comms.c int xb_read(void *data, unsigned len) · line 386

Quoted 10 lines
drivers/xen/xenbus/xenbus_comms.c +10 -0
+
+		state.idx++;
+		state.used = 0;
+	}
+
+	/*
+	 * You would expect the following to be racy, but as the response is
+	 * being read by our thread there is no risk of req being freed
+	 * under our feet.
+	 */

I don't think I understand this (and it's missing a "so" or something like that between "thread" and "there"). If this is not racy, why are we doing this under xb_write_mutex?

drivers/xen/xenbus/xenbus_comms.c int xb_read(void *data, unsigned len) · line 399

Quoted 13 lines
drivers/xen/xenbus/xenbus_comms.c +13 -0
+	list_del(&state.req->list);
+	state.req->state = xb_req_state_wait_reply;
+	list_add_tail(&state.req->list, &xs_reply_list);
+	state.req = NULL;
+
+ out:
+	mutex_unlock(&xb_write_mutex);
+
+	return 0;
+
+ out_err:
+	state.req->msg.type = XS_ERROR;
+	state.req->err = err;

You don't seem to need this for xb_req_state_aborted since you are freeing state_req. OTOH, why shouldn't aborted requests generate an error reply as well?

Quoted 27 lines
drivers/xen/xenbus/xenbus_comms.c +27 -0
+	list_del(&state.req->list);
+	if (state.req->state == xb_req_state_aborted)
+		kfree(state.req);
+	else {
+		state.req->state = xb_req_state_got_reply;
+		wake_up(&state.req->wq);
+	}
+
+	mutex_unlock(&xb_write_mutex);
+
+	state.req = NULL;
+
+	return err;
+}
+
+static int xb_thread_work(void)
+{
+	return xb_data_to_read() || xb_data_to_write();
+}
+
+static int xenbus_thread(void *unused)
+{
+	int err;
+
+	while (!kthread_should_stop()) {
+		if (wait_event_interruptible(xb_waitq, xb_thread_work()))
+			continue;

drivers/xen/xenbus/xenbus_comms.c int xb_read(void *data, unsigned len) · line 436

Quoted 10 lines
drivers/xen/xenbus/xenbus_comms.c +10 -0
+
+		err = process_msg();
+		if (err == -ENOMEM)
+			schedule();
+		else if (err)
+			pr_warn("error %d while reading message\n", err);
+
+		err = process_writes();
+		if (err)
+			pr_warn("error %d while writing message\n", err);

Is there a chance that errors are persistent and you then spam the log?

-boris

Quoted 33 lines
drivers/xen/xenbus/xenbus_comms.c +11 -0
+	}
+
+	xenbus_task = NULL;
 	return 0;
 }
 
@@ -223,6 +464,7 @@ int xb_init_comms(void)
 		rebind_evtchn_irq(xen_store_evtchn, xenbus_irq);
 	} else {
 		int err;
+
 		err = bind_evtchn_to_irqhandler(xen_store_evtchn, wake_waiting,
 						0, "xenbus", &xb_waitq);
 		if (err < 0) {
@@ -231,6 +473,13 @@ int xb_init_comms(void)
 		}
 
 		xenbus_irq = err;
+
+		if (!xenbus_task) {
+			xenbus_task = kthread_run(xenbus_thread, NULL,
+						  "xenbus");
+			if (IS_ERR(xenbus_task))
+				return PTR_ERR(xenbus_task);
+		}
 	}
 
 	return 0;
author maintainer
#18 lore raw

Re: [PATCH 3/3] xen: optimize xenbus driver for multiple concurrent xenstore accesses

Quoted 64 lines · Boris Ostrovsky
Quoted 63 lines · Juergen Gross

Handling of multiple concurrent Xenstore accesses through xenbus driver either from the kernel or user land is rather lame today: xenbus is capable to have one access active only at one point of time.

Rewrite xenbus to handle multiple requests concurrently by making use of the request id of the Xenstore protocol. This requires to:

- Instead of blocking inside xb_read() when trying to read data from the xenstore ring buffer do so only in the main loop of xenbus_thread().

- Instead of doing writes to the xenstore ring buffer in the context of the caller just queue the request and do the write in the dedicated xenbus thread.

- Instead of just forwarding the request id specified by the caller of xenbus to xenstore use a xenbus internal unique request id. This will allow multiple outstanding requests.

- Modify the locking scheme in order to allow multiple requests being active in parallel.

- Instead of waiting for the reply of a user's xenstore request after writing the request to the xenstore ring buffer return directly to the caller and do the waiting in the read path.

Additionally signal handling was optimized by avoiding waking up the xenbus thread or sending an event to Xenstore in case the addressed entity is known to be running already.

As a result communication with Xenstore is sped up by a factor of up to 5: depending on the request type (read or write) and the amount of data transferred the gain was at least 20% (small reads) and went up to a factor of 5 for large writes.

In the end some more rough edges of xenbus have been smoothed:

- Handling of memory shortage when reading from xenstore ring buffer in the xenbus driver was not optimal: it was busy looping and issuing a warning in each loop.

- In case of xenstore not running in dom0 but in a stubdom we end up with two xenbus threads running as the initialization of xenbus in dom0 expecting a local xenstored will be redone later when connecting to the xenstore domain. Up to now this was no problem as locking would prevent the two xenbus threads interfering with each other, but this was just a waste of kernel resources.

- An out of memory situation while writing to or reading from the xenstore ring buffer no longer will lead to a possible loss of synchronization with xenstore.

- The user read and write part are now interruptible by signals.

---
I'm aware that the changes are quite large. I thought about sending a version split into multiple patches, but a lot of lines would have been touched by more than one patch. I still have the multiple patch variant lying around - this patch is split into 11 smaller ones. While all steps of this larger series is operational some steps are not optimal as they are even slower than the original version of xenbus.

Quoted 4 lines
Quoted 1 line

Nevertheless I can send the large series if there are requests for it.

I will comment only on xen_comms changes for now since otherwise I am afraid it may be difficult to keep track of conversation.

Okay.

Quoted 16 lines
Quoted 16 lines
drivers/xen/xenbus/xenbus_comms.c +1 -0
index c21ec02..fa054ca 100644
@@ -34,6 +34,7 @@
 
 #include <linux/wait.h>
 #include <linux/interrupt.h>
+#include <linux/kthread.h>
 #include <linux/sched.h>
 #include <linux/err.h>
 #include <xen/xenbus.h>
@@ -42,11 +43,40 @@
 #include <xen/page.h>
 #include "xenbus.h"
 
Quoted 6 lines

drivers/xen/xenbus/xenbus_comms.c line 49

Quoted 4 lines
drivers/xen/xenbus/xenbus_comms.c +4 -0
+struct xs_thread_state_write {
+	struct xb_req_data *req;
+	int idx;
+	unsigned int used;

"written" or "sent"?

I don't mind.

Quoted 6 lines
Quoted 6 lines
drivers/xen/xenbus/xenbus_comms.c +6 -0
+};
+
+struct xs_thread_state_read {
+	struct xsd_sockmsg msg;
+	char *body;
+	union {
Quoted 8 lines

drivers/xen/xenbus/xenbus_comms.c line 60

Quoted 5 lines
drivers/xen/xenbus/xenbus_comms.c +5 -0
+		void *alloc;
+		struct xs_watch_event *watch;
+	};
+	bool in_msg;
+	bool in_hdr;

It may be better to keep track of which state we are in using a bitmap. Otherwise it easy to lose track of one or the other.

Hmm, really? It's rather easy:
in_msg: are we processing any message?
in_hdr: are we processing the message header (in_msg is true)?

Quoted 3 lines

drivers/xen/xenbus/xenbus_comms.c line 61

Quoted 1 line
drivers/xen/xenbus/xenbus_comms.c +1 -0
+	unsigned int used;

"read" or"received"?

Sure, can change.

Quoted 4 lines

drivers/xen/xenbus/xenbus_comms.c line 62

Quoted 1 line
drivers/xen/xenbus/xenbus_comms.c +1 -0
+};

Both of these are private to process_msg/process_write so perhaps they can be declared in those routines' scopes.

I can do this if you want.

Quoted 19 lines
Quoted 19 lines
drivers/xen/xenbus/xenbus_comms.c +2 -6
 		/* Read indexes, then verify. */
 		cons = intf->req_cons;
 		prod = intf->req_prod;
@@ -115,59 +146,57 @@ int xb_write(const void *data, unsigned len)
 			intf->req_cons = intf->req_prod = 0;
 			return -EIO;
 		}
-
-		dst = get_output_chunk(cons, prod, intf->req, &avail);
-		if (avail == 0)
-			continue;
-		if (avail > len)
-			avail = len;
+		if (!xb_data_to_write())
+			return bytes;
 
 		/* Must write data /after/ reading the consumer index. */
 		virt_mb();
 
Quoted 6 lines

drivers/xen/xenbus/xenbus_comms.c int xb_write(const void *data, unsigned len) · line 157

Quoted 3 lines
drivers/xen/xenbus/xenbus_comms.c +3 -0
+		dst = get_output_chunk(cons, prod, intf->req, &avail);
+		if (avail == 0)
+			continue;

Should we continue the loop here or return? We are waiting for the reader to get stuff off the ring.

avail == 0 can happen only if the reader just modified req_cons between us reading it to cons and testing for free space via xb_data_to_write(). So the (local) retry should happen only very rarely and exactly once between writing any further bytes. We could return, of course, but then the retry would happen anyway via the main thread loop.

Quoted 32 lines
Quoted 32 lines
drivers/xen/xenbus/xenbus_comms.c +32 -0
+static int process_msg(void)
+{
+	static struct xs_thread_state_read state;
+	struct xb_req_data *req;
+	int err;
+	unsigned int len;
+
+	if (!state.in_msg) {
+		state.in_msg = true;
+		state.in_hdr = true;
+		state.used = 0;
+
+		/*
+		 * We must disallow save/restore while reading a message.
+		 * A partial read across s/r leaves us out of sync with
+		 * xenstored.
+		 */
+		mutex_lock(&xs_response_mutex);
+
+		if (!xb_data_to_read()) {
+			/* We raced with save/restore: pending data 'gone'. */
+			mutex_unlock(&xs_response_mutex);
+			state.in_msg = false;
+			return 0;
+		}
+	}
+
+	if (state.in_hdr) {
+		if (state.used != sizeof(state.msg)) {
+			err = xb_read((void *)&state.msg + state.used,
+				      sizeof(state.msg) - state.used);
+			if (err < 0)
Quoted 8 lines

drivers/xen/xenbus/xenbus_comms.c int xb_read(void *data, unsigned len) · line 266

Quoted 4 lines
drivers/xen/xenbus/xenbus_comms.c +4 -0
+				goto out;
+			state.used += err;
+			if (state.used != sizeof(state.msg))
+				return 0;

Would it be possible to do locking at the caller? I understand that you are trying to hold the lock across multiple invocations of this function but it feels somewhat counter-intuitive and bug-prone.

I think that would be difficult.

Quoted 2 lines

If it's not possible then at least please add a comment explaining locking algorithm.

Okay. Something like:

/*
* xs_response_mutex is locked as long as we are processing one
* message. state.in_msg will be true as long as we are holding the
* lock in process_msg().
*/

Quoted 5 lines
Quoted 5 lines
drivers/xen/xenbus/xenbus_comms.c +5 -0
+			if (state.msg.len > XENSTORE_PAYLOAD_MAX) {
+				err = -EINVAL;
+				goto out;
+			}
+		}
Quoted 8 lines

drivers/xen/xenbus/xenbus_comms.c int xb_read(void *data, unsigned len) · line 277

Quoted 6 lines
drivers/xen/xenbus/xenbus_comms.c +6 -0
+
+		len = state.msg.len + 1;
+		if (state.msg.type == XS_WATCH_EVENT)
+			len += sizeof(*state.watch);
+
+		state.alloc = kmalloc(len, GFP_NOIO | __GFP_HIGH);

Why can't you kmalloc to state.body only when type!=XS_WATCH_EVENT ?

I need to read the watch data, too.

Quoted 53 lines
Quoted 53 lines
drivers/xen/xenbus/xenbus_comms.c +52 -0
+		if (!state.alloc)
+			return -ENOMEM;
+
+		if (state.msg.type == XS_WATCH_EVENT)
+			state.body = state.watch->body;
+		else
+			state.body = state.alloc;
+		state.in_hdr = false;
+		state.used = 0;
+	}
 
+static int process_writes(void)
+{
+	static struct xs_thread_state_write state;
+	void *base;
+	unsigned int len;
+	int err = 0;
+
+	if (!xb_data_to_write())
+		return 0;
+
+	mutex_lock(&xb_write_mutex);
+
+	if (!state.req) {
+		state.req = list_first_entry(&xb_write_list,
+					     struct xb_req_data, list);
+		state.idx = -1;
+		state.used = 0;
+	}
+
+	if (state.req->state == xb_req_state_aborted)
+		goto out_err;
+
+	while (state.idx < state.req->num_vecs) {
+		if (state.idx < 0) {
+			base = &state.req->msg;
+			len = sizeof(state.req->msg);
+		} else {
+			base = state.req->vec[state.idx].iov_base;
+			len = state.req->vec[state.idx].iov_len;
+		}
+		err = xb_write(base + state.used, len - state.used);
+		if (err < 0)
+			goto out_err;
+		state.used += err;
+		if (state.used != len)
+			goto out;
+
+		state.idx++;
+		state.used = 0;
+	}
+
+	/*
Quoted 8 lines

drivers/xen/xenbus/xenbus_comms.c int xb_read(void *data, unsigned len) · line 386

Quoted 4 lines
drivers/xen/xenbus/xenbus_comms.c +4 -0
+	 * You would expect the following to be racy, but as the response is
+	 * being read by our thread there is no risk of req being freed
+	 * under our feet.
+	 */

I don't think I understand this (and it's missing a "so" or something like that between "thread" and "there"). If this is not racy, why are we doing this under xb_write_mutex?

You are right. This was a problem in an intermediate stage of development, but now the freeing of req is done with xb_write_mutex held. I'll remove the comment.

Quoted 9 lines
Quoted 9 lines
drivers/xen/xenbus/xenbus_comms.c +9 -0
+	list_del(&state.req->list);
+	state.req->state = xb_req_state_wait_reply;
+	list_add_tail(&state.req->list, &xs_reply_list);
+	state.req = NULL;
+
+ out:
+	mutex_unlock(&xb_write_mutex);
+
+	return 0;
Quoted 8 lines

drivers/xen/xenbus/xenbus_comms.c int xb_read(void *data, unsigned len) · line 399

Quoted 4 lines
drivers/xen/xenbus/xenbus_comms.c +4 -0
+
+ out_err:
+	state.req->msg.type = XS_ERROR;
+	state.req->err = err;

You don't seem to need this for xb_req_state_aborted since you are freeing state_req. OTOH, why shouldn't aborted requests generate an error reply as well?

They do. Before setting xb_req_state_aborted a possible error is taken from req (see xs_wait_for_reply()). In case of an early error returned (EIO in read_reply()) there is nobody waiting for (another) response.

Quoted 31 lines
Quoted 31 lines
drivers/xen/xenbus/xenbus_comms.c +31 -0
+	list_del(&state.req->list);
+	if (state.req->state == xb_req_state_aborted)
+		kfree(state.req);
+	else {
+		state.req->state = xb_req_state_got_reply;
+		wake_up(&state.req->wq);
+	}
+
+	mutex_unlock(&xb_write_mutex);
+
+	state.req = NULL;
+
+	return err;
+}
+
+static int xb_thread_work(void)
+{
+	return xb_data_to_read() || xb_data_to_write();
+}
+
+static int xenbus_thread(void *unused)
+{
+	int err;
+
+	while (!kthread_should_stop()) {
+		if (wait_event_interruptible(xb_waitq, xb_thread_work()))
+			continue;
+
+		err = process_msg();
+		if (err == -ENOMEM)
+			schedule();
Quoted 8 lines

drivers/xen/xenbus/xenbus_comms.c int xb_read(void *data, unsigned len) · line 436

Quoted 6 lines
drivers/xen/xenbus/xenbus_comms.c +6 -0
+		else if (err)
+			pr_warn("error %d while reading message\n", err);
+
+		err = process_writes();
+		if (err)
+			pr_warn("error %d while writing message\n", err);

Is there a chance that errors are persistent and you then spam the log?

Only in case xenstored is spamming the ring buffer with illegal data. I believe this is rather improbable and we are doomed in this case anyway. OTOH it doesn't hurt to switch to pr_warn_ratelimited().

Quoted 1 line

-boris

Thanks for the comments!

Juergen

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

author maintainer
#19 lore raw

Re: [PATCH 3/3] xen: optimize xenbus driver for multiple concurrent xenstore accesses

In reply to Boris Ostrovsky
Quoted 64 lines · Boris Ostrovsky
Quoted 63 lines · Juergen Gross

Handling of multiple concurrent Xenstore accesses through xenbus driver either from the kernel or user land is rather lame today: xenbus is capable to have one access active only at one point of time.

Rewrite xenbus to handle multiple requests concurrently by making use of the request id of the Xenstore protocol. This requires to:

- Instead of blocking inside xb_read() when trying to read data from the xenstore ring buffer do so only in the main loop of xenbus_thread().

- Instead of doing writes to the xenstore ring buffer in the context of the caller just queue the request and do the write in the dedicated xenbus thread.

- Instead of just forwarding the request id specified by the caller of xenbus to xenstore use a xenbus internal unique request id. This will allow multiple outstanding requests.

- Modify the locking scheme in order to allow multiple requests being active in parallel.

- Instead of waiting for the reply of a user's xenstore request after writing the request to the xenstore ring buffer return directly to the caller and do the waiting in the read path.

Additionally signal handling was optimized by avoiding waking up the xenbus thread or sending an event to Xenstore in case the addressed entity is known to be running already.

As a result communication with Xenstore is sped up by a factor of up to 5: depending on the request type (read or write) and the amount of data transferred the gain was at least 20% (small reads) and went up to a factor of 5 for large writes.

In the end some more rough edges of xenbus have been smoothed:

- Handling of memory shortage when reading from xenstore ring buffer in the xenbus driver was not optimal: it was busy looping and issuing a warning in each loop.

- In case of xenstore not running in dom0 but in a stubdom we end up with two xenbus threads running as the initialization of xenbus in dom0 expecting a local xenstored will be redone later when connecting to the xenstore domain. Up to now this was no problem as locking would prevent the two xenbus threads interfering with each other, but this was just a waste of kernel resources.

- An out of memory situation while writing to or reading from the xenstore ring buffer no longer will lead to a possible loss of synchronization with xenstore.

- The user read and write part are now interruptible by signals.

---
I'm aware that the changes are quite large. I thought about sending a version split into multiple patches, but a lot of lines would have been touched by more than one patch. I still have the multiple patch variant lying around - this patch is split into 11 smaller ones. While all steps of this larger series is operational some steps are not optimal as they are even slower than the original version of xenbus.

Quoted 4 lines
Quoted 1 line

Nevertheless I can send the large series if there are requests for it.

I will comment only on xen_comms changes for now since otherwise I am afraid it may be difficult to keep track of conversation.

Okay.

Quoted 16 lines
Quoted 16 lines
drivers/xen/xenbus/xenbus_comms.c +1 -0
index c21ec02..fa054ca 100644
@@ -34,6 +34,7 @@
 
 #include <linux/wait.h>
 #include <linux/interrupt.h>
+#include <linux/kthread.h>
 #include <linux/sched.h>
 #include <linux/err.h>
 #include <xen/xenbus.h>
@@ -42,11 +43,40 @@
 #include <xen/page.h>
 #include "xenbus.h"
 
Quoted 6 lines

drivers/xen/xenbus/xenbus_comms.c line 49

Quoted 4 lines
drivers/xen/xenbus/xenbus_comms.c +4 -0
+struct xs_thread_state_write {
+	struct xb_req_data *req;
+	int idx;
+	unsigned int used;

"written" or "sent"?

I don't mind.

Quoted 6 lines
Quoted 6 lines
drivers/xen/xenbus/xenbus_comms.c +6 -0
+};
+
+struct xs_thread_state_read {
+	struct xsd_sockmsg msg;
+	char *body;
+	union {
Quoted 8 lines

drivers/xen/xenbus/xenbus_comms.c line 60

Quoted 5 lines
drivers/xen/xenbus/xenbus_comms.c +5 -0
+		void *alloc;
+		struct xs_watch_event *watch;
+	};
+	bool in_msg;
+	bool in_hdr;

It may be better to keep track of which state we are in using a bitmap. Otherwise it easy to lose track of one or the other.

Hmm, really? It's rather easy:
in_msg: are we processing any message?
in_hdr: are we processing the message header (in_msg is true)?

Quoted 3 lines

drivers/xen/xenbus/xenbus_comms.c line 61

Quoted 1 line
drivers/xen/xenbus/xenbus_comms.c +1 -0
+	unsigned int used;

"read" or"received"?

Sure, can change.

Quoted 4 lines

drivers/xen/xenbus/xenbus_comms.c line 62

Quoted 1 line
drivers/xen/xenbus/xenbus_comms.c +1 -0
+};

Both of these are private to process_msg/process_write so perhaps they can be declared in those routines' scopes.

I can do this if you want.

Quoted 19 lines
Quoted 19 lines
drivers/xen/xenbus/xenbus_comms.c +2 -6
 		/* Read indexes, then verify. */
 		cons = intf->req_cons;
 		prod = intf->req_prod;
@@ -115,59 +146,57 @@ int xb_write(const void *data, unsigned len)
 			intf->req_cons = intf->req_prod = 0;
 			return -EIO;
 		}
-
-		dst = get_output_chunk(cons, prod, intf->req, &avail);
-		if (avail == 0)
-			continue;
-		if (avail > len)
-			avail = len;
+		if (!xb_data_to_write())
+			return bytes;
 
 		/* Must write data /after/ reading the consumer index. */
 		virt_mb();
 
Quoted 6 lines

drivers/xen/xenbus/xenbus_comms.c int xb_write(const void *data, unsigned len) · line 157

Quoted 3 lines
drivers/xen/xenbus/xenbus_comms.c +3 -0
+		dst = get_output_chunk(cons, prod, intf->req, &avail);
+		if (avail == 0)
+			continue;

Should we continue the loop here or return? We are waiting for the reader to get stuff off the ring.

avail == 0 can happen only if the reader just modified req_cons between us reading it to cons and testing for free space via xb_data_to_write(). So the (local) retry should happen only very rarely and exactly once between writing any further bytes. We could return, of course, but then the retry would happen anyway via the main thread loop.

Quoted 32 lines
Quoted 32 lines
drivers/xen/xenbus/xenbus_comms.c +32 -0
+static int process_msg(void)
+{
+	static struct xs_thread_state_read state;
+	struct xb_req_data *req;
+	int err;
+	unsigned int len;
+
+	if (!state.in_msg) {
+		state.in_msg = true;
+		state.in_hdr = true;
+		state.used = 0;
+
+		/*
+		 * We must disallow save/restore while reading a message.
+		 * A partial read across s/r leaves us out of sync with
+		 * xenstored.
+		 */
+		mutex_lock(&xs_response_mutex);
+
+		if (!xb_data_to_read()) {
+			/* We raced with save/restore: pending data 'gone'. */
+			mutex_unlock(&xs_response_mutex);
+			state.in_msg = false;
+			return 0;
+		}
+	}
+
+	if (state.in_hdr) {
+		if (state.used != sizeof(state.msg)) {
+			err = xb_read((void *)&state.msg + state.used,
+				      sizeof(state.msg) - state.used);
+			if (err < 0)
Quoted 8 lines

drivers/xen/xenbus/xenbus_comms.c int xb_read(void *data, unsigned len) · line 266

Quoted 4 lines
drivers/xen/xenbus/xenbus_comms.c +4 -0
+				goto out;
+			state.used += err;
+			if (state.used != sizeof(state.msg))
+				return 0;

Would it be possible to do locking at the caller? I understand that you are trying to hold the lock across multiple invocations of this function but it feels somewhat counter-intuitive and bug-prone.

I think that would be difficult.

Quoted 2 lines

If it's not possible then at least please add a comment explaining locking algorithm.

Okay. Something like:

/*
* xs_response_mutex is locked as long as we are processing one
* message. state.in_msg will be true as long as we are holding the
* lock in process_msg().
*/

Quoted 5 lines
Quoted 5 lines
drivers/xen/xenbus/xenbus_comms.c +5 -0
+			if (state.msg.len > XENSTORE_PAYLOAD_MAX) {
+				err = -EINVAL;
+				goto out;
+			}
+		}
Quoted 8 lines

drivers/xen/xenbus/xenbus_comms.c int xb_read(void *data, unsigned len) · line 277

Quoted 6 lines
drivers/xen/xenbus/xenbus_comms.c +6 -0
+
+		len = state.msg.len + 1;
+		if (state.msg.type == XS_WATCH_EVENT)
+			len += sizeof(*state.watch);
+
+		state.alloc = kmalloc(len, GFP_NOIO | __GFP_HIGH);

Why can't you kmalloc to state.body only when type!=XS_WATCH_EVENT ?

I need to read the watch data, too.

Quoted 53 lines
Quoted 53 lines
drivers/xen/xenbus/xenbus_comms.c +52 -0
+		if (!state.alloc)
+			return -ENOMEM;
+
+		if (state.msg.type == XS_WATCH_EVENT)
+			state.body = state.watch->body;
+		else
+			state.body = state.alloc;
+		state.in_hdr = false;
+		state.used = 0;
+	}
 
+static int process_writes(void)
+{
+	static struct xs_thread_state_write state;
+	void *base;
+	unsigned int len;
+	int err = 0;
+
+	if (!xb_data_to_write())
+		return 0;
+
+	mutex_lock(&xb_write_mutex);
+
+	if (!state.req) {
+		state.req = list_first_entry(&xb_write_list,
+					     struct xb_req_data, list);
+		state.idx = -1;
+		state.used = 0;
+	}
+
+	if (state.req->state == xb_req_state_aborted)
+		goto out_err;
+
+	while (state.idx < state.req->num_vecs) {
+		if (state.idx < 0) {
+			base = &state.req->msg;
+			len = sizeof(state.req->msg);
+		} else {
+			base = state.req->vec[state.idx].iov_base;
+			len = state.req->vec[state.idx].iov_len;
+		}
+		err = xb_write(base + state.used, len - state.used);
+		if (err < 0)
+			goto out_err;
+		state.used += err;
+		if (state.used != len)
+			goto out;
+
+		state.idx++;
+		state.used = 0;
+	}
+
+	/*
Quoted 8 lines

drivers/xen/xenbus/xenbus_comms.c int xb_read(void *data, unsigned len) · line 386

Quoted 4 lines
drivers/xen/xenbus/xenbus_comms.c +4 -0
+	 * You would expect the following to be racy, but as the response is
+	 * being read by our thread there is no risk of req being freed
+	 * under our feet.
+	 */

I don't think I understand this (and it's missing a "so" or something like that between "thread" and "there"). If this is not racy, why are we doing this under xb_write_mutex?

You are right. This was a problem in an intermediate stage of development, but now the freeing of req is done with xb_write_mutex held. I'll remove the comment.

Quoted 9 lines
Quoted 9 lines
drivers/xen/xenbus/xenbus_comms.c +9 -0
+	list_del(&state.req->list);
+	state.req->state = xb_req_state_wait_reply;
+	list_add_tail(&state.req->list, &xs_reply_list);
+	state.req = NULL;
+
+ out:
+	mutex_unlock(&xb_write_mutex);
+
+	return 0;
Quoted 8 lines

drivers/xen/xenbus/xenbus_comms.c int xb_read(void *data, unsigned len) · line 399

Quoted 4 lines
drivers/xen/xenbus/xenbus_comms.c +4 -0
+
+ out_err:
+	state.req->msg.type = XS_ERROR;
+	state.req->err = err;

You don't seem to need this for xb_req_state_aborted since you are freeing state_req. OTOH, why shouldn't aborted requests generate an error reply as well?

They do. Before setting xb_req_state_aborted a possible error is taken from req (see xs_wait_for_reply()). In case of an early error returned (EIO in read_reply()) there is nobody waiting for (another) response.

Quoted 31 lines
Quoted 31 lines
drivers/xen/xenbus/xenbus_comms.c +31 -0
+	list_del(&state.req->list);
+	if (state.req->state == xb_req_state_aborted)
+		kfree(state.req);
+	else {
+		state.req->state = xb_req_state_got_reply;
+		wake_up(&state.req->wq);
+	}
+
+	mutex_unlock(&xb_write_mutex);
+
+	state.req = NULL;
+
+	return err;
+}
+
+static int xb_thread_work(void)
+{
+	return xb_data_to_read() || xb_data_to_write();
+}
+
+static int xenbus_thread(void *unused)
+{
+	int err;
+
+	while (!kthread_should_stop()) {
+		if (wait_event_interruptible(xb_waitq, xb_thread_work()))
+			continue;
+
+		err = process_msg();
+		if (err == -ENOMEM)
+			schedule();
Quoted 8 lines

drivers/xen/xenbus/xenbus_comms.c int xb_read(void *data, unsigned len) · line 436

Quoted 6 lines
drivers/xen/xenbus/xenbus_comms.c +6 -0
+		else if (err)
+			pr_warn("error %d while reading message\n", err);
+
+		err = process_writes();
+		if (err)
+			pr_warn("error %d while writing message\n", err);

Is there a chance that errors are persistent and you then spam the log?

Only in case xenstored is spamming the ring buffer with illegal data. I believe this is rather improbable and we are doomed in this case anyway. OTOH it doesn't hurt to switch to pr_warn_ratelimited().

Quoted 1 line

-boris

Thanks for the comments!

Juergen

#20 lore raw

Re: [PATCH 3/3] xen: optimize xenbus driver for multiple concurrent xenstore accesses

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
drivers/xen/xenbus/xenbus_comms.c +15 -0
+static int process_msg(void)
+{
+	static struct xs_thread_state_read state;
+	struct xb_req_data *req;
+	int err;
+	unsigned int len;
+
+	if (!state.in_msg) {
+		state.in_msg = true;
+		state.in_hdr = true;
+		state.used = 0;
+
+		/*
+		 * We must disallow save/restore while reading a message.
+		 * A partial read across s/r leaves us out of sync with
Quoted 8 lines
Quoted 8 lines

drivers/xen/xenbus/xenbus_comms.c int xb_read(void *data, unsigned len) · line 253

Quoted 8 lines
drivers/xen/xenbus/xenbus_comms.c +8 -0
+		 * xenstored.
+		 */
+		mutex_lock(&xs_response_mutex);
+
+		if (!xb_data_to_read()) {
+			/* We raced with save/restore: pending data 'gone'. */
+			mutex_unlock(&xs_response_mutex);
+			state.in_msg = false;

Just noticed: should in_hdr be set to false here as well?

Quoted 2 lines
Quoted 2 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+			return 0;
+		}

Or set it to true here.

Quoted 16 lines
Quoted 6 lines
Quoted 6 lines
drivers/xen/xenbus/xenbus_comms.c +6 -0
+	}
+
+	if (state.in_hdr) {
+		if (state.used != sizeof(state.msg)) {
+			err = xb_read((void *)&state.msg + state.used,
+				      sizeof(state.msg) - state.used);
Quoted 8 lines

drivers/xen/xenbus/xenbus_comms.c int xb_read(void *data, unsigned len) · line 266

Quoted 5 lines
drivers/xen/xenbus/xenbus_comms.c +5 -0
+			if (err < 0)
+				goto out;
+			state.used += err;
+			if (state.used != sizeof(state.msg))
+				return 0;

Would it be possible to do locking at the caller? I understand that you are trying to hold the lock across multiple invocations of this function but it feels somewhat counter-intuitive and bug-prone.

I think that would be difficult.

Quoted 8 lines
Quoted 2 lines

If it's not possible then at least please add a comment explaining locking algorithm.

Okay. Something like:

/*
* xs_response_mutex is locked as long as we are processing one
* message. state.in_msg will be true as long as we are holding the
* lock in process_msg().

Then in_msg is the same as mutex_is_locked(&xs_response_mutex). And if so, do we really need it?

-boris

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

#21 lore raw

Re: [PATCH 3/3] xen: optimize xenbus driver for multiple concurrent xenstore accesses

In reply to Juergen Gross
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
drivers/xen/xenbus/xenbus_comms.c +15 -0
+static int process_msg(void)
+{
+	static struct xs_thread_state_read state;
+	struct xb_req_data *req;
+	int err;
+	unsigned int len;
+
+	if (!state.in_msg) {
+		state.in_msg = true;
+		state.in_hdr = true;
+		state.used = 0;
+
+		/*
+		 * We must disallow save/restore while reading a message.
+		 * A partial read across s/r leaves us out of sync with
Quoted 8 lines
Quoted 8 lines

drivers/xen/xenbus/xenbus_comms.c int xb_read(void *data, unsigned len) · line 253

Quoted 8 lines
drivers/xen/xenbus/xenbus_comms.c +8 -0
+		 * xenstored.
+		 */
+		mutex_lock(&xs_response_mutex);
+
+		if (!xb_data_to_read()) {
+			/* We raced with save/restore: pending data 'gone'. */
+			mutex_unlock(&xs_response_mutex);
+			state.in_msg = false;

Just noticed: should in_hdr be set to false here as well?

Quoted 2 lines
Quoted 2 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+			return 0;
+		}

Or set it to true here.

Quoted 16 lines
Quoted 6 lines
Quoted 6 lines
drivers/xen/xenbus/xenbus_comms.c +6 -0
+	}
+
+	if (state.in_hdr) {
+		if (state.used != sizeof(state.msg)) {
+			err = xb_read((void *)&state.msg + state.used,
+				      sizeof(state.msg) - state.used);
Quoted 8 lines

drivers/xen/xenbus/xenbus_comms.c int xb_read(void *data, unsigned len) · line 266

Quoted 5 lines
drivers/xen/xenbus/xenbus_comms.c +5 -0
+			if (err < 0)
+				goto out;
+			state.used += err;
+			if (state.used != sizeof(state.msg))
+				return 0;

Would it be possible to do locking at the caller? I understand that you are trying to hold the lock across multiple invocations of this function but it feels somewhat counter-intuitive and bug-prone.

I think that would be difficult.

Quoted 8 lines
Quoted 2 lines

If it's not possible then at least please add a comment explaining locking algorithm.

Okay. Something like:

/*
* xs_response_mutex is locked as long as we are processing one
* message. state.in_msg will be true as long as we are holding the
* lock in process_msg().

Then in_msg is the same as mutex_is_locked(&xs_response_mutex). And if so, do we really need it?

-boris

#22 lore raw

Re: [PATCH 3/3] xen: optimize xenbus driver for multiple concurrent xenstore accesses

Quoted 8 lines
Quoted 5 lines

/*
* xs_response_mutex is locked as long as we are processing one
* message. state.in_msg will be true as long as we are holding the
* lock in process_msg().

Then in_msg is the same as mutex_is_locked(&xs_response_mutex). And if so, do we really need it?

Nevermind this. The lock can be held by others, obviously.

-boris

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

#23 lore raw

Re: [PATCH 3/3] xen: optimize xenbus driver for multiple concurrent xenstore accesses

In reply to Boris Ostrovsky
Quoted 8 lines
Quoted 5 lines

/*
* xs_response_mutex is locked as long as we are processing one
* message. state.in_msg will be true as long as we are holding the
* lock in process_msg().

Then in_msg is the same as mutex_is_locked(&xs_response_mutex). And if so, do we really need it?

Nevermind this. The lock can be held by others, obviously.

-boris

author maintainer
#24 lore raw

Re: [PATCH 3/3] xen: optimize xenbus driver for multiple concurrent xenstore accesses

In reply to Boris Ostrovsky
Quoted 18 lines · Boris Ostrovsky
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
drivers/xen/xenbus/xenbus_comms.c +17 -0
+static int process_msg(void)
+{
+	static struct xs_thread_state_read state;
+	struct xb_req_data *req;
+	int err;
+	unsigned int len;
+
+	if (!state.in_msg) {
+		state.in_msg = true;
+		state.in_hdr = true;
+		state.used = 0;
+
+		/*
+		 * We must disallow save/restore while reading a message.
+		 * A partial read across s/r leaves us out of sync with
+		 * xenstored.
+		 */
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines

drivers/xen/xenbus/xenbus_comms.c int xb_read(void *data, unsigned len) · line 253

Quoted 6 lines
drivers/xen/xenbus/xenbus_comms.c +6 -0
+		mutex_lock(&xs_response_mutex);
+
+		if (!xb_data_to_read()) {
+			/* We raced with save/restore: pending data 'gone'. */
+			mutex_unlock(&xs_response_mutex);
+			state.in_msg = false;

Just noticed: should in_hdr be set to false here as well?

Doesn't matter: It is valid only if in_msg is true.

Quoted 26 lines
Quoted 3 lines
Quoted 3 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+			return 0;
+		}

Or set it to true here.

Quoted 20 lines
Quoted 6 lines
Quoted 6 lines
drivers/xen/xenbus/xenbus_comms.c +6 -0
+	}
+
+	if (state.in_hdr) {
+		if (state.used != sizeof(state.msg)) {
+			err = xb_read((void *)&state.msg + state.used,
+				      sizeof(state.msg) - state.used);
Quoted 8 lines

drivers/xen/xenbus/xenbus_comms.c int xb_read(void *data, unsigned len) · line 266

Quoted 5 lines
drivers/xen/xenbus/xenbus_comms.c +5 -0
+			if (err < 0)
+				goto out;
+			state.used += err;
+			if (state.used != sizeof(state.msg))
+				return 0;

Would it be possible to do locking at the caller? I understand that you are trying to hold the lock across multiple invocations of this function but it feels somewhat counter-intuitive and bug-prone.

I think that would be difficult.

Quoted 2 lines

If it's not possible then at least please add a comment explaining locking algorithm.

Okay. Something like:

Quoted 8 lines
Quoted 4 lines

/*
* xs_response_mutex is locked as long as we are processing one
* message. state.in_msg will be true as long as we are holding the
* lock in process_msg().

Then in_msg is the same as mutex_is_locked(&xs_response_mutex). And if so, do we really need it?

Yes. xs_response_mutex is used in suspend path, too.

Juergen

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

author maintainer
#25 lore raw

Re: [PATCH 3/3] xen: optimize xenbus driver for multiple concurrent xenstore accesses

In reply to Boris Ostrovsky
Quoted 18 lines · Boris Ostrovsky
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
drivers/xen/xenbus/xenbus_comms.c +17 -0
+static int process_msg(void)
+{
+	static struct xs_thread_state_read state;
+	struct xb_req_data *req;
+	int err;
+	unsigned int len;
+
+	if (!state.in_msg) {
+		state.in_msg = true;
+		state.in_hdr = true;
+		state.used = 0;
+
+		/*
+		 * We must disallow save/restore while reading a message.
+		 * A partial read across s/r leaves us out of sync with
+		 * xenstored.
+		 */
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines

drivers/xen/xenbus/xenbus_comms.c int xb_read(void *data, unsigned len) · line 253

Quoted 6 lines
drivers/xen/xenbus/xenbus_comms.c +6 -0
+		mutex_lock(&xs_response_mutex);
+
+		if (!xb_data_to_read()) {
+			/* We raced with save/restore: pending data 'gone'. */
+			mutex_unlock(&xs_response_mutex);
+			state.in_msg = false;

Just noticed: should in_hdr be set to false here as well?

Doesn't matter: It is valid only if in_msg is true.

Quoted 26 lines
Quoted 3 lines
Quoted 3 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+			return 0;
+		}

Or set it to true here.

Quoted 20 lines
Quoted 6 lines
Quoted 6 lines
drivers/xen/xenbus/xenbus_comms.c +6 -0
+	}
+
+	if (state.in_hdr) {
+		if (state.used != sizeof(state.msg)) {
+			err = xb_read((void *)&state.msg + state.used,
+				      sizeof(state.msg) - state.used);
Quoted 8 lines

drivers/xen/xenbus/xenbus_comms.c int xb_read(void *data, unsigned len) · line 266

Quoted 5 lines
drivers/xen/xenbus/xenbus_comms.c +5 -0
+			if (err < 0)
+				goto out;
+			state.used += err;
+			if (state.used != sizeof(state.msg))
+				return 0;

Would it be possible to do locking at the caller? I understand that you are trying to hold the lock across multiple invocations of this function but it feels somewhat counter-intuitive and bug-prone.

I think that would be difficult.

Quoted 2 lines

If it's not possible then at least please add a comment explaining locking algorithm.

Okay. Something like:

Quoted 8 lines
Quoted 4 lines

/*
* xs_response_mutex is locked as long as we are processing one
* message. state.in_msg will be true as long as we are holding the
* lock in process_msg().

Then in_msg is the same as mutex_is_locked(&xs_response_mutex). And if so, do we really need it?

Yes. xs_response_mutex is used in suspend path, too.

Juergen

#27 lore raw

Re: [PATCH 3/3] xen: optimize xenbus driver for multiple concurrent xenstore accesses

In reply to Juergen Gross

drivers/xen/xenbus/xenbus_dev_frontend.c

Quoted 6 lines
drivers/xen/xenbus/xenbus_dev_frontend.c +0 -0
index e4b9847..4d343ee 100644

LGTM.

-boris

#28 lore raw

Re: [PATCH 3/3] xen: optimize xenbus driver for multiple concurrent xenstore accesses

In reply to Juergen Gross
Quoted 19 lines
drivers/xen/xenbus/xenbus_xs.c +8 -2
index ebc768f..ebdfbee 100644
 
 
-
-static struct xs_handle xs_state;
+/*
+ * Framework to protect suspend/resume handling against normal Xenstore
+ * message handling:
+ * During suspend/resume there must be no open transaction and no pending
+ * Xenstore request.
+ * New watch events happening in this time can be ignored by firing all watches
+ * after resume.
+ */

drivers/xen/xenbus/xenbus_xs.c line 71

Quoted 10 lines
drivers/xen/xenbus/xenbus_xs.c +10 -0
+/* Lock protecting enter/exit critical region. */
+static DEFINE_SPINLOCK(xs_state_lock);
+/* Wait queue for all callers waiting for critical region to become usable. */
+static DECLARE_WAIT_QUEUE_HEAD(xs_state_enter_wq);
+/* Wait queue for suspend handling waiting for critical region being empty. */
+static DECLARE_WAIT_QUEUE_HEAD(xs_state_exit_wq);
+/* Number of users in critical region. */
+static unsigned int xs_state_users;
+/* Suspend handler waiting or already active? */
+static int xs_suspend_active;

I think these two should be declared next to xs_state _lock since they are protected by it. Or maybe even put them into some sort of a state struct.

drivers/xen/xenbus/xenbus_xs.c static bool xenbus_ok(void) · line 192

Quoted 9 lines
drivers/xen/xenbus/xenbus_xs.c +9 -0
+
+
+static bool test_reply(struct xb_req_data *req)
+{
+	if (req->state == xb_req_state_got_reply || !xenbus_ok())
+		return true;
+
+	/* Make sure to reread req->state each time. */
+	cpu_relax();

I don't think I understand why this is needed.

Quoted 26 lines
drivers/xen/xenbus/xenbus_xs.c +10 -11
+
+	return false;
+}
+
 
 
+static void xs_send(struct xb_req_data *req, struct xsd_sockmsg *msg)
 {
-	mutex_lock(&xs_state.transaction_mutex);
-	atomic_inc(&xs_state.transaction_count);
-	mutex_unlock(&xs_state.transaction_mutex);
-}
+	bool notify;
 
-static void transaction_end(void)
-{
-	if (atomic_dec_and_test(&xs_state.transaction_count))
-		wake_up(&xs_state.transaction_wq);
-}
+	req->msg = *msg;
+	req->err = 0;
+	req->state = xb_req_state_queued;
+	init_waitqueue_head(&req->wq);
 
-static void transaction_suspend(void)
-{

drivers/xen/xenbus/xenbus_xs.c static void *read_reply(enum xsd_sockmsg_type *type, unsigned int *len) · line 229

Quoted 10 lines
drivers/xen/xenbus/xenbus_xs.c +2 -7
-	mutex_lock(&xs_state.transaction_mutex);
-	wait_event(xs_state.transaction_wq,
-		   atomic_read(&xs_state.transaction_count) == 0);
-}
+	xs_request_enter(req);
 
-static void transaction_resume(void)
-{
-	mutex_unlock(&xs_state.transaction_mutex);
+	req->msg.req_id = xs_request_id++;

Is it safe to do this without a lock?

drivers/xen/xenbus/xenbus_xs.c static void *read_reply(enum xsd_sockmsg_type *type, unsigned int *len) · line 272

Quoted 7 lines
drivers/xen/xenbus/xenbus_xs.c +7 -0
+
+int xenbus_dev_request_and_reply(struct xsd_sockmsg *msg, void *par)
+{
+	struct xb_req_data *req;
+	struct kvec *vec;
+
+	req = kmalloc(sizeof(*req) + sizeof(*vec), GFP_KERNEL);

Is there a reason why you are using different flags here?

Quoted 10 lines
drivers/xen/xenbus/xenbus_xs.c +1 -0
@@ -263,11 +295,20 @@ static void *xs_talkv(struct xenbus_transaction t,
 		      unsigned int num_vecs,
 		      unsigned int *len)
 {
+	struct xb_req_data *req;
 	struct xsd_sockmsg msg;
 	void *ret = NULL;
 	unsigned int i;
 	int err;
 

drivers/xen/xenbus/xenbus_xs.c static void *xs_talkv(struct xenbus_transaction t, · line 313

Quoted 10 lines
drivers/xen/xenbus/xenbus_xs.c +8 -0
+	req = kmalloc(sizeof(*req), GFP_NOIO | __GFP_HIGH);
+	if (!req)
+		return ERR_PTR(-ENOMEM);
+
+	req->vec = iovec;
+	req->num_vecs = num_vecs;
+	req->cb = xs_wake_up;
+
 	msg.tx_id = t.id;
 	msg.req_id = 0;

Is this still needed? You are assigning it in xs_send().

Quoted 4 lines
drivers/xen/xenbus/xenbus_xs.c +2 -1
+static int xs_reboot_notify(struct notifier_block *nb,
+			    unsigned long code, void *unused)
 {
-	struct xs_stored_msg *msg;

drivers/xen/xenbus/xenbus_xs.c static int xenwatch_thread(void *unused) · line 897

Quoted 10 lines
drivers/xen/xenbus/xenbus_xs.c +7 -0
 
 
 
+	struct xb_req_data *req;
+
+	mutex_lock(&xb_write_mutex);
+	list_for_each_entry(req, &xs_reply_list, list)
+		wake_up(&req->wq);
+	list_for_each_entry(req, &xb_write_list, list)
+		wake_up(&req->wq);

We are waking up waiters here but there is not guarantee that waiting threads will have a chance to run, is there?

-boris

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

#29 lore raw

Re: [PATCH 3/3] xen: optimize xenbus driver for multiple concurrent xenstore accesses

In reply to Juergen Gross
Quoted 19 lines
drivers/xen/xenbus/xenbus_xs.c +8 -2
index ebc768f..ebdfbee 100644
 
 
-
-static struct xs_handle xs_state;
+/*
+ * Framework to protect suspend/resume handling against normal Xenstore
+ * message handling:
+ * During suspend/resume there must be no open transaction and no pending
+ * Xenstore request.
+ * New watch events happening in this time can be ignored by firing all watches
+ * after resume.
+ */

drivers/xen/xenbus/xenbus_xs.c line 71

Quoted 10 lines
drivers/xen/xenbus/xenbus_xs.c +10 -0
+/* Lock protecting enter/exit critical region. */
+static DEFINE_SPINLOCK(xs_state_lock);
+/* Wait queue for all callers waiting for critical region to become usable. */
+static DECLARE_WAIT_QUEUE_HEAD(xs_state_enter_wq);
+/* Wait queue for suspend handling waiting for critical region being empty. */
+static DECLARE_WAIT_QUEUE_HEAD(xs_state_exit_wq);
+/* Number of users in critical region. */
+static unsigned int xs_state_users;
+/* Suspend handler waiting or already active? */
+static int xs_suspend_active;

I think these two should be declared next to xs_state _lock since they are protected by it. Or maybe even put them into some sort of a state struct.

drivers/xen/xenbus/xenbus_xs.c static bool xenbus_ok(void) · line 192

Quoted 9 lines
drivers/xen/xenbus/xenbus_xs.c +9 -0
+
+
+static bool test_reply(struct xb_req_data *req)
+{
+	if (req->state == xb_req_state_got_reply || !xenbus_ok())
+		return true;
+
+	/* Make sure to reread req->state each time. */
+	cpu_relax();

I don't think I understand why this is needed.

Quoted 26 lines
drivers/xen/xenbus/xenbus_xs.c +10 -11
+
+	return false;
+}
+
 
 
+static void xs_send(struct xb_req_data *req, struct xsd_sockmsg *msg)
 {
-	mutex_lock(&xs_state.transaction_mutex);
-	atomic_inc(&xs_state.transaction_count);
-	mutex_unlock(&xs_state.transaction_mutex);
-}
+	bool notify;
 
-static void transaction_end(void)
-{
-	if (atomic_dec_and_test(&xs_state.transaction_count))
-		wake_up(&xs_state.transaction_wq);
-}
+	req->msg = *msg;
+	req->err = 0;
+	req->state = xb_req_state_queued;
+	init_waitqueue_head(&req->wq);
 
-static void transaction_suspend(void)
-{

drivers/xen/xenbus/xenbus_xs.c static void *read_reply(enum xsd_sockmsg_type *type, unsigned int *len) · line 229

Quoted 10 lines
drivers/xen/xenbus/xenbus_xs.c +2 -7
-	mutex_lock(&xs_state.transaction_mutex);
-	wait_event(xs_state.transaction_wq,
-		   atomic_read(&xs_state.transaction_count) == 0);
-}
+	xs_request_enter(req);
 
-static void transaction_resume(void)
-{
-	mutex_unlock(&xs_state.transaction_mutex);
+	req->msg.req_id = xs_request_id++;

Is it safe to do this without a lock?

drivers/xen/xenbus/xenbus_xs.c static void *read_reply(enum xsd_sockmsg_type *type, unsigned int *len) · line 272

Quoted 7 lines
drivers/xen/xenbus/xenbus_xs.c +7 -0
+
+int xenbus_dev_request_and_reply(struct xsd_sockmsg *msg, void *par)
+{
+	struct xb_req_data *req;
+	struct kvec *vec;
+
+	req = kmalloc(sizeof(*req) + sizeof(*vec), GFP_KERNEL);

Is there a reason why you are using different flags here?

Quoted 10 lines
drivers/xen/xenbus/xenbus_xs.c +1 -0
@@ -263,11 +295,20 @@ static void *xs_talkv(struct xenbus_transaction t,
 		      unsigned int num_vecs,
 		      unsigned int *len)
 {
+	struct xb_req_data *req;
 	struct xsd_sockmsg msg;
 	void *ret = NULL;
 	unsigned int i;
 	int err;
 

drivers/xen/xenbus/xenbus_xs.c static void *xs_talkv(struct xenbus_transaction t, · line 313

Quoted 10 lines
drivers/xen/xenbus/xenbus_xs.c +8 -0
+	req = kmalloc(sizeof(*req), GFP_NOIO | __GFP_HIGH);
+	if (!req)
+		return ERR_PTR(-ENOMEM);
+
+	req->vec = iovec;
+	req->num_vecs = num_vecs;
+	req->cb = xs_wake_up;
+
 	msg.tx_id = t.id;
 	msg.req_id = 0;

Is this still needed? You are assigning it in xs_send().

Quoted 4 lines
drivers/xen/xenbus/xenbus_xs.c +2 -1
+static int xs_reboot_notify(struct notifier_block *nb,
+			    unsigned long code, void *unused)
 {
-	struct xs_stored_msg *msg;

drivers/xen/xenbus/xenbus_xs.c static int xenwatch_thread(void *unused) · line 897

Quoted 10 lines
drivers/xen/xenbus/xenbus_xs.c +7 -0
 
 
 
+	struct xb_req_data *req;
+
+	mutex_lock(&xb_write_mutex);
+	list_for_each_entry(req, &xs_reply_list, list)
+		wake_up(&req->wq);
+	list_for_each_entry(req, &xb_write_list, list)
+		wake_up(&req->wq);

We are waking up waiters here but there is not guarantee that waiting threads will have a chance to run, is there?

-boris

author maintainer
#30 lore raw

Re: [PATCH 3/3] xen: optimize xenbus driver for multiple concurrent xenstore accesses

Quoted 25 lines · Boris Ostrovsky
Quoted 25 lines
drivers/xen/xenbus/xenbus_xs.c +14 -2
index ebc768f..ebdfbee 100644
 
 
-
-static struct xs_handle xs_state;
+/*
+ * Framework to protect suspend/resume handling against normal Xenstore
+ * message handling:
+ * During suspend/resume there must be no open transaction and no pending
+ * Xenstore request.
+ * New watch events happening in this time can be ignored by firing all watches
+ * after resume.
+ */
+/* Lock protecting enter/exit critical region. */
+static DEFINE_SPINLOCK(xs_state_lock);
+/* Wait queue for all callers waiting for critical region to become usable. */
+static DECLARE_WAIT_QUEUE_HEAD(xs_state_enter_wq);
+/* Wait queue for suspend handling waiting for critical region being empty. */
+static DECLARE_WAIT_QUEUE_HEAD(xs_state_exit_wq);
Quoted 8 lines

drivers/xen/xenbus/xenbus_xs.c line 71

Quoted 4 lines
drivers/xen/xenbus/xenbus_xs.c +4 -0
+/* Number of users in critical region. */
+static unsigned int xs_state_users;
+/* Suspend handler waiting or already active? */
+static int xs_suspend_active;

I think these two should be declared next to xs_state _lock since they are protected by it. Or maybe even put them into some sort of a state struct.

I think placing them near the lock and adding a comment is enough.

Quoted 11 lines

drivers/xen/xenbus/xenbus_xs.c static bool xenbus_ok(void) · line 192

Quoted 9 lines
drivers/xen/xenbus/xenbus_xs.c +9 -0
+
+
+static bool test_reply(struct xb_req_data *req)
+{
+	if (req->state == xb_req_state_got_reply || !xenbus_ok())
+		return true;
+
+	/* Make sure to reread req->state each time. */
+	cpu_relax();

I don't think I understand why this is needed.

I need a compiler barrier. Otherwise the compiler read req->state only once outside the while loop.

Quoted 32 lines
Quoted 32 lines
drivers/xen/xenbus/xenbus_xs.c +11 -15
+
+	return false;
+}
+
 
 
+static void xs_send(struct xb_req_data *req, struct xsd_sockmsg *msg)
 {
-	mutex_lock(&xs_state.transaction_mutex);
-	atomic_inc(&xs_state.transaction_count);
-	mutex_unlock(&xs_state.transaction_mutex);
-}
+	bool notify;
 
-static void transaction_end(void)
-{
-	if (atomic_dec_and_test(&xs_state.transaction_count))
-		wake_up(&xs_state.transaction_wq);
-}
+	req->msg = *msg;
+	req->err = 0;
+	req->state = xb_req_state_queued;
+	init_waitqueue_head(&req->wq);
 
-static void transaction_suspend(void)
-{
-	mutex_lock(&xs_state.transaction_mutex);
-	wait_event(xs_state.transaction_wq,
-		   atomic_read(&xs_state.transaction_count) == 0);
-}
+	xs_request_enter(req);
Quoted 6 lines

drivers/xen/xenbus/xenbus_xs.c static void *read_reply(enum xsd_sockmsg_type *type, unsigned int *len) · line 229

Quoted 4 lines
drivers/xen/xenbus/xenbus_xs.c +1 -3
-static void transaction_resume(void)
-{
-	mutex_unlock(&xs_state.transaction_mutex);
+	req->msg.req_id = xs_request_id++;

Is it safe to do this without a lock?

You are right: I should move this to xs_request_enter() inside the lock. I think I'll let return xs_request_enter() the request id.

Quoted 9 lines

drivers/xen/xenbus/xenbus_xs.c static void *read_reply(enum xsd_sockmsg_type *type, unsigned int *len) · line 272

Quoted 7 lines
drivers/xen/xenbus/xenbus_xs.c +7 -0
+
+int xenbus_dev_request_and_reply(struct xsd_sockmsg *msg, void *par)
+{
+	struct xb_req_data *req;
+	struct kvec *vec;
+
+	req = kmalloc(sizeof(*req) + sizeof(*vec), GFP_KERNEL);

Is there a reason why you are using different flags here?

Yes. This function is always called in user context. No need to be more restrictive.

Quoted 10 lines
Quoted 10 lines
drivers/xen/xenbus/xenbus_xs.c +1 -0
@@ -263,11 +295,20 @@ static void *xs_talkv(struct xenbus_transaction t,
 		      unsigned int num_vecs,
 		      unsigned int *len)
 {
+	struct xb_req_data *req;
 	struct xsd_sockmsg msg;
 	void *ret = NULL;
 	unsigned int i;
 	int err;
 
Quoted 12 lines

drivers/xen/xenbus/xenbus_xs.c static void *xs_talkv(struct xenbus_transaction t, · line 313

Quoted 10 lines
drivers/xen/xenbus/xenbus_xs.c +8 -0
+	req = kmalloc(sizeof(*req), GFP_NOIO | __GFP_HIGH);
+	if (!req)
+		return ERR_PTR(-ENOMEM);
+
+	req->vec = iovec;
+	req->num_vecs = num_vecs;
+	req->cb = xs_wake_up;
+
 	msg.tx_id = t.id;
 	msg.req_id = 0;

Is this still needed? You are assigning it in xs_send().

Right. Can be removed.

Quoted 7 lines
Quoted 4 lines
drivers/xen/xenbus/xenbus_xs.c +2 -1
+static int xs_reboot_notify(struct notifier_block *nb,
+			    unsigned long code, void *unused)
 {
-	struct xs_stored_msg *msg;
Quoted 10 lines

drivers/xen/xenbus/xenbus_xs.c static int xenwatch_thread(void *unused) · line 897

Quoted 7 lines
drivers/xen/xenbus/xenbus_xs.c +7 -0
+	struct xb_req_data *req;
+
+	mutex_lock(&xb_write_mutex);
+	list_for_each_entry(req, &xs_reply_list, list)
+		wake_up(&req->wq);
+	list_for_each_entry(req, &xb_write_list, list)
+		wake_up(&req->wq);

We are waking up waiters here but there is not guarantee that waiting threads will have a chance to run, is there?

You are right. But this isn't the point. We want to avoid blocking a reboot due to some needed thread waiting for xenstore. And this task is being accomplished here.

Juergen

author maintainer
#34 lore raw

Re: [PATCH 3/3] xen: optimize xenbus driver for multiple concurrent xenstore accesses

In reply to Boris Ostrovsky
Quoted 25 lines · Boris Ostrovsky
Quoted 25 lines
drivers/xen/xenbus/xenbus_xs.c +14 -2
index ebc768f..ebdfbee 100644
 
 
-
-static struct xs_handle xs_state;
+/*
+ * Framework to protect suspend/resume handling against normal Xenstore
+ * message handling:
+ * During suspend/resume there must be no open transaction and no pending
+ * Xenstore request.
+ * New watch events happening in this time can be ignored by firing all watches
+ * after resume.
+ */
+/* Lock protecting enter/exit critical region. */
+static DEFINE_SPINLOCK(xs_state_lock);
+/* Wait queue for all callers waiting for critical region to become usable. */
+static DECLARE_WAIT_QUEUE_HEAD(xs_state_enter_wq);
+/* Wait queue for suspend handling waiting for critical region being empty. */
+static DECLARE_WAIT_QUEUE_HEAD(xs_state_exit_wq);
Quoted 8 lines

drivers/xen/xenbus/xenbus_xs.c line 71

Quoted 4 lines
drivers/xen/xenbus/xenbus_xs.c +4 -0
+/* Number of users in critical region. */
+static unsigned int xs_state_users;
+/* Suspend handler waiting or already active? */
+static int xs_suspend_active;

I think these two should be declared next to xs_state _lock since they are protected by it. Or maybe even put them into some sort of a state struct.

I think placing them near the lock and adding a comment is enough.

Quoted 11 lines

drivers/xen/xenbus/xenbus_xs.c static bool xenbus_ok(void) · line 192

Quoted 9 lines
drivers/xen/xenbus/xenbus_xs.c +9 -0
+
+
+static bool test_reply(struct xb_req_data *req)
+{
+	if (req->state == xb_req_state_got_reply || !xenbus_ok())
+		return true;
+
+	/* Make sure to reread req->state each time. */
+	cpu_relax();

I don't think I understand why this is needed.

I need a compiler barrier. Otherwise the compiler read req->state only once outside the while loop.

Quoted 32 lines
Quoted 32 lines
drivers/xen/xenbus/xenbus_xs.c +11 -15
+
+	return false;
+}
+
 
 
+static void xs_send(struct xb_req_data *req, struct xsd_sockmsg *msg)
 {
-	mutex_lock(&xs_state.transaction_mutex);
-	atomic_inc(&xs_state.transaction_count);
-	mutex_unlock(&xs_state.transaction_mutex);
-}
+	bool notify;
 
-static void transaction_end(void)
-{
-	if (atomic_dec_and_test(&xs_state.transaction_count))
-		wake_up(&xs_state.transaction_wq);
-}
+	req->msg = *msg;
+	req->err = 0;
+	req->state = xb_req_state_queued;
+	init_waitqueue_head(&req->wq);
 
-static void transaction_suspend(void)
-{
-	mutex_lock(&xs_state.transaction_mutex);
-	wait_event(xs_state.transaction_wq,
-		   atomic_read(&xs_state.transaction_count) == 0);
-}
+	xs_request_enter(req);
Quoted 6 lines

drivers/xen/xenbus/xenbus_xs.c static void *read_reply(enum xsd_sockmsg_type *type, unsigned int *len) · line 229

Quoted 4 lines
drivers/xen/xenbus/xenbus_xs.c +1 -3
-static void transaction_resume(void)
-{
-	mutex_unlock(&xs_state.transaction_mutex);
+	req->msg.req_id = xs_request_id++;

Is it safe to do this without a lock?

You are right: I should move this to xs_request_enter() inside the lock. I think I'll let return xs_request_enter() the request id.

Quoted 9 lines

drivers/xen/xenbus/xenbus_xs.c static void *read_reply(enum xsd_sockmsg_type *type, unsigned int *len) · line 272

Quoted 7 lines
drivers/xen/xenbus/xenbus_xs.c +7 -0
+
+int xenbus_dev_request_and_reply(struct xsd_sockmsg *msg, void *par)
+{
+	struct xb_req_data *req;
+	struct kvec *vec;
+
+	req = kmalloc(sizeof(*req) + sizeof(*vec), GFP_KERNEL);

Is there a reason why you are using different flags here?

Yes. This function is always called in user context. No need to be more restrictive.

Quoted 10 lines
Quoted 10 lines
drivers/xen/xenbus/xenbus_xs.c +1 -0
@@ -263,11 +295,20 @@ static void *xs_talkv(struct xenbus_transaction t,
 		      unsigned int num_vecs,
 		      unsigned int *len)
 {
+	struct xb_req_data *req;
 	struct xsd_sockmsg msg;
 	void *ret = NULL;
 	unsigned int i;
 	int err;
 
Quoted 12 lines

drivers/xen/xenbus/xenbus_xs.c static void *xs_talkv(struct xenbus_transaction t, · line 313

Quoted 10 lines
drivers/xen/xenbus/xenbus_xs.c +8 -0
+	req = kmalloc(sizeof(*req), GFP_NOIO | __GFP_HIGH);
+	if (!req)
+		return ERR_PTR(-ENOMEM);
+
+	req->vec = iovec;
+	req->num_vecs = num_vecs;
+	req->cb = xs_wake_up;
+
 	msg.tx_id = t.id;
 	msg.req_id = 0;

Is this still needed? You are assigning it in xs_send().

Right. Can be removed.

Quoted 7 lines
Quoted 4 lines
drivers/xen/xenbus/xenbus_xs.c +2 -1
+static int xs_reboot_notify(struct notifier_block *nb,
+			    unsigned long code, void *unused)
 {
-	struct xs_stored_msg *msg;
Quoted 10 lines

drivers/xen/xenbus/xenbus_xs.c static int xenwatch_thread(void *unused) · line 897

Quoted 7 lines
drivers/xen/xenbus/xenbus_xs.c +7 -0
+	struct xb_req_data *req;
+
+	mutex_lock(&xb_write_mutex);
+	list_for_each_entry(req, &xs_reply_list, list)
+		wake_up(&req->wq);
+	list_for_each_entry(req, &xb_write_list, list)
+		wake_up(&req->wq);

We are waking up waiters here but there is not guarantee that waiting threads will have a chance to run, is there?

You are right. But this isn't the point. We want to avoid blocking a reboot due to some needed thread waiting for xenstore. And this task is being accomplished here.

Juergen

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

#31 lore raw

Re: [PATCH 3/3] xen: optimize xenbus driver for multiple concurrent xenstore accesses

In reply to Juergen Gross
Quoted 11 lines
Quoted 11 lines

drivers/xen/xenbus/xenbus_xs.c static bool xenbus_ok(void) · line 192

Quoted 10 lines
drivers/xen/xenbus/xenbus_xs.c +9 -0
+
+
+static bool test_reply(struct xb_req_data *req)
+{
+	if (req->state == xb_req_state_got_reply || !xenbus_ok())
+		return true;
+
+	/* Make sure to reread req->state each time. */
+	cpu_relax();

I don't think I understand why this is needed.

Quoted 2 lines

I need a compiler barrier. Otherwise the compiler read req->state only once outside the while loop.

Then barrier() looks the right primitive to use here. cpu_relax(), while doing what you want, is intended for other purposes.

Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
drivers/xen/xenbus/xenbus_xs.c +11 -15
+
+	return false;
+}
+
 
+static void xs_send(struct xb_req_data *req, struct xsd_sockmsg *msg)
 {
-	mutex_lock(&xs_state.transaction_mutex);
-	atomic_inc(&xs_state.transaction_count);
-	mutex_unlock(&xs_state.transaction_mutex);
-}
+	bool notify;
 
-static void transaction_end(void)
-{
-	if (atomic_dec_and_test(&xs_state.transaction_count))
-		wake_up(&xs_state.transaction_wq);
-}
+	req->msg = *msg;
+	req->err = 0;
+	req->state = xb_req_state_queued;
+	init_waitqueue_head(&req->wq);
 
-static void transaction_suspend(void)
-{
-	mutex_lock(&xs_state.transaction_mutex);
-	wait_event(xs_state.transaction_wq,
-		   atomic_read(&xs_state.transaction_count) == 0);
-}
+	xs_request_enter(req);
Quoted 7 lines
Quoted 5 lines

drivers/xen/xenbus/xenbus_xs.c static void *read_reply(enum xsd_sockmsg_type *type, unsigned int *len) · line 229

Quoted 4 lines
drivers/xen/xenbus/xenbus_xs.c +1 -3
-static void transaction_resume(void)
-{
-	mutex_unlock(&xs_state.transaction_mutex);
+	req->msg.req_id = xs_request_id++;

Is it safe to do this without a lock?

You are right: I should move this to xs_request_enter() inside the lock. I think I'll let return xs_request_enter() the request id.

Then please move xs_request_id's declaration close to xs_state_lock's declaration (just like you are going to move the two other state variables)

Quoted 16 lines
Quoted 7 lines
Quoted 5 lines
drivers/xen/xenbus/xenbus_xs.c +2 -1
+static int xs_reboot_notify(struct notifier_block *nb,
+			    unsigned long code, void *unused)
 {
-	struct xs_stored_msg *msg;
Quoted 9 lines

drivers/xen/xenbus/xenbus_xs.c static int xenwatch_thread(void *unused) · line 897

Quoted 7 lines
drivers/xen/xenbus/xenbus_xs.c +7 -0
+	struct xb_req_data *req;
+
+	mutex_lock(&xb_write_mutex);
+	list_for_each_entry(req, &xs_reply_list, list)
+		wake_up(&req->wq);
+	list_for_each_entry(req, &xb_write_list, list)
+		wake_up(&req->wq);

We are waking up waiters here but there is not guarantee that waiting threads will have a chance to run, is there?

Quoted 3 lines

You are right. But this isn't the point. We want to avoid blocking a reboot due to some needed thread waiting for xenstore. And this task is being accomplished here.

I think it's worth adding a comment mentioning this.

-boris

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

author maintainer
#32 lore raw

Re: [PATCH 3/3] xen: optimize xenbus driver for multiple concurrent xenstore accesses

Quoted 11 lines · Boris Ostrovsky
Quoted 11 lines
Quoted 11 lines

drivers/xen/xenbus/xenbus_xs.c static bool xenbus_ok(void) · line 192

Quoted 10 lines
drivers/xen/xenbus/xenbus_xs.c +9 -0
+
+
+static bool test_reply(struct xb_req_data *req)
+{
+	if (req->state == xb_req_state_got_reply || !xenbus_ok())
+		return true;
+
+	/* Make sure to reread req->state each time. */
+	cpu_relax();

I don't think I understand why this is needed.

Quoted 6 lines
Quoted 2 lines

I need a compiler barrier. Otherwise the compiler read req->state only once outside the while loop.

Then barrier() looks the right primitive to use here. cpu_relax(), while doing what you want, is intended for other purposes.

Hmm, yes, this sounds better.

Quoted 37 lines
Quoted 37 lines
Quoted 37 lines
Quoted 26 lines
drivers/xen/xenbus/xenbus_xs.c +10 -11
+
+	return false;
+}
+
 
+static void xs_send(struct xb_req_data *req, struct xsd_sockmsg *msg)
 {
-	mutex_lock(&xs_state.transaction_mutex);
-	atomic_inc(&xs_state.transaction_count);
-	mutex_unlock(&xs_state.transaction_mutex);
-}
+	bool notify;
 
-static void transaction_end(void)
-{
-	if (atomic_dec_and_test(&xs_state.transaction_count))
-		wake_up(&xs_state.transaction_wq);
-}
+	req->msg = *msg;
+	req->err = 0;
+	req->state = xb_req_state_queued;
+	init_waitqueue_head(&req->wq);
 
-static void transaction_suspend(void)
-{

drivers/xen/xenbus/xenbus_xs.c static void *read_reply(enum xsd_sockmsg_type *type, unsigned int *len) · line 229

Quoted 10 lines
drivers/xen/xenbus/xenbus_xs.c +2 -7
-	mutex_lock(&xs_state.transaction_mutex);
-	wait_event(xs_state.transaction_wq,
-		   atomic_read(&xs_state.transaction_count) == 0);
-}
+	xs_request_enter(req);
 
-static void transaction_resume(void)
-{
-	mutex_unlock(&xs_state.transaction_mutex);
+	req->msg.req_id = xs_request_id++;

Is it safe to do this without a lock?

Quoted 6 lines
Quoted 2 lines

You are right: I should move this to xs_request_enter() inside the lock. I think I'll let return xs_request_enter() the request id.

Then please move xs_request_id's declaration close to xs_state_lock's declaration (just like you are going to move the two other state variables)

Already done. :-)

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 4 lines
drivers/xen/xenbus/xenbus_xs.c +2 -0
+static int xs_reboot_notify(struct notifier_block *nb,
+			    unsigned long code, void *unused)
 {

drivers/xen/xenbus/xenbus_xs.c static int xenwatch_thread(void *unused) · line 897

Quoted 10 lines
drivers/xen/xenbus/xenbus_xs.c +7 -1
-	struct xs_stored_msg *msg;
 
 
+	struct xb_req_data *req;
+
+	mutex_lock(&xb_write_mutex);
+	list_for_each_entry(req, &xs_reply_list, list)
+		wake_up(&req->wq);
+	list_for_each_entry(req, &xb_write_list, list)
+		wake_up(&req->wq);

We are waking up waiters here but there is not guarantee that waiting threads will have a chance to run, is there?

Quoted 6 lines
Quoted 3 lines

You are right. But this isn't the point. We want to avoid blocking a reboot due to some needed thread waiting for xenstore. And this task is being accomplished here.

I think it's worth adding a comment mentioning this.

Okay.

Juergen

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

author maintainer
#33 lore raw

Re: [PATCH 3/3] xen: optimize xenbus driver for multiple concurrent xenstore accesses

In reply to Boris Ostrovsky
Quoted 11 lines · Boris Ostrovsky
Quoted 11 lines
Quoted 11 lines

drivers/xen/xenbus/xenbus_xs.c static bool xenbus_ok(void) · line 192

Quoted 10 lines
drivers/xen/xenbus/xenbus_xs.c +9 -0
+
+
+static bool test_reply(struct xb_req_data *req)
+{
+	if (req->state == xb_req_state_got_reply || !xenbus_ok())
+		return true;
+
+	/* Make sure to reread req->state each time. */
+	cpu_relax();

I don't think I understand why this is needed.

Quoted 6 lines
Quoted 2 lines

I need a compiler barrier. Otherwise the compiler read req->state only once outside the while loop.

Then barrier() looks the right primitive to use here. cpu_relax(), while doing what you want, is intended for other purposes.

Hmm, yes, this sounds better.

Quoted 37 lines
Quoted 37 lines
Quoted 37 lines
Quoted 26 lines
drivers/xen/xenbus/xenbus_xs.c +10 -11
+
+	return false;
+}
+
 
+static void xs_send(struct xb_req_data *req, struct xsd_sockmsg *msg)
 {
-	mutex_lock(&xs_state.transaction_mutex);
-	atomic_inc(&xs_state.transaction_count);
-	mutex_unlock(&xs_state.transaction_mutex);
-}
+	bool notify;
 
-static void transaction_end(void)
-{
-	if (atomic_dec_and_test(&xs_state.transaction_count))
-		wake_up(&xs_state.transaction_wq);
-}
+	req->msg = *msg;
+	req->err = 0;
+	req->state = xb_req_state_queued;
+	init_waitqueue_head(&req->wq);
 
-static void transaction_suspend(void)
-{

drivers/xen/xenbus/xenbus_xs.c static void *read_reply(enum xsd_sockmsg_type *type, unsigned int *len) · line 229

Quoted 10 lines
drivers/xen/xenbus/xenbus_xs.c +2 -7
-	mutex_lock(&xs_state.transaction_mutex);
-	wait_event(xs_state.transaction_wq,
-		   atomic_read(&xs_state.transaction_count) == 0);
-}
+	xs_request_enter(req);
 
-static void transaction_resume(void)
-{
-	mutex_unlock(&xs_state.transaction_mutex);
+	req->msg.req_id = xs_request_id++;

Is it safe to do this without a lock?

Quoted 6 lines
Quoted 2 lines

You are right: I should move this to xs_request_enter() inside the lock. I think I'll let return xs_request_enter() the request id.

Then please move xs_request_id's declaration close to xs_state_lock's declaration (just like you are going to move the two other state variables)

Already done. :-)

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 4 lines
drivers/xen/xenbus/xenbus_xs.c +2 -0
+static int xs_reboot_notify(struct notifier_block *nb,
+			    unsigned long code, void *unused)
 {

drivers/xen/xenbus/xenbus_xs.c static int xenwatch_thread(void *unused) · line 897

Quoted 10 lines
drivers/xen/xenbus/xenbus_xs.c +7 -1
-	struct xs_stored_msg *msg;
 
 
+	struct xb_req_data *req;
+
+	mutex_lock(&xb_write_mutex);
+	list_for_each_entry(req, &xs_reply_list, list)
+		wake_up(&req->wq);
+	list_for_each_entry(req, &xb_write_list, list)
+		wake_up(&req->wq);

We are waking up waiters here but there is not guarantee that waiting threads will have a chance to run, is there?

Quoted 6 lines
Quoted 3 lines

You are right. But this isn't the point. We want to avoid blocking a reboot due to some needed thread waiting for xenstore. And this task is being accomplished here.

I think it's worth adding a comment mentioning this.

Okay.

Juergen