Skip to content

Commit 72d3e09

Browse files
obbardcrichardweinberger
authored andcommitted
um: random: Register random as hwrng-core device
The UML random driver creates a dummy device under the guest, /dev/hw_random. When this file is read from the guest, the driver reads from the host machine's /dev/random, in-turn reading from the host kernel's entropy pool. This entropy pool could have been filled by a hardware random number generator or just the host kernel's internal software entropy generator. Currently the driver does not fill the guests kernel entropy pool, this requires a userspace tool running inside the guest (like rng-tools) to read from the dummy device provided by this driver, which then would fill the guest's internal entropy pool. This all seems quite pointless when we are already reading from an entropy pool, so this patch aims to register the device as a hwrng device using the hwrng-core framework. This not only improves and cleans up the driver, but also fills the guest's entropy pool without having to resort to using extra userspace tools in the guest. This is typically a nuisance when booting a guest: the random pool takes a long time (~200s) to build up enough entropy since the dummy hwrng is not used to fill the guest's pool. This port was originally attempted by Alexander Neville "dark" (in CC, discussion in Link), but the conversation there stalled since the handling of -EAGAIN errors were no removed and longer handled by the driver. This patch attempts to use the existing method of error handling but utilises the new hwrng core. The issue can be noticed when booting a UML guest: [ 2.560000] random: fast init done [ 214.000000] random: crng init done With the patch applied, filling the pool becomes a lot quicker: [ 2.560000] random: fast init done [ 12.000000] random: crng init done Cc: Alexander Neville <dark@volatile.bz> Link: https://lore.kernel.org/lkml/20190828204609.02a7ff70@TheDarkness/ Link: https://lore.kernel.org/lkml/20190829135001.6a5ff940@TheDarkness.local/ Cc: Sjoerd Simons <sjoerd.simons@collabora.co.uk> Signed-off-by: Christopher Obbard <chris.obbard@collabora.com> Acked-by: Anton Ivanov <anton.ivanov@cambridgegreys.com> Signed-off-by: Richard Weinberger <richard@nod.at>
1 parent db03b42 commit 72d3e09

2 files changed

Lines changed: 33 additions & 84 deletions

File tree

‎arch/um/drivers/random.c‎

Lines changed: 25 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -11,105 +11,53 @@
1111
#include <linux/fs.h>
1212
#include <linux/interrupt.h>
1313
#include <linux/miscdevice.h>
14+
#include <linux/hw_random.h>
1415
#include <linux/delay.h>
1516
#include <linux/uaccess.h>
1617
#include <init.h>
1718
#include <irq_kern.h>
1819
#include <os.h>
1920

2021
/*
21-
* core module and version information
22+
* core module information
2223
*/
23-
#define RNG_VERSION "1.0.0"
2424
#define RNG_MODULE_NAME "hw_random"
2525

2626
/* Changed at init time, in the non-modular case, and at module load
2727
* time, in the module case. Presumably, the module subsystem
2828
* protects against a module being loaded twice at the same time.
2929
*/
3030
static int random_fd = -1;
31-
static DECLARE_WAIT_QUEUE_HEAD(host_read_wait);
31+
static struct hwrng hwrng = { 0, };
32+
static DECLARE_COMPLETION(have_data);
3233

33-
static int rng_dev_open (struct inode *inode, struct file *filp)
34+
static int rng_dev_read(struct hwrng *rng, void *buf, size_t max, bool block)
3435
{
35-
/* enforce read-only access to this chrdev */
36-
if ((filp->f_mode & FMODE_READ) == 0)
37-
return -EINVAL;
38-
if ((filp->f_mode & FMODE_WRITE) != 0)
39-
return -EINVAL;
36+
int ret;
4037

41-
return 0;
42-
}
43-
44-
static atomic_t host_sleep_count = ATOMIC_INIT(0);
45-
46-
static ssize_t rng_dev_read (struct file *filp, char __user *buf, size_t size,
47-
loff_t *offp)
48-
{
49-
u32 data;
50-
int n, ret = 0, have_data;
51-
52-
while (size) {
53-
n = os_read_file(random_fd, &data, sizeof(data));
54-
if (n > 0) {
55-
have_data = n;
56-
while (have_data && size) {
57-
if (put_user((u8) data, buf++)) {
58-
ret = ret ? : -EFAULT;
59-
break;
60-
}
61-
size--;
62-
ret++;
63-
have_data--;
64-
data >>= 8;
65-
}
66-
}
67-
else if (n == -EAGAIN) {
68-
DECLARE_WAITQUEUE(wait, current);
69-
70-
if (filp->f_flags & O_NONBLOCK)
71-
return ret ? : -EAGAIN;
72-
73-
atomic_inc(&host_sleep_count);
38+
for (;;) {
39+
ret = os_read_file(random_fd, buf, max);
40+
if (block && ret == -EAGAIN) {
7441
add_sigio_fd(random_fd);
7542

76-
add_wait_queue(&host_read_wait, &wait);
77-
set_current_state(TASK_INTERRUPTIBLE);
43+
ret = wait_for_completion_killable(&have_data);
7844

79-
schedule();
80-
remove_wait_queue(&host_read_wait, &wait);
45+
ignore_sigio_fd(random_fd);
46+
deactivate_fd(random_fd, RANDOM_IRQ);
8147

82-
if (atomic_dec_and_test(&host_sleep_count)) {
83-
ignore_sigio_fd(random_fd);
84-
deactivate_fd(random_fd, RANDOM_IRQ);
85-
}
48+
if (ret < 0)
49+
break;
50+
} else {
51+
break;
8652
}
87-
else
88-
return n;
89-
90-
if (signal_pending (current))
91-
return ret ? : -ERESTARTSYS;
9253
}
93-
return ret;
94-
}
9554

96-
static const struct file_operations rng_chrdev_ops = {
97-
.owner = THIS_MODULE,
98-
.open = rng_dev_open,
99-
.read = rng_dev_read,
100-
.llseek = noop_llseek,
101-
};
102-
103-
/* rng_init shouldn't be called more than once at boot time */
104-
static struct miscdevice rng_miscdev = {
105-
HWRNG_MINOR,
106-
RNG_MODULE_NAME,
107-
&rng_chrdev_ops,
108-
};
55+
return ret != -EAGAIN ? ret : 0;
56+
}
10957

11058
static irqreturn_t random_interrupt(int irq, void *data)
11159
{
112-
wake_up(&host_read_wait);
60+
complete(&have_data);
11361

11462
return IRQ_HANDLED;
11563
}
@@ -126,18 +74,19 @@ static int __init rng_init (void)
12674
goto out;
12775

12876
random_fd = err;
129-
13077
err = um_request_irq(RANDOM_IRQ, random_fd, IRQ_READ, random_interrupt,
13178
0, "random", NULL);
13279
if (err)
13380
goto err_out_cleanup_hw;
13481

13582
sigio_broken(random_fd, 1);
83+
hwrng.name = RNG_MODULE_NAME;
84+
hwrng.read = rng_dev_read;
85+
hwrng.quality = 1024;
13686

137-
err = misc_register (&rng_miscdev);
87+
err = hwrng_register(&hwrng);
13888
if (err) {
139-
printk (KERN_ERR RNG_MODULE_NAME ": misc device register "
140-
"failed\n");
89+
pr_err(RNG_MODULE_NAME " registering failed (%d)\n", err);
14190
goto err_out_cleanup_hw;
14291
}
14392
out:
@@ -161,8 +110,8 @@ static void cleanup(void)
161110

162111
static void __exit rng_cleanup(void)
163112
{
113+
hwrng_unregister(&hwrng);
164114
os_close_file(random_fd);
165-
misc_deregister (&rng_miscdev);
166115
}
167116

168117
module_init (rng_init);

‎drivers/char/hw_random/Kconfig‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -540,15 +540,15 @@ endif # HW_RANDOM
540540

541541
config UML_RANDOM
542542
depends on UML
543-
tristate "Hardware random number generator"
543+
select HW_RANDOM
544+
tristate "UML Random Number Generator support"
544545
help
545546
This option enables UML's "hardware" random number generator. It
546547
attaches itself to the host's /dev/random, supplying as much entropy
547548
as the host has, rather than the small amount the UML gets from its
548-
own drivers. It registers itself as a standard hardware random number
549-
generator, major 10, minor 183, and the canonical device name is
550-
/dev/hwrng.
551-
The way to make use of this is to install the rng-tools package
552-
(check your distro, or download from
553-
http://sourceforge.net/projects/gkernel/). rngd periodically reads
554-
/dev/hwrng and injects the entropy into /dev/random.
549+
own drivers. It registers itself as a rng-core driver thus providing
550+
a device which is usually called /dev/hwrng. This hardware random
551+
number generator does feed into the kernel's random number generator
552+
entropy pool.
553+
554+
If unsure, say Y.

0 commit comments

Comments
 (0)