Skip to content

Commit c56d300

Browse files
Alan-Coxtorvalds
authored andcommitted
usb_serial: Fix remaining ref count/lock bugs
This fixes - locking bug that was hidden by ecc2e05 - Regression #13821 - Spurious warning when closing and blocking for data write out With these changes my PL2303 always ends up as ttyUSB0 when it should and the module refcounts stay correct. I'll do a more wholesale split & tidy of _open in the next release or two as we get a standard tty_port_open and port->ops->init port->ops->shutdown call backs. Copy sent to Alan Stern and Carlos Mafra just to confirm it fixes all the reports but it passes local testing with the same hardware as Alan Stern. Signed-off-by: Alan Cox <alan@linux.intel.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent fc013a5 commit c56d300

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

‎drivers/usb/serial/usb-serial.c‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <linux/mutex.h>
3333
#include <linux/list.h>
3434
#include <linux/uaccess.h>
35+
#include <linux/serial.h>
3536
#include <linux/usb.h>
3637
#include <linux/usb/serial.h>
3738
#include "pl2303.h"
@@ -184,6 +185,7 @@ static int serial_open (struct tty_struct *tty, struct file *filp)
184185
struct usb_serial_port *port;
185186
unsigned int portNumber;
186187
int retval = 0;
188+
int first = 0;
187189

188190
dbg("%s", __func__);
189191

@@ -223,7 +225,7 @@ static int serial_open (struct tty_struct *tty, struct file *filp)
223225

224226
/* If the console is attached, the device is already open */
225227
if (port->port.count == 1 && !port->console) {
226-
228+
first = 1;
227229
/* lock this module before we call it
228230
* this may fail, which means we must bail out,
229231
* safe because we are called with BKL held */
@@ -246,13 +248,21 @@ static int serial_open (struct tty_struct *tty, struct file *filp)
246248
if (retval)
247249
goto bailout_interface_put;
248250
mutex_unlock(&serial->disc_mutex);
251+
set_bit(ASYNCB_INITIALIZED, &port->port.flags);
249252
}
250253
mutex_unlock(&port->mutex);
251254
/* Now do the correct tty layer semantics */
252255
retval = tty_port_block_til_ready(&port->port, tty, filp);
253-
if (retval == 0)
256+
if (retval == 0) {
257+
if (!first)
258+
usb_serial_put(serial);
254259
return 0;
255-
260+
}
261+
mutex_lock(&port->mutex);
262+
if (first == 0)
263+
goto bailout_mutex_unlock;
264+
/* Undo the initial port actions */
265+
mutex_lock(&serial->disc_mutex);
256266
bailout_interface_put:
257267
usb_autopm_put_interface(serial->interface);
258268
bailout_module_put:
@@ -411,7 +421,6 @@ static int serial_chars_in_buffer(struct tty_struct *tty)
411421
struct usb_serial_port *port = tty->driver_data;
412422
dbg("%s = port %d", __func__, port->number);
413423

414-
WARN_ON(!port->port.count);
415424
/* if the device was unplugged then any remaining characters
416425
fell out of the connector ;) */
417426
if (port->serial->disconnected)

0 commit comments

Comments
 (0)