sched_ext: cid/cmask interface prep
Subsystems (2)
Tejun Heo
reviewerPATCH 1/1 sched_ext: cid/cmask interface prep
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
Tejun Heo
reviewerPATCH 1/3 sched_ext: Order single-cid cmask helpers as (cid, mask)
__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).
--- kernel/sched/ext_cid.c | 2 +- kernel/sched/ext_cid.h | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-)
kernel/sched/ext_cid.c +1 -1
@@ -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); } }
kernel/sched/ext_cid.h +5 -5
@@ -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 */
Signature · 1 line
2.54.0
Tejun Heo
reviewerPATCH 2/3 sched_ext: Add scx_cmask_test() and scx_cmask_for_each_cid()
In reply to Tejun HeoAdd single-bit test and iterator over set cids in an scx_cmask.
--- kernel/sched/ext_cid.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)
kernel/sched/ext_cid.h +31 -0
@@ -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 */
Signature · 1 line
2.54.0
Tejun Heo
reviewerPATCH 3/3 sched_ext: Make scx_bpf_kick_cid() return s32
In reply to Tejun HeoSwitch 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.
--- kernel/sched/ext.c | 13 ++++++++----- tools/sched_ext/include/scx/common.bpf.h | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-)
kernel/sched/ext.c +8 -5
@@ -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_auxguard (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 ; }/**
tools/sched_ext/include/scx/common.bpf.h +1 -1
@@ -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 ;
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH 1/3] sched_ext: Order single-cid cmask helpers as (cid, mask)
In reply to Tejun HeoThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Incomplete refactoring of cmask helpers argument order, causing an API divergence between kernel…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Incomplete refactoring of cmask helpers argument order, causing an API divergence between kernel and BPF implementations. --
commit cae9caa6c3655ef0cb4c126820c635529b2d5e19
Author: Tejun Heo <[email protected]>
sched_ext: Order single-cid cmask helpers as (cid, mask)
This commit reorders the parameters of single-cid cmask helpers to take the cid before the mask. This change is intended to make the parameters consistent with standard kernel predicates like cpumask_test_cpu(cpu, mask).
Quoted 4 lines
kernel/sched/ext_cid.h +0
-0
[ ... ]
Quoted 4 lines
kernel/sched/ext_cid.h +0
-0@@ -218,11 +218,11 @@ static inline void scx_cmask_reframe(struct scx_cmask *m, u32 base, u32 nr_cids) m->nr_cids = nr_cids; }
kernel/sched/ext_cid.h static inline void scx_cmask_reframe(struct scx_cmask *m, u32 base, u32 nr_cids) · line 227
Quoted 10 lines
kernel/sched/ext_cid.h +3
-3-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 ); }
[Severity: Medium]
The commit message states the intent to reorder the cmask helpers for consistency, but does this refactoring inadvertently omit the BPF-facing helpers?
The kernel-side helpers in ext_cid.h are updated here to use the new (cid, mask) order, but the equivalent functions in tools/sched_ext/include/scx/cid.bpf.h appear to still use the old (mask, cid) ordering:
tools/sched_ext/include/scx/cid.bpf.h:
static __always_inline void cmask_set(struct scx_cmask __arena *m, u32 cid) static __always_inline bool cmask_test(const struct scx_cmask __arena *m, u32 cid) static __always_inline bool __cmask_contains(const struct scx_cmask __arena *m, u32 cid)
Could this introduce an API divergence between the kernel and BPF implementations for developers writing BPF schedulers?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewRe: [PATCH 2/3] sched_ext: Add scx_cmask_test() and scx_cmask_for_each_cid()
In reply to Tejun HeoThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] scx_cmask_for_each_cid yields out-of-bounds phantom CIDs due to an overestimated loop bound and…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] scx_cmask_for_each_cid yields out-of-bounds phantom CIDs due to an overestimated loop bound and stale padding words. --
commit edd3ffa1eb6aa37c70fee16c5770f542f1484a83
Author: Tejun Heo <[email protected]>
Subject: sched_ext: Add scx_cmask_test() and scx_cmask_for_each_cid()
Add single-bit test and iterator over set cids in an scx_cmask.
Quoted 4 lines
kernel/sched/ext_cid.h +0
-0
[ ... ]
Quoted 36 lines
kernel/sched/ext_cid.h +25
-3@@ -218,11 +218,42 @@ 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 ); } +/** +* 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.
kernel/sched/ext_cid.h static inline void scx_cmask_reframe(struct scx_cmask *m, u32 base, u32 nr_cids) · line 259
Quoted 10 lines
kernel/sched/ext_cid.h +9
-0+*/ +#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 */
[Severity: High]
Does this iterator yield phantom CIDs outside of the mask's active range?
Since SCX_CMASK_NR_WORDS() overestimates worst-case capacity (e.g. for nr_cids=1, it evaluates to 2), __nw will be larger than necessary. The iterator lacks a strict cid < base + nr_cids bound check to compensate.
When scx_cmask_reframe() shrinks a mask, it clears only m->bits[0] and m->bits[last_word]:
kernel/sched/ext_cid.h:scx_cmask_reframe() {
if (nr_cids) {
u32 last_word = ((base & 63) + nr_cids - 1) / 64;
m->bits[0] = 0;
m->bits[last_word] = 0;
}
...
}Any words beyond last_word remain uncleared. If a mask is reframed to a smaller size (like nr_cids=1), SCX_CMASK_NR_WORDS(1) is 2, so the loop bound __nw is 2. The iterator will process the stale, uncleared bits in m->bits[1], yielding CIDs outside the active range.
Similarly, if nr_cids is 0, scx_cmask_reframe() skips clearing entirely, but SCX_CMASK_NR_WORDS(0) is 1. The iterator will traverse m->bits[0] and yield CIDs for an empty mask.
Is a strict boundary check inside the loop needed?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Andrea Righi
reviewerRe: [PATCH 3/3] sched_ext: Make scx_bpf_kick_cid() return s32
In reply to Tejun HeoHi Tejun,
Quoted 30 lines · 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-EPERMreturns when the calling sub-sched lacks the required cap on the target cid.Signed-off-by Tejun Heo <[email protected]>--- kernel/sched/ext.c | 13 ++++++++----- tools/sched_ext/include/scx/common.bpf.h | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-)kernel/sched/ext.c +3
-2@@ -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;
kernel/sched/ext.c __bpf_kfunc void scx_bpf_kick_cid(s32 cid, u64 flags, const struct bpf_prog_aux · line 9422
Quoted 10 lines
kernel/sched/ext.c +3
-3@@ -9415,10 +9416,12 @@ __bpf_kfunc void scx_bpf_kick_cid(s32 cid, u64 flags, const struct bpf_prog_auxguard (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
Quoted 15 lines · 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 addsscx_cmask_test()andscx_cmask_for_each_cid().
- Patch 3 switchesscx_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.
Quoted 2 lines
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).
Thanks,
-Andrea