Commit f7f154f1 authored by Rusty Russell's avatar Rusty Russell

hw_random: make buffer usable in scatterlist.

virtio_rng feeds the randomness buffer handed by the core directly
into the scatterlist, since commit bb347d98.

However, if CONFIG_HW_RANDOM=m, the static buffer isn't a linear address
(at least on most archs).  We could fix this in virtio_rng, but it's actually
far easier to just do it in the core as virtio_rng would have to allocate
a buffer every time (it doesn't know how much the core will want to read).
Reported-by: default avatarAurelien Jarno <aurelien@aurel32.net>
Tested-by: default avatarAurelien Jarno <aurelien@aurel32.net>
Signed-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
Cc: stable@kernel.org
parent 6dbe51c2
...@@ -40,6 +40,7 @@ ...@@ -40,6 +40,7 @@
#include <linux/init.h> #include <linux/init.h>
#include <linux/miscdevice.h> #include <linux/miscdevice.h>
#include <linux/delay.h> #include <linux/delay.h>
#include <linux/slab.h>
#include <asm/uaccess.h> #include <asm/uaccess.h>
...@@ -52,8 +53,12 @@ static struct hwrng *current_rng; ...@@ -52,8 +53,12 @@ static struct hwrng *current_rng;
static LIST_HEAD(rng_list); static LIST_HEAD(rng_list);
static DEFINE_MUTEX(rng_mutex); static DEFINE_MUTEX(rng_mutex);
static int data_avail; static int data_avail;
static u8 rng_buffer[SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES] static u8 *rng_buffer;
__cacheline_aligned;
static size_t rng_buffer_size(void)
{
return SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES;
}
static inline int hwrng_init(struct hwrng *rng) static inline int hwrng_init(struct hwrng *rng)
{ {
...@@ -116,7 +121,7 @@ static ssize_t rng_dev_read(struct file *filp, char __user *buf, ...@@ -116,7 +121,7 @@ static ssize_t rng_dev_read(struct file *filp, char __user *buf,
if (!data_avail) { if (!data_avail) {
bytes_read = rng_get_data(current_rng, rng_buffer, bytes_read = rng_get_data(current_rng, rng_buffer,
sizeof(rng_buffer), rng_buffer_size(),
!(filp->f_flags & O_NONBLOCK)); !(filp->f_flags & O_NONBLOCK));
if (bytes_read < 0) { if (bytes_read < 0) {
err = bytes_read; err = bytes_read;
...@@ -307,6 +312,14 @@ int hwrng_register(struct hwrng *rng) ...@@ -307,6 +312,14 @@ int hwrng_register(struct hwrng *rng)
mutex_lock(&rng_mutex); mutex_lock(&rng_mutex);
/* kmalloc makes this safe for virt_to_page() in virtio_rng.c */
err = -ENOMEM;
if (!rng_buffer) {
rng_buffer = kmalloc(rng_buffer_size(), GFP_KERNEL);
if (!rng_buffer)
goto out_unlock;
}
/* Must not register two RNGs with the same name. */ /* Must not register two RNGs with the same name. */
err = -EEXIST; err = -EEXIST;
list_for_each_entry(tmp, &rng_list, list) { list_for_each_entry(tmp, &rng_list, list) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment