From: Mirian Shilakadze <mirian.shilakadze@virtuozzo.com>
To: khorenko@virtuozzo.com, vladimir.riabchun@virtuozzo.com
Cc: devel@openvz.org
Subject: [Devel] [PATCH hci-8.2 v2 1/3] selftests/ve: open the host cgroup2 mount instead of creating one
Date: Thu, 19 Nov 2026 12:16:26 +0400 [thread overview]
Message-ID: <20261119081654.484107-2-mirian.shilakadze@virtuozzo.com> (raw)
In-Reply-To: <20261119081654.484107-1-mirian.shilakadze@virtuozzo.com>
mount_cg2_fd() gave the tests their cgroup2 fd by creating a fresh mount
with fsopen(), fsconfig() and fsmount(), with no options. That ends in
cgroup_get_tree(), which calls apply_cgroup_root_flags() with the new
mount's options once the tree is built. For a caller in the root cgroup
namespace that function does not only set the flags the new mount asked
for, it also clears on cgrp_dfl_root the nsdelegate,
memory_recursiveprot and other root options the new mount did not carry,
and cgrp_dfl_root is the one hierarchy behind every cgroup2 mount on the
host. So every fixture setup switched nsdelegate and
memory_recursiveprot off on the host /sys/fs/cgroup until something
remounted it. A node the ve tests had run on shows its cgroup2 mount
without either option, three untouched nodes of the same image still
show both.
Open the existing mount instead. open_cg2_fd() opens /sys/fs/cgroup as a
directory fd and checks with fstatfs() that it is cgroup2. The *at()
calls the tests make work on that fd as they did on the fsmount fd, and
nothing is mounted, so the host flags stay as systemd set them. The
tests now need the host cgroup2 mount at /sys/fs/cgroup, which every
vz10 host has. Every caller switches to it, and the messages that said
"mount" now say "open". The header also no longer needs the glibc 2.36
mount API wrappers.
Fixes: 2f614e4b0e5c ("selftests/ve: factor common helpers into ve_selftest.h")
Feature: fix selftests
https://virtuozzo.atlassian.net/browse/VSTOR-139515
Signed-off-by: Mirian Shilakadze <mirian.shilakadze@virtuozzo.com>
---
tools/testing/selftests/ve/ve_aio_ring_test.c | 2 +-
.../selftests/ve/ve_cpuid_override_test.c | 10 +++---
.../selftests/ve/ve_mount_accounting_test.c | 2 +-
tools/testing/selftests/ve/ve_ns_owner_test.c | 2 +-
.../selftests/ve/ve_numa_migrate_test.c | 4 +--
.../ve/ve_numa_syscalls_prohibited_test.c | 2 +-
tools/testing/selftests/ve/ve_perms_test.c | 2 +-
tools/testing/selftests/ve/ve_selftest.h | 36 ++++++++++---------
.../testing/selftests/ve/ve_tmpfs_mpol_test.c | 2 +-
tools/testing/selftests/ve/ve_vtty_stress.c | 4 +--
10 files changed, 35 insertions(+), 31 deletions(-)
diff --git a/tools/testing/selftests/ve/ve_aio_ring_test.c b/tools/testing/selftests/ve/ve_aio_ring_test.c
index 1922a05aceef..c5b90427bdb3 100644
--- a/tools/testing/selftests/ve/ve_aio_ring_test.c
+++ b/tools/testing/selftests/ve/ve_aio_ring_test.c
@@ -152,7 +152,7 @@ FIXTURE_SETUP(ve_aio_ring)
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
ASSERT_NE(self->arg, MAP_FAILED);
- self->cgv2_fd = mount_cg2_fd();
+ self->cgv2_fd = open_cg2_fd();
ASSERT_GE(self->cgv2_fd, 0);
ASSERT_EQ(write_file_at(self->cgv2_fd, "cgroup.subtree_control",
diff --git a/tools/testing/selftests/ve/ve_cpuid_override_test.c b/tools/testing/selftests/ve/ve_cpuid_override_test.c
index b9f5e49d6c43..fe8959b2adf3 100644
--- a/tools/testing/selftests/ve/ve_cpuid_override_test.c
+++ b/tools/testing/selftests/ve/ve_cpuid_override_test.c
@@ -153,8 +153,8 @@ FIXTURE_SETUP(ve_cpuid_override)
self->host.edx & ~self->bit);
/* Last, so that a failed ASSERT above leaves no VE cgroup behind. */
- self->cgv2_fd = mount_cg2_fd();
- ASSERT_GE(self->cgv2_fd, 0) TH_LOG("cgroup2 mount: %s", strerror(errno));
+ self->cgv2_fd = open_cg2_fd();
+ ASSERT_GE(self->cgv2_fd, 0) TH_LOG("cannot open cgroup2: %s", strerror(errno));
ASSERT_EQ(write_file_at(self->cgv2_fd, "cgroup.subtree_control",
VE_CONTROLLERS), 0);
self->ctid = make_ve(self->cgv2_fd, CTID_MIN);
@@ -164,7 +164,7 @@ FIXTURE_SETUP(ve_cpuid_override)
/*
* Runs after the test body in whichever process the harness picks, the test
* process itself after a failed ASSERT, so the table is restored whether or
- * not one fired. The setup fd may be gone by then, hence the fresh mount.
+ * not one fired. The setup fd may be gone by then, hence a fresh one.
*/
FIXTURE_TEARDOWN_PARENT(ve_cpuid_override)
{
@@ -179,9 +179,9 @@ FIXTURE_TEARDOWN_PARENT(ve_cpuid_override)
if (self->child > 0)
kill(self->child, SIGKILL);
- cgv2_fd = mount_cg2_fd();
+ cgv2_fd = open_cg2_fd();
if (cgv2_fd < 0) {
- TH_LOG("cgroup2 mount: %s, VE cgroup %d left behind",
+ TH_LOG("cannot open cgroup2: %s, VE cgroup %d left behind",
strerror(errno), self->ctid);
return;
}
diff --git a/tools/testing/selftests/ve/ve_mount_accounting_test.c b/tools/testing/selftests/ve/ve_mount_accounting_test.c
index 8bb13e961542..3c0791e80527 100644
--- a/tools/testing/selftests/ve/ve_mount_accounting_test.c
+++ b/tools/testing/selftests/ve/ve_mount_accounting_test.c
@@ -294,7 +294,7 @@ FIXTURE_SETUP(ve_mnt_acc)
int initial_mnt_avail_nr;
char path[64];
- self->cgv2_fd = mount_cg2_fd();
+ self->cgv2_fd = open_cg2_fd();
ASSERT_GE(self->cgv2_fd, 0);
mkdir(TMP_DIR, 0755);
diff --git a/tools/testing/selftests/ve/ve_ns_owner_test.c b/tools/testing/selftests/ve/ve_ns_owner_test.c
index dcff4d54172d..a177514343a2 100644
--- a/tools/testing/selftests/ve/ve_ns_owner_test.c
+++ b/tools/testing/selftests/ve/ve_ns_owner_test.c
@@ -142,7 +142,7 @@ FIXTURE_SETUP(ve_ns_owner)
char val[16];
char path[PATH_MAX];
- self->cgv2_fd = mount_cg2_fd();
+ self->cgv2_fd = open_cg2_fd();
ASSERT_GE(self->cgv2_fd, 0);
ASSERT_EQ(write_file_at(self->cgv2_fd, "cgroup.subtree_control",
diff --git a/tools/testing/selftests/ve/ve_numa_migrate_test.c b/tools/testing/selftests/ve/ve_numa_migrate_test.c
index 02e134bc914d..2223e770be62 100644
--- a/tools/testing/selftests/ve/ve_numa_migrate_test.c
+++ b/tools/testing/selftests/ve/ve_numa_migrate_test.c
@@ -363,9 +363,9 @@ static int prepare(FIXTURE_DATA(numa_migrate) *self, const char **why)
{
int fd;
- self->cgv2_fd = mount_cg2_fd();
+ self->cgv2_fd = open_cg2_fd();
if (self->cgv2_fd < 0) {
- *why = "cannot mount cgroup2";
+ *why = "cannot open cgroup2";
goto fail;
}
if (write_file_at(self->cgv2_fd, "cgroup.subtree_control",
diff --git a/tools/testing/selftests/ve/ve_numa_syscalls_prohibited_test.c b/tools/testing/selftests/ve/ve_numa_syscalls_prohibited_test.c
index c76fcb1c79ff..ef44b1191c70 100644
--- a/tools/testing/selftests/ve/ve_numa_syscalls_prohibited_test.c
+++ b/tools/testing/selftests/ve/ve_numa_syscalls_prohibited_test.c
@@ -126,7 +126,7 @@ FIXTURE_SETUP(numa_syscalls)
}
ASSERT_LT(i, NODE_BITS);
- self->cgv2_fd = mount_cg2_fd();
+ self->cgv2_fd = open_cg2_fd();
ASSERT_GE(self->cgv2_fd, 0);
ASSERT_EQ(write_file_at(self->cgv2_fd, "cgroup.subtree_control",
VE_CONTROLLERS), 0);
diff --git a/tools/testing/selftests/ve/ve_perms_test.c b/tools/testing/selftests/ve/ve_perms_test.c
index c57325d10901..375f98c36f87 100644
--- a/tools/testing/selftests/ve/ve_perms_test.c
+++ b/tools/testing/selftests/ve/ve_perms_test.c
@@ -189,7 +189,7 @@ static int default_granted(int cgv2_fd, const char *fs, const char *entry)
FIXTURE_SETUP(ve_perms)
{
- self->cgv2_fd = mount_cg2_fd();
+ self->cgv2_fd = open_cg2_fd();
ASSERT_GE(self->cgv2_fd, 0);
ASSERT_EQ(write_file_at(self->cgv2_fd, "cgroup.subtree_control",
diff --git a/tools/testing/selftests/ve/ve_selftest.h b/tools/testing/selftests/ve/ve_selftest.h
index 1fc3f8dcac18..af7725600453 100644
--- a/tools/testing/selftests/ve/ve_selftest.h
+++ b/tools/testing/selftests/ve/ve_selftest.h
@@ -1,8 +1,8 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
- * Shared helpers for the ve selftests: a private cgroup2 mount, small file and
- * cgroup helpers, VE cgroup create and destroy, and a mount point query, used
- * across the tests in this directory.
+ * Shared helpers for the ve selftests: the host cgroup2 mount as a directory
+ * fd, small file and cgroup helpers, VE cgroup create and destroy, and a mount
+ * point query, used across the tests in this directory.
*/
#ifndef __SELFTESTS_VE_VE_SELFTEST_H
#define __SELFTESTS_VE_VE_SELFTEST_H
@@ -15,8 +15,9 @@
#include <errno.h>
#include <limits.h>
#include <sys/stat.h>
-#include <sys/mount.h>
+#include <sys/vfs.h>
#include <sys/syscall.h>
+#include <linux/magic.h>
#include <linux/openat2.h>
#ifndef CLONE_NEWVE
@@ -245,23 +246,26 @@ static inline int read_s32_at(int dirfd, const char *path,
return 0;
}
-/* A fresh, private cgroup2 mount, returned as an fd to use with *at() calls. */
-static inline int mount_cg2_fd(void)
+/*
+ * The host cgroup2 mount as a directory fd to use with *at() calls. The mount
+ * is opened, not created: mounting cgroup2 from the root cgroup namespace
+ * applies the new mount's options to the whole hierarchy and clears the ones
+ * it does not carry, nsdelegate and memory_recursiveprot included.
+ */
+static inline int open_cg2_fd(void)
{
- int fs_fd, mnt_fd;
+ struct statfs st;
+ int fd;
- fs_fd = fsopen("cgroup2", 0);
- if (fs_fd < 0)
+ fd = open("/sys/fs/cgroup", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
+ if (fd < 0)
return -1;
-
- if (fsconfig(fs_fd, FSCONFIG_CMD_CREATE, NULL, NULL, 0) < 0) {
- close(fs_fd);
+ if (fstatfs(fd, &st) < 0 || st.f_type != CGROUP2_SUPER_MAGIC) {
+ close(fd);
+ errno = ENODEV;
return -1;
}
-
- mnt_fd = fsmount(fs_fd, 0, 0);
- close(fs_fd);
- return mnt_fd;
+ return fd;
}
/* Move the caller into cgroup @ctid (or the root when @ctid is 0). */
diff --git a/tools/testing/selftests/ve/ve_tmpfs_mpol_test.c b/tools/testing/selftests/ve/ve_tmpfs_mpol_test.c
index 6254f95fadec..dbc1a51941a0 100644
--- a/tools/testing/selftests/ve/ve_tmpfs_mpol_test.c
+++ b/tools/testing/selftests/ve/ve_tmpfs_mpol_test.c
@@ -57,7 +57,7 @@ FIXTURE_SETUP(tmpfs_mpol)
SKIP(return, "no NUMA API on the host");
ASSERT_EQ(ret, 0);
- self->cgv2_fd = mount_cg2_fd();
+ self->cgv2_fd = open_cg2_fd();
ASSERT_GE(self->cgv2_fd, 0);
ASSERT_EQ(write_file_at(self->cgv2_fd, "cgroup.subtree_control",
VE_CONTROLLERS), 0);
diff --git a/tools/testing/selftests/ve/ve_vtty_stress.c b/tools/testing/selftests/ve/ve_vtty_stress.c
index f4bdd81133b8..318ccab5bcdb 100644
--- a/tools/testing/selftests/ve/ve_vtty_stress.c
+++ b/tools/testing/selftests/ve/ve_vtty_stress.c
@@ -192,9 +192,9 @@ int main(int argc, char **argv)
"(vzdev, vzmon) are needed\n",
strerror(errno));
- cg = mount_cg2_fd();
+ cg = open_cg2_fd();
if (cg < 0)
- ksft_exit_skip("cannot mount cgroup2: %s\n", strerror(errno));
+ ksft_exit_skip("cannot open cgroup2: %s\n", strerror(errno));
if (write_file_at(cg, "cgroup.subtree_control", VE_CONTROLLERS) < 0)
ksft_exit_skip("cannot enable the ve controller: %s\n",
strerror(errno));
--
2.47.3
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
next prev parent reply other threads:[~2026-11-19 8:18 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 8:16 [Devel] [PATCH hci-8.2 v2 0/3] selftests/ve: move ve_devcg_bpf and ve_printk into ve/ Mirian Shilakadze
2026-11-19 8:16 ` Mirian Shilakadze [this message]
2026-11-19 8:21 ` [Devel] [PATCH hci-8.2 v2 1/3] selftests/ve: open the host cgroup2 mount instead of creating one no-reply
2026-11-19 17:00 ` Konstantin Khorenko
2026-11-19 8:16 ` [Devel] [PATCH hci-8.2 v2 2/3] selftests/ve: move ve_devcg_bpf into ve/ and reuse ve_selftest.h Mirian Shilakadze
2026-11-19 8:20 ` no-reply
2026-11-19 17:00 ` Konstantin Khorenko
2026-11-19 8:16 ` [Devel] [PATCH hci-8.2 v2 3/3] selftests/ve: move ve_printk " Mirian Shilakadze
2026-11-19 8:22 ` no-reply
2026-11-19 17:00 ` Konstantin Khorenko
2026-11-19 17:00 ` [Devel] [PATCH hci-8.2 v2 0/3] selftests/ve: move ve_devcg_bpf and ve_printk into ve/ Konstantin Khorenko
2026-11-19 17:01 ` Konstantin Khorenko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261119081654.484107-2-mirian.shilakadze@virtuozzo.com \
--to=mirian.shilakadze@virtuozzo.com \
--cc=devel@openvz.org \
--cc=khorenko@virtuozzo.com \
--cc=vladimir.riabchun@virtuozzo.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.