Skip to contentSkip to conversation

cgroup: dmem: add selftest helper, coverage, and VM runner

PATCH Superseded Albert Esteve 1 participant 5 messages May 19

Subsystems (2)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v4 cover cgroup: dmem: add selftest helper, coverage, and VM runner

Hi all,

This small series adds practical test coverage for the dmem cgroup controller.

The motivation came from following the recent dmem API discussion in thread [1]. That discussion considered changing the dmem API and adding a new knob. Currently there are no dedicated tests covering dmem behaviour, which makes such changes riskier.

Adding selftests has an additional challenge: dmem charging paths are driver-driven today, so regression testing is harder unless a suitable driver is present in the test environment.

This series addresses that by adding:
- a kernel-side selftest helper module to trigger charge/uncharge from userspace in a controlled way,
- cgroup selftests covering dmem accounting and protection semantics (including dmem.max enforcement and byte-granularity checks),
- a virtme-based VM runner for repeatable execution of the dmem tests.

The goal is to make dmem behavior easier to validate when evolving the API and implementation, while keeping tests deterministic and driver-independent.

Thanks.

[1] - https://lore.kernel.org/all/aZoHfloupKvF2oSu@fedora/

---
Changes in v4:
- Fix charged_pool leak in dmem_selftest_init()
- Replace ssh-based VM approach in vmtest-dmem.sh with vng --exec
- Add main() wrapper to vmtest-dmem.sh
- Other small fixes suggested by Sashiko
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Set charged flag on unexpected over-limit charge success.
- Add CONFIG_DMEM_SELFTEST=m to selftest config.
- Simplify -v to a boolean; single -v was a no-op.
- Unquote kernel_opt to avoid empty-string arg to vng.
- Document -b in usage() output.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Fix debugfs_create_dir() error check
- Fix module teardown race: call dmem_selftest_remove() before
uncharging so debugfs files are torn down
- Use IS_ERR_OR_NULL() in selftest() sanity check
- Add CONFIG_CGROUP_DMEM=y to the cgroup selftest config
- Replace config-file parsing in check_guest_requirements() with
a direct check of /sys/fs/cgroup/cgroup.controllers
- Add new patch 4 (from Eric Chanudet): vmtest-dmem.sh -b flag
to configure and build a local kernel tree
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Albert Esteve (4):
cgroup: Add dmem_selftest module
selftests: cgroup: Add dmem selftest coverage
selftests: cgroup: Add vmtest-dmem runner script
selftests: cgroup: handle vmtest-dmem -b to test locally built kernel

 init/Kconfig                                  |  12 +
 kernel/cgroup/Makefile                        |   1 +
 kernel/cgroup/dmem_selftest.c                 | 198 +++++++++++
 tools/testing/selftests/cgroup/.gitignore     |   1 +
 tools/testing/selftests/cgroup/Makefile       |   4 +-
 tools/testing/selftests/cgroup/config         |   2 +
 tools/testing/selftests/cgroup/test_dmem.c    | 492 ++++++++++++++++++++++++++
 tools/testing/selftests/cgroup/vmtest-dmem.sh | 177 +++++++++
 8 files changed, 886 insertions(+), 1 deletion(-)

---
base-commit: 70eda68668d1476b459b64e69b8f36659fa9dfa8
change-id: 20260318-kunit_cgroups-7fb0b9e64017

Best regards,

Signature · 1 line
Albert Esteve <[email protected]>
#1 lore raw

PATCH v4 1/4 cgroup: Add dmem_selftest module

Currently, dmem charging is driver-driven through direct calls to dmem_cgroup_try_charge(), so cgroup selftests do not have a generic way to trigger charge and uncharge paths from userspace.

This limits any selftest coverage to configuration/readout checks unless a specific driver exposing charge hooks is present in the test environment.

Add kernel/cgroup/dmem_selftest.c as a helper module (CONFIG_DMEM_SELFTEST) that registers a synthetic dmem region (dmem_selftest) and exposes debugfs control files:
/sys/kernel/debug/dmem_selftest/charge /sys/kernel/debug/dmem_selftest/uncharge

Writing a size to charge triggers dmem_cgroup_try_charge() for the calling task's cgroup (the module calls kstrtou64()). Writing to uncharge releases the outstanding charge via dmem_cgroup_uncharge(). Only a single outstanding charge is supported.

This provides a deterministic, driver-independent mechanism for exercising dmem accounting paths in selftests.

---
 init/Kconfig                  |  12 +++
 kernel/cgroup/Makefile        |   1 +
 kernel/cgroup/dmem_selftest.c | 198 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 211 insertions(+)
init/Kconfig +12 -0
index 2937c4d308aec..4620ad92f72c0 100644
@@ -1256,6 +1256,18 @@ config CGROUP_DMEM
 	  As an example, it allows you to restrict VRAM usage for applications
 	  in the DRM subsystem.
 
+config DMEM_SELFTEST
+	tristate "dmem cgroup selftest helper module"
+	depends on CGROUP_DMEM && DEBUG_FS
+	default n
+	help
+	  Builds a small loadable module that registers a dmem region named
+	  "dmem_selftest" and exposes debugfs files under
+	  /sys/kernel/debug/dmem_selftest/ so kselftests can trigger
+	  dmem charge/uncharge operations from userspace.
+
+	  Say N unless you run dmem selftests or develop the dmem controller.
+
 config CGROUP_FREEZER
 	bool "Freezer controller"
 	help
kernel/cgroup/Makefile +1 -0
index ede31601a363a..febc36e60f9f9 100644
@@ -8,4 +8,5 @@ obj-$(CONFIG_CPUSETS) += cpuset.o
 obj-$(CONFIG_CPUSETS_V1) += cpuset-v1.o
 obj-$(CONFIG_CGROUP_MISC) += misc.o
 obj-$(CONFIG_CGROUP_DMEM) += dmem.o
+obj-$(CONFIG_DMEM_SELFTEST) += dmem_selftest.o
 obj-$(CONFIG_CGROUP_DEBUG) += debug.o
kernel/cgroup/dmem_selftest.c new file +198 -0

198 lines not on this page · raw message

Signature · 1 line
2.53.0
#2 lore raw

PATCH v4 2/4 selftests: cgroup: Add dmem selftest coverage

In reply to Albert Esteve

Currently, tools/testing/selftests/cgroup/ does not include a dmem-specific test binary. This leaves dmem charge and limit behavior largely unvalidated in kselftest coverage.

Add test_dmem and wire it into the cgroup selftests Makefile. The new test exercises dmem controller behavior through the dmem_selftest debugfs interface for the dmem_selftest region.

The test adds three complementary checks:
- test_dmem_max creates a nested hierarchy with per-leaf dmem.max values and verifies that over-limit charges fail while in-limit charges succeed with bounded rounding in dmem.current.
- test_dmem_min and test_dmem_low verify that charging from a cgroup with the corresponding protection knob set updates dmem.current as expected.
- test_dmem_charge_byte_granularity validates accounting bounds for non-page-aligned charge sizes and uncharge-to-zero behavior.

This provides deterministic userspace coverage for dmem accounting and hard-limit enforcement using a test helper module, without requiring subsystem-specific production drivers.

---
 tools/testing/selftests/cgroup/.gitignore  |   1 +
 tools/testing/selftests/cgroup/Makefile    |   2 +
 tools/testing/selftests/cgroup/config      |   2 +
 tools/testing/selftests/cgroup/test_dmem.c | 492 +++++++++++++++++++++++++++++
 4 files changed, 497 insertions(+)
tools/testing/selftests/cgroup/.gitignore +1 -0
index 952e4448bf070..ea2322598217d 100644
@@ -2,6 +2,7 @@
 test_core
 test_cpu
 test_cpuset
+test_dmem
 test_freezer
 test_hugetlb_memcg
 test_kill
tools/testing/selftests/cgroup/Makefile +2 -0
index e01584c2189ac..e1a5e9316620e 100644
@@ -10,6 +10,7 @@ TEST_GEN_FILES := wait_inotify
 TEST_GEN_PROGS  = test_core
 TEST_GEN_PROGS += test_cpu
 TEST_GEN_PROGS += test_cpuset
+TEST_GEN_PROGS += test_dmem
 TEST_GEN_PROGS += test_freezer
 TEST_GEN_PROGS += test_hugetlb_memcg
 TEST_GEN_PROGS += test_kill
@@ -26,6 +27,7 @@ include lib/libcgroup.mk
 $(OUTPUT)/test_core: $(LIBCGROUP_O)
 $(OUTPUT)/test_cpu: $(LIBCGROUP_O)
 $(OUTPUT)/test_cpuset: $(LIBCGROUP_O)
+$(OUTPUT)/test_dmem: $(LIBCGROUP_O)
 $(OUTPUT)/test_freezer: $(LIBCGROUP_O)
 $(OUTPUT)/test_hugetlb_memcg: $(LIBCGROUP_O)
 $(OUTPUT)/test_kill: $(LIBCGROUP_O)
tools/testing/selftests/cgroup/config +2 -0
index 39f979690dd3b..5728278310a31 100644
@@ -1,6 +1,8 @@
 CONFIG_CGROUPS=y
 CONFIG_CGROUP_CPUACCT=y
+CONFIG_CGROUP_DMEM=y
 CONFIG_CGROUP_FREEZER=y
 CONFIG_CGROUP_SCHED=y
+CONFIG_DMEM_SELFTEST=m
 CONFIG_MEMCG=y
 CONFIG_PAGE_COUNTER=y
tools/testing/selftests/cgroup/test_dmem.c new file +492 -0

492 lines not on this page · raw message

Signature · 1 line
2.53.0
#3 lore raw

PATCH v4 3/4 selftests: cgroup: Add vmtest-dmem runner script

In reply to Albert Esteve

Currently, test_dmem relies on the dmem_selftest helper module and a VM setup that may not have the helper preinstalled. This makes automated coverage of dmem charge paths harder in virtme-based runs.

Add tools/testing/selftests/cgroup/vmtest-dmem.sh to provide a repeatable VM workflow for dmem tests. The script uses vng --exec to run the test directly inside a virtme-ng guest with minimal setup.

The script boots a virtme-ng guest, validates dmem controller availability, ensures the dmem helper path is present, and runs tools/testing/selftests/cgroup/test_dmem. If the helper is not available as a loaded module, it attempts module build/load for the running guest kernel before executing the test binary.

The runner also supports interactive shell mode (-s) and reuses the verbosity and KTAP exit-code conventions used by other vmtest scripts, so it integrates with existing kselftest workflows.

---
 tools/testing/selftests/cgroup/Makefile       |   2 +-
 tools/testing/selftests/cgroup/vmtest-dmem.sh | 156 ++++++++++++++++++++++++++
 2 files changed, 157 insertions(+), 1 deletion(-)
tools/testing/selftests/cgroup/Makefile +1 -1
index e1a5e9316620e..2c407710c6e3b 100644
@@ -3,7 +3,7 @@ CFLAGS += -Wall -pthread
 
 all: ${HELPER_PROGS}
 
-TEST_FILES     := with_stress.sh
+TEST_FILES     := with_stress.sh vmtest-dmem.sh
 TEST_PROGS     := test_stress.sh test_cpuset_prs.sh test_cpuset_v1_hp.sh
 TEST_GEN_FILES := wait_inotify
 # Keep the lists lexicographically sorted
tools/testing/selftests/cgroup/vmtest-dmem.sh new file +156 -0

156 lines not on this page · raw message

Signature · 1 line
2.53.0
#4 lore raw

PATCH v4 4/4 selftests: cgroup: handle vmtest-dmem -b to test locally built kernel

In reply to Albert Esteve

Currently vmtest-dmem.sh relies on the host's running kernel or a pre-built one when booting the virtme-ng VM, with no option to configure and build a local kernel tree directly.

This adds friction to the development cycle: the user must manually run vng --kconfig with the correct config fragment, build the kernel, and pass the result to the script.

Add a -b flag that automates this workflow. When set, handle_build() configures the kernel using vng --kconfig with the selftest config fragment, builds it with make -j$(nproc), and run_vm() passes the local tree to vng --run so the VM boots the freshly built kernel.

---
 tools/testing/selftests/cgroup/vmtest-dmem.sh | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
tools/testing/selftests/cgroup/vmtest-dmem.sh +22 -1
index b395b7153f635..415c1722b08e7 100755
@@ -15,6 +15,7 @@ readonly KERNEL_CHECKOUT="$(realpath "${SCRIPT_DIR}"/../../../../)"
 
 source "${SCRIPT_DIR}"/../kselftest/ktap_helpers.sh
 
+BUILD=0
 QEMU="qemu-system-$(uname -m)"
 VERBOSE=0
 SHELL_MODE=0
@@ -26,6 +27,7 @@ function usage() {
 	cat <<EOF
 $0 [OPTIONS]
 Options:
+	-b	Build kernel from source tree before booting
 	-q	QEMU binary/path (default: ${QEMU})
 	-s	Start interactive shell in VM instead of running tests
 	-v	Verbose output (vng boot logs on stdout)
@@ -60,16 +62,33 @@ function check_deps() {
 	done
 }
 
+function handle_build() {
+	[[ "${BUILD}" -eq 1 ]] || return 0
+
+	[[ -d "${KERNEL_CHECKOUT}" ]] || \
+		fail "-b requires vmtest-dmem.sh called from the kernel source tree"
+
+	pushd "${KERNEL_CHECKOUT}" &>/dev/null
+	vng --kconfig --config "${SCRIPT_DIR}"/config || \
+		fail "failed to generate .config for kernel source tree (${KERNEL_CHECKOUT})"
+	make O= KBUILD_OUTPUT= -j"$(nproc)" || \
+		fail "failed to build kernel from source tree (${KERNEL_CHECKOUT})"
+	popd &>/dev/null
+}
+
 # Run vng with common flags. Extra arguments are appended by the caller:
 #   --exec <script>  for automated test runs
 #   (nothing)        for interactive shell mode
 function run_vm() {
 	local verbose_opt=""
+	local kernel_opt=""
 
 	[[ "${VERBOSE}" -eq 1 ]] && verbose_opt="--verbose"
+	[[ "${BUILD}" -eq 1 ]] && kernel_opt="${KERNEL_CHECKOUT}"
 
 	vng \
 		--run \
+		${kernel_opt:+"${kernel_opt}"} \
 		${verbose_opt:+"${verbose_opt}"} \
 		--qemu="$(command -v "${QEMU}")" \
 		--user root \
@@ -78,10 +97,11 @@ function run_vm() {
 }
 
 function main() {
-	while getopts ':hvq:s' opt; do
+	while getopts ':hvq:sb' opt; do
 		case "${opt}" in
 		v) VERBOSE=1 ;;
 		q) QEMU="${OPTARG}" ;;
+		b) BUILD=1 ;;
 		s) SHELL_MODE=1 ;;
 		h) usage; exit 0 ;;
 		*) usage; exit 1 ;;
@@ -89,6 +109,7 @@ function main() {
 	done
 
 	check_deps
+	handle_build
 
 	if [[ "${SHELL_MODE}" -eq 1 ]]; then
 		echo "Starting interactive shell in VM. Exit to stop VM."
Signature · 1 line
2.53.0