Skip to contentSkip to conversation

tty: Fix close races in USB serial

PATCH In review Alan Cox 2 participants 4 messages Nov 4, 2008

Subsystems (2)
lore
Share & tools
lore

Alan Cox

author
#0 lore raw

PATCH 1/1 tty: Fix close races in USB serial

From: Alan Cox <[email protected]>

USB serial has always had races where the tty port usage count can hit zero during a receive event. The internal locking is a mutex so we can't use that in the IRQ handlers.

With krefs we can tackle this differently but we still need to be careful.

---

 drivers/usb/serial/usb-serial.c |   15 ++++++++++-----
 1 files changed, 10 insertions(+), 5 deletions(-)
drivers/usb/serial/usb-serial.c +10 -5
index 794b5ff..aafa684 100644
@@ -269,15 +269,19 @@ static void serial_close(struct tty_struct *tty, struct file *filp)
 		return;
 	}
 
-	--port->port.count;
-	if (port->port.count == 0)
+	if (port->port.count == 1)
 		/* only call the device specific close if this
-		 * port is being closed by the last owner */
+		 * port is being closed by the last owner. Ensure we do
+		 * this before we drop the port count. The call is protected
+		 * by the port mutex
+		 */
 		port->serial->type->close(tty, port, filp);
 
-	if (port->port.count == (port->console? 1 : 0)) {
+	if (port->port.count == (port->console ? 2 : 1)) {
 		struct tty_struct *tty = tty_port_tty_get(&port->port);
 		if (tty) {
+			/* We must do this before we drop the port count to
+			   zero. */
 			if (tty->driver_data)
 				tty->driver_data = NULL;
 			tty_port_tty_set(&port->port, NULL);
@@ -285,13 +289,14 @@ static void serial_close(struct tty_struct *tty, struct file *filp)
 		}
 	}
 
-	if (port->port.count == 0) {
+	if (port->port.count == 1) {
 		mutex_lock(&port->serial->disc_mutex);
 		if (!port->serial->disconnected)
 			usb_autopm_put_interface(port->serial->interface);
 		mutex_unlock(&port->serial->disc_mutex);
 		module_put(port->serial->type->driver.owner);
 	}
+	--port->port.count;
 
 	mutex_unlock(&port->mutex);
 	usb_serial_put(port->serial);
#1 lore raw
Quoted 26 lines · Alan Cox

From: Alan Cox <[email protected]>

USB serial has always had races where the tty port usage count can hit zero during a receive event. The internal locking is a mutex so we can't use that in the IRQ handlers.

With krefs we can tackle this differently but we still need to be careful.

---

 drivers/usb/serial/usb-serial.c |   15 ++++++++++-----
 1 files changed, 10 insertions(+), 5 deletions(-)
drivers/usb/serial/usb-serial.c +1 -2
index 794b5ff..aafa684 100644
@@ -269,15 +269,19 @@ static void serial_close(struct tty_struct *tty, struct file *filp)
 		return;
 	}
 
-	--port->port.count;
-	if (port->port.count == 0)
+	if (port->port.count == 1)

drivers/usb/serial/usb-serial.c static void serial_close(struct tty_struct *tty, struct file *filp) · line 280

Quoted 10 lines
drivers/usb/serial/usb-serial.c +5 -2
 		/* only call the device specific close if this
-		 * port is being closed by the last owner */
+		 * port is being closed by the last owner. Ensure we do
+		 * this before we drop the port count. The call is protected
+		 * by the port mutex
+		 */
 		port->serial->type->close(tty, port, filp);
 
-	if (port->port.count == (port->console? 1 : 0)) {
+	if (port->port.count == (port->console ? 2 : 1)) {

Why are you testing for 2 here?

confused,

greg k-h

author
#2 lore raw
Quoted 4 lines

drivers/usb/serial/usb-serial.c static void serial_close(struct tty_struct *tty, struct file *filp) · line 280

Quoted 2 lines
drivers/usb/serial/usb-serial.c +1 -1
-	if (port->port.count == (port->console? 1 : 0)) {
+	if (port->port.count == (port->console ? 2 : 1)) {

Why are you testing for 2 here?

At the point we enter this routine port->port.count is the number of users of the port. If this is the final close then this is 1 (us), except if it is the USB console (in which case there will be two references).

The old code dropped the reference count first, the new code drops the kref and port->tty first, then shuts down the port and then drops the refcount. That avoids the race where we get the usb serial drivers trying to stuff bytes into a closed port and the ldisc layer in turn trying to echo them
- which causes the warnings.

Alan

#3 lore raw
Quoted 9 lines · Alan Cox
Quoted 4 lines

drivers/usb/serial/usb-serial.c static void serial_close(struct tty_struct *tty, struct file *filp) · line 280

Quoted 2 lines
drivers/usb/serial/usb-serial.c +1 -1
-	if (port->port.count == (port->console? 1 : 0)) {
+	if (port->port.count == (port->console ? 2 : 1)) {

Why are you testing for 2 here?

At the point we enter this routine port->port.count is the number of users of the port. If this is the final close then this is 1 (us), except if it is the USB console (in which case there will be two references).

Quoted 6 lines

The old code dropped the reference count first, the new code drops the kref and port->tty first, then shuts down the port and then drops the refcount. That avoids the race where we get the usb serial drivers trying to stuff bytes into a closed port and the ldisc layer in turn trying to echo them
- which causes the warnings.

Ah, ok, that makes sense, thanks.

You might want to document that in there so that others understand it as well.

thanks,

greg k-h