From 069f9911a35c6191ea0cace0b5b5c8061e9b7720 Mon Sep 17 00:00:00 2001 From: Tom Lendacky Date: Fri, 8 Mar 2024 07:31:11 -0800 Subject: [PATCH] OvmfPkg/BaseMemEncryptSevLib: Maximize Page State Change efficiency BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=4654 When building the Page State Change entries for a range of memory, it can happen that multiple calls to BuildPageStateBuffer() need to be made. If the size of the input work area passed to BuildPageStateBuffer() exceeds the number of entries that can be passed to the hypervisor using the GHCB shared buffer, the Page State Change VMGEXIT support will issue multiple VMGEXITs to process all entries in the buffer. However, it could be that the final VMGEXIT for each round of Page State Changes is only for a small number of entries and subsequent VMGEXITs may still be issued to handle the full range of memory requested. To maximize the number of entries processed during the Page State Change VMGEXIT, limit BuildPageStateBuffer() to not build entries that exceed the maximum number of entries that can be handled in a single Page State Change VMGEXIT. Cc: Ard Biesheuvel Cc: Erdem Aktas Cc: Gerd Hoffmann Cc: Jiewen Yao Cc: Laszlo Ersek Cc: Michael Roth Cc: Min Xu Reviewed-by: Gerd Hoffmann Signed-off-by: Tom Lendacky --- .../X64/SnpPageStateChangeInternal.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/SnpPageStateChangeInternal.c b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/SnpPageStateChangeInternal.c index bcc0798d6b..f1883239a6 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/SnpPageStateChangeInternal.c +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/SnpPageStateChangeInternal.c @@ -145,6 +145,7 @@ BuildPageStateBuffer ( UINTN RmpPageSize; UINTN Index; UINTN IndexMax; + UINTN PscIndexMax; // Clear the page state structure SetMem (Info, InfoSize, 0); @@ -153,6 +154,16 @@ BuildPageStateBuffer ( IndexMax = (InfoSize - sizeof (Info->Header)) / sizeof (Info->Entry[0]); NextAddress = EndAddress; + // + // Make the use of the work area as efficient as possible relative to + // exiting from the guest to the hypervisor. Maximize the number of entries + // that can be processed per exit. + // + PscIndexMax = (IndexMax / SNP_PAGE_STATE_MAX_ENTRY) * SNP_PAGE_STATE_MAX_ENTRY; + if (PscIndexMax > 0) { + IndexMax = MIN (IndexMax, PscIndexMax); + } + // // Populate the page state entry structure //