Skip to contentSkip to conversation

zram: fix backing device setup issue

PATCH Superseded Kairui Song 2 participants 6 messages Dec 4, 2024 – Dec 9, 2024

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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.

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 | 9 +++++++++
 1 file changed, 9 insertions(+)
Signature · 1 line
2.47.0
#1 lore raw

PATCH 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 0ca6d55c9917..dd48df5b97c8 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.0
maintainer Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] zram: refuse to use zero sized block device as backing device

Quoted 11 lines · Kairui Song

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.

Quoted 7 lines

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

Fixes 013bf95a83ec ("zram: add interface to specif backing device")
#3 lore raw

PATCH 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 check and release the backing device when resetting or removing ZRAM.

Fixes 013bf95a83ec ("zram: add interface to specif backing device")
---
 drivers/block/zram/zram_drv.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/block/zram/zram_drv.c +3 -0
index dd48df5b97c8..dfe9a994e437 100644
@@ -2335,6 +2335,9 @@ static void zram_reset_device(struct zram *zram)
 	zram->limit_pages = 0;
 
 	if (!init_done(zram)) {
+		/* Backing device could be set before ZRAM initialization. */
+		reset_bdev(zram);
+
 		up_write(&zram->init_lock);
 		return;
 	}
Signature · 1 line
2.47.0
#4 lore raw

Re: [PATCH 2/2] zram: fix uninitialized ZRAM not releasing backing device

Quoted 24 lines · 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 check and release the backing device when resetting or removing ZRAM.

Fixes 013bf95a83ec ("zram: add interface to specif backing device")
---
 drivers/block/zram/zram_drv.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/block/zram/zram_drv.c +0 -0
index dd48df5b97c8..dfe9a994e437 100644

drivers/block/zram/zram_drv.c static void zram_reset_device(struct zram *zram) · line 2343

Quoted 9 lines
drivers/block/zram/zram_drv.c +3 -0
@@ -2335,6 +2335,9 @@ static void zram_reset_device(struct zram *zram)
 	zram->limit_pages = 0;
 
 	if (!init_done(zram)) {
+		/* Backing device could be set before ZRAM initialization. */
+		reset_bdev(zram);
+
 		up_write(&zram->init_lock);
 		return;
 	}

So here I think we better remove that if entirely and always reset the device. Something like this (untested):

---

drivers/block/zram/zram_drv.c +4 -6
index 0ca6d55c9917..8773b12afc9d 100644
@@ -1438,12 +1438,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)
@@ -2327,12 +2331,6 @@ static void zram_reset_device(struct zram *zram)
 	down_write(&zram->init_lock);
 
 	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);
 
#5 lore raw

Re: [PATCH 2/2] zram: fix uninitialized ZRAM not releasing backing device

Quoted 66 lines · Sergey Senozhatsky
Quoted 23 lines · 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 check and release the backing device when resetting or removing ZRAM.

Fixes 013bf95a83ec ("zram: add interface to specif backing device")
---
 drivers/block/zram/zram_drv.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/block/zram/zram_drv.c +0 -0
index dd48df5b97c8..dfe9a994e437 100644

drivers/block/zram/zram_drv.c static void zram_reset_device(struct zram *zram)

Quoted 10 lines
drivers/block/zram/zram_drv.c +3 -1
@@ -2335,6 +2335,9 @@ static void zram_reset_device(struct zram *zram)
      zram->limit_pages = 0;
 
      if (!init_done(zram)) {
+             /* Backing device could be set before ZRAM initialization. */
+             reset_bdev(zram);
+
              up_write(&zram->init_lock);
              return;
      }
--

So here I think we better remove that if entirely and always reset the device. Something like this (untested):

---

drivers/block/zram/zram_drv.c +4 -0
index 0ca6d55c9917..8773b12afc9d 100644
@@ -1438,12 +1438,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)
@@ -2327,12 +2331,6 @@ static void zram_reset_device(struct zram *zram)
        down_write(&zram->init_lock);
 

drivers/block/zram/zram_drv.c static void zram_reset_device(struct zram *zram) · line 2336

Quoted 10 lines
drivers/block/zram/zram_drv.c +0 -6
        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);
 

Thanks for the suggestion, I've tested it and it works well. Will send a V2 shortly.