From patchwork Mon Sep 21 11:37: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: 11789483 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 9112A59D for ; Mon, 21 Sep 2020 11:37:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7798020EDD for ; Mon, 21 Sep 2020 11:37:56 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="DXl0Yl4L" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726861AbgIULhz (ORCPT ); Mon, 21 Sep 2020 07:37:55 -0400 Received: from mx2.suse.de ([195.135.220.15]:45680 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726803AbgIULhz (ORCPT ); Mon, 21 Sep 2020 07:37: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=1600688274; 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=sYvQsLemH5qScXlX0gMPR+JDqQ7X1AsCDAL6RYSxupc=; b=DXl0Yl4LCBg2hGspUu84S//Qgn8OlK8vkU5x2AKFAlH40iDc+Op2WhBILLWEHFfez74Z4C TN6G+CsxEFsRYjrY1X1GtzUr3LeZ2SX43Ek4ROUlBiK0s/H2IYqMjcuKZMMnAXrTQm4hwl ChmgUaLmaLHsHQCILEullo+0W4FQd4k= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 640B4AF2A; Mon, 21 Sep 2020 11:38:30 +0000 (UTC) From: Oliver Neukum To: keithp@keithp.com, linux-usb@vger.kernel.org Cc: Oliver Neukum Subject: [RFT 1/4] chaoskey: O_NONBLOCK in concurrent reads Date: Mon, 21 Sep 2020 13:37:29 +0200 Message-Id: <20200921113732.11524-2-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200921113732.11524-1-oneukum@suse.com> References: <20200921113732.11524-1-oneukum@suse.com> Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org This changes the locking in chaoskey_read() to correctly handle O_NONBLOCK in the case of concurrent readers. Signed-off-by: Oliver Neukum --- drivers/usb/misc/chaoskey.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/drivers/usb/misc/chaoskey.c b/drivers/usb/misc/chaoskey.c index 665c75a1cdf9..ad4c0b6d02cf 100644 --- a/drivers/usb/misc/chaoskey.c +++ b/drivers/usb/misc/chaoskey.c @@ -435,9 +435,19 @@ static ssize_t chaoskey_read(struct file *file, goto bail; mutex_unlock(&dev->rng_lock); - result = mutex_lock_interruptible(&dev->lock); - if (result) - goto bail; + if (file->f_flags & O_NONBLOCK) { + result = mutex_trylock(&dev->lock); + if (result == 0) { + result = -EAGAIN; + goto bail; + } else { + result = 0; + } + } else { + result = mutex_lock_interruptible(&dev->lock); + if (result) + goto bail; + } if (dev->valid == dev->used) { result = _chaoskey_fill(dev); if (result < 0) { From patchwork Mon Sep 21 11:37:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 11789487 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 7291859D for ; Mon, 21 Sep 2020 11:37:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 54F1020BED for ; Mon, 21 Sep 2020 11:37:58 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="k0rulDly" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726862AbgIULh5 (ORCPT ); Mon, 21 Sep 2020 07:37:57 -0400 Received: from mx2.suse.de ([195.135.220.15]:45696 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726860AbgIULh4 (ORCPT ); Mon, 21 Sep 2020 07:37:56 -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=1600688274; 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=Yu2UIPZdnC3rC5zUhHQZTIJxVQ7KVQ/n4uYuoJNyZhk=; b=k0rulDlyohydW1hhaN/zJNsqZeVbE/FLjWpi7LdOp9oXaJ07hD8WBhIxCYf9RzRicKC8+5 mdd80l+kkhR+5Bck/D3O6bn2F54124uPMYzzsQuZRcLh+i7ae4aJbOmAbtuM7XK2A+mGRm UDJZ4SRjBMc23MZeAnZvYks2cjIPgpk= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id EB5B3B54A; Mon, 21 Sep 2020 11:38:30 +0000 (UTC) From: Oliver Neukum To: keithp@keithp.com, linux-usb@vger.kernel.org Cc: Oliver Neukum Subject: [RFT 2/4] chaoskey: introduce asynchronous reads Date: Mon, 21 Sep 2020 13:37:30 +0200 Message-Id: <20200921113732.11524-3-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200921113732.11524-1-oneukum@suse.com> References: <20200921113732.11524-1-oneukum@suse.com> Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org This divides requesting IO and waiting for IO from each other. Signed-off-by: Oliver Neukum Reported-by: kernel test robot --- drivers/usb/misc/chaoskey.c | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/drivers/usb/misc/chaoskey.c b/drivers/usb/misc/chaoskey.c index ad4c0b6d02cf..d47c2cc65269 100644 --- a/drivers/usb/misc/chaoskey.c +++ b/drivers/usb/misc/chaoskey.c @@ -336,15 +336,13 @@ static void chaos_read_callback(struct urb *urb) wake_up(&dev->wait_q); } -/* Fill the buffer. Called with dev->lock held - */ -static int _chaoskey_fill(struct chaoskey *dev) +static int chaoskey_request_fill(struct chaoskey *dev) { DEFINE_WAIT(wait); int result; bool started; - usb_dbg(dev->interface, "fill"); + usb_dbg(dev->interface, "request fill"); /* Return immediately if someone called before the buffer was * empty */ @@ -382,10 +380,26 @@ static int _chaoskey_fill(struct chaoskey *dev) */ started = dev->reads_started; dev->reads_started = true; + /* + * powering down while a read is under way + * is blocked in suspend() + */ + usb_autopm_put_interface(dev->interface); + return 0; +out: + usb_autopm_put_interface(dev->interface); + return result; +} + +static int chaoskey_wait_fill(struct chaoskey *dev) +{ + DEFINE_WAIT(wait); + int result; + result = wait_event_interruptible_timeout( dev->wait_q, !dev->reading, - (started ? NAK_TIMEOUT : ALEA_FIRST_TIMEOUT) ); + (dev->reads_started ? NAK_TIMEOUT : ALEA_FIRST_TIMEOUT) ); if (result < 0) { usb_kill_urb(dev->urb); @@ -400,7 +414,6 @@ static int _chaoskey_fill(struct chaoskey *dev) } out: /* Let the device go back to sleep eventually */ - usb_autopm_put_interface(dev->interface); usb_dbg(dev->interface, "read %d bytes", dev->valid); @@ -449,7 +462,12 @@ static ssize_t chaoskey_read(struct file *file, goto bail; } if (dev->valid == dev->used) { - result = _chaoskey_fill(dev); + result = chaoskey_request_fill(dev); + if (result < 0) { + mutex_unlock(&dev->lock); + goto bail; + } + result = chaoskey_wait_fill(dev); if (result < 0) { mutex_unlock(&dev->lock); goto bail; @@ -517,7 +535,7 @@ static int chaoskey_rng_read(struct hwrng *rng, void *data, * the buffer will still be empty */ if (dev->valid == dev->used) - (void) _chaoskey_fill(dev); + (void) chaoskey_request_fill(dev); this_time = dev->valid - dev->used; if (this_time > max) @@ -537,6 +555,11 @@ static int chaoskey_rng_read(struct hwrng *rng, void *data, static int chaoskey_suspend(struct usb_interface *interface, pm_message_t message) { + struct chaoskey *dev = usb_get_intfdata(interface); + + if (dev->reading && PMSG_IS_AUTO(message)) + return -EBUSY; + usb_dbg(interface, "suspend"); return 0; } From patchwork Mon Sep 21 11:37:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 11789485 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 C8FE459D for ; Mon, 21 Sep 2020 11:37:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AF44A20EDD for ; Mon, 21 Sep 2020 11:37:57 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="ReQwmY7U" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726876AbgIULh5 (ORCPT ); Mon, 21 Sep 2020 07:37:57 -0400 Received: from mx2.suse.de ([195.135.220.15]:45710 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726553AbgIULh4 (ORCPT ); Mon, 21 Sep 2020 07:37:56 -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=1600688275; 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=ahVSMqMWxQiTVFLLP0Rh9U0UGtU0tS0+yc58D7AdFLc=; b=ReQwmY7UPSCvYxWDvYdS/CIau9V8b7R1cSHs9vFHjKA4DxC7VeDaeqOKqvEYF2FU7BBAFv IGWhyyHcnxGocFSqQZRz5zmpa/nuaA2WA8z73CHy3qRBRX/HJLpTY3SfnakKRkb8GHb1aL kuuBBAlv1spaNfC4Ei3EHk+CV51wYyA= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 75CFEB54C; Mon, 21 Sep 2020 11:38:31 +0000 (UTC) From: Oliver Neukum To: keithp@keithp.com, linux-usb@vger.kernel.org Cc: Oliver Neukum Subject: [RFT 3/4] chaoskey: make read() obey O_NONBLOCK Date: Mon, 21 Sep 2020 13:37:31 +0200 Message-Id: <20200921113732.11524-4-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200921113732.11524-1-oneukum@suse.com> References: <20200921113732.11524-1-oneukum@suse.com> Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org This skips waiting for a read if O_NONBLOCK is set. Signed-off-by: Oliver Neukum --- drivers/usb/misc/chaoskey.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/drivers/usb/misc/chaoskey.c b/drivers/usb/misc/chaoskey.c index d47c2cc65269..0d80cba162a4 100644 --- a/drivers/usb/misc/chaoskey.c +++ b/drivers/usb/misc/chaoskey.c @@ -467,10 +467,14 @@ static ssize_t chaoskey_read(struct file *file, mutex_unlock(&dev->lock); goto bail; } - result = chaoskey_wait_fill(dev); - if (result < 0) { - mutex_unlock(&dev->lock); - goto bail; + if (!(file->f_flags & O_NONBLOCK)) { + result = chaoskey_wait_fill(dev); + if (result < 0) { + mutex_unlock(&dev->lock); + goto bail; + } + } else { + result = -EAGAIN; } } From patchwork Mon Sep 21 11:37:32 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 11789489 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 47B4A59D for ; Mon, 21 Sep 2020 11:37:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2442F20BED for ; Mon, 21 Sep 2020 11:37:59 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="hWQ1cc0r" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726877AbgIULh6 (ORCPT ); Mon, 21 Sep 2020 07:37:58 -0400 Received: from mx2.suse.de ([195.135.220.15]:45724 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726803AbgIULh4 (ORCPT ); Mon, 21 Sep 2020 07:37:56 -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=1600688275; 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=kOAvk89wVGacoYWtvsN0jTXXcK4NTRAGpet9z8LSDX4=; b=hWQ1cc0r7FtbcFH+mFfjk5QF2rQYf134LDMF0EEG4C4wzQeWxOLr6qksdmbhlVUhYkv6Dm 1r50H1ca1OXNXj/XuuXtsHexLf/h2ACVEyj35g9YHxNbVZnvz4rwlexV78bh9VNdEIyFGL 8vVffhrCo8jQp0gaYEXAWzB30ruqj3U= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id B621AB54D; Mon, 21 Sep 2020 11:38:31 +0000 (UTC) From: Oliver Neukum To: keithp@keithp.com, linux-usb@vger.kernel.org Cc: Oliver Neukum Subject: [RFT 4/4] chaoskey: request data asynchronously Date: Mon, 21 Sep 2020 13:37:32 +0200 Message-Id: <20200921113732.11524-5-oneukum@suse.com> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20200921113732.11524-1-oneukum@suse.com> References: <20200921113732.11524-1-oneukum@suse.com> Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org This requests more data if a read has exhausted the buffer just to have it ready sooner. Signed-off-by: Oliver Neukum --- drivers/usb/misc/chaoskey.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/drivers/usb/misc/chaoskey.c b/drivers/usb/misc/chaoskey.c index 0d80cba162a4..5773cf477e4d 100644 --- a/drivers/usb/misc/chaoskey.c +++ b/drivers/usb/misc/chaoskey.c @@ -358,6 +358,9 @@ static int chaoskey_request_fill(struct chaoskey *dev) return -ENODEV; } + if (dev->reading) + return -EBUSY; + /* Make sure the device is awake */ result = usb_autopm_get_interface(dev->interface); if (result) { @@ -500,13 +503,16 @@ static ssize_t chaoskey_read(struct file *file, dev->used += this_time; mutex_unlock(&dev->lock); } + /* request data on suspicion that it will eventually be used */ + if (dev->valid == dev->used) + (void)chaoskey_request_fill(dev); bail: if (read_count) { usb_dbg(dev->interface, "read %zu bytes", read_count); return read_count; } usb_dbg(dev->interface, "empty read, result %d", result); - if (result == -ETIMEDOUT) + if (result == -ETIMEDOUT || result == -EBUSY) result = -EAGAIN; return result; }