mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] sched/core: Avoid atomic cpuset_lock() in __sched_setscheduler()
@ 2026-11-25 22:27 Elkin Cruz
  2026-11-26 11:12 ` Juri Lelli
  0 siblings, 1 reply; 2+ messages in thread
From: Elkin Cruz @ 2026-11-25 22:27 UTC (permalink / raw)
  To: Ingo Molnar, Peter Zijlstra, Juri Lelli, Vincent Guittot
  Cc: Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
	Valentin Schneider, K Prateek Nayak, linux-kernel, llvm, irogers,
	Elkin Cruz, stable

When Magic SysRq-N invokes normalize_rt_tasks(), it holds
read_lock(&tasklist_lock) in atomic context and calls
__sched_setscheduler(p, &attr, false, false) with pi == false to reset
real-time and deadline tasks to SCHED_NORMAL.

For a SCHED_DEADLINE task, dl_policy(p->policy) is true, so
__sched_setscheduler() calls cpuset_lock() (mutex_lock(&cpuset_mutex))
while in atomic context under tasklist_lock and then skips
cpuset_unlock() on exit because unlocking is gated on if (pi), leaving
cpuset_mutex permanently locked.

Guard cpuset_lock() with pi (matching the non-atomic caller contract,
where switched_from_dl() releases deadline utilization under rq/dl_bw
spinlocks) and call cpuset_unlock() on exit whenever cpuset_locked is
set.

Fixes: 111cd11bbc54 ("sched/cpuset: Bring back cpuset_mutex")
Signed-off-by: Elkin Cruz <elkin@google.com>
Cc: stable@vger.kernel.org
---

I am trying out new Clang static analysis checks for kernel bug
classes, and they flagged this code as possibly buggy. The findings
were triaged/fixed and commit descriptions were drafted with help of AI,
then checked against linux-next (not runtime-tested on hardware).

Thanks,
Elkin

 kernel/sched/syscalls.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/kernel/sched/syscalls.c b/kernel/sched/syscalls.c
index a5ff3c47e9ea..f44fc426404c 100644
--- a/kernel/sched/syscalls.c
+++ b/kernel/sched/syscalls.c
@@ -575,7 +575,7 @@ int __sched_setscheduler(struct task_struct *p,
 	 * SCHED_DEADLINE bandwidth accounting relies on stable cpusets
 	 * information.
 	 */
-	if (dl_policy(policy) || dl_policy(p->policy)) {
+	if (pi && (dl_policy(policy) || dl_policy(p->policy))) {
 		cpuset_locked = true;
 		cpuset_lock();
 	}
@@ -724,11 +724,11 @@ int __sched_setscheduler(struct task_struct *p,
 	head = splice_balance_callbacks(rq);
 	task_rq_unlock(rq, p, &rf);
 
-	if (pi) {
-		if (cpuset_locked)
-			cpuset_unlock();
+	if (cpuset_locked)
+		cpuset_unlock();
+
+	if (pi)
 		rt_mutex_adjust_pi(p);
-	}
 
 	/* Run balance callbacks after we've adjusted the PI chain: */
 	balance_callbacks(rq, head);
-- 
2.56.1.839.g22a8290db9-goog


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

end of thread, other threads:[~2026-11-26 11:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-25 22:27 [PATCH] sched/core: Avoid atomic cpuset_lock() in __sched_setscheduler() Elkin Cruz
2026-11-26 11:12 ` Juri Lelli

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®