Skip to content

Commit beca3ec

Browse files
sergey-senozhatskytorvalds
authored andcommitted
zram: add multi stream functionality
Existing zram (zcomp) implementation has only one compression stream (buffer and algorithm private part), so in order to prevent data corruption only one write (compress operation) can use this compression stream, forcing all concurrent write operations to wait for stream lock to be released. This patch changes zcomp to keep a compression streams list of user-defined size (via sysfs device attr). Each write operation still exclusively holds compression stream, the difference is that we can have N write operations (depending on size of streams list) executing in parallel. See TEST section later in commit message for performance data. Introduce struct zcomp_strm_multi and a set of functions to manage zcomp_strm stream access. zcomp_strm_multi has a list of idle zcomp_strm structs, spinlock to protect idle list and wait queue, making it possible to perform parallel compressions. The following set of functions added: - zcomp_strm_multi_find()/zcomp_strm_multi_release() find and release a compression stream, implement required locking - zcomp_strm_multi_create()/zcomp_strm_multi_destroy() create and destroy zcomp_strm_multi zcomp ->strm_find() and ->strm_release() callbacks are set during initialisation to zcomp_strm_multi_find()/zcomp_strm_multi_release() correspondingly. Each time zcomp issues a zcomp_strm_multi_find() call, the following set of operations performed: - spin lock strm_lock - if idle list is not empty, remove zcomp_strm from idle list, spin unlock and return zcomp stream pointer to caller - if idle list is empty, current adds itself to wait queue. it will be awaken by zcomp_strm_multi_release() caller. zcomp_strm_multi_release(): - spin lock strm_lock - add zcomp stream to idle list - spin unlock, wake up sleeper Minchan Kim reported that spinlock-based locking scheme has demonstrated a severe perfomance regression for single compression stream case, comparing to mutex-based (see https://lkml.org/lkml/2014/2/18/16) base spinlock mutex ==Initial write ==Initial write ==Initial write records: 5 records: 5 records: 5 avg: 1642424.35 avg: 699610.40 avg: 1655583.71 std: 39890.95(2.43%) std: 232014.19(33.16%) std: 52293.96 max: 1690170.94 max: 1163473.45 max: 1697164.75 min: 1568669.52 min: 573429.88 min: 1553410.23 ==Rewrite ==Rewrite ==Rewrite records: 5 records: 5 records: 5 avg: 1611775.39 avg: 501406.64 avg: 1684419.11 std: 17144.58(1.06%) std: 15354.41(3.06%) std: 18367.42 max: 1641800.95 max: 531356.78 max: 1706445.84 min: 1593515.27 min: 488817.78 min: 1655335.73 When only one compression stream available, mutex with spin on owner tends to perform much better than frequent wait_event()/wake_up(). This is why single stream implemented as a special case with mutex locking. Introduce and document zram device attribute max_comp_streams. This attr shows and stores current zcomp's max number of zcomp streams (max_strm). Extend zcomp's zcomp_create() with `max_strm' parameter. `max_strm' limits the number of zcomp_strm structs in compression backend's idle list (max_comp_streams). max_comp_streams used during initialisation as follows: -- passing to zcomp_create() max_strm equals to 1 will initialise zcomp using single compression stream zcomp_strm_single (mutex-based locking). -- passing to zcomp_create() max_strm greater than 1 will initialise zcomp using multi compression stream zcomp_strm_multi (spinlock-based locking). default max_comp_streams value is 1, meaning that zram with single stream will be initialised. Later patch will introduce configuration knob to change max_comp_streams on already initialised and used zcomp. TEST iozone -t 3 -R -r 16K -s 60M -I +Z test base 1 strm (mutex) 3 strm (spinlock) ----------------------------------------------------------------------- Initial write 589286.78 583518.39 718011.05 Rewrite 604837.97 596776.38 1515125.72 Random write 584120.11 595714.58 1388850.25 Pwrite 535731.17 541117.38 739295.27 Fwrite 1418083.88 1478612.72 1484927.06 Usage example: set max_comp_streams to 4 echo 4 > /sys/block/zram0/max_comp_streams show current max_comp_streams (default value is 1). cat /sys/block/zram0/max_comp_streams Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com> Acked-by: Minchan Kim <minchan@kernel.org> Cc: Jerome Marchand <jmarchan@redhat.com> Cc: Nitin Gupta <ngupta@vflare.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 9cc9752 commit beca3ec

6 files changed

Lines changed: 201 additions & 11 deletions

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,21 @@ Description:
5050
The failed_reads file is read-only and specifies the number of
5151
failed reads happened on this device.
5252

53-
5453
What: /sys/block/zram<id>/failed_writes
5554
Date: February 2014
5655
Contact: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
5756
Description:
5857
The failed_writes file is read-only and specifies the number of
5958
failed writes happened on this device.
6059

60+
What: /sys/block/zram<id>/max_comp_streams
61+
Date: February 2014
62+
Contact: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
63+
Description:
64+
The max_comp_streams file is read-write and specifies the
65+
number of backend's zcomp_strm compression streams (number of
66+
concurrent compress operations).
67+
6168
What: /sys/block/zram<id>/notify_free
6269
Date: August 2010
6370
Contact: Nitin Gupta <ngupta@vflare.org>

‎Documentation/blockdev/zram.txt‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,28 @@ Following shows a typical sequence of steps for using zram.
2121
This creates 4 devices: /dev/zram{0,1,2,3}
2222
(num_devices parameter is optional. Default: 1)
2323

24-
2) Set Disksize
24+
2) Set max number of compression streams
25+
Compression backend may use up to max_comp_streams compression streams,
26+
thus allowing up to max_comp_streams concurrent compression operations.
27+
By default, compression backend uses single compression stream.
28+
29+
Examples:
30+
#show max compression streams number
31+
cat /sys/block/zram0/max_comp_streams
32+
33+
#set max compression streams number to 3
34+
echo 3 > /sys/block/zram0/max_comp_streams
35+
36+
Note:
37+
In order to enable compression backend's multi stream support max_comp_streams
38+
must be initially set to desired concurrency level before ZRAM device
39+
initialisation. Once the device initialised as a single stream compression
40+
backend (max_comp_streams equals to 0) changing the value of max_comp_streams
41+
will not take any effect, because single stream compression backend implemented
42+
as a special case and does not support dynamic max_comp_streams. Only multi
43+
stream backend supports dynamic max_comp_streams adjustment.
44+
45+
3) Set Disksize
2546
Set disk size by writing the value to sysfs node 'disksize'.
2647
The value can be either in bytes or you can use mem suffixes.
2748
Examples:
@@ -38,14 +59,14 @@ There is little point creating a zram of greater than twice the size of memory
3859
since we expect a 2:1 compression ratio. Note that zram uses about 0.1% of the
3960
size of the disk when not in use so a huge zram is wasteful.
4061

41-
3) Activate:
62+
4) Activate:
4263
mkswap /dev/zram0
4364
swapon /dev/zram0
4465

4566
mkfs.ext4 /dev/zram1
4667
mount /dev/zram1 /tmp
4768

48-
4) Stats:
69+
5) Stats:
4970
Per-device statistics are exported as various nodes under
5071
/sys/block/zram<id>/
5172
disksize
@@ -60,11 +81,11 @@ size of the disk when not in use so a huge zram is wasteful.
6081
compr_data_size
6182
mem_used_total
6283

63-
5) Deactivate:
84+
6) Deactivate:
6485
swapoff /dev/zram0
6586
umount /dev/zram1
6687

67-
6) Reset:
88+
7) Reset:
6889
Write any positive value to 'reset' sysfs node
6990
echo 1 > /sys/block/zram0/reset
7091
echo 1 > /sys/block/zram1/reset

‎drivers/block/zram/zcomp.c‎

Lines changed: 122 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@ struct zcomp_strm_single {
2424
struct zcomp_strm *zstrm;
2525
};
2626

27+
/*
28+
* multi zcomp_strm backend
29+
*/
30+
struct zcomp_strm_multi {
31+
/* protect strm list */
32+
spinlock_t strm_lock;
33+
/* max possible number of zstrm streams */
34+
int max_strm;
35+
/* number of available zstrm streams */
36+
int avail_strm;
37+
/* list of available strms */
38+
struct list_head idle_strm;
39+
wait_queue_head_t strm_wait;
40+
};
41+
2742
static struct zcomp_backend *find_backend(const char *compress)
2843
{
2944
if (strncmp(compress, "lzo", 3) == 0)
@@ -62,6 +77,107 @@ static struct zcomp_strm *zcomp_strm_alloc(struct zcomp *comp)
6277
return zstrm;
6378
}
6479

80+
/*
81+
* get idle zcomp_strm or wait until other process release
82+
* (zcomp_strm_release()) one for us
83+
*/
84+
static struct zcomp_strm *zcomp_strm_multi_find(struct zcomp *comp)
85+
{
86+
struct zcomp_strm_multi *zs = comp->stream;
87+
struct zcomp_strm *zstrm;
88+
89+
while (1) {
90+
spin_lock(&zs->strm_lock);
91+
if (!list_empty(&zs->idle_strm)) {
92+
zstrm = list_entry(zs->idle_strm.next,
93+
struct zcomp_strm, list);
94+
list_del(&zstrm->list);
95+
spin_unlock(&zs->strm_lock);
96+
return zstrm;
97+
}
98+
/* zstrm streams limit reached, wait for idle stream */
99+
if (zs->avail_strm >= zs->max_strm) {
100+
spin_unlock(&zs->strm_lock);
101+
wait_event(zs->strm_wait, !list_empty(&zs->idle_strm));
102+
continue;
103+
}
104+
/* allocate new zstrm stream */
105+
zs->avail_strm++;
106+
spin_unlock(&zs->strm_lock);
107+
108+
zstrm = zcomp_strm_alloc(comp);
109+
if (!zstrm) {
110+
spin_lock(&zs->strm_lock);
111+
zs->avail_strm--;
112+
spin_unlock(&zs->strm_lock);
113+
wait_event(zs->strm_wait, !list_empty(&zs->idle_strm));
114+
continue;
115+
}
116+
break;
117+
}
118+
return zstrm;
119+
}
120+
121+
/* add stream back to idle list and wake up waiter or free the stream */
122+
static void zcomp_strm_multi_release(struct zcomp *comp, struct zcomp_strm *zstrm)
123+
{
124+
struct zcomp_strm_multi *zs = comp->stream;
125+
126+
spin_lock(&zs->strm_lock);
127+
if (zs->avail_strm <= zs->max_strm) {
128+
list_add(&zstrm->list, &zs->idle_strm);
129+
spin_unlock(&zs->strm_lock);
130+
wake_up(&zs->strm_wait);
131+
return;
132+
}
133+
134+
zs->avail_strm--;
135+
spin_unlock(&zs->strm_lock);
136+
zcomp_strm_free(comp, zstrm);
137+
}
138+
139+
static void zcomp_strm_multi_destroy(struct zcomp *comp)
140+
{
141+
struct zcomp_strm_multi *zs = comp->stream;
142+
struct zcomp_strm *zstrm;
143+
144+
while (!list_empty(&zs->idle_strm)) {
145+
zstrm = list_entry(zs->idle_strm.next,
146+
struct zcomp_strm, list);
147+
list_del(&zstrm->list);
148+
zcomp_strm_free(comp, zstrm);
149+
}
150+
kfree(zs);
151+
}
152+
153+
static int zcomp_strm_multi_create(struct zcomp *comp, int max_strm)
154+
{
155+
struct zcomp_strm *zstrm;
156+
struct zcomp_strm_multi *zs;
157+
158+
comp->destroy = zcomp_strm_multi_destroy;
159+
comp->strm_find = zcomp_strm_multi_find;
160+
comp->strm_release = zcomp_strm_multi_release;
161+
zs = kmalloc(sizeof(struct zcomp_strm_multi), GFP_KERNEL);
162+
if (!zs)
163+
return -ENOMEM;
164+
165+
comp->stream = zs;
166+
spin_lock_init(&zs->strm_lock);
167+
INIT_LIST_HEAD(&zs->idle_strm);
168+
init_waitqueue_head(&zs->strm_wait);
169+
zs->max_strm = max_strm;
170+
zs->avail_strm = 1;
171+
172+
zstrm = zcomp_strm_alloc(comp);
173+
if (!zstrm) {
174+
kfree(zs);
175+
return -ENOMEM;
176+
}
177+
list_add(&zstrm->list, &zs->idle_strm);
178+
return 0;
179+
}
180+
65181
static struct zcomp_strm *zcomp_strm_single_find(struct zcomp *comp)
66182
{
67183
struct zcomp_strm_single *zs = comp->stream;
@@ -139,7 +255,7 @@ void zcomp_destroy(struct zcomp *comp)
139255
* if requested algorithm is not supported or in case
140256
* of init error
141257
*/
142-
struct zcomp *zcomp_create(const char *compress)
258+
struct zcomp *zcomp_create(const char *compress, int max_strm)
143259
{
144260
struct zcomp *comp;
145261
struct zcomp_backend *backend;
@@ -153,7 +269,11 @@ struct zcomp *zcomp_create(const char *compress)
153269
return NULL;
154270

155271
comp->backend = backend;
156-
if (zcomp_strm_single_create(comp) != 0) {
272+
if (max_strm > 1)
273+
zcomp_strm_multi_create(comp, max_strm);
274+
else
275+
zcomp_strm_single_create(comp);
276+
if (!comp->stream) {
157277
kfree(comp);
158278
return NULL;
159279
}

‎drivers/block/zram/zcomp.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ struct zcomp_strm {
2121
* working memory)
2222
*/
2323
void *private;
24+
/* used in multi stream backend, protected by backend strm_lock */
25+
struct list_head list;
2426
};
2527

2628
/* static compression backend */
@@ -47,7 +49,7 @@ struct zcomp {
4749
void (*destroy)(struct zcomp *comp);
4850
};
4951

50-
struct zcomp *zcomp_create(const char *comp);
52+
struct zcomp *zcomp_create(const char *comp, int max_strm);
5153
void zcomp_destroy(struct zcomp *comp);
5254

5355
struct zcomp_strm *zcomp_strm_find(struct zcomp *comp);

‎drivers/block/zram/zram_drv.c‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,40 @@ static ssize_t mem_used_total_show(struct device *dev,
108108
return sprintf(buf, "%llu\n", val);
109109
}
110110

111+
static ssize_t max_comp_streams_show(struct device *dev,
112+
struct device_attribute *attr, char *buf)
113+
{
114+
int val;
115+
struct zram *zram = dev_to_zram(dev);
116+
117+
down_read(&zram->init_lock);
118+
val = zram->max_comp_streams;
119+
up_read(&zram->init_lock);
120+
121+
return sprintf(buf, "%d\n", val);
122+
}
123+
124+
static ssize_t max_comp_streams_store(struct device *dev,
125+
struct device_attribute *attr, const char *buf, size_t len)
126+
{
127+
int num;
128+
struct zram *zram = dev_to_zram(dev);
129+
130+
if (kstrtoint(buf, 0, &num))
131+
return -EINVAL;
132+
if (num < 1)
133+
return -EINVAL;
134+
down_write(&zram->init_lock);
135+
if (init_done(zram)) {
136+
up_write(&zram->init_lock);
137+
pr_info("Can't set max_comp_streams for initialized device\n");
138+
return -EBUSY;
139+
}
140+
zram->max_comp_streams = num;
141+
up_write(&zram->init_lock);
142+
return len;
143+
}
144+
111145
/* flag operations needs meta->tb_lock */
112146
static int zram_test_flag(struct zram_meta *meta, u32 index,
113147
enum zram_pageflags flag)
@@ -502,6 +536,8 @@ static void zram_reset_device(struct zram *zram, bool reset_capacity)
502536
}
503537

504538
zcomp_destroy(zram->comp);
539+
zram->max_comp_streams = 1;
540+
505541
zram_meta_free(zram->meta);
506542
zram->meta = NULL;
507543
/* Reset stats */
@@ -537,7 +573,7 @@ static ssize_t disksize_store(struct device *dev,
537573
goto out_free_meta;
538574
}
539575

540-
zram->comp = zcomp_create(default_compressor);
576+
zram->comp = zcomp_create(default_compressor, zram->max_comp_streams);
541577
if (!zram->comp) {
542578
pr_info("Cannot initialise %s compressing backend\n",
543579
default_compressor);
@@ -698,6 +734,8 @@ static DEVICE_ATTR(initstate, S_IRUGO, initstate_show, NULL);
698734
static DEVICE_ATTR(reset, S_IWUSR, NULL, reset_store);
699735
static DEVICE_ATTR(orig_data_size, S_IRUGO, orig_data_size_show, NULL);
700736
static DEVICE_ATTR(mem_used_total, S_IRUGO, mem_used_total_show, NULL);
737+
static DEVICE_ATTR(max_comp_streams, S_IRUGO | S_IWUSR,
738+
max_comp_streams_show, max_comp_streams_store);
701739

702740
ZRAM_ATTR_RO(num_reads);
703741
ZRAM_ATTR_RO(num_writes);
@@ -722,6 +760,7 @@ static struct attribute *zram_disk_attrs[] = {
722760
&dev_attr_orig_data_size.attr,
723761
&dev_attr_compr_data_size.attr,
724762
&dev_attr_mem_used_total.attr,
763+
&dev_attr_max_comp_streams.attr,
725764
NULL,
726765
};
727766

@@ -784,6 +823,7 @@ static int create_device(struct zram *zram, int device_id)
784823
}
785824

786825
zram->meta = NULL;
826+
zram->max_comp_streams = 1;
787827
return 0;
788828

789829
out_free_disk:

‎drivers/block/zram/zram_drv.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ struct zram {
9999
* we can store in a disk.
100100
*/
101101
u64 disksize; /* bytes */
102-
102+
int max_comp_streams;
103103
struct zram_stats stats;
104104
};
105105
#endif

0 commit comments

Comments
 (0)