backend_zstd.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #include <linux/kernel.h>
  3. #include <linux/slab.h>
  4. #include <linux/vmalloc.h>
  5. #include <linux/zstd.h>
  6. #include "backend_zstd.h"
  7. struct zstd_ctx {
  8. zstd_cctx *cctx;
  9. zstd_dctx *dctx;
  10. void *cctx_mem;
  11. void *dctx_mem;
  12. };
  13. struct zstd_params {
  14. zstd_custom_mem custom_mem;
  15. zstd_cdict *cdict;
  16. zstd_ddict *ddict;
  17. zstd_parameters cprm;
  18. };
  19. /*
  20. * For C/D dictionaries we need to provide zstd with zstd_custom_mem,
  21. * which zstd uses internally to allocate/free memory when needed.
  22. *
  23. * This means that allocator.customAlloc() can be called from zcomp_compress()
  24. * under local-lock (per-CPU compression stream), in which case we must use
  25. * GFP_ATOMIC.
  26. *
  27. * Another complication here is that we can be configured as a swap device.
  28. */
  29. static void *zstd_custom_alloc(void *opaque, size_t size)
  30. {
  31. if (!preemptible())
  32. return kvzalloc(size, GFP_ATOMIC);
  33. return kvzalloc(size, __GFP_KSWAPD_RECLAIM | __GFP_NOWARN);
  34. }
  35. static void zstd_custom_free(void *opaque, void *address)
  36. {
  37. kvfree(address);
  38. }
  39. static void zstd_release_params(struct zcomp_params *params)
  40. {
  41. struct zstd_params *zp = params->drv_data;
  42. params->drv_data = NULL;
  43. if (!zp)
  44. return;
  45. zstd_free_cdict(zp->cdict);
  46. zstd_free_ddict(zp->ddict);
  47. kfree(zp);
  48. }
  49. static int zstd_setup_params(struct zcomp_params *params)
  50. {
  51. zstd_compression_parameters prm;
  52. struct zstd_params *zp;
  53. zp = kzalloc(sizeof(*zp), GFP_KERNEL);
  54. if (!zp)
  55. return -ENOMEM;
  56. params->drv_data = zp;
  57. if (params->level == ZCOMP_PARAM_NO_LEVEL)
  58. params->level = zstd_default_clevel();
  59. zp->cprm = zstd_get_params(params->level, PAGE_SIZE);
  60. zp->custom_mem.customAlloc = zstd_custom_alloc;
  61. zp->custom_mem.customFree = zstd_custom_free;
  62. prm = zstd_get_cparams(params->level, PAGE_SIZE,
  63. params->dict_sz);
  64. zp->cdict = zstd_create_cdict_byreference(params->dict,
  65. params->dict_sz,
  66. prm,
  67. zp->custom_mem);
  68. if (!zp->cdict)
  69. goto error;
  70. zp->ddict = zstd_create_ddict_byreference(params->dict,
  71. params->dict_sz,
  72. zp->custom_mem);
  73. if (!zp->ddict)
  74. goto error;
  75. return 0;
  76. error:
  77. zstd_release_params(params);
  78. return -EINVAL;
  79. }
  80. static void zstd_destroy(struct zcomp_ctx *ctx)
  81. {
  82. struct zstd_ctx *zctx = ctx->context;
  83. if (!zctx)
  84. return;
  85. /*
  86. * If ->cctx_mem and ->dctx_mem were allocated then we didn't use
  87. * C/D dictionary and ->cctx / ->dctx were "embedded" into these
  88. * buffers.
  89. *
  90. * If otherwise then we need to explicitly release ->cctx / ->dctx.
  91. */
  92. if (zctx->cctx_mem)
  93. vfree(zctx->cctx_mem);
  94. else
  95. zstd_free_cctx(zctx->cctx);
  96. if (zctx->dctx_mem)
  97. vfree(zctx->dctx_mem);
  98. else
  99. zstd_free_dctx(zctx->dctx);
  100. kfree(zctx);
  101. }
  102. static int zstd_create(struct zcomp_params *params, struct zcomp_ctx *ctx)
  103. {
  104. struct zstd_ctx *zctx;
  105. zstd_parameters prm;
  106. size_t sz;
  107. zctx = kzalloc(sizeof(*zctx), GFP_KERNEL);
  108. if (!zctx)
  109. return -ENOMEM;
  110. ctx->context = zctx;
  111. if (params->dict_sz == 0) {
  112. prm = zstd_get_params(params->level, PAGE_SIZE);
  113. sz = zstd_cctx_workspace_bound(&prm.cParams);
  114. zctx->cctx_mem = vzalloc(sz);
  115. if (!zctx->cctx_mem)
  116. goto error;
  117. zctx->cctx = zstd_init_cctx(zctx->cctx_mem, sz);
  118. if (!zctx->cctx)
  119. goto error;
  120. sz = zstd_dctx_workspace_bound();
  121. zctx->dctx_mem = vzalloc(sz);
  122. if (!zctx->dctx_mem)
  123. goto error;
  124. zctx->dctx = zstd_init_dctx(zctx->dctx_mem, sz);
  125. if (!zctx->dctx)
  126. goto error;
  127. } else {
  128. struct zstd_params *zp = params->drv_data;
  129. zctx->cctx = zstd_create_cctx_advanced(zp->custom_mem);
  130. if (!zctx->cctx)
  131. goto error;
  132. zctx->dctx = zstd_create_dctx_advanced(zp->custom_mem);
  133. if (!zctx->dctx)
  134. goto error;
  135. }
  136. return 0;
  137. error:
  138. zstd_release_params(params);
  139. zstd_destroy(ctx);
  140. return -EINVAL;
  141. }
  142. static int zstd_compress(struct zcomp_params *params, struct zcomp_ctx *ctx,
  143. struct zcomp_req *req)
  144. {
  145. struct zstd_params *zp = params->drv_data;
  146. struct zstd_ctx *zctx = ctx->context;
  147. size_t ret;
  148. if (params->dict_sz == 0)
  149. ret = zstd_compress_cctx(zctx->cctx, req->dst, req->dst_len,
  150. req->src, req->src_len, &zp->cprm);
  151. else
  152. ret = zstd_compress_using_cdict(zctx->cctx, req->dst,
  153. req->dst_len, req->src,
  154. req->src_len,
  155. zp->cdict);
  156. if (zstd_is_error(ret))
  157. return -EINVAL;
  158. req->dst_len = ret;
  159. return 0;
  160. }
  161. static int zstd_decompress(struct zcomp_params *params, struct zcomp_ctx *ctx,
  162. struct zcomp_req *req)
  163. {
  164. struct zstd_params *zp = params->drv_data;
  165. struct zstd_ctx *zctx = ctx->context;
  166. size_t ret;
  167. if (params->dict_sz == 0)
  168. ret = zstd_decompress_dctx(zctx->dctx, req->dst, req->dst_len,
  169. req->src, req->src_len);
  170. else
  171. ret = zstd_decompress_using_ddict(zctx->dctx, req->dst,
  172. req->dst_len, req->src,
  173. req->src_len, zp->ddict);
  174. if (zstd_is_error(ret))
  175. return -EINVAL;
  176. return 0;
  177. }
  178. const struct zcomp_ops backend_zstd = {
  179. .compress = zstd_compress,
  180. .decompress = zstd_decompress,
  181. .create_ctx = zstd_create,
  182. .destroy_ctx = zstd_destroy,
  183. .setup_params = zstd_setup_params,
  184. .release_params = zstd_release_params,
  185. .name = "zstd",
  186. };