test_hugetlb_memcg.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define _GNU_SOURCE
  3. #include <linux/limits.h>
  4. #include <sys/mman.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <fcntl.h>
  9. #include "../kselftest.h"
  10. #include "cgroup_util.h"
  11. #define ADDR ((void *)(0x0UL))
  12. #define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB)
  13. /* mapping 8 MBs == 4 hugepages */
  14. #define LENGTH (8UL*1024*1024)
  15. #define PROTECTION (PROT_READ | PROT_WRITE)
  16. /* borrowed from mm/hmm-tests.c */
  17. static long get_hugepage_size(void)
  18. {
  19. int fd;
  20. char buf[2048];
  21. int len;
  22. char *p, *q, *path = "/proc/meminfo", *tag = "Hugepagesize:";
  23. long val;
  24. fd = open(path, O_RDONLY);
  25. if (fd < 0) {
  26. /* Error opening the file */
  27. return -1;
  28. }
  29. len = read(fd, buf, sizeof(buf));
  30. close(fd);
  31. if (len < 0) {
  32. /* Error in reading the file */
  33. return -1;
  34. }
  35. if (len == sizeof(buf)) {
  36. /* Error file is too large */
  37. return -1;
  38. }
  39. buf[len] = '\0';
  40. /* Search for a tag if provided */
  41. if (tag) {
  42. p = strstr(buf, tag);
  43. if (!p)
  44. return -1; /* looks like the line we want isn't there */
  45. p += strlen(tag);
  46. } else
  47. p = buf;
  48. val = strtol(p, &q, 0);
  49. if (*q != ' ') {
  50. /* Error parsing the file */
  51. return -1;
  52. }
  53. return val;
  54. }
  55. static int set_file(const char *path, long value)
  56. {
  57. FILE *file;
  58. int ret;
  59. file = fopen(path, "w");
  60. if (!file)
  61. return -1;
  62. ret = fprintf(file, "%ld\n", value);
  63. fclose(file);
  64. return ret;
  65. }
  66. static int set_nr_hugepages(long value)
  67. {
  68. return set_file("/proc/sys/vm/nr_hugepages", value);
  69. }
  70. static unsigned int check_first(char *addr)
  71. {
  72. return *(unsigned int *)addr;
  73. }
  74. static void write_data(char *addr)
  75. {
  76. unsigned long i;
  77. for (i = 0; i < LENGTH; i++)
  78. *(addr + i) = (char)i;
  79. }
  80. static int hugetlb_test_program(const char *cgroup, void *arg)
  81. {
  82. char *test_group = (char *)arg;
  83. void *addr;
  84. long old_current, expected_current, current;
  85. int ret = EXIT_FAILURE;
  86. old_current = cg_read_long(test_group, "memory.current");
  87. set_nr_hugepages(20);
  88. current = cg_read_long(test_group, "memory.current");
  89. if (current - old_current >= MB(2)) {
  90. ksft_print_msg(
  91. "setting nr_hugepages should not increase hugepage usage.\n");
  92. ksft_print_msg("before: %ld, after: %ld\n", old_current, current);
  93. return EXIT_FAILURE;
  94. }
  95. addr = mmap(ADDR, LENGTH, PROTECTION, FLAGS, 0, 0);
  96. if (addr == MAP_FAILED) {
  97. ksft_print_msg("fail to mmap.\n");
  98. return EXIT_FAILURE;
  99. }
  100. current = cg_read_long(test_group, "memory.current");
  101. if (current - old_current >= MB(2)) {
  102. ksft_print_msg("mmap should not increase hugepage usage.\n");
  103. ksft_print_msg("before: %ld, after: %ld\n", old_current, current);
  104. goto out_failed_munmap;
  105. }
  106. old_current = current;
  107. /* read the first page */
  108. check_first(addr);
  109. expected_current = old_current + MB(2);
  110. current = cg_read_long(test_group, "memory.current");
  111. if (!values_close(expected_current, current, 5)) {
  112. ksft_print_msg("memory usage should increase by around 2MB.\n");
  113. ksft_print_msg(
  114. "expected memory: %ld, actual memory: %ld\n",
  115. expected_current, current);
  116. goto out_failed_munmap;
  117. }
  118. /* write to the whole range */
  119. write_data(addr);
  120. current = cg_read_long(test_group, "memory.current");
  121. expected_current = old_current + MB(8);
  122. if (!values_close(expected_current, current, 5)) {
  123. ksft_print_msg("memory usage should increase by around 8MB.\n");
  124. ksft_print_msg(
  125. "expected memory: %ld, actual memory: %ld\n",
  126. expected_current, current);
  127. goto out_failed_munmap;
  128. }
  129. /* unmap the whole range */
  130. munmap(addr, LENGTH);
  131. current = cg_read_long(test_group, "memory.current");
  132. expected_current = old_current;
  133. if (!values_close(expected_current, current, 5)) {
  134. ksft_print_msg("memory usage should go back down.\n");
  135. ksft_print_msg(
  136. "expected memory: %ld, actual memory: %ld\n",
  137. expected_current, current);
  138. return ret;
  139. }
  140. ret = EXIT_SUCCESS;
  141. return ret;
  142. out_failed_munmap:
  143. munmap(addr, LENGTH);
  144. return ret;
  145. }
  146. static int test_hugetlb_memcg(char *root)
  147. {
  148. int ret = KSFT_FAIL;
  149. char *test_group;
  150. test_group = cg_name(root, "hugetlb_memcg_test");
  151. if (!test_group || cg_create(test_group)) {
  152. ksft_print_msg("fail to create cgroup.\n");
  153. goto out;
  154. }
  155. if (cg_write(test_group, "memory.max", "100M")) {
  156. ksft_print_msg("fail to set cgroup memory limit.\n");
  157. goto out;
  158. }
  159. /* disable swap */
  160. if (cg_write(test_group, "memory.swap.max", "0")) {
  161. ksft_print_msg("fail to disable swap.\n");
  162. goto out;
  163. }
  164. if (!cg_run(test_group, hugetlb_test_program, (void *)test_group))
  165. ret = KSFT_PASS;
  166. out:
  167. cg_destroy(test_group);
  168. free(test_group);
  169. return ret;
  170. }
  171. int main(int argc, char **argv)
  172. {
  173. char root[PATH_MAX];
  174. int ret = EXIT_SUCCESS, has_memory_hugetlb_acc;
  175. has_memory_hugetlb_acc = proc_mount_contains("memory_hugetlb_accounting");
  176. if (has_memory_hugetlb_acc < 0)
  177. ksft_exit_skip("Failed to query cgroup mount option\n");
  178. else if (!has_memory_hugetlb_acc)
  179. ksft_exit_skip("memory hugetlb accounting is disabled\n");
  180. /* Unit is kB! */
  181. if (get_hugepage_size() != 2048) {
  182. ksft_print_msg("test_hugetlb_memcg requires 2MB hugepages\n");
  183. ksft_test_result_skip("test_hugetlb_memcg\n");
  184. return ret;
  185. }
  186. if (cg_find_unified_root(root, sizeof(root), NULL))
  187. ksft_exit_skip("cgroup v2 isn't mounted\n");
  188. switch (test_hugetlb_memcg(root)) {
  189. case KSFT_PASS:
  190. ksft_test_result_pass("test_hugetlb_memcg\n");
  191. break;
  192. case KSFT_SKIP:
  193. ksft_test_result_skip("test_hugetlb_memcg\n");
  194. break;
  195. default:
  196. ret = EXIT_FAILURE;
  197. ksft_test_result_fail("test_hugetlb_memcg\n");
  198. break;
  199. }
  200. return ret;
  201. }