| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * 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 | 4 | */ |
|---|
| 9 | 5 | |
|---|
| 10 | 6 | #include <linux/kernel.h> |
|---|
| .. | .. |
|---|
| 20 | 16 | |
|---|
| 21 | 17 | static const char * const backends[] = { |
|---|
| 22 | 18 | "lzo", |
|---|
| 19 | + "lzo-rle", |
|---|
| 23 | 20 | #if IS_ENABLED(CONFIG_CRYPTO_LZ4) |
|---|
| 24 | 21 | "lz4", |
|---|
| 25 | 22 | #endif |
|---|
| .. | .. |
|---|
| 32 | 29 | #if IS_ENABLED(CONFIG_CRYPTO_ZSTD) |
|---|
| 33 | 30 | "zstd", |
|---|
| 34 | 31 | #endif |
|---|
| 35 | | - NULL |
|---|
| 36 | 32 | }; |
|---|
| 37 | 33 | |
|---|
| 38 | 34 | static void zcomp_strm_free(struct zcomp_strm *zstrm) |
|---|
| .. | .. |
|---|
| 40 | 36 | if (!IS_ERR_OR_NULL(zstrm->tfm)) |
|---|
| 41 | 37 | crypto_free_comp(zstrm->tfm); |
|---|
| 42 | 38 | free_pages((unsigned long)zstrm->buffer, 1); |
|---|
| 43 | | - kfree(zstrm); |
|---|
| 39 | + zstrm->tfm = NULL; |
|---|
| 40 | + zstrm->buffer = NULL; |
|---|
| 44 | 41 | } |
|---|
| 45 | 42 | |
|---|
| 46 | 43 | /* |
|---|
| 47 | | - * allocate new zcomp_strm structure with ->tfm initialized by |
|---|
| 48 | | - * backend, return NULL on error |
|---|
| 44 | + * Initialize zcomp_strm structure with ->tfm initialized by backend, and |
|---|
| 45 | + * ->buffer. Return a negative value on error. |
|---|
| 49 | 46 | */ |
|---|
| 50 | | -static struct zcomp_strm *zcomp_strm_alloc(struct zcomp *comp) |
|---|
| 47 | +static int zcomp_strm_init(struct zcomp_strm *zstrm, struct zcomp *comp) |
|---|
| 51 | 48 | { |
|---|
| 52 | | - struct zcomp_strm *zstrm = kmalloc(sizeof(*zstrm), GFP_KERNEL); |
|---|
| 53 | | - if (!zstrm) |
|---|
| 54 | | - return NULL; |
|---|
| 55 | | - |
|---|
| 56 | 49 | zstrm->tfm = crypto_alloc_comp(comp->name, 0, 0); |
|---|
| 57 | 50 | /* |
|---|
| 58 | 51 | * allocate 2 pages. 1 for compressed data, plus 1 extra for the |
|---|
| .. | .. |
|---|
| 61 | 54 | zstrm->buffer = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1); |
|---|
| 62 | 55 | if (IS_ERR_OR_NULL(zstrm->tfm) || !zstrm->buffer) { |
|---|
| 63 | 56 | zcomp_strm_free(zstrm); |
|---|
| 64 | | - zstrm = NULL; |
|---|
| 57 | + return -ENOMEM; |
|---|
| 65 | 58 | } |
|---|
| 66 | | - return zstrm; |
|---|
| 59 | + return 0; |
|---|
| 67 | 60 | } |
|---|
| 68 | 61 | |
|---|
| 69 | 62 | bool zcomp_available_algorithm(const char *comp) |
|---|
| 70 | 63 | { |
|---|
| 71 | | - int i; |
|---|
| 72 | | - |
|---|
| 73 | | - i = __sysfs_match_string(backends, -1, comp); |
|---|
| 74 | | - if (i >= 0) |
|---|
| 75 | | - return true; |
|---|
| 76 | | - |
|---|
| 77 | 64 | /* |
|---|
| 78 | 65 | * Crypto does not ignore a trailing new line symbol, |
|---|
| 79 | 66 | * so make sure you don't supply a string containing |
|---|
| .. | .. |
|---|
| 89 | 76 | { |
|---|
| 90 | 77 | bool known_algorithm = false; |
|---|
| 91 | 78 | ssize_t sz = 0; |
|---|
| 92 | | - int i = 0; |
|---|
| 79 | + int i; |
|---|
| 93 | 80 | |
|---|
| 94 | | - for (; backends[i]; i++) { |
|---|
| 81 | + for (i = 0; i < ARRAY_SIZE(backends); i++) { |
|---|
| 95 | 82 | if (!strcmp(comp, backends[i])) { |
|---|
| 96 | 83 | known_algorithm = true; |
|---|
| 97 | 84 | sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2, |
|---|
| .. | .. |
|---|
| 116 | 103 | |
|---|
| 117 | 104 | struct zcomp_strm *zcomp_stream_get(struct zcomp *comp) |
|---|
| 118 | 105 | { |
|---|
| 119 | | - return *get_cpu_ptr(comp->stream); |
|---|
| 106 | + local_lock(&comp->stream->lock); |
|---|
| 107 | + return this_cpu_ptr(comp->stream); |
|---|
| 120 | 108 | } |
|---|
| 121 | 109 | |
|---|
| 122 | 110 | void zcomp_stream_put(struct zcomp *comp) |
|---|
| 123 | 111 | { |
|---|
| 124 | | - put_cpu_ptr(comp->stream); |
|---|
| 112 | + local_unlock(&comp->stream->lock); |
|---|
| 125 | 113 | } |
|---|
| 126 | 114 | |
|---|
| 127 | 115 | int zcomp_compress(struct zcomp_strm *zstrm, |
|---|
| .. | .. |
|---|
| 162 | 150 | { |
|---|
| 163 | 151 | struct zcomp *comp = hlist_entry(node, struct zcomp, node); |
|---|
| 164 | 152 | struct zcomp_strm *zstrm; |
|---|
| 153 | + int ret; |
|---|
| 165 | 154 | |
|---|
| 166 | | - if (WARN_ON(*per_cpu_ptr(comp->stream, cpu))) |
|---|
| 167 | | - return 0; |
|---|
| 155 | + zstrm = per_cpu_ptr(comp->stream, cpu); |
|---|
| 156 | + local_lock_init(&zstrm->lock); |
|---|
| 168 | 157 | |
|---|
| 169 | | - zstrm = zcomp_strm_alloc(comp); |
|---|
| 170 | | - if (IS_ERR_OR_NULL(zstrm)) { |
|---|
| 158 | + ret = zcomp_strm_init(zstrm, comp); |
|---|
| 159 | + if (ret) |
|---|
| 171 | 160 | pr_err("Can't allocate a compression stream\n"); |
|---|
| 172 | | - return -ENOMEM; |
|---|
| 173 | | - } |
|---|
| 174 | | - *per_cpu_ptr(comp->stream, cpu) = zstrm; |
|---|
| 175 | | - return 0; |
|---|
| 161 | + return ret; |
|---|
| 176 | 162 | } |
|---|
| 177 | 163 | |
|---|
| 178 | 164 | int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node) |
|---|
| .. | .. |
|---|
| 180 | 166 | struct zcomp *comp = hlist_entry(node, struct zcomp, node); |
|---|
| 181 | 167 | struct zcomp_strm *zstrm; |
|---|
| 182 | 168 | |
|---|
| 183 | | - 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; |
|---|
| 169 | + zstrm = per_cpu_ptr(comp->stream, cpu); |
|---|
| 170 | + zcomp_strm_free(zstrm); |
|---|
| 187 | 171 | return 0; |
|---|
| 188 | 172 | } |
|---|
| 189 | 173 | |
|---|
| .. | .. |
|---|
| 191 | 175 | { |
|---|
| 192 | 176 | int ret; |
|---|
| 193 | 177 | |
|---|
| 194 | | - comp->stream = alloc_percpu(struct zcomp_strm *); |
|---|
| 178 | + comp->stream = alloc_percpu(struct zcomp_strm); |
|---|
| 195 | 179 | if (!comp->stream) |
|---|
| 196 | 180 | return -ENOMEM; |
|---|
| 197 | 181 | |
|---|
| .. | .. |
|---|
| 225 | 209 | struct zcomp *comp; |
|---|
| 226 | 210 | int error; |
|---|
| 227 | 211 | |
|---|
| 212 | + /* |
|---|
| 213 | + * Crypto API will execute /sbin/modprobe if the compression module |
|---|
| 214 | + * is not loaded yet. We must do it here, otherwise we are about to |
|---|
| 215 | + * call /sbin/modprobe under CPU hot-plug lock. |
|---|
| 216 | + */ |
|---|
| 228 | 217 | if (!zcomp_available_algorithm(compress)) |
|---|
| 229 | 218 | return ERR_PTR(-EINVAL); |
|---|
| 230 | 219 | |
|---|