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