Skip to contentSkip to conversation

allow xen-blkback to be cleanly unloaded

PATCH Applied Paul Durrant 3 participants 6 messages Dec 2, 2019 – Dec 4, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v3 cover allow xen-blkback to be cleanly unloaded

Paul Durrant (2):
xen/xenbus: reference count registered modules
xen-blkback: allow module to be cleanly unloaded

 drivers/block/xen-blkback/blkback.c |  8 ++++++++
 drivers/block/xen-blkback/common.h  |  3 +++
 drivers/block/xen-blkback/xenbus.c  | 11 +++++++++++
 drivers/xen/xenbus/xenbus_probe.c   | 13 ++++++++++++-
 4 files changed, 34 insertions(+), 1 deletion(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH v3 1/2 xen/xenbus: reference count registered modules

To prevent a PV driver module being removed whilst attached to its other end, and hence xenbus calling into potentially invalid text, take a reference on the module before calling the probe() method (dropping it if unsuccessful) and drop the reference after returning from the remove() method.

---
Cc: Boris Ostrovsky <[email protected]>
Cc: Juergen Gross <[email protected]>
Cc: Stefano Stabellini <[email protected]>

v2:
- New in v2

v3:
- Use try_module_get() rather than __module)get() and handle failure
- Not added Juergen's R-b because of the change

---
 drivers/xen/xenbus/xenbus_probe.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
drivers/xen/xenbus/xenbus_probe.c +12 -1
index 5b471889d723..4461f4583476 100644
@@ -232,9 +232,16 @@ int xenbus_dev_probe(struct device *_dev)
 		return err;
 	}
 
+	if (!try_module_get(drv->driver.owner)) {
+		dev_warn(&dev->dev, "failed to acquire module reference on '%s'.\n",
+			 drv->driver.name);
+		err = -ESRCH;
+		goto fail;
+        }
+
 	err = drv->probe(dev, id);
 	if (err)
-		goto fail;
+		goto fail_put;
 
 	err = watch_otherend(dev);
 	if (err) {
@@ -244,6 +251,8 @@ int xenbus_dev_probe(struct device *_dev)
 	}
 
 	return 0;
+fail_put:
+	module_put(drv->driver.owner);
 fail:
 	xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
 	xenbus_switch_state(dev, XenbusStateClosed);
@@ -263,6 +272,8 @@ int xenbus_dev_remove(struct device *_dev)
 	if (drv->remove)
 		drv->remove(dev);
 
+	module_put(drv->driver.owner);
+
 	free_otherend_details(dev);
 
 	xenbus_switch_state(dev, XenbusStateClosed);
Signature · 1 line
2.20.1
Reviewed-by on v3 1/2
#2 lore raw

Re: [PATCH v3 1/2] xen/xenbus: reference count registered modules

Quoted 8 lines · Paul Durrant

To prevent a PV driver module being removed whilst attached to its other end, and hence xenbus calling into potentially invalid text, take a reference on the module before calling the probe() method (dropping it if unsuccessful) and drop the reference after returning from the remove() method.

with ...

drivers/xen/xenbus/xenbus_probe.c int xenbus_dev_probe(struct device *_dev) · line 237

Quoted 9 lines
drivers/xen/xenbus/xenbus_probe.c +3 -0
@@ -232,9 +232,16 @@ int xenbus_dev_probe(struct device *_dev)
 		return err;
 	}
 
+	if (!try_module_get(drv->driver.owner)) {
+		dev_warn(&dev->dev, "failed to acquire module reference on '%s'.\n",
+			 drv->driver.name);

... perhaps the full stop dropped here and ...

drivers/xen/xenbus/xenbus_probe.c int xenbus_dev_probe(struct device *_dev) · line 240

Quoted 3 lines
drivers/xen/xenbus/xenbus_probe.c +3 -0
+		err = -ESRCH;
+		goto fail;
+        }

... (definitely) indentation here changed to use a tab.

Jan

maintainer Applied
#3 lore raw

Re: [PATCH v3 1/2] xen/xenbus: reference count registered modules

In reply to Paul Durrant
Quoted 8 lines · Paul Durrant

To prevent a PV driver module being removed whilst attached to its other end, and hence xenbus calling into potentially invalid text, take a reference on the module before calling the probe() method (dropping it if unsuccessful) and drop the reference after returning from the remove() method.

Pushed to xen/tip.git for-linus-5.5b

Juergen

#4 lore raw

PATCH v3 2/2 xen-blkback: allow module to be cleanly unloaded

In reply to Paul Durrant

Add a module_exit() to perform the necessary clean-up.

---
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Jens Axboe <[email protected]>

---
 drivers/block/xen-blkback/blkback.c |  8 ++++++++
 drivers/block/xen-blkback/common.h  |  3 +++
 drivers/block/xen-blkback/xenbus.c  | 11 +++++++++++
 3 files changed, 22 insertions(+)
drivers/block/xen-blkback/blkback.c +8 -0
index fd1e19f1a49f..e562a7e20c3c 100644
@@ -1504,5 +1504,13 @@ static int __init xen_blkif_init(void)
 
 module_init(xen_blkif_init);
 
+static void __exit xen_blkif_fini(void)
+{
+	xen_blkif_xenbus_fini();
+	xen_blkif_interface_fini();
+}
+
+module_exit(xen_blkif_fini);
+
 MODULE_LICENSE("Dual BSD/GPL");
 MODULE_ALIAS("xen-backend:vbd");
drivers/block/xen-blkback/common.h +3 -0
index 1d3002d773f7..49132b0adbbe 100644
@@ -375,9 +375,12 @@ struct phys_req {
 	struct block_device	*bdev;
 	blkif_sector_t		sector_number;
 };
+
 int xen_blkif_interface_init(void);
+void xen_blkif_interface_fini(void);
 
 int xen_blkif_xenbus_init(void);
+void xen_blkif_xenbus_fini(void);
 
 irqreturn_t xen_blkif_be_int(int irq, void *dev_id);
 int xen_blkif_schedule(void *arg);
drivers/block/xen-blkback/xenbus.c +11 -0
index b90dbcd99c03..e8c5c54e1d26 100644
@@ -333,6 +333,12 @@ int __init xen_blkif_interface_init(void)
 	return 0;
 }
 
+void xen_blkif_interface_fini(void)
+{
+	kmem_cache_destroy(xen_blkif_cachep);
+	xen_blkif_cachep = NULL;
+}
+
 /*
  *  sysfs interface for VBD I/O requests
  */
@@ -1122,3 +1128,8 @@ int xen_blkif_xenbus_init(void)
 {
 	return xenbus_register_backend(&xen_blkbk_driver);
 }
+
+void xen_blkif_xenbus_fini(void)
+{
+	xenbus_unregister_driver(&xen_blkbk_driver);
+}
Signature · 1 line
2.20.1