KVM: selftests: Test READ=>WRITE dirty logging behavior for shadow MMU
Sean Christopherson
maintainerPATCH v5 1/1 KVM: selftests: Test READ=>WRITE dirty logging behavior for shadow MMU
Update the nested dirty log test to validate KVM's handling of READ faults when dirty logging is enabled. Specifically, set the Dirty bit in the guest PTEs used to map L2 GPAs, so that KVM will create writable SPTEs when handling L2 read faults. When handling read faults in the shadow MMU, KVM opportunistically creates a writable SPTE if the mapping can be writable *and* the gPTE is dirty (or doesn't support the Dirty bit), i.e. if KVM doesn't need to intercept writes in order to emulate Dirty-bit updates.
To actually test the L2 READ=>WRITE sequence, e.g. without masking a false pass by other test activity, route the READ=>WRITE and WRITE=>WRITE sequences to separate L1 pages, and differentiate between "marked dirty due to a WRITE access/fault" and "marked dirty due to creating a writable SPTE for a READ access/fault". The updated sequence exposes the bug fixed by KVM commit 1f4e5fc83a42 ("KVM: x86: fix nested guest live migration with PML") when the guest performs a READ=>WRITE sequence with dirty guest PTEs.
Opportunistically tweak and rename the address macros, and add comments, to make it more obvious what the test is doing. E.g. NESTED_TEST_MEM1 vs. GUEST_TEST_MEM doesn't make it all that obvious that the test is creating aliases in both the L2 GPA and GVA address spaces, but only when L1 is using TDP to run L2.
---
v5:
- Drop all other patches (already applied).
- Eliminate hardcoding of page indices. [Yosry]
- Add macros to handle getting various addresses. [Yosry]
- Add comments to explain what the test is doing.
- Keep the non-aliased pages contiguous so that the aliases can be computed and reverse engineered with a simple offset.
- Clean up the for-loop checking of the results. [Yosry]
v4 (de facto v1 for this patch):
- https://lore.kernel.org/all/[email protected]
- Make it clear PTE accessors are predicates.
- Add a patch to extend the nested_dirty_log_test to verify that KVM creates writable SPTEs when the gPTEs are writable and dirty.
.../selftests/kvm/include/x86/processor.h | 1 + .../testing/selftests/kvm/lib/x86/processor.c | 7 + .../selftests/kvm/x86/nested_dirty_log_test.c | 186 +++++++++++++----- 3 files changed, 143 insertions(+), 51 deletions(-)
tools/testing/selftests/kvm/include/x86/processor.h +1 -0
@@ -1486,6 +1486,7 @@ bool kvm_cpu_has_tdp(void);void tdp_map (struct kvm_vm *vm,uint64_t nested_paddr,uint64_t paddr,uint64_t size);void tdp_identity_map_default_memslots (struct kvm_vm *vm);void tdp_identity_map_1g (struct kvm_vm *vm,uint64_t addr,uint64_t size); +uint64_t *tdp_get_pte (struct kvm_vm *vm,uint64_t l2_gpa);/* * Basic CPU control in CR0
tools/testing/selftests/kvm/lib/x86/processor.c +7 -0
@@ -390,6 +390,13 @@ static uint64_t *__vm_get_page_table_entry(struct kvm_vm *vm,return virt_get_pte (vm, mmu, pte, vaddr,PG_LEVEL_4K ); } +uint64_t *tdp_get_pte (struct kvm_vm *vm,uint64_t l2_gpa) +{ +int level =PG_LEVEL_4K ; + +return __vm_get_page_table_entry (vm, &vm->stage2_mmu, l2_gpa, &level); +} +uint64_t *vm_get_pte (struct kvm_vm *vm,uint64_t vaddr) {int level =PG_LEVEL_4K ;
tools/testing/selftests/kvm/x86/nested_dirty_log_test.c +135 -51
265 lines not on this page · raw message
base-commit: acdc5446135932ca974b82d9d9a17762c7a82493
Signature · 1 line
2.52.0.457.g6b5491de43-goog
Quoted 18 lines · Sean Christopherson
Update the nested dirty log test to validate KVM's handling of READ faults when dirty logging is enabled. Specifically, set the Dirty bit in the guest PTEs used to map L2 GPAs, so that KVM will create writable SPTEs when handling L2 read faults. When handling read faults in the shadow MMU, KVM opportunistically creates a writable SPTE if the mapping can be writable *and* the gPTE is dirty (or doesn't support the Dirty bit), i.e. if KVM doesn't need to intercept writes in order to emulate Dirty-bit updates.
To actually test the L2 READ=>WRITE sequence, e.g. without masking a false pass by other test activity, route the READ=>WRITE and WRITE=>WRITE sequences to separate L1 pages, and differentiate between "marked dirty due to a WRITE access/fault" and "marked dirty due to creating a writable SPTE for a READ access/fault". The updated sequence exposes the bug fixed by KVM commit
1f4e5fc83a42("KVM: x86: fix nested guest live migration with PML") when the guest performs a READ=>WRITE sequence with dirty guest PTEs.
Quoted 8 lines
Opportunistically tweak and rename the address macros, and add comments, to make it more obvious what the test is doing. E.g.
NESTED_TEST_MEM1vs.GUEST_TEST_MEMdoesn't make it all that obvious that the test is creating aliases in both the L2 GPA and GVA address spaces, but only when L1 is using TDP to run L2.Cc Yosry Ahmed <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
LGTM with one nit/question below:
[..]
Quoted 18 lines
tools/testing/selftests/kvm/x86/nested_dirty_log_test.c +7
-9+static void l2_guest_code (vm_vaddr_t base) { -READ_ONCE (*a); -WRITE_ONCE (*a,1 ); -GUEST_SYNC (true); -GUEST_SYNC (false); +vm_vaddr_t page0 =TEST_GUEST_ADDR (base,0 ); +vm_vaddr_t page1 =TEST_GUEST_ADDR (base,1 ); -WRITE_ONCE (*b,1 ); -GUEST_SYNC (true); -WRITE_ONCE (*b,1 ); -GUEST_SYNC (true); -GUEST_SYNC (false); +READ_ONCE (*(u64 *)page0); +GUEST_SYNC (page0 |TEST_SYNC_READ_FAULT ); +WRITE_ONCE (*(u64 *)page0,1 ); +GUEST_SYNC (page0 |TEST_SYNC_WRITE_FAULT );
tools/testing/selftests/kvm/x86/nested_dirty_log_test.c line 68
Quoted 10 lines
tools/testing/selftests/kvm/x86/nested_dirty_log_test.c +10
-0+READ_ONCE (*(u64 *)page0); +GUEST_SYNC (page0 |TEST_SYNC_NO_FAULT ); + +WRITE_ONCE (*(u64 *)page1,1 ); +GUEST_SYNC (page1 |TEST_SYNC_WRITE_FAULT ); +WRITE_ONCE (*(u64 *)page1,1 ); +GUEST_SYNC (page1 |TEST_SYNC_WRITE_FAULT ); +READ_ONCE (*(u64 *)page1); +GUEST_SYNC (page1 |TEST_SYNC_NO_FAULT ); +GUEST_SYNC (page1 |TEST_SYNC_NO_FAULT );
Extra GUEST_SYNC()?
Quoted 3 lines
tools/testing/selftests/kvm/x86/nested_dirty_log_test.c +0
-0/* Exit to L1 and never come back. */ vmcall ();
Sean Christopherson
maintainerQuoted 23 lines · Yosry Ahmed
Quoted 22 lines · Sean Christopherson
tools/testing/selftests/kvm/x86/nested_dirty_log_test.c +11
-9+static void l2_guest_code (vm_vaddr_t base) { -READ_ONCE (*a); -WRITE_ONCE (*a,1 ); -GUEST_SYNC (true); -GUEST_SYNC (false); +vm_vaddr_t page0 =TEST_GUEST_ADDR (base,0 ); +vm_vaddr_t page1 =TEST_GUEST_ADDR (base,1 ); -WRITE_ONCE (*b,1 ); -GUEST_SYNC (true); -WRITE_ONCE (*b,1 ); -GUEST_SYNC (true); -GUEST_SYNC (false); +READ_ONCE (*(u64 *)page0); +GUEST_SYNC (page0 |TEST_SYNC_READ_FAULT ); +WRITE_ONCE (*(u64 *)page0,1 ); +GUEST_SYNC (page0 |TEST_SYNC_WRITE_FAULT ); +READ_ONCE (*(u64 *)page0); +GUEST_SYNC (page0 |TEST_SYNC_NO_FAULT ); + +WRITE_ONCE (*(u64 *)page1,1 );
Quoted 8 lines
tools/testing/selftests/kvm/x86/nested_dirty_log_test.c line 68
Quoted 6 lines
tools/testing/selftests/kvm/x86/nested_dirty_log_test.c +6
-0+GUEST_SYNC (page1 |TEST_SYNC_WRITE_FAULT ); +WRITE_ONCE (*(u64 *)page1,1 ); +GUEST_SYNC (page1 |TEST_SYNC_WRITE_FAULT ); +READ_ONCE (*(u64 *)page1); +GUEST_SYNC (page1 |TEST_SYNC_NO_FAULT ); +GUEST_SYNC (page1 |TEST_SYNC_NO_FAULT );Extra
GUEST_SYNC()?
Yeah, I'm guessing it was a copy+paste goof, I can't think of any value added by a second sync here. I'll drop it when applying.
Thanks!
Sean Christopherson
maintainerQuoted 10 lines · Sean Christopherson
Update the nested dirty log test to validate KVM's handling of READ faults when dirty logging is enabled. Specifically, set the Dirty bit in the guest PTEs used to map L2 GPAs, so that KVM will create writable SPTEs when handling L2 read faults. When handling read faults in the shadow MMU, KVM opportunistically creates a writable SPTE if the mapping can be writable *and* the gPTE is dirty (or doesn't support the Dirty bit), i.e. if KVM doesn't need to intercept writes in order to emulate Dirty-bit updates.
[...]
Applied to kvm-x86 selftests, without the duplicate sync (hopefully). Thanks for the reviews!
[1/1] KVM: selftests: Test READ=>WRITE dirty logging behavior for shadow MMU
https://github.com/kvm-x86/linux/commit/a91cc4824660