aad533a8bc55661471d6742b0aa68f10b00d9b41
[firefly-linux-kernel-4.4.55.git] / drivers / block / zram / zcomp.c
1 /*
2  * Copyright (C) 2014 Sergey Senozhatsky.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version
7  * 2 of the License, or (at your option) any later version.
8  */
9
10 #include <linux/kernel.h>
11 #include <linux/string.h>
12 #include <linux/slab.h>
13 #include <linux/wait.h>
14 #include <linux/sched.h>
15
16 #include "zcomp.h"
17 #include "zcomp_lzo.h"
18
19 /*
20  * single zcomp_strm backend
21  */
22 struct zcomp_strm_single {
23         struct mutex strm_lock;
24         struct zcomp_strm *zstrm;
25 };
26
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
42 static struct zcomp_backend *backends[] = {
43         &zcomp_lzo,
44         NULL
45 };
46
47 static struct zcomp_backend *find_backend(const char *compress)
48 {
49         int i = 0;
50         while (backends[i]) {
51                 if (sysfs_streq(compress, backends[i]->name))
52                         break;
53                 i++;
54         }
55         return backends[i];
56 }
57
58 static void zcomp_strm_free(struct zcomp *comp, struct zcomp_strm *zstrm)
59 {
60         if (zstrm->private)
61                 comp->backend->destroy(zstrm->private);
62         free_pages((unsigned long)zstrm->buffer, 1);
63         kfree(zstrm);
64 }
65
66 /*
67  * allocate new zcomp_strm structure with ->private initialized by
68  * backend, return NULL on error
69  */
70 static struct zcomp_strm *zcomp_strm_alloc(struct zcomp *comp)
71 {
72         struct zcomp_strm *zstrm = kmalloc(sizeof(*zstrm), GFP_KERNEL);
73         if (!zstrm)
74                 return NULL;
75
76         zstrm->private = comp->backend->create();
77         /*
78          * allocate 2 pages. 1 for compressed data, plus 1 extra for the
79          * case when compressed size is larger than the original one
80          */
81         zstrm->buffer = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
82         if (!zstrm->private || !zstrm->buffer) {
83                 zcomp_strm_free(comp, zstrm);
84                 zstrm = NULL;
85         }
86         return zstrm;
87 }
88
89 /*
90  * get idle zcomp_strm or wait until other process release
91  * (zcomp_strm_release()) one for us
92  */
93 static struct zcomp_strm *zcomp_strm_multi_find(struct zcomp *comp)
94 {
95         struct zcomp_strm_multi *zs = comp->stream;
96         struct zcomp_strm *zstrm;
97
98         while (1) {
99                 spin_lock(&zs->strm_lock);
100                 if (!list_empty(&zs->idle_strm)) {
101                         zstrm = list_entry(zs->idle_strm.next,
102                                         struct zcomp_strm, list);
103                         list_del(&zstrm->list);
104                         spin_unlock(&zs->strm_lock);
105                         return zstrm;
106                 }
107                 /* zstrm streams limit reached, wait for idle stream */
108                 if (zs->avail_strm >= zs->max_strm) {
109                         spin_unlock(&zs->strm_lock);
110                         wait_event(zs->strm_wait, !list_empty(&zs->idle_strm));
111                         continue;
112                 }
113                 /* allocate new zstrm stream */
114                 zs->avail_strm++;
115                 spin_unlock(&zs->strm_lock);
116
117                 zstrm = zcomp_strm_alloc(comp);
118                 if (!zstrm) {
119                         spin_lock(&zs->strm_lock);
120                         zs->avail_strm--;
121                         spin_unlock(&zs->strm_lock);
122                         wait_event(zs->strm_wait, !list_empty(&zs->idle_strm));
123                         continue;
124                 }
125                 break;
126         }
127         return zstrm;
128 }
129
130 /* add stream back to idle list and wake up waiter or free the stream */
131 static void zcomp_strm_multi_release(struct zcomp *comp, struct zcomp_strm *zstrm)
132 {
133         struct zcomp_strm_multi *zs = comp->stream;
134
135         spin_lock(&zs->strm_lock);
136         if (zs->avail_strm <= zs->max_strm) {
137                 list_add(&zstrm->list, &zs->idle_strm);
138                 spin_unlock(&zs->strm_lock);
139                 wake_up(&zs->strm_wait);
140                 return;
141         }
142
143         zs->avail_strm--;
144         spin_unlock(&zs->strm_lock);
145         zcomp_strm_free(comp, zstrm);
146 }
147
148 /* change max_strm limit */
149 static int zcomp_strm_multi_set_max_streams(struct zcomp *comp, int num_strm)
150 {
151         struct zcomp_strm_multi *zs = comp->stream;
152         struct zcomp_strm *zstrm;
153
154         spin_lock(&zs->strm_lock);
155         zs->max_strm = num_strm;
156         /*
157          * if user has lowered the limit and there are idle streams,
158          * immediately free as much streams (and memory) as we can.
159          */
160         while (zs->avail_strm > num_strm && !list_empty(&zs->idle_strm)) {
161                 zstrm = list_entry(zs->idle_strm.next,
162                                 struct zcomp_strm, list);
163                 list_del(&zstrm->list);
164                 zcomp_strm_free(comp, zstrm);
165                 zs->avail_strm--;
166         }
167         spin_unlock(&zs->strm_lock);
168         return 0;
169 }
170
171 static void zcomp_strm_multi_destroy(struct zcomp *comp)
172 {
173         struct zcomp_strm_multi *zs = comp->stream;
174         struct zcomp_strm *zstrm;
175
176         while (!list_empty(&zs->idle_strm)) {
177                 zstrm = list_entry(zs->idle_strm.next,
178                                 struct zcomp_strm, list);
179                 list_del(&zstrm->list);
180                 zcomp_strm_free(comp, zstrm);
181         }
182         kfree(zs);
183 }
184
185 static int zcomp_strm_multi_create(struct zcomp *comp, int max_strm)
186 {
187         struct zcomp_strm *zstrm;
188         struct zcomp_strm_multi *zs;
189
190         comp->destroy = zcomp_strm_multi_destroy;
191         comp->strm_find = zcomp_strm_multi_find;
192         comp->strm_release = zcomp_strm_multi_release;
193         comp->set_max_streams = zcomp_strm_multi_set_max_streams;
194         zs = kmalloc(sizeof(struct zcomp_strm_multi), GFP_KERNEL);
195         if (!zs)
196                 return -ENOMEM;
197
198         comp->stream = zs;
199         spin_lock_init(&zs->strm_lock);
200         INIT_LIST_HEAD(&zs->idle_strm);
201         init_waitqueue_head(&zs->strm_wait);
202         zs->max_strm = max_strm;
203         zs->avail_strm = 1;
204
205         zstrm = zcomp_strm_alloc(comp);
206         if (!zstrm) {
207                 kfree(zs);
208                 return -ENOMEM;
209         }
210         list_add(&zstrm->list, &zs->idle_strm);
211         return 0;
212 }
213
214 static struct zcomp_strm *zcomp_strm_single_find(struct zcomp *comp)
215 {
216         struct zcomp_strm_single *zs = comp->stream;
217         mutex_lock(&zs->strm_lock);
218         return zs->zstrm;
219 }
220
221 static void zcomp_strm_single_release(struct zcomp *comp,
222                 struct zcomp_strm *zstrm)
223 {
224         struct zcomp_strm_single *zs = comp->stream;
225         mutex_unlock(&zs->strm_lock);
226 }
227
228 static int zcomp_strm_single_set_max_streams(struct zcomp *comp, int num_strm)
229 {
230         /* zcomp_strm_single support only max_comp_streams == 1 */
231         return -ENOTSUPP;
232 }
233
234 static void zcomp_strm_single_destroy(struct zcomp *comp)
235 {
236         struct zcomp_strm_single *zs = comp->stream;
237         zcomp_strm_free(comp, zs->zstrm);
238         kfree(zs);
239 }
240
241 static int zcomp_strm_single_create(struct zcomp *comp)
242 {
243         struct zcomp_strm_single *zs;
244
245         comp->destroy = zcomp_strm_single_destroy;
246         comp->strm_find = zcomp_strm_single_find;
247         comp->strm_release = zcomp_strm_single_release;
248         comp->set_max_streams = zcomp_strm_single_set_max_streams;
249         zs = kmalloc(sizeof(struct zcomp_strm_single), GFP_KERNEL);
250         if (!zs)
251                 return -ENOMEM;
252
253         comp->stream = zs;
254         mutex_init(&zs->strm_lock);
255         zs->zstrm = zcomp_strm_alloc(comp);
256         if (!zs->zstrm) {
257                 kfree(zs);
258                 return -ENOMEM;
259         }
260         return 0;
261 }
262
263 /* show available compressors */
264 ssize_t zcomp_available_show(const char *comp, char *buf)
265 {
266         ssize_t sz = 0;
267         int i = 0;
268
269         while (backends[i]) {
270                 if (sysfs_streq(comp, backends[i]->name))
271                         sz += sprintf(buf + sz, "[%s] ", backends[i]->name);
272                 else
273                         sz += sprintf(buf + sz, "%s ", backends[i]->name);
274                 i++;
275         }
276         sz += sprintf(buf + sz, "\n");
277         return sz;
278 }
279
280 int zcomp_set_max_streams(struct zcomp *comp, int num_strm)
281 {
282         return comp->set_max_streams(comp, num_strm);
283 }
284
285 struct zcomp_strm *zcomp_strm_find(struct zcomp *comp)
286 {
287         return comp->strm_find(comp);
288 }
289
290 void zcomp_strm_release(struct zcomp *comp, struct zcomp_strm *zstrm)
291 {
292         comp->strm_release(comp, zstrm);
293 }
294
295 int zcomp_compress(struct zcomp *comp, struct zcomp_strm *zstrm,
296                 const unsigned char *src, size_t *dst_len)
297 {
298         return comp->backend->compress(src, zstrm->buffer, dst_len,
299                         zstrm->private);
300 }
301
302 int zcomp_decompress(struct zcomp *comp, const unsigned char *src,
303                 size_t src_len, unsigned char *dst)
304 {
305         return comp->backend->decompress(src, src_len, dst);
306 }
307
308 void zcomp_destroy(struct zcomp *comp)
309 {
310         comp->destroy(comp);
311         kfree(comp);
312 }
313
314 /*
315  * search available compressors for requested algorithm.
316  * allocate new zcomp and initialize it. return NULL
317  * if requested algorithm is not supported or in case
318  * of init error
319  */
320 struct zcomp *zcomp_create(const char *compress, int max_strm)
321 {
322         struct zcomp *comp;
323         struct zcomp_backend *backend;
324
325         backend = find_backend(compress);
326         if (!backend)
327                 return NULL;
328
329         comp = kzalloc(sizeof(struct zcomp), GFP_KERNEL);
330         if (!comp)
331                 return NULL;
332
333         comp->backend = backend;
334         if (max_strm > 1)
335                 zcomp_strm_multi_create(comp, max_strm);
336         else
337                 zcomp_strm_single_create(comp);
338         if (!comp->stream) {
339                 kfree(comp);
340                 return NULL;
341         }
342         return comp;
343 }