Path: blob/master/tools/testing/selftests/cgroup/test_freezer.c
25923 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#define T(x) { x, #x }807struct cgfreezer_test {808int (*fn)(const char *root);809const char *name;810} tests[] = {811T(test_cgfreezer_simple),812T(test_cgfreezer_tree),813T(test_cgfreezer_forkbomb),814T(test_cgfreezer_mkdir),815T(test_cgfreezer_rmdir),816T(test_cgfreezer_migrate),817T(test_cgfreezer_ptrace),818T(test_cgfreezer_stopped),819T(test_cgfreezer_ptraced),820T(test_cgfreezer_vfork),821};822#undef T823824int main(int argc, char *argv[])825{826char root[PATH_MAX];827int i, ret = EXIT_SUCCESS;828829if (cg_find_unified_root(root, sizeof(root), NULL))830ksft_exit_skip("cgroup v2 isn't mounted\n");831for (i = 0; i < ARRAY_SIZE(tests); i++) {832switch (tests[i].fn(root)) {833case KSFT_PASS:834ksft_test_result_pass("%s\n", tests[i].name);835break;836case KSFT_SKIP:837ksft_test_result_skip("%s\n", tests[i].name);838break;839default:840ret = EXIT_FAILURE;841ksft_test_result_fail("%s\n", tests[i].name);842break;843}844}845846return ret;847}848849850