[Devel] [PATCH hci-8.1 v4 2/2] selftests: ve: Test sched_getaffinity in VE
Vladimir Riabchun
vladimir.riabchun at virtuozzo.com
Thu Nov 19 12:51:24 MSK 2026
On 11/18/26 20:50, no-reply at 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 at 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
More information about the Devel
mailing list