Skip to contentSkip to conversation

Staging: zram: Fix access of NULL pointer

PATCH Superseded Rashika Kheria 5 participants 35 messages Oct 28, 2013 – Nov 10, 2013 2 messages not in the archive

Subsystems (2)
lore
Share & tools
lore
#1 lore raw

PATCH v5 1/3 Staging: zram: Fix access of NULL pointer

In reply to a message not in the archive

This patch fixes the bug in reset_store caused by accessing NULL pointer. Hence, It introduces a check for bdev. It also removes unnecessary check of bdev for fsync_bdev().

---

This revision fixes the following issues of the previous revision- Seperate patch into Bug Fix and Smatch Fix

 drivers/staging/zram/zram_drv.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/staging/zram/zram_drv.c +4 -2
index 2c4ed52..d640a8f 100644
@@ -648,6 +648,9 @@ static ssize_t reset_store(struct device *dev,
 	zram = dev_to_zram(dev);
 	bdev = bdget_disk(zram->disk, 0);
 
+	if (!bdev)
+		return -EBUSY;
+
 	/* Do not reset an active device! */
 	if (bdev->bd_holders)
 		return -EBUSY;
@@ -660,8 +663,7 @@ static ssize_t reset_store(struct device *dev,
 		return -EINVAL;
 
 	/* Make sure all pending I/O is finished */
-	if (bdev)
-		fsync_bdev(bdev);
+	fsync_bdev(bdev);
 
 	zram_reset_device(zram, true);
 	return len;
Signature · 1 line
1.7.9.5
#3 lore raw

PATCH v5 3/3 Staging: zram: Fix decrement of variable by calling bdput()

In reply to a message not in the archive

As suggested by 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 therefor incremented but never decremented."

Hence, this patch introduces a call to bdput() to decrement the variable after usage.

---
 drivers/staging/zram/zram_drv.c |    1 +
 1 file changed, 1 insertion(+)
drivers/staging/zram/zram_drv.c +1 -0
index 9027975..98dac15 100644
@@ -664,6 +664,7 @@ static ssize_t reset_store(struct device *dev,
 
 	/* Make sure all pending I/O is finished */
 	fsync_bdev(bdev);
+	bdput(bdev);
 
 	zram_reset_device(zram, true);
 	return len;
Signature · 1 line
1.7.9.5
#2 lore raw

PATCH v5 2/3 Staging: zram: Fix variable dereferenced before check

In reply to a message not in the archive

This patch fixes the following Smatch warning in zram_drv.c- drivers/staging/zram/zram_drv.c:899
destroy_device() warn: variable dereferenced before check 'zram->disk' (see line 896)

---

This revision fixes the following issues of the previous revision- Seperating patches into Bug fix and Smatch fix

 drivers/staging/zram/zram_drv.c |    9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/staging/zram/zram_drv.c +3 -6
index d640a8f..9027975 100644
@@ -898,13 +898,10 @@ static void destroy_device(struct zram *zram)
 	sysfs_remove_group(&disk_to_dev(zram->disk)->kobj,
 			&zram_disk_attr_group);
 
-	if (zram->disk) {
-		del_gendisk(zram->disk);
-		put_disk(zram->disk);
-	}
+	del_gendisk(zram->disk);
+	put_disk(zram->disk);
 
-	if (zram->queue)
-		blk_cleanup_queue(zram->queue);
+	blk_cleanup_queue(zram->queue);
 }
 
 static int __init zram_init(void)
Signature · 1 line
1.7.9.5
#4 lore raw

PATCH v6 1/3 Staging: zram: Fix access of NULL pointer

In reply to a message not in the archive

This patch fixes the bug in reset_store caused by accessing NULL pointer. Hence, It introduces a check for bdev. It also removes unnecessary check of bdev for fsync_bdev().

---

This revision fixes the following issues of the previous revision- Seperating patches into Bug fix and Smatch fix

 drivers/staging/zram/zram_drv.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/staging/zram/zram_drv.c +4 -2
index 2c4ed52..d640a8f 100644
@@ -648,6 +648,9 @@ static ssize_t reset_store(struct device *dev,
 	zram = dev_to_zram(dev);
 	bdev = bdget_disk(zram->disk, 0);
 
+	if (!bdev)
+		return -EBUSY;
+
 	/* Do not reset an active device! */
 	if (bdev->bd_holders)
 		return -EBUSY;
@@ -660,8 +663,7 @@ static ssize_t reset_store(struct device *dev,
 		return -EINVAL;
 
 	/* Make sure all pending I/O is finished */
-	if (bdev)
-		fsync_bdev(bdev);
+	fsync_bdev(bdev);
 
 	zram_reset_device(zram, true);
 	return len;
Signature · 1 line
1.7.9.5
#9 lore raw

PATCH v6 2/3 Staging: zram: Fix variable dereferenced before check

In reply to a message not in the archive

This patch fixes the following Smatch warning in zram_drv.c- drivers/staging/zram/zram_drv.c:899
destroy_device() warn: variable dereferenced before check 'zram->disk' (see line 896)

---

This revision fixes the following issues of the previous revision- Seperating patches into Bug fix and Smatch fix

 drivers/staging/zram/zram_drv.c |    9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/staging/zram/zram_drv.c +3 -6
index d640a8f..9027975 100644
@@ -898,13 +898,10 @@ static void destroy_device(struct zram *zram)
 	sysfs_remove_group(&disk_to_dev(zram->disk)->kobj,
 			&zram_disk_attr_group);
 
-	if (zram->disk) {
-		del_gendisk(zram->disk);
-		put_disk(zram->disk);
-	}
+	del_gendisk(zram->disk);
+	put_disk(zram->disk);
 
-	if (zram->queue)
-		blk_cleanup_queue(zram->queue);
+	blk_cleanup_queue(zram->queue);
 }
 
 static int __init zram_init(void)
Signature · 1 line
1.7.9.5
#11 lore raw

PATCH v6 3/3 Staging: zram: Fix decrement of variable by calling bdput()

In reply to a message not in the archive

As suggested by 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 therefor incremented but never decremented."

Hence, this patch introduces a call to bdput() to decrement the variable after usage.

---
 drivers/staging/zram/zram_drv.c |    1 +
 1 file changed, 1 insertion(+)
drivers/staging/zram/zram_drv.c +1 -0
index 9027975..98dac15 100644
@@ -664,6 +664,7 @@ static ssize_t reset_store(struct device *dev,
 
 	/* Make sure all pending I/O is finished */
 	fsync_bdev(bdev);
+	bdput(bdev);
 
 	zram_reset_device(zram, true);
 	return len;
Signature · 1 line
1.7.9.5
#5 lore raw
In reply to Rashika Kheria
Quoted 18 lines · Rashika Kheria

This patch fixes the bug in reset_store caused by accessing NULL pointer. Hence, It introduces a check for bdev. It also removes unnecessary check of bdev for fsync_bdev().

---

This revision fixes the following issues of the previous revision- Seperating patches into Bug fix and Smatch fix

 drivers/staging/zram/zram_drv.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/staging/zram/zram_drv.c +0 -0
index 2c4ed52..d640a8f 100644

drivers/staging/zram/zram_drv.c static ssize_t reset_store(struct device *dev, · line 653

Quoted 6 lines
drivers/staging/zram/zram_drv.c +3 -0
@@ -648,6 +648,9 @@ static ssize_t reset_store(struct device *dev,
        zram = dev_to_zram(dev);
        bdev = bdget_disk(zram->disk, 0);
 
+       if (!bdev)
+               return -EBUSY;
+

I am not sure how does it happen. Would you please make it clear to me?

Thanks

Quoted 21 lines
drivers/staging/zram/zram_drv.c +1 -3
        /* Do not reset an active device! */
        if (bdev->bd_holders)
                return -EBUSY;
@@ -660,8 +663,7 @@ static ssize_t reset_store(struct device *dev,
                return -EINVAL;
 
        /* Make sure all pending I/O is finished */
-       if (bdev)
-               fsync_bdev(bdev);
+       fsync_bdev(bdev);
 
        zram_reset_device(zram, true);
        return len;
--

1.7.9.5

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

#7 lore raw
In reply to a message not in the archive
Quoted 38 lines · Rashika Kheria
Quoted 26 lines · Weijie Yang
Quoted 23 lines · Rashika Kheria

This patch fixes the bug in reset_store caused by accessing NULL pointer.
Hence, It introduces a check for bdev. It also removes unnecessary check of bdev for fsync_bdev().

---

This revision fixes the following issues of the previous revision- Seperating patches into Bug fix and Smatch fix

 drivers/staging/zram/zram_drv.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/staging/zram/zram_drv.c +0 -0
index 2c4ed52..d640a8f 100644
@@ -648,6 +648,9 @@ static ssize_t reset_store(struct device *dev,
        zram = dev_to_zram(dev);
        bdev = bdget_disk(zram->disk, 0);
 
Quoted 6 lines

drivers/staging/zram/zram_drv.c static ssize_t reset_store(struct device *dev, · line 653

Quoted 3 lines
drivers/staging/zram/zram_drv.c +3 -0
+       if (!bdev)
+               return -EBUSY;
+

I am not sure how does it happen. Would you please make it clear to me?

Quoted 7 lines

Hi Weijie,

The bdev gets its value from bdget_disk() which could fail when memory is less available and hence bdev can be NULL.

If this check is not introduced then we might reference NULL pointer in the later part of the code.

I see. Thanks!

Quoted 8 lines

I hope I am clear.

Thanks,

--
Rashika Kheria
B.Tech CSE
IIIT Hyderabad

#8 lore raw
In reply to Rashika Kheria

Hello Rashika,

Quoted 3 lines · Rashika Kheria

This patch fixes the bug in reset_store caused by accessing NULL pointer. Hence, It introduces a check for bdev. It also removes unnecessary check of bdev for fsync_bdev().

It's better than old but I still want it more better.
First of all, I hope patch description is more clear in case of bugfix patch because other maintainers from stable/distro or other people could understand well so they could judge once again whether they need to backport or not. If maintainer judge that it's not severe, even they couldn't backport.

Patch description includes followin as, at least.

1. When this bug happens?
2. What's the result of user POV?
3. How to fix it.

You were good for 2 and 3 but 1

I can help.

If memory pressure is severe, inode could be reclaimed. In this case, bdget_disk can return NULL because allocation of inode in bdget could fail.

I hope you could do better massage.

In addition, this bug have been for a long time so it deserves backporting to stable tree. In such case, you could Cc stable tree with stable mark. Please refer Documentation/stable_kernel_rules.txt and others patches in LKML.

After this year kernel summit, it seems to change something rule for marking stable but I thinks it's not solid so old rule still would be valid.

Thanks!

Quoted 42 lines

---

This revision fixes the following issues of the previous revision- Seperating patches into Bug fix and Smatch fix

 drivers/staging/zram/zram_drv.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/staging/zram/zram_drv.c +4 -2
index 2c4ed52..d640a8f 100644
@@ -648,6 +648,9 @@ static ssize_t reset_store(struct device *dev,
 	zram = dev_to_zram(dev);
 	bdev = bdget_disk(zram->disk, 0);
 
+	if (!bdev)
+		return -EBUSY;
+
 	/* Do not reset an active device! */
 	if (bdev->bd_holders)
 		return -EBUSY;
@@ -660,8 +663,7 @@ static ssize_t reset_store(struct device *dev,
 		return -EINVAL;
 
 	/* Make sure all pending I/O is finished */
-	if (bdev)
-		fsync_bdev(bdev);
+	fsync_bdev(bdev);
 
 	zram_reset_device(zram, true);
 	return len;
Signature · 7 lines
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/
Signature · 2 lines
Kind regards,
Minchan Kim
Acked-by on v6 2/3
#10 lore raw

Re: [PATCH v6 2/3] Staging: zram: Fix variable dereferenced before check

In reply to Rashika Kheria
Quoted 5 lines · Rashika Kheria

This patch fixes the following Smatch warning in zram_drv.c- drivers/staging/zram/zram_drv.c:899
destroy_device() warn: variable dereferenced before check 'zram->disk' (see line 896)

Signature · 2 lines
Kind regards,
Minchan Kim
#12 lore raw

Re: [PATCH v6 3/3] Staging: zram: Fix decrement of variable by calling bdput()

In reply to Rashika Kheria
Quoted 7 lines · Rashika Kheria

As suggested by 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 therefor incremented but never decremented."

Hence, this patch introduces a call to bdput() to decrement the variable after usage.

I think this patch is for stable, too.
So, please read my previous reply.

And please relocate this with [2/3] and do bdput in all of error cases in reset_store.

Quoted 24 lines
---
 drivers/staging/zram/zram_drv.c |    1 +
 1 file changed, 1 insertion(+)
drivers/staging/zram/zram_drv.c +1 -0
index 9027975..98dac15 100644
@@ -664,6 +664,7 @@ static ssize_t reset_store(struct device *dev,
 
 	/* Make sure all pending I/O is finished */
 	fsync_bdev(bdev);
+	bdput(bdev);
 
 	zram_reset_device(zram, true);
 	return len;
Signature · 7 lines
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/
Signature · 2 lines
Kind regards,
Minchan Kim
#13 lore raw

PATCH v7 1/3 Staging: zram: Fix access of NULL pointer

In reply to a message not in the archive

This patch fixes the bug in reset_store caused by accessing NULL pointer.

The bdev gets its value from bdget_disk() which could fail when memory pressure is severe and hence can return NULL because allocation of inode in bdget could fail.

Hence, this patch introduces a check for bdev to prevent reference to a NULL pointer in the later part of the code. It also removes unnecessary check of bdev for fsync_bdev().

---

This patch fixes the following issues of the previous revision- Better Description

 drivers/staging/zram/zram_drv.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/staging/zram/zram_drv.c +4 -2
index 2c4ed52..d640a8f 100644
@@ -648,6 +648,9 @@ static ssize_t reset_store(struct device *dev,
 	zram = dev_to_zram(dev);
 	bdev = bdget_disk(zram->disk, 0);
 
+	if (!bdev)
+		return -EBUSY;
+
 	/* Do not reset an active device! */
 	if (bdev->bd_holders)
 		return -EBUSY;
@@ -660,8 +663,7 @@ static ssize_t reset_store(struct device *dev,
 		return -EINVAL;
 
 	/* Make sure all pending I/O is finished */
-	if (bdev)
-		fsync_bdev(bdev);
+	fsync_bdev(bdev);
 
 	zram_reset_device(zram, true);
 	return len;
Signature · 1 line
1.7.9.5
#17 lore raw

PATCH v7 2/3 Staging: zram: Fix decrement of variable by calling bdput()

In reply to a message not in the archive

As suggested by 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 therefor incremented but never decremented."

Hence, this patch introduces a call to bdput() to decrement the variable after usage.

---

This revision fixes the following issues of the previous revision- Handle more error cases

 drivers/staging/zram/zram_drv.c |    3 +++
 1 file changed, 3 insertions(+)
drivers/staging/zram/zram_drv.c +3 -0
index d640a8f..592e760 100644
@@ -664,6 +664,9 @@ static ssize_t reset_store(struct device *dev,
 
 	/* Make sure all pending I/O is finished */
 	fsync_bdev(bdev);
+	bdput(bdev);
+	bdput(bdev->bd_holders);
+	bdput(do_reset);
 
 	zram_reset_device(zram, true);
 	return len;
Signature · 1 line
1.7.9.5
#19 lore raw

PATCH v7 3/3 Staging: zram: Fix variable dereferenced before check

In reply to a message not in the archive

This patch fixes the following Smatch warning in zram_drv.c- drivers/staging/zram/zram_drv.c:899
destroy_device() warn: variable dereferenced before check 'zram->disk' (see line 896)

---
 drivers/staging/zram/zram_drv.c |    9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/staging/zram/zram_drv.c +3 -6
index 592e760..bf28d56 100644
@@ -901,13 +901,10 @@ static void destroy_device(struct zram *zram)
 	sysfs_remove_group(&disk_to_dev(zram->disk)->kobj,
 			&zram_disk_attr_group);
 
-	if (zram->disk) {
-		del_gendisk(zram->disk);
-		put_disk(zram->disk);
-	}
+	del_gendisk(zram->disk);
+	put_disk(zram->disk);
 
-	if (zram->queue)
-		blk_cleanup_queue(zram->queue);
+	blk_cleanup_queue(zram->queue);
 }
 
 static int __init zram_init(void)
Signature · 1 line
1.7.9.5
Acked-by on v7 1/3
#14 lore raw
In reply to Rashika Kheria
Quoted 6 lines · Rashika Kheria

This patch fixes the bug in reset_store caused by accessing NULL pointer.

The bdev gets its value from bdget_disk() which could fail when memory pressure is severe and hence can return NULL because allocation of inode in bdget could fail.

Quoted 6 lines

Hence, this patch introduces a check for bdev to prevent reference to a NULL pointer in the later part of the code. It also removes unnecessary check of bdev for fsync_bdev().

Signature · 2 lines
Kind regards,
Minchan Kim
#18 lore raw

Re: [PATCH v7 2/3] Staging: zram: Fix decrement of variable by calling bdput()

In reply to Rashika Kheria
Quoted 21 lines · Rashika Kheria

As suggested by 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 therefor incremented but never decremented."

Hence, this patch introduces a call to bdput() to decrement the variable after usage.

---

This revision fixes the following issues of the previous revision- Handle more error cases

 drivers/staging/zram/zram_drv.c |    3 +++
 1 file changed, 3 insertions(+)
drivers/staging/zram/zram_drv.c +0 -0
index d640a8f..592e760 100644

drivers/staging/zram/zram_drv.c static ssize_t reset_store(struct device *dev, · line 669

Quoted 6 lines
drivers/staging/zram/zram_drv.c +3 -0
@@ -664,6 +664,9 @@ static ssize_t reset_store(struct device *dev,
 
 	/* Make sure all pending I/O is finished */
 	fsync_bdev(bdev);
+	bdput(bdev);
+	bdput(bdev->bd_holders);
+	bdput(do_reset);

I meant it.

drivers/staging/zram/zram_drv.c +15 -7
index e77fb6e..32f9a44 100644
@@ -621,22 +621,30 @@ static ssize_t reset_store(struct device *dev,
 	bdev = bdget_disk(zram->disk, 0);
 
 	/* 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 */
-	if (bdev)
-		fsync_bdev(bdev);
+	fsync_bdev(bdev);
+	bdput(bdev);
 
 	zram_reset_device(zram);
 	return len;
+
+out:
+	bdput(bdev);
+	return ret;
 }
 
 static void __zram_make_request(struct zram *zram, struct bio *bio, int rw)
Signature · 2 lines
Kind regards,
Minchan Kim
#20 lore raw

Re: [PATCH v7 3/3] Staging: zram: Fix variable dereferenced before check

In reply to Rashika Kheria
Quoted 5 lines · Rashika Kheria

This patch fixes the following Smatch warning in zram_drv.c- drivers/staging/zram/zram_drv.c:899
destroy_device() warn: variable dereferenced before check 'zram->disk' (see line 896)

It shouldn't be a stable stuff because it's just warning of Smatch but there is no bug in real practice.

Quoted 35 lines
---
 drivers/staging/zram/zram_drv.c |    9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/staging/zram/zram_drv.c +3 -6
index 592e760..bf28d56 100644
@@ -901,13 +901,10 @@ static void destroy_device(struct zram *zram)
 	sysfs_remove_group(&disk_to_dev(zram->disk)->kobj,
 			&zram_disk_attr_group);
 
-	if (zram->disk) {
-		del_gendisk(zram->disk);
-		put_disk(zram->disk);
-	}
+	del_gendisk(zram->disk);
+	put_disk(zram->disk);
 
-	if (zram->queue)
-		blk_cleanup_queue(zram->queue);
+	blk_cleanup_queue(zram->queue);
 }
 
 static int __init zram_init(void)
Signature · 7 lines
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/
Signature · 2 lines
Kind regards,
Minchan Kim
#15 lore raw
In reply to Rashika Kheria
Quoted 25 lines · Rashika Kheria

This patch fixes the bug in reset_store caused by accessing NULL pointer.

The bdev gets its value from bdget_disk() which could fail when memory pressure is severe and hence can return NULL because allocation of inode in bdget could fail.

Hence, this patch introduces a check for bdev to prevent reference to a NULL pointer in the later part of the code. It also removes unnecessary check of bdev for fsync_bdev().

---

This patch fixes the following issues of the previous revision- Better Description

 drivers/staging/zram/zram_drv.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/staging/zram/zram_drv.c +0 -0
index 2c4ed52..d640a8f 100644

drivers/staging/zram/zram_drv.c static ssize_t reset_store(struct device *dev, · line 653

Quoted 6 lines
drivers/staging/zram/zram_drv.c +3 -0
@@ -648,6 +648,9 @@ static ssize_t reset_store(struct device *dev,
 	zram = dev_to_zram(dev);
 	bdev = bdget_disk(zram->disk, 0);
 
+	if (!bdev)
+		return -EBUSY;
+

Oops, Sorry.
I don't think EBUSY is right. How about ENOMEM?

Signature · 2 lines
Kind regards,
Minchan Kim
Acked-by on v7 1/3
#16 lore raw
In reply to Rashika Kheria
Quoted 6 lines · Rashika Kheria

This patch fixes the bug in reset_store caused by accessing NULL pointer.

The bdev gets its value from bdget_disk() which could fail when memory pressure is severe and hence can return NULL because allocation of inode in bdget could fail.

Quoted 6 lines

Hence, this patch introduces a check for bdev to prevent reference to a NULL pointer in the later part of the code. It also removes unnecessary check of bdev for fsync_bdev().

Quoted 33 lines

---

This patch fixes the following issues of the previous revision- Better Description

 drivers/staging/zram/zram_drv.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/staging/zram/zram_drv.c +4 -2
index 2c4ed52..d640a8f 100644
@@ -648,6 +648,9 @@ static ssize_t reset_store(struct device *dev,
 	zram = dev_to_zram(dev);
 	bdev = bdget_disk(zram->disk, 0);
 
+	if (!bdev)
+		return -EBUSY;
+
 	/* Do not reset an active device! */
 	if (bdev->bd_holders)
 		return -EBUSY;
@@ -660,8 +663,7 @@ static ssize_t reset_store(struct device *dev,
 		return -EINVAL;
 
 	/* Make sure all pending I/O is finished */
-	if (bdev)
-		fsync_bdev(bdev);
+	fsync_bdev(bdev);
 
 	zram_reset_device(zram, true);
 	return len;
Acked-by on v7 3/3
#21 lore raw

Re: [PATCH v7 3/3] Staging: zram: Fix variable dereferenced before check

In reply to Rashika Kheria
Quoted 7 lines · Rashika Kheria

This patch fixes the following Smatch warning in zram_drv.c- drivers/staging/zram/zram_drv.c:899
destroy_device() warn: variable dereferenced before check 'zram->disk' (see line 896)

Quoted 26 lines
---
 drivers/staging/zram/zram_drv.c |    9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/staging/zram/zram_drv.c +3 -6
index 592e760..bf28d56 100644
@@ -901,13 +901,10 @@ static void destroy_device(struct zram *zram)
 	sysfs_remove_group(&disk_to_dev(zram->disk)->kobj,
 			&zram_disk_attr_group);
 
-	if (zram->disk) {
-		del_gendisk(zram->disk);
-		put_disk(zram->disk);
-	}
+	del_gendisk(zram->disk);
+	put_disk(zram->disk);
 
-	if (zram->queue)
-		blk_cleanup_queue(zram->queue);
+	blk_cleanup_queue(zram->queue);
 }
 
 static int __init zram_init(void)
#22 lore raw

PATCH v8 1/3 Staging: zram: Fix access of NULL pointer

In reply to a message not in the archive

This patch fixes the bug in reset_store caused by accessing NULL pointer.

The bdev gets its value from bdget_disk() which could fail when memory pressure is severe and hence can return NULL because allocation of inode in bdget could fail.

Hence, this patch introduces a check for bdev to prevent reference to a NULL pointer in the later part of the code. It also removes unnecessary check of bdev for fsync_bdev().

---

The previous revision fixes the following issues of the previous revision- Change return error code

 drivers/staging/zram/zram_drv.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/staging/zram/zram_drv.c +4 -2
index 2c4ed52..012ba15 100644
@@ -648,6 +648,9 @@ static ssize_t reset_store(struct device *dev,
 	zram = dev_to_zram(dev);
 	bdev = bdget_disk(zram->disk, 0);
 
+	if (!bdev)
+		return -ENOMEM;
+
 	/* Do not reset an active device! */
 	if (bdev->bd_holders)
 		return -EBUSY;
@@ -660,8 +663,7 @@ static ssize_t reset_store(struct device *dev,
 		return -EINVAL;
 
 	/* Make sure all pending I/O is finished */
-	if (bdev)
-		fsync_bdev(bdev);
+	fsync_bdev(bdev);
 
 	zram_reset_device(zram, true);
 	return len;
Signature · 1 line
1.7.9.5
#23 lore raw

PATCH v8 2/3 Staging: zram: Fix decrement of variable by calling bdput()

In reply to a message not in the archive

As suggested by 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.

---

This revision fixes the following issues of the previous revision- Proper error handling

 drivers/staging/zram/zram_drv.c |   25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)
drivers/staging/zram/zram_drv.c +18 -7
index 012ba15..0bc2835 100644
@@ -648,25 +648,36 @@ static ssize_t reset_store(struct device *dev,
 	zram = dev_to_zram(dev);
 	bdev = bdget_disk(zram->disk, 0);
 
-	if (!bdev)
-		return -ENOMEM;
+	if (!bdev) {
+		ret = -ENOMEM;
+		goto out;
+	}
 
 	/* 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);
 
 	zram_reset_device(zram, true);
 	return len;
+
+out:	
+	bdput(bdev);
+	return ret;
 }
 
 static void __zram_make_request(struct zram *zram, struct bio *bio, int rw)
Signature · 1 line
1.7.9.5
#25 lore raw

PATCH v8 3/3 Staging: zram: Fix variable dereferenced before check

In reply to a message not in the archive

This patch fixes the following Smatch warning in zram_drv.c- drivers/staging/zram/zram_drv.c:899
destroy_device() warn: variable dereferenced before check 'zram->disk' (see line 896)

---

This revision fixes the following issues of the previous revision- Removes Cc to [email protected] as it is just a Smatch warning fix

 drivers/staging/zram/zram_drv.c |    9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/staging/zram/zram_drv.c +3 -6
index 0bc2835..cfa1d2e 100644
@@ -909,13 +909,10 @@ static void destroy_device(struct zram *zram)
 	sysfs_remove_group(&disk_to_dev(zram->disk)->kobj,
 			&zram_disk_attr_group);
 
-	if (zram->disk) {
-		del_gendisk(zram->disk);
-		put_disk(zram->disk);
-	}
+	del_gendisk(zram->disk);
+	put_disk(zram->disk);
 
-	if (zram->queue)
-		blk_cleanup_queue(zram->queue);
+	blk_cleanup_queue(zram->queue);
 }
 
 static int __init zram_init(void)
Signature · 1 line
1.7.9.5
maintainer Applied
#26 lore raw

Re: [PATCH v8 3/3] Staging: zram: Fix variable dereferenced before check

Quoted 11 lines · Rashika Kheria

This patch fixes the following Smatch warning in zram_drv.c- drivers/staging/zram/zram_drv.c:899
destroy_device() warn: variable dereferenced before check 'zram->disk' (see line 896)

---

This revision fixes the following issues of the previous revision- Removes Cc to [email protected] as it is just a Smatch warning fix

Applied, thanks.

greg k-h

#24 lore raw

Re: [PATCH v8 2/3] Staging: zram: Fix decrement of variable by calling bdput()

In reply to Rashika Kheria

Hello, Rashika

Quoted 21 lines · Rashika Kheria

As suggested by 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.

---

This revision fixes the following issues of the previous revision- Proper error handling

 drivers/staging/zram/zram_drv.c |   25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)
drivers/staging/zram/zram_drv.c +0 -0
index 012ba15..0bc2835 100644

drivers/staging/zram/zram_drv.c static ssize_t reset_store(struct device *dev, · line 654

Quoted 9 lines
drivers/staging/zram/zram_drv.c +4 -2
@@ -648,25 +648,36 @@ static ssize_t reset_store(struct device *dev,
        zram = dev_to_zram(dev);
        bdev = bdget_disk(zram->disk, 0);
 
-       if (!bdev)
-               return -ENOMEM;
+       if (!bdev) {
+               ret = -ENOMEM;
+               goto out;
+       }

If bdev is NULL, just return -ENOMEM; DO NOT goto out; or you will get a NULL point reference in bdput(bdev);

Quoted 41 lines
drivers/staging/zram/zram_drv.c +14 -6
        /* 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);
 
        zram_reset_device(zram, true);
        return len;
+
+out:
+       bdput(bdev);
+       return ret;
 }
 
 static void __zram_make_request(struct zram *zram, struct bio *bio, int rw)
--

1.7.9.5

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

#27 lore raw

PATCH v9 1/2 Staging: zram: Fix access of NULL pointer

In reply to a message not in the archive

This patch fixes the bug in reset_store caused by accessing NULL pointer.

The bdev gets its value from bdget_disk() which could fail when memory pressure is severe and hence can return NULL because allocation of inode in bdget could fail.

Hence, this patch introduces a check for bdev to prevent reference to a NULL pointer in the later part of the code. It also removes unnecessary check of bdev for fsync_bdev().

---
 drivers/staging/zram/zram_drv.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/staging/zram/zram_drv.c +4 -2
index 2c4ed52..012ba15 100644
@@ -648,6 +648,9 @@ static ssize_t reset_store(struct device *dev,
 	zram = dev_to_zram(dev);
 	bdev = bdget_disk(zram->disk, 0);
 
+	if (!bdev)
+		return -ENOMEM;
+
 	/* Do not reset an active device! */
 	if (bdev->bd_holders)
 		return -EBUSY;
@@ -660,8 +663,7 @@ static ssize_t reset_store(struct device *dev,
 		return -EINVAL;
 
 	/* Make sure all pending I/O is finished */
-	if (bdev)
-		fsync_bdev(bdev);
+	fsync_bdev(bdev);
 
 	zram_reset_device(zram, true);
 	return len;
Signature · 1 line
1.7.9.5
#28 lore raw

PATCH v9 2/2 Staging: zram: Fix decrement of variable by calling bdput()

In reply to a message not in the archive

As suggested by 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.

---

This revision fixes the following issues of the previous revision- Proper error handling

 drivers/staging/zram/zram_drv.c |   19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
drivers/staging/zram/zram_drv.c +14 -5
index 012ba15..a1f8b1f 100644
@@ -652,21 +652,30 @@ 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);
 
 	zram_reset_device(zram, true);
 	return len;
+
+out:	
+	bdput(bdev);
+	return ret;
 }
 
 static void __zram_make_request(struct zram *zram, struct bio *bio, int rw)
Signature · 1 line
1.7.9.5
Acked-by on v9 2/2
#29 lore raw

Re: [PATCH v9 2/2] Staging: zram: Fix decrement of variable by calling bdput()

I hope subject should be "Fix memory leak by refcount mismatch"

Quoted 8 lines · Rashika Kheria

As suggested by 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.

Other than that,

Rashika, Thanks!

Signature · 2 lines
Kind regards,
Minchan Kim
Acked-by on v9 2/2
#30 lore raw

Re: [PATCH v9 2/2] Staging: zram: Fix decrement of variable by calling bdput()

In reply to Rashika Kheria
Quoted 1 line · Rashika Kheria

As suggested by Jerome Marchand "The code in reset_store get the block device

The credit should actually go to Minchan. He found the bug. I merely explained it.

Quoted 7 lines

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

This version finally looks good. Thanks for your effort.

Quoted 49 lines

---

This revision fixes the following issues of the previous revision- Proper error handling

 drivers/staging/zram/zram_drv.c |   19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
drivers/staging/zram/zram_drv.c +14 -5
index 012ba15..a1f8b1f 100644
@@ -652,21 +652,30 @@ 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);
 
 	zram_reset_device(zram, true);
 	return len;
+
+out:	
+	bdput(bdev);
+	return ret;
 }
 
 static void __zram_make_request(struct zram *zram, struct bio *bio, int rw)
#32 lore raw

PATCH v10 1/1 Staging: zram: Fix memory leak by refcount mismatch

In reply to a message not in the archive

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.

---

This revision fixes the following issue of the previous revision- Incorrect Subject Line

 drivers/staging/zram/zram_drv.c |   19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
drivers/staging/zram/zram_drv.c +14 -5
index 012ba15..a1f8b1f 100644
@@ -652,21 +652,30 @@ 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);
 
 	zram_reset_device(zram, true);
 	return len;
+
+out:	
+	bdput(bdev);
+	return ret;
 }
 
 static void __zram_make_request(struct zram *zram, struct bio *bio, int rw)
Signature · 1 line
1.7.9.5
#31 lore raw

Re: [PATCH v9 2/2] Staging: zram: Fix decrement of variable by calling bdput()

In reply to Jerome Marchand
Quoted 9 lines · Jerome Marchand
Quoted 1 line · Rashika Kheria

As suggested by Jerome Marchand "The code in reset_store get the block device

The credit should actually go to Minchan. He found the bug. I merely explained it.

Quoted 3 lines

(bdget_disk()) but it does not put it (bdput()) when it's done using it. The usage count is therefore incremented but never decremented."

Quoted 10 lines
Quoted 4 lines

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

This version finally looks good. Thanks for your effort.

Hi,

Thanks Minchan and Jerome for all you support and patience. Your guidance helped me learn more about zram and kernel coding style in general. :)

Thanks,

Signature · 3 lines
Rashika Kheria
B.Tech CSE
IIIT Hyderabad
#33 lore raw

Re: [PATCH v10] Staging: zram: Fix memory leak by refcount mismatch

In reply to Rashika Kheria
Quoted 7 lines · Rashika Kheria

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.

Quoted 8 lines

---

This revision fixes the following issue of the previous revision- Incorrect Subject Line

I'm a bit confused now, I see three different zram patches from you, with different subjects, are they all now just in one patch, this one?

Can you just send me the outstanding zram patches that you have gotten acks from that you want applied, as I'm lost here.

thanks,

greg "easily confused" k-h

#34 lore raw

Re: [OPW kernel] Re: [PATCH v10] Staging: zram: Fix memory leak by refcount mismatch

Quoted 20 lines · Greg Kroah-Hartman
Quoted 7 lines · Rashika Kheria

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.

Quoted 8 lines

---

This revision fixes the following issue of the previous revision- Incorrect Subject Line

I'm a bit confused now, I see three different zram patches from you, with different subjects, are they all now just in one patch, this one?

Quoted 7 lines

Can you just send me the outstanding zram patches that you have gotten acks from that you want applied, as I'm lost here.

thanks,

greg "easily confused" k-h

Hi Greg,

You have already applied the rest two patches for this driver. This is the only patch which is left.

But I think you might have problem applying this because there have been changes in previous patches later i.e you applied v8 of this series, while, maintainers later suggested to change more and hence v9 was also introduced.

Therefore, v9's patch 1 and v10 patch is correct to be applied in the tree.

Thanks,

Signature · 3 lines
Rashika Kheria
B.Tech CSE
IIIT Hyderabad
#35 lore raw

Re: [OPW kernel] Re: [PATCH v10] Staging: zram: Fix memory leak by refcount mismatch

Quoted 35 lines · Rashika Kheria
Quoted 20 lines · Greg Kroah-Hartman
Quoted 7 lines · Rashika Kheria

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.

Quoted 8 lines

---

This revision fixes the following issue of the previous revision- Incorrect Subject Line

I'm a bit confused now, I see three different zram patches from you, with different subjects, are they all now just in one patch, this one?

Quoted 7 lines

Can you just send me the outstanding zram patches that you have gotten acks from that you want applied, as I'm lost here.

thanks,

greg "easily confused" k-h

Hi Greg,

You have already applied the rest two patches for this driver. This is the only patch which is left.

Quoted 6 lines

But I think you might have problem applying this because there have been changes in previous patches later i.e you applied v8 of this series, while, maintainers later suggested to change more and hence v9 was also introduced.

Therefore, v9's patch 1 and v10 patch is correct to be applied in the tree.

Hm, can you please resend them, as I no longer have them in my queue.

thanks,

greg k-h

#36 lore raw

Re: [OPW kernel] Re: [PATCH v10] Staging: zram: Fix memory leak by refcount mismatch

Quoted 41 lines · Greg Kroah-Hartman
Quoted 40 lines · Rashika Kheria
Quoted 20 lines · Greg Kroah-Hartman
Quoted 7 lines · Rashika Kheria

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.

Quoted 8 lines

---

This revision fixes the following issue of the previous revision- Incorrect Subject Line

I'm a bit confused now, I see three different zram patches from you, with different subjects, are they all now just in one patch, this one?

Quoted 7 lines

Can you just send me the outstanding zram patches that you have gotten acks from that you want applied, as I'm lost here.

thanks,

greg "easily confused" k-h

Hi Greg,

You have already applied the rest two patches for this driver. This is the only patch which is left.

But I think you might have problem applying this because there have been changes in previous patches later i.e you applied v8 of this series, while, maintainers later suggested to change more and hence v9 was also introduced.

Quoted 8 lines
Quoted 1 line

Therefore, v9's patch 1 and v10 patch is correct to be applied in the tree.

Hm, can you please resend them, as I no longer have them in my queue.

thanks,

greg k-h

Resent.

Thanks,

Signature · 3 lines
Rashika Kheria
B.Tech CSE
IIIT Hyderabad