From patchwork Wed Sep 23 09:21:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 258447 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 56F59C4741F for ; Wed, 23 Sep 2020 09:22:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 11901206C3 for ; Wed, 23 Sep 2020 09:22:13 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="FBqil4qI" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726402AbgIWJWI (ORCPT ); Wed, 23 Sep 2020 05:22:08 -0400 Received: from mx2.suse.de ([195.135.220.15]:53646 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726524AbgIWJWG (ORCPT ); Wed, 23 Sep 2020 05:22:06 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1600852924; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:in-reply-to:in-reply-to:references:references; bh=2uWvnpc3bmPq2wl5EDlk5vxdx9IFG3yUbr1P4YdA22Y=; b=FBqil4qIOg+4F8DPQ7hm8QEU1Hc1EiZ7z8tjzfOG+Zm/heJnwiD5RvcX/spwnrzl91alH8 52/sQaj0ClMlLeDbiK0G3+C8Hq+Mk0xnd3aukuUu3eYMMeWk58fiNGUYUDW9U2RP7GNOWb f+lKqWpDMGxGR0Voy2ibxOB7PMeBWj8= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id A625FAB3E; Wed, 23 Sep 2020 09:22:41 +0000 (UTC) From: Oliver Neukum To: penguin-kernel@i-love.sakura.ne.jp, bjorn@mork.no, linux-usb@vger.kernel.org, gregKH@linuxfoundation.org Cc: Oliver Neukum Subject: [PATCH 1/7] CDC-WDM: fix hangs in flush() in multithreaded cases Date: Wed, 23 Sep 2020 11:21:30 +0200 Message-Id: <20200923092136.14824-2-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200923092136.14824-1-oneukum@suse.com> References: <20200923092136.14824-1-oneukum@suse.com> Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org In a multithreaded scenario an arbitrary number of threads can be in wdm_write() and in wdm_flush(). Hence whenever WDM_IN_USE is reset, all possible waiters need to be notified. This is true whether this is due to IO completing or to an error starting 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 Wed Sep 23 09:21:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 297537 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 90CA6C4741F for ; Wed, 23 Sep 2020 09:22:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 488B020719 for ; Wed, 23 Sep 2020 09:22:09 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="PT5nUMNm" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726584AbgIWJWI (ORCPT ); Wed, 23 Sep 2020 05:22:08 -0400 Received: from mx2.suse.de ([195.135.220.15]:53656 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726526AbgIWJWG (ORCPT ); Wed, 23 Sep 2020 05:22:06 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1600852924; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:in-reply-to:in-reply-to:references:references; bh=A1zUt81juSwPNJhRNl1BNHYKJV8/bvmy1vlgpsFmi6U=; b=PT5nUMNmW3byPn81UNWfcNmziuM2eZhCvC7e6jc1jUA/I2d8+Vms3GU071vganIUJAcyW3 Q6NpSU9FKD8kY5sBktWD1YSWvlYxDx5EhlVq5Rzh0ppv/ou8gYzR33JXrksTVHym1eC1VB erT4hHiG6NDKmH6DwfwTnJSl/nG0lFE= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id A8721AD2E; Wed, 23 Sep 2020 09:22:41 +0000 (UTC) From: Oliver Neukum To: penguin-kernel@i-love.sakura.ne.jp, bjorn@mork.no, linux-usb@vger.kernel.org, gregKH@linuxfoundation.org Cc: Oliver Neukum Subject: [PATCH 2/7] CDC-WDM: introduce a timeout in flush() Date: Wed, 23 Sep 2020 11:21:31 +0200 Message-Id: <20200923092136.14824-3-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200923092136.14824-1-oneukum@suse.com> References: <20200923092136.14824-1-oneukum@suse.com> 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. Making the wait for IO interruptible would not solve the issue. While it would avoid a hang, it would not allow any progress and we would end up with an unclosable fd. The 30 seconds are coming out of thin air. Reported-by: Tetsuo Handa Signed-off-by: Oliver Neukum --- drivers/usb/class/cdc-wdm.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/drivers/usb/class/cdc-wdm.c b/drivers/usb/class/cdc-wdm.c index adb3fc307083..b3a3f249a915 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", From patchwork Wed Sep 23 09:21:32 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 258448 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.9 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 47EA9C2D0A8 for ; Wed, 23 Sep 2020 09:22:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F0C2020719 for ; Wed, 23 Sep 2020 09:22:07 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="mUIO5NW/" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726550AbgIWJWF (ORCPT ); Wed, 23 Sep 2020 05:22:05 -0400 Received: from mx2.suse.de ([195.135.220.15]:53630 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726381AbgIWJWF (ORCPT ); Wed, 23 Sep 2020 05:22:05 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1600852924; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:in-reply-to:in-reply-to:references:references; bh=RxFQPXtnIY8PwL4wcaQVQPCXz6qQ+yIZN0Kpgnlyksg=; b=mUIO5NW/8IJArEX3K4UmRa4SvPA3x+0KulOs8+GWS1EydKnoTGCKkH6GZ4zfhLC3I3KwM+ QSOIK8IJClQrm5vvw6bw5dkFv5eDrY6Aichf8PXXdH01wjzJSpy6keuV34B6Oo+PGABHKI mir72r8FbQROMhjZ7PkHPFlQ5aBRi8I= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id A7AEBACD0; Wed, 23 Sep 2020 09:22:41 +0000 (UTC) From: Oliver Neukum To: penguin-kernel@i-love.sakura.ne.jp, bjorn@mork.no, linux-usb@vger.kernel.org, gregKH@linuxfoundation.org Cc: Oliver Neukum Subject: [PATCH 3/7] CDC-WDM: remove use of intf->dev after potential disconnect Date: Wed, 23 Sep 2020 11:21:32 +0200 Message-Id: <20200923092136.14824-4-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200923092136.14824-1-oneukum@suse.com> References: <20200923092136.14824-1-oneukum@suse.com> 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 b3a3f249a915..1ca2c85a977d 100644 --- a/drivers/usb/class/cdc-wdm.c +++ b/drivers/usb/class/cdc-wdm.c @@ -603,16 +603,17 @@ 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) return -EIO; rv = desc->werr; - if (rv < 0) - dev_err(&desc->intf->dev, "Error in flush path: %d\n", - rv); return usb_translate_errors(rv); } From patchwork Wed Sep 23 09:21:33 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 258446 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B7C62C4727D for ; Wed, 23 Sep 2020 09:22:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 72A222137B for ; Wed, 23 Sep 2020 09:22:13 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="rEg0lEUM" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726574AbgIWJWH (ORCPT ); Wed, 23 Sep 2020 05:22:07 -0400 Received: from mx2.suse.de ([195.135.220.15]:53664 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726532AbgIWJWG (ORCPT ); Wed, 23 Sep 2020 05:22:06 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1600852924; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:in-reply-to:in-reply-to:references:references; bh=3J4awFmP6XNuAxKP8nvMUO9NLGMgKpv8n8B5oWGFW6o=; b=rEg0lEUMryq9M1jSuNNui7d/OdKzolxk6oxAHVpFsbGeYOZ97ieC8yVbrxoq3KZPZRopFr kdcNfFIOYW5CG/m45UcIe9/X9f4mcm7f10qXWotvbyj+x7dmyCGT7TfwNtX+c1N3irawAW 2JkuorUPJs9o47/VYRILqkvmbbVUfAY= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id AAD38AD87; Wed, 23 Sep 2020 09:22:41 +0000 (UTC) From: Oliver Neukum To: penguin-kernel@i-love.sakura.ne.jp, bjorn@mork.no, linux-usb@vger.kernel.org, gregKH@linuxfoundation.org Cc: Oliver Neukum Subject: [PATCH 4/7] CDC-WDM: fix race reporting errors in flush Date: Wed, 23 Sep 2020 11:21:33 +0200 Message-Id: <20200923092136.14824-5-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200923092136.14824-1-oneukum@suse.com> References: <20200923092136.14824-1-oneukum@suse.com> 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 | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/usb/class/cdc-wdm.c b/drivers/usb/class/cdc-wdm.c index 1ca2c85a977d..5fb855404403 100644 --- a/drivers/usb/class/cdc-wdm.c +++ b/drivers/usb/class/cdc-wdm.c @@ -613,7 +613,10 @@ static int wdm_flush(struct file *file, fl_owner_t id) if (!rv) return -EIO; + spin_lock_irq(&desc->iuspin); rv = desc->werr; + desc->werr = 0; + spin_unlock_irq(&desc->iuspin); return usb_translate_errors(rv); } From patchwork Wed Sep 23 09:21:34 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 297535 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A0173C47426 for ; Wed, 23 Sep 2020 09:22:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5C73E20719 for ; Wed, 23 Sep 2020 09:22:15 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="im8NhybU" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726623AbgIWJWO (ORCPT ); Wed, 23 Sep 2020 05:22:14 -0400 Received: from mx2.suse.de ([195.135.220.15]:53724 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726557AbgIWJWG (ORCPT ); Wed, 23 Sep 2020 05:22:06 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1600852924; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:in-reply-to:in-reply-to:references:references; bh=enkvzSACilXPdBEKnE+T7BwCxIvLK9Ib/5smmf0iTFA=; b=im8NhybU1/hc9MFbaytwoEAFq3UESxoflFBOKCHn698ksNGcqBNWDxEGEk6ir0O2nxrgnl cWbtGWIBQquGK0W4v6tMkGcBRgAMMv5elnZcam8sTHeNeGNZVTbFPMHOySDOTKjjQS0+DH 5zlCmX3Yuf9hojEP6F/S/JD20YqxguM= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id D5DD4AE14; Wed, 23 Sep 2020 09:22:41 +0000 (UTC) From: Oliver Neukum To: penguin-kernel@i-love.sakura.ne.jp, bjorn@mork.no, linux-usb@vger.kernel.org, gregKH@linuxfoundation.org Cc: Oliver Neukum Subject: [PATCH 5/7] CDC-WDM: correct error reporting in write() Date: Wed, 23 Sep 2020 11:21:34 +0200 Message-Id: <20200923092136.14824-6-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200923092136.14824-1-oneukum@suse.com> References: <20200923092136.14824-1-oneukum@suse.com> 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 5fb855404403..242f83118208 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; From patchwork Wed Sep 23 09:21:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 297536 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7E214C47420 for ; Wed, 23 Sep 2020 09:22:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3F82E21D91 for ; Wed, 23 Sep 2020 09:22:14 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="EvLS/nBc" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726620AbgIWJWN (ORCPT ); Wed, 23 Sep 2020 05:22:13 -0400 Received: from mx2.suse.de ([195.135.220.15]:53720 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726558AbgIWJWG (ORCPT ); Wed, 23 Sep 2020 05:22:06 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1600852924; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:in-reply-to:in-reply-to:references:references; bh=IRlIWc5ddJjwc1HhS2ARyazLOr5MJTHSYzCeSf9kUQU=; b=EvLS/nBcCVjQwt46aUaAU1KYbNpvlElVk0xoJG3eg+kWgg6aQ3jupMWU2UD+R44mv/A800 tsNGqa7do7ZcSD4idayUK1L0cW0OGiD6sa/mci10BpqpVxksSqYF3I/3SAp4DSllif22X/ 1gPQQsDCSEwyfL1G1slXg0epBIBKs6g= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id D767BAE16; Wed, 23 Sep 2020 09:22:41 +0000 (UTC) From: Oliver Neukum To: penguin-kernel@i-love.sakura.ne.jp, bjorn@mork.no, linux-usb@vger.kernel.org, gregKH@linuxfoundation.org Cc: Oliver Neukum Subject: [PATCH 6/7] CDC-WDM: implement fsync Date: Wed, 23 Sep 2020 11:21:35 +0200 Message-Id: <20200923092136.14824-7-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200923092136.14824-1-oneukum@suse.com> References: <20200923092136.14824-1-oneukum@suse.com> Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org Some users want to be very sure that data has gone out to the device. This needs fsync. Reported-by: Tetsuo Handa Signed-off-by: Oliver Neukum --- drivers/usb/class/cdc-wdm.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/drivers/usb/class/cdc-wdm.c b/drivers/usb/class/cdc-wdm.c index 242f83118208..6ea03c12380c 100644 --- a/drivers/usb/class/cdc-wdm.c +++ b/drivers/usb/class/cdc-wdm.c @@ -590,6 +590,33 @@ static ssize_t wdm_read return rv; } +/* + * The difference to flush is that we wait forever. If you don't like + * that behavior, you need to send a signal. + */ + +static int wdm_fsync(struct file *file, loff_t start, loff_t end, int datasync) +{ + struct wdm_device *desc = file->private_data; + int rv; + + rv = wait_event_interruptible(desc->wait, + !test_bit(WDM_IN_USE, &desc->flags) || + test_bit(WDM_DISCONNECTING, &desc->flags)); + + if (test_bit(WDM_DISCONNECTING, &desc->flags)) + return -ENODEV; + if (rv < 0) + return -EINTR; + + spin_lock_irq(&desc->iuspin); + rv = desc->werr; + desc->werr = 0; + spin_unlock_irq(&desc->iuspin); + + return usb_translate_errors(rv); +} + static int wdm_flush(struct file *file, fl_owner_t id) { struct wdm_device *desc = file->private_data; @@ -746,6 +773,7 @@ static const struct file_operations wdm_fops = { .owner = THIS_MODULE, .read = wdm_read, .write = wdm_write, + .fsync = wdm_fsync, .open = wdm_open, .flush = wdm_flush, .release = wdm_release, From patchwork Wed Sep 23 09:21:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 258445 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 593AFC4741F for ; Wed, 23 Sep 2020 09:22:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2619121D91 for ; Wed, 23 Sep 2020 09:22:16 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="TzNMVOx7" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726566AbgIWJWO (ORCPT ); Wed, 23 Sep 2020 05:22:14 -0400 Received: from mx2.suse.de ([195.135.220.15]:53722 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726381AbgIWJWG (ORCPT ); Wed, 23 Sep 2020 05:22:06 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1600852924; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:in-reply-to:in-reply-to:references:references; bh=QHxctgCuNqwgEnaUO19yG0Cl1EVwRzPGYwo+m2GJyFc=; b=TzNMVOx7xnHIZpJorV1B5XTPIBTUkB3CCcfWIDLNrXYxHajC3PnEKOquVj/9oJWP8nCvfJ N5P23dC3pnv/MU3hEjEL89Q9Or/WNtDNrgkMwhLM2z0RHxh3wWIl0cvGWWj07XV/yqYv6s nb4royaUgqIFsn8uCnS/IcPJn025/vE= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 17DFCAE8C; Wed, 23 Sep 2020 09:22:42 +0000 (UTC) From: Oliver Neukum To: penguin-kernel@i-love.sakura.ne.jp, bjorn@mork.no, linux-usb@vger.kernel.org, gregKH@linuxfoundation.org Cc: Oliver Neukum Subject: [PATCH 7/7] CDC-WDM: making flush() interruptible Date: Wed, 23 Sep 2020 11:21:36 +0200 Message-Id: <20200923092136.14824-8-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200923092136.14824-1-oneukum@suse.com> References: <20200923092136.14824-1-oneukum@suse.com> 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 -EINTR. 30 seconds is quite long a time to sleep in an uninterruptible state. Change it to an interruptible sleep. Reported-by: Tetsuo Handa Signed-off-by: Oliver Neukum --- drivers/usb/class/cdc-wdm.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/usb/class/cdc-wdm.c b/drivers/usb/class/cdc-wdm.c index 6ea03c12380c..b9cca1cb5058 100644 --- a/drivers/usb/class/cdc-wdm.c +++ b/drivers/usb/class/cdc-wdm.c @@ -622,7 +622,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 @@ -642,6 +642,8 @@ static int wdm_flush(struct file *file, fl_owner_t id) return -ENODEV; if (!rv) return -EIO; + if (rv < 0) + return -EINTR; spin_lock_irq(&desc->iuspin); rv = desc->werr;