]> rtime.felk.cvut.cz Git - zynq/linux.git/blob - drivers/block/zram/zcomp.c
Apply preempt_rt patch-4.9-rt1.patch.xz
[zynq/linux.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/err.h>
13 #include <linux/slab.h>
14 #include <linux/wait.h>
15 #include <linux/sched.h>
16 #include <linux/cpu.h>
17 #include <linux/crypto.h>
18
19 #include "zcomp.h"
20
21 static const char * const backends[] = {
22         "lzo",
23 #if IS_ENABLED(CONFIG_CRYPTO_LZ4)
24         "lz4",
25 #endif
26 #if IS_ENABLED(CONFIG_CRYPTO_DEFLATE)
27         "deflate",
28 #endif
29 #if IS_ENABLED(CONFIG_CRYPTO_LZ4HC)
30         "lz4hc",
31 #endif
32 #if IS_ENABLED(CONFIG_CRYPTO_842)
33         "842",
34 #endif
35         NULL
36 };
37
38 static void zcomp_strm_free(struct zcomp_strm *zstrm)
39 {
40         if (!IS_ERR_OR_NULL(zstrm->tfm))
41                 crypto_free_comp(zstrm->tfm);
42         free_pages((unsigned long)zstrm->buffer, 1);
43         kfree(zstrm);
44 }
45
46 /*
47  * allocate new zcomp_strm structure with ->tfm initialized by
48  * backend, return NULL on error
49  */
50 static struct zcomp_strm *zcomp_strm_alloc(struct zcomp *comp)
51 {
52         struct zcomp_strm *zstrm = kmalloc(sizeof(*zstrm), GFP_KERNEL);
53         if (!zstrm)
54                 return NULL;
55
56         zstrm->tfm = crypto_alloc_comp(comp->name, 0, 0);
57         /*
58          * allocate 2 pages. 1 for compressed data, plus 1 extra for the
59          * case when compressed size is larger than the original one
60          */
61         zstrm->buffer = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
62         if (IS_ERR_OR_NULL(zstrm->tfm) || !zstrm->buffer) {
63                 zcomp_strm_free(zstrm);
64                 zstrm = NULL;
65         }
66         return zstrm;
67 }
68
69 bool zcomp_available_algorithm(const char *comp)
70 {
71         int i = 0;
72
73         while (backends[i]) {
74                 if (sysfs_streq(comp, backends[i]))
75                         return true;
76                 i++;
77         }
78
79         /*
80          * Crypto does not ignore a trailing new line symbol,
81          * so make sure you don't supply a string containing
82          * one.
83          * This also means that we permit zcomp initialisation
84          * with any compressing algorithm known to crypto api.
85          */
86         return crypto_has_comp(comp, 0, 0) == 1;
87 }
88
89 /* show available compressors */
90 ssize_t zcomp_available_show(const char *comp, char *buf)
91 {
92         bool known_algorithm = false;
93         ssize_t sz = 0;
94         int i = 0;
95
96         for (; backends[i]; i++) {
97                 if (!strcmp(comp, backends[i])) {
98                         known_algorithm = true;
99                         sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2,
100                                         "[%s] ", backends[i]);
101                 } else {
102                         sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2,
103                                         "%s ", backends[i]);
104                 }
105         }
106
107         /*
108          * Out-of-tree module known to crypto api or a missing
109          * entry in `backends'.
110          */
111         if (!known_algorithm && crypto_has_comp(comp, 0, 0) == 1)
112                 sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2,
113                                 "[%s] ", comp);
114
115         sz += scnprintf(buf + sz, PAGE_SIZE - sz, "\n");
116         return sz;
117 }
118
119 struct zcomp_strm *zcomp_stream_get(struct zcomp *comp)
120 {
121         struct zcomp_strm *zstrm;
122
123         zstrm = *this_cpu_ptr(comp->stream);
124         spin_lock(&zstrm->zcomp_lock);
125         return zstrm;
126 }
127
128 void zcomp_stream_put(struct zcomp *comp)
129 {
130         struct zcomp_strm *zstrm;
131
132         zstrm = *this_cpu_ptr(comp->stream);
133         spin_unlock(&zstrm->zcomp_lock);
134 }
135
136 int zcomp_compress(struct zcomp_strm *zstrm,
137                 const void *src, unsigned int *dst_len)
138 {
139         /*
140          * Our dst memory (zstrm->buffer) is always `2 * PAGE_SIZE' sized
141          * because sometimes we can endup having a bigger compressed data
142          * due to various reasons: for example compression algorithms tend
143          * to add some padding to the compressed buffer. Speaking of padding,
144          * comp algorithm `842' pads the compressed length to multiple of 8
145          * and returns -ENOSP when the dst memory is not big enough, which
146          * is not something that ZRAM wants to see. We can handle the
147          * `compressed_size > PAGE_SIZE' case easily in ZRAM, but when we
148          * receive -ERRNO from the compressing backend we can't help it
149          * anymore. To make `842' happy we need to tell the exact size of
150          * the dst buffer, zram_drv will take care of the fact that
151          * compressed buffer is too big.
152          */
153         *dst_len = PAGE_SIZE * 2;
154
155         return crypto_comp_compress(zstrm->tfm,
156                         src, PAGE_SIZE,
157                         zstrm->buffer, dst_len);
158 }
159
160 int zcomp_decompress(struct zcomp_strm *zstrm,
161                 const void *src, unsigned int src_len, void *dst)
162 {
163         unsigned int dst_len = PAGE_SIZE;
164
165         return crypto_comp_decompress(zstrm->tfm,
166                         src, src_len,
167                         dst, &dst_len);
168 }
169
170 static int __zcomp_cpu_notifier(struct zcomp *comp,
171                 unsigned long action, unsigned long cpu)
172 {
173         struct zcomp_strm *zstrm;
174
175         switch (action) {
176         case CPU_UP_PREPARE:
177                 if (WARN_ON(*per_cpu_ptr(comp->stream, cpu)))
178                         break;
179                 zstrm = zcomp_strm_alloc(comp);
180                 if (IS_ERR_OR_NULL(zstrm)) {
181                         pr_err("Can't allocate a compression stream\n");
182                         return NOTIFY_BAD;
183                 }
184                 spin_lock_init(&zstrm->zcomp_lock);
185                 *per_cpu_ptr(comp->stream, cpu) = zstrm;
186                 break;
187         case CPU_DEAD:
188         case CPU_UP_CANCELED:
189                 zstrm = *per_cpu_ptr(comp->stream, cpu);
190                 if (!IS_ERR_OR_NULL(zstrm))
191                         zcomp_strm_free(zstrm);
192                 *per_cpu_ptr(comp->stream, cpu) = NULL;
193                 break;
194         default:
195                 break;
196         }
197         return NOTIFY_OK;
198 }
199
200 static int zcomp_cpu_notifier(struct notifier_block *nb,
201                 unsigned long action, void *pcpu)
202 {
203         unsigned long cpu = (unsigned long)pcpu;
204         struct zcomp *comp = container_of(nb, typeof(*comp), notifier);
205
206         return __zcomp_cpu_notifier(comp, action, cpu);
207 }
208
209 static int zcomp_init(struct zcomp *comp)
210 {
211         unsigned long cpu;
212         int ret;
213
214         comp->notifier.notifier_call = zcomp_cpu_notifier;
215
216         comp->stream = alloc_percpu(struct zcomp_strm *);
217         if (!comp->stream)
218                 return -ENOMEM;
219
220         cpu_notifier_register_begin();
221         for_each_online_cpu(cpu) {
222                 ret = __zcomp_cpu_notifier(comp, CPU_UP_PREPARE, cpu);
223                 if (ret == NOTIFY_BAD)
224                         goto cleanup;
225         }
226         __register_cpu_notifier(&comp->notifier);
227         cpu_notifier_register_done();
228         return 0;
229
230 cleanup:
231         for_each_online_cpu(cpu)
232                 __zcomp_cpu_notifier(comp, CPU_UP_CANCELED, cpu);
233         cpu_notifier_register_done();
234         return -ENOMEM;
235 }
236
237 void zcomp_destroy(struct zcomp *comp)
238 {
239         unsigned long cpu;
240
241         cpu_notifier_register_begin();
242         for_each_online_cpu(cpu)
243                 __zcomp_cpu_notifier(comp, CPU_UP_CANCELED, cpu);
244         __unregister_cpu_notifier(&comp->notifier);
245         cpu_notifier_register_done();
246
247         free_percpu(comp->stream);
248         kfree(comp);
249 }
250
251 /*
252  * search available compressors for requested algorithm.
253  * allocate new zcomp and initialize it. return compressing
254  * backend pointer or ERR_PTR if things went bad. ERR_PTR(-EINVAL)
255  * if requested algorithm is not supported, ERR_PTR(-ENOMEM) in
256  * case of allocation error, or any other error potentially
257  * returned by zcomp_init().
258  */
259 struct zcomp *zcomp_create(const char *compress)
260 {
261         struct zcomp *comp;
262         int error;
263
264         if (!zcomp_available_algorithm(compress))
265                 return ERR_PTR(-EINVAL);
266
267         comp = kzalloc(sizeof(struct zcomp), GFP_KERNEL);
268         if (!comp)
269                 return ERR_PTR(-ENOMEM);
270
271         comp->name = compress;
272         error = zcomp_init(comp);
273         if (error) {
274                 kfree(comp);
275                 return ERR_PTR(error);
276         }
277         return comp;
278 }