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