1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *
4  * A test for the patch "Allow compaction of unevictable pages".
5  * With this patch we should be able to allocate at least 1/4
6  * of RAM in huge pages. Without the patch much less is
7  * allocated.
8  */
9 
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <sys/mman.h>
13 #include <sys/resource.h>
14 #include <fcntl.h>
15 #include <errno.h>
16 #include <unistd.h>
17 #include <string.h>
18 
19 #include "../kselftest.h"
20 
21 #define MAP_SIZE_MB	100
22 #define MAP_SIZE	(MAP_SIZE_MB * 1024 * 1024)
23 
24 struct map_list {
25 	void *map;
26 	struct map_list *next;
27 };
28 
29 int read_memory_info(unsigned long *memfree, unsigned long *hugepagesize)
30 {
31 	char  buffer[256] = {0};
32 	char *cmd = "cat /proc/meminfo | grep -i memfree | grep -o '[0-9]*'";
33 	FILE *cmdfile = popen(cmd, "r");
34 
35 	if (!(fgets(buffer, sizeof(buffer), cmdfile))) {
36 		perror("Failed to read meminfo\n");
37 		return -1;
38 	}
39 
40 	pclose(cmdfile);
41 
42 	*memfree = atoll(buffer);
43 	cmd = "cat /proc/meminfo | grep -i hugepagesize | grep -o '[0-9]*'";
44 	cmdfile = popen(cmd, "r");
45 
46 	if (!(fgets(buffer, sizeof(buffer), cmdfile))) {
47 		perror("Failed to read meminfo\n");
48 		return -1;
49 	}
50 
51 	pclose(cmdfile);
52 	*hugepagesize = atoll(buffer);
53 
54 	return 0;
55 }
56 
57 int prereq(void)
58 {
59 	char allowed;
60 	int fd;
61 
62 	fd = open("/proc/sys/vm/compact_unevictable_allowed",
63 		  O_RDONLY | O_NONBLOCK);
64 	if (fd < 0) {
65 		perror("Failed to open\n"
66 		       "/proc/sys/vm/compact_unevictable_allowed\n");
67 		return -1;
68 	}
69 
70 	if (read(fd, &allowed, sizeof(char)) != sizeof(char)) {
71 		perror("Failed to read from\n"
72 		       "/proc/sys/vm/compact_unevictable_allowed\n");
73 		close(fd);
74 		return -1;
75 	}
76 
77 	close(fd);
78 	if (allowed == '1')
79 		return 0;
80 
81 	return -1;
82 }
83 
84 int check_compaction(unsigned long mem_free, unsigned int hugepage_size)
85 {
86 	int fd;
87 	int compaction_index = 0;
88 	char initial_nr_hugepages[10] = {0};
89 	char nr_hugepages[10] = {0};
90 
91 	/* We want to test with 80% of available memory. Else, OOM killer comes
92 	   in to play */
93 	mem_free = mem_free * 0.8;
94 
95 	fd = open("/proc/sys/vm/nr_hugepages", O_RDWR | O_NONBLOCK);
96 	if (fd < 0) {
97 		perror("Failed to open /proc/sys/vm/nr_hugepages");
98 		return -1;
99 	}
100 
101 	if (read(fd, initial_nr_hugepages, sizeof(initial_nr_hugepages)) <= 0) {
102 		perror("Failed to read from /proc/sys/vm/nr_hugepages");
103 		goto close_fd;
104 	}
105 
106 	lseek(fd, 0, SEEK_SET);
107 
108 	/* Start with the initial condition of 0 huge pages*/
109 	if (write(fd, "0", sizeof(char)) != sizeof(char)) {
110 		perror("Failed to write 0 to /proc/sys/vm/nr_hugepages\n");
111 		goto close_fd;
112 	}
113 
114 	lseek(fd, 0, SEEK_SET);
115 
116 	/* Request a large number of huge pages. The Kernel will allocate
117 	   as much as it can */
118 	if (write(fd, "100000", (6*sizeof(char))) != (6*sizeof(char))) {
119 		perror("Failed to write 100000 to /proc/sys/vm/nr_hugepages\n");
120 		goto close_fd;
121 	}
122 
123 	lseek(fd, 0, SEEK_SET);
124 
125 	if (read(fd, nr_hugepages, sizeof(nr_hugepages)) <= 0) {
126 		perror("Failed to re-read from /proc/sys/vm/nr_hugepages\n");
127 		goto close_fd;
128 	}
129 
130 	/* We should have been able to request at least 1/3 rd of the memory in
131 	   huge pages */
132 	compaction_index = mem_free/(atoi(nr_hugepages) * hugepage_size);
133 
134 	if (compaction_index > 3) {
135 		printf("No of huge pages allocated = %d\n",
136 		       (atoi(nr_hugepages)));
137 		fprintf(stderr, "ERROR: Less that 1/%d of memory is available\n"
138 			"as huge pages\n", compaction_index);
139 		goto close_fd;
140 	}
141 
142 	printf("No of huge pages allocated = %d\n",
143 	       (atoi(nr_hugepages)));
144 
145 	lseek(fd, 0, SEEK_SET);
146 
147 	if (write(fd, initial_nr_hugepages, strlen(initial_nr_hugepages))
148 	    != strlen(initial_nr_hugepages)) {
149 		perror("Failed to write value to /proc/sys/vm/nr_hugepages\n");
150 		goto close_fd;
151 	}
152 
153 	close(fd);
154 	return 0;
155 
156  close_fd:
157 	close(fd);
158 	printf("Not OK. Compaction test failed.");
159 	return -1;
160 }
161 
162 
163 int main(int argc, char **argv)
164 {
165 	struct rlimit lim;
166 	struct map_list *list, *entry;
167 	size_t page_size, i;
168 	void *map = NULL;
169 	unsigned long mem_free = 0;
170 	unsigned long hugepage_size = 0;
171 	long mem_fragmentable_MB = 0;
172 
173 	if (prereq() != 0) {
174 		printf("Either the sysctl compact_unevictable_allowed is not\n"
175 		       "set to 1 or couldn't read the proc file.\n"
176 		       "Skipping the test\n");
177 		return KSFT_SKIP;
178 	}
179 
180 	lim.rlim_cur = RLIM_INFINITY;
181 	lim.rlim_max = RLIM_INFINITY;
182 	if (setrlimit(RLIMIT_MEMLOCK, &lim)) {
183 		perror("Failed to set rlimit:\n");
184 		return -1;
185 	}
186 
187 	page_size = getpagesize();
188 
189 	list = NULL;
190 
191 	if (read_memory_info(&mem_free, &hugepage_size) != 0) {
192 		printf("ERROR: Cannot read meminfo\n");
193 		return -1;
194 	}
195 
196 	mem_fragmentable_MB = mem_free * 0.8 / 1024;
197 
198 	while (mem_fragmentable_MB > 0) {
199 		map = mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE,
200 			   MAP_ANONYMOUS | MAP_PRIVATE | MAP_LOCKED, -1, 0);
201 		if (map == MAP_FAILED)
202 			break;
203 
204 		entry = malloc(sizeof(struct map_list));
205 		if (!entry) {
206 			munmap(map, MAP_SIZE);
207 			break;
208 		}
209 		entry->map = map;
210 		entry->next = list;
211 		list = entry;
212 
213 		/* Write something (in this case the address of the map) to
214 		 * ensure that KSM can't merge the mapped pages
215 		 */
216 		for (i = 0; i < MAP_SIZE; i += page_size)
217 			*(unsigned long *)(map + i) = (unsigned long)map + i;
218 
219 		mem_fragmentable_MB -= MAP_SIZE_MB;
220 	}
221 
222 	for (entry = list; entry != NULL; entry = entry->next) {
223 		munmap(entry->map, MAP_SIZE);
224 		if (!entry->next)
225 			break;
226 		entry = entry->next;
227 	}
228 
229 	if (check_compaction(mem_free, hugepage_size) == 0)
230 		return 0;
231 
232 	return -1;
233 }
234