This source file includes following definitions.
- zcomp_strm_free
- zcomp_strm_alloc
- zcomp_available_algorithm
- zcomp_available_show
- zcomp_stream_get
- zcomp_stream_put
- zcomp_compress
- zcomp_decompress
- zcomp_cpu_up_prepare
- zcomp_cpu_dead
- zcomp_init
- zcomp_destroy
- zcomp_create
1
2
3
4
5
6 #include <linux/kernel.h>
7 #include <linux/string.h>
8 #include <linux/err.h>
9 #include <linux/slab.h>
10 #include <linux/wait.h>
11 #include <linux/sched.h>
12 #include <linux/cpu.h>
13 #include <linux/crypto.h>
14
15 #include "zcomp.h"
16
17 static const char * const backends[] = {
18 "lzo",
19 "lzo-rle",
20 #if IS_ENABLED(CONFIG_CRYPTO_LZ4)
21 "lz4",
22 #endif
23 #if IS_ENABLED(CONFIG_CRYPTO_LZ4HC)
24 "lz4hc",
25 #endif
26 #if IS_ENABLED(CONFIG_CRYPTO_842)
27 "842",
28 #endif
29 #if IS_ENABLED(CONFIG_CRYPTO_ZSTD)
30 "zstd",
31 #endif
32 NULL
33 };
34
35 static void zcomp_strm_free(struct zcomp_strm *zstrm)
36 {
37 if (!IS_ERR_OR_NULL(zstrm->tfm))
38 crypto_free_comp(zstrm->tfm);
39 free_pages((unsigned long)zstrm->buffer, 1);
40 kfree(zstrm);
41 }
42
43
44
45
46
47 static struct zcomp_strm *zcomp_strm_alloc(struct zcomp *comp)
48 {
49 struct zcomp_strm *zstrm = kmalloc(sizeof(*zstrm), GFP_KERNEL);
50 if (!zstrm)
51 return NULL;
52
53 zstrm->tfm = crypto_alloc_comp(comp->name, 0, 0);
54
55
56
57
58 zstrm->buffer = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
59 if (IS_ERR_OR_NULL(zstrm->tfm) || !zstrm->buffer) {
60 zcomp_strm_free(zstrm);
61 zstrm = NULL;
62 }
63 return zstrm;
64 }
65
66 bool zcomp_available_algorithm(const char *comp)
67 {
68 int i;
69
70 i = __sysfs_match_string(backends, -1, comp);
71 if (i >= 0)
72 return true;
73
74
75
76
77
78
79
80
81 return crypto_has_comp(comp, 0, 0) == 1;
82 }
83
84
85 ssize_t zcomp_available_show(const char *comp, char *buf)
86 {
87 bool known_algorithm = false;
88 ssize_t sz = 0;
89 int i = 0;
90
91 for (; backends[i]; i++) {
92 if (!strcmp(comp, backends[i])) {
93 known_algorithm = true;
94 sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2,
95 "[%s] ", backends[i]);
96 } else {
97 sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2,
98 "%s ", backends[i]);
99 }
100 }
101
102
103
104
105
106 if (!known_algorithm && crypto_has_comp(comp, 0, 0) == 1)
107 sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2,
108 "[%s] ", comp);
109
110 sz += scnprintf(buf + sz, PAGE_SIZE - sz, "\n");
111 return sz;
112 }
113
114 struct zcomp_strm *zcomp_stream_get(struct zcomp *comp)
115 {
116 return *get_cpu_ptr(comp->stream);
117 }
118
119 void zcomp_stream_put(struct zcomp *comp)
120 {
121 put_cpu_ptr(comp->stream);
122 }
123
124 int zcomp_compress(struct zcomp_strm *zstrm,
125 const void *src, unsigned int *dst_len)
126 {
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141 *dst_len = PAGE_SIZE * 2;
142
143 return crypto_comp_compress(zstrm->tfm,
144 src, PAGE_SIZE,
145 zstrm->buffer, dst_len);
146 }
147
148 int zcomp_decompress(struct zcomp_strm *zstrm,
149 const void *src, unsigned int src_len, void *dst)
150 {
151 unsigned int dst_len = PAGE_SIZE;
152
153 return crypto_comp_decompress(zstrm->tfm,
154 src, src_len,
155 dst, &dst_len);
156 }
157
158 int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
159 {
160 struct zcomp *comp = hlist_entry(node, struct zcomp, node);
161 struct zcomp_strm *zstrm;
162
163 if (WARN_ON(*per_cpu_ptr(comp->stream, cpu)))
164 return 0;
165
166 zstrm = zcomp_strm_alloc(comp);
167 if (IS_ERR_OR_NULL(zstrm)) {
168 pr_err("Can't allocate a compression stream\n");
169 return -ENOMEM;
170 }
171 *per_cpu_ptr(comp->stream, cpu) = zstrm;
172 return 0;
173 }
174
175 int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node)
176 {
177 struct zcomp *comp = hlist_entry(node, struct zcomp, node);
178 struct zcomp_strm *zstrm;
179
180 zstrm = *per_cpu_ptr(comp->stream, cpu);
181 if (!IS_ERR_OR_NULL(zstrm))
182 zcomp_strm_free(zstrm);
183 *per_cpu_ptr(comp->stream, cpu) = NULL;
184 return 0;
185 }
186
187 static int zcomp_init(struct zcomp *comp)
188 {
189 int ret;
190
191 comp->stream = alloc_percpu(struct zcomp_strm *);
192 if (!comp->stream)
193 return -ENOMEM;
194
195 ret = cpuhp_state_add_instance(CPUHP_ZCOMP_PREPARE, &comp->node);
196 if (ret < 0)
197 goto cleanup;
198 return 0;
199
200 cleanup:
201 free_percpu(comp->stream);
202 return ret;
203 }
204
205 void zcomp_destroy(struct zcomp *comp)
206 {
207 cpuhp_state_remove_instance(CPUHP_ZCOMP_PREPARE, &comp->node);
208 free_percpu(comp->stream);
209 kfree(comp);
210 }
211
212
213
214
215
216
217
218
219
220 struct zcomp *zcomp_create(const char *compress)
221 {
222 struct zcomp *comp;
223 int error;
224
225 if (!zcomp_available_algorithm(compress))
226 return ERR_PTR(-EINVAL);
227
228 comp = kzalloc(sizeof(struct zcomp), GFP_KERNEL);
229 if (!comp)
230 return ERR_PTR(-ENOMEM);
231
232 comp->name = compress;
233 error = zcomp_init(comp);
234 if (error) {
235 kfree(comp);
236 return ERR_PTR(error);
237 }
238 return comp;
239 }