Received: by 2002:ab2:6991:0:b0:1f7:f6c3:9cb1 with SMTP id v17csp44252lqo; Tue, 7 May 2024 11:35:30 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUw8Wxm4SATp/4inuc0F1yUQS5Jd0AmwTtgFjLC9AtqdajpQu3nBIEkqCco3E5vBnRGgiD5IQNgEGWYSSXH8sE9EonIpK1CfXc9dsCD7w== X-Google-Smtp-Source: AGHT+IFbUCvD1U3I8YfqPrrD+WFsFCuxfgAhoWzcIzhO7jW74Gy2bTz8NM1iV/2KtbiZyGJ77/VI X-Received: by 2002:a05:6e02:1386:b0:36c:501e:d8da with SMTP id e9e14a558f8ab-36caecd4f4dmr3708065ab.5.1715106930518; Tue, 07 May 2024 11:35:30 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715106930; cv=pass; d=google.com; s=arc-20160816; b=OmwssXWjRkhK1Y+GVK1dtsEQ8bZF8LNw0OMCPLy1Z/Mi3LaXxWJaXaUoCCr37JOBL1 DcPW0KGlv5dKtcVk77flXZg2lNr9os16Mq9CcczpY215oB01DTxSpBDzxGrNHkHaWx8M 7IV5dAGIFax3TXRFWXEhazEfERS/sGfF6SY3FYl7Bwln60JgtYC115dZsyw3gj8m193v yT6Qhc9VAxn6xpGiu28jxToIP6H/M0yCM6fQvTc0HYtSOh2imdkmpiTFYdcxOMyOCpek 7CLiOj8tD4lljn5R2IDno2qbdWf7GPLBgCEqXxhco9AiYPKOa3HHHJ32Qh3f9w3UnwEq EQDA== 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=iJo566QBGqWzWWdhoS3/FTnwQOx8annMHtRX1QpbOmc=; fh=uwOp2UVTonYvy7JjkwYgWmEzMj+LYWBj3xu2PrpNAkc=; b=GgfrO1BJLgfu2NYLAHApa1bpd6TbQVYaDbGdQd2Ae/IPXH2t9MDmLxd6HMdKbGhsX3 SziHm4WxmWn7eiItggdo7asg6DVlHjQr5amPuO76/V6wpqm7CVC07c6j+oJviIOQVdH2 DJl4MUl8F6XS+AhvvTN0oGUUOOOwh+QwjEgNw13gr/2413IauF5JMR3xOah1LaqFhZi8 n2MO1y+cSTpxf7+CJw/rGLEZRpco+fTPTQk/8HLKHZgu7sGAM2MCO0uXZAupqE1gJcGe 3uQU5z6x5sm4FYPimQp1Uft+49Tjl/nl1Jb6Z3e+/iADHqO33imMVpP3UJIdMcYTt+Rx h48A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=GZeHomFu; 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-171962-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-171962-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id r2-20020a655082000000b0061b569abd3csi10257797pgp.364.2024.05.07.11.35.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 11:35:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-171962-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; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=GZeHomFu; 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-171962-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-171962-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 6132AB27BD3 for ; Tue, 7 May 2024 18:09:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E11C016F261; Tue, 7 May 2024 18:07:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="GZeHomFu" 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 56D9816D9B3 for ; Tue, 7 May 2024 18:07:38 +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=1715105260; cv=none; b=HNdCFhxRLSGaDJJwpd7T/RGhnkaFc8vms0lWRrY1X6JTmHd7zZc56mIOTmDQhwXiAjRkkkmLyMr54GYSXSIEy0t+U5nCS6zMsZXE/zr3evVJgHw5t2N2ePyBTZea5n96flT01nl8z/vg3avEBFAlgQBZkVYjmCejU4CL14peZto= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715105260; c=relaxed/simple; bh=jvqvn+8mocOHRHNhWwM9lYAjWgSVkEtH4f321YwzVPw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=nCMGv7S9V/aUC8z8ieqEM/z9P44wGm/bM0Yc8AUccSc84HgzJC7moXRdpDtrb4409eMilmHwswUC99kRuMiW21ewcOK4Rt6Fg51b+r7czexwqBEQQdabtiGdpI791sSUvBoo4b9eIRP6lSEUqo0DRdtp7sZeL2nFsy1iT18OkCw= 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=GZeHomFu; 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=1715105257; 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=iJo566QBGqWzWWdhoS3/FTnwQOx8annMHtRX1QpbOmc=; b=GZeHomFuCOLt/iWG+4RyoS779TEHtYRpNWhBC8TXnDaLdFbAr7WwZXlA+0MVPrLEf1wQbx 5sCqosAxuwvIhqpqL7elDPNMgk71Tn0EObe3B7pSiuKK/88K8A9hYAZYjdpdHYCIBp6mlN lUWsXFSEXeqZk4RokAu5lR/8nIzPvIY= 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-572-4HGfDG3FOxys7MugJCTPpQ-1; Tue, 07 May 2024 14:07:32 -0400 X-MC-Unique: 4HGfDG3FOxys7MugJCTPpQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (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 EE924101A54F; Tue, 7 May 2024 18:07:31 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id AE44B40C6EB7; Tue, 7 May 2024 18:07:31 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: vbabka@suse.cz, isaku.yamahata@intel.com, xiaoyao.li@intel.com, binbin.wu@linux.intel.com, seanjc@google.com, rick.p.edgecombe@intel.com, michael.roth@amd.com, yilun.xu@intel.com Subject: [PATCH 6/9] KVM: guest_memfd: extract __kvm_gmem_get_pfn() Date: Tue, 7 May 2024 14:07:26 -0400 Message-ID: <20240507180729.3975856-7-pbonzini@redhat.com> In-Reply-To: <20240507180729.3975856-1-pbonzini@redhat.com> References: <20240507180729.3975856-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.2 In preparation for adding a function that walks a set of pages provided by userspace and populates them in a guest_memfd, add a version of kvm_gmem_get_pfn() that has a "bool prepare" argument and passes it down to kvm_gmem_get_folio(). Populating guest memory has to call repeatedly __kvm_gmem_get_pfn() on the same file, so make the new function take struct file*. Signed-off-by: Paolo Bonzini --- virt/kvm/guest_memfd.c | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 0176089be731..bfe437098b79 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -528,33 +528,29 @@ void kvm_gmem_unbind(struct kvm_memory_slot *slot) fput(file); } -int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, - gfn_t gfn, kvm_pfn_t *pfn, int *max_order) +static int __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot, + gfn_t gfn, kvm_pfn_t *pfn, int *max_order, bool prepare) { pgoff_t index = gfn - slot->base_gfn + slot->gmem.pgoff; - struct kvm_gmem *gmem; + struct kvm_gmem *gmem = file->private_data; struct folio *folio; struct page *page; - struct file *file; int r; - file = kvm_gmem_get_file(slot); - if (!file) + if (file != slot->gmem.file) { + WARN_ON_ONCE(slot->gmem.file); return -EFAULT; + } gmem = file->private_data; - if (xa_load(&gmem->bindings, index) != slot) { WARN_ON_ONCE(xa_load(&gmem->bindings, index)); - r = -EIO; - goto out_fput; + return -EIO; } - folio = kvm_gmem_get_folio(file_inode(file), index, true); - if (IS_ERR(folio)) { - r = PTR_ERR(folio); - goto out_fput; - } + folio = kvm_gmem_get_folio(file_inode(file), index, prepare); + if (IS_ERR(folio)) + return PTR_ERR(folio); if (folio_test_hwpoison(folio)) { r = -EHWPOISON; @@ -571,9 +567,21 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, out_unlock: folio_unlock(folio); -out_fput: - fput(file); return r; } + +int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, + gfn_t gfn, kvm_pfn_t *pfn, int *max_order) +{ + struct file *file = kvm_gmem_get_file(slot); + int r; + + if (!file) + return -EFAULT; + + r = __kvm_gmem_get_pfn(file, slot, gfn, pfn, max_order, true); + fput(file); + return r; +} EXPORT_SYMBOL_GPL(kvm_gmem_get_pfn); -- 2.43.0