[PATCH 3.5 19/96] Staging: zram: Fix memory leak by refcount mismatch

From: Luis Henriques
Date: Mon Jan 13 2014 - 09:37:27 EST


3.5.7.29 -stable review patch. If anyone has any objections, please let me know.

------------------

From: Rashika Kheria <rashika.kheria@xxxxxxxxx>

commit 1b672224d128ec2570eb37572ff803cfe452b4f7 upstream.

As suggested by Minchan Kim and Jerome Marchand "The code in reset_store
get the block device (bdget_disk()) but it does not put it (bdput()) when
it's done using it. The usage count is therefore incremented but never
decremented."

This patch also puts bdput() for all error cases.

Acked-by: Minchan Kim <minchan@xxxxxxxxxx>
Acked-by: Jerome Marchand <jmarchan@xxxxxxxxxx>
Signed-off-by: Rashika Kheria <rashika.kheria@xxxxxxxxx>
Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
[ luis: backported to 3.5:
- adjusted filename: zram_drv.c -> zram_sysfs.c
- adjusted context ]
Signed-off-by: Luis Henriques <luis.henriques@xxxxxxxxxxxxx>
---
drivers/staging/zram/zram_sysfs.c | 19 ++++++++++++++-----
1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/drivers/staging/zram/zram_sysfs.c b/drivers/staging/zram/zram_sysfs.c
index 01f9a79..aafcf03 100644
--- a/drivers/staging/zram/zram_sysfs.c
+++ b/drivers/staging/zram/zram_sysfs.c
@@ -99,18 +99,23 @@ static ssize_t reset_store(struct device *dev,
return -ENOMEM;

/* Do not reset an active device! */
- if (bdev->bd_holders)
- return -EBUSY;
+ if (bdev->bd_holders) {
+ ret = -EBUSY;
+ goto out;
+ }

ret = kstrtou16(buf, 10, &do_reset);
if (ret)
- return ret;
+ goto out;

- if (!do_reset)
- return -EINVAL;
+ if (!do_reset) {
+ ret = -EINVAL;
+ goto out;
+ }

/* Make sure all pending I/O is finished */
fsync_bdev(bdev);
+ bdput(bdev);

down_write(&zram->init_lock);
if (zram->init_done)
@@ -118,6 +123,10 @@ static ssize_t reset_store(struct device *dev,
up_write(&zram->init_lock);

return len;
+
+out:
+ bdput(bdev);
+ return ret;
}

static ssize_t num_reads_show(struct device *dev,
--
1.8.3.2

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/