All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] sched/ext: convert scx_tasks_lock to raw spinlock
@ 2025-11-12 18:07 Emil Tsalapatis
  2025-11-12 18:44 ` Andrea Righi
  2025-11-12 18:45 ` Tejun Heo
  0 siblings, 2 replies; 3+ messages in thread
From: Emil Tsalapatis @ 2025-11-12 18:07 UTC (permalink / raw)
  To: sched-ext; +Cc: tj, arighi, changwoo, void, Emil Tsalapatis, Emil Tsalapatis

Update scx_task_locks so that it's safe to lock/unlock in a
non-sleepable context in PREEMPT_RT kernels. scx_task_locks is
(non-raw) spinlock used to protect the list of tasks under SCX.
This list is updated during from finish_task_switch(), which
cannot sleep. Regular spinlocks can be locked in such a context
in non-RT kernels, but are sleepable under when CONFIG_PREEMPT_RT=y.

Convert scx_task_locks into a raw spinlock, which is not sleepable
even on RT kernels.

Sample backtrace:

<TASK>
dump_stack_lvl+0x83/0xa0
__might_resched+0x14a/0x200
rt_spin_lock+0x61/0x1c0
? sched_ext_dead+0x2d/0xf0
? lock_release+0xc6/0x280
sched_ext_dead+0x2d/0xf0
? srso_alias_return_thunk+0x5/0xfbef5
finish_task_switch.isra.0+0x254/0x360
__schedule+0x584/0x11d0
? srso_alias_return_thunk+0x5/0xfbef5
? srso_alias_return_thunk+0x5/0xfbef5
? tick_nohz_idle_exit+0x7e/0x120
schedule_idle+0x23/0x40
cpu_startup_entry+0x29/0x30
start_secondary+0xf8/0x100
common_startup_64+0x13e/0x148
</TASK>


Signed-off-by: Emil Tsalapatis <emil@etsalapatis.com>
---
 kernel/sched/ext.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c
index bbd7529fa420..4b1007e06e9a 100644
--- a/kernel/sched/ext.c
+++ b/kernel/sched/ext.c
@@ -25,7 +25,7 @@ static struct scx_sched __rcu *scx_root;
  * guarantee system safety. Maintain a dedicated task list which contains every
  * task between its fork and eventual free.
  */
-static DEFINE_SPINLOCK(scx_tasks_lock);
+static DEFINE_RAW_SPINLOCK(scx_tasks_lock);
 static LIST_HEAD(scx_tasks);
 
 /* ops enable/disable */
@@ -520,7 +520,7 @@ static void scx_task_iter_start(struct scx_task_iter *iter)
 {
 	memset(iter, 0, sizeof(*iter));
 
-	spin_lock_irq(&scx_tasks_lock);
+	raw_spin_lock_irq(&scx_tasks_lock);
 
 	iter->cursor = (struct sched_ext_entity){ .flags = SCX_TASK_CURSOR };
 	list_add(&iter->cursor.tasks_node, &scx_tasks);
@@ -549,14 +549,14 @@ static void scx_task_iter_unlock(struct scx_task_iter *iter)
 	__scx_task_iter_rq_unlock(iter);
 	if (iter->list_locked) {
 		iter->list_locked = false;
-		spin_unlock_irq(&scx_tasks_lock);
+		raw_spin_unlock_irq(&scx_tasks_lock);
 	}
 }
 
 static void __scx_task_iter_maybe_relock(struct scx_task_iter *iter)
 {
 	if (!iter->list_locked) {
-		spin_lock_irq(&scx_tasks_lock);
+		raw_spin_lock_irq(&scx_tasks_lock);
 		iter->list_locked = true;
 	}
 }
@@ -2983,9 +2983,9 @@ void scx_post_fork(struct task_struct *p)
 		}
 	}
 
-	spin_lock_irq(&scx_tasks_lock);
+	raw_spin_lock_irq(&scx_tasks_lock);
 	list_add_tail(&p->scx.tasks_node, &scx_tasks);
-	spin_unlock_irq(&scx_tasks_lock);
+	raw_spin_unlock_irq(&scx_tasks_lock);
 
 	percpu_up_read(&scx_fork_rwsem);
 }
@@ -3009,9 +3009,9 @@ void sched_ext_dead(struct task_struct *p)
 {
 	unsigned long flags;
 
-	spin_lock_irqsave(&scx_tasks_lock, flags);
+	raw_spin_lock_irqsave(&scx_tasks_lock, flags);
 	list_del_init(&p->scx.tasks_node);
-	spin_unlock_irqrestore(&scx_tasks_lock, flags);
+	raw_spin_unlock_irqrestore(&scx_tasks_lock, flags);
 
 	/*
 	 * @p is off scx_tasks and wholly ours. scx_enable()'s READY -> ENABLED
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] sched/ext: convert scx_tasks_lock to raw spinlock
  2025-11-12 18:07 [PATCH] sched/ext: convert scx_tasks_lock to raw spinlock Emil Tsalapatis
@ 2025-11-12 18:44 ` Andrea Righi
  2025-11-12 18:45 ` Tejun Heo
  1 sibling, 0 replies; 3+ messages in thread
From: Andrea Righi @ 2025-11-12 18:44 UTC (permalink / raw)
  To: Emil Tsalapatis; +Cc: sched-ext, tj, changwoo, void, Emil Tsalapatis

Hi Emil,

On Wed, Nov 12, 2025 at 10:07:55AM -0800, Emil Tsalapatis wrote:
> Update scx_task_locks so that it's safe to lock/unlock in a
> non-sleepable context in PREEMPT_RT kernels. scx_task_locks is
> (non-raw) spinlock used to protect the list of tasks under SCX.
> This list is updated during from finish_task_switch(), which
> cannot sleep. Regular spinlocks can be locked in such a context
> in non-RT kernels, but are sleepable under when CONFIG_PREEMPT_RT=y.
> 
> Convert scx_task_locks into a raw spinlock, which is not sleepable
> even on RT kernels.
> 
> Sample backtrace:
> 
> <TASK>
> dump_stack_lvl+0x83/0xa0
> __might_resched+0x14a/0x200
> rt_spin_lock+0x61/0x1c0
> ? sched_ext_dead+0x2d/0xf0
> ? lock_release+0xc6/0x280
> sched_ext_dead+0x2d/0xf0
> ? srso_alias_return_thunk+0x5/0xfbef5
> finish_task_switch.isra.0+0x254/0x360
> __schedule+0x584/0x11d0
> ? srso_alias_return_thunk+0x5/0xfbef5
> ? srso_alias_return_thunk+0x5/0xfbef5
> ? tick_nohz_idle_exit+0x7e/0x120
> schedule_idle+0x23/0x40
> cpu_startup_entry+0x29/0x30
> start_secondary+0xf8/0x100
> common_startup_64+0x13e/0x148
> </TASK>
> 
> 
> Signed-off-by: Emil Tsalapatis <emil@etsalapatis.com>

Looks good.

Acked-by: Andrea Righi <arighi@nvidia.com>

Thanks,
-Andrea

> ---
>  kernel/sched/ext.c | 16 ++++++++--------
>  1 file changed, 8 insertions(+), 8 deletions(-)
> 
> diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c
> index bbd7529fa420..4b1007e06e9a 100644
> --- a/kernel/sched/ext.c
> +++ b/kernel/sched/ext.c
> @@ -25,7 +25,7 @@ static struct scx_sched __rcu *scx_root;
>   * guarantee system safety. Maintain a dedicated task list which contains every
>   * task between its fork and eventual free.
>   */
> -static DEFINE_SPINLOCK(scx_tasks_lock);
> +static DEFINE_RAW_SPINLOCK(scx_tasks_lock);
>  static LIST_HEAD(scx_tasks);
>  
>  /* ops enable/disable */
> @@ -520,7 +520,7 @@ static void scx_task_iter_start(struct scx_task_iter *iter)
>  {
>  	memset(iter, 0, sizeof(*iter));
>  
> -	spin_lock_irq(&scx_tasks_lock);
> +	raw_spin_lock_irq(&scx_tasks_lock);
>  
>  	iter->cursor = (struct sched_ext_entity){ .flags = SCX_TASK_CURSOR };
>  	list_add(&iter->cursor.tasks_node, &scx_tasks);
> @@ -549,14 +549,14 @@ static void scx_task_iter_unlock(struct scx_task_iter *iter)
>  	__scx_task_iter_rq_unlock(iter);
>  	if (iter->list_locked) {
>  		iter->list_locked = false;
> -		spin_unlock_irq(&scx_tasks_lock);
> +		raw_spin_unlock_irq(&scx_tasks_lock);
>  	}
>  }
>  
>  static void __scx_task_iter_maybe_relock(struct scx_task_iter *iter)
>  {
>  	if (!iter->list_locked) {
> -		spin_lock_irq(&scx_tasks_lock);
> +		raw_spin_lock_irq(&scx_tasks_lock);
>  		iter->list_locked = true;
>  	}
>  }
> @@ -2983,9 +2983,9 @@ void scx_post_fork(struct task_struct *p)
>  		}
>  	}
>  
> -	spin_lock_irq(&scx_tasks_lock);
> +	raw_spin_lock_irq(&scx_tasks_lock);
>  	list_add_tail(&p->scx.tasks_node, &scx_tasks);
> -	spin_unlock_irq(&scx_tasks_lock);
> +	raw_spin_unlock_irq(&scx_tasks_lock);
>  
>  	percpu_up_read(&scx_fork_rwsem);
>  }
> @@ -3009,9 +3009,9 @@ void sched_ext_dead(struct task_struct *p)
>  {
>  	unsigned long flags;
>  
> -	spin_lock_irqsave(&scx_tasks_lock, flags);
> +	raw_spin_lock_irqsave(&scx_tasks_lock, flags);
>  	list_del_init(&p->scx.tasks_node);
> -	spin_unlock_irqrestore(&scx_tasks_lock, flags);
> +	raw_spin_unlock_irqrestore(&scx_tasks_lock, flags);
>  
>  	/*
>  	 * @p is off scx_tasks and wholly ours. scx_enable()'s READY -> ENABLED
> -- 
> 2.47.3
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] sched/ext: convert scx_tasks_lock to raw spinlock
  2025-11-12 18:07 [PATCH] sched/ext: convert scx_tasks_lock to raw spinlock Emil Tsalapatis
  2025-11-12 18:44 ` Andrea Righi
@ 2025-11-12 18:45 ` Tejun Heo
  1 sibling, 0 replies; 3+ messages in thread
From: Tejun Heo @ 2025-11-12 18:45 UTC (permalink / raw)
  To: Emil Tsalapatis; +Cc: sched-ext, arighi, changwoo, void, Emil Tsalapatis

Applied to sched_ext/for-6.18-fixes with the function name adjusted to
sched_ext_free() to match the current code.

Thanks.

--
tejun

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-11-12 18:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-12 18:07 [PATCH] sched/ext: convert scx_tasks_lock to raw spinlock Emil Tsalapatis
2025-11-12 18:44 ` Andrea Righi
2025-11-12 18:45 ` Tejun Heo

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.