Skip to contentSkip to conversation

Introduce KUnit tests for TTM subsystem

RFC Superseded Karolina Stolarek 2 participants 8 messages Jul 14, 2023 – Aug 8, 2023

Subsystems (3)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover Introduce KUnit tests for TTM subsystem

This series introduces KUnit[1] tests for TTM (Translation Table Manager) subsystem, a memory manager used by graphics drivers to create and manage memory buffers across different memory domains, such as system memory or VRAM.

Unit tests implemented here cover two data structures:
- ttm_device -- referred as a buffer object device, which stores resource managers and page pools
- ttm_pool -- a struct of pools (ttm_pool_type) of different page orders and caching attributes, with pages that can be reused on the next buffer allocation

Use kunit_tool script to manually run the tests:

$ ./tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm/ttm/tests

To build a kernel with TTM KUnit tests, first enable CONFIG_KUNIT, and then CONFIG_DRM_TTM_KUNIT_TEST.

As for now, tests are architecture-agnostic (i.e. KUnit runner uses UML kernel), which means that we have limited coverage in some places. For example, we can't fully test the initialization of global page pools, such as global_write_combined. It is to be decided if we want to stick to UML or use CONFIG_X86 (at least to some extent).

These patches are just a beginning of the work to improve the test coverage of TTM. Feel free to suggest changes, test cases or priorities.

Many thanks,
Karolina

v5:
- Drop unnecessary brackets in 2/3
- Rebase KConfig file on the top of drm-tip

v4:
- Test helpers have been changed to make the creation of init/fini functions for each test suite easier:
+ Decouple device creation from test initialization by adding helpers that initialize ttm_test_devices, a struct which stores DRM/TTM devices, and can be used in test-specific init/finis (see ttm_pool_tests.c for an example)
+ Introduce generic init/fini functions for tests that only need devices
+ Add ttm_device field to ttm_test_devices (previously ttm_test_devices_priv)
- Make TTM buffer object outlive its TT (Christian)
- Add a dedicated struct for ttm_pool_test (struct ttm_pool_test_priv)
- Rename functions and structs:
+ struct ttm_test_devices_priv --> struct ttm_test_devices
+ ttm_kunit_helper_init_device() --> ttm_device_kunit_init()
+ ttm_kunit_helper_ttm_bo_init() --> ttm_bo_kunit_init()
- Split ttm_kunit_helper_init() into full config (with ttm_device init) and basic (init only with device/drm_device) initialization functions

v3:
- Rename ttm_kunit_helper_alloc_device() to ttm_kunit_helper_init_device() (Christian)
- Don't leak a full-blown drm_gem_object in ttm_kunit_helper_ttm_bo_init(). (Christian). Create a small mock object just to get ttm_tt_init_fields() to init the right number of pages
- As a follow up to the change above, delete ttm_kunit_helper_ttm_bo_fini() and just use ttm_bo_put()

v2:
- Add missing symbol exports in ttm_kunit_helpers.c
- Update helpers include to fix compilation issues (didn't catch it as KUnit tests weren't enabled in the kernel I tested, an oversight on my part)
- Add checks for ttm_pool fields in ttm_pool_alloc_basic(), including the one for NUMA node id
- Rebase the changes on the top of drm-tip

--------------------------------
[1] - https://www.kernel.org/doc/html/latest/dev-tools/kunit/index.html

Karolina Stolarek (3):
drm/ttm: Introduce KUnit test
drm/ttm/tests: Add tests for ttm_device
drm/ttm/tests: Add tests for ttm_pool

 drivers/gpu/drm/Kconfig                       |  15 +
 drivers/gpu/drm/ttm/Makefile                  |   1 +
 drivers/gpu/drm/ttm/tests/.kunitconfig        |   4 +
 drivers/gpu/drm/ttm/tests/Makefile            |   6 +
 drivers/gpu/drm/ttm/tests/ttm_device_test.c   | 212 +++++++++
 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c | 115 +++++
 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h |  41 ++
 drivers/gpu/drm/ttm/tests/ttm_pool_test.c     | 441 ++++++++++++++++++
 8 files changed, 835 insertions(+)
 create mode 100644 drivers/gpu/drm/ttm/tests/.kunitconfig
 create mode 100644 drivers/gpu/drm/ttm/tests/Makefile
 create mode 100644 drivers/gpu/drm/ttm/tests/ttm_device_test.c
 create mode 100644 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c
 create mode 100644 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h
 create mode 100644 drivers/gpu/drm/ttm/tests/ttm_pool_test.c
Signature · 1 line
2.25.1
#1 lore raw

RFC v5 1/3 drm/ttm: Introduce KUnit test

Add the initial version of unit tests for ttm_device struct, together with helper functions.

---
 drivers/gpu/drm/Kconfig                       | 15 +++
 drivers/gpu/drm/ttm/Makefile                  |  1 +
 drivers/gpu/drm/ttm/tests/.kunitconfig        |  4 +
 drivers/gpu/drm/ttm/tests/Makefile            |  5 +
 drivers/gpu/drm/ttm/tests/ttm_device_test.c   | 54 ++++++++++
 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c | 98 +++++++++++++++++++
 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h | 37 +++++++
 7 files changed, 214 insertions(+)
 create mode 100644 drivers/gpu/drm/ttm/tests/.kunitconfig
 create mode 100644 drivers/gpu/drm/ttm/tests/Makefile
 create mode 100644 drivers/gpu/drm/ttm/tests/ttm_device_test.c
 create mode 100644 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c
 create mode 100644 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h
drivers/gpu/drm/Kconfig +15 -0
index 22c1ba9ea28c..3203204bc63b 100644
@@ -195,6 +195,21 @@ config DRM_TTM
 	  GPU memory types. Will be enabled automatically if a device driver
 	  uses it.
 
+config DRM_TTM_KUNIT_TEST
+        tristate "KUnit tests for TTM" if !KUNIT_ALL_TESTS
+        default n
+        depends on DRM && KUNIT
+        select DRM_TTM
+        select DRM_EXPORT_FOR_TESTS if m
+        select DRM_KUNIT_TEST_HELPERS
+        default KUNIT_ALL_TESTS
+        help
+          Enables unit tests for TTM, a GPU memory manager subsystem used
+          to manage memory buffers. This option is mostly useful for kernel
+          developers.
+
+          If in doubt, say "N".
+
 config DRM_EXEC
 	tristate
 	depends on DRM
drivers/gpu/drm/ttm/Makefile +1 -0
index f906b22959cf..dad298127226 100644
@@ -8,3 +8,4 @@ ttm-y := ttm_tt.o ttm_bo.o ttm_bo_util.o ttm_bo_vm.o ttm_module.o \
 ttm-$(CONFIG_AGP) += ttm_agp_backend.o
 
 obj-$(CONFIG_DRM_TTM) += ttm.o
+obj-$(CONFIG_DRM_TTM_KUNIT_TEST) += tests/
drivers/gpu/drm/ttm/tests/.kunitconfig new file +4 -0
new file mode 100644
index 000000000000..75fdce0cd98e
@@ -0,0 +1,4 @@
+CONFIG_KUNIT=y
+CONFIG_DRM=y
+CONFIG_DRM_KUNIT_TEST_HELPERS=y
+CONFIG_DRM_TTM_KUNIT_TEST=y
drivers/gpu/drm/ttm/tests/Makefile new file +5 -0
new file mode 100644
index 000000000000..7917805f37af
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0 AND MIT
+
+obj-$(CONFIG_DRM_TTM_KUNIT_TEST) += \
+        ttm_device_test.o \
+        ttm_kunit_helpers.o
drivers/gpu/drm/ttm/tests/ttm_device_test.c new file +54 -0

54 lines not on this page · raw message

drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c new file +98 -0

98 lines not on this page · raw message

drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h new file +37 -0
new file mode 100644
index 000000000000..f9f5bc03e93a
@@ -0,0 +1,37 @@
+/* SPDX-License-Identifier: GPL-2.0 AND MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+#ifndef TTM_KUNIT_HELPERS_H
+#define TTM_KUNIT_HELPERS_H
+
+#include <drm/drm_drv.h>
+#include <drm/ttm/ttm_device.h>
+
+#include <drm/drm_kunit_helpers.h>
+#include <kunit/test.h>
+
+extern struct ttm_device_funcs ttm_dev_funcs;
+
+struct ttm_test_devices {
+	struct drm_device *drm;
+	struct device *dev;
+	struct ttm_device *ttm_dev;
+};
+
+/* Building blocks for test-specific init functions */
+int ttm_device_kunit_init(struct ttm_test_devices *priv,
+			  struct ttm_device *ttm,
+			  bool use_dma_alloc,
+			  bool use_dma32);
+
+struct ttm_test_devices *ttm_test_devices_basic(struct kunit *test);
+struct ttm_test_devices *ttm_test_devices_all(struct kunit *test);
+
+void ttm_test_devices_put(struct kunit *test, struct ttm_test_devices *devs);
+
+/* Generic init/fini for tests that only need DRM/TTM devices */
+int ttm_test_devices_init(struct kunit *test);
+void ttm_test_devices_fini(struct kunit *test);
+
+#endif // TTM_KUNIT_HELPERS_H
Signature · 1 line
2.25.1
#2 lore raw

RFC v5 2/3 drm/ttm/tests: Add tests for ttm_device

In reply to Karolina Stolarek

Test initialization and cleanup of the ttm_device struct, including some error paths. Verify the creation of page pools if use_dma_alloc param is true.

---
 drivers/gpu/drm/ttm/tests/ttm_device_test.c | 158 ++++++++++++++++++++
 1 file changed, 158 insertions(+)
drivers/gpu/drm/ttm/tests/ttm_device_test.c +158 -0
index 76d927d07501..b1b423b68cdf 100644
@@ -8,6 +8,13 @@
 
 #include "ttm_kunit_helpers.h"
 
+struct ttm_device_test_case {
+	const char *description;
+	bool use_dma_alloc;
+	bool use_dma32;
+	bool pools_init_expected;
+};
+
 static void ttm_device_init_basic(struct kunit *test)
 {
 	struct ttm_test_devices *priv = test->priv;
@@ -37,8 +44,159 @@ static void ttm_device_init_basic(struct kunit *test)
 	ttm_device_fini(ttm_dev);
 }
 
+static void ttm_device_init_multiple(struct kunit *test)
+{
+	struct ttm_test_devices *priv = test->priv;
+	struct ttm_device *ttm_devs;
+	unsigned int i, num_dev = 3;
+	int err;
+
+	ttm_devs = kunit_kcalloc(test, num_dev, sizeof(*ttm_devs), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, ttm_devs);
+
+	for (i = 0; i < num_dev; i++) {
+		err = ttm_device_kunit_init(priv, &ttm_devs[i], false, false);
+		KUNIT_ASSERT_EQ(test, err, 0);
+
+		KUNIT_EXPECT_PTR_EQ(test, ttm_devs[i].dev_mapping,
+				    priv->drm->anon_inode->i_mapping);
+		KUNIT_ASSERT_NOT_NULL(test, ttm_devs[i].wq);
+		KUNIT_EXPECT_PTR_EQ(test, ttm_devs[i].funcs, &ttm_dev_funcs);
+		KUNIT_ASSERT_NOT_NULL(test, ttm_devs[i].man_drv[TTM_PL_SYSTEM]);
+	}
+
+	KUNIT_ASSERT_EQ(test, list_count_nodes(&ttm_devs[0].device_list), num_dev);
+
+	for (i = 0; i < num_dev; i++)
+		ttm_device_fini(&ttm_devs[i]);
+}
+
+static void ttm_device_fini_basic(struct kunit *test)
+{
+	struct ttm_test_devices *priv = test->priv;
+	struct ttm_device *ttm_dev;
+	struct ttm_resource_manager *man;
+	int err;
+
+	ttm_dev = kunit_kzalloc(test, sizeof(*ttm_dev), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, ttm_dev);
+
+	err = ttm_device_kunit_init(priv, ttm_dev, false, false);
+	KUNIT_ASSERT_EQ(test, err, 0);
+
+	man = ttm_manager_type(ttm_dev, TTM_PL_SYSTEM);
+	KUNIT_ASSERT_NOT_NULL(test, man);
+
+	ttm_device_fini(ttm_dev);
+
+	KUNIT_ASSERT_FALSE(test, man->use_type);
+	KUNIT_ASSERT_TRUE(test, list_empty(&man->lru[0]));
+	KUNIT_ASSERT_NULL(test, ttm_dev->man_drv[TTM_PL_SYSTEM]);
+}
+
+static void ttm_device_init_no_vma_man(struct kunit *test)
+{
+	struct ttm_test_devices *priv = test->priv;
+	struct drm_device *drm = priv->drm;
+	struct ttm_device *ttm_dev;
+	struct drm_vma_offset_manager *vma_man;
+	int err;
+
+	ttm_dev = kunit_kzalloc(test, sizeof(*ttm_dev), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, ttm_dev);
+
+	/* Let's pretend there's no VMA manager allocated */
+	vma_man = drm->vma_offset_manager;
+	drm->vma_offset_manager = NULL;
+
+	err = ttm_device_kunit_init(priv, ttm_dev, false, false);
+	KUNIT_EXPECT_EQ(test, err, -EINVAL);
+
+	/* Bring the manager back for a graceful cleanup */
+	drm->vma_offset_manager = vma_man;
+}
+
+static const struct ttm_device_test_case ttm_device_cases[] = {
+	{
+		.description = "No DMA allocations, no DMA32 required",
+		.use_dma_alloc = false,
+		.use_dma32 = false,
+		.pools_init_expected = false,
+	},
+	{
+		.description = "DMA allocations, DMA32 required",
+		.use_dma_alloc = true,
+		.use_dma32 = true,
+		.pools_init_expected = true,
+	},
+	{
+		.description = "No DMA allocations, DMA32 required",
+		.use_dma_alloc = false,
+		.use_dma32 = true,
+		.pools_init_expected = false,
+	},
+	{
+		.description = "DMA allocations, no DMA32 required",
+		.use_dma_alloc = true,
+		.use_dma32 = false,
+		.pools_init_expected = true,
+	},
+};
+
+static void ttm_device_case_desc(const struct ttm_device_test_case *t, char *desc)
+{
+	strscpy(desc, t->description, KUNIT_PARAM_DESC_SIZE);
+}
+
+KUNIT_ARRAY_PARAM(ttm_device, ttm_device_cases, ttm_device_case_desc);
+
+static void ttm_device_init_pools(struct kunit *test)
+{
+	struct ttm_test_devices *priv = test->priv;
+	const struct ttm_device_test_case *params = test->param_value;
+	struct ttm_device *ttm_dev;
+	struct ttm_pool *pool;
+	struct ttm_pool_type pt;
+	int err;
+
+	ttm_dev = kunit_kzalloc(test, sizeof(*ttm_dev), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, ttm_dev);
+
+	err = ttm_device_kunit_init(priv, ttm_dev,
+				    params->use_dma_alloc,
+				    params->use_dma32);
+	KUNIT_ASSERT_EQ(test, err, 0);
+
+	pool = &ttm_dev->pool;
+	KUNIT_ASSERT_NOT_NULL(test, pool);
+	KUNIT_EXPECT_PTR_EQ(test, pool->dev, priv->dev);
+	KUNIT_EXPECT_EQ(test, pool->use_dma_alloc, params->use_dma_alloc);
+	KUNIT_EXPECT_EQ(test, pool->use_dma32, params->use_dma32);
+
+	if (params->pools_init_expected) {
+		for (int i = 0; i < TTM_NUM_CACHING_TYPES; ++i) {
+			for (int j = 0; j <= MAX_ORDER; ++j) {
+				pt = pool->caching[i].orders[j];
+				KUNIT_EXPECT_PTR_EQ(test, pt.pool, pool);
+				KUNIT_EXPECT_EQ(test, pt.caching, i);
+				KUNIT_EXPECT_EQ(test, pt.order, j);
+
+				if (params->use_dma_alloc)
+					KUNIT_ASSERT_FALSE(test,
+							   list_empty(&pt.pages));
+			}
+		}
+	}
+
+	ttm_device_fini(ttm_dev);
+}
+
 static struct kunit_case ttm_device_test_cases[] = {
 	KUNIT_CASE(ttm_device_init_basic),
+	KUNIT_CASE(ttm_device_init_multiple),
+	KUNIT_CASE(ttm_device_fini_basic),
+	KUNIT_CASE(ttm_device_init_no_vma_man),
+	KUNIT_CASE_PARAM(ttm_device_init_pools, ttm_device_gen_params),
 	{}
 };
 
Signature · 1 line
2.25.1
#3 lore raw

RFC v5 3/3 drm/ttm/tests: Add tests for ttm_pool

In reply to Karolina Stolarek

Add KUnit tests that exercise page allocation using page pools and freeing pages, either by returning them to the pool or freeing them. Add a basic test for ttm_pool cleanup. Introduce helpers to create a dummy ttm_buffer_object.

---
 drivers/gpu/drm/ttm/tests/Makefile            |   1 +
 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c |  17 +
 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h |   4 +
 drivers/gpu/drm/ttm/tests/ttm_pool_test.c     | 441 ++++++++++++++++++
 4 files changed, 463 insertions(+)
 create mode 100644 drivers/gpu/drm/ttm/tests/ttm_pool_test.c
drivers/gpu/drm/ttm/tests/Makefile +1 -0
index 7917805f37af..ec87c4fc1ad5 100644
@@ -2,4 +2,5 @@
 
 obj-$(CONFIG_DRM_TTM_KUNIT_TEST) += \
         ttm_device_test.o \
+        ttm_pool_test.o \
         ttm_kunit_helpers.o
drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c +17 -0
index 5f1499761f16..236d7da65627 100644
@@ -25,6 +25,23 @@ int ttm_device_kunit_init(struct ttm_test_devices *priv,
 }
 EXPORT_SYMBOL_GPL(ttm_device_kunit_init);
 
+struct ttm_buffer_object *ttm_bo_kunit_init(struct kunit *test,
+					    struct ttm_test_devices *devs,
+					    size_t size)
+{
+	struct drm_gem_object gem_obj = { .size = size };
+	struct ttm_buffer_object *bo;
+
+	bo = kunit_kzalloc(test, sizeof(*bo), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, bo);
+
+	bo->base = gem_obj;
+	bo->bdev = devs->ttm_dev;
+
+	return bo;
+}
+EXPORT_SYMBOL_GPL(ttm_bo_kunit_init);
+
 struct ttm_test_devices *ttm_test_devices_basic(struct kunit *test)
 {
 	struct ttm_test_devices *devs;
drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h +4 -0
index f9f5bc03e93a..e261e3660d0b 100644
@@ -7,6 +7,7 @@
 
 #include <drm/drm_drv.h>
 #include <drm/ttm/ttm_device.h>
+#include <drm/ttm/ttm_bo.h>
 
 #include <drm/drm_kunit_helpers.h>
 #include <kunit/test.h>
@@ -24,6 +25,9 @@ int ttm_device_kunit_init(struct ttm_test_devices *priv,
 			  struct ttm_device *ttm,
 			  bool use_dma_alloc,
 			  bool use_dma32);
+struct ttm_buffer_object *ttm_bo_kunit_init(struct kunit *test,
+					    struct ttm_test_devices *devs,
+					    size_t size);
 
 struct ttm_test_devices *ttm_test_devices_basic(struct kunit *test);
 struct ttm_test_devices *ttm_test_devices_all(struct kunit *test);
drivers/gpu/drm/ttm/tests/ttm_pool_test.c new file +441 -0

441 lines not on this page · raw message

Signature · 1 line
2.25.1
#4 lore raw
In reply to Karolina Stolarek

Feel free to add Reviewed-by: Christian König <[email protected]> to the whole series and push to drm-misc-next.

Thanks,
Christian.

Am 14.07.23 um 16:10 schrieb Karolina Stolarek:

Quoted 98 lines

This series introduces KUnit[1] tests for TTM (Translation Table Manager) subsystem, a memory manager used by graphics drivers to create and manage memory buffers across different memory domains, such as system memory or VRAM.

Unit tests implemented here cover two data structures:
- ttm_device -- referred as a buffer object device, which stores resource managers and page pools
- ttm_pool -- a struct of pools (ttm_pool_type) of different page orders and caching attributes, with pages that can be reused on the next buffer allocation

Use kunit_tool script to manually run the tests:

$ ./tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm/ttm/tests

To build a kernel with TTM KUnit tests, first enable CONFIG_KUNIT, and then CONFIG_DRM_TTM_KUNIT_TEST.

As for now, tests are architecture-agnostic (i.e. KUnit runner uses UML kernel), which means that we have limited coverage in some places. For example, we can't fully test the initialization of global page pools, such as global_write_combined. It is to be decided if we want to stick to UML or use CONFIG_X86 (at least to some extent).

These patches are just a beginning of the work to improve the test coverage of TTM. Feel free to suggest changes, test cases or priorities.

Many thanks,
Karolina

v5:
- Drop unnecessary brackets in 2/3
- Rebase KConfig file on the top of drm-tip

v4:
- Test helpers have been changed to make the creation of init/fini functions for each test suite easier:
+ Decouple device creation from test initialization by adding helpers that initialize ttm_test_devices, a struct which stores DRM/TTM devices, and can be used in test-specific init/finis (see ttm_pool_tests.c for an example)
+ Introduce generic init/fini functions for tests that only need devices
+ Add ttm_device field to ttm_test_devices (previously ttm_test_devices_priv)
- Make TTM buffer object outlive its TT (Christian)
- Add a dedicated struct for ttm_pool_test (struct ttm_pool_test_priv)
- Rename functions and structs:
+ struct ttm_test_devices_priv --> struct ttm_test_devices
+ ttm_kunit_helper_init_device() --> ttm_device_kunit_init()
+ ttm_kunit_helper_ttm_bo_init() --> ttm_bo_kunit_init()
- Split ttm_kunit_helper_init() into full config (with ttm_device init) and basic (init only with device/drm_device) initialization functions

v3:
- Rename ttm_kunit_helper_alloc_device() to ttm_kunit_helper_init_device() (Christian)
- Don't leak a full-blown drm_gem_object in ttm_kunit_helper_ttm_bo_init(). (Christian). Create a small mock object just to get ttm_tt_init_fields() to init the right number of pages
- As a follow up to the change above, delete ttm_kunit_helper_ttm_bo_fini() and just use ttm_bo_put()

v2:
- Add missing symbol exports in ttm_kunit_helpers.c
- Update helpers include to fix compilation issues (didn't catch it as KUnit tests weren't enabled in the kernel I tested, an oversight on my part)
- Add checks for ttm_pool fields in ttm_pool_alloc_basic(), including the one for NUMA node id
- Rebase the changes on the top of drm-tip

--------------------------------
[1] - https://www.kernel.org/doc/html/latest/dev-tools/kunit/index.html

Karolina Stolarek (3):
drm/ttm: Introduce KUnit test
drm/ttm/tests: Add tests for ttm_device
drm/ttm/tests: Add tests for ttm_pool

  drivers/gpu/drm/Kconfig                       |  15 +
  drivers/gpu/drm/ttm/Makefile                  |   1 +
  drivers/gpu/drm/ttm/tests/.kunitconfig        |   4 +
  drivers/gpu/drm/ttm/tests/Makefile            |   6 +
  drivers/gpu/drm/ttm/tests/ttm_device_test.c   | 212 +++++++++
  drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c | 115 +++++
  drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h |  41 ++
  drivers/gpu/drm/ttm/tests/ttm_pool_test.c     | 441 ++++++++++++++++++
  8 files changed, 835 insertions(+)

create mode 100644 drivers/gpu/drm/ttm/tests/.kunitconfig create mode 100644 drivers/gpu/drm/ttm/tests/Makefile create mode 100644 drivers/gpu/drm/ttm/tests/ttm_device_test.c create mode 100644 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c create mode 100644 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h create mode 100644 drivers/gpu/drm/ttm/tests/ttm_pool_test.c

#5 lore raw

Hi Christian,

Quoted 1 line · Christian König

Feel free to add Reviewed-by: Christian König <[email protected]> to the whole series and push to drm-misc-next.

Thanks for reviewing the patches while I was away.

I don't have commit rights to push it to drm-misc-next, so I'll go and find someone to help me out. Still, I was thinking if I should send v6 of the series. I fixed a couple of small issues while working on new tests, like UAF warnings from my kunit helpers when running kunit.py with --raw_output option, but I can include them as a separate patch in the new series. What's your preference?

All the best,
Karolina

Quoted 105 lines

Thanks,
Christian.

Am 14.07.23 um 16:10 schrieb Karolina Stolarek:

Quoted 99 lines

This series introduces KUnit[1] tests for TTM (Translation Table Manager) subsystem, a memory manager used by graphics drivers to create and manage memory buffers across different memory domains, such as system memory or VRAM.

Unit tests implemented here cover two data structures:
   - ttm_device -- referred as a buffer object device, which stores resource managers and page pools
   - ttm_pool -- a struct of pools (ttm_pool_type) of different page orders and caching attributes, with pages that can be reused on the next buffer allocation

Use kunit_tool script to manually run the tests:

$ ./tools/testing/kunit/kunit.py run 
--kunitconfig=drivers/gpu/drm/ttm/tests

To build a kernel with TTM KUnit tests, first enable CONFIG_KUNIT, and then CONFIG_DRM_TTM_KUNIT_TEST.

As for now, tests are architecture-agnostic (i.e. KUnit runner uses UML kernel), which means that we have limited coverage in some places. For example, we can't fully test the initialization of global page pools, such as global_write_combined. It is to be decided if we want to stick to UML or use CONFIG_X86 (at least to some extent).

These patches are just a beginning of the work to improve the test coverage of TTM. Feel free to suggest changes, test cases or priorities.

Many thanks,
Karolina

v5:
   - Drop unnecessary brackets in 2/3
   - Rebase KConfig file on the top of drm-tip

v4:
   - Test helpers have been changed to make the creation of init/fini functions for each test suite easier:
     + Decouple device creation from test initialization by adding helpers that initialize ttm_test_devices, a struct which stores DRM/TTM devices, and can be used in test-specific init/finis (see ttm_pool_tests.c for an example)
     + Introduce generic init/fini functions for tests that only need devices
     + Add ttm_device field to ttm_test_devices (previously ttm_test_devices_priv)
   - Make TTM buffer object outlive its TT (Christian)
   - Add a dedicated struct for ttm_pool_test (struct ttm_pool_test_priv)
   - Rename functions and structs:
     + struct ttm_test_devices_priv   --> struct ttm_test_devices
     + ttm_kunit_helper_init_device() --> ttm_device_kunit_init()
     + ttm_kunit_helper_ttm_bo_init() --> ttm_bo_kunit_init()
   - Split ttm_kunit_helper_init() into full config (with ttm_device init) and basic (init only with device/drm_device) initialization functions

v3:
   - Rename ttm_kunit_helper_alloc_device() to ttm_kunit_helper_init_device() (Christian)
   - Don't leak a full-blown drm_gem_object in ttm_kunit_helper_ttm_bo_init(). (Christian). Create a small mock object just to get ttm_tt_init_fields() to init the right number of pages
   - As a follow up to the change above, delete ttm_kunit_helper_ttm_bo_fini() and just use ttm_bo_put()

v2:
   - Add missing symbol exports in ttm_kunit_helpers.c
   - Update helpers include to fix compilation issues (didn't catch it as KUnit tests weren't enabled in the kernel I tested, an oversight on my part)
   - Add checks for ttm_pool fields in ttm_pool_alloc_basic(), including the one for NUMA node id
   - Rebase the changes on the top of drm-tip

--------------------------------
[1] - https://www.kernel.org/doc/html/latest/dev-tools/kunit/index.html

Karolina Stolarek (3):
   drm/ttm: Introduce KUnit test
   drm/ttm/tests: Add tests for ttm_device
   drm/ttm/tests: Add tests for ttm_pool

  drivers/gpu/drm/Kconfig                       |  15 + drivers/gpu/drm/ttm/Makefile                  |   1 + drivers/gpu/drm/ttm/tests/.kunitconfig        |   4 + drivers/gpu/drm/ttm/tests/Makefile            |   6 + drivers/gpu/drm/ttm/tests/ttm_device_test.c   | 212 +++++++++ drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c | 115 +++++ drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h |  41 ++ drivers/gpu/drm/ttm/tests/ttm_pool_test.c     | 441 ++++++++++++++++++

  8 files changed, 835 insertions(+)

  create mode 100644 drivers/gpu/drm/ttm/tests/.kunitconfig create mode 100644 drivers/gpu/drm/ttm/tests/Makefile create mode 100644 drivers/gpu/drm/ttm/tests/ttm_device_test.c create mode 100644 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c create mode 100644 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h create mode 100644 drivers/gpu/drm/ttm/tests/ttm_pool_test.c

#6 lore raw

Am 07.08.23 um 14:21 schrieb Karolina Stolarek:

Quoted 8 lines

Hi Christian,

Quoted 1 line · Christian König

Feel free to add Reviewed-by: Christian König <[email protected]> to the whole series and push to drm-misc-next.

Thanks for reviewing the patches while I was away.

I don't have commit rights to push it to drm-misc-next, so I'll go and find someone to help me out. Still, I was thinking if I should send v6 of the series. I fixed a couple of small issues while working on new tests, like UAF warnings from my kunit helpers when running kunit.py with --raw_output option, but I can include them as a separate patch in the new series. What's your preference?

Please send out the series once more based on current drm-misc-next and I can push it later today or tomorrow.

Regards,
Christian.

Quoted 110 lines

All the best,
Karolina

Quoted 106 lines

Thanks,
Christian.

Am 14.07.23 um 16:10 schrieb Karolina Stolarek:

Quoted 100 lines

This series introduces KUnit[1] tests for TTM (Translation Table Manager) subsystem, a memory manager used by graphics drivers to create and manage memory buffers across different memory domains, such as system memory or VRAM.

Unit tests implemented here cover two data structures:
   - ttm_device -- referred as a buffer object device, which stores resource managers and page pools
   - ttm_pool -- a struct of pools (ttm_pool_type) of different page orders and caching attributes, with pages that can be reused on the next buffer allocation

Use kunit_tool script to manually run the tests:

$ ./tools/testing/kunit/kunit.py run 
--kunitconfig=drivers/gpu/drm/ttm/tests

To build a kernel with TTM KUnit tests, first enable CONFIG_KUNIT, and then CONFIG_DRM_TTM_KUNIT_TEST.

As for now, tests are architecture-agnostic (i.e. KUnit runner uses UML kernel), which means that we have limited coverage in some places. For example, we can't fully test the initialization of global page pools, such as global_write_combined. It is to be decided if we want to stick to UML or use CONFIG_X86 (at least to some extent).

These patches are just a beginning of the work to improve the test coverage of TTM. Feel free to suggest changes, test cases or priorities.

Many thanks,
Karolina

v5:
   - Drop unnecessary brackets in 2/3
   - Rebase KConfig file on the top of drm-tip

v4:
   - Test helpers have been changed to make the creation of init/fini functions for each test suite easier:
     + Decouple device creation from test initialization by adding helpers that initialize ttm_test_devices, a struct which stores DRM/TTM devices, and can be used in test-specific init/finis (see ttm_pool_tests.c for an example)
     + Introduce generic init/fini functions for tests that only need devices
     + Add ttm_device field to ttm_test_devices (previously ttm_test_devices_priv)
   - Make TTM buffer object outlive its TT (Christian)
   - Add a dedicated struct for ttm_pool_test (struct ttm_pool_test_priv)
   - Rename functions and structs:
     + struct ttm_test_devices_priv   --> struct ttm_test_devices
     + ttm_kunit_helper_init_device() --> ttm_device_kunit_init()
     + ttm_kunit_helper_ttm_bo_init() --> ttm_bo_kunit_init()
   - Split ttm_kunit_helper_init() into full config (with ttm_device init) and basic (init only with device/drm_device) initialization functions

v3:
   - Rename ttm_kunit_helper_alloc_device() to ttm_kunit_helper_init_device() (Christian)
   - Don't leak a full-blown drm_gem_object in ttm_kunit_helper_ttm_bo_init(). (Christian). Create a small mock object just to get ttm_tt_init_fields() to init the right number of pages
   - As a follow up to the change above, delete ttm_kunit_helper_ttm_bo_fini() and just use ttm_bo_put()

v2:
   - Add missing symbol exports in ttm_kunit_helpers.c
   - Update helpers include to fix compilation issues (didn't catch it as KUnit tests weren't enabled in the kernel I tested, an oversight on my part)
   - Add checks for ttm_pool fields in ttm_pool_alloc_basic(), including the one for NUMA node id
   - Rebase the changes on the top of drm-tip

--------------------------------
[1] - https://www.kernel.org/doc/html/latest/dev-tools/kunit/index.html

Karolina Stolarek (3):
   drm/ttm: Introduce KUnit test
   drm/ttm/tests: Add tests for ttm_device
   drm/ttm/tests: Add tests for ttm_pool

  drivers/gpu/drm/Kconfig                       |  15 + drivers/gpu/drm/ttm/Makefile                  |   1 + drivers/gpu/drm/ttm/tests/.kunitconfig        |   4 + drivers/gpu/drm/ttm/tests/Makefile            |   6 + drivers/gpu/drm/ttm/tests/ttm_device_test.c   | 212 +++++++++ drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c | 115 +++++ drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h |  41 ++ drivers/gpu/drm/ttm/tests/ttm_pool_test.c     | 441
++++++++++++++++++

  8 files changed, 835 insertions(+)

  create mode 100644 drivers/gpu/drm/ttm/tests/.kunitconfig create mode 100644 drivers/gpu/drm/ttm/tests/Makefile create mode 100644 drivers/gpu/drm/ttm/tests/ttm_device_test.c create mode 100644 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c create mode 100644 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h create mode 100644 drivers/gpu/drm/ttm/tests/ttm_pool_test.c

#7 lore raw

Hi Christian,

Quoted 11 lines · Christian König

Am 07.08.23 um 14:21 schrieb Karolina Stolarek:

Quoted 8 lines

Hi Christian,

Quoted 1 line · Christian König

Feel free to add Reviewed-by: Christian König <[email protected]> to the whole series and push to drm-misc-next.

Thanks for reviewing the patches while I was away.

I don't have commit rights to push it to drm-misc-next, so I'll go and find someone to help me out. Still, I was thinking if I should send v6 of the series. I fixed a couple of small issues while working on new tests, like UAF warnings from my kunit helpers when running kunit.py with --raw_output option, but I can include them as a separate patch in the new series. What's your preference?

Please send out the series once more based on current drm-misc-next and I can push it later today or tomorrow.

I'll send out the series in a minute or so. I decided to _not_ add your r-b to the patches I modified, so you can take the final look and make sure everything is fine. There were no conflicts during the rebase, you can always take v5 if you fancy so.

Thank you for your help!

All the best,
Karolina

Quoted 115 lines

Regards,
Christian.

Quoted 110 lines

All the best,
Karolina

Quoted 106 lines

Thanks,
Christian.

Am 14.07.23 um 16:10 schrieb Karolina Stolarek:

Quoted 100 lines

This series introduces KUnit[1] tests for TTM (Translation Table Manager) subsystem, a memory manager used by graphics drivers to create and manage memory buffers across different memory domains, such as system memory or VRAM.

Unit tests implemented here cover two data structures:
   - ttm_device -- referred as a buffer object device, which stores resource managers and page pools
   - ttm_pool -- a struct of pools (ttm_pool_type) of different page orders and caching attributes, with pages that can be reused on the next buffer allocation

Use kunit_tool script to manually run the tests:

$ ./tools/testing/kunit/kunit.py run 
--kunitconfig=drivers/gpu/drm/ttm/tests

To build a kernel with TTM KUnit tests, first enable CONFIG_KUNIT, and then CONFIG_DRM_TTM_KUNIT_TEST.

As for now, tests are architecture-agnostic (i.e. KUnit runner uses UML kernel), which means that we have limited coverage in some places. For example, we can't fully test the initialization of global page pools, such as global_write_combined. It is to be decided if we want to stick to UML or use CONFIG_X86 (at least to some extent).

These patches are just a beginning of the work to improve the test coverage of TTM. Feel free to suggest changes, test cases or priorities.

Many thanks,
Karolina

v5:
   - Drop unnecessary brackets in 2/3
   - Rebase KConfig file on the top of drm-tip

v4:
   - Test helpers have been changed to make the creation of init/fini functions for each test suite easier:
     + Decouple device creation from test initialization by adding helpers that initialize ttm_test_devices, a struct which stores DRM/TTM devices, and can be used in test-specific init/finis (see ttm_pool_tests.c for an example)
     + Introduce generic init/fini functions for tests that only need devices
     + Add ttm_device field to ttm_test_devices (previously ttm_test_devices_priv)
   - Make TTM buffer object outlive its TT (Christian)
   - Add a dedicated struct for ttm_pool_test (struct ttm_pool_test_priv)
   - Rename functions and structs:
     + struct ttm_test_devices_priv   --> struct ttm_test_devices
     + ttm_kunit_helper_init_device() --> ttm_device_kunit_init()
     + ttm_kunit_helper_ttm_bo_init() --> ttm_bo_kunit_init()
   - Split ttm_kunit_helper_init() into full config (with ttm_device init) and basic (init only with device/drm_device) initialization functions

v3:
   - Rename ttm_kunit_helper_alloc_device() to ttm_kunit_helper_init_device() (Christian)
   - Don't leak a full-blown drm_gem_object in ttm_kunit_helper_ttm_bo_init(). (Christian). Create a small mock object just to get ttm_tt_init_fields() to init the right number of pages
   - As a follow up to the change above, delete ttm_kunit_helper_ttm_bo_fini() and just use ttm_bo_put()

v2:
   - Add missing symbol exports in ttm_kunit_helpers.c
   - Update helpers include to fix compilation issues (didn't catch it as KUnit tests weren't enabled in the kernel I tested, an oversight on my part)
   - Add checks for ttm_pool fields in ttm_pool_alloc_basic(), including the one for NUMA node id
   - Rebase the changes on the top of drm-tip

--------------------------------
[1] - https://www.kernel.org/doc/html/latest/dev-tools/kunit/index.html

Karolina Stolarek (3):
   drm/ttm: Introduce KUnit test
   drm/ttm/tests: Add tests for ttm_device
   drm/ttm/tests: Add tests for ttm_pool

  drivers/gpu/drm/Kconfig                       |  15 + drivers/gpu/drm/ttm/Makefile                  |   1 + drivers/gpu/drm/ttm/tests/.kunitconfig        |   4 + drivers/gpu/drm/ttm/tests/Makefile            |   6 + drivers/gpu/drm/ttm/tests/ttm_device_test.c   | 212 +++++++++ drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c | 115 +++++ drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h |  41 ++ drivers/gpu/drm/ttm/tests/ttm_pool_test.c     | 441
++++++++++++++++++

  8 files changed, 835 insertions(+)

  create mode 100644 drivers/gpu/drm/ttm/tests/.kunitconfig create mode 100644 drivers/gpu/drm/ttm/tests/Makefile create mode 100644 drivers/gpu/drm/ttm/tests/ttm_device_test.c create mode 100644 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c create mode 100644 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h create mode 100644 drivers/gpu/drm/ttm/tests/ttm_pool_test.c