From nobody Thu Sep 19 01:03:54 2024 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 157ED149E17 for ; Fri, 26 Jul 2024 18:52:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722019933; cv=none; b=eP/rUedUyIvC04WoxCz+DZ/P9xJfc2sPU9Dan0nz+7QJ20IUq0NGk5wB2rUCPbTbxWTwSNRLN+R95+icOXZ3+1ftiCZjtYohhASypatcRBkU/VmgSQqVNeNp1xV5CgNJ8CkBL03BqRzvWV9N3ir04icv87Y9daj6r5aCZ4NKCJM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722019933; c=relaxed/simple; bh=XSdNQoHsGUkDa3pf0xSuEozquSEJkf0oa8FSNs1tQMc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=O58LizFW7dOV41i0rNRBXWA/r6b0dFE9CaCNfKFu6+rpUHBCAhrZLPmgPMw722ZUO9KkjBradQodzTakVl7ja5XJhCoLfr0Avjpw9nXuAWIp30vIfJHaQe8KD/ltGMaSUDbOCtdMsxD64Ro6JIqbdmYuBfTFZ77nVm6zbSJa09A= 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=a/EwnLUy; arc=none smtp.client-ip=170.10.133.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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="a/EwnLUy" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722019931; 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=TGMAXDTsfgqY90JMKJHbO1dD70msUk6Bwq2HA67kBP4=; b=a/EwnLUy2MJnvwIVi2OF4JMcNHPnWDQhK6HhqE2v3boKc7BYK3QWOlt7hoGzx/KnxtYNJX eWK/cj8+uFQk14Ixc6LFuE+cj7hULE6p2fKv5N58ryZGj2MhPzlAxtQbgFxHDic9NWDFWB piWz8ms341Uhs+l3AnczBjfZntnYVpc= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-196-hx5-ql0RN12ZrOGqrR38WA-1; Fri, 26 Jul 2024 14:52:06 -0400 X-MC-Unique: hx5-ql0RN12ZrOGqrR38WA-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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 mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A965C1955D57; Fri, 26 Jul 2024 18:52:05 +0000 (UTC) Received: from virtlab1023.lab.eng.rdu2.redhat.com (virtlab1023.lab.eng.rdu2.redhat.com [10.8.1.187]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id D445F3000194; Fri, 26 Jul 2024 18:52:04 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com, michael.roth@amd.com Subject: [PATCH v2 08/14] KVM: guest_memfd: make kvm_gmem_prepare_folio() operate on a single struct kvm Date: Fri, 26 Jul 2024 14:51:51 -0400 Message-ID: <20240726185157.72821-9-pbonzini@redhat.com> In-Reply-To: <20240726185157.72821-1-pbonzini@redhat.com> References: <20240726185157.72821-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-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" This is now possible because preparation is done by kvm_gmem_get_pfn() instead of fallocate(). In practice this is not a limitation, because even though guest_memfd can be bound to multiple struct kvm, for hardware implementations of confidential computing only one guest (identified by an ASID on SEV-SNP, or an HKID on TDX) will be able to access it. In the case of intra-host migration (not implemented yet for SEV-SNP, but we can use SEV-ES as an idea of how it will work), the new struct kvm inherits the same ASID and preparation need not be repeated. Reviewed-by: Michael Roth Signed-off-by: Paolo Bonzini --- virt/kvm/guest_memfd.c | 47 ++++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 29 deletions(-) diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 5af278c7adba..444ded162154 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -25,37 +25,27 @@ static inline kvm_pfn_t folio_file_pfn(struct folio *fo= lio, pgoff_t index) return folio_pfn(folio) + (index & (folio_nr_pages(folio) - 1)); } =20 -static int __kvm_gmem_prepare_folio(struct inode *inode, pgoff_t index, st= ruct folio *folio) +static int __kvm_gmem_prepare_folio(struct kvm *kvm, struct kvm_memory_slo= t *slot, + pgoff_t index, struct folio *folio) { #ifdef CONFIG_HAVE_KVM_ARCH_GMEM_PREPARE - struct list_head *gmem_list =3D &inode->i_mapping->i_private_list; - struct kvm_gmem *gmem; + kvm_pfn_t pfn; + gfn_t gfn; + int rc; =20 - list_for_each_entry(gmem, gmem_list, entry) { - struct kvm_memory_slot *slot; - struct kvm *kvm =3D gmem->kvm; - kvm_pfn_t pfn; - gfn_t gfn; - int rc; + if (!kvm_arch_gmem_prepare_needed(kvm)) + return 0; =20 - if (!kvm_arch_gmem_prepare_needed(kvm)) - continue; - - slot =3D xa_load(&gmem->bindings, index); - if (!slot) - continue; - - pfn =3D folio_file_pfn(folio, index); - gfn =3D slot->base_gfn + index - slot->gmem.pgoff; - rc =3D kvm_arch_gmem_prepare(kvm, gfn, pfn, folio_order(folio)); - if (rc) { - pr_warn_ratelimited("gmem: Failed to prepare folio for GFN %llx PFN %ll= x error %d.\n", - gfn, pfn, rc); - return rc; - } + pfn =3D folio_file_pfn(folio, index); + gfn =3D slot->base_gfn + index - slot->gmem.pgoff; + rc =3D kvm_arch_gmem_prepare(kvm, gfn, pfn, folio_order(folio)); + if (rc) { + pr_warn_ratelimited("gmem: Failed to prepare folio for index %lx GFN %ll= x PFN %llx error %d.\n", + index, gfn, pfn, rc); + return rc; } - #endif + return 0; } =20 @@ -65,7 +55,7 @@ static int __kvm_gmem_prepare_folio(struct inode *inode, = pgoff_t index, struct f * On successful return the guest sees a zero page so as to avoid * leaking host data and the up-to-date flag is set. */ -static int kvm_gmem_prepare_folio(struct file *file, struct kvm_memory_slo= t *slot, +static int kvm_gmem_prepare_folio(struct kvm *kvm, struct kvm_memory_slot = *slot, gfn_t gfn, struct folio *folio) { unsigned long nr_pages, i; @@ -95,8 +85,7 @@ static int kvm_gmem_prepare_folio(struct file *file, stru= ct kvm_memory_slot *slo WARN_ON(!IS_ALIGNED(slot->gmem.pgoff, 1 << folio_order(folio))); index =3D gfn - slot->base_gfn + slot->gmem.pgoff; index =3D ALIGN_DOWN(index, 1 << folio_order(folio)); - - r =3D __kvm_gmem_prepare_folio(file_inode(file), index, folio); + r =3D __kvm_gmem_prepare_folio(kvm, slot, index, folio); if (!r) folio_mark_uptodate(folio); =20 @@ -619,7 +608,7 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory= _slot *slot, goto out; } =20 - r =3D kvm_gmem_prepare_folio(file, slot, gfn, folio); + r =3D kvm_gmem_prepare_folio(kvm, slot, gfn, folio); folio_unlock(folio); if (r < 0) folio_put(folio); --=20 2.43.0