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 <strings.h> /* ffsl() */ 7 #include <unistd.h> /* _SC_PAGESIZE */ 8 #include "../kselftest.h" 9 10 #define BIT_ULL(nr) (1ULL << (nr)) 11 #define PM_SOFT_DIRTY BIT_ULL(55) 12 #define PM_MMAP_EXCLUSIVE BIT_ULL(56) 13 #define PM_UFFD_WP BIT_ULL(57) 14 #define PM_GUARD_REGION BIT_ULL(58) 15 #define PM_FILE BIT_ULL(61) 16 #define PM_SWAP BIT_ULL(62) 17 #define PM_PRESENT BIT_ULL(63) 18 19 extern unsigned int __page_size; 20 extern unsigned int __page_shift; 21 22 static inline unsigned int psize(void) 23 { 24 if (!__page_size) 25 __page_size = sysconf(_SC_PAGESIZE); 26 return __page_size; 27 } 28 29 static inline unsigned int pshift(void) 30 { 31 if (!__page_shift) 32 __page_shift = (ffsl(psize()) - 1); 33 return __page_shift; 34 } 35 36 /* 37 * Plan 9 FS has bugs (at least on QEMU) where certain operations fail with 38 * ENOENT on unlinked files. See 39 * https://gitlab.com/qemu-project/qemu/-/issues/103 for some info about such 40 * bugs. There are rumours of NFS implementations with similar bugs. 41 * 42 * Ideally, tests should just detect filesystems known to have such issues and 43 * bail early. But 9pfs has the additional "feature" that it causes fstatfs to 44 * pass through the f_type field from the host filesystem. To avoid having to 45 * scrape /proc/mounts or some other hackery, tests can call this function when 46 * it seems such a bug might have been encountered. 47 */ 48 static inline void skip_test_dodgy_fs(const char *op_name) 49 { 50 ksft_test_result_skip("%s failed with ENOENT. Filesystem might be buggy (9pfs?)\n", op_name); 51 } 52 53 uint64_t pagemap_get_entry(int fd, char *start); 54 bool pagemap_is_softdirty(int fd, char *start); 55 bool pagemap_is_swapped(int fd, char *start); 56 bool pagemap_is_populated(int fd, char *start); 57 unsigned long pagemap_get_pfn(int fd, char *start); 58 void clear_softdirty(void); 59 bool check_for_pattern(FILE *fp, const char *pattern, char *buf, size_t len); 60 uint64_t read_pmd_pagesize(void); 61 unsigned long rss_anon(void); 62 bool check_huge_anon(void *addr, int nr_hpages, uint64_t hpage_size); 63 bool check_huge_file(void *addr, int nr_hpages, uint64_t hpage_size); 64 bool check_huge_shmem(void *addr, int nr_hpages, uint64_t hpage_size); 65 int64_t allocate_transhuge(void *ptr, int pagemap_fd); 66 unsigned long default_huge_page_size(void); 67 int detect_hugetlb_page_sizes(size_t sizes[], int max); 68 69 int uffd_register(int uffd, void *addr, uint64_t len, 70 bool miss, bool wp, bool minor); 71 int uffd_unregister(int uffd, void *addr, uint64_t len); 72 int uffd_register_with_ioctls(int uffd, void *addr, uint64_t len, 73 bool miss, bool wp, bool minor, uint64_t *ioctls); 74 unsigned long get_free_hugepages(void); 75 bool check_vmflag_io(void *addr); 76 77 /* 78 * On ppc64 this will only work with radix 2M hugepage size 79 */ 80 #define HPAGE_SHIFT 21 81 #define HPAGE_SIZE (1 << HPAGE_SHIFT) 82 83 #define PAGEMAP_PRESENT(ent) (((ent) & (1ull << 63)) != 0) 84 #define PAGEMAP_PFN(ent) ((ent) & ((1ull << 55) - 1)) 85