From patchwork Wed Jan 22 15:27:31 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13947430 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 0C567C02182 for ; Wed, 22 Jan 2025 15:27:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 958806B008C; Wed, 22 Jan 2025 10:27:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 86AB66B0092; Wed, 22 Jan 2025 10:27:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 70CD5280001; Wed, 22 Jan 2025 10:27:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 4F6996B008C for ; Wed, 22 Jan 2025 10:27:48 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 03C1A1A0395 for ; Wed, 22 Jan 2025 15:27:47 +0000 (UTC) X-FDA: 83035467816.07.9D13A38 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) by imf01.hostedemail.com (Postfix) with ESMTP id 210D340008 for ; Wed, 22 Jan 2025 15:27:45 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="JIo24/yH"; spf=pass (imf01.hostedemail.com: domain of 3cA6RZwUKCBUEvwwv19916z.x97638FI-775Gvx5.9C1@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3cA6RZwUKCBUEvwwv19916z.x97638FI-775Gvx5.9C1@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737559666; 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=Jgc9nfq2iOlwc6s0mq0Zmi5Z5E3sXJP1ktmubqSKFNg=; b=tuOOyBgqM3RmtS5w0u2STJREFqpCNtoc3sBP261sVacQCfgEwEgcwY5KqaOIWszgpNm2xV a5QjqaeCgwLvNSj2KbN/QTxFJu8bwkcDflTI2+o5tugCz2gDpasO3eUenvTdkjKJai5GO9 d6DCFM6SaLt4hwwhQUGD1a/ejpuAv2Q= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="JIo24/yH"; spf=pass (imf01.hostedemail.com: domain of 3cA6RZwUKCBUEvwwv19916z.x97638FI-775Gvx5.9C1@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3cA6RZwUKCBUEvwwv19916z.x97638FI-775Gvx5.9C1@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737559666; a=rsa-sha256; cv=none; b=k6yuf4huZGEdScpkIWUUO3lKmeTAwwR2XVOTsxOd297fxu78b1mk057Hi4sMYOkRXPUboM a7iA5PsYjBogKi49+0aj2h7pps6UDXv2yZMJ1XgJGrDsdRgsXXg/vTDwoOaYGltbSzVnjY rY2w/C4PsHPErkTIlwIU9sB4L+yeU7A= Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-43628594d34so38939905e9.2 for ; Wed, 22 Jan 2025 07:27:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737559665; x=1738164465; 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=Jgc9nfq2iOlwc6s0mq0Zmi5Z5E3sXJP1ktmubqSKFNg=; b=JIo24/yHtvAgDCE9bJsSv2rGHgygZegVZ8uM/7gfVUoyzuqKpDXJO+1RsC9vVtMwsy 1uMAmCIAAiWhLQzng4jM3YEBIs7vLMLRwcFpd4QGD5eV2QS3rjngFMIaA4bGCFvH2tc0 Z6CczEdDep0AHs6q8rMtkNqNprXmgjsflNDTVaoQV1l3SGppZYUloxMJdZqrPrZkKoBU oN78wjgk57Lz5XIMf6cOr+KKW47ZQZxEcQsLgKMeA92PzcZawsJWX/Cld1vDxWDGUSEf 1rRmaa/1EakbI8gFFvIYLLUpuPjruyxXRy2wp/e/cQ4hjqC1H5YJmOK7ZYHOz/z11f9m ViVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737559665; x=1738164465; 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=Jgc9nfq2iOlwc6s0mq0Zmi5Z5E3sXJP1ktmubqSKFNg=; b=Pt39U9pQeUWira+J4FzSyXdMm6P8C53tBrIyQwYfdBIr7vuKD8GKgcHJGVQMTtX0oS ScJsD7t2aTVkBbJo/GTe2wYJVAn1bz6eD3x50b5unOoHG3lusQanlErBAH2vU32oUNOe /oD9Hqo4N7Oz/xmNSV10hdyYNvp1vAw8fCdSzLZcj1EDUa288lFQ2A4iA5L0vqWVM65/ EmMYjFCHsZ7sxGTPI8uUeL2siW9E3ICcRwrcxL9w8yxTJoIkahdiDJbtHNMzd/xtchhQ Sng1h0fp3m9Dw+bsSxRHGqI5XJ0PuFSknx4xUbhg4zYvNrnXP1FQ7aD4awRno91zhrtv /+ig== X-Forwarded-Encrypted: i=1; AJvYcCVc0oxdcfjbiuO5RFZhZ5KqyGvfUi2eOvlhF1yzfWhg7n54fuAnAS19LtX8KwvBKFqG/zBrh5rgIg==@kvack.org X-Gm-Message-State: AOJu0YyOzmI03hTAIP4K+FTS3wpoayHN69PAexSjvNiBMg2Z7UOJUrmb j9G9aqkF4OXUnqpYJFXzzp7hAdf0G0VJtOuQCzmJyJWeMeWho7rhUcD0ASXrH/c3rFhESZrbGw= = X-Google-Smtp-Source: AGHT+IHEkzSzLwVsFu5ntmtLdr5cJ4QHwoT2gxzzZv4Yr44awgwt7PF8ZqHc/3pFw4mwJzLSVOUAJ9pbQg== X-Received: from wmof18.prod.google.com ([2002:a05:600c:44d2:b0:438:af71:5fbb]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:310a:b0:434:f335:855 with SMTP id 5b1f17b1804b1-43891440ab1mr187994315e9.28.1737559664727; Wed, 22 Jan 2025 07:27:44 -0800 (PST) Date: Wed, 22 Jan 2025 15:27:31 +0000 In-Reply-To: <20250122152738.1173160-1-tabba@google.com> Mime-Version: 1.0 References: <20250122152738.1173160-1-tabba@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <20250122152738.1173160-3-tabba@google.com> Subject: [RFC PATCH v1 2/9] 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-Queue-Id: 210D340008 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: drmekgd1ioofk1xs1osxeirkoc1bywd5 X-HE-Tag: 1737559665-984697 X-HE-Meta: U2FsdGVkX186/BDB5Wr2nuGUWfCG3CeFMoIignDSZ/LtVJJiJJ7RMP+NCPIfFkGu2VAcYaRyZa3wQxj2ovS8NSWtRrOWMVgwzHeWfb/WbgK5scsXJc5tBySu9vGwCl/0F8Ha6Gfpnyc9n7Ws8cJC9RMb3yyxe/a+sWDvCBStfRel0RGWbjOdzwev737MGAetMFw6uiAtYrdHLjeG3ha54dPqUkrkx2CWJ4I/BH6LVt98+yuydsCnuR1dTwVz2P+ZWbbMp+YkVPWu24P2Zxb6sJ9I5nvFPW3WoeOQ4R/6Lc+vRnVJm9iEPnHCERZEl83mOlKXElXjMq2Se9G/LIinMa9yTg6jrVO/qgICuOPHp2Np+Jiy079KFnOP899tw7mIH1ID56okO0okQELNFHC2DG0A3bX5oDjMHIO4KfxrpK0SgZnTeq6EueYJ0hgTxnDP+atR9lzloTl9QjdSExjlLcnzqHMzWcp/VM6pxL9gzSW+ayws65uC9ipOHv/p5Z3pVhBpe44vdTVzmD2T2tbXGharZ6XRZLtoSd0qrYGL/QHorV9AgwMbXYR2GJ1J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/4ez831l4+dGfOUEQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, 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 | 118 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 99 insertions(+), 19 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index de2c11dae231..ad9802012a3f 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3094,21 +3094,93 @@ static int next_segment(unsigned long len, int offset) return len; } -/* 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) +#ifdef CONFIG_KVM_GMEM_MAPPABLE +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(kvm, slot, gfn, &pfn, &page, NULL); + if (r) + return r; + + memcpy(data, page_address(page) + offset, len); + 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(kvm, slot, gfn, &pfn, &page, NULL); + if (r) + return r; + + memcpy(page_address(page) + offset, data, len); + 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 *kvm, struct kvm_memory_slot *slot, + gfn_t gfn, void *data, int offset, int len) +{ unsigned long addr; if (WARN_ON_ONCE(offset + len > PAGE_SIZE)) return -EFAULT; + if (kvm_arch_private_mem_inplace(kvm) && + 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; } @@ -3118,7 +3190,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); @@ -3127,7 +3199,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); @@ -3204,22 +3276,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 (kvm_arch_private_mem_inplace(kvm) && + 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; }