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