Fix sub-sched locking issues
Subsystems (2)
Tejun Heo
reviewerPATCH 1/1 Fix sub-sched locking issues
Hello,
Cheng-Yang reported a lockdep circular dependency between scx_sched_lock and rq->__lock. scx_bypass() and sysrq_handle_sched_ext_dump() take scx_sched_lock -> rq lock, while scx_claim_exit() (reachable from many paths with rq lock held) takes rq -> scx_sched_lock. In addition, scx_disable() directly calling kthread_queue_work() under scx_sched_lock creates another chain through worker->lock -> pi_lock -> rq->__lock.
This patchset fixes these issues:
1. Fix wrong sub_detach op check.
2. Add scx_dump_lock and dump_disabled to decouple dump from scx_sched_lock.
3. Always bounce scx_disable() through irq_work to avoid lock nesting.
4. Flip scx_bypass() lock order and drop scx_sched_lock from sysrq dump.
5. Reject sub-sched attachment to a disabled parent.
Tested on three machines (16-CPU QEMU, 192-CPU dual-socket EPYC, AMD Ryzen) with lockdep trigger tests and an 11-test stress suite covering attach/detach, nesting, reverse teardown, rapid cycling, error injection, SysRq-D/S dump/exit, and combined stress. Lockdep triggered on baseline, clean after patches.
Based on sched_ext/for-7.1 (b8840942644c).
1. sched_ext: Fix sub_detach op check to test the parent's ops
2. sched_ext: Add scx_dump_lock and dump_disabled
3. sched_ext: Always bounce scx_disable() through irq_work
4. sched_ext: Fix scx_sched_lock / rq lock ordering
5. sched_ext: Reject sub-sched attachment to a disabled parent
kernel/sched/ext.c | 59 +++++++++++++++++++++++++++++++++++---------- kernel/sched/ext_internal.h | 3 ++- 2 files changed, 48 insertions(+), 14 deletions(-)
--
tejun
Tejun Heo
reviewerPATCH 1/5 sched_ext: Fix sub_detach op check to test the parent's ops
sub_detach is the parent's op called to notify the parent that a child is detaching. Test parent->ops.sub_detach instead of sch->ops.sub_detach.
--- kernel/sched/ext.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/ext.c +1 -1
@@ -5438,7 +5438,7 @@ static void scx_sub_disable(struct scx_sched *sch)*/ wake_up_all (&scx_unlink_waitq); -if (sch->ops.sub_detach && sch->sub_attached) { +if (parent->ops.sub_detach && sch->sub_attached) {struct scx_sub_detach_args sub_detach_args = { .ops = &sch->ops, .cgroup_path = sch->cgrp_path,
Signature · 1 line
2.53.0
Tejun Heo
reviewerPATCH 2/5 sched_ext: Add scx_dump_lock and dump_disabled
In reply to Tejun HeoAdd a dedicated scx_dump_lock and per-sched dump_disabled flag so that debug dumping can be safely disabled during sched teardown without relying on scx_sched_lock. This is a prep for the next patch which decouples the sysrq dump path from scx_sched_lock to resolve a lock ordering issue.
--- kernel/sched/ext.c | 25 ++++++++++++++++++++++--- kernel/sched/ext_internal.h | 1 + 2 files changed, 23 insertions(+), 3 deletions(-)
kernel/sched/ext.c +22 -3
@@ -136,6 +136,8 @@ static DEFINE_RAW_SPINLOCK(scx_exit_bstr_buf_lock);static struct scx_bstr_buf scx_exit_bstr_buf;/* ops debug dump */ +static DEFINE_RAW_SPINLOCK (scx_dump_lock); +struct scx_dump_data {s32 cpu;bool first;
@@ -5279,6 +5281,17 @@ static void scx_unlink_sched(struct scx_sched *sch)refresh_watchdog (); } +/* +* Called to disable future dumps and wait for in-progress one while disabling +* @sch. Once @sch becomes empty during disable, there's no point in dumping it. +* This prevents calling dump ops on a dead sch. +*/ +static void scx_disable_dump (struct scx_sched *sch) +{ +guard (raw_spinlock_irqsave)(&scx_dump_lock); + sch->dump_disabled = true; +} +#ifdef CONFIG_EXT_SUB_SCHED static DECLARE_WAIT_QUEUE_HEAD (scx_unlink_waitq);
@@ -5414,6 +5427,8 @@ static void scx_sub_disable(struct scx_sched *sch) }scx_task_iter_stop (&sti); +scx_disable_dump (sch); +scx_cgroup_unlock ();percpu_up_write (&scx_fork_rwsem);
@@ -5525,6 +5540,8 @@ static void scx_root_disable(struct scx_sched *sch) }scx_task_iter_stop (&sti); +scx_disable_dump (sch); +scx_cgroup_lock ();set_cgroup_sched (sch_cgroup (sch),NULL );scx_cgroup_unlock ();
@@ -5680,7 +5697,7 @@ static __printf(2, 3) void dump_line(struct seq_buf *s, const char *fmt, ...)#ifdef CONFIG_TRACEPOINTS if (trace_sched_ext_dump_enabled ()) { -/* protected by scx_dump_state()::dump_lock */ +/* protected by scx_dump_lock */ static char line_buf[SCX_EXIT_MSG_LEN ];va_start (args, fmt);
@@ -5842,7 +5859,6 @@ static void scx_dump_task(struct scx_sched *sch,static void scx_dump_state (struct scx_sched *sch,struct scx_exit_info *ei,size_t dump_len,bool dump_all_tasks) { -static DEFINE_RAW_SPINLOCK (dump_lock);static const char trunc_marker[] ="\n\n~~~~ TRUNCATED ~~~~\n" ;struct scx_dump_ctx dctx = { .kind = ei->kind,
@@ -5856,7 +5872,10 @@ static void scx_dump_state(struct scx_sched *sch, struct scx_exit_info *ei,char *buf;int cpu; -guard (raw_spinlock_irqsave)(&dump_lock); +guard (raw_spinlock_irqsave)(&scx_dump_lock); + +if (sch->dump_disabled) +return ;seq_buf_init (&s, ei->dump, dump_len);
kernel/sched/ext_internal.h +1 -0
@@ -1003,6 +1003,7 @@ struct scx_sched {atomic_t bypass_dsp_enable_depth;bool aborting; +bool dump_disabled;/* protected by scx_dump_lock */ u32 dsp_max_batch;s32 level;
Signature · 1 line
2.53.0
Tejun Heo
reviewerPATCH 3/5 sched_ext: Always bounce scx_disable() through irq_work
In reply to Tejun Heoscx_disable() directly called kthread_queue_work() which can acquire worker->lock, pi_lock and rq->__lock. This made scx_disable() unsafe to call while holding locks that conflict with this chain - in particular, scx_claim_exit() calls scx_disable() for each descendant while holding scx_sched_lock, which nests inside rq->__lock in scx_bypass().
The error path (scx_vexit()) was already bouncing through irq_work to avoid this issue. Generalize the pattern to all scx_disable() calls by always going through irq_work. irq_work_queue() is lockless and safe to call from any context, and the actual kthread_queue_work() call happens in the irq_work handler outside any locks.
Rename error_irq_work to disable_irq_work to reflect the broader usage.
--- kernel/sched/ext.c | 12 ++++++------ kernel/sched/ext_internal.h | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-)
kernel/sched/ext.c +6 -6
@@ -4498,7 +4498,7 @@ static void scx_sched_free_rcu_work(struct work_struct *work)struct scx_dispatch_q *dsq;int cpu, node; -irq_work_sync (&sch->error_irq_work); +irq_work_sync (&sch->disable_irq_work);kthread_destroy_worker (sch->helper);timer_shutdown_sync (&sch->bypass_lb_timer);
@@ -5679,7 +5679,7 @@ static void scx_disable(struct scx_sched *sch, enum scx_exit_kind kind) {guard (preempt)();if (scx_claim_exit (sch, kind)) -kthread_queue_work (sch->helper, &sch->disable_work); +irq_work_queue (&sch->disable_irq_work); }static void dump_newline (struct seq_buf *s)
@@ -6012,9 +6012,9 @@ static void scx_dump_state(struct scx_sched *sch, struct scx_exit_info *ei, trunc_marker, sizeof(trunc_marker)); } -static void scx_error_irq_workfn (struct irq_work *irq_work) +static void scx_disable_irq_workfn (struct irq_work *irq_work) { - struct scx_sched *sch = container_of(irq_work, struct scx_sched, error_irq_work); + struct scx_sched *sch = container_of(irq_work, struct scx_sched, disable_irq_work);struct scx_exit_info *ei = sch->exit_info;if (ei->kind >=SCX_EXIT_ERROR )
@@ -6048,7 +6048,7 @@ static bool scx_vexit(struct scx_sched *sch, ei->kind = kind; ei->reason =scx_exit_reason (ei->kind); -irq_work_queue (&sch->error_irq_work); +irq_work_queue (&sch->disable_irq_work);return true; }
@@ -6184,7 +6184,7 @@ static struct scx_sched *scx_alloc_and_add_sched(struct sched_ext_ops *ops, sch->slice_dfl =SCX_SLICE_DFL ;atomic_set (&sch->exit_kind,SCX_EXIT_NONE ); -init_irq_work (&sch->error_irq_work, scx_error_irq_workfn); +init_irq_work (&sch->disable_irq_work, scx_disable_irq_workfn);kthread_init_work (&sch->disable_work, scx_disable_workfn);timer_setup (&sch->bypass_lb_timer, scx_bypass_lb_timerfn,0 ); sch->ops = *ops;
kernel/sched/ext_internal.h +1 -1
@@ -1042,7 +1042,7 @@ struct scx_sched {struct kobject kobj;struct kthread_worker *helper; -struct irq_work error_irq_work; +struct irq_work disable_irq_work;struct kthread_work disable_work;struct timer_list bypass_lb_timer;struct rcu_work rcu_work;
Signature · 1 line
2.53.0
Tejun Heo
reviewerPATCH 4/5 sched_ext: Fix scx_sched_lock / rq lock ordering
In reply to Tejun HeoThere are two sites that nest rq lock inside scx_sched_lock:
- scx_bypass() takes scx_sched_lock then rq lock per CPU to propagate per-cpu bypass flags and re-enqueue tasks.
- sysrq_handle_sched_ext_dump() takes scx_sched_lock to iterate all scheds, scx_dump_state() then takes rq lock per CPU for dump.
And scx_claim_exit() takes scx_sched_lock to propagate exits to descendants. It can be reached from scx_tick(), BPF kfuncs, and many other paths with rq lock already held, creating the reverse ordering:
rq lock -> scx_sched_lock vs. scx_sched_lock -> rq lock
Fix by flipping scx_bypass() to take rq lock first, and dropping scx_sched_lock from sysrq_handle_sched_ext_dump() as scx_sched_all is already RCU-traversable and scx_dump_lock now prevents dumping a dead sched. This makes the consistent ordering rq lock -> scx_sched_lock.
--- kernel/sched/ext.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
kernel/sched/ext.c +1 -3
@@ -5097,8 +5097,8 @@ static void scx_bypass(struct scx_sched *sch, bool bypass)struct rq *rq =cpu_rq (cpu);struct task_struct *p, *n; -raw_spin_lock (&scx_sched_lock);raw_spin_rq_lock (rq); +raw_spin_lock (&scx_sched_lock);scx_for_each_descendant_pre (pos, sch) {struct scx_sched_pcpu *pcpu =per_cpu_ptr (pos->pcpu, cpu);
@@ -7240,8 +7240,6 @@ static void sysrq_handle_sched_ext_dump(u8 key)struct scx_exit_info ei = { .kind =SCX_EXIT_NONE , .reason ="SysRq-D" };struct scx_sched *sch; -guard (raw_spinlock_irqsave)(&scx_sched_lock); -list_for_each_entry_rcu (sch, &scx_sched_all, all)scx_dump_state (sch, &ei,0 , false); }
Signature · 1 line
2.53.0
Re: [PATCH 4/5] sched_ext: Fix scx_sched_lock / rq lock ordering
Hi Tejun,
Quoted 2 lines · Tejun Heo
Just a little nit, this link is invalid.
It should use the modern lore.kernel.org domain instead:
Signature · 2 lines
Thanks, Cheng-Yang
Andrea Righi
reviewerRe: [PATCH 4/5] sched_ext: Fix scx_sched_lock / rq lock ordering
In reply to Tejun HeoHi Tejun,
Quoted 42 lines · Tejun Heo
There are two sites that nest rq lock inside
scx_sched_lock:-
scx_bypass()takesscx_sched_lockthen rq lock per CPU to propagate per-cpu bypass flags and re-enqueue tasks.-
sysrq_handle_sched_ext_dump()takesscx_sched_lockto iterate all scheds,scx_dump_state()then takes rq lock per CPU for dump.And
scx_claim_exit()takesscx_sched_lockto propagate exits to descendants. It can be reached fromscx_tick(), BPF kfuncs, and many other paths with rq lock already held, creating the reverse ordering:rq lock ->
scx_sched_lockvs.scx_sched_lock-> rq lockFix by flipping
scx_bypass()to take rq lock first, and droppingscx_sched_lockfromsysrq_handle_sched_ext_dump()asscx_sched_allis already RCU-traversable andscx_dump_locknow prevents dumping a dead sched. This makes the consistent ordering rq lock ->scx_sched_lock.Reported-by Cheng-Yang Chou <[email protected]>Fixes ebeca1f930ea ("sched_ext: Introduce cgroup sub-sched support")Signed-off-by Tejun Heo <[email protected]>--- kernel/sched/ext.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)kernel/sched/ext.c +1
-1@@ -5097,8 +5097,8 @@ static void scx_bypass(struct scx_sched *sch, bool bypass)struct rq *rq =cpu_rq (cpu);struct task_struct *p, *n; -raw_spin_lock (&scx_sched_lock);raw_spin_rq_lock (rq); +raw_spin_lock (&scx_sched_lock);scx_for_each_descendant_pre (pos, sch) {struct scx_sched_pcpu *pcpu =per_cpu_ptr (pos->pcpu, cpu);
kernel/sched/ext.c static void sysrq_handle_sched_ext_dump(u8 key) · line 7244
Quoted 5 lines
kernel/sched/ext.c +0
-2@@ -7240,8 +7240,6 @@ static void sysrq_handle_sched_ext_dump(u8 key)struct scx_exit_info ei = { .kind =SCX_EXIT_NONE , .reason ="SysRq-D" };struct scx_sched *sch; -guard (raw_spinlock_irqsave)(&scx_sched_lock); -
Don't we need RCU protection here?
kernel/sched/ext.c static void sysrq_handle_sched_ext_dump(u8 key) · line 7245
Quoted 3 lines
kernel/sched/ext.c +0
-0list_for_each_entry_rcu (sch, &scx_sched_all, all)scx_dump_state (sch, &ei,0 , false); }
Thanks,
-Andrea
Andrea Righi
reviewerRe: [PATCH 4/5] sched_ext: Fix scx_sched_lock / rq lock ordering
Quoted 48 lines · Andrea Righi
Hi Tejun,
Quoted 45 lines · Tejun Heo
There are two sites that nest rq lock inside
scx_sched_lock:-
scx_bypass()takesscx_sched_lockthen rq lock per CPU to propagate per-cpu bypass flags and re-enqueue tasks.-
sysrq_handle_sched_ext_dump()takesscx_sched_lockto iterate all scheds,scx_dump_state()then takes rq lock per CPU for dump.And
scx_claim_exit()takesscx_sched_lockto propagate exits to descendants. It can be reached fromscx_tick(), BPF kfuncs, and many other paths with rq lock already held, creating the reverse ordering:rq lock ->
scx_sched_lockvs.scx_sched_lock-> rq lockFix by flipping
scx_bypass()to take rq lock first, and droppingscx_sched_lockfromsysrq_handle_sched_ext_dump()asscx_sched_allis already RCU-traversable andscx_dump_locknow prevents dumping a dead sched. This makes the consistent ordering rq lock ->scx_sched_lock.Reported-by Cheng-Yang Chou <[email protected]>Fixes ebeca1f930ea ("sched_ext: Introduce cgroup sub-sched support")Signed-off-by Tejun Heo <[email protected]>--- kernel/sched/ext.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)kernel/sched/ext.c +1
-1@@ -5097,8 +5097,8 @@ static void scx_bypass(struct scx_sched *sch, bool bypass)struct rq *rq =cpu_rq (cpu);struct task_struct *p, *n; -raw_spin_lock (&scx_sched_lock);raw_spin_rq_lock (rq); +raw_spin_lock (&scx_sched_lock);scx_for_each_descendant_pre (pos, sch) {struct scx_sched_pcpu *pcpu =per_cpu_ptr (pos->pcpu, cpu);@@ -7240,8 +7240,6 @@ static void sysrq_handle_sched_ext_dump(u8 key)struct scx_exit_info ei = { .kind =SCX_EXIT_NONE , .reason ="SysRq-D" };struct scx_sched *sch;
Quoted 4 lines
kernel/sched/ext.c static void sysrq_handle_sched_ext_dump(u8 key) · line 7244
Quoted 2 lines
kernel/sched/ext.c +0
-2-guard (raw_spinlock_irqsave)(&scx_sched_lock); -Don't we need RCU protection here?
Nevermind, __handle_sysrq() is already doing rcu_read_lock/unlock(), so this looks good.
Sorry for the noise,
-Andrea
Quoted 7 lines
kernel/sched/ext.c static void sysrq_handle_sched_ext_dump(u8 key) · line 7245
Quoted 4 lines
kernel/sched/ext.c +0
-0list_for_each_entry_rcu (sch, &scx_sched_all, all)scx_dump_state (sch, &ei,0 , false); }Thanks,
-Andrea
Tejun Heo
reviewerPATCH 5/5 sched_ext: Reject sub-sched attachment to a disabled parent
In reply to Tejun Heoscx_claim_exit() propagates exits to descendants under scx_sched_lock. A sub-sched being attached concurrently could be missed if it links after the propagation. Check the parent's exit_kind in scx_link_sched() under scx_sched_lock to interlock against scx_claim_exit() - either the parent sees the child in its iteration or the child sees the parent's non-NONE exit_kind and fails attachment.
--- kernel/sched/ext.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
kernel/sched/ext.c +16 -0
@@ -5247,6 +5247,17 @@ static s32 scx_link_sched(struct scx_sched *sch)s32 ret;if (parent) { +/* +* scx_claim_exit() propagates exit_kind transition to +* its sub-scheds while holding scx_sched_lock - either +* we can see the parent's non-NONE exit_kind or the +* parent can shoot us down. +*/ +if (atomic_read (&parent->exit_kind) !=SCX_EXIT_NONE ) { +scx_error (sch,"parent disabled" ); +return -ENOENT ; + } + ret =rhashtable_lookup_insert_fast (&scx_sched_hash, &sch->hash_node, scx_sched_hash_params);if (ret) {
@@ -5638,6 +5649,11 @@ static bool scx_claim_exit(struct scx_sched *sch, enum scx_exit_kind kind)* serialized, running them in separate threads allows parallelizing * ops.exit(), which can take arbitrarily long prolonging bypass mode. * +* To guarantee forward progress, this propagation must be in-line so +* that ->aborting is synchronously asserted for all sub-scheds. The +* propagation is also the interlocking point against sub-sched +* attachment. See scx_link_sched(). +* * This doesn't cause recursions as propagation only takes place for * non-propagation exits. */
Signature · 1 line
2.53.0
Quoted 17 lines · Tejun Heo
Hello,
Cheng-Yang reported a lockdep circular dependency between
scx_sched_lockandrq->__lock.scx_bypass()andsysrq_handle_sched_ext_dump()takescx_sched_lock-> rq lock, whilescx_claim_exit()(reachable from many paths with rq lock held) takes rq ->scx_sched_lock. In addition,scx_disable()directly callingkthread_queue_work()underscx_sched_lockcreates another chain throughworker->lock->pi_lock->rq->__lock.This patchset fixes these issues:
1. Fix wrong
sub_detachop check.
2. Addscx_dump_lockanddump_disabledto decouple dump fromscx_sched_lock.
3. Always bouncescx_disable()throughirq_workto avoid lock nesting.
4. Flipscx_bypass()lock order and dropscx_sched_lockfrom sysrq dump.
5. Reject sub-sched attachment to a disabled parent.
Quoted 5 lines
Tested on three machines (16-CPU QEMU, 192-CPU dual-socket EPYC, AMD Ryzen) with lockdep trigger tests and an 11-test stress suite covering attach/detach, nesting, reverse teardown, rapid cycling, error injection, SysRq-D/S dump/exit, and combined stress. Lockdep triggered on baseline, clean after patches.
With the comment from Cheng-Yang about fixing the link in patch 4/5.
Thanks,
-Andrea
Applied to sched_ext/for-7.1 with Andrea's Reviewed-by tags and corrected Link tag in patch 4.
Thanks.
--
tejun