From nobody Sat Sep 26 17:09:07 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 20214432BC2 for ; Mon, 31 Aug 2026 13:25:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788182743; cv=none; b=AoNKf0SLTHO+oWf+tLNMRgbc1KuAbKSQtAZ1QqQYUKTWUoooBQ7/8hh6XbPIasNJGxn9VYG6mQUZFO9h3zd51NtiL4BpFhf1EPpBQPrYMDOJ3gchgAVU0Z5YyfK04+Kr25LmVDD3rjUa2yZqTKuRGbtXFbOEuLQ3EgeKQFtBGj8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788182743; c=relaxed/simple; bh=3+VoVKeZv3gsnQJua4pDGn0oOb/kZqlkTYmTWEKGOg4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Ik0rdW++KqvpGirujBI2P9clfN1aOhO0lesrKnH4oSqo84huBXzY0UE9gFOhRYmJB49QCCpJAK27gWl1As0MTKpcN8J0jlbuCl98KvaLNngW/rFW/iTBMgvdQRff4QH3grRep15px9I14uayvJcOfL9ehFhxQk8zcoH8A9KmvFs= 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=A9ILomLB; arc=none smtp.client-ip=170.10.129.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="A9ILomLB" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1788182739; 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=obgsA+wmkoM83ZDQQaFWaWOWiW1c97++AGpx79Oxzms=; b=A9ILomLBmtO8DToLEbBnelvcnwlUSSa3i87w0NvWHLFsKyqEg2AgJbNW0Y63QWvRtm1NSO CjD7WM0qhoLqF46Qfq00Uz8rpl7KDfDkJ88NsE317IeDQHAvn1zb3xDrSo0ej3mJhTHwzT Uqek2mWWyMGkmcwoRZoEq2gMAO4ZADQ= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-97-bT7zN3AnMdqPTgtdrhG5Bw-1; Mon, 31 Aug 2026 09:25:35 -0400 X-MC-Unique: bT7zN3AnMdqPTgtdrhG5Bw-1 X-Mimecast-MFC-AGG-ID: bT7zN3AnMdqPTgtdrhG5Bw_1788182734 Received: from mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.95]) (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-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E093D190B693; Mon, 31 Aug 2026 13:25:19 +0000 (UTC) Received: from aesteve-thinkpadp1gen8.rmtes.csb (headnet03.pony-001.prod.iad2.dc.redhat.com [10.2.32.114]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3DFA440E; Mon, 31 Aug 2026 13:25:18 +0000 (UTC) From: Albert Esteve Date: Mon, 31 Aug 2026 15:25:11 +0200 Subject: [PATCH v6 1/3] selftests: cgroup: Add dmem_selftest helper 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: <20260831-kunit_cgroups-v6-1-63b6c7695287@redhat.com> References: <20260831-kunit_cgroups-v6-0-63b6c7695287@redhat.com> In-Reply-To: <20260831-kunit_cgroups-v6-0-63b6c7695287@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 X-Developer-Signature: v=1; a=ed25519-sha256; t=1788182715; l=7299; i=aesteve@redhat.com; s=20260827; h=from:subject:message-id; bh=3+VoVKeZv3gsnQJua4pDGn0oOb/kZqlkTYmTWEKGOg4=; b=vzcM11inmXYQJkP5+sVqbEHqX+yOkuj7JFbrCgN1VQ40GNiWP1dcwgS+MsRWbvpQhk6yEeQP0 VUvNrwb8SYICR+1Zf98G68PvpIDkyeOQs/ssTUa7l9E33T/oGqjJM04 X-Developer-Key: i=aesteve@redhat.com; a=ed25519; pk=zI2RBeZgocwYm3AkJW4CtjoJnKoDZrfBl1xe5Ed6h+U= X-Scanned-By: MIMEDefang 3.6 on 10.30.177.95 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 Tested-by: R Nageswara Sastry --- 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/selfte= sts/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 :=3D with_stress.sh TEST_PROGS :=3D test_stress.sh test_cpuset_prs.sh test_cpuset_v1_hp.sh TEST_GEN_FILES :=3D wait_inotify +TEST_GEN_MODS_DIR :=3D test_modules # Keep the lists lexicographically sorted TEST_GEN_PROGS =3D test_core TEST_GEN_PROGS +=3D test_cpu diff --git a/tools/testing/selftests/cgroup/config b/tools/testing/selftest= s/cgroup/config index 39f979690dd3..2ee0488c3d65 100644 --- a/tools/testing/selftests/cgroup/config +++ b/tools/testing/selftests/cgroup/config @@ -1,5 +1,6 @@ CONFIG_CGROUPS=3Dy CONFIG_CGROUP_CPUACCT=3Dy +CONFIG_CGROUP_DMEM=3Dy CONFIG_CGROUP_FREEZER=3Dy CONFIG_CGROUP_SCHED=3Dy CONFIG_MEMCG=3Dy diff --git a/tools/testing/selftests/cgroup/test_modules/Makefile b/tools/t= esting/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 :=3D $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST)))= )) +KDIR ?=3D /lib/modules/$(shell uname -r)/build + +obj-m +=3D dmem_selftest.o + +# Ensure that KDIR exists, otherwise skip the compilation +modules: +ifneq ("$(wildcard $(KDIR))", "") + $(Q)$(MAKE) -C $(KDIR) modules KBUILD_EXTMOD=3D$(TESTMODS_DIR) +endif + +# Ensure that KDIR exists, otherwise skip the clean target +clean: +ifneq ("$(wildcard $(KDIR))", "") + $(Q)$(MAKE) -C $(KDIR) clean KBUILD_EXTMOD=3D$(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 +#include +#include +#include +#include +#include +#include + +#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 =3D NULL, *limit =3D NULL; + u64 size; + int ret; + + if (!selftest_region) + return -ENODEV; + + ret =3D 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 =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(&alloc_lock); + return ret; + } + + alloc_pool =3D pool; + alloc_size =3D 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 =3D alloc_size; + mutex_unlock(&alloc_lock); + return sysfs_emit(buffer, "%llu\n", size); +} + +static const struct kernel_param_ops alloc_ops =3D { + .set =3D param_set_alloc, + .get =3D 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 =3D NULL; + alloc_size =3D 0; + mutex_unlock(&alloc_lock); + return 0; +} + +static const struct kernel_param_ops free_ops =3D { + .flags =3D KERNEL_PARAM_OPS_FL_NOARG, + .set =3D 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 =3D { + .size =3D DM_SELFTEST_REGION_SIZE, + }; + + selftest_region =3D 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 =3D NULL; + alloc_size =3D 0; + } + mutex_unlock(&alloc_lock); + + if (selftest_region) { + dmem_cgroup_unregister_region(selftest_region); + selftest_region =3D NULL; + } + pr_info("unloaded.\n"); +} + +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"); +MODULE_INFO(test, "Y"); --=20 2.55.0 From nobody Sat Sep 26 17:09:07 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 272EB432BD3 for ; Mon, 31 Aug 2026 13:25:41 +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=1788182742; cv=none; b=YmnbeaUd8WPaA4xHAOSXkEFtv2bZJL9zc51mCVEoNzZVk3CwpSIEwL3g8kkvoTToXn6VElFvYYlOO4kG06ycgwWnxKNZVConfqnkWiRI5iPnjG3LpeT8TadD65wQwSfFvpVIl/l8bT3zKhuyaxwJSQW5BvzIEwCdehrddxlNSeo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788182742; c=relaxed/simple; bh=PE4JPXxCMmTovVJCHys+isKwpKyed0ZqK7DIvREANXQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=mHoKwJdroy0C2q8WQHdx4vl2Hr1GRzDwDMavEi+UrlR11p80LMx787MrFeNWfjv5DwfP0iuKe9Mc7d2w6ugC6jamcyViLeH1M7KxF6qDCIivQgRfDjn2QTNpspAbshvGMvCBglF/4rRcXDwMv5YN3pBfYGe3k6o2AVflZ/SFBgs= 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=MMKBvvdP; 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="MMKBvvdP" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1788182740; 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=ykDFvI3Z1xGBk6sVr4D5Kbs14PCVANsX2P0KGcoLyiA=; b=MMKBvvdPuIspuYf5JBZT4WVH5+sd7FeVwMrjuF43FDADLo5wwj5OKqEawmEgX4N7LDAj72 DSFGqjoEyoZQU2+jUQogka9cNvJqjHPNmgsPraHVsqZ5G6Yryk7Tp0014PlZjqbgexXdwB geL1puocvOiYRV8Oie8osLQs+ueZvEI= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-310-dS4ZbrEXMQez5N6CxENTiA-1; Mon, 31 Aug 2026 09:25:34 -0400 X-MC-Unique: dS4ZbrEXMQez5N6CxENTiA-1 X-Mimecast-MFC-AGG-ID: dS4ZbrEXMQez5N6CxENTiA_1788182733 Received: from mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.95]) (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-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 54A7F18A3C09; Mon, 31 Aug 2026 13:25:22 +0000 (UTC) Received: from aesteve-thinkpadp1gen8.rmtes.csb (headnet03.pony-001.prod.iad2.dc.redhat.com [10.2.32.114]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 4D54A40E; Mon, 31 Aug 2026 13:25:20 +0000 (UTC) From: Albert Esteve Date: Mon, 31 Aug 2026 15:25:12 +0200 Subject: [PATCH v6 2/3] selftests: cgroup: Add KB() helper 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: <20260831-kunit_cgroups-v6-2-63b6c7695287@redhat.com> References: <20260831-kunit_cgroups-v6-0-63b6c7695287@redhat.com> In-Reply-To: <20260831-kunit_cgroups-v6-0-63b6c7695287@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 X-Developer-Signature: v=1; a=ed25519-sha256; t=1788182715; l=776; i=aesteve@redhat.com; s=20260827; h=from:subject:message-id; bh=PE4JPXxCMmTovVJCHys+isKwpKyed0ZqK7DIvREANXQ=; b=215S8Y2djG8kZm2WkG+9TfxCaGvQGMCtOyYrmzkgu0AlJ+nyvuiaAFSJ8k2I9i9JZjOOU80GG XDgaD8gFoARAaTw6i4rK6+yCxtF9j0g4UVyDsV0pfEDRNujuSB9a3O5 X-Developer-Key: i=aesteve@redhat.com; a=ed25519; pk=zI2RBeZgocwYm3AkJW4CtjoJnKoDZrfBl1xe5Ed6h+U= X-Scanned-By: MIMEDefang 3.6 on 10.30.177.95 cgroup_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. Signed-off-by: Albert Esteve Tested-by: R Nageswara Sastry --- tools/testing/selftests/cgroup/lib/include/cgroup_util.h | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/testing/selftests/cgroup/lib/include/cgroup_util.h b/too= ls/testing/selftests/cgroup/lib/include/cgroup_util.h index 5d39c709ac7a..0e5d7b87ad19 100644 --- a/tools/testing/selftests/cgroup/lib/include/cgroup_util.h +++ b/tools/testing/selftests/cgroup/lib/include/cgroup_util.h @@ -6,6 +6,7 @@ #define BUF_SIZE 4096 #endif =20 +#define KB(x) (x << 10) #define MB(x) (x << 20) #define GB(x) ((unsigned long long)(x) << 30) =20 --=20 2.55.0 From nobody Sat Sep 26 17:09:07 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 A6A664322EC for ; Mon, 31 Aug 2026 13:25:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788182743; cv=none; b=hukyquJi0ecO4kUH9QcO1l37WK5wIkIGJL65Eowlj07+dIQWGiHlDZ6VB/WugTNkB28Zlqp8MHXe0Du002pd1mLhEL0+/VOblP0jjl179ugc9w/qlr60IG9hVqRyWGEbjCaQIrj41cFFnOg1x9ZuR+3Ugk6Cuza7yYh6bVmYNVM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788182743; c=relaxed/simple; bh=8Kr2YZNNSN192SnAtC2dKJKhWLemLjQtnhC6rdjd7ao=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=HDSPka7JjnuEuLQs6n3L9DDSrNQCsCgrTpD2qD8ykjfdkvE6vdAQBWfdahteQke+YRHV8dXG7tA8wVqevMMc9FrH/d5p88zc8xhUX54bJJtxE4RlYTorJ7aUMUynQO7xVYuWzyRVohHyOQfrX6hEd+/HA3kES510BUrgJ88+LPY= 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=Nj5v3fDj; arc=none smtp.client-ip=170.10.129.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="Nj5v3fDj" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1788182739; 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=3fAt4QX4GgLPNS42ubuGBQ665ezuuMkEG/rcgHIK/9M=; b=Nj5v3fDjUKHyhlbib28D7K0OO9tBOPgF6pn01fx0iPZ8rGRF+VTYfG7TCW0yNVAkDVGKj1 CQ1CMJztFnQrAAUqMzACMWZjUH72LazqgtyXDXq9QmVLQCkqtsLxTx8D/YGFyytxvQF1fQ +Oa5TvexjsoS6Kv/IefsYLDtlFgJtDg= Received: from mx-prod-mc-01.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-227-wtKlriVgO4uE57ic7aKn9Q-1; Mon, 31 Aug 2026 09:25:35 -0400 X-MC-Unique: wtKlriVgO4uE57ic7aKn9Q-1 X-Mimecast-MFC-AGG-ID: wtKlriVgO4uE57ic7aKn9Q_1788182733 Received: from mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.95]) (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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id BB3C61945CBE; Mon, 31 Aug 2026 13:25:24 +0000 (UTC) Received: from aesteve-thinkpadp1gen8.rmtes.csb (headnet03.pony-001.prod.iad2.dc.redhat.com [10.2.32.114]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id AC70840E; Mon, 31 Aug 2026 13:25:22 +0000 (UTC) From: Albert Esteve Date: Mon, 31 Aug 2026 15:25:13 +0200 Subject: [PATCH v6 3/3] selftests: cgroup: Add dmem selftest coverage 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: <20260831-kunit_cgroups-v6-3-63b6c7695287@redhat.com> References: <20260831-kunit_cgroups-v6-0-63b6c7695287@redhat.com> In-Reply-To: <20260831-kunit_cgroups-v6-0-63b6c7695287@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 X-Developer-Signature: v=1; a=ed25519-sha256; t=1788182715; l=10309; i=aesteve@redhat.com; s=20260827; h=from:subject:message-id; bh=8Kr2YZNNSN192SnAtC2dKJKhWLemLjQtnhC6rdjd7ao=; b=QN0ZJ18d5Obu5q1QBi7Es7NtzGNR4ZIUKCF682UWwvKNZA7VX9ER9Zyw/+D6S0KqRrn6EWJFx lzOOjLFgko8AnmHLAK5wF8aT9/bIDTPJPKguL/zs+Keucprco+ajnsw X-Developer-Key: i=aesteve@redhat.com; a=ed25519; pk=zI2RBeZgocwYm3AkJW4CtjoJnKoDZrfBl1xe5Ed6h+U= X-Scanned-By: MIMEDefang 3.6 on 10.30.177.95 Currently, 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. Signed-off-by: Albert Esteve Tested-by: R Nageswara Sastry --- 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(+) diff --git a/tools/testing/selftests/cgroup/.gitignore b/tools/testing/self= tests/cgroup/.gitignore index 952e4448bf07..ea2322598217 100644 --- a/tools/testing/selftests/cgroup/.gitignore +++ b/tools/testing/selftests/cgroup/.gitignore @@ -2,6 +2,7 @@ test_core test_cpu test_cpuset +test_dmem test_freezer test_hugetlb_memcg test_kill diff --git a/tools/testing/selftests/cgroup/Makefile b/tools/testing/selfte= sts/cgroup/Makefile index a08661320a07..d01e8eb1ead2 100644 --- a/tools/testing/selftests/cgroup/Makefile +++ b/tools/testing/selftests/cgroup/Makefile @@ -11,6 +11,7 @@ TEST_GEN_MODS_DIR :=3D test_modules TEST_GEN_PROGS =3D test_core TEST_GEN_PROGS +=3D test_cpu TEST_GEN_PROGS +=3D test_cpuset +TEST_GEN_PROGS +=3D test_dmem TEST_GEN_PROGS +=3D test_freezer TEST_GEN_PROGS +=3D test_hugetlb_memcg TEST_GEN_PROGS +=3D 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) diff --git a/tools/testing/selftests/cgroup/test_dmem.c b/tools/testing/sel= ftests/cgroup/test_dmem.c new file mode 100644 index 000000000000..3793664678b9 --- /dev/null +++ b/tools/testing/selftests/cgroup/test_dmem.c @@ -0,0 +1,314 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Test the dmem (device memory) cgroup controller. + * + * Depends on the dmem_selftest helper module + * (tools/testing/selftests/cgroup/test_modules/dmem_selftest.c). + */ + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include + +#include "kselftest.h" +#include "cgroup_util.h" + +#define DM_SELFTEST_REGION "dmem_selftest" +#define DM_SELFTEST_ALLOC "/sys/module/dmem_selftest/parameters/alloc" +#define DM_SELFTEST_FREE "/sys/module/dmem_selftest/parameters/free" + +static long dmem_read_limit(const char *cgroup, const char *ctrl) +{ + return cg_read_key_long(cgroup, ctrl, DM_SELFTEST_REGION " "); +} + +static int dmem_write_limit(const char *cgroup, const char *ctrl, long val) +{ + char wr[64]; + + snprintf(wr, sizeof(wr), "%s %ld", DM_SELFTEST_REGION, val); + return cg_write(cgroup, ctrl, wr); +} + +static int dmem_selftest_alloc(long bytes) +{ + char wr[32]; + + snprintf(wr, sizeof(wr), "%ld", bytes); + return write_text(DM_SELFTEST_ALLOC, wr, strlen(wr)); +} + +static int dmem_selftest_free(void) +{ + char wr[] =3D "1"; + + return write_text(DM_SELFTEST_FREE, wr, strlen(wr)); +} + +/* + * First, this test creates the following hierarchy: + * A + * A/B dmem.max=3D8M + * A/B/C dmem.max=3D2M + * A/B/D dmem.max=3D1M + * A/B/E dmem.max=3D75K + * A/B/F dmem.max=3D25K + * A/B/G dmem.max=3D8K + * A/B/H dmem.max=3D0 + * + * Then for each leaf cgroup it tries to alloc above dmem.max + * and expects the request to fail and dmem.current to remain + * unchanged. + * + * For leaves with non-zero dmem.max, it additionally allocs a + * smaller amount and verifies dmem.current matches the requested + * size exactly (the controller accounts in bytes, with no page + * rounding), then frees and verifies dmem.current returns + * to the previous value. + */ +static int test_dmem_max(const char *root) +{ + static const long leaf_max[] =3D { + MB(2), MB(1), KB(75), KB(25), KB(8), 0 + }; + static const long pass_sz[] =3D { + MB(1), MB(1), KB(4), KB(4), KB(4), 0 + }; + char *parent[2] =3D {NULL}; + char *children[ARRAY_SIZE(leaf_max)] =3D {NULL}; + + _Static_assert(ARRAY_SIZE(pass_sz) =3D=3D ARRAY_SIZE(leaf_max), + "pass_sz doesn't match leaf_max length"); + long cur_before, cur_after; + int ret =3D KSFT_FAIL; + int charged =3D 0; + int in_child =3D 0; + long v; + int i; + + parent[0] =3D cg_name(root, "dmem_prot_0"); + if (!parent[0]) + goto cleanup; + + parent[1] =3D cg_name(parent[0], "dmem_prot_1"); + if (!parent[1]) + goto cleanup; + + if (cg_create(parent[0])) + goto cleanup; + + if (cg_write(parent[0], "cgroup.subtree_control", "+dmem")) + goto cleanup; + + if (cg_create(parent[1])) + goto cleanup; + + if (cg_write(parent[1], "cgroup.subtree_control", "+dmem")) + goto cleanup; + + for (i =3D 0; i < ARRAY_SIZE(children); i++) { + children[i] =3D cg_name_indexed(parent[1], "dmem_child", i); + if (!children[i]) + goto cleanup; + if (cg_create(children[i])) + goto cleanup; + } + + if (dmem_write_limit(parent[1], "dmem.max", MB(8))) + goto cleanup; + for (i =3D 0; i < ARRAY_SIZE(children); i++) + if (dmem_write_limit(children[i], "dmem.max", leaf_max[i])) + goto cleanup; + + v =3D dmem_read_limit(parent[1], "dmem.max"); + if (v !=3D MB(8)) + goto cleanup; + for (i =3D 0; i < ARRAY_SIZE(children); i++) { + v =3D dmem_read_limit(children[i], "dmem.max"); + if (v !=3D leaf_max[i]) + goto cleanup; + } + + for (i =3D 0; i < ARRAY_SIZE(children); i++) { + if (cg_enter_current(children[i])) + goto cleanup; + in_child =3D 1; + + cur_before =3D dmem_read_limit(children[i], "dmem.current"); + if (cur_before < 0) + goto cleanup; + + if (dmem_selftest_alloc(leaf_max[i] + 1) >=3D 0) { + charged =3D 1; + goto cleanup; + } + + cur_after =3D dmem_read_limit(children[i], "dmem.current"); + if (cur_after !=3D cur_before) + goto cleanup; + + if (pass_sz[i] > 0) { + if (dmem_selftest_alloc(pass_sz[i]) < 0) + goto cleanup; + charged =3D 1; + + cur_after =3D dmem_read_limit(children[i], "dmem.current"); + if (cur_after !=3D cur_before + pass_sz[i]) + goto cleanup; + + if (dmem_selftest_free() < 0) + goto cleanup; + charged =3D 0; + + cur_after =3D dmem_read_limit(children[i], "dmem.current"); + if (cur_after !=3D cur_before) + goto cleanup; + } + + if (cg_enter_current(root)) + goto cleanup; + in_child =3D 0; + } + + ret =3D KSFT_PASS; + +cleanup: + if (charged) + dmem_selftest_free(); + if (in_child) + cg_enter_current(root); + for (i =3D ARRAY_SIZE(children) - 1; i >=3D 0; i--) { + if (!children[i]) + continue; + cg_destroy(children[i]); + free(children[i]); + } + for (i =3D ARRAY_SIZE(parent) - 1; i >=3D 0; i--) { + if (!parent[i]) + continue; + cg_destroy(parent[i]); + free(parent[i]); + } + return ret; +} + +/* + * Alloc non-page-aligned byte sizes and verify dmem.current matches + * the requested size exactly. The controller charges the byte count + * passed to dmem_cgroup_try_charge() with no page rounding, for this + * helper and for production drivers. Then free must return usage to 0. + */ +static int test_dmem_alloc_byte_granularity(const char *root) +{ + static const long sizes[] =3D { + 1, 4095, 4097, KB(75) + 1, MB(1), MB(1) + 1 + }; + char *cg =3D NULL; + long cur; + int ret =3D KSFT_FAIL; + int charged =3D 0; + int in_child =3D 0; + size_t i; + + cg =3D cg_name(root, "dmem_dbg_byte_gran"); + if (!cg) + goto cleanup; + + if (cg_create(cg)) + goto cleanup; + + if (dmem_write_limit(cg, "dmem.max", MB(16))) + goto cleanup; + + if (cg_enter_current(cg)) + goto cleanup; + in_child =3D 1; + + for (i =3D 0; i < ARRAY_SIZE(sizes); i++) { + if (dmem_selftest_alloc(sizes[i]) < 0) + goto cleanup; + charged =3D 1; + + cur =3D dmem_read_limit(cg, "dmem.current"); + if (cur !=3D sizes[i]) + goto cleanup; + + if (dmem_selftest_free() < 0) + goto cleanup; + charged =3D 0; + + cur =3D dmem_read_limit(cg, "dmem.current"); + if (cur !=3D 0) + goto cleanup; + } + + ret =3D KSFT_PASS; + +cleanup: + if (charged) + dmem_selftest_free(); + if (in_child) + cg_enter_current(root); + if (cg) { + cg_destroy(cg); + free(cg); + } + return ret; +} + +#define T(x) { x, #x } +struct dmem_test { + int (*fn)(const char *root); + const char *name; +} tests[] =3D { + T(test_dmem_max), + T(test_dmem_alloc_byte_granularity), +}; +#undef T + +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"); + + 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"); + + if (dmem_read_limit(root, "dmem.capacity") < 0) + ksft_exit_skip("dmem_selftest region not registered\n"); + + ksft_set_plan(ARRAY_SIZE(tests)); + + for (i =3D 0; i < ARRAY_SIZE(tests); i++) { + switch (tests[i].fn(root)) { + case KSFT_PASS: + ksft_test_result_pass("%s\n", tests[i].name); + break; + default: + ksft_test_result_fail("%s\n", tests[i].name); + break; + } + } + + ksft_finished(); +} --=20 2.55.0