All of lore.kernel.org
 help / color / mirror / Atom feed
From: Matt Mackall <mpm@selenic.com>
To: Andrew Morton <akpm@osdl.org>
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH 12/22] /dev/random: add pool for /dev/urandom to prevent starv
Date: Thu, 25 Mar 2004 17:57:44 -0600	[thread overview]
Message-ID: <13.524465763@selenic.com> (raw)
In-Reply-To: <12.524465763@selenic.com>


/dev/random  add pool for /dev/urandom to prevent starvation

Prevent readers of /dev/urandom from starving readers of /dev/random
by adding a second output pool for nonblocking reads. /dev/urandom
will always leave enough entropy in input pool for /dev/random to
reseed itself.


 tiny-mpm/drivers/char/random.c |   32 +++++++++++++++++---------------
 1 files changed, 17 insertions(+), 15 deletions(-)

diff -puN drivers/char/random.c~nonblocking-pool drivers/char/random.c
--- tiny/drivers/char/random.c~nonblocking-pool	2004-03-20 13:38:28.000000000 -0600
+++ tiny-mpm/drivers/char/random.c	2004-03-20 13:38:28.000000000 -0600
@@ -399,8 +399,7 @@ static struct poolinfo {
 /*
  * Static global variables
  */
-static struct entropy_store *input_pool; /* The default global store */
-static struct entropy_store *blocking_pool; /* secondary store */
+static struct entropy_store *input_pool, *blocking_pool, *nonblocking_pool;
 static DECLARE_WAIT_QUEUE_HEAD(random_read_wait);
 static DECLARE_WAIT_QUEUE_HEAD(random_write_wait);
 
@@ -477,9 +476,11 @@ static inline __u32 int_ln_12bits(__u32 
 #endif
 
 #if 0
-#define DEBUG_ENT(fmt, arg...) printk(KERN_DEBUG "random %04d %04d: " fmt,\
+#define DEBUG_ENT(fmt, arg...) \
+printk(KERN_DEBUG "random %04d %04d %04d: " fmt,\
 	input_pool->entropy_count,\
 	blocking_pool->entropy_count,\
+	nonblocking_pool->entropy_count,\
 	## arg)
 #else
 #define DEBUG_ENT(fmt, arg...) do {} while (0)
@@ -700,8 +701,7 @@ EXPORT_SYMBOL(batch_entropy_store);
  */
 static void batch_entropy_process(void *private_)
 {
-	struct entropy_store *r	= (struct entropy_store *) private_, *p;
-	int max_entropy = r->poolinfo->POOLBITS;
+	struct entropy_store *r	= input_pool;
 	unsigned head, tail;
 
 	/* Mixing into the pool is expensive, so copy over the batch
@@ -720,18 +720,17 @@ static void batch_entropy_process(void *
 
 	spin_unlock_irq(&batch_lock);
 
-	p = r;
 	while (head != tail) {
-		if (r->entropy_count >= max_entropy) {
-			r = (r == blocking_pool) ? input_pool :
-							blocking_pool;
-			max_entropy = r->poolinfo->POOLBITS;
-		}
+		if (r->entropy_count >= r->poolinfo->POOLBITS)
+			r = blocking_pool;
+		if (r->entropy_count >= r->poolinfo->POOLBITS)
+			r = nonblocking_pool;
+
 		add_entropy_words(r, batch_entropy_copy[tail].data, 2);
 		credit_entropy_store(r, batch_entropy_copy[tail].credit);
 		tail = (tail+1) & (batch_max-1);
 	}
-	if (p->entropy_count >= random_read_wakeup_thresh)
+	if (input_pool->entropy_count >= random_read_wakeup_thresh)
 		wake_up_interruptible(&random_read_wait);
 }
 
@@ -1427,7 +1426,7 @@ static ssize_t extract_entropy(struct en
 void get_random_bytes(void *buf, int nbytes)
 {
 	BUG_ON(!blocking_pool);
-	extract_entropy(blocking_pool, buf, nbytes, 0, 0, 0);
+	extract_entropy(nonblocking_pool, buf, nbytes, 0, 0, 0);
 }
 
 EXPORT_SYMBOL(get_random_bytes);
@@ -1473,8 +1472,11 @@ static int __init rand_initialize(void)
 
 	input_pool = create_entropy_store(INPUT_POOL_SIZE, "input");
 	blocking_pool = create_entropy_store(BLOCKING_POOL_SIZE, "blocking");
+	nonblocking_pool = create_entropy_store(BLOCKING_POOL_SIZE,
+						"nonblocking");
+	nonblocking_pool->reserved = 1;
 
-	if (!(input_pool && blocking_pool))
+	if (!(input_pool && blocking_pool && nonblocking_pool))
 		return -1;
 
 	if (batch_entropy_init(BATCH_ENTROPY_SIZE, input_pool))
@@ -1595,7 +1597,7 @@ static ssize_t
 urandom_read(struct file * file, char * buf,
 		      size_t nbytes, loff_t *ppos)
 {
-	return extract_entropy(blocking_pool, buf, nbytes, 0, 0,
+	return extract_entropy(nonblocking_pool, buf, nbytes, 0, 0,
 			       EXTRACT_ENTROPY_USER);
 }
 

_

  reply	other threads:[~2004-03-26  0:05 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-03-25 23:57 [PATCH 0/22] /dev/random: Assorted fixes and cleanups Matt Mackall
2004-03-25 23:57 ` [PATCH 1/22] /dev/random: Simplify entropy debugging Matt Mackall
2004-03-25 23:57   ` [PATCH 2/22] /dev/random: Cleanup sleep logic Matt Mackall
2004-03-25 23:57     ` [PATCH 3/22] /dev/random: remove broken resizing sysctl Matt Mackall
2004-03-25 23:57       ` [PATCH 4/22] /dev/random: remove outdated RNDGETPOOL ioctl Matt Mackall
2004-03-25 23:57         ` [PATCH 5/22] /dev/random: pool struct cleanup and rename Matt Mackall
2004-03-25 23:57           ` [PATCH 6/22] /dev/random: simplify pool initialization Matt Mackall
2004-03-25 23:57             ` [PATCH 7/22] /dev/random: simplify reseed logic Matt Mackall
2004-03-25 23:57               ` [PATCH 8/22] /dev/random: BUG on premature random users Matt Mackall
2004-03-25 23:57                 ` [PATCH 9/22] /dev/random: more robust catastrophic reseed logic Matt Mackall
2004-03-25 23:57                   ` [PATCH 10/22] /dev/random: entropy reserve logic for starvation preve Matt Mackall
2004-03-25 23:57                     ` [PATCH 11/22] /dev/random: flag pools that need entropy reserve Matt Mackall
2004-03-25 23:57                       ` Matt Mackall [this message]
2004-03-25 23:57                         ` [PATCH 13/22] /dev/random: kill extract_timer_state Matt Mackall
2004-03-25 23:57                           ` [PATCH 14/22] /dev/random: kill unused md5 copy Matt Mackall
2004-03-25 23:57                             ` [PATCH 15/22] /dev/random: kill unrolled SHA code Matt Mackall
2004-03-25 23:57                               ` [PATCH 16/22] /dev/random: kill 2.2 cruft Matt Mackall
2004-03-25 23:57                                 ` [PATCH 17/22] /dev/random: minor shrinkage Matt Mackall
2004-03-25 23:57                                   ` [PATCH 18/22] /dev/random: bitop cleanup Matt Mackall
2004-03-25 23:57                                     ` [PATCH 19/22] /dev/random: use sched_clock for timing data Matt Mackall
2004-03-25 23:57                                       ` [PATCH 20/22] /dev/random: cleanup rol bitop Matt Mackall
2004-03-25 23:57                                         ` [PATCH 21/22] /dev/random: kill batching of entropy mixing Matt Mackall
2004-03-25 23:57                                           ` [PATCH 22/22] /dev/random: update credits Matt Mackall
2004-03-27 13:52                                           ` [PATCH 21/22] /dev/random: kill batching of entropy mixing Jamie Lokier
2004-03-27 15:17                                             ` Matt Mackall
2004-03-26  1:43                               ` [PATCH 15/22] /dev/random: kill unrolled SHA code Jeff Garzik
2004-03-26  3:59                                 ` Matt Mackall
2004-03-27 13:49                                   ` Jamie Lokier
2004-03-26  0:15         ` [PATCH 4/22] /dev/random: remove outdated RNDGETPOOL ioctl Andrew Morton
2004-03-26  0:15       ` [PATCH 3/22] /dev/random: remove broken resizing sysctl Andrew Morton
2004-03-26  3:53         ` Matt Mackall
2004-03-26  0:14     ` [PATCH 2/22] /dev/random: Cleanup sleep logic Andrew Morton
2004-03-26  3:49       ` Matt Mackall

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=13.524465763@selenic.com \
    --to=mpm@selenic.com \
    --cc=akpm@osdl.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.