From 6616bffa9b52da1338cf8bff9882404e02b9e361 Mon Sep 17 00:00:00 2001 From: Andrea Parri Date: Mon, 2 Nov 2026 14:54:19 +0100 Subject: [PATCH 1/5] sched/deadline: Fix spurious -EPERM for migration-disabled tasks sched_setattr() can refuse to make a task SCHED_DEADLINE with -EPERM, even though the task's affinity covers the whole root domain. A retry can succeed, but the error blames an affinity the task does not have. __sched_setscheduler() tests p->cpus_ptr, which is not always the task's affinity. When a task is scheduled out inside a migrate_disable() section, migrate_disable_switch() points p->cpus_ptr at the current CPU until migrate_enable(). On PREEMPT_RT every spinlock_t holder is in such a section, so ordinary kernel work opens the window. Elsewhere, seccomp filters run in one. Test p->cpus_mask instead, which keeps the task's affinity and is what sched_getaffinity() reports. This should be safe because an admitted task's cpus_mask covers the root domain, so set_cpus_allowed_dl() moves no bandwidth when migrate_enable() restores p->cpus_ptr. The following program, run as root, had 553679, 453720 and 424623 of its 1000000 requests refused on three boots of a 4-CPU PREEMPT_RT guest, and 236182, 217174 and 237183 without PREEMPT_RT. With this applied the count is 0, also with lockdep, and a task pinned to one CPU is still refused: #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #define REQUESTS 1000000 #define INSNS 300 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 long set(struct attr a) { a.size = sizeof(a); return syscall(SYS_sched_setattr, target, &a, 0); } static void *worker(void *arg) { struct sock_filter f[INSNS + 1]; struct sock_fprog prog = { .len = INSNS + 1, .filter = f }; int i; /* Reading the arguments keeps seccomp from caching the result */ for (i = 0; i < INSNS; i++) f[i] = (struct sock_filter) BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, args)); f[INSNS] = (struct sock_filter) BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW); syscall(SYS_seccomp, SECCOMP_SET_MODE_FILTER, 0, &prog); target = syscall(SYS_gettid); while (!stop) syscall(SYS_getppid); return arg; } static void *hog(void *arg) { while (!stop) ; return arg; } int main(void) { pthread_t t[9]; long refused = 0; int i; pthread_create(&t[0], NULL, worker, NULL); while (!target) usleep(100); for (i = 1; i < 9; i++) pthread_create(&t[i], NULL, hog, NULL); for (i = 0; i < REQUESTS; i++) { /* SCHED_DEADLINE, 2us every 1s */ if (set((struct attr){ .policy = 6, .runtime = 2000, .deadline = 1000000000, .period = 1000000000 }) && errno == EPERM) refused++; set((struct attr){ .policy = 0 }); /* SCHED_OTHER */ } stop = 1; for (i = 0; i < 9; i++) pthread_join(t[i], NULL); printf("%ld of %d requests refused\n", refused, REQUESTS); return 0; } An LLM helped find this bug and write this patch and its test. Fixes: af449901b84c ("sched: Add migrate_disable()") Signed-off-by: Andrea Parri Signed-off-by: Peter Zijlstra (Intel) Signed-off-by: Ingo Molnar Reviewed-by: Sebastian Andrzej Siewior Acked-by: Juri Lelli Cc: stable@vger.kernel.org Link: https://patch.msgid.link/20261102135420.244745-1-parri.andrea@gmail.com --- kernel/sched/syscalls.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/sched/syscalls.c b/kernel/sched/syscalls.c index 45a127deeadd..fb8f332074b8 100644 --- a/kernel/sched/syscalls.c +++ b/kernel/sched/syscalls.c @@ -628,7 +628,7 @@ change: * the entire root_domain to become SCHED_DEADLINE. We * will also fail if there's no bandwidth available. */ - if (!cpumask_subset(span, p->cpus_ptr) || + if (!cpumask_subset(span, &p->cpus_mask) || rq->rd->dl_bw.bw == 0) { retval = -EPERM; goto unlock; From 8127ec8458df29bd702db602473042ccb9e619b9 Mon Sep 17 00:00:00 2001 From: Andrea Parri Date: Mon, 2 Nov 2026 14:48:30 +0100 Subject: [PATCH 2/5] sched/uclamp: Reject clamp values above SCHED_CAPACITY_SCALE sched_setattr() accepts sched_util_min and sched_util_max values that do not fit in uclamp_se::value. A single thread can do: sched_setattr(max = 800) returns 0 sched_setattr(min = 0xfffffffe) returns 0 sched_getattr() min=2046 max=800 which leaves the minimum above both SCHED_CAPACITY_SCALE and the task's own maximum. The range check in uclamp_validate() relies on the + 1 wrapping -1, the reset value, to 0: if (util_min + 1 > SCHED_CAPACITY_SCALE + 1) That only works for an unsigned type. util_min and util_max are int and SCHED_CAPACITY_SCALE is a long, so every negative value passes the check, and so does INT_MAX, whose + 1 wraps. uclamp_se_set() then truncates what got through to the 11 bits of uclamp_se::value, but computes the bucket from the full value. A minimum of 0xfffff800 is stored as 0 in the top bucket, where uclamp_rq_max_value() takes it for the highest active clamp once a higher value leaves the rq. The rq clamp falls below what the remaining tasks requested, and emptying their bucket trips the WARN_ON_ONCE() in uclamp_rq_dec_id(), without any privilege: WARNING: kernel/sched/core.c:1862 at dequeue_task+0x2a3/0x2b0, CPU#0: repro/126 Call Trace: __schedule+0x933/0x1320 schedule+0x36/0x100 do_nanosleep+0x61/0x190 hrtimer_nanosleep+0xb5/0x1d0 Make util_min and util_max unsigned int, so that -1 is the only value that wraps. The pair check below is unaffected, since it skips -1 explicitly and anything else is at most SCHED_CAPACITY_SCALE by then. Requests that were only accepted because of this now fail with -EINVAL. The following program triggers the warning when run by any user outside a child cpu cgroup. With this applied, its 0xfffff800 request fails with -EINVAL and the warning no longer appears: #define _GNU_SOURCE #include #include #include #include #include #include struct attr { uint32_t size, policy; uint64_t flags; int32_t nice; uint32_t prio; uint64_t runtime, deadline, period; uint32_t util_min, util_max; }; static volatile int stop; static void set_min(uint32_t v) { /* KEEP_POLICY | KEEP_PARAMS | UTIL_CLAMP_MIN */ struct attr a = { .size = sizeof(a), .flags = 0x38, .util_min = v }; syscall(SYS_sched_setattr, 0, &a, 0); } static void burn(long us) { struct timespec s, n; clock_gettime(CLOCK_MONOTONIC, &s); do clock_gettime(CLOCK_MONOTONIC, &n); while ((n.tv_sec - s.tv_sec) * 1000000 + (n.tv_nsec - s.tv_nsec) / 1000 < us); } static void *bogus(void *arg) { set_min(0xfffff800); while (!stop) burn(1000); return arg; } static void *worker(void *arg) { long v = (long)arg; set_min(v); while (!stop) { burn(v == 800 ? 300 : 1500); usleep(100); } return arg; } int main(void) { pthread_t t[3]; cpu_set_t c; int i; CPU_ZERO(&c); CPU_SET(0, &c); sched_setaffinity(0, sizeof(c), &c); pthread_create(&t[0], NULL, bogus, NULL); usleep(50000); pthread_create(&t[1], NULL, worker, (void *)500L); pthread_create(&t[2], NULL, worker, (void *)800L); sleep(15); stop = 1; for (i = 0; i < 3; i++) pthread_join(t[i], NULL); return 0; } An LLM helped find the warning and write this patch and its tests. Fixes: 480a6ca2dc6e ("sched/uclamp: Allow to reset a task uclamp constraint value") Reported-by: Sashiko Signed-off-by: Andrea Parri Signed-off-by: Peter Zijlstra (Intel) Signed-off-by: Ingo Molnar Reviewed-by: Vincent Guittot Cc: stable@vger.kernel.org Link: https://sashiko.dev/#/patchset/20261016062629.2102883-1-sh_def%40163.com Link: https://patch.msgid.link/20261102134831.243255-1-parri.andrea@gmail.com --- kernel/sched/syscalls.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/sched/syscalls.c b/kernel/sched/syscalls.c index fb8f332074b8..98be11a9d5d7 100644 --- a/kernel/sched/syscalls.c +++ b/kernel/sched/syscalls.c @@ -318,8 +318,8 @@ static inline void __setscheduler_dl_pi(int newprio, int policy, static int uclamp_validate(struct task_struct *p, const struct sched_attr *attr) { - int util_min = p->uclamp_req[UCLAMP_MIN].value; - int util_max = p->uclamp_req[UCLAMP_MAX].value; + unsigned int util_min = p->uclamp_req[UCLAMP_MIN].value; + unsigned int util_max = p->uclamp_req[UCLAMP_MAX].value; if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MIN) { util_min = attr->sched_util_min; From f7141257dd67d4936f906a6cfd1d0007721f6898 Mon Sep 17 00:00:00 2001 From: Andrea Parri Date: Mon, 2 Nov 2026 14:46:05 +0100 Subject: [PATCH 3/5] 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: [] __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 --- kernel/sched/syscalls.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/kernel/sched/syscalls.c b/kernel/sched/syscalls.c index 98be11a9d5d7..0ec401af80d1 100644 --- 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; } From 78b47fb995b5f3e7e54f0bde365063b530299726 Mon Sep 17 00:00:00 2001 From: Zhan Xusheng Date: Mon, 2 Nov 2026 10:15:36 +0800 Subject: [PATCH 4/5] sched/cache: Don't use fits_capacity() to compare thread and CPU counts invalid_llc_nr() uses fits_capacity() to compare a thread count with a CPU count, only for its 20% margin. Qais's series changes fits_capacity() to take a CPU number as its second argument, and this call would then pass the CPU count as a CPU number without a warning. Give the check its own helper. x * 5 < y * 4 is the same comparison as x * 1280 < y * 1024, so there is no functional change. Signed-off-by: Zhan Xusheng Signed-off-by: Peter Zijlstra (Intel) Signed-off-by: Ingo Molnar Reviewed-by: Qais Yousef Reviewed-by: Chen Yu Reviewed-by: Tim Chen Link: https://lore.kernel.org/all/20260504020003.71306-5-qyousef@layalina.io/ Link: https://patch.msgid.link/20261102021536.1026143-1-zhanxusheng1024@gmail.com --- kernel/sched/fair.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 79f6b3a4fd7f..0c8af1cac2d5 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -1469,6 +1469,12 @@ static bool exceed_llc_capacity(struct sched_cache_group *grp, int cpu) return false; } +/* Are the running threads (in SMT siblings) below ~80% of the LLC's CPUs? */ +static inline bool fits_llc_nr(u64 nr_threads, unsigned int nr_cpus) +{ + return nr_threads * 5 < (u64)nr_cpus * 4; +} + static bool invalid_llc_nr(struct sched_cache_group *grp, struct task_struct *p, int cpu) { @@ -1485,8 +1491,8 @@ static bool invalid_llc_nr(struct sched_cache_group *grp, struct task_struct *p, if (scale == INT_MAX) return false; - return !fits_capacity((READ_ONCE(grp->nr_running_avg) * cpu_smt_num_threads), - (scale * per_cpu(sd_llc_size, cpu))); + return !fits_llc_nr(READ_ONCE(grp->nr_running_avg) * cpu_smt_num_threads, + scale * per_cpu(sd_llc_size, cpu)); } /* From ec6f6731538e0d45872eb279fdeb477bed152d6f Mon Sep 17 00:00:00 2001 From: Seiji Nishikawa Date: Wed, 4 Nov 2026 11:20:04 +0900 Subject: [PATCH 5/5] sched/core: Do not touch SMT siblings that have not joined the core yet When a CPU comes online, it is added to cpu_smt_mask() of its siblings before sched_core_cpu_starting() sets its rq->core to the core leader. For example, on x86, ap_starting() calls set_cpu_sibling_map() first, and calls notify_cpu_starting() after that. This order is intended. Before sched_core_cpu_starting() runs, rq->core of the new CPU still points to its own rq. But rq->core_enabled of the new CPU is already set. __sched_core_flip() sets it for all possible CPUs, also for offline CPUs, and CPU offline does not clear it. So __rq_lockp() of the new CPU returns its own rq->__lock. It does not return the core wide lock. If a sibling CPU runs pick_next_task() for SCHED_CORE at this time, it checks all CPUs in cpu_smt_mask(). It calls update_rq_clock() and pick_task() for the rq of the new CPU. But it holds only its own core lock, not the lock of the new CPU. Then lockdep shows this warning in update_rq_clock(). debug_locks && !(lock_is_held(&(__rq_lockp(rq))->dep_map) != 0) WARNING: kernel/sched/sched.h:1666 at update_rq_clock+0x40a/0xd20 kernel/sched/core.c:875, CPU#0: syz-executor251/5712 Call Trace: pick_next_task kernel/sched/core.c:6375 [inline] __schedule+0x1c69/0x6920 kernel/sched/core.c:7199 preempt_schedule_irq+0x4e/0x90 kernel/sched/core.c:7606 The next loops in pick_next_task() also change rq_i->core_pick of this rq, and can call resched_curr() for it, without its lock. resched_curr() has the same lockdep_assert_rq_held() check. __sched_core_account_forceidle() also checks all CPUs in the same mask. CPU offline does not have this problem. remove_siblinginfo() and sched_core_cpu_dying() both run in take_cpu_down() under stop_machine. So no sibling CPU can be in pick_next_task() at that time. Fix this. In these loops, skip a sibling CPU if its rq->core is different from our rq->core. At run time, rq->core of a CPU is changed by sched_core_cpu_starting(), sched_core_cpu_deactivate() and sched_core_cpu_dying(). The first two hold sched_core_lock(), and sched_core_lock() takes the rq locks of all CPUs in the SMT mask. The last one runs under stop_machine, as written above. So while we hold our core lock, the result of this check does not change. A CPU that is not in the core yet will pick its own tasks. The comment in the reschedule loop of pick_next_task() already expects this case. I tested this with the syzbot C reproducer in QEMU. The two vCPUs were SMT siblings (-smp 2,sockets=1,cores=1,threads=2). Without this patch, the warning came after about 100 seconds. Just before the warning, a debug print showed that the loop on CPU 0 saw CPU 1 with rq->core pointing to CPU 1 itself, and cpu_online() was false for CPU 1. With this patch, the same state still happened, but there was no warning in more than 2 hours. Fixes: 3c474b3239f1 ("sched: Fix Core-wide rq->lock for uninitialized CPUs") Closes: https://syzkaller.appspot.com/bug?extid=681e729b2cf0d4860042 Reported-by: syzbot+681e729b2cf0d4860042@syzkaller.appspotmail.com Signed-off-by: Seiji Nishikawa Signed-off-by: Peter Zijlstra (Intel) Signed-off-by: Ingo Molnar Cc: stable@vger.kernel.org Link: https://patch.msgid.link/20261104022004.3810619-1-snishika@redhat.com --- kernel/sched/core.c | 9 +++------ kernel/sched/core_sched.c | 3 +-- kernel/sched/sched.h | 19 +++++++++++++++++++ 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 1a088d4576f5..5ed660f099a5 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -6297,7 +6297,6 @@ pick_next_task(struct rq *rq, struct rq_flags *rf) __must_hold(__rq_lockp(rq)) { struct task_struct *next, *p, *max; - const struct cpumask *smt_mask; bool fi_before = false; bool core_clock_updated = (rq == rq->core); unsigned long cookie; @@ -6344,8 +6343,6 @@ pick_next_task(struct rq *rq, struct rq_flags *rf) prev_balance(rq, rf); - smt_mask = cpu_smt_mask(cpu); - restart: need_sync |= !!rq->core->core_cookie; @@ -6413,7 +6410,7 @@ restart: * Tie-break prio towards the current CPU */ max = NULL; - for_each_cpu_wrap(i, smt_mask, cpu) { + for_each_core_cpu_wrap(i, rq) { rq_i = cpu_rq(i); /* @@ -6446,7 +6443,7 @@ restart: * For each thread: try and find a runnable task that matches @max or * force idle. */ - for_each_cpu(i, smt_mask) { + for_each_core_cpu(i, rq) { rq_i = cpu_rq(i); p = rq_i->core_pick; @@ -6491,7 +6488,7 @@ restart: * their task. This ensures there is no inter-sibling overlap between * non-matching user state. */ - for_each_cpu(i, smt_mask) { + for_each_core_cpu(i, rq) { rq_i = cpu_rq(i); /* diff --git a/kernel/sched/core_sched.c b/kernel/sched/core_sched.c index 43e0bde3038e..d56be1c31435 100644 --- a/kernel/sched/core_sched.c +++ b/kernel/sched/core_sched.c @@ -241,7 +241,6 @@ out: /* REQUIRES: rq->core's clock recently updated. */ void __sched_core_account_forceidle(struct rq *rq) { - const struct cpumask *smt_mask = cpu_smt_mask(cpu_of(rq)); u64 delta, now = rq_clock(rq->core); struct rq *rq_i; struct task_struct *p; @@ -273,7 +272,7 @@ void __sched_core_account_forceidle(struct rq *rq) delta = div_u64(delta, rq->core->core_forceidle_occupation); } - for_each_cpu(i, smt_mask) { + for_each_core_cpu(i, rq) { rq_i = cpu_rq(i); p = rq_i->core_pick ?: rq_i->curr; diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 1d76950dbb0a..92134ce326a4 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -1534,6 +1534,25 @@ static inline raw_spinlock_t *__rq_lockp(struct rq *rq) return &rq->__lock; } +/* + * A CPU coming online is set in cpu_smt_mask() before + * sched_core_cpu_starting() links its rq->core to the core leader. + * Until then it does not share the core-wide lock, so it must not be + * touched as part of the core. + */ +static inline bool sched_core_sibling(struct rq *rq, struct rq *rq_i) +{ + return rq_i->core == rq->core; +} + +#define for_each_core_cpu(i, rq) \ + for_each_cpu(i, cpu_smt_mask(rq->cpu)) \ + if (sched_core_sibling(rq, cpu_rq(i))) + +#define for_each_core_cpu_wrap(i, rq) \ + for_each_cpu_wrap(i, cpu_smt_mask(rq->cpu), rq->cpu) \ + if (sched_core_sibling(rq, cpu_rq(i))) + extern bool cfs_prio_less(const struct task_struct *a, const struct task_struct *b, bool fi);