From patchwork Thu Apr 17 14:45:27 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Neukum X-Patchwork-Id: 882567 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8A1602512C5 for ; Thu, 17 Apr 2025 14:47:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744901246; cv=none; b=Va33RKniwAduYeEx0PugCCmeP0SNhzXpMmcCcRs5b1xaacE/KO+Txbocyi0kQwYnnYt3V8b5eOgrDyZatskQJvpJN/o7br9QOr/rpToNaAJhGbTESIlyVa04heCMB9RMugr6nzDsr+F7vZpdi4O+My8Ysc41Mk5yIMrakvKHAyI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744901246; c=relaxed/simple; bh=5W9U/ko3+PTtmdOZxuvPLBnCNsTnmlhTl93LPTxZ6SY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=t45T3HZN8r/kA/T3TRMKDkk0FD+ERjV1EB+v2Kk0vuaaFvKt0CYLdtmiCdiYWhPBL9j+X876170vfLg2M47PeWkHmHyq3skWrTBSqgx2+K5uLcFUiR+MoQatf/t60cFSbot6Kh58J8QTfDuPuLXCI/J7RZEOy/CbSRU7H7XG/nU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 7089C2116B; Thu, 17 Apr 2025 14:47:22 +0000 (UTC) Authentication-Results: smtp-out1.suse.de; none Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 54B8A1398F; Thu, 17 Apr 2025 14:47:22 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id 6EqRE3oUAWidWQAAD6G6ig (envelope-from ); Thu, 17 Apr 2025 14:47:22 +0000 From: Oliver Neukum To: keithp@keithp.com, linux-usb@vger.kernel.org Cc: Oliver Neukum Subject: [RFC 1/3] chaoskey: O_NONBLOCK in concurrent reads Date: Thu, 17 Apr 2025 16:45:27 +0200 Message-ID: <20250417144719.182160-2-oneukum@suse.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250417144719.182160-1-oneukum@suse.com> References: <20250417144719.182160-1-oneukum@suse.com> Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Spam-Level: X-Spamd-Result: default: False [-4.00 / 50.00]; REPLY(-4.00)[] X-Spam-Score: -4.00 X-Spam-Flag: NO X-Rspamd-Queue-Id: 7089C2116B X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Rspamd-Action: no action X-Rspamd-Server: rspamd2.dmz-prg2.suse.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 225863321dc4..45cff32656c6 100644 --- a/drivers/usb/misc/chaoskey.c +++ b/drivers/usb/misc/chaoskey.c @@ -444,9 +444,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) {