Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=195.10.208.50 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796292451; b=GfraOkUGgjm6d2lmjTyuikvjllBxupCGCJr6/W96W2yTVHHdTvyScaf2lG/jSCX8wNeO Q/7cHudoYZQ2DWu1kXvNPPsz9SMGyd9Hy0GvBf4mazmrjp5zM0v0dRMVeWcbPBvFFkScO p7iPwwrXt46YCiY1GHnbGYf4N2l9cAIlEETc4KtCT/Bs4bhuGBg1Fjm1bYAbNcDOoGwwY BK+GTWOVktXsLPvu6jDUnoTtkFGYEDmAD5xQz3wfK3ktUyYdZx+Kr3nxFksdP3rYHYp1Y l1VDTCDsC6MCG+3m3usecZNSU7wi+AC2Rsbo2nVoQvF6ouYvbl0n2nGViUpqCXs+ccw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796292451; h=DMARC-Filter:Received:DKIM-Signature:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Rspamd-Queue-Id; bh=ygUajoCHOhHniZIKzTlIDoSAQzyZFJzJ6L3RiRPjch0=; b=a2YcRRLQiFVFuZWiV0BWL4+Mf1tYEEp1E/LOjRzMxLJT+53Odej0o0Edwy94l14BVCd1 0XR226SORQ8WDledjfeUXaXYISsM42yO7jukLkovYbakcOd6PnOcH0ioAGHj9PLYVEt9p Hz/kz2XcdaML/KdU2kcnb+SryTZJ6tVxqtnsGXi7iKMyiKaizBtNdRAdBODoJnXBKmkRF ZLwkx2O+SrRNf488nQZ16xW9M5knxR0mEQPLgPfAvn6x1n+ApWIDDitghbs6QfpbSNBtB vFdZO9JoTfXhTMD8tcroLdfwXo0I+1e0jofJ7Uqq+4NTAGxjPjoUAbHZdinOB8xh8eQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=mandelbit.com; spf=pass smtp.mailfrom=mandelbit.com; dkim=pass header.d=mandelbit.com header.i=@mandelbit.com header.a=rsa-sha256 header.s=MBO0001 header.b=b5pO8R17; arc=none smtp.remote-ip=195.10.208.50 Received: from mout-b-105.mailbox.org (mout-b-105.mailbox.org [195.10.208.50]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0779B606A322 for ; Thu, 3 Dec 2026 10:07:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0779B606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=mandelbit.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mandelbit.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=mandelbit.com header.i=@mandelbit.com header.a=rsa-sha256 header.s=MBO0001 header.b=b5pO8R17 Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:b231:465::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519MLKEM768 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-b-105.mailbox.org (Postfix) with ESMTPS id 4jg9KN6ZYVz9ttX; Thu, 03 Dec 2026 11:07:20 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mandelbit.com; s=MBO0001; t=1796292441; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ygUajoCHOhHniZIKzTlIDoSAQzyZFJzJ6L3RiRPjch0=; b=b5pO8R17oPZItY9PNTgHGNCh42jupiHf2P9cxwaDXeHYXA8Zf3/f712kLvAh+5sU4Qq4lO kW1X4hGwYw3u012WedO6PeJk86Z+HzTl1fJ9txZsBb+Jm65rdZsoufzqDNrJPRyZ8or5EK PGm42r0PX06BGJWaYJLPUyEbTqchVGRCSxBs6V7gr7mLtv/xM+wJL2vTByOh4XBo5SiSrP 9WBt4vdG+YY6bUXzpdUzKGg2twevujX4KjBumGwCE3Wz7cBvz4tDgaQ3cmbSONNQV+jegt 5JkhOuhHq0tXCbgu56I3U8CB89oBwhjmcNtUO9DETeNMScTjp0O9S6jRB12p/g== Authentication-Results: outgoing_mbo_mout; dkim=none; spf=pass (outgoing_mbo_mout: domain of ralf@mandelbit.com designates 2001:67c:2050:b231:465::2 as permitted sender) smtp.mailfrom=ralf@mandelbit.com From: Ralf Lici To: sashiko-reviews@lists.linux.dev Cc: sashiko-bot@kernel.org, linux-crypto@vger.kernel.org, Antoine Tenart , Herbert Xu , "David S. Miller" , Nikita Zhandarovich , Antonio Quartulli 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 Message-ID: <20261203100718.185095-1-ralf@mandelbit.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: References: X-Mailing-List: linux-crypto@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 4jg9KN6ZYVz9ttX 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