Skip to contentSkip to conversation

zram: force disksize setting before using zram

PATCH In review Minchan Kim 3 participants 8 messages Nov 23, 2012 – Nov 28, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 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     |    7 +++--
 drivers/staging/zram/zram_drv.c   |   57 ++++++++++++++-----------------------
 drivers/staging/zram/zram_drv.h   |    5 +---
 drivers/staging/zram/zram_sysfs.c |    6 +---
 4 files changed, 27 insertions(+), 48 deletions(-)
drivers/staging/zram/zram.txt +4 -3
index 5f75d29..00ae66b 100644
@@ -23,10 +23,9 @@ 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):
+2) Set Disksize
 	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.
+	(in bytes).
 
 	# Initialize /dev/zram0 with 50MB disksize
 	echo $((50*1024*1024)) > /sys/block/zram0/disksize
@@ -67,6 +66,8 @@ Following shows a typical sequence of steps for using zram.
 
 	(This frees all the memory allocated for the given device).
 
+	If you want to use zram again, you should set disksize first
+	due to reset zram.
 
 Please report any problems at:
  - Mailing list: linux-mm-cc at laptop dot org
drivers/staging/zram/zram_drv.c +21 -36

99 lines not on this page · raw message

drivers/staging/zram/zram_drv.h +1 -4
index df2eec4..5b671d1 100644
@@ -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
index de1eacf..4143af9 100644
@@ -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>
#1 lore raw

PATCH 2/2 zram: allocate metadata when disksize is set up

Lockdep complains about recursive deadlock of zram->init_lock. Because zram_init_device could be called in reclaim context and it requires a page with GFP_KERNEL.

We can fix it via replacing GFP_KERNEL with GFP_NOIO.
But more big problem is vzalloc in zram_init_device which calls GFP_KERNEL. We can change it with __vmalloc which can receive gfp_t.
But still we have a problem. Although __vmalloc can handle gfp_t, it calls allocation of GFP_KERNEL. That's why I sent the patch.
https://lkml.org/lkml/2012/4/23/77

Yes. Fundamental problem is utter crap API vmalloc.
If we can fix it, everyone would be happy. But life isn't simple like seeing my thread of the patch.

So next option is to give up lazy initialization and initialize it at the very disksize setting time. But it makes unnecessary metadata waste until zram is really used. But let's think about it.

1) 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   |   21 ++++-----------------
 drivers/staging/zram/zram_sysfs.c |    1 +
 2 files changed, 5 insertions(+), 17 deletions(-)
drivers/staging/zram/zram_drv.c +4 -17
index 9ef1eca..f364fb5 100644
@@ -441,16 +441,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));
@@ -458,9 +455,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);
 }
 
@@ -509,19 +505,12 @@ void zram_reset_device(struct zram *zram)
 	up_write(&zram->init_lock);
 }
 
+/* zram->init_lock should be hold */
 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;
-	}
-
-	BUG_ON(!zram->disksize);
-
 	if (zram->disksize > 2 * (totalram_pages << PAGE_SHIFT)) {
 		pr_info(
 		"There is little point creating a zram of greater than "
@@ -570,7 +559,6 @@ int zram_init_device(struct zram *zram)
 	}
 
 	zram->init_done = 1;
-	up_write(&zram->init_lock);
 
 	pr_debug("Initialization done!\n");
 	return 0;
@@ -580,7 +568,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
index 4143af9..369db12 100644
@@ -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>
#6 lore raw
In reply to Minchan Kim
Quoted 27 lines · Minchan Kim

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     |    7 +++--
  drivers/staging/zram/zram_drv.c   |   57 ++++++++++++++-----------------------
  drivers/staging/zram/zram_drv.h   |    5 +---
  drivers/staging/zram/zram_sysfs.c |    6 +---
  4 files changed, 27 insertions(+), 48 deletions(-)
drivers/staging/zram/zram.txt +0 -0
index 5f75d29..00ae66b 100644

drivers/staging/zram/zram.txt Following shows a typical sequence of steps for using zram. · line 29

Quoted 10 lines
drivers/staging/zram/zram.txt +2 -3
@@ -23,10 +23,9 @@ 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):
+2) Set Disksize
  	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.
+	(in bytes).
 

Disksize can now be set using K/M/G suffixes also (see Sergey's change: handle mem suffixes in disk size ...). So, this should be documented as:

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

drivers/staging/zram/zram.txt Following shows a typical sequence of steps for using zram. · line 70

Quoted 8 lines
drivers/staging/zram/zram.txt +2 -0
  	# Initialize /dev/zram0 with 50MB disksize
  	echo $((50*1024*1024)) > /sys/block/zram0/disksize
@@ -67,6 +66,8 @@ Following shows a typical sequence of steps for using zram.
 
  	(This frees all the memory allocated for the given device).
 
+	If you want to use zram again, you should set disksize first
+	due to reset zram.

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.

Quoted 66 lines
drivers/staging/zram/zram.txt +0 -0
  Please report any problems at:
   - Mailing list: linux-mm-cc at laptop dot org
drivers/staging/zram/zram_drv.c +5 -30
index fb4a7c9..9ef1eca 100644
@@ -104,35 +104,6 @@ static int page_zero_filled(void *ptr)
  	return 1;
  }
 
-static void zram_set_disksize(struct zram *zram, size_t totalram_bytes)
-{
-	if (!zram->disksize) {
-		pr_info(
-		"disk size not provided. You can use disksize_kb module "
-		"param to specify size.\nUsing default: (%u%% of RAM).\n",
-		default_disksize_perc_ram
-		);
-		zram->disksize = default_disksize_perc_ram *
-					(totalram_bytes / 100);
-	}
-
-	if (zram->disksize > 2 * (totalram_bytes)) {
-		pr_info(
-		"There is little point creating a zram of greater than "
-		"twice the size of memory since we expect a 2:1 compression "
-		"ratio. Note that zram uses about 0.1%% of the size of "
-		"the disk when not in use so a huge zram is "
-		"wasteful.\n"
-		"\tMemory Size: %zu kB\n"
-		"\tSize you selected: %llu kB\n"
-		"Continuing anyway ...\n",
-		totalram_bytes >> 10, zram->disksize
-		);
-	}
-
-	zram->disksize &= PAGE_MASK;
-}
-
  static void zram_free_page(struct zram *zram, size_t index)
  {
  	unsigned long handle = zram->table[index].handle;
@@ -497,6 +468,9 @@ void __zram_reset_device(struct zram *zram)
  {
  	size_t index;
 
+	if (!zram->init_done)
+		goto out;
+
  	zram->init_done = 0;
 
  	/* Free various per-device buffers */
@@ -523,8 +497,9 @@ void __zram_reset_device(struct zram *zram)
 
  	/* Reset stats */
  	memset(&zram->stats, 0, sizeof(zram->stats));
-
+out:
  	zram->disksize = 0;
+	set_capacity(zram->disk, 0);
  }
 
  void zram_reset_device(struct zram *zram)
@@ -540,13 +515,26 @@ int zram_init_device(struct zram *zram)
  	size_t num_pages;

drivers/staging/zram/zram_drv.c int zram_init_device(struct zram *zram) · line 523

Quoted 10 lines
drivers/staging/zram/zram_drv.c +1 -2
 
  	down_write(&zram->init_lock);
-
  	if (zram->init_done) {
  		up_write(&zram->init_lock);
  		return 0;
  	}
 
-	zram_set_disksize(zram, totalram_pages << PAGE_SHIFT);
+	BUG_ON(!zram->disksize);

It shouldn't cause a crash if user sets disksize to zero; a noop seems better.

Thanks,
Nitin

--
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>

#2 lore raw

Re: [PATCH 2/2] zram: allocate metadata when disksize is set up

In reply to Minchan Kim
Quoted 80 lines · Minchan Kim

Lockdep complains about recursive deadlock of zram->init_lock. Because zram_init_device could be called in reclaim context and it requires a page with GFP_KERNEL.

We can fix it via replacing GFP_KERNEL with GFP_NOIO.
But more big problem is vzalloc in zram_init_device which calls GFP_KERNEL. We can change it with __vmalloc which can receive gfp_t.
But still we have a problem. Although __vmalloc can handle gfp_t, it calls allocation of GFP_KERNEL. That's why I sent the patch.
https://lkml.org/lkml/2012/4/23/77

Yes. Fundamental problem is utter crap API vmalloc.
If we can fix it, everyone would be happy. But life isn't simple like seeing my thread of the patch.

So next option is to give up lazy initialization and initialize it at the very disksize setting time. But it makes unnecessary metadata waste until zram is really used. But let's think about it.

1) 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   |   21 ++++-----------------
  drivers/staging/zram/zram_sysfs.c |    1 +
  2 files changed, 5 insertions(+), 17 deletions(-)
drivers/staging/zram/zram_drv.c +3 -7
index 9ef1eca..f364fb5 100644
@@ -441,16 +441,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));
@@ -458,9 +455,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);
  }
 

drivers/staging/zram/zram_drv.c void zram_reset_device(struct zram *zram) · line 508

Quoted 4 lines
drivers/staging/zram/zram_drv.c +1 -0
@@ -509,19 +505,12 @@ void zram_reset_device(struct zram *zram)
  	up_write(&zram->init_lock);
  }
 
+/* zram->init_lock should be hold */

s/hold/held

btw, shouldn't we also change GFP_KERNEL to GFP_NOIO in is_partial_io() case in both read/write handlers?

Rest of the patch looks good.

Thanks,
Nitin

Quoted 38 lines
drivers/staging/zram/zram_drv.c +0 -10
  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;
-	}
-
-	BUG_ON(!zram->disksize);
-
  	if (zram->disksize > 2 * (totalram_pages << PAGE_SHIFT)) {
  		pr_info(
  		"There is little point creating a zram of greater than "
@@ -570,7 +559,6 @@ int zram_init_device(struct zram *zram)
  	}
 
  	zram->init_done = 1;
-	up_write(&zram->init_lock);
 
  	pr_debug("Initialization done!\n");
  	return 0;
@@ -580,7 +568,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 +0 -0
index 4143af9..369db12 100644

drivers/staging/zram/zram_sysfs.c static ssize_t disksize_store(struct device *dev, · line 77

Quoted 7 lines
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;

--
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>

#3 lore raw

Re: [PATCH 2/2] zram: allocate metadata when disksize is set up

Quoted 84 lines · Nitin Gupta
Quoted 83 lines · Minchan Kim

Lockdep complains about recursive deadlock of zram->init_lock. Because zram_init_device could be called in reclaim context and it requires a page with GFP_KERNEL.

We can fix it via replacing GFP_KERNEL with GFP_NOIO.
But more big problem is vzalloc in zram_init_device which calls GFP_KERNEL. We can change it with __vmalloc which can receive gfp_t.
But still we have a problem. Although __vmalloc can handle gfp_t, it calls allocation of GFP_KERNEL. That's why I sent the patch.
https://lkml.org/lkml/2012/4/23/77

Yes. Fundamental problem is utter crap API vmalloc.
If we can fix it, everyone would be happy. But life isn't simple like seeing my thread of the patch.

So next option is to give up lazy initialization and initialize it at the very disksize setting time. But it makes unnecessary metadata waste until zram is really used. But let's think about it.

1) 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   |   21 ++++-----------------
  drivers/staging/zram/zram_sysfs.c |    1 +
  2 files changed, 5 insertions(+), 17 deletions(-)
drivers/staging/zram/zram_drv.c +3 -7
index 9ef1eca..f364fb5 100644
@@ -441,16 +441,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));
@@ -458,9 +455,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);
  }
 
@@ -509,19 +505,12 @@ void zram_reset_device(struct zram *zram)
  	up_write(&zram->init_lock);
  }
 
Quoted 6 lines

drivers/staging/zram/zram_drv.c void zram_reset_device(struct zram *zram) · line 508

Quoted 1 line
drivers/staging/zram/zram_drv.c +1 -0
+/* zram->init_lock should be hold */

s/hold/held

btw, shouldn't we also change GFP_KERNEL to GFP_NOIO in is_partial_io() case in both read/write handlers?

Good point. Actually, the one in zram_bvec_read() should actually be GFP_ATOMIC because of the kmap_atomic() above (or be moved out of kmap_atomic/kunmap_atomic nest).
Another solution would be to allocate some working buffer at device init as it's done for compress_buffer/workmem. It would make zram_bvec_read/write look simpler (no need to free memory or manage kmalloc failure).

Jerome

Quoted 45 lines

Rest of the patch looks good.

Thanks,
Nitin

Quoted 38 lines
drivers/staging/zram/zram_drv.c +0 -10
  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;
-	}
-
-	BUG_ON(!zram->disksize);
-
  	if (zram->disksize > 2 * (totalram_pages << PAGE_SHIFT)) {
  		pr_info(
  		"There is little point creating a zram of greater than "
@@ -570,7 +559,6 @@ int zram_init_device(struct zram *zram)
  	}
 
  	zram->init_done = 1;
-	up_write(&zram->init_lock);
 
  	pr_debug("Initialization done!\n");
  	return 0;
@@ -580,7 +568,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 +0 -0
index 4143af9..369db12 100644
@@ -71,6 +71,7 @@ static ssize_t disksize_store(struct device *dev,
 
Quoted 8 lines

drivers/staging/zram/zram_sysfs.c static ssize_t disksize_store(struct device *dev, · line 77

Quoted 7 lines
drivers/staging/zram/zram_sysfs.c +1 -0
  	zram->disksize = PAGE_ALIGN(disksize);
  	set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
+	zram_init_device(zram);
  	up_write(&zram->init_lock);
 
  	return len;

--
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>

#7 lore raw
In reply to Nitin Gupta
Quoted 50 lines · Nitin Gupta
Quoted 27 lines · Minchan Kim

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     |    7 +++--
 drivers/staging/zram/zram_drv.c   |   57 ++++++++++++++-----------------------
 drivers/staging/zram/zram_drv.h   |    5 +---
 drivers/staging/zram/zram_sysfs.c |    6 +---
 4 files changed, 27 insertions(+), 48 deletions(-)
drivers/staging/zram/zram.txt +0 -0
index 5f75d29..00ae66b 100644

drivers/staging/zram/zram.txt Following shows a typical sequence of steps for using zram. · line 29

Quoted 10 lines
drivers/staging/zram/zram.txt +2 -3
@@ -23,10 +23,9 @@ 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):
+2) Set Disksize
 	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.
+	(in bytes).
 

Disksize can now be set using K/M/G suffixes also (see Sergey's
change: handle mem suffixes in disk size ...). So, this should be documented as:

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

Quoted 5 lines

# Using mem suffixes
echo 256K > /sys/block/zram0/disksize
echo 512M > /sys/block/zram0/disksize
echo 1G > /sys/block/zram0/disksize

Done.

Quoted 7 lines
Quoted 7 lines
drivers/staging/zram/zram.txt +0 -0
 	# Initialize /dev/zram0 with 50MB disksize
 	echo $((50*1024*1024)) > /sys/block/zram0/disksize
@@ -67,6 +66,8 @@ Following shows a typical sequence of steps for using zram.
 
 	(This frees all the memory allocated for the given device).
 
Quoted 7 lines

drivers/staging/zram/zram.txt Following shows a typical sequence of steps for using zram. · line 70

Quoted 2 lines
drivers/staging/zram/zram.txt +2 -0
+	If you want to use zram again, you should set disksize first
+	due to reset zram.

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.

Done.

Quoted 75 lines
Quoted 75 lines
drivers/staging/zram/zram.txt +0 -0
 Please report any problems at:
  - Mailing list: linux-mm-cc at laptop dot org
drivers/staging/zram/zram_drv.c +5 -31
index fb4a7c9..9ef1eca 100644
@@ -104,35 +104,6 @@ static int page_zero_filled(void *ptr)
 	return 1;
 }
 
-static void zram_set_disksize(struct zram *zram, size_t totalram_bytes)
-{
-	if (!zram->disksize) {
-		pr_info(
-		"disk size not provided. You can use disksize_kb module "
-		"param to specify size.\nUsing default: (%u%% of RAM).\n",
-		default_disksize_perc_ram
-		);
-		zram->disksize = default_disksize_perc_ram *
-					(totalram_bytes / 100);
-	}
-
-	if (zram->disksize > 2 * (totalram_bytes)) {
-		pr_info(
-		"There is little point creating a zram of greater than "
-		"twice the size of memory since we expect a 2:1 compression "
-		"ratio. Note that zram uses about 0.1%% of the size of "
-		"the disk when not in use so a huge zram is "
-		"wasteful.\n"
-		"\tMemory Size: %zu kB\n"
-		"\tSize you selected: %llu kB\n"
-		"Continuing anyway ...\n",
-		totalram_bytes >> 10, zram->disksize
-		);
-	}
-
-	zram->disksize &= PAGE_MASK;
-}
-
 static void zram_free_page(struct zram *zram, size_t index)
 {
 	unsigned long handle = zram->table[index].handle;
@@ -497,6 +468,9 @@ void __zram_reset_device(struct zram *zram)
 {
 	size_t index;
 
+	if (!zram->init_done)
+		goto out;
+
 	zram->init_done = 0;
 
 	/* Free various per-device buffers */
@@ -523,8 +497,9 @@ void __zram_reset_device(struct zram *zram)
 
 	/* Reset stats */
 	memset(&zram->stats, 0, sizeof(zram->stats));
-
+out:
 	zram->disksize = 0;
+	set_capacity(zram->disk, 0);
 }
 
 void zram_reset_device(struct zram *zram)
@@ -540,13 +515,26 @@ int zram_init_device(struct zram *zram)
 	size_t num_pages;
 
 	down_write(&zram->init_lock);
-
 	if (zram->init_done) {
 		up_write(&zram->init_lock);
 		return 0;
 	}
 
Quoted 5 lines

drivers/staging/zram/zram_drv.c int zram_init_device(struct zram *zram) · line 523

Quoted 2 lines
drivers/staging/zram/zram_drv.c +1 -1
-	zram_set_disksize(zram, totalram_pages << PAGE_SHIFT);
+	BUG_ON(!zram->disksize);

It shouldn't cause a crash if user sets disksize to zero; a noop seems better.

I removed it because following patch gets rid of it. Thanks for good suggestion for document.

Signature · 8 lines
Kind regards,
Minchan Kim

--
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>
#5 lore raw

Re: [PATCH 2/2] zram: allocate metadata when disksize is set up

In reply to Nitin Gupta
Quoted 80 lines · Nitin Gupta
Quoted 79 lines · Minchan Kim

Lockdep complains about recursive deadlock of zram->init_lock. Because zram_init_device could be called in reclaim context and it requires a page with GFP_KERNEL.

We can fix it via replacing GFP_KERNEL with GFP_NOIO.
But more big problem is vzalloc in zram_init_device which calls GFP_KERNEL. We can change it with __vmalloc which can receive gfp_t.
But still we have a problem. Although __vmalloc can handle gfp_t, it calls allocation of GFP_KERNEL. That's why I sent the patch.
https://lkml.org/lkml/2012/4/23/77

Yes. Fundamental problem is utter crap API vmalloc.
If we can fix it, everyone would be happy. But life isn't simple like seeing my thread of the patch.

So next option is to give up lazy initialization and initialize it at the very disksize setting time. But it makes unnecessary metadata waste until zram is really used. But let's think about it.

1) 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   |   21 ++++-----------------
 drivers/staging/zram/zram_sysfs.c |    1 +
 2 files changed, 5 insertions(+), 17 deletions(-)
drivers/staging/zram/zram_drv.c +3 -7
index 9ef1eca..f364fb5 100644
@@ -441,16 +441,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));
@@ -458,9 +455,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);
 }
 
Quoted 7 lines

drivers/staging/zram/zram_drv.c void zram_reset_device(struct zram *zram) · line 508

Quoted 5 lines
drivers/staging/zram/zram_drv.c +1 -0
@@ -509,19 +505,12 @@ void zram_reset_device(struct zram *zram)
 	up_write(&zram->init_lock);
 }
 
+/* zram->init_lock should be hold */

s/hold/held

Done.

Quoted 3 lines

btw, shouldn't we also change GFP_KERNEL to GFP_NOIO in is_partial_io() case in both read/write handlers?

Absolutely. The previous patch isn't complete but sent by mistake. Sorry for the noise.
I just sent new patch.

Thanks.

Signature · 8 lines
Kind regards,
Minchan Kim

--
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>
#4 lore raw

Re: [PATCH 2/2] zram: allocate metadata when disksize is set up

In reply to Jerome Marchand
Quoted 89 lines · Jerome Marchand
Quoted 88 lines · Nitin Gupta
Quoted 80 lines · Minchan Kim

Lockdep complains about recursive deadlock of zram->init_lock. Because zram_init_device could be called in reclaim context and it requires a page with GFP_KERNEL.

We can fix it via replacing GFP_KERNEL with GFP_NOIO.
But more big problem is vzalloc in zram_init_device which calls GFP_KERNEL. We can change it with __vmalloc which can receive gfp_t.
But still we have a problem. Although __vmalloc can handle gfp_t, it calls allocation of GFP_KERNEL. That's why I sent the patch.
https://lkml.org/lkml/2012/4/23/77

Yes. Fundamental problem is utter crap API vmalloc.
If we can fix it, everyone would be happy. But life isn't simple like seeing my thread of the patch.

So next option is to give up lazy initialization and initialize it at the very disksize setting time. But it makes unnecessary metadata waste until zram is really used. But let's think about it.

1) 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   |   21 ++++-----------------
  drivers/staging/zram/zram_sysfs.c |    1 +
  2 files changed, 5 insertions(+), 17 deletions(-)
drivers/staging/zram/zram_drv.c +3 -7
index 9ef1eca..f364fb5 100644
@@ -441,16 +441,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));
@@ -458,9 +455,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);
  }
 

drivers/staging/zram/zram_drv.c void zram_reset_device(struct zram *zram) · line 508

Quoted 4 lines
drivers/staging/zram/zram_drv.c +1 -0
@@ -509,19 +505,12 @@ void zram_reset_device(struct zram *zram)
  	up_write(&zram->init_lock);
  }
 
+/* zram->init_lock should be hold */

s/hold/held

Quoted 5 lines
Quoted 2 lines

btw, shouldn't we also change GFP_KERNEL to GFP_NOIO in is_partial_io() case in both read/write handlers?

Good point. Actually, the one in zram_bvec_read() should actually be GFP_ATOMIC because of the kmap_atomic() above (or be moved out of

Right.

Quoted 5 lines

kmap_atomic/kunmap_atomic nest).
Another solution would be to allocate some working buffer at device init as it's done for compress_buffer/workmem. It would make zram_bvec_read/write look simpler (no need to free memory or manage kmalloc failure).

Fair enough.
I sent a patch which replace GFP_KERNEL with GFP_ATOMIC but your suggestion would be better. It could be a separate patch. I will send it.

Thanks.

Quoted 62 lines

Jerome

Quoted 45 lines

Rest of the patch looks good.

Thanks,
Nitin

Quoted 38 lines
drivers/staging/zram/zram_drv.c +0 -10
  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;
-	}
-
-	BUG_ON(!zram->disksize);
-
  	if (zram->disksize > 2 * (totalram_pages << PAGE_SHIFT)) {
  		pr_info(
  		"There is little point creating a zram of greater than "
@@ -570,7 +559,6 @@ int zram_init_device(struct zram *zram)
  	}
 
  	zram->init_done = 1;
-	up_write(&zram->init_lock);
 
  	pr_debug("Initialization done!\n");
  	return 0;
@@ -580,7 +568,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 +0 -0
index 4143af9..369db12 100644
@@ -71,6 +71,7 @@ static ssize_t disksize_store(struct device *dev,
 
Quoted 8 lines

drivers/staging/zram/zram_sysfs.c static ssize_t disksize_store(struct device *dev, · line 77

Quoted 7 lines
drivers/staging/zram/zram_sysfs.c +1 -0
  	zram->disksize = PAGE_ALIGN(disksize);
  	set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
+	zram_init_device(zram);
  	up_write(&zram->init_lock);
 
  	return len;

--
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 · 8 lines
Kind regards,
Minchan Kim

--
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>