acompress.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Asynchronous Compression operations
  4. *
  5. * Copyright (c) 2016, Intel Corporation
  6. * Authors: Weigang Li <weigang.li@intel.com>
  7. * Giovanni Cabiddu <giovanni.cabiddu@intel.com>
  8. */
  9. #ifndef _CRYPTO_ACOMP_H
  10. #define _CRYPTO_ACOMP_H
  11. #include <linux/atomic.h>
  12. #include <linux/args.h>
  13. #include <linux/compiler_types.h>
  14. #include <linux/container_of.h>
  15. #include <linux/crypto.h>
  16. #include <linux/err.h>
  17. #include <linux/scatterlist.h>
  18. #include <linux/slab.h>
  19. #include <linux/spinlock_types.h>
  20. #include <linux/types.h>
  21. /* Set this bit if source is virtual address instead of SG list. */
  22. #define CRYPTO_ACOMP_REQ_SRC_VIRT 0x00000002
  23. /* Set this bit for if virtual address source cannot be used for DMA. */
  24. #define CRYPTO_ACOMP_REQ_SRC_NONDMA 0x00000004
  25. /* Set this bit if destination is virtual address instead of SG list. */
  26. #define CRYPTO_ACOMP_REQ_DST_VIRT 0x00000008
  27. /* Set this bit for if virtual address destination cannot be used for DMA. */
  28. #define CRYPTO_ACOMP_REQ_DST_NONDMA 0x00000010
  29. /* Private flags that should not be touched by the user. */
  30. #define CRYPTO_ACOMP_REQ_PRIVATE \
  31. (CRYPTO_ACOMP_REQ_SRC_VIRT | CRYPTO_ACOMP_REQ_SRC_NONDMA | \
  32. CRYPTO_ACOMP_REQ_DST_VIRT | CRYPTO_ACOMP_REQ_DST_NONDMA)
  33. #define CRYPTO_ACOMP_DST_MAX 131072
  34. #define MAX_SYNC_COMP_REQSIZE 0
  35. #define ACOMP_REQUEST_ON_STACK(name, tfm) \
  36. char __##name##_req[sizeof(struct acomp_req) + \
  37. MAX_SYNC_COMP_REQSIZE] CRYPTO_MINALIGN_ATTR; \
  38. struct acomp_req *name = acomp_request_on_stack_init( \
  39. __##name##_req, (tfm))
  40. #define ACOMP_REQUEST_CLONE(name, gfp) \
  41. acomp_request_clone(name, sizeof(__##name##_req), gfp)
  42. struct acomp_req;
  43. struct folio;
  44. struct acomp_req_chain {
  45. crypto_completion_t compl;
  46. void *data;
  47. struct scatterlist ssg;
  48. struct scatterlist dsg;
  49. union {
  50. const u8 *src;
  51. struct folio *sfolio;
  52. };
  53. union {
  54. u8 *dst;
  55. struct folio *dfolio;
  56. };
  57. u32 flags;
  58. };
  59. /**
  60. * struct acomp_req - asynchronous (de)compression request
  61. *
  62. * @base: Common attributes for asynchronous crypto requests
  63. * @src: Source scatterlist
  64. * @dst: Destination scatterlist
  65. * @svirt: Source virtual address
  66. * @dvirt: Destination virtual address
  67. * @slen: Size of the input buffer
  68. * @dlen: Size of the output buffer and number of bytes produced
  69. * @chain: Private API code data, do not use
  70. * @__ctx: Start of private context data
  71. */
  72. struct acomp_req {
  73. struct crypto_async_request base;
  74. union {
  75. struct scatterlist *src;
  76. const u8 *svirt;
  77. };
  78. union {
  79. struct scatterlist *dst;
  80. u8 *dvirt;
  81. };
  82. unsigned int slen;
  83. unsigned int dlen;
  84. struct acomp_req_chain chain;
  85. void *__ctx[] CRYPTO_MINALIGN_ATTR;
  86. };
  87. /**
  88. * struct crypto_acomp - user-instantiated objects which encapsulate
  89. * algorithms and core processing logic
  90. *
  91. * @compress: Function performs a compress operation
  92. * @decompress: Function performs a de-compress operation
  93. * @reqsize: Context size for (de)compression requests
  94. * @fb: Synchronous fallback tfm
  95. * @base: Common crypto API algorithm data structure
  96. */
  97. struct crypto_acomp {
  98. int (*compress)(struct acomp_req *req);
  99. int (*decompress)(struct acomp_req *req);
  100. unsigned int reqsize;
  101. struct crypto_tfm base;
  102. };
  103. #define COMP_ALG_COMMON { \
  104. struct crypto_alg base; \
  105. }
  106. struct comp_alg_common COMP_ALG_COMMON;
  107. /**
  108. * DOC: Asynchronous Compression API
  109. *
  110. * The Asynchronous Compression API is used with the algorithms of type
  111. * CRYPTO_ALG_TYPE_ACOMPRESS (listed as type "acomp" in /proc/crypto)
  112. */
  113. /**
  114. * crypto_alloc_acomp() -- allocate ACOMPRESS tfm handle
  115. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  116. * compression algorithm e.g. "deflate"
  117. * @type: specifies the type of the algorithm
  118. * @mask: specifies the mask for the algorithm
  119. *
  120. * Allocate a handle for a compression algorithm. The returned struct
  121. * crypto_acomp is the handle that is required for any subsequent
  122. * API invocation for the compression operations.
  123. *
  124. * Return: allocated handle in case of success; IS_ERR() is true in case
  125. * of an error, PTR_ERR() returns the error code.
  126. */
  127. struct crypto_acomp *crypto_alloc_acomp(const char *alg_name, u32 type,
  128. u32 mask);
  129. /**
  130. * crypto_alloc_acomp_node() -- allocate ACOMPRESS tfm handle with desired NUMA node
  131. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  132. * compression algorithm e.g. "deflate"
  133. * @type: specifies the type of the algorithm
  134. * @mask: specifies the mask for the algorithm
  135. * @node: specifies the NUMA node the ZIP hardware belongs to
  136. *
  137. * Allocate a handle for a compression algorithm. Drivers should try to use
  138. * (de)compressors on the specified NUMA node.
  139. * The returned struct crypto_acomp is the handle that is required for any
  140. * subsequent API invocation for the compression operations.
  141. *
  142. * Return: allocated handle in case of success; IS_ERR() is true in case
  143. * of an error, PTR_ERR() returns the error code.
  144. */
  145. struct crypto_acomp *crypto_alloc_acomp_node(const char *alg_name, u32 type,
  146. u32 mask, int node);
  147. static inline struct crypto_tfm *crypto_acomp_tfm(struct crypto_acomp *tfm)
  148. {
  149. return &tfm->base;
  150. }
  151. static inline struct comp_alg_common *__crypto_comp_alg_common(
  152. struct crypto_alg *alg)
  153. {
  154. return container_of(alg, struct comp_alg_common, base);
  155. }
  156. static inline struct crypto_acomp *__crypto_acomp_tfm(struct crypto_tfm *tfm)
  157. {
  158. return container_of(tfm, struct crypto_acomp, base);
  159. }
  160. static inline struct comp_alg_common *crypto_comp_alg_common(
  161. struct crypto_acomp *tfm)
  162. {
  163. return __crypto_comp_alg_common(crypto_acomp_tfm(tfm)->__crt_alg);
  164. }
  165. static inline unsigned int crypto_acomp_reqsize(struct crypto_acomp *tfm)
  166. {
  167. return tfm->reqsize;
  168. }
  169. static inline void acomp_request_set_tfm(struct acomp_req *req,
  170. struct crypto_acomp *tfm)
  171. {
  172. crypto_request_set_tfm(&req->base, crypto_acomp_tfm(tfm));
  173. }
  174. static inline bool acomp_is_async(struct crypto_acomp *tfm)
  175. {
  176. return crypto_comp_alg_common(tfm)->base.cra_flags &
  177. CRYPTO_ALG_ASYNC;
  178. }
  179. static inline struct crypto_acomp *crypto_acomp_reqtfm(struct acomp_req *req)
  180. {
  181. return __crypto_acomp_tfm(req->base.tfm);
  182. }
  183. /**
  184. * crypto_free_acomp() -- free ACOMPRESS tfm handle
  185. *
  186. * @tfm: ACOMPRESS tfm handle allocated with crypto_alloc_acomp()
  187. *
  188. * If @tfm is a NULL or error pointer, this function does nothing.
  189. */
  190. static inline void crypto_free_acomp(struct crypto_acomp *tfm)
  191. {
  192. crypto_destroy_tfm(tfm, crypto_acomp_tfm(tfm));
  193. }
  194. static inline int crypto_has_acomp(const char *alg_name, u32 type, u32 mask)
  195. {
  196. type &= ~CRYPTO_ALG_TYPE_MASK;
  197. type |= CRYPTO_ALG_TYPE_ACOMPRESS;
  198. mask |= CRYPTO_ALG_TYPE_ACOMPRESS_MASK;
  199. return crypto_has_alg(alg_name, type, mask);
  200. }
  201. static inline const char *crypto_acomp_alg_name(struct crypto_acomp *tfm)
  202. {
  203. return crypto_tfm_alg_name(crypto_acomp_tfm(tfm));
  204. }
  205. static inline const char *crypto_acomp_driver_name(struct crypto_acomp *tfm)
  206. {
  207. return crypto_tfm_alg_driver_name(crypto_acomp_tfm(tfm));
  208. }
  209. /**
  210. * acomp_request_alloc() -- allocates asynchronous (de)compression request
  211. *
  212. * @tfm: ACOMPRESS tfm handle allocated with crypto_alloc_acomp()
  213. * @gfp: gfp to pass to kzalloc (defaults to GFP_KERNEL)
  214. *
  215. * Return: allocated handle in case of success or NULL in case of an error
  216. */
  217. static inline struct acomp_req *acomp_request_alloc_extra_noprof(
  218. struct crypto_acomp *tfm, size_t extra, gfp_t gfp)
  219. {
  220. struct acomp_req *req;
  221. size_t len;
  222. len = ALIGN(sizeof(*req) + crypto_acomp_reqsize(tfm), CRYPTO_MINALIGN);
  223. if (check_add_overflow(len, extra, &len))
  224. return NULL;
  225. req = kzalloc_noprof(len, gfp);
  226. if (likely(req))
  227. acomp_request_set_tfm(req, tfm);
  228. return req;
  229. }
  230. #define acomp_request_alloc_noprof(tfm, ...) \
  231. CONCATENATE(acomp_request_alloc_noprof_, COUNT_ARGS(__VA_ARGS__))( \
  232. tfm, ##__VA_ARGS__)
  233. #define acomp_request_alloc_noprof_0(tfm) \
  234. acomp_request_alloc_noprof_1(tfm, GFP_KERNEL)
  235. #define acomp_request_alloc_noprof_1(tfm, gfp) \
  236. acomp_request_alloc_extra_noprof(tfm, 0, gfp)
  237. #define acomp_request_alloc(...) alloc_hooks(acomp_request_alloc_noprof(__VA_ARGS__))
  238. /**
  239. * acomp_request_alloc_extra() -- allocate acomp request with extra memory
  240. *
  241. * @tfm: ACOMPRESS tfm handle allocated with crypto_alloc_acomp()
  242. * @extra: amount of extra memory
  243. * @gfp: gfp to pass to kzalloc
  244. *
  245. * Return: allocated handle in case of success or NULL in case of an error
  246. */
  247. #define acomp_request_alloc_extra(...) alloc_hooks(acomp_request_alloc_extra_noprof(__VA_ARGS__))
  248. static inline void *acomp_request_extra(struct acomp_req *req)
  249. {
  250. struct crypto_acomp *tfm = crypto_acomp_reqtfm(req);
  251. size_t len;
  252. len = ALIGN(sizeof(*req) + crypto_acomp_reqsize(tfm), CRYPTO_MINALIGN);
  253. return (void *)((char *)req + len);
  254. }
  255. static inline bool acomp_req_on_stack(struct acomp_req *req)
  256. {
  257. return crypto_req_on_stack(&req->base);
  258. }
  259. /**
  260. * acomp_request_free() -- zeroize and free asynchronous (de)compression
  261. * request as well as the output buffer if allocated
  262. * inside the algorithm
  263. *
  264. * @req: request to free
  265. */
  266. static inline void acomp_request_free(struct acomp_req *req)
  267. {
  268. if (!req || acomp_req_on_stack(req))
  269. return;
  270. kfree_sensitive(req);
  271. }
  272. /**
  273. * acomp_request_set_callback() -- Sets an asynchronous callback
  274. *
  275. * Callback will be called when an asynchronous operation on a given
  276. * request is finished.
  277. *
  278. * @req: request that the callback will be set for
  279. * @flgs: specify for instance if the operation may backlog
  280. * @cmlp: callback which will be called
  281. * @data: private data used by the caller
  282. */
  283. static inline void acomp_request_set_callback(struct acomp_req *req,
  284. u32 flgs,
  285. crypto_completion_t cmpl,
  286. void *data)
  287. {
  288. flgs &= ~CRYPTO_ACOMP_REQ_PRIVATE;
  289. flgs |= req->base.flags & CRYPTO_ACOMP_REQ_PRIVATE;
  290. crypto_request_set_callback(&req->base, flgs, cmpl, data);
  291. }
  292. /**
  293. * acomp_request_set_params() -- Sets request parameters
  294. *
  295. * Sets parameters required by an acomp operation
  296. *
  297. * @req: asynchronous compress request
  298. * @src: pointer to input buffer scatterlist
  299. * @dst: pointer to output buffer scatterlist. If this is NULL, the
  300. * acomp layer will allocate the output memory
  301. * @slen: size of the input buffer
  302. * @dlen: size of the output buffer. If dst is NULL, this can be used by
  303. * the user to specify the maximum amount of memory to allocate
  304. */
  305. static inline void acomp_request_set_params(struct acomp_req *req,
  306. struct scatterlist *src,
  307. struct scatterlist *dst,
  308. unsigned int slen,
  309. unsigned int dlen)
  310. {
  311. req->src = src;
  312. req->dst = dst;
  313. req->slen = slen;
  314. req->dlen = dlen;
  315. req->base.flags &= ~(CRYPTO_ACOMP_REQ_SRC_VIRT |
  316. CRYPTO_ACOMP_REQ_SRC_NONDMA |
  317. CRYPTO_ACOMP_REQ_DST_VIRT |
  318. CRYPTO_ACOMP_REQ_DST_NONDMA);
  319. }
  320. /**
  321. * acomp_request_set_src_sg() -- Sets source scatterlist
  322. *
  323. * Sets source scatterlist required by an acomp operation.
  324. *
  325. * @req: asynchronous compress request
  326. * @src: pointer to input buffer scatterlist
  327. * @slen: size of the input buffer
  328. */
  329. static inline void acomp_request_set_src_sg(struct acomp_req *req,
  330. struct scatterlist *src,
  331. unsigned int slen)
  332. {
  333. req->src = src;
  334. req->slen = slen;
  335. req->base.flags &= ~CRYPTO_ACOMP_REQ_SRC_NONDMA;
  336. req->base.flags &= ~CRYPTO_ACOMP_REQ_SRC_VIRT;
  337. }
  338. /**
  339. * acomp_request_set_src_dma() -- Sets DMA source virtual address
  340. *
  341. * Sets source virtual address required by an acomp operation.
  342. * The address must be usable for DMA.
  343. *
  344. * @req: asynchronous compress request
  345. * @src: virtual address pointer to input buffer
  346. * @slen: size of the input buffer
  347. */
  348. static inline void acomp_request_set_src_dma(struct acomp_req *req,
  349. const u8 *src, unsigned int slen)
  350. {
  351. req->svirt = src;
  352. req->slen = slen;
  353. req->base.flags &= ~CRYPTO_ACOMP_REQ_SRC_NONDMA;
  354. req->base.flags |= CRYPTO_ACOMP_REQ_SRC_VIRT;
  355. }
  356. /**
  357. * acomp_request_set_src_nondma() -- Sets non-DMA source virtual address
  358. *
  359. * Sets source virtual address required by an acomp operation.
  360. * The address can not be used for DMA.
  361. *
  362. * @req: asynchronous compress request
  363. * @src: virtual address pointer to input buffer
  364. * @slen: size of the input buffer
  365. */
  366. static inline void acomp_request_set_src_nondma(struct acomp_req *req,
  367. const u8 *src,
  368. unsigned int slen)
  369. {
  370. req->svirt = src;
  371. req->slen = slen;
  372. req->base.flags |= CRYPTO_ACOMP_REQ_SRC_NONDMA;
  373. req->base.flags |= CRYPTO_ACOMP_REQ_SRC_VIRT;
  374. }
  375. /**
  376. * acomp_request_set_src_folio() -- Sets source folio
  377. *
  378. * Sets source folio required by an acomp operation.
  379. *
  380. * @req: asynchronous compress request
  381. * @folio: pointer to input folio
  382. * @off: input folio offset
  383. * @len: size of the input buffer
  384. */
  385. static inline void acomp_request_set_src_folio(struct acomp_req *req,
  386. struct folio *folio, size_t off,
  387. unsigned int len)
  388. {
  389. sg_init_table(&req->chain.ssg, 1);
  390. sg_set_folio(&req->chain.ssg, folio, len, off);
  391. acomp_request_set_src_sg(req, &req->chain.ssg, len);
  392. }
  393. /**
  394. * acomp_request_set_dst_sg() -- Sets destination scatterlist
  395. *
  396. * Sets destination scatterlist required by an acomp operation.
  397. *
  398. * @req: asynchronous compress request
  399. * @dst: pointer to output buffer scatterlist
  400. * @dlen: size of the output buffer
  401. */
  402. static inline void acomp_request_set_dst_sg(struct acomp_req *req,
  403. struct scatterlist *dst,
  404. unsigned int dlen)
  405. {
  406. req->dst = dst;
  407. req->dlen = dlen;
  408. req->base.flags &= ~CRYPTO_ACOMP_REQ_DST_NONDMA;
  409. req->base.flags &= ~CRYPTO_ACOMP_REQ_DST_VIRT;
  410. }
  411. /**
  412. * acomp_request_set_dst_dma() -- Sets DMA destination virtual address
  413. *
  414. * Sets destination virtual address required by an acomp operation.
  415. * The address must be usable for DMA.
  416. *
  417. * @req: asynchronous compress request
  418. * @dst: virtual address pointer to output buffer
  419. * @dlen: size of the output buffer
  420. */
  421. static inline void acomp_request_set_dst_dma(struct acomp_req *req,
  422. u8 *dst, unsigned int dlen)
  423. {
  424. req->dvirt = dst;
  425. req->dlen = dlen;
  426. req->base.flags &= ~CRYPTO_ACOMP_REQ_DST_NONDMA;
  427. req->base.flags |= CRYPTO_ACOMP_REQ_DST_VIRT;
  428. }
  429. /**
  430. * acomp_request_set_dst_nondma() -- Sets non-DMA destination virtual address
  431. *
  432. * Sets destination virtual address required by an acomp operation.
  433. * The address can not be used for DMA.
  434. *
  435. * @req: asynchronous compress request
  436. * @dst: virtual address pointer to output buffer
  437. * @dlen: size of the output buffer
  438. */
  439. static inline void acomp_request_set_dst_nondma(struct acomp_req *req,
  440. u8 *dst, unsigned int dlen)
  441. {
  442. req->dvirt = dst;
  443. req->dlen = dlen;
  444. req->base.flags |= CRYPTO_ACOMP_REQ_DST_NONDMA;
  445. req->base.flags |= CRYPTO_ACOMP_REQ_DST_VIRT;
  446. }
  447. /**
  448. * acomp_request_set_dst_folio() -- Sets destination folio
  449. *
  450. * Sets destination folio required by an acomp operation.
  451. *
  452. * @req: asynchronous compress request
  453. * @folio: pointer to input folio
  454. * @off: input folio offset
  455. * @len: size of the input buffer
  456. */
  457. static inline void acomp_request_set_dst_folio(struct acomp_req *req,
  458. struct folio *folio, size_t off,
  459. unsigned int len)
  460. {
  461. sg_init_table(&req->chain.dsg, 1);
  462. sg_set_folio(&req->chain.dsg, folio, len, off);
  463. acomp_request_set_dst_sg(req, &req->chain.dsg, len);
  464. }
  465. /**
  466. * crypto_acomp_compress() -- Invoke asynchronous compress operation
  467. *
  468. * Function invokes the asynchronous compress operation
  469. *
  470. * @req: asynchronous compress request
  471. *
  472. * Return: zero on success; error code in case of error
  473. */
  474. int crypto_acomp_compress(struct acomp_req *req);
  475. /**
  476. * crypto_acomp_decompress() -- Invoke asynchronous decompress operation
  477. *
  478. * Function invokes the asynchronous decompress operation
  479. *
  480. * @req: asynchronous compress request
  481. *
  482. * Return: zero on success; error code in case of error
  483. */
  484. int crypto_acomp_decompress(struct acomp_req *req);
  485. static inline struct acomp_req *acomp_request_on_stack_init(
  486. char *buf, struct crypto_acomp *tfm)
  487. {
  488. struct acomp_req *req = (void *)buf;
  489. crypto_stack_request_init(&req->base, crypto_acomp_tfm(tfm));
  490. return req;
  491. }
  492. struct acomp_req *acomp_request_clone(struct acomp_req *req,
  493. size_t total, gfp_t gfp);
  494. #endif