Skip to content

Commit 9de2c43

Browse files
Pete Zaitcevgregkh
authored andcommitted
USB: usblp: fix a hang in poll() if disconnected
Apparently an application that opens a device and calls select() on it, will hang if the decice is disconnected. It's a little surprising that we had this bug for 15 years, but apparently nobody ever uses select() with a printer: only write() and read(), and those work fine. Well, you can also select() with a timeout. The fix is modeled after devio.c. A few other drivers check the condition first, then do not add the wait queue in case the device is disconnected. We doubt that's completely race-free. So, this patch adds the process first, then locks properly and checks for the disconnect. Reviewed-by: Zqiang <qiang.zhang@windriver.com> Signed-off-by: Pete Zaitcev <zaitcev@redhat.com> Cc: stable <stable@vger.kernel.org> Link: https://lore.kernel.org/r/20210303221053.1cf3313e@suzdal.zaitcev.lan Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 414c20d commit 9de2c43

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

drivers/usb/class/usblp.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -494,16 +494,24 @@ static int usblp_release(struct inode *inode, struct file *file)
494494
/* No kernel lock - fine */
495495
static __poll_t usblp_poll(struct file *file, struct poll_table_struct *wait)
496496
{
497-
__poll_t ret;
497+
struct usblp *usblp = file->private_data;
498+
__poll_t ret = 0;
498499
unsigned long flags;
499500

500-
struct usblp *usblp = file->private_data;
501501
/* Should we check file->f_mode & FMODE_WRITE before poll_wait()? */
502502
poll_wait(file, &usblp->rwait, wait);
503503
poll_wait(file, &usblp->wwait, wait);
504+
505+
mutex_lock(&usblp->mut);
506+
if (!usblp->present)
507+
ret |= EPOLLHUP;
508+
mutex_unlock(&usblp->mut);
509+
504510
spin_lock_irqsave(&usblp->lock, flags);
505-
ret = ((usblp->bidir && usblp->rcomplete) ? EPOLLIN | EPOLLRDNORM : 0) |
506-
((usblp->no_paper || usblp->wcomplete) ? EPOLLOUT | EPOLLWRNORM : 0);
511+
if (usblp->bidir && usblp->rcomplete)
512+
ret |= EPOLLIN | EPOLLRDNORM;
513+
if (usblp->no_paper || usblp->wcomplete)
514+
ret |= EPOLLOUT | EPOLLWRNORM;
507515
spin_unlock_irqrestore(&usblp->lock, flags);
508516
return ret;
509517
}

0 commit comments

Comments
 (0)