mbox series

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

Message ID 20250109225533.1841097-1-kevinloughlin@google.com (mailing list archive)
Headers show
Series KVM: SEV: Prefer WBNOINVD over WBINVD for cache maintenance efficiency | expand

Message

Kevin Loughlin Jan. 9, 2025, 10:55 p.m. UTC
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 WBNOINVD similar to how WBINVD
is invoked. Second, check for WBNOINVD support and execute WBNOINVD if
possible in lieu of WBINVD to avoid cache invalidations.

Changelog
---
v2: rebase to tip @ dffeaed35cef, drop unnecessary Xen changes, reword.
---
Kevin Loughlin (2):
  x86, lib: Add WBNOINVD helper functions
  KVM: SEV: Prefer WBNOINVD over WBINVD for cache maintenance efficiency

 arch/x86/include/asm/smp.h           |  7 ++++++
 arch/x86/include/asm/special_insns.h |  7 +++++-
 arch/x86/kvm/svm/sev.c               | 35 +++++++++++++++++-----------
 arch/x86/lib/cache-smp.c             | 12 ++++++++++
 4 files changed, 47 insertions(+), 14 deletions(-)