Path: blob/master/tools/testing/selftests/cgroup/test_freezer.c
29270 views
/* SPDX-License-Identifier: GPL-2.0 */1#include <stdbool.h>2#include <linux/limits.h>3#include <sys/ptrace.h>4#include <sys/types.h>5#include <sys/mman.h>6#include <unistd.h>7#include <stdio.h>8#include <errno.h>9#include <stdlib.h>10#include <string.h>11#include <sys/wait.h>1213#include "../kselftest.h"14#include "cgroup_util.h"1516#define DEBUG17#ifdef DEBUG18#define debug(args...) fprintf(stderr, args)19#else20#define debug(args...)21#endif2223/*24* Check if the cgroup is frozen by looking at the cgroup.events::frozen value.25*/26static int cg_check_frozen(const char *cgroup, bool frozen)27{28if (frozen) {29if (cg_read_strstr(cgroup, "cgroup.events", "frozen 1") != 0) {30debug("Cgroup %s isn't frozen\n", cgroup);31return -1;32}33} else {34/*35* Check the cgroup.events::frozen value.36*/37if (cg_read_strstr(cgroup, "cgroup.events", "frozen 0") != 0) {38debug("Cgroup %s is frozen\n", cgroup);39return -1;40}41}4243return 0;44}4546/*47* Freeze the given cgroup.48*/49static int cg_freeze_nowait(const char *cgroup, bool freeze)50{51return cg_write(cgroup, "cgroup.freeze", freeze ? "1" : "0");52}5354/*55* Attach a task to the given cgroup and wait for a cgroup frozen event.56* All transient events (e.g. populated) are ignored.57*/58static int cg_enter_and_wait_for_frozen(const char *cgroup, int pid,59bool frozen)60{61int fd, ret = -1;62int attempts;6364fd = cg_prepare_for_wait(cgroup);65if (fd < 0)66return fd;6768ret = cg_enter(cgroup, pid);69if (ret)70goto out;7172for (attempts = 0; attempts < 10; attempts++) {73ret = cg_wait_for(fd);74if (ret)75break;7677ret = cg_check_frozen(cgroup, frozen);78if (ret)79continue;80}8182out:83close(fd);84return ret;85}8687/*88* Freeze the given cgroup and wait for the inotify signal.89* If there are no events in 10 seconds, treat this as an error.90* Then check that the cgroup is in the desired state.91*/92static int cg_freeze_wait(const char *cgroup, bool freeze)93{94int fd, ret = -1;9596fd = cg_prepare_for_wait(cgroup);97if (fd < 0)98return fd;99100ret = cg_freeze_nowait(cgroup, freeze);101if (ret) {102debug("Error: cg_freeze_nowait() failed\n");103goto out;104}105106ret = cg_wait_for(fd);107if (ret)108goto out;109110ret = cg_check_frozen(cgroup, freeze);111out:112close(fd);113return ret;114}115116/*117* A simple process running in a sleep loop until being118* re-parented.119*/120static int child_fn(const char *cgroup, void *arg)121{122int ppid = getppid();123124while (getppid() == ppid)125usleep(1000);126127return getppid() == ppid;128}129130/*131* A simple test for the cgroup freezer: populated the cgroup with 100132* running processes and freeze it. Then unfreeze it. Then it kills all133* processes and destroys the cgroup.134*/135static int test_cgfreezer_simple(const char *root)136{137int ret = KSFT_FAIL;138char *cgroup = NULL;139int i;140141cgroup = cg_name(root, "cg_test_simple");142if (!cgroup)143goto cleanup;144145if (cg_create(cgroup))146goto cleanup;147148for (i = 0; i < 100; i++)149cg_run_nowait(cgroup, child_fn, NULL);150151if (cg_wait_for_proc_count(cgroup, 100))152goto cleanup;153154if (cg_check_frozen(cgroup, false))155goto cleanup;156157if (cg_freeze_wait(cgroup, true))158goto cleanup;159160if (cg_freeze_wait(cgroup, false))161goto cleanup;162163ret = KSFT_PASS;164165cleanup:166if (cgroup)167cg_destroy(cgroup);168free(cgroup);169return ret;170}171172/*173* The test creates the following hierarchy:174* A175* / / \ \176* B E I K177* /\ |178* C D F179* |180* G181* |182* H183*184* with a process in C, H and 3 processes in K.185* Then it tries to freeze and unfreeze the whole tree.186*/187static int test_cgfreezer_tree(const char *root)188{189char *cgroup[10] = {0};190int ret = KSFT_FAIL;191int i;192193cgroup[0] = cg_name(root, "cg_test_tree_A");194if (!cgroup[0])195goto cleanup;196197cgroup[1] = cg_name(cgroup[0], "B");198if (!cgroup[1])199goto cleanup;200201cgroup[2] = cg_name(cgroup[1], "C");202if (!cgroup[2])203goto cleanup;204205cgroup[3] = cg_name(cgroup[1], "D");206if (!cgroup[3])207goto cleanup;208209cgroup[4] = cg_name(cgroup[0], "E");210if (!cgroup[4])211goto cleanup;212213cgroup[5] = cg_name(cgroup[4], "F");214if (!cgroup[5])215goto cleanup;216217cgroup[6] = cg_name(cgroup[5], "G");218if (!cgroup[6])219goto cleanup;220221cgroup[7] = cg_name(cgroup[6], "H");222if (!cgroup[7])223goto cleanup;224225cgroup[8] = cg_name(cgroup[0], "I");226if (!cgroup[8])227goto cleanup;228229cgroup[9] = cg_name(cgroup[0], "K");230if (!cgroup[9])231goto cleanup;232233for (i = 0; i < 10; i++)234if (cg_create(cgroup[i]))235goto cleanup;236237cg_run_nowait(cgroup[2], child_fn, NULL);238cg_run_nowait(cgroup[7], child_fn, NULL);239cg_run_nowait(cgroup[9], child_fn, NULL);240cg_run_nowait(cgroup[9], child_fn, NULL);241cg_run_nowait(cgroup[9], child_fn, NULL);242243/*244* Wait until all child processes will enter245* corresponding cgroups.246*/247248if (cg_wait_for_proc_count(cgroup[2], 1) ||249cg_wait_for_proc_count(cgroup[7], 1) ||250cg_wait_for_proc_count(cgroup[9], 3))251goto cleanup;252253/*254* Freeze B.255*/256if (cg_freeze_wait(cgroup[1], true))257goto cleanup;258259/*260* Freeze F.261*/262if (cg_freeze_wait(cgroup[5], true))263goto cleanup;264265/*266* Freeze G.267*/268if (cg_freeze_wait(cgroup[6], true))269goto cleanup;270271/*272* Check that A and E are not frozen.273*/274if (cg_check_frozen(cgroup[0], false))275goto cleanup;276277if (cg_check_frozen(cgroup[4], false))278goto cleanup;279280/*281* Freeze A. Check that A, B and E are frozen.282*/283if (cg_freeze_wait(cgroup[0], true))284goto cleanup;285286if (cg_check_frozen(cgroup[1], true))287goto cleanup;288289if (cg_check_frozen(cgroup[4], true))290goto cleanup;291292/*293* Unfreeze B, F and G294*/295if (cg_freeze_nowait(cgroup[1], false))296goto cleanup;297298if (cg_freeze_nowait(cgroup[5], false))299goto cleanup;300301if (cg_freeze_nowait(cgroup[6], false))302goto cleanup;303304/*305* Check that C and H are still frozen.306*/307if (cg_check_frozen(cgroup[2], true))308goto cleanup;309310if (cg_check_frozen(cgroup[7], true))311goto cleanup;312313/*314* Unfreeze A. Check that A, C and K are not frozen.315*/316if (cg_freeze_wait(cgroup[0], false))317goto cleanup;318319if (cg_check_frozen(cgroup[2], false))320goto cleanup;321322if (cg_check_frozen(cgroup[9], false))323goto cleanup;324325ret = KSFT_PASS;326327cleanup:328for (i = 9; i >= 0 && cgroup[i]; i--) {329cg_destroy(cgroup[i]);330free(cgroup[i]);331}332333return ret;334}335336/*337* A fork bomb emulator.338*/339static int forkbomb_fn(const char *cgroup, void *arg)340{341int ppid;342343fork();344fork();345346ppid = getppid();347348while (getppid() == ppid)349usleep(1000);350351return getppid() == ppid;352}353354/*355* The test runs a fork bomb in a cgroup and tries to freeze it.356* Then it kills all processes and checks that cgroup isn't populated357* anymore.358*/359static int test_cgfreezer_forkbomb(const char *root)360{361int ret = KSFT_FAIL;362char *cgroup = NULL;363364cgroup = cg_name(root, "cg_forkbomb_test");365if (!cgroup)366goto cleanup;367368if (cg_create(cgroup))369goto cleanup;370371cg_run_nowait(cgroup, forkbomb_fn, NULL);372373usleep(100000);374375if (cg_freeze_wait(cgroup, true))376goto cleanup;377378if (cg_killall(cgroup))379goto cleanup;380381if (cg_wait_for_proc_count(cgroup, 0))382goto cleanup;383384ret = KSFT_PASS;385386cleanup:387if (cgroup)388cg_destroy(cgroup);389free(cgroup);390return ret;391}392393/*394* The test creates a cgroups and freezes it. Then it creates a child cgroup395* and populates it with a task. After that it checks that the child cgroup396* is frozen and the parent cgroup remains frozen too.397*/398static int test_cgfreezer_mkdir(const char *root)399{400int ret = KSFT_FAIL;401char *parent, *child = NULL;402int pid;403404parent = cg_name(root, "cg_test_mkdir_A");405if (!parent)406goto cleanup;407408child = cg_name(parent, "cg_test_mkdir_B");409if (!child)410goto cleanup;411412if (cg_create(parent))413goto cleanup;414415if (cg_freeze_wait(parent, true))416goto cleanup;417418if (cg_create(child))419goto cleanup;420421pid = cg_run_nowait(child, child_fn, NULL);422if (pid < 0)423goto cleanup;424425if (cg_wait_for_proc_count(child, 1))426goto cleanup;427428if (cg_check_frozen(child, true))429goto cleanup;430431if (cg_check_frozen(parent, true))432goto cleanup;433434ret = KSFT_PASS;435436cleanup:437if (child)438cg_destroy(child);439free(child);440if (parent)441cg_destroy(parent);442free(parent);443return ret;444}445446/*447* The test creates two nested cgroups, freezes the parent448* and removes the child. Then it checks that the parent cgroup449* remains frozen and it's possible to create a new child450* without unfreezing. The new child is frozen too.451*/452static int test_cgfreezer_rmdir(const char *root)453{454int ret = KSFT_FAIL;455char *parent, *child = NULL;456457parent = cg_name(root, "cg_test_rmdir_A");458if (!parent)459goto cleanup;460461child = cg_name(parent, "cg_test_rmdir_B");462if (!child)463goto cleanup;464465if (cg_create(parent))466goto cleanup;467468if (cg_create(child))469goto cleanup;470471if (cg_freeze_wait(parent, true))472goto cleanup;473474if (cg_destroy(child))475goto cleanup;476477if (cg_check_frozen(parent, true))478goto cleanup;479480if (cg_create(child))481goto cleanup;482483if (cg_check_frozen(child, true))484goto cleanup;485486ret = KSFT_PASS;487488cleanup:489if (child)490cg_destroy(child);491free(child);492if (parent)493cg_destroy(parent);494free(parent);495return ret;496}497498/*499* The test creates two cgroups: A and B, runs a process in A500* and performs several migrations:501* 1) A (running) -> B (frozen)502* 2) B (frozen) -> A (running)503* 3) A (frozen) -> B (frozen)504*505* On each step it checks the actual state of both cgroups.506*/507static int test_cgfreezer_migrate(const char *root)508{509int ret = KSFT_FAIL;510char *cgroup[2] = {0};511int pid;512513cgroup[0] = cg_name(root, "cg_test_migrate_A");514if (!cgroup[0])515goto cleanup;516517cgroup[1] = cg_name(root, "cg_test_migrate_B");518if (!cgroup[1])519goto cleanup;520521if (cg_create(cgroup[0]))522goto cleanup;523524if (cg_create(cgroup[1]))525goto cleanup;526527pid = cg_run_nowait(cgroup[0], child_fn, NULL);528if (pid < 0)529goto cleanup;530531if (cg_wait_for_proc_count(cgroup[0], 1))532goto cleanup;533534/*535* Migrate from A (running) to B (frozen)536*/537if (cg_freeze_wait(cgroup[1], true))538goto cleanup;539540if (cg_enter_and_wait_for_frozen(cgroup[1], pid, true))541goto cleanup;542543if (cg_check_frozen(cgroup[0], false))544goto cleanup;545546/*547* Migrate from B (frozen) to A (running)548*/549if (cg_enter_and_wait_for_frozen(cgroup[0], pid, false))550goto cleanup;551552if (cg_check_frozen(cgroup[1], true))553goto cleanup;554555/*556* Migrate from A (frozen) to B (frozen)557*/558if (cg_freeze_wait(cgroup[0], true))559goto cleanup;560561if (cg_enter_and_wait_for_frozen(cgroup[1], pid, true))562goto cleanup;563564if (cg_check_frozen(cgroup[0], true))565goto cleanup;566567ret = KSFT_PASS;568569cleanup:570if (cgroup[0])571cg_destroy(cgroup[0]);572free(cgroup[0]);573if (cgroup[1])574cg_destroy(cgroup[1]);575free(cgroup[1]);576return ret;577}578579/*580* The test checks that ptrace works with a tracing process in a frozen cgroup.581*/582static int test_cgfreezer_ptrace(const char *root)583{584int ret = KSFT_FAIL;585char *cgroup = NULL;586siginfo_t siginfo;587int pid;588589cgroup = cg_name(root, "cg_test_ptrace");590if (!cgroup)591goto cleanup;592593if (cg_create(cgroup))594goto cleanup;595596pid = cg_run_nowait(cgroup, child_fn, NULL);597if (pid < 0)598goto cleanup;599600if (cg_wait_for_proc_count(cgroup, 1))601goto cleanup;602603if (cg_freeze_wait(cgroup, true))604goto cleanup;605606if (ptrace(PTRACE_SEIZE, pid, NULL, NULL))607goto cleanup;608609if (ptrace(PTRACE_INTERRUPT, pid, NULL, NULL))610goto cleanup;611612waitpid(pid, NULL, 0);613614/*615* Cgroup has to remain frozen, however the test task616* is in traced state.617*/618if (cg_check_frozen(cgroup, true))619goto cleanup;620621if (ptrace(PTRACE_GETSIGINFO, pid, NULL, &siginfo))622goto cleanup;623624if (ptrace(PTRACE_DETACH, pid, NULL, NULL))625goto cleanup;626627if (cg_check_frozen(cgroup, true))628goto cleanup;629630ret = KSFT_PASS;631632cleanup:633if (cgroup)634cg_destroy(cgroup);635free(cgroup);636return ret;637}638639/*640* Check if the process is stopped.641*/642static int proc_check_stopped(int pid)643{644char buf[PAGE_SIZE];645int len;646647len = proc_read_text(pid, 0, "stat", buf, sizeof(buf));648if (len == -1) {649debug("Can't get %d stat\n", pid);650return -1;651}652653if (strstr(buf, "(test_freezer) T ") == NULL) {654debug("Process %d in the unexpected state: %s\n", pid, buf);655return -1;656}657658return 0;659}660661/*662* Test that it's possible to freeze a cgroup with a stopped process.663*/664static int test_cgfreezer_stopped(const char *root)665{666int pid, ret = KSFT_FAIL;667char *cgroup = NULL;668669cgroup = cg_name(root, "cg_test_stopped");670if (!cgroup)671goto cleanup;672673if (cg_create(cgroup))674goto cleanup;675676pid = cg_run_nowait(cgroup, child_fn, NULL);677678if (cg_wait_for_proc_count(cgroup, 1))679goto cleanup;680681if (kill(pid, SIGSTOP))682goto cleanup;683684if (cg_check_frozen(cgroup, false))685goto cleanup;686687if (cg_freeze_wait(cgroup, true))688goto cleanup;689690if (cg_freeze_wait(cgroup, false))691goto cleanup;692693if (proc_check_stopped(pid))694goto cleanup;695696ret = KSFT_PASS;697698cleanup:699if (cgroup)700cg_destroy(cgroup);701free(cgroup);702return ret;703}704705/*706* Test that it's possible to freeze a cgroup with a ptraced process.707*/708static int test_cgfreezer_ptraced(const char *root)709{710int pid, ret = KSFT_FAIL;711char *cgroup = NULL;712siginfo_t siginfo;713714cgroup = cg_name(root, "cg_test_ptraced");715if (!cgroup)716goto cleanup;717718if (cg_create(cgroup))719goto cleanup;720721pid = cg_run_nowait(cgroup, child_fn, NULL);722723if (cg_wait_for_proc_count(cgroup, 1))724goto cleanup;725726if (ptrace(PTRACE_SEIZE, pid, NULL, NULL))727goto cleanup;728729if (ptrace(PTRACE_INTERRUPT, pid, NULL, NULL))730goto cleanup;731732waitpid(pid, NULL, 0);733734if (cg_check_frozen(cgroup, false))735goto cleanup;736737if (cg_freeze_wait(cgroup, true))738goto cleanup;739740/*741* cg_check_frozen(cgroup, true) will fail here,742* because the task is in the TRACEd state.743*/744if (cg_freeze_wait(cgroup, false))745goto cleanup;746747if (ptrace(PTRACE_GETSIGINFO, pid, NULL, &siginfo))748goto cleanup;749750if (ptrace(PTRACE_DETACH, pid, NULL, NULL))751goto cleanup;752753ret = KSFT_PASS;754755cleanup:756if (cgroup)757cg_destroy(cgroup);758free(cgroup);759return ret;760}761762static int vfork_fn(const char *cgroup, void *arg)763{764int pid = vfork();765766if (pid == 0)767while (true)768sleep(1);769770return pid;771}772773/*774* Test that it's possible to freeze a cgroup with a process,775* which called vfork() and is waiting for a child.776*/777static int test_cgfreezer_vfork(const char *root)778{779int ret = KSFT_FAIL;780char *cgroup = NULL;781782cgroup = cg_name(root, "cg_test_vfork");783if (!cgroup)784goto cleanup;785786if (cg_create(cgroup))787goto cleanup;788789cg_run_nowait(cgroup, vfork_fn, NULL);790791if (cg_wait_for_proc_count(cgroup, 2))792goto cleanup;793794if (cg_freeze_wait(cgroup, true))795goto cleanup;796797ret = KSFT_PASS;798799cleanup:800if (cgroup)801cg_destroy(cgroup);802free(cgroup);803return ret;804}805806/*807* Get the current frozen_usec for the cgroup.808*/809static long cg_check_freezetime(const char *cgroup)810{811return cg_read_key_long(cgroup, "cgroup.stat.local",812"frozen_usec ");813}814815/*816* Test that the freeze time will behave as expected for an empty cgroup.817*/818static int test_cgfreezer_time_empty(const char *root)819{820int ret = KSFT_FAIL;821char *cgroup = NULL;822long prev, curr;823824cgroup = cg_name(root, "cg_time_test_empty");825if (!cgroup)826goto cleanup;827828/*829* 1) Create an empty cgroup and check that its freeze time830* is 0.831*/832if (cg_create(cgroup))833goto cleanup;834835curr = cg_check_freezetime(cgroup);836if (curr < 0) {837ret = KSFT_SKIP;838goto cleanup;839}840if (curr > 0) {841debug("Expect time (%ld) to be 0\n", curr);842goto cleanup;843}844845if (cg_freeze_nowait(cgroup, true))846goto cleanup;847848/*849* 2) Sleep for 1000 us. Check that the freeze time is at850* least 1000 us.851*/852usleep(1000);853curr = cg_check_freezetime(cgroup);854if (curr < 1000) {855debug("Expect time (%ld) to be at least 1000 us\n",856curr);857goto cleanup;858}859860/*861* 3) Unfreeze the cgroup. Check that the freeze time is862* larger than at 2).863*/864if (cg_freeze_nowait(cgroup, false))865goto cleanup;866prev = curr;867curr = cg_check_freezetime(cgroup);868if (curr <= prev) {869debug("Expect time (%ld) to be more than previous check (%ld)\n",870curr, prev);871goto cleanup;872}873874/*875* 4) Check the freeze time again to ensure that it has not876* changed.877*/878prev = curr;879curr = cg_check_freezetime(cgroup);880if (curr != prev) {881debug("Expect time (%ld) to be unchanged from previous check (%ld)\n",882curr, prev);883goto cleanup;884}885886ret = KSFT_PASS;887888cleanup:889if (cgroup)890cg_destroy(cgroup);891free(cgroup);892return ret;893}894895/*896* A simple test for cgroup freezer time accounting. This test follows897* the same flow as test_cgfreezer_time_empty, but with a single process898* in the cgroup.899*/900static int test_cgfreezer_time_simple(const char *root)901{902int ret = KSFT_FAIL;903char *cgroup = NULL;904long prev, curr;905906cgroup = cg_name(root, "cg_time_test_simple");907if (!cgroup)908goto cleanup;909910/*911* 1) Create a cgroup and check that its freeze time is 0.912*/913if (cg_create(cgroup))914goto cleanup;915916curr = cg_check_freezetime(cgroup);917if (curr < 0) {918ret = KSFT_SKIP;919goto cleanup;920}921if (curr > 0) {922debug("Expect time (%ld) to be 0\n", curr);923goto cleanup;924}925926/*927* 2) Populate the cgroup with one child and check that the928* freeze time is still 0.929*/930cg_run_nowait(cgroup, child_fn, NULL);931prev = curr;932curr = cg_check_freezetime(cgroup);933if (curr > prev) {934debug("Expect time (%ld) to be 0\n", curr);935goto cleanup;936}937938if (cg_freeze_nowait(cgroup, true))939goto cleanup;940941/*942* 3) Sleep for 1000 us. Check that the freeze time is at943* least 1000 us.944*/945usleep(1000);946prev = curr;947curr = cg_check_freezetime(cgroup);948if (curr < 1000) {949debug("Expect time (%ld) to be at least 1000 us\n",950curr);951goto cleanup;952}953954/*955* 4) Unfreeze the cgroup. Check that the freeze time is956* larger than at 3).957*/958if (cg_freeze_nowait(cgroup, false))959goto cleanup;960prev = curr;961curr = cg_check_freezetime(cgroup);962if (curr <= prev) {963debug("Expect time (%ld) to be more than previous check (%ld)\n",964curr, prev);965goto cleanup;966}967968/*969* 5) Sleep for 1000 us. Check that the freeze time is the970* same as at 4).971*/972usleep(1000);973prev = curr;974curr = cg_check_freezetime(cgroup);975if (curr != prev) {976debug("Expect time (%ld) to be unchanged from previous check (%ld)\n",977curr, prev);978goto cleanup;979}980981ret = KSFT_PASS;982983cleanup:984if (cgroup)985cg_destroy(cgroup);986free(cgroup);987return ret;988}989990/*991* Test that freezer time accounting works as expected, even while we're992* populating a cgroup with processes.993*/994static int test_cgfreezer_time_populate(const char *root)995{996int ret = KSFT_FAIL;997char *cgroup = NULL;998long prev, curr;999int i;10001001cgroup = cg_name(root, "cg_time_test_populate");1002if (!cgroup)1003goto cleanup;10041005if (cg_create(cgroup))1006goto cleanup;10071008curr = cg_check_freezetime(cgroup);1009if (curr < 0) {1010ret = KSFT_SKIP;1011goto cleanup;1012}1013if (curr > 0) {1014debug("Expect time (%ld) to be 0\n", curr);1015goto cleanup;1016}10171018/*1019* 1) Populate the cgroup with 100 processes. Check that1020* the freeze time is 0.1021*/1022for (i = 0; i < 100; i++)1023cg_run_nowait(cgroup, child_fn, NULL);1024prev = curr;1025curr = cg_check_freezetime(cgroup);1026if (curr != prev) {1027debug("Expect time (%ld) to be 0\n", curr);1028goto cleanup;1029}10301031/*1032* 2) Wait for the group to become fully populated. Check1033* that the freeze time is 0.1034*/1035if (cg_wait_for_proc_count(cgroup, 100))1036goto cleanup;1037prev = curr;1038curr = cg_check_freezetime(cgroup);1039if (curr != prev) {1040debug("Expect time (%ld) to be 0\n", curr);1041goto cleanup;1042}10431044/*1045* 3) Freeze the cgroup and then populate it with 100 more1046* processes. Check that the freeze time continues to grow.1047*/1048if (cg_freeze_nowait(cgroup, true))1049goto cleanup;1050prev = curr;1051curr = cg_check_freezetime(cgroup);1052if (curr <= prev) {1053debug("Expect time (%ld) to be more than previous check (%ld)\n",1054curr, prev);1055goto cleanup;1056}10571058for (i = 0; i < 100; i++)1059cg_run_nowait(cgroup, child_fn, NULL);1060prev = curr;1061curr = cg_check_freezetime(cgroup);1062if (curr <= prev) {1063debug("Expect time (%ld) to be more than previous check (%ld)\n",1064curr, prev);1065goto cleanup;1066}10671068/*1069* 4) Wait for the group to become fully populated. Check1070* that the freeze time is larger than at 3).1071*/1072if (cg_wait_for_proc_count(cgroup, 200))1073goto cleanup;1074prev = curr;1075curr = cg_check_freezetime(cgroup);1076if (curr <= prev) {1077debug("Expect time (%ld) to be more than previous check (%ld)\n",1078curr, prev);1079goto cleanup;1080}10811082/*1083* 5) Unfreeze the cgroup. Check that the freeze time is1084* larger than at 4).1085*/1086if (cg_freeze_nowait(cgroup, false))1087goto cleanup;1088prev = curr;1089curr = cg_check_freezetime(cgroup);1090if (curr <= prev) {1091debug("Expect time (%ld) to be more than previous check (%ld)\n",1092curr, prev);1093goto cleanup;1094}10951096/*1097* 6) Kill the processes. Check that the freeze time is the1098* same as it was at 5).1099*/1100if (cg_killall(cgroup))1101goto cleanup;1102prev = curr;1103curr = cg_check_freezetime(cgroup);1104if (curr != prev) {1105debug("Expect time (%ld) to be unchanged from previous check (%ld)\n",1106curr, prev);1107goto cleanup;1108}11091110/*1111* 7) Freeze and unfreeze the cgroup. Check that the freeze1112* time is larger than it was at 6).1113*/1114if (cg_freeze_nowait(cgroup, true))1115goto cleanup;1116if (cg_freeze_nowait(cgroup, false))1117goto cleanup;1118prev = curr;1119curr = cg_check_freezetime(cgroup);1120if (curr <= prev) {1121debug("Expect time (%ld) to be more than previous check (%ld)\n",1122curr, prev);1123goto cleanup;1124}11251126ret = KSFT_PASS;11271128cleanup:1129if (cgroup)1130cg_destroy(cgroup);1131free(cgroup);1132return ret;1133}11341135/*1136* Test that frozen time for a cgroup continues to work as expected,1137* even as processes are migrated. Frozen cgroup A's freeze time should1138* continue to increase and running cgroup B's should stay 0.1139*/1140static int test_cgfreezer_time_migrate(const char *root)1141{1142long prev_A, curr_A, curr_B;1143char *cgroup[2] = {0};1144int ret = KSFT_FAIL;1145int pid;11461147cgroup[0] = cg_name(root, "cg_time_test_migrate_A");1148if (!cgroup[0])1149goto cleanup;11501151cgroup[1] = cg_name(root, "cg_time_test_migrate_B");1152if (!cgroup[1])1153goto cleanup;11541155if (cg_create(cgroup[0]))1156goto cleanup;11571158if (cg_check_freezetime(cgroup[0]) < 0) {1159ret = KSFT_SKIP;1160goto cleanup;1161}11621163if (cg_create(cgroup[1]))1164goto cleanup;11651166pid = cg_run_nowait(cgroup[0], child_fn, NULL);1167if (pid < 0)1168goto cleanup;11691170if (cg_wait_for_proc_count(cgroup[0], 1))1171goto cleanup;11721173curr_A = cg_check_freezetime(cgroup[0]);1174if (curr_A) {1175debug("Expect time (%ld) to be 0\n", curr_A);1176goto cleanup;1177}1178curr_B = cg_check_freezetime(cgroup[1]);1179if (curr_B) {1180debug("Expect time (%ld) to be 0\n", curr_B);1181goto cleanup;1182}11831184/*1185* Freeze cgroup A.1186*/1187if (cg_freeze_wait(cgroup[0], true))1188goto cleanup;1189prev_A = curr_A;1190curr_A = cg_check_freezetime(cgroup[0]);1191if (curr_A <= prev_A) {1192debug("Expect time (%ld) to be > 0\n", curr_A);1193goto cleanup;1194}11951196/*1197* Migrate from A (frozen) to B (running).1198*/1199if (cg_enter(cgroup[1], pid))1200goto cleanup;12011202usleep(1000);1203curr_B = cg_check_freezetime(cgroup[1]);1204if (curr_B) {1205debug("Expect time (%ld) to be 0\n", curr_B);1206goto cleanup;1207}12081209prev_A = curr_A;1210curr_A = cg_check_freezetime(cgroup[0]);1211if (curr_A <= prev_A) {1212debug("Expect time (%ld) to be more than previous check (%ld)\n",1213curr_A, prev_A);1214goto cleanup;1215}12161217ret = KSFT_PASS;12181219cleanup:1220if (cgroup[0])1221cg_destroy(cgroup[0]);1222free(cgroup[0]);1223if (cgroup[1])1224cg_destroy(cgroup[1]);1225free(cgroup[1]);1226return ret;1227}12281229/*1230* The test creates a cgroup and freezes it. Then it creates a child cgroup.1231* After that it checks that the child cgroup has a non-zero freeze time1232* that is less than the parent's. Next, it freezes the child, unfreezes1233* the parent, and sleeps. Finally, it checks that the child's freeze1234* time has grown larger than the parent's.1235*/1236static int test_cgfreezer_time_parent(const char *root)1237{1238char *parent, *child = NULL;1239int ret = KSFT_FAIL;1240long ptime, ctime;12411242parent = cg_name(root, "cg_test_parent_A");1243if (!parent)1244goto cleanup;12451246child = cg_name(parent, "cg_test_parent_B");1247if (!child)1248goto cleanup;12491250if (cg_create(parent))1251goto cleanup;12521253if (cg_check_freezetime(parent) < 0) {1254ret = KSFT_SKIP;1255goto cleanup;1256}12571258if (cg_freeze_wait(parent, true))1259goto cleanup;12601261usleep(1000);1262if (cg_create(child))1263goto cleanup;12641265if (cg_check_frozen(child, true))1266goto cleanup;12671268/*1269* Since the parent was frozen the entire time the child cgroup1270* was being created, we expect the parent's freeze time to be1271* larger than the child's.1272*1273* Ideally, we would be able to check both times simultaneously,1274* but here we get the child's after we get the parent's.1275*/1276ptime = cg_check_freezetime(parent);1277ctime = cg_check_freezetime(child);1278if (ptime <= ctime) {1279debug("Expect ptime (%ld) > ctime (%ld)\n", ptime, ctime);1280goto cleanup;1281}12821283if (cg_freeze_nowait(child, true))1284goto cleanup;12851286if (cg_freeze_wait(parent, false))1287goto cleanup;12881289if (cg_check_frozen(child, true))1290goto cleanup;12911292usleep(100000);12931294ctime = cg_check_freezetime(child);1295ptime = cg_check_freezetime(parent);12961297if (ctime <= ptime) {1298debug("Expect ctime (%ld) > ptime (%ld)\n", ctime, ptime);1299goto cleanup;1300}13011302ret = KSFT_PASS;13031304cleanup:1305if (child)1306cg_destroy(child);1307free(child);1308if (parent)1309cg_destroy(parent);1310free(parent);1311return ret;1312}13131314/*1315* The test creates a parent cgroup and a child cgroup. Then, it freezes1316* the child and checks that the child's freeze time is greater than the1317* parent's, which should be zero.1318*/1319static int test_cgfreezer_time_child(const char *root)1320{1321char *parent, *child = NULL;1322int ret = KSFT_FAIL;1323long ptime, ctime;13241325parent = cg_name(root, "cg_test_child_A");1326if (!parent)1327goto cleanup;13281329child = cg_name(parent, "cg_test_child_B");1330if (!child)1331goto cleanup;13321333if (cg_create(parent))1334goto cleanup;13351336if (cg_check_freezetime(parent) < 0) {1337ret = KSFT_SKIP;1338goto cleanup;1339}13401341if (cg_create(child))1342goto cleanup;13431344if (cg_freeze_wait(child, true))1345goto cleanup;13461347ctime = cg_check_freezetime(child);1348ptime = cg_check_freezetime(parent);1349if (ptime != 0) {1350debug("Expect ptime (%ld) to be 0\n", ptime);1351goto cleanup;1352}13531354if (ctime <= ptime) {1355debug("Expect ctime (%ld) <= ptime (%ld)\n", ctime, ptime);1356goto cleanup;1357}13581359ret = KSFT_PASS;13601361cleanup:1362if (child)1363cg_destroy(child);1364free(child);1365if (parent)1366cg_destroy(parent);1367free(parent);1368return ret;1369}13701371/*1372* The test creates the following hierarchy:1373* A1374* |1375* B1376* |1377* C1378*1379* Then it freezes the cgroups in the order C, B, A.1380* Then it unfreezes the cgroups in the order A, B, C.1381* Then it checks that C's freeze time is larger than B's and1382* that B's is larger than A's.1383*/1384static int test_cgfreezer_time_nested(const char *root)1385{1386char *cgroup[3] = {0};1387int ret = KSFT_FAIL;1388long time[3] = {0};1389int i;13901391cgroup[0] = cg_name(root, "cg_test_time_A");1392if (!cgroup[0])1393goto cleanup;13941395cgroup[1] = cg_name(cgroup[0], "B");1396if (!cgroup[1])1397goto cleanup;13981399cgroup[2] = cg_name(cgroup[1], "C");1400if (!cgroup[2])1401goto cleanup;14021403if (cg_create(cgroup[0]))1404goto cleanup;14051406if (cg_check_freezetime(cgroup[0]) < 0) {1407ret = KSFT_SKIP;1408goto cleanup;1409}14101411if (cg_create(cgroup[1]))1412goto cleanup;14131414if (cg_create(cgroup[2]))1415goto cleanup;14161417if (cg_freeze_nowait(cgroup[2], true))1418goto cleanup;14191420if (cg_freeze_nowait(cgroup[1], true))1421goto cleanup;14221423if (cg_freeze_nowait(cgroup[0], true))1424goto cleanup;14251426usleep(1000);14271428if (cg_freeze_nowait(cgroup[0], false))1429goto cleanup;14301431if (cg_freeze_nowait(cgroup[1], false))1432goto cleanup;14331434if (cg_freeze_nowait(cgroup[2], false))1435goto cleanup;14361437time[2] = cg_check_freezetime(cgroup[2]);1438time[1] = cg_check_freezetime(cgroup[1]);1439time[0] = cg_check_freezetime(cgroup[0]);14401441if (time[2] <= time[1]) {1442debug("Expect C's time (%ld) > B's time (%ld)", time[2], time[1]);1443goto cleanup;1444}14451446if (time[1] <= time[0]) {1447debug("Expect B's time (%ld) > A's time (%ld)", time[1], time[0]);1448goto cleanup;1449}14501451ret = KSFT_PASS;14521453cleanup:1454for (i = 2; i >= 0 && cgroup[i]; i--) {1455cg_destroy(cgroup[i]);1456free(cgroup[i]);1457}14581459return ret;1460}14611462#define T(x) { x, #x }1463struct cgfreezer_test {1464int (*fn)(const char *root);1465const char *name;1466} tests[] = {1467T(test_cgfreezer_simple),1468T(test_cgfreezer_tree),1469T(test_cgfreezer_forkbomb),1470T(test_cgfreezer_mkdir),1471T(test_cgfreezer_rmdir),1472T(test_cgfreezer_migrate),1473T(test_cgfreezer_ptrace),1474T(test_cgfreezer_stopped),1475T(test_cgfreezer_ptraced),1476T(test_cgfreezer_vfork),1477T(test_cgfreezer_time_empty),1478T(test_cgfreezer_time_simple),1479T(test_cgfreezer_time_populate),1480T(test_cgfreezer_time_migrate),1481T(test_cgfreezer_time_parent),1482T(test_cgfreezer_time_child),1483T(test_cgfreezer_time_nested),1484};1485#undef T14861487int main(int argc, char *argv[])1488{1489char root[PATH_MAX];1490int i, ret = EXIT_SUCCESS;14911492if (cg_find_unified_root(root, sizeof(root), NULL))1493ksft_exit_skip("cgroup v2 isn't mounted\n");1494for (i = 0; i < ARRAY_SIZE(tests); i++) {1495switch (tests[i].fn(root)) {1496case KSFT_PASS:1497ksft_test_result_pass("%s\n", tests[i].name);1498break;1499case KSFT_SKIP:1500ksft_test_result_skip("%s\n", tests[i].name);1501break;1502default:1503ret = EXIT_FAILURE;1504ksft_test_result_fail("%s\n", tests[i].name);1505break;1506}1507}15081509return ret;1510}151115121513