Module Name:    src
Committed By:   tls
Date:           Sat Jan 26 19:05:12 UTC 2013

Modified Files:
        src/sys/kern: kern_rndq.c
        src/sys/sys: rnd.h

Log Message:
Rather than holding samples from each source until we have 64 at a time to
process, process them ASAP for low-rate sources, and for all sources if we
have not yet acquired initial entropy.


To generate a diff of this commit:
cvs rdiff -u -r1.8 -r1.9 src/sys/kern/kern_rndq.c
cvs rdiff -u -r1.34 -r1.35 src/sys/sys/rnd.h

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

Modified files:

Index: src/sys/kern/kern_rndq.c
diff -u src/sys/kern/kern_rndq.c:1.8 src/sys/kern/kern_rndq.c:1.9
--- src/sys/kern/kern_rndq.c:1.8	Thu Jan 24 14:23:45 2013
+++ src/sys/kern/kern_rndq.c	Sat Jan 26 19:05:12 2013
@@ -1,7 +1,7 @@
-/*	$NetBSD: kern_rndq.c,v 1.8 2013/01/24 14:23:45 riastradh Exp $	*/
+/*	$NetBSD: kern_rndq.c,v 1.9 2013/01/26 19:05:12 tls Exp $	*/
 
 /*-
- * Copyright (c) 1997-2011 The NetBSD Foundation, Inc.
+ * Copyright (c) 1997-2013 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_rndq.c,v 1.8 2013/01/24 14:23:45 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_rndq.c,v 1.9 2013/01/26 19:05:12 tls Exp $");
 
 #include <sys/param.h>
 #include <sys/ioctl.h>
@@ -80,12 +80,6 @@ int	rnd_debug = 0;
 #endif
 
 /*
- * The size of a temporary buffer, kmem_alloc()ed when needed, and used for
- * reading and writing data.
- */
-#define	RND_TEMP_BUFFER_SIZE	128
-
-/*
  * This is a little bit of state information attached to each device that we
  * collect entropy from.  This is simply a collection buffer, and when it
  * is full it will be "detached" from the source and added to the entropy
@@ -665,6 +659,7 @@ rnd_add_data_ts(krndsource_t *rs, const 
 	rnd_sample_t *state = NULL;
 	const uint32_t *dint = data;
 	int todo, done, filled = 0;
+	int sample_count;
 	SIMPLEQ_HEAD(, _rnd_sample_t) tmp_samples =
 	    		SIMPLEQ_HEAD_INITIALIZER(tmp_samples);
 
@@ -672,11 +667,40 @@ rnd_add_data_ts(krndsource_t *rs, const 
 		return;
 	}
 
+	todo = len / sizeof(*dint);
+	/*
+	 * Let's try to be efficient: if we are warm, and a source
+	 * is adding entropy at a rate of at least 1 bit every 10 seconds,
+	 * mark it as "fast" and add its samples in bulk.
+	 */
+	if (__predict_true(rs->flags & RND_FLAG_FAST)) {
+		sample_count = RND_SAMPLE_COUNT;
+	} else {
+		if (!cold && rnd_initial_entropy) {
+			struct timeval upt;
+
+			getmicrouptime(&upt);
+			if ((todo >= RND_SAMPLE_COUNT) ||
+			    (rs->total > upt.tv_sec) ||
+			     (upt.tv_sec > 10 &&
+			      rs->total > upt.tv_sec / 10)) {
+#ifdef RND_VERBOSE
+				printf("rnd: source %s is fast (%d samples "
+				       "at once, %d bits in %lld seconds), "
+				       "processing samples in bulk.\n",
+				       rs->name, todo, rs->total,
+				       (long long int)upt.tv_sec);
+#endif
+				rs->flags |= RND_FLAG_FAST;
+			}
+		}
+		sample_count = 2;
+	}
+
 	/*
 	 * Loop over data packaging it into sample buffers.
 	 * If a sample buffer allocation fails, drop all data.
 	 */
-	todo = len / sizeof(*dint);
 	for (done = 0; done < todo ; done++) {
 		state = rs->state;
 		if (state == NULL) {
@@ -691,7 +715,7 @@ rnd_add_data_ts(krndsource_t *rs, const 
 		state->values[state->cursor] = dint[done];
 		state->cursor++;
 
-		if (state->cursor == RND_SAMPLE_COUNT) {
+		if (state->cursor == sample_count) {
 			SIMPLEQ_INSERT_HEAD(&tmp_samples, state, next);
 			filled++;
 			rs->state = NULL;

Index: src/sys/sys/rnd.h
diff -u src/sys/sys/rnd.h:1.34 src/sys/sys/rnd.h:1.35
--- src/sys/sys/rnd.h:1.34	Sun Nov 25 15:29:55 2012
+++ src/sys/sys/rnd.h	Sat Jan 26 19:05:11 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: rnd.h,v 1.34 2012/11/25 15:29:55 christos Exp $	*/
+/*	$NetBSD: rnd.h,v 1.35 2013/01/26 19:05:11 tls Exp $	*/
 
 /*-
  * Copyright (c) 1997 The NetBSD Foundation, Inc.
@@ -91,6 +91,7 @@ typedef struct {
  */
 #define	RND_FLAG_NO_ESTIMATE	0x00000100	/* don't estimate entropy */
 #define	RND_FLAG_NO_COLLECT	0x00000200	/* don't collect entropy */
+#define RND_FLAG_FAST		0x00000400	/* process samples in bulk */
 
 #define	RND_TYPE_UNKNOWN	0	/* unknown source */
 #define	RND_TYPE_DISK		1	/* source is physical disk */

Reply via email to