KVM: guest_memfd() and per-page attributes
Subsystems (20)
Sean Christopherson
maintainerv11 cover KVM: guest_memfd() and per-page attributes
This is the next iteration of implementing fd-based (instead of vma-based) memory for KVM guests. If you want the full background of why we are doing this, please go read the v10 cover letter[1].
The biggest change from v10 is to implement the backing storage in KVM itself, and expose it via a KVM ioctl() instead of a "generic" sycall. See link[2] for details on why we pivoted to a KVM-specific approach.
Key word is "biggest". Relative to v10, there are many big changes. Highlights below (I can't remember everything that got changed at this point).
Tagged RFC as there are a lot of empty changelogs, and a lot of missing documentation. And ideally, we'll have even more tests before merging. There are also several gaps/opens (to be discussed in tomorrow's PUCK).
v11:
- Test private<=>shared conversions *without* doing fallocate()
- PUNCH_HOLE all memory between iterations of the conversion test so that KVM doesn't retain pages in the guest_memfd
- Rename hugepage control to be a very generic ALLOW_HUGEPAGE, instead of giving it a THP or PMD specific name.
- Fold in fixes from a lot of people (thank you!)
- Zap SPTEs *before* updating attributes to ensure no weirdness, e.g. if KVM handles a page fault and looks at inconsistent attributes
- Refactor MMU interaction with attributes updates to reuse much of KVM's framework for mmu_notifiers.
[1] https://lore.kernel.org/all/20221202061347.1070246-1-chao.p.peng at linux.intel.com
[2] https://lore.kernel.org/all/ZEM5Zq8oo+xnApW9 at google.com
Ackerley Tng (1):
KVM: selftests: Test KVM exit behavior for private memory/access
Chao Peng (7):
KVM: Use gfn instead of hva for mmu_notifier_retry
KVM: Add KVM_EXIT_MEMORY_FAULT exit
KVM: Introduce per-page memory attributes
KVM: x86: Disallow hugepages when memory attributes are mixed
KVM: x86/mmu: Handle page fault for private memory
KVM: selftests: Add KVM_SET_USER_MEMORY_REGION2 helper
KVM: selftests: Expand set_memory_region_test to validate
guest_memfd()
Sean Christopherson (18):
KVM: Wrap kvm_gfn_range.pte in a per-action union
KVM: Tweak kvm_hva_range and hva_handler_t to allow reusing for gfn
ranges
KVM: PPC: Drop dead code related to KVM_ARCH_WANT_MMU_NOTIFIER
KVM: Convert KVM_ARCH_WANT_MMU_NOTIFIER to
CONFIG_KVM_GENERIC_MMU_NOTIFIER
KVM: Introduce KVM_SET_USER_MEMORY_REGION2
mm: Add AS_UNMOVABLE to mark mapping as completely unmovable
security: Export security_inode_init_security_anon() for use by KVM
KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing
memory
KVM: Add transparent hugepage support for dedicated guest memory
KVM: Drop superfluous __KVM_VCPU_MULTIPLE_ADDRESS_SPACE macro
KVM: Allow arch code to track number of memslot address spaces per VM
KVM: x86: Add support for "protected VMs" that can utilize private
memory
KVM: selftests: Drop unused kvm_userspace_memory_region_find() helper
KVM: selftests: Convert lib's mem regions to
KVM_SET_USER_MEMORY_REGION2
KVM: selftests: Add support for creating private memslots
KVM: selftests: Introduce VM "shape" to allow tests to specify the VM
type
KVM: selftests: Add GUEST_SYNC[1-6] macros for synchronizing more data
KVM: selftests: Add basic selftest for guest_memfd()
Vishal Annapurve (3):
KVM: selftests: Add helpers to convert guest memory b/w private and
shared
KVM: selftests: Add helpers to do KVM_HC_MAP_GPA_RANGE hypercalls
(x86)
KVM: selftests: Add x86-only selftest for private memory conversions
Documentation/virt/kvm/api.rst | 114 ++++ arch/arm64/include/asm/kvm_host.h | 2 - arch/arm64/kvm/Kconfig | 2 +- arch/arm64/kvm/mmu.c | 2 +- arch/mips/include/asm/kvm_host.h | 2 - arch/mips/kvm/Kconfig | 2 +- arch/mips/kvm/mmu.c | 2 +- arch/powerpc/include/asm/kvm_host.h | 2 - arch/powerpc/kvm/Kconfig | 8 +- arch/powerpc/kvm/book3s_hv.c | 2 +- arch/powerpc/kvm/powerpc.c | 5 +- arch/riscv/include/asm/kvm_host.h | 2 - arch/riscv/kvm/Kconfig | 2 +- arch/riscv/kvm/mmu.c | 2 +- arch/x86/include/asm/kvm_host.h | 17 +- arch/x86/include/uapi/asm/kvm.h | 3 + arch/x86/kvm/Kconfig | 14 +- arch/x86/kvm/debugfs.c | 2 +- arch/x86/kvm/mmu/mmu.c | 287 +++++++- arch/x86/kvm/mmu/mmu_internal.h | 4 + arch/x86/kvm/mmu/mmutrace.h | 1 + arch/x86/kvm/mmu/tdp_mmu.c | 8 +- arch/x86/kvm/vmx/vmx.c | 11 +- arch/x86/kvm/x86.c | 24 +- include/linux/kvm_host.h | 129 +++- include/linux/pagemap.h | 11 + include/uapi/linux/kvm.h | 50 ++ include/uapi/linux/magic.h | 1 + mm/compaction.c | 4 + mm/migrate.c | 2 + security/security.c | 1 + tools/testing/selftests/kvm/Makefile | 3 + tools/testing/selftests/kvm/dirty_log_test.c | 2 +- .../testing/selftests/kvm/guest_memfd_test.c | 114 ++++ .../selftests/kvm/include/kvm_util_base.h | 141 +++- .../testing/selftests/kvm/include/test_util.h | 5 + .../selftests/kvm/include/ucall_common.h | 12 + .../selftests/kvm/include/x86_64/processor.h | 15 + .../selftests/kvm/kvm_page_table_test.c | 2 +- tools/testing/selftests/kvm/lib/kvm_util.c | 230 ++++--- tools/testing/selftests/kvm/lib/memstress.c | 3 +- .../selftests/kvm/set_memory_region_test.c | 99 +++ .../kvm/x86_64/private_mem_conversions_test.c | 408 +++++++++++ .../kvm/x86_64/private_mem_kvm_exits_test.c | 115 ++++ .../kvm/x86_64/ucna_injection_test.c | 2 +- virt/kvm/Kconfig | 17 + virt/kvm/Makefile.kvm | 1 + virt/kvm/dirty_ring.c | 2 +- virt/kvm/guest_mem.c | 635 ++++++++++++++++++ virt/kvm/kvm_main.c | 384 +++++++++-- virt/kvm/kvm_mm.h | 38 ++ 51 files changed, 2700 insertions(+), 246 deletions(-) create mode 100644 tools/testing/selftests/kvm/guest_memfd_test.c create mode 100644 tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c create mode 100644 tools/testing/selftests/kvm/x86_64/private_mem_kvm_exits_test.c create mode 100644 virt/kvm/guest_mem.c
base-commit: fdf0eaf11452d72945af31804e2a1048ee1b574c
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Sean Christopherson
maintainerRFC PATCH v11 1/29 KVM: Wrap kvm_gfn_range.pte in a per-action union
--- arch/arm64/kvm/mmu.c | 2 +- arch/mips/kvm/mmu.c | 2 +- arch/riscv/kvm/mmu.c | 2 +- arch/x86/kvm/mmu/mmu.c | 2 +- arch/x86/kvm/mmu/tdp_mmu.c | 6 +++--- include/linux/kvm_host.h | 5 ++++- virt/kvm/kvm_main.c | 16 ++++++++++------ 7 files changed, 21 insertions(+), 14 deletions(-)
arch/arm64/kvm/mmu.c +1 -1
@@ -1721,7 +1721,7 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)bool kvm_set_spte_gfn (struct kvm *kvm,struct kvm_gfn_range *range) { -kvm_pfn_t pfn =pte_pfn (range->pte); +kvm_pfn_t pfn =pte_pfn (range->arg.pte);if (!kvm->arch.mmu.pgt)return false;
arch/mips/kvm/mmu.c +1 -1
@@ -447,7 +447,7 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)bool kvm_set_spte_gfn (struct kvm *kvm,struct kvm_gfn_range *range) {gpa_t gpa = range->start <<PAGE_SHIFT ; -pte_t hva_pte = range->pte; +pte_t hva_pte = range->arg.pte;pte_t *gpa_pte =kvm_mips_pte_for_gpa (kvm,NULL , gpa);pte_t old_pte;
arch/riscv/kvm/mmu.c +1 -1
@@ -559,7 +559,7 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)bool kvm_set_spte_gfn (struct kvm *kvm,struct kvm_gfn_range *range) {int ret; -kvm_pfn_t pfn =pte_pfn (range->pte); +kvm_pfn_t pfn =pte_pfn (range->arg.pte);if (!kvm->arch.pgd)return false;
arch/x86/kvm/mmu/mmu.c +1 -1
@@ -1588,7 +1588,7 @@ static __always_inline bool kvm_handle_gfn_range(struct kvm *kvm,for_each_slot_rmap_range (range->slot,PG_LEVEL_4K ,KVM_MAX_HUGEPAGE_LEVEL , range->start, range->end -1 , &iterator) ret |=handler (kvm, iterator.rmap, range->slot, iterator.gfn, - iterator.level, range->pte); + iterator.level, range->arg.pte);return ret; }
arch/x86/kvm/mmu/tdp_mmu.c +3 -3
@@ -1241,7 +1241,7 @@ static bool set_spte_gfn(struct kvm *kvm, struct tdp_iter *iter,u64 new_spte;/* Huge pages aren't expected to be modified without first being zapped. */ -WARN_ON (pte_huge (range->pte) || range->start +1 != range->end); +WARN_ON (pte_huge (range->arg.pte) || range->start +1 != range->end);if (iter->level !=PG_LEVEL_4K || !is_shadow_present_pte (iter->old_spte))
@@ -1255,9 +1255,9 @@ static bool set_spte_gfn(struct kvm *kvm, struct tdp_iter *iter,*/ tdp_mmu_iter_set_spte (kvm, iter,0 ); -if (!pte_write (range->pte)) { +if (!pte_write (range->arg.pte)) { new_spte =kvm_mmu_changed_pte_notifier_make_spte (iter->old_spte, -pte_pfn (range->pte)); +pte_pfn (range->arg.pte));tdp_mmu_iter_set_spte (kvm, iter, new_spte); }
include/linux/kvm_host.h +4 -1
@@ -260,7 +260,10 @@ struct kvm_gfn_range {struct kvm_memory_slot *slot;gfn_t start;gfn_t end; -pte_t pte; +union { +pte_t pte; +u64 raw; + } arg;bool may_block; };bool kvm_unmap_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range);
virt/kvm/kvm_main.c +10 -6
@@ -526,7 +526,10 @@ typedef void (*on_unlock_fn_t)(struct kvm *kvm);struct kvm_hva_range {unsigned long start;unsigned long end; -pte_t pte; +union { +pte_t pte; +u64 raw; + } arg;hva_handler_t handler;on_lock_fn_t on_lock;on_unlock_fn_t on_unlock;
@@ -562,6 +565,10 @@ static __always_inline int __kvm_handle_hva_range(struct kvm *kvm,struct kvm_memslots *slots;int i, idx; +BUILD_BUG_ON (sizeof (gfn_range.arg) !=sizeof (gfn_range.arg.raw)); +BUILD_BUG_ON (sizeof (range->arg) !=sizeof (range->arg.raw)); +BUILD_BUG_ON (sizeof (gfn_range.arg) !=sizeof (range->arg)); +if (WARN_ON_ONCE (range->end <= range->start))return 0 ;
@@ -591,7 +598,7 @@ static __always_inline int __kvm_handle_hva_range(struct kvm *kvm,* bother making these conditional (to avoid writes on * the second or later invocation of the handler). */ - gfn_range.pte = range->pte; + gfn_range.arg.raw = range->arg.raw; gfn_range.may_block = range->may_block;/*
@@ -639,7 +646,7 @@ static __always_inline int kvm_handle_hva_range(struct mmu_notifier *mn,const struct kvm_hva_range range = { .start = start, .end = end, - .pte = pte, + .arg.pte = pte, .handler = handler, .on_lock = (void *)kvm_null_fn, .on_unlock = (void *)kvm_null_fn,
@@ -659,7 +666,6 @@ static __always_inline int kvm_handle_hva_range_no_flush(struct mmu_notifier *mnconst struct kvm_hva_range range = { .start = start, .end = end, - .pte =__pte (0 ), .handler = handler, .on_lock = (void *)kvm_null_fn, .on_unlock = (void *)kvm_null_fn,
@@ -747,7 +753,6 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,const struct kvm_hva_range hva_range = { .start = range->start, .end = range->end, - .pte =__pte (0 ), .handler = kvm_unmap_gfn_range, .on_lock = kvm_mmu_invalidate_begin, .on_unlock = kvm_arch_guest_memory_reclaimed,
@@ -812,7 +817,6 @@ static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,const struct kvm_hva_range hva_range = { .start = range->start, .end = range->end, - .pte =__pte (0 ), .handler = (void *)kvm_null_fn, .on_lock = kvm_mmu_invalidate_end, .on_unlock = (void *)kvm_null_fn,
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Re: [RFC PATCH v11 01/29] KVM: Wrap kvm_gfn_range.pte in a per-action union
arch/x86/kvm/mmu/tdp_mmu.c static bool set_spte_gfn(struct kvm *kvm, struct tdp_iter *iter, · line 1244
Quoted 3 lines · Sean Christopherson
arch/x86/kvm/mmu/tdp_mmu.c +1
-1/* Huge pages aren't expected to be modified without first being zapped. */ -WARN_ON (pte_huge (range->pte) || range->start +1 != range->end); +WARN_ON (pte_huge (range->arg.pte) || range->start +1 != range->end);
Not familiar with this code. Just checking whether whether instead pr_{warn,err}() combined with return false would be a more graceful option?
BR, Jarkko
Sean Christopherson
maintainerRe: [RFC PATCH v11 01/29] KVM: Wrap kvm_gfn_range.pte in a per-action union
Quoted 7 lines · Jarkko Sakkinen
arch/x86/kvm/mmu/tdp_mmu.c static bool set_spte_gfn(struct kvm *kvm, struct tdp_iter *iter, · line 1244
Quoted 3 lines · Sean Christopherson
arch/x86/kvm/mmu/tdp_mmu.c +1
-1/* Huge pages aren't expected to be modified without first being zapped. */ -WARN_ON (pte_huge (range->pte) || range->start +1 != range->end); +WARN_ON (pte_huge (range->arg.pte) || range->start +1 != range->end);Not familiar with this code. Just checking whether whether instead pr_{warn,err}()
The "full" WARN is desirable, this is effecitvely an assert on the contract between the primary MMU, generic KVM code, and x86's TDP MMU. The .change_pte() mmu_notifier callback doesn't allow for hugepages, i.e. it's a (likely fatal) kernel bug if a hugepage is encountered at this point. Ditto for the "start + 1 == end" check, if that fails then generic KVM likely has a fatal bug.
Quoted 1 line
combined with return false would be a more graceful option?
The return value communicates whether or not a TLB flush is needed, not whether or not the operation was successful, i.e. there is no way to cancel the unexpected PTE change.
Re: [RFC PATCH v11 01/29] KVM: Wrap kvm_gfn_range.pte in a per-action union
In reply to Sean Christophersonvirt/kvm/kvm_main.c static __always_inline int __kvm_handle_hva_range(struct kvm *kvm, · line 569
Quoted 2 lines · Sean Christopherson
virt/kvm/kvm_main.c +2
-0+BUILD_BUG_ON (sizeof (gfn_range.arg) !=sizeof (gfn_range.arg.raw)); +BUILD_BUG_ON (sizeof (range->arg) !=sizeof (range->arg.raw));
I think these should be static assertions near the definition of the structs. However another possibility is to remove 'raw' and just assign the whole union.
Apart from this,
Paolo
Quoted 1 line
virt/kvm/kvm_main.c +1
-0+BUILD_BUG_ON (sizeof (gfn_range.arg) !=sizeof (range->arg));
Sean Christopherson
maintainerRe: [RFC PATCH v11 01/29] KVM: Wrap kvm_gfn_range.pte in a per-action union
Quoted 7 lines · Paolo Bonzini
Review
Quoted 2 lines · Sean Christopherson
(quoted diff) +2
-0+ BUILD_BUG_ON(sizeof(gfn_range.arg) != sizeof(gfn_range.arg.raw)); + BUILD_BUG_ON(sizeof(range->arg) != sizeof(range->arg.raw));I think these should be static assertions near the definition of the structs. However another possibility is to remove 'raw' and just assign the whole union.
Duh, and use a named union. I think when I first proposed this I forgot that a single value would be passed between kvm_hva_range *and* kvm_gfn_range, and so created an anonymous union without thinking about the impliciations.
A named union is _much_ cleaner. I'll post a complete version of the below snippet as a standalone non-RFC patch.
include/linux/kvm_host.h +5 -1
@@ -256,11 +256,15 @@ int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu);#endif #ifdef KVM_ARCH_WANT_MMU_NOTIFIER +union kvm_mmu_notifier_arg { +pte_t pte; +}; +struct kvm_gfn_range {struct kvm_memory_slot *slot;gfn_t start;gfn_t end; -pte_t pte; +union kvm_mmu_notifier_arg arg;bool may_block; };bool kvm_unmap_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range);
virt/kvm/kvm_main.c +4 -2
@@ -526,7 +526,7 @@ typedef void (*on_unlock_fn_t)(struct kvm *kvm);struct kvm_hva_range {unsigned long start;unsigned long end; -pte_t pte; +union kvm_mmu_notifier_arg arg;hva_handler_t handler;on_lock_fn_t on_lock;on_unlock_fn_t on_unlock;
@@ -547,6 +547,8 @@ static void kvm_null_fn(void) }#define IS_KVM_NULL_FN (fn) ((fn) == (void *)kvm_null_fn) +static const union kvm_mmu_notifier_arg KVM_NO_ARG ; +/* Iterate over each memslot intersecting [start, last] (inclusive) range */ #define kvm_for_each_memslot_in_hva_range (node, slots, start, last) \ for (node = interval_tree_iter_first(&slots->hva_tree, start, last); \
@@ -591,7 +593,7 @@ static __always_inline int __kvm_handle_hva_range(struct kvm *kvm,* bother making these conditional (to avoid writes on * the second or later invocation of the handler). */ - gfn_range.pte = range->pte; + gfn_range.arg = range->arg; gfn_range.may_block = range->may_block;/*
Re: [RFC PATCH v11 01/29] KVM: Wrap kvm_gfn_range.pte in a per-action union
In reply to Sean ChristophersonOn Tue, Jul 18, 2023 at 04:44:44PM -0700, Sean Christopherson wrote:
May I know why KVM now needs to register to callback .change_pte()? As also commented in kvm_mmu_notifier_change_pte(), .change_pte() must be surrounded by .invalidate_range_{start,end}().
While kvm_mmu_notifier_invalidate_range_start() has called kvm_unmap_gfn_range() to zap all leaf SPTEs, and page fault path will not install new SPTEs successfully before kvm_mmu_notifier_invalidate_range_end(), kvm_set_spte_gfn() should not be able to find any shadow present leaf entries to update PFN.
Or could we just delete completely
"kvm_handle_hva_range(mn, address, address + 1, pte, kvm_set_spte_gfn);" from kvm_mmu_notifier_change_pte() ?
Quoted 77 lines
arch/arm64/kvm/mmu.c +1
-1@@ -1721,7 +1721,7 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)bool kvm_set_spte_gfn (struct kvm *kvm,struct kvm_gfn_range *range) { -kvm_pfn_t pfn =pte_pfn (range->pte); +kvm_pfn_t pfn =pte_pfn (range->arg.pte);if (!kvm->arch.mmu.pgt)return false;arch/mips/kvm/mmu.c +1
-1@@ -447,7 +447,7 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)bool kvm_set_spte_gfn (struct kvm *kvm,struct kvm_gfn_range *range) {gpa_t gpa = range->start <<PAGE_SHIFT ; -pte_t hva_pte = range->pte; +pte_t hva_pte = range->arg.pte;pte_t *gpa_pte =kvm_mips_pte_for_gpa (kvm,NULL , gpa);pte_t old_pte;arch/riscv/kvm/mmu.c +1
-1@@ -559,7 +559,7 @@ bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)bool kvm_set_spte_gfn (struct kvm *kvm,struct kvm_gfn_range *range) {int ret; -kvm_pfn_t pfn =pte_pfn (range->pte); +kvm_pfn_t pfn =pte_pfn (range->arg.pte);if (!kvm->arch.pgd)return false;arch/x86/kvm/mmu/mmu.c +1
-1@@ -1588,7 +1588,7 @@ static __always_inline bool kvm_handle_gfn_range(struct kvm *kvm,for_each_slot_rmap_range (range->slot,PG_LEVEL_4K ,KVM_MAX_HUGEPAGE_LEVEL , range->start, range->end -1 , &iterator) ret |=handler (kvm, iterator.rmap, range->slot, iterator.gfn, - iterator.level, range->pte); + iterator.level, range->arg.pte);return ret; }arch/x86/kvm/mmu/tdp_mmu.c +3
-3@@ -1241,7 +1241,7 @@ static bool set_spte_gfn(struct kvm *kvm, struct tdp_iter *iter,u64 new_spte;/* Huge pages aren't expected to be modified without first being zapped. */ -WARN_ON (pte_huge (range->pte) || range->start +1 != range->end); +WARN_ON (pte_huge (range->arg.pte) || range->start +1 != range->end);if (iter->level !=PG_LEVEL_4K || !is_shadow_present_pte (iter->old_spte))@@ -1255,9 +1255,9 @@ static bool set_spte_gfn(struct kvm *kvm, struct tdp_iter *iter,*/ tdp_mmu_iter_set_spte (kvm, iter,0 ); -if (!pte_write (range->pte)) { +if (!pte_write (range->arg.pte)) { new_spte =kvm_mmu_changed_pte_notifier_make_spte (iter->old_spte, -pte_pfn (range->pte)); +pte_pfn (range->arg.pte));tdp_mmu_iter_set_spte (kvm, iter, new_spte); }
Re: [RFC PATCH v11 01/29] KVM: Wrap kvm_gfn_range.pte in a per-action union
Quoted 3 lines · Yan Zhao
On Tue, Jul 18, 2023 at 04:44:44PM -0700, Sean Christopherson wrote:
May I know why KVM now needs to register to callback .change_pte()?
I can see the original purpose is to "setting a pte in the shadow page table directly, instead of flushing the shadow page table entry and then getting vmexit to set it"[1].
IIUC, KVM is expected to directly make the new pte present for new pages in this callback, like for COW.
Quoted 8 lines
As also commented in
kvm_mmu_notifier_change_pte(), .change_pte() must be surrounded by .invalidate_range_{start,end}().While
kvm_mmu_notifier_invalidate_range_start()has calledkvm_unmap_gfn_range()to zap all leaf SPTEs, and page fault path will not install new SPTEs successfully beforekvm_mmu_notifier_invalidate_range_end(),kvm_set_spte_gfn()should not be able to find any shadow present leaf entries to update PFN.
I also failed to figure out how the kvm_set_spte_gfn() could pass several !is_shadow_present_pte(iter.old_spte) check then write the new pte.
[1] https://lore.kernel.org/all/200909222039.n8MKd4TL002696 at imap1.linux-foundation.org/
Thanks,
Yilun
Quoted 4 lines
Or could we just delete completely
"kvm_handle_hva_range(mn, address, address + 1, pte, kvm_set_spte_gfn);" fromkvm_mmu_notifier_change_pte()?
Sean Christopherson
maintainerRe: [RFC PATCH v11 01/29] KVM: Wrap kvm_gfn_range.pte in a per-action union
Quoted 11 lines · Xu Yilun
Quoted 3 lines · Yan Zhao
On Tue, Jul 18, 2023 at 04:44:44PM -0700, Sean Christopherson wrote:
May I know why KVM now needs to register to callback .change_pte()?
I can see the original purpose is to "setting a pte in the shadow page table directly, instead of flushing the shadow page table entry and then getting vmexit to set it"[1].
IIUC, KVM is expected to directly make the new pte present for new pages in this callback, like for COW.
Yes.
Quoted 9 lines
Quoted 8 lines
As also commented in
kvm_mmu_notifier_change_pte(), .change_pte() must be surrounded by .invalidate_range_{start,end}().While
kvm_mmu_notifier_invalidate_range_start()has calledkvm_unmap_gfn_range()to zap all leaf SPTEs, and page fault path will not install new SPTEs successfully beforekvm_mmu_notifier_invalidate_range_end(),kvm_set_spte_gfn()should not be able to find any shadow present leaf entries to update PFN.
Quoted 3 lines
I also failed to figure out how the
kvm_set_spte_gfn()could pass several !is_shadow_present_pte(iter.old_spte) check then write the new pte.
It can't. .change_pte() has been dead code on x86 for 10+ years at this point, and if my assessment from a few years back still holds true, it's dead code on all architectures.
The only reason I haven't formally proposed dropping the hook is that I don't want to risk the patch backfiring, i.e. I don't want to prompt someone to care enough to try and fix it.
commit c13fda237f08a388ba8a0849785045944bf39834
Author: Sean Christopherson <[email protected]> Date: Fri Apr 2 02:56:49 2021 +0200
KVM: Assert that notifier count is elevated in .change_pte()
In KVM's .change_pte() notification callback, replace the notifier sequence bump with a WARN_ON assertion that the notifier count is elevated. An elevated count provides stricter protections than bumping the sequence, and the sequence is guarnateed to be bumped before the count hits zero.
When .change_pte() was added by commit 828502d30073 ("ksm: add mmu_notifier set_pte_at_notify()"), bumping the sequence was necessary as .change_pte() would be invoked without any surrounding notifications.
However, since commit 6bdb913f0a70 ("mm: wrap calls to set_pte_at_notify with invalidate_range_start and invalidate_range_end"), all calls to .change_pte() are guaranteed to be surrounded by start() and end(), and so are guaranteed to run with an elevated notifier count.
Note, wrapping .change_pte() with .invalidate_range_{start,end}() is a bug of sorts, as invalidating the secondary MMU's (KVM's) PTE defeats the purpose of .change_pte(). Every arch's kvm_set_spte_hva() assumes .change_pte() is called when the relevant SPTE is present in KVM's MMU, as the original goal was to accelerate Kernel Samepage Merging (KSM) by updating KVM's SPTEs without requiring a VM-Exit (due to invalidating the SPTE). I.e. it means that .change_pte() is effectively dead code on _all_ architectures.
x86 and MIPS are clearcut nops if the old SPTE is not-present, and that is guaranteed due to the prior invalidation. PPC simply unmaps the SPTE, which again should be a nop due to the invalidation. arm64 is a bit murky, but it's also likely a nop because kvm_pgtable_stage2_map() is called without a cache pointer, which means it will map an entry if and only if an existing PTE was found.
For now, take advantage of the bug to simplify future consolidation of
KVMs's MMU notifier code. Doing so will not greatly complicate fixing
.change_pte(), assuming it's even worth fixing. .change_pte() has been broken for 8+ years and no one has complained. Even if there are KSM+KVM users that care deeply about its performance, the benefits of avoiding VM-Exits via .change_pte() need to be reevaluated to justify the added complexity and testing burden. Ripping out .change_pte() entirely would be a lot easier.
Sean Christopherson
maintainerRFC PATCH v11 2/29 KVM: Tweak kvm_hva_range and hva_handler_t to allow reusing for gfn ranges
In reply to Sean Christopherson--- virt/kvm/kvm_main.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-)
virt/kvm/kvm_main.c +19 -15
@@ -516,21 +516,25 @@ static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn) return container_of(mn, struct kvm, mmu_notifier); } -typedef bool (*hva_handler_t )(struct kvm *kvm,struct kvm_gfn_range *range); +typedef bool (*gfn_handler_t )(struct kvm *kvm,struct kvm_gfn_range *range);typedef void (*on_lock_fn_t )(struct kvm *kvm,unsigned long start,unsigned long end);typedef void (*on_unlock_fn_t )(struct kvm *kvm); -struct kvm_hva_range { -unsigned long start; -unsigned long end; +struct kvm_mmu_notifier_range { +/* +* 64-bit addresses, as KVM notifiers can operate on host virtual +* addresses (unsigned long) and guest physical addresses (64-bit). +*/ +u64 start; +u64 end;union {pte_t pte;u64 raw; } arg; -hva_handler_t handler; +gfn_handler_t handler;on_lock_fn_t on_lock;on_unlock_fn_t on_unlock;bool flush_on_ret;
@@ -557,7 +561,7 @@ static void kvm_null_fn(void) node = interval_tree_iter_next(node, start, last)) \ static __always_inline int __kvm_handle_hva_range(struct kvm *kvm, -const struct kvm_hva_range *range) +const struct kvm_mmu_notifier_range *range) {bool ret = false, locked = false;struct kvm_gfn_range gfn_range;
@@ -588,9 +592,9 @@ static __always_inline int __kvm_handle_hva_range(struct kvm *kvm,unsigned long hva_start, hva_end; slot = container_of(node, struct kvm_memory_slot, hva_node[slots->node_idx]); - hva_start =max (range->start, slot->userspace_addr); - hva_end =min (range->end, slot->userspace_addr + - (slot->npages <<PAGE_SHIFT )); + hva_start = max_t(unsigned long, range->start, slot->userspace_addr); + hva_end = min_t(unsigned long, range->end, + slot->userspace_addr + (slot->npages <<PAGE_SHIFT ));/* * To optimize for the likely case where the address
@@ -640,10 +644,10 @@ static __always_inline int kvm_handle_hva_range(struct mmu_notifier *mn,unsigned long start, unsigned long end, pte_t pte, - hva_handler_t handler) + gfn_handler_t handler) { struct kvm *kvm = mmu_notifier_to_kvm(mn); - const struct kvm_hva_range range = { + const struct kvm_mmu_notifier_range range = { .start = start, .end = end, .arg.pte = pte,
@@ -660,10 +664,10 @@ static __always_inline int kvm_handle_hva_range(struct mmu_notifier *mn, static __always_inline int kvm_handle_hva_range_no_flush(struct mmu_notifier *mn,unsigned long start,unsigned long end, -hva_handler_t handler) +gfn_handler_t handler) {struct kvm *kvm =mmu_notifier_to_kvm (mn); -const struct kvm_hva_range range = { +const struct kvm_mmu_notifier_range range = { .start = start, .end = end, .handler = handler,
@@ -750,7 +754,7 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, const struct mmu_notifier_range *range) {struct kvm *kvm =mmu_notifier_to_kvm (mn); -const struct kvm_hva_range hva_range = { +const struct kvm_mmu_notifier_range hva_range = { .start = range->start, .end = range->end, .handler = kvm_unmap_gfn_range,
@@ -814,7 +818,7 @@ static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn, const struct mmu_notifier_range *range) {struct kvm *kvm =mmu_notifier_to_kvm (mn); -const struct kvm_hva_range hva_range = { +const struct kvm_mmu_notifier_range hva_range = { .start = range->start, .end = range->end, .handler = (void *)kvm_null_fn,
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Re: [RFC PATCH v11 02/29] KVM: Tweak kvm_hva_range and hva_handler_t to allow reusing for gfn ranges
Quoted 4 lines · Sean Christopherson
Signed-off-by Sean Christopherson<[email protected]>--- virt/kvm/kvm_main.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-)
Sean Christopherson
maintainerRFC PATCH v11 3/29 KVM: Use gfn instead of hva for mmu_notifier_retry
In reply to Sean ChristophersonFrom: Chao Peng <[email protected]>
Currently in mmu_notifier invalidate path, hva range is recorded and then checked against by mmu_notifier_retry_hva() in the page fault handling path. However, for the to be introduced private memory, a page fault may not have a hva associated, checking gfn(gpa) makes more sense.
For existing hva based shared memory, gfn is expected to also work. The only downside is when aliasing multiple gfns to a single hva, the current algorithm of checking multiple ranges could result in a much larger range being rejected. Such aliasing should be uncommon, so the impact is expected small.
--- arch/x86/kvm/mmu/mmu.c | 10 ++++++---- arch/x86/kvm/vmx/vmx.c | 11 +++++------ include/linux/kvm_host.h | 33 +++++++++++++++++++++------------ virt/kvm/kvm_main.c | 40 +++++++++++++++++++++++++++++++--------- 4 files changed, 63 insertions(+), 31 deletions(-)
arch/x86/kvm/mmu/mmu.c +6 -4
@@ -3087,7 +3087,7 @@ static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)* * There are several ways to safely use this helper: * -* - Check mmu_invalidate_retry_hva() after grabbing the mapping level, before +* - Check mmu_invalidate_retry_gfn() after grabbing the mapping level, before * consuming it. In this case, mmu_lock doesn't need to be held during the * lookup, but it does need to be held while checking the MMU notifier. *
@@ -4400,7 +4400,7 @@ static bool is_page_fault_stale(struct kvm_vcpu *vcpu,return true;return fault->slot && -mmu_invalidate_retry_hva (vcpu->kvm, fault->mmu_seq, fault->hva); +mmu_invalidate_retry_gfn (vcpu->kvm, fault->mmu_seq, fault->gfn); }static int direct_page_fault (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault)
@@ -6301,7 +6301,9 @@ void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end)write_lock (&kvm->mmu_lock); -kvm_mmu_invalidate_begin (kvm,0 ,-1ul ); +kvm_mmu_invalidate_begin (kvm); + +kvm_mmu_invalidate_range_add (kvm, gfn_start, gfn_end); flush =kvm_rmap_zap_gfn_range (kvm, gfn_start, gfn_end);
@@ -6314,7 +6316,7 @@ void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end)if (flush)kvm_flush_remote_tlbs_range (kvm, gfn_start, gfn_end - gfn_start); -kvm_mmu_invalidate_end (kvm,0 ,-1ul ); +kvm_mmu_invalidate_end (kvm);write_unlock (&kvm->mmu_lock); }
arch/x86/kvm/vmx/vmx.c +5 -6
@@ -6729,10 +6729,10 @@ static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu)return ;/* -* Grab the memslot so that the hva lookup for the mmu_notifier retry -* is guaranteed to use the same memslot as the pfn lookup, i.e. rely -* on the pfn lookup's validation of the memslot to ensure a valid hva -* is used for the retry check. +* Explicitly grab the memslot using KVM's internal slot ID to ensure +* KVM doesn't unintentionally grab a userspace memslot. It _should_ +* be impossible for userspace to create a memslot for the APIC when +* APICv is enabled, but paranoia won't hurt in this case. */ slot =id_to_memslot (slots,APIC_ACCESS_PAGE_PRIVATE_MEMSLOT );if (!slot || slot->flags &KVM_MEMSLOT_INVALID )
@@ -6757,8 +6757,7 @@ static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu)return ;read_lock (&vcpu->kvm->mmu_lock); -if (mmu_invalidate_retry_hva (kvm, mmu_seq, -gfn_to_hva_memslot (slot, gfn))) { +if (mmu_invalidate_retry_gfn (kvm, mmu_seq, gfn)) {kvm_make_request (KVM_REQ_APIC_PAGE_RELOAD , vcpu);read_unlock (&vcpu->kvm->mmu_lock); goto out;
include/linux/kvm_host.h +21 -12
@@ -788,8 +788,8 @@ struct kvm {struct mmu_notifier mmu_notifier;unsigned long mmu_invalidate_seq;long mmu_invalidate_in_progress; -unsigned long mmu_invalidate_range_start; -unsigned long mmu_invalidate_range_end; +gfn_t mmu_invalidate_range_start; +gfn_t mmu_invalidate_range_end;#endif struct list_head devices;u64 manual_dirty_log_protect;
@@ -1371,10 +1371,9 @@ void kvm_mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc);void *kvm_mmu_memory_cache_alloc (struct kvm_mmu_memory_cache *mc);#endif -void kvm_mmu_invalidate_begin (struct kvm *kvm,unsigned long start, -unsigned long end); -void kvm_mmu_invalidate_end (struct kvm *kvm,unsigned long start, -unsigned long end); +void kvm_mmu_invalidate_begin (struct kvm *kvm); +void kvm_mmu_invalidate_range_add (struct kvm *kvm,gfn_t start,gfn_t end); +void kvm_mmu_invalidate_end (struct kvm *kvm);long kvm_arch_dev_ioctl (struct file *filp,unsigned int ioctl,unsigned long arg);
@@ -1940,9 +1939,9 @@ static inline int mmu_invalidate_retry(struct kvm *kvm, unsigned long mmu_seq)return 0 ; } -static inline int mmu_invalidate_retry_hva (struct kvm *kvm, +static inline int mmu_invalidate_retry_gfn (struct kvm *kvm,unsigned long mmu_seq, -unsigned long hva) +gfn_t gfn) {lockdep_assert_held (&kvm->mmu_lock);/*
@@ -1951,10 +1950,20 @@ static inline int mmu_invalidate_retry_hva(struct kvm *kvm,* that might be being invalidated. Note that it may include some false * positives, due to shortcuts when handing concurrent invalidations. */ -if (unlikely (kvm->mmu_invalidate_in_progress) && - hva >= kvm->mmu_invalidate_range_start && - hva < kvm->mmu_invalidate_range_end) -return 1 ; +if (unlikely (kvm->mmu_invalidate_in_progress)) { +/* +* Dropping mmu_lock after bumping mmu_invalidate_in_progress +* but before updating the range is a KVM bug. +*/ +if (WARN_ON_ONCE (kvm->mmu_invalidate_range_start ==INVALID_GPA || + kvm->mmu_invalidate_range_end ==INVALID_GPA )) +return 1 ; + +if (gfn >= kvm->mmu_invalidate_range_start && + gfn < kvm->mmu_invalidate_range_end) +return 1 ; + } +if (kvm->mmu_invalidate_seq != mmu_seq)return 1 ;return 0 ;
virt/kvm/kvm_main.c +31 -9
@@ -518,9 +518,7 @@ static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)typedef bool (*gfn_handler_t )(struct kvm *kvm,struct kvm_gfn_range *range); -typedef void (*on_lock_fn_t )(struct kvm *kvm,unsigned long start, -unsigned long end); - +typedef void (*on_lock_fn_t )(struct kvm *kvm);typedef void (*on_unlock_fn_t )(struct kvm *kvm);struct kvm_mmu_notifier_range {
@@ -617,7 +615,8 @@ static __always_inline int __kvm_handle_hva_range(struct kvm *kvm, locked = true;KVM_MMU_LOCK (kvm);if (!IS_KVM_NULL_FN (range->on_lock)) - range->on_lock (kvm, range->start, range->end); + range->on_lock (kvm); +if (IS_KVM_NULL_FN (range->handler))break ; }
@@ -721,15 +720,26 @@ static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,kvm_handle_hva_range (mn, address, address +1 , pte, kvm_change_spte_gfn); } -void kvm_mmu_invalidate_begin (struct kvm *kvm,unsigned long start, -unsigned long end) +void kvm_mmu_invalidate_begin (struct kvm *kvm) { +lockdep_assert_held_write (&kvm->mmu_lock);/* * The count increase must become visible at unlock time as no * spte can be established without taking the mmu_lock and * count is also read inside the mmu_lock critical section. */ kvm->mmu_invalidate_in_progress++; + +if (likely (kvm->mmu_invalidate_in_progress ==1 )) + kvm->mmu_invalidate_range_start =INVALID_GPA ; +} + +void kvm_mmu_invalidate_range_add (struct kvm *kvm,gfn_t start,gfn_t end) +{ +lockdep_assert_held_write (&kvm->mmu_lock); + +WARN_ON_ONCE (!kvm->mmu_invalidate_in_progress); +if (likely (kvm->mmu_invalidate_in_progress ==1 )) { kvm->mmu_invalidate_range_start = start; kvm->mmu_invalidate_range_end = end;
@@ -750,6 +760,12 @@ void kvm_mmu_invalidate_begin(struct kvm *kvm, unsigned long start, } } +static bool kvm_mmu_unmap_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range) +{ +kvm_mmu_invalidate_range_add (kvm, range->start, range->end); +return kvm_unmap_gfn_range (kvm, range); +} +static int kvm_mmu_notifier_invalidate_range_start (struct mmu_notifier *mn,const struct mmu_notifier_range *range) {
@@ -757,7 +773,7 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,const struct kvm_mmu_notifier_range hva_range = { .start = range->start, .end = range->end, - .handler = kvm_unmap_gfn_range, + .handler = kvm_mmu_unmap_gfn_range, .on_lock = kvm_mmu_invalidate_begin, .on_unlock = kvm_arch_guest_memory_reclaimed, .flush_on_ret = true,
@@ -796,8 +812,7 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,return 0 ; } -void kvm_mmu_invalidate_end (struct kvm *kvm,unsigned long start, -unsigned long end) +void kvm_mmu_invalidate_end (struct kvm *kvm) {/* * This sequence increase will notify the kvm page fault that
@@ -812,6 +827,13 @@ void kvm_mmu_invalidate_end(struct kvm *kvm, unsigned long start,* in conjunction with the smp_rmb in mmu_invalidate_retry(). */ kvm->mmu_invalidate_in_progress--; + +/* +* Assert that at least one range must be added between start() and +* end(). Not adding a range isn't fatal, but it is a KVM bug. +*/ +WARN_ON_ONCE (kvm->mmu_invalidate_in_progress && + kvm->mmu_invalidate_range_start ==INVALID_GPA ); } static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Re: [RFC PATCH v11 03/29] KVM: Use gfn instead of hva for mmu_notifier_retry
Quoted 7 lines · Sean Christopherson
From: Chao Peng<[email protected]>
Currently in
mmu_notifierinvalidate path, hva range is recorded and then checked against bymmu_notifier_retry_hva()in the page fault handling path. However, for the to be introduced private memory, a page fault may not have a hva associated, checking gfn(gpa) makes more sense.
Quoted 5 lines
For existing hva based shared memory, gfn is expected to also work. The only downside is when aliasing multiple gfns to a single hva, the current algorithm of checking multiple ranges could result in a much larger range being rejected. Such aliasing should be uncommon, so the impact is expected small.
Quoted 214 lines
Suggested-by Sean Christopherson<[email protected]>Signed-off-by Chao Peng<[email protected]>Reviewed-by Fuad Tabba<[email protected]>Tested-by Fuad Tabba<[email protected]>[sean: convert vmx_set_apic_access_page_addr() to gfn-based API]Signed-off-by Sean Christopherson<[email protected]>--- arch/x86/kvm/mmu/mmu.c | 10 ++++++---- arch/x86/kvm/vmx/vmx.c | 11 +++++------ include/linux/kvm_host.h | 33 +++++++++++++++++++++------------ virt/kvm/kvm_main.c | 40 +++++++++++++++++++++++++++++++--------- 4 files changed, 63 insertions(+), 31 deletions(-)arch/x86/kvm/mmu/mmu.c +6
-4@@ -3087,7 +3087,7 @@ static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)* * There are several ways to safely use this helper: * -* - Check mmu_invalidate_retry_hva() after grabbing the mapping level, before +* - Check mmu_invalidate_retry_gfn() after grabbing the mapping level, before * consuming it. In this case, mmu_lock doesn't need to be held during the * lookup, but it does need to be held while checking the MMU notifier. * @@ -4400,7 +4400,7 @@ static bool is_page_fault_stale(struct kvm_vcpu *vcpu,return true;return fault->slot && -mmu_invalidate_retry_hva (vcpu->kvm, fault->mmu_seq, fault->hva); +mmu_invalidate_retry_gfn (vcpu->kvm, fault->mmu_seq, fault->gfn); }static int direct_page_fault (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault)@@ -6301,7 +6301,9 @@ void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end)write_lock (&kvm->mmu_lock); -kvm_mmu_invalidate_begin (kvm,0 ,-1ul ); +kvm_mmu_invalidate_begin (kvm); + +kvm_mmu_invalidate_range_add (kvm, gfn_start, gfn_end); flush =kvm_rmap_zap_gfn_range (kvm, gfn_start, gfn_end);@@ -6314,7 +6316,7 @@ void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end)if (flush)kvm_flush_remote_tlbs_range (kvm, gfn_start, gfn_end - gfn_start); -kvm_mmu_invalidate_end (kvm,0 ,-1ul ); +kvm_mmu_invalidate_end (kvm);write_unlock (&kvm->mmu_lock); }arch/x86/kvm/vmx/vmx.c +5
-6@@ -6729,10 +6729,10 @@ static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu)return ;/* -* Grab the memslot so that the hva lookup for the mmu_notifier retry -* is guaranteed to use the same memslot as the pfn lookup, i.e. rely -* on the pfn lookup's validation of the memslot to ensure a valid hva -* is used for the retry check. +* Explicitly grab the memslot using KVM's internal slot ID to ensure +* KVM doesn't unintentionally grab a userspace memslot. It_should_ +* be impossible for userspace to create a memslot for the APIC when +* APICv is enabled, but paranoia won't hurt in this case. */ slot =id_to_memslot (slots,APIC_ACCESS_PAGE_PRIVATE_MEMSLOT );if (!slot || slot->flags &KVM_MEMSLOT_INVALID )@@ -6757,8 +6757,7 @@ static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu)return ;read_lock (&vcpu->kvm->mmu_lock); -if (mmu_invalidate_retry_hva (kvm, mmu_seq, -gfn_to_hva_memslot (slot, gfn))) { +if (mmu_invalidate_retry_gfn (kvm, mmu_seq, gfn)) {kvm_make_request (KVM_REQ_APIC_PAGE_RELOAD , vcpu);read_unlock (&vcpu->kvm->mmu_lock); goto out;include/linux/kvm_host.h +21
-12@@ -788,8 +788,8 @@ struct kvm {struct mmu_notifier mmu_notifier;unsigned long mmu_invalidate_seq;long mmu_invalidate_in_progress; -unsigned long mmu_invalidate_range_start; -unsigned long mmu_invalidate_range_end; +gfn_t mmu_invalidate_range_start; +gfn_t mmu_invalidate_range_end;#endif struct list_head devices;u64 manual_dirty_log_protect;@@ -1371,10 +1371,9 @@ void kvm_mmu_free_memory_cache(struct kvm_mmu_memory_cache *mc);void *kvm_mmu_memory_cache_alloc (struct kvm_mmu_memory_cache *mc);#endif -void kvm_mmu_invalidate_begin (struct kvm *kvm,unsigned long start, -unsigned long end); -void kvm_mmu_invalidate_end (struct kvm *kvm,unsigned long start, -unsigned long end); +void kvm_mmu_invalidate_begin (struct kvm *kvm); +void kvm_mmu_invalidate_range_add (struct kvm *kvm,gfn_t start,gfn_t end); +void kvm_mmu_invalidate_end (struct kvm *kvm);long kvm_arch_dev_ioctl (struct file *filp,unsigned int ioctl,unsigned long arg);@@ -1940,9 +1939,9 @@ static inline int mmu_invalidate_retry(struct kvm *kvm, unsigned long mmu_seq)return 0 ; } -static inline int mmu_invalidate_retry_hva (struct kvm *kvm, +static inline int mmu_invalidate_retry_gfn (struct kvm *kvm,unsigned long mmu_seq, -unsigned long hva) +gfn_t gfn) {lockdep_assert_held (&kvm->mmu_lock);/* @@ -1951,10 +1950,20 @@ static inline int mmu_invalidate_retry_hva(struct kvm *kvm,* that might be being invalidated. Note that it may include some false * positives, due to shortcuts when handing concurrent invalidations. */ -if (unlikely (kvm->mmu_invalidate_in_progress) && - hva >= kvm->mmu_invalidate_range_start && - hva < kvm->mmu_invalidate_range_end) -return 1 ; +if (unlikely (kvm->mmu_invalidate_in_progress)) { +/* +* Dropping mmu_lock after bumping mmu_invalidate_in_progress +* but before updating the range is a KVM bug. +*/ +if (WARN_ON_ONCE (kvm->mmu_invalidate_range_start ==INVALID_GPA || + kvm->mmu_invalidate_range_end ==INVALID_GPA )) +return 1 ; + +if (gfn >= kvm->mmu_invalidate_range_start && + gfn < kvm->mmu_invalidate_range_end) +return 1 ; + } +if (kvm->mmu_invalidate_seq != mmu_seq)return 1 ;return 0 ;virt/kvm/kvm_main.c +22
-6@@ -518,9 +518,7 @@ static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)typedef bool (*gfn_handler_t )(struct kvm *kvm,struct kvm_gfn_range *range); -typedef void (*on_lock_fn_t )(struct kvm *kvm,unsigned long start, -unsigned long end); - +typedef void (*on_lock_fn_t )(struct kvm *kvm);typedef void (*on_unlock_fn_t )(struct kvm *kvm);struct kvm_mmu_notifier_range {@@ -617,7 +615,8 @@ static __always_inline int __kvm_handle_hva_range(struct kvm *kvm, locked = true;KVM_MMU_LOCK (kvm);if (!IS_KVM_NULL_FN (range->on_lock)) - range->on_lock (kvm, range->start, range->end); + range->on_lock (kvm); +if (IS_KVM_NULL_FN (range->handler))break ; }@@ -721,15 +720,26 @@ static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,kvm_handle_hva_range (mn, address, address +1 , pte, kvm_change_spte_gfn); } -void kvm_mmu_invalidate_begin (struct kvm *kvm,unsigned long start, -unsigned long end) +void kvm_mmu_invalidate_begin (struct kvm *kvm) { +lockdep_assert_held_write (&kvm->mmu_lock);/* * The count increase must become visible at unlock time as no * spte can be established without taking the mmu_lock and * count is also read inside the mmu_lock critical section. */ kvm->mmu_invalidate_in_progress++; + +if (likely (kvm->mmu_invalidate_in_progress ==1 )) + kvm->mmu_invalidate_range_start =INVALID_GPA ; +} + +void kvm_mmu_invalidate_range_add (struct kvm *kvm,gfn_t start,gfn_t end) +{ +lockdep_assert_held_write (&kvm->mmu_lock); + +WARN_ON_ONCE (!kvm->mmu_invalidate_in_progress); +if (likely (kvm->mmu_invalidate_in_progress ==1 )) { kvm->mmu_invalidate_range_start = start; kvm->mmu_invalidate_range_end = end;@@ -750,6 +760,12 @@ void kvm_mmu_invalidate_begin(struct kvm *kvm, unsigned long start, } } +static bool kvm_mmu_unmap_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range) +{ +kvm_mmu_invalidate_range_add (kvm, range->start, range->end); +return kvm_unmap_gfn_range (kvm, range); +} +
Sean Christopherson
maintainerRFC PATCH v11 4/29 KVM: PPC: Drop dead code related to KVM_ARCH_WANT_MMU_NOTIFIER
In reply to Sean Christopherson--- arch/powerpc/kvm/powerpc.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
arch/powerpc/kvm/powerpc.c +4 -3
@@ -634,10 +634,11 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_SYNC_MMU :#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE r = hv_enabled; -#elif defined(KVM_ARCH_WANT_MMU_NOTIFIER ) - r =1 ;#else - r =0 ; +#ifndef KVM_ARCH_WANT_MMU_NOTIFIER +BUILD_BUG (); +#endif + r =1 ;#endif break ;#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Paolo Bonzini
maintainerRe: [RFC PATCH v11 04/29] KVM: PPC: Drop dead code related to KVM_ARCH_WANT_MMU_NOTIFIER
arch/powerpc/kvm/powerpc.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 636
Quoted 13 lines · Sean Christopherson
Signed-off-by Sean Christopherson<[email protected]>--- arch/powerpc/kvm/powerpc.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)arch/powerpc/kvm/powerpc.c +0
-0@@ -634,10 +634,11 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_SYNC_MMU :#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE r = hv_enabled;
This could actually be unnecessarily conservative. Even book3s_pr.c knows how to do unmap and set_spte, so it should be able to support KVM_CAP_SYNC_MMU. Alex, Nick, do you remember any of this? This would mean moving KVM_CAP_SYNC_MMU to virt/kvm/kvm_main.c, which is nice.
Paolo
Quoted 11 lines
arch/powerpc/kvm/powerpc.c +4
-3-#elif defined(KVM_ARCH_WANT_MMU_NOTIFIER ) - r =1 ;#else - r =0 ; +#ifndef KVM_ARCH_WANT_MMU_NOTIFIER +BUILD_BUG (); +#endif + r =1 ;#endif break ;#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
Sean Christopherson
maintainerRFC PATCH v11 5/29 KVM: Convert KVM_ARCH_WANT_MMU_NOTIFIER to CONFIG_KVM_GENERIC_MMU_NOTIFIER
In reply to Sean Christopherson--- arch/arm64/include/asm/kvm_host.h | 2 -- arch/arm64/kvm/Kconfig | 2 +- arch/mips/include/asm/kvm_host.h | 2 -- arch/mips/kvm/Kconfig | 2 +- arch/powerpc/include/asm/kvm_host.h | 2 -- arch/powerpc/kvm/Kconfig | 8 ++++---- arch/powerpc/kvm/powerpc.c | 4 +--- arch/riscv/include/asm/kvm_host.h | 2 -- arch/riscv/kvm/Kconfig | 2 +- arch/x86/include/asm/kvm_host.h | 2 -- arch/x86/kvm/Kconfig | 2 +- include/linux/kvm_host.h | 8 +++++--- virt/kvm/Kconfig | 4 ++++ virt/kvm/kvm_main.c | 10 +++++----- 14 files changed, 23 insertions(+), 29 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -2
@@ -912,8 +912,6 @@ int __kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,int __kvm_arm_vcpu_set_events (struct kvm_vcpu *vcpu,struct kvm_vcpu_events *events); -#define KVM_ARCH_WANT_MMU_NOTIFIER -void kvm_arm_halt_guest (struct kvm *kvm);void kvm_arm_resume_guest (struct kvm *kvm);
arch/arm64/kvm/Kconfig +1 -1
@@ -22,7 +22,7 @@ menuconfig KVM bool "Kernel-based Virtual Machine (KVM) support" depends on HAVE_KVM select KVM_GENERIC_HARDWARE_ENABLING - select MMU_NOTIFIER + select KVM_GENERIC_MMU_NOTIFIER select PREEMPT_NOTIFIERS select HAVE_KVM_CPU_RELAX_INTERCEPT select HAVE_KVM_ARCH_TLB_FLUSH_ALL
arch/mips/include/asm/kvm_host.h +0 -2
@@ -810,8 +810,6 @@ int kvm_mips_mkclean_gpa_pt(struct kvm *kvm, gfn_t start_gfn, gfn_t end_gfn);pgd_t *kvm_pgd_alloc (void );void kvm_mmu_free_memory_caches (struct kvm_vcpu *vcpu); -#define KVM_ARCH_WANT_MMU_NOTIFIER -/* Emulation */ enum emulation_result update_pc (struct kvm_vcpu *vcpu,u32 cause);int kvm_get_badinstr (u32 *opc,struct kvm_vcpu *vcpu,u32 *out);
arch/mips/kvm/Kconfig +1 -1
@@ -25,7 +25,7 @@ config KVM select HAVE_KVM_EVENTFD select HAVE_KVM_VCPU_ASYNC_IOCTL select KVM_MMIO - select MMU_NOTIFIER + select KVM_GENERIC_MMU_NOTIFIER select INTERVAL_TREE select KVM_GENERIC_HARDWARE_ENABLING help
arch/powerpc/include/asm/kvm_host.h +0 -2
@@ -62,8 +62,6 @@#include <linux/mmu_notifier.h> -#define KVM_ARCH_WANT_MMU_NOTIFIER -#define HPTEG_CACHE_NUM (1 << 15)#define HPTEG_HASH_BITS_PTE 13#define HPTEG_HASH_BITS_PTE_LONG 12
arch/powerpc/kvm/Kconfig +4 -4
@@ -42,7 +42,7 @@ config KVM_BOOK3S_64_HANDLER config KVM_BOOK3S_PR_POSSIBLE bool select KVM_MMIO - select MMU_NOTIFIER + select KVM_GENERIC_MMU_NOTIFIER config KVM_BOOK3S_HV_POSSIBLE bool
@@ -85,7 +85,7 @@ config KVM_BOOK3S_64_HV tristate "KVM for POWER7 and later using hypervisor mode in host" depends on KVM_BOOK3S_64 && PPC_POWERNV select KVM_BOOK3S_HV_POSSIBLE - select MMU_NOTIFIER + select KVM_GENERIC_MMU_NOTIFIER select CMA help Support running unmodified book3s_64 guest kernels in
@@ -194,7 +194,7 @@ config KVM_E500V2 depends on !CONTEXT_TRACKING_USER select KVM select KVM_MMIO - select MMU_NOTIFIER + select KVM_GENERIC_MMU_NOTIFIER help Support running unmodified E500 guest kernels in virtual machines on E500v2 host processors.
@@ -211,7 +211,7 @@ config KVM_E500MC select KVM select KVM_MMIO select KVM_BOOKE_HV - select MMU_NOTIFIER + select KVM_GENERIC_MMU_NOTIFIER help Support running unmodified E500MC/E5500/E6500 guest kernels in virtual machines on E500MC/E5500/E6500 host processors.
arch/powerpc/kvm/powerpc.c +1 -3
@@ -635,9 +635,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE r = hv_enabled;#else -#ifndef KVM_ARCH_WANT_MMU_NOTIFIER -BUILD_BUG (); -#endif +BUILD_BUG_ON (!IS_ENABLED (CONFIG_KVM_GENERIC_MMU_NOTIFIER )); r =1 ;#endif break ;
arch/riscv/include/asm/kvm_host.h +0 -2
@@ -249,8 +249,6 @@ struct kvm_vcpu_arch {static inline void kvm_arch_sync_events (struct kvm *kvm) {}static inline void kvm_arch_sched_in (struct kvm_vcpu *vcpu,int cpu) {} -#define KVM_ARCH_WANT_MMU_NOTIFIER -#define KVM_RISCV_GSTAGE_TLB_MIN_ORDER 12 void kvm_riscv_local_hfence_gvma_vmid_gpa(unsigned long vmid,
arch/riscv/kvm/Kconfig +1 -1
@@ -30,7 +30,7 @@ config KVM select KVM_GENERIC_HARDWARE_ENABLING select KVM_MMIO select KVM_XFER_TO_GUEST_WORK - select MMU_NOTIFIER + select KVM_GENERIC_MMU_NOTIFIER select PREEMPT_NOTIFIERS help Support hosting virtualized guest machines.
arch/x86/include/asm/kvm_host.h +0 -2
@@ -2110,8 +2110,6 @@ enum {# define kvm_memslots_for_spte_role (kvm, role) __kvm_memslots(kvm, 0)#endif -#define KVM_ARCH_WANT_MMU_NOTIFIER -int kvm_cpu_has_injectable_intr (struct kvm_vcpu *v);int kvm_cpu_has_interrupt (struct kvm_vcpu *vcpu);int kvm_cpu_has_extint (struct kvm_vcpu *v);
arch/x86/kvm/Kconfig +1 -1
@@ -24,7 +24,7 @@ config KVM depends on HIGH_RES_TIMERS depends on X86_LOCAL_APIC select PREEMPT_NOTIFIERS - select MMU_NOTIFIER + select KVM_GENERIC_MMU_NOTIFIER select HAVE_KVM_IRQCHIP select HAVE_KVM_PFNCACHE select HAVE_KVM_IRQFD
include/linux/kvm_host.h +5 -3
@@ -255,7 +255,9 @@ bool kvm_setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,int kvm_async_pf_wakeup_all (struct kvm_vcpu *vcpu);#endif -#ifdef KVM_ARCH_WANT_MMU_NOTIFIER +struct kvm_gfn_range ; + +#ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER struct kvm_gfn_range { struct kvm_memory_slot *slot; gfn_t start;
@@ -784,7 +786,7 @@ struct kvm {struct hlist_head irq_ack_notifier_list;#endif -#if defined(CONFIG_MMU_NOTIFIER ) && defined(KVM_ARCH_WANT_MMU_NOTIFIER ) +#ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER struct mmu_notifier mmu_notifier;unsigned long mmu_invalidate_seq;long mmu_invalidate_in_progress;
@@ -1916,7 +1918,7 @@ extern const struct _kvm_stats_desc kvm_vm_stats_desc[];extern const struct kvm_stats_header kvm_vcpu_stats_header;extern const struct _kvm_stats_desc kvm_vcpu_stats_desc[]; -#if defined(CONFIG_MMU_NOTIFIER ) && defined(KVM_ARCH_WANT_MMU_NOTIFIER ) +#ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER static inline int mmu_invalidate_retry (struct kvm *kvm,unsigned long mmu_seq) {if (unlikely (kvm->mmu_invalidate_in_progress))
virt/kvm/Kconfig +4 -0
@@ -95,3 +95,7 @@ config HAVE_KVM_PM_NOTIFIER config KVM_GENERIC_HARDWARE_ENABLING bool + +config KVM_GENERIC_MMU_NOTIFIER + select MMU_NOTIFIER + bool
virt/kvm/kvm_main.c +5 -5
@@ -510,7 +510,7 @@ void kvm_destroy_vcpus(struct kvm *kvm) }EXPORT_SYMBOL_GPL (kvm_destroy_vcpus); -#if defined(CONFIG_MMU_NOTIFIER ) && defined(KVM_ARCH_WANT_MMU_NOTIFIER ) +#ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER static inline struct kvm *mmu_notifier_to_kvm (struct mmu_notifier *mn) { return container_of(mn, struct kvm, mmu_notifier);
@@ -938,14 +938,14 @@ static int kvm_init_mmu_notifier(struct kvm *kvm)return mmu_notifier_register (&kvm->mmu_notifier, current->mm); } -#else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */ +#else /* !CONFIG_KVM_GENERIC_MMU_NOTIFIER */ static int kvm_init_mmu_notifier (struct kvm *kvm) {return 0 ; } -#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */ +#endif /* CONFIG_KVM_GENERIC_MMU_NOTIFIER */ #ifdef CONFIG_HAVE_KVM_PM_NOTIFIER static int kvm_pm_notifier_call(struct notifier_block *bl,
@@ -1265,7 +1265,7 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname) out_err_no_debugfs:kvm_coalesced_mmio_free (kvm); out_no_coalesced_mmio: -#if defined(CONFIG_MMU_NOTIFIER ) && defined(KVM_ARCH_WANT_MMU_NOTIFIER ) +#ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER if (kvm->mmu_notifier.ops)mmu_notifier_unregister (&kvm->mmu_notifier, current->mm);#endif
@@ -1325,7 +1325,7 @@ static void kvm_destroy_vm(struct kvm *kvm) kvm->buses[i] =NULL ; }kvm_coalesced_mmio_free (kvm); -#if defined(CONFIG_MMU_NOTIFIER ) && defined(KVM_ARCH_WANT_MMU_NOTIFIER ) +#ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER mmu_notifier_unregister (&kvm->mmu_notifier, kvm->mm);/* * At this point, pending calls to invalidate_range_start()
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Re: [RFC PATCH v11 05/29] KVM: Convert KVM_ARCH_WANT_MMU_NOTIFIER to CONFIG_KVM_GENERIC_MMU_NOTIFIER
Quoted 195 lines · Sean Christopherson
Signed-off-by Sean Christopherson <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 2 -- arch/arm64/kvm/Kconfig | 2 +- arch/mips/include/asm/kvm_host.h | 2 -- arch/mips/kvm/Kconfig | 2 +- arch/powerpc/include/asm/kvm_host.h | 2 -- arch/powerpc/kvm/Kconfig | 8 ++++---- arch/powerpc/kvm/powerpc.c | 4 +--- arch/riscv/include/asm/kvm_host.h | 2 -- arch/riscv/kvm/Kconfig | 2 +- arch/x86/include/asm/kvm_host.h | 2 -- arch/x86/kvm/Kconfig | 2 +- include/linux/kvm_host.h | 8 +++++--- virt/kvm/Kconfig | 4 ++++ virt/kvm/kvm_main.c | 10 +++++----- 14 files changed, 23 insertions(+), 29 deletions(-)arch/arm64/include/asm/kvm_host.h +0
-2@@ -912,8 +912,6 @@ int __kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,int __kvm_arm_vcpu_set_events (struct kvm_vcpu *vcpu,struct kvm_vcpu_events *events); -#define KVM_ARCH_WANT_MMU_NOTIFIER -void kvm_arm_halt_guest (struct kvm *kvm);void kvm_arm_resume_guest (struct kvm *kvm);arch/arm64/kvm/Kconfig +1
-1@@ -22,7 +22,7 @@ menuconfig KVM bool "Kernel-based Virtual Machine (KVM) support" depends on HAVE_KVM select KVM_GENERIC_HARDWARE_ENABLING - select MMU_NOTIFIER + select KVM_GENERIC_MMU_NOTIFIER select PREEMPT_NOTIFIERS select HAVE_KVM_CPU_RELAX_INTERCEPT select HAVE_KVM_ARCH_TLB_FLUSH_ALLarch/mips/include/asm/kvm_host.h +0
-2@@ -810,8 +810,6 @@ int kvm_mips_mkclean_gpa_pt(struct kvm *kvm, gfn_t start_gfn, gfn_t end_gfn);pgd_t *kvm_pgd_alloc (void );void kvm_mmu_free_memory_caches (struct kvm_vcpu *vcpu); -#define KVM_ARCH_WANT_MMU_NOTIFIER -/* Emulation */ enum emulation_result update_pc (struct kvm_vcpu *vcpu,u32 cause);int kvm_get_badinstr (u32 *opc,struct kvm_vcpu *vcpu,u32 *out);arch/mips/kvm/Kconfig +1
-1@@ -25,7 +25,7 @@ config KVM select HAVE_KVM_EVENTFD select HAVE_KVM_VCPU_ASYNC_IOCTL select KVM_MMIO - select MMU_NOTIFIER + select KVM_GENERIC_MMU_NOTIFIER select INTERVAL_TREE select KVM_GENERIC_HARDWARE_ENABLING helparch/powerpc/include/asm/kvm_host.h +0
-2@@ -62,8 +62,6 @@#include <linux/mmu_notifier.h> -#define KVM_ARCH_WANT_MMU_NOTIFIER -#define HPTEG_CACHE_NUM (1 << 15)#define HPTEG_HASH_BITS_PTE 13#define HPTEG_HASH_BITS_PTE_LONG 12arch/powerpc/kvm/Kconfig +4
-4@@ -42,7 +42,7 @@ config KVM_BOOK3S_64_HANDLER config KVM_BOOK3S_PR_POSSIBLE bool select KVM_MMIO - select MMU_NOTIFIER + select KVM_GENERIC_MMU_NOTIFIER config KVM_BOOK3S_HV_POSSIBLE bool@@ -85,7 +85,7 @@ config KVM_BOOK3S_64_HV tristate "KVM for POWER7 and later using hypervisor mode in host" depends on KVM_BOOK3S_64 && PPC_POWERNV select KVM_BOOK3S_HV_POSSIBLE - select MMU_NOTIFIER + select KVM_GENERIC_MMU_NOTIFIER select CMA help Support running unmodified book3s_64 guest kernels in@@ -194,7 +194,7 @@ config KVM_E500V2 depends on !CONTEXT_TRACKING_USER select KVM select KVM_MMIO - select MMU_NOTIFIER + select KVM_GENERIC_MMU_NOTIFIER help Support running unmodified E500 guest kernels in virtual machines on E500v2 host processors.@@ -211,7 +211,7 @@ config KVM_E500MC select KVM select KVM_MMIO select KVM_BOOKE_HV - select MMU_NOTIFIER + select KVM_GENERIC_MMU_NOTIFIER help Support running unmodified E500MC/E5500/E6500 guest kernels in virtual machines on E500MC/E5500/E6500 host processors.arch/powerpc/kvm/powerpc.c +1
-3@@ -635,9 +635,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE r = hv_enabled;#else -#ifndef KVM_ARCH_WANT_MMU_NOTIFIER -BUILD_BUG (); -#endif +BUILD_BUG_ON (!IS_ENABLED (CONFIG_KVM_GENERIC_MMU_NOTIFIER )); r =1 ;#endif break ;arch/riscv/include/asm/kvm_host.h +0
-2@@ -249,8 +249,6 @@ struct kvm_vcpu_arch {static inline void kvm_arch_sync_events (struct kvm *kvm) {}static inline void kvm_arch_sched_in (struct kvm_vcpu *vcpu,int cpu) {} -#define KVM_ARCH_WANT_MMU_NOTIFIER -#define KVM_RISCV_GSTAGE_TLB_MIN_ORDER 12 void kvm_riscv_local_hfence_gvma_vmid_gpa(unsigned long vmid,arch/riscv/kvm/Kconfig +1
-1@@ -30,7 +30,7 @@ config KVM select KVM_GENERIC_HARDWARE_ENABLING select KVM_MMIO select KVM_XFER_TO_GUEST_WORK - select MMU_NOTIFIER + select KVM_GENERIC_MMU_NOTIFIER select PREEMPT_NOTIFIERS help Support hosting virtualized guest machines.arch/x86/include/asm/kvm_host.h +0
-2@@ -2110,8 +2110,6 @@ enum {# define kvm_memslots_for_spte_role (kvm, role) __kvm_memslots(kvm, 0)#endif -#define KVM_ARCH_WANT_MMU_NOTIFIER -int kvm_cpu_has_injectable_intr (struct kvm_vcpu *v);int kvm_cpu_has_interrupt (struct kvm_vcpu *vcpu);int kvm_cpu_has_extint (struct kvm_vcpu *v);arch/x86/kvm/Kconfig +1
-1@@ -24,7 +24,7 @@ config KVM depends on HIGH_RES_TIMERS depends on X86_LOCAL_APIC select PREEMPT_NOTIFIERS - select MMU_NOTIFIER + select KVM_GENERIC_MMU_NOTIFIER select HAVE_KVM_IRQCHIP select HAVE_KVM_PFNCACHE select HAVE_KVM_IRQFDinclude/linux/kvm_host.h +0
-0
include/linux/kvm_host.h bool kvm_setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, · line 258
Quoted 5 lines
include/linux/kvm_host.h +1
-1@@ -255,7 +255,9 @@ bool kvm_setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,int kvm_async_pf_wakeup_all (struct kvm_vcpu *vcpu);#endif -#ifdef KVM_ARCH_WANT_MMU_NOTIFIER +struct kvm_gfn_range ;
Not sure why a declaration here, it's
defined for ARCHs which defined KVM_ARCH_WANT_MMU_NOTIFIER before.
Quoted 86 lines
include/linux/kvm_host.h +4
-2+ +#ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER struct kvm_gfn_range { struct kvm_memory_slot *slot; gfn_t start;@@ -784,7 +786,7 @@ struct kvm {struct hlist_head irq_ack_notifier_list;#endif -#if defined(CONFIG_MMU_NOTIFIER ) && defined(KVM_ARCH_WANT_MMU_NOTIFIER ) +#ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER struct mmu_notifier mmu_notifier;unsigned long mmu_invalidate_seq;long mmu_invalidate_in_progress;@@ -1916,7 +1918,7 @@ extern const struct _kvm_stats_desc kvm_vm_stats_desc[];extern const struct kvm_stats_header kvm_vcpu_stats_header;extern const struct _kvm_stats_desc kvm_vcpu_stats_desc[]; -#if defined(CONFIG_MMU_NOTIFIER ) && defined(KVM_ARCH_WANT_MMU_NOTIFIER ) +#ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER static inline int mmu_invalidate_retry (struct kvm *kvm,unsigned long mmu_seq) {if (unlikely (kvm->mmu_invalidate_in_progress))virt/kvm/Kconfig +4
-0@@ -95,3 +95,7 @@ config HAVE_KVM_PM_NOTIFIER config KVM_GENERIC_HARDWARE_ENABLING bool + +config KVM_GENERIC_MMU_NOTIFIER + select MMU_NOTIFIER + boolvirt/kvm/kvm_main.c +5
-6@@ -510,7 +510,7 @@ void kvm_destroy_vcpus(struct kvm *kvm) }EXPORT_SYMBOL_GPL (kvm_destroy_vcpus); -#if defined(CONFIG_MMU_NOTIFIER ) && defined(KVM_ARCH_WANT_MMU_NOTIFIER ) +#ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER static inline struct kvm *mmu_notifier_to_kvm (struct mmu_notifier *mn) { return container_of(mn, struct kvm, mmu_notifier);@@ -938,14 +938,14 @@ static int kvm_init_mmu_notifier(struct kvm *kvm)return mmu_notifier_register (&kvm->mmu_notifier, current->mm); } -#else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */ +#else /* !CONFIG_KVM_GENERIC_MMU_NOTIFIER */ static int kvm_init_mmu_notifier (struct kvm *kvm) {return 0 ; } -#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */ +#endif /* CONFIG_KVM_GENERIC_MMU_NOTIFIER */ #ifdef CONFIG_HAVE_KVM_PM_NOTIFIER static int kvm_pm_notifier_call(struct notifier_block *bl,@@ -1265,7 +1265,7 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname) out_err_no_debugfs:kvm_coalesced_mmio_free (kvm); out_no_coalesced_mmio: -#if defined(CONFIG_MMU_NOTIFIER ) && defined(KVM_ARCH_WANT_MMU_NOTIFIER ) +#ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER if (kvm->mmu_notifier.ops)mmu_notifier_unregister (&kvm->mmu_notifier, current->mm);#endif @@ -1325,7 +1325,7 @@ static void kvm_destroy_vm(struct kvm *kvm) kvm->buses[i] =NULL ; }kvm_coalesced_mmio_free (kvm); -#if defined(CONFIG_MMU_NOTIFIER ) && defined(KVM_ARCH_WANT_MMU_NOTIFIER ) +#ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER mmu_notifier_unregister (&kvm->mmu_notifier, kvm->mm);/* * At this point, pending calls to invalidate_range_start() -- 2.41.0.255.g8b1d071c50-goog
Sean Christopherson
maintainerRe: [RFC PATCH v11 05/29] KVM: Convert KVM_ARCH_WANT_MMU_NOTIFIER to CONFIG_KVM_GENERIC_MMU_NOTIFIER
Quoted 9 lines · Yuan Yao
Quoted 8 lines · Sean Christopherson
include/linux/kvm_host.h +0
-0@@ -255,7 +255,9 @@ bool kvm_setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,int kvm_async_pf_wakeup_all (struct kvm_vcpu *vcpu);#endif
Quoted 5 lines
include/linux/kvm_host.h bool kvm_setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, · line 258
Quoted 2 lines
include/linux/kvm_host.h +1
-1-#ifdef KVM_ARCH_WANT_MMU_NOTIFIER +struct kvm_gfn_range ;Not sure why a declaration here, it's defined for ARCHs which defined
KVM_ARCH_WANT_MMU_NOTIFIERbefore.
The forward declaration exists to handle cases where CONFIG_KVM=n, specifically arch/powerpc/include/asm/kvm_ppc.h's declaration of hooks to forward calls to uarch modules:
bool (*unmap_gfn_range)(struct kvm *kvm, struct kvm_gfn_range *range); bool (*age_gfn)(struct kvm *kvm, struct kvm_gfn_range *range); bool (*test_age_gfn)(struct kvm *kvm, struct kvm_gfn_range *range); bool (*set_spte_gfn)(struct kvm *kvm, struct kvm_gfn_range *range);
Prior to using a Kconfig, a forward declaration wasn't necessary because arch/powerpc/include/asm/kvm_host.h would #define KVM_ARCH_WANT_MMU_NOTIFIER even if CONFIG_KVM=n.
Alternatively, kvm_ppc.h could declare the struct. I went this route mainly to avoid the possibility of someone encountering the same problem on a different architecture.
Re: [RFC PATCH v11 05/29] KVM: Convert KVM_ARCH_WANT_MMU_NOTIFIER to CONFIG_KVM_GENERIC_MMU_NOTIFIER
Quoted 25 lines · Sean Christopherson
Quoted 9 lines · Yuan Yao
Quoted 8 lines · Sean Christopherson
include/linux/kvm_host.h +0
-0@@ -255,7 +255,9 @@ bool kvm_setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,int kvm_async_pf_wakeup_all (struct kvm_vcpu *vcpu);#endif Quoted 5 lines
include/linux/kvm_host.h bool kvm_setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, · line 258
Quoted 2 lines
include/linux/kvm_host.h +1
-1-#ifdef KVM_ARCH_WANT_MMU_NOTIFIER +struct kvm_gfn_range ;Not sure why a declaration here, it's defined for ARCHs which defined
KVM_ARCH_WANT_MMU_NOTIFIERbefore.The forward declaration exists to handle cases where
CONFIG_KVM=n, specifically arch/powerpc/include/asm/kvm_ppc.h's declaration of hooks to forward calls to uarch modules:bool (*unmap_gfn_range)(struct kvm *kvm, struct kvm_gfn_range *range); bool (*age_gfn)(struct kvm *kvm, struct kvm_gfn_range *range); bool (*test_age_gfn)(struct kvm *kvm, struct kvm_gfn_range *range); bool (*set_spte_gfn)(struct kvm *kvm, struct kvm_gfn_range *range);
Quoted 7 lines
Prior to using a
Kconfig, a forward declaration wasn't necessary becausearch/powerpc/include/asm/kvm_host.hwould #defineKVM_ARCH_WANT_MMU_NOTIFIEReven ifCONFIG_KVM=n.Alternatively,
kvm_ppc.hcould declare the struct. I went this route mainly to avoid the possibility of someone encountering the same problem on a different architecture.
Ah I see, thanks for your explanation!
Sean Christopherson
maintainerRFC PATCH v11 6/29 KVM: Introduce KVM_SET_USER_MEMORY_REGION2
In reply to Sean Christopherson--- arch/x86/kvm/x86.c | 2 +- include/linux/kvm_host.h | 4 ++-- include/uapi/linux/kvm.h | 13 +++++++++++++ virt/kvm/kvm_main.c | 38 ++++++++++++++++++++++++++++++-------- 4 files changed, 46 insertions(+), 11 deletions(-)
arch/x86/kvm/x86.c +1 -1
@@ -12420,7 +12420,7 @@ void __user * __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, }for (i =0 ; i <KVM_ADDRESS_SPACE_NUM ; i++) { -struct kvm_userspace_memory_region m; +struct kvm_userspace_memory_region2 m; m.slot = id | (i <<16 ); m.flags =0 ;
include/linux/kvm_host.h +2 -2
@@ -1130,9 +1130,9 @@ enum kvm_mr_change { };int kvm_set_memory_region (struct kvm *kvm, -const struct kvm_userspace_memory_region *mem); +const struct kvm_userspace_memory_region2 *mem);int __kvm_set_memory_region (struct kvm *kvm, -const struct kvm_userspace_memory_region *mem); +const struct kvm_userspace_memory_region2 *mem);void kvm_arch_free_memslot (struct kvm *kvm,struct kvm_memory_slot *slot);void kvm_arch_memslots_updated (struct kvm *kvm,u64 gen); int kvm_arch_prepare_memory_region(struct kvm *kvm,
include/uapi/linux/kvm.h +13 -0
@@ -95,6 +95,16 @@ struct kvm_userspace_memory_region {__u64 userspace_addr;/* start of the userspace allocated memory */ }; +/* for KVM_SET_USER_MEMORY_REGION2 */ +struct kvm_userspace_memory_region2 { +__u32 slot; +__u32 flags; +__u64 guest_phys_addr; +__u64 memory_size; +__u64 userspace_addr; +__u64 pad[16 ]; +}; +/* * The bit 0 ~ bit 15 of kvm_userspace_memory_region::flags are visible for * userspace, other bits are reserved for kvm internal use which are defined
@@ -1192,6 +1202,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_COUNTER_OFFSET 227#define KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE 228#define KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES 229 +#define KVM_CAP_USER_MEMORY2 230#ifdef KVM_CAP_IRQ_ROUTING
@@ -1466,6 +1477,8 @@ struct kvm_vfio_spapr_tce { struct kvm_userspace_memory_region)#define KVM_SET_TSS_ADDR _IO(KVMIO, 0x47)#define KVM_SET_IDENTITY_MAP_ADDR _IOW(KVMIO, 0x48, __u64) +#define KVM_SET_USER_MEMORY_REGION2 _IOW(KVMIO, 0x49, \ + struct kvm_userspace_memory_region2)/* enable ucontrol for s390 */ struct kvm_s390_ucas_mapping {
virt/kvm/kvm_main.c +30 -8
@@ -1549,7 +1549,7 @@ static void kvm_replace_memslot(struct kvm *kvm, } } -static int check_memory_region_flags (const struct kvm_userspace_memory_region *mem) +static int check_memory_region_flags (const struct kvm_userspace_memory_region2 *mem) {u32 valid_flags =KVM_MEM_LOG_DIRTY_PAGES ;
@@ -1951,7 +1951,7 @@ static bool kvm_check_memslot_overlap(struct kvm_memslots *slots, int id,* Must be called holding kvm->slots_lock for write. */ int __kvm_set_memory_region (struct kvm *kvm, -const struct kvm_userspace_memory_region *mem) +const struct kvm_userspace_memory_region2 *mem) {struct kvm_memory_slot *old, *new;struct kvm_memslots *slots;
@@ -2055,7 +2055,7 @@ int __kvm_set_memory_region(struct kvm *kvm,EXPORT_SYMBOL_GPL (__kvm_set_memory_region);int kvm_set_memory_region (struct kvm *kvm, -const struct kvm_userspace_memory_region *mem) +const struct kvm_userspace_memory_region2 *mem) {int r;
@@ -2067,7 +2067,7 @@ int kvm_set_memory_region(struct kvm *kvm,EXPORT_SYMBOL_GPL (kvm_set_memory_region);static int kvm_vm_ioctl_set_memory_region (struct kvm *kvm, -struct kvm_userspace_memory_region *mem) +struct kvm_userspace_memory_region2 *mem) {if ((u16 )mem->slot >=KVM_USER_MEM_SLOTS )return -EINVAL ;
@@ -4514,6 +4514,7 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg) {switch (arg) {case KVM_CAP_USER_MEMORY : +case KVM_CAP_USER_MEMORY2 :case KVM_CAP_DESTROY_MEMORY_REGION_WORKS :case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS :case KVM_CAP_INTERNAL_ERROR_DATA :
@@ -4757,6 +4758,14 @@ static int kvm_vm_ioctl_get_stats_fd(struct kvm *kvm)return fd; } +#define SANITY_CHECK_MEM_REGION_FIELD (field) \ +do { \ + BUILD_BUG_ON(offsetof(struct kvm_userspace_memory_region, field) != \ + offsetof(struct kvm_userspace_memory_region2, field)); \ + BUILD_BUG_ON(sizeof_field(struct kvm_userspace_memory_region, field) != \ + sizeof_field(struct kvm_userspace_memory_region2, field)); \ +} while (0) +static long kvm_vm_ioctl (struct file *filp,unsigned int ioctl,unsigned long arg) {
@@ -4779,15 +4788,28 @@ static long kvm_vm_ioctl(struct file *filp, r =kvm_vm_ioctl_enable_cap_generic (kvm, &cap);break ; } +case KVM_SET_USER_MEMORY_REGION2 :case KVM_SET_USER_MEMORY_REGION : { -struct kvm_userspace_memory_region kvm_userspace_mem; +struct kvm_userspace_memory_region2 mem; +unsigned long size; + +if (ioctl ==KVM_SET_USER_MEMORY_REGION ) + size =sizeof (struct kvm_userspace_memory_region ); +else + size =sizeof (struct kvm_userspace_memory_region2 ); + +/* Ensure the common parts of the two structs are identical. */ +SANITY_CHECK_MEM_REGION_FIELD (slot); +SANITY_CHECK_MEM_REGION_FIELD (flags); +SANITY_CHECK_MEM_REGION_FIELD (guest_phys_addr); +SANITY_CHECK_MEM_REGION_FIELD (memory_size); +SANITY_CHECK_MEM_REGION_FIELD (userspace_addr); r = -EFAULT ; -if (copy_from_user (&kvm_userspace_mem, argp, -sizeof (kvm_userspace_mem))) +if (copy_from_user (&mem, argp, size)) goto out; - r =kvm_vm_ioctl_set_memory_region (kvm, &kvm_userspace_mem); + r =kvm_vm_ioctl_set_memory_region (kvm, &mem);break ; }case KVM_GET_DIRTY_LOG : {
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Re: [RFC PATCH v11 06/29] KVM: Introduce KVM_SET_USER_MEMORY_REGION2
Quoted 162 lines · Sean Christopherson
Cc Jarkko Sakkinen <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/x86.c | 2 +- include/linux/kvm_host.h | 4 ++-- include/uapi/linux/kvm.h | 13 +++++++++++++ virt/kvm/kvm_main.c | 38 ++++++++++++++++++++++++++++++-------- 4 files changed, 46 insertions(+), 11 deletions(-)arch/x86/kvm/x86.c +1
-1@@ -12420,7 +12420,7 @@ void __user * __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, }for (i =0 ; i <KVM_ADDRESS_SPACE_NUM ; i++) { -struct kvm_userspace_memory_region m; +struct kvm_userspace_memory_region2 m; m.slot = id | (i <<16 ); m.flags =0 ;include/linux/kvm_host.h +2
-2@@ -1130,9 +1130,9 @@ enum kvm_mr_change { };int kvm_set_memory_region (struct kvm *kvm, -const struct kvm_userspace_memory_region *mem); +const struct kvm_userspace_memory_region2 *mem);int __kvm_set_memory_region (struct kvm *kvm, -const struct kvm_userspace_memory_region *mem); +const struct kvm_userspace_memory_region2 *mem);void kvm_arch_free_memslot (struct kvm *kvm,struct kvm_memory_slot *slot);void kvm_arch_memslots_updated (struct kvm *kvm,u64 gen); int kvm_arch_prepare_memory_region(struct kvm *kvm,include/uapi/linux/kvm.h +13
-0@@ -95,6 +95,16 @@ struct kvm_userspace_memory_region {__u64 userspace_addr;/* start of the userspace allocated memory */ }; +/* for KVM_SET_USER_MEMORY_REGION2 */ +struct kvm_userspace_memory_region2 { +__u32 slot; +__u32 flags; +__u64 guest_phys_addr; +__u64 memory_size; +__u64 userspace_addr; +__u64 pad[16 ]; +}; +/* * The bit 0 ~ bit 15 of kvm_userspace_memory_region::flags are visible for * userspace, other bits are reserved for kvm internal use which are defined @@ -1192,6 +1202,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_COUNTER_OFFSET 227#define KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE 228#define KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES 229 +#define KVM_CAP_USER_MEMORY2 230#ifdef KVM_CAP_IRQ_ROUTING @@ -1466,6 +1477,8 @@ struct kvm_vfio_spapr_tce { struct kvm_userspace_memory_region)#define KVM_SET_TSS_ADDR _IO(KVMIO, 0x47)#define KVM_SET_IDENTITY_MAP_ADDR _IOW(KVMIO, 0x48, __u64) +#define KVM_SET_USER_MEMORY_REGION2 _IOW(KVMIO, 0x49, \ + struct kvm_userspace_memory_region2)/* enable ucontrol for s390 */ struct kvm_s390_ucas_mapping {virt/kvm/kvm_main.c +28
-5@@ -1549,7 +1549,7 @@ static void kvm_replace_memslot(struct kvm *kvm, } } -static int check_memory_region_flags (const struct kvm_userspace_memory_region *mem) +static int check_memory_region_flags (const struct kvm_userspace_memory_region2 *mem) {u32 valid_flags =KVM_MEM_LOG_DIRTY_PAGES ;@@ -1951,7 +1951,7 @@ static bool kvm_check_memslot_overlap(struct kvm_memslots *slots, int id,* Must be called holding kvm->slots_lock for write. */ int __kvm_set_memory_region (struct kvm *kvm, -const struct kvm_userspace_memory_region *mem) +const struct kvm_userspace_memory_region2 *mem) {struct kvm_memory_slot *old, *new;struct kvm_memslots *slots;@@ -2055,7 +2055,7 @@ int __kvm_set_memory_region(struct kvm *kvm,EXPORT_SYMBOL_GPL (__kvm_set_memory_region);int kvm_set_memory_region (struct kvm *kvm, -const struct kvm_userspace_memory_region *mem) +const struct kvm_userspace_memory_region2 *mem) {int r;@@ -2067,7 +2067,7 @@ int kvm_set_memory_region(struct kvm *kvm,EXPORT_SYMBOL_GPL (kvm_set_memory_region);static int kvm_vm_ioctl_set_memory_region (struct kvm *kvm, -struct kvm_userspace_memory_region *mem) +struct kvm_userspace_memory_region2 *mem) {if ((u16 )mem->slot >=KVM_USER_MEM_SLOTS )return -EINVAL ;@@ -4514,6 +4514,7 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg) {switch (arg) {case KVM_CAP_USER_MEMORY : +case KVM_CAP_USER_MEMORY2 :case KVM_CAP_DESTROY_MEMORY_REGION_WORKS :case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS :case KVM_CAP_INTERNAL_ERROR_DATA :@@ -4757,6 +4758,14 @@ static int kvm_vm_ioctl_get_stats_fd(struct kvm *kvm)return fd; } +#define SANITY_CHECK_MEM_REGION_FIELD (field) \ +do { \ + BUILD_BUG_ON(offsetof(struct kvm_userspace_memory_region, field) != \ + offsetof(struct kvm_userspace_memory_region2, field)); \ + BUILD_BUG_ON(sizeof_field(struct kvm_userspace_memory_region, field) != \ + sizeof_field(struct kvm_userspace_memory_region2, field)); \ +} while (0) +static long kvm_vm_ioctl (struct file *filp,unsigned int ioctl,unsigned long arg) {@@ -4779,15 +4788,28 @@ static long kvm_vm_ioctl(struct file *filp, r =kvm_vm_ioctl_enable_cap_generic (kvm, &cap);break ; } +case KVM_SET_USER_MEMORY_REGION2 :case KVM_SET_USER_MEMORY_REGION : { -struct kvm_userspace_memory_region kvm_userspace_mem; +struct kvm_userspace_memory_region2 mem; +unsigned long size; + +if (ioctl ==KVM_SET_USER_MEMORY_REGION ) + size =sizeof (struct kvm_userspace_memory_region ); +else + size =sizeof (struct kvm_userspace_memory_region2 ); + +/* Ensure the common parts of the two structs are identical. */ +SANITY_CHECK_MEM_REGION_FIELD (slot); +SANITY_CHECK_MEM_REGION_FIELD (flags); +SANITY_CHECK_MEM_REGION_FIELD (guest_phys_addr); +SANITY_CHECK_MEM_REGION_FIELD (memory_size); +SANITY_CHECK_MEM_REGION_FIELD (userspace_addr); r = -EFAULT ;
virt/kvm/kvm_main.c static long kvm_vm_ioctl(struct file *filp, · line 4815
Quoted 10 lines
virt/kvm/kvm_main.c +2
-3-if (copy_from_user (&kvm_userspace_mem, argp, -sizeof (kvm_userspace_mem))) +if (copy_from_user (&mem, argp, size)) goto out; - r =kvm_vm_ioctl_set_memory_region (kvm, &kvm_userspace_mem); + r =kvm_vm_ioctl_set_memory_region (kvm, &mem);break ; }case KVM_GET_DIRTY_LOG : {
Re: [RFC PATCH v11 06/29] KVM: Introduce KVM_SET_USER_MEMORY_REGION2
In reply to Sean ChristophersonQuoted 4 lines · Sean Christopherson
include/uapi/linux/kvm.h +0
-0@@ -95,6 +95,16 @@ struct kvm_userspace_memory_region {__u64 userspace_addr;/* start of the userspace allocated memory */
include/uapi/linux/kvm.h struct kvm_userspace_memory_region { · line 105
Quoted 10 lines
include/uapi/linux/kvm.h +8
-0}; +/* for KVM_SET_USER_MEMORY_REGION2 */ +struct kvm_userspace_memory_region2 { +__u32 slot; +__u32 flags; +__u64 guest_phys_addr; +__u64 memory_size; +__u64 userspace_addr; +__u64 pad[16 ];
Should we replace that pad[16] with:
__u64 size;
where 'size' is the size of the structure as seen by userspace? This is used in other UAPIs (see struct sched_attr for example) and is a bit more robust for future extensions (e.g. an 'old' kernel can correctly reject a newer version of the struct with additional fields it doesn't know about if that makes sense, etc).
Sean Christopherson
maintainerRe: [RFC PATCH v11 06/29] KVM: Introduce KVM_SET_USER_MEMORY_REGION2
Quoted 18 lines · Quentin Perret
Quoted 4 lines · Sean Christopherson
include/uapi/linux/kvm.h +0
-0@@ -95,6 +95,16 @@ struct kvm_userspace_memory_region {__u64 userspace_addr;/* start of the userspace allocated memory */ include/uapi/linux/kvm.h struct kvm_userspace_memory_region { · line 105
Quoted 10 lines
include/uapi/linux/kvm.h +8
-0}; +/* for KVM_SET_USER_MEMORY_REGION2 */ +struct kvm_userspace_memory_region2 { +__u32 slot; +__u32 flags; +__u64 guest_phys_addr; +__u64 memory_size; +__u64 userspace_addr; +__u64 pad[16 ];Should we replace that pad[16] with:
Quoted 7 lines
__u64 size;where 'size' is the size of the structure as seen by userspace? This is used in other UAPIs (see struct
sched_attrfor example) and is a bit more robust for future extensions (e.g. an 'old' kernel can correctly reject a newer version of the struct with additional fields it doesn't know about if that makes sense, etc).
"flags" serves that purpose, i.e. allows userspace to opt-in to having KVM actually consume what is currently just padding.
The padding is there mainly to simplify kernel/KVM code, e.g. the number of bytes that KVM needs to copy in is static.
But now that I think more on this, I don't know why we didn't just unconditionally bump the size of kvm_userspace_memory_region. We tried to play games with unions and overlays, but that was a mess[*].
KVM would need to do multiple uaccess reads, but that's not a big deal. Am I missing something, or did past us just get too clever and miss the obvious solution?
Re: [RFC PATCH v11 06/29] KVM: Introduce KVM_SET_USER_MEMORY_REGION2
Quoted 21 lines · Sean Christopherson
Quoted 20 lines · Quentin Perret
Quoted 4 lines · Sean Christopherson
include/uapi/linux/kvm.h +0
-0@@ -95,6 +95,16 @@ struct kvm_userspace_memory_region {__u64 userspace_addr;/* start of the userspace allocated memory */ include/uapi/linux/kvm.h struct kvm_userspace_memory_region { · line 105
Quoted 10 lines
include/uapi/linux/kvm.h +8
-0}; +/* for KVM_SET_USER_MEMORY_REGION2 */ +struct kvm_userspace_memory_region2 { +__u32 slot; +__u32 flags; +__u64 guest_phys_addr; +__u64 memory_size; +__u64 userspace_addr; +__u64 pad[16 ];Should we replace that pad[16] with:
__u64 size;
Quoted 8 lines
Quoted 5 lines
where 'size' is the size of the structure as seen by userspace? This is used in other UAPIs (see struct
sched_attrfor example) and is a bit more robust for future extensions (e.g. an 'old' kernel can correctly reject a newer version of the struct with additional fields it doesn't know about if that makes sense, etc)."flags" serves that purpose, i.e. allows userspace to opt-in to having KVM actually consume what is currently just padding.
Sure, I've just grown to dislike static padding of that type -- it ends up being either a waste a space, or is too small, while the 'superior' alternative (having a 'size' member) doesn't cost much and avoids those problems.
But no strong opinion really, this struct really shouldn't grow much, so I'm sure that'll be fine in practice.
Quoted 11 lines
The padding is there mainly to simplify kernel/KVM code, e.g. the number of bytes that KVM needs to copy in is static.
But now that I think more on this, I don't know why we didn't just unconditionally bump the size of
kvm_userspace_memory_region. We tried to play games with unions and overlays, but that was a mess[*].KVM would need to do multiple uaccess reads, but that's not a big deal. Am I missing something, or did past us just get too clever and miss the obvious solution?
Right, so the first uaccess would get_user() the flags, based on that we'd figure out the size of the struct, copy_from_user() what we need, and then sanity check the flags are the same from both reads, or something along those lines?
That doesn't sound too complicated to me, and as long as every extension to the struct does come with a new flag I can't immediately see what would go wrong.
Paolo Bonzini
maintainerRe: [RFC PATCH v11 06/29] KVM: Introduce KVM_SET_USER_MEMORY_REGION2
In reply to Sean ChristophersonQuoted 3 lines · Sean Christopherson
KVM would need to do multiple uaccess reads, but that's not a big deal. Am I missing something, or did past us just get too clever and miss the obvious solution?
You would have to introduce struct kvm_userspace_memory_region2 anyway, though not a new ioctl, for two reasons:
1) the current size of the struct is part of the userspace API via the KVM_SET_USER_MEMORY_REGION #define, so introducing a new struct is the easiest way to preserve this
2) the struct can (at least theoretically) enter the ABI of a shared library, and such mismatches are really hard to detect and resolve. So it's better to add the padding to a new struct, and keep struct kvm_userspace_memory_region backwards-compatible.
As to whether we should introduce a new ioctl: doing so makes KVM_SET_USER_MEMORY_REGION's detection of bad flags a bit more robust; it's not like we cannot introduce new flags at all, of course, but having out-of-bounds reads as a side effect of new flags is a bit nasty.
Protecting programs from their own bugs gets into diminishing returns
very quickly, but introducing a new ioctl can make exploits a bit harder when struct kvm_userspace_memory_region is on the stack and adjacent to an attacker-controlled location.
Paolo
Sean Christopherson
maintainerRFC PATCH v11 7/29 KVM: Add KVM_EXIT_MEMORY_FAULT exit
In reply to Sean ChristophersonFrom: Chao Peng <[email protected]>
This new KVM exit allows userspace to handle memory-related errors. It indicates an error happens in KVM at guest memory range [gpa, gpa+size). The flags includes additional information for userspace to handle the error. Currently bit 0 is defined as 'private memory' where '1' indicates error happens due to private memory access and '0' indicates error happens due to shared memory access.
When private memory is enabled, this new exit will be used for KVM to exit to userspace for shared <-> private memory conversion in memory encryption usage. In such usage, typically there are two kind of memory conversions:
- explicit conversion: happens when guest explicitly calls into KVM to map a range (as private or shared), KVM then exits to userspace to perform the map/unmap operations.
- implicit conversion: happens in KVM page fault handler where KVM exits to userspace for an implicit conversion when the page is in a different state than requested (private or shared).
--- Documentation/virt/kvm/api.rst | 22 ++++++++++++++++++++++ include/uapi/linux/kvm.h | 8 ++++++++ 2 files changed, 30 insertions(+)
Documentation/virt/kvm/api.rst +22 -0
@@ -6700,6 +6700,28 @@ array field represents return values. The userspace should update the return values of SBI call before resuming the VCPU. For more details on RISC-V SBI spec refer, https://github.com/riscv/riscv-sbi-doc. +:: + + /* KVM_EXIT_MEMORY_FAULT */ + struct { + #define KVM_MEMORY_EXIT_FLAG_PRIVATE (1ULL << 3) + __u64 flags; + __u64 gpa; + __u64 size; + } memory; + +If exit reason is KVM_EXIT_MEMORY_FAULT then it indicates that the VCPU has +encountered a memory error which is not handled by KVM kernel module and +userspace may choose to handle it. The 'flags' field indicates the memory +properties of the exit. + + - KVM_MEMORY_EXIT_FLAG_PRIVATE - indicates the memory error is caused by + private memory access when the bit is set. Otherwise the memory error is + caused by shared memory access when the bit is clear. + +'gpa' and 'size' indicate the memory range the error occurs at. The userspace +may handle the error and return to KVM to retry the previous memory access. + :: /* KVM_EXIT_NOTIFY */
include/uapi/linux/kvm.h +8 -0
@@ -274,6 +274,7 @@ struct kvm_xen_exit {#define KVM_EXIT_RISCV_SBI 35#define KVM_EXIT_RISCV_CSR 36#define KVM_EXIT_NOTIFY 37 +#define KVM_EXIT_MEMORY_FAULT 38/* For KVM_EXIT_INTERNAL_ERROR */ /* Emulate instruction failed. */
@@ -520,6 +521,13 @@ struct kvm_run {#define KVM_NOTIFY_CONTEXT_INVALID (1 << 0)__u32 flags; } notify; +/* KVM_EXIT_MEMORY_FAULT */ +struct { +#define KVM_MEMORY_EXIT_FLAG_PRIVATE (1ULL << 3) +__u64 flags; +__u64 gpa; +__u64 size; + } memory;/* Fix the size of the union. */ char padding[256 ]; };
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Re: [RFC PATCH v11 07/29] KVM: Add KVM_EXIT_MEMORY_FAULT exit
Quoted 8 lines · Sean Christopherson
From: Chao Peng <[email protected]>
This new KVM exit allows userspace to handle memory-related errors. It indicates an error happens in KVM at guest memory range [gpa, gpa+size). The flags includes additional information for userspace to handle the error. Currently bit 0 is defined as 'private memory' where '1' indicates error happens due to private memory access and '0' indicates error happens due to shared memory access.
Now it's bit 3:
#define KVM_MEMORY_EXIT_FLAG_PRIVATE (1ULL << 3)
I remember some other attributes were introduced in v10 yet:
#define KVM_MEMORY_ATTRIBUTE_READ (1ULL << 0) #define KVM_MEMORY_ATTRIBUTE_WRITE (1ULL << 1) #define KVM_MEMORY_ATTRIBUTE_EXECUTE (1ULL << 2) #define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3)
So KVM_MEMORY_EXIT_FLAG_PRIVATE changed to bit 3 due to above things, or other reason ? (Sorry I didn't follow v10 too much before).
Quoted 86 lines
When private memory is enabled, this new exit will be used for KVM to exit to userspace for shared <-> private memory conversion in memory encryption usage. In such usage, typically there are two kind of memory conversions:
- explicit conversion: happens when guest explicitly calls into KVM to map a range (as private or shared), KVM then exits to userspace to perform the map/unmap operations.
- implicit conversion: happens in KVM page fault handler where KVM exits to userspace for an implicit conversion when the page is in a different state than requested (private or shared).Suggested-by Sean Christopherson <[email protected]>Co-developed-by Yu Zhang <[email protected]>Signed-off-by Yu Zhang <[email protected]>Signed-off-by Chao Peng <[email protected]>Reviewed-by Fuad Tabba <[email protected]>Tested-by Fuad Tabba <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- Documentation/virt/kvm/api.rst | 22 ++++++++++++++++++++++ include/uapi/linux/kvm.h | 8 ++++++++ 2 files changed, 30 insertions(+)Documentation/virt/kvm/api.rst +22
-0@@ -6700,6 +6700,28 @@ array field represents return values. The userspace should update the return values of SBI call before resuming the VCPU. For more details on RISC-V SBI spec refer, https://github.com/riscv/riscv-sbi-doc. +:: + + /* KVM_EXIT_MEMORY_FAULT */ + struct { + #define KVM_MEMORY_EXIT_FLAG_PRIVATE (1ULL << 3) + __u64 flags; + __u64 gpa; + __u64 size; + } memory; + +If exit reason is KVM_EXIT_MEMORY_FAULT then it indicates that the VCPU has +encountered a memory error which is not handled by KVM kernel module and +userspace may choose to handle it. The 'flags' field indicates the memory +properties of the exit. + + - KVM_MEMORY_EXIT_FLAG_PRIVATE - indicates the memory error is caused by + private memory access when the bit is set. Otherwise the memory error is + caused by shared memory access when the bit is clear. + +'gpa' and 'size' indicate the memory range the error occurs at. The userspace +may handle the error and return to KVM to retry the previous memory access. + :: /* KVM_EXIT_NOTIFY */include/uapi/linux/kvm.h +8
-1@@ -274,6 +274,7 @@ struct kvm_xen_exit {#define KVM_EXIT_RISCV_SBI 35#define KVM_EXIT_RISCV_CSR 36#define KVM_EXIT_NOTIFY 37 +#define KVM_EXIT_MEMORY_FAULT 38/* For KVM_EXIT_INTERNAL_ERROR */ /* Emulate instruction failed. */ @@ -520,6 +521,13 @@ struct kvm_run {#define KVM_NOTIFY_CONTEXT_INVALID (1 << 0)__u32 flags; } notify; +/* KVM_EXIT_MEMORY_FAULT */ +struct { +#define KVM_MEMORY_EXIT_FLAG_PRIVATE (1ULL << 3) +__u64 flags; +__u64 gpa; +__u64 size; + } memory;/* Fix the size of the union. */ char padding[256 ]; }; --2.41.0.255.g8b1d071c50-goog
Sean Christopherson
maintainerRe: [RFC PATCH v11 07/29] KVM: Add KVM_EXIT_MEMORY_FAULT exit
Quoted 11 lines · Yuan Yao
Quoted 8 lines · Sean Christopherson
From: Chao Peng <[email protected]>
This new KVM exit allows userspace to handle memory-related errors. It indicates an error happens in KVM at guest memory range [gpa, gpa+size). The flags includes additional information for userspace to handle the error. Currently bit 0 is defined as 'private memory' where '1' indicates error happens due to private memory access and '0' indicates error happens due to shared memory access.
Now it's bit 3:
Yeah, I need to update (or write) a lot of changelogs.
Quoted 11 lines
#define
KVM_MEMORY_EXIT_FLAG_PRIVATE(1ULL << 3)I remember some other attributes were introduced in v10 yet:
#define KVM_MEMORY_ATTRIBUTE_READ (1ULL << 0) #define KVM_MEMORY_ATTRIBUTE_WRITE (1ULL << 1) #define KVM_MEMORY_ATTRIBUTE_EXECUTE (1ULL << 2) #define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3)So
KVM_MEMORY_EXIT_FLAG_PRIVATEchanged to bit 3 due to above things, or other reason ? (Sorry I didn't follow v10 too much before).
Yep, I want to reserve space for the RWX bits.
Sean Christopherson
maintainerRFC PATCH v11 8/29 KVM: Introduce per-page memory attributes
In reply to Sean ChristophersonFrom: Chao Peng <[email protected]>
In confidential computing usages, whether a page is private or shared is necessary information for KVM to perform operations like page fault handling, page zapping etc. There are other potential use cases for per-page memory attributes, e.g. to make memory read-only (or no-exec, or exec-only, etc.) without having to modify memslots.
Introduce two ioctls (advertised by KVM_CAP_MEMORY_ATTRIBUTES) to allow userspace to operate on the per-page memory attributes.
- KVM_SET_MEMORY_ATTRIBUTES to set the per-page memory attributes to a guest memory range.
- KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES to return the KVM supported memory attributes.
Use an xarray to store the per-page attributes internally, with a naive, not fully optimized implementation, i.e. prioritize correctness over performance for the initial implementation.
Because setting memory attributes is roughly analogous to mprotect() on memory that is mapped into the guest, zap existing mappings prior to updating the memory attributes. Opportunistically provide an arch hook for the post-set path (needed to complete invalidation anyways) in anticipation of x86 needing the hook to update metadata related to determining whether or not a given gfn can be backed with various sizes of hugepages.
It's possible that future usages may not require an invalidation, e.g. if KVM ends up supporting RWX protections and userspace grants _more_ protections, but again opt for simplicity and punt optimizations to if/when they are needed.
--- Documentation/virt/kvm/api.rst | 60 ++++++++++++ include/linux/kvm_host.h | 14 +++ include/uapi/linux/kvm.h | 14 +++ virt/kvm/Kconfig | 4 + virt/kvm/kvm_main.c | 170 +++++++++++++++++++++++++++++++++ 5 files changed, 262 insertions(+)
Documentation/virt/kvm/api.rst +60 -0
@@ -6068,6 +6068,56 @@ writes to the CNTVCT_EL0 and CNTPCT_EL0 registers using the SET_ONE_REG interface. No error will be returned, but the resulting offset will not be applied. +4.139 KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES +----------------------------------------- + +:Capability: KVM_CAP_MEMORY_ATTRIBUTES +:Architectures: x86 +:Type: vm ioctl +:Parameters: u64 memory attributes bitmask(out) +:Returns: 0 on success, <0 on error + +Returns supported memory attributes bitmask. Supported memory attributes will +have the corresponding bits set in u64 memory attributes bitmask. + +The following memory attributes are defined:: + + #define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3) + +4.140 KVM_SET_MEMORY_ATTRIBUTES +----------------------------------------- + +:Capability: KVM_CAP_MEMORY_ATTRIBUTES +:Architectures: x86 +:Type: vm ioctl +:Parameters: struct kvm_memory_attributes(in/out) +:Returns: 0 on success, <0 on error + +Sets memory attributes for pages in a guest memory range. Parameters are +specified via the following structure:: + + struct kvm_memory_attributes { + __u64 address; + __u64 size; + __u64 attributes; + __u64 flags; + }; + +The user sets the per-page memory attributes to a guest memory range indicated +by address/size, and in return KVM adjusts address and size to reflect the +actual pages of the memory range have been successfully set to the attributes. +If the call returns 0, "address" is updated to the last successful address + 1 +and "size" is updated to the remaining address size that has not been set +successfully. The user should check the return value as well as the size to +decide if the operation succeeded for the whole range or not. The user may want +to retry the operation with the returned address/size if the previous range was +partially successful. + +Both address and size should be page aligned and the supported attributes can be +retrieved with KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES. + +The "flags" field may be used for future extensions and should be set to 0s. + 5. The kvm_run structure ========================
@@ -8494,6 +8544,16 @@ block sizes is exposed in KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES as a 64-bit bitmap (each bit describing a block size). The default value is 0, to disable the eager page splitting. +8.41 KVM_CAP_MEMORY_ATTRIBUTES +------------------------------ + +:Capability: KVM_CAP_MEMORY_ATTRIBUTES +:Architectures: x86 +:Type: vm + +This capability indicates KVM supports per-page memory attributes and ioctls +KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES/KVM_SET_MEMORY_ATTRIBUTES are available. + 9. Known KVM API problems =========================
include/linux/kvm_host.h +14 -0
@@ -264,6 +264,7 @@ struct kvm_gfn_range {gfn_t end;union {pte_t pte; +unsigned long attributes;u64 raw; } arg;bool may_block;
@@ -809,6 +810,9 @@ struct kvm {#ifdef CONFIG_HAVE_KVM_PM_NOTIFIER struct notifier_block pm_notifier; +#endif +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +struct xarray mem_attr_array;#endif char stats_id[KVM_STATS_NAME_SIZE ]; };
@@ -2301,4 +2305,14 @@ static inline void kvm_account_pgtable_pages(void *virt, int nr)/* Max number of entries allowed for each kvm dirty ring */ #define KVM_DIRTY_RING_MAX_ENTRIES 65536 +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +static inline unsigned long kvm_get_memory_attributes (struct kvm *kvm,gfn_t gfn) +{ +return xa_to_value (xa_load (&kvm->mem_attr_array, gfn)); +} + +bool kvm_arch_post_set_memory_attributes (struct kvm *kvm, +struct kvm_gfn_range *range); +#endif /* CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES */ +#endif
include/uapi/linux/kvm.h +14 -0
@@ -1211,6 +1211,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE 228#define KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES 229#define KVM_CAP_USER_MEMORY2 230 +#define KVM_CAP_MEMORY_ATTRIBUTES 231#ifdef KVM_CAP_IRQ_ROUTING
@@ -2270,4 +2271,17 @@ struct kvm_s390_zpci_op {/* flags for kvm_s390_zpci_op->u.reg_aen.flags */ #define KVM_S390_ZPCIOP_REGAEN_HOST (1 << 0) +/* Available with KVM_CAP_MEMORY_ATTRIBUTES */ +#define KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES _IOR(KVMIO, 0xd2, __u64) +#define KVM_SET_MEMORY_ATTRIBUTES _IOW(KVMIO, 0xd3, struct kvm_memory_attributes) + +struct kvm_memory_attributes { +__u64 address; +__u64 size; +__u64 attributes; +__u64 flags; +}; + +#define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3) +#endif /* __LINUX_KVM_H */
virt/kvm/Kconfig +4 -0
@@ -99,3 +99,7 @@ config KVM_GENERIC_HARDWARE_ENABLING config KVM_GENERIC_MMU_NOTIFIER select MMU_NOTIFIER bool + +config KVM_GENERIC_MEMORY_ATTRIBUTES + select KVM_GENERIC_MMU_NOTIFIER + bool
virt/kvm/kvm_main.c +170 -0
206 lines not on this page · raw message
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Re: [RFC PATCH v11 08/29] KVM: Introduce per-page memory attributes
Quoted 148 lines · Sean Christopherson
From: Chao Peng <[email protected]>
In confidential computing usages, whether a page is private or shared is necessary information for KVM to perform operations like page fault handling, page zapping etc. There are other potential use cases for per-page memory attributes, e.g. to make memory read-only (or no-exec, or exec-only, etc.) without having to modify memslots.
Introduce two ioctls (advertised by
KVM_CAP_MEMORY_ATTRIBUTES) to allow userspace to operate on the per-page memory attributes.
-KVM_SET_MEMORY_ATTRIBUTESto set the per-page memory attributes to a guest memory range.
-KVM_GET_SUPPORTED_MEMORY_ATTRIBUTESto return the KVM supported memory attributes.Use an xarray to store the per-page attributes internally, with a naive, not fully optimized implementation, i.e. prioritize correctness over performance for the initial implementation.
Because setting memory attributes is roughly analogous to
mprotect()on memory that is mapped into the guest, zap existing mappings prior to updating the memory attributes. Opportunistically provide an arch hook for the post-set path (needed to complete invalidation anyways) in anticipation of x86 needing the hook to update metadata related to determining whether or not a given gfn can be backed with various sizes of hugepages.It's possible that future usages may not require an invalidation, e.g. if KVM ends up supporting RWX protections and userspace grants _more_ protections, but again opt for simplicity and punt optimizations to if/when they are needed.
Suggested-by Sean Christopherson <[email protected]>Link https://lore.kernel.org/all/Y2WB48kD0J4VGynX at google.comCc Fuad Tabba <[email protected]>Signed-off-by Chao Peng <[email protected]>Co-developed-by Sean Christopherson <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- Documentation/virt/kvm/api.rst | 60 ++++++++++++ include/linux/kvm_host.h | 14 +++ include/uapi/linux/kvm.h | 14 +++ virt/kvm/Kconfig | 4 + virt/kvm/kvm_main.c | 170 +++++++++++++++++++++++++++++++++ 5 files changed, 262 insertions(+)Documentation/virt/kvm/api.rst +60
-0@@ -6068,6 +6068,56 @@ writes to the CNTVCT_EL0 and CNTPCT_EL0 registers using the SET_ONE_REG interface. No error will be returned, but the resulting offset will not be applied. +4.139 KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES +----------------------------------------- + +:Capability: KVM_CAP_MEMORY_ATTRIBUTES +:Architectures: x86 +:Type: vm ioctl +:Parameters: u64 memory attributes bitmask(out) +:Returns: 0 on success, <0 on error + +Returns supported memory attributes bitmask. Supported memory attributes will +have the corresponding bits set in u64 memory attributes bitmask. + +The following memory attributes are defined:: + + #define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3) + +4.140 KVM_SET_MEMORY_ATTRIBUTES +----------------------------------------- + +:Capability: KVM_CAP_MEMORY_ATTRIBUTES +:Architectures: x86 +:Type: vm ioctl +:Parameters: struct kvm_memory_attributes(in/out) +:Returns: 0 on success, <0 on error + +Sets memory attributes for pages in a guest memory range. Parameters are +specified via the following structure:: + + struct kvm_memory_attributes { + __u64 address; + __u64 size; + __u64 attributes; + __u64 flags; + }; + +The user sets the per-page memory attributes to a guest memory range indicated +by address/size, and in return KVM adjusts address and size to reflect the +actual pages of the memory range have been successfully set to the attributes. +If the call returns 0, "address" is updated to the last successful address + 1 +and "size" is updated to the remaining address size that has not been set +successfully. The user should check the return value as well as the size to +decide if the operation succeeded for the whole range or not. The user may want +to retry the operation with the returned address/size if the previous range was +partially successful. + +Both address and size should be page aligned and the supported attributes can be +retrieved with KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES. + +The "flags" field may be used for future extensions and should be set to 0s. + 5. The kvm_run structure ========================@@ -8494,6 +8544,16 @@ block sizes is exposed in KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES as a 64-bit bitmap (each bit describing a block size). The default value is 0, to disable the eager page splitting. +8.41 KVM_CAP_MEMORY_ATTRIBUTES +------------------------------ + +:Capability: KVM_CAP_MEMORY_ATTRIBUTES +:Architectures: x86 +:Type: vm + +This capability indicates KVM supports per-page memory attributes and ioctls +KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES/KVM_SET_MEMORY_ATTRIBUTES are available. + 9. Known KVM API problems =========================include/linux/kvm_host.h +4
-0@@ -264,6 +264,7 @@ struct kvm_gfn_range {gfn_t end;union {pte_t pte; +unsigned long attributes;u64 raw; } arg;bool may_block;@@ -809,6 +810,9 @@ struct kvm {#ifdef CONFIG_HAVE_KVM_PM_NOTIFIER struct notifier_block pm_notifier; +#endif +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +struct xarray mem_attr_array;#endif char stats_id[KVM_STATS_NAME_SIZE ]; };@@ -2301,4 +2305,14 @@ static inline void kvm_account_pgtable_pages(void *virt, int nr)/* Max number of entries allowed for each kvm dirty ring */
include/linux/kvm_host.h static inline void kvm_account_pgtable_pages(void *virt, int nr) · line 2315
Quoted 10 lines
include/linux/kvm_host.h +8
-0#define KVM_DIRTY_RING_MAX_ENTRIES 65536 +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +static inline unsigned long kvm_get_memory_attributes (struct kvm *kvm,gfn_t gfn) +{ +return xa_to_value (xa_load (&kvm->mem_attr_array, gfn)); +} + +bool kvm_arch_post_set_memory_attributes (struct kvm *kvm, +struct kvm_gfn_range *range);
Used but no definition in this patch, it's defined in next patch 09. How about add weak version in this patch and let ARCHs to overide it ?
Quoted 264 lines
include/linux/kvm_host.h +2
-0+#endif /* CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES */ +#endif include/uapi/linux/kvm.h +14
-0@@ -1211,6 +1211,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE 228#define KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES 229#define KVM_CAP_USER_MEMORY2 230 +#define KVM_CAP_MEMORY_ATTRIBUTES 231#ifdef KVM_CAP_IRQ_ROUTING @@ -2270,4 +2271,17 @@ struct kvm_s390_zpci_op {/* flags for kvm_s390_zpci_op->u.reg_aen.flags */ #define KVM_S390_ZPCIOP_REGAEN_HOST (1 << 0) +/* Available with KVM_CAP_MEMORY_ATTRIBUTES */ +#define KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES _IOR(KVMIO, 0xd2, __u64) +#define KVM_SET_MEMORY_ATTRIBUTES _IOW(KVMIO, 0xd3, struct kvm_memory_attributes) + +struct kvm_memory_attributes { +__u64 address; +__u64 size; +__u64 attributes; +__u64 flags; +}; + +#define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3) +#endif /* __LINUX_KVM_H */ virt/kvm/Kconfig +4
-0@@ -99,3 +99,7 @@ config KVM_GENERIC_HARDWARE_ENABLING config KVM_GENERIC_MMU_NOTIFIER select MMU_NOTIFIER bool + +config KVM_GENERIC_MEMORY_ATTRIBUTES + select KVM_GENERIC_MMU_NOTIFIER + boolvirt/kvm/kvm_main.c +170
-1@@ -530,6 +530,7 @@ struct kvm_mmu_notifier_range {u64 end;union {pte_t pte; +unsigned long attributes;u64 raw; } arg;gfn_handler_t handler;@@ -1175,6 +1176,9 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname)spin_lock_init (&kvm->mn_invalidate_lock);rcuwait_init (&kvm->mn_memslots_update_rcuwait);xa_init (&kvm->vcpu_array); +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +xa_init (&kvm->mem_attr_array); +#endif INIT_LIST_HEAD (&kvm->gpc_list);spin_lock_init (&kvm->gpc_lock);@@ -1346,6 +1350,9 @@ static void kvm_destroy_vm(struct kvm *kvm)kvm_free_memslots (kvm, &kvm->__memslots[i][0 ]);kvm_free_memslots (kvm, &kvm->__memslots[i][1 ]); } +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +xa_destroy (&kvm->mem_attr_array); +#endif cleanup_srcu_struct (&kvm->irq_srcu);cleanup_srcu_struct (&kvm->srcu);kvm_arch_free_vm (kvm);@@ -2346,6 +2353,145 @@ static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, }#endif /* CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT */ +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +static u64 kvm_supported_mem_attributes (struct kvm *kvm) +{ +return 0 ; +} + +static __always_inline void kvm_handle_gfn_range(struct kvm *kvm, +struct kvm_mmu_notifier_range *range) +{ +struct kvm_gfn_range gfn_range; +struct kvm_memory_slot *slot; +struct kvm_memslots *slots; +struct kvm_memslot_iter iter; +bool locked = false; +bool ret = false; +int i; + + gfn_range.arg.raw = range->arg.raw; + gfn_range.may_block = range->may_block; + +for (i =0 ; i <KVM_ADDRESS_SPACE_NUM ; i++) { + slots =__kvm_memslots (kvm, i); + +kvm_for_each_memslot_in_gfn_range (&iter, slots, range->start, range->end) { + slot = iter.slot; + gfn_range.slot = slot; + + gfn_range.start =max (range->start, slot->base_gfn); + gfn_range.end =min (range->end, slot->base_gfn + slot->npages); +if (gfn_range.start >= gfn_range.end) +continue ; + +if (!locked) { + locked = true; +KVM_MMU_LOCK (kvm); +if (!IS_KVM_NULL_FN (range->on_lock)) + range->on_lock (kvm); + } + + ret |= range->handler (kvm, &gfn_range); + } + } + +if (range->flush_on_ret && ret) +kvm_flush_remote_tlbs (kvm); + +if (locked) { +KVM_MMU_UNLOCK (kvm); +if (!IS_KVM_NULL_FN (range->on_unlock)) + range->on_unlock (kvm); + } +} + +static int kvm_vm_set_mem_attributes (struct kvm *kvm,unsigned long attributes, +gfn_t start,gfn_t end) +{ +struct kvm_mmu_notifier_range unmap_range = { + .start = start, + .end = end, + .handler = kvm_mmu_unmap_gfn_range, + .on_lock = kvm_mmu_invalidate_begin, + .on_unlock = (void *)kvm_null_fn, + .flush_on_ret = true, + .may_block = true, + }; +struct kvm_mmu_notifier_range post_set_range = { + .start = start, + .end = end, + .arg.attributes = attributes, + .handler = kvm_arch_post_set_memory_attributes, + .on_lock = (void *)kvm_null_fn, + .on_unlock = kvm_mmu_invalidate_end, + .may_block = true, + }; +unsigned long i; +void *entry; +int r; + + entry = attributes ?xa_mk_value (attributes) :NULL ; + +mutex_lock (&kvm->slots_lock); + +/* +* Reserve memory ahead of time to avoid having to deal with failures +* partway through setting the new attributes. +*/ +for (i = start; i < end; i++) { + r =xa_reserve (&kvm->mem_attr_array, i,GFP_KERNEL_ACCOUNT ); +if (r) + goto out_unlock; + } + +kvm_handle_gfn_range (kvm, &unmap_range); + +for (i = start; i < end; i++) { + r =xa_err (xa_store (&kvm->mem_attr_array, i, entry, +GFP_KERNEL_ACCOUNT )); +KVM_BUG_ON (r, kvm); + } + +kvm_handle_gfn_range (kvm, &post_set_range); + +out_unlock: +mutex_unlock (&kvm->slots_lock); + +return r; +} +static int kvm_vm_ioctl_set_mem_attributes (struct kvm *kvm, +struct kvm_memory_attributes *attrs) +{ +gfn_t start, end; + +/* flags is currently not used. */ +if (attrs->flags) +return -EINVAL ; +if (attrs->attributes & ~kvm_supported_mem_attributes (kvm)) +return -EINVAL ; +if (attrs->size ==0 || attrs->address + attrs->size < attrs->address) +return -EINVAL ; +if (!PAGE_ALIGNED (attrs->address) || !PAGE_ALIGNED (attrs->size)) +return -EINVAL ; + + start = attrs->address >>PAGE_SHIFT ; + end = (attrs->address + attrs->size -1 +PAGE_SIZE ) >>PAGE_SHIFT ; + +if (WARN_ON_ONCE (start == end)) +return -EINVAL ; + +/* +* xarray tracks data using "unsigned long", and as a result so does +* KVM. For simplicity, supports generic attributes only on 64-bit +* architectures. +*/ +BUILD_BUG_ON (sizeof (attrs->attributes) !=sizeof (unsigned long )); + +return kvm_vm_set_mem_attributes (kvm, attrs->attributes, start, end); +} +#endif /* CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES */ +struct kvm_memory_slot *gfn_to_memslot (struct kvm *kvm,gfn_t gfn) {return __gfn_to_memslot (kvm_memslots (kvm), gfn);@@ -4521,6 +4667,9 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)#ifdef CONFIG_HAVE_KVM_MSI case KVM_CAP_SIGNAL_MSI :#endif +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +case KVM_CAP_MEMORY_ATTRIBUTES : +#endif #ifdef CONFIG_HAVE_KVM_IRQFD case KVM_CAP_IRQFD :#endif @@ -4937,6 +5086,27 @@ static long kvm_vm_ioctl(struct file *filp, break; }#endif /* CONFIG_HAVE_KVM_IRQ_ROUTING */ +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +case KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES : { +u64 attrs =kvm_supported_mem_attributes (kvm); + + r = -EFAULT ; +if (copy_to_user (argp, &attrs,sizeof (attrs))) + goto out; + r =0 ; +break ; + } +case KVM_SET_MEMORY_ATTRIBUTES : { +struct kvm_memory_attributes attrs; + + r = -EFAULT ; +if (copy_from_user (&attrs, argp,sizeof (attrs))) + goto out; + + r =kvm_vm_ioctl_set_mem_attributes (kvm, &attrs); +break ; + } +#endif /* CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES */ case KVM_CREATE_DEVICE : {struct kvm_create_device cd; --2.41.0.255.g8b1d071c50-goog
Re: [RFC PATCH v11 08/29] KVM: Introduce per-page memory attributes
Quoted 151 lines · Yuan Yao
Quoted 150 lines · Sean Christopherson
From: Chao Peng <[email protected]>
In confidential computing usages, whether a page is private or shared is necessary information for KVM to perform operations like page fault handling, page zapping etc. There are other potential use cases for per-page memory attributes, e.g. to make memory read-only (or no-exec, or exec-only, etc.) without having to modify memslots.
Introduce two ioctls (advertised by
KVM_CAP_MEMORY_ATTRIBUTES) to allow userspace to operate on the per-page memory attributes.
-KVM_SET_MEMORY_ATTRIBUTESto set the per-page memory attributes to a guest memory range.
-KVM_GET_SUPPORTED_MEMORY_ATTRIBUTESto return the KVM supported memory attributes.Use an xarray to store the per-page attributes internally, with a naive, not fully optimized implementation, i.e. prioritize correctness over performance for the initial implementation.
Because setting memory attributes is roughly analogous to
mprotect()on memory that is mapped into the guest, zap existing mappings prior to updating the memory attributes. Opportunistically provide an arch hook for the post-set path (needed to complete invalidation anyways) in anticipation of x86 needing the hook to update metadata related to determining whether or not a given gfn can be backed with various sizes of hugepages.It's possible that future usages may not require an invalidation, e.g. if KVM ends up supporting RWX protections and userspace grants _more_ protections, but again opt for simplicity and punt optimizations to if/when they are needed.
Suggested-by Sean Christopherson <[email protected]>Link https://lore.kernel.org/all/Y2WB48kD0J4VGynX at google.comCc Fuad Tabba <[email protected]>Signed-off-by Chao Peng <[email protected]>Co-developed-by Sean Christopherson <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- Documentation/virt/kvm/api.rst | 60 ++++++++++++ include/linux/kvm_host.h | 14 +++ include/uapi/linux/kvm.h | 14 +++ virt/kvm/Kconfig | 4 + virt/kvm/kvm_main.c | 170 +++++++++++++++++++++++++++++++++ 5 files changed, 262 insertions(+)Documentation/virt/kvm/api.rst +60
-0@@ -6068,6 +6068,56 @@ writes to the CNTVCT_EL0 and CNTPCT_EL0 registers using the SET_ONE_REG interface. No error will be returned, but the resulting offset will not be applied. +4.139 KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES +----------------------------------------- + +:Capability: KVM_CAP_MEMORY_ATTRIBUTES +:Architectures: x86 +:Type: vm ioctl +:Parameters: u64 memory attributes bitmask(out) +:Returns: 0 on success, <0 on error + +Returns supported memory attributes bitmask. Supported memory attributes will +have the corresponding bits set in u64 memory attributes bitmask. + +The following memory attributes are defined:: + + #define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3) + +4.140 KVM_SET_MEMORY_ATTRIBUTES +----------------------------------------- + +:Capability: KVM_CAP_MEMORY_ATTRIBUTES +:Architectures: x86 +:Type: vm ioctl +:Parameters: struct kvm_memory_attributes(in/out) +:Returns: 0 on success, <0 on error + +Sets memory attributes for pages in a guest memory range. Parameters are +specified via the following structure:: + + struct kvm_memory_attributes { + __u64 address; + __u64 size; + __u64 attributes; + __u64 flags; + }; + +The user sets the per-page memory attributes to a guest memory range indicated +by address/size, and in return KVM adjusts address and size to reflect the +actual pages of the memory range have been successfully set to the attributes. +If the call returns 0, "address" is updated to the last successful address + 1 +and "size" is updated to the remaining address size that has not been set +successfully. The user should check the return value as well as the size to +decide if the operation succeeded for the whole range or not. The user may want +to retry the operation with the returned address/size if the previous range was +partially successful. + +Both address and size should be page aligned and the supported attributes can be +retrieved with KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES. + +The "flags" field may be used for future extensions and should be set to 0s. + 5. The kvm_run structure ========================@@ -8494,6 +8544,16 @@ block sizes is exposed in KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES as a 64-bit bitmap (each bit describing a block size). The default value is 0, to disable the eager page splitting. +8.41 KVM_CAP_MEMORY_ATTRIBUTES +------------------------------ + +:Capability: KVM_CAP_MEMORY_ATTRIBUTES +:Architectures: x86 +:Type: vm + +This capability indicates KVM supports per-page memory attributes and ioctls +KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES/KVM_SET_MEMORY_ATTRIBUTES are available. + 9. Known KVM API problems =========================include/linux/kvm_host.h +4
-0@@ -264,6 +264,7 @@ struct kvm_gfn_range {gfn_t end;union {pte_t pte; +unsigned long attributes;u64 raw; } arg;bool may_block;@@ -809,6 +810,9 @@ struct kvm {#ifdef CONFIG_HAVE_KVM_PM_NOTIFIER struct notifier_block pm_notifier; +#endif +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +struct xarray mem_attr_array;#endif char stats_id[KVM_STATS_NAME_SIZE ]; };@@ -2301,4 +2305,14 @@ static inline void kvm_account_pgtable_pages(void *virt, int nr)/* Max number of entries allowed for each kvm dirty ring */ #define KVM_DIRTY_RING_MAX_ENTRIES 65536
Quoted 11 lines
include/linux/kvm_host.h static inline void kvm_account_pgtable_pages(void *virt, int nr) · line 2315
Quoted 8 lines
include/linux/kvm_host.h +8
-0+#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +static inline unsigned long kvm_get_memory_attributes (struct kvm *kvm,gfn_t gfn) +{ +return xa_to_value (xa_load (&kvm->mem_attr_array, gfn)); +} + +bool kvm_arch_post_set_memory_attributes (struct kvm *kvm, +struct kvm_gfn_range *range);Used but no definition in this patch, it's defined in next patch 09. How about add weak version in this patch and let ARCHs to overide it ?
It is guarded by CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES.
Signature · 1 line
Isaku Yamahata <[email protected]>
Sean Christopherson
maintainerRe: [RFC PATCH v11 08/29] KVM: Introduce per-page memory attributes
Quoted 20 lines · Isaku Yamahata
Quoted 5 lines · Yuan Yao
Quoted 4 lines · Sean Christopherson
include/linux/kvm_host.h +0
-0@@ -2301,4 +2305,14 @@ static inline void kvm_account_pgtable_pages(void *virt, int nr)/* Max number of entries allowed for each kvm dirty ring */ #define KVM_DIRTY_RING_MAX_ENTRIES 65536Quoted 11 lines
include/linux/kvm_host.h static inline void kvm_account_pgtable_pages(void *virt, int nr) · line 2315
Quoted 8 lines
include/linux/kvm_host.h +8
-0+#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +static inline unsigned long kvm_get_memory_attributes (struct kvm *kvm,gfn_t gfn) +{ +return xa_to_value (xa_load (&kvm->mem_attr_array, gfn)); +} + +bool kvm_arch_post_set_memory_attributes (struct kvm *kvm, +struct kvm_gfn_range *range);Used but no definition in this patch, it's defined in next patch 09. How about add weak version in this patch and let ARCHs to overide it ?
Quoted 1 line
It is guarded by
CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES.
Yep. I don't love the ordering, e.g. this patch can't even be compile tested until later in the series, but I wanted to separate x86 usage from the generic support code.
Re: [RFC PATCH v11 08/29] KVM: Introduce per-page memory attributes
In reply to Sean ChristophersonQuoted 32 lines · Sean Christopherson
From: Chao Peng <[email protected]>
In confidential computing usages, whether a page is private or shared is necessary information for KVM to perform operations like page fault handling, page zapping etc. There are other potential use cases for per-page memory attributes, e.g. to make memory read-only (or no-exec, or exec-only, etc.) without having to modify memslots.
Introduce two ioctls (advertised by
KVM_CAP_MEMORY_ATTRIBUTES) to allow userspace to operate on the per-page memory attributes.
-KVM_SET_MEMORY_ATTRIBUTESto set the per-page memory attributes to a guest memory range.
-KVM_GET_SUPPORTED_MEMORY_ATTRIBUTESto return the KVM supported memory attributes.Use an xarray to store the per-page attributes internally, with a naive, not fully optimized implementation, i.e. prioritize correctness over performance for the initial implementation.
Because setting memory attributes is roughly analogous to
mprotect()on memory that is mapped into the guest, zap existing mappings prior to updating the memory attributes. Opportunistically provide an arch hook for the post-set path (needed to complete invalidation anyways) in anticipation of x86 needing the hook to update metadata related to determining whether or not a given gfn can be backed with various sizes of hugepages.It's possible that future usages may not require an invalidation, e.g. if KVM ends up supporting RWX protections and userspace grants _more_ protections, but again opt for simplicity and punt optimizations to if/when they are needed.
Quoted 6 lines
Suggested-by Sean Christopherson <[email protected]>Link https://lore.kernel.org/all/Y2WB48kD0J4VGynX at google.comCc Fuad Tabba <[email protected]>Signed-off-by Chao Peng <[email protected]>Co-developed-by Sean Christopherson <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Re: [RFC PATCH v11 08/29] KVM: Introduce per-page memory attributes
In reply to Sean ChristophersonQuoted 18 lines · Sean Christopherson
+4.140
KVM_SET_MEMORY_ATTRIBUTES
+-----------------------------------------Documentation/virt/kvm/api.rst +16
-0+ +:Capability: KVM_CAP_MEMORY_ATTRIBUTES +:Architectures: x86 +:Type: vm ioctl +:Parameters: struct kvm_memory_attributes(in/out) +:Returns: 0 on success, <0 on error + +Sets memory attributes for pages in a guest memory range. Parameters are +specified via the following structure:: + + struct kvm_memory_attributes { + __u64 address; + __u64 size; + __u64 attributes; + __u64 flags; + };
Documentation/virt/kvm/api.rst writes to the CNTVCT_EL0 and CNTPCT_EL0 registers using the SET_ONE_REG · line 6114
Quoted 10 lines
Documentation/virt/kvm/api.rst +10
-0+ +The user sets the per-page memory attributes to a guest memory range indicated +by address/size, and in return KVM adjusts address and size to reflect the +actual pages of the memory range have been successfully set to the attributes. +If the call returns 0, "address" is updated to the last successful address + 1 +and "size" is updated to the remaining address size that has not been set +successfully. The user should check the return value as well as the size to +decide if the operation succeeded for the whole range or not. The user may want +to retry the operation with the returned address/size if the previous range was +partially successful.
This does not match with the implementation. Please fix either one to make them consistent.
Re: [RFC PATCH v11 08/29] KVM: Introduce per-page memory attributes
In reply to Sean ChristophersonQuoted 38 lines · Sean Christopherson
From: Chao Peng <[email protected]>
In confidential computing usages, whether a page is private or shared is necessary information for KVM to perform operations like page fault handling, page zapping etc. There are other potential use cases for per-page memory attributes, e.g. to make memory read-only (or no-exec, or exec-only, etc.) without having to modify memslots.
Introduce two ioctls (advertised by
KVM_CAP_MEMORY_ATTRIBUTES) to allow userspace to operate on the per-page memory attributes.
-KVM_SET_MEMORY_ATTRIBUTESto set the per-page memory attributes to a guest memory range.
-KVM_GET_SUPPORTED_MEMORY_ATTRIBUTESto return the KVM supported memory attributes.Use an xarray to store the per-page attributes internally, with a naive, not fully optimized implementation, i.e. prioritize correctness over performance for the initial implementation.
Because setting memory attributes is roughly analogous to
mprotect()on memory that is mapped into the guest, zap existing mappings prior to updating the memory attributes. Opportunistically provide an arch hook for the post-set path (needed to complete invalidation anyways) in anticipation of x86 needing the hook to update metadata related to determining whether or not a given gfn can be backed with various sizes of hugepages.It's possible that future usages may not require an invalidation, e.g. if KVM ends up supporting RWX protections and userspace grants _more_ protections, but again opt for simplicity and punt optimizations to if/when they are needed.
Suggested-by Sean Christopherson <[email protected]>Link https://lore.kernel.org/all/Y2WB48kD0J4VGynX at google.comCc Fuad Tabba <[email protected]>Signed-off-by Chao Peng <[email protected]>Co-developed-by Sean Christopherson <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
Quoted 8 lines
--- Documentation/virt/kvm/api.rst | 60 ++++++++++++ include/linux/kvm_host.h | 14 +++ include/uapi/linux/kvm.h | 14 +++ virt/kvm/Kconfig | 4 + virt/kvm/kvm_main.c | 170 +++++++++++++++++++++++++++++++++ 5 files changed, 262 insertions(+)
Only some trivial concerns below.
[...]
Quoted 11 lines
virt/kvm/kvm_main.c +3
-0@@ -1175,6 +1176,9 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname)spin_lock_init (&kvm->mn_invalidate_lock);rcuwait_init (&kvm->mn_memslots_update_rcuwait);xa_init (&kvm->vcpu_array); +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +xa_init (&kvm->mem_attr_array); +#endif INIT_LIST_HEAD (&kvm->gpc_list);spin_lock_init (&kvm->gpc_lock);
virt/kvm/kvm_main.c static void kvm_destroy_vm(struct kvm *kvm) · line 1355
Quoted 6 lines
virt/kvm/kvm_main.c +3
-0@@ -1346,6 +1350,9 @@ static void kvm_destroy_vm(struct kvm *kvm)kvm_free_memslots (kvm, &kvm->__memslots[i][0 ]);kvm_free_memslots (kvm, &kvm->__memslots[i][1 ]); } +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +xa_destroy (&kvm->mem_attr_array); +#endif
Is it better to make the destruction in reverse order from the creation? To put xa_destroy(&kvm->mem_attr_array) after cleanup_srcu_struct(&kvm->srcu), or put xa_init(&kvm->mem_attr_array) after init_srcu_struct(&kvm->irq_srcu).
Quoted 6 lines
virt/kvm/kvm_main.c +0
-0cleanup_srcu_struct (&kvm->irq_srcu);cleanup_srcu_struct (&kvm->srcu);kvm_arch_free_vm (kvm);@@ -2346,6 +2353,145 @@ static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, }#endif /* CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT */
[...]
Quoted 7 lines
virt/kvm/kvm_main.c +7
-0+static int kvm_vm_ioctl_set_mem_attributes (struct kvm *kvm, +struct kvm_memory_attributes *attrs) +{ +gfn_t start, end; + +/* flags is currently not used. */ +if (attrs->flags)
virt/kvm/kvm_main.c static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, · line 2479
Quoted 10 lines
virt/kvm/kvm_main.c +10
-0+return -EINVAL ; +if (attrs->attributes & ~kvm_supported_mem_attributes (kvm)) +return -EINVAL ; +if (attrs->size ==0 || attrs->address + attrs->size < attrs->address) +return -EINVAL ; +if (!PAGE_ALIGNED (attrs->address) || !PAGE_ALIGNED (attrs->size)) +return -EINVAL ; + + start = attrs->address >>PAGE_SHIFT ; + end = (attrs->address + attrs->size -1 +PAGE_SIZE ) >>PAGE_SHIFT ;
As the attrs->address/size are both garanteed to be non-zero, non-wrap and page aligned in prevous check. Is it OK to simplify the calculation, like:
end = (attrs->address + attrs->size) >> PAGE_SHIFT;
virt/kvm/kvm_main.c static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, · line 2482
Quoted 3 lines
virt/kvm/kvm_main.c +3
-0+ +if (WARN_ON_ONCE (start == end)) +return -EINVAL ;
Also, is this check possible to be hit? Maybe remove it?
Thanks,
Yilun
Quoted 11 lines
virt/kvm/kvm_main.c +11
-0+ +/* +* xarray tracks data using "unsigned long", and as a result so does +* KVM. For simplicity, supports generic attributes only on 64-bit +* architectures. +*/ +BUILD_BUG_ON (sizeof (attrs->attributes) !=sizeof (unsigned long )); + + return kvm_vm_set_mem_attributes(kvm, attrs->attributes, start, end); +} +#endif /* CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES */
Sean Christopherson
maintainerRe: [RFC PATCH v11 08/29] KVM: Introduce per-page memory attributes
Quoted 10 lines · Xu Yilun
virt/kvm/kvm_main.c static void kvm_destroy_vm(struct kvm *kvm) · line 1355
Quoted 7 lines · Sean Christopherson
virt/kvm/kvm_main.c +3
-0@@ -1346,6 +1350,9 @@ static void kvm_destroy_vm(struct kvm *kvm)kvm_free_memslots (kvm, &kvm->__memslots[i][0 ]);kvm_free_memslots (kvm, &kvm->__memslots[i][1 ]); } +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +xa_destroy (&kvm->mem_attr_array); +#endif Is it better to make the destruction in reverse order from the creation?
Yeah. It _shoudn't_ matter, but there's no reason not keep things tidy and consistent.
Quoted 2 lines
To put
xa_destroy(&kvm->mem_attr_array)aftercleanup_srcu_struct(&kvm->srcu), or putxa_init(&kvm->mem_attr_array)afterinit_srcu_struct(&kvm->irq_srcu).
The former, because init_srcu_struct() can fail (allocates memory), whereas xa_init() is a "pure" initialization routine.
Quoted 31 lines
Quoted 6 lines
virt/kvm/kvm_main.c +0
-0cleanup_srcu_struct (&kvm->irq_srcu);cleanup_srcu_struct (&kvm->srcu);kvm_arch_free_vm (kvm);@@ -2346,6 +2353,145 @@ static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, }#endif /* CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT */ [...]
Quoted 7 lines
virt/kvm/kvm_main.c +7
-0+static int kvm_vm_ioctl_set_mem_attributes (struct kvm *kvm, +struct kvm_memory_attributes *attrs) +{ +gfn_t start, end; + +/* flags is currently not used. */ +if (attrs->flags)virt/kvm/kvm_main.c static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, · line 2479
Quoted 10 lines
virt/kvm/kvm_main.c +10
-0+return -EINVAL ; +if (attrs->attributes & ~kvm_supported_mem_attributes (kvm)) +return -EINVAL ; +if (attrs->size ==0 || attrs->address + attrs->size < attrs->address) +return -EINVAL ; +if (!PAGE_ALIGNED (attrs->address) || !PAGE_ALIGNED (attrs->size)) +return -EINVAL ; + + start = attrs->address >>PAGE_SHIFT ; + end = (attrs->address + attrs->size -1 +PAGE_SIZE ) >>PAGE_SHIFT ;As the attrs->address/size are both garanteed to be non-zero, non-wrap and page aligned in prevous check. Is it OK to simplify the calculation, like:
Quoted 1 line
end = (attrs->address + attrs->size) >> PAGE_SHIFT;
Yes, that should work.
Chao, am I missing something? Or did we just end up with unnecessarly convoluted code as things evolved?
Quoted 5 lines
virt/kvm/kvm_main.c static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, · line 2482
Quoted 3 lines
virt/kvm/kvm_main.c +3
-0+ +if (WARN_ON_ONCE (start == end)) +return -EINVAL ;Also, is this check possible to be hit? Maybe remove it?
It should be impossible to, hence the WARN. I added the check for two reasons:
(1) to help document that end is exclusive, and (2) to guard against future bugs.
Re: [RFC PATCH v11 08/29] KVM: Introduce per-page memory attributes
Quoted 9 lines · Sean Christopherson
Quoted 5 lines · Xu Yilun
virt/kvm/kvm_main.c static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, · line 2482
Quoted 2 lines · Sean Christopherson
virt/kvm/kvm_main.c +2
-0+if (WARN_ON_ONCE (start == end)) +return -EINVAL ;Also, is this check possible to be hit? Maybe remove it?
It should be impossible to, hence the WARN. I added the check for two reasons:
(1) to help document that end is exclusive, and (2) to guard against future bugs.
Understood. I'm good to it.
Re: [RFC PATCH v11 08/29] KVM: Introduce per-page memory attributes
In reply to Sean ChristophersonQuoted 301 lines · Sean Christopherson
From: Chao Peng <[email protected]>
In confidential computing usages, whether a page is private or shared is necessary information for KVM to perform operations like page fault handling, page zapping etc. There are other potential use cases for per-page memory attributes, e.g. to make memory read-only (or no-exec, or exec-only, etc.) without having to modify memslots.
Introduce two ioctls (advertised by
KVM_CAP_MEMORY_ATTRIBUTES) to allow userspace to operate on the per-page memory attributes.
-KVM_SET_MEMORY_ATTRIBUTESto set the per-page memory attributes to a guest memory range.
-KVM_GET_SUPPORTED_MEMORY_ATTRIBUTESto return the KVM supported memory attributes.Use an xarray to store the per-page attributes internally, with a naive, not fully optimized implementation, i.e. prioritize correctness over performance for the initial implementation.
Because setting memory attributes is roughly analogous to
mprotect()on memory that is mapped into the guest, zap existing mappings prior to updating the memory attributes. Opportunistically provide an arch hook for the post-set path (needed to complete invalidation anyways) in anticipation of x86 needing the hook to update metadata related to determining whether or not a given gfn can be backed with various sizes of hugepages.It's possible that future usages may not require an invalidation, e.g. if KVM ends up supporting RWX protections and userspace grants _more_ protections, but again opt for simplicity and punt optimizations to if/when they are needed.
Suggested-by Sean Christopherson <[email protected]>Link https://lore.kernel.org/all/Y2WB48kD0J4VGynX at google.comCc Fuad Tabba <[email protected]>Signed-off-by Chao Peng <[email protected]>Co-developed-by Sean Christopherson <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- Documentation/virt/kvm/api.rst | 60 ++++++++++++ include/linux/kvm_host.h | 14 +++ include/uapi/linux/kvm.h | 14 +++ virt/kvm/Kconfig | 4 + virt/kvm/kvm_main.c | 170 +++++++++++++++++++++++++++++++++ 5 files changed, 262 insertions(+)Documentation/virt/kvm/api.rst +60
-0@@ -6068,6 +6068,56 @@ writes to the CNTVCT_EL0 and CNTPCT_EL0 registers using the SET_ONE_REG interface. No error will be returned, but the resulting offset will not be applied. +4.139 KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES +----------------------------------------- + +:Capability: KVM_CAP_MEMORY_ATTRIBUTES +:Architectures: x86 +:Type: vm ioctl +:Parameters: u64 memory attributes bitmask(out) +:Returns: 0 on success, <0 on error + +Returns supported memory attributes bitmask. Supported memory attributes will +have the corresponding bits set in u64 memory attributes bitmask. + +The following memory attributes are defined:: + + #define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3) + +4.140 KVM_SET_MEMORY_ATTRIBUTES +----------------------------------------- + +:Capability: KVM_CAP_MEMORY_ATTRIBUTES +:Architectures: x86 +:Type: vm ioctl +:Parameters: struct kvm_memory_attributes(in/out) +:Returns: 0 on success, <0 on error + +Sets memory attributes for pages in a guest memory range. Parameters are +specified via the following structure:: + + struct kvm_memory_attributes { + __u64 address; + __u64 size; + __u64 attributes; + __u64 flags; + }; + +The user sets the per-page memory attributes to a guest memory range indicated +by address/size, and in return KVM adjusts address and size to reflect the +actual pages of the memory range have been successfully set to the attributes. +If the call returns 0, "address" is updated to the last successful address + 1 +and "size" is updated to the remaining address size that has not been set +successfully. The user should check the return value as well as the size to +decide if the operation succeeded for the whole range or not. The user may want +to retry the operation with the returned address/size if the previous range was +partially successful. + +Both address and size should be page aligned and the supported attributes can be +retrieved with KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES. + +The "flags" field may be used for future extensions and should be set to 0s. + 5. The kvm_run structure ========================@@ -8494,6 +8544,16 @@ block sizes is exposed in KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES as a 64-bit bitmap (each bit describing a block size). The default value is 0, to disable the eager page splitting. +8.41 KVM_CAP_MEMORY_ATTRIBUTES +------------------------------ + +:Capability: KVM_CAP_MEMORY_ATTRIBUTES +:Architectures: x86 +:Type: vm + +This capability indicates KVM supports per-page memory attributes and ioctls +KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES/KVM_SET_MEMORY_ATTRIBUTES are available. + 9. Known KVM API problems =========================include/linux/kvm_host.h +14
-0@@ -264,6 +264,7 @@ struct kvm_gfn_range {gfn_t end;union {pte_t pte; +unsigned long attributes;u64 raw; } arg;bool may_block;@@ -809,6 +810,9 @@ struct kvm {#ifdef CONFIG_HAVE_KVM_PM_NOTIFIER struct notifier_block pm_notifier; +#endif +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +struct xarray mem_attr_array;#endif char stats_id[KVM_STATS_NAME_SIZE ]; };@@ -2301,4 +2305,14 @@ static inline void kvm_account_pgtable_pages(void *virt, int nr)/* Max number of entries allowed for each kvm dirty ring */ #define KVM_DIRTY_RING_MAX_ENTRIES 65536 +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +static inline unsigned long kvm_get_memory_attributes (struct kvm *kvm,gfn_t gfn) +{ +return xa_to_value (xa_load (&kvm->mem_attr_array, gfn)); +} + +bool kvm_arch_post_set_memory_attributes (struct kvm *kvm, +struct kvm_gfn_range *range); +#endif /* CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES */ +#endif include/uapi/linux/kvm.h +14
-0@@ -1211,6 +1211,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE 228#define KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES 229#define KVM_CAP_USER_MEMORY2 230 +#define KVM_CAP_MEMORY_ATTRIBUTES 231#ifdef KVM_CAP_IRQ_ROUTING @@ -2270,4 +2271,17 @@ struct kvm_s390_zpci_op {/* flags for kvm_s390_zpci_op->u.reg_aen.flags */ #define KVM_S390_ZPCIOP_REGAEN_HOST (1 << 0) +/* Available with KVM_CAP_MEMORY_ATTRIBUTES */ +#define KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES _IOR(KVMIO, 0xd2, __u64) +#define KVM_SET_MEMORY_ATTRIBUTES _IOW(KVMIO, 0xd3, struct kvm_memory_attributes) + +struct kvm_memory_attributes { +__u64 address; +__u64 size; +__u64 attributes; +__u64 flags; +}; + +#define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3) +#endif /* __LINUX_KVM_H */ virt/kvm/Kconfig +4
-0@@ -99,3 +99,7 @@ config KVM_GENERIC_HARDWARE_ENABLING config KVM_GENERIC_MMU_NOTIFIER select MMU_NOTIFIER bool + +config KVM_GENERIC_MEMORY_ATTRIBUTES + select KVM_GENERIC_MMU_NOTIFIER + boolvirt/kvm/kvm_main.c +69
-0@@ -530,6 +530,7 @@ struct kvm_mmu_notifier_range {u64 end;union {pte_t pte; +unsigned long attributes;u64 raw; } arg;gfn_handler_t handler;@@ -1175,6 +1176,9 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname)spin_lock_init (&kvm->mn_invalidate_lock);rcuwait_init (&kvm->mn_memslots_update_rcuwait);xa_init (&kvm->vcpu_array); +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +xa_init (&kvm->mem_attr_array); +#endif INIT_LIST_HEAD (&kvm->gpc_list);spin_lock_init (&kvm->gpc_lock);@@ -1346,6 +1350,9 @@ static void kvm_destroy_vm(struct kvm *kvm)kvm_free_memslots (kvm, &kvm->__memslots[i][0 ]);kvm_free_memslots (kvm, &kvm->__memslots[i][1 ]); } +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +xa_destroy (&kvm->mem_attr_array); +#endif cleanup_srcu_struct (&kvm->irq_srcu);cleanup_srcu_struct (&kvm->srcu);kvm_arch_free_vm (kvm);@@ -2346,6 +2353,145 @@ static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, }#endif /* CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT */ +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +static u64 kvm_supported_mem_attributes (struct kvm *kvm) +{ +return 0 ; +} + +static __always_inline void kvm_handle_gfn_range(struct kvm *kvm, +struct kvm_mmu_notifier_range *range) +{ +struct kvm_gfn_range gfn_range; +struct kvm_memory_slot *slot; +struct kvm_memslots *slots; +struct kvm_memslot_iter iter; +bool locked = false; +bool ret = false; +int i; + + gfn_range.arg.raw = range->arg.raw; + gfn_range.may_block = range->may_block; + +for (i =0 ; i <KVM_ADDRESS_SPACE_NUM ; i++) { + slots =__kvm_memslots (kvm, i); + +kvm_for_each_memslot_in_gfn_range (&iter, slots, range->start, range->end) { + slot = iter.slot; + gfn_range.slot = slot; + + gfn_range.start =max (range->start, slot->base_gfn); + gfn_range.end =min (range->end, slot->base_gfn + slot->npages); +if (gfn_range.start >= gfn_range.end) +continue ; + +if (!locked) { + locked = true; +KVM_MMU_LOCK (kvm); +if (!IS_KVM_NULL_FN (range->on_lock)) + range->on_lock (kvm); + } + + ret |= range->handler (kvm, &gfn_range); + } + } + +if (range->flush_on_ret && ret) +kvm_flush_remote_tlbs (kvm); + +if (locked) { +KVM_MMU_UNLOCK (kvm); +if (!IS_KVM_NULL_FN (range->on_unlock)) + range->on_unlock (kvm); + } +} + +static int kvm_vm_set_mem_attributes (struct kvm *kvm,unsigned long attributes, +gfn_t start,gfn_t end) +{ +struct kvm_mmu_notifier_range unmap_range = { + .start = start, + .end = end, + .handler = kvm_mmu_unmap_gfn_range, + .on_lock = kvm_mmu_invalidate_begin, + .on_unlock = (void *)kvm_null_fn,
virt/kvm/kvm_main.c static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, · line 2427
Quoted 10 lines
virt/kvm/kvm_main.c +10
-0+ .flush_on_ret = true, + .may_block = true, + }; +struct kvm_mmu_notifier_range post_set_range = { + .start = start, + .end = end, + .arg.attributes = attributes, + .handler = kvm_arch_post_set_memory_attributes, + .on_lock = (void *)kvm_null_fn, + .on_unlock = kvm_mmu_invalidate_end,
on_unlock is called after unlocking mmu_lock. So kvm::mmu_invalidate_in_progress is touched out side of it. Here is a quick fix.
WARNING: CPU: 108 PID: 62218 at arch/x86/kvm/../../../virt/kvm/kvm_main.c:757 kvm_mmu_unmap_gfn_range+0x32/0x70 [kvm] ... RIP: 0010:kvm_mmu_unmap_gfn_range+0x32/0x70 [kvm] ... Call Trace: <TASK> kvm_gmem_invalidate_begin+0xd0/0x130 [kvm] kvm_gmem_fallocate+0x134/0x290 [kvm] vfs_fallocate+0x151/0x380 __x64_sys_fallocate+0x3c/0x70 do_syscall_64+0x40/0x90 entry_SYSCALL_64_after_hwframe+0x6e/0xd8
From c06084048271278d3508f534479b356f49f619ce Mon Sep 17 00:00:00 2001
Message-Id: <c06084048271278d3508f534479b356f49f619ce.1690873712.git.isaku.yamahata@intel.com>
From: Isaku Yamahata <[email protected]>
Date: Mon, 31 Jul 2023 22:58:15 -0700
Subject: [PATCH] KVM: guest_memfd(): protect kvm_mmu_invalidate_end()
kvm_mmu_invalidate_end() updates struct kvm::mmu_invalidate_in_progress and it's protected by kvm::mmu_lock. call kvm_mmu_invalidate_end() before unlocking it. Not after the unlock.
--- virt/kvm/kvm_main.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
virt/kvm/kvm_main.c +14 -1
@@ -548,6 +548,7 @@ struct kvm_mmu_notifier_range { } arg;gfn_handler_t handler;on_lock_fn_t on_lock; +on_unlock_fn_t before_unlock;on_unlock_fn_t on_unlock;bool flush_on_ret;bool may_block;
@@ -644,6 +645,8 @@ static __always_inline int __kvm_handle_hva_range(struct kvm *kvm,kvm_flush_remote_tlbs (kvm);if (locked) { +if (!IS_KVM_NULL_FN (range->before_unlock)) + range->before_unlock (kvm);KVM_MMU_UNLOCK (kvm);if (!IS_KVM_NULL_FN (range->on_unlock)) range->on_unlock (kvm);
@@ -668,6 +671,7 @@ static __always_inline int kvm_handle_hva_range(struct mmu_notifier *mn, .arg.pte = pte, .handler = handler, .on_lock = (void *)kvm_null_fn, + .before_unlock = (void *)kvm_null_fn, .on_unlock = (void *)kvm_null_fn, .flush_on_ret = true, .may_block = false,
@@ -687,6 +691,7 @@ static __always_inline int kvm_handle_hva_range_no_flush(struct mmu_notifier *mn .end = end, .handler = handler, .on_lock = (void *)kvm_null_fn, + .before_unlock = (void *)kvm_null_fn, .on_unlock = (void *)kvm_null_fn, .flush_on_ret = false, .may_block = false,
@@ -791,6 +796,7 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, .end = range->end, .handler = kvm_mmu_unmap_gfn_range, .on_lock = kvm_mmu_invalidate_begin, + .before_unlock = (void *)kvm_null_fn, .on_unlock = kvm_arch_guest_memory_reclaimed, .flush_on_ret = true, .may_block = mmu_notifier_range_blockable(range),
@@ -830,6 +836,8 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,void kvm_mmu_invalidate_end (struct kvm *kvm) { +lockdep_assert_held_write (&kvm->mmu_lock); +/* * This sequence increase will notify the kvm page fault that * the page that is going to be mapped in the spte could have
@@ -861,6 +869,7 @@ static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn, .end = range->end, .handler = (void *)kvm_null_fn, .on_lock = kvm_mmu_invalidate_end, + .before_unlock = (void *)kvm_null_fn, .on_unlock = (void *)kvm_null_fn, .flush_on_ret = false, .may_block = mmu_notifier_range_blockable(range),
@@ -2466,6 +2475,8 @@ static __always_inline void kvm_handle_gfn_range(struct kvm *kvm,kvm_flush_remote_tlbs (kvm);if (locked) { +if (!IS_KVM_NULL_FN (range->before_unlock)) + range->before_unlock (kvm);KVM_MMU_UNLOCK (kvm);if (!IS_KVM_NULL_FN (range->on_unlock)) range->on_unlock (kvm);
@@ -2480,6 +2491,7 @@ static int kvm_vm_set_mem_attributes(struct kvm *kvm, unsigned long attributes, .end = end, .handler = kvm_mmu_unmap_gfn_range, .on_lock = kvm_mmu_invalidate_begin, + .before_unlock = (void *)kvm_null_fn, .on_unlock = (void *)kvm_null_fn, .flush_on_ret = true, .may_block = true,
@@ -2490,7 +2502,8 @@ static int kvm_vm_set_mem_attributes(struct kvm *kvm, unsigned long attributes, .arg.attributes = attributes, .handler = kvm_arch_post_set_memory_attributes, .on_lock = (void *)kvm_null_fn, - .on_unlock = kvm_mmu_invalidate_end, + .before_unlock = kvm_mmu_invalidate_end, + .on_unlock = (void *)kvm_null_fn, .may_block = true, };unsigned long i;
Signature · 5 lines
2.25.1 -- Isaku Yamahata <[email protected]>
Re: [RFC PATCH v11 08/29] KVM: Introduce per-page memory attributes
In reply to Sean ChristophersonQuoted 15 lines · Sean Christopherson
From: Chao Peng <[email protected]>
In confidential computing usages, whether a page is private or shared is necessary information for KVM to perform operations like page fault handling, page zapping etc. There are other potential use cases for per-page memory attributes, e.g. to make memory read-only (or no-exec, or exec-only, etc.) without having to modify memslots.
Introduce two ioctls (advertised by
KVM_CAP_MEMORY_ATTRIBUTES) to allow userspace to operate on the per-page memory attributes.
-KVM_SET_MEMORY_ATTRIBUTESto set the per-page memory attributes to a guest memory range.
-KVM_GET_SUPPORTED_MEMORY_ATTRIBUTESto return the KVM supported memory attributes.
Quoted 8 lines
Use an xarray to store the per-page attributes internally, with a naive, not fully optimized implementation, i.e. prioritize correctness over performance for the initial implementation.
Because setting memory attributes is roughly analogous to
mprotect()on memory that is mapped into the guest, zap existing mappings prior to updating the memory attributes. Opportunistically provide an arch hook for the post-set path (needed to complete invalidation anyways) in
s/anyways/anyway
Quoted 285 lines
anticipation of x86 needing the hook to update metadata related to determining whether or not a given gfn can be backed with various sizes of hugepages.
It's possible that future usages may not require an invalidation, e.g. if KVM ends up supporting RWX protections and userspace grants _more_ protections, but again opt for simplicity and punt optimizations to if/when they are needed.
Suggested-by Sean Christopherson <[email protected]>Link https://lore.kernel.org/all/Y2WB48kD0J4VGynX at google.comCc Fuad Tabba <[email protected]>Signed-off-by Chao Peng <[email protected]>Co-developed-by Sean Christopherson <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- Documentation/virt/kvm/api.rst | 60 ++++++++++++ include/linux/kvm_host.h | 14 +++ include/uapi/linux/kvm.h | 14 +++ virt/kvm/Kconfig | 4 + virt/kvm/kvm_main.c | 170 +++++++++++++++++++++++++++++++++ 5 files changed, 262 insertions(+)Documentation/virt/kvm/api.rst +60
-0@@ -6068,6 +6068,56 @@ writes to the CNTVCT_EL0 and CNTPCT_EL0 registers using the SET_ONE_REG interface. No error will be returned, but the resulting offset will not be applied. +4.139 KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES +----------------------------------------- + +:Capability: KVM_CAP_MEMORY_ATTRIBUTES +:Architectures: x86 +:Type: vm ioctl +:Parameters: u64 memory attributes bitmask(out) +:Returns: 0 on success, <0 on error + +Returns supported memory attributes bitmask. Supported memory attributes will +have the corresponding bits set in u64 memory attributes bitmask. + +The following memory attributes are defined:: + + #define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3) + +4.140 KVM_SET_MEMORY_ATTRIBUTES +----------------------------------------- + +:Capability: KVM_CAP_MEMORY_ATTRIBUTES +:Architectures: x86 +:Type: vm ioctl +:Parameters: struct kvm_memory_attributes(in/out) +:Returns: 0 on success, <0 on error + +Sets memory attributes for pages in a guest memory range. Parameters are +specified via the following structure:: + + struct kvm_memory_attributes { + __u64 address; + __u64 size; + __u64 attributes; + __u64 flags; + }; + +The user sets the per-page memory attributes to a guest memory range indicated +by address/size, and in return KVM adjusts address and size to reflect the +actual pages of the memory range have been successfully set to the attributes. +If the call returns 0, "address" is updated to the last successful address + 1 +and "size" is updated to the remaining address size that has not been set +successfully. The user should check the return value as well as the size to +decide if the operation succeeded for the whole range or not. The user may want +to retry the operation with the returned address/size if the previous range was +partially successful. + +Both address and size should be page aligned and the supported attributes can be +retrieved with KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES. + +The "flags" field may be used for future extensions and should be set to 0s. + 5. The kvm_run structure ========================@@ -8494,6 +8544,16 @@ block sizes is exposed in KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES as a 64-bit bitmap (each bit describing a block size). The default value is 0, to disable the eager page splitting. +8.41 KVM_CAP_MEMORY_ATTRIBUTES +------------------------------ + +:Capability: KVM_CAP_MEMORY_ATTRIBUTES +:Architectures: x86 +:Type: vm + +This capability indicates KVM supports per-page memory attributes and ioctls +KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES/KVM_SET_MEMORY_ATTRIBUTES are available. + 9. Known KVM API problems =========================include/linux/kvm_host.h +14
-0@@ -264,6 +264,7 @@ struct kvm_gfn_range {gfn_t end;union {pte_t pte; +unsigned long attributes;u64 raw; } arg;bool may_block;@@ -809,6 +810,9 @@ struct kvm {#ifdef CONFIG_HAVE_KVM_PM_NOTIFIER struct notifier_block pm_notifier; +#endif +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +struct xarray mem_attr_array;#endif char stats_id[KVM_STATS_NAME_SIZE ]; };@@ -2301,4 +2305,14 @@ static inline void kvm_account_pgtable_pages(void *virt, int nr)/* Max number of entries allowed for each kvm dirty ring */ #define KVM_DIRTY_RING_MAX_ENTRIES 65536 +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +static inline unsigned long kvm_get_memory_attributes (struct kvm *kvm,gfn_t gfn) +{ +return xa_to_value (xa_load (&kvm->mem_attr_array, gfn)); +} + +bool kvm_arch_post_set_memory_attributes (struct kvm *kvm, +struct kvm_gfn_range *range); +#endif /* CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES */ +#endif include/uapi/linux/kvm.h +14
-0@@ -1211,6 +1211,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE 228#define KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES 229#define KVM_CAP_USER_MEMORY2 230 +#define KVM_CAP_MEMORY_ATTRIBUTES 231#ifdef KVM_CAP_IRQ_ROUTING @@ -2270,4 +2271,17 @@ struct kvm_s390_zpci_op {/* flags for kvm_s390_zpci_op->u.reg_aen.flags */ #define KVM_S390_ZPCIOP_REGAEN_HOST (1 << 0) +/* Available with KVM_CAP_MEMORY_ATTRIBUTES */ +#define KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES _IOR(KVMIO, 0xd2, __u64) +#define KVM_SET_MEMORY_ATTRIBUTES _IOW(KVMIO, 0xd3, struct kvm_memory_attributes) + +struct kvm_memory_attributes { +__u64 address; +__u64 size; +__u64 attributes; +__u64 flags; +}; + +#define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3) +#endif /* __LINUX_KVM_H */ virt/kvm/Kconfig +4
-0@@ -99,3 +99,7 @@ config KVM_GENERIC_HARDWARE_ENABLING config KVM_GENERIC_MMU_NOTIFIER select MMU_NOTIFIER bool + +config KVM_GENERIC_MEMORY_ATTRIBUTES + select KVM_GENERIC_MMU_NOTIFIER + boolvirt/kvm/kvm_main.c +76
-0@@ -530,6 +530,7 @@ struct kvm_mmu_notifier_range {u64 end;union {pte_t pte; +unsigned long attributes;u64 raw; } arg;gfn_handler_t handler;@@ -1175,6 +1176,9 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname)spin_lock_init (&kvm->mn_invalidate_lock);rcuwait_init (&kvm->mn_memslots_update_rcuwait);xa_init (&kvm->vcpu_array); +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +xa_init (&kvm->mem_attr_array); +#endif INIT_LIST_HEAD (&kvm->gpc_list);spin_lock_init (&kvm->gpc_lock);@@ -1346,6 +1350,9 @@ static void kvm_destroy_vm(struct kvm *kvm)kvm_free_memslots (kvm, &kvm->__memslots[i][0 ]);kvm_free_memslots (kvm, &kvm->__memslots[i][1 ]); } +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +xa_destroy (&kvm->mem_attr_array); +#endif cleanup_srcu_struct (&kvm->irq_srcu);cleanup_srcu_struct (&kvm->srcu);kvm_arch_free_vm (kvm);@@ -2346,6 +2353,145 @@ static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, }#endif /* CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT */ +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +static u64 kvm_supported_mem_attributes (struct kvm *kvm) +{ +return 0 ; +} + +static __always_inline void kvm_handle_gfn_range(struct kvm *kvm, +struct kvm_mmu_notifier_range *range) +{ +struct kvm_gfn_range gfn_range; +struct kvm_memory_slot *slot; +struct kvm_memslots *slots; +struct kvm_memslot_iter iter; +bool locked = false; +bool ret = false; +int i; + + gfn_range.arg.raw = range->arg.raw; + gfn_range.may_block = range->may_block; + +for (i =0 ; i <KVM_ADDRESS_SPACE_NUM ; i++) { + slots =__kvm_memslots (kvm, i); + +kvm_for_each_memslot_in_gfn_range (&iter, slots, range->start, range->end) { + slot = iter.slot; + gfn_range.slot = slot; + + gfn_range.start =max (range->start, slot->base_gfn); + gfn_range.end =min (range->end, slot->base_gfn + slot->npages); +if (gfn_range.start >= gfn_range.end) +continue ; + +if (!locked) { + locked = true; +KVM_MMU_LOCK (kvm); +if (!IS_KVM_NULL_FN (range->on_lock)) + range->on_lock (kvm); + } + + ret |= range->handler (kvm, &gfn_range); + } + } + +if (range->flush_on_ret && ret) +kvm_flush_remote_tlbs (kvm); + +if (locked) { +KVM_MMU_UNLOCK (kvm); +if (!IS_KVM_NULL_FN (range->on_unlock)) + range->on_unlock (kvm); + } +} + +static int kvm_vm_set_mem_attributes (struct kvm *kvm,unsigned long attributes, +gfn_t start,gfn_t end) +{ +struct kvm_mmu_notifier_range unmap_range = { + .start = start, + .end = end, + .handler = kvm_mmu_unmap_gfn_range, + .on_lock = kvm_mmu_invalidate_begin, + .on_unlock = (void *)kvm_null_fn, + .flush_on_ret = true, + .may_block = true, + }; +struct kvm_mmu_notifier_range post_set_range = { + .start = start, + .end = end, + .arg.attributes = attributes,
virt/kvm/kvm_main.c static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, · line 2434
Quoted 10 lines
virt/kvm/kvm_main.c +10
-0+ .handler = kvm_arch_post_set_memory_attributes, + .on_lock = (void *)kvm_null_fn, + .on_unlock = kvm_mmu_invalidate_end, + .may_block = true, + }; +unsigned long i; +void *entry; +int r; + + entry = attributes ?xa_mk_value (attributes) :NULL ;
Why attributes of value 0 is considered not a value? Is it because 0 is not a valid value when RWX is considered in the future?
Quoted 35 lines
virt/kvm/kvm_main.c +35
-0+ +mutex_lock (&kvm->slots_lock); + +/* +* Reserve memory ahead of time to avoid having to deal with failures +* partway through setting the new attributes. +*/ +for (i = start; i < end; i++) { + r =xa_reserve (&kvm->mem_attr_array, i,GFP_KERNEL_ACCOUNT ); +if (r) + goto out_unlock; + } + +kvm_handle_gfn_range (kvm, &unmap_range); + +for (i = start; i < end; i++) { + r =xa_err (xa_store (&kvm->mem_attr_array, i, entry, +GFP_KERNEL_ACCOUNT )); +KVM_BUG_ON (r, kvm); + } + +kvm_handle_gfn_range (kvm, &post_set_range); + +out_unlock: +mutex_unlock (&kvm->slots_lock); + +return r; +} +static int kvm_vm_ioctl_set_mem_attributes (struct kvm *kvm, +struct kvm_memory_attributes *attrs) +{ +gfn_t start, end; + +/* flags is currently not used. */ +if (attrs->flags)
virt/kvm/kvm_main.c static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, · line 2479
Quoted 10 lines
virt/kvm/kvm_main.c +10
-0+return -EINVAL ; +if (attrs->attributes & ~kvm_supported_mem_attributes (kvm)) +return -EINVAL ; +if (attrs->size ==0 || attrs->address + attrs->size < attrs->address) +return -EINVAL ; +if (!PAGE_ALIGNED (attrs->address) || !PAGE_ALIGNED (attrs->size)) +return -EINVAL ; + + start = attrs->address >>PAGE_SHIFT ; + end = (attrs->address + attrs->size -1 +PAGE_SIZE ) >>PAGE_SHIFT ;
No need to handle the alignment again since both address and size are page aligned.
Quoted 41 lines
virt/kvm/kvm_main.c +27
-0+ +if (WARN_ON_ONCE (start == end)) +return -EINVAL ; + +/* +* xarray tracks data using "unsigned long", and as a result so does +* KVM. For simplicity, supports generic attributes only on 64-bit +* architectures. +*/ +BUILD_BUG_ON (sizeof (attrs->attributes) !=sizeof (unsigned long )); + + return kvm_vm_set_mem_attributes(kvm, attrs->attributes, start, end); +} +#endif /* CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES */ +struct kvm_memory_slot *gfn_to_memslot (struct kvm *kvm,gfn_t gfn) {return __gfn_to_memslot (kvm_memslots (kvm), gfn);@@ -4521,6 +4667,9 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)#ifdef CONFIG_HAVE_KVM_MSI case KVM_CAP_SIGNAL_MSI :#endif +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +case KVM_CAP_MEMORY_ATTRIBUTES : +#endif #ifdef CONFIG_HAVE_KVM_IRQFD case KVM_CAP_IRQFD :#endif @@ -4937,6 +5086,27 @@ static long kvm_vm_ioctl(struct file *filp,break ; }#endif /* CONFIG_HAVE_KVM_IRQ_ROUTING */ +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +case KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES : { +u64 attrs =kvm_supported_mem_attributes (kvm); + + r = -EFAULT ; +if (copy_to_user (argp, &attrs,sizeof (attrs))) + goto out; + r =0 ; +break ;
virt/kvm/kvm_main.c static long kvm_vm_ioctl(struct file *filp, · line 5107
Quoted 10 lines
virt/kvm/kvm_main.c +10
-0+ } +case KVM_SET_MEMORY_ATTRIBUTES : { +struct kvm_memory_attributes attrs; + + r = -EFAULT ; +if (copy_from_user (&attrs, argp,sizeof (attrs))) + goto out; + + r =kvm_vm_ioctl_set_mem_attributes (kvm, &attrs); +break ;
Both the changelog and the document added mention that the address and size of attrs will be updated to
"reflect the actual pages of the memory range have been successfully set to the attributes", but it doesn't.
Quoted 5 lines
virt/kvm/kvm_main.c +2
-0+ } +#endif /* CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES */ case KVM_CREATE_DEVICE : {struct kvm_create_device cd;
Sean Christopherson
maintainerRe: [RFC PATCH v11 08/29] KVM: Introduce per-page memory attributes
Quoted 18 lines · Binbin Wu
Quoted 4 lines · Sean Christopherson
virt/kvm/kvm_main.c +4
-0+struct kvm_mmu_notifier_range post_set_range = { + .start = start, + .end = end, + .arg.attributes = attributes,virt/kvm/kvm_main.c static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, · line 2434
Quoted 10 lines
virt/kvm/kvm_main.c +10
-0+ .handler = kvm_arch_post_set_memory_attributes, + .on_lock = (void *)kvm_null_fn, + .on_unlock = kvm_mmu_invalidate_end, + .may_block = true, + }; +unsigned long i; +void *entry; +int r; + + entry = attributes ?xa_mk_value (attributes) :NULL ;Why attributes of value 0 is considered not a value? Is it because 0 is not a valid value when RWX is considered in the future?
0 values don't require an entry in the xarray, i.e. don't need to be stored and so don't consume memory. The potential conflict with a RWX=0 entry has already been noted, but we'll cross that bridge when we get to it, e.g. KVM can easily support RWX=0 by using an internal "valid" flag.
Quoted 4 lines
Both the changelog and the document added mention that the address and size of attrs will be updated to
"reflect the actual pages of the memory range have been successfully set to the attributes", but it doesn't.
Yeah, on the todo list, all of the changelogs are horribly stale.
Sean Christopherson
maintainerRFC PATCH v11 9/29 KVM: x86: Disallow hugepages when memory attributes are mixed
In reply to Sean ChristophersonFrom: Chao Peng <[email protected]>
Disallow creating hugepages with mixed memory attributes, e.g. shared versus private, as mapping a hugepage in this case would allow the guest to access memory with the wrong attributes, e.g. overlaying private memory with a shared hugepage.
Tracking whether or not attributes are mixed via the existing disallow_lpage field, but use the most significant bit in 'disallow_lpage' to indicate a hugepage has mixed attributes instead using the normal refcounting. Whether or not attributes are mixed is binary; either they are or they aren't. Attempting to squeeze that info into the refcount is unnecessarily complex as it would require knowing the previous state of the mixed count when updating attributes. Using a flag means KVM just needs to ensure the current status is reflected in the memslots.
--- arch/x86/include/asm/kvm_host.h | 3 + arch/x86/kvm/mmu/mmu.c | 185 +++++++++++++++++++++++++++++++- arch/x86/kvm/x86.c | 4 + 3 files changed, 190 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm_host.h +3 -0
@@ -1816,6 +1816,9 @@ int kvm_mmu_create(struct kvm_vcpu *vcpu);int kvm_mmu_init_vm (struct kvm *kvm);void kvm_mmu_uninit_vm (struct kvm *kvm); +void kvm_mmu_init_memslot_memory_attributes (struct kvm *kvm, +struct kvm_memory_slot *slot); +void kvm_mmu_after_set_cpuid (struct kvm_vcpu *vcpu);void kvm_mmu_reset_context (struct kvm_vcpu *vcpu); void kvm_mmu_slot_remove_write_access(struct kvm *kvm,
arch/x86/kvm/mmu/mmu.c +183 -2
202 lines not on this page · raw message
arch/x86/kvm/x86.c +4 -0
@@ -12570,6 +12570,10 @@ static int kvm_alloc_memslot_metadata(struct kvm *kvm, } } +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES +kvm_mmu_init_memslot_memory_attributes (kvm, slot); +#endif +if (kvm_page_track_create_memslot (kvm, slot, npages)) goto out_free;
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Paolo Bonzini
maintainerRe: [RFC PATCH v11 09/29] KVM: x86: Disallow hugepages when memory attributes are mixed
Quoted 22 lines · Sean Christopherson
arch/x86/kvm/mmu/mmu.c +22
-0+static bool range_has_attrs (struct kvm *kvm,gfn_t start,gfn_t end, +unsigned long attrs) +{ +XA_STATE (xas, &kvm->mem_attr_array, start); +unsigned long index; +bool has_attrs; +void *entry; + +rcu_read_lock (); + +if (!attrs) { + has_attrs = !xas_find (&xas, end); + goto out; + } + + has_attrs = true; +for (index = start; index < end; index++) { +do { + entry =xas_next (&xas); + }while (xas_retry (&xas, entry)); + +if (xas.xa_index != index ||xa_to_value (entry) != attrs) {
arch/x86/kvm/mmu/mmu.c void kvm_mmu_pre_destroy_vm(struct kvm *kvm) · line 7288
Quoted 10 lines
arch/x86/kvm/mmu/mmu.c +10
-0+ has_attrs = false; +break ; + } + } + +out: +rcu_read_unlock (); +return has_attrs; +} +
Can you move this function to virt/kvm/kvm_main.c?
Thanks,
Paolo
Sean Christopherson
maintainerRe: [RFC PATCH v11 09/29] KVM: x86: Disallow hugepages when memory attributes are mixed
Quoted 27 lines · Paolo Bonzini
Quoted 26 lines · Sean Christopherson
arch/x86/kvm/mmu/mmu.c +26
-0+static bool range_has_attrs (struct kvm *kvm,gfn_t start,gfn_t end, +unsigned long attrs) +{ +XA_STATE (xas, &kvm->mem_attr_array, start); +unsigned long index; +bool has_attrs; +void *entry; + +rcu_read_lock (); + +if (!attrs) { + has_attrs = !xas_find (&xas, end); + goto out; + } + + has_attrs = true; +for (index = start; index < end; index++) { +do { + entry =xas_next (&xas); + }while (xas_retry (&xas, entry)); + +if (xas.xa_index != index ||xa_to_value (entry) != attrs) { + has_attrs = false; +break ; + } + }
Quoted 8 lines
arch/x86/kvm/mmu/mmu.c void kvm_mmu_pre_destroy_vm(struct kvm *kvm) · line 7288
Quoted 6 lines
arch/x86/kvm/mmu/mmu.c +6
-0+ +out: +rcu_read_unlock (); +return has_attrs; +} +Can you move this function to
virt/kvm/kvm_main.c?
Ah, yeah, that's a good idea.
Sean Christopherson
maintainerRFC PATCH v11 10/29 mm: Add AS_UNMOVABLE to mark mapping as completely unmovable
In reply to Sean Christopherson--- include/linux/pagemap.h | 11 +++++++++++ mm/compaction.c | 4 ++++ mm/migrate.c | 2 ++ 3 files changed, 17 insertions(+)
include/linux/pagemap.h +11 -0
@@ -203,6 +203,7 @@ enum mapping_flags {/* writeback related tags are not used */ AS_NO_WRITEBACK_TAGS =5 ,AS_LARGE_FOLIO_SUPPORT =6 , + AS_UNMOVABLE = 7, /* The mapping cannot be moved, ever */ };/**
@@ -273,6 +274,16 @@ static inline int mapping_use_writeback_tags(struct address_space *mapping)return !test_bit (AS_NO_WRITEBACK_TAGS , &mapping->flags); } +static inline void mapping_set_unmovable (struct address_space *mapping) +{ +set_bit (AS_UNMOVABLE , &mapping->flags); +} + +static inline bool mapping_unmovable (struct address_space *mapping) +{ +return test_bit (AS_UNMOVABLE , &mapping->flags); +} +static inline gfp_t mapping_gfp_mask (struct address_space * mapping) {return mapping->gfp_mask;
mm/compaction.c +4 -0
@@ -1047,6 +1047,10 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,if (!mapping && (folio_ref_count (folio) -1 ) >folio_mapcount (folio)) goto isolate_fail_put; +/* The mapping truly isn't movable. */ +if (mapping &&mapping_unmovable (mapping)) + goto isolate_fail_put; +/* * Only allow to migrate anonymous pages in GFP_NOFS context * because those do not depend on fs locks.
mm/migrate.c +2 -0
@@ -954,6 +954,8 @@ static int move_to_new_folio(struct folio *dst, struct folio *src,if (!mapping) rc =migrate_folio (mapping, dst, src, mode); +else if (mapping_unmovable (mapping)) + rc = -EOPNOTSUPP ;else if (mapping->a_ops->migrate_folio)/* * Most folios have a mapping and most filesystems
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Re: [RFC PATCH v11 10/29] mm: Add AS_UNMOVABLE to mark mapping as completely unmovable
mm/compaction.c isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, · line 1053
Quoted 12 lines · Sean Christopherson
mm/compaction.c +4
-0@@ -1047,6 +1047,10 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,if (!mapping && (folio_ref_count (folio) -1 ) >folio_mapcount (folio)) goto isolate_fail_put; +/* The mapping truly isn't movable. */ +if (mapping &&mapping_unmovable (mapping)) + goto isolate_fail_put; +
I doubt that it is safe to dereference mapping here. I believe the folio can be truncated from under us and the mapping freed with the inode.
The folio has to be locked to dereference mapping safely (given that the mapping is still tied to the folio).
Vlastimil, any comments?
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Matthew Wilcox
maintainerRe: [RFC PATCH v11 10/29] mm: Add AS_UNMOVABLE to mark mapping as completely unmovable
Quoted 17 lines · Kirill A . Shutemov
mm/compaction.c isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, · line 1053
Quoted 12 lines · Sean Christopherson
mm/compaction.c +4
-0@@ -1047,6 +1047,10 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,if (!mapping && (folio_ref_count (folio) -1 ) >folio_mapcount (folio)) goto isolate_fail_put; +/* The mapping truly isn't movable. */ +if (mapping &&mapping_unmovable (mapping)) + goto isolate_fail_put; +I doubt that it is safe to dereference mapping here. I believe the folio can be truncated from under us and the mapping freed with the inode.
Quoted 2 lines
The folio has to be locked to dereference mapping safely (given that the mapping is still tied to the folio).
There's even a comment to that effect later on in the function:
/*
* Only pages without mappings or that have a
* ->migrate_folio callback are possible to migrate
* without blocking. However, we can be racing with
* truncation so it's necessary to lock the page
* to stabilise the mapping as truncation holds
* the page lock until after the page is removed
* from the page cache.
*/
(that could be reworded to make it clear how dangerous dereferencing ->mapping is without the lock ... and it does need to be changed to say "folio lock" instead of "page lock", so ...)
How does this look?
/*
* Only folios without mappings or that have
* a ->migrate_folio callback are possible to
* migrate without blocking. However, we can
* be racing with truncation, which can free
* the mapping. Truncation holds the folio lock
* until after the folio is removed from the page
* cache so holding it ourselves is sufficient. */
Re: [RFC PATCH v11 10/29] mm: Add AS_UNMOVABLE to mark mapping as completely unmovable
Quoted 39 lines · Matthew Wilcox
Quoted 17 lines · Kirill A . Shutemov
mm/compaction.c isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, · line 1053
Quoted 12 lines · Sean Christopherson
mm/compaction.c +4
-0@@ -1047,6 +1047,10 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,if (!mapping && (folio_ref_count (folio) -1 ) >folio_mapcount (folio)) goto isolate_fail_put; +/* The mapping truly isn't movable. */ +if (mapping &&mapping_unmovable (mapping)) + goto isolate_fail_put; +I doubt that it is safe to dereference mapping here. I believe the folio can be truncated from under us and the mapping freed with the inode.
Quoted 2 lines
The folio has to be locked to dereference mapping safely (given that the mapping is still tied to the folio).
There's even a comment to that effect later on in the function:
/*
* Only pages without mappings or that have a
* ->migrate_folio callback are possible to migrate
* without blocking. However, we can be racing with
* truncation so it's necessary to lock the page
* to stabilise the mapping as truncation holds
* the page lock until after the page is removed
* from the page cache.
*/(that could be reworded to make it clear how dangerous dereferencing ->mapping is without the lock ... and it does need to be changed to say "folio lock" instead of "page lock", so ...)
How does this look?
Quoted 10 lines
/*
* Only folios without mappings or that have
* a ->migrate_folio callback are possible to
* migrate without blocking. However, we can
* be racing with truncation, which can free
* the mapping. Truncation holds the folio lock
* until after the folio is removed from the page
* cache so holding it ourselves is sufficient. */
Looks good to me.
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Re: [RFC PATCH v11 10/29] mm: Add AS_UNMOVABLE to mark mapping as completely unmovable
In reply to Matthew WilcoxQuoted 18 lines · Matthew Wilcox
Quoted 17 lines · Kirill A . Shutemov
mm/compaction.c isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, · line 1053
Quoted 12 lines · Sean Christopherson
mm/compaction.c +4
-0@@ -1047,6 +1047,10 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,if (!mapping && (folio_ref_count (folio) -1 ) >folio_mapcount (folio)) goto isolate_fail_put; +/* The mapping truly isn't movable. */ +if (mapping &&mapping_unmovable (mapping)) + goto isolate_fail_put; +I doubt that it is safe to dereference mapping here. I believe the folio can be truncated from under us and the mapping freed with the inode.
Quoted 4 lines
Quoted 2 lines
The folio has to be locked to dereference mapping safely (given that the mapping is still tied to the folio).
There's even a comment to that effect later on in the function:
Hmm, well spotted. But it wouldn't be so great if we now had to lock every inspected page (and not just dirty pages), just to check the AS_ bit.
But I wonder if this is leftover from previous versions. Are the guest pages even PageLRU currently? (and should they be, given how they can't be swapped out or anything?) If not, isolate_migratepages_block will skip them anyway.
Quoted 27 lines
/*
* Only pages without mappings or that have a
* ->migrate_folio callback are possible to migrate
* without blocking. However, we can be racing with
* truncation so it's necessary to lock the page
* to stabilise the mapping as truncation holds
* the page lock until after the page is removed
* from the page cache.
*/(that could be reworded to make it clear how dangerous dereferencing ->mapping is without the lock ... and it does need to be changed to say "folio lock" instead of "page lock", so ...)
How does this look?
/*
* Only folios without mappings or that have
* a ->migrate_folio callback are possible to
* migrate without blocking. However, we can
* be racing with truncation, which can free
* the mapping. Truncation holds the folio lock
* until after the folio is removed from the page
* cache so holding it ourselves is sufficient. */
Paolo Bonzini
maintainerRe: [RFC PATCH v11 10/29] mm: Add AS_UNMOVABLE to mark mapping as completely unmovable
Quoted 7 lines · Vlastimil Babka
Quoted 1 line
There's even a comment to that effect later on in the function:
Hmm, well spotted. But it wouldn't be so great if we now had to lock every inspected page (and not just dirty pages), just to check the
AS_bit.But I wonder if this is leftover from previous versions. Are the guest pages even PageLRU currently? (and should they be, given how they can't be swapped out or anything?) If not,
isolate_migratepages_blockwill skip them anyway.
No, they're not (migration or even swap-out is not excluded for the future, but for now it's left for future work.
Paolo
Re: [RFC PATCH v11 10/29] mm: Add AS_UNMOVABLE to mark mapping as completely unmovable
In reply to Matthew WilcoxQuoted 39 lines · Matthew Wilcox
Quoted 17 lines · Kirill A . Shutemov
mm/compaction.c isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, · line 1053
Quoted 12 lines · Sean Christopherson
mm/compaction.c +4
-0@@ -1047,6 +1047,10 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,if (!mapping && (folio_ref_count (folio) -1 ) >folio_mapcount (folio)) goto isolate_fail_put; +/* The mapping truly isn't movable. */ +if (mapping &&mapping_unmovable (mapping)) + goto isolate_fail_put; +I doubt that it is safe to dereference mapping here. I believe the folio can be truncated from under us and the mapping freed with the inode.
Quoted 2 lines
The folio has to be locked to dereference mapping safely (given that the mapping is still tied to the folio).
There's even a comment to that effect later on in the function:
/*
* Only pages without mappings or that have a
* ->migrate_folio callback are possible to migrate
* without blocking. However, we can be racing with
* truncation so it's necessary to lock the page
* to stabilise the mapping as truncation holds
* the page lock until after the page is removed
* from the page cache.
*/(that could be reworded to make it clear how dangerous dereferencing ->mapping is without the lock ... and it does need to be changed to say "folio lock" instead of "page lock", so ...)
How does this look?
Quoted 9 lines
/*
* Only folios without mappings or that have
* a ->migrate_folio callback are possible to
* migrate without blocking. However, we can
* be racing with truncation, which can free
* the mapping. Truncation holds the folio lock
* until after the folio is removed from the page
* cache so holding it ourselves is sufficient. */
Incorporated to my attempt at a fix (posted separately per the requested process):
https://lore.kernel.org/all/20230901082025.20548-2-vbabka at suse.cz/
Sean Christopherson
maintainerRFC PATCH v11 11/29 security: Export security_inode_init_security_anon() for use by KVM
In reply to Sean Christopherson--- security/security.c | 1 + 1 file changed, 1 insertion(+)
security/security.c +1 -0
@@ -1654,6 +1654,7 @@ int security_inode_init_security_anon(struct inode *inode,return call_int_hook (inode_init_security_anon,0 , inode, name, context_inode); } +EXPORT_SYMBOL_GPL (security_inode_init_security_anon);#ifdef CONFIG_SECURITY_PATH /**
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Re: [RFC PATCH v11 11/29] security: Export security_inode_init_security_anon() for use by KVM
Quoted 5 lines · On Tue, Jul 18, 2023 at 7:48?PM Sean Christopherson <[email protected]> wrote:
Signed-off-by Sean Christopherson <[email protected]>--- security/security.c | 1 + 1 file changed, 1 insertion(+)
Quoted 13 lines
security/security.c +1
-1@@ -1654,6 +1654,7 @@ int security_inode_init_security_anon(struct inode *inode,return call_int_hook (inode_init_security_anon,0 , inode, name, context_inode); } +EXPORT_SYMBOL_GPL (security_inode_init_security_anon);#ifdef CONFIG_SECURITY_PATH /** --
Quoted 1 line
2.41.0.255.g8b1d071c50-goog
--
paul-moore.com
Re: [RFC PATCH v11 11/29] security: Export security_inode_init_security_anon() for use by KVM
In reply to Sean ChristophersonQuoted 1 line · Sean Christopherson
Signed-off-by Sean Christopherson <[email protected]>
Process wise this will probably be frowned upon when done separately, so I'd fold it in the patch using the export, seems to be the next one.
Quoted 16 lines
--- security/security.c | 1 + 1 file changed, 1 insertion(+)security/security.c +1
-0@@ -1654,6 +1654,7 @@ int security_inode_init_security_anon(struct inode *inode,return call_int_hook (inode_init_security_anon,0 , inode, name, context_inode); } +EXPORT_SYMBOL_GPL (security_inode_init_security_anon);#ifdef CONFIG_SECURITY_PATH /**
Sean Christopherson
maintainerRFC PATCH v11 12/29 KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
In reply to Sean ChristophersonTODO
--- include/linux/kvm_host.h | 48 +++ include/uapi/linux/kvm.h | 14 +- include/uapi/linux/magic.h | 1 + virt/kvm/Kconfig | 4 + virt/kvm/Makefile.kvm | 1 + virt/kvm/guest_mem.c | 591 +++++++++++++++++++++++++++++++++++++ virt/kvm/kvm_main.c | 58 +++- virt/kvm/kvm_mm.h | 38 +++ 8 files changed, 750 insertions(+), 5 deletions(-) create mode 100644 virt/kvm/guest_mem.c
include/linux/kvm_host.h +48 -0
@@ -592,8 +592,20 @@ struct kvm_memory_slot {u32 flags;short id;u16 as_id; + +#ifdef CONFIG_KVM_PRIVATE_MEM +struct { +struct file __rcu *file; +pgoff_t pgoff; + } gmem; +#endif }; +static inline bool kvm_slot_can_be_private (const struct kvm_memory_slot *slot) +{ +return slot && (slot->flags &KVM_MEM_PRIVATE ); +} +static inline bool kvm_slot_dirty_track_enabled (const struct kvm_memory_slot *slot) {return slot->flags &KVM_MEM_LOG_DIRTY_PAGES ;
@@ -688,6 +700,17 @@ static inline int kvm_arch_vcpu_memslots_id(struct kvm_vcpu *vcpu) }#endif +/* +* Arch code must define kvm_arch_has_private_mem if support for private memory +* is enabled. +*/ +#if !defined(kvm_arch_has_private_mem) && !IS_ENABLED (CONFIG_KVM_PRIVATE_MEM ) +static inline bool kvm_arch_has_private_mem (struct kvm *kvm) +{ +return false; +} +#endif +struct kvm_memslots {u64 generation;atomic_long_t last_used_slot;
@@ -1380,6 +1403,7 @@ void *kvm_mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc);void kvm_mmu_invalidate_begin (struct kvm *kvm);void kvm_mmu_invalidate_range_add (struct kvm *kvm,gfn_t start,gfn_t end);void kvm_mmu_invalidate_end (struct kvm *kvm); +bool kvm_mmu_unmap_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range);long kvm_arch_dev_ioctl (struct file *filp,unsigned int ioctl,unsigned long arg);
@@ -2313,6 +2337,30 @@ static inline unsigned long kvm_get_memory_attributes(struct kvm *kvm, gfn_t gfnbool kvm_arch_post_set_memory_attributes (struct kvm *kvm,struct kvm_gfn_range *range); + +static inline bool kvm_mem_is_private (struct kvm *kvm,gfn_t gfn) +{ +return IS_ENABLED (CONFIG_KVM_PRIVATE_MEM ) && +kvm_get_memory_attributes (kvm, gfn) &KVM_MEMORY_ATTRIBUTE_PRIVATE ; +} +#else +static inline bool kvm_mem_is_private (struct kvm *kvm,gfn_t gfn) +{ +return false; +}#endif /* CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES */ +#ifdef CONFIG_KVM_PRIVATE_MEM +int kvm_gmem_get_pfn (struct kvm *kvm,struct kvm_memory_slot *slot, +gfn_t gfn,kvm_pfn_t *pfn,int *max_order); +#else +static inline int kvm_gmem_get_pfn (struct kvm *kvm, +struct kvm_memory_slot *slot,gfn_t gfn, +kvm_pfn_t *pfn,int *max_order) +{ +KVM_BUG_ON (1 , kvm); +return -EIO ; +} +#endif /* CONFIG_KVM_PRIVATE_MEM */ +#endif
include/uapi/linux/kvm.h +13 -1
@@ -102,7 +102,10 @@ struct kvm_userspace_memory_region2 {__u64 guest_phys_addr;__u64 memory_size;__u64 userspace_addr; -__u64 pad[16 ]; +__u64 gmem_offset; +__u32 gmem_fd; +__u32 pad1; +__u64 pad2[14 ]; };/*
@@ -112,6 +115,7 @@ struct kvm_userspace_memory_region2 {*/ #define KVM_MEM_LOG_DIRTY_PAGES (1UL << 0)#define KVM_MEM_READONLY (1UL << 1) +#define KVM_MEM_PRIVATE (1UL << 2)/* for KVM_IRQ_LINE */ struct kvm_irq_level {
@@ -2284,4 +2288,12 @@ struct kvm_memory_attributes {#define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3) +#define KVM_CREATE_GUEST_MEMFD _IOWR(KVMIO, 0xd4, struct kvm_create_guest_memfd) + +struct kvm_create_guest_memfd { +__u64 size; +__u64 flags; +__u64 reserved[6 ]; +}; +#endif /* __LINUX_KVM_H */
include/uapi/linux/magic.h +1 -0
@@ -101,5 +101,6 @@#define DMA_BUF_MAGIC 0x444d4142/* "DMAB" */ #define DEVMEM_MAGIC 0x454d444d/* "DMEM" */ #define SECRETMEM_MAGIC 0x5345434d/* "SECM" */ +#define GUEST_MEMORY_MAGIC 0x474d454d/* "GMEM" */ #endif /* __LINUX_MAGIC_H__ */
virt/kvm/Kconfig +4 -0
@@ -103,3 +103,7 @@ config KVM_GENERIC_MMU_NOTIFIER config KVM_GENERIC_MEMORY_ATTRIBUTES select KVM_GENERIC_MMU_NOTIFIER bool + +config KVM_PRIVATE_MEM + select XARRAY_MULTI + bool
virt/kvm/Makefile.kvm +1 -0
@@ -12,3 +12,4 @@ kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o kvm-$(CONFIG_HAVE_KVM_IRQ_ROUTING) += $(KVM)/irqchip.o kvm-$(CONFIG_HAVE_KVM_DIRTY_RING) += $(KVM)/dirty_ring.o kvm-$(CONFIG_HAVE_KVM_PFNCACHE) += $(KVM)/pfncache.o +kvm-$(CONFIG_KVM_PRIVATE_MEM) += $(KVM)/guest_mem.o
virt/kvm/guest_mem.c new file +591 -0
591 lines not on this page · raw message
virt/kvm/kvm_main.c +54 -4
136 lines not on this page · raw message
virt/kvm/kvm_mm.h +38 -0
@@ -37,4 +37,42 @@ static inline void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm, }#endif /* HAVE_KVM_PFNCACHE */ +#ifdef CONFIG_KVM_PRIVATE_MEM +int kvm_gmem_init (void ); +void kvm_gmem_exit (void ); +int kvm_gmem_create (struct kvm *kvm,struct kvm_create_guest_memfd *args); +int kvm_gmem_bind (struct kvm *kvm,struct kvm_memory_slot *slot, +unsigned int fd,loff_t offset); +void kvm_gmem_unbind (struct kvm_memory_slot *slot); +#else +static inline int kvm_gmem_init (void ) +{ +return 0 ; +} + +static inline void kvm_gmem_exit (void ) +{ + +} + +static inline int kvm_gmem_create (struct kvm *kvm, +struct kvm_create_guest_memfd *args) +{ +return -EOPNOTSUPP ; +} + +static inline int kvm_gmem_bind (struct kvm *kvm, +struct kvm_memory_slot *slot, +unsigned int fd,loff_t offset) +{ +WARN_ON_ONCE (1 ); +return -EIO ; +} + +static inline void kvm_gmem_unbind (struct kvm_memory_slot *slot) +{ +WARN_ON_ONCE (1 ); +} +#endif /* CONFIG_KVM_PRIVATE_MEM */ +#endif /* __KVM_MM_H__ */
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Re: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
Quoted 19 lines · On Tue, Jul 18, 2023 at 4:49?PM Sean Christopherson <[email protected]> wrote:
...
virt/kvm/guest_mem.c new file +18
-0+static int kvm_gmem_error_page (struct address_space *mapping,struct page *page) +{ +struct list_head *gmem_list = &mapping->private_list; +struct kvm_memory_slot *slot; +struct kvm_gmem *gmem; +unsigned long index; +pgoff_t start, end; +gfn_t gfn; + +filemap_invalidate_lock_shared (mapping); + + start = page->index; + end = start +thp_nr_pages (page); + +list_for_each_entry (gmem, gmem_list, entry) { +xa_for_each_range (&gmem->bindings, index, slot, start, end -1 ) { +for (gfn = start; gfn < end; gfn++) { + if (WARN_ON_ONCE(gfn < slot->base_gfn ||
virt/kvm/guest_mem.c line 295
Quoted 10 lines
virt/kvm/guest_mem.c new file +10
-0+ gfn >= slot->base_gfn + slot->npages)) +continue ; + +/* +* FIXME: Tell userspace that the *private* +* memory encountered an error. +*/ +send_sig_mceerr (BUS_MCEERR_AR , + (void __user *)gfn_to_hva_memslot (slot, gfn), +PAGE_SHIFT , current);
Does it make sense to replicate what happens with MCE handling on tmpfs backed guest memory:
1) Unmap gpa from guest
2) On the next guest EPT fault, exit to userspace to handle/log the mce error for the gpa.
IIUC, such MCEs could be asynchronous and "current" might not always be the intended recipient of this signal.
Quoted 9 lines
virt/kvm/guest_mem.c new file +9
-0+ } + } + } + +filemap_invalidate_unlock_shared (mapping); + +return 0 ; +} +
Sean Christopherson
maintainerRe: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
Quoted 28 lines · Vishal Annapurve
Quoted 27 lines · On Tue, Jul 18, 2023 at 4:49?PM Sean Christopherson <[email protected]> wrote:
...
virt/kvm/guest_mem.c new file +26
-0+static int kvm_gmem_error_page (struct address_space *mapping,struct page *page) +{ +struct list_head *gmem_list = &mapping->private_list; +struct kvm_memory_slot *slot; +struct kvm_gmem *gmem; +unsigned long index; +pgoff_t start, end; +gfn_t gfn; + +filemap_invalidate_lock_shared (mapping); + + start = page->index; + end = start +thp_nr_pages (page); + +list_for_each_entry (gmem, gmem_list, entry) { +xa_for_each_range (&gmem->bindings, index, slot, start, end -1 ) { +for (gfn = start; gfn < end; gfn++) { +if (WARN_ON_ONCE (gfn < slot->base_gfn || + gfn >= slot->base_gfn + slot->npages)) +continue ; + +/* +* FIXME: Tell userspace that the *private* +* memory encountered an error. +*/ + send_sig_mceerr(BUS_MCEERR_AR,
Quoted 8 lines
virt/kvm/guest_mem.c line 295
Quoted 2 lines
virt/kvm/guest_mem.c new file +2
-0+ (void __user *)gfn_to_hva_memslot (slot, gfn), + PAGE_SHIFT, current);Does it make sense to replicate what happens with MCE handling on tmpfs backed guest memory:
1) Unmap gpa from guest
2) On the next guest EPT fault, exit to userspace to handle/log the mce error for the gpa.
Hmm, yes, that would be much better. Ah, and kvm_gmem_get_pfn() needs to check folio_test_hwpoison() and potentially PageHWPoison(). E.g. if the folio is huge, KVM needs to restrict the mapping to order-0 (target page isn't poisoned), or return KVM_PFN_ERR_HWPOISON (taget page IS poisoned).
Alternatively, KVM could punch a hole in kvm_gmem_error_page(), but I don't think we want to do that because that would prevent forwarding the #MC to the guest.
Re: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
In reply to Sean ChristophersonQuoted 4 lines · Sean Christopherson
virt/kvm/kvm_main.c +0
-0@@ -5134,6 +5167,16 @@ static long kvm_vm_ioctl(struct file *filp,case KVM_GET_STATS_FD : r =kvm_vm_ioctl_get_stats_fd (kvm);break ;
virt/kvm/kvm_main.c static long kvm_vm_ioctl(struct file *filp, · line 5179
Quoted 10 lines
virt/kvm/kvm_main.c +10
-0+case KVM_CREATE_GUEST_MEMFD : { +struct kvm_create_guest_memfd guest_memfd; + + r = -EFAULT ; +if (copy_from_user (&guest_memfd, argp,sizeof (guest_memfd))) + goto out; + + r =kvm_gmem_create (kvm, &guest_memfd); +break ; + }
Does it need a new CAP to indicate the support of guest_memfd?
This is patch series introduces 3 new CAPs and it seems any one of them can serve as the indicator of guest_memfd.
+#define KVM_CAP_USER_MEMORY2 230
+#define KVM_CAP_MEMORY_ATTRIBUTES 231
+#define KVM_CAP_VM_TYPES 232
or we just go and try the ioctl, the return value will tell the result?
Sean Christopherson
maintainerRe: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
Quoted 17 lines · Xiaoyao Li
Quoted 4 lines · Sean Christopherson
virt/kvm/kvm_main.c +0
-0@@ -5134,6 +5167,16 @@ static long kvm_vm_ioctl(struct file *filp,case KVM_GET_STATS_FD : r =kvm_vm_ioctl_get_stats_fd (kvm);break ;virt/kvm/kvm_main.c static long kvm_vm_ioctl(struct file *filp, · line 5179
Quoted 10 lines
virt/kvm/kvm_main.c +10
-0+case KVM_CREATE_GUEST_MEMFD : { +struct kvm_create_guest_memfd guest_memfd; + + r = -EFAULT ; +if (copy_from_user (&guest_memfd, argp,sizeof (guest_memfd))) + goto out; + + r =kvm_gmem_create (kvm, &guest_memfd); +break ; + }Does it need a new CAP to indicate the support of
guest_memfd?
Yeah, I meant to add that to the TODO list and forgot (obviously).
Review
Quoted 6 lines
This is patch series introduces 3 new CAPs and it seems any one of them can serve as the indicator of
guest_memfd.(quoted diff) +3
-0+#define KVM_CAP_USER_MEMORY2 230 +#define KVM_CAP_MEMORY_ATTRIBUTES 231 +#define KVM_CAP_VM_TYPES 232
The number of new caps being added is the main why I didn't just add another one. On the other hand, we have room for a few billion caps, so one more isn't a big deal. So yeah, KVM_CAP_GUEST_MEMFD is probably the way to go.
Re: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
In reply to Sean ChristophersonQuoted 34 lines · Sean Christopherson
(quoted diff) +34
-0+static int kvm_gmem_release(struct inode *inode, struct file *file) +{ + struct kvm_gmem *gmem = file->private_data; + struct kvm_memory_slot *slot; + struct kvm *kvm = gmem->kvm; + unsigned long index; + + filemap_invalidate_lock(inode->i_mapping); + + /* + * Prevent concurrent attempts to *unbind* a memslot. This is the last + * reference to the file and thus no new bindings can be created, but + * dereferencing the slot for existing bindings needs to be protected + * against memslot updates, specifically so that unbind doesn't race + * and free the memslot (kvm_gmem_get_file() will return NULL). + */ + mutex_lock(&kvm->slots_lock); + + xa_for_each(&gmem->bindings, index, slot) + rcu_assign_pointer(slot->gmem.file, NULL); + + synchronize_rcu(); + + /* + * All in-flight operations are gone and new bindings can be created. + * Zap all SPTEs pointed at by this file. Do not free the backing + * memory, as its lifetime is associated with the inode, not the file. + */ + kvm_gmem_invalidate_begin(gmem, 0, -1ul); + kvm_gmem_invalidate_end(gmem, 0, -1ul); + + mutex_unlock(&kvm->slots_lock); + + list_del(&gmem->entry);
Review
Quoted 10 lines
(quoted diff) +10
-0+ + filemap_invalidate_unlock(inode->i_mapping); + + xa_destroy(&gmem->bindings); + kfree(gmem); + + kvm_put_kvm(kvm); + + return 0; +}
The lockdep complains with the filemapping lock and the kvm slot lock.
From bc45eb084a761f93a87ba1f6d3a9949c17adeb31 Mon Sep 17 00:00:00 2001
Message-Id: <bc45eb084a761f93a87ba1f6d3a9949c17adeb31.1689888438.git.isaku.yamahata@intel.com>
From: Isaku Yamahata <[email protected]>
Date: Thu, 20 Jul 2023 14:16:21 -0700
Subject: [PATCH] KVM/gmem: Fix locking ordering in kvm_gmem_release()
The lockdep complains the locking order. Fix kvm_gmem_release()
VM destruction:
- fput()
...
\-kvm_gmem_release()
\-filemap_invalidate_lock(inode->i_mapping);
lock(&kvm->slots_lock);slot creation:kvm_set_memory_region()
mutex_lock(&kvm->slots_lock);
__kvm_set_memory_region(kvm, mem);
\-kvm_gmem_bind()
\-filemap_invalidate_lock(inode->i_mapping);======================================================
WARNING: possible circular locking dependency detected
------------------------------------------------------
...
the existing dependency chain (in reverse order) is:
-> #1 (mapping.invalidate_lock#4){+.+.}-{4:4}:
...
down_write+0x40/0xe0
kvm_gmem_bind+0xd9/0x1b0 [kvm]
__kvm_set_memory_region.part.0+0x4fc/0x620 [kvm]
__kvm_set_memory_region+0x6b/0x90 [kvm]
kvm_vm_ioctl+0x350/0xa00 [kvm]
__x64_sys_ioctl+0x95/0xd0
do_syscall_64+0x39/0x90
entry_SYSCALL_64_after_hwframe+0x6e/0xd8-> #0 (&kvm->slots_lock){+.+.}-{4:4}:
...
mutex_lock_nested+0x1b/0x30
kvm_gmem_release+0x56/0x1b0 [kvm]
__fput+0x115/0x2e0
____fput+0xe/0x20
task_work_run+0x5e/0xb0
do_exit+0x2dd/0x5b0
do_group_exit+0x3b/0xb0
__x64_sys_exit_group+0x18/0x20
do_syscall_64+0x39/0x90
entry_SYSCALL_64_after_hwframe+0x6e/0xd8other info that might help us debug this:
Possible unsafe locking scenario:
CPU0 CPU1
---- ----
lock(mapping.invalidate_lock#4);
lock(&kvm->slots_lock);
lock(mapping.invalidate_lock#4); lock(&kvm->slots_lock);
--- virt/kvm/guest_mem.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
virt/kvm/guest_mem.c +4 -4
@@ -274,8 +274,6 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)struct kvm *kvm = gmem->kvm;unsigned long index; -filemap_invalidate_lock (inode->i_mapping); -/* * Prevent concurrent attempts to *unbind* a memslot. This is the last * reference to the file and thus no new bindings can be created, but
@@ -285,6 +283,8 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)*/ mutex_lock (&kvm->slots_lock); +filemap_invalidate_lock (inode->i_mapping); +xa_for_each (&gmem->bindings, index, slot)rcu_assign_pointer (slot->gmem.file,NULL );
@@ -299,12 +299,12 @@ static int kvm_gmem_release(struct inode *inode, struct file *file)kvm_gmem_issue_arch_invalidate (gmem->kvm,file_inode (file),0 ,-1ul );kvm_gmem_invalidate_end (gmem,0 ,-1ul ); -mutex_unlock (&kvm->slots_lock); -list_del (&gmem->entry);filemap_invalidate_unlock (inode->i_mapping); +mutex_unlock (&kvm->slots_lock); +xa_destroy (&gmem->bindings);kfree (gmem);
Signature · 6 lines
2.25.1 -- Isaku Yamahata <[email protected]>
Re: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
In reply to Sean ChristophersonQuoted 466 lines · Sean Christopherson
TODO
Cc Fuad Tabba <[email protected]>Cc Vishal Annapurve <[email protected]>Cc Ackerley Tng <[email protected]>Cc Jarkko Sakkinen <[email protected]>Cc Maciej Szmigiero <[email protected]>Cc Vlastimil Babka <[email protected]>Cc David Hildenbrand <[email protected]>Cc Quentin Perret <[email protected]>Cc Michael Roth <[email protected]>Cc Wang <[email protected]>Cc Liam Merwick <[email protected]>Cc Isaku Yamahata <[email protected]>Co-developed-by Kirill A. Shutemov <[email protected]>Signed-off-by Kirill A. Shutemov <[email protected]>Co-developed-by Yu Zhang <[email protected]>Signed-off-by Yu Zhang <[email protected]>Co-developed-by Chao Peng <[email protected]>Signed-off-by Chao Peng <[email protected]>Co-developed-by Ackerley Tng <[email protected]>Signed-off-by Ackerley Tng <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- include/linux/kvm_host.h | 48 +++ include/uapi/linux/kvm.h | 14 +- include/uapi/linux/magic.h | 1 + virt/kvm/Kconfig | 4 + virt/kvm/Makefile.kvm | 1 + virt/kvm/guest_mem.c | 591 +++++++++++++++++++++++++++++++++++++ virt/kvm/kvm_main.c | 58 +++- virt/kvm/kvm_mm.h | 38 +++ 8 files changed, 750 insertions(+), 5 deletions(-) create mode 100644 virt/kvm/guest_mem.cinclude/linux/kvm_host.h +48
-0@@ -592,8 +592,20 @@ struct kvm_memory_slot {u32 flags;short id;u16 as_id; + +#ifdef CONFIG_KVM_PRIVATE_MEM +struct { +struct file __rcu *file; +pgoff_t pgoff; + } gmem; +#endif }; +static inline bool kvm_slot_can_be_private (const struct kvm_memory_slot *slot) +{ +return slot && (slot->flags &KVM_MEM_PRIVATE ); +} +static inline bool kvm_slot_dirty_track_enabled (const struct kvm_memory_slot *slot) {return slot->flags &KVM_MEM_LOG_DIRTY_PAGES ;@@ -688,6 +700,17 @@ static inline int kvm_arch_vcpu_memslots_id(struct kvm_vcpu *vcpu) }#endif +/* +* Arch code must define kvm_arch_has_private_mem if support for private memory +* is enabled. +*/ +#if !defined(kvm_arch_has_private_mem) && !IS_ENABLED (CONFIG_KVM_PRIVATE_MEM ) +static inline bool kvm_arch_has_private_mem (struct kvm *kvm) +{ +return false; +} +#endif +struct kvm_memslots {u64 generation;atomic_long_t last_used_slot;@@ -1380,6 +1403,7 @@ void *kvm_mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc);void kvm_mmu_invalidate_begin (struct kvm *kvm);void kvm_mmu_invalidate_range_add (struct kvm *kvm,gfn_t start,gfn_t end);void kvm_mmu_invalidate_end (struct kvm *kvm); +bool kvm_mmu_unmap_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range);long kvm_arch_dev_ioctl (struct file *filp,unsigned int ioctl,unsigned long arg);@@ -2313,6 +2337,30 @@ static inline unsigned long kvm_get_memory_attributes(struct kvm *kvm, gfn_t gfnbool kvm_arch_post_set_memory_attributes (struct kvm *kvm,struct kvm_gfn_range *range); + +static inline bool kvm_mem_is_private (struct kvm *kvm,gfn_t gfn) +{ +return IS_ENABLED (CONFIG_KVM_PRIVATE_MEM ) && +kvm_get_memory_attributes (kvm, gfn) &KVM_MEMORY_ATTRIBUTE_PRIVATE ; +} +#else +static inline bool kvm_mem_is_private (struct kvm *kvm,gfn_t gfn) +{ +return false; +}#endif /* CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES */ +#ifdef CONFIG_KVM_PRIVATE_MEM +int kvm_gmem_get_pfn (struct kvm *kvm,struct kvm_memory_slot *slot, +gfn_t gfn,kvm_pfn_t *pfn,int *max_order); +#else +static inline int kvm_gmem_get_pfn (struct kvm *kvm, +struct kvm_memory_slot *slot,gfn_t gfn, +kvm_pfn_t *pfn,int *max_order) +{ +KVM_BUG_ON (1 , kvm); +return -EIO ; +} +#endif /* CONFIG_KVM_PRIVATE_MEM */ +#endif include/uapi/linux/kvm.h +13
-1@@ -102,7 +102,10 @@ struct kvm_userspace_memory_region2 {__u64 guest_phys_addr;__u64 memory_size;__u64 userspace_addr; -__u64 pad[16 ]; +__u64 gmem_offset; +__u32 gmem_fd; +__u32 pad1; +__u64 pad2[14 ]; };/* @@ -112,6 +115,7 @@ struct kvm_userspace_memory_region2 {*/ #define KVM_MEM_LOG_DIRTY_PAGES (1UL << 0)#define KVM_MEM_READONLY (1UL << 1) +#define KVM_MEM_PRIVATE (1UL << 2)/* for KVM_IRQ_LINE */ struct kvm_irq_level {@@ -2284,4 +2288,12 @@ struct kvm_memory_attributes {#define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3) +#define KVM_CREATE_GUEST_MEMFD _IOWR(KVMIO, 0xd4, struct kvm_create_guest_memfd) + +struct kvm_create_guest_memfd { +__u64 size; +__u64 flags; +__u64 reserved[6 ]; +}; +#endif /* __LINUX_KVM_H */ include/uapi/linux/magic.h +1
-0@@ -101,5 +101,6 @@#define DMA_BUF_MAGIC 0x444d4142/* "DMAB" */ #define DEVMEM_MAGIC 0x454d444d/* "DMEM" */ #define SECRETMEM_MAGIC 0x5345434d/* "SECM" */ +#define GUEST_MEMORY_MAGIC 0x474d454d/* "GMEM" */ #endif /* __LINUX_MAGIC_H__ */ virt/kvm/Kconfig +4
-0@@ -103,3 +103,7 @@ config KVM_GENERIC_MMU_NOTIFIER config KVM_GENERIC_MEMORY_ATTRIBUTES select KVM_GENERIC_MMU_NOTIFIER bool + +config KVM_PRIVATE_MEM + select XARRAY_MULTI + boolvirt/kvm/Makefile.kvm +1
-0@@ -12,3 +12,4 @@ kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o kvm-$(CONFIG_HAVE_KVM_IRQ_ROUTING) += $(KVM)/irqchip.o kvm-$(CONFIG_HAVE_KVM_DIRTY_RING) += $(KVM)/dirty_ring.o kvm-$(CONFIG_HAVE_KVM_PFNCACHE) += $(KVM)/pfncache.o +kvm-$(CONFIG_KVM_PRIVATE_MEM) += $(KVM)/guest_mem.ovirt/kvm/guest_mem.c new file +274
-0@@ -0,0 +1,591 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <linux/backing-dev.h> +#include <linux/falloc.h> +#include <linux/kvm_host.h> +#include <linux/pagemap.h> +#include <linux/pseudo_fs.h> + +#include <uapi/linux/magic.h> + +#include "kvm_mm.h" + +static struct vfsmount *kvm_gmem_mnt; + +struct kvm_gmem { +struct kvm *kvm; +struct xarray bindings; +struct list_head entry; +}; + +static struct folio *kvm_gmem_get_folio (struct file *file,pgoff_t index) +{ +struct folio *folio; + +/* TODO: Support huge pages. */ + folio =filemap_grab_folio (file->f_mapping, index); +if (!folio) +return NULL ; + +/* +* Use the up-to-date flag to track whether or not the memory has been +* zeroed before being handed off to the guest. There is no backing +* storage for the memory, so the folio will remain up-to-date until +* it's removed. +* +* TODO: Skip clearing pages when trusted firmware will do it when +* assigning memory to the guest. +*/ +if (!folio_test_uptodate (folio)) { +unsigned long nr_pages =folio_nr_pages (folio); +unsigned long i; + +for (i =0 ; i < nr_pages; i++) +clear_highpage (folio_page (folio, i)); + +folio_mark_uptodate (folio); + } + +/* +* Ignore accessed, referenced, and dirty flags. The memory is +* unevictable and there is no storage to write back to. +*/ +return folio; +} + +static void kvm_gmem_invalidate_begin (struct kvm_gmem *gmem,pgoff_t start, +pgoff_t end) +{ +struct kvm_memory_slot *slot; +struct kvm *kvm = gmem->kvm; +unsigned long index; +bool flush = false; + +KVM_MMU_LOCK (kvm); + +kvm_mmu_invalidate_begin (kvm); + +xa_for_each_range (&gmem->bindings, index, slot, start, end -1 ) { +pgoff_t pgoff = slot->gmem.pgoff; + +struct kvm_gfn_range gfn_range = { + .start = slot->base_gfn +max (pgoff, start) - pgoff, + .end = slot->base_gfn +min (pgoff + slot->npages, end) - pgoff, + .slot = slot, + .may_block = true, + }; + + flush |=kvm_mmu_unmap_gfn_range (kvm, &gfn_range); + } + +if (flush) +kvm_flush_remote_tlbs (kvm); + +KVM_MMU_UNLOCK (kvm); +} + +static void kvm_gmem_invalidate_end (struct kvm_gmem *gmem,pgoff_t start, +pgoff_t end) +{ +struct kvm *kvm = gmem->kvm; + +KVM_MMU_LOCK (kvm); +if (xa_find (&gmem->bindings, &start, end -1 ,XA_PRESENT )) +kvm_mmu_invalidate_end (kvm); +KVM_MMU_UNLOCK (kvm); +} + +static long kvm_gmem_punch_hole (struct inode *inode,loff_t offset,loff_t len) +{ +struct list_head *gmem_list = &inode->i_mapping->private_list; +pgoff_t start = offset >>PAGE_SHIFT ; +pgoff_t end = (offset + len) >>PAGE_SHIFT ; +struct kvm_gmem *gmem; + +/* +* Bindings must stable across invalidation to ensure the start+end +* are balanced. +*/ +filemap_invalidate_lock (inode->i_mapping); + +list_for_each_entry (gmem, gmem_list, entry) +kvm_gmem_invalidate_begin (gmem, start, end); + +truncate_inode_pages_range (inode->i_mapping, offset, offset + len -1 ); + +list_for_each_entry (gmem, gmem_list, entry) +kvm_gmem_invalidate_end (gmem, start, end); + +filemap_invalidate_unlock (inode->i_mapping); + +return 0 ; +} + +static long kvm_gmem_allocate (struct inode *inode,loff_t offset,loff_t len) +{ +struct address_space *mapping = inode->i_mapping; +pgoff_t start, index, end; +int r; + +/* Dedicated guest is immutable by default. */ +if (offset + len >i_size_read (inode)) +return -EINVAL ; + +filemap_invalidate_lock_shared (mapping); + + start = offset >>PAGE_SHIFT ; + end = (offset + len) >>PAGE_SHIFT ; + + r =0 ; +for (index = start; index < end; ) { +struct folio *folio; + +if (signal_pending (current)) { + r = -EINTR ; +break ; + } + + folio =kvm_gmem_get_folio (inode, index); +if (!folio) { + r = -ENOMEM ; +break ; + } + + index =folio_next_index (folio); + +folio_unlock (folio); +folio_put (folio); + +/* 64-bit only, wrapping the index should be impossible. */ +if (WARN_ON_ONCE (!index)) +break ; + +cond_resched (); + } + +filemap_invalidate_unlock_shared (mapping); + +return r; +} + +static long kvm_gmem_fallocate (struct file *file,int mode,loff_t offset, +loff_t len) +{ +int ret; + +if (!(mode &FALLOC_FL_KEEP_SIZE )) +return -EOPNOTSUPP ; + +if (mode & ~(FALLOC_FL_KEEP_SIZE |FALLOC_FL_PUNCH_HOLE )) +return -EOPNOTSUPP ; + +if (!PAGE_ALIGNED (offset) || !PAGE_ALIGNED (len)) +return -EINVAL ; + +if (mode &FALLOC_FL_PUNCH_HOLE ) + ret =kvm_gmem_punch_hole (file_inode (file), offset, len); +else + ret =kvm_gmem_allocate (file_inode (file), offset, len); + +if (!ret) +file_modified (file); +return ret; +} + +static int kvm_gmem_release (struct inode *inode,struct file *file) +{ +struct kvm_gmem *gmem = file->private_data; +struct kvm_memory_slot *slot; +struct kvm *kvm = gmem->kvm; +unsigned long index; + +filemap_invalidate_lock (inode->i_mapping); + +/* +* Prevent concurrent attempts to *unbind* a memslot. This is the last +* reference to the file and thus no new bindings can be created, but +* dereferencing the slot for existing bindings needs to be protected +* against memslot updates, specifically so that unbind doesn't race +* and free the memslot (kvm_gmem_get_file() will return NULL). +*/ +mutex_lock (&kvm->slots_lock); + +xa_for_each (&gmem->bindings, index, slot) +rcu_assign_pointer (slot->gmem.file,NULL ); + +synchronize_rcu (); + +/* +* All in-flight operations are gone and new bindings can be created. +* Zap all SPTEs pointed at by this file. Do not free the backing +* memory, as its lifetime is associated with the inode, not the file. +*/ +kvm_gmem_invalidate_begin (gmem,0 ,-1ul ); +kvm_gmem_invalidate_end (gmem,0 ,-1ul ); + +mutex_unlock (&kvm->slots_lock); + +list_del (&gmem->entry); + +filemap_invalidate_unlock (inode->i_mapping); + +xa_destroy (&gmem->bindings); +kfree (gmem); + +kvm_put_kvm (kvm); + +return 0 ; +} + +static struct file *kvm_gmem_get_file (struct kvm_memory_slot *slot) +{ +struct file *file; + +rcu_read_lock (); + + file =rcu_dereference (slot->gmem.file); +if (file && !get_file_rcu (file)) + file =NULL ; + +rcu_read_unlock (); + +return file; +} + +static const struct file_operations kvm_gmem_fops = { + .open = generic_file_open, + .release = kvm_gmem_release, + .fallocate = kvm_gmem_fallocate, +}; + +static int kvm_gmem_migrate_folio (struct address_space *mapping, +struct folio *dst,struct folio *src, +enum migrate_mode mode) +{ +WARN_ON_ONCE (1 ); +return -EINVAL ; +} + +static int kvm_gmem_error_page (struct address_space *mapping,struct page *page) +{ +struct list_head *gmem_list = &mapping->private_list; +struct kvm_memory_slot *slot; +struct kvm_gmem *gmem; +unsigned long index; +pgoff_t start, end;
virt/kvm/guest_mem.c line 284
Quoted 10 lines
virt/kvm/guest_mem.c new file +10
-0+gfn_t gfn; + +filemap_invalidate_lock_shared (mapping); + + start = page->index; + end = start +thp_nr_pages (page); + +list_for_each_entry (gmem, gmem_list, entry) { +xa_for_each_range (&gmem->bindings, index, slot, start, end -1 ) { +for (gfn = start; gfn < end; gfn++) {
Why the start end range used as gfn here ?
the page->index is offset of inode's page cache mapping and gmem address space, IIUC, gfn calculation should follow same way as kvm_gmem_invalidate_begin().
Quoted 508 lines
virt/kvm/guest_mem.c new file +307
-0+if (WARN_ON_ONCE (gfn < slot->base_gfn || + gfn >= slot->base_gfn + slot->npages)) +continue ; + +/* +* FIXME: Tell userspace that the *private* +* memory encountered an error. +*/ +send_sig_mceerr (BUS_MCEERR_AR , + (void __user *)gfn_to_hva_memslot (slot, gfn), +PAGE_SHIFT , current); + } + } + } + +filemap_invalidate_unlock_shared (mapping); + +return 0 ; +} + +static const struct address_space_operations kvm_gmem_aops = { + .dirty_folio = noop_dirty_folio, +#ifdef CONFIG_MIGRATION + .migrate_folio = kvm_gmem_migrate_folio, +#endif + .error_remove_page = kvm_gmem_error_page, +}; + +static int kvm_gmem_getattr (struct mnt_idmap *idmap, +const struct path *path,struct kstat *stat, +u32 request_mask,unsigned int query_flags) +{ +struct inode *inode = path->dentry->d_inode; + +/* TODO */ +generic_fillattr (idmap, inode, stat); +return 0 ; +} + +static int kvm_gmem_setattr (struct mnt_idmap *idmap,struct dentry *dentry, +struct iattr *attr) +{ +/* TODO */ +return -EINVAL ; +} +static const struct inode_operations kvm_gmem_iops = { + .getattr = kvm_gmem_getattr, + .setattr = kvm_gmem_setattr, +}; + +static int __kvm_gmem_create (struct kvm *kvm,loff_t size,struct vfsmount *mnt) +{ +const char *anon_name ="[kvm-gmem]" ; +const struct qstr qname =QSTR_INIT (anon_name,strlen (anon_name)); +struct kvm_gmem *gmem; +struct inode *inode; +struct file *file; +int fd, err; + + inode =alloc_anon_inode (mnt->mnt_sb); +if (IS_ERR (inode)) +return PTR_ERR (inode); + + err =security_inode_init_security_anon (inode, &qname,NULL ); +if (err) + goto err_inode; + + inode->i_private = (void *)(unsigned long )flags; + inode->i_op = &kvm_gmem_iops; + inode->i_mapping->a_ops = &kvm_gmem_aops; + inode->i_mode |=S_IFREG ; + inode->i_size = size; +mapping_set_gfp_mask (inode->i_mapping,GFP_HIGHUSER ); +mapping_set_unevictable (inode->i_mapping); +mapping_set_unmovable (inode->i_mapping); + + fd =get_unused_fd_flags (0 ); +if (fd <0 ) { + err = fd; + goto err_inode; + } + + file =alloc_file_pseudo (inode, mnt,"kvm-gmem" ,O_RDWR , &kvm_gmem_fops); +if (IS_ERR (file)) { + err =PTR_ERR (file); + goto err_fd; + } + + file->f_flags |=O_LARGEFILE ; + file->f_mapping = inode->i_mapping; + + gmem =kzalloc (sizeof (*gmem),GFP_KERNEL ); +if (!gmem) { + err = -ENOMEM ; + goto err_file; + } + +kvm_get_kvm (kvm); + gmem->kvm = kvm; +xa_init (&gmem->bindings); + + file->private_data = gmem; + +list_add (&gmem->entry, &inode->i_mapping->private_list); + +fd_install (fd, file); +return fd; + +err_file: +fput (file); +err_fd: +put_unused_fd (fd); +err_inode: +iput (inode); +return err; +} + +static bool kvm_gmem_is_valid_size (loff_t size,u64 flags) +{ +if (size <0 || !PAGE_ALIGNED (size)) +return false; + +return true; +} + +int kvm_gmem_create (struct kvm *kvm,struct kvm_create_guest_memfd *args) +{ +loff_t size = args->size; +u64 flags = args->flags; +u64 valid_flags =0 ; + +if (flags & ~valid_flags) +return -EINVAL ; + +if (!kvm_gmem_is_valid_size (size, flags)) +return -EINVAL ; + +return __kvm_gmem_create (kvm, size, flags, kvm_gmem_mnt); +} + +int kvm_gmem_bind (struct kvm *kvm,struct kvm_memory_slot *slot, +unsigned int fd,loff_t offset) +{ +loff_t size = slot->npages <<PAGE_SHIFT ; +unsigned long start, end, flags; +struct kvm_gmem *gmem; +struct inode *inode; +struct file *file; + +BUILD_BUG_ON (sizeof (gfn_t) !=sizeof (slot->gmem.pgoff)); + + file =fget (fd); +if (!file) +return -EINVAL ; + +if (file->f_op != &kvm_gmem_fops) + goto err; + + gmem = file->private_data; +if (gmem->kvm != kvm) + goto err; + + inode =file_inode (file); + flags = (unsigned long )inode->i_private; + +/* +* For simplicity, require the offset into the file and the size of the +* memslot to be aligned to the largest possible page size used to back +* the file (same as the size of the file itself). +*/ +if (!kvm_gmem_is_valid_size (offset, flags) || + !kvm_gmem_is_valid_size (size, flags)) + goto err; + +if (offset + size >i_size_read (inode)) + goto err; + +filemap_invalidate_lock (inode->i_mapping); + + start = offset >>PAGE_SHIFT ; + end = start + slot->npages; + +if (!xa_empty (&gmem->bindings) && +xa_find (&gmem->bindings, &start, end -1 ,XA_PRESENT )) { +filemap_invalidate_unlock (inode->i_mapping); + goto err; + } + +/* +* No synchronize_rcu() needed, any in-flight readers are guaranteed to +* be see either a NULL file or this new file, no need for them to go +* away. +*/ +rcu_assign_pointer (slot->gmem.file, file); + slot->gmem.pgoff = start; + +xa_store_range (&gmem->bindings, start, end -1 , slot,GFP_KERNEL ); +filemap_invalidate_unlock (inode->i_mapping); + +/* +* Drop the reference to the file, even on success. The file pins KVM, +* not the other way 'round. Active bindings are invalidated if the +* file is closed before memslots are destroyed. +*/ +fput (file); +return 0 ; + +err: +fput (file); +return -EINVAL ; +} + +void kvm_gmem_unbind (struct kvm_memory_slot *slot) +{ +unsigned long start = slot->gmem.pgoff; +unsigned long end = start + slot->npages; +struct kvm_gmem *gmem; +struct file *file; + +/* +* Nothing to do if the underlying file was already closed (or is being +* closed right now), kvm_gmem_release() invalidates all bindings. +*/ + file =kvm_gmem_get_file (slot); +if (!file) +return ; + + gmem = file->private_data; + +filemap_invalidate_lock (file->f_mapping); +xa_store_range (&gmem->bindings, start, end -1 ,NULL ,GFP_KERNEL ); +rcu_assign_pointer (slot->gmem.file,NULL ); +synchronize_rcu (); +filemap_invalidate_unlock (file->f_mapping); + +fput (file); +} + +int kvm_gmem_get_pfn (struct kvm *kvm,struct kvm_memory_slot *slot, +gfn_t gfn,kvm_pfn_t *pfn,int *max_order) +{ +pgoff_t index = gfn - slot->base_gfn + slot->gmem.pgoff; +struct kvm_gmem *gmem; +struct folio *folio; +struct page *page; +struct file *file; + + file =kvm_gmem_get_file (slot); +if (!file) +return -EFAULT ; + + gmem = file->private_data; + +if (WARN_ON_ONCE (xa_load (&gmem->bindings, index) != slot)) { +fput (file); +return -EIO ; + } + + folio =kvm_gmem_get_folio (file_inode (file), index); +if (!folio) { +fput (file); +return -ENOMEM ; + } + + page =folio_file_page (folio, index); + + *pfn =page_to_pfn (page); + *max_order =compound_order (compound_head (page)); + +folio_unlock (folio); +fput (file); + +return 0 ; +} +EXPORT_SYMBOL_GPL (kvm_gmem_get_pfn); + +static int kvm_gmem_init_fs_context (struct fs_context *fc) +{ +if (!init_pseudo (fc,GUEST_MEMORY_MAGIC )) +return -ENOMEM ; + +return 0 ; +} + +static struct file_system_type kvm_gmem_fs = { + .name ="kvm_guest_memory" , + .init_fs_context = kvm_gmem_init_fs_context, + .kill_sb = kill_anon_super, +}; + +int kvm_gmem_init (void ) +{ + kvm_gmem_mnt =kern_mount (&kvm_gmem_fs); +if (IS_ERR (kvm_gmem_mnt)) +return PTR_ERR (kvm_gmem_mnt); + +/* For giggles. Userspace can never map this anyways. */ + kvm_gmem_mnt->mnt_flags |=MNT_NOEXEC ; + +return 0 ; +} + +void kvm_gmem_exit (void ) +{ +kern_unmount (kvm_gmem_mnt); + kvm_gmem_mnt =NULL ; +}virt/kvm/kvm_main.c +54
-4@@ -761,7 +761,7 @@ void kvm_mmu_invalidate_range_add(struct kvm *kvm, gfn_t start, gfn_t end) } } -static bool kvm_mmu_unmap_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range) +bool kvm_mmu_unmap_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range) {kvm_mmu_invalidate_range_add (kvm, range->start, range->end);return kvm_unmap_gfn_range (kvm, range);@@ -992,6 +992,9 @@ static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)/* This does not remove the slot from struct kvm_memslots data structures */ static void kvm_free_memslot (struct kvm *kvm,struct kvm_memory_slot *slot) { +if (slot->flags &KVM_MEM_PRIVATE ) +kvm_gmem_unbind (slot); +kvm_destroy_dirty_bitmap (slot);kvm_arch_free_memslot (kvm, slot);@@ -1556,10 +1559,18 @@ static void kvm_replace_memslot(struct kvm *kvm, } } -static int check_memory_region_flags (const struct kvm_userspace_memory_region2 *mem) +static int check_memory_region_flags (struct kvm *kvm, +const struct kvm_userspace_memory_region2 *mem) {u32 valid_flags =KVM_MEM_LOG_DIRTY_PAGES ; +if (kvm_arch_has_private_mem (kvm)) + valid_flags |=KVM_MEM_PRIVATE ; + +/* Dirty logging private memory is not currently supported. */ +if (mem->flags &KVM_MEM_PRIVATE ) + valid_flags &= ~KVM_MEM_LOG_DIRTY_PAGES ; +#ifdef __KVM_HAVE_READONLY_MEM valid_flags |=KVM_MEM_READONLY ;#endif @@ -1968,7 +1979,7 @@ int __kvm_set_memory_region(struct kvm *kvm,int as_id, id;int r; - r =check_memory_region_flags (mem); + r =check_memory_region_flags (kvm, mem);if (r)return r;@@ -1987,6 +1998,10 @@ int __kvm_set_memory_region(struct kvm *kvm, !access_ok ((void __user *)(unsigned long )mem->userspace_addr, mem->memory_size)) return -EINVAL; +if (mem->flags &KVM_MEM_PRIVATE && + (mem->gmem_offset & (PAGE_SIZE -1 ) || + mem->gmem_offset + mem->memory_size < mem->gmem_offset)) +return -EINVAL ;if (as_id >=KVM_ADDRESS_SPACE_NUM || id >=KVM_MEM_SLOTS_NUM )return -EINVAL ;if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)@@ -2025,6 +2040,9 @@ int __kvm_set_memory_region(struct kvm *kvm,if ((kvm->nr_memslot_pages + npages) < kvm->nr_memslot_pages)return -EINVAL ; }else {/* Modify an existing slot. */ +/* Private memslots are immutable, they can only be deleted. */ +if (mem->flags &KVM_MEM_PRIVATE ) +return -EINVAL ;if ((mem->userspace_addr != old->userspace_addr) || (npages != old->npages) || ((mem->flags ^ old->flags) &KVM_MEM_READONLY ))@@ -2053,10 +2071,23 @@ int __kvm_set_memory_region(struct kvm *kvm, new->npages = npages; new->flags = mem->flags; new->userspace_addr = mem->userspace_addr; +if (mem->flags &KVM_MEM_PRIVATE ) { + r =kvm_gmem_bind (kvm, new, mem->gmem_fd, mem->gmem_offset); +if (r) + goto out; + } r =kvm_set_memslot (kvm, old, new, change);if (r) -kfree (new); + goto out_restricted; + +return 0 ; + +out_restricted: +if (mem->flags &KVM_MEM_PRIVATE ) +kvm_gmem_unbind (new); +out: +kfree (new);return r; }EXPORT_SYMBOL_GPL (__kvm_set_memory_region);@@ -2356,6 +2387,8 @@ static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm,#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES static u64 kvm_supported_mem_attributes (struct kvm *kvm) { +if (kvm_arch_has_private_mem (kvm)) +return KVM_MEMORY_ATTRIBUTE_PRIVATE ;return 0 ; }@@ -5134,6 +5167,16 @@ static long kvm_vm_ioctl(struct file *filp,case KVM_GET_STATS_FD : r =kvm_vm_ioctl_get_stats_fd (kvm);break ; +case KVM_CREATE_GUEST_MEMFD : { +struct kvm_create_guest_memfd guest_memfd; + + r = -EFAULT ; +if (copy_from_user (&guest_memfd, argp,sizeof (guest_memfd))) + goto out; + + r =kvm_gmem_create (kvm, &guest_memfd); +break ; + }default : r =kvm_arch_vm_ioctl (filp, ioctl, arg); }@@ -6255,12 +6298,17 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module)if (r) goto err_async_pf; + r =kvm_gmem_init (); +if (r) + goto err_gmem; + kvm_chardev_ops.owner = module; kvm_preempt_ops.sched_in = kvm_sched_in; kvm_preempt_ops.sched_out = kvm_sched_out;kvm_init_debug (); +kvm_gmem_init (); r =kvm_vfio_ops_init ();if (WARN_ON_ONCE (r))@@ -6281,6 +6329,8 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module) err_register:kvm_vfio_ops_exit (); err_vfio: +kvm_gmem_exit (); +err_gmem:kvm_async_pf_deinit (); err_async_pf:kvm_irqfd_exit ();virt/kvm/kvm_mm.h +38
-1@@ -37,4 +37,42 @@ static inline void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm, }#endif /* HAVE_KVM_PFNCACHE */ +#ifdef CONFIG_KVM_PRIVATE_MEM +int kvm_gmem_init (void ); +void kvm_gmem_exit (void ); +int kvm_gmem_create (struct kvm *kvm,struct kvm_create_guest_memfd *args); +int kvm_gmem_bind (struct kvm *kvm,struct kvm_memory_slot *slot, +unsigned int fd,loff_t offset); +void kvm_gmem_unbind (struct kvm_memory_slot *slot); +#else +static inline int kvm_gmem_init (void ) +{ +return 0 ; +} + +static inline void kvm_gmem_exit (void ) +{ + +} + +static inline int kvm_gmem_create (struct kvm *kvm, +struct kvm_create_guest_memfd *args) +{ +return -EOPNOTSUPP ; +} + +static inline int kvm_gmem_bind (struct kvm *kvm, +struct kvm_memory_slot *slot, +unsigned int fd,loff_t offset) +{ +WARN_ON_ONCE (1 ); +return -EIO ; +} + +static inline void kvm_gmem_unbind (struct kvm_memory_slot *slot) +{ +WARN_ON_ONCE (1 ); +} +#endif /* CONFIG_KVM_PRIVATE_MEM */ +#endif /* __KVM_MM_H__ */ --2.41.0.255.g8b1d071c50-goog
Re: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
Quoted 480 lines · Yuan Yao
Quoted 466 lines · Sean Christopherson
TODO
Cc Fuad Tabba <[email protected]>Cc Vishal Annapurve <[email protected]>Cc Ackerley Tng <[email protected]>Cc Jarkko Sakkinen <[email protected]>Cc Maciej Szmigiero <[email protected]>Cc Vlastimil Babka <[email protected]>Cc David Hildenbrand <[email protected]>Cc Quentin Perret <[email protected]>Cc Michael Roth <[email protected]>Cc Wang <[email protected]>Cc Liam Merwick <[email protected]>Cc Isaku Yamahata <[email protected]>Co-developed-by Kirill A. Shutemov <[email protected]>Signed-off-by Kirill A. Shutemov <[email protected]>Co-developed-by Yu Zhang <[email protected]>Signed-off-by Yu Zhang <[email protected]>Co-developed-by Chao Peng <[email protected]>Signed-off-by Chao Peng <[email protected]>Co-developed-by Ackerley Tng <[email protected]>Signed-off-by Ackerley Tng <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- include/linux/kvm_host.h | 48 +++ include/uapi/linux/kvm.h | 14 +- include/uapi/linux/magic.h | 1 + virt/kvm/Kconfig | 4 + virt/kvm/Makefile.kvm | 1 + virt/kvm/guest_mem.c | 591 +++++++++++++++++++++++++++++++++++++ virt/kvm/kvm_main.c | 58 +++- virt/kvm/kvm_mm.h | 38 +++ 8 files changed, 750 insertions(+), 5 deletions(-) create mode 100644 virt/kvm/guest_mem.cinclude/linux/kvm_host.h +48
-0@@ -592,8 +592,20 @@ struct kvm_memory_slot {u32 flags;short id;u16 as_id; + +#ifdef CONFIG_KVM_PRIVATE_MEM +struct { +struct file __rcu *file; +pgoff_t pgoff; + } gmem; +#endif }; +static inline bool kvm_slot_can_be_private (const struct kvm_memory_slot *slot) +{ +return slot && (slot->flags &KVM_MEM_PRIVATE ); +} +static inline bool kvm_slot_dirty_track_enabled (const struct kvm_memory_slot *slot) {return slot->flags &KVM_MEM_LOG_DIRTY_PAGES ;@@ -688,6 +700,17 @@ static inline int kvm_arch_vcpu_memslots_id(struct kvm_vcpu *vcpu) }#endif +/* +* Arch code must define kvm_arch_has_private_mem if support for private memory +* is enabled. +*/ +#if !defined(kvm_arch_has_private_mem) && !IS_ENABLED (CONFIG_KVM_PRIVATE_MEM ) +static inline bool kvm_arch_has_private_mem (struct kvm *kvm) +{ +return false; +} +#endif +struct kvm_memslots {u64 generation;atomic_long_t last_used_slot;@@ -1380,6 +1403,7 @@ void *kvm_mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc);void kvm_mmu_invalidate_begin (struct kvm *kvm);void kvm_mmu_invalidate_range_add (struct kvm *kvm,gfn_t start,gfn_t end);void kvm_mmu_invalidate_end (struct kvm *kvm); +bool kvm_mmu_unmap_gfn_range (struct kvm *kvm,struct kvm_gfn_range *range);long kvm_arch_dev_ioctl (struct file *filp,unsigned int ioctl,unsigned long arg);@@ -2313,6 +2337,30 @@ static inline unsigned long kvm_get_memory_attributes(struct kvm *kvm, gfn_t gfnbool kvm_arch_post_set_memory_attributes (struct kvm *kvm,struct kvm_gfn_range *range); + +static inline bool kvm_mem_is_private (struct kvm *kvm,gfn_t gfn) +{ +return IS_ENABLED (CONFIG_KVM_PRIVATE_MEM ) && +kvm_get_memory_attributes (kvm, gfn) &KVM_MEMORY_ATTRIBUTE_PRIVATE ; +} +#else +static inline bool kvm_mem_is_private (struct kvm *kvm,gfn_t gfn) +{ +return false; +}#endif /* CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES */ +#ifdef CONFIG_KVM_PRIVATE_MEM +int kvm_gmem_get_pfn (struct kvm *kvm,struct kvm_memory_slot *slot, +gfn_t gfn,kvm_pfn_t *pfn,int *max_order); +#else +static inline int kvm_gmem_get_pfn (struct kvm *kvm, +struct kvm_memory_slot *slot,gfn_t gfn, +kvm_pfn_t *pfn,int *max_order) +{ +KVM_BUG_ON (1 , kvm); +return -EIO ; +} +#endif /* CONFIG_KVM_PRIVATE_MEM */ +#endif include/uapi/linux/kvm.h +13
-1@@ -102,7 +102,10 @@ struct kvm_userspace_memory_region2 {__u64 guest_phys_addr;__u64 memory_size;__u64 userspace_addr; -__u64 pad[16 ]; +__u64 gmem_offset; +__u32 gmem_fd; +__u32 pad1; +__u64 pad2[14 ]; };/* @@ -112,6 +115,7 @@ struct kvm_userspace_memory_region2 {*/ #define KVM_MEM_LOG_DIRTY_PAGES (1UL << 0)#define KVM_MEM_READONLY (1UL << 1) +#define KVM_MEM_PRIVATE (1UL << 2)/* for KVM_IRQ_LINE */ struct kvm_irq_level {@@ -2284,4 +2288,12 @@ struct kvm_memory_attributes {#define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3) +#define KVM_CREATE_GUEST_MEMFD _IOWR(KVMIO, 0xd4, struct kvm_create_guest_memfd) + +struct kvm_create_guest_memfd { +__u64 size; +__u64 flags; +__u64 reserved[6 ]; +}; +#endif /* __LINUX_KVM_H */ include/uapi/linux/magic.h +1
-0@@ -101,5 +101,6 @@#define DMA_BUF_MAGIC 0x444d4142/* "DMAB" */ #define DEVMEM_MAGIC 0x454d444d/* "DMEM" */ #define SECRETMEM_MAGIC 0x5345434d/* "SECM" */ +#define GUEST_MEMORY_MAGIC 0x474d454d/* "GMEM" */ #endif /* __LINUX_MAGIC_H__ */ virt/kvm/Kconfig +4
-0@@ -103,3 +103,7 @@ config KVM_GENERIC_MMU_NOTIFIER config KVM_GENERIC_MEMORY_ATTRIBUTES select KVM_GENERIC_MMU_NOTIFIER bool + +config KVM_PRIVATE_MEM + select XARRAY_MULTI + boolvirt/kvm/Makefile.kvm +1
-0@@ -12,3 +12,4 @@ kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o kvm-$(CONFIG_HAVE_KVM_IRQ_ROUTING) += $(KVM)/irqchip.o kvm-$(CONFIG_HAVE_KVM_DIRTY_RING) += $(KVM)/dirty_ring.o kvm-$(CONFIG_HAVE_KVM_PFNCACHE) += $(KVM)/pfncache.o +kvm-$(CONFIG_KVM_PRIVATE_MEM) += $(KVM)/guest_mem.ovirt/kvm/guest_mem.c new file +274
-0@@ -0,0 +1,591 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <linux/backing-dev.h> +#include <linux/falloc.h> +#include <linux/kvm_host.h> +#include <linux/pagemap.h> +#include <linux/pseudo_fs.h> + +#include <uapi/linux/magic.h> + +#include "kvm_mm.h" + +static struct vfsmount *kvm_gmem_mnt; + +struct kvm_gmem { +struct kvm *kvm; +struct xarray bindings; +struct list_head entry; +}; + +static struct folio *kvm_gmem_get_folio (struct file *file,pgoff_t index) +{ +struct folio *folio; + +/* TODO: Support huge pages. */ + folio =filemap_grab_folio (file->f_mapping, index); +if (!folio) +return NULL ; + +/* +* Use the up-to-date flag to track whether or not the memory has been +* zeroed before being handed off to the guest. There is no backing +* storage for the memory, so the folio will remain up-to-date until +* it's removed. +* +* TODO: Skip clearing pages when trusted firmware will do it when +* assigning memory to the guest. +*/ +if (!folio_test_uptodate (folio)) { +unsigned long nr_pages =folio_nr_pages (folio); +unsigned long i; + +for (i =0 ; i < nr_pages; i++) +clear_highpage (folio_page (folio, i)); + +folio_mark_uptodate (folio); + } + +/* +* Ignore accessed, referenced, and dirty flags. The memory is +* unevictable and there is no storage to write back to. +*/ +return folio; +} + +static void kvm_gmem_invalidate_begin (struct kvm_gmem *gmem,pgoff_t start, +pgoff_t end) +{ +struct kvm_memory_slot *slot; +struct kvm *kvm = gmem->kvm; +unsigned long index; +bool flush = false; + +KVM_MMU_LOCK (kvm); + +kvm_mmu_invalidate_begin (kvm); + +xa_for_each_range (&gmem->bindings, index, slot, start, end -1 ) { +pgoff_t pgoff = slot->gmem.pgoff; + +struct kvm_gfn_range gfn_range = { + .start = slot->base_gfn +max (pgoff, start) - pgoff, + .end = slot->base_gfn +min (pgoff + slot->npages, end) - pgoff, + .slot = slot, + .may_block = true, + }; + + flush |=kvm_mmu_unmap_gfn_range (kvm, &gfn_range); + } + +if (flush) +kvm_flush_remote_tlbs (kvm); + +KVM_MMU_UNLOCK (kvm); +} + +static void kvm_gmem_invalidate_end (struct kvm_gmem *gmem,pgoff_t start, +pgoff_t end) +{ +struct kvm *kvm = gmem->kvm; + +KVM_MMU_LOCK (kvm); +if (xa_find (&gmem->bindings, &start, end -1 ,XA_PRESENT )) +kvm_mmu_invalidate_end (kvm); +KVM_MMU_UNLOCK (kvm); +} + +static long kvm_gmem_punch_hole (struct inode *inode,loff_t offset,loff_t len) +{ +struct list_head *gmem_list = &inode->i_mapping->private_list; +pgoff_t start = offset >>PAGE_SHIFT ; +pgoff_t end = (offset + len) >>PAGE_SHIFT ; +struct kvm_gmem *gmem; + +/* +* Bindings must stable across invalidation to ensure the start+end +* are balanced. +*/ +filemap_invalidate_lock (inode->i_mapping); + +list_for_each_entry (gmem, gmem_list, entry) +kvm_gmem_invalidate_begin (gmem, start, end); + +truncate_inode_pages_range (inode->i_mapping, offset, offset + len -1 ); + +list_for_each_entry (gmem, gmem_list, entry) +kvm_gmem_invalidate_end (gmem, start, end); + +filemap_invalidate_unlock (inode->i_mapping); + +return 0 ; +} + +static long kvm_gmem_allocate (struct inode *inode,loff_t offset,loff_t len) +{ +struct address_space *mapping = inode->i_mapping; +pgoff_t start, index, end; +int r; + +/* Dedicated guest is immutable by default. */ +if (offset + len >i_size_read (inode)) +return -EINVAL ; + +filemap_invalidate_lock_shared (mapping); + + start = offset >>PAGE_SHIFT ; + end = (offset + len) >>PAGE_SHIFT ; + + r =0 ; +for (index = start; index < end; ) { +struct folio *folio; + +if (signal_pending (current)) { + r = -EINTR ; +break ; + } + + folio =kvm_gmem_get_folio (inode, index); +if (!folio) { + r = -ENOMEM ; +break ; + } + + index =folio_next_index (folio); + +folio_unlock (folio); +folio_put (folio); + +/* 64-bit only, wrapping the index should be impossible. */ +if (WARN_ON_ONCE (!index)) +break ; + +cond_resched (); + } + +filemap_invalidate_unlock_shared (mapping); + +return r; +} + +static long kvm_gmem_fallocate (struct file *file,int mode,loff_t offset, +loff_t len) +{ +int ret; + +if (!(mode &FALLOC_FL_KEEP_SIZE )) +return -EOPNOTSUPP ; + +if (mode & ~(FALLOC_FL_KEEP_SIZE |FALLOC_FL_PUNCH_HOLE )) +return -EOPNOTSUPP ; + +if (!PAGE_ALIGNED (offset) || !PAGE_ALIGNED (len)) +return -EINVAL ; + +if (mode &FALLOC_FL_PUNCH_HOLE ) + ret =kvm_gmem_punch_hole (file_inode (file), offset, len); +else + ret =kvm_gmem_allocate (file_inode (file), offset, len); + +if (!ret) +file_modified (file); +return ret; +} + +static int kvm_gmem_release (struct inode *inode,struct file *file) +{ +struct kvm_gmem *gmem = file->private_data; +struct kvm_memory_slot *slot; +struct kvm *kvm = gmem->kvm; +unsigned long index; + +filemap_invalidate_lock (inode->i_mapping); + +/* +* Prevent concurrent attempts to *unbind* a memslot. This is the last +* reference to the file and thus no new bindings can be created, but +* dereferencing the slot for existing bindings needs to be protected +* against memslot updates, specifically so that unbind doesn't race +* and free the memslot (kvm_gmem_get_file() will return NULL). +*/ +mutex_lock (&kvm->slots_lock); + +xa_for_each (&gmem->bindings, index, slot) +rcu_assign_pointer (slot->gmem.file,NULL ); + +synchronize_rcu (); + +/* +* All in-flight operations are gone and new bindings can be created. +* Zap all SPTEs pointed at by this file. Do not free the backing +* memory, as its lifetime is associated with the inode, not the file. +*/ +kvm_gmem_invalidate_begin (gmem,0 ,-1ul ); +kvm_gmem_invalidate_end (gmem,0 ,-1ul ); + +mutex_unlock (&kvm->slots_lock); + +list_del (&gmem->entry); + +filemap_invalidate_unlock (inode->i_mapping); + +xa_destroy (&gmem->bindings); +kfree (gmem); + +kvm_put_kvm (kvm); + +return 0 ; +} + +static struct file *kvm_gmem_get_file (struct kvm_memory_slot *slot) +{ +struct file *file; + +rcu_read_lock (); + + file =rcu_dereference (slot->gmem.file); +if (file && !get_file_rcu (file)) + file =NULL ; + +rcu_read_unlock (); + +return file; +} + +static const struct file_operations kvm_gmem_fops = { + .open = generic_file_open, + .release = kvm_gmem_release, + .fallocate = kvm_gmem_fallocate, +}; + +static int kvm_gmem_migrate_folio (struct address_space *mapping, +struct folio *dst,struct folio *src, +enum migrate_mode mode) +{ +WARN_ON_ONCE (1 ); +return -EINVAL ; +} + +static int kvm_gmem_error_page (struct address_space *mapping,struct page *page) +{ +struct list_head *gmem_list = &mapping->private_list; +struct kvm_memory_slot *slot; +struct kvm_gmem *gmem; +unsigned long index; +pgoff_t start, end;virt/kvm/guest_mem.c line 284
Quoted 10 lines
virt/kvm/guest_mem.c new file +10
-0+gfn_t gfn; + +filemap_invalidate_lock_shared (mapping); + + start = page->index; + end = start +thp_nr_pages (page); + +list_for_each_entry (gmem, gmem_list, entry) { +xa_for_each_range (&gmem->bindings, index, slot, start, end -1 ) { +for (gfn = start; gfn < end; gfn++) {Why the start end range used as gfn here ?
Quoted 3 lines
the
page->indexis offset of inode's page cache mapping and gmem address space, IIUC, gfn calculation should follow same way askvm_gmem_invalidate_begin().
Also instead of sending signal multiple times, we can utilize lsb argument. Something like this?
virt/kvm/guest_mem.c +20 -5
@@ -349,20 +349,35 @@ static int kvm_gmem_error_page(struct address_space *mapping, struct page *page)struct kvm_gmem *gmem;unsigned long index;pgoff_t start, end; -gfn_t gfn; +unsigned int order; +int nr_pages; +gfn_t gfn, gfn_end;filemap_invalidate_lock_shared (mapping); start = page->index; end = start +thp_nr_pages (page); + nr_pages =thp_nr_pages (page); + order =thp_order (page);list_for_each_entry (gmem, gmem_list, entry) {xa_for_each_range (&gmem->bindings, index, slot, start, end -1 ) { -for (gfn = start; gfn < end; gfn++) { -if (WARN_ON_ONCE (gfn < slot->base_gfn || - gfn >= slot->base_gfn + slot->npages)) -continue ; + gfn = slot->base_gfn + page->index - slot->gmem.pgoff; +if (page->index + nr_pages <= slot->gmem.pgoff + slot->npages && + !(gfn & ~((1ULL << order) -1 ))) { +/* +* FIXME: Tell userspace that the *private* +* memory encountered an error. +*/ +send_sig_mceerr (BUS_MCEERR_AR , + (void __user *)gfn_to_hva_memslot (slot, gfn), + order, current); +break ; + } + + gfn_end =min (gfn + nr_pages, slot->base_gfn + slot->npages); +for (; gfn < gfn_end; gfn++) {/* * FIXME: Tell userspace that the *private* * memory encountered an error.
Signature · 1 line
Isaku Yamahata <[email protected]>
Sean Christopherson
maintainerRe: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
Quoted 13 lines · Isaku Yamahata
Quoted 11 lines · Yuan Yao
Quoted 11 lines
(quoted diff) +11
-0+static int kvm_gmem_error_page(struct address_space *mapping, struct page *page) +{ + struct list_head *gmem_list = &mapping->private_list; + struct kvm_memory_slot *slot; + struct kvm_gmem *gmem; + unsigned long index; + pgoff_t start, end; + gfn_t gfn; + + filemap_invalidate_lock_shared(mapping); +
Quoted 8 lines
Quoted 8 lines
Review
Quoted 6 lines
(quoted diff) +6
-0+ start = page->index; + end = start + thp_nr_pages(page); + + list_for_each_entry(gmem, gmem_list, entry) { + xa_for_each_range(&gmem->bindings, index, slot, start, end - 1) { + for (gfn = start; gfn < end; gfn++) {Why the start end range used as gfn here ?
Math is hard? I almost always mess up these types of things, and then catch my
bugs via tests. But I don't have tests for this particular flow... Which
reminds me, we need tests for this :-) Hopefully error injection provides most of what we need?
Quoted 5 lines
Quoted 3 lines
the
page->indexis offset of inode's page cache mapping and gmem address space, IIUC, gfn calculation should follow same way askvm_gmem_invalidate_begin().Also instead of sending signal multiple times, we can utilize lsb argument.
As Vishal pointed out, this code shouldn't be sending signals in the first place.
Re: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
In reply to Sean ChristophersonQuoted 5 lines · Sean Christopherson
virt/kvm/kvm_main.c +1
-0@@ -6255,12 +6298,17 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module)if (r) goto err_async_pf; + r =kvm_gmem_init ();
virt/kvm/kvm_main.c int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module) · line 6311
Quoted 10 lines
virt/kvm/kvm_main.c +4
-0+if (r) + goto err_gmem; + kvm_chardev_ops.owner = module; kvm_preempt_ops.sched_in = kvm_sched_in; kvm_preempt_ops.sched_out = kvm_sched_out;kvm_init_debug (); +kvm_gmem_init ();
why kvm_gmem_init() needs to be called again? by mistake?
Re: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
Quoted 16 lines · Xiaoyao Li
Review int kvm_init(unsigned vcpu_size, unsigned · line 6309
Quoted 13 lines · Sean Christopherson
(quoted diff) +5
-0@@ -6255,12 +6298,17 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module) ????? if (r) ????????? goto err_async_pf; +??? r = kvm_gmem_init(); +??? if (r) +??????? goto err_gmem; + ????? kvm_chardev_ops.owner = module; ????? kvm_preempt_ops.sched_in = kvm_sched_in; ????? kvm_preempt_ops.sched_out = kvm_sched_out; ????? kvm_init_debug(); +??? kvm_gmem_init();why
kvm_gmem_init()needs to be called again? by mistake?
I'm sure it's a mistake.
I'm testing the gmem QEMU with this series. SW_PROTECTED_VM gets stuck in a loop in early OVMF code due to two shared page of OVMF get zapped and re-mapped infinitely. Removing the second call of kvm_gmem_init() can solve the issue, though I'm not sure about the reason.
Sean Christopherson
maintainerRe: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
Quoted 18 lines · Xiaoyao Li
Quoted 16 lines · Xiaoyao Li
Review int kvm_init(unsigned vcpu_size, unsigned · line 6309
Quoted 13 lines · Sean Christopherson
(quoted diff) +5
-0@@ -6255,12 +6298,17 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module) ????? if (r) ????????? goto err_async_pf; +??? r = kvm_gmem_init(); +??? if (r) +??????? goto err_gmem; + ????? kvm_chardev_ops.owner = module; ????? kvm_preempt_ops.sched_in = kvm_sched_in; ????? kvm_preempt_ops.sched_out = kvm_sched_out; ????? kvm_init_debug(); +??? kvm_gmem_init();why
kvm_gmem_init()needs to be called again? by mistake?
Quoted 1 line
I'm sure it's a mistake.
Yeah, definitely a bug.
Quoted 4 lines
I'm testing the gmem QEMU with this series.
SW_PROTECTED_VMgets stuck in a loop in early OVMF code due to two shared page of OVMF get zapped and re-mapped infinitely. Removing the second call ofkvm_gmem_init()can solve the issue, though I'm not sure about the reason.
Not worth investigating unless you want to satiate your curiosity :-)
Paolo Bonzini
maintainerRe: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
In reply to Sean Christophersonvirt/kvm/guest_mem.c line 351
Quoted 8 lines · Sean Christopherson
virt/kvm/guest_mem.c new file +8
-0+ inode =alloc_anon_inode (mnt->mnt_sb); +if (IS_ERR (inode)) +return PTR_ERR (inode); + + err =security_inode_init_security_anon (inode, &qname,NULL ); +if (err) + goto err_inode; +
I don't understand the need to have a separate filesystem. If it is to fully setup the inode before it's given a struct file, why not just export anon_inode_make_secure_inode instead of security_inode_init_security_anon?
Paolo
Sean Christopherson
maintainerRe: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
Quoted 13 lines · Paolo Bonzini
virt/kvm/guest_mem.c line 351
Quoted 8 lines · Sean Christopherson
virt/kvm/guest_mem.c new file +8
-0+ inode =alloc_anon_inode (mnt->mnt_sb); +if (IS_ERR (inode)) +return PTR_ERR (inode); + + err =security_inode_init_security_anon (inode, &qname,NULL ); +if (err) + goto err_inode; +I don't understand the need to have a separate filesystem. If it is to fully setup the inode before it's given a struct file, why not just export
anon_inode_make_secure_inodeinstead ofsecurity_inode_init_security_anon?
Ugh, this is why comments are important, I can't remember either.
I suspect I implemented a dedicated filesystem to kinda sorta show that we could allow userspace to provide the mount point with e.g. NUMA hints[*]. But my preference would be to not support a userspace provided mount and instead implement fbind() to let userspace control NUMA and whatnot.
[*] https://lore.kernel.org/all/ef48935e5e6f947f6f0c6d748232b14ef5d5ad70.1681176340.git.ackerleytng at google.com
RE: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
In reply to Sean ChristophersonQuoted 19 lines · Sean Christopherson
virt/kvm/guest_mem.c new file +19
-0+int kvm_gmem_get_pfn (struct kvm *kvm,struct kvm_memory_slot *slot, +gfn_t gfn,kvm_pfn_t *pfn,int *max_order) { +pgoff_t index = gfn - slot->base_gfn + slot->gmem.pgoff; +struct kvm_gmem *gmem; +struct folio *folio; +struct page *page; +struct file *file; + + file =kvm_gmem_get_file (slot); +if (!file) +return -EFAULT ; + + gmem = file->private_data; + +if (WARN_ON_ONCE (xa_load (&gmem->bindings, index) != slot)) { +fput (file); +return -EIO ; + } +
virt/kvm/guest_mem.c line 552
Quoted 10 lines
virt/kvm/guest_mem.c new file +10
-0+ folio =kvm_gmem_get_folio (file_inode (file), index); +if (!folio) { +fput (file); +return -ENOMEM ; + } + + page =folio_file_page (folio, index); + + *pfn =page_to_pfn (page); + *max_order =compound_order (compound_head (page));
Maybe better to check if caller provided a buffer to get the max_order:
if (max_order)
*max_order = compound_order(compound_head(page));
This is what the previous version did (restrictedmem_get_page), so that callers who only want to get a pfn don't need to define an unused "order" param.
Sean Christopherson
maintainerRe: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
Quoted 33 lines · Wei W Wang
Quoted 19 lines · Sean Christopherson
virt/kvm/guest_mem.c new file +19
-0+int kvm_gmem_get_pfn (struct kvm *kvm,struct kvm_memory_slot *slot, +gfn_t gfn,kvm_pfn_t *pfn,int *max_order) { +pgoff_t index = gfn - slot->base_gfn + slot->gmem.pgoff; +struct kvm_gmem *gmem; +struct folio *folio; +struct page *page; +struct file *file; + + file =kvm_gmem_get_file (slot); +if (!file) +return -EFAULT ; + + gmem = file->private_data; + +if (WARN_ON_ONCE (xa_load (&gmem->bindings, index) != slot)) { +fput (file); +return -EIO ; + } +virt/kvm/guest_mem.c line 552
Quoted 10 lines
virt/kvm/guest_mem.c new file +10
-0+ folio =kvm_gmem_get_folio (file_inode (file), index); +if (!folio) { +fput (file); +return -ENOMEM ; + } + + page =folio_file_page (folio, index); + + *pfn =page_to_pfn (page); + *max_order =compound_order (compound_head (page));Maybe better to check if caller provided a buffer to get the
max_order:
if (max_order)
Quoted 5 lines
*max_order = compound_order(compound_head(page));This is what the previous version did (
restrictedmem_get_page), so that callers who only want to get a pfn don't need to define an unused "order" param.
My preference would be to require @max_order. I can kinda sorta see why a generic implementation (restrictedmem) would make the param optional, but with gmem being KVM-internal I think it makes sense to require the param. Even if pKVM doesn't _currently_ need/want the order of the backing allocation, presumably that's because hugepage support is still on the TODO list, not because pKVM fundamentally doesn't need to know the order of the backing allocation.
RE: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
Quoted 40 lines · Sean Christopherson
Quoted 33 lines · Wei W Wang
Quoted 19 lines · Sean Christopherson
virt/kvm/guest_mem.c new file +19
-0+int kvm_gmem_get_pfn (struct kvm *kvm,struct kvm_memory_slot *slot, +gfn_t gfn,kvm_pfn_t *pfn,int *max_order) { +pgoff_t index = gfn - slot->base_gfn + slot->gmem.pgoff; +struct kvm_gmem *gmem; +struct folio *folio; +struct page *page; +struct file *file; + + file =kvm_gmem_get_file (slot); +if (!file) +return -EFAULT ; + + gmem = file->private_data; + +if (WARN_ON_ONCE (xa_load (&gmem->bindings, index) != slot)) { +fput (file); +return -EIO ; + } +virt/kvm/guest_mem.c line 552
Quoted 10 lines
virt/kvm/guest_mem.c new file +10
-0+ folio =kvm_gmem_get_folio (file_inode (file), index); +if (!folio) { +fput (file); +return -ENOMEM ; + } + + page =folio_file_page (folio, index); + + *pfn =page_to_pfn (page); + *max_order =compound_order (compound_head (page));Maybe better to check if caller provided a buffer to get the
max_order:
if (max_order)Quoted 5 lines
*max_order = compound_order(compound_head(page));This is what the previous version did (
restrictedmem_get_page), so that callers who only want to get a pfn don't need to define an unused "order" param.
Quoted 7 lines
My preference would be to require @max_order. I can kinda sorta see why a generic implementation (restrictedmem) would make the param optional, but with gmem being KVM-internal I think it makes sense to require the param. Even if pKVM doesn't _currently_ need/want the order of the backing allocation, presumably that's because hugepage support is still on the TODO list, not because pKVM fundamentally doesn't need to know the order of the backing allocation.
Another usage is live migration. The migration flow works with 4KB pages only, and we only need to get the pfn from the given gfn. "order" doesn't seem to be useful for this case.
Re: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
In reply to Sean ChristophersonHi Sean,
Quoted 41 lines · On Tue, Jul 25, 2023 at 5:04?PM Sean Christopherson <[email protected]> wrote:
Quoted 33 lines · Wei W Wang
Quoted 19 lines · Sean Christopherson
virt/kvm/guest_mem.c new file +19
-0+int kvm_gmem_get_pfn (struct kvm *kvm,struct kvm_memory_slot *slot, +gfn_t gfn,kvm_pfn_t *pfn,int *max_order) { +pgoff_t index = gfn - slot->base_gfn + slot->gmem.pgoff; +struct kvm_gmem *gmem; +struct folio *folio; +struct page *page; +struct file *file; + + file =kvm_gmem_get_file (slot); +if (!file) +return -EFAULT ; + + gmem = file->private_data; + +if (WARN_ON_ONCE (xa_load (&gmem->bindings, index) != slot)) { +fput (file); +return -EIO ; + } +virt/kvm/guest_mem.c line 552
Quoted 10 lines
virt/kvm/guest_mem.c new file +10
-0+ folio =kvm_gmem_get_folio (file_inode (file), index); +if (!folio) { +fput (file); +return -ENOMEM ; + } + + page =folio_file_page (folio, index); + + *pfn =page_to_pfn (page); + *max_order =compound_order (compound_head (page));Maybe better to check if caller provided a buffer to get the
max_order:
if (max_order)Quoted 5 lines
*max_order = compound_order(compound_head(page));This is what the previous version did (
restrictedmem_get_page), so that callers who only want to get a pfn don't need to define an unused "order" param.
Quoted 6 lines
My preference would be to require @max_order. I can kinda sorta see why a generic implementation (restrictedmem) would make the param optional, but with gmem being KVM-internal I think it makes sense to require the param. Even if pKVM doesn't _currently_ need/want the order of the backing allocation, presumably that's because hugepage support is still on the TODO list, not because pKVM fundamentally doesn't need to know the order of the backing allocation.
You're right that with huge pages pKVM will eventually need to know the order of the backing allocation, but there is at least one use case where it doesn't, which I ran into in the previous ports as well as this one. In pKVM (and in possibly other implementations), the host needs to access (shared) guest memory that isn't mapped. For that, I've used kvm_*_get_pfn(), only requiring the pfn, so get the page via pfn_to_page().
Although it's not that big, my preference would be for max_order to be optional.
Thanks!
/fuad
Re: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
In reply to Sean ChristophersonQuoted 1 line · Sean Christopherson
TODO
<snip>
include/uapi/linux/magic.h line 104
Quoted 9 lines
include/uapi/linux/magic.h +1
-0@@ -101,5 +101,6 @@#define DMA_BUF_MAGIC 0x444d4142/* "DMAB" */ #define DEVMEM_MAGIC 0x454d444d/* "DMEM" */ #define SECRETMEM_MAGIC 0x5345434d/* "SECM" */ +#define GUEST_MEMORY_MAGIC 0x474d454d/* "GMEM" */
Should this be:
#define GUEST_MEMORY_KVM_MAGIC
or KVM_GUEST_MEMORY_KVM_MAGIC?
BALLOON_KVM_MAGIC is KVM-specific few lines above.
---
Originally, I was planning to use the generic guest memfd infrastructure to support Gunyah hypervisor, however I see that's probably not going to be possible now that the guest memfd implementation is KVM-specific. I think this is good for both KVM and Gunyah as there will be some Gunyah specifics and some KVM specifics in each of implementation, as you mentioned in the previous series.
I'll go through series over next week or so and I'll try to find how much similar Gunyah guest mem fd implementation would be and we can see if it's better to pull whatever that ends up being into a common implementation? We could also agree to have completely divergent fd implementations like we do for the UAPI. Thoughts?
Thanks,
Elliot
<snip>
Sean Christopherson
maintainerRe: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
Quoted 18 lines · Elliot Berman
Quoted 1 line · Sean Christopherson
TODO
<snip>
include/uapi/linux/magic.h line 104
Quoted 9 lines
include/uapi/linux/magic.h +1
-0@@ -101,5 +101,6 @@#define DMA_BUF_MAGIC 0x444d4142/* "DMAB" */ #define DEVMEM_MAGIC 0x454d444d/* "DMEM" */ #define SECRETMEM_MAGIC 0x5345434d/* "SECM" */ +#define GUEST_MEMORY_MAGIC 0x474d454d/* "GMEM" */ Should this be:
Quoted 5 lines
#define
GUEST_MEMORY_KVM_MAGICor
KVM_GUEST_MEMORY_KVM_MAGIC?
BALLOON_KVM_MAGICis KVM-specific few lines above.
Ah, good point. My preference would be either KVM_GUEST_MEMORY_MAGIC or KVM_GUEST_MEMFD_MAGIC. Though hopefully we don't actually need a dedicated filesystem, I _think_ it's unnecessary if we don't try to support userspace mounts.
Quoted 8 lines
---
Originally, I was planning to use the generic guest memfd infrastructure to support Gunyah hypervisor, however I see that's probably not going to be possible now that the guest memfd implementation is KVM-specific. I think this is good for both KVM and Gunyah as there will be some Gunyah specifics and some KVM specifics in each of implementation, as you mentioned in the previous series.
Yeah, that's where my headspace is at too. Sharing the actual uAPI, and even internal APIs to some extent, doesn't save all that much, e.g. wiring up an ioctl() is the easy part. Whereas I strongly suspect each hypervisor use case will want different semantics for the uAPI.
Quoted 3 lines
I'll go through series over next week or so and I'll try to find how much similar Gunyah guest mem fd implementation would be and we can see if it's better to pull whatever that ends up being into a common implementation?
That would be awesome!
Quoted 2 lines
We could also agree to have completely divergent fd implementations like we do for the UAPI. Thoughts?
I'd like to avoid _completely_ divergent implementations, e.g. the majority of kvm_gmem_allocate() and __kvm_gmem_create() isn't KVM specific. I think there would be value in sharing the core allocation logic, even if the other details are different. Especially if we fully commit to not supporting migration or swap, and decide to use xarray directly to manage folios instead of bouncing through the filemap APIs.
Thanks!
Re: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
In reply to Sean ChristophersonHi Sean,
<snip>
...
Quoted 4 lines
virt/kvm/kvm_main.c +0
-0@@ -5134,6 +5167,16 @@ static long kvm_vm_ioctl(struct file *filp,case KVM_GET_STATS_FD : r =kvm_vm_ioctl_get_stats_fd (kvm);break ;
virt/kvm/kvm_main.c static long kvm_vm_ioctl(struct file *filp, · line 5179
Quoted 10 lines
virt/kvm/kvm_main.c +10
-0+case KVM_CREATE_GUEST_MEMFD : { +struct kvm_create_guest_memfd guest_memfd; + + r = -EFAULT ; +if (copy_from_user (&guest_memfd, argp,sizeof (guest_memfd))) + goto out; + + r =kvm_gmem_create (kvm, &guest_memfd); +break ; + }
I'm thinking line of sight here, by having this as a vm ioctl (rather than a system iocl), would it complicate making it possible in the future to share/donate memory between VMs?
Cheers,
/fuad
Sean Christopherson
maintainerRe: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
Quoted 15 lines · Fuad Tabba
Hi Sean,
<snip>
...Quoted 10 lines
virt/kvm/kvm_main.c +6
-0@@ -5134,6 +5167,16 @@ static long kvm_vm_ioctl(struct file *filp,case KVM_GET_STATS_FD : r =kvm_vm_ioctl_get_stats_fd (kvm);break ; +case KVM_CREATE_GUEST_MEMFD : { +struct kvm_create_guest_memfd guest_memfd; + + r = -EFAULT ; +if (copy_from_user (&guest_memfd, argp,sizeof (guest_memfd))) + goto out;
Quoted 8 lines
virt/kvm/kvm_main.c static long kvm_vm_ioctl(struct file *filp, · line 5179
Quoted 4 lines
virt/kvm/kvm_main.c +4
-0+ + r =kvm_gmem_create (kvm, &guest_memfd); +break ; + }I'm thinking line of sight here, by having this as a vm ioctl (rather than a system iocl), would it complicate making it possible in the future to share/donate memory between VMs?
Maybe, but I hope not?
There would still be a primary owner of the memory, i.e. the memory would still need to be allocated in the context of a specific VM. And the primary owner should be able to restrict privileges, e.g. allow a different VM to read but not write memory.
My current thinking is to (a) tie the lifetime of the backing pages to the inode, i.e. allow allocations to outlive the original VM, and (b) create a new file each time memory is shared/donated with a different VM (or other entity in the kernel).
That should make it fairly straightforward to provide different permissions, e.g. track them per-file, and I think should also avoid the need to change the memslot binding logic since each VM would have it's own view/bindings.
Copy+pasting a relevant snippet from a lengthier response in a different thread[*]:
Conceptually, I think KVM should to bind to the file. The inode is effectively the raw underlying physical storage, while the file is the VM's view of that storage.
Practically, I think that gives us a clean, intuitive way to handle intra-host migration. Rather than transfer ownership of the file, instantiate a new file for the target VM, using the gmem inode from the source VM, i.e. create a hard link. That'd probably require new uAPI, but I don't think that will be hugely problematic. KVM would need to ensure the new VM's guest_memfd can't be mapped until KVM_CAP_VM_MOVE_ENC_CONTEXT_FROM (which would also need to verify the memslots/bindings are identical), but that should be easy enough to enforce.
That way, a VM, its memslots, and its SPTEs are tied to the file, while allowing the memory and the *contents* of memory to outlive the VM, i.e. be effectively transfered to the new target VM. And we'll maintain the invariant that each guest_memfd is bound 1:1 with a single VM.
As above, that should also help us draw the line between mapping memory into a VM (file), and freeing/reclaiming the memory (inode).
There will be extra complexity/overhead as we'll have to play nice with the possibility of multiple files per inode, e.g. to zap mappings across all files when punching a hole, but the extra complexity is quite small, e.g. we can use address_space.private_list to keep track of the guest_memfd instances associated with the inode.
Setting aside TDX and SNP for the moment, as it's not clear how they'll support memory that is "private" but shared between multiple VMs, I think per-VM files would work well for sharing gmem between two VMs. E.g. would allow a give page to be bound to a different gfn for each VM, would allow having different permissions for each file (e.g. to allow fallocate() only from the original owner).
[*] https://lore.kernel.org/all/ZLGiEfJZTyl7M8mS at google.com
Re: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
Hi Sean,
Quoted 41 lines · On Thu, Jul 27, 2023 at 6:13?PM Sean Christopherson <[email protected]> wrote:
Quoted 15 lines · Fuad Tabba
Hi Sean,
<snip>
...Quoted 10 lines
virt/kvm/kvm_main.c +6
-0@@ -5134,6 +5167,16 @@ static long kvm_vm_ioctl(struct file *filp,case KVM_GET_STATS_FD : r =kvm_vm_ioctl_get_stats_fd (kvm);break ; +case KVM_CREATE_GUEST_MEMFD : { +struct kvm_create_guest_memfd guest_memfd; + + r = -EFAULT ; +if (copy_from_user (&guest_memfd, argp,sizeof (guest_memfd))) + goto out;Quoted 8 lines
virt/kvm/kvm_main.c static long kvm_vm_ioctl(struct file *filp, · line 5179
Quoted 4 lines
virt/kvm/kvm_main.c +4
-0+ + r =kvm_gmem_create (kvm, &guest_memfd); +break ; + }I'm thinking line of sight here, by having this as a vm ioctl (rather than a system iocl), would it complicate making it possible in the future to share/donate memory between VMs?
Maybe, but I hope not?
There would still be a primary owner of the memory, i.e. the memory would still need to be allocated in the context of a specific VM. And the primary owner should be able to restrict privileges, e.g. allow a different VM to read but not write memory.
My current thinking is to (a) tie the lifetime of the backing pages to the inode, i.e. allow allocations to outlive the original VM, and (b) create a new file each time memory is shared/donated with a different VM (or other entity in the kernel).
That should make it fairly straightforward to provide different permissions, e.g. track them per-file, and I think should also avoid the need to change the memslot binding logic since each VM would have it's own view/bindings.
Quoted 5 lines
Copy+pasting a relevant snippet from a lengthier response in a different thread[*]:
Conceptually, I think KVM should to bind to the file. The inode is effectively the raw underlying physical storage, while the file is the VM's view of that storage.
I'm not aware of any implementation of sharing memory between VMs in KVM before (afaik, since there was no need for one). The following is me thinking out loud, rather than any strong opinions on my part.
If an allocation can outlive the original VM, then why associate it with that (or a) VM to begin with? Wouldn't it be more flexible if it were a system-level construct, which is effectively what it was in previous iterations of this? This doesn't rule out binding to the file, and keeping the inode as the underlying physical storage.
The binding of a VM to a guestmem object could happen implicitly with KVM_SET_USER_MEMORY_REGION2, or we could have a new ioctl specifically for handling binding.
Cheers,
/fuad
Quoted 30 lines
Practically, I think that gives us a clean, intuitive way to handle intra-host migration. Rather than transfer ownership of the file, instantiate a new file for the target VM, using the gmem inode from the source VM, i.e. create a hard link. That'd probably require new uAPI, but I don't think that will be hugely problematic. KVM would need to ensure the new VM's
guest_memfdcan't be mapped untilKVM_CAP_VM_MOVE_ENC_CONTEXT_FROM(which would also need to verify the memslots/bindings are identical), but that should be easy enough to enforce.That way, a VM, its memslots, and its SPTEs are tied to the file, while allowing the memory and the *contents* of memory to outlive the VM, i.e. be effectively transfered to the new target VM. And we'll maintain the invariant that each
guest_memfdis bound 1:1 with a single VM.As above, that should also help us draw the line between mapping memory into a VM (file), and freeing/reclaiming the memory (inode).
There will be extra complexity/overhead as we'll have to play nice with the possibility of multiple files per inode, e.g. to zap mappings across all files when punching a hole, but the extra complexity is quite small, e.g. we can use address_space.private_list to keep track of the
guest_memfdinstances associated with the inode.Setting aside TDX and SNP for the moment, as it's not clear how they'll support memory that is "private" but shared between multiple VMs, I think per-VM files would work well for sharing gmem between two VMs. E.g. would allow a give page to be bound to a different gfn for each VM, would allow having different permissions for each file (e.g. to allow
fallocate()only from the original owner).[*] https://lore.kernel.org/all/ZLGiEfJZTyl7M8mS at google.com
Re: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
In reply to Sean Christophersonvirt/kvm/guest_mem.c line 27
Quoted 8 lines
virt/kvm/guest_mem.c new file +8
-0+static struct folio *kvm_gmem_get_folio (struct file *file,pgoff_t index) +{ +struct folio *folio; + +/* TODO: Support huge pages. */ + folio =filemap_grab_folio (file->f_mapping, index); +if (!folio) +return NULL ;
In Linux 6.4, filemap_grab_folio() may also return an error value. Instead of just checking for NULL, "IS_ERR_OR_NULL(folio)" will be needed.
Re: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
In reply to Sean ChristophersonQuoted 113 lines · Sean Christopherson
<snip>
virt/kvm/guest_mem.c new file +108
-0+static int kvm_gmem_release (struct inode *inode,struct file *file) +{ +struct kvm_gmem *gmem = file->private_data; +struct kvm_memory_slot *slot; +struct kvm *kvm = gmem->kvm; +unsigned long index; + +filemap_invalidate_lock (inode->i_mapping); + +/* +* Prevent concurrent attempts to *unbind* a memslot. This is the last +* reference to the file and thus no new bindings can be created, but +* dereferencing the slot for existing bindings needs to be protected +* against memslot updates, specifically so that unbind doesn't race +* and free the memslot (kvm_gmem_get_file() will return NULL). +*/ +mutex_lock (&kvm->slots_lock); + +xa_for_each (&gmem->bindings, index, slot) +rcu_assign_pointer (slot->gmem.file,NULL ); + +synchronize_rcu (); + +/* +* All in-flight operations are gone and new bindings can be created. +* Zap all SPTEs pointed at by this file. Do not free the backing +* memory, as its lifetime is associated with the inode, not the file. +*/ +kvm_gmem_invalidate_begin (gmem,0 ,-1ul ); +kvm_gmem_invalidate_end (gmem,0 ,-1ul ); + +mutex_unlock (&kvm->slots_lock); + +list_del (&gmem->entry); + +filemap_invalidate_unlock (inode->i_mapping); + +xa_destroy (&gmem->bindings); +kfree (gmem); + +kvm_put_kvm (kvm); + +return 0 ; +} + + +int kvm_gmem_bind (struct kvm *kvm,struct kvm_memory_slot *slot, +unsigned int fd,loff_t offset) +{ +loff_t size = slot->npages <<PAGE_SHIFT ; +unsigned long start, end, flags; +struct kvm_gmem *gmem; +struct inode *inode; +struct file *file; + +BUILD_BUG_ON (sizeof (gfn_t) !=sizeof (slot->gmem.pgoff)); + + file =fget (fd); +if (!file) +return -EINVAL ; + +if (file->f_op != &kvm_gmem_fops) + goto err; + + gmem = file->private_data; +if (gmem->kvm != kvm) + goto err; + + inode =file_inode (file); + flags = (unsigned long )inode->i_private; + +/* +* For simplicity, require the offset into the file and the size of the +* memslot to be aligned to the largest possible page size used to back +* the file (same as the size of the file itself). +*/ +if (!kvm_gmem_is_valid_size (offset, flags) || + !kvm_gmem_is_valid_size (size, flags)) + goto err; + +if (offset + size >i_size_read (inode)) + goto err; + +filemap_invalidate_lock (inode->i_mapping); + + start = offset >>PAGE_SHIFT ; + end = start + slot->npages; + +if (!xa_empty (&gmem->bindings) && +xa_find (&gmem->bindings, &start, end -1 ,XA_PRESENT )) { +filemap_invalidate_unlock (inode->i_mapping); + goto err; + } + +/* +* No synchronize_rcu() needed, any in-flight readers are guaranteed to +* be see either a NULL file or this new file, no need for them to go +* away. +*/ +rcu_assign_pointer (slot->gmem.file, file); + slot->gmem.pgoff = start; + +xa_store_range (&gmem->bindings, start, end -1 , slot,GFP_KERNEL ); +filemap_invalidate_unlock (inode->i_mapping); + +/* +* Drop the reference to the file, even on success. The file pins KVM, +* not the other way 'round. Active bindings are invalidated if the
virt/kvm/guest_mem.c line 496
Quoted 10 lines
virt/kvm/guest_mem.c new file +10
-0+* file is closed before memslots are destroyed. +*/ +fput (file); +return 0 ; + +err: +fput (file); +return -EINVAL ; +} +
I?d like to propose an alternative to the refcounting approach between the gmem file and associated kvm, where we think of KVM?s memslots as users of the gmem file.
Instead of having the gmem file pin the VM (i.e. take a refcount on kvm), we could let memslot take a refcount on the gmem file when the memslots are configured.
Here?s a POC patch that flips the refcounting (and modified selftests in the next commit):
https://github.com/googleprodkernel/linux-cc/commit/7f487b029b89b9f3e9b094a721bc0772f3c8c797
One side effect of having the gmem file pin the VM is that now the gmem file becomes sort of a false handle on the VM:
+ Closing the file destroys the file pointers in the VM and invalidates the pointers
+ Keeping the file open keeps the VM around in the kernel even though the VM fd may already be closed.
I feel that memslots form a natural way of managing usage of the gmem file. When a memslot is created, it is using the file; hence we take a refcount on the gmem file, and as memslots are removed, we drop refcounts on the gmem file.
The KVM pointer is shared among all the bindings in gmem?s xarray, and we can enforce that a gmem file is used only with one VM:
+ When binding a memslot to the file, if a kvm pointer exists, it must be the same kvm as the one in this binding
+ When the binding to the last memslot is removed from a file, NULL the kvm pointer.
When the VM is freed, KVM will iterate over all the memslots, removing them one at a time and eventually NULLing the kvm pointer.
I believe the ?KVM?s memslots using the file? approach is also simpler because all accesses to the bindings xarray and kvm pointer can be serialized using filemap_invalidate_lock(), and we are already using this lock regardless of refcounting approach. This serialization means we don?t need to use RCU on file/kvm pointers since accesses are already serialized.
There?s also no need to specially clean up the associated KVM when the file reference close, because by the time the .release() handler is called, any file references held by memslots would have been dropped, and so the bindings would have been removed, and the kvm pointer would have been NULLed out.
The corollary to this approach is that at creation time, the file won?t be associated with any kvm, and we can use a system ioctl instead of a VM-specific ioctl as Fuad brought up [1] (Association with kvm before the file is used with memslots is possible would mean more tracking so that kvm can close associated files when it is closed.)
One reason for binding gmem files to a specific VM on creation is to allow (in future) a primary VM to control permissions on the memory for other files [2]. This permission control can still be enforced with the ?KVM?s memslots using the file? approach. The enforcement rules will just be delayed till the first binding between a VM and a gmem file.
Could binding gmem files not on creation, but at memslot configuration time be sufficient and simpler?
[1] https://lore.kernel.org/lkml/CA+EHjTzP2fypgkJbRpSPrKaWytW7v8ANEifofMnQCkdvYaX6Eg at mail.gmail.com/
[2] https://lore.kernel.org/lkml/ZMKlo+Fe8n%2FeLQ82 at google.com/
Quoted 1 line
<snip>
Sean Christopherson
maintainerRe: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
Quoted 12 lines · Ackerley Tng
I?d like to propose an alternative to the refcounting approach between the gmem file and associated kvm, where we think of KVM?s memslots as users of the gmem file.
Instead of having the gmem file pin the VM (i.e. take a refcount on kvm), we could let memslot take a refcount on the gmem file when the memslots are configured.
Here?s a POC patch that flips the refcounting (and modified selftests in the next commit):
https://github.com/googleprodkernel/linux-cc/commit/7f487b029b89b9f3e9b094a721bc0772f3c8c797
Quoted 5 lines
One side effect of having the gmem file pin the VM is that now the gmem file becomes sort of a false handle on the VM:
+ Closing the file destroys the file pointers in the VM and invalidates the pointers
Yeah, this is less than ideal. But, it's also how things operate today. KVM doesn't hold references to VMAs or files, e.g. if userspace munmap()s memory, any and all SPTEs pointing at the memory are zapped. The only difference with gmem is that KVM needs to explicitly invalidate file pointers, instead of that happening behind the scenes (no more VMAs to find). Again, I agree the resulting code is more complex than I would prefer, but from a userspace perspective I don't see this as problematic.
Quoted 2 lines
+ Keeping the file open keeps the VM around in the kernel even though the VM fd may already be closed.
That is perfectly ok. There is plenty of prior art, as well as plenty of ways for userspace to shoot itself in the foot. E.g. open a stats fd for a vCPU and the VM and all its vCPUs will be kept alive. And conceptually it's sound, anything created in the scope of a VM _should_ pin the VM.
Quoted 4 lines
I feel that memslots form a natural way of managing usage of the gmem file. When a memslot is created, it is using the file; hence we take a refcount on the gmem file, and as memslots are removed, we drop refcounts on the gmem file.
Yes and no. It's definitely more natural *if* the goal is to allow guest_memfd memory to exist without being attached to a VM. But I'm not at all convinced that we want to allow that, or that it has desirable properties. With TDX and SNP in particuarly, I'm pretty sure that allowing memory to outlive the VM is very underisable (more below).
Quoted 7 lines
The KVM pointer is shared among all the bindings in gmem?s xarray, and we can enforce that a gmem file is used only with one VM:
+ When binding a memslot to the file, if a kvm pointer exists, it must be the same kvm as the one in this binding
+ When the binding to the last memslot is removed from a file, NULL the kvm pointer.
Nullifying the KVM pointer isn't sufficient, because without additional actions userspace could extract data from a VM by deleting its memslots and then binding the guest_memfd to an attacker controlled VM. Or more likely with TDX and SNP, induce badness by coercing KVM into mapping memory into a guest with the wrong ASID/HKID.
I can think of three ways to handle that:
(a) prevent a different VM from *ever* binding to the gmem instance
(b) free/zero physical pages when unbinding
(c) free/zero when binding to a different VM
Option (a) is easy, but that pretty much defeats the purpose of decopuling guest_memfd from a VM.
Option (b) isn't hard to implement, but it screws up the lifecycle of the memory, e.g. would require memory when a memslot is deleted. That isn't necessarily a deal-breaker, but it runs counter to how KVM memlots currently operate. Memslots are basically just weird page tables, e.g. deleting a memslot doesn't have any impact on the underlying data in memory. TDX throws a wrench in this as removing a page from the Secure EPT is effectively destructive to the data (can't be mapped back in to the VM without zeroing the data), but IMO that's an oddity with TDX and not necessarily something we want to carry over to other VM types.
There would also be performance implications (probably a non-issue in practice), and weirdness if/when we get to sharing, linking and/or mmap()ing gmem. E.g. what should happen if the last memslot (binding) is deleted, but there outstanding userspace mappings?
Option (c) is better from a lifecycle perspective, but it adds its own flavor of complexity, e.g. the performant way to reclaim TDX memory requires the TDMR (effectively the VM pointer), and so a deferred relcaim doesn't really work for TDX. And I'm pretty sure it *can't* work for SNP, because RMP entries must not outlive the VM; KVM can't reuse an ASID if there are pages assigned to that ASID in the RMP, i.e. until all memory belonging to the VM has been fully freed.
Quoted 2 lines
Could binding gmem files not on creation, but at memslot configuration time be sufficient and simpler?
After working through the flows, I think binding on-demand would simplify the refcounting (stating the obvious), but complicate the lifecycle of the memory as well as the contract between KVM and userspace, and would break the separation of concerns between the inode (physical memory / data) and file (VM's view / mappings).
Re: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
Quoted 7 lines · On Tue, Aug 8, 2023 at 2:13?PM Sean Christopherson <[email protected]> wrote:
...
Quoted 4 lines
+ When binding a memslot to the file, if a kvm pointer exists, it must be the same kvm as the one in this binding
+ When the binding to the last memslot is removed from a file, NULL the kvm pointer.
Quoted 6 lines
Nullifying the KVM pointer isn't sufficient, because without additional actions userspace could extract data from a VM by deleting its memslots and then binding the
guest_memfdto an attacker controlled VM. Or more likely with TDX and SNP, induce badness by coercing KVM into mapping memory into a guest with the wrong ASID/HKID.
TDX/SNP have mechanisms i.e. PAMT/RMP tables to ensure that the same memory is not assigned to two different VMs. Deleting memslots should also clear out the contents of the memory as the EPT tables will be zapped in the process and the host will reclaim the memory.
Regards,
Vishal
Sean Christopherson
maintainerRe: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
Quoted 15 lines · Vishal Annapurve
Quoted 7 lines · On Tue, Aug 8, 2023 at 2:13?PM Sean Christopherson <[email protected]> wrote:
...
Quoted 4 lines
+ When binding a memslot to the file, if a kvm pointer exists, it must be the same kvm as the one in this binding
+ When the binding to the last memslot is removed from a file, NULL the kvm pointer.Quoted 6 lines
Nullifying the KVM pointer isn't sufficient, because without additional actions userspace could extract data from a VM by deleting its memslots and then binding the
guest_memfdto an attacker controlled VM. Or more likely with TDX and SNP, induce badness by coercing KVM into mapping memory into a guest with the wrong ASID/HKID.
Quoted 2 lines
TDX/SNP have mechanisms i.e. PAMT/RMP tables to ensure that the same memory is not assigned to two different VMs.
One of the main reasons we pivoted away from using a flag in "struct page" to indicate that a page was private was so that KVM could enforce 1:1 VM:page ownership *without* relying on hardware.
And FWIW, the PAMT provides no protection in this specific case because KVM does TDH.MEM.PAGE.REMOVE when zapping S-EPT entries, and that marks the page clear in the PAMT. The danger there is that physical memory is still encrypted with the guest's HKID, and so mapping the memory into a different VM, which might not be a TDX guest!, could lead to corruption and/or poison #MCs.
The HKID issues wouldn't be a problem if v15 is merged as-is, because zapping S-EPT entries also fully purges and reclaims the page, but as we discussed in one of the many threads, reclaiming physical memory should be tied to the inode, i.e. to memory truly being freed, and not to S-EPTs being zapped. And there is a very good reason for wanting to do that, as it allows KVM to do the expensive cache flush + clear outside of mmu_lock.
Quoted 2 lines
Deleting memslots should also clear out the contents of the memory as the EPT tables will be zapped in the process
No, deleting a memslot should not clear memory. As I said in my previous response, the fact that zapping S-EPT entries is destructive is a limitiation of TDX, not a feature we want to apply to other VM types. And that's not even a fundamental property of TDX, e.g. TDX could remove the limitation, at the cost of consuming quite a bit more memory, by tracking the exact owner by HKID in the PAMT and decoupling S-EPT entries from page ownership.
Or in theory, KVM could workaround the limitation by only doing TDH.MEM.RANGE.BLOCK when zapping S-EPTs. Hmm, that might actually be worth looking at.
Quoted 1 line
and the host will reclaim the memory.
There are no guarantees that the host will reclaim the memory. E.g. QEMU will delete and re-create memslots for "regular" VMs when emulating option ROMs. Even if that use case is nonsensical for confidential VMs (and it probably is nonsensical), I don't want to define KVM's ABI based on what we *think* userspace will do.
Re: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
In reply to Sean ChristophersonQuoted 27 lines · Sean Christopherson
Quoted 12 lines · Ackerley Tng
I?d like to propose an alternative to the refcounting approach between the gmem file and associated kvm, where we think of KVM?s memslots as users of the gmem file.
Instead of having the gmem file pin the VM (i.e. take a refcount on kvm), we could let memslot take a refcount on the gmem file when the memslots are configured.
Here?s a POC patch that flips the refcounting (and modified selftests in the next commit):
https://github.com/googleprodkernel/linux-cc/commit/7f487b029b89b9f3e9b094a721bc0772f3c8c797Quoted 5 lines
One side effect of having the gmem file pin the VM is that now the gmem file becomes sort of a false handle on the VM:
+ Closing the file destroys the file pointers in the VM and invalidates the pointers
Yeah, this is less than ideal. But, it's also how things operate today. KVM doesn't hold references to VMAs or files, e.g. if userspace munmap()s memory, any and all SPTEs pointing at the memory are zapped. The only difference with gmem is that KVM needs to explicitly invalidate file pointers, instead of that happening behind the scenes (no more VMAs to find). Again, I agree the resulting code is more complex than I would prefer, but from a userspace perspective I don't see this as problematic.
Quoted 8 lines
Quoted 2 lines
+ Keeping the file open keeps the VM around in the kernel even though the VM fd may already be closed.
That is perfectly ok. There is plenty of prior art, as well as plenty of ways for userspace to shoot itself in the foot. E.g. open a stats fd for a vCPU and the VM and all its vCPUs will be kept alive. And conceptually it's sound, anything created in the scope of a VM _should_ pin the VM.
Thanks for explaining!
Quoted 11 lines
Quoted 4 lines
I feel that memslots form a natural way of managing usage of the gmem file. When a memslot is created, it is using the file; hence we take a refcount on the gmem file, and as memslots are removed, we drop refcounts on the gmem file.
Yes and no. It's definitely more natural *if* the goal is to allow
guest_memfdmemory to exist without being attached to a VM. But I'm not at all convinced that we want to allow that, or that it has desirable properties. With TDX and SNP in particuarly, I'm pretty sure that allowing memory to outlive the VM is very underisable (more below).
This is a little confusing, with the file/inode split in gmem where the physical memory/data is attached to the inode and the file represents the VM's view of that memory, won't the memory outlive the VM?
This [1] POC was built based on that premise, that the gmem inode can be linked to another file and handed off to another VM, to facilitate intra-host migration, where the point is to save the work of rebuilding the VM's memory in the destination VM.
With this, the bindings don't outlive the VM, but the data/memory does. I think this split design you proposed is really nice.
Quoted 37 lines
Quoted 7 lines
The KVM pointer is shared among all the bindings in gmem?s xarray, and we can enforce that a gmem file is used only with one VM:
+ When binding a memslot to the file, if a kvm pointer exists, it must be the same kvm as the one in this binding
+ When the binding to the last memslot is removed from a file, NULL the kvm pointer.Nullifying the KVM pointer isn't sufficient, because without additional actions userspace could extract data from a VM by deleting its memslots and then binding the
guest_memfdto an attacker controlled VM. Or more likely with TDX and SNP, induce badness by coercing KVM into mapping memory into a guest with the wrong ASID/HKID.I can think of three ways to handle that:
(a) prevent a different VM from *ever* binding to the gmem instance
(b) free/zero physical pages when unbinding
(c) free/zero when binding to a different VMOption (a) is easy, but that pretty much defeats the purpose of decopuling
guest_memfdfrom a VM.Option (b) isn't hard to implement, but it screws up the lifecycle of the memory, e.g. would require memory when a memslot is deleted. That isn't necessarily a deal-breaker, but it runs counter to how KVM memlots currently operate. Memslots are basically just weird page tables, e.g. deleting a memslot doesn't have any impact on the underlying data in memory. TDX throws a wrench in this as removing a page from the Secure EPT is effectively destructive to the data (can't be mapped back in to the VM without zeroing the data), but IMO that's an oddity with TDX and not necessarily something we want to carry over to other VM types.
There would also be performance implications (probably a non-issue in practice), and weirdness if/when we get to sharing, linking and/or mmap()ing gmem. E.g. what should happen if the last memslot (binding) is deleted, but there outstanding userspace mappings?
Quoted 7 lines
Option (c) is better from a lifecycle perspective, but it adds its own flavor of complexity, e.g. the performant way to reclaim TDX memory requires the TDMR (effectively the VM pointer), and so a deferred relcaim doesn't really work for TDX. And I'm pretty sure it *can't* work for SNP, because RMP entries must not outlive the VM; KVM can't reuse an ASID if there are pages assigned to that ASID in the RMP, i.e. until all memory belonging to the VM has been fully freed.
If we are on the same page that the memory should outlive the VM but not the bindings, then associating the gmem inode to a new VM should be a feature and not a bug.
What do we want to defend against here?
(a) Malicious host VMM
For a malicious host VMM to read guest memory (with TDX and SNP), it can create a new VM with the same HKID/ASID as the victim VM, rebind the gmem inode to a VM crafted with an image that dumps the memory.
I believe it is not possible for userspace to arbitrarily select a matching HKID unless userspace uses the intra-host migration ioctls, but if the migration ioctl is used, then EPTs are migrated and the memory dumper VM can't successfully run a different image from the victim VM. If the dumper VM needs to run the same image as the victim VM, then it would be a successful migration rather than an attack. (Perhaps we need to clean up some #MCs here but that can be a separate patch)
(b) Malicious host kernel
A malicious host kernel can allow a malicious host VMM to re-use a HKID for the dumper VM, but this isn't something a better gmem design can defend against.
(c) Attacks using gmem for software-protected VMs
Attacks using gmem for software-protected VMs are possible since there is no real encryption with HKID/ASID (yet?). The selftest for [1] actually uses this lack of encryption to test that the destination VM can read the source VM's memory after the migration. In the POC [1], as long as both destination VM knows where in the inode's memory to read, it can read what it wants to. This is a problem for software-protected VMs, but I feel that it is also a separate issue from gmem's design.
Quoted 6 lines
Quoted 2 lines
Could binding gmem files not on creation, but at memslot configuration time be sufficient and simpler?
After working through the flows, I think binding on-demand would simplify the refcounting (stating the obvious), but complicate the lifecycle of the memory as well as the contract between KVM and userspace,
If we are on the same page that the memory should outlive the VM but not the bindings, does it still complicate the lifecycle of the memory and the userspace/KVM contract? Could it just be a different contract?
Quoted 2 lines
and would break the separation of
concerns between the inode (physical memory / data) and file (VM's view / mappings).
Binding on-demand is orthogonal to the separation of concerns between inode and file, because it can be built regardless of whether we do the gmem file/inode split.
+ This flip-the-refcounting POC is built with the file/inode split and
+ In [2] (the delayed binding approach to solve intra-host migration), I also tried flipping the refcounting, and that without the gmem file/inode split. (Refcounting in [2] is buggy because the file can't take a refcount on KVM, but it would work without taking that refcount)
[1] https://lore.kernel.org/lkml/cover.1691446946.git.ackerleytng at google.com/T/
[2] https://github.com/googleprodkernel/linux-cc/commit/dd5ac5e53f14a1ef9915c9c1e4cc1006a40b49df
Sean Christopherson
maintainerRe: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
Quoted 14 lines · Ackerley Tng
Quoted 11 lines · Sean Christopherson
Quoted 4 lines
I feel that memslots form a natural way of managing usage of the gmem file. When a memslot is created, it is using the file; hence we take a refcount on the gmem file, and as memslots are removed, we drop refcounts on the gmem file.
Yes and no. It's definitely more natural *if* the goal is to allow
guest_memfdmemory to exist without being attached to a VM. But I'm not at all convinced that we want to allow that, or that it has desirable properties. With TDX and SNP in particuarly, I'm pretty sure that allowing memory to outlive the VM is very underisable (more below).
Quoted 3 lines
This is a little confusing, with the file/inode split in gmem where the physical memory/data is attached to the inode and the file represents the VM's view of that memory, won't the memory outlive the VM?
Doh, I overloaded the term "VM". By "VM" I meant the virtual machine as a "thing" the rest of the world sees and interacts with, not the original "struct kvm" object.
Because yes, you're absolutely correct that the memory will outlive "struct kvm", but it won't outlive the virtual machine, and specifically won't outlive the ASID (SNP) / HKID (TDX) to which it's bound.
Quoted 65 lines
This [1] POC was built based on that premise, that the gmem inode can be linked to another file and handed off to another VM, to facilitate intra-host migration, where the point is to save the work of rebuilding the VM's memory in the destination VM.
With this, the bindings don't outlive the VM, but the data/memory does. I think this split design you proposed is really nice.
Quoted 37 lines
Quoted 7 lines
The KVM pointer is shared among all the bindings in gmem?s xarray, and we can enforce that a gmem file is used only with one VM:
+ When binding a memslot to the file, if a kvm pointer exists, it must be the same kvm as the one in this binding
+ When the binding to the last memslot is removed from a file, NULL the kvm pointer.Nullifying the KVM pointer isn't sufficient, because without additional actions userspace could extract data from a VM by deleting its memslots and then binding the
guest_memfdto an attacker controlled VM. Or more likely with TDX and SNP, induce badness by coercing KVM into mapping memory into a guest with the wrong ASID/HKID.I can think of three ways to handle that:
(a) prevent a different VM from *ever* binding to the gmem instance
(b) free/zero physical pages when unbinding
(c) free/zero when binding to a different VMOption (a) is easy, but that pretty much defeats the purpose of decopuling
guest_memfdfrom a VM.Option (b) isn't hard to implement, but it screws up the lifecycle of the memory, e.g. would require memory when a memslot is deleted. That isn't necessarily a deal-breaker, but it runs counter to how KVM memlots currently operate. Memslots are basically just weird page tables, e.g. deleting a memslot doesn't have any impact on the underlying data in memory. TDX throws a wrench in this as removing a page from the Secure EPT is effectively destructive to the data (can't be mapped back in to the VM without zeroing the data), but IMO that's an oddity with TDX and not necessarily something we want to carry over to other VM types.
There would also be performance implications (probably a non-issue in practice), and weirdness if/when we get to sharing, linking and/or mmap()ing gmem. E.g. what should happen if the last memslot (binding) is deleted, but there outstanding userspace mappings?
Quoted 7 lines
Option (c) is better from a lifecycle perspective, but it adds its own flavor of complexity, e.g. the performant way to reclaim TDX memory requires the TDMR (effectively the VM pointer), and so a deferred relcaim doesn't really work for TDX. And I'm pretty sure it *can't* work for SNP, because RMP entries must not outlive the VM; KVM can't reuse an ASID if there are pages assigned to that ASID in the RMP, i.e. until all memory belonging to the VM has been fully freed.
If we are on the same page that the memory should outlive the VM but not the bindings, then associating the gmem inode to a new VM should be a feature and not a bug.
What do we want to defend against here?
(a) Malicious host VMM
For a malicious host VMM to read guest memory (with TDX and SNP), it can create a new VM with the same HKID/ASID as the victim VM, rebind the gmem inode to a VM crafted with an image that dumps the memory.
Quoted 7 lines
I believe it is not possible for userspace to arbitrarily select a matching HKID unless userspace uses the intra-host migration ioctls, but if the migration ioctl is used, then EPTs are migrated and the memory dumper VM can't successfully run a different image from the victim VM. If the dumper VM needs to run the same image as the victim VM, then it would be a successful migration rather than an attack. (Perhaps we need to clean up some #MCs here but that can be a separate patch).
From a guest security perspective, throw TDX and SNP out the window. As far as the design of guest_memfd is concerned, I truly do not care what security properties they provide, I only care about whether or not KVM's support for TDX and SNP is clean, robust, and functionally correct.
Note, I'm not saying I don't care about TDX/SNP. What I'm saying is that I don't want to design something that is beneficial only to what is currently a very niche class of VMs that require specific flavors of hardware.
Quoted 5 lines
(b) Malicious host kernel
A malicious host kernel can allow a malicious host VMM to re-use a HKID for the dumper VM, but this isn't something a better gmem design can defend against.
Yep, completely out-of-scope.
Quoted 8 lines
(c) Attacks using gmem for software-protected VMs
Attacks using gmem for software-protected VMs are possible since there is no real encryption with HKID/ASID (yet?). The selftest for [1] actually uses this lack of encryption to test that the destination VM can read the source VM's memory after the migration. In the POC [1], as long as both destination VM knows where in the inode's memory to read, it can read what it wants to.
Encryption is not required to protect guest memory from less privileged software. The selftests don't rely on lack of encryption, they rely on KVM incorporating host userspace into the TCB.
Just because this RFC doesn't remove the VMM from the TCB for SW-protected VMS, doesn't mean we _can't_ remove the VMM from the TCB. pKVM has already shown that such an implementation is possible. We didn't tackle pKVM-like support in the initial implementation because it's non-trivial, doesn't yet have a concrete use case to fund/drive development, and would have significantly delayed support for the use cases people do actually care about.
There are certainly benefits from memory being encrypted, but it's neither a requirement nor a panacea, as proven by the never ending stream of speculative execution attacks.
Quoted 2 lines
This is a problem for software-protected VMs, but I feel that it is also a separate issue from gmem's design.
No, I don't want guest_memfd to be just be a vehicle for SNP/TDX VMs. Having line of sight to removing host userspace from the TCB is absolutely a must have for me, and having line of sight to improving KVM's security posture for "regular" VMs is even more of a must have. If guest_memfd doesn't provide us a very direct path to (eventually) achieving those goals, then IMO it's a failure.
Which leads me to:
(d) Buggy components
Today, for all intents and purposes, guest memory *must* be mapped writable in the VMM, which means it is all too easy for a benign-but-buggy host component to corrupt guest memory. There are ways to mitigate potential problems, e.g. by developing userspace to adhere to the principle of least privilege inasmuch as possible, but such mitigations would be far less robust than what can be achieved via guest_memfd, and practically speaking I don't see us (Google, but also KVM in general) making progress on deprivileging userspace without forcing the issue.
Quoted 10 lines
Quoted 6 lines
Quoted 2 lines
Could binding gmem files not on creation, but at memslot configuration time be sufficient and simpler?
After working through the flows, I think binding on-demand would simplify the refcounting (stating the obvious), but complicate the lifecycle of the memory as well as the contract between KVM and userspace,
If we are on the same page that the memory should outlive the VM but not the bindings, does it still complicate the lifecycle of the memory and the userspace/KVM contract? Could it just be a different contract?
Not entirely sure I understand what you're asking. Does this question go away with my clarification about struct kvm vs. virtual machine?
Quoted 15 lines
Quoted 2 lines
and would break the separation of
concerns between the inode (physical memory / data) and file (VM's view / mappings).Binding on-demand is orthogonal to the separation of concerns between inode and file, because it can be built regardless of whether we do the gmem file/inode split.
+ This flip-the-refcounting POC is built with the file/inode split and
+ In [2] (the delayed binding approach to solve intra-host migration), I also tried flipping the refcounting, and that without the gmem file/inode split. (Refcounting in [2] is buggy because the file can't take a refcount on KVM, but it would work without taking that refcount)[1] https://lore.kernel.org/lkml/cover.1691446946.git.ackerleytng at google.com/T/
[2] https://github.com/googleprodkernel/linux-cc/commit/dd5ac5e53f14a1ef9915c9c1e4cc1006a40b49df
Re: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
Quoted 19 lines · Sean Christopherson
Quoted 14 lines · Ackerley Tng
Quoted 11 lines · Sean Christopherson
Quoted 4 lines
I feel that memslots form a natural way of managing usage of the gmem file. When a memslot is created, it is using the file; hence we take a refcount on the gmem file, and as memslots are removed, we drop refcounts on the gmem file.
Yes and no. It's definitely more natural *if* the goal is to allow
guest_memfdmemory to exist without being attached to a VM. But I'm not at all convinced that we want to allow that, or that it has desirable properties. With TDX and SNP in particuarly, I'm pretty sure that allowing memory to outlive the VM is very underisable (more below).Quoted 3 lines
This is a little confusing, with the file/inode split in gmem where the physical memory/data is attached to the inode and the file represents the VM's view of that memory, won't the memory outlive the VM?
Quoted 7 lines
Doh, I overloaded the term "VM". By "VM" I meant the virtual machine as a "thing" the rest of the world sees and interacts with, not the original "struct kvm" object.
Because yes, you're absolutely correct that the memory will outlive "struct kvm", but it won't outlive the virtual machine, and specifically won't outlive the ASID (SNP) / HKID (TDX) to which it's bound.
Yup we agree on this now :) The memory should not outlive the the ASID (SNP) / HKID (TDX) to which it's bound.
Quoted 127 lines
Quoted 65 lines
This [1] POC was built based on that premise, that the gmem inode can be linked to another file and handed off to another VM, to facilitate intra-host migration, where the point is to save the work of rebuilding the VM's memory in the destination VM.
With this, the bindings don't outlive the VM, but the data/memory does. I think this split design you proposed is really nice.
Quoted 37 lines
Quoted 7 lines
The KVM pointer is shared among all the bindings in gmem?s xarray, and we can enforce that a gmem file is used only with one VM:
+ When binding a memslot to the file, if a kvm pointer exists, it must be the same kvm as the one in this binding
+ When the binding to the last memslot is removed from a file, NULL the kvm pointer.Nullifying the KVM pointer isn't sufficient, because without additional actions userspace could extract data from a VM by deleting its memslots and then binding the
guest_memfdto an attacker controlled VM. Or more likely with TDX and SNP, induce badness by coercing KVM into mapping memory into a guest with the wrong ASID/HKID.I can think of three ways to handle that:
(a) prevent a different VM from *ever* binding to the gmem instance
(b) free/zero physical pages when unbinding
(c) free/zero when binding to a different VMOption (a) is easy, but that pretty much defeats the purpose of decopuling
guest_memfdfrom a VM.Option (b) isn't hard to implement, but it screws up the lifecycle of the memory, e.g. would require memory when a memslot is deleted. That isn't necessarily a deal-breaker, but it runs counter to how KVM memlots currently operate. Memslots are basically just weird page tables, e.g. deleting a memslot doesn't have any impact on the underlying data in memory. TDX throws a wrench in this as removing a page from the Secure EPT is effectively destructive to the data (can't be mapped back in to the VM without zeroing the data), but IMO that's an oddity with TDX and not necessarily something we want to carry over to other VM types.
There would also be performance implications (probably a non-issue in practice), and weirdness if/when we get to sharing, linking and/or mmap()ing gmem. E.g. what should happen if the last memslot (binding) is deleted, but there outstanding userspace mappings?
Quoted 7 lines
Option (c) is better from a lifecycle perspective, but it adds its own flavor of complexity, e.g. the performant way to reclaim TDX memory requires the TDMR (effectively the VM pointer), and so a deferred relcaim doesn't really work for TDX. And I'm pretty sure it *can't* work for SNP, because RMP entries must not outlive the VM; KVM can't reuse an ASID if there are pages assigned to that ASID in the RMP, i.e. until all memory belonging to the VM has been fully freed.
If we are on the same page that the memory should outlive the VM but not the bindings, then associating the gmem inode to a new VM should be a feature and not a bug.
What do we want to defend against here?
(a) Malicious host VMM
For a malicious host VMM to read guest memory (with TDX and SNP), it can create a new VM with the same HKID/ASID as the victim VM, rebind the gmem inode to a VM crafted with an image that dumps the memory.
Quoted 7 lines
I believe it is not possible for userspace to arbitrarily select a matching HKID unless userspace uses the intra-host migration ioctls, but if the migration ioctl is used, then EPTs are migrated and the memory dumper VM can't successfully run a different image from the victim VM. If the dumper VM needs to run the same image as the victim VM, then it would be a successful migration rather than an attack. (Perhaps we need to clean up some #MCs here but that can be a separate patch).
From a guest security perspective, throw TDX and SNP out the window. As far as the design of
guest_memfdis concerned, I truly do not care what security properties they provide, I only care about whether or not KVM's support for TDX and SNP is clean, robust, and functionally correct.Note, I'm not saying I don't care about TDX/SNP. What I'm saying is that I don't want to design something that is beneficial only to what is currently a very niche class of VMs that require specific flavors of hardware.
Quoted 5 lines
(b) Malicious host kernel
A malicious host kernel can allow a malicious host VMM to re-use a HKID for the dumper VM, but this isn't something a better gmem design can defend against.
Yep, completely out-of-scope.
Quoted 8 lines
(c) Attacks using gmem for software-protected VMs
Attacks using gmem for software-protected VMs are possible since there is no real encryption with HKID/ASID (yet?). The selftest for [1] actually uses this lack of encryption to test that the destination VM can read the source VM's memory after the migration. In the POC [1], as long as both destination VM knows where in the inode's memory to read, it can read what it wants to.
Encryption is not required to protect guest memory from less privileged software. The selftests don't rely on lack of encryption, they rely on KVM incorporating host userspace into the TCB.
Just because this RFC doesn't remove the VMM from the TCB for SW-protected VMS, doesn't mean we _can't_ remove the VMM from the TCB. pKVM has already shown that such an implementation is possible. We didn't tackle pKVM-like support in the initial implementation because it's non-trivial, doesn't yet have a concrete use case to fund/drive development, and would have significantly delayed support for the use cases people do actually care about.
There are certainly benefits from memory being encrypted, but it's neither a requirement nor a panacea, as proven by the never ending stream of speculative execution attacks.
Quoted 2 lines
This is a problem for software-protected VMs, but I feel that it is also a separate issue from gmem's design.
No, I don't want
guest_memfdto be just be a vehicle for SNP/TDX VMs. Having line of sight to removing host userspace from the TCB is absolutely a must have for me, and having line of sight to improving KVM's security posture for "regular" VMs is even more of a must have. Ifguest_memfddoesn't provide us a very direct path to (eventually) achieving those goals, then IMO it's a failure.Which leads me to:
(d) Buggy components
Quoted 8 lines
Today, for all intents and purposes, guest memory *must* be mapped writable in the VMM, which means it is all too easy for a benign-but-buggy host component to corrupt guest memory. There are ways to mitigate potential problems, e.g. by developing userspace to adhere to the principle of least privilege inasmuch as possible, but such mitigations would be far less robust than what can be achieved via
guest_memfd, and practically speaking I don't see us (Google, but also KVM in general) making progress on deprivileging userspace without forcing the issue.
Thanks for adding this point! I should clarify that when I asked about what we want to defend against, I meant that in response to the point that nulling the KVM pointer is insufficient. IIUC (d) explains what the whole of gmem is meant to defend against.
I agree with you that nulling the KVM pointer is insufficient to keep host userspace out of the TCB. Among the three options (a) preventing a different VM (HKID/ASID) from binding to the gmem instance, or zeroing the memory either (b) on unbinding, or (c) on binding to another VM (HKID/ASID),
(a) sounds like adding a check issued to TDX/SNP upon binding and this check would just return OK for software-protected VMs (line of sight to removing host userspace from TCB).
Or, we could go further for software-protected VMs and add tracking in the inode to prevent the same inode from being bound to different "HKID/ASID"s, perhaps like this:
+ On first binding, store the KVM pointer in the inode - not file (but not hold a refcount)
+ On rebinding, check that the KVM matches the pointer in the inode
+ On intra-host migration, update the KVM pointer in the inode to allow binding to the new struct kvm
I think you meant associating the file with a struct kvm at creation time as an implementation for (a), but technically since the inode is the representation of memory, tracking of struct kvm should be with the inode instead of the file.
(b) You're right that this messes up the lifecycle of the memory and wouldn't work with intra-host migration.
(c) sounds like doing the clearing on a check similar to that of (a)
If we track struct kvm with the inode, then I think (a), (b) and (c) can be independent of the refcounting method. What do you think?
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 2 lines
Could binding gmem files not on creation, but at memslot configuration time be sufficient and simpler?
After working through the flows, I think binding on-demand would simplify the refcounting (stating the obvious), but complicate the lifecycle of the memory as well as the contract between KVM and userspace,
Quoted 7 lines
Quoted 3 lines
If we are on the same page that the memory should outlive the VM but not the bindings, does it still complicate the lifecycle of the memory and the userspace/KVM contract? Could it just be a different contract?
Not entirely sure I understand what you're asking. Does this question go away with my clarification about struct kvm vs. virtual machine?
Yes, this question goes away. Thanks!
Quoted 15 lines
Quoted 15 lines
Quoted 2 lines
and would break the separation of
concerns between the inode (physical memory / data) and file (VM's view / mappings).Binding on-demand is orthogonal to the separation of concerns between inode and file, because it can be built regardless of whether we do the gmem file/inode split.
+ This flip-the-refcounting POC is built with the file/inode split and
+ In [2] (the delayed binding approach to solve intra-host migration), I also tried flipping the refcounting, and that without the gmem file/inode split. (Refcounting in [2] is buggy because the file can't take a refcount on KVM, but it would work without taking that refcount)[1] https://lore.kernel.org/lkml/cover.1691446946.git.ackerleytng at google.com/T/
[2] https://github.com/googleprodkernel/linux-cc/commit/dd5ac5e53f14a1ef9915c9c1e4cc1006a40b49df
Sean Christopherson
maintainerRe: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
Quoted 39 lines · Ackerley Tng
Quoted 37 lines · Sean Christopherson
Quoted 36 lines · Ackerley Tng
Quoted 35 lines · Sean Christopherson
Nullifying the KVM pointer isn't sufficient, because without additional actions userspace could extract data from a VM by deleting its memslots and then binding the
guest_memfdto an attacker controlled VM. Or more likely with TDX and SNP, induce badness by coercing KVM into mapping memory into a guest with the wrong ASID/HKID.I can think of three ways to handle that:
(a) prevent a different VM from *ever* binding to the gmem instance
(b) free/zero physical pages when unbinding
(c) free/zero when binding to a different VMOption (a) is easy, but that pretty much defeats the purpose of decopuling
guest_memfdfrom a VM.Option (b) isn't hard to implement, but it screws up the lifecycle of the memory, e.g. would require memory when a memslot is deleted. That isn't necessarily a deal-breaker, but it runs counter to how KVM memlots currently operate. Memslots are basically just weird page tables, e.g. deleting a memslot doesn't have any impact on the underlying data in memory. TDX throws a wrench in this as removing a page from the Secure EPT is effectively destructive to the data (can't be mapped back in to the VM without zeroing the data), but IMO that's an oddity with TDX and not necessarily something we want to carry over to other VM types.
There would also be performance implications (probably a non-issue in practice), and weirdness if/when we get to sharing, linking and/or mmap()ing gmem. E.g. what should happen if the last memslot (binding) is deleted, but there outstanding userspace mappings?
Option (c) is better from a lifecycle perspective, but it adds its own flavor of complexity, e.g. the performant way to reclaim TDX memory requires the TDMR (effectively the VM pointer), and so a deferred relcaim doesn't really work for TDX. And I'm pretty sure it *can't* work for SNP, because RMP entries must not outlive the VM; KVM can't reuse an ASID if there are pages assigned to that ASID in the RMP, i.e. until all memory belonging to the VM has been fully freed.
...
Quoted 25 lines
I agree with you that nulling the KVM pointer is insufficient to keep host userspace out of the TCB. Among the three options (a) preventing a different VM (HKID/ASID) from binding to the gmem instance, or zeroing the memory either (b) on unbinding, or (c) on binding to another VM (HKID/ASID),
(a) sounds like adding a check issued to TDX/SNP upon binding and this check would just return OK for software-protected VMs (line of sight to removing host userspace from TCB).
Or, we could go further for software-protected VMs and add tracking in the inode to prevent the same inode from being bound to different "HKID/ASID"s, perhaps like this:
+ On first binding, store the KVM pointer in the inode - not file (but not hold a refcount)
+ On rebinding, check that the KVM matches the pointer in the inode
+ On intra-host migration, update the KVM pointer in the inode to allow binding to the new struct kvmI think you meant associating the file with a struct kvm at creation time as an implementation for (a), but technically since the inode is the representation of memory, tracking of struct kvm should be with the inode instead of the file.
Quoted 4 lines
(b) You're right that this messes up the lifecycle of the memory and wouldn't work with intra-host migration.
(c) sounds like doing the clearing on a check similar to that of (a)
Sort of, though it's much nastier, because it requires the "old" KVM instance to be alive enough to support various operations. I.e. we'd have to make stronger guarantees about exactly when the handoff/transition could happen.
Quoted 2 lines
If we track struct kvm with the inode, then I think (a), (b) and (c) can be independent of the refcounting method. What do you think?
No go. Because again, the inode (physical memory) is coupled to the virtual machine as a thing, not to a "struct kvm". Or more concretely, the inode is coupled to an ASID or an HKID, and there can be multiple "struct kvm" objects associated with a single ASID. And at some point in the future, I suspect we'll have multiple KVM objects per HKID too.
The current SEV use case is for the migration helper, where two KVM objects share a single ASID (the "real" VM and the helper). I suspect TDX will end up with similar behavior where helper "VMs" can use the HKID of the "real" VM. For KVM, that means multiple struct kvm objects being associated with a single HKID.
To prevent use-after-free, KVM "just" needs to ensure the helper instances can't outlive the real instance, i.e. can't use the HKID/ASID after the owning virtual machine has been destroyed.
To put it differently, "struct kvm" is a KVM software construct that _usually_, but not always, is associated 1:1 with a virtual machine.
And FWIW, stashing the pointer without holding a reference would not be a complete solution, because it couldn't guard against KVM reusing a pointer. E.g. if a struct kvm was unbound and then freed, KVM could reuse the same memory for a new struct kvm, with a different ASID/HKID, and get a false negative on the rebinding check.
Re: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
Quoted 68 lines · Sean Christopherson
Quoted 39 lines · Ackerley Tng
Quoted 37 lines · Sean Christopherson
Quoted 36 lines · Ackerley Tng
Quoted 35 lines · Sean Christopherson
Nullifying the KVM pointer isn't sufficient, because without additional actions userspace could extract data from a VM by deleting its memslots and then binding the
guest_memfdto an attacker controlled VM. Or more likely with TDX and SNP, induce badness by coercing KVM into mapping memory into a guest with the wrong ASID/HKID.I can think of three ways to handle that:
(a) prevent a different VM from *ever* binding to the gmem instance
(b) free/zero physical pages when unbinding
(c) free/zero when binding to a different VMOption (a) is easy, but that pretty much defeats the purpose of decopuling
guest_memfdfrom a VM.Option (b) isn't hard to implement, but it screws up the lifecycle of the memory, e.g. would require memory when a memslot is deleted. That isn't necessarily a deal-breaker, but it runs counter to how KVM memlots currently operate. Memslots are basically just weird page tables, e.g. deleting a memslot doesn't have any impact on the underlying data in memory. TDX throws a wrench in this as removing a page from the Secure EPT is effectively destructive to the data (can't be mapped back in to the VM without zeroing the data), but IMO that's an oddity with TDX and not necessarily something we want to carry over to other VM types.
There would also be performance implications (probably a non-issue in practice), and weirdness if/when we get to sharing, linking and/or mmap()ing gmem. E.g. what should happen if the last memslot (binding) is deleted, but there outstanding userspace mappings?
Option (c) is better from a lifecycle perspective, but it adds its own flavor of complexity, e.g. the performant way to reclaim TDX memory requires the TDMR (effectively the VM pointer), and so a deferred relcaim doesn't really work for TDX. And I'm pretty sure it *can't* work for SNP, because RMP entries must not outlive the VM; KVM can't reuse an ASID if there are pages assigned to that ASID in the RMP, i.e. until all memory belonging to the VM has been fully freed.
...
Quoted 25 lines
I agree with you that nulling the KVM pointer is insufficient to keep host userspace out of the TCB. Among the three options (a) preventing a different VM (HKID/ASID) from binding to the gmem instance, or zeroing the memory either (b) on unbinding, or (c) on binding to another VM (HKID/ASID),
(a) sounds like adding a check issued to TDX/SNP upon binding and this check would just return OK for software-protected VMs (line of sight to removing host userspace from TCB).
Or, we could go further for software-protected VMs and add tracking in the inode to prevent the same inode from being bound to different "HKID/ASID"s, perhaps like this:
+ On first binding, store the KVM pointer in the inode - not file (but not hold a refcount)
+ On rebinding, check that the KVM matches the pointer in the inode
+ On intra-host migration, update the KVM pointer in the inode to allow binding to the new struct kvmI think you meant associating the file with a struct kvm at creation time as an implementation for (a), but technically since the inode is the representation of memory, tracking of struct kvm should be with the inode instead of the file.
Quoted 9 lines
Quoted 4 lines
(b) You're right that this messes up the lifecycle of the memory and wouldn't work with intra-host migration.
(c) sounds like doing the clearing on a check similar to that of (a)
Sort of, though it's much nastier, because it requires the "old" KVM instance to be alive enough to support various operations. I.e. we'd have to make stronger guarantees about exactly when the handoff/transition could happen.
Good point!
Quoted 18 lines
Quoted 2 lines
If we track struct kvm with the inode, then I think (a), (b) and (c) can be independent of the refcounting method. What do you think?
No go. Because again, the inode (physical memory) is coupled to the virtual machine as a thing, not to a "struct kvm". Or more concretely, the inode is coupled to an ASID or an HKID, and there can be multiple "struct kvm" objects associated with a single ASID. And at some point in the future, I suspect we'll have multiple KVM objects per HKID too.
The current SEV use case is for the migration helper, where two KVM objects share a single ASID (the "real" VM and the helper). I suspect TDX will end up with similar behavior where helper "VMs" can use the HKID of the "real" VM. For KVM, that means multiple struct kvm objects being associated with a single HKID.
To prevent use-after-free, KVM "just" needs to ensure the helper instances can't outlive the real instance, i.e. can't use the HKID/ASID after the owning virtual machine has been destroyed.
Quoted 8 lines
To put it differently, "struct kvm" is a KVM software construct that _usually_, but not always, is associated 1:1 with a virtual machine.
And FWIW, stashing the pointer without holding a reference would not be a complete solution, because it couldn't guard against KVM reusing a pointer. E.g. if a struct kvm was unbound and then freed, KVM could reuse the same memory for a new struct kvm, with a different ASID/HKID, and get a false negative on the rebinding check.
I agree that inode (physical memory) is coupled to the virtual machine as a more generic concept.
I was hoping that in the absence of CC hardware providing a HKID/ASID, the struct kvm pointer could act as a representation of the "virtual machine". You're definitely right that KVM could reuse a pointer and so that idea doesn't stand.
I thought about generating UUIDs to represent "virtual machines" in the absence of CC hardware, and this UUID could be transferred during intra-host migration, but this still doesn't take host userspace out of the TCB. A malicious host VMM could just use the migration ioctl to copy the UUID to a malicious dumper VM, which would then pass checks with a gmem file linked to the malicious dumper VM. This is fine for HKID/ASIDs because the memory is encrypted; with UUIDs there's no memory encryption.
Circling back to the original topic, was associating the file with struct kvm at gmem file creation time meant to constrain the use of the gmem file to one struct kvm, or one virtual machine, or something else?
Follow up questions:
1. Since the physical memory's representation is the inode and should be coupled to the virtual machine (as a concept, not struct kvm), should the binding/coupling be with the file, or the inode?
2. Should struct kvm still be bound to the file/inode at gmem file creation time, since
+ struct kvm isn't a good representation of a "virtual machine"
+ we currently don't have anything that really represents a "virtual machine" without hardware support
I'd also like to bring up another userspace use case that Google has:
re-use of gmem files for rebooting guests when the KVM instance is destroyed and rebuilt.
When rebooting a VM there are some steps relating to gmem that are performance-sensitive:
a. Zeroing pages from the old VM when we close a gmem file/inode
b. Deallocating pages from the old VM when we close a gmem file/inode
c. Allocating pages for the new VM from the new gmem file/inode
d. Zeroing pages on page allocation
We want to reuse the gmem file to save re-allocating pages (b. and c.), and one of the two page zeroing allocations (a. or d.).
Binding the gmem file to a struct kvm on creation time means the gmem file can't be reused with another VM on reboot. Also, host userspace is forced to close the gmem file to allow the old VM to be freed.
For other places where files pin KVM, like the stats fd pinning vCPUs, I guess that matters less since there isn't much of a penalty to close and re-open the stats fd.
Re: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
Quoted 4 lines · Ackerley Tng
1. Since the physical memory's representation is the inode and should be coupled to the virtual machine (as a concept, not struct kvm), should the binding/coupling be with the file, or the inode?
I've been working on Gunyah's implementation in parallel (not yet posted anywhere). Thus far, I've coupled the virtual machine struct to the struct file so that I can increment the file refcount when mapping the gmem to the virtual machine.
Quoted 23 lines
2. Should struct kvm still be bound to the file/inode at gmem file creation time, since
+ struct kvm isn't a good representation of a "virtual machine"
+ we currently don't have anything that really represents a "virtual machine" without hardware supportI'd also like to bring up another userspace use case that Google has:
re-use of gmem files for rebooting guests when the KVM instance is destroyed and rebuilt.When rebooting a VM there are some steps relating to gmem that are performance-sensitive:
a. Zeroing pages from the old VM when we close a gmem file/inode
b. Deallocating pages from the old VM when we close a gmem file/inode
c. Allocating pages for the new VM from the new gmem file/inode
d. Zeroing pages on page allocationWe want to reuse the gmem file to save re-allocating pages (b. and c.), and one of the two page zeroing allocations (a. or d.).
Quoted 7 lines
Binding the gmem file to a struct kvm on creation time means the gmem file can't be reused with another VM on reboot. Also, host userspace is forced to close the gmem file to allow the old VM to be freed.
For other places where files pin KVM, like the stats fd pinning vCPUs, I guess that matters less since there isn't much of a penalty to close and re-open the stats fd.
I had a 3rd question that's related to how to wire the gmem up to a virtual machine:
I learned of a usecase to implement copy-on-write for gmem. The premise would be to have a "golden copy" of the memory that multiple virtual machines can map in as RO. If a virtual machine tries to write to those pages, they get copied to a virtual machine-specific page that isn't shared with other VMs. How do we track those pages?
Thanks,
Elliot
Sean Christopherson
maintainerRe: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
Quoted 8 lines · Elliot Berman
I had a 3rd question that's related to how to wire the gmem up to a virtual machine:
I learned of a usecase to implement copy-on-write for gmem. The premise would be to have a "golden copy" of the memory that multiple virtual machines can map in as RO. If a virtual machine tries to write to those pages, they get copied to a virtual machine-specific page that isn't shared with other VMs. How do we track those pages?
The answer is going to be gunyah specific, because gmem itself isn't designed to provide a virtualization layer ("virtual" in the virtual memory sense, not in the virtual machine sense). Like any other CoW implementation, the RO page would need to be copied to a different physical page, and whatever layer translates gfns to physical pages would need to be updated. E.g. in gmem terms, allocate a new gmem page/instance and update the gfn=>gmem[offset] translation in KVM/gunyah.
For VMA-based memory, that translation happens in the primary MMU, and is largely transparent to KVM (or any other secondary MMU). E.g. the primary MMU works with the backing store (if necessary) to allocate a new page and do the copy, notifies secondary MMUs, zaps the old PTE(s), and then installs the new PTE(s). KVM/gunyah just needs to react to the mmu_notifier event, e.g. zap secondary MMU PTEs, and then KVM/gunyah naturally gets the new, writable page/PTE when following the host virtual address, e.g. via gup().
The downside of eliminating the middle-man (primary MMU) from gmem is that the "owner" (KVM or gunyah) is now responsible for these types of operations. For some things, e.g. page migration, it's actually easier in some ways, but for CoW it's quite a bit more work for KVM/gunyah because KVM/gunyah now needs to do things that were previously handled by the primary MMU.
In KVM, assuming no additional support in KVM, doing CoW would mean modifying memslots to redirect the gfn from the RO page to the writable page. For a variety of reasons, that would be _extremely_ expensive in KVM, but still possible. If there were a strong use case for supporting CoW with KVM+gmem, then I suspect that we'd probably implement new KVM uAPI of some form to provide reasonable performance.
But I highly doubt we'll ever do that, because one of core tenets of KVM+gmem is to isolate guest memory from the rest of the world, and especially from host userspace, and that just doesn't mesh well with CoW'd memory being shared across multiple VMs.
Sean Christopherson
maintainerRe: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
In reply to Ackerley TngQuoted 36 lines · Ackerley Tng
Quoted 18 lines · Sean Christopherson
Quoted 2 lines
If we track struct kvm with the inode, then I think (a), (b) and (c) can be independent of the refcounting method. What do you think?
No go. Because again, the inode (physical memory) is coupled to the virtual machine as a thing, not to a "struct kvm". Or more concretely, the inode is coupled to an ASID or an HKID, and there can be multiple "struct kvm" objects associated with a single ASID. And at some point in the future, I suspect we'll have multiple KVM objects per HKID too.
The current SEV use case is for the migration helper, where two KVM objects share a single ASID (the "real" VM and the helper). I suspect TDX will end up with similar behavior where helper "VMs" can use the HKID of the "real" VM. For KVM, that means multiple struct kvm objects being associated with a single HKID.
To prevent use-after-free, KVM "just" needs to ensure the helper instances can't outlive the real instance, i.e. can't use the HKID/ASID after the owning virtual machine has been destroyed.
Quoted 8 lines
To put it differently, "struct kvm" is a KVM software construct that _usually_, but not always, is associated 1:1 with a virtual machine.
And FWIW, stashing the pointer without holding a reference would not be a complete solution, because it couldn't guard against KVM reusing a pointer. E.g. if a struct kvm was unbound and then freed, KVM could reuse the same memory for a new struct kvm, with a different ASID/HKID, and get a false negative on the rebinding check.
I agree that inode (physical memory) is coupled to the virtual machine as a more generic concept.
I was hoping that in the absence of CC hardware providing a HKID/ASID, the struct kvm pointer could act as a representation of the "virtual machine". You're definitely right that KVM could reuse a pointer and so that idea doesn't stand.
Quoted 8 lines
I thought about generating UUIDs to represent "virtual machines" in the absence of CC hardware, and this UUID could be transferred during intra-host migration, but this still doesn't take host userspace out of the TCB. A malicious host VMM could just use the migration ioctl to copy the UUID to a malicious dumper VM, which would then pass checks with a gmem file linked to the malicious dumper VM. This is fine for HKID/ASIDs because the memory is encrypted; with UUIDs there's no memory encryption.
I don't understand what problem you're trying to solve. I don't see a need to provide a single concrete representation/definition of a "virtual machine". E.g. there's no need for a formal definition to securely perform intrahost migration, KVM just needs to ensure that the migration doesn't compromise guest security, functionality, etc.
That gets a lot more complex if the target KVM instance (module, not "struct kvm") is a different KVM, e.g. when migrating to a different host. Then there needs to be a way to attest that the target is trusted and whatnot, but that still doesn't require there to be a formal definition of a "virtual machine".
Quoted 3 lines
Circling back to the original topic, was associating the file with struct kvm at gmem file creation time meant to constrain the use of the gmem file to one struct kvm, or one virtual machine, or something else?
It's meant to keep things as simple as possible (relatively speaking). A 1:1 association between a KVM instance and a gmem instance means we don't have to worry about the edge cases and oddities I pointed out earlier in this thread.
Quoted 5 lines
Follow up questions:
1. Since the physical memory's representation is the inode and should be coupled to the virtual machine (as a concept, not struct kvm), should the binding/coupling be with the file, or the inode?
Both. The @kvm instance is bound to a file, because the file is that @kvm's view of the underlying memory, e.g. effectively provides the translation of guest addresses to host memory. The @kvm instance is indirectly bound to the inode because the file is bound to the inode.
Quoted 2 lines
2. Should struct kvm still be bound to the file/inode at gmem file creation time, since
Yes.
Quoted 1 line
+ struct kvm isn't a good representation of a "virtual machine"
I don't see how this is relevant, because as above, I don't see why we need a canonical represenation of a virtual machine.
Quoted 2 lines
+ we currently don't have anything that really represents a "virtual machine" without hardware support
HKIDs and ASIDs don't provide a "virtual machine" representation either. E.g. if a TDX guest is live migrated to a different host, it will likely have a different HKID, and definitely have a different encryption key, but it's still the same virtual machine.
Quoted 6 lines
I'd also like to bring up another userspace use case that Google has:
re-use of gmem files for rebooting guests when the KVM instance is destroyed and rebuilt.When rebooting a VM there are some steps relating to gmem that are performance-sensitive:
If we (Google) really cared about performance, then we shouldn't destroy and recreate the VM in the first place. E.g. the cost of zapping, freeing, re-allocating and re-populating SPTEs is far from trivial. Pulling RESET shouldn't change what memory that is assigned to a VM, and reseting stats is downright bizarre IMO.
In other words, I think Google's approach of destroying the VM to emulate a reboot is asinine. I'm not totally against extending KVM's uAPI to play nice with such an approach, but I'm not exactly sympathetic either.
Quoted 10 lines
a. Zeroing pages from the old VM when we close a gmem file/inode
b. Deallocating pages from the old VM when we close a gmem file/inode
c. Allocating pages for the new VM from the new gmem file/inode
d. Zeroing pages on page allocationWe want to reuse the gmem file to save re-allocating pages (b. and c.), and one of the two page zeroing allocations (a. or d.).
Binding the gmem file to a struct kvm on creation time means the gmem file can't be reused with another VM on reboot.
Not without KVM's assistance, which userspace will need for TDX and SNP VMs no matter what, e.g. to ensure the new and old KVM instance get the same HKID/ASID. And we've already mapped out the more complex case of intrahost migration, so I don't expect this to be at all challenging to implement.
Quoted 2 lines
Also, host userspace is forced to close the gmem file to allow the old VM to be freed.
Yes, but that can happen after the "new" VM has instantiated its file/view of guest memory.
Quoted 3 lines
For other places where files pin KVM, like the stats fd pinning vCPUs, I guess that matters less since there isn't much of a penalty to close and re-open the stats fd.
Re: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
Quoted 57 lines · Sean Christopherson
Quoted 36 lines · Ackerley Tng
Quoted 18 lines · Sean Christopherson
Quoted 2 lines
If we track struct kvm with the inode, then I think (a), (b) and (c) can be independent of the refcounting method. What do you think?
No go. Because again, the inode (physical memory) is coupled to the virtual machine as a thing, not to a "struct kvm". Or more concretely, the inode is coupled to an ASID or an HKID, and there can be multiple "struct kvm" objects associated with a single ASID. And at some point in the future, I suspect we'll have multiple KVM objects per HKID too.
The current SEV use case is for the migration helper, where two KVM objects share a single ASID (the "real" VM and the helper). I suspect TDX will end up with similar behavior where helper "VMs" can use the HKID of the "real" VM. For KVM, that means multiple struct kvm objects being associated with a single HKID.
To prevent use-after-free, KVM "just" needs to ensure the helper instances can't outlive the real instance, i.e. can't use the HKID/ASID after the owning virtual machine has been destroyed.
Quoted 8 lines
To put it differently, "struct kvm" is a KVM software construct that _usually_, but not always, is associated 1:1 with a virtual machine.
And FWIW, stashing the pointer without holding a reference would not be a complete solution, because it couldn't guard against KVM reusing a pointer. E.g. if a struct kvm was unbound and then freed, KVM could reuse the same memory for a new struct kvm, with a different ASID/HKID, and get a false negative on the rebinding check.
I agree that inode (physical memory) is coupled to the virtual machine as a more generic concept.
I was hoping that in the absence of CC hardware providing a HKID/ASID, the struct kvm pointer could act as a representation of the "virtual machine". You're definitely right that KVM could reuse a pointer and so that idea doesn't stand.
Quoted 8 lines
I thought about generating UUIDs to represent "virtual machines" in the absence of CC hardware, and this UUID could be transferred during intra-host migration, but this still doesn't take host userspace out of the TCB. A malicious host VMM could just use the migration ioctl to copy the UUID to a malicious dumper VM, which would then pass checks with a gmem file linked to the malicious dumper VM. This is fine for HKID/ASIDs because the memory is encrypted; with UUIDs there's no memory encryption.
I don't understand what problem you're trying to solve. I don't see a need to provide a single concrete representation/definition of a "virtual machine". E.g. there's no need for a formal definition to securely perform intrahost migration, KVM just needs to ensure that the migration doesn't compromise guest security, functionality, etc.
That gets a lot more complex if the target KVM instance (module, not "struct kvm") is a different KVM, e.g. when migrating to a different host. Then there needs to be a way to attest that the target is trusted and whatnot, but that still doesn't require there to be a formal definition of a "virtual machine".
Quoted 8 lines
Quoted 3 lines
Circling back to the original topic, was associating the file with struct kvm at gmem file creation time meant to constrain the use of the gmem file to one struct kvm, or one virtual machine, or something else?
It's meant to keep things as simple as possible (relatively speaking). A 1:1 association between a KVM instance and a gmem instance means we don't have to worry about the edge cases and oddities I pointed out earlier in this thread.
I looked through this thread again and re-read the edge cases and oddities that was pointed out earlier (last paragraph at [1]) and I think I understand better, and I have just one last clarification.
It was previously mentioned that binding on creation time simplifies the lifecycle of memory:
"(a) prevent a different VM from *ever* binding to the gmem instance" [1]
Does this actually mean
"prevent a different struct kvm from *ever* binding to this gmem file"
?
If so, then binding on creation
+ Makes the gmem *file* (and just not the bindings xarray) the binding between struct kvm and the file.
+ Simplifies the KVM-userspace contract to "this gmem file can only be used with this struct kvm"
Binding on creation doesn't offer any way to block the contents of the inode from being used with another "virtual machine" though, since we can have more than one gmem file pointing to the same inode, and the other gmem file is associated with another struct kvm. (And a strut kvm isn't associated 1:1 with a virtual machine [2])
The point about an inode needing to be coupled to a virtual machine as a thing [2] led me to try to find a single concrete representation of a "virtual machine".
Is locking inode contents to a "virtual machine" outside the scope of gmem? If so, then it is fine to bind on creation time, use a VM ioctl over a system ioctl, and the method of refcounting in gmem v12 is okay.
[1] https://lore.kernel.org/lkml/ZNKv9ul2I7A4V7IF at google.com/
[2] https://lore.kernel.org/lkml/ZOO782YGRY0YMuPu at google.com/
Quoted 1 line
<snip>
Sean Christopherson
maintainerRe: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
Quoted 75 lines · Ackerley Tng
Quoted 57 lines · Sean Christopherson
Quoted 36 lines · Ackerley Tng
Quoted 18 lines · Sean Christopherson
Quoted 2 lines
If we track struct kvm with the inode, then I think (a), (b) and (c) can be independent of the refcounting method. What do you think?
No go. Because again, the inode (physical memory) is coupled to the virtual machine as a thing, not to a "struct kvm". Or more concretely, the inode is coupled to an ASID or an HKID, and there can be multiple "struct kvm" objects associated with a single ASID. And at some point in the future, I suspect we'll have multiple KVM objects per HKID too.
The current SEV use case is for the migration helper, where two KVM objects share a single ASID (the "real" VM and the helper). I suspect TDX will end up with similar behavior where helper "VMs" can use the HKID of the "real" VM. For KVM, that means multiple struct kvm objects being associated with a single HKID.
To prevent use-after-free, KVM "just" needs to ensure the helper instances can't outlive the real instance, i.e. can't use the HKID/ASID after the owning virtual machine has been destroyed.
Quoted 8 lines
To put it differently, "struct kvm" is a KVM software construct that _usually_, but not always, is associated 1:1 with a virtual machine.
And FWIW, stashing the pointer without holding a reference would not be a complete solution, because it couldn't guard against KVM reusing a pointer. E.g. if a struct kvm was unbound and then freed, KVM could reuse the same memory for a new struct kvm, with a different ASID/HKID, and get a false negative on the rebinding check.
I agree that inode (physical memory) is coupled to the virtual machine as a more generic concept.
I was hoping that in the absence of CC hardware providing a HKID/ASID, the struct kvm pointer could act as a representation of the "virtual machine". You're definitely right that KVM could reuse a pointer and so that idea doesn't stand.
Quoted 8 lines
I thought about generating UUIDs to represent "virtual machines" in the absence of CC hardware, and this UUID could be transferred during intra-host migration, but this still doesn't take host userspace out of the TCB. A malicious host VMM could just use the migration ioctl to copy the UUID to a malicious dumper VM, which would then pass checks with a gmem file linked to the malicious dumper VM. This is fine for HKID/ASIDs because the memory is encrypted; with UUIDs there's no memory encryption.
I don't understand what problem you're trying to solve. I don't see a need to provide a single concrete representation/definition of a "virtual machine". E.g. there's no need for a formal definition to securely perform intrahost migration, KVM just needs to ensure that the migration doesn't compromise guest security, functionality, etc.
That gets a lot more complex if the target KVM instance (module, not "struct kvm") is a different KVM, e.g. when migrating to a different host. Then there needs to be a way to attest that the target is trusted and whatnot, but that still doesn't require there to be a formal definition of a "virtual machine".
Quoted 8 lines
Quoted 3 lines
Circling back to the original topic, was associating the file with struct kvm at gmem file creation time meant to constrain the use of the gmem file to one struct kvm, or one virtual machine, or something else?
It's meant to keep things as simple as possible (relatively speaking). A 1:1 association between a KVM instance and a gmem instance means we don't have to worry about the edge cases and oddities I pointed out earlier in this thread.
I looked through this thread again and re-read the edge cases and oddities that was pointed out earlier (last paragraph at [1]) and I think I understand better, and I have just one last clarification.
It was previously mentioned that binding on creation time simplifies the lifecycle of memory:
Quoted 7 lines
"(a) prevent a different VM from *ever* binding to the gmem instance" [1]
Does this actually mean
"prevent a different struct kvm from *ever* binding to this gmem file"
?
Yes.
Quoted 4 lines
If so, then binding on creation
+ Makes the gmem *file* (and just not the bindings xarray) the binding between struct kvm and the file.
Yep.
Quoted 2 lines
+ Simplifies the KVM-userspace contract to "this gmem file can only be used with this struct kvm"
Yep.
Quoted 5 lines
Binding on creation doesn't offer any way to block the contents of the inode from being used with another "virtual machine" though, since we can have more than one gmem file pointing to the same inode, and the other gmem file is associated with another struct kvm. (And a strut kvm isn't associated 1:1 with a virtual machine [2])
Yep.
Quoted 6 lines
The point about an inode needing to be coupled to a virtual machine as a thing [2] led me to try to find a single concrete representation of a "virtual machine".
Is locking inode contents to a "virtual machine" outside the scope of gmem?
Yes, because it's not gmem's responsibility to define "secure" (from a guest perspective) or "safe" (from a platform stability and correctness perspective).
E.g. inserting additional vCPUs into the VM a la the SEV migration helper thing is comically insecure without some way to attest the helper code. Building policy into the host kernel/KVM to do that attestation or otherwise determine what code is/isn't safe for the guest to run is firmly out-of-scope. KVM can certainly provide the tools and help with enforcement, but the policy needs to be defined elsewhere. Even for something like pKVM, where KVM is in the TCB, KVM still doesn't define who/what to trust (though KVM is heavily involved in enforcing security stuff).
And for platform safety, e.g. not allowing two VMs to use the same HKID (ignoring helpers for the moment), that's a KVM problem but NOT a gmem problem. The point I raised in link[2] about a gmem inode and thus the HKID/ASID associated with the inode being bound to the "virtual machine" still holds true, but (a) it's not a 1:1 correlation, e.g. a VM could utilize multiple gmem inodes (all with the same HKID/ASID), and (b) the safety and functional correctness aspects aren't unique to gmem, e.g. even when when gmem isn't in the picture, KVM needs to make sure it manages ASIDs correctly. The only difference with SNP in the picture is that if KVM screws up ASID management, bad things happen to the host, not (just) the guest.
Quoted 7 lines
If so, then it is fine to bind on creation time, use a VM ioctl
over a system ioctl, and the method of refcounting in gmem v12 is okay.[1] https://lore.kernel.org/lkml/ZNKv9ul2I7A4V7IF at google.com/
[2] https://lore.kernel.org/lkml/ZOO782YGRY0YMuPu at google.com/Quoted 1 line
<snip>
Re: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
In reply to Sean ChristophersonOn 7/19/2023 7:44 AM, Sean Christopherson wrote:
[...]
virt/kvm/guest_mem.c line 26
Quoted 8 lines
virt/kvm/guest_mem.c new file +8
-0+ +static struct folio *kvm_gmem_get_folio (struct file *file,pgoff_t index) +{ +struct folio *folio; + +/* TODO: Support huge pages. */ + folio =filemap_grab_folio (file->f_mapping, index); +if (!folio)
Should use? if ((IS_ERR(folio)) instead.
Quoted 27 lines
virt/kvm/guest_mem.c new file +27
-0+return NULL ; + +/* +* Use the up-to-date flag to track whether or not the memory has been +* zeroed before being handed off to the guest. There is no backing +* storage for the memory, so the folio will remain up-to-date until +* it's removed. +* +* TODO: Skip clearing pages when trusted firmware will do it when +* assigning memory to the guest. +*/ +if (!folio_test_uptodate (folio)) { +unsigned long nr_pages =folio_nr_pages (folio); +unsigned long i; + +for (i =0 ; i < nr_pages; i++) +clear_highpage (folio_page (folio, i)); + +folio_mark_uptodate (folio); + } + +/* +* Ignore accessed, referenced, and dirty flags. The memory is +* unevictable and there is no storage to write back to. +*/ +return folio; +}
[...]
Quoted 25 lines
virt/kvm/guest_mem.c new file +25
-0+ +static long kvm_gmem_allocate (struct inode *inode,loff_t offset,loff_t len) +{ +struct address_space *mapping = inode->i_mapping; +pgoff_t start, index, end; +int r; + +/* Dedicated guest is immutable by default. */ +if (offset + len >i_size_read (inode)) +return -EINVAL ; + +filemap_invalidate_lock_shared (mapping); + + start = offset >>PAGE_SHIFT ; + end = (offset + len) >>PAGE_SHIFT ; + + r =0 ; +for (index = start; index < end; ) { +struct folio *folio; + +if (signal_pending (current)) { + r = -EINTR ; +break ; + } +
virt/kvm/guest_mem.c line 156
Quoted 10 lines
virt/kvm/guest_mem.c new file +10
-0+ folio =kvm_gmem_get_folio (inode, index); +if (!folio) { + r = -ENOMEM ; +break ; + } + + index =folio_next_index (folio); + +folio_unlock (folio); +folio_put (folio);
May be a dumb question, why we get the folio and then put it immediately? Will it make the folio be released back to the page allocator?
Quoted 13 lines
virt/kvm/guest_mem.c new file +13
-0+ +/* 64-bit only, wrapping the index should be impossible. */ +if (WARN_ON_ONCE (!index)) +break ; + +cond_resched (); + } + +filemap_invalidate_unlock_shared (mapping); + +return r; +} +
[...]
Quoted 53 lines
virt/kvm/guest_mem.c new file +53
-0+ +int kvm_gmem_bind (struct kvm *kvm,struct kvm_memory_slot *slot, +unsigned int fd,loff_t offset) +{ +loff_t size = slot->npages <<PAGE_SHIFT ; +unsigned long start, end, flags; +struct kvm_gmem *gmem; +struct inode *inode; +struct file *file; + +BUILD_BUG_ON (sizeof (gfn_t) !=sizeof (slot->gmem.pgoff)); + + file =fget (fd); +if (!file) +return -EINVAL ; + +if (file->f_op != &kvm_gmem_fops) + goto err; + + gmem = file->private_data; +if (gmem->kvm != kvm) + goto err; + + inode =file_inode (file); + flags = (unsigned long )inode->i_private; + +/* +* For simplicity, require the offset into the file and the size of the +* memslot to be aligned to the largest possible page size used to back +* the file (same as the size of the file itself). +*/ +if (!kvm_gmem_is_valid_size (offset, flags) || + !kvm_gmem_is_valid_size (size, flags)) + goto err; + +if (offset + size >i_size_read (inode)) + goto err; + +filemap_invalidate_lock (inode->i_mapping); + + start = offset >>PAGE_SHIFT ; + end = start + slot->npages; + +if (!xa_empty (&gmem->bindings) && +xa_find (&gmem->bindings, &start, end -1 ,XA_PRESENT )) { +filemap_invalidate_unlock (inode->i_mapping); + goto err; + } + +/* +* No synchronize_rcu() needed, any in-flight readers are guaranteed to +* be see either a NULL file or this new file, no need for them to go +* away.
virt/kvm/guest_mem.c line 486
Quoted 10 lines
virt/kvm/guest_mem.c new file +10
-0+*/ +rcu_assign_pointer (slot->gmem.file, file); + slot->gmem.pgoff = start; + +xa_store_range (&gmem->bindings, start, end -1 , slot,GFP_KERNEL ); +filemap_invalidate_unlock (inode->i_mapping); + +/* +* Drop the reference to the file, even on success. The file pins KVM, +* not the other way 'round. Active bindings are invalidated if the
an extra ',? or maybe around?
Quoted 10 lines
virt/kvm/guest_mem.c new file +10
-0+* file is closed before memslots are destroyed. +*/ +fput (file); +return 0 ; + +err: +fput (file); +return -EINVAL ; +} +
[...]
Quoted 1 line
[]
Re: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
Quoted 30 lines · Binbin Wu
Quoted 30 lines
<snip>
virt/kvm/guest_mem.c new file +28
-0+static long kvm_gmem_allocate (struct inode *inode,loff_t offset,loff_t len) +{ +struct address_space *mapping = inode->i_mapping; +pgoff_t start, index, end; +int r; + +/* Dedicated guest is immutable by default. */ +if (offset + len >i_size_read (inode)) +return -EINVAL ; + +filemap_invalidate_lock_shared (mapping); + + start = offset >>PAGE_SHIFT ; + end = (offset + len) >>PAGE_SHIFT ; + + r =0 ; +for (index = start; index < end; ) { +struct folio *folio; + +if (signal_pending (current)) { + r = -EINTR ; +break ; + } + + folio =kvm_gmem_get_folio (inode, index); +if (!folio) { + r = -ENOMEM ; +break ;
Quoted 9 lines
virt/kvm/guest_mem.c line 156
Quoted 6 lines
virt/kvm/guest_mem.c new file +6
-0+ } + + index =folio_next_index (folio); + +folio_unlock (folio); +folio_put (folio);May be a dumb question, why we get the folio and then put it immediately? Will it make the folio be released back to the page allocator?
I was wondering this too, but it is correct.
In filemap_grab_folio(), the refcount is incremented in three places:
+ When the folio is created in filemap_alloc_folio(), it is given a refcount of 1 in
filemap_alloc_folio() -> folio_alloc() -> __folio_alloc_node() ->
__folio_alloc() -> __alloc_pages() -> get_page_from_freelist() ->
prep_new_page() -> post_alloc_hook() -> set_page_refcounted()+ Then, in filemap_add_folio(), the refcount is incremented twice:
+ The first is from the filemap (1 refcount per page if this is a
hugepage):
filemap_add_folio() -> __filemap_add_folio() -> folio_ref_add()
+ The second is a refcount from the lru list
filemap_add_folio() -> folio_add_lru() -> folio_get() ->
folio_ref_inc()In the other path, if the folio exists in the page cache (filemap), the refcount is also incremented through
filemap_grab_folio() -> __filemap_get_folio() -> filemap_get_entry()
-> folio_try_get_rcu()I believe all the branches in kvm_gmem_get_folio() are taking a refcount on the folio while the kernel does some work on the folio like clearing the folio in clear_highpage() or getting the next index, and then when done, the kernel does folio_put().
This pattern is also used in shmem and hugetlb. :)
I'm not sure whose refcount the folio_put() in kvm_gmem_allocate() is dropping though:
+ The refcount for the filemap depends on whether this is a hugepage or not, but folio_put() strictly drops a refcount of 1.
+ The refcount for the lru list is just 1, but doesn't the page still remain in the lru list?
Quoted 15 lines
Quoted 15 lines
virt/kvm/guest_mem.c new file +13
-0+ +/* 64-bit only, wrapping the index should be impossible. */ +if (WARN_ON_ONCE (!index)) +break ; + +cond_resched (); + } + +filemap_invalidate_unlock_shared (mapping); + +return r; +} +
Re: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
Quoted 70 lines · Ackerley Tng
Quoted 30 lines · Binbin Wu
Quoted 30 lines
<snip>
virt/kvm/guest_mem.c new file +28
-0+static long kvm_gmem_allocate (struct inode *inode,loff_t offset,loff_t len) +{ +struct address_space *mapping = inode->i_mapping; +pgoff_t start, index, end; +int r; + +/* Dedicated guest is immutable by default. */ +if (offset + len >i_size_read (inode)) +return -EINVAL ; + +filemap_invalidate_lock_shared (mapping); + + start = offset >>PAGE_SHIFT ; + end = (offset + len) >>PAGE_SHIFT ; + + r =0 ; +for (index = start; index < end; ) { +struct folio *folio; + +if (signal_pending (current)) { + r = -EINTR ; +break ; + } + + folio =kvm_gmem_get_folio (inode, index); +if (!folio) { + r = -ENOMEM ; +break ;Quoted 9 lines
virt/kvm/guest_mem.c line 156
Quoted 6 lines
virt/kvm/guest_mem.c new file +6
-0+ } + + index =folio_next_index (folio); + +folio_unlock (folio); +folio_put (folio);May be a dumb question, why we get the folio and then put it immediately? Will it make the folio be released back to the page allocator?
I was wondering this too, but it is correct.
In
filemap_grab_folio(), the refcount is incremented in three places:+ When the folio is created in
filemap_alloc_folio(), it is given a refcount of 1 infilemap_alloc_folio() -> folio_alloc() -> __folio_alloc_node() -> __folio_alloc() -> __alloc_pages() -> get_page_from_freelist() -> prep_new_page() -> post_alloc_hook() -> set_page_refcounted()+ Then, in
filemap_add_folio(), the refcount is incremented twice:+ The first is from the filemap (1 refcount per page if this is a hugepage): filemap_add_folio() -> __filemap_add_folio() -> folio_ref_add() + The second is a refcount from the lru list filemap_add_folio() -> folio_add_lru() -> folio_get() -> folio_ref_inc()In the other path, if the folio exists in the page cache (filemap), the refcount is also incremented through
filemap_grab_folio() -> __filemap_get_folio() -> filemap_get_entry() -> folio_try_get_rcu()
Quoted 6 lines
I believe all the branches in
kvm_gmem_get_folio()are taking a refcount on the folio while the kernel does some work on the folio like clearing the folio inclear_highpage()or getting the next index, and then when done, the kernel doesfolio_put().This pattern is also used in shmem and hugetlb. :)
Thanks for your explanation. It helps a lot.
Quoted 8 lines
I'm not sure whose refcount the
folio_put()inkvm_gmem_allocate()is dropping though:+ The refcount for the filemap depends on whether this is a hugepage or not, but
folio_put()strictly drops a refcount of 1.
+ The refcount for the lru list is just 1, but doesn't the page still remain in the lru list?
I guess the refcount drop here is the one get on the fresh allocation. Now the filemap has grabbed the folio, so the lifecycle of the folio now is decided by the filemap/inode?
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
virt/kvm/guest_mem.c new file +13
-0+ +/* 64-bit only, wrapping the index should be impossible. */ +if (WARN_ON_ONCE (!index)) +break ; + +cond_resched (); + } + +filemap_invalidate_unlock_shared (mapping); + +return r; +} +
Re: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory
Quoted 6 lines · Binbin Wu
<snip>
Quoted 4 lines
I'm not sure whose refcount the
folio_put()inkvm_gmem_allocate()is dropping though:
Quoted 9 lines
Quoted 4 lines
+ The refcount for the filemap depends on whether this is a hugepage or not, but
folio_put()strictly drops a refcount of 1.
+ The refcount for the lru list is just 1, but doesn't the page still remain in the lru list?I guess the refcount drop here is the one get on the fresh allocation. Now the filemap has grabbed the folio, so the lifecycle of the folio now is decided by the filemap/inode?
This makes sense! So folio_put() here is saying, I'm not using this folio anymore, but the filemap and the lru list are stil using the folio.
Quoted 1 line
<snip>
Sean Christopherson
maintainerRFC PATCH v11 13/29 KVM: Add transparent hugepage support for dedicated guest memory
In reply to Sean Christopherson--- include/uapi/linux/kvm.h | 2 ++ virt/kvm/guest_mem.c | 52 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 50 insertions(+), 4 deletions(-)
include/uapi/linux/kvm.h +2 -0
@@ -2290,6 +2290,8 @@ struct kvm_memory_attributes {#define KVM_CREATE_GUEST_MEMFD _IOWR(KVMIO, 0xd4, struct kvm_create_guest_memfd) +#define KVM_GUEST_MEMFD_ALLOW_HUGEPAGE (1ULL << 0) +struct kvm_create_guest_memfd {__u64 size;__u64 flags;
virt/kvm/guest_mem.c +48 -4
88 lines not on this page · raw message
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Paolo Bonzini
maintainerRe: [RFC PATCH v11 13/29] KVM: Add transparent hugepage support for dedicated guest memory
virt/kvm/guest_mem.c int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args) · line 459
Quoted 8 lines · Sean Christopherson
virt/kvm/guest_mem.c +3
-0@@ -413,6 +454,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_TRANSPARENT_HUGEPAGE )) + valid_flags |=KVM_GUEST_MEMFD_ALLOW_HUGEPAGE ; +
I think it should be always allowed. The outcome would just be "never have a hugepage" if thp is not enabled in the kernel.
Paolo
Sean Christopherson
maintainerRe: [RFC PATCH v11 13/29] KVM: Add transparent hugepage support for dedicated guest memory
Quoted 10 lines · Paolo Bonzini
virt/kvm/guest_mem.c int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args) · line 458
Quoted 6 lines · Sean Christopherson
virt/kvm/guest_mem.c +3
-0@@ -413,6 +454,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_TRANSPARENT_HUGEPAGE )) + valid_flags |=KVM_GUEST_MEMFD_ALLOW_HUGEPAGE ; +I think it should be always allowed. The outcome would just be "never have a hugepage" if thp is not enabled in the kernel.
I don't have a strong preference. My thinking was that userspace would probably rather have an explicit error, as opposed to silently running with a misconfigured setup.
Paolo Bonzini
maintainerRe: [RFC PATCH v11 13/29] KVM: Add transparent hugepage support for dedicated guest memory
Quoted 12 lines · On Fri, Jul 21, 2023 at 7:13?PM Sean Christopherson <[email protected]> wrote:
Quoted 10 lines · Paolo Bonzini
virt/kvm/guest_mem.c int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args) · line 458
Quoted 6 lines · Sean Christopherson
virt/kvm/guest_mem.c +3
-0@@ -413,6 +454,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_TRANSPARENT_HUGEPAGE )) + valid_flags |=KVM_GUEST_MEMFD_ALLOW_HUGEPAGE ; +I think it should be always allowed. The outcome would just be "never have a hugepage" if thp is not enabled in the kernel.
Quoted 3 lines
I don't have a strong preference. My thinking was that userspace would probably rather have an explicit error, as opposed to silently running with a misconfigured setup.
Considering that is how madvise(MADV_HUGEPAGE) behaves, your patch is good. I disagree but consistency is better.
Paolo
Sean Christopherson
maintainerRFC PATCH v11 14/29 KVM: x86/mmu: Handle page fault for private memory
In reply to Sean ChristophersonFrom: Chao Peng <[email protected]>
A KVM_MEM_PRIVATE memslot can include both fd-based private memory and hva-based shared memory. Architecture code (like TDX code) can tell whether the on-going fault is private or not. This patch adds a 'is_private' field to kvm_page_fault to indicate this and architecture code is expected to set it.
To handle page fault for such memslot, the handling logic is different depending on whether the fault is private or shared. KVM checks if 'is_private' matches the host's view of the page (maintained in mem_attr_array).
- For a successful match, private pfn is obtained with restrictedmem_get_page() and shared pfn is obtained with existing get_user_pages().
- For a failed match, KVM causes a KVM_EXIT_MEMORY_FAULT exit to userspace. Userspace then can convert memory between private/shared in host's view and retry the fault.
--- arch/x86/kvm/mmu/mmu.c | 82 +++++++++++++++++++++++++++++++-- arch/x86/kvm/mmu/mmu_internal.h | 3 ++ arch/x86/kvm/mmu/mmutrace.h | 1 + 3 files changed, 81 insertions(+), 5 deletions(-)
arch/x86/kvm/mmu/mmu.c +77 -5
@@ -3179,9 +3179,9 @@ static int host_pfn_mapping_level(struct kvm *kvm, gfn_t gfn,return level; } -int kvm_mmu_max_mapping_level (struct kvm *kvm, -const struct kvm_memory_slot *slot,gfn_t gfn, -int max_level) +static int __kvm_mmu_max_mapping_level (struct kvm *kvm, +const struct kvm_memory_slot *slot, +gfn_t gfn,int max_level,bool is_private) {struct kvm_lpage_info *linfo;int host_level;
@@ -3193,6 +3193,9 @@ int kvm_mmu_max_mapping_level(struct kvm *kvm,break ; } +if (is_private) +return max_level; +if (max_level ==PG_LEVEL_4K )return PG_LEVEL_4K ;
@@ -3200,6 +3203,16 @@ int kvm_mmu_max_mapping_level(struct kvm *kvm,return min (host_level, max_level); } +int kvm_mmu_max_mapping_level (struct kvm *kvm, +const struct kvm_memory_slot *slot,gfn_t gfn, +int max_level) +{ +bool is_private =kvm_slot_can_be_private (slot) && +kvm_mem_is_private (kvm, gfn); + +return __kvm_mmu_max_mapping_level (kvm, slot, gfn, max_level, is_private); +} +void kvm_mmu_hugepage_adjust (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) {struct kvm_memory_slot *slot = fault->slot;
@@ -3220,8 +3233,9 @@ void kvm_mmu_hugepage_adjust(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault* Enforce the iTLB multihit workaround after capturing the requested * level, which will be used to do precise, accurate accounting. */ - fault->req_level =kvm_mmu_max_mapping_level (vcpu->kvm, slot, - fault->gfn, fault->max_level); + fault->req_level =__kvm_mmu_max_mapping_level (vcpu->kvm, slot, + fault->gfn, fault->max_level, + fault->is_private);if (fault->req_level ==PG_LEVEL_4K || fault->huge_page_disallowed)return ;
@@ -4304,6 +4318,55 @@ void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)kvm_mmu_do_page_fault (vcpu, work->cr2_or_gpa,0 , true,NULL ); } +static inline u8 kvm_max_level_for_order (int order) +{ +BUILD_BUG_ON (KVM_MAX_HUGEPAGE_LEVEL >PG_LEVEL_1G ); + +MMU_WARN_ON (order !=KVM_HPAGE_GFN_SHIFT (PG_LEVEL_1G ) && + order !=KVM_HPAGE_GFN_SHIFT (PG_LEVEL_2M ) && + order !=KVM_HPAGE_GFN_SHIFT (PG_LEVEL_4K )); + +if (order >=KVM_HPAGE_GFN_SHIFT (PG_LEVEL_1G )) +return PG_LEVEL_1G ; + +if (order >=KVM_HPAGE_GFN_SHIFT (PG_LEVEL_2M )) +return PG_LEVEL_2M ; + +return PG_LEVEL_4K ; +} + +static int kvm_do_memory_fault_exit (struct kvm_vcpu *vcpu, +struct kvm_page_fault *fault) +{ + vcpu->run->exit_reason =KVM_EXIT_MEMORY_FAULT ; +if (fault->is_private) + vcpu->run->memory.flags =KVM_MEMORY_EXIT_FLAG_PRIVATE ; +else + vcpu->run->memory.flags =0 ; + vcpu->run->memory.gpa = fault->gfn <<PAGE_SHIFT ; + vcpu->run->memory.size =PAGE_SIZE ; +return RET_PF_USER ; +} + +static int kvm_faultin_pfn_private (struct kvm_vcpu *vcpu, +struct kvm_page_fault *fault) +{ +int max_order, r; + +if (!kvm_slot_can_be_private (fault->slot)) +return kvm_do_memory_fault_exit (vcpu, fault); + + r =kvm_gmem_get_pfn (vcpu->kvm, fault->slot, fault->gfn, &fault->pfn, + &max_order); +if (r) +return r; + + fault->max_level =min (kvm_max_level_for_order (max_order), + fault->max_level); + fault->map_writable = !(fault->slot->flags &KVM_MEM_READONLY ); +return RET_PF_CONTINUE ; +} +static int __kvm_faultin_pfn (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) {struct kvm_memory_slot *slot = fault->slot;
@@ -4336,6 +4399,12 @@ static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *faultreturn RET_PF_EMULATE ; } +if (fault->is_private !=kvm_mem_is_private (vcpu->kvm, fault->gfn)) +return kvm_do_memory_fault_exit (vcpu, fault); + +if (fault->is_private) +return kvm_faultin_pfn_private (vcpu, fault); + async = false; fault->pfn = __gfn_to_pfn_memslot(slot, fault->gfn, false, false, &async, fault->write, &fault->map_writable,
@@ -5771,6 +5840,9 @@ int noinline kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 errreturn -EIO ; } +if (r ==RET_PF_USER ) +return 0 ; +if (r <0 )return r;if (r !=RET_PF_EMULATE )
arch/x86/kvm/mmu/mmu_internal.h +3 -0
@@ -203,6 +203,7 @@ struct kvm_page_fault {/* Derived from mmu and global state. */ const bool is_tdp; +const bool is_private;const bool nx_huge_page_workaround_enabled;/*
@@ -259,6 +260,7 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault);* RET_PF_RETRY: let CPU fault again on the address. * RET_PF_EMULATE: mmio page fault, emulate the instruction directly. * RET_PF_INVALID: the spte is invalid, let the real page fault path update it. +* RET_PF_USER: need to exit to userspace to handle this fault. * RET_PF_FIXED: The faulting entry has been fixed. * RET_PF_SPURIOUS: The faulting entry was already fixed, e.g. by another vCPU. *
@@ -275,6 +277,7 @@ enum {RET_PF_RETRY ,RET_PF_EMULATE ,RET_PF_INVALID , +RET_PF_USER ,RET_PF_FIXED , RET_PF_SPURIOUS, };
arch/x86/kvm/mmu/mmutrace.h +1 -0
@@ -58,6 +58,7 @@ TRACE_DEFINE_ENUM(RET_PF_CONTINUE);TRACE_DEFINE_ENUM (RET_PF_RETRY );TRACE_DEFINE_ENUM (RET_PF_EMULATE );TRACE_DEFINE_ENUM (RET_PF_INVALID ); +TRACE_DEFINE_ENUM (RET_PF_USER );TRACE_DEFINE_ENUM (RET_PF_FIXED );TRACE_DEFINE_ENUM (RET_PF_SPURIOUS );
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Re: [RFC PATCH v11 14/29] KVM: x86/mmu: Handle page fault for private memory
Quoted 199 lines · Sean Christopherson
From: Chao Peng <[email protected]>
A
KVM_MEM_PRIVATEmemslot can include both fd-based private memory and hva-based shared memory. Architecture code (like TDX code) can tell whether the on-going fault is private or not. This patch adds a 'is_private' field tokvm_page_faultto indicate this and architecture code is expected to set it.To handle page fault for such memslot, the handling logic is different depending on whether the fault is private or shared. KVM checks if '
is_private' matches the host's view of the page (maintained inmem_attr_array).
- For a successful match, private pfn is obtained withrestrictedmem_get_page()and shared pfn is obtained with existingget_user_pages().
- For a failed match, KVM causes aKVM_EXIT_MEMORY_FAULTexit to userspace. Userspace then can convert memory between private/shared in host's view and retry the fault.Co-developed-by Yu Zhang <[email protected]>Signed-off-by Yu Zhang <[email protected]>Signed-off-by Chao Peng <[email protected]>Reviewed-by Fuad Tabba <[email protected]>Tested-by Fuad Tabba <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/mmu/mmu.c | 82 +++++++++++++++++++++++++++++++-- arch/x86/kvm/mmu/mmu_internal.h | 3 ++ arch/x86/kvm/mmu/mmutrace.h | 1 + 3 files changed, 81 insertions(+), 5 deletions(-)arch/x86/kvm/mmu/mmu.c +77
-5@@ -3179,9 +3179,9 @@ static int host_pfn_mapping_level(struct kvm *kvm, gfn_t gfn,return level; } -int kvm_mmu_max_mapping_level (struct kvm *kvm, -const struct kvm_memory_slot *slot,gfn_t gfn, -int max_level) +static int __kvm_mmu_max_mapping_level (struct kvm *kvm, +const struct kvm_memory_slot *slot, +gfn_t gfn,int max_level,bool is_private) {struct kvm_lpage_info *linfo;int host_level;@@ -3193,6 +3193,9 @@ int kvm_mmu_max_mapping_level(struct kvm *kvm,break ; } +if (is_private) +return max_level; +if (max_level ==PG_LEVEL_4K )return PG_LEVEL_4K ;@@ -3200,6 +3203,16 @@ int kvm_mmu_max_mapping_level(struct kvm *kvm,return min (host_level, max_level); } +int kvm_mmu_max_mapping_level (struct kvm *kvm, +const struct kvm_memory_slot *slot,gfn_t gfn, +int max_level) +{ +bool is_private =kvm_slot_can_be_private (slot) && +kvm_mem_is_private (kvm, gfn); + +return __kvm_mmu_max_mapping_level (kvm, slot, gfn, max_level, is_private); +} +void kvm_mmu_hugepage_adjust (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) {struct kvm_memory_slot *slot = fault->slot;@@ -3220,8 +3233,9 @@ void kvm_mmu_hugepage_adjust(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault* Enforce the iTLB multihit workaround after capturing the requested * level, which will be used to do precise, accurate accounting. */ - fault->req_level =kvm_mmu_max_mapping_level (vcpu->kvm, slot, - fault->gfn, fault->max_level); + fault->req_level =__kvm_mmu_max_mapping_level (vcpu->kvm, slot, + fault->gfn, fault->max_level, + fault->is_private);if (fault->req_level ==PG_LEVEL_4K || fault->huge_page_disallowed)return ;@@ -4304,6 +4318,55 @@ void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)kvm_mmu_do_page_fault (vcpu, work->cr2_or_gpa,0 , true,NULL ); } +static inline u8 kvm_max_level_for_order (int order) +{ +BUILD_BUG_ON (KVM_MAX_HUGEPAGE_LEVEL >PG_LEVEL_1G ); + +MMU_WARN_ON (order !=KVM_HPAGE_GFN_SHIFT (PG_LEVEL_1G ) && + order !=KVM_HPAGE_GFN_SHIFT (PG_LEVEL_2M ) && + order !=KVM_HPAGE_GFN_SHIFT (PG_LEVEL_4K )); + +if (order >=KVM_HPAGE_GFN_SHIFT (PG_LEVEL_1G )) +return PG_LEVEL_1G ; + +if (order >=KVM_HPAGE_GFN_SHIFT (PG_LEVEL_2M )) +return PG_LEVEL_2M ; + +return PG_LEVEL_4K ; +} + +static int kvm_do_memory_fault_exit (struct kvm_vcpu *vcpu, +struct kvm_page_fault *fault) +{ + vcpu->run->exit_reason =KVM_EXIT_MEMORY_FAULT ; +if (fault->is_private) + vcpu->run->memory.flags =KVM_MEMORY_EXIT_FLAG_PRIVATE ; +else + vcpu->run->memory.flags =0 ; + vcpu->run->memory.gpa = fault->gfn <<PAGE_SHIFT ; + vcpu->run->memory.size =PAGE_SIZE ; +return RET_PF_USER ; +} + +static int kvm_faultin_pfn_private (struct kvm_vcpu *vcpu, +struct kvm_page_fault *fault) +{ +int max_order, r; + +if (!kvm_slot_can_be_private (fault->slot)) +return kvm_do_memory_fault_exit (vcpu, fault); + + r =kvm_gmem_get_pfn (vcpu->kvm, fault->slot, fault->gfn, &fault->pfn, + &max_order); +if (r) +return r; + + fault->max_level =min (kvm_max_level_for_order (max_order), + fault->max_level); + fault->map_writable = !(fault->slot->flags &KVM_MEM_READONLY ); +return RET_PF_CONTINUE ; +} +static int __kvm_faultin_pfn (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) {struct kvm_memory_slot *slot = fault->slot;@@ -4336,6 +4399,12 @@ static int __kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *faultreturn RET_PF_EMULATE ; } +if (fault->is_private !=kvm_mem_is_private (vcpu->kvm, fault->gfn)) +return kvm_do_memory_fault_exit (vcpu, fault); + +if (fault->is_private) +return kvm_faultin_pfn_private (vcpu, fault); + async = false; fault->pfn = __gfn_to_pfn_memslot(slot, fault->gfn, false, false, &async, fault->write, &fault->map_writable,@@ -5771,6 +5840,9 @@ int noinline kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 errreturn -EIO ; } +if (r ==RET_PF_USER ) +return 0 ; +if (r <0 )return r;if (r !=RET_PF_EMULATE )arch/x86/kvm/mmu/mmu_internal.h +3
-0@@ -203,6 +203,7 @@ struct kvm_page_fault {/* Derived from mmu and global state. */ const bool is_tdp; +const bool is_private;const bool nx_huge_page_workaround_enabled;/* @@ -259,6 +260,7 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault);* RET_PF_RETRY: let CPU fault again on the address. * RET_PF_EMULATE: mmio page fault, emulate the instruction directly. * RET_PF_INVALID: the spte is invalid, let the real page fault path update it. +* RET_PF_USER: need to exit to userspace to handle this fault. * RET_PF_FIXED: The faulting entry has been fixed. * RET_PF_SPURIOUS: The faulting entry was already fixed, e.g. by another vCPU. * @@ -275,6 +277,7 @@ enum {RET_PF_RETRY ,RET_PF_EMULATE ,RET_PF_INVALID , +RET_PF_USER ,RET_PF_FIXED , RET_PF_SPURIOUS, };arch/x86/kvm/mmu/mmutrace.h +0
-0
arch/x86/kvm/mmu/mmutrace.h TRACE_DEFINE_ENUM(RET_PF_CONTINUE); · line 64
Quoted 7 lines
arch/x86/kvm/mmu/mmutrace.h +1
-0@@ -58,6 +58,7 @@ TRACE_DEFINE_ENUM(RET_PF_CONTINUE);TRACE_DEFINE_ENUM (RET_PF_RETRY );TRACE_DEFINE_ENUM (RET_PF_EMULATE );TRACE_DEFINE_ENUM (RET_PF_INVALID ); +TRACE_DEFINE_ENUM (RET_PF_USER );TRACE_DEFINE_ENUM (RET_PF_FIXED );TRACE_DEFINE_ENUM (RET_PF_SPURIOUS );
Sean Christopherson
maintainerRFC PATCH v11 15/29 KVM: Drop superfluous __KVM_VCPU_MULTIPLE_ADDRESS_SPACE macro
In reply to Sean Christopherson--- arch/x86/include/asm/kvm_host.h | 1 - include/linux/kvm_host.h | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -1
@@ -2105,7 +2105,6 @@ enum {#define HF_SMM_MASK (1 << 1)#define HF_SMM_INSIDE_NMI_MASK (1 << 2) -# define __KVM_VCPU_MULTIPLE_ADDRESS_SPACE# define KVM_ADDRESS_SPACE_NUM 2# define kvm_arch_vcpu_memslots_id (vcpu) ((vcpu)->arch.hflags & HF_SMM_MASK ? 1 : 0)# define kvm_memslots_for_spte_role (kvm, role) __kvm_memslots(kvm, (role).smm)
include/linux/kvm_host.h +1 -1
@@ -693,7 +693,7 @@ bool kvm_arch_irqchip_in_kernel(struct kvm *kvm);#define KVM_MEM_SLOTS_NUM SHRT_MAX#define KVM_USER_MEM_SLOTS (KVM_MEM_SLOTS_NUM - KVM_INTERNAL_MEM_SLOTS) -#ifndef __KVM_VCPU_MULTIPLE_ADDRESS_SPACE +#if KVM_ADDRESS_SPACE_NUM ==1 static inline int kvm_arch_vcpu_memslots_id (struct kvm_vcpu *vcpu) {return 0 ;
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Re: [RFC PATCH v11 15/29] KVM: Drop superfluous __KVM_VCPU_MULTIPLE_ADDRESS_SPACE macro
Quoted 23 lines · Sean Christopherson
Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/include/asm/kvm_host.h | 1 - include/linux/kvm_host.h | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-)arch/x86/include/asm/kvm_host.h +0
-1@@ -2105,7 +2105,6 @@ enum {#define HF_SMM_MASK (1 << 1)#define HF_SMM_INSIDE_NMI_MASK (1 << 2) -# define __KVM_VCPU_MULTIPLE_ADDRESS_SPACE# define KVM_ADDRESS_SPACE_NUM 2# define kvm_arch_vcpu_memslots_id (vcpu) ((vcpu)->arch.hflags & HF_SMM_MASK ? 1 : 0)# define kvm_memslots_for_spte_role (kvm, role) __kvm_memslots(kvm, (role).smm)include/linux/kvm_host.h +0
-0
include/linux/kvm_host.h bool kvm_arch_irqchip_in_kernel(struct kvm *kvm); · line 699
Quoted 8 lines
include/linux/kvm_host.h +1
-1@@ -693,7 +693,7 @@ bool kvm_arch_irqchip_in_kernel(struct kvm *kvm);#define KVM_MEM_SLOTS_NUM SHRT_MAX#define KVM_USER_MEM_SLOTS (KVM_MEM_SLOTS_NUM - KVM_INTERNAL_MEM_SLOTS) -#ifndef __KVM_VCPU_MULTIPLE_ADDRESS_SPACE +#if KVM_ADDRESS_SPACE_NUM ==1 static inline int kvm_arch_vcpu_memslots_id (struct kvm_vcpu *vcpu) {return 0 ;
Sean Christopherson
maintainerRFC PATCH v11 16/29 KVM: Allow arch code to track number of memslot address spaces per VM
In reply to Sean Christopherson--- arch/powerpc/kvm/book3s_hv.c | 2 +- arch/x86/include/asm/kvm_host.h | 8 +++++++- arch/x86/kvm/debugfs.c | 2 +- arch/x86/kvm/mmu/mmu.c | 8 ++++---- arch/x86/kvm/mmu/tdp_mmu.c | 2 +- arch/x86/kvm/x86.c | 2 +- include/linux/kvm_host.h | 17 +++++++++++------ virt/kvm/dirty_ring.c | 2 +- virt/kvm/kvm_main.c | 24 ++++++++++++------------ 9 files changed, 39 insertions(+), 28 deletions(-)
arch/powerpc/kvm/book3s_hv.c +1 -1
@@ -6084,7 +6084,7 @@ static int kvmhv_svm_off(struct kvm *kvm) } srcu_idx =srcu_read_lock (&kvm->srcu); -for (i =0 ; i <KVM_ADDRESS_SPACE_NUM ; i++) { +for (i =0 ; i <kvm_arch_nr_memslot_as_ids (kvm); i++) {struct kvm_memory_slot *memslot;struct kvm_memslots *slots =__kvm_memslots (kvm, i);int bkt;
arch/x86/include/asm/kvm_host.h +7 -1
@@ -2105,9 +2105,15 @@ enum {#define HF_SMM_MASK (1 << 1)#define HF_SMM_INSIDE_NMI_MASK (1 << 2) -# define KVM_ADDRESS_SPACE_NUM 2 +# define KVM_MAX_NR_ADDRESS_SPACES 2# define kvm_arch_vcpu_memslots_id (vcpu) ((vcpu)->arch.hflags & HF_SMM_MASK ? 1 : 0)# define kvm_memslots_for_spte_role (kvm, role) __kvm_memslots(kvm, (role).smm) + +static inline int kvm_arch_nr_memslot_as_ids (struct kvm *kvm) +{ +return KVM_MAX_NR_ADDRESS_SPACES ; +} +#else # define kvm_memslots_for_spte_role (kvm, role) __kvm_memslots(kvm, 0)#endif
arch/x86/kvm/debugfs.c +1 -1
@@ -111,7 +111,7 @@ static int kvm_mmu_rmaps_stat_show(struct seq_file *m, void *v)mutex_lock (&kvm->slots_lock);write_lock (&kvm->mmu_lock); -for (i =0 ; i <KVM_ADDRESS_SPACE_NUM ; i++) { +for (i =0 ; i <kvm_arch_nr_memslot_as_ids (kvm); i++) {int bkt; slots =__kvm_memslots (kvm, i);
arch/x86/kvm/mmu/mmu.c +4 -4
@@ -3801,7 +3801,7 @@ static int mmu_first_shadow_root_alloc(struct kvm *kvm) kvm_page_track_write_tracking_enabled(kvm)) goto out_success; -for (i =0 ; i <KVM_ADDRESS_SPACE_NUM ; i++) { +for (i =0 ; i <kvm_arch_nr_memslot_as_ids (kvm); i++) { slots =__kvm_memslots (kvm, i);kvm_for_each_memslot (slot, bkt, slots) {/*
@@ -6351,7 +6351,7 @@ static bool kvm_rmap_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_eif (!kvm_memslots_have_rmaps (kvm))return flush; -for (i =0 ; i <KVM_ADDRESS_SPACE_NUM ; i++) { +for (i =0 ; i <kvm_arch_nr_memslot_as_ids (kvm); i++) { slots =__kvm_memslots (kvm, i);kvm_for_each_memslot_in_gfn_range (&iter, slots, gfn_start, gfn_end) {
@@ -6391,7 +6391,7 @@ void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end) flush =kvm_rmap_zap_gfn_range (kvm, gfn_start, gfn_end);if (tdp_mmu_enabled) { -for (i =0 ; i <KVM_ADDRESS_SPACE_NUM ; i++) +for (i =0 ; i <kvm_arch_nr_memslot_as_ids (kvm); i++) flush =kvm_tdp_mmu_zap_leafs (kvm, i, gfn_start, gfn_end, true, flush); }
@@ -6855,7 +6855,7 @@ void kvm_mmu_invalidate_mmio_sptes(struct kvm *kvm, u64 gen)* modifier prior to checking for a wrap of the MMIO generation so * that a wrap in any address space is detected. */ - gen &= ~((u64 )KVM_ADDRESS_SPACE_NUM -1 ); + gen &= ~((u64 )kvm_arch_nr_memslot_as_ids (kvm) -1 );/* * The very rare case: if the MMIO generation number has wrapped,
arch/x86/kvm/mmu/tdp_mmu.c +1 -1
@@ -905,7 +905,7 @@ void kvm_tdp_mmu_zap_all(struct kvm *kvm)* is being destroyed or the userspace VMM has exited. In both cases, * KVM_RUN is unreachable, i.e. no vCPUs will ever service the request. */ -for (i =0 ; i <KVM_ADDRESS_SPACE_NUM ; i++) { +for (i =0 ; i <kvm_arch_nr_memslot_as_ids (kvm); i++) {for_each_tdp_mmu_root_yield_safe (kvm, root, i)tdp_mmu_zap_root (kvm, root, false); }
arch/x86/kvm/x86.c +1 -1
@@ -12419,7 +12419,7 @@ void __user * __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, hva = slot->userspace_addr; } -for (i =0 ; i <KVM_ADDRESS_SPACE_NUM ; i++) { +for (i =0 ; i <kvm_arch_nr_memslot_as_ids (kvm); i++) {struct kvm_userspace_memory_region2 m; m.slot = id | (i <<16 );
include/linux/kvm_host.h +11 -6
@@ -80,8 +80,8 @@/* Two fragments for cross MMIO pages. */ #define KVM_MAX_MMIO_FRAGMENTS 2 -#ifndef KVM_ADDRESS_SPACE_NUM -#define KVM_ADDRESS_SPACE_NUM 1 +#ifndef KVM_MAX_NR_ADDRESS_SPACES +#define KVM_MAX_NR_ADDRESS_SPACES 1#endif /*
@@ -693,7 +693,12 @@ bool kvm_arch_irqchip_in_kernel(struct kvm *kvm);#define KVM_MEM_SLOTS_NUM SHRT_MAX#define KVM_USER_MEM_SLOTS (KVM_MEM_SLOTS_NUM - KVM_INTERNAL_MEM_SLOTS) -#if KVM_ADDRESS_SPACE_NUM ==1 +#if KVM_MAX_NR_ADDRESS_SPACES ==1 +static inline int kvm_arch_nr_memslot_as_ids (struct kvm *kvm) +{ +return KVM_MAX_NR_ADDRESS_SPACES ; +} +static inline int kvm_arch_vcpu_memslots_id (struct kvm_vcpu *vcpu) {return 0 ;
@@ -748,9 +753,9 @@ struct kvm {struct mm_struct *mm;/* userspace tied to this vm */ unsigned long nr_memslot_pages;/* The two memslot sets - active and inactive (per address space) */ -struct kvm_memslots __memslots[KVM_ADDRESS_SPACE_NUM ][2 ]; +struct kvm_memslots __memslots[KVM_MAX_NR_ADDRESS_SPACES ][2 ];/* The current active memslot set for each address space */ -struct kvm_memslots __rcu *memslots[KVM_ADDRESS_SPACE_NUM ]; +struct kvm_memslots __rcu *memslots[KVM_MAX_NR_ADDRESS_SPACES ];struct xarray vcpu_array;/* * Protected by slots_lock, but can be read outside if an
@@ -1000,7 +1005,7 @@ void kvm_put_kvm_no_destroy(struct kvm *kvm);static inline struct kvm_memslots *__kvm_memslots (struct kvm *kvm,int as_id) { - as_id =array_index_nospec (as_id,KVM_ADDRESS_SPACE_NUM ); + as_id =array_index_nospec (as_id,KVM_MAX_NR_ADDRESS_SPACES );return srcu_dereference_check (kvm->memslots[as_id], &kvm->srcu,lockdep_is_held (&kvm->slots_lock) || !refcount_read (&kvm->users_count));
virt/kvm/dirty_ring.c +1 -1
@@ -58,7 +58,7 @@ static void kvm_reset_dirty_gfn(struct kvm *kvm, u32 slot, u64 offset, u64 mask) as_id = slot >>16 ; id = (u16 )slot; -if (as_id >=KVM_ADDRESS_SPACE_NUM || id >=KVM_USER_MEM_SLOTS ) +if (as_id >=kvm_arch_nr_memslot_as_ids (kvm) || id >=KVM_USER_MEM_SLOTS )return ; memslot =id_to_memslot (__kvm_memslots (kvm, as_id), id);
virt/kvm/kvm_main.c +12 -12
@@ -582,7 +582,7 @@ static __always_inline int __kvm_handle_hva_range(struct kvm *kvm, idx =srcu_read_lock (&kvm->srcu); -for (i =0 ; i <KVM_ADDRESS_SPACE_NUM ; i++) { +for (i =0 ; i <kvm_arch_nr_memslot_as_ids (kvm); i++) {struct interval_tree_node *node; slots =__kvm_memslots (kvm, i);
@@ -1206,7 +1206,7 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname) goto out_err_no_irq_srcu;refcount_set (&kvm->users_count,1 ); -for (i =0 ; i <KVM_ADDRESS_SPACE_NUM ; i++) { +for (i =0 ; i <kvm_arch_nr_memslot_as_ids (kvm); i++) {for (j =0 ; j <2 ; j++) { slots = &kvm->__memslots[i][j];
@@ -1349,7 +1349,7 @@ static void kvm_destroy_vm(struct kvm *kvm)#endif kvm_arch_destroy_vm (kvm);kvm_destroy_devices (kvm); -for (i =0 ; i <KVM_ADDRESS_SPACE_NUM ; i++) { +for (i =0 ; i <kvm_arch_nr_memslot_as_ids (kvm); i++) {kvm_free_memslots (kvm, &kvm->__memslots[i][0 ]);kvm_free_memslots (kvm, &kvm->__memslots[i][1 ]); }
@@ -1632,7 +1632,7 @@ static void kvm_swap_active_memslots(struct kvm *kvm, int as_id)* space 0 will use generations 0, 2, 4, ... while address space 1 will * use generations 1, 3, 5, ... */ - gen +=KVM_ADDRESS_SPACE_NUM ; + gen +=kvm_arch_nr_memslot_as_ids (kvm);kvm_arch_memslots_updated (kvm, gen);
@@ -2002,7 +2002,7 @@ int __kvm_set_memory_region(struct kvm *kvm, (mem->gmem_offset & (PAGE_SIZE -1 ) || mem->gmem_offset + mem->memory_size < mem->gmem_offset)) return -EINVAL; -if (as_id >=KVM_ADDRESS_SPACE_NUM || id >=KVM_MEM_SLOTS_NUM ) +if (as_id >=kvm_arch_nr_memslot_as_ids (kvm) || id >=KVM_MEM_SLOTS_NUM )return -EINVAL ;if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)return -EINVAL ;
@@ -2138,7 +2138,7 @@ int kvm_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log, as_id = log->slot >>16 ; id = (u16 )log->slot; -if (as_id >=KVM_ADDRESS_SPACE_NUM || id >=KVM_USER_MEM_SLOTS ) +if (as_id >=kvm_arch_nr_memslot_as_ids (kvm) || id >=KVM_USER_MEM_SLOTS )return -EINVAL ; slots =__kvm_memslots (kvm, as_id);
@@ -2200,7 +2200,7 @@ static int kvm_get_dirty_log_protect(struct kvm *kvm, struct kvm_dirty_log *log) as_id = log->slot >>16 ; id = (u16 )log->slot; -if (as_id >=KVM_ADDRESS_SPACE_NUM || id >=KVM_USER_MEM_SLOTS ) +if (as_id >=kvm_arch_nr_memslot_as_ids (kvm) || id >=KVM_USER_MEM_SLOTS )return -EINVAL ; slots =__kvm_memslots (kvm, as_id);
@@ -2312,7 +2312,7 @@ static int kvm_clear_dirty_log_protect(struct kvm *kvm, as_id = log->slot >>16 ; id = (u16 )log->slot; -if (as_id >=KVM_ADDRESS_SPACE_NUM || id >=KVM_USER_MEM_SLOTS ) +if (as_id >=kvm_arch_nr_memslot_as_ids (kvm) || id >=KVM_USER_MEM_SLOTS )return -EINVAL ;if (log->first_page &63 )
@@ -2406,7 +2406,7 @@ static __always_inline void kvm_handle_gfn_range(struct kvm *kvm, gfn_range.arg.raw = range->arg.raw; gfn_range.may_block = range->may_block; -for (i =0 ; i <KVM_ADDRESS_SPACE_NUM ; i++) { +for (i =0 ; i <kvm_arch_nr_memslot_as_ids (kvm); i++) { slots =__kvm_memslots (kvm, i);kvm_for_each_memslot_in_gfn_range (&iter, slots, range->start, range->end) {
@@ -4725,9 +4725,9 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)case KVM_CAP_IRQ_ROUTING :return KVM_MAX_IRQ_ROUTES ;#endif -#if KVM_ADDRESS_SPACE_NUM >1 +#if KVM_MAX_NR_ADDRESS_SPACES >1 case KVM_CAP_MULTI_ADDRESS_SPACE : -return KVM_ADDRESS_SPACE_NUM ; +return KVM_MAX_NR_ADDRESS_SPACES ;#endif case KVM_CAP_NR_MEMSLOTS :return KVM_USER_MEM_SLOTS ;
@@ -4827,7 +4827,7 @@ bool kvm_are_all_memslots_empty(struct kvm *kvm)lockdep_assert_held (&kvm->slots_lock); -for (i =0 ; i <KVM_ADDRESS_SPACE_NUM ; i++) { +for (i =0 ; i <kvm_arch_nr_memslot_as_ids (kvm); i++) {if (!kvm_memslots_empty (__kvm_memslots (kvm, i)))return false; }
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Paolo Bonzini
maintainerRe: [RFC PATCH v11 16/29] KVM: Allow arch code to track number of memslot address spaces per VM
virt/kvm/kvm_main.c static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg) · line 4731
Quoted 10 lines · Sean Christopherson
virt/kvm/kvm_main.c +2
-2@@ -4725,9 +4725,9 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)case KVM_CAP_IRQ_ROUTING :return KVM_MAX_IRQ_ROUTES ;#endif -#if KVM_ADDRESS_SPACE_NUM >1 +#if KVM_MAX_NR_ADDRESS_SPACES >1 case KVM_CAP_MULTI_ADDRESS_SPACE : -return KVM_ADDRESS_SPACE_NUM ; +return KVM_MAX_NR_ADDRESS_SPACES ;#endif
Since this is a VM ioctl, it should returnkvm_arch_nr_memslot_as_ids(kvm) if kvm != NULL.
Paolo
Sean Christopherson
maintainerRFC PATCH v11 17/29 KVM: x86: Add support for "protected VMs" that can utilize private memory
In reply to Sean Christopherson--- Documentation/virt/kvm/api.rst | 32 ++++++++++++++++++++++++++++++++ arch/x86/include/asm/kvm_host.h | 15 +++++++++------ arch/x86/include/uapi/asm/kvm.h | 3 +++ arch/x86/kvm/Kconfig | 12 ++++++++++++ arch/x86/kvm/mmu/mmu_internal.h | 1 + arch/x86/kvm/x86.c | 16 +++++++++++++++- include/uapi/linux/kvm.h | 1 + virt/kvm/Kconfig | 5 +++++ 8 files changed, 78 insertions(+), 7 deletions(-)
Documentation/virt/kvm/api.rst +32 -0
@@ -147,10 +147,29 @@ described as 'basic' will be available. The new VM has no virtual cpus and no memory. You probably want to use 0 as machine type. +X86: +^^^^ + +Supported X86 VM types can be queried via KVM_CAP_VM_TYPES. + +S390: +^^^^^ + In order to create user controlled virtual machines on S390, check KVM_CAP_S390_UCONTROL and use the flag KVM_VM_S390_UCONTROL as privileged user (CAP_SYS_ADMIN). +MIPS: +^^^^^ + +To use hardware assisted virtualization on MIPS (VZ ASE) rather than +the default trap & emulate implementation (which changes the virtual +memory layout to fit in user mode), check KVM_CAP_MIPS_VZ and use the +flag KVM_VM_MIPS_VZ. + +ARM64: +^^^^^^ + On arm64, the physical address size for a VM (IPA Size limit) is limited to 40bits by default. The limit can be configured if the host supports the extension KVM_CAP_ARM_VM_IPA_SIZE. When supported, use
@@ -8554,6 +8573,19 @@ block sizes is exposed in KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES as a This capability indicates KVM supports per-page memory attributes and ioctls KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES/KVM_SET_MEMORY_ATTRIBUTES are available. +8.41 KVM_CAP_VM_TYPES +--------------------- + +:Capability: KVM_CAP_MEMORY_ATTRIBUTES +:Architectures: x86 +:Type: system ioctl + +This capability returns a bitmap of support VM types. The 1-setting of bit @n +means the VM type with value @n is supported. Possible values of @n are:: + + #define KVM_X86_DEFAULT_VM 0 + #define KVM_X86_SW_PROTECTED_VM 1 + 9. Known KVM API problems =========================
arch/x86/include/asm/kvm_host.h +9 -6
@@ -1227,6 +1227,7 @@ enum kvm_apicv_inhibit { };struct kvm_arch { +unsigned long vm_type;unsigned long n_used_mmu_pages;unsigned long n_requested_mmu_pages;unsigned long n_max_mmu_pages;
@@ -2058,6 +2059,12 @@ void kvm_mmu_new_pgd(struct kvm_vcpu *vcpu, gpa_t new_pgd);void kvm_configure_mmu (bool enable_tdp,int tdp_forced_root_level,int tdp_max_root_level,int tdp_huge_page_level); +#ifdef CONFIG_KVM_PRIVATE_MEM +#define kvm_arch_has_private_mem (kvm) ((kvm)->arch.vm_type != KVM_X86_DEFAULT_VM) +#else +#define kvm_arch_has_private_mem (kvm) false +#endif +static inline u16 kvm_read_ldt (void ) {u16 ldt;
@@ -2106,14 +2113,10 @@ enum {#define HF_SMM_INSIDE_NMI_MASK (1 << 2)# define KVM_MAX_NR_ADDRESS_SPACES 2 +/* SMM is currently unsupported for guests with private memory. */ +# define kvm_arch_nr_memslot_as_ids (kvm) (kvm_arch_has_private_mem(kvm) ? 1 : 2)# define kvm_arch_vcpu_memslots_id (vcpu) ((vcpu)->arch.hflags & HF_SMM_MASK ? 1 : 0)# define kvm_memslots_for_spte_role (kvm, role) __kvm_memslots(kvm, (role).smm) - -static inline int kvm_arch_nr_memslot_as_ids (struct kvm *kvm) -{ -return KVM_MAX_NR_ADDRESS_SPACES ; -} -#else # define kvm_memslots_for_spte_role (kvm, role) __kvm_memslots(kvm, 0)#endif
arch/x86/include/uapi/asm/kvm.h +3 -0
@@ -562,4 +562,7 @@ struct kvm_pmu_event_filter {/* x86-specific KVM_EXIT_HYPERCALL flags. */ #define KVM_EXIT_HYPERCALL_LONG_MODE BIT(0) +#define KVM_X86_DEFAULT_VM 0 +#define KVM_X86_SW_PROTECTED_VM 1 +#endif /* _ASM_X86_KVM_H */
arch/x86/kvm/Kconfig +12 -0
@@ -77,6 +77,18 @@ config KVM_WERROR If in doubt, say "N". +config KVM_SW_PROTECTED_VM + bool "Enable support for KVM software-protected VMs" + depends on EXPERT + depends on X86_64 + select KVM_GENERIC_PRIVATE_MEM + help + Enable support for KVM software-protected VMs. Currently "protected" + means the VM can be backed with memory provided by + KVM_CREATE_GUEST_MEMFD. + + If unsure, say "N". + config KVM_INTEL tristate "KVM for Intel (and compatible) processors support" depends on KVM && IA32_FEAT_CTL
arch/x86/kvm/mmu/mmu_internal.h +1 -0
@@ -301,6 +301,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .max_level = KVM_MAX_HUGEPAGE_LEVEL, .req_level = PG_LEVEL_4K, .goal_level = PG_LEVEL_4K, + .is_private = kvm_mem_is_private(vcpu->kvm, cr2_or_gpa >> PAGE_SHIFT), };int r;
arch/x86/kvm/x86.c +15 -1
@@ -4427,6 +4427,13 @@ static int kvm_ioctl_get_supported_hv_cpuid(struct kvm_vcpu *vcpu,return 0 ; } +static bool kvm_is_vm_type_supported (unsigned long type) +{ +return type ==KVM_X86_DEFAULT_VM || + (type ==KVM_X86_SW_PROTECTED_VM && +IS_ENABLED (CONFIG_KVM_SW_PROTECTED_VM ) && tdp_enabled); +} +int kvm_vm_ioctl_check_extension (struct kvm *kvm,long ext) {int r =0 ;
@@ -4617,6 +4624,11 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_X86_NOTIFY_VMEXIT : r = kvm_caps.has_notify_vmexit;break ; +case KVM_CAP_VM_TYPES : + r =BIT (KVM_X86_DEFAULT_VM ); +if (kvm_is_vm_type_supported (KVM_X86_SW_PROTECTED_VM )) + r |=BIT (KVM_X86_SW_PROTECTED_VM ); +break ;default :break ; }
@@ -12274,9 +12286,11 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)int ret;unsigned long flags; -if (type) +if (!kvm_is_vm_type_supported (type))return -EINVAL ; + kvm->arch.vm_type = type; + ret =kvm_page_track_init (kvm);if (ret) goto out;
include/uapi/linux/kvm.h +1 -0
@@ -1216,6 +1216,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES 229#define KVM_CAP_USER_MEMORY2 230#define KVM_CAP_MEMORY_ATTRIBUTES 231 +#define KVM_CAP_VM_TYPES 232#ifdef KVM_CAP_IRQ_ROUTING
virt/kvm/Kconfig +5 -0
@@ -107,3 +107,8 @@ config KVM_GENERIC_MEMORY_ATTRIBUTES config KVM_PRIVATE_MEM select XARRAY_MULTI bool + +config KVM_GENERIC_PRIVATE_MEM + select KVM_GENERIC_MEMORY_ATTRIBUTES + select KVM_PRIVATE_MEM + bool
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Sean Christopherson
maintainerRFC PATCH v11 18/29 KVM: selftests: Drop unused kvm_userspace_memory_region_find() helper
In reply to Sean ChristophersonDrop kvm_userspace_memory_region_find(), it's unused and a terrible API (probably why it's unused). If anything outside of kvm_util.c needs to get at the memslot, userspace_mem_region_find() can be exposed to give others full access to all memory region/slot information.
--- .../selftests/kvm/include/kvm_util_base.h | 4 --- tools/testing/selftests/kvm/lib/kvm_util.c | 29 ------------------- 2 files changed, 33 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +0 -4
@@ -753,10 +753,6 @@ vm_adjust_num_guest_pages(enum vm_guest_mode mode, unsigned int num_guest_pages)return n; } -struct kvm_userspace_memory_region * -kvm_userspace_memory_region_find (struct kvm_vm *vm,uint64_t start, -uint64_t end); -#define sync_global_to_guest (vm, g) ({ \ typeof(g) *_p = addr_gva2hva(vm, (vm_vaddr_t)&(g)); \ memcpy(_p, &(g), sizeof(g)); \
tools/testing/selftests/kvm/lib/kvm_util.c +0 -29
@@ -586,35 +586,6 @@ userspace_mem_region_find(struct kvm_vm *vm, uint64_t start, uint64_t end)return NULL ; } -/* -* KVM Userspace Memory Region Find -* -* Input Args: -* vm - Virtual Machine -* start - Starting VM physical address -* end - Ending VM physical address, inclusive. -* -* Output Args: None -* -* Return: -* Pointer to overlapping region, NULL if no such region. -* -* Public interface to userspace_mem_region_find. Allows tests to look up -* the memslot datastructure for a given range of guest physical memory. -*/ -struct kvm_userspace_memory_region * -kvm_userspace_memory_region_find (struct kvm_vm *vm,uint64_t start, -uint64_t end) -{ -struct userspace_mem_region *region; - - region =userspace_mem_region_find (vm, start, end); -if (!region) -return NULL ; - -return ®ion->region; -} -__weak void vcpu_arch_free (struct kvm_vcpu *vcpu) {
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Paolo Bonzini
maintainerRe: [RFC PATCH v11 18/29] KVM: selftests: Drop unused kvm_userspace_memory_region_find() helper
Quoted 56 lines · Sean Christopherson
Drop
kvm_userspace_memory_region_find(), it's unused and a terrible API (probably why it's unused). If anything outside ofkvm_util.cneeds to get at the memslot,userspace_mem_region_find()can be exposed to give others full access to all memory region/slot information.Signed-off-by Sean Christopherson <[email protected]>--- .../selftests/kvm/include/kvm_util_base.h | 4 --- tools/testing/selftests/kvm/lib/kvm_util.c | 29 ------------------- 2 files changed, 33 deletions(-)tools/testing/selftests/kvm/include/kvm_util_base.h +0
-4@@ -753,10 +753,6 @@ vm_adjust_num_guest_pages(enum vm_guest_mode mode, unsigned int num_guest_pages)return n; } -struct kvm_userspace_memory_region * -kvm_userspace_memory_region_find (struct kvm_vm *vm,uint64_t start, -uint64_t end); -#define sync_global_to_guest (vm, g) ({ \ typeof(g) *_p = addr_gva2hva(vm, (vm_vaddr_t)&(g)); \ memcpy(_p, &(g), sizeof(g)); \tools/testing/selftests/kvm/lib/kvm_util.c +0
-22@@ -586,35 +586,6 @@ userspace_mem_region_find(struct kvm_vm *vm, uint64_t start, uint64_t end)return NULL ; } -/* -* KVM Userspace Memory Region Find -* -* Input Args: -* vm - Virtual Machine -* start - Starting VM physical address -* end - Ending VM physical address, inclusive. -* -* Output Args: None -* -* Return: -* Pointer to overlapping region, NULL if no such region. -* -* Public interface to userspace_mem_region_find. Allows tests to look up -* the memslot datastructure for a given range of guest physical memory. -*/ -struct kvm_userspace_memory_region * -kvm_userspace_memory_region_find (struct kvm_vm *vm,uint64_t start, -uint64_t end) -{ -struct userspace_mem_region *region; -
tools/testing/selftests/kvm/lib/kvm_util.c userspace_mem_region_find(struct kvm_vm *vm, uint64_t start, uint64_t end) · line 591
Quoted 10 lines
tools/testing/selftests/kvm/lib/kvm_util.c +0
-7- region =userspace_mem_region_find (vm, start, end); -if (!region) -return NULL ; - -return ®ion->region; -} -__weak void vcpu_arch_free (struct kvm_vcpu *vcpu) {
Will queue this.
Paolo
Sean Christopherson
maintainerRFC PATCH v11 19/29 KVM: selftests: Convert lib's mem regions to KVM_SET_USER_MEMORY_REGION2
In reply to Sean Christopherson--- .../selftests/kvm/include/kvm_util_base.h | 2 +- tools/testing/selftests/kvm/lib/kvm_util.c | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +1 -1
@@ -43,7 +43,7 @@ typedef uint64_t vm_paddr_t; /* Virtual Machine (Guest) physical address */typedef uint64_t vm_vaddr_t ;/* Virtual Machine (Guest) virtual address */ struct userspace_mem_region { -struct kvm_userspace_memory_region region; +struct kvm_userspace_memory_region2 region;struct sparsebit *unused_phy_pages;int fd;off_t offset;
tools/testing/selftests/kvm/lib/kvm_util.c +9 -9
@@ -449,8 +449,8 @@ void kvm_vm_restart(struct kvm_vm *vmp)vm_create_irqchip (vmp);hash_for_each (vmp->regions.slot_hash, ctr, region, slot_node) { -int ret =ioctl (vmp->fd,KVM_SET_USER_MEMORY_REGION , ®ion->region); - TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed,\n" +int ret =ioctl (vmp->fd,KVM_SET_USER_MEMORY_REGION2 , ®ion->region); + TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION2 IOCTL failed,\n" " rc: %i errno: %i\n" " slot: %u flags: 0x%x\n" " guest_phys_addr: 0x%llx size: 0x%llx",
@@ -653,7 +653,7 @@ static void __vm_mem_region_delete(struct kvm_vm *vm, } region->region.memory_size =0 ; -vm_ioctl (vm,KVM_SET_USER_MEMORY_REGION , ®ion->region); +vm_ioctl (vm,KVM_SET_USER_MEMORY_REGION2 , ®ion->region);sparsebit_free (®ion->unused_phy_pages); ret =munmap (region->mmap_start, region->mmap_size);
@@ -1010,8 +1010,8 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, region->region.guest_phys_addr = guest_paddr; region->region.memory_size = npages * vm->page_size; region->region.userspace_addr = (uintptr_t ) region->host_mem; - ret =__vm_ioctl (vm,KVM_SET_USER_MEMORY_REGION , ®ion->region); - TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed,\n" + ret =__vm_ioctl (vm,KVM_SET_USER_MEMORY_REGION2 , ®ion->region); + TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION2 IOCTL failed,\n" " rc: %i errno: %i\n" " slot: %u flags: 0x%x\n" " guest_phys_addr: 0x%lx size: 0x%lx",
@@ -1093,9 +1093,9 @@ void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags) region->region.flags = flags; - ret =__vm_ioctl (vm,KVM_SET_USER_MEMORY_REGION , ®ion->region); + ret =__vm_ioctl (vm,KVM_SET_USER_MEMORY_REGION2 , ®ion->region); -TEST_ASSERT (ret ==0 ,"KVM_SET_USER_MEMORY_REGION IOCTL failed,\n" +TEST_ASSERT (ret ==0 ,"KVM_SET_USER_MEMORY_REGION2 IOCTL failed,\n" " rc: %i errno: %i slot: %u flags: 0x%x" , ret, errno, slot, flags); }
@@ -1123,9 +1123,9 @@ void vm_mem_region_move(struct kvm_vm *vm, uint32_t slot, uint64_t new_gpa) region->region.guest_phys_addr = new_gpa; - ret =__vm_ioctl (vm,KVM_SET_USER_MEMORY_REGION , ®ion->region); + ret =__vm_ioctl (vm,KVM_SET_USER_MEMORY_REGION2 , ®ion->region); -TEST_ASSERT (!ret,"KVM_SET_USER_MEMORY_REGION failed\n" +TEST_ASSERT (!ret,"KVM_SET_USER_MEMORY_REGION2 failed\n" "ret: %i errno: %i slot: %u new_gpa: 0x%lx" , ret, errno, slot, new_gpa); }
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Sean Christopherson
maintainerRFC PATCH v11 20/29 KVM: selftests: Add support for creating private memslots
In reply to Sean Christopherson--- .../selftests/kvm/include/kvm_util_base.h | 16 ++++ .../testing/selftests/kvm/include/test_util.h | 5 ++ tools/testing/selftests/kvm/lib/kvm_util.c | 85 ++++++++++++------- 3 files changed, 75 insertions(+), 31 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +16 -0
@@ -407,6 +407,19 @@ static inline uint64_t vm_get_stat(struct kvm_vm *vm, const char *stat_name) }void vm_create_irqchip (struct kvm_vm *vm); +static inline int vm_create_guest_memfd (struct kvm_vm *vm,uint64_t size, +uint64_t flags) +{ +struct kvm_create_guest_memfd gmem = { + .size = size, + .flags = flags, + }; + +int fd =__vm_ioctl (vm,KVM_CREATE_GUEST_MEMFD , &gmem); + +TEST_ASSERT (fd >=0 ,KVM_IOCTL_ERROR (KVM_CREATE_GUEST_MEMFD , fd)); +return fd; +}void vm_set_user_memory_region (struct kvm_vm *vm,uint32_t slot,uint32_t flags,uint64_t gpa,uint64_t size,void *hva);
@@ -416,6 +429,9 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,enum vm_mem_backing_src_type src_type, uint64_t guest_paddr, uint32_t slot, uint64_t npages, uint32_t flags); +void vm_mem_add (struct kvm_vm *vm,enum vm_mem_backing_src_type src_type, +uint64_t guest_paddr,uint32_t slot,uint64_t npages, +uint32_t flags,int gmem_fd,uint64_t gmem_offset);void vm_mem_region_set_flags (struct kvm_vm *vm,uint32_t slot,uint32_t flags);void vm_mem_region_move (struct kvm_vm *vm,uint32_t slot,uint64_t new_gpa);
tools/testing/selftests/kvm/include/test_util.h +5 -0
@@ -143,6 +143,11 @@ static inline bool backing_src_is_shared(enum vm_mem_backing_src_type t)return vm_mem_backing_src_alias (t)->flag &MAP_SHARED ; } +static inline bool backing_src_can_be_huge (enum vm_mem_backing_src_type t) +{ +return t !=VM_MEM_SRC_ANONYMOUS && t !=VM_MEM_SRC_SHMEM ; +} +/* Aligns x up to the next multiple of size. Size must be a power of 2. */ static inline uint64_t align_up (uint64_t x,uint64_t size) {
tools/testing/selftests/kvm/lib/kvm_util.c +54 -31
132 lines not on this page · raw message
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Sean Christopherson
maintainerRFC PATCH v11 21/29 KVM: selftests: Add helpers to convert guest memory b/w private and shared
In reply to Sean ChristophersonFrom: Vishal Annapurve <[email protected]>
--- .../selftests/kvm/include/kvm_util_base.h | 48 +++++++++++++++++++ tools/testing/selftests/kvm/lib/kvm_util.c | 26 ++++++++++ 2 files changed, 74 insertions(+)
tools/testing/selftests/kvm/include/kvm_util_base.h +48 -0
@@ -312,6 +312,54 @@ static inline void vm_enable_cap(struct kvm_vm *vm, uint32_t cap, uint64_t arg0)vm_ioctl (vm,KVM_ENABLE_CAP , &enable_cap); } +static inline void vm_set_memory_attributes (struct kvm_vm *vm,uint64_t gpa, +uint64_t size,uint64_t attributes) +{ +struct kvm_memory_attributes attr = { + .attributes = attributes, + .address = gpa, + .size = size, + .flags =0 , + }; + +/* +* KVM_SET_MEMORY_ATTRIBUTES overwrites _all_ attributes. These flows +* need significant enhancements to support multiple attributes. +*/ +TEST_ASSERT (!attributes || attributes ==KVM_MEMORY_ATTRIBUTE_PRIVATE , +"Update me to support multiple attributes!" ); + +vm_ioctl (vm,KVM_SET_MEMORY_ATTRIBUTES , &attr); +} + + +static inline void vm_mem_set_private (struct kvm_vm *vm,uint64_t gpa, +uint64_t size) +{ +vm_set_memory_attributes (vm, gpa, size,KVM_MEMORY_ATTRIBUTE_PRIVATE ); +} + +static inline void vm_mem_set_shared (struct kvm_vm *vm,uint64_t gpa, +uint64_t size) +{ +vm_set_memory_attributes (vm, gpa, size,0 ); +} + +void vm_guest_mem_fallocate (struct kvm_vm *vm,uint64_t gpa,uint64_t size, +bool punch_hole); + +static inline void vm_guest_mem_punch_hole (struct kvm_vm *vm,uint64_t gpa, +uint64_t size) +{ +vm_guest_mem_fallocate (vm, gpa, size, true); +} + +static inline void vm_guest_mem_allocate (struct kvm_vm *vm,uint64_t gpa, +uint64_t size) +{ +vm_guest_mem_fallocate (vm, gpa, size, false); +} +void vm_enable_dirty_ring (struct kvm_vm *vm,uint32_t ring_size);const char *vm_guest_mode_string (uint32_t i);
tools/testing/selftests/kvm/lib/kvm_util.c +26 -0
@@ -1171,6 +1171,32 @@ void vm_mem_region_delete(struct kvm_vm *vm, uint32_t slot)__vm_mem_region_delete (vm,memslot2region (vm, slot), true); } +void vm_guest_mem_fallocate (struct kvm_vm *vm,uint64_t gpa,uint64_t size, +bool punch_hole) +{ +struct userspace_mem_region *region; +uint64_t end = gpa + size -1 ; +off_t fd_offset; +int mode, ret; + + region =userspace_mem_region_find (vm, gpa, gpa); +TEST_ASSERT (region && region->region.flags &KVM_MEM_PRIVATE , +"Private memory region not found for GPA 0x%lx" , gpa); + +TEST_ASSERT (region ==userspace_mem_region_find (vm, end, end), +"fallocate() for guest_memfd must act on a single memslot" ); + + fd_offset = region->region.gmem_offset + + (gpa - region->region.guest_phys_addr); + + mode =FALLOC_FL_KEEP_SIZE | (punch_hole ?FALLOC_FL_PUNCH_HOLE :0 ); + + ret =fallocate (region->region.gmem_fd, mode, fd_offset, size); +TEST_ASSERT (!ret,"fallocate() failed to %s at %lx[%lu], fd = %d, mode = %x, offset = %lx\n" , + punch_hole ?"punch hole" :"allocate" , gpa, size, + region->region.gmem_fd, mode, fd_offset); +} +/* Returns the size of a vCPU's kvm_run structure. */ static int vcpu_mmap_sz (void ) {
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Sean Christopherson
maintainerRFC PATCH v11 22/29 KVM: selftests: Add helpers to do KVM_HC_MAP_GPA_RANGE hypercalls (x86)
In reply to Sean ChristophersonFrom: Vishal Annapurve <[email protected]>
--- .../selftests/kvm/include/x86_64/processor.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
tools/testing/selftests/kvm/include/x86_64/processor.h +15 -0
@@ -15,6 +15,7 @@#include <asm/msr-index.h> #include <asm/prctl.h> +#include <linux/kvm_para.h> #include <linux/stringify.h> #include "../kvm_util.h"
@@ -1166,6 +1167,20 @@ uint64_t kvm_hypercall(uint64_t nr, uint64_t a0, uint64_t a1, uint64_t a2,uint64_t __xen_hypercall (uint64_t nr,uint64_t a0,void *a1);void xen_hypercall (uint64_t nr,uint64_t a0,void *a1); +static inline uint64_t __kvm_hypercall_map_gpa_range (uint64_t gpa, +uint64_t size,uint64_t flags) +{ +return kvm_hypercall (KVM_HC_MAP_GPA_RANGE , gpa, size >>PAGE_SHIFT , flags,0 ); +} + +static inline void kvm_hypercall_map_gpa_range (uint64_t gpa,uint64_t size, +uint64_t flags) +{ +uint64_t ret =__kvm_hypercall_map_gpa_range (gpa, size, flags); + +GUEST_ASSERT_1 (!ret, ret); +} +void __vm_xsave_require_permission (uint64_t xfeature,const char *name);#define vm_xsave_require_permission (xfeature) \
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Sean Christopherson
maintainerRFC PATCH v11 23/29 KVM: selftests: Introduce VM "shape" to allow tests to specify the VM type
In reply to Sean Christopherson--- tools/testing/selftests/kvm/dirty_log_test.c | 2 +- .../selftests/kvm/include/kvm_util_base.h | 54 +++++++++++++++---- .../selftests/kvm/kvm_page_table_test.c | 2 +- tools/testing/selftests/kvm/lib/kvm_util.c | 43 +++++++-------- tools/testing/selftests/kvm/lib/memstress.c | 3 +- .../kvm/x86_64/ucna_injection_test.c | 2 +- 6 files changed, 72 insertions(+), 34 deletions(-)
tools/testing/selftests/kvm/dirty_log_test.c +1 -1
@@ -699,7 +699,7 @@ static struct kvm_vm *create_vm(enum vm_guest_mode mode, struct kvm_vcpu **vcpu,pr_info ("Testing guest mode: %s\n" ,vm_guest_mode_string (mode)); - vm =__vm_create (mode,1 , extra_mem_pages); + vm =__vm_create (VM_SHAPE (mode),1 , extra_mem_pages);log_mode_create_vm_done (vm); *vcpu =vm_vcpu_add (vm,0 , guest_code);
tools/testing/selftests/kvm/include/kvm_util_base.h +45 -9
101 lines not on this page · raw message
tools/testing/selftests/kvm/kvm_page_table_test.c +1 -1
@@ -254,7 +254,7 @@ static struct kvm_vm *pre_init_before_test(enum vm_guest_mode mode, void *arg)/* Create a VM with enough guest pages */ guest_num_pages = test_mem_size / guest_page_size; - vm =__vm_create_with_vcpus (mode, nr_vcpus, guest_num_pages, + vm =__vm_create_with_vcpus (VM_SHAPE (mode), nr_vcpus, guest_num_pages, guest_code, test_args.vcpus);/* Align down GPA of the testing memslot */
tools/testing/selftests/kvm/lib/kvm_util.c +22 -21
107 lines not on this page · raw message
tools/testing/selftests/kvm/lib/memstress.c +2 -1
@@ -168,7 +168,8 @@ struct kvm_vm *memstress_create_vm(enum vm_guest_mode mode, int nr_vcpus,* The memory is also added to memslot 0, but that's a benign side * effect as KVM allows aliasing HVAs in meslots. */ - vm =__vm_create_with_vcpus (mode, nr_vcpus, slot0_pages + guest_num_pages, + vm =__vm_create_with_vcpus (VM_SHAPE (mode), nr_vcpus, + slot0_pages + guest_num_pages, memstress_guest_code, vcpus); args->vm = vm;
tools/testing/selftests/kvm/x86_64/ucna_injection_test.c +1 -1
@@ -271,7 +271,7 @@ int main(int argc, char *argv[])kvm_check_cap (KVM_CAP_MCE ); - vm =__vm_create (VM_MODE_DEFAULT ,3 ,0 ); + vm =__vm_create (VM_SHAPE_DEFAULT ,3 ,0 );kvm_ioctl (vm->kvm_fd,KVM_X86_GET_MCE_CAP_SUPPORTED , &supported_mcg_caps);
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Sean Christopherson
maintainerRFC PATCH v11 24/29 KVM: selftests: Add GUEST_SYNC[1-6] macros for synchronizing more data
In reply to Sean Christopherson--- tools/testing/selftests/kvm/include/ucall_common.h | 12 ++++++++++++ 1 file changed, 12 insertions(+)
tools/testing/selftests/kvm/include/ucall_common.h +12 -0
@@ -46,6 +46,18 @@ void ucall_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa);#define GUEST_SYNC_ARGS (stage, arg1, arg2, arg3, arg4) \ ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4)#define GUEST_SYNC (stage) ucall(UCALL_SYNC, 2, "hello", stage) + +#define GUEST_SYNC1 (arg0) ucall(UCALL_SYNC, 1, arg0) +#define GUEST_SYNC2 (arg0, arg1) ucall(UCALL_SYNC, 2, arg0, arg1) +#define GUEST_SYNC3 (arg0, arg1, arg2) \ + ucall(UCALL_SYNC, 3, arg0, arg1, arg2) +#define GUEST_SYNC4 (arg0, arg1, arg2, arg3) \ + ucall(UCALL_SYNC, 4, arg0, arg1, arg2, arg3) +#define GUEST_SYNC5 (arg0, arg1, arg2, arg3, arg4) \ + ucall(UCALL_SYNC, 5, arg0, arg1, arg2, arg3, arg4) +#define GUEST_SYNC6 (arg0, arg1, arg2, arg3, arg4, arg5) \ + ucall(UCALL_SYNC, 6, arg0, arg1, arg2, arg3, arg4, arg5) +#define GUEST_DONE () ucall(UCALL_DONE, 0)enum guest_assert_builtin_args {
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Sean Christopherson
maintainerRFC PATCH v11 25/29 KVM: selftests: Add x86-only selftest for private memory conversions
In reply to Sean ChristophersonFrom: Vishal Annapurve <[email protected]>
Add a selftest to exercise implicit/explicit conversion functionality within KVM and verify:
- Shared memory is visible to host userspace
- Private memory is not visible to host userspace
- Host userspace and guest can communicate over shared memory
- Data in shared backing is preserved across conversions (test's host userspace doesn't free the data)
- Private memory is bound to the lifetime of the VM
TODO: rewrite this to allow backing a single region of guest memory with multiple memslots for _all_ backing types and shapes, i.e. make the code for using a single backing fd across multiple memslots apply to regular memory as well.
--- tools/testing/selftests/kvm/Makefile | 1 + .../kvm/x86_64/private_mem_conversions_test.c | 408 ++++++++++++++++++ 2 files changed, 409 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c
tools/testing/selftests/kvm/Makefile +1 -0
@@ -80,6 +80,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/monitor_mwait_test TEST_GEN_PROGS_x86_64 += x86_64/nested_exceptions_test TEST_GEN_PROGS_x86_64 += x86_64/platform_info_test TEST_GEN_PROGS_x86_64 += x86_64/pmu_event_filter_test +TEST_GEN_PROGS_x86_64 += x86_64/private_mem_conversions_test TEST_GEN_PROGS_x86_64 += x86_64/set_boot_cpu_id TEST_GEN_PROGS_x86_64 += x86_64/set_sregs_test TEST_GEN_PROGS_x86_64 += x86_64/smaller_maxphyaddr_emulation_test
tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c new file +408 -0
408 lines not on this page · raw message
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Sean Christopherson
maintainerRFC PATCH v11 26/29 KVM: selftests: Add KVM_SET_USER_MEMORY_REGION2 helper
In reply to Sean ChristophersonFrom: Chao Peng <[email protected]>
Provide a raw version as well as an assert-success version to reduce the amount of boilerplate code need for basic usage.
--- .../selftests/kvm/include/kvm_util_base.h | 7 +++++ tools/testing/selftests/kvm/lib/kvm_util.c | 29 +++++++++++++++++++ 2 files changed, 36 insertions(+)
tools/testing/selftests/kvm/include/kvm_util_base.h +7 -0
@@ -492,6 +492,13 @@ void vm_set_user_memory_region(struct kvm_vm *vm, uint32_t slot, uint32_t flags, uint64_t gpa, uint64_t size, void *hva);int __vm_set_user_memory_region (struct kvm_vm *vm,uint32_t slot,uint32_t flags,uint64_t gpa,uint64_t size,void *hva); +void vm_set_user_memory_region2 (struct kvm_vm *vm,uint32_t slot, +uint32_t flags,uint64_t gpa,uint64_t size, +void *hva,uint32_t gmem_fd,uint64_t gmem_offset); +int __vm_set_user_memory_region2 (struct kvm_vm *vm,uint32_t slot, +uint32_t flags,uint64_t gpa,uint64_t size, +void *hva,uint32_t gmem_fd,uint64_t gmem_offset); + void vm_userspace_mem_region_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, uint64_t guest_paddr, uint32_t slot, uint64_t npages,
tools/testing/selftests/kvm/lib/kvm_util.c +29 -0
@@ -868,6 +868,35 @@ void vm_set_user_memory_region(struct kvm_vm *vm, uint32_t slot, uint32_t flags, errno, strerror(errno)); } +int __vm_set_user_memory_region2 (struct kvm_vm *vm,uint32_t slot, +uint32_t flags,uint64_t gpa,uint64_t size, +void *hva,uint32_t gmem_fd,uint64_t gmem_offset) +{ +struct kvm_userspace_memory_region2 region = { + .slot = slot, + .flags = flags, + .guest_phys_addr = gpa, + .memory_size = size, + .userspace_addr = (uintptr_t )hva, + .gmem_fd = gmem_fd, + .gmem_offset = gmem_offset, + }; + +return ioctl (vm->fd,KVM_SET_USER_MEMORY_REGION2 , ®ion); +} + +void vm_set_user_memory_region2 (struct kvm_vm *vm,uint32_t slot, +uint32_t flags,uint64_t gpa,uint64_t size, +void *hva,uint32_t gmem_fd,uint64_t gmem_offset) +{ +int ret =__vm_set_user_memory_region2 (vm, slot, flags, gpa, size, hva, + gmem_fd, gmem_offset); + +TEST_ASSERT (!ret,"KVM_SET_USER_MEMORY_REGION2 failed, errno = %d (%s)" , + errno,strerror (errno)); +} + +/* FIXME: This thing needs to be ripped apart and rewritten. */ void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, uint64_t guest_paddr, uint32_t slot, uint64_t npages,
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Sean Christopherson
maintainerRFC PATCH v11 27/29 KVM: selftests: Expand set_memory_region_test to validate guest_memfd()
In reply to Sean ChristophersonFrom: Chao Peng <[email protected]>
Expand set_memory_region_test to exercise various positive and negative testcases for private memory.
- Non-guest_memfd() file descriptor for private memory
- guest_memfd() from different VM
- Overlapping bindings
- Unaligned bindings
--- .../selftests/kvm/include/kvm_util_base.h | 10 ++ .../selftests/kvm/set_memory_region_test.c | 99 +++++++++++++++++++ 2 files changed, 109 insertions(+)
tools/testing/selftests/kvm/include/kvm_util_base.h +10 -0
@@ -789,6 +789,16 @@ static inline struct kvm_vm *vm_create_barebones(void)return ____vm_create (VM_SHAPE_DEFAULT ); } +static inline struct kvm_vm *vm_create_barebones_protected_vm (void ) +{ +const struct vm_shape shape = { + .mode =VM_MODE_DEFAULT , + .type =KVM_X86_SW_PROTECTED_VM , + }; + +return ____vm_create (shape); +} +static inline struct kvm_vm *vm_create (uint32_t nr_runnable_vcpus) {return __vm_create (VM_SHAPE_DEFAULT , nr_runnable_vcpus,0 );
tools/testing/selftests/kvm/set_memory_region_test.c +99 -0
111 lines not on this page · raw message
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Re: [RFC PATCH v11 27/29] KVM: selftests: Expand set_memory_region_test to validate guest_memfd()
Quoted 36 lines · Sean Christopherson
From: Chao Peng <[email protected]>
Expand
set_memory_region_testto exercise various positive and negative testcases for private memory.- Non-guest_memfd() file descriptor for private memory
-guest_memfd()from different VM
- Overlapping bindings
- Unaligned bindingsSigned-off-by Chao Peng <[email protected]>Co-developed-by Ackerley Tng <[email protected]>Signed-off-by Ackerley Tng <[email protected]>[sean: trim the testcases to remove duplicate coverage]Signed-off-by Sean Christopherson <[email protected]>--- .../selftests/kvm/include/kvm_util_base.h | 10 ++ .../selftests/kvm/set_memory_region_test.c | 99 +++++++++++++++++++ 2 files changed, 109 insertions(+)tools/testing/selftests/kvm/include/kvm_util_base.h +5
-0@@ -789,6 +789,16 @@ static inline struct kvm_vm *vm_create_barebones(void)return ____vm_create (VM_SHAPE_DEFAULT ); } + +static void test_add_private_memory_region (void ) +{ +struct kvm_vm *vm, *vm2; +int memfd, i;
tools/testing/selftests/kvm/include/kvm_util_base.h static inline struct kvm_vm *vm_create_barebones(void)
Quoted 10 lines
tools/testing/selftests/kvm/include/kvm_util_base.h +10
-0+ +pr_info ("Testing ADD of KVM_MEM_PRIVATE memory regions\n" ); + + vm =vm_create_barebones_protected_vm (); + +test_invalid_guest_memfd (vm, vm->kvm_fd,0 ,"KVM fd should fail" ); +test_invalid_guest_memfd (vm, vm->fd,0 ,"VM's fd should fail" ); + + memfd =kvm_memfd_alloc (MEM_REGION_SIZE , false); +test_invalid_guest_memfd (vm, vm->fd,0 ,"Regular memfd() should fail" );
This should be
test_invalid_guest_memfd(vm, memfd, 0, "Regular memfd() should fail");
Quoted 24 lines
tools/testing/selftests/kvm/include/kvm_util_base.h +22
-0+close (memfd); + + vm2 =vm_create_barebones_protected_vm (); + memfd =vm_create_guest_memfd (vm2,MEM_REGION_SIZE ,0 ); +test_invalid_guest_memfd (vm, memfd,0 ,"Other VM's guest_memfd() should fail" ); + +vm_set_user_memory_region2 (vm2,MEM_REGION_SLOT ,KVM_MEM_PRIVATE , +MEM_REGION_GPA ,MEM_REGION_SIZE ,0 , memfd,0 ); +close (memfd); +kvm_vm_free (vm2); + + memfd =vm_create_guest_memfd (vm,MEM_REGION_SIZE ,0 ); +for (i =1 ; i <PAGE_SIZE ; i++) +test_invalid_guest_memfd (vm, memfd, i,"Unaligned offset should fail" ); + +vm_set_user_memory_region2 (vm,MEM_REGION_SLOT ,KVM_MEM_PRIVATE , +MEM_REGION_GPA ,MEM_REGION_SIZE ,0 , memfd,0 ); +close (memfd); + +kvm_vm_free (vm); +} +
Sean Christopherson
maintainerRFC PATCH v11 28/29 KVM: selftests: Add basic selftest for guest_memfd()
In reply to Sean ChristophersonAdd a selftest to verify the basic functionality of guest_memfd():
+ file descriptor created with the guest_memfd() ioctl does not allow read/write/mmap operations
+ file size and block size as returned from fstat are as expected
+ fallocate on the fd checks that offset/length on fallocate(FALLOC_FL_PUNCH_HOLE) should be page aligned
--- tools/testing/selftests/kvm/Makefile | 1 + .../testing/selftests/kvm/guest_memfd_test.c | 114 ++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 tools/testing/selftests/kvm/guest_memfd_test.c
tools/testing/selftests/kvm/Makefile +1 -0
@@ -123,6 +123,7 @@ TEST_GEN_PROGS_x86_64 += access_tracking_perf_test TEST_GEN_PROGS_x86_64 += demand_paging_test TEST_GEN_PROGS_x86_64 += dirty_log_test TEST_GEN_PROGS_x86_64 += dirty_log_perf_test +TEST_GEN_PROGS_x86_64 += guest_memfd_test TEST_GEN_PROGS_x86_64 += hardware_disable_test TEST_GEN_PROGS_x86_64 += kvm_create_max_vcpus TEST_GEN_PROGS_x86_64 += kvm_page_table_test
tools/testing/selftests/kvm/guest_memfd_test.c new file +114 -0
114 lines not on this page · raw message
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Re: [RFC PATCH v11 28/29] KVM: selftests: Add basic selftest for guest_memfd()
Quoted 18 lines · Sean Christopherson
Add a selftest to verify the basic functionality of
guest_memfd():+ file descriptor created with the
guest_memfd()ioctl does not allow read/write/mmap operations
+ file size and block size as returned from fstat are as expected
+ fallocate on the fd checks that offset/length onfallocate(FALLOC_FL_PUNCH_HOLE)should be page aligned<snip>
tools/testing/selftests/kvm/guest_memfd_test.c new file +7
-0+ +static void test_fallocate (int fd,size_t page_size,size_t total_size) +{ +int ret; + + ret =fallocate (fd,FALLOC_FL_KEEP_SIZE ,0 , total_size); +TEST_ASSERT (!ret,"fallocate with aligned offset and size should succeed" );
tools/testing/selftests/kvm/guest_memfd_test.c line 71
Quoted 10 lines
tools/testing/selftests/kvm/guest_memfd_test.c new file +10
-0+ + ret =fallocate (fd,FALLOC_FL_KEEP_SIZE |FALLOC_FL_PUNCH_HOLE , + page_size -1 , page_size); +TEST_ASSERT (ret,"fallocate with unaligned offset should fail" ); + + ret =fallocate (fd,FALLOC_FL_KEEP_SIZE , total_size, page_size); +TEST_ASSERT (ret,"fallocate beginning at total_size should fail" ); + + ret =fallocate (fd,FALLOC_FL_KEEP_SIZE , total_size + page_size, page_size); +TEST_ASSERT (ret,"fallocate beginning at total_size should fail" );
This should be
TEST_ASSERT(ret, "fallocate beginning after total_size should fail");
Quoted 22 lines
tools/testing/selftests/kvm/guest_memfd_test.c new file +20
-0+ + ret =fallocate (fd,FALLOC_FL_KEEP_SIZE |FALLOC_FL_PUNCH_HOLE , + total_size, page_size); +TEST_ASSERT (!ret,"fallocate(PUNCH_HOLE) at total_size should succeed" ); + + ret =fallocate (fd,FALLOC_FL_KEEP_SIZE |FALLOC_FL_PUNCH_HOLE , + total_size + page_size, page_size); +TEST_ASSERT (!ret,"fallocate(PUNCH_HOLE) after total_size should succeed" ); + + ret =fallocate (fd,FALLOC_FL_KEEP_SIZE |FALLOC_FL_PUNCH_HOLE , + page_size, page_size -1 ); +TEST_ASSERT (ret,"fallocate with unaligned size should fail" ); + + ret =fallocate (fd,FALLOC_FL_KEEP_SIZE |FALLOC_FL_PUNCH_HOLE , + page_size, page_size); +TEST_ASSERT (!ret,"fallocate(PUNCH_HOLE) with aligned offset and size should succeed" ); + + ret =fallocate (fd,FALLOC_FL_KEEP_SIZE , page_size, page_size); +TEST_ASSERT (!ret,"fallocate to restore punched hole should succeed" ); +}
Sean Christopherson
maintainerRe: [RFC PATCH v11 28/29] KVM: selftests: Add basic selftest for guest_memfd()
Quoted 7 lines · Ackerley Tng
tools/testing/selftests/kvm/guest_memfd_test.c line 71
Quoted 2 lines · Sean Christopherson
tools/testing/selftests/kvm/guest_memfd_test.c new file +2
-0+ ret =fallocate (fd,FALLOC_FL_KEEP_SIZE , total_size + page_size, page_size); +TEST_ASSERT (ret,"fallocate beginning at total_size should fail" );This should be
TEST_ASSERT(ret, "fallocate beginning after total_size should fail");
Roger that, I'll push a fixup commit directly to kvm-x86/guest_memfd. Thanks!
Re: [RFC PATCH v11 28/29] KVM: selftests: Add basic selftest for guest_memfd()
In reply to Sean ChristophersonQuoted 3 lines · Sean Christopherson
Add a selftest to verify the basic functionality of
guest_memfd():<snip>
Here's one more test:
From 72dc6836f01bdd613d64d4c6a4f2af8f2b777ba2 Mon Sep 17 00:00:00 2001
From: Ackerley Tng <[email protected]>
Date: Tue, 1 Aug 2023 18:02:50 +0000
Subject: [PATCH] KVM: selftests: Add tests - invalid inputs for
KVM_CREATE_GUEST_MEMFD
Test that invalid inputs for KVM_CREATE_GUEST_MEMFD, such as non-page-aligned page size and invalid flags, are rejected by the KVM_CREATE_GUEST_MEMFD with EINVAL
--- tools/testing/selftests/kvm/guest_memfd_test.c | 17 +++++++++++++++++ .../selftests/kvm/include/kvm_util_base.h | 11 +++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/guest_memfd_test.c +17 -0
@@ -90,6 +90,21 @@ static void test_fallocate(int fd, size_t page_size, size_t total_size)TEST_ASSERT (!ret,"fallocate to restore punched hole should succeed" ); } +static void test_create_guest_memfd_invalid (struct kvm_vm *vm,size_t page_size) +{ +int fd; + +/* Non-page-aligned page_size */ + fd =__vm_create_guest_memfd (vm,1 ,0 ); +ASSERT_EQ (fd,-1 ); +ASSERT_EQ (errno,EINVAL ); + +/* Invalid flags */ + fd =__vm_create_guest_memfd (vm, page_size,99 ); +ASSERT_EQ (fd,-1 ); +ASSERT_EQ (errno,EINVAL ); +} +int main (int argc,char *argv[]) {
@@ -103,6 +118,8 @@ int main(int argc, char *argv[]) vm =vm_create_barebones (); +test_create_guest_memfd_invalid (vm, page_size); + fd =vm_create_guest_memfd (vm, total_size,0 );test_file_read_write (fd);
tools/testing/selftests/kvm/include/kvm_util_base.h +9 -2
@@ -474,7 +474,8 @@ static inline uint64_t vm_get_stat(struct kvm_vm *vm, const char *stat_name) }void vm_create_irqchip (struct kvm_vm *vm); -static inline int vm_create_guest_memfd (struct kvm_vm *vm,uint64_t size, + +static inline int __vm_create_guest_memfd (struct kvm_vm *vm,uint64_t size,uint64_t flags) {struct kvm_create_guest_memfd gmem = {
@@ -482,7 +483,13 @@ static inline int vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size, .flags = flags, }; -int fd =__vm_ioctl (vm,KVM_CREATE_GUEST_MEMFD , &gmem); +return __vm_ioctl (vm,KVM_CREATE_GUEST_MEMFD , &gmem); +} + +static inline int vm_create_guest_memfd (struct kvm_vm *vm,uint64_t size, +uint64_t flags) +{ +int fd =__vm_create_guest_memfd (vm, size, flags);TEST_ASSERT (fd >=0 ,KVM_IOCTL_ERROR (KVM_CREATE_GUEST_MEMFD , fd));return fd;
Signature · 1 line
2.41.0.640.ga95def55d0-goog
Sean Christopherson
maintainerRe: [RFC PATCH v11 28/29] KVM: selftests: Add basic selftest for guest_memfd()
Quoted 7 lines · Ackerley Tng
Quoted 3 lines · Sean Christopherson
Add a selftest to verify the basic functionality of
guest_memfd():<snip>
Here's one more test:
First off, thank you! I greatly appreciate all the selftests work you (and others!) have been doing.
For v2, can you please post a standalone patch? My workflow barfs on unrelated, inlined patches. I'm guessing I can get b4 to play nice, but it's easier to just yell at people :-)
Quoted 21 lines
Quoted 1 line
From
72dc6836f01bdd613d64d4c6a4f2af8f2b777ba2Mon Sep 17 00:00:00 2001From: Ackerley Tng <[email protected]>
Date: Tue, 1 Aug 2023 18:02:50 +0000
Subject: [PATCH] KVM: selftests: Add tests - invalid inputs for
KVM_CREATE_GUEST_MEMFDTest that invalid inputs for
KVM_CREATE_GUEST_MEMFD, such as non-page-aligned page size and invalid flags, are rejected by theKVM_CREATE_GUEST_MEMFDwith EINVALSigned-off-by Ackerley Tng <[email protected]>--- tools/testing/selftests/kvm/guest_memfd_test.c | 17 +++++++++++++++++ .../selftests/kvm/include/kvm_util_base.h | 11 +++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-)tools/testing/selftests/kvm/guest_memfd_test.c +0
-0
tools/testing/selftests/kvm/guest_memfd_test.c static void test_fallocate(int fd, size_t page_size, size_t total_size) · line 97
Quoted 8 lines
tools/testing/selftests/kvm/guest_memfd_test.c +5
-0@@ -90,6 +90,21 @@ static void test_fallocate(int fd, size_t page_size, size_t total_size)TEST_ASSERT (!ret,"fallocate to restore punched hole should succeed" ); } +static void test_create_guest_memfd_invalid (struct kvm_vm *vm,size_t page_size) +{ +int fd; + +/* Non-page-aligned page_size */
Instead of adding a comment, use the message from TEST_ASSERT() to communicate that information to the reader *and* to anyone that encounters failures.
tools/testing/selftests/kvm/guest_memfd_test.c static void test_fallocate(int fd, size_t page_size, size_t total_size) · line 98
Quoted 1 line
tools/testing/selftests/kvm/guest_memfd_test.c +1
-0+ fd =__vm_create_guest_memfd (vm,1 ,0 );
ioctls() are fast. Rather than hardcode one value, iterate over a range of values, e.g.
for (size = 0; size < page_size; size++) {
r = __vm_create_guest_memfd(vm, size, 0);
TEST_ASSERT(r && errno == EINVAL,
"Informative error message...);
}tools/testing/selftests/kvm/guest_memfd_test.c static void test_fallocate(int fd, size_t page_size, size_t total_size) · line 105
Quoted 6 lines
tools/testing/selftests/kvm/guest_memfd_test.c +6
-0+ASSERT_EQ (errno,EINVAL ); + +/* Invalid flags */ + fd =__vm_create_guest_memfd (vm, page_size,99 ); +ASSERT_EQ (fd,-1 ); +ASSERT_EQ (errno,EINVAL );
And then same thing here. Then you can use the legal flags to determine what is and isn't valid, instead of using a completely arbitrary magic number.
Re: [RFC PATCH v11 28/29] KVM: selftests: Add basic selftest for guest_memfd()
Quoted 7 lines · Sean Christopherson
Quoted 6 lines · Ackerley Tng
Quoted 3 lines · Sean Christopherson
Add a selftest to verify the basic functionality of
guest_memfd():<snip>
Quoted 9 lines
Quoted 1 line
Here's one more test:
First off, thank you! I greatly appreciate all the selftests work you (and others!) have been doing.
For v2, can you please post a standalone patch? My workflow barfs on unrelated, inlined patches. I'm guessing I can get b4 to play nice, but it's easier to just yell at people :-)
Here's a standalone patch :)
https://lore.kernel.org/lkml/20230821194411.2165757-1-ackerleytng at google.com/
Quoted 1 line
<snip>
Sean Christopherson
maintainerRFC PATCH v11 29/29 KVM: selftests: Test KVM exit behavior for private memory/access
In reply to Sean ChristophersonFrom: Ackerley Tng <[email protected]>
"Testing private access when memslot gets deleted" tests the behavior of KVM when a private memslot gets deleted while the VM is using the private memslot. When KVM looks up the deleted (slot = NULL) memslot, KVM should exit to userspace with KVM_EXIT_MEMORY_FAULT.
In the second test, upon a private access to non-private memslot, KVM should also exit to userspace with KVM_EXIT_MEMORY_FAULT.
sean: These testcases belong in set_memory_region_test.c, they're private variants on existing testscases and aren't as robust, e.g. don't ensure the vCPU is actually running and accessing memory when converting and deleting.
--- tools/testing/selftests/kvm/Makefile | 1 + .../kvm/x86_64/private_mem_kvm_exits_test.c | 115 ++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/private_mem_kvm_exits_test.c
tools/testing/selftests/kvm/Makefile +1 -0
@@ -81,6 +81,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/nested_exceptions_test TEST_GEN_PROGS_x86_64 += x86_64/platform_info_test TEST_GEN_PROGS_x86_64 += x86_64/pmu_event_filter_test TEST_GEN_PROGS_x86_64 += x86_64/private_mem_conversions_test +TEST_GEN_PROGS_x86_64 += x86_64/private_mem_kvm_exits_test TEST_GEN_PROGS_x86_64 += x86_64/set_boot_cpu_id TEST_GEN_PROGS_x86_64 += x86_64/set_sregs_test TEST_GEN_PROGS_x86_64 += x86_64/smaller_maxphyaddr_emulation_test
tools/testing/selftests/kvm/x86_64/private_mem_kvm_exits_test.c new file +115 -0
115 lines not on this page · raw message
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
Quoted 8 lines · Sean Christopherson
This is the next iteration of implementing fd-based (instead of vma-based) memory for KVM guests. If you want the full background of why we are doing this, please go read the v10 cover letter[1].
The biggest change from v10 is to implement the backing storage in KVM itself, and expose it via a KVM
ioctl()instead of a "generic" sycall. See link[2] for details on why we pivoted to a KVM-specific approach.
Quoted 7 lines
Key word is "biggest". Relative to v10, there are many big changes. Highlights below (I can't remember everything that got changed at this point).
Tagged RFC as there are a lot of empty changelogs, and a lot of missing documentation. And ideally, we'll have even more tests before merging. There are also several gaps/opens (to be discussed in tomorrow's PUCK).
As per our discussion on the PUCK call, here are the memory/NUMA accounting related observations that I had while working on SNP guest secure page migration:
* gmem allocations are currently treated as file page allocations accounted to the kernel and not to the QEMU process.
Starting an SNP guest with 40G memory with memory interleave between Node2 and Node3
$ numactl -i 2,3 ./bootg_snp.sh
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 242179 root 20 0 40.4g 99580 51676 S 78.0 0.0 0:56.58 qemu-system-x86
-> Incorrect process resident memory and shared memory is reported
Accounting of the memory happens in the host page fault handler path, but for private guest pages we will never hit that.
* NUMA allocation does use the process mempolicy for appropriate node allocation (Node2 and Node3), but they again do not get attributed to the QEMU process
Every 1.0s: sudo numastat -m -p qemu-system-x86 | egrep -i "qemu|PID|Node|Filepage" gomati: Mon Jul 24 11:51:34 2023
Per-node process memory usage (in MBs)
PID Node 0 Node 1 Node 2 Node 3 Total
242179 (qemu-system-x86) 21.14 1.61 39.44 39.38 101.57
Per-node system memory usage (in MBs):
Node 0 Node 1 Node 2 Node 3 Total
FilePages 2475.63 2395.83 23999.46 23373.22 52244.14
* Most of the memory accounting relies on the VMAs and as private-fd of gmem doesn't have a VMA(and that was the design goal), user-space fails to attribute the memory appropriately to the process.
/proc/<qemu pid>/numa_maps
7f528be00000 interleave:2-3 file=/memfd:memory-backend-memfd-shared\040(deleted) anon=1070 dirty=1070 mapped=1987 mapmax=256 active=1956 N2=582 N3=1405 kernelpagesize_kB=4 7f5c90200000 interleave:2-3 file=/memfd:rom-backend-memfd-shared\040(deleted)
7f5c90400000 interleave:2-3 file=/memfd:rom-backend-memfd-shared\040(deleted) dirty=32 active=0 N2=32 kernelpagesize_kB=4
7f5c90800000 interleave:2-3 file=/memfd:rom-backend-memfd-shared\040(deleted) dirty=892 active=0 N2=512 N3=380 kernelpagesize_kB=4
/proc/<qemu pid>/smaps
7f528be00000-7f5c8be00000 rw-p 00000000 00:01 26629 /memfd:memory-backend-memfd-shared (deleted) 7f5c90200000-7f5c90220000 rw-s 00000000 00:01 44033 /memfd:rom-backend-memfd-shared (deleted) 7f5c90400000-7f5c90420000 rw-s 00000000 00:01 44032 /memfd:rom-backend-memfd-shared (deleted) 7f5c90800000-7f5c90b7c000 rw-s 00000000 00:01 1025 /memfd:rom-backend-memfd-shared (deleted)
* QEMU based NUMA bindings will not work. Memory backend uses mbind() to set the policy for a particular virtual memory range but gmem private-FD does not have a virtual memory range visible in the host.
Regards,
Nikunj
Sean Christopherson
maintainerQuoted 17 lines · Nikunj A. Dadhania
Quoted 8 lines · Sean Christopherson
This is the next iteration of implementing fd-based (instead of vma-based) memory for KVM guests. If you want the full background of why we are doing this, please go read the v10 cover letter[1].
The biggest change from v10 is to implement the backing storage in KVM itself, and expose it via a KVM
ioctl()instead of a "generic" sycall. See link[2] for details on why we pivoted to a KVM-specific approach.Quoted 7 lines
Key word is "biggest". Relative to v10, there are many big changes. Highlights below (I can't remember everything that got changed at this point).
Tagged RFC as there are a lot of empty changelogs, and a lot of missing documentation. And ideally, we'll have even more tests before merging. There are also several gaps/opens (to be discussed in tomorrow's PUCK).
Quoted 5 lines
As per our discussion on the PUCK call, here are the memory/NUMA accounting related observations that I had while working on SNP guest secure page migration:
* gmem allocations are currently treated as file page allocations accounted to the kernel and not to the QEMU process.
We need to level set on terminology: these are all *stats*, not accounting. That distinction matters because we have wiggle room on stats, e.g. we can probably get away with just about any definition of how guest_memfd memory impacts stats, so long as the information that is surfaced to userspace is useful and expected.
But we absolutely need to get accounting correct, specifically the allocations need to be correctly accounted in memcg. And unless I'm missing something, nothing in here shows anything related to memcg.
Quoted 9 lines
Starting an SNP guest with 40G memory with memory interleave between Node2 and Node3
$ numactl -i 2,3 ./bootg_snp.shPID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 242179 root 20 0 40.4g 99580 51676 S 78.0 0.0 0:56.58 qemu-system-x86-> Incorrect process resident memory and shared memory is reported
I don't know that I would call these "incorrect". Shared memory definitely is correct, because by definition guest_memfd isn't shared. RSS is less clear cut; gmem memory is resident in RAM, but if we show gmem in RSS then we'll end up with scenarios where RSS > VIRT, which will be quite confusing for unaware users (I'm assuming the 40g of VIRT here comes from QEMU mapping the shared half of gmem memslots).
Quoted 27 lines
Accounting of the memory happens in the host page fault handler path, but for private guest pages we will never hit that.
* NUMA allocation does use the process mempolicy for appropriate node allocation (Node2 and Node3), but they again do not get attributed to the QEMU process
Every 1.0s: sudo numastat -m -p qemu-system-x86 | egrep -i "qemu|PID|Node|Filepage" gomati: Mon Jul 24 11:51:34 2023
Per-node process memory usage (in MBs)
PID Node 0 Node 1 Node 2 Node 3 Total
242179 (qemu-system-x86) 21.14 1.61 39.44 39.38 101.57
Per-node system memory usage (in MBs):
Node 0 Node 1 Node 2 Node 3 Total
FilePages 2475.63 2395.83 23999.46 23373.22 52244.14* Most of the memory accounting relies on the VMAs and as private-fd of gmem doesn't have a VMA(and that was the design goal), user-space fails to attribute the memory appropriately to the process.
/proc/<qemu pid>/numa_maps
7f528be00000interleave:2-3 file=/memfd:memory-backend-memfd-shared\040(deleted) anon=1070 dirty=1070 mapped=1987 mapmax=256 active=1956 N2=582 N3=1405 kernelpagesize_kB=47f5c90200000interleave:2-3 file=/memfd:rom-backend-memfd-shared\040(deleted)
7f5c90400000interleave:2-3 file=/memfd:rom-backend-memfd-shared\040(deleted) dirty=32 active=0 N2=32 kernelpagesize_kB=4
7f5c90800000interleave:2-3 file=/memfd:rom-backend-memfd-shared\040(deleted) dirty=892 active=0 N2=512 N3=380 kernelpagesize_kB=4
Quoted 5 lines
/proc/<qemu pid>/smaps
7f528be00000-7f5c8be00000 rw-p 00000000 00:01 26629 /memfd:memory-backend-memfd-shared (deleted) 7f5c90200000-7f5c90220000 rw-s 00000000 00:01 44033 /memfd:rom-backend-memfd-shared (deleted) 7f5c90400000-7f5c90420000 rw-s 00000000 00:01 44032 /memfd:rom-backend-memfd-shared (deleted) 7f5c90800000-7f5c90b7c000 rw-s 00000000 00:01 1025 /memfd:rom-backend-memfd-shared (deleted)
This is all expected, and IMO correct. There are no userspace mappings, and so not accounting anything is working as intended.
Quoted 3 lines
* QEMU based NUMA bindings will not work. Memory backend uses
mbind()to set the policy for a particular virtual memory range but gmem private-FD does not have a virtual memory range visible in the host.
Yes, adding a generic fbind() is the way to solve silve.
Hi Sean,
Quoted 24 lines · Sean Christopherson
Quoted 17 lines · Nikunj A. Dadhania
Quoted 8 lines · Sean Christopherson
This is the next iteration of implementing fd-based (instead of vma-based) memory for KVM guests. If you want the full background of why we are doing this, please go read the v10 cover letter[1].
The biggest change from v10 is to implement the backing storage in KVM itself, and expose it via a KVM
ioctl()instead of a "generic" sycall. See link[2] for details on why we pivoted to a KVM-specific approach.Quoted 7 lines
Key word is "biggest". Relative to v10, there are many big changes. Highlights below (I can't remember everything that got changed at this point).
Tagged RFC as there are a lot of empty changelogs, and a lot of missing documentation. And ideally, we'll have even more tests before merging. There are also several gaps/opens (to be discussed in tomorrow's PUCK).
Quoted 5 lines
As per our discussion on the PUCK call, here are the memory/NUMA accounting related observations that I had while working on SNP guest secure page migration:
* gmem allocations are currently treated as file page allocations accounted to the kernel and not to the QEMU process.
Quoted 8 lines
We need to level set on terminology: these are all *stats*, not accounting. That distinction matters because we have wiggle room on stats, e.g. we can probably get away with just about any definition of how
guest_memfdmemory impacts stats, so long as the information that is surfaced to userspace is useful and expected.But we absolutely need to get accounting correct, specifically the allocations need to be correctly accounted in memcg. And unless I'm missing something, nothing in here shows anything related to memcg.
I tried out memcg after creating a separate cgroup for the qemu process. Guest memory is accounted in memcg.
$ egrep -w "file|file_thp|unevictable" memory.stat file 42978775040 file_thp 42949672960 unevictable 42953588736
NUMA allocations are coming from right nodes as set by the numactl.
$ egrep -w "file|file_thp|unevictable" memory.numa_stat file N0=0 N1=20480 N2=21489377280 N3=21489377280 file_thp N0=0 N1=0 N2=21472739328 N3=21476933632 unevictable N0=0 N1=0 N2=21474697216 N3=21478891520
Quoted 9 lines
Quoted 9 lines
Starting an SNP guest with 40G memory with memory interleave between Node2 and Node3
$ numactl -i 2,3 ./bootg_snp.shPID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 242179 root 20 0 40.4g 99580 51676 S 78.0 0.0 0:56.58 qemu-system-x86
Quoted 8 lines
Quoted 1 line
-> Incorrect process resident memory and shared memory is reported
I don't know that I would call these "incorrect". Shared memory definitely is correct, because by definition
guest_memfdisn't shared. RSS is less clear cut; gmem memory is resident in RAM, but if we show gmem in RSS then we'll end up with scenarios where RSS > VIRT, which will be quite confusing for unaware users (I'm assuming the 40g of VIRT here comes from QEMU mapping the shared half of gmem memslots).
I am not sure why will RSS exceed the VIRT, it should be at max 40G (assuming all the memory is private)
As per my experiments with a hack below. MM_FILEPAGES does get accounted to RSS/SHR in top
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 4339 root 20 0 40.4g 40.1g 40.1g S 76.7 16.0 0:13.83 qemu-system-x86
mm/memory.c +1 -0
@@ -166,6 +166,7 @@ void mm_trace_rss_stat(struct mm_struct *mm, int member) {trace_rss_stat (mm, member); } +EXPORT_SYMBOL (mm_trace_rss_stat);/* * Note: this doesn't free the actual pages themselves. That
virt/kvm/guest_mem.c +4 -0
@@ -91,6 +91,10 @@ static struct folio *kvm_gmem_get_folio(struct file *file, pgoff_t index)clear_highpage (folio_page (folio, i)); } +/* Account only once for the first time */ +if (!folio_test_dirty (folio)) +add_mm_counter (current->mm,MM_FILEPAGES ,folio_nr_pages (folio)); +folio_mark_accessed (folio);folio_mark_dirty (folio);folio_mark_uptodate (folio);
We can update the rss_stat appropriately to get correct reporting in userspace.
Quoted 28 lines
Quoted 28 lines
Accounting of the memory happens in the host page fault handler path, but for private guest pages we will never hit that.
* NUMA allocation does use the process mempolicy for appropriate node allocation (Node2 and Node3), but they again do not get attributed to the QEMU process
Every 1.0s: sudo numastat -m -p qemu-system-x86 | egrep -i "qemu|PID|Node|Filepage" gomati: Mon Jul 24 11:51:34 2023
Per-node process memory usage (in MBs)
PID Node 0 Node 1 Node 2 Node 3 Total
242179 (qemu-system-x86) 21.14 1.61 39.44 39.38 101.57Per-node system memory usage (in MBs):
Node 0 Node 1 Node 2 Node 3 Total
FilePages 2475.63 2395.83 23999.46 23373.22 52244.14* Most of the memory accounting relies on the VMAs and as private-fd of gmem doesn't have a VMA(and that was the design goal), user-space fails to attribute the memory appropriately to the process.
/proc/<qemu pid>/numa_maps
7f528be00000interleave:2-3 file=/memfd:memory-backend-memfd-shared\040(deleted) anon=1070 dirty=1070 mapped=1987 mapmax=256 active=1956 N2=582 N3=1405 kernelpagesize_kB=47f5c90200000interleave:2-3 file=/memfd:rom-backend-memfd-shared\040(deleted)
7f5c90400000interleave:2-3 file=/memfd:rom-backend-memfd-shared\040(deleted) dirty=32 active=0 N2=32 kernelpagesize_kB=4
7f5c90800000interleave:2-3 file=/memfd:rom-backend-memfd-shared\040(deleted) dirty=892 active=0 N2=512 N3=380 kernelpagesize_kB=4
Quoted 8 lines
Quoted 5 lines
/proc/<qemu pid>/smaps
7f528be00000-7f5c8be00000 rw-p 00000000 00:01 26629 /memfd:memory-backend-memfd-shared (deleted) 7f5c90200000-7f5c90220000 rw-s 00000000 00:01 44033 /memfd:rom-backend-memfd-shared (deleted) 7f5c90400000-7f5c90420000 rw-s 00000000 00:01 44032 /memfd:rom-backend-memfd-shared (deleted) 7f5c90800000-7f5c90b7c000 rw-s 00000000 00:01 1025 /memfd:rom-backend-memfd-shared (deleted)This is all expected, and IMO correct. There are no userspace mappings, and so not accounting anything is working as intended.
Doesn't sound that correct, if 10 SNP guests are running each using 10GB, how would we know who is using 100GB of memory?
Quoted 6 lines
Quoted 4 lines
* QEMU based NUMA bindings will not work. Memory backend uses
mbind()to set the policy for a particular virtual memory range but gmem private-FD does not have a virtual memory range visible in the host.Yes, adding a generic
fbind()is the way to solve silve.
Regards,
Nikunj
Sean Christopherson
maintainerQuoted 20 lines · Nikunj A. Dadhania
Hi Sean,
Quoted 8 lines · Sean Christopherson
Quoted 8 lines
Starting an SNP guest with 40G memory with memory interleave between Node2 and Node3
$ numactl -i 2,3 ./bootg_snp.shPID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 242179 root 20 0 40.4g 99580 51676 S 78.0 0.0 0:56.58 qemu-system-x86Quoted 8 lines
Quoted 1 line
-> Incorrect process resident memory and shared memory is reported
I don't know that I would call these "incorrect". Shared memory definitely is correct, because by definition
guest_memfdisn't shared. RSS is less clear cut; gmem memory is resident in RAM, but if we show gmem in RSS then we'll end up with scenarios where RSS > VIRT, which will be quite confusing for unaware users (I'm assuming the 40g of VIRT here comes from QEMU mapping the shared half of gmem memslots).
Quoted 2 lines
I am not sure why will RSS exceed the VIRT, it should be at max 40G (assuming all the memory is private)
And also assuming that (a) userspace mmap()'d the shared side of things 1:1 with
private memory and (b) that the shared mappings have not been populated. Those
assumptions will mostly probably hold true for QEMU, but kernel correctness shouldn't depend on assumptions about one specific userspace application.
Quoted 10 lines
Quoted 8 lines
Quoted 5 lines
/proc/<qemu pid>/smaps
7f528be00000-7f5c8be00000 rw-p 00000000 00:01 26629 /memfd:memory-backend-memfd-shared (deleted) 7f5c90200000-7f5c90220000 rw-s 00000000 00:01 44033 /memfd:rom-backend-memfd-shared (deleted) 7f5c90400000-7f5c90420000 rw-s 00000000 00:01 44032 /memfd:rom-backend-memfd-shared (deleted) 7f5c90800000-7f5c90b7c000 rw-s 00000000 00:01 1025 /memfd:rom-backend-memfd-shared (deleted)This is all expected, and IMO correct. There are no userspace mappings, and so not accounting anything is working as intended.
Doesn't sound that correct, if 10 SNP guests are running each using 10GB, how would we know who is using 100GB of memory?
It's correct with respect to what the interfaces show, which is how much memory is *mapped* into userspace.
As I said (or at least tried to say) in my first reply, I am not against exposing memory usage to userspace via stats, only that it's not obvious to me that the existing VMA-based stats are the most appropriate way to surface this information.
Quoted 14 lines · Sean Christopherson
Quoted 8 lines · Nikunj A. Dadhania
Quoted 6 lines · Sean Christopherson
Quoted 5 lines
/proc/<qemu pid>/smaps
7f528be00000-7f5c8be00000 rw-p 00000000 00:01 26629 /memfd:memory-backend-memfd-shared (deleted) 7f5c90200000-7f5c90220000 rw-s 00000000 00:01 44033 /memfd:rom-backend-memfd-shared (deleted) 7f5c90400000-7f5c90420000 rw-s 00000000 00:01 44032 /memfd:rom-backend-memfd-shared (deleted) 7f5c90800000-7f5c90b7c000 rw-s 00000000 00:01 1025 /memfd:rom-backend-memfd-shared (deleted)Quoted 4 lines
Quoted 2 lines
This is all expected, and IMO correct. There are no userspace mappings, and so not accounting anything is working as intended.
Doesn't sound that correct, if 10 SNP guests are running each using 10GB, how would we know who is using 100GB of memory?
Quoted 6 lines
It's correct with respect to what the interfaces show, which is how much memory is *mapped* into userspace.
As I said (or at least tried to say) in my first reply, I am not against exposing memory usage to userspace via stats, only that it's not obvious to me that the existing VMA-based stats are the most appropriate way to surface this information.
Right, then should we think in the line of creating a VM IOCTL for querying current memory usage for guest-memfd ?
We could use memcg for statistics, but then memory cgroup can be disabled and so memcg isn't really a dependable option.
Do you have some ideas on how to expose the memory usage to the user space other than VMA-based stats ?
Regards,
Nikunj
Quoted 94 lines · Nikunj A. Dadhania
Hi Sean,
Quoted 24 lines · Sean Christopherson
Quoted 17 lines · Nikunj A. Dadhania
Quoted 8 lines · Sean Christopherson
This is the next iteration of implementing fd-based (instead of vma-based) memory for KVM guests. If you want the full background of why we are doing this, please go read the v10 cover letter[1].
The biggest change from v10 is to implement the backing storage in KVM itself, and expose it via a KVM
ioctl()instead of a "generic" sycall. See link[2] for details on why we pivoted to a KVM-specific approach.Quoted 7 lines
Key word is "biggest". Relative to v10, there are many big changes. Highlights below (I can't remember everything that got changed at this point).
Tagged RFC as there are a lot of empty changelogs, and a lot of missing documentation. And ideally, we'll have even more tests before merging. There are also several gaps/opens (to be discussed in tomorrow's PUCK).
Quoted 5 lines
As per our discussion on the PUCK call, here are the memory/NUMA accounting related observations that I had while working on SNP guest secure page migration:
* gmem allocations are currently treated as file page allocations accounted to the kernel and not to the QEMU process.
Quoted 8 lines
We need to level set on terminology: these are all *stats*, not accounting. That distinction matters because we have wiggle room on stats, e.g. we can probably get away with just about any definition of how
guest_memfdmemory impacts stats, so long as the information that is surfaced to userspace is useful and expected.But we absolutely need to get accounting correct, specifically the allocations need to be correctly accounted in memcg. And unless I'm missing something, nothing in here shows anything related to memcg.
I tried out memcg after creating a separate cgroup for the qemu process. Guest memory is accounted in memcg.
$ egrep -w "file|file_thp|unevictable" memory.stat file 42978775040 file_thp 42949672960 unevictable 42953588736NUMA allocations are coming from right nodes as set by the numactl.
$ egrep -w "file|file_thp|unevictable" memory.numa_stat file N0=0 N1=20480 N2=21489377280 N3=21489377280 file_thp N0=0 N1=0 N2=21472739328 N3=21476933632 unevictable N0=0 N1=0 N2=21474697216 N3=21478891520Quoted 9 lines
Quoted 9 lines
Starting an SNP guest with 40G memory with memory interleave between Node2 and Node3
$ numactl -i 2,3 ./bootg_snp.shPID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 242179 root 20 0 40.4g 99580 51676 S 78.0 0.0 0:56.58 qemu-system-x86Quoted 8 lines
Quoted 1 line
-> Incorrect process resident memory and shared memory is reported
I don't know that I would call these "incorrect". Shared memory definitely is correct, because by definition
guest_memfdisn't shared. RSS is less clear cut; gmem memory is resident in RAM, but if we show gmem in RSS then we'll end up with scenarios where RSS > VIRT, which will be quite confusing for unaware users (I'm assuming the 40g of VIRT here comes from QEMU mapping the shared half of gmem memslots).I am not sure why will RSS exceed the VIRT, it should be at max 40G (assuming all the memory is private)
As per my experiments with a hack below.
MM_FILEPAGESdoes get accounted to RSS/SHR in topPID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 4339 root 20 0 40.4g 40.1g 40.1g S 76.7 16.0 0:13.83 qemu-system-x86mm/memory.c +1
-0@@ -166,6 +166,7 @@ void mm_trace_rss_stat(struct mm_struct *mm, int member) {trace_rss_stat (mm, member); } +EXPORT_SYMBOL (mm_trace_rss_stat);/* * Note: this doesn't free the actual pages themselves. That virt/kvm/guest_mem.c +0
-0
virt/kvm/guest_mem.c static struct folio *kvm_gmem_get_folio(struct file *file, pgoff_t index) · line 96
Quoted 6 lines
virt/kvm/guest_mem.c +3
-0@@ -91,6 +91,10 @@ static struct folio *kvm_gmem_get_folio(struct file *file, pgoff_t index)clear_highpage (folio_page (folio, i)); } +/* Account only once for the first time */ +if (!folio_test_dirty (folio)) +add_mm_counter (current->mm,MM_FILEPAGES ,folio_nr_pages (folio));
I think this alone would cause "Bad rss-counter" messages when the process exits, because there's no corresponding decrement when page tables are torn down. We would probably have to instantiate the page tables (i.e. with PROT_NONE so userspace can't really do accesses through them) for this to work properly.
So then it wouldn't technically be "unmapped private memory" anymore, but effectively still would be. Maybe there would be more benefits, like the mbind() working. But where would the PROT_NONE page tables be instantiated if there's no page fault? During the ioctl? And is perhaps too much (CPU) work for little benefit? Maybe, but we could say it makes things simpler and can be optimized later?
Anyway IMHO it would be really great if the memory usage was attributable the usual way without new IOCTLs or something. Each time some memory appears "unaccounted" somewhere, it causes confusion.
Quoted 55 lines
virt/kvm/guest_mem.c +1
-0+folio_mark_accessed (folio);folio_mark_dirty (folio);folio_mark_uptodate (folio);We can update the
rss_statappropriately to get correct reporting in userspace.Quoted 28 lines
Quoted 28 lines
Accounting of the memory happens in the host page fault handler path, but for private guest pages we will never hit that.
* NUMA allocation does use the process mempolicy for appropriate node allocation (Node2 and Node3), but they again do not get attributed to the QEMU process
Every 1.0s: sudo numastat -m -p qemu-system-x86 | egrep -i "qemu|PID|Node|Filepage" gomati: Mon Jul 24 11:51:34 2023
Per-node process memory usage (in MBs)
PID Node 0 Node 1 Node 2 Node 3 Total
242179 (qemu-system-x86) 21.14 1.61 39.44 39.38 101.57Per-node system memory usage (in MBs):
Node 0 Node 1 Node 2 Node 3 Total
FilePages 2475.63 2395.83 23999.46 23373.22 52244.14* Most of the memory accounting relies on the VMAs and as private-fd of gmem doesn't have a VMA(and that was the design goal), user-space fails to attribute the memory appropriately to the process.
/proc/<qemu pid>/numa_maps
7f528be00000interleave:2-3 file=/memfd:memory-backend-memfd-shared\040(deleted) anon=1070 dirty=1070 mapped=1987 mapmax=256 active=1956 N2=582 N3=1405 kernelpagesize_kB=47f5c90200000interleave:2-3 file=/memfd:rom-backend-memfd-shared\040(deleted)
7f5c90400000interleave:2-3 file=/memfd:rom-backend-memfd-shared\040(deleted) dirty=32 active=0 N2=32 kernelpagesize_kB=4
7f5c90800000interleave:2-3 file=/memfd:rom-backend-memfd-shared\040(deleted) dirty=892 active=0 N2=512 N3=380 kernelpagesize_kB=4Quoted 8 lines
Quoted 5 lines
/proc/<qemu pid>/smaps
7f528be00000-7f5c8be00000 rw-p 00000000 00:01 26629 /memfd:memory-backend-memfd-shared (deleted) 7f5c90200000-7f5c90220000 rw-s 00000000 00:01 44033 /memfd:rom-backend-memfd-shared (deleted) 7f5c90400000-7f5c90420000 rw-s 00000000 00:01 44032 /memfd:rom-backend-memfd-shared (deleted) 7f5c90800000-7f5c90b7c000 rw-s 00000000 00:01 1025 /memfd:rom-backend-memfd-shared (deleted)This is all expected, and IMO correct. There are no userspace mappings, and so not accounting anything is working as intended.
Doesn't sound that correct, if 10 SNP guests are running each using 10GB, how would we know who is using 100GB of memory?
Quoted 6 lines
Quoted 4 lines
* QEMU based NUMA bindings will not work. Memory backend uses
mbind()to set the policy for a particular virtual memory range but gmem private-FD does not have a virtual memory range visible in the host.Yes, adding a generic
fbind()is the way to solve silve.Regards,
Nikunj
Sean Christopherson
maintainerDropped non-KVM folks from Cc: so as not to bother them too much.
Quoted 8 lines · Sean Christopherson
This is the next iteration of implementing fd-based (instead of vma-based) memory for KVM guests. If you want the full background of why we are doing this, please go read the v10 cover letter[1].
The biggest change from v10 is to implement the backing storage in KVM itself, and expose it via a KVM
ioctl()instead of a "generic" sycall. See link[2] for details on why we pivoted to a KVM-specific approach.
Quoted 7 lines
Key word is "biggest". Relative to v10, there are many big changes. Highlights below (I can't remember everything that got changed at this point).
Tagged RFC as there are a lot of empty changelogs, and a lot of missing documentation. And ideally, we'll have even more tests before merging. There are also several gaps/opens (to be discussed in tomorrow's PUCK).
I've pushed this to
https://github.com/kvm-x86/linux/tree/guest_memfd
along with Isaku's fix for the lock ordering bug on top.
As discussed at PUCK, I'll apply fixes/tweaks/changes on top until development stabilizes, and will only squash/fixup when we're ready to post v12 for broad review.
Please "formally" post patches just like you normally would do, i.e. don't *just* repond to the buggy mail (though that is also helpful). Standalone patches make it easier for me to manage things via lore/b4.
If you can, put gmem or guest_memfd inside the square braces, e.g.
[PATCH gmem] KVM: <shortlog>
so that it's obvious the patch is intended for the guest_memfd branch. For fixes, please also be sure to use Fixes: tags and split patches to fix exactly one base commit, again to make my life easier.
I'll likely add my own annotations when applying, e.g. [FIXUP] and whatnot, but that's purely notes for myself for the future squash/rebase.
Thanks!
Sean Christopherson
maintainerQuoted 28 lines · Sean Christopherson
This is the next iteration of implementing fd-based (instead of vma-based) memory for KVM guests. If you want the full background of why we are doing this, please go read the v10 cover letter[1].
The biggest change from v10 is to implement the backing storage in KVM itself, and expose it via a KVM
ioctl()instead of a "generic" sycall. See link[2] for details on why we pivoted to a KVM-specific approach.Key word is "biggest". Relative to v10, there are many big changes. Highlights below (I can't remember everything that got changed at this point).
Tagged RFC as there are a lot of empty changelogs, and a lot of missing documentation. And ideally, we'll have even more tests before merging. There are also several gaps/opens (to be discussed in tomorrow's PUCK).
v11:
- Test private<=>shared conversions *without* doingfallocate()
-PUNCH_HOLEall memory between iterations of the conversion test so that KVM doesn't retain pages in theguest_memfd
- Rename hugepage control to be a very genericALLOW_HUGEPAGE, instead of giving it a THP or PMD specific name.
- Fold in fixes from a lot of people (thank you!)
- Zap SPTEs *before* updating attributes to ensure no weirdness, e.g. if KVM handles a page fault and looks at inconsistent attributes
- Refactor MMU interaction with attributes updates to reuse much of KVM's framework formmu_notifiers.
Quoted 2 lines
[1] https://lore.kernel.org/all/[email protected]
[2] https://lore.kernel.org/all/[email protected]
Trimmed the Cc substantially to discuss what needs to be done (within our control) to have a chance of landing this "soon".
We've chipped away at the todo list a bit, but there are still several non-trivial things that need to get addressed before we can merge guest_memfd(). If we move *really* fast, e.g. get everything address in less than 3 weeks, we have an outside chance at hitting 6.7. But honestly, I think it 6.7 is extremely unlikely.
For 6.8, we'll be in good shape if we can get a non-RFC posted in the next ~6 weeks, i.e. by end of September, though obviously the sooner the better. If we slip much beyond that, 6.8 is going to be tough due to people disappearing for year-end stuff and holidays, i.e. we won't have enough time to address feedback _and_ get a another round of reviews.
Speaking purely from a personal perspective, I really, really want to hit 6.8 so that this doesn't drag into 2024.
Loosely ordered by size and urgency (bigger, more urgent stuff first). Please holler if I've missed something (taking notes is my Achilles heel).
Filemap vs. xarray
------------------
This is the main item that needs attention. I don't want to merge guest_memfd() without doing this comparison, as not using filemap means we don't need AS_UNMOVABLE. Arguably we could merge a filemap implementation without AS_UNMOVABLE and just eat the suboptimal behavior, but not waiting a little while longer to do everything we can to get this right the first time seems ridiculous after we've been working on this for literally years.
Paolo was going to work on an axarray implementation, but AFAIK he hasn't done anything yet. We (Google) don't have anyone available to work on an xarray implementation for several weeks (at best), so if anyone has the bandwidth and desire to take stab at an xarray implementation, please speak up.
kvm_gmem_error_page()
---------------------
As pointed out by Vishal[*], guest_memfd()'s error/poison handling is garbage. KVM needs to unmap, check for poison, and probably also restrict the allowed mapping size if a partial page is poisoned.
This item also needs actually testing, e.g. via error injection. Writing a proper selftest may not be feasible, but at a bare minimum, someone needs to manually verify an error on a guest_memfd() can get routed all the way into the guest, e.g. as an #MC on x86.
This needs an owner. I'm guessing 2-3 weeks? Though I tend to be overly optimistic when sizing these things...
[*] https://lore.kernel.org/all/CAGtprH9a2jX-hdww9GPuMrO9noNeXkoqE8oejtVn2vD0AZa3zA@mail.gmail.com
Documentation
-------------
Obviously a must have. AFAIK, no one is "officially" signed up to work on this. I honestly haven't looked at the document in recent versions, so I have no idea how much effort is required.
Fully anonymous inode vs. proper filesystem
-------------------------------------------
This is another one that needs to get sorted out before merging, but it should be a much smaller task (a day or two). I will get to this in a few weeks unless someone beats me to the punch.
KVM_CAP_GUEST_MEMFD
-------------------
New ioctl() needs a new cap. Trivial, just capturing here so I don't forget.
Changelogs
----------
This one is on me, though I will probably procrastinate until all the other todo items are close to being finished.
Tests
-----
I would really like to have a test that verifies KVM actually installs hugepages, but I'm ok merging without such a test, mainly because I suspect it will be annoyingly difficult to end up with a test that isn't flaky.
Beyond that, and the aforementioned memory poisoining, IMO, we have enough test coverage. I am always open to more tests, but I don't think adding more coverage is a must have for merging.
.release_folio and .invalidate_folio versus .evict_inode
--------------------------------------------------------
I think we're good on this one? IIRC, without a need to "clean" physical memory (SNP and TDX), we don't need to do anything special.
Mike or Ackerley, am I forgetting anything?
NUMA
----
I am completely comfortable doing nothing as part of the initial merge. We have line of sight to supporting NUMA policies in the form of fbind(), and I would be quite surprised if we get much pushback on implementing fbind().
RSS stats
---------
My preference is to not do anything in the initial implementation, and defer any changes until later. IMO, while odd, not capturing guest_memfd() in RSS is acceptable as there are no virtual mappings to account. I completely agree that we would ideally surface the memory usage to userspace in some way, but I don't think it's so critical that it needs to happen as part of the initial merge.
Intrahost migration support
---------------------------
Ackerley's RFC[*] is enough for me to have confidence that we can support intrahost migration without having to rework the ABI. Please holler if you disagree.
[*] https://lkml.kernel.org/r/cover.1691446946.git.ackerleytng%40google.com
Quoted 11 lines · Sean Christopherson
Filemap vs. xarray
------------------
This is the main item that needs attention. I don't want to mergeguest_memfd()without doing this comparison, as not using filemap means we don't needAS_UNMOVABLE. Arguably we could merge a filemap implementation withoutAS_UNMOVABLEand just eat the suboptimal behavior, but not waiting a little while longer to do everything we can to get this right the first time seems ridiculous after we've been working on this for literally years.
Quoted 4 lines
Paolo was going to work on an axarray implementation, but AFAIK he hasn't done anything yet. We (Google) don't have anyone available to work on an xarray implementation for several weeks (at best), so if anyone has the bandwidth and desire to take stab at an xarray implementation, please speak up.
I can do some experiments in the following weeks on the xarray direction. I'm not quite confident I understood all what Paolo originally wanted to do, so questions may have.
Chao