xref: /linux/tools/testing/selftests/kvm/lib/memstress.c (revision 2b7deea3ec7c81a92d4c17751d3bcd780d065ae4)
19fda6753SDavid Matlack // SPDX-License-Identifier: GPL-2.0
29fda6753SDavid Matlack /*
39fda6753SDavid Matlack  * Copyright (C) 2020, Google LLC.
49fda6753SDavid Matlack  */
59fda6753SDavid Matlack #include <inttypes.h>
6de10b798SBen Gardon #include <linux/bitmap.h>
79fda6753SDavid Matlack 
89fda6753SDavid Matlack #include "kvm_util.h"
99fda6753SDavid Matlack #include "memstress.h"
109fda6753SDavid Matlack #include "processor.h"
11*2b7deea3SSean Christopherson #include "ucall_common.h"
129fda6753SDavid Matlack 
137812d80cSDavid Matlack struct memstress_args memstress_args;
149fda6753SDavid Matlack 
159fda6753SDavid Matlack /*
169fda6753SDavid Matlack  * Guest virtual memory offset of the testing memory slot.
179fda6753SDavid Matlack  * Must not conflict with identity mapped test code.
189fda6753SDavid Matlack  */
199fda6753SDavid Matlack static uint64_t guest_test_virt_mem = DEFAULT_GUEST_TEST_MEM;
209fda6753SDavid Matlack 
219fda6753SDavid Matlack struct vcpu_thread {
229fda6753SDavid Matlack 	/* The index of the vCPU. */
239fda6753SDavid Matlack 	int vcpu_idx;
249fda6753SDavid Matlack 
259fda6753SDavid Matlack 	/* The pthread backing the vCPU. */
269fda6753SDavid Matlack 	pthread_t thread;
279fda6753SDavid Matlack 
289fda6753SDavid Matlack 	/* Set to true once the vCPU thread is up and running. */
299fda6753SDavid Matlack 	bool running;
309fda6753SDavid Matlack };
319fda6753SDavid Matlack 
329fda6753SDavid Matlack /* The vCPU threads involved in this test. */
339fda6753SDavid Matlack static struct vcpu_thread vcpu_threads[KVM_MAX_VCPUS];
349fda6753SDavid Matlack 
359fda6753SDavid Matlack /* The function run by each vCPU thread, as provided by the test. */
367812d80cSDavid Matlack static void (*vcpu_thread_fn)(struct memstress_vcpu_args *);
379fda6753SDavid Matlack 
389fda6753SDavid Matlack /* Set to true once all vCPU threads are up and running. */
399fda6753SDavid Matlack static bool all_vcpu_threads_running;
409fda6753SDavid Matlack 
419fda6753SDavid Matlack static struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
429fda6753SDavid Matlack 
439fda6753SDavid Matlack /*
449fda6753SDavid Matlack  * Continuously write to the first 8 bytes of each page in the
459fda6753SDavid Matlack  * specified region.
469fda6753SDavid Matlack  */
477812d80cSDavid Matlack void memstress_guest_code(uint32_t vcpu_idx)
489fda6753SDavid Matlack {
497812d80cSDavid Matlack 	struct memstress_args *args = &memstress_args;
507812d80cSDavid Matlack 	struct memstress_vcpu_args *vcpu_args = &args->vcpu_args[vcpu_idx];
519fda6753SDavid Matlack 	struct guest_random_state rand_state;
529fda6753SDavid Matlack 	uint64_t gva;
539fda6753SDavid Matlack 	uint64_t pages;
549fda6753SDavid Matlack 	uint64_t addr;
559fda6753SDavid Matlack 	uint64_t page;
569fda6753SDavid Matlack 	int i;
579fda6753SDavid Matlack 
58cb6c6914SSean Christopherson 	rand_state = new_guest_random_state(guest_random_seed + vcpu_idx);
599fda6753SDavid Matlack 
609fda6753SDavid Matlack 	gva = vcpu_args->gva;
619fda6753SDavid Matlack 	pages = vcpu_args->pages;
629fda6753SDavid Matlack 
639fda6753SDavid Matlack 	/* Make sure vCPU args data structure is not corrupt. */
649fda6753SDavid Matlack 	GUEST_ASSERT(vcpu_args->vcpu_idx == vcpu_idx);
659fda6753SDavid Matlack 
669fda6753SDavid Matlack 	while (true) {
6707b4b2f4SPaolo Bonzini 		for (i = 0; i < sizeof(memstress_args); i += args->guest_page_size)
6807b4b2f4SPaolo Bonzini 			(void) *((volatile char *)args + i);
6907b4b2f4SPaolo Bonzini 
709fda6753SDavid Matlack 		for (i = 0; i < pages; i++) {
71a008a335SDavid Matlack 			if (args->random_access)
729fda6753SDavid Matlack 				page = guest_random_u32(&rand_state) % pages;
739fda6753SDavid Matlack 			else
749fda6753SDavid Matlack 				page = i;
759fda6753SDavid Matlack 
76a008a335SDavid Matlack 			addr = gva + (page * args->guest_page_size);
779fda6753SDavid Matlack 
7873369acdSSean Christopherson 			if (__guest_random_bool(&rand_state, args->write_percent))
799fda6753SDavid Matlack 				*(uint64_t *)addr = 0x0123456789ABCDEF;
809fda6753SDavid Matlack 			else
819fda6753SDavid Matlack 				READ_ONCE(*(uint64_t *)addr);
829fda6753SDavid Matlack 		}
839fda6753SDavid Matlack 
849fda6753SDavid Matlack 		GUEST_SYNC(1);
859fda6753SDavid Matlack 	}
869fda6753SDavid Matlack }
879fda6753SDavid Matlack 
887812d80cSDavid Matlack void memstress_setup_vcpus(struct kvm_vm *vm, int nr_vcpus,
899fda6753SDavid Matlack 			   struct kvm_vcpu *vcpus[],
909fda6753SDavid Matlack 			   uint64_t vcpu_memory_bytes,
919fda6753SDavid Matlack 			   bool partition_vcpu_memory_access)
929fda6753SDavid Matlack {
937812d80cSDavid Matlack 	struct memstress_args *args = &memstress_args;
947812d80cSDavid Matlack 	struct memstress_vcpu_args *vcpu_args;
959fda6753SDavid Matlack 	int i;
969fda6753SDavid Matlack 
979fda6753SDavid Matlack 	for (i = 0; i < nr_vcpus; i++) {
98a008a335SDavid Matlack 		vcpu_args = &args->vcpu_args[i];
999fda6753SDavid Matlack 
1009fda6753SDavid Matlack 		vcpu_args->vcpu = vcpus[i];
1019fda6753SDavid Matlack 		vcpu_args->vcpu_idx = i;
1029fda6753SDavid Matlack 
1039fda6753SDavid Matlack 		if (partition_vcpu_memory_access) {
1049fda6753SDavid Matlack 			vcpu_args->gva = guest_test_virt_mem +
1059fda6753SDavid Matlack 					 (i * vcpu_memory_bytes);
1069fda6753SDavid Matlack 			vcpu_args->pages = vcpu_memory_bytes /
107a008a335SDavid Matlack 					   args->guest_page_size;
108a008a335SDavid Matlack 			vcpu_args->gpa = args->gpa + (i * vcpu_memory_bytes);
1099fda6753SDavid Matlack 		} else {
1109fda6753SDavid Matlack 			vcpu_args->gva = guest_test_virt_mem;
1119fda6753SDavid Matlack 			vcpu_args->pages = (nr_vcpus * vcpu_memory_bytes) /
112a008a335SDavid Matlack 					   args->guest_page_size;
113a008a335SDavid Matlack 			vcpu_args->gpa = args->gpa;
1149fda6753SDavid Matlack 		}
1159fda6753SDavid Matlack 
1169fda6753SDavid Matlack 		vcpu_args_set(vcpus[i], 1, i);
1179fda6753SDavid Matlack 
1189fda6753SDavid Matlack 		pr_debug("Added VCPU %d with test mem gpa [%lx, %lx)\n",
1199fda6753SDavid Matlack 			 i, vcpu_args->gpa, vcpu_args->gpa +
120a008a335SDavid Matlack 			 (vcpu_args->pages * args->guest_page_size));
1219fda6753SDavid Matlack 	}
1229fda6753SDavid Matlack }
1239fda6753SDavid Matlack 
1247812d80cSDavid Matlack struct kvm_vm *memstress_create_vm(enum vm_guest_mode mode, int nr_vcpus,
1259fda6753SDavid Matlack 				   uint64_t vcpu_memory_bytes, int slots,
1269fda6753SDavid Matlack 				   enum vm_mem_backing_src_type backing_src,
1279fda6753SDavid Matlack 				   bool partition_vcpu_memory_access)
1289fda6753SDavid Matlack {
1297812d80cSDavid Matlack 	struct memstress_args *args = &memstress_args;
1309fda6753SDavid Matlack 	struct kvm_vm *vm;
1319fda6753SDavid Matlack 	uint64_t guest_num_pages, slot0_pages = 0;
1329fda6753SDavid Matlack 	uint64_t backing_src_pagesz = get_backing_src_pagesz(backing_src);
1339fda6753SDavid Matlack 	uint64_t region_end_gfn;
1349fda6753SDavid Matlack 	int i;
1359fda6753SDavid Matlack 
1369fda6753SDavid Matlack 	pr_info("Testing guest mode: %s\n", vm_guest_mode_string(mode));
1379fda6753SDavid Matlack 
1389fda6753SDavid Matlack 	/* By default vCPUs will write to memory. */
139a008a335SDavid Matlack 	args->write_percent = 100;
1409fda6753SDavid Matlack 
1419fda6753SDavid Matlack 	/*
1429fda6753SDavid Matlack 	 * Snapshot the non-huge page size.  This is used by the guest code to
1439fda6753SDavid Matlack 	 * access/dirty pages at the logging granularity.
1449fda6753SDavid Matlack 	 */
145a008a335SDavid Matlack 	args->guest_page_size = vm_guest_mode_params[mode].page_size;
1469fda6753SDavid Matlack 
1479fda6753SDavid Matlack 	guest_num_pages = vm_adjust_num_guest_pages(mode,
148a008a335SDavid Matlack 				(nr_vcpus * vcpu_memory_bytes) / args->guest_page_size);
1499fda6753SDavid Matlack 
1509fda6753SDavid Matlack 	TEST_ASSERT(vcpu_memory_bytes % getpagesize() == 0,
1519fda6753SDavid Matlack 		    "Guest memory size is not host page size aligned.");
152a008a335SDavid Matlack 	TEST_ASSERT(vcpu_memory_bytes % args->guest_page_size == 0,
1539fda6753SDavid Matlack 		    "Guest memory size is not guest page size aligned.");
1549fda6753SDavid Matlack 	TEST_ASSERT(guest_num_pages % slots == 0,
1559fda6753SDavid Matlack 		    "Guest memory cannot be evenly divided into %d slots.",
1569fda6753SDavid Matlack 		    slots);
1579fda6753SDavid Matlack 
1589fda6753SDavid Matlack 	/*
1599fda6753SDavid Matlack 	 * If using nested, allocate extra pages for the nested page tables and
1609fda6753SDavid Matlack 	 * in-memory data structures.
1619fda6753SDavid Matlack 	 */
162a008a335SDavid Matlack 	if (args->nested)
1637812d80cSDavid Matlack 		slot0_pages += memstress_nested_pages(nr_vcpus);
1649fda6753SDavid Matlack 
1659fda6753SDavid Matlack 	/*
1669fda6753SDavid Matlack 	 * Pass guest_num_pages to populate the page tables for test memory.
1679fda6753SDavid Matlack 	 * The memory is also added to memslot 0, but that's a benign side
1689fda6753SDavid Matlack 	 * effect as KVM allows aliasing HVAs in meslots.
1699fda6753SDavid Matlack 	 */
170672eaa35SSean Christopherson 	vm = __vm_create_with_vcpus(VM_SHAPE(mode), nr_vcpus,
171672eaa35SSean Christopherson 				    slot0_pages + guest_num_pages,
1727812d80cSDavid Matlack 				    memstress_guest_code, vcpus);
1739fda6753SDavid Matlack 
174a008a335SDavid Matlack 	args->vm = vm;
1759fda6753SDavid Matlack 
1769fda6753SDavid Matlack 	/* Put the test region at the top guest physical memory. */
1779fda6753SDavid Matlack 	region_end_gfn = vm->max_gfn + 1;
1789fda6753SDavid Matlack 
1799fda6753SDavid Matlack #ifdef __x86_64__
1809fda6753SDavid Matlack 	/*
1819fda6753SDavid Matlack 	 * When running vCPUs in L2, restrict the test region to 48 bits to
1829fda6753SDavid Matlack 	 * avoid needing 5-level page tables to identity map L2.
1839fda6753SDavid Matlack 	 */
184a008a335SDavid Matlack 	if (args->nested)
185a008a335SDavid Matlack 		region_end_gfn = min(region_end_gfn, (1UL << 48) / args->guest_page_size);
1869fda6753SDavid Matlack #endif
1879fda6753SDavid Matlack 	/*
1889fda6753SDavid Matlack 	 * If there should be more memory in the guest test region than there
1899fda6753SDavid Matlack 	 * can be pages in the guest, it will definitely cause problems.
1909fda6753SDavid Matlack 	 */
1919fda6753SDavid Matlack 	TEST_ASSERT(guest_num_pages < region_end_gfn,
1929fda6753SDavid Matlack 		    "Requested more guest memory than address space allows.\n"
1939fda6753SDavid Matlack 		    "    guest pages: %" PRIx64 " max gfn: %" PRIx64
194250e138dSAndrew Jones 		    " nr_vcpus: %d wss: %" PRIx64 "]",
1959fda6753SDavid Matlack 		    guest_num_pages, region_end_gfn - 1, nr_vcpus, vcpu_memory_bytes);
1969fda6753SDavid Matlack 
197a008a335SDavid Matlack 	args->gpa = (region_end_gfn - guest_num_pages - 1) * args->guest_page_size;
198a008a335SDavid Matlack 	args->gpa = align_down(args->gpa, backing_src_pagesz);
1999fda6753SDavid Matlack #ifdef __s390x__
2009fda6753SDavid Matlack 	/* Align to 1M (segment size) */
201a008a335SDavid Matlack 	args->gpa = align_down(args->gpa, 1 << 20);
2029fda6753SDavid Matlack #endif
203a008a335SDavid Matlack 	args->size = guest_num_pages * args->guest_page_size;
2049fda6753SDavid Matlack 	pr_info("guest physical test memory: [0x%lx, 0x%lx)\n",
205a008a335SDavid Matlack 		args->gpa, args->gpa + args->size);
2069fda6753SDavid Matlack 
2079fda6753SDavid Matlack 	/* Add extra memory slots for testing */
2089fda6753SDavid Matlack 	for (i = 0; i < slots; i++) {
2099fda6753SDavid Matlack 		uint64_t region_pages = guest_num_pages / slots;
210a008a335SDavid Matlack 		vm_paddr_t region_start = args->gpa + region_pages * args->guest_page_size * i;
2119fda6753SDavid Matlack 
2129fda6753SDavid Matlack 		vm_userspace_mem_region_add(vm, backing_src, region_start,
2137812d80cSDavid Matlack 					    MEMSTRESS_MEM_SLOT_INDEX + i,
2149fda6753SDavid Matlack 					    region_pages, 0);
2159fda6753SDavid Matlack 	}
2169fda6753SDavid Matlack 
2179fda6753SDavid Matlack 	/* Do mapping for the demand paging memory slot */
218a008a335SDavid Matlack 	virt_map(vm, guest_test_virt_mem, args->gpa, guest_num_pages);
2199fda6753SDavid Matlack 
2207812d80cSDavid Matlack 	memstress_setup_vcpus(vm, nr_vcpus, vcpus, vcpu_memory_bytes,
2219fda6753SDavid Matlack 			      partition_vcpu_memory_access);
2229fda6753SDavid Matlack 
223a008a335SDavid Matlack 	if (args->nested) {
2249fda6753SDavid Matlack 		pr_info("Configuring vCPUs to run in L2 (nested).\n");
2257812d80cSDavid Matlack 		memstress_setup_nested(vm, nr_vcpus, vcpus);
2269fda6753SDavid Matlack 	}
2279fda6753SDavid Matlack 
2289fda6753SDavid Matlack 	/* Export the shared variables to the guest. */
2297812d80cSDavid Matlack 	sync_global_to_guest(vm, memstress_args);
2309fda6753SDavid Matlack 
2319fda6753SDavid Matlack 	return vm;
2329fda6753SDavid Matlack }
2339fda6753SDavid Matlack 
2347812d80cSDavid Matlack void memstress_destroy_vm(struct kvm_vm *vm)
2359fda6753SDavid Matlack {
2369fda6753SDavid Matlack 	kvm_vm_free(vm);
2379fda6753SDavid Matlack }
2389fda6753SDavid Matlack 
2397812d80cSDavid Matlack void memstress_set_write_percent(struct kvm_vm *vm, uint32_t write_percent)
2409fda6753SDavid Matlack {
2417812d80cSDavid Matlack 	memstress_args.write_percent = write_percent;
2427812d80cSDavid Matlack 	sync_global_to_guest(vm, memstress_args.write_percent);
2439fda6753SDavid Matlack }
2449fda6753SDavid Matlack 
2457812d80cSDavid Matlack void memstress_set_random_access(struct kvm_vm *vm, bool random_access)
2469fda6753SDavid Matlack {
2477812d80cSDavid Matlack 	memstress_args.random_access = random_access;
2487812d80cSDavid Matlack 	sync_global_to_guest(vm, memstress_args.random_access);
2499fda6753SDavid Matlack }
2509fda6753SDavid Matlack 
2517812d80cSDavid Matlack uint64_t __weak memstress_nested_pages(int nr_vcpus)
2529fda6753SDavid Matlack {
2539fda6753SDavid Matlack 	return 0;
2549fda6753SDavid Matlack }
2559fda6753SDavid Matlack 
2567812d80cSDavid Matlack void __weak memstress_setup_nested(struct kvm_vm *vm, int nr_vcpus, struct kvm_vcpu **vcpus)
2579fda6753SDavid Matlack {
2589fda6753SDavid Matlack 	pr_info("%s() not support on this architecture, skipping.\n", __func__);
2599fda6753SDavid Matlack 	exit(KSFT_SKIP);
2609fda6753SDavid Matlack }
2619fda6753SDavid Matlack 
2629fda6753SDavid Matlack static void *vcpu_thread_main(void *data)
2639fda6753SDavid Matlack {
2649fda6753SDavid Matlack 	struct vcpu_thread *vcpu = data;
2659fda6753SDavid Matlack 	int vcpu_idx = vcpu->vcpu_idx;
2669fda6753SDavid Matlack 
2677812d80cSDavid Matlack 	if (memstress_args.pin_vcpus)
2687812d80cSDavid Matlack 		kvm_pin_this_task_to_pcpu(memstress_args.vcpu_to_pcpu[vcpu_idx]);
2699fda6753SDavid Matlack 
2709fda6753SDavid Matlack 	WRITE_ONCE(vcpu->running, true);
2719fda6753SDavid Matlack 
2729fda6753SDavid Matlack 	/*
2739fda6753SDavid Matlack 	 * Wait for all vCPU threads to be up and running before calling the test-
2749fda6753SDavid Matlack 	 * provided vCPU thread function. This prevents thread creation (which
2759fda6753SDavid Matlack 	 * requires taking the mmap_sem in write mode) from interfering with the
2769fda6753SDavid Matlack 	 * guest faulting in its memory.
2779fda6753SDavid Matlack 	 */
2789fda6753SDavid Matlack 	while (!READ_ONCE(all_vcpu_threads_running))
2799fda6753SDavid Matlack 		;
2809fda6753SDavid Matlack 
2817812d80cSDavid Matlack 	vcpu_thread_fn(&memstress_args.vcpu_args[vcpu_idx]);
2829fda6753SDavid Matlack 
2839fda6753SDavid Matlack 	return NULL;
2849fda6753SDavid Matlack }
2859fda6753SDavid Matlack 
2867812d80cSDavid Matlack void memstress_start_vcpu_threads(int nr_vcpus,
2877812d80cSDavid Matlack 				  void (*vcpu_fn)(struct memstress_vcpu_args *))
2889fda6753SDavid Matlack {
2899fda6753SDavid Matlack 	int i;
2909fda6753SDavid Matlack 
2919fda6753SDavid Matlack 	vcpu_thread_fn = vcpu_fn;
2929fda6753SDavid Matlack 	WRITE_ONCE(all_vcpu_threads_running, false);
293eb561891SPaolo Bonzini 	WRITE_ONCE(memstress_args.stop_vcpus, false);
2949fda6753SDavid Matlack 
2959fda6753SDavid Matlack 	for (i = 0; i < nr_vcpus; i++) {
2969fda6753SDavid Matlack 		struct vcpu_thread *vcpu = &vcpu_threads[i];
2979fda6753SDavid Matlack 
2989fda6753SDavid Matlack 		vcpu->vcpu_idx = i;
2999fda6753SDavid Matlack 		WRITE_ONCE(vcpu->running, false);
3009fda6753SDavid Matlack 
3019fda6753SDavid Matlack 		pthread_create(&vcpu->thread, NULL, vcpu_thread_main, vcpu);
3029fda6753SDavid Matlack 	}
3039fda6753SDavid Matlack 
3049fda6753SDavid Matlack 	for (i = 0; i < nr_vcpus; i++) {
3059fda6753SDavid Matlack 		while (!READ_ONCE(vcpu_threads[i].running))
3069fda6753SDavid Matlack 			;
3079fda6753SDavid Matlack 	}
3089fda6753SDavid Matlack 
3099fda6753SDavid Matlack 	WRITE_ONCE(all_vcpu_threads_running, true);
3109fda6753SDavid Matlack }
3119fda6753SDavid Matlack 
3127812d80cSDavid Matlack void memstress_join_vcpu_threads(int nr_vcpus)
3139fda6753SDavid Matlack {
3149fda6753SDavid Matlack 	int i;
3159fda6753SDavid Matlack 
316eb561891SPaolo Bonzini 	WRITE_ONCE(memstress_args.stop_vcpus, true);
317eb561891SPaolo Bonzini 
3189fda6753SDavid Matlack 	for (i = 0; i < nr_vcpus; i++)
3199fda6753SDavid Matlack 		pthread_join(vcpu_threads[i].thread, NULL);
3209fda6753SDavid Matlack }
321de10b798SBen Gardon 
322de10b798SBen Gardon static void toggle_dirty_logging(struct kvm_vm *vm, int slots, bool enable)
323de10b798SBen Gardon {
324de10b798SBen Gardon 	int i;
325de10b798SBen Gardon 
326de10b798SBen Gardon 	for (i = 0; i < slots; i++) {
327de10b798SBen Gardon 		int slot = MEMSTRESS_MEM_SLOT_INDEX + i;
328de10b798SBen Gardon 		int flags = enable ? KVM_MEM_LOG_DIRTY_PAGES : 0;
329de10b798SBen Gardon 
330de10b798SBen Gardon 		vm_mem_region_set_flags(vm, slot, flags);
331de10b798SBen Gardon 	}
332de10b798SBen Gardon }
333de10b798SBen Gardon 
334de10b798SBen Gardon void memstress_enable_dirty_logging(struct kvm_vm *vm, int slots)
335de10b798SBen Gardon {
336de10b798SBen Gardon 	toggle_dirty_logging(vm, slots, true);
337de10b798SBen Gardon }
338de10b798SBen Gardon 
339de10b798SBen Gardon void memstress_disable_dirty_logging(struct kvm_vm *vm, int slots)
340de10b798SBen Gardon {
341de10b798SBen Gardon 	toggle_dirty_logging(vm, slots, false);
342de10b798SBen Gardon }
343de10b798SBen Gardon 
344de10b798SBen Gardon void memstress_get_dirty_log(struct kvm_vm *vm, unsigned long *bitmaps[], int slots)
345de10b798SBen Gardon {
346de10b798SBen Gardon 	int i;
347de10b798SBen Gardon 
348de10b798SBen Gardon 	for (i = 0; i < slots; i++) {
349de10b798SBen Gardon 		int slot = MEMSTRESS_MEM_SLOT_INDEX + i;
350de10b798SBen Gardon 
351de10b798SBen Gardon 		kvm_vm_get_dirty_log(vm, slot, bitmaps[i]);
352de10b798SBen Gardon 	}
353de10b798SBen Gardon }
354de10b798SBen Gardon 
355de10b798SBen Gardon void memstress_clear_dirty_log(struct kvm_vm *vm, unsigned long *bitmaps[],
356de10b798SBen Gardon 			       int slots, uint64_t pages_per_slot)
357de10b798SBen Gardon {
358de10b798SBen Gardon 	int i;
359de10b798SBen Gardon 
360de10b798SBen Gardon 	for (i = 0; i < slots; i++) {
361de10b798SBen Gardon 		int slot = MEMSTRESS_MEM_SLOT_INDEX + i;
362de10b798SBen Gardon 
363de10b798SBen Gardon 		kvm_vm_clear_dirty_log(vm, slot, bitmaps[i], 0, pages_per_slot);
364de10b798SBen Gardon 	}
365de10b798SBen Gardon }
366de10b798SBen Gardon 
367de10b798SBen Gardon unsigned long **memstress_alloc_bitmaps(int slots, uint64_t pages_per_slot)
368de10b798SBen Gardon {
369de10b798SBen Gardon 	unsigned long **bitmaps;
370de10b798SBen Gardon 	int i;
371de10b798SBen Gardon 
372de10b798SBen Gardon 	bitmaps = malloc(slots * sizeof(bitmaps[0]));
373de10b798SBen Gardon 	TEST_ASSERT(bitmaps, "Failed to allocate bitmaps array.");
374de10b798SBen Gardon 
375de10b798SBen Gardon 	for (i = 0; i < slots; i++) {
376de10b798SBen Gardon 		bitmaps[i] = bitmap_zalloc(pages_per_slot);
377de10b798SBen Gardon 		TEST_ASSERT(bitmaps[i], "Failed to allocate slot bitmap.");
378de10b798SBen Gardon 	}
379de10b798SBen Gardon 
380de10b798SBen Gardon 	return bitmaps;
381de10b798SBen Gardon }
382de10b798SBen Gardon 
383de10b798SBen Gardon void memstress_free_bitmaps(unsigned long *bitmaps[], int slots)
384de10b798SBen Gardon {
385de10b798SBen Gardon 	int i;
386de10b798SBen Gardon 
387de10b798SBen Gardon 	for (i = 0; i < slots; i++)
388de10b798SBen Gardon 		free(bitmaps[i]);
389de10b798SBen Gardon 
390de10b798SBen Gardon 	free(bitmaps);
391de10b798SBen Gardon }
392