[PATCH] sched_ext: Replace rq_lock() to raw_spin_rq_lock() in scx_ops_bypass()

Changwoo Min posted 1 patch 11 months, 2 weeks ago
There is a newer version of this series
kernel/sched/ext.c | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
[PATCH] sched_ext: Replace rq_lock() to raw_spin_rq_lock() in scx_ops_bypass()
Posted by Changwoo Min 11 months, 2 weeks ago
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().

Signed-off-by: Changwoo Min <changwoo@igalia.com>
---
 kernel/sched/ext.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c
index 8fe64c27004e..741398f3e730 100644
--- a/kernel/sched/ext.c
+++ b/kernel/sched/ext.c
@@ -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);
-- 
2.47.1
Re: [PATCH] sched_ext: Replace rq_lock() to raw_spin_rq_lock() in scx_ops_bypass()
Posted by Andrea Righi 11 months, 2 weeks ago
Hi Changwoo,

On Wed, Jan 08, 2025 at 11:55:21AM +0900, Changwoo Min wrote:
> 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().

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:

Fixes: 0e7ffff1b811 ("scx: Fix raciness in scx_ops_bypass()")

> 
> Signed-off-by: Changwoo Min <changwoo@igalia.com>
> ---
>  kernel/sched/ext.c | 7 +++----
>  1 file changed, 3 insertions(+), 4 deletions(-)
> 
> diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c
> index 8fe64c27004e..741398f3e730 100644
> --- a/kernel/sched/ext.c
> +++ b/kernel/sched/ext.c
> @@ -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);
>  		}

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);

>  
> -		rq_unlock(rq, &rf);
> +		raw_spin_rq_unlock(rq);
>  

And remove the following:

>  		/* resched to restore ticks and idle state */
>  		resched_cpu(cpu);

Thanks,
-Andrea
Re: [PATCH] sched_ext: Replace rq_lock() to raw_spin_rq_lock() in scx_ops_bypass()
Posted by Changwoo Min 11 months, 2 weeks ago
Hi Andrea,

Thank you for the review and suggestion!

On 25. 1. 8. 16:16, Andrea Righi wrote:
> 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.

> 
> Moreover, we can also add:
> 
> Fixes: 0e7ffff1b811 ("scx: Fix raciness in scx_ops_bypass()")
Will add this in the next version.

> 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);
> 
>>   
>> -		rq_unlock(rq, &rf);
>> +		raw_spin_rq_unlock(rq);
>>   
> 
> And remove the following:
> 
>>   		/* 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