1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Userfaultfd tests common header 4 * 5 * Copyright (C) 2015-2023 Red Hat, Inc. 6 */ 7 #ifndef __UFFD_COMMON_H__ 8 #define __UFFD_COMMON_H__ 9 10 #define _GNU_SOURCE 11 #include <stdio.h> 12 #include <errno.h> 13 #include <unistd.h> 14 #include <stdlib.h> 15 #include <sys/types.h> 16 #include <sys/stat.h> 17 #include <fcntl.h> 18 #include <time.h> 19 #include <signal.h> 20 #include <poll.h> 21 #include <string.h> 22 #include <linux/mman.h> 23 #include <sys/mman.h> 24 #include <sys/syscall.h> 25 #include <sys/ioctl.h> 26 #include <sys/wait.h> 27 #include <pthread.h> 28 #include <linux/userfaultfd.h> 29 #include <setjmp.h> 30 #include <stdbool.h> 31 #include <assert.h> 32 #include <inttypes.h> 33 #include <stdint.h> 34 #include <sys/random.h> 35 36 #include "../kselftest.h" 37 #include "vm_util.h" 38 39 #define UFFD_FLAGS (O_CLOEXEC | O_NONBLOCK | UFFD_USER_MODE_ONLY) 40 41 #define _err(fmt, ...) \ 42 do { \ 43 int ret = errno; \ 44 fprintf(stderr, "ERROR: " fmt, ##__VA_ARGS__); \ 45 fprintf(stderr, " (errno=%d, @%s:%d)\n", \ 46 ret, __FILE__, __LINE__); \ 47 } while (0) 48 49 #define errexit(exitcode, fmt, ...) \ 50 do { \ 51 _err(fmt, ##__VA_ARGS__); \ 52 exit(exitcode); \ 53 } while (0) 54 55 #define err(fmt, ...) errexit(1, fmt, ##__VA_ARGS__) 56 57 /* pthread_mutex_t starts at page offset 0 */ 58 #define area_mutex(___area, ___nr) \ 59 ((pthread_mutex_t *) ((___area) + (___nr)*page_size)) 60 /* 61 * count is placed in the page after pthread_mutex_t naturally aligned 62 * to avoid non alignment faults on non-x86 archs. 63 */ 64 #define area_count(___area, ___nr) \ 65 ((volatile unsigned long long *) ((unsigned long) \ 66 ((___area) + (___nr)*page_size + \ 67 sizeof(pthread_mutex_t) + \ 68 sizeof(unsigned long long) - 1) & \ 69 ~(unsigned long)(sizeof(unsigned long long) \ 70 - 1))) 71 72 /* Userfaultfd test statistics */ 73 struct uffd_args { 74 int cpu; 75 /* Whether apply wr-protects when installing pages */ 76 bool apply_wp; 77 unsigned long missing_faults; 78 unsigned long wp_faults; 79 unsigned long minor_faults; 80 81 /* A custom fault handler; defaults to uffd_handle_page_fault. */ 82 void (*handle_fault)(struct uffd_msg *msg, struct uffd_args *args); 83 }; 84 85 struct uffd_test_ops { 86 int (*allocate_area)(void **alloc_area, bool is_src); 87 void (*release_pages)(char *rel_area); 88 void (*alias_mapping)(__u64 *start, size_t len, unsigned long offset); 89 void (*check_pmd_mapping)(void *p, int expect_nr_hpages); 90 }; 91 typedef struct uffd_test_ops uffd_test_ops_t; 92 93 struct uffd_test_case_ops { 94 int (*pre_alloc)(const char **errmsg); 95 int (*post_alloc)(const char **errmsg); 96 }; 97 typedef struct uffd_test_case_ops uffd_test_case_ops_t; 98 99 extern unsigned long nr_cpus, nr_pages, nr_pages_per_cpu, page_size; 100 extern char *area_src, *area_src_alias, *area_dst, *area_dst_alias, *area_remap; 101 extern int uffd, uffd_flags, finished, *pipefd, test_type; 102 extern bool map_shared; 103 extern bool test_uffdio_wp; 104 extern unsigned long long *count_verify; 105 extern volatile bool test_uffdio_copy_eexist; 106 107 extern uffd_test_ops_t anon_uffd_test_ops; 108 extern uffd_test_ops_t shmem_uffd_test_ops; 109 extern uffd_test_ops_t hugetlb_uffd_test_ops; 110 extern uffd_test_ops_t *uffd_test_ops; 111 extern uffd_test_case_ops_t *uffd_test_case_ops; 112 113 void uffd_stats_report(struct uffd_args *args, int n_cpus); 114 int uffd_test_ctx_init(uint64_t features, const char **errmsg); 115 void uffd_test_ctx_clear(void); 116 int userfaultfd_open(uint64_t *features); 117 int uffd_read_msg(int ufd, struct uffd_msg *msg); 118 void wp_range(int ufd, __u64 start, __u64 len, bool wp); 119 void uffd_handle_page_fault(struct uffd_msg *msg, struct uffd_args *args); 120 int __copy_page(int ufd, unsigned long offset, bool retry, bool wp); 121 int copy_page(int ufd, unsigned long offset, bool wp); 122 int move_page(int ufd, unsigned long offset, unsigned long len); 123 void *uffd_poll_thread(void *arg); 124 125 int uffd_open_dev(unsigned int flags); 126 int uffd_open_sys(unsigned int flags); 127 int uffd_open(unsigned int flags); 128 int uffd_get_features(uint64_t *features); 129 130 #define TEST_ANON 1 131 #define TEST_HUGETLB 2 132 #define TEST_SHMEM 3 133 134 #endif 135