cgroup: dmem: add selftest helper and coverage
v7 cover cgroup: dmem: add selftest helper and coverage
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.
No reviews were received on the last version. The series is small and self-contained, and was tested by R Nageswara Sastry, so reviewing it should be cheap.
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:
- an out-of-tree dmem_selftest helper with alloc/free module parameters,
- KB() definition next to existing MB()/GB() helpers
- test_dmem for dmem.max enforcement and byte-accurate dmem.current
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 v7:
- Rebased onto v7.4-rc3. No functional changes since v6.
- Link to v6: https://lore.kernel.org/r/[email protected]
Changes in v6:
- Drop virtme-ng wrappers
- Build the helper out of tree, drop CONFIG_DMEM_SELFTEST
- Rename charge/uncharge to alloc/free
- Add KB() in cgroup_util.h
- Use cg_read_key_long(), ARRAY_SIZE(), and KB()/MB() size literals
- Drop test_dmem_min/low
- Check dmem.current for exact equality
- stat() the helper once in main() instead of per-subtest
- Link to v5: https://lore.kernel.org/r/[email protected]
Changes in v5:
- Change parse_first_region() by find_selftest_region to scan
dmem.capacity lines
- Use exact equality for limit readback, not values_close()
- Write VM tempfile to $SCRIPT_DIR
- Fix KTAP by dropping outer framing in test_dmem
- Use array for optional vng args to handle paths with spaces
- Respect O=/KBUILD_OUTPUT= when building, per bpf/vmtest.sh convention
- Link to v4: https://lore.kernel.org/r/[email protected]
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 (3):
selftests: cgroup: Add dmem_selftest helper module
selftests: cgroup: Add KB() helper
selftests: cgroup: Add dmem selftest coverage
tools/testing/selftests/cgroup/.gitignore | 1 + tools/testing/selftests/cgroup/Makefile | 3 + tools/testing/selftests/cgroup/config | 1 + .../selftests/cgroup/lib/include/cgroup_util.h | 1 + tools/testing/selftests/cgroup/test_dmem.c | 314 +++++++++++++++++++++ .../testing/selftests/cgroup/test_modules/Makefile | 17 ++ .../selftests/cgroup/test_modules/dmem_selftest.c | 146 ++++++++++ 7 files changed, 483 insertions(+)
---
base-commit: 666539eecdf4db623f5e3c3e55cf9f2dfad24aa4
change-id: 20260318-kunit_cgroups-7fb0b9e64017
Best regards,
Signature · 1 line
Albert Esteve <[email protected]>
PATCH v7 1/3 selftests: cgroup: Add dmem_selftest helper module
Currently, dmem charging is driver-driven through direct calls to dmem_cgroup_try_charge() (for example TTM). That does not give cgroup selftests a generic way to trigger alloc/free paths from userspace in a chosen cgroup, so coverage of nested dmem.max still needs a synthetic region.
Add tools/testing/selftests/cgroup/test_modules/dmem_selftest.c as an OOT helper, built like other test_modules (no extra Kconfig needed). Tests can insmod the .ko module to register a "dmem_selftest" region.
Module parameters drive a single outstanding allocation against the calling task's cgroup:
/sys/module/dmem_selftest/parameters/alloc
/sys/module/dmem_selftest/parameters/free
Writing a size to alloc triggers dmem_cgroup_try_charge() for the calling task's cgroup. Writing to free with no argument 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.
--- tools/testing/selftests/cgroup/Makefile | 1 + tools/testing/selftests/cgroup/config | 1 + .../testing/selftests/cgroup/test_modules/Makefile | 17 +++ .../selftests/cgroup/test_modules/dmem_selftest.c | 146 +++++++++++++++++++++ 4 files changed, 165 insertions(+)
tools/testing/selftests/cgroup/Makefile +1 -0
@@ -6,6 +6,7 @@ all: ${HELPER_PROGS} TEST_FILES := with_stress.sh TEST_PROGS := test_stress.sh test_cpuset_prs.sh test_cpuset_v1_hp.sh TEST_GEN_FILES := wait_inotify +TEST_GEN_MODS_DIR := test_modules # Keep the lists lexicographically sorted TEST_GEN_PROGS = test_core TEST_GEN_PROGS += test_cpu
tools/testing/selftests/cgroup/config +1 -0
@@ -1,5 +1,6 @@ CONFIG_CGROUPS=y CONFIG_CGROUP_CPUACCT=y +CONFIG_CGROUP_DMEM=y CONFIG_CGROUP_FREEZER=y CONFIG_CGROUP_SCHED=y CONFIG_MEMCG=y
tools/testing/selftests/cgroup/test_modules/Makefile new file +17 -0
@@ -0,0 +1,17 @@ +# SPDX-License-Identifier: GPL-2.0 +TESTMODS_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST))))) +KDIR ?= /lib/modules/$(shell uname -r)/build + +obj-m += dmem_selftest.o + +# Ensure that KDIR exists, otherwise skip the compilation +modules: +ifneq ("$(wildcard $(KDIR))", "") + $(Q)$(MAKE) -C $(KDIR) modules KBUILD_EXTMOD=$(TESTMODS_DIR) +endif + +# Ensure that KDIR exists, otherwise skip the clean target +clean: +ifneq ("$(wildcard $(KDIR))", "") + $(Q)$(MAKE) -C $(KDIR) clean KBUILD_EXTMOD=$(TESTMODS_DIR) +endif
tools/testing/selftests/cgroup/test_modules/dmem_selftest.c new file +146 -0
146 lines not on this page · raw message
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v7 1/3] selftests: cgroup: Add dmem_selftest helper module
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] selftests: cgroup: default KDIR in test_modules/Makefile breaks build - [Medium] selftests…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] selftests: cgroup: default KDIR in test_modules/Makefile breaks build
- [Medium] selftests: cgroup: race on selftest_region in dmem_selftest_exit()
- [Medium] selftests: cgroup: ERR_PTR stored in selftest_region in dmem_selftest_init()
--
commit ff1dfd0d8169df4ccb406a2e9530169608e94814
Author: Albert Esteve <[email protected]>
selftests: cgroup: Add dmem_selftest helper module
This commit adds an out-of-tree dmem_selftest kernel module under tools/testing/selftests/cgroup/test_modules/ that registers a synthetic dmem cgroup region and exposes alloc and free module parameters for selftests.
Quoted 7 lines
tools/testing/selftests/cgroup/test_modules/Makefile new file +1
-0@@ -0,0 +1,17 @@ +# SPDX-License-Identifier: GPL-2.0
tools/testing/selftests/cgroup/test_modules/Makefile line 11
Quoted 10 lines
tools/testing/selftests/cgroup/test_modules/Makefile new file +10
-0+TESTMODS_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST))))) +KDIR ?= /lib/modules/$(shell uname -r)/build + +obj-m += dmem_selftest.o + +# Ensure that KDIR exists, otherwise skip the compilation +modules: +ifneq ("$(wildcard $(KDIR))", "") + $(Q)$(MAKE) -C $(KDIR) modules KBUILD_EXTMOD=$(TESTMODS_DIR) +endif
[Severity: Medium]
Does defaulting KDIR to /lib/modules/$(shell uname -r)/build break the cgroup selftest build when the host's running kernel headers are older than the target kernel tree or when cross-compiling?
Because tools/testing/selftests/cgroup/Makefile sets TEST_GEN_MODS_DIR, the default all target invokes this Makefile unconditionally. When /lib/modules/$(uname -r)/build exists for an older host kernel that lacks linux/cgroup_dmem.h or struct dmem_cgroup_init, Kbuild compiles dmem_selftest.c against the host kernel headers instead of the tree under test and fails the build. In addition, checking $(wildcard $(KDIR)) without checking $(wildcard $(KDIR)/Module.symvers) does not verify that the kernel tree has been built.
Defaulting KDIR to $(if $(O),$(O),$(realpath ../../../../..)) and checking $(wildcard $(KDIR)/Module.symvers), as in tools/testing/selftests/sched_ext/test_modules/Makefile, would build against the target kernel tree when available.
[ ... ]
Quoted 6 lines
tools/testing/selftests/cgroup/test_modules/dmem_selftest.c new file +0
-0@@ -0,0 +1,146 @@
[ ... ]
Quoted 39 lines
tools/testing/selftests/cgroup/test_modules/dmem_selftest.c new file +39
-0+static struct dmem_cgroup_region *selftest_region; +static struct dmem_cgroup_pool_state *alloc_pool; +static u64 alloc_size; +static DEFINE_MUTEX (alloc_lock); + +static int param_set_alloc (const char *val,const struct kernel_param *kp) +{ +struct dmem_cgroup_pool_state *pool =NULL , *limit =NULL ; +u64 size; +int ret; + +if (!selftest_region) +return -ENODEV ; + + ret =kstrtou64 (val,0 , &size); +if (ret) +return ret; +if (!size || size >DM_SELFTEST_REGION_SIZE ) +return -EINVAL ; + +mutex_lock (&alloc_lock); +if (alloc_pool) { +mutex_unlock (&alloc_lock); +return -EBUSY ; + } + + ret =dmem_cgroup_try_charge (selftest_region, size, &pool, &limit); +if (ret == -EAGAIN && limit) +dmem_cgroup_pool_state_put (limit); +if (ret) { +mutex_unlock (&alloc_lock); +return ret; + } + + alloc_pool = pool; + alloc_size = size; +mutex_unlock (&alloc_lock); +return 0 ; +}
[ ... ]
tools/testing/selftests/cgroup/test_modules/dmem_selftest.c line 113
Quoted 9 lines
tools/testing/selftests/cgroup/test_modules/dmem_selftest.c new file +9
-0+static int __init dmem_selftest_init (void ) +{ +static const struct dmem_cgroup_init init = { + .size =DM_SELFTEST_REGION_SIZE , + }; + + selftest_region =dmem_cgroup_register_region (&init,DM_SELFTEST_REGION_NAME ); +if (IS_ERR (selftest_region)) +return PTR_ERR (selftest_region);
[Severity: Medium]
If dmem_cgroup_register_region() returns an error pointer, can a concurrent write to the alloc parameter dereference that ERR_PTR in dmem_cgroup_try_charge()?
Module parameter sysfs attributes are created by mod_sysfs_setup() before dmem_selftest_init() runs and remain accessible until do_init_module() tears them down on error. Storing the return value directly into selftest_region leaves a non-NULL ERR_PTR(-ENOMEM) visible to param_set_alloc(), which only checks !selftest_region before passing it to dmem_cgroup_try_charge() -> get_cg_pool_unlocked(). Checking the return value in a local variable before assigning selftest_region avoids exposing the error pointer.
[ ... ]
Quoted 6 lines
tools/testing/selftests/cgroup/test_modules/dmem_selftest.c new file +6
-0+static void __exit dmem_selftest_exit(void) +{ +mutex_lock (&alloc_lock); +if (alloc_pool) { +dmem_cgroup_uncharge (alloc_pool, alloc_size); + alloc_pool =NULL ;
tools/testing/selftests/cgroup/test_modules/dmem_selftest.c line 138
Quoted 10 lines
tools/testing/selftests/cgroup/test_modules/dmem_selftest.c new file +10
-0+ alloc_size =0 ; + } +mutex_unlock (&alloc_lock); + +if (selftest_region) { +dmem_cgroup_unregister_region (selftest_region); + selftest_region =NULL ; + } +pr_info ("unloaded.\n" ); +}
[Severity: Medium]
Can param_set_alloc() race with dmem_selftest_exit() over selftest_region and alloc_pool?
In delete_module(), dmem_selftest_exit() runs before free_module() removes the module parameter sysfs files, and param_attr_store() does not hold a module reference:
CPU0 CPU1param_set_alloc() dmem_selftest_exit()
if (!selftest_region)
mutex_lock(&alloc_lock)
...
mutex_unlock(&alloc_lock) dmem_cgroup_unregister_region(...) selftest_region = NULL
mutex_lock(&alloc_lock) dmem_cgroup_try_charge(selftest_region, ...)
get_cg_pool_unlocked(NULL, ...) <-- NULL pointer dereference
If CPU0 instead acquires alloc_lock right after CPU1 unlocks it and before dmem_cgroup_unregister_region() marks the region unregistered, dmem_cgroup_try_charge() succeeds and sets alloc_pool after dmem_selftest_exit() has already finished uncharging alloc_pool, leaking the charge, pool state, and cgroup CSS reference.
Checking selftest_region under alloc_lock in param_set_alloc() and detaching selftest_region under the same lock in dmem_selftest_exit() before unregistering the region would close both races.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v7 2/3 selftests: cgroup: Add KB() helper
In reply to Albert Estevecgroup_util.h already has MB() and GB() for size literals. Add KB() in the same style so tests can spell kilobyte values without in-code shifts.
--- tools/testing/selftests/cgroup/lib/include/cgroup_util.h | 1 + 1 file changed, 1 insertion(+)
tools/testing/selftests/cgroup/lib/include/cgroup_util.h +1 -0
@@ -6,6 +6,7 @@#define BUF_SIZE 4096#endif +#define KB (x) (x << 10)#define MB (x) (x << 20)#define GB (x) ((unsigned long long)(x) << 30)
Signature · 1 line
2.55.0
PATCH v7 3/3 selftests: cgroup: Add dmem selftest coverage
In reply to Albert EsteveCurrently, tools/testing/selftests/cgroup/ has no dmem-specific test. This leaves dmem accounting and dmem.max enforcement largely unvalidated without a production driver.
Add test_dmem, driven by the dmem_selftest helper module parameters (alloc/free) on the dmem_selftest region. insmod the helper before running the tests.
The tests cover:
- test_dmem_max: nested hierarchy with per-leaf dmem.max values and verifies that over-limit charges fail while in-limit charges succeed in dmem.current.
- test_dmem_alloc_byte_granularity: non-page-aligned alloc sizes, dmem.current equals the requested byte count, and free-to-zero behavior.
Limits are read with cg_read_key_long(). The helper is stat'd in main() so a missing module skips the whole test instead of each subtest.
--- tools/testing/selftests/cgroup/.gitignore | 1 + tools/testing/selftests/cgroup/Makefile | 2 + tools/testing/selftests/cgroup/test_dmem.c | 314 +++++++++++++++++++++++++++++ 3 files changed, 317 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
@@ -11,6 +11,7 @@ TEST_GEN_MODS_DIR := test_modules 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
@@ -27,6 +28,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/test_dmem.c new file +314 -0
314 lines not on this page · raw message
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v7 3/3] selftests: cgroup: Add dmem selftest coverage
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] selftests: cgroup: test_dmem always skips under kselftest runner --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] selftests: cgroup: test_dmem always skips under kselftest runner
--
commit b1601ac573b3434d7a724ddf07cd272e92c72579
Author: Albert Esteve <[email protected]>
selftests: cgroup: Add dmem selftest coverage
This commit adds the test_dmem selftest to verify dmem.max enforcement and byte-granularity accounting using the dmem_selftest kernel module.
Quoted 12 lines
tools/testing/selftests/cgroup/Makefile +1
-0@@ -11,6 +11,7 @@ TEST_GEN_MODS_DIR := test_modules 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
[ ... ]
Quoted 5 lines
tools/testing/selftests/cgroup/test_dmem.c new file +0
-0
[ ... ]
Quoted 11 lines
tools/testing/selftests/cgroup/test_dmem.c new file +11
-0+int main (int argc,char **argv) +{ +char root[PATH_MAX ]; +struct stat st; +int i; + +ksft_print_header (); + +if (cg_find_unified_root (root,sizeof (root),NULL )) +ksft_exit_skip ("cgroup v2 isn't mounted\n" ); +
tools/testing/selftests/cgroup/test_dmem.c line 295
Quoted 10 lines
tools/testing/selftests/cgroup/test_dmem.c new file +10
-0+if (cg_read_strstr (root,"cgroup.controllers" ,"dmem" )) +ksft_exit_skip ("dmem controller isn't available (CONFIG_CGROUP_DMEM?)\n" ); + +if (cg_read_strstr (root,"cgroup.subtree_control" ,"dmem" )) +if (cg_write (root,"cgroup.subtree_control" ,"+dmem" )) +ksft_exit_skip ("Failed to enable dmem controller\n" ); + +if (stat (DM_SELFTEST_ALLOC , &st) <0 ) +ksft_exit_skip ( +"dmem_selftest helper not loaded (insmod test_modules/dmem_selftest.ko)\n" );
[Severity: Medium]
Will test_dmem always be skipped when run via the standard kselftest runner (make run_tests or run_kselftest.sh)?
The kselftest framework builds and installs modules in TEST_GEN_MODS_DIR, but runner.sh executes TEST_GEN_PROGS directly without loading those modules first. Unless test_dmem loads and unloads test_modules/dmem_selftest.ko itself (for example via finit_module() and delete_module()) or is invoked through a wrapper script in TEST_PROGS, automated kselftest runs will never execute these tests.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3