sched: Fix cpuset_mutex double unlock in __sched_setscheduler()
Subsystems (3)
PATCH 1/1 sched: Fix cpuset_mutex double unlock in __sched_setscheduler()
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:
[<ffffffff9a17cacc>] __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 <pthread.h>
#include <stdint.h>
#include <sys/syscall.h>
#include <unistd.h>
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.
---
Sashiko reported this while reviewing [1], which adds another exit from the second pass. The bug does not depend on that patch.
Tested on x86_64 with lockdep and DEBUG_MUTEXES: the splat fires without this patch and not with it.
Also tested on PREEMPT_RT with lockdep, with DEBUG_RT_MUTEXES only and with
neither: each kernel oopses within seconds without this patch, and runs clean for 30 seconds with it.
The 2us reservation only keeps the test running: tasks leaving SCHED_DEADLINE while asleep leak total_bw [2], so a 1ms/10ms reservation stops being admitted after 38 switches on 4 CPUs, and 2us after about 2 million.
[1] https://lore.kernel.org/[email protected]/
[2] https://lore.kernel.org/[email protected]/
kernel/sched/syscalls.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/sched/syscalls.c +3 -1
@@ -640,8 +640,10 @@ int __sched_setscheduler(struct task_struct *p,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; }
Signature · 1 line
2.53.0
Quoted 159 lines · Andrea Parri
A
sched_setattr()withSCHED_FLAG_KEEP_POLICYon aSCHED_DEADLINEtask unlockscpuset_mutextwice 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: [<ffffffff9a17cacc>] __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()samplesp->policybefore taking any lock, and restarts from the recheck label ifp->policydiffers under the rq lock. The restart dropscpuset_mutexbut leavescpuset_lockedset, 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, orCONFIG_DEBUG_RT_MUTEXESonPREEMPT_RT, nothing warns. The second unlock still clears the owner field regardless of who holds the mutex, so another task can losecpuset_mutexinside its critical section. EnteringSCHED_DEADLINEneedsCAP_SYS_NICE, but a racing caller that owns the task does not need it if it samples a policy other thanSCHED_DEADLINE.Clear
cpuset_lockedwhen 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_RTit crashes the kernel within seconds, which this also fixes:#define _GNU_SOURCE #include <pthread.h> #include <stdint.h> #include <sys/syscall.h> #include <unistd.h> 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_RTcrash and write this patch and its test.Fixes 111cd11bbc54 ("sched/cpuset: Bring back cpuset_mutex")Reported-by Sashiko <[email protected]>Signed-off-by Andrea Parri <[email protected]>---
Sashiko reported this while reviewing [1], which adds another exit from the second pass. The bug does not depend on that patch.Tested on
x86_64with lockdep andDEBUG_MUTEXES: the splat fires without this patch and not with it.Also tested on
PREEMPT_RTwith lockdep, withDEBUG_RT_MUTEXESonly and with
neither: each kernel oopses within seconds without this patch, and runs clean for 30 seconds with it.The 2us reservation only keeps the test running: tasks leaving
SCHED_DEADLINEwhile asleep leaktotal_bw[2], so a 1ms/10ms reservation stops being admitted after 38 switches on 4 CPUs, and 2us after about 2 million.[1] https://lore.kernel.org/[email protected]/
[2] https://lore.kernel.org/[email protected]/kernel/sched/syscalls.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)kernel/sched/syscalls.c +0
-0@@ -640,8 +640,10 @@ int __sched_setscheduler(struct task_struct *p,if (unlikely (oldpolicy !=-1 && oldpolicy != p->policy)) {
kernel/sched/syscalls.c int __sched_setscheduler(struct task_struct *p, · line 649
Quoted 10 lines
kernel/sched/syscalls.c +3
-1policy = oldpolicy =-1 ;task_rq_unlock (rq, p, &rf); -if (cpuset_locked) +if (cpuset_locked) {cpuset_unlock (); + cpuset_locked = false; + } goto recheck; }
LGTM
On Mon, Nov 02, 2026 at 02:46:05PM +0100, Andrea Parri wrote:
:
Quoted 2 lines
A
sched_setattr()withSCHED_FLAG_KEEP_POLICYon aSCHED_DEADLINEtask unlockscpuset_mutextwice if the task's policy changes concurrently:
Uh-oh.
Quoted 22 lines
kernel/sched/syscalls.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)kernel/sched/syscalls.c +3
-1@@ -640,8 +640,10 @@ int __sched_setscheduler(struct task_struct *p,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; }Signature · 1 line
2.53.0
So the issue is that when tasks policy changes before we come to this check, we don't clear cpuset_locked while calling cpuset_unlock().
In the other calls of cpuset_unlock() this is not a problem since we just return and don't use the local cpuset_locked. But on this one we do. And if we don't retake the lock it just stays true. Even if we didn't claimed the lock. And that causes bad, bad, bad, bad problems.
Thanks,
Kayra
Hello,
Quoted 124 lines · Andrea Parri
A
sched_setattr()withSCHED_FLAG_KEEP_POLICYon aSCHED_DEADLINEtask unlockscpuset_mutextwice 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:[<ffffffff9a17cacc>] __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()samplesp->policybefore taking any lock, and restarts from the recheck label ifp->policydiffers under the rq lock. The restart dropscpuset_mutexbut leavescpuset_lockedset, 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, orCONFIG_DEBUG_RT_MUTEXESonPREEMPT_RT, nothing warns. The second unlock still clears the owner field regardless of who holds the mutex, so another task can losecpuset_mutexinside its critical section. EnteringSCHED_DEADLINEneedsCAP_SYS_NICE, but a racing caller that owns the task does not need it if it samples a policy other thanSCHED_DEADLINE.Clear
cpuset_lockedwhen 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_RTit crashes the kernel within seconds, which this also fixes:#define _GNU_SOURCE #include <pthread.h> #include <stdint.h> #include <sys/syscall.h> #include <unistd.h> 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 volatilepid_ttarget;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;}
Quoted 8 lines
An LLM helped find the
PREEMPT_RTcrash and write this patch and its test.Fixes 111cd11bbc54 ("sched/cpuset: Bring back cpuset_mutex")Reported-by Sashiko <[email protected]>Signed-off-by Andrea Parri <[email protected]>---
The fix looks good to me.
As I said in the other fix, I'm not sure we want to keep the reproducer in the changelog, or if it's better to start collecting them somewhere else (it might be easier to actually run them).
Thanks,
Juri
On Tue, Nov 03, 2026 at 11:40:32AM +0100, Juri Lelli wrote:
[...]
Quoted 3 lines
The fix looks good to me.
Reviewed-by Juri Lelli <[email protected]>
Thanks!
Quoted 3 lines
As I said in the other fix, I'm not sure we want to keep the reproducer in the changelog, or if it's better to start collecting them somewhere else (it might be easier to actually run them).
Same as in the other thread: fine with moving it to your tests, and if you'd like a v2 with the program replaced by a link, just let me know.
Thanks,
Andrea
The following commit has been merged into the sched/urgent branch of tip:
The following commit has been merged into the sched/urgent branch of tip:
Commit-ID: 986efdec58355e41e213bbef125da2d2b766eee0 Gitweb: https://git.kernel.org/tip/986efdec58355e41e213bbef125da2d2b766eee0 Author: Andrea Parri <[email protected]> AuthorDate: Mon, 02 Nov 2026 14:46:05 +01:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Fri, 06 Nov 2026 11:45:17 +01:00
sched: Fix cpuset_mutex double unlock in __sched_setscheduler()
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:
[<ffffffff9a17cacc>] __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 <pthread.h>
#include <stdint.h>
#include <sys/syscall.h>
#include <unistd.h>
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.
--- kernel/sched/syscalls.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/sched/syscalls.c +3 -1
@@ -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; }
[tip: sched/core] sched/core: Fix cpuset_mutex double unlock in __sched_setscheduler()
In reply to Andrea ParriThe following commit has been merged into the sched/core branch of tip:
The following commit has been merged into the sched/core branch of tip:
Commit-ID: f7141257dd67d4936f906a6cfd1d0007721f6898 Gitweb: https://git.kernel.org/tip/f7141257dd67d4936f906a6cfd1d0007721f6898 Author: Andrea Parri <[email protected]> AuthorDate: Mon, 02 Nov 2026 14:46:05 +01:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Mon, 09 Nov 2026 10:09:18 +01:00
sched/core: Fix cpuset_mutex double unlock in __sched_setscheduler()
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:
[<ffffffff9a17cacc>] __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 <pthread.h>
#include <stdint.h>
#include <sys/syscall.h>
#include <unistd.h>
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.
--- kernel/sched/syscalls.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/sched/syscalls.c +3 -1
@@ -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; }