aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/block/zram/zcomp.c
diff options
context:
space:
mode:
authorSergey Senozhatsky <sergey.senozhatsky@gmail.com>2014-04-07 18:38:14 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2014-04-07 19:36:01 -0400
commitbeca3ec71fe5490ee9237dc42400f50402baf83e (patch)
treecac925d78139fe53a14d9c66939d07c472ca726b /drivers/block/zram/zcomp.c
parent9cc97529a180b369fcb7e5265771b6ba7e01f05b (diff)
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>
Diffstat (limited to 'drivers/block/zram/zcomp.c')
-rw-r--r--drivers/block/zram/zcomp.c124
1 files changed, 122 insertions, 2 deletions
diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c
index 72e8071f9d73..c06f75f54718 100644
--- a/drivers/block/zram/zcomp.c
+++ b/drivers/block/zram/zcomp.c
@@ -24,6 +24,21 @@ struct zcomp_strm_single {
24 struct zcomp_strm *zstrm; 24 struct zcomp_strm *zstrm;
25}; 25};
26 26
27/*
28 * multi zcomp_strm backend
29 */
30struct 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
27static struct zcomp_backend *find_backend(const char *compress) 42static struct zcomp_backend *find_backend(const char *compress)
28{ 43{
29 if (strncmp(compress, "lzo", 3) == 0) 44 if (strncmp(compress, "lzo", 3) == 0)
@@ -62,6 +77,107 @@ static struct zcomp_strm *zcomp_strm_alloc(struct zcomp *comp)
62 return zstrm; 77 return zstrm;
63} 78}
64 79
80/*
81 * get idle zcomp_strm or wait until other process release
82 * (zcomp_strm_release()) one for us
83 */
84static 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 */
122static 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
139static 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
153static 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
65static struct zcomp_strm *zcomp_strm_single_find(struct zcomp *comp) 181static struct zcomp_strm *zcomp_strm_single_find(struct zcomp *comp)
66{ 182{
67 struct zcomp_strm_single *zs = comp->stream; 183 struct zcomp_strm_single *zs = comp->stream;
@@ -139,7 +255,7 @@ void zcomp_destroy(struct zcomp *comp)
139 * if requested algorithm is not supported or in case 255 * if requested algorithm is not supported or in case
140 * of init error 256 * of init error
141 */ 257 */
142struct zcomp *zcomp_create(const char *compress) 258struct zcomp *zcomp_create(const char *compress, int max_strm)
143{ 259{
144 struct zcomp *comp; 260 struct zcomp *comp;
145 struct zcomp_backend *backend; 261 struct zcomp_backend *backend;
@@ -153,7 +269,11 @@ struct zcomp *zcomp_create(const char *compress)
153 return NULL; 269 return NULL;
154 270
155 comp->backend = backend; 271 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) {
157 kfree(comp); 277 kfree(comp);
158 return NULL; 278 return NULL;
159 } 279 }