Commit 91d53d96e27018d4f49b9e5994cc1e74a4fc5d92

Authored by Giovanni Cabiddu
Committed by Herbert Xu
1 parent 8cd9330e0a

crypto: acomp - add support for lz4hc via scomp

Add scomp backend for lz4hc compression algorithm.

Signed-off-by: Giovanni Cabiddu <giovanni.cabiddu@intel.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Showing 2 changed files with 83 additions and 10 deletions Side-by-side Diff

... ... @@ -1615,6 +1615,7 @@
1615 1615 config CRYPTO_LZ4HC
1616 1616 tristate "LZ4HC compression algorithm"
1617 1617 select CRYPTO_ALGAPI
  1618 + select CRYPTO_ACOMP2
1618 1619 select LZ4HC_COMPRESS
1619 1620 select LZ4_DECOMPRESS
1620 1621 help
... ... @@ -22,37 +22,53 @@
22 22 #include <linux/crypto.h>
23 23 #include <linux/vmalloc.h>
24 24 #include <linux/lz4.h>
  25 +#include <crypto/internal/scompress.h>
25 26  
26 27 struct lz4hc_ctx {
27 28 void *lz4hc_comp_mem;
28 29 };
29 30  
  31 +static void *lz4hc_alloc_ctx(struct crypto_scomp *tfm)
  32 +{
  33 + void *ctx;
  34 +
  35 + ctx = vmalloc(LZ4HC_MEM_COMPRESS);
  36 + if (!ctx)
  37 + return ERR_PTR(-ENOMEM);
  38 +
  39 + return ctx;
  40 +}
  41 +
30 42 static int lz4hc_init(struct crypto_tfm *tfm)
31 43 {
32 44 struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm);
33 45  
34   - ctx->lz4hc_comp_mem = vmalloc(LZ4HC_MEM_COMPRESS);
35   - if (!ctx->lz4hc_comp_mem)
  46 + ctx->lz4hc_comp_mem = lz4hc_alloc_ctx(NULL);
  47 + if (IS_ERR(ctx->lz4hc_comp_mem))
36 48 return -ENOMEM;
37 49  
38 50 return 0;
39 51 }
40 52  
  53 +static void lz4hc_free_ctx(struct crypto_scomp *tfm, void *ctx)
  54 +{
  55 + vfree(ctx);
  56 +}
  57 +
41 58 static void lz4hc_exit(struct crypto_tfm *tfm)
42 59 {
43 60 struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm);
44 61  
45   - vfree(ctx->lz4hc_comp_mem);
  62 + lz4hc_free_ctx(NULL, ctx->lz4hc_comp_mem);
46 63 }
47 64  
48   -static int lz4hc_compress_crypto(struct crypto_tfm *tfm, const u8 *src,
49   - unsigned int slen, u8 *dst, unsigned int *dlen)
  65 +static int __lz4hc_compress_crypto(const u8 *src, unsigned int slen,
  66 + u8 *dst, unsigned int *dlen, void *ctx)
50 67 {
51   - struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm);
52 68 size_t tmp_len = *dlen;
53 69 int err;
54 70  
55   - err = lz4hc_compress(src, slen, dst, &tmp_len, ctx->lz4hc_comp_mem);
  71 + err = lz4hc_compress(src, slen, dst, &tmp_len, ctx);
56 72  
57 73 if (err < 0)
58 74 return -EINVAL;
59 75  
... ... @@ -61,9 +77,26 @@
61 77 return 0;
62 78 }
63 79  
64   -static int lz4hc_decompress_crypto(struct crypto_tfm *tfm, const u8 *src,
65   - unsigned int slen, u8 *dst, unsigned int *dlen)
  80 +static int lz4hc_scompress(struct crypto_scomp *tfm, const u8 *src,
  81 + unsigned int slen, u8 *dst, unsigned int *dlen,
  82 + void *ctx)
66 83 {
  84 + return __lz4hc_compress_crypto(src, slen, dst, dlen, ctx);
  85 +}
  86 +
  87 +static int lz4hc_compress_crypto(struct crypto_tfm *tfm, const u8 *src,
  88 + unsigned int slen, u8 *dst,
  89 + unsigned int *dlen)
  90 +{
  91 + struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm);
  92 +
  93 + return __lz4hc_compress_crypto(src, slen, dst, dlen,
  94 + ctx->lz4hc_comp_mem);
  95 +}
  96 +
  97 +static int __lz4hc_decompress_crypto(const u8 *src, unsigned int slen,
  98 + u8 *dst, unsigned int *dlen, void *ctx)
  99 +{
67 100 int err;
68 101 size_t tmp_len = *dlen;
69 102 size_t __slen = slen;
... ... @@ -76,6 +109,20 @@
76 109 return err;
77 110 }
78 111  
  112 +static int lz4hc_sdecompress(struct crypto_scomp *tfm, const u8 *src,
  113 + unsigned int slen, u8 *dst, unsigned int *dlen,
  114 + void *ctx)
  115 +{
  116 + return __lz4hc_decompress_crypto(src, slen, dst, dlen, NULL);
  117 +}
  118 +
  119 +static int lz4hc_decompress_crypto(struct crypto_tfm *tfm, const u8 *src,
  120 + unsigned int slen, u8 *dst,
  121 + unsigned int *dlen)
  122 +{
  123 + return __lz4hc_decompress_crypto(src, slen, dst, dlen, NULL);
  124 +}
  125 +
79 126 static struct crypto_alg alg_lz4hc = {
80 127 .cra_name = "lz4hc",
81 128 .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
82 129  
83 130  
... ... @@ -89,14 +136,39 @@
89 136 .coa_decompress = lz4hc_decompress_crypto } }
90 137 };
91 138  
  139 +static struct scomp_alg scomp = {
  140 + .alloc_ctx = lz4hc_alloc_ctx,
  141 + .free_ctx = lz4hc_free_ctx,
  142 + .compress = lz4hc_scompress,
  143 + .decompress = lz4hc_sdecompress,
  144 + .base = {
  145 + .cra_name = "lz4hc",
  146 + .cra_driver_name = "lz4hc-scomp",
  147 + .cra_module = THIS_MODULE,
  148 + }
  149 +};
  150 +
92 151 static int __init lz4hc_mod_init(void)
93 152 {
94   - return crypto_register_alg(&alg_lz4hc);
  153 + int ret;
  154 +
  155 + ret = crypto_register_alg(&alg_lz4hc);
  156 + if (ret)
  157 + return ret;
  158 +
  159 + ret = crypto_register_scomp(&scomp);
  160 + if (ret) {
  161 + crypto_unregister_alg(&alg_lz4hc);
  162 + return ret;
  163 + }
  164 +
  165 + return ret;
95 166 }
96 167  
97 168 static void __exit lz4hc_mod_fini(void)
98 169 {
99 170 crypto_unregister_alg(&alg_lz4hc);
  171 + crypto_unregister_scomp(&scomp);
100 172 }
101 173  
102 174 module_init(lz4hc_mod_init);