xref: /linux/drivers/block/zram/zcomp.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 #include <linux/kernel.h>
4 #include <linux/string.h>
5 #include <linux/err.h>
6 #include <linux/slab.h>
7 #include <linux/wait.h>
8 #include <linux/sched.h>
9 #include <linux/cpuhotplug.h>
10 #include <linux/highmem.h>
11 #include <linux/scatterlist.h>
12 #include <linux/vmalloc.h>
13 #include <linux/sysfs.h>
14 
15 #include "zcomp.h"
16 
17 #include "backend_lzo.h"
18 #include "backend_lzorle.h"
19 #include "backend_lz4.h"
20 #include "backend_lz4hc.h"
21 #include "backend_zstd.h"
22 #include "backend_deflate.h"
23 #include "backend_842.h"
24 
25 static const struct zcomp_ops *backends[] = {
26 #if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZO)
27 	&backend_lzorle,
28 	&backend_lzo,
29 #endif
30 #if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZ4)
31 	&backend_lz4,
32 #endif
33 #if IS_ENABLED(CONFIG_ZRAM_BACKEND_LZ4HC)
34 	&backend_lz4hc,
35 #endif
36 #if IS_ENABLED(CONFIG_ZRAM_BACKEND_ZSTD)
37 	&backend_zstd,
38 #endif
39 #if IS_ENABLED(CONFIG_ZRAM_BACKEND_DEFLATE)
40 	&backend_deflate,
41 #endif
42 #if IS_ENABLED(CONFIG_ZRAM_BACKEND_842)
43 	&backend_842,
44 #endif
45 	NULL
46 };
47 
48 static void zcomp_strm_free(struct zcomp *comp, struct zcomp_strm *zstrm)
49 {
50 	comp->ops->destroy_ctx(&zstrm->ctx);
51 	vfree(zstrm->local_copy);
52 	vfree(zstrm->buffer);
53 	zstrm->buffer = NULL;
54 }
55 
56 static int zcomp_strm_init(struct zcomp *comp, struct zcomp_strm *zstrm)
57 {
58 	int ret;
59 
60 	ret = comp->ops->create_ctx(comp->params, &zstrm->ctx);
61 	if (ret)
62 		return ret;
63 
64 	zstrm->local_copy = vzalloc(PAGE_SIZE);
65 	/*
66 	 * allocate 2 pages. 1 for compressed data, plus 1 extra for the
67 	 * case when compressed size is larger than the original one
68 	 */
69 	zstrm->buffer = vzalloc(2 * PAGE_SIZE);
70 	if (!zstrm->buffer || !zstrm->local_copy) {
71 		zcomp_strm_free(comp, zstrm);
72 		return -ENOMEM;
73 	}
74 	return 0;
75 }
76 
77 static const struct zcomp_ops *lookup_backend_ops(const char *comp)
78 {
79 	int i = 0;
80 
81 	while (backends[i]) {
82 		if (sysfs_streq(comp, backends[i]->name))
83 			break;
84 		i++;
85 	}
86 	return backends[i];
87 }
88 
89 const char *zcomp_lookup_backend_name(const char *comp)
90 {
91 	const struct zcomp_ops *backend = lookup_backend_ops(comp);
92 
93 	if (backend)
94 		return backend->name;
95 
96 	return NULL;
97 }
98 
99 /* show available compressors */
100 ssize_t zcomp_available_show(const char *comp, char *buf, ssize_t at)
101 {
102 	int i;
103 
104 	for (i = 0; i < ARRAY_SIZE(backends) - 1; i++) {
105 		if (!strcmp(comp, backends[i]->name)) {
106 			at += sysfs_emit_at(buf, at, "[%s] ",
107 					    backends[i]->name);
108 		} else {
109 			at += sysfs_emit_at(buf, at, "%s ", backends[i]->name);
110 		}
111 	}
112 
113 	at += sysfs_emit_at(buf, at, "\n");
114 	return at;
115 }
116 
117 struct zcomp_strm *zcomp_stream_get(struct zcomp *comp)
118 {
119 	for (;;) {
120 		struct zcomp_strm *zstrm = raw_cpu_ptr(comp->stream);
121 
122 		/*
123 		 * Inspired by zswap
124 		 *
125 		 * stream is returned with ->mutex locked which prevents
126 		 * cpu_dead() from releasing this stream under us, however
127 		 * there is still a race window between raw_cpu_ptr() and
128 		 * mutex_lock(), during which we could have been migrated
129 		 * from a CPU that has already destroyed its stream.  If
130 		 * so then unlock and re-try on the current CPU.
131 		 */
132 		mutex_lock(&zstrm->lock);
133 		if (likely(zstrm->buffer))
134 			return zstrm;
135 		mutex_unlock(&zstrm->lock);
136 	}
137 }
138 
139 void zcomp_stream_put(struct zcomp_strm *zstrm)
140 {
141 	mutex_unlock(&zstrm->lock);
142 }
143 
144 int zcomp_compress(struct zcomp *comp, struct zcomp_strm *zstrm,
145 		   const void *src, unsigned int *dst_len)
146 {
147 	struct zcomp_req req = {
148 		.src = src,
149 		.dst = zstrm->buffer,
150 		.src_len = PAGE_SIZE,
151 		.dst_len = 2 * PAGE_SIZE,
152 	};
153 	int ret;
154 
155 	might_sleep();
156 	ret = comp->ops->compress(comp->params, &zstrm->ctx, &req);
157 	if (!ret)
158 		*dst_len = req.dst_len;
159 	return ret;
160 }
161 
162 int zcomp_decompress(struct zcomp *comp, struct zcomp_strm *zstrm,
163 		     struct scatterlist *sg, unsigned int src_len, void *dst)
164 {
165 	struct zcomp_req req = {
166 		.dst = dst,
167 		.src_len = src_len,
168 		.dst_len = PAGE_SIZE,
169 	};
170 	void *src = NULL;
171 	int ret;
172 
173 	might_sleep();
174 
175 	if (sg_is_last(sg)) {
176 		/* the object is contained within one page, read it in-place */
177 		src = kmap_local_page(sg_page(sg));
178 		req.src = src + sg->offset;
179 	} else {
180 		/* the object spans two pages, linearize it into local copy */
181 		sg_copy_to_buffer(sg, 2, zstrm->local_copy, src_len);
182 		req.src = zstrm->local_copy;
183 	}
184 
185 	ret = comp->ops->decompress(comp->params, &zstrm->ctx, &req);
186 	if (src)
187 		kunmap_local(src);
188 	return ret;
189 }
190 
191 int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
192 {
193 	struct zcomp *comp = hlist_entry(node, struct zcomp, node);
194 	struct zcomp_strm *zstrm = per_cpu_ptr(comp->stream, cpu);
195 	int ret;
196 
197 	ret = zcomp_strm_init(comp, zstrm);
198 	if (ret)
199 		pr_err("Can't allocate a compression stream\n");
200 	return ret;
201 }
202 
203 int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node)
204 {
205 	struct zcomp *comp = hlist_entry(node, struct zcomp, node);
206 	struct zcomp_strm *zstrm = per_cpu_ptr(comp->stream, cpu);
207 
208 	mutex_lock(&zstrm->lock);
209 	zcomp_strm_free(comp, zstrm);
210 	mutex_unlock(&zstrm->lock);
211 	return 0;
212 }
213 
214 static int zcomp_init(struct zcomp *comp, struct zcomp_params *params)
215 {
216 	int ret, cpu;
217 
218 	comp->stream = alloc_percpu(struct zcomp_strm);
219 	if (!comp->stream)
220 		return -ENOMEM;
221 
222 	comp->params = params;
223 	ret = comp->ops->setup_params(comp->params);
224 	if (ret)
225 		goto cleanup;
226 
227 	for_each_possible_cpu(cpu)
228 		mutex_init(&per_cpu_ptr(comp->stream, cpu)->lock);
229 
230 	ret = cpuhp_state_add_instance(CPUHP_ZCOMP_PREPARE, &comp->node);
231 	if (ret < 0)
232 		goto cleanup;
233 
234 	return 0;
235 
236 cleanup:
237 	comp->ops->release_params(comp->params);
238 	free_percpu(comp->stream);
239 	return ret;
240 }
241 
242 void zcomp_destroy(struct zcomp *comp)
243 {
244 	cpuhp_state_remove_instance(CPUHP_ZCOMP_PREPARE, &comp->node);
245 	comp->ops->release_params(comp->params);
246 	free_percpu(comp->stream);
247 	kfree(comp);
248 }
249 
250 struct zcomp *zcomp_create(const char *alg, struct zcomp_params *params)
251 {
252 	struct zcomp *comp;
253 	int error;
254 
255 	/*
256 	 * The backends array has a sentinel NULL value, so the minimum
257 	 * size is 1. In order to be valid the array, apart from the
258 	 * sentinel NULL element, should have at least one compression
259 	 * backend selected.
260 	 */
261 	BUILD_BUG_ON(ARRAY_SIZE(backends) <= 1);
262 
263 	comp = kzalloc_obj(struct zcomp);
264 	if (!comp)
265 		return ERR_PTR(-ENOMEM);
266 
267 	comp->ops = lookup_backend_ops(alg);
268 	if (!comp->ops) {
269 		kfree(comp);
270 		return ERR_PTR(-EINVAL);
271 	}
272 
273 	error = zcomp_init(comp, params);
274 	if (error) {
275 		kfree(comp);
276 		return ERR_PTR(error);
277 	}
278 	return comp;
279 }
280