xref: /aosp_15_r20/external/ltp/testcases/kernel/controllers/memcg/regression/memcg_test_3.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2017 FUJITSU LIMITED. All rights reserved.
4  * Author: Guangwen Feng <[email protected]>
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 static pid_t ppid;
25 
sighandler(int sig LTP_ATTRIBUTE_UNUSED)26 static void sighandler(int sig LTP_ATTRIBUTE_UNUSED)
27 {
28 	sigcounter++;
29 }
30 
do_child(void)31 static void do_child(void)
32 {
33 	while (getppid() == ppid)
34 		SAFE_KILL(ppid, SIGUSR1);
35 
36 	exit(0);
37 }
38 
do_test(void)39 static void do_test(void)
40 {
41 	pid_t cpid;
42 
43 	SAFE_SIGNAL(SIGUSR1, sighandler);
44 	ppid = getpid();
45 
46 	cpid = SAFE_FORK();
47 	if (cpid == 0)
48 		do_child();
49 
50 	while (sigcounter < 50000) {
51 		test_cg = tst_cg_group_mk(tst_cg, "test");
52 
53 		if (test_cg)
54 			test_cg = tst_cg_group_rm(test_cg);
55 	}
56 
57 	SAFE_KILL(cpid, SIGKILL);
58 	SAFE_WAIT(NULL);
59 
60 	tst_res(TPASS, "Bug not reproduced");
61 }
62 
setup(void)63 static void setup(void)
64 {
65 	struct tst_cg_opts opts;
66 
67 	memset(&opts, 0, sizeof(opts));
68 
69 	tst_cg_require("memory", &opts);
70 	tst_cg_init();
71 	if (TST_CG_VER(tst_cg, "memory") != TST_CG_V1)
72 		SAFE_CG_PRINT(tst_cg, "cgroup.subtree_control", "+memory");
73 }
74 
cleanup(void)75 static void cleanup(void)
76 {
77 	if (test_cg)
78 		test_cg = tst_cg_group_rm(test_cg);
79 
80 	tst_cg_cleanup();
81 }
82 
83 static struct tst_test test = {
84 	.needs_root = 1,
85 	.forks_child = 1,
86 	.setup = setup,
87 	.cleanup = cleanup,
88 	.test_all = do_test,
89 };
90