* [PATCH] USB: usblp: Add device status detection in usblp_poll() @ 2021-03-01 4:03 qiang.zhang 2021-03-01 8:00 ` Greg KH 0 siblings, 1 reply; 13+ messages in thread From: qiang.zhang @ 2021-03-01 4:03 UTC (permalink / raw) To: zaitcev, gregkh; +Cc: linux-usb From: Zqiang <qiang.zhang@windriver.com> If usblp device has been disconnected, direct return 'EPOLLHUP | EPOLLERR' in usblp_poll(). Signed-off-by: Zqiang <qiang.zhang@windriver.com> --- drivers/usb/class/usblp.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/drivers/usb/class/usblp.c b/drivers/usb/class/usblp.c index c9f6e9758288..40095c8826d2 100644 --- a/drivers/usb/class/usblp.c +++ b/drivers/usb/class/usblp.c @@ -498,6 +498,12 @@ static __poll_t usblp_poll(struct file *file, struct poll_table_struct *wait) unsigned long flags; struct usblp *usblp = file->private_data; + mutex_lock(&usblp->mut); + if (!usblp->present) { + ret = EPOLLHUP | EPOLLERR; + goto no_poll; + } + /* Should we check file->f_mode & FMODE_WRITE before poll_wait()? */ poll_wait(file, &usblp->rwait, wait); poll_wait(file, &usblp->wwait, wait); @@ -505,6 +511,8 @@ static __poll_t usblp_poll(struct file *file, struct poll_table_struct *wait) ret = ((usblp->bidir && usblp->rcomplete) ? EPOLLIN | EPOLLRDNORM : 0) | ((usblp->no_paper || usblp->wcomplete) ? EPOLLOUT | EPOLLWRNORM : 0); spin_unlock_irqrestore(&usblp->lock, flags); +no_poll: + mutex_unlock(&usblp->mut); return ret; } -- 2.25.1 ^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: [PATCH] USB: usblp: Add device status detection in usblp_poll() 2021-03-01 4:03 [PATCH] USB: usblp: Add device status detection in usblp_poll() qiang.zhang @ 2021-03-01 8:00 ` Greg KH 2021-03-01 8:26 ` 回复: " Zhang, Qiang 0 siblings, 1 reply; 13+ messages in thread From: Greg KH @ 2021-03-01 8:00 UTC (permalink / raw) To: qiang.zhang; +Cc: zaitcev, linux-usb On Mon, Mar 01, 2021 at 12:03:00PM +0800, qiang.zhang@windriver.com wrote: > From: Zqiang <qiang.zhang@windriver.com> > > If usblp device has been disconnected, direct return > 'EPOLLHUP | EPOLLERR' in usblp_poll(). Why? What problem are you solving here? Is this fixing an older commit, if so, what one? Please provide a lot more information here, this is not very descriptive. thanks, greg k-h ^ permalink raw reply [flat|nested] 13+ messages in thread
* 回复: [PATCH] USB: usblp: Add device status detection in usblp_poll() 2021-03-01 8:00 ` Greg KH @ 2021-03-01 8:26 ` Zhang, Qiang 2021-03-01 8:32 ` Greg KH 0 siblings, 1 reply; 13+ messages in thread From: Zhang, Qiang @ 2021-03-01 8:26 UTC (permalink / raw) To: Greg KH; +Cc: zaitcev, linux-usb ________________________________________ 发件人: Greg KH <gregkh@linuxfoundation.org> 发送时间: 2021年3月1日 16:00 收件人: Zhang, Qiang 抄送: zaitcev@redhat.com; linux-usb@vger.kernel.org 主题: Re: [PATCH] USB: usblp: Add device status detection in usblp_poll() [Please note: This e-mail is from an EXTERNAL e-mail address] On Mon, Mar 01, 2021 at 12:03:00PM +0800, qiang.zhang@windriver.com wrote: > From: Zqiang <qiang.zhang@windriver.com> > > If usblp device has been disconnected, direct return > 'EPOLLHUP | EPOLLERR' in usblp_poll(). > >Why? What problem are you solving here? Is this fixing an >older >commit, if so, what one? > There is not need fix older commit. After the device is disconnected, if the userspace call poll/select() function, will block and never be awakened . Thanks Qiang >Please provide a lot more information here, this is not very >descriptive. > >thanks, > >greg k-h ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: 回复: [PATCH] USB: usblp: Add device status detection in usblp_poll() 2021-03-01 8:26 ` 回复: " Zhang, Qiang @ 2021-03-01 8:32 ` Greg KH 2021-03-01 8:52 ` 回复: " Zhang, Qiang 0 siblings, 1 reply; 13+ messages in thread From: Greg KH @ 2021-03-01 8:32 UTC (permalink / raw) To: Zhang, Qiang; +Cc: zaitcev, linux-usb On Mon, Mar 01, 2021 at 08:26:22AM +0000, Zhang, Qiang wrote: > > > ________________________________________ > 发件人: Greg KH <gregkh@linuxfoundation.org> > 发送时间: 2021年3月1日 16:00 > 收件人: Zhang, Qiang > 抄送: zaitcev@redhat.com; linux-usb@vger.kernel.org > 主题: Re: [PATCH] USB: usblp: Add device status detection in usblp_poll() > > [Please note: This e-mail is from an EXTERNAL e-mail address] > > On Mon, Mar 01, 2021 at 12:03:00PM +0800, qiang.zhang@windriver.com wrote: > > From: Zqiang <qiang.zhang@windriver.com> > > > > If usblp device has been disconnected, direct return > > 'EPOLLHUP | EPOLLERR' in usblp_poll(). > > > >Why? What problem are you solving here? Is this fixing an >older > >commit, if so, what one? > > > > There is not need fix older commit. > After the device is disconnected, if the userspace call poll/select() function, will block and never be awakened . If the device is disconnected, how can the file be opened at all? Shouldn't it have been torn down already? And now you are holding a lock across some poll_wait() calls, is that going to cause problems? thanks, greg k-h ^ permalink raw reply [flat|nested] 13+ messages in thread
* 回复: 回复: [PATCH] USB: usblp: Add device status detection in usblp_poll() 2021-03-01 8:32 ` Greg KH @ 2021-03-01 8:52 ` Zhang, Qiang 2021-03-02 7:04 ` Greg KH 0 siblings, 1 reply; 13+ messages in thread From: Zhang, Qiang @ 2021-03-01 8:52 UTC (permalink / raw) To: Greg KH; +Cc: zaitcev, linux-usb ________________________________________ 发件人: Greg KH <gregkh@linuxfoundation.org> 发送时间: 2021年3月1日 16:32 收件人: Zhang, Qiang 抄送: zaitcev@redhat.com; linux-usb@vger.kernel.org 主题: Re: 回复: [PATCH] USB: usblp: Add device status detection in usblp_poll() [Please note: This e-mail is from an EXTERNAL e-mail address] On Mon, Mar 01, 2021 at 08:26:22AM +0000, Zhang, Qiang wrote: > > > ________________________________________ > 发件人: Greg KH <gregkh@linuxfoundation.org> > 发送时间: 2021年3月1日 16:00 > 收件人: Zhang, Qiang > 抄送: zaitcev@redhat.com; linux-usb@vger.kernel.org > 主题: Re: [PATCH] USB: usblp: Add device status detection in usblp_poll() > > [Please note: This e-mail is from an EXTERNAL e-mail address] > > On Mon, Mar 01, 2021 at 12:03:00PM +0800, qiang.zhang@windriver.com wrote: > > From: Zqiang <qiang.zhang@windriver.com> > > > > If usblp device has been disconnected, direct return > > 'EPOLLHUP | EPOLLERR' in usblp_poll(). > > > >Why? What problem are you solving here? Is this fixing an >older > >commit, if so, what one? > > > > There is not need fix older commit. > After the device is disconnected, if the userspace call >poll/select() function, will block and never be awakened . > >If the device is disconnected, how can the file be opened at >all? >Shouldn't it have been torn down already? If the device disconnect happen after the file be opend, and then we call poll/select() will always blocked. Also before we operate the device, we need to judge the state of the device. If it is disconnected, there is no need for subsequent operation . > >And now you are holding a lock across some poll_wait() calls, >is that >going to cause problems? The 'usblp->present' is portect by 'usblp->mut', when devices disconnect, will acquire 'usblp->mut', and set 'usblp->present' zero. Thanks Qiang > >thanks, > >greg k-h ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: 回复: 回复: [PATCH] USB: usblp: Add device status detection in usblp_poll() 2021-03-01 8:52 ` 回复: " Zhang, Qiang @ 2021-03-02 7:04 ` Greg KH 2021-03-02 7:41 ` 回复: " Zhang, Qiang 0 siblings, 1 reply; 13+ messages in thread From: Greg KH @ 2021-03-02 7:04 UTC (permalink / raw) To: Zhang, Qiang; +Cc: zaitcev, linux-usb On Mon, Mar 01, 2021 at 08:52:58AM +0000, Zhang, Qiang wrote: > > > ________________________________________ > 发件人: Greg KH <gregkh@linuxfoundation.org> > 发送时间: 2021年3月1日 16:32 > 收件人: Zhang, Qiang > 抄送: zaitcev@redhat.com; linux-usb@vger.kernel.org > 主题: Re: 回复: [PATCH] USB: usblp: Add device status detection in usblp_poll() > > [Please note: This e-mail is from an EXTERNAL e-mail address] > > On Mon, Mar 01, 2021 at 08:26:22AM +0000, Zhang, Qiang wrote: > > > > > > ________________________________________ > > 发件人: Greg KH <gregkh@linuxfoundation.org> > > 发送时间: 2021年3月1日 16:00 > > 收件人: Zhang, Qiang > > 抄送: zaitcev@redhat.com; linux-usb@vger.kernel.org > > 主题: Re: [PATCH] USB: usblp: Add device status detection in usblp_poll() > > > > [Please note: This e-mail is from an EXTERNAL e-mail address] > > > > On Mon, Mar 01, 2021 at 12:03:00PM +0800, qiang.zhang@windriver.com wrote: > > > From: Zqiang <qiang.zhang@windriver.com> > > > > > > If usblp device has been disconnected, direct return > > > 'EPOLLHUP | EPOLLERR' in usblp_poll(). > > > > > >Why? What problem are you solving here? Is this fixing an >older > > >commit, if so, what one? > > > > > > > There is not need fix older commit. > > After the device is disconnected, if the userspace call >poll/select() function, will block and never be awakened . > > > >If the device is disconnected, how can the file be opened at >all? > >Shouldn't it have been torn down already? > > If the device disconnect happen after the file be opend, and then we call poll/select() will always blocked. > Also before we operate the device, we need to judge the state of the device. If it is disconnected, there is no need for subsequent operation . But shouldn't the normal call to poll_wait() fail here? > >And now you are holding a lock across some poll_wait() calls, >is that > >going to cause problems? > > The 'usblp->present' is portect by 'usblp->mut', when devices disconnect, will acquire 'usblp->mut', and set 'usblp->present' > zero. But you are now holding a mutex across a very very long period of time, what will that change? thanks, greg k-h ^ permalink raw reply [flat|nested] 13+ messages in thread
* 回复: 回复: 回复: [PATCH] USB: usblp: Add device status detection in usblp_poll() 2021-03-02 7:04 ` Greg KH @ 2021-03-02 7:41 ` Zhang, Qiang 2021-03-03 5:12 ` Pete Zaitcev 0 siblings, 1 reply; 13+ messages in thread From: Zhang, Qiang @ 2021-03-02 7:41 UTC (permalink / raw) To: Greg KH; +Cc: zaitcev, linux-usb On Mon, Mar 01, 2021 at 08:52:58AM +0000, Zhang, Qiang wrote: > > > ________________________________________ > 发件人: Greg KH <gregkh@linuxfoundation.org> > 发送时间: 2021年3月1日 16:32 > 收件人: Zhang, Qiang > 抄送: zaitcev@redhat.com; linux-usb@vger.kernel.org > 主题: Re: 回复: [PATCH] USB: usblp: Add device status detection in usblp_poll() > > [Please note: This e-mail is from an EXTERNAL e-mail address] > > On Mon, Mar 01, 2021 at 08:26:22AM +0000, Zhang, Qiang wrote: > > > > > > ________________________________________ > > 发件人: Greg KH <gregkh@linuxfoundation.org> > > 发送时间: 2021年3月1日 16:00 > > 收件人: Zhang, Qiang > > 抄送: zaitcev@redhat.com; linux-usb@vger.kernel.org > > 主题: Re: [PATCH] USB: usblp: Add device status detection in usblp_poll() > > > > [Please note: This e-mail is from an EXTERNAL e-mail address] > > > > On Mon, Mar 01, 2021 at 12:03:00PM +0800, qiang.zhang@windriver.com wrote: > > > From: Zqiang <qiang.zhang@windriver.com> > > > > > > If usblp device has been disconnected, direct return > > > 'EPOLLHUP | EPOLLERR' in usblp_poll(). > > > > > >Why? What problem are you solving here? Is this fixing an >older > > >commit, if so, what one? > > > > > > > There is not need fix older commit. > > After the device is disconnected, if the userspace call >poll/select() function, will block and never be awakened . > > > >If the device is disconnected, how can the file be opened at >all? > >Shouldn't it have been torn down already? > > If the device disconnect happen after the file be opend, and then we call poll/select() will always blocked. > Also before we operate the device, we need to judge the state of the >device. If it is disconnected, there is no need for subsequent >operation . > >But shouldn't the normal call to poll_wait() fail here? poll_wait() is not fail, its main operation is to call add_wait_queue() > > >And now you are holding a lock across some poll_wait() calls, >is >that > >going to cause problems? > > The 'usblp->present' is portect by 'usblp->mut', when devices disconnect, will acquire 'usblp->mut', and set 'usblp->present' > zero. > >But you are now holding a mutex across a very very long period of >time, >what will that change? maybe acquire mutex lock will take a long time, but it's safe. and also I find similar usage in usb/class/usbtmc.c thanks Qiang > >thanks, > >greg k-h ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH] USB: usblp: Add device status detection in usblp_poll() 2021-03-02 7:41 ` 回复: " Zhang, Qiang @ 2021-03-03 5:12 ` Pete Zaitcev 2021-03-03 6:12 ` 回复: " Zhang, Qiang 2021-03-03 17:13 ` Alan Stern 0 siblings, 2 replies; 13+ messages in thread From: Pete Zaitcev @ 2021-03-03 5:12 UTC (permalink / raw) To: Zhang, Qiang; +Cc: Greg KH, linux-usb, zaitcev On Tue, 2 Mar 2021 07:41:07 +0000 "Zhang, Qiang" <Qiang.Zhang@windriver.com> wrote: > and also I find similar usage in usb/class/usbtmc.c Seems like a bug indeed, but I don't like the example in usbtmc.c. Please let me know if the following is acceptable: commit 83591ac63bc666a44f250b43af6c0f5a1e001841 Author: Pete Zaitcev <zaitcev@kotori.zaitcev.us> Date: Tue Mar 2 23:00:28 2021 -0600 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 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. Reported-by: Zqiang <qiang.zhang@windriver.com> Signed-off-by: Pete Zaitcev <zaitcev@redhat.com> diff --git a/drivers/usb/class/usblp.c b/drivers/usb/class/usblp.c index fd87405adbed..5733a0067f5b 100644 --- a/drivers/usb/class/usblp.c +++ b/drivers/usb/class/usblp.c @@ -494,16 +494,24 @@ static int usblp_release(struct inode *inode, struct file *file) /* No kernel lock - fine */ static __poll_t usblp_poll(struct file *file, struct poll_table_struct *wait) { - __poll_t ret; + struct usblp *usblp = file->private_data; + __poll_t ret = 0; unsigned long flags; - struct usblp *usblp = file->private_data; /* Should we check file->f_mode & FMODE_WRITE before poll_wait()? */ poll_wait(file, &usblp->rwait, wait); poll_wait(file, &usblp->wwait, wait); + + mutex_lock(&usblp->mut); + if (!usblp->present) + ret != EPOLLHUP; + mutex_unlock(&usblp->mut); + spin_lock_irqsave(&usblp->lock, flags); - ret = ((usblp->bidir && usblp->rcomplete) ? EPOLLIN | EPOLLRDNORM : 0) | - ((usblp->no_paper || usblp->wcomplete) ? EPOLLOUT | EPOLLWRNORM : 0); + if (usblp->bidir && usblp->rcomplete) + ret |= EPOLLIN | EPOLLRDNORM; + if (usblp->no_paper || usblp->wcomplete) + ret |= EPOLLOUT | EPOLLWRNORM; spin_unlock_irqrestore(&usblp->lock, flags); return ret; } ^ permalink raw reply related [flat|nested] 13+ messages in thread
* 回复: [PATCH] USB: usblp: Add device status detection in usblp_poll() 2021-03-03 5:12 ` Pete Zaitcev @ 2021-03-03 6:12 ` Zhang, Qiang 2021-03-03 6:56 ` Greg KH 2021-03-03 7:15 ` Zhang, Qiang 2021-03-03 17:13 ` Alan Stern 1 sibling, 2 replies; 13+ messages in thread From: Zhang, Qiang @ 2021-03-03 6:12 UTC (permalink / raw) To: Pete Zaitcev; +Cc: Greg KH, linux-usb >________________________________________ >发件人: Pete Zaitcev <zaitcev@redhat.com> >发送时间: 2021年3月3日 13:12 >收件人: Zhang, Qiang >抄送: Greg KH; linux-usb@vger.kernel.org; zaitcev@redhat.com >主题: Re: [PATCH] USB: usblp: Add device status detection in >usblp_poll() > >[Please note: This e-mail is from an EXTERNAL e-mail address] > >On Tue, 2 Mar 2021 07:41:07 +0000 >"Zhang, Qiang" <Qiang.Zhang@windriver.com> wrote: > and also I find similar usage in usb/class/usbtmc.c >Seems like a bug indeed, but I don't like the example in usbtmc.c. >Please let me know if the following is acceptable: I agree with this change . Thanks Qiang > >commit 83591ac63bc666a44f250b43af6c0f5a1e001841 >Author: Pete Zaitcev <zaitcev@kotori.zaitcev.us> >Date: Tue Mar 2 23:00:28 2021 -0600 > > 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 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. > > Reported-by: Zqiang <qiang.zhang@windriver.com> > Signed-off-by: Pete Zaitcev <zaitcev@redhat.com> > >diff --git a/drivers/usb/class/usblp.c b/drivers/usb/class/usblp.c >index fd87405adbed..5733a0067f5b 100644 >--- a/drivers/usb/class/usblp.c >+++ b/drivers/usb/class/usblp.c >@@ -494,16 +494,24 @@ static int usblp_release(struct inode *inode, >struct file *file) >/* No kernel lock - fine */ > static __poll_t usblp_poll(struct file *file, struct poll_table_struct >*wait) > { >- __poll_t ret; >+ struct usblp *usblp = file->private_data; >+ __poll_t ret = 0; > unsigned long flags; > >- struct usblp *usblp = file->private_data; > /* Should we check file->f_mode & FMODE_WRITE before >poll_wait()? */ > poll_wait(file, &usblp->rwait, wait); > poll_wait(file, &usblp->wwait, wait); >+ >+ mutex_lock(&usblp->mut); >+ if (!usblp->present) >+ ret != EPOLLHUP; >+ mutex_unlock(&usblp->mut); >+ > spin_lock_irqsave(&usblp->lock, flags); >- ret = ((usblp->bidir && usblp->rcomplete) ? EPOLLIN | >EPOLLRDNORM : 0) | >- ((usblp->no_paper || usblp->wcomplete) ? EPOLLOUT | >EPOLLWRNORM : 0); >+ if (usblp->bidir && usblp->rcomplete) >+ ret |= EPOLLIN | EPOLLRDNORM; >+ if (usblp->no_paper || usblp->wcomplete) >+ ret |= EPOLLOUT | EPOLLWRNORM; > spin_unlock_irqrestore(&usblp->lock, flags); > return ret; > } ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: 回复: [PATCH] USB: usblp: Add device status detection in usblp_poll() 2021-03-03 6:12 ` 回复: " Zhang, Qiang @ 2021-03-03 6:56 ` Greg KH 2021-03-03 7:15 ` Zhang, Qiang 1 sibling, 0 replies; 13+ messages in thread From: Greg KH @ 2021-03-03 6:56 UTC (permalink / raw) To: Zhang, Qiang; +Cc: Pete Zaitcev, linux-usb On Wed, Mar 03, 2021 at 06:12:47AM +0000, Zhang, Qiang wrote: > > > >________________________________________ > >发件人: Pete Zaitcev <zaitcev@redhat.com> > >发送时间: 2021年3月3日 13:12 > >收件人: Zhang, Qiang > >抄送: Greg KH; linux-usb@vger.kernel.org; zaitcev@redhat.com > >主题: Re: [PATCH] USB: usblp: Add device status detection in >usblp_poll() > > > >[Please note: This e-mail is from an EXTERNAL e-mail address] > > > >On Tue, 2 Mar 2021 07:41:07 +0000 > >"Zhang, Qiang" <Qiang.Zhang@windriver.com> wrote: > > > and also I find similar usage in usb/class/usbtmc.c > > >Seems like a bug indeed, but I don't like the example in usbtmc.c. > >Please let me know if the following is acceptable: > > I agree with this change . Great, can I get a "Reviewed-by:" line from you then? thanks, greg k-h ^ permalink raw reply [flat|nested] 13+ messages in thread
* 回复: [PATCH] USB: usblp: Add device status detection in usblp_poll() 2021-03-03 6:12 ` 回复: " Zhang, Qiang 2021-03-03 6:56 ` Greg KH @ 2021-03-03 7:15 ` Zhang, Qiang 2021-03-03 15:57 ` Pete Zaitcev 1 sibling, 1 reply; 13+ messages in thread From: Zhang, Qiang @ 2021-03-03 7:15 UTC (permalink / raw) To: Pete Zaitcev, Greg KH; +Cc: linux-usb ________________________________________ 发件人: Zhang, Qiang <Qiang.Zhang@windriver.com> 发送时间: 2021年3月3日 14:12 收件人: Pete Zaitcev 抄送: Greg KH; linux-usb@vger.kernel.org 主题: 回复: [PATCH] USB: usblp: Add device status detection in usblp_poll() >________________________________________ >发件人: Pete Zaitcev <zaitcev@redhat.com> >发送时间: 2021年3月3日 13:12 >收件人: Zhang, Qiang >抄送: Greg KH; linux-usb@vger.kernel.org; zaitcev@redhat.com >主题: Re: [PATCH] USB: usblp: Add device status detection in >usblp_poll() > >[Please note: This e-mail is from an EXTERNAL e-mail address] > >On Tue, 2 Mar 2021 07:41:07 +0000 >"Zhang, Qiang" <Qiang.Zhang@windriver.com> wrote: > > and also I find similar usage in usb/class/usbtmc.c > >Seems like a bug indeed, but I don't like the example in usbtmc.c. >Please let me know if the following is acceptable: > >I agree with this change . > >Thanks >Qiang > > >commit 83591ac63bc666a44f250b43af6c0f5a1e001841 >Author: Pete Zaitcev <zaitcev@kotori.zaitcev.us> >Date: Tue Mar 2 23:00:28 2021 -0600 > > 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 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. > > Reported-by: Zqiang <qiang.zhang@windriver.com> > Signed-off-by: Pete Zaitcev <zaitcev@redhat.com> > >diff --git a/drivers/usb/class/usblp.c b/drivers/usb/class/usblp.c >index fd87405adbed..5733a0067f5b 100644 >--- a/drivers/usb/class/usblp.c >+++ b/drivers/usb/class/usblp.c >@@ -494,16 +494,24 @@ static int usblp_release(struct inode *inode, >struct file *file) >/* No kernel lock - fine */ > static __poll_t usblp_poll(struct file *file, struct poll_table_struct >*wait) > { >- __poll_t ret; >+ struct usblp *usblp = file->private_data; >+ __poll_t ret = 0; > unsigned long flags; > >- struct usblp *usblp = file->private_data; > /* Should we check file->f_mode & FMODE_WRITE before >poll_wait()? */ > poll_wait(file, &usblp->rwait, wait); > poll_wait(file, &usblp->wwait, wait); >+ >+ mutex_lock(&usblp->mut); >+ if (!usblp->present) >+ ret != EPOLLHUP; Should it be ret |= EPOLLHUP; Reviewed-by: Zqiang <qiang.zhang@windriver.com> Thanks Qiang >+ mutex_unlock(&usblp->mut); >+ > spin_lock_irqsave(&usblp->lock, flags); >- ret = ((usblp->bidir && usblp->rcomplete) ? EPOLLIN | >EPOLLRDNORM : 0) | >- ((usblp->no_paper || usblp->wcomplete) ? EPOLLOUT | >EPOLLWRNORM : 0); >+ if (usblp->bidir && usblp->rcomplete) >+ ret |= EPOLLIN | EPOLLRDNORM; >+ if (usblp->no_paper || usblp->wcomplete) >+ ret |= EPOLLOUT | EPOLLWRNORM; > spin_unlock_irqrestore(&usblp->lock, flags); > return ret; > } ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH] USB: usblp: Add device status detection in usblp_poll() 2021-03-03 7:15 ` Zhang, Qiang @ 2021-03-03 15:57 ` Pete Zaitcev 0 siblings, 0 replies; 13+ messages in thread From: Pete Zaitcev @ 2021-03-03 15:57 UTC (permalink / raw) To: Zhang, Qiang; +Cc: Greg KH, linux-usb On Wed, 3 Mar 2021 07:15:08 +0000 "Zhang, Qiang" <Qiang.Zhang@windriver.com> wrote: > >+ if (!usblp->present) > >+ ret != EPOLLHUP; > Should it be ret |= EPOLLHUP; > Reviewed-by: Zqiang <qiang.zhang@windriver.com> Ouch. I'll fix that, do some (sadly minimal) tests, and submit. -- Pete ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH] USB: usblp: Add device status detection in usblp_poll() 2021-03-03 5:12 ` Pete Zaitcev 2021-03-03 6:12 ` 回复: " Zhang, Qiang @ 2021-03-03 17:13 ` Alan Stern 1 sibling, 0 replies; 13+ messages in thread From: Alan Stern @ 2021-03-03 17:13 UTC (permalink / raw) To: Pete Zaitcev; +Cc: Zhang, Qiang, Greg KH, linux-usb On Tue, Mar 02, 2021 at 11:12:54PM -0600, Pete Zaitcev wrote: > On Tue, 2 Mar 2021 07:41:07 +0000 > "Zhang, Qiang" <Qiang.Zhang@windriver.com> wrote: > > > and also I find similar usage in usb/class/usbtmc.c > > Seems like a bug indeed, but I don't like the example in usbtmc.c. > Please let me know if the following is acceptable: > > commit 83591ac63bc666a44f250b43af6c0f5a1e001841 > Author: Pete Zaitcev <zaitcev@kotori.zaitcev.us> > Date: Tue Mar 2 23:00:28 2021 -0600 > > 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 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. > > Reported-by: Zqiang <qiang.zhang@windriver.com> > Signed-off-by: Pete Zaitcev <zaitcev@redhat.com> > > diff --git a/drivers/usb/class/usblp.c b/drivers/usb/class/usblp.c > index fd87405adbed..5733a0067f5b 100644 > --- a/drivers/usb/class/usblp.c > +++ b/drivers/usb/class/usblp.c > @@ -494,16 +494,24 @@ static int usblp_release(struct inode *inode, struct file *file) > /* No kernel lock - fine */ > static __poll_t usblp_poll(struct file *file, struct poll_table_struct *wait) > { > - __poll_t ret; > + struct usblp *usblp = file->private_data; > + __poll_t ret = 0; > unsigned long flags; > > - struct usblp *usblp = file->private_data; > /* Should we check file->f_mode & FMODE_WRITE before poll_wait()? */ > poll_wait(file, &usblp->rwait, wait); > poll_wait(file, &usblp->wwait, wait); > + > + mutex_lock(&usblp->mut); > + if (!usblp->present) > + ret != EPOLLHUP; Typo: ! instead of |. You have to look closely to see the difference. alan Stern > + mutex_unlock(&usblp->mut); > + > spin_lock_irqsave(&usblp->lock, flags); > - ret = ((usblp->bidir && usblp->rcomplete) ? EPOLLIN | EPOLLRDNORM : 0) | > - ((usblp->no_paper || usblp->wcomplete) ? EPOLLOUT | EPOLLWRNORM : 0); > + if (usblp->bidir && usblp->rcomplete) > + ret |= EPOLLIN | EPOLLRDNORM; > + if (usblp->no_paper || usblp->wcomplete) > + ret |= EPOLLOUT | EPOLLWRNORM; > spin_unlock_irqrestore(&usblp->lock, flags); > return ret; > } > ^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2021-03-04 0:28 UTC | newest] Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2021-03-01 4:03 [PATCH] USB: usblp: Add device status detection in usblp_poll() qiang.zhang 2021-03-01 8:00 ` Greg KH 2021-03-01 8:26 ` 回复: " Zhang, Qiang 2021-03-01 8:32 ` Greg KH 2021-03-01 8:52 ` 回复: " Zhang, Qiang 2021-03-02 7:04 ` Greg KH 2021-03-02 7:41 ` 回复: " Zhang, Qiang 2021-03-03 5:12 ` Pete Zaitcev 2021-03-03 6:12 ` 回复: " Zhang, Qiang 2021-03-03 6:56 ` Greg KH 2021-03-03 7:15 ` Zhang, Qiang 2021-03-03 15:57 ` Pete Zaitcev 2021-03-03 17:13 ` Alan Stern
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).