diff options
author | Alan Cox <alan@redhat.com> | 2009-01-02 08:44:04 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2009-01-02 13:19:37 -0500 |
commit | 4bd43f2c31848d751f63e8753cd2788d48fb5f30 (patch) | |
tree | f88e890bbee89421271437761024130e0c831853 /drivers/usb/serial/usb-serial.c | |
parent | 7e94b1d9bffc18dca3b45554d9d118a3ffcc4d1b (diff) |
tty: Fix close races in USB serial
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.
Signed-off-by: Alan Cox <alan@redhat.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'drivers/usb/serial/usb-serial.c')
-rw-r--r-- | drivers/usb/serial/usb-serial.c | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/drivers/usb/serial/usb-serial.c b/drivers/usb/serial/usb-serial.c index 794b5ffe4397..aafa684a900f 100644 --- a/drivers/usb/serial/usb-serial.c +++ b/drivers/usb/serial/usb-serial.c | |||
@@ -269,15 +269,19 @@ static void serial_close(struct tty_struct *tty, struct file *filp) | |||
269 | return; | 269 | return; |
270 | } | 270 | } |
271 | 271 | ||
272 | --port->port.count; | 272 | if (port->port.count == 1) |
273 | if (port->port.count == 0) | ||
274 | /* only call the device specific close if this | 273 | /* only call the device specific close if this |
275 | * port is being closed by the last owner */ | 274 | * port is being closed by the last owner. Ensure we do |
275 | * this before we drop the port count. The call is protected | ||
276 | * by the port mutex | ||
277 | */ | ||
276 | port->serial->type->close(tty, port, filp); | 278 | port->serial->type->close(tty, port, filp); |
277 | 279 | ||
278 | if (port->port.count == (port->console? 1 : 0)) { | 280 | if (port->port.count == (port->console ? 2 : 1)) { |
279 | struct tty_struct *tty = tty_port_tty_get(&port->port); | 281 | struct tty_struct *tty = tty_port_tty_get(&port->port); |
280 | if (tty) { | 282 | if (tty) { |
283 | /* We must do this before we drop the port count to | ||
284 | zero. */ | ||
281 | if (tty->driver_data) | 285 | if (tty->driver_data) |
282 | tty->driver_data = NULL; | 286 | tty->driver_data = NULL; |
283 | tty_port_tty_set(&port->port, NULL); | 287 | tty_port_tty_set(&port->port, NULL); |
@@ -285,13 +289,14 @@ static void serial_close(struct tty_struct *tty, struct file *filp) | |||
285 | } | 289 | } |
286 | } | 290 | } |
287 | 291 | ||
288 | if (port->port.count == 0) { | 292 | if (port->port.count == 1) { |
289 | mutex_lock(&port->serial->disc_mutex); | 293 | mutex_lock(&port->serial->disc_mutex); |
290 | if (!port->serial->disconnected) | 294 | if (!port->serial->disconnected) |
291 | usb_autopm_put_interface(port->serial->interface); | 295 | usb_autopm_put_interface(port->serial->interface); |
292 | mutex_unlock(&port->serial->disc_mutex); | 296 | mutex_unlock(&port->serial->disc_mutex); |
293 | module_put(port->serial->type->driver.owner); | 297 | module_put(port->serial->type->driver.owner); |
294 | } | 298 | } |
299 | --port->port.count; | ||
295 | 300 | ||
296 | mutex_unlock(&port->mutex); | 301 | mutex_unlock(&port->mutex); |
297 | usb_serial_put(port->serial); | 302 | usb_serial_put(port->serial); |