2 * Copyright (C) 2014 Sergey Senozhatsky.
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.
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>
21 static const char * const backends[] = {
23 #if IS_ENABLED(CONFIG_CRYPTO_LZ4)
26 #if IS_ENABLED(CONFIG_CRYPTO_DEFLATE)
29 #if IS_ENABLED(CONFIG_CRYPTO_LZ4HC)
32 #if IS_ENABLED(CONFIG_CRYPTO_842)
38 static void zcomp_strm_free(struct zcomp_strm *zstrm)
40 if (!IS_ERR_OR_NULL(zstrm->tfm))
41 crypto_free_comp(zstrm->tfm);
42 free_pages((unsigned long)zstrm->buffer, 1);
47 * allocate new zcomp_strm structure with ->tfm initialized by
48 * backend, return NULL on error
50 static struct zcomp_strm *zcomp_strm_alloc(struct zcomp *comp)
52 struct zcomp_strm *zstrm = kmalloc(sizeof(*zstrm), GFP_KERNEL);
56 zstrm->tfm = crypto_alloc_comp(comp->name, 0, 0);
58 * allocate 2 pages. 1 for compressed data, plus 1 extra for the
59 * case when compressed size is larger than the original one
61 zstrm->buffer = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
62 if (IS_ERR_OR_NULL(zstrm->tfm) || !zstrm->buffer) {
63 zcomp_strm_free(zstrm);
69 bool zcomp_available_algorithm(const char *comp)
74 if (sysfs_streq(comp, backends[i]))
80 * Crypto does not ignore a trailing new line symbol,
81 * so make sure you don't supply a string containing
83 * This also means that we permit zcomp initialisation
84 * with any compressing algorithm known to crypto api.
86 return crypto_has_comp(comp, 0, 0) == 1;
89 /* show available compressors */
90 ssize_t zcomp_available_show(const char *comp, char *buf)
92 bool known_algorithm = false;
96 for (; backends[i]; i++) {
97 if (!strcmp(comp, backends[i])) {
98 known_algorithm = true;
99 sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2,
100 "[%s] ", backends[i]);
102 sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2,
108 * Out-of-tree module known to crypto api or a missing
109 * entry in `backends'.
111 if (!known_algorithm && crypto_has_comp(comp, 0, 0) == 1)
112 sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2,
115 sz += scnprintf(buf + sz, PAGE_SIZE - sz, "\n");
119 struct zcomp_strm *zcomp_stream_get(struct zcomp *comp)
121 return *get_cpu_ptr(comp->stream);
124 void zcomp_stream_put(struct zcomp *comp)
126 put_cpu_ptr(comp->stream);
129 int zcomp_compress(struct zcomp_strm *zstrm,
130 const void *src, unsigned int *dst_len)
133 * Our dst memory (zstrm->buffer) is always `2 * PAGE_SIZE' sized
134 * because sometimes we can endup having a bigger compressed data
135 * due to various reasons: for example compression algorithms tend
136 * to add some padding to the compressed buffer. Speaking of padding,
137 * comp algorithm `842' pads the compressed length to multiple of 8
138 * and returns -ENOSP when the dst memory is not big enough, which
139 * is not something that ZRAM wants to see. We can handle the
140 * `compressed_size > PAGE_SIZE' case easily in ZRAM, but when we
141 * receive -ERRNO from the compressing backend we can't help it
142 * anymore. To make `842' happy we need to tell the exact size of
143 * the dst buffer, zram_drv will take care of the fact that
144 * compressed buffer is too big.
146 *dst_len = PAGE_SIZE * 2;
148 return crypto_comp_compress(zstrm->tfm,
150 zstrm->buffer, dst_len);
153 int zcomp_decompress(struct zcomp_strm *zstrm,
154 const void *src, unsigned int src_len, void *dst)
156 unsigned int dst_len = PAGE_SIZE;
158 return crypto_comp_decompress(zstrm->tfm,
163 int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
165 struct zcomp *comp = hlist_entry(node, struct zcomp, node);
166 struct zcomp_strm *zstrm;
168 if (WARN_ON(*per_cpu_ptr(comp->stream, cpu)))
171 zstrm = zcomp_strm_alloc(comp);
172 if (IS_ERR_OR_NULL(zstrm)) {
173 pr_err("Can't allocate a compression stream\n");
176 *per_cpu_ptr(comp->stream, cpu) = zstrm;
180 int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node)
182 struct zcomp *comp = hlist_entry(node, struct zcomp, node);
183 struct zcomp_strm *zstrm;
185 zstrm = *per_cpu_ptr(comp->stream, cpu);
186 if (!IS_ERR_OR_NULL(zstrm))
187 zcomp_strm_free(zstrm);
188 *per_cpu_ptr(comp->stream, cpu) = NULL;
192 static int zcomp_init(struct zcomp *comp)
196 comp->stream = alloc_percpu(struct zcomp_strm *);
200 ret = cpuhp_state_add_instance(CPUHP_ZCOMP_PREPARE, &comp->node);
206 free_percpu(comp->stream);
210 void zcomp_destroy(struct zcomp *comp)
212 cpuhp_state_remove_instance(CPUHP_ZCOMP_PREPARE, &comp->node);
213 free_percpu(comp->stream);
218 * search available compressors for requested algorithm.
219 * allocate new zcomp and initialize it. return compressing
220 * backend pointer or ERR_PTR if things went bad. ERR_PTR(-EINVAL)
221 * if requested algorithm is not supported, ERR_PTR(-ENOMEM) in
222 * case of allocation error, or any other error potentially
223 * returned by zcomp_init().
225 struct zcomp *zcomp_create(const char *compress)
230 if (!zcomp_available_algorithm(compress))
231 return ERR_PTR(-EINVAL);
233 comp = kzalloc(sizeof(struct zcomp), GFP_KERNEL);
235 return ERR_PTR(-ENOMEM);
237 comp->name = compress;
238 error = zcomp_init(comp);
241 return ERR_PTR(error);