2020-01-23 10:04:27 -08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
|
|
|
* KVM demand paging test
|
|
|
|
* Adapted from dirty_log_test.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018, Red Hat, Inc.
|
|
|
|
* Copyright (C) 2019, Google, Inc.
|
|
|
|
*/
|
2021-05-19 13:03:31 -07:00
|
|
|
#include <inttypes.h>
|
2020-01-23 10:04:27 -08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <pthread.h>
|
2020-02-20 18:09:12 +01:00
|
|
|
#include <linux/userfaultfd.h>
|
2020-12-18 15:17:32 +01:00
|
|
|
#include <sys/syscall.h>
|
2020-01-23 10:04:27 -08:00
|
|
|
|
2020-12-18 15:17:32 +01:00
|
|
|
#include "kvm_util.h"
|
2020-10-27 16:37:29 -07:00
|
|
|
#include "test_util.h"
|
2022-10-12 09:57:27 -07:00
|
|
|
#include "memstress.h"
|
2020-12-18 15:17:32 +01:00
|
|
|
#include "guest_modes.h"
|
2024-03-14 16:26:20 -07:00
|
|
|
#include "ucall_common.h"
|
2022-10-17 19:58:21 +00:00
|
|
|
#include "userfaultfd_util.h"
|
2020-01-23 10:04:27 -08:00
|
|
|
|
2020-02-20 18:09:12 +01:00
|
|
|
#ifdef __NR_userfaultfd
|
2020-01-23 10:04:27 -08:00
|
|
|
|
2020-12-18 15:17:34 +01:00
|
|
|
static int nr_vcpus = 1;
|
|
|
|
static uint64_t guest_percpu_mem_size = DEFAULT_PER_VCPU_MEM_SIZE;
|
2022-10-17 19:58:21 +00:00
|
|
|
|
2021-05-19 13:03:33 -07:00
|
|
|
static size_t demand_paging_size;
|
2020-02-20 18:09:12 +01:00
|
|
|
static char *guest_data_prototype;
|
|
|
|
|
2022-10-12 09:57:29 -07:00
|
|
|
static void vcpu_worker(struct memstress_vcpu_args *vcpu_args)
|
2020-01-23 10:04:27 -08:00
|
|
|
{
|
2022-02-16 13:38:12 -08:00
|
|
|
struct kvm_vcpu *vcpu = vcpu_args->vcpu;
|
|
|
|
int vcpu_idx = vcpu_args->vcpu_idx;
|
|
|
|
struct kvm_run *run = vcpu->run;
|
2020-10-27 16:37:31 -07:00
|
|
|
struct timespec start;
|
|
|
|
struct timespec ts_diff;
|
2022-02-16 13:38:12 -08:00
|
|
|
int ret;
|
2020-01-23 10:04:27 -08:00
|
|
|
|
2020-01-23 10:04:34 -08:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &start);
|
|
|
|
|
2020-01-23 10:04:27 -08:00
|
|
|
/* Let the guest access its memory */
|
2022-06-02 13:41:33 -07:00
|
|
|
ret = _vcpu_run(vcpu);
|
2023-12-06 18:02:43 +01:00
|
|
|
TEST_ASSERT(ret == 0, "vcpu_run failed: %d", ret);
|
2022-06-02 13:41:33 -07:00
|
|
|
if (get_ucall(vcpu, NULL) != UCALL_SYNC) {
|
2020-01-23 10:04:27 -08:00
|
|
|
TEST_ASSERT(false,
|
2023-12-06 18:02:43 +01:00
|
|
|
"Invalid guest sync status: exit_reason=%s",
|
2020-01-23 10:04:27 -08:00
|
|
|
exit_reason_str(run->exit_reason));
|
|
|
|
}
|
|
|
|
|
2021-01-12 13:42:48 -08:00
|
|
|
ts_diff = timespec_elapsed(start);
|
2022-02-16 13:38:12 -08:00
|
|
|
PER_VCPU_DEBUG("vCPU %d execution time: %ld.%.9lds\n", vcpu_idx,
|
2020-03-16 18:37:03 +01:00
|
|
|
ts_diff.tv_sec, ts_diff.tv_nsec);
|
2020-01-23 10:04:27 -08:00
|
|
|
}
|
|
|
|
|
2022-10-17 19:58:21 +00:00
|
|
|
static int handle_uffd_page_request(int uffd_mode, int uffd,
|
|
|
|
struct uffd_msg *msg)
|
2020-02-20 18:09:12 +01:00
|
|
|
{
|
KVM: selftests: allow using UFFD minor faults for demand paging
UFFD handling of MINOR faults is a new feature whose use case is to
speed up demand paging (compared to MISSING faults). So, it's
interesting to let this selftest exercise this new mode.
Modify the demand paging test to have the option of using UFFD minor
faults, as opposed to missing faults. Now, when turning on userfaultfd
with '-u', the desired mode has to be specified ("MISSING" or "MINOR").
If we're in minor mode, before registering, prefault via the *alias*.
This way, the guest will trigger minor faults, instead of missing
faults, and we can UFFDIO_CONTINUE to resolve them.
Modify the page fault handler function to use the right ioctl depending
on the mode we're running in. In MINOR mode, use UFFDIO_CONTINUE.
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
Message-Id: <20210519200339.829146-10-axelrasmussen@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2021-05-19 13:03:38 -07:00
|
|
|
pid_t tid = syscall(__NR_gettid);
|
2022-10-17 19:58:21 +00:00
|
|
|
uint64_t addr = msg->arg.pagefault.address;
|
2020-01-23 10:04:34 -08:00
|
|
|
struct timespec start;
|
2020-10-27 16:37:31 -07:00
|
|
|
struct timespec ts_diff;
|
2020-02-20 18:09:12 +01:00
|
|
|
int r;
|
|
|
|
|
KVM: selftests: allow using UFFD minor faults for demand paging
UFFD handling of MINOR faults is a new feature whose use case is to
speed up demand paging (compared to MISSING faults). So, it's
interesting to let this selftest exercise this new mode.
Modify the demand paging test to have the option of using UFFD minor
faults, as opposed to missing faults. Now, when turning on userfaultfd
with '-u', the desired mode has to be specified ("MISSING" or "MINOR").
If we're in minor mode, before registering, prefault via the *alias*.
This way, the guest will trigger minor faults, instead of missing
faults, and we can UFFDIO_CONTINUE to resolve them.
Modify the page fault handler function to use the right ioctl depending
on the mode we're running in. In MINOR mode, use UFFDIO_CONTINUE.
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
Message-Id: <20210519200339.829146-10-axelrasmussen@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2021-05-19 13:03:38 -07:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &start);
|
2020-02-20 18:09:12 +01:00
|
|
|
|
KVM: selftests: allow using UFFD minor faults for demand paging
UFFD handling of MINOR faults is a new feature whose use case is to
speed up demand paging (compared to MISSING faults). So, it's
interesting to let this selftest exercise this new mode.
Modify the demand paging test to have the option of using UFFD minor
faults, as opposed to missing faults. Now, when turning on userfaultfd
with '-u', the desired mode has to be specified ("MISSING" or "MINOR").
If we're in minor mode, before registering, prefault via the *alias*.
This way, the guest will trigger minor faults, instead of missing
faults, and we can UFFDIO_CONTINUE to resolve them.
Modify the page fault handler function to use the right ioctl depending
on the mode we're running in. In MINOR mode, use UFFDIO_CONTINUE.
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
Message-Id: <20210519200339.829146-10-axelrasmussen@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2021-05-19 13:03:38 -07:00
|
|
|
if (uffd_mode == UFFDIO_REGISTER_MODE_MISSING) {
|
|
|
|
struct uffdio_copy copy;
|
2020-02-20 18:09:12 +01:00
|
|
|
|
KVM: selftests: allow using UFFD minor faults for demand paging
UFFD handling of MINOR faults is a new feature whose use case is to
speed up demand paging (compared to MISSING faults). So, it's
interesting to let this selftest exercise this new mode.
Modify the demand paging test to have the option of using UFFD minor
faults, as opposed to missing faults. Now, when turning on userfaultfd
with '-u', the desired mode has to be specified ("MISSING" or "MINOR").
If we're in minor mode, before registering, prefault via the *alias*.
This way, the guest will trigger minor faults, instead of missing
faults, and we can UFFDIO_CONTINUE to resolve them.
Modify the page fault handler function to use the right ioctl depending
on the mode we're running in. In MINOR mode, use UFFDIO_CONTINUE.
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
Message-Id: <20210519200339.829146-10-axelrasmussen@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2021-05-19 13:03:38 -07:00
|
|
|
copy.src = (uint64_t)guest_data_prototype;
|
|
|
|
copy.dst = addr;
|
|
|
|
copy.len = demand_paging_size;
|
|
|
|
copy.mode = 0;
|
|
|
|
|
|
|
|
r = ioctl(uffd, UFFDIO_COPY, ©);
|
2024-02-15 23:54:02 +00:00
|
|
|
/*
|
|
|
|
* With multiple vCPU threads fault on a single page and there are
|
|
|
|
* multiple readers for the UFFD, at least one of the UFFDIO_COPYs
|
|
|
|
* will fail with EEXIST: handle that case without signaling an
|
|
|
|
* error.
|
|
|
|
*
|
|
|
|
* Note that this also suppress any EEXISTs occurring from,
|
|
|
|
* e.g., the first UFFDIO_COPY/CONTINUEs on a page. That never
|
|
|
|
* happens here, but a realistic VMM might potentially maintain
|
|
|
|
* some external state to correctly surface EEXISTs to userspace
|
|
|
|
* (or prevent duplicate COPY/CONTINUEs in the first place).
|
|
|
|
*/
|
|
|
|
if (r == -1 && errno != EEXIST) {
|
|
|
|
pr_info("Failed UFFDIO_COPY in 0x%lx from thread %d, errno = %d\n",
|
KVM: selftests: allow using UFFD minor faults for demand paging
UFFD handling of MINOR faults is a new feature whose use case is to
speed up demand paging (compared to MISSING faults). So, it's
interesting to let this selftest exercise this new mode.
Modify the demand paging test to have the option of using UFFD minor
faults, as opposed to missing faults. Now, when turning on userfaultfd
with '-u', the desired mode has to be specified ("MISSING" or "MINOR").
If we're in minor mode, before registering, prefault via the *alias*.
This way, the guest will trigger minor faults, instead of missing
faults, and we can UFFDIO_CONTINUE to resolve them.
Modify the page fault handler function to use the right ioctl depending
on the mode we're running in. In MINOR mode, use UFFDIO_CONTINUE.
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
Message-Id: <20210519200339.829146-10-axelrasmussen@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2021-05-19 13:03:38 -07:00
|
|
|
addr, tid, errno);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
} else if (uffd_mode == UFFDIO_REGISTER_MODE_MINOR) {
|
|
|
|
struct uffdio_continue cont = {0};
|
|
|
|
|
|
|
|
cont.range.start = addr;
|
|
|
|
cont.range.len = demand_paging_size;
|
2020-01-23 10:04:34 -08:00
|
|
|
|
KVM: selftests: allow using UFFD minor faults for demand paging
UFFD handling of MINOR faults is a new feature whose use case is to
speed up demand paging (compared to MISSING faults). So, it's
interesting to let this selftest exercise this new mode.
Modify the demand paging test to have the option of using UFFD minor
faults, as opposed to missing faults. Now, when turning on userfaultfd
with '-u', the desired mode has to be specified ("MISSING" or "MINOR").
If we're in minor mode, before registering, prefault via the *alias*.
This way, the guest will trigger minor faults, instead of missing
faults, and we can UFFDIO_CONTINUE to resolve them.
Modify the page fault handler function to use the right ioctl depending
on the mode we're running in. In MINOR mode, use UFFDIO_CONTINUE.
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
Message-Id: <20210519200339.829146-10-axelrasmussen@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2021-05-19 13:03:38 -07:00
|
|
|
r = ioctl(uffd, UFFDIO_CONTINUE, &cont);
|
2024-02-15 23:54:02 +00:00
|
|
|
/*
|
|
|
|
* With multiple vCPU threads fault on a single page and there are
|
|
|
|
* multiple readers for the UFFD, at least one of the UFFDIO_COPYs
|
|
|
|
* will fail with EEXIST: handle that case without signaling an
|
|
|
|
* error.
|
|
|
|
*
|
|
|
|
* Note that this also suppress any EEXISTs occurring from,
|
|
|
|
* e.g., the first UFFDIO_COPY/CONTINUEs on a page. That never
|
|
|
|
* happens here, but a realistic VMM might potentially maintain
|
|
|
|
* some external state to correctly surface EEXISTs to userspace
|
|
|
|
* (or prevent duplicate COPY/CONTINUEs in the first place).
|
|
|
|
*/
|
|
|
|
if (r == -1 && errno != EEXIST) {
|
|
|
|
pr_info("Failed UFFDIO_CONTINUE in 0x%lx, thread %d, errno = %d\n",
|
KVM: selftests: allow using UFFD minor faults for demand paging
UFFD handling of MINOR faults is a new feature whose use case is to
speed up demand paging (compared to MISSING faults). So, it's
interesting to let this selftest exercise this new mode.
Modify the demand paging test to have the option of using UFFD minor
faults, as opposed to missing faults. Now, when turning on userfaultfd
with '-u', the desired mode has to be specified ("MISSING" or "MINOR").
If we're in minor mode, before registering, prefault via the *alias*.
This way, the guest will trigger minor faults, instead of missing
faults, and we can UFFDIO_CONTINUE to resolve them.
Modify the page fault handler function to use the right ioctl depending
on the mode we're running in. In MINOR mode, use UFFDIO_CONTINUE.
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
Message-Id: <20210519200339.829146-10-axelrasmussen@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2021-05-19 13:03:38 -07:00
|
|
|
addr, tid, errno);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
TEST_FAIL("Invalid uffd mode %d", uffd_mode);
|
2020-02-20 18:09:12 +01:00
|
|
|
}
|
|
|
|
|
2021-01-12 13:42:48 -08:00
|
|
|
ts_diff = timespec_elapsed(start);
|
2020-01-23 10:04:34 -08:00
|
|
|
|
KVM: selftests: allow using UFFD minor faults for demand paging
UFFD handling of MINOR faults is a new feature whose use case is to
speed up demand paging (compared to MISSING faults). So, it's
interesting to let this selftest exercise this new mode.
Modify the demand paging test to have the option of using UFFD minor
faults, as opposed to missing faults. Now, when turning on userfaultfd
with '-u', the desired mode has to be specified ("MISSING" or "MINOR").
If we're in minor mode, before registering, prefault via the *alias*.
This way, the guest will trigger minor faults, instead of missing
faults, and we can UFFDIO_CONTINUE to resolve them.
Modify the page fault handler function to use the right ioctl depending
on the mode we're running in. In MINOR mode, use UFFDIO_CONTINUE.
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
Message-Id: <20210519200339.829146-10-axelrasmussen@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2021-05-19 13:03:38 -07:00
|
|
|
PER_PAGE_DEBUG("UFFD page-in %d \t%ld ns\n", tid,
|
2020-10-27 16:37:31 -07:00
|
|
|
timespec_to_ns(ts_diff));
|
2020-01-23 10:04:34 -08:00
|
|
|
PER_PAGE_DEBUG("Paged in %ld bytes at 0x%lx from thread %d\n",
|
2021-05-19 13:03:33 -07:00
|
|
|
demand_paging_size, addr, tid);
|
2020-01-23 10:04:34 -08:00
|
|
|
|
2020-02-20 18:09:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-10-17 19:58:21 +00:00
|
|
|
struct test_params {
|
KVM: selftests: allow using UFFD minor faults for demand paging
UFFD handling of MINOR faults is a new feature whose use case is to
speed up demand paging (compared to MISSING faults). So, it's
interesting to let this selftest exercise this new mode.
Modify the demand paging test to have the option of using UFFD minor
faults, as opposed to missing faults. Now, when turning on userfaultfd
with '-u', the desired mode has to be specified ("MISSING" or "MINOR").
If we're in minor mode, before registering, prefault via the *alias*.
This way, the guest will trigger minor faults, instead of missing
faults, and we can UFFDIO_CONTINUE to resolve them.
Modify the page fault handler function to use the right ioctl depending
on the mode we're running in. In MINOR mode, use UFFDIO_CONTINUE.
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
Message-Id: <20210519200339.829146-10-axelrasmussen@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2021-05-19 13:03:38 -07:00
|
|
|
int uffd_mode;
|
2024-02-15 23:54:02 +00:00
|
|
|
bool single_uffd;
|
2022-10-17 19:58:21 +00:00
|
|
|
useconds_t uffd_delay;
|
2024-02-15 23:54:02 +00:00
|
|
|
int readers_per_uffd;
|
2022-10-17 19:58:21 +00:00
|
|
|
enum vm_mem_backing_src_type src_type;
|
|
|
|
bool partition_vcpu_memory_access;
|
2020-02-20 18:09:12 +01:00
|
|
|
};
|
|
|
|
|
2022-10-17 19:58:21 +00:00
|
|
|
static void prefault_mem(void *alias, uint64_t len)
|
2020-02-20 18:09:12 +01:00
|
|
|
{
|
2022-10-17 19:58:21 +00:00
|
|
|
size_t p;
|
KVM: selftests: allow using UFFD minor faults for demand paging
UFFD handling of MINOR faults is a new feature whose use case is to
speed up demand paging (compared to MISSING faults). So, it's
interesting to let this selftest exercise this new mode.
Modify the demand paging test to have the option of using UFFD minor
faults, as opposed to missing faults. Now, when turning on userfaultfd
with '-u', the desired mode has to be specified ("MISSING" or "MINOR").
If we're in minor mode, before registering, prefault via the *alias*.
This way, the guest will trigger minor faults, instead of missing
faults, and we can UFFDIO_CONTINUE to resolve them.
Modify the page fault handler function to use the right ioctl depending
on the mode we're running in. In MINOR mode, use UFFDIO_CONTINUE.
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
Message-Id: <20210519200339.829146-10-axelrasmussen@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2021-05-19 13:03:38 -07:00
|
|
|
|
2022-10-17 19:58:21 +00:00
|
|
|
TEST_ASSERT(alias != NULL, "Alias required for minor faults");
|
|
|
|
for (p = 0; p < (len / demand_paging_size); ++p) {
|
|
|
|
memcpy(alias + (p * demand_paging_size),
|
|
|
|
guest_data_prototype, demand_paging_size);
|
KVM: selftests: allow using UFFD minor faults for demand paging
UFFD handling of MINOR faults is a new feature whose use case is to
speed up demand paging (compared to MISSING faults). So, it's
interesting to let this selftest exercise this new mode.
Modify the demand paging test to have the option of using UFFD minor
faults, as opposed to missing faults. Now, when turning on userfaultfd
with '-u', the desired mode has to be specified ("MISSING" or "MINOR").
If we're in minor mode, before registering, prefault via the *alias*.
This way, the guest will trigger minor faults, instead of missing
faults, and we can UFFDIO_CONTINUE to resolve them.
Modify the page fault handler function to use the right ioctl depending
on the mode we're running in. In MINOR mode, use UFFDIO_CONTINUE.
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
Message-Id: <20210519200339.829146-10-axelrasmussen@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2021-05-19 13:03:38 -07:00
|
|
|
}
|
2020-02-20 18:09:12 +01:00
|
|
|
}
|
|
|
|
|
2020-12-18 15:17:32 +01:00
|
|
|
static void run_test(enum vm_guest_mode mode, void *arg)
|
2020-01-23 10:04:27 -08:00
|
|
|
{
|
2023-04-27 16:11:11 -04:00
|
|
|
struct memstress_vcpu_args *vcpu_args;
|
2020-12-18 15:17:32 +01:00
|
|
|
struct test_params *p = arg;
|
2022-10-17 19:58:21 +00:00
|
|
|
struct uffd_desc **uffd_descs = NULL;
|
2024-02-15 23:54:02 +00:00
|
|
|
uint64_t uffd_region_size;
|
2020-10-27 16:37:31 -07:00
|
|
|
struct timespec start;
|
|
|
|
struct timespec ts_diff;
|
2024-02-15 23:54:01 +00:00
|
|
|
double vcpu_paging_rate;
|
2020-01-23 10:04:27 -08:00
|
|
|
struct kvm_vm *vm;
|
2024-02-15 23:54:02 +00:00
|
|
|
int i, num_uffds = 0;
|
2020-01-23 10:04:27 -08:00
|
|
|
|
2022-10-12 09:57:29 -07:00
|
|
|
vm = memstress_create_vm(mode, nr_vcpus, guest_percpu_mem_size, 1,
|
2021-11-11 00:03:09 +00:00
|
|
|
p->src_type, p->partition_vcpu_memory_access);
|
2020-01-23 10:04:27 -08:00
|
|
|
|
2021-05-19 13:03:34 -07:00
|
|
|
demand_paging_size = get_backing_src_pagesz(p->src_type);
|
2021-05-19 13:03:33 -07:00
|
|
|
|
|
|
|
guest_data_prototype = malloc(demand_paging_size);
|
2020-01-23 10:04:33 -08:00
|
|
|
TEST_ASSERT(guest_data_prototype,
|
|
|
|
"Failed to allocate buffer for guest data pattern");
|
2021-05-19 13:03:33 -07:00
|
|
|
memset(guest_data_prototype, 0xAB, demand_paging_size);
|
2020-01-23 10:04:33 -08:00
|
|
|
|
2023-04-27 16:11:11 -04:00
|
|
|
if (p->uffd_mode == UFFDIO_REGISTER_MODE_MINOR) {
|
2024-02-15 23:54:02 +00:00
|
|
|
num_uffds = p->single_uffd ? 1 : nr_vcpus;
|
|
|
|
for (i = 0; i < num_uffds; i++) {
|
2023-04-27 16:11:11 -04:00
|
|
|
vcpu_args = &memstress_args.vcpu_args[i];
|
|
|
|
prefault_mem(addr_gpa2alias(vm, vcpu_args->gpa),
|
|
|
|
vcpu_args->pages * memstress_args.guest_page_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
KVM: selftests: allow using UFFD minor faults for demand paging
UFFD handling of MINOR faults is a new feature whose use case is to
speed up demand paging (compared to MISSING faults). So, it's
interesting to let this selftest exercise this new mode.
Modify the demand paging test to have the option of using UFFD minor
faults, as opposed to missing faults. Now, when turning on userfaultfd
with '-u', the desired mode has to be specified ("MISSING" or "MINOR").
If we're in minor mode, before registering, prefault via the *alias*.
This way, the guest will trigger minor faults, instead of missing
faults, and we can UFFDIO_CONTINUE to resolve them.
Modify the page fault handler function to use the right ioctl depending
on the mode we're running in. In MINOR mode, use UFFDIO_CONTINUE.
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
Message-Id: <20210519200339.829146-10-axelrasmussen@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2021-05-19 13:03:38 -07:00
|
|
|
if (p->uffd_mode) {
|
2024-02-15 23:54:02 +00:00
|
|
|
num_uffds = p->single_uffd ? 1 : nr_vcpus;
|
|
|
|
uffd_region_size = nr_vcpus * guest_percpu_mem_size / num_uffds;
|
|
|
|
|
|
|
|
uffd_descs = malloc(num_uffds * sizeof(struct uffd_desc *));
|
2022-10-17 19:58:21 +00:00
|
|
|
TEST_ASSERT(uffd_descs, "Memory allocation failed");
|
2024-02-15 23:54:02 +00:00
|
|
|
for (i = 0; i < num_uffds; i++) {
|
|
|
|
struct memstress_vcpu_args *vcpu_args;
|
2020-10-27 16:37:29 -07:00
|
|
|
void *vcpu_hva;
|
2020-01-23 10:04:33 -08:00
|
|
|
|
2022-10-12 09:57:29 -07:00
|
|
|
vcpu_args = &memstress_args.vcpu_args[i];
|
2020-01-23 10:04:33 -08:00
|
|
|
|
KVM: selftests: allow using UFFD minor faults for demand paging
UFFD handling of MINOR faults is a new feature whose use case is to
speed up demand paging (compared to MISSING faults). So, it's
interesting to let this selftest exercise this new mode.
Modify the demand paging test to have the option of using UFFD minor
faults, as opposed to missing faults. Now, when turning on userfaultfd
with '-u', the desired mode has to be specified ("MISSING" or "MINOR").
If we're in minor mode, before registering, prefault via the *alias*.
This way, the guest will trigger minor faults, instead of missing
faults, and we can UFFDIO_CONTINUE to resolve them.
Modify the page fault handler function to use the right ioctl depending
on the mode we're running in. In MINOR mode, use UFFDIO_CONTINUE.
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
Message-Id: <20210519200339.829146-10-axelrasmussen@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2021-05-19 13:03:38 -07:00
|
|
|
/* Cache the host addresses of the region */
|
2021-11-11 00:03:05 +00:00
|
|
|
vcpu_hva = addr_gpa2hva(vm, vcpu_args->gpa);
|
2020-01-23 10:04:33 -08:00
|
|
|
/*
|
|
|
|
* Set up user fault fd to handle demand paging
|
|
|
|
* requests.
|
|
|
|
*/
|
2022-10-17 19:58:21 +00:00
|
|
|
uffd_descs[i] = uffd_setup_demand_paging(
|
|
|
|
p->uffd_mode, p->uffd_delay, vcpu_hva,
|
2024-02-15 23:54:02 +00:00
|
|
|
uffd_region_size,
|
|
|
|
p->readers_per_uffd,
|
2022-10-17 19:58:21 +00:00
|
|
|
&handle_uffd_page_request);
|
2020-01-23 10:04:33 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-14 15:59:16 +01:00
|
|
|
pr_info("Finished creating vCPUs and starting uffd threads\n");
|
2020-01-23 10:04:33 -08:00
|
|
|
|
2020-01-23 10:04:34 -08:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &start);
|
2022-10-12 09:57:29 -07:00
|
|
|
memstress_start_vcpu_threads(nr_vcpus, vcpu_worker);
|
2020-02-14 15:59:16 +01:00
|
|
|
pr_info("Started all vCPUs\n");
|
2020-01-23 10:04:33 -08:00
|
|
|
|
2022-10-12 09:57:29 -07:00
|
|
|
memstress_join_vcpu_threads(nr_vcpus);
|
2021-01-12 13:42:48 -08:00
|
|
|
ts_diff = timespec_elapsed(start);
|
2020-10-27 16:37:31 -07:00
|
|
|
pr_info("All vCPU threads joined\n");
|
2020-01-23 10:04:34 -08:00
|
|
|
|
KVM: selftests: allow using UFFD minor faults for demand paging
UFFD handling of MINOR faults is a new feature whose use case is to
speed up demand paging (compared to MISSING faults). So, it's
interesting to let this selftest exercise this new mode.
Modify the demand paging test to have the option of using UFFD minor
faults, as opposed to missing faults. Now, when turning on userfaultfd
with '-u', the desired mode has to be specified ("MISSING" or "MINOR").
If we're in minor mode, before registering, prefault via the *alias*.
This way, the guest will trigger minor faults, instead of missing
faults, and we can UFFDIO_CONTINUE to resolve them.
Modify the page fault handler function to use the right ioctl depending
on the mode we're running in. In MINOR mode, use UFFDIO_CONTINUE.
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
Message-Id: <20210519200339.829146-10-axelrasmussen@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2021-05-19 13:03:38 -07:00
|
|
|
if (p->uffd_mode) {
|
2020-01-23 10:04:33 -08:00
|
|
|
/* Tell the user fault fd handler threads to quit */
|
2024-02-15 23:54:02 +00:00
|
|
|
for (i = 0; i < num_uffds; i++)
|
2022-10-17 19:58:21 +00:00
|
|
|
uffd_stop_demand_paging(uffd_descs[i]);
|
2020-02-20 18:09:12 +01:00
|
|
|
}
|
|
|
|
|
2024-02-15 23:54:01 +00:00
|
|
|
pr_info("Total guest execution time:\t%ld.%.9lds\n",
|
2020-03-16 18:37:03 +01:00
|
|
|
ts_diff.tv_sec, ts_diff.tv_nsec);
|
2024-02-15 23:54:01 +00:00
|
|
|
|
|
|
|
vcpu_paging_rate = memstress_args.vcpu_args[0].pages /
|
|
|
|
((double)ts_diff.tv_sec + (double)ts_diff.tv_nsec / NSEC_PER_SEC);
|
|
|
|
pr_info("Per-vcpu demand paging rate:\t%f pgs/sec/vcpu\n",
|
|
|
|
vcpu_paging_rate);
|
|
|
|
pr_info("Overall demand paging rate:\t%f pgs/sec\n",
|
|
|
|
vcpu_paging_rate * nr_vcpus);
|
2020-01-23 10:04:34 -08:00
|
|
|
|
2022-10-12 09:57:29 -07:00
|
|
|
memstress_destroy_vm(vm);
|
2020-02-20 18:09:12 +01:00
|
|
|
|
|
|
|
free(guest_data_prototype);
|
2022-10-17 19:58:21 +00:00
|
|
|
if (p->uffd_mode)
|
|
|
|
free(uffd_descs);
|
2020-01-23 10:04:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void help(char *name)
|
|
|
|
{
|
|
|
|
puts("");
|
2024-02-15 23:54:02 +00:00
|
|
|
printf("usage: %s [-h] [-m vm_mode] [-u uffd_mode] [-a]\n"
|
|
|
|
" [-d uffd_delay_usec] [-r readers_per_uffd] [-b memory]\n"
|
|
|
|
" [-s type] [-v vcpus] [-c cpu_list] [-o]\n", name);
|
2020-12-18 15:17:32 +01:00
|
|
|
guest_modes_help();
|
KVM: selftests: allow using UFFD minor faults for demand paging
UFFD handling of MINOR faults is a new feature whose use case is to
speed up demand paging (compared to MISSING faults). So, it's
interesting to let this selftest exercise this new mode.
Modify the demand paging test to have the option of using UFFD minor
faults, as opposed to missing faults. Now, when turning on userfaultfd
with '-u', the desired mode has to be specified ("MISSING" or "MINOR").
If we're in minor mode, before registering, prefault via the *alias*.
This way, the guest will trigger minor faults, instead of missing
faults, and we can UFFDIO_CONTINUE to resolve them.
Modify the page fault handler function to use the right ioctl depending
on the mode we're running in. In MINOR mode, use UFFDIO_CONTINUE.
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
Message-Id: <20210519200339.829146-10-axelrasmussen@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2021-05-19 13:03:38 -07:00
|
|
|
printf(" -u: use userfaultfd to handle vCPU page faults. Mode is a\n"
|
|
|
|
" UFFD registration mode: 'MISSING' or 'MINOR'.\n");
|
2023-06-06 17:12:26 -07:00
|
|
|
kvm_print_vcpu_pinning_help();
|
2024-02-15 23:54:02 +00:00
|
|
|
printf(" -a: Use a single userfaultfd for all of guest memory, instead of\n"
|
|
|
|
" creating one for each region paged by a unique vCPU\n"
|
|
|
|
" Set implicitly with -o, and no effect without -u.\n");
|
2020-02-20 18:09:59 +01:00
|
|
|
printf(" -d: add a delay in usec to the User Fault\n"
|
|
|
|
" FD handler to simulate demand paging\n"
|
|
|
|
" overheads. Ignored without -u.\n");
|
2024-02-15 23:54:02 +00:00
|
|
|
printf(" -r: Set the number of reader threads per uffd.\n");
|
2020-01-23 10:04:30 -08:00
|
|
|
printf(" -b: specify the size of the memory region which should be\n"
|
2020-01-23 10:04:33 -08:00
|
|
|
" demand paged by each vCPU. e.g. 10M or 3G.\n"
|
|
|
|
" Default: 1G\n");
|
2021-09-17 17:36:56 +00:00
|
|
|
backing_src_help("-s");
|
2020-01-23 10:04:33 -08:00
|
|
|
printf(" -v: specify the number of vCPUs to run.\n");
|
2021-01-12 13:42:52 -08:00
|
|
|
printf(" -o: Overlap guest memory accesses instead of partitioning\n"
|
|
|
|
" them into a separate region of memory for each vCPU.\n");
|
2020-01-23 10:04:27 -08:00
|
|
|
puts("");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2020-11-04 22:23:53 +01:00
|
|
|
int max_vcpus = kvm_check_cap(KVM_CAP_MAX_VCPUS);
|
2023-06-06 17:12:26 -07:00
|
|
|
const char *cpulist = NULL;
|
2021-01-12 13:42:52 -08:00
|
|
|
struct test_params p = {
|
2021-09-17 17:36:56 +00:00
|
|
|
.src_type = DEFAULT_VM_MEM_SRC,
|
2021-01-12 13:42:52 -08:00
|
|
|
.partition_vcpu_memory_access = true,
|
2024-02-15 23:54:02 +00:00
|
|
|
.readers_per_uffd = 1,
|
|
|
|
.single_uffd = false,
|
2021-01-12 13:42:52 -08:00
|
|
|
};
|
2020-12-18 15:17:32 +01:00
|
|
|
int opt;
|
|
|
|
|
|
|
|
guest_modes_append_default();
|
2020-01-23 10:04:27 -08:00
|
|
|
|
2024-02-15 23:54:02 +00:00
|
|
|
while ((opt = getopt(argc, argv, "ahom:u:d:b:s:v:c:r:")) != -1) {
|
2020-01-23 10:04:27 -08:00
|
|
|
switch (opt) {
|
|
|
|
case 'm':
|
2020-12-18 15:17:32 +01:00
|
|
|
guest_modes_cmdline(optarg);
|
2020-01-23 10:04:27 -08:00
|
|
|
break;
|
2020-02-20 18:09:12 +01:00
|
|
|
case 'u':
|
KVM: selftests: allow using UFFD minor faults for demand paging
UFFD handling of MINOR faults is a new feature whose use case is to
speed up demand paging (compared to MISSING faults). So, it's
interesting to let this selftest exercise this new mode.
Modify the demand paging test to have the option of using UFFD minor
faults, as opposed to missing faults. Now, when turning on userfaultfd
with '-u', the desired mode has to be specified ("MISSING" or "MINOR").
If we're in minor mode, before registering, prefault via the *alias*.
This way, the guest will trigger minor faults, instead of missing
faults, and we can UFFDIO_CONTINUE to resolve them.
Modify the page fault handler function to use the right ioctl depending
on the mode we're running in. In MINOR mode, use UFFDIO_CONTINUE.
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
Message-Id: <20210519200339.829146-10-axelrasmussen@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2021-05-19 13:03:38 -07:00
|
|
|
if (!strcmp("MISSING", optarg))
|
|
|
|
p.uffd_mode = UFFDIO_REGISTER_MODE_MISSING;
|
|
|
|
else if (!strcmp("MINOR", optarg))
|
|
|
|
p.uffd_mode = UFFDIO_REGISTER_MODE_MINOR;
|
|
|
|
TEST_ASSERT(p.uffd_mode, "UFFD mode must be 'MISSING' or 'MINOR'.");
|
2020-02-20 18:09:12 +01:00
|
|
|
break;
|
2024-02-15 23:54:02 +00:00
|
|
|
case 'a':
|
|
|
|
p.single_uffd = true;
|
|
|
|
break;
|
2020-02-20 18:09:59 +01:00
|
|
|
case 'd':
|
2020-12-18 15:17:32 +01:00
|
|
|
p.uffd_delay = strtoul(optarg, NULL, 0);
|
|
|
|
TEST_ASSERT(p.uffd_delay >= 0, "A negative UFFD delay is not supported.");
|
2020-02-20 18:09:59 +01:00
|
|
|
break;
|
2020-01-23 10:04:30 -08:00
|
|
|
case 'b':
|
2020-11-04 22:23:52 +01:00
|
|
|
guest_percpu_mem_size = parse_size(optarg);
|
2020-01-23 10:04:33 -08:00
|
|
|
break;
|
2021-09-17 17:36:55 +00:00
|
|
|
case 's':
|
2021-05-19 13:03:34 -07:00
|
|
|
p.src_type = parse_backing_src_type(optarg);
|
|
|
|
break;
|
2020-01-23 10:04:33 -08:00
|
|
|
case 'v':
|
2022-11-03 12:17:18 -07:00
|
|
|
nr_vcpus = atoi_positive("Number of vCPUs", optarg);
|
|
|
|
TEST_ASSERT(nr_vcpus <= max_vcpus,
|
2020-11-04 22:23:53 +01:00
|
|
|
"Invalid number of vcpus, must be between 1 and %d", max_vcpus);
|
2020-01-23 10:04:30 -08:00
|
|
|
break;
|
2023-06-06 17:12:26 -07:00
|
|
|
case 'c':
|
|
|
|
cpulist = optarg;
|
|
|
|
break;
|
2021-01-12 13:42:52 -08:00
|
|
|
case 'o':
|
|
|
|
p.partition_vcpu_memory_access = false;
|
2024-02-15 23:54:02 +00:00
|
|
|
p.single_uffd = true;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
p.readers_per_uffd = atoi(optarg);
|
|
|
|
TEST_ASSERT(p.readers_per_uffd >= 1,
|
|
|
|
"Invalid number of readers per uffd %d: must be >=1",
|
|
|
|
p.readers_per_uffd);
|
2021-01-12 13:42:52 -08:00
|
|
|
break;
|
2020-01-23 10:04:27 -08:00
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
help(argv[0]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 13:03:39 -07:00
|
|
|
if (p.uffd_mode == UFFDIO_REGISTER_MODE_MINOR &&
|
|
|
|
!backing_src_is_shared(p.src_type)) {
|
2021-09-17 17:36:55 +00:00
|
|
|
TEST_FAIL("userfaultfd MINOR mode requires shared memory; pick a different -s");
|
2021-05-19 13:03:39 -07:00
|
|
|
}
|
KVM: selftests: allow using UFFD minor faults for demand paging
UFFD handling of MINOR faults is a new feature whose use case is to
speed up demand paging (compared to MISSING faults). So, it's
interesting to let this selftest exercise this new mode.
Modify the demand paging test to have the option of using UFFD minor
faults, as opposed to missing faults. Now, when turning on userfaultfd
with '-u', the desired mode has to be specified ("MISSING" or "MINOR").
If we're in minor mode, before registering, prefault via the *alias*.
This way, the guest will trigger minor faults, instead of missing
faults, and we can UFFDIO_CONTINUE to resolve them.
Modify the page fault handler function to use the right ioctl depending
on the mode we're running in. In MINOR mode, use UFFDIO_CONTINUE.
Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
Message-Id: <20210519200339.829146-10-axelrasmussen@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2021-05-19 13:03:38 -07:00
|
|
|
|
2023-06-06 17:12:26 -07:00
|
|
|
if (cpulist) {
|
|
|
|
kvm_parse_vcpu_pinning(cpulist, memstress_args.vcpu_to_pcpu,
|
|
|
|
nr_vcpus);
|
|
|
|
memstress_args.pin_vcpus = true;
|
|
|
|
}
|
|
|
|
|
2020-12-18 15:17:32 +01:00
|
|
|
for_each_guest_mode(run_test, &p);
|
2020-01-23 10:04:27 -08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-02-20 18:09:12 +01:00
|
|
|
|
|
|
|
#else /* __NR_userfaultfd */
|
|
|
|
|
|
|
|
#warning "missing __NR_userfaultfd definition"
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2020-03-10 10:15:56 +01:00
|
|
|
print_skip("__NR_userfaultfd must be present for userfaultfd test");
|
|
|
|
return KSFT_SKIP;
|
2020-02-20 18:09:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* __NR_userfaultfd */
|