Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

selftests/sched_ext: Fix init_enable_count flakiness

The init_enable_count test is flaky. The test forks 1024 children before
attaching the scheduler to verify that existing tasks get ops.init_task()
called. The children were using sleep(1) before exiting.

7900aa699c34 ("sched_ext: Fix cgroup exit ordering by moving sched_ext_free()
to finish_task_switch()") changed when tasks are removed from scx_tasks -
previously when the task_struct was freed, now immediately in
finish_task_switch() when the task dies.

Before the commit, pre-forked children would linger on scx_tasks until freed
regardless of when they exited, so the scheduler would always see them during
iteration. The sleep(1) was unnecessary. After the commit, children are
removed as soon as they die. The sleep(1) masks the problem in most cases but
the test becomes flaky depending on timing.

Fix by synchronizing properly using a pipe. All children block on read() and
the parent signals them to exit by closing the write end after attaching the
scheduler. The children are auto-reaped so there's no need to wait on them.

Reported-by: Ihor Solodrai <ihor.solodrai@linux.dev>
Cc: David Vernet <void@manifault.com>
Cc: Andrea Righi <arighi@nvidia.com>
Cc: Changwoo Min <changwoo@igalia.com>
Cc: Emil Tsalapatis <emil@etsalapatis.com>
Signed-off-by: Tejun Heo <tj@kernel.org>

Tejun Heo 4544e9c4 2e06d54e

+23 -11
+23 -11
tools/testing/selftests/sched_ext/init_enable_count.c
··· 4 4 * Copyright (c) 2023 David Vernet <dvernet@meta.com> 5 5 * Copyright (c) 2023 Tejun Heo <tj@kernel.org> 6 6 */ 7 + #include <signal.h> 7 8 #include <stdio.h> 8 9 #include <unistd.h> 9 10 #include <sched.h> ··· 24 23 int ret, i, status; 25 24 struct sched_param param = {}; 26 25 pid_t pids[num_pre_forks]; 26 + int pipe_fds[2]; 27 + 28 + SCX_FAIL_IF(pipe(pipe_fds) < 0, "Failed to create pipe"); 27 29 28 30 skel = init_enable_count__open(); 29 31 SCX_FAIL_IF(!skel, "Failed to open"); ··· 42 38 * ensure (at least in practical terms) that there are more tasks that 43 39 * transition from SCHED_OTHER -> SCHED_EXT than there are tasks that 44 40 * take the fork() path either below or in other processes. 41 + * 42 + * All children will block on read() on the pipe until the parent closes 43 + * the write end after attaching the scheduler, which signals all of 44 + * them to exit simultaneously. Auto-reap so we don't have to wait on 45 + * them. 45 46 */ 47 + signal(SIGCHLD, SIG_IGN); 46 48 for (i = 0; i < num_pre_forks; i++) { 47 - pids[i] = fork(); 48 - SCX_FAIL_IF(pids[i] < 0, "Failed to fork child"); 49 - if (pids[i] == 0) { 50 - sleep(1); 49 + pid_t pid = fork(); 50 + 51 + SCX_FAIL_IF(pid < 0, "Failed to fork child"); 52 + if (pid == 0) { 53 + char buf; 54 + 55 + close(pipe_fds[1]); 56 + read(pipe_fds[0], &buf, 1); 57 + close(pipe_fds[0]); 51 58 exit(0); 52 59 } 53 60 } 61 + close(pipe_fds[0]); 54 62 55 63 link = bpf_map__attach_struct_ops(skel->maps.init_enable_count_ops); 56 64 SCX_FAIL_IF(!link, "Failed to attach struct_ops"); 57 65 58 - for (i = 0; i < num_pre_forks; i++) { 59 - SCX_FAIL_IF(waitpid(pids[i], &status, 0) != pids[i], 60 - "Failed to wait for pre-forked child\n"); 61 - 62 - SCX_FAIL_IF(status != 0, "Pre-forked child %d exited with status %d\n", i, 63 - status); 64 - } 66 + /* Signal all pre-forked children to exit. */ 67 + close(pipe_fds[1]); 68 + signal(SIGCHLD, SIG_DFL); 65 69 66 70 bpf_link__destroy(link); 67 71 SCX_GE(skel->bss->init_task_cnt, num_pre_forks);