From: "Jason A. Donenfeld" <Jason@zx2c4.com>
To: Jens Axboe <axboe@kernel.dk>, LKML <linux-kernel@vger.kernel.org>
Cc: "Jason A . Donenfeld" <Jason@zx2c4.com>
Subject: [PATCH v3 1/3] random: convert to using fops->read_iter()
Date: Fri, 20 May 2022 02:40:56 +0200 [thread overview]
Message-ID: <20220520004058.96691-2-Jason@zx2c4.com> (raw)
In-Reply-To: <20220520004058.96691-1-Jason@zx2c4.com>
From: Jens Axboe <axboe@kernel.dk>
This is a pre-requisite to writing up splice() again for the random
and urandom drivers.
Signed-off-by: Jens Axboe <axboe@kernel.dk>
[Jason: simplify control flow a bit in get_random_bytes_user().]
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
drivers/char/random.c | 49 ++++++++++++++++++++-----------------------
1 file changed, 23 insertions(+), 26 deletions(-)
diff --git a/drivers/char/random.c b/drivers/char/random.c
index 0958fa91a964..2b2c3681f172 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -397,13 +397,13 @@ void get_random_bytes(void *buf, size_t len)
}
EXPORT_SYMBOL(get_random_bytes);
-static ssize_t get_random_bytes_user(void __user *ubuf, size_t len)
+static ssize_t get_random_bytes_user(struct iov_iter *to)
{
- size_t block_len, left, ret = 0;
u32 chacha_state[CHACHA_STATE_WORDS];
u8 output[CHACHA_BLOCK_SIZE];
+ size_t ret = 0;
- if (!len)
+ if (!iov_iter_count(to))
return 0;
/*
@@ -417,27 +417,21 @@ static ssize_t get_random_bytes_user(void __user *ubuf, size_t len)
* use chacha_state after, so we can simply return those bytes to
* the user directly.
*/
- if (len <= CHACHA_KEY_SIZE) {
- ret = len - copy_to_user(ubuf, &chacha_state[4], len);
+ if (iov_iter_count(to) <= CHACHA_KEY_SIZE) {
+ ret = copy_to_iter(&chacha_state[4], iov_iter_count(to), to);
goto out_zero_chacha;
}
for (;;) {
+ size_t copied, block_len = min_t(size_t, iov_iter_count(to), CHACHA_BLOCK_SIZE);
+
chacha20_block(chacha_state, output);
if (unlikely(chacha_state[12] == 0))
++chacha_state[13];
- block_len = min_t(size_t, len, CHACHA_BLOCK_SIZE);
- left = copy_to_user(ubuf, output, block_len);
- if (left) {
- ret += block_len - left;
- break;
- }
-
- ubuf += block_len;
- ret += block_len;
- len -= block_len;
- if (!len)
+ copied = copy_to_iter(output, block_len, to);
+ ret += copied;
+ if (!iov_iter_count(to) || copied != block_len)
break;
BUILD_BUG_ON(PAGE_SIZE % CHACHA_BLOCK_SIZE != 0);
@@ -1248,6 +1242,9 @@ static void __cold try_to_generate_entropy(void)
SYSCALL_DEFINE3(getrandom, char __user *, ubuf, size_t, len, unsigned int, flags)
{
+ struct iovec iov = { .iov_base = ubuf };
+ struct iov_iter iter;
+
if (flags & ~(GRND_NONBLOCK | GRND_RANDOM | GRND_INSECURE))
return -EINVAL;
@@ -1270,7 +1267,9 @@ SYSCALL_DEFINE3(getrandom, char __user *, ubuf, size_t, len, unsigned int, flags
if (unlikely(ret))
return ret;
}
- return get_random_bytes_user(ubuf, len);
+ iov.iov_len = len;
+ iov_iter_init(&iter, READ, &iov, 1, len);
+ return get_random_bytes_user(&iter);
}
static __poll_t random_poll(struct file *file, poll_table *wait)
@@ -1314,8 +1313,7 @@ static ssize_t random_write(struct file *file, const char __user *ubuf,
return (ssize_t)len;
}
-static ssize_t urandom_read(struct file *file, char __user *ubuf,
- size_t len, loff_t *ppos)
+static ssize_t urandom_read_iter(struct kiocb *kiocb, struct iov_iter *to)
{
static int maxwarn = 10;
@@ -1332,22 +1330,21 @@ static ssize_t urandom_read(struct file *file, char __user *ubuf,
else if (ratelimit_disable || __ratelimit(&urandom_warning)) {
--maxwarn;
pr_notice("%s: uninitialized urandom read (%zd bytes read)\n",
- current->comm, len);
+ current->comm, iov_iter_count(to));
}
}
- return get_random_bytes_user(ubuf, len);
+ return get_random_bytes_user(to);
}
-static ssize_t random_read(struct file *file, char __user *ubuf,
- size_t len, loff_t *ppos)
+static ssize_t random_read_iter(struct kiocb *kiocb, struct iov_iter *to)
{
int ret;
ret = wait_for_random_bytes();
if (ret != 0)
return ret;
- return get_random_bytes_user(ubuf, len);
+ return get_random_bytes_user(to);
}
static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
@@ -1409,7 +1406,7 @@ static int random_fasync(int fd, struct file *filp, int on)
}
const struct file_operations random_fops = {
- .read = random_read,
+ .read_iter = random_read_iter,
.write = random_write,
.poll = random_poll,
.unlocked_ioctl = random_ioctl,
@@ -1419,7 +1416,7 @@ const struct file_operations random_fops = {
};
const struct file_operations urandom_fops = {
- .read = urandom_read,
+ .read_iter = urandom_read_iter,
.write = random_write,
.unlocked_ioctl = random_ioctl,
.compat_ioctl = compat_ptr_ioctl,
--
2.35.1
next prev parent reply other threads:[~2022-05-20 0:41 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-20 0:40 [PATCH v3 0/3] random: Jens' {read,write}_iter respin Jason A. Donenfeld
2022-05-20 0:40 ` Jason A. Donenfeld [this message]
2022-05-20 1:11 ` [PATCH v3 1/3] random: convert to using fops->read_iter() Jens Axboe
2022-05-20 9:04 ` Jason A. Donenfeld
2022-05-20 0:40 ` [PATCH v3 2/3] random: convert to using fops->write_iter() Jason A. Donenfeld
2022-05-20 0:40 ` [PATCH v3 3/3] random: wire up fops->splice_{read,write}_iter() Jason A. Donenfeld
2022-05-20 1:11 ` [PATCH v3 0/3] random: Jens' {read,write}_iter respin Jens Axboe
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220520004058.96691-2-Jason@zx2c4.com \
--to=jason@zx2c4.com \
--cc=axboe@kernel.dk \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®