From nobody Mon Sep 28 13:59:31 2026 Received: from smtp-relay-internal-0.canonical.com (smtp-relay-internal-0.canonical.com [185.125.188.122]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 404F03C4551 for ; Fri, 21 Aug 2026 04:56:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.125.188.122 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787288182; cv=none; b=fBu8W5tAAE56byCpwqJKwGuRh2F3Ntg9AcVkw4v/SyXleUjYKyRhNFEbm0Etg7U5cZRVX3KLGYYhERngT+iuwNQlKcCuv5IJ4bWMpHtxqQ90YIRCw/tXUPYi4K6IoBIli6oei+7iu596ghuGlqsRfguCDfQsT6ZA8FOB5bpYlNQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787288182; c=relaxed/simple; bh=lkNrMKx37gIAO05dD9M/A6tKLLRdr7XkW7tjnvEObAE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bByE5WX/d8lKUNDUi3ZG3CDpmjFn4KpATmwwAe9bVHa+mFMeQBb8H2myvrNB9cyxK4kpwHzgqOVUf/vEDNcQgLVU7uT6nbQVz5ZdyEoCqtjUUTRptBRavfJEje5YSf0rUp5pGBGBSYO2gkjCu4ILGDwkMQp/SKObm9O29GCrofc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=canonical.com; spf=pass smtp.mailfrom=canonical.com; dkim=pass (4096-bit key) header.d=canonical.com header.i=@canonical.com header.b=XwHjjMSZ; arc=none smtp.client-ip=185.125.188.122 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=canonical.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=canonical.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (4096-bit key) header.d=canonical.com header.i=@canonical.com header.b="XwHjjMSZ" Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.198]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id 2C22744B91 for ; Fri, 21 Aug 2026 04:56:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20251003; t=1787288170; bh=gyIdXhgrta0UR+ylzsvsJajvblw6ZqKRaY7y7WPUpYs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XwHjjMSZUbZ3lY5dFrWPXEw9EHD4/5MDBScXKHZEuLTJObCjYw88RmcFWQ4NlrVtC aXfox8TgfF3dljXzu/aVhc2KxoQ8atqY1i+gORfjHq0WwFCMyWVMN/JA/iLtFfTwzx FPMkjM41F8TJzDU9CSntR1DJPeHwhit4WrQQkRGi5R7rfnHKsRFZ+OndlaVrD5uBpj Y9qs8AkmKlaJI02qOfdn70THpRgFW1pvoTUeU5WztXUIEQUuheROiWw1VH7MfYDM6h xiNXkbijzq3Lr112fn0SogrYIydnI5OSI4Ip/udOdU32fEWF1InKUO/bc2LOxhDJpI 0TmYDe+/x5aj4Cr5roWxsi6SDr4M9sloQDBlhQT05uzTyHzaFowS3lxXj5kU1Ww0R5 dz+BLT1OtxdEqMG7kIcU+A0fxIUIRCUQSJFBO3w4vqqj6ZbsWWYW2lc6xnrCSi5+rI ZJY/5JX+4WpNM0E4EdXlWMdkzyv52rzkqEhpUFuqq0nXa4m2kbhHNjkRgu9+qB0Unx Xact1VoCQqeKw75XbyMoFJgNGFgOKhGL9vTVA52uWTxOd6CeqRjC2WPO1o+SMQq4J8 JecYA1gcxmdwio4s06jRnHp12ihUVfaJAK0QPBKjGy+kF8jiuLACeEOH3XNWSyyJcV MECPRkO7MqHwsRFL439YU8P8= Received: by mail-pg1-f198.google.com with SMTP id 41be03b00d2f7-ca6bd8a190cso870465a12.0 for ; Thu, 20 Aug 2026 21:56:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787288168; x=1787892968; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=gyIdXhgrta0UR+ylzsvsJajvblw6ZqKRaY7y7WPUpYs=; b=eDQ7PVKa8TetaC/udVcoYi8M6g5mXvrZdvduGwqiRdw1iT6HeCDPRfzDVuRCp0jtNR dr7wYxCZUkdHd+fLRycpwsWfz6W2xc1ZhWFa83Q1lDq/bZH82YPmKb3j2pm3EtlIrfPw CDMPq/nnkRtycBse7V3Mm9xgSTjp9bwdualIDMJBLoRViTd3KrLTj226C9bkfMn7oCq1 POneJ9dr+kwRduNKNxm7cRqBFm1hX3kf72UvslzGoP0ooUYcyJy/qM2pt5E2lAqCJXEG S8OhykUJjuaeg+wfb84UPSfGbYX9AKgI2DXw4V1Qfvzj3PXkN5lPjVkZd/hDoWieLFtL PMmA== X-Forwarded-Encrypted: i=1; AHgh+Rqv2quQVxBTx6Ud35s0xVjPpS5Gu9yS7l6LJPaJ7Xl6GhG86Tf2/7BT5y76kTEp0istjxGv90fru0PsZAs=@vger.kernel.org X-Gm-Message-State: AFuF++kekH2Ig4CbrFv6vJKb6j/y9a4cWGtE1QjnpEuqFN5Nzf9GPexh xPXLvX1TZBpWpeMID1zNIAkjpiKcV1KN/ZO2NqZyXOUE1cPxWkvnqmEzTVld2nEGzFUeWAjhVeW 9Pg5qI52YfMD+I0SYH3hOwkqfLHiAfKIu66XYEdzkXyfLHwNW1wU8vai7tlK/OVgvX9FdvJujZ1 YESW7yAA== X-Gm-Gg: AR+sD126OMHbgI+yajaKDgQ9lNFYEiQ1bX3UNK4qXr8mNlNhKSPEYddqTjpD9RyccZ0 TohqiLJBEsj1R+8NgiQG151/QW+NybjoRYUzTXi83XM7iiJKn0wZPez1Wtg4cBzTQLGJqZ26a8Z vlbLymbT/3WZhqbQtdbtEMCVpQCYlh0jrm8XrD4fIebl4Iy1sY9ulRh4CmM8IZl0KFyUwoi8QTR FFk8eXV+ftkJ7Xw+xm2nYG41HWvKJiAXPRtyJaYEJisu/pR8ovgdgjnEIcewxuhf7ZlqVCBR1b2 Xt+bkpcURsP8HhN/AXnpTGU39EV6iOADORSZAIr/Q3QNCBzH7QvNETMGuWnh3bhbPZm3IuNqQZr o/hZhgFdFugg= X-Received: by 2002:a17:90b:4fc2:b0:38d:dfd1:7a8 with SMTP id 98e67ed59e1d1-395c33e1194mr6834637a91.2.1787288168504; Thu, 20 Aug 2026 21:56:08 -0700 (PDT) X-Received: by 2002:a17:90b:4fc2:b0:38d:dfd1:7a8 with SMTP id 98e67ed59e1d1-395c33e1194mr6834602a91.2.1787288168129; Thu, 20 Aug 2026 21:56:08 -0700 (PDT) Received: from ZBook.gateway ([123.208.39.53]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-395c4553168sm1535224a91.1.2026.08.20.21.56.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Aug 2026 21:56:07 -0700 (PDT) From: Changwei Zou To: horia.geanta@nxp.com, pankaj.gupta@nxp.com, gaurav.jain@nxp.com, herbert@gondor.apana.org.au, davem@davemloft.net Cc: linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, lukas@wunner.de, changwei.zou@canonical.com Subject: [PATCH 1/2] crypto: caam - Fix wrong unmap operations Date: Fri, 21 Aug 2026 14:55:54 +1000 Message-ID: <20260821045555.806471-2-changwei.zou@canonical.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260821045555.806471-1-changwei.zou@canonical.com> References: <20260821045555.806471-1-changwei.zou@canonical.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Both akcipher_do_one_req() and akcipher_enqueue_req() call rsa_pub_unmap() regardless of the key type. priv_form only takes values FORM1/FORM2/FORM3 with no distinct public key enumerator. Use key->d to distinguish public from private key operations, then dispatch to the correct unmap function based on key->priv_form. caam_rsa_set_priv_key_form() implicitly relies on zero-initialization for priv_form. Set priv_form =3D FORM1 explicitly at the head of the functi= on for clarity and robustness. Signed-off-by: Changwei Zou Assisted-by: OpenCode:claude-sonnet-4.6 --- drivers/crypto/caam/caampkc.c | 43 +++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/drivers/crypto/caam/caampkc.c b/drivers/crypto/caam/caampkc.c index cb001aa1de66..840271840cce 100644 --- a/drivers/crypto/caam/caampkc.c +++ b/drivers/crypto/caam/caampkc.c @@ -379,6 +379,7 @@ static int akcipher_do_one_req(struct crypto_engine *en= gine, void *areq) struct crypto_akcipher *tfm =3D crypto_akcipher_reqtfm(req); struct caam_rsa_req_ctx *req_ctx =3D akcipher_request_ctx(req); struct caam_rsa_ctx *ctx =3D akcipher_tfm_ctx_dma(tfm); + struct caam_rsa_key *key =3D &ctx->key; struct device *jrdev =3D ctx->dev; u32 *desc =3D req_ctx->edesc->hw_desc; int ret; @@ -391,7 +392,21 @@ static int akcipher_do_one_req(struct crypto_engine *e= ngine, void *areq) return ret; =20 if (ret !=3D -EINPROGRESS) { - rsa_pub_unmap(jrdev, req_ctx->edesc, req); + if (key->d) { + switch (key->priv_form) { + case FORM1: + rsa_priv_f1_unmap(jrdev, req_ctx->edesc, req); + break; + case FORM2: + rsa_priv_f2_unmap(jrdev, req_ctx->edesc, req); + break; + case FORM3: + rsa_priv_f3_unmap(jrdev, req_ctx->edesc, req); + break; + } + } else { + rsa_pub_unmap(jrdev, req_ctx->edesc, req); + } rsa_io_unmap(jrdev, req_ctx->edesc, req); kfree(req_ctx->edesc); } else { @@ -691,17 +706,19 @@ static int akcipher_enqueue_req(struct device *jrdev, ret =3D caam_jr_enqueue(jrdev, desc, cbk, req); =20 if ((ret !=3D -EINPROGRESS) && (ret !=3D -EBUSY)) { - switch (key->priv_form) { - case FORM1: - rsa_priv_f1_unmap(jrdev, edesc, req); - break; - case FORM2: - rsa_priv_f2_unmap(jrdev, edesc, req); - break; - case FORM3: - rsa_priv_f3_unmap(jrdev, edesc, req); - break; - default: + if (key->d) { + switch (key->priv_form) { + case FORM1: + rsa_priv_f1_unmap(jrdev, edesc, req); + break; + case FORM2: + rsa_priv_f2_unmap(jrdev, edesc, req); + break; + case FORM3: + rsa_priv_f3_unmap(jrdev, edesc, req); + break; + } + } else { rsa_pub_unmap(jrdev, edesc, req); } rsa_io_unmap(jrdev, edesc, req); @@ -992,6 +1009,8 @@ static int caam_rsa_set_priv_key_form(struct caam_rsa_= ctx *ctx, size_t q_sz =3D raw_key->q_sz; unsigned aligned_size; =20 + rsa_key->priv_form =3D FORM1; + rsa_key->p =3D caam_read_raw_data(raw_key->p, &p_sz); if (!rsa_key->p) return -ENOMEM; --=20 2.43.0 From nobody Mon Sep 28 13:59:31 2026 Received: from smtp-relay-internal-0.canonical.com (smtp-relay-internal-0.canonical.com [185.125.188.122]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5355037BE63 for ; Fri, 21 Aug 2026 04:56:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.125.188.122 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787288181; cv=none; b=qeNK6jUIgbZmGBXiJLwjYNSBD6EQ1hU0C52auR3LFuK6ccctYt5KNCX6JYBPhpTITmDEEsrVmnYMGIMwuYbDrBl+cZOhD1noeeAG2X4CHFmnHuFg/VgbQTgBjDoEJUY0XIB7n5udIn3YXPgz6bwk2dMaehqhuwdB4p4JBBB/q60= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787288181; c=relaxed/simple; bh=V7Z9H9oiwaKPz94p8f09aZT6Dm4cOFU/0XbPl/jd4zk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PJC8lW1B+2WiCssZuvQ5HQgFeUjcOB776CCw4/dfFQj5ByU0Ld6XW5U413IzvO1T9vSdV0UjRTbFNF5OxtdUnvbvwZl2Lsl1JS7YhNU1hVHDIcHv1LaX5kXiTO+tFtxn8Y+xVIXyKix/fTk90kFgdqCSlAfjETikm8KUi+El50M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=canonical.com; spf=pass smtp.mailfrom=canonical.com; dkim=pass (4096-bit key) header.d=canonical.com header.i=@canonical.com header.b=PWWgrY3q; arc=none smtp.client-ip=185.125.188.122 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=canonical.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=canonical.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (4096-bit key) header.d=canonical.com header.i=@canonical.com header.b="PWWgrY3q" Received: from mail-pj1-f69.google.com (mail-pj1-f69.google.com [209.85.216.69]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id D8D5B44B97 for ; Fri, 21 Aug 2026 04:56:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20251003; t=1787288174; bh=AI1sq5q1K9dh5wmYC3SNv2bg0usWJqlItyrFzBnnDRg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PWWgrY3q6NJpiVvoCTGoC+0dqnaxRx4VXws/fJbECuECmD4GaUMFpHUheXgzXJGg1 X+6mhga62YY1avgaYjakpT+310KGtpkT8QKPDBv4KVZy8bWRee1gn3tLXG4iYFXOr1 u7le3+WXyOTYZJuDH2G94xRVJHeAoUeOawuZnAgjb4NcJya8mhtmsZEtBYCaNLXZub vdheKzhHTp32RUtQ15CE3SqETrLZCrO/pLcS0mH2ZKCy0/icQRbGZXguURRJFSOGts yif2ys8UaT3X1q1rkdXXXnNAMXd6c0FHekMTZtSuSpLByGHNhyLwfXWhl3ldrFXwcI 5SfNYjR1aMRH555pz/1DoXZ7/CZ+5rX/SC+R6xbcjIfrqKQ+RkMDxwhA0YRaHzihIx 80q1b5MNmZxlPRhfRmHF20TCzwHbxweP99lU2ippFEsnKHW2sXkoEQT3Pb14FLCSCq 45rcYOpAyjc+/A0/ClcQ3uUr6AZezNZAQOPuhT6ryFNhggUcfPHq6DilTVUWD+PAvX DYQAcakWuQqhSRufhnfEqVaMAFCD9pc48f86O1XmGEN6BDkp5gbGHFySznhjhThg5Y 56ydnftODs68Akcyd9vzmKs0QIk+NBIsXboaiP1HCi+PDMnV0Flapmy7xpB0kvYrdg KLC+tbIFC+SQQzCRe+I8WVfE= Received: by mail-pj1-f69.google.com with SMTP id 98e67ed59e1d1-3931cd2e4cbso666858a91.2 for ; Thu, 20 Aug 2026 21:56:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787288173; x=1787892973; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=AI1sq5q1K9dh5wmYC3SNv2bg0usWJqlItyrFzBnnDRg=; b=dvUivf3U7TEyGSQsK8bDOW3XsUf1VmqZXhyW/Y346G4NK+c8wb/9aPTJcrLeMZCUBI T/J5V+pMJpEWHDa3V4J0oMYMcakV+evZi09MYvwpRmuiLsH02SHfnj5mRY/t44OaPWok 8Cg6ikuAbLh6fpCMnneQtXrEcJii1W49EZMm/xVqACvH+e1eDRTmPsPCKWprqxfKIgUd R+Cuv2vn9wtFLvstIK8TdihcUz+NPDptxWo75v1EZbN/1n290zZP9WzwTRFCIa1Sa6+U 8BBMWQ3bN4XScCXCY0OOCKc2B52Je+wSVZ9eOqA45R+e1nx4/KsGoYyIUZ0Vmd2vnflw lHIQ== X-Forwarded-Encrypted: i=1; AHgh+Rp8qitAQ7f34t2kJ4zsKTe0cKTNZgd9Crdq+vxDIlRmwOPuvsOBcr9A/XJ+lQi93WxZXQ+iKdL8SdcWRzI=@vger.kernel.org X-Gm-Message-State: AFuF++n/Al3tvg4rmb3Ui5ESo6F/wF0VY9H8gSL2o1HFQncwJoo1NM7W 1tM4Bd4x4aYTwYJnQj3jUJ+YzHLSU/Nc14LoEOH96+nl3TXGRjx0p+TBcWifbRoEFtDJe+1NRED aSbRoX4qTH0RMf/2m5745OFpuVj8GmJbsFvplUSv1WCFBQFju5hXpMfQVqoG25j2V543OnnDprT pOLlREIQ== X-Gm-Gg: AR+sD10dz5vQeav6EyO3Nt0seDgqkBNw5Fi26zfRCteclF7/Wb5QDOsOZbHwdD5Yja4 kPll/FQ+lKta6icPbPXJaGsnjSXyOY20G6aa+2yCcl2fPz4g4g8SybFoAwAffgxDIu/bdTatFqL mIU7ayjJY99IXp4/Nkcy5mJlX4O9Yo6vn8M98UB332dcM4bEhSAe/kBmTmzz4EL1v/1CtoQMFpr 3oNkjx3oLnHbepskoE9i3ql8k0kH7VJQSB4SFaH3RY3VWQp0s6/ydrYw2HT8xC6vI78YupJzNmu WGis/WDnWdjbxHAlmaK1/2Dt8kfcUCAjab1FMUfhAVqpqWRFbFX9d/OJMP6GiwryCJreIcc9v/U AMW4G7AZyqe0= X-Received: by 2002:a17:90b:1a90:b0:37f:bfd6:8b40 with SMTP id 98e67ed59e1d1-395c33a0168mr5128276a91.5.1787288173138; Thu, 20 Aug 2026 21:56:13 -0700 (PDT) X-Received: by 2002:a17:90b:1a90:b0:37f:bfd6:8b40 with SMTP id 98e67ed59e1d1-395c33a0168mr5128250a91.5.1787288172748; Thu, 20 Aug 2026 21:56:12 -0700 (PDT) Received: from ZBook.gateway ([123.208.39.53]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-395c4553168sm1535224a91.1.2026.08.20.21.56.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Aug 2026 21:56:12 -0700 (PDT) From: Changwei Zou To: horia.geanta@nxp.com, pankaj.gupta@nxp.com, gaurav.jain@nxp.com, herbert@gondor.apana.org.au, davem@davemloft.net Cc: linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, lukas@wunner.de, changwei.zou@canonical.com Subject: [PATCH 2/2] crypto: caam - Use bounce buffer for unaligned RSA destination buffers Date: Fri, 21 Aug 2026 14:55:55 +1000 Message-ID: <20260821045555.806471-3-changwei.zou@canonical.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260821045555.806471-1-changwei.zou@canonical.com> References: <20260821045555.806471-1-changwei.zou@canonical.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The rsa-caam driver directly DMA-maps the destination buffer supplied by the caller via req->dst without checking whether it meets the cacheline alignment requirements of DMA-incoherent hardware such as i.MX8. On CPUs with non-coherent DMA caches, if the destination buffer shares a cacheline with other data (i.e. it is not cacheline-aligned), cache writeback/invalidation during DMA can corrupt adjacent memory or cause stale data to be read back. This manifests as intermittent -EKEYREJECTED errors when loading signed kernel modules. When any segment of req->dst is not cacheline-aligned in either its start offset or length, allocate a single contiguous aligned bounce buffer covering the full dst_len rounded up to a cacheline multiple, redirect the operation to it, and copy the result back to the original destination once the hardware has completed successfully. Introduce a helper sg_is_dma_aligned() that checks both sg->offset and sg->length for cacheline alignment. Checking sg->offset suffices for the start address since physical pages are always page-aligned and PAGE_SIZE is a multiple of dma_get_cache_alignment(). sg->length is also checked to ensure the buffer end does not share a cacheline with adjacent memory. The intermittent error 'Key was rejected by service' on i.MX8 with CAAM can be triggered when loading signed kernel modules: for i in $(seq 1 100); do sudo modprobe xfs 2>&1 && echo "SUCCESS on attempt $i" \ && sudo rmmod xfs || echo "FAILED on attempt $i" done Signed-off-by: Changwei Zou Assisted-by: OpenCode:claude-sonnet-4.6 --- drivers/crypto/caam/caampkc.c | 77 ++++++++++++++++++++++++++++++++++- drivers/crypto/caam/caampkc.h | 6 +++ 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/drivers/crypto/caam/caampkc.c b/drivers/crypto/caam/caampkc.c index 840271840cce..11c6b07f5dad 100644 --- a/drivers/crypto/caam/caampkc.c +++ b/drivers/crypto/caam/caampkc.c @@ -59,6 +59,40 @@ static void rsa_io_unmap(struct device *dev, struct rsa_= edesc *edesc, DMA_TO_DEVICE); } =20 +static int do_rsa_bounce_buf(struct akcipher_request *req, int req_err) +{ + struct caam_rsa_req_ctx *req_ctx =3D akcipher_request_ctx(req); + int nents, err =3D 0; + + if (!req_ctx->bounce_buf) + return 0; + + /* Only copy back to the original destination on success */ + if (!req_err) { + nents =3D sg_nents_for_len(req_ctx->orig_dst, req->dst_len); + if (nents < 0) + err =3D nents; + else if (sg_copy_from_buffer(req_ctx->orig_dst, nents, + req_ctx->bounce_buf, + req->dst_len) !=3D req->dst_len) + err =3D -EFAULT; + } + + kfree(req_ctx->bounce_buf); + req_ctx->bounce_buf =3D NULL; + req->dst =3D req_ctx->orig_dst; + + return err; +} + +static inline void rsa_bounce_buf_done(struct akcipher_request *req, int *= err) +{ + int cperr =3D do_rsa_bounce_buf(req, *err); + + if (!*err) + *err =3D cperr; +} + static void rsa_pub_unmap(struct device *dev, struct rsa_edesc *edesc, struct akcipher_request *req) { @@ -138,6 +172,7 @@ static void rsa_pub_done(struct device *dev, u32 *desc,= u32 err, void *context) rsa_pub_unmap(dev, edesc, req); rsa_io_unmap(dev, edesc, req); kfree(edesc); + rsa_bounce_buf_done(req, &ecode); =20 /* * If no backlog flag, the completion of the request is done @@ -181,6 +216,7 @@ static void rsa_priv_f_done(struct device *dev, u32 *de= sc, u32 err, =20 rsa_io_unmap(dev, edesc, req); kfree(edesc); + rsa_bounce_buf_done(req, &ecode); =20 /* * If no backlog flag, the completion of the request is done @@ -246,6 +282,12 @@ static int caam_rsa_count_leading_zeros(struct scatter= list *sgl, return tbytes - nbytes; } =20 +static inline bool sg_is_dma_aligned(struct scatterlist *sg) +{ + return IS_ALIGNED(sg->offset, dma_get_cache_alignment()) && + IS_ALIGNED(sg->length, dma_get_cache_alignment()); +} + static struct rsa_edesc *rsa_edesc_alloc(struct akcipher_request *req, size_t desclen) { @@ -291,11 +333,34 @@ static struct rsa_edesc *rsa_edesc_alloc(struct akcip= her_request *req, req_ctx->fixup_src_len); dst_nents =3D sg_nents_for_len(req->dst, req->dst_len); =20 + req_ctx->bounce_buf =3D NULL; + req_ctx->orig_dst =3D req->dst; + if (req->dst_len > 0) { + struct scatterlist *sg; + int i; + + for_each_sg(req->dst, sg, dst_nents, i) { + if (!sg_is_dma_aligned(sg)) { + req_ctx->bounce_buf =3D + kzalloc(ALIGN(req->dst_len, + dma_get_cache_alignment()), + flags); + if (!req_ctx->bounce_buf) + return ERR_PTR(-ENOMEM); + sg_init_one(&req_ctx->dst, req_ctx->bounce_buf, + req->dst_len); + req->dst =3D &req_ctx->dst; + dst_nents =3D 1; + break; + } + } + } + mapped_src_nents =3D dma_map_sg(dev, req_ctx->fixup_src, src_nents, DMA_TO_DEVICE); if (unlikely(!mapped_src_nents)) { dev_err(dev, "unable to map source\n"); - return ERR_PTR(-ENOMEM); + goto bounce_fail; } mapped_dst_nents =3D dma_map_sg(dev, req->dst, dst_nents, DMA_FROM_DEVICE); @@ -368,6 +433,10 @@ static struct rsa_edesc *rsa_edesc_alloc(struct akciph= er_request *req, dma_unmap_sg(dev, req->dst, dst_nents, DMA_FROM_DEVICE); src_fail: dma_unmap_sg(dev, req_ctx->fixup_src, src_nents, DMA_TO_DEVICE); +bounce_fail: + kfree(req_ctx->bounce_buf); + req_ctx->bounce_buf =3D NULL; + req->dst =3D req_ctx->orig_dst; return ERR_PTR(-ENOMEM); } =20 @@ -409,6 +478,7 @@ static int akcipher_do_one_req(struct crypto_engine *en= gine, void *areq) } rsa_io_unmap(jrdev, req_ctx->edesc, req); kfree(req_ctx->edesc); + rsa_bounce_buf_done(req, &ret); } else { ret =3D 0; } @@ -723,6 +793,7 @@ static int akcipher_enqueue_req(struct device *jrdev, } rsa_io_unmap(jrdev, edesc, req); kfree(edesc); + rsa_bounce_buf_done(req, &ret); } =20 return ret; @@ -764,6 +835,7 @@ static int caam_rsa_enc(struct akcipher_request *req) init_fail: rsa_io_unmap(jrdev, edesc, req); kfree(edesc); + rsa_bounce_buf_done(req, &ret); return ret; } =20 @@ -793,6 +865,7 @@ static int caam_rsa_dec_priv_f1(struct akcipher_request= *req) init_fail: rsa_io_unmap(jrdev, edesc, req); kfree(edesc); + rsa_bounce_buf_done(req, &ret); return ret; } =20 @@ -822,6 +895,7 @@ static int caam_rsa_dec_priv_f2(struct akcipher_request= *req) init_fail: rsa_io_unmap(jrdev, edesc, req); kfree(edesc); + rsa_bounce_buf_done(req, &ret); return ret; } =20 @@ -851,6 +925,7 @@ static int caam_rsa_dec_priv_f3(struct akcipher_request= *req) init_fail: rsa_io_unmap(jrdev, edesc, req); kfree(edesc); + rsa_bounce_buf_done(req, &ret); return ret; } =20 diff --git a/drivers/crypto/caam/caampkc.h b/drivers/crypto/caam/caampkc.h index 96d03704c9be..efad91d6058f 100644 --- a/drivers/crypto/caam/caampkc.h +++ b/drivers/crypto/caam/caampkc.h @@ -103,6 +103,9 @@ struct caam_rsa_ctx { * @src : input scatterlist (stripped of leading zeros) * @fixup_src : input scatterlist (that might be stripped of leading z= eros) * @fixup_src_len : length of the fixup_src input scatterlist + * @dst : destination scatterlist backed by bounce buffer (if ne= eded) + * @bounce_buf : DMA-aligned bounce buffer for destination (or NULL) + * @orig_dst : original destination scatterlist (before bounce substi= tution) * @edesc : s/w-extended rsa descriptor * @akcipher_op_done : callback used when operation is done */ @@ -110,6 +113,9 @@ struct caam_rsa_req_ctx { struct scatterlist src[2]; struct scatterlist *fixup_src; unsigned int fixup_src_len; + struct scatterlist dst; + u8 *bounce_buf; + struct scatterlist *orig_dst; struct rsa_edesc *edesc; void (*akcipher_op_done)(struct device *jrdev, u32 *desc, u32 err, void *context); --=20 2.43.0