All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Ralf Lici" <ralf@mandelbit.com>, linux-crypto@vger.kernel.org
Cc: "Antoine Tenart" <atenart@kernel.org>,
	"Herbert Xu" <herbert@gondor.apana.org.au>,
	"David S. Miller" <davem@davemloft.net>,
	"Nikita Zhandarovich" <n.zhandarovich@fintech.ru>,
	"Antonio Quartulli" <antonio@openvpn.net>
Subject: Re: [PATCH crypto v2 2/2] crypto: safexcel - Map AEAD buffers with accurate DMA directions
Date: Thu, 03 Dec 2026 09:09:12 +0000	[thread overview]
Message-ID: <sashiko-outbox-247427@kernel.org> (raw)
In-Reply-To: <b17181f83d18c6f7354eb1c17212b9347f7eb3e5.1796286347.git.ralf@mandelbit.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] crypto: safexcel: per-entry AEAD DMA mapping regresses
  coherent/IOMMU paths
- [Medium] crypto: safexcel: safexcel_aead_dma_dir() over-maps
  out-of-place dst buffers
- [Low] crypto: safexcel: duplicated DMA unmap dispatch in cipher req
  paths
--

commit 0fc5903031a933ef50a45baa1e600103db377d61
Author: Ralf Lici <ralf@mandelbit.com>

crypto: safexcel - Map AEAD buffers with accurate DMA directions

This commit replaces batched dma_map_sg() and dma_unmap_sg() calls on
AEAD scatterlists with per-entry dma_map_page() and dma_unmap_page()
helpers that select the DMA direction from each entry's overlap with the
logical output range.

> diff --git a/drivers/crypto/inside-secure/safexcel_cipher.c b/drivers/crypto/inside-secure/safexcel_cipher.c
> index b9d8f5179de6c..78f90ba6c1365 100644
> --- a/drivers/crypto/inside-secure/safexcel_cipher.c
> +++ b/drivers/crypto/inside-secure/safexcel_cipher.c

[ ... ]

> @@ -606,6 +608,71 @@ static int safexcel_context_control(struct safexcel_cipher_ctx *ctx,
>  	return 0;
>  }
>  
> +/* classify each scatterlist entry by its overlap with the logical output */
> +static enum dma_data_direction safexcel_aead_dma_dir(unsigned int offset,
> +						     unsigned int len,
> +						     unsigned int output_offset,
> +						     unsigned int output_len,
> +						     bool inplace)
> +{
> +	unsigned int end = offset + len;
> +	unsigned int output_end = output_offset + output_len;
> +
> +	if (!output_len || offset >= output_end || output_offset >= end)
> +		return DMA_TO_DEVICE;
> +
> +	if (inplace || offset < output_offset || end > output_end)
> +		return DMA_BIDIRECTIONAL;
> +
> +	return DMA_FROM_DEVICE;
> +}

[Severity: Medium]
Should safexcel_aead_dma_dir() map out-of-place destination entries with
DMA_TO_DEVICE or DMA_BIDIRECTIONAL when the hardware never reads dst?

When inplace is false, safexcel_send_req() skips pure-AAD dst entries in
the result descriptor loop without using their DMA addresses.  Mapping
and unmapping those entries with DMA_TO_DEVICE adds unnecessary cache
maintenance and barriers on non-coherent ARM64 and redundant IOMMU
mappings on hot out-of-place paths such as kTLS.

Mapping uninitialized out-of-place dst buffers with DMA_TO_DEVICE or
DMA_BIDIRECTIONAL also triggers kmsan_internal_check_memory() under
KMSAN, regressing callers like generate_aead_message() in
crypto/testmgr.c that pass uninitialized kmalloc() buffers.  Skipping
non-overlapping out-of-place entries (while still setting sg_dma_len())
and mapping only the written sub-range of partial-overlap entries with
DMA_FROM_DEVICE would avoid both issues.

> +static void safexcel_unmap_aead(struct device *dev, struct scatterlist *sgl,
> +				int nents, unsigned int output_offset,
> +				unsigned int output_len, bool inplace)
> +{
> +	struct scatterlist *sg;
> +	unsigned int offset = 0;
> +	int i;
> +
> +	for_each_sg(sgl, sg, nents, i) {
> +		enum dma_data_direction dir;
> +
> +		dir = safexcel_aead_dma_dir(offset, sg->length, output_offset,
> +					    output_len, inplace);
> +		dma_unmap_page(dev, sg_dma_address(sg), sg_dma_len(sg), dir);
> +		offset += sg->length;
> +	}
> +}
> +
> +static int safexcel_map_aead(struct device *dev, struct scatterlist *sgl,
> +			     int nents, unsigned int output_offset,
> +			     unsigned int output_len, bool inplace)
> +{
> +	struct scatterlist *sg;
> +	unsigned int offset = 0;
> +	int i;
> +
> +	for_each_sg(sgl, sg, nents, i) {
> +		enum dma_data_direction dir;
> +
> +		dir = safexcel_aead_dma_dir(offset, sg->length, output_offset,
> +					    output_len, inplace);
> +		sg_dma_address(sg) = dma_map_page(dev, sg_page(sg), sg->offset,
> +						  sg->length, dir);
> +		if (dma_mapping_error(dev, sg_dma_address(sg)))
> +			goto err_unmap;
> +		sg_dma_len(sg) = sg->length;
> +		offset += sg->length;
> +	}
> +
> +	return nents;
> +
> +err_unmap:
> +	safexcel_unmap_aead(dev, sgl, i, output_offset, output_len, inplace);
> +	return 0;
> +}

[Severity: Medium]
Does unconditionally replacing dma_map_sg() and dma_unmap_sg() with
per-entry dma_map_page() and dma_unmap_page() regress AEAD throughput on
DMA-coherent and IOMMU-backed devices?

dma_kmalloc_safe() returns true when dev_is_dma_coherent(dev) is set, so
coherent platforms do not bounce through SWIOTLB or hit the non-coherent
cacheline corruption.  On IOMMU-backed safexcel devices (such as PCIe
EIP197 boards or coherent Armada CP11x), per-entry calls lose batched
IOVA allocation and segment coalescing and perform an
iommu_iova_to_phys() walk plus a separate __iommu_dma_unmap() per entry.

On non-coherent ARM64 dma-direct platforms, per-entry dma_map_page() and
dma_unmap_page() also execute arch_sync_dma_flush() (dsb(sy)) on every
entry, including DMA_TO_DEVICE unmaps, instead of once per scatterlist.
Gating the per-entry path on !dev_is_dma_coherent(priv->dev) would
preserve batched dma_map_sg() performance on coherent and IOMMU-backed
devices.

[ ... ]

> @@ -911,16 +1014,30 @@ static int safexcel_send_req(struct crypto_async_request *base, int ring,
>  		safexcel_ring_rollback_wptr(priv, &priv->ring[ring].cdr);
>  unmap:
>  	if (src == dst) {
> -		if (src_mapped)
> -			dma_unmap_sg(priv->dev, src, sreq->nr_src,
> -				     DMA_BIDIRECTIONAL);
> +		if (src_mapped) {
> +			if (ctx->aead)
> +				safexcel_unmap_aead(priv->dev, src,
> +						    sreq->nr_src,
> +						    sreq->output_offset,
> +						    sreq->output_len, true);
> +			else
> +				dma_unmap_sg(priv->dev, src, sreq->nr_src,
> +					     DMA_BIDIRECTIONAL);
> +		}
>  	} else {
>  		if (src_mapped)
>  			dma_unmap_sg(priv->dev, src, sreq->nr_src,
>  				     DMA_TO_DEVICE);
> -		if (dst_mapped)
> -			dma_unmap_sg(priv->dev, dst, sreq->nr_dst,
> -				     DMA_FROM_DEVICE);
> +		if (dst_mapped) {
> +			if (ctx->aead)
> +				safexcel_unmap_aead(priv->dev, dst,
> +						    sreq->nr_dst,
> +						    sreq->output_offset,
> +						    sreq->output_len, false);
> +			else
> +				dma_unmap_sg(priv->dev, dst, sreq->nr_dst,
> +					     DMA_FROM_DEVICE);
> +		}
>  	}

[Severity: Low]
This isn't a bug, but this unmap block duplicates the same nested
if (src == dst) / if (ctx->aead) dispatch in safexcel_handle_req_result(),
differing only in checking src_mapped/dst_mapped versus
sreq->nr_src > 0 / sreq->nr_dst > 0.  Extracting a shared helper would
avoid keeping both copies in sync.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796286347.git.ralf@mandelbit.com?part=2

  reply	other threads:[~2026-12-03  9:09 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  8:40 [PATCH crypto v2 0/2] crypto: safexcel: fix AEAD DMA mapping and cleanup Ralf Lici
2026-12-03  8:40 ` [PATCH crypto v2 1/2] crypto: safexcel - Avoid unmapping failed DMA mappings Ralf Lici
2026-12-03  8:40 ` [PATCH crypto v2 2/2] crypto: safexcel - Map AEAD buffers with accurate DMA directions Ralf Lici
2026-12-03  9:09   ` sashiko-bot [this message]
2026-12-03 10:07     ` Ralf Lici

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-247427@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=antonio@openvpn.net \
    --cc=atenart@kernel.org \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-crypto@vger.kernel.org \
    --cc=n.zhandarovich@fintech.ru \
    --cc=ralf@mandelbit.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.