xref: /linux/tools/testing/selftests/mm/vm_util.h (revision e9ef810dfee7a2227da9d423aecb0ced35faddbe)
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 /*
22  * Ignore the checkpatch warning, we must read from x but don't want to do
23  * anything with it in order to trigger a read page fault. We therefore must use
24  * volatile to stop the compiler from optimising this away.
25  */
26 #define FORCE_READ(x) (*(volatile typeof(x) *)x)
27 
28 extern unsigned int __page_size;
29 extern unsigned int __page_shift;
30 
31 /*
32  * Represents an open fd and PROCMAP_QUERY state for binary (via ioctl)
33  * /proc/$pid/[s]maps lookup.
34  */
35 struct procmap_fd {
36 	int fd;
37 	struct procmap_query query;
38 };
39 
psize(void)40 static inline unsigned int psize(void)
41 {
42 	if (!__page_size)
43 		__page_size = sysconf(_SC_PAGESIZE);
44 	return __page_size;
45 }
46 
pshift(void)47 static inline unsigned int pshift(void)
48 {
49 	if (!__page_shift)
50 		__page_shift = (ffsl(psize()) - 1);
51 	return __page_shift;
52 }
53 
54 bool detect_huge_zeropage(void);
55 
56 /*
57  * Plan 9 FS has bugs (at least on QEMU) where certain operations fail with
58  * ENOENT on unlinked files. See
59  * https://gitlab.com/qemu-project/qemu/-/issues/103 for some info about such
60  * bugs. There are rumours of NFS implementations with similar bugs.
61  *
62  * Ideally, tests should just detect filesystems known to have such issues and
63  * bail early. But 9pfs has the additional "feature" that it causes fstatfs to
64  * pass through the f_type field from the host filesystem. To avoid having to
65  * scrape /proc/mounts or some other hackery, tests can call this function when
66  * it seems such a bug might have been encountered.
67  */
skip_test_dodgy_fs(const char * op_name)68 static inline void skip_test_dodgy_fs(const char *op_name)
69 {
70 	ksft_test_result_skip("%s failed with ENOENT. Filesystem might be buggy (9pfs?)\n", op_name);
71 }
72 
73 uint64_t pagemap_get_entry(int fd, char *start);
74 bool pagemap_is_softdirty(int fd, char *start);
75 bool pagemap_is_swapped(int fd, char *start);
76 bool pagemap_is_populated(int fd, char *start);
77 unsigned long pagemap_get_pfn(int fd, char *start);
78 void clear_softdirty(void);
79 bool check_for_pattern(FILE *fp, const char *pattern, char *buf, size_t len);
80 uint64_t read_pmd_pagesize(void);
81 unsigned long rss_anon(void);
82 bool check_huge_anon(void *addr, int nr_hpages, uint64_t hpage_size);
83 bool check_huge_file(void *addr, int nr_hpages, uint64_t hpage_size);
84 bool check_huge_shmem(void *addr, int nr_hpages, uint64_t hpage_size);
85 int64_t allocate_transhuge(void *ptr, int pagemap_fd);
86 unsigned long default_huge_page_size(void);
87 int detect_hugetlb_page_sizes(size_t sizes[], int max);
88 
89 int uffd_register(int uffd, void *addr, uint64_t len,
90 		  bool miss, bool wp, bool minor);
91 int uffd_unregister(int uffd, void *addr, uint64_t len);
92 int uffd_register_with_ioctls(int uffd, void *addr, uint64_t len,
93 			      bool miss, bool wp, bool minor, uint64_t *ioctls);
94 unsigned long get_free_hugepages(void);
95 bool check_vmflag_io(void *addr);
96 int open_procmap(pid_t pid, struct procmap_fd *procmap_out);
97 int query_procmap(struct procmap_fd *procmap);
98 bool find_vma_procmap(struct procmap_fd *procmap, void *address);
99 int close_procmap(struct procmap_fd *procmap);
100 int write_sysfs(const char *file_path, unsigned long val);
101 int read_sysfs(const char *file_path, unsigned long *val);
102 
open_self_procmap(struct procmap_fd * procmap_out)103 static inline int open_self_procmap(struct procmap_fd *procmap_out)
104 {
105 	pid_t pid = getpid();
106 
107 	return open_procmap(pid, procmap_out);
108 }
109 
110 /* These helpers need to be inline to match the kselftest.h idiom. */
111 static char test_name[1024];
112 
log_test_start(const char * name,...)113 static inline void log_test_start(const char *name, ...)
114 {
115 	va_list args;
116 	va_start(args, name);
117 
118 	vsnprintf(test_name, sizeof(test_name), name, args);
119 	ksft_print_msg("[RUN] %s\n", test_name);
120 
121 	va_end(args);
122 }
123 
log_test_result(int result)124 static inline void log_test_result(int result)
125 {
126 	ksft_test_result_report(result, "%s\n", test_name);
127 }
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