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

Jason Gunthorpe
2025-07-04T17:03:07Z

On Fri, Jul 04, 2025 at 05:04:17PM +0100, Will Deacon wrote:
> On Fri, Jul 04, 2025 at 02:21:32PM +0200, David Hildenbrand wrote:
> > On 30.06.25 14:25, Jason Gunthorpe wrote:
> > > 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.
> >
> > 100%
>
> The part I don't understand, however, is that I can't see an MMU notifier
> anywhere on the successful remap_pfn_range() path. So if a driver is
> using that interface to change the mapping properties of a VM_PFNMAP VMA,
> how do we ensure that the guest doesn't use whatever stale mappings it's
> faulted in previously? Did I just miss something?

Generally mmu notifiers are for invalidation, not used when
establishing new mappings.

It is not legal to use remap_pfn_range() to replace already mapped
PTEs. It can only be used during a fop mmap callback to establish the
first mapping during VMA creation. Thus there can be no present
mapping cached in a secondary and no need to invalidate.

To have dynamic mappings you have to use the fault path and a vmf_XX
function - which only happens if the address is non-present, so again
no invalidation.

Jason