CVS commit: src/sys/arch/xen/xenbus

2021-06-16 Thread Manuel Bouyer
Module Name:src
Committed By:   bouyer
Date:   Wed Jun 16 10:49:50 UTC 2021

Modified Files:
src/sys/arch/xen/xenbus: xenbus_probe.c

Log Message:
Grab KERNEL_LOCK before calling config_found()


To generate a diff of this commit:
cvs rdiff -u -r1.56 -r1.57 src/sys/arch/xen/xenbus/xenbus_probe.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/xen/xenbus/xenbus_probe.c
diff -u src/sys/arch/xen/xenbus/xenbus_probe.c:1.56 src/sys/arch/xen/xenbus/xenbus_probe.c:1.57
--- src/sys/arch/xen/xenbus/xenbus_probe.c:1.56	Sat Apr 24 23:36:52 2021
+++ src/sys/arch/xen/xenbus/xenbus_probe.c	Wed Jun 16 10:49:50 2021
@@ -1,4 +1,4 @@
-/* $NetBSD: xenbus_probe.c,v 1.56 2021/04/24 23:36:52 thorpej Exp $ */
+/* $NetBSD: xenbus_probe.c,v 1.57 2021/06/16 10:49:50 bouyer Exp $ */
 /**
  * Talks to Xen Store to figure out what devices we have.
  *
@@ -29,7 +29,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: xenbus_probe.c,v 1.56 2021/04/24 23:36:52 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xenbus_probe.c,v 1.57 2021/06/16 10:49:50 bouyer Exp $");
 
 #if 0
 #define DPRINTK(fmt, args...) \
@@ -469,8 +469,11 @@ xenbus_probe_device_type(const char *pat
 kmem_free(xbusd, xbusd->xbusd_sz);
 break;
 			}
+
+			KERNEL_LOCK(1, curlwp);
 			xbusd->xbusd_u.f.f_dev = config_found(xenbus_dev,
 			, xenbus_print, CFARG_EOL);
+			KERNEL_UNLOCK_ONE(curlwp);
 			if (xbusd->xbusd_u.f.f_dev == NULL) {
 kmem_free(xbusd, xbusd->xbusd_sz);
 continue;
@@ -652,8 +655,10 @@ xenbus_probe(void *unused)
 	register_xenbus_watch(_watch);
 
 	/* attach balloon. */
+	KERNEL_LOCK(1, curlwp);
 	config_found(xenbus_dev, _xa, xenbus_print,
 	CFARG_EOL);
+	KERNEL_UNLOCK_ONE(curlwp);
 
 	shutdown_xenbus_setup();
 



CVS commit: src/sys/arch/xen/xenbus

2020-05-14 Thread Jaromir Dolecek
Module Name:src
Committed By:   jdolecek
Date:   Thu May 14 13:18:55 UTC 2020

Modified Files:
src/sys/arch/xen/xenbus: xenbus_probe.c

Log Message:
fix compile when DPRINK() is defined


To generate a diff of this commit:
cvs rdiff -u -r1.53 -r1.54 src/sys/arch/xen/xenbus/xenbus_probe.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/xen/xenbus/xenbus_probe.c
diff -u src/sys/arch/xen/xenbus/xenbus_probe.c:1.53 src/sys/arch/xen/xenbus/xenbus_probe.c:1.54
--- src/sys/arch/xen/xenbus/xenbus_probe.c:1.53	Wed May 13 13:19:38 2020
+++ src/sys/arch/xen/xenbus/xenbus_probe.c	Thu May 14 13:18:55 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: xenbus_probe.c,v 1.53 2020/05/13 13:19:38 jdolecek Exp $ */
+/* $NetBSD: xenbus_probe.c,v 1.54 2020/05/14 13:18:55 jdolecek Exp $ */
 /**
  * Talks to Xen Store to figure out what devices we have.
  *
@@ -29,7 +29,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: xenbus_probe.c,v 1.53 2020/05/13 13:19:38 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xenbus_probe.c,v 1.54 2020/05/14 13:18:55 jdolecek Exp $");
 
 #if 0
 #define DPRINTK(fmt, args...) \
@@ -195,7 +195,7 @@ read_otherend_details(struct xenbus_devi
 		return err;
 	}
 	DPRINTK("read_otherend_details: read %s/%s returned %s\n",
-	xendev->xbusd_path, path_node, val);
+	xendev->xbusd_path, path_node, xendev->xbusd_otherend);
 
 	if (strlen(xendev->xbusd_otherend) == 0 ||
 	!xenbus_exists(NULL, xendev->xbusd_otherend, "")) {
@@ -322,7 +322,7 @@ xenbus_probe_device_type(const char *pat
 
 	DPRINTK("probe %s type %s", path, type);
 	err = xenbus_directory(NULL, path, "", _dir_n, );
-	DPRINTK("directory err %d dir_n %d", err, dir_n);
+	DPRINTK("directory err %d dir_n %d", err, orig_dir_n);
 	if (err)
 		return err;
 	dir_n = orig_dir_n;



CVS commit: src/sys/arch/xen/xenbus

2020-05-06 Thread Manuel Bouyer
Module Name:src
Committed By:   bouyer
Date:   Wed May  6 16:50:13 UTC 2020

Modified Files:
src/sys/arch/xen/xenbus: xenbus_comms.c xenbus_comms.h xenbus_probe.c
xenbus_xs.c

Log Message:
Convert remaining splfoo/splx and tsleep/wakeup to mutex and condvar.
Mark kernel threads and interrupt handlers MPSAFE.


To generate a diff of this commit:
cvs rdiff -u -r1.22 -r1.23 src/sys/arch/xen/xenbus/xenbus_comms.c
cvs rdiff -u -r1.6 -r1.7 src/sys/arch/xen/xenbus/xenbus_comms.h
cvs rdiff -u -r1.51 -r1.52 src/sys/arch/xen/xenbus/xenbus_probe.c
cvs rdiff -u -r1.26 -r1.27 src/sys/arch/xen/xenbus/xenbus_xs.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/xen/xenbus/xenbus_comms.c
diff -u src/sys/arch/xen/xenbus/xenbus_comms.c:1.22 src/sys/arch/xen/xenbus/xenbus_comms.c:1.23
--- src/sys/arch/xen/xenbus/xenbus_comms.c:1.22	Sat Apr 25 15:26:18 2020
+++ src/sys/arch/xen/xenbus/xenbus_comms.c	Wed May  6 16:50:13 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: xenbus_comms.c,v 1.22 2020/04/25 15:26:18 bouyer Exp $ */
+/* $NetBSD: xenbus_comms.c,v 1.23 2020/05/06 16:50:13 bouyer Exp $ */
 /**
  * xenbus_comms.c
  *
@@ -29,7 +29,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: xenbus_comms.c,v 1.22 2020/04/25 15:26:18 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xenbus_comms.c,v 1.23 2020/05/06 16:50:13 bouyer Exp $");
 
 #include 
 #include  
@@ -37,6 +37,7 @@ __KERNEL_RCSID(0, "$NetBSD: xenbus_comms
 #include 
 #include 
 #include 
+#include 
 
 #include 	/* for xendomain_is_dom0() */
 #include 	/* for xendomain_is_dom0() */
@@ -54,6 +55,8 @@ __KERNEL_RCSID(0, "$NetBSD: xenbus_comms
 
 static struct intrhand *ih;
 struct xenstore_domain_interface *xenstore_interface;
+static kmutex_t xenstore_lock;
+static kcondvar_t xenstore_cv;
 
 extern int xenstored_ready;
 // static DECLARE_WORK(probe_work, xenbus_probe, NULL);
@@ -76,11 +79,12 @@ static int
 wake_waiting(void *arg)
 {
 	if (__predict_false(xenstored_ready == 0 && xendomain_is_dom0())) {
-		xenstored_ready = 1;
-		wakeup(_ready);
+		xb_xenstored_make_ready();
 	} 
 
-	wakeup(_interface);
+	mutex_enter(_lock);
+	cv_broadcast(_cv);
+	mutex_exit(_lock);
 	return 1;
 }
 
@@ -118,16 +122,15 @@ xb_write(const void *data, unsigned len)
 	struct xenstore_domain_interface *intf = xenstore_domain_interface();
 	XENSTORE_RING_IDX cons, prod;
 
-	int s = spltty();
-
+	mutex_enter(_lock);
 	while (len != 0) {
 		void *dst;
 		unsigned int avail;
 
 		while ((intf->req_prod - intf->req_cons) == XENSTORE_RING_SIZE) {
-			XENPRINTF(("xb_write tsleep\n"));
-			tsleep(_interface, PRIBIO, "wrst", 0);
-			XENPRINTF(("xb_write tsleep done\n"));
+			XENPRINTF(("xb_write cv_wait\n"));
+			cv_wait(_cv, _lock);
+			XENPRINTF(("xb_write cv_wait done\n"));
 		}
 
 		/* Read indexes, then verify. */
@@ -135,7 +138,7 @@ xb_write(const void *data, unsigned len)
 		prod = intf->req_prod;
 		xen_rmb();
 		if (!check_indexes(cons, prod)) {
-			splx(s);
+			mutex_exit(_lock);
 			return EIO;
 		}
 
@@ -156,8 +159,7 @@ xb_write(const void *data, unsigned len)
 
 		hypervisor_notify_via_evtchn(xen_start_info.store_evtchn);
 	}
-
-	splx(s);
+	mutex_exit(_lock);
 	return 0;
 }
 
@@ -167,14 +169,14 @@ xb_read(void *data, unsigned len)
 	struct xenstore_domain_interface *intf = xenstore_domain_interface();
 	XENSTORE_RING_IDX cons, prod;
 
-	int s = spltty();
+	mutex_enter(_lock);
 
 	while (len != 0) {
 		unsigned int avail;
 		const char *src;
 
 		while (intf->rsp_cons == intf->rsp_prod)
-			tsleep(_interface, PRIBIO, "rdst", 0);
+			cv_wait(_cv, _lock);
 
 		/* Read indexes, then verify. */
 		cons = intf->rsp_cons;
@@ -182,7 +184,7 @@ xb_read(void *data, unsigned len)
 		xen_rmb();
 		if (!check_indexes(cons, prod)) {
 			XENPRINTF(("xb_read EIO\n"));
-			splx(s);
+			mutex_exit(_lock);
 			return EIO;
 		}
 
@@ -209,8 +211,7 @@ xb_read(void *data, unsigned len)
 
 		hypervisor_notify_via_evtchn(xen_start_info.store_evtchn);
 	}
-
-	splx(s);
+	mutex_exit(_lock);
 	return 0;
 }
 
@@ -220,10 +221,13 @@ xb_init_comms(device_t dev)
 {
 	int evtchn;
 
+	mutex_init(_lock, MUTEX_DEFAULT, IPL_TTY);
+	cv_init(_cv, "xsio");
+
 	evtchn = xen_start_info.store_evtchn;
 
 	ih = xen_intr_establish_xname(-1, _pic, evtchn, IST_LEVEL, IPL_TTY,
-	wake_waiting, NULL, false, device_xname(dev));
+	wake_waiting, NULL, true, device_xname(dev));
 
 	hypervisor_unmask_event(evtchn);
 	aprint_verbose_dev(dev, "using event channel %d\n", evtchn);

Index: src/sys/arch/xen/xenbus/xenbus_comms.h
diff -u src/sys/arch/xen/xenbus/xenbus_comms.h:1.6 src/sys/arch/xen/xenbus/xenbus_comms.h:1.7
--- src/sys/arch/xen/xenbus/xenbus_comms.h:1.6	Tue Sep 20 00:12:25 2011
+++ src/sys/arch/xen/xenbus/xenbus_comms.h	Wed May  6 16:50:13 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: xenbus_comms.h,v 1.6 2011/09/20 00:12:25 jym Exp $ */
+/* $NetBSD: 

CVS commit: src/sys/arch/xen/xenbus

2020-04-28 Thread Manuel Bouyer
Module Name:src
Committed By:   bouyer
Date:   Tue Apr 28 13:21:01 UTC 2020

Modified Files:
src/sys/arch/xen/xenbus: xenbus_probe.c

Log Message:
Skip block device  with device-type "cdrom", as their emulation can't be
disabled; and the backend driver doesn't handle them either.
Fix hang when booting with 'ioemu:hdc:cdrom' type disks.
While there convert some printf to aprint_error()


To generate a diff of this commit:
cvs rdiff -u -r1.50 -r1.51 src/sys/arch/xen/xenbus/xenbus_probe.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/xen/xenbus/xenbus_probe.c
diff -u src/sys/arch/xen/xenbus/xenbus_probe.c:1.50 src/sys/arch/xen/xenbus/xenbus_probe.c:1.51
--- src/sys/arch/xen/xenbus/xenbus_probe.c:1.50	Sat Apr 25 15:26:18 2020
+++ src/sys/arch/xen/xenbus/xenbus_probe.c	Tue Apr 28 13:21:01 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: xenbus_probe.c,v 1.50 2020/04/25 15:26:18 bouyer Exp $ */
+/* $NetBSD: xenbus_probe.c,v 1.51 2020/04/28 13:21:01 bouyer Exp $ */
 /**
  * Talks to Xen Store to figure out what devices we have.
  *
@@ -29,7 +29,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: xenbus_probe.c,v 1.50 2020/04/25 15:26:18 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xenbus_probe.c,v 1.51 2020/04/28 13:21:01 bouyer Exp $");
 
 #if 0
 #define DPRINTK(fmt, args...) \
@@ -399,7 +399,7 @@ xenbus_probe_device_type(const char *pat
 		err = xenbus_read_ul(NULL, xbusd->xbusd_path, "state",
 		, 10);
 		if (err) {
-			printf("xenbus: can't get state "
+			aprint_error_dev(xenbus_dev, "can't get state "
 			"for %s (%d)\n", xbusd->xbusd_path, err);
 			kmem_free(xbusd, xbusd->xbusd_sz);
 			err = 0;
@@ -418,8 +418,9 @@ xenbus_probe_device_type(const char *pat
 			xbusd->xbusd_type = XENBUS_BACKEND_DEVICE;
 			err = read_frontend_details(xbusd);
 			if (err != 0) {
-printf("xenbus: can't get frontend details "
-"for %s (%d)\n", xbusd->xbusd_path, err);
+aprint_error_dev(xenbus_dev,
+"can't get frontend details for %s (%d)\n",
+xbusd->xbusd_path, err);
 break;
 			}
 			if (create(xbusd)) {
@@ -432,16 +433,36 @@ xenbus_probe_device_type(const char *pat
 			xa.xa_type = type;
 			xa.xa_id = strtoul(dir[i], , 0);
 			if (dir[i][0] == '\0' || *ep != '\0') {
-printf("xenbus device type %s: id %s is not a"
-" number\n", type, dir[i]);
+aprint_error_dev(xenbus_dev,
+"device type %s: id %s is not a number\n",
+type, dir[i]);
 err = EFTYPE;
 kmem_free(xbusd, xbusd->xbusd_sz);
 break;
 			}
+			if (strcmp(xa.xa_type, "vbd") == 0) {
+char dtype[10];
+if (xenbus_read(NULL, xbusd->xbusd_path,
+"device-type", dtype, sizeof(dtype)) !=0) {
+	aprint_error_dev(xenbus_dev,
+	"%s: can't read device-type\n",
+	xbusd->xbusd_path);
+	kmem_free(xbusd, xbusd->xbusd_sz);
+	break;
+}
+if (strcmp(dtype, "cdrom") == 0) {
+	aprint_verbose_dev(xenbus_dev,
+	"ignoring %s type cdrom\n",
+	xbusd->xbusd_path);
+	kmem_free(xbusd, xbusd->xbusd_sz);
+	continue;
+}
+			}
 			err = read_backend_details(xbusd);
 			if (err != 0) {
-printf("xenbus: can't get backend details "
-"for %s (%d)\n", xbusd->xbusd_path, err);
+aprint_error_dev(xenbus_dev,
+"can't get backend details for %s (%d)\n",
+xbusd->xbusd_path, err);
 kmem_free(xbusd, xbusd->xbusd_sz);
 break;
 			}



CVS commit: src/sys/arch/xen/xenbus

2020-04-10 Thread Jaromir Dolecek
Module Name:src
Committed By:   jdolecek
Date:   Fri Apr 10 12:38:40 UTC 2020

Modified Files:
src/sys/arch/xen/xenbus: xenbus_probe.c

Log Message:
g/c unused xenwatch_mutex extern, and streq()


To generate a diff of this commit:
cvs rdiff -u -r1.46 -r1.47 src/sys/arch/xen/xenbus/xenbus_probe.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/xen/xenbus/xenbus_probe.c
diff -u src/sys/arch/xen/xenbus/xenbus_probe.c:1.46 src/sys/arch/xen/xenbus/xenbus_probe.c:1.47
--- src/sys/arch/xen/xenbus/xenbus_probe.c:1.46	Tue Apr  7 15:59:57 2020
+++ src/sys/arch/xen/xenbus/xenbus_probe.c	Fri Apr 10 12:38:40 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: xenbus_probe.c,v 1.46 2020/04/07 15:59:57 jdolecek Exp $ */
+/* $NetBSD: xenbus_probe.c,v 1.47 2020/04/10 12:38:40 jdolecek Exp $ */
 /**
  * Talks to Xen Store to figure out what devices we have.
  *
@@ -29,7 +29,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: xenbus_probe.c,v 1.46 2020/04/07 15:59:57 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xenbus_probe.c,v 1.47 2020/04/10 12:38:40 jdolecek Exp $");
 
 #if 0
 #define DPRINTK(fmt, args...) \
@@ -55,10 +55,6 @@ __KERNEL_RCSID(0, "$NetBSD: xenbus_probe
 
 #include "xenbus_comms.h"
 
-extern struct semaphore xenwatch_mutex;
-
-#define streq(a, b) (strcmp((a), (b)) == 0)
-
 static int  xenbus_match(device_t, cfdata_t, void *);
 static void xenbus_attach(device_t, device_t, void *);
 static int  xenbus_print(void *, const char *);



CVS commit: src/sys/arch/xen/xenbus

2020-04-07 Thread Jaromir Dolecek
Module Name:src
Committed By:   jdolecek
Date:   Tue Apr  7 15:43:42 UTC 2020

Modified Files:
src/sys/arch/xen/xenbus: xenbus_dev.c

Log Message:
mostly convert to kmem_alloc()

doing this, remove check for failed allocation with KM_SLEEP, and make
sure to not hold mutex during the call


To generate a diff of this commit:
cvs rdiff -u -r1.15 -r1.16 src/sys/arch/xen/xenbus/xenbus_dev.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/xen/xenbus/xenbus_dev.c
diff -u src/sys/arch/xen/xenbus/xenbus_dev.c:1.15 src/sys/arch/xen/xenbus/xenbus_dev.c:1.16
--- src/sys/arch/xen/xenbus/xenbus_dev.c:1.15	Tue Apr  7 08:14:43 2020
+++ src/sys/arch/xen/xenbus/xenbus_dev.c	Tue Apr  7 15:43:42 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: xenbus_dev.c,v 1.15 2020/04/07 08:14:43 jdolecek Exp $ */
+/* $NetBSD: xenbus_dev.c,v 1.16 2020/04/07 15:43:42 jdolecek Exp $ */
 /*
  * xenbus_dev.c
  * 
@@ -31,16 +31,16 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: xenbus_dev.c,v 1.15 2020/04/07 08:14:43 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xenbus_dev.c,v 1.16 2020/04/07 15:43:42 jdolecek Exp $");
 
 #include "opt_xen.h"
 
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -289,8 +289,7 @@ xenbus_dev_write(void *v)
 		err = xenbus_dev_request_and_reply(>u.msg, );
 		if (err == 0) {
 			if (xlwp->u.msg.type == XS_TRANSACTION_START) {
-trans = malloc(sizeof(*trans), M_DEVBUF,
-M_WAITOK);
+trans = kmem_alloc(sizeof(*trans), KM_SLEEP);
 trans->handle = (struct xenbus_transaction *)
 	strtoul(reply, NULL, 0);
 SLIST_INSERT_HEAD(>transactions,
@@ -308,7 +307,7 @@ xenbus_dev_write(void *v)
 }
 SLIST_REMOVE(>transactions, trans, 
 xenbus_dev_transaction, trans_next);
-free(trans, M_DEVBUF);
+kmem_free(trans, sizeof(*trans));
 			}
 			queue_reply(xlwp, (char *)>u.msg,
 		sizeof(xlwp->u.msg));
@@ -348,37 +347,50 @@ xenbus_dev_open(void *v)
 	mutex_enter(_dev_open_mtx);
 	u = kfs->kfs_v;
 	if (u == NULL) {
-		u = malloc(sizeof(*u), M_DEVBUF, M_WAITOK);
-		if (u == NULL) {  
-			mutex_exit(_dev_open_mtx);
-			return ENOMEM;
-		}
-		memset(u, 0, sizeof(*u));
+		mutex_exit(_dev_open_mtx);
+
+		u = kmem_zalloc(sizeof(*u), KM_SLEEP);
 		SLIST_INIT(>lwps); 
 		mutex_init(>mtx, MUTEX_DEFAULT, IPL_NONE);
-		kfs->kfs_v = u;   
+
+		mutex_enter(_dev_open_mtx);
+		/*
+		 * Must re-check if filled while waiting in alloc
+		 * by some other lwp.
+		 */
+		if (kfs->kfs_v) {
+			kmem_free(u, sizeof(*u));
+			u = kfs->kfs_v;
+		} else {
+			kfs->kfs_v = u;   
+		}
 	};
-	mutex_enter(>mtx);
 	mutex_exit(_dev_open_mtx);
+
+	mutex_enter(>mtx);
 	SLIST_FOREACH(xlwp, >lwps, lwp_next) {
 		if (xlwp->lwp == curlwp) {
 			break;
 		}
 	}
 	if (xlwp == NULL) {
-		xlwp = malloc(sizeof(*xlwp ), M_DEVBUF, M_WAITOK);  
-		if (xlwp  == NULL) {  
-			mutex_exit(>mtx);
-			return ENOMEM;
-		}
-		memset(xlwp, 0, sizeof(*xlwp));
+		mutex_exit(>mtx);
+
+		xlwp = kmem_zalloc(sizeof(*xlwp), KM_SLEEP);  
 		xlwp->lwp = curlwp;   
 		SLIST_INIT(>transactions);
 		mutex_init(>mtx, MUTEX_DEFAULT, IPL_NONE);
-		SLIST_INSERT_HEAD(>lwps,
-		xlwp, lwp_next);  
+
+		mutex_enter(>mtx);
+		/*
+		 * While alloc can block, this can't be re-entered with
+		 * curlwp, so no need to re-check. Also the node can't
+		 * be closed while we are blocked here.
+		 */
+		SLIST_INSERT_HEAD(>lwps, xlwp, lwp_next);  
 	}
 	mutex_exit(>mtx);
+
 	return 0;
 }
 
@@ -415,7 +427,7 @@ xenbus_dev_close(void *v)
 		trans = SLIST_FIRST(>transactions);
 		xenbus_transaction_end(trans->handle, 1);
 		SLIST_REMOVE_HEAD(>transactions, trans_next);
-		free(trans, M_DEVBUF);
+		kmem_free(trans, sizeof(*trans));
 	}
 	mutex_exit(>mtx);
 	SLIST_REMOVE(>lwps, xlwp, xenbus_dev_lwp, lwp_next);
@@ -430,8 +442,8 @@ xenbus_dev_close(void *v)
 	mutex_destroy(>mtx);
 	kfs->kfs_v = NULL;
 	mutex_exit(_dev_open_mtx);
-	free(xlwp, M_DEVBUF);
-	free(u, M_DEVBUF);
+	kmem_free(xlwp, sizeof(*xlwp));
+	kmem_free(u, sizeof(*u));
 	return 0;
 }
 



CVS commit: src/sys/arch/xen/xenbus

2020-04-07 Thread Jaromir Dolecek
Module Name:src
Committed By:   jdolecek
Date:   Tue Apr  7 15:16:52 UTC 2020

Modified Files:
src/sys/arch/xen/xenbus: xenbus_client.c

Log Message:
convert from malloc() to kmem_alloc()


To generate a diff of this commit:
cvs rdiff -u -r1.16 -r1.17 src/sys/arch/xen/xenbus/xenbus_client.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/xen/xenbus/xenbus_client.c
diff -u src/sys/arch/xen/xenbus/xenbus_client.c:1.16 src/sys/arch/xen/xenbus/xenbus_client.c:1.17
--- src/sys/arch/xen/xenbus/xenbus_client.c:1.16	Tue Apr  7 14:07:01 2020
+++ src/sys/arch/xen/xenbus/xenbus_client.c	Tue Apr  7 15:16:52 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: xenbus_client.c,v 1.16 2020/04/07 14:07:01 jdolecek Exp $ */
+/* $NetBSD: xenbus_client.c,v 1.17 2020/04/07 15:16:52 jdolecek Exp $ */
 /**
  * Client-facing interface for the Xenbus driver.  In other words, the
  * interface between the Xenbus and the device-specific code, be it the
@@ -29,7 +29,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: xenbus_client.c,v 1.16 2020/04/07 14:07:01 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xenbus_client.c,v 1.17 2020/04/07 15:16:52 jdolecek Exp $");
 
 #if 0
 #define DPRINTK(fmt, args...) \
@@ -41,7 +41,6 @@ __KERNEL_RCSID(0, "$NetBSD: xenbus_clien
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
 
@@ -129,13 +128,12 @@ xenbus_switch_state(struct xenbus_device
  * If the value returned is non-NULL, then it is the caller's to kfree.
  */
 static char *
-error_path(struct xenbus_device *dev)
+error_path(struct xenbus_device *dev, size_t *len)
 {
-	char *path_buffer = malloc(strlen("error/") + strlen(dev->xbusd_path) +
-1, M_DEVBUF, M_NOWAIT);
-	if (path_buffer == NULL) {
+	*len = strlen("error/") + strlen(dev->xbusd_path) + 1;
+	char *path_buffer = kmem_alloc(*len, KM_NOSLEEP);
+	if (path_buffer == NULL)
 		return NULL;
-	}
 
 	strcpy(path_buffer, "error/");
 	strcpy(path_buffer + strlen("error/"), dev->xbusd_path);
@@ -151,9 +149,10 @@ _dev_error(struct xenbus_device *dev, in
 	int ret __diagused;
 	unsigned int len;
 	char *printf_buffer = NULL, *path_buffer = NULL;
+	size_t path_buffer_sz = 0;
 
 #define PRINTF_BUFFER_SIZE 4096
-	printf_buffer = malloc(PRINTF_BUFFER_SIZE, M_DEVBUF, M_NOWAIT);
+	printf_buffer = kmem_alloc(PRINTF_BUFFER_SIZE, KM_NOSLEEP);
 	if (printf_buffer == NULL)
 		goto fail;
 
@@ -163,8 +162,7 @@ _dev_error(struct xenbus_device *dev, in
 	KASSERT(len + ret < PRINTF_BUFFER_SIZE);
 	dev->xbusd_has_error = 1;
 
-	path_buffer = error_path(dev);
-
+	path_buffer = error_path(dev, _buffer_sz);
 	if (path_buffer == NULL) {
 		printk("xenbus: failed to write error node for %s (%s)\n",
 		   dev->xbusd_path, printf_buffer);
@@ -179,9 +177,9 @@ _dev_error(struct xenbus_device *dev, in
 
 fail:
 	if (printf_buffer)
-		free(printf_buffer, M_DEVBUF);
+		kmem_free(printf_buffer, PRINTF_BUFFER_SIZE);
 	if (path_buffer)
-		free(path_buffer, M_DEVBUF);
+		kmem_free(path_buffer, path_buffer_sz);
 }
 
 



CVS commit: src/sys/arch/xen/xenbus

2019-02-26 Thread Joerg Sonnenberger
Module Name:src
Committed By:   joerg
Date:   Tue Feb 26 15:55:33 UTC 2019

Modified Files:
src/sys/arch/xen/xenbus: xenbus_probe.c

Log Message:
Compute storage size for a string correctly.


To generate a diff of this commit:
cvs rdiff -u -r1.39 -r1.40 src/sys/arch/xen/xenbus/xenbus_probe.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/xen/xenbus/xenbus_probe.c
diff -u src/sys/arch/xen/xenbus/xenbus_probe.c:1.39 src/sys/arch/xen/xenbus/xenbus_probe.c:1.40
--- src/sys/arch/xen/xenbus/xenbus_probe.c:1.39	Thu Jul  7 06:55:40 2016
+++ src/sys/arch/xen/xenbus/xenbus_probe.c	Tue Feb 26 15:55:33 2019
@@ -1,4 +1,4 @@
-/* $NetBSD: xenbus_probe.c,v 1.39 2016/07/07 06:55:40 msaitoh Exp $ */
+/* $NetBSD: xenbus_probe.c,v 1.40 2019/02/26 15:55:33 joerg Exp $ */
 /**
  * Talks to Xen Store to figure out what devices we have.
  *
@@ -29,7 +29,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: xenbus_probe.c,v 1.39 2016/07/07 06:55:40 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xenbus_probe.c,v 1.40 2019/02/26 15:55:33 joerg Exp $");
 
 #if 0
 #define DPRINTK(fmt, args...) \
@@ -623,11 +623,11 @@ xenbus_probe(void *unused)
 	xenbus_probe_backends();
 
 	/* Watch for changes. */
-	fe_watch.node = malloc(strlen("device" + 1), M_DEVBUF, M_NOWAIT);
+	fe_watch.node = malloc(strlen("device") + 1, M_DEVBUF, M_NOWAIT);
 	strcpy(fe_watch.node, "device");
 	fe_watch.xbw_callback = frontend_changed;
 	register_xenbus_watch(_watch);
-	be_watch.node = malloc(strlen("backend" + 1), M_DEVBUF, M_NOWAIT);
+	be_watch.node = malloc(strlen("backend") + 1, M_DEVBUF, M_NOWAIT);
 	strcpy(be_watch.node, "backend");
 	be_watch.xbw_callback = backend_changed;
 	register_xenbus_watch(_watch);



CVS commit: src/sys/arch/xen/xenbus

2017-11-12 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon Nov 13 02:12:29 UTC 2017

Modified Files:
src/sys/arch/xen/xenbus: xenbus_comms.c

Log Message:
Missed a spot: preserve known_mpsafe = (level != IPL_VM).

Noted by kre -- sorry!


To generate a diff of this commit:
cvs rdiff -u -r1.16 -r1.17 src/sys/arch/xen/xenbus/xenbus_comms.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/xen/xenbus/xenbus_comms.c
diff -u src/sys/arch/xen/xenbus/xenbus_comms.c:1.16 src/sys/arch/xen/xenbus/xenbus_comms.c:1.17
--- src/sys/arch/xen/xenbus/xenbus_comms.c:1.16	Mon Nov  6 15:27:09 2017
+++ src/sys/arch/xen/xenbus/xenbus_comms.c	Mon Nov 13 02:12:29 2017
@@ -1,4 +1,4 @@
-/* $NetBSD: xenbus_comms.c,v 1.16 2017/11/06 15:27:09 cherry Exp $ */
+/* $NetBSD: xenbus_comms.c,v 1.17 2017/11/13 02:12:29 riastradh Exp $ */
 /**
  * xenbus_comms.c
  *
@@ -29,7 +29,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: xenbus_comms.c,v 1.16 2017/11/06 15:27:09 cherry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xenbus_comms.c,v 1.17 2017/11/13 02:12:29 riastradh Exp $");
 
 #include 
 #include  
@@ -222,7 +222,7 @@ xb_init_comms(device_t dev)
 	evtchn = xen_start_info.store_evtchn;
 
 	ih = intr_establish_xname(0, _pic, evtchn, IST_LEVEL, IPL_TTY,
-	wake_waiting, NULL, true, "xenbus");
+	wake_waiting, NULL, false, "xenbus");
 
 	hypervisor_enable_event(evtchn);
 	aprint_verbose_dev(dev, "using event channel %d\n", evtchn);



CVS commit: src/sys/arch/xen/xenbus

2017-03-27 Thread Manuel Bouyer
Module Name:src
Committed By:   bouyer
Date:   Mon Mar 27 18:39:55 UTC 2017

Modified Files:
src/sys/arch/xen/xenbus: xenbus_dev.c

Log Message:
Avoid variable reassignement; reported by dcb...@hotmail.com in
PR port-xen/52112


To generate a diff of this commit:
cvs rdiff -u -r1.13 -r1.14 src/sys/arch/xen/xenbus/xenbus_dev.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/xen/xenbus/xenbus_dev.c
diff -u src/sys/arch/xen/xenbus/xenbus_dev.c:1.13 src/sys/arch/xen/xenbus/xenbus_dev.c:1.14
--- src/sys/arch/xen/xenbus/xenbus_dev.c:1.13	Thu Mar 23 14:52:36 2017
+++ src/sys/arch/xen/xenbus/xenbus_dev.c	Mon Mar 27 18:39:55 2017
@@ -1,4 +1,4 @@
-/* $NetBSD: xenbus_dev.c,v 1.13 2017/03/23 14:52:36 bouyer Exp $ */
+/* $NetBSD: xenbus_dev.c,v 1.14 2017/03/27 18:39:55 bouyer Exp $ */
 /*
  * xenbus_dev.c
  * 
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: xenbus_dev.c,v 1.13 2017/03/23 14:52:36 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xenbus_dev.c,v 1.14 2017/03/27 18:39:55 bouyer Exp $");
 
 #include "opt_xen.h"
 
@@ -392,7 +392,7 @@ xenbus_dev_close(void *v)
 	} */ *ap = v;
 	struct kernfs_node *kfs = VTOKERN(ap->a_vp);
 
-	struct xenbus_dev_data *u = kfs->kfs_v;
+	struct xenbus_dev_data *u;
 	struct xenbus_dev_lwp *xlwp;
 	struct xenbus_dev_transaction *trans;
 



CVS commit: src/sys/arch/xen/xenbus

2017-03-23 Thread Manuel Bouyer
Module Name:src
Committed By:   bouyer
Date:   Thu Mar 23 14:52:36 UTC 2017

Modified Files:
src/sys/arch/xen/xenbus: xenbus_dev.c

Log Message:
xlwp->mtx will never be used in interrupt context; set to IPL_NONE.
avoids a KASSERT when sleeping for the reply.


To generate a diff of this commit:
cvs rdiff -u -r1.12 -r1.13 src/sys/arch/xen/xenbus/xenbus_dev.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/xen/xenbus/xenbus_dev.c
diff -u src/sys/arch/xen/xenbus/xenbus_dev.c:1.12 src/sys/arch/xen/xenbus/xenbus_dev.c:1.13
--- src/sys/arch/xen/xenbus/xenbus_dev.c:1.12	Wed Mar 22 22:33:32 2017
+++ src/sys/arch/xen/xenbus/xenbus_dev.c	Thu Mar 23 14:52:36 2017
@@ -1,4 +1,4 @@
-/* $NetBSD: xenbus_dev.c,v 1.12 2017/03/22 22:33:32 bouyer Exp $ */
+/* $NetBSD: xenbus_dev.c,v 1.13 2017/03/23 14:52:36 bouyer Exp $ */
 /*
  * xenbus_dev.c
  * 
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: xenbus_dev.c,v 1.12 2017/03/22 22:33:32 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xenbus_dev.c,v 1.13 2017/03/23 14:52:36 bouyer Exp $");
 
 #include "opt_xen.h"
 
@@ -374,7 +374,7 @@ xenbus_dev_open(void *v)
 		memset(xlwp, 0, sizeof(*xlwp));
 		xlwp->lwp = curlwp;   
 		SLIST_INIT(>transactions);
-		mutex_init(>mtx, MUTEX_DEFAULT, IPL_VM);
+		mutex_init(>mtx, MUTEX_DEFAULT, IPL_NONE);
 		SLIST_INSERT_HEAD(>lwps,
 		xlwp, lwp_next);  
 	}



CVS commit: src/sys/arch/xen/xenbus

2017-03-22 Thread Manuel Bouyer
Module Name:src
Committed By:   bouyer
Date:   Wed Mar 22 22:33:32 UTC 2017

Modified Files:
src/sys/arch/xen/xenbus: xenbus_dev.c

Log Message:
On second through, it may be possible to have a NULL kfs_v in read and write
(if we're not the LWP which did the open).
Add the appropriate locks and checks.


To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/sys/arch/xen/xenbus/xenbus_dev.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/xen/xenbus/xenbus_dev.c
diff -u src/sys/arch/xen/xenbus/xenbus_dev.c:1.11 src/sys/arch/xen/xenbus/xenbus_dev.c:1.12
--- src/sys/arch/xen/xenbus/xenbus_dev.c:1.11	Wed Mar 22 21:21:39 2017
+++ src/sys/arch/xen/xenbus/xenbus_dev.c	Wed Mar 22 22:33:32 2017
@@ -1,4 +1,4 @@
-/* $NetBSD: xenbus_dev.c,v 1.11 2017/03/22 21:21:39 bouyer Exp $ */
+/* $NetBSD: xenbus_dev.c,v 1.12 2017/03/22 22:33:32 bouyer Exp $ */
 /*
  * xenbus_dev.c
  * 
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: xenbus_dev.c,v 1.11 2017/03/22 21:21:39 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xenbus_dev.c,v 1.12 2017/03/22 22:33:32 bouyer Exp $");
 
 #include "opt_xen.h"
 
@@ -153,13 +153,19 @@ xenbus_dev_read(void *v)
 	} */ *ap = v;
 	struct kernfs_node *kfs = VTOKERN(ap->a_vp);
 	struct uio *uio = ap->a_uio;
-	struct xenbus_dev_data *u = kfs->kfs_v;
+	struct xenbus_dev_data *u;
 	struct xenbus_dev_lwp *xlwp;
 	int err;
 	off_t offset;
 
-	KASSERT(u != NULL);
+	mutex_enter(_dev_open_mtx);
+	u = kfs->kfs_v;
+	if (u == NULL) {
+		mutex_exit(_dev_open_mtx);
+		return EBADF;
+	}
 	mutex_enter(>mtx);
+	mutex_exit(_dev_open_mtx);
 	SLIST_FOREACH(xlwp, >lwps, lwp_next) {
 		if (xlwp->lwp == curlwp) {
 			break;
@@ -221,15 +227,21 @@ xenbus_dev_write(void *v)
 	struct kernfs_node *kfs = VTOKERN(ap->a_vp);
 	struct uio *uio = ap->a_uio;
 
-	struct xenbus_dev_data *u = kfs->kfs_v;
+	struct xenbus_dev_data *u;
 	struct xenbus_dev_lwp *xlwp;
 	struct xenbus_dev_transaction *trans;
 	void *reply;
 	int err;
 	size_t size;
 
-	KASSERT(u != NULL);
+	mutex_enter(_dev_open_mtx);
+	u = kfs->kfs_v;
+	if (u == NULL) {
+		mutex_exit(_dev_open_mtx);
+		return EBADF;
+	}
 	mutex_enter(>mtx);
+	mutex_exit(_dev_open_mtx);
 	SLIST_FOREACH(xlwp, >lwps, lwp_next) {
 		if (xlwp->lwp == curlwp) {
 			break;



CVS commit: src/sys/arch/xen/xenbus

2017-03-22 Thread Manuel Bouyer
Module Name:src
Committed By:   bouyer
Date:   Wed Mar 22 21:21:40 UTC 2017

Modified Files:
src/sys/arch/xen/xenbus: xenbus_dev.c

Log Message:
Fix /kern/xen/xenbus handling. It's badly broken and will do bad things
if more than one thread tries to use it at the same time (hang, memory leak,
panic).
In a kernfs node, the fileops (open, close, read, write) gets a vnode,
but no way to know the file descriptor from which the request comes from.
As /kern/xen/xenbus interface is statefull (write sends a command and read
gets the response), a way to track "clients" is needed.
This commit implement states using the lwp pointer from the caller as a key.
It will fail (with an error) if a kernfs file descriptor is reused by a child
after a fork(), or if a file descriptor is shared by two threads of the same
process but fortunably the xen tools using this interface don't do this.

This should fixes occasional hangs of the Xen tools (one in "xbrd" state,
others in tstile) reported on port-xen by Alaric Snell-Pym.


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.11 src/sys/arch/xen/xenbus/xenbus_dev.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/xen/xenbus/xenbus_dev.c
diff -u src/sys/arch/xen/xenbus/xenbus_dev.c:1.10 src/sys/arch/xen/xenbus/xenbus_dev.c:1.11
--- src/sys/arch/xen/xenbus/xenbus_dev.c:1.10	Thu Jul  7 06:55:40 2016
+++ src/sys/arch/xen/xenbus/xenbus_dev.c	Wed Mar 22 21:21:39 2017
@@ -1,4 +1,4 @@
-/* $NetBSD: xenbus_dev.c,v 1.10 2016/07/07 06:55:40 msaitoh Exp $ */
+/* $NetBSD: xenbus_dev.c,v 1.11 2017/03/22 21:21:39 bouyer Exp $ */
 /*
  * xenbus_dev.c
  * 
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: xenbus_dev.c,v 1.10 2016/07/07 06:55:40 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xenbus_dev.c,v 1.11 2017/03/22 21:21:39 bouyer Exp $");
 
 #include "opt_xen.h"
 
@@ -61,11 +61,6 @@ static int xenbus_dev_open(void *);
 static int xenbus_dev_close(void *);
 static int xsd_port_read(void *);
 
-struct xenbus_dev_transaction {
-	SLIST_ENTRY(xenbus_dev_transaction) trans_next;
-	struct xenbus_transaction *handle;
-};
-
 #define DIR_MODE	 (S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
 #define PRIVCMD_MODE(S_IRUSR | S_IWUSR)
 static const struct kernfs_fileop xenbus_fileops[] = {
@@ -80,6 +75,8 @@ static const struct kernfs_fileop xsd_po
 { .kf_fileop = KERNFS_FILEOP_READ, .kf_vop = xsd_port_read },
 };
 
+static kmutex_t xenbus_dev_open_mtx;
+
 void
 xenbus_kernfs_init(void)
 {
@@ -99,26 +96,52 @@ xenbus_kernfs_init(void)
 		kfst, VREG, XSD_MODE);
 		kernfs_addentry(kernxen_pkt, dkt);
 	}
+	mutex_init(_dev_open_mtx, MUTEX_DEFAULT, IPL_NONE);
 }
 
-struct xenbus_dev_data {
+/*
+ * several process may open /kern/xen/xenbus in parallel.
+ * In a transaction one or more write is followed by one or more read.
+ * Unfortunably we don't get a file descriptor identifier down there,
+ * which we could use to link a read() to a transaction started in a write().
+ * To work around this we keep a list of lwp that opended the xenbus file.
+ * This assumes that a single lwp won't open /kern/xen/xenbus more
+ * than once, and that a transaction started by one lwp won't be ended
+ * by another.
+ * because of this, we also assume that we always got the data before
+ * the read() syscall.
+ */
+
+struct xenbus_dev_transaction {
+	SLIST_ENTRY(xenbus_dev_transaction) trans_next;
+	struct xenbus_transaction *handle;
+};
+
+struct xenbus_dev_lwp {
+	SLIST_ENTRY(xenbus_dev_lwp) lwp_next;
+	SLIST_HEAD(, xenbus_dev_transaction) transactions;
+	lwp_t *lwp;
+	/* Response queue. */
 #define BUFFER_SIZE (PAGE_SIZE)
 #define MASK_READ_IDX(idx) ((idx)&(BUFFER_SIZE-1))
-	/* In-progress transaction. */
-	SLIST_HEAD(, xenbus_dev_transaction) transactions;
-
+	char read_buffer[BUFFER_SIZE];
+	unsigned int read_cons, read_prod;
 	/* Partial request. */
 	unsigned int len;
 	union {
 		struct xsd_sockmsg msg;
 		char buffer[BUFFER_SIZE];
 	} u;
+	kmutex_t mtx;
+};
 
-	/* Response queue. */
-	char read_buffer[BUFFER_SIZE];
-	unsigned int read_cons, read_prod;
+struct xenbus_dev_data {
+	/* lwps which opended this device */
+	SLIST_HEAD(, xenbus_dev_lwp) lwps;
+	kmutex_t mtx;
 };
 
+
 static int
 xenbus_dev_read(void *v)
 {
@@ -131,47 +154,59 @@ xenbus_dev_read(void *v)
 	struct kernfs_node *kfs = VTOKERN(ap->a_vp);
 	struct uio *uio = ap->a_uio;
 	struct xenbus_dev_data *u = kfs->kfs_v;
+	struct xenbus_dev_lwp *xlwp;
 	int err;
 	off_t offset;
-	int s = spltty();
 
-	while (u->read_prod == u->read_cons) {
-		err = tsleep(u, PRIBIO | PCATCH, "xbrd", 0);
-		if (err)
-			goto end;
+	KASSERT(u != NULL);
+	mutex_enter(>mtx);
+	SLIST_FOREACH(xlwp, >lwps, lwp_next) {
+		if (xlwp->lwp == curlwp) {
+			break;
+		}
+	}
+	if (xlwp == NULL) {
+		mutex_exit(>mtx);
+		return EBADF;
+	}
+	mutex_enter(>mtx);
+	mutex_exit(>mtx);
+
+	if (xlwp->read_prod == xlwp->read_cons) {
+		err = 

CVS commit: src/sys/arch/xen/xenbus

2012-11-28 Thread Roger Pau Monne
Module Name:src
Committed By:   royger
Date:   Wed Nov 28 16:26:59 UTC 2012

Modified Files:
src/sys/arch/xen/xenbus: xenbus_xs.c

Log Message:
xen: prevent adding duplicate xenwatches

When a xenstore watch triggers, the event is processed on process_msg
and if a valid handle it's found the handler is queued for execution
on the pending xen watches queue (watch_events).

This may present a problem if we trigger a xenwatch several times and
then disconnect the device. If several xenwatch events are added to
the watch_events queue and the device is disconnected afterwards, the
first processed xenwatch event will disconnect the device, remove the
watch and free all resources. This triggers a panic if there are
pending xenwatch events for that device already queued in the local
queue of the function xenwatch_thread, since when the next watch that
has the same handler tries to execute we get a panic due to the fact
that the device is already disconnected and all resources had been
freed:

xenbus_watch: 0xab7cd1d0
xbw_callback: 0x80755dd4
otherend_changed: backend/vif/1/0
backend/vif/1/0/state 6
backend/vif/1/0 - Closed
backend/vif/1/0 - backend_device, b_detach: 0x8075a2bf
xenbus_watch: 0xab7cd1d0
xbw_callback: 0xfc5ec02183e547a8
fatal protection fault in supervisor mode
trap type 4 code 0 rip 80756596 cs e030 rflags 10246 cr2
7f7ff7b4c020 ilevel 0 rsp a000e6d82c50
curlwp 0xaa72d580 pid 0 lid 36 lowest kstack
0xa000e6d7f000
kernel: protection fault trap, code=0
Stopped in pid 0.36 (system) at netbsd:xenwatch_thread+0xc7:call
*10(%rax
)
xenwatch_thread() at netbsd:xenwatch_thread+0xc7
ds  f
es  5987
fs  2c40
gs  1460
rdi ab7cd1d0
rsi aa5477f0
rbp a000e6d82c70
rbx ab7c14c0
rdx 2
rcx f
rax ab7cd1d0
r8  78
r9  ffef
r10 deadbeef
r11 1
r12 a000e6d82c50
r13 aa72d580
r14 aa72d580
r15 0
rip 80756596xenwatch_thread+0xc7
cs  e030
rflags  10246
rsp a000e6d82c50
ss  e02b
netbsd:xenwatch_thread+0xc7:call*10(%rax)


To generate a diff of this commit:
cvs rdiff -u -r1.22 -r1.23 src/sys/arch/xen/xenbus/xenbus_xs.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/xen/xenbus/xenbus_xs.c
diff -u src/sys/arch/xen/xenbus/xenbus_xs.c:1.22 src/sys/arch/xen/xenbus/xenbus_xs.c:1.23
--- src/sys/arch/xen/xenbus/xenbus_xs.c:1.22	Wed Jul 27 23:11:23 2011
+++ src/sys/arch/xen/xenbus/xenbus_xs.c	Wed Nov 28 16:26:59 2012
@@ -1,4 +1,4 @@
-/* $NetBSD: xenbus_xs.c,v 1.22 2011/07/27 23:11:23 matt Exp $ */
+/* $NetBSD: xenbus_xs.c,v 1.23 2012/11/28 16:26:59 royger Exp $ */
 /**
  * xenbus_xs.c
  *
@@ -30,7 +30,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: xenbus_xs.c,v 1.22 2011/07/27 23:11:23 matt Exp $);
+__KERNEL_RCSID(0, $NetBSD: xenbus_xs.c,v 1.23 2012/11/28 16:26:59 royger Exp $);
 
 #if 0
 #define DPRINTK(fmt, args...) \
@@ -751,7 +751,7 @@ xenwatch_thread(void *unused)
 static int
 process_msg(void)
 {
-	struct xs_stored_msg *msg;
+	struct xs_stored_msg *msg, *s_msg;
 	char *body;
 	int err;
 
@@ -782,7 +782,7 @@ process_msg(void)
 	body[msg-hdr.len] = '\0';
 
 	if (msg-hdr.type == XS_WATCH_EVENT) {
-		bool found;
+		bool found, repeated;
 
 		DPRINTK(process_msg: XS_WATCH_EVENT);
 		msg-u.watch.vec = split(body, msg-hdr.len,
@@ -796,14 +796,24 @@ process_msg(void)
 		msg-u.watch.handle = find_watch(
 		msg-u.watch.vec[XS_WATCH_TOKEN]);
 		found = (msg-u.watch.handle != NULL);
+		repeated = false;
 		if (found) {
 			mutex_enter(watch_events_lock);
-			SIMPLEQ_INSERT_TAIL(watch_events, msg, msg_next);
-			cv_broadcast(watch_cv);
+			/* Don't add duplicate events to the queue of pending watches */
+			SIMPLEQ_FOREACH(s_msg, watch_events, msg_next) {
+if (s_msg-u.watch.handle == msg-u.watch.handle) {
+	repeated = true;
+	break;
+}
+			}
+			if (!repeated) {
+SIMPLEQ_INSERT_TAIL(watch_events, msg, msg_next);
+cv_broadcast(watch_cv);
+			}
 			mutex_exit(watch_events_lock);
 		}
 		mutex_exit(watches_lock);
-		if (!found) {
+		if (!found || repeated) {
 			free(msg-u.watch.vec, M_DEVBUF);
 			free(msg, M_DEVBUF);
 		}



CVS commit: src/sys/arch/xen/xenbus

2012-06-07 Thread Stephen Borrill
Module Name:src
Committed By:   sborrill
Date:   Thu Jun  7 14:05:14 UTC 2012

Modified Files:
src/sys/arch/xen/xenbus: xenbus_probe.c

Log Message:
Fix problem where devices with ID 0 were skipped as invalid as it didn't
distinguish between numerical zero and invalid numeric string.


To generate a diff of this commit:
cvs rdiff -u -r1.36 -r1.37 src/sys/arch/xen/xenbus/xenbus_probe.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/xen/xenbus/xenbus_probe.c
diff -u src/sys/arch/xen/xenbus/xenbus_probe.c:1.36 src/sys/arch/xen/xenbus/xenbus_probe.c:1.37
--- src/sys/arch/xen/xenbus/xenbus_probe.c:1.36	Tue Jun  5 10:58:56 2012
+++ src/sys/arch/xen/xenbus/xenbus_probe.c	Thu Jun  7 14:05:13 2012
@@ -1,4 +1,4 @@
-/* $NetBSD: xenbus_probe.c,v 1.36 2012/06/05 10:58:56 sborrill Exp $ */
+/* $NetBSD: xenbus_probe.c,v 1.37 2012/06/07 14:05:13 sborrill Exp $ */
 /**
  * Talks to Xen Store to figure out what devices we have.
  *
@@ -29,7 +29,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: xenbus_probe.c,v 1.36 2012/06/05 10:58:56 sborrill Exp $);
+__KERNEL_RCSID(0, $NetBSD: xenbus_probe.c,v 1.37 2012/06/07 14:05:13 sborrill Exp $);
 
 #if 0
 #define DPRINTK(fmt, args...) \
@@ -354,7 +354,11 @@ xenbus_probe_device_type(const char *pat
 
 		/* Convert string values to numeric; skip invalid */
 		for (i = 0; i  dir_n; i++) {
-			id[i] = strtoul(dir[i], ep, 10);
+			/*
+			 * Add one to differentiate numerical zero from invalid
+			 * string. Has no effect on sort order.
+			 */
+			id[i] = strtoul(dir[i], ep, 10) + 1;
 			if (dir[i][0] == '\0' || *ep != '\0')
 id[i] = 0;
 		}



CVS commit: src/sys/arch/xen/xenbus

2012-06-05 Thread Stephen Borrill
Module Name:src
Committed By:   sborrill
Date:   Tue Jun  5 10:58:57 UTC 2012

Modified Files:
src/sys/arch/xen/xenbus: xenbus_probe.c

Log Message:
Sort vif and vbd device IDs numerically so that attach order does not depend
on the order they are passed in through xenstore. While this works for
hand-crafted Xen configuration files, it does not work for XenServer, XCP or
EC2 instances. This means that adding an extra virtual disk can make the
domU unbootable.

ID is actually based on the Linux device major/minor so this approach isn't
entirely correct (for instance, you can specify devices to be non-contiguous
which doesn't fit too well with our autoconf approach), but it works as a
first approximation.

Tested by me on XenServer and riz@ on EC2. OK bouyer@


To generate a diff of this commit:
cvs rdiff -u -r1.35 -r1.36 src/sys/arch/xen/xenbus/xenbus_probe.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/xen/xenbus/xenbus_probe.c
diff -u src/sys/arch/xen/xenbus/xenbus_probe.c:1.35 src/sys/arch/xen/xenbus/xenbus_probe.c:1.36
--- src/sys/arch/xen/xenbus/xenbus_probe.c:1.35	Thu Sep 22 23:02:35 2011
+++ src/sys/arch/xen/xenbus/xenbus_probe.c	Tue Jun  5 10:58:56 2012
@@ -1,4 +1,4 @@
-/* $NetBSD: xenbus_probe.c,v 1.35 2011/09/22 23:02:35 jym Exp $ */
+/* $NetBSD: xenbus_probe.c,v 1.36 2012/06/05 10:58:56 sborrill Exp $ */
 /**
  * Talks to Xen Store to figure out what devices we have.
  *
@@ -29,7 +29,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: xenbus_probe.c,v 1.35 2011/09/22 23:02:35 jym Exp $);
+__KERNEL_RCSID(0, $NetBSD: xenbus_probe.c,v 1.36 2012/06/05 10:58:56 sborrill Exp $);
 
 #if 0
 #define DPRINTK(fmt, args...) \
@@ -321,7 +321,8 @@ static int
 xenbus_probe_device_type(const char *path, const char *type,
 int (*create)(struct xenbus_device *))
 {
-	int err, i, msize;
+	int err, i, pos, msize;
+	int *lookup = NULL;
 	unsigned long state;
 	char **dir;
 	unsigned int dir_n = 0;
@@ -335,8 +336,58 @@ xenbus_probe_device_type(const char *pat
 	if (err)
 		return err;
 
-	for (i = 0; i  dir_n; i++) {
+	/* Only sort frontend devices i.e. create == NULL*/
+	if (dir_n  1  create == NULL) {
+		int minp;
+		unsigned long minv;
+		unsigned long *id;
+
+		lookup = malloc(sizeof(int) * dir_n, M_DEVBUF,
+		M_WAITOK | M_ZERO);
+		if (lookup == NULL)
+			panic(can't malloc lookup);
+
+		id = malloc(sizeof(unsigned long) * dir_n, M_DEVBUF,
+		M_WAITOK | M_ZERO);
+		if (id == NULL)
+			panic(can't malloc id);
+
+		/* Convert string values to numeric; skip invalid */
+		for (i = 0; i  dir_n; i++) {
+			id[i] = strtoul(dir[i], ep, 10);
+			if (dir[i][0] == '\0' || *ep != '\0')
+id[i] = 0;
+		}
+		
+		/* Build lookup table in ascending order */
+		for (pos = 0; pos  dir_n; ) {
+			minv = UINT32_MAX;
+			minp = -1;
+			for (i = 0; i  dir_n; i++) {
+if (id[i]  minv  id[i]  0) {
+	minv = id[i];
+	minp = i;
+}
+			}
+			if (minp = 0) {
+lookup[pos++] = minp;
+id[minp] = 0;
+			}
+			else
+break;
+		}
+		
+		free(id, M_DEVBUF);
+		/* Adjust in case we had to skip non-numeric entries */
+		dir_n = pos;
+	}
+
+	for (pos = 0; pos  dir_n; pos++) {
 		err = 0;
+		if (lookup)
+			i = lookup[pos];
+		else
+			i = pos;
 		/*
 		 * add size of path to size of xenbus_device. xenbus_device
 		 * already has room for one char in xbusd_path.
@@ -413,6 +464,9 @@ xenbus_probe_device_type(const char *pat
 		watch_otherend(xbusd);
 	}
 	free(dir, M_DEVBUF);
+	if (lookup)
+		free(lookup, M_DEVBUF);
+	
 	return err;
 }
 



CVS commit: src/sys/arch/xen/xenbus

2011-07-27 Thread Matt Thomas
Module Name:src
Committed By:   matt
Date:   Wed Jul 27 23:11:24 UTC 2011

Modified Files:
src/sys/arch/xen/xenbus: xenbus_xs.c

Log Message:
Change a cast to appease gcc4.5


To generate a diff of this commit:
cvs rdiff -u -r1.21 -r1.22 src/sys/arch/xen/xenbus/xenbus_xs.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/xen/xenbus/xenbus_xs.c
diff -u src/sys/arch/xen/xenbus/xenbus_xs.c:1.21 src/sys/arch/xen/xenbus/xenbus_xs.c:1.22
--- src/sys/arch/xen/xenbus/xenbus_xs.c:1.21	Sun Jul 17 20:54:49 2011
+++ src/sys/arch/xen/xenbus/xenbus_xs.c	Wed Jul 27 23:11:23 2011
@@ -1,4 +1,4 @@
-/* $NetBSD: xenbus_xs.c,v 1.21 2011/07/17 20:54:49 joerg Exp $ */
+/* $NetBSD: xenbus_xs.c,v 1.22 2011/07/27 23:11:23 matt Exp $ */
 /**
  * xenbus_xs.c
  *
@@ -30,7 +30,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: xenbus_xs.c,v 1.21 2011/07/17 20:54:49 joerg Exp $);
+__KERNEL_RCSID(0, $NetBSD: xenbus_xs.c,v 1.22 2011/07/27 23:11:23 matt Exp $);
 
 #if 0
 #define DPRINTK(fmt, args...) \
@@ -740,7 +740,7 @@
 			SIMPLEQ_REMOVE_HEAD(events_to_proces, msg_next);
 			msg-u.watch.handle-xbw_callback(
 msg-u.watch.handle,
-(const char **)msg-u.watch.vec,
+(void *)msg-u.watch.vec,
 msg-u.watch.vec_size);
 			free(msg-u.watch.vec, M_DEVBUF);
 			free(msg, M_DEVBUF);



CVS commit: src/sys/arch/xen/xenbus

2011-06-07 Thread Manuel Bouyer
Module Name:src
Committed By:   bouyer
Date:   Tue Jun  7 13:52:31 UTC 2011

Modified Files:
src/sys/arch/xen/xenbus: xenbus_xs.c

Log Message:
check that the list is empty before calling cv_wait(). Otherwise
we may sleep waiting for an event which is already in the queue.


To generate a diff of this commit:
cvs rdiff -u -r1.19 -r1.20 src/sys/arch/xen/xenbus/xenbus_xs.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/xen/xenbus/xenbus_xs.c
diff -u src/sys/arch/xen/xenbus/xenbus_xs.c:1.19 src/sys/arch/xen/xenbus/xenbus_xs.c:1.20
--- src/sys/arch/xen/xenbus/xenbus_xs.c:1.19	Sun May 22 04:27:15 2011
+++ src/sys/arch/xen/xenbus/xenbus_xs.c	Tue Jun  7 13:52:30 2011
@@ -1,4 +1,4 @@
-/* $NetBSD: xenbus_xs.c,v 1.19 2011/05/22 04:27:15 rmind Exp $ */
+/* $NetBSD: xenbus_xs.c,v 1.20 2011/06/07 13:52:30 bouyer Exp $ */
 /**
  * xenbus_xs.c
  *
@@ -30,7 +30,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: xenbus_xs.c,v 1.19 2011/05/22 04:27:15 rmind Exp $);
+__KERNEL_RCSID(0, $NetBSD: xenbus_xs.c,v 1.20 2011/06/07 13:52:30 bouyer Exp $);
 
 #if 0
 #define DPRINTK(fmt, args...) \
@@ -730,13 +730,15 @@
 	SIMPLEQ_INIT(events_to_proces);
 	for (;;) {
 		mutex_enter(watch_events_lock);
-		cv_wait(watch_cv, watch_events_lock);
+		while (SIMPLEQ_EMPTY(watch_events))
+			cv_wait(watch_cv, watch_events_lock);
 		SIMPLEQ_CONCAT(events_to_proces, watch_events);
 		mutex_exit(watch_events_lock);
 
 		DPRINTK(xenwatch_thread: processing events);
 
 		while ((msg = SIMPLEQ_FIRST(events_to_proces)) != NULL) {
+			DPRINTK(xenwatch_thread: got event);
 			SIMPLEQ_REMOVE_HEAD(events_to_proces, msg_next);
 			msg-u.watch.handle-xbw_callback(
 msg-u.watch.handle,



CVS commit: src/sys/arch/xen/xenbus

2011-04-11 Thread Christoph Egger
Module Name:src
Committed By:   cegger
Date:   Mon Apr 11 15:00:49 UTC 2011

Modified Files:
src/sys/arch/xen/xenbus: xenbus_probe.c

Log Message:
Continue scanning for other frontends when initialization
of one frontend failed. Bug reported by Konrad Wilk on port-xen@.
Fix this for all error pathes within the loop.


To generate a diff of this commit:
cvs rdiff -u -r1.29 -r1.30 src/sys/arch/xen/xenbus/xenbus_probe.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/xen/xenbus/xenbus_probe.c
diff -u src/sys/arch/xen/xenbus/xenbus_probe.c:1.29 src/sys/arch/xen/xenbus/xenbus_probe.c:1.30
--- src/sys/arch/xen/xenbus/xenbus_probe.c:1.29	Wed Mar 30 22:34:03 2011
+++ src/sys/arch/xen/xenbus/xenbus_probe.c	Mon Apr 11 15:00:49 2011
@@ -1,4 +1,4 @@
-/* $NetBSD: xenbus_probe.c,v 1.29 2011/03/30 22:34:03 jym Exp $ */
+/* $NetBSD: xenbus_probe.c,v 1.30 2011/04/11 15:00:49 cegger Exp $ */
 /**
  * Talks to Xen Store to figure out what devices we have.
  *
@@ -29,7 +29,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: xenbus_probe.c,v 1.29 2011/03/30 22:34:03 jym Exp $);
+__KERNEL_RCSID(0, $NetBSD: xenbus_probe.c,v 1.30 2011/04/11 15:00:49 cegger Exp $);
 
 #if 0
 #define DPRINTK(fmt, args...) \
@@ -294,6 +294,7 @@
 		return err;
 
 	for (i = 0; i  dir_n; i++) {
+		err = 0;
 		/*
 		 * add size of path to size of xenbus_device. xenbus_device
 		 * already has room for one char in xbusd_path.



CVS commit: src/sys/arch/xen/xenbus

2011-04-11 Thread Christoph Egger
Module Name:src
Committed By:   cegger
Date:   Tue Apr 12 05:09:32 UTC 2011

Modified Files:
src/sys/arch/xen/xenbus: xenbus_probe.c

Log Message:
previous fix does not work if there is exactly only one entry where continue
exits the loop.
Apply fix from Konrad Wilke on port-xen@
That makes NetBSD DomU boot on Linux Dom0 with xl.


To generate a diff of this commit:
cvs rdiff -u -r1.30 -r1.31 src/sys/arch/xen/xenbus/xenbus_probe.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/xen/xenbus/xenbus_probe.c
diff -u src/sys/arch/xen/xenbus/xenbus_probe.c:1.30 src/sys/arch/xen/xenbus/xenbus_probe.c:1.31
--- src/sys/arch/xen/xenbus/xenbus_probe.c:1.30	Mon Apr 11 15:00:49 2011
+++ src/sys/arch/xen/xenbus/xenbus_probe.c	Tue Apr 12 05:09:32 2011
@@ -1,4 +1,4 @@
-/* $NetBSD: xenbus_probe.c,v 1.30 2011/04/11 15:00:49 cegger Exp $ */
+/* $NetBSD: xenbus_probe.c,v 1.31 2011/04/12 05:09:32 cegger Exp $ */
 /**
  * Talks to Xen Store to figure out what devices we have.
  *
@@ -29,7 +29,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: xenbus_probe.c,v 1.30 2011/04/11 15:00:49 cegger Exp $);
+__KERNEL_RCSID(0, $NetBSD: xenbus_probe.c,v 1.31 2011/04/12 05:09:32 cegger Exp $);
 
 #if 0
 #define DPRINTK(fmt, args...) \
@@ -317,6 +317,7 @@
 			printf(xenbus: can't get state 
 			for %s (%d)\n, xbusd-xbusd_path, err);
 			free(xbusd, M_DEVBUF);
+			err = 0;
 			continue;
 		}
 		if (state != XenbusStateInitialising) {



CVS commit: src/sys/arch/xen/xenbus

2011-03-30 Thread Jean-Yves Migeon
Module Name:src
Committed By:   jym
Date:   Wed Mar 30 22:34:03 UTC 2011

Modified Files:
src/sys/arch/xen/xenbus: xenbus_probe.c

Log Message:
(purely cosmetic changes)

- Use free_otherend_details() instead of calling free() on xbusd_otherend.
- rename talk_to_otherend() to watch_otherend(). We register a watch for
changes in the otherend device state; we are not really talking to it.
- add missing prototypes.


To generate a diff of this commit:
cvs rdiff -u -r1.28 -r1.29 src/sys/arch/xen/xenbus/xenbus_probe.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/xen/xenbus/xenbus_probe.c
diff -u src/sys/arch/xen/xenbus/xenbus_probe.c:1.28 src/sys/arch/xen/xenbus/xenbus_probe.c:1.29
--- src/sys/arch/xen/xenbus/xenbus_probe.c:1.28	Tue Jul  6 15:00:10 2010
+++ src/sys/arch/xen/xenbus/xenbus_probe.c	Wed Mar 30 22:34:03 2011
@@ -1,4 +1,4 @@
-/* $NetBSD: xenbus_probe.c,v 1.28 2010/07/06 15:00:10 cherry Exp $ */
+/* $NetBSD: xenbus_probe.c,v 1.29 2011/03/30 22:34:03 jym Exp $ */
 /**
  * Talks to Xen Store to figure out what devices we have.
  *
@@ -29,7 +29,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: xenbus_probe.c,v 1.28 2010/07/06 15:00:10 cherry Exp $);
+__KERNEL_RCSID(0, $NetBSD: xenbus_probe.c,v 1.29 2011/03/30 22:34:03 jym Exp $);
 
 #if 0
 #define DPRINTK(fmt, args...) \
@@ -69,6 +69,16 @@
 static void xenbus_attach(device_t, device_t, void *);
 static int  xenbus_print(void *, const char *);
 
+/* routines gathering device information from XenStore */
+static int  read_otherend_details(struct xenbus_device *,
+		const char *, const char *);
+static int  read_backend_details (struct xenbus_device *);
+static int  read_frontend_details(struct xenbus_device *);
+static void free_otherend_details(struct xenbus_device *);
+
+static int  watch_otherend (struct xenbus_device *);
+static void free_otherend_watch(struct xenbus_device *);
+
 static void xenbus_probe_init(void *);
 
 static struct xenbus_device *xenbus_lookup_device_path(const char *);
@@ -158,8 +168,7 @@
 		printf(missing other end from %s\n, xendev-xbusd_path);
 		xenbus_dev_fatal(xendev, -ENOENT, missing other end from %s,
  xendev-xbusd_path);
-		free(xendev-xbusd_otherend, M_DEVBUF);
-		xendev-xbusd_otherend = NULL;
+		free_otherend_details(xendev);
 		return ENOENT;
 	}
 
@@ -179,15 +188,12 @@
 	return read_otherend_details(xendev, frontend-id, frontend);
 }
 
-#if unused
 static void
 free_otherend_details(struct xenbus_device *dev)
 {
 	free(dev-xbusd_otherend, M_DEVBUF);
 	dev-xbusd_otherend = NULL;
 }
-#endif
-
 
 static void
 free_otherend_watch(struct xenbus_device *dev)
@@ -248,7 +254,7 @@
 }
 
 static int
-talk_to_otherend(struct xenbus_device *dev)
+watch_otherend(struct xenbus_device *dev)
 {
 	free_otherend_watch(dev);
 
@@ -360,7 +366,7 @@
 		}
 		SLIST_INSERT_HEAD(xenbus_device_list,
 		xbusd, xbusd_entries);
-		talk_to_otherend(xbusd);
+		watch_otherend(xbusd);
 	}
 	free(dir, M_DEVBUF);
 	return err;
@@ -467,7 +473,7 @@
 	KASSERT(xenbus_lookup_device_path(xbusd-xbusd_path) == xbusd);
 	SLIST_REMOVE(xenbus_device_list, xbusd, xenbus_device, xbusd_entries);
 	free_otherend_watch(xbusd);
-	free(xbusd-xbusd_otherend, M_DEVBUF);
+	free_otherend_details(xbusd);
 	xenbus_switch_state(xbusd, NULL, XenbusStateClosed);
 	free(xbusd, M_DEVBUF);
 	return 0;



CVS commit: src/sys/arch/xen/xenbus

2009-04-27 Thread Christoph Egger
Module Name:src
Committed By:   cegger
Date:   Tue Apr 28 05:44:43 UTC 2009

Modified Files:
src/sys/arch/xen/xenbus: xenbus_client.c xenbus_xs.c

Log Message:
sprintf - snprintf


To generate a diff of this commit:
cvs rdiff -u -r1.9 -r1.10 src/sys/arch/xen/xenbus/xenbus_client.c
cvs rdiff -u -r1.17 -r1.18 src/sys/arch/xen/xenbus/xenbus_xs.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/xen/xenbus/xenbus_client.c
diff -u src/sys/arch/xen/xenbus/xenbus_client.c:1.9 src/sys/arch/xen/xenbus/xenbus_client.c:1.10
--- src/sys/arch/xen/xenbus/xenbus_client.c:1.9	Fri Sep  5 13:37:24 2008
+++ src/sys/arch/xen/xenbus/xenbus_client.c	Tue Apr 28 05:44:43 2009
@@ -1,4 +1,4 @@
-/* $NetBSD: xenbus_client.c,v 1.9 2008/09/05 13:37:24 tron Exp $ */
+/* $NetBSD: xenbus_client.c,v 1.10 2009/04/28 05:44:43 cegger Exp $ */
 /**
  * Client-facing interface for the Xenbus driver.  In other words, the
  * interface between the Xenbus and the device-specific code, be it the
@@ -29,7 +29,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: xenbus_client.c,v 1.9 2008/09/05 13:37:24 tron Exp $);
+__KERNEL_RCSID(0, $NetBSD: xenbus_client.c,v 1.10 2009/04/28 05:44:43 cegger Exp $);
 
 #if 0
 #define DPRINTK(fmt, args...) \
@@ -170,7 +170,7 @@
 	if (printf_buffer == NULL)
 		goto fail;
 
-	len = sprintf(printf_buffer, %i , -err);
+	len = snprintf(printf_buffer, PRINTF_BUFFER_SIZE, %i , -err);
 	ret = vsnprintf(printf_buffer+len, PRINTF_BUFFER_SIZE-len, fmt, ap);
 
 	KASSERT(len + ret  PRINTF_BUFFER_SIZE);

Index: src/sys/arch/xen/xenbus/xenbus_xs.c
diff -u src/sys/arch/xen/xenbus/xenbus_xs.c:1.17 src/sys/arch/xen/xenbus/xenbus_xs.c:1.18
--- src/sys/arch/xen/xenbus/xenbus_xs.c:1.17	Wed Oct 29 13:53:15 2008
+++ src/sys/arch/xen/xenbus/xenbus_xs.c	Tue Apr 28 05:44:43 2009
@@ -1,4 +1,4 @@
-/* $NetBSD: xenbus_xs.c,v 1.17 2008/10/29 13:53:15 cegger Exp $ */
+/* $NetBSD: xenbus_xs.c,v 1.18 2009/04/28 05:44:43 cegger Exp $ */
 /**
  * xenbus_xs.c
  *
@@ -30,7 +30,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: xenbus_xs.c,v 1.17 2008/10/29 13:53:15 cegger Exp $);
+__KERNEL_RCSID(0, $NetBSD: xenbus_xs.c,v 1.18 2009/04/28 05:44:43 cegger Exp $);
 
 #if 0
 #define DPRINTK(fmt, args...) \
@@ -651,7 +651,7 @@
 	int err;
 	int s;
 
-	sprintf(token, %lX, (long)watch);
+	snprintf(token, sizeof(token), %lX, (long)watch);
 
 	s = spltty();
 
@@ -681,7 +681,7 @@
 	char token[sizeof(watch) * 2 + 1];
 	int err, s;
 
-	sprintf(token, %lX, (long)watch);
+	snprintf(token, sizeof(token), %lX, (long)watch);
 
 	s = spltty();
 
@@ -725,7 +725,7 @@
 	char token[sizeof(watch) * 2 + 1];
 	/* No need for watches_lock: the suspend_mutex is sufficient. */
 	SLIST_FOREACH(watch, watches, watch_next) {
-		sprintf(token, %lX, (long)watch);
+		snprintf(token, sizeof(token), %lX, (long)watch);
 		xs_watch(watch-node, token);
 	}