This series adds restricted mmap() support to guest_memfd, as well as support for guest_memfd on arm64. It is based on Linux 6.12-rc2. Changes since V2 [1]: - Use refcount to determine whether a page/folio is mapped by the host rather than folio_mapcount()+folio_maybe_dma_pinned() (DavidH) - Track of mappability of guest memory at the host in the guest_memfd inode (Ackerly) - Refactoring and tidying up (Sean, Ackerly) By design, guest_memfd cannot be mapped, read, or written by the host. In pKVM, memory shared between a protected guest and the host is shared in-place, unlike other confidential computing solutions that guest_memfd was originally envisaged for (e.g, TDX). When initializing a guest, as well as when accessing memory shared by a protected guest with the host, it would be useful to support mapping guest memory at the host to avoid copying its contents. One of the benefits of guest_memfd is that it prevents a misbehaving host from crashing the system when attempting to access private guest memory (deliberately or accidentally), since this memory isn't mapped to begin with. Without guest_memfd, the hypervisor would still prevent such accesses, but in certain cases the host kernel wouldn't be able to recover, causing the system to crash. Support for mmap() in this patch series maintains the invariant that only memory shared with the host, either explicitly by the guest or implicitly before the guest has started running (in order to populate its memory) is allowed to have a valid mapping at the host. At no point should _private_ guest memory have any mappings at the host. This patch series is divided into two parts: The first part is to the KVM core code. It adds opt-in support for mapping guest memory only as long as it is shared, or optionally when it is first created. For that, the host needs to know the mappability status of guest memory. Therefore, the series adds a structure to track whether memory is mappable. This new structure is associated with each guest_memfd inode object. The second part of the series adds guest_memfd support for arm64. The patch series enforces the invariant that only memory shared with the host can be mapped by the host userspace in vm_operations_struct:fault(), instead of file_operations:mmap(). On a fault, we check whether the page is allowed to be mapped. If not, we deliver a SIGBUS to the current task, as discussed in the Linux MM Alignment Session and LPC 2024 on this topic [2,3 ]. Currently, there's no support for huge pages, which is something we hope to support in the near future [4]. Cheers, /fuad [1] https://lore.kernel.org/all/20240801090117.3841080-1-tabba@xxxxxxxxxx/ [2] https://lore.kernel.org/all/20240712232937.2861788-1-ackerleytng@xxxxxxxxxx/ [3] https://lpc.events/event/18/sessions/183/#20240919 [4] https://lore.kernel.org/all/cover.1726009989.git.ackerleytng@xxxxxxxxxx/ Ackerley Tng (2): KVM: guest_memfd: Make guest mem use guest mem inodes instead of anonymous inodes KVM: guest_memfd: Track mappability within a struct kvm_gmem_private Fuad Tabba (9): KVM: guest_memfd: Introduce kvm_gmem_get_pfn_locked(), which retains the folio lock KVM: guest_memfd: Allow host to mmap guest_memfd() pages when shared KVM: guest_memfd: Add guest_memfd support to kvm_(read|/write)_guest_page() KVM: guest_memfd: Add KVM capability to check if guest_memfd is host mappable KVM: guest_memfd: Add a guest_memfd() flag to initialize it as mappable KVM: guest_memfd: selftests: guest_memfd mmap() test when mapping is allowed KVM: arm64: Skip VMA checks for slots without userspace address KVM: arm64: Handle guest_memfd()-backed guest page faults KVM: arm64: Enable guest_memfd private memory when pKVM is enabled Documentation/virt/kvm/api.rst | 4 + arch/arm64/include/asm/kvm_host.h | 3 + arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/mmu.c | 120 +++++- include/linux/kvm_host.h | 63 +++ include/uapi/linux/kvm.h | 2 + include/uapi/linux/magic.h | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../testing/selftests/kvm/guest_memfd_test.c | 57 ++- virt/kvm/Kconfig | 4 + virt/kvm/guest_memfd.c | 397 ++++++++++++++++-- virt/kvm/kvm_main.c | 279 +++++++++++- 12 files changed, 877 insertions(+), 55 deletions(-) base-commit: 8cf0b93919e13d1e8d4466eb4080a4c4d9d66d7b -- 2.47.0.rc0.187.ge670bccf7e-goog