Skip to contentSkip to conversation

add cgroup_dmem test

PATCH Superseded Thadeu Lima de Souza Cascardo 3 participants 25 messages Sep 3 – Sep 18

Subsystems (2)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v6 cover add cgroup_dmem test

v6 TLDR:
Change igt_dmem_driver API by moving handle array allocation out of it and into the test itself and including dmem.max new behavior test.

This patchset is based on the work of Thomas Hellström at [1] and a followup I have submitted at [2].

With this version, I reverted the order in which to apply the patchsets, as this one adds the infrastructure and the drivers mechanisms, and a test for dmem.current and its behavior around dmem.max that is currently supported in the kernel.

As for the new dmem.max eviction behavior, introduced by [3], it has been now included since it has been merged to Linux v7.3-rc1.

I have run the tests, including the ones that test dmem.max eviction behavior on a system with a 1GiB VRAM amdgpu card. Rodrigo Siqueira helped me run it on a Xe system. We have had applied the patchset at [3] on top of then current linux origin/master [5], with a fixup for the amdgpu driver.

This latest version has not been tested on Xe, except for the dmem.current behavior, which was tested with a 7.0 kernel.

Latest tests for amdgpu used v7.3-rc1.

Now, for the changes since v1 [2]:
- Changed the test name from drv_dmem_cgroups to dmem_cgroups.
- Have system includes before igt ones.
- Documented the current SUBTEST.
- Changed free_vram driver hook to only release a given BO. This allows for the eviction trigger by a new allocation added by write_eviction_nonblock subtest.
- Changed allocate_vram to keep more state necessary for releasing a single BO.
- Added a global free_vram helper to free all allocated VRAM.

Compared to v3 of [1]:
- I have kept wait_for_usage_drop as release can be lazy and we test for it on test_current SUBTEST, but had USAGE_DROP_TIMEOUT_MS be a little longer.
- Changed BO_SIZE and EVICT_STEP to allow for more than two eviction steps on a 1GiB VRAM system.
- Reduced USAGE_SLACK to avoid false positives, but still allow for some page table accounting.

Compared to v2 of this one [6]:

- Ignore -ENOMEM and -ENOSPC when trying to fill VRAM.
- When limiting max memory, allow for some slack, as there might be some overhead.
- On Xe, allocate GEM object with no deferred backing, and skip binding.

Compared to v3 of this one [7]:

- Merge dmem_cgroups tests into cgroup_dmem and making cgroup_dmem original test a subtest.
- Remove assertion when trying to verify allocations have failed as amdgpu will fallback to GTT.

Compared to v4 of this one [8]:
- Removed the creation of vm in Xe, that was leading to extra bytes being used and was no longer necessary.
- Removed the USAGE_SLACK that was necessary due to the extra bytes allocated due to the use of vm in Xe.

Compared to v5 of this one [10]:
- Documented igt_dmem_driver structure.
- Simplified igt_dmem_driver API, such that handle array allocation is not done, and handles are returned as part of allocation.
- Make get_region_name callback use the context, instead of fd, which avoids repeating the vram region lookup for Xe.
- Moved handle array allocation into the test.
- init/deinit the igt_dmem_driver per driver, not per subtest.

[1] https://patchwork.freedesktop.org/series/163935/#rev3
[2] https://patchwork.freedesktop.org/series/166496/
[3] https://lore.kernel.org/dri-devel/[email protected]/
[4] https://gitlab.freedesktop.org/cascardo/igt-gpu-tools/-/commits/dmem_max?ref_type=heads
[5] 502d801f0ab0 ("Merge tag 'erofs-for-7.2-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/xiang/erofs")
[6] https://lore.kernel.org/dri-devel/[email protected]/
[7] https://lore.kernel.org/dri-devel/[email protected]/
[8] https://lore.kernel.org/dri-devel/[email protected]/
[9] https://lore.kernel.org/dri-devel/[email protected]/
[10] https://lore.kernel.org/dri-devel/[email protected]/

To: [email protected]
Cc: [email protected]
Cc: Thadeu Lima de Souza Cascardo <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: Christian Koenig <[email protected]>
Cc: [email protected]
Cc: Thomas Hellström <[email protected]>
Cc: Kamil Konieczny <[email protected]>
Cc: Janusz Krzysztofik <[email protected]>
Cc: Vitaly Prosyak <[email protected]>
Cc: Natalie Vock <[email protected]>
Cc: Tvrtko Ursulin <[email protected]>
Cc: [email protected]

--
2.47.3

---
Thadeu Lima de Souza Cascardo (4):
lib/xe: Introduce dmem driver and implement Xe support lib/amdgpu: add amdgpu_cgroup_region_name lib/amdgpu: add amdgpu support to igt_dmem_driver tests/cgroup_dmem: add test for dmem.current

Thomas Hellström (5):
lib/igt_cgroup: add cgroup v2 and dmem controller helpers tests/cgroup_dmem: add dmem cgroup controller test lib/xe: add xe_cgroup_region_name() helper tests/cgroup_dmem: add dmem cgroup eviction test tests/cgroup_dmem: add write_eviction_nonblock subtest

 lib/amdgpu/amd_dmem.c   |  90 +++++++
 lib/amdgpu/amd_memory.c |  25 ++
 lib/amdgpu/amd_memory.h |   2 +
 lib/igt.h               |   1 +
 lib/igt_cgroup.c        | 638 ++++++++++++++++++++++++++++++++++++++++++++++++
 lib/igt_cgroup.h        |  56 +++++
 lib/igt_dmem_driver.h   |  35 +++
 lib/meson.build         |   3 +
 lib/xe/xe_dmem.c        |  97 ++++++++
 lib/xe/xe_query.c       |  32 +++
 lib/xe/xe_query.h       |   2 +
 tests/cgroup_dmem.c     | 524 +++++++++++++++++++++++++++++++++++++++
 tests/meson.build       |   1 +
 13 files changed, 1506 insertions(+)

---
base-commit: aafc4bcf336167ad12356050b2e1da7e8c314555
change-id: 20260902-dmem_max-1a5e837347a8

Best regards,

Signature · 1 line
Thadeu Lima de Souza Cascardo <[email protected]>
#1 lore raw

PATCH v6 1/9 lib/igt_cgroup: add cgroup v2 and dmem controller helpers

From: Thomas Hellström <[email protected]>

Add igt_cgroup, a library module providing helpers to create and manage cgroup v2 sub-cgroups from IGT tests, with support for the dmem controller that governs device memory (e.g. GPU VRAM) limits.

The API covers:
- igt_cgroup_new() / igt_cgroup_free(): create and destroy a named sub-cgroup under the unified cgroupv2 hierarchy, enabling the dmem controller automatically.
- igt_cgroup_move_current(): move the calling process into a cgroup.
- igt_cgroup_dmem_set/get_max/min/low(): write and read dmem.max, dmem.min and dmem.low for a named device memory region.
- igt_cgroup_dmem_get_current(): read current per-cgroup device memory usage.
- igt_cgroup_dmem_get_system_current(): read system-wide device memory usage from the root cgroup.
- igt_cgroup_dmem_get_capacity(): read total region capacity from the root cgroup's dmem.capacity file.
- igt_cgroup_dmem_regions() / igt_cgroup_dmem_regions_free(): enumerate all registered device memory regions.

All public API functions that can fail use igt_assert internally rather than returning error codes, following the IGT convention.

Assisted-by GitHub Copilot:claude-sonnet-4.6
---
 lib/igt.h        |   1 +
 lib/igt_cgroup.c | 638 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/igt_cgroup.h |  56 +++++
 lib/meson.build  |   1 +
 4 files changed, 696 insertions(+)
lib/igt.h +1 -0
index 173ca70bff28..d8e5de7dcd2f 100644
@@ -27,6 +27,7 @@
 #include "drmtest.h"
 #include "i915_3d.h"
 #include "igt_aux.h"
+#include "igt_cgroup.h"
 #include "igt_configfs.h"
 #include "igt_core.h"
 #include "igt_debugfs.h"
lib/igt_cgroup.c new file +638 -0

638 lines not on this page · raw message

lib/igt_cgroup.h new file +56 -0

56 lines not on this page · raw message

lib/meson.build +1 -0
index a7cde027ed04..b7e1be61d844 100644
@@ -19,6 +19,7 @@ lib_sources = [
 	'i915/i915_dp.c',
 	'igt_collection.c',
 	'igt_color_encoding.c',
+	'igt_cgroup.c',
 	'igt_configfs.c',
 	'igt_facts.c',
 	'igt_crc.c',
Signature · 1 line
2.47.3
#2 lore raw

PATCH v6 2/9 tests/cgroup_dmem: add dmem cgroup controller test

In reply to Thadeu Lima de Souza Cascardo

From: Thomas Hellström <[email protected]>

Add a test that exercises the cgroup v2 dmem controller interface using the new igt_cgroup library.

The test uses igt_simple_main and:
- Skips if no dmem regions are registered (no cgroup v2 or no dmem-capable device).
- Creates a sub-cgroup and moves the test process into it.
- Enumerates all registered device memory regions and prints their capacity, system-wide current usage, per-cgroup current usage, and configured min, low and max limits.
- Destroys the cgroup on completion.

Assisted-by GitHub Copilot:claude-sonnet-4.6
---
 tests/cgroup_dmem.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/meson.build   |  1 +
 2 files changed, 93 insertions(+)
tests/cgroup_dmem.c new file +92 -0
new file mode 100644
index 000000000000..442c965f9bbf
@@ -0,0 +1,92 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2025 Intel Corporation
+ */
+
+/**
+ * TEST: cgroup dmem
+ * Description: Exercises the cgroup v2 dmem controller interface.  Creates a
+ *              cgroup, moves the process into it, enumerates all dmem regions,
+ *              prints their capacity, system-wide current usage, per-cgroup
+ *              current usage and configured limits, then destroys the cgroup.
+ * Category: Core
+ * Mega feature: General Core features
+ * Sub-category: uapi
+ * Functionality: cgroup
+ * Feature: dmem
+ * Test category: uapi
+ */
+
+#include <inttypes.h>
+
+#include "igt.h"
+#include "igt_cgroup.h"
+
+IGT_TEST_DESCRIPTION("Exercises the cgroup v2 dmem controller interface.");
+
+static void fmt_bytes(uint64_t v, char *buf, size_t len)
+{
+	if (v == IGT_CGROUP_DMEM_MAX)
+		snprintf(buf, len, "max");
+	else
+		snprintf(buf, len, "%" PRIu64, v);
+}
+
+int igt_simple_main()
+{
+	struct igt_cgroup *cg;
+	const char *region;
+	char **regions;
+	uint64_t capacity, sys_current, cg_current, min, low, max;
+	char cap_s[32], sys_s[32], cg_s[32];
+	char min_s[32], low_s[32], max_s[32];
+	int i;
+
+	igt_require_f(igt_cgroup_dmem_available(),
+		      "No dmem regions found; is cgroup v2 with the "
+		      "dmem controller available?\n");
+
+	cg = igt_cgroup_new("igt-cgroup-dmem-test");
+	igt_assert_f(cg, "Failed to create cgroup\n");
+
+	igt_cgroup_move_current(cg);
+
+	regions = igt_cgroup_dmem_regions();
+	igt_assert_f(regions, "Failed to enumerate dmem regions\n");
+
+	igt_info("%-40s %16s %16s %16s %16s %16s %16s\n",
+		 "region", "capacity", "system-current",
+		 "cgroup-current", "min", "low", "max");
+	igt_info("%-40s %16s %16s %16s %16s %16s %16s\n",
+		 "------", "--------", "--------------",
+		 "--------------", "---", "---", "---");
+
+	for (i = 0; regions[i]; i++) {
+		region = regions[i];
+
+		igt_cgroup_dmem_get_capacity(region, &capacity);
+		fmt_bytes(capacity, cap_s, sizeof(cap_s));
+
+		igt_cgroup_dmem_get_system_current(region, &sys_current);
+		fmt_bytes(sys_current, sys_s, sizeof(sys_s));
+
+		igt_cgroup_dmem_get_current(cg, region, &cg_current);
+		fmt_bytes(cg_current, cg_s, sizeof(cg_s));
+
+		igt_cgroup_dmem_get_min(cg, region, &min);
+		fmt_bytes(min, min_s, sizeof(min_s));
+
+		igt_cgroup_dmem_get_low(cg, region, &low);
+		fmt_bytes(low, low_s, sizeof(low_s));
+
+		igt_cgroup_dmem_get_max(cg, region, &max);
+		fmt_bytes(max, max_s, sizeof(max_s));
+
+		igt_info("%-40s %16s %16s %16s %16s %16s %16s\n",
+			 region, cap_s, sys_s, cg_s,
+			 min_s, low_s, max_s);
+	}
+
+	igt_cgroup_dmem_regions_free(regions);
+	igt_cgroup_free(cg);
+}
tests/meson.build +1 -0
index 1ac89bab7e15..c362d42888ba 100644
@@ -1,4 +1,5 @@
 test_progs = [
+	'cgroup_dmem',
 	'core_auth',
 	'core_debugfs',
 	'core_getclient',
Signature · 1 line
2.47.3
#3 lore raw

Re: [PATCH i-g-t v6 2/9] tests/cgroup_dmem: add dmem cgroup controller test

Hi Thadeu,

Quoted 52 lines · Thadeu Lima de Souza Cascardo

From: Thomas Hellström <[email protected]>

Add a test that exercises the cgroup v2 dmem controller interface using the new igt_cgroup library.

The test uses igt_simple_main and:
- Skips if no dmem regions are registered (no cgroup v2 or no dmem-capable device).
- Creates a sub-cgroup and moves the test process into it.
- Enumerates all registered device memory regions and prints their capacity, system-wide current usage, per-cgroup current usage, and configured min, low and max limits.
- Destroys the cgroup on completion.

Assisted-by GitHub Copilot:claude-sonnet-4.6
---
 tests/cgroup_dmem.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/meson.build   |  1 +
 2 files changed, 93 insertions(+)
tests/cgroup_dmem.c new file +25 -0
new file mode 100644
index 000000000000..442c965f9bbf
@@ -0,0 +1,92 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2025 Intel Corporation
+ */
+
+/**
+ * TEST: cgroup dmem
+ * Description: Exercises the cgroup v2 dmem controller interface.  Creates a
+ *              cgroup, moves the process into it, enumerates all dmem regions,
+ *              prints their capacity, system-wide current usage, per-cgroup
+ *              current usage and configured limits, then destroys the cgroup.
+ * Category: Core
+ * Mega feature: General Core features
+ * Sub-category: uapi
+ * Functionality: cgroup
+ * Feature: dmem
+ * Test category: uapi
+ */
+
+#include <inttypes.h>
+
+#include "igt.h"
+#include "igt_cgroup.h"
+
+IGT_TEST_DESCRIPTION("Exercises the cgroup v2 dmem controller interface.");

tests/cgroup_dmem.c line 35

Quoted 10 lines
tests/cgroup_dmem.c new file +10 -0
+
+static void fmt_bytes(uint64_t v, char *buf, size_t len)
+{
+	if (v == IGT_CGROUP_DMEM_MAX)
+		snprintf(buf, len, "max");
+	else
+		snprintf(buf, len, "%" PRIu64, v);
+}
+
+int igt_simple_main()

Can it be igt_main() with igt_fixture for require and a basic subtest? It is not blocker, it could be converted later.

Regards,
Kamil

Quoted 71 lines
tests/cgroup_dmem.c new file +57 -0
+{
+	struct igt_cgroup *cg;
+	const char *region;
+	char **regions;
+	uint64_t capacity, sys_current, cg_current, min, low, max;
+	char cap_s[32], sys_s[32], cg_s[32];
+	char min_s[32], low_s[32], max_s[32];
+	int i;
+
+	igt_require_f(igt_cgroup_dmem_available(),
+		      "No dmem regions found; is cgroup v2 with the "
+		      "dmem controller available?\n");
+
+	cg = igt_cgroup_new("igt-cgroup-dmem-test");
+	igt_assert_f(cg, "Failed to create cgroup\n");
+
+	igt_cgroup_move_current(cg);
+
+	regions = igt_cgroup_dmem_regions();
+	igt_assert_f(regions, "Failed to enumerate dmem regions\n");
+
+	igt_info("%-40s %16s %16s %16s %16s %16s %16s\n",
+		 "region", "capacity", "system-current",
+		 "cgroup-current", "min", "low", "max");
+	igt_info("%-40s %16s %16s %16s %16s %16s %16s\n",
+		 "------", "--------", "--------------",
+		 "--------------", "---", "---", "---");
+
+	for (i = 0; regions[i]; i++) {
+		region = regions[i];
+
+		igt_cgroup_dmem_get_capacity(region, &capacity);
+		fmt_bytes(capacity, cap_s, sizeof(cap_s));
+
+		igt_cgroup_dmem_get_system_current(region, &sys_current);
+		fmt_bytes(sys_current, sys_s, sizeof(sys_s));
+
+		igt_cgroup_dmem_get_current(cg, region, &cg_current);
+		fmt_bytes(cg_current, cg_s, sizeof(cg_s));
+
+		igt_cgroup_dmem_get_min(cg, region, &min);
+		fmt_bytes(min, min_s, sizeof(min_s));
+
+		igt_cgroup_dmem_get_low(cg, region, &low);
+		fmt_bytes(low, low_s, sizeof(low_s));
+
+		igt_cgroup_dmem_get_max(cg, region, &max);
+		fmt_bytes(max, max_s, sizeof(max_s));
+
+		igt_info("%-40s %16s %16s %16s %16s %16s %16s\n",
+			 region, cap_s, sys_s, cg_s,
+			 min_s, low_s, max_s);
+	}
+
+	igt_cgroup_dmem_regions_free(regions);
+	igt_cgroup_free(cg);
+}
tests/meson.build +1 -0
index 1ac89bab7e15..c362d42888ba 100644
@@ -1,4 +1,5 @@
 test_progs = [
+	'cgroup_dmem',
 	'core_auth',
 	'core_debugfs',
 	'core_getclient',
Signature · 2 lines
-- 
2.47.3
#4 lore raw

Re: [PATCH i-g-t v6 2/9] tests/cgroup_dmem: add dmem cgroup controller test

Quoted 68 lines · Kamil Konieczny

Hi Thadeu,

Quoted 52 lines · Thadeu Lima de Souza Cascardo

From: Thomas Hellström <[email protected]>

Add a test that exercises the cgroup v2 dmem controller interface using the new igt_cgroup library.

The test uses igt_simple_main and:
- Skips if no dmem regions are registered (no cgroup v2 or no dmem-capable device).
- Creates a sub-cgroup and moves the test process into it.
- Enumerates all registered device memory regions and prints their capacity, system-wide current usage, per-cgroup current usage, and configured min, low and max limits.
- Destroys the cgroup on completion.

Assisted-by GitHub Copilot:claude-sonnet-4.6
---
 tests/cgroup_dmem.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/meson.build   |  1 +
 2 files changed, 93 insertions(+)
tests/cgroup_dmem.c new file +25 -0
new file mode 100644
index 000000000000..442c965f9bbf
@@ -0,0 +1,92 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2025 Intel Corporation
+ */
+
+/**
+ * TEST: cgroup dmem
+ * Description: Exercises the cgroup v2 dmem controller interface.  Creates a
+ *              cgroup, moves the process into it, enumerates all dmem regions,
+ *              prints their capacity, system-wide current usage, per-cgroup
+ *              current usage and configured limits, then destroys the cgroup.
+ * Category: Core
+ * Mega feature: General Core features
+ * Sub-category: uapi
+ * Functionality: cgroup
+ * Feature: dmem
+ * Test category: uapi
+ */
+
+#include <inttypes.h>
+
+#include "igt.h"
+#include "igt_cgroup.h"
+
+IGT_TEST_DESCRIPTION("Exercises the cgroup v2 dmem controller interface.");

tests/cgroup_dmem.c line 35

Quoted 10 lines
tests/cgroup_dmem.c new file +10 -0
+
+static void fmt_bytes(uint64_t v, char *buf, size_t len)
+{
+	if (v == IGT_CGROUP_DMEM_MAX)
+		snprintf(buf, len, "max");
+	else
+		snprintf(buf, len, "%" PRIu64, v);
+}
+
+int igt_simple_main()

Can it be igt_main() with igt_fixture for require and a basic subtest? It is not blocker, it could be converted later.

Quoted 3 lines

Regards,
Kamil

I do change it to igt_main() in a later patch. So, it is converted later. I will it as is, then.

Thanks.
Cascardo.

Quoted 71 lines
Quoted 71 lines
tests/cgroup_dmem.c new file +57 -0
+{
+	struct igt_cgroup *cg;
+	const char *region;
+	char **regions;
+	uint64_t capacity, sys_current, cg_current, min, low, max;
+	char cap_s[32], sys_s[32], cg_s[32];
+	char min_s[32], low_s[32], max_s[32];
+	int i;
+
+	igt_require_f(igt_cgroup_dmem_available(),
+		      "No dmem regions found; is cgroup v2 with the "
+		      "dmem controller available?\n");
+
+	cg = igt_cgroup_new("igt-cgroup-dmem-test");
+	igt_assert_f(cg, "Failed to create cgroup\n");
+
+	igt_cgroup_move_current(cg);
+
+	regions = igt_cgroup_dmem_regions();
+	igt_assert_f(regions, "Failed to enumerate dmem regions\n");
+
+	igt_info("%-40s %16s %16s %16s %16s %16s %16s\n",
+		 "region", "capacity", "system-current",
+		 "cgroup-current", "min", "low", "max");
+	igt_info("%-40s %16s %16s %16s %16s %16s %16s\n",
+		 "------", "--------", "--------------",
+		 "--------------", "---", "---", "---");
+
+	for (i = 0; regions[i]; i++) {
+		region = regions[i];
+
+		igt_cgroup_dmem_get_capacity(region, &capacity);
+		fmt_bytes(capacity, cap_s, sizeof(cap_s));
+
+		igt_cgroup_dmem_get_system_current(region, &sys_current);
+		fmt_bytes(sys_current, sys_s, sizeof(sys_s));
+
+		igt_cgroup_dmem_get_current(cg, region, &cg_current);
+		fmt_bytes(cg_current, cg_s, sizeof(cg_s));
+
+		igt_cgroup_dmem_get_min(cg, region, &min);
+		fmt_bytes(min, min_s, sizeof(min_s));
+
+		igt_cgroup_dmem_get_low(cg, region, &low);
+		fmt_bytes(low, low_s, sizeof(low_s));
+
+		igt_cgroup_dmem_get_max(cg, region, &max);
+		fmt_bytes(max, max_s, sizeof(max_s));
+
+		igt_info("%-40s %16s %16s %16s %16s %16s %16s\n",
+			 region, cap_s, sys_s, cg_s,
+			 min_s, low_s, max_s);
+	}
+
+	igt_cgroup_dmem_regions_free(regions);
+	igt_cgroup_free(cg);
+}
tests/meson.build +1 -0
index 1ac89bab7e15..c362d42888ba 100644
@@ -1,4 +1,5 @@
 test_progs = [
+	'cgroup_dmem',
 	'core_auth',
 	'core_debugfs',
 	'core_getclient',
Signature · 2 lines
-- 
2.47.3
#5 lore raw

PATCH v6 3/9 lib/xe: add xe_cgroup_region_name() helper

In reply to Thadeu Lima de Souza Cascardo

From: Thomas Hellström <[email protected]>

Add xe_cgroup_region_name(fd, region) which constructs the dmem cgroup region path for a given xe memory region. The returned string has the form "drm/<pci-slot>/<region>" (e.g. "drm/0000:03:00.0/vram0"), matching the name registered by the kernel via drmm_cgroup_register_region().

Only VRAM regions are tracked by the dmem controller; system and stolen memory regions return NULL.

Assisted-by GitHub Copilot:claude-sonnet-4.6
---
 lib/xe/xe_query.c | 32 ++++++++++++++++++++++++++++++++
 lib/xe/xe_query.h |  2 ++
 2 files changed, 34 insertions(+)
lib/xe/xe_query.c +32 -0
index 68e60ddc75a2..e2b25be21140 100644
@@ -7,6 +7,7 @@
  */
 
 #include <fcntl.h>
+#include <limits.h>
 #include <stdlib.h>
 #include <pthread.h>
 
@@ -21,6 +22,7 @@
 
 #include "drmtest.h"
 #include "igt_debugfs.h"
+#include "igt_device.h"
 #include "ioctl_wrappers.h"
 #include "igt_map.h"
 #include "intel_common.h"
@@ -1304,6 +1306,36 @@ int xe_query_eu_thread_count(int fd, int gt)
 		xe_hwconfig_lookup_value_u32(fd, INTEL_HWCONFIG_NUM_THREADS_PER_EU);
 }
 
+/**
+ * xe_cgroup_region_name() - Build the dmem cgroup region name for an xe memory region.
+ * @fd: xe device fd.
+ * @region: Region mask (as used by xe_mem_region(), xe_region_name(), etc.).
+ *
+ * Constructs the full dmem cgroup region path for @region on the device
+ * identified by @fd.  The returned string has the form
+ * ``drm/<pci-slot>/<region>`` (e.g. ``drm/0000:03:00.0/vram0``), matching
+ * the name registered by the kernel driver via drmm_cgroup_register_region().
+ *
+ * Only VRAM regions are registered with the dmem controller; passing a
+ * system-memory region returns %NULL.
+ *
+ * Return: A newly allocated string that the caller must free(), or %NULL if
+ * @region is not tracked by the dmem cgroup controller.
+ */
+char *xe_cgroup_region_name(int fd, uint64_t region)
+{
+	char pci_slot[NAME_MAX];
+	char *name;
+
+	if (xe_region_class(fd, region) != DRM_XE_MEM_REGION_CLASS_VRAM)
+		return NULL;
+
+	igt_device_get_pci_slot_name(fd, pci_slot);
+
+	igt_assert(asprintf(&name, "drm/%s/%s", pci_slot, xe_region_name(region)) > 0);
+	return name;
+}
+
 igt_constructor
 {
 	xe_device_cache_init();
lib/xe/xe_query.h +2 -0
index 59330d80fd1b..a6ef9cab9aa1 100644
@@ -206,4 +206,6 @@ void xe_device_put(int fd);
 int xe_query_eu_count(int fd, int gt);
 int xe_query_eu_thread_count(int fd, int gt);
 
+char *xe_cgroup_region_name(int fd, uint64_t region);
+
 #endif	/* XE_QUERY_H */
Signature · 1 line
2.47.3
#6 lore raw

PATCH v6 4/9 lib/xe: Introduce dmem driver and implement Xe support

In reply to Thadeu Lima de Souza Cascardo

In order to be reuse the same dmem tests with multiple drivers, we need to abstract a few operations. That includes getting the region name, and allocating and releasing VRAM. As there is some initialization also when multiple allocations are done, also provide init and deinit functions.

The Xe implementation was based on the original operations from xe_cgroups.c written by Thomas Hellström. However, instead of doing a deferred backing, followed by a bind, it does a simple non-deferred GEM object creation on the VRAM region.

---
 lib/igt_dmem_driver.h | 34 ++++++++++++++++++
 lib/meson.build       |  1 +
 lib/xe/xe_dmem.c      | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 132 insertions(+)
lib/igt_dmem_driver.h new file +34 -0
new file mode 100644
index 000000000000..e6998387eff9
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright 2026 Valve Corporation
+ * Authors:
+ *  Thadeu Lima de Souza Cascardo <[email protected]>
+ */
+
+#ifndef __IGT_DMEM_DRIVER_H__
+#define __IGT_DMEM_DRIVER_H__
+
+#include <stdlib.h>
+
+/**
+ * struct igt_dmem_driver - vendor driver to allocate and free device memory
+ *
+ */
+struct igt_dmem_driver {
+	/** @name: Driver name */
+	const char *name;
+	/** @init: Initialize an opaque context given a DRM device fd */
+	int (*init)(void **ctx, int fd);
+	/** @deinit: Release resources associated with context */
+	void (*deinit)(void *ctx);
+	/** @get_region_name: Return expected region name at dmem cgroup files */
+	char * (*get_region_name)(void *ctx);
+	/** @allocate_vram: Allocate @len sized vram and return an opaque @handle */
+	int (*allocate_vram)(void *ctx, size_t len, void **handle);
+	/** @free_vram: Free vram associated with @handle */
+	void (*free_vram)(void *ctx, void *handle);
+};
+
+extern const struct igt_dmem_driver xe_dmem_driver;
+
+#endif
lib/meson.build +1 -0
index b7e1be61d844..022408ce6864 100644
@@ -130,6 +130,7 @@ lib_sources = [
 	'igt_dsc.c',
 	'igt_hook.c',
 	'xe/xe_device.c',
+	'xe/xe_dmem.c',
 	'xe/xe_ggtt.c',
 	'xe/xe_gt.c',
 	'xe/xe_ioctl.c',
lib/xe/xe_dmem.c new file +97 -0

97 lines not on this page · raw message

Signature · 1 line
2.47.3
Reviewed-by on v6 4/9
#7 lore raw

Re: [PATCH i-g-t v6 4/9] lib/xe: Introduce dmem driver and implement Xe support

Quoted 38 lines · Thadeu Lima de Souza Cascardo

In order to be reuse the same dmem tests with multiple drivers, we need to abstract a few operations. That includes getting the region name, and allocating and releasing VRAM. As there is some initialization also when multiple allocations are done, also provide init and deinit functions.

The Xe implementation was based on the original operations from xe_cgroups.c written by Thomas Hellström. However, instead of doing a deferred backing, followed by a bind, it does a simple non-deferred GEM object creation on the VRAM region.

---
  lib/igt_dmem_driver.h | 34 ++++++++++++++++++
  lib/meson.build       |  1 +
  lib/xe/xe_dmem.c      | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++
  3 files changed, 132 insertions(+)
lib/igt_dmem_driver.h new file +15 -0
new file mode 100644
index 000000000000..e6998387eff9
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright 2026 Valve Corporation
+ * Authors:
+ *  Thadeu Lima de Souza Cascardo <[email protected]>
+ */
+
+#ifndef __IGT_DMEM_DRIVER_H__
+#define __IGT_DMEM_DRIVER_H__
+
+#include <stdlib.h>
+
+/**
+ * struct igt_dmem_driver - vendor driver to allocate and free device memory
+ *

lib/igt_dmem_driver.h line 25

Quoted 10 lines
lib/igt_dmem_driver.h new file +10 -0
+ */
+struct igt_dmem_driver {
+	/** @name: Driver name */
+	const char *name;
+	/** @init: Initialize an opaque context given a DRM device fd */
+	int (*init)(void **ctx, int fd);
+	/** @deinit: Release resources associated with context */
+	void (*deinit)(void *ctx);
+	/** @get_region_name: Return expected region name at dmem cgroup files */
+	char * (*get_region_name)(void *ctx);

Nit - mention it returns newly allocated memory caller must free?

But LGTM on the whole:

Regards,

Tvrtko

Quoted 125 lines
lib/igt_dmem_driver.h new file +9 -0
+	/** @allocate_vram: Allocate @len sized vram and return an opaque @handle */
+	int (*allocate_vram)(void *ctx, size_t len, void **handle);
+	/** @free_vram: Free vram associated with @handle */
+	void (*free_vram)(void *ctx, void *handle);
+};
+
+extern const struct igt_dmem_driver xe_dmem_driver;
+
+#endif
lib/meson.build +1 -0
index b7e1be61d844..022408ce6864 100644
@@ -130,6 +130,7 @@ lib_sources = [
  	'igt_dsc.c',
  	'igt_hook.c',
  	'xe/xe_device.c',
+	'xe/xe_dmem.c',
  	'xe/xe_ggtt.c',
  	'xe/xe_gt.c',
  	'xe/xe_ioctl.c',
lib/xe/xe_dmem.c new file +97 -0
new file mode 100644
index 000000000000..628c905997d4
@@ -0,0 +1,97 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright 2026 Valve Corporation
+ * Authors:
+ *  Thadeu Lima de Souza Cascardo <[email protected]>
+ */
+
+#include <errno.h>
+
+#include "igt.h"
+#include "igt_cgroup.h"
+#include "igt_dmem_driver.h"
+#include "xe_drm.h"
+#include "xe/xe_ioctl.h"
+#include "xe/xe_query.h"
+
+struct xe_dmem_ctx {
+	int fd;
+	uint64_t vram_region;
+};
+
+static int xe_dmem_init(void **ctx, int fd)
+{
+	struct xe_dmem_ctx *xe_ctx;
+	uint64_t region;
+
+	xe_ctx = malloc(sizeof(*xe_ctx));
+	if (!xe_ctx)
+		return -ENOMEM;
+
+	xe_ctx->vram_region = 0;
+	/* Find first VRAM region */
+	xe_for_each_mem_region(fd, all_memory_regions(fd), region) {
+		if (xe_region_class(fd, region) == DRM_XE_MEM_REGION_CLASS_VRAM) {
+			xe_ctx->vram_region = region;
+			break;
+		}
+	}
+	if (!xe_ctx->vram_region)
+		goto out;
+
+	xe_ctx->fd = fd;
+
+	*ctx = xe_ctx;
+
+	return 0;
+
+out:
+	free(xe_ctx);
+
+	return -ENOMEM;
+}
+
+static void xe_dmem_deinit(void *ctx)
+{
+	struct xe_dmem_ctx *xe_ctx = ctx;
+
+	free(xe_ctx);
+}
+
+static char * xe_dmem_get_region_name(void *ctx)
+{
+	struct xe_dmem_ctx *xe_ctx = ctx;
+
+	return xe_cgroup_region_name(xe_ctx->fd, xe_ctx->vram_region);
+}
+
+static int xe_dmem_allocate_vram(void *ctx, size_t len, void **ret_handle)
+{
+	struct xe_dmem_ctx *xe_ctx = ctx;
+	uint32_t handle;
+	int err;
+
+	err = __xe_bo_create(xe_ctx->fd, 0, len, xe_ctx->vram_region, 0,
+			     NULL, &handle);
+	if (err)
+		return err;
+
+	*ret_handle = (void *)(uintptr_t) handle;
+	return 0;
+}
+
+static void xe_dmem_free_vram(void *ctx, void *handle)
+{
+	struct xe_dmem_ctx *xe_ctx = ctx;
+
+	gem_close(xe_ctx->fd, (uint32_t)(uintptr_t) handle);
+}
+
+const struct igt_dmem_driver xe_dmem_driver = {
+	.name = "xe",
+	.get_region_name = xe_dmem_get_region_name,
+	.init = xe_dmem_init,
+	.deinit = xe_dmem_deinit,
+	.allocate_vram = xe_dmem_allocate_vram,
+	.free_vram = xe_dmem_free_vram,
+};
#8 lore raw

Re: [PATCH i-g-t v6 4/9] lib/xe: Introduce dmem driver and implement Xe support

In reply to Thadeu Lima de Souza Cascardo

Hi Thadeu,

Quoted 24 lines · Thadeu Lima de Souza Cascardo

In order to be reuse the same dmem tests with multiple drivers, we need to abstract a few operations. That includes getting the region name, and allocating and releasing VRAM. As there is some initialization also when multiple allocations are done, also provide init and deinit functions.

The Xe implementation was based on the original operations from xe_cgroups.c written by Thomas Hellström. However, instead of doing a deferred backing, followed by a bind, it does a simple non-deferred GEM object creation on the VRAM region.

---
 lib/igt_dmem_driver.h | 34 ++++++++++++++++++
 lib/meson.build       |  1 +
 lib/xe/xe_dmem.c      | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 132 insertions(+)
lib/igt_dmem_driver.h new file +1 -0
new file mode 100644
index 000000000000..e6998387eff9
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: MIT

lib/igt_dmem_driver.h line 11

Quoted 10 lines
lib/igt_dmem_driver.h new file +10 -0
+/*
+ * Copyright 2026 Valve Corporation
+ * Authors:
+ *  Thadeu Lima de Souza Cascardo <[email protected]>
+ */
+
+#ifndef __IGT_DMEM_DRIVER_H__
+#define __IGT_DMEM_DRIVER_H__
+
+#include <stdlib.h>

Please drop this include from header, and if needed move to .c lib.

Regards,
Kamil

Quoted 142 lines
lib/igt_dmem_driver.h new file +23 -0
+
+/**
+ * struct igt_dmem_driver - vendor driver to allocate and free device memory
+ *
+ */
+struct igt_dmem_driver {
+	/** @name: Driver name */
+	const char *name;
+	/** @init: Initialize an opaque context given a DRM device fd */
+	int (*init)(void **ctx, int fd);
+	/** @deinit: Release resources associated with context */
+	void (*deinit)(void *ctx);
+	/** @get_region_name: Return expected region name at dmem cgroup files */
+	char * (*get_region_name)(void *ctx);
+	/** @allocate_vram: Allocate @len sized vram and return an opaque @handle */
+	int (*allocate_vram)(void *ctx, size_t len, void **handle);
+	/** @free_vram: Free vram associated with @handle */
+	void (*free_vram)(void *ctx, void *handle);
+};
+
+extern const struct igt_dmem_driver xe_dmem_driver;
+
+#endif
lib/meson.build +1 -0
index b7e1be61d844..022408ce6864 100644
@@ -130,6 +130,7 @@ lib_sources = [
 	'igt_dsc.c',
 	'igt_hook.c',
 	'xe/xe_device.c',
+	'xe/xe_dmem.c',
 	'xe/xe_ggtt.c',
 	'xe/xe_gt.c',
 	'xe/xe_ioctl.c',
lib/xe/xe_dmem.c new file +97 -0
new file mode 100644
index 000000000000..628c905997d4
@@ -0,0 +1,97 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright 2026 Valve Corporation
+ * Authors:
+ *  Thadeu Lima de Souza Cascardo <[email protected]>
+ */
+
+#include <errno.h>
+
+#include "igt.h"
+#include "igt_cgroup.h"
+#include "igt_dmem_driver.h"
+#include "xe_drm.h"
+#include "xe/xe_ioctl.h"
+#include "xe/xe_query.h"
+
+struct xe_dmem_ctx {
+	int fd;
+	uint64_t vram_region;
+};
+
+static int xe_dmem_init(void **ctx, int fd)
+{
+	struct xe_dmem_ctx *xe_ctx;
+	uint64_t region;
+
+	xe_ctx = malloc(sizeof(*xe_ctx));
+	if (!xe_ctx)
+		return -ENOMEM;
+
+	xe_ctx->vram_region = 0;
+	/* Find first VRAM region */
+	xe_for_each_mem_region(fd, all_memory_regions(fd), region) {
+		if (xe_region_class(fd, region) == DRM_XE_MEM_REGION_CLASS_VRAM) {
+			xe_ctx->vram_region = region;
+			break;
+		}
+	}
+	if (!xe_ctx->vram_region)
+		goto out;
+
+	xe_ctx->fd = fd;
+
+	*ctx = xe_ctx;
+
+	return 0;
+
+out:
+	free(xe_ctx);
+
+	return -ENOMEM;
+}
+
+static void xe_dmem_deinit(void *ctx)
+{
+	struct xe_dmem_ctx *xe_ctx = ctx;
+
+	free(xe_ctx);
+}
+
+static char * xe_dmem_get_region_name(void *ctx)
+{
+	struct xe_dmem_ctx *xe_ctx = ctx;
+
+	return xe_cgroup_region_name(xe_ctx->fd, xe_ctx->vram_region);
+}
+
+static int xe_dmem_allocate_vram(void *ctx, size_t len, void **ret_handle)
+{
+	struct xe_dmem_ctx *xe_ctx = ctx;
+	uint32_t handle;
+	int err;
+
+	err = __xe_bo_create(xe_ctx->fd, 0, len, xe_ctx->vram_region, 0,
+			     NULL, &handle);
+	if (err)
+		return err;
+
+	*ret_handle = (void *)(uintptr_t) handle;
+	return 0;
+}
+
+static void xe_dmem_free_vram(void *ctx, void *handle)
+{
+	struct xe_dmem_ctx *xe_ctx = ctx;
+
+	gem_close(xe_ctx->fd, (uint32_t)(uintptr_t) handle);
+}
+
+const struct igt_dmem_driver xe_dmem_driver = {
+	.name = "xe",
+	.get_region_name = xe_dmem_get_region_name,
+	.init = xe_dmem_init,
+	.deinit = xe_dmem_deinit,
+	.allocate_vram = xe_dmem_allocate_vram,
+	.free_vram = xe_dmem_free_vram,
+};
Signature · 2 lines
-- 
2.47.3
#9 lore raw

PATCH v6 5/9 lib/amdgpu: add amdgpu_cgroup_region_name

In reply to Thadeu Lima de Souza Cascardo

The amdgpu dmem region name uses its PCI address, just like the one from Xe, but there is only a single VRAM region.

---
 lib/amdgpu/amd_memory.c | 25 +++++++++++++++++++++++++
 lib/amdgpu/amd_memory.h |  2 ++
 2 files changed, 27 insertions(+)
lib/amdgpu/amd_memory.c +25 -0
index 12fe23c65ab2..2da4a8a4baee 100644
@@ -30,9 +30,11 @@
 #include <amdgpu_drm.h>
 #include <stdio.h>
 #include <string.h>
+#include <limits.h>
 #include <unistd.h>
 #include <sys/mman.h>
 #include <inttypes.h>
+#include "igt_device.h"
 
 /**
  *
@@ -679,6 +681,29 @@ bool virtual_free_memory(void *address, unsigned int size)
 	}
 }
 
+/**
+ * amdgpu_cgroup_region_name() - Build the dmem cgroup region name for an amdgpu.
+ * @fd: amdgpu device fd.
+ *
+ * Constructs the full dmem cgroup region path for VRAM on the device
+ * identified by @fd.  The returned string has the form
+ * ``drm/<pci-slot>/vram`` (e.g. ``drm/0000:03:00.0/vram``), matching
+ * the name registered by the kernel driver via drmm_cgroup_register_region().
+ *
+ * Return: A newly allocated string that the caller must free(), or %NULL if
+ * @region is not tracked by the dmem cgroup controller.
+ */
+char *amdgpu_cgroup_region_name(int fd)
+{
+	char pci_slot[NAME_MAX];
+	char *name;
+
+	igt_device_get_pci_slot_name(fd, pci_slot);
+
+	igt_assert(asprintf(&name, "drm/%s/vram", pci_slot) > 0);
+	return name;
+}
+
 /**
  * Wait for specific value in memory with timeout
  */
lib/amdgpu/amd_memory.h +2 -0
index e26c85bc4b0a..de169e580c1b 100644
@@ -105,6 +105,8 @@ void
 bool
 virtual_free_memory(void *address, unsigned int size);
 
+char *amdgpu_cgroup_region_name(int fd);
+
 bool
 wait_on_value(unsigned int *ptr, unsigned int expected);
 #endif
Signature · 1 line
2.47.3
Reviewed-by on v6 5/9
#10 lore raw

Re: [PATCH i-g-t v6 5/9] lib/amdgpu: add amdgpu_cgroup_region_name

Quoted 61 lines · Thadeu Lima de Souza Cascardo

The amdgpu dmem region name uses its PCI address, just like the one from Xe, but there is only a single VRAM region.

---
  lib/amdgpu/amd_memory.c | 25 +++++++++++++++++++++++++
  lib/amdgpu/amd_memory.h |  2 ++
  2 files changed, 27 insertions(+)
lib/amdgpu/amd_memory.c +25 -0
index 12fe23c65ab2..2da4a8a4baee 100644
@@ -30,9 +30,11 @@
  #include <amdgpu_drm.h>
  #include <stdio.h>
  #include <string.h>
+#include <limits.h>
  #include <unistd.h>
  #include <sys/mman.h>
  #include <inttypes.h>
+#include "igt_device.h"
 
  /**
   *
@@ -679,6 +681,29 @@ bool virtual_free_memory(void *address, unsigned int size)
  	}
  }
 
+/**
+ * amdgpu_cgroup_region_name() - Build the dmem cgroup region name for an amdgpu.
+ * @fd: amdgpu device fd.
+ *
+ * Constructs the full dmem cgroup region path for VRAM on the device
+ * identified by @fd.  The returned string has the form
+ * ``drm/<pci-slot>/vram`` (e.g. ``drm/0000:03:00.0/vram``), matching
+ * the name registered by the kernel driver via drmm_cgroup_register_region().
+ *
+ * Return: A newly allocated string that the caller must free(), or %NULL if
+ * @region is not tracked by the dmem cgroup controller.
+ */
+char *amdgpu_cgroup_region_name(int fd)
+{
+	char pci_slot[NAME_MAX];
+	char *name;
+
+	igt_device_get_pci_slot_name(fd, pci_slot);
+
+	igt_assert(asprintf(&name, "drm/%s/vram", pci_slot) > 0);
+	return name;
+}
+
  /**
   * Wait for specific value in memory with timeout
   */
lib/amdgpu/amd_memory.h +0 -0
index e26c85bc4b0a..de169e580c1b 100644

lib/amdgpu/amd_memory.h void · line 112

Quoted 8 lines
lib/amdgpu/amd_memory.h +2 -0
@@ -105,6 +105,8 @@ void
  bool
  virtual_free_memory(void *address, unsigned int size);
 
+char *amdgpu_cgroup_region_name(int fd);
+
  bool
  wait_on_value(unsigned int *ptr, unsigned int expected);
  #endif

Regards,

Tvrtko

#11 lore raw

PATCH v6 6/9 lib/amdgpu: add amdgpu support to igt_dmem_driver

In reply to Thadeu Lima de Souza Cascardo

This allows dmem cgroups tests to run on top of amdgpu driver, adding support to allocate and release VRAM memory.

This does this by allocating a BO from VRAM domain, which will try to place BOs on VRAM, but may fallback to GTT.

---
 lib/amdgpu/amd_dmem.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/igt_dmem_driver.h |  1 +
 lib/meson.build       |  1 +
 3 files changed, 92 insertions(+)
lib/amdgpu/amd_dmem.c new file +90 -0
new file mode 100644
index 000000000000..cb0fed4b870a
@@ -0,0 +1,90 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright 2026 Valve Corporation
+ * Authors:
+ *  Thadeu Lima de Souza Cascardo <[email protected]>
+ */
+
+#include <errno.h>
+
+#include "igt.h"
+#include "igt_cgroup.h"
+#include "igt_dmem_driver.h"
+#include "lib/amdgpu/amd_memory.h"
+
+struct amdgpu_dmem_ctx {
+	int fd;
+	amdgpu_device_handle device;
+};
+
+static int amdgpu_dmem_init(void **ctx, int fd)
+{
+	struct amdgpu_dmem_ctx *actx;
+	uint32_t major, minor;
+	int err;
+
+	actx = malloc(sizeof(*actx));
+	if (!actx)
+		return -ENOMEM;
+
+	err = amdgpu_device_initialize(fd, &major, &minor, &actx->device);
+	if (err)
+		goto out;
+
+	actx->fd = fd;
+
+	*ctx = actx;
+
+	return 0;
+
+out:
+	free(actx);
+
+	return err;
+}
+
+static void amdgpu_dmem_deinit(void *ctx)
+{
+	struct amdgpu_dmem_ctx *actx = ctx;
+
+	amdgpu_device_deinitialize(actx->device);
+	free(actx);
+}
+
+static char * amdgpu_dmem_get_region_name(void *ctx)
+{
+	struct amdgpu_dmem_ctx *actx = ctx;
+
+	return amdgpu_cgroup_region_name(actx->fd);
+}
+
+static int amdgpu_dmem_allocate_vram(void *ctx, size_t len, void **ret_handle)
+{
+	struct amdgpu_dmem_ctx *actx = ctx;
+	amdgpu_bo_handle handle;
+	int err;
+
+	err = amdgpu_bo_alloc_wrap(actx->device, len, 4096,
+				   AMDGPU_GEM_DOMAIN_VRAM, 0, &handle);
+	if (err)
+		return err;
+
+	if (ret_handle)
+		*ret_handle = (void *) handle;
+
+	return 0;
+}
+
+static void amdgpu_dmem_free_vram(void *ctx, void *handle)
+{
+	amdgpu_bo_free(handle);
+}
+
+const struct igt_dmem_driver amdgpu_dmem_driver = {
+	.name = "amdgpu",
+	.get_region_name = amdgpu_dmem_get_region_name,
+	.init = amdgpu_dmem_init,
+	.deinit = amdgpu_dmem_deinit,
+	.allocate_vram = amdgpu_dmem_allocate_vram,
+	.free_vram = amdgpu_dmem_free_vram,
+};
lib/igt_dmem_driver.h +1 -0
index e6998387eff9..f2d9d74196db 100644
@@ -30,5 +30,6 @@ struct igt_dmem_driver {
 };
 
 extern const struct igt_dmem_driver xe_dmem_driver;
+extern const struct igt_dmem_driver amdgpu_dmem_driver;
 
 #endif
lib/meson.build +1 -0
index 022408ce6864..e191793f0097 100644
@@ -205,6 +205,7 @@ if libdrm_amdgpu.found()
 		'amdgpu/amd_mmd_shared.c',
 		'amdgpu/amd_jpeg_shared.c',
 		'amdgpu/amd_utils.c',
+		'amdgpu/amd_dmem.c',
 		'amdgpu/amd_vcn_shared.c'
 	]
 	if libdrm_amdgpu.version().version_compare('> 2.4.99')
Signature · 1 line
2.47.3
Reviewed-by on v6 6/9
#12 lore raw

Re: [PATCH i-g-t v6 6/9] lib/amdgpu: add amdgpu support to igt_dmem_driver

Quoted 82 lines · Thadeu Lima de Souza Cascardo

This allows dmem cgroups tests to run on top of amdgpu driver, adding support to allocate and release VRAM memory.

This does this by allocating a BO from VRAM domain, which will try to place BOs on VRAM, but may fallback to GTT.

---
  lib/amdgpu/amd_dmem.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++
  lib/igt_dmem_driver.h |  1 +
  lib/meson.build       |  1 +
  3 files changed, 92 insertions(+)
lib/amdgpu/amd_dmem.c new file +63 -0
new file mode 100644
index 000000000000..cb0fed4b870a
@@ -0,0 +1,90 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright 2026 Valve Corporation
+ * Authors:
+ *  Thadeu Lima de Souza Cascardo <[email protected]>
+ */
+
+#include <errno.h>
+
+#include "igt.h"
+#include "igt_cgroup.h"
+#include "igt_dmem_driver.h"
+#include "lib/amdgpu/amd_memory.h"
+
+struct amdgpu_dmem_ctx {
+	int fd;
+	amdgpu_device_handle device;
+};
+
+static int amdgpu_dmem_init(void **ctx, int fd)
+{
+	struct amdgpu_dmem_ctx *actx;
+	uint32_t major, minor;
+	int err;
+
+	actx = malloc(sizeof(*actx));
+	if (!actx)
+		return -ENOMEM;
+
+	err = amdgpu_device_initialize(fd, &major, &minor, &actx->device);
+	if (err)
+		goto out;
+
+	actx->fd = fd;
+
+	*ctx = actx;
+
+	return 0;
+
+out:
+	free(actx);
+
+	return err;
+}
+
+static void amdgpu_dmem_deinit(void *ctx)
+{
+	struct amdgpu_dmem_ctx *actx = ctx;
+
+	amdgpu_device_deinitialize(actx->device);
+	free(actx);
+}
+
+static char * amdgpu_dmem_get_region_name(void *ctx)
+{
+	struct amdgpu_dmem_ctx *actx = ctx;
+
+	return amdgpu_cgroup_region_name(actx->fd);
+}
+
+static int amdgpu_dmem_allocate_vram(void *ctx, size_t len, void **ret_handle)
+{
+	struct amdgpu_dmem_ctx *actx = ctx;

lib/amdgpu/amd_dmem.c line 73

Quoted 10 lines
lib/amdgpu/amd_dmem.c new file +10 -0
+	amdgpu_bo_handle handle;
+	int err;
+
+	err = amdgpu_bo_alloc_wrap(actx->device, len, 4096,
+				   AMDGPU_GEM_DOMAIN_VRAM, 0, &handle);
+	if (err)
+		return err;
+
+	if (ret_handle)
+		*ret_handle = (void *) handle;

Is there an use case for ret_handle == NULL? Xe backend does not bother with the check. This aside, the rest looks good to me:

Regards,

Tvrtko

Quoted 41 lines
lib/amdgpu/amd_dmem.c new file +17 -0
+
+	return 0;
+}
+
+static void amdgpu_dmem_free_vram(void *ctx, void *handle)
+{
+	amdgpu_bo_free(handle);
+}
+
+const struct igt_dmem_driver amdgpu_dmem_driver = {
+	.name = "amdgpu",
+	.get_region_name = amdgpu_dmem_get_region_name,
+	.init = amdgpu_dmem_init,
+	.deinit = amdgpu_dmem_deinit,
+	.allocate_vram = amdgpu_dmem_allocate_vram,
+	.free_vram = amdgpu_dmem_free_vram,
+};
lib/igt_dmem_driver.h +1 -0
index e6998387eff9..f2d9d74196db 100644
@@ -30,5 +30,6 @@ struct igt_dmem_driver {
  };
 
  extern const struct igt_dmem_driver xe_dmem_driver;
+extern const struct igt_dmem_driver amdgpu_dmem_driver;
 
  #endif
lib/meson.build +1 -0
index 022408ce6864..e191793f0097 100644
@@ -205,6 +205,7 @@ if libdrm_amdgpu.found()
  		'amdgpu/amd_mmd_shared.c',
  		'amdgpu/amd_jpeg_shared.c',
  		'amdgpu/amd_utils.c',
+		'amdgpu/amd_dmem.c',
  		'amdgpu/amd_vcn_shared.c'
  	]
  	if libdrm_amdgpu.version().version_compare('> 2.4.99')
#13 lore raw

PATCH v6 7/9 tests/cgroup_dmem: add test for dmem.current

In reply to Thadeu Lima de Souza Cascardo

Based on the work of Thomas Hellström to test dmem.max eviction, add a test for dmem.current usage after allocations and setting dmem.max.

Create a dmem cgroup, allocate close to capacity (or at most 4GiB), check current usage is within a small slack of the expected allocation. Then, set max to a small value and check allocations and current usage are limited to the max set. Set max to less than a single BO size, then check no allocations are allowed and current usage is also within the slack. After each allocation, release memory and check current usage has gone down.

---
 tests/cgroup_dmem.c | 262 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 257 insertions(+), 5 deletions(-)
tests/cgroup_dmem.c +257 -5
index 442c965f9bbf..ba5e6a2e3deb 100644
@@ -1,6 +1,8 @@
 // SPDX-License-Identifier: MIT
 /*
  * Copyright © 2025 Intel Corporation
+ * Copyright © 2026 Intel Corporation
+ * Copyright 2026 Valve Corporation
  */
 
 /**
@@ -17,10 +19,217 @@
  * Test category: uapi
  */
 
+#include <errno.h>
 #include <inttypes.h>
+#include <signal.h>
+#include <stdatomic.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
 
+#include "drmtest.h"
 #include "igt.h"
+#include "igt_aux.h"
 #include "igt_cgroup.h"
+#include "igt_dmem_driver.h"
+
+#define BO_SIZE			SZ_64M
+#define MAX_LIMIT		((uint64_t)4 * SZ_1G)
+#define USAGE_POLL_MS		10
+#define USAGE_DROP_TIMEOUT_MS	1000
+
+/**
+ * SUBTEST: simple
+ * DESCRIPTION:
+ *  Creates a cgroup, moves the process into it, enumerates all dmem regions,
+ *  prints their capacity, system-wide current usage, per-cgroup current usage
+ *  and configured limits, then destroys the cgroup.
+ */
+
+/**
+ * SUBTEST: current
+ * DESCRIPTION:
+ *  Create a dmem cgroup, allocate close to capacity (or at most 4GiB),
+ *  check current usage is within a small slack of the expected allocation.
+ *  Then, set max to a small value and check allocations and current usage
+ *  are limited to the max set.
+ *  Set max to less than a single BO size, then check no allocations are allowed
+ *  and current usage is also within the slack.
+ *  After each allocation, release memory and check current usage has gone
+ *  down.
+ * REQUIREMENTS: xe or amdgpu device with at least one VRAM region
+ */
+
+static uint64_t wait_for_usage_drop(struct igt_cgroup *cg, const char *region,
+				    uint64_t limit)
+{
+	uint64_t current;
+	unsigned int elapsed = 0;
+
+	do {
+		igt_cgroup_dmem_get_current(cg, region, &current);
+		if (current <= limit)
+			return current;
+		usleep(USAGE_POLL_MS * 1000);
+		elapsed += USAGE_POLL_MS;
+	} while (elapsed < USAGE_DROP_TIMEOUT_MS);
+
+	return current;
+}
+
+static int allocate_vram(void **handles, const struct igt_dmem_driver *drv,
+			 void *ctx, int max_bo, size_t len)
+{
+	int i, err = 0;
+	for (i = 0; i < max_bo; i++) {
+		err = drv->allocate_vram(ctx, len, &handles[i]);
+		if (err)
+			break;
+	}
+	/* These are expected failures we can ignore. */
+	if (err == -ENOMEM || err == -ENOSPC)
+		err = 0;
+	if (!err)
+		return i;
+	for (i--; i >= 0; i--)
+		drv->free_vram(ctx, handles[i]);
+	return err;
+}
+
+static void free_vram(void **handles, const struct igt_dmem_driver *drv,
+		      void *ctx, int max_bo)
+{
+	int i;
+	for (i = 0; i < max_bo; i++)
+		drv->free_vram(ctx, handles[i]);
+}
+
+static void test_current(int fd, char *cg_region, unsigned int flags, const struct igt_dmem_driver *drv, void *ctx)
+{
+	struct igt_cgroup *cg;
+	void **handles;
+	uint64_t current, capacity, cg_max;
+	int n_bo = 0, max_bo;
+
+	igt_cgroup_dmem_get_capacity(cg_region, &capacity);
+	igt_require_f(capacity >= 4 * BO_SIZE,
+		      "VRAM capacity (%"PRIu64" MiB) too small to test\n",
+		      capacity / SZ_1M);
+
+	/*
+	 * Use up to 4 GiB, or the full capacity if the device has less.
+	 * Leave one BO_SIZE worth of headroom so the device isn't completely
+	 * exhausted before the cgroup limit is hit.
+	 */
+	cg_max = min(MAX_LIMIT, capacity - BO_SIZE);
+	cg_max = ALIGN_DOWN(cg_max, BO_SIZE);
+
+	/* Create cgroup and move into it */
+	cg = igt_cgroup_new("igt_cgroups_test");
+	igt_cgroup_move_current(cg);
+
+	max_bo = cg_max / BO_SIZE;
+
+	handles = calloc(max_bo, sizeof(handles[0]));
+	igt_assert_f(handles, "failed to allocate handles array");
+
+	n_bo = allocate_vram(handles, drv, ctx, max_bo, BO_SIZE);
+	igt_assert_f(n_bo > 0, "failed to allocate VRAM\n");
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After fill: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current == cg_max,
+		     "current usage (%"PRIu64" MiB) is not requested allocation (%"PRIu64" MiB)\n",
+		     current / SZ_1M, cg_max / SZ_1M);
+
+	free_vram(handles, drv, ctx, n_bo);
+	wait_for_usage_drop(cg, cg_region, 0);
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After free: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current == 0,
+		     "current usage (%"PRIu64" MiB) is not zero\n",
+		     current / SZ_1M);
+
+	/* Allow for a slack as there might be some extra pages allocated. */
+	igt_cgroup_dmem_set_max(cg, cg_region, 2 * BO_SIZE, false);
+
+	n_bo = allocate_vram(handles, drv, ctx, max_bo, BO_SIZE);
+	igt_assert_f(n_bo > 0, "failed to allocate VRAM\n");
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After fill: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current == 2 * BO_SIZE,
+		     "current usage (%"PRIu64" MiB) is not requested max allocation (%"PRIu64" MiB)\n",
+		     current / SZ_1M, cg_max / SZ_1M);
+
+	free_vram(handles, drv, ctx, n_bo);
+	wait_for_usage_drop(cg, cg_region, 0);
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After free: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current == 0,
+		     "current usage (%"PRIu64" MiB) is not zero\n",
+		     current / SZ_1M);
+
+	igt_cgroup_dmem_set_max(cg, cg_region, 0, false);
+
+	n_bo = allocate_vram(handles, drv, ctx, max_bo, BO_SIZE);
+
+	/*
+	 * amdgpu may succeed the allocation, by falling back to GTT, so no assertion here.
+	 * Verify by reading current usage.
+	 */
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After fill: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current == 0,
+		     "current usage (%"PRIu64" MiB) is not zero\n",
+		     current / SZ_1M);
+
+	if (n_bo > 0)
+		free_vram(handles, drv, ctx, n_bo);
+	wait_for_usage_drop(cg, cg_region, 0);
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After free: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current == 0,
+		     "current usage (%"PRIu64" MiB) is not zero\n",
+		     current / SZ_1M);
+
+	igt_cgroup_free(cg);
+}
+
+static const struct {
+	const char *name;
+	void (*test_fn)(int fd, char *cg_region, unsigned int flags, const struct igt_dmem_driver *drv, void *ctx);
+	unsigned int flags;
+} subtests[] = {
+	{ "current", test_current, 0 },
+	{ }
+};
+
+static const struct {
+	int driver_flag;
+	const struct igt_dmem_driver *driver;
+} drivers[] = {
+	{ DRIVER_XE, &xe_dmem_driver },
+	{ DRIVER_AMDGPU, &amdgpu_dmem_driver },
+	{ },
+};
 
 IGT_TEST_DESCRIPTION("Exercises the cgroup v2 dmem controller interface.");
 
@@ -32,7 +241,7 @@ static void fmt_bytes(uint64_t v, char *buf, size_t len)
 		snprintf(buf, len, "%" PRIu64, v);
 }
 
-int igt_simple_main()
+static void simple_cgroup(void)
 {
 	struct igt_cgroup *cg;
 	const char *region;
@@ -42,10 +251,6 @@ int igt_simple_main()
 	char min_s[32], low_s[32], max_s[32];
 	int i;
 
-	igt_require_f(igt_cgroup_dmem_available(),
-		      "No dmem regions found; is cgroup v2 with the "
-		      "dmem controller available?\n");
-
 	cg = igt_cgroup_new("igt-cgroup-dmem-test");
 	igt_assert_f(cg, "Failed to create cgroup\n");
 
@@ -90,3 +295,50 @@ int igt_simple_main()
 	igt_cgroup_dmem_regions_free(regions);
 	igt_cgroup_free(cg);
 }
+
+int igt_main()
+{
+	igt_fixture() {
+		igt_require_f(getuid() == 0, "Test requires root\n");
+		/* Check dmem cgroup controller is available before doing anything else */
+		igt_require_f(igt_cgroup_dmem_available(),
+			      "dmem cgroup controller not available (no cgroup v2 or no registered regions)\n");
+
+	}
+
+	igt_subtest("simple")
+		simple_cgroup();
+
+	for (int d = 0; drivers[d].driver; d++) {
+		igt_subtest_group() {
+			int fd = -1;
+			int ret = -1;
+			char *cg_region = NULL;
+			void *ctx = NULL;
+			igt_fixture() {
+				fd = drm_open_driver(drivers[d].driver_flag);
+				igt_require_f(fd >= 0,
+					"No %s device found, skipping\n",
+					drivers[d].driver->name);
+				ret = drivers[d].driver->init(&ctx, fd);
+				igt_require_f(ret == 0,
+					"Failed to initialize %s device, skipping\n",
+					drivers[d].driver->name);
+				cg_region = drivers[d].driver->get_region_name(ctx);
+				igt_require_f(cg_region, "Region not tracked by dmem cgroup controller\n");
+			}
+
+			for (int i = 0; subtests[i].name; i++)
+				igt_subtest_f("%s-%s", drivers[d].driver->name, subtests[i].name)
+					subtests[i].test_fn(fd, cg_region, subtests[i].flags, drivers[d].driver, ctx);
+
+			igt_fixture() {
+				if (!ret)
+					drivers[d].driver->deinit(ctx);
+				if (fd >= 0)
+					drm_close_driver(fd);
+				free(cg_region);
+			}
+		}
+	}
+}
Signature · 1 line
2.47.3
#14 lore raw

Re: [PATCH i-g-t v6 7/9] tests/cgroup_dmem: add test for dmem.current

Quoted 21 lines · Thadeu Lima de Souza Cascardo

Based on the work of Thomas Hellström to test dmem.max eviction, add a test for dmem.current usage after allocations and setting dmem.max.

Create a dmem cgroup, allocate close to capacity (or at most 4GiB), check current usage is within a small slack of the expected allocation. Then, set max to a small value and check allocations and current usage are limited to the max set. Set max to less than a single BO size, then check no allocations are allowed and current usage is also within the slack. After each allocation, release memory and check current usage has gone down.

---
  tests/cgroup_dmem.c | 262 +++++++++++++++++++++++++++++++++++++++++++++++++++-
  1 file changed, 257 insertions(+), 5 deletions(-)
tests/cgroup_dmem.c +0 -0
index 442c965f9bbf..ba5e6a2e3deb 100644

tests/cgroup_dmem.c line 4

Quoted 4 lines
tests/cgroup_dmem.c +1 -0
@@ -1,6 +1,8 @@
  // SPDX-License-Identifier: MIT
  /*
   * Copyright © 2025 Intel Corporation
+ * Copyright © 2026 Intel Corporation

This is from a patch by Thomas? I would put a Co-developed-by: or his SoB even if that is agreeable since Intel's copyright is being recorded.

Quoted 38 lines
tests/cgroup_dmem.c +27 -0
+ * Copyright 2026 Valve Corporation
   */
 
  /**
@@ -17,10 +19,217 @@
   * Test category: uapi
   */
 
+#include <errno.h>
  #include <inttypes.h>
+#include <signal.h>
+#include <stdatomic.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
 
+#include "drmtest.h"
  #include "igt.h"
+#include "igt_aux.h"
  #include "igt_cgroup.h"
+#include "igt_dmem_driver.h"
+
+#define BO_SIZE			SZ_64M
+#define MAX_LIMIT		((uint64_t)4 * SZ_1G)
+#define USAGE_POLL_MS		10
+#define USAGE_DROP_TIMEOUT_MS	1000
+
+/**
+ * SUBTEST: simple
+ * DESCRIPTION:
+ *  Creates a cgroup, moves the process into it, enumerates all dmem regions,
+ *  prints their capacity, system-wide current usage, per-cgroup current usage
+ *  and configured limits, then destroys the cgroup.
+ */
+
+/**
+ * SUBTEST: current

tests/cgroup_dmem.c line 61

Quoted 10 lines
tests/cgroup_dmem.c +10 -0
+ * DESCRIPTION:
+ *  Create a dmem cgroup, allocate close to capacity (or at most 4GiB),
+ *  check current usage is within a small slack of the expected allocation.
+ *  Then, set max to a small value and check allocations and current usage
+ *  are limited to the max set.
+ *  Set max to less than a single BO size, then check no allocations are allowed
+ *  and current usage is also within the slack.
+ *  After each allocation, release memory and check current usage has gone
+ *  down.
+ * REQUIREMENTS: xe or amdgpu device with at least one VRAM region

I am out of touch the IGT documentation requirements but assuming the REQUIREMENTS: is real, I would suggest instead of "xe or amdgpu device" you put something like "a device supported by igt_dmem_driver.c" or something like that.

Quoted 13 lines
tests/cgroup_dmem.c +13 -0
+ */
+
+static uint64_t wait_for_usage_drop(struct igt_cgroup *cg, const char *region,
+				    uint64_t limit)
+{
+	uint64_t current;
+	unsigned int elapsed = 0;
+
+	do {
+		igt_cgroup_dmem_get_current(cg, region, &current);
+		if (current <= limit)
+			return current;
+		usleep(USAGE_POLL_MS * 1000);

tests/cgroup_dmem.c line 84

Quoted 10 lines
tests/cgroup_dmem.c +10 -0
+		elapsed += USAGE_POLL_MS;
+	} while (elapsed < USAGE_DROP_TIMEOUT_MS);
+
+	return current;
+}
+
+static int allocate_vram(void **handles, const struct igt_dmem_driver *drv,
+			 void *ctx, int max_bo, size_t len)
+{
+	int i, err = 0;

Nit - blank lines between the declarations and bodies are a bit inconsistent in the patch.

Quoted 22 lines
tests/cgroup_dmem.c +22 -0
+	for (i = 0; i < max_bo; i++) {
+		err = drv->allocate_vram(ctx, len, &handles[i]);
+		if (err)
+			break;
+	}
+	/* These are expected failures we can ignore. */
+	if (err == -ENOMEM || err == -ENOSPC)
+		err = 0;
+	if (!err)
+		return i;
+	for (i--; i >= 0; i--)
+		drv->free_vram(ctx, handles[i]);
+	return err;
+}
+
+static void free_vram(void **handles, const struct igt_dmem_driver *drv,
+		      void *ctx, int max_bo)
+{
+	int i;
+	for (i = 0; i < max_bo; i++)
+		drv->free_vram(ctx, handles[i]);
+}

tests/cgroup_dmem.c line 116

Quoted 10 lines
tests/cgroup_dmem.c +10 -0
+
+static void test_current(int fd, char *cg_region, unsigned int flags, const struct igt_dmem_driver *drv, void *ctx)
+{
+	struct igt_cgroup *cg;
+	void **handles;
+	uint64_t current, capacity, cg_max;
+	int n_bo = 0, max_bo;
+
+	igt_cgroup_dmem_get_capacity(cg_region, &capacity);
+	igt_require_f(capacity >= 4 * BO_SIZE,

4 * BO_SIZE = 256 Mib - is that what you intended?

tests/cgroup_dmem.c line 129

Quoted 13 lines
tests/cgroup_dmem.c +13 -0
+		      "VRAM capacity (%"PRIu64" MiB) too small to test\n",
+		      capacity / SZ_1M);
+
+	/*
+	 * Use up to 4 GiB, or the full capacity if the device has less.
+	 * Leave one BO_SIZE worth of headroom so the device isn't completely
+	 * exhausted before the cgroup limit is hit.
+	 */
+	cg_max = min(MAX_LIMIT, capacity - BO_SIZE);
+	cg_max = ALIGN_DOWN(cg_max, BO_SIZE);
+
+	/* Create cgroup and move into it */
+	cg = igt_cgroup_new("igt_cgroups_test");

Looking at the igt_cgroup_new() implementation there isn't any automatic cleanup on failure registered, right? If so, then any test failure below will prevent further tests from running. At least for the Intel's automated CI that will be a problem but I let Intel people give their opinion on what should happen.

tests/cgroup_dmem.c line 134

Quoted 5 lines
tests/cgroup_dmem.c +5 -0
+	igt_cgroup_move_current(cg);
+
+	max_bo = cg_max / BO_SIZE;
+
+	handles = calloc(max_bo, sizeof(handles[0]));

If clearing on alloc is required then it could be that clearing in free_vram() also is since the array is reused in the test? Or clearing is not needed at either end? What I am trying to say is that it looks both should be the same in this respect.

tests/cgroup_dmem.c line 146

Quoted 12 lines
tests/cgroup_dmem.c +12 -0
+	igt_assert_f(handles, "failed to allocate handles array");
+
+	n_bo = allocate_vram(handles, drv, ctx, max_bo, BO_SIZE);
+	igt_assert_f(n_bo > 0, "failed to allocate VRAM\n");
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After fill: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current == cg_max,
+		     "current usage (%"PRIu64" MiB) is not requested allocation (%"PRIu64" MiB)\n",
+		     current / SZ_1M, cg_max / SZ_1M);

Similar class of a problem as above - if the test fails here objects are not freed and because fd is shared subsquent tests will also fail, right?

On the assert itself, the strict equality may become a problem if driver internal objects start getting charger to the cgroup. I know there are plans underway to close that gap. So maybe it would be best to start with current >= cg_max.

tests/cgroup_dmem.c line 149

Quoted 3 lines
tests/cgroup_dmem.c +3 -0
+
+	free_vram(handles, drv, ctx, n_bo);
+	wait_for_usage_drop(cg, cg_region, 0);

Similar as above - usage may not drop to zero all while the fd is open. I would make it robust by sampling a baseline at the start of the test and have the checks relative to it.

tests/cgroup_dmem.c line 160

Quoted 11 lines
tests/cgroup_dmem.c +11 -0
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After free: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current == 0,
+		     "current usage (%"PRIu64" MiB) is not zero\n",
+		     current / SZ_1M);
+
+	/* Allow for a slack as there might be some extra pages allocated. */
+	igt_cgroup_dmem_set_max(cg, cg_region, 2 * BO_SIZE, false);

I did not get what is the slack? Max is set to 2 BOs and then below it is checked usage is exactly 2 BOs.

Quoted 21 lines
tests/cgroup_dmem.c +21 -0
+
+	n_bo = allocate_vram(handles, drv, ctx, max_bo, BO_SIZE);
+	igt_assert_f(n_bo > 0, "failed to allocate VRAM\n");
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After fill: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current == 2 * BO_SIZE,
+		     "current usage (%"PRIu64" MiB) is not requested max allocation (%"PRIu64" MiB)\n",
+		     current / SZ_1M, cg_max / SZ_1M);
+
+	free_vram(handles, drv, ctx, n_bo);
+	wait_for_usage_drop(cg, cg_region, 0);
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After free: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current == 0,
+		     "current usage (%"PRIu64" MiB) is not zero\n",

tests/cgroup_dmem.c line 191

Quoted 10 lines
tests/cgroup_dmem.c +10 -0
+		     current / SZ_1M);
+
+	igt_cgroup_dmem_set_max(cg, cg_region, 0, false);
+
+	n_bo = allocate_vram(handles, drv, ctx, max_bo, BO_SIZE);
+
+	/*
+	 * amdgpu may succeed the allocation, by falling back to GTT, so no assertion here.
+	 * Verify by reading current usage.
+	 */

On APUs or even discrete? Could you make the test explicitly control it and ask for no automatic fallback?

Ar at least pass drivers[].driver_flag so test can have stricter asserts. Even strcmp on drv->name could work.

Regards,

Tvrtko

Quoted 116 lines
tests/cgroup_dmem.c +89 -5
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After fill: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current == 0,
+		     "current usage (%"PRIu64" MiB) is not zero\n",
+		     current / SZ_1M);
+
+	if (n_bo > 0)
+		free_vram(handles, drv, ctx, n_bo);
+	wait_for_usage_drop(cg, cg_region, 0);
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After free: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current == 0,
+		     "current usage (%"PRIu64" MiB) is not zero\n",
+		     current / SZ_1M);
+
+	igt_cgroup_free(cg);
+}
+
+static const struct {
+	const char *name;
+	void (*test_fn)(int fd, char *cg_region, unsigned int flags, const struct igt_dmem_driver *drv, void *ctx);
+	unsigned int flags;
+} subtests[] = {
+	{ "current", test_current, 0 },
+	{ }
+};
+
+static const struct {
+	int driver_flag;
+	const struct igt_dmem_driver *driver;
+} drivers[] = {
+	{ DRIVER_XE, &xe_dmem_driver },
+	{ DRIVER_AMDGPU, &amdgpu_dmem_driver },
+	{ },
+};
 
  IGT_TEST_DESCRIPTION("Exercises the cgroup v2 dmem controller interface.");
 
@@ -32,7 +241,7 @@ static void fmt_bytes(uint64_t v, char *buf, size_t len)
  		snprintf(buf, len, "%" PRIu64, v);
  }
 
-int igt_simple_main()
+static void simple_cgroup(void)
  {
  	struct igt_cgroup *cg;
  	const char *region;
@@ -42,10 +251,6 @@ int igt_simple_main()
  	char min_s[32], low_s[32], max_s[32];
  	int i;
 
-	igt_require_f(igt_cgroup_dmem_available(),
-		      "No dmem regions found; is cgroup v2 with the "
-		      "dmem controller available?\n");
-
  	cg = igt_cgroup_new("igt-cgroup-dmem-test");
  	igt_assert_f(cg, "Failed to create cgroup\n");
 
@@ -90,3 +295,50 @@ int igt_simple_main()
  	igt_cgroup_dmem_regions_free(regions);
  	igt_cgroup_free(cg);
  }
+
+int igt_main()
+{
+	igt_fixture() {
+		igt_require_f(getuid() == 0, "Test requires root\n");
+		/* Check dmem cgroup controller is available before doing anything else */
+		igt_require_f(igt_cgroup_dmem_available(),
+			      "dmem cgroup controller not available (no cgroup v2 or no registered regions)\n");
+
+	}
+
+	igt_subtest("simple")
+		simple_cgroup();
+
+	for (int d = 0; drivers[d].driver; d++) {
+		igt_subtest_group() {
+			int fd = -1;
+			int ret = -1;
+			char *cg_region = NULL;
+			void *ctx = NULL;
+			igt_fixture() {
+				fd = drm_open_driver(drivers[d].driver_flag);
+				igt_require_f(fd >= 0,
+					"No %s device found, skipping\n",
+					drivers[d].driver->name);
+				ret = drivers[d].driver->init(&ctx, fd);
+				igt_require_f(ret == 0,
+					"Failed to initialize %s device, skipping\n",
+					drivers[d].driver->name);
+				cg_region = drivers[d].driver->get_region_name(ctx);
+				igt_require_f(cg_region, "Region not tracked by dmem cgroup controller\n");
+			}
+
+			for (int i = 0; subtests[i].name; i++)
+				igt_subtest_f("%s-%s", drivers[d].driver->name, subtests[i].name)
+					subtests[i].test_fn(fd, cg_region, subtests[i].flags, drivers[d].driver, ctx);
+
+			igt_fixture() {
+				if (!ret)
+					drivers[d].driver->deinit(ctx);
+				if (fd >= 0)
+					drm_close_driver(fd);
+				free(cg_region);
+			}
+		}
+	}
+}
#15 lore raw

Re: [PATCH i-g-t v6 7/9] tests/cgroup_dmem: add test for dmem.current

Quoted 18 lines · Tvrtko Ursulin
Quoted 16 lines · Thadeu Lima de Souza Cascardo

Based on the work of Thomas Hellström to test dmem.max eviction, add a test for dmem.current usage after allocations and setting dmem.max.

Create a dmem cgroup, allocate close to capacity (or at most 4GiB), check current usage is within a small slack of the expected allocation. Then, set max to a small value and check allocations and current usage are limited to the max set. Set max to less than a single BO size, then check no allocations are allowed and current usage is also within the slack. After each allocation, release memory and check current usage has gone down.

---
  tests/cgroup_dmem.c | 262 +++++++++++++++++++++++++++++++++++++++++++++++++++-
  1 file changed, 257 insertions(+), 5 deletions(-)
Quoted 13 lines

tests/cgroup_dmem.c line 4

Quoted 9 lines
tests/cgroup_dmem.c +1 -0
index 442c965f9bbf..ba5e6a2e3deb 100644
@@ -1,6 +1,8 @@
  // SPDX-License-Identifier: MIT
  /*
   * Copyright © 2025 Intel Corporation
+ * Copyright © 2026 Intel Corporation

This is from a patch by Thomas? I would put a Co-developed-by: or his SoB even if that is agreeable since Intel's copyright is being recorded.

Well, Thomas submitted what is equivalente to the last two patches here. Then, I wrote these tests on top. But as the dmem.max behavior was not merged upstream yet, I reordered the patches, so there was some leftover of Thomas original here, specially with the helper functions and igt_main. I can just reorder again and what's left is probably my own only.

Thomas, any preference here?

Quoted 42 lines
Quoted 42 lines
tests/cgroup_dmem.c +34 -0
+ * Copyright 2026 Valve Corporation
   */
  /**
@@ -17,10 +19,217 @@
   * Test category: uapi
   */
+#include <errno.h>
  #include <inttypes.h>
+#include <signal.h>
+#include <stdatomic.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include "drmtest.h"
  #include "igt.h"
+#include "igt_aux.h"
  #include "igt_cgroup.h"
+#include "igt_dmem_driver.h"
+
+#define BO_SIZE			SZ_64M
+#define MAX_LIMIT		((uint64_t)4 * SZ_1G)
+#define USAGE_POLL_MS		10
+#define USAGE_DROP_TIMEOUT_MS	1000
+
+/**
+ * SUBTEST: simple
+ * DESCRIPTION:
+ *  Creates a cgroup, moves the process into it, enumerates all dmem regions,
+ *  prints their capacity, system-wide current usage, per-cgroup current usage
+ *  and configured limits, then destroys the cgroup.
+ */
+
+/**
+ * SUBTEST: current
+ * DESCRIPTION:
+ *  Create a dmem cgroup, allocate close to capacity (or at most 4GiB),
+ *  check current usage is within a small slack of the expected allocation.
+ *  Then, set max to a small value and check allocations and current usage
+ *  are limited to the max set.
+ *  Set max to less than a single BO size, then check no allocations are allowed
+ *  and current usage is also within the slack.
Quoted 9 lines

tests/cgroup_dmem.c line 61

Quoted 3 lines
tests/cgroup_dmem.c +3 -0
+ *  After each allocation, release memory and check current usage has gone
+ *  down.
+ * REQUIREMENTS: xe or amdgpu device with at least one VRAM region

I am out of touch the IGT documentation requirements but assuming the
REQUIREMENTS: is real, I would suggest instead of "xe or amdgpu device" you put something like "a device supported by igt_dmem_driver.c" or something like that.

I can change that, not sure what are the requirements for REQUIREMENTS here. :-)

Quoted 18 lines
Quoted 18 lines
tests/cgroup_dmem.c +18 -0
+ */
+
+static uint64_t wait_for_usage_drop(struct igt_cgroup *cg, const char *region,
+				    uint64_t limit)
+{
+	uint64_t current;
+	unsigned int elapsed = 0;
+
+	do {
+		igt_cgroup_dmem_get_current(cg, region, &current);
+		if (current <= limit)
+			return current;
+		usleep(USAGE_POLL_MS * 1000);
+		elapsed += USAGE_POLL_MS;
+	} while (elapsed < USAGE_DROP_TIMEOUT_MS);
+
+	return current;
+}
Quoted 9 lines

tests/cgroup_dmem.c line 84

Quoted 5 lines
tests/cgroup_dmem.c +5 -0
+
+static int allocate_vram(void **handles, const struct igt_dmem_driver *drv,
+			 void *ctx, int max_bo, size_t len)
+{
+	int i, err = 0;

Nit - blank lines between the declarations and bodies are a bit inconsistent in the patch.

I will sort that out in v7.

Quoted 26 lines
Quoted 26 lines
tests/cgroup_dmem.c +26 -0
+	for (i = 0; i < max_bo; i++) {
+		err = drv->allocate_vram(ctx, len, &handles[i]);
+		if (err)
+			break;
+	}
+	/* These are expected failures we can ignore. */
+	if (err == -ENOMEM || err == -ENOSPC)
+		err = 0;
+	if (!err)
+		return i;
+	for (i--; i >= 0; i--)
+		drv->free_vram(ctx, handles[i]);
+	return err;
+}
+
+static void free_vram(void **handles, const struct igt_dmem_driver *drv,
+		      void *ctx, int max_bo)
+{
+	int i;
+	for (i = 0; i < max_bo; i++)
+		drv->free_vram(ctx, handles[i]);
+}
+
+static void test_current(int fd, char *cg_region, unsigned int flags, const struct igt_dmem_driver *drv, void *ctx)
+{
+	struct igt_cgroup *cg;
Quoted 9 lines

tests/cgroup_dmem.c line 116

Quoted 6 lines
tests/cgroup_dmem.c +6 -0
+	void **handles;
+	uint64_t current, capacity, cg_max;
+	int n_bo = 0, max_bo;
+
+	igt_cgroup_dmem_get_capacity(cg_region, &capacity);
+	igt_require_f(capacity >= 4 * BO_SIZE,

4 * BO_SIZE = 256 Mib - is that what you intended?

That is right. Whatever the BO_SIZE is, we already test for 2 * BO_SIZE below. We cap the capacity to one less than that and then align down. So this will test for 3 * BO as max, given there is some headroom as the comment right below that says.

Quoted 11 lines
Quoted 11 lines
tests/cgroup_dmem.c +11 -0
+		      "VRAM capacity (%"PRIu64" MiB) too small to test\n",
+		      capacity / SZ_1M);
+
+	/*
+	 * Use up to 4 GiB, or the full capacity if the device has less.
+	 * Leave one BO_SIZE worth of headroom so the device isn't completely
+	 * exhausted before the cgroup limit is hit.
+	 */
+	cg_max = min(MAX_LIMIT, capacity - BO_SIZE);
+	cg_max = ALIGN_DOWN(cg_max, BO_SIZE);
+
Quoted 9 lines

tests/cgroup_dmem.c line 129

Quoted 2 lines
tests/cgroup_dmem.c +2 -0
+	/* Create cgroup and move into it */
+	cg = igt_cgroup_new("igt_cgroups_test");

Looking at the igt_cgroup_new() implementation there isn't any automatic cleanup on failure registered, right? If so, then any test failure below will prevent further tests from running. At least for the Intel's automated CI that will be a problem but I let Intel people give their opinion on what should happen.

Yep, that is one of the main problems here, lacking this cleanup. I will see what I can come up with as I am not so much familiar with how to best do it. I appreciate any tips.

Quoted 11 lines

tests/cgroup_dmem.c line 134

Quoted 5 lines
tests/cgroup_dmem.c +5 -0
+	igt_cgroup_move_current(cg);
+
+	max_bo = cg_max / BO_SIZE;
+
+	handles = calloc(max_bo, sizeof(handles[0]));

If clearing on alloc is required then it could be that clearing in free_vram() also is since the array is reused in the test? Or clearing is not needed at either end? What I am trying to say is that it looks both should be the same in this respect.

I think that is not strictly necessary. One of the other reasons for calloc is avoiding any overflow here, though I don't see how this could happen here anyway.

But AFAIR, we keep track of the last handle set, so we don't reuse any "cleaned" array member. I will review it, though.

Quoted 7 lines
Quoted 7 lines
tests/cgroup_dmem.c +7 -0
+	igt_assert_f(handles, "failed to allocate handles array");
+
+	n_bo = allocate_vram(handles, drv, ctx, max_bo, BO_SIZE);
+	igt_assert_f(n_bo > 0, "failed to allocate VRAM\n");
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After fill: cgroup current = %"PRIu64" MiB, "
Quoted 9 lines

tests/cgroup_dmem.c line 146

Quoted 5 lines
tests/cgroup_dmem.c +5 -0
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current == cg_max,
+		     "current usage (%"PRIu64" MiB) is not requested allocation (%"PRIu64" MiB)\n",
+		     current / SZ_1M, cg_max / SZ_1M);

Similar class of a problem as above - if the test fails here objects are not freed and because fd is shared subsquent tests will also fail, right?

I did not share fds before between subtests. I do now as of this version. What is the best approach you think we should take here? I can close/open the device between subtests.

Quoted 5 lines

On the assert itself, the strict equality may become a problem if driver internal objects start getting charger to the cgroup. I know there are plans underway to close that gap. So maybe it would be best to start with current

Quoted 1 line

= cg_max.

Some of the internal objects would be page tables set by the binding, which we have removed here, so I would keep this as is, so we can track any such issues happening, right?

Quoted 7 lines

tests/cgroup_dmem.c line 149

Quoted 3 lines
tests/cgroup_dmem.c +3 -0
+
+	free_vram(handles, drv, ctx, n_bo);
+	wait_for_usage_drop(cg, cg_region, 0);

Similar as above - usage may not drop to zero all while the fd is open. I would make it robust by sampling a baseline at the start of the test and have the checks relative to it.

Hum. I can do it, though I haven't observed this yet.

Quoted 16 lines

tests/cgroup_dmem.c line 160

Quoted 12 lines
tests/cgroup_dmem.c +11 -0
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After free: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current == 0,
+		     "current usage (%"PRIu64" MiB) is not zero\n",
+		     current / SZ_1M);
+
+	/* Allow for a slack as there might be some extra pages allocated. */
+	igt_cgroup_dmem_set_max(cg, cg_region, 2 * BO_SIZE, false);

I did not get what is the slack? Max is set to 2 BOs and then below it is checked usage is exactly 2 BOs.

This was reminiscent of previous versions, I should remove it. The issue was exactly due to the binding on Xe creating page tables on VRAM. As we removed the binding, there is no need for such "slack" anymore and the comment is outdated.

Quoted 26 lines
Quoted 26 lines
tests/cgroup_dmem.c +26 -0
+
+	n_bo = allocate_vram(handles, drv, ctx, max_bo, BO_SIZE);
+	igt_assert_f(n_bo > 0, "failed to allocate VRAM\n");
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After fill: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current == 2 * BO_SIZE,
+		     "current usage (%"PRIu64" MiB) is not requested max allocation (%"PRIu64" MiB)\n",
+		     current / SZ_1M, cg_max / SZ_1M);
+
+	free_vram(handles, drv, ctx, n_bo);
+	wait_for_usage_drop(cg, cg_region, 0);
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After free: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current == 0,
+		     "current usage (%"PRIu64" MiB) is not zero\n",
+		     current / SZ_1M);
+
+	igt_cgroup_dmem_set_max(cg, cg_region, 0, false);
+
+	n_bo = allocate_vram(handles, drv, ctx, max_bo, BO_SIZE);
Quoted 9 lines

tests/cgroup_dmem.c line 191

Quoted 5 lines
tests/cgroup_dmem.c +5 -0
+
+	/*
+	 * amdgpu may succeed the allocation, by falling back to GTT, so no assertion here.
+	 * Verify by reading current usage.
+	 */

On APUs or even discrete? Could you make the test explicitly control it and ask for no automatic fallback?

Well, specially on APUs. There is no way that I know of to avoid the fallback on APUs.

Quoted 3 lines

Ar at least pass drivers[].driver_flag so test can have stricter asserts. Even strcmp on drv->name could work.

I will give it some thought whether it is worth the trouble. It might be, and Intel people might care, as -ENOMEM/-ENOSPC should be expected in such cases.

Quoted 4 lines

Regards,

Tvrtko

Thanks.
Cascardo.

Quoted 112 lines
Quoted 111 lines
tests/cgroup_dmem.c +89 -5
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After fill: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current == 0,
+		     "current usage (%"PRIu64" MiB) is not zero\n",
+		     current / SZ_1M);
+
+	if (n_bo > 0)
+		free_vram(handles, drv, ctx, n_bo);
+	wait_for_usage_drop(cg, cg_region, 0);
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After free: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current == 0,
+		     "current usage (%"PRIu64" MiB) is not zero\n",
+		     current / SZ_1M);
+
+	igt_cgroup_free(cg);
+}
+
+static const struct {
+	const char *name;
+	void (*test_fn)(int fd, char *cg_region, unsigned int flags, const struct igt_dmem_driver *drv, void *ctx);
+	unsigned int flags;
+} subtests[] = {
+	{ "current", test_current, 0 },
+	{ }
+};
+
+static const struct {
+	int driver_flag;
+	const struct igt_dmem_driver *driver;
+} drivers[] = {
+	{ DRIVER_XE, &xe_dmem_driver },
+	{ DRIVER_AMDGPU, &amdgpu_dmem_driver },
+	{ },
+};
  IGT_TEST_DESCRIPTION("Exercises the cgroup v2 dmem controller interface.");
@@ -32,7 +241,7 @@ static void fmt_bytes(uint64_t v, char *buf, size_t len)
  		snprintf(buf, len, "%" PRIu64, v);
  }
-int igt_simple_main()
+static void simple_cgroup(void)
  {
  	struct igt_cgroup *cg;
  	const char *region;
@@ -42,10 +251,6 @@ int igt_simple_main()
  	char min_s[32], low_s[32], max_s[32];
  	int i;
-	igt_require_f(igt_cgroup_dmem_available(),
-		      "No dmem regions found; is cgroup v2 with the "
-		      "dmem controller available?\n");
-
  	cg = igt_cgroup_new("igt-cgroup-dmem-test");
  	igt_assert_f(cg, "Failed to create cgroup\n");
@@ -90,3 +295,50 @@ int igt_simple_main()
  	igt_cgroup_dmem_regions_free(regions);
  	igt_cgroup_free(cg);
  }
+
+int igt_main()
+{
+	igt_fixture() {
+		igt_require_f(getuid() == 0, "Test requires root\n");
+		/* Check dmem cgroup controller is available before doing anything else */
+		igt_require_f(igt_cgroup_dmem_available(),
+			      "dmem cgroup controller not available (no cgroup v2 or no registered regions)\n");
+
+	}
+
+	igt_subtest("simple")
+		simple_cgroup();
+
+	for (int d = 0; drivers[d].driver; d++) {
+		igt_subtest_group() {
+			int fd = -1;
+			int ret = -1;
+			char *cg_region = NULL;
+			void *ctx = NULL;
+			igt_fixture() {
+				fd = drm_open_driver(drivers[d].driver_flag);
+				igt_require_f(fd >= 0,
+					"No %s device found, skipping\n",
+					drivers[d].driver->name);
+				ret = drivers[d].driver->init(&ctx, fd);
+				igt_require_f(ret == 0,
+					"Failed to initialize %s device, skipping\n",
+					drivers[d].driver->name);
+				cg_region = drivers[d].driver->get_region_name(ctx);
+				igt_require_f(cg_region, "Region not tracked by dmem cgroup controller\n");
+			}
+
+			for (int i = 0; subtests[i].name; i++)
+				igt_subtest_f("%s-%s", drivers[d].driver->name, subtests[i].name)
+					subtests[i].test_fn(fd, cg_region, subtests[i].flags, drivers[d].driver, ctx);
+
+			igt_fixture() {
+				if (!ret)
+					drivers[d].driver->deinit(ctx);
+				if (fd >= 0)
+					drm_close_driver(fd);
+				free(cg_region);
+			}
+		}
+	}
+}
#16 lore raw

Re: [PATCH i-g-t v6 7/9] tests/cgroup_dmem: add test for dmem.current

In reply to Thadeu Lima de Souza Cascardo

Hi Thadeu,

Quoted 21 lines · Thadeu Lima de Souza Cascardo

Based on the work of Thomas Hellström to test dmem.max eviction, add a test for dmem.current usage after allocations and setting dmem.max.

Create a dmem cgroup, allocate close to capacity (or at most 4GiB), check current usage is within a small slack of the expected allocation. Then, set max to a small value and check allocations and current usage are limited to the max set. Set max to less than a single BO size, then check no allocations are allowed and current usage is also within the slack. After each allocation, release memory and check current usage has gone down.

---
 tests/cgroup_dmem.c | 262 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 257 insertions(+), 5 deletions(-)
tests/cgroup_dmem.c +0 -0
index 442c965f9bbf..ba5e6a2e3deb 100644

tests/cgroup_dmem.c line 4

Quoted 4 lines
tests/cgroup_dmem.c +1 -0
@@ -1,6 +1,8 @@
 // SPDX-License-Identifier: MIT
 /*
  * Copyright © 2025 Intel Corporation
+ * Copyright © 2026 Intel Corporation

Can you make it shorter? Just add 2026 to it:
* Copyright © 2025,2026 Intel Corporation

tests/cgroup_dmem.c line 5

Quoted 1 line
tests/cgroup_dmem.c +1 -0
+ * Copyright 2026 Valve Corporation

Missing (c) or its UTF char:

* Copyright © 2026 Valve Corporation

Quoted 37 lines
tests/cgroup_dmem.c +26 -0
  */
 
 /**
@@ -17,10 +19,217 @@
  * Test category: uapi
  */
 
+#include <errno.h>
 #include <inttypes.h>
+#include <signal.h>
+#include <stdatomic.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
 
+#include "drmtest.h"
 #include "igt.h"
+#include "igt_aux.h"
 #include "igt_cgroup.h"
+#include "igt_dmem_driver.h"
+
+#define BO_SIZE			SZ_64M
+#define MAX_LIMIT		((uint64_t)4 * SZ_1G)
+#define USAGE_POLL_MS		10
+#define USAGE_DROP_TIMEOUT_MS	1000
+
+/**
+ * SUBTEST: simple
+ * DESCRIPTION:
+ *  Creates a cgroup, moves the process into it, enumerates all dmem regions,
+ *  prints their capacity, system-wide current usage, per-cgroup current usage
+ *  and configured limits, then destroys the cgroup.
+ */
+
+/**
+ * SUBTEST: current

tests/cgroup_dmem.c line 61

Quoted 10 lines
tests/cgroup_dmem.c +10 -0
+ * DESCRIPTION:
+ *  Create a dmem cgroup, allocate close to capacity (or at most 4GiB),
+ *  check current usage is within a small slack of the expected allocation.
+ *  Then, set max to a small value and check allocations and current usage
+ *  are limited to the max set.
+ *  Set max to less than a single BO size, then check no allocations are allowed
+ *  and current usage is also within the slack.
+ *  After each allocation, release memory and check current usage has gone
+ *  down.
+ * REQUIREMENTS: xe or amdgpu device with at least one VRAM region

Make this part of description, also imho drop xe/amdgpu, so:

* down. This subtest requires GPU device with at least one VRAM region.

For VRAM, add specific checks for this in beginning of test and bail out if it is iGPU.

Regards,
Kamil

Quoted 249 lines
tests/cgroup_dmem.c +219 -5
+ */
+
+static uint64_t wait_for_usage_drop(struct igt_cgroup *cg, const char *region,
+				    uint64_t limit)
+{
+	uint64_t current;
+	unsigned int elapsed = 0;
+
+	do {
+		igt_cgroup_dmem_get_current(cg, region, &current);
+		if (current <= limit)
+			return current;
+		usleep(USAGE_POLL_MS * 1000);
+		elapsed += USAGE_POLL_MS;
+	} while (elapsed < USAGE_DROP_TIMEOUT_MS);
+
+	return current;
+}
+
+static int allocate_vram(void **handles, const struct igt_dmem_driver *drv,
+			 void *ctx, int max_bo, size_t len)
+{
+	int i, err = 0;
+	for (i = 0; i < max_bo; i++) {
+		err = drv->allocate_vram(ctx, len, &handles[i]);
+		if (err)
+			break;
+	}
+	/* These are expected failures we can ignore. */
+	if (err == -ENOMEM || err == -ENOSPC)
+		err = 0;
+	if (!err)
+		return i;
+	for (i--; i >= 0; i--)
+		drv->free_vram(ctx, handles[i]);
+	return err;
+}
+
+static void free_vram(void **handles, const struct igt_dmem_driver *drv,
+		      void *ctx, int max_bo)
+{
+	int i;
+	for (i = 0; i < max_bo; i++)
+		drv->free_vram(ctx, handles[i]);
+}
+
+static void test_current(int fd, char *cg_region, unsigned int flags, const struct igt_dmem_driver *drv, void *ctx)
+{
+	struct igt_cgroup *cg;
+	void **handles;
+	uint64_t current, capacity, cg_max;
+	int n_bo = 0, max_bo;
+
+	igt_cgroup_dmem_get_capacity(cg_region, &capacity);
+	igt_require_f(capacity >= 4 * BO_SIZE,
+		      "VRAM capacity (%"PRIu64" MiB) too small to test\n",
+		      capacity / SZ_1M);
+
+	/*
+	 * Use up to 4 GiB, or the full capacity if the device has less.
+	 * Leave one BO_SIZE worth of headroom so the device isn't completely
+	 * exhausted before the cgroup limit is hit.
+	 */
+	cg_max = min(MAX_LIMIT, capacity - BO_SIZE);
+	cg_max = ALIGN_DOWN(cg_max, BO_SIZE);
+
+	/* Create cgroup and move into it */
+	cg = igt_cgroup_new("igt_cgroups_test");
+	igt_cgroup_move_current(cg);
+
+	max_bo = cg_max / BO_SIZE;
+
+	handles = calloc(max_bo, sizeof(handles[0]));
+	igt_assert_f(handles, "failed to allocate handles array");
+
+	n_bo = allocate_vram(handles, drv, ctx, max_bo, BO_SIZE);
+	igt_assert_f(n_bo > 0, "failed to allocate VRAM\n");
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After fill: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current == cg_max,
+		     "current usage (%"PRIu64" MiB) is not requested allocation (%"PRIu64" MiB)\n",
+		     current / SZ_1M, cg_max / SZ_1M);
+
+	free_vram(handles, drv, ctx, n_bo);
+	wait_for_usage_drop(cg, cg_region, 0);
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After free: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current == 0,
+		     "current usage (%"PRIu64" MiB) is not zero\n",
+		     current / SZ_1M);
+
+	/* Allow for a slack as there might be some extra pages allocated. */
+	igt_cgroup_dmem_set_max(cg, cg_region, 2 * BO_SIZE, false);
+
+	n_bo = allocate_vram(handles, drv, ctx, max_bo, BO_SIZE);
+	igt_assert_f(n_bo > 0, "failed to allocate VRAM\n");
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After fill: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current == 2 * BO_SIZE,
+		     "current usage (%"PRIu64" MiB) is not requested max allocation (%"PRIu64" MiB)\n",
+		     current / SZ_1M, cg_max / SZ_1M);
+
+	free_vram(handles, drv, ctx, n_bo);
+	wait_for_usage_drop(cg, cg_region, 0);
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After free: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current == 0,
+		     "current usage (%"PRIu64" MiB) is not zero\n",
+		     current / SZ_1M);
+
+	igt_cgroup_dmem_set_max(cg, cg_region, 0, false);
+
+	n_bo = allocate_vram(handles, drv, ctx, max_bo, BO_SIZE);
+
+	/*
+	 * amdgpu may succeed the allocation, by falling back to GTT, so no assertion here.
+	 * Verify by reading current usage.
+	 */
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After fill: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current == 0,
+		     "current usage (%"PRIu64" MiB) is not zero\n",
+		     current / SZ_1M);
+
+	if (n_bo > 0)
+		free_vram(handles, drv, ctx, n_bo);
+	wait_for_usage_drop(cg, cg_region, 0);
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After free: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current == 0,
+		     "current usage (%"PRIu64" MiB) is not zero\n",
+		     current / SZ_1M);
+
+	igt_cgroup_free(cg);
+}
+
+static const struct {
+	const char *name;
+	void (*test_fn)(int fd, char *cg_region, unsigned int flags, const struct igt_dmem_driver *drv, void *ctx);
+	unsigned int flags;
+} subtests[] = {
+	{ "current", test_current, 0 },
+	{ }
+};
+
+static const struct {
+	int driver_flag;
+	const struct igt_dmem_driver *driver;
+} drivers[] = {
+	{ DRIVER_XE, &xe_dmem_driver },
+	{ DRIVER_AMDGPU, &amdgpu_dmem_driver },
+	{ },
+};
 
 IGT_TEST_DESCRIPTION("Exercises the cgroup v2 dmem controller interface.");
 
@@ -32,7 +241,7 @@ static void fmt_bytes(uint64_t v, char *buf, size_t len)
 		snprintf(buf, len, "%" PRIu64, v);
 }
 
-int igt_simple_main()
+static void simple_cgroup(void)
 {
 	struct igt_cgroup *cg;
 	const char *region;
@@ -42,10 +251,6 @@ int igt_simple_main()
 	char min_s[32], low_s[32], max_s[32];
 	int i;
 
-	igt_require_f(igt_cgroup_dmem_available(),
-		      "No dmem regions found; is cgroup v2 with the "
-		      "dmem controller available?\n");
-
 	cg = igt_cgroup_new("igt-cgroup-dmem-test");
 	igt_assert_f(cg, "Failed to create cgroup\n");
 
@@ -90,3 +295,50 @@ int igt_simple_main()
 	igt_cgroup_dmem_regions_free(regions);
 	igt_cgroup_free(cg);
 }
+
+int igt_main()
+{
+	igt_fixture() {
+		igt_require_f(getuid() == 0, "Test requires root\n");
+		/* Check dmem cgroup controller is available before doing anything else */
+		igt_require_f(igt_cgroup_dmem_available(),
+			      "dmem cgroup controller not available (no cgroup v2 or no registered regions)\n");
+
+	}
+
+	igt_subtest("simple")
+		simple_cgroup();
+
+	for (int d = 0; drivers[d].driver; d++) {
+		igt_subtest_group() {
+			int fd = -1;
+			int ret = -1;
+			char *cg_region = NULL;
+			void *ctx = NULL;
+			igt_fixture() {
+				fd = drm_open_driver(drivers[d].driver_flag);
+				igt_require_f(fd >= 0,
+					"No %s device found, skipping\n",
+					drivers[d].driver->name);
+				ret = drivers[d].driver->init(&ctx, fd);
+				igt_require_f(ret == 0,
+					"Failed to initialize %s device, skipping\n",
+					drivers[d].driver->name);
+				cg_region = drivers[d].driver->get_region_name(ctx);
+				igt_require_f(cg_region, "Region not tracked by dmem cgroup controller\n");
+			}
+
+			for (int i = 0; subtests[i].name; i++)
+				igt_subtest_f("%s-%s", drivers[d].driver->name, subtests[i].name)
+					subtests[i].test_fn(fd, cg_region, subtests[i].flags, drivers[d].driver, ctx);
+
+			igt_fixture() {
+				if (!ret)
+					drivers[d].driver->deinit(ctx);
+				if (fd >= 0)
+					drm_close_driver(fd);
+				free(cg_region);
+			}
+		}
+	}
+}
Signature · 2 lines
-- 
2.47.3
#17 lore raw

PATCH v6 8/9 tests/cgroup_dmem: add dmem cgroup eviction test

In reply to Thadeu Lima de Souza Cascardo

From: Thomas Hellström <[email protected]>

The write_eviction subtest:
- Creates a sub-cgroup and moves the test process into it.
- Sets a dmem.max limit on the first VRAM region (up to 4 GiB, or the full capacity if smaller).
- Fills VRAM by repeatedly creating BOs placed in VRAM, depending on card support.
- Verifies that cgroup current usage is within the expected range when the limit is hit.
- Lowers dmem.max in 128 MiB steps, waiting for usage to follow each reduction.

The write_eviction_interruptible subtest runs the same test with SIGCONT signals injected via igt_fork_signal_helper() and reports the number of signals received. When a signal interrupts kernel-side eviction, a small BO allocation is used to re-trigger it.

Assisted-by GitHub Copilot:claude-sonnet-4.6
---
 tests/cgroup_dmem.c | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 149 insertions(+)
tests/cgroup_dmem.c +149 -0
index ba5e6a2e3deb..0d3b415acd54 100644
@@ -36,9 +36,12 @@
 
 #define BO_SIZE			SZ_64M
 #define MAX_LIMIT		((uint64_t)4 * SZ_1G)
+#define EVICT_STEP		SZ_128M
 #define USAGE_POLL_MS		10
 #define USAGE_DROP_TIMEOUT_MS	1000
 
+#define TEST_INTERRUPTIBLE	(1 << 0)
+
 /**
  * SUBTEST: simple
  * DESCRIPTION:
@@ -61,6 +64,62 @@
  * REQUIREMENTS: xe or amdgpu device with at least one VRAM region
  */
 
+/**
+ * SUBTEST: write_eviction
+ * DESCRIPTION:
+ *   Create a dmem cgroup, move the current process into it and set the max
+ *   device memory limit for the first VRAM region to 4 GiB.  Then fill VRAM
+ *   by creating BOs with %DRM_XE_GEM_CREATE_FLAG_DEFER_BACKING (so that the
+ *   physical allocation is deferred until VM_BIND) and binding them into an
+ *   LR VM until the cgroup limit is hit.  Verify that the reported cgroup
+ *   current usage is within the expected range when the error occurs.
+ *   Finally lower the max limit in 256 MiB steps and verify that the cgroup
+ *   usage follows.
+ * REQUIREMENTS: must run as root; xe device with at least one VRAM region
+ */
+
+/**
+ * SUBTEST: write_eviction_interruptible
+ * DESCRIPTION:
+ *   Same as write_eviction but with SIGCONT signals injected throughout via
+ *   igt_fork_signal_helper() to verify that the dmem.max write path handles
+ *   signal interruption correctly.  A signal handler counts received signals
+ *   and the count is reported as debug output at the end of the test.
+ *   A signal interrupts the set-time eviction, and further eviction can be
+ *   triggered by an explicit allocation.
+ * REQUIREMENTS: must run as root; xe device with at least one VRAM region
+ */
+
+static atomic_int signal_count;
+static struct sigaction sigcont_oldact;
+
+static void sigcont_handler(int sig)
+{
+	atomic_fetch_add(&signal_count, 1);
+
+	/* Chain to the previous handler (IGT's dummy sig_handler) */
+	if (sigcont_oldact.sa_handler &&
+	    sigcont_oldact.sa_handler != SIG_IGN &&
+	    sigcont_oldact.sa_handler != SIG_DFL)
+		sigcont_oldact.sa_handler(sig);
+}
+
+static void install_sigcont_counter(void)
+{
+	struct sigaction sa;
+
+	atomic_store(&signal_count, 0);
+	igt_fork_signal_helper();
+	/*
+	 * Install the counter after igt_fork_signal_helper() so our handler
+	 * is not overwritten.  Save the old handler so we can chain to it.
+	 */
+	memset(&sa, 0, sizeof(sa));
+	sa.sa_handler = sigcont_handler;
+	sigemptyset(&sa.sa_mask);
+	sigaction(SIGCONT, &sa, &sigcont_oldact);
+}
+
 static uint64_t wait_for_usage_drop(struct igt_cgroup *cg, const char *region,
 				    uint64_t limit)
 {
@@ -213,12 +272,102 @@ static void test_current(int fd, char *cg_region, unsigned int flags, const stru
 	igt_cgroup_free(cg);
 }
 
+static void test_write_eviction(int fd, char *cg_region, unsigned int flags, const struct igt_dmem_driver *drv, void *ctx)
+{
+	struct igt_cgroup *cg;
+	void **handles;
+	int max_bo;
+	uint64_t current, capacity, cg_max, limit, after;
+	int err;
+
+	igt_cgroup_dmem_get_capacity(cg_region, &capacity);
+	igt_require_f(capacity >= 4 * BO_SIZE,
+		      "VRAM capacity (%"PRIu64" MiB) too small to test\n",
+		      capacity / SZ_1M);
+
+	/*
+	 * Use up to 4 GiB, or the full capacity if the device has less.
+	 * Leave one BO_SIZE worth of headroom so the device isn't completely
+	 * exhausted before the cgroup limit is hit.
+	 */
+	cg_max = min(MAX_LIMIT, capacity - BO_SIZE);
+	cg_max = ALIGN_DOWN(cg_max, EVICT_STEP);
+
+	if (flags & TEST_INTERRUPTIBLE)
+		install_sigcont_counter();
+
+	/* Create cgroup and move into it */
+	cg = igt_cgroup_new("igt_cgroups_test");
+	igt_cgroup_move_current(cg);
+	igt_cgroup_dmem_set_max(cg, cg_region, cg_max, false);
+
+	max_bo = (cg_max / BO_SIZE) + 8; /* headroom for overcommit */
+
+	handles = calloc(max_bo, sizeof(handles[0]));
+	igt_assert_f(handles, "failed to allocate handles array");
+
+	allocate_vram(handles, drv, ctx, max_bo, BO_SIZE);
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After fill: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+
+	igt_assert_f(current <= cg_max,
+		     "Usage %"PRIu64" MiB exceeds max %"PRIu64" MiB + slack\n",
+		     current / SZ_1M, cg_max / SZ_1M);
+
+	/* Phase 2: lower max in 256 MiB steps, verify usage follows */
+	limit = cg_max;
+	while (limit >= EVICT_STEP) {
+
+		limit -= EVICT_STEP;
+		igt_cgroup_dmem_set_max(cg, cg_region, limit, false);
+
+		igt_cgroup_dmem_get_current(cg, cg_region, &after);
+		igt_debug("Lowered max to %"PRIu64" MiB: usage = %"PRIu64" MiB\n",
+			  limit / SZ_1M, after / SZ_1M);
+
+		if (limit > EVICT_STEP) {
+			if ((flags & TEST_INTERRUPTIBLE) && after > limit) {
+				/* Let a new bo creation trigger eviction. */
+				void *handle;
+				err = drv->allocate_vram(ctx, BO_SIZE / 8, &handle);
+				igt_assert_f(err == 0,
+					"Error trying to allocate more VRAM to trigger eviction.");
+				drv->free_vram(ctx, handle);
+
+				igt_cgroup_dmem_get_current(cg, cg_region, &after);
+				igt_debug("Forced eviction max is %"PRIu64
+					  " MiB: usage = %"PRIu64" MiB\n",
+					  limit / SZ_1M, after / SZ_1M);
+			}
+
+			igt_assert_f(after <= limit,
+				     "Usage %"PRIu64" MiB did not follow max %"PRIu64" MiB\n",
+				     after / SZ_1M, limit / SZ_1M);
+		}
+	}
+
+	if (flags & TEST_INTERRUPTIBLE) {
+		igt_stop_signal_helper();
+		igt_info("Signals received during test: %d\n",
+			 atomic_load(&signal_count));
+	}
+
+	/* Cleanup */
+	igt_cgroup_dmem_set_max(cg, cg_region, IGT_CGROUP_DMEM_MAX, false);
+	igt_cgroup_free(cg);
+}
+
 static const struct {
 	const char *name;
 	void (*test_fn)(int fd, char *cg_region, unsigned int flags, const struct igt_dmem_driver *drv, void *ctx);
 	unsigned int flags;
 } subtests[] = {
 	{ "current", test_current, 0 },
+	{ "write_eviction", test_write_eviction, 0 },
+	{ "write_eviction_interruptible", test_write_eviction, TEST_INTERRUPTIBLE },
 	{ }
 };
 
Signature · 1 line
2.47.3
#18 lore raw

Re: [PATCH i-g-t v6 8/9] tests/cgroup_dmem: add dmem cgroup eviction test

Quoted 48 lines · Thadeu Lima de Souza Cascardo

From: Thomas Hellström <[email protected]>

The write_eviction subtest:
- Creates a sub-cgroup and moves the test process into it.
- Sets a dmem.max limit on the first VRAM region (up to 4 GiB, or the full capacity if smaller).
- Fills VRAM by repeatedly creating BOs placed in VRAM, depending on card support.
- Verifies that cgroup current usage is within the expected range when the limit is hit.
- Lowers dmem.max in 128 MiB steps, waiting for usage to follow each reduction.

The write_eviction_interruptible subtest runs the same test with SIGCONT signals injected via igt_fork_signal_helper() and reports the number of signals received. When a signal interrupts kernel-side eviction, a small BO allocation is used to re-trigger it.

Assisted-by GitHub Copilot:claude-sonnet-4.6
---
  tests/cgroup_dmem.c | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 149 insertions(+)
tests/cgroup_dmem.c +5 -0
index ba5e6a2e3deb..0d3b415acd54 100644
@@ -36,9 +36,12 @@
 
  #define BO_SIZE			SZ_64M
  #define MAX_LIMIT		((uint64_t)4 * SZ_1G)
+#define EVICT_STEP		SZ_128M
  #define USAGE_POLL_MS		10
  #define USAGE_DROP_TIMEOUT_MS	1000
 
+#define TEST_INTERRUPTIBLE	(1 << 0)
+
  /**
   * SUBTEST: simple
   * DESCRIPTION:
@@ -61,6 +64,62 @@
   * REQUIREMENTS: xe or amdgpu device with at least one VRAM region
   */
 
+/**
+ * SUBTEST: write_eviction

tests/cgroup_dmem.c line 78

Quoted 10 lines
tests/cgroup_dmem.c +10 -0
+ * DESCRIPTION:
+ *   Create a dmem cgroup, move the current process into it and set the max
+ *   device memory limit for the first VRAM region to 4 GiB.  Then fill VRAM
+ *   by creating BOs with %DRM_XE_GEM_CREATE_FLAG_DEFER_BACKING (so that the
+ *   physical allocation is deferred until VM_BIND) and binding them into an
+ *   LR VM until the cgroup limit is hit.  Verify that the reported cgroup
+ *   current usage is within the expected range when the error occurs.
+ *   Finally lower the max limit in 256 MiB steps and verify that the cgroup
+ *   usage follows.
+ * REQUIREMENTS: must run as root; xe device with at least one VRAM region

It feels that adding an explicit igt_require(DRIVER_XE) to the test would be a good move.

Quoted 64 lines
tests/cgroup_dmem.c +57 -0
+ */
+
+/**
+ * SUBTEST: write_eviction_interruptible
+ * DESCRIPTION:
+ *   Same as write_eviction but with SIGCONT signals injected throughout via
+ *   igt_fork_signal_helper() to verify that the dmem.max write path handles
+ *   signal interruption correctly.  A signal handler counts received signals
+ *   and the count is reported as debug output at the end of the test.
+ *   A signal interrupts the set-time eviction, and further eviction can be
+ *   triggered by an explicit allocation.
+ * REQUIREMENTS: must run as root; xe device with at least one VRAM region
+ */
+
+static atomic_int signal_count;
+static struct sigaction sigcont_oldact;
+
+static void sigcont_handler(int sig)
+{
+	atomic_fetch_add(&signal_count, 1);
+
+	/* Chain to the previous handler (IGT's dummy sig_handler) */
+	if (sigcont_oldact.sa_handler &&
+	    sigcont_oldact.sa_handler != SIG_IGN &&
+	    sigcont_oldact.sa_handler != SIG_DFL)
+		sigcont_oldact.sa_handler(sig);
+}
+
+static void install_sigcont_counter(void)
+{
+	struct sigaction sa;
+
+	atomic_store(&signal_count, 0);
+	igt_fork_signal_helper();
+	/*
+	 * Install the counter after igt_fork_signal_helper() so our handler
+	 * is not overwritten.  Save the old handler so we can chain to it.
+	 */
+	memset(&sa, 0, sizeof(sa));
+	sa.sa_handler = sigcont_handler;
+	sigemptyset(&sa.sa_mask);
+	sigaction(SIGCONT, &sa, &sigcont_oldact);
+}
+
  static uint64_t wait_for_usage_drop(struct igt_cgroup *cg, const char *region,
  				    uint64_t limit)
  {
@@ -213,12 +272,102 @@ static void test_current(int fd, char *cg_region, unsigned int flags, const stru
  	igt_cgroup_free(cg);
  }
 
+static void test_write_eviction(int fd, char *cg_region, unsigned int flags, const struct igt_dmem_driver *drv, void *ctx)
+{
+	struct igt_cgroup *cg;
+	void **handles;
+	int max_bo;
+	uint64_t current, capacity, cg_max, limit, after;
+	int err;
+
+	igt_cgroup_dmem_get_capacity(cg_region, &capacity);
+	igt_require_f(capacity >= 4 * BO_SIZE,
+		      "VRAM capacity (%"PRIu64" MiB) too small to test\n",
+		      capacity / SZ_1M);
+

tests/cgroup_dmem.c static void test_current(int fd, char *cg_region, unsigned int flags, const stru · line 297

Quoted 10 lines
tests/cgroup_dmem.c +10 -0
+	/*
+	 * Use up to 4 GiB, or the full capacity if the device has less.
+	 * Leave one BO_SIZE worth of headroom so the device isn't completely
+	 * exhausted before the cgroup limit is hit.
+	 */
+	cg_max = min(MAX_LIMIT, capacity - BO_SIZE);
+	cg_max = ALIGN_DOWN(cg_max, EVICT_STEP);
+
+	if (flags & TEST_INTERRUPTIBLE)
+		install_sigcont_counter();

I stumbled upon IGT docs recommending igt_while_interruptible as a gentler alternative. Would that still work here and be simpler?

Quoted 41 lines
tests/cgroup_dmem.c +41 -0
+
+	/* Create cgroup and move into it */
+	cg = igt_cgroup_new("igt_cgroups_test");
+	igt_cgroup_move_current(cg);
+	igt_cgroup_dmem_set_max(cg, cg_region, cg_max, false);
+
+	max_bo = (cg_max / BO_SIZE) + 8; /* headroom for overcommit */
+
+	handles = calloc(max_bo, sizeof(handles[0]));
+	igt_assert_f(handles, "failed to allocate handles array");
+
+	allocate_vram(handles, drv, ctx, max_bo, BO_SIZE);
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After fill: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+
+	igt_assert_f(current <= cg_max,
+		     "Usage %"PRIu64" MiB exceeds max %"PRIu64" MiB + slack\n",
+		     current / SZ_1M, cg_max / SZ_1M);
+
+	/* Phase 2: lower max in 256 MiB steps, verify usage follows */
+	limit = cg_max;
+	while (limit >= EVICT_STEP) {
+
+		limit -= EVICT_STEP;
+		igt_cgroup_dmem_set_max(cg, cg_region, limit, false);
+
+		igt_cgroup_dmem_get_current(cg, cg_region, &after);
+		igt_debug("Lowered max to %"PRIu64" MiB: usage = %"PRIu64" MiB\n",
+			  limit / SZ_1M, after / SZ_1M);
+
+		if (limit > EVICT_STEP) {
+			if ((flags & TEST_INTERRUPTIBLE) && after > limit) {
+				/* Let a new bo creation trigger eviction. */
+				void *handle;
+				err = drv->allocate_vram(ctx, BO_SIZE / 8, &handle);
+				igt_assert_f(err == 0,
+					"Error trying to allocate more VRAM to trigger eviction.");
+				drv->free_vram(ctx, handle);

tests/cgroup_dmem.c static void test_current(int fd, char *cg_region, unsigned int flags, const stru · line 348

Quoted 10 lines
tests/cgroup_dmem.c +10 -0
+
+				igt_cgroup_dmem_get_current(cg, cg_region, &after);
+				igt_debug("Forced eviction max is %"PRIu64
+					  " MiB: usage = %"PRIu64" MiB\n",
+					  limit / SZ_1M, after / SZ_1M);
+			}
+
+			igt_assert_f(after <= limit,
+				     "Usage %"PRIu64" MiB did not follow max %"PRIu64" MiB\n",
+				     after / SZ_1M, limit / SZ_1M);

I was quite confused by what is the interrupt by signal business about. My best guess is the point is not to interrupt the write to the sysfs on the superficial level, but to interrupt the actual eviction process which happens during the write? Is that a cgroup requirement that the new limit has to become effective (or attempted at least) during the write itself? And once interrupted it will not re-try it until new "activity" in the dmem cgroup happens (the dummy allocation above)?

Regards,

Tvrtko

Quoted 26 lines
tests/cgroup_dmem.c +16 -0
+		}
+	}
+
+	if (flags & TEST_INTERRUPTIBLE) {
+		igt_stop_signal_helper();
+		igt_info("Signals received during test: %d\n",
+			 atomic_load(&signal_count));
+	}
+
+	/* Cleanup */
+	igt_cgroup_dmem_set_max(cg, cg_region, IGT_CGROUP_DMEM_MAX, false);
+	igt_cgroup_free(cg);
+}
+
  static const struct {
  	const char *name;
  	void (*test_fn)(int fd, char *cg_region, unsigned int flags, const struct igt_dmem_driver *drv, void *ctx);
  	unsigned int flags;
  } subtests[] = {
  	{ "current", test_current, 0 },
+	{ "write_eviction", test_write_eviction, 0 },
+	{ "write_eviction_interruptible", test_write_eviction, TEST_INTERRUPTIBLE },
  	{ }
  };
 
#19 lore raw

Re: [PATCH i-g-t v6 8/9] tests/cgroup_dmem: add dmem cgroup eviction test

Quoted 52 lines · Tvrtko Ursulin
Quoted 50 lines · Thadeu Lima de Souza Cascardo

From: Thomas Hellström <[email protected]>

The write_eviction subtest:
- Creates a sub-cgroup and moves the test process into it.
- Sets a dmem.max limit on the first VRAM region (up to 4 GiB, or the full capacity if smaller).
- Fills VRAM by repeatedly creating BOs placed in VRAM, depending on card support.
- Verifies that cgroup current usage is within the expected range when the limit is hit.
- Lowers dmem.max in 128 MiB steps, waiting for usage to follow each reduction.

The write_eviction_interruptible subtest runs the same test with SIGCONT signals injected via igt_fork_signal_helper() and reports the number of signals received. When a signal interrupts kernel-side eviction, a small BO allocation is used to re-trigger it.

Assisted-by GitHub Copilot:claude-sonnet-4.6
---
  tests/cgroup_dmem.c | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 149 insertions(+)
tests/cgroup_dmem.c +10 -0
index ba5e6a2e3deb..0d3b415acd54 100644
@@ -36,9 +36,12 @@
  #define BO_SIZE			SZ_64M
  #define MAX_LIMIT		((uint64_t)4 * SZ_1G)
+#define EVICT_STEP		SZ_128M
  #define USAGE_POLL_MS		10
  #define USAGE_DROP_TIMEOUT_MS	1000
+#define TEST_INTERRUPTIBLE	(1 << 0)
+
  /**
   * SUBTEST: simple
   * DESCRIPTION:
@@ -61,6 +64,62 @@
   * REQUIREMENTS: xe or amdgpu device with at least one VRAM region
   */
+/**
+ * SUBTEST: write_eviction
+ * DESCRIPTION:
+ *   Create a dmem cgroup, move the current process into it and set the max
+ *   device memory limit for the first VRAM region to 4 GiB.  Then fill VRAM
+ *   by creating BOs with %DRM_XE_GEM_CREATE_FLAG_DEFER_BACKING (so that the
+ *   physical allocation is deferred until VM_BIND) and binding them into an
Quoted 9 lines

tests/cgroup_dmem.c line 78

Quoted 5 lines
tests/cgroup_dmem.c +5 -0
+ *   LR VM until the cgroup limit is hit.  Verify that the reported cgroup
+ *   current usage is within the expected range when the error occurs.
+ *   Finally lower the max limit in 256 MiB steps and verify that the cgroup
+ *   usage follows.
+ * REQUIREMENTS: must run as root; xe device with at least one VRAM region

It feels that adding an explicit igt_require(DRIVER_XE) to the test would be a good move.

Ah, the REQUIREMENTS are the same for the current test, so this is outdated and amdgpu is another acceptable driver/device too.

Quoted 68 lines
Quoted 68 lines
tests/cgroup_dmem.c +62 -0
+ */
+
+/**
+ * SUBTEST: write_eviction_interruptible
+ * DESCRIPTION:
+ *   Same as write_eviction but with SIGCONT signals injected throughout via
+ *   igt_fork_signal_helper() to verify that the dmem.max write path handles
+ *   signal interruption correctly.  A signal handler counts received signals
+ *   and the count is reported as debug output at the end of the test.
+ *   A signal interrupts the set-time eviction, and further eviction can be
+ *   triggered by an explicit allocation.
+ * REQUIREMENTS: must run as root; xe device with at least one VRAM region
+ */
+
+static atomic_int signal_count;
+static struct sigaction sigcont_oldact;
+
+static void sigcont_handler(int sig)
+{
+	atomic_fetch_add(&signal_count, 1);
+
+	/* Chain to the previous handler (IGT's dummy sig_handler) */
+	if (sigcont_oldact.sa_handler &&
+	    sigcont_oldact.sa_handler != SIG_IGN &&
+	    sigcont_oldact.sa_handler != SIG_DFL)
+		sigcont_oldact.sa_handler(sig);
+}
+
+static void install_sigcont_counter(void)
+{
+	struct sigaction sa;
+
+	atomic_store(&signal_count, 0);
+	igt_fork_signal_helper();
+	/*
+	 * Install the counter after igt_fork_signal_helper() so our handler
+	 * is not overwritten.  Save the old handler so we can chain to it.
+	 */
+	memset(&sa, 0, sizeof(sa));
+	sa.sa_handler = sigcont_handler;
+	sigemptyset(&sa.sa_mask);
+	sigaction(SIGCONT, &sa, &sigcont_oldact);
+}
+
  static uint64_t wait_for_usage_drop(struct igt_cgroup *cg, const char *region,
  				    uint64_t limit)
  {
@@ -213,12 +272,102 @@ static void test_current(int fd, char *cg_region, unsigned int flags, const stru
  	igt_cgroup_free(cg);
  }
+static void test_write_eviction(int fd, char *cg_region, unsigned int flags, const struct igt_dmem_driver *drv, void *ctx)
+{
+	struct igt_cgroup *cg;
+	void **handles;
+	int max_bo;
+	uint64_t current, capacity, cg_max, limit, after;
+	int err;
+
+	igt_cgroup_dmem_get_capacity(cg_region, &capacity);
+	igt_require_f(capacity >= 4 * BO_SIZE,
+		      "VRAM capacity (%"PRIu64" MiB) too small to test\n",
+		      capacity / SZ_1M);
+
+	/*
+	 * Use up to 4 GiB, or the full capacity if the device has less.
+	 * Leave one BO_SIZE worth of headroom so the device isn't completely
+	 * exhausted before the cgroup limit is hit.
+	 */
Quoted 9 lines

tests/cgroup_dmem.c static void test_current(int fd, char *cg_region, unsigned int flags, const stru · line 297

Quoted 5 lines
tests/cgroup_dmem.c +5 -0
+	cg_max = min(MAX_LIMIT, capacity - BO_SIZE);
+	cg_max = ALIGN_DOWN(cg_max, EVICT_STEP);
+
+	if (flags & TEST_INTERRUPTIBLE)
+		install_sigcont_counter();

I stumbled upon IGT docs recommending igt_while_interruptible as a gentler alternative. Would that still work here and be simpler?

I will look into it.

Thomas, any comment about this?

Quoted 52 lines
Quoted 41 lines
tests/cgroup_dmem.c +41 -0
+
+	/* Create cgroup and move into it */
+	cg = igt_cgroup_new("igt_cgroups_test");
+	igt_cgroup_move_current(cg);
+	igt_cgroup_dmem_set_max(cg, cg_region, cg_max, false);
+
+	max_bo = (cg_max / BO_SIZE) + 8; /* headroom for overcommit */
+
+	handles = calloc(max_bo, sizeof(handles[0]));
+	igt_assert_f(handles, "failed to allocate handles array");
+
+	allocate_vram(handles, drv, ctx, max_bo, BO_SIZE);
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After fill: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+
+	igt_assert_f(current <= cg_max,
+		     "Usage %"PRIu64" MiB exceeds max %"PRIu64" MiB + slack\n",
+		     current / SZ_1M, cg_max / SZ_1M);
+
+	/* Phase 2: lower max in 256 MiB steps, verify usage follows */
+	limit = cg_max;
+	while (limit >= EVICT_STEP) {
+
+		limit -= EVICT_STEP;
+		igt_cgroup_dmem_set_max(cg, cg_region, limit, false);
+
+		igt_cgroup_dmem_get_current(cg, cg_region, &after);
+		igt_debug("Lowered max to %"PRIu64" MiB: usage = %"PRIu64" MiB\n",
+			  limit / SZ_1M, after / SZ_1M);
+
+		if (limit > EVICT_STEP) {
+			if ((flags & TEST_INTERRUPTIBLE) && after > limit) {
+				/* Let a new bo creation trigger eviction. */
+				void *handle;
+				err = drv->allocate_vram(ctx, BO_SIZE / 8, &handle);
+				igt_assert_f(err == 0,
+					"Error trying to allocate more VRAM to trigger eviction.");
+				drv->free_vram(ctx, handle);

tests/cgroup_dmem.c static void test_current(int fd, char *cg_region, unsigned int flags, const stru · line 348

Quoted 10 lines
tests/cgroup_dmem.c +10 -0
+
+				igt_cgroup_dmem_get_current(cg, cg_region, &after);
+				igt_debug("Forced eviction max is %"PRIu64
+					  " MiB: usage = %"PRIu64" MiB\n",
+					  limit / SZ_1M, after / SZ_1M);
+			}
+
+			igt_assert_f(after <= limit,
+				     "Usage %"PRIu64" MiB did not follow max %"PRIu64" MiB\n",
+				     after / SZ_1M, limit / SZ_1M);
Quoted 8 lines

I was quite confused by what is the interrupt by signal business about. My best guess is the point is not to interrupt the write to the sysfs on the superficial level, but to interrupt the actual eviction process which happens during the write? Is that a cgroup requirement that the new limit has to become effective (or attempted at least) during the write itself? And once interrupted it will not re-try it until new "activity" in the dmem cgroup happens (the dummy allocation above)?

The NONBLOCK write will trigger eviction and can be interrupted. Then, when we try to allocate again, it will trigger such eviction and dmem.max should be respected.

Thanks.
Cascardo.

Quoted 30 lines

Regards,

Tvrtko

Quoted 25 lines
tests/cgroup_dmem.c +16 -0
+		}
+	}
+
+	if (flags & TEST_INTERRUPTIBLE) {
+		igt_stop_signal_helper();
+		igt_info("Signals received during test: %d\n",
+			 atomic_load(&signal_count));
+	}
+
+	/* Cleanup */
+	igt_cgroup_dmem_set_max(cg, cg_region, IGT_CGROUP_DMEM_MAX, false);
+	igt_cgroup_free(cg);
+}
+
  static const struct {
  	const char *name;
  	void (*test_fn)(int fd, char *cg_region, unsigned int flags, const struct igt_dmem_driver *drv, void *ctx);
  	unsigned int flags;
  } subtests[] = {
  	{ "current", test_current, 0 },
+	{ "write_eviction", test_write_eviction, 0 },
+	{ "write_eviction_interruptible", test_write_eviction, TEST_INTERRUPTIBLE },
  	{ }
  };
 
#20 lore raw

Re: [PATCH i-g-t v6 8/9] tests/cgroup_dmem: add dmem cgroup eviction test

In reply to Thadeu Lima de Souza Cascardo

Hi Thadeu,

Quoted 48 lines · Thadeu Lima de Souza Cascardo

From: Thomas Hellström <[email protected]>

The write_eviction subtest:
- Creates a sub-cgroup and moves the test process into it.
- Sets a dmem.max limit on the first VRAM region (up to 4 GiB, or the full capacity if smaller).
- Fills VRAM by repeatedly creating BOs placed in VRAM, depending on card support.
- Verifies that cgroup current usage is within the expected range when the limit is hit.
- Lowers dmem.max in 128 MiB steps, waiting for usage to follow each reduction.

The write_eviction_interruptible subtest runs the same test with SIGCONT signals injected via igt_fork_signal_helper() and reports the number of signals received. When a signal interrupts kernel-side eviction, a small BO allocation is used to re-trigger it.

Assisted-by GitHub Copilot:claude-sonnet-4.6
---
 tests/cgroup_dmem.c | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 149 insertions(+)
tests/cgroup_dmem.c +5 -0
index ba5e6a2e3deb..0d3b415acd54 100644
@@ -36,9 +36,12 @@
 
 #define BO_SIZE			SZ_64M
 #define MAX_LIMIT		((uint64_t)4 * SZ_1G)
+#define EVICT_STEP		SZ_128M
 #define USAGE_POLL_MS		10
 #define USAGE_DROP_TIMEOUT_MS	1000
 
+#define TEST_INTERRUPTIBLE	(1 << 0)
+
 /**
  * SUBTEST: simple
  * DESCRIPTION:
@@ -61,6 +64,62 @@
  * REQUIREMENTS: xe or amdgpu device with at least one VRAM region
  */
 
+/**
+ * SUBTEST: write_eviction

tests/cgroup_dmem.c line 78

Quoted 10 lines
tests/cgroup_dmem.c +10 -0
+ * DESCRIPTION:
+ *   Create a dmem cgroup, move the current process into it and set the max
+ *   device memory limit for the first VRAM region to 4 GiB.  Then fill VRAM
+ *   by creating BOs with %DRM_XE_GEM_CREATE_FLAG_DEFER_BACKING (so that the
+ *   physical allocation is deferred until VM_BIND) and binding them into an
+ *   LR VM until the cgroup limit is hit.  Verify that the reported cgroup
+ *   current usage is within the expected range when the error occurs.
+ *   Finally lower the max limit in 256 MiB steps and verify that the cgroup
+ *   usage follows.
+ * REQUIREMENTS: must run as root; xe device with at least one VRAM region

We do not have a subfield 'REQUIREMENTS', so why did you introduced this? Second, almost all IGT tests should be run as root, so no point in writing this down. imho just add this at end of description:

* usage follows. This test requires GPU device with at least one VRAM region.

tests/cgroup_dmem.c line 91

Quoted 13 lines
tests/cgroup_dmem.c +13 -0
+ */
+
+/**
+ * SUBTEST: write_eviction_interruptible
+ * DESCRIPTION:
+ *   Same as write_eviction but with SIGCONT signals injected throughout via
+ *   igt_fork_signal_helper() to verify that the dmem.max write path handles
+ *   signal interruption correctly.  A signal handler counts received signals
+ *   and the count is reported as debug output at the end of the test.
+ *   A signal interrupts the set-time eviction, and further eviction can be
+ *   triggered by an explicit allocation.
+ * REQUIREMENTS: must run as root; xe device with at least one VRAM region
+ */

Same here.

Regards,
Kamil

Quoted 141 lines
tests/cgroup_dmem.c +121 -0
+
+static atomic_int signal_count;
+static struct sigaction sigcont_oldact;
+
+static void sigcont_handler(int sig)
+{
+	atomic_fetch_add(&signal_count, 1);
+
+	/* Chain to the previous handler (IGT's dummy sig_handler) */
+	if (sigcont_oldact.sa_handler &&
+	    sigcont_oldact.sa_handler != SIG_IGN &&
+	    sigcont_oldact.sa_handler != SIG_DFL)
+		sigcont_oldact.sa_handler(sig);
+}
+
+static void install_sigcont_counter(void)
+{
+	struct sigaction sa;
+
+	atomic_store(&signal_count, 0);
+	igt_fork_signal_helper();
+	/*
+	 * Install the counter after igt_fork_signal_helper() so our handler
+	 * is not overwritten.  Save the old handler so we can chain to it.
+	 */
+	memset(&sa, 0, sizeof(sa));
+	sa.sa_handler = sigcont_handler;
+	sigemptyset(&sa.sa_mask);
+	sigaction(SIGCONT, &sa, &sigcont_oldact);
+}
+
 static uint64_t wait_for_usage_drop(struct igt_cgroup *cg, const char *region,
 				    uint64_t limit)
 {
@@ -213,12 +272,102 @@ static void test_current(int fd, char *cg_region, unsigned int flags, const stru
 	igt_cgroup_free(cg);
 }
 
+static void test_write_eviction(int fd, char *cg_region, unsigned int flags, const struct igt_dmem_driver *drv, void *ctx)
+{
+	struct igt_cgroup *cg;
+	void **handles;
+	int max_bo;
+	uint64_t current, capacity, cg_max, limit, after;
+	int err;
+
+	igt_cgroup_dmem_get_capacity(cg_region, &capacity);
+	igt_require_f(capacity >= 4 * BO_SIZE,
+		      "VRAM capacity (%"PRIu64" MiB) too small to test\n",
+		      capacity / SZ_1M);
+
+	/*
+	 * Use up to 4 GiB, or the full capacity if the device has less.
+	 * Leave one BO_SIZE worth of headroom so the device isn't completely
+	 * exhausted before the cgroup limit is hit.
+	 */
+	cg_max = min(MAX_LIMIT, capacity - BO_SIZE);
+	cg_max = ALIGN_DOWN(cg_max, EVICT_STEP);
+
+	if (flags & TEST_INTERRUPTIBLE)
+		install_sigcont_counter();
+
+	/* Create cgroup and move into it */
+	cg = igt_cgroup_new("igt_cgroups_test");
+	igt_cgroup_move_current(cg);
+	igt_cgroup_dmem_set_max(cg, cg_region, cg_max, false);
+
+	max_bo = (cg_max / BO_SIZE) + 8; /* headroom for overcommit */
+
+	handles = calloc(max_bo, sizeof(handles[0]));
+	igt_assert_f(handles, "failed to allocate handles array");
+
+	allocate_vram(handles, drv, ctx, max_bo, BO_SIZE);
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After fill: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+
+	igt_assert_f(current <= cg_max,
+		     "Usage %"PRIu64" MiB exceeds max %"PRIu64" MiB + slack\n",
+		     current / SZ_1M, cg_max / SZ_1M);
+
+	/* Phase 2: lower max in 256 MiB steps, verify usage follows */
+	limit = cg_max;
+	while (limit >= EVICT_STEP) {
+
+		limit -= EVICT_STEP;
+		igt_cgroup_dmem_set_max(cg, cg_region, limit, false);
+
+		igt_cgroup_dmem_get_current(cg, cg_region, &after);
+		igt_debug("Lowered max to %"PRIu64" MiB: usage = %"PRIu64" MiB\n",
+			  limit / SZ_1M, after / SZ_1M);
+
+		if (limit > EVICT_STEP) {
+			if ((flags & TEST_INTERRUPTIBLE) && after > limit) {
+				/* Let a new bo creation trigger eviction. */
+				void *handle;
+				err = drv->allocate_vram(ctx, BO_SIZE / 8, &handle);
+				igt_assert_f(err == 0,
+					"Error trying to allocate more VRAM to trigger eviction.");
+				drv->free_vram(ctx, handle);
+
+				igt_cgroup_dmem_get_current(cg, cg_region, &after);
+				igt_debug("Forced eviction max is %"PRIu64
+					  " MiB: usage = %"PRIu64" MiB\n",
+					  limit / SZ_1M, after / SZ_1M);
+			}
+
+			igt_assert_f(after <= limit,
+				     "Usage %"PRIu64" MiB did not follow max %"PRIu64" MiB\n",
+				     after / SZ_1M, limit / SZ_1M);
+		}
+	}
+
+	if (flags & TEST_INTERRUPTIBLE) {
+		igt_stop_signal_helper();
+		igt_info("Signals received during test: %d\n",
+			 atomic_load(&signal_count));
+	}
+
+	/* Cleanup */
+	igt_cgroup_dmem_set_max(cg, cg_region, IGT_CGROUP_DMEM_MAX, false);
+	igt_cgroup_free(cg);
+}
+
 static const struct {
 	const char *name;
 	void (*test_fn)(int fd, char *cg_region, unsigned int flags, const struct igt_dmem_driver *drv, void *ctx);
 	unsigned int flags;
 } subtests[] = {
 	{ "current", test_current, 0 },
+	{ "write_eviction", test_write_eviction, 0 },
+	{ "write_eviction_interruptible", test_write_eviction, TEST_INTERRUPTIBLE },
 	{ }
 };
 
Signature · 2 lines
-- 
2.47.3
#21 lore raw

PATCH v6 9/9 tests/cgroup_dmem: add write_eviction_nonblock subtest

In reply to Thadeu Lima de Souza Cascardo

From: Thomas Hellström <[email protected]>

Add write_eviction_nonblock to exercise the O_NONBLOCK path of the dmem cgroup max interface. After filling VRAM to the cgroup limit, each limit-lowering step writes dmem.max with O_NONBLOCK so that synchronous eviction is skipped. The test then verifies that usage has not yet dropped below the new limit, allocates a small BO to trigger eviction explicitly, and finally confirms that usage falls within bounds.

Assisted-by GitHub Copilot:claude-sonnet-4.6
---
 tests/cgroup_dmem.c | 37 ++++++++++++++++++++++++++++++++++---
 1 file changed, 34 insertions(+), 3 deletions(-)
tests/cgroup_dmem.c +34 -3
index 0d3b415acd54..ee6345c17878 100644
@@ -41,6 +41,7 @@
 #define USAGE_DROP_TIMEOUT_MS	1000
 
 #define TEST_INTERRUPTIBLE	(1 << 0)
+#define TEST_NONBLOCK		(1 << 1)
 
 /**
  * SUBTEST: simple
@@ -90,6 +91,18 @@
  * REQUIREMENTS: must run as root; xe device with at least one VRAM region
  */
 
+/**
+ * SUBTEST: write_eviction_nonblock
+ * DESCRIPTION:
+ *   Same fill phase as write_eviction.  In the limit-lowering phase dmem.max
+ *   is written with O_NONBLOCK, which causes the kernel to skip synchronous
+ *   eviction.  After each nonblock write the test verifies that usage has not
+ *   yet dropped below the new limit, then triggers eviction explicitly by
+ *   allocating a small BO.  Finally verifies that usage falls within bounds
+ *   after the forced eviction.
+ * REQUIREMENTS: must run as root; xe device with at least one VRAM region
+ */
+
 static atomic_int signal_count;
 static struct sigaction sigcont_oldact;
 
@@ -277,7 +290,7 @@ static void test_write_eviction(int fd, char *cg_region, unsigned int flags, con
 	struct igt_cgroup *cg;
 	void **handles;
 	int max_bo;
-	uint64_t current, capacity, cg_max, limit, after;
+	uint64_t current, capacity, cg_max, limit, after, before;
 	int err;
 
 	igt_cgroup_dmem_get_capacity(cg_region, &capacity);
@@ -322,14 +335,31 @@ static void test_write_eviction(int fd, char *cg_region, unsigned int flags, con
 	while (limit >= EVICT_STEP) {
 
 		limit -= EVICT_STEP;
-		igt_cgroup_dmem_set_max(cg, cg_region, limit, false);
+
+		if (flags & TEST_NONBLOCK)
+			igt_cgroup_dmem_get_current(cg, cg_region, &before);
+
+		igt_cgroup_dmem_set_max(cg, cg_region, limit,
+					!!(flags & TEST_NONBLOCK));
 
 		igt_cgroup_dmem_get_current(cg, cg_region, &after);
 		igt_debug("Lowered max to %"PRIu64" MiB: usage = %"PRIu64" MiB\n",
 			  limit / SZ_1M, after / SZ_1M);
 
+		if (flags & TEST_NONBLOCK) {
+			/*
+			 * O_NONBLOCK skips eviction: verify usage has not
+			 * dropped below the new limit yet.
+			 */
+			igt_assert_f(after == before,
+				     "Expected no eviction with O_NONBLOCK, but "
+				     "usage dropped from %"PRIu64" MiB to %"PRIu64" MiB "
+				     "(limit %"PRIu64" MiB)\n",
+				     before / SZ_1M, after / SZ_1M, limit / SZ_1M);
+		}
+
 		if (limit > EVICT_STEP) {
-			if ((flags & TEST_INTERRUPTIBLE) && after > limit) {
+			if ((flags & (TEST_INTERRUPTIBLE | TEST_NONBLOCK)) && after > limit) {
 				/* Let a new bo creation trigger eviction. */
 				void *handle;
 				err = drv->allocate_vram(ctx, BO_SIZE / 8, &handle);
@@ -368,6 +398,7 @@ static const struct {
 	{ "current", test_current, 0 },
 	{ "write_eviction", test_write_eviction, 0 },
 	{ "write_eviction_interruptible", test_write_eviction, TEST_INTERRUPTIBLE },
+	{ "write_eviction_nonblock", test_write_eviction, TEST_NONBLOCK },
 	{ }
 };
 
Signature · 1 line
2.47.3
#22 lore raw

Re: [PATCH i-g-t v6 9/9] tests/cgroup_dmem: add write_eviction_nonblock subtest

Quoted 67 lines · Thadeu Lima de Souza Cascardo

From: Thomas Hellström <[email protected]>

Add write_eviction_nonblock to exercise the O_NONBLOCK path of the dmem cgroup max interface. After filling VRAM to the cgroup limit, each limit-lowering step writes dmem.max with O_NONBLOCK so that synchronous eviction is skipped. The test then verifies that usage has not yet dropped below the new limit, allocates a small BO to trigger eviction explicitly, and finally confirms that usage falls within bounds.

Assisted-by GitHub Copilot:claude-sonnet-4.6
---
  tests/cgroup_dmem.c | 37 ++++++++++++++++++++++++++++++++++---
  1 file changed, 34 insertions(+), 3 deletions(-)
tests/cgroup_dmem.c +20 -2
index 0d3b415acd54..ee6345c17878 100644
@@ -41,6 +41,7 @@
  #define USAGE_DROP_TIMEOUT_MS	1000
 
  #define TEST_INTERRUPTIBLE	(1 << 0)
+#define TEST_NONBLOCK		(1 << 1)
 
  /**
   * SUBTEST: simple
@@ -90,6 +91,18 @@
   * REQUIREMENTS: must run as root; xe device with at least one VRAM region
   */
 
+/**
+ * SUBTEST: write_eviction_nonblock
+ * DESCRIPTION:
+ *   Same fill phase as write_eviction.  In the limit-lowering phase dmem.max
+ *   is written with O_NONBLOCK, which causes the kernel to skip synchronous
+ *   eviction.  After each nonblock write the test verifies that usage has not
+ *   yet dropped below the new limit, then triggers eviction explicitly by
+ *   allocating a small BO.  Finally verifies that usage falls within bounds
+ *   after the forced eviction.
+ * REQUIREMENTS: must run as root; xe device with at least one VRAM region
+ */
+
  static atomic_int signal_count;
  static struct sigaction sigcont_oldact;
 
@@ -277,7 +290,7 @@ static void test_write_eviction(int fd, char *cg_region, unsigned int flags, con
  	struct igt_cgroup *cg;
  	void **handles;
  	int max_bo;
-	uint64_t current, capacity, cg_max, limit, after;
+	uint64_t current, capacity, cg_max, limit, after, before;
  	int err;
 
  	igt_cgroup_dmem_get_capacity(cg_region, &capacity);
@@ -322,14 +335,31 @@ static void test_write_eviction(int fd, char *cg_region, unsigned int flags, con
  	while (limit >= EVICT_STEP) {
 
  		limit -= EVICT_STEP;
-		igt_cgroup_dmem_set_max(cg, cg_region, limit, false);
+
+		if (flags & TEST_NONBLOCK)
+			igt_cgroup_dmem_get_current(cg, cg_region, &before);
+
+		igt_cgroup_dmem_set_max(cg, cg_region, limit,
+					!!(flags & TEST_NONBLOCK));

tests/cgroup_dmem.c static void test_write_eviction(int fd, char *cg_region, unsigned int flags, con · line 353

Quoted 10 lines
tests/cgroup_dmem.c +5 -0
 
  		igt_cgroup_dmem_get_current(cg, cg_region, &after);
  		igt_debug("Lowered max to %"PRIu64" MiB: usage = %"PRIu64" MiB\n",
  			  limit / SZ_1M, after / SZ_1M);
 
+		if (flags & TEST_NONBLOCK) {
+			/*
+			 * O_NONBLOCK skips eviction: verify usage has not
+			 * dropped below the new limit yet.
+			 */

Is this userspace ABI contract or happens to be? It feels odd - even if we ask for non block for the write why would kernel not be allowed to do stuff behind the covers?

Regards,

Tvrtko

Quoted 22 lines
tests/cgroup_dmem.c +9 -1
+			igt_assert_f(after == before,
+				     "Expected no eviction with O_NONBLOCK, but "
+				     "usage dropped from %"PRIu64" MiB to %"PRIu64" MiB "
+				     "(limit %"PRIu64" MiB)\n",
+				     before / SZ_1M, after / SZ_1M, limit / SZ_1M);
+		}
+
  		if (limit > EVICT_STEP) {
-			if ((flags & TEST_INTERRUPTIBLE) && after > limit) {
+			if ((flags & (TEST_INTERRUPTIBLE | TEST_NONBLOCK)) && after > limit) {
  				/* Let a new bo creation trigger eviction. */
  				void *handle;
  				err = drv->allocate_vram(ctx, BO_SIZE / 8, &handle);
@@ -368,6 +398,7 @@ static const struct {
  	{ "current", test_current, 0 },
  	{ "write_eviction", test_write_eviction, 0 },
  	{ "write_eviction_interruptible", test_write_eviction, TEST_INTERRUPTIBLE },
+	{ "write_eviction_nonblock", test_write_eviction, TEST_NONBLOCK },
  	{ }
  };
 
#23 lore raw

Re: [PATCH i-g-t v6 9/9] tests/cgroup_dmem: add write_eviction_nonblock subtest

Quoted 67 lines · Tvrtko Ursulin
Quoted 65 lines · Thadeu Lima de Souza Cascardo

From: Thomas Hellström <[email protected]>

Add write_eviction_nonblock to exercise the O_NONBLOCK path of the dmem cgroup max interface. After filling VRAM to the cgroup limit, each limit-lowering step writes dmem.max with O_NONBLOCK so that synchronous eviction is skipped. The test then verifies that usage has not yet dropped below the new limit, allocates a small BO to trigger eviction explicitly, and finally confirms that usage falls within bounds.

Assisted-by GitHub Copilot:claude-sonnet-4.6
---
  tests/cgroup_dmem.c | 37 ++++++++++++++++++++++++++++++++++---
  1 file changed, 34 insertions(+), 3 deletions(-)
tests/cgroup_dmem.c +21 -2
index 0d3b415acd54..ee6345c17878 100644
@@ -41,6 +41,7 @@
  #define USAGE_DROP_TIMEOUT_MS	1000
  #define TEST_INTERRUPTIBLE	(1 << 0)
+#define TEST_NONBLOCK		(1 << 1)
  /**
   * SUBTEST: simple
@@ -90,6 +91,18 @@
   * REQUIREMENTS: must run as root; xe device with at least one VRAM region
   */
+/**
+ * SUBTEST: write_eviction_nonblock
+ * DESCRIPTION:
+ *   Same fill phase as write_eviction.  In the limit-lowering phase dmem.max
+ *   is written with O_NONBLOCK, which causes the kernel to skip synchronous
+ *   eviction.  After each nonblock write the test verifies that usage has not
+ *   yet dropped below the new limit, then triggers eviction explicitly by
+ *   allocating a small BO.  Finally verifies that usage falls within bounds
+ *   after the forced eviction.
+ * REQUIREMENTS: must run as root; xe device with at least one VRAM region
+ */
+
  static atomic_int signal_count;
  static struct sigaction sigcont_oldact;
@@ -277,7 +290,7 @@ static void test_write_eviction(int fd, char *cg_region, unsigned int flags, con
  	struct igt_cgroup *cg;
  	void **handles;
  	int max_bo;
-	uint64_t current, capacity, cg_max, limit, after;
+	uint64_t current, capacity, cg_max, limit, after, before;
  	int err;
  	igt_cgroup_dmem_get_capacity(cg_region, &capacity);
@@ -322,14 +335,31 @@ static void test_write_eviction(int fd, char *cg_region, unsigned int flags, con
  	while (limit >= EVICT_STEP) {
  		limit -= EVICT_STEP;
-		igt_cgroup_dmem_set_max(cg, cg_region, limit, false);
+
+		if (flags & TEST_NONBLOCK)
+			igt_cgroup_dmem_get_current(cg, cg_region, &before);
+
+		igt_cgroup_dmem_set_max(cg, cg_region, limit,
+					!!(flags & TEST_NONBLOCK));
  		igt_cgroup_dmem_get_current(cg, cg_region, &after);
  		igt_debug("Lowered max to %"PRIu64" MiB: usage = %"PRIu64" MiB\n",
  			  limit / SZ_1M, after / SZ_1M);
+		if (flags & TEST_NONBLOCK) {
Quoted 9 lines

tests/cgroup_dmem.c static void test_write_eviction(int fd, char *cg_region, unsigned int flags, con · line 353

Quoted 4 lines
tests/cgroup_dmem.c +4 -0
+			/*
+			 * O_NONBLOCK skips eviction: verify usage has not
+			 * dropped below the new limit yet.
+			 */

Is this userspace ABI contract or happens to be? It feels odd - even if we ask for non block for the write why would kernel not be allowed to do stuff behind the covers?

You bring up an interesting perspective. So maybe the test below might fail. But we are assuming the test is running with no other users, so this would be unexpected. Unless we setup dmem.min, there is no guarantee that eviction won't happen. However, I think the test is still useful as it is. We are checking that nonblocking behavior will not trigger eviction right away. We risk not catching up such regression if we leave it out. If we end up noticing on CI that this happens, we can investigate and revisit.

Thanks.
Cascardo.

Quoted 26 lines

Regards,

Tvrtko

Quoted 21 lines
tests/cgroup_dmem.c +9 -1
+			igt_assert_f(after == before,
+				     "Expected no eviction with O_NONBLOCK, but "
+				     "usage dropped from %"PRIu64" MiB to %"PRIu64" MiB "
+				     "(limit %"PRIu64" MiB)\n",
+				     before / SZ_1M, after / SZ_1M, limit / SZ_1M);
+		}
+
  		if (limit > EVICT_STEP) {
-			if ((flags & TEST_INTERRUPTIBLE) && after > limit) {
+			if ((flags & (TEST_INTERRUPTIBLE | TEST_NONBLOCK)) && after > limit) {
  				/* Let a new bo creation trigger eviction. */
  				void *handle;
  				err = drv->allocate_vram(ctx, BO_SIZE / 8, &handle);
@@ -368,6 +398,7 @@ static const struct {
  	{ "current", test_current, 0 },
  	{ "write_eviction", test_write_eviction, 0 },
  	{ "write_eviction_interruptible", test_write_eviction, TEST_INTERRUPTIBLE },
+	{ "write_eviction_nonblock", test_write_eviction, TEST_NONBLOCK },
  	{ }
  };
 
#24 lore raw

Re: [PATCH i-g-t v6 9/9] tests/cgroup_dmem: add write_eviction_nonblock subtest

Quoted 78 lines · Thadeu Lima de Souza Cascardo
Quoted 67 lines · Tvrtko Ursulin
Quoted 65 lines · Thadeu Lima de Souza Cascardo

From: Thomas Hellström <[email protected]>

Add write_eviction_nonblock to exercise the O_NONBLOCK path of the dmem cgroup max interface. After filling VRAM to the cgroup limit, each limit-lowering step writes dmem.max with O_NONBLOCK so that synchronous eviction is skipped. The test then verifies that usage has not yet dropped below the new limit, allocates a small BO to trigger eviction explicitly, and finally confirms that usage falls within bounds.

Assisted-by GitHub Copilot:claude-sonnet-4.6
---
   tests/cgroup_dmem.c | 37 ++++++++++++++++++++++++++++++++++---
   1 file changed, 34 insertions(+), 3 deletions(-)
tests/cgroup_dmem.c +21 -2
index 0d3b415acd54..ee6345c17878 100644
@@ -41,6 +41,7 @@
   #define USAGE_DROP_TIMEOUT_MS	1000
   #define TEST_INTERRUPTIBLE	(1 << 0)
+#define TEST_NONBLOCK		(1 << 1)
   /**
    * SUBTEST: simple
@@ -90,6 +91,18 @@
    * REQUIREMENTS: must run as root; xe device with at least one VRAM region
    */
+/**
+ * SUBTEST: write_eviction_nonblock
+ * DESCRIPTION:
+ *   Same fill phase as write_eviction.  In the limit-lowering phase dmem.max
+ *   is written with O_NONBLOCK, which causes the kernel to skip synchronous
+ *   eviction.  After each nonblock write the test verifies that usage has not
+ *   yet dropped below the new limit, then triggers eviction explicitly by
+ *   allocating a small BO.  Finally verifies that usage falls within bounds
+ *   after the forced eviction.
+ * REQUIREMENTS: must run as root; xe device with at least one VRAM region
+ */
+
   static atomic_int signal_count;
   static struct sigaction sigcont_oldact;
@@ -277,7 +290,7 @@ static void test_write_eviction(int fd, char *cg_region, unsigned int flags, con
   	struct igt_cgroup *cg;
   	void **handles;
   	int max_bo;
-	uint64_t current, capacity, cg_max, limit, after;
+	uint64_t current, capacity, cg_max, limit, after, before;
   	int err;
   	igt_cgroup_dmem_get_capacity(cg_region, &capacity);
@@ -322,14 +335,31 @@ static void test_write_eviction(int fd, char *cg_region, unsigned int flags, con
   	while (limit >= EVICT_STEP) {
   		limit -= EVICT_STEP;
-		igt_cgroup_dmem_set_max(cg, cg_region, limit, false);
+
+		if (flags & TEST_NONBLOCK)
+			igt_cgroup_dmem_get_current(cg, cg_region, &before);
+
+		igt_cgroup_dmem_set_max(cg, cg_region, limit,
+					!!(flags & TEST_NONBLOCK));
   		igt_cgroup_dmem_get_current(cg, cg_region, &after);
   		igt_debug("Lowered max to %"PRIu64" MiB: usage = %"PRIu64" MiB\n",
   			  limit / SZ_1M, after / SZ_1M);
+		if (flags & TEST_NONBLOCK) {
Quoted 9 lines

tests/cgroup_dmem.c static void test_write_eviction(int fd, char *cg_region, unsigned int flags, con · line 353

Quoted 4 lines
tests/cgroup_dmem.c +4 -0
+			/*
+			 * O_NONBLOCK skips eviction: verify usage has not
+			 * dropped below the new limit yet.
+			 */

Is this userspace ABI contract or happens to be? It feels odd - even if we ask for non block for the write why would kernel not be allowed to do stuff behind the covers?

Quoted 7 lines

You bring up an interesting perspective. So maybe the test below might fail. But we are assuming the test is running with no other users, so this would be unexpected. Unless we setup dmem.min, there is no guarantee that eviction won't happen. However, I think the test is still useful as it is. We are checking that nonblocking behavior will not trigger eviction right away. We risk not catching up such regression if we leave it out. If we end up noticing on CI that this happens, we can investigate and revisit.

I looked at the commit which added it:

commit 747c4bb450adcd8b99d3543448419e1952755832

Author: Thomas Hellström <[email protected]>
Date:   Sat Jul 25 12:00:33 2026 +0200

cgroup/dmem: Add reclaim callback for lowering max below current usage

...

Also honor O_NONBLOCK so that if that flag is set during the max value write, no reclaim is initiated. The idea is to avoid charging the reclaim cost to the writer of the max value.

So it was by design. I assume the charging avoidance is for the case when eviction needs to allocate in a different placement. Perhaps a bit of a kludge but perhaps I also miss some historical conversations about it.

Either way, the test itself is a-okay.

Regards,

Tvrtko

Quoted 22 lines
Quoted 22 lines
Quoted 21 lines
tests/cgroup_dmem.c +9 -1
+			igt_assert_f(after == before,
+				     "Expected no eviction with O_NONBLOCK, but "
+				     "usage dropped from %"PRIu64" MiB to %"PRIu64" MiB "
+				     "(limit %"PRIu64" MiB)\n",
+				     before / SZ_1M, after / SZ_1M, limit / SZ_1M);
+		}
+
   		if (limit > EVICT_STEP) {
-			if ((flags & TEST_INTERRUPTIBLE) && after > limit) {
+			if ((flags & (TEST_INTERRUPTIBLE | TEST_NONBLOCK)) && after > limit) {
   				/* Let a new bo creation trigger eviction. */
   				void *handle;
   				err = drv->allocate_vram(ctx, BO_SIZE / 8, &handle);
@@ -368,6 +398,7 @@ static const struct {
   	{ "current", test_current, 0 },
   	{ "write_eviction", test_write_eviction, 0 },
   	{ "write_eviction_interruptible", test_write_eviction, TEST_INTERRUPTIBLE },
+	{ "write_eviction_nonblock", test_write_eviction, TEST_NONBLOCK },
   	{ }
   };