xref: /linux/tools/testing/selftests/kvm/s390x/shared_zeropage_test.c (revision 6e7fd890f1d6ac83805409e9c346240de2705584)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Test shared zeropage handling (with/without storage keys)
4  *
5  * Copyright (C) 2024, Red Hat, Inc.
6  */
7 #include <sys/mman.h>
8 
9 #include <linux/fs.h>
10 
11 #include "test_util.h"
12 #include "kvm_util.h"
13 #include "kselftest.h"
14 #include "ucall_common.h"
15 
16 static void set_storage_key(void *addr, uint8_t skey)
17 {
18 	asm volatile("sske %0,%1" : : "d" (skey), "a" (addr));
19 }
20 
21 static void guest_code(void)
22 {
23 	/* Issue some storage key instruction. */
24 	set_storage_key((void *)0, 0x98);
25 	GUEST_DONE();
26 }
27 
28 /*
29  * Returns 1 if the shared zeropage is mapped, 0 if something else is mapped.
30  * Returns < 0 on error or if nothing is mapped.
31  */
32 static int maps_shared_zeropage(int pagemap_fd, void *addr)
33 {
34 	struct page_region region;
35 	struct pm_scan_arg arg = {
36 		.start = (uintptr_t)addr,
37 		.end = (uintptr_t)addr + 4096,
38 		.vec = (uintptr_t)&region,
39 		.vec_len = 1,
40 		.size = sizeof(struct pm_scan_arg),
41 		.category_mask = PAGE_IS_PFNZERO,
42 		.category_anyof_mask = PAGE_IS_PRESENT,
43 		.return_mask = PAGE_IS_PFNZERO,
44 	};
45 	return ioctl(pagemap_fd, PAGEMAP_SCAN, &arg);
46 }
47 
48 int main(int argc, char *argv[])
49 {
50 	char *mem, *page0, *page1, *page2, tmp;
51 	const size_t pagesize = getpagesize();
52 	struct kvm_vcpu *vcpu;
53 	struct kvm_vm *vm;
54 	struct ucall uc;
55 	int pagemap_fd;
56 
57 	ksft_print_header();
58 	ksft_set_plan(3);
59 
60 	/*
61 	 * We'll use memory that is not mapped into the VM for simplicity.
62 	 * Shared zeropages are enabled/disabled per-process.
63 	 */
64 	mem = mmap(0, 3 * pagesize, PROT_READ, MAP_PRIVATE | MAP_ANON, -1, 0);
65 	TEST_ASSERT(mem != MAP_FAILED, "mmap() failed");
66 
67 	/* Disable THP. Ignore errors on older kernels. */
68 	madvise(mem, 3 * pagesize, MADV_NOHUGEPAGE);
69 
70 	page0 = mem;
71 	page1 = page0 + pagesize;
72 	page2 = page1 + pagesize;
73 
74 	/* Can we even detect shared zeropages? */
75 	pagemap_fd = open("/proc/self/pagemap", O_RDONLY);
76 	TEST_REQUIRE(pagemap_fd >= 0);
77 
78 	tmp = *page0;
79 	asm volatile("" : "+r" (tmp));
80 	TEST_REQUIRE(maps_shared_zeropage(pagemap_fd, page0) == 1);
81 
82 	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
83 
84 	/* Verify that we get the shared zeropage after VM creation. */
85 	tmp = *page1;
86 	asm volatile("" : "+r" (tmp));
87 	ksft_test_result(maps_shared_zeropage(pagemap_fd, page1) == 1,
88 			 "Shared zeropages should be enabled\n");
89 
90 	/*
91 	 * Let our VM execute a storage key instruction that should
92 	 * unshare all shared zeropages.
93 	 */
94 	vcpu_run(vcpu);
95 	get_ucall(vcpu, &uc);
96 	TEST_ASSERT_EQ(uc.cmd, UCALL_DONE);
97 
98 	/* Verify that we don't have a shared zeropage anymore. */
99 	ksft_test_result(!maps_shared_zeropage(pagemap_fd, page1),
100 			 "Shared zeropage should be gone\n");
101 
102 	/* Verify that we don't get any new shared zeropages. */
103 	tmp = *page2;
104 	asm volatile("" : "+r" (tmp));
105 	ksft_test_result(!maps_shared_zeropage(pagemap_fd, page2),
106 			 "Shared zeropages should be disabled\n");
107 
108 	kvm_vm_free(vm);
109 
110 	ksft_finished();
111 }
112