test_kmem.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define _GNU_SOURCE
  3. #include <linux/limits.h>
  4. #include <fcntl.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <sys/stat.h>
  9. #include <sys/types.h>
  10. #include <unistd.h>
  11. #include <sys/wait.h>
  12. #include <errno.h>
  13. #include <sys/sysinfo.h>
  14. #include <pthread.h>
  15. #include "kselftest.h"
  16. #include "cgroup_util.h"
  17. /*
  18. * Memory cgroup charging is performed using percpu batches 64 pages
  19. * big (look at MEMCG_CHARGE_BATCH), whereas memory.stat is exact. So
  20. * the maximum discrepancy between charge and vmstat entries is number
  21. * of cpus multiplied by 64 pages.
  22. */
  23. #define MAX_VMSTAT_ERROR (4096 * 64 * get_nprocs())
  24. #define KMEM_DEAD_WAIT_RETRIES 80
  25. static int alloc_dcache(const char *cgroup, void *arg)
  26. {
  27. unsigned long i;
  28. struct stat st;
  29. char buf[128];
  30. for (i = 0; i < (unsigned long)arg; i++) {
  31. snprintf(buf, sizeof(buf),
  32. "/something-non-existent-with-a-long-name-%64lu-%d",
  33. i, getpid());
  34. stat(buf, &st);
  35. }
  36. return 0;
  37. }
  38. /*
  39. * This test allocates 100000 of negative dentries with long names.
  40. * Then it checks that "slab" in memory.stat is larger than 1M.
  41. * Then it sets memory.high to 1M and checks that at least 1/2
  42. * of slab memory has been reclaimed.
  43. */
  44. static int test_kmem_basic(const char *root)
  45. {
  46. int ret = KSFT_FAIL;
  47. char *cg = NULL;
  48. long slab0, slab1, current;
  49. cg = cg_name(root, "kmem_basic_test");
  50. if (!cg)
  51. goto cleanup;
  52. if (cg_create(cg))
  53. goto cleanup;
  54. if (cg_run(cg, alloc_dcache, (void *)100000))
  55. goto cleanup;
  56. slab0 = cg_read_key_long(cg, "memory.stat", "slab ");
  57. if (slab0 < (1 << 20))
  58. goto cleanup;
  59. cg_write(cg, "memory.high", "1M");
  60. /* wait for RCU freeing */
  61. sleep(1);
  62. slab1 = cg_read_key_long(cg, "memory.stat", "slab ");
  63. if (slab1 < 0)
  64. goto cleanup;
  65. current = cg_read_long(cg, "memory.current");
  66. if (current < 0)
  67. goto cleanup;
  68. if (slab1 < slab0 / 2 && current < slab0 / 2)
  69. ret = KSFT_PASS;
  70. cleanup:
  71. cg_destroy(cg);
  72. free(cg);
  73. return ret;
  74. }
  75. static void *alloc_kmem_fn(void *arg)
  76. {
  77. alloc_dcache(NULL, (void *)100);
  78. return NULL;
  79. }
  80. static int alloc_kmem_smp(const char *cgroup, void *arg)
  81. {
  82. int nr_threads = 2 * get_nprocs();
  83. pthread_t *tinfo;
  84. unsigned long i;
  85. int ret = -1;
  86. tinfo = calloc(nr_threads, sizeof(pthread_t));
  87. if (tinfo == NULL)
  88. return -1;
  89. for (i = 0; i < nr_threads; i++) {
  90. if (pthread_create(&tinfo[i], NULL, &alloc_kmem_fn,
  91. (void *)i)) {
  92. free(tinfo);
  93. return -1;
  94. }
  95. }
  96. for (i = 0; i < nr_threads; i++) {
  97. ret = pthread_join(tinfo[i], NULL);
  98. if (ret)
  99. break;
  100. }
  101. free(tinfo);
  102. return ret;
  103. }
  104. static int cg_run_in_subcgroups(const char *parent,
  105. int (*fn)(const char *cgroup, void *arg),
  106. void *arg, int times)
  107. {
  108. char *child;
  109. int i;
  110. for (i = 0; i < times; i++) {
  111. child = cg_name_indexed(parent, "child", i);
  112. if (!child)
  113. return -1;
  114. if (cg_create(child)) {
  115. cg_destroy(child);
  116. free(child);
  117. return -1;
  118. }
  119. if (cg_run(child, fn, NULL)) {
  120. cg_destroy(child);
  121. free(child);
  122. return -1;
  123. }
  124. cg_destroy(child);
  125. free(child);
  126. }
  127. return 0;
  128. }
  129. /*
  130. * The test creates and destroys a large number of cgroups. In each cgroup it
  131. * allocates some slab memory (mostly negative dentries) using 2 * NR_CPUS
  132. * threads. Then it checks the sanity of numbers on the parent level:
  133. * the total size of the cgroups should be roughly equal to
  134. * anon + file + kernel + sock.
  135. */
  136. static int test_kmem_memcg_deletion(const char *root)
  137. {
  138. long current, anon, file, kernel, sock, sum;
  139. int ret = KSFT_FAIL;
  140. char *parent;
  141. parent = cg_name(root, "kmem_memcg_deletion_test");
  142. if (!parent)
  143. goto cleanup;
  144. if (cg_create(parent))
  145. goto cleanup;
  146. if (cg_write(parent, "cgroup.subtree_control", "+memory"))
  147. goto cleanup;
  148. if (cg_run_in_subcgroups(parent, alloc_kmem_smp, NULL, 100))
  149. goto cleanup;
  150. current = cg_read_long(parent, "memory.current");
  151. anon = cg_read_key_long(parent, "memory.stat", "anon ");
  152. file = cg_read_key_long(parent, "memory.stat", "file ");
  153. kernel = cg_read_key_long(parent, "memory.stat", "kernel ");
  154. sock = cg_read_key_long(parent, "memory.stat", "sock ");
  155. if (current < 0 || anon < 0 || file < 0 || kernel < 0 || sock < 0)
  156. goto cleanup;
  157. sum = anon + file + kernel + sock;
  158. if (labs(sum - current) < MAX_VMSTAT_ERROR) {
  159. ret = KSFT_PASS;
  160. } else {
  161. printf("memory.current = %ld\n", current);
  162. printf("anon + file + kernel + sock = %ld\n", sum);
  163. printf("anon = %ld\n", anon);
  164. printf("file = %ld\n", file);
  165. printf("kernel = %ld\n", kernel);
  166. printf("sock = %ld\n", sock);
  167. }
  168. cleanup:
  169. cg_destroy(parent);
  170. free(parent);
  171. return ret;
  172. }
  173. /*
  174. * The test reads the entire /proc/kpagecgroup. If the operation went
  175. * successfully (and the kernel didn't panic), the test is treated as passed.
  176. */
  177. static int test_kmem_proc_kpagecgroup(const char *root)
  178. {
  179. unsigned long buf[128];
  180. int ret = KSFT_FAIL;
  181. ssize_t len;
  182. int fd;
  183. fd = open("/proc/kpagecgroup", O_RDONLY);
  184. if (fd < 0)
  185. return ret;
  186. do {
  187. len = read(fd, buf, sizeof(buf));
  188. } while (len > 0);
  189. if (len == 0)
  190. ret = KSFT_PASS;
  191. close(fd);
  192. return ret;
  193. }
  194. static void *pthread_wait_fn(void *arg)
  195. {
  196. sleep(100);
  197. return NULL;
  198. }
  199. static int spawn_1000_threads(const char *cgroup, void *arg)
  200. {
  201. int nr_threads = 1000;
  202. pthread_t *tinfo;
  203. unsigned long i;
  204. long stack;
  205. int ret = -1;
  206. tinfo = calloc(nr_threads, sizeof(pthread_t));
  207. if (tinfo == NULL)
  208. return -1;
  209. for (i = 0; i < nr_threads; i++) {
  210. if (pthread_create(&tinfo[i], NULL, &pthread_wait_fn,
  211. (void *)i)) {
  212. free(tinfo);
  213. return(-1);
  214. }
  215. }
  216. stack = cg_read_key_long(cgroup, "memory.stat", "kernel_stack ");
  217. if (stack >= 4096 * 1000)
  218. ret = 0;
  219. free(tinfo);
  220. return ret;
  221. }
  222. /*
  223. * The test spawns a process, which spawns 1000 threads. Then it checks
  224. * that memory.stat's kernel_stack is at least 1000 pages large.
  225. */
  226. static int test_kmem_kernel_stacks(const char *root)
  227. {
  228. int ret = KSFT_FAIL;
  229. char *cg = NULL;
  230. cg = cg_name(root, "kmem_kernel_stacks_test");
  231. if (!cg)
  232. goto cleanup;
  233. if (cg_create(cg))
  234. goto cleanup;
  235. if (cg_run(cg, spawn_1000_threads, NULL))
  236. goto cleanup;
  237. ret = KSFT_PASS;
  238. cleanup:
  239. cg_destroy(cg);
  240. free(cg);
  241. return ret;
  242. }
  243. /*
  244. * This test sequentionally creates 30 child cgroups, allocates some
  245. * kernel memory in each of them, and deletes them. Then it checks
  246. * that the number of dying cgroups on the parent level is 0.
  247. */
  248. static int test_kmem_dead_cgroups(const char *root)
  249. {
  250. int ret = KSFT_FAIL;
  251. char *parent;
  252. long dead = -1;
  253. parent = cg_name(root, "kmem_dead_cgroups_test");
  254. if (!parent)
  255. goto cleanup;
  256. if (cg_create(parent))
  257. goto cleanup;
  258. if (cg_write(parent, "cgroup.subtree_control", "+memory"))
  259. goto cleanup;
  260. if (cg_run_in_subcgroups(parent, alloc_dcache, (void *)100, 30))
  261. goto cleanup;
  262. /*
  263. * Allow up to ~8s for reclaim of dying descendants to complete.
  264. * This is a generous upper bound derived from stress testing, not
  265. * from a specific kernel constant, and can be adjusted if reclaim
  266. * behavior changes in the future.
  267. */
  268. dead = cg_read_key_long_poll(parent, "cgroup.stat",
  269. "nr_dying_descendants ", 0, KMEM_DEAD_WAIT_RETRIES,
  270. DEFAULT_WAIT_INTERVAL_US);
  271. if (dead)
  272. goto cleanup;
  273. ret = KSFT_PASS;
  274. cleanup:
  275. cg_destroy(parent);
  276. free(parent);
  277. return ret;
  278. }
  279. /*
  280. * This test creates a sub-tree with 1000 memory cgroups.
  281. * Then it checks that the memory.current on the parent level
  282. * is greater than 0 and approximates matches the percpu value
  283. * from memory.stat.
  284. */
  285. static int test_percpu_basic(const char *root)
  286. {
  287. int ret = KSFT_FAIL;
  288. char *parent, *child;
  289. long current, percpu;
  290. int i;
  291. parent = cg_name(root, "percpu_basic_test");
  292. if (!parent)
  293. goto cleanup;
  294. if (cg_create(parent))
  295. goto cleanup;
  296. if (cg_write(parent, "cgroup.subtree_control", "+memory"))
  297. goto cleanup;
  298. for (i = 0; i < 1000; i++) {
  299. child = cg_name_indexed(parent, "child", i);
  300. if (!child)
  301. return -1;
  302. if (cg_create(child))
  303. goto cleanup_children;
  304. free(child);
  305. }
  306. current = cg_read_long(parent, "memory.current");
  307. percpu = cg_read_key_long(parent, "memory.stat", "percpu ");
  308. if (current > 0 && percpu > 0 && labs(current - percpu) <
  309. MAX_VMSTAT_ERROR)
  310. ret = KSFT_PASS;
  311. else
  312. printf("memory.current %ld\npercpu %ld\n",
  313. current, percpu);
  314. cleanup_children:
  315. for (i = 0; i < 1000; i++) {
  316. child = cg_name_indexed(parent, "child", i);
  317. cg_destroy(child);
  318. free(child);
  319. }
  320. cleanup:
  321. cg_destroy(parent);
  322. free(parent);
  323. return ret;
  324. }
  325. #define T(x) { x, #x }
  326. struct kmem_test {
  327. int (*fn)(const char *root);
  328. const char *name;
  329. } tests[] = {
  330. T(test_kmem_basic),
  331. T(test_kmem_memcg_deletion),
  332. T(test_kmem_proc_kpagecgroup),
  333. T(test_kmem_kernel_stacks),
  334. T(test_kmem_dead_cgroups),
  335. T(test_percpu_basic),
  336. };
  337. #undef T
  338. int main(int argc, char **argv)
  339. {
  340. char root[PATH_MAX];
  341. int i;
  342. ksft_print_header();
  343. ksft_set_plan(ARRAY_SIZE(tests));
  344. if (cg_find_unified_root(root, sizeof(root), NULL))
  345. ksft_exit_skip("cgroup v2 isn't mounted\n");
  346. /*
  347. * Check that memory controller is available:
  348. * memory is listed in cgroup.controllers
  349. */
  350. if (cg_read_strstr(root, "cgroup.controllers", "memory"))
  351. ksft_exit_skip("memory controller isn't available\n");
  352. if (cg_read_strstr(root, "cgroup.subtree_control", "memory"))
  353. if (cg_write(root, "cgroup.subtree_control", "+memory"))
  354. ksft_exit_skip("Failed to set memory controller\n");
  355. for (i = 0; i < ARRAY_SIZE(tests); i++) {
  356. switch (tests[i].fn(root)) {
  357. case KSFT_PASS:
  358. ksft_test_result_pass("%s\n", tests[i].name);
  359. break;
  360. case KSFT_SKIP:
  361. ksft_test_result_skip("%s\n", tests[i].name);
  362. break;
  363. default:
  364. ksft_test_result_fail("%s\n", tests[i].name);
  365. break;
  366. }
  367. }
  368. ksft_finished();
  369. }