The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCHSET sched_ext/for-7.2] sched_ext: cid/cmask interface prep
@ 2026-06-03 17:21 Tejun Heo
  2026-06-03 17:21 ` [PATCH 1/3] sched_ext: Order single-cid cmask helpers as (cid, mask) Tejun Heo
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Tejun Heo @ 2026-06-03 17:21 UTC (permalink / raw)
  To: David Vernet, Andrea Righi, Changwoo Min
  Cc: Emil Tsalapatis, sched-ext, linux-kernel, Tejun Heo

Hello,

Three preparatory changes to the cid/cmask interface that landed in for-7.2
this cycle - a helper-argument reorder, two new helpers, and a kfunc
return-type change. Posting ahead of the merge window, in particular the
scx_bpf_kick_cid() return-type change in patch 3 - it changes a kfunc's
signature and is cleaner to settle before 7.2 ships.

- Patch 1 reorders the single-cid cmask helpers to (cid, mask), matching the
  kernel's index-first bit and cpumask predicates.
- Patch 2 adds scx_cmask_test() and scx_cmask_for_each_cid().
- Patch 3 switches scx_bpf_kick_cid() from void to s32 so future cap
  enforcement can surface failures. The cid interface has no external users
  yet, so the ABI change is safe now.

These are extracted from the in-development sub-scheduler series. Nothing
here depends on the rest of that work.

Base
----

sched_ext/for-7.2 (dad836524259)

 kernel/sched/ext.c                       | 13 ++++++++-----
 kernel/sched/ext_cid.c                   |  2 +-
 kernel/sched/ext_cid.h                   | 41 ++++++++++++++++++++++++++++----
 tools/sched_ext/include/scx/common.bpf.h |  2 +-
 4 files changed, 46 insertions(+), 12 deletions(-)

Thanks.

--
tejun

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/3] sched_ext: Order single-cid cmask helpers as (cid, mask)
  2026-06-03 17:21 [PATCHSET sched_ext/for-7.2] sched_ext: cid/cmask interface prep Tejun Heo
@ 2026-06-03 17:21 ` Tejun Heo
  2026-06-03 17:21 ` [PATCH 2/3] sched_ext: Add scx_cmask_test() and scx_cmask_for_each_cid() Tejun Heo
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Tejun Heo @ 2026-06-03 17:21 UTC (permalink / raw)
  To: David Vernet, Andrea Righi, Changwoo Min
  Cc: Emil Tsalapatis, sched-ext, linux-kernel, Tejun Heo

__scx_cmask_set(), __scx_cmask_contains() and __scx_cmask_word() take the
cmask first and the cid second. The kernel's bit and cpumask predicates put
the index first: test_bit(nr, addr), cpumask_test_cpu(cpu, mask). Reorder
the cmask helpers to (cid, mask) for consistency, ahead of new single-cid
ops added next. Mask-level ops (and/or/andnot/copy/subset/intersects) keep
(dst, src).

Signed-off-by: Tejun Heo <tj@kernel.org>
---
 kernel/sched/ext_cid.c |  2 +-
 kernel/sched/ext_cid.h | 10 +++++-----
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/kernel/sched/ext_cid.c b/kernel/sched/ext_cid.c
index 808c6390da5a..66944a7ef79d 100644
--- a/kernel/sched/ext_cid.c
+++ b/kernel/sched/ext_cid.c
@@ -267,7 +267,7 @@ void scx_cpumask_to_cmask(const struct cpumask *src, struct scx_cmask *dst)
 		s32 cid = __scx_cpu_to_cid(cpu);
 
 		if (cid >= 0)
-			__scx_cmask_set(dst, cid);
+			__scx_cmask_set(cid, dst);
 	}
 }
 
diff --git a/kernel/sched/ext_cid.h b/kernel/sched/ext_cid.h
index abea22ba2cc2..46fd8eda0443 100644
--- a/kernel/sched/ext_cid.h
+++ b/kernel/sched/ext_cid.h
@@ -147,13 +147,13 @@ static inline bool scx_is_cid_type(void)
 	return static_branch_unlikely(&__scx_is_cid_type);
 }
 
-static inline bool __scx_cmask_contains(const struct scx_cmask *m, u32 cid)
+static inline bool __scx_cmask_contains(u32 cid, const struct scx_cmask *m)
 {
 	return likely(cid >= m->base && cid < m->base + m->nr_cids);
 }
 
 /* Word in bits[] covering @cid. @cid must satisfy __scx_cmask_contains(). */
-static inline u64 *__scx_cmask_word(const struct scx_cmask *m, u32 cid)
+static inline u64 *__scx_cmask_word(u32 cid, const struct scx_cmask *m)
 {
 	return (u64 *)&m->bits[cid / 64 - m->base / 64];
 }
@@ -218,11 +218,11 @@ static inline void scx_cmask_reframe(struct scx_cmask *m, u32 base, u32 nr_cids)
 	m->nr_cids = nr_cids;
 }
 
-static inline void __scx_cmask_set(struct scx_cmask *m, u32 cid)
+static inline void __scx_cmask_set(u32 cid, struct scx_cmask *m)
 {
-	if (!__scx_cmask_contains(m, cid))
+	if (!__scx_cmask_contains(cid, m))
 		return;
-	*__scx_cmask_word(m, cid) |= BIT_U64(cid & 63);
+	*__scx_cmask_word(cid, m) |= BIT_U64(cid & 63);
 }
 
 #endif /* _KERNEL_SCHED_EXT_CID_H */
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/3] sched_ext: Add scx_cmask_test() and scx_cmask_for_each_cid()
  2026-06-03 17:21 [PATCHSET sched_ext/for-7.2] sched_ext: cid/cmask interface prep Tejun Heo
  2026-06-03 17:21 ` [PATCH 1/3] sched_ext: Order single-cid cmask helpers as (cid, mask) Tejun Heo
@ 2026-06-03 17:21 ` Tejun Heo
  2026-06-03 17:21 ` [PATCH 3/3] sched_ext: Make scx_bpf_kick_cid() return s32 Tejun Heo
  2026-06-03 18:41 ` [PATCHSET sched_ext/for-7.2] sched_ext: cid/cmask interface prep Andrea Righi
  3 siblings, 0 replies; 6+ messages in thread
From: Tejun Heo @ 2026-06-03 17:21 UTC (permalink / raw)
  To: David Vernet, Andrea Righi, Changwoo Min
  Cc: Emil Tsalapatis, sched-ext, linux-kernel, Tejun Heo

Add single-bit test and iterator over set cids in an scx_cmask.

Signed-off-by: Tejun Heo <tj@kernel.org>
---
 kernel/sched/ext_cid.h | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/kernel/sched/ext_cid.h b/kernel/sched/ext_cid.h
index 46fd8eda0443..d4c3ebba7416 100644
--- a/kernel/sched/ext_cid.h
+++ b/kernel/sched/ext_cid.h
@@ -225,4 +225,35 @@ static inline void __scx_cmask_set(u32 cid, struct scx_cmask *m)
 	*__scx_cmask_word(cid, m) |= BIT_U64(cid & 63);
 }
 
+/**
+ * scx_cmask_test - test whether @cid is set in @m
+ * @cid: cid to test
+ * @m: cmask to test
+ *
+ * Return %false if @cid is outside @m's active range. Otherwise return the
+ * bit's value. Read via READ_ONCE so callers can race set/clear writers.
+ */
+static inline bool scx_cmask_test(u32 cid, const struct scx_cmask *m)
+{
+	if (!__scx_cmask_contains(cid, m))
+		return false;
+	return READ_ONCE(*__scx_cmask_word(cid, m)) & BIT_U64(cid & 63);
+}
+
+/**
+ * scx_cmask_for_each_cid - iterate set cids in @m
+ * @cid: s32 loop var that receives each set cid in turn
+ * @m: cmask to iterate
+ *
+ * Visits set bits within @m's active range in ascending order. Relies on the
+ * head/tail-zero invariant of bits[], so no per-cid range check is needed.
+ */
+#define scx_cmask_for_each_cid(cid, m)						\
+	for (u64 __bs = (m)->base & ~63u, __wi = 0,				\
+		     __nw = SCX_CMASK_NR_WORDS((m)->nr_cids);			\
+	     __wi < __nw; __wi++)						\
+		for (u64 __w = READ_ONCE((m)->bits[__wi]);			\
+		     __w && ((cid) = __bs + __wi * 64 + __ffs64(__w), true);	\
+		     __w &= __w - 1)
+
 #endif /* _KERNEL_SCHED_EXT_CID_H */
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 3/3] sched_ext: Make scx_bpf_kick_cid() return s32
  2026-06-03 17:21 [PATCHSET sched_ext/for-7.2] sched_ext: cid/cmask interface prep Tejun Heo
  2026-06-03 17:21 ` [PATCH 1/3] sched_ext: Order single-cid cmask helpers as (cid, mask) Tejun Heo
  2026-06-03 17:21 ` [PATCH 2/3] sched_ext: Add scx_cmask_test() and scx_cmask_for_each_cid() Tejun Heo
@ 2026-06-03 17:21 ` Tejun Heo
  2026-06-03 18:38   ` Andrea Righi
  2026-06-03 18:41 ` [PATCHSET sched_ext/for-7.2] sched_ext: cid/cmask interface prep Andrea Righi
  3 siblings, 1 reply; 6+ messages in thread
From: Tejun Heo @ 2026-06-03 17:21 UTC (permalink / raw)
  To: David Vernet, Andrea Righi, Changwoo Min
  Cc: Emil Tsalapatis, sched-ext, linux-kernel, Tejun Heo

Switch scx_bpf_kick_cid() from void to s32 so future cap enforcement can
surface failures. cid interface is introduced in this cycle and has no
external users, so the ABI change is safe. Subsequent patches will add
-EPERM returns when the calling sub-sched lacks the required cap on the
target cid.

Signed-off-by: Tejun Heo <tj@kernel.org>
---
 kernel/sched/ext.c                       | 13 ++++++++-----
 tools/sched_ext/include/scx/common.bpf.h |  2 +-
 2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c
index 32ebbc351564..fedd501de67e 100644
--- a/kernel/sched/ext.c
+++ b/kernel/sched/ext.c
@@ -9405,9 +9405,10 @@ __bpf_kfunc void scx_bpf_kick_cpu(s32 cpu, u64 flags, const struct bpf_prog_aux
  * @flags: %SCX_KICK_* flags
  * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs
  *
- * cid-addressed equivalent of scx_bpf_kick_cpu().
+ * cid-addressed equivalent of scx_bpf_kick_cpu(). Return 0 on success,
+ * -errno otherwise.
  */
-__bpf_kfunc void scx_bpf_kick_cid(s32 cid, u64 flags, const struct bpf_prog_aux *aux)
+__bpf_kfunc s32 scx_bpf_kick_cid(s32 cid, u64 flags, const struct bpf_prog_aux *aux)
 {
 	struct scx_sched *sch;
 	s32 cpu;
@@ -9415,10 +9416,12 @@ __bpf_kfunc void scx_bpf_kick_cid(s32 cid, u64 flags, const struct bpf_prog_aux
 	guard(rcu)();
 	sch = scx_prog_sched(aux);
 	if (unlikely(!sch))
-		return;
+		return -ENODEV;
 	cpu = scx_cid_to_cpu(sch, cid);
-	if (cpu >= 0)
-		scx_kick_cpu(sch, cpu, flags);
+	if (cpu < 0)
+		return -EINVAL;
+	scx_kick_cpu(sch, cpu, flags);
+	return 0;
 }
 
 /**
diff --git a/tools/sched_ext/include/scx/common.bpf.h b/tools/sched_ext/include/scx/common.bpf.h
index 5f715d69cde6..9591a6e778ce 100644
--- a/tools/sched_ext/include/scx/common.bpf.h
+++ b/tools/sched_ext/include/scx/common.bpf.h
@@ -105,7 +105,7 @@ void scx_bpf_events(struct scx_event_stats *events, size_t events__sz) __ksym __
 s32 scx_bpf_cpu_to_cid(s32 cpu) __ksym __weak;
 s32 scx_bpf_cid_to_cpu(s32 cid) __ksym __weak;
 void scx_bpf_cid_topo(s32 cid, struct scx_cid_topo *out) __ksym __weak;
-void scx_bpf_kick_cid(s32 cid, u64 flags) __ksym __weak;
+s32 scx_bpf_kick_cid(s32 cid, u64 flags) __ksym __weak;
 s32 scx_bpf_task_cid(const struct task_struct *p) __ksym __weak;
 s32 scx_bpf_this_cid(void) __ksym __weak;
 struct task_struct *scx_bpf_cid_curr(s32 cid) __ksym __weak;
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 3/3] sched_ext: Make scx_bpf_kick_cid() return s32
  2026-06-03 17:21 ` [PATCH 3/3] sched_ext: Make scx_bpf_kick_cid() return s32 Tejun Heo
@ 2026-06-03 18:38   ` Andrea Righi
  0 siblings, 0 replies; 6+ messages in thread
From: Andrea Righi @ 2026-06-03 18:38 UTC (permalink / raw)
  To: Tejun Heo
  Cc: David Vernet, Changwoo Min, Emil Tsalapatis, sched-ext,
	linux-kernel

Hi Tejun,

On Wed, Jun 03, 2026 at 07:21:53AM -1000, Tejun Heo wrote:
> Switch scx_bpf_kick_cid() from void to s32 so future cap enforcement can
> surface failures. cid interface is introduced in this cycle and has no
> external users, so the ABI change is safe. Subsequent patches will add
> -EPERM returns when the calling sub-sched lacks the required cap on the
> target cid.
> 
> Signed-off-by: Tejun Heo <tj@kernel.org>
> ---
>  kernel/sched/ext.c                       | 13 ++++++++-----
>  tools/sched_ext/include/scx/common.bpf.h |  2 +-
>  2 files changed, 9 insertions(+), 6 deletions(-)
> 
> diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c
> index 32ebbc351564..fedd501de67e 100644
> --- a/kernel/sched/ext.c
> +++ b/kernel/sched/ext.c
> @@ -9405,9 +9405,10 @@ __bpf_kfunc void scx_bpf_kick_cpu(s32 cpu, u64 flags, const struct bpf_prog_aux
>   * @flags: %SCX_KICK_* flags
>   * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs
>   *
> - * cid-addressed equivalent of scx_bpf_kick_cpu().
> + * cid-addressed equivalent of scx_bpf_kick_cpu(). Return 0 on success,
> + * -errno otherwise.
>   */
> -__bpf_kfunc void scx_bpf_kick_cid(s32 cid, u64 flags, const struct bpf_prog_aux *aux)
> +__bpf_kfunc s32 scx_bpf_kick_cid(s32 cid, u64 flags, const struct bpf_prog_aux *aux)
>  {
>  	struct scx_sched *sch;
>  	s32 cpu;
> @@ -9415,10 +9416,12 @@ __bpf_kfunc void scx_bpf_kick_cid(s32 cid, u64 flags, const struct bpf_prog_aux
>  	guard(rcu)();
>  	sch = scx_prog_sched(aux);
>  	if (unlikely(!sch))
> -		return;
> +		return -ENODEV;
>  	cpu = scx_cid_to_cpu(sch, cid);
> -	if (cpu >= 0)
> -		scx_kick_cpu(sch, cpu, flags);
> +	if (cpu < 0)
> +		return -EINVAL;

Right now scx_cid_to_cpu() can only return -EINVAL, but should we propagate the
error to the caller and return cpu here (in case we add more errors in the
future)?

Thanks,
-Andrea

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCHSET sched_ext/for-7.2] sched_ext: cid/cmask interface prep
  2026-06-03 17:21 [PATCHSET sched_ext/for-7.2] sched_ext: cid/cmask interface prep Tejun Heo
                   ` (2 preceding siblings ...)
  2026-06-03 17:21 ` [PATCH 3/3] sched_ext: Make scx_bpf_kick_cid() return s32 Tejun Heo
@ 2026-06-03 18:41 ` Andrea Righi
  3 siblings, 0 replies; 6+ messages in thread
From: Andrea Righi @ 2026-06-03 18:41 UTC (permalink / raw)
  To: Tejun Heo
  Cc: David Vernet, Changwoo Min, Emil Tsalapatis, sched-ext,
	linux-kernel

On Wed, Jun 03, 2026 at 07:21:50AM -1000, Tejun Heo wrote:
> Hello,
> 
> Three preparatory changes to the cid/cmask interface that landed in for-7.2
> this cycle - a helper-argument reorder, two new helpers, and a kfunc
> return-type change. Posting ahead of the merge window, in particular the
> scx_bpf_kick_cid() return-type change in patch 3 - it changes a kfunc's
> signature and is cleaner to settle before 7.2 ships.
> 
> - Patch 1 reorders the single-cid cmask helpers to (cid, mask), matching the
>   kernel's index-first bit and cpumask predicates.
> - Patch 2 adds scx_cmask_test() and scx_cmask_for_each_cid().
> - Patch 3 switches scx_bpf_kick_cid() from void to s32 so future cap
>   enforcement can surface failures. The cid interface has no external users
>   yet, so the ABI change is safe now.
> 
> These are extracted from the in-development sub-scheduler series. Nothing
> here depends on the rest of that work.

I sent a comment about patch 3, other than that looks good to me (and thanks for
the cid / mask reordering).

Reviewed-by: Andrea Righi <arighi@nvidia.com>

Thanks,
-Andrea

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-06-03 18:41 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-03 17:21 [PATCHSET sched_ext/for-7.2] sched_ext: cid/cmask interface prep Tejun Heo
2026-06-03 17:21 ` [PATCH 1/3] sched_ext: Order single-cid cmask helpers as (cid, mask) Tejun Heo
2026-06-03 17:21 ` [PATCH 2/3] sched_ext: Add scx_cmask_test() and scx_cmask_for_each_cid() Tejun Heo
2026-06-03 17:21 ` [PATCH 3/3] sched_ext: Make scx_bpf_kick_cid() return s32 Tejun Heo
2026-06-03 18:38   ` Andrea Righi
2026-06-03 18:41 ` [PATCHSET sched_ext/for-7.2] sched_ext: cid/cmask interface prep Andrea Righi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox