From patchwork Tue Jan 26 08:15:09 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joonsoo Kim X-Patchwork-Id: 8118641 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 B79B99FD29 for ; Tue, 26 Jan 2016 08:16:23 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id C71292025B for ; Tue, 26 Jan 2016 08:16:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id C41542026C for ; Tue, 26 Jan 2016 08:16:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933828AbcAZIQB (ORCPT ); Tue, 26 Jan 2016 03:16:01 -0500 Received: from mail-pa0-f65.google.com ([209.85.220.65]:36549 "EHLO mail-pa0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933749AbcAZIP7 (ORCPT ); Tue, 26 Jan 2016 03:15:59 -0500 Received: by mail-pa0-f65.google.com with SMTP id a20so7630229pag.3; Tue, 26 Jan 2016 00:15:59 -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=m8Jl9Nzfcb41ijQvre92tFG+wYCxgbCqh8ck6bJGbLE=; b=Ff1iMuFFXohiRaVhMfl+0yoNP5SEg9ma+rJMR0wo3osfam6OIJ8oE2xFJ0bVgxL8XT WEXoltOSGdqEKtVR7v4IWqVJ6Usc1HkSnmmrAk6hODlPfQGFP6fhKIPNgiwQ3/pIJctl KSdsmJxiBkFNRLWhCIPcX2+eMRp6iglInEzeOd9+TuXq4taddzF3z/x7EGfeRGez6674 U4nwRk4NC5tr44TCMa/7zJrwWvyCv2Sta9JQK88yv4ByS33NOqxdP0OhBK7Fg6qtjSJl UviEnxpkRWF/VMKWbW/Lt6O3A/poU4/6kfjDC459I1qIZ6sUwb64XRlkBwkn/Ww6TRBv BdbQ== 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=m8Jl9Nzfcb41ijQvre92tFG+wYCxgbCqh8ck6bJGbLE=; b=CmxtqlVyjj2Z7WaFpLIINzw3JqGAKas65uxbKB8rJgcfYe3pViuox4W5wkjlR8ZqR3 eQTRXk5aG2XkmAhCQ7KRE7DX5N8+vRSPGLRhfzyb7TGWBufITh1GsP3P6PpeRCygVAUR mw9qj0TnlfogZ0k3iBCiW/C4HxuTrZz/lY7EOvhpmBpYkQE/0GXo9k2KD19wNczDr5Mz ly5k/Mpw6SNZeldoWLzk/eKtp0ZYOZgv1Uh2YiRkAhzlUr6V9A4iNyg8xTALHTzp6JZu M33rR7sfiKb29NZCTlNqMI/6rV6S/uPStNHmhrh0mXtcvg72VUQ1ntZp0Uq5wdg0oCtX w23g== X-Gm-Message-State: AG10YOTzjT9eO4ocMQkbl6o4aAwfcCfwjY6xVQjt6LUFHq2G1eNC6ED1mzvSweUzACAjaQ== X-Received: by 10.66.234.8 with SMTP id ua8mr32185633pac.45.1453796159155; Tue, 26 Jan 2016 00:15:59 -0800 (PST) Received: from localhost.localdomain ([119.69.155.252]) by smtp.gmail.com with ESMTPSA id d21sm342139pfj.32.2016.01.26.00.15.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 26 Jan 2016 00:15:58 -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 07/10] crypto/lz4hc: support new compression APIs Date: Tue, 26 Jan 2016 17:15:09 +0900 Message-Id: <1453796112-14273-8-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/lz4hc.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 82 insertions(+), 10 deletions(-) diff --git a/crypto/Kconfig b/crypto/Kconfig index 72ab0d7..2641a60 100644 --- a/crypto/Kconfig +++ b/crypto/Kconfig @@ -1529,6 +1529,7 @@ config CRYPTO_LZ4HC select CRYPTO_ALGAPI select LZ4HC_COMPRESS select LZ4_DECOMPRESS + select CRYPTO_COMPRESS2 help This is the LZ4 high compression mode algorithm. diff --git a/crypto/lz4hc.c b/crypto/lz4hc.c index a1d3b5b..569a8a7 100644 --- a/crypto/lz4hc.c +++ b/crypto/lz4hc.c @@ -23,36 +23,53 @@ #include #include +#include + struct lz4hc_ctx { void *lz4hc_comp_mem; }; +static void *lz4hc_alloc_ctx(struct crypto_scomp *tfm) +{ + void *ctx; + + ctx = vmalloc(LZ4HC_MEM_COMPRESS); + if (!ctx) + return ERR_PTR(-ENOMEM); + + return ctx; +} + static int lz4hc_init(struct crypto_tfm *tfm) { struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm); - ctx->lz4hc_comp_mem = vmalloc(LZ4HC_MEM_COMPRESS); - if (!ctx->lz4hc_comp_mem) + ctx->lz4hc_comp_mem = lz4hc_alloc_ctx(NULL); + if (IS_ERR(ctx->lz4hc_comp_mem)) return -ENOMEM; return 0; } +static void lz4hc_free_ctx(struct crypto_scomp *tfm, void *ctx) +{ + vfree(ctx); +} + static void lz4hc_exit(struct crypto_tfm *tfm) { struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm); - vfree(ctx->lz4hc_comp_mem); + lz4hc_free_ctx(NULL, ctx->lz4hc_comp_mem); } -static int lz4hc_compress_crypto(struct crypto_tfm *tfm, const u8 *src, - unsigned int slen, u8 *dst, unsigned int *dlen) +static int __lz4hc_compress_crypto(const u8 *src, unsigned int slen, + u8 *dst, unsigned int *dlen, void *ctx) { - struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm); size_t tmp_len = *dlen; int err; - err = lz4hc_compress(src, slen, dst, &tmp_len, ctx->lz4hc_comp_mem); + err = lz4hc_compress(src, slen, dst, &tmp_len, ctx); if (err < 0) return -EINVAL; @@ -61,8 +78,23 @@ static int lz4hc_compress_crypto(struct crypto_tfm *tfm, const u8 *src, return 0; } -static int lz4hc_decompress_crypto(struct crypto_tfm *tfm, const u8 *src, - unsigned int slen, u8 *dst, unsigned int *dlen) +static int lz4hc_scompress(struct crypto_scomp *tfm, const u8 *src, + unsigned int slen, u8 *dst, unsigned int *dlen, void *ctx) +{ + return __lz4hc_compress_crypto(src, slen, dst, dlen, ctx); +} + +static int lz4hc_compress_crypto(struct crypto_tfm *tfm, const u8 *src, + unsigned int slen, u8 *dst, unsigned int *dlen) +{ + struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm); + + return __lz4hc_compress_crypto(src, slen, dst, dlen, + ctx->lz4hc_comp_mem); +} + +static int __lz4hc_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 lz4hc_decompress_crypto(struct crypto_tfm *tfm, const u8 *src, return err; } +static int lz4hc_sdecompress(struct crypto_scomp *tfm, const u8 *src, + unsigned int slen, u8 *dst, unsigned int *dlen, void *ctx) +{ + return __lz4hc_decompress_crypto(src, slen, dst, dlen, NULL); +} + +static int lz4hc_decompress_crypto(struct crypto_tfm *tfm, const u8 *src, + unsigned int slen, u8 *dst, unsigned int *dlen) +{ + return __lz4hc_decompress_crypto(src, slen, dst, dlen, NULL); +} + static struct crypto_alg alg_lz4hc = { .cra_name = "lz4hc", .cra_flags = CRYPTO_ALG_TYPE_COMPRESS, @@ -89,14 +133,41 @@ static struct crypto_alg alg_lz4hc = { .coa_decompress = lz4hc_decompress_crypto } } }; +static struct scomp_alg scomp = { + .alloc_ctx = lz4hc_alloc_ctx, + .free_ctx = lz4hc_free_ctx, + .compress = lz4hc_scompress, + .decompress = lz4hc_sdecompress, + .base = { + .cra_name = "lz4hc", + .cra_driver_name= "lz4hc-scomp", + .cra_flags = CRYPTO_ALG_TYPE_SCOMPRESS | + CRYPTO_SCOMP_DECOMP_NOCTX, + .cra_module = THIS_MODULE, + } +}; + static int __init lz4hc_mod_init(void) { - return crypto_register_alg(&alg_lz4hc); + int ret; + + ret = crypto_register_alg(&alg_lz4hc); + if (ret) + return ret; + + ret = crypto_register_scomp(&scomp); + if (ret) { + crypto_unregister_alg(&alg_lz4hc); + return ret; + } + + return ret; } static void __exit lz4hc_mod_fini(void) { crypto_unregister_alg(&alg_lz4hc); + crypto_unregister_scomp(&scomp); } module_init(lz4hc_mod_init);