Skip to contentSkip to conversation

zram: fix backing device setup issue

PATCH Applied Kairui Song 1 participant 3 messages Dec 9, 2024

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover zram: fix backing device setup issue

From: Kairui Song <[email protected]>

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/[email protected]/
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(-)
Signature · 1 line
2.47.1
#1 lore raw

PATCH v2 1/2 zram: refuse to use zero sized block device as backing device

From: Kairui Song <[email protected]>

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")
---
 drivers/block/zram/zram_drv.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/block/zram/zram_drv.c +6 -0
index 3dee026988dc..e86cc3d2f4d2 100644
@@ -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) {
Signature · 1 line
2.47.1
#2 lore raw

PATCH v2 2/2 zram: fix uninitialized ZRAM not releasing backing device

In reply to Kairui Song

From: Kairui Song <[email protected]>

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")
---
 drivers/block/zram/zram_drv.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/block/zram/zram_drv.c +4 -5
index e86cc3d2f4d2..45df5eeabc5e 100644
@@ -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);
 
Signature · 1 line
2.47.1