From patchwork Tue Jan 26 08:15:08 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joonsoo Kim X-Patchwork-Id: 8118631 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Original-To: patchwork-linux-crypto@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id A98709F859 for ; Tue, 26 Jan 2016 08:16:22 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id BF4C420266 for ; Tue, 26 Jan 2016 08:16:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id C6F182025B for ; Tue, 26 Jan 2016 08:16:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933746AbcAZIP6 (ORCPT ); Tue, 26 Jan 2016 03:15:58 -0500 Received: from mail-pa0-f67.google.com ([209.85.220.67]:36541 "EHLO mail-pa0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933527AbcAZIPy (ORCPT ); Tue, 26 Jan 2016 03:15:54 -0500 Received: by mail-pa0-f67.google.com with SMTP id a20so7630164pag.3; Tue, 26 Jan 2016 00:15:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=KhbleezZLGbfgJRAAE13tvt+vkR9D10DH78kcw4TVXg=; b=vtmYmaPrR4W+jA16OvtSZXlbfN6aiL6k3BCQ1qskIDy8aREeP4xQYHEbv8vkKdy/FL O4jRWF3NVRi/bFA7NHfUxE5sRxwqyeTD/X9Fh1iP3l84mWg+sZ5R3LURovB7JcUTHlOZ xFeAedMmLYo7FHgCMBww9HxkYz/aCj73dpfwWnVlW2Dk8F9MCmEKockHj+t17uSJZS05 dx8dr8ByCOYDqCyJmiTCuUBqw3R1pyiU+E1+YZMGz+Tnjx0j3baz+LBre/krz9HQTitH iFFBF8U+jjnMW5GdgKGT3EIbsFKWavi79A04BHpswWfhPEP5rUH2k4BQhYMaJWSXp4F+ 3lyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=KhbleezZLGbfgJRAAE13tvt+vkR9D10DH78kcw4TVXg=; b=mFyLUTjEv0zTcCYQ/NzPsE0xRcaTJHcOlV5w47Hrvkkxu8qinpgnF1QThD8gpbUUKD 38hOg1gwuhDQH8KtStVBtrHtlOSnNPuN8VMqk8TTOxXWMurf/rWSaaIms+pxPlHCpQ90 zPrqQeDEtb4EwzhzAGw2dnVSo6l7qScwp6QrybNyQTUFrvdNvuR1M4Wkg67Pt+D6+t+a 1Jaebsu9sVRd+ulijUo88ny+OTbwrvjUb7lNlgP3yZXXF5wKKUIS486c55U4x6wkqmzk r3J2lIfKP3eZB0EBdXQOqwHe/O0/ef3VdylLsGbRz35LbumlHEzMS1QlB8x+yU7ugCaR Rxpg== X-Gm-Message-State: AG10YOSpuz+1sjYWN1KpKR9m/rltsG+me0MYs11uBrCsEQYo6H1cSNWi7LJbrYs5C4MZgQ== X-Received: by 10.67.6.67 with SMTP id cs3mr32166075pad.143.1453796154432; Tue, 26 Jan 2016 00:15:54 -0800 (PST) Received: from localhost.localdomain ([119.69.155.252]) by smtp.gmail.com with ESMTPSA id d21sm342139pfj.32.2016.01.26.00.15.50 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 26 Jan 2016 00:15:53 -0800 (PST) From: Joonsoo Kim X-Google-Original-From: Joonsoo Kim To: Herbert Xu Cc: "David S. Miller" , Weigang Li , Sergey Senozhatsky , Minchan Kim , Dan Streetman , linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, Joonsoo Kim Subject: [PATCH v2 06/10] crypto/lz4: support new compression APIs Date: Tue, 26 Jan 2016 17:15:08 +0900 Message-Id: <1453796112-14273-7-git-send-email-iamjoonsoo.kim@lge.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1453796112-14273-1-git-send-email-iamjoonsoo.kim@lge.com> References: <1453796112-14273-1-git-send-email-iamjoonsoo.kim@lge.com> Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Now, new compression APIs are introduced and it has some benefits. Let's support it. Signed-off-by: Joonsoo Kim --- crypto/Kconfig | 1 + crypto/lz4.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 82 insertions(+), 10 deletions(-) diff --git a/crypto/Kconfig b/crypto/Kconfig index b4b485c..72ab0d7 100644 --- a/crypto/Kconfig +++ b/crypto/Kconfig @@ -1520,6 +1520,7 @@ config CRYPTO_LZ4 select CRYPTO_ALGAPI select LZ4_COMPRESS select LZ4_DECOMPRESS + select CRYPTO_COMPRESS2 help This is the LZ4 algorithm. diff --git a/crypto/lz4.c b/crypto/lz4.c index aefbcea..728c6c4 100644 --- a/crypto/lz4.c +++ b/crypto/lz4.c @@ -24,35 +24,53 @@ #include #include +#include + struct lz4_ctx { void *lz4_comp_mem; }; +static void *lz4_alloc_ctx(struct crypto_scomp *tfm) +{ + void *ctx; + + ctx = vmalloc(LZ4_MEM_COMPRESS); + if (!ctx) + return ERR_PTR(-ENOMEM); + + return ctx; +} + static int lz4_init(struct crypto_tfm *tfm) { struct lz4_ctx *ctx = crypto_tfm_ctx(tfm); - ctx->lz4_comp_mem = vmalloc(LZ4_MEM_COMPRESS); - if (!ctx->lz4_comp_mem) + ctx->lz4_comp_mem = lz4_alloc_ctx(NULL); + if (IS_ERR(ctx->lz4_comp_mem)) return -ENOMEM; return 0; } +static void lz4_free_ctx(struct crypto_scomp *tfm, void *ctx) +{ + vfree(ctx); +} + static void lz4_exit(struct crypto_tfm *tfm) { struct lz4_ctx *ctx = crypto_tfm_ctx(tfm); - vfree(ctx->lz4_comp_mem); + + lz4_free_ctx(NULL, ctx->lz4_comp_mem); } -static int lz4_compress_crypto(struct crypto_tfm *tfm, const u8 *src, - unsigned int slen, u8 *dst, unsigned int *dlen) +static int __lz4_compress_crypto(const u8 *src, unsigned int slen, + u8 *dst, unsigned int *dlen, void *ctx) { - struct lz4_ctx *ctx = crypto_tfm_ctx(tfm); size_t tmp_len = *dlen; int err; - err = lz4_compress(src, slen, dst, &tmp_len, ctx->lz4_comp_mem); + err = lz4_compress(src, slen, dst, &tmp_len, ctx); if (err < 0) return -EINVAL; @@ -61,8 +79,22 @@ static int lz4_compress_crypto(struct crypto_tfm *tfm, const u8 *src, return 0; } -static int lz4_decompress_crypto(struct crypto_tfm *tfm, const u8 *src, - unsigned int slen, u8 *dst, unsigned int *dlen) +static int lz4_scompress(struct crypto_scomp *tfm, const u8 *src, + unsigned int slen, u8 *dst, unsigned int *dlen, void *ctx) +{ + return __lz4_compress_crypto(src, slen, dst, dlen, ctx); +} + +static int lz4_compress_crypto(struct crypto_tfm *tfm, const u8 *src, + unsigned int slen, u8 *dst, unsigned int *dlen) +{ + struct lz4_ctx *ctx = crypto_tfm_ctx(tfm); + + return __lz4_compress_crypto(src, slen, dst, dlen, ctx->lz4_comp_mem); +} + +static int __lz4_decompress_crypto(const u8 *src, unsigned int slen, + u8 *dst, unsigned int *dlen, void *ctx) { int err; size_t tmp_len = *dlen; @@ -76,6 +108,18 @@ static int lz4_decompress_crypto(struct crypto_tfm *tfm, const u8 *src, return err; } +static int lz4_sdecompress(struct crypto_scomp *tfm, const u8 *src, + unsigned int slen, u8 *dst, unsigned int *dlen, void *ctx) +{ + return __lz4_decompress_crypto(src, slen, dst, dlen, NULL); +} + +static int lz4_decompress_crypto(struct crypto_tfm *tfm, const u8 *src, + unsigned int slen, u8 *dst, unsigned int *dlen) +{ + return __lz4_decompress_crypto(src, slen, dst, dlen, NULL); +} + static struct crypto_alg alg_lz4 = { .cra_name = "lz4", .cra_flags = CRYPTO_ALG_TYPE_COMPRESS, @@ -89,14 +133,41 @@ static struct crypto_alg alg_lz4 = { .coa_decompress = lz4_decompress_crypto } } }; +static struct scomp_alg scomp = { + .alloc_ctx = lz4_alloc_ctx, + .free_ctx = lz4_free_ctx, + .compress = lz4_scompress, + .decompress = lz4_sdecompress, + .base = { + .cra_name = "lz4", + .cra_driver_name= "lz4-scomp", + .cra_flags = CRYPTO_ALG_TYPE_SCOMPRESS | + CRYPTO_SCOMP_DECOMP_NOCTX, + .cra_module = THIS_MODULE, + } +}; + static int __init lz4_mod_init(void) { - return crypto_register_alg(&alg_lz4); + int ret; + + ret = crypto_register_alg(&alg_lz4); + if (ret) + return ret; + + ret = crypto_register_scomp(&scomp); + if (ret) { + crypto_unregister_alg(&alg_lz4); + return ret; + } + + return ret; } static void __exit lz4_mod_fini(void) { crypto_unregister_alg(&alg_lz4); + crypto_unregister_scomp(&scomp); } module_init(lz4_mod_init);