Sched_ext development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Changwoo Min" <changwoo@igalia.com>
Cc: sched-ext@lists.linux.dev
Subject: Re: [PATCH v2 8/8] selftests/sched_ext: Test task field reset across sub-scheduler handoffs
Date: Thu, 03 Dec 2026 05:32:55 +0000	[thread overview]
Message-ID: <sashiko-outbox-247202@kernel.org> (raw)
In-Reply-To: <20261203045618.982539-9-changwoo@igalia.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] selftests/sched_ext: unsynchronized child sleep triggers
  SCX_EXIT_ERROR_REENQ
- [Medium] selftests/sched_ext: unchecked kfunc returns allow vacuous
  sub_handoff passes
- [Medium] selftests/sched_ext: handoffs() never checks sub_skel UEI
  liveness or exit
- [Low] selftests/sched_ext: sub_handoff.c duplicates get_cgroup_id()
  and now_ns()
- [Low] selftests/sched_ext: sub_handoff_enable() increments nr_enable
  too early
--

commit 606e70f4ac2fdd8ea9df9beb5122bf3ee7426a80
Author: Changwoo Min <changwoo@igalia.com>

selftests/sched_ext: Test task field reset across sub-scheduler handoffs

This commit adds the sub_handoff sched_ext selftest to verify that
p->scx.bpf_priv and p->scx.dsq_vtime are cleared when a task moves
between a root scheduler and a sub-scheduler, and that ops.exit_task()
cannot set bpf_priv.

> 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 0000000000000..d7ce3f4e150c6
> --- /dev/null
> +++ b/tools/testing/selftests/sched_ext/sub_handoff.bpf.c

[ ... ]

> +void BPF_STRUCT_OPS(sub_handoff_enable, struct task_struct *p,
> +		    struct scx_enable_args *args)
> +{
> +	if (p->pid != target_pid)
> +		return;
> +
> +	__sync_fetch_and_add(&nr_enable, 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);
> +
> +	scx_bpf_task_set_priv(p, PRIV_MAGIC | (u32)p->pid);
> +}

[Severity: Low]
Should nr_enable be incremented at the end of sub_handoff_enable()
instead of before the stale-field checks?

wait_u64() polls nr_enable with __ATOMIC_ACQUIRE as the completion gate
before handoffs() asserts on nr_stale_priv and nr_stale_vtime.  If a
concurrent observer such as a BPF link iterator holds a transient
reference to sub_link during bpf_link__destroy(), bpf_scx_unreg() runs
asynchronously when that reference is dropped:

CPU0                              CPU1
handoffs()
  bpf_link__destroy(*sub_link)
  (deferred by extra link ref)
  wait_u64(&root->nr_enable, 2)
                                  bpf_link_put_deferred()
                                    ...
                                    sub_handoff_enable()
                                      nr_enable++ (now 2)
  reads nr_stale_priv == 0  <-- stale check has not run yet
  reads nr_stale_vtime == 0
                                      if (p->scx.bpf_priv)
                                        nr_stale_priv++

Userspace can then observe zero stale counts even when a regression
leaves bpf_priv or dsq_vtime uncleared.  Moving the nr_enable increment
to the end of sub_handoff_enable() fixes the release-acquire ordering.

> +
> +/*
> + * @p is still on this scheduler but no longer enabled. Neither write 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;
> +
> +	if (scx_bpf_task_set_priv(p, PRIV_MAGIC | (u32)p->pid))
> +		__sync_fetch_and_add(&nr_exit_set, 1);
> +	scx_bpf_task_set_dsq_vtime(p, VTIME_MAGIC);
> +}

[Severity: Medium]
Can the assertions in handoffs() pass vacuously if the setup writes fail
or ops.exit_task() never runs?

Both scx_bpf_task_set_priv() and scx_bpf_task_set_dsq_vtime() return a
bool, yet sub_handoff_enable() ignores the return of
scx_bpf_task_set_priv() and sub_handoff_exit_task() ignores the return
of scx_bpf_task_set_dsq_vtime().

sub_handoff_exit_task() also does not count invocations on target_pid.
If a regression causes either write to fail or skips ops.exit_task() on
handoff, the fields remain 0 and the zero-count assertions in handoffs()
still pass.

Tracking write failures and ops.exit_task() counts and asserting them in
handoffs() would ensure non-zero values were written before verifying
they were cleared.

> 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 0000000000000..18e20416c9110
> --- /dev/null
> +++ b/tools/testing/selftests/sched_ext/sub_handoff.c

[ ... ]

> +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;
> +}

[Severity: Low]
This isn't a bug, but get_cgroup_id() duplicates get_cgroup_id() in
sub_core_caps.c and cgroup_id() in cgroup_nr_cpus.c, and now_ns()
duplicates now_ns() in sub_core_caps.c.  Could these helpers be moved to
the shared util.c and util.h instead?

[ ... ]

> +	/* Step 3: Detaching the sub-scheduler hands the target back */
> +	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 stale
> +	 * counters also cover its first ops.enable() in step 1.
> +	 */
> +	SCX_EQ(wait_u64(&root->nr_enable, 2), 2);
> +	SCX_EQ(sub->nr_exit_set, 0);
> +	SCX_EQ(root->nr_stale_priv, 0);
> +	SCX_EQ(root->nr_stale_vtime, 0);
> +
> +	/* Neither handoff may have made the root exit with an error */
> +	SCX_ASSERT_ALIVE(ctx->root_skel, uei);
> +	return SCX_TEST_PASS;
> +}

[Severity: Medium]
Should handoffs() also verify that sub_skel stayed alive until step 3
and exited cleanly with SCX_EXIT_UNREG?

If sub_skel aborts with a runtime error during step 2, the kernel
disables it and moves target_pid back to root_skel.  root->nr_enable
still reaches 2 and all current assertions pass despite the error exit.

Checking SCX_ASSERT_ALIVE(ctx->sub_skel, uei) before
bpf_link__destroy(*sub_link) and EXIT_KIND(SCX_EXIT_UNREG) afterward
would catch unexpected sub_skel exits.

[ ... ]

> +	/* 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]);
> +
> +	status = handoffs(ctx, pid, &root_link, &sub_link);

[Severity: Medium]
Can the child still be runnable when step 2 attaches sub_link and
trigger SCX_EXIT_ERROR_REENQ on sub_skel?

run() enters handoffs() immediately after fork() without waiting for the
child to block in read().  Because sub_handoff_sub_attach() grants no
CPU capabilities to sub_skel and sub_handoff_enqueue() unconditionally
inserts into SCX_DSQ_LOCAL without SCX_ENQ_RESCUE, a runnable child on
sub_skel cannot be placed on a local DSQ:

CPU0                              CPU1
run()
  fork()
  handoffs()
    attach sub_link               (child still runnable, not in read())
    scx_sub_enable_workfn()
      scx_bypass(sch, false)
        sub_handoff_enqueue()
          scx_bpf_dsq_insert(..., SCX_DSQ_LOCAL, ...)
            __scx_resolve_local_dsq() -> reject_dsq
            scx_reenq_reject() (repeats 256x)  <-- SCX_EXIT_ERROR_REENQ

After SCX_REENQ_MAX_REPEAT rejections, scx_do_enqueue_task() aborts
sub_skel with SCX_EXIT_ERROR_REENQ and moves the target back to
root_skel before step 3 detaches it.  Waiting for the child to sleep
before calling handoffs() (as in sub_core_caps.c) avoids this race.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203045618.982539-1-changwoo@igalia.com?part=8

      reply	other threads:[~2026-12-03  5:32 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  4:56 [PATCH v2 0/8] sched_ext: Add p->scx.bpf_priv for O(1) task context lookup Changwoo Min
2026-12-03  4:56 ` [PATCH v2 1/8] sched_ext: Reset task fields after ops.exit_task() Changwoo Min
2026-12-04  6:10   ` Andrea Righi
2026-12-03  4:56 ` [PATCH v2 2/8] sched_ext: Mark a task enabled before ops.enable() Changwoo Min
2026-12-04  6:10   ` Andrea Righi
2026-12-03  4:56 ` [PATCH v2 3/8] sched_ext: Add scx_bpf_sched_id() and scx_bpf_task_sched_id() Changwoo Min
2026-12-03  5:14   ` sashiko-bot
2026-12-04  6:44   ` Andrea Righi
2026-12-03  4:56 ` [PATCH v2 4/8] selftests/sched_ext: Test " Changwoo Min
2026-12-03  5:14   ` sashiko-bot
2026-12-03  4:56 ` [PATCH v2 5/8] sched_ext: Add p->scx.bpf_priv for O(1) task context lookup Changwoo Min
2026-12-03  5:20   ` sashiko-bot
2026-12-03  4:56 ` [PATCH v2 6/8] sched_ext: scx_qmap: Cache task_ctx in p->scx.bpf_priv Changwoo Min
2026-12-03  5:19   ` sashiko-bot
2026-12-03  4:56 ` [PATCH v2 7/8] selftests/sched_ext: Test p->scx.bpf_priv across class changes Changwoo Min
2026-12-03  5:26   ` sashiko-bot
2026-12-03  4:56 ` [PATCH v2 8/8] selftests/sched_ext: Test task field reset across sub-scheduler handoffs Changwoo Min
2026-12-03  5:32   ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-247202@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=changwoo@igalia.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=sched-ext@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox