Module Name:    src
Committed By:   jdolecek
Date:           Sun Jun 24 20:15:00 UTC 2018

Modified Files:
        src/sys/arch/xen/xen: pciback.c xbdback_xenbus.c

Log Message:
similar treatment as xennetback_xenbus.c:
- protect instance list with mutex
- mark more local variables static
- mark with XXXSMP what looks suspicious
- in pciback.c use kmem_zalloc() et.al to allocate the device structures


To generate a diff of this commit:
cvs rdiff -u -r1.13 -r1.14 src/sys/arch/xen/xen/pciback.c
cvs rdiff -u -r1.65 -r1.66 src/sys/arch/xen/xen/xbdback_xenbus.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/xen/pciback.c
diff -u src/sys/arch/xen/xen/pciback.c:1.13 src/sys/arch/xen/xen/pciback.c:1.14
--- src/sys/arch/xen/xen/pciback.c:1.13	Sun Jun 24 13:35:33 2018
+++ src/sys/arch/xen/xen/pciback.c	Sun Jun 24 20:15:00 2018
@@ -1,4 +1,4 @@
-/*      $NetBSD: pciback.c,v 1.13 2018/06/24 13:35:33 jdolecek Exp $      */
+/*      $NetBSD: pciback.c,v 1.14 2018/06/24 20:15:00 jdolecek Exp $      */
 
 /*
  * Copyright (c) 2009 Manuel Bouyer.
@@ -26,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: pciback.c,v 1.13 2018/06/24 13:35:33 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pciback.c,v 1.14 2018/06/24 20:15:00 jdolecek Exp $");
 
 #include "opt_xen.h"
 
@@ -79,7 +79,8 @@ struct pciback_pci_dev {
 };
 
 /* list of devices we want to match */
-SLIST_HEAD(pciback_pci_devlist, pciback_pci_dev) pciback_pci_devlist_head  =
+static SLIST_HEAD(pciback_pci_devlist, pciback_pci_dev)
+    pciback_pci_devlist_head  =
     SLIST_HEAD_INITIALIZER(pciback_pci_devlist_head);
 	
 /* PCI-related functions and definitions  */
@@ -128,7 +129,7 @@ static const struct kernfs_fileop pcibac
 static int  pciback_xenbus_create(struct xenbus_device *);
 static int  pciback_xenbus_destroy(void *);
 static void pciback_xenbus_frontend_changed(void *, XenbusState);
-static struct pb_xenbus_instance * pbxif_lookup(domid_t);
+static bool pbxif_lookup(domid_t);
 static void pciback_xenbus_export_device(struct pb_xenbus_instance *, char *);
 static void pciback_xenbus_export_roots(struct pb_xenbus_instance *);
 
@@ -192,7 +193,8 @@ struct pb_xenbus_instance {
         grant_handle_t pbx_shinfo_handle; /* to unmap shared page */
 };
 
-SLIST_HEAD(, pb_xenbus_instance) pb_xenbus_instances;
+static SLIST_HEAD(, pb_xenbus_instance) pb_xenbus_instances;
+static kmutex_t pb_xenbus_lock;
 
 static struct xenbus_backend_driver pci_backend_driver = {
         .xbakd_create = pciback_xenbus_create,
@@ -351,6 +353,7 @@ static struct pciback_pci_dev*
 pciback_pci_lookup(u_int bus, u_int dev, u_int func)
 {
 	struct pciback_pci_dev *pbd;
+	/* Safe without lock, only written during init */
 	SLIST_FOREACH(pbd, &pciback_pci_devlist_head, pb_devlist_next) {
 		if (pbd->pb_bus == bus &&
 		    pbd->pb_device == dev &&
@@ -374,7 +377,7 @@ pciback_pci_init(void)
 	if (strlen(xi.xcp_pcidevs) == 0)
 		return;
 	pcidevs = xi.xcp_pcidevs;
-	for(pcidevs = xi.xcp_pcidevs; *pcidevs != '\0';) {
+	for (pcidevs = xi.xcp_pcidevs; *pcidevs != '\0';) {
 		if (*pcidevs != '(')
 			goto error;
 		pcidevs++;
@@ -384,12 +387,7 @@ pciback_pci_init(void)
 			goto error;
 		*c = '\0';
 		if (pciback_parse_pci(pcidevs, &bus, &dev, &func) == 0) {
-			pb = malloc(sizeof(struct pciback_pci_dev), M_DEVBUF,
-			    M_NOWAIT | M_ZERO);
-			if (pb == NULL) {
-				aprint_error("pciback_pci_init: out or memory\n");
-				return;
-			}
+			pb = kmem_zalloc(sizeof(*pb), KM_SLEEP);
 			pb->pb_bus = bus;
 			pb->pb_device = dev;
 			pb->pb_function = func;
@@ -400,6 +398,10 @@ pciback_pci_init(void)
 		}
 		pcidevs = c + 1;
 	}
+
+	SLIST_INIT(&pb_xenbus_instances);
+	mutex_init(&pb_xenbus_lock, MUTEX_DEFAULT, IPL_NONE);
+
 	xenbus_backend_register(&pci_backend_driver);
 
 	KERNFS_ALLOCENTRY(dkt, M_TEMP, M_WAITOK);
@@ -459,14 +461,10 @@ pciback_xenbus_create(struct xenbus_devi
 		return err;
 	}
 
-	if (pbxif_lookup(domid) != NULL) {
+	if (pbxif_lookup(domid)) {
 		return EEXIST;
 	}
-	pbxi = malloc(sizeof(struct pb_xenbus_instance), M_DEVBUF,
-	    M_NOWAIT | M_ZERO);
-	if (pbxi == NULL) {
-		return ENOMEM;
-	}
+	pbxi = kmem_zalloc(sizeof(*pbxi), KM_SLEEP);
 	pbxi->pbx_domid = domid;
 
 	xbusd->xbusd_u.b.b_cookie = pbxi;
@@ -475,7 +473,9 @@ pciback_xenbus_create(struct xenbus_devi
 
 	SLIST_INIT(&pbxi->pbx_pb_pci_dev);
 
+	mutex_enter(&pb_xenbus_lock);
 	SLIST_INSERT_HEAD(&pb_xenbus_instances, pbxi, pbx_next);
+	mutex_exit(&pb_xenbus_lock);
 
 	xbusd->xbusd_otherend_changed = pciback_xenbus_frontend_changed;
 
@@ -511,7 +511,7 @@ pciback_xenbus_create(struct xenbus_devi
 
 	return 0;
 fail:
-	free(pbxi, M_DEVBUF);
+	kmem_free(pbxi, sizeof(*pbxi));
 	return err;
 }
 
@@ -527,8 +527,10 @@ pciback_xenbus_destroy(void *arg)
 	event_remove_handler(pbxi->pbx_evtchn,
 	    pciback_xenbus_evthandler, pbxi);
 
+	mutex_enter(&pb_xenbus_lock);
 	SLIST_REMOVE(&pb_xenbus_instances,
 	    pbxi, pb_xenbus_instance, pbx_next);
+	mutex_exit(&pb_xenbus_lock);
 
 	if (pbxi->pbx_sh_info) {
 		op.host_addr = (vaddr_t)pbxi->pbx_sh_info;
@@ -545,7 +547,7 @@ pciback_xenbus_destroy(void *arg)
 	}
 	uvm_km_free(kernel_map, (vaddr_t)pbxi->pbx_sh_info,
 	    PAGE_SIZE, UVM_KMF_VAONLY);
-	free(pbxi, M_DEVBUF);
+	kmem_free(pbxi, sizeof(*pbxi));
 	return 0;
 }
 
@@ -645,16 +647,22 @@ err1:
 }
 
 /* lookup a pbxi based on domain id and interface handle */
-static struct pb_xenbus_instance *
+static bool
 pbxif_lookup(domid_t dom)
 {
 	struct pb_xenbus_instance *pbxi;
+	bool found = false;
 
+	mutex_enter(&pb_xenbus_lock);
 	SLIST_FOREACH(pbxi, &pb_xenbus_instances, pbx_next) {
-		if (pbxi->pbx_domid == dom)
-			return pbxi;
+		if (pbxi->pbx_domid == dom) {
+			found = true;
+			break;
+		}
 	}
-	return NULL;
+	mutex_exit(&pb_xenbus_lock);
+
+	return found;
 }
 
 static void

Index: src/sys/arch/xen/xen/xbdback_xenbus.c
diff -u src/sys/arch/xen/xen/xbdback_xenbus.c:1.65 src/sys/arch/xen/xen/xbdback_xenbus.c:1.66
--- src/sys/arch/xen/xen/xbdback_xenbus.c:1.65	Sat Nov 11 21:03:01 2017
+++ src/sys/arch/xen/xen/xbdback_xenbus.c	Sun Jun 24 20:15:00 2018
@@ -1,4 +1,4 @@
-/*      $NetBSD: xbdback_xenbus.c,v 1.65 2017/11/11 21:03:01 riastradh Exp $      */
+/*      $NetBSD: xbdback_xenbus.c,v 1.66 2018/06/24 20:15:00 jdolecek Exp $      */
 
 /*
  * Copyright (c) 2006 Manuel Bouyer.
@@ -26,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: xbdback_xenbus.c,v 1.65 2017/11/11 21:03:01 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xbdback_xenbus.c,v 1.66 2018/06/24 20:15:00 jdolecek Exp $");
 
 #include <sys/atomic.h>
 #include <sys/buf.h>
@@ -212,7 +212,8 @@ do {                                    
                xbdback_finish_disconnect(xbdip);             \
 } while (/* CONSTCOND */ 0)
 
-SLIST_HEAD(, xbdback_instance) xbdback_instances;
+static SLIST_HEAD(, xbdback_instance) xbdback_instances;
+static kmutex_t xbdback_lock;
 
 /*
  * For each request from a guest, a xbdback_request is allocated from
@@ -281,6 +282,7 @@ struct xbdback_fragment {
  * Pools to manage the chain of block requests and I/Os fragments
  * submitted by frontend.
  */
+/* XXXSMP */
 struct xbdback_pool {
 	struct pool_cache pc;
 	struct timeval last_warning;
@@ -308,7 +310,7 @@ static int  xbdback_connect(struct xbdba
 static void xbdback_disconnect(struct xbdback_instance *);
 static void xbdback_finish_disconnect(struct xbdback_instance *);
 
-static struct xbdback_instance *xbdif_lookup(domid_t, uint32_t);
+static bool xbdif_lookup(domid_t, uint32_t);
 
 static void *xbdback_co_main(struct xbdback_instance *, void *);
 static void *xbdback_co_main_loop(struct xbdback_instance *, void *);
@@ -363,6 +365,7 @@ xbdbackattach(int n)
 	 * and send driver up message.
 	 */
 	SLIST_INIT(&xbdback_instances);
+	mutex_init(&xbdback_lock, MUTEX_DEFAULT, IPL_NONE);
 	SIMPLEQ_INIT(&xbdback_shmq);
 	xbdback_shmcb = 0;
 
@@ -424,7 +427,7 @@ xbdback_xenbus_create(struct xenbus_devi
 		return EFTYPE;
 	}
 			
-	if (xbdif_lookup(domid, handle) != NULL) {
+	if (xbdif_lookup(domid, handle)) {
 		return EEXIST;
 	}
 	xbdi = kmem_zalloc(sizeof(*xbdi), KM_SLEEP);
@@ -440,7 +443,9 @@ xbdback_xenbus_create(struct xenbus_devi
 
 	mutex_init(&xbdi->xbdi_lock, MUTEX_DEFAULT, IPL_BIO);
 	cv_init(&xbdi->xbdi_cv, xbdi->xbdi_name);
+	mutex_enter(&xbdback_lock);
 	SLIST_INSERT_HEAD(&xbdback_instances, xbdi, next);
+	mutex_exit(&xbdback_lock);
 
 	xbusd->xbusd_u.b.b_cookie = xbdi;	
 	xbusd->xbusd_u.b.b_detach = xbdback_xenbus_destroy;
@@ -512,7 +517,9 @@ xbdback_xenbus_destroy(void *arg)
 		    name, xbdi->xbdi_domid);
 		vn_close(xbdi->xbdi_vp, FREAD, NOCRED);
 	}
+	mutex_enter(&xbdback_lock);
 	SLIST_REMOVE(&xbdback_instances, xbdi, xbdback_instance, next);
+	mutex_exit(&xbdback_lock);
 	mutex_destroy(&xbdi->xbdi_lock);
 	cv_destroy(&xbdi->xbdi_cv);
 	kmem_free(xbdi, sizeof(*xbdi));
@@ -894,16 +901,22 @@ xbdback_finish_disconnect(struct xbdback
 	cv_signal(&xbdi->xbdi_cv);
 }
 
-static struct xbdback_instance *
+static bool
 xbdif_lookup(domid_t dom , uint32_t handle)
 {
 	struct xbdback_instance *xbdi;
+	bool found = false;
 
+	mutex_enter(&xbdback_lock);
 	SLIST_FOREACH(xbdi, &xbdback_instances, next) {
-		if (xbdi->xbdi_domid == dom && xbdi->xbdi_handle == handle)
-			return xbdi;
+		if (xbdi->xbdi_domid == dom && xbdi->xbdi_handle == handle) {
+			found = true;
+			break;
+		}
 	}
-	return NULL;
+	mutex_exit(&xbdback_lock);
+
+	return found;
 }
 
 static int

Reply via email to