sched_ext: cmask improvements
Subsystems (2)
Tejun Heo
reviewerPATCH RESEND 1/1 sched_ext: cmask improvements
Hello,
Resend with the correct cover letter. The earlier posting ([email protected]) went out under a stale cover from an unrelated draft; the three patches themselves were the right ones and are unchanged in this resend. Apologies for the noise.
Three patches for cmask: tidy active-range bookkeeping and add the mask-on-mask op helpers the sub-sched series will use.
Not backward-compat with the current scx_cmask layout/API, but cmask landed in for-7.2 and hasn't been released; scx_qmap is the only user.
0001 - sched_ext: Rename scx_cmask.nr_bits to nr_cids 0002 - sched_ext: Track bits[] storage size in struct scx_cmask 0003 - sched_ext: Add cmask mask ops
Based on sched_ext/for-7.2 (c9017d335aab).
Git tree: git://git.kernel.org/pub/scm/linux/kernel/git/tj/sched_ext.git cmask-prep
kernel/sched/ext_cid.c | 307 +++++++++++++++++++++++++++++++++- kernel/sched/ext_cid.h | 71 +++++++- kernel/sched/ext_types.h | 64 +++++-- tools/sched_ext/include/scx/cid.bpf.h | 117 +++++++++---- 4 files changed, 506 insertions(+), 53 deletions(-)
Thanks.
--
tejun
Tejun Heo
reviewerPATCH 1/3 sched_ext: Rename scx_cmask.nr_bits to nr_cids
struct scx_cmask is a base-windowed bitmap over cid space. Each bit represents one cid, so the count of active bits is the count of cids. The sibling struct scx_cid_shard already uses nr_cids. Rename as a prep so the following patches that grow the cmask API can use the consistent name.
--- kernel/sched/ext_cid.h | 8 ++-- kernel/sched/ext_types.h | 12 ++--- tools/sched_ext/include/scx/cid.bpf.h | 64 +++++++++++++-------------- 3 files changed, 42 insertions(+), 42 deletions(-)
kernel/sched/ext_cid.h +4 -4
@@ -138,7 +138,7 @@ static inline bool scx_is_cid_type(void)static inline bool __scx_cmask_contains (const struct scx_cmask *m,u32 cid) { -return likely (cid >= m->base && cid < m->base + m->nr_bits); +return likely (cid >= m->base && cid < m->base + m->nr_cids); }/* Word in bits[] covering @cid. @cid must satisfy __scx_cmask_contains(). */
@@ -147,11 +147,11 @@ static inline u64 *__scx_cmask_word(const struct scx_cmask *m, u32 cid)return (u64 *)&m->bits[cid /64 - m->base /64 ]; } -static inline void scx_cmask_init (struct scx_cmask *m,u32 base,u32 nr_bits) +static inline void scx_cmask_init (struct scx_cmask *m,u32 base,u32 nr_cids) { m->base = base; - m->nr_bits = nr_bits; -memset (m->bits,0 ,SCX_CMASK_NR_WORDS (nr_bits) *sizeof (u64)); + m->nr_cids = nr_cids; +memset (m->bits,0 ,SCX_CMASK_NR_WORDS (nr_cids) *sizeof (u64)); }static inline void __scx_cmask_set (struct scx_cmask *m,u32 cid)
kernel/sched/ext_types.h +6 -6
@@ -67,10 +67,10 @@ struct scx_cid_topo {* cmask: variable-length, base-windowed bitmap over cid space * ----------------------------------------------------------- * -* A cmask covers the cid range [base, base + nr_bits). bits[] is aligned to the +* A cmask covers the cid range [base, base + nr_cids). bits[] is aligned to the * global 64-cid grid: bits[0] spans [base & ~63, (base & ~63) + 64), so the * first (base & 63) bits of bits[0] are head padding and any tail past base + -* nr_bits is tail padding. Both must stay zero for the lifetime of the mask; +* nr_cids is tail padding. Both must stay zero for the lifetime of the mask; * all mutating helpers preserve that invariant. * * Grid alignment means two cmasks always address bits[] against the same global
@@ -82,21 +82,21 @@ struct scx_cid_topo {*/ struct scx_cmask {u32 base; -u32 nr_bits; +u32 nr_cids; DECLARE_FLEX_ARRAY(u64, bits); };/* -* Number of u64 words of bits[] storage that covers @nr_bits regardless of base +* Number of u64 words of bits[] storage that covers @nr_cids regardless of base * alignment. The +1 absorbs up to 63 bits of head padding when base is not * 64-aligned - always allocating one extra word beats branching on base or * splitting the compute. */ -#define SCX_CMASK_NR_WORDS (nr_bits) (((nr_bits) + 63) / 64 + 1) +#define SCX_CMASK_NR_WORDS (nr_cids) (((nr_cids) + 63) / 64 + 1)/* * Define an on-stack cmask for up to @cap_bits. @name is a struct scx_cmask * -* aliasing zero-initialized storage; call scx_cmask_init() to set base/nr_bits. +* aliasing zero-initialized storage; call scx_cmask_init() to set base/nr_cids. */ #define SCX_CMASK_DEFINE (name, cap_bits) \ DEFINE_RAW_FLEX(struct scx_cmask, name, bits, SCX_CMASK_NR_WORDS(cap_bits))
tools/sched_ext/include/scx/cid.bpf.h +32 -32
191 lines not on this page · raw message
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH 1/3] sched_ext: Rename scx_cmask.nr_bits to nr_cids
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The struct field 'nr_bits' in 'struct scx_cmask' was renamed to 'nr_cids', but the function…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The struct field 'nr_bits' in 'struct scx_cmask' was renamed to 'nr_cids', but the function 'cmask_copy_from_kernel()' in 'tools/sched_ext/include/scx/cid.bpf.h' still references 'src->nr_bits' and 'dst->nr_bits'. This will cause a compilation error for BPF programs including this header. --
commit 10e6d97c74e4139adeb947156f1940e0b2848eb4
Author: Tejun Heo <[email protected]>
sched_ext: Rename scx_cmask.nr_bits to nr_cids
This commit renames the nr_bits field in struct scx_cmask to nr_cids. This aligns the naming with the sibling struct scx_cid_shard to provide a consistent API for future cmask patches.
kernel/sched/ext_types.h struct scx_cid_topo { · line 87
Quoted 12 lines
kernel/sched/ext_types.h +1
-1@@ -82,21 +82,21 @@ struct scx_cid_topo {*/ struct scx_cmask {u32 base; -u32 nr_bits; +u32 nr_cids; DECLARE_FLEX_ARRAY(u64, bits); };
Since the field is renamed to nr_cids here, does cmask_copy_from_kernel() in tools/sched_ext/include/scx/cid.bpf.h also need to be updated?
It appears to still reference the old nr_bits field:
tools/sched_ext/include/scx/cid.bpf.h:cmask_copy_from_kernel() {
...
if (bpf_probe_read_kernel(&nr_bits, sizeof(nr_bits), &src->nr_bits)) {
scx_bpf_error("probe-read cmask->nr_bits failed");
return;
}
if (nr_bits > dst->nr_bits) {
scx_bpf_error("src cmask nr_bits=%u exceeds dst nr_bits=%u",
nr_bits, dst->nr_bits);
return;
}
...
}Will this cause a compilation error for BPF programs including this header?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Tejun Heo
reviewerPATCH v2 1/3 sched_ext: Rename scx_cmask.nr_bits to nr_cids
In reply to Tejun Heostruct scx_cmask is a base-windowed bitmap over cid space. Each bit represents one cid, so the count of active bits is the count of cids. The sibling struct scx_cid_shard already uses nr_cids. Rename as a prep so the following patches that grow the cmask API can use the consistent name.
v2: Also rename src->nr_bits / dst->nr_bits in
cmask_copy_from_kernel(). (sashiko AI)
--- kernel/sched/ext_cid.h | 8 +-- kernel/sched/ext_types.h | 12 ++--- tools/sched_ext/include/scx/cid.bpf.h | 78 +++++++++++++-------------- 3 files changed, 49 insertions(+), 49 deletions(-)
kernel/sched/ext_cid.h +4 -4
@@ -138,7 +138,7 @@ static inline bool scx_is_cid_type(void)static inline bool __scx_cmask_contains (const struct scx_cmask *m,u32 cid) { -return likely (cid >= m->base && cid < m->base + m->nr_bits); +return likely (cid >= m->base && cid < m->base + m->nr_cids); }/* Word in bits[] covering @cid. @cid must satisfy __scx_cmask_contains(). */
@@ -147,11 +147,11 @@ static inline u64 *__scx_cmask_word(const struct scx_cmask *m, u32 cid)return (u64 *)&m->bits[cid /64 - m->base /64 ]; } -static inline void scx_cmask_init (struct scx_cmask *m,u32 base,u32 nr_bits) +static inline void scx_cmask_init (struct scx_cmask *m,u32 base,u32 nr_cids) { m->base = base; - m->nr_bits = nr_bits; -memset (m->bits,0 ,SCX_CMASK_NR_WORDS (nr_bits) *sizeof (u64)); + m->nr_cids = nr_cids; +memset (m->bits,0 ,SCX_CMASK_NR_WORDS (nr_cids) *sizeof (u64)); }static inline void __scx_cmask_set (struct scx_cmask *m,u32 cid)
kernel/sched/ext_types.h +6 -6
@@ -67,10 +67,10 @@ struct scx_cid_topo {* cmask: variable-length, base-windowed bitmap over cid space * ----------------------------------------------------------- * -* A cmask covers the cid range [base, base + nr_bits). bits[] is aligned to the +* A cmask covers the cid range [base, base + nr_cids). bits[] is aligned to the * global 64-cid grid: bits[0] spans [base & ~63, (base & ~63) + 64), so the * first (base & 63) bits of bits[0] are head padding and any tail past base + -* nr_bits is tail padding. Both must stay zero for the lifetime of the mask; +* nr_cids is tail padding. Both must stay zero for the lifetime of the mask; * all mutating helpers preserve that invariant. * * Grid alignment means two cmasks always address bits[] against the same global
@@ -82,21 +82,21 @@ struct scx_cid_topo {*/ struct scx_cmask {u32 base; -u32 nr_bits; +u32 nr_cids; DECLARE_FLEX_ARRAY(u64, bits); };/* -* Number of u64 words of bits[] storage that covers @nr_bits regardless of base +* Number of u64 words of bits[] storage that covers @nr_cids regardless of base * alignment. The +1 absorbs up to 63 bits of head padding when base is not * 64-aligned - always allocating one extra word beats branching on base or * splitting the compute. */ -#define SCX_CMASK_NR_WORDS (nr_bits) (((nr_bits) + 63) / 64 + 1) +#define SCX_CMASK_NR_WORDS (nr_cids) (((nr_cids) + 63) / 64 + 1)/* * Define an on-stack cmask for up to @cap_bits. @name is a struct scx_cmask * -* aliasing zero-initialized storage; call scx_cmask_init() to set base/nr_bits. +* aliasing zero-initialized storage; call scx_cmask_init() to set base/nr_cids. */ #define SCX_CMASK_DEFINE (name, cap_bits) \ DEFINE_RAW_FLEX(struct scx_cmask, name, bits, SCX_CMASK_NR_WORDS(cap_bits))
tools/sched_ext/include/scx/cid.bpf.h +39 -39
223 lines not on this page · raw message
Signature · 1 line
2.54.0
Tejun Heo
reviewerPATCH 2/3 sched_ext: Track bits[] storage size in struct scx_cmask
In reply to Tejun Heoscx_cmask carries @base and @nr_cids but not the bits[] allocation size, so helpers reshaping the active range have no way to check it fits and later kfuncs taking caller-provided storage can't validate it.
Add @alloc_words (u64 word count) annotated with __counted_by, and split the bit-range API into three helpers:
- SCX_CMASK_DEFINE() / __SCX_CMASK_DEFINE() define an on-stack cmask, the latter taking an explicit capacity for oversized storage. SCX_CMASK_DEFINE_SHARD() is a thin wrapper that always reserves SCX_CID_SHARD_MAX_CPUS bits of storage.
- scx_cmask_init() / __scx_cmask_init() initialize a cmask, with the same tight-vs-explicit split.
- scx_cmask_reframe() reshapes the active range without resizing storage.
The BPF mirror (cmask_init / __cmask_init / cmask_reframe) gets the same shape.
Add scx_cmask_clear() and scx_cmask_fill() to zero and set the active-range bits respectively. scx_cpumask_to_cmask() uses scx_cmask_clear(); scx_cmask_init() would otherwise re-write @alloc_words on every call.
A later patch uses @alloc_words in scx_cmask_ref_shard() to refuse output storage that can't hold the requested shard.
--- kernel/sched/ext_cid.c | 48 ++++++++++++++++++++-- kernel/sched/ext_cid.h | 57 ++++++++++++++++++++++++++- kernel/sched/ext_types.h | 56 +++++++++++++++++++++----- tools/sched_ext/include/scx/cid.bpf.h | 57 +++++++++++++++++++++++++-- 4 files changed, 202 insertions(+), 16 deletions(-)
kernel/sched/ext_cid.c +45 -3
@@ -222,19 +222,61 @@ s32 scx_cid_init(struct scx_sched *sch)return 0 ; } +/** +* scx_cmask_clear - Zero every bit in @m's active range +* @m: cmask to clear +* +* Storage past the active range is left as is. +*/ +void scx_cmask_clear (struct scx_cmask *m) +{ +u32 nr_words; + +if (!m->nr_cids) +return ; + nr_words = (m->base + m->nr_cids -1 ) /64 - m->base /64 +1 ; +memset (m->bits,0 , nr_words *sizeof (u64)); +} + +/** +* scx_cmask_fill - Set every bit in @m's active range +* @m: cmask to fill +* +* Counterpart to scx_cmask_clear(). Storage past the active range is left as is. +*/ +void scx_cmask_fill (struct scx_cmask *m) +{ +u32 nr_words, head_bits, tail_bits; + +if (!m->nr_cids) +return ; + nr_words = (m->base + m->nr_cids -1 ) /64 - m->base /64 +1 ; +memset (m->bits,0xff , nr_words *sizeof (u64)); + +/* clear word-0 bits below base */ + head_bits = m->base &63 ; +if (head_bits) + m->bits[0 ] &= ~((1ULL << head_bits) -1 ); + +/* clear last-word bits at or past base + nr_cids */ + tail_bits = (m->base + m->nr_cids) &63 ; +if (tail_bits) + m->bits[nr_words -1 ] &= (1ULL << tail_bits) -1 ; +} +/** * scx_cpumask_to_cmask - Translate a kernel cpumask into a cmask * @src: source cpumask * @dst: cmask to write * -* Initialize @dst to cover the full cid space [0, num_possible_cpus()) and -* set the bit for each cid whose cpu is in @src. +* Clear @dst's active range and set the bit for each cid whose cpu is in +* @src and lies within that range. Out-of-range cids are silently ignored. */ void scx_cpumask_to_cmask (const struct cpumask *src,struct scx_cmask *dst) {s32 cpu; -scx_cmask_init (dst,0 ,num_possible_cpus ()); +scx_cmask_clear (dst);for_each_cpu (cpu, src) {s32 cid =__scx_cpu_to_cid (cpu);
kernel/sched/ext_cid.h +56 -1
73 lines not on this page · raw message
kernel/sched/ext_types.h +47 -9
74 lines not on this page · raw message
tools/sched_ext/include/scx/cid.bpf.h +54 -3
74 lines not on this page · raw message
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH 2/3] sched_ext: Track bits[] storage size in struct scx_cmask
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Uninitialized per-CPU cmask scratch buffer silently breaks CPU affinity tracking - [Medium]…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Uninitialized per-CPU cmask scratch buffer silently breaks CPU affinity tracking
- [Medium] Missing bounds validation in BPF helper __cmask_init() permits arena out-of-bounds access --
commit ef0793b90f8713b1c92aecf6ec1f064d2df562a5
Author: Tejun Heo <[email protected]>
sched_ext: Track bits[] storage size in struct scx_cmask
This patch adds tracking for the allocation size of bits[] in struct scx_cmask to allow validation of active range reshapes. It splits the bit-range API, introduces scx_cmask_clear() and scx_cmask_fill(), and updates the BPF mirror functions.
Quoted 4 lines
kernel/sched/ext_cid.c +0
-0
[ ... ]
kernel/sched/ext_cid.c void scx_cpumask_to_cmask(const struct cpumask *src, struct scx_cmask *dst) · line 279
Quoted 12 lines
kernel/sched/ext_cid.c +3
-1@@ -274,10 +316,9 @@ void scx_cpumask_to_cmask(const struct cpumask *src, struct scx_cmask *dst)* Initialize @dst to cover the full cid space [0, num_possible_cpus()) and * set the bit for each cid whose cpu is in @src. +* Clear @dst's active range and set the bit for each cid whose cpu is in +* @src and lies within that range. Out-of-range cids are silently ignored. */ void scx_cpumask_to_cmask (const struct cpumask *src,struct scx_cmask *dst) {s32 cpu; -scx_cmask_init (dst,0 ,num_possible_cpus ()); +scx_cmask_clear (dst);
Since dst (scx_set_cmask_scratch) is zero-initialized via __alloc_percpu(), does it ever get its nr_cids explicitly initialized now that the call to scx_cmask_init() has been removed?
If m->nr_cids remains 0, scx_cmask_clear() will return immediately. The subsequent loop calling __scx_cmask_set() would then ignore all CPUs because the bounds check in __scx_cmask_contains() fails for a 0-length range.
Could this result in BPF schedulers receiving an empty cmask in their ops_cid.set_cmask() callback, breaking task affinity tracking?
Quoted 2 lines
kernel/sched/ext_cid.c +0
-0for_each_cpu (cpu, src) {s32 cid =__scx_cpu_to_cid (cpu);
[ ... ]
Quoted 17 lines
tools/sched_ext/include/scx/cid.bpf.h +8
-1@@ -44,20 +44,71 @@ static __always_inline u64 __arena *__cmask_word(const struct scx_cmask __arena return (u64 __arena *)&m->bits[cid / 64 - m->base / 64]; } -static __always_inline void cmask_init(struct scx_cmask __arena *m, u32 base, u32 nr_cids) +/** +* __cmask_init - Initialize @m with explicit storage capacity +* @m: cmask to initialize +* @base: first cid of the active range +* @nr_cids: number of cids in the active range +* @alloc_cids: storage capacity in cids, at least @nr_cids +* +* Use when storage is sized larger than the initial active range. All of
tools/sched_ext/include/scx/cid.bpf.h static __always_inline u64 __arena *__cmask_word(const struct scx_cmask __arena · line 63
Quoted 10 lines
tools/sched_ext/include/scx/cid.bpf.h +5
-1+* bits[] is zeroed. +*/ +static __always_inline void __cmask_init(struct scx_cmask __arena *m, u32 base, +u32 nr_cids,u32 alloc_cids) { -u32 nr_words =CMASK_NR_WORDS (nr_cids), i; +u32 alloc_words =CMASK_NR_WORDS (alloc_cids), i; m->base = base; m->nr_cids = nr_cids;
Should this helper validate that nr_cids does not exceed alloc_cids?
The C implementation (__scx_cmask_init) safely clamps nr_cids to alloc_cids. Without a similar check here or a call to scx_bpf_error(), passing an oversized nr_cids allows __cmask_contains() to pass bounds checks for bits beyond alloc_words.
Could this allow BPF programs to bypass intended constraints and corrupt their own arena structures during cmask_test() or __cmask_word() calls?
Quoted 3 lines
tools/sched_ext/include/scx/cid.bpf.h +1
-0+ m->alloc_words = alloc_words; bpf_for(i, 0, CMASK_MAX_WORDS) {
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Tejun Heo
reviewerPATCH v2 2/3 sched_ext: Track bits[] storage size in struct scx_cmask
In reply to Tejun Heoscx_cmask carries @base and @nr_cids but not the bits[] allocation size, so helpers reshaping the active range have no way to check it fits and later kfuncs taking caller-provided storage can't validate it.
Add @alloc_words (u64 word count) annotated with __counted_by, and split the bit-range API into three helpers:
- SCX_CMASK_DEFINE() / __SCX_CMASK_DEFINE() define an on-stack cmask, the latter taking an explicit capacity for oversized storage. SCX_CMASK_DEFINE_SHARD() is a thin wrapper that always reserves SCX_CID_SHARD_MAX_CPUS bits of storage.
- scx_cmask_init() / __scx_cmask_init() initialize a cmask, with the same tight-vs-explicit split.
- scx_cmask_reframe() reshapes the active range without resizing storage.
The BPF mirror (cmask_init / __cmask_init / cmask_reframe) gets the same shape.
Add scx_cmask_clear() and scx_cmask_fill() to zero and set the active-range bits respectively. scx_cpumask_to_cmask() uses scx_cmask_clear(); scx_cmask_init() would otherwise re-write @alloc_words on every call.
A later patch uses @alloc_words in scx_cmask_ref_shard() to refuse output storage that can't hold the requested shard.
v2: Init per-CPU scx_set_cmask_scratch (was zero-init, emitted empty
cmasks). Add nr_cids/alloc_cids check in BPF __cmask_init(). (sashiko AI)
--- kernel/sched/ext_cid.c | 52 ++++++++++++++++++++-- kernel/sched/ext_cid.h | 57 +++++++++++++++++++++++- kernel/sched/ext_types.h | 56 +++++++++++++++++++---- tools/sched_ext/include/scx/cid.bpf.h | 64 +++++++++++++++++++++++++-- 4 files changed, 213 insertions(+), 16 deletions(-)
kernel/sched/ext_cid.c +49 -3
@@ -55,6 +55,7 @@ static s32 scx_cid_arrays_alloc(void)s16 *cid_to_cpu, *cpu_to_cid;struct scx_cid_topo *cid_topo;struct scx_cmask __percpu *set_cmask_scratch; +s32 cpu;if (scx_cid_to_cpu_tbl)return 0 ;
@@ -77,6 +78,9 @@ static s32 scx_cid_arrays_alloc(void)WRITE_ONCE (scx_cid_to_cpu_tbl, cid_to_cpu);WRITE_ONCE (scx_cpu_to_cid_tbl, cpu_to_cid);WRITE_ONCE (scx_cid_topo, cid_topo); +for_each_possible_cpu (cpu) +scx_cmask_init (per_cpu_ptr (set_cmask_scratch, cpu), +0 , npossible);WRITE_ONCE (scx_set_cmask_scratch, set_cmask_scratch);return 0 ; }
@@ -222,19 +226,61 @@ s32 scx_cid_init(struct scx_sched *sch)return 0 ; } +/** +* scx_cmask_clear - Zero every bit in @m's active range +* @m: cmask to clear +* +* Storage past the active range is left as is. +*/ +void scx_cmask_clear (struct scx_cmask *m) +{ +u32 nr_words; + +if (!m->nr_cids) +return ; + nr_words = (m->base + m->nr_cids -1 ) /64 - m->base /64 +1 ; +memset (m->bits,0 , nr_words *sizeof (u64)); +} + +/** +* scx_cmask_fill - Set every bit in @m's active range +* @m: cmask to fill +* +* Counterpart to scx_cmask_clear(). Storage past the active range is left as is. +*/ +void scx_cmask_fill (struct scx_cmask *m) +{ +u32 nr_words, head_bits, tail_bits; + +if (!m->nr_cids) +return ; + nr_words = (m->base + m->nr_cids -1 ) /64 - m->base /64 +1 ; +memset (m->bits,0xff , nr_words *sizeof (u64)); + +/* clear word-0 bits below base */ + head_bits = m->base &63 ; +if (head_bits) + m->bits[0 ] &= ~((1ULL << head_bits) -1 ); + +/* clear last-word bits at or past base + nr_cids */ + tail_bits = (m->base + m->nr_cids) &63 ; +if (tail_bits) + m->bits[nr_words -1 ] &= (1ULL << tail_bits) -1 ; +} +/** * scx_cpumask_to_cmask - Translate a kernel cpumask into a cmask * @src: source cpumask * @dst: cmask to write * -* Initialize @dst to cover the full cid space [0, num_possible_cpus()) and -* set the bit for each cid whose cpu is in @src. +* Clear @dst's active range and set the bit for each cid whose cpu is in +* @src and lies within that range. Out-of-range cids are silently ignored. */ void scx_cpumask_to_cmask (const struct cpumask *src,struct scx_cmask *dst) {s32 cpu; -scx_cmask_init (dst,0 ,num_possible_cpus ()); +scx_cmask_clear (dst);for_each_cpu (cpu, src) {s32 cid =__scx_cpu_to_cid (cpu);
kernel/sched/ext_cid.h +56 -1
73 lines not on this page · raw message
kernel/sched/ext_types.h +47 -9
74 lines not on this page · raw message
tools/sched_ext/include/scx/cid.bpf.h +61 -3
81 lines not on this page · raw message
Signature · 1 line
2.54.0
Andrea Righi
reviewerRe: [PATCH v2 2/3] sched_ext: Track bits[] storage size in struct scx_cmask
Hi Tejun,
Quoted 34 lines · Tejun Heo
scx_cmaskcarries @base and @nr_cids but not the bits[] allocation size, so helpers reshaping the active range have no way to check it fits and later kfuncs taking caller-provided storage can't validate it.Add @alloc_words (u64 word count) annotated with
__counted_by, and split the bit-range API into three helpers:-
SCX_CMASK_DEFINE()/__SCX_CMASK_DEFINE()define an on-stack cmask, the latter taking an explicit capacity for oversized storage.SCX_CMASK_DEFINE_SHARD()is a thin wrapper that always reservesSCX_CID_SHARD_MAX_CPUSbits of storage.-
scx_cmask_init()/__scx_cmask_init()initialize a cmask, with the same tight-vs-explicit split.-
scx_cmask_reframe()reshapes the active range without resizing storage.The BPF mirror (
cmask_init/__cmask_init/cmask_reframe) gets the same shape.Add
scx_cmask_clear()andscx_cmask_fill()to zero and set the active-range bits respectively.scx_cpumask_to_cmask()usesscx_cmask_clear();scx_cmask_init()would otherwise re-write @alloc_words on every call.A later patch uses @alloc_words in
scx_cmask_ref_shard()to refuse output storage that can't hold the requested shard.v2: Init per-CPU
scx_set_cmask_scratch(was zero-init, emitted empty
cmasks). Add nr_cids/alloc_cids check in BPF__cmask_init(). (sashiko AI)Signed-off-by Tejun Heo <[email protected]>---
...
kernel/sched/ext_cid.h static inline u64 *__scx_cmask_word(const struct scx_cmask *m, u32 cid) · line 199
Quoted 13 lines
kernel/sched/ext_cid.h +13
-0+/** +* scx_cmask_reframe - Reshape @m's active range without resizing storage +* @m: cmask to reframe +* @base: new active range base +* @nr_cids: new active range length, must fit within @m->alloc_words +* +* Body bits within the new range become garbage - only the head and tail +* words are zeroed to keep the padding invariant. +*/ +static inline void scx_cmask_reframe (struct scx_cmask *m,u32 base,u32 nr_cids) +{ +if (WARN_ON_ONCE (SCX_CMASK_NR_WORDS (nr_cids) > m->alloc_words)) +return ;
Considering that:
#define SCX_CMASK_NR_WORDS(nr_cids) (((nr_cids) + 63) / 64 + 1)
If we pass nr_cids == UINT_MAX here, we have:
CMASK_NR_WORDS(UINT_MAX) = (UINT_MAX + 63)/64 + 1 = 62/64 + 1 = 1 (wraps)
Should we simply reject if it's greater than a certain reasonable upper bound?
Thanks,
-Andrea
Quoted 12 lines
kernel/sched/ext_cid.h +8
-1+ +if (nr_cids) { +u32 last_word = ((base &63 ) + nr_cids -1 ) /64 ; + + m->bits[0 ] =0 ; + m->bits[last_word] =0 ; + } + m->base = base; m->nr_cids = nr_cids; -memset (m->bits,0 ,SCX_CMASK_NR_WORDS (nr_cids) *sizeof (u64)); }
Tejun Heo
reviewerRe: [PATCH v2 2/3] sched_ext: Track bits[] storage size in struct scx_cmask
Quoted 16 lines · Andrea Righi
kernel/sched/ext_cid.h static inline u64 *__scx_cmask_word(const struct scx_cmask *m, u32 cid) · line 199
Quoted 13 lines
kernel/sched/ext_cid.h +13
-0+/** +* scx_cmask_reframe - Reshape @m's active range without resizing storage +* @m: cmask to reframe +* @base: new active range base +* @nr_cids: new active range length, must fit within @m->alloc_words +* +* Body bits within the new range become garbage - only the head and tail +* words are zeroed to keep the padding invariant. +*/ +static inline void scx_cmask_reframe (struct scx_cmask *m,u32 base,u32 nr_cids) +{ +if (WARN_ON_ONCE (SCX_CMASK_NR_WORDS (nr_cids) > m->alloc_words)) +return ;Considering that:
Quoted 7 lines
#define
SCX_CMASK_NR_WORDS(nr_cids)(((nr_cids) + 63) / 64 + 1)If we pass
nr_cids==UINT_MAXhere, we have:
CMASK_NR_WORDS(UINT_MAX)= (UINT_MAX+ 63)/64 + 1 = 62/64 + 1 = 1 (wraps)Should we simply reject if it's greater than a certain reasonable upper bound?
I'm not sure what we do matters. No matter what, this would be a clear bug and an unlikely one at that. As long as the backtrace is dumped, I think anything is fine.
Thanks.
Signature · 1 line
tejun
Andrea Righi
reviewerRe: [PATCH v2 2/3] sched_ext: Track bits[] storage size in struct scx_cmask
Hi Tejun,
Quoted 21 lines · Tejun Heo
Quoted 20 lines · Andrea Righi
kernel/sched/ext_cid.h static inline u64 *__scx_cmask_word(const struct scx_cmask *m, u32 cid) · line 199
Quoted 13 lines
kernel/sched/ext_cid.h +13
-0+/** +* scx_cmask_reframe - Reshape @m's active range without resizing storage +* @m: cmask to reframe +* @base: new active range base +* @nr_cids: new active range length, must fit within @m->alloc_words +* +* Body bits within the new range become garbage - only the head and tail +* words are zeroed to keep the padding invariant. +*/ +static inline void scx_cmask_reframe (struct scx_cmask *m,u32 base,u32 nr_cids) +{ +if (WARN_ON_ONCE (SCX_CMASK_NR_WORDS (nr_cids) > m->alloc_words)) +return ;Considering that:
#define
SCX_CMASK_NR_WORDS(nr_cids)(((nr_cids) + 63) / 64 + 1)If we pass
nr_cids==UINT_MAXhere, we have:
Quoted 7 lines
Quoted 3 lines
CMASK_NR_WORDS(UINT_MAX)= (UINT_MAX+ 63)/64 + 1 = 62/64 + 1 = 1 (wraps)Should we simply reject if it's greater than a certain reasonable upper bound?
I'm not sure what we do matters. No matter what, this would be a clear bug and an unlikely one at that. As long as the backtrace is dumped, I think anything is fine.
Agreed that the bug is unlikely to happen, but the WARN_ON_ONCE() wouldn't fire at all for nr_cids == UINT_MAX.
However, IIUC scx_cmask_reframe() is internal kernel code, with no callers yet and the upcoming consumers will probably drive nr_cids from bounded sources, likely num_possible_cpus() and such. So, the wrap shouldn't really reachable on the kernel side. Therefore, I guess we can simply drop the WARN_ON_ONCE().
Instead, cmask_reframe() is the mirrored version that is called from BPF, so any loaded BPF prog can potentially pass an arbitrary u32.
How about changing the CMASK_NR_WORDS() macro as following?
#define CMASK_NR_WORDS(nr_cids) ((u32)(((u64)(nr_cids) + 63) / 64 + 1))
In this way CMASK_NR_WORDS(UINT_MAX) returns ~67M instead of 1 and in this way we'd get a clear scx_bpf_error() backtrace if it wraps. WDYT?
Thanks,
-Andrea
Tejun Heo
reviewerPATCH 3/3 sched_ext: Add cmask mask ops
In reply to Tejun HeoSub-sched cap code and other upcoming consumers need bulk cmask ops, both mutating (and/or/copy/andnot) and predicate (subset/intersects).
cmask_walk_op2() walks the intersection of two ranges word by word; cmask_walk_op1() walks one range. Both are __always_inline and dispatched on a compile-time-constant op enum, so each public entry collapses to a specialized loop with the inner switch reduced to one arm.
Two-cmask ops only touch bits in the intersection of the two ranges; bits outside are left unchanged. scx_cmask_or_racy() and scx_cmask_copy_racy() mirror the locking forms but read @src word-by-word through data_race(); callers handle ordering with concurrent writers themselves.
--- kernel/sched/ext_cid.c | 259 +++++++++++++++++++++++++++++++++++++++++ kernel/sched/ext_cid.h | 8 ++ 2 files changed, 267 insertions(+)
kernel/sched/ext_cid.c +259 -0
@@ -393,6 +393,265 @@ __bpf_kfunc s32 scx_bpf_cpu_to_cid(s32 cpu, const struct bpf_prog_aux *aux)return scx_cpu_to_cid (sch, cpu); } +/* +* Set ops on cmasks. cmask_walk_op2() shares one walk across mutating +* (and/or/copy/andnot) and predicate (subset/intersects) two-cmask forms; +* cmask_walk_op1() does the same shape over a single cmask range. Every public +* entry passes a compile-time-constant @op; cmask_walk_op{1,2}() and +* cmask_word_op{1,2}() are __always_inline so the inner switch collapses to the +* selected op and cmask_op2_is_pred() folds the predicate early-exit out of +* mutating ops. +* +* Two-cmask ops only touch @dst bits inside the intersection of the two ranges; +* bits outside stay untouched. In particular, scx_cmask_copy() does NOT zero +* @dst bits that lie outside @src's range. +* +* The _RACY variants are otherwise identical to their non-racy counterpart but +* read @src word-by-word via data_race(). Memory ordering with concurrent +* writers is the caller's responsibility. +*/ +enum cmask_op2 { +/* mutating */ +CMASK_OP2_AND , +CMASK_OP2_OR , +CMASK_OP2_OR_RACY , +CMASK_OP2_COPY , +CMASK_OP2_COPY_RACY , +CMASK_OP2_ANDNOT , +/* predicates - short-circuit when the per-word result is true */ +CMASK_OP2_SUBSET , +CMASK_OP2_INTERSECTS , +}; + +static __always_inline bool cmask_op2_is_pred(const enum cmask_op2 op) +{ +return op ==CMASK_OP2_SUBSET || op ==CMASK_OP2_INTERSECTS ; +} + +static __always_inline bool cmask_word_op2(u64 *av, const u64 *bp, u64 mask, +const enum cmask_op2 op) +{ +switch (op) { +case CMASK_OP2_AND : + *av &= ~mask | *bp; +return false; +case CMASK_OP2_OR : + *av |= *bp & mask; +return false; +case CMASK_OP2_OR_RACY : + *av |=data_race (*bp) & mask; +return false; +case CMASK_OP2_COPY : + *av = (*av & ~mask) | (*bp & mask); +return false; +case CMASK_OP2_COPY_RACY : + *av = (*av & ~mask) | (data_race (*bp) & mask); +return false; +case CMASK_OP2_ANDNOT : + *av &= ~(*bp & mask); +return false; +case CMASK_OP2_SUBSET : +/* stop on the first bit in @sub not set in @super */ +return (*bp & ~*av) & mask; +case CMASK_OP2_INTERSECTS : +return (*av & *bp) & mask; + } +unreachable (); +} + +/* +* Walk the intersection of [@a_base, @a_base + @a_nr_cids) with [@b_base, +* @b_base + @b_nr_cids) word by word, applying @op. Mutating ops walk all words +* and return false; predicates return true on the first word whose per-word +* test is true. Empty intersection returns false (matches "no bits to consider" +* for both mutate and predicate). +* +* Base/nr_cids are taken as parameters so callers with snapshotted bounds can +* drive the walk with values independent of the cmask's header. +*/ +static __always_inline bool cmask_walk_op2(u64 *a_bits, u32 a_base, u32 a_nr_cids, +const u64 *b_bits,u32 b_base,u32 b_nr_cids, +const enum cmask_op2 op) +{ +u32 lo =max (a_base, b_base); +u32 hi =min (a_base + a_nr_cids, b_base + b_nr_cids); +u32 a_word_off = a_base /64 ; +u32 b_word_off = b_base /64 ; +u32 lo_word = lo /64 ; +u32 hi_word = (hi -1 ) /64 ; +u64 head_mask =GENMASK_U64 (63 , lo &63 ); +u64 tail_mask =GENMASK_U64 ((hi -1 ) &63 ,0 ); +u32 w; + +if (lo >= hi) +return false; + +if (lo_word == hi_word) +return cmask_word_op2 (&a_bits[lo_word - a_word_off], + &b_bits[lo_word - b_word_off], + head_mask & tail_mask, op); + +if (cmask_word_op2 (&a_bits[lo_word - a_word_off], + &b_bits[lo_word - b_word_off], head_mask, op) && +cmask_op2_is_pred (op)) +return true; + +for (w = lo_word +1 ; w < hi_word; w++) +if (cmask_word_op2 (&a_bits[w - a_word_off], + &b_bits[w - b_word_off], ~0ULL , op) && +cmask_op2_is_pred (op)) +return true; + +return cmask_word_op2 (&a_bits[hi_word - a_word_off], + &b_bits[hi_word - b_word_off], tail_mask, op); +} + +enum cmask_op1 { +CMASK_OP1_ANY_SET , +}; + +static __always_inline bool cmask_word_op1(const u64 *ap, u64 mask, +const enum cmask_op1 op) +{ +switch (op) { +case CMASK_OP1_ANY_SET : +return *ap & mask; + } +unreachable (); +} + +/* +* Walk [@a_base, @a_base + @a_nr_cids) of @a_bits word by word, applying @op. +* Returns true on the first word whose per-word test is true; returns false if +* no word matches or the range is empty. All current op1s short-circuit on +* per-word true; if a non-predicate op1 lands here, add a cmask_op1_is_pred() +* guard analogous to cmask_op2_is_pred(). +*/ +static __always_inline bool cmask_walk_op1(const u64 *a_bits, u32 a_base, +u32 a_nr_cids, +const enum cmask_op1 op) +{ +u32 lo = a_base; +u32 hi = a_base + a_nr_cids; +u32 a_word_off = a_base /64 ; +u32 lo_word = lo /64 ; +u32 hi_word = (hi -1 ) /64 ; +u64 head_mask =GENMASK_U64 (63 , lo &63 ); +u64 tail_mask =GENMASK_U64 ((hi -1 ) &63 ,0 ); +u32 w; + +if (lo >= hi) +return false; + +if (lo_word == hi_word) +return cmask_word_op1 (&a_bits[lo_word - a_word_off], + head_mask & tail_mask, op); + +if (cmask_word_op1 (&a_bits[lo_word - a_word_off], head_mask, op)) +return true; +for (w = lo_word +1 ; w < hi_word; w++) +if (cmask_word_op1 (&a_bits[w - a_word_off], ~0ULL , op)) +return true; +return cmask_word_op1 (&a_bits[hi_word - a_word_off], tail_mask, op); +} + +void scx_cmask_and (struct scx_cmask *dst,const struct scx_cmask *src) +{ +cmask_walk_op2 (dst->bits, dst->base, dst->nr_cids, + src->bits, src->base, src->nr_cids,CMASK_OP2_AND ); +} + +void scx_cmask_or (struct scx_cmask *dst,const struct scx_cmask *src) +{ +cmask_walk_op2 (dst->bits, dst->base, dst->nr_cids, + src->bits, src->base, src->nr_cids,CMASK_OP2_OR ); +} + +/** +* scx_cmask_or_racy - OR @src into @dst, reading @src without locking +* +* @src is read word-by-word through data_race(). Same per-bit independence +* rationale as scx_cmask_copy_racy(). Memory ordering with writers is the +* caller's responsibility. +*/ +void scx_cmask_or_racy (struct scx_cmask *dst,const struct scx_cmask *src) +{ +cmask_walk_op2 (dst->bits, dst->base, dst->nr_cids, + src->bits, src->base, src->nr_cids,CMASK_OP2_OR_RACY ); +} + +void scx_cmask_copy (struct scx_cmask *dst,const struct scx_cmask *src) +{ +cmask_walk_op2 (dst->bits, dst->base, dst->nr_cids, + src->bits, src->base, src->nr_cids,CMASK_OP2_COPY ); +} + +/** +* scx_cmask_copy_racy - Snapshot @src into @dst without locking +* +* @src is read word-by-word through data_race(). Head/tail masking matches +* scx_cmask_copy(). Each bit in a cmask is independent, so partial updates +* just leave some bits fresher than others. Memory ordering with writers is +* the caller's responsibility. +*/ +void scx_cmask_copy_racy (struct scx_cmask *dst,const struct scx_cmask *src) +{ +cmask_walk_op2 (dst->bits, dst->base, dst->nr_cids, + src->bits, src->base, src->nr_cids,CMASK_OP2_COPY_RACY ); +} + +void scx_cmask_andnot (struct scx_cmask *dst,const struct scx_cmask *src) +{ +cmask_walk_op2 (dst->bits, dst->base, dst->nr_cids, + src->bits, src->base, src->nr_cids,CMASK_OP2_ANDNOT ); +} + +/* +* Return true if @cm has any bit set in [@lo, @hi). Caller must ensure +* [@lo, @hi) is contained in @cm's range. +*/ +static bool cmask_any_set_in_range (const struct scx_cmask *cm,u32 lo,u32 hi) +{ +if (lo >= hi) +return false; +return cmask_walk_op1 (&cm->bits[lo /64 - cm->base /64 ], lo, hi - lo, +CMASK_OP1_ANY_SET ); +} + +/** +* scx_cmask_subset - test whether @sub is a subset of @super +* @sub: cmask to test +* @super: cmask to test against +* +* Return true iff every set bit of @sub is also set in @super. +*/ +bool scx_cmask_subset (const struct scx_cmask *sub,const struct scx_cmask *super) +{ +u32 super_end = super->base + super->nr_cids; +u32 sub_end = sub->base + sub->nr_cids; + +/* +* Set bits in @sub outside @super's range can't be in @super, so any +* such bit means not a subset. The walk below only visits words +* common to both ranges, so these need a separate scan. +*/ +if (sub->base < super->base && +cmask_any_set_in_range (sub, sub->base,min (super->base, sub_end))) +return false; +if (sub_end > super_end && +cmask_any_set_in_range (sub,max (sub->base, super_end), sub_end)) +return false; + +return !cmask_walk_op2 ((u64 *)super->bits, super->base, super->nr_cids, + sub->bits, sub->base, sub->nr_cids,CMASK_OP2_SUBSET ); +} + +bool scx_cmask_intersects (const struct scx_cmask *a,const struct scx_cmask *b) +{ +return cmask_walk_op2 ((u64 *)a->bits, a->base, a->nr_cids, + b->bits, b->base, b->nr_cids,CMASK_OP2_INTERSECTS ); +} +/** * scx_bpf_cid_topo - Copy out per-cid topology info * @cid: cid to look up
kernel/sched/ext_cid.h +8 -0
@@ -53,6 +53,14 @@ extern struct btf_id_set8 scx_kfunc_ids_init;void scx_cmask_clear (struct scx_cmask *m);void scx_cmask_fill (struct scx_cmask *m); +void scx_cmask_and (struct scx_cmask *dst,const struct scx_cmask *src); +void scx_cmask_or (struct scx_cmask *dst,const struct scx_cmask *src); +void scx_cmask_or_racy (struct scx_cmask *dst,const struct scx_cmask *src); +void scx_cmask_copy (struct scx_cmask *dst,const struct scx_cmask *src); +void scx_cmask_copy_racy (struct scx_cmask *dst,const struct scx_cmask *src); +void scx_cmask_andnot (struct scx_cmask *dst,const struct scx_cmask *src); +bool scx_cmask_subset (const struct scx_cmask *sub,const struct scx_cmask *super); +bool scx_cmask_intersects (const struct scx_cmask *a,const struct scx_cmask *b);s32 scx_cid_init (struct scx_sched *sch);int scx_cid_kfunc_init (void );void scx_cpumask_to_cmask (const struct cpumask *src,struct scx_cmask *dst);
Signature · 1 line
2.54.0
Tejun Heo
reviewerPATCH v2 3/3 sched_ext: Add cmask mask ops
Sub-sched cap code and other upcoming consumers need bulk cmask ops, both mutating (and/or/copy/andnot) and predicate (subset/intersects/empty).
cmask_walk_op2() walks the intersection of two ranges word by word; cmask_walk_op1() walks one range. Both are __always_inline and dispatched on a compile-time-constant op enum, so each public entry collapses to a specialized loop with the inner switch reduced to one arm.
Two-cmask ops only touch bits in the intersection of the two ranges; bits outside are left unchanged. scx_cmask_or_racy() and scx_cmask_copy_racy() mirror the locking forms but read @src word-by-word through data_race(); callers handle ordering with concurrent writers themselves.
v2: Add scx_cmask_empty().
--- kernel/sched/ext_cid.c | 270 +++++++++++++++++++++++++++++++++++++++++ kernel/sched/ext_cid.h | 9 ++ 2 files changed, 279 insertions(+)
kernel/sched/ext_cid.c +270 -0
@@ -397,6 +397,276 @@ __bpf_kfunc s32 scx_bpf_cpu_to_cid(s32 cpu, const struct bpf_prog_aux *aux)return scx_cpu_to_cid (sch, cpu); } +/* +* Set ops on cmasks. cmask_walk_op2() shares one walk across mutating +* (and/or/copy/andnot) and predicate (subset/intersects) two-cmask forms; +* cmask_walk_op1() does the same shape over a single cmask range. Every public +* entry passes a compile-time-constant @op; cmask_walk_op{1,2}() and +* cmask_word_op{1,2}() are __always_inline so the inner switch collapses to the +* selected op and cmask_op2_is_pred() folds the predicate early-exit out of +* mutating ops. +* +* Two-cmask ops only touch @dst bits inside the intersection of the two ranges; +* bits outside stay untouched. In particular, scx_cmask_copy() does NOT zero +* @dst bits that lie outside @src's range. +* +* The _RACY variants are otherwise identical to their non-racy counterpart but +* read @src word-by-word via data_race(). Memory ordering with concurrent +* writers is the caller's responsibility. +*/ +enum cmask_op2 { +/* mutating */ +CMASK_OP2_AND , +CMASK_OP2_OR , +CMASK_OP2_OR_RACY , +CMASK_OP2_COPY , +CMASK_OP2_COPY_RACY , +CMASK_OP2_ANDNOT , +/* predicates - short-circuit when the per-word result is true */ +CMASK_OP2_SUBSET , +CMASK_OP2_INTERSECTS , +}; + +static __always_inline bool cmask_op2_is_pred(const enum cmask_op2 op) +{ +return op ==CMASK_OP2_SUBSET || op ==CMASK_OP2_INTERSECTS ; +} + +static __always_inline bool cmask_word_op2(u64 *av, const u64 *bp, u64 mask, +const enum cmask_op2 op) +{ +switch (op) { +case CMASK_OP2_AND : + *av &= ~mask | *bp; +return false; +case CMASK_OP2_OR : + *av |= *bp & mask; +return false; +case CMASK_OP2_OR_RACY : + *av |=data_race (*bp) & mask; +return false; +case CMASK_OP2_COPY : + *av = (*av & ~mask) | (*bp & mask); +return false; +case CMASK_OP2_COPY_RACY : + *av = (*av & ~mask) | (data_race (*bp) & mask); +return false; +case CMASK_OP2_ANDNOT : + *av &= ~(*bp & mask); +return false; +case CMASK_OP2_SUBSET : +/* stop on the first bit in @sub not set in @super */ +return (*bp & ~*av) & mask; +case CMASK_OP2_INTERSECTS : +return (*av & *bp) & mask; + } +unreachable (); +} + +/* +* Walk the intersection of [@a_base, @a_base + @a_nr_cids) with [@b_base, +* @b_base + @b_nr_cids) word by word, applying @op. Mutating ops walk all words +* and return false; predicates return true on the first word whose per-word +* test is true. Empty intersection returns false (matches "no bits to consider" +* for both mutate and predicate). +* +* Base/nr_cids are taken as parameters so callers with snapshotted bounds can +* drive the walk with values independent of the cmask's header. +*/ +static __always_inline bool cmask_walk_op2(u64 *a_bits, u32 a_base, u32 a_nr_cids, +const u64 *b_bits,u32 b_base,u32 b_nr_cids, +const enum cmask_op2 op) +{ +u32 lo =max (a_base, b_base); +u32 hi =min (a_base + a_nr_cids, b_base + b_nr_cids); +u32 a_word_off = a_base /64 ; +u32 b_word_off = b_base /64 ; +u32 lo_word = lo /64 ; +u32 hi_word = (hi -1 ) /64 ; +u64 head_mask =GENMASK_U64 (63 , lo &63 ); +u64 tail_mask =GENMASK_U64 ((hi -1 ) &63 ,0 ); +u32 w; + +if (lo >= hi) +return false; + +if (lo_word == hi_word) +return cmask_word_op2 (&a_bits[lo_word - a_word_off], + &b_bits[lo_word - b_word_off], + head_mask & tail_mask, op); + +if (cmask_word_op2 (&a_bits[lo_word - a_word_off], + &b_bits[lo_word - b_word_off], head_mask, op) && +cmask_op2_is_pred (op)) +return true; + +for (w = lo_word +1 ; w < hi_word; w++) +if (cmask_word_op2 (&a_bits[w - a_word_off], + &b_bits[w - b_word_off], ~0ULL , op) && +cmask_op2_is_pred (op)) +return true; + +return cmask_word_op2 (&a_bits[hi_word - a_word_off], + &b_bits[hi_word - b_word_off], tail_mask, op); +} + +enum cmask_op1 { +CMASK_OP1_ANY_SET , +}; + +static __always_inline bool cmask_word_op1(const u64 *ap, u64 mask, +const enum cmask_op1 op) +{ +switch (op) { +case CMASK_OP1_ANY_SET : +return *ap & mask; + } +unreachable (); +} + +/* +* Walk [@a_base, @a_base + @a_nr_cids) of @a_bits word by word, applying @op. +* Returns true on the first word whose per-word test is true; returns false if +* no word matches or the range is empty. All current op1s short-circuit on +* per-word true; if a non-predicate op1 lands here, add a cmask_op1_is_pred() +* guard analogous to cmask_op2_is_pred(). +*/ +static __always_inline bool cmask_walk_op1(const u64 *a_bits, u32 a_base, +u32 a_nr_cids, +const enum cmask_op1 op) +{ +u32 lo = a_base; +u32 hi = a_base + a_nr_cids; +u32 a_word_off = a_base /64 ; +u32 lo_word = lo /64 ; +u32 hi_word = (hi -1 ) /64 ; +u64 head_mask =GENMASK_U64 (63 , lo &63 ); +u64 tail_mask =GENMASK_U64 ((hi -1 ) &63 ,0 ); +u32 w; + +if (lo >= hi) +return false; + +if (lo_word == hi_word) +return cmask_word_op1 (&a_bits[lo_word - a_word_off], + head_mask & tail_mask, op); + +if (cmask_word_op1 (&a_bits[lo_word - a_word_off], head_mask, op)) +return true; +for (w = lo_word +1 ; w < hi_word; w++) +if (cmask_word_op1 (&a_bits[w - a_word_off], ~0ULL , op)) +return true; +return cmask_word_op1 (&a_bits[hi_word - a_word_off], tail_mask, op); +} + +void scx_cmask_and (struct scx_cmask *dst,const struct scx_cmask *src) +{ +cmask_walk_op2 (dst->bits, dst->base, dst->nr_cids, + src->bits, src->base, src->nr_cids,CMASK_OP2_AND ); +} + +void scx_cmask_or (struct scx_cmask *dst,const struct scx_cmask *src) +{ +cmask_walk_op2 (dst->bits, dst->base, dst->nr_cids, + src->bits, src->base, src->nr_cids,CMASK_OP2_OR ); +} + +/** +* scx_cmask_or_racy - OR @src into @dst, reading @src without locking +* +* @src is read word-by-word through data_race(). Same per-bit independence +* rationale as scx_cmask_copy_racy(). Memory ordering with writers is the +* caller's responsibility. +*/ +void scx_cmask_or_racy (struct scx_cmask *dst,const struct scx_cmask *src) +{ +cmask_walk_op2 (dst->bits, dst->base, dst->nr_cids, + src->bits, src->base, src->nr_cids,CMASK_OP2_OR_RACY ); +} + +void scx_cmask_copy (struct scx_cmask *dst,const struct scx_cmask *src) +{ +cmask_walk_op2 (dst->bits, dst->base, dst->nr_cids, + src->bits, src->base, src->nr_cids,CMASK_OP2_COPY ); +} + +/** +* scx_cmask_copy_racy - Snapshot @src into @dst without locking +* +* @src is read word-by-word through data_race(). Head/tail masking matches +* scx_cmask_copy(). Each bit in a cmask is independent, so partial updates +* just leave some bits fresher than others. Memory ordering with writers is +* the caller's responsibility. +*/ +void scx_cmask_copy_racy (struct scx_cmask *dst,const struct scx_cmask *src) +{ +cmask_walk_op2 (dst->bits, dst->base, dst->nr_cids, + src->bits, src->base, src->nr_cids,CMASK_OP2_COPY_RACY ); +} + +void scx_cmask_andnot (struct scx_cmask *dst,const struct scx_cmask *src) +{ +cmask_walk_op2 (dst->bits, dst->base, dst->nr_cids, + src->bits, src->base, src->nr_cids,CMASK_OP2_ANDNOT ); +} + +/* +* Return true if @cm has any bit set in [@lo, @hi). Caller must ensure +* [@lo, @hi) is contained in @cm's range. +*/ +static bool cmask_any_set_in_range (const struct scx_cmask *cm,u32 lo,u32 hi) +{ +if (lo >= hi) +return false; +return cmask_walk_op1 (&cm->bits[lo /64 - cm->base /64 ], lo, hi - lo, +CMASK_OP1_ANY_SET ); +} + +/** +* scx_cmask_subset - test whether @sub is a subset of @super +* @sub: cmask to test +* @super: cmask to test against +* +* Return true iff every set bit of @sub is also set in @super. +*/ +bool scx_cmask_subset (const struct scx_cmask *sub,const struct scx_cmask *super) +{ +u32 super_end = super->base + super->nr_cids; +u32 sub_end = sub->base + sub->nr_cids; + +/* +* Set bits in @sub outside @super's range can't be in @super, so any +* such bit means not a subset. The walk below only visits words +* common to both ranges, so these need a separate scan. +*/ +if (sub->base < super->base && +cmask_any_set_in_range (sub, sub->base,min (super->base, sub_end))) +return false; +if (sub_end > super_end && +cmask_any_set_in_range (sub,max (sub->base, super_end), sub_end)) +return false; + +return !cmask_walk_op2 ((u64 *)super->bits, super->base, super->nr_cids, + sub->bits, sub->base, sub->nr_cids,CMASK_OP2_SUBSET ); +} + +bool scx_cmask_intersects (const struct scx_cmask *a,const struct scx_cmask *b) +{ +return cmask_walk_op2 ((u64 *)a->bits, a->base, a->nr_cids, + b->bits, b->base, b->nr_cids,CMASK_OP2_INTERSECTS ); +} + +/** +* scx_cmask_empty - Test whether @m has no bits set +* @m: cmask to test +* +* Return true iff @m's active range has no bits set. +*/ +bool scx_cmask_empty (const struct scx_cmask *m) +{ +return !cmask_any_set_in_range (m, m->base, m->base + m->nr_cids); +} +/** * scx_bpf_cid_topo - Copy out per-cid topology info * @cid: cid to look up
kernel/sched/ext_cid.h +9 -0
@@ -53,6 +53,15 @@ extern struct btf_id_set8 scx_kfunc_ids_init;void scx_cmask_clear (struct scx_cmask *m);void scx_cmask_fill (struct scx_cmask *m); +void scx_cmask_and (struct scx_cmask *dst,const struct scx_cmask *src); +void scx_cmask_or (struct scx_cmask *dst,const struct scx_cmask *src); +void scx_cmask_or_racy (struct scx_cmask *dst,const struct scx_cmask *src); +void scx_cmask_copy (struct scx_cmask *dst,const struct scx_cmask *src); +void scx_cmask_copy_racy (struct scx_cmask *dst,const struct scx_cmask *src); +void scx_cmask_andnot (struct scx_cmask *dst,const struct scx_cmask *src); +bool scx_cmask_subset (const struct scx_cmask *sub,const struct scx_cmask *super); +bool scx_cmask_intersects (const struct scx_cmask *a,const struct scx_cmask *b); +bool scx_cmask_empty (const struct scx_cmask *m);s32 scx_cid_init (struct scx_sched *sch);int scx_cid_kfunc_init (void );void scx_cpumask_to_cmask (const struct cpumask *src,struct scx_cmask *dst);
Signature · 1 line
2.54.0