From: Kuba Piecuch <jpiecuch@google.com>
To: Cheng-Yang Chou <yphbchou0911@gmail.com>,
<sched-ext@lists.linux.dev>, Tejun Heo <tj@kernel.org>,
David Vernet <void@manifault.com>,
Andrea Righi <arighi@nvidia.com>,
Changwoo Min <changwoo@igalia.com>
Cc: Ching-Chun Huang <jserv@ccns.ncku.edu.tw>,
Chia-Ping Tsai <chia7712@gmail.com>,
Kuba Piecuch <jpiecuch@google.com>
Subject: Re: [PATCH] sched_ext: Add cookie API for early qseq capture
Date: Wed, 06 May 2026 10:08:11 +0000 [thread overview]
Message-ID: <DIBHYUZEPZC8.2X00QWHIOZP8R@google.com> (raw)
In-Reply-To: <20260506075925.371138-1-yphbchou0911@gmail.com>
Hi Cheng-Yang,
On Wed May 6, 2026 at 7:59 AM UTC, Cheng-Yang Chou wrote:
> @@ -8505,13 +8507,67 @@ __bpf_kfunc void scx_bpf_dsq_insert_vtime(struct task_struct *p, u64 dsq_id,
> scx_dsq_insert_vtime(sch, p, dsq_id, slice, vtime, enq_flags);
> }
>
> +/**
> + * scx_bpf_task_get_cookie - Get an opaque dispatch cookie for a task
> + * @p: task_struct to read cookie from
> + *
> + * Returns an opaque u64 cookie encoding @p's current qseq. Call this
> + * before pre-dispatch validity checks and pass the result to
> + * scx_bpf_dsq_insert_with_cookie() to extend the qseq protection window.
This is just my opinion, but I thought the point of introducing cookies is to
not have to expose qseq as a concept to the BPF schedulers (or people writing
them). This includes comments documenting the kfuncs.
Wouldn't it be better if we just focus on describing the semantics without
mentioning qseq? Perhaps something like:
Returns an opaque u64 dispatch cookie. Pass the cookie to
scx_bpf_dsq_insert_with_cookie() to extend the time window during which
sched_ext will detect racing dequeues/enqueues of the task being dispatched.
The extended time window begins with the call to scx_bpf_task_get_cookie()
and ends at the same point as for dispatches without cookies, i.e. at the
point where sched_ext attempts to finish the dispatch.
> + *
> + * For schedulers that do not implement properly synchronized dequeue only.
"only" seems quite strong here. How about "This API is intended for schedulers
that do not implement properly synchronized dequeue"?
> + */
> +__bpf_kfunc u64 scx_bpf_task_get_cookie(struct task_struct *p)
> +{
> + return atomic_long_read(&p->scx.ops_state) & SCX_OPSS_QSEQ_MASK;
> +}
> +
> +/**
> + * scx_bpf_dsq_insert_with_cookie - Insert a task using an early-captured cookie
> + * @p: task_struct to insert
> + * @dsq_id: DSQ to insert into
> + * @enq_flags: SCX_ENQ_*
> + * @cookie: cookie from scx_bpf_task_get_cookie()
> + * @aux: implicit BPF argument
> + *
> + * Like scx_bpf_dsq_insert() with slice=0, but uses @cookie's qseq instead
> + * of re-reading ops_state at insert time. A stale cookie causes
> + * finish_dispatch() to silently discard the dispatch. Use
> + * scx_bpf_task_set_slice() to set a non-default slice.
> + *
> + * Returns %true on success, %false on failure.
> + */
> +__bpf_kfunc bool scx_bpf_dsq_insert_with_cookie(struct task_struct *p,
> + u64 dsq_id, u64 enq_flags,
> + u64 cookie,
> + const struct bpf_prog_aux *aux)
> +{
> + struct scx_sched *sch;
> +
> + guard(rcu)();
> + sch = scx_prog_sched(aux);
> + if (unlikely(!sch))
> + return false;
> +
> + if (!scx_dsq_insert_preamble(sch, p, dsq_id, &enq_flags))
> + return false;
> +
> + p->scx.slice = p->scx.slice ?: 1;
> +
> + scx_dsq_insert_commit(sch, p, dsq_id, enq_flags, (unsigned long)cookie);
> +
> + return true;
> +}
> +
> __bpf_kfunc_end_defs();
>
> BTF_KFUNCS_START(scx_kfunc_ids_enqueue_dispatch)
> BTF_ID_FLAGS(func, scx_bpf_dsq_insert, KF_IMPLICIT_ARGS | KF_RCU)
> BTF_ID_FLAGS(func, scx_bpf_dsq_insert___v2, KF_IMPLICIT_ARGS | KF_RCU)
> +BTF_ID_FLAGS(func, scx_bpf_dsq_insert_with_cookie, KF_IMPLICIT_ARGS | KF_RCU)
> BTF_ID_FLAGS(func, __scx_bpf_dsq_insert_vtime, KF_IMPLICIT_ARGS | KF_RCU)
> BTF_ID_FLAGS(func, scx_bpf_dsq_insert_vtime, KF_RCU)
> +BTF_ID_FLAGS(func, scx_bpf_task_get_cookie, KF_RCU)
> BTF_KFUNCS_END(scx_kfunc_ids_enqueue_dispatch)
>
> static const struct btf_kfunc_id_set scx_kfunc_set_enqueue_dispatch = {
> @@ -10181,6 +10237,7 @@ BTF_ID_FLAGS(func, scx_bpf_put_cpumask, KF_RELEASE)
> BTF_ID_FLAGS(func, scx_bpf_task_running, KF_RCU)
> BTF_ID_FLAGS(func, scx_bpf_task_cpu, KF_RCU)
> BTF_ID_FLAGS(func, scx_bpf_task_cid, KF_RCU)
> +BTF_ID_FLAGS(func, scx_bpf_task_get_cookie, KF_RCU)
> BTF_ID_FLAGS(func, scx_bpf_cpu_rq, KF_IMPLICIT_ARGS)
> BTF_ID_FLAGS(func, scx_bpf_locked_rq, KF_IMPLICIT_ARGS | KF_RET_NULL)
> BTF_ID_FLAGS(func, scx_bpf_cpu_curr, KF_IMPLICIT_ARGS | KF_RET_NULL | KF_RCU_PROTECTED)
Thank you for the patch!
Could you add a simple selftest for the two new kfuncs?
Thanks,
Kuba
next prev parent reply other threads:[~2026-05-06 10:08 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-06 7:59 [PATCH] sched_ext: Add cookie API for early qseq capture Cheng-Yang Chou
2026-05-06 10:08 ` Kuba Piecuch [this message]
2026-05-06 12:39 ` Cheng-Yang Chou
2026-05-06 10:58 ` sashiko-bot
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=DIBHYUZEPZC8.2X00QWHIOZP8R@google.com \
--to=jpiecuch@google.com \
--cc=arighi@nvidia.com \
--cc=changwoo@igalia.com \
--cc=chia7712@gmail.com \
--cc=jserv@ccns.ncku.edu.tw \
--cc=sched-ext@lists.linux.dev \
--cc=tj@kernel.org \
--cc=void@manifault.com \
--cc=yphbchou0911@gmail.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.