From patchwork Tue Sep 13 12:49:40 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Cabiddu, Giovanni" X-Patchwork-Id: 9328967 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id CAD136077F for ; Tue, 13 Sep 2016 12:50:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BF4E428531 for ; Tue, 13 Sep 2016 12:50:08 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B3C3228E5B; Tue, 13 Sep 2016 12:50:08 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 337C328531 for ; Tue, 13 Sep 2016 12:50:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754612AbcIMMuE (ORCPT ); Tue, 13 Sep 2016 08:50:04 -0400 Received: from mga05.intel.com ([192.55.52.43]:6697 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754631AbcIMMt4 (ORCPT ); Tue, 13 Sep 2016 08:49:56 -0400 Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga105.fm.intel.com with ESMTP; 13 Sep 2016 05:49:55 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.30,328,1470726000"; d="scan'208";a="7842092" Received: from irvmail001.ir.intel.com ([163.33.26.43]) by fmsmga006.fm.intel.com with ESMTP; 13 Sep 2016 05:49:54 -0700 Received: from sivswdev01.ir.intel.com (sivswdev01.ir.intel.com [10.237.217.45]) by irvmail001.ir.intel.com (8.14.3/8.13.6/MailSET/Hub) with ESMTP id u8DCnrwB020714; Tue, 13 Sep 2016 13:49:53 +0100 Received: from sivswdev01.ir.intel.com (localhost [127.0.0.1]) by sivswdev01.ir.intel.com with ESMTP id u8DCnrWh012260; Tue, 13 Sep 2016 13:49:53 +0100 Received: (from gcabiddu@localhost) by sivswdev01.ir.intel.com with id u8DCnrfG012256; Tue, 13 Sep 2016 13:49:53 +0100 From: Giovanni Cabiddu To: herbert@gondor.apana.org.au Cc: linux-crypto@vger.kernel.org, Giovanni Cabiddu Subject: [PATCH v7 8/9] crypto: acomp - add support for deflate via scomp Date: Tue, 13 Sep 2016 13:49:40 +0100 Message-Id: <1473770981-9869-9-git-send-email-giovanni.cabiddu@intel.com> X-Mailer: git-send-email 1.7.4.1 In-Reply-To: <1473770981-9869-1-git-send-email-giovanni.cabiddu@intel.com> References: <1473770981-9869-1-git-send-email-giovanni.cabiddu@intel.com> Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add scomp backend for deflate compression algorithm Signed-off-by: Giovanni Cabiddu --- crypto/Kconfig | 1 + crypto/deflate.c | 166 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 157 insertions(+), 10 deletions(-) diff --git a/crypto/Kconfig b/crypto/Kconfig index ac7b519..7d4808f 100644 --- a/crypto/Kconfig +++ b/crypto/Kconfig @@ -1578,6 +1578,7 @@ comment "Compression" config CRYPTO_DEFLATE tristate "Deflate compression algorithm" select CRYPTO_ALGAPI + select CRYPTO_ACOMP2 select ZLIB_INFLATE select ZLIB_DEFLATE help diff --git a/crypto/deflate.c b/crypto/deflate.c index 95d8d37..f924031 100644 --- a/crypto/deflate.c +++ b/crypto/deflate.c @@ -32,16 +32,22 @@ #include #include #include +#include +#include #define DEFLATE_DEF_LEVEL Z_DEFAULT_COMPRESSION #define DEFLATE_DEF_WINBITS 11 #define DEFLATE_DEF_MEMLEVEL MAX_MEM_LEVEL +#define DEFLATE_SCR_SIZE 131072 struct deflate_ctx { struct z_stream_s comp_stream; struct z_stream_s decomp_stream; }; +static void * __percpu *deflate_src_scratches; +static void * __percpu *deflate_dst_scratches; + static int deflate_comp_init(struct deflate_ctx *ctx) { int ret = 0; @@ -101,9 +107,8 @@ static void deflate_decomp_exit(struct deflate_ctx *ctx) vfree(ctx->decomp_stream.workspace); } -static int deflate_init(struct crypto_tfm *tfm) +static int __deflate_init(void *ctx) { - struct deflate_ctx *ctx = crypto_tfm_ctx(tfm); int ret; ret = deflate_comp_init(ctx); @@ -116,19 +121,55 @@ out: return ret; } -static void deflate_exit(struct crypto_tfm *tfm) +static void *deflate_alloc_ctx(struct crypto_scomp *tfm) +{ + struct deflate_ctx *ctx; + int ret; + + ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); + if (!ctx) + return ERR_PTR(-ENOMEM); + + ret = __deflate_init(ctx); + if (ret) { + kfree(ctx); + return ERR_PTR(ret); + } + + return ctx; +} + +static int deflate_init(struct crypto_tfm *tfm) { struct deflate_ctx *ctx = crypto_tfm_ctx(tfm); + return __deflate_init(ctx); +} + +static void __deflate_exit(void *ctx) +{ deflate_comp_exit(ctx); deflate_decomp_exit(ctx); } -static int deflate_compress(struct crypto_tfm *tfm, const u8 *src, - unsigned int slen, u8 *dst, unsigned int *dlen) +static void deflate_free_ctx(struct crypto_scomp *tfm, void *ctx) +{ + __deflate_exit(ctx); + kzfree(ctx); +} + +static void deflate_exit(struct crypto_tfm *tfm) +{ + struct deflate_ctx *ctx = crypto_tfm_ctx(tfm); + + __deflate_exit(ctx); +} + +static int __deflate_compress(const u8 *src, unsigned int slen, + u8 *dst, unsigned int *dlen, void *ctx) { int ret = 0; - struct deflate_ctx *dctx = crypto_tfm_ctx(tfm); + struct deflate_ctx *dctx = ctx; struct z_stream_s *stream = &dctx->comp_stream; ret = zlib_deflateReset(stream); @@ -153,12 +194,20 @@ out: return ret; } -static int deflate_decompress(struct crypto_tfm *tfm, const u8 *src, - unsigned int slen, u8 *dst, unsigned int *dlen) +static int deflate_compress(struct crypto_tfm *tfm, const u8 *src, + unsigned int slen, u8 *dst, unsigned int *dlen) +{ + struct deflate_ctx *dctx = crypto_tfm_ctx(tfm); + + return __deflate_compress(src, slen, dst, dlen, dctx); +} + +static int __deflate_decompress(const u8 *src, unsigned int slen, + u8 *dst, unsigned int *dlen, void *ctx) { int ret = 0; - struct deflate_ctx *dctx = crypto_tfm_ctx(tfm); + struct deflate_ctx *dctx = ctx; struct z_stream_s *stream = &dctx->decomp_stream; ret = zlib_inflateReset(stream); @@ -194,6 +243,61 @@ out: return ret; } +static int deflate_decompress(struct crypto_tfm *tfm, const u8 *src, + unsigned int slen, u8 *dst, unsigned int *dlen) +{ + struct deflate_ctx *dctx = crypto_tfm_ctx(tfm); + + return __deflate_decompress(src, slen, dst, dlen, dctx); +} + +static int deflate_scomp_comp_decomp(struct crypto_scomp *tfm, + struct scatterlist *src, + unsigned int slen, + struct scatterlist *dst, + unsigned int *dlen, void *ctx, int dir) +{ + const int cpu = get_cpu(); + u8 *scratch_src = *per_cpu_ptr(deflate_src_scratches, cpu); + u8 *scratch_dst = *per_cpu_ptr(deflate_dst_scratches, cpu); + int ret; + + if (slen > DEFLATE_SCR_SIZE || *dlen > DEFLATE_SCR_SIZE) { + ret = -EINVAL; + goto out; + } + + scatterwalk_map_and_copy(scratch_src, src, 0, slen, 0); + if (dir) + ret = __deflate_compress(scratch_src, slen, scratch_dst, + dlen, ctx); + else + ret = __deflate_decompress(scratch_src, slen, scratch_dst, + dlen, ctx); + if (!ret) + scatterwalk_map_and_copy(scratch_dst, dst, 0, *dlen, 1); + +out: + put_cpu(); + return ret; +} + +static int deflate_scomp_compress(struct crypto_scomp *tfm, + struct scatterlist *src, unsigned int slen, + struct scatterlist *dst, unsigned int *dlen, + void *ctx) +{ + return deflate_scomp_comp_decomp(tfm, src, slen, dst, dlen, ctx, 1); +} + +static int deflate_scomp_decompress(struct crypto_scomp *tfm, + struct scatterlist *src, unsigned int slen, + struct scatterlist *dst, unsigned int *dlen, + void *ctx) +{ + return deflate_scomp_comp_decomp(tfm, src, slen, dst, dlen, ctx, 0); +} + static struct crypto_alg alg = { .cra_name = "deflate", .cra_flags = CRYPTO_ALG_TYPE_COMPRESS, @@ -206,14 +310,56 @@ static struct crypto_alg alg = { .coa_decompress = deflate_decompress } } }; +static struct scomp_alg scomp = { + .alloc_ctx = deflate_alloc_ctx, + .free_ctx = deflate_free_ctx, + .compress = deflate_scomp_compress, + .decompress = deflate_scomp_decompress, + .base = { + .cra_name = "deflate", + .cra_driver_name = "deflate-scomp", + .cra_module = THIS_MODULE, + } +}; + static int __init deflate_mod_init(void) { - return crypto_register_alg(&alg); + int ret; + + deflate_src_scratches = crypto_scomp_alloc_scratches(DEFLATE_SCR_SIZE); + if (!deflate_src_scratches) + return -ENOMEM; + + deflate_dst_scratches = crypto_scomp_alloc_scratches(DEFLATE_SCR_SIZE); + if (!deflate_dst_scratches) { + ret = -ENOMEM; + goto error; + } + + ret = crypto_register_alg(&alg); + if (ret) + goto error; + + ret = crypto_register_scomp(&scomp); + if (ret) { + crypto_unregister_alg(&alg); + goto error; + } + + return ret; + +error: + crypto_scomp_free_scratches(deflate_src_scratches); + crypto_scomp_free_scratches(deflate_dst_scratches); + return ret; } static void __exit deflate_mod_fini(void) { crypto_unregister_alg(&alg); + crypto_unregister_scomp(&scomp); + crypto_scomp_free_scratches(deflate_src_scratches); + crypto_scomp_free_scratches(deflate_dst_scratches); } module_init(deflate_mod_init);