From patchwork Mon Sep 21 11:20:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 258488 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 295A5C43465 for ; Mon, 21 Sep 2020 11:21:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CBEE820EDD for ; Mon, 21 Sep 2020 11:21:16 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="jE9zsGvE" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726628AbgIULVQ (ORCPT ); Mon, 21 Sep 2020 07:21:16 -0400 Received: from mx2.suse.de ([195.135.220.15]:58058 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726424AbgIULVQ (ORCPT ); Mon, 21 Sep 2020 07:21:16 -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=1600687274; 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=jE9zsGvE7WSt9LjMhLFbpX2R42G0XOJLeZS2yvwRGECRfWBSh6FsGJr3Oi0ouoXPfaeUKy tmMLUWKfEs0m0IHEozR835vRb0HqbY0tkbkHt27D3AIK8StIao8JfQ4LvTnOAz/PmkZz+C rlfgx3kWLuvLU6JHiT6dhMCkbIh12e8= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 6A3FFADE2; Mon, 21 Sep 2020 11:21:50 +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/8] CDC-WDM: fix hangs in flush() Date: Mon, 21 Sep 2020 13:20:45 +0200 Message-Id: <20200921112052.27943-2-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200921112052.27943-1-oneukum@suse.com> References: <20200921112052.27943-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 Mon Sep 21 11:20:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 297571 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 4871CC43466 for ; Mon, 21 Sep 2020 11:21:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E66802145D for ; Mon, 21 Sep 2020 11:21:22 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="ge7sQTBC" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726424AbgIULVW (ORCPT ); Mon, 21 Sep 2020 07:21:22 -0400 Received: from mx2.suse.de ([195.135.220.15]:58074 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726367AbgIULVQ (ORCPT ); Mon, 21 Sep 2020 07:21:16 -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=1600687274; 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=ge7sQTBCMxh280t5RfKq+JSlq4X678mNsMqAhzPDylc4ofVFA8GCQT0oIFoXCsYDAww3u6 n+5b9T6azRsC7etWWfQHlge5iVciuMphXQzCkL9YW2sUk7HKSU6rgz+28afFPxfeUzT1IX k/pw4y0uPdEtg+uyOb6UYPBP6gxognU= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id BC3CCB182; Mon, 21 Sep 2020 11:21:50 +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/8] CDC-WDM: introduce a timeout in flush() Date: Mon, 21 Sep 2020 13:20:46 +0200 Message-Id: <20200921112052.27943-3-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200921112052.27943-1-oneukum@suse.com> References: <20200921112052.27943-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 Mon Sep 21 11:20:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 258487 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 C2992C43463 for ; Mon, 21 Sep 2020 11:21:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8F3FE207BC for ; Mon, 21 Sep 2020 11:21:18 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="btKs0g1v" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726649AbgIULVR (ORCPT ); Mon, 21 Sep 2020 07:21:17 -0400 Received: from mx2.suse.de ([195.135.220.15]:58094 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726578AbgIULVQ (ORCPT ); Mon, 21 Sep 2020 07:21:16 -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=1600687275; 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=FeAcLLM341BKKNzjslQfIRBdgdtgiXj38Xh/Q56X87Y=; b=btKs0g1vrlBu/YgGrMQ1YBrlGHcTF0+KALecmSaGELUtvPdlSx4PMepp/rfi3kEIRQR2Mo pCi+5XS+AhoBO7EzQOUv5ZzYh/dmkig6HIjsWUuowFpTAS4GkKoWHk0SbtDDib3oQssGMg pPeJJ7AmE0lNPDxIrr3lLHNKtI4alA0= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 10D10B200; Mon, 21 Sep 2020 11:21:51 +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/8] CDC-WDM: making flush() interruptible Date: Mon, 21 Sep 2020 13:20:47 +0200 Message-Id: <20200921112052.27943-4-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200921112052.27943-1-oneukum@suse.com> References: <20200921112052.27943-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. 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 Mon Sep 21 11:20:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 258485 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 0A530C43465 for ; Mon, 21 Sep 2020 11:21:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BEEBC21531 for ; Mon, 21 Sep 2020 11:21:19 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="IalD1rie" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726648AbgIULVR (ORCPT ); Mon, 21 Sep 2020 07:21:17 -0400 Received: from mx2.suse.de ([195.135.220.15]:58110 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726593AbgIULVQ (ORCPT ); Mon, 21 Sep 2020 07:21:16 -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=1600687275; 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=6sv+MNNsiVmYiUMnZzQlm95iIxK3Jn1uonWRbwxA3U0=; b=IalD1rie1RRHB6Jm/i5sjNLebzxbcPMJ+Vwx0eZORie0rAGRFEGmAUb1h89xAC7PaNiLkH +TaTKgWedsvuMmt7sVCMJ1sQ69WQ1SQLsvpg4Vyvy8O5LZUak4RLt7bzqq6dcZTgNomncH n5E4xyBSOjTZdSM9jchX/1E3B+Hulec= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 5462DB202; Mon, 21 Sep 2020 11:21:51 +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/8] CDC-WDM: remove use of intf->dev after potential disconnect Date: Mon, 21 Sep 2020 13:20:48 +0200 Message-Id: <20200921112052.27943-5-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200921112052.27943-1-oneukum@suse.com> References: <20200921112052.27943-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 | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/drivers/usb/class/cdc-wdm.c b/drivers/usb/class/cdc-wdm.c index eee19532e67e..40661b9f6775 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) @@ -611,9 +615,7 @@ static int wdm_flush(struct file *file, fl_owner_t id) if (rv < 0) return -EINTR; rv = desc->werr; - if (rv < 0) - dev_err(&desc->intf->dev, "Error in flush path: %d\n", - rv); + desc->werr = 0; return usb_translate_errors(rv); } From patchwork Mon Sep 21 11:20:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 297574 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 47CC7C43464 for ; Mon, 21 Sep 2020 11:21:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0D75220874 for ; Mon, 21 Sep 2020 11:21:18 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="VV8GTBfG" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726632AbgIULVR (ORCPT ); Mon, 21 Sep 2020 07:21:17 -0400 Received: from mx2.suse.de ([195.135.220.15]:58124 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726627AbgIULVQ (ORCPT ); Mon, 21 Sep 2020 07:21:16 -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=1600687275; 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=dPYL0e0Ftt9dWXB+VlV5J7hKo86XPX/rxVVpKU2I0Q4=; b=VV8GTBfGPLCUMpAhc3C2w1Nzrm+lheD729/iQ47fsmTwzySAheQRfOX898g2ds2Wd6K3yS 1tH8HxjpnKAWlqhTwg/VspIMJKKJ5qODGYb8R+QB9SPOoIPgLqYKy5Che1wu9p6MkC/Nzh CkDpYu7PnTckxm1TIKcYTtoEPV1tnYY= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 9F3D9B207; Mon, 21 Sep 2020 11:21:51 +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/8] CDC-WDM: fix race reporting errors in flush Date: Mon, 21 Sep 2020 13:20:49 +0200 Message-Id: <20200921112052.27943-6-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200921112052.27943-1-oneukum@suse.com> References: <20200921112052.27943-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 40661b9f6775..89929f6438e3 100644 --- a/drivers/usb/class/cdc-wdm.c +++ b/drivers/usb/class/cdc-wdm.c @@ -614,8 +614,11 @@ 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); return usb_translate_errors(rv); } From patchwork Mon Sep 21 11:20:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 297572 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 62E94C43463 for ; Mon, 21 Sep 2020 11:21:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2D01F21531 for ; Mon, 21 Sep 2020 11:21:21 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="JWet3tZA" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726457AbgIULVU (ORCPT ); Mon, 21 Sep 2020 07:21:20 -0400 Received: from mx2.suse.de ([195.135.220.15]:58136 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726424AbgIULVR (ORCPT ); Mon, 21 Sep 2020 07:21:17 -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=1600687276; 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=JWet3tZA9Ckzkgyvsq8d7Q4MB5Q4MrVc5HNPWv1pTb8xn8T5WKz3RBblBmDq7EtM45PdKB FWHAAyKAfLQd7ERsQPUTUiWup1OjLKrqsyvXDlE9f00S1qrABU9POT2/LylPGQW6hcXl/l ukB6Y/tazyKpqR8LmPekbmxZvg829ic= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id E8E1AB208; Mon, 21 Sep 2020 11:21:51 +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/8] CDC-WDM: correct error reporting in write() Date: Mon, 21 Sep 2020 13:20:50 +0200 Message-Id: <20200921112052.27943-7-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200921112052.27943-1-oneukum@suse.com> References: <20200921112052.27943-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 Mon Sep 21 11:20:51 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 258486 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 39335C43464 for ; Mon, 21 Sep 2020 11:21:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EC07720EDD for ; Mon, 21 Sep 2020 11:21:20 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="kBgbhuss" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726666AbgIULVT (ORCPT ); Mon, 21 Sep 2020 07:21:19 -0400 Received: from mx2.suse.de ([195.135.220.15]:58154 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726630AbgIULVR (ORCPT ); Mon, 21 Sep 2020 07:21:17 -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=1600687276; 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=kBgbhussuUGVt27LIDWVUCrG9jMv/3OYbYEOlyw6dSG4K2qZvjNSfXmb453v4L28cQhvwP zmbjvRpmlmsp2XZV4/powBXgw9eUN36g7XRI7PXxEVknlQbVC2oV9i91r39ir+Tds2XoP4 uy9TEeRTYXsj89aDZzbS+txtCqpU+fA= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 46CCCB225; Mon, 21 Sep 2020 11:21:52 +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/8] CDC-WDM: implement fsync Date: Mon, 21 Sep 2020 13:20:51 +0200 Message-Id: <20200921112052.27943-8-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200921112052.27943-1-oneukum@suse.com> References: <20200921112052.27943-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, From patchwork Mon Sep 21 11:20:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 297573 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 CE7AEC43466 for ; Mon, 21 Sep 2020 11:21:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 98F5C20EDD for ; Mon, 21 Sep 2020 11:21:19 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="ZH5NCXjw" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726654AbgIULVS (ORCPT ); Mon, 21 Sep 2020 07:21:18 -0400 Received: from mx2.suse.de ([195.135.220.15]:58110 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726532AbgIULVR (ORCPT ); Mon, 21 Sep 2020 07:21:17 -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=1600687276; 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=/ngJDL6/z4BJCeT0oPCblUO16rv/1+hCOitTErETCi0=; b=ZH5NCXjwyacORxWak9yPY8h/p1Ab6S6o8DhyEpemCng4vexkRw0mGlpbHw2IspJfCdD2OH pCHDtpqjpRpQ/YBJNdkGUq2gSVVYSXRS4DzppypFxIRX3boLI6KKB245384dFP/FW3Ep7z 5ic+98WKbaVAsdxGJUJcIb6tXt2AqGo= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 8B98EB20B; Mon, 21 Sep 2020 11:21:52 +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 8/8] CDC-WDM: reduce scope of wdm_mutex Date: Mon, 21 Sep 2020 13:20:52 +0200 Message-Id: <20200921112052.27943-9-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200921112052.27943-1-oneukum@suse.com> References: <20200921112052.27943-1-oneukum@suse.com> Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org Use the global mutex for as short as possible in open() Signed-off-by: Oliver Neukum --- drivers/usb/class/cdc-wdm.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/drivers/usb/class/cdc-wdm.c b/drivers/usb/class/cdc-wdm.c index 7607ab2bbe07..230fe66828e3 100644 --- a/drivers/usb/class/cdc-wdm.c +++ b/drivers/usb/class/cdc-wdm.c @@ -704,6 +704,7 @@ static int wdm_open(struct inode *inode, struct file *file) /* using write lock to protect desc->count */ mutex_lock(&desc->wlock); + mutex_unlock(&wdm_mutex); if (!desc->count++) { /* in case flush() had timed out */ usb_kill_urb(desc->command); @@ -716,13 +717,14 @@ static int wdm_open(struct inode *inode, struct file *file) "Error submitting int urb - %d\n", rv); rv = usb_translate_errors(rv); } + if (desc->count == 1) + desc->manage_power(intf, 1); } else { rv = 0; } mutex_unlock(&desc->wlock); - if (desc->count == 1) - desc->manage_power(intf, 1); usb_autopm_put_interface(desc->intf); + return rv; out: mutex_unlock(&wdm_mutex); return rv;