xref: /aosp_15_r20/external/ltp/testcases/kernel/sched/autogroup/autogroup01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2017 Fujitsu Ltd.
4*49cdfc7eSAndroid Build Coastguard Worker  * Ported: Guangwen Feng <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*
8*49cdfc7eSAndroid Build Coastguard Worker  * This is a regression test about the race in autogroup, this test
9*49cdfc7eSAndroid Build Coastguard Worker  * can crash the buggy kernel, and the bug has been fixed in:
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  *   commit 18f649ef344127ef6de23a5a4272dbe2fdb73dde
12*49cdfc7eSAndroid Build Coastguard Worker  *   Author: Oleg Nesterov <[email protected]>
13*49cdfc7eSAndroid Build Coastguard Worker  *   Date:   Mon Nov 14 19:46:09 2016 +0100
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  *   sched/autogroup: Fix autogroup_move_group() to never skip sched_move_task()
16*49cdfc7eSAndroid Build Coastguard Worker  */
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker #define LOOPS	1000
24*49cdfc7eSAndroid Build Coastguard Worker #define PATH_AUTOGROUP	"/proc/sys/kernel/sched_autogroup_enabled"
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker static int orig_autogroup = -1;
27*49cdfc7eSAndroid Build Coastguard Worker 
do_test(void)28*49cdfc7eSAndroid Build Coastguard Worker static void do_test(void)
29*49cdfc7eSAndroid Build Coastguard Worker {
30*49cdfc7eSAndroid Build Coastguard Worker 	int i;
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker 	if (!SAFE_FORK()) {
33*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_FILE_PRINTF(PATH_AUTOGROUP, "%d", 1);
34*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_SETSID();
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 		if (SAFE_FORK())
37*49cdfc7eSAndroid Build Coastguard Worker 			pause();
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_KILL(getppid(), SIGKILL);
40*49cdfc7eSAndroid Build Coastguard Worker 		usleep(1000);
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker 		// The child has gone, the grandchild runs with kref == 1
43*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_FILE_PRINTF(PATH_AUTOGROUP, "%d", 0);
44*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_SETSID();
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker 		// runs with the freed ag/tg
47*49cdfc7eSAndroid Build Coastguard Worker 		for (i = 0; i < LOOPS; i++)
48*49cdfc7eSAndroid Build Coastguard Worker 			usleep(10);
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker 		TST_CHECKPOINT_WAKE(0);
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker 		exit(0);
53*49cdfc7eSAndroid Build Coastguard Worker 	}
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WAIT(NULL); // destroy the child's ag/tg
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker 	TST_CHECKPOINT_WAIT(0);
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "Bug not reproduced");
60*49cdfc7eSAndroid Build Coastguard Worker }
61*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)62*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
63*49cdfc7eSAndroid Build Coastguard Worker {
64*49cdfc7eSAndroid Build Coastguard Worker 	if (access(PATH_AUTOGROUP, F_OK))
65*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "autogroup not supported");
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_SCANF(PATH_AUTOGROUP, "%d", &orig_autogroup);
68*49cdfc7eSAndroid Build Coastguard Worker }
69*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)70*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
71*49cdfc7eSAndroid Build Coastguard Worker {
72*49cdfc7eSAndroid Build Coastguard Worker 	if (orig_autogroup != -1)
73*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_FILE_PRINTF(PATH_AUTOGROUP, "%d", orig_autogroup);
74*49cdfc7eSAndroid Build Coastguard Worker }
75*49cdfc7eSAndroid Build Coastguard Worker 
76*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
77*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
78*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
79*49cdfc7eSAndroid Build Coastguard Worker 	.needs_checkpoints = 1,
80*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
81*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
82*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = do_test,
83*49cdfc7eSAndroid Build Coastguard Worker 	.tags = (const struct tst_tag[]) {
84*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "18f649ef3441"},
85*49cdfc7eSAndroid Build Coastguard Worker 		{}
86*49cdfc7eSAndroid Build Coastguard Worker 	}
87*49cdfc7eSAndroid Build Coastguard Worker };
88