From patchwork Wed Oct 14 07:38:18 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joonsoo Kim X-Patchwork-Id: 7391161 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 78E84BEEA4 for ; Wed, 14 Oct 2015 07:41:18 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 8B83C20761 for ; Wed, 14 Oct 2015 07:41:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 7FD2A20614 for ; Wed, 14 Oct 2015 07:41:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752624AbbJNHil (ORCPT ); Wed, 14 Oct 2015 03:38:41 -0400 Received: from mail-pa0-f46.google.com ([209.85.220.46]:32949 "EHLO mail-pa0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751990AbbJNHii (ORCPT ); Wed, 14 Oct 2015 03:38:38 -0400 Received: by pabrc13 with SMTP id rc13so46664628pab.0; Wed, 14 Oct 2015 00:38:37 -0700 (PDT) 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=/XRrglcfnmbGbnzgFIDqPya5skUamQYwrZCF1ZqGoJg=; b=y9fXlygTacLhtY0DdXcVv/eBEfv2sc8n1JZA9KiPCmzH39Jc+RGkJVzaDn6Y8FgEmq uP+/te0/g9moSmTPB7LXBqQ4wDaFJydX4aEvyqzvotEagj5nJyCEGJWFZCDCEUUOXKq0 kqZbAs1YlsoTPN0T5WxaNoc8wxFY6LxTeAf6IXnxyAIpMInaF0a6x8gZF2au0+kesvqD VBgk7Zcre2UGr44ETqJ7XSNC/qN3dKjGCPeSUBT3cBCxogOHfC1vfJwcAGHD9SnFQU4d 66CssXm6rDPgie2vBs+N8KDyQDY147Drwat8LPvjbXL9X0DtE8f8mIeqV++QuSPIXV3I HaSg== X-Received: by 10.68.110.165 with SMTP id ib5mr2198250pbb.58.1444808317585; Wed, 14 Oct 2015 00:38:37 -0700 (PDT) Received: from localhost.localdomain ([119.69.155.252]) by smtp.gmail.com with ESMTPSA id qr8sm4147173pbb.47.2015.10.14.00.38.34 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 14 Oct 2015 00:38:37 -0700 (PDT) From: Joonsoo Kim X-Google-Original-From: Joonsoo Kim To: Andrew Morton Cc: Minchan Kim , Nitin Gupta , Sergey Senozhatsky , Herbert Xu , "David S. Miller" , Stephan Mueller , linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org, Joonsoo Kim Subject: [PATCH v4 2/8] crypto/lzo: support contextless compression API Date: Wed, 14 Oct 2015 16:38:18 +0900 Message-Id: <1444808304-29320-3-git-send-email-iamjoonsoo.kim@lge.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1444808304-29320-1-git-send-email-iamjoonsoo.kim@lge.com> References: <1444808304-29320-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, T_DKIM_INVALID, T_RP_MATCHES_RCVD, 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, contextless compression API is introduced and it can reduce memory overhead in some cases. All compression algorithm will support it. Signed-off-by: Joonsoo Kim --- crypto/Kconfig | 1 + crypto/lzo.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 69 insertions(+), 13 deletions(-) diff --git a/crypto/Kconfig b/crypto/Kconfig index cfc42e6..d25746f 100644 --- a/crypto/Kconfig +++ b/crypto/Kconfig @@ -1494,6 +1494,7 @@ config CRYPTO_DEFLATE config CRYPTO_LZO tristate "LZO compression algorithm" select CRYPTO_ALGAPI + select CRYPTO_CCOMPRESS select LZO_COMPRESS select LZO_DECOMPRESS help diff --git a/crypto/lzo.c b/crypto/lzo.c index 4b3e925..a6ae752 100644 --- a/crypto/lzo.c +++ b/crypto/lzo.c @@ -22,40 +22,56 @@ #include #include #include +#include struct lzo_ctx { void *lzo_comp_mem; }; +static void *lzo_alloc_context(struct crypto_ccomp *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_context(NULL); + if (IS_ERR(ctx->lzo_comp_mem)) return -ENOMEM; return 0; } +static void lzo_free_context(struct crypto_ccomp *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_context(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 +80,16 @@ 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_decompress(const u8 *src, unsigned int slen, + u8 *dst, unsigned int *dlen, void *ctx) { int err; size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */ @@ -77,7 +101,12 @@ 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, NULL); } static struct crypto_alg alg = { @@ -92,14 +121,40 @@ static struct crypto_alg alg = { .coa_decompress = lzo_decompress } } }; +static struct ccomp_alg ccomp = { + .alloc_context = lzo_alloc_context, + .free_context = lzo_free_context, + .compress = __lzo_compress, + .decompress = __lzo_decompress, + .flags = CCOMP_TYPE_DECOMP_NOCTX, + .base = { + .cra_name = "lzo", + .cra_flags = CRYPTO_ALG_TYPE_CCOMPRESS, + .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_ccomp(&ccomp); + if (ret) { + crypto_unregister_alg(&alg); + return ret; + } + + return ret; } static void __exit lzo_mod_fini(void) { crypto_unregister_alg(&alg); + crypto_unregister_ccomp(&ccomp); } module_init(lzo_mod_init);