Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3526403pxf; Mon, 15 Mar 2021 11:31:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxU/k602WGNyFqOjqwQhDgsiRGIFguIu5PqCbIfgi8LB9IQ7MR7wzn4VXKsKpTTwk6RH0JK X-Received: by 2002:a17:907:216d:: with SMTP id rl13mr24841586ejb.362.1615833076498; Mon, 15 Mar 2021 11:31:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1615833076; cv=none; d=google.com; s=arc-20160816; b=ER2Q1uqhcIJjBGou/fuNiPD+kXMvDWBWYq9mre9BWsApm8OARR1Vb61f2bMBpUPSaj 7FZuIIqYr913e0vUFDM1gRjm1zCJtC2Yoy0TxYYl+QWwMYjyqH0qcZ5pHDGyOHwPgc7l lZG55RWJaU1B7dYQxs/j2V+4PRqvdBLE8WfS9IjvtKMISth2c+G1ZGUyWvILhdoG4I54 HGCII3Utru5CtXyCwLOHq4o9TXXjcvOiXmX9DATjOoq8+RUhCjPeAWpsiQA0WJNMDS+J 01Idc/aJ9040xRvhERlFyOqnhaMfZnCU48bdaBNZQ9fkW0pO6T0MPDg8Anqd2Ss6Jp2o 5VKQ== 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=EgA1ZY7OUCEqNKYcmfyz1Ssj97GfV6wwdC3mjGB4EUfXze21VuwKjuxOJ8sZ+ss83R cdUOxzpZTNZdz9TCZeYEmXzRQeV/qU3CzKNypGlyV83vx4d7b9oo64sZC0WkUZ+rdaSG RIRK7f4/S3Z7Qat5cycAXp4e/I29pRxX7t4acE0JfQJw71/k9eUowdQGhLGOaM6l4Hfd +WLv3ao2B864mhZnt8PQLvRlGNNNH9z0TJu7J3tcOi+bofdYWiPdN5rw989pPf5e1PHy dda3JjORr/e7ZDsN9NrwMEHdVWSeUpYJDK0/MOFeeebE2VntIcylWCA6ks2B8Lq5Br6L pIDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=N9+JW4SB; 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 lg14si11562843ejb.6.2021.03.15.11.30.54; Mon, 15 Mar 2021 11:31:16 -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=N9+JW4SB; 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 S240779AbhCOOiY (ORCPT + 99 others); Mon, 15 Mar 2021 10:38:24 -0400 Received: from mail.kernel.org ([198.145.29.99]:48766 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233556AbhCOOCB (ORCPT ); Mon, 15 Mar 2021 10:02:01 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id DB22264F17; Mon, 15 Mar 2021 14:01:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1615816920; bh=WdSA0u9QNjoXc1vg2BJB76lxbzJAX+Hz+pf+MzpjaqE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=N9+JW4SBBga7vVBIgfpHontEjougyBlqhMYk+OZujXNh1ngVAqiGcPbH6rtPnkNBZ zmUGOhbiPoYDEN5cjHaxjfOXpn5nh0mHo7flR5NtRx5llcLJAv3DMxs7a5aRHzJFnH pxGL6Eqw3svzxYB+57xstxlfudzaFImDW8EK87aM= From: gregkh@linuxfoundation.org To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Zqiang , Pete Zaitcev Subject: [PATCH 5.11 202/306] USB: usblp: fix a hang in poll() if disconnected Date: Mon, 15 Mar 2021 14:54:25 +0100 Message-Id: <20210315135514.466896343@linuxfoundation.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210315135507.611436477@linuxfoundation.org> References: <20210315135507.611436477@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; }