Module Name:    src
Committed By:   riastradh
Date:           Fri May  8 15:57:24 UTC 2020

Modified Files:
        src/sys/dev: random.c

Log Message:
Simplify loops by putting interrupt test at end.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/dev/random.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/random.c
diff -u src/sys/dev/random.c:1.5 src/sys/dev/random.c:1.6
--- src/sys/dev/random.c:1.5	Fri May  8 15:55:05 2020
+++ src/sys/dev/random.c	Fri May  8 15:57:24 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: random.c,v 1.5 2020/05/08 15:55:05 riastradh Exp $	*/
+/*	$NetBSD: random.c,v 1.6 2020/05/08 15:57:24 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2019 The NetBSD Foundation, Inc.
@@ -47,7 +47,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: random.c,v 1.5 2020/05/08 15:55:05 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: random.c,v 1.6 2020/05/08 15:57:24 riastradh Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -213,7 +213,6 @@ random_read(dev_t dev, struct uio *uio, 
 	struct nist_hash_drbg drbg;
 	uint8_t *buf;
 	int extractflags;
-	bool interruptible;
 	int error;
 
 	/* Get a buffer for transfers.  */
@@ -258,18 +257,10 @@ random_read(dev_t dev, struct uio *uio, 
 	/* Promptly zero the seed.  */
 	explicit_memset(seed, 0, sizeof seed);
 
-	/*
-	 * Generate data.  Assume no error until failure.  No
-	 * interruption at this point until we've generated at least
-	 * one block of output.
-	 */
+	/* Generate data.  */
 	error = 0;
-	interruptible = false;
 	while (uio->uio_resid) {
-		size_t n = uio->uio_resid;
-
-		/* No more than one buffer's worth.  */
-		n = MIN(n, RANDOM_BUFSIZE);
+		size_t n = MIN(uio->uio_resid, RANDOM_BUFSIZE);
 
 		/*
 		 * Clamp /dev/random output to the entropy capacity and
@@ -286,22 +277,6 @@ random_read(dev_t dev, struct uio *uio, 
 			CTASSERT(sizeof seed <= RANDOM_BUFSIZE);
 		}
 
-		/* Yield if requested.  */
-		if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
-			preempt();
-
-		/*
-		 * Allow interruption, but only after providing a
-		 * minimum number of bytes.
-		 */
-		CTASSERT(RANDOM_BUFSIZE >= 256);
-		/* Check for interruption.  */
-		if (__predict_false(curlwp->l_flag & LW_PENDSIG) &&
-		    interruptible && sigispending(curlwp, 0)) {
-			error = EINTR; /* XXX ERESTART? */
-			break;
-		}
-
 		/*
 		 * Try to generate a block of data, but if we've hit
 		 * the DRBG reseed interval, reseed.
@@ -348,11 +323,17 @@ random_read(dev_t dev, struct uio *uio, 
 			break;
 		}
 
-		/*
-		 * We have generated one block of output, so it is
-		 * reasonable to allow interruption after this point.
-		 */
-		interruptible = true;
+		/* Yield if requested.  */
+		if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
+			preempt();
+
+		/* Check for interruption after at least 256 bytes.  */
+		CTASSERT(RANDOM_BUFSIZE >= 256);
+		if (__predict_false(curlwp->l_flag & LW_PENDSIG) &&
+		    sigispending(curlwp, 0)) {
+			error = EINTR;
+			break;
+		}
 	}
 
 out:	/* Zero the buffer and free it.  */
@@ -401,10 +382,14 @@ random_write(dev_t dev, struct uio *uio,
 
 	/* Consume data.  */
 	while (uio->uio_resid) {
-		size_t n = uio->uio_resid;
+		size_t n = MIN(uio->uio_resid, RANDOM_BUFSIZE);
 
-		/* No more than one buffer's worth in one step.  */
-		n = MIN(uio->uio_resid, RANDOM_BUFSIZE);
+		/* Transfer n bytes in and enter them into the pool.  */
+		error = uiomove(buf, n, uio);
+		if (error)
+			break;
+		rnd_add_data(&user_rndsource, buf, n, privileged ? n*NBBY : 0);
+		any = true;
 
 		/* Yield if requested.  */
 		if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
@@ -416,13 +401,6 @@ random_write(dev_t dev, struct uio *uio,
 			error = EINTR; /* XXX ERESTART?  */
 			break;
 		}
-
-		/* Transfer n bytes in and enter them into the pool.  */
-		error = uiomove(buf, n, uio);
-		if (error)
-			break;
-		rnd_add_data(&user_rndsource, buf, n, privileged ? n*NBBY : 0);
-		any = true;
 	}
 
 	/* Zero the buffer and free it.  */

Reply via email to