From nobody Mon May 4 13:03:51 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 98CA134251B for ; Mon, 4 May 2026 08:39:41 +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=1777883983; cv=none; b=bmutfIKGVzVrlNfJ9ruMTSaDZHgUUwwAQ0bNZfcjl1YclaYPqmAaTHOAZZEMqvnrSuIFpRCCG4ro+TKO2xFMSpnARojdNWnKGM6evCbAqwoHwISXGpCOllE5WBSO7G8f48LJipBijC9meB0/7f6MxmqeMx+R/fl0YRR4ohOUKvM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777883983; c=relaxed/simple; bh=pJd60wbLypgMU61U+DU70Fjl3v119u5fGZsjoBbN8Mg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=pVshl6KCMsXDtJYvX/PT4J4qctGKtG3B9xbhtdASGGe+fyOY4D+YVzFwj7hhbG27oESC8+BK8eQgVcowMNMERnojQ/bsd9cmrTsE5tcNm5L24QxcPyGs2wqfJoEg12TWRqTCch6YLvxDxyrIdc5A614OOR4tg8+287WtcS5pyd8= 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=BF1tdJ2o; 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="BF1tdJ2o" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1777883980; 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=qkLS8kO2m4APQscuVIfZ1FfJanC+NFBqJUSsaAOTf70=; b=BF1tdJ2oA1xX6PtGZQST/piSm9Ickp6lKxPIJgCH5gLAQ7sY0UVj6H5uG3wARF5wTWGEFI MnzUgl3DHKLb5hGTBpt85lkhdrLpsHAAhO8Z5aHNKWld2OCVzkX9hbisqr/6A2Tw5yh9T0 t8hQc51UswehzOBdRYbM/LGkJSupGIE= 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-463-R26usSUVNV227Ysf7rPXuA-1; Mon, 04 May 2026 04:39:35 -0400 X-MC-Unique: R26usSUVNV227Ysf7rPXuA-1 X-Mimecast-MFC-AGG-ID: R26usSUVNV227Ysf7rPXuA_1777883974 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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 CE0BC1800473; Mon, 4 May 2026 08:39:33 +0000 (UTC) Received: from [192.168.1.153] (headnet01.pony-001.prod.iad2.dc.redhat.com [10.2.32.101]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E275A1800446; Mon, 4 May 2026 08:39:31 +0000 (UTC) From: Albert Esteve Date: Mon, 04 May 2026 10:39:23 +0200 Subject: [PATCH v3 1/4] 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: <20260504-kunit_cgroups-v3-1-4eac90b76f91@redhat.com> References: <20260504-kunit_cgroups-v3-0-4eac90b76f91@redhat.com> In-Reply-To: <20260504-kunit_cgroups-v3-0-4eac90b76f91@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 X-Developer-Signature: v=1; a=ed25519-sha256; t=1777883968; l=7447; i=aesteve@redhat.com; s=20260303; h=from:subject:message-id; bh=pJd60wbLypgMU61U+DU70Fjl3v119u5fGZsjoBbN8Mg=; b=eTvPmlMxXAJfqwea/pCZYkVz4JWQAc6SR1PvSExo/P2c/C/fsDgFvwuu39D/P91quu5THt3Zi C46eImAEO4ND1bE9BwGKe/aMlKp6ic5BG4qnry+1jqQAc0H+reFaVGX X-Developer-Key: i=aesteve@redhat.com; a=ed25519; pk=YSFz6sOHd2L45+Fr8DIvHTi6lSIjhLZ5T+rkxspJt1s= X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 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 | 199 ++++++++++++++++++++++++++++++++++++++= ++++ 3 files changed, 212 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..15520807e2dc8 --- /dev/null +++ b/kernel/cgroup/dmem_selftest.c @@ -0,0 +1,199 @@ +// 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) +{ + int ret =3D 0; + + 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 (IS_ERR(dbg_dir)) { + ret =3D PTR_ERR(dbg_dir); + goto dbgfs_error; + } + + 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 ret; + +dbgfs_error: + dmem_cgroup_unregister_region(selftest_region); + dbg_dir =3D NULL; + selftest_region =3D NULL; + return ret; +} + +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(IS_ERR_OR_NULL(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"); + + dmem_selftest_remove(); + + mutex_lock(&charge_lock); + if (charged_pool) { + dmem_cgroup_uncharge(charged_pool, charged_size); + charged_pool =3D NULL; + } + mutex_unlock(&charge_lock); +} + +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.53.0 From nobody Mon May 4 13:03: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 A29EF34214A for ; Mon, 4 May 2026 08:39:42 +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=1777883984; cv=none; b=eBrM6sbBs3MnCPOYHFMI8n3vP90/T3AFTxMUNgKcQiFhN0v2qgEyJp22j+/JoN7JNPyMNath+Z9EUG/RJHBlSxUMgxvqdVT5hn433kzohFXIl9MLNW0A6tO8nBTqKBmSb81u5z2/U/YpzuGEycS5TGL2QRDDEJWeCjWkndyjH8I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777883984; c=relaxed/simple; bh=6uWaCheGmLgukNolf3HipU9NOA5Fe1HK6CjH1nEq/eQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=b4aKO3lD7+Z8rt9iavhNjjdY4CSnrvwfrhlqSemF6ygEsyufDSNIO1iOcrdJjGszO9Rw5jvzg2KRCAD6e4Je6b9HsjH873k93Yp+b6FsPoqUeF03xChYK3MzD+xM1K1yFI6ex7c3QF4tlx//DbO5e06FAhO+/whGZvjN8d2gGUI= 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=Pn1dJ8s/; 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="Pn1dJ8s/" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1777883981; 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=p9LU2kvm8WmZLGE0Hzo3NSZzBLUUNYvesoYfRDXEHVk=; b=Pn1dJ8s/P/5X79TgHYftLVNZaBRwD4HnK4WuOox6HFRSSwjtlhy2Kmu6CzYyns9u3lILoF vO2L4kjG4Q94nuDwq8sDIMEpjou25iyTlB5/UadHw4SUDiT9yUAzc/R4Q8bPjS8lQoJn4K miEEB0xUvegwFKipwI3bfrIpA9B1u/4= 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-614-nuSdj70DPYWGFrYSQlcKsg-1; Mon, 04 May 2026 04:39:37 -0400 X-MC-Unique: nuSdj70DPYWGFrYSQlcKsg-1 X-Mimecast-MFC-AGG-ID: nuSdj70DPYWGFrYSQlcKsg_1777883976 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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 5BD86180034A; Mon, 4 May 2026 08:39:36 +0000 (UTC) Received: from [192.168.1.153] (headnet01.pony-001.prod.iad2.dc.redhat.com [10.2.32.101]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3A25D180045E; Mon, 4 May 2026 08:39:34 +0000 (UTC) From: Albert Esteve Date: Mon, 04 May 2026 10:39:24 +0200 Subject: [PATCH v3 2/4] 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: <20260504-kunit_cgroups-v3-2-4eac90b76f91@redhat.com> References: <20260504-kunit_cgroups-v3-0-4eac90b76f91@redhat.com> In-Reply-To: <20260504-kunit_cgroups-v3-0-4eac90b76f91@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 X-Developer-Signature: v=1; a=ed25519-sha256; t=1777883968; l=15431; i=aesteve@redhat.com; s=20260303; h=from:subject:message-id; bh=6uWaCheGmLgukNolf3HipU9NOA5Fe1HK6CjH1nEq/eQ=; b=USjH69OI+OILEI/qtezkVXTKIWpz6i8+ouT72wMtbY/llQVbTC/d0JVzIwqyqxMUth6rOqCs1 u13ionUip2DBFmctbmVaz6nPxLu/7VX10fgFFBl399zSz3ORH4PlMVU X-Developer-Key: i=aesteve@redhat.com; a=ed25519; pk=YSFz6sOHd2L45+Fr8DIvHTi6lSIjhLZ5T+rkxspJt1s= X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Currently, 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. Signed-off-by: Albert Esteve --- 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(+) diff --git a/tools/testing/selftests/cgroup/.gitignore b/tools/testing/self= tests/cgroup/.gitignore index 952e4448bf070..ea2322598217d 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 e01584c2189ac..e1a5e9316620e 100644 --- a/tools/testing/selftests/cgroup/Makefile +++ b/tools/testing/selftests/cgroup/Makefile @@ -10,6 +10,7 @@ TEST_GEN_FILES :=3D wait_inotify 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 @@ -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) diff --git a/tools/testing/selftests/cgroup/config b/tools/testing/selftest= s/cgroup/config index 39f979690dd3b..5728278310a31 100644 --- a/tools/testing/selftests/cgroup/config +++ b/tools/testing/selftests/cgroup/config @@ -1,6 +1,8 @@ CONFIG_CGROUPS=3Dy CONFIG_CGROUP_CPUACCT=3Dy +CONFIG_CGROUP_DMEM=3Dy CONFIG_CGROUP_FREEZER=3Dy CONFIG_CGROUP_SCHED=3Dy +CONFIG_DMEM_SELFTEST=3Dm CONFIG_MEMCG=3Dy CONFIG_PAGE_COUNTER=3Dy diff --git a/tools/testing/selftests/cgroup/test_dmem.c b/tools/testing/sel= ftests/cgroup/test_dmem.c new file mode 100644 index 0000000000000..0a1b9561a1b9e --- /dev/null +++ b/tools/testing/selftests/cgroup/test_dmem.c @@ -0,0 +1,492 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Test the dmem (device memory) cgroup controller. + * + * Depends on dmem_selftest kernel module. + */ + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "kselftest.h" +#include "cgroup_util.h" + +/* kernel/cgroup/dmem_selftest.c */ +#define DM_SELFTEST_REGION "dmem_selftest" +#define DM_SELFTEST_CHARGE "/sys/kernel/debug/dmem_selftest/charge" +#define DM_SELFTEST_UNCHARGE "/sys/kernel/debug/dmem_selftest/uncharge" + +/* + * Parse the first line of dmem.capacity (root): + * " " + * Returns 1 if a region was found, 0 if capacity is empty, -1 on read err= or. + */ +static int parse_first_region(const char *root, char *name, size_t name_le= n, + unsigned long long *size_out) +{ + char buf[4096]; + char nm[256]; + unsigned long long sz; + + if (cg_read(root, "dmem.capacity", buf, sizeof(buf)) < 0) + return -1; + + if (sscanf(buf, "%255s %llu", nm, &sz) < 2) + return 0; + + if (name_len <=3D strlen(nm)) + return -1; + + strcpy(name, nm); + *size_out =3D sz; + return 1; +} + +/* + * Read the numeric limit for @region_name from a multiline + * dmem.{min,low,max} file. Returns bytes, + * or -1 if the line is " max", or -2 if missing/err. + */ +static long long dmem_read_limit_for_region(const char *cgroup, const char= *ctrl, + const char *region_name) +{ + char buf[4096]; + char *line, *saveptr =3D NULL; + char fname[256]; + char fval[64]; + + if (cg_read(cgroup, ctrl, buf, sizeof(buf)) < 0) + return -2; + + for (line =3D strtok_r(buf, "\n", &saveptr); line; + line =3D strtok_r(NULL, "\n", &saveptr)) { + if (!line[0]) + continue; + if (sscanf(line, "%255s %63s", fname, fval) !=3D 2) + continue; + if (strcmp(fname, region_name)) + continue; + if (!strcmp(fval, "max")) + return -1; + return strtoll(fval, NULL, 0); + } + return -2; +} + +static long long dmem_read_limit(const char *cgroup, const char *ctrl) +{ + return dmem_read_limit_for_region(cgroup, ctrl, DM_SELFTEST_REGION); +} + +static int dmem_write_limit(const char *cgroup, const char *ctrl, + const char *val) +{ + char wr[512]; + + snprintf(wr, sizeof(wr), "%s %s", DM_SELFTEST_REGION, val); + return cg_write(cgroup, ctrl, wr); +} + +static int dmem_selftest_charge_bytes(unsigned long long bytes) +{ + char wr[32]; + + snprintf(wr, sizeof(wr), "%llu", bytes); + return write_text(DM_SELFTEST_CHARGE, wr, strlen(wr)); +} + +static int dmem_selftest_uncharge(void) +{ + return write_text(DM_SELFTEST_UNCHARGE, "\n", 1); +} + +/* + * First, this test creates the following hierarchy: + * A + * A/B dmem.max=3D1M + * A/B/C dmem.max=3D75K + * A/B/D dmem.max=3D25K + * A/B/E dmem.max=3D8K + * A/B/F dmem.max=3D0 + * + * Then for each leaf cgroup it tries to charge above dmem.max + * and expects the charge request to fail and dmem.current to + * remain unchanged. + * + * For leaves with non-zero dmem.max, it additionally charges a + * smaller amount and verifies accounting grows within one PAGE_SIZE + * rounding bound, then uncharges and verifies dmem.current returns + * to the previous value. + * + */ +static int test_dmem_max(const char *root) +{ + static const char * const leaf_max[] =3D { "75K", "25K", "8K", "0" }; + static const unsigned long long fail_sz[] =3D { + (75ULL * 1024ULL) + 1ULL, + (25ULL * 1024ULL) + 1ULL, + (8ULL * 1024ULL) + 1ULL, + 1ULL + }; + static const unsigned long long pass_sz[] =3D { + 4096ULL, 4096ULL, 4096ULL, 0ULL + }; + char *parent[2] =3D {NULL}; + char *children[4] =3D {NULL}; + unsigned long long cap; + char region[256]; + long long page_size; + long long cur_before, cur_after; + int ret =3D KSFT_FAIL; + int charged =3D 0; + int in_child =3D 0; + long long v; + int i; + + if (access(DM_SELFTEST_CHARGE, W_OK) !=3D 0) + return KSFT_SKIP; + + if (parse_first_region(root, region, sizeof(region), &cap) !=3D 1) + return KSFT_SKIP; + if (strcmp(region, DM_SELFTEST_REGION) !=3D 0) + return KSFT_SKIP; + + page_size =3D sysconf(_SC_PAGESIZE); + if (page_size <=3D 0) + goto cleanup; + + 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 < 4; 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", "1M")) + goto cleanup; + for (i =3D 0; i < 4; i++) + if (dmem_write_limit(children[i], "dmem.max", leaf_max[i])) + goto cleanup; + + v =3D dmem_read_limit(parent[1], "dmem.max"); + if (!values_close(v, 1024LL * 1024LL, 3)) + goto cleanup; + v =3D dmem_read_limit(children[0], "dmem.max"); + if (!values_close(v, 75LL * 1024LL, 3)) + goto cleanup; + v =3D dmem_read_limit(children[1], "dmem.max"); + if (!values_close(v, 25LL * 1024LL, 3)) + goto cleanup; + v =3D dmem_read_limit(children[2], "dmem.max"); + if (!values_close(v, 8LL * 1024LL, 3)) + goto cleanup; + v =3D dmem_read_limit(children[3], "dmem.max"); + if (v !=3D 0) + goto cleanup; + + for (i =3D 0; i < 4; 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_charge_bytes(fail_sz[i]) >=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_charge_bytes(pass_sz[i]) < 0) + goto cleanup; + charged =3D 1; + + cur_after =3D dmem_read_limit(children[i], "dmem.current"); + if (cur_after < cur_before + (long long)pass_sz[i]) + goto cleanup; + if (cur_after > cur_before + (long long)pass_sz[i] + page_size) + goto cleanup; + + if (dmem_selftest_uncharge() < 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_uncharge(); + if (in_child) + cg_enter_current(root); + for (i =3D 3; i >=3D 0; i--) { + if (!children[i]) + continue; + cg_destroy(children[i]); + free(children[i]); + } + for (i =3D 1; i >=3D 0; i--) { + if (!parent[i]) + continue; + cg_destroy(parent[i]); + free(parent[i]); + } + return ret; +} + +/* + * This test sets dmem.min and dmem.low on a child cgroup, then charge + * from that context and verify dmem.current tracks the charged bytes + * (within one page rounding). + */ +static int test_dmem_charge_with_attr(const char *root, bool min) +{ + char region[256]; + unsigned long long cap; + const unsigned long long charge_sz =3D 12345ULL; + const char *attribute =3D min ? "dmem.min" : "dmem.low"; + int ret =3D KSFT_FAIL; + char *cg =3D NULL; + long long cur; + long long page_size; + int charged =3D 0; + int in_child =3D 0; + + if (access(DM_SELFTEST_CHARGE, W_OK) !=3D 0) + return KSFT_SKIP; + + if (parse_first_region(root, region, sizeof(region), &cap) !=3D 1) + return KSFT_SKIP; + if (strcmp(region, DM_SELFTEST_REGION) !=3D 0) + return KSFT_SKIP; + + page_size =3D sysconf(_SC_PAGESIZE); + if (page_size <=3D 0) + goto cleanup; + + cg =3D cg_name(root, "test_dmem_attr"); + if (!cg) + goto cleanup; + + if (cg_create(cg)) + goto cleanup; + + if (cg_enter_current(cg)) + goto cleanup; + in_child =3D 1; + + if (dmem_write_limit(cg, attribute, "16K")) + goto cleanup; + + if (dmem_selftest_charge_bytes(charge_sz) < 0) + goto cleanup; + charged =3D 1; + + cur =3D dmem_read_limit(cg, "dmem.current"); + if (cur < (long long)charge_sz) + goto cleanup; + if (cur > (long long)charge_sz + page_size) + goto cleanup; + + if (dmem_selftest_uncharge() < 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_uncharge(); + if (in_child) + cg_enter_current(root); + cg_destroy(cg); + free(cg); + return ret; +} + +static int test_dmem_min(const char *root) +{ + return test_dmem_charge_with_attr(root, true); +} + +static int test_dmem_low(const char *root) +{ + return test_dmem_charge_with_attr(root, false); +} + +/* + * This test charges non-page-aligned byte sizes and verify dmem.current + * stays consistent: it must account at least the requested bytes and + * never exceed one kernel page of rounding overhead. Then uncharge must + * return usage to 0. + */ +static int test_dmem_charge_byte_granularity(const char *root) +{ + static const unsigned long long sizes[] =3D { 1ULL, 4095ULL, 4097ULL, 123= 45ULL }; + char *cg =3D NULL; + unsigned long long cap; + char region[256]; + long long cur; + long long page_size; + int ret =3D KSFT_FAIL; + int charged =3D 0; + int in_child =3D 0; + size_t i; + + if (access(DM_SELFTEST_CHARGE, W_OK) !=3D 0) + return KSFT_SKIP; + + if (parse_first_region(root, region, sizeof(region), &cap) !=3D 1) + return KSFT_SKIP; + if (strcmp(region, DM_SELFTEST_REGION) !=3D 0) + return KSFT_SKIP; + + page_size =3D sysconf(_SC_PAGESIZE); + if (page_size <=3D 0) + goto cleanup; + + 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", "8M")) + 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_charge_bytes(sizes[i]) < 0) + goto cleanup; + charged =3D 1; + + cur =3D dmem_read_limit(cg, "dmem.current"); + if (cur < (long long)sizes[i]) + goto cleanup; + if (cur > (long long)sizes[i] + page_size) + goto cleanup; + + if (dmem_selftest_uncharge() < 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_uncharge(); + 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_min), + T(test_dmem_low), + T(test_dmem_charge_byte_granularity), +}; +#undef T + +int main(int argc, char **argv) +{ + char root[PATH_MAX]; + int i; + + ksft_print_header(); + ksft_set_plan(ARRAY_SIZE(tests)); + + 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"); + + 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; + case KSFT_SKIP: + ksft_test_result_skip( + "%s (need CONFIG_DMEM_SELFTEST, modprobe dmem_selftest)\n", + tests[i].name); + break; + default: + ksft_test_result_fail("%s\n", tests[i].name); + break; + } + } + + ksft_finished(); +} --=20 2.53.0 From nobody Mon May 4 13:03:51 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 2A353343D85 for ; Mon, 4 May 2026 08:39:43 +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=1777883985; cv=none; b=pvNJ9E4dklhayMp60P66/MVg9OxjcmCkR4ZOg0aIoCtWkTRRDk1353rVud5Rx5uLKrtwO3a5pFeFwvbFi6NJzU/atE3Qv4Gd1Xlxmg6jMXlGuGHmd0wiiL/hsFArdHlSZYYAW9krhSmZMX9lpYAbsBopdJ8UnuKg+6KMmxjqtIw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777883985; c=relaxed/simple; bh=GlIRzQ/b52ziRsnq+N2ijPaXDw9lEj1Qjp3mG6f8mKc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=PErgoRbz4vesiCQAnIjU8PZBTF0fLdUNfMO/8Oy8UxC3ZdU+7gut12xzfDMdC4ULxxcap2BdCgb3JtnWg7GUkKYL1v4awMwTmw6cifJ6e86GATPDsjNZdVZm+4erF7mdO7GyN2iEqDpn2VxK+k2Gx35MrKvNpA2o0M6xrpk292o= 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=YZ3si/JT; 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="YZ3si/JT" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1777883983; 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=O36gqHIlCiPEtJMDTWpzt35ehRl1+0NGiHSrqGYxABU=; b=YZ3si/JTmHK/9S210ZYW6eYGyGtcsspm/Ckq7pUKtZx9+LSCO2u1asoNPln2DmuicLs8hU jBDiI0UU50PHPS5AnvUxc9Y1fYoMkQxUjZ9TcRaldaYzHUcrroeC89D9MAjvxgajOP1hPt fi6KtzAyIIR/L29R4oCO8mrpQjeb4WU= 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-3-tYPILtMUOEajpPJhL-tRbQ-1; Mon, 04 May 2026 04:39:40 -0400 X-MC-Unique: tYPILtMUOEajpPJhL-tRbQ-1 X-Mimecast-MFC-AGG-ID: tYPILtMUOEajpPJhL-tRbQ_1777883978 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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 AF710180049F; Mon, 4 May 2026 08:39:38 +0000 (UTC) Received: from [192.168.1.153] (headnet01.pony-001.prod.iad2.dc.redhat.com [10.2.32.101]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id AFD1A1800446; Mon, 4 May 2026 08:39:36 +0000 (UTC) From: Albert Esteve Date: Mon, 04 May 2026 10:39:25 +0200 Subject: [PATCH v3 3/4] selftests: cgroup: Add vmtest-dmem runner based on hid vmtest 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: <20260504-kunit_cgroups-v3-3-4eac90b76f91@redhat.com> References: <20260504-kunit_cgroups-v3-0-4eac90b76f91@redhat.com> In-Reply-To: <20260504-kunit_cgroups-v3-0-4eac90b76f91@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 X-Developer-Signature: v=1; a=ed25519-sha256; t=1777883968; l=6517; i=aesteve@redhat.com; s=20260303; h=from:subject:message-id; bh=GlIRzQ/b52ziRsnq+N2ijPaXDw9lEj1Qjp3mG6f8mKc=; b=5ShFkk9agNlbXZGB+dPh2dfVh29zdTKe8ga2BfkCO/e3rMlZCSA0YNIA99n3K2YxpBuqXQ7Wi qL21ZoAai8TDOBPWXwoviTYSXc4EiG6OKFGgZ8zDk+rsi5hkk/PvkHt X-Developer-Key: i=aesteve@redhat.com; a=ed25519; pk=YSFz6sOHd2L45+Fr8DIvHTi6lSIjhLZ5T+rkxspJt1s= X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Currently, 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. Signed-off-by: Albert Esteve --- tools/testing/selftests/cgroup/vmtest-dmem.sh | 197 ++++++++++++++++++++++= ++++ 1 file changed, 197 insertions(+) diff --git a/tools/testing/selftests/cgroup/vmtest-dmem.sh b/tools/testing/= selftests/cgroup/vmtest-dmem.sh new file mode 100755 index 0000000000000..9524dbddb06b7 --- /dev/null +++ b/tools/testing/selftests/cgroup/vmtest-dmem.sh @@ -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=3D"$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && p= wd -P)" +readonly KERNEL_CHECKOUT=3D"$(realpath "${SCRIPT_DIR}"/../../../../)" + +source "${SCRIPT_DIR}"/../kselftest/ktap_helpers.sh + +readonly SSH_GUEST_PORT=3D"${SSH_GUEST_PORT:-22}" +readonly WAIT_PERIOD=3D3 +readonly WAIT_PERIOD_MAX=3D80 +readonly WAIT_TOTAL=3D$((WAIT_PERIOD * WAIT_PERIOD_MAX)) +readonly QEMU_PIDFILE=3D"$(mktemp /tmp/qemu_dmem_vmtest_XXXX.pid)" +readonly QEMU_OPTS=3D" --pidfile ${QEMU_PIDFILE} " + +QEMU=3D"qemu-system-$(uname -m)" +VERBOSE=3D0 +SHELL_MODE=3D0 +GUEST_TREE=3D"${GUEST_TREE:-$KERNEL_CHECKOUT}" + +usage() { + echo + echo "$0 [OPTIONS]" + echo " -q 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=3D/dev/null + local verbose_opt=3D"" + + if [[ "${VERBOSE}" -eq 1 ]]; then + verbose_opt=3D"--verbose" + logfile=3D/dev/stdout + fi + + vng \ + --run \ + ${verbose_opt} \ + --qemu-opts=3D"${QEMU_OPTS}" \ + --qemu=3D"$(command -v "${QEMU}")" \ + --user root \ + --ssh "${SSH_GUEST_PORT}" \ + --rw &>"${logfile}" & + + local vng_pid=3D$! + local elapsed=3D0 + + while [[ ! -s "${QEMU_PIDFILE}" ]]; do + kill -0 "${vng_pid}" 2>/dev/null || die "vng exited early; failed to boo= t VM" + [[ "${elapsed}" -ge "${WAIT_TOTAL}" ]] && die "timed out waiting for VM = boot" + sleep 1 + elapsed=3D$((elapsed + 1)) + done +} + +vm_wait_for_ssh() { + local i=3D0 + while true; do + vm_ssh -- true && break + i=3D$((i + 1)) + [[ "${i}" -gt "${WAIT_PERIOD_MAX}" ]] && die "timed out waiting for gues= t ssh" + sleep "${WAIT_PERIOD}" + done +} + +check_guest_requirements() { + local cfg_ok + cfg_ok=3D"$(vm_ssh -- " \ + grep -q dmem /sys/fs/cgroup/cgroup.controllers && \ + grep -q memory /sys/fs/cgroup/cgroup.controllers; + echo \$? + ")" + [[ "${cfg_ok}" =3D=3D "0" ]] || die "guest kernel missing CONFIG_CGROUP_D= MEM" +} + +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=3D"$(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=3D'${GUEST_TREE}/kernel/cgroup' \ + CONFIG_DMEM_SELFTEST=3Dm 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= =3Dcgroup" + vm_ssh -- "cd '${GUEST_TREE}' && ./tools/testing/selftests/cgroup/test_dm= em" +} + +while getopts ":hvq:s" o; do + case "${o}" in + v) VERBOSE=3D1 ;; + q) QEMU=3D"${OPTARG}" ;; + s) SHELL_MODE=3D1 ;; + 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}" --=20 2.53.0 From nobody Mon May 4 13:03:51 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 66E86344DA9 for ; Mon, 4 May 2026 08:39:49 +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=1777883990; cv=none; b=olAuk1Sudw9XeW2nCybF/PuzRjLJf1tOXsR+GrfU96rPRA/jkXiwXat7f1L53IEwNHvWW0VG3E3piIlwnNMShxHBf6jlg/8SNAmbGyN+0PdRUh7qDWVZwlF+YwlzbUvMpqxGwlR2VgI489g++SvDP4Yj+6pdeskTQ0YX3k+XDaY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777883990; c=relaxed/simple; bh=2RTP13Nu7Z4tEtxogQgWd3CW8wKZRNhB047xK4y1OSk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ZG1ltrSC0wnWRMODicvXzehDphHcM8TYZeDwQdEfAZW4HPnZKF7a5myDyLNClcUX79XrxmDjFdTfeaVF87PFAB7exXMphGO38rXAaxCtF5uujAQUAj7UcanyWWEeLbDUaNYp/5Eh9cyMFXn3vcPDfmH9SWkSudKZvVhBqGfGSoU= 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=Iwk7lop5; 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="Iwk7lop5" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1777883988; 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=1FCmw5PTtSXzNIV6yVE0OtGGUXuOUm+K8ky9wYITGY8=; b=Iwk7lop5olnTUwps/SIorz3NaJvgDC6JaXknBJ14dZFWiUOq7fvHW9c8cGha0VB0YK7xrx dREvR9XzDHs4D2CRJmxBLx9kGU36caWdC4NssLvmqysZ7FecDsdDmRiFV1rkXrABsE8MoF iP/v+Rb27tSF5LkBD8c3Lk4dtSSoMww= 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-327-IuvFYTI_OCaecIwOPj80oQ-1; Mon, 04 May 2026 04:39:43 -0400 X-MC-Unique: IuvFYTI_OCaecIwOPj80oQ-1 X-Mimecast-MFC-AGG-ID: IuvFYTI_OCaecIwOPj80oQ_1777883981 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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 62DA6195608C; Mon, 4 May 2026 08:39:41 +0000 (UTC) Received: from [192.168.1.153] (headnet01.pony-001.prod.iad2.dc.redhat.com [10.2.32.101]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 259E21800347; Mon, 4 May 2026 08:39:38 +0000 (UTC) From: Albert Esteve Date: Mon, 04 May 2026 10:39:26 +0200 Subject: [PATCH v3 4/4] selftests: cgroup: handle vmtest-dmem -b to test locally built kernel 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: <20260504-kunit_cgroups-v3-4-4eac90b76f91@redhat.com> References: <20260504-kunit_cgroups-v3-0-4eac90b76f91@redhat.com> In-Reply-To: <20260504-kunit_cgroups-v3-0-4eac90b76f91@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, Eric Chanudet X-Developer-Signature: v=1; a=ed25519-sha256; t=1777883968; l=3222; i=aesteve@redhat.com; s=20260303; h=from:subject:message-id; bh=5hsHacvz/JuYV7yLWF/sNuSW9yMcwd+DRLDYe4UgWBM=; b=v3BtPSCdpKlecFOBGNnBJRWFTR7dDWB4TkXbPzSuXKpw1MxZctKXcdTgQgKZegLjznHx9K8HQ Q8XxjB+ed+7BTynlEn+JXGVup51q1PfE+zsXrIUKZIhSpRauDEWtsJY X-Developer-Key: i=aesteve@redhat.com; a=ed25519; pk=YSFz6sOHd2L45+Fr8DIvHTi6lSIjhLZ5T+rkxspJt1s= X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 From: Eric Chanudet 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. Signed-off-by: Eric Chanudet Signed-off-by: Albert Esteve --- tools/testing/selftests/cgroup/vmtest-dmem.sh | 35 +++++++++++++++++++++++= +++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/cgroup/vmtest-dmem.sh b/tools/testing/= selftests/cgroup/vmtest-dmem.sh index 9524dbddb06b7..b6e4777285c1b 100755 --- a/tools/testing/selftests/cgroup/vmtest-dmem.sh +++ b/tools/testing/selftests/cgroup/vmtest-dmem.sh @@ -23,6 +23,7 @@ readonly WAIT_TOTAL=3D$((WAIT_PERIOD * WAIT_PERIOD_MAX)) readonly QEMU_PIDFILE=3D"$(mktemp /tmp/qemu_dmem_vmtest_XXXX.pid)" readonly QEMU_OPTS=3D" --pidfile ${QEMU_PIDFILE} " =20 +BUILD=3D0 QEMU=3D"qemu-system-$(uname -m)" VERBOSE=3D0 SHELL_MODE=3D0 @@ -31,6 +32,7 @@ GUEST_TREE=3D"${GUEST_TREE:-$KERNEL_CHECKOUT}" usage() { echo echo "$0 [OPTIONS]" + echo " -b Build kernel from source tree before booting" echo " -q 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 } =20 +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_CHECKOU= T})" + fi + + if ! make -j"$(nproc)"; then + die "failed to build kernel from source tree (${KERNEL_CHECKOUT})" + fi + + popd &>/dev/null +} + vm_start() { local logfile=3D/dev/null local verbose_opt=3D"" + local kernel_opt=3D"" =20 if [[ "${VERBOSE}" -eq 1 ]]; then verbose_opt=3D"--verbose" logfile=3D/dev/stdout fi =20 + if [[ "${BUILD}" -eq 1 ]]; then + kernel_opt=3D"${KERNEL_CHECKOUT}" + fi + vng \ --run \ + ${kernel_opt} \ ${verbose_opt} \ --qemu-opts=3D"${QEMU_OPTS}" \ --qemu=3D"$(command -v "${QEMU}")" \ @@ -165,10 +196,11 @@ run_test() { vm_ssh -- "cd '${GUEST_TREE}' && ./tools/testing/selftests/cgroup/test_dm= em" } =20 -while getopts ":hvq:s" o; do +while getopts ":hvq:sb" o; do case "${o}" in v) VERBOSE=3D1 ;; q) QEMU=3D"${OPTARG}" ;; + b) BUILD=3D1 ;; s) SHELL_MODE=3D1 ;; h|*) usage ;; esac @@ -177,6 +209,7 @@ done trap cleanup EXIT =20 check_deps +handle_build echo "Booting virtme-ng VM..." vm_start vm_wait_for_ssh --=20 2.53.0