diff mbox series

[1/1] usb: serial: Fix mos7840 resume from standby

Message ID 20240604225507.6801-2-d.smirnov@inbox.lv
State New
Headers show
Series Kernel hang issue with mos7840 on resume from standby | expand

Commit Message

Dmitry Smirnov June 4, 2024, 10:55 p.m. UTC
From: Dmitry <dimonija@gmail.com>

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

Comments

Greg KH June 5, 2024, 10:03 a.m. UTC | #1
On Wed, Jun 05, 2024 at 01:55:07AM +0300, Dmitry wrote:
> From: Dmitry <dimonija@gmail.com>
> 
> ---
>  drivers/usb/serial/mos7840.c | 34 ++++++++++++++++++++++++++++++++++
>  1 file changed, 34 insertions(+)
> 
> diff --git a/drivers/usb/serial/mos7840.c b/drivers/usb/serial/mos7840.c
> index 8b0308d84270..7443d2f018b4 100644
> --- a/drivers/usb/serial/mos7840.c
> +++ b/drivers/usb/serial/mos7840.c
> @@ -770,6 +770,7 @@ static void mos7840_close(struct usb_serial_port *port)
>  		if (mos7840_port->write_urb_pool[j]) {
>  			kfree(mos7840_port->write_urb_pool[j]->transfer_buffer);
>  			usb_free_urb(mos7840_port->write_urb_pool[j]);
> +			mos7840_port->write_urb_pool[j] = NULL;
>  		}
>  	}
>  
> @@ -1737,6 +1738,37 @@ 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)
> +{
> +    dev_dbg(&serial->interface->dev, "mos7840_suspend\n");
> +
> +    return 0;
> +}
> +
> +static int mos7840_resume(struct usb_serial *serial)
> +{
> +	struct moschip_port *mos7840_port;
> +	struct usb_serial_port *port;
> +	int i;
> +	dev_dbg(&serial->interface->dev, "mos7840_resume\n");
> +
> +	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)
> +			usb_submit_urb(mos7840_port->read_urb, GFP_NOIO);
> +
> +		/*if (port->bulk_out_size)
> +			usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);*/
> +	}
> +
> +    return 0;
> +}
> +
>  static struct usb_serial_driver moschip7840_4port_device = {
>  	.driver = {
>  		   .owner = THIS_MODULE,
> @@ -1764,6 +1796,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[] = {
> -- 
> 2.45.1
> 
> 

Hi,

This is the friendly patch-bot of Greg Kroah-Hartman.  You have sent him
a patch that has triggered this response.  He used to manually respond
to these common problems, but in order to save his sanity (he kept
writing the same thing over and over, yet to different people), I was
created.  Hopefully you will not take offence and will fix the problem
in your patch and resubmit it so that it can be accepted into the Linux
kernel tree.

You are receiving this message because of the following common error(s)
as indicated below:

- Your patch does not have a Signed-off-by: line.  Please read the
  kernel file, Documentation/process/submitting-patches.rst and resend
  it after adding that line.  Note, the line needs to be in the body of
  the email, before the patch, not at the bottom of the patch or in the
  email signature.

- You did not specify a description of why the patch is needed, or
  possibly, any description at all, in the email body.  Please read the
  section entitled "The canonical patch format" in the kernel file,
  Documentation/process/submitting-patches.rst for what is needed in
  order to properly describe the change.

- You did not write a descriptive Subject: for the patch, allowing Greg,
  and everyone else, to know what this patch is all about.  Please read
  the section entitled "The canonical patch format" in the kernel file,
  Documentation/process/submitting-patches.rst for what a proper
  Subject: line should look like.

- It looks like you did not use your "real" name for the patch on either
  the Signed-off-by: line, or the From: line (both of which have to
  match).  Please read the kernel file,
  Documentation/process/submitting-patches.rst for how to do this
  correctly.


If you wish to discuss this problem further, or you have questions about
how to resolve this issue, please feel free to respond to this email and
Greg will reply once he has dug out from the pending patches received
from other developers.

thanks,

greg k-h's patch email bot
Oliver Neukum June 5, 2024, 12:13 p.m. UTC | #2
On 05.06.24 00:55, Dmitry wrote:
> From: Dmitry <dimonija@gmail.com>

Sorry,

first thank you for the patch. It is good to see patches,
but that is a NACK.

Let me explain.

> ---
>   drivers/usb/serial/mos7840.c | 34 ++++++++++++++++++++++++++++++++++
>   1 file changed, 34 insertions(+)
> 
> diff --git a/drivers/usb/serial/mos7840.c b/drivers/usb/serial/mos7840.c
> index 8b0308d84270..7443d2f018b4 100644
> --- a/drivers/usb/serial/mos7840.c
> +++ b/drivers/usb/serial/mos7840.c
> @@ -770,6 +770,7 @@ static void mos7840_close(struct usb_serial_port *port)
>   		if (mos7840_port->write_urb_pool[j]) {
>   			kfree(mos7840_port->write_urb_pool[j]->transfer_buffer);
>   			usb_free_urb(mos7840_port->write_urb_pool[j]);
> +			mos7840_port->write_urb_pool[j] = NULL;

Why is that needed? It is a good idea, but is it needed?

>   		}
>   	}
>   
> @@ -1737,6 +1738,37 @@ 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)
> +{
> +    dev_dbg(&serial->interface->dev, "mos7840_suspend\n");
> +
> +    return 0;
> +}

You are providing a suspend() method that does nothing.

If we look at usbserial:

int usb_serial_suspend(struct usb_interface *intf, pm_message_t message)
{
         struct usb_serial *serial = usb_get_intfdata(intf);
         int i, r;

         /* suspend when called for first sibling interface */
         if (serial->suspend_count++)
                 return 0;

         /*
          * serial->type->suspend() MUST return 0 in system sleep context,
          * otherwise, the resume callback has to recover device from
          * previous suspend failure.
          */
         if (serial->type->suspend) {
                 r = serial->type->suspend(serial, message);
                 if (r < 0) {
                         serial->suspend_count--;
                         return r;
                 }
         }

         for (i = 0; i < serial->num_ports; ++i)
                 usb_serial_port_poison_urbs(serial->port[i]);

         return 0;
}
EXPORT_SYMBOL(usb_serial_suspend);

There is no point in doing so.


> +static int mos7840_resume(struct usb_serial *serial)
> +{
> +	struct moschip_port *mos7840_port;
> +	struct usb_serial_port *port;
> +	int i;
> +	dev_dbg(&serial->interface->dev, "mos7840_resume\n");
> +
> +	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)
> +			usb_submit_urb(mos7840_port->read_urb, GFP_NOIO);
This can fail.
And much worse, if this is necessary, the generic usbserial code does not handle
the resubmission of the read URB. If that is the case, it will not be killed if
the system suspends. That means if the suspend run fails further down the chain,
you will resubmit a running URB.

And, worse, there will be IO to a suspended device, which is very bad
and can corrupt memory with S4.
If you need to manage the URB in the individual driver you need to handle
it both upon suspend() and resume(). Hence you need some like the attached
patch in addition to your patch.

	Regards
		Oliver
diff mbox series

Patch

diff --git a/drivers/usb/serial/mos7840.c b/drivers/usb/serial/mos7840.c
index 8b0308d84270..7443d2f018b4 100644
--- a/drivers/usb/serial/mos7840.c
+++ b/drivers/usb/serial/mos7840.c
@@ -770,6 +770,7 @@  static void mos7840_close(struct usb_serial_port *port)
 		if (mos7840_port->write_urb_pool[j]) {
 			kfree(mos7840_port->write_urb_pool[j]->transfer_buffer);
 			usb_free_urb(mos7840_port->write_urb_pool[j]);
+			mos7840_port->write_urb_pool[j] = NULL;
 		}
 	}
 
@@ -1737,6 +1738,37 @@  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)
+{
+    dev_dbg(&serial->interface->dev, "mos7840_suspend\n");
+
+    return 0;
+}
+
+static int mos7840_resume(struct usb_serial *serial)
+{
+	struct moschip_port *mos7840_port;
+	struct usb_serial_port *port;
+	int i;
+	dev_dbg(&serial->interface->dev, "mos7840_resume\n");
+
+	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)
+			usb_submit_urb(mos7840_port->read_urb, GFP_NOIO);
+
+		/*if (port->bulk_out_size)
+			usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);*/
+	}
+
+    return 0;
+}
+
 static struct usb_serial_driver moschip7840_4port_device = {
 	.driver = {
 		   .owner = THIS_MODULE,
@@ -1764,6 +1796,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[] = {