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