From patchwork Thu Sep 10 12:21:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 11767739 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id EC72C92C for ; Thu, 10 Sep 2020 12:24:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DBFFB208FE for ; Thu, 10 Sep 2020 12:24:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729911AbgIJMX6 (ORCPT ); Thu, 10 Sep 2020 08:23:58 -0400 Received: from mx2.suse.de ([195.135.220.15]:53628 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730507AbgIJMVc (ORCPT ); Thu, 10 Sep 2020 08:21:32 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 664D8B016; Thu, 10 Sep 2020 12:21:33 +0000 (UTC) From: Oliver Neukum To: penguin-kernel@i-love.sakura.ne.jp, bjorn@mork.no, linux-usb@vger.kernel.org Cc: Oliver Neukum Subject: [RFC 1/6] CDC-WDM: fix hangs in flush() Date: Thu, 10 Sep 2020 14:21:00 +0200 Message-Id: <20200910122105.13398-1-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org In a multithreaded scenario a flush() and a write() may be waiting for the same IO to complete. Hence completion of output must use wake_up_all(), even in error handling, while a flush may be waiting for an intended but not started Io. Reported-by: Tetsuo Handa Signed-off-by: Oliver Neukum --- drivers/usb/class/cdc-wdm.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/drivers/usb/class/cdc-wdm.c b/drivers/usb/class/cdc-wdm.c index e3db6fbeadef..adb3fc307083 100644 --- a/drivers/usb/class/cdc-wdm.c +++ b/drivers/usb/class/cdc-wdm.c @@ -151,7 +151,7 @@ static void wdm_out_callback(struct urb *urb) kfree(desc->outbuf); desc->outbuf = NULL; clear_bit(WDM_IN_USE, &desc->flags); - wake_up(&desc->wait); + wake_up_all(&desc->wait); } static void wdm_in_callback(struct urb *urb) @@ -424,6 +424,7 @@ static ssize_t wdm_write if (rv < 0) { desc->outbuf = NULL; clear_bit(WDM_IN_USE, &desc->flags); + wake_up_all(&desc->wait); /* for flush() */ dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv); rv = usb_translate_errors(rv); goto out_free_mem_pm; @@ -586,6 +587,7 @@ static ssize_t wdm_read static int wdm_flush(struct file *file, fl_owner_t id) { struct wdm_device *desc = file->private_data; + int rv; wait_event(desc->wait, /* @@ -600,11 +602,12 @@ static int wdm_flush(struct file *file, fl_owner_t id) /* cannot dereference desc->intf if WDM_DISCONNECTING */ if (test_bit(WDM_DISCONNECTING, &desc->flags)) return -ENODEV; - if (desc->werr < 0) + rv = desc->werr; + if (rv < 0) dev_err(&desc->intf->dev, "Error in flush path: %d\n", - desc->werr); + rv); - return usb_translate_errors(desc->werr); + return usb_translate_errors(rv); } static __poll_t wdm_poll(struct file *file, struct poll_table_struct *wait) From patchwork Thu Sep 10 12:21:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 11767743 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 48B82698 for ; Thu, 10 Sep 2020 12:25:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 31F1C214F1 for ; Thu, 10 Sep 2020 12:25:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730477AbgIJMYt (ORCPT ); Thu, 10 Sep 2020 08:24:49 -0400 Received: from mx2.suse.de ([195.135.220.15]:53638 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730508AbgIJMVb (ORCPT ); Thu, 10 Sep 2020 08:21:31 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 65F6EAFF6; Thu, 10 Sep 2020 12:21:33 +0000 (UTC) From: Oliver Neukum To: penguin-kernel@i-love.sakura.ne.jp, bjorn@mork.no, linux-usb@vger.kernel.org Cc: Oliver Neukum Subject: [RFC 2/6] CDC-WDM: introduce a timeout in flush() Date: Thu, 10 Sep 2020 14:21:01 +0200 Message-Id: <20200910122105.13398-2-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200910122105.13398-1-oneukum@suse.com> References: <20200910122105.13398-1-oneukum@suse.com> Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org Malicious or defective hardware may cease communication while flush() is running. In last consequence we need a timeout, as hardware that remains silent must be ignored. The 30 seconds are coming out of thin air. Reported-by: Tetsuo Handa Signed-off-by: Oliver Neukum --- drivers/usb/class/cdc-wdm.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/drivers/usb/class/cdc-wdm.c b/drivers/usb/class/cdc-wdm.c index adb3fc307083..fef011dc8dc4 100644 --- a/drivers/usb/class/cdc-wdm.c +++ b/drivers/usb/class/cdc-wdm.c @@ -58,6 +58,9 @@ MODULE_DEVICE_TABLE (usb, wdm_ids); #define WDM_MAX 16 +/* flush() is uninterruptible, but we cannot wait forever */ +#define WDM_FLUSH_TIMEOUT (30 * HZ) + /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */ #define WDM_DEFAULT_BUFSIZE 256 @@ -589,7 +592,7 @@ static int wdm_flush(struct file *file, fl_owner_t id) struct wdm_device *desc = file->private_data; int rv; - wait_event(desc->wait, + rv = wait_event_timeout(desc->wait, /* * needs both flags. We cannot do with one * because resetting it would cause a race @@ -597,11 +600,15 @@ static int wdm_flush(struct file *file, fl_owner_t id) * a disconnect */ !test_bit(WDM_IN_USE, &desc->flags) || - test_bit(WDM_DISCONNECTING, &desc->flags)); + test_bit(WDM_DISCONNECTING, &desc->flags), + WDM_FLUSH_TIMEOUT); /* cannot dereference desc->intf if WDM_DISCONNECTING */ if (test_bit(WDM_DISCONNECTING, &desc->flags)) return -ENODEV; + if (!rv) + return -EIO; + rv = desc->werr; if (rv < 0) dev_err(&desc->intf->dev, "Error in flush path: %d\n", @@ -662,6 +669,8 @@ static int wdm_open(struct inode *inode, struct file *file) /* using write lock to protect desc->count */ mutex_lock(&desc->wlock); if (!desc->count++) { + /* in case flush() had timed out */ + usb_kill_urb(desc->command); desc->werr = 0; desc->rerr = 0; rv = usb_submit_urb(desc->validity, GFP_KERNEL); From patchwork Thu Sep 10 12:21:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 11769451 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 98C4B59D for ; Thu, 10 Sep 2020 21:43:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 89FDF221E2 for ; Thu, 10 Sep 2020 21:43:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728415AbgIJVnI (ORCPT ); Thu, 10 Sep 2020 17:43:08 -0400 Received: from mx2.suse.de ([195.135.220.15]:54888 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730559AbgIJMYT (ORCPT ); Thu, 10 Sep 2020 08:24:19 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id AD532AE64; Thu, 10 Sep 2020 12:21:33 +0000 (UTC) From: Oliver Neukum To: penguin-kernel@i-love.sakura.ne.jp, bjorn@mork.no, linux-usb@vger.kernel.org Cc: Oliver Neukum Subject: [PATCH 3/6] CDC-WDM: making flush() interruptible Date: Thu, 10 Sep 2020 14:21:02 +0200 Message-Id: <20200910122105.13398-3-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200910122105.13398-1-oneukum@suse.com> References: <20200910122105.13398-1-oneukum@suse.com> Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org There is no need for flush() to be uninterruptible. close(2) is allowed to return -EAGAIN. Change it. Reported-by: Tetsuo Handa Signed-off-by: Oliver Neukum --- drivers/usb/class/cdc-wdm.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/usb/class/cdc-wdm.c b/drivers/usb/class/cdc-wdm.c index fef011dc8dc4..eee19532e67e 100644 --- a/drivers/usb/class/cdc-wdm.c +++ b/drivers/usb/class/cdc-wdm.c @@ -592,7 +592,7 @@ static int wdm_flush(struct file *file, fl_owner_t id) struct wdm_device *desc = file->private_data; int rv; - rv = wait_event_timeout(desc->wait, + rv = wait_event_interruptible_timeout(desc->wait, /* * needs both flags. We cannot do with one * because resetting it would cause a race @@ -608,7 +608,8 @@ static int wdm_flush(struct file *file, fl_owner_t id) return -ENODEV; if (!rv) return -EIO; - + if (rv < 0) + return -EINTR; rv = desc->werr; if (rv < 0) dev_err(&desc->intf->dev, "Error in flush path: %d\n", From patchwork Thu Sep 10 12:21:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 11767741 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 72C56698 for ; Thu, 10 Sep 2020 12:24:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 61552208FE for ; Thu, 10 Sep 2020 12:24:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730067AbgIJMY3 (ORCPT ); Thu, 10 Sep 2020 08:24:29 -0400 Received: from mx2.suse.de ([195.135.220.15]:53666 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730522AbgIJMVc (ORCPT ); Thu, 10 Sep 2020 08:21:32 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 00F3DB01F; Thu, 10 Sep 2020 12:21:34 +0000 (UTC) From: Oliver Neukum To: penguin-kernel@i-love.sakura.ne.jp, bjorn@mork.no, linux-usb@vger.kernel.org Cc: Oliver Neukum Subject: [RFC 4/6] CDC-WDM: fix race reporting errors in flush Date: Thu, 10 Sep 2020 14:21:03 +0200 Message-Id: <20200910122105.13398-4-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200910122105.13398-1-oneukum@suse.com> References: <20200910122105.13398-1-oneukum@suse.com> Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org In case a race was lost and multiple fds used, an error could be reported multiple times. To fix this a spinlock must be taken. Signed-off-by: Oliver Neukum --- drivers/usb/class/cdc-wdm.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/drivers/usb/class/cdc-wdm.c b/drivers/usb/class/cdc-wdm.c index eee19532e67e..930000b1d43b 100644 --- a/drivers/usb/class/cdc-wdm.c +++ b/drivers/usb/class/cdc-wdm.c @@ -610,10 +610,14 @@ static int wdm_flush(struct file *file, fl_owner_t id) return -EIO; if (rv < 0) return -EINTR; + + spin_lock_irq(&desc->iuspin); rv = desc->werr; + desc->werr = 0; + spin_unlock_irq(&desc->iuspin); + if (rv < 0) - dev_err(&desc->intf->dev, "Error in flush path: %d\n", - rv); + dev_err(&desc->intf->dev, "Error in flush path: %d\n", rv); return usb_translate_errors(rv); } From patchwork Thu Sep 10 12:21:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 11769455 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CF932746 for ; Thu, 10 Sep 2020 21:53:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C0712208E4 for ; Thu, 10 Sep 2020 21:53:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728711AbgIJVnC (ORCPT ); Thu, 10 Sep 2020 17:43:02 -0400 Received: from mx2.suse.de ([195.135.220.15]:54886 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730507AbgIJMYT (ORCPT ); Thu, 10 Sep 2020 08:24:19 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 47E85B020; Thu, 10 Sep 2020 12:21:34 +0000 (UTC) From: Oliver Neukum To: penguin-kernel@i-love.sakura.ne.jp, bjorn@mork.no, linux-usb@vger.kernel.org Cc: Oliver Neukum Subject: [RFC 5/6] CDC-WDM: remove use of intf->dev after potential disconnect Date: Thu, 10 Sep 2020 14:21:04 +0200 Message-Id: <20200910122105.13398-5-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200910122105.13398-1-oneukum@suse.com> References: <20200910122105.13398-1-oneukum@suse.com> Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org After a disconnect intf->dev is not a valid pointer any longer as flush() uses it only for logging purposes logging is not worth it. Remove the dev_err() Reported-by: Tetsuo Handa Signed-off-by: Oliver Neukum --- drivers/usb/class/cdc-wdm.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/drivers/usb/class/cdc-wdm.c b/drivers/usb/class/cdc-wdm.c index 930000b1d43b..89929f6438e3 100644 --- a/drivers/usb/class/cdc-wdm.c +++ b/drivers/usb/class/cdc-wdm.c @@ -603,7 +603,11 @@ static int wdm_flush(struct file *file, fl_owner_t id) test_bit(WDM_DISCONNECTING, &desc->flags), WDM_FLUSH_TIMEOUT); - /* cannot dereference desc->intf if WDM_DISCONNECTING */ + /* + * to report the correct error. + * This is best effort + * We are inevitably racing with the hardware. + */ if (test_bit(WDM_DISCONNECTING, &desc->flags)) return -ENODEV; if (!rv) @@ -616,9 +620,6 @@ static int wdm_flush(struct file *file, fl_owner_t id) desc->werr = 0; spin_unlock_irq(&desc->iuspin); - if (rv < 0) - dev_err(&desc->intf->dev, "Error in flush path: %d\n", rv); - return usb_translate_errors(rv); } From patchwork Thu Sep 10 12:21:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 11767747 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A8F9D698 for ; Thu, 10 Sep 2020 12:28:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9A6DF20BED for ; Thu, 10 Sep 2020 12:28:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730716AbgIJM1n (ORCPT ); Thu, 10 Sep 2020 08:27:43 -0400 Received: from mx2.suse.de ([195.135.220.15]:54890 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730618AbgIJMYT (ORCPT ); Thu, 10 Sep 2020 08:24:19 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 8BED3B023; Thu, 10 Sep 2020 12:21:34 +0000 (UTC) From: Oliver Neukum To: penguin-kernel@i-love.sakura.ne.jp, bjorn@mork.no, linux-usb@vger.kernel.org Cc: Oliver Neukum Subject: [RFC 6/6] CDC-WDM: correct error reporting in write() Date: Thu, 10 Sep 2020 14:21:05 +0200 Message-Id: <20200910122105.13398-6-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200910122105.13398-1-oneukum@suse.com> References: <20200910122105.13398-1-oneukum@suse.com> Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org In case our wait was interrupted by a disconnect, we should report that. Signed-off-by: Oliver Neukum --- drivers/usb/class/cdc-wdm.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/usb/class/cdc-wdm.c b/drivers/usb/class/cdc-wdm.c index 89929f6438e3..f952eec87b0f 100644 --- a/drivers/usb/class/cdc-wdm.c +++ b/drivers/usb/class/cdc-wdm.c @@ -396,6 +396,9 @@ static ssize_t wdm_write if (test_bit(WDM_RESETTING, &desc->flags)) r = -EIO; + if (test_bit(WDM_DISCONNECTING, &desc->flags)) + r = -ENODEV; + if (r < 0) { rv = r; goto out_free_mem_pm;