From nobody Wed Jan 22 09:59:50 2025 Received: from mail-io1-f74.google.com (mail-io1-f74.google.com [209.85.166.74]) (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 4F7D69463 for ; Wed, 22 Jan 2025 00:14:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737504852; cv=none; b=ruQ5vHFw/tI1pW6zTK9+QQpoB19q1rfYX+FPn63HaleUNSiSyJSXpERSPVKaQiONNvwLlar4+XgLMw/P25EHwk1Ys/55lu32X9eIbkCca3q0pmDy0zvb2gONIHbayuMK4XhIrTDMtVB8WmW0Ziovo0tg0PExr4MjZNwABZUR758= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737504852; c=relaxed/simple; bh=pavw5Q/YtL/Q2BNOYCX6WtkCWskWvTT3iu40/uQ7w3Q=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ZDmPgIikZ0Big9USAq/dLrQGPBEcS8JRe2HYgX9jLFFdvusokSTCbbwYNVMJXPUo8BcO3WAc1ZyUey4PdVyJ7r61Cz+xuem9Kr8nUvE3+uJ2BUeUY7P5oDgjIeofPzEeWEs4SKK/UQo6T2K+3dMzOgOGW1ZTbVlAv0E2ulEieTQ= 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=x8HEX7mw; arc=none smtp.client-ip=209.85.166.74 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="x8HEX7mw" Received: by mail-io1-f74.google.com with SMTP id ca18e2360f4ac-844dfe8dad5so884072639f.1 for ; Tue, 21 Jan 2025 16:14:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737504850; x=1738109650; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ACQ8RHKDxdTw4Fdpix6AB+QY5l9iusJFQGzSKvRZf9w=; b=x8HEX7mwP9gDAhqHIJUcWL50DCHMKlzCFEWVogjswzmLt6Ex8FWuOw5CixT1RiNBUq FglrD7kP12ipNIWH9LLJE9Gy6jwa0SYi1VbgcKX69Ig4amfHgTth5r9GxbtRwnjuQ3Vu KVuIRNaC+G5h4L7TjoG6J6hwQadPpo6T64I6f8uziuzKJ/wTkZ6cd5m18Qwr0zf2Alqk UuQczqqX6+sC6G96uGyohEmppI46WDVQt9HWv7xYOz8FmNbE1v6phWnMBgHXTYspuPtR g+2KziEmosGdoM93dCE3ImXuYVd5X1Yd7v98OeTfr6R4yil4msP8i7nSBcuDfS9H8yKF 5FLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737504850; x=1738109650; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ACQ8RHKDxdTw4Fdpix6AB+QY5l9iusJFQGzSKvRZf9w=; b=vO+oVPgVNa96pczFkAMNqh57E749PtcQ3z48Y/cJNtuzGjejrtyqfLqIUPf4WnjfzJ sOJKfGW0/3P6vSnIm7qoYX4WKY8FgoCa5C2SZTUq/xvGis1ZA5/iQw5cBcXVVu2P5cgG kqBIboOrxPiN+TPgtUxDmljtNWVi2yGyCXD/OvIwuY2CcJsUj9dpz3ALBuh5jENg6FTa 6nef+5aGJ+Lzo93099SBwbHuu+cnxqT3v/WmKH9Yn6QUOn5lBAii88+Hs5KK9JYu2jLe Xts/C4Zzwr4sy3G/FVNb8bzzHCkLAtAhDwlUPL5+fV01d/XLUAs68c0b3iXOxIzefoIJ LyrA== X-Gm-Message-State: AOJu0Yy7xgxWn33kUvQ9R7l01T4gG9fAakFIBnNp8eqXhB9fQvFnxljD AyPakcr9JJfWo/LocL8VbrsMk7eCCp5pHj1+99wk5BjRrESwoTYpZjL4wIC56KCHNZP+yUgYDk9 wvoRJJinQWHZKZTAbrnU4ls9l2R9BnwAQosJVGEJICsjzSnjM7/B/zpwXHpc5LHpve6QoCu7fUw HwuGL5UznBPXcVqmgM9uvovMcNwLCbOxiDrnqFTwti1k8mhOQLcJdbEJv1zdWNsYG2DQg5zr3aF oPt/w== X-Google-Smtp-Source: AGHT+IGzHze8zH6E7AZfedxE1Bu2uF2bJ9lTyRygPYdEojRb8Oibvph7VH1HQ03BFDj1dJe2iqHpAdN8TQTdgT/gaZVs X-Received: from iobhb10.prod.google.com ([2002:a05:6602:678a:b0:84f:4cc1:bae3]) (user=kevinloughlin job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6602:26c5:b0:83b:5221:2a87 with SMTP id ca18e2360f4ac-851b616986dmr1741327739f.3.1737504850348; Tue, 21 Jan 2025 16:14:10 -0800 (PST) Date: Wed, 22 Jan 2025 00:13:28 +0000 In-Reply-To: <20250122001329.647970-1-kevinloughlin@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250109225533.1841097-1-kevinloughlin@google.com> <20250122001329.647970-1-kevinloughlin@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250122001329.647970-2-kevinloughlin@google.com> Subject: [PATCH v3 1/2] x86, lib: Add WBNOINVD helper functions 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, jgross@suse.com, kvm@vger.kernel.org, thomas.lendacky@amd.com, pgonda@google.com, sidtelang@google.com, mizhang@google.com, rientjes@google.com, manalinandan@google.com, szy0127@sjtu.edu.cn Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In line with WBINVD usage, add WBONINVD helper functions. For the wbnoinvd() helper, fall back to WBINVD if X86_FEATURE_WBNOINVD is not present. Signed-off-by: Kevin Loughlin --- arch/x86/include/asm/smp.h | 7 +++++++ arch/x86/include/asm/special_insns.h | 7 ++++++- arch/x86/lib/cache-smp.c | 12 ++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/smp.h b/arch/x86/include/asm/smp.h index ca073f40698f..ecf93a243b83 100644 --- a/arch/x86/include/asm/smp.h +++ b/arch/x86/include/asm/smp.h @@ -112,6 +112,7 @@ void native_play_dead(void); void play_dead_common(void); void wbinvd_on_cpu(int cpu); int wbinvd_on_all_cpus(void); +int wbnoinvd_on_all_cpus(void); =20 void smp_kick_mwait_play_dead(void); =20 @@ -160,6 +161,12 @@ static inline int wbinvd_on_all_cpus(void) return 0; } =20 +static inline int wbnoinvd_on_all_cpus(void) +{ + wbnoinvd(); + return 0; +} + static inline struct cpumask *cpu_llc_shared_mask(int cpu) { return (struct cpumask *)cpumask_of(0); diff --git a/arch/x86/include/asm/special_insns.h b/arch/x86/include/asm/sp= ecial_insns.h index 03e7c2d49559..bd2eb7430cd6 100644 --- a/arch/x86/include/asm/special_insns.h +++ b/arch/x86/include/asm/special_insns.h @@ -117,7 +117,12 @@ static inline void wrpkru(u32 pkru) =20 static __always_inline void wbinvd(void) { - asm volatile("wbinvd": : :"memory"); + asm volatile("wbinvd" : : : "memory"); +} + +static __always_inline void wbnoinvd(void) +{ + alternative("wbinvd", "wbnoinvd", X86_FEATURE_WBNOINVD); } =20 static inline unsigned long __read_cr4(void) diff --git a/arch/x86/lib/cache-smp.c b/arch/x86/lib/cache-smp.c index 7af743bd3b13..7ac5cca53031 100644 --- a/arch/x86/lib/cache-smp.c +++ b/arch/x86/lib/cache-smp.c @@ -20,3 +20,15 @@ int wbinvd_on_all_cpus(void) return 0; } EXPORT_SYMBOL(wbinvd_on_all_cpus); + +static void __wbnoinvd(void *dummy) +{ + wbnoinvd(); +} + +int wbnoinvd_on_all_cpus(void) +{ + on_each_cpu(__wbnoinvd, NULL, 1); + return 0; +} +EXPORT_SYMBOL(wbnoinvd_on_all_cpus); --=20 2.48.1.262.g85cc9f2d1e-goog From nobody Wed Jan 22 09:59:50 2025 Received: from mail-il1-f201.google.com (mail-il1-f201.google.com [209.85.166.201]) (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 465C315AF6 for ; Wed, 22 Jan 2025 00:14:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737504856; cv=none; b=cnEu0BQ3JsClai7iAeY7kGsZFTP9FJ34NHu2z30fx5ZPxOqqipjuieRIUR3TzVkIuPAbkGYMJDCFSNXyQWnTSJ0TVEJfFoL+W5/MI4WXjyA2OzWf65QuN2WHCr1QEdk72Wt4TiR2UtrNWh+4jHJt68WBirSm1lfrUkr8OmwMWVk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737504856; c=relaxed/simple; bh=yPf27K03/uVqqrLt0hh23/xHA+vw8UXWLwpu1KmsK20=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Ee2uXqypgmG7NQeuEl+kAM4hYyti0+5oh7M/Khy9Hj6qO6iwPyjMk827gSD9/jPFw4MWyWy12DQL67F0kHHhF7trs0u2uOpEEzLUOBqu2xQIPvejj3/kGMZ2bZVjjXGZKdgfvWuHCujIVG7iG3Pmzm4QPfDmxA82lpdKtpCEzTs= 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=Mz/txDfB; arc=none smtp.client-ip=209.85.166.201 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="Mz/txDfB" Received: by mail-il1-f201.google.com with SMTP id e9e14a558f8ab-3a9c9b37244so104092925ab.1 for ; Tue, 21 Jan 2025 16:14:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737504854; x=1738109654; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=o1kWLqC/0VVh3Ec7HiMF8h2C2kgo4gHLLUGbm+ZaoH4=; b=Mz/txDfBCfH3IfImCF+OCNlvZD4Oktau31dKrcg6QN1E/MOQiUdkBwL7TOSzntGWe+ OO3mJLhxfXvYgwZj+8N3hTNidNNhN3F0qkP9gzuijKBNHydyYsrgE2Prhm0VtpwrferM 0Zq9Hy4BGydH61MtDmNAnAYS17rfArYDWe6M2TfXUL9AhT/B4H1DtWs3UNhXqzcfYb8J Q3hsR/X1FFS7X8s4dpJdeibndmdQR+0kl2RotDGiA9kWAYXRMrYJh8r8QCBFy7Q3Uwmw 0UZgdHovTOS8pNZigXYLR8aFTEA+wV3+X7zUi6IOzteTQZ0MHfw9da80/lVUctaMA3P5 6VmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737504854; x=1738109654; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=o1kWLqC/0VVh3Ec7HiMF8h2C2kgo4gHLLUGbm+ZaoH4=; b=HIPTFGk22gjVkRmprDY9LIdczVIRGLyuAHPxAM635wsi9OzzDR4zTu3v1kGqxlW+CR ESlPbz6fM2i4djbbqbUkFUhr1cu+bILtHOVEsTI91tJrt2n/MAXNlDYvWhoxnFxFu6K8 wKL2taNN1dqIl03NtaJQiJzk7B/H5MrizTtEAB5qnjsUxusSrUtK/SRN4zAQv0PTo5Jf Bg7R+X2G8ENEXUoguTmcN/iR+92j9BkrX/+eONupbNV+le6dxwN/nvxpuqLHEX9mbzVD V+QFATUPkQ5pZIwj5wf0dPlNh4YaIYJTrwE3yZ3HdiPJrstuzSR6W5MkLHhvGtCJJFSf 3JwA== X-Gm-Message-State: AOJu0YyM7JlwtZmLU5e0wPlJtSZYe76u1LD1BNFK/v5AGNv6ZYE4mkxX sneEl37jj8qvfZWEEWVMFqeb7T6zxRU9CJYmbtnJnNj0Fs6rUG0dRk1DDE5tm/flaB5P/x9ZFyl 9xeFvezRskow4KUtqhZGHN/MTOu3hWmsP1fj9Z4bnedhRvTQ2zE+Xp0Kqj0O8u2sGrYaTy7ZGnw S6cADWgETj6b3uIwghuoYM2HCAi+jwG+Jhb+F7hyciZfPID5PV1iO0mkuAmh+pb5KAmCovJZ6fh w7AQg== X-Google-Smtp-Source: AGHT+IG305xU5CD0b2jVDKdRvmBdRl1YZXRs3dTwFnmSMf0/bvpcEMobbX1Bp+YetUiisp6N5NyrhFzXMOPYd/tSNjeP X-Received: from ilbcr4.prod.google.com ([2002:a05:6e02:3a84:b0:3ce:8579:c1eb]) (user=kevinloughlin job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6e02:1789:b0:3ce:8e89:90c2 with SMTP id e9e14a558f8ab-3cf7443c362mr158017415ab.13.1737504854292; Tue, 21 Jan 2025 16:14:14 -0800 (PST) Date: Wed, 22 Jan 2025 00:13:29 +0000 In-Reply-To: <20250122001329.647970-1-kevinloughlin@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250109225533.1841097-1-kevinloughlin@google.com> <20250122001329.647970-1-kevinloughlin@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250122001329.647970-3-kevinloughlin@google.com> Subject: [PATCH v3 2/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, jgross@suse.com, kvm@vger.kernel.org, thomas.lendacky@amd.com, pgonda@google.com, sidtelang@google.com, mizhang@google.com, rientjes@google.com, manalinandan@google.com, szy0127@sjtu.edu.cn Content-Transfer-Encoding: quoted-printable 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=3D1) version of a dirty cache line might otherwise only be written back after the memory is written in a different context (ex: C=3D0), yielding corruption. However, WBINVD is performance-costly, especially because it invalidates processor caches. Strictly-speaking, unless the SEV ASID is being recycled (meaning the SNP firmware requires the use of WBINVD prior to DF_FLUSH), 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. Note that the implementation of wbnoinvd() ensures fall back to WBINVD if WBNOINVD is unavailable. In anticipation of forthcoming optimizations to limit the WBNOINVD only to physical CPUs that have executed SEV guests, place the call to wbnoinvd_on_all_cpus() in a wrapper function sev_writeback_caches(). Signed-off-by: Kevin Loughlin Reviewed-by: Mingwei Zhang --- arch/x86/kvm/svm/sev.c | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index fe6cc763fd51..f10f1c53345e 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -116,6 +116,7 @@ static int sev_flush_asids(unsigned int min_asid, unsig= ned int max_asid) */ down_write(&sev_deactivate_lock); =20 + /* SNP firmware requires use of WBINVD for ASID recycling. */ wbinvd_on_all_cpus(); =20 if (sev_snp_enabled) @@ -710,6 +711,16 @@ static void sev_clflush_pages(struct page *pages[], un= signed long npages) } } =20 +static inline void sev_writeback_caches(void) +{ + /* + * Ensure that all dirty guest tagged cache entries are written back + * before releasing the pages back to the system for use. CLFLUSH will + * not do this without SME_COHERENT, so issue a WBNOINVD. + */ + wbnoinvd_on_all_cpus(); +} + static unsigned long get_num_contig_pages(unsigned long idx, struct page **inpages, unsigned long npages) { @@ -2773,12 +2784,7 @@ int sev_mem_enc_unregister_region(struct kvm *kvm, goto failed; } =20 - /* - * Ensure that all guest tagged cache entries are flushed before - * releasing the pages back to the system for use. CLFLUSH will - * not do this, so issue a WBINVD. - */ - wbinvd_on_all_cpus(); + sev_writeback_caches(); =20 __unregister_enc_region_locked(kvm, region); =20 @@ -2899,12 +2905,7 @@ void sev_vm_destroy(struct kvm *kvm) return; } =20 - /* - * Ensure that all guest tagged cache entries are flushed before - * releasing the pages back to the system for use. CLFLUSH will - * not do this, so issue a WBINVD. - */ - wbinvd_on_all_cpus(); + sev_writeback_caches(); =20 /* * if userspace was terminated before unregistering the memory regions @@ -3126,16 +3127,16 @@ static void sev_flush_encrypted_page(struct kvm_vcp= u *vcpu, void *va) =20 /* * VM Page Flush takes a host virtual address and a guest ASID. Fall - * back to WBINVD if this faults so as not to make any problems worse + * back to WBNOINVD if this faults so as not to make any problems worse * by leaving stale encrypted data in the cache. */ if (WARN_ON_ONCE(wrmsrl_safe(MSR_AMD64_VM_PAGE_FLUSH, addr | asid))) - goto do_wbinvd; + goto do_sev_writeback_caches; =20 return; =20 -do_wbinvd: - wbinvd_on_all_cpus(); +do_sev_writeback_caches: + sev_writeback_caches(); } =20 void sev_guest_memory_reclaimed(struct kvm *kvm) @@ -3144,12 +3145,12 @@ void sev_guest_memory_reclaimed(struct kvm *kvm) * With SNP+gmem, private/encrypted memory is unreachable via the * hva-based mmu notifiers, so these events are only actually * pertaining to shared pages where there is no need to perform - * the WBINVD to flush associated caches. + * the WBNOINVD to flush associated caches. */ if (!sev_guest(kvm) || sev_snp_guest(kvm)) return; =20 - wbinvd_on_all_cpus(); + sev_writeback_caches(); } =20 void sev_free_vcpu(struct kvm_vcpu *vcpu) @@ -3858,7 +3859,7 @@ static int __sev_snp_update_protected_guest_state(str= uct kvm_vcpu *vcpu) * guest-mapped page rather than the initial one allocated * by KVM in svm->sev_es.vmsa. In theory, svm->sev_es.vmsa * could be free'd and cleaned up here, but that involves - * cleanups like wbinvd_on_all_cpus() which would ideally + * cleanups like sev_writeback_caches() which would ideally * be handled during teardown rather than guest boot. * Deferring that also allows the existing logic for SEV-ES * VMSAs to be re-used with minimal SNP-specific changes. @@ -4910,7 +4911,7 @@ void sev_gmem_invalidate(kvm_pfn_t start, kvm_pfn_t e= nd) =20 /* * SEV-ES avoids host/guest cache coherency issues through - * WBINVD hooks issued via MMU notifiers during run-time, and + * WBNOINVD hooks issued via MMU notifiers during run-time, and * KVM's VM destroy path at shutdown. Those MMU notifier events * don't cover gmem since there is no requirement to map pages * to a HVA in order to use them for a running guest. While the --=20 2.48.1.262.g85cc9f2d1e-goog