1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2014 Sergey Senozhatsky.
6 #include <linux/kernel.h>
7 #include <linux/string.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 #include <linux/vmalloc.h>
18 static const char * const backends[] = {
19 #if IS_ENABLED(CONFIG_CRYPTO_LZO)
23 #if IS_ENABLED(CONFIG_CRYPTO_LZ4)
26 #if IS_ENABLED(CONFIG_CRYPTO_LZ4HC)
29 #if IS_ENABLED(CONFIG_CRYPTO_842)
32 #if IS_ENABLED(CONFIG_CRYPTO_ZSTD)
37 static void zcomp_strm_free(struct zcomp_strm *zstrm)
39 if (!IS_ERR_OR_NULL(zstrm->tfm))
40 crypto_free_comp(zstrm->tfm);
47 * Initialize zcomp_strm structure with ->tfm initialized by backend, and
48 * ->buffer. Return a negative value on error.
50 static int zcomp_strm_init(struct zcomp_strm *zstrm, struct zcomp *comp)
52 zstrm->tfm = crypto_alloc_comp(comp->name, 0, 0);
54 * allocate 2 pages. 1 for compressed data, plus 1 extra for the
55 * case when compressed size is larger than the original one
57 zstrm->buffer = vzalloc(2 * PAGE_SIZE);
58 if (IS_ERR_OR_NULL(zstrm->tfm) || !zstrm->buffer) {
59 zcomp_strm_free(zstrm);
65 bool zcomp_available_algorithm(const char *comp)
68 * Crypto does not ignore a trailing new line symbol,
69 * so make sure you don't supply a string containing
71 * This also means that we permit zcomp initialisation
72 * with any compressing algorithm known to crypto api.
74 return crypto_has_comp(comp, 0, 0) == 1;
77 /* show available compressors */
78 ssize_t zcomp_available_show(const char *comp, char *buf)
80 bool known_algorithm = false;
84 for (i = 0; i < ARRAY_SIZE(backends); i++) {
85 if (!strcmp(comp, backends[i])) {
86 known_algorithm = true;
87 sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2,
88 "[%s] ", backends[i]);
90 sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2,
96 * Out-of-tree module known to crypto api or a missing
97 * entry in `backends'.
99 if (!known_algorithm && crypto_has_comp(comp, 0, 0) == 1)
100 sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2,
103 sz += scnprintf(buf + sz, PAGE_SIZE - sz, "\n");
107 struct zcomp_strm *zcomp_stream_get(struct zcomp *comp)
109 local_lock(&comp->stream->lock);
110 return this_cpu_ptr(comp->stream);
113 void zcomp_stream_put(struct zcomp *comp)
115 local_unlock(&comp->stream->lock);
118 int zcomp_compress(struct zcomp_strm *zstrm,
119 const void *src, unsigned int *dst_len)
122 * Our dst memory (zstrm->buffer) is always `2 * PAGE_SIZE' sized
123 * because sometimes we can endup having a bigger compressed data
124 * due to various reasons: for example compression algorithms tend
125 * to add some padding to the compressed buffer. Speaking of padding,
126 * comp algorithm `842' pads the compressed length to multiple of 8
127 * and returns -ENOSP when the dst memory is not big enough, which
128 * is not something that ZRAM wants to see. We can handle the
129 * `compressed_size > PAGE_SIZE' case easily in ZRAM, but when we
130 * receive -ERRNO from the compressing backend we can't help it
131 * anymore. To make `842' happy we need to tell the exact size of
132 * the dst buffer, zram_drv will take care of the fact that
133 * compressed buffer is too big.
135 *dst_len = PAGE_SIZE * 2;
137 return crypto_comp_compress(zstrm->tfm,
139 zstrm->buffer, dst_len);
142 int zcomp_decompress(struct zcomp_strm *zstrm,
143 const void *src, unsigned int src_len, void *dst)
145 unsigned int dst_len = PAGE_SIZE;
147 return crypto_comp_decompress(zstrm->tfm,
152 int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
154 struct zcomp *comp = hlist_entry(node, struct zcomp, node);
155 struct zcomp_strm *zstrm;
158 zstrm = per_cpu_ptr(comp->stream, cpu);
159 local_lock_init(&zstrm->lock);
161 ret = zcomp_strm_init(zstrm, comp);
163 pr_err("Can't allocate a compression stream\n");
167 int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node)
169 struct zcomp *comp = hlist_entry(node, struct zcomp, node);
170 struct zcomp_strm *zstrm;
172 zstrm = per_cpu_ptr(comp->stream, cpu);
173 zcomp_strm_free(zstrm);
177 static int zcomp_init(struct zcomp *comp)
181 comp->stream = alloc_percpu(struct zcomp_strm);
185 ret = cpuhp_state_add_instance(CPUHP_ZCOMP_PREPARE, &comp->node);
191 free_percpu(comp->stream);
195 void zcomp_destroy(struct zcomp *comp)
197 cpuhp_state_remove_instance(CPUHP_ZCOMP_PREPARE, &comp->node);
198 free_percpu(comp->stream);
203 * search available compressors for requested algorithm.
204 * allocate new zcomp and initialize it. return compressing
205 * backend pointer or ERR_PTR if things went bad. ERR_PTR(-EINVAL)
206 * if requested algorithm is not supported, ERR_PTR(-ENOMEM) in
207 * case of allocation error, or any other error potentially
208 * returned by zcomp_init().
210 struct zcomp *zcomp_create(const char *alg)
216 * Crypto API will execute /sbin/modprobe if the compression module
217 * is not loaded yet. We must do it here, otherwise we are about to
218 * call /sbin/modprobe under CPU hot-plug lock.
220 if (!zcomp_available_algorithm(alg))
221 return ERR_PTR(-EINVAL);
223 comp = kzalloc(sizeof(struct zcomp), GFP_KERNEL);
225 return ERR_PTR(-ENOMEM);
228 error = zcomp_init(comp);
231 return ERR_PTR(error);