From patchwork Wed Jan 29 17:23:12 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13953967 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 6A50EC02192 for ; Wed, 29 Jan 2025 17:23:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9AD9C280080; Wed, 29 Jan 2025 12:23:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8E795280067; Wed, 29 Jan 2025 12:23:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6519C280080; Wed, 29 Jan 2025 12:23:32 -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 45769280067 for ; Wed, 29 Jan 2025 12:23:32 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 113D8A0A99 for ; Wed, 29 Jan 2025 17:23:32 +0000 (UTC) X-FDA: 83061161064.29.19DCD7B Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) by imf16.hostedemail.com (Postfix) with ESMTP id 279DE18001D for ; Wed, 29 Jan 2025 17:23:29 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=eSkmBjev; spf=pass (imf16.hostedemail.com: domain of 3EGSaZwUKCIU2jkkjpxxpun.lxvurw36-vvt4jlt.x0p@flex--tabba.bounces.google.com designates 209.85.221.74 as permitted sender) smtp.mailfrom=3EGSaZwUKCIU2jkkjpxxpun.lxvurw36-vvt4jlt.x0p@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=1738171410; 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=RoVkPwaMvkLCkrMEK45Vw1CTJ2upgTYFUQnx7UT1Wys=; b=j2vUXd53wJPQgvH9WtEDrISWKGEl2umcZ8BH+4KDQ/g7ROpwcIG/A3H2DdbxXsiiADymRt MniXw04EQwsFZl1BEkQztQBPZcFMi5zpoWfbA0+rjxfpVSTgyOga/LR0yiaWbJvKisPVP0 YRvCZuSqVWD9CSPEhIpOgvDj5O257fA= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=eSkmBjev; spf=pass (imf16.hostedemail.com: domain of 3EGSaZwUKCIU2jkkjpxxpun.lxvurw36-vvt4jlt.x0p@flex--tabba.bounces.google.com designates 209.85.221.74 as permitted sender) smtp.mailfrom=3EGSaZwUKCIU2jkkjpxxpun.lxvurw36-vvt4jlt.x0p@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738171410; a=rsa-sha256; cv=none; b=SfM24yjW6z+VKp2oW68D6/Mp3mUzfcDB8gmpPjk40CiWkZEDBP5uDbKk0SvyYpneOENghf vjoYsyVJt81B+4rU5AgmejunxFHqN0AvONZ4UHqox8dMBVwaO7sHp0wB8pj3lxcCbTy1Vt ox2nNg/iWz+7HXCkjOeeYGjaOv7kbh0= Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-385fdff9db5so2511814f8f.0 for ; Wed, 29 Jan 2025 09:23:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738171408; x=1738776208; 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=RoVkPwaMvkLCkrMEK45Vw1CTJ2upgTYFUQnx7UT1Wys=; b=eSkmBjevPyp1nO7v1j6X7JJwwCbdh7ufwQaU4rmXxNFh6tMLz2S+UovdIAshXRTZdX d3FvLEQ8YDPBiA9BU8Yf2yaCrQs5VTMqALUn71AIc7M+wQcRKVSMPpaQmjaIQcaHt8sT iUas1nqXxPQ4VCl86zeW0DpjzTUjw8vhf5xAsmS5uLJmk/Sj72J7vXhDPCMsNUChR8H5 teUb4vHX1lxtzVqBovlwLKgu1OIBwILqAV2+tQj6attFuLYjH/nHJ6Jk13nKWf8+iCz2 91QuVSXzeWJfLuxWljRO5TxBJUnNERgzgT7eaoxLMdtykJK7LRkSeCuHEAHzv8sqZPoO Sk3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738171408; x=1738776208; 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=RoVkPwaMvkLCkrMEK45Vw1CTJ2upgTYFUQnx7UT1Wys=; b=dzhzGWTxQCZ40MniHvTLEUjx9UVf9S2bfSStI9+Z9HzJCg3nVbBaomtD0SOOxX2ZhL KuiguTle35IG2XXFg8y5QOvS+z2zBbCw0h6pW3j+CTNmsKce7S0dMx77fcCdCUI39v3V Ok+eLXlNTs92MupvaTz5kUen0Mog4GCyxgPPI6wA4HzpgAy0BeCRNLZuzb+bQ8UlNcSv JFmYJN/LNKM4eOuuhDsbRR8/HQQuZAorKokeL87pQdHdTOhvbJgy84kq19wNFq+9IZ0c EvT70xKAxlgRtRixYcVT5Mu/gZ13LhZgJhs9JsGGrPCV6coAJyUMmOx9KEEPBEKex1K/ 2ing== X-Forwarded-Encrypted: i=1; AJvYcCVxLO+TZfnzHFTRoatb/H83BKbdnJFGL788PJxwIT74CJX/Oq+svr2jUYrWARWFAIsEBk4v3n7oCw==@kvack.org X-Gm-Message-State: AOJu0YzORwFWXl4MXObfB0PfR0hrY11SdRLzixo0Z9Z0ZviowgGdca3H vLwqeRVHvclbkaAK/oWto/H/1NTjALF91HqSMv1knKlKZ/+ro+S/qyNKg7vUuXXou5P2v6VdCw= = X-Google-Smtp-Source: AGHT+IFhvEin4XE4wsGkRNdDKMzEHI3B5ItG8XmtbU1PM70tgZsQHYBr2xJu8rB4jweNCcCb7JB0XXISUQ== X-Received: from wmbez5.prod.google.com ([2002:a05:600c:83c5:b0:434:f9da:44af]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:154c:b0:38a:39ad:3e2f with SMTP id ffacd0b85a97d-38c5194c9femr3444938f8f.2.1738171408672; Wed, 29 Jan 2025 09:23:28 -0800 (PST) Date: Wed, 29 Jan 2025 17:23:12 +0000 In-Reply-To: <20250129172320.950523-1-tabba@google.com> Mime-Version: 1.0 References: <20250129172320.950523-1-tabba@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129172320.950523-4-tabba@google.com> Subject: [RFC PATCH v2 03/11] KVM: guest_memfd: Allow host to map guest_memfd() pages 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-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 279DE18001D X-Stat-Signature: djped8wfutktrnh76h7nha4bkdafasks X-HE-Tag: 1738171409-539586 X-HE-Meta: U2FsdGVkX18Jr47xpqIZgaVx73XzMu59ahMuothCdpCP8QzZLkA85m0L+yjUe7z7sFFO1CbHZVedwgsvEmUQJquhLI9YeRO2iC1gsnE4BWScDuRphyfxNcNyhnFV6RJkN7+JaFPJLv0id/paWIO61G8HCxLelwGTkn1mp9SurA+yIx1kygpzMAYSOvY0w8u+LZJrlxC8j1WpsSRQQQ24EKkgz+Pd9JP3Zwo+T7ies5fYt+UazI6PU0EEJYhQrM7NwyKnaFFLFsIwo7oJhaK4lKjqffSpeVdvQhgUnaV7U116U+s0wWKAlMYRu+3sOwUqkqsTuTcwjgkiJwjxacohweh8ETpHD5V2Omv07Lp+hMzTsUVn/3FiMRbZLhIYTWqr1RSMfigXCN1N/8u9d74LAGUS88WBOBa3i59aAIgPW3nA35t5vmnY7LLnBEpTxWBZEijBk9NRaeykQDSsyc9KhyZE1WcOldVxHW70CMk/aLDvLeILbH0JHBOQ/dbZcMs63lA5+rzykoqVyW1+5wvyaByC7S/XCrkKl1IWb4WWEdknTqJiN3b/e7ezCwraqG9j8D2RdBkgyyC1ENFuPjFDT66fCfAa5e1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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: Add support for mmap() and fault() for guest_memfd backed memory in the host for VMs that support in-place conversion between shared and private (shared memory). To that end, this patch adds the ability to check whether the VM type has that support, and only allows mapping its memory if that's the case. Additionally, this behavior is gated with a new configuration option, CONFIG_KVM_GMEM_SHARED_MEM. Signed-off-by: Fuad Tabba --- This patch series will allow shared memory support for software VMs in x86. It will also introduce a similar VM type for arm64 and allow shared memory support for that. In the future, pKVM will also support shared memory. --- include/linux/kvm_host.h | 11 ++++++ virt/kvm/Kconfig | 4 +++ virt/kvm/guest_memfd.c | 77 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 401439bb21e3..408429f13bf4 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -717,6 +717,17 @@ static inline bool kvm_arch_has_private_mem(struct kvm *kvm) } #endif +/* + * Arch code must define kvm_arch_gmem_supports_shared_mem if support for + * private memory is enabled and it supports in-place shared/private conversion. + */ +#if !defined(kvm_arch_gmem_supports_shared_mem) && !IS_ENABLED(CONFIG_KVM_PRIVATE_MEM) +static inline bool kvm_arch_gmem_supports_shared_mem(struct kvm *kvm) +{ + return false; +} +#endif + #ifndef kvm_arch_has_readonly_mem static inline bool kvm_arch_has_readonly_mem(struct kvm *kvm) { diff --git a/virt/kvm/Kconfig b/virt/kvm/Kconfig index 54e959e7d68f..4e759e8020c5 100644 --- a/virt/kvm/Kconfig +++ b/virt/kvm/Kconfig @@ -124,3 +124,7 @@ config HAVE_KVM_ARCH_GMEM_PREPARE config HAVE_KVM_ARCH_GMEM_INVALIDATE bool depends on KVM_PRIVATE_MEM + +config KVM_GMEM_SHARED_MEM + select KVM_PRIVATE_MEM + bool diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 47a9f68f7b24..86441581c9ae 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -307,7 +307,84 @@ static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn) return gfn - slot->base_gfn + slot->gmem.pgoff; } +#ifdef CONFIG_KVM_GMEM_SHARED_MEM +static vm_fault_t kvm_gmem_fault(struct vm_fault *vmf) +{ + struct inode *inode = file_inode(vmf->vma->vm_file); + struct folio *folio; + vm_fault_t ret = VM_FAULT_LOCKED; + + filemap_invalidate_lock_shared(inode->i_mapping); + + folio = kvm_gmem_get_folio(inode, vmf->pgoff); + if (IS_ERR(folio)) { + ret = VM_FAULT_SIGBUS; + goto out_filemap; + } + + if (folio_test_hwpoison(folio)) { + ret = VM_FAULT_HWPOISON; + goto out_folio; + } + + if (WARN_ON_ONCE(folio_test_guestmem(folio))) { + ret = VM_FAULT_SIGBUS; + goto out_folio; + } + + /* No support for huge pages. */ + if (WARN_ON_ONCE(folio_nr_pages(folio) > 1)) { + ret = VM_FAULT_SIGBUS; + goto out_folio; + } + + if (!folio_test_uptodate(folio)) { + clear_highpage(folio_page(folio, 0)); + folio_mark_uptodate(folio); + } + + vmf->page = folio_file_page(folio, vmf->pgoff); + +out_folio: + if (ret != VM_FAULT_LOCKED) { + folio_unlock(folio); + folio_put(folio); + } + +out_filemap: + filemap_invalidate_unlock_shared(inode->i_mapping); + + return ret; +} + +static const struct vm_operations_struct kvm_gmem_vm_ops = { + .fault = kvm_gmem_fault, +}; + +static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma) +{ + struct kvm_gmem *gmem = file->private_data; + + if (!kvm_arch_gmem_supports_shared_mem(gmem->kvm)) + return -ENODEV; + + if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) != + (VM_SHARED | VM_MAYSHARE)) { + return -EINVAL; + } + + file_accessed(file); + vm_flags_set(vma, VM_DONTDUMP); + vma->vm_ops = &kvm_gmem_vm_ops; + + return 0; +} +#else +#define kvm_gmem_mmap NULL +#endif /* CONFIG_KVM_GMEM_SHARED_MEM */ + static struct file_operations kvm_gmem_fops = { + .mmap = kvm_gmem_mmap, .open = generic_file_open, .release = kvm_gmem_release, .fallocate = kvm_gmem_fallocate,