zram: fix backing device setup issue
Subsystems (4)
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
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.
--- drivers/block/zram/zram_drv.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/block/zram/zram_drv.c +6 -0
@@ -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
PATCH 2/2 zram: fix uninitialized ZRAM not releasing backing device
In reply to Kairui SongFrom: 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.
--- drivers/block/zram/zram_drv.c | 3 +++ 1 file changed, 3 insertions(+)
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 ; }
Signature · 1 line
2.47.0
Re: [PATCH 1/2] zram: refuse to use zero sized block device as backing device
In reply to Kairui SongQuoted 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_devIt'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")Reported-by Desheng Wu <[email protected]>Signed-off-by Kairui Song <[email protected]>
Sergey Senozhatsky
maintainerRe: [PATCH 2/2] zram: fix uninitialized ZRAM not releasing backing device
In reply to Kairui SongQuoted 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")Reported-by Desheng Wu <[email protected]>Signed-off-by Kairui Song <[email protected]>--- drivers/block/zram/zram_drv.c | 3 +++ 1 file changed, 3 insertions(+)drivers/block/zram/zram_drv.c +0
-0
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
@@ -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 );
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")Reported-by Desheng Wu <[email protected]>Signed-off-by Kairui Song <[email protected]>--- drivers/block/zram/zram_drv.c | 3 +++ 1 file changed, 3 insertions(+)drivers/block/zram/zram_drv.c +0
-0drivers/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@@ -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
-6zram->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.