Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp2815635rwb; Mon, 15 Aug 2022 11:55:34 -0700 (PDT) X-Google-Smtp-Source: AA6agR6VYd52zsRtDM+s0Btb7QfA2V3Tebpsie6h8SJtxombmVjC6CoErOsDOCyZ+Nu9LXcNaR8s X-Received: by 2002:a17:906:ae95:b0:730:7c7d:3868 with SMTP id md21-20020a170906ae9500b007307c7d3868mr10715782ejb.564.1660589733777; Mon, 15 Aug 2022 11:55:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660589733; cv=none; d=google.com; s=arc-20160816; b=dW4d3dp4W7hDnPp+SVSTwTCl2orq+bOTWam3dZiuE7Zv7dSApSLaI5N0/M/fm5SU2P sH2ZLiGOBH87EaKm25vnZDiGwjYwWzR46u5SYrjP70wok/VMTnoVZbdD9iUr/AKqxJei RNBwUK+Uch14+sbwIIXNVmcf9Biz7xb9Lzrx3Deq/XFSp1kSI5z7aEClWYurfPvfeRcK 7xD3cfJyReti8xPp1ynOXOJSiSdduFVYOiOSg0oNsTUTfi2BzeuXjz66udxBz7tmbIes 3W0k6nv5pJpXZnvAMdPuXJcX8cei65T0PGuQUSiVJUrntCIjhg0i61IzTVxp3jG0mDbZ +X4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=ShRCxRqhwId4JMrsM26lrzxycJL6ALupj0GT1AdcSbE=; b=0h/b3xGrf+6a0EgM7ElKSKUlCOVsbBJ60DBdFUQ1aVh5KQ1eJxnDCA3UKHdmgD738J RI9PV+h3af/AUkH4aQn5rM+FdrVRO6KTo/Ows+omLtK8IKzqbigHzE/BHtX0LTYxfwLx 2xnqxGS3/hpb5w4x/l3fQeZxie4RG400Q7rgpR9Qz1pUc3mBEklX1YgIpYQC1NScrFBA sEW9GODhYuhlAfaMHZA7V0dAitXZjpUggZNDSVR0LmVl/95i4Y1/+8NGSnlH4sUQYZ6N 1+PjqvBz839KudI0s9slqMxk6sZHKgFy0IZBoJVS9pKU/OXmnmsQQFX+Cg8+AREmgBni jhzA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Vq1cGRQZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z21-20020a056402275500b0043c432626ffsi9617687edd.5.2022.08.15.11.55.04; Mon, 15 Aug 2022 11:55:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Vq1cGRQZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244447AbiHOSvL (ORCPT + 99 others); Mon, 15 Aug 2022 14:51:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244101AbiHOSqd (ORCPT ); Mon, 15 Aug 2022 14:46:33 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 92BE740BC5; Mon, 15 Aug 2022 11:28:07 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 3D0FB60FBA; Mon, 15 Aug 2022 18:28:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 89ECDC43146; Mon, 15 Aug 2022 18:28:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660588086; bh=h5vc3yvXunEp0SALg3u9XixYh+znlF9+B4f8XKf+EY0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Vq1cGRQZDP+i3O/EonRvVGUJIxfarnSoUlqmO1/fXRUJjc7Lu3Pt68yrvrUoU+UoO Vtxqe38FIaI6xlr4UjpbrjBCWF+adyI1O4yj6HK+uvtYwvoTTDKVUARUE/Q8WRGU4w wJkkOoCqYDjjwh6mxDcC/0xolCrbjXu5A25/dIQc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Corentin Labbe , Herbert Xu , Sasha Levin Subject: [PATCH 5.15 259/779] crypto: sun8i-ss - do not allocate memory when handling hash requests Date: Mon, 15 Aug 2022 19:58:23 +0200 Message-Id: <20220815180348.408876754@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180337.130757997@linuxfoundation.org> References: <20220815180337.130757997@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Corentin Labbe [ Upstream commit 8eec4563f152981a441693fc97c5459843dc5e6e ] Instead of allocate memory on each requests, it is easier to pre-allocate buffers. This made error path easier. Signed-off-by: Corentin Labbe Signed-off-by: Herbert Xu Signed-off-by: Sasha Levin --- drivers/crypto/allwinner/sun8i-ss/sun8i-ss-core.c | 10 ++++++++++ drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c | 15 +++------------ drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h | 4 ++++ 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-core.c b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-core.c index 657530578643..786b6f5cf300 100644 --- a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-core.c +++ b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-core.c @@ -486,6 +486,16 @@ static int allocate_flows(struct sun8i_ss_dev *ss) goto error_engine; } + /* the padding could be up to two block. */ + ss->flows[i].pad = devm_kmalloc(ss->dev, SHA256_BLOCK_SIZE * 2, + GFP_KERNEL | GFP_DMA); + if (!ss->flows[i].pad) + goto error_engine; + ss->flows[i].result = devm_kmalloc(ss->dev, SHA256_DIGEST_SIZE, + GFP_KERNEL | GFP_DMA); + if (!ss->flows[i].result) + goto error_engine; + ss->flows[i].engine = crypto_engine_alloc_init(ss->dev, true); if (!ss->flows[i].engine) { dev_err(ss->dev, "Cannot allocate engine\n"); diff --git a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c index ca4f280af35d..f89a580618aa 100644 --- a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c +++ b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c @@ -342,18 +342,11 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq) if (digestsize == SHA224_DIGEST_SIZE) digestsize = SHA256_DIGEST_SIZE; - /* the padding could be up to two block. */ - pad = kzalloc(algt->alg.hash.halg.base.cra_blocksize * 2, GFP_KERNEL | GFP_DMA); - if (!pad) - return -ENOMEM; + result = ss->flows[rctx->flow].result; + pad = ss->flows[rctx->flow].pad; + memset(pad, 0, algt->alg.hash.halg.base.cra_blocksize * 2); bf = (__le32 *)pad; - result = kzalloc(digestsize, GFP_KERNEL | GFP_DMA); - if (!result) { - kfree(pad); - return -ENOMEM; - } - for (i = 0; i < MAX_SG; i++) { rctx->t_dst[i].addr = 0; rctx->t_dst[i].len = 0; @@ -449,8 +442,6 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq) memcpy(areq->result, result, algt->alg.hash.halg.digestsize); theend: - kfree(pad); - kfree(result); local_bh_disable(); crypto_finalize_hash_request(engine, breq, err); local_bh_enable(); diff --git a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h index 57ada8653855..eb82ee5345ae 100644 --- a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h +++ b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h @@ -123,6 +123,8 @@ struct sginfo { * @stat_req: number of request done by this flow * @iv: list of IV to use for each step * @biv: buffer which contain the backuped IV + * @pad: padding buffer for hash operations + * @result: buffer for storing the result of hash operations */ struct sun8i_ss_flow { struct crypto_engine *engine; @@ -130,6 +132,8 @@ struct sun8i_ss_flow { int status; u8 *iv[MAX_SG]; u8 *biv; + void *pad; + void *result; #ifdef CONFIG_CRYPTO_DEV_SUN8I_SS_DEBUG unsigned long stat_req; #endif -- 2.35.1