* [Devel] [PATCH hci-8.1 v4 0/2] sched: Fix incorrect nproc report inside CT
@ 2026-11-18 19:42 Vladimir Riabchun
2026-11-18 19:43 ` [Devel] [PATCH hci-8.1 v4 1/2] sched: Fix sched_getaffinity syscall Vladimir Riabchun
2026-11-18 19:43 ` [Devel] [PATCH hci-8.1 v4 2/2] selftests: ve: Test sched_getaffinity in VE Vladimir Riabchun
0 siblings, 2 replies; 6+ messages in thread
From: Vladimir Riabchun @ 2026-11-18 19:42 UTC (permalink / raw)
To: Konstantin Khorenko; +Cc: devel
We observed weird nproc behavior - it reported 64 CPUs
no matter what the CT limits were or how many CPUs the host
actually had.
nproc code uses sched_getaffinity syscall under the hood[1].
It appears that this syscall returned complete nonsence when
called from VE - we ignored both num_online_vcpus and
cgroup cpuset limitations.
Now it is fixed and a new selftest is added.
[1]: https://github.com/coreutils/gnulib/blob/master/lib/nproc.c#L144
https://virtuozzo.atlassian.net/browse/VSTOR-141682
Signed-off-by: Vladimir Riabchun <vladimir.riabchun@virtuozzo.com>
---
Changes in v4:
- Patch 1: updated comment about setaffinity and commit mesage.
- Patch 2: added include and used CPU_SETSIZE.
- Link to v3: http://lore.virtuozzo.com/20261029-ct-invalid-nrpoc-v3-0-89d4b0b7d520@virtuozzo.com
Changes in v3:
- Patch 1: num_online_vcpus() -> task_nr_cpus(p)
- Patch 2: updated a comment and moved CPU check in setup fixture to the top.
- Link to v2: http://lore.virtuozzo.com/20261028-ct-invalid-nrpoc-v2-0-968fe985ce27@virtuozzo.com
Changes in v2:
- Updated vCPU logic in patch 1. Now we don't return actual
cpumask, insted first min(vcpu, count(mask)) bits are set.
This is closer to the real CT resources.
- Patch 2: adjusted logic and added one more test.
- Link to v1: http://lore.virtuozzo.com/20261027-ct-invalid-nrpoc-v1-0-48727c3afe16@virtuozzo.com
---
Vladimir Riabchun (2):
sched: Fix sched_getaffinity syscall
selftests: ve: Test sched_getaffinity in VE
kernel/sched/syscalls.c | 18 +-
tools/testing/selftests/ve/.gitignore | 1 +
tools/testing/selftests/ve/Makefile | 1 +
.../selftests/ve/ve_sched_getaffinity_test.c | 212 +++++++++++++++++++++
4 files changed, 227 insertions(+), 5 deletions(-)
---
base-commit: d816dd0e4cdc45208ba0c488d295c5b2f943e079
change-id: 20261027-ct-invalid-nrpoc-f183b3ba691d
Best regards,
--
Vladimir Riabchun <vladimir.riabchun@virtuozzo.com>
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 6+ messages in thread* [Devel] [PATCH hci-8.1 v4 1/2] sched: Fix sched_getaffinity syscall 2026-11-18 19:42 [Devel] [PATCH hci-8.1 v4 0/2] sched: Fix incorrect nproc report inside CT Vladimir Riabchun @ 2026-11-18 19:43 ` Vladimir Riabchun 2026-11-18 19:47 ` no-reply 2026-11-18 19:43 ` [Devel] [PATCH hci-8.1 v4 2/2] selftests: ve: Test sched_getaffinity in VE Vladimir Riabchun 1 sibling, 1 reply; 6+ messages in thread From: Vladimir Riabchun @ 2026-11-18 19:43 UTC (permalink / raw) To: Konstantin Khorenko; +Cc: devel We observed incorrect cpu numbers inside containers: [root@localhost ~]# nproc 64 even when there were much less physical/virtual CPUs. The root cause is incorrect bitmap_fill usage - it sets whole longs only, without handling bit tails, so the actual number of set bits is 64 = bits in ULONG. If we use cpumask only or vcpus only, nproc and other getaffinity users will get incorrect info about CT resources. We can't run on more than count(cpuset) CPUs and we are limited by vCPU number. So the minimum of these two is the real limit. Return first bits set - anyway they can't be used for anything except counting from inside CT. Host behavior is not changed. It is also intentional that we perform VE check against current exec env - CT users will get virtualized view and host users will get real affinity without vCPU info. This fixes only getaffinity syscall, its value may be lower than /proc/cpuinfo or /sys/devices/system/cpu/online. This is expected. https://virtuozzo.atlassian.net/browse/VSTOR-141682 Fixes: 17e30fe0e094 ("sched: Return only virtual cpus in sched_getaffinity()") Feature: sched: virtualize CT cpu affinity management Signed-off-by: Vladimir Riabchun <vladimir.riabchun@virtuozzo.com> --- kernel/sched/syscalls.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/kernel/sched/syscalls.c b/kernel/sched/syscalls.c index 6d921043e9e7..274d897f52cd 100644 --- a/kernel/sched/syscalls.c +++ b/kernel/sched/syscalls.c @@ -1316,15 +1316,23 @@ long sched_getaffinity(pid_t pid, struct cpumask *mask) if (retval) return retval; + scoped_guard (raw_spinlock_irqsave, &p->pi_lock) { + cpumask_and(mask, &p->cpus_mask, cpu_active_mask); + } + if (!ve_is_super(get_exec_env())) { + /* + * setaffinity is a no-op inside CT, so these bits + * can only be used for counting. + * We can't have more than vcpus and we can't run on CPUs + * outside the mask. Reporting smallest will provide true + * information about CT resources. + */ + unsigned int vcpus_avail = min(task_nr_cpus(p), cpumask_weight(mask)); cpumask_clear(mask); - bitmap_fill(cpumask_bits(mask), num_online_vcpus()); - return 0; + bitmap_set(cpumask_bits(mask), 0, vcpus_avail); } - guard(raw_spinlock_irqsave)(&p->pi_lock); - cpumask_and(mask, &p->cpus_mask, cpu_active_mask); - return 0; } -- 2.52.0 _______________________________________________ Devel mailing list Devel@openvz.org https://lists.openvz.org/mailman/listinfo/devel ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Devel] [PATCH hci-8.1 v4 1/2] sched: Fix sched_getaffinity syscall 2026-11-18 19:43 ` [Devel] [PATCH hci-8.1 v4 1/2] sched: Fix sched_getaffinity syscall Vladimir Riabchun @ 2026-11-18 19:47 ` no-reply 0 siblings, 0 replies; 6+ messages in thread From: no-reply @ 2026-11-18 19:47 UTC (permalink / raw) To: Vladimir Riabchun, Konstantin Khorenko; +Cc: devel > We observed incorrect cpu numbers inside containers: > [root@localhost ~]# nproc > 64 > > even when there were much less physical/virtual CPUs. > > The root cause is incorrect bitmap_fill usage - it sets whole > longs only, without handling bit tails, so the actual number of > set bits is 64 = bits in ULONG. > If we use cpumask only or vcpus only, nproc and other getaffinity > users will get incorrect info about CT resources. > > We can't run on more than count(cpuset) CPUs and we are > limited by vCPU number. So the minimum of these two is the > real limit. > [ ... ] > > https://virtuozzo.atlassian.net/browse/VSTOR-141682 > Fixes: 17e30fe0e094 ("sched: Return only virtual cpus in sched_getaffinity()") > Feature: sched: virtualize CT cpu affinity management > Signed-off-by: Vladimir Riabchun <vladimir.riabchun@virtuozzo.com> Sashiko has reviewed this patch and found no issues. Carry the trailer below into the next revision and Sashiko will not review this patch again. Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com> -- Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261118-ct-invalid-nrpoc-v4-0-30baaeda3f87@virtuozzo.com?part=1 _______________________________________________ Devel mailing list Devel@openvz.org https://lists.openvz.org/mailman/listinfo/devel ^ permalink raw reply [flat|nested] 6+ messages in thread
* [Devel] [PATCH hci-8.1 v4 2/2] selftests: ve: Test sched_getaffinity in VE 2026-11-18 19:42 [Devel] [PATCH hci-8.1 v4 0/2] sched: Fix incorrect nproc report inside CT Vladimir Riabchun 2026-11-18 19:43 ` [Devel] [PATCH hci-8.1 v4 1/2] sched: Fix sched_getaffinity syscall Vladimir Riabchun @ 2026-11-18 19:43 ` Vladimir Riabchun 2026-11-18 19:50 ` no-reply 1 sibling, 1 reply; 6+ messages in thread From: Vladimir Riabchun @ 2026-11-18 19:43 UTC (permalink / raw) To: Konstantin Khorenko; +Cc: devel Add a new selftest that verifies that sched_getaffinity respects both cgroup cpuset and nr_cpus limits. https://virtuozzo.atlassian.net/browse/VSTOR-141682 Feature: sched: virtualize CT cpu affinity management Signed-off-by: Vladimir Riabchun <vladimir.riabchun@virtuozzo.com> --- tools/testing/selftests/ve/.gitignore | 1 + tools/testing/selftests/ve/Makefile | 1 + .../selftests/ve/ve_sched_getaffinity_test.c | 212 +++++++++++++++++++++ 3 files changed, 214 insertions(+) diff --git a/tools/testing/selftests/ve/.gitignore b/tools/testing/selftests/ve/.gitignore index 3d1179bd2c5f..13dbfc28e3a7 100644 --- a/tools/testing/selftests/ve/.gitignore +++ b/tools/testing/selftests/ve/.gitignore @@ -4,3 +4,4 @@ ve_mount_accounting_test ve_cpuid_override_test ve_vtty_stress ve_numa_migrate_test +ve_sched_getaffinity_test diff --git a/tools/testing/selftests/ve/Makefile b/tools/testing/selftests/ve/Makefile index e96320a18384..34f901c9e4c9 100644 --- a/tools/testing/selftests/ve/Makefile +++ b/tools/testing/selftests/ve/Makefile @@ -7,6 +7,7 @@ TEST_GEN_PROGS += ve_perms_test TEST_GEN_PROGS += ve_mount_accounting_test TEST_GEN_PROGS += ve_vtty_stress TEST_GEN_PROGS += ve_numa_migrate_test +TEST_GEN_PROGS += ve_sched_getaffinity_test # CPUID faulting is x86-64 only. ARCH says "x86" for 32-bit too and lib.mk # picks the clang target by its emptiness, so decide by the compiler. diff --git a/tools/testing/selftests/ve/ve_sched_getaffinity_test.c b/tools/testing/selftests/ve/ve_sched_getaffinity_test.c new file mode 100644 index 000000000000..73cbdaeb95be --- /dev/null +++ b/tools/testing/selftests/ve/ve_sched_getaffinity_test.c @@ -0,0 +1,212 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * ve_sched_getaffinity selftests + * + * Tests to check cpuset virtualization. + * Not suitable for running inside CT. + */ +#define _GNU_SOURCE +#include <asm/unistd.h> +#include <linux/sched.h> +#include <sys/mman.h> +#include <sys/syscall.h> +#include <sys/stat.h> +#include <errno.h> +#include <sched.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> + +#include "../kselftest_harness.h" +#include "ve_selftest.h" + +FIXTURE(ve_getaff_test) +{ + int cgv2_fd; + int ctid; + struct namespace_ctx ns_ctx; + cpu_set_t orig_cpuset; + int orig_cpucount; +}; + +static int __getaffinity_ve(void *cpuset) +{ + cpu_set_t *dst = cpuset; + + return sched_getaffinity(getpid(), CPU_SETSIZE, dst); +} + +static int getaffinity_ve(FIXTURE_DATA(ve_getaff_test) * self, cpu_set_t *dst) +{ + int ret = -1; + cpu_set_t *tmp = mmap(NULL, sizeof(cpu_set_t), + PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); + + if (tmp == MAP_FAILED) { + fprintf(stderr, "Failed to mmap shared region: %s\n", strerror(errno)); + return -1; + } + CPU_ZERO(tmp); + CPU_ZERO(dst); + + ret = run_in_ve(self->cgv2_fd, self->ctid, &self->ns_ctx, + CLONE_NEWVE, __getaffinity_ve, tmp); + + if (!ret) + memcpy(dst, tmp, sizeof(cpu_set_t)); + + munmap(tmp, sizeof(cpu_set_t)); + return ret; +} + +static int check_cpumask_is_prefix(cpu_set_t *mask, int expected) +{ + int i, count = CPU_COUNT(mask); + + if (expected >= 0 && expected != count) { + fprintf(stderr, "cpumask count mismatch: %d != %d\n", expected, count); + return 0; + } + for (i = 0; i < count; i++) { + if (!CPU_ISSET(i, mask)) { + fprintf(stderr, "cpumask is not a prefix: %d is OFF\n", i); + return 0; + } + } + return 1; +} + +static int get_nth_cpu(cpu_set_t *mask, int n) +{ + int i; + + for (i = 0; i < CPU_SETSIZE; i++) { + if (!CPU_ISSET(i, mask) || --n) + continue; + + return i; + } + return -1; +} + +FIXTURE_SETUP(ve_getaff_test) +{ + ASSERT_EQ(sched_getaffinity(getpid(), CPU_SETSIZE, &self->orig_cpuset), 0); + self->orig_cpucount = CPU_COUNT(&self->orig_cpuset); + if (self->orig_cpucount < 2) + SKIP(return, "Not enough CPUs available"); + + self->cgv2_fd = mount_cg2_fd(); + ASSERT_GE(self->cgv2_fd, 0); + + ASSERT_EQ(write_file_at(self->cgv2_fd, "cgroup.subtree_control", + VE_CONTROLLERS), 0); + + self->ctid = make_ve(self->cgv2_fd, CTID_MIN); + ASSERT_GE(self->ctid, 0); + + ASSERT_EQ(prepare_namespaces(&self->ns_ctx, CLONE_NEWVE, + self->cgv2_fd, self->ctid), 0); +} + +FIXTURE_TEARDOWN(ve_getaff_test) +{ + close_namespace_fds(&self->ns_ctx); + destroy_ve(self->cgv2_fd, self->ctid); + close(self->cgv2_fd); +} + +/* Test correct processing of cpuset.cpus updates */ +TEST_F(ve_getaff_test, update_cg_cpuset) +{ + char path[PATH_MAX]; + int cpu_leave; + cpu_set_t in_ve; + + snprintf(path, sizeof(path), "%d/cpuset.cpus", self->ctid); + if (faccessat(self->cgv2_fd, path, F_OK, 0)) + SKIP(return, "CT cgroup doesn't have cpuset.cpus entry"); + + ASSERT_EQ(getaffinity_ve(self, &in_ve), 0); + ASSERT_TRUE(check_cpumask_is_prefix(&in_ve, self->orig_cpucount)); + + /* Find two CPUs and leave the second via cgroup interface */ + cpu_leave = get_nth_cpu(&self->orig_cpuset, 2); + ASSERT_GE(cpu_leave, 0); + ASSERT_EQ(write_u64_at(self->cgv2_fd, path, cpu_leave), 0); + + /* Verify sched_getaffinity sees 1 CPU, that is the first one */ + ASSERT_EQ(getaffinity_ve(self, &in_ve), 0); + ASSERT_TRUE(check_cpumask_is_prefix(&in_ve, 1)); +} + +/* Test correct processing of cpu.nr_cpus updates */ +TEST_F(ve_getaff_test, nr_proc_bound) +{ + char path[PATH_MAX]; + cpu_set_t in_ve; + + snprintf(path, sizeof(path), "%d/cpu.nr_cpus", self->ctid); + if (faccessat(self->cgv2_fd, path, F_OK, 0)) + SKIP(return, "CT cgroup doesn't have cpu.nr_cpus entry"); + + /* We expect here CPU_COUNT >= 2 */ + ASSERT_EQ(getaffinity_ve(self, &in_ve), 0); + ASSERT_TRUE(check_cpumask_is_prefix(&in_ve, self->orig_cpucount)); + + /* Leave one vCPU */ + ASSERT_EQ(write_u64_at(self->cgv2_fd, path, 1), 0); + + /* Verify sched_getaffinity sees only one vCPU */ + ASSERT_EQ(getaffinity_ve(self, &in_ve), 0); + ASSERT_TRUE(check_cpumask_is_prefix(&in_ve, 1)); +} + +/* Test correct interaction of cpu.nr_cpus and cpuset.cpus */ +TEST_F(ve_getaff_test, limits_interaction) +{ + char path_nr[PATH_MAX], path_set[PATH_MAX], tmp[128]; + int c1, c3; + cpu_set_t in_ve; + + snprintf(path_nr, sizeof(path_nr), "%d/cpu.nr_cpus", self->ctid); + if (faccessat(self->cgv2_fd, path_nr, F_OK, 0)) + SKIP(return, "CT cgroup doesn't have cpu.nr_cpus entry"); + + snprintf(path_set, sizeof(path_set), "%d/cpuset.cpus", self->ctid); + if (faccessat(self->cgv2_fd, path_set, F_OK, 0)) + SKIP(return, "CT cgroup doesn't have cpuset.cpus entry"); + + if (self->orig_cpucount < 3) + SKIP(return, "This test requires at least 3 CPUs"); + + /* We expect here CPU_COUNT >= 3 */ + ASSERT_EQ(getaffinity_ve(self, &in_ve), 0); + ASSERT_TRUE(check_cpumask_is_prefix(&in_ve, self->orig_cpucount)); + + /* Leave two CPUs in cpuset */ + c1 = get_nth_cpu(&self->orig_cpuset, 1); + c3 = get_nth_cpu(&self->orig_cpuset, 3); + ASSERT_TRUE(c1 >= 0 && c3 >= 0); + + snprintf(tmp, sizeof(tmp), "%d,%d", c1, c3); + ASSERT_EQ(write_file_at(self->cgv2_fd, path_set, tmp), 0); + + /* Verify sched_getaffinity sees two vCPU */ + ASSERT_EQ(getaffinity_ve(self, &in_ve), 0); + ASSERT_TRUE(check_cpumask_is_prefix(&in_ve, 2)); + + /* Set vCPU limit to 2 and check that we still have two vCPUs */ + ASSERT_EQ(write_u64_at(self->cgv2_fd, path_nr, 2), 0); + ASSERT_EQ(getaffinity_ve(self, &in_ve), 0); + ASSERT_TRUE(check_cpumask_is_prefix(&in_ve, 2)); + + /* Set vCPU limit to 1 and check that we have one vCPU only */ + ASSERT_EQ(write_u64_at(self->cgv2_fd, path_nr, 1), 0); + ASSERT_EQ(getaffinity_ve(self, &in_ve), 0); + ASSERT_TRUE(check_cpumask_is_prefix(&in_ve, 1)); + +} + +TEST_HARNESS_MAIN -- 2.52.0 _______________________________________________ Devel mailing list Devel@openvz.org https://lists.openvz.org/mailman/listinfo/devel ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Devel] [PATCH hci-8.1 v4 2/2] selftests: ve: Test sched_getaffinity in VE 2026-11-18 19:43 ` [Devel] [PATCH hci-8.1 v4 2/2] selftests: ve: Test sched_getaffinity in VE Vladimir Riabchun @ 2026-11-18 19:50 ` no-reply 2026-11-19 9:51 ` Vladimir Riabchun 0 siblings, 1 reply; 6+ messages in thread From: no-reply @ 2026-11-18 19:50 UTC (permalink / raw) To: Vladimir Riabchun, Konstantin Khorenko; +Cc: devel Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [Medium] selftests/ve: sched_getaffinity() gets CPU_SETSIZE bits as byte length - [Low] selftests/ve: getaffinity test fails instead of skipping without root/cgroup2 - [Low] selftests/ve: expected VE CPU count taken from runner affinity, not CT cpuset - [Low] selftests/ve: CT cgroup leaked when FIXTURE_SETUP assert fails - [Low] selftests/ve: ambiguous 'leave' comment and stray blank line in getaffinity test -- commit e9584f474b4d974bdc60fd7bc11784a4df6ee364 Author: Vladimir Riabchun <vladimir.riabchun@virtuozzo.com> selftests: ve: Test sched_getaffinity in VE Add a new selftest that verifies that sched_getaffinity respects both cgroup cpuset and nr_cpus limits. > diff --git a/tools/testing/selftests/ve/ve_sched_getaffinity_test.c b/tools/testing/selftests/ve/ve_sched_getaffinity_test.c > new file mode 100644 > index 0000000000000..73cbdaeb95be0 > --- /dev/null > +++ b/tools/testing/selftests/ve/ve_sched_getaffinity_test.c [ ... ] > +static int __getaffinity_ve(void *cpuset) > +{ > + cpu_set_t *dst = cpuset; > + > + return sched_getaffinity(getpid(), CPU_SETSIZE, dst); > +} [Severity: Medium] Is CPU_SETSIZE the right length here? sched_getaffinity() takes the mask size in bytes, but CPU_SETSIZE is a bit count (1024), while cpu_set_t is only 128 bytes. After the syscall returns, the glibc wrapper clears the remaining cpusetsize - res bytes, so it writes roughly 900 bytes past the end of dst. Here the destination is a one-page mmap, so the overrun happens to stay in the page. The same call in FIXTURE_SETUP below targets self->orig_cpuset, a field of the fixture data, so it overruns into neighbouring memory. Would sizeof(cpu_set_t) be correct, keeping CPU_SETSIZE only as the loop bound in get_nth_cpu()? [ ... ] > +FIXTURE_SETUP(ve_getaff_test) > +{ > + ASSERT_EQ(sched_getaffinity(getpid(), CPU_SETSIZE, &self->orig_cpuset), 0); > + self->orig_cpucount = CPU_COUNT(&self->orig_cpuset); [Severity: Low] Is orig_cpucount the right expected value for the CT? Attaching the child to the CT cgroup goes through cpuset_attach() -> cpuset_attach_task(), which overwrites its cpus_mask with the CT cgroup's effective cpus. prepare_namespaces() likewise moves the runner into the root of the private cgroup2 mount, which resets its own affinity. The VE's sched_getaffinity() therefore reports the CT cpuset's CPU count, not the count captured here. If the test is started with a narrowed affinity, for example taskset -c 0,1 on a larger host, orig_cpucount is 2 while the VE reports every online CPU. The check_cpumask_is_prefix(..., self->orig_cpucount) assertions in all three tests then fail spuriously. Should the expected count come from the CT cgroup's effective cpuset, or should the test skip when the two differ? > + if (self->orig_cpucount < 2) > + SKIP(return, "Not enough CPUs available"); > + > + self->cgv2_fd = mount_cg2_fd(); > + ASSERT_GE(self->cgv2_fd, 0); [Severity: Low] Should a missing prerequisite be a skip here rather than a failure? An unprivileged run, or one without cgroup2 support, fails on this ASSERT_GE. The sibling ve_cpuid_override_test and ve_numa_migrate_test skip with a "needs root" reason via geteuid(). The same applies to the subtree_control write, make_ve() and prepare_namespaces() below. > + > + ASSERT_EQ(write_file_at(self->cgv2_fd, "cgroup.subtree_control", > + VE_CONTROLLERS), 0); > + > + self->ctid = make_ve(self->cgv2_fd, CTID_MIN); > + ASSERT_GE(self->ctid, 0); > + > + ASSERT_EQ(prepare_namespaces(&self->ns_ctx, CLONE_NEWVE, > + self->cgv2_fd, self->ctid), 0); > +} [Severity: Low] Can this leak the CT cgroup when prepare_namespaces() fails? The harness exits from a failed setup before setting setup_completed, so FIXTURE_TEARDOWN is not run. destroy_ve() and close() are then skipped, and the cgroup created by make_ve() stays behind and consumes a CTID. Should the failure path in FIXTURE_SETUP clean up after itself, or a comment say that the leak is intentional? [ ... ] > + /* Find two CPUs and leave the second via cgroup interface */ > + cpu_leave = get_nth_cpu(&self->orig_cpuset, 2); > + ASSERT_GE(cpu_leave, 0); > + ASSERT_EQ(write_u64_at(self->cgv2_fd, path, cpu_leave), 0); [Severity: Low] This isn't a bug, but "leave the second" can be read as removing the second CPU, while the code keeps only that CPU in cpuset.cpus. The name cpu_leave adds to the confusion. Also, the next comment says "that is the first one", which means the VE's CPU 0, not the host's first CPU. Rewording would make both clearer. [ ... ] > + /* Set vCPU limit to 1 and check that we have one vCPU only */ > + ASSERT_EQ(write_u64_at(self->cgv2_fd, path_nr, 1), 0); > + ASSERT_EQ(getaffinity_ve(self, &in_ve), 0); > + ASSERT_TRUE(check_cpumask_is_prefix(&in_ve, 1)); > + > +} [Severity: Low] This isn't a bug, but there is a stray blank line before the closing brace of limits_interaction(). -- Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261118-ct-invalid-nrpoc-v4-0-30baaeda3f87@virtuozzo.com?part=2 _______________________________________________ Devel mailing list Devel@openvz.org https://lists.openvz.org/mailman/listinfo/devel ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Devel] [PATCH hci-8.1 v4 2/2] selftests: ve: Test sched_getaffinity in VE 2026-11-18 19:50 ` no-reply @ 2026-11-19 9:51 ` Vladimir Riabchun 0 siblings, 0 replies; 6+ messages in thread From: Vladimir Riabchun @ 2026-11-19 9:51 UTC (permalink / raw) To: Konstantin Khorenko; +Cc: devel On 11/18/26 20:50, no-reply@sashiko.virtuozzo.com wrote: > Thank you for your contribution! Sashiko AI review found 5 potential > issues to consider: > - [Medium] selftests/ve: sched_getaffinity() gets CPU_SETSIZE bits as > byte length > - [Low] selftests/ve: getaffinity test fails instead of skipping without > root/cgroup2 > - [Low] selftests/ve: expected VE CPU count taken from runner affinity, > not CT cpuset > - [Low] selftests/ve: CT cgroup leaked when FIXTURE_SETUP assert fails > - [Low] selftests/ve: ambiguous 'leave' comment and stray blank line in > getaffinity test > -- > > commit e9584f474b4d974bdc60fd7bc11784a4df6ee364 > Author: Vladimir Riabchun <vladimir.riabchun@virtuozzo.com> > > selftests: ve: Test sched_getaffinity in VE > > Add a new selftest that verifies that sched_getaffinity respects both > cgroup cpuset and nr_cpus limits. > >> diff --git a/tools/testing/selftests/ve/ve_sched_getaffinity_test.c b/tools/testing/selftests/ve/ve_sched_getaffinity_test.c >> new file mode 100644 >> index 0000000000000..73cbdaeb95be0 >> --- /dev/null >> +++ b/tools/testing/selftests/ve/ve_sched_getaffinity_test.c > [ ... ] >> +static int __getaffinity_ve(void *cpuset) >> +{ >> + cpu_set_t *dst = cpuset; >> + >> + return sched_getaffinity(getpid(), CPU_SETSIZE, dst); >> +} > > [Severity: Medium] > Is CPU_SETSIZE the right length here? sched_getaffinity() takes the mask > size in bytes, but CPU_SETSIZE is a bit count (1024), while cpu_set_t is > only 128 bytes. After the syscall returns, the glibc wrapper clears the > remaining cpusetsize - res bytes, so it writes roughly 900 bytes past the > end of dst. > > Here the destination is a one-page mmap, so the overrun happens to stay in > the page. The same call in FIXTURE_SETUP below targets self->orig_cpuset, > a field of the fixture data, so it overruns into neighbouring memory. > Would sizeof(cpu_set_t) be correct, keeping CPU_SETSIZE only as the loop > bound in get_nth_cpu()? True, will fix in v5. > > [ ... ] >> +FIXTURE_SETUP(ve_getaff_test) >> +{ >> + ASSERT_EQ(sched_getaffinity(getpid(), CPU_SETSIZE, &self->orig_cpuset), 0); >> + self->orig_cpucount = CPU_COUNT(&self->orig_cpuset); > > [Severity: Low] > Is orig_cpucount the right expected value for the CT? Attaching the child > to the CT cgroup goes through cpuset_attach() -> cpuset_attach_task(), > which overwrites its cpus_mask with the CT cgroup's effective cpus. > prepare_namespaces() likewise moves the runner into the root of the private > cgroup2 mount, which resets its own affinity. The VE's sched_getaffinity() > therefore reports the CT cpuset's CPU count, not the count captured here. > > If the test is started with a narrowed affinity, for example taskset -c 0,1 > on a larger host, orig_cpucount is 2 while the VE reports every online CPU. > The check_cpumask_is_prefix(..., self->orig_cpucount) assertions in all > three tests then fail spuriously. Should the expected count come from the > CT cgroup's effective cpuset, or should the test skip when the two differ? Replied in v3. > >> + if (self->orig_cpucount < 2) >> + SKIP(return, "Not enough CPUs available"); >> + >> + self->cgv2_fd = mount_cg2_fd(); >> + ASSERT_GE(self->cgv2_fd, 0); > > [Severity: Low] > Should a missing prerequisite be a skip here rather than a failure? An > unprivileged run, or one without cgroup2 support, fails on this ASSERT_GE. > The sibling ve_cpuid_override_test and ve_numa_migrate_test skip with a > "needs root" reason via geteuid(). The same applies to the > subtree_control write, make_ve() and prepare_namespaces() below. Okay, this may worth it for selftests in CT. > >> + >> + ASSERT_EQ(write_file_at(self->cgv2_fd, "cgroup.subtree_control", >> + VE_CONTROLLERS), 0); >> + >> + self->ctid = make_ve(self->cgv2_fd, CTID_MIN); >> + ASSERT_GE(self->ctid, 0); >> + >> + ASSERT_EQ(prepare_namespaces(&self->ns_ctx, CLONE_NEWVE, >> + self->cgv2_fd, self->ctid), 0); >> +} > > [Severity: Low] > Can this leak the CT cgroup when prepare_namespaces() fails? The harness > exits from a failed setup before setting setup_completed, so > FIXTURE_TEARDOWN is not run. destroy_ve() and close() are then skipped, > and the cgroup created by make_ve() stays behind and consumes a CTID. > Should the failure path in FIXTURE_SETUP clean up after itself, or a > comment say that the leak is intentional? Replied in v3. > > [ ... ] >> + /* Find two CPUs and leave the second via cgroup interface */ >> + cpu_leave = get_nth_cpu(&self->orig_cpuset, 2); >> + ASSERT_GE(cpu_leave, 0); >> + ASSERT_EQ(write_u64_at(self->cgv2_fd, path, cpu_leave), 0); > > [Severity: Low] > This isn't a bug, but "leave the second" can be read as removing the second No, it can't. > CPU, while the code keeps only that CPU in cpuset.cpus. The name cpu_leave > adds to the confusion. Also, the next comment says "that is the first > one", which means the VE's CPU 0, not the host's first CPU. Rewording > would make both clearer. > > [ ... ] >> + /* Set vCPU limit to 1 and check that we have one vCPU only */ >> + ASSERT_EQ(write_u64_at(self->cgv2_fd, path_nr, 1), 0); >> + ASSERT_EQ(getaffinity_ve(self, &in_ve), 0); >> + ASSERT_TRUE(check_cpumask_is_prefix(&in_ve, 1)); >> + >> +} > > [Severity: Low] > This isn't a bug, but there is a stray blank line before the closing brace > of limits_interaction(). Will fix. > -- Best regards, Riabchun Vladimir Linux Kernel Developer, Virtuozzo _______________________________________________ Devel mailing list Devel@openvz.org https://lists.openvz.org/mailman/listinfo/devel ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-11-19 9:52 UTC | newest] Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2026-11-18 19:42 [Devel] [PATCH hci-8.1 v4 0/2] sched: Fix incorrect nproc report inside CT Vladimir Riabchun 2026-11-18 19:43 ` [Devel] [PATCH hci-8.1 v4 1/2] sched: Fix sched_getaffinity syscall Vladimir Riabchun 2026-11-18 19:47 ` no-reply 2026-11-18 19:43 ` [Devel] [PATCH hci-8.1 v4 2/2] selftests: ve: Test sched_getaffinity in VE Vladimir Riabchun 2026-11-18 19:50 ` no-reply 2026-11-19 9:51 ` Vladimir Riabchun
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox