blob: 5b8992beffec865634897c078877cd15614db0aa [file] [log] [blame]
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -07001/*
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>
Sergey Senozhatskyfcfa8d92014-04-07 15:38:20 -070012#include <linux/err.h>
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -070013#include <linux/slab.h>
14#include <linux/wait.h>
15#include <linux/sched.h>
Sergey Senozhatskyda9556a2016-05-20 16:59:51 -070016#include <linux/cpu.h>
Sergey Senozhatskyebaf9ab2016-07-26 15:22:45 -070017#include <linux/crypto.h>
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -070018
19#include "zcomp.h"
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -070020
Sergey Senozhatskyebaf9ab2016-07-26 15:22:45 -070021static const char * const backends[] = {
22 "lzo",
Sergey Senozhatskyce1ed9f2016-07-26 15:22:54 -070023#if IS_ENABLED(CONFIG_CRYPTO_LZ4)
Sergey Senozhatskyebaf9ab2016-07-26 15:22:45 -070024 "lz4",
Sergey Senozhatsky6e766682014-04-07 15:38:18 -070025#endif
Sergey Senozhatskyeb9f56d2016-07-26 15:22:56 -070026#if IS_ENABLED(CONFIG_CRYPTO_DEFLATE)
27 "deflate",
28#endif
29#if IS_ENABLED(CONFIG_CRYPTO_LZ4HC)
30 "lz4hc",
31#endif
32#if IS_ENABLED(CONFIG_CRYPTO_842)
33 "842",
34#endif
Sergey Senozhatskye46b8a02014-04-07 15:38:17 -070035 NULL
36};
37
Sergey Senozhatskyebaf9ab2016-07-26 15:22:45 -070038static void zcomp_strm_free(struct zcomp_strm *zstrm)
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -070039{
Sergey Senozhatskyebaf9ab2016-07-26 15:22:45 -070040 if (!IS_ERR_OR_NULL(zstrm->tfm))
41 crypto_free_comp(zstrm->tfm);
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -070042 free_pages((unsigned long)zstrm->buffer, 1);
43 kfree(zstrm);
44}
45
46/*
Sergey Senozhatskyebaf9ab2016-07-26 15:22:45 -070047 * allocate new zcomp_strm structure with ->tfm initialized by
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -070048 * backend, return NULL on error
49 */
Sergey Senozhatsky16d37722016-07-26 15:22:59 -070050static struct zcomp_strm *zcomp_strm_alloc(struct zcomp *comp)
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -070051{
Sergey Senozhatsky16d37722016-07-26 15:22:59 -070052 struct zcomp_strm *zstrm = kmalloc(sizeof(*zstrm), GFP_KERNEL);
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -070053 if (!zstrm)
54 return NULL;
55
Sergey Senozhatskyebaf9ab2016-07-26 15:22:45 -070056 zstrm->tfm = crypto_alloc_comp(comp->name, 0, 0);
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -070057 /*
58 * allocate 2 pages. 1 for compressed data, plus 1 extra for the
59 * case when compressed size is larger than the original one
60 */
Sergey Senozhatsky16d37722016-07-26 15:22:59 -070061 zstrm->buffer = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
Sergey Senozhatskyebaf9ab2016-07-26 15:22:45 -070062 if (IS_ERR_OR_NULL(zstrm->tfm) || !zstrm->buffer) {
63 zcomp_strm_free(zstrm);
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -070064 zstrm = NULL;
65 }
66 return zstrm;
67}
68
Sergey Senozhatsky415403b2016-07-26 15:22:48 -070069bool zcomp_available_algorithm(const char *comp)
Sergey Senozhatskye46b8a02014-04-07 15:38:17 -070070{
Andy Shevchenkoed8a5552017-07-10 15:48:34 -070071 int i;
Sergey Senozhatskye46b8a02014-04-07 15:38:17 -070072
Andy Shevchenkoed8a5552017-07-10 15:48:34 -070073 i = __sysfs_match_string(backends, -1, comp);
74 if (i >= 0)
75 return true;
Sergey Senozhatsky415403b2016-07-26 15:22:48 -070076
77 /*
78 * Crypto does not ignore a trailing new line symbol,
79 * so make sure you don't supply a string containing
80 * one.
81 * This also means that we permit zcomp initialisation
82 * with any compressing algorithm known to crypto api.
83 */
84 return crypto_has_comp(comp, 0, 0) == 1;
Sergey Senozhatskye46b8a02014-04-07 15:38:17 -070085}
86
Sergey Senozhatsky415403b2016-07-26 15:22:48 -070087/* show available compressors */
88ssize_t zcomp_available_show(const char *comp, char *buf)
Sergey Senozhatskyd93435c2015-06-25 15:00:32 -070089{
Sergey Senozhatsky415403b2016-07-26 15:22:48 -070090 bool known_algorithm = false;
91 ssize_t sz = 0;
92 int i = 0;
93
94 for (; backends[i]; i++) {
95 if (!strcmp(comp, backends[i])) {
96 known_algorithm = true;
97 sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2,
98 "[%s] ", backends[i]);
99 } else {
100 sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2,
101 "%s ", backends[i]);
102 }
103 }
104
105 /*
106 * Out-of-tree module known to crypto api or a missing
107 * entry in `backends'.
108 */
109 if (!known_algorithm && crypto_has_comp(comp, 0, 0) == 1)
110 sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2,
111 "[%s] ", comp);
112
113 sz += scnprintf(buf + sz, PAGE_SIZE - sz, "\n");
114 return sz;
Sergey Senozhatskyd93435c2015-06-25 15:00:32 -0700115}
116
Sergey Senozhatsky2aea8492016-07-26 15:22:42 -0700117struct zcomp_strm *zcomp_stream_get(struct zcomp *comp)
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -0700118{
Sergey Senozhatskyda9556a2016-05-20 16:59:51 -0700119 return *get_cpu_ptr(comp->stream);
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -0700120}
121
Sergey Senozhatsky2aea8492016-07-26 15:22:42 -0700122void zcomp_stream_put(struct zcomp *comp)
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -0700123{
Sergey Senozhatskyda9556a2016-05-20 16:59:51 -0700124 put_cpu_ptr(comp->stream);
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -0700125}
126
Sergey Senozhatskyebaf9ab2016-07-26 15:22:45 -0700127int zcomp_compress(struct zcomp_strm *zstrm,
128 const void *src, unsigned int *dst_len)
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -0700129{
Sergey Senozhatskyebaf9ab2016-07-26 15:22:45 -0700130 /*
131 * Our dst memory (zstrm->buffer) is always `2 * PAGE_SIZE' sized
132 * because sometimes we can endup having a bigger compressed data
133 * due to various reasons: for example compression algorithms tend
134 * to add some padding to the compressed buffer. Speaking of padding,
135 * comp algorithm `842' pads the compressed length to multiple of 8
136 * and returns -ENOSP when the dst memory is not big enough, which
137 * is not something that ZRAM wants to see. We can handle the
138 * `compressed_size > PAGE_SIZE' case easily in ZRAM, but when we
139 * receive -ERRNO from the compressing backend we can't help it
140 * anymore. To make `842' happy we need to tell the exact size of
141 * the dst buffer, zram_drv will take care of the fact that
142 * compressed buffer is too big.
143 */
144 *dst_len = PAGE_SIZE * 2;
145
146 return crypto_comp_compress(zstrm->tfm,
147 src, PAGE_SIZE,
148 zstrm->buffer, dst_len);
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -0700149}
150
Sergey Senozhatskyebaf9ab2016-07-26 15:22:45 -0700151int zcomp_decompress(struct zcomp_strm *zstrm,
152 const void *src, unsigned int src_len, void *dst)
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -0700153{
Sergey Senozhatskyebaf9ab2016-07-26 15:22:45 -0700154 unsigned int dst_len = PAGE_SIZE;
155
156 return crypto_comp_decompress(zstrm->tfm,
157 src, src_len,
158 dst, &dst_len);
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -0700159}
160
Anna-Maria Gleixner1dd6c832016-11-27 00:13:46 +0100161int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
Sergey Senozhatskyda9556a2016-05-20 16:59:51 -0700162{
Anna-Maria Gleixner1dd6c832016-11-27 00:13:46 +0100163 struct zcomp *comp = hlist_entry(node, struct zcomp, node);
Sergey Senozhatskyda9556a2016-05-20 16:59:51 -0700164 struct zcomp_strm *zstrm;
165
Anna-Maria Gleixner1dd6c832016-11-27 00:13:46 +0100166 if (WARN_ON(*per_cpu_ptr(comp->stream, cpu)))
167 return 0;
168
169 zstrm = zcomp_strm_alloc(comp);
170 if (IS_ERR_OR_NULL(zstrm)) {
171 pr_err("Can't allocate a compression stream\n");
172 return -ENOMEM;
Sergey Senozhatskyda9556a2016-05-20 16:59:51 -0700173 }
Anna-Maria Gleixner1dd6c832016-11-27 00:13:46 +0100174 *per_cpu_ptr(comp->stream, cpu) = zstrm;
175 return 0;
Sergey Senozhatskyda9556a2016-05-20 16:59:51 -0700176}
177
Anna-Maria Gleixner1dd6c832016-11-27 00:13:46 +0100178int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node)
Sergey Senozhatskyda9556a2016-05-20 16:59:51 -0700179{
Anna-Maria Gleixner1dd6c832016-11-27 00:13:46 +0100180 struct zcomp *comp = hlist_entry(node, struct zcomp, node);
181 struct zcomp_strm *zstrm;
Sergey Senozhatskyda9556a2016-05-20 16:59:51 -0700182
Anna-Maria Gleixner1dd6c832016-11-27 00:13:46 +0100183 zstrm = *per_cpu_ptr(comp->stream, cpu);
184 if (!IS_ERR_OR_NULL(zstrm))
185 zcomp_strm_free(zstrm);
186 *per_cpu_ptr(comp->stream, cpu) = NULL;
187 return 0;
Sergey Senozhatskyda9556a2016-05-20 16:59:51 -0700188}
189
190static int zcomp_init(struct zcomp *comp)
191{
Sergey Senozhatskyda9556a2016-05-20 16:59:51 -0700192 int ret;
193
Sergey Senozhatskyda9556a2016-05-20 16:59:51 -0700194 comp->stream = alloc_percpu(struct zcomp_strm *);
195 if (!comp->stream)
196 return -ENOMEM;
197
Anna-Maria Gleixner1dd6c832016-11-27 00:13:46 +0100198 ret = cpuhp_state_add_instance(CPUHP_ZCOMP_PREPARE, &comp->node);
199 if (ret < 0)
200 goto cleanup;
Sergey Senozhatskyda9556a2016-05-20 16:59:51 -0700201 return 0;
202
203cleanup:
Anna-Maria Gleixner1dd6c832016-11-27 00:13:46 +0100204 free_percpu(comp->stream);
205 return ret;
Sergey Senozhatskyda9556a2016-05-20 16:59:51 -0700206}
207
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -0700208void zcomp_destroy(struct zcomp *comp)
209{
Anna-Maria Gleixner1dd6c832016-11-27 00:13:46 +0100210 cpuhp_state_remove_instance(CPUHP_ZCOMP_PREPARE, &comp->node);
Sergey Senozhatskyda9556a2016-05-20 16:59:51 -0700211 free_percpu(comp->stream);
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -0700212 kfree(comp);
213}
214
215/*
216 * search available compressors for requested algorithm.
Sergey Senozhatskyfcfa8d92014-04-07 15:38:20 -0700217 * allocate new zcomp and initialize it. return compressing
218 * backend pointer or ERR_PTR if things went bad. ERR_PTR(-EINVAL)
219 * if requested algorithm is not supported, ERR_PTR(-ENOMEM) in
Luis Henriques3aaf14d2015-09-17 16:01:40 -0700220 * case of allocation error, or any other error potentially
Sergey Senozhatskyda9556a2016-05-20 16:59:51 -0700221 * returned by zcomp_init().
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -0700222 */
Sergey Senozhatskyda9556a2016-05-20 16:59:51 -0700223struct zcomp *zcomp_create(const char *compress)
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -0700224{
225 struct zcomp *comp;
Luis Henriques3aaf14d2015-09-17 16:01:40 -0700226 int error;
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -0700227
Sergey Senozhatsky415403b2016-07-26 15:22:48 -0700228 if (!zcomp_available_algorithm(compress))
Sergey Senozhatskyfcfa8d92014-04-07 15:38:20 -0700229 return ERR_PTR(-EINVAL);
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -0700230
231 comp = kzalloc(sizeof(struct zcomp), GFP_KERNEL);
232 if (!comp)
Sergey Senozhatskyfcfa8d92014-04-07 15:38:20 -0700233 return ERR_PTR(-ENOMEM);
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -0700234
Sergey Senozhatsky415403b2016-07-26 15:22:48 -0700235 comp->name = compress;
Sergey Senozhatskyda9556a2016-05-20 16:59:51 -0700236 error = zcomp_init(comp);
Luis Henriques3aaf14d2015-09-17 16:01:40 -0700237 if (error) {
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -0700238 kfree(comp);
Luis Henriques3aaf14d2015-09-17 16:01:40 -0700239 return ERR_PTR(error);
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -0700240 }
241 return comp;
242}