From patchwork Thu Sep 17 10:06:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 297615 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.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED, 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 89E00C433E2 for ; Thu, 17 Sep 2020 10:06:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 37374208DB for ; Thu, 17 Sep 2020 10:06:39 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="key not found in DNS" (0-bit key) header.d=suse.com header.i=@suse.com header.b="O4EE4Sp5" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726576AbgIQKGf (ORCPT ); Thu, 17 Sep 2020 06:06:35 -0400 Received: from mx2.suse.de ([195.135.220.15]:37614 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726505AbgIQKGa (ORCPT ); Thu, 17 Sep 2020 06:06:30 -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=cantorsusede; t=1600337188; 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=TkAAh4qN2sx9w0z1qXmsZmHjmzJsXUMLdDAvu1G9/ro=; b=O4EE4Sp5OaGlw8Dri/MZ3leyXobledDVkLffWe77fS4ckU/XtbzopOWaqKoJSTXVLGSd3R mo0vTauWyMrJQuttKAHbmrgoYMRjWXAe3UCllRdqvavpV2EXzuQmVCYs0Uy/McaTxV+vC5 2WWTLH/vhxsh1SorJjmmRO8Mg6DeD1426/9vgExQ9dNdl+e4khPXxNbyZLlnWyFoCMqKRH ZJEvFV8WPZShM0YACnzY16uClYckW7Gy6sbZa2EHwyWhmipNBU+N6xdKZr1A/Ij4xr9rRd uDUKqxLo7C9e1du/OwJDhTQ7MNqmA/p78cnAqtA9SiZowfRehlRrgJtPZFW3zQ== Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 9A59CAD5C; Thu, 17 Sep 2020 10:07:02 +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() Date: Thu, 17 Sep 2020 12:06:08 +0200 Message-Id: <20200917100614.1010-2-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200917100614.1010-1-oneukum@suse.com> References: <20200917100614.1010-1-oneukum@suse.com> 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 17 10:06:09 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 258525 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.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED, 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 0D8ECC433E2 for ; Thu, 17 Sep 2020 10:06:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BA27E2083B for ; Thu, 17 Sep 2020 10:06:43 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="key not found in DNS" (0-bit key) header.d=suse.com header.i=@suse.com header.b="PIaZ+rXS" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726410AbgIQKGj (ORCPT ); Thu, 17 Sep 2020 06:06:39 -0400 Received: from mx2.suse.de ([195.135.220.15]:37634 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726200AbgIQKGb (ORCPT ); Thu, 17 Sep 2020 06:06:31 -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=cantorsusede; t=1600337189; 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=qJjJjJ8liZ8RUpbm0zpKhZs3JIhUIbUCld6FGwXTjKs=; b=PIaZ+rXSorAdAh162z73PZQvKAGnxBZ4OGHujbrK0kDJJsrBfLvTwI47jpjWYl3NbfW0Ea 4qpjnrZnIt/ofnvfUq/oUjl7wOPT2SBNpf+lP+kOZqIDSh5RtdoFPOAG5qn4BZfYXthfAc GTnVD7NINvT1SgxDXPUmXTK40Ue7z9G7qrYlX+vkW3rGyoLAAAk11fEmaf7EK3gayNPm+C oC9o0GSvF8uAKwa1DoFG7jBPMD+9r4lSnTotTPHeFtmgR7PtkhR1FsDtM8iHrfDMkRW8qa vqJ/4zPp5NLPnzda8YkKbKibx/bopt3QN+j+k3tAD+wLPdBqYhrqHmgUbTJKpg== Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id EC395AD73; Thu, 17 Sep 2020 10:07:02 +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: Thu, 17 Sep 2020 12:06:09 +0200 Message-Id: <20200917100614.1010-3-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200917100614.1010-1-oneukum@suse.com> References: <20200917100614.1010-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. 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 17 10:06:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 297614 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.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED, 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 A8815C43461 for ; Thu, 17 Sep 2020 10:06:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5AD902083B for ; Thu, 17 Sep 2020 10:06:46 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="key not found in DNS" (0-bit key) header.d=suse.com header.i=@suse.com header.b="FAMQDtsv" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726582AbgIQKGn (ORCPT ); Thu, 17 Sep 2020 06:06:43 -0400 Received: from mx2.suse.de ([195.135.220.15]:37652 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726520AbgIQKGc (ORCPT ); Thu, 17 Sep 2020 06:06:32 -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=cantorsusede; t=1600337189; 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=s4M7sHgMh7BrpXP+6qi8QCUAy+sGdEkc6ndrA3Yc+Qo=; b=FAMQDtsvZiPSjpvilGyfqCCAZjxQd7hEAvqYZzeasO5W9ww5j9+pfKN5Fji40Q1Sm6vGqa eObNHoAbKemUvx7AHXqUIqs4HG4yR2CBGU+PP73XATuBCwYKmsfRXYuHopCBJ0wIFQMUAD R14uhw9960ZcF9w8WYwR1M2aeQg1RDe5amKUIC7qvYZWV3gt8nuZd5p/PU3JkuZaPrj+zf u0ptGPajJaPqFvz9kHhE5q7/7FM1ShGf9uRLnJOghw99DONCpdGiDbhAeqaUZGZh564u3h gLPYXzxrrtR1d53CBTo/6VluRKzl4kSmTojbXpNPde5xaRgzFf+Ehpr0siWlTA== Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 3D85FAD64; Thu, 17 Sep 2020 10:07:03 +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: making flush() interruptible Date: Thu, 17 Sep 2020 12:06:10 +0200 Message-Id: <20200917100614.1010-4-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200917100614.1010-1-oneukum@suse.com> References: <20200917100614.1010-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 -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 17 10:06:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 258524 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.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED, 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 70D75C433E2 for ; Thu, 17 Sep 2020 10:06:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 312AC2083B for ; Thu, 17 Sep 2020 10:06:47 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="key not found in DNS" (0-bit key) header.d=suse.com header.i=@suse.com header.b="hsf4IaSZ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726584AbgIQKGq (ORCPT ); Thu, 17 Sep 2020 06:06:46 -0400 Received: from mx2.suse.de ([195.135.220.15]:37668 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726524AbgIQKGd (ORCPT ); Thu, 17 Sep 2020 06:06:33 -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=cantorsusede; t=1600337189; 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=02ogVbl2/wFuqInSwBJuVrTNQykLfclOjm8SmDAqKiE=; b=hsf4IaSZ93RRF1iBPCIkPPynJH7AdJRUYWf9ObLABI2Yd57PB2+UMVArMSfARY/R0gClyA t63hfAUMRTpZgaIIw69M/6BTsoYZ3MbjPIhyJaPLcMCZJFl+IvjOTPV9NdMujDcSyB+Utx v5MxeuumhvzxgWmSk3VjkU78jSx4zSkG+tb5MAv0TCpDFzHxTUMWgtnxNuvgc3+3Jjc4oO 1i0dhLvyMUrw/pUp7yF9lLfDDgJo5NDLYA3V6wfwnJXm5a8pCE7DCAgQH90dIAzEUCtvU+ P+7mk8xrcwlVpmygbbkosbiAAC7rQdJWB10br6BLiTclahOTPfCWrC6/miVq6Q== Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 854ADADA2; Thu, 17 Sep 2020 10:07:03 +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: Thu, 17 Sep 2020 12:06:11 +0200 Message-Id: <20200917100614.1010-5-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200917100614.1010-1-oneukum@suse.com> References: <20200917100614.1010-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 | 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 17 10:06:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 297613 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.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED, 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 839E5C43461 for ; Thu, 17 Sep 2020 10:06:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 45BFF2083B for ; Thu, 17 Sep 2020 10:06:48 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="key not found in DNS" (0-bit key) header.d=suse.com header.i=@suse.com header.b="jTKjfjaR" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726423AbgIQKGr (ORCPT ); Thu, 17 Sep 2020 06:06:47 -0400 Received: from mx2.suse.de ([195.135.220.15]:37706 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726554AbgIQKGe (ORCPT ); Thu, 17 Sep 2020 06:06:34 -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=cantorsusede; t=1600337190; 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=Xy983x5GYa/afyX2dJHq8mBNPl8bQlZM2OJUHuYc0WQ=; b=jTKjfjaRCHiwqyceYDUWgz0/aG5mC6sEK6thyqp9V7vciCK5SVPalulPnoixbeoB85JFei a5JQWVkyMrvEQz/C5ZEZK2dyqYxCnnHWpEWKzH9iYQRYzNtNBrzocGmdNWkvKr17Md68e+ Q50jX9buS1o4wqECNkmh9Glk0OBnp7LaNZcZZrSSUKX0aEfD3sPU7lMOoR5iExE5iPU8Ya 58sMbVCNAauZUXYYAbX0rj2IglpXzqB80Zy/ZoqeHXLN2JWeioTMjTZrdzm2z0Z2SoxD2r gpbU5k8qfA2FFou20vfn+Ncl6FMsi3BaMoZ6eGovxL37IIFnb7/bdf+kTIkBPQ== Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id C37E9ADA8; Thu, 17 Sep 2020 10:07:03 +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: remove use of intf->dev after potential disconnect Date: Thu, 17 Sep 2020 12:06:12 +0200 Message-Id: <20200917100614.1010-6-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200917100614.1010-1-oneukum@suse.com> References: <20200917100614.1010-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 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 17 10:06:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 297612 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.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED, 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 B37E4C43461 for ; Thu, 17 Sep 2020 10:06:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7D2152083B for ; Thu, 17 Sep 2020 10:06:50 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="key not found in DNS" (0-bit key) header.d=suse.com header.i=@suse.com header.b="QZwx9DTb" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726559AbgIQKGt (ORCPT ); Thu, 17 Sep 2020 06:06:49 -0400 Received: from mx2.suse.de ([195.135.220.15]:37704 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726545AbgIQKGe (ORCPT ); Thu, 17 Sep 2020 06:06:34 -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=cantorsusede; t=1600337190; 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=8d1aXqgHDJwOdjA+ia7wq/6Fdl+kzdMh7SX4pt8jxVs=; b=QZwx9DTbcTD3soVk1bAslHuJ05NdTbBVEOeFQ6vXWL0tk4a3V+yle7ZEj5+DAidj0b5XnT i1cRUVH17fnedXTFV7SC4LkekCODlakLPCF0NYFRzdwehlLhGTMClwS5OteIFZgvdA4fE6 HiLfVA5VhrBT2vvWdPWoj80TqzWnQN0h5qgW2lAn0puNo8k+UCDui0Vh1Uc4UpqZZuJr3C vlVYFrKVm0Ft9CbFVMxuVvoMSsAVWpp7e8w//LfOp/0ouauoFlcwAYMqDqPdXdwSmRBvFA sFvELfsujG1C/AG0WyWePZl3MvVoyv8LFFtgMyrTDL1gRdwWKpDFyKmaQHANVQ== Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 15CF3ADC2; Thu, 17 Sep 2020 10:07:04 +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: correct error reporting in write() Date: Thu, 17 Sep 2020 12:06:13 +0200 Message-Id: <20200917100614.1010-7-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200917100614.1010-1-oneukum@suse.com> References: <20200917100614.1010-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 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; From patchwork Thu Sep 17 10:06:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 258523 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.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED, 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 28007C433E2 for ; Thu, 17 Sep 2020 10:06:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E1AC52087D for ; Thu, 17 Sep 2020 10:06:49 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="key not found in DNS" (0-bit key) header.d=suse.com header.i=@suse.com header.b="TFlhU1zZ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726554AbgIQKGs (ORCPT ); Thu, 17 Sep 2020 06:06:48 -0400 Received: from mx2.suse.de ([195.135.220.15]:37708 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726559AbgIQKGf (ORCPT ); Thu, 17 Sep 2020 06:06:35 -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=cantorsusede; t=1600337190; 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=lAPuK4woBdNl3RoIv0cwrcWM2DcttdWFgRNJGM6HIXI=; b=TFlhU1zZGfB0hxAudWANKwucjCh54sFyz1e6vLmX5pQfJ8yEXmi6FrGTBBRk/0Dz+0DKWa T4yWUiWYTdFY0yShF7eg6bP1a/OeqVkTJDqmIYwpwGlNXIW3sLzbfLQSrJD7hNhh+FaUZ0 +M83YT4B33W8nCyBjV1dLkJJ/KLmivp4Wpcszpq6MUVjPV7D0XzFric5TBp0CaDVVhKEhM SvR5Ol8h5xtx1Z6sOuumQz4eQa4rX7J7lkZRgzIhA8yPyPjYtdXPhF8Suk2M7Zzijo+6sf QlP3oPQpTejpyl4zd9tPJr/NXEbTzTtQ0yqjjmKeeXIqGkKLGzB33efn3WQWyw== Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 625AEADEE; Thu, 17 Sep 2020 10:07:04 +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: implement fsync Date: Thu, 17 Sep 2020 12:06:14 +0200 Message-Id: <20200917100614.1010-8-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200917100614.1010-1-oneukum@suse.com> References: <20200917100614.1010-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 f952eec87b0f..7607ab2bbe07 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; @@ -750,6 +777,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,