diff options
| author | Ricardo Koller <ricarkol@google.com> | 2022-10-17 19:58:21 +0000 |
|---|---|---|
| committer | Marc Zyngier <maz@kernel.org> | 2022-11-10 19:10:27 +0000 |
| commit | a93871d0ea9fd59fb5eb783619334183d7f07f51 (patch) | |
| tree | 1d0a4d9bec147f734f25a1c090c5b2bd57925b42 /tools/testing/selftests/kvm/include/userfaultfd_util.h | |
| parent | 30a0b95b1335e12efef89dd78518ed3e4a71a763 (diff) | |
| download | linux-next-a93871d0ea9fd59fb5eb783619334183d7f07f51.tar.gz linux-next-a93871d0ea9fd59fb5eb783619334183d7f07f51.zip | |
KVM: selftests: Add a userfaultfd library
Move the generic userfaultfd code out of demand_paging_test.c into a
common library, userfaultfd_util. This library consists of a setup and a
stop function. The setup function starts a thread for handling page
faults using the handler callback function. This setup returns a
uffd_desc object which is then used in the stop function (to wait and
destroy the threads).
Reviewed-by: Oliver Upton <oliver.upton@linux.dev>
Reviewed-by: Ben Gardon <bgardon@google.com>
Signed-off-by: Ricardo Koller <ricarkol@google.com>
Signed-off-by: Marc Zyngier <maz@kernel.org>
Link: https://lore.kernel.org/r/20221017195834.2295901-2-ricarkol@google.com
Diffstat (limited to 'tools/testing/selftests/kvm/include/userfaultfd_util.h')
| -rw-r--r-- | tools/testing/selftests/kvm/include/userfaultfd_util.h | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/tools/testing/selftests/kvm/include/userfaultfd_util.h b/tools/testing/selftests/kvm/include/userfaultfd_util.h new file mode 100644 index 000000000000..877449c34592 --- /dev/null +++ b/tools/testing/selftests/kvm/include/userfaultfd_util.h @@ -0,0 +1,45 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * KVM userfaultfd util + * + * Copyright (C) 2018, Red Hat, Inc. + * Copyright (C) 2019-2022 Google LLC + */ + +#define _GNU_SOURCE /* for pipe2 */ + +#include <inttypes.h> +#include <time.h> +#include <pthread.h> +#include <linux/userfaultfd.h> + +#include "test_util.h" + +typedef int (*uffd_handler_t)(int uffd_mode, int uffd, struct uffd_msg *msg); + +struct uffd_desc { + int uffd_mode; + int uffd; + int pipefds[2]; + useconds_t delay; + uffd_handler_t handler; + pthread_t thread; +}; + +struct uffd_desc *uffd_setup_demand_paging(int uffd_mode, useconds_t delay, + void *hva, uint64_t len, + uffd_handler_t handler); + +void uffd_stop_demand_paging(struct uffd_desc *uffd); + +#ifdef PRINT_PER_PAGE_UPDATES +#define PER_PAGE_DEBUG(...) printf(__VA_ARGS__) +#else +#define PER_PAGE_DEBUG(...) _no_printf(__VA_ARGS__) +#endif + +#ifdef PRINT_PER_VCPU_UPDATES +#define PER_VCPU_DEBUG(...) printf(__VA_ARGS__) +#else +#define PER_VCPU_DEBUG(...) _no_printf(__VA_ARGS__) +#endif |
