1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2017 FUJITSU LIMITED. All rights reserved.
4 * Author: Guangwen Feng <fenggw-fnst@cn.fujitsu.com>
5 */
6
7 /*
8 * This is a regression test for a crash caused by memcg function
9 * reentrant on buggy kernel. When doing rmdir(), a pending signal can
10 * interrupt the execution and lead to cgroup_clear_css_refs()
11 * being entered repeatedly, this results in a BUG_ON().
12 */
13
14 #include <errno.h>
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include <sys/types.h>
18 #include <sys/mount.h>
19 #include "tst_test.h"
20 #include "tst_cgroup.h"
21
22 static volatile int sigcounter;
23 static struct tst_cg_group *test_cg;
24
sighandler(int sig LTP_ATTRIBUTE_UNUSED)25 static void sighandler(int sig LTP_ATTRIBUTE_UNUSED)
26 {
27 sigcounter++;
28 }
29
do_child(void)30 static void do_child(void)
31 {
32 while (1)
33 SAFE_KILL(getppid(), SIGUSR1);
34
35 exit(0);
36 }
37
do_test(void)38 static void do_test(void)
39 {
40 pid_t cpid;
41
42 SAFE_SIGNAL(SIGUSR1, sighandler);
43
44 cpid = SAFE_FORK();
45 if (cpid == 0)
46 do_child();
47
48 while (sigcounter < 50000) {
49 test_cg = tst_cg_group_mk(tst_cg, "test");
50
51 if (test_cg)
52 test_cg = tst_cg_group_rm(test_cg);
53 }
54
55 SAFE_KILL(cpid, SIGKILL);
56 SAFE_WAIT(NULL);
57
58 tst_res(TPASS, "Bug not reproduced");
59 }
60
setup(void)61 static void setup(void)
62 {
63 struct tst_cg_opts opts;
64
65 memset(&opts, 0, sizeof(opts));
66
67 tst_cg_require("memory", &opts);
68 tst_cg_init();
69 if (TST_CG_VER(tst_cg, "memory") != TST_CG_V1)
70 SAFE_CG_PRINT(tst_cg, "cgroup.subtree_control", "+memory");
71 }
72
cleanup(void)73 static void cleanup(void)
74 {
75 if (test_cg)
76 test_cg = tst_cg_group_rm(test_cg);
77
78 tst_cg_cleanup();
79 }
80
81 static struct tst_test test = {
82 .needs_root = 1,
83 .forks_child = 1,
84 .setup = setup,
85 .cleanup = cleanup,
86 .test_all = do_test,
87 };
88