KVM: x86/mmu: Allocate memslot rmaps on demand
cover KVM: x86/mmu: Allocate memslot rmaps on demand
From: Fred Griffoul <[email protected]>
When an L1 guest runs a nested guest, KVM allocates rmaps for all of L1's memory the first time it creates a shadow root, one 8-byte head per 4 KiB page. For an L1 with 4 TiB of memory that is 8 GiB, allocated at once on the first nested VM-Enter, whether L2 maps 1% or all of L1's memory. Sean pointed at this cost on Mushahid's lpage_info patch [1].
This RFC allocates rmaps in page-sized chunks instead, when the first SPTE in a chunk's range is added. Each memslot keeps a per-level directory of chunks, 8 bytes per 2 MiB at the 4 KiB level, so the 4 TiB L1 above allocates 16 MiB on the first nested VM-Enter. A missing chunk reads as empty rmaps and slot walks skip it. When L2 maps every gfn, rmaps cost what they did before plus the directory.
Patches 1 and 2 prepare the slot walk and the debugfs rmap statistics, with no functional change. Patch 3 does the chunking. Patches 4 and 5 add a selftest in which L2 writes pages around chunk boundaries and checks the dirty log after the memslot is write-protected, including eager splitting of nested huge pages.
I would like feedback on:
- The data structure. A directory of page-sized chunks keeps lookups to one extra load. An xarray would be simpler to grow but adds a tree walk to every rmap lookup.
- Freeing chunks once they are empty. That would let the shadow MMU page limit bound rmap memory too, but lockless aging reads chunks without mmu_lock, so freeing needs an RCU grace period. I left it out of this RFC.
- Doing the same for gfn_write_track, 2 bytes per 4 KiB page, which is still allocated for every memslot on the first shadow root.
Limitations:
- Chunks are only freed with the memslot.
- Eager page splitting on dirty-log enable splits L2's huge mappings, so every 2 MiB that L2 mapped with a huge page then gets a chunk.
The series is based on kvm.git next.
[1] https://lore.kernel.org/all/[email protected]/
Fred
Fred Griffoul (5):
KVM: x86/mmu: Walk memslot rmaps by index
KVM: x86: Count rmaps per level in a debugfs helper
KVM: x86/mmu: Allocate memslot rmaps in on-demand chunks
KVM: selftests: Declare __tdp_map() in the x86 processor header
KVM: selftests: Test dirty logging of sparse nested mappings
arch/x86/include/asm/kvm_host.h | 22 +- arch/x86/kvm/debugfs.c | 46 ++- arch/x86/kvm/mmu/mmu.c | 126 +++++++- arch/x86/kvm/x86.c | 22 +- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/include/x86/processor.h | 1 + .../kvm/x86/nested_dirty_log_sparse_test.c | 275 ++++++++++++++++++ 7 files changed, 460 insertions(+), 33 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86/nested_dirty_log_sparse_test.c
base-commit: d21eef2084a9af87ac2a783bc7c2b175ba114e4e
Signature · 1 line
2.47.3
RFC PATCH 1/5 KVM: x86/mmu: Walk memslot rmaps by index
From: Fred Griffoul <[email protected]>
In preparation for storing memslot rmaps in chunks that are not contiguous in memory, walk a slot's rmaps by index instead of stepping a pointer through the array, and look each head up through a new __idx_to_rmap() helper.
No functional change intended.
--- arch/x86/kvm/mmu/mmu.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-)
arch/x86/kvm/mmu/mmu.c +24 -10
@@ -1236,13 +1236,17 @@ unsigned int pte_list_count(struct kvm_rmap_head *rmap_head)return desc->tail_count + desc->spte_count; } +static struct kvm_rmap_head *__idx_to_rmap (const struct kvm_memory_slot *slot, +int level,unsigned long idx) +{ +return &slot->arch.rmap[level -PG_LEVEL_4K ][idx]; +} +static struct kvm_rmap_head *gfn_to_rmap (gfn_t gfn,int level,const struct kvm_memory_slot *slot) { -unsigned long idx; - - idx =gfn_to_index (gfn, slot->base_gfn, level); -return &slot->arch.rmap[level -PG_LEVEL_4K ][idx]; +return __idx_to_rmap (slot, level, +gfn_to_index (gfn, slot->base_gfn, level)); }static void rmap_remove (struct kvm *kvm,u64 *spte)
@@ -1577,17 +1581,21 @@ struct slot_rmap_walk_iterator {struct kvm_rmap_head *rmap;int level; -/* private field. */ -struct kvm_rmap_head *end_rmap; +/* private fields. */ +unsigned long idx; +unsigned long end_idx; };static void rmap_walk_init_level (struct slot_rmap_walk_iterator *iterator,int level) { +const struct kvm_memory_slot *slot = iterator->slot; + iterator->level = level; iterator->gfn = iterator->start_gfn; - iterator->rmap =gfn_to_rmap (iterator->gfn, level, iterator->slot); - iterator->end_rmap =gfn_to_rmap (iterator->end_gfn, level, iterator->slot); + iterator->idx =gfn_to_index (iterator->start_gfn, slot->base_gfn, level); + iterator->end_idx =gfn_to_index (iterator->end_gfn, slot->base_gfn, level); + iterator->rmap =gfn_to_rmap (iterator->gfn, level, slot); } static void slot_rmap_walk_init(struct slot_rmap_walk_iterator *iterator,
@@ -1611,11 +1619,17 @@ static bool slot_rmap_walk_okay(struct slot_rmap_walk_iterator *iterator)static void slot_rmap_walk_next (struct slot_rmap_walk_iterator *iterator) { -while (++iterator->rmap <= iterator->end_rmap) { +struct kvm_rmap_head *rmap_head; + +while (++iterator->idx <= iterator->end_idx) { iterator->gfn +=KVM_PAGES_PER_HPAGE (iterator->level); -if (atomic_long_read (&iterator->rmap->val)) + rmap_head =__idx_to_rmap (iterator->slot, iterator->level, + iterator->idx); +if (atomic_long_read (&rmap_head->val)) { + iterator->rmap = rmap_head;return ; + } }if (++iterator->level > iterator->end_level) {
Signature · 1 line
2.47.3
RFC PATCH 2/5 KVM: x86: Count rmaps per level in a debugfs helper
In reply to Fred GriffoulFrom: Fred Griffoul <[email protected]>
In preparation for storing memslot rmaps in chunks, move the counting of one memslot level's rmaps into a helper, so that it can later walk a level one chunk at a time.
No functional change intended.
--- arch/x86/kvm/debugfs.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-)
arch/x86/kvm/debugfs.c +19 -14
@@ -86,16 +86,30 @@ void kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu, struct dentry *debugfs_static const char *kvm_lpage_str[KVM_NR_PAGE_SIZES ] = {"4K" ,"2M" ,"1G" }; +static void kvm_mmu_rmaps_stat_level (struct kvm_memory_slot *slot,int level, +unsigned int *cur) +{ +struct kvm_rmap_head *rmap = slot->arch.rmap[level -PG_LEVEL_4K ]; +unsigned long lpages =kvm_mmu_slot_lpages (slot, level); +unsigned int bucket; +unsigned long l; + +for (l =0 ; l < lpages; l++) { + bucket =fls (pte_list_count (&rmap[l])); +if (WARN_ON_ONCE (bucket >=RMAP_LOG_SIZE )) + bucket =RMAP_LOG_SIZE -1 ; + cur[bucket]++; + } +} +static int kvm_mmu_rmaps_stat_show (struct seq_file *m,void *v) { -struct kvm_rmap_head *rmap;struct kvm *kvm = m->private;struct kvm_memory_slot *slot;struct kvm_memslots *slots; -unsigned int lpage_size, index;/* Still small enough to be on the stack */ unsigned int *log[KVM_NR_PAGE_SIZES ], *cur; -int i, j, k, l, ret; +int i, j, k, ret;if (!kvm_memslots_have_rmaps (kvm))return 0 ;
@@ -116,17 +130,8 @@ static int kvm_mmu_rmaps_stat_show(struct seq_file *m, void *v) slots =__kvm_memslots (kvm, i);kvm_for_each_memslot (slot, bkt, slots) -for (k =0 ; k <KVM_NR_PAGE_SIZES ; k++) { - rmap = slot->arch.rmap[k]; - lpage_size =kvm_mmu_slot_lpages (slot, k +1 ); - cur = log[k]; -for (l =0 ; l < lpage_size; l++) { - index =fls (pte_list_count (&rmap[l])); -if (WARN_ON_ONCE (index >=RMAP_LOG_SIZE )) - index =RMAP_LOG_SIZE -1 ; - cur[index]++; - } - } +for (k =0 ; k <KVM_NR_PAGE_SIZES ; k++) +kvm_mmu_rmaps_stat_level (slot, k +1 , log[k]); }write_unlock (&kvm->mmu_lock);
Signature · 1 line
2.47.3
RFC PATCH 3/5 KVM: x86/mmu: Allocate memslot rmaps in on-demand chunks
In reply to Fred GriffoulFrom: Fred Griffoul <[email protected]>
When L1 runs a nested guest, KVM allocates rmaps for all of L1's memory the first time it creates a shadow root, which is 8 GiB in one allocation for a VM with 4 TiB of memory. Only the heads of gfns that L2 maps are ever used, so an L1 whose L2s map a fraction of its memory pays mostly for empty rmaps.
Allocate rmaps in page-sized chunks instead, when the first SPTE in a chunk's range is added, and keep a per-level directory of chunks in the memslot. A missing chunk reads as empty rmaps. That way, the rmap cost follows what L2 maps, and when L2 maps every gfn it is what it was before plus the directory.
--- arch/x86/include/asm/kvm_host.h | 22 ++++++- arch/x86/kvm/debugfs.c | 31 +++++++--- arch/x86/kvm/mmu/mmu.c | 100 +++++++++++++++++++++++++++++--- arch/x86/kvm/x86.c | 22 +++++-- 4 files changed, 153 insertions(+), 22 deletions(-)
arch/x86/include/asm/kvm_host.h +21 -1
@@ -431,6 +431,8 @@ struct kvm_rmap_head {atomic_long_t val; }; +#define KVM_RMAP_CHUNK_NR_HEADS (PAGE_SIZE / sizeof(struct kvm_rmap_head)) +struct kvm_pio_request {unsigned long count;int in;
@@ -785,6 +787,7 @@ struct kvm_vcpu_arch {u64 pdptrs[4 ];/* pae */ struct kvm_mmu_memory_cache mmu_pte_list_desc_cache; +struct kvm_mmu_memory_cache mmu_rmap_chunk_cache;struct kvm_mmu_memory_cache mmu_shadow_page_cache;struct kvm_mmu_memory_cache mmu_shadowed_info_cache;struct kvm_mmu_memory_cache mmu_page_header_cache;
@@ -1044,7 +1047,13 @@ struct kvm_lpage_info { };struct kvm_arch_memory_slot { -struct kvm_rmap_head *rmap[KVM_NR_PAGE_SIZES ]; +/* +* Per-level directories of rmap chunks, each chunk holding +* KVM_RMAP_CHUNK_NR_HEADS rmap heads. Chunks are allocated when KVM +* adds the first SPTE to a head in the chunk's range. A NULL chunk +* means all of its heads are empty. +*/ +struct kvm_rmap_head **rmap[KVM_NR_PAGE_SIZES ];struct kvm_lpage_info *lpage_info[KVM_NR_PAGE_SIZES -1 ];unsigned short *gfn_write_track; };
@@ -1418,6 +1427,17 @@ struct kvm_arch {#define SPLIT_DESC_CACHE_MIN_NR_OBJECTS (SPTE_ENT_PER_PAGE + 1)struct kvm_mmu_memory_cache split_desc_cache; +/* +* Memory cache used to allocate rmap chunks while splitting huge +* pages. The rmap heads of the lower level leaf SPTEs of one huge +* page are contiguous, but span two chunks if the memslot's base gfn +* is not aligned to the chunk size. +* +* Protected by kvm->slots_lock. +*/ +#define SPLIT_RMAP_CHUNK_CACHE_NR_OBJECTS 2 +struct kvm_mmu_memory_cache split_rmap_chunk_cache; +gfn_t gfn_direct_bits;int mirror_root_level;
arch/x86/kvm/debugfs.c +22 -9
@@ -89,16 +89,29 @@ static const char *kvm_lpage_str[KVM_NR_PAGE_SIZES] = { "4K", "2M", "1G" };static void kvm_mmu_rmaps_stat_level (struct kvm_memory_slot *slot,int level,unsigned int *cur) { -struct kvm_rmap_head *rmap = slot->arch.rmap[level -PG_LEVEL_4K ]; +struct kvm_rmap_head **rmap = slot->arch.rmap[level -PG_LEVEL_4K ];unsigned long lpages =kvm_mmu_slot_lpages (slot, level); -unsigned int bucket; -unsigned long l; - -for (l =0 ; l < lpages; l++) { - bucket =fls (pte_list_count (&rmap[l])); -if (WARN_ON_ONCE (bucket >=RMAP_LOG_SIZE )) - bucket =RMAP_LOG_SIZE -1 ; - cur[bucket]++; +struct kvm_rmap_head *chunk; +unsigned long l, nr; +unsigned int index; + +for (l =0 ; l < lpages; l += nr) { + nr =min (lpages - l,KVM_RMAP_CHUNK_NR_HEADS ); + chunk = rmap[l /KVM_RMAP_CHUNK_NR_HEADS ]; + +/* An unallocated chunk holds only empty rmaps. */ +if (!chunk) { + cur[0 ] += nr; +continue ; + } + +for (index =0 ; index < nr; index++) { +unsigned int bucket =fls (pte_list_count (&chunk[index])); + +if (WARN_ON_ONCE (bucket >=RMAP_LOG_SIZE )) + bucket =RMAP_LOG_SIZE -1 ; + cur[bucket]++; + } } }
arch/x86/kvm/mmu/mmu.c +92 -8
192 lines not on this page · raw message
arch/x86/kvm/x86.c +18 -4
@@ -10141,12 +10141,27 @@ void kvm_arch_destroy_vm(struct kvm *kvm)kvm_x86_call (vm_destroy)(kvm); } +static unsigned long memslot_rmap_nr_chunks (struct kvm_memory_slot *slot, +unsigned long npages,int level) +{ +return DIV_ROUND_UP (__kvm_mmu_slot_lpages (slot, npages, level), +KVM_RMAP_CHUNK_NR_HEADS ); +} +static void memslot_rmap_free (struct kvm_memory_slot *slot) { +unsigned long j, nr_chunks;int i;for (i =0 ; i <KVM_NR_PAGE_SIZES ; ++i) { -vfree (slot->arch.rmap[i]); +if (!slot->arch.rmap[i]) +continue ; + + nr_chunks =memslot_rmap_nr_chunks (slot, slot->npages, i +1 ); +for (j =0 ; j < nr_chunks; j++) +free_page ((unsigned long )slot->arch.rmap[i][j]); + +kvfree (slot->arch.rmap[i]); slot->arch.rmap[i] =NULL ; } }
@@ -10171,13 +10186,12 @@ int memslot_rmap_alloc(struct kvm_memory_slot *slot, unsigned long npages)int i;for (i =0 ; i <KVM_NR_PAGE_SIZES ; ++i) { -int level = i +1 ; -int lpages =__kvm_mmu_slot_lpages (slot, npages, level); +unsigned long nr_chunks =memslot_rmap_nr_chunks (slot, npages, i +1 );if (slot->arch.rmap[i])continue ; - slot->arch.rmap[i] =__vcalloc (lpages, sz,GFP_KERNEL_ACCOUNT ); + slot->arch.rmap[i] =kvcalloc (nr_chunks, sz,GFP_KERNEL_ACCOUNT );if (!slot->arch.rmap[i]) {memslot_rmap_free (slot);return -ENOMEM ;
Signature · 1 line
2.47.3
RFC PATCH 4/5 KVM: selftests: Declare __tdp_map() in the x86 processor header
In reply to Fred GriffoulFrom: Fred Griffoul <[email protected]>
Tests can only map L2 memory with 4 KiB or 1 GiB pages in the TDP page tables L1 uses for L2, because only tdp_map() and tdp_identity_map_1g() are declared. Declare __tdp_map(), which takes the page size, so that tests can also use 2 MiB pages.
--- tools/testing/selftests/kvm/include/x86/processor.h | 1 + 1 file changed, 1 insertion(+)
tools/testing/selftests/kvm/include/x86/processor.h +1 -0
@@ -1613,6 +1613,7 @@ void virt_map_level(struct kvm_vm *vm, gva_t gva, gpa_t gpa,void vm_enable_tdp (struct kvm_vm *vm);bool kvm_cpu_has_tdp (void ); +void __tdp_map (struct kvm_vm *vm,gpa_t l2_gpa,gpa_t gpa,u64 size,int level);void tdp_map (struct kvm_vm *vm,gpa_t l2_gpa,gpa_t gpa,u64 size);void tdp_identity_map_default_memslots (struct kvm_vm *vm);void tdp_identity_map_1g (struct kvm_vm *vm,u64 addr,u64 size);
Signature · 1 line
2.47.3
RFC PATCH 5/5 KVM: selftests: Test dirty logging of sparse nested mappings
In reply to Fred GriffoulFrom: Fred Griffoul <[email protected]>
With memslot rmaps allocated in chunks, slot walks skip unallocated chunks. A walk that skips too far leaves a mapped page writable after dirty logging write-protects the memslot, and L2's next write to that page is missing from the dirty log.
Add a test in which L2 writes pages at chunk boundaries and in a chunk surrounded by unallocated ones, and checks that exactly the pages it writes after dirty logging is enabled are reported dirty. A second pass maps L2 with 2 MiB pages and checks that eager page splitting splits them and logs the resulting 4 KiB SPTEs. It needs four free 2 MiB hugepages and eager page splitting, and is skipped otherwise.
--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../kvm/x86/nested_dirty_log_sparse_test.c | 275 ++++++++++++++++++ 2 files changed, 276 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86/nested_dirty_log_sparse_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
@@ -99,6 +99,7 @@ TEST_GEN_PROGS_x86 += x86/monitor_mwait_test TEST_GEN_PROGS_x86 += x86/msrs_test TEST_GEN_PROGS_x86 += x86/nested_close_kvm_test TEST_GEN_PROGS_x86 += x86/nested_dirty_log_test +TEST_GEN_PROGS_x86 += x86/nested_dirty_log_sparse_test TEST_GEN_PROGS_x86 += x86/nested_emulation_test TEST_GEN_PROGS_x86 += x86/nested_exceptions_test TEST_GEN_PROGS_x86 += x86/nested_invalid_cr3_test
tools/testing/selftests/kvm/x86/nested_dirty_log_sparse_test.c new file +275 -0
275 lines not on this page · raw message
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [RFC PATCH 5/5] KVM: selftests: Test dirty logging of sparse nested mappings
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Medium] KVM: selftests: test_dirty_log() uses default HugeTLB pool instead of 2M - [Low] KVM: selftests…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] KVM: selftests: test_dirty_log() uses default HugeTLB pool instead of 2M
- [Low] KVM: selftests: free_2m_hugepages() duplicates get_free_hugepages()
- [Low] KVM: selftests: test_dirty_log() pre-divides extra_mem_pages by 512 --
commit f746099353da91d06474f4e326f2f464849f22eb
Author: Fred Griffoul <[email protected]>
KVM: selftests: Test dirty logging of sparse nested mappings
This commit adds an x86 KVM selftest that verifies dirty logging of sparsely mapped L2 memory across shadow MMU rmap chunk boundaries, as well as eager page splitting of 2 MiB nested TDP mappings when dirty logging is enabled.
Quoted 6 lines
tools/testing/selftests/kvm/x86/nested_dirty_log_sparse_test.c new file +0
-0@@ -0,0 +1,275 @@
[ ... ]
Quoted 7 lines
tools/testing/selftests/kvm/x86/nested_dirty_log_sparse_test.c new file +7
-0+static void test_dirty_log (bool nested_tdp,bool huge) +{ +u64 npages = huge ?HUGE_MEM_PAGES :SPARSE_MEM_PAGES ; +enum vm_mem_backing_src_type src; +u64 pages_2m =0 , pages_4k =0 ; +gva_t nested_gva =0 ; +struct kvm_vcpu *vcpu;
tools/testing/selftests/kvm/x86/nested_dirty_log_sparse_test.c line 166
Quoted 10 lines
tools/testing/selftests/kvm/x86/nested_dirty_log_sparse_test.c new file +10
-0+unsigned long *bmap; +struct kvm_vm *vm; +struct ucall uc; + +pr_info ("Nested TDP: %s, L1 TDP page size: %s\n" , + nested_tdp ?"enabled" :"disabled" , huge ?"2M" :"4K" ); + +/* Room for L1's page tables and TDP page tables of the test memory. */ + vm =__vm_create_with_one_vcpu (&vcpu,2 * (npages /512 +16 ), + l1_guest_code);
[Severity: Low]
Does passing 2 * (npages / 512 + 16) as extra_mem_pages divide npages by 512 twice?
__vm_create_with_one_vcpu() passes extra_mem_pages to vm_nr_pages_required(), which already divides extra_mem_pages by PTES_PER_MIN_PAGE (512):
tools/testing/selftests/kvm/lib/kvm_util.c:vm_nr_pages_required() {
...
nr_pages += (nr_pages + extra_mem_pages) / PTES_PER_MIN_PAGE * 2;
...
}Because npages is pre-divided by 512 here, extra_mem_pages is 96 (or 40 when huge is true), so (nr_pages + extra_mem_pages) / 512 evaluates to 1 and adds no extra page-table pages beyond the default. Should this pass 2 * npages instead?
tools/testing/selftests/kvm/x86/nested_dirty_log_sparse_test.c line 178
Quoted 12 lines
tools/testing/selftests/kvm/x86/nested_dirty_log_sparse_test.c new file +12
-0+if (nested_tdp) +vm_enable_tdp (vm); + +if (kvm_cpu_has (X86_FEATURE_VMX )) +vcpu_alloc_vmx (vm, &nested_gva); +else +vcpu_alloc_svm (vm, &nested_gva); +vcpu_args_set (vcpu,1 , nested_gva); + + src = huge ?VM_MEM_SRC_ANONYMOUS_HUGETLB :VM_MEM_SRC_ANONYMOUS ; +vm_userspace_mem_region_add (vm, src,TEST_MEM_BASE ,TEST_MEM_SLOT , + npages,0 );
[Severity: Medium]
Should src use VM_MEM_SRC_ANONYMOUS_HUGETLB_2MB instead of VM_MEM_SRC_ANONYMOUS_HUGETLB when huge is true?
In main(), the test checks specifically for four free 2 MiB hugepages in /sys/kernel/mm/hugepages/hugepages-2048kB/free_hugepages and sizes the region to 8 MiB (HUGE_MEM_PAGES).
However, VM_MEM_SRC_ANONYMOUS_HUGETLB uses the system default HugeTLB page size without MAP_HUGE_2MB:
tools/testing/selftests/kvm/lib/test_util.c:get_backing_src_pagesz() {
switch (i) {
...
case VM_MEM_SRC_ANONYMOUS_HUGETLB:
case VM_MEM_SRC_SHARED_HUGETLB:
return get_def_hugetlb_pagesz();
default:
return MAP_HUGE_PAGE_SIZE(flag);
}
}On a host booted with default_hugepagesz=1G that has 2 MiB hugepages allocated in the 2048kB pool, get_def_hugetlb_pagesz() will check the 1 GiB pool in /proc/meminfo and either skip the test if HugePages_Total is 0, or return 1 GiB and cause __kvm_mmap() in vm_mem_add() to fail with EINVAL because the 8 MiB region is not 1 GiB aligned.
[ ... ]
Quoted 19 lines
tools/testing/selftests/kvm/x86/nested_dirty_log_sparse_test.c new file +19
-0+static u64 free_2m_hugepages (void ) +{ +u64 nr =0 ; +FILE *f; + + f =fopen ("/sys/kernel/mm/hugepages/hugepages-2048kB/free_hugepages" ,"r" ); +if (!f) +return 0 ; +if (fscanf (f,"%lu" , &nr) !=1 ) + nr =0 ; +fclose (f); + +return nr; +} + +int main (int argc,char *argv[]) +{ +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_VMX ) ||kvm_cpu_has (X86_FEATURE_SVM )); +
tools/testing/selftests/kvm/x86/nested_dirty_log_sparse_test.c line 270
Quoted 10 lines
tools/testing/selftests/kvm/x86/nested_dirty_log_sparse_test.c new file +10
-0+test_dirty_log (/*nested_tdp=*/ false,/*huge=*/ false); + +if (!kvm_cpu_has_tdp ()) +return 0 ; + +test_dirty_log (/*nested_tdp=*/ true,/*huge=*/ false); + +if (free_2m_hugepages () >=HUGE_MEM_PAGES /HUGE_PAGES_PER_2M && +get_kvm_param_bool ("tdp_mmu" ) &&get_kvm_param_bool ("eager_page_split" )) +test_dirty_log (/*nested_tdp=*/ true,/*huge=*/ true);
[Severity: Low]
This isn't a bug, but test_util.h already provides get_free_hugepages() in tools/testing/selftests/kvm/lib/test_util.c, which reads the same sysfs file and returns the free bytes:
tools/testing/selftests/kvm/lib/test_util.c:get_free_hugepages() {
char path[128];
size_t free;
snprintf(path, sizeof(path),
"/sys/kernel/mm/hugepages/hugepages-%zukB/free_hugepages",
page_size >> 10);
if (!test_sysfs_path(path))
return 0;
free = get_sysfs_val(path);
return free * page_size;
}Could main() use get_free_hugepages(SZ_2M) >= HUGE_MEM_PAGES * PAGE_SIZE instead of defining free_2m_hugepages()?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5