From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=213.97.179.56 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796535492; b=IwVbP9TCY+DZRCWj59al4IX+PjPAXxl8YAWFNRU4N/DkF7vhJ7tVBLmWtdGreSJ0ANRP P1iTAQLYZkf33IvWv21Ifk3Fv84pxNRIc9O+hV6WDYWLg5NzuTw1Am6kNlz08CZ1Swcfm lHiNBNrpyjwlzYlU7PJOy6+1yxyc5PmbC710wB0bG8h/9+BeMbToiDZieL9xXBY/bomF/ JdUcnJLFrRV6ZVWQpiRiOG9Q8R81kpPC3Y4o3fdETze0jQsP44MbcntdLYe/TuoBN20GL E4HRzUlboADVWFpI5PKQM7NWa93GLzBuUQjeQINHt9gJk6pLPoIfE8hUSxZm3sj5FMA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796535492; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=kyH+K4X+LBZQyVV5Bc0ZSUIDgYd8mWFsy2BbFKz7IAY=; b=pEE3mwKoeGzF0D6qthueBbr9DXNfs3KFXY2En5iPvXGdUDB2iE2LFmRK/emQxl3pGwPG xGMWeODaOYpljX3b5JDdJSezUcvKqp7uBetW+acnsJC03HXCNlvRCftCmh1Q/Tf833lXH HFJa70JAi/cVWRYcJMJWRzwO3aAV35PWkw3XRKzUqwlWryGus9DP0kmKtgAQVR4cLRjWR nTgT7r2qCveJILuoeqi6p9HWj7TpD3TLSLkbjaL1HrQl7KaUyhLAiMpFjovB6t5awShjz qb+ly29Eg+kpana/4w2TJajha2ipFdPiIQqe9N7XwjO74eX5oHc5fweZBsDI9JCEqnQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=igalia.com; spf=pass smtp.mailfrom=igalia.com; dkim=pass header.d=igalia.com header.i=@igalia.com header.a=rsa-sha256 header.s=20170329 header.b=VcRfQMqZ; arc=none smtp.remote-ip=213.97.179.56 Received: from fanzine2.igalia.com (fanzine2.igalia.com [213.97.179.56]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7EDC7606A322 for ; Sun, 6 Dec 2026 05:38:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7EDC7606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=igalia.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=igalia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=igalia.com header.i=@igalia.com header.a=rsa-sha256 header.s=20170329 header.b=VcRfQMqZ DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:Message-ID:Date:Subject: Cc:To:From:From:Reply-To; bh=kyH+K4X+LBZQyVV5Bc0ZSUIDgYd8mWFsy2BbFKz7IAY=; t=1796535490; x=2111895490; b=VcRfQMqZ+NvEPnMWwbqIPDAAGrkvwkzSoArEO47Rycml1X7 XbI80Wt3u2t+twIA+dkmqjDNGAbbycsgIywqZlcKXlry3Kf6XAfYwBIKOyV1/gi+qASL95ntPgxO8 6gU7I5bWCdvKa7tbNzot4jgJCTaOfyJyaxb21CRi7JtpGfrD5N5z0UVr3aL97o1RpHUDTDfU9aFPp VKFzk64jAYYrPyMerqdFhgesXL3epUCulGj2N3J0GoZamQpHG4oh7w09lWX8kMtP732EPrismeFiP rsAxkQo2veyQVp+0jDtLcwLUlEVMSRJslvtjRvCPqNDsYN8s5ALfA8KEoQMvxHOg==; Received: from [58.29.145.179] (helo=localhost) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) (envelope-from ) id 1xa4wo-00000002dOh-1cED; Sun, 06 Dec 2026 06:38:07 +0100 From: Changwoo Min To: tj@kernel.org, void@manifault.com, arighi@nvidia.com, changwoo@igalia.com Cc: david.dai@linux.dev, Gavin Guo , kernel-dev@igalia.com, sched-ext@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v3 6/6] selftests/sched_ext: Test task field reset across sub-scheduler handoffs Date: Sun, 6 Dec 2026 14:37:28 +0900 Message-ID: <20261206053728.2026272-7-changwoo@igalia.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261206053728.2026272-1-changwoo@igalia.com> References: <20261206053728.2026272-1-changwoo@igalia.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit A task handed between a root scheduler and a sub-scheduler must not carry the bpf_priv and dsq_vtime of the scheduler it left, even when that scheduler writes them from ops.exit_task(). Load one scheduler twice, as the root and as a sub-scheduler on a cgroup. A task in that cgroup goes to the sub-scheduler when it attaches and back to the root when it detaches. Both schedulers set bpf_priv from ops.enable(), and set bpf_priv and dsq_vtime from ops.exit_task(). Every ops.enable() must find both fields cleared. The receiving scheduler's ops.init_task() runs while the task is still on the other scheduler. It must see the task as not its own and fail to set bpf_priv, and scx_task_sched_id() there must report the other scheduler, which a test with a single scheduler can't show. The test skips where sub-schedulers aren't supported. Signed-off-by: Changwoo Min --- tools/testing/selftests/sched_ext/Makefile | 1 + .../selftests/sched_ext/sub_handoff.bpf.c | 166 +++++++++ .../testing/selftests/sched_ext/sub_handoff.c | 341 ++++++++++++++++++ 3 files changed, 508 insertions(+) create mode 100644 tools/testing/selftests/sched_ext/sub_handoff.bpf.c create mode 100644 tools/testing/selftests/sched_ext/sub_handoff.c diff --git a/tools/testing/selftests/sched_ext/Makefile b/tools/testing/selftests/sched_ext/Makefile index 1ab274250403..1dcaf60c5b33 100644 --- a/tools/testing/selftests/sched_ext/Makefile +++ b/tools/testing/selftests/sched_ext/Makefile @@ -201,6 +201,7 @@ auto-test-targets := \ sched_id \ sub_caps_kfunc \ sub_core_caps \ + sub_handoff \ task_bpf_priv \ test_example \ total_bw \ diff --git a/tools/testing/selftests/sched_ext/sub_handoff.bpf.c b/tools/testing/selftests/sched_ext/sub_handoff.bpf.c new file mode 100644 index 000000000000..183ec98d9469 --- /dev/null +++ b/tools/testing/selftests/sched_ext/sub_handoff.bpf.c @@ -0,0 +1,166 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * A cid-form scheduler for the sub_handoff selftest, loaded twice: as the root + * and as a sub-scheduler on a cgroup. + * + * Copyright (c) 2026 Meta Platforms, Inc. and affiliates. + * Author: Changwoo Min + */ +#include + +char _license[] SEC("license") = "GPL"; + +#define PRIV_MAGIC 0x5c7da7a000000000ULL +#define VTIME_MAGIC 0x5c7d7173e0000000ULL + +struct { + __uint(type, BPF_MAP_TYPE_ARENA); + __uint(map_flags, BPF_F_MMAPABLE); + __uint(max_entries, 1 << 4); +} arena SEC(".maps"); + +u64 __arena_global arena_touch; + +/* The task handed between the schedulers, set by set_target() */ +s32 target_pid; + +/* The same task's pid in user space's PID namespace, set by user space */ +s32 target_vpid; + +/* ops.enable() calls on the target, and those that saw a value left behind */ +u64 nr_enable, nr_stale_priv, nr_stale_vtime; + +/* Failed writes that the checks above depend on */ +u64 nr_enable_set_fail, nr_exit_vtime_fail; + +/* ops.exit_task() calls on the target, and those that failed to set bpf_priv */ +u64 nr_exit, nr_exit_set_fail; + +/* + * ops.init_task() calls on the target, those that saw a bpf_priv, those that + * found the target on this scheduler, and those that set bpf_priv + */ +u64 nr_init, nr_init_priv, nr_init_own, nr_init_set; + +/* This scheduler's id, and the target's owner id at ops.init_task() */ +u64 sched_id, init_owner_id; + +/* ops.enable() calls that didn't find the target on this scheduler */ +u64 nr_enable_not_own; + +UEI_DEFINE(uei); + +s32 BPF_STRUCT_OPS(sub_handoff_select_cid, struct task_struct *p, s32 prev_cid, + u64 wake_flags) +{ + return prev_cid; +} + +void BPF_STRUCT_OPS(sub_handoff_enqueue, struct task_struct *p, u64 enq_flags) +{ + scx_bpf_dsq_insert(p, SCX_DSQ_LOCAL, SCX_SLICE_DFL, enq_flags); +} + +void BPF_STRUCT_OPS(sub_handoff_enable, struct task_struct *p, + struct scx_enable_args *args) +{ + if (p->pid != target_pid) + return; + + if (scx_task_sched_id(p) != scx_bpf_sched_id()) + __sync_fetch_and_add(&nr_enable_not_own, 1); + if (p->scx.bpf_priv) + __sync_fetch_and_add(&nr_stale_priv, 1); + if (p->scx.dsq_vtime) + __sync_fetch_and_add(&nr_stale_vtime, 1); + + if (!scx_bpf_task_set_priv(p, PRIV_MAGIC | (u32)p->pid)) + __sync_fetch_and_add(&nr_enable_set_fail, 1); + __sync_fetch_and_add(&nr_enable, 1); +} + +/* + * @p is not on this scheduler yet. On a handoff, it is still on the other + * scheduler, whose bpf_priv this sees and must not be able to set. + */ +s32 BPF_STRUCT_OPS(sub_handoff_init_task, struct task_struct *p, + struct scx_init_task_args *args) +{ + u64 owner; + + if (p->pid != target_pid) + return 0; + + __sync_fetch_and_add(&nr_init, 1); + if (p->scx.bpf_priv) + __sync_fetch_and_add(&nr_init_priv, 1); + owner = scx_task_sched_id(p); + init_owner_id = owner; + if (owner == scx_bpf_sched_id()) + __sync_fetch_and_add(&nr_init_own, 1); + if (scx_bpf_task_set_priv(p, PRIV_MAGIC | (u32)p->pid)) + __sync_fetch_and_add(&nr_init_set, 1); + return 0; +} + +/* + * @p is still on this scheduler, so both writes succeed. Neither may reach the + * scheduler @p moves to. + */ +void BPF_STRUCT_OPS(sub_handoff_exit_task, struct task_struct *p, + struct scx_exit_task_args *args) +{ + if (p->pid != target_pid) + return; + + __sync_fetch_and_add(&nr_exit, 1); + if (!scx_bpf_task_set_priv(p, PRIV_MAGIC | (u32)p->pid)) + __sync_fetch_and_add(&nr_exit_set_fail, 1); + if (!scx_bpf_task_set_dsq_vtime(p, VTIME_MAGIC)) + __sync_fetch_and_add(&nr_exit_vtime_fail, 1); +} + +s32 BPF_STRUCT_OPS_SLEEPABLE(sub_handoff_sub_attach, + struct scx_sub_attach_args *args) +{ + return 0; +} + +s32 BPF_STRUCT_OPS_SLEEPABLE(sub_handoff_init) +{ + arena_touch++; + sched_id = scx_bpf_sched_id(); + return 0; +} + +void BPF_STRUCT_OPS(sub_handoff_exit, struct scx_exit_info *ei) +{ + UEI_RECORD(uei, ei); +} + +struct task_struct *bpf_task_from_vpid(s32 vpid) __ksym; + +/* p->pid is in the initial PID namespace, which user space may not be in */ +SEC("syscall") +int set_target(void *ctx) +{ + struct task_struct *p = bpf_task_from_vpid(target_vpid); + + if (!p) + return -ESRCH; + target_pid = p->pid; + bpf_task_release(p); + return 0; +} + +SCX_OPS_CID_DEFINE(sub_handoff_ops, + .select_cid = (void *)sub_handoff_select_cid, + .enqueue = (void *)sub_handoff_enqueue, + .init_task = (void *)sub_handoff_init_task, + .enable = (void *)sub_handoff_enable, + .exit_task = (void *)sub_handoff_exit_task, + .sub_attach = (void *)sub_handoff_sub_attach, + .init = (void *)sub_handoff_init, + .exit = (void *)sub_handoff_exit, + .flags = SCX_OPS_SWITCH_PARTIAL, + .name = "sub_handoff"); diff --git a/tools/testing/selftests/sched_ext/sub_handoff.c b/tools/testing/selftests/sched_ext/sub_handoff.c new file mode 100644 index 000000000000..e79eea4c9542 --- /dev/null +++ b/tools/testing/selftests/sched_ext/sub_handoff.c @@ -0,0 +1,341 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Verify that a task handed between a root scheduler and a sub-scheduler + * arrives without the p->scx.bpf_priv and dsq_vtime of the scheduler it left, + * even when that scheduler writes them from ops.exit_task(). + * + * Copyright (c) 2026 Meta Platforms, Inc. and affiliates. + * Author: Changwoo Min + */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "cgroup_util.h" +#include "scx_test.h" +#include "sub_handoff.bpf.skel.h" + +#ifndef SCHED_EXT +#define SCHED_EXT 7 +#endif + +/* As long as the longest sched_ext watchdog timeout */ +#define WAIT_TIMEOUT_MS 30000 + +struct sub_handoff_ctx { + struct sub_handoff *root_skel; + struct sub_handoff *sub_skel; + char sub_cg[PATH_MAX]; + u64 sub_cgid; + bool cg_created; +}; + +static u64 get_cgroup_id(const char *path) +{ + union { + u64 id; + unsigned char bytes[8]; + } id = {}; + struct file_handle *handle; + int mount_id, ret; + + handle = calloc(1, sizeof(*handle) + sizeof(id)); + if (!handle) + return 0; + handle->handle_bytes = sizeof(id); + ret = name_to_handle_at(AT_FDCWD, path, handle, &mount_id, 0); + if (!ret && handle->handle_bytes == sizeof(id)) + memcpy(id.bytes, handle->f_handle, sizeof(id)); + free(handle); + + return ret ? 0 : id.id; +} + +static u64 now_ns(void) +{ + struct timespec ts; + + clock_gettime(CLOCK_MONOTONIC, &ts); + return (u64)ts.tv_sec * 1000000000ULL + ts.tv_nsec; +} + +/* Wait until *@ptr reaches @val, return its last value */ +static u64 wait_u64(u64 *ptr, u64 val) +{ + u64 deadline = now_ns() + WAIT_TIMEOUT_MS * 1000000ULL; + u64 v; + + while ((v = __atomic_load_n(ptr, __ATOMIC_ACQUIRE)) < val && + now_ns() < deadline) + usleep(1000); + return v; +} + +/* + * Wait until @pid sleeps. /proc/PID/stat starts with "PID (COMM) STATE", and + * COMM may contain spaces and ')', so STATE follows the last ')'. 'S' means + * interruptible sleep. + */ +static bool wait_pid_sleeping(pid_t pid, u64 timeout_ms) +{ + char path[64], buf[256]; + u64 deadline = now_ns() + timeout_ms * 1000000ULL; + + snprintf(path, sizeof(path), "/proc/%d/stat", pid); + while (now_ns() < deadline) { + int fd = open(path, O_RDONLY); + ssize_t n = -1; + char *state; + + if (fd >= 0) { + n = read(fd, buf, sizeof(buf) - 1); + close(fd); + } + if (n > 0) { + buf[n] = '\0'; + state = strrchr(buf, ')'); + if (state && !strncmp(state, ") S", 3)) + return true; + } + usleep(500); + } + return false; +} + +static void cleanup(void *ctx_ptr) +{ + struct sub_handoff_ctx *ctx = ctx_ptr; + + sub_handoff__destroy(ctx->sub_skel); + sub_handoff__destroy(ctx->root_skel); + if (ctx->cg_created) + cg_destroy(ctx->sub_cg); + free(ctx); +} + +static struct sub_handoff *open_and_load(u64 sub_cgid) +{ + struct sub_handoff *skel; + + skel = sub_handoff__open(); + if (!skel) + return NULL; + SCX_ENUM_INIT(skel); + skel->struct_ops.sub_handoff_ops->sub_cgroup_id = sub_cgid; + if (sub_handoff__load(skel)) { + sub_handoff__destroy(skel); + return NULL; + } + return skel; +} + +static enum scx_test_status setup(void **ctx_ptr) +{ + struct sub_handoff_ctx *ctx; + char root[PATH_MAX]; + + if (!__COMPAT_struct_has_field("sched_ext_ops", "sub_cgroup_id")) { + printf("SKIP: sub-schedulers not supported\n"); + return SCX_TEST_SKIP; + } + + if (cg_find_unified_root(root, sizeof(root), NULL)) { + printf("SKIP: cgroup v2 root not found\n"); + return SCX_TEST_SKIP; + } + + ctx = calloc(1, sizeof(*ctx)); + SCX_FAIL_IF(!ctx, "Failed to allocate context"); + + if (snprintf(ctx->sub_cg, sizeof(ctx->sub_cg), "%s/scx_sub_handoff_%d", + root, getpid()) >= (int)sizeof(ctx->sub_cg)) { + cleanup(ctx); + SCX_FAIL("Sub cgroup path too long"); + } + if (cg_create(ctx->sub_cg)) { + cleanup(ctx); + SCX_FAIL("Failed to create sub cgroup (%d)", errno); + } + ctx->cg_created = true; + + ctx->sub_cgid = get_cgroup_id(ctx->sub_cg); + if (!ctx->sub_cgid) { + cleanup(ctx); + SCX_FAIL("Failed to read sub cgroup ID"); + } + + ctx->root_skel = open_and_load(0); + if (!ctx->root_skel) { + cleanup(ctx); + SCX_FAIL("Failed to load root skel"); + } + + ctx->sub_skel = open_and_load(ctx->sub_cgid); + if (!ctx->sub_skel) { + cleanup(ctx); + SCX_FAIL("Failed to load sub skel"); + } + + *ctx_ptr = ctx; + return SCX_TEST_PASS; +} + +static int set_target(struct sub_handoff *skel, pid_t pid) +{ + LIBBPF_OPTS(bpf_test_run_opts, topts); + + skel->bss->target_vpid = pid; + return bpf_prog_test_run_opts(bpf_program__fd(skel->progs.set_target), + &topts) ?: topts.retval; +} + +static enum scx_test_status handoffs(struct sub_handoff_ctx *ctx, pid_t pid, + struct bpf_link **root_link, + struct bpf_link **sub_link) +{ + struct sub_handoff__bss *root = ctx->root_skel->bss; + struct sub_handoff__bss *sub = ctx->sub_skel->bss; + struct sched_param param = {}; + + SCX_FAIL_IF(set_target(ctx->root_skel, pid), + "Failed to set the root's target"); + SCX_FAIL_IF(set_target(ctx->sub_skel, pid), + "Failed to set the sub-scheduler's target"); + + *root_link = bpf_map__attach_struct_ops(ctx->root_skel->maps.sub_handoff_ops); + SCX_FAIL_IF(!*root_link, "Failed to attach root scheduler"); + + /* Step 1: Move the target to the sub-cgroup */ + SCX_FAIL_IF(cg_enter(ctx->sub_cg, pid), "Failed to move target into sub cgroup"); + + /* + * %SCX_OPS_SWITCH_PARTIAL takes only SCHED_EXT tasks. Switching the + * target makes the root, the only scheduler yet, enable it and set its + * bpf_priv. + */ + SCX_EQ(sched_setscheduler(pid, SCHED_EXT, ¶m), 0); + SCX_EQ(wait_u64(&root->nr_enable, 1), 1); + + /* Step 2: Attaching the sub-scheduler hands the target over */ + *sub_link = bpf_map__attach_struct_ops(ctx->sub_skel->maps.sub_handoff_ops); + if (!*sub_link && errno == EOPNOTSUPP) { + printf("SKIP: sub-schedulers not supported\n"); + return SCX_TEST_SKIP; + } + SCX_FAIL_IF(!*sub_link, "Failed to attach sub scheduler (%d)", errno); + + /* + * The sub-scheduler's ops.init_task() runs while the target is still on + * the root. It sees the root's bpf_priv but must not be able to set it. + * The root then exits the target, and its ops.exit_task() sets bpf_priv + * and dsq_vtime, which the kernel must clear afterwards. Both fields + * start at 0 on a clean handoff, so the sub-scheduler's ops.enable() + * must see neither. + */ + SCX_EQ(wait_u64(&sub->nr_enable, 1), 1); + /* The sub-scheduler's ops.init_task() found the target on the root */ + SCX_GT(root->sched_id, 0); + SCX_GT(sub->sched_id, root->sched_id); + SCX_EQ(sub->init_owner_id, root->sched_id); + SCX_EQ(sub->nr_init, 1); + SCX_EQ(sub->nr_init_priv, 1); + SCX_EQ(sub->nr_init_own, 0); + SCX_EQ(sub->nr_init_set, 0); + SCX_EQ(root->nr_exit, 1); + SCX_EQ(root->nr_exit_vtime_fail, 0); + SCX_EQ(root->nr_exit_set_fail, 0); + SCX_EQ(sub->nr_stale_priv, 0); + SCX_EQ(sub->nr_stale_vtime, 0); + + /* Step 3: Detaching the sub-scheduler hands the target back */ + SCX_ASSERT_ALIVE(ctx->sub_skel, uei); + bpf_link__destroy(*sub_link); + *sub_link = NULL; + + /* + * The same checks in the other direction, from the sub-scheduler's + * ops.exit_task() to the root's second ops.enable(). The root's counters + * also cover its first ops.init_task() and ops.enable(), when it loaded + * and in step 1, where the target had no bpf_priv yet. + */ + SCX_EQ(wait_u64(&root->nr_enable, 2), 2); + SCX_FAIL_IF(!SCX_TIMED_WAIT_UEI(ctx->sub_skel, uei, WAIT_TIMEOUT_MS), + "Sub-scheduler didn't exit"); + SCX_EQ(ctx->sub_skel->data->uei.kind, EXIT_KIND(SCX_EXIT_UNREG)); + SCX_EQ(root->nr_enable_set_fail, 0); + SCX_EQ(sub->nr_enable_set_fail, 0); + /* The root's second ops.init_task() found the target on the sub-scheduler */ + SCX_EQ(root->init_owner_id, sub->sched_id); + SCX_EQ(root->nr_init, 2); + SCX_EQ(root->nr_init_priv, 1); + SCX_EQ(root->nr_init_own, 0); + SCX_EQ(root->nr_init_set, 0); + SCX_EQ(sub->nr_exit, 1); + SCX_EQ(sub->nr_exit_vtime_fail, 0); + SCX_EQ(sub->nr_exit_set_fail, 0); + SCX_EQ(root->nr_stale_priv, 0); + SCX_EQ(root->nr_stale_vtime, 0); + SCX_EQ(root->nr_enable_not_own, 0); + SCX_EQ(sub->nr_enable_not_own, 0); + + /* Neither handoff may have made the root exit with an error */ + SCX_ASSERT_ALIVE(ctx->root_skel, uei); + return SCX_TEST_PASS; +} + +static enum scx_test_status run(void *ctx_ptr) +{ + struct sub_handoff_ctx *ctx = ctx_ptr; + struct bpf_link *root_link = NULL, *sub_link = NULL; + enum scx_test_status status; + int pfd[2]; + pid_t pid; + char c; + + SCX_FAIL_IF(pipe(pfd), "Failed to create pipe (%d)", errno); + + /* The target sleeps through the handoffs and exits when the pipe closes */ + pid = fork(); + if (pid < 0) { + close(pfd[0]); + close(pfd[1]); + SCX_FAIL("Failed to fork target (%d)", errno); + } + if (pid == 0) { + close(pfd[1]); + _exit(read(pfd[0], &c, 1) == 0 ? 0 : 1); + } + close(pfd[0]); + + if (wait_pid_sleeping(pid, WAIT_TIMEOUT_MS)) { + status = handoffs(ctx, pid, &root_link, &sub_link); + } else { + SCX_ERR("Target didn't go to sleep"); + status = SCX_TEST_FAIL; + } + + bpf_link__destroy(sub_link); + bpf_link__destroy(root_link); + close(pfd[1]); + waitpid(pid, NULL, 0); + return status; +} + +struct scx_test sub_handoff = { + .name = "sub_handoff", + .description = "Verify that sub-scheduler handoffs reset the task fields", + .setup = setup, + .run = run, + .cleanup = cleanup, +}; +REGISTER_SCX_TEST(&sub_handoff) -- 2.56.1