Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3520588pxf; Mon, 15 Mar 2021 11:23:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzCckxXE0BrbMhK1Ya9dwvPoVkiTRS/a3QfAHBOytsMkrQUo478/WgmW4UxrpASgBxv3nyN X-Received: by 2002:a50:f391:: with SMTP id g17mr31230970edm.26.1615832591010; Mon, 15 Mar 2021 11:23:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1615832591; cv=none; d=google.com; s=arc-20160816; b=JdqfGd69bAiNjOFlcAtYP+4lVmbHFuxd5yuBLfC8QN8eZh9v7b/S6C0ggL5GghB5AY e/CD4ALY+JjvZtpe+uREtPYVoUinzFWoGHGngvhNOiORjC5hRAoeqbCJd0z7Egrry9uI qRDHPDmAFEiFPErIGTnJD3uqvWMarR2GLc4QnavlitkXXPpF50PRpUIPGRYaqOrQDUIi yR9+2mfDkVFZtlkxMk2jmmtZ/Mj4+u66mIR0RBspAwCgk5TAJsSV20BplmaLEIz/3IZb N6oS6qt8jFmn/8AcCE1ha1bfTJcZNcD454U2KxUlI0hyuoh3kqn+pH3fMoGEMnUepQa6 Geyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=dzEo42VYW09K8B3N4/na2Eb1a7NBEvbWa+UJChW7gNE=; b=flIBsGyXXQyREvaXqFaVQtHvzGeoTl0i/Q+BJrrBy0rbZoXWEmGAp5drCBRWoW6ZAC CBUoPym+E9ZrBD73NE2APckDzfLedvagnZJbd8TPPzkyL+Gs+ZwAFrMOVkADA9nsO9Qv HvbCQQ0X3zIuYFWXS56dPVAQl11dcbhGRbIn45aKpfMoGQ/hhkdXyPpxwyFgVFHYigw5 /Rq2iKVao0KDDn/RZbKMtiTRVnb2l7sGgZAoVVBCNu3lU5Vx0DE3kSUFJXXZgCyx9ini OZTBh8n+F3e6S+BM+M8I4ELb3D1XIPENAo8XTI3Zz1x1kFntv6pSdBtfwMqYal81i2U5 uzyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=iUH7XHQB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a5si3833988ejj.325.2021.03.15.11.22.49; Mon, 15 Mar 2021 11:23:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=iUH7XHQB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238024AbhCOOWb (ORCPT + 99 others); Mon, 15 Mar 2021 10:22:31 -0400 Received: from mail.kernel.org ([198.145.29.99]:37836 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232936AbhCOOAM (ORCPT ); Mon, 15 Mar 2021 10:00:12 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 6C6FD64EEF; Mon, 15 Mar 2021 13:59:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1615816798; bh=WdSA0u9QNjoXc1vg2BJB76lxbzJAX+Hz+pf+MzpjaqE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iUH7XHQBzyt6yIPNGTKs+izsYS0uDKXD+oHGpxEUXskaPtckj9yYi2C0Aq238dhpS 0Q4Cg/yp7TKUSEgq9bcM/avLsYqWvK4hrQEl5WEYFbc7xoO6yMXTnnarwjCf+rMvqR zv3oS8U93+wF5MzjVHX7YKeLX+fhFJzIPgHc0jZQ= From: gregkh@linuxfoundation.org To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Zqiang , Pete Zaitcev Subject: [PATCH 4.19 075/120] USB: usblp: fix a hang in poll() if disconnected Date: Mon, 15 Mar 2021 14:57:06 +0100 Message-Id: <20210315135722.433601433@linuxfoundation.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210315135720.002213995@linuxfoundation.org> References: <20210315135720.002213995@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Greg Kroah-Hartman From: Pete Zaitcev commit 9de2c43acf37a17dc4c69ff78bb099b80fb74325 upstream. 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 Signed-off-by: Pete Zaitcev Cc: stable Link: https://lore.kernel.org/r/20210303221053.1cf3313e@suzdal.zaitcev.lan Signed-off-by: Greg Kroah-Hartman --- drivers/usb/class/usblp.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) --- a/drivers/usb/class/usblp.c +++ b/drivers/usb/class/usblp.c @@ -494,16 +494,24 @@ static int usblp_release(struct inode *i /* 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; }