From: "Jason A. Donenfeld" <Jason@zx2c4.com>
To: Jens Axboe <axboe@kernel.dk>, Theodore Ts'o <tytso@mit.edu>,
Christoph Hellwig <hch@lst.de>,
LKML <linux-kernel@vger.kernel.org>,
Al Viro <viro@zeniv.linux.org.uk>
Cc: "Jason A . Donenfeld" <Jason@zx2c4.com>
Subject: [PATCH v4 2/3] random: convert to using fops->write_iter()
Date: Fri, 20 May 2022 11:44:58 +0200 [thread overview]
Message-ID: <20220520094459.116240-3-Jason@zx2c4.com> (raw)
In-Reply-To: <20220520094459.116240-1-Jason@zx2c4.com>
From: Jens Axboe <axboe@kernel.dk>
Now that the read side has been converted to fix a regression with
splice, convert the write side as well to have some symmetry in the
interface used (and help deprecate ->write()).
Signed-off-by: Jens Axboe <axboe@kernel.dk>
[Jason: cleaned up random_ioctl a bit, require full writes in
RNDADDENTROPY since it's crediting entropy, simplify control flow of
write_pool(), and incorporate suggestions from Al.]
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
drivers/char/random.c | 67 ++++++++++++++++++++++---------------------
1 file changed, 35 insertions(+), 32 deletions(-)
diff --git a/drivers/char/random.c b/drivers/char/random.c
index f8cd747acec2..831cafcd1034 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -1274,39 +1274,31 @@ static __poll_t random_poll(struct file *file, poll_table *wait)
return crng_ready() ? EPOLLIN | EPOLLRDNORM : EPOLLOUT | EPOLLWRNORM;
}
-static int write_pool(const char __user *ubuf, size_t len)
+static ssize_t write_pool(struct iov_iter *from)
{
- size_t block_len;
- int ret = 0;
u8 block[BLAKE2S_BLOCK_SIZE];
+ ssize_t ret = 0;
+ size_t copied;
- while (len) {
- block_len = min(len, sizeof(block));
- if (copy_from_user(block, ubuf, block_len)) {
- ret = -EFAULT;
- goto out;
- }
- len -= block_len;
- ubuf += block_len;
- mix_pool_bytes(block, block_len);
+ if (!iov_iter_count(from))
+ return 0;
+
+ for (;;) {
+ copied = copy_from_iter(block, sizeof(block), from);
+ ret += copied;
+ mix_pool_bytes(block, copied);
+ if (!iov_iter_count(from) || copied != sizeof(block))
+ break;
cond_resched();
}
-out:
memzero_explicit(block, sizeof(block));
- return ret;
+ return ret ? ret : -EFAULT;
}
-static ssize_t random_write(struct file *file, const char __user *ubuf,
- size_t len, loff_t *ppos)
+static ssize_t random_write_iter(struct kiocb *kiocb, struct iov_iter *from)
{
- int ret;
-
- ret = write_pool(ubuf, len);
- if (ret)
- return ret;
-
- return (ssize_t)len;
+ return write_pool(from);
}
static ssize_t urandom_read_iter(struct kiocb *kiocb, struct iov_iter *to)
@@ -1345,9 +1337,8 @@ static ssize_t random_read_iter(struct kiocb *kiocb, struct iov_iter *to)
static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
{
- int size, ent_count;
int __user *p = (int __user *)arg;
- int retval;
+ int ent_count;
switch (cmd) {
case RNDGETENTCNT:
@@ -1364,20 +1355,32 @@ static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
return -EINVAL;
credit_init_bits(ent_count);
return 0;
- case RNDADDENTROPY:
+ case RNDADDENTROPY: {
+ struct iov_iter from;
+ struct iovec iov;
+ ssize_t ret;
+ int len;
+
if (!capable(CAP_SYS_ADMIN))
return -EPERM;
if (get_user(ent_count, p++))
return -EFAULT;
if (ent_count < 0)
return -EINVAL;
- if (get_user(size, p++))
+ if (get_user(len, p++))
+ return -EFAULT;
+
+ ret = import_single_range(WRITE, p, len, &iov, &from);
+ if (unlikely(ret))
+ return ret;
+ ret = write_pool(&from);
+ if (unlikely(ret < 0))
+ return ret;
+ if (unlikely(ret != len))
return -EFAULT;
- retval = write_pool((const char __user *)p, size);
- if (retval < 0)
- return retval;
credit_init_bits(ent_count);
return 0;
+ }
case RNDZAPENTCNT:
case RNDCLEARPOOL:
/* No longer has any effect. */
@@ -1403,7 +1406,7 @@ static int random_fasync(int fd, struct file *filp, int on)
const struct file_operations random_fops = {
.read_iter = random_read_iter,
- .write = random_write,
+ .write_iter = random_write_iter,
.poll = random_poll,
.unlocked_ioctl = random_ioctl,
.compat_ioctl = compat_ptr_ioctl,
@@ -1413,7 +1416,7 @@ const struct file_operations random_fops = {
const struct file_operations urandom_fops = {
.read_iter = urandom_read_iter,
- .write = random_write,
+ .write_iter = random_write_iter,
.unlocked_ioctl = random_ioctl,
.compat_ioctl = compat_ptr_ioctl,
.fasync = random_fasync,
--
2.35.1
next prev parent reply other threads:[~2022-05-20 9:45 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-20 9:44 [PATCH v4 0/3] random: convert to using iters, for Al Viro Jason A. Donenfeld
2022-05-20 9:44 ` [PATCH v4 1/3] random: convert to using fops->read_iter() Jason A. Donenfeld
2022-05-20 13:37 ` Jason A. Donenfeld
2022-05-20 14:36 ` Jens Axboe
2022-05-20 14:39 ` Jason A. Donenfeld
2022-05-20 15:12 ` Al Viro
2022-05-20 9:44 ` Jason A. Donenfeld [this message]
2022-05-20 9:44 ` [PATCH v4 3/3] random: wire up fops->splice_{read,write}_iter() Jason A. Donenfeld
2022-05-20 12:16 ` [PATCH v4 0/3] random: convert to using iters, for Al Viro Jens Axboe
2022-05-20 15:25 ` Jason A. Donenfeld
2022-05-20 15:34 ` Jens Axboe
2022-05-20 15:39 ` Jason A. Donenfeld
2022-05-20 15:44 ` Jens Axboe
2022-05-20 15:55 ` Jason A. Donenfeld
2022-05-20 15:58 ` Jens Axboe
2022-05-20 16:03 ` Jason A. Donenfeld
2022-05-20 16:06 ` Jens Axboe
2022-05-20 15:46 ` Jason A. Donenfeld
2022-05-20 15:51 ` Jens Axboe
2022-05-20 15:58 ` Jason A. Donenfeld
2022-05-20 16:00 ` Jens Axboe
2022-05-20 15:47 ` Al Viro
2022-05-20 15:53 ` Jens Axboe
2022-05-20 16:15 ` Al Viro
2022-05-20 16:24 ` Jens Axboe
2022-05-20 16:39 ` Jason A. Donenfeld
2022-05-20 16:41 ` Jens Axboe
2022-05-24 4:52 ` Eric W. Biederman
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=20220520094459.116240-3-Jason@zx2c4.com \
--to=jason@zx2c4.com \
--cc=axboe@kernel.dk \
--cc=hch@lst.de \
--cc=linux-kernel@vger.kernel.org \
--cc=tytso@mit.edu \
--cc=viro@zeniv.linux.org.uk \
/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