From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-io1-f73.google.com (mail-io1-f73.google.com [209.85.166.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 70A7D2054F9 for ; Thu, 9 Jan 2025 22:55:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736463354; cv=none; b=JJRwa8WqDwI27iT9UGFjvIFU9kywvq4uiqSu4667VhzMC5PDu2jHPmmH+MVvqlRvtbBA0J9IRytB0MbOQZZf8+50q9gMU2M6KEJwfzqnorBE26PYIHoEbbRtmMjEfl7Qz9CwYlsAu1BRh9o/idtLM6FziCtim4BkgLdXNVBajUQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736463354; c=relaxed/simple; bh=W0EseXR27uaH+2rvnliUPd4fCcSkw5GgQuEBIx6U3zU=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=MWH0Ibd8tcM8tirBCiZICNrIK6LBGADacb3mQuWzJhXYXJ3XGdG4cirpMY4MjoDRfswJUjDLXUf/uBIsjyp7VE41FZFxa8vaOLcHwRopXxHQsIYFb6t/cpe9YNx0XzUg2MXF7xZQnFpYDJWTAO2V7HCc728GejmKU0GLVlDlXZs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--kevinloughlin.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Mk+/AiHG; arc=none smtp.client-ip=209.85.166.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--kevinloughlin.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Mk+/AiHG" Received: by mail-io1-f73.google.com with SMTP id ca18e2360f4ac-844eac513efso116653139f.2 for ; Thu, 09 Jan 2025 14:55:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736463351; x=1737068151; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=Bg7OFBM7Iyaw8rDI2YVgCZ6Mugo0msQNybpcVxWKPAk=; b=Mk+/AiHGr9+KJIhMHEGDY38scxPYGrcVUpYUGefjeNRYbar5IPKaxtyO1NaUvQS+z1 Q5PCfgscxtvwVS4Qc61FqzYVtrsdGcIf/Nl7gfMvmSLqT5lmyXEHp7APbCz1FeiDDuwP elRnIxG5ZnRbaO+qctWEBg5bnmUoSwyIDrpa15PmCgFXM/kXFE/hv9H7M6Ekl5V8tBRO N2MGG+kuGJVR6PSbHvDaZHGOjKxw+wxFZUkGKyiL1DUBFBPt8yFZjLx4sCykX5JibEnA dMQsr8leVCCIRZQvgc+30q24XBEJ57YTr23of5CH3lcX8g0isdPcvLFSmlQoo6XkxJXy Lx9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736463351; x=1737068151; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=Bg7OFBM7Iyaw8rDI2YVgCZ6Mugo0msQNybpcVxWKPAk=; b=TmHBoNZrn3pgDz2GSHxWSES/Tripkc8AxNs6vnuLSfUsqzmvVnGjQKI0w4GkRuAnTX Ry9fcVvyrCDBR3HAmsc/wF1hGrr9YHvBFbYiP2/cc23FO13YlWU+FjN33CxXJi4xZAP3 g9Tmkm/c/oiZIvVzqaGX/1KpL2Wk43c+ksY+qs4ycT1Lr5dmIXNye4qXsWw61IVSdTai /mOqOE0fEAJWYN2FDFJ9Sr+OXj+/mXXtKsmRDh6W6Y6mu2ZrdWIH0RiORyNU2GLfjlp+ eUMaOYBSfZ1ve/p0vhBMkQ+eqWKTeAWIYeRxFNaQ2g/JKCYo6cHapi57o4zlWl+2l5ic g+MQ== X-Gm-Message-State: AOJu0Yy19mZtnRTiIM4rf0twurI+Tr7aeRbX3Tl5levYU3DcdcLhs9wc vEtlKvHxRWN7Raqc0a1kyOCtwAq4K22MeMTnH3dzHdqPYX16psGfi7U+ELKU2agE6KSQgwGBdD0 8aY9YGmy9Cuj8Kv0nezR6wzFEmCOmYWxkrn/ucMdUyS6sr1chmA08sEG+OBOriYha3CL/SunnVa epUNneKd5q+V9yo09ESpKU4HAnACBmINN4SFJ/jAgeBCUDRy32zWRMZemB+HVGpd29GLBGgLiAY Em0Ww== X-Google-Smtp-Source: AGHT+IH6TwFJV+BPqg6h8Oejd7g1Ev34mvyO1X35nslxu87ElQQW1f6+tkLEnW4PrqmqE5yOxXQe7iLwl7b7G2zh6NZr X-Received: from iobee12.prod.google.com ([2002:a05:6602:488c:b0:83a:b10a:a59a]) (user=kevinloughlin job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6602:2c07:b0:84c:ea14:9315 with SMTP id ca18e2360f4ac-84cea14982amr563273639f.12.1736463351528; Thu, 09 Jan 2025 14:55:51 -0800 (PST) Date: Thu, 9 Jan 2025 22:55:31 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.47.1.688.g23fc6f90ad-goog Message-ID: <20250109225533.1841097-1-kevinloughlin@google.com> Subject: [PATCH v2 0/2] KVM: SEV: Prefer WBNOINVD over WBINVD for cache maintenance efficiency From: Kevin Loughlin To: linux-kernel@vger.kernel.org Cc: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, seanjc@google.com, pbonzini@redhat.com, kevinloughlin@google.com, kirill.shutemov@linux.intel.com, kai.huang@intel.com, ubizjak@gmail.com, dave.jiang@intel.com, jgross@suse.com, kvm@vger.kernel.org, thomas.lendacky@amd.com, pgonda@google.com, sidtelang@google.com, mizhang@google.com, rientjes@google.com, szy0127@sjtu.edu.cn Content-Type: text/plain; charset="UTF-8" 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(-) -- 2.47.1.688.g23fc6f90ad-goog