From patchwork Fri Dec 13 16:48:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13907466 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 129FCE77180 for ; Fri, 13 Dec 2024 16:48:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D92046B00A7; Fri, 13 Dec 2024 11:48:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D13B46B00A8; Fri, 13 Dec 2024 11:48:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B1A116B00A9; Fri, 13 Dec 2024 11:48:33 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 9225A6B00A7 for ; Fri, 13 Dec 2024 11:48:33 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 4ADB11A10B8 for ; Fri, 13 Dec 2024 16:48:33 +0000 (UTC) X-FDA: 82890518760.18.2D72F67 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) by imf02.hostedemail.com (Postfix) with ESMTP id 3FF7D80014 for ; Fri, 13 Dec 2024 16:47:42 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=LunGMe6j; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf02.hostedemail.com: domain of 3XmVcZwUKCNsQ7887DLLDIB.9LJIFKRU-JJHS79H.LOD@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3XmVcZwUKCNsQ7887DLLDIB.9LJIFKRU-JJHS79H.LOD@flex--tabba.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734108487; a=rsa-sha256; cv=none; b=w1J2kZVBTpOhgbw+ig4LjjaLy2ah2cOmmLmnICx5c2W7InmopCwN78LGmr1JHlDEcYRl5o WHUcZdYD5Whzlh/gQ+TvHyUDFt6iLi5Y7Ff3qGPu4i09clRevzoCFnk7DoZn41gAuyzHXN 5lrywWhbTYfqrfD8dhOLUvSHwSC4rcQ= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=LunGMe6j; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf02.hostedemail.com: domain of 3XmVcZwUKCNsQ7887DLLDIB.9LJIFKRU-JJHS79H.LOD@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3XmVcZwUKCNsQ7887DLLDIB.9LJIFKRU-JJHS79H.LOD@flex--tabba.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734108487; h=from:from:sender: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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=W5zxHybLV3NWne95v7ysBYYUl2v+8+sVLD5dfkruHJg=; b=P+JLfavL37ERp/ShyfedOluxRQyJgnyWfOObvQ3UjwkX4WYUBFpeRh0kFHtnYbLFFr/ok8 QVL2SlpGdSpWsqGH3maa5O6I8LkWH9nLvW7Sb2hHnjWuK5VDAJn/jaPyOTWlH/2WSJeYa/ joUrPay6b+nFeb5Vl8wZTC1D0Rekeus= Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-4361c040ba8so4598235e9.1 for ; Fri, 13 Dec 2024 08:48:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734108510; x=1734713310; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=W5zxHybLV3NWne95v7ysBYYUl2v+8+sVLD5dfkruHJg=; b=LunGMe6j7xqv36SHpVqhUyzXKqPjjLTDjXPe4MaC0nbsYMtbo40ozJsVcGwQDsW9Ga Hd2rjd7OV2dZcUjDgffsHkSVKpRlbuuCH2JJsCKAJXf86Q+57qiCTkPzyMsCV4DpDbZd YpEvQralAhWxaoDqEmP6ZQcgNIqLyYe38k9ZhqaYqH7GPq7f9Y7OJ0wWCd4tmlG2P7Cp RV1HK68iVyXWsw9PklwdWSlsfD54/5dkLDl2VR1UwiJQ8djBoqdH5/stiH2vxgPmLsj7 EVQGoHag6ioEZ5/KHfKgFjLi1MBSTPlmk456xEo7GkZgdR9IA2djGKRDiVnT97ThkJnp Jq+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734108510; x=1734713310; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=W5zxHybLV3NWne95v7ysBYYUl2v+8+sVLD5dfkruHJg=; b=rLNLCeeuSVpSSQa+PxWI46kaqqTVC9uIAGlqpDg47vwfDV/4Md9neJZfruZQ0T6+1H EoqtVWFhuxEOXcyO1gTlGXqJiVNVfV7LtBhgndB39Z5toTS5kYV3+KzTbgV8Z9BILg1k o/5EHlHkjo54o0Om6x1AA0OVDIXNUmZPBigqHQs99a0KTeGXjPp8/+m2FUVFYJcVSWCD Harhjs7vXRBMGOnalIYP8jhEDq96YxyFaP1EjtT/GfjSd+M6iu0mwtxjERZio8Hd/l+g z07dKXOJNQj65Dh1xopl+28EeECJJqoP3SusPpBuDmvW698Kob/j2VteFPk0pMfh7isY R42A== X-Forwarded-Encrypted: i=1; AJvYcCXntKFFeYqFQoeqW9kGw+s4F6QTYwIpQEGs/Sy9MPsjNYIwKmf+/2FbbCQ0gktsSQ0aRd9xGTu4WA==@kvack.org X-Gm-Message-State: AOJu0Yz+uutEejQ9Wi59aSSIbHoGXJ547uWH8e39ALwLynHXkrj7w5kz wDMF+c0gXMC38lPUNOR/x70x88MImNPEy6H+I4LU1Gf9G6YNOcWNDM+1xui87Ef9M+HdeJCzfQ= = X-Google-Smtp-Source: AGHT+IE5qr7blgR8UT1xyGN9dbKV8Uu0Nxy8xzMzxQfp0eU0uQYPzB6TjeBHpyTiJ8UPA6pvno7rqFYahw== X-Received: from wmmu14.prod.google.com ([2002:a05:600c:ce:b0:434:f0a3:7876]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:4b12:b0:385:edb7:69ba with SMTP id ffacd0b85a97d-38880ac5f25mr3423932f8f.1.1734108510104; Fri, 13 Dec 2024 08:48:30 -0800 (PST) Date: Fri, 13 Dec 2024 16:48:04 +0000 In-Reply-To: <20241213164811.2006197-1-tabba@google.com> Mime-Version: 1.0 References: <20241213164811.2006197-1-tabba@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241213164811.2006197-9-tabba@google.com> Subject: [RFC PATCH v4 08/14] KVM: guest_memfd: Add guest_memfd support to kvm_(read|/write)_guest_page() From: Fuad Tabba To: kvm@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-mm@kvack.org Cc: pbonzini@redhat.com, chenhuacai@kernel.org, mpe@ellerman.id.au, anup@brainfault.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, seanjc@google.com, viro@zeniv.linux.org.uk, brauner@kernel.org, willy@infradead.org, akpm@linux-foundation.org, xiaoyao.li@intel.com, yilun.xu@intel.com, chao.p.peng@linux.intel.com, jarkko@kernel.org, amoorthy@google.com, dmatlack@google.com, yu.c.zhang@linux.intel.com, isaku.yamahata@intel.com, mic@digikod.net, vbabka@suse.cz, vannapurve@google.com, ackerleytng@google.com, mail@maciej.szmigiero.name, david@redhat.com, michael.roth@amd.com, wei.w.wang@intel.com, liam.merwick@oracle.com, isaku.yamahata@gmail.com, kirill.shutemov@linux.intel.com, suzuki.poulose@arm.com, steven.price@arm.com, quic_eberman@quicinc.com, quic_mnalajal@quicinc.com, quic_tsoni@quicinc.com, quic_svaddagi@quicinc.com, quic_cvanscha@quicinc.com, quic_pderrin@quicinc.com, quic_pheragu@quicinc.com, catalin.marinas@arm.com, james.morse@arm.com, yuzenghui@huawei.com, oliver.upton@linux.dev, maz@kernel.org, will@kernel.org, qperret@google.com, keirf@google.com, roypat@amazon.co.uk, shuah@kernel.org, hch@infradead.org, jgg@nvidia.com, rientjes@google.com, jhubbard@nvidia.com, fvdl@google.com, hughd@google.com, jthoughton@google.com, tabba@google.com X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 3FF7D80014 X-Stat-Signature: rhny4oa3bwme395e19fcqxf1wxag73ma X-Rspam-User: X-HE-Tag: 1734108462-211046 X-HE-Meta: U2FsdGVkX1/mgS883mlQuteVpCzOXb30DuwpSXoS/O/eLYJAbhM29zFrRVVfjTuieehm8N2PHYSEYX8JbUsF9WGmuL2nlTW1ltudj6wK8nkvKfPMk8qcNC8ZIINFzsTm7NTTqInymMRrkM9pqJz4h8TRtxixoDDbLHxuV2nwT0u632kEJxTMPJlmz1IhXIY4nOmMmZHzvikjWWE/TgsmQBtwq4Us1fnaoYDZcjFi02KSM2zamjv/0gHl8QaM98ahkAtVO+EB7tf9yTOGKvjzxG7LIKd7xvWHRDnjIEjQihB2tFL+LA7a99vlmvZFsiW9nRs76jMwe1J0GWseUkRiP6xYHjtNqUs4Dxk8jKKABO6vRpRllrjR7Le/XQgRNl5J90gCEEGwE5wuT/j43Ad1H5RnJO3WO7v0f9VnES9z3dfjHdtcZB/TsQ5A6UxOxxTpf5H2+OzGP+nNWDa0nAaC55h8pc6OWwi+8ZsphkzJSVcAQnmFyvbumNSMAwaut0fcaGCEYuhuOEquX1si9kQnPR/UO0ZBcBI2fhh67mQcB9xWnR/a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89uEd+3lkWQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000007, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Make kvm_(read|/write)_guest_page() capable of accessing guest memory for slots that don't have a userspace address, but only if the memory is mappable, which also indicates that it is accessible by the host. Signed-off-by: Fuad Tabba --- virt/kvm/kvm_main.c | 133 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 114 insertions(+), 19 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index fffff01cebe7..53692feb6213 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3184,23 +3184,110 @@ int kvm_gmem_clear_mappable(struct kvm *kvm, gfn_t start, gfn_t end) return r; } +static int __kvm_read_guest_memfd_page(struct kvm *kvm, + struct kvm_memory_slot *slot, + gfn_t gfn, void *data, int offset, + int len) +{ + struct page *page; + u64 pfn; + int r; + + /* + * Holds the folio lock until after checking whether it can be faulted + * in, to avoid races with paths that change a folio's mappability. + */ + r = kvm_gmem_get_pfn_locked(kvm, slot, gfn, &pfn, &page, NULL); + if (r) + return r; + + if (!kvm_gmem_is_mappable(kvm, gfn, gfn + 1)) { + r = -EPERM; + goto unlock; + } + memcpy(data, page_address(page) + offset, len); +unlock: + unlock_page(page); + if (r) + put_page(page); + else + kvm_release_page_clean(page); + + return r; +} + +static int __kvm_write_guest_memfd_page(struct kvm *kvm, + struct kvm_memory_slot *slot, + gfn_t gfn, const void *data, + int offset, int len) +{ + struct page *page; + u64 pfn; + int r; + + /* + * Holds the folio lock until after checking whether it can be faulted + * in, to avoid races with paths that change a folio's mappability. + */ + r = kvm_gmem_get_pfn_locked(kvm, slot, gfn, &pfn, &page, NULL); + if (r) + return r; + + if (!kvm_gmem_is_mappable(kvm, gfn, gfn + 1)) { + r = -EPERM; + goto unlock; + } + memcpy(page_address(page) + offset, data, len); +unlock: + unlock_page(page); + if (r) + put_page(page); + else + kvm_release_page_dirty(page); + + return r; +} +#else +static int __kvm_read_guest_memfd_page(struct kvm *kvm, + struct kvm_memory_slot *slot, + gfn_t gfn, void *data, int offset, + int len) +{ + WARN_ON_ONCE(1); + return -EIO; +} + +static int __kvm_write_guest_memfd_page(struct kvm *kvm, + struct kvm_memory_slot *slot, + gfn_t gfn, const void *data, + int offset, int len) +{ + WARN_ON_ONCE(1); + return -EIO; +} #endif /* CONFIG_KVM_GMEM_MAPPABLE */ /* Copy @len bytes from guest memory at '(@gfn * PAGE_SIZE) + @offset' to @data */ -static int __kvm_read_guest_page(struct kvm_memory_slot *slot, gfn_t gfn, - void *data, int offset, int len) + +static int __kvm_read_guest_page(struct kvm *kvm, struct kvm_memory_slot *slot, + gfn_t gfn, void *data, int offset, int len) { - int r; unsigned long addr; if (WARN_ON_ONCE(offset + len > PAGE_SIZE)) return -EFAULT; + if (IS_ENABLED(CONFIG_KVM_GMEM_MAPPABLE) && + kvm_slot_can_be_private(slot) && + !slot->userspace_addr) { + return __kvm_read_guest_memfd_page(kvm, slot, gfn, data, + offset, len); + } + addr = gfn_to_hva_memslot_prot(slot, gfn, NULL); if (kvm_is_error_hva(addr)) return -EFAULT; - r = __copy_from_user(data, (void __user *)addr + offset, len); - if (r) + if (__copy_from_user(data, (void __user *)addr + offset, len)) return -EFAULT; return 0; } @@ -3210,7 +3297,7 @@ int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, { struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn); - return __kvm_read_guest_page(slot, gfn, data, offset, len); + return __kvm_read_guest_page(kvm, slot, gfn, data, offset, len); } EXPORT_SYMBOL_GPL(kvm_read_guest_page); @@ -3219,7 +3306,7 @@ int kvm_vcpu_read_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, void *data, { struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); - return __kvm_read_guest_page(slot, gfn, data, offset, len); + return __kvm_read_guest_page(vcpu->kvm, slot, gfn, data, offset, len); } EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest_page); @@ -3296,22 +3383,30 @@ EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest_atomic); /* Copy @len bytes from @data into guest memory at '(@gfn * PAGE_SIZE) + @offset' */ static int __kvm_write_guest_page(struct kvm *kvm, - struct kvm_memory_slot *memslot, gfn_t gfn, - const void *data, int offset, int len) + struct kvm_memory_slot *slot, gfn_t gfn, + const void *data, int offset, int len) { - int r; - unsigned long addr; - if (WARN_ON_ONCE(offset + len > PAGE_SIZE)) return -EFAULT; - addr = gfn_to_hva_memslot(memslot, gfn); - if (kvm_is_error_hva(addr)) - return -EFAULT; - r = __copy_to_user((void __user *)addr + offset, data, len); - if (r) - return -EFAULT; - mark_page_dirty_in_slot(kvm, memslot, gfn); + if (IS_ENABLED(CONFIG_KVM_GMEM_MAPPABLE) && + kvm_slot_can_be_private(slot) && + !slot->userspace_addr) { + int r = __kvm_write_guest_memfd_page(kvm, slot, gfn, data, + offset, len); + + if (r) + return r; + } else { + unsigned long addr = gfn_to_hva_memslot(slot, gfn); + + if (kvm_is_error_hva(addr)) + return -EFAULT; + if (__copy_to_user((void __user *)addr + offset, data, len)) + return -EFAULT; + } + + mark_page_dirty_in_slot(kvm, slot, gfn); return 0; }