From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 C83DA606A32F for ; Tue, 1 Dec 2026 19:47:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=LhqylkWQ; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=RNyybugz DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: Content-Type:In-Reply-To:From:References:Cc:To:Subject:MIME-Version:Date: Message-ID:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Unsubscribe-Post: List-Owner; bh=la9WOJ9MevQmCObp2UrwbM3sHJ8sBG3mBpDla5uTff0=; b=LhqylkWQNbJUR4 tPMTutHW1HGsmSEZJ9t3nyxzflUHVim2WWEfVgvxKPtLmDZMpR4AKEjknLPEhA0G/ZWVCov2HRSAH LYmg7hXt0FmlblcfqxdXEB39pP+rn7s7RolrdxNcPX8qson++LbFzQX5gRC7S+6mCaSfwEuk2VsXi 8BVisDpeGdcnlYJpL+6nvimQ3cZ02rI2scFgvUAqJ5Acu4E0+4JND5kJ48gIuZ7YIBlr68joJkIwy 9ZmG/I7THRT9wmIl6y3XuoU08jo8qwh7STV7f3e8gwx7tuSHGft+1CPEqO0dLfq8T47XnbIvh8Ygd dGMZaOgFWHyl+H7YwZlw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYToY-00000009GQj-21jG; Tue, 01 Dec 2026 19:46:58 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYToV-00000009GPz-0ZXE for linux-arm-kernel@lists.infradead.org; Tue, 01 Dec 2026 19:46:56 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 53ECD152B; Tue, 1 Dec 2026 11:46:51 -0800 (PST) Received: from [10.57.72.26] (unknown [10.57.72.26]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id AC2693F632; Tue, 1 Dec 2026 11:46:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1796154414; bh=NskiITn/Rq2YTqtMe6fB04eypdjaNQX6w8lKkrey0/w=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=RNyybugz6NYMwgRACEqSIA5BsALhS8vHEINA+l1RSHnjSGT5d8EwYg/vNz6RkuMNP 6yHkN+lv0DN7eJAeYY0C3jtrQ4g0P3TDKihAdwzQDric5gfUPedoCKDJupjGLussw8 LHjyKXEfQq2VKfMeqCIKmIDbA4zvZC7/BemGl87M= Message-ID: Date: Tue, 1 Dec 2026 19:46:49 +0000 X-Archived-By: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH RFC] iommu/arm-smmu: allow per-impl capability override and prot masking To: Bibek Kumar Patro , Rob Clark , Will Deacon , "Joerg Roedel (AMD)" Cc: iommu@lists.linux.dev, linux-arm-msm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Prakash Gupta References: <20261201-vfio_non_coherent-v1-1-0e6f78e111bb@oss.qualcomm.com> From: Robin Murphy Content-Language: en-GB In-Reply-To: <20261201-vfio_non_coherent-v1-1-0e6f78e111bb@oss.qualcomm.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261201_114655_263647_6B9A2C9A X-CRM114-Status: GOOD ( 50.38 ) X-Spam-Score: -4.4 (----) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" X-Spam-Status: No, score=-22.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS, USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 On 2026-12-01 3:56 pm, Bibek Kumar Patro wrote: > An SMMU can expose non-coherent translation interfaces while cache > coherency is managed outside the IOMMU driver. Such instances do not set > ARM_SMMU_FEAT_COHERENT_WALK, causing arm_smmu_capable() to reject > IOMMU_CAP_CACHE_COHERENCY and preventing VFIO and iommufd binding. > > iommufd also sets IOMMU_CACHE for its mappings. On a non-coherent > interconnect, io-pgtable translates this into Normal WB-RA-WA, > Inner-Shareable attributes that request snoops the interconnect cannot > provide. > > Add implementation hooks for overriding capability queries and masking > unsupported protection flags. Apply the protection mask only when > ARM_SMMU_FEAT_COHERENT_WALK is absent, preserving the existing behavior > for coherent instances. Heck no. If you want an unsafe VFIO mode that allows userspace to abuse a lack of coherency to leak stale kernel data from newly-allocated pages, propose a VFIO interface for that. Do not hack individual IOMMU drivers to just lie and claim somethning is safe that isn't. Thanks, Robin. > Wire the hooks into qcom_smmu_500_impl and mask IOMMU_CACHE for > non-coherent instances. Export arm_smmu_capable() for use as the default > fallback. > > Signed-off-by: Prakash Gupta > Signed-off-by: Bibek Kumar Patro > --- > On QCOM devices that are not DMA-coherent: the interconnect between > the device and memory does not keep CPU caches and device-visible memory > consistent. Such a device can still support valid IOMMU translation > and isolation, but the current VFIO/iommufd capability check requires > IOMMU_CAP_CACHE_COHERENCY, which these devices cannot report, so they > fail to bind. This is not specific to any one userspace framework; > any userspace driver capable of performing the required cache > maintenance faces the same restriction. > > USERSPACE (setup, one-time) > +---------------------------------------------------+ > | Userspace device driver | > +---------------------------------------------------+ > Discover / request Request DMA > non-coherent mode mapping > | | > +--------------+ +-----------+ > | | > ioctl(VFIO / iommufd) > -------------------------|----- USER / KERNEL -------- > v > +-------------------------------------------------+ > | VFIO / iommufd device binding | > | - authorize userspace-managed coherency (*) | > | - pin pages, build DMA mapping | > +-------------------------------------------------+ > | > Programs translation and > access permissions > v > +--------------+ > | SMMU | > | IOVA -> PA | > +--------------+ > > (*) Only required when the device is not DMA-coherent; > coherent devices bind without this step. > > USERSPACE (steady state, per operation) > +---------------------------------------------------+ > | Userspace device driver | > +---------------------------------------------------+ > CPU load/store + (DMA proceeds using > DC CVAC / DC CIVAC the mapping built > - no further ioctl above) > | | > v v > +----------------+ +----------------------+ > | CPU / CPU | | Device -> SMMU -> | > | cache | | memory (translated | > | | | DMA access) | > +-------+--------+ +----------+-----------+ > | | > +---------------+--------------+ > | > v > +-------------------------+ > | Memory / interconnect | > +-------------------------+ > > Where coherency needs to be applied: > > - Before the device reads CPU-written data (including descriptor > fields), the CPU must clean those cache lines, ordered ahead of > publishing ownership (e.g. before a doorbell write). > - Before the CPU reads device-written data or status/descriptor > updates, it must invalidate its stale cache lines over that > region. > - Cache-line-shared objects cannot be blindly cleaned or > invalidated while a neighbor on the same line is device-owned. > > How coherent DMA handles this inherently: > > A DMA-coherent device sits on an interconnect that snoops CPU caches, > so hardware keeps the CPU-cache and device-visible views consistent > without software cache maintenance. "dma-coherent" in firmware > describes this hardware property; it does not create it, and > declaring it on hardware that lacks it suppresses required > maintenance and causes stale reads. Coherent DMA still needs memory > barriers around ownership handoff - only the cache clean/invalidate > step becomes unnecessary. > > IOMMU_CACHE is a separate, page-table-level attribute: it tells the > IOMMU what memory type and shareability to encode in a mapping, not > whether the device's DMA is cache-coherent. It must not be set on an > interconnect that cannot honor those attributes, regardless of > whether the device is DMA-coherent. > > Existing alternatives fall short of a native, isolated userspace > driver: > > - AF_XDP: DMA sync stays in the kernel driver path; no native > userspace driver with direct device access. > - Generic UIO: MMIO/interrupt access only, no VFIO/iommufd-style > IOVA mapping and isolation ABI. > - VFIO no-IOMMU and out-of-tree non-cacheable remapping both drop > IOMMU isolation or rely on an unsuitable production ABI: > (https://github.com/nxp-qoriq/dpdk-extras/tree/main/linux/kpage_ncache) > > VFIO/iommufd is not merely a conduit to the SMMU. It is the only > kernel mechanism giving userspace an isolated, pinned DMA mapping, > it owns the IOMMU_CAP_CACHE_COHERENCY check that blocks binding > today, and it sets the IOMMU page-table memory-type attributes for > the mapping - a kernel-only operation independent of what userspace > later does for its own cache lines. Any per-device opt-in > authorization has to live in VFIO/iommufd's binding lifecycle for > the same reason: it is the only place that decides whether the > device is exposed to userspace at all. > > This proposal - > 1. Keeps IOMMU page-table-walk coherency kernel-owned, unrelated to > device DMA coherency. > 2. Targets the VFIO/iommufd binding check, which requires > cache-coherent DMA operation even for a device that supports valid > translated, isolated DMA. Removing the check alone is insufficient, > since the kernel must still mask mapping attributes (such as > IOMMU_CACHE) that the interconnect cannot honor. > 3. Needs a handshake so the kernel confirms and authorizes userspace > coherency handling per device, instead of silently assuming > coherence. > > The attached patch implements only the SMMU side of point 2 above: > the capability override and IOMMU_CACHE masking. It does not > implement the opt-in handshake of point 3, or any userspace-side > cache maintenance (the CPU/cache path shown above) - that remains the > userspace driver's responsibility once authorized, as illustrated > by the examples below. This RFC is asking for direction on that > handshake before it is written. > > We are seeking feedback on an explicit, per-device opt-in model: > 1. No change for devices/drivers using coherent DMA. > 2. VFIO/iommufd exposes whether a device needs userspace-managed > coherency. > 3. Userspace explicitly requests and is authorized for that mode > before binding. > 4. Without authorization, today's behavior and failure mode are > unchanged. > 5. Once authorized, the userspace driver owns all DMA-buffer and > descriptor cache maintenance for that device. > > Two examples of the userspace-side mechanism (neither implements > this opt-in contract, and this RFC is not DPDK-specific): > - DPDK stmmac PMD, ARM64 DC CVAC/DC CIVAC at ownership transitions, > under review: > https://lore.kernel.org/all/20261114145539.2470711-1-pkiran@qti.qualcomm.com/ > - enetfec (DPDK + UIO) cache maintenance: > https://mails.dpdk.org/archives/dev/2025-October/325443.html > > To conclude, the intended outcome is not a general coherency bypass. > It is a narrowly scoped, explicit, per-device contract that preserves > IOMMU translation and isolation while allowing an authorized userspace > driver to manage DMA-buffer coherency on hardware that cannot provide > coherent DMA transactions. > --- > drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c | 16 ++++++++++++++++ > drivers/iommu/arm/arm-smmu/arm-smmu.c | 18 +++++++++++++++++- > drivers/iommu/arm/arm-smmu/arm-smmu.h | 24 ++++++++++++++++++++++++ > 3 files changed, 57 insertions(+), 1 deletion(-) > > diff --git a/drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c b/drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c > index 795e8ec7e8fa..264f554ea7d3 100644 > --- a/drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c > +++ b/drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c > @@ -608,6 +608,20 @@ static int qcom_sdm845_smmu500_reset(struct arm_smmu_device *smmu) > return ret; > } > > +static bool qcom_smmu_500_capable(struct device *dev, enum iommu_cap cap) > +{ > + struct arm_smmu_master_cfg *cfg = dev_iommu_priv_get(dev); > + > + switch (cap) { > + case IOMMU_CAP_CACHE_COHERENCY: > + if (!(cfg->smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)) > + return true; > + fallthrough; > + default: > + return arm_smmu_capable_default(dev, cap); > + } > +} > + > static const struct arm_smmu_impl qcom_smmu_v2_impl = { > .init_context = qcom_smmu_init_context, > .cfg_probe = qcom_smmu_cfg_probe, > @@ -623,6 +637,8 @@ static const struct arm_smmu_impl qcom_smmu_500_impl = { > .reset = arm_mmu500_reset, > .write_s2cr = qcom_smmu_write_s2cr, > .tlb_sync = qcom_smmu_tlb_sync, > + .capable = qcom_smmu_500_capable, > + .prot_mask = IOMMU_CACHE, > #ifdef CONFIG_ARM_SMMU_QCOM_DEBUG > .context_fault = qcom_smmu_context_fault, > .context_fault_needs_threaded_irq = true, > diff --git a/drivers/iommu/arm/arm-smmu/arm-smmu.c b/drivers/iommu/arm/arm-smmu/arm-smmu.c > index 798ab2ed5369..324a9e729112 100644 > --- a/drivers/iommu/arm/arm-smmu/arm-smmu.c > +++ b/drivers/iommu/arm/arm-smmu/arm-smmu.c > @@ -1271,6 +1271,11 @@ static int arm_smmu_map_pages(struct iommu_domain *domain, unsigned long iova, > if (!ops) > return -ENODEV; > > + /* Non-coherent walkers cannot safely use cacheable page-table attributes. */ > + if (smmu->impl && smmu->impl->prot_mask && > + !(smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)) > + prot &= ~smmu->impl->prot_mask; > + > arm_smmu_rpm_get(smmu); > ret = ops->map_pages(ops, iova, paddr, pgsize, pgcount, prot, gfp, mapped); > arm_smmu_rpm_put(smmu); > @@ -1394,7 +1399,7 @@ static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain, > return ops->iova_to_phys(ops, iova); > } > > -static bool arm_smmu_capable(struct device *dev, enum iommu_cap cap) > +bool arm_smmu_capable_default(struct device *dev, enum iommu_cap cap) > { > struct arm_smmu_master_cfg *cfg = dev_iommu_priv_get(dev); > > @@ -1416,6 +1421,17 @@ static bool arm_smmu_capable(struct device *dev, enum iommu_cap cap) > } > } > > +bool arm_smmu_capable(struct device *dev, enum iommu_cap cap) > +{ > + struct arm_smmu_master_cfg *cfg = dev_iommu_priv_get(dev); > + > + if (cfg->smmu->impl && cfg->smmu->impl->capable) > + return cfg->smmu->impl->capable(dev, cap); > + > + return arm_smmu_capable_default(dev, cap); > +} > +EXPORT_SYMBOL_GPL(arm_smmu_capable); > + > static > struct arm_smmu_device *arm_smmu_get_by_fwnode(struct fwnode_handle *fwnode) > { > diff --git a/drivers/iommu/arm/arm-smmu/arm-smmu.h b/drivers/iommu/arm/arm-smmu/arm-smmu.h > index 26d2e33cd328..edeb08fb33cf 100644 > --- a/drivers/iommu/arm/arm-smmu/arm-smmu.h > +++ b/drivers/iommu/arm/arm-smmu/arm-smmu.h > @@ -455,8 +455,32 @@ struct arm_smmu_impl { > void (*write_s2cr)(struct arm_smmu_device *smmu, int idx); > void (*write_sctlr)(struct arm_smmu_device *smmu, int idx, u32 reg); > void (*probe_finalize)(struct arm_smmu_device *smmu, struct device *dev); > + /** > + * @capable: Per-implementation override for iommu_cap queries. > + * > + * When set, arm_smmu_capable() calls this hook instead of the > + * default COHERENT_WALK / DEV_DMA_COHERENT feature check. The > + * hook receives the full arm_smmu_master_cfg so it can inspect > + * smmu->features (e.g. ARM_SMMU_FEAT_COHERENT_WALK) to decide > + * whether to override or delegate back to arm_smmu_capable(). > + */ > + bool (*capable)(struct device *dev, enum iommu_cap cap); > + /** > + * @prot_mask: IOMMU_* protection bits to clear in > + * arm_smmu_map_pages() before the flags reach io-pgtable. > + * > + * Applied only when ARM_SMMU_FEAT_COHERENT_WALK is absent on the > + * SMMU instance, so coherent platforms that share the same impl > + * are not affected. Non-coherent instances set IOMMU_CACHE here > + * to prevent arm_lpae_prot_to_pte() from selecting WB+IS > + * attributes on an interconnect that cannot honour them. > + */ > + int prot_mask; > }; > > +bool arm_smmu_capable(struct device *dev, enum iommu_cap cap); > +bool arm_smmu_capable_default(struct device *dev, enum iommu_cap cap); > + > #define INVALID_SMENDX -1 > #define cfg_smendx(cfg, fw, i) \ > (i >= fw->num_ids ? INVALID_SMENDX : cfg->smendx[i]) > > --- > base-commit: 253f86660c1f882fbf554db5eaa34bc5d66dc7e8 > change-id: 20261201-vfio_non_coherent-7ad5a9908d1e > > Best regards, > -- > Bibek Kumar Patro >