xref: /linux/tools/testing/selftests/mm/vm_util.h (revision ab93e0dd72c37d378dd936f031ffb83ff2bd87ce)
1642bc52aSMuhammad Usama Anjum /* SPDX-License-Identifier: GPL-2.0 */
2642bc52aSMuhammad Usama Anjum #include <stdint.h>
3642bc52aSMuhammad Usama Anjum #include <stdbool.h>
4af605d26SPeter Xu #include <sys/mman.h>
5af605d26SPeter Xu #include <err.h>
63f192afbSMark Brown #include <stdarg.h>
7176517c9SEdward Liaw #include <strings.h> /* ffsl() */
8af605d26SPeter Xu #include <unistd.h> /* _SC_PAGESIZE */
9d8a866c7SBrendan Jackman #include "../kselftest.h"
10bd23f293SLorenzo Stoakes #include <linux/fs.h>
11af605d26SPeter Xu 
129f74696bSPeter Xu #define BIT_ULL(nr)                   (1ULL << (nr))
139f74696bSPeter Xu #define PM_SOFT_DIRTY                 BIT_ULL(55)
149f74696bSPeter Xu #define PM_MMAP_EXCLUSIVE             BIT_ULL(56)
159f74696bSPeter Xu #define PM_UFFD_WP                    BIT_ULL(57)
16f3b92176SLorenzo Stoakes #define PM_GUARD_REGION               BIT_ULL(58)
179f74696bSPeter Xu #define PM_FILE                       BIT_ULL(61)
189f74696bSPeter Xu #define PM_SWAP                       BIT_ULL(62)
199f74696bSPeter Xu #define PM_PRESENT                    BIT_ULL(63)
209f74696bSPeter Xu 
21af605d26SPeter Xu /*
22af605d26SPeter Xu  * Ignore the checkpatch warning, we must read from x but don't want to do
23af605d26SPeter Xu  * anything with it in order to trigger a read page fault. We therefore must use
24bd23f293SLorenzo Stoakes  * volatile to stop the compiler from optimising this away.
25bd23f293SLorenzo Stoakes  */
26bd23f293SLorenzo Stoakes #define FORCE_READ(x) (*(volatile typeof(x) *)x)
27bd23f293SLorenzo Stoakes 
28bd23f293SLorenzo Stoakes extern unsigned int __page_size;
29bd23f293SLorenzo Stoakes extern unsigned int __page_shift;
30bd23f293SLorenzo Stoakes 
31bd23f293SLorenzo Stoakes /*
32bd23f293SLorenzo Stoakes  * Represents an open fd and PROCMAP_QUERY state for binary (via ioctl)
33af605d26SPeter Xu  * /proc/$pid/[s]maps lookup.
34af605d26SPeter Xu  */
35af605d26SPeter Xu struct procmap_fd {
36af605d26SPeter Xu 	int fd;
37af605d26SPeter Xu 	struct procmap_query query;
38af605d26SPeter Xu };
39af605d26SPeter Xu 
psize(void)40af605d26SPeter Xu static inline unsigned int psize(void)
41af605d26SPeter Xu {
42af605d26SPeter Xu 	if (!__page_size)
43af605d26SPeter Xu 		__page_size = sysconf(_SC_PAGESIZE);
44af605d26SPeter Xu 	return __page_size;
45af605d26SPeter Xu }
46642bc52aSMuhammad Usama Anjum 
pshift(void)47d8a866c7SBrendan Jackman static inline unsigned int pshift(void)
48d8a866c7SBrendan Jackman {
49d8a866c7SBrendan Jackman 	if (!__page_shift)
50d8a866c7SBrendan Jackman 		__page_shift = (ffsl(psize()) - 1);
51d8a866c7SBrendan Jackman 	return __page_shift;
52d8a866c7SBrendan Jackman }
53d8a866c7SBrendan Jackman 
54d8a866c7SBrendan Jackman bool detect_huge_zeropage(void);
55d8a866c7SBrendan Jackman 
56d8a866c7SBrendan Jackman /*
57d8a866c7SBrendan Jackman  * Plan 9 FS has bugs (at least on QEMU) where certain operations fail with
58d8a866c7SBrendan Jackman  * ENOENT on unlinked files. See
59d8a866c7SBrendan Jackman  * https://gitlab.com/qemu-project/qemu/-/issues/103 for some info about such
60d8a866c7SBrendan Jackman  * bugs. There are rumours of NFS implementations with similar bugs.
61d8a866c7SBrendan Jackman  *
62d8a866c7SBrendan Jackman  * Ideally, tests should just detect filesystems known to have such issues and
63d8a866c7SBrendan Jackman  * bail early. But 9pfs has the additional "feature" that it causes fstatfs to
64642bc52aSMuhammad Usama Anjum  * pass through the f_type field from the host filesystem. To avoid having to
65642bc52aSMuhammad Usama Anjum  * scrape /proc/mounts or some other hackery, tests can call this function when
6669c66addSDavid Hildenbrand  * it seems such a bug might have been encountered.
67a905e82aSDavid Hildenbrand  */
skip_test_dodgy_fs(const char * op_name)6893fb70aaSDavid Hildenbrand static inline void skip_test_dodgy_fs(const char *op_name)
69642bc52aSMuhammad Usama Anjum {
70c07c343cSZach O'Keefe 	ksft_test_result_skip("%s failed with ENOENT. Filesystem might be buggy (9pfs?)\n", op_name);
71642bc52aSMuhammad Usama Anjum }
72391e8697SAlexander Zhu 
73c07c343cSZach O'Keefe uint64_t pagemap_get_entry(int fd, char *start);
741b03d0d5SZach O'Keefe bool pagemap_is_softdirty(int fd, char *start);
751b03d0d5SZach O'Keefe bool pagemap_is_swapped(int fd, char *start);
76af605d26SPeter Xu bool pagemap_is_populated(int fd, char *start);
77bd4d67e7SPeter Xu unsigned long pagemap_get_pfn(int fd, char *start);
7881b1e3f9SDavid Hildenbrand void clear_softdirty(void);
79af605d26SPeter Xu bool check_for_pattern(FILE *fp, const char *pattern, char *buf, size_t len);
80c4277cb6SPeter Xu uint64_t read_pmd_pagesize(void);
81c4277cb6SPeter Xu unsigned long rss_anon(void);
82c4277cb6SPeter Xu bool check_huge_anon(void *addr, int nr_hpages, uint64_t hpage_size);
83c3315502SPeter Xu bool check_huge_file(void *addr, int nr_hpages, uint64_t hpage_size);
84c3315502SPeter Xu bool check_huge_shmem(void *addr, int nr_hpages, uint64_t hpage_size);
85c8b90731SBreno Leitao int64_t allocate_transhuge(void *ptr, int pagemap_fd);
863bd61372SThomas Weißschuh unsigned long default_huge_page_size(void);
87bd23f293SLorenzo Stoakes int detect_hugetlb_page_sizes(size_t sizes[], int max);
88bd23f293SLorenzo Stoakes 
89bd23f293SLorenzo Stoakes int uffd_register(int uffd, void *addr, uint64_t len,
90bd23f293SLorenzo Stoakes 		  bool miss, bool wp, bool minor);
91*6fb62233SPu Lehui int uffd_unregister(int uffd, void *addr, uint64_t len);
92*6fb62233SPu Lehui int uffd_register_with_ioctls(int uffd, void *addr, uint64_t len,
93bd23f293SLorenzo Stoakes 			      bool miss, bool wp, bool minor, uint64_t *ioctls);
94bd23f293SLorenzo Stoakes unsigned long get_free_hugepages(void);
95bd23f293SLorenzo Stoakes bool check_vmflag_io(void *addr);
96bd23f293SLorenzo Stoakes int open_procmap(pid_t pid, struct procmap_fd *procmap_out);
97bd23f293SLorenzo Stoakes int query_procmap(struct procmap_fd *procmap);
98bd23f293SLorenzo Stoakes bool find_vma_procmap(struct procmap_fd *procmap, void *address);
99bd23f293SLorenzo Stoakes int close_procmap(struct procmap_fd *procmap);
100c4277cb6SPeter Xu int write_sysfs(const char *file_path, unsigned long val);
1013f192afbSMark Brown int read_sysfs(const char *file_path, unsigned long *val);
1023f192afbSMark Brown 
open_self_procmap(struct procmap_fd * procmap_out)1033f192afbSMark Brown static inline int open_self_procmap(struct procmap_fd *procmap_out)
1043f192afbSMark Brown {
1053f192afbSMark Brown 	pid_t pid = getpid();
1063f192afbSMark Brown 
1073f192afbSMark Brown 	return open_procmap(pid, procmap_out);
1083f192afbSMark Brown }
1093f192afbSMark Brown 
1103f192afbSMark Brown /* These helpers need to be inline to match the kselftest.h idiom. */
1113f192afbSMark Brown static char test_name[1024];
1123f192afbSMark Brown 
log_test_start(const char * name,...)1133f192afbSMark Brown static inline void log_test_start(const char *name, ...)
1143f192afbSMark Brown {
1153f192afbSMark Brown 	va_list args;
1163f192afbSMark Brown 	va_start(args, name);
1173f192afbSMark Brown 
1183f192afbSMark Brown 	vsnprintf(test_name, sizeof(test_name), name, args);
1193f192afbSMark Brown 	ksft_print_msg("[RUN] %s\n", test_name);
120af605d26SPeter Xu 
121af605d26SPeter Xu 	va_end(args);
122af605d26SPeter Xu }
123af605d26SPeter Xu 
log_test_result(int result)124af605d26SPeter Xu static inline void log_test_result(int result)
125af605d26SPeter Xu {
126af605d26SPeter Xu 	ksft_test_result_report(result, "%s\n", test_name);
127af605d26SPeter Xu }
128 
129 void *sys_mremap(void *old_address, unsigned long old_size,
130 		 unsigned long new_size, int flags, void *new_address);
131 
132 /*
133  * On ppc64 this will only work with radix 2M hugepage size
134  */
135 #define HPAGE_SHIFT 21
136 #define HPAGE_SIZE (1 << HPAGE_SHIFT)
137 
138 #define PAGEMAP_PRESENT(ent)	(((ent) & (1ull << 63)) != 0)
139 #define PAGEMAP_PFN(ent)	((ent) & ((1ull << 55) - 1))
140