1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #include <stdint.h> 3 #include <stdbool.h> 4 #include <sys/mman.h> 5 #include <err.h> 6 #include <stdarg.h> 7 #include <strings.h> /* ffsl() */ 8 #include <unistd.h> /* _SC_PAGESIZE */ 9 #include "kselftest.h" 10 #include <linux/fs.h> 11 12 #define BIT_ULL(nr) (1ULL << (nr)) 13 #define PM_SOFT_DIRTY BIT_ULL(55) 14 #define PM_MMAP_EXCLUSIVE BIT_ULL(56) 15 #define PM_UFFD_WP BIT_ULL(57) 16 #define PM_GUARD_REGION BIT_ULL(58) 17 #define PM_FILE BIT_ULL(61) 18 #define PM_SWAP BIT_ULL(62) 19 #define PM_PRESENT BIT_ULL(63) 20 21 #define KPF_COMPOUND_HEAD BIT_ULL(15) 22 #define KPF_COMPOUND_TAIL BIT_ULL(16) 23 #define KPF_HWPOISON BIT_ULL(19) 24 #define KPF_THP BIT_ULL(22) 25 /* 26 * Ignore the checkpatch warning, we must read from x but don't want to do 27 * anything with it in order to trigger a read page fault. We therefore must use 28 * volatile to stop the compiler from optimising this away. 29 */ 30 #define FORCE_READ(x) (*(const volatile typeof(x) *)&(x)) 31 32 extern unsigned int __page_size; 33 extern unsigned int __page_shift; 34 35 /* 36 * Represents an open fd and PROCMAP_QUERY state for binary (via ioctl) 37 * /proc/$pid/[s]maps lookup. 38 */ 39 struct procmap_fd { 40 int fd; 41 struct procmap_query query; 42 }; 43 44 static inline unsigned int psize(void) 45 { 46 if (!__page_size) 47 __page_size = sysconf(_SC_PAGESIZE); 48 return __page_size; 49 } 50 51 static inline unsigned int pshift(void) 52 { 53 if (!__page_shift) 54 __page_shift = (ffsl(psize()) - 1); 55 return __page_shift; 56 } 57 58 static inline void force_read_pages(char *addr, unsigned int nr_pages, 59 size_t pagesize) 60 { 61 for (unsigned int i = 0; i < nr_pages; i++) 62 FORCE_READ(addr[i * pagesize]); 63 } 64 65 bool detect_huge_zeropage(void); 66 67 /* 68 * Plan 9 FS has bugs (at least on QEMU) where certain operations fail with 69 * ENOENT on unlinked files. See 70 * https://gitlab.com/qemu-project/qemu/-/issues/103 for some info about such 71 * bugs. There are rumours of NFS implementations with similar bugs. 72 * 73 * Ideally, tests should just detect filesystems known to have such issues and 74 * bail early. But 9pfs has the additional "feature" that it causes fstatfs to 75 * pass through the f_type field from the host filesystem. To avoid having to 76 * scrape /proc/mounts or some other hackery, tests can call this function when 77 * it seems such a bug might have been encountered. 78 */ 79 static inline void skip_test_dodgy_fs(const char *op_name) 80 { 81 ksft_test_result_skip("%s failed with ENOENT. Filesystem might be buggy (9pfs?)\n", op_name); 82 } 83 84 uint64_t pagemap_get_entry(int fd, char *start); 85 bool pagemap_is_softdirty(int fd, char *start); 86 bool pagemap_is_swapped(int fd, char *start); 87 bool pagemap_is_populated(int fd, char *start); 88 unsigned long pagemap_get_pfn(int fd, char *start); 89 void clear_softdirty(void); 90 bool check_for_pattern(FILE *fp, const char *pattern, char *buf, size_t len); 91 uint64_t read_pmd_pagesize(void); 92 unsigned long rss_anon(void); 93 bool check_huge_anon(void *addr, int nr_hpages, uint64_t hpage_size); 94 bool check_huge_file(void *addr, int nr_hpages, uint64_t hpage_size); 95 bool check_huge_shmem(void *addr, int nr_hpages, uint64_t hpage_size); 96 int64_t allocate_transhuge(void *ptr, int pagemap_fd); 97 int pageflags_get(unsigned long pfn, int kpageflags_fd, uint64_t *flags); 98 99 int uffd_register(int uffd, void *addr, uint64_t len, 100 bool miss, bool wp, bool minor); 101 int uffd_unregister(int uffd, void *addr, uint64_t len); 102 int uffd_register_with_ioctls(int uffd, void *addr, uint64_t len, 103 bool miss, bool wp, bool minor, uint64_t *ioctls); 104 bool check_vmflag_io(void *addr); 105 bool check_vmflag_pfnmap(void *addr); 106 bool check_vmflag_guard(void *addr); 107 int open_procmap(pid_t pid, struct procmap_fd *procmap_out); 108 int query_procmap(struct procmap_fd *procmap); 109 bool find_vma_procmap(struct procmap_fd *procmap, void *address); 110 int close_procmap(struct procmap_fd *procmap); 111 int write_sysfs(const char *file_path, unsigned long val); 112 int read_sysfs(const char *file_path, unsigned long *val); 113 bool softdirty_supported(void); 114 115 static inline int open_self_procmap(struct procmap_fd *procmap_out) 116 { 117 pid_t pid = getpid(); 118 119 return open_procmap(pid, procmap_out); 120 } 121 122 /* These helpers need to be inline to match the kselftest.h idiom. */ 123 static char test_name[1024]; 124 125 static inline void log_test_start(const char *name, ...) 126 { 127 va_list args; 128 va_start(args, name); 129 130 vsnprintf(test_name, sizeof(test_name), name, args); 131 ksft_print_msg("[RUN] %s\n", test_name); 132 133 va_end(args); 134 } 135 136 static inline void log_test_result(int result) 137 { 138 ksft_test_result_report(result, "%s\n", test_name); 139 } 140 141 static inline int sz2ord(size_t size, size_t pagesize) 142 { 143 return __builtin_ctzll(size / pagesize); 144 } 145 146 void *sys_mremap(void *old_address, unsigned long old_size, 147 unsigned long new_size, int flags, void *new_address); 148 149 long ksm_get_self_zero_pages(void); 150 long ksm_get_self_merging_pages(void); 151 long ksm_get_full_scans(void); 152 int ksm_use_zero_pages(void); 153 int ksm_start(void); 154 int ksm_stop(void); 155 int get_hardware_corrupted_size(unsigned long *val); 156 int unpoison_memory(unsigned long pfn); 157 158 /* 159 * On ppc64 this will only work with radix 2M hugepage size 160 */ 161 #define HPAGE_SHIFT 21 162 #define HPAGE_SIZE (1 << HPAGE_SHIFT) 163 164 #define PAGEMAP_PRESENT(ent) (((ent) & (1ull << 63)) != 0) 165 #define PAGEMAP_PFN(ent) ((ent) & ((1ull << 55) - 1)) 166 167 void write_file(const char *path, const char *buf, size_t buflen); 168 int read_file(const char *path, char *buf, size_t buflen); 169 unsigned long read_num(const char *path); 170 void write_num(const char *path, unsigned long num); 171 172 void shm_limits_prepare(unsigned long length); 173 void __shm_limits_restore(void); 174 175 #define SHM_LIMITS_RESTORE() \ 176 static void __attribute__((destructor)) shm_limits_restore(void) \ 177 { \ 178 __shm_limits_restore(); \ 179 } 180