1 // SPDX-License-Identifier: GPL-2.0
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
13 #include <sys/resource.h>
19 #include "../kselftest.h"
21 #define MAP_SIZE_MB 100
22 #define MAP_SIZE (MAP_SIZE_MB * 1024 * 1024)
26 struct map_list *next;
29 int read_memory_info(unsigned long *memfree, unsigned long *hugepagesize)
31 char buffer[256] = {0};
32 char *cmd = "cat /proc/meminfo | grep -i memfree | grep -o '[0-9]*'";
33 FILE *cmdfile = popen(cmd, "r");
35 if (!(fgets(buffer, sizeof(buffer), cmdfile))) {
36 perror("Failed to read meminfo\n");
42 *memfree = atoll(buffer);
43 cmd = "cat /proc/meminfo | grep -i hugepagesize | grep -o '[0-9]*'";
44 cmdfile = popen(cmd, "r");
46 if (!(fgets(buffer, sizeof(buffer), cmdfile))) {
47 perror("Failed to read meminfo\n");
52 *hugepagesize = atoll(buffer);
62 fd = open("/proc/sys/vm/compact_unevictable_allowed",
63 O_RDONLY | O_NONBLOCK);
65 perror("Failed to open\n"
66 "/proc/sys/vm/compact_unevictable_allowed\n");
70 if (read(fd, &allowed, sizeof(char)) != sizeof(char)) {
71 perror("Failed to read from\n"
72 "/proc/sys/vm/compact_unevictable_allowed\n");
84 int check_compaction(unsigned long mem_free, unsigned int hugepage_size)
87 int compaction_index = 0;
88 char initial_nr_hugepages[10] = {0};
89 char nr_hugepages[10] = {0};
91 /* We want to test with 80% of available memory. Else, OOM killer comes
93 mem_free = mem_free * 0.8;
95 fd = open("/proc/sys/vm/nr_hugepages", O_RDWR | O_NONBLOCK);
97 perror("Failed to open /proc/sys/vm/nr_hugepages");
101 if (read(fd, initial_nr_hugepages, sizeof(initial_nr_hugepages)) <= 0) {
102 perror("Failed to read from /proc/sys/vm/nr_hugepages");
106 /* Start with the initial condition of 0 huge pages*/
107 if (write(fd, "0", sizeof(char)) != sizeof(char)) {
108 perror("Failed to write 0 to /proc/sys/vm/nr_hugepages\n");
112 lseek(fd, 0, SEEK_SET);
114 /* Request a large number of huge pages. The Kernel will allocate
116 if (write(fd, "100000", (6*sizeof(char))) != (6*sizeof(char))) {
117 perror("Failed to write 100000 to /proc/sys/vm/nr_hugepages\n");
121 lseek(fd, 0, SEEK_SET);
123 if (read(fd, nr_hugepages, sizeof(nr_hugepages)) <= 0) {
124 perror("Failed to re-read from /proc/sys/vm/nr_hugepages\n");
128 /* We should have been able to request at least 1/3 rd of the memory in
130 compaction_index = mem_free/(atoi(nr_hugepages) * hugepage_size);
132 if (compaction_index > 3) {
133 printf("No of huge pages allocated = %d\n",
134 (atoi(nr_hugepages)));
135 fprintf(stderr, "ERROR: Less that 1/%d of memory is available\n"
136 "as huge pages\n", compaction_index);
140 printf("No of huge pages allocated = %d\n",
141 (atoi(nr_hugepages)));
143 lseek(fd, 0, SEEK_SET);
145 if (write(fd, initial_nr_hugepages, strlen(initial_nr_hugepages))
146 != strlen(initial_nr_hugepages)) {
147 perror("Failed to write value to /proc/sys/vm/nr_hugepages\n");
156 printf("Not OK. Compaction test failed.");
161 int main(int argc, char **argv)
164 struct map_list *list, *entry;
167 unsigned long mem_free = 0;
168 unsigned long hugepage_size = 0;
169 long mem_fragmentable_MB = 0;
172 printf("Either the sysctl compact_unevictable_allowed is not\n"
173 "set to 1 or couldn't read the proc file.\n"
174 "Skipping the test\n");
178 lim.rlim_cur = RLIM_INFINITY;
179 lim.rlim_max = RLIM_INFINITY;
180 if (setrlimit(RLIMIT_MEMLOCK, &lim)) {
181 perror("Failed to set rlimit:\n");
185 page_size = getpagesize();
189 if (read_memory_info(&mem_free, &hugepage_size) != 0) {
190 printf("ERROR: Cannot read meminfo\n");
194 mem_fragmentable_MB = mem_free * 0.8 / 1024;
196 while (mem_fragmentable_MB > 0) {
197 map = mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE,
198 MAP_ANONYMOUS | MAP_PRIVATE | MAP_LOCKED, -1, 0);
199 if (map == MAP_FAILED)
202 entry = malloc(sizeof(struct map_list));
204 munmap(map, MAP_SIZE);
211 /* Write something (in this case the address of the map) to
212 * ensure that KSM can't merge the mapped pages
214 for (i = 0; i < MAP_SIZE; i += page_size)
215 *(unsigned long *)(map + i) = (unsigned long)map + i;
217 mem_fragmentable_MB -= MAP_SIZE_MB;
220 for (entry = list; entry != NULL; entry = entry->next) {
221 munmap(entry->map, MAP_SIZE);
227 if (check_compaction(mem_free, hugepage_size) == 0)