Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Robin Murphy <robin.murphy@arm.com>
To: Bibek Kumar Patro <bibek.patro@oss.qualcomm.com>,
	Rob Clark <robin.clark@oss.qualcomm.com>,
	Will Deacon <will@kernel.org>,
	"Joerg Roedel (AMD)" <joro@8bytes.org>
Cc: iommu@lists.linux.dev, linux-arm-msm@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	Prakash Gupta <prakash.gupta@oss.qualcomm.com>
Subject: Re: [PATCH RFC] iommu/arm-smmu: allow per-impl capability override and prot masking
Date: Tue, 1 Dec 2026 19:46:49 +0000	[thread overview]
Message-ID: <cc994cf3-0b04-4a4d-8160-c5a823edafff@arm.com> (raw)
In-Reply-To: <20261201-vfio_non_coherent-v1-1-0e6f78e111bb@oss.qualcomm.com>

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 <prakash.gupta@oss.qualcomm.com>
> Signed-off-by: Bibek Kumar Patro <bibek.patro@oss.qualcomm.com>
> ---
> 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 <bibek.patro@oss.qualcomm.com>
> 



      reply	other threads:[~2026-12-01 19:47 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 15:56 [PATCH RFC] iommu/arm-smmu: allow per-impl capability override and prot masking Bibek Kumar Patro
2026-12-01 19:46 ` Robin Murphy [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=cc994cf3-0b04-4a4d-8160-c5a823edafff@arm.com \
    --to=robin.murphy@arm.com \
    --cc=bibek.patro@oss.qualcomm.com \
    --cc=iommu@lists.linux.dev \
    --cc=joro@8bytes.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=prakash.gupta@oss.qualcomm.com \
    --cc=robin.clark@oss.qualcomm.com \
    --cc=will@kernel.org \
    /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