Re: [PATCH v9 3/6] KVM: arm64: Block cacheable PFNMAP mapping

Jason Gunthorpe
2025-06-30T12:43:47Z

On Mon, Jun 30, 2025 at 01:56:43AM +0000, Ankit Agrawal wrote:
> > Sorry for the drive-by comment, but I was looking at this old series from
> > Paolo (look at the cover letter and patch 5):
> >
> > https://lore.kernel.org/r/20250109133817.314401-1-pbonzini@redhat.com
> >
> > in which he points out that the arm64 get_vma_page_shift() function
> > incorrectly assumes that a VM_PFNMAP VMA is physically contiguous, which
> > may not be the case if a driver calls remap_pfn_range() to mess around
> > with mappings within the VMA. I think that implies that the optimisation
> > in 2aa53d68cee6 ("KVM: arm64: Try stage2 block mapping for host device
> > MMIO") is unsound.
>
> Hm yeah, that does seem problematic. Perhaps we need a new
> vma flag that could help the driver communicate to the KVM that the
> mapping is contiguous and it can go ahead with the optimization?
> E.g. something similar to VM_ALLOW_ANY_UNCACHED.

I think Paolo has the right direction - remove any attempts by KVM to
expand contiguity, it should only copy the primary's PTEs and rely on
the primary to discover contiguity. No new flags.

Instead we need to keep pushing forward with Peter Xu's work to have
PFNMAPs use large PTEs. I believe ARM still has some missing parts for
full support bot overall it is a far better direction.

> > But it got me thinking -- given that remap_pfn_range() also takes a 'prot'
> > argument, how do we ensure that this is reflected in the guest? It feels
> > a bit dodgy to rely on drivers always passing 'vma->vm_page_prot'.

But broadly, if you take that perspective KVM as a misdesign.. KVM
should be getting the VMA's PTE so that the S2 PTE can duplicate the
page-by-page flags, instead of only copying the writable bit.

So, let's call this out of scope for this series. ARM64 KVM does not
support page-by-page cachability, it never has, and there is no known
use case right now.. The only user of VMA_CACHED PFNMAPs is being
consistent.

Jason