Skip to content

Commit 461a8ee

Browse files
minchanktorvalds
authored andcommitted
zram: report maximum used memory
Normally, zram user could get maximum memory usage zram consumed via polling mem_used_total with sysfs in userspace. But it has a critical problem because user can miss peak memory usage during update inverval of polling. For avoiding that, user should poll it with shorter interval(ie, 0.0000000001s) with mlocking to avoid page fault delay when memory pressure is heavy. It would be troublesome. This patch adds new knob "mem_used_max" so user could see the maximum memory usage easily via reading the knob and reset it via "echo 0 > /sys/block/zram0/mem_used_max". Signed-off-by: Minchan Kim <minchan@kernel.org> Reviewed-by: Dan Streetman <ddstreet@ieee.org> Cc: Sergey Senozhatsky <sergey.senozhatsky@gmail.com> Cc: Jerome Marchand <jmarchan@redhat.com> Cc: <juno.choi@lge.com> Cc: <seungho1.park@lge.com> Cc: Luigi Semenzato <semenzato@google.com> Cc: Nitin Gupta <ngupta@vflare.org> Cc: Seth Jennings <sjennings@variantweb.net> Reviewed-by: David Horner <ds2horner@gmail.com> Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 9ada9da commit 461a8ee

4 files changed

Lines changed: 70 additions & 2 deletions

File tree

‎Documentation/ABI/testing/sysfs-block-zram‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ Description:
120120
statistic.
121121
Unit: bytes
122122

123+
What: /sys/block/zram<id>/mem_used_max
124+
Date: August 2014
125+
Contact: Minchan Kim <minchan@kernel.org>
126+
Description:
127+
The mem_used_max file is read/write and specifies the amount
128+
of maximum memory zram have consumed to store compressed data.
129+
For resetting the value, you should write "0". Otherwise,
130+
you could see -EINVAL.
131+
Unit: bytes
132+
123133
What: /sys/block/zram<id>/mem_limit
124134
Date: August 2014
125135
Contact: Minchan Kim <minchan@kernel.org>

‎Documentation/blockdev/zram.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ size of the disk when not in use so a huge zram is wasteful.
111111
orig_data_size
112112
compr_data_size
113113
mem_used_total
114+
mem_used_max
114115

115116
8) Deactivate:
116117
swapoff /dev/zram0

‎drivers/block/zram/zram_drv.c‎

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,41 @@ static ssize_t mem_limit_store(struct device *dev,
153153
return len;
154154
}
155155

156+
static ssize_t mem_used_max_show(struct device *dev,
157+
struct device_attribute *attr, char *buf)
158+
{
159+
u64 val = 0;
160+
struct zram *zram = dev_to_zram(dev);
161+
162+
down_read(&zram->init_lock);
163+
if (init_done(zram))
164+
val = atomic_long_read(&zram->stats.max_used_pages);
165+
up_read(&zram->init_lock);
166+
167+
return scnprintf(buf, PAGE_SIZE, "%llu\n", val << PAGE_SHIFT);
168+
}
169+
170+
static ssize_t mem_used_max_store(struct device *dev,
171+
struct device_attribute *attr, const char *buf, size_t len)
172+
{
173+
int err;
174+
unsigned long val;
175+
struct zram *zram = dev_to_zram(dev);
176+
struct zram_meta *meta = zram->meta;
177+
178+
err = kstrtoul(buf, 10, &val);
179+
if (err || val != 0)
180+
return -EINVAL;
181+
182+
down_read(&zram->init_lock);
183+
if (init_done(zram))
184+
atomic_long_set(&zram->stats.max_used_pages,
185+
zs_get_total_pages(meta->mem_pool));
186+
up_read(&zram->init_lock);
187+
188+
return len;
189+
}
190+
156191
static ssize_t max_comp_streams_store(struct device *dev,
157192
struct device_attribute *attr, const char *buf, size_t len)
158193
{
@@ -465,6 +500,21 @@ static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
465500
return ret;
466501
}
467502

503+
static inline void update_used_max(struct zram *zram,
504+
const unsigned long pages)
505+
{
506+
int old_max, cur_max;
507+
508+
old_max = atomic_long_read(&zram->stats.max_used_pages);
509+
510+
do {
511+
cur_max = old_max;
512+
if (pages > cur_max)
513+
old_max = atomic_long_cmpxchg(
514+
&zram->stats.max_used_pages, cur_max, pages);
515+
} while (old_max != cur_max);
516+
}
517+
468518
static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
469519
int offset)
470520
{
@@ -476,6 +526,7 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
476526
struct zram_meta *meta = zram->meta;
477527
struct zcomp_strm *zstrm;
478528
bool locked = false;
529+
unsigned long alloced_pages;
479530

480531
page = bvec->bv_page;
481532
if (is_partial_io(bvec)) {
@@ -545,13 +596,15 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
545596
goto out;
546597
}
547598

548-
if (zram->limit_pages &&
549-
zs_get_total_pages(meta->mem_pool) > zram->limit_pages) {
599+
alloced_pages = zs_get_total_pages(meta->mem_pool);
600+
if (zram->limit_pages && alloced_pages > zram->limit_pages) {
550601
zs_free(meta->mem_pool, handle);
551602
ret = -ENOMEM;
552603
goto out;
553604
}
554605

606+
update_used_max(zram, alloced_pages);
607+
555608
cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_WO);
556609

557610
if ((clen == PAGE_SIZE) && !is_partial_io(bvec)) {
@@ -901,6 +954,8 @@ static DEVICE_ATTR(orig_data_size, S_IRUGO, orig_data_size_show, NULL);
901954
static DEVICE_ATTR(mem_used_total, S_IRUGO, mem_used_total_show, NULL);
902955
static DEVICE_ATTR(mem_limit, S_IRUGO | S_IWUSR, mem_limit_show,
903956
mem_limit_store);
957+
static DEVICE_ATTR(mem_used_max, S_IRUGO | S_IWUSR, mem_used_max_show,
958+
mem_used_max_store);
904959
static DEVICE_ATTR(max_comp_streams, S_IRUGO | S_IWUSR,
905960
max_comp_streams_show, max_comp_streams_store);
906961
static DEVICE_ATTR(comp_algorithm, S_IRUGO | S_IWUSR,
@@ -930,6 +985,7 @@ static struct attribute *zram_disk_attrs[] = {
930985
&dev_attr_compr_data_size.attr,
931986
&dev_attr_mem_used_total.attr,
932987
&dev_attr_mem_limit.attr,
988+
&dev_attr_mem_used_max.attr,
933989
&dev_attr_max_comp_streams.attr,
934990
&dev_attr_comp_algorithm.attr,
935991
NULL,

‎drivers/block/zram/zram_drv.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ struct zram_stats {
9090
atomic64_t notify_free; /* no. of swap slot free notifications */
9191
atomic64_t zero_pages; /* no. of zero filled pages */
9292
atomic64_t pages_stored; /* no. of pages currently stored */
93+
atomic_long_t max_used_pages; /* no. of maximum pages stored */
9394
};
9495

9596
struct zram_meta {

0 commit comments

Comments
 (0)