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 #ifdef CONFIG_ZRAM_LZ4_COMPRESS
29 static const char *find_backend(const char *compress
)
33 if (sysfs_streq(compress
, backends
[i
]))
40 static void zcomp_strm_free(struct zcomp_strm
*zstrm
)
42 if (!IS_ERR_OR_NULL(zstrm
->tfm
))
43 crypto_free_comp(zstrm
->tfm
);
44 free_pages((unsigned long)zstrm
->buffer
, 1);
49 * allocate new zcomp_strm structure with ->tfm initialized by
50 * backend, return NULL on error
52 static struct zcomp_strm
*zcomp_strm_alloc(struct zcomp
*comp
, gfp_t flags
)
54 struct zcomp_strm
*zstrm
= kmalloc(sizeof(*zstrm
), flags
);
58 zstrm
->tfm
= crypto_alloc_comp(comp
->name
, 0, 0);
60 * allocate 2 pages. 1 for compressed data, plus 1 extra for the
61 * case when compressed size is larger than the original one
63 zstrm
->buffer
= (void *)__get_free_pages(flags
| __GFP_ZERO
, 1);
64 if (IS_ERR_OR_NULL(zstrm
->tfm
) || !zstrm
->buffer
) {
65 zcomp_strm_free(zstrm
);
71 /* show available compressors */
72 ssize_t
zcomp_available_show(const char *comp
, char *buf
)
78 if (!strcmp(comp
, backends
[i
]))
79 sz
+= scnprintf(buf
+ sz
, PAGE_SIZE
- sz
- 2,
80 "[%s] ", backends
[i
]);
82 sz
+= scnprintf(buf
+ sz
, PAGE_SIZE
- sz
- 2,
86 sz
+= scnprintf(buf
+ sz
, PAGE_SIZE
- sz
, "\n");
90 bool zcomp_available_algorithm(const char *comp
)
92 return find_backend(comp
) != NULL
;
95 struct zcomp_strm
*zcomp_stream_get(struct zcomp
*comp
)
97 return *get_cpu_ptr(comp
->stream
);
100 void zcomp_stream_put(struct zcomp
*comp
)
102 put_cpu_ptr(comp
->stream
);
105 int zcomp_compress(struct zcomp_strm
*zstrm
,
106 const void *src
, unsigned int *dst_len
)
109 * Our dst memory (zstrm->buffer) is always `2 * PAGE_SIZE' sized
110 * because sometimes we can endup having a bigger compressed data
111 * due to various reasons: for example compression algorithms tend
112 * to add some padding to the compressed buffer. Speaking of padding,
113 * comp algorithm `842' pads the compressed length to multiple of 8
114 * and returns -ENOSP when the dst memory is not big enough, which
115 * is not something that ZRAM wants to see. We can handle the
116 * `compressed_size > PAGE_SIZE' case easily in ZRAM, but when we
117 * receive -ERRNO from the compressing backend we can't help it
118 * anymore. To make `842' happy we need to tell the exact size of
119 * the dst buffer, zram_drv will take care of the fact that
120 * compressed buffer is too big.
122 *dst_len
= PAGE_SIZE
* 2;
124 return crypto_comp_compress(zstrm
->tfm
,
126 zstrm
->buffer
, dst_len
);
129 int zcomp_decompress(struct zcomp_strm
*zstrm
,
130 const void *src
, unsigned int src_len
, void *dst
)
132 unsigned int dst_len
= PAGE_SIZE
;
134 return crypto_comp_decompress(zstrm
->tfm
,
139 static int __zcomp_cpu_notifier(struct zcomp
*comp
,
140 unsigned long action
, unsigned long cpu
)
142 struct zcomp_strm
*zstrm
;
146 if (WARN_ON(*per_cpu_ptr(comp
->stream
, cpu
)))
148 zstrm
= zcomp_strm_alloc(comp
, GFP_KERNEL
);
149 if (IS_ERR_OR_NULL(zstrm
)) {
150 pr_err("Can't allocate a compression stream\n");
153 *per_cpu_ptr(comp
->stream
, cpu
) = zstrm
;
156 case CPU_UP_CANCELED
:
157 zstrm
= *per_cpu_ptr(comp
->stream
, cpu
);
158 if (!IS_ERR_OR_NULL(zstrm
))
159 zcomp_strm_free(zstrm
);
160 *per_cpu_ptr(comp
->stream
, cpu
) = NULL
;
168 static int zcomp_cpu_notifier(struct notifier_block
*nb
,
169 unsigned long action
, void *pcpu
)
171 unsigned long cpu
= (unsigned long)pcpu
;
172 struct zcomp
*comp
= container_of(nb
, typeof(*comp
), notifier
);
174 return __zcomp_cpu_notifier(comp
, action
, cpu
);
177 static int zcomp_init(struct zcomp
*comp
)
182 comp
->notifier
.notifier_call
= zcomp_cpu_notifier
;
184 comp
->stream
= alloc_percpu(struct zcomp_strm
*);
188 cpu_notifier_register_begin();
189 for_each_online_cpu(cpu
) {
190 ret
= __zcomp_cpu_notifier(comp
, CPU_UP_PREPARE
, cpu
);
191 if (ret
== NOTIFY_BAD
)
194 __register_cpu_notifier(&comp
->notifier
);
195 cpu_notifier_register_done();
199 for_each_online_cpu(cpu
)
200 __zcomp_cpu_notifier(comp
, CPU_UP_CANCELED
, cpu
);
201 cpu_notifier_register_done();
205 void zcomp_destroy(struct zcomp
*comp
)
209 cpu_notifier_register_begin();
210 for_each_online_cpu(cpu
)
211 __zcomp_cpu_notifier(comp
, CPU_UP_CANCELED
, cpu
);
212 __unregister_cpu_notifier(&comp
->notifier
);
213 cpu_notifier_register_done();
215 free_percpu(comp
->stream
);
220 * search available compressors for requested algorithm.
221 * allocate new zcomp and initialize it. return compressing
222 * backend pointer or ERR_PTR if things went bad. ERR_PTR(-EINVAL)
223 * if requested algorithm is not supported, ERR_PTR(-ENOMEM) in
224 * case of allocation error, or any other error potentially
225 * returned by zcomp_init().
227 struct zcomp
*zcomp_create(const char *compress
)
233 backend
= find_backend(compress
);
235 return ERR_PTR(-EINVAL
);
237 comp
= kzalloc(sizeof(struct zcomp
), GFP_KERNEL
);
239 return ERR_PTR(-ENOMEM
);
241 comp
->name
= backend
;
242 error
= zcomp_init(comp
);
245 return ERR_PTR(error
);