cgroup: dmem: add selftest helper, coverage, and VM runner
v3 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 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 (3):
cgroup: Add dmem_selftest module
selftests: cgroup: Add dmem selftest coverage
selftests: cgroup: Add vmtest-dmem runner based on hid vmtest
Eric Chanudet (1):
selftests: cgroup: handle vmtest-dmem -b to test locally built kernel
init/Kconfig | 12 + kernel/cgroup/Makefile | 1 + kernel/cgroup/dmem_selftest.c | 199 +++++++++++ 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 ++++++++++++++++++++++++++ tools/testing/selftests/cgroup/vmtest-dmem.sh | 230 ++++++++++++ 8 files changed, 939 insertions(+)
---
base-commit: 80234b5ab240f52fa45d201e899e207b9265ef91
change-id: 20260318-kunit_cgroups-7fb0b9e64017
Best regards,
Signature · 1 line
Albert Esteve <[email protected]>
PATCH v3 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 | 199 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 212 insertions(+)
init/Kconfig +12 -0
@@ -1238,6 +1238,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
@@ -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 +199 -0
199 lines not on this page · raw message
Signature · 1 line
2.53.0
PATCH v3 2/4 selftests: cgroup: Add dmem selftest coverage
In reply to Albert EsteveCurrently, 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
@@ -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
@@ -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
@@ -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
PATCH v3 3/4 selftests: cgroup: Add vmtest-dmem runner based on hid vmtest
In reply to Albert EsteveCurrently, 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, modeled after the existing selftests vmtest runners (notably tools/testing/selftests/hid/vmtest.sh), to provide a repeatable VM workflow for dmem tests.
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 and reuses the same verbosity and exit-code conventions used by other vmtest scripts, so it integrates with existing kselftest workflows.
--- tools/testing/selftests/cgroup/vmtest-dmem.sh | 197 ++++++++++++++++++++++++++ 1 file changed, 197 insertions(+)
tools/testing/selftests/cgroup/vmtest-dmem.sh new file +197 -0
@@ -0,0 +1,197 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright (c) 2026 Red Hat, Inc. +# +# Run cgroup test_dmem inside a virtme-ng VM. +# Dependencies: +# * virtme-ng +# * busybox-static (used by virtme-ng) +# * qemu (used by virtme-ng) + +set -euo pipefail + +readonly SCRIPT_DIR="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)" +readonly KERNEL_CHECKOUT="$(realpath "${SCRIPT_DIR}"/../../../../)" + +source "${SCRIPT_DIR}"/../kselftest/ktap_helpers.sh + +readonly SSH_GUEST_PORT="${SSH_GUEST_PORT:-22}" +readonly WAIT_PERIOD=3 +readonly WAIT_PERIOD_MAX=80 +readonly WAIT_TOTAL=$((WAIT_PERIOD * WAIT_PERIOD_MAX)) +readonly QEMU_PIDFILE="$(mktemp /tmp/qemu_dmem_vmtest_XXXX.pid)" +readonly QEMU_OPTS=" --pidfile ${QEMU_PIDFILE} " + +QEMU="qemu-system-$(uname -m)" +VERBOSE=0 +SHELL_MODE=0 +GUEST_TREE="${GUEST_TREE:-$KERNEL_CHECKOUT}" + +usage() { + echo + echo "$0 [OPTIONS]" + echo " -q <qemu> QEMU binary/path (default: ${QEMU})" + echo " -s Start interactive shell in VM" + echo " -v Verbose output (vng boot logs on stdout)" + echo + + exit 1 +} + +die() { + echo "$*" >&2 + exit "${KSFT_FAIL}" +} + +cleanup() { + if [[ -s "${QEMU_PIDFILE}" ]]; then + pkill -SIGTERM -F "${QEMU_PIDFILE}" >/dev/null 2>&1 || true + fi + + # If failure occurred during or before qemu start up, then we need + # to clean this up ourselves. + if [[ -e "${QEMU_PIDFILE}" ]]; then + rm -f "${QEMU_PIDFILE}" + fi +} + +vm_ssh() { + stdbuf -oL ssh -q \ + -F "${HOME}/.cache/virtme-ng/.ssh/virtme-ng-ssh.conf" \ + -l root "virtme-ng%${SSH_GUEST_PORT}" \ + "$@" +} + +check_deps() { + for dep in vng "${QEMU}" busybox pkill ssh; do + if ! command -v "${dep}" >/dev/null 2>&1; then + echo "skip: dependency ${dep} not found" + exit "${KSFT_SKIP}" + fi + done +} + +vm_start() { + local logfile=/dev/null + local verbose_opt="" + + if [[ "${VERBOSE}" -eq 1 ]]; then + verbose_opt="--verbose" + logfile=/dev/stdout + fi + + vng \ + --run \ + ${verbose_opt} \ + --qemu-opts="${QEMU_OPTS}" \ + --qemu="$(command -v "${QEMU}")" \ + --user root \ + --ssh "${SSH_GUEST_PORT}" \ + --rw &>"${logfile}" & + + local vng_pid=$! + local elapsed=0 + + while [[ ! -s "${QEMU_PIDFILE}" ]]; do + kill -0 "${vng_pid}" 2>/dev/null || die "vng exited early; failed to boot VM" + [[ "${elapsed}" -ge "${WAIT_TOTAL}" ]] && die "timed out waiting for VM boot" + sleep 1 + elapsed=$((elapsed + 1)) + done +} + +vm_wait_for_ssh() { + local i=0 + while true; do + vm_ssh -- true && break + i=$((i + 1)) + [[ "${i}" -gt "${WAIT_PERIOD_MAX}" ]] && die "timed out waiting for guest ssh" + sleep "${WAIT_PERIOD}" + done +} + +check_guest_requirements() { + local cfg_ok + cfg_ok="$(vm_ssh -- " \ + grep -q dmem /sys/fs/cgroup/cgroup.controllers && \ + grep -q memory /sys/fs/cgroup/cgroup.controllers; + echo \$? + ")" + [[ "${cfg_ok}" == "0" ]] || die "guest kernel missing CONFIG_CGROUP_DMEM" +} + +setup_guest_dmem_helper() { + local kdir + + vm_ssh -- "mountpoint -q /sys/kernel/debug || \ + mount -t debugfs none /sys/kernel/debug" || true + + # Already available (built-in or loaded). + if vm_ssh -- "[[ -e /sys/kernel/debug/dmem_selftest/charge ]]"; then + echo "dmem_selftest ready" + return 0 + fi + + # Fast path: try installed module. + vm_ssh -- "modprobe -q dmem_selftest 2>/dev/null || true" + if vm_ssh -- "[[ -e /sys/kernel/debug/dmem_selftest/charge ]]"; then + echo "dmem_selftest ready" + return 0 + fi + + # Fallback: build only this module against running guest kernel, + # then insert it. + kdir="$(vm_ssh -- "echo /lib/modules/\$(uname -r)/build")" + if vm_ssh -- "[[ -d '${kdir}' ]]"; then + echo "Building dmem_selftest.ko against running guest kernel..." + vm_ssh -- "make -C '${kdir}' \ + M='${GUEST_TREE}/kernel/cgroup' \ + CONFIG_DMEM_SELFTEST=m modules" + vm_ssh -- "insmod '${GUEST_TREE}/kernel/cgroup/dmem_selftest.ko' \ + 2>/dev/null || modprobe -q dmem_selftest 2>/dev/null || true" + fi + + if vm_ssh -- "[[ -e /sys/kernel/debug/dmem_selftest/charge ]]"; then + echo "dmem_selftest ready" + return 0 + fi + + die "dmem_selftest unavailable (modprobe/build+insmod failed)" +} + +run_test() { + vm_ssh -- "cd '${GUEST_TREE}' && make -C tools/testing/selftests TARGETS=cgroup" + vm_ssh -- "cd '${GUEST_TREE}' && ./tools/testing/selftests/cgroup/test_dmem" +} + +while getopts ":hvq:s" o; do + case "${o}" in + v) VERBOSE=1 ;; + q) QEMU="${OPTARG}" ;; + s) SHELL_MODE=1 ;; + h|*) usage ;; + esac +done + +trap cleanup EXIT + +check_deps +echo "Booting virtme-ng VM..." +vm_start +vm_wait_for_ssh +echo "VM is reachable via SSH." + +if [[ "${SHELL_MODE}" -eq 1 ]]; then + echo "Starting interactive shell in VM. Exit to stop VM." + vm_ssh -t -- "cd '${GUEST_TREE}' && exec bash --noprofile --norc" + exit "${KSFT_PASS}" +fi + +check_guest_requirements +setup_guest_dmem_helper + +echo "Running cgroup/test_dmem in VM..." +run_test +echo "PASS: test_dmem completed" +exit "${KSFT_PASS}"
Signature · 1 line
2.53.0
PATCH v3 4/4 selftests: cgroup: handle vmtest-dmem -b to test locally built kernel
In reply to Albert EsteveFrom: Eric Chanudet <[email protected]>
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 vm_start() passes the local tree to vng --run so the VM boots the freshly built kernel.
--- tools/testing/selftests/cgroup/vmtest-dmem.sh | 35 ++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-)
tools/testing/selftests/cgroup/vmtest-dmem.sh +34 -1
@@ -23,6 +23,7 @@ readonly WAIT_TOTAL=$((WAIT_PERIOD * WAIT_PERIOD_MAX)) readonly QEMU_PIDFILE="$(mktemp /tmp/qemu_dmem_vmtest_XXXX.pid)" readonly QEMU_OPTS=" --pidfile ${QEMU_PIDFILE} " +BUILD=0 QEMU="qemu-system-$(uname -m)" VERBOSE=0 SHELL_MODE=0
@@ -31,6 +32,7 @@ GUEST_TREE="${GUEST_TREE:-$KERNEL_CHECKOUT}" usage() { echo echo "$0 [OPTIONS]" + echo " -b Build kernel from source tree before booting" echo " -q <qemu> QEMU binary/path (default: ${QEMU})" echo " -s Start interactive shell in VM" echo " -v Verbose output (vng boot logs on stdout)"
@@ -72,17 +74,46 @@ check_deps() { done } +handle_build() { + if [[ ! "${BUILD}" -eq 1 ]]; then + return + fi + + if [[ ! -d "${KERNEL_CHECKOUT}" ]]; then + echo "-b requires vmtest-dmem.sh called from the kernel source tree" >&2 + exit 1 + fi + + pushd "${KERNEL_CHECKOUT}" &>/dev/null + + if ! vng --kconfig --config "${SCRIPT_DIR}"/config; then + die "failed to generate .config for kernel source tree (${KERNEL_CHECKOUT})" + fi + + if ! make -j"$(nproc)"; then + die "failed to build kernel from source tree (${KERNEL_CHECKOUT})" + fi + + popd &>/dev/null +} + vm_start() { local logfile=/dev/null local verbose_opt="" + local kernel_opt="" if [[ "${VERBOSE}" -eq 1 ]]; then verbose_opt="--verbose" logfile=/dev/stdout fi + if [[ "${BUILD}" -eq 1 ]]; then + kernel_opt="${KERNEL_CHECKOUT}" + fi + vng \ --run \ + ${kernel_opt} \ ${verbose_opt} \ --qemu-opts="${QEMU_OPTS}" \ --qemu="$(command -v "${QEMU}")" \
@@ -165,10 +196,11 @@ run_test() { vm_ssh -- "cd '${GUEST_TREE}' && ./tools/testing/selftests/cgroup/test_dmem" } -while getopts ":hvq:s" o; do +while getopts ":hvq:sb" o; do case "${o}" in v) VERBOSE=1 ;; q) QEMU="${OPTARG}" ;; + b) BUILD=1 ;; s) SHELL_MODE=1 ;; h|*) usage ;; esac
@@ -177,6 +209,7 @@ done trap cleanup EXIT check_deps +handle_build echo "Booting virtme-ng VM..." vm_start vm_wait_for_ssh
Signature · 1 line
2.53.0