Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3521202pxf; Mon, 15 Mar 2021 11:24:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxT6TDTXCCK1rGyZn9N5UgFdKBBHcI3amx5ev8uD2RuPICPkhaFezZakpRevzQhfFisHd/5 X-Received: by 2002:a05:6402:3493:: with SMTP id v19mr26345454edc.355.1615832648140; Mon, 15 Mar 2021 11:24:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1615832648; cv=none; d=google.com; s=arc-20160816; b=ZTQBBW6oF0hCHJkRtHtfOBs3KzQQi7UBCzXINbIjQV1Ys8l39F49VE/8Bp8qlK8jUu G8AY4zDIbm3GuhenPOjd4Etf8rZWtKPbhEGMG+H9287rm/gbUaxbIONgRvAtrW3marWs Bq6Y7APoOlhsCbX8/gFNebtVBhFqzOiBHhfVNoCQx0nS1bOgPOF8W+vCo6qaog6qXo5H A5m7YiZh/tEMg8dwDEFMkVrz1MIrt5/rR5/eseFRrwoxuuB1mRb7MOPJAusinQysG4a/ CmruTYbs70O+Q5xKDPDnSu2B2BlCpnf+Tsp8H+bnuOy6lRxFYtMaqNYcW0SG6hGRGk7t 2Nsg== 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=DIBSvI0q2GHSKdp5D9zRvSc03LlhsucjjdH5d3tZ3H/s1iQVOTg/KXNrmPsppgQ3DQ 96QPC3uCElP5KRe5OhSyViGzGvg/wzfBxOCjf2JnNApjindJGvbtPiQKM/yVJROjrQGW 2UOOlnEro3Xme2yg8o8ZuFqd4eqmbQKnaiuTsDsOKPJMeh7YEx0Vzr94Bh589JH61N75 oM//4QjeDLQGV3uqIkjGYfVCK5KTh/CH3kvKVW9zKHtVukg4kmm36V+qe6CbrgpVMezf AlRfr8xeIpfkZveoQH5bg1rXOyq9Ffsjg9Ovu61k4mGmnhKaWmjfE1hMkYoDCcKQ/JSB irjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=B9xAMCdi; 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 u4si11416415ejy.135.2021.03.15.11.23.39; Mon, 15 Mar 2021 11:24:08 -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=B9xAMCdi; 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 S233789AbhCOOVe (ORCPT + 99 others); Mon, 15 Mar 2021 10:21:34 -0400 Received: from mail.kernel.org ([198.145.29.99]:38284 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232881AbhCOOAF (ORCPT ); Mon, 15 Mar 2021 10:00:05 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id EDCA364F56; Mon, 15 Mar 2021 13:59:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1615816787; bh=WdSA0u9QNjoXc1vg2BJB76lxbzJAX+Hz+pf+MzpjaqE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=B9xAMCdio+kZQTjLkjrogyL8zv6+5TVRlOg5mfjRxgxmc0KxP3mZDG5MAj1YSashL /I/9CRRWu8Avlmk+AQqJmZpQiSwFtd5CMH1dDdwXwzr7vQSyjbJ76VNLVu4iZ/Zww1 n7GAJAjAnNBzOEkRcawtUH5qTcQw5FS3+onWDxUM= From: gregkh@linuxfoundation.org To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Zqiang , Pete Zaitcev Subject: [PATCH 5.4 109/168] USB: usblp: fix a hang in poll() if disconnected Date: Mon, 15 Mar 2021 14:55:41 +0100 Message-Id: <20210315135553.940841112@linuxfoundation.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210315135550.333963635@linuxfoundation.org> References: <20210315135550.333963635@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; }