From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795619946; b=yGXsIJA+ewqU73zDdxMQYwJIh1dxmNh9qyHsYqlXNqppYRl5udddtZR517iKICWSpE3b gZZethlFA1CAj8lJL2NMJeLjmMgFbd39K7MM5cizQGXlz6bG8HNBxiU8+vpoPyHAX8NOa RFqC6y1svlcDrtPSIq+7FM4kN2NYsnkmDfqoCYTL9bh/2tcIzahDVaXg5TZx6utRUWxvx ZZkQNftmJUvt/mADHH78MSfZCrPkWFldV473u5XPwcXDhE/xw3ADAHA0TpWM+MtBle9y8 awqRM6Ae9y+MkhbJBGr5JIEr9AB6sE1UFj8GW4L08ZnP2Sq5Gr0cXgJkw8Ap6e5drMw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795619946; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=zcn42t6tgu/9zchOpYUT2b8KFxSicEoqSrcxDbY6CN0=; b=kiCglpSs4s2C7WLQoV/rfA68F9A6v6VVvrZ0k6781EED1JwhLy+JEsYEQbOgQBPpA/CE +zm0SeufUChywo28FiU9l53xGYd9wYlhJ91zhRx1LhqoENL0KE+g42C7dfecq8xmjY9k+ 7Y2zzD7TjIo37ENxq2EsTlPoS5ZwLsy726Zhrma90ej+MPcWkRK2xKJY3jt4ZxduykK7Q Iwkbimya1JK+ZLeAuaPlhgnIbF69Js0pzb+sQ7ixYfCCz9/Xnv3/yqJDUlePQxPu7Qkcp j4++GSfbQefzbFxBJXbBbi0QXW+DhA+LhfQ01bN8kcH58924pDhp8f9FdpzelUKOsGQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=yWenTxDY; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 95EE6606A323; Wed, 25 Nov 2026 15:19:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=yWenTxDY Received: by smtp.kernel.org (Postfix) with ESMTPSA id 868DF1F000C0; Wed, 25 Nov 2026 15:19:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1795619946; bh=zcn42t6tgu/9zchOpYUT2b8KFxSicEoqSrcxDbY6CN0=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=yWenTxDY0LFS2GBDCTWbW2KPw+EJTz7gqH86zDg21hrNEZM8Kx7pO/Gr3UApJIcIk ei88fqi8krpXDgv20i1wwGEBGumhqdoWqhpotJU+dsgFtz9SXD+RG7oYY/ZbH4JGyY Lwc7eqJ/s+Ay3RkGDiMKOZReqNCfpI76cFE9q2JE= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Sashiko , Andrea Parri , "Peter Zijlstra (Intel)" , Ingo Molnar , Waiman Long , Kayra Cizmeci , Juri Lelli Subject: [PATCH 7.3 0759/1005] sched/core: Fix cpuset_mutex double unlock in __sched_setscheduler() Date: Wed, 25 Nov 2026 15:34:57 +0100 Message-ID: <20261125143839.526694975@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261125143821.729802100@linuxfoundation.org> References: <20261125143821.729802100@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 7.3-stable review patch. If anyone has any objections, please let me know. ------------------ From: Andrea Parri commit f7141257dd67d4936f906a6cfd1d0007721f6898 upstream. A sched_setattr() with SCHED_FLAG_KEEP_POLICY on a SCHED_DEADLINE task unlocks cpuset_mutex twice if the task's policy changes concurrently: WARNING: bad unlock balance detected! 7.4.0-rc1 #1 Not tainted ------------------------------------- repro/126 is trying to release lock (cpuset_mutex) at: [] __sched_setscheduler+0x9cc/0xc40 but there are no more locks to release! ... Call Trace: print_unlock_imbalance_bug.part.0+0xfb/0x110 lock_release+0x286/0x2e0 __mutex_unlock_slowpath+0x1a/0x400 __sched_setscheduler+0x9cc/0xc40 __x64_sys_sched_setattr+0xa3/0x1b0 do_syscall_64+0x106/0x5f0 entry_SYSCALL_64_after_hwframe+0x77/0x7f __sched_setscheduler() samples p->policy before taking any lock, and restarts from the recheck label if p->policy differs under the rq lock. The restart drops cpuset_mutex but leaves cpuset_locked set, so a pass that no longer needs the mutex still unlocks it on its way out: CPU 1 CPU 2 __sched_setscheduler(KEEP_POLICY) policy = p->policy /* DEADLINE */ __sched_setscheduler(SCHED_OTHER) p->policy = SCHED_OTHER cpuset_locked = true cpuset_lock() rq = task_rq_lock(p) oldpolicy != p->policy cpuset_unlock() goto recheck /* no cpuset_lock() this time */ ... if (cpuset_locked) cpuset_unlock() Without CONFIG_DEBUG_MUTEXES, or CONFIG_DEBUG_RT_MUTEXES on PREEMPT_RT, nothing warns. The second unlock still clears the owner field regardless of who holds the mutex, so another task can lose cpuset_mutex inside its critical section. Entering SCHED_DEADLINE needs CAP_SYS_NICE, but a racing caller that owns the task does not need it if it samples a policy other than SCHED_DEADLINE. Clear cpuset_locked when dropping the mutex for the restart. This should be safe because the flag is only tested on the paths leaving the next pass, after that pass has set it again if it took the mutex. The following program, run as root, reproduces the splat, which no longer appears with this applied. On PREEMPT_RT it crashes the kernel within seconds, which this also fixes: #define _GNU_SOURCE #include #include #include #include struct attr { uint32_t size, policy; uint64_t flags; int32_t nice; uint32_t prio; uint64_t runtime, deadline, period; }; static volatile int stop; static volatile pid_t target; static void set(struct attr a) { a.size = sizeof(a); syscall(SYS_sched_setattr, target, &a, 0); } static void *sleeper(void *arg) { target = syscall(SYS_gettid); while (!stop) usleep(1000); return arg; } static void *switcher(void *arg) { while (!stop) { /* SCHED_DEADLINE, 2us every 1s */ set((struct attr){ .policy = 6, .runtime = 2000, .deadline = 1000000000, .period = 1000000000 }); set((struct attr){ .policy = 0 }); /* SCHED_OTHER */ } return arg; } static void *racer(void *arg) { /* KEEP_POLICY | KEEP_PARAMS */ while (!stop) set((struct attr){ .flags = 0x18 }); return arg; } int main(void) { pthread_t t[5]; int i; pthread_create(&t[0], NULL, sleeper, NULL); while (!target) usleep(100); pthread_create(&t[1], NULL, switcher, NULL); for (i = 2; i < 5; i++) pthread_create(&t[i], NULL, racer, NULL); sleep(30); stop = 1; for (i = 0; i < 5; i++) pthread_join(t[i], NULL); return 0; } An LLM helped find the PREEMPT_RT crash and write this patch and its test. Fixes: 111cd11bbc54 ("sched/cpuset: Bring back cpuset_mutex") Reported-by: Sashiko Signed-off-by: Andrea Parri Signed-off-by: Peter Zijlstra (Intel) Signed-off-by: Ingo Molnar Reviewed-by: Waiman Long Reviewed-by: Kayra Cizmeci Reviewed-by: Juri Lelli Cc: stable@vger.kernel.org Link: https://sashiko.dev/#/patchset/20261016062629.2102883-1-sh_def%40163.com Link: https://patch.msgid.link/20261102134606.242743-1-parri.andrea@gmail.com Signed-off-by: Greg Kroah-Hartman --- kernel/sched/syscalls.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) --- a/kernel/sched/syscalls.c +++ b/kernel/sched/syscalls.c @@ -640,8 +640,10 @@ change: if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) { policy = oldpolicy = -1; task_rq_unlock(rq, p, &rf); - if (cpuset_locked) + if (cpuset_locked) { cpuset_unlock(); + cpuset_locked = false; + } goto recheck; }