[Devel] [PATCH hci-8.1 1/2] sched: Fix sched_getaffinity syscall

Vladimir Riabchun vladimir.riabchun at virtuozzo.com
Tue Oct 27 23:51:10 MSK 2026


We observed incorrect cpu numbers inside containers:
[root at localhost ~]# nproc
64

even when there were much less physical/virtual CPUs.

The root cause is incorrect bitmap_fill usage - it sets whole
integers only, without handling bit tails, so the actual number of
set bits is 64 = bits in ULONG.
Anyway, this approach is incorrect - it doesn't respect
container cpuset.

Fix sched_getaffinity syscall by reusing p->cpus_mask, and bounding it
with num_online_vcpus().

https://virtuozzo.atlassian.net/browse/VSTOR-141682

Signed-off-by: Vladimir Riabchun <vladimir.riabchun at virtuozzo.com>
---
 kernel/sched/syscalls.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/kernel/sched/syscalls.c b/kernel/sched/syscalls.c
index 6d921043e9e7..92051bace212 100644
--- a/kernel/sched/syscalls.c
+++ b/kernel/sched/syscalls.c
@@ -1305,6 +1305,7 @@ SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
 long sched_getaffinity(pid_t pid, struct cpumask *mask)
 {
 	struct task_struct *p;
+	unsigned int online_vcpus = num_online_vcpus();
 	int retval;
 
 	guard(rcu)();
@@ -1316,14 +1317,14 @@ long sched_getaffinity(pid_t pid, struct cpumask *mask)
 	if (retval)
 		return retval;
 
-	if (!ve_is_super(get_exec_env())) {
-		cpumask_clear(mask);
-		bitmap_fill(cpumask_bits(mask), num_online_vcpus());
-		return 0;
+	scoped_guard (raw_spinlock_irqsave, &p->pi_lock) {
+		cpumask_and(mask, &p->cpus_mask, cpu_active_mask);
 	}
 
-	guard(raw_spinlock_irqsave)(&p->pi_lock);
-	cpumask_and(mask, &p->cpus_mask, cpu_active_mask);
+	if (!ve_is_super(get_exec_env()) &&
+	    !WARN_ON_ONCE(online_vcpus > nr_cpumask_bits))
+		bitmap_clear(cpumask_bits(mask), online_vcpus,
+			     nr_cpumask_bits - online_vcpus);
 
 	return 0;
 }

-- 
2.52.0



More information about the Devel mailing list