Skip to contentSkip to conversation

random: convert to using fops->write_iter()

PATCH In review Jens Axboe 3 participants 9 messages May 19, 2022 – May 20, 2022 Random number driver

lore
Share & tools
lore

Jens Axboe

author
#0 lore raw

PATCH 1/1 random: convert to using fops->write_iter()

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()).

---

Jason, this has only been booted. I did verify that it seems to take a write just fine, but I would appreciate if you could vet this one with your testing. Thanks!

drivers/char/random.c +21 -20
index 41ca5966aa4f..3da04068c225 100644
@@ -1283,20 +1283,20 @@ 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 size_t write_pool(struct iov_iter *iter)
 {
 	size_t block_len;
 	int ret = 0;
 	u8 block[BLAKE2S_BLOCK_SIZE];
 
-	while (len) {
-		block_len = min(len, sizeof(block));
-		if (copy_from_user(block, ubuf, block_len)) {
-			ret = -EFAULT;
+	while (iov_iter_count(iter)) {
+		block_len = min(iov_iter_count(iter), sizeof(block));
+		if (!copy_from_iter(block, block_len, iter)) {
+			if (!ret)
+				ret = -EFAULT;
 			goto out;
 		}
-		len -= block_len;
-		ubuf += block_len;
+		ret += block_len;
 		mix_pool_bytes(block, block_len);
 		cond_resched();
 	}
@@ -1306,16 +1306,9 @@ static int write_pool(const char __user *ubuf, size_t len)
 	return ret;
 }
 
-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)
@@ -1373,7 +1366,10 @@ 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 iter;
+		struct iovec iov;
+
 		if (!capable(CAP_SYS_ADMIN))
 			return -EPERM;
 		if (get_user(ent_count, p++))
@@ -1382,11 +1378,16 @@ static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
 			return -EINVAL;
 		if (get_user(size, p++))
 			return -EFAULT;
-		retval = write_pool((const char __user *)p, size);
+
+		iov.iov_base = p;
+		iov.iov_len = size;
+		iov_iter_init(&iter, WRITE, &iov, 1, size);
+		retval = write_pool(&iter);
 		if (retval < 0)
 			return retval;
 		credit_init_bits(ent_count);
 		return 0;
+		}
 	case RNDZAPENTCNT:
 	case RNDCLEARPOOL:
 		/* No longer has any effect. */
@@ -1412,7 +1413,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,
@@ -1423,7 +1424,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,
Signature · 1 line
Jens Axboe
author
#1 lore raw
Quoted 3 lines · Jens Axboe

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()).

Oh, and similarly to the other one, you can then add ->splice_write as well with using iter_file_splice_write for that. If you want to add that, please do.

Signature · 1 line
Jens Axboe
#2 lore raw
In reply to Jens Axboe

Hi Jens,

drivers/char/random.c static __poll_t random_poll(struct file *file, poll_table *wait) · line 1289

Quoted 5 lines · Jens Axboe
drivers/char/random.c +1 -1
-static int write_pool(const char __user *ubuf, size_t len)
+static size_t write_pool(struct iov_iter *iter)
 {
 	size_t block_len;
 	int ret = 0;

Changing the return value to size_t isn't quite right, as this can return -EFAULT. So at the very minimum, it should return a ssize_t.

Jason

author
#3 lore raw
Quoted 11 lines · Jason A. Donenfeld

Hi Jens,

drivers/char/random.c static __poll_t random_poll(struct file *file, poll_table *wait) · line 1289

Quoted 5 lines · Jens Axboe
drivers/char/random.c +1 -1
-static int write_pool(const char __user *ubuf, size_t len)
+static size_t write_pool(struct iov_iter *iter)
 {
 	size_t block_len;
 	int ret = 0;

Changing the return value to size_t isn't quite right, as this can return -EFAULT. So at the very minimum, it should return a ssize_t.

Good catch, yes let's make that a ssize_t instead. Can you do that while testing/applying?

Signature · 1 line
Jens Axboe
#4 lore raw
In reply to Jens Axboe

Hi Jens,

drivers/char/random.c static __poll_t random_poll(struct file *file, poll_table *wait) · line 1293

Quoted 2 lines · Jens Axboe
drivers/char/random.c +2 -0
+	while (iov_iter_count(iter)) {
+		block_len = min(iov_iter_count(iter), sizeof(block));

Whereas get_random_bytes_user() used a separate len param, it looks like this function is using iov's len. Maybe that's not such a bad idea? I'll give a shot at fixing that up in both places, and you can tell me what you think.

Jason

author
#5 lore raw
Quoted 10 lines · Jason A. Donenfeld

Hi Jens,

drivers/char/random.c static __poll_t random_poll(struct file *file, poll_table *wait) · line 1293

Quoted 2 lines · Jens Axboe
drivers/char/random.c +2 -0
+	while (iov_iter_count(iter)) {
+		block_len = min(iov_iter_count(iter), sizeof(block));

Whereas get_random_bytes_user() used a separate len param, it looks like this function is using iov's len. Maybe that's not such a bad idea? I'll give a shot at fixing that up in both places, and you can tell me what you think.

Yes that'd be fine, it's just reading iter->count anyway. Agree that it'd be nicer to just kill the separate 'len' as the copy advances the iter anyway.

Signature · 1 line
Jens Axboe
#6 lore raw
In reply to Jens Axboe
Quoted 7 lines · Jens Axboe
drivers/char/random.c +1 -2
-static int write_pool(const char __user *ubuf, size_t len)
+static size_t write_pool(struct iov_iter *iter)
 {
 	size_t block_len;
 	int ret = 0;
 
-	while (len) {

drivers/char/random.c static __poll_t random_poll(struct file *file, poll_table *wait) · line 1298

Quoted 10 lines
drivers/char/random.c +5 -3
-		block_len = min(len, sizeof(block));
-		if (copy_from_user(block, ubuf, block_len)) {
-			ret = -EFAULT;
+	while (iov_iter_count(iter)) {
+		block_len = min(iov_iter_count(iter), sizeof(block));
+		if (!copy_from_iter(block, block_len, iter)) {
+			if (!ret)
+				ret = -EFAULT;
 			goto out;
 		}

Feed it a buffer with only 1 byte mapped, watch it'll pass to mix_pool_bytes(). And see how much of 'block' has been used uninitialized...

And why bother with that min thing, anyway?

	ssize_t ret = 0;

	while (iov_iter_count(iter)) {
	  	u8 block[BLAKE2S_BLOCK_SIZE];
		size_t copied = copy_from_iter(block, sizeof(block), iter);
		if (!copied) {
			if (!ret)
				ret = -EFAULT;
			break;
		}
		mix_pool_bytes(block, copied);
		ret += copied;
	}
	return ret;

and be done with that...

drivers/char/random.c static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg) · line 1384

Quoted 9 lines
drivers/char/random.c +4 -1
@@ -1382,11 +1378,16 @@ static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
 			return -EINVAL;
 		if (get_user(size, p++))
 			return -EFAULT;
-		retval = write_pool((const char __user *)p, size);
+
+		iov.iov_base = p;
+		iov.iov_len = size;
+		iov_iter_init(&iter, WRITE, &iov, 1, size);

That'd be

		import_single_range(WRITE, p, size, &iov, &iter);
author
#7 lore raw
Quoted 8 lines · Al Viro
Quoted 6 lines · Jens Axboe
drivers/char/random.c +1 -1
-static int write_pool(const char __user *ubuf, size_t len)
+static size_t write_pool(struct iov_iter *iter)
 {
 	size_t block_len;
 	int ret = 0;
Quoted 14 lines

drivers/char/random.c static __poll_t random_poll(struct file *file, poll_table *wait) · line 1298

Quoted 11 lines
drivers/char/random.c +5 -4
-	while (len) {
-		block_len = min(len, sizeof(block));
-		if (copy_from_user(block, ubuf, block_len)) {
-			ret = -EFAULT;
+	while (iov_iter_count(iter)) {
+		block_len = min(iov_iter_count(iter), sizeof(block));
+		if (!copy_from_iter(block, block_len, iter)) {
+			if (!ret)
+				ret = -EFAULT;
 			goto out;
 		}

Feed it a buffer with only 1 byte mapped, watch it'll pass to mix_pool_bytes(). And see how much of 'block' has been used uninitialized...

I don't follow? Buffer with 1 byte, iter setup with 1 byte. We copy 1 byte, and we pass 1 byte to mix_pool_bytes(). What am I missing?

Quoted 17 lines

And why bother with that min thing, anyway?

	ssize_t ret = 0;

	while (iov_iter_count(iter)) {
	  	u8 block[BLAKE2S_BLOCK_SIZE];
		size_t copied = copy_from_iter(block, sizeof(block), iter);
		if (!copied) {
			if (!ret)
				ret = -EFAULT;
			break;
		}
		mix_pool_bytes(block, copied);
		ret += copied;
	}
	return ret;
Quoted 1 line

and be done with that...

Agree, that does look better, the min() part could've been killed with the conversion indeed.

Quoted 11 lines

drivers/char/random.c static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg) · line 1384

Quoted 9 lines
drivers/char/random.c +4 -1
@@ -1382,11 +1378,16 @@ static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
 			return -EINVAL;
 		if (get_user(size, p++))
 			return -EFAULT;
-		retval = write_pool((const char __user *)p, size);
+
+		iov.iov_base = p;
+		iov.iov_len = size;
+		iov_iter_init(&iter, WRITE, &iov, 1, size);

That'd be

Quoted 1 line
		import_single_range(WRITE, p, size, &iov, &iter);

Yep that'd be a simpler equivalent.

Signature · 1 line
Jens Axboe
#8 lore raw
Quoted 5 lines · Jens Axboe
Quoted 2 lines

Feed it a buffer with only 1 byte mapped, watch it'll pass to mix_pool_bytes(). And see how much of 'block' has been used uninitialized...

I don't follow? Buffer with 1 byte, iter setup with 1 byte. We copy 1 byte, and we pass 1 byte to mix_pool_bytes(). What am I missing?

"only 1 byte mapped" != "len is 1"... Anonymous mmap() of 2*PAGE_SIZE, munmap() the second half, ask that sucker to write PAGE_SIZE from buffer + PAGE_SIZE - 1...