Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp1855341rwb; Fri, 19 Aug 2022 10:30:09 -0700 (PDT) X-Google-Smtp-Source: AA6agR5gtrx/RFPsZFjCSOkPke6hnaSyoOc8FzzzaMAmfECXuPlUrhTpkcZmOkSrObV1JtL8mvPp X-Received: by 2002:a17:90b:4b0e:b0:1fa:f531:544c with SMTP id lx14-20020a17090b4b0e00b001faf531544cmr1524713pjb.12.1660930208861; Fri, 19 Aug 2022 10:30:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660930208; cv=none; d=google.com; s=arc-20160816; b=yMS/7wZwy8doVMbido64HN7geXoXiAJ3O8FGSNSbytreeUQxk9O7bdgNBvU6tnEKb5 fSMW2Ri4fYKBgkJO+A2rplVaChYE5lTFTmQC9WYsbQ/vVIHrvW3ME/7Ppt2RKYHLfXUw K6hojgDLtdwUg6QMEvExMlbx1hLOIJztFebu0xr1J4lmZajoXxd+jXFWtTNwnIxL/gCO CXwJrwIeTyv1FblZmRwWxzOkbRUndoGcZM6beLO6hGBnS+vUDNfmU/9nwqO3hGnz9COt YzaVx0yDxN0x8GwQL+yAlnsUpeqQMW/rygpfeRje9T2oCF3371qP/jWChj+gfTFYeZUu 6VBg== 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=MBhSsphYs3xeow9A7hA76tDq4Gt9x9hjp+0xb0eoves=; b=ZyM8xWWDCOCx1c9ZXTCYeoXltiAPQwLj1BqMSzCgowY7MfOgfhZeNOFQEsRbXrBgX+ CJnKPxKOJIaXPN09BElrRczj1ABsQIm7mFTU9vAIR1xpIiuH2FkyzVRm9uhz7ghhvr32 P3l2cRLtHtn6EUWHDnuel7zXZ0btnlJLzkxYI6XmJIbq1MtVjTweavtL6LXXU6QYgtEL Fpe9RZlZl1TQz7xBxuF2j9U6vjCHTKYo788Nm9XyJUJlr8Z/oNQ0U4/PZHgvoUq6e8Xg W4i644dVLMt2GLNCGvpEmfwU2P0lmeYsMl45P2PFdJRIcb3dKMy8ftTtVdgCQuyG+853 +qYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=PwppsAZl; 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 i7-20020a1709026ac700b0016daa0c3ab5si4097056plt.406.2022.08.19.10.29.57; Fri, 19 Aug 2022 10:30:08 -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=PwppsAZl; 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 S1351626AbiHSQGK (ORCPT + 99 others); Fri, 19 Aug 2022 12:06:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35214 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351634AbiHSQCR (ORCPT ); Fri, 19 Aug 2022 12:02:17 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0590BD4186; Fri, 19 Aug 2022 08:54:10 -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 A8601616B3; Fri, 19 Aug 2022 15:54:08 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 980EEC433C1; Fri, 19 Aug 2022 15:54:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660924448; bh=vMLDaD6RIPebT8Tbxvkmwx1IRuFGPy69sWEBfpPdUpQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PwppsAZl8m3Zc3YdbMtUvyKCKntvNV4449lqzj1JnJ658Ck4uWKTwhiWj3ZT5blWB 8BU8OX5TUFkpi9yUumUJyqmb+xY7pLyXTMFVZ8F8hhRLeHyVp0Ka0ISNOdUVdqxFmI W2uHPJWFBCLdxksSLvTsEIJKheFOn92b/EgvL9A0= 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.10 173/545] crypto: sun8i-ss - do not allocate memory when handling hash requests Date: Fri, 19 Aug 2022 17:39:03 +0200 Message-Id: <20220819153837.098892242@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220819153829.135562864@linuxfoundation.org> References: <20220819153829.135562864@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 55d652cd468b..98040794acdc 100644 --- a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c +++ b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c @@ -341,18 +341,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; @@ -447,8 +440,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 49147195ecf6..a97a790ae451 100644 --- a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h +++ b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss.h @@ -122,6 +122,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; @@ -129,6 +131,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