public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* A bug in read operation for /dev/zero and a proposed fix.
@ 2009-06-04 20:32 Salman Qazi
  2009-06-04 20:50 ` Andrew Morton
  0 siblings, 1 reply; 5+ messages in thread
From: Salman Qazi @ 2009-06-04 20:32 UTC (permalink / raw)
  To: akpm, linux-kernel

While running 20 parallel instances of dd as follows:

#!/bin/bash

for i in `seq 1 20`; do
         dd if=/dev/zero of=/export/hda3/dd_$i bs=1073741824 count=1 &
done
wait

on a 16G machine, we noticed that rather than just killing the
processes, the entire kernel went down.  Stracing dd reveals that it first
does an mmap2, which makes 1GB worth of zero page mappings.  Then it 
performs
a read on those pages from /dev/zero, and finally it performs a write. 
The
machine died during the reads.  Looking at the code, it was noticed that
/dev/zero's read operation had been changed at some point from giving
zero page mappings to actually zeroing the page.  The zeroing of the
pages causes physical pages to be allocated to the process.  But, when
the process exhausts all the memory that it can, the kernel cannot kill
it, as it is still in the kernel mode allocating more memory. 
Consequently,
the kernel eventually crashes.

To fix this, I propose that when a fatal signal is pending during
/dev/zero read operation, we simply return and let the user process die.
Here is a patch that does that.

Signed-off-by: Salman Qazi <sqazi@google.com>
---
diff --git a/drivers/char/mem.c b/drivers/char/mem.c
index 8f05c38..2ffa36e 100644
--- a/drivers/char/mem.c
+++ b/drivers/char/mem.c
@@ -696,6 +696,11 @@ static ssize_t read_zero(struct file * file, char __user * buf,
  			break;
  		buf += chunk;
  		count -= chunk;
+		/* The exit code here doesn't actually matter, as userland
+		 * will never see it.
+		 */
+		if (fatal_signal_pending(current))
+			return -ENOMEM;
  		cond_resched();
  	}
  	return written ? written : -EFAULT;

^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-06-04 21:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-06-04 20:32 A bug in read operation for /dev/zero and a proposed fix Salman Qazi
2009-06-04 20:50 ` Andrew Morton
2009-06-04 20:56   ` Salman Qazi
2009-06-04 21:01   ` Linus Torvalds
2009-06-04 21:05     ` Linus Torvalds

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox