xref: /linux/drivers/block/zram/zcomp.c (revision 376b1446153ca67e7028e6b9555d9b17477f568b)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2014 Sergey Senozhatsky.
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 #if IS_ENABLED(CONFIG_CRYPTO_LZO)
19 	"lzo",
20 	"lzo-rle",
21 #endif
22 #if IS_ENABLED(CONFIG_CRYPTO_LZ4)
23 	"lz4",
24 #endif
25 #if IS_ENABLED(CONFIG_CRYPTO_LZ4HC)
26 	"lz4hc",
27 #endif
28 #if IS_ENABLED(CONFIG_CRYPTO_842)
29 	"842",
30 #endif
31 #if IS_ENABLED(CONFIG_CRYPTO_ZSTD)
32 	"zstd",
33 #endif
34 };
35 
36 static void zcomp_strm_free(struct zcomp_strm *zstrm)
37 {
38 	if (!IS_ERR_OR_NULL(zstrm->tfm))
39 		crypto_free_comp(zstrm->tfm);
40 	free_pages((unsigned long)zstrm->buffer, 1);
41 	zstrm->tfm = NULL;
42 	zstrm->buffer = NULL;
43 }
44 
45 /*
46  * Initialize zcomp_strm structure with ->tfm initialized by backend, and
47  * ->buffer. Return a negative value on error.
48  */
49 static int zcomp_strm_init(struct zcomp_strm *zstrm, struct zcomp *comp)
50 {
51 	zstrm->tfm = crypto_alloc_comp(comp->name, 0, 0);
52 	/*
53 	 * allocate 2 pages. 1 for compressed data, plus 1 extra for the
54 	 * case when compressed size is larger than the original one
55 	 */
56 	zstrm->buffer = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
57 	if (IS_ERR_OR_NULL(zstrm->tfm) || !zstrm->buffer) {
58 		zcomp_strm_free(zstrm);
59 		return -ENOMEM;
60 	}
61 	return 0;
62 }
63 
64 bool zcomp_available_algorithm(const char *comp)
65 {
66 	/*
67 	 * Crypto does not ignore a trailing new line symbol,
68 	 * so make sure you don't supply a string containing
69 	 * one.
70 	 * This also means that we permit zcomp initialisation
71 	 * with any compressing algorithm known to crypto api.
72 	 */
73 	return crypto_has_comp(comp, 0, 0) == 1;
74 }
75 
76 /* show available compressors */
77 ssize_t zcomp_available_show(const char *comp, char *buf)
78 {
79 	bool known_algorithm = false;
80 	ssize_t sz = 0;
81 	int i;
82 
83 	for (i = 0; i < ARRAY_SIZE(backends); i++) {
84 		if (!strcmp(comp, backends[i])) {
85 			known_algorithm = true;
86 			sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2,
87 					"[%s] ", backends[i]);
88 		} else {
89 			sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2,
90 					"%s ", backends[i]);
91 		}
92 	}
93 
94 	/*
95 	 * Out-of-tree module known to crypto api or a missing
96 	 * entry in `backends'.
97 	 */
98 	if (!known_algorithm && crypto_has_comp(comp, 0, 0) == 1)
99 		sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2,
100 				"[%s] ", comp);
101 
102 	sz += scnprintf(buf + sz, PAGE_SIZE - sz, "\n");
103 	return sz;
104 }
105 
106 struct zcomp_strm *zcomp_stream_get(struct zcomp *comp)
107 {
108 	local_lock(&comp->stream->lock);
109 	return this_cpu_ptr(comp->stream);
110 }
111 
112 void zcomp_stream_put(struct zcomp *comp)
113 {
114 	local_unlock(&comp->stream->lock);
115 }
116 
117 int zcomp_compress(struct zcomp_strm *zstrm,
118 		const void *src, unsigned int *dst_len)
119 {
120 	/*
121 	 * Our dst memory (zstrm->buffer) is always `2 * PAGE_SIZE' sized
122 	 * because sometimes we can endup having a bigger compressed data
123 	 * due to various reasons: for example compression algorithms tend
124 	 * to add some padding to the compressed buffer. Speaking of padding,
125 	 * comp algorithm `842' pads the compressed length to multiple of 8
126 	 * and returns -ENOSP when the dst memory is not big enough, which
127 	 * is not something that ZRAM wants to see. We can handle the
128 	 * `compressed_size > PAGE_SIZE' case easily in ZRAM, but when we
129 	 * receive -ERRNO from the compressing backend we can't help it
130 	 * anymore. To make `842' happy we need to tell the exact size of
131 	 * the dst buffer, zram_drv will take care of the fact that
132 	 * compressed buffer is too big.
133 	 */
134 	*dst_len = PAGE_SIZE * 2;
135 
136 	return crypto_comp_compress(zstrm->tfm,
137 			src, PAGE_SIZE,
138 			zstrm->buffer, dst_len);
139 }
140 
141 int zcomp_decompress(struct zcomp_strm *zstrm,
142 		const void *src, unsigned int src_len, void *dst)
143 {
144 	unsigned int dst_len = PAGE_SIZE;
145 
146 	return crypto_comp_decompress(zstrm->tfm,
147 			src, src_len,
148 			dst, &dst_len);
149 }
150 
151 int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
152 {
153 	struct zcomp *comp = hlist_entry(node, struct zcomp, node);
154 	struct zcomp_strm *zstrm;
155 	int ret;
156 
157 	zstrm = per_cpu_ptr(comp->stream, cpu);
158 	local_lock_init(&zstrm->lock);
159 
160 	ret = zcomp_strm_init(zstrm, comp);
161 	if (ret)
162 		pr_err("Can't allocate a compression stream\n");
163 	return ret;
164 }
165 
166 int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node)
167 {
168 	struct zcomp *comp = hlist_entry(node, struct zcomp, node);
169 	struct zcomp_strm *zstrm;
170 
171 	zstrm = per_cpu_ptr(comp->stream, cpu);
172 	zcomp_strm_free(zstrm);
173 	return 0;
174 }
175 
176 static int zcomp_init(struct zcomp *comp)
177 {
178 	int ret;
179 
180 	comp->stream = alloc_percpu(struct zcomp_strm);
181 	if (!comp->stream)
182 		return -ENOMEM;
183 
184 	ret = cpuhp_state_add_instance(CPUHP_ZCOMP_PREPARE, &comp->node);
185 	if (ret < 0)
186 		goto cleanup;
187 	return 0;
188 
189 cleanup:
190 	free_percpu(comp->stream);
191 	return ret;
192 }
193 
194 void zcomp_destroy(struct zcomp *comp)
195 {
196 	cpuhp_state_remove_instance(CPUHP_ZCOMP_PREPARE, &comp->node);
197 	free_percpu(comp->stream);
198 	kfree(comp);
199 }
200 
201 /*
202  * search available compressors for requested algorithm.
203  * allocate new zcomp and initialize it. return compressing
204  * backend pointer or ERR_PTR if things went bad. ERR_PTR(-EINVAL)
205  * if requested algorithm is not supported, ERR_PTR(-ENOMEM) in
206  * case of allocation error, or any other error potentially
207  * returned by zcomp_init().
208  */
209 struct zcomp *zcomp_create(const char *alg)
210 {
211 	struct zcomp *comp;
212 	int error;
213 
214 	/*
215 	 * Crypto API will execute /sbin/modprobe if the compression module
216 	 * is not loaded yet. We must do it here, otherwise we are about to
217 	 * call /sbin/modprobe under CPU hot-plug lock.
218 	 */
219 	if (!zcomp_available_algorithm(alg))
220 		return ERR_PTR(-EINVAL);
221 
222 	comp = kzalloc(sizeof(struct zcomp), GFP_KERNEL);
223 	if (!comp)
224 		return ERR_PTR(-ENOMEM);
225 
226 	comp->name = alg;
227 	error = zcomp_init(comp);
228 	if (error) {
229 		kfree(comp);
230 		return ERR_PTR(error);
231 	}
232 	return comp;
233 }
234