Re: [PATCH 05/21] KVM: VMX: Teach EPT violation helper about private mem

From: Huang, Kai
Date: Wed Sep 11 2024 - 21:20:26 EST




On 4/09/2024 3:07 pm, Rick Edgecombe wrote:
Teach EPT violation helper to check shared mask of a GPA to find out
whether the GPA is for private memory.

When EPT violation is triggered after TD accessing a private GPA, KVM will
exit to user space if the corresponding GFN's attribute is not private.
User space will then update GFN's attribute during its memory conversion
process. After that, TD will re-access the private GPA and trigger EPT
violation again. Only with GFN's attribute matches to private, KVM will
fault in private page, map it in mirrored TDP root, and propagate changes
to private EPT to resolve the EPT violation.

Relying on GFN's attribute tracking xarray to determine if a GFN is
private, as for KVM_X86_SW_PROTECTED_VM, may lead to endless EPT
violations.

Sorry for not finishing in the previous reply:

IMHO in the very beginning of fault handler, we should just use hardware as the source to determine whether a *faulting* GPA is private or not. It doesn't quite matter whether KVM maintains memory attributes and how does it handle based on it -- it just must handle this properly.

E.g., even using memory attributes (to determine private) won't lead to endless EPT violations, it is wrong to use it to determine, because at the beginning of fault handler, we must know the *hardware* behaviour.

So I think the changelog should be something like this (the title could be enhanced too perhaps):

When TDX guests access memory causes EPT violation, TDX determines whether the faulting GPA is private or shared by checking whether the faulting GPA contains the shared bit (either bit 47 or bit 51 depending on the configuration of the guest).

KVM maintains an Xarray to record whether a GPA is private or not, e.g., for KVM_X86_SW_PROTECTED_VM guests. TDX needs to honor this too. The memory attributes (private or shared) for a given GPA that KVM records may not match the type of the faulting GPA. E.g., the TDX guest can explicitly convert memory type from private to shared or the opposite. In this case KVM will exit to userspace to handle (e.g., change to the new memory attributes, issue the memory conversion and go back to guest). After KVM determines the faulting type is legal and can proceed, it sets up the actual mapping, using TDX-specific ops for private one.

The common KVM fault handler uses the PFERR_PRIVATE_ACCESS bit of the error code to tell whether a faulting GPA is private. Check the faulting GPA for TDX and convert it to the PFERR_PRIVATE_ACCESS so the common code can handle.

The specific operations to setup private mapping when the faulting GPA is private will follow in future patches.