Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp2230788rbb; Tue, 27 Feb 2024 15:27:37 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCV9WDIidnje7mdn7D44OS2wgc7t/SGRaCXSxtA4D5fO1PXOkSFTFMt7Td7uD1XmkfEjGDBoXd/VGTodSvxhvW6QP37u+BJ+pRlOb9KGXg== X-Google-Smtp-Source: AGHT+IGJfrU9z63kyHQ1xj5V7xQKBSf+79pCTVGUDd7Xa6/L20Oc3EuzRw5DH0l2IPbPSggUfhfn X-Received: by 2002:a05:6512:69:b0:512:b517:982 with SMTP id i9-20020a056512006900b00512b5170982mr6170035lfo.20.1709076456866; Tue, 27 Feb 2024 15:27:36 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709076456; cv=pass; d=google.com; s=arc-20160816; b=v2Hoe+I0OV7hQBxhiR6hZxIPgMW6yni4jBDAM4A/ZPCCpYavE6TdDyJqQOWpFoKiHO IbktfLmvQNBtUdQHILfko29ns/9IZ0ShXfRxIgrBi14NiJX7XniXqvjCikHY7JCHZYx6 x3SgOamHiSiZiwya6W81GvUlNKXVl5A6PBi/xm4sDn105z3f8Pzd7UijkIgCP8ZQ4FO0 eg8Zx8R9MYJPUpsM44BNAuKocYSB33Ym8g+n2DXHBZlGp6h0fzhuZ9SOVS0015AyzUA+ GuBwcd9BnjMmE8n4bvhP3+MOU9hKKJW3yzcqBWcVq7IhhaBNuRzg+YA/I07UjegsYeSA s+Tw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=T8A/JfH64nTOH0V0ceFUqpMW1k22o4T4wPuWKt4c/d4=; fh=miuXyzfw5j8qsOURKCmYrSiXirUlKyDkR37pPJ+5+/s=; b=apC6Hje10ukJYj6AFAf40zz0z8KMBewjjjJVRsNfuSxqm5KzxlEOMUnuMxn0WwiBl5 TJh8oIDM019rdR4vjgooT+K5sL3tC9UBEiMJyBEWHCTig/1yZnDF2C2WpBiaTg5TwXKs o5H0UjnQH3UdA6NTszy3Hdg5nwZvsI5xvfCcpUeWiYOnQisMw+bCxxxFYP2/nH+cTyPX s7zy9JV2ExZLME5qqkxko5XNqqazt8lDMuZEC6HtkARDw9umFUhmjJmfnLGl/0Jki4yR CUx4Z6gDY7obDGPNrpQVVEbF0IItAPnY0wmJtNPTWRDret4ufG9B1EeibQzruOnBdX2V 0gHA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=JBWM+ec2; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-84207-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84207-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id w5-20020a056402070500b005654d6a248asi1118894edx.85.2024.02.27.15.27.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 15:27:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-84207-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=JBWM+ec2; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-84207-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84207-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com 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 am.mirrors.kernel.org (Postfix) with ESMTPS id 6B9B31F2389C for ; Tue, 27 Feb 2024 23:27:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BE4385F462; Tue, 27 Feb 2024 23:21:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="JBWM+ec2" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 D0F7758136 for ; Tue, 27 Feb 2024 23:21:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709076074; cv=none; b=gNADp4A4dVg+Js3ivPxdHiSIuug3AD281ljkk2JwKU0OgPU8TYyQukBZZTsZkqnwbebd0dfQw2ABBzcPCLA/RygjIFqnL3hceskG/5aXDd2tZmmAVg5ZtoQv8HTrE4xoEuZKn8vxLlG7/wpJr9aCardI5coEnThWjZ/yjCNfS+o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709076074; c=relaxed/simple; bh=rJ+0CMX/DI45udGZgNZqTlQKuapoycVU2cowegxlNxo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=PmDOpFHsKw5fLd/b1ERPIQ0vMRGx707qEHUbmAUh4lTJZg04TyrCoFfDmqfOBkmk1FlnJIb1aMLTOp/XnNLb6C1LDe/maVejpwtESZtUYKIjpKSXnO/F9OTg6Yx6xDdLQqm3MpW7ETn/7Js2DbeuFdIL5iHK8h3hU+8pUzN9QsY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=JBWM+ec2; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709076069; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=T8A/JfH64nTOH0V0ceFUqpMW1k22o4T4wPuWKt4c/d4=; b=JBWM+ec2VaHe74a0ygL0kjaM2JpX0ZUXqRDFQar8OnBe4C+8F8Gi+yQjbbSXV4AziaR64o lbQAQIeAvRU65ClFrbTSrUveS6qPJXTj4XoHuWEeTMfzW4XpDpd22aBgFhGkdstc96kfzx 0YHsWg1TVgtBiatN9/VTn59/4yIy7po= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-393-2HVa7PztN72zcSYx2dj5sA-1; Tue, 27 Feb 2024 18:21:06 -0500 X-MC-Unique: 2HVa7PztN72zcSYx2dj5sA-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 905CB85A58B; Tue, 27 Feb 2024 23:21:05 +0000 (UTC) Received: from virtlab511.virt.lab.eng.bos.redhat.com (virtlab511.virt.lab.eng.bos.redhat.com [10.19.152.198]) by smtp.corp.redhat.com (Postfix) with ESMTP id 621FE492BCF; Tue, 27 Feb 2024 23:21:05 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com, michael.roth@amd.com, isaku.yamahata@intel.com, thomas.lendacky@amd.com Subject: [PATCH 20/21] KVM: x86: Add gmem hook for invalidating memory Date: Tue, 27 Feb 2024 18:20:59 -0500 Message-Id: <20240227232100.478238-21-pbonzini@redhat.com> In-Reply-To: <20240227232100.478238-1-pbonzini@redhat.com> References: <20240227232100.478238-1-pbonzini@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 From: Michael Roth In some cases, like with SEV-SNP, guest memory needs to be updated in a platform-specific manner before it can be safely freed back to the host. Wire up arch-defined hooks to the .free_folio kvm_gmem_aops callback to allow for special handling of this sort when freeing memory in response to FALLOC_FL_PUNCH_HOLE operations and when releasing the inode, and go ahead and define an arch-specific hook for x86 since it will be needed for handling memory used for SEV-SNP guests. Signed-off-by: Michael Roth Message-Id: <20231230172351.574091-6-michael.roth@amd.com> Signed-off-by: Paolo Bonzini --- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 7 +++++++ include/linux/kvm_host.h | 4 ++++ virt/kvm/Kconfig | 4 ++++ virt/kvm/guest_memfd.c | 14 ++++++++++++++ 6 files changed, 31 insertions(+) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index adfaad15e7e6..42474acb7375 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -140,6 +140,7 @@ KVM_X86_OP(vcpu_deliver_sipi_vector) KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons); KVM_X86_OP_OPTIONAL(get_untagged_addr) KVM_X86_OP_OPTIONAL_RET0(gmem_prepare) +KVM_X86_OP_OPTIONAL(gmem_invalidate) #undef KVM_X86_OP #undef KVM_X86_OP_OPTIONAL diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 6d873d08f739..e523b204697d 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1805,6 +1805,7 @@ struct kvm_x86_ops { gva_t (*get_untagged_addr)(struct kvm_vcpu *vcpu, gva_t gva, unsigned int flags); int (*gmem_prepare)(struct kvm *kvm, kvm_pfn_t pfn, gfn_t gfn, int max_order); + void (*gmem_invalidate)(kvm_pfn_t start, kvm_pfn_t end); }; struct kvm_x86_nested_ops { diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index eff532ea59c9..9d5603adf542 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13605,6 +13605,13 @@ int kvm_arch_gmem_prepare(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, int max_ord } #endif +#ifdef CONFIG_HAVE_KVM_GMEM_INVALIDATE +void kvm_arch_gmem_invalidate(kvm_pfn_t start, kvm_pfn_t end) +{ + static_call_cond(kvm_x86_gmem_invalidate)(start, end); +} +#endif + int kvm_spec_ctrl_test_value(u64 value) { /* diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 192c58116220..3835732491b9 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2465,4 +2465,8 @@ static inline int kvm_gmem_undo_get_pfn(struct kvm *kvm, int kvm_arch_gmem_prepare(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, int max_order); #endif +#ifdef CONFIG_HAVE_KVM_GMEM_INVALIDATE +void kvm_arch_gmem_invalidate(kvm_pfn_t start, kvm_pfn_t end); +#endif + #endif diff --git a/virt/kvm/Kconfig b/virt/kvm/Kconfig index dcce0c3b5b13..39356f5babbd 100644 --- a/virt/kvm/Kconfig +++ b/virt/kvm/Kconfig @@ -115,3 +115,7 @@ config KVM_GENERIC_PRIVATE_MEM config HAVE_KVM_GMEM_PREPARE bool depends on KVM_PRIVATE_MEM + +config HAVE_KVM_GMEM_INVALIDATE + bool + depends on KVM_PRIVATE_MEM diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 535ef1aa34fb..74e19170af8a 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -345,10 +345,24 @@ static int kvm_gmem_error_folio(struct address_space *mapping, struct folio *fol return MF_DELAYED; } +#ifdef CONFIG_HAVE_KVM_GMEM_INVALIDATE +static void kvm_gmem_free_folio(struct folio *folio) +{ + struct page *page = folio_page(folio, 0); + kvm_pfn_t pfn = page_to_pfn(page); + int order = folio_order(folio); + + kvm_arch_gmem_invalidate(pfn, pfn + (1ul << order)); +} +#endif + static const struct address_space_operations kvm_gmem_aops = { .dirty_folio = noop_dirty_folio, .migrate_folio = kvm_gmem_migrate_folio, .error_remove_folio = kvm_gmem_error_folio, +#ifdef CONFIG_HAVE_KVM_GMEM_INVALIDATE + .free_folio = kvm_gmem_free_folio, +#endif }; static int kvm_gmem_getattr(struct mnt_idmap *idmap, const struct path *path, -- 2.39.0