Skip to contentSkip to conversation

adding support for nonblocking reads to chaoskey

RFC New Oliver Neukum 1 participant 4 messages Apr 17, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

In general drivers should support nonblocking IO.
This is intended to add it to the chaoskey driver
In-Reply-To:

#1 lore raw

RFC 1/3 chaoskey: O_NONBLOCK in concurrent reads

This changes the locking in chaoskey_read() to correctly handle O_NONBLOCK in the case of concurrent readers.

---
 drivers/usb/misc/chaoskey.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/usb/misc/chaoskey.c +13 -3
index 225863321dc4..45cff32656c6 100644
@@ -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) {
Signature · 1 line
2.49.0
#2 lore raw

RFC 2/3 chaoskey: introduce asynchronous reads

In reply to Oliver Neukum

This divides requesting IO and waiting for IO from each other. This allows for nonblocking IO.

---
 drivers/usb/misc/chaoskey.c | 58 +++++++++++++++++++++++++------------
 1 file changed, 40 insertions(+), 18 deletions(-)
drivers/usb/misc/chaoskey.c +40 -18
index 45cff32656c6..4c53e432c416 100644
@@ -345,15 +345,12 @@ 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 */
@@ -378,19 +375,27 @@ static int _chaoskey_fill(struct chaoskey *dev)
 
 	dev->reading = true;
 	result = usb_submit_urb(dev->urb, GFP_KERNEL);
-	if (result < 0) {
-		result = usb_translate_errors(result);
-		dev->reading = false;
+	if (result < 0)
 		goto out;
-	}
 
-	/* The first read on the Alea takes a little under 2 seconds.
-	 * Reads after the first read take only a few microseconds
-	 * though.  Presumably the entropy-generating circuit needs
-	 * time to ramp up.  So, we wait longer on the first read.
+	/*
+	 * powering down while a read is under way
+	 * is blocked in suspend()
 	 */
-	started = dev->reads_started;
-	dev->reads_started = true;
+	usb_autopm_put_interface(dev->interface);
+	return 0;
+out:
+	dev->reading = false;
+	usb_autopm_put_interface(dev->interface);
+	return usb_translate_errors(result);
+}
+
+static int chaoskey_wait_fill(struct chaoskey *dev)
+{
+	DEFINE_WAIT(wait);
+	int result;
+	bool started = dev->reads_started;
+
 	result = wait_event_interruptible_timeout(
 		dev->wait_q,
 		!dev->reading,
@@ -406,10 +411,17 @@ static int _chaoskey_fill(struct chaoskey *dev)
 		usb_kill_urb(dev->urb);
 	} else {
 		result = dev->valid;
+
+		/* The first read on the Alea takes a little under 2 seconds.
+		 * Reads after the first read take only a few microseconds
+		 * though.  Presumably the entropy-generating circuit needs
+		 * time to ramp up.  So, we waited longer on the first read.
+		 */
+		dev->reads_started = true;
 	}
+
 out:
 	/* Let the device go back to sleep eventually */
-	usb_autopm_put_interface(dev->interface);
 
 	usb_dbg(dev->interface, "read %d bytes", dev->valid);
 
@@ -458,7 +470,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;
@@ -526,7 +543,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)
@@ -546,6 +563,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;
 }
Signature · 1 line
2.49.0
#3 lore raw

RFC 3/3 chaoskey: make read() obey O_NONBLOCK

In reply to Oliver Neukum

This skips waiting for a read if O_NONBLOCK is set.

---
 drivers/usb/misc/chaoskey.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/usb/misc/chaoskey.c +8 -4
index 4c53e432c416..509eb9bb5000 100644
@@ -475,10 +475,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;
 			}
 		}
 
Signature · 1 line
2.49.0