zram: avoid null access when fail to alloc meta
Subsystems (3)
Minchan Kim
maintainerPATCH 1/2 zram: avoid null access when fail to alloc meta
zram_meta_alloc could be failed so caller should check it. Otherwise, your system will be hang.
--- drivers/block/zram/zram_drv.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/block/zram/zram_drv.c +2 -0
@@ -554,6 +554,8 @@ static ssize_t disksize_store(struct device *dev, disksize =PAGE_ALIGN (disksize); meta =zram_meta_alloc (disksize); +if (!meta) +return -ENOMEM ;down_write (&zram->init_lock);if (init_done (zram)) {up_write (&zram->init_lock);
Signature · 1 line
1.8.5.3
Minchan Kim
maintainerPATCH 2/2 zram: delete zram_init_device() function
From: Sergey Senozhatsky <[email protected]>
allocate new `zram_meta' in disksize_store() only for uninitialised zram device, saving a number of allocations and deallocations in case if disksize_store() was called on currently used device. at the same time zram_meta stack variable is not necessary, because we can set ->meta directly. there is also no need in setting QUEUE_FLAG_NONROT queue on every disksize_store(), set it once during device creation.
[[email protected]]: handled zram->meta alloc fail case.
--- drivers/block/zram/zram_drv.c | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-)
drivers/block/zram/zram_drv.c +9 -16
@@ -533,40 +533,32 @@ static void zram_reset_device(struct zram *zram, bool reset_capacity)up_write (&zram->init_lock); } -static void zram_init_device (struct zram *zram,struct zram_meta *meta) -{ -/* zram devices sort of resembles non-rotational disks */ -queue_flag_set_unlocked (QUEUE_FLAG_NONROT , zram->disk->queue); - zram->meta = meta; -pr_debug ("Initialization done!\n" ); -} -static ssize_t disksize_store (struct device *dev,struct device_attribute *attr,const char *buf,size_t len) {u64 disksize; -struct zram_meta *meta;struct zram *zram =dev_to_zram (dev); disksize =memparse (buf,NULL );if (!disksize)return -EINVAL ; - disksize =PAGE_ALIGN (disksize); - meta =zram_meta_alloc (disksize); -if (!meta) -return -ENOMEM ;down_write (&zram->init_lock);if (init_done (zram)) {up_write (&zram->init_lock); -zram_meta_free (meta);pr_info ("Cannot change disksize for initialized device\n" );return -EBUSY ; } + disksize =PAGE_ALIGN (disksize); + zram->meta =zram_meta_alloc (disksize); +if (!zram->meta) { +up_write (&zram->init_lock); +return -ENOMEM ; + } + zram->disksize = disksize;set_capacity (zram->disk, zram->disksize >>SECTOR_SHIFT ); -zram_init_device (zram, meta);up_write (&zram->init_lock);return len;
@@ -776,7 +768,8 @@ static int create_device(struct zram *zram, int device_id)/* Actual capacity set using syfs (/sys/block/zram<id>/disksize */ set_capacity (zram->disk,0 ); - +/* zram devices sort of resembles non-rotational disks */ +queue_flag_set_unlocked (QUEUE_FLAG_NONROT , zram->disk->queue);/* * To ensure that we always get PAGE_SIZE aligned * and n*PAGE_SIZED sized I/O requests.
Signature · 1 line
1.8.5.3
Re: [PATCH 2/2] zram: delete zram_init_device() function
Quoted 9 lines · Minchan Kim
From: Sergey Senozhatsky <[email protected]>
allocate new `zram_meta' in
disksize_store()only for uninitialised zram device, saving a number of allocations and deallocations in case ifdisksize_store()was called on currently used device. at the same timezram_metastack variable is not necessary, because we can set ->meta directly. there is also no need in settingQUEUE_FLAG_NONROTqueue on everydisksize_store(), set it once during device creation.
Quoted 3 lines
[[email protected]]: handled
zram->metaalloc fail case.Signed-off-by Sergey Senozhatsky <[email protected]>Signed-off-by Minchan Kim <[email protected]>
Quoted 67 lines
--- drivers/block/zram/zram_drv.c | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-)drivers/block/zram/zram_drv.c +9
-16@@ -533,40 +533,32 @@ static void zram_reset_device(struct zram *zram, bool reset_capacity)up_write (&zram->init_lock); } -static void zram_init_device (struct zram *zram,struct zram_meta *meta) -{ -/* zram devices sort of resembles non-rotational disks */ -queue_flag_set_unlocked (QUEUE_FLAG_NONROT , zram->disk->queue); - zram->meta = meta; -pr_debug ("Initialization done!\n" ); -} -static ssize_t disksize_store (struct device *dev,struct device_attribute *attr,const char *buf,size_t len) {u64 disksize; -struct zram_meta *meta;struct zram *zram =dev_to_zram (dev); disksize =memparse (buf,NULL );if (!disksize)return -EINVAL ; - disksize =PAGE_ALIGN (disksize); - meta =zram_meta_alloc (disksize); -if (!meta) -return -ENOMEM ;down_write (&zram->init_lock);if (init_done (zram)) {up_write (&zram->init_lock); -zram_meta_free (meta);pr_info ("Cannot change disksize for initialized device\n" );return -EBUSY ; } + disksize =PAGE_ALIGN (disksize); + zram->meta =zram_meta_alloc (disksize); +if (!zram->meta) { +up_write (&zram->init_lock); +return -ENOMEM ; + } + zram->disksize = disksize;set_capacity (zram->disk, zram->disksize >>SECTOR_SHIFT ); -zram_init_device (zram, meta);up_write (&zram->init_lock);return len;@@ -776,7 +768,8 @@ static int create_device(struct zram *zram, int device_id)/* Actual capacity set using syfs (/sys/block/zram<id>/disksize */ set_capacity (zram->disk,0 ); - +/* zram devices sort of resembles non-rotational disks */ +queue_flag_set_unlocked (QUEUE_FLAG_NONROT , zram->disk->queue);/* * To ensure that we always get PAGE_SIZE aligned * and n*PAGE_SIZED sized I/O requests.
Quoted 5 lines · Minchan Kim
zram_meta_alloccould be failed so caller should check it. Otherwise, your system will be hang.Cc <[email protected]>Signed-off-by Minchan Kim <[email protected]>
Quoted 18 lines
--- drivers/block/zram/zram_drv.c | 2 ++ 1 file changed, 2 insertions(+)drivers/block/zram/zram_drv.c +2
-0@@ -554,6 +554,8 @@ static ssize_t disksize_store(struct device *dev, disksize =PAGE_ALIGN (disksize); meta =zram_meta_alloc (disksize); +if (!meta) +return -ENOMEM ;down_write (&zram->init_lock);if (init_done (zram)) {up_write (&zram->init_lock);