1
2
3
4
5
6
7
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
21static 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
38static 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
48
49
50static 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
59
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
69bool zcomp_available_algorithm(const char *comp)
70{
71 int i;
72
73 i = __sysfs_match_string(backends, -1, comp);
74 if (i >= 0)
75 return true;
76
77
78
79
80
81
82
83
84 return crypto_has_comp(comp, 0, 0) == 1;
85}
86
87
88ssize_t zcomp_available_show(const char *comp, char *buf)
89{
90 bool known_algorithm = false;
91 ssize_t sz = 0;
92 int i = 0;
93
94 for (; backends[i]; i++) {
95 if (!strcmp(comp, backends[i])) {
96 known_algorithm = true;
97 sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2,
98 "[%s] ", backends[i]);
99 } else {
100 sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2,
101 "%s ", backends[i]);
102 }
103 }
104
105
106
107
108
109 if (!known_algorithm && crypto_has_comp(comp, 0, 0) == 1)
110 sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2,
111 "[%s] ", comp);
112
113 sz += scnprintf(buf + sz, PAGE_SIZE - sz, "\n");
114 return sz;
115}
116
117struct zcomp_strm *zcomp_stream_get(struct zcomp *comp)
118{
119 return *get_cpu_ptr(comp->stream);
120}
121
122void zcomp_stream_put(struct zcomp *comp)
123{
124 put_cpu_ptr(comp->stream);
125}
126
127int zcomp_compress(struct zcomp_strm *zstrm,
128 const void *src, unsigned int *dst_len)
129{
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144 *dst_len = PAGE_SIZE * 2;
145
146 return crypto_comp_compress(zstrm->tfm,
147 src, PAGE_SIZE,
148 zstrm->buffer, dst_len);
149}
150
151int zcomp_decompress(struct zcomp_strm *zstrm,
152 const void *src, unsigned int src_len, void *dst)
153{
154 unsigned int dst_len = PAGE_SIZE;
155
156 return crypto_comp_decompress(zstrm->tfm,
157 src, src_len,
158 dst, &dst_len);
159}
160
161int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
162{
163 struct zcomp *comp = hlist_entry(node, struct zcomp, node);
164 struct zcomp_strm *zstrm;
165
166 if (WARN_ON(*per_cpu_ptr(comp->stream, cpu)))
167 return 0;
168
169 zstrm = zcomp_strm_alloc(comp);
170 if (IS_ERR_OR_NULL(zstrm)) {
171 pr_err("Can't allocate a compression stream\n");
172 return -ENOMEM;
173 }
174 *per_cpu_ptr(comp->stream, cpu) = zstrm;
175 return 0;
176}
177
178int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node)
179{
180 struct zcomp *comp = hlist_entry(node, struct zcomp, node);
181 struct zcomp_strm *zstrm;
182
183 zstrm = *per_cpu_ptr(comp->stream, cpu);
184 if (!IS_ERR_OR_NULL(zstrm))
185 zcomp_strm_free(zstrm);
186 *per_cpu_ptr(comp->stream, cpu) = NULL;
187 return 0;
188}
189
190static int zcomp_init(struct zcomp *comp)
191{
192 int ret;
193
194 comp->stream = alloc_percpu(struct zcomp_strm *);
195 if (!comp->stream)
196 return -ENOMEM;
197
198 ret = cpuhp_state_add_instance(CPUHP_ZCOMP_PREPARE, &comp->node);
199 if (ret < 0)
200 goto cleanup;
201 return 0;
202
203cleanup:
204 free_percpu(comp->stream);
205 return ret;
206}
207
208void zcomp_destroy(struct zcomp *comp)
209{
210 cpuhp_state_remove_instance(CPUHP_ZCOMP_PREPARE, &comp->node);
211 free_percpu(comp->stream);
212 kfree(comp);
213}
214
215
216
217
218
219
220
221
222
223struct zcomp *zcomp_create(const char *compress)
224{
225 struct zcomp *comp;
226 int error;
227
228 if (!zcomp_available_algorithm(compress))
229 return ERR_PTR(-EINVAL);
230
231 comp = kzalloc(sizeof(struct zcomp), GFP_KERNEL);
232 if (!comp)
233 return ERR_PTR(-ENOMEM);
234
235 comp->name = compress;
236 error = zcomp_init(comp);
237 if (error) {
238 kfree(comp);
239 return ERR_PTR(error);
240 }
241 return comp;
242}
243