Skip to contentSkip to conversation

/dev/zero: also implement ->read

PATCH Superseded Christoph Hellwig 6 participants 17 messages Sep 3, 2020 – Sep 7, 2020 Char and misc drivers

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 /dev/zero: also implement ->read

Christophe reported a major speedup due to avoiding the iov_iter overhead, so just add this trivial function. Note that /dev/zero already implements both an iter and non-iter writes so this just makes it more symmetric.

Christophe Leroy <[email protected]>

---
 drivers/char/mem.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
drivers/char/mem.c +22 -0
index abd4ffdc8cdebc..1dc99ab158457a 100644
@@ -726,6 +726,27 @@ static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter)
 	return written;
 }
 
+static ssize_t read_zero(struct file *file, char __user *buf,
+			 size_t count, loff_t *ppos)
+{
+	size_t cleared = 0;
+
+	while (count) {
+		size_t chunk = min_t(size_t, count, PAGE_SIZE);
+
+		if (clear_user(buf + cleared, chunk))
+			return cleared ? cleared : -EFAULT;
+		cleared += chunk;
+		count -= chunk;
+
+		if (signal_pending(current))
+			return cleared ? cleared : -ERESTARTSYS;
+		cond_resched();
+	}
+
+	return cleared;
+}
+
 static int mmap_zero(struct file *file, struct vm_area_struct *vma)
 {
 #ifndef CONFIG_MMU
@@ -921,6 +942,7 @@ static const struct file_operations zero_fops = {
 	.llseek		= zero_lseek,
 	.write		= write_zero,
 	.read_iter	= read_iter_zero,
+	.read		= read_zero,
 	.write_iter	= write_iter_zero,
 	.mmap		= mmap_zero,
 	.get_unmapped_area = get_unmapped_area_zero,
Signature · 1 line
2.28.0
Tested-by on v1 1/1
#1 lore raw

Le 03/09/2020 à 17:59, Christoph Hellwig a écrit :

Quoted 7 lines

Christophe reported a major speedup due to avoiding the iov_iter overhead, so just add this trivial function. Note that /dev/zero already implements both an iter and non-iter writes so this just makes it more symmetric.

Christophe Leroy <[email protected]>

Quoted 45 lines
---
  drivers/char/mem.c | 22 ++++++++++++++++++++++
  1 file changed, 22 insertions(+)
drivers/char/mem.c +22 -0
index abd4ffdc8cdebc..1dc99ab158457a 100644
@@ -726,6 +726,27 @@ static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter)
  	return written;
  }
 
+static ssize_t read_zero(struct file *file, char __user *buf,
+			 size_t count, loff_t *ppos)
+{
+	size_t cleared = 0;
+
+	while (count) {
+		size_t chunk = min_t(size_t, count, PAGE_SIZE);
+
+		if (clear_user(buf + cleared, chunk))
+			return cleared ? cleared : -EFAULT;
+		cleared += chunk;
+		count -= chunk;
+
+		if (signal_pending(current))
+			return cleared ? cleared : -ERESTARTSYS;
+		cond_resched();
+	}
+
+	return cleared;
+}
+
  static int mmap_zero(struct file *file, struct vm_area_struct *vma)
  {
  #ifndef CONFIG_MMU
@@ -921,6 +942,7 @@ static const struct file_operations zero_fops = {
  	.llseek		= zero_lseek,
  	.write		= write_zero,
  	.read_iter	= read_iter_zero,
+	.read		= read_zero,
  	.write_iter	= write_iter_zero,
  	.mmap		= mmap_zero,
  	.get_unmapped_area = get_unmapped_area_zero,
#2 lore raw

From: Christophe Leroy

Quoted 8 lines

Le 03/09/2020 à 17:59, Christoph Hellwig a écrit :

Quoted 5 lines

Christophe reported a major speedup due to avoiding the iov_iter overhead, so just add this trivial function. Note that /dev/zero already implements both an iter and non-iter writes so this just makes it more symmetric.

Quoted 4 lines
Quoted 2 lines

Christophe Leroy <[email protected]>

Any idea what has happened to make the 'iter' version so bad?

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#3 lore raw

Hi!

Quoted 11 lines
Quoted 9 lines
Quoted 7 lines

Christophe reported a major speedup due to avoiding the iov_iter overhead, so just add this trivial function. Note that /dev/zero already implements both an iter and non-iter writes so this just makes it more symmetric.

Christophe Leroy <[email protected]>

Any idea what has happened to make the 'iter' version so bad?

Exactly. Also it would be nice to note how the speedup was measured and what the speedup is.

Best regads,
Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#4 lore raw

Hi,

Le 06/09/2020 à 20:21, Pavel Machek a écrit :

Quoted 10 lines

Hi!

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

Christophe reported a major speedup due to avoiding the iov_iter overhead, so just add this trivial function. Note that /dev/zero already implements both an iter and non-iter writes so this just makes it more symmetric.

Christophe Leroy <[email protected]>

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Any idea what has happened to make the 'iter' version so bad?

Exactly. Also it would be nice to note how the speedup was measured and what the speedup is.

Was measured on an 8xx powerpc running at 132MHz with:

dd if=/dev/zero of=/dev/null count=1M

With the patch, dd displays a throughput of 113.5MB/s Without the patch it is 99.9MB/s

Christophe

#5 lore raw
Quoted 21 lines · Christophe Leroy

Hi,

Le 06/09/2020 à 20:21, Pavel Machek a écrit :

Quoted 10 lines

Hi!

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

Christophe reported a major speedup due to avoiding the iov_iter overhead, so just add this trivial function. Note that /dev/zero already implements both an iter and non-iter writes so this just makes it more symmetric.

Christophe Leroy <[email protected]>

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Any idea what has happened to make the 'iter' version so bad?

Exactly. Also it would be nice to note how the speedup was measured and what the speedup is.

Quoted 6 lines

Was measured on an 8xx powerpc running at 132MHz with:

dd if=/dev/zero of=/dev/null count=1M

With the patch, dd displays a throughput of 113.5MB/s Without the patch it is 99.9MB/s

Actually... that does not seem like a huge deal. read(/dev/zero) is not that common operation.

Are you getting similar speedups on normal hardware?

Pavel

Signature · 2 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
#6 lore raw

Le 06/09/2020 à 20:38, Pavel Machek a écrit :

Quoted 16 lines · Christophe Leroy
Quoted 15 lines

Hi,

Le 06/09/2020 à 20:21, Pavel Machek a écrit :

Quoted 12 lines

Hi!

Quoted 10 lines
Quoted 9 lines
Quoted 7 lines

Christophe reported a major speedup due to avoiding the iov_iter overhead, so just add this trivial function. Note that /dev/zero already implements both an iter and non-iter writes so this just makes it more symmetric.

Christophe Leroy <[email protected]>

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Any idea what has happened to make the 'iter' version so bad?

Exactly. Also it would be nice to note how the speedup was measured and what the speedup is.

Was measured on an 8xx powerpc running at 132MHz with:

Oops. That was not on an 8xx but on an 8321 running at 333MHz, sorry.

Quoted 8 lines
Quoted 5 lines

dd if=/dev/zero of=/dev/null count=1M

With the patch, dd displays a throughput of 113.5MB/s Without the patch it is 99.9MB/s

Actually... that does not seem like a huge deal. read(/dev/zero) is not that common operation.

That's 14% more. It is not negligeable.

I think I need to measure the /dev/zero read standalone. I guess the write to /dev/null flatters the result.

Quoted 3 lines

Are you getting similar speedups on normal hardware?

Do you regard powerpc embedded devices as abnormal ? AFAIK the 832x is embedded in millions of ADSL boxes. What processor do you have in mind as normal hardware ?

Christophe

#7 lore raw
In reply to Pavel Machek
Quoted 24 lines · Pavel Machek
Quoted 23 lines · Christophe Leroy

Hi,

Le 06/09/2020 à 20:21, Pavel Machek a écrit :

Quoted 10 lines

Hi!

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

Christophe reported a major speedup due to avoiding the iov_iter overhead, so just add this trivial function. Note that /dev/zero already implements both an iter and non-iter writes so this just makes it more symmetric.

Christophe Leroy <[email protected]>

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Any idea what has happened to make the 'iter' version so bad?

Exactly. Also it would be nice to note how the speedup was measured and what the speedup is.

Was measured on an 8xx powerpc running at 132MHz with:

Quoted 7 lines
Quoted 4 lines

dd if=/dev/zero of=/dev/null count=1M

With the patch, dd displays a throughput of 113.5MB/s Without the patch it is 99.9MB/s

Actually... that does not seem like a huge deal. read(/dev/zero) is not that common operation.

There is nothing wrong with this patch (aside from the sparse warning), and it's in my tree now, so I don't understand complaining about it...

greg k-h

#8 lore raw
In reply to Christophe Leroy

From: Christophe Leroy

Quoted 22 lines

Sent: 06 September 2020 19:36
Hi,

Le 06/09/2020 à 20:21, Pavel Machek a écrit :

Quoted 10 lines

Hi!

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

Christophe reported a major speedup due to avoiding the iov_iter overhead, so just add this trivial function. Note that /dev/zero already implements both an iter and non-iter writes so this just makes it more symmetric.

Christophe Leroy <[email protected]>

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Any idea what has happened to make the 'iter' version so bad?

Exactly. Also it would be nice to note how the speedup was measured and what the speedup is.

Quoted 6 lines

Was measured on an 8xx powerpc running at 132MHz with:

dd if=/dev/zero of=/dev/null count=1M

With the patch, dd displays a throughput of 113.5MB/s Without the patch it is 99.9MB/s

That in itself isn't a problem.
What was the throughput before any of these patches?

I just remember another thread about the same test running a lot slower after one of the related changes. While this speeds up read /dev/zero (which is uncommon) if this is needed to get near the old performance then the changes to the 'iter' code will affect real workloads.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#9 lore raw

Le 06/09/2020 à 22:52, David Laight a écrit :

Quoted 33 lines

From: Christophe Leroy

Quoted 22 lines

Sent: 06 September 2020 19:36
Hi,

Le 06/09/2020 à 20:21, Pavel Machek a écrit :

Quoted 10 lines

Hi!

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

Christophe reported a major speedup due to avoiding the iov_iter overhead, so just add this trivial function. Note that /dev/zero already implements both an iter and non-iter writes so this just makes it more symmetric.

Christophe Leroy <[email protected]>

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Any idea what has happened to make the 'iter' version so bad?

Exactly. Also it would be nice to note how the speedup was measured and what the speedup is.

Quoted 6 lines

Was measured on an 8xx powerpc running at 132MHz with:

dd if=/dev/zero of=/dev/null count=1M

With the patch, dd displays a throughput of 113.5MB/s Without the patch it is 99.9MB/s

That in itself isn't a problem.
What was the throughput before any of these patches?

Quoted 6 lines

I just remember another thread about the same test running a lot slower after one of the related changes.

While this speeds up read /dev/zero (which is uncommon) if this is needed to get near the old performance then the changes to the 'iter' code will affect real workloads.

If you are talking about the tests around the set_fs series from Christoph, I identified that the degradation was linked to CONFIG_STACKPROTECTOR_STRONG being selected by default, which provides unreliable results from one patch to another, GCC doing some strange things linked to unrelated changes.

With CONFIG_STACKPROTECTOR set to N, I get stable performance and no degradation with any of the patches of the set_fs series.

Christophe

#10 lore raw

From: Christophe Leroy

Quoted 1 line

Sent: 07 September 2020 05:44

...

Quoted 8 lines

If you are talking about the tests around the set_fs series from Christoph, I identified that the degradation was linked to CONFIG_STACKPROTECTOR_STRONG being selected by default, which provides unreliable results from one patch to another, GCC doing some strange things linked to unrelated changes.

With CONFIG_STACKPROTECTOR set to N, I get stable performance and no degradation with any of the patches of the set_fs series.

Ah I didn't spot that response going through.

Sounds like that option should be disabled if it causes that much of a performance drop.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#11 lore raw
In reply to Christoph Hellwig

Le 03/09/2020 à 17:59, Christoph Hellwig a écrit :

Quoted 44 lines

Christophe reported a major speedup due to avoiding the iov_iter overhead, so just add this trivial function. Note that /dev/zero already implements both an iter and non-iter writes so this just makes it more symmetric.

Christophe Leroy <[email protected]>

---
  drivers/char/mem.c | 22 ++++++++++++++++++++++
  1 file changed, 22 insertions(+)
drivers/char/mem.c +21 -0
index abd4ffdc8cdebc..1dc99ab158457a 100644
@@ -726,6 +726,27 @@ static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter)
  	return written;
  }
 
+static ssize_t read_zero(struct file *file, char __user *buf,
+			 size_t count, loff_t *ppos)
+{
+	size_t cleared = 0;
+
+	while (count) {
+		size_t chunk = min_t(size_t, count, PAGE_SIZE);
+
+		if (clear_user(buf + cleared, chunk))
+			return cleared ? cleared : -EFAULT;
+		cleared += chunk;
+		count -= chunk;
+
+		if (signal_pending(current))
+			return cleared ? cleared : -ERESTARTSYS;
+		cond_resched();
+	}
+
+	return cleared;
+}
+
  static int mmap_zero(struct file *file, struct vm_area_struct *vma)
  {
  #ifndef CONFIG_MMU

drivers/char/mem.c static const struct file_operations zero_fops = { · line 945

Quoted 4 lines
drivers/char/mem.c +1 -0
@@ -921,6 +942,7 @@ static const struct file_operations zero_fops = {
  	.llseek		= zero_lseek,
  	.write		= write_zero,
  	.read_iter	= read_iter_zero,
+	.read		= read_zero,

Wondering if .read should be before .write, so that we get in order read, write, read_iter, write_iter.

drivers/char/mem.c static const struct file_operations zero_fops = { · line 948

Quoted 4 lines
drivers/char/mem.c +0 -0
  	.write_iter	= write_iter_zero,
  	.mmap		= mmap_zero,
  	.get_unmapped_area = get_unmapped_area_zero,

Christophe

maintainer
#12 lore raw
Quoted 45 lines · Christophe Leroy

Le 03/09/2020 à 17:59, Christoph Hellwig a écrit :

Quoted 42 lines

Christophe reported a major speedup due to avoiding the iov_iter overhead, so just add this trivial function. Note that /dev/zero already implements both an iter and non-iter writes so this just makes it more symmetric.

Christophe Leroy <[email protected]>

---
  drivers/char/mem.c | 22 ++++++++++++++++++++++
  1 file changed, 22 insertions(+)
drivers/char/mem.c +21 -0
index abd4ffdc8cdebc..1dc99ab158457a 100644
@@ -726,6 +726,27 @@ static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter)
  	return written;
  }
+static ssize_t read_zero(struct file *file, char __user *buf,
+			 size_t count, loff_t *ppos)
+{
+	size_t cleared = 0;
+
+	while (count) {
+		size_t chunk = min_t(size_t, count, PAGE_SIZE);
+
+		if (clear_user(buf + cleared, chunk))
+			return cleared ? cleared : -EFAULT;
+		cleared += chunk;
+		count -= chunk;
+
+		if (signal_pending(current))
+			return cleared ? cleared : -ERESTARTSYS;
+		cond_resched();
+	}
+
+	return cleared;
+}
+
  static int mmap_zero(struct file *file, struct vm_area_struct *vma)
  {
  #ifndef CONFIG_MMU
Quoted 8 lines

drivers/char/mem.c static const struct file_operations zero_fops = { · line 945

Quoted 5 lines
drivers/char/mem.c +1 -0
@@ -921,6 +942,7 @@ static const struct file_operations zero_fops = {
  	.llseek		= zero_lseek,
  	.write		= write_zero,
  	.read_iter	= read_iter_zero,
+	.read		= read_zero,

Wondering if .read should be before .write, so that we get in order read, write, read_iter, write_iter.

It really does not matter :)

thanks,

greg k-h

#13 lore raw
In reply to Christoph Hellwig
Quoted 6 lines · Christoph Hellwig

Christophe reported a major speedup due to avoiding the iov_iter overhead, so just add this trivial function. Note that /dev/zero already implements both an iter and non-iter writes so this just makes it more symmetric.

Christophe Leroy <[email protected]>

?-by ?

Quoted 13 lines
---
 drivers/char/mem.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
drivers/char/mem.c +0 -0
index abd4ffdc8cdebc..1dc99ab158457a 100644
@@ -726,6 +726,27 @@ static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter)
 	return written;
 }
 

drivers/char/mem.c static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter) · line 738

Quoted 10 lines
drivers/char/mem.c +10 -0
+static ssize_t read_zero(struct file *file, char __user *buf,
+			 size_t count, loff_t *ppos)
+{
+	size_t cleared = 0;
+
+	while (count) {
+		size_t chunk = min_t(size_t, count, PAGE_SIZE);
+
+		if (clear_user(buf + cleared, chunk))
+			return cleared ? cleared : -EFAULT;

Probably nobody really cares, but currently doing

read(fd, &unmapped_page - 5, 123);

returns 5, and those five bytes do get cleared; if I'm reading the above right you'd return -EFAULT for that case.

drivers/char/mem.c static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter) · line 743

Quoted 5 lines
drivers/char/mem.c +5 -0
+		cleared += chunk;
+		count -= chunk;
+
+		if (signal_pending(current))
+			return cleared ? cleared : -ERESTARTSYS;

I can't see how we can get here without 'cleared' being positive, so this can just be 'return cleared' (and if you fix the above EFAULT case to more accurately track how much got cleared, there's probably no longer any code to be symmetric with anyway).

Rasmus

#14 lore raw
Quoted 9 lines · Rasmus Villemoes
Quoted 6 lines · Christoph Hellwig

Christophe reported a major speedup due to avoiding the iov_iter overhead, so just add this trivial function. Note that /dev/zero already implements both an iter and non-iter writes so this just makes it more symmetric.

Christophe Leroy <[email protected]>

?-by ?

Suggested-by,

Quoted 19 lines

drivers/char/mem.c static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter) · line 738

Quoted 10 lines
drivers/char/mem.c +10 -0
+static ssize_t read_zero(struct file *file, char __user *buf,
+			 size_t count, loff_t *ppos)
+{
+	size_t cleared = 0;
+
+	while (count) {
+		size_t chunk = min_t(size_t, count, PAGE_SIZE);
+
+		if (clear_user(buf + cleared, chunk))
+			return cleared ? cleared : -EFAULT;

Probably nobody really cares, but currently doing

read(fd, &unmapped_page - 5, 123);

returns 5, and those five bytes do get cleared; if I'm reading the above right you'd return -EFAULT for that case.

Quoted 10 lines

drivers/char/mem.c static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter) · line 743

Quoted 5 lines
drivers/char/mem.c +5 -0
+		cleared += chunk;
+		count -= chunk;
+
+		if (signal_pending(current))
+			return cleared ? cleared : -ERESTARTSYS;

I can't see how we can get here without 'cleared' being positive, so this can just be 'return cleared' (and if you fix the above EFAULT case to more accurately track how much got cleared, there's probably no longer any code to be symmetric with anyway).

Yeah, I'll fix these up and resend.

#15 lore raw
Quoted 33 lines · Christoph Hellwig
Quoted 21 lines · Rasmus Villemoes

drivers/char/mem.c static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter) · line 738

Quoted 10 lines · Christoph Hellwig
drivers/char/mem.c +10 -0
+static ssize_t read_zero(struct file *file, char __user *buf,
+			 size_t count, loff_t *ppos)
+{
+	size_t cleared = 0;
+
+	while (count) {
+		size_t chunk = min_t(size_t, count, PAGE_SIZE);
+
+		if (clear_user(buf + cleared, chunk))
+			return cleared ? cleared : -EFAULT;

Probably nobody really cares, but currently doing

read(fd, &unmapped_page - 5, 123);

returns 5, and those five bytes do get cleared; if I'm reading the above right you'd return -EFAULT for that case.

Quoted 10 lines

drivers/char/mem.c static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter) · line 743

Quoted 5 lines
drivers/char/mem.c +5 -0
+		cleared += chunk;
+		count -= chunk;
+
+		if (signal_pending(current))
+			return cleared ? cleared : -ERESTARTSYS;

I can't see how we can get here without 'cleared' being positive, so this can just be 'return cleared' (and if you fix the above EFAULT case to more accurately track how much got cleared, there's probably no longer any code to be symmetric with anyway).

Quoted 2 lines

Yeah, I'll fix these up and resend.

Actually, while you're micro-optimizing it, AFAIK VFS already handles count==0, so you can avoid the initial branch and the last cond_resched() by writing it something like

  while (1) {
    size_t chunk = min_t(size_t, count, PAGE_SIZE), c;
    c = chunk - clear_user(buf + cleared, chunk);
    if (unlikely(!c))
       return cleared ?: -EFAULT;
    cleared += c;
    count -= c;
    if (!count || signal_pending())
       return cleared;
    cond_resched();
  }

For the dd test case with the default bs=512 that avoids cond_resched() and signal_pending() altogether.

Rasmus

#16 lore raw
Quoted 3 lines · Rasmus Villemoes

Actually, while you're micro-optimizing it, AFAIK VFS already handles count==0,

It doesn't.