small bugfixes and code improvements for zram
cover small bugfixes and code improvements for zram
This patchset is to fix issues in zram found by code inspection. There is still one more issue left: should we repalce zram_stat64_xxx() with atomic64_xxx()?
Jiang Liu (8):
zram: simplify and optimize zram_to_dev()
zram: avoid invalid memory access in zram_exit()
zram: use zram->lock to protect zram_free_page() in swap free notify
path
zram: destroy all devices on error recovery path in zram_init()
zram: avoid double free in error recovery path of zram_bvec_write()
zram: avoid access beyond the zram device
zram: optimize memory operations with clear_page()/copy_page()
zram: protect sysfs handler from invalid memory access
drivers/staging/zram/zram_drv.c | 41 +++++++++++++++++++++++++-------------- drivers/staging/zram/zram_sysfs.c | 15 ++++---------- 2 files changed, 30 insertions(+), 26 deletions(-)
Signature · 1 line
1.8.1.2
RFC PATCH 1/8 zram: simplify and optimize zram_to_dev()
--- drivers/staging/zram/zram_sysfs.c | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-)
drivers/staging/zram/zram_sysfs.c +2 -11
@@ -30,18 +30,9 @@ static u64 zram_stat64_read(struct zram *zram, u64 *v)return val; } -static struct zram *dev_to_zram (struct device *dev) +static inline struct zram *dev_to_zram (struct device *dev) { -int i; -struct zram *zram =NULL ; - -for (i =0 ; i <zram_get_num_devices (); i++) { - zram = &zram_devices[i]; -if (disk_to_dev (zram->disk) == dev) -break ; - } - -return zram; +return (struct zram *)dev_to_disk (dev)->private_data; } static ssize_t disksize_show(struct device *dev,
Signature · 1 line
1.8.1.2
Re: [RFC PATCH v1 1/8] zram: simplify and optimize zram_to_dev()
Hi,
Please write a commit message, no matter how straightforward a patch may seem to you.
Also the subject suffers from dyslexia: it's dev_to_zram, not zram_to_dev.
Thanks,
Jerome
Quoted 31 lines · Jiang Liu
Signed-off-by Jiang Liu <[email protected]>--- drivers/staging/zram/zram_sysfs.c | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-)drivers/staging/zram/zram_sysfs.c +2
-11@@ -30,18 +30,9 @@ static u64 zram_stat64_read(struct zram *zram, u64 *v)return val; } -static struct zram *dev_to_zram (struct device *dev) +static inline struct zram *dev_to_zram (struct device *dev) { -int i; -struct zram *zram =NULL ; - -for (i =0 ; i <zram_get_num_devices (); i++) { - zram = &zram_devices[i]; -if (disk_to_dev (zram->disk) == dev) -break ; - } - -return zram; +return (struct zram *)dev_to_disk (dev)->private_data; } static ssize_t disksize_show(struct device *dev,
Re: [RFC PATCH v1 1/8] zram: simplify and optimize zram_to_dev()
Quoted 8 lines · Jerome Marchand
Hi,
Please write a commit message, no matter how straightforward a patch may seem to you.
Also the subject suffers from dyslexia: it'sdev_to_zram, notzram_to_dev.Thanks,
Jerome
Hi Jerome,
Thanks for review, will fix it in next version.
Regards!
Gerry
Quoted 34 lines · Jiang Liu
Quoted 31 lines
Signed-off-by Jiang Liu <[email protected]>--- drivers/staging/zram/zram_sysfs.c | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-)drivers/staging/zram/zram_sysfs.c +2
-11@@ -30,18 +30,9 @@ static u64 zram_stat64_read(struct zram *zram, u64 *v)return val; } -static struct zram *dev_to_zram (struct device *dev) +static inline struct zram *dev_to_zram (struct device *dev) { -int i; -struct zram *zram =NULL ; - -for (i =0 ; i <zram_get_num_devices (); i++) { - zram = &zram_devices[i]; -if (disk_to_dev (zram->disk) == dev) -break ; - } - -return zram; +return (struct zram *)dev_to_disk (dev)->private_data; } static ssize_t disksize_show(struct device *dev,
RFC PATCH 2/8 zram: avoid invalid memory access in zram_exit()
In reply to Jiang LiuMemory for zram->disk object may have already been freed after returning from destroy_device(zram), then it's unsafe for zram_reset_device(zram) to access zram->disk again.
Fix it by holding an extra reference to zram->disk before calling destroy_device(zram).
--- drivers/staging/zram/zram_drv.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/staging/zram/zram_drv.c +2 -0
@@ -727,8 +727,10 @@ static void __exit zram_exit(void)for (i =0 ; i < num_devices; i++) { zram = &zram_devices[i]; +get_disk (zram->disk);destroy_device (zram);zram_reset_device (zram); +put_disk (zram->disk); }unregister_blkdev (zram_major,"zram" );
Signature · 1 line
1.8.1.2
Re: [RFC PATCH v1 2/8] zram: avoid invalid memory access in zram_exit()
Quoted 17 lines · Jiang Liu
Memory for
zram->diskobject may have already been freed after returning fromdestroy_device(zram), then it's unsafe forzram_reset_device(zram)to accesszram->diskagain.Fix it by holding an extra reference to
zram->diskbefore callingdestroy_device(zram).Signed-off-by Jiang Liu <[email protected]>--- drivers/staging/zram/zram_drv.c | 2 ++ 1 file changed, 2 insertions(+)drivers/staging/zram/zram_drv.c +0
-0
drivers/staging/zram/zram_drv.c static void __exit zram_exit(void) · line 733
Quoted 7 lines
drivers/staging/zram/zram_drv.c +2
-0@@ -727,8 +727,10 @@ static void __exit zram_exit(void)for (i =0 ; i < num_devices; i++) { zram = &zram_devices[i]; +get_disk (zram->disk);destroy_device (zram);zram_reset_device (zram); +put_disk (zram->disk);
Can't we simple reverse calling order of above two functions?
zram_reset_device(zram);
destroy_device(zram);Signature · 2 lines
Kind regards, Minchan Kim
Re: [RFC PATCH v1 2/8] zram: avoid invalid memory access in zram_exit()
Quoted 28 lines · Minchan Kim
Quoted 17 lines · Jiang Liu
Memory for
zram->diskobject may have already been freed after returning fromdestroy_device(zram), then it's unsafe forzram_reset_device(zram)to accesszram->diskagain.Fix it by holding an extra reference to
zram->diskbefore callingdestroy_device(zram).Signed-off-by Jiang Liu <[email protected]>--- drivers/staging/zram/zram_drv.c | 2 ++ 1 file changed, 2 insertions(+)drivers/staging/zram/zram_drv.c +0
-0drivers/staging/zram/zram_drv.c static void __exit zram_exit(void) · line 733
Quoted 7 lines
drivers/staging/zram/zram_drv.c +2
-0@@ -727,8 +727,10 @@ static void __exit zram_exit(void)for (i =0 ; i < num_devices; i++) { zram = &zram_devices[i]; +get_disk (zram->disk);destroy_device (zram);zram_reset_device (zram); +put_disk (zram->disk);Can't we simple reverse calling order of above two functions?
Quoted 3 lines
zram_reset_device(zram);
destroy_device(zram);Hi Minchan,
We can't solve this bug by changing the order of the two functions.
If we change the order, it will cause corner cases to zram sysfs handler,
which will be hard to solve too.
Regards!
Gerry
RFC PATCH 3/8 zram: use zram->lock to protect zram_free_page() in swap free notify path
In reply to Jiang Liuzram_free_page() is protected by down_write(&zram->lock) when called by zram_bvec_write(), but there's no such protection when called by zram_slot_free_notify(), which may cause wrong states to zram object.
So enhance zram_slot_free_notify() to acquire writer lock on zram->lock before calling zram_free_page().
--- drivers/staging/zram/zram_drv.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/staging/zram/zram_drv.c +2 -0
@@ -582,7 +582,9 @@ static void zram_slot_free_notify(struct block_device *bdev,struct zram *zram; zram = bdev->bd_disk->private_data; +down_write (&zram->lock);zram_free_page (zram, index); +up_write (&zram->lock);zram_stat64_inc (zram, &zram->stats.notify_free); }
Signature · 1 line
1.8.1.2
RFC PATCH 4/8 zram: destroy all devices on error recovery path in zram_init()
In reply to Jiang LiuOn error recovery path of zram_init(), it leaks the zram device object causing the failure.
--- drivers/staging/zram/zram_drv.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/staging/zram/zram_drv.c +2 -2
@@ -712,8 +712,8 @@ static int __init zram_init(void)return 0 ; free_devices: -while (dev_id) -destroy_device (&zram_devices[--dev_id]); +while (dev_id >=0 ) +destroy_device (&zram_devices[dev_id--]);kfree (zram_devices); unregister:unregister_blkdev (zram_major,"zram" );
Signature · 1 line
1.8.1.2
Re: [RFC PATCH v1 4/8] zram: destroy all devices on error recovery path in zram_init()
Everyone stop putting RFC on their bugfixes! :P No one wants to pre-review patches.
Quoted 3 lines · Jiang Liu
On error recovery path of
zram_init(), it leaks the zram device object causing the failure.
This is a real bug but the fix isn't right. The object causing the failure has only been partially set up. And in some cases it has been partially cleaned up, for example we could end up releasing ->queue twice.
The better way is to make create_device() clean up after itself completely instead of only partly and sometimes.
regards,
dan carpenter
Re: [RFC PATCH v1 4/8] zram: destroy all devices on error recovery path in zram_init()
Quoted 13 lines · Dan Carpenter
Everyone stop putting RFC on their bugfixes! :P No one wants to pre-review patches.
Quoted 3 lines · Jiang Liu
On error recovery path of
zram_init(), it leaks the zram device object causing the failure.This is a real bug but the fix isn't right. The object causing the failure has only been partially set up. And in some cases it has been partially cleaned up, for example we could end up releasing ->queue twice.
Quoted 6 lines
The better way is to make
create_device()clean up after itself completely instead of only partly and sometimes.regards,
dan carpenter
Hi Dan,
Thanks for your review, will fix it in next version.
Regards!
Gerry
RFC PATCH 5/8 zram: avoid double free in error recovery path of zram_bvec_write()
In reply to Jiang Liu--- drivers/staging/zram/zram_drv.c | 2 -- 1 file changed, 2 deletions(-)
drivers/staging/zram/zram_drv.c +0 -2
@@ -272,8 +272,6 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,if (page_zero_filled (uncmem)) {kunmap_atomic (user_mem); -if (is_partial_io (bvec)) -kfree (uncmem); zram->stats.pages_zero++;zram_set_flag (meta, index,ZRAM_ZERO ); ret =0 ;
Signature · 1 line
1.8.1.2
Re: [RFC PATCH v1 5/8] zram: avoid double free in error recovery path of zram_bvec_write()
The patch seems right, but the title is wrong: this is not a error recovery path. Also, the description is missing again.
Jerome
Quoted 19 lines · Jiang Liu
Signed-off-by Jiang Liu <[email protected]>--- drivers/staging/zram/zram_drv.c | 2 -- 1 file changed, 2 deletions(-)drivers/staging/zram/zram_drv.c +0
-2@@ -272,8 +272,6 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,if (page_zero_filled (uncmem)) {kunmap_atomic (user_mem); -if (is_partial_io (bvec)) -kfree (uncmem); zram->stats.pages_zero++;zram_set_flag (meta, index,ZRAM_ZERO ); ret =0 ;
RFC PATCH 6/8 zram: avoid access beyond the zram device
In reply to Jiang LiuFunction valid_io_request() should verify the entire request doesn't exceed the zram device, otherwise it will cause invalid memory access.
--- drivers/staging/zram/zram_drv.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/staging/zram/zram_drv.c +4 -0
@@ -428,6 +428,10 @@ static inline int valid_io_request(struct zram *zram, struct bio *bio)return 0 ; } +if (unlikely ((bio->bi_sector <<SECTOR_SHIFT ) + bio->bi_size >= + zram->disksize)) +return 0 ; +/* I/O request is valid */ return 1 ; }
Signature · 1 line
1.8.1.2
Re: [RFC PATCH v1 6/8] zram: avoid access beyond the zram device
Quoted 13 lines · Jiang Liu
Function
valid_io_request()should verify the entire request doesn't exceed the zram device, otherwise it will cause invalid memory access.Signed-off-by Jiang Liu <[email protected]>--- drivers/staging/zram/zram_drv.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/staging/zram/zram_drv.c +0
-0
drivers/staging/zram/zram_drv.c static inline int valid_io_request(struct zram *zram, struct bio *bio) · line 434
Quoted 7 lines
drivers/staging/zram/zram_drv.c +4
-0@@ -428,6 +428,10 @@ static inline int valid_io_request(struct zram *zram, struct bio *bio)return 0 ; } +if (unlikely ((bio->bi_sector <<SECTOR_SHIFT ) + bio->bi_size >= + zram->disksize)) +return 0 ; +
This test make the first line of previous test redundant. Why not just update it like the following:
- (bio->bi_sector >= (zram->disksize >> SECTOR_SHIFT)) ||
+ ((bio->bi_sector << SECTOR_SHIFT) + bio->bi_size >=
+ zram->disksize)) ||
Jerome
Quoted 4 lines
drivers/staging/zram/zram_drv.c +0
-0/* I/O request is valid */ return 1 ; }
Re: [RFC PATCH v1 6/8] zram: avoid access beyond the zram device
Quoted 30 lines · Jerome Marchand
Quoted 13 lines · Jiang Liu
Function
valid_io_request()should verify the entire request doesn't exceed the zram device, otherwise it will cause invalid memory access.Signed-off-by Jiang Liu <[email protected]>--- drivers/staging/zram/zram_drv.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/staging/zram/zram_drv.c +0
-0drivers/staging/zram/zram_drv.c static inline int valid_io_request(struct zram *zram, struct bio *bio) · line 434
Quoted 7 lines
drivers/staging/zram/zram_drv.c +4
-0@@ -428,6 +428,10 @@ static inline int valid_io_request(struct zram *zram, struct bio *bio)return 0 ; } +if (unlikely ((bio->bi_sector <<SECTOR_SHIFT ) + bio->bi_size >= + zram->disksize)) +return 0 ; +This test make the first line of previous test redundant. Why not just update it like the following:
(quoted diff) +2
-1- (bio->bi_sector >= (zram->disksize >> SECTOR_SHIFT)) || + ((bio->bi_sector << SECTOR_SHIFT) + bio->bi_size >= + zram->disksize)) ||
Quoted 1 line
Jerome
Hi Jerome,
I think the test "bio->bi_sector >= (zram->disksize >>
SECTOR_SHIFT)" is still
needed to protect "(bio->bi_sector << SECTOR_SHIFT) + bio->bi_size" from wrapping
around.
Regards!
Gerry
Quoted 6 lines
Quoted 5 lines
drivers/staging/zram/zram_drv.c +0
-0/* I/O request is valid */ return 1 ; }
Re: [RFC PATCH v1 6/8] zram: avoid access beyond the zram device
Quoted 31 lines · Jiang Liu
Quoted 30 lines · Jerome Marchand
Quoted 13 lines · Jiang Liu
Function
valid_io_request()should verify the entire request doesn't exceed the zram device, otherwise it will cause invalid memory access.Signed-off-by Jiang Liu <[email protected]>--- drivers/staging/zram/zram_drv.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/staging/zram/zram_drv.c +0
-0drivers/staging/zram/zram_drv.c static inline int valid_io_request(struct zram *zram, struct bio *bio) · line 434
Quoted 7 lines
drivers/staging/zram/zram_drv.c +4
-0@@ -428,6 +428,10 @@ static inline int valid_io_request(struct zram *zram, struct bio *bio)return 0 ; } +if (unlikely ((bio->bi_sector <<SECTOR_SHIFT ) + bio->bi_size >= + zram->disksize)) +return 0 ; +This test make the first line of previous test redundant. Why not just update it like the following:
(quoted diff) +2
-1- (bio->bi_sector >= (zram->disksize >> SECTOR_SHIFT)) || + ((bio->bi_sector << SECTOR_SHIFT) + bio->bi_size >= + zram->disksize)) ||
Quoted 7 lines
Quoted 1 line
Jerome
Hi Jerome,
I think the test "bio->bi_sector >= (zram->disksize >>
SECTOR_SHIFT)" is still
needed to protect "(bio->bi_sector<<SECTOR_SHIFT) +bio->bi_size" from wrapping
around.
Good point, but I don't see how this is going to catch all the possible values that overflow. You still need an explicit overflow test (bio->bi_sector << SECTOR_SHIFT) + bio->bi_size < bio->bi_size), at which point the first test would be useless.
Jerome
Quoted 11 lines
Regards!
GerryQuoted 6 lines
Quoted 5 lines
drivers/staging/zram/zram_drv.c +0
-0/* I/O request is valid */ return 1 ; }
RFC PATCH 7/8 zram: optimize memory operations with clear_page()/copy_page()
In reply to Jiang Liu--- drivers/staging/zram/zram_drv.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-)
drivers/staging/zram/zram_drv.c +16 -11
@@ -128,23 +128,26 @@ static void zram_free_page(struct zram *zram, size_t index) meta->table[index].size =0 ; } +static inline int is_partial_io (struct bio_vec *bvec) +{ +return bvec->bv_len !=PAGE_SIZE ; +} +static void handle_zero_page (struct bio_vec *bvec) {struct page *page = bvec->bv_page;void *user_mem; user_mem =kmap_atomic (page); -memset (user_mem + bvec->bv_offset,0 , bvec->bv_len); +if (is_partial_io (bvec)) +memset (user_mem + bvec->bv_offset,0 , bvec->bv_len); +else +clear_page (user_mem);kunmap_atomic (user_mem);flush_dcache_page (page); } -static inline int is_partial_io (struct bio_vec *bvec) -{ -return bvec->bv_len !=PAGE_SIZE ; -} -static int zram_decompress_page (struct zram *zram,char *mem,u32 index) {int ret =LZO_E_OK ;
@@ -154,13 +157,13 @@ static int zram_decompress_page(struct zram *zram, char *mem, u32 index)unsigned long handle = meta->table[index].handle;if (!handle ||zram_test_flag (meta, index,ZRAM_ZERO )) { -memset (mem,0 ,PAGE_SIZE ); +clear_page (mem);return 0 ; } cmem =zs_map_object (meta->mem_pool, handle,ZS_MM_RO );if (meta->table[index].size ==PAGE_SIZE ) -memcpy (mem, cmem,PAGE_SIZE ); +copy_page (mem, cmem);else ret =lzo1x_decompress_safe (cmem, meta->table[index].size, mem, &clen);
@@ -309,11 +312,13 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index, } cmem =zs_map_object (meta->mem_pool, handle,ZS_MM_WO ); -if ((clen ==PAGE_SIZE ) && !is_partial_io (bvec)) +if ((clen ==PAGE_SIZE ) && !is_partial_io (bvec)) { src =kmap_atomic (page); -memcpy (cmem, src, clen); -if ((clen ==PAGE_SIZE ) && !is_partial_io (bvec)) +copy_page (cmem, src);kunmap_atomic (src); + }else { +memcpy (cmem, src, clen); + }zs_unmap_object (meta->mem_pool, handle);
Signature · 1 line
1.8.1.2
RFC PATCH 8/8 zram: protect sysfs handler from invalid memory access
In reply to Jiang LiuUse zram->init_lock to protect access to zram->meta, otherwise it may cause invalid memory access if zram->meta has been freed by __zram_reset_device().
--- drivers/staging/zram/zram_sysfs.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/staging/zram/zram_sysfs.c +2 -0
@@ -179,8 +179,10 @@ static ssize_t mem_used_total_show(struct device *dev,struct zram *zram =dev_to_zram (dev);struct zram_meta *meta = zram->meta; +down_read (&zram->init_lock);if (zram->init_done) val =zs_get_total_size_bytes (meta->mem_pool); +up_read (&zram->init_lock);return sprintf (buf,"%llu\n" , val); }
Signature · 1 line
1.8.1.2
Hello,
Quoted 4 lines · Jiang Liu
This patchset is to fix issues in zram found by code inspection. There is still one more issue left: should we repalce
zram_stat64_xxx()withatomic64_xxx()?
Quoted 10 lines
Jiang Liu (8):
zram: simplify and optimizezram_to_dev()
zram: avoid invalid memory access inzram_exit()
zram: usezram->lockto protectzram_free_page()in swap free notify
path
zram: destroy all devices on error recovery path inzram_init()
zram: avoid double free in error recovery path ofzram_bvec_write()
zram: avoid access beyond the zram device
zram: optimize memory operations with clear_page()/copy_page()
zram: protect sysfs handler from invalid memory access
I reviewed your patchset roughly and I feel most of patches make sense to me but some of that isn't not sure because you didn't write up the possible scenario, expecially "zram: use zram->lock to protect zram_free_page() in swap free notify path".
If your patchset fix real problem, it should go to the stable tree so you need to write description up in detail.
So, please rewrite up description on all of patches and resend. Thanks!
Signature · 2 lines
Kind regards, Minchan Kim