Message ID | 20240227232100.478238-20-pbonzini@redhat.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-84195-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp3036816dyb; Tue, 27 Feb 2024 16:00:12 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCW6tGN/rmKEAkXN7V1KGjW/OuNtCrmIzjIomsdsuolY46sCYLm1IouIl9Ot2FBRojArDWQ3bC/UEeBBCXBR7QQKH3fjxg== X-Google-Smtp-Source: AGHT+IHzG85lhdHEEFsIKwXdJoE524OfAiL0rLmXI09kloN9INBwt9eDD6cXGRnCVEvIdKysnDq4 X-Received: by 2002:a17:90a:eb12:b0:29a:c6fa:4223 with SMTP id j18-20020a17090aeb1200b0029ac6fa4223mr6514775pjz.43.1709078412052; Tue, 27 Feb 2024 16:00:12 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709078412; cv=pass; d=google.com; s=arc-20160816; b=ORlUfgABkulVlzotkAMpJTCr4E0xQNC2pP3miR5KZ4U6f4iZclDKCew9gOKiJqGKuw cWHH03UG1JxxF7U8QXE/FYx60+ZYcyeAFrdkdfIjccXply7ckps10t6JGPpjUwCmv0xj OksJ5rCwwF+2tezpsc3xqSUm9lOVFhjikge3dLpf6jMjfhCPdFX+riaZHdikgh0rBE2G 1nX0Fi1W72xaRk2+Hemrui5nfr3s6Vxyy329PVbvfTQjFJ4k9dvEWasbHWdzD6+I7IY4 81+3hUexzXLW1dzotFyPkY4b3KECByyCILmYLzmHh/jP+Nh85agsAF2faD9x9t4SCzwr p+Hw== 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=Gp15/1o40tx19f6g5lxcL1ZqsKDK8j34/b7p8btRGpA=; fh=miuXyzfw5j8qsOURKCmYrSiXirUlKyDkR37pPJ+5+/s=; b=EhJLoGHt4wYAfre64Npm6Zy3Vtc/2OsBjjnMA9rJiCr6iNj3VJr2uIBoCloucQb394 CVpSPhgLiUQm7sSoI4jSjsaMeB7TA7vJyxrc5x83xuJZvd3FIb+8NnXYRRPjlxfpy14b cNulI2EPkRHdnaylTiwxWq6oCeo3jIkw2Wy51Y82Z4qiyDfFitKW59pZYECVvy6yzpRR GNcRoVO55WjxqxWtg1f+jPi02WfSQQ2ATKq181RWQ4OBbx/HMWq4lNz2b6vwja5/Vb+k Ihs671GNHWAcUP+lpxazr4tLnQB1bQROv4d9EBTKgsK61+21i9AkU4yR0bvDJg2UdMSO FP0Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=E5e8faDK; 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-84195-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84195-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id om18-20020a17090b3a9200b00298aaff9c2esi223520pjb.47.2024.02.27.16.00.11 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 16:00:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-84195-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=E5e8faDK; 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-84195-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84195-ouuuleilei=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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 69201B2F71B for <ouuuleilei@gmail.com>; Tue, 27 Feb 2024 23:26:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DB3455D8E9; Tue, 27 Feb 2024 23:21:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="E5e8faDK" 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 F2B0157300 for <linux-kernel@vger.kernel.org>; Tue, 27 Feb 2024 23:21:07 +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=1709076070; cv=none; b=fhi+D3ZswuYt0rrjFYdVPlVZPcxvLwQwTiOuE9MPmjPu0uPA3ITzKLrQ9dyVO9e5qJia6q9RUPtpEQu6iT33imRV6ZCFKQoBsZf82SrrGzx3laY+MpEEndU7BR3JGl6fkAXypAffoKcHwbNuJqkyubbe3rmzFuEy5T+G37AaIno= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709076070; c=relaxed/simple; bh=p/JP/QGMe6e0T/PI6DjPUbUZdUsMnpQL0OaG3VJunZI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=P70kDmnQ6it3vOS9AGUBM0w7/NwTSsoeUgpFba5nStkxhMfaqgMDfVeRIADkp4CzwZfC6ctuqwFCORaoZAlsjOhVg2/QzwOXHjfHO2CaTJfe+CaM/+opjOz9rQDUlSVfw+411/7n0rVYnqGlAKS6lNgkkeFsY/F/RbDctK65GDs= 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=E5e8faDK; 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709076067; 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=Gp15/1o40tx19f6g5lxcL1ZqsKDK8j34/b7p8btRGpA=; b=E5e8faDKdX95wV+JWi05G4Iq6kCNVkEOJaXcs86uREeOME0fMp1bRKrOGQT/RbiRxYiooc ZwjSpRpgWBIKzjoMdNso3zo3yOmYJEnn08o+GtqPMv9DyCqEHHIPZfFfGwCFDIk6DxjJPJ EfgOhFcVJ3K5t6SfB4gJyioDlwaihFQ= 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-680-exvMBCBoPyGkfuGQOqaNvQ-1; Tue, 27 Feb 2024 18:21:05 -0500 X-MC-Unique: exvMBCBoPyGkfuGQOqaNvQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (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 41F3584AE40; 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 1512BC1596E; Tue, 27 Feb 2024 23:21:05 +0000 (UTC) From: Paolo Bonzini <pbonzini@redhat.com> 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 19/21] KVM: guest_memfd: add API to undo kvm_gmem_get_uninit_pfn Date: Tue, 27 Feb 2024 18:20:58 -0500 Message-Id: <20240227232100.478238-20-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: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792098605025742500 X-GMAIL-MSGID: 1792098605025742500 |
Series |
TDX/SNP part 1 of n, for 6.9
|
|
Commit Message
Paolo Bonzini
Feb. 27, 2024, 11:20 p.m. UTC
In order to be able to redo kvm_gmem_get_uninit_pfn, a hole must be punched
into the filemap, thus allowing FGP_CREAT_ONLY to succeed again. This will
be used whenever an operation that follows kvm_gmem_get_uninit_pfn fails.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
include/linux/kvm_host.h | 7 +++++++
virt/kvm/guest_memfd.c | 28 ++++++++++++++++++++++++++++
2 files changed, 35 insertions(+)
Comments
On Tue, Feb 27, 2024 at 06:20:58PM -0500, Paolo Bonzini wrote: > In order to be able to redo kvm_gmem_get_uninit_pfn, a hole must be punched > into the filemap, thus allowing FGP_CREAT_ONLY to succeed again. This will > be used whenever an operation that follows kvm_gmem_get_uninit_pfn fails. > > Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> > --- > include/linux/kvm_host.h | 7 +++++++ > virt/kvm/guest_memfd.c | 28 ++++++++++++++++++++++++++++ > 2 files changed, 35 insertions(+) > > diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h > index 03bf616b7308..192c58116220 100644 > --- a/include/linux/kvm_host.h > +++ b/include/linux/kvm_host.h > @@ -2436,6 +2436,8 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, > gfn_t gfn, kvm_pfn_t *pfn, int *max_order); > int kvm_gmem_get_uninit_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, > gfn_t gfn, kvm_pfn_t *pfn, int *max_order); > +int kvm_gmem_undo_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, > + gfn_t gfn, int order); > #else > static inline int kvm_gmem_get_pfn(struct kvm *kvm, > struct kvm_memory_slot *slot, gfn_t gfn, > @@ -2452,6 +2454,11 @@ static inline int kvm_gmem_get_uninit_pfn(struct kvm *kvm, > KVM_BUG_ON(1, kvm); > return -EIO; > } > + > +static inline int kvm_gmem_undo_get_pfn(struct kvm *kvm, > + struct kvm_memory_slot *slot, gfn_t gfn, > + int order) > +{} return -EIO; or compiler would complain that no return value. > #endif /* CONFIG_KVM_PRIVATE_MEM */ > > #ifdef CONFIG_HAVE_KVM_GMEM_PREPARE > diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c > index 7ec7afafc960..535ef1aa34fb 100644 > --- a/virt/kvm/guest_memfd.c > +++ b/virt/kvm/guest_memfd.c > @@ -590,3 +590,31 @@ int kvm_gmem_get_uninit_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, > return __kvm_gmem_get_pfn(kvm, slot, gfn, pfn, max_order, false); > } > EXPORT_SYMBOL_GPL(kvm_gmem_get_uninit_pfn); > + > +int kvm_gmem_undo_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, > + gfn_t gfn, int order) Didn't see the caller yet, but do we need to ensure the gfn is aligned with page order? e.g. WARN_ON(gfn & ((1UL << order) - 1)); > +{ > + pgoff_t index = gfn - slot->base_gfn + slot->gmem.pgoff; > + struct kvm_gmem *gmem; > + struct file *file; > + int r; > + > + file = kvm_gmem_get_file(slot); > + if (!file) > + return -EFAULT; > + > + gmem = file->private_data; > + > + if (WARN_ON_ONCE(xa_load(&gmem->bindings, index) != slot)) { > + r = -EIO; > + goto out_fput; > + } > + > + r = kvm_gmem_punch_hole(file_inode(file), index << PAGE_SHIFT, PAGE_SHIFT << order); ^ PAGE_SIZE << order Thanks, Yilun > + > +out_fput: > + fput(file); > + > + return r; > +} > +EXPORT_SYMBOL_GPL(kvm_gmem_undo_get_pfn); > -- > 2.39.0 > > >
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 03bf616b7308..192c58116220 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2436,6 +2436,8 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn, kvm_pfn_t *pfn, int *max_order); int kvm_gmem_get_uninit_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn, kvm_pfn_t *pfn, int *max_order); +int kvm_gmem_undo_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, + gfn_t gfn, int order); #else static inline int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn, @@ -2452,6 +2454,11 @@ static inline int kvm_gmem_get_uninit_pfn(struct kvm *kvm, KVM_BUG_ON(1, kvm); return -EIO; } + +static inline int kvm_gmem_undo_get_pfn(struct kvm *kvm, + struct kvm_memory_slot *slot, gfn_t gfn, + int order) +{} #endif /* CONFIG_KVM_PRIVATE_MEM */ #ifdef CONFIG_HAVE_KVM_GMEM_PREPARE diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 7ec7afafc960..535ef1aa34fb 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -590,3 +590,31 @@ int kvm_gmem_get_uninit_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, return __kvm_gmem_get_pfn(kvm, slot, gfn, pfn, max_order, false); } EXPORT_SYMBOL_GPL(kvm_gmem_get_uninit_pfn); + +int kvm_gmem_undo_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, + gfn_t gfn, int order) +{ + pgoff_t index = gfn - slot->base_gfn + slot->gmem.pgoff; + struct kvm_gmem *gmem; + struct file *file; + int r; + + file = kvm_gmem_get_file(slot); + if (!file) + return -EFAULT; + + gmem = file->private_data; + + if (WARN_ON_ONCE(xa_load(&gmem->bindings, index) != slot)) { + r = -EIO; + goto out_fput; + } + + r = kvm_gmem_punch_hole(file_inode(file), index << PAGE_SHIFT, PAGE_SHIFT << order); + +out_fput: + fput(file); + + return r; +} +EXPORT_SYMBOL_GPL(kvm_gmem_undo_get_pfn);