diff options
| author | Conrad Meyer <cem@FreeBSD.org> | 2018-03-16 18:50:26 +0000 |
|---|---|---|
| committer | Conrad Meyer <cem@FreeBSD.org> | 2018-03-16 18:50:26 +0000 |
| commit | db488e4f52a8644bf5d0f6727367b08e18b729ee (patch) | |
| tree | e1fd2167ea35479e53b4c22a886cf792551270ea /sys/dev/random/randomdev.c | |
| parent | 3fcd5d6e9248659ddf7c8077cb974f77d74482b3 (diff) | |
Notes
Diffstat (limited to 'sys/dev/random/randomdev.c')
| -rw-r--r-- | sys/dev/random/randomdev.c | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/sys/dev/random/randomdev.c b/sys/dev/random/randomdev.c index 28a191f8f7b6..51c25ba408a3 100644 --- a/sys/dev/random/randomdev.c +++ b/sys/dev/random/randomdev.c @@ -130,6 +130,12 @@ READ_RANDOM_UIO(struct uio *uio, bool nonblock) uint8_t *random_buf; int error, spamcount; ssize_t read_len, total_read, c; + /* 16 MiB takes about 0.08 s CPU time on my 2017 AMD Zen CPU */ +#define SIGCHK_PERIOD (16 * 1024 * 1024) + const size_t sigchk_period = SIGCHK_PERIOD; + + CTASSERT(SIGCHK_PERIOD % PAGE_SIZE == 0); +#undef SIGCHK_PERIOD random_buf = malloc(PAGE_SIZE, M_ENTROPY, M_WAITOK); p_random_alg_context->ra_pre_read(); @@ -167,11 +173,22 @@ READ_RANDOM_UIO(struct uio *uio, bool nonblock) read_len = MIN(read_len, PAGE_SIZE); p_random_alg_context->ra_read(random_buf, read_len); c = MIN(uio->uio_resid, read_len); + /* + * uiomove() may yield the CPU before each 'c' bytes + * (up to PAGE_SIZE) are copied out. + */ error = uiomove(random_buf, c, uio); total_read += c; + /* + * Poll for signals every few MBs to avoid very long + * uninterruptible syscalls. + */ + if (error == 0 && uio->uio_resid != 0 && + total_read % sigchk_period == 0) + error = tsleep_sbt(&random_alg_context, PCATCH, + "randrd", SBT_1NS, 0, C_HARDCLOCK); } - if (total_read != uio->uio_resid && (error == ERESTART || error == EINTR)) - /* Return partial read, not error. */ + if (error == ERESTART || error == EINTR) error = 0; } free(random_buf, M_ENTROPY); |
