[PATCH] memory leakage in ext4_ext_zeroout

Previous thread: Re: bug in ext3 code causing OOM error on systems with small memory by Andrew Morton on Friday, March 12, 2010 - 2:57 pm. (3 messages)

Next thread: Re: Bug#564084: debugfs: logdump -b<block> show incorrect allocation status in block bitmap by tytso on Sunday, March 14, 2010 - 4:02 pm. (1 message)
From: jing zhang
Date: Friday, March 12, 2010 - 11:33 pm

From: Jing Zhang &lt;zj.barak@gmail.com&gt;

Date: Sat Mar 13 14:05:27     2010

When EIO occurs after bio is submitted, there is no memory free
operation for bio, which results in memory leakage. And there is also
no check against bio_alloc() for bio.

Cc: Dave Kleikamp &lt;shaggy@linux.vnet.ibm.com&gt;
Signed-off-by: Jing Zhang &lt;zj.barak@gmail.com&gt;

---

--- o/fs/ext4/extents.c	2010-03-13 13:47:22.000000000 +0800
+++ c/fs/ext4/extents.c	2010-03-13 13:54:36.000000000 +0800
@@ -2568,6 +2568,10 @@ static int ext4_ext_zeroout(struct inode
 			len = ee_len;

 		bio = bio_alloc(GFP_NOIO, len);
+		if (! bio) {
+			ret = -ENOMEM;
+			break;
+		}	
 		bio-&gt;bi_sector = ee_pblock;
 		bio-&gt;bi_bdev   = inode-&gt;i_sb-&gt;s_bdev;

@@ -2598,6 +2602,7 @@ static int ext4_ext_zeroout(struct inode
 		if (test_bit(BIO_UPTODATE, &amp;bio-&gt;bi_flags))
 			ret = 0;
 		else {
+			bio_put(bio);
 			ret = -EIO;
 			break;
 		}
--

From: tytso
Date: Sunday, March 21, 2010 - 6:02 pm

Thanks for the patch.

This is what I've checked in, which also simplifies the error handling
a bit and makes the code a bit easier to follow.

      	  	    	       	      	 - Ted

ext4: fix memory leaks in error path handling of ext4_ext_zeroout()

From: jing zhang &lt;zj.barak@gmail.com&gt;

When EIO occurs after bio is submitted, there is no memory free
operation for bio, which results in memory leakage. And there is also
no check against bio_alloc() for bio.

Acked-by: Dave Kleikamp &lt;shaggy@linux.vnet.ibm.com&gt;
Signed-off-by: Jing Zhang &lt;zj.barak@gmail.com&gt;
Signed-off-by: &quot;Theodore Ts'o&quot; &lt;tytso@mit.edu&gt;
---
 fs/ext4/extents.c |   15 ++++++++-------
 1 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index 94c8ee8..8bdee27 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -2544,7 +2544,7 @@ static void bi_complete(struct bio *bio, int error)
 /* FIXME!! we need to try to merge to left or right after zero-out  */
 static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
 {
-	int ret = -EIO;
+	int ret;
 	struct bio *bio;
 	int blkbits, blocksize;
 	sector_t ee_pblock;
@@ -2568,6 +2568,9 @@ static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
 			len = ee_len;
 
 		bio = bio_alloc(GFP_NOIO, len);
+		if (!bio)
+			return -ENOMEM;
+
 		bio-&gt;bi_sector = ee_pblock;
 		bio-&gt;bi_bdev   = inode-&gt;i_sb-&gt;s_bdev;
 
@@ -2595,17 +2598,15 @@ static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
 		submit_bio(WRITE, bio);
 		wait_for_completion(&amp;event);
 
-		if (test_bit(BIO_UPTODATE, &amp;bio-&gt;bi_flags))
-			ret = 0;
-		else {
-			ret = -EIO;
-			break;
+		if (!test_bit(BIO_UPTODATE, &amp;bio-&gt;bi_flags)) {
+			bio_put(bio);
+			return -EIO;
 		}
 		bio_put(bio);
 		ee_len    -= done;
 		ee_pblock += done  &lt;&lt; (blkbits - 9);
 	}
-	return ret;
+	return 0;
 }
 
 #define EXT4_EXT_ZERO_LEN 7

--

Previous thread: Re: bug in ext3 code causing OOM error on systems with small memory by Andrew Morton on Friday, March 12, 2010 - 2:57 pm. (3 messages)

Next thread: Re: Bug#564084: debugfs: logdump -b<block> show incorrect allocation status in block bitmap by tytso on Sunday, March 14, 2010 - 4:02 pm. (1 message)