From nobody Thu Apr 2 18:53:51 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DB2F73D1710 for ; Fri, 27 Mar 2026 08:53:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774601616; cv=none; b=VSLCJ7fohK72wTIMMbQmqbssAIr8ZcCthYSaDlI3DssGLYTgHuf/bRY/51KjMuwIO4hcxVLN+X89Mh/EO3HcJw3fVbH49E1mv87LHOlxcxWfqUVXoZ6QokBrRp4tfRqKU1TU9w85LIvoNwrAJmmj5aa/yVYAtADgYexPIdkpQMQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774601616; c=relaxed/simple; bh=f5gpLJhuhPMc/OfaBqrWcgTtw9DqyU8ZF6Loo+xN8os=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=qxBKzjGWwG3YVdHWBv6j/CDfe3vVJAYDO0sAEJtTcihFIaPJ6fM2eM785uSIIYMfe5ZPaq4q+Rw63XaCspk/Ozutg/Bfg6x8Gcdwnbt6vC/LiPYCyskjIhwXjAAmHL01NrU1P/JxOGuwC77xhz9oYXTJ54HBExt9oKY0bOH3tco= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Kbqh4ju0; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Kbqh4ju0" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1774601614; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OXaMsUe2Wvs4qHcv9gv5D1OBCw4At/Fvbrmgty1pEEo=; b=Kbqh4ju0mLxVoc5N9racRTtIfaJWB3/rJnAUFC583C81W8x9NHHYl4g88IuOcvPF6R/9Mi 1nr39MTntzrZtZcNNPKxwuqlXLdPQvZZGwBrfliovCo+sQaLg9R2FHwBzSlyT22YHCP1AO cAtN+XTzx3B9gNv7ruila6bjlTM6f3U= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-372-hkR45RwaPz6DDvLBPbe7Cg-1; Fri, 27 Mar 2026 04:53:30 -0400 X-MC-Unique: hkR45RwaPz6DDvLBPbe7Cg-1 X-Mimecast-MFC-AGG-ID: hkR45RwaPz6DDvLBPbe7Cg_1774601608 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 7DAB71944F05; Fri, 27 Mar 2026 08:53:28 +0000 (UTC) Received: from [192.168.1.153] (unknown [10.44.32.245]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E4F5A1800671; Fri, 27 Mar 2026 08:53:24 +0000 (UTC) From: Albert Esteve Date: Fri, 27 Mar 2026 09:53:03 +0100 Subject: [PATCH 1/3] cgroup: Add dmem_selftest module Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260327-kunit_cgroups-v1-1-971b3c739a00@redhat.com> References: <20260327-kunit_cgroups-v1-0-971b3c739a00@redhat.com> In-Reply-To: <20260327-kunit_cgroups-v1-0-971b3c739a00@redhat.com> To: Tejun Heo , Johannes Weiner , =?utf-8?q?Michal_Koutn=C3=BD?= , Shuah Khan Cc: linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-kselftest@vger.kernel.org, Albert Esteve , mripard@redhat.com, echanude@redhat.com X-Developer-Signature: v=1; a=ed25519-sha256; t=1774601599; l=7324; i=aesteve@redhat.com; s=20260303; h=from:subject:message-id; bh=f5gpLJhuhPMc/OfaBqrWcgTtw9DqyU8ZF6Loo+xN8os=; b=o56Rm205SeHIQa35xicHqeJNIHiAuAWAgZSPXmabK3jiJ6aUEwPfZmLxioo5ops2cFTjMnvmV ofciMuXYTqhAAZkjB2C7M8ts81Xt1a9sWt94TcnRAoOMNFwU8DDYSq+ X-Developer-Key: i=aesteve@redhat.com; a=ed25519; pk=YSFz6sOHd2L45+Fr8DIvHTi6lSIjhLZ5T+rkxspJt1s= X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 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. Signed-off-by: Albert Esteve --- init/Kconfig | 12 +++ kernel/cgroup/Makefile | 1 + kernel/cgroup/dmem_selftest.c | 192 ++++++++++++++++++++++++++++++++++++++= ++++ 3 files changed, 205 insertions(+) diff --git a/init/Kconfig b/init/Kconfig index 444ce811ea674..060ba8ca49333 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1238,6 +1238,18 @@ config CGROUP_DMEM As an example, it allows you to restrict VRAM usage for applications in the DRM subsystem. =20 +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 diff --git a/kernel/cgroup/Makefile b/kernel/cgroup/Makefile index ede31601a363a..febc36e60f9f9 100644 --- a/kernel/cgroup/Makefile +++ b/kernel/cgroup/Makefile @@ -8,4 +8,5 @@ obj-$(CONFIG_CPUSETS) +=3D cpuset.o obj-$(CONFIG_CPUSETS_V1) +=3D cpuset-v1.o obj-$(CONFIG_CGROUP_MISC) +=3D misc.o obj-$(CONFIG_CGROUP_DMEM) +=3D dmem.o +obj-$(CONFIG_DMEM_SELFTEST) +=3D dmem_selftest.o obj-$(CONFIG_CGROUP_DEBUG) +=3D debug.o diff --git a/kernel/cgroup/dmem_selftest.c b/kernel/cgroup/dmem_selftest.c new file mode 100644 index 0000000000000..09df70f718969 --- /dev/null +++ b/kernel/cgroup/dmem_selftest.c @@ -0,0 +1,192 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Kselftest helper for the dmem cgroup controller. + * + * Registers a dmem region and debugfs files so tests can trigger charges + * from the calling task's cgroup. + * + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../../tools/testing/selftests/kselftest_module.h" + +#define DM_SELFTEST_REGION_NAME "dmem_selftest" +#define DM_SELFTEST_REGION_SIZE (256ULL * 1024 * 1024) + +KSTM_MODULE_GLOBALS(); + +static struct dmem_cgroup_region *selftest_region; +static struct dentry *dbg_dir; + +static struct dmem_cgroup_pool_state *charged_pool; +static u64 charged_size; +static DEFINE_MUTEX(charge_lock); + +static ssize_t dmem_selftest_charge_write(struct file *file, const char __= user *user_buf, + size_t count, loff_t *ppos) +{ + struct dmem_cgroup_pool_state *pool =3D NULL, *limit =3D NULL; + u64 size; + char buf[32]; + int ret; + + if (!selftest_region) + return -ENODEV; + + if (count =3D=3D 0 || count >=3D sizeof(buf)) + return -EINVAL; + + if (copy_from_user(buf, user_buf, count)) + return -EFAULT; + buf[count] =3D '\0'; + + ret =3D kstrtou64(strim(buf), 0, &size); + if (ret) + return ret; + if (!size) + return -EINVAL; + + mutex_lock(&charge_lock); + if (charged_pool) { + mutex_unlock(&charge_lock); + return -EBUSY; + } + + ret =3D dmem_cgroup_try_charge(selftest_region, size, &pool, &limit); + if (ret =3D=3D -EAGAIN && limit) + dmem_cgroup_pool_state_put(limit); + if (ret) { + mutex_unlock(&charge_lock); + return ret; + } + + charged_pool =3D pool; + charged_size =3D size; + mutex_unlock(&charge_lock); + + return count; +} + +static ssize_t dmem_selftest_uncharge_write(struct file *file, const char = __user *user_buf, + size_t count, loff_t *ppos) +{ + if (!count) + return -EINVAL; + + mutex_lock(&charge_lock); + if (!charged_pool) { + mutex_unlock(&charge_lock); + return -EINVAL; + } + + dmem_cgroup_uncharge(charged_pool, charged_size); + charged_pool =3D NULL; + charged_size =3D 0; + mutex_unlock(&charge_lock); + + return count; +} + +static const struct file_operations dmem_selftest_charge_fops =3D { + .write =3D dmem_selftest_charge_write, + .llseek =3D noop_llseek, +}; + +static const struct file_operations dmem_selftest_uncharge_fops =3D { + .write =3D dmem_selftest_uncharge_write, + .llseek =3D noop_llseek, +}; + +static int __init dmem_selftest_register(void) +{ + selftest_region =3D dmem_cgroup_register_region( + DM_SELFTEST_REGION_SIZE, DM_SELFTEST_REGION_NAME); + if (IS_ERR(selftest_region)) + return PTR_ERR(selftest_region); + if (!selftest_region) + return -EINVAL; + + dbg_dir =3D debugfs_create_dir("dmem_selftest", NULL); + if (!dbg_dir) { + dmem_cgroup_unregister_region(selftest_region); + selftest_region =3D NULL; + return -ENOMEM; + } + + debugfs_create_file("charge", 0200, dbg_dir, NULL, &dmem_selftest_charge_= fops); + debugfs_create_file("uncharge", 0200, dbg_dir, NULL, &dmem_selftest_uncha= rge_fops); + + pr_info("region '%s' registered; debugfs at dmem_selftest/{charge,uncharg= e}\n", + DM_SELFTEST_REGION_NAME); + return 0; +} + +static void dmem_selftest_remove(void) +{ + debugfs_remove_recursive(dbg_dir); + dbg_dir =3D NULL; + + if (selftest_region) { + dmem_cgroup_unregister_region(selftest_region); + selftest_region =3D NULL; + } +} + +static void __init selftest(void) +{ + KSTM_CHECK_ZERO(!selftest_region); + KSTM_CHECK_ZERO(!dbg_dir); +} + +static int __init dmem_selftest_init(void) +{ + int report_rc; + int err; + + err =3D dmem_selftest_register(); + if (err) + return err; + + pr_info("loaded.\n"); + add_taint(TAINT_TEST, LOCKDEP_STILL_OK); + selftest(); + report_rc =3D kstm_report(total_tests, failed_tests, skipped_tests); + if (report_rc) { + dmem_selftest_remove(); + return report_rc; + } + + return 0; +} + +static void __exit dmem_selftest_exit(void) +{ + pr_info("unloaded.\n"); + + mutex_lock(&charge_lock); + if (charged_pool) { + dmem_cgroup_uncharge(charged_pool, charged_size); + charged_pool =3D NULL; + } + mutex_unlock(&charge_lock); + + dmem_selftest_remove(); +} + +module_init(dmem_selftest_init); +module_exit(dmem_selftest_exit); + +MODULE_AUTHOR("Albert Esteve "); +MODULE_DESCRIPTION("Kselftest helper for cgroup dmem controller"); +MODULE_LICENSE("GPL"); --=20 2.52.0