scompress.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Synchronous Compression operations
  4. *
  5. * Copyright 2015 LG Electronics Inc.
  6. * Copyright (c) 2016, Intel Corporation
  7. * Author: Giovanni Cabiddu <giovanni.cabiddu@intel.com>
  8. */
  9. #include <crypto/internal/acompress.h>
  10. #include <crypto/internal/scompress.h>
  11. #include <crypto/scatterwalk.h>
  12. #include <linux/cryptouser.h>
  13. #include <linux/err.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/scatterlist.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/slab.h>
  19. #include <linux/string.h>
  20. #include <linux/vmalloc.h>
  21. #include <net/netlink.h>
  22. #include "compress.h"
  23. struct scomp_scratch {
  24. spinlock_t lock;
  25. void *src;
  26. void *dst;
  27. };
  28. static DEFINE_PER_CPU(struct scomp_scratch, scomp_scratch) = {
  29. .lock = __SPIN_LOCK_UNLOCKED(scomp_scratch.lock),
  30. };
  31. static const struct crypto_type crypto_scomp_type;
  32. static int scomp_scratch_users;
  33. static DEFINE_MUTEX(scomp_lock);
  34. static int __maybe_unused crypto_scomp_report(
  35. struct sk_buff *skb, struct crypto_alg *alg)
  36. {
  37. struct crypto_report_comp rscomp;
  38. memset(&rscomp, 0, sizeof(rscomp));
  39. strscpy(rscomp.type, "scomp", sizeof(rscomp.type));
  40. return nla_put(skb, CRYPTOCFGA_REPORT_COMPRESS,
  41. sizeof(rscomp), &rscomp);
  42. }
  43. static void crypto_scomp_show(struct seq_file *m, struct crypto_alg *alg)
  44. __maybe_unused;
  45. static void crypto_scomp_show(struct seq_file *m, struct crypto_alg *alg)
  46. {
  47. seq_puts(m, "type : scomp\n");
  48. }
  49. static void crypto_scomp_free_scratches(void)
  50. {
  51. struct scomp_scratch *scratch;
  52. int i;
  53. for_each_possible_cpu(i) {
  54. scratch = per_cpu_ptr(&scomp_scratch, i);
  55. vfree(scratch->src);
  56. vfree(scratch->dst);
  57. scratch->src = NULL;
  58. scratch->dst = NULL;
  59. }
  60. }
  61. static int crypto_scomp_alloc_scratches(void)
  62. {
  63. struct scomp_scratch *scratch;
  64. int i;
  65. for_each_possible_cpu(i) {
  66. void *mem;
  67. scratch = per_cpu_ptr(&scomp_scratch, i);
  68. mem = vmalloc_node(SCOMP_SCRATCH_SIZE, cpu_to_node(i));
  69. if (!mem)
  70. goto error;
  71. scratch->src = mem;
  72. mem = vmalloc_node(SCOMP_SCRATCH_SIZE, cpu_to_node(i));
  73. if (!mem)
  74. goto error;
  75. scratch->dst = mem;
  76. }
  77. return 0;
  78. error:
  79. crypto_scomp_free_scratches();
  80. return -ENOMEM;
  81. }
  82. static int crypto_scomp_init_tfm(struct crypto_tfm *tfm)
  83. {
  84. int ret = 0;
  85. mutex_lock(&scomp_lock);
  86. if (!scomp_scratch_users++)
  87. ret = crypto_scomp_alloc_scratches();
  88. mutex_unlock(&scomp_lock);
  89. return ret;
  90. }
  91. static int scomp_acomp_comp_decomp(struct acomp_req *req, int dir)
  92. {
  93. struct crypto_acomp *tfm = crypto_acomp_reqtfm(req);
  94. void **tfm_ctx = acomp_tfm_ctx(tfm);
  95. struct crypto_scomp *scomp = *tfm_ctx;
  96. void **ctx = acomp_request_ctx(req);
  97. struct scomp_scratch *scratch;
  98. void *src, *dst;
  99. unsigned int dlen;
  100. int ret;
  101. if (!req->src || !req->slen || req->slen > SCOMP_SCRATCH_SIZE)
  102. return -EINVAL;
  103. if (req->dst && !req->dlen)
  104. return -EINVAL;
  105. if (!req->dlen || req->dlen > SCOMP_SCRATCH_SIZE)
  106. req->dlen = SCOMP_SCRATCH_SIZE;
  107. dlen = req->dlen;
  108. scratch = raw_cpu_ptr(&scomp_scratch);
  109. spin_lock(&scratch->lock);
  110. if (sg_nents(req->src) == 1 && !PageHighMem(sg_page(req->src))) {
  111. src = page_to_virt(sg_page(req->src)) + req->src->offset;
  112. } else {
  113. scatterwalk_map_and_copy(scratch->src, req->src, 0,
  114. req->slen, 0);
  115. src = scratch->src;
  116. }
  117. if (req->dst && sg_nents(req->dst) == 1 && !PageHighMem(sg_page(req->dst)))
  118. dst = page_to_virt(sg_page(req->dst)) + req->dst->offset;
  119. else
  120. dst = scratch->dst;
  121. if (dir)
  122. ret = crypto_scomp_compress(scomp, src, req->slen,
  123. dst, &req->dlen, *ctx);
  124. else
  125. ret = crypto_scomp_decompress(scomp, src, req->slen,
  126. dst, &req->dlen, *ctx);
  127. if (!ret) {
  128. if (!req->dst) {
  129. req->dst = sgl_alloc(req->dlen, GFP_ATOMIC, NULL);
  130. if (!req->dst) {
  131. ret = -ENOMEM;
  132. goto out;
  133. }
  134. } else if (req->dlen > dlen) {
  135. ret = -ENOSPC;
  136. goto out;
  137. }
  138. if (dst == scratch->dst) {
  139. scatterwalk_map_and_copy(scratch->dst, req->dst, 0,
  140. req->dlen, 1);
  141. } else {
  142. int nr_pages = DIV_ROUND_UP(req->dst->offset + req->dlen, PAGE_SIZE);
  143. int i;
  144. struct page *dst_page = sg_page(req->dst);
  145. for (i = 0; i < nr_pages; i++)
  146. flush_dcache_page(dst_page + i);
  147. }
  148. }
  149. out:
  150. spin_unlock(&scratch->lock);
  151. return ret;
  152. }
  153. static int scomp_acomp_compress(struct acomp_req *req)
  154. {
  155. return scomp_acomp_comp_decomp(req, 1);
  156. }
  157. static int scomp_acomp_decompress(struct acomp_req *req)
  158. {
  159. return scomp_acomp_comp_decomp(req, 0);
  160. }
  161. static void crypto_exit_scomp_ops_async(struct crypto_tfm *tfm)
  162. {
  163. struct crypto_scomp **ctx = crypto_tfm_ctx(tfm);
  164. crypto_free_scomp(*ctx);
  165. mutex_lock(&scomp_lock);
  166. if (!--scomp_scratch_users)
  167. crypto_scomp_free_scratches();
  168. mutex_unlock(&scomp_lock);
  169. }
  170. int crypto_init_scomp_ops_async(struct crypto_tfm *tfm)
  171. {
  172. struct crypto_alg *calg = tfm->__crt_alg;
  173. struct crypto_acomp *crt = __crypto_acomp_tfm(tfm);
  174. struct crypto_scomp **ctx = crypto_tfm_ctx(tfm);
  175. struct crypto_scomp *scomp;
  176. if (!crypto_mod_get(calg))
  177. return -EAGAIN;
  178. scomp = crypto_create_tfm(calg, &crypto_scomp_type);
  179. if (IS_ERR(scomp)) {
  180. crypto_mod_put(calg);
  181. return PTR_ERR(scomp);
  182. }
  183. *ctx = scomp;
  184. tfm->exit = crypto_exit_scomp_ops_async;
  185. crt->compress = scomp_acomp_compress;
  186. crt->decompress = scomp_acomp_decompress;
  187. crt->dst_free = sgl_free;
  188. crt->reqsize = sizeof(void *);
  189. return 0;
  190. }
  191. struct acomp_req *crypto_acomp_scomp_alloc_ctx(struct acomp_req *req)
  192. {
  193. struct crypto_acomp *acomp = crypto_acomp_reqtfm(req);
  194. struct crypto_tfm *tfm = crypto_acomp_tfm(acomp);
  195. struct crypto_scomp **tfm_ctx = crypto_tfm_ctx(tfm);
  196. struct crypto_scomp *scomp = *tfm_ctx;
  197. void *ctx;
  198. ctx = crypto_scomp_alloc_ctx(scomp);
  199. if (IS_ERR(ctx)) {
  200. kfree(req);
  201. return NULL;
  202. }
  203. *req->__ctx = ctx;
  204. return req;
  205. }
  206. void crypto_acomp_scomp_free_ctx(struct acomp_req *req)
  207. {
  208. struct crypto_acomp *acomp = crypto_acomp_reqtfm(req);
  209. struct crypto_tfm *tfm = crypto_acomp_tfm(acomp);
  210. struct crypto_scomp **tfm_ctx = crypto_tfm_ctx(tfm);
  211. struct crypto_scomp *scomp = *tfm_ctx;
  212. void *ctx = *req->__ctx;
  213. if (ctx)
  214. crypto_scomp_free_ctx(scomp, ctx);
  215. }
  216. static const struct crypto_type crypto_scomp_type = {
  217. .extsize = crypto_alg_extsize,
  218. .init_tfm = crypto_scomp_init_tfm,
  219. #ifdef CONFIG_PROC_FS
  220. .show = crypto_scomp_show,
  221. #endif
  222. #if IS_ENABLED(CONFIG_CRYPTO_USER)
  223. .report = crypto_scomp_report,
  224. #endif
  225. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  226. .maskset = CRYPTO_ALG_TYPE_MASK,
  227. .type = CRYPTO_ALG_TYPE_SCOMPRESS,
  228. .tfmsize = offsetof(struct crypto_scomp, base),
  229. };
  230. int crypto_register_scomp(struct scomp_alg *alg)
  231. {
  232. struct crypto_alg *base = &alg->calg.base;
  233. comp_prepare_alg(&alg->calg);
  234. base->cra_type = &crypto_scomp_type;
  235. base->cra_flags |= CRYPTO_ALG_TYPE_SCOMPRESS;
  236. return crypto_register_alg(base);
  237. }
  238. EXPORT_SYMBOL_GPL(crypto_register_scomp);
  239. void crypto_unregister_scomp(struct scomp_alg *alg)
  240. {
  241. crypto_unregister_alg(&alg->base);
  242. }
  243. EXPORT_SYMBOL_GPL(crypto_unregister_scomp);
  244. int crypto_register_scomps(struct scomp_alg *algs, int count)
  245. {
  246. int i, ret;
  247. for (i = 0; i < count; i++) {
  248. ret = crypto_register_scomp(&algs[i]);
  249. if (ret)
  250. goto err;
  251. }
  252. return 0;
  253. err:
  254. for (--i; i >= 0; --i)
  255. crypto_unregister_scomp(&algs[i]);
  256. return ret;
  257. }
  258. EXPORT_SYMBOL_GPL(crypto_register_scomps);
  259. void crypto_unregister_scomps(struct scomp_alg *algs, int count)
  260. {
  261. int i;
  262. for (i = count - 1; i >= 0; --i)
  263. crypto_unregister_scomp(&algs[i]);
  264. }
  265. EXPORT_SYMBOL_GPL(crypto_unregister_scomps);
  266. MODULE_LICENSE("GPL");
  267. MODULE_DESCRIPTION("Synchronous compression type");