Skip to content

Commit 3a6c085

Browse files
committed
KVM: selftests: Stash the host page size in a global in the guest_memfd test
Use a global variable to track the host page size in the guest_memfd test so that the information doesn't need to be constantly passed around. The state is purely a reflection of the underlying system, i.e. can't be set by the test and is constant for a given invocation of the test, and thus explicitly passing the host page size to individual testcases adds no value, e.g. doesn't allow testing different combinations. Making page_size a global will simplify an upcoming change to create a new guest_memfd instance per testcase. No functional change intended. Reviewed-by: Fuad Tabba <tabba@google.com> Tested-by: Fuad Tabba <tabba@google.com> Reviewed-by: David Hildenbrand <david@redhat.com> Reviewed-by: Ackerley Tng <ackerleytng@google.com> Tested-by: Ackerley Tng <ackerleytng@google.com> Link: https://lore.kernel.org/r/20251003232606.4070510-7-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 44c6cb9 commit 3a6c085

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

tools/testing/selftests/kvm/guest_memfd_test.c

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include "test_util.h"
2525
#include "ucall_common.h"
2626

27+
static size_t page_size;
28+
2729
static void test_file_read_write(int fd)
2830
{
2931
char buf[64];
@@ -38,7 +40,7 @@ static void test_file_read_write(int fd)
3840
"pwrite on a guest_mem fd should fail");
3941
}
4042

41-
static void test_mmap_supported(int fd, size_t page_size, size_t total_size)
43+
static void test_mmap_supported(int fd, size_t total_size)
4244
{
4345
const char val = 0xaa;
4446
char *mem;
@@ -78,7 +80,7 @@ void fault_sigbus_handler(int signum)
7880
siglongjmp(jmpbuf, 1);
7981
}
8082

81-
static void test_fault_overflow(int fd, size_t page_size, size_t total_size)
83+
static void test_fault_overflow(int fd, size_t total_size)
8284
{
8385
struct sigaction sa_old, sa_new = {
8486
.sa_handler = fault_sigbus_handler,
@@ -106,7 +108,7 @@ static void test_fault_overflow(int fd, size_t page_size, size_t total_size)
106108
TEST_ASSERT(!ret, "munmap() should succeed.");
107109
}
108110

109-
static void test_mmap_not_supported(int fd, size_t page_size, size_t total_size)
111+
static void test_mmap_not_supported(int fd, size_t total_size)
110112
{
111113
char *mem;
112114

@@ -117,7 +119,7 @@ static void test_mmap_not_supported(int fd, size_t page_size, size_t total_size)
117119
TEST_ASSERT_EQ(mem, MAP_FAILED);
118120
}
119121

120-
static void test_file_size(int fd, size_t page_size, size_t total_size)
122+
static void test_file_size(int fd, size_t total_size)
121123
{
122124
struct stat sb;
123125
int ret;
@@ -128,7 +130,7 @@ static void test_file_size(int fd, size_t page_size, size_t total_size)
128130
TEST_ASSERT_EQ(sb.st_blksize, page_size);
129131
}
130132

131-
static void test_fallocate(int fd, size_t page_size, size_t total_size)
133+
static void test_fallocate(int fd, size_t total_size)
132134
{
133135
int ret;
134136

@@ -165,7 +167,7 @@ static void test_fallocate(int fd, size_t page_size, size_t total_size)
165167
TEST_ASSERT(!ret, "fallocate to restore punched hole should succeed");
166168
}
167169

168-
static void test_invalid_punch_hole(int fd, size_t page_size, size_t total_size)
170+
static void test_invalid_punch_hole(int fd, size_t total_size)
169171
{
170172
struct {
171173
off_t offset;
@@ -196,8 +198,7 @@ static void test_invalid_punch_hole(int fd, size_t page_size, size_t total_size)
196198
}
197199

198200
static void test_create_guest_memfd_invalid_sizes(struct kvm_vm *vm,
199-
uint64_t guest_memfd_flags,
200-
size_t page_size)
201+
uint64_t guest_memfd_flags)
201202
{
202203
size_t size;
203204
int fd;
@@ -214,7 +215,6 @@ static void test_create_guest_memfd_multiple(struct kvm_vm *vm)
214215
{
215216
int fd1, fd2, ret;
216217
struct stat st1, st2;
217-
size_t page_size = getpagesize();
218218

219219
fd1 = __vm_create_guest_memfd(vm, page_size, 0);
220220
TEST_ASSERT(fd1 != -1, "memfd creation should succeed");
@@ -242,7 +242,6 @@ static void test_create_guest_memfd_multiple(struct kvm_vm *vm)
242242
static void test_guest_memfd_flags(struct kvm_vm *vm)
243243
{
244244
uint64_t valid_flags = vm_check_cap(vm, KVM_CAP_GUEST_MEMFD_FLAGS);
245-
size_t page_size = getpagesize();
246245
uint64_t flag;
247246
int fd;
248247

@@ -265,11 +264,9 @@ static void test_guest_memfd(unsigned long vm_type)
265264
{
266265
struct kvm_vm *vm;
267266
size_t total_size;
268-
size_t page_size;
269267
uint64_t flags;
270268
int fd;
271269

272-
page_size = getpagesize();
273270
total_size = page_size * 4;
274271

275272
vm = vm_create_barebones_type(vm_type);
@@ -280,22 +277,22 @@ static void test_guest_memfd(unsigned long vm_type)
280277
flags &= ~GUEST_MEMFD_FLAG_MMAP;
281278

282279
test_create_guest_memfd_multiple(vm);
283-
test_create_guest_memfd_invalid_sizes(vm, flags, page_size);
280+
test_create_guest_memfd_invalid_sizes(vm, flags);
284281

285282
fd = vm_create_guest_memfd(vm, total_size, flags);
286283

287284
test_file_read_write(fd);
288285

289286
if (flags & GUEST_MEMFD_FLAG_MMAP) {
290-
test_mmap_supported(fd, page_size, total_size);
291-
test_fault_overflow(fd, page_size, total_size);
287+
test_mmap_supported(fd, total_size);
288+
test_fault_overflow(fd, total_size);
292289
} else {
293-
test_mmap_not_supported(fd, page_size, total_size);
290+
test_mmap_not_supported(fd, total_size);
294291
}
295292

296-
test_file_size(fd, page_size, total_size);
297-
test_fallocate(fd, page_size, total_size);
298-
test_invalid_punch_hole(fd, page_size, total_size);
293+
test_file_size(fd, total_size);
294+
test_fallocate(fd, total_size);
295+
test_invalid_punch_hole(fd, total_size);
299296

300297
test_guest_memfd_flags(vm);
301298

@@ -374,6 +371,8 @@ int main(int argc, char *argv[])
374371

375372
TEST_REQUIRE(kvm_has_cap(KVM_CAP_GUEST_MEMFD));
376373

374+
page_size = getpagesize();
375+
377376
/*
378377
* Not all architectures support KVM_CAP_VM_TYPES. However, those that
379378
* support guest_memfd have that support for the default VM type.

0 commit comments

Comments
 (0)