Skip to content

Commit a4edf25

Browse files
Ricardo KollerMarc Zyngier
authored andcommitted
KVM: selftests: aarch64: Add dirty logging tests into page_fault_test
Add some dirty logging tests into page_fault_test. Mark the data and/or page-table memory regions for dirty logging, perform some accesses, and check that the dirty log bits are set or clean when expected. Signed-off-by: Ricardo Koller <[email protected]> Signed-off-by: Marc Zyngier <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 3b1d915 commit a4edf25

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

tools/testing/selftests/kvm/aarch64/page_fault_test.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ static uint64_t *guest_test_memory = (uint64_t *)TEST_GVA;
3131
#define CMD_SKIP_TEST (1ULL << 1)
3232
#define CMD_HOLE_PT (1ULL << 2)
3333
#define CMD_HOLE_DATA (1ULL << 3)
34+
#define CMD_CHECK_WRITE_IN_DIRTY_LOG (1ULL << 4)
35+
#define CMD_CHECK_S1PTW_WR_IN_DIRTY_LOG (1ULL << 5)
36+
#define CMD_CHECK_NO_WRITE_IN_DIRTY_LOG (1ULL << 6)
37+
#define CMD_CHECK_NO_S1PTW_WR_IN_DIRTY_LOG (1ULL << 7)
38+
#define CMD_SET_PTE_AF (1ULL << 8)
3439

3540
#define PREPARE_FN_NR 10
3641
#define CHECK_FN_NR 10
@@ -213,6 +218,21 @@ static void guest_check_pte_af(void)
213218
GUEST_ASSERT_EQ(*((uint64_t *)TEST_PTE_GVA) & PTE_AF, PTE_AF);
214219
}
215220

221+
static void guest_check_write_in_dirty_log(void)
222+
{
223+
GUEST_SYNC(CMD_CHECK_WRITE_IN_DIRTY_LOG);
224+
}
225+
226+
static void guest_check_no_write_in_dirty_log(void)
227+
{
228+
GUEST_SYNC(CMD_CHECK_NO_WRITE_IN_DIRTY_LOG);
229+
}
230+
231+
static void guest_check_s1ptw_wr_in_dirty_log(void)
232+
{
233+
GUEST_SYNC(CMD_CHECK_S1PTW_WR_IN_DIRTY_LOG);
234+
}
235+
216236
static void guest_exec(void)
217237
{
218238
int (*code)(void) = (int (*)(void))TEST_EXEC_GVA;
@@ -395,6 +415,22 @@ static bool punch_hole_in_backing_store(struct kvm_vm *vm,
395415
return true;
396416
}
397417

418+
static bool check_write_in_dirty_log(struct kvm_vm *vm,
419+
struct userspace_mem_region *region,
420+
uint64_t host_pg_nr)
421+
{
422+
unsigned long *bmap;
423+
bool first_page_dirty;
424+
uint64_t size = region->region.memory_size;
425+
426+
/* getpage_size() is not always equal to vm->page_size */
427+
bmap = bitmap_zalloc(size / getpagesize());
428+
kvm_vm_get_dirty_log(vm, region->region.slot, bmap);
429+
first_page_dirty = test_bit(host_pg_nr, bmap);
430+
free(bmap);
431+
return first_page_dirty;
432+
}
433+
398434
/* Returns true to continue the test, and false if it should be skipped. */
399435
static bool handle_cmd(struct kvm_vm *vm, int cmd)
400436
{
@@ -411,6 +447,18 @@ static bool handle_cmd(struct kvm_vm *vm, int cmd)
411447
continue_test = punch_hole_in_backing_store(vm, pt_region);
412448
if (cmd & CMD_HOLE_DATA)
413449
continue_test = punch_hole_in_backing_store(vm, data_region);
450+
if (cmd & CMD_CHECK_WRITE_IN_DIRTY_LOG)
451+
TEST_ASSERT(check_write_in_dirty_log(vm, data_region, 0),
452+
"Missing write in dirty log");
453+
if (cmd & CMD_CHECK_S1PTW_WR_IN_DIRTY_LOG)
454+
TEST_ASSERT(check_write_in_dirty_log(vm, pt_region, 0),
455+
"Missing s1ptw write in dirty log");
456+
if (cmd & CMD_CHECK_NO_WRITE_IN_DIRTY_LOG)
457+
TEST_ASSERT(!check_write_in_dirty_log(vm, data_region, 0),
458+
"Unexpected write in dirty log");
459+
if (cmd & CMD_CHECK_NO_S1PTW_WR_IN_DIRTY_LOG)
460+
TEST_ASSERT(!check_write_in_dirty_log(vm, pt_region, 0),
461+
"Unexpected s1ptw write in dirty log");
414462

415463
return continue_test;
416464
}
@@ -673,6 +721,19 @@ static void help(char *name)
673721
.expected_events = { .uffd_faults = _uffd_faults, }, \
674722
}
675723

724+
#define TEST_DIRTY_LOG(_access, _with_af, _test_check) \
725+
{ \
726+
.name = SCAT3(dirty_log, _access, _with_af), \
727+
.data_memslot_flags = KVM_MEM_LOG_DIRTY_PAGES, \
728+
.pt_memslot_flags = KVM_MEM_LOG_DIRTY_PAGES, \
729+
.guest_prepare = { _PREPARE(_with_af), \
730+
_PREPARE(_access) }, \
731+
.guest_test = _access, \
732+
.guest_test_check = { _CHECK(_with_af), _test_check, \
733+
guest_check_s1ptw_wr_in_dirty_log}, \
734+
.expected_events = { 0 }, \
735+
}
736+
676737
static struct test_desc tests[] = {
677738

678739
/* Check that HW is setting the Access Flag (AF) (sanity checks). */
@@ -732,6 +793,21 @@ static struct test_desc tests[] = {
732793
TEST_UFFD(guest_exec, with_af, CMD_HOLE_DATA | CMD_HOLE_PT,
733794
uffd_data_read_handler, uffd_pt_write_handler, 2),
734795

796+
/*
797+
* Try accesses when the data and PT memory regions are both
798+
* tracked for dirty logging.
799+
*/
800+
TEST_DIRTY_LOG(guest_read64, with_af, guest_check_no_write_in_dirty_log),
801+
/* no_af should also lead to a PT write. */
802+
TEST_DIRTY_LOG(guest_read64, no_af, guest_check_no_write_in_dirty_log),
803+
TEST_DIRTY_LOG(guest_ld_preidx, with_af, guest_check_no_write_in_dirty_log),
804+
TEST_DIRTY_LOG(guest_at, no_af, guest_check_no_write_in_dirty_log),
805+
TEST_DIRTY_LOG(guest_exec, with_af, guest_check_no_write_in_dirty_log),
806+
TEST_DIRTY_LOG(guest_write64, with_af, guest_check_write_in_dirty_log),
807+
TEST_DIRTY_LOG(guest_cas, with_af, guest_check_write_in_dirty_log),
808+
TEST_DIRTY_LOG(guest_dc_zva, with_af, guest_check_write_in_dirty_log),
809+
TEST_DIRTY_LOG(guest_st_preidx, with_af, guest_check_write_in_dirty_log),
810+
735811
{ 0 }
736812
};
737813

0 commit comments

Comments
 (0)