Re: [RFC PATCH 6/8] KVM: x86: Implement kvm_arch_{, pre_}vcpu_map_memory()

Huang, Kai
2024-03-12T21:46:21Z

On Tue, 2024-03-12 at 21:41 +0000, Huang, Kai wrote:
> On Tue, 2024-03-12 at 07:20 -0700, Sean Christopherson wrote:
> > On Tue, Mar 12, 2024, Kai Huang wrote:
> > > > Wait. KVM doesn't *need* to do PAGE.ADD from deep in the MMU. The only inputs to
> > > > PAGE.ADD are the gfn, pfn, tdr (vm), and source. The S-EPT structures need to be
> > > > pre-built, but when they are built is irrelevant, so long as they are in place
> > > > before PAGE.ADD.
> > > >
> > > > Crazy idea. For TDX S-EPT, what if KVM_MAP_MEMORY does all of the SEPT.ADD stuff,
> > > > which doesn't affect the measurement, and even fills in KVM's copy of the leaf EPTE,
> > > > but tdx_sept_set_private_spte() doesn't do anything if the TD isn't finalized?
> > > >
> > > > Then KVM provides a dedicated TDX ioctl(), i.e. what is/was KVM_TDX_INIT_MEM_REGION,
> > > > to do PAGE.ADD. KVM_TDX_INIT_MEM_REGION wouldn't need to map anything, it would
> > > > simply need to verify that the pfn from guest_memfd() is the same as what's in
> > > > the TDP MMU.
> > >
> > > One small question:
> > >
> > > What if the memory region passed to KVM_TDX_INIT_MEM_REGION hasn't been pre-
> > > populated? If we want to make KVM_TDX_INIT_MEM_REGION work with these regions,
> > > then we still need to do the real map. Or we can make KVM_TDX_INIT_MEM_REGION
> > > return error when it finds the region hasn't been pre-populated?
> >
> > Return an error. I don't love the idea of bleeding so many TDX details into
> > userspace, but I'm pretty sure that ship sailed a long, long time ago.
>
> In this case, IIUC the KVM_MAP_MEMORY ioctl() will be mandatory for TDX
> (presumbly also SNP) guests, but _optional_ for other VMs. Not sure whether
> this is ideal.
>
> And just want to make sure I understand the background correctly:
>
> The KVM_MAP_MEMORY ioctl() is supposed to be generic, and it should be able to
> be used by any VM but not just CoCo VMs (including SW_PROTECTED ones)?
>
> But it is only supposed to be used by the VMs which use guest_memfd()? Because
> IIUC for normal VMs using mmap() we already have MAP_POPULATE for this purpose.
>
> Looking at [*], it doesn't say what kind of VM the sender was trying to use.
>
> Therefore can we interpret KVM_MAP_MEMORY ioctl() is effectively for CoCo VMs?
> SW_PROTECTED VMs can also use guest_memfd(), but I believe nobody is going to
> use it seriously.
>
> [*] https://lore.kernel.org/all/65262e67-7885-971a-896d-ad9c0a760907@polito.it/
>
>

Hmm.. Just after sending I realized the MAP_POPULATE only pre-populate page
table at host side, not EPT...

So the KVM_MAP_MEMORY is indeed can be used by _ALL_ VMs. My bad :-(