compaction_test.c 6.6 KB

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