zram: Fix deadlock bug in partial write
Subsystems (3)
PATCH RESEND v5 1/4 zram: Fix deadlock bug in partial write
Now zram allocates new page with GFP_KERNEL in zram I/O path if IO is partial. Unfortunately, It may cuase deadlock with reclaim path so this patch solves the problem.
--- drivers/staging/zram/zram_drv.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/staging/zram/zram_drv.c +2 -2
@@ -220,7 +220,7 @@ static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec, user_mem =kmap_atomic (page);if (is_partial_io (bvec))/* Use a temporary buffer to decompress the page */ - uncmem =kmalloc (PAGE_SIZE ,GFP_KERNEL ); + uncmem =kmalloc (PAGE_SIZE ,GFP_ATOMIC );else uncmem = user_mem;
@@ -268,7 +268,7 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,* This is a partial IO. We need to read the full page * before to write the changes. */ - uncmem =kmalloc (PAGE_SIZE ,GFP_KERNEL ); + uncmem =kmalloc (PAGE_SIZE ,GFP_NOIO );if (!uncmem) {pr_info ("Error allocating temp memory!\n" ); ret = -ENOMEM ;
Signature · 1 line
1.7.9.5
PATCH RESEND v5 2/4 zram: force disksize setting before using zram
Now zram document syas "set disksize is optional"
but partly it's wrong. When you try to use zram firstly after booting, you must set disksize, otherwise zram can't work because zram gendisk's size is 0. But once you do it, you can use zram freely after reset because reset doesn't reset to zero paradoxically. So in this time, disksize setting is optional.:(
It's inconsitent for user behavior and not straightforward.
This patch forces always setting disksize firstly before using zram. Yes. It changes current behavior so someone could complain when he upgrades zram. Apparently it could be a problem if zram is mainline but it still lives in staging so behavior could be changed for right way to go. Let them excuse.
--- drivers/staging/zram/zram.txt | 27 ++++++++++---------- drivers/staging/zram/zram_drv.c | 51 +++++++++++++------------------------ drivers/staging/zram/zram_drv.h | 5 +--- drivers/staging/zram/zram_sysfs.c | 6 +---- 4 files changed, 34 insertions(+), 55 deletions(-)
drivers/staging/zram/zram.txt +14 -13
@@ -23,17 +23,17 @@ Following shows a typical sequence of steps for using zram. This creates 4 devices: /dev/zram{0,1,2,3} (num_devices parameter is optional. Default: 1) -2) Set Disksize (Optional): - Set disk size by writing the value to sysfs node 'disksize' - (in bytes). If disksize is not given, default value of 25% - of RAM is used. - - # Initialize /dev/zram0 with 50MB disksize - echo $((50*1024*1024)) > /sys/block/zram0/disksize - - NOTE: disksize cannot be changed if the disk contains any - data. So, for such a disk, you need to issue 'reset' (see below) - before you can change its disksize. +2) Set Disksize + Set disk size by writing the value to sysfs node 'disksize'. + The value can be either in bytes or you can use mem suffixes. + Examples: + # Initialize /dev/zram0 with 50MB disksize + echo $((50*1024*1024)) > /sys/block/zram0/disksize + + # Using mem suffixes + echo 256K > /sys/block/zram0/disksize + echo 512M > /sys/block/zram0/disksize + echo 1G > /sys/block/zram0/disksize 3) Activate: mkswap /dev/zram0
@@ -65,8 +65,9 @@ Following shows a typical sequence of steps for using zram. echo 1 > /sys/block/zram0/reset echo 1 > /sys/block/zram1/reset - (This frees all the memory allocated for the given device). - + This frees all the memory allocated for the given device and + resets the disksize to zero. You must set the disksize again + before reusing the device. Please report any problems at: - Mailing list: linux-mm-cc at laptop dot org
drivers/staging/zram/zram_drv.c +18 -33
87 lines not on this page · raw message
drivers/staging/zram/zram_drv.h +1 -4
@@ -28,9 +28,6 @@ static const unsigned max_num_devices = 32;/*-- Configurable parameters */ -/* Default zram disk size: 25% of total RAM */ -static const unsigned default_disksize_perc_ram =25 ; -/* * Pages that compress to size greater than this are stored * uncompressed in memory.
@@ -115,6 +112,6 @@ extern struct attribute_group zram_disk_attr_group;#endif extern int zram_init_device (struct zram *zram); -extern void __zram_reset_device (struct zram *zram); +extern void zram_reset_device (struct zram *zram);#endif
drivers/staging/zram/zram_sysfs.c +1 -5
@@ -110,11 +110,7 @@ static ssize_t reset_store(struct device *dev,if (bdev)fsync_bdev (bdev); -down_write (&zram->init_lock); -if (zram->init_done) -__zram_reset_device (zram); -up_write (&zram->init_lock); - +zram_reset_device (zram);return len; }
Signature · 7 lines
1.7.9.5 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH RESEND v5 3/4 zram: give up lazy initialization of zram metadata
In reply to Minchan Kim1) User of zram normally do mkfs.xxx or mkswap before using the zram block device(ex, normally, do it at booting time) It ends up allocating such metadata of zram before real usage so benefit of lazy initialzation would be mitigated.
2) Some user want to use zram when memory pressure is high.(ie, load zram dynamically, NOT booting time). It does make sense because people don't want to waste memory until memory pressure is high(ie, where zram is really helpful time). In this case, lazy initialzation could be failed easily because we will use GFP_NOIO instead of GFP_KERNEL for avoiding deadlock. So the benefit of lazy initialzation would be mitigated, too.
3) Metadata overhead is not critical and Nitin has a plan to diet it. 4K : 12 byte(64bit machine) -> 64G : 192M so 0.3% isn't big overhead If insane user use such big zram device up to 20, it could consume 6% of ram but efficieny of zram will cover the waste.
So this patch gives up lazy initialization and instead we initialize metadata at disksize setting time.
--- drivers/staging/zram/zram_drv.c | 20 ++++---------------- drivers/staging/zram/zram_sysfs.c | 1 + 2 files changed, 5 insertions(+), 16 deletions(-)
drivers/staging/zram/zram_drv.c +4 -16
@@ -440,16 +440,13 @@ static void zram_make_request(struct request_queue *queue, struct bio *bio) {struct zram *zram = queue->queuedata; -if (unlikely (!zram->init_done) &&zram_init_device (zram)) - goto error; -down_read (&zram->init_lock);if (unlikely (!zram->init_done)) - goto error_unlock; + goto error;if (!valid_io_request (zram, bio)) {zram_stat64_inc (zram, &zram->stats.invalid_io); - goto error_unlock; + goto error; }__zram_make_request (zram, bio,bio_data_dir (bio));
@@ -457,9 +454,8 @@ static void zram_make_request(struct request_queue *queue, struct bio *bio)return ; -error_unlock: -up_read (&zram->init_lock); error: +up_read (&zram->init_lock);bio_io_error (bio); }
@@ -508,18 +504,12 @@ void zram_reset_device(struct zram *zram)up_write (&zram->init_lock); } +/* zram->init_lock should be held */ int zram_init_device (struct zram *zram) {int ret;size_t num_pages; -down_write (&zram->init_lock); - -if (zram->init_done) { -up_write (&zram->init_lock); -return 0 ; - } -if (zram->disksize >2 * (totalram_pages <<PAGE_SHIFT )) {pr_info ( "There is little point creating a zram of greater than "
@@ -568,7 +558,6 @@ int zram_init_device(struct zram *zram) } zram->init_done =1 ; -up_write (&zram->init_lock);pr_debug ("Initialization done!\n" );return 0 ;
@@ -578,7 +567,6 @@ fail_no_table: zram->disksize =0 ; fail:__zram_reset_device (zram); -up_write (&zram->init_lock);pr_err ("Initialization failed: err=%d\n" , ret);return ret; }
drivers/staging/zram/zram_sysfs.c +1 -0
@@ -71,6 +71,7 @@ static ssize_t disksize_store(struct device *dev, zram->disksize =PAGE_ALIGN (disksize);set_capacity (zram->disk, zram->disksize >>SECTOR_SHIFT ); +zram_init_device (zram);up_write (&zram->init_lock);return len;
Signature · 7 lines
1.7.9.5 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH RESEND v5 4/4 zram: get rid of lockdep warning
In reply to Minchan KimLockdep complains about recursive deadlock of zram->init_lock.
[1] made it false positive because we can't request IO to zram before setting disksize. Anyway, we should shut lockdep up to avoid many reporting from user.
[1] : zram: force disksize setting before using zram
--- drivers/staging/zram/zram_drv.c | 189 +++++++++++++++++++------------------ drivers/staging/zram/zram_drv.h | 12 ++- drivers/staging/zram/zram_sysfs.c | 11 ++- 3 files changed, 116 insertions(+), 96 deletions(-)
drivers/staging/zram/zram_drv.c +99 -90
@@ -61,22 +61,22 @@ static void zram_stat64_inc(struct zram *zram, u64 *v)zram_stat64_add (zram, v,1 ); } -static int zram_test_flag (struct zram *zram,u32 index, +static int zram_test_flag (struct zram_meta *meta,u32 index,enum zram_pageflags flag) { -return zram->table[index].flags &BIT (flag); +return meta->table[index].flags &BIT (flag); } -static void zram_set_flag (struct zram *zram,u32 index, +static void zram_set_flag (struct zram_meta *meta,u32 index,enum zram_pageflags flag) { - zram->table[index].flags |=BIT (flag); + meta->table[index].flags |=BIT (flag); } -static void zram_clear_flag (struct zram *zram,u32 index, +static void zram_clear_flag (struct zram_meta *meta,u32 index,enum zram_pageflags flag) { - zram->table[index].flags &= ~BIT (flag); + meta->table[index].flags &= ~BIT (flag); }static int page_zero_filled (void *ptr)
@@ -96,16 +96,17 @@ static int page_zero_filled(void *ptr)static void zram_free_page (struct zram *zram,size_t index) { -unsigned long handle = zram->table[index].handle; -u16 size = zram->table[index].size; +struct zram_meta *meta = zram->meta; +unsigned long handle = meta->table[index].handle; +u16 size = meta->table[index].size;if (unlikely (!handle)) {/* * No memory is allocated for zero filled pages. * Simply clear zero page flag. */ -if (zram_test_flag (zram, index,ZRAM_ZERO )) { -zram_clear_flag (zram, index,ZRAM_ZERO ); +if (zram_test_flag (meta, index,ZRAM_ZERO )) { +zram_clear_flag (meta, index,ZRAM_ZERO ); zram->stats.pages_zero--; }return ;
@@ -114,17 +115,17 @@ static void zram_free_page(struct zram *zram, size_t index)if (unlikely (size > max_zpage_size)) zram->stats.bad_compress--; -zs_free (zram->mem_pool, handle); +zs_free (meta->mem_pool, handle);if (size <=PAGE_SIZE /2 ) zram->stats.good_compress--;zram_stat64_sub (zram, &zram->stats.compr_size, - zram->table[index].size); + meta->table[index].size); zram->stats.pages_stored--; - zram->table[index].handle =0 ; - zram->table[index].size =0 ; + meta->table[index].handle =0 ; + meta->table[index].size =0 ; }static void handle_zero_page (struct bio_vec *bvec)
@@ -149,20 +150,21 @@ static int zram_decompress_page(struct zram *zram, char *mem, u32 index)int ret =LZO_E_OK ;size_t clen =PAGE_SIZE ;unsigned char *cmem; -unsigned long handle = zram->table[index].handle; +struct zram_meta *meta = zram->meta; +unsigned long handle = meta->table[index].handle; -if (!handle ||zram_test_flag (zram, index,ZRAM_ZERO )) { +if (!handle ||zram_test_flag (meta, index,ZRAM_ZERO )) {memset (mem,0 ,PAGE_SIZE );return 0 ; } - cmem =zs_map_object (zram->mem_pool, handle,ZS_MM_RO ); -if (zram->table[index].size ==PAGE_SIZE ) + cmem =zs_map_object (meta->mem_pool, handle,ZS_MM_RO ); +if (meta->table[index].size ==PAGE_SIZE )memcpy (mem, cmem,PAGE_SIZE );else - ret =lzo1x_decompress_safe (cmem, zram->table[index].size, + ret =lzo1x_decompress_safe (cmem, meta->table[index].size, mem, &clen); -zs_unmap_object (zram->mem_pool, handle); +zs_unmap_object (meta->mem_pool, handle);/* Should NEVER happen. Return bio error if it does. */ if (unlikely (ret !=LZO_E_OK )) {
@@ -180,11 +182,11 @@ static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,int ret;struct page *page;unsigned char *user_mem, *uncmem =NULL ; - +struct zram_meta *meta = zram->meta; page = bvec->bv_page; -if (unlikely (!zram->table[index].handle) || -zram_test_flag (zram, index,ZRAM_ZERO )) { +if (unlikely (!meta->table[index].handle) || +zram_test_flag (meta, index,ZRAM_ZERO )) {handle_zero_page (bvec);return 0 ; }
@@ -231,9 +233,10 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,unsigned long handle;struct page *page;unsigned char *user_mem, *cmem, *src, *uncmem =NULL ; +struct zram_meta *meta = zram->meta; page = bvec->bv_page; - src = zram->compress_buffer; + src = meta->compress_buffer;if (is_partial_io (bvec)) {/*
@@ -255,8 +258,8 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,* System overwrites unused sectors. Free memory associated * with this sector now. */ -if (zram->table[index].handle || -zram_test_flag (zram, index,ZRAM_ZERO )) +if (meta->table[index].handle || +zram_test_flag (meta, index,ZRAM_ZERO ))zram_free_page (zram, index); user_mem =kmap_atomic (page);
@@ -274,13 +277,13 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,if (!is_partial_io (bvec))kunmap_atomic (user_mem); zram->stats.pages_zero++; -zram_set_flag (zram, index,ZRAM_ZERO ); +zram_set_flag (meta, index,ZRAM_ZERO ); ret =0 ; goto out; } ret =lzo1x_1_compress (uncmem,PAGE_SIZE , src, &clen, - zram->compress_workmem); + meta->compress_workmem);if (!is_partial_io (bvec)) {kunmap_atomic (user_mem);
@@ -301,14 +304,14 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index, src = uncmem; } - handle =zs_malloc (zram->mem_pool, clen); + handle =zs_malloc (meta->mem_pool, clen);if (!handle) {pr_info ("Error allocating memory for compressed " "page: %u, size=%zu\n" , index, clen); ret = -ENOMEM ; goto out; } - cmem =zs_map_object (zram->mem_pool, handle,ZS_MM_WO ); + cmem =zs_map_object (meta->mem_pool, handle,ZS_MM_WO );if ((clen ==PAGE_SIZE ) && !is_partial_io (bvec)) src =kmap_atomic (page);
@@ -316,10 +319,10 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,if ((clen ==PAGE_SIZE ) && !is_partial_io (bvec))kunmap_atomic (src); -zs_unmap_object (zram->mem_pool, handle); +zs_unmap_object (meta->mem_pool, handle); - zram->table[index].handle = handle; - zram->table[index].size = clen; + meta->table[index].handle = handle; + meta->table[index].size = clen;/* Update stats */ zram_stat64_add (zram, &zram->stats.compr_size, clen);
@@ -462,34 +465,25 @@ error:void __zram_reset_device (struct zram *zram) {size_t index; +struct zram_meta *meta;if (!zram->init_done)return ; + meta = zram->meta; zram->init_done =0 ; -/* Free various per-device buffers */ -kfree (zram->compress_workmem); -free_pages ((unsigned long )zram->compress_buffer,1 ); - - zram->compress_workmem =NULL ; - zram->compress_buffer =NULL ; -/* Free all pages that are still in this zram device */ for (index =0 ; index < zram->disksize >>PAGE_SHIFT ; index++) { -unsigned long handle = zram->table[index].handle; +unsigned long handle = meta->table[index].handle;if (!handle)continue ; -zs_free (zram->mem_pool, handle); +zs_free (meta->mem_pool, handle); } -vfree (zram->table); - zram->table =NULL ; - -zs_destroy_pool (zram->mem_pool); - zram->mem_pool =NULL ; - +zram_meta_free (zram->meta); + zram->meta =NULL ;/* Reset stats */ memset (&zram->stats,0 ,sizeof (zram->stats));
@@ -504,12 +498,65 @@ void zram_reset_device(struct zram *zram)up_write (&zram->init_lock); } -/* zram->init_lock should be held */ -int zram_init_device (struct zram *zram) +void zram_meta_free (struct zram_meta *meta) +{ +zs_destroy_pool (meta->mem_pool); +kfree (meta->compress_workmem); +free_pages ((unsigned long )meta->compress_buffer,1 ); +vfree (meta->table); +kfree (meta); +} + +struct zram_meta *zram_meta_alloc (u64 disksize) { -int ret;size_t num_pages; +struct zram_meta *meta =kmalloc (sizeof (*meta),GFP_KERNEL ); +if (!meta) + goto out; + + meta->compress_workmem =kzalloc (LZO1X_MEM_COMPRESS ,GFP_KERNEL ); +if (!meta->compress_workmem) { +pr_err ("Error allocating compressor working memory!\n" ); + goto free_meta; + } + + meta->compress_buffer = + (void *)__get_free_pages (GFP_KERNEL | __GFP_ZERO,1 ); +if (!meta->compress_buffer) { +pr_err ("Error allocating compressor buffer space\n" ); + goto free_workmem; + } + + num_pages = disksize >>PAGE_SHIFT ; + meta->table =vzalloc (num_pages *sizeof (*meta->table)); +if (!meta->table) { +pr_err ("Error allocating zram address table\n" ); + goto free_buffer; + } + + meta->mem_pool =zs_create_pool ("zram" ,GFP_NOIO | __GFP_HIGHMEM); +if (!meta->mem_pool) { +pr_err ("Error creating memory pool\n" ); + goto free_table; + } + +return meta; + +free_table: +vfree (meta->table); +free_buffer: +free_pages ((unsigned long )meta->compress_buffer,1 ); +free_workmem: +kfree (meta->compress_workmem); +free_meta: +kfree (meta); + meta =NULL ; +out: +return meta; +} +void zram_init_device (struct zram *zram,struct zram_meta *meta) +{if (zram->disksize >2 * (totalram_pages <<PAGE_SHIFT )) {pr_info ( "There is little point creating a zram of greater than "
@@ -524,51 +571,13 @@ int zram_init_device(struct zram *zram) ); } - zram->compress_workmem =kzalloc (LZO1X_MEM_COMPRESS ,GFP_KERNEL ); -if (!zram->compress_workmem) { -pr_err ("Error allocating compressor working memory!\n" ); - ret = -ENOMEM ; - goto fail_no_table; - } - - zram->compress_buffer = - (void *)__get_free_pages (GFP_KERNEL | __GFP_ZERO,1 ); -if (!zram->compress_buffer) { -pr_err ("Error allocating compressor buffer space\n" ); - ret = -ENOMEM ; - goto fail_no_table; - } - - num_pages = zram->disksize >>PAGE_SHIFT ; - zram->table =vzalloc (num_pages *sizeof (*zram->table)); -if (!zram->table) { -pr_err ("Error allocating zram address table\n" ); - ret = -ENOMEM ; - goto fail_no_table; - } -/* zram devices sort of resembles non-rotational disks */ queue_flag_set_unlocked (QUEUE_FLAG_NONROT , zram->disk->queue); - zram->mem_pool =zs_create_pool ("zram" ,GFP_NOIO | __GFP_HIGHMEM); -if (!zram->mem_pool) { -pr_err ("Error creating memory pool\n" ); - ret = -ENOMEM ; - goto fail; - } - + zram->meta = meta; zram->init_done =1 ;pr_debug ("Initialization done!\n" ); -return 0 ; - -fail_no_table: -/* To prevent accessing table entries during cleanup */ - zram->disksize =0 ; -fail: -__zram_reset_device (zram); -pr_err ("Initialization failed: err=%d\n" , ret); -return ret; } static void zram_slot_free_notify(struct block_device *bdev,
drivers/staging/zram/zram_drv.h +9 -3
@@ -83,11 +83,15 @@ struct zram_stats {u32 bad_compress;/* % of pages with compression ratio>=75% */ }; -struct zram { -struct zs_pool *mem_pool; +struct zram_meta {void *compress_workmem;void *compress_buffer;struct table *table; +struct zs_pool *mem_pool; +}; + +struct zram { +struct zram_meta *meta;spinlock_t stat64_lock;/* protect 64-bit stats */ struct rw_semaphore lock;/* protect compression buffers and table * against concurrent read and writes */
@@ -111,7 +115,9 @@ unsigned int zram_get_num_devices(void);extern struct attribute_group zram_disk_attr_group;#endif -extern int zram_init_device (struct zram *zram);extern void zram_reset_device (struct zram *zram); +extern struct zram_meta *zram_meta_alloc (u64 disksize); +extern void zram_meta_free (struct zram_meta *meta); +extern void zram_init_device (struct zram *zram,struct zram_meta *meta);#endif
drivers/staging/zram/zram_sysfs.c +8 -3
@@ -56,22 +56,26 @@ 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);down_write (&zram->init_lock);if (zram->init_done) {up_write (&zram->init_lock); +zram_meta_free (meta);pr_info ("Cannot change disksize for initialized device\n" );return -EBUSY ; } - zram->disksize =PAGE_ALIGN (disksize); + zram->disksize = disksize;set_capacity (zram->disk, zram->disksize >>SECTOR_SHIFT ); -zram_init_device (zram); +zram_init_device (zram, meta);up_write (&zram->init_lock);return len;
@@ -182,9 +186,10 @@ static ssize_t mem_used_total_show(struct device *dev, {u64 val =0 ;struct zram *zram =dev_to_zram (dev); +struct zram_meta *meta = zram->meta;if (zram->init_done) - val =zs_get_total_size_bytes (zram->mem_pool); + val =zs_get_total_size_bytes (meta->mem_pool);return sprintf (buf,"%llu\n" , val); }
Signature · 7 lines
1.7.9.5 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 2 lines · Minchan Kim
Now zram allocates new page with
GFP_KERNELin zram I/O path if IO is partial. Unfortunately, It may cuase deadlock with
s/cuase/cause/g
Quoted 1 line
reclaim path so this patch solves the problem.
It'd be nice to know about the problem in more detail. I'm also curious on why you decided on GFP_ATOMIC for the read path and GFP_NOIO in the write path.
Quoted 39 lines
Cc Jerome Marchand <[email protected]>Acked-by Nitin Gupta <[email protected]>Signed-off-by Minchan Kim <[email protected]>--- drivers/staging/zram/zram_drv.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/staging/zram/zram_drv.c +2
-3@@ -220,7 +220,7 @@ static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec, user_mem =kmap_atomic (page);if (is_partial_io (bvec))/* Use a temporary buffer to decompress the page */ - uncmem =kmalloc (PAGE_SIZE ,GFP_KERNEL ); + uncmem =kmalloc (PAGE_SIZE ,GFP_ATOMIC );else uncmem = user_mem;@@ -268,7 +268,7 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,* This is a partial IO. We need to read the full page * before to write the changes. */ - uncmem =kmalloc (PAGE_SIZE ,GFP_KERNEL ); + uncmem =kmalloc (PAGE_SIZE ,GFP_NOIO );if (!uncmem) {pr_info ("Error allocating temp memory!\n" ); ret = -ENOMEM ; --1.7.9.5
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 11 lines · Pekka Enberg
Quoted 2 lines · Minchan Kim
Now zram allocates new page with
GFP_KERNELin zram I/O path if IO is partial. Unfortunately, It may cuase deadlock withs/cuase/cause/g
Quoted 1 line
reclaim path so this patch solves the problem.
It'd be nice to know about the problem in more detail. I'm also curious on why you decided on
GFP_ATOMICfor the read path andGFP_NOIOin the write path.
This is because we're holding a kmap_atomic page in the read path.
Jerome
Quoted 40 lines
Quoted 40 lines
Cc Jerome Marchand <[email protected]>Acked-by Nitin Gupta <[email protected]>Signed-off-by Minchan Kim <[email protected]>--- drivers/staging/zram/zram_drv.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/staging/zram/zram_drv.c +2
-3@@ -220,7 +220,7 @@ static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec, user_mem =kmap_atomic (page);if (is_partial_io (bvec))/* Use a temporary buffer to decompress the page */ - uncmem =kmalloc (PAGE_SIZE ,GFP_KERNEL ); + uncmem =kmalloc (PAGE_SIZE ,GFP_ATOMIC );else uncmem = user_mem;@@ -268,7 +268,7 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,* This is a partial IO. We need to read the full page * before to write the changes. */ - uncmem =kmalloc (PAGE_SIZE ,GFP_KERNEL ); + uncmem =kmalloc (PAGE_SIZE ,GFP_NOIO );if (!uncmem) {pr_info ("Error allocating temp memory!\n" ); ret = -ENOMEM ; --1.7.9.5
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 7 lines · Jerome Marchand
Quoted 6 lines · Pekka Enberg
Quoted 2 lines · Minchan Kim
Now zram allocates new page with
GFP_KERNELin zram I/O path if IO is partial. Unfortunately, It may cuase deadlock withs/cuase/cause/g
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
reclaim path so this patch solves the problem.
It'd be nice to know about the problem in more detail. I'm also curious on why you decided on
GFP_ATOMICfor the read path andGFP_NOIOin the write path.This is because we're holding a
kmap_atomicpage in the read path.
Okay, so that's about partial *reads* and not even mentioned in the changelog, no?
AFAICT, you could rearrange the code in zram_bvec_read() as follows:
if (is_partial_io(bvec))
/* Use a temporary buffer to decompress the page */
uncmem = kmalloc(PAGE_SIZE, GFP_KERNEL);
else {
uncmem = user_mem = kmap_atomic(page);
}and avoid the GFP_ATOMIC allocation.
Quoted 21 lines · Pekka Enberg
Quoted 7 lines · Jerome Marchand
Quoted 6 lines · Pekka Enberg
Quoted 2 lines · Minchan Kim
Now zram allocates new page with
GFP_KERNELin zram I/O path if IO is partial. Unfortunately, It may cuase deadlock withs/cuase/cause/g
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
reclaim path so this patch solves the problem.
It'd be nice to know about the problem in more detail. I'm also curious on why you decided on
GFP_ATOMICfor the read path andGFP_NOIOin the write path.This is because we're holding a
kmap_atomicpage in the read path.Okay, so that's about partial *reads* and not even mentioned in the changelog, no?
AFAICT, you could rearrange the code in
zram_bvec_read()as follows:
Quoted 9 lines
if (is_partial_io(bvec)) /* Use a temporary buffer to decompress the page */ uncmem = kmalloc(PAGE_SIZE, GFP_KERNEL); else { uncmem = user_mem = kmap_atomic(page); }and avoid the
GFP_ATOMICallocation.
user_mem still has to be mapped in case of partial I/O too. But the temporary buffer allocation could happen before. The allocation still would need to be GFP_NOIO to avoid possible deadlocks.
Anyhow, the commit message could definitely be more explicit.
Regards,
Jerome
Minchan Kim
Acked-by Nitin GuptaQuoted 5 lines · Pekka Enberg
Quoted 2 lines · Minchan Kim
Now zram allocates new page with
GFP_KERNELin zram I/O path if IO is partial. Unfortunately, It may cuase deadlock withs/cuase/cause/g
Thanks!
Quoted 6 lines
Quoted 2 lines
reclaim path so this patch solves the problem.
It'd be nice to know about the problem in more detail. I'm also curious on why you decided on
GFP_ATOMICfor the read path andGFP_NOIOin the write path.
In read path, we called kmap_atomic.
How about this?
------------------------- >8 -------------------------------
Quoted 1 line
From
9f8756ae0b0f2819f93cb94dcd38da372843aa12Mon Sep 17 00:00:00 2001
From: Minchan Kim <[email protected]>
Date: Mon, 21 Jan 2013 13:58:52 +0900
Subject: [RESEND PATCH v5 1/4] zram: Fix deadlock bug in partial read/write
Now zram allocates new page with GFP_KERNEL in zram I/O path if IO is partial. Unfortunately, It may cause deadlock with reclaim path like below.
write_page from fsfs_lockallocation(GFP_KERNEL)
reclaim
pageout
write_page from fs fs_lock <-- deadlock
This patch fixes it by using GFP_ATOMIC and GFP_NOIO. In read path, we called kmap_atomic so that we need GFP_ATOMIC while we need GFP_NOIO in write path.
---
We could use GFP_IO instead of GFP_ATOMIC in zram_bvec_read with some modification related to buffer allocation in case of partial IO. But it needs more churn and prevent merge this patch into stable if we should send this to stable so I'd like to keep it as simple as possbile. GFP_IO usage could be separate patch after we merge it. Thanks.
drivers/staging/zram/zram_drv.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/staging/zram/zram_drv.c +2 -2
@@ -220,7 +220,7 @@ static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec, user_mem =kmap_atomic (page);if (is_partial_io (bvec))/* Use a temporary buffer to decompress the page */ - uncmem =kmalloc (PAGE_SIZE ,GFP_KERNEL ); + uncmem =kmalloc (PAGE_SIZE ,GFP_ATOMIC );else uncmem = user_mem;
@@ -268,7 +268,7 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,* This is a partial IO. We need to read the full page * before to write the changes. */ - uncmem =kmalloc (PAGE_SIZE ,GFP_KERNEL ); + uncmem =kmalloc (PAGE_SIZE ,GFP_NOIO );if (!uncmem) {pr_info ("Error allocating temp memory!\n" ); ret = -ENOMEM ;
Signature · 5 lines
1.7.9.5 -- Kind regards, Minchan Kim
Pekka Enberg
Acked-by Nitin GuptaQuoted 20 lines · Minchan Kim
How about this?
------------------------- >8 -------------------------------From
9f8756ae0b0f2819f93cb94dcd38da372843aa12Mon Sep 17 00:00:00 2001
From: Minchan Kim <[email protected]>
Date: Mon, 21 Jan 2013 13:58:52 +0900
Subject: [RESEND PATCH v5 1/4] zram: Fix deadlock bug in partial read/writeNow zram allocates new page with
GFP_KERNELin zram I/O path if IO is partial. Unfortunately, It may cause deadlock with reclaim path like below.
write_pagefrom fsfs_lockallocation(GFP_KERNEL)
reclaim
pageout
write_pagefrom fsfs_lock<-- deadlock
Quoted 3 lines
This patch fixes it by using
GFP_ATOMICandGFP_NOIO. In read path, we calledkmap_atomicso that we needGFP_ATOMICwhile we needGFP_NOIOin write path.
The patch description makes sense now. Thanks!
Quoted 5 lines · Minchan Kim
We could use
GFP_IOinstead ofGFP_ATOMICinzram_bvec_readwith some modification related to buffer allocation in case of partial IO. But it needs more churn and prevent merge this patch into stable if we should send this to stable so I'd like to keep it as simple as possbile.GFP_IOusage could be separate patch after we merge it.
I don't see why something like below couldn't be merged for stable. Going for GFP_ATOMIC might seem like the simplest thing to go for but usually bites you in the end.
Pekka
------------------------- >8 -------------------------------
Quoted 1 line
From
936a12b423c58542628d6fd683e859f752eb3d41Mon Sep 17 00:00:00 2001
From: Minchan Kim <[email protected]>
Date: Mon, 21 Jan 2013 13:58:52 +0900
Subject: [PATCH] zram: Fix deadlock bug in partial read/write
Now zram allocates new page with GFP_KERNEL in zram I/O path if IO is partial. Unfortunately, It may cause deadlock with reclaim path like below.
write_page from fsfs_lockallocation(GFP_KERNEL)
reclaim
pageout
write_page from fs fs_lock <-- deadlock
This patch fixes it by using GFP_NOIO. In read path, we reorganize code flow so that kmap_atomic is called after the GFP_NOIO allocation.
--- drivers/staging/zram/zram_drv.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/staging/zram/zram_drv.c +5 -4
@@ -228,11 +228,12 @@ static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,return 0 ; } - user_mem =kmap_atomic (page);if (is_partial_io (bvec))/* Use a temporary buffer to decompress the page */ - uncmem =kmalloc (PAGE_SIZE ,GFP_KERNEL ); -else + uncmem =kmalloc (PAGE_SIZE ,GFP_NOIO ); + + user_mem =kmap_atomic (page); +if (!is_partial_io (bvec)) uncmem = user_mem;if (!uncmem) {
@@ -279,7 +280,7 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,* This is a partial IO. We need to read the full page * before to write the changes. */ - uncmem =kmalloc (PAGE_SIZE ,GFP_KERNEL ); + uncmem =kmalloc (PAGE_SIZE ,GFP_NOIO );if (!uncmem) {pr_info ("Error allocating temp memory!\n" ); ret = -ENOMEM ;
Signature · 1 line
1.7.11.7
Quoted 34 lines · Pekka Enberg
Quoted 20 lines · Minchan Kim
How about this?
------------------------- >8 -------------------------------From
9f8756ae0b0f2819f93cb94dcd38da372843aa12Mon Sep 17 00:00:00 2001
From: Minchan Kim <[email protected]>
Date: Mon, 21 Jan 2013 13:58:52 +0900
Subject: [RESEND PATCH v5 1/4] zram: Fix deadlock bug in partial read/writeNow zram allocates new page with
GFP_KERNELin zram I/O path if IO is partial. Unfortunately, It may cause deadlock with reclaim path like below.
write_pagefrom fsfs_lockallocation(GFP_KERNEL)
reclaim
pageout
write_pagefrom fsfs_lock<-- deadlockQuoted 3 lines
This patch fixes it by using
GFP_ATOMICandGFP_NOIO. In read path, we calledkmap_atomicso that we needGFP_ATOMICwhile we needGFP_NOIOin write path.The patch description makes sense now. Thanks!
Quoted 5 lines · Minchan Kim
We could use
GFP_IOinstead ofGFP_ATOMICinzram_bvec_readwith some modification related to buffer allocation in case of partial IO. But it needs more churn and prevent merge this patch into stable if we should send this to stable so I'd like to keep it as simple as possbile.GFP_IOusage could be separate patch after we merge it.
Quoted 3 lines
I don't see why something like below couldn't be merged for stable. Going for
GFP_ATOMICmight seem like the simplest thing to go for but usually bites you in the end.
Looks good to me. I will resend it.
Thanks, Pekka.
Quoted 73 lines
Pekka
------------------------- >8 -------------------------------
Quoted 1 line
From
936a12b423c58542628d6fd683e859f752eb3d41Mon Sep 17 00:00:00 2001From: Minchan Kim <[email protected]>
Date: Mon, 21 Jan 2013 13:58:52 +0900
Subject: [PATCH] zram: Fix deadlock bug in partial read/writeNow zram allocates new page with
GFP_KERNELin zram I/O path if IO is partial. Unfortunately, It may cause deadlock with reclaim path like below.
write_pagefrom fsfs_lockallocation(GFP_KERNEL)
reclaim
pageout
write_pagefrom fsfs_lock<-- deadlockThis patch fixes it by using
GFP_NOIO. In read path, we reorganize code flow so thatkmap_atomicis called after theGFP_NOIOallocation.Cc Jerome Marchand <[email protected]>Acked-by Nitin Gupta <[email protected]>Signed-off-by Minchan Kim <[email protected]>[ [email protected]: don't use GFP_ATOMIC ]Signed-off-by Pekka Enberg <[email protected]>--- drivers/staging/zram/zram_drv.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)drivers/staging/zram/zram_drv.c +5
-4@@ -228,11 +228,12 @@ static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,return 0 ; } - user_mem =kmap_atomic (page);if (is_partial_io (bvec))/* Use a temporary buffer to decompress the page */ - uncmem =kmalloc (PAGE_SIZE ,GFP_KERNEL ); -else + uncmem =kmalloc (PAGE_SIZE ,GFP_NOIO ); + + user_mem =kmap_atomic (page); +if (!is_partial_io (bvec)) uncmem = user_mem;if (!uncmem) {@@ -279,7 +280,7 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,* This is a partial IO. We need to read the full page * before to write the changes. */ - uncmem =kmalloc (PAGE_SIZE ,GFP_KERNEL ); + uncmem =kmalloc (PAGE_SIZE ,GFP_NOIO );if (!uncmem) {pr_info ("Error allocating temp memory!\n" ); ret = -ENOMEM ;Signature · 7 lines
1.7.11.7 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Signature · 2 lines
Kind regards, Minchan Kim
Greg Kroah-Hartman
maintainerQuoted 11 lines · Minchan Kim
Now zram allocates new page with
GFP_KERNELin zram I/O path if IO is partial. Unfortunately, It may cuase deadlock with reclaim path so this patch solves the problem.Cc Jerome Marchand <[email protected]>Acked-by Nitin Gupta <[email protected]>Signed-off-by Minchan Kim <[email protected]>--- drivers/staging/zram/zram_drv.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Due to the discussion on this series, I don't know what patch to apply, so care to do a v6 of this with the patches that everyone has finally agreed on?
I'm dropping these from my to-apply queue now.
thanks,
greg k-h
Hi Greg,
Quoted 13 lines · Greg Kroah-Hartman
Quoted 11 lines · Minchan Kim
Now zram allocates new page with
GFP_KERNELin zram I/O path if IO is partial. Unfortunately, It may cuase deadlock with reclaim path so this patch solves the problem.Cc Jerome Marchand <[email protected]>Acked-by Nitin Gupta <[email protected]>Signed-off-by Minchan Kim <[email protected]>--- drivers/staging/zram/zram_drv.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 3 lines
Due to the discussion on this series, I don't know what patch to apply, so care to do a v6 of this with the patches that everyone has finally agreed on?
I already sent v6.
https://lkml.org/lkml/2013/1/29/680
Thanks.
Signature · 2 lines
Kind regards, Minchan Kim
Quoted 16 lines · Minchan Kim
Hi Greg,
Quoted 13 lines · Greg Kroah-Hartman
Quoted 11 lines · Minchan Kim
Now zram allocates new page with
GFP_KERNELin zram I/O path if IO is partial. Unfortunately, It may cuase deadlock with reclaim path so this patch solves the problem.Cc Jerome Marchand <[email protected]>Acked-by Nitin Gupta <[email protected]>Signed-off-by Minchan Kim <[email protected]>--- drivers/staging/zram/zram_drv.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 6 lines
Quoted 3 lines
Due to the discussion on this series, I don't know what patch to apply, so care to do a v6 of this with the patches that everyone has finally agreed on?
I already sent v6.
https://lkml.org/lkml/2013/1/29/680
Greg, If you have a trouble to merge it, let me know it. Will resend it when linux-next comes in.
Thanks.
Signature · 2 lines
Kind regards, Minchan Kim