From patchwork Tue May 23 15:34:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Meenakshi Aggarwal X-Patchwork-Id: 13252556 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4D84CC77B75 for ; Tue, 23 May 2023 15:35:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237314AbjEWPfN (ORCPT ); Tue, 23 May 2023 11:35:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237212AbjEWPfJ (ORCPT ); Tue, 23 May 2023 11:35:09 -0400 Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05on2088.outbound.protection.outlook.com [40.107.20.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 625EEC4; Tue, 23 May 2023 08:35:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DLmfTBgrxB8pE8xon5rR2KmDlSKgdqfdJ3K8fQSHw34fc3AWSf5kTzQTnLOMIucmjgB9c/IAEM+TAfBOpbasZNdgWgwEzbFpBmjUa0TYdBRD0++1wfyKNASDBdtAduADnHdR4EwEAw7mRfvfLeatXEa6oXxz31UlzI8KfXT8e0P9g7aKbC/FyBIZnB3cUQe6siccBau6twnDcUPI6rA8RsLFHWWxt8QPPkk5cbTO6MgCDT2L/vxuzipRZq4kA+X3TbTx4hsWGbMZjuCQ8BWPwiUl0pvBWoOwd5P1yd6CZ/D98Z3w63cbmUibLePLdEgz29Fdiq38neQk8I38CmscOQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=C1CRUfDyIq9V+N40XewrDJcqIvsAj8oSwwKFkjXRNJU=; b=aPcD2nIIofn0M2Nwhj8Ujycmtu3r2k76B6xM0sfC1H3nicOzlYwnbY0ABRYgeovqmiOsGJXuwX+kC9anMKmUOv3JfpZAexSAIa3THRk8E7/F+5qWxGmn19SMFXHjXUEdJXPcN85Oq1PcYNYsZ7iOuui4U42TbxO5Bn6JjVXRcyzp1zgjcRMvIpdWig+CV6elkY7zl4TX8hPib9SxUnyKSMfzmZNBWDiYjPOfmWnE3l7dW/y0PorO3SxeoV8UEugem63VQTcH/Su14Z6mmixn27/V1QYuLjAaNwlger3SvMLO9VgBrbVoXkE2AZPSM/e9N2+ik7wUpMdF7QVyBWMekg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=C1CRUfDyIq9V+N40XewrDJcqIvsAj8oSwwKFkjXRNJU=; b=lhh4BQ2zOIocRakj/UpaKbe4p3Fj9zPgP8cMDADV0kZis8u9pby1K1xq7EVFsfpUZJsyAkvMzjJ09mKTBuiX86x0JTU7NUt4RKQNOcq18Xj67Ei/cLnXsyV5GKtw20EDZNg7REo/WxPfuB8N0fVnqC22yMCsd4bF7m7YemNEouI= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from PAXPR04MB9571.eurprd04.prod.outlook.com (2603:10a6:102:24e::7) by AS8PR04MB7544.eurprd04.prod.outlook.com (2603:10a6:20b:23f::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6411.27; Tue, 23 May 2023 15:34:54 +0000 Received: from PAXPR04MB9571.eurprd04.prod.outlook.com ([fe80::b082:c033:f721:d448]) by PAXPR04MB9571.eurprd04.prod.outlook.com ([fe80::b082:c033:f721:d448%7]) with mapi id 15.20.6411.029; Tue, 23 May 2023 15:34:54 +0000 From: meenakshi.aggarwal@nxp.com To: horia.geanta@nxp.com, V.sethi@nxp.com, pankaj.gupta@nxp.com, gaurav.jain@nxp.com, herbert@gondor.apana.org.au, davem@davemloft.net, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, iuliana.prodan@nxp.com Cc: Meenakshi Aggarwal Subject: [PATCH 1/5] crypto:caam - avoid allocating memory at crypto request runtime for skcipher Date: Tue, 23 May 2023 17:34:17 +0200 Message-Id: <20230523153421.1528359-2-meenakshi.aggarwal@nxp.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230523153421.1528359-1-meenakshi.aggarwal@nxp.com> References: <20230523153421.1528359-1-meenakshi.aggarwal@nxp.com> X-ClientProxiedBy: AM4PR07CA0020.eurprd07.prod.outlook.com (2603:10a6:205:1::33) To DU0PR04MB9561.eurprd04.prod.outlook.com (2603:10a6:10:312::7) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PAXPR04MB9571:EE_|AS8PR04MB7544:EE_ X-MS-Office365-Filtering-Correlation-Id: c21474dc-ac13-446c-5ed6-08db5ba3416f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: TqCEE98vQN5MZfWRGhz3iy0Rp0wav4vnU+z+5rHWQ2Ke+xdTytQV8Ke+cHmKpQS3rp4ZMUa3zGa97jZi5OIBWjqW0ImkC1sN5sq+C3rACYw21TZGQ3KBjk57DhTVxXF4PLQV5f/mMqTawf46C/7LeHyJNdRPn0/wKSm1pG/8Ro2e2MF6ntOLXND/Tm14nLcyQjbMVQPVcDJ39wiRLHvjzn3gti922+FX/riC8SReJJj9rF5vN7l63rsq4XE4ifUwUWsb2Dfvbf5d9Mn0gti8pzKV0tDDT/q5+m1fSrYD6GeH0RkoMkcDzZ3By17nkXrl2Rmr/QtKB4ovd0R51gvrmJyTC+WX4BX/dMN2a/AuJMzokQitFjiDAVT0lAgNKTYSUC1h/Hui4vfsJ+Zvm6IsQfloX7Ap4r1oX/WOL7AXBYO6n+9uu2B4h8hU2Bc1gNE+qCB6AgxleTA3SmpKpn9kFfXnf6EfdN47i74MkmvSS96nIOx+dtWp4sLSfxnVugT7zbVOk2rBjMdlBJWz7ScrI6ZVNlR3fPgrING8ffZ5+AUtgQksFNdPAmhnOA+rszrw9CWgc1qC4RBAIGldS48w7W5x1KIaum0EV/qAeXRUuQBp1JWq5uTBEbC+byitZQZV X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PAXPR04MB9571.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(39860400002)(366004)(346002)(136003)(376002)(396003)(451199021)(38350700002)(38100700002)(36756003)(86362001)(9686003)(6512007)(6506007)(8676002)(1076003)(8936002)(2616005)(2906002)(186003)(478600001)(4326008)(316002)(5660300002)(26005)(41300700001)(6666004)(66476007)(6486002)(52116002)(66556008)(66946007)(83380400001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: KKMfb9k7VVOcmI3OjCIL7NRiRCc32iUkjt1Gwf9VbPqGqPoW6om5bg18N4D8lHSgaJdbS2QvwwC+YbP26fMNsfNqsGUzffloAx2nYAXnmb4+iw78Is16BhwKRPexa3T9M1g1U9fQck3SkkcndvnlLsUfrJ+jFlGE9PsBmgZonrXExUr2IB68UirPAQU8iZm9bzXGNn2NeXJ8o4S35kOHiwG/kEwIGKOtysGHx15sRtNPNg6619+qNuoTKDSPI4MPkx4ETEioJOptsIROlOpebD39wfrZQkqIMFZoy73WvZYnIIVjh7wZf6RiEQVZqmmq84GERhr+DTWKDC/6Uie9hZ0Chg2ttyoKfgZDN87US9khdlasN6K2jhEHVHA+cc2Vejh8bTHj+GPBugsKPIpb4VJuKxPQejyhi/aRJeoH+zYN71O/mX7RA6ZclPO8ZpDM2L/Bst3lWht6zoUks2xLkTx7LHcmvO+JQsRj+DqLmKwvmKyZurDGkJVyyxgmgMQ3x5QfS6db68R34zMB/Wzc/pUHM2k45LDnbiNkKE/4qC732oted9W5tWEAQAx2fE0Ij+Wf3fNXCEbHhGvw3Lg9d+5N9DxM3D3k/9wYz/bFznN4CPrRMMlyP7B1GBvBxqJu+Pcvlezc5xzR3d6ot3XtKlBz/5Rkpgy3FURbOiaXUhn+HH94B3HqPi6i1DaJKpr++8P4Hg5Tqc+l96XW+OGO7kGZvnI/B76HGnIdTXtyB+1gTnGRO7lyCT6hndkp4xIlhDSp+SHJt+4Ddyl9NzUbTJHX74yIoLngcHI3DRpKbLP4/VwSwBSv8Gqy78OGz9SXh+14Jev/Gwz0qmNUJAKJC4GevFZuHEmooVOvUuUeXzQRSO3Unc64uGPsMA/63FZpN5zeiiyPp9mWmZ3U91bfabBYPye0R3xkw3CI2fS1CF7YfbSWnUQwhFp0bWJjioB2wc2jbkVjM2OvvnSisVtnrrqOXtnMOnxEGlIct4oEFexcPBK3z85ER0HmalZpWfcX2fDfH2btMapm68xLusNVaZkDI8i2npYSosJX5jtb+f2u1GyCHx7TZ3p5iRdfCBjqkc2t/lwJ9SRS5Dig7t3oyz0hASN/CI0KjyGCKZxmdFBM3B/4DxWV/kj6GZ1cRwKFljTDzZrqRMw5GtsUnl52DaUkthtCGRiydjl8DvfdcR7A/LNwqOlEG72m90QN/N819FA/fopZFr6zl/UMmNkVKC9duKfKSrJkFzFay3TfcSp0YNfPG3xzo8JcSqksOGJULSzpIM/m45x74cdmWAVbZUvT2VmucFvOBqoYOQ1FojRS680WklesbJDuwfsFLFIar6mAhbeSkXGmXw1jO/7kETZg7+y9MXxSRr+Tx6Mzux75vLwfiazX34Yed7baDZ5EAy+eMMPXE+KTzs6oU6vhtb9o1b4s6/YhuXvXkEzrBppI2aqVn8378l3VE2ejOQuS4eQ97CSxn1aVErQA6662CXCSpBapx06WYyO4bbfLyYSg8+msfYUag6sAp97FuLEjoxXgB62dFWoT+sDw8u1HPv8hLM1hXIMv69zXovaR9jkSCb8MhCgx9yUpXlLB84DK/NXymmWgiofGfAf8yZL+qA== X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: c21474dc-ac13-446c-5ed6-08db5ba3416f X-MS-Exchange-CrossTenant-AuthSource: DU0PR04MB9561.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 May 2023 15:34:54.2205 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: VoeZV7vinJCU+rGszWxulC69uaHPGGh62OzAnA6jgyszKfALWPapKw284ghD8lB4491hupRIHN8dURuSQwuj0VWYD0usODs28JBpUIZktH4= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR04MB7544 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Meenakshi Aggarwal Remove CRYPTO_ALG_ALLOCATES_MEMORY flag and allocate the memory needed by the driver, to fulfil a request, within the crypto request object. The extra size needed for base extended descriptor and hw descriptor commands, link tables, IV is computed in frontend driver (caamalg) initialization and saved in reqsize field that indicates how much memory could be needed per request. CRYPTO_ALG_ALLOCATES_MEMORY flag is limited only to dm-crypt use-cases, which seems to be 4 entries maximum. Therefore in reqsize we allocate memory for maximum 4 entries for src and 1 for IV, and the same for dst, both aligned. If the driver needs more than the 4 entries maximum, the memory is dynamically allocated, at runtime. Signed-off-by: Iuliana Prodan Signed-off-by: Meenakshi Aggarwal --- drivers/crypto/caam/caamalg.c | 73 ++++++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 19 deletions(-) diff --git a/drivers/crypto/caam/caamalg.c b/drivers/crypto/caam/caamalg.c index feb86013dbf6..dbc5d5eaf695 100644 --- a/drivers/crypto/caam/caamalg.c +++ b/drivers/crypto/caam/caamalg.c @@ -911,6 +911,7 @@ struct aead_edesc { * @iv_dma: dma address of iv for checking continuity and link table * @sec4_sg_bytes: length of dma mapped sec4_sg space * @bklog: stored to determine if the request needs backlog + * @free: stored to determine if skcipher_edesc needs to be freed * @sec4_sg_dma: bus physical mapped address of h/w link table * @sec4_sg: pointer to h/w link table * @hw_desc: the h/w job descriptor followed by any referenced link tables @@ -924,6 +925,7 @@ struct skcipher_edesc { dma_addr_t iv_dma; int sec4_sg_bytes; bool bklog; + bool free; dma_addr_t sec4_sg_dma; struct sec4_sg_entry *sec4_sg; u32 hw_desc[]; @@ -1049,7 +1051,8 @@ static void skcipher_crypt_done(struct device *jrdev, u32 *desc, u32 err, DUMP_PREFIX_ADDRESS, 16, 4, req->dst, edesc->dst_nents > 1 ? 100 : req->cryptlen, 1); - kfree(edesc); + if (edesc->free) + kfree(edesc); /* * If no backlog flag, the completion of the request is done @@ -1690,20 +1693,35 @@ static struct skcipher_edesc *skcipher_edesc_alloc(struct skcipher_request *req, sec4_sg_bytes = sec4_sg_ents * sizeof(struct sec4_sg_entry); - /* - * allocate space for base edesc and hw desc commands, link tables, IV - */ + /* Check if there's enough space for edesc saved in req */ aligned_size = sizeof(*edesc) + desc_bytes + sec4_sg_bytes; aligned_size = ALIGN(aligned_size, dma_get_cache_alignment()); aligned_size += ~(ARCH_KMALLOC_MINALIGN - 1) & (dma_get_cache_alignment() - 1); aligned_size += ALIGN(ivsize, dma_get_cache_alignment()); - edesc = kzalloc(aligned_size, flags); - if (!edesc) { - dev_err(jrdev, "could not allocate extended descriptor\n"); - caam_unmap(jrdev, req->src, req->dst, src_nents, dst_nents, 0, - 0, 0, 0); - return ERR_PTR(-ENOMEM); + + if (aligned_size > (crypto_skcipher_reqsize(skcipher) - + sizeof(struct caam_skcipher_req_ctx))) { + /* + * allocate space for base edesc and hw desc commands, + * link tables, IV + */ + edesc = kzalloc(aligned_size, flags); + if (!edesc) { + caam_unmap(jrdev, req->src, req->dst, src_nents, + dst_nents, 0, 0, 0, 0); + return ERR_PTR(-ENOMEM); + } + edesc->free = true; + } else { + /* + * get address for base edesc and hw desc commands, + * link tables, IV + */ + edesc = (struct skcipher_edesc *)((u8 *)rctx + + sizeof(struct caam_skcipher_req_ctx)); + /* clear memory */ + memset(edesc, 0, sizeof(*edesc)); } edesc->src_nents = src_nents; @@ -1725,7 +1743,8 @@ static struct skcipher_edesc *skcipher_edesc_alloc(struct skcipher_request *req, dev_err(jrdev, "unable to map IV\n"); caam_unmap(jrdev, req->src, req->dst, src_nents, dst_nents, 0, 0, 0, 0); - kfree(edesc); + if (edesc->free) + kfree(edesc); return ERR_PTR(-ENOMEM); } @@ -1755,7 +1774,8 @@ static struct skcipher_edesc *skcipher_edesc_alloc(struct skcipher_request *req, dev_err(jrdev, "unable to map S/G table\n"); caam_unmap(jrdev, req->src, req->dst, src_nents, dst_nents, iv_dma, ivsize, 0, 0); - kfree(edesc); + if (edesc->free) + kfree(edesc); return ERR_PTR(-ENOMEM); } } @@ -1786,7 +1806,8 @@ static int skcipher_do_one_req(struct crypto_engine *engine, void *areq) if (ret != -EINPROGRESS) { skcipher_unmap(ctx->jrdev, rctx->edesc, req); - kfree(rctx->edesc); + if (rctx->edesc->free) + kfree(rctx->edesc); } else { ret = 0; } @@ -1863,7 +1884,8 @@ static inline int skcipher_crypt(struct skcipher_request *req, bool encrypt) if ((ret != -EINPROGRESS) && (ret != -EBUSY)) { skcipher_unmap(jrdev, edesc, req); - kfree(edesc); + if (edesc->free) + kfree(edesc); } return ret; @@ -3415,10 +3437,22 @@ static int caam_cra_init(struct crypto_skcipher *tfm) container_of(alg, typeof(*caam_alg), skcipher); struct caam_ctx *ctx = crypto_skcipher_ctx_dma(tfm); u32 alg_aai = caam_alg->caam.class1_alg_type & OP_ALG_AAI_MASK; - int ret = 0; + int ret = 0, extra_reqsize = 0; ctx->enginectx.op.do_one_request = skcipher_do_one_req; + /* + * Compute extra space needed for base edesc and + * hw desc commands, link tables, IV + */ + extra_reqsize = sizeof(struct skcipher_edesc) + + DESC_JOB_IO_LEN * CAAM_CMD_SZ + /* hw desc commands */ + /* link tables for src and dst: + * 4 entries max + 1 for IV, aligned = 8 + */ + (16 * sizeof(struct sec4_sg_entry)) + + AES_BLOCK_SIZE; /* ivsize */ + if (alg_aai == OP_ALG_AAI_XTS) { const char *tfm_name = crypto_tfm_alg_name(&tfm->base); struct crypto_skcipher *fallback; @@ -3433,9 +3467,11 @@ static int caam_cra_init(struct crypto_skcipher *tfm) ctx->fallback = fallback; crypto_skcipher_set_reqsize(tfm, sizeof(struct caam_skcipher_req_ctx) + - crypto_skcipher_reqsize(fallback)); + crypto_skcipher_reqsize(fallback) + + extra_reqsize); } else { - crypto_skcipher_set_reqsize(tfm, sizeof(struct caam_skcipher_req_ctx)); + crypto_skcipher_set_reqsize(tfm, sizeof(struct caam_skcipher_req_ctx) + + extra_reqsize); } ret = caam_init_common(ctx, &caam_alg->caam, false); @@ -3508,8 +3544,7 @@ static void caam_skcipher_alg_init(struct caam_skcipher_alg *t_alg) alg->base.cra_module = THIS_MODULE; alg->base.cra_priority = CAAM_CRA_PRIORITY; alg->base.cra_ctxsize = sizeof(struct caam_ctx) + crypto_dma_padding(); - alg->base.cra_flags |= (CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY | - CRYPTO_ALG_KERN_DRIVER_ONLY); + alg->base.cra_flags |= (CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY); alg->init = caam_cra_init; alg->exit = caam_cra_exit; From patchwork Tue May 23 15:34:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Meenakshi Aggarwal X-Patchwork-Id: 13252558 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id ECEEFC7EE26 for ; Tue, 23 May 2023 15:35:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237341AbjEWPfW (ORCPT ); Tue, 23 May 2023 11:35:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41928 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237453AbjEWPfN (ORCPT ); Tue, 23 May 2023 11:35:13 -0400 Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05on2088.outbound.protection.outlook.com [40.107.20.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 836B1139; Tue, 23 May 2023 08:35:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=iHMiKqtRkUshOyQpPQX7afFWYKVxIfviflPbTSSPGJA53HgYRUuxgLlPRMt2ZVzXr4RZWdNw2vpbRYO/Q/ocwbjmOdqOA64jIfiaBuMAemdXEwWlDQDO7haVW3gl4sVeKzgkaC4m4O6IaqqYhQwBL/6ljjTq/FdyQ4vgHX5sEEdtQOWFzQT7d1BV6ATUJAiSO3MCwpxE4BotJe4Pa0c+2ZDUKqmH9e5BNgptAOzbEQfXhwl8l07lo8lEZeq4FowiLDpwxi4Tobi+c0x/E4XpkCxKQ1mUHdVoeoTKNwZ5llO3SoxODHjE5aPBALcanZ9IyIHdK/qV6+rQ2c3oCdAMgg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=AWff3JRzv+TcPY5FBSukENdfI0kfOpnU9HCsjOMSzok=; b=QnjvlXT/cxapaT8QtUxXkjdvi19OhOgdwz+dGJxMaWe2jS7/bTDxFI9FGE91wXfX7uFOp/LgY1/+bh6zllo6z0+mF0aW1tfWbB9zJVtW6vSpimDd+qhLcbiBm6BMDrldVKyky9nMTHxizR8wojZPZYj9UJDS2JVgCNfJMRf+CimFRWHYMIzFyaocsdgTx8auUR0g40d444rgtomJ8x1jaZqo1QB8WXuZhkxmVAzz/ZyTrFgfylEHyLVXUYY/yHIcohdTBbUj61v96b25uv1TFo3K5bKmfmDvGpC35aJwhH3E3AGUVSgEEqXrmztOyFcKzyMsiXAVBDlOQxhFEJinog== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=AWff3JRzv+TcPY5FBSukENdfI0kfOpnU9HCsjOMSzok=; b=nCwtxrWJCvu5lSgjxyPHvhqhWKrraYtfSbjGzeQmMOggOEfrjLkLGwNGab7bDgApkfZKg4Yu7/vxSFoWInXJ/7r3p9BRXi3k43dv9N5KKCmonFfnFnWIgdLOf1+PTQIOfIJZwgAAD9yXzhoIuLssj2+JsrQ5lFElX2cIvLhj1Pw= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from PAXPR04MB9571.eurprd04.prod.outlook.com (2603:10a6:102:24e::7) by AS8PR04MB7544.eurprd04.prod.outlook.com (2603:10a6:20b:23f::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6411.27; Tue, 23 May 2023 15:34:55 +0000 Received: from PAXPR04MB9571.eurprd04.prod.outlook.com ([fe80::b082:c033:f721:d448]) by PAXPR04MB9571.eurprd04.prod.outlook.com ([fe80::b082:c033:f721:d448%7]) with mapi id 15.20.6411.029; Tue, 23 May 2023 15:34:55 +0000 From: meenakshi.aggarwal@nxp.com To: horia.geanta@nxp.com, V.sethi@nxp.com, pankaj.gupta@nxp.com, gaurav.jain@nxp.com, herbert@gondor.apana.org.au, davem@davemloft.net, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, iuliana.prodan@nxp.com Cc: Meenakshi Aggarwal Subject: [PATCH 2/5] crypto:caam - avoid allocating memory at crypto request runtime for aead Date: Tue, 23 May 2023 17:34:18 +0200 Message-Id: <20230523153421.1528359-3-meenakshi.aggarwal@nxp.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230523153421.1528359-1-meenakshi.aggarwal@nxp.com> References: <20230523153421.1528359-1-meenakshi.aggarwal@nxp.com> X-ClientProxiedBy: AM4PR07CA0020.eurprd07.prod.outlook.com (2603:10a6:205:1::33) To DU0PR04MB9561.eurprd04.prod.outlook.com (2603:10a6:10:312::7) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PAXPR04MB9571:EE_|AS8PR04MB7544:EE_ X-MS-Office365-Filtering-Correlation-Id: 6486cd51-944a-4884-7b74-08db5ba341f2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: drcdE8/4F0Jk5gI9Xe+kVxJez7ftDYztFLAzXsv+/ZEL/32ktFokgphnV9RRBFZr+4m6USCJjQ/4JiQQb0wCR3wc/DKSJxASo5ydqX/C20YHBOQ79RjrNYVXbQ5AWZREHiU3nkhB6HrVj78KDOkj0DQTRi0fX6Eor7WOvm96FuIJz7U0wKURgFfCRldNs6KDXYuOE/gAe18VKXmPKgbXdb/f1+64e4Qad71wMZpXKF/kzSxkyn61ZV/woP0GaLr2vzgFYvCVtRcDYUmc95/eYcbQCuTEJimpLOv1TaRzlUkVjZCiiGnHC+Scm2USucDFEmmSCtdbzLbRn0Rk0DnzSSeWPgoDtQEZC5wv84HF++XDaDBQ4SLYrLu+y+9nIN1i0w8E9L0Q0FF+HXCkN6fSiaPFCEMa2b/154HznTP7uFI7mCCJLn6onQcgZbg8KS3q2YoiToqP4mpVToSBnp6P6AB7nZ8eN+WyryY79wsoJcLr6FBTkI1EjpH7fnFIug4JQVB1Vo83uUSrtOzARcUQhf5AVsCi1bOtfmneMSd2lKHUS7lXHzLOL42h+x5QxJoSRY4V2LFwOSmTOMi401gRkB54+RYuUjuvZrGDdPVbHLX0B8KQWKOjJCWem99+4NKY X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PAXPR04MB9571.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(39860400002)(366004)(346002)(136003)(376002)(396003)(451199021)(38350700002)(38100700002)(36756003)(86362001)(9686003)(6512007)(6506007)(8676002)(1076003)(8936002)(2616005)(2906002)(186003)(478600001)(4326008)(316002)(5660300002)(26005)(41300700001)(6666004)(66476007)(6486002)(52116002)(66556008)(66946007)(83380400001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: kmfCDMEEB5jeo/DZh32pyqS2CMJ9VmuI4kkbzcXBSCyE2qayTwsg0HHdzecYhup8NpDt25/viqsuA9XWjnzUnePAHBnVIZXD0GjXsfP87jbSU3pBnILY0X0zKKOUSic1KfCpjFq41oe2wri7WTSlzW/Vgd3j4RgEHCZHYP+/RODQGzVbRdbfe/qm3vHSgjJQUXKTCR1x99fmCrFWZAbuDR4l3nzge8gv7iiQYCMLSSvBCkRRB8y54pYtJCMjyRNfKgMqjVpxZNCG9nXlWc9bGBLMmCClWt0wg+ySy4o+1KKiHPhtOXkdiqzGuejhCp8sOE/vcJmE5ooELS72KfwWIUhEr9JnnaiObLTA0mzhoK/bX6OkJTfMjpsZfQVOLxFnGX9sfcsRbRvRda1XQlglGiWZaeLsJb1FM5v0pR4dTG/dkP5HXbjNWmqembhwazhAnEM3nzKZmWuqfyghJSAkMwpUZOyuV5k+yPBDtlSKO8vjAUg/DxCQhZUMMybikoZvGxbPH6ShHqyRw9LL0JGX19TzLm5JU3757wFmFso46axy3lZwAG4TuKzGtOtzskjWBeTifLt/V1tmVgxxgnE1EaN92+n1QFiW/+IKvxEOhVshtBOfPy3J3msjy5d1v6QTnMe/cuu6lnCq7uKvV5VK4JFvumBjv9CyXH8BlCDRXmK7oeFHT2HVayo3GLdncVKk79ERLluQ+OhjXekuSxR1p5Gm1GmS9AAplJanaXlbXIm6q0FyOOA+s+F/qvtWpGMuuUEuiwnI1fWjmnc6Y8aSUpP+KcBwJ0M4RWWcBpxFE4BofQ+9er3i1B/1lUeCTWDeL7d6FWLVXKCf2H3aEi7046vRaWXeMrZ38q5sOeRaMya3iZqCMwYdEt+joSlCTj0vIQPhBHmFnKme1XXgNEV2O54F1dc/uK6d9ragmuUBBbxfRx2Sl4hNvZAXgj1RaVGxBQkvY2/a00RHYYr5prk/rlPUu9chTGkJhQvi9d1VUc4wKW3vVCct1g4returxbh+MUGZ0zEnTNIplXIUUJedaNCWRMItfAdLWnQpXeEisOl1gQuc3xff06L1waJvCiRUyz0ECjn4y5jgh+VNzqOKJvxQW9Ov+e0PRwHc3nrf4io/gq3jplFPbUofQjdmeX/OnFy02/+sJ3PsEY1Q8myaQzHlXZhnYb/v73nK3Ah3UPwswdIVGYFQWVYY73yknbZvMHGuc05ISFN+/bbzRnHLN5WpGqNTb8e3WZv7yGAO4MuYhkDPUKIs4pkVtLAwUfqdwNl7cMmhP3W9jhEI6C+gn+l1SijjGiTGl0jQUsFqqDHv4tpQckUpeTYyc37bWI6dQQjxoWSLRoWQPXdM7E06QaLDpAc+vMwT+371o4VsYbIJnM1+5WpJ4HJySyc2OP5GHBE9AjER3SEraJ5b9tlgh4LXSEEWQGas63CosIEWj2ZohNkgVS2H/gcmShUPDEWJag5FjSDyKVfCvjhjNO6YQFJJrc4Eg7Jfvcu24v6x5QIN9RRGzRox7hw8tM1IxwDMMYZIfPxVM0MnpZpEYjIQRM4XXXaNmHs1lbiRsTLTROuIyR3jCIzquAgRL/qoQLfo4Rb4FzVjmp6Tzoe9ojDZ4w== X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6486cd51-944a-4884-7b74-08db5ba341f2 X-MS-Exchange-CrossTenant-AuthSource: DU0PR04MB9561.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 May 2023 15:34:55.0336 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: H3O0g6jR2J049ywZuEhJMauvelywoDJxIlJGpCzKRYb09fQMPSvEZ3LA8ou45NQf27qqZp+J4cLB4IhZq5lMbkBcsOojuh9NyO9wOHQOX2Y= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR04MB7544 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Meenakshi Aggarwal Remove CRYPTO_ALG_ALLOCATES_MEMORY flag and allocate the memory needed by the driver, to fulfil a request, within the crypto request object. The extra size needed for base extended descriptor, hw descriptor commands and link tables is computed in frontend driver (caamalg) initialization and saved in reqsize field that indicates how much memory could be needed per request. CRYPTO_ALG_ALLOCATES_MEMORY flag is limited only to dm-crypt use-cases, which seems to be 4 entries maximum. Therefore in reqsize we allocate memory for maximum 4 entries for src and 4 for dst, aligned. If the driver needs more than the 4 entries maximum, the memory is dynamically allocated, at runtime. Signed-off-by: Iuliana Prodan Signed-off-by: Meenakshi Aggarwal --- drivers/crypto/caam/caamalg.c | 69 ++++++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 17 deletions(-) diff --git a/drivers/crypto/caam/caamalg.c b/drivers/crypto/caam/caamalg.c index dbc5d5eaf695..dea2a32f2f25 100644 --- a/drivers/crypto/caam/caamalg.c +++ b/drivers/crypto/caam/caamalg.c @@ -886,6 +886,7 @@ static int xts_skcipher_setkey(struct crypto_skcipher *skcipher, const u8 *key, * @mapped_dst_nents: number of segments in output h/w link table * @sec4_sg_bytes: length of dma mapped sec4_sg space * @bklog: stored to determine if the request needs backlog + * @free: stored to determine if aead_edesc needs to be freed * @sec4_sg_dma: bus physical mapped address of h/w link table * @sec4_sg: pointer to h/w link table * @hw_desc: the h/w job descriptor followed by any referenced link tables @@ -897,6 +898,7 @@ struct aead_edesc { int mapped_dst_nents; int sec4_sg_bytes; bool bklog; + bool free; dma_addr_t sec4_sg_dma; struct sec4_sg_entry *sec4_sg; u32 hw_desc[]; @@ -993,8 +995,8 @@ static void aead_crypt_done(struct device *jrdev, u32 *desc, u32 err, ecode = caam_jr_strstatus(jrdev, err); aead_unmap(jrdev, edesc, req); - - kfree(edesc); + if (edesc->free) + kfree(edesc); /* * If no backlog flag, the completion of the request is done @@ -1313,7 +1315,7 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req, int src_nents, mapped_src_nents, dst_nents = 0, mapped_dst_nents = 0; int src_len, dst_len = 0; struct aead_edesc *edesc; - int sec4_sg_index, sec4_sg_len, sec4_sg_bytes; + int sec4_sg_index, sec4_sg_len, sec4_sg_bytes, edesc_size = 0; unsigned int authsize = ctx->authsize; if (unlikely(req->dst != req->src)) { @@ -1393,12 +1395,30 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req, sec4_sg_bytes = sec4_sg_len * sizeof(struct sec4_sg_entry); - /* allocate space for base edesc and hw desc commands, link tables */ - edesc = kzalloc(sizeof(*edesc) + desc_bytes + sec4_sg_bytes, flags); - if (!edesc) { - caam_unmap(jrdev, req->src, req->dst, src_nents, dst_nents, 0, - 0, 0, 0); - return ERR_PTR(-ENOMEM); + /* Check if there's enough space for edesc saved in req */ + edesc_size = sizeof(*edesc) + desc_bytes + sec4_sg_bytes; + if (edesc_size > (crypto_aead_reqsize(aead) - + sizeof(struct caam_aead_req_ctx))) { + /* + * allocate space for base edesc and + * hw desc commands, link tables + */ + edesc = kzalloc(edesc_size, flags); + if (!edesc) { + caam_unmap(jrdev, req->src, req->dst, src_nents, + dst_nents, 0, 0, 0, 0); + return ERR_PTR(-ENOMEM); + } + edesc->free = true; + } else { + /* + * get address for base edesc and + * hw desc commands, link tables + */ + edesc = (struct aead_edesc *)((u8 *)rctx + + sizeof(struct caam_aead_req_ctx)); + /* clear memory */ + memset(edesc, 0, sizeof(*edesc)); } edesc->src_nents = src_nents; @@ -1431,7 +1451,8 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req, if (dma_mapping_error(jrdev, edesc->sec4_sg_dma)) { dev_err(jrdev, "unable to map S/G table\n"); aead_unmap(jrdev, edesc, req); - kfree(edesc); + if (edesc->free) + kfree(edesc); return ERR_PTR(-ENOMEM); } @@ -1461,7 +1482,8 @@ static int aead_enqueue_req(struct device *jrdev, struct aead_request *req) if ((ret != -EINPROGRESS) && (ret != -EBUSY)) { aead_unmap(jrdev, edesc, req); - kfree(rctx->edesc); + if (rctx->edesc->free) + kfree(rctx->edesc); } return ret; @@ -1552,7 +1574,8 @@ static int aead_do_one_req(struct crypto_engine *engine, void *areq) if (ret != -EINPROGRESS) { aead_unmap(ctx->jrdev, rctx->edesc, req); - kfree(rctx->edesc); + if (rctx->edesc->free) + kfree(rctx->edesc); } else { ret = 0; } @@ -3450,8 +3473,10 @@ static int caam_cra_init(struct crypto_skcipher *tfm) /* link tables for src and dst: * 4 entries max + 1 for IV, aligned = 8 */ - (16 * sizeof(struct sec4_sg_entry)) + - AES_BLOCK_SIZE; /* ivsize */ + (16 * sizeof(struct sec4_sg_entry)); + extra_reqsize = ALIGN(extra_reqsize, dma_get_cache_alignment()); + extra_reqsize += ~(ARCH_KMALLOC_MINALIGN - 1) & (dma_get_cache_alignment() - 1); + extra_reqsize += ALIGN(AES_BLOCK_SIZE, dma_get_cache_alignment()); /* ivsize */ if (alg_aai == OP_ALG_AAI_XTS) { const char *tfm_name = crypto_tfm_alg_name(&tfm->base); @@ -3487,8 +3512,19 @@ static int caam_aead_init(struct crypto_aead *tfm) struct caam_aead_alg *caam_alg = container_of(alg, struct caam_aead_alg, aead); struct caam_ctx *ctx = crypto_aead_ctx_dma(tfm); + int extra_reqsize = 0; + + /* + * Compute extra space needed for base edesc and + * hw desc commands, link tables, IV + */ + extra_reqsize = sizeof(struct aead_edesc) + + /* max size for hw desc commands */ + (AEAD_DESC_JOB_IO_LEN + CAAM_CMD_SZ * 6) + + /* link tables for src and dst, 4 entries max, aligned */ + (8 * sizeof(struct sec4_sg_entry)); - crypto_aead_set_reqsize(tfm, sizeof(struct caam_aead_req_ctx)); + crypto_aead_set_reqsize(tfm, sizeof(struct caam_aead_req_ctx) + extra_reqsize); ctx->enginectx.op.do_one_request = aead_do_one_req; @@ -3557,8 +3593,7 @@ static void caam_aead_alg_init(struct caam_aead_alg *t_alg) alg->base.cra_module = THIS_MODULE; alg->base.cra_priority = CAAM_CRA_PRIORITY; alg->base.cra_ctxsize = sizeof(struct caam_ctx) + crypto_dma_padding(); - alg->base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY | - CRYPTO_ALG_KERN_DRIVER_ONLY; + alg->base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY; alg->init = caam_aead_init; alg->exit = caam_aead_exit; From patchwork Tue May 23 15:34:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Meenakshi Aggarwal X-Patchwork-Id: 13252555 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 78239C7EE26 for ; Tue, 23 May 2023 15:35:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237481AbjEWPfM (ORCPT ); Tue, 23 May 2023 11:35:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237142AbjEWPfH (ORCPT ); Tue, 23 May 2023 11:35:07 -0400 Received: from EUR04-VI1-obe.outbound.protection.outlook.com (mail-vi1eur04on2055.outbound.protection.outlook.com [40.107.8.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5D81184; Tue, 23 May 2023 08:35:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dckue7xO8RVubcEE/pz+ENeVYe5GNn9ZAEQtzHGTQRsUjRsP8moof2/o/PZNvtm8HLllPaMCwxDwHXtU6ukTxGJl98abdN8W8Cthl1KOTaMKyOaxPLNtpQEkqrg8JSU5uorD+G4h0TO/FtDZO29T6pGCNRXt60ZPeaOexFBpuBVBXPzGf55t6JZ8ZYxEhc2ZNuIylkQjoXXocIAM4TscmhzMERB9tc2SMwHxcwHj/RoAJiaT8ABkcfewD1JCLoOfzjBNZE8WWrS+7emIKOpTnbdcB+ErNVGcRtfroB5xgiv+rClLfbHhgrLI8VhrsNA2FBU40dq7GxvXL9yxH7t7IA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=XILjacwRWzE95f3Osfm39NIwAXS8y+Ydou9mJyuZTxo=; b=nPXn2LjbKV0efh6c592FUqZJJ5jV7sX/W29vImYlRl0PkvsPdVGT0WtiKQT81ZlIdyfcXEvc8urYuExMxF3G4VTibWuT5TbqaziOlR3BfkZ/dfW8Uo/+rZxslbdWglPYLwMbqZIUEKJiuuNaNKIJOmLvmNzlIqKNV8SoZDRYiQqCM+bFfAVndyL+XscOipxKGDkfcqk7zFsOwkaNgiHw1bFLXaT7lLDj6yXyjC/p9fKNA34vAgnbaMI74RspfotRb96UjGnf5PJsisFZIXK0xJmkLubLV/csv9Eo1hHc71yFcm8xT/OKtlmLkMcWB2+5KlRX68Sw4pocl/d2Yxvufw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=XILjacwRWzE95f3Osfm39NIwAXS8y+Ydou9mJyuZTxo=; b=HKMwnN8YEoihci9nnq6HWPFFlwFn4oK7E/GBhiHJSw4fyGqXXKMrzd70uYG0m1RdDOxV+3Dm/Ahlo36CTOBqHLcP5bwbkf1pqpiC4Ha0trqy+c8tSqCGp8dCSXtOw/TVUjU7UwW5vC+owDMZfDy5EXjL7akS8V86LstSnPl8CDI= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from PAXPR04MB9571.eurprd04.prod.outlook.com (2603:10a6:102:24e::7) by AM0PR04MB7156.eurprd04.prod.outlook.com (2603:10a6:208:19b::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6411.29; Tue, 23 May 2023 15:34:57 +0000 Received: from PAXPR04MB9571.eurprd04.prod.outlook.com ([fe80::b082:c033:f721:d448]) by PAXPR04MB9571.eurprd04.prod.outlook.com ([fe80::b082:c033:f721:d448%7]) with mapi id 15.20.6411.029; Tue, 23 May 2023 15:34:55 +0000 From: meenakshi.aggarwal@nxp.com To: horia.geanta@nxp.com, V.sethi@nxp.com, pankaj.gupta@nxp.com, gaurav.jain@nxp.com, herbert@gondor.apana.org.au, davem@davemloft.net, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, iuliana.prodan@nxp.com Cc: Meenakshi Aggarwal Subject: [PATCH 3/5] crypto: caam - avoid allocating memory at crypto request runtime for hash Date: Tue, 23 May 2023 17:34:19 +0200 Message-Id: <20230523153421.1528359-4-meenakshi.aggarwal@nxp.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230523153421.1528359-1-meenakshi.aggarwal@nxp.com> References: <20230523153421.1528359-1-meenakshi.aggarwal@nxp.com> X-ClientProxiedBy: AM4PR07CA0020.eurprd07.prod.outlook.com (2603:10a6:205:1::33) To DU0PR04MB9561.eurprd04.prod.outlook.com (2603:10a6:10:312::7) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PAXPR04MB9571:EE_|AM0PR04MB7156:EE_ X-MS-Office365-Filtering-Correlation-Id: aea6391a-7570-478a-0b9e-08db5ba3426c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: U6AhXdzqH9t5WIjPm0yPkSg350Vri8yGkIm5lTEgMgRq8hP536w8n4CSHRgU+gU0rURxXkbtc4jmOncbnYXRBjCeNtPDg0F9X3sXXEDJU4N9cR6bV+98ZmhRL39IA7FTHCSM82s0qKPSvXNUCBAPxDuA/LkPgnFD/OqZrZUV6j32oxytXy0uh9ppoKKYm0D2mSo/7trlDn52d5JBh23bdL8wX1Kd+iHEDFVhIq/9s0DXDIlpjpBDRweHCpehrURm21ni3CUiuELpvPBFpQ68KxxHCoE6Rxf8OBPrGaXQgAoYyH7TTudPFJtprvCqj0L/oDnDWMdytiiGBL3gFJPLiM4ykvU6ZirZP9eOVmXD0zTcVLD7yz+RVcwCQxei6igGwiCKy0mLsqekmfOdmQhKhYg/lA7HFtuh2JvmkkFVf842Uh5Rr1isFm/7MWafsHQ/cuXF7+BTxKPg5H8MFnQmS1v2BG0QCiwrw82rw9mAN8tMSVWwra/jMcxFTkqbWnVIMX+gDeeBkzkktJqwpTiPj1MHGAqt2y432bcOrFA8dAYJwgcHCEa9WeyykFe/M5XpCiPgHVDi4UVUGA9SfAYp9m2WRROjGtORAHisQIRYodD9eEfp9I4AKgxUo+Nptag+ X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PAXPR04MB9571.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(376002)(366004)(346002)(136003)(396003)(39860400002)(451199021)(52116002)(41300700001)(66556008)(66476007)(66946007)(2906002)(26005)(1076003)(186003)(478600001)(6486002)(4326008)(316002)(6666004)(5660300002)(6512007)(8936002)(8676002)(9686003)(36756003)(2616005)(6506007)(83380400001)(86362001)(38100700002)(38350700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: N8Vx7QHsdiS+c+K94ejBIoaqQxruxUObMj1fHejyOaKrEUGl5iXe4jnqu7N+tZybkqWykGK7X6KhmkCpGYDmDvTW1U7fwfQPWiFH73Dz1E8cmuJdqaATP5DdI+WLJM3xJW5yXGS3OuLpUppQKHcmR2rTMd16aTXKpDiqqTGkCWNi2VZfsPYdZfwAeKKQl/aB4Nkxltxy9gkPuHV4s42DVWFWhbncEousUlqWBTFExtOrjqy7INw5lypPydvMyyVneXoJQvQ393xpwgMrSfZaVbXpZhMaTg9GSYpTzpqfD73a9js3YHKyUjxPjt4Wv75G6VeTsXjNE4GhDGjWgsqEYiacaC418r1iQTjHv5R3ne0+Wo+WaydycPCxe1n6j6tqfVd+VX3qUOBXURheXk4iSDkxEgoEvqs3k2GdfxHwLM6dsZHePXT1eOadVvInYacFGT75YoPDkSjg2IzxyaNKO1pmj60z83N4H4prpCpnRc1uxyOwwRPQOvUKQ8ZLBN0o1lsvxpCHWcQ5GLXmqFxHGx3IriK/LSGZF6Hazx4kIYYRuNnQ3FohcQo+4D4ImJ1FlC/qywwNZNA3OK0ZPlyggrZmLsQs8YbD5tDnekwWmLWqoX8DYf5radGhe7voDOAa3ebEwn/1zpw92q1oYJHEbTUZy5Qv6Uj8edD1iCNJjLKqFKUGIZwkbKzaLeY+7YyfBqVTpWKls1jX03eIH6Pcb9sbYYvhq1g5wIYzmPVrDV0X7aGqLBDM43xZOuQT8Vvv2pe/0hYRe1YYLi/5le5+WsVED/qK1K8G/20tDB6y51q2PCkYXcYIii08u1/4Ozy6Hc/KR/KxmDe0zlRhaID3wEUTsrtuQc4dR8AyYIMD4NzzlegZxdvc0wqOybPev/SP1SgG94fhDmmBoflDsjEgV0h4zvxZvgORbxgeEOeXeJ3Vp1u/7HMTpwSwmiDc0pU5Lvq/Y+kXUFZIiCemZOZQVsg0ltAP7wzHPgMcnTNWInhFeoLom5igd2k/y73JXFM1LgNmO9EtWl47XkEL5By4QS3U9rfVxZjLp1jRdc+ZJP+mKQhb8zMu66Ykfi5AiZBi+hggUuEGmp2tz9nwKvzyLcC9NX32NyB9QvqpOL7B0UlsLhT5Hj6s9EIWdQAqokgj1BC1EDgK0neqZjSWN4N/jj3Pp31cgEYCocXbdLeQXgP9QAjurbpgVjsd6qrGmcbdeVBgiit+AIzXBkF/NlH9nGomajQE2RWjdLHjCAPtRJLItoswz5vIgk2CMmT7Tj0eH+/7rNxUcZsSDkVxZjyRJqbwW+HN+J4j0ApqlBbz1FJYBneR1Gc2wyiSUqudWDKMsmoiPJP4/0QKdJyiGlaryj3dXDs2AQyVsFs7Xopai2ylQ4SPfYUJ5rEiFSGJR9s6LP+1eLM5HIfchrLdN1a3dceleQcCPy1aC+kZ3/m7sXVAXmTBrq8A858EmP2pf0AN4a0lEUWV25Patr8jXvbK+tKwpoWXNoi/9ykhxhmSEkS3Bhtzermb4ZpycQy0ZWJD8gHxvr/5+9pDCuz6FZexXF05yMsInaL6gR6Qn0dC8l/L25RGl4PN5i1/av4ogstEfROMjaN5YInnX6bCErQHBQ== X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: aea6391a-7570-478a-0b9e-08db5ba3426c X-MS-Exchange-CrossTenant-AuthSource: DU0PR04MB9561.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 May 2023 15:34:55.8265 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: EWfuaLYLY9NRu47e2L1OMyprbD227FL+B0EIWzCLRTYoxIxA1x/HIXMo0gLJqrWOZwNDgBTPouTBbMAxubac3G8Kt8SXJgdL+qSAYtuqXXk= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0PR04MB7156 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Meenakshi Aggarwal Remove CRYPTO_ALG_ALLOCATES_MEMORY flag and allocate the memory needed by the driver, to fulfil a request, within the crypto request object. The extra size needed for base extended descriptor and link tables is computed in frontend driver (caamhash) initialization and saved in reqsize field that indicates how much memory could be needed per request. CRYPTO_ALG_ALLOCATES_MEMORY flag is limited only to dm-crypt use-cases, which seems to be 4 entries maximum. Therefore in reqsize we allocate memory for maximum 4 entries for src and 4, aligned. If the driver needs more than the 4 entries maximum, the memory is dynamically allocated, at runtime. Signed-off-by: Iuliana Prodan Signed-off-by: Meenakshi Aggarwal --- drivers/crypto/caam/caamhash.c | 77 ++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 21 deletions(-) diff --git a/drivers/crypto/caam/caamhash.c b/drivers/crypto/caam/caamhash.c index 80deb003f0a5..75df8bca9ca9 100644 --- a/drivers/crypto/caam/caamhash.c +++ b/drivers/crypto/caam/caamhash.c @@ -535,6 +535,7 @@ static int acmac_setkey(struct crypto_ahash *ahash, const u8 *key, * @src_nents: number of segments in input scatterlist * @sec4_sg_bytes: length of dma mapped sec4_sg space * @bklog: stored to determine if the request needs backlog + * @free: stored to determine if ahash_edesc needs to be freed * @hw_desc: the h/w job descriptor followed by any referenced link tables * @sec4_sg: h/w link table */ @@ -543,6 +544,7 @@ struct ahash_edesc { int src_nents; int sec4_sg_bytes; bool bklog; + bool free; u32 hw_desc[DESC_JOB_IO_LEN_MAX / sizeof(u32)] ____cacheline_aligned; struct sec4_sg_entry sec4_sg[]; }; @@ -603,7 +605,8 @@ static inline void ahash_done_cpy(struct device *jrdev, u32 *desc, u32 err, ahash_unmap_ctx(jrdev, edesc, req, digestsize, dir); memcpy(req->result, state->caam_ctx, digestsize); - kfree(edesc); + if (edesc->free) + kfree(edesc); print_hex_dump_debug("ctx@"__stringify(__LINE__)": ", DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx, @@ -652,7 +655,8 @@ static inline void ahash_done_switch(struct device *jrdev, u32 *desc, u32 err, ecode = caam_jr_strstatus(jrdev, err); ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, dir); - kfree(edesc); + if (edesc->free) + kfree(edesc); scatterwalk_map_and_copy(state->buf, req->src, req->nbytes - state->next_buflen, @@ -703,17 +707,28 @@ static struct ahash_edesc *ahash_edesc_alloc(struct ahash_request *req, dma_addr_t sh_desc_dma) { struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); - struct caam_hash_ctx *ctx = crypto_ahash_ctx_dma(ahash); struct caam_hash_state *state = ahash_request_ctx_dma(req); gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? GFP_KERNEL : GFP_ATOMIC; struct ahash_edesc *edesc; unsigned int sg_size = sg_num * sizeof(struct sec4_sg_entry); - - edesc = kzalloc(sizeof(*edesc) + sg_size, flags); - if (!edesc) { - dev_err(ctx->jrdev, "could not allocate extended descriptor\n"); - return NULL; + int edesc_size; + + /* Check if there's enough space for edesc saved in req */ + edesc_size = sizeof(*edesc) + sg_size; + if (edesc_size > (crypto_ahash_reqsize(ahash) - + sizeof(struct caam_hash_state))) { + /* allocate space for base edesc and link tables */ + edesc = kzalloc(sizeof(*edesc) + sg_size, flags); + if (!edesc) + return ERR_PTR(-ENOMEM); + edesc->free = true; + } else { + /* get address for base edesc and link tables */ + edesc = (struct ahash_edesc *)((u8 *)state + + sizeof(struct caam_hash_state)); + /* clear memory */ + memset(edesc, 0, sizeof(*edesc)); } state->edesc = edesc; @@ -778,7 +793,8 @@ static int ahash_do_one_req(struct crypto_engine *engine, void *areq) if (ret != -EINPROGRESS) { ahash_unmap(jrdev, state->edesc, req, 0); - kfree(state->edesc); + if (state->edesc->free) + kfree(state->edesc); } else { ret = 0; } @@ -813,7 +829,8 @@ static int ahash_enqueue_req(struct device *jrdev, if ((ret != -EINPROGRESS) && (ret != -EBUSY)) { ahash_unmap_ctx(jrdev, edesc, req, dst_len, dir); - kfree(edesc); + if (edesc->free) + kfree(edesc); } return ret; @@ -941,7 +958,8 @@ static int ahash_update_ctx(struct ahash_request *req) return ret; unmap_ctx: ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_BIDIRECTIONAL); - kfree(edesc); + if (edesc->free) + kfree(edesc); return ret; } @@ -1002,7 +1020,8 @@ static int ahash_final_ctx(struct ahash_request *req) digestsize, DMA_BIDIRECTIONAL); unmap_ctx: ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_BIDIRECTIONAL); - kfree(edesc); + if (edesc->free) + kfree(edesc); return ret; } @@ -1076,7 +1095,8 @@ static int ahash_finup_ctx(struct ahash_request *req) digestsize, DMA_BIDIRECTIONAL); unmap_ctx: ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_BIDIRECTIONAL); - kfree(edesc); + if (edesc->free) + kfree(edesc); return ret; } @@ -1125,7 +1145,8 @@ static int ahash_digest(struct ahash_request *req) req->nbytes); if (ret) { ahash_unmap(jrdev, edesc, req, digestsize); - kfree(edesc); + if (edesc->free) + kfree(edesc); return ret; } @@ -1134,7 +1155,8 @@ static int ahash_digest(struct ahash_request *req) ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize); if (ret) { ahash_unmap(jrdev, edesc, req, digestsize); - kfree(edesc); + if (edesc->free) + kfree(edesc); return -ENOMEM; } @@ -1191,7 +1213,8 @@ static int ahash_final_no_ctx(struct ahash_request *req) digestsize, DMA_FROM_DEVICE); unmap: ahash_unmap(jrdev, edesc, req, digestsize); - kfree(edesc); + if (edesc->free) + kfree(edesc); return -ENOMEM; } @@ -1312,7 +1335,8 @@ static int ahash_update_no_ctx(struct ahash_request *req) return ret; unmap_ctx: ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_TO_DEVICE); - kfree(edesc); + if (edesc->free) + kfree(edesc); return ret; } @@ -1387,7 +1411,8 @@ static int ahash_finup_no_ctx(struct ahash_request *req) digestsize, DMA_FROM_DEVICE); unmap: ahash_unmap(jrdev, edesc, req, digestsize); - kfree(edesc); + if (edesc->free) + kfree(edesc); return -ENOMEM; } @@ -1495,7 +1520,8 @@ static int ahash_update_first(struct ahash_request *req) return ret; unmap_ctx: ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, DMA_TO_DEVICE); - kfree(edesc); + if (edesc->free) + kfree(edesc); return ret; } @@ -1782,6 +1808,7 @@ static int caam_hash_cra_init(struct crypto_tfm *tfm) sh_desc_update); dma_addr_t dma_addr; struct caam_drv_private *priv; + int extra_reqsize = 0; /* * Get a Job ring from Job Ring driver to ensure in-order @@ -1862,7 +1889,15 @@ static int caam_hash_cra_init(struct crypto_tfm *tfm) ctx->enginectx.op.do_one_request = ahash_do_one_req; - crypto_ahash_set_reqsize_dma(ahash, sizeof(struct caam_hash_state)); + /* Compute extra space needed for base edesc and link tables */ + extra_reqsize = sizeof(struct ahash_edesc) + + /* link tables for src: + * 4 entries max + max 2 for remaining buf, aligned = 8 + */ + (8 * sizeof(struct sec4_sg_entry)); + + crypto_ahash_set_reqsize_dma(ahash, + sizeof(struct caam_hash_state) + extra_reqsize); /* * For keyed hash algorithms shared descriptors @@ -1937,7 +1972,7 @@ caam_hash_alloc(struct caam_hash_template *template, alg->cra_priority = CAAM_CRA_PRIORITY; alg->cra_blocksize = template->blocksize; alg->cra_alignmask = 0; - alg->cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY; + alg->cra_flags = CRYPTO_ALG_ASYNC; t_alg->alg_type = template->alg_type; From patchwork Tue May 23 15:34:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Meenakshi Aggarwal X-Patchwork-Id: 13252557 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EC8B4C7EE2F for ; Tue, 23 May 2023 15:35:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237499AbjEWPfN (ORCPT ); Tue, 23 May 2023 11:35:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237199AbjEWPfI (ORCPT ); Tue, 23 May 2023 11:35:08 -0400 Received: from EUR02-AM0-obe.outbound.protection.outlook.com (mail-am0eur02on2086.outbound.protection.outlook.com [40.107.247.86]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4E9E3188; Tue, 23 May 2023 08:35:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jvtksHHOOwk7BYineddBxlo7oWb8WdGuqNRk+7wYGBjbmwkSZlxzJpjAbfyVMuQv2gXzPuT9ju6gzgYQvbcw35DmoHN/vbWtMVwhVcxqJiWpOiQLm273/4Cr8ZPnNpMVImxbnbaHbjRMMyBDsWHCwznPwmwi8wi/QXix1Qk0PJ9IuOQzhUYbqoZ/5wOLMk0dBfVukMLBSxb3q4ZRpT5NbXoiNHuyEuAogVP4lyIDUv2isOn+euQ7XzwwypDubLjOUWbX+pfTG8QySmKxJFhBsP1avJuvgG6d+eN2Cp6nqN1bsvAYh4zYpsa6wqizE4sp++D+S8zVeRuz9QZQ5XGCRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=lgnsORy7pN77TaNeDUoifgr/9zb/l8eEG+nlOdK/89E=; b=bFtFVkBIrnLtKd9T3jkTPyMk3UKqZPGQFT9B75dZ0a1X5Fy00gXSxdev3x4bzdmWUoBq9JgpJprrbUcQZZDSrgdxxqh1PneX64gBLY9OJGpLAD7jknfqkZcEb7hS+x6dUyCr5PBW5ypcLjvj68AzIJp8soPRYNfFuOpOXaIQHEC1Hukb4CSN0e9wgNA8Ia1/W+HAhigk+uP0pYll9cO4wH0Y7qp9P8cu8/4x6vteWxdB318aZ1xMpN/SjfD3HukMdk8Ted/4ZY4MZV9BtbW4pVwRJZKerYKE+94buKT3i/H4IiWFEN/KA7ZNxyowcMzwewIiVgyUT9Ku8aQSB3nsWQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=lgnsORy7pN77TaNeDUoifgr/9zb/l8eEG+nlOdK/89E=; b=eTOO6pYt4mNIUPu5Z6rjQCli2mkpQrQ7/NQGqqIe2kO4EltuyWrC/niRFQ1ZX3d/PneKUmFCnHSm3VUbSjlYJwknCjtm6eb+x84BgVNWQdZHpAvkB54O/02+dlqBpo5hq6GLVLVxmpQYmD9jBqOk/+Z6mIFscqREqnasp4A5YlA= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from PAXPR04MB9571.eurprd04.prod.outlook.com (2603:10a6:102:24e::7) by AM9PR04MB8586.eurprd04.prod.outlook.com (2603:10a6:20b:439::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6411.29; Tue, 23 May 2023 15:34:58 +0000 Received: from PAXPR04MB9571.eurprd04.prod.outlook.com ([fe80::b082:c033:f721:d448]) by PAXPR04MB9571.eurprd04.prod.outlook.com ([fe80::b082:c033:f721:d448%7]) with mapi id 15.20.6411.029; Tue, 23 May 2023 15:34:58 +0000 From: meenakshi.aggarwal@nxp.com To: horia.geanta@nxp.com, V.sethi@nxp.com, pankaj.gupta@nxp.com, gaurav.jain@nxp.com, herbert@gondor.apana.org.au, davem@davemloft.net, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, iuliana.prodan@nxp.com Cc: Meenakshi Aggarwal Subject: [PATCH 4/5] crypto: caam/qi - avoid allocating memory at crypto request runtime Date: Tue, 23 May 2023 17:34:20 +0200 Message-Id: <20230523153421.1528359-5-meenakshi.aggarwal@nxp.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230523153421.1528359-1-meenakshi.aggarwal@nxp.com> References: <20230523153421.1528359-1-meenakshi.aggarwal@nxp.com> X-ClientProxiedBy: AM4PR07CA0020.eurprd07.prod.outlook.com (2603:10a6:205:1::33) To DU0PR04MB9561.eurprd04.prod.outlook.com (2603:10a6:10:312::7) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PAXPR04MB9571:EE_|AM9PR04MB8586:EE_ X-MS-Office365-Filtering-Correlation-Id: 733ae638-9d5e-45e2-9d85-08db5ba342e5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: xbT9PmqqFSk1gx43TNIxU1eKaSDsehbBj7CiMny1k8WnMHVz1MK0TMy96csWqdwlsSjY0Mi4vGgeRCSCZiDMC6qCGJQLMlCSaJ/pFvthSgLOUQOlXgASQiO4QxMlQg6fxlE9IDdr5MpzTXkLjCStjD4eIHS/2p6DJZrVjT9ekGseO5QjUKvrtZ/KSBBXmHhdJoJqF52IUuaE4GVw76wQZJCj1xLp0Nm+Lfykwy66mEy2evsbTDDY0EN1UyzZ5AN0NcDBKDXRMOgmoFGxqJEheyUUeNqJkDtBWjVY2ygLZHOeAevYdwYpq9oxtHZysPkhXdNHgVSNW8wBOG9EkUFblx+I883HGVdSl0uZupFAc0+5SN8r2Gm7SQ14t91Z49SP3Rf+pTgNafBmwW44KXVS33vQtpaOxQLgj6nw13Cg4GBvDnvoUHXtJHiWUxJL0nPiEkgjKUH1xd/v3ucs+Ct4wnDBtAZK9mxuddX73Nnzo4xajnpV+FnrKI7kE59hRqaKvmMxi7MI1IcLtc4r6DYBhQsmWoUeQZOvg/GLTRGInLwISH6Gsw/ZhU/KkZmkEfG3ZoGH6Xl+W/QO2b7MySm1qeApDdRNW0HbrRt/CzkvARZBml91Z8ciosZdyNnI5a5a X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PAXPR04MB9571.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(136003)(346002)(366004)(376002)(396003)(39860400002)(451199021)(41300700001)(6486002)(52116002)(6666004)(316002)(26005)(9686003)(6506007)(6512007)(1076003)(5660300002)(36756003)(8936002)(8676002)(38100700002)(38350700002)(478600001)(86362001)(2616005)(83380400001)(30864003)(186003)(66476007)(66556008)(66946007)(2906002)(4326008);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: RgaWkAX+B5EyVsQU1ivQYM4mZt6nGUiivpFVu27lknkBBcyfyyxleRyIe5diH9KF07ls9IZsQNEC4YYngwoGvacgIWakx9cjw2adW0UNijRJgEIDYMG/2rm2vzvV45judwJ3Y8rpftnY1C+wzyO8P8ZgT6uatHY20uyagQlgoUb1xdqRLq8tWXtJYiO6zKduoYqd9U8m02UUXLdsgPIf13CGnwYToIwKuIDsqemFT0SbJkdAd648c+hN02yftlSnX1aMZR7AEDzcNEd+13amt4BhmMknGyX4NijAT8TIVW1MRwvXpFM0J1ttDsBB6zd22K5gaDTovphBgh9lKT/6+1xlfkJmPIFgZMAqwp44TIHGs5t/4vVGJXxoTQcB4hz7rtcKHPtbtQrOZ/TCCztpSPrWDYXvlYXAyjvib35EaBy6KkG27qOnJvjvbHniEQvk4zq8jNYMZFoXeiihRab33M5NGoC2tGdbGbI5xRU+iFCbF0HlmxuaYpHUBPPn6a3N/Ol/3So/h7Q2oAJ8mm+zOMwszl8EiVTsilhBeJHI7qDKgLpalrdSw2vCVJIAC01Oh0/m5EiNRcOwH+HGDdVavg2uMTbNufQbOhkEBggtCJc0N2o+3iwEFh06h0l6FddUMcyxlnYiSRvie5tAA3AO29Bp5UCDEOHOsiYWSnJULzX8UZ+5RX2cczxYrcI58L9cM1J87Ya9fzyB9FgdD6yiW5WyUbsEO8GBsbGpWmHFVCg2M9W+W/vEH8H3Ia4MeZrjJrOhKDrp/6dK5HxgmxULTj9mTZPIqjfOI8UTT3Y7pfd8zEhhx6DJXnQTo6k/d5vnjozjezhwJXWCrQZ1ozq/4NZaVonas3/gno44h5lA67bgUhD7WEjWAdA7phnnHHE8hrLBaB1+bn1EqGwZ8ct+hYtFgGF9NSMKcYiTBb1P6gqzVccz6U1jxHpkZpdaAEc0KojApbESulbTb9QFjNF26RMk/osmcOtKoqySBvsCKif9agNewcoCFrIX2wpDVkfxjGnddyW9jhFpwZdXMaCzTmhfuSagI441EW1cl/b+WT2B8/3g/Ufq59HNHAbh4i2BRCxrL25TLangLhbyQ8p0D8X7rb9u7sc2/fEaG+G7viOpfJHqos+SX7pUYaSTyqQX07VcpK+qZuk9cds0bhFcP0wtfZp6edthf0wqxC7bcABy/4mPbguvNUhWymVPH1gcr4j4oqMCHWL6Ozn3auSoRzbBEg5EdLkYq2toNvsn5WFLyZ4TW86whEhdPWXcNW9mTC72vO+vAN0360Vp+sqtWA5Xbg+LGxjYIIYT6FzieTdPsrQMDCNO8p/3/ZTi+a4f1KvrUSqGLJ+iPzWfGYb6/Kf0hBLPRn1x7gGTOhDwy2ZndWdiY05SXrGD0iJXpGwpZqks2EG3K26NUODcO1M8kWMRaY8OjQIfJQMIvnK+vINljfJ9mDuH+YrDRZjQgIn1Yj7sjD5UXZAsQEyNyl1+unP0BUBqKZAmnXb7sx7+SKVMIA4zn6Fa9ar9YpU6GptJofnecbofhnMdBNblbR5dhfgA7udrrgMfqUcuZykGtv/kuOl07iVaYT3/6KDxEgomX5x+GPuJvaN5hk41ALNcSQ== X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 733ae638-9d5e-45e2-9d85-08db5ba342e5 X-MS-Exchange-CrossTenant-AuthSource: DU0PR04MB9561.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 May 2023 15:34:56.6542 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: W18hR2n7dZ8cY/C1OiBrS84QKVb9ei0Lb1J7L2lHRne88H58hGgg/bwSi8Me/7gOy4f2tejf4M184OL635v9eP8FwtlGh+jeoa+UlpI1GNQ= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM9PR04MB8586 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Meenakshi Aggarwal Remove CRYPTO_ALG_ALLOCATES_MEMORY flag and allocate the memory needed by the driver, to fulfil a request, within the crypto request object. The extra size needed for base extended descriptor, hw descriptor commands and link tables is computed in frontend driver (caamalg_qi) initialization and saved in reqsize field that indicates how much memory could be needed per request. CRYPTO_ALG_ALLOCATES_MEMORY flag is limited only to dm-crypt use-cases, which seems to be 4 entries maximum. Therefore in reqsize we allocate memory for maximum 4 entries for src and 4 for dst, aligned. If the driver needs more than the 4 entries maximum, the memory is dynamically allocated, at runtime. Signed-off-by: Iuliana Prodan Signed-off-by: Meenakshi Aggarwal --- drivers/crypto/caam/caamalg_qi.c | 131 +++++++++++++++++++++---------- 1 file changed, 89 insertions(+), 42 deletions(-) diff --git a/drivers/crypto/caam/caamalg_qi.c b/drivers/crypto/caam/caamalg_qi.c index 743ce50c14f2..d7078f37ef9f 100644 --- a/drivers/crypto/caam/caamalg_qi.c +++ b/drivers/crypto/caam/caamalg_qi.c @@ -793,6 +793,7 @@ static int xts_skcipher_setkey(struct crypto_skcipher *skcipher, const u8 *key, * @dst_nents: number of segments in output scatterlist * @iv_dma: dma address of iv for checking continuity and link table * @qm_sg_bytes: length of dma mapped h/w link table + * @free: stored to determine if aead_edesc needs to be freed * @qm_sg_dma: bus physical mapped address of h/w link table * @assoclen: associated data length, in CAAM endianness * @assoclen_dma: bus physical mapped address of req->assoclen @@ -804,6 +805,7 @@ struct aead_edesc { int dst_nents; dma_addr_t iv_dma; int qm_sg_bytes; + bool free; dma_addr_t qm_sg_dma; unsigned int assoclen; dma_addr_t assoclen_dma; @@ -817,6 +819,7 @@ struct aead_edesc { * @dst_nents: number of segments in output scatterlist * @iv_dma: dma address of iv for checking continuity and link table * @qm_sg_bytes: length of dma mapped h/w link table + * @free: stored to determine if skcipher_edesc needs to be freed * @qm_sg_dma: bus physical mapped address of h/w link table * @drv_req: driver-specific request structure * @sgt: the h/w link table, followed by IV @@ -826,6 +829,7 @@ struct skcipher_edesc { int dst_nents; dma_addr_t iv_dma; int qm_sg_bytes; + bool free; dma_addr_t qm_sg_dma; struct caam_drv_req drv_req; struct qm_sg_entry sgt[]; @@ -932,7 +936,8 @@ static void aead_done(struct caam_drv_req *drv_req, u32 status) aead_unmap(qidev, edesc, aead_req); aead_request_complete(aead_req, ecode); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); } /* @@ -954,7 +959,7 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req, dma_addr_t qm_sg_dma, iv_dma = 0; int ivsize = 0; unsigned int authsize = ctx->authsize; - int qm_sg_index = 0, qm_sg_ents = 0, qm_sg_bytes; + int qm_sg_index = 0, qm_sg_ents = 0, qm_sg_bytes, edesc_size = 0; int in_len, out_len; struct qm_sg_entry *sg_table, *fd_sgt; struct caam_drv_ctx *drv_ctx; @@ -963,13 +968,6 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req, if (IS_ERR(drv_ctx)) return (struct aead_edesc *)drv_ctx; - /* allocate space for base edesc and hw desc commands, link tables */ - edesc = qi_cache_alloc(flags); - if (unlikely(!edesc)) { - dev_err(qidev, "could not allocate extended descriptor\n"); - return ERR_PTR(-ENOMEM); - } - if (likely(req->src == req->dst)) { src_len = req->assoclen + req->cryptlen + (encrypt ? authsize : 0); @@ -978,7 +976,6 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req, if (unlikely(src_nents < 0)) { dev_err(qidev, "Insufficient bytes (%d) in src S/G\n", src_len); - qi_cache_free(edesc); return ERR_PTR(src_nents); } @@ -986,7 +983,6 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req, DMA_BIDIRECTIONAL); if (unlikely(!mapped_src_nents)) { dev_err(qidev, "unable to map source\n"); - qi_cache_free(edesc); return ERR_PTR(-ENOMEM); } } else { @@ -997,7 +993,6 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req, if (unlikely(src_nents < 0)) { dev_err(qidev, "Insufficient bytes (%d) in src S/G\n", src_len); - qi_cache_free(edesc); return ERR_PTR(src_nents); } @@ -1005,7 +1000,6 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req, if (unlikely(dst_nents < 0)) { dev_err(qidev, "Insufficient bytes (%d) in dst S/G\n", dst_len); - qi_cache_free(edesc); return ERR_PTR(dst_nents); } @@ -1014,7 +1008,6 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req, src_nents, DMA_TO_DEVICE); if (unlikely(!mapped_src_nents)) { dev_err(qidev, "unable to map source\n"); - qi_cache_free(edesc); return ERR_PTR(-ENOMEM); } } else { @@ -1029,7 +1022,6 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req, dev_err(qidev, "unable to map destination\n"); dma_unmap_sg(qidev, req->src, src_nents, DMA_TO_DEVICE); - qi_cache_free(edesc); return ERR_PTR(-ENOMEM); } } else { @@ -1061,18 +1053,35 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req, else qm_sg_ents = pad_sg_nents(qm_sg_ents); - sg_table = &edesc->sgt[0]; qm_sg_bytes = qm_sg_ents * sizeof(*sg_table); - if (unlikely(offsetof(struct aead_edesc, sgt) + qm_sg_bytes + ivsize > - CAAM_QI_MEMCACHE_SIZE)) { + + /* Check if there's enough space for edesc saved in req */ + edesc_size = offsetof(struct aead_edesc, sgt) + qm_sg_bytes + ivsize; + if (unlikely(edesc_size > CAAM_QI_MEMCACHE_SIZE)) { dev_err(qidev, "No space for %d S/G entries and/or %dB IV\n", qm_sg_ents, ivsize); caam_unmap(qidev, req->src, req->dst, src_nents, dst_nents, 0, 0, DMA_NONE, 0, 0); - qi_cache_free(edesc); return ERR_PTR(-ENOMEM); + } else if (edesc_size > crypto_aead_reqsize(aead)) { + /* allocate space for base edesc, link tables and IV */ + edesc = qi_cache_alloc(flags); + if (unlikely(!edesc)) { + dev_err(qidev, "could not allocate extended descriptor\n"); + caam_unmap(qidev, req->src, req->dst, src_nents, + dst_nents, 0, 0, DMA_NONE, 0, 0); + return ERR_PTR(-ENOMEM); + } + edesc->free = true; + } else { + /* get address for base edesc, link tables and IV */ + edesc = (struct aead_edesc *)((u8 *)aead_request_ctx(req)); + /* clear memory */ + memset(edesc, 0, sizeof(*edesc)); } + sg_table = &edesc->sgt[0]; + if (ivsize) { u8 *iv = (u8 *)(sg_table + qm_sg_ents); @@ -1084,7 +1093,8 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req, dev_err(qidev, "unable to map IV\n"); caam_unmap(qidev, req->src, req->dst, src_nents, dst_nents, 0, 0, DMA_NONE, 0, 0); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); return ERR_PTR(-ENOMEM); } } @@ -1103,7 +1113,8 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req, dev_err(qidev, "unable to map assoclen\n"); caam_unmap(qidev, req->src, req->dst, src_nents, dst_nents, iv_dma, ivsize, DMA_TO_DEVICE, 0, 0); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); return ERR_PTR(-ENOMEM); } @@ -1125,7 +1136,8 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req, dma_unmap_single(qidev, edesc->assoclen_dma, 4, DMA_TO_DEVICE); caam_unmap(qidev, req->src, req->dst, src_nents, dst_nents, iv_dma, ivsize, DMA_TO_DEVICE, 0, 0); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); return ERR_PTR(-ENOMEM); } @@ -1179,7 +1191,8 @@ static inline int aead_crypt(struct aead_request *req, bool encrypt) ret = -EINPROGRESS; } else { aead_unmap(ctx->qidev, edesc, req); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); } return ret; @@ -1247,7 +1260,8 @@ static void skcipher_done(struct caam_drv_req *drv_req, u32 status) if (!ecode) memcpy(req->iv, skcipher_edesc_iv(edesc), ivsize); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); skcipher_request_complete(req, ecode); } @@ -1333,21 +1347,28 @@ static struct skcipher_edesc *skcipher_edesc_alloc(struct skcipher_request *req, len = ALIGN(len, dma_get_cache_alignment()); len += ivsize; + /* Check if there's enough space for edesc saved in req */ if (unlikely(len > CAAM_QI_MEMCACHE_SIZE)) { dev_err(qidev, "No space for %d S/G entries and/or %dB IV\n", qm_sg_ents, ivsize); caam_unmap(qidev, req->src, req->dst, src_nents, dst_nents, 0, 0, DMA_NONE, 0, 0); return ERR_PTR(-ENOMEM); - } - - /* allocate space for base edesc, link tables and IV */ - edesc = qi_cache_alloc(flags); - if (unlikely(!edesc)) { - dev_err(qidev, "could not allocate extended descriptor\n"); - caam_unmap(qidev, req->src, req->dst, src_nents, dst_nents, 0, - 0, DMA_NONE, 0, 0); - return ERR_PTR(-ENOMEM); + } else if (len > crypto_skcipher_reqsize(skcipher)) { + /* allocate space for base edesc, link tables and IV */ + edesc = qi_cache_alloc(flags); + if (unlikely(!edesc)) { + dev_err(qidev, "could not allocate extended descriptor\n"); + caam_unmap(qidev, req->src, req->dst, src_nents, dst_nents, 0, + 0, DMA_NONE, 0, 0); + return ERR_PTR(-ENOMEM); + } + edesc->free = true; + } else { + /* get address for base edesc, link tables and IV */ + edesc = (struct skcipher_edesc *)((u8 *)skcipher_request_ctx(req)); + /* clear memory */ + memset(edesc, 0, sizeof(*edesc)); } edesc->src_nents = src_nents; @@ -1367,7 +1388,8 @@ static struct skcipher_edesc *skcipher_edesc_alloc(struct skcipher_request *req, dev_err(qidev, "unable to map IV\n"); caam_unmap(qidev, req->src, req->dst, src_nents, dst_nents, 0, 0, DMA_NONE, 0, 0); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); return ERR_PTR(-ENOMEM); } @@ -1388,7 +1410,8 @@ static struct skcipher_edesc *skcipher_edesc_alloc(struct skcipher_request *req, dev_err(qidev, "unable to map S/G table\n"); caam_unmap(qidev, req->src, req->dst, src_nents, dst_nents, iv_dma, ivsize, DMA_BIDIRECTIONAL, 0, 0); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); return ERR_PTR(-ENOMEM); } @@ -1462,7 +1485,8 @@ static inline int skcipher_crypt(struct skcipher_request *req, bool encrypt) ret = -EINPROGRESS; } else { skcipher_unmap(ctx->qidev, edesc, req); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); } return ret; @@ -2509,7 +2533,16 @@ static int caam_cra_init(struct crypto_skcipher *tfm) container_of(alg, typeof(*caam_alg), skcipher); struct caam_ctx *ctx = crypto_skcipher_ctx_dma(tfm); u32 alg_aai = caam_alg->caam.class1_alg_type & OP_ALG_AAI_MASK; - int ret = 0; + int ret = 0, extra_reqsize = 0; + + /* Compute extra space needed for base edesc, link tables and IV */ + extra_reqsize = sizeof(struct skcipher_edesc) + + /* link tables for src and dst: + * 4 entries max + 1 for IV, aligned = 8 + */ + 16 * sizeof(struct qm_sg_entry); + extra_reqsize += ALIGN(extra_reqsize, dma_get_cache_alignment()); + extra_reqsize += AES_BLOCK_SIZE; /* ivsize */ if (alg_aai == OP_ALG_AAI_XTS) { const char *tfm_name = crypto_tfm_alg_name(&tfm->base); @@ -2525,7 +2558,10 @@ static int caam_cra_init(struct crypto_skcipher *tfm) ctx->fallback = fallback; crypto_skcipher_set_reqsize(tfm, sizeof(struct caam_skcipher_req_ctx) + - crypto_skcipher_reqsize(fallback)); + crypto_skcipher_reqsize(fallback) + + extra_reqsize); + } else { + crypto_skcipher_set_reqsize(tfm, extra_reqsize); } ret = caam_init_common(ctx, &caam_alg->caam, false); @@ -2541,6 +2577,19 @@ static int caam_aead_init(struct crypto_aead *tfm) struct caam_aead_alg *caam_alg = container_of(alg, typeof(*caam_alg), aead); struct caam_ctx *ctx = crypto_aead_ctx_dma(tfm); + int extra_reqsize = 0; + + /* Compute extra space needed for base edesc, link tables and IV */ + extra_reqsize = sizeof(struct aead_edesc) + + /* link tables for src and dst: + * 4 entries max + 1 for IV, aligned = 8 + */ + (16 * sizeof(struct qm_sg_entry)) + + AES_BLOCK_SIZE; /* ivsize */ + /* + * Set the size for the space needed for base edesc, link tables, IV + */ + crypto_aead_set_reqsize(tfm, extra_reqsize); return caam_init_common(ctx, &caam_alg->caam, !caam_alg->caam.nodkp); } @@ -2596,8 +2645,7 @@ static void caam_skcipher_alg_init(struct caam_skcipher_alg *t_alg) alg->base.cra_module = THIS_MODULE; alg->base.cra_priority = CAAM_CRA_PRIORITY; alg->base.cra_ctxsize = sizeof(struct caam_ctx) + crypto_dma_padding(); - alg->base.cra_flags |= (CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY | - CRYPTO_ALG_KERN_DRIVER_ONLY); + alg->base.cra_flags |= (CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY); alg->init = caam_cra_init; alg->exit = caam_cra_exit; @@ -2610,8 +2658,7 @@ static void caam_aead_alg_init(struct caam_aead_alg *t_alg) alg->base.cra_module = THIS_MODULE; alg->base.cra_priority = CAAM_CRA_PRIORITY; alg->base.cra_ctxsize = sizeof(struct caam_ctx) + crypto_dma_padding(); - alg->base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY | - CRYPTO_ALG_KERN_DRIVER_ONLY; + alg->base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY; alg->init = caam_aead_init; alg->exit = caam_aead_exit; From patchwork Tue May 23 15:34:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Meenakshi Aggarwal X-Patchwork-Id: 13252559 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EC346C77B75 for ; Tue, 23 May 2023 15:35:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237598AbjEWPfc (ORCPT ); Tue, 23 May 2023 11:35:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41916 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237473AbjEWPfN (ORCPT ); Tue, 23 May 2023 11:35:13 -0400 Received: from EUR02-AM0-obe.outbound.protection.outlook.com (mail-am0eur02on2086.outbound.protection.outlook.com [40.107.247.86]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1DEB2126; Tue, 23 May 2023 08:35:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VWS0v4FTnV/k5snWDChS6n0nSe7FNaaHDVbPyuXsB7T4GldFPXDN7tgu3Bp6QqymTGbaol0vClz3EB38LuLkVdsZI+H3WUtIyI9nlxz93SfWS0pnjGAc+KiOCOZao93PL3AYys/eeQMZIUj9BK9yQRYVlGCzqdw6wPo8ywlxdRYhnQVDqR7S23muDJhqkCTDgVGlLQLNzEE61Hu9fEZkyODJrnz+3d4FXZWbhm5D6IIGsJZHmhtIRTZ5/ITY7An/uP6OL8px7LZwrApya2bfXiiKWl3mqcqmMMYy7i+Xn32vglKes1WSRDnlMwTs3dov+lmOTdRh0jsYYgSp65ymBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=HwLFTO49hOmOtrrgW7Di3P+CrgGZZnLOsuy0+ujom3Y=; b=gqbKtjHV97a5HX95jL2Y/+DW1P7FJf5zCA21QqGMYL5hTc3QU82W2blqyXFX0bKl7Ja9MpyQiTtJebjyZ0nSncRQ2PGB/oClqBR4Q5kLAPgrzmL83I9U7nFQTAkusaA6WWHf0qN3kHGmLq4Dc0BbxgdWBSqmsNlJ4mgDb9AdP5sFcuVfiinX2vYgYfO7edXQ7zj7pX517SJ/OzcY1qnZNQXMEvPtCri/RXxXvZxQoAkUdLv5D9U0XUmDWmGTBdqaIZRkr4ZVZO/IUAfc0zCmBKmPuAu3eASdyd1dms8kjr1z36cr/EKmyDeln+eUWjwNl2FKrjpxabLO9i1O+J6Ddw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=HwLFTO49hOmOtrrgW7Di3P+CrgGZZnLOsuy0+ujom3Y=; b=BelkduLV5rlglrwOEomD8GixNLSi3+ibflWBXCv42O6YAnLn2R+eqLliSb291IX02k/LgycwdJ1DtHHnGYlGXdF85iAgJu26kG0mkIAYa4hHQR+NNfhbq70O2RMx159giBTjmRIWsAeYtI1V8v6p2M/KOzAEyi6L7vc0tHMzgKA= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from PAXPR04MB9571.eurprd04.prod.outlook.com (2603:10a6:102:24e::7) by AM9PR04MB8586.eurprd04.prod.outlook.com (2603:10a6:20b:439::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6411.29; Tue, 23 May 2023 15:34:58 +0000 Received: from PAXPR04MB9571.eurprd04.prod.outlook.com ([fe80::b082:c033:f721:d448]) by PAXPR04MB9571.eurprd04.prod.outlook.com ([fe80::b082:c033:f721:d448%7]) with mapi id 15.20.6411.029; Tue, 23 May 2023 15:34:58 +0000 From: meenakshi.aggarwal@nxp.com To: horia.geanta@nxp.com, V.sethi@nxp.com, pankaj.gupta@nxp.com, gaurav.jain@nxp.com, herbert@gondor.apana.org.au, davem@davemloft.net, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, iuliana.prodan@nxp.com Cc: Meenakshi Aggarwal Subject: [PATCH 5/5] crypto: caam/qi2 - avoid allocating memory at crypto request runtime Date: Tue, 23 May 2023 17:34:21 +0200 Message-Id: <20230523153421.1528359-6-meenakshi.aggarwal@nxp.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230523153421.1528359-1-meenakshi.aggarwal@nxp.com> References: <20230523153421.1528359-1-meenakshi.aggarwal@nxp.com> X-ClientProxiedBy: AM4PR07CA0020.eurprd07.prod.outlook.com (2603:10a6:205:1::33) To DU0PR04MB9561.eurprd04.prod.outlook.com (2603:10a6:10:312::7) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PAXPR04MB9571:EE_|AM9PR04MB8586:EE_ X-MS-Office365-Filtering-Correlation-Id: f503457d-b0f5-4494-b853-08db5ba34368 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: AtzLK8eJKg1At2V+deddvi7TXGdo+t0qzUhzeLTJQL8C+GfhwJwf62s1ArIzqruGXP8WZaoAg8AvkFjTvIuOCHb8wk1CkxgDHhcE1vQvrlkWTWpEhlRgXr1nMx9WFYoyaXaDX3mX9auLtciGlEN6au8SyVzMQhf72/6ouujXkVOuP/tDb0qijbYz5eDrppNclEtGGwwGEQ6unRZJS/543LvKV9Up7SoxhIkwZg8LgQt4sBm1TvYeOBp/Qkm9uY6xcpCDLpNf8Syyzci9gdlsFl0X5qiZQ+2esG9+7KR2Rv3ZwptgU9s7E+W8KDEiRcr4SK0gIsAnJNG2muLPrjoSN4k4VnjnrfzfGloh/clqclatu1DKC/D7oct3vmzucpmhyJBuFI3Lm76i/67cmtgIDL6fyHNDDyailuyis2tYW0/9XBw9PmX5zz8R4pBZG/s/XxvL9o/AgIKbnfBmeHFBHAKJHOWu5xhHExMEOX1rNhr1AHbqsHnjmNp8xGFa2Z0hMb9oHYN7Z64zh3YwHmGuCeSzaasS+FqufB9n0i2DUAxVTmMRzChSLXEr33HAsLP+KiSlnx3OCVwpQ+jMxu5/n2vd9I+hsznGardrZokYfyLIwz4KBlQHlIeg95z9bjam X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PAXPR04MB9571.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(136003)(346002)(366004)(376002)(396003)(39860400002)(451199021)(41300700001)(6486002)(52116002)(6666004)(316002)(26005)(9686003)(6506007)(6512007)(1076003)(5660300002)(36756003)(8936002)(8676002)(38100700002)(38350700002)(478600001)(86362001)(2616005)(83380400001)(30864003)(186003)(66476007)(66556008)(66946007)(2906002)(4326008);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Eu+qxid+cP3mmkKQaK8T0s7p+ero8Pl26aIWPl42pgf3yFcS0fSCFEgbdM9sVghc9danBsj6CrpGRJhA1HsMAKEO6mS4JTswvgbt/V5/eydQFwLWgi1Z5vv3zfxwFAmTZ+/eMGxlrSkolmAG60blPTSGNNdK2DWsm0U0C5HtxsiLEEL0S6T6fqd6qRoyTwqOQBqMp97N2pgRReIzegOcHg3redlxDn/nO6dgevp+8zSf1bzQCCdWrhNxDvaiPtzyICuEE9m9OOphd4lLt2mcYEG6kT/3OnHXehdpXEZsXLbS2xcCVJVAnp57zRQEG8Fobayw9MxLveSlLRyhXhLxdMcWGE+/MEZL961hZTtaLQqcjr/lpHipPjkK4tdcitqHHXkO8MVh0vHIMODFJD+9s9AT+r/TeCslAEJWgXp2qxA/Wfir0sBowXNMzMrHlfo1luYzxFeOj9s/t1rhnxBGni4OGrWzrR/dU06eTEpp/r7XcZfqFx3y0TInal2c/DzxGkAQn9rJxRaer2xtVgOTJdJbiavG39UaakL9sjPX33Ina+2bXh7g0ADe1ZbidvsAWrYEn8ycpwCoDwSbKbKlAJQP0Ih5Qj8/99i6FKnEogLKKulf75QBktBPGW6RPWcXgztHnlR29eI7PdFnPvJYmzPasQXRhzzDrSJhgwv7QjDYyMH5bNEKxhLITU3uIHw+H9mD4ZAfkZypBhNVRpYIp0+Xq2m+spIPLQ+iToPge6FlA59rX3Mg9HIiDDQaavucyr/HJpZvhMeILUwUyLo6prbXtBNa+2khWLqwhfGn/k5Kp2unj8Jrx22kJKKo/oo05hIh7LTMQ3IuRNmTVpSxUua/mUvjC9XyQw6b6IBKMQ+yMD5/IjmYTukD1nPF0lU9DebxNcTU4idkrHqn+jyNHkw5STsML+F8BIFhwHUnh6YQffaS0sohQMZlxvmpcbvuqHripEGFSX3StWpJsv2TprvRK/VYemev04w1zVlplB/61g8F1oAttj4hB+t6ULnck1ITMUyHVUZM+aUSWbI7CMn4c0AoayQL7BjXxy+rKnO7E/I9pwcaA2WOWxWjj5CL9/eldl4cs5+OsraJsfFAjiPe3/3ayvqnZZu0xT0ZuHugXP1tB2on0k2sL48IA0+cco4O9z8N6GgNYeYIeQK9c5aXOBGZB37Y8GNJZIV00QT9eA/pFVRZq2RK1z542wpj1k41/Q8y7FzZyI2a4wzZHJMoR4feTu3cvbQHx+Tt4UfERAhQlFAQIKWnVOBQXHQfHKV3Eo0+cc7A7pc2R3wPJjtR5nwaEEYH742NDWY5GbcdOYdHbtAA5bSMmT8zvcIO6x8K+Rb0+Lxnu1GKZtUU53LSR8egeUEfz7peyNdBxT7f0iX4QEkt0mDEvnFbIGJn9RujcFWLYqdxP6nsH/67bs5/eCEcFo1VJCOQ+3oPFuB48v1GqfBkEeLIT4TkCxxtyoNzJa4PawERopE8yMTViRWbHjO8hoWWY2JzNuINz7g7h4BlwFPR+Ud9vIlp7WCan6dIH/HSIKz43qg1I4BsbeNB09TqI0tq1K08Ab3N/kqLCiCKHKaNxt2qwHyo3y+vTrQTV14eb7kaDtbHT1vKdQ== X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: f503457d-b0f5-4494-b853-08db5ba34368 X-MS-Exchange-CrossTenant-AuthSource: DU0PR04MB9561.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 May 2023 15:34:57.6498 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: pMjoXIo7GBPJy+Z+KjeMDhrHZfS71usM1J0OczjtOVXXRe4ntWhh5Y2FzqT6ZERBXKHDGGsT3hlEi5m0WGWXaaWse+JZfJIfVYdKaNt9yMs= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM9PR04MB8586 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Meenakshi Aggarwal Remove CRYPTO_ALG_ALLOCATES_MEMORY flag and allocate the memory needed by the driver, to fulfil a request, within the crypto request object. The extra size needed for base extended descriptor, hw descriptor commands and link tables is computed in frontend driver (caamalg_qi2) initialization and saved in reqsize field that indicates how much memory could be needed per request. CRYPTO_ALG_ALLOCATES_MEMORY flag is limited only to dm-crypt use-cases, which seems to be 4 entries maximum. Therefore in reqsize we allocate memory for maximum 4 entries for src and 4 for dst, aligned. If the driver needs more than the 4 entries maximum, the memory is dynamically allocated, at runtime. Signed-off-by: Iuliana Prodan Signed-off-by: Meenakshi Aggarwal --- drivers/crypto/caam/caamalg_qi2.c | 421 ++++++++++++++++++++---------- drivers/crypto/caam/caamalg_qi2.h | 6 + 2 files changed, 293 insertions(+), 134 deletions(-) diff --git a/drivers/crypto/caam/caamalg_qi2.c b/drivers/crypto/caam/caamalg_qi2.c index 5c8d35edaa1c..5bf6e29ec2f5 100644 --- a/drivers/crypto/caam/caamalg_qi2.c +++ b/drivers/crypto/caam/caamalg_qi2.c @@ -367,17 +367,10 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req, dma_addr_t qm_sg_dma, iv_dma = 0; int ivsize = 0; unsigned int authsize = ctx->authsize; - int qm_sg_index = 0, qm_sg_nents = 0, qm_sg_bytes; + int qm_sg_index = 0, qm_sg_nents = 0, qm_sg_bytes, edesc_size = 0; int in_len, out_len; struct dpaa2_sg_entry *sg_table; - /* allocate space for base edesc, link tables and IV */ - edesc = qi_cache_zalloc(flags); - if (unlikely(!edesc)) { - dev_err(dev, "could not allocate extended descriptor\n"); - return ERR_PTR(-ENOMEM); - } - if (unlikely(req->dst != req->src)) { src_len = req->assoclen + req->cryptlen; dst_len = src_len + (encrypt ? authsize : (-authsize)); @@ -386,7 +379,6 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req, if (unlikely(src_nents < 0)) { dev_err(dev, "Insufficient bytes (%d) in src S/G\n", src_len); - qi_cache_free(edesc); return ERR_PTR(src_nents); } @@ -394,7 +386,6 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req, if (unlikely(dst_nents < 0)) { dev_err(dev, "Insufficient bytes (%d) in dst S/G\n", dst_len); - qi_cache_free(edesc); return ERR_PTR(dst_nents); } @@ -403,7 +394,6 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req, DMA_TO_DEVICE); if (unlikely(!mapped_src_nents)) { dev_err(dev, "unable to map source\n"); - qi_cache_free(edesc); return ERR_PTR(-ENOMEM); } } else { @@ -417,7 +407,6 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req, dev_err(dev, "unable to map destination\n"); dma_unmap_sg(dev, req->src, src_nents, DMA_TO_DEVICE); - qi_cache_free(edesc); return ERR_PTR(-ENOMEM); } } else { @@ -431,7 +420,6 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req, if (unlikely(src_nents < 0)) { dev_err(dev, "Insufficient bytes (%d) in src S/G\n", src_len); - qi_cache_free(edesc); return ERR_PTR(src_nents); } @@ -439,7 +427,6 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req, DMA_BIDIRECTIONAL); if (unlikely(!mapped_src_nents)) { dev_err(dev, "unable to map source\n"); - qi_cache_free(edesc); return ERR_PTR(-ENOMEM); } } @@ -469,18 +456,35 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req, else qm_sg_nents = pad_sg_nents(qm_sg_nents); - sg_table = &edesc->sgt[0]; qm_sg_bytes = qm_sg_nents * sizeof(*sg_table); - if (unlikely(offsetof(struct aead_edesc, sgt) + qm_sg_bytes + ivsize > - CAAM_QI_MEMCACHE_SIZE)) { + + /* Check if there's enough space for edesc saved in req */ + edesc_size = offsetof(struct aead_edesc, sgt) + qm_sg_bytes + ivsize; + if (unlikely(edesc_size > CAAM_QI_MEMCACHE_SIZE)) { dev_err(dev, "No space for %d S/G entries and/or %dB IV\n", qm_sg_nents, ivsize); caam_unmap(dev, req->src, req->dst, src_nents, dst_nents, 0, 0, DMA_NONE, 0, 0); - qi_cache_free(edesc); return ERR_PTR(-ENOMEM); + } else if (edesc_size > (crypto_aead_reqsize(aead) - + sizeof(struct caam_request))) { + /* allocate space for base edesc, link tables and IV */ + edesc = qi_cache_zalloc(flags); + if (unlikely(!edesc)) { + dev_err(dev, "could not allocate extended descriptor\n"); + return ERR_PTR(-ENOMEM); + } + edesc->free = true; + } else { + /* get address for base edesc, link tables and IV */ + edesc = (struct aead_edesc *)((u8 *)req_ctx + + sizeof(struct caam_request)); + /* clear memory */ + memset(edesc, 0, sizeof(*edesc)); } + sg_table = &edesc->sgt[0]; + if (ivsize) { u8 *iv = (u8 *)(sg_table + qm_sg_nents); @@ -492,7 +496,8 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req, dev_err(dev, "unable to map IV\n"); caam_unmap(dev, req->src, req->dst, src_nents, dst_nents, 0, 0, DMA_NONE, 0, 0); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); return ERR_PTR(-ENOMEM); } } @@ -516,7 +521,8 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req, dev_err(dev, "unable to map assoclen\n"); caam_unmap(dev, req->src, req->dst, src_nents, dst_nents, iv_dma, ivsize, DMA_TO_DEVICE, 0, 0); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); return ERR_PTR(-ENOMEM); } @@ -538,7 +544,8 @@ static struct aead_edesc *aead_edesc_alloc(struct aead_request *req, dma_unmap_single(dev, edesc->assoclen_dma, 4, DMA_TO_DEVICE); caam_unmap(dev, req->src, req->dst, src_nents, dst_nents, iv_dma, ivsize, DMA_TO_DEVICE, 0, 0); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); return ERR_PTR(-ENOMEM); } @@ -1123,7 +1130,7 @@ static struct skcipher_edesc *skcipher_edesc_alloc(struct skcipher_request *req) dma_addr_t iv_dma; u8 *iv; int ivsize = crypto_skcipher_ivsize(skcipher); - int dst_sg_idx, qm_sg_ents, qm_sg_bytes; + int dst_sg_idx, qm_sg_ents, qm_sg_bytes, edesc_size = 0; struct dpaa2_sg_entry *sg_table; src_nents = sg_nents_for_len(req->src, req->cryptlen); @@ -1181,22 +1188,31 @@ static struct skcipher_edesc *skcipher_edesc_alloc(struct skcipher_request *req) qm_sg_ents = 1 + pad_sg_nents(qm_sg_ents); qm_sg_bytes = qm_sg_ents * sizeof(struct dpaa2_sg_entry); - if (unlikely(offsetof(struct skcipher_edesc, sgt) + qm_sg_bytes + - ivsize > CAAM_QI_MEMCACHE_SIZE)) { + /* Check if there's enough space for edesc saved in req */ + edesc_size = offsetof(struct skcipher_edesc, sgt) + qm_sg_bytes + ivsize; + if (unlikely(edesc_size > CAAM_QI_MEMCACHE_SIZE)) { dev_err(dev, "No space for %d S/G entries and/or %dB IV\n", qm_sg_ents, ivsize); caam_unmap(dev, req->src, req->dst, src_nents, dst_nents, 0, 0, DMA_NONE, 0, 0); return ERR_PTR(-ENOMEM); - } - - /* allocate space for base edesc, link tables and IV */ - edesc = qi_cache_zalloc(flags); - if (unlikely(!edesc)) { - dev_err(dev, "could not allocate extended descriptor\n"); - caam_unmap(dev, req->src, req->dst, src_nents, dst_nents, 0, - 0, DMA_NONE, 0, 0); - return ERR_PTR(-ENOMEM); + } else if (edesc_size > (crypto_skcipher_reqsize(skcipher) - + sizeof(struct caam_request))) { + /* allocate space for base edesc, link tables and IV */ + edesc = qi_cache_zalloc(flags); + if (unlikely(!edesc)) { + dev_err(dev, "could not allocate extended descriptor\n"); + caam_unmap(dev, req->src, req->dst, src_nents, + dst_nents, 0, 0, DMA_NONE, 0, 0); + return ERR_PTR(-ENOMEM); + } + edesc->free = true; + } else { + /* get address for base edesc, link tables and IV */ + edesc = (struct skcipher_edesc *)((u8 *)req_ctx + + sizeof(struct caam_request)); + /* clear memory */ + memset(edesc, 0, sizeof(*edesc)); } /* Make sure IV is located in a DMAable area */ @@ -1209,7 +1225,8 @@ static struct skcipher_edesc *skcipher_edesc_alloc(struct skcipher_request *req) dev_err(dev, "unable to map IV\n"); caam_unmap(dev, req->src, req->dst, src_nents, dst_nents, 0, 0, DMA_NONE, 0, 0); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); return ERR_PTR(-ENOMEM); } @@ -1233,7 +1250,8 @@ static struct skcipher_edesc *skcipher_edesc_alloc(struct skcipher_request *req) dev_err(dev, "unable to map S/G table\n"); caam_unmap(dev, req->src, req->dst, src_nents, dst_nents, iv_dma, ivsize, DMA_BIDIRECTIONAL, 0, 0); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); return ERR_PTR(-ENOMEM); } @@ -1297,7 +1315,8 @@ static void aead_encrypt_done(void *cbk_ctx, u32 status) ecode = caam_qi2_strstatus(ctx->dev, status); aead_unmap(ctx->dev, edesc, req); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); aead_request_complete(req, ecode); } @@ -1318,7 +1337,8 @@ static void aead_decrypt_done(void *cbk_ctx, u32 status) ecode = caam_qi2_strstatus(ctx->dev, status); aead_unmap(ctx->dev, edesc, req); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); aead_request_complete(req, ecode); } @@ -1344,7 +1364,8 @@ static int aead_encrypt(struct aead_request *req) if (ret != -EINPROGRESS && !(ret == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) { aead_unmap(ctx->dev, edesc, req); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); } return ret; @@ -1372,7 +1393,8 @@ static int aead_decrypt(struct aead_request *req) if (ret != -EINPROGRESS && !(ret == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) { aead_unmap(ctx->dev, edesc, req); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); } return ret; @@ -1422,7 +1444,8 @@ static void skcipher_encrypt_done(void *cbk_ctx, u32 status) memcpy(req->iv, (u8 *)&edesc->sgt[0] + edesc->qm_sg_bytes, ivsize); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); skcipher_request_complete(req, ecode); } @@ -1460,7 +1483,8 @@ static void skcipher_decrypt_done(void *cbk_ctx, u32 status) memcpy(req->iv, (u8 *)&edesc->sgt[0] + edesc->qm_sg_bytes, ivsize); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); skcipher_request_complete(req, ecode); } @@ -1516,7 +1540,8 @@ static int skcipher_encrypt(struct skcipher_request *req) if (ret != -EINPROGRESS && !(ret == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) { skcipher_unmap(ctx->dev, edesc, req); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); } return ret; @@ -1566,7 +1591,8 @@ static int skcipher_decrypt(struct skcipher_request *req) if (ret != -EINPROGRESS && !(ret == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) { skcipher_unmap(ctx->dev, edesc, req); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); } return ret; @@ -1607,7 +1633,15 @@ static int caam_cra_init_skcipher(struct crypto_skcipher *tfm) container_of(alg, typeof(*caam_alg), skcipher); struct caam_ctx *ctx = crypto_skcipher_ctx_dma(tfm); u32 alg_aai = caam_alg->caam.class1_alg_type & OP_ALG_AAI_MASK; - int ret = 0; + int ret = 0, extra_reqsize = 0; + + /* Compute extra space needed for base edesc, link tables and IV */ + extra_reqsize = sizeof(struct skcipher_edesc) + + /* link tables for src and dst: + * 4 entries max + 1 for IV, aligned = 8 + */ + (16 * sizeof(struct dpaa2_sg_entry)) + + AES_BLOCK_SIZE; /* ivsize */ if (alg_aai == OP_ALG_AAI_XTS) { const char *tfm_name = crypto_tfm_alg_name(&tfm->base); @@ -1625,10 +1659,12 @@ static int caam_cra_init_skcipher(struct crypto_skcipher *tfm) ctx->fallback = fallback; crypto_skcipher_set_reqsize_dma( tfm, sizeof(struct caam_request) + - crypto_skcipher_reqsize(fallback)); + crypto_skcipher_reqsize(fallback + + extra_reqsize)); } else { crypto_skcipher_set_reqsize_dma(tfm, - sizeof(struct caam_request)); + sizeof(struct caam_request) + + extra_reqsize); } ret = caam_cra_init(ctx, &caam_alg->caam, false); @@ -1644,7 +1680,18 @@ static int caam_cra_init_aead(struct crypto_aead *tfm) struct caam_aead_alg *caam_alg = container_of(alg, typeof(*caam_alg), aead); - crypto_aead_set_reqsize_dma(tfm, sizeof(struct caam_request)); + int extra_reqsize = 0; + + /* Compute extra space needed for base edesc, link tables and IV */ + extra_reqsize = sizeof(struct aead_edesc) + + /* link tables for src and dst: + * 4 entries max + 1 for IV, aligned = 8 + */ + (16 * sizeof(struct dpaa2_sg_entry)) + + AES_BLOCK_SIZE; /* ivsize */ + + crypto_aead_set_reqsize_dma(tfm, sizeof(struct caam_request) + + extra_reqsize); return caam_cra_init(crypto_aead_ctx_dma(tfm), &caam_alg->caam, !caam_alg->caam.nodkp); } @@ -3013,8 +3060,7 @@ static void caam_skcipher_alg_init(struct caam_skcipher_alg *t_alg) alg->base.cra_module = THIS_MODULE; alg->base.cra_priority = CAAM_CRA_PRIORITY; alg->base.cra_ctxsize = sizeof(struct caam_ctx) + crypto_dma_padding(); - alg->base.cra_flags |= (CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY | - CRYPTO_ALG_KERN_DRIVER_ONLY); + alg->base.cra_flags |= (CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY); alg->init = caam_cra_init_skcipher; alg->exit = caam_cra_exit; @@ -3027,8 +3073,7 @@ static void caam_aead_alg_init(struct caam_aead_alg *t_alg) alg->base.cra_module = THIS_MODULE; alg->base.cra_priority = CAAM_CRA_PRIORITY; alg->base.cra_ctxsize = sizeof(struct caam_ctx) + crypto_dma_padding(); - alg->base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY | - CRYPTO_ALG_KERN_DRIVER_ONLY; + alg->base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY; alg->init = caam_cra_init_aead; alg->exit = caam_cra_exit_aead; @@ -3413,7 +3458,8 @@ static void ahash_done(void *cbk_ctx, u32 status) ahash_unmap_ctx(ctx->dev, edesc, req, DMA_FROM_DEVICE); memcpy(req->result, state->caam_ctx, digestsize); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); print_hex_dump_debug("ctx@" __stringify(__LINE__)": ", DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx, @@ -3438,7 +3484,8 @@ static void ahash_done_bi(void *cbk_ctx, u32 status) ecode = caam_qi2_strstatus(ctx->dev, status); ahash_unmap_ctx(ctx->dev, edesc, req, DMA_BIDIRECTIONAL); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); scatterwalk_map_and_copy(state->buf, req->src, req->nbytes - state->next_buflen, @@ -3478,7 +3525,8 @@ static void ahash_done_ctx_src(void *cbk_ctx, u32 status) ahash_unmap_ctx(ctx->dev, edesc, req, DMA_BIDIRECTIONAL); memcpy(req->result, state->caam_ctx, digestsize); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); print_hex_dump_debug("ctx@" __stringify(__LINE__)": ", DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx, @@ -3503,7 +3551,8 @@ static void ahash_done_ctx_dst(void *cbk_ctx, u32 status) ecode = caam_qi2_strstatus(ctx->dev, status); ahash_unmap_ctx(ctx->dev, edesc, req, DMA_FROM_DEVICE); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); scatterwalk_map_and_copy(state->buf, req->src, req->nbytes - state->next_buflen, @@ -3541,7 +3590,7 @@ static int ahash_update_ctx(struct ahash_request *req) int in_len = *buflen + req->nbytes, to_hash; int src_nents, mapped_nents, qm_sg_bytes, qm_sg_src_index; struct ahash_edesc *edesc; - int ret = 0; + int ret = 0, edesc_size = 0; *next_buflen = in_len & (crypto_tfm_alg_blocksize(&ahash->base) - 1); to_hash = in_len - *next_buflen; @@ -3567,18 +3616,31 @@ static int ahash_update_ctx(struct ahash_request *req) mapped_nents = 0; } - /* allocate space for base edesc and link tables */ - edesc = qi_cache_zalloc(flags); - if (!edesc) { - dma_unmap_sg(ctx->dev, req->src, src_nents, - DMA_TO_DEVICE); - return -ENOMEM; - } - - edesc->src_nents = src_nents; qm_sg_src_index = 1 + (*buflen ? 1 : 0); qm_sg_bytes = pad_sg_nents(qm_sg_src_index + mapped_nents) * sizeof(*sg_table); + + /* Check if there's enough space for edesc saved in req */ + edesc_size = sizeof(*edesc) + qm_sg_bytes; + if (edesc_size > (crypto_ahash_reqsize(ahash) - + sizeof(struct caam_hash_state))) { + /* allocate space for base edesc and link tables */ + edesc = qi_cache_zalloc(flags); + if (!edesc) { + dma_unmap_sg(ctx->dev, req->src, src_nents, + DMA_TO_DEVICE); + return -ENOMEM; + } + edesc->free = true; + } else { + /* get address for base edesc and link tables */ + edesc = (struct ahash_edesc *)((u8 *)state + + sizeof(struct caam_hash_state)); + /* clear memory */ + memset(edesc, 0, sizeof(*edesc)); + } + + edesc->src_nents = src_nents; sg_table = &edesc->sgt[0]; ret = ctx_map_to_qm_sg(ctx->dev, state, ctx->ctx_len, sg_table, @@ -3640,7 +3702,8 @@ static int ahash_update_ctx(struct ahash_request *req) return ret; unmap_ctx: ahash_unmap_ctx(ctx->dev, edesc, req, DMA_BIDIRECTIONAL); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); return ret; } @@ -3655,18 +3718,31 @@ static int ahash_final_ctx(struct ahash_request *req) gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? GFP_KERNEL : GFP_ATOMIC; int buflen = state->buflen; - int qm_sg_bytes; + int qm_sg_bytes, edesc_size = 0; int digestsize = crypto_ahash_digestsize(ahash); struct ahash_edesc *edesc; struct dpaa2_sg_entry *sg_table; int ret; - /* allocate space for base edesc and link tables */ - edesc = qi_cache_zalloc(flags); - if (!edesc) - return -ENOMEM; - qm_sg_bytes = pad_sg_nents(1 + (buflen ? 1 : 0)) * sizeof(*sg_table); + + /* Check if there's enough space for edesc saved in req */ + edesc_size = sizeof(*edesc) + qm_sg_bytes; + if (edesc_size > (crypto_ahash_reqsize(ahash) - + sizeof(struct caam_hash_state))) { + /* allocate space for base edesc and link tables */ + edesc = qi_cache_zalloc(flags); + if (!edesc) + return -ENOMEM; + edesc->free = true; + } else { + /* get address for base edesc and link tables */ + edesc = (struct ahash_edesc *)((u8 *)state + + sizeof(struct caam_hash_state)); + /* clear memory */ + memset(edesc, 0, sizeof(*edesc)); + } + sg_table = &edesc->sgt[0]; ret = ctx_map_to_qm_sg(ctx->dev, state, ctx->ctx_len, sg_table, @@ -3711,7 +3787,8 @@ static int ahash_final_ctx(struct ahash_request *req) unmap_ctx: ahash_unmap_ctx(ctx->dev, edesc, req, DMA_BIDIRECTIONAL); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); return ret; } @@ -3726,7 +3803,7 @@ static int ahash_finup_ctx(struct ahash_request *req) gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? GFP_KERNEL : GFP_ATOMIC; int buflen = state->buflen; - int qm_sg_bytes, qm_sg_src_index; + int qm_sg_bytes, qm_sg_src_index, edesc_size = 0; int src_nents, mapped_nents; int digestsize = crypto_ahash_digestsize(ahash); struct ahash_edesc *edesc; @@ -3750,17 +3827,31 @@ static int ahash_finup_ctx(struct ahash_request *req) mapped_nents = 0; } - /* allocate space for base edesc and link tables */ - edesc = qi_cache_zalloc(flags); - if (!edesc) { - dma_unmap_sg(ctx->dev, req->src, src_nents, DMA_TO_DEVICE); - return -ENOMEM; - } - - edesc->src_nents = src_nents; qm_sg_src_index = 1 + (buflen ? 1 : 0); qm_sg_bytes = pad_sg_nents(qm_sg_src_index + mapped_nents) * sizeof(*sg_table); + + /* Check if there's enough space for edesc saved in req */ + edesc_size = sizeof(*edesc) + qm_sg_bytes; + if (edesc_size > (crypto_ahash_reqsize(ahash) - + sizeof(struct caam_hash_state))) { + /* allocate space for base edesc and link tables */ + edesc = qi_cache_zalloc(flags); + if (!edesc) { + dma_unmap_sg(ctx->dev, req->src, src_nents, + DMA_TO_DEVICE); + return -ENOMEM; + } + edesc->free = true; + } else { + /* get address for base edesc and link tables */ + edesc = (struct ahash_edesc *)((u8 *)state + + sizeof(struct caam_hash_state)); + /* clear memory */ + memset(edesc, 0, sizeof(*edesc)); + } + + edesc->src_nents = src_nents; sg_table = &edesc->sgt[0]; ret = ctx_map_to_qm_sg(ctx->dev, state, ctx->ctx_len, sg_table, @@ -3805,7 +3896,8 @@ static int ahash_finup_ctx(struct ahash_request *req) unmap_ctx: ahash_unmap_ctx(ctx->dev, edesc, req, DMA_BIDIRECTIONAL); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); return ret; } @@ -3820,8 +3912,9 @@ static int ahash_digest(struct ahash_request *req) gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? GFP_KERNEL : GFP_ATOMIC; int digestsize = crypto_ahash_digestsize(ahash); - int src_nents, mapped_nents; + int src_nents, mapped_nents, qm_sg_bytes, edesc_size = 0; struct ahash_edesc *edesc; + struct dpaa2_sg_entry *sg_table; int ret = -ENOMEM; state->buf_dma = 0; @@ -3843,21 +3936,33 @@ static int ahash_digest(struct ahash_request *req) mapped_nents = 0; } - /* allocate space for base edesc and link tables */ - edesc = qi_cache_zalloc(flags); - if (!edesc) { - dma_unmap_sg(ctx->dev, req->src, src_nents, DMA_TO_DEVICE); - return ret; + qm_sg_bytes = pad_sg_nents(mapped_nents) * sizeof(*sg_table); + + /* Check if there's enough space for edesc saved in req */ + edesc_size = sizeof(*edesc) + qm_sg_bytes; + if (edesc_size > (crypto_ahash_reqsize(ahash) - + sizeof(struct caam_hash_state))) { + /* allocate space for base edesc and link tables */ + edesc = qi_cache_zalloc(flags); + if (!edesc) { + dma_unmap_sg(ctx->dev, req->src, src_nents, + DMA_TO_DEVICE); + return ret; + } + edesc->free = true; + } else { + /* get address for base edesc and link tables */ + edesc = (struct ahash_edesc *)((u8 *)state + + sizeof(struct caam_hash_state)); + /* clear memory */ + memset(edesc, 0, sizeof(*edesc)); } edesc->src_nents = src_nents; memset(&req_ctx->fd_flt, 0, sizeof(req_ctx->fd_flt)); if (mapped_nents > 1) { - int qm_sg_bytes; - struct dpaa2_sg_entry *sg_table = &edesc->sgt[0]; - - qm_sg_bytes = pad_sg_nents(mapped_nents) * sizeof(*sg_table); + sg_table = &edesc->sgt[0]; sg_to_qm_sg_last(req->src, req->nbytes, sg_table, 0); edesc->qm_sg_dma = dma_map_single(ctx->dev, sg_table, qm_sg_bytes, DMA_TO_DEVICE); @@ -3900,7 +4005,8 @@ static int ahash_digest(struct ahash_request *req) unmap: ahash_unmap_ctx(ctx->dev, edesc, req, DMA_FROM_DEVICE); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); return ret; } @@ -3912,18 +4018,17 @@ static int ahash_final_no_ctx(struct ahash_request *req) struct caam_request *req_ctx = &state->caam_req; struct dpaa2_fl_entry *in_fle = &req_ctx->fd_flt[1]; struct dpaa2_fl_entry *out_fle = &req_ctx->fd_flt[0]; - gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? - GFP_KERNEL : GFP_ATOMIC; u8 *buf = state->buf; int buflen = state->buflen; int digestsize = crypto_ahash_digestsize(ahash); struct ahash_edesc *edesc; int ret = -ENOMEM; - /* allocate space for base edesc and link tables */ - edesc = qi_cache_zalloc(flags); - if (!edesc) - return ret; + /* get address for base edesc and link tables */ + edesc = (struct ahash_edesc *)((u8 *)state + + sizeof(struct caam_hash_state)); + /* clear memory */ + memset(edesc, 0, sizeof(*edesc)); if (buflen) { state->buf_dma = dma_map_single(ctx->dev, buf, buflen, @@ -3973,7 +4078,6 @@ static int ahash_final_no_ctx(struct ahash_request *req) unmap: ahash_unmap_ctx(ctx->dev, edesc, req, DMA_FROM_DEVICE); - qi_cache_free(edesc); return ret; } @@ -3991,7 +4095,7 @@ static int ahash_update_no_ctx(struct ahash_request *req) int *buflen = &state->buflen; int *next_buflen = &state->next_buflen; int in_len = *buflen + req->nbytes, to_hash; - int qm_sg_bytes, src_nents, mapped_nents; + int qm_sg_bytes, src_nents, mapped_nents, edesc_size = 0; struct ahash_edesc *edesc; int ret = 0; @@ -4019,17 +4123,30 @@ static int ahash_update_no_ctx(struct ahash_request *req) mapped_nents = 0; } - /* allocate space for base edesc and link tables */ - edesc = qi_cache_zalloc(flags); - if (!edesc) { - dma_unmap_sg(ctx->dev, req->src, src_nents, - DMA_TO_DEVICE); - return -ENOMEM; + qm_sg_bytes = pad_sg_nents(1 + mapped_nents) * + sizeof(*sg_table); + + /* Check if there's enough space for edesc saved in req */ + edesc_size = sizeof(*edesc) + qm_sg_bytes; + if (edesc_size > (crypto_ahash_reqsize(ahash) - + sizeof(struct caam_hash_state))) { + /* allocate space for base edesc and link tables */ + edesc = qi_cache_zalloc(flags); + if (!edesc) { + dma_unmap_sg(ctx->dev, req->src, src_nents, + DMA_TO_DEVICE); + return -ENOMEM; + } + edesc->free = true; + } else { + /* get address for base edesc and link tables */ + edesc = (struct ahash_edesc *)((u8 *)state + + sizeof(struct caam_hash_state)); + /* clear memory */ + memset(edesc, 0, sizeof(*edesc)); } edesc->src_nents = src_nents; - qm_sg_bytes = pad_sg_nents(1 + mapped_nents) * - sizeof(*sg_table); sg_table = &edesc->sgt[0]; ret = buf_map_to_qm_sg(ctx->dev, sg_table, state); @@ -4094,7 +4211,8 @@ static int ahash_update_no_ctx(struct ahash_request *req) return ret; unmap_ctx: ahash_unmap_ctx(ctx->dev, edesc, req, DMA_TO_DEVICE); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); return ret; } @@ -4109,7 +4227,7 @@ static int ahash_finup_no_ctx(struct ahash_request *req) gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? GFP_KERNEL : GFP_ATOMIC; int buflen = state->buflen; - int qm_sg_bytes, src_nents, mapped_nents; + int qm_sg_bytes, src_nents, mapped_nents, edesc_size = 0; int digestsize = crypto_ahash_digestsize(ahash); struct ahash_edesc *edesc; struct dpaa2_sg_entry *sg_table; @@ -4132,15 +4250,29 @@ static int ahash_finup_no_ctx(struct ahash_request *req) mapped_nents = 0; } - /* allocate space for base edesc and link tables */ - edesc = qi_cache_zalloc(flags); - if (!edesc) { - dma_unmap_sg(ctx->dev, req->src, src_nents, DMA_TO_DEVICE); - return ret; + qm_sg_bytes = pad_sg_nents(2 + mapped_nents) * sizeof(*sg_table); + + /* Check if there's enough space for edesc saved in req */ + edesc_size = sizeof(*edesc) + qm_sg_bytes; + if (edesc_size > (crypto_ahash_reqsize(ahash) - + sizeof(struct caam_hash_state))) { + /* allocate space for base edesc and link tables */ + edesc = qi_cache_zalloc(flags); + if (!edesc) { + dma_unmap_sg(ctx->dev, req->src, src_nents, + DMA_TO_DEVICE); + return ret; + } + edesc->free = true; + } else { + /* get address for base edesc and link tables */ + edesc = (struct ahash_edesc *)((u8 *)state + + sizeof(struct caam_hash_state)); + /* clear memory */ + memset(edesc, 0, sizeof(*edesc)); } edesc->src_nents = src_nents; - qm_sg_bytes = pad_sg_nents(2 + mapped_nents) * sizeof(*sg_table); sg_table = &edesc->sgt[0]; ret = buf_map_to_qm_sg(ctx->dev, sg_table, state); @@ -4190,7 +4322,8 @@ static int ahash_finup_no_ctx(struct ahash_request *req) return ret; unmap: ahash_unmap_ctx(ctx->dev, edesc, req, DMA_FROM_DEVICE); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); return ret; } @@ -4208,7 +4341,7 @@ static int ahash_update_first(struct ahash_request *req) int *buflen = &state->buflen; int *next_buflen = &state->next_buflen; int to_hash; - int src_nents, mapped_nents; + int src_nents, mapped_nents, qm_sg_bytes, edesc_size = 0; struct ahash_edesc *edesc; int ret = 0; @@ -4237,12 +4370,26 @@ static int ahash_update_first(struct ahash_request *req) mapped_nents = 0; } - /* allocate space for base edesc and link tables */ - edesc = qi_cache_zalloc(flags); - if (!edesc) { - dma_unmap_sg(ctx->dev, req->src, src_nents, - DMA_TO_DEVICE); - return -ENOMEM; + qm_sg_bytes = pad_sg_nents(mapped_nents) * sizeof(*sg_table); + + /* Check if there's enough space for edesc saved in req */ + edesc_size = sizeof(*edesc) + qm_sg_bytes; + if (edesc_size > (crypto_ahash_reqsize(ahash) - + sizeof(struct caam_hash_state))) { + /* allocate space for base edesc and link tables */ + edesc = qi_cache_zalloc(flags); + if (!edesc) { + dma_unmap_sg(ctx->dev, req->src, src_nents, + DMA_TO_DEVICE); + return -ENOMEM; + } + edesc->free = true; + } else { + /* get address for base edesc and link tables */ + edesc = (struct ahash_edesc *)((u8 *)state + + sizeof(struct caam_hash_state)); + /* clear memory */ + memset(edesc, 0, sizeof(*edesc)); } edesc->src_nents = src_nents; @@ -4253,11 +4400,7 @@ static int ahash_update_first(struct ahash_request *req) dpaa2_fl_set_len(in_fle, to_hash); if (mapped_nents > 1) { - int qm_sg_bytes; - sg_to_qm_sg_last(req->src, src_len, sg_table, 0); - qm_sg_bytes = pad_sg_nents(mapped_nents) * - sizeof(*sg_table); edesc->qm_sg_dma = dma_map_single(ctx->dev, sg_table, qm_sg_bytes, DMA_TO_DEVICE); @@ -4319,7 +4462,8 @@ static int ahash_update_first(struct ahash_request *req) return ret; unmap_ctx: ahash_unmap_ctx(ctx->dev, edesc, req, DMA_TO_DEVICE); - qi_cache_free(edesc); + if (edesc->free) + qi_cache_free(edesc); return ret; } @@ -4566,7 +4710,7 @@ static int caam_hash_cra_init(struct crypto_tfm *tfm) HASH_MSG_LEN + 64, HASH_MSG_LEN + SHA512_DIGEST_SIZE }; dma_addr_t dma_addr; - int i; + int i, extra_reqsize = 0; ctx->dev = caam_hash->dev; @@ -4604,7 +4748,16 @@ static int caam_hash_cra_init(struct crypto_tfm *tfm) OP_ALG_ALGSEL_SUBMASK) >> OP_ALG_ALGSEL_SHIFT]; - crypto_ahash_set_reqsize_dma(ahash, sizeof(struct caam_hash_state)); + /* Compute extra space needed for base edesc and link tables */ + extra_reqsize = sizeof(struct ahash_edesc) + + /* link tables for src: + * 4 entries max + max 2 for remaining buf, aligned = 8 + */ + (8 * sizeof(struct dpaa2_sg_entry)); + + crypto_ahash_set_reqsize_dma(ahash, + sizeof(struct caam_hash_state) + + extra_reqsize); /* * For keyed hash algorithms shared descriptors @@ -4659,7 +4812,7 @@ static struct caam_hash_alg *caam_hash_alloc(struct device *dev, alg->cra_priority = CAAM_CRA_PRIORITY; alg->cra_blocksize = template->blocksize; alg->cra_alignmask = 0; - alg->cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY; + alg->cra_flags = CRYPTO_ALG_ASYNC; t_alg->alg_type = template->alg_type; t_alg->dev = dev; diff --git a/drivers/crypto/caam/caamalg_qi2.h b/drivers/crypto/caam/caamalg_qi2.h index abb502bb675c..c700438de9e6 100644 --- a/drivers/crypto/caam/caamalg_qi2.h +++ b/drivers/crypto/caam/caamalg_qi2.h @@ -100,6 +100,7 @@ struct dpaa2_caam_priv_per_cpu { * @dst_nents: number of segments in output scatterlist * @iv_dma: dma address of iv for checking continuity and link table * @qm_sg_bytes: length of dma mapped h/w link table + * @free: stored to determine if aead_edesc needs to be freed * @qm_sg_dma: bus physical mapped address of h/w link table * @assoclen: associated data length, in CAAM endianness * @assoclen_dma: bus physical mapped address of req->assoclen @@ -110,6 +111,7 @@ struct aead_edesc { int dst_nents; dma_addr_t iv_dma; int qm_sg_bytes; + bool free; dma_addr_t qm_sg_dma; unsigned int assoclen; dma_addr_t assoclen_dma; @@ -122,6 +124,7 @@ struct aead_edesc { * @dst_nents: number of segments in output scatterlist * @iv_dma: dma address of iv for checking continuity and link table * @qm_sg_bytes: length of dma mapped qm_sg space + * @free: stored to determine if skcipher_edesc needs to be freed * @qm_sg_dma: I/O virtual address of h/w link table * @sgt: the h/w link table, followed by IV */ @@ -130,6 +133,7 @@ struct skcipher_edesc { int dst_nents; dma_addr_t iv_dma; int qm_sg_bytes; + bool free; dma_addr_t qm_sg_dma; struct dpaa2_sg_entry sgt[]; }; @@ -139,12 +143,14 @@ struct skcipher_edesc { * @qm_sg_dma: I/O virtual address of h/w link table * @src_nents: number of segments in input scatterlist * @qm_sg_bytes: length of dma mapped qm_sg space + * @free: stored to determine if ahash_edesc needs to be freed * @sgt: pointer to h/w link table */ struct ahash_edesc { dma_addr_t qm_sg_dma; int src_nents; int qm_sg_bytes; + bool free; struct dpaa2_sg_entry sgt[]; };