xref: /linux/tools/testing/selftests/cgroup/test_kill.c (revision 3cf2ab17d1ede9d143b7be115e0dd30545050eaf)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 #include <errno.h>
4 #include <linux/limits.h>
5 #include <sched.h>
6 #include <signal.h>
7 #include <stdbool.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/syscall.h>
12 #include <sys/types.h>
13 #include <sys/wait.h>
14 #include <unistd.h>
15 
16 #include "kselftest.h"
17 #include "../pidfd/pidfd.h"
18 #include "cgroup_util.h"
19 
20 /*
21  * Kill the given cgroup and wait for the inotify signal.
22  * If there are no events in 10 seconds, treat this as an error.
23  * Then check that the cgroup is in the desired state.
24  */
25 static int cg_kill_wait(const char *cgroup)
26 {
27 	int fd, ret = -1;
28 
29 	fd = cg_prepare_for_wait(cgroup);
30 	if (fd < 0)
31 		return fd;
32 
33 	ret = cg_write(cgroup, "cgroup.kill", "1");
34 	if (ret)
35 		goto out;
36 
37 	ret = cg_wait_for(fd);
38 	if (ret)
39 		goto out;
40 
41 out:
42 	close(fd);
43 	return ret;
44 }
45 
46 /*
47  * A simple process running in a sleep loop until being
48  * re-parented.
49  */
50 static int child_fn(const char *cgroup, void *arg)
51 {
52 	int ppid = getppid();
53 
54 	while (getppid() == ppid)
55 		usleep(1000);
56 
57 	return getppid() == ppid;
58 }
59 
60 static int test_cgkill_simple(const char *root)
61 {
62 	pid_t pids[100];
63 	int ret = KSFT_FAIL;
64 	char *cgroup = NULL;
65 	int i;
66 
67 	cgroup = cg_name(root, "cg_test_simple");
68 	if (!cgroup)
69 		goto cleanup;
70 
71 	if (cg_create(cgroup))
72 		goto cleanup;
73 
74 	for (i = 0; i < 100; i++)
75 		pids[i] = cg_run_nowait(cgroup, child_fn, NULL);
76 
77 	if (cg_wait_for_proc_count(cgroup, 100))
78 		goto cleanup;
79 
80 	if (cg_read_strcmp(cgroup, "cgroup.events", "populated 1\n"))
81 		goto cleanup;
82 
83 	if (cg_kill_wait(cgroup))
84 		goto cleanup;
85 
86 	ret = KSFT_PASS;
87 
88 cleanup:
89 	for (i = 0; i < 100; i++)
90 		wait_for_pid(pids[i]);
91 
92 	if (ret == KSFT_PASS &&
93 	    cg_read_strcmp_wait(cgroup, "cgroup.events", "populated 0\n"))
94 		ret = KSFT_FAIL;
95 
96 	if (cgroup)
97 		cg_destroy(cgroup);
98 	free(cgroup);
99 	return ret;
100 }
101 
102 /*
103  * The test creates the following hierarchy:
104  *       A
105  *    / / \ \
106  *   B  E  I K
107  *  /\  |
108  * C  D F
109  *      |
110  *      G
111  *      |
112  *      H
113  *
114  * with a process in C, H and 3 processes in K.
115  * Then it tries to kill the whole tree.
116  */
117 static int test_cgkill_tree(const char *root)
118 {
119 	pid_t pids[5];
120 	char *cgroup[10] = {0};
121 	int ret = KSFT_FAIL;
122 	int i;
123 
124 	cgroup[0] = cg_name(root, "cg_test_tree_A");
125 	if (!cgroup[0])
126 		goto cleanup;
127 
128 	cgroup[1] = cg_name(cgroup[0], "B");
129 	if (!cgroup[1])
130 		goto cleanup;
131 
132 	cgroup[2] = cg_name(cgroup[1], "C");
133 	if (!cgroup[2])
134 		goto cleanup;
135 
136 	cgroup[3] = cg_name(cgroup[1], "D");
137 	if (!cgroup[3])
138 		goto cleanup;
139 
140 	cgroup[4] = cg_name(cgroup[0], "E");
141 	if (!cgroup[4])
142 		goto cleanup;
143 
144 	cgroup[5] = cg_name(cgroup[4], "F");
145 	if (!cgroup[5])
146 		goto cleanup;
147 
148 	cgroup[6] = cg_name(cgroup[5], "G");
149 	if (!cgroup[6])
150 		goto cleanup;
151 
152 	cgroup[7] = cg_name(cgroup[6], "H");
153 	if (!cgroup[7])
154 		goto cleanup;
155 
156 	cgroup[8] = cg_name(cgroup[0], "I");
157 	if (!cgroup[8])
158 		goto cleanup;
159 
160 	cgroup[9] = cg_name(cgroup[0], "K");
161 	if (!cgroup[9])
162 		goto cleanup;
163 
164 	for (i = 0; i < 10; i++)
165 		if (cg_create(cgroup[i]))
166 			goto cleanup;
167 
168 	pids[0] = cg_run_nowait(cgroup[2], child_fn, NULL);
169 	pids[1] = cg_run_nowait(cgroup[7], child_fn, NULL);
170 	pids[2] = cg_run_nowait(cgroup[9], child_fn, NULL);
171 	pids[3] = cg_run_nowait(cgroup[9], child_fn, NULL);
172 	pids[4] = cg_run_nowait(cgroup[9], child_fn, NULL);
173 
174 	/*
175 	 * Wait until all child processes will enter
176 	 * corresponding cgroups.
177 	 */
178 
179 	if (cg_wait_for_proc_count(cgroup[2], 1) ||
180 	    cg_wait_for_proc_count(cgroup[7], 1) ||
181 	    cg_wait_for_proc_count(cgroup[9], 3))
182 		goto cleanup;
183 
184 	/*
185 	 * Kill A and check that we get an empty notification.
186 	 */
187 	if (cg_kill_wait(cgroup[0]))
188 		goto cleanup;
189 
190 	ret = KSFT_PASS;
191 
192 cleanup:
193 	for (i = 0; i < 5; i++)
194 		wait_for_pid(pids[i]);
195 
196 	if (ret == KSFT_PASS &&
197 	    cg_read_strcmp_wait(cgroup[0], "cgroup.events",
198 				   "populated 0\n"))
199 		ret = KSFT_FAIL;
200 
201 	for (i = 9; i >= 0 && cgroup[i]; i--) {
202 		cg_destroy(cgroup[i]);
203 		free(cgroup[i]);
204 	}
205 
206 	return ret;
207 }
208 
209 static int forkbomb_fn(const char *cgroup, void *arg)
210 {
211 	int ppid;
212 
213 	fork();
214 	fork();
215 
216 	ppid = getppid();
217 
218 	while (getppid() == ppid)
219 		usleep(1000);
220 
221 	return getppid() == ppid;
222 }
223 
224 /*
225  * The test runs a fork bomb in a cgroup and tries to kill it.
226  */
227 static int test_cgkill_forkbomb(const char *root)
228 {
229 	int ret = KSFT_FAIL;
230 	char *cgroup = NULL;
231 	pid_t pid = -ESRCH;
232 
233 	cgroup = cg_name(root, "cg_forkbomb_test");
234 	if (!cgroup)
235 		goto cleanup;
236 
237 	if (cg_create(cgroup))
238 		goto cleanup;
239 
240 	pid = cg_run_nowait(cgroup, forkbomb_fn, NULL);
241 	if (pid < 0)
242 		goto cleanup;
243 
244 	usleep(100000);
245 
246 	if (cg_kill_wait(cgroup))
247 		goto cleanup;
248 
249 	if (cg_wait_for_proc_count(cgroup, 0))
250 		goto cleanup;
251 
252 	ret = KSFT_PASS;
253 
254 cleanup:
255 	if (pid > 0)
256 		wait_for_pid(pid);
257 
258 	if (ret == KSFT_PASS &&
259 	    cg_read_strcmp_wait(cgroup, "cgroup.events", "populated 0\n"))
260 		ret = KSFT_FAIL;
261 
262 	if (cgroup)
263 		cg_destroy(cgroup);
264 	free(cgroup);
265 	return ret;
266 }
267 
268 /*
269  * Test that a cgroup that was killed in the past can still be the target
270  * of clone3(CLONE_INTO_CGROUP): writing cgroup.kill must only kill the
271  * tasks in the cgroup at the time of the write, not tasks cloned into
272  * it afterwards.
273  */
274 static int test_cgkill_clone_into_killed(const char *root)
275 {
276 	pid_t pid;
277 	int cgroup_fd = -EBADF;
278 	int ret = KSFT_FAIL;
279 	char *cgroup = NULL;
280 
281 	cgroup = cg_name(root, "cg_test_clone_into_killed");
282 	if (!cgroup)
283 		goto cleanup;
284 
285 	if (cg_create(cgroup))
286 		goto cleanup;
287 
288 	/* Kill the cgroup while it is still empty. */
289 	if (cg_write(cgroup, "cgroup.kill", "1"))
290 		goto cleanup;
291 
292 	cgroup_fd = dirfd_open_opath(cgroup);
293 	if (cgroup_fd < 0)
294 		goto cleanup;
295 
296 	pid = clone_into_cgroup(cgroup_fd);
297 	if (pid < 0) {
298 		if (errno == ENOSYS)
299 			ret = KSFT_SKIP;
300 		goto cleanup;
301 	}
302 
303 	if (pid == 0)
304 		exit(EXIT_SUCCESS);
305 
306 	/* The child must not be SIGKILLed; it has to exit cleanly. */
307 	if (clone_reap(pid, WEXITED) != EXIT_SUCCESS)
308 		goto cleanup;
309 
310 	ret = KSFT_PASS;
311 
312 cleanup:
313 	if (cgroup_fd >= 0)
314 		close(cgroup_fd);
315 	if (cgroup)
316 		cg_destroy(cgroup);
317 	free(cgroup);
318 	return ret;
319 }
320 
321 static int sleep_forever(void *arg)
322 {
323 	for (;;)
324 		pause();
325 	return 0;
326 }
327 
328 /*
329  * Start a thread and exit the main thread only, leaving behind a zombie
330  * thread group leader with a live thread.
331  */
332 static int exit_leader_fn(const char *cgroup, void *arg)
333 {
334 	const size_t stack_size = 64 * 1024;
335 	char *stack = malloc(stack_size);
336 
337 	if (!stack)
338 		return EXIT_FAILURE;
339 
340 	if (clone(sleep_forever, stack + stack_size,
341 		  CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
342 		  CLONE_THREAD | CLONE_SYSVSEM, NULL) < 0)
343 		return EXIT_FAILURE;
344 
345 	syscall(SYS_exit, 0);
346 	return EXIT_FAILURE;
347 }
348 
349 static int wait_for_zombie_leader(pid_t pid)
350 {
351 	int i;
352 
353 	for (i = 0; i < 100; i++) {
354 		if (!proc_read_strstr(pid, false, "stat", ") Z "))
355 			return 0;
356 		usleep(10000);
357 	}
358 
359 	return -1;
360 }
361 
362 /*
363  * Test that cgroup.kill kills a process whose thread group leader has
364  * already exited while its other threads are still running.
365  */
366 static int test_cgkill_exited_leader(const char *root)
367 {
368 	int ret = KSFT_FAIL;
369 	char *cgroup = NULL;
370 	pid_t pid = -ESRCH;
371 
372 	cgroup = cg_name(root, "cg_test_exited_leader");
373 	if (!cgroup)
374 		goto cleanup;
375 
376 	if (cg_create(cgroup))
377 		goto cleanup;
378 
379 	pid = cg_run_nowait(cgroup, exit_leader_fn, NULL);
380 	if (pid < 0)
381 		goto cleanup;
382 
383 	if (wait_for_zombie_leader(pid))
384 		goto cleanup;
385 
386 	if (cg_read_strcmp(cgroup, "cgroup.events", "populated 1\n"))
387 		goto cleanup;
388 
389 	if (cg_write(cgroup, "cgroup.kill", "1"))
390 		goto cleanup;
391 
392 	if (cg_read_strcmp_wait(cgroup, "cgroup.events", "populated 0\n"))
393 		goto cleanup;
394 
395 	ret = KSFT_PASS;
396 
397 cleanup:
398 	if (pid > 0) {
399 		kill(pid, SIGKILL);
400 		wait_for_pid(pid);
401 	}
402 	if (cgroup)
403 		cg_destroy(cgroup);
404 	free(cgroup);
405 	return ret;
406 }
407 
408 /*
409  * Test that cgroup.kill kills the threads of a process that were migrated
410  * to the cgroup after their thread group leader had exited, and so are
411  * not listed in its cgroup.procs.
412  */
413 static int test_cgkill_exited_leader_migrated(const char *root)
414 {
415 	char *parent = NULL, *src = NULL, *dst = NULL;
416 	int ret = KSFT_FAIL;
417 	pid_t pid = -ESRCH;
418 
419 	parent = cg_name(root, "cg_test_exited_leader_migrated");
420 	if (!parent)
421 		goto cleanup;
422 
423 	src = cg_name(parent, "src");
424 	if (!src)
425 		goto cleanup;
426 
427 	dst = cg_name(parent, "dst");
428 	if (!dst)
429 		goto cleanup;
430 
431 	if (cg_create(parent) || cg_create(src) || cg_create(dst))
432 		goto cleanup;
433 
434 	pid = cg_run_nowait(src, exit_leader_fn, NULL);
435 	if (pid < 0)
436 		goto cleanup;
437 
438 	if (wait_for_zombie_leader(pid))
439 		goto cleanup;
440 
441 	/* The exiting leader stays behind, only the live thread is moved. */
442 	if (cg_enter(dst, pid))
443 		goto cleanup;
444 
445 	if (cg_read_strcmp(dst, "cgroup.events", "populated 1\n") ||
446 	    cg_read_strcmp(dst, "cgroup.procs", ""))
447 		goto cleanup;
448 
449 	if (cg_write(dst, "cgroup.kill", "1"))
450 		goto cleanup;
451 
452 	if (cg_read_strcmp_wait(dst, "cgroup.events", "populated 0\n"))
453 		goto cleanup;
454 
455 	ret = KSFT_PASS;
456 
457 cleanup:
458 	if (pid > 0) {
459 		kill(pid, SIGKILL);
460 		wait_for_pid(pid);
461 	}
462 	if (dst)
463 		cg_destroy(dst);
464 	if (src)
465 		cg_destroy(src);
466 	if (parent)
467 		cg_destroy(parent);
468 	free(dst);
469 	free(src);
470 	free(parent);
471 	return ret;
472 }
473 
474 #define T(x) { x, #x }
475 struct cgkill_test {
476 	int (*fn)(const char *root);
477 	const char *name;
478 } tests[] = {
479 	T(test_cgkill_simple),
480 	T(test_cgkill_tree),
481 	T(test_cgkill_forkbomb),
482 	T(test_cgkill_clone_into_killed),
483 	T(test_cgkill_exited_leader),
484 	T(test_cgkill_exited_leader_migrated),
485 };
486 #undef T
487 
488 int main(int argc, char *argv[])
489 {
490 	char root[PATH_MAX];
491 	int i;
492 
493 	ksft_print_header();
494 	if (cg_find_unified_root(root, sizeof(root), NULL))
495 		ksft_exit_skip("cgroup v2 isn't mounted\n");
496 	ksft_set_plan(ARRAY_SIZE(tests));
497 	for (i = 0; i < ARRAY_SIZE(tests); i++) {
498 		switch (tests[i].fn(root)) {
499 		case KSFT_PASS:
500 			ksft_test_result_pass("%s\n", tests[i].name);
501 			break;
502 		case KSFT_SKIP:
503 			ksft_test_result_skip("%s\n", tests[i].name);
504 			break;
505 		default:
506 			ksft_test_result_fail("%s\n", tests[i].name);
507 			break;
508 		}
509 	}
510 
511 	ksft_finished();
512 }
513