usb: serial: prevent suspend/resume from racing against probe/remove

Some usb-serial drivers may access port data in their suspend/
resume functions. Such drivers must always verify the validity
of the data as both suspend and resume can be called both before
usb_serial_device_probe and after usb_serial_device_remove.

But the port data may be invalidated during port_probe and
port_remove. This patch prevents the race against suspend and
resume by disabling suspend while port_probe or port_remove is
running.

Suggested-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Bjørn Mork <bjorn@mork.no>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
Bjørn Mork 2012-07-27 01:11:42 +02:00 коммит произвёл Greg Kroah-Hartman
Родитель a1028f0abf
Коммит d5fd650cfc
1 изменённых файлов: 13 добавлений и 2 удалений

Просмотреть файл

@ -61,18 +61,23 @@ static int usb_serial_device_probe(struct device *dev)
goto exit; goto exit;
} }
/* make sure suspend/resume doesn't race against port_probe */
retval = usb_autopm_get_interface(port->serial->interface);
if (retval)
goto exit;
driver = port->serial->type; driver = port->serial->type;
if (driver->port_probe) { if (driver->port_probe) {
retval = driver->port_probe(port); retval = driver->port_probe(port);
if (retval) if (retval)
goto exit; goto exit_with_autopm;
} }
retval = device_create_file(dev, &dev_attr_port_number); retval = device_create_file(dev, &dev_attr_port_number);
if (retval) { if (retval) {
if (driver->port_remove) if (driver->port_remove)
retval = driver->port_remove(port); retval = driver->port_remove(port);
goto exit; goto exit_with_autopm;
} }
minor = port->number; minor = port->number;
@ -81,6 +86,8 @@ static int usb_serial_device_probe(struct device *dev)
"%s converter now attached to ttyUSB%d\n", "%s converter now attached to ttyUSB%d\n",
driver->description, minor); driver->description, minor);
exit_with_autopm:
usb_autopm_put_interface(port->serial->interface);
exit: exit:
return retval; return retval;
} }
@ -96,6 +103,9 @@ static int usb_serial_device_remove(struct device *dev)
if (!port) if (!port)
return -ENODEV; return -ENODEV;
/* make sure suspend/resume doesn't race against port_remove */
usb_autopm_get_interface(port->serial->interface);
device_remove_file(&port->dev, &dev_attr_port_number); device_remove_file(&port->dev, &dev_attr_port_number);
driver = port->serial->type; driver = port->serial->type;
@ -107,6 +117,7 @@ static int usb_serial_device_remove(struct device *dev)
dev_info(dev, "%s converter now disconnected from ttyUSB%d\n", dev_info(dev, "%s converter now disconnected from ttyUSB%d\n",
driver->description, minor); driver->description, minor);
usb_autopm_put_interface(port->serial->interface);
return retval; return retval;
} }