Linux cryptographic layer development
 help / color / mirror / Atom feed
From: Ralf Lici <ralf@mandelbit.com>
To: sashiko-reviews@lists.linux.dev
Cc: sashiko-bot@kernel.org, linux-crypto@vger.kernel.org,
	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,  3 Dec 2026 11:07:18 +0100	[thread overview]
Message-ID: <20261203100718.185095-1-ralf@mandelbit.com> (raw)
In-Reply-To: <sashiko-outbox-247427@kernel.org>

On Thu, 03 Dec 2026 09:09:12 +0000, sashiko-bot@kernel.org wrote:
> > @@ -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.
>

The extra cache maintenance is not new, since the stock driver already
maps the reserved dst AAD (with DMA_FROM_DEVICE). The testmgr example
does not apply either, as generate_aead_message runs on the generic
implementation and testmgr poisons its destination buffers.

The KMSAN concern OTOH does hold for mixed out-of-place dst entries,
which are now mapped DMA_BIDIRECTIONAL while their output part is
usually uninitialized. Since the engine skips the dst AAD anyway, the
cleaner option would be to fast-forward dst past assoclen and map only
the output, DMA_FROM_DEVICE.

> > +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.
>

Coherent devices never hit the bug, so the per-entry path could be
limited to non-coherent devices. The extra barrier per entry is real
since v7.1, but small next to the per-request cost of the EIP97.

> > @@ -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.
>

The two unmap paths were already duplicated before this patch, as are
other hunks in the driver, so I kept the fix consistent with the
existing style rather than refactoring in a patch meant for stable.

Addressing the first two findings would make the patch larger rather
than smaller, which goes the opposite way of the simpler approach
Herbert suggested in v1. So I'd rather wait for his opinion on the
overall approach before reworking it.

Herbert, would you like these changes in a v3, or do you prefer to keep
the patch as it is?

-- 
Ralf Lici
Mandelbit Srl

      reply	other threads:[~2026-12-03 10:07 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
2026-12-03 10:07     ` Ralf Lici [this message]

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=20261203100718.185095-1-ralf@mandelbit.com \
    --to=ralf@mandelbit.com \
    --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=sashiko-bot@kernel.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox