1G page support for guest_memfd
Subsystems (10)
Re: [RFC PATCH 39/39] KVM: guest_memfd: Dynamically split/reconstruct HugeTLB page
In reply to Ackerley TngQuoted 211 lines · Ackerley Tng
Quoted 149 lines · Yan Zhao
Quoted 148 lines · Ackerley Tng
Quoted 133 lines · Yan Zhao
Quoted 122 lines · Ackerley Tng
Quoted 112 lines · Vishal Annapurve
Quoted 81 lines · Yan Zhao
Quoted 62 lines · Chenyi Qiang
Quoted 50 lines · Yan Zhao
Quoted 49 lines · Yan Zhao
Quoted 47 lines · Ackerley Tng
Quoted 23 lines · Yan Zhao
Quoted 20 lines · Ackerley Tng
+/*
+ * Allocates and then caches a folio in the filemap. Returns a folio with
+ * refcount of 2: 1 after allocation, and 1 taken by the filemap.
+ */
+static struct folio *kvm_gmem_hugetlb_alloc_and_cache_folio(struct inode *inode,
+ pgoff_t index)
+{
+ struct kvm_gmem_hugetlb *hgmem;
+ pgoff_t aligned_index;
+ struct folio *folio;
+ int nr_pages;
+ int ret;
+
+ hgmem = kvm_gmem_hgmem(inode);
+ folio = kvm_gmem_hugetlb_alloc_folio(hgmem->h, hgmem->spool);
+ if (IS_ERR(folio))
+ return folio;
+
+ nr_pages = 1UL << huge_page_order(hgmem->h);
+ aligned_index = round_down(index, nr_pages);Maybe a gap here.
Quoted 3 lines
When a
guest_memfdis bound to a slot whereslot->base_gfnis not aligned to 2M/1G andslot->gmem.pgoffis 0, even if an index is 2M/1G aligned, the corresponding GFN is not 2M/1G aligned.Thanks for looking into this.
In 1G page support for
guest_memfd, the offset and size are always hugepage aligned to the hugepage size requested atguest_memfdcreation time, and it is true that when binding to a memslot,slot->base_gfnandslot->npagesmay not be hugepage aligned.Quoted 3 lines
However, TDX requires that private huge pages be 2M aligned in GFN.
IIUC other factors also contribute to determining the mapping level in the guest page tables, like
lpage_infoand .private_max_mapping_level() inkvm_x86_ops.If
slot->base_gfnandslot->npagesare not hugepage aligned,lpage_infowill track that and not allow faulting into guest page tables at higher granularity.Quoted 8 lines
Quoted 8 lines
lpage_infoonly checks the alignments ofslot->base_gfnandslot->base_gfn+ npages. e.g.,if
slot->base_gfnis 8K, npages is 8M, then for this slot, lpage_info[2M][0].disallow_lpage = 1, which is for GFN [4K, 2M+8K); lpage_info[2M][1].disallow_lpage = 0, which is for GFN [2M+8K, 4M+8K); lpage_info[2M][2].disallow_lpage = 0, which is for GFN [4M+8K, 6M+8K); lpage_info[2M][3].disallow_lpage = 1, which is for GFN [6M+8K, 8M+8K);Quoted 6 lines
Should it be?
lpage_info[2M][0].disallow_lpage = 1, which is for GFN [8K, 2M); lpage_info[2M][1].disallow_lpage = 0, which is for GFN [2M, 4M); lpage_info[2M][2].disallow_lpage = 0, which is for GFN [4M, 6M); lpage_info[2M][3].disallow_lpage = 0, which is for GFN [6M, 8M); lpage_info[2M][4].disallow_lpage = 1, which is for GFN [8M, 8M+8K);Right. Good catch. Thanks!
Let me update the example as below:
slot->base_gfnis 2 (for GPA 8KB), npages 2000 (for a 8MB range)lpage_info[2M][0].disallow_lpage = 1, which is for GPA [8KB, 2MB); lpage_info[2M][1].disallow_lpage = 0, which is for GPA [2MB, 4MB); lpage_info[2M][2].disallow_lpage = 0, which is for GPA [4MB, 6MB); lpage_info[2M][3].disallow_lpage = 0, which is for GPA [6MB, 8MB); lpage_info[2M][4].disallow_lpage = 1, which is for GPA [8MB, 8MB+8KB);
Quoted 26 lines
lpage_infoindicates that a 2MB mapping is alllowed to cover GPA 4MB and GPA 4MB+16KB. However, theiraligned_indexvalues leadguest_memfdto allocate two 2MB folios, whose physical addresses may not be contiguous.Additionally, if the guest accesses two GPAs, e.g., GPA 2MB+8KB and GPA 4MB, KVM could create two 2MB mappings to cover GPA ranges [2MB, 4MB), [4MB, 6MB). However,
guest_memfdjust allocates the same 2MB folio for both faults.Quoted 17 lines
Quoted 17 lines
Quoted 9 lines
---------------------------------------------------------
| | | | | | | | | 8K 2M 2M+8K 4M 4M+8K 6M 6M+8K 8M 8M+8KFor GFN 6M and GFN 6M+4K, as they both belong to lpage_info[2M][2], huge page is allowed. Also, they have the same
aligned_index2 inguest_memfd. So,guest_memfdallocates the same huge folio of 2M order for them.Sorry, sent too fast this morning. The example is not right. The correct one is:
For GFN 4M and GFN 4M+16K,
lpage_infoindicates that 2M is allowed. So, KVM will create a 2M mapping for them.However, in
guest_memfd, GFN 4M and GFN 4M+16K do not correspond to the same 2M folio and physical addresses may not be contiguous.Then during binding, guest memfd offset misalignment with hugepage should be same as gfn misalignment. i.e.
Quoted 7 lines
(offset & ~huge_page_mask(h)) == ((
slot->base_gfn<<PAGE_SHIFT) & ~huge_page_mask(h));For non
guest_memfdbacked scenarios, KVM allows slot gfn ranges that are not hugepage aligned, soguest_memfdshould also be able to support non-hugepage aligned memslots.Quoted 5 lines
I drew up a picture [1] which hopefully clarifies this.
Thanks for pointing this out, I understand better now and we will add an extra constraint during memslot binding of
guest_memfdto check that gfn offsets within a hugepage must beguest_memfdoffsets.I'm a bit confused.
As "index = gfn -
slot->base_gfn+slot->gmem.pgoff", do you mean you are going to force "slot->base_gfn==slot->gmem.pgoff" ?Quoted 9 lines
For some memory region, e.g., "pc.ram", it's divided into 2 parts:
- one with offset 0, size 0x80000000(2G),
positioned at GPA 0, which is below GPA 4G;
- one with offset 0x80000000(2G), size 0x80000000(2G), positioned at GPA 0x100000000(4G), which is above GPA 4G.For the second part, its
slot->base_gfnis0x100000000, whileslot->gmem.pgoffis0x80000000.Nope I don't mean to enforce that they are equal, we just need the offsets within the page to be equal.
Quoted 8 lines
Quoted 6 lines
I edited Vishal's code snippet, perhaps it would help explain better:
page_sizeis the size of the hugepage, so in our example,page_size = SZ_2M; page_mask = ~(page_size - 1);
page_mask=page_size- 1 ?Yes, thank you!
Quoted 35 lines
Quoted 6 lines
offset_within_page = slot->gmem.pgoff & page_mask; gfn_within_page = (slot->base_gfn << PAGE_SHIFT) & page_mask;We will enforce that
offset_within_page == gfn_within_page;For "pc.ram", if it has 2.5G below 4G, it would be configured as follows
- slot 1: slot->gmem.pgoff=0, base GPA 0, size=2.5G
- slot 2: slot->gmem.pgoff=2.5G, base GPA 4G, size=1.5GWhen binding these two slots to the same
guest_memfdcreated with flagKVM_GUEST_MEMFD_HUGE_1GB:
- binding the 1st slot will succeed;
- binding the 2nd slot will fail.What options does userspace have in this scenario?
It can't reduce the flag toKVM_GUEST_MEMFD_HUGE_2MB. Adjusting the gmem.pgoff isn't ideal either.What about something similar as below?
virt/kvm/guest_memfd.c +3
-2@@ -1842,8 +1842,16 @@ __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot, } *pfn =folio_file_pfn (folio, index); -if (max_order) - *max_order =folio_order (folio); +if (max_order) { +int order; +virt/kvm/guest_memfd.c __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot, · line 1857
Quoted 10 lines
virt/kvm/guest_memfd.c +7
-0+ order =folio_order (folio); + +while (order >0 && ((slot->base_gfn ^ slot->gmem.pgoff) & ((1 << order) -1 ))) + order--; + + *max_order = order; + } *is_prepared =folio_test_uptodate (folio);return folio;Vishal was wondering how this is working before
guest_memfdwas introduced, for other backing memory like HugeTLB.
Quoted 4 lines
I then poked around and found this [1]. I will be adding a similar check for any slot where
kvm_slot_can_be_private(slot).Yan, that should work, right?
No, I don't think the checking of ugfn [1] should work.
1. Even for slots bound to in-place-conversion guest_memfd (i.e. shared memory are allocated from guest_memfd), the slot->userspace_addr does not necessarily have the same offset as slot->gmem.pgoff. Even if we audit the offset in kvm_gmem_bind(), userspace could invoke munmap() and mmap() afterwards, causing slot->userspace_addr to point to a different offset.
2. for slots bound to guest_memfd that do not support in-place-conversion, shared memory is allocated from a different backend. Therefore, checking "slot->base_gfn ^ slot->gmem.pgoff" is required for private memory. The check is currently absent because guest_memfd supports 4K only.
Quoted 11 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Adding checks at binding time will allow hugepage-unaligned offsets (to be at parity with non-guest_memfd backing memory) but still fix this issue.
lpage_infowill make sure that ranges near the bounds will be fragmented, but the hugepages in the middle will still be mappable as hugepages.
Re: [RFC PATCH 39/39] KVM: guest_memfd: Dynamically split/reconstruct HugeTLB page
Quoted 48 lines · Yan Zhao
Quoted 36 lines
Quoted 22 lines
<snip>
What options does userspace have in this scenario?
It can't reduce the flag toKVM_GUEST_MEMFD_HUGE_2MB. Adjusting the gmem.pgoff isn't ideal either.What about something similar as below?
virt/kvm/guest_memfd.c +3
-2@@ -1842,8 +1842,16 @@ __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot, } *pfn =folio_file_pfn (folio, index); -if (max_order) - *max_order =folio_order (folio); +if (max_order) { +int order; +virt/kvm/guest_memfd.c __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot, · line 1857
Quoted 10 lines
virt/kvm/guest_memfd.c +7
-0+ order =folio_order (folio); + +while (order >0 && ((slot->base_gfn ^ slot->gmem.pgoff) & ((1 << order) -1 ))) + order--; + + *max_order = order; + } *is_prepared =folio_test_uptodate (folio);return folio;Vishal was wondering how this is working before
guest_memfdwas introduced, for other backing memory like HugeTLB.Quoted 4 lines
I then poked around and found this [1]. I will be adding a similar check for any slot where
kvm_slot_can_be_private(slot).Yan, that should work, right?
No, I don't think the checking of ugfn [1] should work.
1. Even for slots bound to in-place-conversion
guest_memfd(i.e. shared memory are allocated fromguest_memfd), theslot->userspace_addrdoes not necessarily have the same offset asslot->gmem.pgoff. Even if we audit the offset inkvm_gmem_bind(), userspace could invokemunmap()andmmap()afterwards, causingslot->userspace_addrto point to a different offset.
Quoted 6 lines
2. for slots bound to
guest_memfdthat do not support in-place-conversion, shared memory is allocated from a different backend. Therefore, checking "slot->base_gfn^slot->gmem.pgoff" is required for private memory. The check is currently absent becauseguest_memfdsupports 4K only.
Let me clarify, I meant these changes:
arch/x86/kvm/x86.c +14 -5
@@ -12938,6 +12938,11 @@ int memslot_rmap_alloc(struct kvm_memory_slot *slot, unsigned long npages)return 0 ; } +static inline bool kvm_is_level_aligned (u64 value,int level) +{ +return IS_ALIGNED (value,KVM_PAGES_PER_HPAGE (level)); +} +static int kvm_alloc_memslot_metadata (struct kvm *kvm,struct kvm_memory_slot *slot) {
@@ -12971,16 +12976,20 @@ static int kvm_alloc_memslot_metadata(struct kvm *kvm, slot->arch.lpage_info[i -1 ] = linfo; -if (slot->base_gfn & (KVM_PAGES_PER_HPAGE (level) -1 )) +if (!kvm_is_level_aligned (slot->base_gfn, level)) linfo[0 ].disallow_lpage =1 ; -if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE (level) -1 )) +if (!kvm_is_level_aligned (slot->base_gfn + npages, level)) linfo[lpages -1 ].disallow_lpage =1 ; ugfn = slot->userspace_addr >>PAGE_SHIFT ;/* -* If the gfn and userspace address are not aligned wrt each -* other, disable large page support for this slot. +* If the gfn and userspace address are not aligned or if gfn +* and guest_memfd offset are not aligned wrt each other, +* disable large page support for this slot. */ -if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE (level) -1 )) { +if (!kvm_is_level_aligned (slot->base_gfn ^ ugfn, level) || + (kvm_slot_can_be_private (slot) && + !kvm_is_level_aligned (slot->base_gfn ^ slot->gmem.pgoff, + level))) {unsigned long j;for (j =0 ; j < lpages; ++j)
This does not rely on the ugfn check, but adds a similar check for gmem.pgoff.
I think this should take care of case (1.), for guest_memfds going to be used for both shared and private memory. Userspace can't update slot->userspace_addr, since guest_memfd memslots cannot be updated and can only be deleted.
If userspace re-uses slot->userspace_addr for some other memory address without deleting and re-adding a memslot,
+ KVM's access to memory should still be fine, since after the recent discussion at guest_memfd upstream call, KVM's guest faults will always go via fd+offset and KVM's access won't be disrupted there. Whatever checking done at memslot binding time will still be valid.
+ Host's access and other accesses (e.g. instruction emulation, which uses slot->userspace_addr) to guest memory will be broken, but I think there's nothing protecting against that. The same breakage would happen for non-guest_memfd memslot.
p.s. I will be adding the validation as you suggested [1], though that shouldn't make a difference here, since the above check directly validates against gmem.pgoff.
Regarding 2., checking this checks against gmem.pgoff and should handle that as well.
[1] https://lore.kernel.org/all/[email protected]/
I prefer checking at binding time because it aligns with the ugfn check that is already there, and avoids having to check at every fault.
Quoted 11 lines
Quoted 11 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Adding checks at binding time will allow hugepage-unaligned offsets (to be at parity with non-guest_memfd backing memory) but still fix this issue.
lpage_infowill make sure that ranges near the bounds will be fragmented, but the hugepages in the middle will still be mappable as hugepages.
Re: [RFC PATCH 39/39] KVM: guest_memfd: Dynamically split/reconstruct HugeTLB page
Quoted 102 lines · Ackerley Tng
Quoted 48 lines · Yan Zhao
Quoted 36 lines
Quoted 22 lines
<snip>
What options does userspace have in this scenario?
It can't reduce the flag toKVM_GUEST_MEMFD_HUGE_2MB. Adjusting the gmem.pgoff isn't ideal either.What about something similar as below?
virt/kvm/guest_memfd.c +3
-2@@ -1842,8 +1842,16 @@ __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot, } *pfn =folio_file_pfn (folio, index); -if (max_order) - *max_order =folio_order (folio); +if (max_order) { +int order; +virt/kvm/guest_memfd.c __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot, · line 1857
Quoted 10 lines
virt/kvm/guest_memfd.c +7
-0+ order =folio_order (folio); + +while (order >0 && ((slot->base_gfn ^ slot->gmem.pgoff) & ((1 << order) -1 ))) + order--; + + *max_order = order; + } *is_prepared =folio_test_uptodate (folio);return folio;Vishal was wondering how this is working before
guest_memfdwas introduced, for other backing memory like HugeTLB.Quoted 4 lines
I then poked around and found this [1]. I will be adding a similar check for any slot where
kvm_slot_can_be_private(slot).Yan, that should work, right?
No, I don't think the checking of ugfn [1] should work.
1. Even for slots bound to in-place-conversion
guest_memfd(i.e. shared memory are allocated fromguest_memfd), theslot->userspace_addrdoes not necessarily have the same offset asslot->gmem.pgoff. Even if we audit the offset inkvm_gmem_bind(), userspace could invokemunmap()andmmap()afterwards, causingslot->userspace_addrto point to a different offset.Quoted 6 lines
2. for slots bound to
guest_memfdthat do not support in-place-conversion, shared memory is allocated from a different backend. Therefore, checking "slot->base_gfn^slot->gmem.pgoff" is required for private memory. The check is currently absent becauseguest_memfdsupports 4K only.Let me clarify, I meant these changes:
arch/x86/kvm/x86.c +14
-5@@ -12938,6 +12938,11 @@ int memslot_rmap_alloc(struct kvm_memory_slot *slot, unsigned long npages)return 0 ; } +static inline bool kvm_is_level_aligned (u64 value,int level) +{ +return IS_ALIGNED (value,KVM_PAGES_PER_HPAGE (level)); +} +static int kvm_alloc_memslot_metadata (struct kvm *kvm,struct kvm_memory_slot *slot) {@@ -12971,16 +12976,20 @@ static int kvm_alloc_memslot_metadata(struct kvm *kvm, slot->arch.lpage_info[i -1 ] = linfo; -if (slot->base_gfn & (KVM_PAGES_PER_HPAGE (level) -1 )) +if (!kvm_is_level_aligned (slot->base_gfn, level)) linfo[0 ].disallow_lpage =1 ; -if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE (level) -1 )) +if (!kvm_is_level_aligned (slot->base_gfn + npages, level)) linfo[lpages -1 ].disallow_lpage =1 ; ugfn = slot->userspace_addr >>PAGE_SHIFT ;/* -* If the gfn and userspace address are not aligned wrt each -* other, disable large page support for this slot. +* If the gfn and userspace address are not aligned or if gfn +* and guest_memfd offset are not aligned wrt each other, +* disable large page support for this slot. */ -if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE (level) -1 )) { +if (!kvm_is_level_aligned (slot->base_gfn ^ ugfn, level) || + (kvm_slot_can_be_private (slot) && + !kvm_is_level_aligned (slot->base_gfn ^ slot->gmem.pgoff, + level))) {unsigned long j;for (j =0 ; j < lpages; ++j)
Quoted 1 line
This does not rely on the ugfn check, but adds a similar check for gmem.pgoff.
In the case of shared memory is not allocated from guest_memfd, (e.g. with the current upstream code), the checking of gmem.pgoff here will disallow huge page of shared memory even if "slot->base_gfn ^ ugfn" is aligned.
Quoted 5 lines
I think this should take care of case (1.), for
guest_memfdsgoing to be used for both shared and private memory. Userspace can't updateslot->userspace_addr, sinceguest_memfdmemslots cannot be updated and can only be deleted.
Quoted 8 lines
If userspace re-uses
slot->userspace_addrfor some other memory address without deleting and re-adding a memslot,+ KVM's access to memory should still be fine, since after the recent discussion at
guest_memfdupstream call, KVM's guest faults will always go via fd+offset and KVM's access won't be disrupted there. Whatever checking done at memslot binding time will still be valid.
Could the offset of shared memory and offset of private memory be different if userspace re-uses slot->userspace_addr without deleting and re-adding a memslot?
Then though the two offsets are validated as equal in kvm_gmem_bind(), they may differ later on.
Quoted 4 lines
+ Host's access and other accesses (e.g. instruction emulation, which uses
slot->userspace_addr) to guest memory will be broken, but I think there's nothing protecting against that. The same breakage would happen for non-guest_memfd memslot.
Why is host access broken in non-guest_memfd case? The HVA is still a valid one in QEMU's mmap-ed address space.
Quoted 23 lines
p.s. I will be adding the validation as you suggested [1], though that shouldn't make a difference here, since the above check directly validates against gmem.pgoff.
Regarding 2., checking this checks against gmem.pgoff and should handle that as well.
[1] https://lore.kernel.org/all/[email protected]/
I prefer checking at binding time because it aligns with the ugfn check that is already there, and avoids having to check at every fault.
Quoted 11 lines
Quoted 11 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Adding checks at binding time will allow hugepage-unaligned offsets (to be at parity with non-guest_memfd backing memory) but still fix this issue.
lpage_infowill make sure that ranges near the bounds will be fragmented, but the hugepages in the middle will still be mappable as hugepages.
Re: [RFC PATCH 39/39] KVM: guest_memfd: Dynamically split/reconstruct HugeTLB page
Quoted 101 lines · Yan Zhao
Quoted 100 lines · Ackerley Tng
Quoted 48 lines · Yan Zhao
Quoted 36 lines
Quoted 22 lines
<snip>
What options does userspace have in this scenario?
It can't reduce the flag toKVM_GUEST_MEMFD_HUGE_2MB. Adjusting the gmem.pgoff isn't ideal either.What about something similar as below?
virt/kvm/guest_memfd.c +3
-2@@ -1842,8 +1842,16 @@ __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot, } *pfn =folio_file_pfn (folio, index); -if (max_order) - *max_order =folio_order (folio); +if (max_order) { +int order; +virt/kvm/guest_memfd.c __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot, · line 1857
Quoted 10 lines
virt/kvm/guest_memfd.c +7
-0+ order =folio_order (folio); + +while (order >0 && ((slot->base_gfn ^ slot->gmem.pgoff) & ((1 << order) -1 ))) + order--; + + *max_order = order; + } *is_prepared =folio_test_uptodate (folio);return folio;Vishal was wondering how this is working before
guest_memfdwas introduced, for other backing memory like HugeTLB.Quoted 4 lines
I then poked around and found this [1]. I will be adding a similar check for any slot where
kvm_slot_can_be_private(slot).Yan, that should work, right?
No, I don't think the checking of ugfn [1] should work.
1. Even for slots bound to in-place-conversion
guest_memfd(i.e. shared memory are allocated fromguest_memfd), theslot->userspace_addrdoes not necessarily have the same offset asslot->gmem.pgoff. Even if we audit the offset inkvm_gmem_bind(), userspace could invokemunmap()andmmap()afterwards, causingslot->userspace_addrto point to a different offset.Quoted 6 lines
2. for slots bound to
guest_memfdthat do not support in-place-conversion, shared memory is allocated from a different backend. Therefore, checking "slot->base_gfn^slot->gmem.pgoff" is required for private memory. The check is currently absent becauseguest_memfdsupports 4K only.Let me clarify, I meant these changes:
arch/x86/kvm/x86.c +14
-5@@ -12938,6 +12938,11 @@ int memslot_rmap_alloc(struct kvm_memory_slot *slot, unsigned long npages)return 0 ; } +static inline bool kvm_is_level_aligned (u64 value,int level) +{ +return IS_ALIGNED (value,KVM_PAGES_PER_HPAGE (level)); +} +static int kvm_alloc_memslot_metadata (struct kvm *kvm,struct kvm_memory_slot *slot) {@@ -12971,16 +12976,20 @@ static int kvm_alloc_memslot_metadata(struct kvm *kvm, slot->arch.lpage_info[i -1 ] = linfo; -if (slot->base_gfn & (KVM_PAGES_PER_HPAGE (level) -1 )) +if (!kvm_is_level_aligned (slot->base_gfn, level)) linfo[0 ].disallow_lpage =1 ; -if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE (level) -1 )) +if (!kvm_is_level_aligned (slot->base_gfn + npages, level)) linfo[lpages -1 ].disallow_lpage =1 ; ugfn = slot->userspace_addr >>PAGE_SHIFT ;/* -* If the gfn and userspace address are not aligned wrt each -* other, disable large page support for this slot. +* If the gfn and userspace address are not aligned or if gfn +* and guest_memfd offset are not aligned wrt each other, +* disable large page support for this slot. */ -if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE (level) -1 )) { +if (!kvm_is_level_aligned (slot->base_gfn ^ ugfn, level) || + (kvm_slot_can_be_private (slot) && + !kvm_is_level_aligned (slot->base_gfn ^ slot->gmem.pgoff, + level))) {unsigned long j;
Quoted 7 lines
Quoted 3 lines
arch/x86/kvm/x86.c +0
-0for (j =0 ; j < lpages; ++j)This does not rely on the ugfn check, but adds a similar check for gmem.pgoff.
In the case of shared memory is not allocated from
guest_memfd, (e.g. with the current upstream code), the checking of gmem.pgoff here will disallow huge page of shared memory even if "slot->base_gfn^ ugfn" is aligned.
Thanks, I get it now. What you mean is that the memslot could have been set up such that
+ slot->userspace_addr is aligned with slot->base_gfn, to be used for shared memory, and
+ slot->gmem.pgoff is not aligned with slot->base_gfn, to be used for private memory
and this check would disallow huge page mappings even though this memslot was going to only be used for shared memory.
The only way to fix this would indeed be a runtime check, since the shared/private status can change at runtime.
I think it is okay that this check is stricter than necessary, since it just results in mapping without huge pages.
What do you think?
Quoted 8 lines
Quoted 8 lines
I think this should take care of case (1.), for
guest_memfdsgoing to be used for both shared and private memory. Userspace can't updateslot->userspace_addr, sinceguest_memfdmemslots cannot be updated and can only be deleted.If userspace re-uses
slot->userspace_addrfor some other memory address without deleting and re-adding a memslot,
Quoted 8 lines
Quoted 5 lines
+ KVM's access to memory should still be fine, since after the recent discussion at
guest_memfdupstream call, KVM's guest faults will always go via fd+offset and KVM's access won't be disrupted there. Whatever checking done at memslot binding time will still be valid.Could the offset of shared memory and offset of private memory be different if userspace re-uses
slot->userspace_addrwithout deleting and re-adding a memslot?
They could be different, yes. I think what you mean is if userspace does something like
addr = mmap(guest_memfd); ioctl(KVM_SET_USER_MEMORY_REGION, addr, guest_memfd); munmap(addr);
addr = mmap(addr, other_fd);
(with no second call to KVM_SET_USER_MEMORY_REGION)
Without guest_memfd, when munmap() happens, KVM should get a notification via mmu_notifiers. That will unmap the pages from guest page tables. At the next fault, host page tables will be consulted to determine max_mapping_level, and at that time the mapping level would be the new mapping level in host page tables.
Quoted 3 lines
Then though the two offsets are validated as equal in
kvm_gmem_bind(), they may differ later on.
This is true.
Continuing from above, with guest_memfd, no issues if guest_memfd is only used for private memory, since shared memory uses the same mechanism as before guest_memfd.
If guest_memfd is used for both private and shared memory, on unmapping, KVM will also get notified via mmu_notifiers. On the next fault, the mapping level is determined as follows (I have a patch coming up that will illustrate this better)
1. guest_memfd will return 4K since this is a shared folio and shared folios are always split to 4K. But suppose in future guest_memfd supports shared folios at higher levels, say 1G, we continue...
2. lpage info (not updated since userspace swapped out addr) will say map at 1G
3. Since this is a shared fault, we check host page tables, which would say 4K since there was a munmap() and mmap().
I think it should still work as expected.
Quoted 7 lines
Quoted 4 lines
+ Host's access and other accesses (e.g. instruction emulation, which uses
slot->userspace_addr) to guest memory will be broken, but I think there's nothing protecting against that. The same breakage would happen for non-guest_memfd memslot.Why is host access broken in non-guest_memfd case? The HVA is still a valid one in QEMU's mmap-ed address space.
I was thinking that if a guest was executing code and the code gets swapped out from under its feet by replacing the memory pointed to by addr, the guest would be broken.
Now that I think about it again, it could be a valid use case. You're right, thanks for pointing this out!
Quoted 23 lines
Quoted 23 lines
p.s. I will be adding the validation as you suggested [1], though that shouldn't make a difference here, since the above check directly validates against gmem.pgoff.
Regarding 2., checking this checks against gmem.pgoff and should handle that as well.
[1] https://lore.kernel.org/all/[email protected]/
I prefer checking at binding time because it aligns with the ugfn check that is already there, and avoids having to check at every fault.
Quoted 11 lines
Quoted 11 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Adding checks at binding time will allow hugepage-unaligned offsets (to be at parity with non-guest_memfd backing memory) but still fix this issue.
lpage_infowill make sure that ranges near the bounds will be fragmented, but the hugepages in the middle will still be mappable as hugepages.