FAILED: patch "[PATCH] sched_ext: Add a size argument to scx_bpf_cid_topo() so" failed to apply to 7.2-stable tree
Subsystems (3)
[email protected]
maintainerThe patch below does not apply to the 7.2-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-7.2.y
git checkout FETCH_HEAD
git cherry-pick -x 94480606a677deb68d5622cf0ded88626514b3f2
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026092914-daughter-crudeness-399c@gregkh' --subject-prefix 'PATCH 7.2.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 94480606a677deb68d5622cf0ded88626514b3f2 Mon Sep 17 00:00:00 2001
From: Tejun Heo <[email protected]>
Date: Sat, 26 Sep 2026 09:23:02 -1000
Subject: [PATCH] sched_ext: Add a size argument to scx_bpf_cid_topo() so
struct scx_cid_topo can grow
scx_bpf_cid_topo() copies struct scx_cid_topo into a buffer the BPF program sized from its own vmlinux.h while the verifier sizes the write from the running kernel's BTF. The struct may grow and each growth then breaks every scheduler built against the older layout, rejected at load or written past its buffer. This is the usual hole for a struct handed to BPF, closed elsewhere with a size argument, and it was missed here.
Take the buffer size, copy the smaller of it and the kernel's struct and set the rest to -1. Accesses to the copy are CO-RE relocated, so the struct can grow by appending fields, which its comment now states. The kfunc changes in
place: the cid interface is still being finalized and no released scheduler uses the current form.
kernel/sched/ext/cid.c +16 -10
@@ -912,30 +912,36 @@ bool scx_cmask_empty(const struct scx_cmask *m)/** * scx_bpf_cid_topo - Copy out per-cid topology info * @cid: cid to look up -* @out__uninit: where to copy the topology info; fully written by this call +* @out: where to copy the topology info +* @out__sz: size of @out, the program's sizeof(struct scx_cid_topo) * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs * -* Fill @out__uninit with the topology info for @cid. Trigger scx_error() if -* @cid is out of range. If @cid is valid but in the no-topo section, all fields -* are set to -1. All fields are also set to -1 when no cid tables have been -* published yet, which a program may observe while racing the root enable. +* Fill @out with the topology info for @cid. Trigger scx_error() if @cid is out +* of range. If @cid is valid but in the no-topo section, all fields are set to +* -1. All fields are also set to -1 when no cid tables have been published yet, +* which a program may observe while racing the root enable. +* +* The program's struct may be older or newer than the kernel's. The smaller of +* @out__sz and the kernel's size is copied and the rest of @out is set to -1. */ -__bpf_kfunc void scx_bpf_cid_topo(s32 cid, struct scx_cid_topo *out__uninit, +__bpf_kfunc void scx_bpf_cid_topo(s32 cid, struct scx_cid_topo *out, size_t out__sz,const struct bpf_prog_aux *aux) { +size_t len =min (out__sz,sizeof (*out));struct scx_cid_topo *topo;struct scx_sched *sch; +/* the error cases and fields the kernel lacks read as -1 */ +memset (out,0xff , out__sz); +guard (rcu)(); sch =scx_prog_sched (aux); topo =rcu_dereference (scx_cid_topo); -if (unlikely (!sch) || !cid_valid (sch, cid) ||unlikely (!topo)) { - *out__uninit =SCX_CID_TOPO_NEG ; +if (unlikely (!sch) || !cid_valid (sch, cid) ||unlikely (!topo))return ; - } - *out__uninit = topo[cid]; +memcpy (out, &topo[cid], len); }__bpf_kfunc_end_defs ();
kernel/sched/ext/types.h +4 -0
@@ -70,6 +70,10 @@ enum scx_consts {* smaller shards if the LLC exceeds the target size. No-topo cids are packed * into their own max-sized shards. * +* New fields are appended, never inserted: scx_bpf_cid_topo() copies this +* struct out sized by the program's own layout, and an older program's copy +* must stay a prefix of the kernel's. +* * @core_cid: first cid of this cid's core (smt-sibling group) * @core_idx: global index of that core, in [0, nr_cores_at_init) * @llc_cid: first cid of this cid's LLC
tools/sched_ext/include/scx/common.bpf.h +1 -1
@@ -106,7 +106,7 @@ u64 scx_bpf_now(void) __ksym __weak; void scx_bpf_events(struct scx_event_stats *events, size_t events__sz) __ksym __weak; 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_cid_topo(s32 cid, struct scx_cid_topo *out, size_t out__sz) __ksym __weak; void 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;
Sasha Levin
maintainerPATCH 1/3 sched_ext: Generalize local-DSQ handling to rq-owned DSQs
From: Tejun Heo <[email protected]>
[ Upstream commit bf6cf1886a56eaf929d8193a3f4b30be59a778e2 ]
The local DSQ is synchronized by the containing rq lock rather than its own dsq->lock. A later patch adds a second such DSQ. In preparation, factor the "rq owns the lock" test into dsq_is_rq_owned() and rename local_dsq_post_enq() to rq_owned_post_enq(), taking @rq explicitly.
No behavior change.
Stable-dep-of: 94480606a677 ("sched_ext: Add a size argument to scx_bpf_cid_topo() so struct scx_cid_topo can grow")
--- kernel/sched/ext/ext.c | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-)
kernel/sched/ext/ext.c +22 -12
@@ -95,6 +95,17 @@ static atomic_long_t scx_hotplug_seq = ATOMIC_LONG_INIT(0);/* Global cursor for the per-CPU tid allocator. Starts at 1; tid 0 is reserved. */ static atomic64_t scx_tid_cursor =ATOMIC64_INIT (1 ); +/* is @dsq synchronized by the containing rq lock instead of dsq->lock? */ +static bool dsq_is_rq_owned (struct scx_dispatch_q *dsq) +{ +switch (dsq->id) { +case SCX_DSQ_LOCAL : +return true; +default : +return false; + } +} +#ifdef CONFIG_EXT_SUB_SCHED /* * The sub sched being enabled. Used by scx_disable_and_exit_task() to exit
@@ -1466,11 +1477,10 @@ static void call_task_dequeue(struct scx_sched *sch, struct rq *rq, p->scx.flags &= ~SCX_TASK_IN_CUSTODY ; } -static void local_dsq_post_enq (struct scx_sched *sch,struct scx_dispatch_q *dsq, -struct task_struct *p,u64 enq_flags) +static void rq_owned_post_enq (struct scx_sched *sch,struct rq *rq, +struct scx_dispatch_q *dsq,struct task_struct *p, +u64 enq_flags) { - struct rq *rq = container_of(dsq, struct rq, scx.local_dsq); -call_task_dequeue (sch, rq, p,0 );/*
@@ -1530,13 +1540,13 @@ static void dispatch_enqueue(struct scx_sched *sch, struct rq *rq, struct scx_dispatch_q *dsq, struct task_struct *p, u64 enq_flags) { - bool is_local = dsq->id == SCX_DSQ_LOCAL; + bool is_rq_owned = dsq_is_rq_owned(dsq);WARN_ON_ONCE (p->scx.dsq || !list_empty (&p->scx.dsq_list.node));WARN_ON_ONCE ((p->scx.dsq_flags &SCX_TASK_DSQ_ON_PRIQ ) || !RB_EMPTY_NODE (&p->scx.dsq_priq)); -if (!is_local) { +if (!is_rq_owned) {raw_spin_lock_nested (&dsq->lock, (enq_flags &SCX_ENQ_NESTED ) ?SINGLE_DEPTH_NESTING :0 );
@@ -1631,8 +1641,8 @@ static void dispatch_enqueue(struct scx_sched *sch, struct rq *rq,* ops_state first, both sides would modify p->scx.flags * concurrently in a non-atomic way. */ -if (is_local) { -local_dsq_post_enq (sch, dsq, p, enq_flags); +if (is_rq_owned) { +rq_owned_post_enq (sch, rq, dsq, p, enq_flags); }else {/* * Task on global/bypass DSQ: leave custody, task on
@@ -1679,7 +1689,7 @@ static void task_unlink_from_dsq(struct task_struct *p,static void dispatch_dequeue (struct rq *rq,struct task_struct *p) {struct scx_dispatch_q *dsq = p->scx.dsq; -bool is_local = dsq == &rq->scx.local_dsq; +bool is_rq_owned = dsq &&dsq_is_rq_owned (dsq);lockdep_assert_rq_held (rq);
@@ -1703,7 +1713,7 @@ static void dispatch_dequeue(struct rq *rq, struct task_struct *p)return ; } -if (!is_local) +if (!is_rq_owned)raw_spin_lock (&dsq->lock);/*
@@ -1725,7 +1735,7 @@ static void dispatch_dequeue(struct rq *rq, struct task_struct *p) } p->scx.dsq =NULL ; -if (!is_local) +if (!is_rq_owned)raw_spin_unlock (&dsq->lock); }
@@ -2277,7 +2287,7 @@ static void move_local_task_to_local_dsq(struct scx_sched *sch,dsq_inc_nr (dst_dsq, p, enq_flags); p->scx.dsq = dst_dsq; -local_dsq_post_enq (sch, dst_dsq, p, enq_flags); +rq_owned_post_enq (sch, dst_rq, dst_dsq, p, enq_flags); }/**
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/3 sched_ext: Build the cid tables privately and publish them with RCU
From: Tejun Heo <[email protected]>
[ Upstream commit 3a773220d39ba993dfe5d135f8b610be10794d5d ]
The cid tables are visible to the cid kfuncs while being modified: the first enable publishes the global pointers before filling them, ops.init_cids() overrides rewrite them in place, and re-enables rebuild them in place. A racing TRACING or SYSCALL program can read unfilled entries, including uninitialized memory in the kmalloc'd tables, or torn topo updates.
Tie the tables' lifetimes to the root sched instead: each root enable builds a fresh set privately and publishes the per-table __rcu globals once the layout is final, and root disable unpublishes and RCU-frees the set. A non-NULL global is now always a fully built table which stays valid for the reader's RCU read section, and lookups stay two loads. Kfuncs treat NULL as no-mapping, also after the scheduler exits instead of reporting the stale last mapping.
The cid kfuncs are available whether the root scheduler is cid-form or cpu-form, the latter to allow gradual migration to cids. Every root therefore builds and publishes a default mapping.
Every reader must either be gated on scheduler liveness or NULL-check inside an RCU read section. Fix the two kfuncs that were neither:scx_bpf_this_cid() read the table with no RCU or preemption protection and scx_bpf_task_cid() relied on KF_RCU, which doesn't put a sleepable program in an RCU read section. The hotplug callbacks are instead serialized by retiring the tables inside the cpus_read_lock() section that clears scx_root.
v2: Document why every root builds the tables (desc + cid.c comment).
[ Stable dependency adaptation for 94480606a677:
Limit private construction and RCU publication to scx_cid_topo, the table read by the target. 7.2 has no cid shards or sub.c and permits overrides from ops.init(), not ops.init_cids(). Keep its existing cpu<->cid arrays and callback interface. Build a fresh topology table on every enable and override, publish only complete tables, and revoke the table on root disable. Reclaim replaced tables after synchronize_rcu(), outside the override's read-side critical section. Reuse existing functions; omit upstream's new allocation/publication/free helpers and shard changes.
Publish the default topology before ops.init() and override replacements before returning, preserving topology queries during this callback. Allocation and validation failures release unpublished temporary tables.
Also fold in the existing-function/prototype changes from b38332be61a8 (sched_ext: Make scx_bpf_kick_cid() return void). This matches the adjacent context in the target's common.bpf.h hunk; in-tree callers do not consume the return value. The target applies cleanly with a three-way application. This dependency does not change scx_bpf_cid_topo()'s signature. ]
[ sashal: Reduced backport -- upstream 3a773220d39ba touches 5 file(s), this
backport carries 4. Not backported here:
kernel/sched/ext/internal.h
kernel/sched/ext/sub.c
This note is generated from the file lists only; see the resolution record for the reasoning. ]
Stable-dep-of: 94480606a677 ("sched_ext: Add a size argument to scx_bpf_cid_topo() so struct scx_cid_topo can grow")
--- kernel/sched/ext/cid.c | 114 ++++++++++++++--------- kernel/sched/ext/cid.h | 2 +- kernel/sched/ext/ext.c | 19 ++-- tools/sched_ext/include/scx/common.bpf.h | 2 +- 4 files changed, 83 insertions(+), 54 deletions(-)
kernel/sched/ext/cid.c +68 -46
@@ -13,14 +13,15 @@/* * cid tables. * -* Pointers are published once on first enable and never revoked. The default -* mapping is populated before ops.init() runs; scx_bpf_cid_override() commits -* before it returns. As long as the BPF scheduler only uses the tables from -* those points onward, it sees a consistent view. +* The cpu<->cid arrays are allocated once and populated before ops.init(). +* The topology table is built privately and published with RCU, including +* when ops.init() overrides the mapping. Published topology tables are never +* modified and are retired after an RCU grace period. Root disable revokes +* the topology table, so readers must check for NULL. */ s16 *scx_cid_to_cpu_tbl;s16 *scx_cpu_to_cid_tbl; -struct scx_cid_topo *scx_cid_topo; +struct scx_cid_topo __rcu *scx_cid_topo;#define SCX_CID_TOPO_NEG (struct scx_cid_topo) { \ .core_cid = -1, .core_idx = -1, .llc_cid = -1, .llc_idx = -1, \
@@ -43,30 +44,26 @@ static const struct cpumask *cpu_llc_mask(int cpu, struct cpumask *fallbacks)return &ci->info_list[ci->num_leaves -1 ].shared_cpu_map; } -/* Allocate the cid tables once on first enable; never freed. */ +/* Allocate the cpu<->cid arrays once on first enable; never freed. */ static s32 scx_cid_arrays_alloc (void ) {u32 npossible =num_possible_cpus ();s16 *cid_to_cpu, *cpu_to_cid; -struct scx_cid_topo *cid_topo;if (scx_cid_to_cpu_tbl)return 0 ; cid_to_cpu =kzalloc_objs (*scx_cid_to_cpu_tbl, npossible,GFP_KERNEL ); cpu_to_cid =kzalloc_objs (*scx_cpu_to_cid_tbl, nr_cpu_ids,GFP_KERNEL ); - cid_topo =kmalloc_objs (*scx_cid_topo, npossible,GFP_KERNEL ); -if (!cid_to_cpu || !cpu_to_cid || !cid_topo) { +if (!cid_to_cpu || !cpu_to_cid) {kfree (cid_to_cpu);kfree (cpu_to_cid); -kfree (cid_topo);return -ENOMEM ; }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);return 0 ; }
@@ -88,6 +85,8 @@ s32 scx_cid_init(struct scx_sched *sch) cpumask_var_t core_scratch __free(free_cpumask_var) = CPUMASK_VAR_NULL; cpumask_var_t llc_fallback __free(free_cpumask_var) = CPUMASK_VAR_NULL; cpumask_var_t online_no_topo __free(free_cpumask_var) = CPUMASK_VAR_NULL; + struct scx_cid_topo *topo __free(kfree) = NULL; +struct scx_cid_topo *old_topo;u32 next_cid =0 ;s32 next_node_idx =0 , next_llc_idx =0 , next_core_idx =0 ;s32 cpu, ret;
@@ -101,6 +100,10 @@ s32 scx_cid_init(struct scx_sched *sch)if (ret)return ret; + topo =kmalloc_objs (*topo,num_possible_cpus (),GFP_KERNEL ); +if (!topo) +return -ENOMEM ; + if (!zalloc_cpumask_var(&to_walk, GFP_KERNEL) || !zalloc_cpumask_var(&node_scratch, GFP_KERNEL) || !zalloc_cpumask_var(&llc_scratch, GFP_KERNEL) ||
@@ -165,7 +168,7 @@ s32 scx_cid_init(struct scx_sched *sch) scx_cid_to_cpu_tbl[cid] = ccpu; scx_cpu_to_cid_tbl[ccpu] = cid; - scx_cid_topo[cid] = (struct scx_cid_topo ){ + topo[cid] = (struct scx_cid_topo ){ .core_cid = core_cid, .core_idx = core_idx, .llc_cid = llc_cid,
@@ -198,7 +201,7 @@ s32 scx_cid_init(struct scx_sched *sch) cid = next_cid++; scx_cid_to_cpu_tbl[cid] = cpu; scx_cpu_to_cid_tbl[cpu] = cid; - scx_cid_topo[cid] =SCX_CID_TOPO_NEG ; + topo[cid] =SCX_CID_TOPO_NEG ; }if (!cpumask_empty (llc_fallback))
@@ -208,6 +211,13 @@ s32 scx_cid_init(struct scx_sched *sch)pr_warn ("scx_cid: online cpus with no usable topology: %*pbl\n" ,cpumask_pr_args (online_no_topo)); +/* Root enable and ops.init() serialize topology table updates. */ + old_topo =rcu_replace_pointer (scx_cid_topo,no_free_ptr (topo), true); +if (old_topo) { +synchronize_rcu (); +kfree (old_topo); + } +return 0 ; }
@@ -291,51 +301,60 @@ __bpf_kfunc void scx_bpf_cid_override(const s32 *cpu_to_cid, u32 cpu_to_cid__sz, const struct bpf_prog_aux *aux) { cpumask_var_t seen __free(free_cpumask_var) = CPUMASK_VAR_NULL; + struct scx_cid_topo *topo __free(kfree) = NULL; +struct scx_cid_topo *old_topo;struct scx_sched *sch;bool alloced;s32 cpu, cid;/* GFP_KERNEL alloc must happen before the rcu read section */ alloced =zalloc_cpumask_var (&seen,GFP_KERNEL ); + topo =kmalloc_objs (*topo,num_possible_cpus (),GFP_KERNEL ); -guard (rcu)(); - - sch =scx_prog_sched (aux); -if (unlikely (!sch)) -return ; +scoped_guard (rcu) { + sch =scx_prog_sched (aux); +if (unlikely (!sch)) +return ; -if (!alloced) { -scx_error (sch,"scx_bpf_cid_override: failed to allocate cpumask" ); -return ; - } +if (!alloced || !topo) { +scx_error (sch,"scx_bpf_cid_override: failed to allocate tables" ); +return ; + } -if (scx_parent (sch)) { -scx_error (sch,"scx_bpf_cid_override() only allowed from root sched" ); -return ; - } +if (scx_parent (sch)) { +scx_error (sch,"scx_bpf_cid_override() only allowed from root sched" ); +return ; + } -if (cpu_to_cid__sz != nr_cpu_ids *sizeof (s32)) { -scx_error (sch,"scx_bpf_cid_override: expected %zu bytes, got %u" , - nr_cpu_ids *sizeof (s32), cpu_to_cid__sz); -return ; - } +if (cpu_to_cid__sz != nr_cpu_ids *sizeof (s32)) { +scx_error (sch,"scx_bpf_cid_override: expected %zu bytes, got %u" , + nr_cpu_ids *sizeof (s32), cpu_to_cid__sz); +return ; + } -for_each_possible_cpu (cpu) { -s32 c = cpu_to_cid[cpu]; +for_each_possible_cpu (cpu) { +s32 c = cpu_to_cid[cpu]; -if (!cid_valid (sch, c)) -return ; -if (cpumask_test_and_set_cpu (c, seen)) { -scx_error (sch,"cid %d assigned to multiple cpus" , c); -return ; +if (!cid_valid (sch, c)) +return ; +if (cpumask_test_and_set_cpu (c, seen)) { +scx_error (sch,"cid %d assigned to multiple cpus" , c); +return ; + } + scx_cpu_to_cid_tbl[cpu] = c; + scx_cid_to_cpu_tbl[c] = cpu; } - scx_cpu_to_cid_tbl[cpu] = c; - scx_cid_to_cpu_tbl[c] = cpu; + +/* The override carries no topology. Publish a complete replacement. */ +for (cid =0 ; cid <num_possible_cpus (); cid++) + topo[cid] =SCX_CID_TOPO_NEG ; + + old_topo =rcu_replace_pointer (scx_cid_topo,no_free_ptr (topo), true); } -/* Invalidate stale topo info - the override carries no topology. */ -for (cid =0 ; cid <num_possible_cpus (); cid++) - scx_cid_topo[cid] =SCX_CID_TOPO_NEG ; +/* This sleepable kfunc must leave its RCU read section before waiting. */ +synchronize_rcu (); +kfree (old_topo); }/**
@@ -660,22 +679,25 @@ bool scx_cmask_empty(const struct scx_cmask *m)* * Fill @out__uninit with the topology info for @cid. Trigger scx_error() if * @cid is out of range. If @cid is valid but in the no-topo section, all fields -* are set to -1. +* are set to -1. All fields are also set to -1 when no cid tables have been +* published yet, which a program may observe while racing the root enable. */ __bpf_kfunc void scx_bpf_cid_topo(s32 cid, struct scx_cid_topo *out__uninit,const struct bpf_prog_aux *aux) { +struct scx_cid_topo *topo;struct scx_sched *sch;guard (rcu)(); sch =scx_prog_sched (aux); -if (unlikely (!sch) || !cid_valid (sch, cid)) { + topo =rcu_dereference (scx_cid_topo); +if (unlikely (!sch) || !cid_valid (sch, cid) ||unlikely (!topo)) { *out__uninit =SCX_CID_TOPO_NEG ;return ; } - *out__uninit =READ_ONCE (scx_cid_topo)[cid]; + *out__uninit = topo[cid]; }__bpf_kfunc_end_defs ();
kernel/sched/ext/cid.h +1 -1
@@ -50,7 +50,7 @@ struct scx_sched;*/ extern s16 *scx_cid_to_cpu_tbl;extern s16 *scx_cpu_to_cid_tbl; -extern struct scx_cid_topo *scx_cid_topo; +extern struct scx_cid_topo __rcu *scx_cid_topo;extern struct btf_id_set8 scx_kfunc_ids_init;void scx_cmask_clear (struct scx_cmask *m);
kernel/sched/ext/ext.c +13 -6
@@ -6226,6 +6226,7 @@ static inline void scx_sub_disable(struct scx_sched *sch) { }static void scx_root_disable (struct scx_sched *sch) { +struct scx_cid_topo *topo;struct scx_task_iter sti;struct task_struct *p;bool was_switched_all;
@@ -6355,8 +6356,15 @@ static void scx_root_disable(struct scx_sched *sch)*/ cpus_read_lock ();RCU_INIT_POINTER (scx_root,NULL ); + topo =rcu_replace_pointer (scx_cid_topo,NULL , +lockdep_is_held (&scx_enable_mutex));cpus_read_unlock (); +if (topo) { +synchronize_rcu (); +kfree (topo); + } +/* * Delete the kobject from the hierarchy synchronously. Otherwise, sysfs * could observe an object of the same name still in the hierarchy when
@@ -9630,10 +9638,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(). Return 0 on success, -* -errno otherwise. +* cid-addressed equivalent of scx_bpf_kick_cpu(). An invalid @cid aborts the +* scheduler via scx_cid_to_cpu(). */ -__bpf_kfunc s32 scx_bpf_kick_cid(s32 cid, u64 flags, const struct bpf_prog_aux *aux) +__bpf_kfunc void scx_bpf_kick_cid(s32 cid, u64 flags, const struct bpf_prog_aux *aux) {struct scx_sched *sch;s32 cpu;
@@ -9641,12 +9649,11 @@ __bpf_kfunc s32 scx_bpf_kick_cid(s32 cid, u64 flags, const struct bpf_prog_aux *guard (rcu)(); sch =scx_prog_sched (aux);if (unlikely (!sch)) -return -ENODEV ; +return ; cpu =scx_cid_to_cpu (sch, cid);if (cpu <0 ) -return cpu; +return ;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; -s32 scx_bpf_kick_cid(s32 cid, u64 flags) __ksym __weak; +void 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.53.0
Sasha Levin
maintainerPATCH 3/3 sched_ext: Add a size argument to scx_bpf_cid_topo() so struct scx_cid_topo can grow
In reply to Sasha LevinFrom: Tejun Heo <[email protected]>
[ Upstream commit 94480606a677deb68d5622cf0ded88626514b3f2 ]
scx_bpf_cid_topo() copies struct scx_cid_topo into a buffer the BPF program sized from its own vmlinux.h while the verifier sizes the write from the running kernel's BTF. The struct may grow and each growth then breaks every scheduler built against the older layout, rejected at load or written past its buffer. This is the usual hole for a struct handed to BPF, closed elsewhere with a size argument, and it was missed here.
Take the buffer size, copy the smaller of it and the kernel's struct and set the rest to -1. Accesses to the copy are CO-RE relocated, so the struct can grow by appending fields, which its comment now states. The kfunc changes in
place: the cid interface is still being finalized and no released scheduler uses the current form.
--- kernel/sched/ext/cid.c | 26 +++++++++++++++--------- kernel/sched/ext/types.h | 4 ++++ tools/sched_ext/include/scx/common.bpf.h | 2 +- 3 files changed, 21 insertions(+), 11 deletions(-)
kernel/sched/ext/cid.c +16 -10
@@ -674,30 +674,36 @@ bool scx_cmask_empty(const struct scx_cmask *m)/** * scx_bpf_cid_topo - Copy out per-cid topology info * @cid: cid to look up -* @out__uninit: where to copy the topology info; fully written by this call +* @out: where to copy the topology info +* @out__sz: size of @out, the program's sizeof(struct scx_cid_topo) * @aux: implicit BPF argument to access bpf_prog_aux hidden from BPF progs * -* Fill @out__uninit with the topology info for @cid. Trigger scx_error() if -* @cid is out of range. If @cid is valid but in the no-topo section, all fields -* are set to -1. All fields are also set to -1 when no cid tables have been -* published yet, which a program may observe while racing the root enable. +* Fill @out with the topology info for @cid. Trigger scx_error() if @cid is out +* of range. If @cid is valid but in the no-topo section, all fields are set to +* -1. All fields are also set to -1 when no cid tables have been published yet, +* which a program may observe while racing the root enable. +* +* The program's struct may be older or newer than the kernel's. The smaller of +* @out__sz and the kernel's size is copied and the rest of @out is set to -1. */ -__bpf_kfunc void scx_bpf_cid_topo(s32 cid, struct scx_cid_topo *out__uninit, +__bpf_kfunc void scx_bpf_cid_topo(s32 cid, struct scx_cid_topo *out, size_t out__sz,const struct bpf_prog_aux *aux) { +size_t len =min (out__sz,sizeof (*out));struct scx_cid_topo *topo;struct scx_sched *sch; +/* the error cases and fields the kernel lacks read as -1 */ +memset (out,0xff , out__sz); +guard (rcu)(); sch =scx_prog_sched (aux); topo =rcu_dereference (scx_cid_topo); -if (unlikely (!sch) || !cid_valid (sch, cid) ||unlikely (!topo)) { - *out__uninit =SCX_CID_TOPO_NEG ; +if (unlikely (!sch) || !cid_valid (sch, cid) ||unlikely (!topo))return ; - } - *out__uninit = topo[cid]; +memcpy (out, &topo[cid], len); }__bpf_kfunc_end_defs ();
kernel/sched/ext/types.h +4 -0
@@ -53,6 +53,10 @@ enum scx_consts {* over [0, nr_cores_at_init) with no gaps. No-topo cids have all fields set * to -1. * +* New fields are appended, never inserted: scx_bpf_cid_topo() copies this +* struct out sized by the program's own layout, and an older program's copy +* must stay a prefix of the kernel's. +* * @core_cid: first cid of this cid's core (smt-sibling group) * @core_idx: global index of that core, in [0, nr_cores_at_init) * @llc_cid: first cid of this cid's LLC
tools/sched_ext/include/scx/common.bpf.h +1 -1
@@ -104,7 +104,7 @@ u64 scx_bpf_now(void) __ksym __weak; void scx_bpf_events(struct scx_event_stats *events, size_t events__sz) __ksym __weak; 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_cid_topo(s32 cid, struct scx_cid_topo *out, size_t out__sz) __ksym __weak; void 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;
Signature · 1 line
2.53.0