From patchwork Tue Jan 26 08:15:07 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joonsoo Kim X-Patchwork-Id: 8118741 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Original-To: patchwork-linux-crypto@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 80602BEEE5 for ; Tue, 26 Jan 2016 08:18:07 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 8C67120266 for ; Tue, 26 Jan 2016 08:18:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 798622025B for ; Tue, 26 Jan 2016 08:18:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932135AbcAZIRv (ORCPT ); Tue, 26 Jan 2016 03:17:51 -0500 Received: from mail-pa0-f68.google.com ([209.85.220.68]:35238 "EHLO mail-pa0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933648AbcAZIPu (ORCPT ); Tue, 26 Jan 2016 03:15:50 -0500 Received: by mail-pa0-f68.google.com with SMTP id gi1so7685676pac.2; Tue, 26 Jan 2016 00:15:49 -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=VOr5/P+ZsugwXMsxtJKkxvlgX/I8RExrTWPboDQ8+Bo=; b=04Xqz2lNmOIXIo6T3B9zqIMeMghy3AiE0BJ7XQGH3HlLk7IKOBY1O5fxbGFhcEoOhU dbaXrDDCGzOuEbdr/mFqY+juAFOssyvJEWX7orA/9GPTDxYcA/HCtlmclBpO1qIu5zqh 6G5FWoFBdo8lOS9ZBQxFfnDmi2QjcShVOeZlull4aN4KshXEIKMlv/1xihClQ+VtD+8N SAqB8q41A9vChCH1Nsq/O/gHSOfrxr3fXgingNePAXmwrcRqk2C/pKqQsm5DZ6Ms2Q8R 8u/bkdn6wypEAcf0C2GLqLldh8jBN81a/x4yvoOkBYlh4Puk5kfHvEfOmxjRxWp1l06K 7SrQ== 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=VOr5/P+ZsugwXMsxtJKkxvlgX/I8RExrTWPboDQ8+Bo=; b=DIXA5i/+5gGnoW+LxpuLjhv4XooKIWQRXIdhX0l3qEdKq0Ie3PpAmnYZR+7Vdb66MQ IH3HSwQF64M1S/fp/OTn6ZsLvPOAf7bDuk36blxnJEhNlcbGATf6sDgv3GTNiiILiUXf lUJ6pT5sNPjc8BRYLz3j7Qc45La022sbeg56lYPi5FnUxvKDgfPDF37dQObkwauQWhud Zp59T6JaoVD6agf99QaFp+yfNeSKayYkeGYpsnDMCsVyWOqCWC4swEJQc/bgHy8xAOgu 58qShw8aD7+J2gEkQ9536IFGcTOy2XWWS21qdQP9gz6gYy6UMGVuLGbzPAUCvfV4vPFn AX2Q== X-Gm-Message-State: AG10YORcr4a32SHdKVwluk/3Zl6y88w1ffEnGt909UfXhHS0PPdAXpnUVim0/cnrYtJcyA== X-Received: by 10.67.14.234 with SMTP id fj10mr9289302pad.149.1453796149703; Tue, 26 Jan 2016 00:15:49 -0800 (PST) Received: from localhost.localdomain ([119.69.155.252]) by smtp.gmail.com with ESMTPSA id d21sm342139pfj.32.2016.01.26.00.15.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 26 Jan 2016 00:15:48 -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 05/10] crypto/lzo: support new compression APIs Date: Tue, 26 Jan 2016 17:15:07 +0900 Message-Id: <1453796112-14273-6-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/lzo.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 83 insertions(+), 13 deletions(-) diff --git a/crypto/Kconfig b/crypto/Kconfig index f22f4e9..b4b485c 100644 --- a/crypto/Kconfig +++ b/crypto/Kconfig @@ -1503,6 +1503,7 @@ config CRYPTO_LZO select CRYPTO_ALGAPI select LZO_COMPRESS select LZO_DECOMPRESS + select CRYPTO_COMPRESS2 help This is the LZO algorithm. diff --git a/crypto/lzo.c b/crypto/lzo.c index 4b3e925..94cd7a4 100644 --- a/crypto/lzo.c +++ b/crypto/lzo.c @@ -23,39 +23,56 @@ #include #include +#include + struct lzo_ctx { void *lzo_comp_mem; }; +static void *lzo_alloc_ctx(struct crypto_scomp *tfm) +{ + void *ctx; + + ctx = kmalloc(LZO1X_MEM_COMPRESS, + GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT); + if (!ctx) + ctx = vmalloc(LZO1X_MEM_COMPRESS); + if (!ctx) + return ERR_PTR(-ENOMEM); + + return ctx; +} + static int lzo_init(struct crypto_tfm *tfm) { struct lzo_ctx *ctx = crypto_tfm_ctx(tfm); - ctx->lzo_comp_mem = kmalloc(LZO1X_MEM_COMPRESS, - GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT); - if (!ctx->lzo_comp_mem) - ctx->lzo_comp_mem = vmalloc(LZO1X_MEM_COMPRESS); - if (!ctx->lzo_comp_mem) + ctx->lzo_comp_mem = lzo_alloc_ctx(NULL); + if (IS_ERR(ctx->lzo_comp_mem)) return -ENOMEM; return 0; } +static void lzo_free_ctx(struct crypto_scomp *tfm, void *ctx) +{ + kvfree(ctx); +} + static void lzo_exit(struct crypto_tfm *tfm) { struct lzo_ctx *ctx = crypto_tfm_ctx(tfm); - kvfree(ctx->lzo_comp_mem); + lzo_free_ctx(NULL, ctx->lzo_comp_mem); } -static int lzo_compress(struct crypto_tfm *tfm, const u8 *src, - unsigned int slen, u8 *dst, unsigned int *dlen) +static int __lzo_compress(const u8 *src, unsigned int slen, + u8 *dst, unsigned int *dlen, void *ctx) { - struct lzo_ctx *ctx = crypto_tfm_ctx(tfm); size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */ int err; - err = lzo1x_1_compress(src, slen, dst, &tmp_len, ctx->lzo_comp_mem); + err = lzo1x_1_compress(src, slen, dst, &tmp_len, ctx); if (err != LZO_E_OK) return -EINVAL; @@ -64,8 +81,22 @@ static int lzo_compress(struct crypto_tfm *tfm, const u8 *src, return 0; } -static int lzo_decompress(struct crypto_tfm *tfm, const u8 *src, - unsigned int slen, u8 *dst, unsigned int *dlen) +static int lzo_compress(struct crypto_tfm *tfm, const u8 *src, + unsigned int slen, u8 *dst, unsigned int *dlen) +{ + struct lzo_ctx *ctx = crypto_tfm_ctx(tfm); + + return __lzo_compress(src, slen, dst, dlen, ctx->lzo_comp_mem); +} + +static int lzo_scompress(struct crypto_scomp *tfm, const u8 *src, + unsigned int slen, u8 *dst, unsigned int *dlen, void *ctx) +{ + return __lzo_compress(src, slen, dst, dlen, ctx); +} + +static int __lzo_decompress(const u8 *src, unsigned int slen, + u8 *dst, unsigned int *dlen) { int err; size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */ @@ -77,7 +108,18 @@ static int lzo_decompress(struct crypto_tfm *tfm, const u8 *src, *dlen = tmp_len; return 0; +} + +static int lzo_decompress(struct crypto_tfm *tfm, const u8 *src, + unsigned int slen, u8 *dst, unsigned int *dlen) +{ + return __lzo_decompress(src, slen, dst, dlen); +} +static int lzo_sdecompress(struct crypto_scomp *tfm, const u8 *src, + unsigned int slen, u8 *dst, unsigned int *dlen, void *ctx) +{ + return __lzo_decompress(src, slen, dst, dlen); } static struct crypto_alg alg = { @@ -92,14 +134,41 @@ static struct crypto_alg alg = { .coa_decompress = lzo_decompress } } }; +static struct scomp_alg scomp = { + .alloc_ctx = lzo_alloc_ctx, + .free_ctx = lzo_free_ctx, + .compress = lzo_scompress, + .decompress = lzo_sdecompress, + .base = { + .cra_name = "lzo", + .cra_driver_name= "lzo-scomp", + .cra_flags = CRYPTO_ALG_TYPE_SCOMPRESS | + CRYPTO_SCOMP_DECOMP_NOCTX, + .cra_module = THIS_MODULE, + } +}; + static int __init lzo_mod_init(void) { - return crypto_register_alg(&alg); + int ret; + + ret = crypto_register_alg(&alg); + if (ret) + return ret; + + ret = crypto_register_scomp(&scomp); + if (ret) { + crypto_unregister_alg(&alg); + return ret; + } + + return ret; } static void __exit lzo_mod_fini(void) { crypto_unregister_alg(&alg); + crypto_unregister_scomp(&scomp); } module_init(lzo_mod_init);