Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3355235pxf; Mon, 15 Mar 2021 07:51:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy6LSA9/dObIowEo2FuZGid0kOdEtMMr7Uyzyc08ufGTSC2NG8cPQF1Zw5LPumrnNkGb1kk X-Received: by 2002:a50:ef0a:: with SMTP id m10mr30350157eds.261.1615819888296; Mon, 15 Mar 2021 07:51:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1615819888; cv=none; d=google.com; s=arc-20160816; b=kcj+yk6N7W7G49kii/hEOhBvt54fMqj4E7qeU/gYNMisvUnkUb+tGsW9ekGYuPBAmM 1KgQg6VTnsK7mrlOQe/hzc3sVppPwV10IENDgf5NkErOyIPH+Gcs+Fm4UYPhzSouLDK7 Ql84GNl1OMImybwyPm2W9WcCtLf8fStFv2yhbJcVpDYJ/7SZP8Vvfd9TAtUObGAFqBQl hbQDcvSdc5gN3cNgxkhGKwiRl0d55gCxZf4Pwvx2+eaIQROs1f/3SOHbFLYTJCI3tI+S hC4ShupvnZnT9MPtA2H2EJm/8X72KaPKGo7TfUG0M06y+MMkKA6fAHsUhlcOjeBzind9 MjTw== 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=VZnmD8h4en8jdJ2ts/iSPhMHIPoIkaDkO63C/TlufkpwRgNRXL9Uy0HPExdaJXs8XE IvZNjaS2Dc8kuf6wXjT6bT11ik8dKMDpPPv0nPJNigtEtj72kolu7ZM+P4k9AdKr8K7i kN71caIG4kWs5ymGTe+MybJUqIVVRjIshRb0VpG3HLDiZjYRpqANLLKIzZGH6isngeJm kFKtWfvmjDJCsbBbt9fkBYHiwMlTMHJOyMfkH3pCwv5ZhkMGq7zEPSzZ1A/qPDXfrtxT MeSB8f5lZBQvtJ23V8SPKFRlgzdGX9EYEGUdIhk23lx+oQDgYaXp1oilm4JYtOJTcCr1 WoTw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Lo23kSgQ; 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 ds7si1911017ejc.214.2021.03.15.07.51.05; Mon, 15 Mar 2021 07:51:28 -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=Lo23kSgQ; 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 S240322AbhCOOrm (ORCPT + 99 others); Mon, 15 Mar 2021 10:47:42 -0400 Received: from mail.kernel.org ([198.145.29.99]:49098 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233813AbhCOOC3 (ORCPT ); Mon, 15 Mar 2021 10:02:29 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 4DBEF64EF1; Mon, 15 Mar 2021 14:02:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1615816949; bh=WdSA0u9QNjoXc1vg2BJB76lxbzJAX+Hz+pf+MzpjaqE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Lo23kSgQbvfcmll83Xgvg1UsVcYpu7MrFacaknUd2DPpx9ODLyjS7ta2KRTSVNqjD fqgScvjCZZIR69dlTG+X2/SpPQoEThEFkI7QZzchOoL8ByaZUTylMSkjeSMmQcvoT8 tjQ22TsSP1Ixb4n0OWKxWEkRPnLR87RLJ1C5ZLF0= From: gregkh@linuxfoundation.org To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Zqiang , Pete Zaitcev Subject: [PATCH 5.10 206/290] USB: usblp: fix a hang in poll() if disconnected Date: Mon, 15 Mar 2021 14:54:59 +0100 Message-Id: <20210315135548.887601251@linuxfoundation.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210315135541.921894249@linuxfoundation.org> References: <20210315135541.921894249@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; }