[PATCH] sched/core: Avoid atomic cpuset_lock() in __sched_setscheduler()

From: Elkin Cruz

Date: Wed Nov 25 2026 - 17:28:06 EST


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@xxxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
---

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