sched/deadline: Fix spurious -EPERM for migration-disabled tasks
Subsystems (3)
PATCH 1/1 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 <errno.h>
#include <linux/filter.h>
#include <linux/seccomp.h>
#include <pthread.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/syscall.h>
#include <unistd.h>
#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.
---
This textually conflicts with [1], which rewrites this line and keeps p->cpus_ptr. Whichever goes second needs to keep &p->cpus_mask.
Also tested on PREEMPT_RT with lockdep by holding a 1ms/10ms reservation for 3ms at a time, so that tasks admitted while migration-disabled reach migrate_enable() as SCHED_DEADLINE: no warnings, total_bw unchanged, and none of 6788 requests refused. Without this patch, 826 of 6645 are refused.
Other !PREEMPT_RT migrate_disable() users were not tested.
[1] https://lore.kernel.org/[email protected]/
kernel/sched/syscalls.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/syscalls.c +1 -1
@@ -628,7 +628,7 @@ int __sched_setscheduler(struct task_struct *p,* 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;
Signature · 1 line
2.53.0
Quoted 143 lines · Andrea Parri
sched_setattr()can refuse to make a taskSCHED_DEADLINEwith-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()testsp->cpus_ptr, which is not always the task's affinity. When a task is scheduled out inside amigrate_disable()section,migrate_disable_switch()pointsp->cpus_ptrat the current CPU untilmigrate_enable(). OnPREEMPT_RTeveryspinlock_tholder is in such a section, so ordinary kernel work opens the window. Elsewhere, seccomp filters run in one.Test
p->cpus_maskinstead, which keeps the task's affinity and is whatsched_getaffinity()reports. This should be safe because an admitted task'scpus_maskcovers the root domain, soset_cpus_allowed_dl()moves no bandwidth whenmigrate_enable()restoresp->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_RTguest, and 236182, 217174 and 237183 withoutPREEMPT_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 <errno.h> #include <linux/filter.h> #include <linux/seccomp.h> #include <pthread.h> #include <stddef.h> #include <stdint.h> #include <stdio.h> #include <sys/syscall.h> #include <unistd.h> #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 volatilepid_ttarget;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 <[email protected]>---
This textually conflicts with [1], which rewrites this line and keepsp->cpus_ptr. Whichever goes second needs to keep&p->cpus_mask.Also tested on
PREEMPT_RTwith lockdep by holding a 1ms/10ms reservation for 3ms at a time, so that tasks admitted while migration-disabled reachmigrate_enable()asSCHED_DEADLINE: no warnings,total_bwunchanged, and none of 6788 requests refused. Without this patch, 826 of 6645 are refused.Other !PREEMPT_RT
migrate_disable()users were not tested.[1] https://lore.kernel.org/[email protected]/
kernel/sched/syscalls.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/sched/syscalls.c +1
-1@@ -628,7 +628,7 @@ int __sched_setscheduler(struct task_struct *p,* 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;Signature · 1 line
2.53.0
Hi Andrea,
Do you mind if I include your patch in my series [1]?
Also, please detail the LLM used as per Documentation/process/coding-assistants.rst:
[1]: https://lore.kernel.org/all/[email protected]/
Kind regards,
Signature · 1 line
Aaron Tomlin
On Mon, Nov 02, 2026 at 09:45:30AM -0500, Aaron Tomlin wrote:
[...]
Quoted 1 line
Do you mind if I include your patch in my series [1]?
Thanks for the offer, but I'd rather keep this one standalone: it's a stable fix, and it shouldn't have to wait for the debugfs series. If it goes in first, your 1/6 just needs to keep &p->cpus_mask on that line:
if (!cpumask_subset(rd->span, &p->cpus_mask) ||
rd->dl_bw.bw == 0) {If the maintainers would rather have it in your series, I'm fine with that, as the first patch and with its authorship and its Fixes and stable tags unchanged.
Quoted 3 lines
Also, please detail the LLM used as per
Documentation/process/coding-assistants.rst:Assisted-by LLM [TOOL1] [TOOL2]
That tag is gone as of v7.4-rc1, which this patch is based on: commit 1e035297ffda ("docs: remove Assisted-by references") dropped it from coding-assistants.rst and checkpatch. tip/sched/core is still on an older base, where the docs have it.
Per Documentation/process/generated-content.rst, the tool was Claude Code with Claude Opus 5.5. It helped find the bug and write the patch, its changelog and its test, and I reviewed the result.
Thanks,
Andrea
Quoted 10 lines · Andrea Parri
On Mon, Nov 02, 2026 at 09:45:30AM -0500, Aaron Tomlin wrote:
[...]
Quoted 1 line
Do you mind if I include your patch in my series [1]?
Thanks for the offer, but I'd rather keep this one standalone: it's a stable fix, and it shouldn't have to wait for the debugfs series. If it goes in first, your 1/6 just needs to keep
&p->cpus_maskon that line:
Quoted 6 lines
if (!cpumask_subset(rd->span, &p->cpus_mask) || rd->dl_bw.bw == 0) {If the maintainers would rather have it in your series, I'm fine with that, as the first patch and with its authorship and its Fixes and stable tags unchanged.
Acknowledged.
Kind regards,
Signature · 1 line
Aaron Tomlin
Hello,
Quoted 104 lines · Andrea Parri
sched_setattr()can refuse to make a taskSCHED_DEADLINEwith-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()testsp->cpus_ptr, which is not always the task's affinity. When a task is scheduled out inside amigrate_disable()section,migrate_disable_switch()pointsp->cpus_ptrat the current CPU untilmigrate_enable(). OnPREEMPT_RTeveryspinlock_tholder is in such a section, so ordinary kernel work opens the window. Elsewhere, seccomp filters run in one.Test
p->cpus_maskinstead, which keeps the task's affinity and is whatsched_getaffinity()reports. This should be safe because an admitted task'scpus_maskcovers the root domain, soset_cpus_allowed_dl()moves no bandwidth whenmigrate_enable()restoresp->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_RTguest, and 236182, 217174 and 237183 withoutPREEMPT_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 <errno.h> #include <linux/filter.h> #include <linux/seccomp.h> #include <pthread.h> #include <stddef.h> #include <stdint.h> #include <stdio.h> #include <sys/syscall.h> #include <unistd.h> #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 volatilepid_ttarget;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;
Quoted 7 lines
}
An LLM helped find this bug and write this patch and its test.
Fixes af449901b84c ("sched: Add migrate_disable()")Signed-off-by Andrea Parri <[email protected]>
The fix makes sense to me.
I only wonder if we want the reproducer in the changelog. An alternative might be to move it to https://github.com/jlelli/sched-deadline-tests
where I keep the rest of SCHED_DEADLINE tests. I can probably do that if you agree and prefer.
Thanks!
Juri
On Tue, Nov 03, 2026 at 08:07:50AM +0100, Juri Lelli wrote:
[...]
Quoted 3 lines
The fix makes sense to me.
Acked-by Juri Lelli <[email protected]>
Thanks!
Quoted 4 lines
I only wonder if we want the reproducer in the changelog. An alternative might be to move it to https://github.com/jlelli/sched-deadline-tests
where I keep the rest ofSCHED_DEADLINEtests. I can probably do that if you agree and prefer.
Fine with me, please go ahead, for this one and for the cpuset_mutex one too, if it fits there. If you'd like a v2 of both, with the programs replaced by a link and the tags collected, just let me know.
Thanks,
Andrea
Quoted 3 lines · Andrea Parri
sched_setattr()can refuse to make a taskSCHED_DEADLINEwith-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.
…
kernel/sched/syscalls.c int __sched_setscheduler(struct task_struct *p, · line 631
Quoted 8 lines
kernel/sched/syscalls.c +1
-1@@ -628,7 +628,7 @@ int __sched_setscheduler(struct task_struct *p,* 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) ||
The task's cpus_ptr must share at least one CPU of cpus_mask. But it is not a subset if more than one CPU is involved. So the problem is that while it is in a migrate_disable() section after it has been scheduled out, it will see only one CPU via ->cpus_ptr while span may have two. That makes sense.
Looking at sched_setaffinity(), there is dl_task_check_affinity() which makes the same check. It is skipped for !task_has_dl_policy(). This looks time like there is a small window in sched_setscheduler() where the affinity check passes, the task is not yet DL, and then sched_setaffinity() makes the affinity smaller.
kernel/sched/syscalls.c int __sched_setscheduler(struct task_struct *p, · line 634
Quoted 3 lines
kernel/sched/syscalls.c +0
-0rq->rd->dl_bw.bw == 0) { retval = -EPERM ; goto unlock;
Sebastian
On Tue, Nov 03, 2026 at 09:11:49AM +0100, Sebastian Andrzej Siewior wrote:
[...]
Quoted 1 line
Reviewed-by Sebastian Andrzej Siewior <[email protected]>
Thanks!
Quoted 5 lines
Looking at
sched_setaffinity(), there isdl_task_check_affinity()which makes the same check. It is skipped for !task_has_dl_policy(). This looks time like there is a small window insched_setscheduler()where the affinity check passes, the task is not yet DL, and thensched_setaffinity()makes the affinity smaller.
Agreed. __sched_setaffinity() calls dl_task_check_affinity() before taking task_rq_lock(), which only __set_cpus_allowed_ptr() takes later, whereas __sched_setscheduler() runs the admission check and switches the policy under a single task_rq_lock(). So setaffinity can see a non-DL task, setscheduler then admits it against the old, wide cpus_mask, and setaffinity finally narrows it.
The window may be wider than it looks, too: it includes setaffinity waiting for task_rq_lock() while setscheduler holds it, so the two only need to overlap.
That's independent of this patch: the same happens with p->cpus_ptr.
restrict_cpus_allowed_ptr() already does its DL check under task_rq_lock(), so doing the same in the setaffinity path looks like the way to close it. One thing to keep in mind there: when __sched_setaffinity() races with a cpuset update, it calls __set_cpus_allowed_ptr() again to reset the task to the cpuset's mask. That second call should probably stay unchecked, as it is today, since cpuset_update_tasks_cpumask() narrows DL tasks without a check anyway; refusing it would leave the task allowed on CPUs outside its cpuset.
I'll follow up on this separately.
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: a98679d5e0bb07175cb39bf66c16ac6f861d2965 Gitweb: https://git.kernel.org/tip/a98679d5e0bb07175cb39bf66c16ac6f861d2965 Author: Andrea Parri <[email protected]> AuthorDate: Mon, 02 Nov 2026 14:54:19 +01:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Fri, 06 Nov 2026 11:45:16 +01:00
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 <errno.h>
#include <linux/filter.h>
#include <linux/seccomp.h>
#include <pthread.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/syscall.h>
#include <unistd.h>
#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.
--- kernel/sched/syscalls.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/syscalls.c +1 -1
@@ -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;
The 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: 6616bffa9b52da1338cf8bff9882404e02b9e361 Gitweb: https://git.kernel.org/tip/6616bffa9b52da1338cf8bff9882404e02b9e361 Author: Andrea Parri <[email protected]> AuthorDate: Mon, 02 Nov 2026 14:54:19 +01:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Mon, 09 Nov 2026 10:08:59 +01:00
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 <errno.h>
#include <linux/filter.h>
#include <linux/seccomp.h>
#include <pthread.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/syscall.h>
#include <unistd.h>
#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.
--- kernel/sched/syscalls.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/syscalls.c +1 -1
@@ -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;