Received: by 2002:ab2:788f:0:b0:1ee:8f2e:70ae with SMTP id b15csp290379lqi; Wed, 6 Mar 2024 18:16:56 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUNmxCuqpOImxtlmdti1M2TtbDf9N6z2S9OgxUjItDMxPKH5xugCvtzwjKRRyqI1mQZbd0jb1haYtQiSpN+7cn0bKiyZ6synWKQyGhmGA== X-Google-Smtp-Source: AGHT+IEl2MEX5XIaMRS9aATOH9lGeJLQ+8D6rxIyxzsUEtMBHHksl9q6kRtB26UorA9rYDugo0E5 X-Received: by 2002:a05:6808:48b:b0:3c1:e832:172f with SMTP id z11-20020a056808048b00b003c1e832172fmr6336612oid.10.1709777816700; Wed, 06 Mar 2024 18:16:56 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709777816; cv=pass; d=google.com; s=arc-20160816; b=h4tJTgbJcbaGq0LtndHVUpqxSFUOV6OFYSCC0kxXxHBF+2TNuhXPtjQUgOfdsU87F9 0vL9LJV8wl9Hwrbh1bt2qmqlIQJ3TD7FUTIJJpWqNANz97kzx0CDmhBaJMv76ML4PJgn 6auxVQETc2MIgSZVxWkeBufTR5Eb9JCGnwHZFGpta9QRbVvGauACBcOrD1gOasI3chmF j5PiJsCHSXVwXrj1UBkmCLr6Z2odqFxmL5IKEgVQQek197FnsUK887I8AhI85shkuJ9p DA7WlEHJ2Mghuvu+JWLXS2GymCyaBSEXgMyLuWvGvgmfncR9TgWZ3hj9I/Xpniv2WTe/ I/NA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=thread-topic:thread-index:content-transfer-encoding:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:subject :message-id:cc:to:from:date; bh=X1n6oBXM6U5FHjOlfF+JD5pRtMNe+q61Kh9AjU0sXxU=; fh=TeRDwIQZ455wtEStv1qYaUWzsEDVAT6wWJrd31lGfWI=; b=J7TuVJSZa0VVhpimjIo4RUNBfRG2ltyd72Ap9aSKQDpfZTgV6MwMozasP+MSdCEcJp ZaTOolruPFMGZhUyLkiyN1TAhVYfbkNbK1iMuCd8voQKkr4pYiISxEvV0H4MT/da/bTN 3iKgrTypKHV9hO4aR+bPzHnv9UZ1hfP/oKUYAzcc5lORQ2KsmweOw3a+XE5IeUdQ1PpH ClcGkXvhdCe7lt2QZUFVNsb3QQAIE+06fg96/O/VV/CpEhl6KuNdRqQd756rRNqxyIYW fO4DxJ/SsFFMmu/18hrCzNGdMEl5j7u0KmH34EvC2vvUIx4HOMz3RnUl2TiRv9n4Bqs2 zumw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=sjtu.edu.cn); spf=pass (google.com: domain of linux-kernel+bounces-94857-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-94857-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id s9-20020a635249000000b005dbf1fcc429si12837127pgl.162.2024.03.06.18.16.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 18:16:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-94857-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=sjtu.edu.cn); spf=pass (google.com: domain of linux-kernel+bounces-94857-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-94857-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 11A62B20FFF for ; Thu, 7 Mar 2024 02:14:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BD4F463A9; Thu, 7 Mar 2024 02:14:40 +0000 (UTC) Received: from smtp238.sjtu.edu.cn (smtp238.sjtu.edu.cn [202.120.2.238]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F0DD2EAD5; Thu, 7 Mar 2024 02:14:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=202.120.2.238 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709777680; cv=none; b=gwz0iyN0tRo6LuclYx3Y+N9xMZl92MqwWv1qJ//87TBqRiDtXvPmGXAOsOdURUillxsIkJkZeNG9vWZvLiXxDWSzSB2/KGa+CY5jYMGmxf1OTRp+nrfC4VRDYffbXsoJ0ybC0qeW5Hz5dJKcmBJQA4fIEAyHzlk+bNn8IAoTszE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709777680; c=relaxed/simple; bh=zgC/d6cds9XJiQqU/dEwldAPOIfK/79YXy+0W9vHRtE=; h=Date:From:To:Cc:Message-ID:Subject:MIME-Version:Content-Type; b=Dop6LWPTE5XQpfHjWwYkmCkZCuDHKN+8w42hxhWa3pluvOpqQr3dR9qWrowcY9pG400sYRgdiNEMMsPLOzXM6i5tuki2Q0X5wtubN2DWjy7UiEU0AO8Zg+ZufKR3Qzw1L/Vfjx8/C1Spdm0Ol6fQZu+aNTyuTzHUisErJ92QCjU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=sjtu.edu.cn; spf=pass smtp.mailfrom=sjtu.edu.cn; arc=none smtp.client-ip=202.120.2.238 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=sjtu.edu.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sjtu.edu.cn Received: from mta91.sjtu.edu.cn (unknown [10.118.0.91]) by smtp238.sjtu.edu.cn (Postfix) with ESMTPS id C5445274D; Thu, 7 Mar 2024 10:14:27 +0800 (CST) Received: from mstore135.sjtu.edu.cn (unknown [10.118.0.135]) by mta91.sjtu.edu.cn (Postfix) with ESMTP id 7F0C337C8F4; Thu, 7 Mar 2024 10:14:27 +0800 (CST) Date: Thu, 7 Mar 2024 10:14:26 +0800 (CST) From: Zheyun Shen To: Sean Christopherson , thomas lendacky , pbonzini , tglx Cc: kvm , linux-kernel Message-ID: <980607740.5109383.1709777666996.JavaMail.zimbra@sjtu.edu.cn> Subject: [PATCH v2] KVM:SVM: Flush cache only on CPUs running SEV guest Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=GB2312 Content-Transfer-Encoding: 7bit X-Mailer: Zimbra 10.0.6_GA_4585 (ZimbraWebClient - GC122 (Win)/10.0.6_GA_4585) Thread-Index: E3LcEnfndLKXNi/4RoxZJ7buiDAZDw== Thread-Topic: Flush cache only on CPUs running SEV guest On AMD CPUs without ensuring cache consistency, each memory page reclamation in an SEV guest triggers a call to wbinvd_on_all_cpus(), thereby affecting the performance of other programs on the host. Typically, an AMD server may have 128 cores or more, while the SEV guest might only utilize 8 of these cores. Meanwhile, host can use qemu-affinity to bind these 8 vCPUs to specific physical CPUs. Therefore, keeping a record of the physical core numbers each time a vCPU runs can help avoid flushing the cache for all CPUs every time. Since the usage of sev_flush_asids() isn't tied to a single VM, we just replace all wbinvd_on_all_cpus() with sev_do_wbinvd() except for that in sev_flush_asids(). Signed-off-by: Zheyun Shen --- arch/x86/kvm/svm/sev.c | 27 +++++++++++++++++++++++---- arch/x86/kvm/svm/svm.h | 3 +++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index f760106c3..743931e33 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -215,6 +215,24 @@ static void sev_asid_free(struct kvm_sev_info *sev) sev->misc_cg = NULL; } +static struct cpumask *sev_get_wbinvd_dirty_mask(struct kvm *kvm) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + + return &sev->wbinvd_dirty_mask; +} + +static void sev_do_wbinvd(struct kvm *kvm) +{ + int cpu; + struct cpumask *dirty_mask = sev_get_wbinvd_dirty_mask(kvm); + + for_each_possible_cpu(cpu) { + if (cpumask_test_and_clear_cpu(cpu, dirty_mask)) + wbinvd_on_cpu(cpu); + } +} + static void sev_decommission(unsigned int handle) { struct sev_data_decommission decommission; @@ -2048,7 +2066,7 @@ int sev_mem_enc_unregister_region(struct kvm *kvm, * releasing the pages back to the system for use. CLFLUSH will * not do this, so issue a WBINVD. */ - wbinvd_on_all_cpus(); + sev_do_wbinvd(kvm); __unregister_enc_region_locked(kvm, region); @@ -2152,7 +2170,7 @@ void sev_vm_destroy(struct kvm *kvm) * releasing the pages back to the system for use. CLFLUSH will * not do this, so issue a WBINVD. */ - wbinvd_on_all_cpus(); + sev_do_wbinvd(kvm); /* * if userspace was terminated before unregistering the memory regions @@ -2343,7 +2361,7 @@ static void sev_flush_encrypted_page(struct kvm_vcpu *vcpu, void *va) return; do_wbinvd: - wbinvd_on_all_cpus(); + sev_do_wbinvd(vcpu->kvm); } void sev_guest_memory_reclaimed(struct kvm *kvm) @@ -2351,7 +2369,7 @@ void sev_guest_memory_reclaimed(struct kvm *kvm) if (!sev_guest(kvm)) return; - wbinvd_on_all_cpus(); + sev_do_wbinvd(kvm); } void sev_free_vcpu(struct kvm_vcpu *vcpu) @@ -2648,6 +2666,7 @@ void pre_sev_run(struct vcpu_svm *svm, int cpu) sd->sev_vmcbs[asid] = svm->vmcb; svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID; vmcb_mark_dirty(svm->vmcb, VMCB_ASID); + cpumask_set_cpu(get_cpu(), sev_get_wbinvd_dirty_mask(svm->vcpu.kvm)); } #define GHCB_SCRATCH_AREA_LIMIT (16ULL * PAGE_SIZE) diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 8ef95139c..de240a9e9 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -90,6 +90,9 @@ struct kvm_sev_info { struct list_head mirror_entry; /* Use as a list entry of mirrors */ struct misc_cg *misc_cg; /* For misc cgroup accounting */ atomic_t migration_in_progress; + + /* CPUs invoked VMRUN should do wbinvd after guest memory is reclaimed */ + struct cpumask wbinvd_dirty_mask; }; struct kvm_svm { -- 2.34.1