KVM: Restricted mapping of guest_memfd at the host and arm64 support
Subsystems (9)
v4 cover KVM: Restricted mapping of guest_memfd at the host and arm64 support
This series adds restricted mmap() support to guest_memfd, as well as support for guest_memfd on arm64. It is based on Linux 6.13-rc2. Please refer to v3 for the context [1].
Main changes since v3:
- Added a new folio type for guestmem, used to register a callback when a folio's reference count reaches 0 (Matthew Wilcox, DavidH) [2]
- Introduce new mappability states for folios, where a folio can be mappable by the host and the guest, only the guest, or by no one (transient state)
- Rebased on Linux 6.13-rc2
- Refactoring and tidying up
Cheers,
/fuad
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
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 (12):
mm: Consolidate freeing of typed folios on final folio_put()
KVM: guest_memfd: Introduce kvm_gmem_get_pfn_locked(), which retains
the folio lock
KVM: guest_memfd: Folio mappability states and functions that manage
their transition
KVM: guest_memfd: Handle final folio_put() of guestmem pages
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 | 119 +++- include/linux/kvm_host.h | 75 +++ include/linux/page-flags.h | 22 + include/uapi/linux/kvm.h | 2 + include/uapi/linux/magic.h | 1 + mm/debug.c | 1 + mm/swap.c | 28 +- tools/testing/selftests/kvm/Makefile | 1 + .../testing/selftests/kvm/guest_memfd_test.c | 64 +- virt/kvm/Kconfig | 4 + virt/kvm/guest_memfd.c | 579 +++++++++++++++++- virt/kvm/kvm_main.c | 229 ++++++- 15 files changed, 1074 insertions(+), 59 deletions(-)
base-commit: fac04efc5c793dccbd07e2d59af9f90b7fc0dca4
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
RFC PATCH v4 1/14 mm: Consolidate freeing of typed folios on final folio_put()
Some folio types, such as hugetlb, handle freeing their own folios. Moreover, guest_memfd will require being notified once a folio's reference count reaches 0 to facilitate shared to private folio conversion, without the folio actually being freed at that point.
As a first step towards that, this patch consolidates freeing folios that have a type. The first user is hugetlb folios. Later in this patch series, guest_memfd will become the second user of this.
--- include/linux/page-flags.h | 15 +++++++++++++++ mm/swap.c | 24 +++++++++++++++++++----- 2 files changed, 34 insertions(+), 5 deletions(-)
include/linux/page-flags.h +15 -0
@@ -970,6 +970,21 @@ static inline bool page_has_type(const struct page *page)return page_mapcount_is_type (data_race (page->page_type)); } +static inline int page_get_type (const struct page *page) +{ +return page->page_type >>24 ; +} + +static inline bool folio_has_type (const struct folio *folio) +{ +return page_has_type (&folio->page); +} + +static inline int folio_get_type (const struct folio *folio) +{ +return page_get_type (&folio->page); +} +#define FOLIO_TYPE_OPS (lname, fname) \ static __always_inline bool folio_test_##fname(const struct folio *folio) \ { \
mm/swap.c +19 -5
@@ -94,6 +94,20 @@ static void page_cache_release(struct folio *folio)unlock_page_lruvec_irqrestore (lruvec, flags); } +static void free_typed_folio (struct folio *folio) +{ +switch (folio_get_type (folio)) { +case PGTY_hugetlb: +free_huge_folio (folio); +return ; +case PGTY_offline: +/* Nothing to do, it's offline. */ +return ; +default : +WARN_ON_ONCE (1 ); + } +} +void __folio_put (struct folio *folio) {if (unlikely (folio_is_zone_device (folio))) {
@@ -101,8 +115,8 @@ void __folio_put(struct folio *folio)return ; } -if (folio_test_hugetlb (folio)) { -free_huge_folio (folio); +if (unlikely (folio_has_type (folio))) { +free_typed_folio (folio);return ; }
@@ -934,13 +948,13 @@ void folios_put_refs(struct folio_batch *folios, unsigned int *refs)if (!folio_ref_sub_and_test (folio, nr_refs))continue ; -/* hugetlb has its own memcg */ -if (folio_test_hugetlb (folio)) { +if (unlikely (folio_has_type (folio))) { +/* typed folios have their own memcg, if any */ if (lruvec) {unlock_page_lruvec_irqrestore (lruvec, flags); lruvec =NULL ; } -free_huge_folio (folio); +free_typed_folio (folio);continue ; }folio_unqueue_deferred_split (folio);
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
kernel test robot
build botRe: [RFC PATCH v4 01/14] mm: Consolidate freeing of typed folios on final folio_put()
kernel test robot noticed the following build errors:
Hi Fuad,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:
[auto build test ERROR on fac04efc5c793dccbd07e2d59af9f90b7fc0dca4]
url: https://github.com/intel-lab-lkp/linux/commits/Fuad-Tabba/mm-Consolidate-freeing-of-typed-folios-on-final-folio_put/20241214-005109 base: fac04efc5c793dccbd07e2d59af9f90b7fc0dca4 patch link: https://lore.kernel.org/r/20241213164811.2006197-2-tabba%40google.com patch subject: [RFC PATCH v4 01/14] mm: Consolidate freeing of typed folios on final folio_put() config: i386-buildonly-randconfig-002-20241215 (https://download.01.org/0day-ci/archive/20241215/[email protected]/config) compiler: gcc-12 (Debian 12.2.0-14) 12.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241215/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
ld: mm/swap.o: in function `free_typed_folio':
>> swap.c:(.text+0x4e9): undefined reference to `free_huge_folio'
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
RFC PATCH v4 2/14 KVM: guest_memfd: Make guest mem use guest mem inodes instead of anonymous inodes
In reply to Fuad TabbaFrom: Ackerley Tng <[email protected]>
Using guest mem inodes allows us to store metadata for the backing memory on the inode. Metadata will be added in a later patch to support HugeTLB pages.
Metadata about backing memory should not be stored on the file, since the file represents a guest_memfd's binding with a struct kvm, and metadata about backing memory is not unique to a specific binding and struct kvm.
--- include/uapi/linux/magic.h | 1 + virt/kvm/guest_memfd.c | 119 ++++++++++++++++++++++++++++++------- 2 files changed, 100 insertions(+), 20 deletions(-)
include/uapi/linux/magic.h +1 -0
@@ -103,5 +103,6 @@#define DEVMEM_MAGIC 0x454d444d/* "DMEM" */ #define SECRETMEM_MAGIC 0x5345434d/* "SECM" */ #define PID_FS_MAGIC 0x50494446/* "PIDF" */ +#define GUEST_MEMORY_MAGIC 0x474d454d/* "GMEM" */ #endif /* __LINUX_MAGIC_H__ */
virt/kvm/guest_memfd.c +99 -20
166 lines not on this page · raw message
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
RFC PATCH v4 3/14 KVM: guest_memfd: Introduce kvm_gmem_get_pfn_locked(), which retains the folio lock
In reply to Fuad TabbaCreate a new variant of kvm_gmem_get_pfn(), which retains the folio lock if it returns successfully. This is needed in subsequent patches in order to protect against races when checking whether a folio can be mapped by the host.
--- include/linux/kvm_host.h | 11 +++++++++++ virt/kvm/guest_memfd.c | 27 ++++++++++++++++++++------- 2 files changed, 31 insertions(+), 7 deletions(-)
include/linux/kvm_host.h +11 -0
@@ -2500,6 +2500,9 @@ static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn)int kvm_gmem_get_pfn (struct kvm *kvm,struct kvm_memory_slot *slot,gfn_t gfn,kvm_pfn_t *pfn,struct page **page,int *max_order); +int kvm_gmem_get_pfn_locked (struct kvm *kvm,struct kvm_memory_slot *slot, +gfn_t gfn,kvm_pfn_t *pfn,struct page **page, +int *max_order);#else static inline int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn,
@@ -2509,6 +2512,14 @@ static inline int kvm_gmem_get_pfn(struct kvm *kvm,KVM_BUG_ON (1 , kvm); return -EIO; } +static inline int kvm_gmem_get_pfn_locked (struct kvm *kvm, +struct kvm_memory_slot *slot, +gfn_t gfn,kvm_pfn_t *pfn, +struct page **page,int *max_order) +{ +KVM_BUG_ON (1 , kvm); +return -EIO ; +}#endif /* CONFIG_KVM_PRIVATE_MEM */ #ifdef CONFIG_HAVE_KVM_ARCH_GMEM_PREPARE
virt/kvm/guest_memfd.c +20 -7
@@ -672,9 +672,9 @@ static struct folio *__kvm_gmem_get_pfn(struct file *file,return folio; } -int kvm_gmem_get_pfn (struct kvm *kvm,struct kvm_memory_slot *slot, -gfn_t gfn,kvm_pfn_t *pfn,struct page **page, -int *max_order) +int kvm_gmem_get_pfn_locked (struct kvm *kvm,struct kvm_memory_slot *slot, +gfn_t gfn,kvm_pfn_t *pfn,struct page **page, +int *max_order) {pgoff_t index =kvm_gmem_get_index (slot, gfn);struct file *file =kvm_gmem_get_file (slot);
@@ -694,17 +694,30 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,if (!is_prepared) r =kvm_gmem_prepare_folio (kvm, slot, gfn, folio); -folio_unlock (folio); - -if (!r) +if (!r) { *page =folio_file_page (folio, index); -else + }else { +folio_unlock (folio);folio_put (folio); + } out:fput (file);return r; } +EXPORT_SYMBOL_GPL (kvm_gmem_get_pfn_locked); + +int kvm_gmem_get_pfn (struct kvm *kvm,struct kvm_memory_slot *slot, +gfn_t gfn,kvm_pfn_t *pfn,struct page **page, +int *max_order) +{ +int r =kvm_gmem_get_pfn_locked (kvm, slot, gfn, pfn, page, max_order); + +if (!r) +unlock_page (*page); + +return r; +}EXPORT_SYMBOL_GPL (kvm_gmem_get_pfn);#ifdef CONFIG_KVM_GENERIC_PRIVATE_MEM
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
RFC PATCH v4 4/14 KVM: guest_memfd: Track mappability within a struct kvm_gmem_private
In reply to Fuad TabbaFrom: Ackerley Tng <[email protected]>
Track whether guest_memfd memory can be mapped within the inode, since it is property of the guest_memfd's memory contents.
The guest_memfd PRIVATE memory attribute is not used for two reasons. First because it reflects the userspace expectation for that memory location, and therefore can be toggled by userspace. The second is, although each guest_memfd file has a 1:1 binding with a KVM instance, the plan is to allow multiple files per inode, e.g. to allow intra-host migration to a new KVM instance, without destroying guest_memfd.
--- virt/kvm/guest_memfd.c | 56 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 5 deletions(-)
virt/kvm/guest_memfd.c +51 -5
@@ -18,6 +18,17 @@ struct kvm_gmem {struct list_head entry; }; +struct kvm_gmem_inode_private { +#ifdef CONFIG_KVM_GMEM_MAPPABLE +struct xarray mappable_offsets; +#endif +}; + +static struct kvm_gmem_inode_private *kvm_gmem_private (struct inode *inode) +{ +return inode->i_mapping->i_private_data; +} +/** * folio_file_pfn - like folio_file_page, but return a pfn. * @folio: The folio which contains this index.
@@ -312,8 +323,28 @@ static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn)return gfn - slot->base_gfn + slot->gmem.pgoff; } +static void kvm_gmem_evict_inode (struct inode *inode) +{ +struct kvm_gmem_inode_private *private =kvm_gmem_private (inode); + +#ifdef CONFIG_KVM_GMEM_MAPPABLE +/* +* .evict_inode can be called before private data is set up if there are +* issues during inode creation. +*/ +if (private) +xa_destroy (&private->mappable_offsets); +#endif + +truncate_inode_pages_final (inode->i_mapping); + +kfree (private); +clear_inode (inode); +} +static const struct super_operations kvm_gmem_super_operations = { - .statfs = simple_statfs, + .statfs = simple_statfs, + .evict_inode = kvm_gmem_evict_inode, };static int kvm_gmem_init_fs_context (struct fs_context *fc)
@@ -440,6 +471,7 @@ static struct inode *kvm_gmem_inode_make_secure_inode(const char *name, loff_t size, u64 flags) {const struct qstr qname =QSTR_INIT (name,strlen (name)); +struct kvm_gmem_inode_private *private;struct inode *inode;int err;
@@ -448,10 +480,19 @@ static struct inode *kvm_gmem_inode_make_secure_inode(const char *name,return inode; err =security_inode_init_security_anon (inode, &qname,NULL ); -if (err) { -iput (inode); -return ERR_PTR (err); - } +if (err) + goto out; + + err = -ENOMEM ; + private =kzalloc (sizeof (*private),GFP_KERNEL ); +if (!private) + goto out; + +#ifdef CONFIG_KVM_GMEM_MAPPABLE +xa_init (&private->mappable_offsets); +#endif + + inode->i_mapping->i_private_data = private; inode->i_private = (void *)(unsigned long )flags; inode->i_op = &kvm_gmem_iops;
@@ -464,6 +505,11 @@ static struct inode *kvm_gmem_inode_make_secure_inode(const char *name,WARN_ON_ONCE (!mapping_unevictable (inode->i_mapping));return inode; + +out: +iput (inode); + +return ERR_PTR (err); } static struct file *kvm_gmem_inode_create_getfile(void *priv, loff_t size,
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
RFC PATCH v4 5/14 KVM: guest_memfd: Folio mappability states and functions that manage their transition
In reply to Fuad TabbaTo allow restricted mapping of guest_memfd folios by the host, guest_memfd needs to track whether they can be mapped and by who, since the mapping will only be allowed under conditions where it safe to access these folios. These conditions depend on the folios being explicitly shared with the host, or not yet exposed to the guest (e.g., at initialization).
This patch introduces states that determine whether the host and the guest can fault in the folios as well as the functions that manage transitioning between those states.
--- include/linux/kvm_host.h | 53 ++++++++++++++ virt/kvm/guest_memfd.c | 153 +++++++++++++++++++++++++++++++++++++++ virt/kvm/kvm_main.c | 92 +++++++++++++++++++++++ 3 files changed, 298 insertions(+)
include/linux/kvm_host.h +53 -0
@@ -2564,4 +2564,57 @@ long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu, struct kvm_pre_fault_memory *range);#endif +#ifdef CONFIG_KVM_GMEM_MAPPABLE +bool kvm_gmem_is_mappable (struct kvm *kvm,gfn_t gfn,gfn_t end); +int kvm_gmem_set_mappable (struct kvm *kvm,gfn_t start,gfn_t end); +int kvm_gmem_clear_mappable (struct kvm *kvm,gfn_t start,gfn_t end); +int kvm_slot_gmem_set_mappable (struct kvm_memory_slot *slot,gfn_t start, +gfn_t end); +int kvm_slot_gmem_clear_mappable (struct kvm_memory_slot *slot,gfn_t start, +gfn_t end); +bool kvm_slot_gmem_is_mappable (struct kvm_memory_slot *slot,gfn_t gfn); +bool kvm_slot_gmem_is_guest_mappable (struct kvm_memory_slot *slot,gfn_t gfn); +#else +static inline bool kvm_gmem_is_mappable (struct kvm *kvm,gfn_t gfn,gfn_t end) +{ +WARN_ON_ONCE (1 ); +return false; +} +static inline int kvm_gmem_set_mappable (struct kvm *kvm,gfn_t start,gfn_t end) +{ +WARN_ON_ONCE (1 ); +return -EINVAL ; +} +static inline int kvm_gmem_clear_mappable (struct kvm *kvm,gfn_t start, +gfn_t end) +{ +WARN_ON_ONCE (1 ); +return -EINVAL ; +} +static inline int kvm_slot_gmem_set_mappable (struct kvm_memory_slot *slot, +gfn_t start,gfn_t end) +{ +WARN_ON_ONCE (1 ); +return -EINVAL ; +} +static inline int kvm_slot_gmem_clear_mappable (struct kvm_memory_slot *slot, +gfn_t start,gfn_t end) +{ +WARN_ON_ONCE (1 ); +return -EINVAL ; +} +static inline bool kvm_slot_gmem_is_mappable (struct kvm_memory_slot *slot, +gfn_t gfn) +{ +WARN_ON_ONCE (1 ); +return false; +} +static inline bool kvm_slot_gmem_is_guest_mappable (struct kvm_memory_slot *slot, +gfn_t gfn) +{ +WARN_ON_ONCE (1 ); +return false; +} +#endif /* CONFIG_KVM_GMEM_MAPPABLE */ +#endif
virt/kvm/guest_memfd.c +153 -0
159 lines not on this page · raw message
virt/kvm/kvm_main.c +92 -0
98 lines not on this page · raw message
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
RFC PATCH v4 6/14 KVM: guest_memfd: Handle final folio_put() of guestmem pages
In reply to Fuad TabbaBefore transitioning a guest_memfd folio to unshared, thereby disallowing access by the host and allowing the hypervisor to transition its view of the guest page as private, we need to be sure that the host doesn't have any references to the folio.
This patch introduces a new type for guest_memfd folios, and uses that to register a callback that informs the guest_memfd subsystem when the last reference is dropped, therefore knowing that the host doesn't have any remaining references.
---
The function kvm_slot_gmem_register_callback() isn't used in this series. It will be used later in code that performs unsharing of memory. I have tested it with pKVM, based on downstream code [*]. It's included in this RFC since it demonstrates the plan to handle unsharing of private folios.
[*] https://android-kvm.googlesource.com/linux/+/refs/heads/tabba/guestmem-6.13-v4-pkvm
--- include/linux/kvm_host.h | 11 +++ include/linux/page-flags.h | 7 ++ mm/debug.c | 1 + mm/swap.c | 4 + virt/kvm/guest_memfd.c | 145 +++++++++++++++++++++++++++++++++++++ 5 files changed, 168 insertions(+)
include/linux/kvm_host.h +11 -0
@@ -2574,6 +2574,8 @@ int kvm_slot_gmem_clear_mappable(struct kvm_memory_slot *slot, gfn_t start, gfn_t end);bool kvm_slot_gmem_is_mappable (struct kvm_memory_slot *slot,gfn_t gfn);bool kvm_slot_gmem_is_guest_mappable (struct kvm_memory_slot *slot,gfn_t gfn); +int kvm_slot_gmem_register_callback (struct kvm_memory_slot *slot,gfn_t gfn); +void kvm_gmem_handle_folio_put (struct folio *folio);#else static inline bool kvm_gmem_is_mappable (struct kvm *kvm,gfn_t gfn,gfn_t end) {
@@ -2615,6 +2617,15 @@ static inline bool kvm_slot_gmem_is_guest_mappable(struct kvm_memory_slot *slot,WARN_ON_ONCE (1 );return false; } +int kvm_slot_gmem_register_callback (struct kvm_memory_slot *slot,gfn_t gfn) +{ +WARN_ON_ONCE (1 ); +return -EINVAL ; +} +static inline void kvm_gmem_handle_folio_put (struct folio *folio) +{ +WARN_ON_ONCE (1 ); +}#endif /* CONFIG_KVM_GMEM_MAPPABLE */ #endif
include/linux/page-flags.h +7 -0
@@ -950,6 +950,7 @@ enum pagetype { PGTY_slab =0xf5 , PGTY_zsmalloc =0xf6 , PGTY_unaccepted =0xf7 , + PGTY_guestmem =0xf8 , PGTY_mapcount_underflow = 0xff };
@@ -1099,6 +1100,12 @@ FOLIO_TYPE_OPS(hugetlb, hugetlb) FOLIO_TEST_FLAG_FALSE(hugetlb)#endif +#ifdef CONFIG_KVM_GMEM_MAPPABLE +FOLIO_TYPE_OPS(guestmem, guestmem) +#else +FOLIO_TEST_FLAG_FALSE(guestmem) +#endif +PAGE_TYPE_OPS (Zsmalloc, zsmalloc, zsmalloc)/*
mm/debug.c +1 -0
@@ -56,6 +56,7 @@ static const char *page_type_names[] = {DEF_PAGETYPE_NAME (table),DEF_PAGETYPE_NAME (buddy),DEF_PAGETYPE_NAME (unaccepted), + DEF_PAGETYPE_NAME(guestmem), };static const char *page_type_name (unsigned int page_type)
mm/swap.c +4 -0
@@ -37,6 +37,7 @@#include <linux/page_idle.h> #include <linux/local_lock.h> #include <linux/buffer_head.h> +#include <linux/kvm_host.h> #include "internal.h"
@@ -103,6 +104,9 @@ static void free_typed_folio(struct folio *folio)case PGTY_offline:/* Nothing to do, it's offline. */ return ; +case PGTY_guestmem: +kvm_gmem_handle_folio_put (folio); +return ;default :WARN_ON_ONCE (1 ); }
virt/kvm/guest_memfd.c +145 -0
164 lines not on this page · raw message
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
kernel test robot
build botRe: [RFC PATCH v4 06/14] KVM: guest_memfd: Handle final folio_put() of guestmem pages
kernel test robot noticed the following build warnings:
Hi Fuad,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:
[auto build test WARNING on fac04efc5c793dccbd07e2d59af9f90b7fc0dca4]
url: https://github.com/intel-lab-lkp/linux/commits/Fuad-Tabba/mm-Consolidate-freeing-of-typed-folios-on-final-folio_put/20241214-005109 base: fac04efc5c793dccbd07e2d59af9f90b7fc0dca4 patch link: https://lore.kernel.org/r/20241213164811.2006197-7-tabba%40google.com patch subject: [RFC PATCH v4 06/14] KVM: guest_memfd: Handle final folio_put() of guestmem pages config: i386-buildonly-randconfig-006-20241215 (https://download.01.org/0day-ci/archive/20241215/[email protected]/config) compiler: clang version 19.1.3 (https://github.com/llvm/llvm-project ab51eccf88f5321e7c60591c5546b254b6afab99) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241215/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
In file included from arch/x86/events/intel/core.c:17:
In file included from include/linux/kvm_host.h:16:
In file included from include/linux/mm.h:2223:
include/linux/vmstat.h:518:36: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]
518 | return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
| ~~~~~~~~~~~ ^ ~~~
In file included from arch/x86/events/intel/core.c:17:
>> include/linux/kvm_host.h:2620:5: warning: no previous prototype for function 'kvm_slot_gmem_register_callback' [-Wmissing-prototypes]
2620 | int kvm_slot_gmem_register_callback(struct kvm_memory_slot *slot, gfn_t gfn)
| ^
include/linux/kvm_host.h:2620:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
2620 | int kvm_slot_gmem_register_callback(struct kvm_memory_slot *slot, gfn_t gfn)
| ^
| static 2 warnings generated.
vim +/kvm_slot_gmem_register_callback +2620 include/linux/kvm_host.h
2566
2567 #ifdef CONFIG_KVM_GMEM_MAPPABLE
2568 bool kvm_gmem_is_mappable(struct kvm *kvm, gfn_t gfn, gfn_t end);
2569 int kvm_gmem_set_mappable(struct kvm *kvm, gfn_t start, gfn_t end);
2570 int kvm_gmem_clear_mappable(struct kvm *kvm, gfn_t start, gfn_t end);
2571 int kvm_slot_gmem_set_mappable(struct kvm_memory_slot *slot, gfn_t start,
2572 gfn_t end);
2573 int kvm_slot_gmem_clear_mappable(struct kvm_memory_slot *slot, gfn_t start,
2574 gfn_t end);
2575 bool kvm_slot_gmem_is_mappable(struct kvm_memory_slot *slot, gfn_t gfn);
2576 bool kvm_slot_gmem_is_guest_mappable(struct kvm_memory_slot *slot, gfn_t gfn);
2577 int kvm_slot_gmem_register_callback(struct kvm_memory_slot *slot, gfn_t gfn);
2578 void kvm_gmem_handle_folio_put(struct folio *folio);
2579 #else
2580 static inline bool kvm_gmem_is_mappable(struct kvm *kvm, gfn_t gfn, gfn_t end)
2581 {
2582 WARN_ON_ONCE(1);
2583 return false;
2584 }
2585 static inline int kvm_gmem_set_mappable(struct kvm *kvm, gfn_t start, gfn_t end)
2586 {
2587 WARN_ON_ONCE(1);
2588 return -EINVAL;
2589 }
2590 static inline int kvm_gmem_clear_mappable(struct kvm *kvm, gfn_t start,
2591 gfn_t end)
2592 {
2593 WARN_ON_ONCE(1);
2594 return -EINVAL;
2595 }
2596 static inline int kvm_slot_gmem_set_mappable(struct kvm_memory_slot *slot,
2597 gfn_t start, gfn_t end)
2598 {
2599 WARN_ON_ONCE(1);
2600 return -EINVAL;
2601 }
2602 static inline int kvm_slot_gmem_clear_mappable(struct kvm_memory_slot *slot,
2603 gfn_t start, gfn_t end)
2604 {
2605 WARN_ON_ONCE(1);
2606 return -EINVAL;
2607 }
2608 static inline bool kvm_slot_gmem_is_mappable(struct kvm_memory_slot *slot,
2609 gfn_t gfn)
2610 {
2611 WARN_ON_ONCE(1);
2612 return false;
2613 }
2614 static inline bool kvm_slot_gmem_is_guest_mappable(struct kvm_memory_slot *slot,
2615 gfn_t gfn)
2616 {
2617 WARN_ON_ONCE(1);
2618 return false;
2619 }
> 2620 int kvm_slot_gmem_register_callback(struct kvm_memory_slot *slot, gfn_t gfn)
2621 {
2622 WARN_ON_ONCE(1);
2623 return -EINVAL;
2624 }
2625 static inline void kvm_gmem_handle_folio_put(struct folio *folio)
2626 {
2627 WARN_ON_ONCE(1);
2628 }
2629 #endif /* CONFIG_KVM_GMEM_MAPPABLE */
2630
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [RFC PATCH v4 06/14] KVM: guest_memfd: Handle final folio_put() of guestmem pages
In reply to Fuad Tabbakernel test robot noticed the following build errors:
Hi Fuad,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:
[auto build test ERROR on fac04efc5c793dccbd07e2d59af9f90b7fc0dca4]
url: https://github.com/intel-lab-lkp/linux/commits/Fuad-Tabba/mm-Consolidate-freeing-of-typed-folios-on-final-folio_put/20241214-005109 base: fac04efc5c793dccbd07e2d59af9f90b7fc0dca4 patch link: https://lore.kernel.org/r/20241213164811.2006197-7-tabba%40google.com patch subject: [RFC PATCH v4 06/14] KVM: guest_memfd: Handle final folio_put() of guestmem pages config: i386-buildonly-randconfig-005-20241215 (https://download.01.org/0day-ci/archive/20241215/[email protected]/config) compiler: gcc-12 (Debian 12.2.0-14) 12.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241215/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
ld: mm/swap.o: in function `kvm_slot_gmem_register_callback':
>> include/linux/kvm_host.h:2621: multiple definition of `kvm_slot_gmem_register_callback'; arch/x86/events/intel/core.o:include/linux/kvm_host.h:2621: first defined here
vim +2621 include/linux/kvm_host.h
2566
2567 #ifdef CONFIG_KVM_GMEM_MAPPABLE
2568 bool kvm_gmem_is_mappable(struct kvm *kvm, gfn_t gfn, gfn_t end);
2569 int kvm_gmem_set_mappable(struct kvm *kvm, gfn_t start, gfn_t end);
2570 int kvm_gmem_clear_mappable(struct kvm *kvm, gfn_t start, gfn_t end);
2571 int kvm_slot_gmem_set_mappable(struct kvm_memory_slot *slot, gfn_t start,
2572 gfn_t end);
2573 int kvm_slot_gmem_clear_mappable(struct kvm_memory_slot *slot, gfn_t start,
2574 gfn_t end);
2575 bool kvm_slot_gmem_is_mappable(struct kvm_memory_slot *slot, gfn_t gfn);
2576 bool kvm_slot_gmem_is_guest_mappable(struct kvm_memory_slot *slot, gfn_t gfn);
2577 int kvm_slot_gmem_register_callback(struct kvm_memory_slot *slot, gfn_t gfn);
2578 void kvm_gmem_handle_folio_put(struct folio *folio);
2579 #else
2580 static inline bool kvm_gmem_is_mappable(struct kvm *kvm, gfn_t gfn, gfn_t end)
2581 {
2582 WARN_ON_ONCE(1);
2583 return false;
2584 }
2585 static inline int kvm_gmem_set_mappable(struct kvm *kvm, gfn_t start, gfn_t end)
2586 {
2587 WARN_ON_ONCE(1);
2588 return -EINVAL;
2589 }
2590 static inline int kvm_gmem_clear_mappable(struct kvm *kvm, gfn_t start,
2591 gfn_t end)
2592 {
2593 WARN_ON_ONCE(1);
2594 return -EINVAL;
2595 }
2596 static inline int kvm_slot_gmem_set_mappable(struct kvm_memory_slot *slot,
2597 gfn_t start, gfn_t end)
2598 {
2599 WARN_ON_ONCE(1);
2600 return -EINVAL;
2601 }
2602 static inline int kvm_slot_gmem_clear_mappable(struct kvm_memory_slot *slot,
2603 gfn_t start, gfn_t end)
2604 {
2605 WARN_ON_ONCE(1);
2606 return -EINVAL;
2607 }
2608 static inline bool kvm_slot_gmem_is_mappable(struct kvm_memory_slot *slot,
2609 gfn_t gfn)
2610 {
2611 WARN_ON_ONCE(1);
2612 return false;
2613 }
2614 static inline bool kvm_slot_gmem_is_guest_mappable(struct kvm_memory_slot *slot,
2615 gfn_t gfn)
2616 {
2617 WARN_ON_ONCE(1);
2618 return false;
2619 }
2620 int kvm_slot_gmem_register_callback(struct kvm_memory_slot *slot, gfn_t gfn)
> 2621 { 2622 WARN_ON_ONCE(1);
2623 return -EINVAL;
2624 }
2625 static inline void kvm_gmem_handle_folio_put(struct folio *folio)
2626 {
2627 WARN_ON_ONCE(1);
2628 }
2629 #endif /* CONFIG_KVM_GMEM_MAPPABLE */
2630
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
RFC PATCH v4 7/14 KVM: guest_memfd: Allow host to mmap guest_memfd() pages when shared
In reply to Fuad TabbaAdd support for mmap() and fault() for guest_memfd in the host. The ability to fault in a guest page is contingent on that page being shared with the host.
The guest_memfd PRIVATE memory attribute is not used for two reasons. First because it reflects the userspace expectation for that memory location, and therefore can be toggled by userspace. The second is, although each guest_memfd file has a 1:1 binding with a KVM instance, the plan is to allow multiple files per inode, e.g. to allow intra-host migration to a new KVM instance, without destroying guest_memfd.
The mapping is restricted to only memory explicitly shared with the host. KVM checks that the host doesn't have any mappings for private memory via the folio's refcount. To avoid races between paths that check mappability and paths that check whether the host has any mappings (via the refcount), the folio lock is held in while either check is being performed.
This new feature is gated with a new configuration option, CONFIG_KVM_GMEM_MAPPABLE.
---
The functions kvm_gmem_is_mapped(), kvm_gmem_set_mappable(), and int kvm_gmem_clear_mappable() are not used in this patch series. They are intended to be used in future patches [*], which check and toggle mapability when the guest shares/unshares pages with the host.
[*] https://android-kvm.googlesource.com/linux/+/refs/heads/tabba/guestmem-6.13-v4-pkvm
--- virt/kvm/Kconfig | 4 ++ virt/kvm/guest_memfd.c | 87 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+)
virt/kvm/Kconfig +4 -0
@@ -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_MAPPABLE + select KVM_PRIVATE_MEM + bool
virt/kvm/guest_memfd.c +87 -0
102 lines not on this page · raw message
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Re: [RFC PATCH v4 07/14] KVM: guest_memfd: Allow host to mmap guest_memfd() pages when shared
Quoted 28 lines · Fuad Tabba
Add support for
mmap()andfault()forguest_memfdin the host. The ability to fault in a guest page is contingent on that page being shared with the host.The
guest_memfdPRIVATE memory attribute is not used for two reasons. First because it reflects the userspace expectation for that memory location, and therefore can be toggled by userspace. The second is, although eachguest_memfdfile has a 1:1 binding with a KVM instance, the plan is to allow multiple files per inode, e.g. to allow intra-host migration to a new KVM instance, without destroyingguest_memfd.The mapping is restricted to only memory explicitly shared with the host. KVM checks that the host doesn't have any mappings for private memory via the folio's refcount. To avoid races between paths that check mappability and paths that check whether the host has any mappings (via the refcount), the folio lock is held in while either check is being performed.
This new feature is gated with a new configuration option,
CONFIG_KVM_GMEM_MAPPABLE.Co-developed-by Ackerley Tng <[email protected]>Signed-off-by Ackerley Tng <[email protected]>Co-developed-by Elliot Berman <[email protected]>Signed-off-by Elliot Berman <[email protected]>Signed-off-by Fuad Tabba <[email protected]>
Quoted 8 lines
---
The functionskvm_gmem_is_mapped(),kvm_gmem_set_mappable(), and intkvm_gmem_clear_mappable()are not used in this patch series. They are intended to be used in future patches [*], which check and toggle mapability when the guest shares/unshares pages with the host.[*] https://android-kvm.googlesource.com/linux/+/refs/heads/tabba/guestmem-6.13-v4-pkvm
This one requires access, can you please push it somewhere public? I am interested in in-place shared<->private memory conversion and I wonder if kvm_gmem_set_mappable() that guy. Thanks,
Signature · 1 line
Alexey
Re: [RFC PATCH v4 07/14] KVM: guest_memfd: Allow host to mmap guest_memfd() pages when shared
Hi Alexey,
Quoted 37 lines · Alexey Kardashevskiy
Quoted 35 lines · Fuad Tabba
Add support for
mmap()andfault()forguest_memfdin the host. The ability to fault in a guest page is contingent on that page being shared with the host.The
guest_memfdPRIVATE memory attribute is not used for two reasons. First because it reflects the userspace expectation for that memory location, and therefore can be toggled by userspace. The second is, although eachguest_memfdfile has a 1:1 binding with a KVM instance, the plan is to allow multiple files per inode, e.g. to allow intra-host migration to a new KVM instance, without destroyingguest_memfd.The mapping is restricted to only memory explicitly shared with the host. KVM checks that the host doesn't have any mappings for private memory via the folio's refcount. To avoid races between paths that check mappability and paths that check whether the host has any mappings (via the refcount), the folio lock is held in while either check is being performed.
This new feature is gated with a new configuration option,
CONFIG_KVM_GMEM_MAPPABLE.Co-developed-by Ackerley Tng <[email protected]>Signed-off-by Ackerley Tng <[email protected]>Co-developed-by Elliot Berman <[email protected]>Signed-off-by Elliot Berman <[email protected]>Signed-off-by Fuad Tabba <[email protected]>---
The functionskvm_gmem_is_mapped(),kvm_gmem_set_mappable(), and intkvm_gmem_clear_mappable()are not used in this patch series. They are intended to be used in future patches [*], which check and toggle mapability when the guest shares/unshares pages with the host.
Quoted 5 lines
Quoted 1 line
[*] https://android-kvm.googlesource.com/linux/+/refs/heads/tabba/guestmem-6.13-v4-pkvm
This one requires access, can you please push it somewhere public? I am interested in in-place shared<->private memory conversion and I wonder if
kvm_gmem_set_mappable()that guy. Thanks,
Sorry for the late reply, I was away, and sorry for the broken link, I'd forgotten to push. Could you try now?
https://android-kvm.googlesource.com/linux/+/refs/heads/tabba/guestmem-6.13-v4-pkvm
Thanks,
/fuad
Quoted 3 lines
--
Alexey
RFC PATCH v4 8/14 KVM: guest_memfd: Add guest_memfd support to kvm_(read|/write)_guest_page()
In reply to Fuad TabbaMake 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.
--- virt/kvm/kvm_main.c | 133 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 114 insertions(+), 19 deletions(-)
virt/kvm/kvm_main.c +114 -19
@@ -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 ; }
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
RFC PATCH v4 9/14 KVM: guest_memfd: Add KVM capability to check if guest_memfd is host mappable
In reply to Fuad TabbaAdd the KVM capability KVM_CAP_GUEST_MEMFD_MAPPABLE, which is true if mapping guest memory is supported by the host.
--- include/uapi/linux/kvm.h | 1 + virt/kvm/kvm_main.c | 4 ++++ 2 files changed, 5 insertions(+)
include/uapi/linux/kvm.h +1 -0
@@ -933,6 +933,7 @@ struct kvm_enable_cap {#define KVM_CAP_PRE_FAULT_MEMORY 236#define KVM_CAP_X86_APIC_BUS_CYCLES_NS 237#define KVM_CAP_X86_GUEST_MODE 238 +#define KVM_CAP_GUEST_MEMFD_MAPPABLE 239struct kvm_irq_routing_irqchip {__u32 irqchip;
virt/kvm/kvm_main.c +4 -0
@@ -4979,6 +4979,10 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)#ifdef CONFIG_KVM_PRIVATE_MEM case KVM_CAP_GUEST_MEMFD :return !kvm ||kvm_arch_has_private_mem (kvm); +#endif +#ifdef CONFIG_KVM_GMEM_MAPPABLE +case KVM_CAP_GUEST_MEMFD_MAPPABLE : +return !kvm ||kvm_arch_has_private_mem (kvm);#endif default :break ;
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
RFC PATCH v4 10/14 KVM: guest_memfd: Add a guest_memfd() flag to initialize it as mappable
In reply to Fuad TabbaNot all use cases require guest_memfd() to be mappable by the host when first created. Add a new flag, GUEST_MEMFD_FLAG_INIT_MAPPABLE, which when set on KVM_CREATE_GUEST_MEMFD initializes the memory as mappable by the host. Otherwise, memory is private until shared by the guest with the host.
--- Documentation/virt/kvm/api.rst | 4 ++++ include/uapi/linux/kvm.h | 1 + tools/testing/selftests/kvm/guest_memfd_test.c | 7 +++++-- virt/kvm/guest_memfd.c | 6 +++++- 4 files changed, 15 insertions(+), 3 deletions(-)
Documentation/virt/kvm/api.rst +4 -0
@@ -6380,6 +6380,10 @@ most one mapping per page, i.e. binding multiple memory regions to a single guest_memfd range is not allowed (any number of memory regions can be bound to a single guest_memfd file, but the bound ranges must not overlap). +If the capability KVM_CAP_GUEST_MEMFD_MAPPABLE is supported, then the flags +field supports GUEST_MEMFD_FLAG_INIT_MAPPABLE, which initializes the memory +as mappable by the host. + See KVM_SET_USER_MEMORY_REGION2 for additional details. 4.143 KVM_PRE_FAULT_MEMORY
include/uapi/linux/kvm.h +1 -0
@@ -1566,6 +1566,7 @@ struct kvm_memory_attributes {#define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3)#define KVM_CREATE_GUEST_MEMFD _IOWR(KVMIO, 0xd4, struct kvm_create_guest_memfd) +#define GUEST_MEMFD_FLAG_INIT_MAPPABLE (1UL << 0)struct kvm_create_guest_memfd {__u64 size;
tools/testing/selftests/kvm/guest_memfd_test.c +5 -2
@@ -123,7 +123,7 @@ static void test_invalid_punch_hole(int fd, size_t page_size, size_t total_size)static void test_create_guest_memfd_invalid (struct kvm_vm *vm) {size_t page_size =getpagesize (); -uint64_t flag; +uint64_t flag =BIT (0 );size_t size;int fd;
@@ -134,7 +134,10 @@ static void test_create_guest_memfd_invalid(struct kvm_vm *vm) size); } -for (flag =BIT (0 ); flag; flag <<=1 ) { +if (kvm_has_cap (KVM_CAP_GUEST_MEMFD_MAPPABLE )) + flag =GUEST_MEMFD_FLAG_INIT_MAPPABLE <<1 ; + +for (; flag; flag <<=1 ) { fd =__vm_create_guest_memfd (vm, page_size, flag); TEST_ASSERT(fd == -1 && errno == EINVAL, "guest_memfd() with flag '0x%lx' should fail with EINVAL",
virt/kvm/guest_memfd.c +5 -1
@@ -939,7 +939,8 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) goto err_gmem; } -if (IS_ENABLED (CONFIG_KVM_GMEM_MAPPABLE )) { +if (IS_ENABLED (CONFIG_KVM_GMEM_MAPPABLE ) && + (flags &GUEST_MEMFD_FLAG_INIT_MAPPABLE )) { err =gmem_set_mappable (file_inode (file),0 , size >>PAGE_SHIFT );if (err) {fput (file);
@@ -968,6 +969,9 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args)u64 flags = args->flags;u64 valid_flags =0 ; +if (IS_ENABLED (CONFIG_KVM_GMEM_MAPPABLE )) + valid_flags |=GUEST_MEMFD_FLAG_INIT_MAPPABLE ; +if (flags & ~valid_flags)return -EINVAL ;
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
RFC PATCH v4 11/14 KVM: guest_memfd: selftests: guest_memfd mmap() test when mapping is allowed
In reply to Fuad TabbaExpand the guest_memfd selftests to include testing mapping guest memory if the capability is supported, and that still checks that memory is not mappable if the capability isn't supported.
Also, build the guest_memfd selftest for aarch64.
--- tools/testing/selftests/kvm/Makefile | 1 + .../testing/selftests/kvm/guest_memfd_test.c | 57 +++++++++++++++++-- 2 files changed, 53 insertions(+), 5 deletions(-)
tools/testing/selftests/kvm/Makefile +1 -0
@@ -174,6 +174,7 @@ TEST_GEN_PROGS_aarch64 += coalesced_io_test TEST_GEN_PROGS_aarch64 += demand_paging_test TEST_GEN_PROGS_aarch64 += dirty_log_test TEST_GEN_PROGS_aarch64 += dirty_log_perf_test +TEST_GEN_PROGS_aarch64 += guest_memfd_test TEST_GEN_PROGS_aarch64 += guest_print_test TEST_GEN_PROGS_aarch64 += get-reg-list TEST_GEN_PROGS_aarch64 += kvm_create_max_vcpus
tools/testing/selftests/kvm/guest_memfd_test.c +52 -5
87 lines not on this page · raw message
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
RFC PATCH v4 12/14 KVM: arm64: Skip VMA checks for slots without userspace address
In reply to Fuad TabbaMemory slots backed by guest memory might be created with no intention of being mapped by the host. These are recognized by not having a userspace address in the memory slot.
VMA checks are neither possible nor necessary for this kind of slot, so skip them.
--- arch/arm64/kvm/mmu.c | 8 ++++++++ 1 file changed, 8 insertions(+)
arch/arm64/kvm/mmu.c +8 -0
@@ -988,6 +988,10 @@ static void stage2_unmap_memslot(struct kvm *kvm,phys_addr_t size =PAGE_SIZE * memslot->npages;hva_t reg_end = hva + size; +/* Host will not map this private memory without a userspace address. */ +if (kvm_slot_can_be_private (memslot) && !hva) +return ; +/* * A memory region could potentially cover multiple VMAs, and any holes * between them, so iterate over all of them to find out if we should
@@ -2133,6 +2137,10 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm, hva = new->userspace_addr; reg_end = hva + (new->npages <<PAGE_SHIFT ); +/* Host will not map this private memory without a userspace address. */ +if ((kvm_slot_can_be_private (new)) && !hva) +return 0 ; +mmap_read_lock (current->mm);/* * A memory region could potentially cover multiple VMAs, and any holes
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
RFC PATCH v4 13/14 KVM: arm64: Handle guest_memfd()-backed guest page faults
In reply to Fuad TabbaAdd arm64 support for resolving guest page faults on guest_memfd() backed memslots. This support is not contingent on pKVM, or other confidential computing support, and works in both VHE and nVHE modes.
Without confidential computing, this support is useful for testing and debugging. In the future, it might also be useful should a user want to use guest_memfd() for all code, whether it's for a protected guest or not.
For now, the fault granule is restricted to PAGE_SIZE.
--- arch/arm64/kvm/mmu.c | 111 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 109 insertions(+), 2 deletions(-)
arch/arm64/kvm/mmu.c +109 -2
@@ -1434,6 +1434,107 @@ static bool kvm_vma_mte_allowed(struct vm_area_struct *vma)return vma->vm_flags &VM_MTE_ALLOWED ; } +static int guest_memfd_abort (struct kvm_vcpu *vcpu,phys_addr_t fault_ipa, +struct kvm_memory_slot *memslot,bool fault_is_perm) +{ +struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache; +bool exec_fault =kvm_vcpu_trap_is_exec_fault (vcpu); +bool logging_active =memslot_is_logging (memslot); +struct kvm_pgtable *pgt = vcpu->arch.hw_mmu->pgt; +enum kvm_pgtable_prot prot =KVM_PGTABLE_PROT_R ; +bool write_fault =kvm_is_write_fault (vcpu); +struct mm_struct *mm = current->mm; +gfn_t gfn =gpa_to_gfn (fault_ipa); +struct kvm *kvm = vcpu->kvm; +struct page *page; +kvm_pfn_t pfn; +int ret; + +/* For now, guest_memfd() only supports PAGE_SIZE granules. */ +if (WARN_ON_ONCE (fault_is_perm && +kvm_vcpu_trap_get_perm_fault_granule (vcpu) !=PAGE_SIZE )) { +return -EFAULT ; + } + +VM_BUG_ON (write_fault && exec_fault); + +if (fault_is_perm && !write_fault && !exec_fault) { +kvm_err ("Unexpected L2 read permission error\n" ); +return -EFAULT ; + } + +/* +* Permission faults just need to update the existing leaf entry, +* and so normally don't require allocations from the memcache. The +* only exception to this is when dirty logging is enabled at runtime +* and a write fault needs to collapse a block entry into a table. +*/ +if (!fault_is_perm || (logging_active && write_fault)) { + ret =kvm_mmu_topup_memory_cache (memcache, +kvm_mmu_cache_min_pages (vcpu->arch.hw_mmu)); +if (ret) +return ret; + } + +/* +* Holds the folio lock until mapped in the guest and its refcount is +* stable, to avoid races with paths that check if the folio is mapped +* by the host. +*/ + ret =kvm_gmem_get_pfn_locked (kvm, memslot, gfn, &pfn, &page,NULL ); +if (ret) +return ret; + +if (!kvm_slot_gmem_is_guest_mappable (memslot, gfn)) { + ret = -EAGAIN ; + goto unlock_page; + } + +/* +* Once it's faulted in, a guest_memfd() page will stay in memory. +* Therefore, count it as locked. +*/ +if (!fault_is_perm) { + ret =account_locked_vm (mm,1 , true); +if (ret) + goto unlock_page; + } + +read_lock (&kvm->mmu_lock); +if (write_fault) + prot |=KVM_PGTABLE_PROT_W ; + +if (exec_fault) + prot |=KVM_PGTABLE_PROT_X ; + +if (cpus_have_final_cap (ARM64_HAS_CACHE_DIC )) + prot |=KVM_PGTABLE_PROT_X ; + +/* +* Under the premise of getting a FSC_PERM fault, we just need to relax +* permissions. +*/ +if (fault_is_perm) + ret =kvm_pgtable_stage2_relax_perms (pgt, fault_ipa, prot); +else + ret =kvm_pgtable_stage2_map (pgt, fault_ipa,PAGE_SIZE , +__pfn_to_phys (pfn), prot, + memcache, +KVM_PGTABLE_WALK_HANDLE_FAULT | +KVM_PGTABLE_WALK_SHARED ); + +kvm_release_faultin_page (kvm, page, !!ret, write_fault); +read_unlock (&kvm->mmu_lock); + +if (ret && !fault_is_perm) +account_locked_vm (mm,1 , false); +unlock_page: +unlock_page (page); +put_page (page); + +return ret != -EAGAIN ? ret :0 ; +} + static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, struct kvm_s2_trans *nested, struct kvm_memory_slot *memslot, unsigned long hva,
@@ -1900,8 +2001,14 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) goto out_unlock; } - ret =user_mem_abort (vcpu, fault_ipa, nested, memslot, hva, -esr_fsc_is_permission_fault (esr)); +if (kvm_slot_can_be_private (memslot)) { + ret =guest_memfd_abort (vcpu, fault_ipa, memslot, +esr_fsc_is_permission_fault (esr)); + }else { + ret =user_mem_abort (vcpu, fault_ipa, nested, memslot, hva, +esr_fsc_is_permission_fault (esr)); + } +if (ret ==0 ) ret =1 ; out:
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Re: [RFC PATCH v4 13/14] KVM: arm64: Handle guest_memfd()-backed guest page faults
Quoted 112 lines · Fuad Tabba
Add arm64 support for resolving guest page faults on
guest_memfd()backed memslots. This support is not contingent on pKVM, or other confidential computing support, and works in both VHE and nVHE modes.Without confidential computing, this support is useful forQ testing and debugging. In the future, it might also be useful should a user want to use
guest_memfd()for all code, whether it's for a protected guest or not.For now, the fault granule is restricted to
PAGE_SIZE.Signed-off-by Fuad Tabba <[email protected]>--- arch/arm64/kvm/mmu.c | 111 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 109 insertions(+), 2 deletions(-)arch/arm64/kvm/mmu.c +87
-0@@ -1434,6 +1434,107 @@ static bool kvm_vma_mte_allowed(struct vm_area_struct *vma)return vma->vm_flags &VM_MTE_ALLOWED ; } +static int guest_memfd_abort (struct kvm_vcpu *vcpu,phys_addr_t fault_ipa, +struct kvm_memory_slot *memslot,bool fault_is_perm) +{ +struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache; +bool exec_fault =kvm_vcpu_trap_is_exec_fault (vcpu); +bool logging_active =memslot_is_logging (memslot); +struct kvm_pgtable *pgt = vcpu->arch.hw_mmu->pgt; +enum kvm_pgtable_prot prot =KVM_PGTABLE_PROT_R ; +bool write_fault =kvm_is_write_fault (vcpu); +struct mm_struct *mm = current->mm; +gfn_t gfn =gpa_to_gfn (fault_ipa); +struct kvm *kvm = vcpu->kvm; +struct page *page; +kvm_pfn_t pfn; +int ret; + +/* For now, guest_memfd() only supports PAGE_SIZE granules. */ +if (WARN_ON_ONCE (fault_is_perm && +kvm_vcpu_trap_get_perm_fault_granule (vcpu) !=PAGE_SIZE )) { +return -EFAULT ; + } + +VM_BUG_ON (write_fault && exec_fault); + +if (fault_is_perm && !write_fault && !exec_fault) { +kvm_err ("Unexpected L2 read permission error\n" ); +return -EFAULT ; + } + +/* +* Permission faults just need to update the existing leaf entry, +* and so normally don't require allocations from the memcache. The +* only exception to this is when dirty logging is enabled at runtime +* and a write fault needs to collapse a block entry into a table. +*/ +if (!fault_is_perm || (logging_active && write_fault)) { + ret =kvm_mmu_topup_memory_cache (memcache, +kvm_mmu_cache_min_pages (vcpu->arch.hw_mmu)); +if (ret) +return ret; + } + +/* +* Holds the folio lock until mapped in the guest and its refcount is +* stable, to avoid races with paths that check if the folio is mapped +* by the host. +*/ + ret =kvm_gmem_get_pfn_locked (kvm, memslot, gfn, &pfn, &page,NULL ); +if (ret) +return ret; + +if (!kvm_slot_gmem_is_guest_mappable (memslot, gfn)) { + ret = -EAGAIN ; + goto unlock_page; + } + +/* +* Once it's faulted in, a guest_memfd() page will stay in memory. +* Therefore, count it as locked. +*/ +if (!fault_is_perm) { + ret =account_locked_vm (mm,1 , true); +if (ret) + goto unlock_page; + } + +read_lock (&kvm->mmu_lock); +if (write_fault) + prot |=KVM_PGTABLE_PROT_W ; + +if (exec_fault) + prot |=KVM_PGTABLE_PROT_X ; + +if (cpus_have_final_cap (ARM64_HAS_CACHE_DIC )) + prot |=KVM_PGTABLE_PROT_X ; + +/* +* Under the premise of getting a FSC_PERM fault, we just need to relax +* permissions. +*/ +if (fault_is_perm) + ret =kvm_pgtable_stage2_relax_perms (pgt, fault_ipa, prot); +else + ret = kvm_pgtable_stage2_map(pgt, fault_ipa, PAGE_SIZE, + __pfn_to_phys(pfn), prot, + memcache, + KVM_PGTABLE_WALK_HANDLE_FAULT |
arch/arm64/kvm/mmu.c static bool kvm_vma_mte_allowed(struct vm_area_struct *vma) · line 1533
Quoted 10 lines
arch/arm64/kvm/mmu.c +10
-0+ KVM_PGTABLE_WALK_SHARED); + +kvm_release_faultin_page (kvm, page, !!ret, write_fault); +read_unlock (&kvm->mmu_lock); + +if (ret && !fault_is_perm) +account_locked_vm (mm,1 , false); +unlock_page: +unlock_page (page); +put_page (page);
There's a double-free of page here, as kvm_release_faultin_page already calls put_page. I fixed it up locally with
+ unlock_page(page);
kvm_release_faultin_page(kvm, page, !!ret, write_fault); read_unlock(&kvm->mmu_lock);
if (ret && !fault_is_perm)
account_locked_vm(mm, 1, false);
+ goto out;
+
unlock_page:
unlock_page(page);
put_page(page);
-
+out:
return ret != -EAGAIN ? ret : 0;
}
which I'm admittedly not sure is correct either because now the locks don't get released in reverse order of acquisition, but with this I was able to boot simple VMs.
Quoted 8 lines
arch/arm64/kvm/mmu.c +4
-0+ +return ret != -EAGAIN ? ret :0 ; +} + static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, struct kvm_s2_trans *nested, struct kvm_memory_slot *memslot, unsigned long hva,
arch/arm64/kvm/mmu.c int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) · line 2004
Quoted 6 lines
arch/arm64/kvm/mmu.c +1
-2@@ -1900,8 +2001,14 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) goto out_unlock; } - ret =user_mem_abort (vcpu, fault_ipa, nested, memslot, hva, -esr_fsc_is_permission_fault (esr)); +if (kvm_slot_can_be_private (memslot)) {
For my setup, I needed
if (kvm_mem_is_private(vcpu->kvm, gfn))
here instead, because I am making use of KVM_GENERIC_MEMORY_ATTRIBUTES, and had a memslot with the KVM_MEM_GUEST_MEMFD flag set, but whose gfn range wasn't actually set to KVM_MEMORY_ATTRIBUTE_PRIVATE.
If I'm reading patch 12 correctly, your memslots always set only one of userspace_addr or guest_memfd, and the stage 2 table setup simply checks which one is the case to decide what to fault in, so maybe to support both cases, this check should be
if (kvm_mem_is_private(vcpu->kvm, gfn) || (kvm_slot_can_be_private(memslot) && !memslot->userspace_addr)
?
Quoted 11 lines
arch/arm64/kvm/mmu.c +7
-1+ ret =guest_memfd_abort (vcpu, fault_ipa, memslot, +esr_fsc_is_permission_fault (esr)); + }else { + ret =user_mem_abort (vcpu, fault_ipa, nested, memslot, hva, +esr_fsc_is_permission_fault (esr)); + } +if (ret ==0 ) ret =1 ; out: --
Quoted 1 line
2.47.1.613.gc27f4b7a9f-goog
Best,
Patrick
Re: [RFC PATCH v4 13/14] KVM: arm64: Handle guest_memfd()-backed guest page faults
Hi Patrick,
Quoted 144 lines · Patrick Roy
Quoted 112 lines · Fuad Tabba
Add arm64 support for resolving guest page faults on
guest_memfd()backed memslots. This support is not contingent on pKVM, or other confidential computing support, and works in both VHE and nVHE modes.Without confidential computing, this support is useful forQ testing and debugging. In the future, it might also be useful should a user want to use
guest_memfd()for all code, whether it's for a protected guest or not.For now, the fault granule is restricted to
PAGE_SIZE.Signed-off-by Fuad Tabba <[email protected]>--- arch/arm64/kvm/mmu.c | 111 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 109 insertions(+), 2 deletions(-)arch/arm64/kvm/mmu.c +87
-0@@ -1434,6 +1434,107 @@ static bool kvm_vma_mte_allowed(struct vm_area_struct *vma)return vma->vm_flags &VM_MTE_ALLOWED ; } +static int guest_memfd_abort (struct kvm_vcpu *vcpu,phys_addr_t fault_ipa, +struct kvm_memory_slot *memslot,bool fault_is_perm) +{ +struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache; +bool exec_fault =kvm_vcpu_trap_is_exec_fault (vcpu); +bool logging_active =memslot_is_logging (memslot); +struct kvm_pgtable *pgt = vcpu->arch.hw_mmu->pgt; +enum kvm_pgtable_prot prot =KVM_PGTABLE_PROT_R ; +bool write_fault =kvm_is_write_fault (vcpu); +struct mm_struct *mm = current->mm; +gfn_t gfn =gpa_to_gfn (fault_ipa); +struct kvm *kvm = vcpu->kvm; +struct page *page; +kvm_pfn_t pfn; +int ret; + +/* For now, guest_memfd() only supports PAGE_SIZE granules. */ +if (WARN_ON_ONCE (fault_is_perm && +kvm_vcpu_trap_get_perm_fault_granule (vcpu) !=PAGE_SIZE )) { +return -EFAULT ; + } + +VM_BUG_ON (write_fault && exec_fault); + +if (fault_is_perm && !write_fault && !exec_fault) { +kvm_err ("Unexpected L2 read permission error\n" ); +return -EFAULT ; + } + +/* +* Permission faults just need to update the existing leaf entry, +* and so normally don't require allocations from the memcache. The +* only exception to this is when dirty logging is enabled at runtime +* and a write fault needs to collapse a block entry into a table. +*/ +if (!fault_is_perm || (logging_active && write_fault)) { + ret =kvm_mmu_topup_memory_cache (memcache, +kvm_mmu_cache_min_pages (vcpu->arch.hw_mmu)); +if (ret) +return ret; + } + +/* +* Holds the folio lock until mapped in the guest and its refcount is +* stable, to avoid races with paths that check if the folio is mapped +* by the host. +*/ + ret =kvm_gmem_get_pfn_locked (kvm, memslot, gfn, &pfn, &page,NULL ); +if (ret) +return ret; + +if (!kvm_slot_gmem_is_guest_mappable (memslot, gfn)) { + ret = -EAGAIN ; + goto unlock_page; + } + +/* +* Once it's faulted in, a guest_memfd() page will stay in memory. +* Therefore, count it as locked. +*/ +if (!fault_is_perm) { + ret =account_locked_vm (mm,1 , true); +if (ret) + goto unlock_page; + } + +read_lock (&kvm->mmu_lock); +if (write_fault) + prot |=KVM_PGTABLE_PROT_W ; + +if (exec_fault) + prot |=KVM_PGTABLE_PROT_X ; + +if (cpus_have_final_cap (ARM64_HAS_CACHE_DIC )) + prot |=KVM_PGTABLE_PROT_X ; + +/* +* Under the premise of getting a FSC_PERM fault, we just need to relax +* permissions. +*/ +if (fault_is_perm) + ret =kvm_pgtable_stage2_relax_perms (pgt, fault_ipa, prot); +else + ret = kvm_pgtable_stage2_map(pgt, fault_ipa, PAGE_SIZE, + __pfn_to_phys(pfn), prot, + memcache, + KVM_PGTABLE_WALK_HANDLE_FAULT |arch/arm64/kvm/mmu.c static bool kvm_vma_mte_allowed(struct vm_area_struct *vma) · line 1533
Quoted 10 lines
arch/arm64/kvm/mmu.c +10
-0+ KVM_PGTABLE_WALK_SHARED); + +kvm_release_faultin_page (kvm, page, !!ret, write_fault); +read_unlock (&kvm->mmu_lock); + +if (ret && !fault_is_perm) +account_locked_vm (mm,1 , false); +unlock_page: +unlock_page (page); +put_page (page);There's a double-free of
pagehere, askvm_release_faultin_pagealready callsput_page. I fixed it up locally with(quoted diff) +4
-1+ unlock_page(page); kvm_release_faultin_page(kvm, page, !!ret, write_fault); read_unlock(&kvm->mmu_lock); if (ret && !fault_is_perm) account_locked_vm(mm, 1, false); + goto out; + unlock_page: unlock_page(page); put_page(page); - +out: return ret != -EAGAIN ? ret : 0; }
Quoted 3 lines
which I'm admittedly not sure is correct either because now the locks don't get released in reverse order of acquisition, but with this I was able to boot simple VMs.
Thanks for that. You're right, I broke this code right before sending out the series while fixing a merge conflict. have prepared a new patch series (rebased on Linux 6.13-rc7), with this redone to be part of user_mem_abort(), as opposed to being in its own function. Makes the code cleaner more maintainable.
Quoted 23 lines
Quoted 8 lines
arch/arm64/kvm/mmu.c +4
-0+ +return ret != -EAGAIN ? ret :0 ; +} + static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, struct kvm_s2_trans *nested, struct kvm_memory_slot *memslot, unsigned long hva,arch/arm64/kvm/mmu.c int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) · line 2004
Quoted 6 lines
arch/arm64/kvm/mmu.c +1
-2@@ -1900,8 +2001,14 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) goto out_unlock; } - ret =user_mem_abort (vcpu, fault_ipa, nested, memslot, hva, -esr_fsc_is_permission_fault (esr)); +if (kvm_slot_can_be_private (memslot)) {For my setup, I needed
if (
kvm_mem_is_private(vcpu->kvm, gfn))here instead, because I am making use of
KVM_GENERIC_MEMORY_ATTRIBUTES, and had a memslot with theKVM_MEM_GUEST_MEMFDflag set, but whose gfn range wasn't actually set toKVM_MEMORY_ATTRIBUTE_PRIVATE.
Quoted 8 lines
If I'm reading patch 12 correctly, your memslots always set only one of
userspace_addrorguest_memfd, and the stage 2 table setup simply checks which one is the case to decide what to fault in, so maybe to support both cases, this check should beif (
kvm_mem_is_private(vcpu->kvm, gfn)|| (kvm_slot_can_be_private(memslot)&& !memslot->userspace_addr)?
I've actually missed supporting both cases, and I think your suggestion is the right way to do it. I'll fix it in the respin.
Cheers,
/fuad
Quoted 19 lines
[1]: https://lore.kernel.org/all/[email protected]/
Quoted 11 lines
arch/arm64/kvm/mmu.c +7
-1+ ret =guest_memfd_abort (vcpu, fault_ipa, memslot, +esr_fsc_is_permission_fault (esr)); + }else { + ret =user_mem_abort (vcpu, fault_ipa, nested, memslot, hva, +esr_fsc_is_permission_fault (esr)); + } +if (ret ==0 ) ret =1 ; out: --Quoted 1 line
2.47.1.613.gc27f4b7a9f-goog
Best,
Patrick
RFC PATCH v4 14/14 KVM: arm64: Enable guest_memfd private memory when pKVM is enabled
In reply to Fuad TabbaImplement kvm_arch_has_private_mem() in arm64 when pKVM is enabled, and make it dependent on the configuration option.
Also, now that the infrastructure is in place for arm64 to support guest private memory, enable it in the arm64 kernel configuration.
--- arch/arm64/include/asm/kvm_host.h | 3 +++ arch/arm64/kvm/Kconfig | 1 + 2 files changed, 4 insertions(+)
arch/arm64/include/asm/kvm_host.h +3 -0
@@ -1529,4 +1529,7 @@ void kvm_set_vm_id_reg(struct kvm *kvm, u32 reg, u64 val);#define kvm_has_s1poe (k) \ (kvm_has_feat((k), ID_AA64MMFR3_EL1, S1POE, IMP)) +#define kvm_arch_has_private_mem (kvm) \ + (IS_ENABLED(CONFIG_KVM_PRIVATE_MEM) && is_protected_kvm_enabled()) +#endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/Kconfig +1 -0
@@ -38,6 +38,7 @@ menuconfig KVM select HAVE_KVM_VCPU_RUN_PID_CHANGE select SCHED_INFO select GUEST_PERF_EVENTS if PERF_EVENTS + select KVM_GMEM_MAPPABLE help Support hosting virtualized guest machines.
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Hi,
As mentioned in the guest_memfd sync (2025-01-09), below is the state diagram that uses the new states in this patch series, and how they would interact with sharing/unsharing in pKVM:
This patch series doesn't necessarily impose all these transitions, many of them would be a matter of policy. This just happens to be the current way I've done it with pKVM/arm64.
Cheers,
/fuad
Quoted 68 lines · Fuad Tabba
This series adds restricted
mmap()support toguest_memfd, as well as support forguest_memfdon arm64. It is based on Linux 6.13-rc2. Please refer to v3 for the context [1].Main changes since v3:
- Added a new folio type for guestmem, used to register a callback when a folio's reference count reaches 0 (Matthew Wilcox, DavidH) [2]
- Introduce new mappability states for folios, where a folio can be mappable by the host and the guest, only the guest, or by no one (transient state)
- Rebased on Linux 6.13-rc2
- Refactoring and tidying upCheers,
/fuad[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/Ackerley Tng (2):
KVM:guest_memfd: Make guest mem use guest mem inodes instead of
anonymous inodes
KVM:guest_memfd: Track mappability within a structkvm_gmem_privateFuad Tabba (12):
mm: Consolidate freeing of typed folios on finalfolio_put()
KVM:guest_memfd: Introducekvm_gmem_get_pfn_locked(), which retains
the folio lock
KVM:guest_memfd: Folio mappability states and functions that manage
their transition
KVM:guest_memfd: Handle finalfolio_put()of guestmem pages
KVM:guest_memfd: Allow host to mmapguest_memfd()pages when shared
KVM:guest_memfd: Addguest_memfdsupport tokvm_(read|/write)_guest_page()KVM:
guest_memfd: Add KVM capability to check ifguest_memfdis host
mappable
KVM:guest_memfd: Add aguest_memfd()flag to initialize it as
mappable
KVM:guest_memfd: selftests:guest_memfdmmap()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: Enableguest_memfdprivate memory when pKVM is enabledDocumentation/virt/kvm/api.rst | 4 + arch/arm64/include/asm/kvm_host.h | 3 + arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/mmu.c | 119 +++- include/linux/kvm_host.h | 75 +++ include/linux/page-flags.h | 22 + include/uapi/linux/kvm.h | 2 + include/uapi/linux/magic.h | 1 + mm/debug.c | 1 + mm/swap.c | 28 +- tools/testing/selftests/kvm/Makefile | 1 + .../testing/selftests/kvm/guest_memfd_test.c | 64 +- virt/kvm/Kconfig | 4 + virt/kvm/guest_memfd.c | 579 +++++++++++++++++- virt/kvm/kvm_main.c | 229 ++++++- 15 files changed, 1074 insertions(+), 59 deletions(-)base-commit:
fac04efc5c793dccbd07e2d59af9f90b7fc0dca4--
2.47.1.613.gc27f4b7a9f-goog
Quoted 7 lines · Fuad Tabba
Hi,
As mentioned in the
guest_memfdsync (2025-01-09), below is the state diagram that uses the new states in this patch series, and how they would interact with sharing/unsharing in pKVM:
Thanks Fuad!
I took a look at the state diagram [1] and the branch that this patch is on [2], and here's what I understand about the flow:
1. From state H in the state diagram, the guest can request to unshare a page. When KVM handles this unsharing, KVM marks the folio mappability as NONE (state J).
2. The transition from state J to state K or I is independent of KVM - userspace has to do this unmapping
3. On the next vcpu_run() from userspace, continuing from userspace's handling of the unshare request, guest_memfd will check and try to register a callback if the folio's mappability is NONE. If the folio is mapped, or if folio is not mapped but refcount is elevated for whatever reason, vcpu_run() fails and exits to userspace. If folio is not mapped and gmem holds the last refcount, set folio mappability to GUEST.
Here's one issue I see based on the above understanding:
Registration of the folio_put() callback only happens if the VMM actually tries to do vcpu_run(). For 4K folios I think this is okay since the 4K folio can be freed via the transition state K -> state I, but for hugetlb folios that have been split for sharing with userspace, not getting a folio_put() callback means never putting the hugetlb folio together. Hence, relying on vcpu_run() to add the folio_put() callback leaves a way that hugetlb pages can be removed from the system.
I think we should try and find a path forward that works for both 4K and hugetlb folios.
IIUC page._mapcount and page.page_type works as a union because page_type is only set for page types that are never mapped to userspace, like PGTY_slab, PGTY_offline, etc.
Technically PGTY_guest_memfd is only set once the page can never be mapped to userspace, but PGTY_guest_memfd can only be set once mapcount reaches 0. Since mapcount is added in the faulting process, could gmem perhaps use some kind of .unmap/.unfault callback, so that gmem gets notified of all unmaps and will know for sure that the mapcount gets to 0?
Alternatively, I took a look at the folio_is_zone_device() implementation, and page.flags is used to identify the page's type. IIUC a ZONE_DEVICE page also falls in the intersection of needing a folio_put() callback and can be mapped to userspace. Could we use a similar approach, using page.flags to identify a page as a guest_memfd page? That way we don't need to know when unmapping happens, and will always be able to get a folio_put() callback.
[1] https://lpc.events/event/18/contributions/1758/attachments/1457/3699/Guestmemfd%20folio%20state%20page_type.pdf
[2] https://android-kvm.googlesource.com/linux/+/764360863785ba16d974253a572c87abdd9fdf0b%5E%21/#F0
Quoted 76 lines
This patch series doesn't necessarily impose all these transitions, many of them would be a matter of policy. This just happens to be the current way I've done it with pKVM/arm64.
Cheers,
/fuadQuoted 68 lines · Fuad Tabba
This series adds restricted
mmap()support toguest_memfd, as well as support forguest_memfdon arm64. It is based on Linux 6.13-rc2. Please refer to v3 for the context [1].Main changes since v3:
- Added a new folio type for guestmem, used to register a callback when a folio's reference count reaches 0 (Matthew Wilcox, DavidH) [2]
- Introduce new mappability states for folios, where a folio can be mappable by the host and the guest, only the guest, or by no one (transient state)
- Rebased on Linux 6.13-rc2
- Refactoring and tidying upCheers,
/fuad[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/Ackerley Tng (2):
KVM:guest_memfd: Make guest mem use guest mem inodes instead of
anonymous inodes
KVM:guest_memfd: Track mappability within a structkvm_gmem_privateFuad Tabba (12):
mm: Consolidate freeing of typed folios on finalfolio_put()
KVM:guest_memfd: Introducekvm_gmem_get_pfn_locked(), which retains
the folio lock
KVM:guest_memfd: Folio mappability states and functions that manage
their transition
KVM:guest_memfd: Handle finalfolio_put()of guestmem pages
KVM:guest_memfd: Allow host to mmapguest_memfd()pages when shared
KVM:guest_memfd: Addguest_memfdsupport tokvm_(read|/write)_guest_page()KVM:
guest_memfd: Add KVM capability to check ifguest_memfdis host
mappable
KVM:guest_memfd: Add aguest_memfd()flag to initialize it as
mappable
KVM:guest_memfd: selftests:guest_memfdmmap()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: Enableguest_memfdprivate memory when pKVM is enabledDocumentation/virt/kvm/api.rst | 4 + arch/arm64/include/asm/kvm_host.h | 3 + arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/mmu.c | 119 +++- include/linux/kvm_host.h | 75 +++ include/linux/page-flags.h | 22 + include/uapi/linux/kvm.h | 2 + include/uapi/linux/magic.h | 1 + mm/debug.c | 1 + mm/swap.c | 28 +- tools/testing/selftests/kvm/Makefile | 1 + .../testing/selftests/kvm/guest_memfd_test.c | 64 +- virt/kvm/Kconfig | 4 + virt/kvm/guest_memfd.c | 579 +++++++++++++++++- virt/kvm/kvm_main.c | 229 ++++++- 15 files changed, 1074 insertions(+), 59 deletions(-)base-commit:
fac04efc5c793dccbd07e2d59af9f90b7fc0dca4--
2.47.1.613.gc27f4b7a9f-goog
Hi Ackerley,
Quoted 39 lines · Ackerley Tng
Quoted 7 lines · Fuad Tabba
Hi,
As mentioned in the
guest_memfdsync (2025-01-09), below is the state diagram that uses the new states in this patch series, and how they would interact with sharing/unsharing in pKVM:Thanks Fuad!
I took a look at the state diagram [1] and the branch that this patch is on [2], and here's what I understand about the flow:
1. From state H in the state diagram, the guest can request to unshare a page. When KVM handles this unsharing, KVM marks the folio mappability as NONE (state J).
2. The transition from state J to state K or I is independent of KVM - userspace has to do this unmapping
3. On the nextvcpu_run()from userspace, continuing from userspace's handling of the unshare request,guest_memfdwill check and try to register a callback if the folio's mappability is NONE. If the folio is mapped, or if folio is not mapped but refcount is elevated for whatever reason,vcpu_run()fails and exits to userspace. If folio is not mapped and gmem holds the last refcount, set folio mappability to GUEST.Here's one issue I see based on the above understanding:
Registration of the
folio_put()callback only happens if the VMM actually tries to dovcpu_run(). For 4K folios I think this is okay since the 4K folio can be freed via the transition state K -> state I, but for hugetlb folios that have been split for sharing with userspace, not getting afolio_put()callback means never putting the hugetlb folio together. Hence, relying onvcpu_run()to add thefolio_put()callback leaves a way that hugetlb pages can be removed from the system.
Quoted 2 lines
I think we should try and find a path forward that works for both 4K and hugetlb folios.
I agree, this could be an issue, but we could find other ways to trigger the callback for huge folios. The important thing I was trying to get to is how to have the callback and be able to register it.
Quoted 3 lines
IIUC page._mapcount and page.page_type works as a union because
page_typeis only set for page types that are never mapped to userspace, likePGTY_slab,PGTY_offline, etc.
In the last guest_memfd sync, David Hildenbrand mentioned that that would be a temporary restriction since the two structures would eventually be decoupled, work being done by Matthew Wilcox I believe.
Quoted 6 lines
Technically
PGTY_guest_memfdis only set once the page can never be mapped to userspace, butPGTY_guest_memfdcan only be set once mapcount reaches 0. Since mapcount is added in the faulting process, could gmem perhaps use some kind of .unmap/.unfault callback, so that gmem gets notified of all unmaps and will know for sure that the mapcount gets to 0?
I'm not sure if there is such a callback. If there were, I'm not sure what that would buy us really. The main pain point is the refcount going down to zero. The mapcount part is pretty straightforard and likely to be only temporary as mentioned, i.e., when it get decoupled, we could register the callback earlier and simplify the transition altogether.
Quoted 7 lines
Alternatively, I took a look at the
folio_is_zone_device()implementation, and page.flags is used to identify the page's type. IIUC aZONE_DEVICEpage also falls in the intersection of needing afolio_put()callback and can be mapped to userspace. Could we use a similar approach, using page.flags to identify a page as aguest_memfdpage? That way we don't need to know when unmapping happens, and will always be able to get afolio_put()callback.
Same as above, with this being temporary, adding a new page flag might not be something that the rest of the community might be too excited about :)
Thanks for your comments!
/fuad
Quoted 79 lines
[1] https://lpc.events/event/18/contributions/1758/attachments/1457/3699/Guestmemfd%20folio%20state%20page_type.pdf
[2] https://android-kvm.googlesource.com/linux/+/764360863785ba16d974253a572c87abdd9fdf0b%5E%21/#F0Quoted 76 lines
This patch series doesn't necessarily impose all these transitions, many of them would be a matter of policy. This just happens to be the current way I've done it with pKVM/arm64.
Cheers,
/fuadQuoted 68 lines · Fuad Tabba
This series adds restricted
mmap()support toguest_memfd, as well as support forguest_memfdon arm64. It is based on Linux 6.13-rc2. Please refer to v3 for the context [1].Main changes since v3:
- Added a new folio type for guestmem, used to register a callback when a folio's reference count reaches 0 (Matthew Wilcox, DavidH) [2]
- Introduce new mappability states for folios, where a folio can be mappable by the host and the guest, only the guest, or by no one (transient state)
- Rebased on Linux 6.13-rc2
- Refactoring and tidying upCheers,
/fuad[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/Ackerley Tng (2):
KVM:guest_memfd: Make guest mem use guest mem inodes instead of
anonymous inodes
KVM:guest_memfd: Track mappability within a structkvm_gmem_privateFuad Tabba (12):
mm: Consolidate freeing of typed folios on finalfolio_put()
KVM:guest_memfd: Introducekvm_gmem_get_pfn_locked(), which retains
the folio lock
KVM:guest_memfd: Folio mappability states and functions that manage
their transition
KVM:guest_memfd: Handle finalfolio_put()of guestmem pages
KVM:guest_memfd: Allow host to mmapguest_memfd()pages when shared
KVM:guest_memfd: Addguest_memfdsupport tokvm_(read|/write)_guest_page()KVM:
guest_memfd: Add KVM capability to check ifguest_memfdis host
mappable
KVM:guest_memfd: Add aguest_memfd()flag to initialize it as
mappable
KVM:guest_memfd: selftests:guest_memfdmmap()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: Enableguest_memfdprivate memory when pKVM is enabledDocumentation/virt/kvm/api.rst | 4 + arch/arm64/include/asm/kvm_host.h | 3 + arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/mmu.c | 119 +++- include/linux/kvm_host.h | 75 +++ include/linux/page-flags.h | 22 + include/uapi/linux/kvm.h | 2 + include/uapi/linux/magic.h | 1 + mm/debug.c | 1 + mm/swap.c | 28 +- tools/testing/selftests/kvm/Makefile | 1 + .../testing/selftests/kvm/guest_memfd_test.c | 64 +- virt/kvm/Kconfig | 4 + virt/kvm/guest_memfd.c | 579 +++++++++++++++++- virt/kvm/kvm_main.c | 229 ++++++- 15 files changed, 1074 insertions(+), 59 deletions(-)base-commit:
fac04efc5c793dccbd07e2d59af9f90b7fc0dca4--
2.47.1.613.gc27f4b7a9f-goog
Quoted 19 lines · Fuad Tabba
Hi Ackerley,
Quoted 11 lines · Ackerley Tng
Registration of the
folio_put()callback only happens if the VMM actually tries to dovcpu_run(). For 4K folios I think this is okay since the 4K folio can be freed via the transition state K -> state I, but for hugetlb folios that have been split for sharing with userspace, not getting afolio_put()callback means never putting the hugetlb folio together. Hence, relying onvcpu_run()to add thefolio_put()callback leaves a way that hugetlb pages can be removed from the system.I think we should try and find a path forward that works for both 4K and hugetlb folios.
I agree, this could be an issue, but we could find other ways to trigger the callback for huge folios. The important thing I was trying to get to is how to have the callback and be able to register it.
Quoted 7 lines
Quoted 3 lines
IIUC page._mapcount and page.page_type works as a union because
page_typeis only set for page types that are never mapped to userspace, likePGTY_slab,PGTY_offline, etc.In the last
guest_memfdsync, David Hildenbrand mentioned that that would be a temporary restriction since the two structures would eventually be decoupled, work being done by Matthew Wilcox I believe.
Note the "temporary" might be few years still, it's a long-term project.
Quoted 22 lines
Quoted 6 lines
Technically
PGTY_guest_memfdis only set once the page can never be mapped to userspace, butPGTY_guest_memfdcan only be set once mapcount reaches 0. Since mapcount is added in the faulting process, could gmem perhaps use some kind of .unmap/.unfault callback, so that gmem gets notified of all unmaps and will know for sure that the mapcount gets to 0?I'm not sure if there is such a callback. If there were, I'm not sure what that would buy us really. The main pain point is the refcount going down to zero. The mapcount part is pretty straightforard and likely to be only temporary as mentioned, i.e., when it get decoupled, we could register the callback earlier and simplify the transition altogether.
Quoted 7 lines
Alternatively, I took a look at the
folio_is_zone_device()implementation, and page.flags is used to identify the page's type. IIUC aZONE_DEVICEpage also falls in the intersection of needing afolio_put()callback and can be mapped to userspace. Could we use a similar approach, using page.flags to identify a page as aguest_memfdpage? That way we don't need to know when unmapping happens, and will always be able to get afolio_put()callback.
Quoted 3 lines
Same as above, with this being temporary, adding a new page flag might not be something that the rest of the community might be too excited about :)
Yeah, adding a page flag is very difficult these days. Also while it's technically true that being a ZONE_DEVICE page is recorded in the page flags field, it's not really a separate flag - just that some bits of the flags field encode the zonenum. But zonenum is a number, not independent flags - i.e. there can be up to 8 zones, using up to 3 flag bits. And page's zonenum also has to match the zone's pfn range, so we couldn't just change the zone of a page to some hypothetical ZONE_MEMFD when it becomes used for memfd.
Quoted 82 lines
Thanks for your comments!
/fuadQuoted 79 lines
[1] https://lpc.events/event/18/contributions/1758/attachments/1457/3699/Guestmemfd%20folio%20state%20page_type.pdf
[2] https://android-kvm.googlesource.com/linux/+/764360863785ba16d974253a572c87abdd9fdf0b%5E%21/#F0Quoted 76 lines
This patch series doesn't necessarily impose all these transitions, many of them would be a matter of policy. This just happens to be the current way I've done it with pKVM/arm64.
Cheers,
/fuadQuoted 68 lines · Fuad Tabba
This series adds restricted
mmap()support toguest_memfd, as well as support forguest_memfdon arm64. It is based on Linux 6.13-rc2. Please refer to v3 for the context [1].Main changes since v3:
- Added a new folio type for guestmem, used to register a callback when a folio's reference count reaches 0 (Matthew Wilcox, DavidH) [2]
- Introduce new mappability states for folios, where a folio can be mappable by the host and the guest, only the guest, or by no one (transient state)
- Rebased on Linux 6.13-rc2
- Refactoring and tidying upCheers,
/fuad[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/Ackerley Tng (2):
KVM:guest_memfd: Make guest mem use guest mem inodes instead of
anonymous inodes
KVM:guest_memfd: Track mappability within a structkvm_gmem_privateFuad Tabba (12):
mm: Consolidate freeing of typed folios on finalfolio_put()
KVM:guest_memfd: Introducekvm_gmem_get_pfn_locked(), which retains
the folio lock
KVM:guest_memfd: Folio mappability states and functions that manage
their transition
KVM:guest_memfd: Handle finalfolio_put()of guestmem pages
KVM:guest_memfd: Allow host to mmapguest_memfd()pages when shared
KVM:guest_memfd: Addguest_memfdsupport tokvm_(read|/write)_guest_page()KVM:
guest_memfd: Add KVM capability to check ifguest_memfdis host
mappable
KVM:guest_memfd: Add aguest_memfd()flag to initialize it as
mappable
KVM:guest_memfd: selftests:guest_memfdmmap()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: Enableguest_memfdprivate memory when pKVM is enabledDocumentation/virt/kvm/api.rst | 4 + arch/arm64/include/asm/kvm_host.h | 3 + arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/mmu.c | 119 +++- include/linux/kvm_host.h | 75 +++ include/linux/page-flags.h | 22 + include/uapi/linux/kvm.h | 2 + include/uapi/linux/magic.h | 1 + mm/debug.c | 1 + mm/swap.c | 28 +- tools/testing/selftests/kvm/Makefile | 1 + .../testing/selftests/kvm/guest_memfd_test.c | 64 +- virt/kvm/Kconfig | 4 + virt/kvm/guest_memfd.c | 579 +++++++++++++++++- virt/kvm/kvm_main.c | 229 ++++++- 15 files changed, 1074 insertions(+), 59 deletions(-)base-commit:
fac04efc5c793dccbd07e2d59af9f90b7fc0dca4--
2.47.1.613.gc27f4b7a9f-goog
Quoted 24 lines · Vlastimil Babka
Quoted 23 lines · Fuad Tabba
Hi Ackerley,
Quoted 11 lines · Ackerley Tng
Registration of the
folio_put()callback only happens if the VMM actually tries to dovcpu_run(). For 4K folios I think this is okay since the 4K folio can be freed via the transition state K -> state I, but for hugetlb folios that have been split for sharing with userspace, not getting afolio_put()callback means never putting the hugetlb folio together. Hence, relying onvcpu_run()to add thefolio_put()callback leaves a way that hugetlb pages can be removed from the system.I think we should try and find a path forward that works for both 4K and hugetlb folios.
I agree, this could be an issue, but we could find other ways to trigger the callback for huge folios. The important thing I was trying to get to is how to have the callback and be able to register it.
Quoted 3 lines
IIUC page._mapcount and page.page_type works as a union because
page_typeis only set for page types that are never mapped to userspace, likePGTY_slab,PGTY_offline, etc.
Quoted 5 lines
Quoted 3 lines
In the last
guest_memfdsync, David Hildenbrand mentioned that that would be a temporary restriction since the two structures would eventually be decoupled, work being done by Matthew Wilcox I believe.Note the "temporary" might be few years still, it's a long-term project.
Right, nobody knows how long it will actually take. Willy thinks the part that would be required here might be feasible in the nearer future: "'d like to lay out some goals for the coming year. I think we can accomplish a big goal this year" [1]
[1] https://lore.kernel.org/all/[email protected]/T/#u
Signature · 3 lines
Cheers, David / dhildenb
Hi Fuad!
I finally got around to giving this patch series a spin for my non-CoCo usecase. I used the below diff to expose the functionality outside of pKVM (Based on Steven P.'s ARM CCA patch for custom VM types on ARM [2]). There's two small things that were broken for me (will post as responses to individual patches), but after fixing those, I was able to boot some guests using a modified Firecracker [1].
Just wondering, are you still looking into posting a separate series with just the MMU changes (e.g. something to have a bare-bones KVM_SW_PROTECTED_VM on ARM, like we do for x86), like you mentioned in the guest_memfd call before Christmas? We're pretty keen to get our hands something like that for our non-CoCo VMs (and ofc, am happy to help with any work required to get there :)
Best,
Patrick
[1]: https://github.com/roypat/firecracker/tree/secret-freedom-mmap
[2]: https://lore.kernel.org/kvm/[email protected]/
---
arch/arm64/include/asm/kvm_host.h +8 -2
@@ -380,6 +380,8 @@ struct kvm_arch {* the associated pKVM instance in the hypervisor. */ struct kvm_protected_vm pkvm; + +unsigned long type; };struct kvm_vcpu_fault_info {
@@ -1529,7 +1531,11 @@ void kvm_set_vm_id_reg(struct kvm *kvm, u32 reg, u64 val);#define kvm_has_s1poe (k) \ (kvm_has_feat((k), ID_AA64MMFR3_EL1, S1POE, IMP)) -#define kvm_arch_has_private_mem (kvm) \ - (IS_ENABLED(CONFIG_KVM_PRIVATE_MEM) && is_protected_kvm_enabled()) +#ifdef CONFIG_KVM_PRIVATE_MEM +#define kvm_arch_has_private_mem (kvm) \ + ((kvm)->arch.type == KVM_VM_TYPE_ARM_SW_PROTECTED || is_protected_kvm_enabled()) +#else +#define kvm_arch_has_private_mem (kvm) false +#endif #endif /* __ARM64_KVM_HOST_H__ */
arch/arm64/kvm/Kconfig +7 -0
@@ -38,6 +38,7 @@ menuconfig KVM select HAVE_KVM_VCPU_RUN_PID_CHANGE select SCHED_INFO select GUEST_PERF_EVENTS if PERF_EVENTS + select KVM_GENERIC_PRIVATE_MEM if KVM_SW_PROTECTED_VM select KVM_GMEM_MAPPABLE help Support hosting virtualized guest machines.
@@ -84,4 +85,10 @@ config PTDUMP_STAGE2_DEBUGFS If in doubt, say N. +config KVM_SW_PROTECTED_VM + bool "Enable support for KVM software-protected VMs" + depends on EXPERT + depends on KVM && ARM64 + select KVM_GENERIC_PRIVATE_MEM + endif # VIRTUALIZATION
arch/arm64/kvm/arm.c +13 -0
@@ -181,6 +181,19 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)mutex_unlock (&kvm->lock);#endif +if (type & ~(KVM_VM_TYPE_ARM_MASK |KVM_VM_TYPE_ARM_IPA_SIZE_MASK )) +return -EINVAL ; + +switch (type &KVM_VM_TYPE_ARM_MASK ) { +case KVM_VM_TYPE_ARM_NORMAL : +case KVM_VM_TYPE_ARM_SW_PROTECTED : +break ; +default : +return -EINVAL ; + } + + kvm->arch.type = type &KVM_VM_TYPE_ARM_MASK ; +kvm_init_nested (kvm); ret =kvm_share_hyp (kvm, kvm +1 );
arch/arm64/kvm/mmu.c +28 -3
@@ -869,9 +869,6 @@ static int kvm_init_ipa_range(struct kvm_s2_mmu *mmu, unsigned long type)u64 mmfr0, mmfr1;u32 phys_shift; -if (type & ~KVM_VM_TYPE_ARM_IPA_SIZE_MASK ) -return -EINVAL ; - phys_shift =KVM_VM_TYPE_ARM_IPA_SIZE (type);if (is_protected_kvm_enabled ()) { phys_shift = kvm_ipa_limit;
@@ -2373,3 +2370,31 @@ void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled)trace_kvm_toggle_cache (*vcpu_pc (vcpu), was_enabled, now_enabled); } + +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +bool kvm_arch_pre_set_memory_attributes (struct kvm *kvm, +struct kvm_gfn_range *range) +{ +/* +* Zap SPTEs even if the slot can't be mapped PRIVATE. KVM only +* supports KVM_MEMORY_ATTRIBUTE_PRIVATE, and so it *seems* like KVM +* can simply ignore such slots. But if userspace is making memory +* PRIVATE, then KVM must prevent the guest from accessing the memory +* as shared. And if userspace is making memory SHARED and this point +* is reached, then at least one page within the range was previously +* PRIVATE, i.e. the slot's possible hugepage ranges are changing. +* Zapping SPTEs in this case ensures KVM will reassess whether or not +* a hugepage can be used for affected ranges. +*/ +if (WARN_ON_ONCE (!kvm_arch_has_private_mem (kvm))) +return false; + +return kvm_unmap_gfn_range (kvm, range); +} + +bool kvm_arch_post_set_memory_attributes (struct kvm *kvm, +struct kvm_gfn_range *range) +{ +return false; +} +#endif
include/uapi/linux/kvm.h +7 -0
@@ -653,6 +653,13 @@ struct kvm_enable_cap {* PA size shift (i.e, log2(PA_Size)). For backward compatibility, * value 0 implies the default IPA size, 40bits. */ +#define KVM_VM_TYPE_ARM_SHIFT 8 +#define KVM_VM_TYPE_ARM_MASK (0xfULL << KVM_VM_TYPE_ARM_SHIFT) +#define KVM_VM_TYPE_ARM (_type) \ + (((_type) << KVM_VM_TYPE_ARM_SHIFT) & KVM_VM_TYPE_ARM_MASK) +#define KVM_VM_TYPE_ARM_NORMAL KVM_VM_TYPE_ARM(0) +#define KVM_VM_TYPE_ARM_SW_PROTECTED KVM_VM_TYPE_ARM(1) +#define KVM_VM_TYPE_ARM_IPA_SIZE_MASK 0xffULL#define KVM_VM_TYPE_ARM_IPA_SIZE (x) \ ((x) & KVM_VM_TYPE_ARM_IPA_SIZE_MASK)
Quoted 66 lines · Fuad Tabba
This series adds restricted
mmap()support toguest_memfd, as well as support forguest_memfdon arm64. It is based on Linux 6.13-rc2. Please refer to v3 for the context [1].Main changes since v3:
- Added a new folio type for guestmem, used to register a callback when a folio's reference count reaches 0 (Matthew Wilcox, DavidH) [2]
- Introduce new mappability states for folios, where a folio can be mappable by the host and the guest, only the guest, or by no one (transient state)
- Rebased on Linux 6.13-rc2
- Refactoring and tidying upCheers,
/fuad[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/Ackerley Tng (2):
KVM:guest_memfd: Make guest mem use guest mem inodes instead of
anonymous inodes
KVM:guest_memfd: Track mappability within a structkvm_gmem_privateFuad Tabba (12):
mm: Consolidate freeing of typed folios on finalfolio_put()
KVM:guest_memfd: Introducekvm_gmem_get_pfn_locked(), which retains
the folio lock
KVM:guest_memfd: Folio mappability states and functions that manage
their transition
KVM:guest_memfd: Handle finalfolio_put()of guestmem pages
KVM:guest_memfd: Allow host to mmapguest_memfd()pages when shared
KVM:guest_memfd: Addguest_memfdsupport tokvm_(read|/write)_guest_page()KVM:
guest_memfd: Add KVM capability to check ifguest_memfdis host
mappable
KVM:guest_memfd: Add aguest_memfd()flag to initialize it as
mappable
KVM:guest_memfd: selftests:guest_memfdmmap()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: Enableguest_memfdprivate memory when pKVM is enabledDocumentation/virt/kvm/api.rst | 4 + arch/arm64/include/asm/kvm_host.h | 3 + arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/mmu.c | 119 +++- include/linux/kvm_host.h | 75 +++ include/linux/page-flags.h | 22 + include/uapi/linux/kvm.h | 2 + include/uapi/linux/magic.h | 1 + mm/debug.c | 1 + mm/swap.c | 28 +- tools/testing/selftests/kvm/Makefile | 1 + .../testing/selftests/kvm/guest_memfd_test.c | 64 +- virt/kvm/Kconfig | 4 + virt/kvm/guest_memfd.c | 579 +++++++++++++++++- virt/kvm/kvm_main.c | 229 ++++++- 15 files changed, 1074 insertions(+), 59 deletions(-)base-commit:
fac04efc5c793dccbd07e2d59af9f90b7fc0dca4--
2.47.1.613.gc27f4b7a9f-goog
Hi Patrick,
Quoted 9 lines · Patrick Roy
Hi Fuad!
I finally got around to giving this patch series a spin for my non-CoCo usecase. I used the below diff to expose the functionality outside of pKVM (Based on Steven P.'s ARM CCA patch for custom VM types on ARM [2]). There's two small things that were broken for me (will post as responses to individual patches), but after fixing those, I was able to boot some guests using a modified Firecracker [1].
That's great, thanks for that, and for your comments on the patches.
Quoted 6 lines
Just wondering, are you still looking into posting a separate series with just the MMU changes (e.g. something to have a bare-bones
KVM_SW_PROTECTED_VMon ARM, like we do for x86), like you mentioned in theguest_memfdcall before Christmas? We're pretty keen to get our hands something like that for our non-CoCo VMs (and ofc, am happy to help with any work required to get there :)
Yes I am. I'm almost done with it now. That said, I need to make it work with attributes as well (as you mention in your comments on the other patch). I should to send it out next week, before the biweekly meeting in case we need to discuss it.
Cheers,
/fuad
Quoted 216 lines
Best,
Patrick[1]: https://github.com/roypat/firecracker/tree/secret-freedom-mmap
[2]: https://lore.kernel.org/kvm/[email protected]/---
arch/arm64/include/asm/kvm_host.h +8
-2@@ -380,6 +380,8 @@ struct kvm_arch {* the associated pKVM instance in the hypervisor. */ struct kvm_protected_vm pkvm; + +unsigned long type; };struct kvm_vcpu_fault_info {@@ -1529,7 +1531,11 @@ void kvm_set_vm_id_reg(struct kvm *kvm, u32 reg, u64 val);#define kvm_has_s1poe (k) \ (kvm_has_feat((k), ID_AA64MMFR3_EL1, S1POE, IMP)) -#define kvm_arch_has_private_mem (kvm) \ - (IS_ENABLED(CONFIG_KVM_PRIVATE_MEM) && is_protected_kvm_enabled()) +#ifdef CONFIG_KVM_PRIVATE_MEM +#define kvm_arch_has_private_mem (kvm) \ + ((kvm)->arch.type == KVM_VM_TYPE_ARM_SW_PROTECTED || is_protected_kvm_enabled()) +#else +#define kvm_arch_has_private_mem (kvm) false +#endif #endif /* __ARM64_KVM_HOST_H__ */ arch/arm64/kvm/Kconfig +7
-0@@ -38,6 +38,7 @@ menuconfig KVM select HAVE_KVM_VCPU_RUN_PID_CHANGE select SCHED_INFO select GUEST_PERF_EVENTS if PERF_EVENTS + select KVM_GENERIC_PRIVATE_MEM if KVM_SW_PROTECTED_VM select KVM_GMEM_MAPPABLE help Support hosting virtualized guest machines.@@ -84,4 +85,10 @@ config PTDUMP_STAGE2_DEBUGFS If in doubt, say N. +config KVM_SW_PROTECTED_VM + bool "Enable support for KVM software-protected VMs" + depends on EXPERT + depends on KVM && ARM64 + select KVM_GENERIC_PRIVATE_MEM + endif # VIRTUALIZATIONarch/arm64/kvm/arm.c +13
-0@@ -181,6 +181,19 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)mutex_unlock (&kvm->lock);#endif +if (type & ~(KVM_VM_TYPE_ARM_MASK |KVM_VM_TYPE_ARM_IPA_SIZE_MASK )) +return -EINVAL ; + +switch (type &KVM_VM_TYPE_ARM_MASK ) { +case KVM_VM_TYPE_ARM_NORMAL : +case KVM_VM_TYPE_ARM_SW_PROTECTED : +break ; +default : +return -EINVAL ; + } + + kvm->arch.type = type &KVM_VM_TYPE_ARM_MASK ; +kvm_init_nested (kvm); ret =kvm_share_hyp (kvm, kvm +1 );arch/arm64/kvm/mmu.c +28
-3@@ -869,9 +869,6 @@ static int kvm_init_ipa_range(struct kvm_s2_mmu *mmu, unsigned long type)u64 mmfr0, mmfr1;u32 phys_shift; -if (type & ~KVM_VM_TYPE_ARM_IPA_SIZE_MASK ) -return -EINVAL ; - phys_shift =KVM_VM_TYPE_ARM_IPA_SIZE (type);if (is_protected_kvm_enabled ()) { phys_shift = kvm_ipa_limit;@@ -2373,3 +2370,31 @@ void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled)trace_kvm_toggle_cache (*vcpu_pc (vcpu), was_enabled, now_enabled); } + +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +bool kvm_arch_pre_set_memory_attributes (struct kvm *kvm, +struct kvm_gfn_range *range) +{ +/* +* Zap SPTEs even if the slot can't be mapped PRIVATE. KVM only +* supports KVM_MEMORY_ATTRIBUTE_PRIVATE, and so it *seems* like KVM +* can simply ignore such slots. But if userspace is making memory +* PRIVATE, then KVM must prevent the guest from accessing the memory +* as shared. And if userspace is making memory SHARED and this point +* is reached, then at least one page within the range was previously +* PRIVATE, i.e. the slot's possible hugepage ranges are changing. +* Zapping SPTEs in this case ensures KVM will reassess whether or not +* a hugepage can be used for affected ranges. +*/ +if (WARN_ON_ONCE (!kvm_arch_has_private_mem (kvm))) +return false; + +return kvm_unmap_gfn_range (kvm, range); +} + +bool kvm_arch_post_set_memory_attributes (struct kvm *kvm, +struct kvm_gfn_range *range) +{ +return false; +} +#endif include/uapi/linux/kvm.h +7
-0@@ -653,6 +653,13 @@ struct kvm_enable_cap {* PA size shift (i.e, log2(PA_Size)). For backward compatibility, * value 0 implies the default IPA size, 40bits. */ +#define KVM_VM_TYPE_ARM_SHIFT 8 +#define KVM_VM_TYPE_ARM_MASK (0xfULL << KVM_VM_TYPE_ARM_SHIFT) +#define KVM_VM_TYPE_ARM (_type) \ + (((_type) << KVM_VM_TYPE_ARM_SHIFT) & KVM_VM_TYPE_ARM_MASK) +#define KVM_VM_TYPE_ARM_NORMAL KVM_VM_TYPE_ARM(0) +#define KVM_VM_TYPE_ARM_SW_PROTECTED KVM_VM_TYPE_ARM(1) +#define KVM_VM_TYPE_ARM_IPA_SIZE_MASK 0xffULL#define KVM_VM_TYPE_ARM_IPA_SIZE (x) \ ((x) & KVM_VM_TYPE_ARM_IPA_SIZE_MASK)Quoted 66 lines · Fuad Tabba
This series adds restricted
mmap()support toguest_memfd, as well as support forguest_memfdon arm64. It is based on Linux 6.13-rc2. Please refer to v3 for the context [1].Main changes since v3:
- Added a new folio type for guestmem, used to register a callback when a folio's reference count reaches 0 (Matthew Wilcox, DavidH) [2]
- Introduce new mappability states for folios, where a folio can be mappable by the host and the guest, only the guest, or by no one (transient state)
- Rebased on Linux 6.13-rc2
- Refactoring and tidying upCheers,
/fuad[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/Ackerley Tng (2):
KVM:guest_memfd: Make guest mem use guest mem inodes instead of
anonymous inodes
KVM:guest_memfd: Track mappability within a structkvm_gmem_privateFuad Tabba (12):
mm: Consolidate freeing of typed folios on finalfolio_put()
KVM:guest_memfd: Introducekvm_gmem_get_pfn_locked(), which retains
the folio lock
KVM:guest_memfd: Folio mappability states and functions that manage
their transition
KVM:guest_memfd: Handle finalfolio_put()of guestmem pages
KVM:guest_memfd: Allow host to mmapguest_memfd()pages when shared
KVM:guest_memfd: Addguest_memfdsupport tokvm_(read|/write)_guest_page()KVM:
guest_memfd: Add KVM capability to check ifguest_memfdis host
mappable
KVM:guest_memfd: Add aguest_memfd()flag to initialize it as
mappable
KVM:guest_memfd: selftests:guest_memfdmmap()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: Enableguest_memfdprivate memory when pKVM is enabledDocumentation/virt/kvm/api.rst | 4 + arch/arm64/include/asm/kvm_host.h | 3 + arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/mmu.c | 119 +++- include/linux/kvm_host.h | 75 +++ include/linux/page-flags.h | 22 + include/uapi/linux/kvm.h | 2 + include/uapi/linux/magic.h | 1 + mm/debug.c | 1 + mm/swap.c | 28 +- tools/testing/selftests/kvm/Makefile | 1 + .../testing/selftests/kvm/guest_memfd_test.c | 64 +- virt/kvm/Kconfig | 4 + virt/kvm/guest_memfd.c | 579 +++++++++++++++++- virt/kvm/kvm_main.c | 229 ++++++- 15 files changed, 1074 insertions(+), 59 deletions(-)base-commit:
fac04efc5c793dccbd07e2d59af9f90b7fc0dca4--
2.47.1.613.gc27f4b7a9f-goog