Re: [RFC PATCH 1/1] crypto: caam - fix in-place RSA payload DMA mapping

From: Sahil Malhotra

Date: Thu Nov 12 2026 - 03:28:40 EST


On Tue, Oct 20, 2026 at 03:25:11PM -0400, Jeff Barnes wrote:
> CAAM can receive RSA requests whose effective source and destination
> scatterlists are identical. The driver currently maps the source with
> DMA_TO_DEVICE and then maps the destination with DMA_FROM_DEVICE.

Hi Jeff,

Thanks for the analysis, and sorry for the slow response.

To answer your first question: yes. When the effective source and
destination are the same scatterlist, I think a single DMA_BIDIRECTIONAL
mapping is the right model. The in-tree user that hits this is
rsassa-pkcs1 verify (crypto/rsassa-pkcs1.c), which calls
akcipher_request_set_crypt(child_req, &sg, &sg, slen, slen) with
slen == key size. That gives an exact, single-entry alias, which matches
the pkcs1(rsa-caam,sha256) failure you saw.

I have a few comments below.

[...]

> + if (req_ctx->fixup_src == req->dst) {

This only detects the case where the two head pointers are equal. When
req->src_len > n_sz and leading zeros are stripped, fixup_src is the
result of scatterwalk_ffwd(). That is either req_ctx->src[] or a later
entry of req->src, so the comparison fails even when req->src == req->dst.
The same memory then still gets one TO_DEVICE and one FROM_DEVICE
mapping, through different struct scatterlist. The sg metadata is no
longer overwritten, but we still have two overlapping streaming mappings
with opposite directions, and CONFIG_DMA_API_DEBUG should complain about
that.

Could you either check req->src == req->dst (before the leading zeros are
stripped), or at least state in the commit message that only an exact
head alias is handled?

> + shared_nents = sg_nents_for_len(req_ctx->fixup_src,
> + max(req_ctx->fixup_src_len,
> + req->dst_len));

[...]

> + if (mapped_src_nents > 1) {
> + /*
> + * Multi-segment aliases need separately bounded source
> + * and destination hardware views. The current SEC4/PDB
> + * layout cannot represent all such requests; defer support.
> + */
> + dma_unmap_sg(dev, req_ctx->fixup_src, shared_nents,
> + DMA_BIDIRECTIONAL);
> + dev_err(dev, "Multi-segment shared S/G is not supported\n");
> + return ERR_PTR(-EOPNOTSUPP);
> + }

I'm concerned this is a regression on systems where CAAM is not behind an
IOMMU, e.g. most i.MX8M parts, which use plain dma-direct. There both
dma_map_sg() calls return the same addresses, so as far as I can tell
in-place multi-entry requests work today. With this patch they would fail
with -EOPNOTSUPP, and the driver has no software fallback. The dev_err()
would also fire on every such request, and since a user can trigger
these requests, it should at least be rate-limited.

On your second question, I would prefer giving the request a separate
destination over growing separate source/destination SEC4 views. For the
multi-segment alias case: map the source DMA_TO_DEVICE, map a
driver-owned buffer DMA_FROM_DEVICE, and copy the result back on
completion. That keeps the SEC4/PDB layout unchanged.

Changwei's v4 2/2 ("Use bounce buffer for unaligned RSA destination
buffers") already adds most of this infrastructure:

https://lore.kernel.org/linux-crypto/20261111230221.40297-3-changwei.zou@xxxxxxxxxxxxx/

so the alias case could simply be one more trigger for the bounce path.

On that point, the two patches rewrite the same block in
rsa_edesc_alloc() and will conflict. On top of Changwei's patch, the new
early "return ERR_PTR(...)" exits here (the sg_nents_for_len() < 0
checks, the shared map failure, and -EOPNOTSUPP) would leak
req_ctx->bounce_buf. They would need to go through his bounce_fail label.
Also, the sg_nents_for_len() checks have to run before his
for_each_sg(req->dst, sg, dst_nents, i) loop. Could the two of you agree
on an order, so that whichever lands second is rebased with this in mind?

> + if (unlikely(src_nents < 0)) {
> + dev_err(dev, "Insufficient bytes (%u) in src S/G\n",
> + req_ctx->fixup_src_len);
> + return ERR_PTR(src_nents);
> + }

These checks are a good fix on their own: today a negative count goes
straight into dma_map_sg(). Could you split them into a separate patch?
That makes them easier to backport.

[...]

> dst_fail:
> + if (shared_nents) {
> + dma_unmap_sg(dev, req_ctx->fixup_src, shared_nents,
> + DMA_BIDIRECTIONAL);
> + return ERR_PTR(-ENOMEM);
> + }

Looks fine. The map/unmap counts and directions match on every path.

A couple of process points for the non-RFC version:

- The double mapping goes back to when RSA support was added, so please
add:

Fixes: 8c419778ab57 ("crypto: caam - add support for RSA algorithm")
Cc: stable@xxxxxxxxxxxxxxx

I can test the respin on i.MX8M (dma-direct, non-coherent) with
CONFIG_DMA_API_DEBUG=y.

Thanks,
Sahil