[RFC PATCH 0/2] KVM: SEV: Prefer WBNOINVD over WBINVD for cache maintenance efficiency

Kevin Loughlin
2024-12-03T01:00:07Z

AMD CPUs currently execute WBINVD in the host when unregistering SEV
guest memory or when deactivating SEV guests. Such cache maintenance is
performed to prevent data corruption, wherein the encrypted (C=1)
version of a dirty cache line might otherwise only be written back
after the memory is written in a different context (ex: C=0), yielding
corruption. However, WBINVD is performance-costly, especially because
it invalidates processor caches.

Strictly-speaking, unless the SEV ASID is being recycled (meaning all
existing cache lines with the recycled ASID must be flushed), the
cache invalidation triggered by WBINVD is unnecessary; only the
writeback is needed to prevent data corruption in remaining scenarios.

To improve performance in these scenarios, use WBNOINVD when available
instead of WBINVD. WBNOINVD still writes back all dirty lines
(preventing host data corruption by SEV guests) but does *not*
invalidate processor caches.

First, provide helper functions to use WBINVD similar to how WBINVD is
invoked. Second, check for WBNOINVD support and execute WBNOINVD if
possible in lieu of WBINVD to avoid cache invalidations

Kevin Loughlin (2):
x86, lib, xenpv: Add WBNOINVD helper functions
KVM: SEV: Prefer WBNOINVD over WBINVD for cache maintenance efficiency

arch/x86/include/asm/paravirt.h | 7 ++++++
arch/x86/include/asm/paravirt_types.h | 1 +
arch/x86/include/asm/smp.h | 7 ++++++
arch/x86/include/asm/special_insns.h | 12 ++++++++-
arch/x86/kernel/paravirt.c | 6 +++++
arch/x86/kvm/svm/sev.c | 35 +++++++++++++++++----------
arch/x86/lib/cache-smp.c | 12 +++++++++
arch/x86/xen/enlighten_pv.c | 1 +
8 files changed, 67 insertions(+), 14 deletions(-)

--
2.47.0.338.g60cca15819-goog