[Devel] [PATCH hci-8.2 v2 2/3] selftests/ve: move ve_devcg_bpf into ve/ and reuse ve_selftest.h

Mirian Shilakadze mirian.shilakadze at virtuozzo.com
Thu Nov 19 11:16:27 MSK 2026


ve_devcg_bpf lives in its own selftests directory and carries private
copies of the helpers that ve_selftest.h provides: a mount_cg2_fd of its
own, which creates the optionless cgroup2 mount the previous commit
stopped the shared header from creating, write_file_at, enter_cgroup,
the free ctid search and VE cgroup creation, the CLONE_NEWVE fallback,
the ctid range and the controller set a VE cgroup needs enabled.

Move the test into tools/testing/selftests/ve/, include ve_selftest.h
and drop the local copies. The fixture takes its cgroup2 fd from
open_cg2_fd(), creates its VE cgroup with make_ve and removes it with
destroy_ve. The vz.slice removal in the teardown stays: the kernel
creates that cgroup under the VE root when the container is started,
and this test starts one. The TOOLS_INCLUDES flag is not carried over,
the ve Makefile builds against the installed kernel headers, which have
orig_prog_len as well. destroy_ve retries a busy cgroup for up to a
second and make_ve removes the cgroup when unhiding the ve files fails.
The old code did neither. The test cases are unchanged.

Feature: fix selftests
https://virtuozzo.atlassian.net/browse/VSTOR-139515
Signed-off-by: Mirian Shilakadze <mirian.shilakadze at virtuozzo.com>
---
 tools/testing/selftests/Makefile              |  1 -
 tools/testing/selftests/ve/.gitignore         |  1 +
 tools/testing/selftests/ve/Makefile           |  1 +
 .../{ve_devcg_bpf => ve}/ve_devcg_bpf_test.c  | 89 ++-----------------
 .../testing/selftests/ve_devcg_bpf/.gitignore |  1 -
 tools/testing/selftests/ve_devcg_bpf/Makefile | 10 ---
 6 files changed, 9 insertions(+), 94 deletions(-)
 rename tools/testing/selftests/{ve_devcg_bpf => ve}/ve_devcg_bpf_test.c (90%)
 delete mode 100644 tools/testing/selftests/ve_devcg_bpf/.gitignore
 delete mode 100644 tools/testing/selftests/ve_devcg_bpf/Makefile

diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index 8ec0ba2deb62..9f820344c16c 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -119,7 +119,6 @@ TARGETS += user_events
 TARGETS += vDSO
 TARGETS += ve
 TARGETS += ve_printk
-TARGETS += ve_devcg_bpf
 TARGETS += mm
 TARGETS += vfio
 TARGETS += x86
diff --git a/tools/testing/selftests/ve/.gitignore b/tools/testing/selftests/ve/.gitignore
index a468ef980e28..9b1a8911b4cf 100644
--- a/tools/testing/selftests/ve/.gitignore
+++ b/tools/testing/selftests/ve/.gitignore
@@ -7,3 +7,4 @@ ve_numa_migrate_test
 ve_aio_ring_test
 ve_numa_syscalls_prohibited_test
 ve_tmpfs_mpol_test
+ve_devcg_bpf_test
diff --git a/tools/testing/selftests/ve/Makefile b/tools/testing/selftests/ve/Makefile
index fa4f858227e5..38d92eecc3c8 100644
--- a/tools/testing/selftests/ve/Makefile
+++ b/tools/testing/selftests/ve/Makefile
@@ -10,6 +10,7 @@ TEST_GEN_PROGS += ve_numa_migrate_test
 TEST_GEN_PROGS += ve_aio_ring_test
 TEST_GEN_PROGS += ve_numa_syscalls_prohibited_test
 TEST_GEN_PROGS += ve_tmpfs_mpol_test
+TEST_GEN_PROGS += ve_devcg_bpf_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_devcg_bpf/ve_devcg_bpf_test.c b/tools/testing/selftests/ve/ve_devcg_bpf_test.c
similarity index 90%
rename from tools/testing/selftests/ve_devcg_bpf/ve_devcg_bpf_test.c
rename to tools/testing/selftests/ve/ve_devcg_bpf_test.c
index cca0f6905d8d..4027754ace34 100644
--- a/tools/testing/selftests/ve_devcg_bpf/ve_devcg_bpf_test.c
+++ b/tools/testing/selftests/ve/ve_devcg_bpf_test.c
@@ -35,13 +35,7 @@
 #include <errno.h>
 
 #include "../kselftest_harness.h"
-
-#define CTID_MIN		108
-#define CTID_MAX		200
-
-#ifndef CLONE_NEWVE
-#define CLONE_NEWVE		0x00000040
-#endif
+#include "ve_selftest.h"
 
 #ifndef VE_FEATURE_BPF
 #define VE_FEATURE_SYSFS	(1ULL << 0)	/* deprecated */
@@ -74,20 +68,6 @@
 #endif
 
 
-static int write_file_at(int dirfd, const char *path, const char *val)
-{
-	int fd, ret;
-	size_t len = strlen(val);
-
-	fd = openat(dirfd, path, O_WRONLY);
-	if (fd < 0)
-		return -1;
-
-	ret = write(fd, val, len);
-	close(fd);
-	return (ret == len) ? 0 : -1;
-}
-
 enum {
 	TEST_PROG_LOAD,
 	TEST_PROG_LOAD_DENIED,
@@ -132,25 +112,6 @@ static int load_devcg_prog(void)
 	return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
 }
 
-static int mount_cg2_fd(void)
-{
-	int fs_fd, mnt_fd;
-
-	fs_fd = syscall(__NR_fsopen, "cgroup2", 0);
-	if (fs_fd < 0)
-		return -1;
-
-	if (syscall(__NR_fsconfig, fs_fd, FSCONFIG_CMD_CREATE,
-		    NULL, NULL, 0) < 0) {
-		close(fs_fd);
-		return -1;
-	}
-
-	mnt_fd = syscall(__NR_fsmount, fs_fd, 0, 0);
-	close(fs_fd);
-	return mnt_fd;
-}
-
 static int test_prog_load(void)
 {
 	int fd;
@@ -524,30 +485,6 @@ int child_func(void *arg)
 	return ret;
 }
 
-int enter_cgroup(int cgv2_fd, int ctid)
-{
-	char cg_path[64];
-	char pid_str[64];
-	int fd;
-	int ret;
-
-	if (ctid)
-		snprintf(cg_path, sizeof(cg_path), "%d/cgroup.procs", ctid);
-	else
-		snprintf(cg_path, sizeof(cg_path), "cgroup.procs");
-	fd = openat(cgv2_fd, cg_path, O_WRONLY);
-	if (fd < 0)
-		return -1;
-
-	snprintf(pid_str, sizeof(pid_str), "%d", getpid());
-	ret = write(fd, pid_str, strlen(pid_str));
-	if (ret < 0 || ret != strlen(pid_str))
-		ret = -1;
-
-	close(fd);
-	return ret;
-}
-
 int run_vzct(struct __test_metadata *_metadata, int cgv2_fd, int ctid,
 	     int testid)
 {
@@ -592,11 +529,11 @@ FIXTURE_SETUP(ve_devcg_bpf)
 	char ctid_str[16];
 	char path[64];
 
-	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",
-		  "+cpuset +cpu +cpuacct +io +memory +hugetlb +pids +rdma +misc +ve"), 0);
+		  VE_CONTROLLERS), 0);
 
 	ASSERT_EQ(write_file_at(self->cgv2_fd,
 		  "ve.default_sysfs_permissions", "/ rx"), 0);
@@ -605,21 +542,10 @@ FIXTURE_SETUP(ve_devcg_bpf)
 	ASSERT_EQ(write_file_at(self->cgv2_fd,
 		  "ve.default_sysfs_permissions", "fs/cgroup rw"), 0);
 
-	self->ctid = CTID_MIN;
-	while (self->ctid < CTID_MAX) {
-		snprintf(ctid_str, sizeof(ctid_str), "%d", self->ctid);
-		if (faccessat(self->cgv2_fd, ctid_str, F_OK, 0) != 0 &&
-		    errno == ENOENT)
-			break;
-		self->ctid++;
-	}
-	ASSERT_LT(self->ctid, CTID_MAX);
-
-	ASSERT_EQ(mkdirat(self->cgv2_fd, ctid_str, 0755), 0);
-
-	snprintf(path, sizeof(path), "%d/cgroup.controllers_hidden", self->ctid);
-	ASSERT_EQ(write_file_at(self->cgv2_fd, path, "-ve"), 0);
+	self->ctid = make_ve(self->cgv2_fd, CTID_MIN);
+	ASSERT_GE(self->ctid, 0);
 
+	snprintf(ctid_str, sizeof(ctid_str), "%d", self->ctid);
 	snprintf(path, sizeof(path), "%d/ve.veid", self->ctid);
 	ASSERT_EQ(write_file_at(self->cgv2_fd, path, ctid_str), 0);
 };
@@ -630,8 +556,7 @@ FIXTURE_TEARDOWN(ve_devcg_bpf)
 
 	snprintf(path, sizeof(path), "%d/vz.slice", self->ctid);
 	unlinkat(self->cgv2_fd, path, AT_REMOVEDIR);
-	snprintf(path, sizeof(path), "%d", self->ctid);
-	unlinkat(self->cgv2_fd, path, AT_REMOVEDIR);
+	destroy_ve(self->cgv2_fd, self->ctid);
 	close(self->cgv2_fd);
 }
 
diff --git a/tools/testing/selftests/ve_devcg_bpf/.gitignore b/tools/testing/selftests/ve_devcg_bpf/.gitignore
deleted file mode 100644
index a1f032ce2428..000000000000
--- a/tools/testing/selftests/ve_devcg_bpf/.gitignore
+++ /dev/null
@@ -1 +0,0 @@
-ve_devcg_bpf_test
diff --git a/tools/testing/selftests/ve_devcg_bpf/Makefile b/tools/testing/selftests/ve_devcg_bpf/Makefile
deleted file mode 100644
index ba811f22e36a..000000000000
--- a/tools/testing/selftests/ve_devcg_bpf/Makefile
+++ /dev/null
@@ -1,10 +0,0 @@
-# SPDX-License-Identifier: GPL-2.0
-# Makefile for ve_devcg_bpf selftests.
-CFLAGS += -g -Wall -O2
-# orig_prog_len/orig_prog_insns may be newer than the system UAPI headers, so
-# build against the in-tree copy under tools/include/uapi.
-CFLAGS += $(TOOLS_INCLUDES)
-
-TEST_GEN_PROGS += ve_devcg_bpf_test
-
-include ../lib.mk
-- 
2.47.3



More information about the Devel mailing list