Commit 5d33e4d7 authored by Jeff Dike's avatar Jeff Dike Committed by Linus Torvalds

uml: random driver fixes

The random driver would essentially hang if the host's /dev/random returned
-EAGAIN.  There was a test of need_resched followed by a schedule inside the
loop, but that didn't help and it's the wrong way to work anyway.

The right way is to ask for an interrupt when there is input available from
the host and handle it then rather than polling.

Now, when the host's /dev/random returns -EAGAIN, the driver asks for a wakeup
when there's randomness available again and sleeps.  The interrupt routine
just wakes up whatever processes are sleeping on host_read_wait.

There is an atomic_t, host_sleep_count, which counts the number of processes
waiting for randomness.  When this reaches zero, the interrupt is disabled.

An added complication is that async I/O notification was only recently added
to /dev/random (by me), so essentially all hosts will lack it.  So, we use the
sigio workaround here, which is to have a separate thread poll on the
descriptor and send an interrupt when there is input on it.  This mechanism is
activated when a process gets -EAGAIN (activating this multiple times is
harmless, if a bit wasteful) and deactivated by the last process still
waiting.

The module name was changed from "random" to "hw_random" in order for udev to
recognize it.

The sigio workaround needed some changes.  sigio_broken was added for cases
when we know that async notification doesn't work.  This is now called from
maybe_sigio_broken, which deals with pts devices.
Signed-off-by: default avatarJeff Dike <jdike@linux.intel.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 60a2988a
...@@ -8,16 +8,18 @@ ...@@ -8,16 +8,18 @@
#include <linux/sched.h> #include <linux/sched.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/fs.h> #include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/miscdevice.h> #include <linux/miscdevice.h>
#include <linux/delay.h> #include <linux/delay.h>
#include <asm/uaccess.h> #include <asm/uaccess.h>
#include "irq_kern.h"
#include "os.h" #include "os.h"
/* /*
* core module and version information * core module and version information
*/ */
#define RNG_VERSION "1.0.0" #define RNG_VERSION "1.0.0"
#define RNG_MODULE_NAME "random" #define RNG_MODULE_NAME "hw_random"
#define RNG_MISCDEV_MINOR 183 /* official */ #define RNG_MISCDEV_MINOR 183 /* official */
...@@ -26,6 +28,7 @@ ...@@ -26,6 +28,7 @@
* protects against a module being loaded twice at the same time. * protects against a module being loaded twice at the same time.
*/ */
static int random_fd = -1; static int random_fd = -1;
static DECLARE_WAIT_QUEUE_HEAD(host_read_wait);
static int rng_dev_open (struct inode *inode, struct file *filp) static int rng_dev_open (struct inode *inode, struct file *filp)
{ {
...@@ -38,6 +41,8 @@ static int rng_dev_open (struct inode *inode, struct file *filp) ...@@ -38,6 +41,8 @@ static int rng_dev_open (struct inode *inode, struct file *filp)
return 0; return 0;
} }
static atomic_t host_sleep_count = ATOMIC_INIT(0);
static ssize_t rng_dev_read (struct file *filp, char __user *buf, size_t size, static ssize_t rng_dev_read (struct file *filp, char __user *buf, size_t size,
loff_t * offp) loff_t * offp)
{ {
...@@ -60,11 +65,26 @@ static ssize_t rng_dev_read (struct file *filp, char __user *buf, size_t size, ...@@ -60,11 +65,26 @@ static ssize_t rng_dev_read (struct file *filp, char __user *buf, size_t size,
} }
} }
else if(n == -EAGAIN){ else if(n == -EAGAIN){
DECLARE_WAITQUEUE(wait, current);
if (filp->f_flags & O_NONBLOCK) if (filp->f_flags & O_NONBLOCK)
return ret ? : -EAGAIN; return ret ? : -EAGAIN;
if(need_resched()) atomic_inc(&host_sleep_count);
schedule_timeout_interruptible(1); reactivate_fd(random_fd, RANDOM_IRQ);
add_sigio_fd(random_fd);
add_wait_queue(&host_read_wait, &wait);
set_task_state(current, TASK_INTERRUPTIBLE);
schedule();
set_task_state(current, TASK_RUNNING);
remove_wait_queue(&host_read_wait, &wait);
if (atomic_dec_and_test(&host_sleep_count)) {
ignore_sigio_fd(random_fd);
deactivate_fd(random_fd, RANDOM_IRQ);
}
} }
else return n; else return n;
if (signal_pending (current)) if (signal_pending (current))
...@@ -86,6 +106,13 @@ static struct miscdevice rng_miscdev = { ...@@ -86,6 +106,13 @@ static struct miscdevice rng_miscdev = {
&rng_chrdev_ops, &rng_chrdev_ops,
}; };
static irqreturn_t random_interrupt(int irq, void *data)
{
wake_up(&host_read_wait);
return IRQ_HANDLED;
}
/* /*
* rng_init - initialize RNG module * rng_init - initialize RNG module
*/ */
...@@ -99,10 +126,14 @@ static int __init rng_init (void) ...@@ -99,10 +126,14 @@ static int __init rng_init (void)
random_fd = err; random_fd = err;
err = os_set_fd_block(random_fd, 0); err = um_request_irq(RANDOM_IRQ, random_fd, IRQ_READ, random_interrupt,
IRQF_DISABLED | IRQF_SAMPLE_RANDOM, "random",
NULL);
if(err) if(err)
goto err_out_cleanup_hw; goto err_out_cleanup_hw;
sigio_broken(random_fd, 1);
err = misc_register (&rng_miscdev); err = misc_register (&rng_miscdev);
if (err) { if (err) {
printk (KERN_ERR RNG_MODULE_NAME ": misc device register failed\n"); printk (KERN_ERR RNG_MODULE_NAME ": misc device register failed\n");
...@@ -113,6 +144,7 @@ static int __init rng_init (void) ...@@ -113,6 +144,7 @@ static int __init rng_init (void)
return err; return err;
err_out_cleanup_hw: err_out_cleanup_hw:
os_close_file(random_fd);
random_fd = -1; random_fd = -1;
goto out; goto out;
} }
...@@ -122,6 +154,7 @@ static int __init rng_init (void) ...@@ -122,6 +154,7 @@ static int __init rng_init (void)
*/ */
static void __exit rng_cleanup (void) static void __exit rng_cleanup (void)
{ {
os_close_file(random_fd);
misc_deregister (&rng_miscdev); misc_deregister (&rng_miscdev);
} }
......
...@@ -290,6 +290,7 @@ extern void os_set_ioignore(void); ...@@ -290,6 +290,7 @@ extern void os_set_ioignore(void);
extern int add_sigio_fd(int fd); extern int add_sigio_fd(int fd);
extern int ignore_sigio_fd(int fd); extern int ignore_sigio_fd(int fd);
extern void maybe_sigio_broken(int fd, int read); extern void maybe_sigio_broken(int fd, int read);
extern void sigio_broken(int fd, int read);
/* sys-x86_64/prctl.c */ /* sys-x86_64/prctl.c */
extern int os_arch_prctl(int pid, int code, unsigned long *addr); extern int os_arch_prctl(int pid, int code, unsigned long *addr);
......
/* /*
* Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com) * Copyright (C) 2000 - 2008 Jeff Dike (jdike@{addtoit,linux.intel}.com)
* Licensed under the GPL * Licensed under the GPL
*/ */
...@@ -8,18 +8,10 @@ ...@@ -8,18 +8,10 @@
#include <signal.h> #include <signal.h>
/* Copied from linux/compiler-gcc.h since we can't include it directly */
#define barrier() __asm__ __volatile__("": : :"memory")
extern void sig_handler(int sig, struct sigcontext sc); extern void sig_handler(int sig, struct sigcontext sc);
extern void alarm_handler(int sig, struct sigcontext sc); extern void alarm_handler(int sig, struct sigcontext sc);
#endif #endif
/*
* Overrides for Emacs so that we follow Linus's tabbing style.
* Emacs will notice this stuff at the end of the file and automatically
* adjust the settings for this buffer only. This must remain at the end
* of the file.
* ---------------------------------------------------------------------------
* Local variables:
* c-file-style: "linux"
* End:
*/
/* /*
* Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) * Copyright (C) 2002 - 2008 Jeff Dike (jdike@{addtoit,linux.intel}.com)
* Licensed under the GPL * Licensed under the GPL
*/ */
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "kern_util.h" #include "kern_util.h"
#include "init.h" #include "init.h"
#include "os.h" #include "os.h"
#include "process.h"
#include "sigio.h" #include "sigio.h"
#include "um_malloc.h" #include "um_malloc.h"
#include "user.h" #include "user.h"
...@@ -338,20 +339,10 @@ static void write_sigio_workaround(void) ...@@ -338,20 +339,10 @@ static void write_sigio_workaround(void)
close(l_write_sigio_fds[1]); close(l_write_sigio_fds[1]);
} }
/* Changed during early boot */ void sigio_broken(int fd, int read)
static int pty_output_sigio = 0;
static int pty_close_sigio = 0;
void maybe_sigio_broken(int fd, int read)
{ {
int err; int err;
if (!isatty(fd))
return;
if ((read || pty_output_sigio) && (!read || pty_close_sigio))
return;
write_sigio_workaround(); write_sigio_workaround();
sigio_lock(); sigio_lock();
...@@ -370,6 +361,21 @@ void maybe_sigio_broken(int fd, int read) ...@@ -370,6 +361,21 @@ void maybe_sigio_broken(int fd, int read)
sigio_unlock(); sigio_unlock();
} }
/* Changed during early boot */
static int pty_output_sigio;
static int pty_close_sigio;
void maybe_sigio_broken(int fd, int read)
{
if (!isatty(fd))
return;
if ((read || pty_output_sigio) && (!read || pty_close_sigio))
return;
sigio_broken(fd, read);
}
static void sigio_cleanup(void) static void sigio_cleanup(void)
{ {
if (write_sigio_pid == -1) if (write_sigio_pid == -1)
...@@ -383,7 +389,7 @@ static void sigio_cleanup(void) ...@@ -383,7 +389,7 @@ static void sigio_cleanup(void)
__uml_exitcall(sigio_cleanup); __uml_exitcall(sigio_cleanup);
/* Used as a flag during SIGIO testing early in boot */ /* Used as a flag during SIGIO testing early in boot */
static volatile int got_sigio = 0; static int got_sigio;
static void __init handler(int sig) static void __init handler(int sig)
{ {
...@@ -498,7 +504,8 @@ static void tty_output(int master, int slave) ...@@ -498,7 +504,8 @@ static void tty_output(int master, int slave)
if (errno != EAGAIN) if (errno != EAGAIN)
printk(UM_KERN_ERR "tty_output : write failed, errno = %d\n", printk(UM_KERN_ERR "tty_output : write failed, errno = %d\n",
errno); errno);
while (((n = read(slave, buf, sizeof(buf))) > 0) && !got_sigio) while (((n = read(slave, buf, sizeof(buf))) > 0) &&
!({ barrier(); got_sigio; }))
; ;
if (got_sigio) { if (got_sigio) {
......
...@@ -15,8 +15,9 @@ ...@@ -15,8 +15,9 @@
#define SIGIO_WRITE_IRQ 11 #define SIGIO_WRITE_IRQ 11
#define TELNETD_IRQ 12 #define TELNETD_IRQ 12
#define XTERM_IRQ 13 #define XTERM_IRQ 13
#define RANDOM_IRQ 14
#define LAST_IRQ XTERM_IRQ #define LAST_IRQ RANDOM_IRQ
#define NR_IRQS (LAST_IRQ + 1) #define NR_IRQS (LAST_IRQ + 1)
#endif #endif
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