1 // SPDX-License-Identifier: GPL-2.0-only
3 * Test for s390x CMMA migration
5 * Copyright IBM Corp. 2023
8 * Nico Boehr <nrb@linux.ibm.com>
11 #define _GNU_SOURCE /* for program_invocation_short_name */
16 #include <sys/ioctl.h>
18 #include "test_util.h"
20 #include "kselftest.h"
22 #define MAIN_PAGE_COUNT 512
24 #define TEST_DATA_PAGE_COUNT 512
25 #define TEST_DATA_MEMSLOT 1
26 #define TEST_DATA_START_GFN 4096
28 #define TEST_DATA_TWO_PAGE_COUNT 256
29 #define TEST_DATA_TWO_MEMSLOT 2
30 #define TEST_DATA_TWO_START_GFN 8192
32 static char cmma_value_buf[MAIN_PAGE_COUNT + TEST_DATA_PAGE_COUNT];
35 * Dirty CMMA attributes of exactly one page in the TEST_DATA memslot,
36 * so use_cmma goes on and the CMMA related ioctls do something.
38 static void guest_do_one_essa(void)
41 /* load TEST_DATA_START_GFN into r1 */
42 " llilf 1,%[start_gfn]\n"
43 /* calculate the address from the gfn */
45 /* set the first page in TEST_DATA memslot to STABLE */
46 " .insn rrf,0xb9ab0000,2,1,1,0\n"
51 : [start_gfn] "L"(TEST_DATA_START_GFN)
52 : "r1", "r2", "memory", "cc"
57 * Touch CMMA attributes of all pages in TEST_DATA memslot. Set them to stable
60 static void guest_dirty_test_data(void)
63 /* r1 = TEST_DATA_START_GFN */
65 " llilf 1,%[start_gfn]\n"
66 /* r5 = TEST_DATA_PAGE_COUNT */
67 " lghi 5,%[page_count]\n"
72 /* essa(r4, r2, SET_STABLE) */
73 " .insn rrf,0xb9ab0000,4,2,1,0\n"
76 /* if r1 < r5 goto 1 */
82 : [start_gfn] "L"(TEST_DATA_START_GFN),
83 [page_count] "L"(TEST_DATA_PAGE_COUNT)
85 /* the counter in our loop over the pages */
87 /* the calculated page physical address */
89 /* ESSA output register */
97 static void create_main_memslot(struct kvm_vm *vm)
101 vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, 0, 0, MAIN_PAGE_COUNT, 0);
102 /* set the array of memslots to zero like __vm_create does */
103 for (i = 0; i < NR_MEM_REGIONS; i++)
107 static void create_test_memslot(struct kvm_vm *vm)
109 vm_userspace_mem_region_add(vm,
110 VM_MEM_SRC_ANONYMOUS,
111 TEST_DATA_START_GFN << vm->page_shift,
113 TEST_DATA_PAGE_COUNT,
116 vm->memslots[MEM_REGION_TEST_DATA] = TEST_DATA_MEMSLOT;
119 static void create_memslots(struct kvm_vm *vm)
122 * Our VM has the following memory layout:
123 * +------+---------------------------+
125 * +------+---------------------------+
127 * | ... | MAIN (Code, Stack, ...) |
129 * +------+---------------------------+
131 * | ... | TEST_DATA |
133 * +------+---------------------------+
135 create_main_memslot(vm);
136 create_test_memslot(vm);
139 static void finish_vm_setup(struct kvm_vm *vm)
141 struct userspace_mem_region *slot0;
143 kvm_vm_elf_load(vm, program_invocation_name);
145 slot0 = memslot2region(vm, 0);
146 ucall_init(vm, slot0->region.guest_phys_addr + slot0->region.memory_size);
148 kvm_arch_vm_post_create(vm);
151 static struct kvm_vm *create_vm_two_memslots(void)
155 vm = vm_create_barebones();
164 static void enable_cmma(struct kvm_vm *vm)
168 r = __kvm_device_attr_set(vm->fd, KVM_S390_VM_MEM_CTRL, KVM_S390_VM_MEM_ENABLE_CMMA, NULL);
169 TEST_ASSERT(!r, "enabling cmma failed r=%d errno=%d", r, errno);
172 static void enable_dirty_tracking(struct kvm_vm *vm)
174 vm_mem_region_set_flags(vm, 0, KVM_MEM_LOG_DIRTY_PAGES);
175 vm_mem_region_set_flags(vm, TEST_DATA_MEMSLOT, KVM_MEM_LOG_DIRTY_PAGES);
178 static int __enable_migration_mode(struct kvm_vm *vm)
180 return __kvm_device_attr_set(vm->fd,
181 KVM_S390_VM_MIGRATION,
182 KVM_S390_VM_MIGRATION_START,
187 static void enable_migration_mode(struct kvm_vm *vm)
189 int r = __enable_migration_mode(vm);
191 TEST_ASSERT(!r, "enabling migration mode failed r=%d errno=%d", r, errno);
194 static bool is_migration_mode_on(struct kvm_vm *vm)
199 r = __kvm_device_attr_get(vm->fd,
200 KVM_S390_VM_MIGRATION,
201 KVM_S390_VM_MIGRATION_STATUS,
204 TEST_ASSERT(!r, "getting migration mode status failed r=%d errno=%d", r, errno);
208 static int vm_get_cmma_bits(struct kvm_vm *vm, u64 flags, int *errno_out)
210 struct kvm_s390_cmma_log args;
215 args = (struct kvm_s390_cmma_log){
217 .count = sizeof(cmma_value_buf),
219 .values = (__u64)&cmma_value_buf[0]
221 rc = __vm_ioctl(vm, KVM_S390_GET_CMMA_BITS, &args);
227 static void test_get_cmma_basic(void)
229 struct kvm_vm *vm = create_vm_two_memslots();
230 struct kvm_vcpu *vcpu;
233 /* GET_CMMA_BITS without CMMA enabled should fail */
234 rc = vm_get_cmma_bits(vm, 0, &errno_out);
235 TEST_ASSERT_EQ(rc, -1);
236 TEST_ASSERT_EQ(errno_out, ENXIO);
239 vcpu = vm_vcpu_add(vm, 1, guest_do_one_essa);
243 /* GET_CMMA_BITS without migration mode and without peeking should fail */
244 rc = vm_get_cmma_bits(vm, 0, &errno_out);
245 TEST_ASSERT_EQ(rc, -1);
246 TEST_ASSERT_EQ(errno_out, EINVAL);
248 /* GET_CMMA_BITS without migration mode and with peeking should work */
249 rc = vm_get_cmma_bits(vm, KVM_S390_CMMA_PEEK, &errno_out);
250 TEST_ASSERT_EQ(rc, 0);
251 TEST_ASSERT_EQ(errno_out, 0);
253 enable_dirty_tracking(vm);
254 enable_migration_mode(vm);
256 /* GET_CMMA_BITS with invalid flags */
257 rc = vm_get_cmma_bits(vm, 0xfeedc0fe, &errno_out);
258 TEST_ASSERT_EQ(rc, -1);
259 TEST_ASSERT_EQ(errno_out, EINVAL);
264 static void assert_exit_was_hypercall(struct kvm_vcpu *vcpu)
266 TEST_ASSERT_EQ(vcpu->run->exit_reason, 13);
267 TEST_ASSERT_EQ(vcpu->run->s390_sieic.icptcode, 4);
268 TEST_ASSERT_EQ(vcpu->run->s390_sieic.ipa, 0x8300);
269 TEST_ASSERT_EQ(vcpu->run->s390_sieic.ipb, 0x5010000);
272 static void test_migration_mode(void)
274 struct kvm_vm *vm = vm_create_barebones();
275 struct kvm_vcpu *vcpu;
279 /* enabling migration mode on a VM without memory should fail */
280 rc = __enable_migration_mode(vm);
281 TEST_ASSERT_EQ(rc, -1);
282 TEST_ASSERT_EQ(errno, EINVAL);
283 TEST_ASSERT(!is_migration_mode_on(vm), "migration mode should still be off");
290 vcpu = vm_vcpu_add(vm, 1, guest_do_one_essa);
291 orig_psw = vcpu->run->psw_addr;
294 * Execute one essa instruction in the guest. Otherwise the guest will
295 * not have use_cmm enabled and GET_CMMA_BITS will return no pages.
298 assert_exit_was_hypercall(vcpu);
300 /* migration mode when memslots have dirty tracking off should fail */
301 rc = __enable_migration_mode(vm);
302 TEST_ASSERT_EQ(rc, -1);
303 TEST_ASSERT_EQ(errno, EINVAL);
304 TEST_ASSERT(!is_migration_mode_on(vm), "migration mode should still be off");
307 /* enable dirty tracking */
308 enable_dirty_tracking(vm);
310 /* enabling migration mode should work now */
311 rc = __enable_migration_mode(vm);
312 TEST_ASSERT_EQ(rc, 0);
313 TEST_ASSERT(is_migration_mode_on(vm), "migration mode should be on");
316 /* execute another ESSA instruction to see this goes fine */
317 vcpu->run->psw_addr = orig_psw;
319 assert_exit_was_hypercall(vcpu);
322 * With migration mode on, create a new memslot with dirty tracking off.
323 * This should turn off migration mode.
325 TEST_ASSERT(is_migration_mode_on(vm), "migration mode should be on");
326 vm_userspace_mem_region_add(vm,
327 VM_MEM_SRC_ANONYMOUS,
328 TEST_DATA_TWO_START_GFN << vm->page_shift,
329 TEST_DATA_TWO_MEMSLOT,
330 TEST_DATA_TWO_PAGE_COUNT,
333 TEST_ASSERT(!is_migration_mode_on(vm),
334 "creating memslot without dirty tracking turns off migration mode"
337 /* ESSA instructions should still execute fine */
338 vcpu->run->psw_addr = orig_psw;
340 assert_exit_was_hypercall(vcpu);
343 * Turn on dirty tracking on the new memslot.
344 * It should be possible to turn migration mode back on again.
346 vm_mem_region_set_flags(vm, TEST_DATA_TWO_MEMSLOT, KVM_MEM_LOG_DIRTY_PAGES);
347 rc = __enable_migration_mode(vm);
348 TEST_ASSERT_EQ(rc, 0);
349 TEST_ASSERT(is_migration_mode_on(vm), "migration mode should be on");
353 * Turn off dirty tracking again, this time with just a flag change.
354 * Again, migration mode should turn off.
356 TEST_ASSERT(is_migration_mode_on(vm), "migration mode should be on");
357 vm_mem_region_set_flags(vm, TEST_DATA_TWO_MEMSLOT, 0);
358 TEST_ASSERT(!is_migration_mode_on(vm),
359 "disabling dirty tracking should turn off migration mode"
362 /* ESSA instructions should still execute fine */
363 vcpu->run->psw_addr = orig_psw;
365 assert_exit_was_hypercall(vcpu);
371 * Given a VM with the MAIN and TEST_DATA memslot, assert that both slots have
372 * CMMA attributes of all pages in both memslots and nothing more dirty.
373 * This has the useful side effect of ensuring nothing is CMMA dirty after this
376 static void assert_all_slots_cmma_dirty(struct kvm_vm *vm)
378 struct kvm_s390_cmma_log args;
381 * First iteration - everything should be dirty.
382 * Start at the main memslot...
384 args = (struct kvm_s390_cmma_log){
386 .count = sizeof(cmma_value_buf),
388 .values = (__u64)&cmma_value_buf[0]
390 memset(cmma_value_buf, 0xff, sizeof(cmma_value_buf));
391 vm_ioctl(vm, KVM_S390_GET_CMMA_BITS, &args);
392 TEST_ASSERT_EQ(args.count, MAIN_PAGE_COUNT);
393 TEST_ASSERT_EQ(args.remaining, TEST_DATA_PAGE_COUNT);
394 TEST_ASSERT_EQ(args.start_gfn, 0);
396 /* ...and then - after a hole - the TEST_DATA memslot should follow */
397 args = (struct kvm_s390_cmma_log){
398 .start_gfn = MAIN_PAGE_COUNT,
399 .count = sizeof(cmma_value_buf),
401 .values = (__u64)&cmma_value_buf[0]
403 memset(cmma_value_buf, 0xff, sizeof(cmma_value_buf));
404 vm_ioctl(vm, KVM_S390_GET_CMMA_BITS, &args);
405 TEST_ASSERT_EQ(args.count, TEST_DATA_PAGE_COUNT);
406 TEST_ASSERT_EQ(args.start_gfn, TEST_DATA_START_GFN);
407 TEST_ASSERT_EQ(args.remaining, 0);
409 /* ...and nothing else should be there */
410 args = (struct kvm_s390_cmma_log){
411 .start_gfn = TEST_DATA_START_GFN + TEST_DATA_PAGE_COUNT,
412 .count = sizeof(cmma_value_buf),
414 .values = (__u64)&cmma_value_buf[0]
416 memset(cmma_value_buf, 0xff, sizeof(cmma_value_buf));
417 vm_ioctl(vm, KVM_S390_GET_CMMA_BITS, &args);
418 TEST_ASSERT_EQ(args.count, 0);
419 TEST_ASSERT_EQ(args.start_gfn, 0);
420 TEST_ASSERT_EQ(args.remaining, 0);
424 * Given a VM, assert no pages are CMMA dirty.
426 static void assert_no_pages_cmma_dirty(struct kvm_vm *vm)
428 struct kvm_s390_cmma_log args;
430 /* If we start from GFN 0 again, nothing should be dirty. */
431 args = (struct kvm_s390_cmma_log){
433 .count = sizeof(cmma_value_buf),
435 .values = (__u64)&cmma_value_buf[0]
437 memset(cmma_value_buf, 0xff, sizeof(cmma_value_buf));
438 vm_ioctl(vm, KVM_S390_GET_CMMA_BITS, &args);
439 if (args.count || args.remaining || args.start_gfn)
440 TEST_FAIL("pages are still dirty start_gfn=0x%llx count=%u remaining=%llu",
447 static void test_get_inital_dirty(void)
449 struct kvm_vm *vm = create_vm_two_memslots();
450 struct kvm_vcpu *vcpu;
453 vcpu = vm_vcpu_add(vm, 1, guest_do_one_essa);
456 * Execute one essa instruction in the guest. Otherwise the guest will
457 * not have use_cmm enabled and GET_CMMA_BITS will return no pages.
460 assert_exit_was_hypercall(vcpu);
462 enable_dirty_tracking(vm);
463 enable_migration_mode(vm);
465 assert_all_slots_cmma_dirty(vm);
467 /* Start from the beginning again and make sure nothing else is dirty */
468 assert_no_pages_cmma_dirty(vm);
473 static void query_cmma_range(struct kvm_vm *vm,
474 u64 start_gfn, u64 gfn_count,
475 struct kvm_s390_cmma_log *res_out)
477 *res_out = (struct kvm_s390_cmma_log){
478 .start_gfn = start_gfn,
481 .values = (__u64)&cmma_value_buf[0]
483 memset(cmma_value_buf, 0xff, sizeof(cmma_value_buf));
484 vm_ioctl(vm, KVM_S390_GET_CMMA_BITS, res_out);
488 * Assert the given cmma_log struct that was executed by query_cmma_range()
489 * indicates the first dirty gfn is at first_dirty_gfn and contains exactly
490 * dirty_gfn_count CMMA values.
492 static void assert_cmma_dirty(u64 first_dirty_gfn,
494 const struct kvm_s390_cmma_log *res)
496 TEST_ASSERT_EQ(res->start_gfn, first_dirty_gfn);
497 TEST_ASSERT_EQ(res->count, dirty_gfn_count);
498 for (size_t i = 0; i < dirty_gfn_count; i++)
499 TEST_ASSERT_EQ(cmma_value_buf[0], 0x0); /* stable state */
500 TEST_ASSERT_EQ(cmma_value_buf[dirty_gfn_count], 0xff); /* not touched */
503 static void test_get_skip_holes(void)
506 struct kvm_vm *vm = create_vm_two_memslots();
507 struct kvm_s390_cmma_log log;
508 struct kvm_vcpu *vcpu;
512 vcpu = vm_vcpu_add(vm, 1, guest_dirty_test_data);
514 orig_psw = vcpu->run->psw_addr;
517 * Execute some essa instructions in the guest. Otherwise the guest will
518 * not have use_cmm enabled and GET_CMMA_BITS will return no pages.
521 assert_exit_was_hypercall(vcpu);
523 enable_dirty_tracking(vm);
524 enable_migration_mode(vm);
526 /* un-dirty all pages */
527 assert_all_slots_cmma_dirty(vm);
529 /* Then, dirty just the TEST_DATA memslot */
530 vcpu->run->psw_addr = orig_psw;
533 gfn_offset = TEST_DATA_START_GFN;
535 * Query CMMA attributes of one page, starting at page 0. Since the
536 * main memslot was not touched by the VM, this should yield the first
537 * page of the TEST_DATA memslot.
538 * The dirty bitmap should now look like this:
540 * [0x1, 0x200): dirty
542 query_cmma_range(vm, 0, 1, &log);
543 assert_cmma_dirty(gfn_offset, 1, &log);
547 * Query CMMA attributes of 32 (0x20) pages past the end of the TEST_DATA
548 * memslot. This should wrap back to the beginning of the TEST_DATA
550 * The dirty bitmap should now look like this:
551 * [0, 0x21): not dirty
552 * [0x21, 0x200): dirty
554 query_cmma_range(vm, TEST_DATA_START_GFN + TEST_DATA_PAGE_COUNT, 0x20, &log);
555 assert_cmma_dirty(gfn_offset, 0x20, &log);
562 * After skipping 32 pages, query the next 32 (0x20) pages.
563 * The dirty bitmap should now look like this:
564 * [0, 0x21): not dirty
565 * [0x21, 0x41): dirty
566 * [0x41, 0x61): not dirty
567 * [0x61, 0x200): dirty
569 query_cmma_range(vm, gfn_offset, 0x20, &log);
570 assert_cmma_dirty(gfn_offset, 0x20, &log);
574 * Query 1 page from the beginning of the TEST_DATA memslot. This should
576 * The dirty bitmap should now look like this:
577 * [0, 0x22): not dirty
578 * [0x22, 0x41): dirty
579 * [0x41, 0x61): not dirty
580 * [0x61, 0x200): dirty
582 query_cmma_range(vm, TEST_DATA_START_GFN, 1, &log);
583 assert_cmma_dirty(TEST_DATA_START_GFN + 0x21, 1, &log);
587 * Query 15 (0xF) pages from page 0x23 in TEST_DATA memslot.
588 * This should yield pages [0x23, 0x33).
589 * The dirty bitmap should now look like this:
590 * [0, 0x22): not dirty
592 * [0x23, 0x33): not dirty
593 * [0x33, 0x41): dirty
594 * [0x41, 0x61): not dirty
595 * [0x61, 0x200): dirty
597 gfn_offset = TEST_DATA_START_GFN + 0x23;
598 query_cmma_range(vm, gfn_offset, 15, &log);
599 assert_cmma_dirty(gfn_offset, 15, &log);
602 * Query 17 (0x11) pages from page 0x22 in TEST_DATA memslot.
603 * This should yield page [0x22, 0x33)
604 * The dirty bitmap should now look like this:
605 * [0, 0x33): not dirty
606 * [0x33, 0x41): dirty
607 * [0x41, 0x61): not dirty
608 * [0x61, 0x200): dirty
610 gfn_offset = TEST_DATA_START_GFN + 0x22;
611 query_cmma_range(vm, gfn_offset, 17, &log);
612 assert_cmma_dirty(gfn_offset, 17, &log);
615 * Query 25 (0x19) pages from page 0x40 in TEST_DATA memslot.
616 * This should yield page 0x40 and nothing more, since there are more
617 * than 16 non-dirty pages after page 0x40.
618 * The dirty bitmap should now look like this:
619 * [0, 0x33): not dirty
620 * [0x33, 0x40): dirty
621 * [0x40, 0x61): not dirty
622 * [0x61, 0x200): dirty
624 gfn_offset = TEST_DATA_START_GFN + 0x40;
625 query_cmma_range(vm, gfn_offset, 25, &log);
626 assert_cmma_dirty(gfn_offset, 1, &log);
629 * Query pages [0x33, 0x40).
630 * The dirty bitmap should now look like this:
631 * [0, 0x61): not dirty
632 * [0x61, 0x200): dirty
634 gfn_offset = TEST_DATA_START_GFN + 0x33;
635 query_cmma_range(vm, gfn_offset, 0x40 - 0x33, &log);
636 assert_cmma_dirty(gfn_offset, 0x40 - 0x33, &log);
639 * Query the remaining pages [0x61, 0x200).
641 gfn_offset = TEST_DATA_START_GFN;
642 query_cmma_range(vm, gfn_offset, TEST_DATA_PAGE_COUNT - 0x61, &log);
643 assert_cmma_dirty(TEST_DATA_START_GFN + 0x61, TEST_DATA_PAGE_COUNT - 0x61, &log);
645 assert_no_pages_cmma_dirty(vm);
652 { "migration mode and dirty tracking", test_migration_mode },
653 { "GET_CMMA_BITS: basic calls", test_get_cmma_basic },
654 { "GET_CMMA_BITS: all pages are dirty initally", test_get_inital_dirty },
655 { "GET_CMMA_BITS: holes are skipped", test_get_skip_holes },
659 * The kernel may support CMMA, but the machine may not (i.e. if running as
662 * In this case, the CMMA capabilities are all there, but the CMMA-related
663 * ioctls fail. To find out whether the machine supports CMMA, create a
664 * temporary VM and then query the CMMA feature of the VM.
666 static int machine_has_cmma(void)
668 struct kvm_vm *vm = vm_create_barebones();
671 r = !__kvm_has_device_attr(vm->fd, KVM_S390_VM_MEM_CTRL, KVM_S390_VM_MEM_ENABLE_CMMA);
677 int main(int argc, char *argv[])
681 TEST_REQUIRE(kvm_has_cap(KVM_CAP_SYNC_REGS));
682 TEST_REQUIRE(kvm_has_cap(KVM_CAP_S390_CMMA_MIGRATION));
683 TEST_REQUIRE(machine_has_cmma());
687 ksft_set_plan(ARRAY_SIZE(testlist));
689 for (idx = 0; idx < ARRAY_SIZE(testlist); idx++) {
690 testlist[idx].test();
691 ksft_test_result_pass("%s\n", testlist[idx].name);
694 ksft_finished(); /* Print results and exit() accordingly */