linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/2] zram: fix backing device setup issue
@ 2024-12-09 16:57 Kairui Song
  2024-12-09 16:57 ` [PATCH v2 1/2] zram: refuse to use zero sized block device as backing device Kairui Song
  2024-12-09 16:57 ` [PATCH v2 2/2] zram: fix uninitialized ZRAM not releasing " Kairui Song
  0 siblings, 2 replies; 3+ messages in thread
From: Kairui Song @ 2024-12-09 16:57 UTC (permalink / raw)
  To: linux-mm
  Cc: Minchan Kim, Sergey Senozhatsky, Andrew Morton, linux-block,
	linux-kernel, Kairui Song

From: Kairui Song <kasong@tencent.com>

This series fixes two bugs of backing device setting:

- ZRAM should reject using a zero sized (or the uninitialized ZRAM
  device itself) as the backing device.
- Fix backing device leaking when removing a uninitialized ZRAM
  device.

V1: https://lore.kernel.org/linux-mm/20241204180224.31069-1-ryncsn@gmail.com/
Update from V1:
- Collect Review-by.
- Update fix for issue 2 as suggested by Sergey Senozhatsky.

Kairui Song (2):
  zram: refuse to use zero sized block device as backing device
  zram: fix uninitialized ZRAM not releasing backing device

 drivers/block/zram/zram_drv.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

-- 
2.47.1



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

* [PATCH v2 1/2] zram: refuse to use zero sized block device as backing device
  2024-12-09 16:57 [PATCH v2 0/2] zram: fix backing device setup issue Kairui Song
@ 2024-12-09 16:57 ` Kairui Song
  2024-12-09 16:57 ` [PATCH v2 2/2] zram: fix uninitialized ZRAM not releasing " Kairui Song
  1 sibling, 0 replies; 3+ messages in thread
From: Kairui Song @ 2024-12-09 16:57 UTC (permalink / raw)
  To: linux-mm
  Cc: Minchan Kim, Sergey Senozhatsky, Andrew Morton, linux-block,
	linux-kernel, Kairui Song, Desheng Wu, stable

From: Kairui Song <kasong@tencent.com>

Setting a zero sized block device as backing device is pointless, and
one can easily create a recursive loop by setting the uninitialized
ZRAM device itself as its own backing device by (zram0 is uninitialized):

    echo /dev/zram0 > /sys/block/zram0/backing_dev

It's definitely a wrong config, and the module will pin itself,
kernel should refuse doing so in the first place.

By refusing to use zero sized device we avoided misuse cases
including this one above.

Fixes: 013bf95a83ec ("zram: add interface to specif backing device")
Reported-by: Desheng Wu <deshengwu@tencent.com>
Signed-off-by: Kairui Song <kasong@tencent.com>
Reviewed-by: Sergey Senozhatsky <senozhatsky@chromium.org>
Cc: stable@vger.kernel.org
---
 drivers/block/zram/zram_drv.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
index 3dee026988dc..e86cc3d2f4d2 100644
--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -614,6 +614,12 @@ static ssize_t backing_dev_store(struct device *dev,
 	}
 
 	nr_pages = i_size_read(inode) >> PAGE_SHIFT;
+	/* Refuse to use zero sized device (also prevents self reference) */
+	if (!nr_pages) {
+		err = -EINVAL;
+		goto out;
+	}
+
 	bitmap_sz = BITS_TO_LONGS(nr_pages) * sizeof(long);
 	bitmap = kvzalloc(bitmap_sz, GFP_KERNEL);
 	if (!bitmap) {
-- 
2.47.1



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

* [PATCH v2 2/2] zram: fix uninitialized ZRAM not releasing backing device
  2024-12-09 16:57 [PATCH v2 0/2] zram: fix backing device setup issue Kairui Song
  2024-12-09 16:57 ` [PATCH v2 1/2] zram: refuse to use zero sized block device as backing device Kairui Song
@ 2024-12-09 16:57 ` Kairui Song
  1 sibling, 0 replies; 3+ messages in thread
From: Kairui Song @ 2024-12-09 16:57 UTC (permalink / raw)
  To: linux-mm
  Cc: Minchan Kim, Sergey Senozhatsky, Andrew Morton, linux-block,
	linux-kernel, Kairui Song, Desheng Wu, stable

From: Kairui Song <kasong@tencent.com>

Setting backing device is done before ZRAM initialization.
If we set the backing device, then remove the ZRAM module without
initializing the device, the backing device reference will be leaked
and the device will be hold forever.

Fix this by always reset the ZRAM fully on rmmod or reset store.

Fixes: 013bf95a83ec ("zram: add interface to specif backing device")
Reported-by: Desheng Wu <deshengwu@tencent.com>
Suggested-by: Sergey Senozhatsky <senozhatsky@chromium.org>
Signed-off-by: Kairui Song <kasong@tencent.com>
Cc: stable@vger.kernel.org
---
 drivers/block/zram/zram_drv.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
index e86cc3d2f4d2..45df5eeabc5e 100644
--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -1444,12 +1444,16 @@ static void zram_meta_free(struct zram *zram, u64 disksize)
 	size_t num_pages = disksize >> PAGE_SHIFT;
 	size_t index;
 
+	if (!zram->table)
+		return;
+
 	/* Free all pages that are still in this zram device */
 	for (index = 0; index < num_pages; index++)
 		zram_free_page(zram, index);
 
 	zs_destroy_pool(zram->mem_pool);
 	vfree(zram->table);
+	zram->table = NULL;
 }
 
 static bool zram_meta_alloc(struct zram *zram, u64 disksize)
@@ -2326,11 +2330,6 @@ static void zram_reset_device(struct zram *zram)
 
 	zram->limit_pages = 0;
 
-	if (!init_done(zram)) {
-		up_write(&zram->init_lock);
-		return;
-	}
-
 	set_capacity_and_notify(zram->disk, 0);
 	part_stat_set_all(zram->disk->part0, 0);
 
-- 
2.47.1



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

end of thread, other threads:[~2024-12-09 16:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-12-09 16:57 [PATCH v2 0/2] zram: fix backing device setup issue Kairui Song
2024-12-09 16:57 ` [PATCH v2 1/2] zram: refuse to use zero sized block device as backing device Kairui Song
2024-12-09 16:57 ` [PATCH v2 2/2] zram: fix uninitialized ZRAM not releasing " Kairui Song

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