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>
19 #include "zcomp_lzo.h"
20 #ifdef CONFIG_ZRAM_LZ4_COMPRESS
21 #include "zcomp_lz4.h"
24 static struct zcomp_backend *backends[] = {
26 #ifdef CONFIG_ZRAM_LZ4_COMPRESS
32 static struct zcomp_backend *find_backend(const char *compress)
36 if (sysfs_streq(compress, backends[i]->name))
43 static void zcomp_strm_free(struct zcomp *comp, struct zcomp_strm *zstrm)
46 comp->backend->destroy(zstrm->private);
47 free_pages((unsigned long)zstrm->buffer, 1);
52 * allocate new zcomp_strm structure with ->private initialized by
53 * backend, return NULL on error
55 static struct zcomp_strm *zcomp_strm_alloc(struct zcomp *comp, gfp_t flags)
57 struct zcomp_strm *zstrm = kmalloc(sizeof(*zstrm), flags);
61 zstrm->private = comp->backend->create(flags);
63 * allocate 2 pages. 1 for compressed data, plus 1 extra for the
64 * case when compressed size is larger than the original one
66 zstrm->buffer = (void *)__get_free_pages(flags | __GFP_ZERO, 1);
67 if (!zstrm->private || !zstrm->buffer) {
68 zcomp_strm_free(comp, zstrm);
74 /* show available compressors */
75 ssize_t zcomp_available_show(const char *comp, char *buf)
81 if (!strcmp(comp, backends[i]->name))
82 sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2,
83 "[%s] ", backends[i]->name);
85 sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2,
86 "%s ", backends[i]->name);
89 sz += scnprintf(buf + sz, PAGE_SIZE - sz, "\n");
93 bool zcomp_available_algorithm(const char *comp)
95 return find_backend(comp) != NULL;
98 struct zcomp_strm *zcomp_strm_find(struct zcomp *comp)
100 return *get_cpu_ptr(comp->stream);
103 void zcomp_strm_release(struct zcomp *comp, struct zcomp_strm *zstrm)
105 put_cpu_ptr(comp->stream);
108 int zcomp_compress(struct zcomp *comp, struct zcomp_strm *zstrm,
109 const unsigned char *src, size_t *dst_len)
111 return comp->backend->compress(src, zstrm->buffer, dst_len,
115 int zcomp_decompress(struct zcomp *comp, const unsigned char *src,
116 size_t src_len, unsigned char *dst)
118 return comp->backend->decompress(src, src_len, dst);
121 static int __zcomp_cpu_notifier(struct zcomp *comp,
122 unsigned long action, unsigned long cpu)
124 struct zcomp_strm *zstrm;
128 if (WARN_ON(*per_cpu_ptr(comp->stream, cpu)))
130 zstrm = zcomp_strm_alloc(comp, GFP_KERNEL);
131 if (IS_ERR_OR_NULL(zstrm)) {
132 pr_err("Can't allocate a compression stream\n");
135 *per_cpu_ptr(comp->stream, cpu) = zstrm;
138 case CPU_UP_CANCELED:
139 zstrm = *per_cpu_ptr(comp->stream, cpu);
140 if (!IS_ERR_OR_NULL(zstrm))
141 zcomp_strm_free(comp, zstrm);
142 *per_cpu_ptr(comp->stream, cpu) = NULL;
150 static int zcomp_cpu_notifier(struct notifier_block *nb,
151 unsigned long action, void *pcpu)
153 unsigned long cpu = (unsigned long)pcpu;
154 struct zcomp *comp = container_of(nb, typeof(*comp), notifier);
156 return __zcomp_cpu_notifier(comp, action, cpu);
159 static int zcomp_init(struct zcomp *comp)
164 comp->notifier.notifier_call = zcomp_cpu_notifier;
166 comp->stream = alloc_percpu(struct zcomp_strm *);
170 cpu_notifier_register_begin();
171 for_each_online_cpu(cpu) {
172 ret = __zcomp_cpu_notifier(comp, CPU_UP_PREPARE, cpu);
173 if (ret == NOTIFY_BAD)
176 __register_cpu_notifier(&comp->notifier);
177 cpu_notifier_register_done();
181 for_each_online_cpu(cpu)
182 __zcomp_cpu_notifier(comp, CPU_UP_CANCELED, cpu);
183 cpu_notifier_register_done();
187 void zcomp_destroy(struct zcomp *comp)
191 cpu_notifier_register_begin();
192 for_each_online_cpu(cpu)
193 __zcomp_cpu_notifier(comp, CPU_UP_CANCELED, cpu);
194 __unregister_cpu_notifier(&comp->notifier);
195 cpu_notifier_register_done();
197 free_percpu(comp->stream);
202 * search available compressors for requested algorithm.
203 * allocate new zcomp and initialize it. return compressing
204 * backend pointer or ERR_PTR if things went bad. ERR_PTR(-EINVAL)
205 * if requested algorithm is not supported, ERR_PTR(-ENOMEM) in
206 * case of allocation error, or any other error potentially
207 * returned by zcomp_init().
209 struct zcomp *zcomp_create(const char *compress)
212 struct zcomp_backend *backend;
215 backend = find_backend(compress);
217 return ERR_PTR(-EINVAL);
219 comp = kzalloc(sizeof(struct zcomp), GFP_KERNEL);
221 return ERR_PTR(-ENOMEM);
223 comp->backend = backend;
224 error = zcomp_init(comp);
227 return ERR_PTR(error);