Skip to contentSkip to conversation

sched: Remove a preempt-disable section in rt_mutex_setprio()

PATCH Superseded Sebastian Andrzej Siewior 3 participants 4 messages Jun 10, 2025 – Jun 11, 2025 Scheduler

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 sched: Remove a preempt-disable section in rt_mutex_setprio()

rt_mutex_setprio() has only one caller: rt_mutex_adjust_prio(). It expects that task_struct::pi_lock and rt_mutex_base::wait_lock are held. Both locks are raw_spinlock_t and are acquired with disabled interrupts.

Nevertheless rt_mutex_setprio() disables preemption while invoking __balance_callbacks() and raw_spin_rq_unlock(). Even if one of the balance callbacks unlocks the rq then it must not enable interrupts because rt_mutex_base::wait_lock is still locked.
Therefore interrupts should remain disabled and disabling preemption is not needed.

Commit 4c9a4bc89a9cc ("sched: Allow balance callbacks for check_class_changed()") adds a preempt-disable section to rt_mutex_setprio() and __sched_setscheduler(). In __sched_setscheduler() the preemption is disabled before rq is unlocked and interrupts enabled but I don't see why it makes a difference in rt_mutex_setprio().

Remove the preempt_disable() section from rt_mutex_setprio().

---

v1…v2: https://lore.kernel.org/all/[email protected]/
- Repost without RFC

 kernel/sched/core.c | 4 ----
 1 file changed, 4 deletions(-)
kernel/sched/core.c +0 -4
index dce50fa57471d..362d8ab888748 100644
@@ -7292,14 +7292,10 @@ void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task)
 
 	check_class_changed(rq, p, prev_class, oldprio);
 out_unlock:
-	/* Avoid rq from going away on us: */
-	preempt_disable();
 
 	rq_unpin_lock(rq, &rf);
 	__balance_callbacks(rq);
 	raw_spin_rq_unlock(rq);
-
-	preempt_enable();
 }
 #endif
 
Signature · 1 line
2.49.0
maintainer
#1 lore raw
Quoted 33 lines · Sebastian Andrzej Siewior

rt_mutex_setprio() has only one caller: rt_mutex_adjust_prio(). It expects that task_struct::pi_lock and rt_mutex_base::wait_lock are held. Both locks are raw_spinlock_t and are acquired with disabled interrupts.

Nevertheless rt_mutex_setprio() disables preemption while invoking __balance_callbacks() and raw_spin_rq_unlock(). Even if one of the balance callbacks unlocks the rq then it must not enable interrupts because rt_mutex_base::wait_lock is still locked.
Therefore interrupts should remain disabled and disabling preemption is not needed.

Commit 4c9a4bc89a9cc ("sched: Allow balance callbacks for check_class_changed()") adds a preempt-disable section to rt_mutex_setprio() and __sched_setscheduler(). In __sched_setscheduler() the preemption is disabled before rq is unlocked and interrupts enabled but I don't see why it makes a difference in rt_mutex_setprio().

Remove the preempt_disable() section from rt_mutex_setprio().

---

v1…v2: https://lore.kernel.org/all/[email protected]/
- Repost without RFC

 kernel/sched/core.c | 4 ----
 1 file changed, 4 deletions(-)
kernel/sched/core.c +0 -0
index dce50fa57471d..362d8ab888748 100644

kernel/sched/core.c void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task) · line 7296

Quoted 5 lines
kernel/sched/core.c +0 -2
@@ -7292,14 +7292,10 @@ void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task)
 
 	check_class_changed(rq, p, prev_class, oldprio);
 out_unlock:
-	/* Avoid rq from going away on us: */
-	preempt_disable();

Perhaps add:

	/* IRQs are still disabled */

or something to that effect such that it is obvious from reading the code that dropping the lock will not enable preemption?

Quoted 12 lines
kernel/sched/core.c +0 -2
 	rq_unpin_lock(rq, &rf);
 	__balance_callbacks(rq);
 	raw_spin_rq_unlock(rq);
-
-	preempt_enable();
 }
 #endif
 
Signature · 1 line
2.49.0
#2 lore raw

PATCH v3 1/1 sched: Remove a preempt-disable section in rt_mutex_setprio()

rt_mutex_setprio() has only one caller: rt_mutex_adjust_prio(). It expects that task_struct::pi_lock and rt_mutex_base::wait_lock are held. Both locks are raw_spinlock_t and are acquired with disabled interrupts.

Nevertheless rt_mutex_setprio() disables preemption while invoking __balance_callbacks() and raw_spin_rq_unlock(). Even if the possible balance callbacks unlock the rq they must not enable interrupts as I doubt that they also unlock rt_mutex_base::wait_lock. Therefore interrupts should remain disabled and disabling preemption is not needed.

Commit 4c9a4bc89a9cc ("sched: Allow balance callbacks for check_class_changed()") adds a preempt-disable section to rt_mutex_setprio() and __sched_setscheduler(). In __sched_setscheduler() the preemption is disabled before rq is unlocked and interrupts enabled but I don't see why it makes a difference in rt_mutex_setprio().

---
v2…v3: https://lore.kernel.org/all/[email protected]/
- Add a comment why IRQs are still disabled.

v1…v2: https://lore.kernel.org/all/[email protected]/
- Repost without RFC

 kernel/sched/core.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
kernel/sched/core.c +1 -4
index dce50fa57471d..537212b33fd84 100644
@@ -7292,14 +7292,11 @@ void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task)
 
 	check_class_changed(rq, p, prev_class, oldprio);
 out_unlock:
-	/* Avoid rq from going away on us: */
-	preempt_disable();
+	/* Caller holds task_struct::pi_lock, IRQs are still disabled */
 
 	rq_unpin_lock(rq, &rf);
 	__balance_callbacks(rq);
 	raw_spin_rq_unlock(rq);
-
-	preempt_enable();
 }
 #endif
 
Signature · 1 line
2.49.0
#3 lore raw
In reply to Peter Zijlstra
Quoted 11 lines · On Wed, 11 Jun 2025 11:03:06 +0200 Peter Zijlstra <[email protected]> wrote:

kernel/sched/core.c void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task) · line 7296

Quoted 8 lines
kernel/sched/core.c +0 -2
@@ -7292,14 +7292,10 @@ void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task)
 
 	check_class_changed(rq, p, prev_class, oldprio);
 out_unlock:
-	/* Avoid rq from going away on us: */
-	preempt_disable();  

Perhaps add:

Quoted 4 lines
	/* IRQs are still disabled */

or something to that effect such that it is obvious from reading the code that dropping the lock will not enable preemption?

Hmm, wouldn't lockdep_assert_irqs_disabled() be better than a comment. It lets people know that interrupts are disabled and when lockdep is enabled it also makes sure they are.

-- Steve