Re: [RFC PATCH v2 9/9] iommu/riscv: Add riscv_iommu_attach_dev_nested
From: Gong Shuai
Date: Tue Nov 10 2026 - 07:04:40 EST
Hi Tan Chi,
Thanks for your review and for pointing these out.
> Hi Shuai and Fei,
>
> I noticed a potential issue with fixing the physical DC's PSCID to 0.
>
> In QEMU, different VFIO devices can have separate stage-1 HWPTs while
> sharing the same vIOMMU and stage-2 parent. This means two devices may
> have different first-stage page table roots but the same host GSCID
> and PSCID=0.
>
> According to the RISC-V IOMMU specification, DC.ta.PSCID identifies
> the first-stage address space when PDTV=0 and the first stage is not
> Bare. Could this cause the physical IOMMU to reuse cached translations
> across different stage-1 address spaces? Even broader invalidations
> would not prevent such cache aliasing.
You are right. We kept PSCID at zero as a simplification for the RFC,
but this was based on a misunderstanding of the PSCID semantics. When
PDTV=0 and stage 1 is not Bare, DC.ta.PSCID identifies the S1
address space. Sharing it across different page tables can cause cached
translations to alias.
>
> There also seems to be a related issue in patch 6.
> riscv_viommu_cache_invalidate() unconditionally forces PSCV=1 and
> PSCID=0. However, an IOTINVAL.VMA with PSCV=0 also invalidates global
> mappings, while PSCV=1 excludes them. This appears to narrow the
> guest's requested invalidation scope and could leave stale global
> translations cached.
Agreed.
>
> One possible approach might be to use the PSCID from the guest-provided
> DC.ta when allocating the nested domain, and preserve the guest's PSCID
> and PSCV settings during invalidation while substituting the host GSCID.
> I'm not sure whether additional considerations are needed for PSCID
> reuse or domain lifetime, though.
Thanks for the suggestion. While analyzing this issue, we found that
using the guest PSCID is the right direction, but preserving it alone
is not sufficient when multiple vIOMMUs share the same host GSCID.
Different vIOMMUs may have independent guest PSCID namespaces, so two
vIOMMUs sharing an S2 parent may use the same guest PSCID for different
S1 address spaces.
My understanding is that the ultimate goal is to ensure that the
(GSCID, PSCID) pair programmed into the physical DC uniquely identifies
the correct nested address space, and therefore the cached translations
belonging to it.
So far, we have identified three possible approaches:
1. Share the S2 parent's GSCID and use the guest PSCID directly.
This requires PSCID uniqueness across all vIOMMUs sharing that S2
parent, so that different S1 address spaces cannot be assigned the
same (GSCID, PSCID) pair. This uniqueness can be provided by the
guest itself or through VMM/guest coordination.
The current ARM mainline model similarly uses the guest ASID directly
while inheriting the VMID from the S2 parent. For Linux guests, the
SMMUv3 driver's global ASID allocator can provide ASID uniqueness
across vSMMUs. The Linux RISC-V driver also uses a global PSCID
allocator. The main question is whether we want to rely on this
guest implementation property.
2. Use the guest PSCID directly, but allocate a separate host GSCID for
each vIOMMU.
This allows independent vIOMMUs to reuse the same guest PSCID without
aliasing, even when they share an S2 parent.
The proposed ARM series follows this model by moving VMID allocation
from the S2 domain to the vSMMU [1].
3. Share the GSCID and maintain a per-vIOMMU mapping from each guest
PSCID to a host-allocated PSCID.
AMD mainline uses an analogous model for DomID. It maintains a
per-vIOMMU gDomID-to-hDomID mapping and programs the mapped hDomID
into the physical DTE.
Option 2 seems better because it gives each vIOMMU its own GSCID, which
isolates the PSCID namespace of each vIOMMU without requiring a mapping
table or relying on a guest implementation property such as a global
PSCID allocator. Its main costs are additional GSCID consumption and the
need to invalidate each vIOMMU's GSCID whenever the shared S2 parent
changes. I would appreciate your thoughts on which approach we should
take.
[1] https://lore.kernel.org/lkml/05dd00dcb2f0d077f59bcbccac1820534ad7b5cf.1773949042.git.nicolinc@xxxxxxxxxx/
Thanks,
Shuai
>
> Thanks,
> Tan Chi
>