From patchwork Tue Sep 22 10:13: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: 11792053 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id F1D22139A for ; Tue, 22 Sep 2020 10:13:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CE7B62395B for ; Tue, 22 Sep 2020 10:13:57 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="nxe71Pf+" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726614AbgIVKN4 (ORCPT ); Tue, 22 Sep 2020 06:13:56 -0400 Received: from mx2.suse.de ([195.135.220.15]:57256 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726461AbgIVKNy (ORCPT ); Tue, 22 Sep 2020 06:13:54 -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=1600769632; 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=nxe71Pf+goYVFg+7mAks34CTC2PLYBJga/vYylhxGjv6oZ6LIEELdVKutw7hlviqKchXgw NKPyFZWmL+AE69g7lBnmwoZ1D53ewxjzc9/zb7NN+XISd3LnYPHQNRCtiE/HIgnCB6muHq UKyjsUt8xl5/l773z7FP4TTn98iEOG0= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 3AC93AFC0; Tue, 22 Sep 2020 10:14:29 +0000 (UTC) From: Oliver Neukum To: penguin-kernel@i-love.sakura.ne.jp, bjorn@mork.no, linux-usb@vger.kernel.org Cc: Oliver Neukum Subject: [RFC 1/6] CDC-WDM: fix hangs in flush() in multithreaded cases Date: Tue, 22 Sep 2020 12:13:24 +0200 Message-Id: <20200922101329.14801-2-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200922101329.14801-1-oneukum@suse.com> References: <20200922101329.14801-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 10:13: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: 11792049 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E9F7E139A for ; Tue, 22 Sep 2020 10:13:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C7D912395B for ; Tue, 22 Sep 2020 10:13:55 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="ClIR5y/A" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726610AbgIVKNz (ORCPT ); Tue, 22 Sep 2020 06:13:55 -0400 Received: from mx2.suse.de ([195.135.220.15]:57288 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726603AbgIVKNy (ORCPT ); Tue, 22 Sep 2020 06:13:54 -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=1600769632; 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=ClIR5y/AzkBBrptKFa/nsVH3qtXs6ZN8ruJYF41+FxNTnVbY40YZQPtyvUmjjsSjIFlKI6 D9TgVVs5UqosWZDrccx3HLiYZSrKV/3kPHYVzaxeuTD+g0DoCFRkHgDE7cKpuux6JZkTo0 fB8CCwRY+ZzP7uLNmkrSnQpVhr/tsFU= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 80328B03D; Tue, 22 Sep 2020 10:14:29 +0000 (UTC) From: Oliver Neukum To: penguin-kernel@i-love.sakura.ne.jp, bjorn@mork.no, linux-usb@vger.kernel.org Cc: Oliver Neukum Subject: [RFC 2/6] CDC-WDM: introduce a timeout in flush() Date: Tue, 22 Sep 2020 12:13:25 +0200 Message-Id: <20200922101329.14801-3-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200922101329.14801-1-oneukum@suse.com> References: <20200922101329.14801-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 10:13: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: 11792047 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 674A8618 for ; Tue, 22 Sep 2020 10:13:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3F3E7238D7 for ; Tue, 22 Sep 2020 10:13:55 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="m/ttvBtC" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726607AbgIVKNy (ORCPT ); Tue, 22 Sep 2020 06:13:54 -0400 Received: from mx2.suse.de ([195.135.220.15]:57334 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726604AbgIVKNy (ORCPT ); Tue, 22 Sep 2020 06:13:54 -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=1600769633; 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=m/ttvBtCfIEOJJJwtIHFToehD7wQ20YaUX4a8XvM+B5UpiIzar//Vl6dIh0eV5loFVPPtI q+NJzZjvdj153zK2nAZ5IepWat1iojtBzElToDtRusz0501scdqKKrPnjwuR5uXBAip0gi R8gqZvNWUuKOGdImyB/3IUsuZrtFrm0= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id C748BB213; Tue, 22 Sep 2020 10:14:29 +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/6] CDC-WDM: remove use of intf->dev after potential disconnect Date: Tue, 22 Sep 2020 12:13:26 +0200 Message-Id: <20200922101329.14801-4-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200922101329.14801-1-oneukum@suse.com> References: <20200922101329.14801-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 10:13:27 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 11792051 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 771016CB for ; Tue, 22 Sep 2020 10:13:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 518CB238D7 for ; Tue, 22 Sep 2020 10:13:57 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="It8rTNJz" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726509AbgIVKNz (ORCPT ); Tue, 22 Sep 2020 06:13:55 -0400 Received: from mx2.suse.de ([195.135.220.15]:57352 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726605AbgIVKNy (ORCPT ); Tue, 22 Sep 2020 06:13:54 -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=1600769633; 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=It8rTNJzce86wU+tUp1PnTI34w/VBTqETGCQ5NlyRgXO+iJ19TJ70cVcrcgPpGp3HiOgyv b1rtjSZ3TRFCn/huAi2WD3WUwhgQlAXMogv27Gy7hO9i34oROqFYAfwiWQ25LobEzyvWtE uXVOsZgtZiPBNklTTkSd0uNHDLIDYgM= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 1BDF0ABD2; Tue, 22 Sep 2020 10:14:30 +0000 (UTC) From: Oliver Neukum To: penguin-kernel@i-love.sakura.ne.jp, bjorn@mork.no, linux-usb@vger.kernel.org Cc: Oliver Neukum Subject: [RFC 4/6] CDC-WDM: fix race reporting errors in flush Date: Tue, 22 Sep 2020 12:13:27 +0200 Message-Id: <20200922101329.14801-5-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200922101329.14801-1-oneukum@suse.com> References: <20200922101329.14801-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 10:13:28 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 11792055 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0D27F6CB for ; Tue, 22 Sep 2020 10:13:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DA88F238D7 for ; Tue, 22 Sep 2020 10:13:58 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="kjG0aSqX" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726461AbgIVKN5 (ORCPT ); Tue, 22 Sep 2020 06:13:57 -0400 Received: from mx2.suse.de ([195.135.220.15]:57420 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726609AbgIVKNz (ORCPT ); Tue, 22 Sep 2020 06:13:55 -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=1600769633; 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=kjG0aSqXh3sVlnYVFb7chij5FgUp8MxK8Qfsftj3ObS92JGytJGyYuhOuDdBNaX4nhLT1h ZwE87Nbm167CgBw9qhFZYYblaSBKZDDd5SKQa3xBHbRFHsXAlCGNsWJukkKzJeK0iQKz5G bj63IhycLk9FdpDcEmDDzJgmxKpAxs8= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 64191B215; Tue, 22 Sep 2020 10:14:30 +0000 (UTC) From: Oliver Neukum To: penguin-kernel@i-love.sakura.ne.jp, bjorn@mork.no, linux-usb@vger.kernel.org Cc: Oliver Neukum Subject: [RFC 5/6] CDC-WDM: correct error reporting in write() Date: Tue, 22 Sep 2020 12:13:28 +0200 Message-Id: <20200922101329.14801-6-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200922101329.14801-1-oneukum@suse.com> References: <20200922101329.14801-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 10:13:29 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 11792059 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 43581618 for ; Tue, 22 Sep 2020 10:14:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1B4BA2395B for ; Tue, 22 Sep 2020 10:14:00 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="BbKT2oAC" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726617AbgIVKN6 (ORCPT ); Tue, 22 Sep 2020 06:13:58 -0400 Received: from mx2.suse.de ([195.135.220.15]:57422 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726604AbgIVKNz (ORCPT ); Tue, 22 Sep 2020 06:13:55 -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=1600769634; 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=BbKT2oAC6RvZNYxT5mDZK+I2Q+SKnEYByF9E4nkQUJIG10mZ8ZtUPJq8TQ0r1O7sP00WTp Uz7sSyvWyr8L0t1mktrhe3b7+EHRNwFnzbKyfgWAgJoSO6wAZXglKZTRcc1M8Lb0ugd/jZ 3W8gYtgwbu2qdM94h9gmSxv2Aynh8C4= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id AC59BB18C; Tue, 22 Sep 2020 10:14:30 +0000 (UTC) From: Oliver Neukum To: penguin-kernel@i-love.sakura.ne.jp, bjorn@mork.no, linux-usb@vger.kernel.org Cc: Oliver Neukum Subject: [RFC 6/6] CDC-WDM: implement fsync Date: Tue, 22 Sep 2020 12:13:29 +0200 Message-Id: <20200922101329.14801-7-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200922101329.14801-1-oneukum@suse.com> References: <20200922101329.14801-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,