test_memcontrol.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #define _GNU_SOURCE
  3. #include <linux/limits.h>
  4. #include <linux/oom.h>
  5. #include <fcntl.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sys/stat.h>
  10. #include <sys/types.h>
  11. #include <unistd.h>
  12. #include <sys/socket.h>
  13. #include <sys/wait.h>
  14. #include <arpa/inet.h>
  15. #include <netinet/in.h>
  16. #include <netdb.h>
  17. #include <errno.h>
  18. #include <sys/mman.h>
  19. #include "../kselftest.h"
  20. #include "cgroup_util.h"
  21. static bool has_localevents;
  22. static bool has_recursiveprot;
  23. /*
  24. * This test creates two nested cgroups with and without enabling
  25. * the memory controller.
  26. */
  27. static int test_memcg_subtree_control(const char *root)
  28. {
  29. char *parent, *child, *parent2 = NULL, *child2 = NULL;
  30. int ret = KSFT_FAIL;
  31. char buf[PAGE_SIZE];
  32. /* Create two nested cgroups with the memory controller enabled */
  33. parent = cg_name(root, "memcg_test_0");
  34. child = cg_name(root, "memcg_test_0/memcg_test_1");
  35. if (!parent || !child)
  36. goto cleanup_free;
  37. if (cg_create(parent))
  38. goto cleanup_free;
  39. if (cg_write(parent, "cgroup.subtree_control", "+memory"))
  40. goto cleanup_parent;
  41. if (cg_create(child))
  42. goto cleanup_parent;
  43. if (cg_read_strstr(child, "cgroup.controllers", "memory"))
  44. goto cleanup_child;
  45. /* Create two nested cgroups without enabling memory controller */
  46. parent2 = cg_name(root, "memcg_test_1");
  47. child2 = cg_name(root, "memcg_test_1/memcg_test_1");
  48. if (!parent2 || !child2)
  49. goto cleanup_free2;
  50. if (cg_create(parent2))
  51. goto cleanup_free2;
  52. if (cg_create(child2))
  53. goto cleanup_parent2;
  54. if (cg_read(child2, "cgroup.controllers", buf, sizeof(buf)))
  55. goto cleanup_all;
  56. if (!cg_read_strstr(child2, "cgroup.controllers", "memory"))
  57. goto cleanup_all;
  58. ret = KSFT_PASS;
  59. cleanup_all:
  60. cg_destroy(child2);
  61. cleanup_parent2:
  62. cg_destroy(parent2);
  63. cleanup_free2:
  64. free(parent2);
  65. free(child2);
  66. cleanup_child:
  67. cg_destroy(child);
  68. cleanup_parent:
  69. cg_destroy(parent);
  70. cleanup_free:
  71. free(parent);
  72. free(child);
  73. return ret;
  74. }
  75. static int alloc_anon_50M_check(const char *cgroup, void *arg)
  76. {
  77. size_t size = MB(50);
  78. char *buf, *ptr;
  79. long anon, current;
  80. int ret = -1;
  81. buf = malloc(size);
  82. if (buf == NULL) {
  83. fprintf(stderr, "malloc() failed\n");
  84. return -1;
  85. }
  86. for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE)
  87. *ptr = 0;
  88. current = cg_read_long(cgroup, "memory.current");
  89. if (current < size)
  90. goto cleanup;
  91. if (!values_close(size, current, 3))
  92. goto cleanup;
  93. anon = cg_read_key_long(cgroup, "memory.stat", "anon ");
  94. if (anon < 0)
  95. goto cleanup;
  96. if (!values_close(anon, current, 3))
  97. goto cleanup;
  98. ret = 0;
  99. cleanup:
  100. free(buf);
  101. return ret;
  102. }
  103. static int alloc_pagecache_50M_check(const char *cgroup, void *arg)
  104. {
  105. size_t size = MB(50);
  106. int ret = -1;
  107. long current, file;
  108. int fd;
  109. fd = get_temp_fd();
  110. if (fd < 0)
  111. return -1;
  112. if (alloc_pagecache(fd, size))
  113. goto cleanup;
  114. current = cg_read_long(cgroup, "memory.current");
  115. if (current < size)
  116. goto cleanup;
  117. file = cg_read_key_long(cgroup, "memory.stat", "file ");
  118. if (file < 0)
  119. goto cleanup;
  120. if (!values_close(file, current, 10))
  121. goto cleanup;
  122. ret = 0;
  123. cleanup:
  124. close(fd);
  125. return ret;
  126. }
  127. /*
  128. * This test create a memory cgroup, allocates
  129. * some anonymous memory and some pagecache
  130. * and checks memory.current, memory.peak, and some memory.stat values.
  131. */
  132. static int test_memcg_current_peak(const char *root)
  133. {
  134. int ret = KSFT_FAIL;
  135. long current, peak, peak_reset;
  136. char *memcg;
  137. bool fd2_closed = false, fd3_closed = false, fd4_closed = false;
  138. int peak_fd = -1, peak_fd2 = -1, peak_fd3 = -1, peak_fd4 = -1;
  139. struct stat ss;
  140. memcg = cg_name(root, "memcg_test");
  141. if (!memcg)
  142. goto cleanup;
  143. if (cg_create(memcg))
  144. goto cleanup;
  145. current = cg_read_long(memcg, "memory.current");
  146. if (current != 0)
  147. goto cleanup;
  148. peak = cg_read_long(memcg, "memory.peak");
  149. if (peak != 0)
  150. goto cleanup;
  151. if (cg_run(memcg, alloc_anon_50M_check, NULL))
  152. goto cleanup;
  153. peak = cg_read_long(memcg, "memory.peak");
  154. if (peak < MB(50))
  155. goto cleanup;
  156. /*
  157. * We'll open a few FDs for the same memory.peak file to exercise the free-path
  158. * We need at least three to be closed in a different order than writes occurred to test
  159. * the linked-list handling.
  160. */
  161. peak_fd = cg_open(memcg, "memory.peak", O_RDWR | O_APPEND | O_CLOEXEC);
  162. if (peak_fd == -1) {
  163. if (errno == ENOENT)
  164. ret = KSFT_SKIP;
  165. goto cleanup;
  166. }
  167. /*
  168. * Before we try to use memory.peak's fd, try to figure out whether
  169. * this kernel supports writing to that file in the first place. (by
  170. * checking the writable bit on the file's st_mode)
  171. */
  172. if (fstat(peak_fd, &ss))
  173. goto cleanup;
  174. if ((ss.st_mode & S_IWUSR) == 0) {
  175. ret = KSFT_SKIP;
  176. goto cleanup;
  177. }
  178. peak_fd2 = cg_open(memcg, "memory.peak", O_RDWR | O_APPEND | O_CLOEXEC);
  179. if (peak_fd2 == -1)
  180. goto cleanup;
  181. peak_fd3 = cg_open(memcg, "memory.peak", O_RDWR | O_APPEND | O_CLOEXEC);
  182. if (peak_fd3 == -1)
  183. goto cleanup;
  184. /* any non-empty string resets, but make it clear */
  185. static const char reset_string[] = "reset\n";
  186. peak_reset = write(peak_fd, reset_string, sizeof(reset_string));
  187. if (peak_reset != sizeof(reset_string))
  188. goto cleanup;
  189. peak_reset = write(peak_fd2, reset_string, sizeof(reset_string));
  190. if (peak_reset != sizeof(reset_string))
  191. goto cleanup;
  192. peak_reset = write(peak_fd3, reset_string, sizeof(reset_string));
  193. if (peak_reset != sizeof(reset_string))
  194. goto cleanup;
  195. /* Make sure a completely independent read isn't affected by our FD-local reset above*/
  196. peak = cg_read_long(memcg, "memory.peak");
  197. if (peak < MB(50))
  198. goto cleanup;
  199. fd2_closed = true;
  200. if (close(peak_fd2))
  201. goto cleanup;
  202. peak_fd4 = cg_open(memcg, "memory.peak", O_RDWR | O_APPEND | O_CLOEXEC);
  203. if (peak_fd4 == -1)
  204. goto cleanup;
  205. peak_reset = write(peak_fd4, reset_string, sizeof(reset_string));
  206. if (peak_reset != sizeof(reset_string))
  207. goto cleanup;
  208. peak = cg_read_long_fd(peak_fd);
  209. if (peak > MB(30) || peak < 0)
  210. goto cleanup;
  211. if (cg_run(memcg, alloc_pagecache_50M_check, NULL))
  212. goto cleanup;
  213. peak = cg_read_long(memcg, "memory.peak");
  214. if (peak < MB(50))
  215. goto cleanup;
  216. /* Make sure everything is back to normal */
  217. peak = cg_read_long_fd(peak_fd);
  218. if (peak < MB(50))
  219. goto cleanup;
  220. peak = cg_read_long_fd(peak_fd4);
  221. if (peak < MB(50))
  222. goto cleanup;
  223. fd3_closed = true;
  224. if (close(peak_fd3))
  225. goto cleanup;
  226. fd4_closed = true;
  227. if (close(peak_fd4))
  228. goto cleanup;
  229. ret = KSFT_PASS;
  230. cleanup:
  231. close(peak_fd);
  232. if (!fd2_closed)
  233. close(peak_fd2);
  234. if (!fd3_closed)
  235. close(peak_fd3);
  236. if (!fd4_closed)
  237. close(peak_fd4);
  238. cg_destroy(memcg);
  239. free(memcg);
  240. return ret;
  241. }
  242. static int alloc_pagecache_50M_noexit(const char *cgroup, void *arg)
  243. {
  244. int fd = (long)arg;
  245. int ppid = getppid();
  246. if (alloc_pagecache(fd, MB(50)))
  247. return -1;
  248. while (getppid() == ppid)
  249. sleep(1);
  250. return 0;
  251. }
  252. static int alloc_anon_noexit(const char *cgroup, void *arg)
  253. {
  254. int ppid = getppid();
  255. size_t size = (unsigned long)arg;
  256. char *buf, *ptr;
  257. buf = malloc(size);
  258. if (buf == NULL) {
  259. fprintf(stderr, "malloc() failed\n");
  260. return -1;
  261. }
  262. for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE)
  263. *ptr = 0;
  264. while (getppid() == ppid)
  265. sleep(1);
  266. free(buf);
  267. return 0;
  268. }
  269. /*
  270. * Wait until processes are killed asynchronously by the OOM killer
  271. * If we exceed a timeout, fail.
  272. */
  273. static int cg_test_proc_killed(const char *cgroup)
  274. {
  275. int limit;
  276. for (limit = 10; limit > 0; limit--) {
  277. if (cg_read_strcmp(cgroup, "cgroup.procs", "") == 0)
  278. return 0;
  279. usleep(100000);
  280. }
  281. return -1;
  282. }
  283. static bool reclaim_until(const char *memcg, long goal);
  284. /*
  285. * First, this test creates the following hierarchy:
  286. * A memory.min = 0, memory.max = 200M
  287. * A/B memory.min = 50M
  288. * A/B/C memory.min = 75M, memory.current = 50M
  289. * A/B/D memory.min = 25M, memory.current = 50M
  290. * A/B/E memory.min = 0, memory.current = 50M
  291. * A/B/F memory.min = 500M, memory.current = 0
  292. *
  293. * (or memory.low if we test soft protection)
  294. *
  295. * Usages are pagecache and the test keeps a running
  296. * process in every leaf cgroup.
  297. * Then it creates A/G and creates a significant
  298. * memory pressure in A.
  299. *
  300. * Then it checks actual memory usages and expects that:
  301. * A/B memory.current ~= 50M
  302. * A/B/C memory.current ~= 29M
  303. * A/B/D memory.current ~= 21M
  304. * A/B/E memory.current ~= 0
  305. * A/B/F memory.current = 0
  306. * (for origin of the numbers, see model in memcg_protection.m.)
  307. *
  308. * After that it tries to allocate more than there is
  309. * unprotected memory in A available, and checks that:
  310. * a) memory.min protects pagecache even in this case,
  311. * b) memory.low allows reclaiming page cache with low events.
  312. *
  313. * Then we try to reclaim from A/B/C using memory.reclaim until its
  314. * usage reaches 10M.
  315. * This makes sure that:
  316. * (a) We ignore the protection of the reclaim target memcg.
  317. * (b) The previously calculated emin value (~29M) should be dismissed.
  318. */
  319. static int test_memcg_protection(const char *root, bool min)
  320. {
  321. int ret = KSFT_FAIL, rc;
  322. char *parent[3] = {NULL};
  323. char *children[4] = {NULL};
  324. const char *attribute = min ? "memory.min" : "memory.low";
  325. long c[4];
  326. long current;
  327. int i, attempts;
  328. int fd;
  329. fd = get_temp_fd();
  330. if (fd < 0)
  331. goto cleanup;
  332. parent[0] = cg_name(root, "memcg_test_0");
  333. if (!parent[0])
  334. goto cleanup;
  335. parent[1] = cg_name(parent[0], "memcg_test_1");
  336. if (!parent[1])
  337. goto cleanup;
  338. parent[2] = cg_name(parent[0], "memcg_test_2");
  339. if (!parent[2])
  340. goto cleanup;
  341. if (cg_create(parent[0]))
  342. goto cleanup;
  343. if (cg_read_long(parent[0], attribute)) {
  344. /* No memory.min on older kernels is fine */
  345. if (min)
  346. ret = KSFT_SKIP;
  347. goto cleanup;
  348. }
  349. if (cg_write(parent[0], "cgroup.subtree_control", "+memory"))
  350. goto cleanup;
  351. if (cg_write(parent[0], "memory.max", "200M"))
  352. goto cleanup;
  353. if (cg_write(parent[0], "memory.swap.max", "0"))
  354. goto cleanup;
  355. if (cg_create(parent[1]))
  356. goto cleanup;
  357. if (cg_write(parent[1], "cgroup.subtree_control", "+memory"))
  358. goto cleanup;
  359. if (cg_create(parent[2]))
  360. goto cleanup;
  361. for (i = 0; i < ARRAY_SIZE(children); i++) {
  362. children[i] = cg_name_indexed(parent[1], "child_memcg", i);
  363. if (!children[i])
  364. goto cleanup;
  365. if (cg_create(children[i]))
  366. goto cleanup;
  367. if (i > 2)
  368. continue;
  369. cg_run_nowait(children[i], alloc_pagecache_50M_noexit,
  370. (void *)(long)fd);
  371. }
  372. if (cg_write(parent[1], attribute, "50M"))
  373. goto cleanup;
  374. if (cg_write(children[0], attribute, "75M"))
  375. goto cleanup;
  376. if (cg_write(children[1], attribute, "25M"))
  377. goto cleanup;
  378. if (cg_write(children[2], attribute, "0"))
  379. goto cleanup;
  380. if (cg_write(children[3], attribute, "500M"))
  381. goto cleanup;
  382. attempts = 0;
  383. while (!values_close(cg_read_long(parent[1], "memory.current"),
  384. MB(150), 3)) {
  385. if (attempts++ > 5)
  386. break;
  387. sleep(1);
  388. }
  389. if (cg_run(parent[2], alloc_anon, (void *)MB(148)))
  390. goto cleanup;
  391. if (!values_close(cg_read_long(parent[1], "memory.current"), MB(50), 3))
  392. goto cleanup;
  393. for (i = 0; i < ARRAY_SIZE(children); i++)
  394. c[i] = cg_read_long(children[i], "memory.current");
  395. if (!values_close(c[0], MB(29), 10))
  396. goto cleanup;
  397. if (!values_close(c[1], MB(21), 10))
  398. goto cleanup;
  399. if (c[3] != 0)
  400. goto cleanup;
  401. rc = cg_run(parent[2], alloc_anon, (void *)MB(170));
  402. if (min && !rc)
  403. goto cleanup;
  404. else if (!min && rc) {
  405. fprintf(stderr,
  406. "memory.low prevents from allocating anon memory\n");
  407. goto cleanup;
  408. }
  409. current = min ? MB(50) : MB(30);
  410. if (!values_close(cg_read_long(parent[1], "memory.current"), current, 3))
  411. goto cleanup;
  412. if (!reclaim_until(children[0], MB(10)))
  413. goto cleanup;
  414. if (min) {
  415. ret = KSFT_PASS;
  416. goto cleanup;
  417. }
  418. for (i = 0; i < ARRAY_SIZE(children); i++) {
  419. int no_low_events_index = 1;
  420. long low, oom;
  421. oom = cg_read_key_long(children[i], "memory.events", "oom ");
  422. low = cg_read_key_long(children[i], "memory.events", "low ");
  423. if (oom)
  424. goto cleanup;
  425. if (i <= no_low_events_index && low <= 0)
  426. goto cleanup;
  427. if (i > no_low_events_index && low)
  428. goto cleanup;
  429. }
  430. ret = KSFT_PASS;
  431. cleanup:
  432. for (i = ARRAY_SIZE(children) - 1; i >= 0; i--) {
  433. if (!children[i])
  434. continue;
  435. cg_destroy(children[i]);
  436. free(children[i]);
  437. }
  438. for (i = ARRAY_SIZE(parent) - 1; i >= 0; i--) {
  439. if (!parent[i])
  440. continue;
  441. cg_destroy(parent[i]);
  442. free(parent[i]);
  443. }
  444. close(fd);
  445. return ret;
  446. }
  447. static int test_memcg_min(const char *root)
  448. {
  449. return test_memcg_protection(root, true);
  450. }
  451. static int test_memcg_low(const char *root)
  452. {
  453. return test_memcg_protection(root, false);
  454. }
  455. static int alloc_pagecache_max_30M(const char *cgroup, void *arg)
  456. {
  457. size_t size = MB(50);
  458. int ret = -1;
  459. long current, high, max;
  460. int fd;
  461. high = cg_read_long(cgroup, "memory.high");
  462. max = cg_read_long(cgroup, "memory.max");
  463. if (high != MB(30) && max != MB(30))
  464. return -1;
  465. fd = get_temp_fd();
  466. if (fd < 0)
  467. return -1;
  468. if (alloc_pagecache(fd, size))
  469. goto cleanup;
  470. current = cg_read_long(cgroup, "memory.current");
  471. if (!values_close(current, MB(30), 5))
  472. goto cleanup;
  473. ret = 0;
  474. cleanup:
  475. close(fd);
  476. return ret;
  477. }
  478. /*
  479. * This test checks that memory.high limits the amount of
  480. * memory which can be consumed by either anonymous memory
  481. * or pagecache.
  482. */
  483. static int test_memcg_high(const char *root)
  484. {
  485. int ret = KSFT_FAIL;
  486. char *memcg;
  487. long high;
  488. memcg = cg_name(root, "memcg_test");
  489. if (!memcg)
  490. goto cleanup;
  491. if (cg_create(memcg))
  492. goto cleanup;
  493. if (cg_read_strcmp(memcg, "memory.high", "max\n"))
  494. goto cleanup;
  495. if (cg_write(memcg, "memory.swap.max", "0"))
  496. goto cleanup;
  497. if (cg_write(memcg, "memory.high", "30M"))
  498. goto cleanup;
  499. if (cg_run(memcg, alloc_anon, (void *)MB(31)))
  500. goto cleanup;
  501. if (!cg_run(memcg, alloc_pagecache_50M_check, NULL))
  502. goto cleanup;
  503. if (cg_run(memcg, alloc_pagecache_max_30M, NULL))
  504. goto cleanup;
  505. high = cg_read_key_long(memcg, "memory.events", "high ");
  506. if (high <= 0)
  507. goto cleanup;
  508. ret = KSFT_PASS;
  509. cleanup:
  510. cg_destroy(memcg);
  511. free(memcg);
  512. return ret;
  513. }
  514. static int alloc_anon_mlock(const char *cgroup, void *arg)
  515. {
  516. size_t size = (size_t)arg;
  517. void *buf;
  518. buf = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON,
  519. 0, 0);
  520. if (buf == MAP_FAILED)
  521. return -1;
  522. mlock(buf, size);
  523. munmap(buf, size);
  524. return 0;
  525. }
  526. /*
  527. * This test checks that memory.high is able to throttle big single shot
  528. * allocation i.e. large allocation within one kernel entry.
  529. */
  530. static int test_memcg_high_sync(const char *root)
  531. {
  532. int ret = KSFT_FAIL, pid, fd = -1;
  533. char *memcg;
  534. long pre_high, pre_max;
  535. long post_high, post_max;
  536. memcg = cg_name(root, "memcg_test");
  537. if (!memcg)
  538. goto cleanup;
  539. if (cg_create(memcg))
  540. goto cleanup;
  541. pre_high = cg_read_key_long(memcg, "memory.events", "high ");
  542. pre_max = cg_read_key_long(memcg, "memory.events", "max ");
  543. if (pre_high < 0 || pre_max < 0)
  544. goto cleanup;
  545. if (cg_write(memcg, "memory.swap.max", "0"))
  546. goto cleanup;
  547. if (cg_write(memcg, "memory.high", "30M"))
  548. goto cleanup;
  549. if (cg_write(memcg, "memory.max", "140M"))
  550. goto cleanup;
  551. fd = memcg_prepare_for_wait(memcg);
  552. if (fd < 0)
  553. goto cleanup;
  554. pid = cg_run_nowait(memcg, alloc_anon_mlock, (void *)MB(200));
  555. if (pid < 0)
  556. goto cleanup;
  557. cg_wait_for(fd);
  558. post_high = cg_read_key_long(memcg, "memory.events", "high ");
  559. post_max = cg_read_key_long(memcg, "memory.events", "max ");
  560. if (post_high < 0 || post_max < 0)
  561. goto cleanup;
  562. if (pre_high == post_high || pre_max != post_max)
  563. goto cleanup;
  564. ret = KSFT_PASS;
  565. cleanup:
  566. if (fd >= 0)
  567. close(fd);
  568. cg_destroy(memcg);
  569. free(memcg);
  570. return ret;
  571. }
  572. /*
  573. * This test checks that memory.max limits the amount of
  574. * memory which can be consumed by either anonymous memory
  575. * or pagecache.
  576. */
  577. static int test_memcg_max(const char *root)
  578. {
  579. int ret = KSFT_FAIL;
  580. char *memcg;
  581. long current, max;
  582. memcg = cg_name(root, "memcg_test");
  583. if (!memcg)
  584. goto cleanup;
  585. if (cg_create(memcg))
  586. goto cleanup;
  587. if (cg_read_strcmp(memcg, "memory.max", "max\n"))
  588. goto cleanup;
  589. if (cg_write(memcg, "memory.swap.max", "0"))
  590. goto cleanup;
  591. if (cg_write(memcg, "memory.max", "30M"))
  592. goto cleanup;
  593. /* Should be killed by OOM killer */
  594. if (!cg_run(memcg, alloc_anon, (void *)MB(100)))
  595. goto cleanup;
  596. if (cg_run(memcg, alloc_pagecache_max_30M, NULL))
  597. goto cleanup;
  598. current = cg_read_long(memcg, "memory.current");
  599. if (current > MB(30) || !current)
  600. goto cleanup;
  601. max = cg_read_key_long(memcg, "memory.events", "max ");
  602. if (max <= 0)
  603. goto cleanup;
  604. ret = KSFT_PASS;
  605. cleanup:
  606. cg_destroy(memcg);
  607. free(memcg);
  608. return ret;
  609. }
  610. /*
  611. * Reclaim from @memcg until usage reaches @goal by writing to
  612. * memory.reclaim.
  613. *
  614. * This function will return false if the usage is already below the
  615. * goal.
  616. *
  617. * This function assumes that writing to memory.reclaim is the only
  618. * source of change in memory.current (no concurrent allocations or
  619. * reclaim).
  620. *
  621. * This function makes sure memory.reclaim is sane. It will return
  622. * false if memory.reclaim's error codes do not make sense, even if
  623. * the usage goal was satisfied.
  624. */
  625. static bool reclaim_until(const char *memcg, long goal)
  626. {
  627. char buf[64];
  628. int retries, err;
  629. long current, to_reclaim;
  630. bool reclaimed = false;
  631. for (retries = 5; retries > 0; retries--) {
  632. current = cg_read_long(memcg, "memory.current");
  633. if (current < goal || values_close(current, goal, 3))
  634. break;
  635. /* Did memory.reclaim return 0 incorrectly? */
  636. else if (reclaimed)
  637. return false;
  638. to_reclaim = current - goal;
  639. snprintf(buf, sizeof(buf), "%ld", to_reclaim);
  640. err = cg_write(memcg, "memory.reclaim", buf);
  641. if (!err)
  642. reclaimed = true;
  643. else if (err != -EAGAIN)
  644. return false;
  645. }
  646. return reclaimed;
  647. }
  648. /*
  649. * This test checks that memory.reclaim reclaims the given
  650. * amount of memory (from both anon and file, if possible).
  651. */
  652. static int test_memcg_reclaim(const char *root)
  653. {
  654. int ret = KSFT_FAIL;
  655. int fd = -1;
  656. int retries;
  657. char *memcg;
  658. long current, expected_usage;
  659. memcg = cg_name(root, "memcg_test");
  660. if (!memcg)
  661. goto cleanup;
  662. if (cg_create(memcg))
  663. goto cleanup;
  664. current = cg_read_long(memcg, "memory.current");
  665. if (current != 0)
  666. goto cleanup;
  667. fd = get_temp_fd();
  668. if (fd < 0)
  669. goto cleanup;
  670. cg_run_nowait(memcg, alloc_pagecache_50M_noexit, (void *)(long)fd);
  671. /*
  672. * If swap is enabled, try to reclaim from both anon and file, else try
  673. * to reclaim from file only.
  674. */
  675. if (is_swap_enabled()) {
  676. cg_run_nowait(memcg, alloc_anon_noexit, (void *) MB(50));
  677. expected_usage = MB(100);
  678. } else
  679. expected_usage = MB(50);
  680. /*
  681. * Wait until current usage reaches the expected usage (or we run out of
  682. * retries).
  683. */
  684. retries = 5;
  685. while (!values_close(cg_read_long(memcg, "memory.current"),
  686. expected_usage, 10)) {
  687. if (retries--) {
  688. sleep(1);
  689. continue;
  690. } else {
  691. fprintf(stderr,
  692. "failed to allocate %ld for memcg reclaim test\n",
  693. expected_usage);
  694. goto cleanup;
  695. }
  696. }
  697. /*
  698. * Reclaim until current reaches 30M, this makes sure we hit both anon
  699. * and file if swap is enabled.
  700. */
  701. if (!reclaim_until(memcg, MB(30)))
  702. goto cleanup;
  703. ret = KSFT_PASS;
  704. cleanup:
  705. cg_destroy(memcg);
  706. free(memcg);
  707. close(fd);
  708. return ret;
  709. }
  710. static int alloc_anon_50M_check_swap(const char *cgroup, void *arg)
  711. {
  712. long mem_max = (long)arg;
  713. size_t size = MB(50);
  714. char *buf, *ptr;
  715. long mem_current, swap_current;
  716. int ret = -1;
  717. buf = malloc(size);
  718. if (buf == NULL) {
  719. fprintf(stderr, "malloc() failed\n");
  720. return -1;
  721. }
  722. for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE)
  723. *ptr = 0;
  724. mem_current = cg_read_long(cgroup, "memory.current");
  725. if (!mem_current || !values_close(mem_current, mem_max, 3))
  726. goto cleanup;
  727. swap_current = cg_read_long(cgroup, "memory.swap.current");
  728. if (!swap_current ||
  729. !values_close(mem_current + swap_current, size, 3))
  730. goto cleanup;
  731. ret = 0;
  732. cleanup:
  733. free(buf);
  734. return ret;
  735. }
  736. /*
  737. * This test checks that memory.swap.max limits the amount of
  738. * anonymous memory which can be swapped out. Additionally, it verifies that
  739. * memory.swap.peak reflects the high watermark and can be reset.
  740. */
  741. static int test_memcg_swap_max_peak(const char *root)
  742. {
  743. int ret = KSFT_FAIL;
  744. char *memcg;
  745. long max, peak;
  746. struct stat ss;
  747. int swap_peak_fd = -1, mem_peak_fd = -1;
  748. /* any non-empty string resets */
  749. static const char reset_string[] = "foobarbaz";
  750. if (!is_swap_enabled())
  751. return KSFT_SKIP;
  752. memcg = cg_name(root, "memcg_test");
  753. if (!memcg)
  754. goto cleanup;
  755. if (cg_create(memcg))
  756. goto cleanup;
  757. if (cg_read_long(memcg, "memory.swap.current")) {
  758. ret = KSFT_SKIP;
  759. goto cleanup;
  760. }
  761. swap_peak_fd = cg_open(memcg, "memory.swap.peak",
  762. O_RDWR | O_APPEND | O_CLOEXEC);
  763. if (swap_peak_fd == -1) {
  764. if (errno == ENOENT)
  765. ret = KSFT_SKIP;
  766. goto cleanup;
  767. }
  768. /*
  769. * Before we try to use memory.swap.peak's fd, try to figure out
  770. * whether this kernel supports writing to that file in the first
  771. * place. (by checking the writable bit on the file's st_mode)
  772. */
  773. if (fstat(swap_peak_fd, &ss))
  774. goto cleanup;
  775. if ((ss.st_mode & S_IWUSR) == 0) {
  776. ret = KSFT_SKIP;
  777. goto cleanup;
  778. }
  779. mem_peak_fd = cg_open(memcg, "memory.peak", O_RDWR | O_APPEND | O_CLOEXEC);
  780. if (mem_peak_fd == -1)
  781. goto cleanup;
  782. if (cg_read_long(memcg, "memory.swap.peak"))
  783. goto cleanup;
  784. if (cg_read_long_fd(swap_peak_fd))
  785. goto cleanup;
  786. /* switch the swap and mem fds into local-peak tracking mode*/
  787. int peak_reset = write(swap_peak_fd, reset_string, sizeof(reset_string));
  788. if (peak_reset != sizeof(reset_string))
  789. goto cleanup;
  790. if (cg_read_long_fd(swap_peak_fd))
  791. goto cleanup;
  792. if (cg_read_long(memcg, "memory.peak"))
  793. goto cleanup;
  794. if (cg_read_long_fd(mem_peak_fd))
  795. goto cleanup;
  796. peak_reset = write(mem_peak_fd, reset_string, sizeof(reset_string));
  797. if (peak_reset != sizeof(reset_string))
  798. goto cleanup;
  799. if (cg_read_long_fd(mem_peak_fd))
  800. goto cleanup;
  801. if (cg_read_strcmp(memcg, "memory.max", "max\n"))
  802. goto cleanup;
  803. if (cg_read_strcmp(memcg, "memory.swap.max", "max\n"))
  804. goto cleanup;
  805. if (cg_write(memcg, "memory.swap.max", "30M"))
  806. goto cleanup;
  807. if (cg_write(memcg, "memory.max", "30M"))
  808. goto cleanup;
  809. /* Should be killed by OOM killer */
  810. if (!cg_run(memcg, alloc_anon, (void *)MB(100)))
  811. goto cleanup;
  812. if (cg_read_key_long(memcg, "memory.events", "oom ") != 1)
  813. goto cleanup;
  814. if (cg_read_key_long(memcg, "memory.events", "oom_kill ") != 1)
  815. goto cleanup;
  816. peak = cg_read_long(memcg, "memory.peak");
  817. if (peak < MB(29))
  818. goto cleanup;
  819. peak = cg_read_long(memcg, "memory.swap.peak");
  820. if (peak < MB(29))
  821. goto cleanup;
  822. peak = cg_read_long_fd(mem_peak_fd);
  823. if (peak < MB(29))
  824. goto cleanup;
  825. peak = cg_read_long_fd(swap_peak_fd);
  826. if (peak < MB(29))
  827. goto cleanup;
  828. /*
  829. * open, reset and close the peak swap on another FD to make sure
  830. * multiple extant fds don't corrupt the linked-list
  831. */
  832. peak_reset = cg_write(memcg, "memory.swap.peak", (char *)reset_string);
  833. if (peak_reset)
  834. goto cleanup;
  835. peak_reset = cg_write(memcg, "memory.peak", (char *)reset_string);
  836. if (peak_reset)
  837. goto cleanup;
  838. /* actually reset on the fds */
  839. peak_reset = write(swap_peak_fd, reset_string, sizeof(reset_string));
  840. if (peak_reset != sizeof(reset_string))
  841. goto cleanup;
  842. peak_reset = write(mem_peak_fd, reset_string, sizeof(reset_string));
  843. if (peak_reset != sizeof(reset_string))
  844. goto cleanup;
  845. peak = cg_read_long_fd(swap_peak_fd);
  846. if (peak > MB(10))
  847. goto cleanup;
  848. /*
  849. * The cgroup is now empty, but there may be a page or two associated
  850. * with the open FD accounted to it.
  851. */
  852. peak = cg_read_long_fd(mem_peak_fd);
  853. if (peak > MB(1))
  854. goto cleanup;
  855. if (cg_read_long(memcg, "memory.peak") < MB(29))
  856. goto cleanup;
  857. if (cg_read_long(memcg, "memory.swap.peak") < MB(29))
  858. goto cleanup;
  859. if (cg_run(memcg, alloc_anon_50M_check_swap, (void *)MB(30)))
  860. goto cleanup;
  861. max = cg_read_key_long(memcg, "memory.events", "max ");
  862. if (max <= 0)
  863. goto cleanup;
  864. peak = cg_read_long(memcg, "memory.peak");
  865. if (peak < MB(29))
  866. goto cleanup;
  867. peak = cg_read_long(memcg, "memory.swap.peak");
  868. if (peak < MB(29))
  869. goto cleanup;
  870. peak = cg_read_long_fd(mem_peak_fd);
  871. if (peak < MB(29))
  872. goto cleanup;
  873. peak = cg_read_long_fd(swap_peak_fd);
  874. if (peak < MB(19))
  875. goto cleanup;
  876. ret = KSFT_PASS;
  877. cleanup:
  878. if (mem_peak_fd != -1 && close(mem_peak_fd))
  879. ret = KSFT_FAIL;
  880. if (swap_peak_fd != -1 && close(swap_peak_fd))
  881. ret = KSFT_FAIL;
  882. cg_destroy(memcg);
  883. free(memcg);
  884. return ret;
  885. }
  886. /*
  887. * This test disables swapping and tries to allocate anonymous memory
  888. * up to OOM. Then it checks for oom and oom_kill events in
  889. * memory.events.
  890. */
  891. static int test_memcg_oom_events(const char *root)
  892. {
  893. int ret = KSFT_FAIL;
  894. char *memcg;
  895. memcg = cg_name(root, "memcg_test");
  896. if (!memcg)
  897. goto cleanup;
  898. if (cg_create(memcg))
  899. goto cleanup;
  900. if (cg_write(memcg, "memory.max", "30M"))
  901. goto cleanup;
  902. if (cg_write(memcg, "memory.swap.max", "0"))
  903. goto cleanup;
  904. if (!cg_run(memcg, alloc_anon, (void *)MB(100)))
  905. goto cleanup;
  906. if (cg_read_strcmp(memcg, "cgroup.procs", ""))
  907. goto cleanup;
  908. if (cg_read_key_long(memcg, "memory.events", "oom ") != 1)
  909. goto cleanup;
  910. if (cg_read_key_long(memcg, "memory.events", "oom_kill ") != 1)
  911. goto cleanup;
  912. ret = KSFT_PASS;
  913. cleanup:
  914. cg_destroy(memcg);
  915. free(memcg);
  916. return ret;
  917. }
  918. struct tcp_server_args {
  919. unsigned short port;
  920. int ctl[2];
  921. };
  922. static int tcp_server(const char *cgroup, void *arg)
  923. {
  924. struct tcp_server_args *srv_args = arg;
  925. struct sockaddr_in6 saddr = { 0 };
  926. socklen_t slen = sizeof(saddr);
  927. int sk, client_sk, ctl_fd, yes = 1, ret = -1;
  928. close(srv_args->ctl[0]);
  929. ctl_fd = srv_args->ctl[1];
  930. saddr.sin6_family = AF_INET6;
  931. saddr.sin6_addr = in6addr_any;
  932. saddr.sin6_port = htons(srv_args->port);
  933. sk = socket(AF_INET6, SOCK_STREAM, 0);
  934. if (sk < 0)
  935. return ret;
  936. if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
  937. goto cleanup;
  938. if (bind(sk, (struct sockaddr *)&saddr, slen)) {
  939. write(ctl_fd, &errno, sizeof(errno));
  940. goto cleanup;
  941. }
  942. if (listen(sk, 1))
  943. goto cleanup;
  944. ret = 0;
  945. if (write(ctl_fd, &ret, sizeof(ret)) != sizeof(ret)) {
  946. ret = -1;
  947. goto cleanup;
  948. }
  949. client_sk = accept(sk, NULL, NULL);
  950. if (client_sk < 0)
  951. goto cleanup;
  952. ret = -1;
  953. for (;;) {
  954. uint8_t buf[0x100000];
  955. if (write(client_sk, buf, sizeof(buf)) <= 0) {
  956. if (errno == ECONNRESET)
  957. ret = 0;
  958. break;
  959. }
  960. }
  961. close(client_sk);
  962. cleanup:
  963. close(sk);
  964. return ret;
  965. }
  966. static int tcp_client(const char *cgroup, unsigned short port)
  967. {
  968. const char server[] = "localhost";
  969. struct addrinfo *ai;
  970. char servport[6];
  971. int retries = 0x10; /* nice round number */
  972. int sk, ret;
  973. long allocated;
  974. allocated = cg_read_long(cgroup, "memory.current");
  975. snprintf(servport, sizeof(servport), "%hd", port);
  976. ret = getaddrinfo(server, servport, NULL, &ai);
  977. if (ret)
  978. return ret;
  979. sk = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
  980. if (sk < 0)
  981. goto free_ainfo;
  982. ret = connect(sk, ai->ai_addr, ai->ai_addrlen);
  983. if (ret < 0)
  984. goto close_sk;
  985. ret = KSFT_FAIL;
  986. while (retries--) {
  987. uint8_t buf[0x100000];
  988. long current, sock;
  989. if (read(sk, buf, sizeof(buf)) <= 0)
  990. goto close_sk;
  991. current = cg_read_long(cgroup, "memory.current");
  992. sock = cg_read_key_long(cgroup, "memory.stat", "sock ");
  993. if (current < 0 || sock < 0)
  994. goto close_sk;
  995. /* exclude the memory not related to socket connection */
  996. if (values_close(current - allocated, sock, 10)) {
  997. ret = KSFT_PASS;
  998. break;
  999. }
  1000. }
  1001. close_sk:
  1002. close(sk);
  1003. free_ainfo:
  1004. freeaddrinfo(ai);
  1005. return ret;
  1006. }
  1007. /*
  1008. * This test checks socket memory accounting.
  1009. * The test forks a TCP server listens on a random port between 1000
  1010. * and 61000. Once it gets a client connection, it starts writing to
  1011. * its socket.
  1012. * The TCP client interleaves reads from the socket with check whether
  1013. * memory.current and memory.stat.sock are similar.
  1014. */
  1015. static int test_memcg_sock(const char *root)
  1016. {
  1017. int bind_retries = 5, ret = KSFT_FAIL, pid, err;
  1018. unsigned short port;
  1019. char *memcg;
  1020. memcg = cg_name(root, "memcg_test");
  1021. if (!memcg)
  1022. goto cleanup;
  1023. if (cg_create(memcg))
  1024. goto cleanup;
  1025. while (bind_retries--) {
  1026. struct tcp_server_args args;
  1027. if (pipe(args.ctl))
  1028. goto cleanup;
  1029. port = args.port = 1000 + rand() % 60000;
  1030. pid = cg_run_nowait(memcg, tcp_server, &args);
  1031. if (pid < 0)
  1032. goto cleanup;
  1033. close(args.ctl[1]);
  1034. if (read(args.ctl[0], &err, sizeof(err)) != sizeof(err))
  1035. goto cleanup;
  1036. close(args.ctl[0]);
  1037. if (!err)
  1038. break;
  1039. if (err != EADDRINUSE)
  1040. goto cleanup;
  1041. waitpid(pid, NULL, 0);
  1042. }
  1043. if (err == EADDRINUSE) {
  1044. ret = KSFT_SKIP;
  1045. goto cleanup;
  1046. }
  1047. if (tcp_client(memcg, port) != KSFT_PASS)
  1048. goto cleanup;
  1049. waitpid(pid, &err, 0);
  1050. if (WEXITSTATUS(err))
  1051. goto cleanup;
  1052. if (cg_read_long(memcg, "memory.current") < 0)
  1053. goto cleanup;
  1054. if (cg_read_key_long(memcg, "memory.stat", "sock "))
  1055. goto cleanup;
  1056. ret = KSFT_PASS;
  1057. cleanup:
  1058. cg_destroy(memcg);
  1059. free(memcg);
  1060. return ret;
  1061. }
  1062. /*
  1063. * This test disables swapping and tries to allocate anonymous memory
  1064. * up to OOM with memory.group.oom set. Then it checks that all
  1065. * processes in the leaf were killed. It also checks that oom_events
  1066. * were propagated to the parent level.
  1067. */
  1068. static int test_memcg_oom_group_leaf_events(const char *root)
  1069. {
  1070. int ret = KSFT_FAIL;
  1071. char *parent, *child;
  1072. long parent_oom_events;
  1073. parent = cg_name(root, "memcg_test_0");
  1074. child = cg_name(root, "memcg_test_0/memcg_test_1");
  1075. if (!parent || !child)
  1076. goto cleanup;
  1077. if (cg_create(parent))
  1078. goto cleanup;
  1079. if (cg_create(child))
  1080. goto cleanup;
  1081. if (cg_write(parent, "cgroup.subtree_control", "+memory"))
  1082. goto cleanup;
  1083. if (cg_write(child, "memory.max", "50M"))
  1084. goto cleanup;
  1085. if (cg_write(child, "memory.swap.max", "0"))
  1086. goto cleanup;
  1087. if (cg_write(child, "memory.oom.group", "1"))
  1088. goto cleanup;
  1089. cg_run_nowait(parent, alloc_anon_noexit, (void *) MB(60));
  1090. cg_run_nowait(child, alloc_anon_noexit, (void *) MB(1));
  1091. cg_run_nowait(child, alloc_anon_noexit, (void *) MB(1));
  1092. if (!cg_run(child, alloc_anon, (void *)MB(100)))
  1093. goto cleanup;
  1094. if (cg_test_proc_killed(child))
  1095. goto cleanup;
  1096. if (cg_read_key_long(child, "memory.events", "oom_kill ") <= 0)
  1097. goto cleanup;
  1098. parent_oom_events = cg_read_key_long(
  1099. parent, "memory.events", "oom_kill ");
  1100. /*
  1101. * If memory_localevents is not enabled (the default), the parent should
  1102. * count OOM events in its children groups. Otherwise, it should not
  1103. * have observed any events.
  1104. */
  1105. if (has_localevents && parent_oom_events != 0)
  1106. goto cleanup;
  1107. else if (!has_localevents && parent_oom_events <= 0)
  1108. goto cleanup;
  1109. ret = KSFT_PASS;
  1110. cleanup:
  1111. if (child)
  1112. cg_destroy(child);
  1113. if (parent)
  1114. cg_destroy(parent);
  1115. free(child);
  1116. free(parent);
  1117. return ret;
  1118. }
  1119. /*
  1120. * This test disables swapping and tries to allocate anonymous memory
  1121. * up to OOM with memory.group.oom set. Then it checks that all
  1122. * processes in the parent and leaf were killed.
  1123. */
  1124. static int test_memcg_oom_group_parent_events(const char *root)
  1125. {
  1126. int ret = KSFT_FAIL;
  1127. char *parent, *child;
  1128. parent = cg_name(root, "memcg_test_0");
  1129. child = cg_name(root, "memcg_test_0/memcg_test_1");
  1130. if (!parent || !child)
  1131. goto cleanup;
  1132. if (cg_create(parent))
  1133. goto cleanup;
  1134. if (cg_create(child))
  1135. goto cleanup;
  1136. if (cg_write(parent, "memory.max", "80M"))
  1137. goto cleanup;
  1138. if (cg_write(parent, "memory.swap.max", "0"))
  1139. goto cleanup;
  1140. if (cg_write(parent, "memory.oom.group", "1"))
  1141. goto cleanup;
  1142. cg_run_nowait(parent, alloc_anon_noexit, (void *) MB(60));
  1143. cg_run_nowait(child, alloc_anon_noexit, (void *) MB(1));
  1144. cg_run_nowait(child, alloc_anon_noexit, (void *) MB(1));
  1145. if (!cg_run(child, alloc_anon, (void *)MB(100)))
  1146. goto cleanup;
  1147. if (cg_test_proc_killed(child))
  1148. goto cleanup;
  1149. if (cg_test_proc_killed(parent))
  1150. goto cleanup;
  1151. ret = KSFT_PASS;
  1152. cleanup:
  1153. if (child)
  1154. cg_destroy(child);
  1155. if (parent)
  1156. cg_destroy(parent);
  1157. free(child);
  1158. free(parent);
  1159. return ret;
  1160. }
  1161. /*
  1162. * This test disables swapping and tries to allocate anonymous memory
  1163. * up to OOM with memory.group.oom set. Then it checks that all
  1164. * processes were killed except those set with OOM_SCORE_ADJ_MIN
  1165. */
  1166. static int test_memcg_oom_group_score_events(const char *root)
  1167. {
  1168. int ret = KSFT_FAIL;
  1169. char *memcg;
  1170. int safe_pid;
  1171. memcg = cg_name(root, "memcg_test_0");
  1172. if (!memcg)
  1173. goto cleanup;
  1174. if (cg_create(memcg))
  1175. goto cleanup;
  1176. if (cg_write(memcg, "memory.max", "50M"))
  1177. goto cleanup;
  1178. if (cg_write(memcg, "memory.swap.max", "0"))
  1179. goto cleanup;
  1180. if (cg_write(memcg, "memory.oom.group", "1"))
  1181. goto cleanup;
  1182. safe_pid = cg_run_nowait(memcg, alloc_anon_noexit, (void *) MB(1));
  1183. if (set_oom_adj_score(safe_pid, OOM_SCORE_ADJ_MIN))
  1184. goto cleanup;
  1185. cg_run_nowait(memcg, alloc_anon_noexit, (void *) MB(1));
  1186. if (!cg_run(memcg, alloc_anon, (void *)MB(100)))
  1187. goto cleanup;
  1188. if (cg_read_key_long(memcg, "memory.events", "oom_kill ") != 3)
  1189. goto cleanup;
  1190. if (kill(safe_pid, SIGKILL))
  1191. goto cleanup;
  1192. ret = KSFT_PASS;
  1193. cleanup:
  1194. if (memcg)
  1195. cg_destroy(memcg);
  1196. free(memcg);
  1197. return ret;
  1198. }
  1199. #define T(x) { x, #x }
  1200. struct memcg_test {
  1201. int (*fn)(const char *root);
  1202. const char *name;
  1203. } tests[] = {
  1204. T(test_memcg_subtree_control),
  1205. T(test_memcg_current_peak),
  1206. T(test_memcg_min),
  1207. T(test_memcg_low),
  1208. T(test_memcg_high),
  1209. T(test_memcg_high_sync),
  1210. T(test_memcg_max),
  1211. T(test_memcg_reclaim),
  1212. T(test_memcg_oom_events),
  1213. T(test_memcg_swap_max_peak),
  1214. T(test_memcg_sock),
  1215. T(test_memcg_oom_group_leaf_events),
  1216. T(test_memcg_oom_group_parent_events),
  1217. T(test_memcg_oom_group_score_events),
  1218. };
  1219. #undef T
  1220. int main(int argc, char **argv)
  1221. {
  1222. char root[PATH_MAX];
  1223. int i, proc_status, ret = EXIT_SUCCESS;
  1224. if (cg_find_unified_root(root, sizeof(root), NULL))
  1225. ksft_exit_skip("cgroup v2 isn't mounted\n");
  1226. /*
  1227. * Check that memory controller is available:
  1228. * memory is listed in cgroup.controllers
  1229. */
  1230. if (cg_read_strstr(root, "cgroup.controllers", "memory"))
  1231. ksft_exit_skip("memory controller isn't available\n");
  1232. if (cg_read_strstr(root, "cgroup.subtree_control", "memory"))
  1233. if (cg_write(root, "cgroup.subtree_control", "+memory"))
  1234. ksft_exit_skip("Failed to set memory controller\n");
  1235. proc_status = proc_mount_contains("memory_recursiveprot");
  1236. if (proc_status < 0)
  1237. ksft_exit_skip("Failed to query cgroup mount option\n");
  1238. has_recursiveprot = proc_status;
  1239. proc_status = proc_mount_contains("memory_localevents");
  1240. if (proc_status < 0)
  1241. ksft_exit_skip("Failed to query cgroup mount option\n");
  1242. has_localevents = proc_status;
  1243. for (i = 0; i < ARRAY_SIZE(tests); i++) {
  1244. switch (tests[i].fn(root)) {
  1245. case KSFT_PASS:
  1246. ksft_test_result_pass("%s\n", tests[i].name);
  1247. break;
  1248. case KSFT_SKIP:
  1249. ksft_test_result_skip("%s\n", tests[i].name);
  1250. break;
  1251. default:
  1252. ret = EXIT_FAILURE;
  1253. ksft_test_result_fail("%s\n", tests[i].name);
  1254. break;
  1255. }
  1256. }
  1257. return ret;
  1258. }