zsmalloc: move pages_allocated to zs_pool
Subsystems (5)
Minchan Kim
maintainerPATCH 1/2 zsmalloc: move pages_allocated to zs_pool
Pages_allocated has counted in size_class structure and when user want to see total_size_bytes, it gathers all of value from each size_class to report the sum.
It's not bad if user don't see the value often but if user start to see the value frequently, it would be not a good deal for performance POV.
Even, this patch moves the variable from size_class to zs_pool so it reduces memory footprint (from [255 * 8byte] to [sizeof(atomic_t)]) but it introduce new atomic opearation but it's not a big deal because atomic operation is called on slow path of zsmalloc where it allocates/free zspage unit, not object.
--- mm/zsmalloc.c | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-)
mm/zsmalloc.c +7 -14
@@ -199,9 +199,6 @@ struct size_class {spinlock_t lock; -/* stats */ -u64 pages_allocated; -struct page *fullness_list[_ZS_NR_FULLNESS_GROUPS]; };
@@ -220,6 +217,7 @@ struct zs_pool {struct size_class size_class[ZS_SIZE_CLASSES ];gfp_t flags;/* allocation flags used when growing pool */ +atomic_t pages_allocated; };/*
@@ -1027,8 +1025,8 @@ unsigned long zs_malloc(struct zs_pool *pool, size_t size)return 0 ;set_zspage_mapping (first_page, class->index,ZS_EMPTY ); +atomic_add (class->pages_per_zspage, &pool->pages_allocated);spin_lock (&class->lock); - class->pages_allocated += class->pages_per_zspage; } obj = (unsigned long )first_page->freelist;
@@ -1081,14 +1079,12 @@ void zs_free(struct zs_pool *pool, unsigned long obj) first_page->inuse--; fullness =fix_fullness_group (pool, first_page); - -if (fullness ==ZS_EMPTY ) - class->pages_allocated -= class->pages_per_zspage; -spin_unlock (&class->lock); -if (fullness ==ZS_EMPTY ) +if (fullness ==ZS_EMPTY ) { +atomic_sub (class->pages_per_zspage, &pool->pages_allocated);free_zspage (first_page); + } }EXPORT_SYMBOL_GPL (zs_free);
@@ -1184,12 +1180,9 @@ EXPORT_SYMBOL_GPL(zs_unmap_object);u64 zs_get_total_size_bytes (struct zs_pool *pool) { -int i; -u64 npages =0 ; - -for (i =0 ; i <ZS_SIZE_CLASSES ; i++) - npages += pool->size_class[i].pages_allocated; +u64 npages; + npages =atomic_read (&pool->pages_allocated);return npages <<PAGE_SHIFT ; }EXPORT_SYMBOL_GPL (zs_get_total_size_bytes);
Signature · 7 lines
2.0.0 -- 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>
Minchan Kim
maintainerPATCH 2/2 zram: limit memory size for zram
Since zram has no control feature to limit memory usage, it makes hard to manage system memrory.
This patch adds new knob "mem_limit" via sysfs to set up the limit.
Note: I added the logic in zram, not zsmalloc because the limit is requirement of zram, not zsmalloc so I'd like to avoid unnecessary branch in zsmalloc.
--- Documentation/blockdev/zram.txt | 20 +++++++++++++++---- drivers/block/zram/zram_drv.c | 43 +++++++++++++++++++++++++++++++++++++++++ drivers/block/zram/zram_drv.h | 1 + 3 files changed, 60 insertions(+), 4 deletions(-)
Documentation/blockdev/zram.txt +16 -4
@@ -74,14 +74,26 @@ 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. -5) Activate: +5) Set memory limit: Optional + Set memory limit by writing the value to sysfs node 'mem_limit'. + The value can be either in bytes or you can use mem suffixes. + Examples: + # limit /dev/zram0 with 50MB memory + echo $((50*1024*1024)) > /sys/block/zram0/mem_limit + + # Using mem suffixes + echo 256K > /sys/block/zram0/mem_limit + echo 512M > /sys/block/zram0/mem_limit + echo 1G > /sys/block/zram0/mem_limit + +6) Activate: mkswap /dev/zram0 swapon /dev/zram0 mkfs.ext4 /dev/zram1 mount /dev/zram1 /tmp -6) Stats: +7) Stats: Per-device statistics are exported as various nodes under /sys/block/zram<id>/ disksize
@@ -96,11 +108,11 @@ size of the disk when not in use so a huge zram is wasteful. compr_data_size mem_used_total -7) Deactivate: +8) Deactivate: swapoff /dev/zram0 umount /dev/zram1 -8) Reset: +9) Reset: Write any positive value to 'reset' sysfs node echo 1 > /sys/block/zram0/reset echo 1 > /sys/block/zram1/reset
drivers/block/zram/zram_drv.c +43 -0
73 lines not on this page · raw message
drivers/block/zram/zram_drv.h +1 -0
@@ -110,6 +110,7 @@ struct zram {* we can store in a disk. */ u64 disksize;/* bytes */ +u64 limit_bytes;int max_comp_streams;struct zram_stats stats;char compressor[10 ];
Signature · 1 line
2.0.0
Just resent new one with correcting Seth's mail address and including the one omitted by mistake in this patchset.
Quoted 87 lines · Minchan Kim
Pages_allocatedhas counted insize_classstructure and when user want to seetotal_size_bytes, it gathers all of value from eachsize_classto report the sum.It's not bad if user don't see the value often but if user start to see the value frequently, it would be not a good deal for performance POV.
Even, this patch moves the variable from
size_classtozs_poolso it reduces memory footprint (from [255 * 8byte] to [sizeof(atomic_t)]) but it introduce new atomic opearation but it's not a big deal because atomic operation is called on slow path of zsmalloc where it allocates/free zspage unit, not object.Signed-off-by Minchan Kim <[email protected]>--- mm/zsmalloc.c | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-)mm/zsmalloc.c +7
-14@@ -199,9 +199,6 @@ struct size_class {spinlock_t lock; -/* stats */ -u64 pages_allocated; -struct page *fullness_list[_ZS_NR_FULLNESS_GROUPS]; };@@ -220,6 +217,7 @@ struct zs_pool {struct size_class size_class[ZS_SIZE_CLASSES ];gfp_t flags;/* allocation flags used when growing pool */ +atomic_t pages_allocated; };/* @@ -1027,8 +1025,8 @@ unsigned long zs_malloc(struct zs_pool *pool, size_t size)return 0 ;set_zspage_mapping (first_page, class->index,ZS_EMPTY ); +atomic_add (class->pages_per_zspage, &pool->pages_allocated);spin_lock (&class->lock); - class->pages_allocated += class->pages_per_zspage; } obj = (unsigned long )first_page->freelist;@@ -1081,14 +1079,12 @@ void zs_free(struct zs_pool *pool, unsigned long obj) first_page->inuse--; fullness =fix_fullness_group (pool, first_page); - -if (fullness ==ZS_EMPTY ) - class->pages_allocated -= class->pages_per_zspage; -spin_unlock (&class->lock); -if (fullness ==ZS_EMPTY ) +if (fullness ==ZS_EMPTY ) { +atomic_sub (class->pages_per_zspage, &pool->pages_allocated);free_zspage (first_page); + } }EXPORT_SYMBOL_GPL (zs_free);@@ -1184,12 +1180,9 @@ EXPORT_SYMBOL_GPL(zs_unmap_object);u64 zs_get_total_size_bytes (struct zs_pool *pool) { -int i; -u64 npages =0 ; - -for (i =0 ; i <ZS_SIZE_CLASSES ; i++) - npages += pool->size_class[i].pages_allocated; +u64 npages; + npages =atomic_read (&pool->pages_allocated);return npages <<PAGE_SHIFT ; }EXPORT_SYMBOL_GPL (zs_get_total_size_bytes);Signature · 1 line
2.0.0
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>
Re: [PATCH 2/2] zram: limit memory size for zram
In reply to Minchan KimQuoted 84 lines · Minchan Kim
Since zram has no control feature to limit memory usage, it makes hard to manage system memrory.
This patch adds new knob "
mem_limit" via sysfs to set up the limit.Note: I added the logic in zram, not zsmalloc because the limit is requirement of zram, not zsmalloc so I'd like to avoid unnecessary branch in zsmalloc.
Signed-off-by Minchan Kim <[email protected]>--- Documentation/blockdev/zram.txt | 20 +++++++++++++++---- drivers/block/zram/zram_drv.c | 43 +++++++++++++++++++++++++++++++++++++++++ drivers/block/zram/zram_drv.h | 1 + 3 files changed, 60 insertions(+), 4 deletions(-)Documentation/blockdev/zram.txt +16
-4@@ -74,14 +74,26 @@ 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. -5) Activate: +5) Set memory limit: Optional + Set memory limit by writing the value to sysfs node 'mem_limit'. + The value can be either in bytes or you can use mem suffixes. + Examples: + # limit /dev/zram0 with 50MB memory + echo $((50*1024*1024)) > /sys/block/zram0/mem_limit + + # Using mem suffixes + echo 256K > /sys/block/zram0/mem_limit + echo 512M > /sys/block/zram0/mem_limit + echo 1G > /sys/block/zram0/mem_limit + +6) Activate: mkswap /dev/zram0 swapon /dev/zram0 mkfs.ext4 /dev/zram1 mount /dev/zram1 /tmp -6) Stats: +7) Stats: Per-device statistics are exported as various nodes under /sys/block/zram<id>/ disksize@@ -96,11 +108,11 @@ size of the disk when not in use so a huge zram is wasteful. compr_data_size mem_used_total -7) Deactivate: +8) Deactivate: swapoff /dev/zram0 umount /dev/zram1 -8) Reset: +9) Reset: Write any positive value to 'reset' sysfs node echo 1 > /sys/block/zram0/reset echo 1 > /sys/block/zram1/resetdrivers/block/zram/zram_drv.c +12
-0@@ -122,6 +122,35 @@ static ssize_t max_comp_streams_show(struct device *dev,return scnprintf (buf,PAGE_SIZE ,"%d\n" , val); } +static ssize_t mem_limit_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +u64 val; +struct zram *zram =dev_to_zram (dev); + +down_read (&zram->init_lock); + val = zram->limit_bytes; +up_read (&zram->init_lock); + +return scnprintf (buf,PAGE_SIZE ,"%llu\n" , val); +}
drivers/block/zram/zram_drv.c static ssize_t max_comp_streams_show(struct device *dev, · line 146
Quoted 10 lines
drivers/block/zram/zram_drv.c +10
-0+ +static ssize_t mem_limit_store (struct device *dev, +struct device_attribute *attr,const char *buf,size_t len) +{ +u64 limit; +struct zram *zram =dev_to_zram (dev); + + limit =memparse (buf,NULL ); +if (!limit) +return -EINVAL ;
Shouldn't passing a 0 limit be allowed, to disable the limit?
Quoted 65 lines
drivers/block/zram/zram_drv.c +21
-0+ +down_write (&zram->init_lock); + zram->limit_bytes = limit; +up_write (&zram->init_lock); +return len; +} +static ssize_t max_comp_streams_store (struct device *dev,struct device_attribute *attr,const char *buf,size_t len) {@@ -513,6 +542,14 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index, ret = -ENOMEM ; goto out; } + +if (zram->limit_bytes && +zs_get_total_size_bytes (meta->mem_pool) > zram->limit_bytes) { +zs_free (meta->mem_pool, handle); + ret = -ENOMEM ; + goto out; + } + cmem =zs_map_object (meta->mem_pool, handle,ZS_MM_WO );if ((clen ==PAGE_SIZE ) && !is_partial_io (bvec)) {@@ -617,6 +654,9 @@ static void zram_reset_device(struct zram *zram, bool reset_capacity)struct zram_meta *meta;down_write (&zram->init_lock); + + zram->limit_bytes =0 ; +if (!init_done (zram)) {up_write (&zram->init_lock);return ;@@ -857,6 +897,8 @@ static DEVICE_ATTR(initstate, S_IRUGO, initstate_show, NULL); static DEVICE_ATTR(reset, S_IWUSR, NULL, reset_store); static DEVICE_ATTR(orig_data_size, S_IRUGO, orig_data_size_show, NULL); static DEVICE_ATTR(mem_used_total, S_IRUGO, mem_used_total_show, NULL); +static DEVICE_ATTR(mem_limit, S_IRUGO | S_IWUSR, mem_limit_show, + mem_limit_store); static DEVICE_ATTR(max_comp_streams, S_IRUGO | S_IWUSR, max_comp_streams_show, max_comp_streams_store); static DEVICE_ATTR(comp_algorithm, S_IRUGO | S_IWUSR,@@ -885,6 +927,7 @@ static struct attribute *zram_disk_attrs[] = { &dev_attr_orig_data_size.attr, &dev_attr_compr_data_size.attr, &dev_attr_mem_used_total.attr, + &dev_attr_mem_limit.attr, &dev_attr_max_comp_streams.attr, &dev_attr_comp_algorithm.attr, NULL,drivers/block/zram/zram_drv.h +1
-1@@ -110,6 +110,7 @@ struct zram {* we can store in a disk. */ u64 disksize;/* bytes */ +u64 limit_bytes;int max_comp_streams;struct zram_stats stats;char compressor[10 ]; --
Quoted 2 lines
2.0.0
--
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>
Minchan Kim
maintainerRe: [PATCH 2/2] zram: limit memory size for zram
Hi Dan,
Quoted 89 lines · Dan Streetman
Quoted 88 lines · Minchan Kim
Since zram has no control feature to limit memory usage, it makes hard to manage system memrory.
This patch adds new knob "
mem_limit" via sysfs to set up the limit.Note: I added the logic in zram, not zsmalloc because the limit is requirement of zram, not zsmalloc so I'd like to avoid unnecessary branch in zsmalloc.
Signed-off-by Minchan Kim <[email protected]>--- Documentation/blockdev/zram.txt | 20 +++++++++++++++---- drivers/block/zram/zram_drv.c | 43 +++++++++++++++++++++++++++++++++++++++++ drivers/block/zram/zram_drv.h | 1 + 3 files changed, 60 insertions(+), 4 deletions(-)Documentation/blockdev/zram.txt +16
-4@@ -74,14 +74,26 @@ 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. -5) Activate: +5) Set memory limit: Optional + Set memory limit by writing the value to sysfs node 'mem_limit'. + The value can be either in bytes or you can use mem suffixes. + Examples: + # limit /dev/zram0 with 50MB memory + echo $((50*1024*1024)) > /sys/block/zram0/mem_limit + + # Using mem suffixes + echo 256K > /sys/block/zram0/mem_limit + echo 512M > /sys/block/zram0/mem_limit + echo 1G > /sys/block/zram0/mem_limit + +6) Activate: mkswap /dev/zram0 swapon /dev/zram0 mkfs.ext4 /dev/zram1 mount /dev/zram1 /tmp -6) Stats: +7) Stats: Per-device statistics are exported as various nodes under /sys/block/zram<id>/ disksize@@ -96,11 +108,11 @@ size of the disk when not in use so a huge zram is wasteful. compr_data_size mem_used_total -7) Deactivate: +8) Deactivate: swapoff /dev/zram0 umount /dev/zram1 -8) Reset: +9) Reset: Write any positive value to 'reset' sysfs node echo 1 > /sys/block/zram0/reset echo 1 > /sys/block/zram1/resetdrivers/block/zram/zram_drv.c +16
-0@@ -122,6 +122,35 @@ static ssize_t max_comp_streams_show(struct device *dev,return scnprintf (buf,PAGE_SIZE ,"%d\n" , val); } +static ssize_t mem_limit_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +u64 val; +struct zram *zram =dev_to_zram (dev); + +down_read (&zram->init_lock); + val = zram->limit_bytes; +up_read (&zram->init_lock); + +return scnprintf (buf,PAGE_SIZE ,"%llu\n" , val); +} + +static ssize_t mem_limit_store (struct device *dev, +struct device_attribute *attr,const char *buf,size_t len) +{
Quoted 8 lines
drivers/block/zram/zram_drv.c static ssize_t max_comp_streams_show(struct device *dev, · line 146
Quoted 6 lines
drivers/block/zram/zram_drv.c +6
-0+u64 limit; +struct zram *zram =dev_to_zram (dev); + + limit =memparse (buf,NULL ); +if (!limit) +return -EINVAL ;Shouldn't passing a 0 limit be allowed, to disable the limit?
Sure. Will fix.
Thanks.
Signature · 2 lines
Kind regards, Minchan Kim