Skip to content

Commit a88cc8d

Browse files
committed
Merge branch 'akpm' (patches from Andrew)
Merge misc fixes from Andrew Morton: "14 fixes" * emailed patches from Andrew Morton <akpm@linux-foundation.org>: mm, page_alloc: do not wake kswapd with zone lock held hugetlbfs: revert "use i_mmap_rwsem for more pmd sharing synchronization" hugetlbfs: revert "Use i_mmap_rwsem to fix page fault/truncate race" mm: page_mapped: don't assume compound page is huge or THP mm/memory.c: initialise mmu_notifier_range correctly tools/vm/page_owner: use page_owner_sort in the use example kasan: fix krealloc handling for tag-based mode kasan: make tag based mode work with CONFIG_HARDENED_USERCOPY kasan, arm64: use ARCH_SLAB_MINALIGN instead of manual aligning mm, memcg: fix reclaim deadlock with writeback mm/usercopy.c: no check page span for stack objects slab: alien caches must not be initialized if the allocation of the alien cache failed fork, memcg: fix cached_stacks case zram: idle writeback fixes and cleanup
2 parents 9cb2feb + 73444bc commit a88cc8d

File tree

21 files changed

+289
-212
lines changed

21 files changed

+289
-212
lines changed

Documentation/ABI/testing/sysfs-block-zram

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,18 @@ Description:
122122
statistics (bd_count, bd_reads, bd_writes) in a format
123123
similar to block layer statistics file format.
124124

125+
What: /sys/block/zram<id>/writeback_limit_enable
126+
Date: November 2018
127+
Contact: Minchan Kim <minchan@kernel.org>
128+
Description:
129+
The writeback_limit_enable file is read-write and specifies
130+
eanbe of writeback_limit feature. "1" means eable the feature.
131+
No limit "0" is the initial state.
132+
125133
What: /sys/block/zram<id>/writeback_limit
126134
Date: November 2018
127135
Contact: Minchan Kim <minchan@kernel.org>
128136
Description:
129137
The writeback_limit file is read-write and specifies the maximum
130138
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.
139+
in run time.

Documentation/blockdev/zram.txt

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -156,22 +156,23 @@ Per-device statistics are exported as various nodes under /sys/block/zram<id>/
156156
A brief description of exported device attributes. For more details please
157157
read Documentation/ABI/testing/sysfs-block-zram.
158158

159-
Name access description
160-
---- ------ -----------
161-
disksize RW show and set the device's disk size
162-
initstate RO shows the initialization state of the device
163-
reset WO trigger device reset
164-
mem_used_max WO reset the `mem_used_max' counter (see later)
165-
mem_limit WO specifies the maximum amount of memory ZRAM can use
166-
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
169-
max_comp_streams RW the number of possible concurrent compress operations
170-
comp_algorithm RW show and change the compression algorithm
171-
compact WO trigger memory compaction
172-
debug_stat RO this file is used for zram debugging purposes
173-
backing_dev RW set up backend storage for zram to write out
174-
idle WO mark allocated slot as idle
159+
Name access description
160+
---- ------ -----------
161+
disksize RW show and set the device's disk size
162+
initstate RO shows the initialization state of the device
163+
reset WO trigger device reset
164+
mem_used_max WO reset the `mem_used_max' counter (see later)
165+
mem_limit WO specifies the maximum amount of memory ZRAM can use
166+
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
169+
writeback_limit_enable RW show and set writeback_limit feature
170+
max_comp_streams RW the number of possible concurrent compress operations
171+
comp_algorithm RW show and change the compression algorithm
172+
compact WO trigger memory compaction
173+
debug_stat RO this file is used for zram debugging purposes
174+
backing_dev RW set up backend storage for zram to write out
175+
idle WO mark allocated slot as idle
175176

176177

177178
User space is advised to use the following files to read the device statistics.
@@ -280,32 +281,51 @@ With the command, zram writeback idle pages from memory to the storage.
280281
If there are lots of write IO with flash device, potentially, it has
281282
flash wearout problem so that admin needs to design write limitation
282283
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.
284+
285+
To overcome the concern, zram supports "writeback_limit" feature.
286+
The "writeback_limit_enable"'s default value is 0 so that it doesn't limit
287+
any writeback. IOW, if admin want to apply writeback budget, he should
288+
enable writeback_limit_enable via
289+
290+
$ echo 1 > /sys/block/zramX/writeback_limit_enable
291+
292+
Once writeback_limit_enable is set, zram doesn't allow any writeback
293+
until admin set the budget via /sys/block/zramX/writeback_limit.
294+
295+
(If admin doesn't enable writeback_limit_enable, writeback_limit's value
296+
assigned via /sys/block/zramX/writeback_limit is meaninless.)
287297

288298
If admin want to limit writeback as per-day 400M, he could do it
289299
like below.
290300

291-
MB_SHIFT=20
292-
4K_SHIFT=12
293-
echo $((400<<MB_SHIFT>>4K_SHIFT)) > \
294-
/sys/block/zram0/writeback_limit.
301+
$ MB_SHIFT=20
302+
$ 4K_SHIFT=12
303+
$ echo $((400<<MB_SHIFT>>4K_SHIFT)) > \
304+
/sys/block/zram0/writeback_limit.
305+
$ echo 1 > /sys/block/zram0/writeback_limit_enable
295306

296-
If admin want to allow further write again, he could do it like below
307+
If admin want to allow further write again once the bugdet is exausted,
308+
he could do it like below
297309

298-
echo 0 > /sys/block/zram0/writeback_limit
310+
$ echo $((400<<MB_SHIFT>>4K_SHIFT)) > \
311+
/sys/block/zram0/writeback_limit
299312

300313
If admin want to see remaining writeback budget since he set,
301314

302-
cat /sys/block/zram0/writeback_limit
315+
$ cat /sys/block/zramX/writeback_limit
316+
317+
If admin want to disable writeback limit, he could do
318+
319+
$ echo 0 > /sys/block/zramX/writeback_limit_enable
303320

304321
The writeback_limit count will reset whenever you reset zram(e.g.,
305322
system reboot, echo 1 > /sys/block/zramX/reset) so keeping how many of
306323
writeback happened until you reset the zram to allocate extra writeback
307324
budget in next setting is user's job.
308325

326+
If admin want to measure writeback count in a certain period, he could
327+
know it via /sys/block/zram0/bd_stat's 3rd column.
328+
309329
= memory tracking
310330

311331
With CONFIG_ZRAM_MEMORY_TRACKING, user can know information of the

arch/arm64/include/asm/cache.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@
5858
*/
5959
#define ARCH_DMA_MINALIGN (128)
6060

61+
#ifdef CONFIG_KASAN_SW_TAGS
62+
#define ARCH_SLAB_MINALIGN (1ULL << KASAN_SHADOW_SCALE_SHIFT)
63+
#else
64+
#define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
65+
#endif
66+
6167
#ifndef __ASSEMBLY__
6268

6369
#include <linux/bitops.h>

drivers/block/zram/zram_drv.c

Lines changed: 66 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,9 @@ static ssize_t idle_store(struct device *dev,
316316
* See the comment in writeback_store.
317317
*/
318318
zram_slot_lock(zram, index);
319-
if (!zram_allocated(zram, index) ||
320-
zram_test_flag(zram, index, ZRAM_UNDER_WB))
321-
goto next;
322-
zram_set_flag(zram, index, ZRAM_IDLE);
323-
next:
319+
if (zram_allocated(zram, index) &&
320+
!zram_test_flag(zram, index, ZRAM_UNDER_WB))
321+
zram_set_flag(zram, index, ZRAM_IDLE);
324322
zram_slot_unlock(zram, index);
325323
}
326324

@@ -330,6 +328,41 @@ static ssize_t idle_store(struct device *dev,
330328
}
331329

332330
#ifdef CONFIG_ZRAM_WRITEBACK
331+
static ssize_t writeback_limit_enable_store(struct device *dev,
332+
struct device_attribute *attr, const char *buf, size_t len)
333+
{
334+
struct zram *zram = dev_to_zram(dev);
335+
u64 val;
336+
ssize_t ret = -EINVAL;
337+
338+
if (kstrtoull(buf, 10, &val))
339+
return ret;
340+
341+
down_read(&zram->init_lock);
342+
spin_lock(&zram->wb_limit_lock);
343+
zram->wb_limit_enable = val;
344+
spin_unlock(&zram->wb_limit_lock);
345+
up_read(&zram->init_lock);
346+
ret = len;
347+
348+
return ret;
349+
}
350+
351+
static ssize_t writeback_limit_enable_show(struct device *dev,
352+
struct device_attribute *attr, char *buf)
353+
{
354+
bool val;
355+
struct zram *zram = dev_to_zram(dev);
356+
357+
down_read(&zram->init_lock);
358+
spin_lock(&zram->wb_limit_lock);
359+
val = zram->wb_limit_enable;
360+
spin_unlock(&zram->wb_limit_lock);
361+
up_read(&zram->init_lock);
362+
363+
return scnprintf(buf, PAGE_SIZE, "%d\n", val);
364+
}
365+
333366
static ssize_t writeback_limit_store(struct device *dev,
334367
struct device_attribute *attr, const char *buf, size_t len)
335368
{
@@ -341,9 +374,9 @@ static ssize_t writeback_limit_store(struct device *dev,
341374
return ret;
342375

343376
down_read(&zram->init_lock);
344-
atomic64_set(&zram->stats.bd_wb_limit, val);
345-
if (val == 0)
346-
zram->stop_writeback = false;
377+
spin_lock(&zram->wb_limit_lock);
378+
zram->bd_wb_limit = val;
379+
spin_unlock(&zram->wb_limit_lock);
347380
up_read(&zram->init_lock);
348381
ret = len;
349382

@@ -357,7 +390,9 @@ static ssize_t writeback_limit_show(struct device *dev,
357390
struct zram *zram = dev_to_zram(dev);
358391

359392
down_read(&zram->init_lock);
360-
val = atomic64_read(&zram->stats.bd_wb_limit);
393+
spin_lock(&zram->wb_limit_lock);
394+
val = zram->bd_wb_limit;
395+
spin_unlock(&zram->wb_limit_lock);
361396
up_read(&zram->init_lock);
362397

363398
return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
@@ -588,8 +623,8 @@ static int read_from_bdev_async(struct zram *zram, struct bio_vec *bvec,
588623
return 1;
589624
}
590625

591-
#define HUGE_WRITEBACK 0x1
592-
#define IDLE_WRITEBACK 0x2
626+
#define HUGE_WRITEBACK 1
627+
#define IDLE_WRITEBACK 2
593628

594629
static ssize_t writeback_store(struct device *dev,
595630
struct device_attribute *attr, const char *buf, size_t len)
@@ -602,7 +637,7 @@ static ssize_t writeback_store(struct device *dev,
602637
struct page *page;
603638
ssize_t ret, sz;
604639
char mode_buf[8];
605-
unsigned long mode = -1UL;
640+
int mode = -1;
606641
unsigned long blk_idx = 0;
607642

608643
sz = strscpy(mode_buf, buf, sizeof(mode_buf));
@@ -618,7 +653,7 @@ static ssize_t writeback_store(struct device *dev,
618653
else if (!strcmp(mode_buf, "huge"))
619654
mode = HUGE_WRITEBACK;
620655

621-
if (mode == -1UL)
656+
if (mode == -1)
622657
return -EINVAL;
623658

624659
down_read(&zram->init_lock);
@@ -645,10 +680,13 @@ static ssize_t writeback_store(struct device *dev,
645680
bvec.bv_len = PAGE_SIZE;
646681
bvec.bv_offset = 0;
647682

648-
if (zram->stop_writeback) {
683+
spin_lock(&zram->wb_limit_lock);
684+
if (zram->wb_limit_enable && !zram->bd_wb_limit) {
685+
spin_unlock(&zram->wb_limit_lock);
649686
ret = -EIO;
650687
break;
651688
}
689+
spin_unlock(&zram->wb_limit_lock);
652690

653691
if (!blk_idx) {
654692
blk_idx = alloc_block_bdev(zram);
@@ -667,10 +705,11 @@ static ssize_t writeback_store(struct device *dev,
667705
zram_test_flag(zram, index, ZRAM_UNDER_WB))
668706
goto next;
669707

670-
if ((mode & IDLE_WRITEBACK &&
671-
!zram_test_flag(zram, index, ZRAM_IDLE)) &&
672-
(mode & HUGE_WRITEBACK &&
673-
!zram_test_flag(zram, index, ZRAM_HUGE)))
708+
if (mode == IDLE_WRITEBACK &&
709+
!zram_test_flag(zram, index, ZRAM_IDLE))
710+
goto next;
711+
if (mode == HUGE_WRITEBACK &&
712+
!zram_test_flag(zram, index, ZRAM_HUGE))
674713
goto next;
675714
/*
676715
* Clearing ZRAM_UNDER_WB is duty of caller.
@@ -732,11 +771,10 @@ static ssize_t writeback_store(struct device *dev,
732771
zram_set_element(zram, index, blk_idx);
733772
blk_idx = 0;
734773
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-
}
774+
spin_lock(&zram->wb_limit_lock);
775+
if (zram->wb_limit_enable && zram->bd_wb_limit > 0)
776+
zram->bd_wb_limit -= 1UL << (PAGE_SHIFT - 12);
777+
spin_unlock(&zram->wb_limit_lock);
740778
next:
741779
zram_slot_unlock(zram, index);
742780
}
@@ -1812,6 +1850,7 @@ static DEVICE_ATTR_RW(comp_algorithm);
18121850
static DEVICE_ATTR_RW(backing_dev);
18131851
static DEVICE_ATTR_WO(writeback);
18141852
static DEVICE_ATTR_RW(writeback_limit);
1853+
static DEVICE_ATTR_RW(writeback_limit_enable);
18151854
#endif
18161855

18171856
static struct attribute *zram_disk_attrs[] = {
@@ -1828,6 +1867,7 @@ static struct attribute *zram_disk_attrs[] = {
18281867
&dev_attr_backing_dev.attr,
18291868
&dev_attr_writeback.attr,
18301869
&dev_attr_writeback_limit.attr,
1870+
&dev_attr_writeback_limit_enable.attr,
18311871
#endif
18321872
&dev_attr_io_stat.attr,
18331873
&dev_attr_mm_stat.attr,
@@ -1867,7 +1907,9 @@ static int zram_add(void)
18671907
device_id = ret;
18681908

18691909
init_rwsem(&zram->init_lock);
1870-
1910+
#ifdef CONFIG_ZRAM_WRITEBACK
1911+
spin_lock_init(&zram->wb_limit_lock);
1912+
#endif
18711913
queue = blk_alloc_queue(GFP_KERNEL);
18721914
if (!queue) {
18731915
pr_err("Error allocating disk queue for device %d\n",

drivers/block/zram/zram_drv.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ 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 */
9089
#endif
9190
};
9291

@@ -114,8 +113,10 @@ struct zram {
114113
*/
115114
bool claim; /* Protected by bdev->bd_mutex */
116115
struct file *backing_dev;
117-
bool stop_writeback;
118116
#ifdef CONFIG_ZRAM_WRITEBACK
117+
spinlock_t wb_limit_lock;
118+
bool wb_limit_enable;
119+
u64 bd_wb_limit;
119120
struct block_device *bdev;
120121
unsigned int old_block_size;
121122
unsigned long *bitmap;

0 commit comments

Comments
 (0)