diff mbox series

[V4] usb: serial: Fix mos7840 hangup on resume

Message ID 20240614224556.23215-1-d.smirnov@inbox.lv (mailing list archive)
State New, archived
Headers show
Series [V4] usb: serial: Fix mos7840 hangup on resume | expand

Commit Message

Dmitry Smirnov June 14, 2024, 10:45 p.m. UTC
This patch addresses a kernel hang issue with the mos7840 driver when
PC resuming from a suspend with a device connected to the serial port.

This patch introduces two new functions: mos7840_suspend() and mos7840_resume().

Tested with Delock 87414 USB 2.0 to 4x serial adapter.

Signed-off-by: Dmitry Smirnov <d.smirnov@inbox.lv>
---
V4: Removed debug prints
V3: Code cleanup
V2: Reworked mos7840_suspend() based on comments from reviewer
V1: Initial version

 drivers/usb/serial/mos7840.c | 48 ++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)


base-commit: c3f38fa61af77b49866b006939479069cd451173

Comments

Dmitry Smirnov July 1, 2024, 12:28 p.m. UTC | #1
Are any further steps required on my part?

---
Dmitry
Johan Hovold July 9, 2024, 4:15 p.m. UTC | #2
On Sat, Jun 15, 2024 at 01:45:56AM +0300, Dmitry Smirnov wrote:
> This patch addresses a kernel hang issue with the mos7840 driver when
> PC resuming from a suspend with a device connected to the serial port.
> 
> This patch introduces two new functions: mos7840_suspend() and mos7840_resume().
> 
> Tested with Delock 87414 USB 2.0 to 4x serial adapter.
> 
> Signed-off-by: Dmitry Smirnov <d.smirnov@inbox.lv>
> ---
> V4: Removed debug prints
> V3: Code cleanup
> V2: Reworked mos7840_suspend() based on comments from reviewer
> V1: Initial version

Thanks for the fix and sorry about the late reply on this one.

I took a closer look at this today, and indeed we have a kernel crash
here on resume with mos7840. This is a bug that dates back to 2011 when
I added support for multiple read URBs and failed to notice this
interaction with drivers that only uses one URB.

This actually affects a few more drivers, but judging from a quick look
not as severely as for mos7840 which uses a different structure for the
URB context. I'll make sure to go back and review this more thoroughly
in the next few weeks.

> +static int mos7840_resume(struct usb_serial *serial)
> +{
> +	struct moschip_port *mos7840_port;
> +	struct usb_serial_port *port;
> +	int res;
> +	int i;
> +
> +	for (i = 0; i < serial->num_ports; ++i) {
> +		port = serial->port[i];
> +		if (!tty_port_initialized(&port->port))
> +			continue;
> +
> +		mos7840_port = usb_get_serial_port_data(port);
> +
> +		if (port->bulk_in_size) {

Not needed, checked on probe.

Need to set the read_urb_busy flag here.

> +			res = usb_submit_urb(mos7840_port->read_urb, GFP_NOIO);
> +			if (res) {
> +				usb_kill_urb(mos7840_port->read_urb);

Not needed as submission failed.

> +				mos7840_port->read_urb_busy = false;
> +			}
> +		}
> +	}
> +
> +	return 0;
> +}

I've reworded the commit message to provide some more background on the
crash, added the missing busy flag set, dropped the unnecessary
bulk_in_size check and dropped the unnecessary usb_kill_urb() in case
submission fails on resume.

The end result is here:

	https://git.kernel.org/pub/scm/linux/kernel/git/johan/usb-serial.git/commit/?h=usb-linus&id=c15a688e49987385baa8804bf65d570e362f8576	

Johan
diff mbox series

Patch

diff --git a/drivers/usb/serial/mos7840.c b/drivers/usb/serial/mos7840.c
index 8b0308d84270..2f4b2c8f4a48 100644
--- a/drivers/usb/serial/mos7840.c
+++ b/drivers/usb/serial/mos7840.c
@@ -1737,6 +1737,52 @@  static void mos7840_port_remove(struct usb_serial_port *port)
 	kfree(mos7840_port);
 }
 
+static int mos7840_suspend(struct usb_serial *serial, pm_message_t message)
+{
+	struct moschip_port *mos7840_port;
+	struct usb_serial_port *port;
+	int i;
+
+	for (i = 0; i < serial->num_ports; ++i) {
+		port = serial->port[i];
+		if (!tty_port_initialized(&port->port))
+			continue;
+
+		mos7840_port = usb_get_serial_port_data(port);
+
+		usb_kill_urb(mos7840_port->read_urb);
+		mos7840_port->read_urb_busy = false;
+	}
+
+	return 0;
+}
+
+static int mos7840_resume(struct usb_serial *serial)
+{
+	struct moschip_port *mos7840_port;
+	struct usb_serial_port *port;
+	int res;
+	int i;
+
+	for (i = 0; i < serial->num_ports; ++i) {
+		port = serial->port[i];
+		if (!tty_port_initialized(&port->port))
+			continue;
+
+		mos7840_port = usb_get_serial_port_data(port);
+
+		if (port->bulk_in_size) {
+			res = usb_submit_urb(mos7840_port->read_urb, GFP_NOIO);
+			if (res) {
+				usb_kill_urb(mos7840_port->read_urb);
+				mos7840_port->read_urb_busy = false;
+			}
+		}
+	}
+
+	return 0;
+}
+
 static struct usb_serial_driver moschip7840_4port_device = {
 	.driver = {
 		   .owner = THIS_MODULE,
@@ -1764,6 +1810,8 @@  static struct usb_serial_driver moschip7840_4port_device = {
 	.port_probe = mos7840_port_probe,
 	.port_remove = mos7840_port_remove,
 	.read_bulk_callback = mos7840_bulk_in_callback,
+	.suspend = mos7840_suspend,
+	.resume = mos7840_resume,
 };
 
 static struct usb_serial_driver * const serial_drivers[] = {