Skip to content

Commit bb416d1

Browse files
minchanktorvalds
authored andcommitted
zram: writeback throttle
If there are lots of write IO with flash device, it could have a wearout problem of storage. To overcome the problem, admin needs to design write limitation to guarantee flash health for entire product life. This patch creates a new knob "writeback_limit" for zram. writeback_limit's default value is 0 so that it doesn't limit any writeback. If admin want to measure writeback count in a certain period, he could know it via /sys/block/zram0/bd_stat's 3rd column. If admin want to limit writeback as per-day 400M, he could do it like below. MB_SHIFT=20 4K_SHIFT=12 echo $((400<<MB_SHIFT>>4K_SHIFT)) > \ /sys/block/zram0/writeback_limit. If admin want to allow further write again, he could do it like below echo 0 > /sys/block/zram0/writeback_limit If admin want to see remaining writeback budget, cat /sys/block/zram0/writeback_limit The writeback_limit count will reset whenever you reset zram (e.g., system reboot, echo 1 > /sys/block/zramX/reset) so keeping how many of writeback happened until you reset the zram to allocate extra writeback budget in next setting is user's job. [minchan@kernel.org: v4] Link: http://lkml.kernel.org/r/20181203024045.153534-8-minchan@kernel.org Link: http://lkml.kernel.org/r/20181127055429.251614-8-minchan@kernel.org Signed-off-by: Minchan Kim <minchan@kernel.org> Reviewed-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com> Cc: Joey Pabalinas <joeypabalinas@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 23eddf3 commit bb416d1

4 files changed

Lines changed: 91 additions & 3 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,12 @@ Description:
121121
The bd_stat file is read-only and represents backing device's
122122
statistics (bd_count, bd_reads, bd_writes) in a format
123123
similar to block layer statistics file format.
124+
125+
What: /sys/block/zram<id>/writeback_limit
126+
Date: November 2018
127+
Contact: Minchan Kim <minchan@kernel.org>
128+
Description:
129+
The writeback_limit file is read-write and specifies the maximum
130+
amount of writeback ZRAM can do. The limit could be changed
131+
in run time and "0" means disable the limit.
132+
No limit is the initial state.

‎Documentation/blockdev/zram.txt‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ reset WO trigger device reset
164164
mem_used_max WO reset the `mem_used_max' counter (see later)
165165
mem_limit WO specifies the maximum amount of memory ZRAM can use
166166
to store the compressed data
167+
writeback_limit WO specifies the maximum amount of write IO zram can
168+
write out to backing device as 4KB unit
167169
max_comp_streams RW the number of possible concurrent compress operations
168170
comp_algorithm RW show and change the compression algorithm
169171
compact WO trigger memory compaction
@@ -275,6 +277,35 @@ Admin can request writeback of those idle pages at right timing via
275277

276278
With the command, zram writeback idle pages from memory to the storage.
277279

280+
If there are lots of write IO with flash device, potentially, it has
281+
flash wearout problem so that admin needs to design write limitation
282+
to guarantee storage health for entire product life.
283+
To overcome the concern, zram supports "writeback_limit".
284+
The "writeback_limit"'s default value is 0 so that it doesn't limit
285+
any writeback. If admin want to measure writeback count in a certain
286+
period, he could know it via /sys/block/zram0/bd_stat's 3rd column.
287+
288+
If admin want to limit writeback as per-day 400M, he could do it
289+
like below.
290+
291+
MB_SHIFT=20
292+
4K_SHIFT=12
293+
echo $((400<<MB_SHIFT>>4K_SHIFT)) > \
294+
/sys/block/zram0/writeback_limit.
295+
296+
If admin want to allow further write again, he could do it like below
297+
298+
echo 0 > /sys/block/zram0/writeback_limit
299+
300+
If admin want to see remaining writeback budget since he set,
301+
302+
cat /sys/block/zram0/writeback_limit
303+
304+
The writeback_limit count will reset whenever you reset zram(e.g.,
305+
system reboot, echo 1 > /sys/block/zramX/reset) so keeping how many of
306+
writeback happened until you reset the zram to allocate extra writeback
307+
budget in next setting is user's job.
308+
278309
= memory tracking
279310

280311
With CONFIG_ZRAM_MEMORY_TRACKING, user can know information of the

‎drivers/block/zram/zram_drv.c‎

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,39 @@ static ssize_t idle_store(struct device *dev,
330330
}
331331

332332
#ifdef CONFIG_ZRAM_WRITEBACK
333+
static ssize_t writeback_limit_store(struct device *dev,
334+
struct device_attribute *attr, const char *buf, size_t len)
335+
{
336+
struct zram *zram = dev_to_zram(dev);
337+
u64 val;
338+
ssize_t ret = -EINVAL;
339+
340+
if (kstrtoull(buf, 10, &val))
341+
return ret;
342+
343+
down_read(&zram->init_lock);
344+
atomic64_set(&zram->stats.bd_wb_limit, val);
345+
if (val == 0)
346+
zram->stop_writeback = false;
347+
up_read(&zram->init_lock);
348+
ret = len;
349+
350+
return ret;
351+
}
352+
353+
static ssize_t writeback_limit_show(struct device *dev,
354+
struct device_attribute *attr, char *buf)
355+
{
356+
u64 val;
357+
struct zram *zram = dev_to_zram(dev);
358+
359+
down_read(&zram->init_lock);
360+
val = atomic64_read(&zram->stats.bd_wb_limit);
361+
up_read(&zram->init_lock);
362+
363+
return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
364+
}
365+
333366
static void reset_bdev(struct zram *zram)
334367
{
335368
struct block_device *bdev;
@@ -612,6 +645,11 @@ static ssize_t writeback_store(struct device *dev,
612645
bvec.bv_len = PAGE_SIZE;
613646
bvec.bv_offset = 0;
614647

648+
if (zram->stop_writeback) {
649+
ret = -EIO;
650+
break;
651+
}
652+
615653
if (!blk_idx) {
616654
blk_idx = alloc_block_bdev(zram);
617655
if (!blk_idx) {
@@ -694,6 +732,11 @@ static ssize_t writeback_store(struct device *dev,
694732
zram_set_element(zram, index, blk_idx);
695733
blk_idx = 0;
696734
atomic64_inc(&zram->stats.pages_stored);
735+
if (atomic64_add_unless(&zram->stats.bd_wb_limit,
736+
-1 << (PAGE_SHIFT - 12), 0)) {
737+
if (atomic64_read(&zram->stats.bd_wb_limit) == 0)
738+
zram->stop_writeback = true;
739+
}
697740
next:
698741
zram_slot_unlock(zram, index);
699742
}
@@ -1018,6 +1061,7 @@ static ssize_t mm_stat_show(struct device *dev,
10181061
}
10191062

10201063
#ifdef CONFIG_ZRAM_WRITEBACK
1064+
#define FOUR_K(x) ((x) * (1 << (PAGE_SHIFT - 12)))
10211065
static ssize_t bd_stat_show(struct device *dev,
10221066
struct device_attribute *attr, char *buf)
10231067
{
@@ -1027,9 +1071,9 @@ static ssize_t bd_stat_show(struct device *dev,
10271071
down_read(&zram->init_lock);
10281072
ret = scnprintf(buf, PAGE_SIZE,
10291073
"%8llu %8llu %8llu\n",
1030-
(u64)atomic64_read(&zram->stats.bd_count) * (PAGE_SHIFT - 12),
1031-
(u64)atomic64_read(&zram->stats.bd_reads) * (PAGE_SHIFT - 12),
1032-
(u64)atomic64_read(&zram->stats.bd_writes) * (PAGE_SHIFT - 12));
1074+
FOUR_K((u64)atomic64_read(&zram->stats.bd_count)),
1075+
FOUR_K((u64)atomic64_read(&zram->stats.bd_reads)),
1076+
FOUR_K((u64)atomic64_read(&zram->stats.bd_writes)));
10331077
up_read(&zram->init_lock);
10341078

10351079
return ret;
@@ -1767,6 +1811,7 @@ static DEVICE_ATTR_RW(comp_algorithm);
17671811
#ifdef CONFIG_ZRAM_WRITEBACK
17681812
static DEVICE_ATTR_RW(backing_dev);
17691813
static DEVICE_ATTR_WO(writeback);
1814+
static DEVICE_ATTR_RW(writeback_limit);
17701815
#endif
17711816

17721817
static struct attribute *zram_disk_attrs[] = {
@@ -1782,6 +1827,7 @@ static struct attribute *zram_disk_attrs[] = {
17821827
#ifdef CONFIG_ZRAM_WRITEBACK
17831828
&dev_attr_backing_dev.attr,
17841829
&dev_attr_writeback.attr,
1830+
&dev_attr_writeback_limit.attr,
17851831
#endif
17861832
&dev_attr_io_stat.attr,
17871833
&dev_attr_mm_stat.attr,

‎drivers/block/zram/zram_drv.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ struct zram_stats {
8686
atomic64_t bd_count; /* no. of pages in backing device */
8787
atomic64_t bd_reads; /* no. of reads from backing device */
8888
atomic64_t bd_writes; /* no. of writes from backing device */
89+
atomic64_t bd_wb_limit; /* writeback limit of backing device */
8990
#endif
9091
};
9192

@@ -113,6 +114,7 @@ struct zram {
113114
*/
114115
bool claim; /* Protected by bdev->bd_mutex */
115116
struct file *backing_dev;
117+
bool stop_writeback;
116118
#ifdef CONFIG_ZRAM_WRITEBACK
117119
struct block_device *bdev;
118120
unsigned int old_block_size;

0 commit comments

Comments
 (0)