scompress.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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/scompress.h>
  10. #include <crypto/scatterwalk.h>
  11. #include <linux/cpumask.h>
  12. #include <linux/cryptouser.h>
  13. #include <linux/err.h>
  14. #include <linux/highmem.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/overflow.h>
  18. #include <linux/scatterlist.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/slab.h>
  21. #include <linux/string.h>
  22. #include <linux/workqueue.h>
  23. #include <net/netlink.h>
  24. #include "compress.h"
  25. struct scomp_scratch {
  26. spinlock_t lock;
  27. union {
  28. void *src __guarded_by(&lock);
  29. unsigned long saddr __guarded_by(&lock);
  30. };
  31. };
  32. static DEFINE_PER_CPU(struct scomp_scratch, scomp_scratch) = {
  33. .lock = __SPIN_LOCK_UNLOCKED(scomp_scratch.lock),
  34. };
  35. static const struct crypto_type crypto_scomp_type;
  36. static DEFINE_MUTEX(scomp_lock);
  37. static int scomp_scratch_users __guarded_by(&scomp_lock);
  38. static cpumask_t scomp_scratch_want;
  39. static void scomp_scratch_workfn(struct work_struct *work);
  40. static DECLARE_WORK(scomp_scratch_work, scomp_scratch_workfn);
  41. static int __maybe_unused crypto_scomp_report(
  42. struct sk_buff *skb, struct crypto_alg *alg)
  43. {
  44. struct crypto_report_comp rscomp;
  45. memset(&rscomp, 0, sizeof(rscomp));
  46. strscpy(rscomp.type, "scomp", sizeof(rscomp.type));
  47. return nla_put(skb, CRYPTOCFGA_REPORT_COMPRESS,
  48. sizeof(rscomp), &rscomp);
  49. }
  50. static void __maybe_unused crypto_scomp_show(struct seq_file *m,
  51. struct crypto_alg *alg)
  52. {
  53. seq_puts(m, "type : scomp\n");
  54. }
  55. static void crypto_scomp_free_scratches(void)
  56. __context_unsafe(/* frees @scratch */)
  57. {
  58. struct scomp_scratch *scratch;
  59. int i;
  60. for_each_possible_cpu(i) {
  61. scratch = per_cpu_ptr(&scomp_scratch, i);
  62. free_page(scratch->saddr);
  63. scratch->src = NULL;
  64. }
  65. }
  66. static int scomp_alloc_scratch(struct scomp_scratch *scratch, int cpu)
  67. {
  68. int node = cpu_to_node(cpu);
  69. struct page *page;
  70. page = alloc_pages_node(node, GFP_KERNEL, 0);
  71. if (!page)
  72. return -ENOMEM;
  73. spin_lock_bh(&scratch->lock);
  74. scratch->src = page_address(page);
  75. spin_unlock_bh(&scratch->lock);
  76. return 0;
  77. }
  78. static void scomp_scratch_workfn(struct work_struct *work)
  79. {
  80. int cpu;
  81. for_each_cpu(cpu, &scomp_scratch_want) {
  82. struct scomp_scratch *scratch;
  83. scratch = per_cpu_ptr(&scomp_scratch, cpu);
  84. if (context_unsafe(scratch->src))
  85. continue;
  86. if (scomp_alloc_scratch(scratch, cpu))
  87. break;
  88. cpumask_clear_cpu(cpu, &scomp_scratch_want);
  89. }
  90. }
  91. static int crypto_scomp_alloc_scratches(void)
  92. __context_unsafe(/* allocates @scratch */)
  93. {
  94. unsigned int i = cpumask_first(cpu_possible_mask);
  95. struct scomp_scratch *scratch;
  96. scratch = per_cpu_ptr(&scomp_scratch, i);
  97. return scomp_alloc_scratch(scratch, i);
  98. }
  99. static int crypto_scomp_init_tfm(struct crypto_tfm *tfm)
  100. {
  101. struct scomp_alg *alg = crypto_scomp_alg(__crypto_scomp_tfm(tfm));
  102. int ret = 0;
  103. mutex_lock(&scomp_lock);
  104. ret = crypto_acomp_alloc_streams(&alg->streams);
  105. if (ret)
  106. goto unlock;
  107. if (!scomp_scratch_users++) {
  108. ret = crypto_scomp_alloc_scratches();
  109. if (ret)
  110. scomp_scratch_users--;
  111. }
  112. unlock:
  113. mutex_unlock(&scomp_lock);
  114. return ret;
  115. }
  116. #define scomp_lock_scratch(...) __acquire_ret(_scomp_lock_scratch(__VA_ARGS__), &__ret->lock)
  117. static struct scomp_scratch *_scomp_lock_scratch(void) __acquires_ret
  118. {
  119. int cpu = raw_smp_processor_id();
  120. struct scomp_scratch *scratch;
  121. scratch = per_cpu_ptr(&scomp_scratch, cpu);
  122. spin_lock(&scratch->lock);
  123. if (likely(scratch->src))
  124. return scratch;
  125. spin_unlock(&scratch->lock);
  126. cpumask_set_cpu(cpu, &scomp_scratch_want);
  127. schedule_work(&scomp_scratch_work);
  128. scratch = per_cpu_ptr(&scomp_scratch, cpumask_first(cpu_possible_mask));
  129. spin_lock(&scratch->lock);
  130. return scratch;
  131. }
  132. static inline void scomp_unlock_scratch(struct scomp_scratch *scratch)
  133. __releases(&scratch->lock)
  134. {
  135. spin_unlock(&scratch->lock);
  136. }
  137. static int scomp_acomp_comp_decomp(struct acomp_req *req, int dir)
  138. {
  139. struct crypto_acomp *tfm = crypto_acomp_reqtfm(req);
  140. struct crypto_scomp **tfm_ctx = acomp_tfm_ctx(tfm);
  141. bool src_isvirt = acomp_request_src_isvirt(req);
  142. bool dst_isvirt = acomp_request_dst_isvirt(req);
  143. struct crypto_scomp *scomp = *tfm_ctx;
  144. unsigned int slen = req->slen;
  145. unsigned int dlen = req->dlen;
  146. struct page *spage, *dpage;
  147. unsigned int n;
  148. const u8 *src;
  149. size_t soff;
  150. size_t doff;
  151. u8 *dst;
  152. int ret;
  153. if (!req->src || !slen)
  154. return -EINVAL;
  155. if (!req->dst || !dlen)
  156. return -EINVAL;
  157. if (dst_isvirt)
  158. dst = req->dvirt;
  159. else {
  160. if (dlen <= req->dst->length) {
  161. dpage = sg_page(req->dst);
  162. doff = req->dst->offset;
  163. } else
  164. return -ENOSYS;
  165. dpage += doff / PAGE_SIZE;
  166. doff = offset_in_page(doff);
  167. n = (dlen - 1) / PAGE_SIZE;
  168. n += (offset_in_page(dlen - 1) + doff) / PAGE_SIZE;
  169. if (PageHighMem(dpage + n) &&
  170. size_add(doff, dlen) > PAGE_SIZE)
  171. return -ENOSYS;
  172. dst = kmap_local_page(dpage) + doff;
  173. }
  174. if (src_isvirt)
  175. src = req->svirt;
  176. else {
  177. src = NULL;
  178. do {
  179. if (slen <= req->src->length) {
  180. spage = sg_page(req->src);
  181. soff = req->src->offset;
  182. } else
  183. break;
  184. spage = spage + soff / PAGE_SIZE;
  185. soff = offset_in_page(soff);
  186. n = (slen - 1) / PAGE_SIZE;
  187. n += (offset_in_page(slen - 1) + soff) / PAGE_SIZE;
  188. if (PageHighMem(spage + n) &&
  189. size_add(soff, slen) > PAGE_SIZE)
  190. break;
  191. src = kmap_local_page(spage) + soff;
  192. } while (0);
  193. }
  194. struct crypto_acomp_stream *stream = crypto_acomp_lock_stream_bh(&crypto_scomp_alg(scomp)->streams);
  195. if (!src_isvirt && !src) {
  196. struct scomp_scratch *scratch = scomp_lock_scratch();
  197. const u8 *src = scratch->src;
  198. memcpy_from_sglist(scratch->src, req->src, 0, slen);
  199. if (dir)
  200. ret = crypto_scomp_compress(scomp, src, slen,
  201. dst, &dlen, stream->ctx);
  202. else
  203. ret = crypto_scomp_decompress(scomp, src, slen,
  204. dst, &dlen, stream->ctx);
  205. scomp_unlock_scratch(scratch);
  206. } else if (dir)
  207. ret = crypto_scomp_compress(scomp, src, slen,
  208. dst, &dlen, stream->ctx);
  209. else
  210. ret = crypto_scomp_decompress(scomp, src, slen,
  211. dst, &dlen, stream->ctx);
  212. crypto_acomp_unlock_stream_bh(stream);
  213. req->dlen = dlen;
  214. if (!src_isvirt && src)
  215. kunmap_local(src);
  216. if (!dst_isvirt) {
  217. kunmap_local(dst);
  218. dlen += doff;
  219. for (;;) {
  220. flush_dcache_page(dpage);
  221. if (dlen <= PAGE_SIZE)
  222. break;
  223. dlen -= PAGE_SIZE;
  224. dpage++;
  225. }
  226. }
  227. return ret;
  228. }
  229. static int scomp_acomp_compress(struct acomp_req *req)
  230. {
  231. return scomp_acomp_comp_decomp(req, 1);
  232. }
  233. static int scomp_acomp_decompress(struct acomp_req *req)
  234. {
  235. return scomp_acomp_comp_decomp(req, 0);
  236. }
  237. static void crypto_exit_scomp_ops_async(struct crypto_tfm *tfm)
  238. {
  239. struct crypto_scomp **ctx = crypto_tfm_ctx(tfm);
  240. crypto_free_scomp(*ctx);
  241. flush_work(&scomp_scratch_work);
  242. mutex_lock(&scomp_lock);
  243. if (!--scomp_scratch_users)
  244. crypto_scomp_free_scratches();
  245. mutex_unlock(&scomp_lock);
  246. }
  247. int crypto_init_scomp_ops_async(struct crypto_tfm *tfm)
  248. {
  249. struct crypto_alg *calg = tfm->__crt_alg;
  250. struct crypto_acomp *crt = __crypto_acomp_tfm(tfm);
  251. struct crypto_scomp **ctx = crypto_tfm_ctx(tfm);
  252. struct crypto_scomp *scomp;
  253. if (!crypto_mod_get(calg))
  254. return -EAGAIN;
  255. scomp = crypto_create_tfm(calg, &crypto_scomp_type);
  256. if (IS_ERR(scomp)) {
  257. crypto_mod_put(calg);
  258. return PTR_ERR(scomp);
  259. }
  260. *ctx = scomp;
  261. tfm->exit = crypto_exit_scomp_ops_async;
  262. crt->compress = scomp_acomp_compress;
  263. crt->decompress = scomp_acomp_decompress;
  264. return 0;
  265. }
  266. static void crypto_scomp_destroy(struct crypto_alg *alg)
  267. {
  268. struct scomp_alg *scomp = __crypto_scomp_alg(alg);
  269. crypto_acomp_free_streams(&scomp->streams);
  270. }
  271. static const struct crypto_type crypto_scomp_type = {
  272. .extsize = crypto_alg_extsize,
  273. .init_tfm = crypto_scomp_init_tfm,
  274. .destroy = crypto_scomp_destroy,
  275. #ifdef CONFIG_PROC_FS
  276. .show = crypto_scomp_show,
  277. #endif
  278. #if IS_ENABLED(CONFIG_CRYPTO_USER)
  279. .report = crypto_scomp_report,
  280. #endif
  281. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  282. .maskset = CRYPTO_ALG_TYPE_MASK,
  283. .type = CRYPTO_ALG_TYPE_SCOMPRESS,
  284. .tfmsize = offsetof(struct crypto_scomp, base),
  285. .algsize = offsetof(struct scomp_alg, base),
  286. };
  287. static void scomp_prepare_alg(struct scomp_alg *alg)
  288. {
  289. struct crypto_alg *base = &alg->calg.base;
  290. comp_prepare_alg(&alg->calg);
  291. base->cra_flags |= CRYPTO_ALG_REQ_VIRT;
  292. }
  293. int crypto_register_scomp(struct scomp_alg *alg)
  294. {
  295. struct crypto_alg *base = &alg->calg.base;
  296. scomp_prepare_alg(alg);
  297. base->cra_type = &crypto_scomp_type;
  298. base->cra_flags |= CRYPTO_ALG_TYPE_SCOMPRESS;
  299. return crypto_register_alg(base);
  300. }
  301. EXPORT_SYMBOL_GPL(crypto_register_scomp);
  302. void crypto_unregister_scomp(struct scomp_alg *alg)
  303. {
  304. crypto_unregister_alg(&alg->base);
  305. }
  306. EXPORT_SYMBOL_GPL(crypto_unregister_scomp);
  307. int crypto_register_scomps(struct scomp_alg *algs, int count)
  308. {
  309. int i, ret;
  310. for (i = 0; i < count; i++) {
  311. ret = crypto_register_scomp(&algs[i]);
  312. if (ret) {
  313. crypto_unregister_scomps(algs, i);
  314. return ret;
  315. }
  316. }
  317. return 0;
  318. }
  319. EXPORT_SYMBOL_GPL(crypto_register_scomps);
  320. void crypto_unregister_scomps(struct scomp_alg *algs, int count)
  321. {
  322. int i;
  323. for (i = count - 1; i >= 0; --i)
  324. crypto_unregister_scomp(&algs[i]);
  325. }
  326. EXPORT_SYMBOL_GPL(crypto_unregister_scomps);
  327. MODULE_LICENSE("GPL");
  328. MODULE_DESCRIPTION("Synchronous compression type");