From: Albert Esteve <aesteve@redhat.com>
To: "Tejun Heo" <tj@kernel.org>,
"Johannes Weiner" <hannes@cmpxchg.org>,
"Michal Koutný" <mkoutny@suse.com>,
"Shuah Khan" <shuah@kernel.org>
Cc: linux-kernel@vger.kernel.org, cgroups@vger.kernel.org,
linux-kselftest@vger.kernel.org,
Albert Esteve <aesteve@redhat.com>
Subject: [PATCH v6 1/3] selftests: cgroup: Add dmem_selftest helper module
Date: Mon, 31 Aug 2026 15:25:11 +0200 [thread overview]
Message-ID: <20260831-kunit_cgroups-v6-1-63b6c7695287@redhat.com> (raw)
In-Reply-To: <20260831-kunit_cgroups-v6-0-63b6c7695287@redhat.com>
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.
Signed-off-by: Albert Esteve <aesteve@redhat.com>
---
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(+)
diff --git a/tools/testing/selftests/cgroup/Makefile b/tools/testing/selftests/cgroup/Makefile
index e01584c2189a..a08661320a07 100644
--- a/tools/testing/selftests/cgroup/Makefile
+++ b/tools/testing/selftests/cgroup/Makefile
@@ -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
diff --git a/tools/testing/selftests/cgroup/config b/tools/testing/selftests/cgroup/config
index 39f979690dd3..2ee0488c3d65 100644
--- a/tools/testing/selftests/cgroup/config
+++ b/tools/testing/selftests/cgroup/config
@@ -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
diff --git a/tools/testing/selftests/cgroup/test_modules/Makefile b/tools/testing/selftests/cgroup/test_modules/Makefile
new file mode 100644
index 000000000000..dd8a17313cec
--- /dev/null
+++ b/tools/testing/selftests/cgroup/test_modules/Makefile
@@ -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
diff --git a/tools/testing/selftests/cgroup/test_modules/dmem_selftest.c b/tools/testing/selftests/cgroup/test_modules/dmem_selftest.c
new file mode 100644
index 000000000000..a763a156f13f
--- /dev/null
+++ b/tools/testing/selftests/cgroup/test_modules/dmem_selftest.c
@@ -0,0 +1,146 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Kselftest helper for the dmem cgroup controller.
+ *
+ * Registers a synthetic dmem region so tests can trigger allocations
+ * from the calling task's cgroup via module parameters:
+ * /sys/module/dmem_selftest/parameters/alloc
+ * /sys/module/dmem_selftest/parameters/free
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/cgroup_dmem.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/moduleparam.h>
+#include <linux/mutex.h>
+#include <linux/panic.h>
+#include <linux/sysfs.h>
+
+#define DM_SELFTEST_REGION_NAME "dmem_selftest"
+#define DM_SELFTEST_REGION_SIZE (256ULL * 1024 * 1024)
+
+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;
+}
+
+static int param_get_alloc(char *buffer, const struct kernel_param *kp)
+{
+ u64 size;
+
+ mutex_lock(&alloc_lock);
+ size = alloc_size;
+ mutex_unlock(&alloc_lock);
+ return sysfs_emit(buffer, "%llu\n", size);
+}
+
+static const struct kernel_param_ops alloc_ops = {
+ .set = param_set_alloc,
+ .get = param_get_alloc,
+};
+
+module_param_cb(alloc, &alloc_ops, NULL, 0644);
+MODULE_PARM_DESC(alloc, "Allocate (charge) SIZE bytes against the calling task's dmem cgroup");
+
+static int param_set_free(const char *val, const struct kernel_param *kp)
+{
+ mutex_lock(&alloc_lock);
+ if (!alloc_pool) {
+ mutex_unlock(&alloc_lock);
+ return -EINVAL;
+ }
+
+ dmem_cgroup_uncharge(alloc_pool, alloc_size);
+ alloc_pool = NULL;
+ alloc_size = 0;
+ mutex_unlock(&alloc_lock);
+ return 0;
+}
+
+static const struct kernel_param_ops free_ops = {
+ .flags = KERNEL_PARAM_OPS_FL_NOARG,
+ .set = param_set_free,
+};
+
+module_param_cb(free, &free_ops, NULL, 0200);
+MODULE_PARM_DESC(free, "Free the outstanding dmem selftest allocation");
+
+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);
+ if (!selftest_region)
+ return -EINVAL;
+
+ add_taint(TAINT_TEST, LOCKDEP_STILL_OK);
+ pr_info("region '%s' registered; parameters alloc/free\n",
+ DM_SELFTEST_REGION_NAME);
+ return 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;
+ alloc_size = 0;
+ }
+ mutex_unlock(&alloc_lock);
+
+ if (selftest_region) {
+ dmem_cgroup_unregister_region(selftest_region);
+ selftest_region = NULL;
+ }
+ pr_info("unloaded.\n");
+}
+
+module_init(dmem_selftest_init);
+module_exit(dmem_selftest_exit);
+
+MODULE_AUTHOR("Albert Esteve <aesteve@redhat.com>");
+MODULE_DESCRIPTION("Kselftest helper for cgroup dmem controller");
+MODULE_LICENSE("GPL");
+MODULE_INFO(test, "Y");
--
2.55.0
next prev parent reply other threads:[~2026-08-31 13:25 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-31 13:25 [PATCH v6 0/3] cgroup: dmem: add selftest helper and coverage Albert Esteve
2026-08-31 13:25 ` Albert Esteve [this message]
2026-08-31 13:25 ` [PATCH v6 2/3] selftests: cgroup: Add KB() helper Albert Esteve
2026-08-31 13:25 ` [PATCH v6 3/3] selftests: cgroup: Add dmem selftest coverage Albert Esteve
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=20260831-kunit_cgroups-v6-1-63b6c7695287@redhat.com \
--to=aesteve@redhat.com \
--cc=cgroups@vger.kernel.org \
--cc=hannes@cmpxchg.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=mkoutny@suse.com \
--cc=shuah@kernel.org \
--cc=tj@kernel.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox