Re: [PATCH v6 08/25] KVM: arm64: iommu: Shadow host stage-2 page table
From: Mostafa Saleh
Date: Mon Jul 13 2026 - 12:03:33 EST
On Mon, Jul 13, 2026 at 04:27:16PM +0100, Vincent Donnefort wrote:
> On Mon, Jul 13, 2026 at 04:14:55PM +0100, Vincent Donnefort wrote:
> > On Mon, Jul 13, 2026 at 02:00:31PM +0000, Mostafa Saleh wrote:
> > > On Mon, Jul 13, 2026 at 02:24:19PM +0100, Vincent Donnefort wrote:
> > > > On Fri, May 01, 2026 at 11:19:10AM +0000, Mostafa Saleh wrote:
> > > > > Create a page-table for the IOMMU that shadows the host CPU stage-2
> > > > > to establish DMA isolation.
> > > > >
> > > > > An initial snapshot is created after the driver init, then
> > > > > on every permission change a callback would be called for
> > > > > the IOMMU driver to update the page table.
> > > > >
> > >
> > > [...]
> > >
> > > > > + */
> > > > > + if (pte && !kvm_pte_valid(pte))
> > > > > + return 0;
> > > > > +
> > > > > + if (kvm_pte_valid(pte)) {
> > > > > + prot = pkvm_to_iommu_prot(kvm_pgtable_stage2_pte_prot(pte));
> > > > > + /* If the range is mapped in a single PTE, it must be the same type.*/
> > > > > + if (!addr_is_memory(start))
> > > > > + prot |= IOMMU_MMIO;
> > > > > +
> > > > > + return kvm_iommu_ops->host_stage2_idmap(start, end, prot);
> > > >
> > > > Do we really need to do that when is_memory()?
> > > >
> > > > fix_host_ownership_walker() by calling host_stage2_idmap_locked() and
> > > > host_stage2_set_owner_locked() should already handle the memory region. That
> > > > would also get rid of kvm_idmap_initialized.
> > > >
> > > > So this one here could only take care of the MMIO?
> > > >
> > > > Overall we would have a common point of synchro which is
> > > > fix_host_ownership_walker() after which the host ownership is ready for both
> > > > CPU stage-2 and the IOMMU?
> > > >
> > >
> > > I am not sure I understand, this is another empty page table, so we
> > > have to walk all of the host CPU stage-2 page table to shadow it in the
> > > IOMMU. if you are refering to the case where it handle zero ptes for
> > > memory, I can drop that but it will not change much in this logic.
> >
> > In fixup_host_ownership() we already walk the hyp pgtable to know what needs to be
> > map/unmapped from the host stage-2. Can't we rely on that for the IOMMU
> > page-table as well?
> >
> > As of, fix_host_ownership() could handle the host stage-2 __and__ the iommu?
>
> What I have in mind is the completely untested snippet:
>
> index 75b00c323310..64cd175ad6c0 100644
> --- a/arch/arm64/kvm/hyp/nvhe/setup.c
> +++ b/arch/arm64/kvm/hyp/nvhe/setup.c
> @@ -226,10 +226,11 @@ static int fix_host_ownership_walker(const struct kvm_pgtable_visit_ctx *ctx,
> case PKVM_PAGE_SHARED_OWNED:
> set_hyp_state(page, PKVM_PAGE_SHARED_OWNED);
> set_host_state(page, PKVM_PAGE_SHARED_BORROWED);
> + kvm_iommu_host_stage2_idmap(phys, PAGE_SIZE, IOMMU_READ | IOMMU_WRITE);
> break;
> case PKVM_PAGE_SHARED_BORROWED:
> set_hyp_state(page, PKVM_PAGE_SHARED_BORROWED);
> - set_host_state(page, PKVM_PAGE_SHARED_OWNED);
> + kvm_iommu_host_stage2_idmap(phys, PAGE_SIZE, IOMMU_READ | IOMMU_WRITE);
> break;
> default:
> return -EINVAL;
> @@ -258,15 +259,20 @@ static int fix_host_ownership(void)
> .cb = fix_host_ownership_walker,
> .flags = KVM_PGTABLE_WALK_LEAF,
> };
> + u64 mmio = 0;
> int i, ret;
>
> for (i = 0; i < hyp_memblock_nr; i++) {
> struct memblock_region *reg = &hyp_memory[i];
> u64 start = (u64)hyp_phys_to_virt(reg->base);
>
> + kvm_iommu_host_stage2_idmap(mmio, start, IOMMU_READ | IOMMU_WRITE | IOMMU_MMIO);
> +
> ret = kvm_pgtable_walk(&pkvm_pgtable, start, reg->size, &walker);
> if (ret)
> return ret;
> +
> + mmio = start + reg->size;
> }
>
> I believe this does the same as kvm_iommu_snapshot_host_stage2(). WDYS?
>
I see. However, that does not init the memory owned by the host, neither
unmaps hypervisor owned MMIO.
Besides, as mentioned in the reply, that mean that the IOMMU has to be
init earlier which has it's own problems. So, I do not think that it
is worth changing the current logic.
Thanks,
Mostafa