diff mbox

[v7,1/5] random: Blocking API for accessing nonblocking_pool

Message ID 143216583.t5fUODkLsI@tachyon.chronox.de (mailing list archive)
State Changes Requested
Delegated to: Herbert Xu
Headers show

Commit Message

Stephan Mueller May 20, 2015, 5:44 p.m. UTC
The added API calls provide a synchronous function call
get_blocking_random_bytes where the caller is blocked until
the nonblocking_pool is initialized.

CC: Andreas Steffen <andreas.steffen@strongswan.org>
CC: Theodore Ts'o <tytso@mit.edu>
CC: Sandy Harris <sandyinchina@gmail.com>
Signed-off-by: Stephan Mueller <smueller@chronox.de>
---
 drivers/char/random.c  | 18 ++++++++++++++++++
 include/linux/random.h |  1 +
 2 files changed, 19 insertions(+)

Comments

Herbert Xu May 20, 2015, 6:45 p.m. UTC | #1
On Wed, May 20, 2015 at 07:44:39PM +0200, Stephan Mueller wrote:
>
> +	if (unlikely(nonblocking_pool.initialized == 0)) {
> +		do {
> +			rc = wait_event_interruptible(urandom_init_wait,
> +						nonblocking_pool.initialized);
> +		} while (rc == -ERESTARTSYS);

This is just a convoluted way of doing an uninterruptible sleep.
Either make it uninterruptible or allow the function to return
an error.

Cheers,
Stephan Mueller May 20, 2015, 6:49 p.m. UTC | #2
Am Donnerstag, 21. Mai 2015, 02:45:35 schrieb Herbert Xu:

Hi Herbert,

> On Wed, May 20, 2015 at 07:44:39PM +0200, Stephan Mueller wrote:
> > +	if (unlikely(nonblocking_pool.initialized == 0)) {
> > +		do {
> > +			rc = wait_event_interruptible(urandom_init_wait,
> > +						nonblocking_pool.initialized);
> > +		} while (rc == -ERESTARTSYS);
> 
> This is just a convoluted way of doing an uninterruptible sleep.
> Either make it uninterruptible or allow the function to return
> an error.

Sorry, I overlooked the availability of wait_event. I was looking for it 
initially, but missed it. I will fix it right away.
Stephan Mueller May 20, 2015, 7:38 p.m. UTC | #3
Am Mittwoch, 20. Mai 2015, 20:49:45 schrieb Stephan Mueller:

Hi Herbert,

> > This is just a convoluted way of doing an uninterruptible sleep.
> > Either make it uninterruptible or allow the function to return
> > an error.
> 
> Sorry, I overlooked the availability of wait_event. I was looking for it
> initially, but missed it. I will fix it right away.

After checking a bit more, I see that an uninterruptible sleep cannot be 
canceled with cancel_work_sync. Therefore, replacing it with wait_event does 
not work.

Thus, go get an uninterruptible sleep which yet can be canceled seems to 
require wait_event_interruptible together with the check for ERESTARTSYS.

Nonetheless, I move the loop out to the DRBG code as requested.
diff mbox

Patch

diff --git a/drivers/char/random.c b/drivers/char/random.c
index 9cd6968..3d1c027 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -1245,6 +1245,24 @@  void get_random_bytes(void *buf, int nbytes)
 EXPORT_SYMBOL(get_random_bytes);
 
 /*
+ * Equivalent function to get_random_bytes with the difference that this
+ * function blocks the request until the nonblocking_pool is initialized.
+ */
+void get_blocking_random_bytes(void *buf, int nbytes)
+{
+	int rc;
+
+	if (unlikely(nonblocking_pool.initialized == 0)) {
+		do {
+			rc = wait_event_interruptible(urandom_init_wait,
+						nonblocking_pool.initialized);
+		} while (rc == -ERESTARTSYS);
+	}
+	extract_entropy(&nonblocking_pool, buf, nbytes, 0, 0);
+}
+EXPORT_SYMBOL(get_blocking_random_bytes);
+
+/*
  * This function will use the architecture-specific hardware random
  * number generator if it is available.  The arch-specific hw RNG will
  * almost certainly be faster than what we can do in software, but it
diff --git a/include/linux/random.h b/include/linux/random.h
index b05856e..796267d 100644
--- a/include/linux/random.h
+++ b/include/linux/random.h
@@ -14,6 +14,7 @@  extern void add_input_randomness(unsigned int type, unsigned int code,
 extern void add_interrupt_randomness(int irq, int irq_flags);
 
 extern void get_random_bytes(void *buf, int nbytes);
+extern void get_blocking_random_bytes(void *buf, int nbytes);
 extern void get_random_bytes_arch(void *buf, int nbytes);
 void generate_random_uuid(unsigned char uuid_out[16]);
 extern int random_int_secret_init(void);