public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] usbip: vhc_hcd: prevent module being removed while device are attached
@ 2018-04-02 20:52 Shuah Khan
  2018-04-02 20:52 ` [PATCH] usbip: vhci_hcd: Fix usb device and sockfd leaks Shuah Khan
  2018-04-03  6:56 ` [PATCH] usbip: vhc_hcd: prevent module being removed while device are attached Greg KH
  0 siblings, 2 replies; 8+ messages in thread
From: Shuah Khan @ 2018-04-02 20:52 UTC (permalink / raw)
  To: valentina.manea.m, shuah, gregkh; +Cc: Shuah Khan, linux-usb, linux-kernel

vhci_hcd module can be removed even when devices are attached. Fix to
prevent module removal when devices are still attached.

Signed-off-by: Shuah Khan <shuahkh@osg.samsung.com>
---
 drivers/usb/usbip/vhci_sysfs.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)

diff --git a/drivers/usb/usbip/vhci_sysfs.c b/drivers/usb/usbip/vhci_sysfs.c
index 48808388ec33..6a54b9aa92be 100644
--- a/drivers/usb/usbip/vhci_sysfs.c
+++ b/drivers/usb/usbip/vhci_sysfs.c
@@ -9,6 +9,7 @@
 #include <linux/net.h>
 #include <linux/platform_device.h>
 #include <linux/slab.h>
+#include <linux/module.h>
 
 #include "usbip_common.h"
 #include "vhci.h"
@@ -252,6 +253,8 @@ static ssize_t detach_store(struct device *dev, struct device_attribute *attr,
 	if (ret < 0)
 		return -EINVAL;
 
+	module_put(THIS_MODULE);
+
 	usbip_dbg_vhci_sysfs("Leave\n");
 
 	return count;
@@ -302,7 +305,7 @@ static ssize_t attach_store(struct device *dev, struct device_attribute *attr,
 	struct vhci_hcd *vhci_hcd;
 	struct vhci_device *vdev;
 	struct vhci *vhci;
-	int err;
+	int err, ret;
 	unsigned long flags;
 
 	/*
@@ -339,10 +342,18 @@ static ssize_t attach_store(struct device *dev, struct device_attribute *attr,
 	else
 		vdev = &vhci->vhci_hcd_hs->vdev[rhport];
 
+	/* get module ref to avoid being removed with active attached devs */
+	if (!try_module_get(THIS_MODULE)) {
+		ret = -EAGAIN;
+		goto module_get_err;
+	}
+
 	/* Extract socket from fd. */
 	socket = sockfd_lookup(sockfd, &err);
-	if (!socket)
-		return -EINVAL;
+	if (!socket) {
+		ret = -EINVAL;
+		goto error;
+	}
 
 	/* now need lock until setting vdev status as used */
 
@@ -362,7 +373,8 @@ static ssize_t attach_store(struct device *dev, struct device_attribute *attr,
 		 * Will be retried from userspace
 		 * if there's another free port.
 		 */
-		return -EBUSY;
+		ret = -EBUSY;
+		goto error;
 	}
 
 	dev_info(dev, "pdev(%u) rhport(%u) sockfd(%d)\n",
@@ -386,6 +398,11 @@ static ssize_t attach_store(struct device *dev, struct device_attribute *attr,
 	rh_port_connect(vdev, speed);
 
 	return count;
+
+error:
+	module_put(THIS_MODULE);
+module_get_err:
+	return ret;
 }
 static DEVICE_ATTR_WO(attach);
 
-- 
2.14.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2018-04-04 15:14 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-04-02 20:52 [PATCH] usbip: vhc_hcd: prevent module being removed while device are attached Shuah Khan
2018-04-02 20:52 ` [PATCH] usbip: vhci_hcd: Fix usb device and sockfd leaks Shuah Khan
2018-04-03  6:56   ` Greg KH
2018-04-03 13:54     ` Shuah Khan
2018-04-03  6:56 ` [PATCH] usbip: vhc_hcd: prevent module being removed while device are attached Greg KH
2018-04-03 15:56   ` Shuah Khan
2018-04-04  8:25     ` Oliver Neukum
2018-04-04 15:14       ` Shuah Khan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox