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