From patchwork Tue Sep 22 11:21:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 258452 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 15E98C4727D for ; Tue, 22 Sep 2020 11:21:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AF5622388B for ; Tue, 22 Sep 2020 11:21:58 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="XGqEUDXY" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726578AbgIVLV6 (ORCPT ); Tue, 22 Sep 2020 07:21:58 -0400 Received: from mx2.suse.de ([195.135.220.15]:37322 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726454AbgIVLVw (ORCPT ); Tue, 22 Sep 2020 07:21:52 -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=1600773710; 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=XGqEUDXYCWlfarTkTCSiNrs+9Ira5kwfsfmAL1RSXehIzZ1hs7bS/MyBmc8viji7BSGLOo y9rboFvu37+Ldy5L4bErPsD7U2QlTcXOqfvwykk0iVPdBRcVD9uelGxSdMp8I5b9qH7Ds5 GXpGIxNQpSEXRHkNGe6gZGEStoslGG4= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id C08BFAD63; Tue, 22 Sep 2020 11:22:26 +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/7] CDC-WDM: fix hangs in flush() in multithreaded cases Date: Tue, 22 Sep 2020 13:21:20 +0200 Message-Id: <20200922112126.16919-2-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200922112126.16919-1-oneukum@suse.com> References: <20200922112126.16919-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 Tue Sep 22 11:21:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 297544 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 D5890C4727D for ; Tue, 22 Sep 2020 11:21:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 804352388B for ; Tue, 22 Sep 2020 11:21:53 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="s1oMw01G" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726566AbgIVLVw (ORCPT ); Tue, 22 Sep 2020 07:21:52 -0400 Received: from mx2.suse.de ([195.135.220.15]:37332 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726506AbgIVLVv (ORCPT ); Tue, 22 Sep 2020 07:21:51 -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=1600773710; 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=s1oMw01GjiHG2xmptKnt9Q2lWKveDNE6c84bpQBhddT4Oq+aksN74m3nX9PllxLN+LPNup GNWnpTQHB85uWphy5+S+NCwYNkcpqLim4m05pysNT2wYdrXUf8R4wlO697lihuAy/lszxf rgPze7vEtvEGb//i677PqyBkLvs3dnE= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id D3207AE08; Tue, 22 Sep 2020 11:22:26 +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/7] CDC-WDM: introduce a timeout in flush() Date: Tue, 22 Sep 2020 13:21:21 +0200 Message-Id: <20200922112126.16919-3-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200922112126.16919-1-oneukum@suse.com> References: <20200922112126.16919-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 Tue Sep 22 11:21:22 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 297545 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 5B1A6C2D0E2 for ; Tue, 22 Sep 2020 11:21:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E9BBD221EB for ; Tue, 22 Sep 2020 11:21:52 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="SuTA4PC6" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726552AbgIVLVv (ORCPT ); Tue, 22 Sep 2020 07:21:51 -0400 Received: from mx2.suse.de ([195.135.220.15]:37362 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726509AbgIVLVv (ORCPT ); Tue, 22 Sep 2020 07:21:51 -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=1600773710; 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=SuTA4PC6TgptY9EvynRvxehxs4KkMUnn0L9nfdGhubbpTzUWZ8gIqTpuPAVBi6AWythJbl Rz7+sDZaXEoyDXD+dB7Lh+wIg9FeWtsivXFfIDGJ/1QX1dWNQF4AhvMZR52wJjVhAroh/g ijLyWaRAqTmvqXl3ZWXslInTE2Uus1U= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 22CD7AE0D; Tue, 22 Sep 2020 11:22:27 +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 3/7] CDC-WDM: remove use of intf->dev after potential disconnect Date: Tue, 22 Sep 2020 13:21:22 +0200 Message-Id: <20200922112126.16919-4-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200922112126.16919-1-oneukum@suse.com> References: <20200922112126.16919-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 Tue Sep 22 11:21:23 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 297542 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 B740AC2D0E2 for ; Tue, 22 Sep 2020 11:21:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 65BEF23A1B for ; Tue, 22 Sep 2020 11:21:57 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="CNKQQq1x" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726554AbgIVLVw (ORCPT ); Tue, 22 Sep 2020 07:21:52 -0400 Received: from mx2.suse.de ([195.135.220.15]:37386 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726513AbgIVLVv (ORCPT ); Tue, 22 Sep 2020 07:21:51 -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=1600773710; 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=CNKQQq1xqYemNXHfqeYqgMdBtlv0veoUgumCXuQaxsuJlQPccmRyEvHT4MZ+p/qd+ZnE6r apkcNBYbQWcWolCDLyacbX3Io8PtWwKPiyye94O1GLpUUk3Nw8bUXHEIVLLH17aiYxxR7h kvzsTxtI3Wfcg6D1kj/XqYZDOR9Uw5w= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 68165AE19; Tue, 22 Sep 2020 11:22:27 +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/7] CDC-WDM: fix race reporting errors in flush Date: Tue, 22 Sep 2020 13:21:23 +0200 Message-Id: <20200922112126.16919-5-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200922112126.16919-1-oneukum@suse.com> References: <20200922112126.16919-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 Tue Sep 22 11:21:24 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 258454 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 32A63C4741F for ; Tue, 22 Sep 2020 11:21:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D0E3A2388B for ; Tue, 22 Sep 2020 11:21:54 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="T9B+ooHd" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726573AbgIVLVx (ORCPT ); Tue, 22 Sep 2020 07:21:53 -0400 Received: from mx2.suse.de ([195.135.220.15]:37422 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726559AbgIVLVw (ORCPT ); Tue, 22 Sep 2020 07:21:52 -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=1600773711; 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=T9B+ooHdqOf/Dns7WNYf+Lzag2Vui9Gj26jUkYCZvUEjnt8/xrKcYqjGVPJ3bBTIby6qZc tip5xlJ4EGJhC/CnyTx/HRwEWD6KyHzf4q/iY9c1P5/1895GQEVga2REvRmq7GoHzIQDuz E1GkUNjSJxEq93ymNRyGFrInssfpE40= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id B228EAE35; Tue, 22 Sep 2020 11:22:27 +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/7] CDC-WDM: correct error reporting in write() Date: Tue, 22 Sep 2020 13:21:24 +0200 Message-Id: <20200922112126.16919-6-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200922112126.16919-1-oneukum@suse.com> References: <20200922112126.16919-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 Tue Sep 22 11:21:25 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 297543 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 A7A1EC2D0E2 for ; Tue, 22 Sep 2020 11:21:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 490F12388B for ; Tue, 22 Sep 2020 11:21:55 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="QACjyhJ5" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726576AbgIVLVx (ORCPT ); Tue, 22 Sep 2020 07:21:53 -0400 Received: from mx2.suse.de ([195.135.220.15]:37424 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726513AbgIVLVw (ORCPT ); Tue, 22 Sep 2020 07:21:52 -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=1600773711; 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=QACjyhJ5a4GGe59gSsK/rAlcIruVC+XUvmWAJxPMgvCmmelTxvFKsRwkQXOLxOD7kLR4gy AlmHOS7pm9UZN/pvJqK0sn6PEonT7i/zCjsyIq1VfS8WokkbHpqOrgjE52JWmhEOkRLUYH 2g3px9ZOSkIWnoJAPdO7hVMgW6hnqTY= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 092DBAE53; Tue, 22 Sep 2020 11:22:28 +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/7] CDC-WDM: implement fsync Date: Tue, 22 Sep 2020 13:21:25 +0200 Message-Id: <20200922112126.16919-7-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200922112126.16919-1-oneukum@suse.com> References: <20200922112126.16919-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 Tue Sep 22 11:21:26 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 258453 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 4B307C4727F for ; Tue, 22 Sep 2020 11:21:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E4A07239D2 for ; Tue, 22 Sep 2020 11:21:55 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="vEs3uoE4" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726577AbgIVLVz (ORCPT ); Tue, 22 Sep 2020 07:21:55 -0400 Received: from mx2.suse.de ([195.135.220.15]:37426 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726571AbgIVLVx (ORCPT ); Tue, 22 Sep 2020 07:21:53 -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=1600773711; 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=vEs3uoE45JluZQ9XBcgv8ZB8cpZqGePbvL1fBibNE9Eqjp4G+0Clr5H+zzhXfTgh02uFI2 bekIBcBSfNJJZ3P58fAVOCYFLkb9m3xTdmfVfDlWjDSWP0v0VZYB0dA4RwseeJNrSYknCH AnY3FuPo3Mq5oEYSEbRjVIgIaOVTvGc= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 454C4AE56; Tue, 22 Sep 2020 11:22:28 +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 7/7] CDC-WDM: making flush() interruptible Date: Tue, 22 Sep 2020 13:21:26 +0200 Message-Id: <20200922112126.16919-8-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200922112126.16919-1-oneukum@suse.com> References: <20200922112126.16919-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;