From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail.openvz.org (mail.openvz.org [69.168.225.77]) by lore.virtuozzo.com (Postfix) with ESMTPS id 8C6228017D for ; Wed, 18 Nov 2026 19:44:30 +0000 (UTC) Received: from mail.openvz.org (localhost [127.0.0.1]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 6AIJhGIg028691; Wed, 18 Nov 2026 22:43:17 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6AIJhGIg028691 Authentication-Results: mail.openvz.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=virtuozzo.com header.i=@virtuozzo.com header.b="D41uRAZ2" Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 6AIJh8nY028664 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=FAIL) for ; Wed, 18 Nov 2026 22:43:08 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6AIJh8nY028664 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-48e0ef019a4so1112450f8f.3 for ; Wed, 18 Nov 2026 11:43:08 -0800 (PST) X-Forwarded-Encrypted: i=2; AL+hErEZitsJBvWphaltrMvz2qo3mQ+13CihZw+D9stYDjUUT3QyRVWrlNxWewU9GR3sI72LjOP3mA==@openvz.org X-Gm-Message-State: APuU4Z9/WNvazVFjL5iGQDJ3JGpIhom51M7ZSM/3AkOa2rdNzrSH9tQm Gdp7pXUAzBPwD7xGm5jGEceKFDsFMQjxiSNkqIWRrl8JMUbk7n/DHIyfqsO4+gkisc1L8dWY9fJ 3VrROw1lxSlUSZ3IDTv1rseMBmUfJWo6BGpbLPpSjaIHC006ctfmweg== X-Gm-Gg: AZFdTZe1FvVlL/GaFGKma+LeV1WBZB7dlhyq8bgq19WdkEGH1K9z1X3n130KfEQneQA snjncjpZifVe1k1Tz6khPm32gVcVYLZUVIqoij4ZtM0w54j4UzLFkpWHoRdh/kdYM+sporK7tOy DsQkuonIB+OdmFcD7SdiH30E9XpnRy4tbeyKxK2LJ6HoKhfkO39ZTRvDU+mljMDBvr784OWzkns yFisWB8FJa4K/Wlqa+d084I9pXNZ1LRCF83JCCM/deyWSxqOWcDc/o1bBMgCi0TA+SvLFDHPGDf BH6rY9HHZ11UcuC4AIRIHbNnYuDVUV0cNdc2GCIOtPzOlRU3JkCuW2TUHd1Iw8aSNGKUYhMvDyA 1mph+LoPLqKgZ1H+4eAnjop3Ykq4Y8A== X-Received: by 2002:a5d:5e03:0:b0:48d:ffcb:28e1 with SMTP id ffacd0b85a97d-4a637f63c2amr7506275f8f.11.1795030988340; Wed, 18 Nov 2026 11:43:08 -0800 (PST) X-Received: by 2002:a5d:5e03:0:b0:48d:ffcb:28e1 with SMTP id ffacd0b85a97d-4a637f63c2amr7506236f8f.11.1795030987775; Wed, 18 Nov 2026 11:43:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1795030987; cv=none; d=google.com; s=arc-20260327; b=C69YSg/JhfElAx4nwDzmdco6rWnhFgf10THJDsrPosrfqNRdIDJZVNmvXxUaj4zD8P 5ydRYx1hd4grcOAojE6613H1B+spvo8JsW/pUuJvtlMwHPNHMlkuV/urNWv7rXU2a3Lc 1Q1pYtEXqymVTDVypjxa08Wc+l9W2vjuyQpAhyvXwVjnsL4QtQP0KHRhX8thHCk6I2XK XNXEZfk1m8vVcmfK83hGc8kn3TjzFeQuV2uDgbrZWkZehb97v8+w68ognIAqmyY33ZAV fW5ZigXwQ6yf6oWK4bdnv9DPizOP36dMv29eCOF3Epgk5E0MF20H7kxT7YmLILHQSeNS BY7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:dkim-signature; bh=xFfRoF9YlYhTJD7IihWAmvAySMK41gzA5IppPVULpyY=; fh=4dppuILCfGifWHfxjIIA1BWoKQtGs1EMopxlFiqV6no=; b=YQGA3YAoWT7HiF3p4EPO2zm/C8zF9f9IlQ9nRU7ZvuffVVcF8Lgk3NRwKpF2HBAzZG iQGoh8Xz7y4UDdW0D4+AnguwiD7Uo/Ksn5hJNnXZR8pa++KyRJUNh/naqz3UmYhOkS7L 5g0qA7R0l/6YuA160s06w59v8hXG4e8ZI1565Q7tAv2JiMixjjHUGlGC2rYKqSbxi52u MMQwR+gUGUcNQgpI549egTiqqmzPRRCxglUWHL0WzX75PXxpe9L65CAA9LPQmtgrihQG mSOs9YcOVEMNJu8TAbkhTPGltqZhZPaisqTxOn2wDP+o5EigE6J5wl6+UB/h1pHXB4kP Secw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=relay header.b=D41uRAZ2; spf=pass (google.com: domain of vladimir.riabchun@virtuozzo.com designates 130.117.225.111 as permitted sender) smtp.mailfrom=vladimir.riabchun@virtuozzo.com; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=virtuozzo.com Received: from relay.virtuozzo.com (relay.virtuozzo.com. [130.117.225.111]) by mx.google.com with ESMTPS id ffacd0b85a97d-4a6573bcfc7si6780057f8f.75.2026.11.18.11.43.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Nov 2026 11:43:07 -0800 (PST) Received-SPF: pass (google.com: domain of vladimir.riabchun@virtuozzo.com designates 130.117.225.111 as permitted sender) client-ip=130.117.225.111; Authentication-Results: mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=relay header.b=D41uRAZ2; spf=pass (google.com: domain of vladimir.riabchun@virtuozzo.com designates 130.117.225.111 as permitted sender) smtp.mailfrom=vladimir.riabchun@virtuozzo.com; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=virtuozzo.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=virtuozzo.com; s=relay; h=Message-Id:Content-Type:MIME-Version:Subject:Date :From; bh=xFfRoF9YlYhTJD7IihWAmvAySMK41gzA5IppPVULpyY=; b=D41uRAZ2Sx3qmlGSQN4 dRCW3VDu09VJ5TEH2OIyvY+wx2IGUJlna8t0low/++0BC3X1jQDftjhMXpaVA/GZS8PypApPWwYbW f3jd8W13OBQw/uHKy50YLxSew+xu4PFYpq691Rw/vGPR50hFcKPz20B4t3M3QLoY98Bp5y4A1Evoz zIC9xVCVfmTdwGAQmCvtg9Lqe78Z3QqJ+sJiWJR2GAFFpm867+6sdGNlrMlZWdFc+Ry21k99eb+UY mriwGa3ZxW3fPq9ewDQnOg4F2dilM1agpNgXzNRk+0L0xZoINvycCe3zitP2oxRd3PhrfYZSc1crB 4YolkNQzzrDuosw==; Received: from ch-demo-asa.virtuozzo.com ([130.117.225.8] helo=192.168.1.87) by relay.virtuozzo.com with esmtps (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1xTlUP-00CGqR-07; Wed, 18 Nov 2026 20:43:06 +0100 From: Vladimir Riabchun Date: Wed, 18 Nov 2026 20:43:01 +0100 MIME-Version: 1.0 Message-Id: <20261118-ct-invalid-nrpoc-v4-2-30baaeda3f87@virtuozzo.com> References: <20261118-ct-invalid-nrpoc-v4-0-30baaeda3f87@virtuozzo.com> In-Reply-To: <20261118-ct-invalid-nrpoc-v4-0-30baaeda3f87@virtuozzo.com> To: Konstantin Khorenko X-Mailer: b4 0.16.0 X-OZ-Fwd: true Cc: devel@openvz.org Subject: [Devel] [PATCH hci-8.1 v4 2/2] selftests: ve: Test sched_getaffinity in VE X-BeenThere: devel@openvz.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: OpenVZ development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: devel-bounces@openvz.org Errors-To: devel-bounces@openvz.org 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 --- 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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