sched_ext: Replace rq_lock() to raw_spin_rq_lock() in scx_ops_bypass()
PATCH 1/1 sched_ext: Replace rq_lock() to raw_spin_rq_lock() in scx_ops_bypass()
scx_ops_bypass() iterates all CPUs to re-enqueue all the scx tasks. For each CPU, it acquires a lock using rq_lock() regardless of whether a CPU is offline or the CPU is currently running a task in a higher scheduler class (e.g., deadline). The rq_lock() is supposed to be used for online CPUs, and the use of rq_lock() may trigger an unnecessary warning in rq_pin_lock(). Therefore, replace rq_lock() to raw_spin_rq_lock() in scx_ops_bypass().
--- kernel/sched/ext.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
kernel/sched/ext.c +3 -4
@@ -4803,10 +4803,9 @@ static void scx_ops_bypass(bool bypass)*/ for_each_possible_cpu (cpu) {struct rq *rq =cpu_rq (cpu); -struct rq_flags rf;struct task_struct *p, *n; -rq_lock (rq, &rf); +raw_spin_rq_lock (rq);if (bypass) {WARN_ON_ONCE (rq->scx.flags &SCX_RQ_BYPASSING );
@@ -4822,7 +4821,7 @@ static void scx_ops_bypass(bool bypass)* sees scx_rq_bypassing() before moving tasks to SCX. */ if (!scx_enabled ()) { -rq_unlock (rq, &rf); +raw_spin_rq_unlock (rq);continue ; }
@@ -4842,7 +4841,7 @@ static void scx_ops_bypass(bool bypass)sched_enq_and_set_task (&ctx); } -rq_unlock (rq, &rf); +raw_spin_rq_unlock (rq);/* resched to restore ticks and idle state */ resched_cpu (cpu);
Signature · 1 line
2.47.1
Hi Changwoo,
Quoted 7 lines · Changwoo Min
scx_ops_bypass()iterates all CPUs to re-enqueue all the scx tasks. For each CPU, it acquires a lock usingrq_lock()regardless of whether a CPU is offline or the CPU is currently running a task in a higher scheduler class (e.g., deadline). Therq_lock()is supposed to be used for online CPUs, and the use ofrq_lock()may trigger an unnecessary warning inrq_pin_lock(). Therefore, replacerq_lock()toraw_spin_rq_lock()inscx_ops_bypass().
Can we include the warning here? In this way people that are hitting the same warning can search for it and find this fix.
Moreover, we can also add:
Quoted 32 lines
Signed-off-by Changwoo Min <[email protected]>--- kernel/sched/ext.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)kernel/sched/ext.c +2
-3@@ -4803,10 +4803,9 @@ static void scx_ops_bypass(bool bypass)*/ for_each_possible_cpu (cpu) {struct rq *rq =cpu_rq (cpu); -struct rq_flags rf;struct task_struct *p, *n; -rq_lock (rq, &rf); +raw_spin_rq_lock (rq);if (bypass) {WARN_ON_ONCE (rq->scx.flags &SCX_RQ_BYPASSING );@@ -4822,7 +4821,7 @@ static void scx_ops_bypass(bool bypass)* sees scx_rq_bypassing() before moving tasks to SCX. */ if (!scx_enabled ()) { -rq_unlock (rq, &rf); +raw_spin_rq_unlock (rq);continue ; }
kernel/sched/ext.c static void scx_ops_bypass(bool bypass) · line 4842
Quoted 2 lines
kernel/sched/ext.c +0
-0@@ -4842,7 +4841,7 @@ static void scx_ops_bypass(bool bypass)sched_enq_and_set_task (&ctx); }
Maybe we can also do this here since we're already holding the rq lock and irqs are disabled:
/* resched to restore ticks and idle state */ if (cpu == smp_processor_id() || cpu_online(cpu)) resched_curr(rq);
kernel/sched/ext.c static void scx_ops_bypass(bool bypass) · line 4827
Quoted 4 lines
kernel/sched/ext.c +1
-1-rq_unlock (rq, &rf); +raw_spin_rq_unlock (rq);
And remove the following:
kernel/sched/ext.c static void scx_ops_bypass(bool bypass) · line 4847
Quoted 2 lines
kernel/sched/ext.c +0
-0/* resched to restore ticks and idle state */ resched_cpu (cpu);
Thanks,
-Andrea
Hi Andrea,
Thank you for the review and suggestion!
Quoted 2 lines · Andrea Righi
Can we include the warning here? In this way people that are hitting the same warning can search for it and find this fix.
Sure. I will add the warning message.
Quoted 4 lines
Moreover, we can also add:
Fixes 0e7ffff1b811 ("scx: Fix raciness in scx_ops_bypass()")
Will add this in the next version.
Quoted 8 lines
Maybe we can also do this here since we're already holding the rq lock and irqs are disabled:
/* resched to restore ticks and idle state */ if (cpu == smp_processor_id() || cpu_online(cpu)) resched_curr(rq);Quoted 1 line
Quoted 8 lines
Review
Quoted 3 lines
(quoted diff) +1
-1- rq_unlock(rq, &rf); + raw_spin_rq_unlock(rq);And remove the following:
Quoted 2 lines
/* resched to restore ticks and idle state */
resched_cpu(cpu);
This optimization makes sense. I will change it as suggested with one minor change: I will flip the order in the if condition as follows since cpu_online() is more commonly true:
/* resched to restore ticks and idle state */ if (cpu_online(cpu) || cpu == smp_processor_id())
resched_curr(rq);
Regards,
Changwoo Min