From patchwork Wed Jan 10 20:11:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexey Romanov X-Patchwork-Id: 761974 Received: from mx1.sberdevices.ru (mx2.sberdevices.ru [45.89.224.132]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6FCFC4EB5A; Wed, 10 Jan 2024 20:12:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=salutedevices.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=salutedevices.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=salutedevices.com header.i=@salutedevices.com header.b="c7P0Fpdc" Received: from p-infra-ksmg-sc-msk02 (localhost [127.0.0.1]) by mx1.sberdevices.ru (Postfix) with ESMTP id 66B4B120022; Wed, 10 Jan 2024 23:12:35 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.sberdevices.ru 66B4B120022 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=salutedevices.com; s=mail; t=1704917555; bh=QBg0hbQyYtA3zcsQMG0ZtdzZkF6uX2vy+T0FdlBY3GM=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type:From; b=c7P0Fpdc+cSMmDaeV8DBPC1fEYqfpZWOsx4g91HJ07eYOfdh3ECizwOQ0ptVPl72r CydtzE8BW8O3IaliGcX5hKrPRrD8EtaWvjmlZKJxLPt6jvaVBD05vqU1n4XIvLa8TL wPmxm8ClWdRAaanMC8z05IgUGRH2D28DhkJdpRnQaqtK0bzIPNGtcMgOAGLEHY6zS9 FltqSAkRkJS0rbfIc/5iUMYRqFjoVwTdftBYaAghsGBSg3SX3ijiR0RDrfV22mVd3h n5+cW387ov/YBu6wZwT1OAzarhHxSQwjs+7pkxxhBJHhof5rNkgXdboGuMle0gmrqL 6FgFPAhsBCtyg== Received: from smtp.sberdevices.ru (p-i-exch-sc-m01.sberdevices.ru [172.16.192.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.sberdevices.ru (Postfix) with ESMTPS; Wed, 10 Jan 2024 23:12:35 +0300 (MSK) Received: from localhost.localdomain (100.64.160.123) by p-i-exch-sc-m01.sberdevices.ru (172.16.192.107) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 10 Jan 2024 23:12:34 +0300 From: Alexey Romanov To: , , , , , , , , , , CC: , , , , , , Jan Dakinevich , Alexey Romanov Subject: [PATCH v1 10/24] drivers: crypto: meson: avoid kzalloc in engine thread Date: Wed, 10 Jan 2024 23:11:26 +0300 Message-ID: <20240110201216.18016-11-avromanov@salutedevices.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240110201216.18016-1-avromanov@salutedevices.com> References: <20240110201216.18016-1-avromanov@salutedevices.com> Precedence: bulk X-Mailing-List: linux-crypto@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: p-i-exch-sc-m02.sberdevices.ru (172.16.192.103) To p-i-exch-sc-m01.sberdevices.ru (172.16.192.107) X-KSMG-Rule-ID: 10 X-KSMG-Message-Action: clean X-KSMG-AntiSpam-Lua-Profiles: 182544 [Jan 10 2024] X-KSMG-AntiSpam-Version: 6.1.0.3 X-KSMG-AntiSpam-Envelope-From: avromanov@salutedevices.com X-KSMG-AntiSpam-Rate: 0 X-KSMG-AntiSpam-Status: not_detected X-KSMG-AntiSpam-Method: none X-KSMG-AntiSpam-Auth: dkim=none X-KSMG-AntiSpam-Info: LuaCore: 7 0.3.7 6d6bf5bd8eea7373134f756a2fd73e9456bb7d1a, {Tracking_from_domain_doesnt_match_to}, 100.64.160.123:7.1.2; d41d8cd98f00b204e9800998ecf8427e.com:7.1.1; salutedevices.com:7.1.1; 127.0.0.199:7.1.2; smtp.sberdevices.ru:5.0.1,7.1.1, FromAlignment: s, ApMailHostAddress: 100.64.160.123 X-MS-Exchange-Organization-SCL: -1 X-KSMG-AntiSpam-Interceptor-Info: scan successful X-KSMG-AntiPhishing: Clean X-KSMG-LinksScanning: Clean X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 2.0.1.6960, bases: 2024/01/10 17:01:00 #23071477 X-KSMG-AntiVirus-Status: Clean, skipped From: Jan Dakinevich It makes no sense to allocate memory via kzalloc, we can use static buffer, speedup data processing and don't think about kfree() calls. Signed-off-by: Jan Dakinevich Signed-off-by: Alexey Romanov --- drivers/crypto/amlogic/amlogic-gxl-cipher.c | 24 ++++++++------------- drivers/crypto/amlogic/amlogic-gxl.h | 6 +++--- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/drivers/crypto/amlogic/amlogic-gxl-cipher.c b/drivers/crypto/amlogic/amlogic-gxl-cipher.c index 59b1c754d326..6ff0e088939f 100644 --- a/drivers/crypto/amlogic/amlogic-gxl-cipher.c +++ b/drivers/crypto/amlogic/amlogic-gxl-cipher.c @@ -155,8 +155,7 @@ static void meson_setup_keyiv_descs(struct cipher_ctx *ctx) return; if (blockmode == DESC_OPMODE_CBC) { - memcpy(op->key + AES_MAX_KEY_SIZE, ctx->areq->iv, ivsize); - ctx->keyiv.len = AES_MAX_KEY_SIZE + ivsize; + memcpy(op->keyiv + AES_MAX_KEY_SIZE, ctx->areq->iv, ivsize); dma_sync_single_for_device(mc->dev, ctx->keyiv.addr, ctx->keyiv.len, DMA_TO_DEVICE); } @@ -319,16 +318,16 @@ static int meson_cipher(struct skcipher_request *areq) mc->chanlist[rctx->flow].stat_req++; #endif - op->key = kzalloc(48, GFP_KERNEL | GFP_DMA); - if (!op.key) - return -ENOMEM; - - memcpy(op->key, op->key, op->keylen); ctx.keyiv.len = op->keylen; if (ctx.keyiv.len == AES_KEYSIZE_192) ctx.keyiv.len = AES_MAX_KEY_SIZE; - ctx.keyiv.addr = dma_map_single(mc->dev, op->key, ctx.keyiv.len, + if (algt->blockmode == DESC_OPMODE_CBC) { + memcpy(op->keyiv + AES_MAX_KEY_SIZE, areq->iv, ivsize); + ctx.keyiv.len = AES_MAX_KEY_SIZE + ivsize; + } + + ctx.keyiv.addr = dma_map_single(mc->dev, op->keyiv, ctx.keyiv.len, DMA_TO_DEVICE); err = dma_mapping_error(mc->dev, ctx.keyiv.addr); if (err) { @@ -366,8 +365,6 @@ static int meson_cipher(struct skcipher_request *areq) meson_unmap_scatterlist(areq, mc); theend: - kfree_sensitive(op->key); - return err; } @@ -455,7 +452,6 @@ static void meson_cipher_exit(struct crypto_tfm *tfm) { struct meson_cipher_tfm_ctx *op = crypto_tfm_ctx(tfm); - kfree_sensitive(op->key); crypto_free_skcipher(op->fallback_tfm); } @@ -479,11 +475,9 @@ static int meson_aes_setkey(struct crypto_skcipher *tfm, const u8 *key, dev_dbg(mc->dev, "ERROR: Invalid keylen %u\n", keylen); return -EINVAL; } - kfree_sensitive(op->key); + + memcpy(op->keyiv, key, keylen); op->keylen = keylen; - op->key = kmemdup(key, keylen, GFP_KERNEL | GFP_DMA); - if (!op->key) - return -ENOMEM; return crypto_skcipher_setkey(op->fallback_tfm, key, keylen); } diff --git a/drivers/crypto/amlogic/amlogic-gxl.h b/drivers/crypto/amlogic/amlogic-gxl.h index 36628a7a4cf3..2eeafdca1669 100644 --- a/drivers/crypto/amlogic/amlogic-gxl.h +++ b/drivers/crypto/amlogic/amlogic-gxl.h @@ -128,7 +128,7 @@ struct meson_cipher_req_ctx { /* * struct meson_cipher_tfm_ctx - context for a skcipher TFM * @enginectx: crypto_engine used by this TFM - * @key: pointer to key data + * @keyiv: key data * @keylen: len of the key * @keymode: The keymode(type and size of key) associated with this TFM * @mc: pointer to the private data of driver handling this TFM @@ -136,8 +136,8 @@ struct meson_cipher_req_ctx { */ struct meson_cipher_tfm_ctx { struct crypto_engine_ctx enginectx; - u32 *key; - u32 keylen; + u8 keyiv[AES_MAX_KEY_SIZE + AES_BLOCK_SIZE] ____cacheline_aligned; + u32 keylen ____cacheline_aligned; u32 keymode; struct meson_dev *mc; struct crypto_skcipher *fallback_tfm;