From patchwork Fri Jun 9 10:38:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Meenakshi Aggarwal X-Patchwork-Id: 691240 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 C1A59C7EE2E for ; Fri, 9 Jun 2023 10:41:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231130AbjFIKlt (ORCPT ); Fri, 9 Jun 2023 06:41:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40696 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239657AbjFIKlR (ORCPT ); Fri, 9 Jun 2023 06:41:17 -0400 Received: from EUR02-DB5-obe.outbound.protection.outlook.com (mail-db5eur02on2086.outbound.protection.outlook.com [40.107.249.86]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 68C953C1D; Fri, 9 Jun 2023 03:39:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=V1TQf+0HLhHbGrK80gP8LiX1g4GIcjWITIPYvhUoyYlXeEN3emFjYEwvkKC6qC3JlcRhJDX/40GGJJkanyHDl5zX4eob3al2iQ9WjjMDqCrTyItUnzurX/zfAX2St1qE5bbuNiN/4imokxUTDf8BohnlUHSLBqmDezxpnwise0wuaXbbG63paegGN9LOWLzb8uf53THxulCn/3tVcY4T9S4NfzK6oYzqM0RC3OsxCTpa3YC6RRWrBpUtQNVFMRyKhHL4cc7qqcw3lXjBsIRIRAUXfUnzhhXdCIc1bVRK6P24AFD0NfnDRN7w/cXSzeLByEjiKoASP9gTdZppTIuaNw== 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=ruc90nzjtmSoMWJJMZQ/qH4qs3sixuPIMnUPjUCbDKA=; b=YztW2MuwxoiTrpp6RsPOj+7uSbIUmzyTeaJoUNNou/nbe8zVIm2qz0DrgpT7d++aQNVKSdOHVOMW0teLA+rSUe14cyoFeJ3btW1T5F0um0j+b16T2YNR98IcANoe5T1EqcoHe0i14c9YsfP2kcA7HUk4r7+3T7/vtx79/rerS++soH+6eN21+lrQb92DT5Zss/3iUx1gRiJ3JY6vcuWCNgSg/5QX8RlvzvDlSTSmCmxBiTaCxym2BMG5RdqPtHMFzKjIs+PtJXG/MaYCyLcUDyEC4GloYoMZfXeT7N5FVkmjv3r2pHSqP9nKoC9bDhccxMQ5vwf8NX4ejhSF6sYvrw== 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=ruc90nzjtmSoMWJJMZQ/qH4qs3sixuPIMnUPjUCbDKA=; b=sg2fkY/TCz9bZruM8vRTYJdr46lW7o99wSlT174d2lZow0Z4a10nP8m+iyvt6UsvhJBMaLVv4fcJfHYDa5DbaMEase2nUhrhy69ASFTghvaczhIl/xtGaTzPPsKpQtMdKsRHIprJ/hEwpJm6IyZ3ifw66N4o8S5JCO8K5FKHAGo= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from DU0PR04MB9563.eurprd04.prod.outlook.com (2603:10a6:10:314::7) by AS1PR04MB9336.eurprd04.prod.outlook.com (2603:10a6:20b:4dc::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.32; Fri, 9 Jun 2023 10:38:56 +0000 Received: from DU0PR04MB9563.eurprd04.prod.outlook.com ([fe80::adc3:90c7:4b37:392c]) by DU0PR04MB9563.eurprd04.prod.outlook.com ([fe80::adc3:90c7:4b37:392c%5]) with mapi id 15.20.6455.030; Fri, 9 Jun 2023 10:38:57 +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 v2 1/5] crypto:caam - avoid allocating memory at crypto request runtime for skcipher Date: Fri, 9 Jun 2023 12:38:38 +0200 Message-Id: <20230609103842.4159321-2-meenakshi.aggarwal@nxp.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230609103842.4159321-1-meenakshi.aggarwal@nxp.com> References: <20230523153421.1528359-1-meenakshi.aggarwal@nxp.com> <20230609103842.4159321-1-meenakshi.aggarwal@nxp.com> X-ClientProxiedBy: AM0PR03CA0096.eurprd03.prod.outlook.com (2603:10a6:208:69::37) To DU0PR04MB9563.eurprd04.prod.outlook.com (2603:10a6:10:314::7) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DU0PR04MB9563:EE_|AS1PR04MB9336:EE_ X-MS-Office365-Filtering-Correlation-Id: cf80d02f-49a7-44cb-1903-08db68d5babe X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: H1LBMPAe0SX/gWoRHujCqcFkWamc0HvsuJv9yW0K4uHxm2noI5ZIc80tynIzhdRFjUuoW6XO0RuPr4N7TpV75G2URfZ/yCBQkkC30n2LUP+kDlMp3oLFGIrxDR8qRxgGjIpzNOoRGLrqDVIGW8HsQjwX3nn+EO0xYpDK6H37O4rRaBw6Yq0zL+EKXCDjEntgxHKOtNQtscs3j7OIaYD1L0IlR5XcJ7DyTqn6PF/o/HH6NfDY1Mg9t1D+Y+XRu5ZJLtNOVItRkkw15FxIVrpSkrI7n2whAz4wfoN5QSvGFjqo9cK5Mss1L7RGz+bE8NaTcXDbiToTlpRYW/TTN7+pGqP1+nGr5A+V/bY8ezmkCfScgIGJiUJxHXL9fJcp94USHl/gh15nN1nTIvdtMNtXCw5CjMESqWM5pDjEJkR8yVvWlebr0r+DZkR00rqwhxrwJGoaSVJzH8YiLqlm8OUNeuQkttANN1DO8w1fQ8fe1CUOr8RW5ZCncatnmLXfclynqfqDpGDn9w+UB0AMfeBV1SkGge/3HfPeNHKVrkkm/vcApM4IDnfKQTXVUU6VlVFiWodpYsIShsnmXbJpSfWuCAG9Hd8L7VS9+G9tCyplt3iyRcukp8WjvCijwmUEEkCT X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DU0PR04MB9563.eurprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(4636009)(366004)(376002)(39860400002)(346002)(396003)(136003)(451199021)(66946007)(66476007)(2906002)(478600001)(4326008)(8936002)(8676002)(316002)(41300700001)(5660300002)(66556008)(6666004)(6486002)(52116002)(9686003)(6506007)(26005)(6512007)(1076003)(186003)(2616005)(38100700002)(83380400001)(38350700002)(36756003)(86362001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 4FetQNqpsiOaPj17juQsZjJFOZrTQXf5fryW1p5BQFDwk0S4mBjXr0jz+segyijVyZADI+wa1hFmNJvgg47XlkD2OyZ0dnUSEiNl7nyVbrzaWHKiwbvqgXHp0AcR+mOeDzXbk+KBIXHTbJF4FyBtPFX8WNL5KzlgH8bdcvp5gqflwgU8G+e6+UOkpK9VP+Aw0cj47SX9i+LrRkIvwD157ThYazuf8u7qkgTwmdf21q3/L85yvoPZ6W6jJ0SkYHY5U02B1NE5WW45RbOJP1nbP/v57fWaAW+HrsbMQtMMCXT8vd2uS5Z32O9pgRtBxtlnB8f3+xFuDWX2pQNSrroRjdLOlT6UcNBBuUDPuWMnx7054WLRjtpklH5enL9WUTNO/ceqYDx8QxaQus0xTGA1jld40vsXZvf1k3NzyLUoh54REn8Y2sE5+AyXEsKx6CvjwuhxtSqH1fEUysXPkFy3Q5VfwMXmTI5YfEH/Q3WkznxREWCfc6kTaoC9iW9LMKTYP24HVjYEdKIE6fGubxKfuYjOl3Zy9aREjNVkGRz0rnxvQcF0gThaPO0SZ2q+R6mNN36BvR3g1DzQ3PRpzFe6Wh8HUpP1Boi77Eqs73D4lCInc4QTGYVBQFsBaHB/0mzWtMGsWQ/WiGX411QWFRwZrizFvPMP6Xs0yRvulyouvYIZxj3HLevaBFPnOIRe7Ennremk5LN2LBovDWIiGsYIyzHwBxdgqCjwBTKrNyg5smFl1OtHY1UlPvtOLj5fEeAPaT1zrQPJamefBG0tG6bkM/d0/dv2kp+T/nPGISzDrc+1u71+fjSr+v82eTCggx6YfNNNil8ceqFdVRkHcSB9Z5Zi5dfPbmlTbDZCwHAekvvSrViJAtNo8GnamYnc5VY8/86QH3nFdv1uNTzSBZ5zG8OmO2wswVivRiOxHuGG1MRTnUOTmtcbb1B2gGIpleVl9+BzBq9wd9E3E1W/SPBP01SfHVLAlhKWZLYTNF62UFsQfNrFqAyWx9SAqt17jdfM0Klcb2faIxGDKw2Mk/KMBz14wqP/8IOBFUnpcF2lh1DM5ljIyza9/lS9qo8ERMcCunTLqtD5aJWd9N86OzmLMKbnDLA8BHfcRHAqo1ZvZdrKYi3aMZzbFxfqjmfImMn6GJV2P/BBkoSS2zygEWClPw+Csi20tHSS2vSQq60sWTbGxRi2iMYX7Qv5cuOpSKFVx+R0QWn6Yhh34ggtlVFABU0AbvuqNemfa+DJNse9GXXIu9cjd6aX6hYVY4xjbd5HigEZvTBhaupgFnLMjLe1PiRpk39xJw1HrTEbUSwgo8O1b9hS+imFxPS09YHoXWx0s40Mnh//GqRlZa7v/BaX3rb6GNRW9LIeaDkrJB9UKEspZY5yzY8yvJ8vKqSR4WtZhzbxnpBXIXd3wcQGgdvgN+cMo0L5OnLQUq3z+hquMtXKk3rR/Hf+8MhRbK2jRn7vhKhO6InTaWu5vui/oPxszV9o6AoghMYs/kMwSvg/VruZzv4KDtD75kPzZ3Jalrl4ZG1cPbiHA9wjPOEIEe3P5MAM5clKFGW0NKcQbWVU5HbBYz1+d/Z+UnZldltQDzlZsmIN49ZeYHVmnPx8EA93WA== X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: cf80d02f-49a7-44cb-1903-08db68d5babe X-MS-Exchange-CrossTenant-AuthSource: DU0PR04MB9563.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Jun 2023 10:38:57.4725 (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: L8/iYIBnF9kFRh0sISkmrGMkDndEDiNj5olD1q6qxw4drED7FwYjiB9HYSJrlOXkqU3CwrbCYWcqabB3pgiA3ql3OVJX3Q/b7jP02W4bI8A= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS1PR04MB9336 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. 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 Fri Jun 9 10:38:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Meenakshi Aggarwal X-Patchwork-Id: 690998 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 33CF7C7EE29 for ; Fri, 9 Jun 2023 10:41:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229737AbjFIKlv (ORCPT ); Fri, 9 Jun 2023 06:41:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41278 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231332AbjFIKlT (ORCPT ); Fri, 9 Jun 2023 06:41:19 -0400 Received: from EUR02-DB5-obe.outbound.protection.outlook.com (mail-db5eur02on2079.outbound.protection.outlook.com [40.107.249.79]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 978E03C21; Fri, 9 Jun 2023 03:39:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=e2OWato2UAs6UjJI8fVhRs+LhAWX1hO7UUaUtq9NtOhx6oow3MAzcRFVO6wX4vm9OpTS4K5r0fYwy+eAOf6cFLCwEe7jDzDQYLmdUEuVJj9cqtI6Vl4t6juZiQg3+3Ms1/l+yOFeiuXnJ9K1/tkQaXrDNmhEeRlJCzA/NYZre5lzti3cx8dCQ/ZUkGqnZSCYwlzB9xbehN0rn1rVc+T3za1xBpZKattP9PM+24dNDFCMD/DJpxhZKra9eN16gsxa9yl36urzTetPSX+wbpu+0zpEaBSy00jakW7YefwDvUBaGWb8fLBaDnyU5+j2yDl+/g/4FJyTFuS0v3GRkZKSyQ== 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=bdND22cx1GljIQcqKTFXZorRGHDlelWRNsDEt1JFTr8=; b=c4TRByrdE3EKaXX521WnytvBwBdu1ULKr9I6wkLm98e95CDvnz5j6b2L1dpaVG/T+lZ0NYsn3gHrV+Gphn8dQETBFT0caApAsEecId2U2P3d7eNwLPmpthV48Q8WBSSWTXGiW7GDlKloS8SpUZ173VsKZ8rYmA3C017THiIWOOfHPSsNaCQKfMAbQZujWsAN5h/M6NqE4U0MIy8IfjHhRFpW2O/e2MD+GK7LH4vloPsuCW2Ac/s+6oyLMWfHw0DIbCLTaHGPz7DvtF+N3bUx/qkbOyTSIgUHvtxaqw9GbwIO3ZpN5ePvknyRmisb30XAbeT03bVe1+eDCel1cxFg9A== 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=bdND22cx1GljIQcqKTFXZorRGHDlelWRNsDEt1JFTr8=; b=k3gE2+m/5F3fyGAUs5eRXq7SjdDe54BwRI7/Z53YoAXovu5dMrJ8IcOkFyCQiCA/yjuyDtyr1wBwkQwC8V5YYWLsPQ/CnbLnQiTHVhhsu1Y4LYjDpR6zioR7xqAn4zKxjkYgLK0deyCbgW1JL0oe2vGUQJQVQ8pXNgyjChJF0SU= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from DU0PR04MB9563.eurprd04.prod.outlook.com (2603:10a6:10:314::7) by AS1PR04MB9336.eurprd04.prod.outlook.com (2603:10a6:20b:4dc::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.32; Fri, 9 Jun 2023 10:38:57 +0000 Received: from DU0PR04MB9563.eurprd04.prod.outlook.com ([fe80::adc3:90c7:4b37:392c]) by DU0PR04MB9563.eurprd04.prod.outlook.com ([fe80::adc3:90c7:4b37:392c%5]) with mapi id 15.20.6455.030; Fri, 9 Jun 2023 10:38: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 v2 2/5] crypto:caam - avoid allocating memory at crypto request runtime for aead Date: Fri, 9 Jun 2023 12:38:39 +0200 Message-Id: <20230609103842.4159321-3-meenakshi.aggarwal@nxp.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230609103842.4159321-1-meenakshi.aggarwal@nxp.com> References: <20230523153421.1528359-1-meenakshi.aggarwal@nxp.com> <20230609103842.4159321-1-meenakshi.aggarwal@nxp.com> X-ClientProxiedBy: AM0PR03CA0096.eurprd03.prod.outlook.com (2603:10a6:208:69::37) To DU0PR04MB9563.eurprd04.prod.outlook.com (2603:10a6:10:314::7) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DU0PR04MB9563:EE_|AS1PR04MB9336:EE_ X-MS-Office365-Filtering-Correlation-Id: 632b7eb9-42c1-41b1-d563-08db68d5bb00 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ggFZ+F7J4wPVrQvQ4smozh8RL1F2Y678dHXDDnLATsWHDQKozQt2iu57u5NCudWcV714I81sWiFBt47rwHrzt7a1lEf94pl42GKlR4LaNfSEn+gJykp6eUYpuftd/27vs+14c7he8KWXUCD8bhAG8GFcZ13R/Q7yPwx3krBnNQHFSuMfBJnUlMloKw0gJNfFr5ZT+65pzpntDr+UUFMmvN4E99toVOgViNQX56e9IvpwnEbtzXI/Mrq31ZOJrVm6eKgFYC3DXfgz6cYJE7peNkUTgXoUsf8hAaUqVGEza4hGNbDO3bxOrxhH93wIgbcZP0ab4NWd8DLSJAY+8eBgAWNA3BGMN/i+V5nOFcxU9TgrCKS6UqGFwVRaq80Hb2GvbWjxKr2qSJLTvj6lh643vTpZ80bBVz9EH9DFC81B/Kio2fuUxZQ6PQ8UHqpf0G3W5UysZs038/8QK8RKq3Dy1ZUKQe8XGkjvigWcoRnAExdlBAHDtnHlxocAicSA5+Djiz/7Eo/HUugxYBGiVvgh3aQv0Xq6NJo36IYFggwozywLRIqJag0dvPzyiDKNwtEdALBVoenas8DNbwPhjyiHhGupQghDycLlcrWwGSQxR5fHm29X1wZre1vdueCg/tJK X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DU0PR04MB9563.eurprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(4636009)(366004)(376002)(39860400002)(346002)(396003)(136003)(451199021)(66946007)(66476007)(2906002)(478600001)(4326008)(8936002)(8676002)(316002)(41300700001)(5660300002)(66556008)(6666004)(6486002)(52116002)(9686003)(6506007)(26005)(6512007)(1076003)(186003)(2616005)(38100700002)(83380400001)(38350700002)(36756003)(86362001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Y/DWWsJPCb9ZkiBifC0jZU7rqir0V+H7IUw6fIamS5v8iIL61j2/2O2nhI9ZSjaULY+9ttDfnIBHc31svMx6QHcyFYIc8F4UHGQO7Ly3CAsclUUvCrY1CjxgQttDENCt/Hb+VvDosybTzcNPXWEo370lHJ7SxMDnXFYdUz052Eq/Utwq/5Vys9/K9kV7/f0BUUokT5kOkc57NRyofR7GvnV8/LzwFC4VYhIvxTQZus7A9Db263acWVFSv7panwj2ONuqM0usMDc26NwWsJ/rMcZqQO+aFGrFM77ovhqkSw1dfeBhKMT8I0p/JDYeKS/u98/N9AWnntX4O8M1TqU2CMA61V9dXqZJPaLDaeVutSWy5fmrWooewDKwjcEawq7ttSJ54l3bjl04Kbad47p/1M6NVWcuYvESMgrqXyZOfOAwMUMdkIw/oqy/BGHRZa9KUSrGx9VkU9SwGJF//JSOaxotEtJbmDuTh8OPmDq6QgoGGWW4/5FNc9Z0aIU2WsfgaG0z1fD7lqQKO3btHGMVVUJMtqrjwgRNgQC206J3Yn1rXrqgl8C1BhDraisGUzStY4Lx+BICl8ZsuSznat/BRvoMZPzXyKgi1jIicj+8m9nxVF/6Ym1Rxg+5hSI/EvvjgM3tHNHi+YJgka5zpW6IojJPQMAtfL9rlOuhd9j5dSoqMFWZ9tStjHkT2EWv8TZg3Zn+MvNPBCPoZIMZ7QmH3Pz/Ej2Jqd/o91I1GFHY/S4uzQvKos98lNwf3aOuXeI5GHKOPhjVhFAnRmtEBRElbwDawlcq9Lc72S/BTHjxUjJRjykk/4QMxMpi1fDYtn0wkSuXXkjSumGKBCX8vp4itrnGNZXrE6i0/AzLaSs1KtVVKdEfzPhpt8NIxgU/EhV6kR5kqzbDf9EVrR9WbZSv6YXaGRyAHZztZwHH+2qJHuFFHZ/4zs9j/gcm5tvO3GEu3cw2rI3/VYZer5oU92wj6VDL6MXSB1NdqxqcvygliCtLDsVZ9SPTKq7Xm3gfHvU5AE4zIypmZW9YkhuW7E1H6wQA7mw5Z7MOIONKlgSMWo7GvGIHRo71sIKWkZuN6/XSjnrQPlLIVnQJiczX+UxrGHXQXL0WFq3G8cbswVUxreurnnQCp2kzGGYOIw5FKn0U/ob/oB8/dXDBqpX0MWmjUs6+nrglR80VmV9dKFZ/WWbTpFaaQ0pbSKH21HjazeqyiHqpcefRNE5CQL5AmDUbR14Ssx25pEzhs0W11wl7WTj/BFyMAuA+SyXkQHGeF9iXV6DQWVu6imkGO5AosgjjqVef5OAbKltpWDRIfeUjMb6UPfQUa6+lMiAe4xGs+iuUFzqz6D66wPZBilsSAufWEsWoBooMGw7EFvmzVWzIy43EOPl8b/hO1INzGPtF7YYy26FSVypuM3awBrAh6I381wSpU0CuHDbbnWlPGa4GY8nX41oGKJ5R9V/t2uZTNWwkV/luDGhgwQgJ/afH44ojUfRi6qXcDEUmin/FsLQgrWtgJHrbQgoD9VbFX3mcgQWoyC4sbK6638ulEe3D89q7lgHbHsZX3V6LQmQW0Ca+qlfGZPDPB31yojATZrZfJSsATYCoH4g44h9t8V3tUXHf9Q== X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 632b7eb9-42c1-41b1-d563-08db68d5bb00 X-MS-Exchange-CrossTenant-AuthSource: DU0PR04MB9563.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Jun 2023 10:38:57.9318 (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: HYXNdH+ikMu/5kXwsVT6vmeXDFCBiGDxvzCownM6VpQLcz7Z3Fjwk5OvKheV4vO135Eq7rYuayfAy+z0BCoy+EfZNt+askbte4XeUNSwdtc= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS1PR04MB9336 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. 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 Fri Jun 9 10:38:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Meenakshi Aggarwal X-Patchwork-Id: 691239 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 C9599C7EE37 for ; Fri, 9 Jun 2023 10:42:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238108AbjFIKmA (ORCPT ); Fri, 9 Jun 2023 06:42:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42580 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239804AbjFIKlW (ORCPT ); Fri, 9 Jun 2023 06:41:22 -0400 Received: from EUR02-DB5-obe.outbound.protection.outlook.com (mail-db5eur02on2086.outbound.protection.outlook.com [40.107.249.86]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 33388199D; Fri, 9 Jun 2023 03:39:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=msNKud5FBjIxwf8EVvhf0LEIOuBYoo5BuB3C7jHw2CWdtWnwgtFmRjk2LFRzPaZMmfOpR88UC/Y6NSw/3Bo6PGoIu8yfRYxh3fNM2w7Mpu1d93cEhWDJWC4tM++ED8aG7rDclD2DniY524Tn2GCuHs5pakb4mH3fhKgmwAKLN7ruI9q6VpunYNcYuoWSUZ68M2SB/IPpsiaen3XnOsl0IgilTAp/pFL3+eZZN0Ba29VoGjE0Kk+w4DTogQIDcKXTzbUQe+IbNy9vrh7iATBEw1C3bKVVxX/QmsalehC5WvqtfwFyaYvNBz5kSpkBAFHtdZ73KaIOvA1p5Waga9vGkA== 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=ctNwIKlhQQ6tNgxnawD38dGrU6vBzcG9uOrix3F3Uwk=; b=YrXb0I2GsUQFUM0H3QtDm2h+ckgqk8wKav42R4YEbkmNRFzi6xqSPkltLPDDw3Q4Taw+Zg6Rp8LV0s7qjTV/IndVsfNlqqeb46p5AubsrLROWQdfH4VZ/Mqdzr0w7BW6ROrZLXpe5GLFXl/a15lXVnq4vSxiC+hm52akuEM8xRW0bY48t4CLXhy1AUdnpI51U2aw1dT0y/TW2ak9OvoPYDdnMi5jRdyrOAW7u0DQHl6vM59oCvzGveerIvnb+9v1sFs8HMXMxxEI36a+wxVswnrjTFfL7yTFGJxUyaFKpIqQha2P1YeSmhavAnt8jbXq0HNPA8er67B41jS/3YW0gA== 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=ctNwIKlhQQ6tNgxnawD38dGrU6vBzcG9uOrix3F3Uwk=; b=QI1jRwJssN2kO/12X3RlBTRqXsYt/PiO5l294C7qpaRTahLHdIDiHfGZrxwD/X7BkxJrMlOJzAWYY2Ud1YtvaJYtsIq0yyLOHUTKjkF279uoEOocYJrjqtfZCSTwz9d8IsVQX7F9O3oseiCIolVGuRYoY+ZXguZdsapLNJ/S5z4= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from DU0PR04MB9563.eurprd04.prod.outlook.com (2603:10a6:10:314::7) by AS1PR04MB9336.eurprd04.prod.outlook.com (2603:10a6:20b:4dc::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.32; Fri, 9 Jun 2023 10:38:57 +0000 Received: from DU0PR04MB9563.eurprd04.prod.outlook.com ([fe80::adc3:90c7:4b37:392c]) by DU0PR04MB9563.eurprd04.prod.outlook.com ([fe80::adc3:90c7:4b37:392c%5]) with mapi id 15.20.6455.030; Fri, 9 Jun 2023 10:38: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 v2 3/5] crypto: caam - avoid allocating memory at crypto request runtime for hash Date: Fri, 9 Jun 2023 12:38:40 +0200 Message-Id: <20230609103842.4159321-4-meenakshi.aggarwal@nxp.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230609103842.4159321-1-meenakshi.aggarwal@nxp.com> References: <20230523153421.1528359-1-meenakshi.aggarwal@nxp.com> <20230609103842.4159321-1-meenakshi.aggarwal@nxp.com> X-ClientProxiedBy: AM0PR03CA0096.eurprd03.prod.outlook.com (2603:10a6:208:69::37) To DU0PR04MB9563.eurprd04.prod.outlook.com (2603:10a6:10:314::7) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DU0PR04MB9563:EE_|AS1PR04MB9336:EE_ X-MS-Office365-Filtering-Correlation-Id: 0c21c45b-df3a-4233-5654-08db68d5bb48 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 9YV24GWiXOPJ9O+VHgHPC3Cq9rL8OC7/YwmQkUcL+zRQyIGy8miiMTwhKbh6xEkZkenEjd7KW1sxmaIp5zBNz9zgpJOpufY453ND6QLP89T6RjsMY1pzJMVs9GHokP11ltdQcnveaE8hN6Czqsi8RhzNy0p5o+GzP9zWXsw9HaOLc+dL9z7gA7i4VBfa29QOJQUEF+tOP2Cu39CUSmusBYkG/HCDbb1o7OLZkgSQ5GJa3OXHxeydLnF57iQ0inK8CsBIfjpuAMHOq4fIxA7YGHBQhezHJO6RwJ8i60XykUvNeFI9/hGRaEMA1bWAltKrMdlq43bTzr/1h2uC5+L8YEYqnu+xjN3IWchUrWDeIQmzoT+U1lyzM/29pPH/tKPl2tv3mOg9nEFAxC5dcbPLvkle/dIeErdieXPcGCWBDhQr8/+hd1Yf4SUHC/VJiUTs0sJvpOIs9Uy26daWm9KMPRPRLBKNIJY6PAFr5vZiQmPNc8eAA8iNlskG8kpWyvB1W5s69mKFdvcbsRe7yZxNYLz+yl1EU6+wWFtTDA6KwPGYwjv8hCqrMmNyR8K3shoLZcW/aZMknakwjMbeFabdhFstJkBTkupCPVIxjkmFND07U9p9l0lgm+oaQXjDGfSM X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DU0PR04MB9563.eurprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(4636009)(366004)(376002)(39860400002)(346002)(396003)(136003)(451199021)(66946007)(66476007)(2906002)(478600001)(4326008)(8936002)(8676002)(316002)(41300700001)(5660300002)(66556008)(6666004)(6486002)(52116002)(9686003)(6506007)(26005)(6512007)(1076003)(186003)(2616005)(38100700002)(83380400001)(38350700002)(36756003)(86362001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Q2fMPtMh0B/KUvll89t8ng+OZZvd89mG0VLEqNaOovLDti5prZq8ikBZhGS+PachpTaZMCrrH6k1D23DA3B0HRieOKTDeGlG8P8HgZ1nYPBfC6E4w+dMUxz6jR2lZT9eZ6N6BnwK/PeGx9/7FVzsgoc5Fc8EigFxjtSTFH0yNgA0K3QXcoGxz/d1t1gPoMthZT/9MEdzo/9NtTmh5H09JgvAqnVI9aDh0AbivSobv7s6J9v510FkwmMal/CQsNYUnLzWmteq/PjSpXIdF0BHp6A7iKU9wp1mmmzNPir2WdbgjSR/UChWBdN2a9rGCjag9mLgjbnZhxJsRXOKDx7F5J79Lw6AE43rLO0k2jwecD/J0wUD4j3XldqLwsmZW0VXlWzgjEBx1Dswn1jbWT5TC7H+yxGwD2xCJwTCBEvygv4Ys3LVvDTiCYIeGFo8BADGDTvzIlVp+mKpACVP2gln9ECgPNLHPqUsdGhM0aXMkkBYCiJBmQwaLC7zGde8Kahpyi3uhVinvlqdQdjiGYzRRpzTiilvq9JVutx1YYQFaxFsExNixW9CGfuwIL1O4clPM5gS/p8WCSj0nwfWctTOBIZUFP/Mfmf5tATPmR0P9fio64N4kb9fug4EsbQmLWgG9jD+IDBYHpv90RD20cWiLuCDsoBRE01j9Lo/QACWvvyM+OQmhlHMSJZjYidCfkEftIylwpkbyAkKt19UpS++jLsXpkJxazWTFaWTpuCXQ9IkKf+YrTGw7fdx0CRdrToLr8/OWHVz+MHVovr/IRm7fZDzyiLdZaBGohzG5Di8JnpPxZ6br7+JkPF1AjBdxp0ayGC01+2B28pC0/4DatPw9E0HXH774cs3tlyqqWDqKvdL+ZXu0bHqTp82nSM7iS0SJg+qmgVmoNWj6sqmq/Y8WX53KxisAGNONh1qVwdTd3NhTuVDQZwBTxy7NOSwbFBdYLq1LmvXblVvxSLzhYi/83zFDWEKMBtTa0UZSg5k9ZP9/fywqPnNKZbUMWgCgcIhOcN6RJ7p6opvUNbqaTOXZpe9DsmdVR0BnwzIHp/KwlW9kwJ2cNNPDtp9kutX6iafBWQZvGvflrV68hZK+7q5X2v8YW6EZJf62Q8OIsMWYcAnRqoj7hM03Dz2gJBBNpwVteu50hDAqZ75d2MvM4Vk0+MrsN2pHSBfYqjuPjd8rSNC8Xx+Z45cFKI9Nr0/7faU2+DwRQJdkkeDWdngO000b1yMDeXdquUreA+BRGi8/2TkGewElUAn4cJ/ywN0ChUrBVuHg3F/UPGOQZcwqkd+Bx8TJ8d6Ydx1mFSSNrpGebarHVR2Zu4QBCPfRxsucIFNJDv0w2M43p+rl+ER7qTWTC/QF6KkzgiEEWe59QcATVEPop01qv0GBUdSFGFLp7f91ay0I98bo9c9smwOzCQY/5Hgk+yF3mMfTixo7Jni3/0A5peJqsDEMfD9vVyvPHu5TOzCxWk8PH7O949HkxuPW0PbDJ04a4a7UYePAfT1KkZrFlatmx7yDBW/GPoZMYddkbAdvLZ7QVNoFCPrnnOEv909Yk9H6jR01Gr3ryKkwVopXz4fuhzTAK5Tg+f7+4Elzi2M0swKHejAMf2lEg4pTw== X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0c21c45b-df3a-4233-5654-08db68d5bb48 X-MS-Exchange-CrossTenant-AuthSource: DU0PR04MB9563.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Jun 2023 10:38:58.3933 (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: ANHN/r6bV2navxUEwz23inR3pzrQxK0iLu1Uq0Alr6Ek2kpHSJG1oUIrLD7LsbE9Mx3ZDjuZYVtAtJ4+ckUmy5lQEv3OQv4ufFxcSKqWjtg= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS1PR04MB9336 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. 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 Fri Jun 9 10:38:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Meenakshi Aggarwal X-Patchwork-Id: 690997 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 BC898C7EE2E for ; Fri, 9 Jun 2023 10:42:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231332AbjFIKmB (ORCPT ); Fri, 9 Jun 2023 06:42:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238242AbjFIKlY (ORCPT ); Fri, 9 Jun 2023 06:41:24 -0400 Received: from EUR02-DB5-obe.outbound.protection.outlook.com (mail-db5eur02on2079.outbound.protection.outlook.com [40.107.249.79]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 35EE73C33; Fri, 9 Jun 2023 03:39:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LPts9aNUWX+lbBiAanDP8vJerXLvZHLnLPpUP9kP32LpPzfmBrbiuqa3hnPvQfK+o4HMGCxDAa3PU6bF13I0c2b55aTdb509w7fFLPoaEOob1/5sxKRC4XRirhIfwRI33zCj7NlxfW+VKxZMkWFqaMm58E6RRXwMacZ7gvRKR8HqZqArFycu4XXVifCKJXA9bMqEP7K0Brq9HETEDLmHsFANzHIasWQkfvV7+onQvjxeUDZmho7chLJkBt78FjTPySNq24hrn7P9q1M9GPc/MsGw7XTahsslPNFw1gNemeKKyiGlNCgxcf/1n9VeccTN6qOxg+CCoFW0a21xwBnyXA== 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=vu8jzSMkF9yxfT2BNRjd5cCY827hkMYZdemRxzoK0Xc=; b=VSIbupQFrvIWRaDulxBCXAfs06BAs1m43AhQx0IzbC4Bbjw1i6i/Vr8jvmC2TYwgnGqxFklmg4OYZcSTMoNeLpD3ESThEpgL7n8cfCpUOLobMYAtcKrDbY3OqsX6Us2mvwVQ31qF2p0OgZ/MjiGRh7fQLz6Bf1KFHSfT2Nf66Le7kE0CAyF1IJhi/KY3kn7u8CkJ9B3oGe+O9N4DhJXBvjcQ1Hy4/6CRxdp/tPQuo1H/r6ouiMR7IKp2896ewWzG4GnUkZMZjRH/CV8EWUNIBBq1JP2d6diDNK3/lYeVvm+H0RmxFevn3yWLVQJtF1d20ZmfTVCxTpvYnJoLWYW8hg== 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=vu8jzSMkF9yxfT2BNRjd5cCY827hkMYZdemRxzoK0Xc=; b=RoHva5H7u+hkABe+Ndavu/+99IudY4Zw1G8UGpWsGe37+EZU1NNlGAhA0o8ugTk4i7mEkm2GgSSRJOPkWoTTIeHzNP69OPrgcWT+LF+DlUfzcWqjW/hx9pKHGN7X65UO+wIlOsTU01WXkSohQ8IvjXD1Ls6AuLGgYtqN5Bl8dgY= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from DU0PR04MB9563.eurprd04.prod.outlook.com (2603:10a6:10:314::7) by AS1PR04MB9336.eurprd04.prod.outlook.com (2603:10a6:20b:4dc::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.32; Fri, 9 Jun 2023 10:38:57 +0000 Received: from DU0PR04MB9563.eurprd04.prod.outlook.com ([fe80::adc3:90c7:4b37:392c]) by DU0PR04MB9563.eurprd04.prod.outlook.com ([fe80::adc3:90c7:4b37:392c%5]) with mapi id 15.20.6455.030; Fri, 9 Jun 2023 10:38: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 v2 4/5] crypto: caam/qi - avoid allocating memory at crypto request runtime Date: Fri, 9 Jun 2023 12:38:41 +0200 Message-Id: <20230609103842.4159321-5-meenakshi.aggarwal@nxp.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230609103842.4159321-1-meenakshi.aggarwal@nxp.com> References: <20230523153421.1528359-1-meenakshi.aggarwal@nxp.com> <20230609103842.4159321-1-meenakshi.aggarwal@nxp.com> X-ClientProxiedBy: AM0PR03CA0096.eurprd03.prod.outlook.com (2603:10a6:208:69::37) To DU0PR04MB9563.eurprd04.prod.outlook.com (2603:10a6:10:314::7) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DU0PR04MB9563:EE_|AS1PR04MB9336:EE_ X-MS-Office365-Filtering-Correlation-Id: 106823bc-1a47-4941-9b89-08db68d5bb94 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: rEd4Z7mLBuJzI1DerUskAJIYiIb2WsXN51ivEBYoFiXfCvreXsXTxHQDdIFS5oNBa5eYduRC/DMoJZdZdNgHP81ura/Ze43iJGViyeXn6pbGD3xKkD3zoV94VOV9TnT3l1ycreoSERFw4KTyGEAco2XBch+v6GyYw7YX9olWrnhE6ccKoitjDuiqj/1n4Plypu6E4X/U9mdGvMEsiaVq/xKwyxiq6cTJrvh06b8NxJHgaQICi4sFNXW5GfDNKb8oGfENrh0jBPRgJ83pSGzWsFzv51ORkPIxgfKk+pOz5OyAnAm2DUuth2erWIGNAGVHdJwW5wnyx76AUC9zZ2QL6e01GHOxlfGJRAlqdUp0eihrwVq2qdCz34xz0k/T42FSXN4Q8d2X0By3YebrEJOLmVMozI82R/udB9Y7MZwkfYFldWvLG9pQHT+Fdg4z9n6evte7BeqV7X1ac8YkiAaKETOwEOfZAukyXgdogCeToVdjc81KE6UpvtEiKQ0iDxHBE+xVgaqEjeT4ki6NX/7qsVHQJP8jxoupNXDfUhfPsZG2anmJU8ouJBLZsyqnx6IhXjFzBpBV3UOXp6LsnacX1d+NETxICbgVApMc5bT40EMHvg1xxMtQQdZcV75hZ0u0 X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DU0PR04MB9563.eurprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(4636009)(366004)(376002)(39860400002)(346002)(396003)(136003)(451199021)(66946007)(66476007)(2906002)(30864003)(478600001)(4326008)(8936002)(8676002)(316002)(41300700001)(5660300002)(66556008)(6666004)(6486002)(52116002)(9686003)(6506007)(26005)(6512007)(1076003)(186003)(2616005)(38100700002)(83380400001)(38350700002)(36756003)(86362001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: d+vxV9zCtmEE9QfYpDAwtrWMwTJSLBQcCuYF3UbH6bx3F/7VwIez/grElboc7uZKV/gdKka6i0QE5slQV8fBqR6tPwPBcGT6e8RH3M57Z6NGD9sdZV0S9oyWzKKafcpvFdQ2wjFKzcHxCGN0zTRgL+uYgzAFSY/eTKV/kW7U80xId27X/Gdrs2E+S8+ZC5HUVqZpBgnyf/OOmU7f81leRT8IqHO0K0uGGMBlTple5Opt6mtYXb3HmOPk2DVQBcxou7JbdUa6e/8TnD3BUQYRklC6sSaDc3DRSNr4FoMtXHEvf/kb6J1war9TvGJIkoxv/Fxxshdshdb1txwq62oKr4cX6IK+AJF0AvD6tYatQ1TVIXDnupK5khxZ9sBR8byoa6b/RmUF+8RzdgfLi73SS+nMSEqoSdEN9szeqsIKESvmV08LRpBFfKq4YMq8jlU3pVfP7fnPg5QrpldHb2kUagOI7pCeCL1Uq/piQJInt+381DtOHXQS7j9/Sd4a72VRpb7i/Q+a/3S0xpC5ZkBiuEYZrI8tUMxLxJUkmU/A8KsR/opP2EwywcRgMY7N9jrL3kUoJ9kDpWGe++iPNJOvITatYf6S9hdO0wMbyfs8/fKow2YVHPHA91Q0/xJDG9D3NvGZcE0lUS/SjR/bbpVHMDztQuS0HdnUNneLiEfDHG0FDN0Ltv3QMlNc7yEd6EJ8M6Gdb7WtmKP1WrnKkO2ewFWgQXhxlHkaMMthU4RSHnLEWlVdCLkMLyayZfJNN4bjhfYG4aFNAlmJMpv2jEPHYMq/ZdZG9EfM0NhguJjKBM800fR+d2kMsAGL4PbW8s9Jq40e9NhPYmKytuKSGoxb+boydMi9skvdXszBc0tnHR2m/8MPLgZEmcy/Vp4+yjpq6RHUv6DC+7STU/ZE4GwNaRw2UqElcuhHk1EdTC02aNtw+t2L1O3y3FlryTLMzpR0aC0ZeP6b2E3IXOSP0hHblWpZ7cG5ayO4zNTYT5+vjOIwPTST4IzgXp7c5rLbwQSxPSdJvvmspJRtE5wQEz5MNlydLoQwJ8AkrCRestwC0e9aKPUFN0DUeh3CzYM9BdAfbpxX9+AHHw35cuW4PhrULrK8UMZxT68cloDdJzDVudmo8qg4TI+dpmgfv0BG1VSp35cpuGrrkPhaoaaCsMvGbQhRsFetV30pb+qtsh6h0evPy2IIv9g1XqoetPYZWdEEFfPDrZAnTe5/+H3iL4gf0D9VZvHYzKfgoqQhdDGJmckWFTDPA836jw6GCSQq9lOP0UnarUv/tMp224cEmtqXSsormcyhvFusaWqIytUG14djkM419iNTVBRZdOiJmo/p1RpoZiKL22mRpTZOMLOXK/iHUsqHMwULqPU58gOE2flzAloEaMu8LhI7SFPphtx+ZOxqGhxpJnpJi7UbSHAGhtmOXCYHTYo2mpuk2q/14eoYgmm1MlM6L/H2EdAZ3noz7+hHHPD+n1Q3OzytvsC5nA3xNtKVpXWeUoS48E8wYHUeuLfvIolk/dekjcbyf0NSSHtvAKvPv7h6lS2SDIgiv+EDLycdp64ehSYGwVBHAyfcy3eihGjPic/c5Waqyl9UmYBXFS2+wk5i4muDEApjhw== X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 106823bc-1a47-4941-9b89-08db68d5bb94 X-MS-Exchange-CrossTenant-AuthSource: DU0PR04MB9563.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Jun 2023 10:38:58.9047 (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: Y0mAe7uwMzD5gmsSkj7kHSshL/SEuuZQ1M5A8HmyHKw+nhQMLxRkrFDj7LliVIepKYL76KNTidDnB1rIdEZb8GMMAa+uFGwZ5fgfjU7EZuk= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS1PR04MB9336 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. 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 Fri Jun 9 10:38:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Meenakshi Aggarwal X-Patchwork-Id: 691238 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 284A5C7EE2E for ; Fri, 9 Jun 2023 10:42:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238680AbjFIKmR (ORCPT ); Fri, 9 Jun 2023 06:42:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41074 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238701AbjFIKl2 (ORCPT ); Fri, 9 Jun 2023 06:41:28 -0400 Received: from EUR02-DB5-obe.outbound.protection.outlook.com (mail-db5eur02on2086.outbound.protection.outlook.com [40.107.249.86]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 19A853C3C; Fri, 9 Jun 2023 03:39:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LaT4seaM0MsANIoU3zfu846Xg9UAqglpnMdDlWSu/bpgB8/uEiaxAHJhGbUxKh/V8FVOIRaGttzSGb9V14I3YHnurHfhpjWFf/VKRqyDu5ZdURYhz18RRaMBsVWTHp8HKAVoyhRW7wqOUt8VszwXndFGL6OkaMrnY83Ti0iBOseb4/Wg8Dg2mjV/puOvd5dW9KxTWg4P+SpVwCky+6dQxavAYZ/v6YzYqKlVEIR5whgjRKy5o63M21HoU+0sEUFaK9d92fDdxMcfJElY26NKL59GTNMIew2GWPlLSBnpf4IH0Y6V/nLPRcmYSkJnRsQhdoMnsXws/w3VMQX7WY8QXg== 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=JXVHn853zmpa6W6wd/J82/Gj0qqyzRxVurxfR2JJkfE=; b=iLvyRB88UR/q4u/u7p9fpcQ+PvjaHNQh4ixErQ16MNsNO+rwZlBKQbyc+5y/cDTSn9R+dz52PjvgxpwsUtNdC9QotcwkTT4MLoDFziWmjLfBijKjn5LjkTWlLtCZNIF4tMKNzGiX7tyPFOW/RVuAIROuPApM89jJpZU/+r3+wE8i0PzsYm1jc1l3B/70tHyFqn+3trNizwWHcYM5kKan6WI6t+5GbGMeyIRCaZtlzrBrHREyFfvebqbjXCcK73GkYcJdvfO6bWGtR0yBD4A9UbTlulY2ETE2TwobBUpixAtDoMUf603OiBvTskGC1np6gZksPPPUhQg47vZ3SbqKIw== 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=JXVHn853zmpa6W6wd/J82/Gj0qqyzRxVurxfR2JJkfE=; b=KRZuOLvHUNX7txtZbLxc3H1F+j6VKS8mp0FDmUeb6Qr1yt3q1VxbXR6frdvV2oewOWcI03+Wa5ZE3O1CO0Mblw/jhMZ6BIM9sIqM+ofwj+/gGdQWiYoAqHDZZsL8Jgmn0nwv9FbWHbIyHXeKCLPPB5VshCzfpY7IN5Zns1ar6hA= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from DU0PR04MB9563.eurprd04.prod.outlook.com (2603:10a6:10:314::7) by AS1PR04MB9336.eurprd04.prod.outlook.com (2603:10a6:20b:4dc::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6455.32; Fri, 9 Jun 2023 10:38:58 +0000 Received: from DU0PR04MB9563.eurprd04.prod.outlook.com ([fe80::adc3:90c7:4b37:392c]) by DU0PR04MB9563.eurprd04.prod.outlook.com ([fe80::adc3:90c7:4b37:392c%5]) with mapi id 15.20.6455.030; Fri, 9 Jun 2023 10:38:59 +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 v2 5/5] crypto: caam/qi2 - avoid allocating memory at crypto request runtime Date: Fri, 9 Jun 2023 12:38:42 +0200 Message-Id: <20230609103842.4159321-6-meenakshi.aggarwal@nxp.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230609103842.4159321-1-meenakshi.aggarwal@nxp.com> References: <20230523153421.1528359-1-meenakshi.aggarwal@nxp.com> <20230609103842.4159321-1-meenakshi.aggarwal@nxp.com> X-ClientProxiedBy: AM0PR03CA0096.eurprd03.prod.outlook.com (2603:10a6:208:69::37) To DU0PR04MB9563.eurprd04.prod.outlook.com (2603:10a6:10:314::7) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DU0PR04MB9563:EE_|AS1PR04MB9336:EE_ X-MS-Office365-Filtering-Correlation-Id: a2470699-60d5-447b-1c9b-08db68d5bbdc X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: eHxnAsCQSh3+K7yr58P2MWY/fQEXcSkM6PNG7ALrEkNW8GtLgxyjEw/bxeHeWm1Q+vMTxuPMvJZF3mhfa8AtOjAODXN4dkchUoXp5c5/zH/paL57kDzEK+ZM28FK6836BUKPTD1yLd9/jkORLgzFoKwBHd7jAT5Y3ThfLSqG6APKOAICt+XA3S7zx510KPg4SjuCdXXpTnJ1iRnBQt/JNQAgFeB03MzrWr6dh1QZKp3h8fINpNjah6idZ9IajZgCQOiLwv/uYcmk5d268mcWh7pMMLLfndM6j0u2L0ia6+YNlC0ud2t+tMRfl8G9yV84CYTL5ZuRjUCgyJoFG6ffxCZCL/IizGpKvMr/ML4dr6uVLTmewIk/Yu9lAJCMCx1VL8e+lB3Qj/wZspdw2MC4DmDGIdgKaueNgpD9rGJURL5rxIts8AVQ/oviKMUwe0vDj0hBr45AFGuqN9B8uSXo0jsJSNg8y8Tk58sQMYslJY2SYgF5GgDUiTwkq0I+Q7vDkMXestSAaRJKDH9azHZ3Svihz4O1MBeXXWOzTQdmvORJEV9/U0JZnLykxk5uVCoHtAEfp907PjKhTOYlyy2oFLzTOlJTLEnSXbkPfl+Aff3eAX29j9tu4ayHaXDmTYj2 X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DU0PR04MB9563.eurprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230028)(4636009)(366004)(376002)(39860400002)(346002)(396003)(136003)(451199021)(66946007)(66476007)(2906002)(30864003)(478600001)(4326008)(8936002)(8676002)(316002)(41300700001)(5660300002)(66556008)(6666004)(6486002)(52116002)(9686003)(6506007)(26005)(6512007)(1076003)(186003)(2616005)(38100700002)(83380400001)(38350700002)(36756003)(86362001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: qyGYzrcHD9zbJrxam3j/tzuJwomiZ3o0um+sQSCSndNZEIu96WmJlcydeir5CD5v8mBnJq50qRU0CoLCxclsIKYB0YACkku+zoNJxZY/LZDZt3V/a4zgbjEW8pNQ9w4pA5jQCNI6rBOIYG10ge944iTmtmKcfoWFwlv0mUJJ5N8MaY046dbg7Fg27A4qKyD8numQPNaXoKvi0D4TwUawoLVBTvjE6pAHY3KZtvMHUnb7L+NqoqmHsztuuEGj9JJocerwaPizCEOOXLU4QcAXilZL87pBNvELZWXaUe0bqzBiI7GwlqA7as6QqiN1Xq2pAYi9SEcE6UHMJ+oAdUj9FULB+LZ4oRYbKFHRIckqLOs23T5qWbd+JufSNFsfNGJOPrxSHtSgBT2cGX2UOhVsdEbsSYtFlhV/UEyJjY/gd27HVr+sEFZTfzBsBJLqb4nrFr1asdN+s4U79o2ZqxhwgDvXwwFMxnBK+SkFIUELH/76Obwz0tuGRvhbMIBwMIpT6YH5uq66Hitvv3UtHdI2w/pOEejjbHhWUpqY1V0ErFhZFiBO+Mbm8pa9E7bEuGhpu0LvmMDuX17K6Ng5CMNZVYT8H4mWH1AH/lDZr7wC9A+KV8YckiK5jKG94Ssh74aoh9icXCmTE9a+gig8jGdfv8Vi4gQMTh9eGBETfy8HB4hfqnHOtqg54fQTRsATM1Wa/E6dD5ytcEctAFm0mdOKY0ocsabySUwkTmEoISWfXECvM+Pkt5E2ZjXUjzeD8xEnxxlKfF6XW/QaLxiD0jvCu8Kh50X0B6zv41Wx5QnepbLR9dOYZEipWWfOMfiSQLrSOn9U5VUzR7QHr93aZNguxAVVl2+l560Ea9JbF7C22spzaEz/m3hGHYjXLMwDLBdRscHXff11P6kIMZSd5G/jSEMYOvHYLzl0HByG+bHyE+3TSq++RtN4SdAbxEMw1pA1h8VOl/erhuaovmNlb0dR8liFwqrrqDA2zaLH65w+YxJ7vrL/ToqbZA1AlNzqQpQAnABRcuQrbrp7hr0bb95Xr4CmkVPyrBXudMLz3kdCSbpSisJqaLBVEGFz+D5n77qTs6CMx/+z9drTS3bzHqjhYhV2oMr8Y7g8q6GB39D5MtAEh5aWZ9lwCaH5wrNErXT1Hm1MYrhf5rPI6oeuYPA38hLYeV5tpnHDVfpASOpio2Qv10wd0aaDEI53SAKylwhKSnTsObgxvn0742bo8Q15VG8sbDfAtVM1PD+ofXcRxdI97EYZJXviEHtFvlF3AFdC7RKxxxKYOWVzw11RupyZi8U7m2E955f27B85q3OVeZT82Tjkk6WxZqgZhpYvUwXm4WkehsU8YsZbzxKQaqfxvE+izyQSW659YE1h+BCamRwflWhxj530c27unIHhX9HJrwi/J22i+kqUjMCXg9vx0fKlWDwjmpBfhymLe0ROb+/VysniA0yBQg0ihwgQTS/+xG89LONBSgMkjF77FBa5VZrTdvZEs5C9Su/qgaLUw1Il7MvE/msOR2HMM5l/raR25UO4mU5O7ZWyJiWD0LPAe4ng9FK0vjdWW+OM/w+IR+juz9ag+USDoXQcCf3UYc270wlxMauqqSZ3h6G9uoDnvw== X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: a2470699-60d5-447b-1c9b-08db68d5bbdc X-MS-Exchange-CrossTenant-AuthSource: DU0PR04MB9563.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Jun 2023 10:38:59.4017 (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: MLVei9ShhDtFV3pB/ggdnExucW36ZS5PQGNHTKwwkq4BnVLTp4CvkclSfD8EbFJ+lCDuwpEEj3qYzN5YzXd9jVBIH/6tjnMlT5QAHcXoqaw= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS1PR04MB9336 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. 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[]; };