Skip to contentSkip to conversation

Improve test coverage of TTM

PATCH Superseded Karolina Stolarek 2 participants 10 messages Sep 12, 2023 – Sep 19, 2023

Subsystems (3)

v3 of 14 Superseded Latest is v14 →

lore
Share & tools
lore
#0 lore raw

v3 cover Improve test coverage of TTM

Add tests for building blocks of the TTM subsystem, such as ttm_resource, ttm_resource_manager, ttm_tt and ttm_buffer_object. This series covers basic functions such as initialization, allocation and clean-up of each struct. Testing of ttm_buffer_object also includes locking and unlocking the object for validation, with special scenarios such as an interrupted wait or deadlock.

Some of the test cases check the bulk move mechanism and how it interacts with pinned buffers. This is to be seen if we want to add dedicated testing for bulk move or not. The resource allocation subtests use ttm_sys_manager for now. Resources that don't use system memory will be indirectly tested via tests for ttm_bo_validate()/ttm_bo_init_validate(), using a mock resource manager.

Use kunit_tool script to manually run all 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.

Many thanks,
Karolina

v3:
- Instead of modifying the main TTM Makefile, use EXPORT_SYMBOL_FOR_TESTS_ONLY() macro for symbols that are tested but not widely exported. Thanks to this change, TTM tests can be built as modules, even when non-exported functions are used
- Change the description of a patch that fixes ttm_pool_pre_populated()

v2:
- Remove Makefile for KUnit tests and move the definitions to the TTM's one
- Switch on CONFIG_DRM_TTM_KUNIT_TEST=m so the tests and TTM module are built as one. This allows building the tests as a module, even if it uses functions that are not exported
- Fix ttm_pool_pre_populated(); a wrong flag was passed to ttm_tt_kunit_init() function

Karolina Stolarek (4):
drm/ttm/tests: Add tests for ttm_resource and ttm_sys_man drm/ttm/tests: Add tests for ttm_tt drm/ttm/tests: Add tests for ttm_bo functions drm/ttm/tests: Fix argument in ttm_tt_kunit_init()

 drivers/gpu/drm/ttm/tests/Makefile            |   3 +
 drivers/gpu/drm/ttm/tests/ttm_bo_test.c       | 620 ++++++++++++++++++
 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c |  48 ++
 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h |   4 +
 drivers/gpu/drm/ttm/tests/ttm_pool_test.c     |   3 +-
 drivers/gpu/drm/ttm/tests/ttm_resource_test.c | 335 ++++++++++
 drivers/gpu/drm/ttm/tests/ttm_tt_test.c       | 277 ++++++++
 drivers/gpu/drm/ttm/ttm_resource.c            |   3 +
 drivers/gpu/drm/ttm/ttm_tt.c                  |   3 +
 9 files changed, 1294 insertions(+), 2 deletions(-)
 create mode 100644 drivers/gpu/drm/ttm/tests/ttm_bo_test.c
 create mode 100644 drivers/gpu/drm/ttm/tests/ttm_resource_test.c
 create mode 100644 drivers/gpu/drm/ttm/tests/ttm_tt_test.c
Signature · 1 line
2.25.1
#1 lore raw

PATCH v3 1/4 drm/ttm/tests: Add tests for ttm_resource and ttm_sys_man

Test initialization of ttm_resource using different memory domains. Add tests for a system memory manager and functions that can be tested without a fully-featured resource manager. Update ttm_bo_kunit_init() to initialize BO's kref and reservation object. Export ttm_resource_alloc symbol for test purposes only.

---
 drivers/gpu/drm/ttm/tests/Makefile            |   1 +
 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c |  23 ++
 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h |   4 +
 drivers/gpu/drm/ttm/tests/ttm_resource_test.c | 335 ++++++++++++++++++
 drivers/gpu/drm/ttm/ttm_resource.c            |   3 +
 5 files changed, 366 insertions(+)
 create mode 100644 drivers/gpu/drm/ttm/tests/ttm_resource_test.c
drivers/gpu/drm/ttm/tests/Makefile +1 -0
index ec87c4fc1ad5..c92fe2052ef6 100644
@@ -3,4 +3,5 @@
 obj-$(CONFIG_DRM_TTM_KUNIT_TEST) += \
         ttm_device_test.o \
         ttm_pool_test.o \
+        ttm_resource_test.o \
         ttm_kunit_helpers.o
drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c +23 -0
index 81661d8827aa..eccc59b981f8 100644
@@ -38,10 +38,33 @@ struct ttm_buffer_object *ttm_bo_kunit_init(struct kunit *test,
 	bo->base = gem_obj;
 	bo->bdev = devs->ttm_dev;
 
+	kref_init(&bo->kref);
+
+	dma_resv_init(&bo->base._resv);
+	bo->base.resv = &bo->base._resv;
+
 	return bo;
 }
 EXPORT_SYMBOL_GPL(ttm_bo_kunit_init);
 
+struct ttm_place *ttm_place_kunit_init(struct kunit *test,
+				       uint32_t mem_type, uint32_t flags,
+				       size_t size)
+{
+	struct ttm_place *place;
+
+	place = kunit_kzalloc(test, sizeof(*place), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, place);
+
+	place->mem_type = mem_type;
+	place->flags = flags;
+	place->fpfn = size >> PAGE_SHIFT;
+	place->lpfn = place->fpfn + (size >> PAGE_SHIFT);
+
+	return place;
+}
+EXPORT_SYMBOL_GPL(ttm_place_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 e261e3660d0b..f38140f93c05 100644
@@ -8,6 +8,7 @@
 #include <drm/drm_drv.h>
 #include <drm/ttm/ttm_device.h>
 #include <drm/ttm/ttm_bo.h>
+#include <drm/ttm/ttm_placement.h>
 
 #include <drm/drm_kunit_helpers.h>
 #include <kunit/test.h>
@@ -28,6 +29,9 @@ int ttm_device_kunit_init(struct ttm_test_devices *priv,
 struct ttm_buffer_object *ttm_bo_kunit_init(struct kunit *test,
 					    struct ttm_test_devices *devs,
 					    size_t size);
+struct ttm_place *ttm_place_kunit_init(struct kunit *test,
+				       uint32_t mem_type, uint32_t flags,
+				       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_resource_test.c new file +335 -0

335 lines not on this page · raw message

drivers/gpu/drm/ttm/ttm_resource.c +3 -0
index 46ff9c75bb12..02b96d23fdb9 100644
@@ -30,6 +30,8 @@
 #include <drm/ttm/ttm_placement.h>
 #include <drm/ttm/ttm_resource.h>
 
+#include <drm/drm_util.h>
+
 /**
  * ttm_lru_bulk_move_init - initialize a bulk move structure
  * @bulk: the structure to init
@@ -240,6 +242,7 @@ int ttm_resource_alloc(struct ttm_buffer_object *bo,
 	spin_unlock(&bo->bdev->lru_lock);
 	return 0;
 }
+EXPORT_SYMBOL_FOR_TESTS_ONLY(ttm_resource_alloc);
 
 void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res)
 {
Signature · 1 line
2.25.1
#7 lore raw

PATCH v3 2/4 drm/ttm/tests: Add tests for ttm_tt

In reply to Karolina Stolarek

Test initialization, creation and destruction of ttm_tt instances. Export ttm_tt_destroy and ttm_tt_create symbols for test purposes.

---
 drivers/gpu/drm/ttm/tests/Makefile            |   1 +
 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c |  20 ++
 drivers/gpu/drm/ttm/tests/ttm_tt_test.c       | 277 ++++++++++++++++++
 drivers/gpu/drm/ttm/ttm_tt.c                  |   3 +
 4 files changed, 301 insertions(+)
 create mode 100644 drivers/gpu/drm/ttm/tests/ttm_tt_test.c
drivers/gpu/drm/ttm/tests/Makefile +1 -0
index c92fe2052ef6..f570530bbb60 100644
@@ -4,4 +4,5 @@ obj-$(CONFIG_DRM_TTM_KUNIT_TEST) += \
         ttm_device_test.o \
         ttm_pool_test.o \
         ttm_resource_test.o \
+        ttm_tt_test.o \
         ttm_kunit_helpers.o
drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c +20 -0
index eccc59b981f8..112381153bbf 100644
@@ -2,9 +2,29 @@
 /*
  * Copyright © 2023 Intel Corporation
  */
+#include <drm/ttm/ttm_tt.h>
+
 #include "ttm_kunit_helpers.h"
 
+static struct ttm_tt *ttm_tt_simple_create(struct ttm_buffer_object *bo,
+					   uint32_t page_flags)
+{
+	struct ttm_tt *tt;
+
+	tt = kzalloc(sizeof(*tt), GFP_KERNEL);
+	ttm_tt_init(tt, bo, 0, ttm_cached, 0);
+
+	return tt;
+}
+
+static void ttm_tt_simple_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
+{
+	kfree(ttm);
+}
+
 struct ttm_device_funcs ttm_dev_funcs = {
+	.ttm_tt_create = ttm_tt_simple_create,
+	.ttm_tt_destroy = ttm_tt_simple_destroy,
 };
 EXPORT_SYMBOL_GPL(ttm_dev_funcs);
 
drivers/gpu/drm/ttm/tests/ttm_tt_test.c new file +277 -0

277 lines not on this page · raw message

drivers/gpu/drm/ttm/ttm_tt.c +3 -0
index e0a77671edd6..d978dc539a9b 100644
@@ -36,6 +36,7 @@
 #include <linux/file.h>
 #include <linux/module.h>
 #include <drm/drm_cache.h>
+#include <drm/drm_util.h>
 #include <drm/ttm/ttm_bo.h>
 #include <drm/ttm/ttm_tt.h>
 
@@ -91,6 +92,7 @@ int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc)
 
 	return 0;
 }
+EXPORT_SYMBOL_FOR_TESTS_ONLY(ttm_tt_create);
 
 /*
  * Allocates storage for pointers to the pages that back the ttm.
@@ -129,6 +131,7 @@ void ttm_tt_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
 {
 	bdev->funcs->ttm_tt_destroy(bdev, ttm);
 }
+EXPORT_SYMBOL_FOR_TESTS_ONLY(ttm_tt_destroy);
 
 static void ttm_tt_init_fields(struct ttm_tt *ttm,
 			       struct ttm_buffer_object *bo,
Signature · 1 line
2.25.1
#8 lore raw

PATCH v3 3/4 drm/ttm/tests: Add tests for ttm_bo functions

In reply to Karolina Stolarek

Test reservation and release of TTM buffer objects. Add tests to check pin and unpin operations.

---
 drivers/gpu/drm/ttm/tests/Makefile            |   1 +
 drivers/gpu/drm/ttm/tests/ttm_bo_test.c       | 620 ++++++++++++++++++
 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c |   5 +
 3 files changed, 626 insertions(+)
 create mode 100644 drivers/gpu/drm/ttm/tests/ttm_bo_test.c
drivers/gpu/drm/ttm/tests/Makefile +1 -0
index f570530bbb60..468535f7eed2 100644
@@ -5,4 +5,5 @@ obj-$(CONFIG_DRM_TTM_KUNIT_TEST) += \
         ttm_pool_test.o \
         ttm_resource_test.o \
         ttm_tt_test.o \
+        ttm_bo_test.o \
         ttm_kunit_helpers.o
drivers/gpu/drm/ttm/tests/ttm_bo_test.c new file +620 -0

620 lines not on this page · raw message

drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c +5 -0
index 112381153bbf..dbe562c59677 100644
@@ -22,6 +22,10 @@ static void ttm_tt_simple_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
 	kfree(ttm);
 }
 
+static void dummy_ttm_bo_destroy(struct ttm_buffer_object *bo)
+{
+}
+
 struct ttm_device_funcs ttm_dev_funcs = {
 	.ttm_tt_create = ttm_tt_simple_create,
 	.ttm_tt_destroy = ttm_tt_simple_destroy,
@@ -57,6 +61,7 @@ struct ttm_buffer_object *ttm_bo_kunit_init(struct kunit *test,
 
 	bo->base = gem_obj;
 	bo->bdev = devs->ttm_dev;
+	bo->destroy = dummy_ttm_bo_destroy;
 
 	kref_init(&bo->kref);
 
Signature · 1 line
2.25.1
#9 lore raw

PATCH v3 4/4 drm/ttm/tests: Fix argument in ttm_tt_kunit_init()

In reply to Karolina Stolarek

Remove a leftover definition of page order and pass an empty flag value in ttm_pool_pre_populated().

---
 drivers/gpu/drm/ttm/tests/ttm_pool_test.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/gpu/drm/ttm/tests/ttm_pool_test.c +1 -2
index 2d9cae8cd984..b97f7b6daf5b 100644
@@ -78,10 +78,9 @@ static struct ttm_pool *ttm_pool_pre_populated(struct kunit *test,
 	struct ttm_test_devices *devs = priv->devs;
 	struct ttm_pool *pool;
 	struct ttm_tt *tt;
-	unsigned long order = __fls(size / PAGE_SIZE);
 	int err;
 
-	tt = ttm_tt_kunit_init(test, order, caching, size);
+	tt = ttm_tt_kunit_init(test, 0, caching, size);
 	KUNIT_ASSERT_NOT_NULL(test, tt);
 
 	pool = kunit_kzalloc(test, sizeof(*pool), GFP_KERNEL);
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH v3 1/4] drm/ttm/tests: Add tests for ttm_resource and ttm_sys_man

In reply to Karolina Stolarek

Am 12.09.23 um 13:49 schrieb Karolina Stolarek:

Quoted 31 lines

Test initialization of ttm_resource using different memory domains. Add tests for a system memory manager and functions that can be tested without a fully-featured resource manager. Update ttm_bo_kunit_init() to initialize BO's kref and reservation object. Export ttm_resource_alloc symbol for test purposes only.

---
  drivers/gpu/drm/ttm/tests/Makefile            |   1 +
  drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c |  23 ++
  drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h |   4 +
  drivers/gpu/drm/ttm/tests/ttm_resource_test.c | 335 ++++++++++++++++++
  drivers/gpu/drm/ttm/ttm_resource.c            |   3 +
  5 files changed, 366 insertions(+)

create mode 100644 drivers/gpu/drm/ttm/tests/ttm_resource_test.c

drivers/gpu/drm/ttm/tests/Makefile +1 -0
index ec87c4fc1ad5..c92fe2052ef6 100644
@@ -3,4 +3,5 @@
  obj-$(CONFIG_DRM_TTM_KUNIT_TEST) += \
          ttm_device_test.o \
          ttm_pool_test.o \
+        ttm_resource_test.o \
          ttm_kunit_helpers.o
drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c +0 -0
index 81661d8827aa..eccc59b981f8 100644

drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c struct ttm_buffer_object *ttm_bo_kunit_init(struct kunit *test, · line 45

Quoted 8 lines
drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c +5 -0
@@ -38,10 +38,33 @@ struct ttm_buffer_object *ttm_bo_kunit_init(struct kunit *test,
  	bo->base = gem_obj;
  	bo->bdev = devs->ttm_dev;
 
+	kref_init(&bo->kref);
+
+	dma_resv_init(&bo->base._resv);
+	bo->base.resv = &bo->base._resv;
+

I'm really wondering if we shouldn't now initialize the GEM object properly?

That would also initialize the reservation object if I remember correctly.

The solution with EXPORT_SYMBOL_FOR_TESTS_ONLY looks really nice I think and apart from that I can't see anything obviously wrong either, but I only skimmed over the code.

Regards,
Christian.

Quoted 409 lines
drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c +18 -0
  	return bo;
  }
  EXPORT_SYMBOL_GPL(ttm_bo_kunit_init);
 
+struct ttm_place *ttm_place_kunit_init(struct kunit *test,
+				       uint32_t mem_type, uint32_t flags,
+				       size_t size)
+{
+	struct ttm_place *place;
+
+	place = kunit_kzalloc(test, sizeof(*place), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, place);
+
+	place->mem_type = mem_type;
+	place->flags = flags;
+	place->fpfn = size >> PAGE_SHIFT;
+	place->lpfn = place->fpfn + (size >> PAGE_SHIFT);
+
+	return place;
+}
+EXPORT_SYMBOL_GPL(ttm_place_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 e261e3660d0b..f38140f93c05 100644
@@ -8,6 +8,7 @@
  #include <drm/drm_drv.h>
  #include <drm/ttm/ttm_device.h>
  #include <drm/ttm/ttm_bo.h>
+#include <drm/ttm/ttm_placement.h>
 
  #include <drm/drm_kunit_helpers.h>
  #include <kunit/test.h>
@@ -28,6 +29,9 @@ int ttm_device_kunit_init(struct ttm_test_devices *priv,
  struct ttm_buffer_object *ttm_bo_kunit_init(struct kunit *test,
  					    struct ttm_test_devices *devs,
  					    size_t size);
+struct ttm_place *ttm_place_kunit_init(struct kunit *test,
+				       uint32_t mem_type, uint32_t flags,
+				       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_resource_test.c new file +335 -0
new file mode 100644
index 000000000000..851cdc43dc37
@@ -0,0 +1,335 @@
+// SPDX-License-Identifier: GPL-2.0 AND MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+#include <drm/ttm/ttm_resource.h>
+
+#include "ttm_kunit_helpers.h"
+
+#define RES_SIZE		SZ_4K
+#define TTM_PRIV_DUMMY_REG	(TTM_NUM_MEM_TYPES - 1)
+
+struct ttm_resource_test_case {
+	const char *description;
+	uint32_t mem_type;
+	uint32_t flags;
+};
+
+struct ttm_resource_test_priv {
+	struct ttm_test_devices *devs;
+	struct ttm_buffer_object *bo;
+	struct ttm_place *place;
+};
+
+static const struct ttm_resource_manager_func ttm_resource_manager_mock_funcs = { };
+
+static int ttm_resource_test_init(struct kunit *test)
+{
+	struct ttm_resource_test_priv *priv;
+
+	priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, priv);
+
+	priv->devs = ttm_test_devices_all(test);
+	KUNIT_ASSERT_NOT_NULL(test, priv->devs);
+
+	test->priv = priv;
+
+	return 0;
+}
+
+static void ttm_resource_test_fini(struct kunit *test)
+{
+	struct ttm_resource_test_priv *priv = test->priv;
+
+	ttm_test_devices_put(test, priv->devs);
+}
+
+static void ttm_init_test_mocks(struct kunit *test,
+				struct ttm_resource_test_priv *priv,
+				uint32_t mem_type, uint32_t flags)
+{
+	size_t size = RES_SIZE;
+
+	/* Make sure we have what we need for a good BO mock */
+	KUNIT_ASSERT_NOT_NULL(test, priv->devs->ttm_dev);
+
+	priv->bo = ttm_bo_kunit_init(test, priv->devs, size);
+	priv->place = ttm_place_kunit_init(test, mem_type, flags, size);
+}
+
+static void ttm_init_test_manager(struct kunit *test,
+				  struct ttm_resource_test_priv *priv,
+				  uint32_t mem_type)
+{
+	struct ttm_device *ttm_dev = priv->devs->ttm_dev;
+	struct ttm_resource_manager *man;
+	size_t size = SZ_16K;
+
+	man = kunit_kzalloc(test, sizeof(*man), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, man);
+
+	man->use_tt = false;
+	man->func = &ttm_resource_manager_mock_funcs;
+
+	ttm_resource_manager_init(man, ttm_dev, size);
+	ttm_set_driver_manager(ttm_dev, mem_type, man);
+	ttm_resource_manager_set_used(man, true);
+}
+
+static const struct ttm_resource_test_case ttm_resource_cases[] = {
+	{
+		.description = "Init resource in TTM_PL_SYSTEM",
+		.mem_type = TTM_PL_SYSTEM,
+	},
+	{
+		.description = "Init resource in TTM_PL_VRAM",
+		.mem_type = TTM_PL_VRAM,
+	},
+	{
+		.description = "Init resource in a private placement",
+		.mem_type = TTM_PRIV_DUMMY_REG,
+	},
+	{
+		.description = "Init resource in TTM_PL_SYSTEM, set placement flags",
+		.mem_type = TTM_PL_SYSTEM,
+		.flags = TTM_PL_FLAG_TOPDOWN,
+	},
+};
+
+static void ttm_resource_case_desc(const struct ttm_resource_test_case *t, char *desc)
+{
+	strscpy(desc, t->description, KUNIT_PARAM_DESC_SIZE);
+}
+
+KUNIT_ARRAY_PARAM(ttm_resource, ttm_resource_cases, ttm_resource_case_desc);
+
+static void ttm_resource_init_basic(struct kunit *test)
+{
+	const struct ttm_resource_test_case *params = test->param_value;
+	struct ttm_resource_test_priv *priv = test->priv;
+	struct ttm_resource *res;
+	struct ttm_buffer_object *bo;
+	struct ttm_place *place;
+	struct ttm_resource_manager *man;
+	uint64_t expected_usage;
+
+	ttm_init_test_mocks(test, priv, params->mem_type, params->flags);
+	bo = priv->bo;
+	place = priv->place;
+
+	if (params->mem_type > TTM_PL_SYSTEM)
+		ttm_init_test_manager(test, priv, params->mem_type);
+
+	res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, res);
+
+	man = ttm_manager_type(priv->devs->ttm_dev, place->mem_type);
+	expected_usage = man->usage + RES_SIZE;
+
+	KUNIT_ASSERT_TRUE(test, list_empty(&man->lru[bo->priority]));
+
+	ttm_resource_init(bo, place, res);
+
+	KUNIT_ASSERT_EQ(test, res->start, 0);
+	KUNIT_ASSERT_EQ(test, res->size, RES_SIZE);
+	KUNIT_ASSERT_EQ(test, res->mem_type, place->mem_type);
+	KUNIT_ASSERT_EQ(test, res->placement, place->flags);
+	KUNIT_ASSERT_PTR_EQ(test, res->bo, bo);
+
+	KUNIT_ASSERT_NULL(test, res->bus.addr);
+	KUNIT_ASSERT_EQ(test, res->bus.offset, 0);
+	KUNIT_ASSERT_FALSE(test, res->bus.is_iomem);
+	KUNIT_ASSERT_EQ(test, res->bus.caching, ttm_cached);
+	KUNIT_ASSERT_EQ(test, man->usage, expected_usage);
+
+	KUNIT_ASSERT_TRUE(test, list_is_singular(&man->lru[bo->priority]));
+
+	ttm_resource_fini(man, res);
+}
+
+static void ttm_resource_init_pinned(struct kunit *test)
+{
+	struct ttm_resource_test_priv *priv = test->priv;
+	struct ttm_resource *res;
+	struct ttm_buffer_object *bo;
+	struct ttm_place *place;
+	struct ttm_resource_manager *man;
+
+	ttm_init_test_mocks(test, priv, TTM_PL_SYSTEM, 0);
+	bo = priv->bo;
+	place = priv->place;
+
+	man = ttm_manager_type(priv->devs->ttm_dev, place->mem_type);
+
+	res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, res);
+	KUNIT_ASSERT_TRUE(test, list_empty(&bo->bdev->pinned));
+
+	dma_resv_lock(bo->base.resv, NULL);
+	ttm_bo_pin(bo);
+	ttm_resource_init(bo, place, res);
+	KUNIT_ASSERT_TRUE(test, list_is_singular(&bo->bdev->pinned));
+
+	ttm_bo_unpin(bo);
+	ttm_resource_fini(man, res);
+	dma_resv_unlock(bo->base.resv);
+
+	KUNIT_ASSERT_TRUE(test, list_empty(&bo->bdev->pinned));
+}
+
+static void ttm_resource_fini_basic(struct kunit *test)
+{
+	struct ttm_resource_test_priv *priv = test->priv;
+	struct ttm_resource *res;
+	struct ttm_buffer_object *bo;
+	struct ttm_place *place;
+	struct ttm_resource_manager *man;
+
+	ttm_init_test_mocks(test, priv, TTM_PL_SYSTEM, 0);
+	bo = priv->bo;
+	place = priv->place;
+
+	man = ttm_manager_type(priv->devs->ttm_dev, place->mem_type);
+
+	res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, res);
+
+	ttm_resource_init(bo, place, res);
+	ttm_resource_fini(man, res);
+
+	KUNIT_ASSERT_TRUE(test, list_empty(&res->lru));
+	KUNIT_ASSERT_EQ(test, man->usage, 0);
+}
+
+static void ttm_resource_manager_init_basic(struct kunit *test)
+{
+	struct ttm_resource_test_priv *priv = test->priv;
+	struct ttm_resource_manager *man;
+	size_t size = SZ_16K;
+
+	man = kunit_kzalloc(test, sizeof(*man), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, man);
+
+	ttm_resource_manager_init(man, priv->devs->ttm_dev, size);
+
+	KUNIT_ASSERT_PTR_EQ(test, man->bdev, priv->devs->ttm_dev);
+	KUNIT_ASSERT_EQ(test, man->size, size);
+	KUNIT_ASSERT_EQ(test, man->usage, 0);
+	KUNIT_ASSERT_NULL(test, man->move);
+	KUNIT_ASSERT_NOT_NULL(test, &man->move_lock);
+
+	for (int i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
+		KUNIT_ASSERT_TRUE(test, list_empty(&man->lru[i]));
+}
+
+static void ttm_resource_manager_usage_basic(struct kunit *test)
+{
+	struct ttm_resource_test_priv *priv = test->priv;
+	struct ttm_resource *res;
+	struct ttm_buffer_object *bo;
+	struct ttm_place *place;
+	struct ttm_resource_manager *man;
+	uint64_t actual_usage;
+
+	ttm_init_test_mocks(test, priv, TTM_PL_SYSTEM, TTM_PL_FLAG_TOPDOWN);
+	bo = priv->bo;
+	place = priv->place;
+
+	res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, res);
+
+	man = ttm_manager_type(priv->devs->ttm_dev, place->mem_type);
+
+	ttm_resource_init(bo, place, res);
+	actual_usage = ttm_resource_manager_usage(man);
+
+	KUNIT_ASSERT_EQ(test, actual_usage, RES_SIZE);
+
+	ttm_resource_fini(man, res);
+}
+
+static void ttm_resource_manager_set_used_basic(struct kunit *test)
+{
+	struct ttm_resource_test_priv *priv = test->priv;
+	struct ttm_resource_manager *man;
+
+	man = ttm_manager_type(priv->devs->ttm_dev, TTM_PL_SYSTEM);
+	KUNIT_ASSERT_TRUE(test, man->use_type);
+
+	ttm_resource_manager_set_used(man, false);
+	KUNIT_ASSERT_FALSE(test, man->use_type);
+}
+
+static void ttm_sys_man_alloc_basic(struct kunit *test)
+{
+	struct ttm_resource_test_priv *priv = test->priv;
+	struct ttm_resource_manager *man;
+	struct ttm_buffer_object *bo;
+	struct ttm_place *place;
+	struct ttm_resource *res;
+	uint32_t mem_type = TTM_PL_SYSTEM;
+	int ret;
+
+	ttm_init_test_mocks(test, priv, mem_type, 0);
+	bo = priv->bo;
+	place = priv->place;
+
+	man = ttm_manager_type(priv->devs->ttm_dev, mem_type);
+	ret = man->func->alloc(man, bo, place, &res);
+
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	KUNIT_ASSERT_EQ(test, res->size, RES_SIZE);
+	KUNIT_ASSERT_EQ(test, res->mem_type, mem_type);
+	KUNIT_ASSERT_PTR_EQ(test, res->bo, bo);
+
+	ttm_resource_fini(man, res);
+}
+
+static void ttm_sys_man_free_basic(struct kunit *test)
+{
+	struct ttm_resource_test_priv *priv = test->priv;
+	struct ttm_resource_manager *man;
+	struct ttm_buffer_object *bo;
+	struct ttm_place *place;
+	struct ttm_resource *res;
+	uint32_t mem_type = TTM_PL_SYSTEM;
+
+	ttm_init_test_mocks(test, priv, mem_type, 0);
+	bo = priv->bo;
+	place = priv->place;
+
+	res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, res);
+
+	ttm_resource_alloc(bo, place, &res);
+
+	man = ttm_manager_type(priv->devs->ttm_dev, mem_type);
+	man->func->free(man, res);
+
+	KUNIT_ASSERT_TRUE(test, list_empty(&man->lru[bo->priority]));
+	KUNIT_ASSERT_EQ(test, man->usage, 0);
+}
+
+static struct kunit_case ttm_resource_test_cases[] = {
+	KUNIT_CASE_PARAM(ttm_resource_init_basic, ttm_resource_gen_params),
+	KUNIT_CASE(ttm_resource_init_pinned),
+	KUNIT_CASE(ttm_resource_fini_basic),
+	KUNIT_CASE(ttm_resource_manager_init_basic),
+	KUNIT_CASE(ttm_resource_manager_usage_basic),
+	KUNIT_CASE(ttm_resource_manager_set_used_basic),
+	KUNIT_CASE(ttm_sys_man_alloc_basic),
+	KUNIT_CASE(ttm_sys_man_free_basic),
+	{}
+};
+
+static struct kunit_suite ttm_resource_test_suite = {
+	.name = "ttm_resource",
+	.init = ttm_resource_test_init,
+	.exit = ttm_resource_test_fini,
+	.test_cases = ttm_resource_test_cases,
+};
+
+kunit_test_suites(&ttm_resource_test_suite);
+
+MODULE_LICENSE("GPL");
drivers/gpu/drm/ttm/ttm_resource.c +3 -0
index 46ff9c75bb12..02b96d23fdb9 100644
@@ -30,6 +30,8 @@
  #include <drm/ttm/ttm_placement.h>
  #include <drm/ttm/ttm_resource.h>
 
+#include <drm/drm_util.h>
+
  /**
   * ttm_lru_bulk_move_init - initialize a bulk move structure
   * @bulk: the structure to init
@@ -240,6 +242,7 @@ int ttm_resource_alloc(struct ttm_buffer_object *bo,
  	spin_unlock(&bo->bdev->lru_lock);
  	return 0;
  }
+EXPORT_SYMBOL_FOR_TESTS_ONLY(ttm_resource_alloc);
 
  void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res)
  {
#3 lore raw

Re: [PATCH v3 1/4] drm/ttm/tests: Add tests for ttm_resource and ttm_sys_man

Quoted 43 lines · Christian König

Am 12.09.23 um 13:49 schrieb Karolina Stolarek:

Quoted 31 lines

Test initialization of ttm_resource using different memory domains. Add tests for a system memory manager and functions that can be tested without a fully-featured resource manager. Update ttm_bo_kunit_init() to initialize BO's kref and reservation object. Export ttm_resource_alloc symbol for test purposes only.

---
  drivers/gpu/drm/ttm/tests/Makefile            |   1 +

  drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c |  23 ++
  drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h |   4 +
  drivers/gpu/drm/ttm/tests/ttm_resource_test.c | 335 ++++++++++++++++++
  drivers/gpu/drm/ttm/ttm_resource.c            |   3 +
  5 files changed, 366 insertions(+)

  create mode 100644 drivers/gpu/drm/ttm/tests/ttm_resource_test.c

drivers/gpu/drm/ttm/tests/Makefile +1 -0
index ec87c4fc1ad5..c92fe2052ef6 100644
@@ -3,4 +3,5 @@
   obj-$(CONFIG_DRM_TTM_KUNIT_TEST) += \
           ttm_device_test.o \
           ttm_pool_test.o \
+        ttm_resource_test.o \

          ttm_kunit_helpers.o

drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c +0 -0
index 81661d8827aa..eccc59b981f8 100644

drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c struct ttm_buffer_object *ttm_bo_kunit_init(struct · line 45

Quoted 8 lines
drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c +5 -0
@@ -38,10 +38,33 @@ struct ttm_buffer_object *ttm_bo_kunit_init(struct 
 kunit *test,
       bo->base = gem_obj;
       bo->bdev = devs->ttm_dev;
+    kref_init(&bo->kref);
+
+    dma_resv_init(&bo->base._resv);
+    bo->base.resv = &bo->base._resv;
+

I'm really wondering if we shouldn't now initialize the GEM object properly?

Quoted 1 line

That would also initialize the reservation object if I remember correctly.

Do you mean by using drm_gem_object_init()?

Quoted 2 lines

The solution with EXPORT_SYMBOL_FOR_TESTS_ONLY looks really nice I think and apart from that I can't see anything obviously wrong either, but I only skimmed over the code.

I'm also glad with EXPORT_SYMBOL_FOR_TESTS_ONLY solution, it's much better now. Right, you can take a closer look at the next version. I'll try to get an actual GEM object here, if you think that's feasible.

All the best,
Karolina

Quoted 414 lines

Regards,
Christian.

Quoted 409 lines
drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c +18 -0
       return bo;
   }
   EXPORT_SYMBOL_GPL(ttm_bo_kunit_init);
+struct ttm_place *ttm_place_kunit_init(struct kunit *test,
+                       uint32_t mem_type, uint32_t flags,
+                       size_t size)
+{
+    struct ttm_place *place;
+
+    place = kunit_kzalloc(test, sizeof(*place), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, place);
+
+    place->mem_type = mem_type;
+    place->flags = flags;
+    place->fpfn = size >> PAGE_SHIFT;
+    place->lpfn = place->fpfn + (size >> PAGE_SHIFT);
+
+    return place;
+}
+EXPORT_SYMBOL_GPL(ttm_place_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 e261e3660d0b..f38140f93c05 100644
@@ -8,6 +8,7 @@
   #include <drm/drm_drv.h>
   #include <drm/ttm/ttm_device.h>
   #include <drm/ttm/ttm_bo.h>
+#include <drm/ttm/ttm_placement.h>
   #include <drm/drm_kunit_helpers.h>
   #include <kunit/test.h>
@@ -28,6 +29,9 @@ int ttm_device_kunit_init(struct ttm_test_devices 
 *priv,
   struct ttm_buffer_object *ttm_bo_kunit_init(struct kunit *test,
                           struct ttm_test_devices *devs,
                           size_t size);
+struct ttm_place *ttm_place_kunit_init(struct kunit *test,
+                       uint32_t mem_type, uint32_t flags,
+                       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_resource_test.c new file +24 -0
new file mode 100644
index 000000000000..851cdc43dc37
@@ -0,0 +1,335 @@
+// SPDX-License-Identifier: GPL-2.0 AND MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+#include <drm/ttm/ttm_resource.h>
+
+#include "ttm_kunit_helpers.h"
+
+#define RES_SIZE        SZ_4K
+#define TTM_PRIV_DUMMY_REG    (TTM_NUM_MEM_TYPES - 1)
+
+struct ttm_resource_test_case {
+    const char *description;
+    uint32_t mem_type;
+    uint32_t flags;
+};
+
+struct ttm_resource_test_priv {
+    struct ttm_test_devices *devs;
+    struct ttm_buffer_object *bo;
+    struct ttm_place *place;
+};
+
+static const struct ttm_resource_manager_func 

ttm_resource_manager_mock_funcs = { };

drivers/gpu/drm/ttm/tests/ttm_resource_test.c new file +70 -0
+
+static int ttm_resource_test_init(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv;
+
+    priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, priv);
+
+    priv->devs = ttm_test_devices_all(test);
+    KUNIT_ASSERT_NOT_NULL(test, priv->devs);
+
+    test->priv = priv;
+
+    return 0;
+}
+
+static void ttm_resource_test_fini(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+
+    ttm_test_devices_put(test, priv->devs);
+}
+
+static void ttm_init_test_mocks(struct kunit *test,
+                struct ttm_resource_test_priv *priv,
+                uint32_t mem_type, uint32_t flags)
+{
+    size_t size = RES_SIZE;
+
+    /* Make sure we have what we need for a good BO mock */
+    KUNIT_ASSERT_NOT_NULL(test, priv->devs->ttm_dev);
+
+    priv->bo = ttm_bo_kunit_init(test, priv->devs, size);
+    priv->place = ttm_place_kunit_init(test, mem_type, flags, size);
+}
+
+static void ttm_init_test_manager(struct kunit *test,
+                  struct ttm_resource_test_priv *priv,
+                  uint32_t mem_type)
+{
+    struct ttm_device *ttm_dev = priv->devs->ttm_dev;
+    struct ttm_resource_manager *man;
+    size_t size = SZ_16K;
+
+    man = kunit_kzalloc(test, sizeof(*man), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, man);
+
+    man->use_tt = false;
+    man->func = &ttm_resource_manager_mock_funcs;
+
+    ttm_resource_manager_init(man, ttm_dev, size);
+    ttm_set_driver_manager(ttm_dev, mem_type, man);
+    ttm_resource_manager_set_used(man, true);
+}
+
+static const struct ttm_resource_test_case ttm_resource_cases[] = {
+    {
+        .description = "Init resource in TTM_PL_SYSTEM",
+        .mem_type = TTM_PL_SYSTEM,
+    },
+    {
+        .description = "Init resource in TTM_PL_VRAM",
+        .mem_type = TTM_PL_VRAM,
+    },
+    {
+        .description = "Init resource in a private placement",
+        .mem_type = TTM_PRIV_DUMMY_REG,
+    },
+    {
+        .description = "Init resource in TTM_PL_SYSTEM, set placement 

flags",
+        .mem_type = TTM_PL_SYSTEM,
+        .flags = TTM_PL_FLAG_TOPDOWN,
+    },

drivers/gpu/drm/ttm/tests/ttm_resource_test.c new file +3 -0
+};
+
+static void ttm_resource_case_desc(const struct 

ttm_resource_test_case *t, char *desc)

drivers/gpu/drm/ttm/tests/ttm_resource_test.c new file +5 -0
+{
+    strscpy(desc, t->description, KUNIT_PARAM_DESC_SIZE);
+}
+
+KUNIT_ARRAY_PARAM(ttm_resource, ttm_resource_cases, 

ttm_resource_case_desc);

drivers/gpu/drm/ttm/tests/ttm_resource_test.c new file +230 -0
+
+static void ttm_resource_init_basic(struct kunit *test)
+{
+    const struct ttm_resource_test_case *params = test->param_value;
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource *res;
+    struct ttm_buffer_object *bo;
+    struct ttm_place *place;
+    struct ttm_resource_manager *man;
+    uint64_t expected_usage;
+
+    ttm_init_test_mocks(test, priv, params->mem_type, params->flags);
+    bo = priv->bo;
+    place = priv->place;
+
+    if (params->mem_type > TTM_PL_SYSTEM)
+        ttm_init_test_manager(test, priv, params->mem_type);
+
+    res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, res);
+
+    man = ttm_manager_type(priv->devs->ttm_dev, place->mem_type);
+    expected_usage = man->usage + RES_SIZE;
+
+    KUNIT_ASSERT_TRUE(test, list_empty(&man->lru[bo->priority]));
+
+    ttm_resource_init(bo, place, res);
+
+    KUNIT_ASSERT_EQ(test, res->start, 0);
+    KUNIT_ASSERT_EQ(test, res->size, RES_SIZE);
+    KUNIT_ASSERT_EQ(test, res->mem_type, place->mem_type);
+    KUNIT_ASSERT_EQ(test, res->placement, place->flags);
+    KUNIT_ASSERT_PTR_EQ(test, res->bo, bo);
+
+    KUNIT_ASSERT_NULL(test, res->bus.addr);
+    KUNIT_ASSERT_EQ(test, res->bus.offset, 0);
+    KUNIT_ASSERT_FALSE(test, res->bus.is_iomem);
+    KUNIT_ASSERT_EQ(test, res->bus.caching, ttm_cached);
+    KUNIT_ASSERT_EQ(test, man->usage, expected_usage);
+
+    KUNIT_ASSERT_TRUE(test, list_is_singular(&man->lru[bo->priority]));
+
+    ttm_resource_fini(man, res);
+}
+
+static void ttm_resource_init_pinned(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource *res;
+    struct ttm_buffer_object *bo;
+    struct ttm_place *place;
+    struct ttm_resource_manager *man;
+
+    ttm_init_test_mocks(test, priv, TTM_PL_SYSTEM, 0);
+    bo = priv->bo;
+    place = priv->place;
+
+    man = ttm_manager_type(priv->devs->ttm_dev, place->mem_type);
+
+    res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, res);
+    KUNIT_ASSERT_TRUE(test, list_empty(&bo->bdev->pinned));
+
+    dma_resv_lock(bo->base.resv, NULL);
+    ttm_bo_pin(bo);
+    ttm_resource_init(bo, place, res);
+    KUNIT_ASSERT_TRUE(test, list_is_singular(&bo->bdev->pinned));
+
+    ttm_bo_unpin(bo);
+    ttm_resource_fini(man, res);
+    dma_resv_unlock(bo->base.resv);
+
+    KUNIT_ASSERT_TRUE(test, list_empty(&bo->bdev->pinned));
+}
+
+static void ttm_resource_fini_basic(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource *res;
+    struct ttm_buffer_object *bo;
+    struct ttm_place *place;
+    struct ttm_resource_manager *man;
+
+    ttm_init_test_mocks(test, priv, TTM_PL_SYSTEM, 0);
+    bo = priv->bo;
+    place = priv->place;
+
+    man = ttm_manager_type(priv->devs->ttm_dev, place->mem_type);
+
+    res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, res);
+
+    ttm_resource_init(bo, place, res);
+    ttm_resource_fini(man, res);
+
+    KUNIT_ASSERT_TRUE(test, list_empty(&res->lru));
+    KUNIT_ASSERT_EQ(test, man->usage, 0);
+}
+
+static void ttm_resource_manager_init_basic(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource_manager *man;
+    size_t size = SZ_16K;
+
+    man = kunit_kzalloc(test, sizeof(*man), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, man);
+
+    ttm_resource_manager_init(man, priv->devs->ttm_dev, size);
+
+    KUNIT_ASSERT_PTR_EQ(test, man->bdev, priv->devs->ttm_dev);
+    KUNIT_ASSERT_EQ(test, man->size, size);
+    KUNIT_ASSERT_EQ(test, man->usage, 0);
+    KUNIT_ASSERT_NULL(test, man->move);
+    KUNIT_ASSERT_NOT_NULL(test, &man->move_lock);
+
+    for (int i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
+        KUNIT_ASSERT_TRUE(test, list_empty(&man->lru[i]));
+}
+
+static void ttm_resource_manager_usage_basic(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource *res;
+    struct ttm_buffer_object *bo;
+    struct ttm_place *place;
+    struct ttm_resource_manager *man;
+    uint64_t actual_usage;
+
+    ttm_init_test_mocks(test, priv, TTM_PL_SYSTEM, TTM_PL_FLAG_TOPDOWN);
+    bo = priv->bo;
+    place = priv->place;
+
+    res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, res);
+
+    man = ttm_manager_type(priv->devs->ttm_dev, place->mem_type);
+
+    ttm_resource_init(bo, place, res);
+    actual_usage = ttm_resource_manager_usage(man);
+
+    KUNIT_ASSERT_EQ(test, actual_usage, RES_SIZE);
+
+    ttm_resource_fini(man, res);
+}
+
+static void ttm_resource_manager_set_used_basic(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource_manager *man;
+
+    man = ttm_manager_type(priv->devs->ttm_dev, TTM_PL_SYSTEM);
+    KUNIT_ASSERT_TRUE(test, man->use_type);
+
+    ttm_resource_manager_set_used(man, false);
+    KUNIT_ASSERT_FALSE(test, man->use_type);
+}
+
+static void ttm_sys_man_alloc_basic(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource_manager *man;
+    struct ttm_buffer_object *bo;
+    struct ttm_place *place;
+    struct ttm_resource *res;
+    uint32_t mem_type = TTM_PL_SYSTEM;
+    int ret;
+
+    ttm_init_test_mocks(test, priv, mem_type, 0);
+    bo = priv->bo;
+    place = priv->place;
+
+    man = ttm_manager_type(priv->devs->ttm_dev, mem_type);
+    ret = man->func->alloc(man, bo, place, &res);
+
+    KUNIT_ASSERT_EQ(test, ret, 0);
+    KUNIT_ASSERT_EQ(test, res->size, RES_SIZE);
+    KUNIT_ASSERT_EQ(test, res->mem_type, mem_type);
+    KUNIT_ASSERT_PTR_EQ(test, res->bo, bo);
+
+    ttm_resource_fini(man, res);
+}
+
+static void ttm_sys_man_free_basic(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource_manager *man;
+    struct ttm_buffer_object *bo;
+    struct ttm_place *place;
+    struct ttm_resource *res;
+    uint32_t mem_type = TTM_PL_SYSTEM;
+
+    ttm_init_test_mocks(test, priv, mem_type, 0);
+    bo = priv->bo;
+    place = priv->place;
+
+    res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, res);
+
+    ttm_resource_alloc(bo, place, &res);
+
+    man = ttm_manager_type(priv->devs->ttm_dev, mem_type);
+    man->func->free(man, res);
+
+    KUNIT_ASSERT_TRUE(test, list_empty(&man->lru[bo->priority]));
+    KUNIT_ASSERT_EQ(test, man->usage, 0);
+}
+
+static struct kunit_case ttm_resource_test_cases[] = {
+    KUNIT_CASE_PARAM(ttm_resource_init_basic, ttm_resource_gen_params),
+    KUNIT_CASE(ttm_resource_init_pinned),
+    KUNIT_CASE(ttm_resource_fini_basic),
+    KUNIT_CASE(ttm_resource_manager_init_basic),
+    KUNIT_CASE(ttm_resource_manager_usage_basic),
+    KUNIT_CASE(ttm_resource_manager_set_used_basic),
+    KUNIT_CASE(ttm_sys_man_alloc_basic),
+    KUNIT_CASE(ttm_sys_man_free_basic),
+    {}
+};
+
+static struct kunit_suite ttm_resource_test_suite = {
+    .name = "ttm_resource",
+    .init = ttm_resource_test_init,
+    .exit = ttm_resource_test_fini,
+    .test_cases = ttm_resource_test_cases,
+};
+
+kunit_test_suites(&ttm_resource_test_suite);
+
+MODULE_LICENSE("GPL");
drivers/gpu/drm/ttm/ttm_resource.c +3 -0
index 46ff9c75bb12..02b96d23fdb9 100644
@@ -30,6 +30,8 @@
   #include <drm/ttm/ttm_placement.h>
   #include <drm/ttm/ttm_resource.h>
+#include <drm/drm_util.h>
+
   /**
    * ttm_lru_bulk_move_init - initialize a bulk move structure
    * @bulk: the structure to init
@@ -240,6 +242,7 @@ int ttm_resource_alloc(struct ttm_buffer_object *bo,
       spin_unlock(&bo->bdev->lru_lock);
       return 0;
   }
+EXPORT_SYMBOL_FOR_TESTS_ONLY(ttm_resource_alloc);
   void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res)
   {
#4 lore raw

Re: [PATCH v3 1/4] drm/ttm/tests: Add tests for ttm_resource and ttm_sys_man

Am 18.09.23 um 13:48 schrieb Karolina Stolarek:

Quoted 45 lines · Christian König
Quoted 44 lines

Am 12.09.23 um 13:49 schrieb Karolina Stolarek:

Quoted 32 lines

Test initialization of ttm_resource using different memory domains. Add tests for a system memory manager and functions that can be tested without a fully-featured resource manager. Update ttm_bo_kunit_init() to initialize BO's kref and reservation object. Export ttm_resource_alloc symbol for test purposes only.

---
  drivers/gpu/drm/ttm/tests/Makefile            |   1 +

  drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c |  23 ++
  drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h |   4 +
  drivers/gpu/drm/ttm/tests/ttm_resource_test.c | 335 
++++++++++++++++++
  drivers/gpu/drm/ttm/ttm_resource.c            |   3 +
  5 files changed, 366 insertions(+)

  create mode 100644 drivers/gpu/drm/ttm/tests/ttm_resource_test.c

drivers/gpu/drm/ttm/tests/Makefile +1 -0
index ec87c4fc1ad5..c92fe2052ef6 100644
@@ -3,4 +3,5 @@
   obj-$(CONFIG_DRM_TTM_KUNIT_TEST) += \
           ttm_device_test.o \
           ttm_pool_test.o \
+        ttm_resource_test.o \

          ttm_kunit_helpers.o

drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c +0 -0
index 81661d8827aa..eccc59b981f8 100644

drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c struct ttm_buffer_object · line 45

Quoted 8 lines
drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c +5 -0
@@ -38,10 +38,33 @@ struct ttm_buffer_object 
 *ttm_bo_kunit_init(struct kunit *test,
       bo->base = gem_obj;
       bo->bdev = devs->ttm_dev;
+    kref_init(&bo->kref);
+
+    dma_resv_init(&bo->base._resv);
+    bo->base.resv = &bo->base._resv;
+

I'm really wondering if we shouldn't now initialize the GEM object properly?

Quoted 3 lines
Quoted 1 line

That would also initialize the reservation object if I remember correctly.

Do you mean by using drm_gem_object_init()?

I think drm_gem_private_object_init is the right function for this, but not 100% sure.

Quoted 5 lines
Quoted 3 lines

The solution with EXPORT_SYMBOL_FOR_TESTS_ONLY looks really nice I think and apart from that I can't see anything obviously wrong either, but I only skimmed over the code.

I'm also glad with EXPORT_SYMBOL_FOR_TESTS_ONLY solution, it's much better now. Right, you can take a closer look at the next version. I'll try to get an actual GEM object here, if you think that's feasible.

I can't promise it, but going to take a look when I have time.

Regards,
Christian.

Quoted 422 lines

All the best,
Karolina

Quoted 418 lines

Regards,
Christian.

Quoted 413 lines
drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c +18 -0
       return bo;
   }
   EXPORT_SYMBOL_GPL(ttm_bo_kunit_init);
+struct ttm_place *ttm_place_kunit_init(struct kunit *test,
+                       uint32_t mem_type, uint32_t flags,
+                       size_t size)
+{
+    struct ttm_place *place;
+
+    place = kunit_kzalloc(test, sizeof(*place), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, place);
+
+    place->mem_type = mem_type;
+    place->flags = flags;
+    place->fpfn = size >> PAGE_SHIFT;
+    place->lpfn = place->fpfn + (size >> PAGE_SHIFT);
+
+    return place;
+}
+EXPORT_SYMBOL_GPL(ttm_place_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 e261e3660d0b..f38140f93c05 100644
@@ -8,6 +8,7 @@
   #include <drm/drm_drv.h>
   #include <drm/ttm/ttm_device.h>
   #include <drm/ttm/ttm_bo.h>
+#include <drm/ttm/ttm_placement.h>
   #include <drm/drm_kunit_helpers.h>
   #include <kunit/test.h>
@@ -28,6 +29,9 @@ int ttm_device_kunit_init(struct ttm_test_devices 
 *priv,
   struct ttm_buffer_object *ttm_bo_kunit_init(struct kunit *test,
                           struct ttm_test_devices *devs,
                           size_t size);
+struct ttm_place *ttm_place_kunit_init(struct kunit *test,
+                       uint32_t mem_type, uint32_t flags,
+                       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_resource_test.c new file +24 -0
new file mode 100644
index 000000000000..851cdc43dc37
@@ -0,0 +1,335 @@
+// SPDX-License-Identifier: GPL-2.0 AND MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+#include <drm/ttm/ttm_resource.h>
+
+#include "ttm_kunit_helpers.h"
+
+#define RES_SIZE        SZ_4K
+#define TTM_PRIV_DUMMY_REG    (TTM_NUM_MEM_TYPES - 1)
+
+struct ttm_resource_test_case {
+    const char *description;
+    uint32_t mem_type;
+    uint32_t flags;
+};
+
+struct ttm_resource_test_priv {
+    struct ttm_test_devices *devs;
+    struct ttm_buffer_object *bo;
+    struct ttm_place *place;
+};
+
+static const struct ttm_resource_manager_func 

ttm_resource_manager_mock_funcs = { };

drivers/gpu/drm/ttm/tests/ttm_resource_test.c new file +70 -0
+
+static int ttm_resource_test_init(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv;
+
+    priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, priv);
+
+    priv->devs = ttm_test_devices_all(test);
+    KUNIT_ASSERT_NOT_NULL(test, priv->devs);
+
+    test->priv = priv;
+
+    return 0;
+}
+
+static void ttm_resource_test_fini(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+
+    ttm_test_devices_put(test, priv->devs);
+}
+
+static void ttm_init_test_mocks(struct kunit *test,
+                struct ttm_resource_test_priv *priv,
+                uint32_t mem_type, uint32_t flags)
+{
+    size_t size = RES_SIZE;
+
+    /* Make sure we have what we need for a good BO mock */
+    KUNIT_ASSERT_NOT_NULL(test, priv->devs->ttm_dev);
+
+    priv->bo = ttm_bo_kunit_init(test, priv->devs, size);
+    priv->place = ttm_place_kunit_init(test, mem_type, flags, size);
+}
+
+static void ttm_init_test_manager(struct kunit *test,
+                  struct ttm_resource_test_priv *priv,
+                  uint32_t mem_type)
+{
+    struct ttm_device *ttm_dev = priv->devs->ttm_dev;
+    struct ttm_resource_manager *man;
+    size_t size = SZ_16K;
+
+    man = kunit_kzalloc(test, sizeof(*man), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, man);
+
+    man->use_tt = false;
+    man->func = &ttm_resource_manager_mock_funcs;
+
+    ttm_resource_manager_init(man, ttm_dev, size);
+    ttm_set_driver_manager(ttm_dev, mem_type, man);
+    ttm_resource_manager_set_used(man, true);
+}
+
+static const struct ttm_resource_test_case ttm_resource_cases[] = {
+    {
+        .description = "Init resource in TTM_PL_SYSTEM",
+        .mem_type = TTM_PL_SYSTEM,
+    },
+    {
+        .description = "Init resource in TTM_PL_VRAM",
+        .mem_type = TTM_PL_VRAM,
+    },
+    {
+        .description = "Init resource in a private placement",
+        .mem_type = TTM_PRIV_DUMMY_REG,
+    },
+    {
+        .description = "Init resource in TTM_PL_SYSTEM, set 

placement flags",
+        .mem_type = TTM_PL_SYSTEM,
+        .flags = TTM_PL_FLAG_TOPDOWN,
+    },

drivers/gpu/drm/ttm/tests/ttm_resource_test.c new file +3 -0
+};
+
+static void ttm_resource_case_desc(const struct 

ttm_resource_test_case *t, char *desc)

drivers/gpu/drm/ttm/tests/ttm_resource_test.c new file +5 -0
+{
+    strscpy(desc, t->description, KUNIT_PARAM_DESC_SIZE);
+}
+
+KUNIT_ARRAY_PARAM(ttm_resource, ttm_resource_cases, 

ttm_resource_case_desc);

drivers/gpu/drm/ttm/tests/ttm_resource_test.c new file +41 -0
+
+static void ttm_resource_init_basic(struct kunit *test)
+{
+    const struct ttm_resource_test_case *params = test->param_value;
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource *res;
+    struct ttm_buffer_object *bo;
+    struct ttm_place *place;
+    struct ttm_resource_manager *man;
+    uint64_t expected_usage;
+
+    ttm_init_test_mocks(test, priv, params->mem_type, params->flags);
+    bo = priv->bo;
+    place = priv->place;
+
+    if (params->mem_type > TTM_PL_SYSTEM)
+        ttm_init_test_manager(test, priv, params->mem_type);
+
+    res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, res);
+
+    man = ttm_manager_type(priv->devs->ttm_dev, place->mem_type);
+    expected_usage = man->usage + RES_SIZE;
+
+    KUNIT_ASSERT_TRUE(test, list_empty(&man->lru[bo->priority]));
+
+    ttm_resource_init(bo, place, res);
+
+    KUNIT_ASSERT_EQ(test, res->start, 0);
+    KUNIT_ASSERT_EQ(test, res->size, RES_SIZE);
+    KUNIT_ASSERT_EQ(test, res->mem_type, place->mem_type);
+    KUNIT_ASSERT_EQ(test, res->placement, place->flags);
+    KUNIT_ASSERT_PTR_EQ(test, res->bo, bo);
+
+    KUNIT_ASSERT_NULL(test, res->bus.addr);
+    KUNIT_ASSERT_EQ(test, res->bus.offset, 0);
+    KUNIT_ASSERT_FALSE(test, res->bus.is_iomem);
+    KUNIT_ASSERT_EQ(test, res->bus.caching, ttm_cached);
+    KUNIT_ASSERT_EQ(test, man->usage, expected_usage);
+
+    KUNIT_ASSERT_TRUE(test, 

list_is_singular(&man->lru[bo->priority]));

drivers/gpu/drm/ttm/tests/ttm_resource_test.c new file +89 -0
+
+    ttm_resource_fini(man, res);
+}
+
+static void ttm_resource_init_pinned(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource *res;
+    struct ttm_buffer_object *bo;
+    struct ttm_place *place;
+    struct ttm_resource_manager *man;
+
+    ttm_init_test_mocks(test, priv, TTM_PL_SYSTEM, 0);
+    bo = priv->bo;
+    place = priv->place;
+
+    man = ttm_manager_type(priv->devs->ttm_dev, place->mem_type);
+
+    res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, res);
+    KUNIT_ASSERT_TRUE(test, list_empty(&bo->bdev->pinned));
+
+    dma_resv_lock(bo->base.resv, NULL);
+    ttm_bo_pin(bo);
+    ttm_resource_init(bo, place, res);
+    KUNIT_ASSERT_TRUE(test, list_is_singular(&bo->bdev->pinned));
+
+    ttm_bo_unpin(bo);
+    ttm_resource_fini(man, res);
+    dma_resv_unlock(bo->base.resv);
+
+    KUNIT_ASSERT_TRUE(test, list_empty(&bo->bdev->pinned));
+}
+
+static void ttm_resource_fini_basic(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource *res;
+    struct ttm_buffer_object *bo;
+    struct ttm_place *place;
+    struct ttm_resource_manager *man;
+
+    ttm_init_test_mocks(test, priv, TTM_PL_SYSTEM, 0);
+    bo = priv->bo;
+    place = priv->place;
+
+    man = ttm_manager_type(priv->devs->ttm_dev, place->mem_type);
+
+    res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, res);
+
+    ttm_resource_init(bo, place, res);
+    ttm_resource_fini(man, res);
+
+    KUNIT_ASSERT_TRUE(test, list_empty(&res->lru));
+    KUNIT_ASSERT_EQ(test, man->usage, 0);
+}
+
+static void ttm_resource_manager_init_basic(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource_manager *man;
+    size_t size = SZ_16K;
+
+    man = kunit_kzalloc(test, sizeof(*man), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, man);
+
+    ttm_resource_manager_init(man, priv->devs->ttm_dev, size);
+
+    KUNIT_ASSERT_PTR_EQ(test, man->bdev, priv->devs->ttm_dev);
+    KUNIT_ASSERT_EQ(test, man->size, size);
+    KUNIT_ASSERT_EQ(test, man->usage, 0);
+    KUNIT_ASSERT_NULL(test, man->move);
+    KUNIT_ASSERT_NOT_NULL(test, &man->move_lock);
+
+    for (int i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
+        KUNIT_ASSERT_TRUE(test, list_empty(&man->lru[i]));
+}
+
+static void ttm_resource_manager_usage_basic(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource *res;
+    struct ttm_buffer_object *bo;
+    struct ttm_place *place;
+    struct ttm_resource_manager *man;
+    uint64_t actual_usage;
+
+    ttm_init_test_mocks(test, priv, TTM_PL_SYSTEM, 

TTM_PL_FLAG_TOPDOWN);
+    bo = priv->bo;
+    place = priv->place;

drivers/gpu/drm/ttm/tests/ttm_resource_test.c new file +78 -0
+
+    res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, res);
+
+    man = ttm_manager_type(priv->devs->ttm_dev, place->mem_type);
+
+    ttm_resource_init(bo, place, res);
+    actual_usage = ttm_resource_manager_usage(man);
+
+    KUNIT_ASSERT_EQ(test, actual_usage, RES_SIZE);
+
+    ttm_resource_fini(man, res);
+}
+
+static void ttm_resource_manager_set_used_basic(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource_manager *man;
+
+    man = ttm_manager_type(priv->devs->ttm_dev, TTM_PL_SYSTEM);
+    KUNIT_ASSERT_TRUE(test, man->use_type);
+
+    ttm_resource_manager_set_used(man, false);
+    KUNIT_ASSERT_FALSE(test, man->use_type);
+}
+
+static void ttm_sys_man_alloc_basic(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource_manager *man;
+    struct ttm_buffer_object *bo;
+    struct ttm_place *place;
+    struct ttm_resource *res;
+    uint32_t mem_type = TTM_PL_SYSTEM;
+    int ret;
+
+    ttm_init_test_mocks(test, priv, mem_type, 0);
+    bo = priv->bo;
+    place = priv->place;
+
+    man = ttm_manager_type(priv->devs->ttm_dev, mem_type);
+    ret = man->func->alloc(man, bo, place, &res);
+
+    KUNIT_ASSERT_EQ(test, ret, 0);
+    KUNIT_ASSERT_EQ(test, res->size, RES_SIZE);
+    KUNIT_ASSERT_EQ(test, res->mem_type, mem_type);
+    KUNIT_ASSERT_PTR_EQ(test, res->bo, bo);
+
+    ttm_resource_fini(man, res);
+}
+
+static void ttm_sys_man_free_basic(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource_manager *man;
+    struct ttm_buffer_object *bo;
+    struct ttm_place *place;
+    struct ttm_resource *res;
+    uint32_t mem_type = TTM_PL_SYSTEM;
+
+    ttm_init_test_mocks(test, priv, mem_type, 0);
+    bo = priv->bo;
+    place = priv->place;
+
+    res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, res);
+
+    ttm_resource_alloc(bo, place, &res);
+
+    man = ttm_manager_type(priv->devs->ttm_dev, mem_type);
+    man->func->free(man, res);
+
+    KUNIT_ASSERT_TRUE(test, list_empty(&man->lru[bo->priority]));
+    KUNIT_ASSERT_EQ(test, man->usage, 0);
+}
+
+static struct kunit_case ttm_resource_test_cases[] = {
+    KUNIT_CASE_PARAM(ttm_resource_init_basic, 

ttm_resource_gen_params),
+    KUNIT_CASE(ttm_resource_init_pinned),
+    KUNIT_CASE(ttm_resource_fini_basic),
+    KUNIT_CASE(ttm_resource_manager_init_basic), +    KUNIT_CASE(ttm_resource_manager_usage_basic), +    KUNIT_CASE(ttm_resource_manager_set_used_basic), +    KUNIT_CASE(ttm_sys_man_alloc_basic),
+    KUNIT_CASE(ttm_sys_man_free_basic),
+    {}

drivers/gpu/drm/ttm/tests/ttm_resource_test.c new file +12 -0
+};
+
+static struct kunit_suite ttm_resource_test_suite = {
+    .name = "ttm_resource",
+    .init = ttm_resource_test_init,
+    .exit = ttm_resource_test_fini,
+    .test_cases = ttm_resource_test_cases,
+};
+
+kunit_test_suites(&ttm_resource_test_suite);
+
+MODULE_LICENSE("GPL");
drivers/gpu/drm/ttm/ttm_resource.c +3 -0
index 46ff9c75bb12..02b96d23fdb9 100644
@@ -30,6 +30,8 @@
   #include <drm/ttm/ttm_placement.h>
   #include <drm/ttm/ttm_resource.h>
+#include <drm/drm_util.h>
+
   /**
    * ttm_lru_bulk_move_init - initialize a bulk move structure
    * @bulk: the structure to init
@@ -240,6 +242,7 @@ int ttm_resource_alloc(struct ttm_buffer_object 
 *bo,
       spin_unlock(&bo->bdev->lru_lock);
       return 0;
   }
+EXPORT_SYMBOL_FOR_TESTS_ONLY(ttm_resource_alloc);
   void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res)
   {
#6 lore raw

Re: [PATCH v3 1/4] drm/ttm/tests: Add tests for ttm_resource and ttm_sys_man

In reply to Karolina Stolarek
Quoted 44 lines · Karolina Stolarek
Quoted 43 lines · Christian König

Am 12.09.23 um 13:49 schrieb Karolina Stolarek:

Quoted 31 lines

Test initialization of ttm_resource using different memory domains. Add tests for a system memory manager and functions that can be tested without a fully-featured resource manager. Update ttm_bo_kunit_init() to initialize BO's kref and reservation object. Export ttm_resource_alloc symbol for test purposes only.

---
  drivers/gpu/drm/ttm/tests/Makefile            |   1 +

  drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c |  23 ++
  drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h |   4 +
  drivers/gpu/drm/ttm/tests/ttm_resource_test.c | 335 ++++++++++++++++++
  drivers/gpu/drm/ttm/ttm_resource.c            |   3 +
  5 files changed, 366 insertions(+)

  create mode 100644 drivers/gpu/drm/ttm/tests/ttm_resource_test.c

drivers/gpu/drm/ttm/tests/Makefile +1 -0
index ec87c4fc1ad5..c92fe2052ef6 100644
@@ -3,4 +3,5 @@
   obj-$(CONFIG_DRM_TTM_KUNIT_TEST) += \
           ttm_device_test.o \
           ttm_pool_test.o \
+        ttm_resource_test.o \

          ttm_kunit_helpers.o

drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c +0 -0
index 81661d8827aa..eccc59b981f8 100644

drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c struct ttm_buffer_object · line 45

Quoted 8 lines
drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c +5 -0
@@ -38,10 +38,33 @@ struct ttm_buffer_object 
 *ttm_bo_kunit_init(struct kunit *test,
       bo->base = gem_obj;
       bo->bdev = devs->ttm_dev;
+    kref_init(&bo->kref);
+
+    dma_resv_init(&bo->base._resv);
+    bo->base.resv = &bo->base._resv;
+

I'm really wondering if we shouldn't now initialize the GEM object properly?

Quoted 3 lines
Quoted 1 line

That would also initialize the reservation object if I remember correctly.

Do you mean by using drm_gem_object_init()?

I tried initializing bo.base with drm_gem_object_init() and it's looking good, but one check in that function makes testing of "Misaligned size" subtest of ttm_tt_init_basic impossible:

BUG_ON((size & (PAGE_SIZE - 1)) != 0);

I wanted to test if ttm_tt_init properly calculates the number of pages, but it looks like in the normal circuimstances it already gets a GEM object of a size that is page-aligned. Would you prefer to write this subtest as a separate test with a dummy misaligned GEM object, or to delete this subtest altogether?

Many thanks,
Karolina

Quoted 424 lines
Quoted 3 lines

The solution with EXPORT_SYMBOL_FOR_TESTS_ONLY looks really nice I think and apart from that I can't see anything obviously wrong either, but I only skimmed over the code.

I'm also glad with EXPORT_SYMBOL_FOR_TESTS_ONLY solution, it's much better now. Right, you can take a closer look at the next version. I'll try to get an actual GEM object here, if you think that's feasible.

All the best,
Karolina

Quoted 415 lines

Regards,
Christian.

Quoted 410 lines
drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c +18 -0
       return bo;
   }
   EXPORT_SYMBOL_GPL(ttm_bo_kunit_init);
+struct ttm_place *ttm_place_kunit_init(struct kunit *test,
+                       uint32_t mem_type, uint32_t flags,
+                       size_t size)
+{
+    struct ttm_place *place;
+
+    place = kunit_kzalloc(test, sizeof(*place), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, place);
+
+    place->mem_type = mem_type;
+    place->flags = flags;
+    place->fpfn = size >> PAGE_SHIFT;
+    place->lpfn = place->fpfn + (size >> PAGE_SHIFT);
+
+    return place;
+}
+EXPORT_SYMBOL_GPL(ttm_place_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 e261e3660d0b..f38140f93c05 100644
@@ -8,6 +8,7 @@
   #include <drm/drm_drv.h>
   #include <drm/ttm/ttm_device.h>
   #include <drm/ttm/ttm_bo.h>
+#include <drm/ttm/ttm_placement.h>
   #include <drm/drm_kunit_helpers.h>
   #include <kunit/test.h>
@@ -28,6 +29,9 @@ int ttm_device_kunit_init(struct ttm_test_devices 
 *priv,
   struct ttm_buffer_object *ttm_bo_kunit_init(struct kunit *test,
                           struct ttm_test_devices *devs,
                           size_t size);
+struct ttm_place *ttm_place_kunit_init(struct kunit *test,
+                       uint32_t mem_type, uint32_t flags,
+                       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_resource_test.c new file +24 -0
new file mode 100644
index 000000000000..851cdc43dc37
@@ -0,0 +1,335 @@
+// SPDX-License-Identifier: GPL-2.0 AND MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+#include <drm/ttm/ttm_resource.h>
+
+#include "ttm_kunit_helpers.h"
+
+#define RES_SIZE        SZ_4K
+#define TTM_PRIV_DUMMY_REG    (TTM_NUM_MEM_TYPES - 1)
+
+struct ttm_resource_test_case {
+    const char *description;
+    uint32_t mem_type;
+    uint32_t flags;
+};
+
+struct ttm_resource_test_priv {
+    struct ttm_test_devices *devs;
+    struct ttm_buffer_object *bo;
+    struct ttm_place *place;
+};
+
+static const struct ttm_resource_manager_func 

ttm_resource_manager_mock_funcs = { };

drivers/gpu/drm/ttm/tests/ttm_resource_test.c new file +70 -0
+
+static int ttm_resource_test_init(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv;
+
+    priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, priv);
+
+    priv->devs = ttm_test_devices_all(test);
+    KUNIT_ASSERT_NOT_NULL(test, priv->devs);
+
+    test->priv = priv;
+
+    return 0;
+}
+
+static void ttm_resource_test_fini(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+
+    ttm_test_devices_put(test, priv->devs);
+}
+
+static void ttm_init_test_mocks(struct kunit *test,
+                struct ttm_resource_test_priv *priv,
+                uint32_t mem_type, uint32_t flags)
+{
+    size_t size = RES_SIZE;
+
+    /* Make sure we have what we need for a good BO mock */
+    KUNIT_ASSERT_NOT_NULL(test, priv->devs->ttm_dev);
+
+    priv->bo = ttm_bo_kunit_init(test, priv->devs, size);
+    priv->place = ttm_place_kunit_init(test, mem_type, flags, size);
+}
+
+static void ttm_init_test_manager(struct kunit *test,
+                  struct ttm_resource_test_priv *priv,
+                  uint32_t mem_type)
+{
+    struct ttm_device *ttm_dev = priv->devs->ttm_dev;
+    struct ttm_resource_manager *man;
+    size_t size = SZ_16K;
+
+    man = kunit_kzalloc(test, sizeof(*man), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, man);
+
+    man->use_tt = false;
+    man->func = &ttm_resource_manager_mock_funcs;
+
+    ttm_resource_manager_init(man, ttm_dev, size);
+    ttm_set_driver_manager(ttm_dev, mem_type, man);
+    ttm_resource_manager_set_used(man, true);
+}
+
+static const struct ttm_resource_test_case ttm_resource_cases[] = {
+    {
+        .description = "Init resource in TTM_PL_SYSTEM",
+        .mem_type = TTM_PL_SYSTEM,
+    },
+    {
+        .description = "Init resource in TTM_PL_VRAM",
+        .mem_type = TTM_PL_VRAM,
+    },
+    {
+        .description = "Init resource in a private placement",
+        .mem_type = TTM_PRIV_DUMMY_REG,
+    },
+    {
+        .description = "Init resource in TTM_PL_SYSTEM, set 

placement flags",
+        .mem_type = TTM_PL_SYSTEM,
+        .flags = TTM_PL_FLAG_TOPDOWN,
+    },

drivers/gpu/drm/ttm/tests/ttm_resource_test.c new file +3 -0
+};
+
+static void ttm_resource_case_desc(const struct 

ttm_resource_test_case *t, char *desc)

drivers/gpu/drm/ttm/tests/ttm_resource_test.c new file +5 -0
+{
+    strscpy(desc, t->description, KUNIT_PARAM_DESC_SIZE);
+}
+
+KUNIT_ARRAY_PARAM(ttm_resource, ttm_resource_cases, 

ttm_resource_case_desc);

drivers/gpu/drm/ttm/tests/ttm_resource_test.c new file +130 -0
+
+static void ttm_resource_init_basic(struct kunit *test)
+{
+    const struct ttm_resource_test_case *params = test->param_value;
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource *res;
+    struct ttm_buffer_object *bo;
+    struct ttm_place *place;
+    struct ttm_resource_manager *man;
+    uint64_t expected_usage;
+
+    ttm_init_test_mocks(test, priv, params->mem_type, params->flags);
+    bo = priv->bo;
+    place = priv->place;
+
+    if (params->mem_type > TTM_PL_SYSTEM)
+        ttm_init_test_manager(test, priv, params->mem_type);
+
+    res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, res);
+
+    man = ttm_manager_type(priv->devs->ttm_dev, place->mem_type);
+    expected_usage = man->usage + RES_SIZE;
+
+    KUNIT_ASSERT_TRUE(test, list_empty(&man->lru[bo->priority]));
+
+    ttm_resource_init(bo, place, res);
+
+    KUNIT_ASSERT_EQ(test, res->start, 0);
+    KUNIT_ASSERT_EQ(test, res->size, RES_SIZE);
+    KUNIT_ASSERT_EQ(test, res->mem_type, place->mem_type);
+    KUNIT_ASSERT_EQ(test, res->placement, place->flags);
+    KUNIT_ASSERT_PTR_EQ(test, res->bo, bo);
+
+    KUNIT_ASSERT_NULL(test, res->bus.addr);
+    KUNIT_ASSERT_EQ(test, res->bus.offset, 0);
+    KUNIT_ASSERT_FALSE(test, res->bus.is_iomem);
+    KUNIT_ASSERT_EQ(test, res->bus.caching, ttm_cached);
+    KUNIT_ASSERT_EQ(test, man->usage, expected_usage);
+
+    KUNIT_ASSERT_TRUE(test, list_is_singular(&man->lru[bo->priority]));
+
+    ttm_resource_fini(man, res);
+}
+
+static void ttm_resource_init_pinned(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource *res;
+    struct ttm_buffer_object *bo;
+    struct ttm_place *place;
+    struct ttm_resource_manager *man;
+
+    ttm_init_test_mocks(test, priv, TTM_PL_SYSTEM, 0);
+    bo = priv->bo;
+    place = priv->place;
+
+    man = ttm_manager_type(priv->devs->ttm_dev, place->mem_type);
+
+    res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, res);
+    KUNIT_ASSERT_TRUE(test, list_empty(&bo->bdev->pinned));
+
+    dma_resv_lock(bo->base.resv, NULL);
+    ttm_bo_pin(bo);
+    ttm_resource_init(bo, place, res);
+    KUNIT_ASSERT_TRUE(test, list_is_singular(&bo->bdev->pinned));
+
+    ttm_bo_unpin(bo);
+    ttm_resource_fini(man, res);
+    dma_resv_unlock(bo->base.resv);
+
+    KUNIT_ASSERT_TRUE(test, list_empty(&bo->bdev->pinned));
+}
+
+static void ttm_resource_fini_basic(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource *res;
+    struct ttm_buffer_object *bo;
+    struct ttm_place *place;
+    struct ttm_resource_manager *man;
+
+    ttm_init_test_mocks(test, priv, TTM_PL_SYSTEM, 0);
+    bo = priv->bo;
+    place = priv->place;
+
+    man = ttm_manager_type(priv->devs->ttm_dev, place->mem_type);
+
+    res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, res);
+
+    ttm_resource_init(bo, place, res);
+    ttm_resource_fini(man, res);
+
+    KUNIT_ASSERT_TRUE(test, list_empty(&res->lru));
+    KUNIT_ASSERT_EQ(test, man->usage, 0);
+}
+
+static void ttm_resource_manager_init_basic(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource_manager *man;
+    size_t size = SZ_16K;
+
+    man = kunit_kzalloc(test, sizeof(*man), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, man);
+
+    ttm_resource_manager_init(man, priv->devs->ttm_dev, size);
+
+    KUNIT_ASSERT_PTR_EQ(test, man->bdev, priv->devs->ttm_dev);
+    KUNIT_ASSERT_EQ(test, man->size, size);
+    KUNIT_ASSERT_EQ(test, man->usage, 0);
+    KUNIT_ASSERT_NULL(test, man->move);
+    KUNIT_ASSERT_NOT_NULL(test, &man->move_lock);
+
+    for (int i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
+        KUNIT_ASSERT_TRUE(test, list_empty(&man->lru[i]));
+}
+
+static void ttm_resource_manager_usage_basic(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource *res;
+    struct ttm_buffer_object *bo;
+    struct ttm_place *place;
+    struct ttm_resource_manager *man;
+    uint64_t actual_usage;
+
+    ttm_init_test_mocks(test, priv, TTM_PL_SYSTEM, 

TTM_PL_FLAG_TOPDOWN);
+    bo = priv->bo;
+    place = priv->place;

drivers/gpu/drm/ttm/tests/ttm_resource_test.c new file +98 -0
+
+    res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, res);
+
+    man = ttm_manager_type(priv->devs->ttm_dev, place->mem_type);
+
+    ttm_resource_init(bo, place, res);
+    actual_usage = ttm_resource_manager_usage(man);
+
+    KUNIT_ASSERT_EQ(test, actual_usage, RES_SIZE);
+
+    ttm_resource_fini(man, res);
+}
+
+static void ttm_resource_manager_set_used_basic(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource_manager *man;
+
+    man = ttm_manager_type(priv->devs->ttm_dev, TTM_PL_SYSTEM);
+    KUNIT_ASSERT_TRUE(test, man->use_type);
+
+    ttm_resource_manager_set_used(man, false);
+    KUNIT_ASSERT_FALSE(test, man->use_type);
+}
+
+static void ttm_sys_man_alloc_basic(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource_manager *man;
+    struct ttm_buffer_object *bo;
+    struct ttm_place *place;
+    struct ttm_resource *res;
+    uint32_t mem_type = TTM_PL_SYSTEM;
+    int ret;
+
+    ttm_init_test_mocks(test, priv, mem_type, 0);
+    bo = priv->bo;
+    place = priv->place;
+
+    man = ttm_manager_type(priv->devs->ttm_dev, mem_type);
+    ret = man->func->alloc(man, bo, place, &res);
+
+    KUNIT_ASSERT_EQ(test, ret, 0);
+    KUNIT_ASSERT_EQ(test, res->size, RES_SIZE);
+    KUNIT_ASSERT_EQ(test, res->mem_type, mem_type);
+    KUNIT_ASSERT_PTR_EQ(test, res->bo, bo);
+
+    ttm_resource_fini(man, res);
+}
+
+static void ttm_sys_man_free_basic(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource_manager *man;
+    struct ttm_buffer_object *bo;
+    struct ttm_place *place;
+    struct ttm_resource *res;
+    uint32_t mem_type = TTM_PL_SYSTEM;
+
+    ttm_init_test_mocks(test, priv, mem_type, 0);
+    bo = priv->bo;
+    place = priv->place;
+
+    res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, res);
+
+    ttm_resource_alloc(bo, place, &res);
+
+    man = ttm_manager_type(priv->devs->ttm_dev, mem_type);
+    man->func->free(man, res);
+
+    KUNIT_ASSERT_TRUE(test, list_empty(&man->lru[bo->priority]));
+    KUNIT_ASSERT_EQ(test, man->usage, 0);
+}
+
+static struct kunit_case ttm_resource_test_cases[] = {
+    KUNIT_CASE_PARAM(ttm_resource_init_basic, ttm_resource_gen_params),
+    KUNIT_CASE(ttm_resource_init_pinned),
+    KUNIT_CASE(ttm_resource_fini_basic),
+    KUNIT_CASE(ttm_resource_manager_init_basic),
+    KUNIT_CASE(ttm_resource_manager_usage_basic),
+    KUNIT_CASE(ttm_resource_manager_set_used_basic),
+    KUNIT_CASE(ttm_sys_man_alloc_basic),
+    KUNIT_CASE(ttm_sys_man_free_basic),
+    {}
+};
+
+static struct kunit_suite ttm_resource_test_suite = {
+    .name = "ttm_resource",
+    .init = ttm_resource_test_init,
+    .exit = ttm_resource_test_fini,
+    .test_cases = ttm_resource_test_cases,
+};
+
+kunit_test_suites(&ttm_resource_test_suite);
+
+MODULE_LICENSE("GPL");
drivers/gpu/drm/ttm/ttm_resource.c +3 -0
index 46ff9c75bb12..02b96d23fdb9 100644
@@ -30,6 +30,8 @@
   #include <drm/ttm/ttm_placement.h>
   #include <drm/ttm/ttm_resource.h>
+#include <drm/drm_util.h>
+
   /**
    * ttm_lru_bulk_move_init - initialize a bulk move structure
    * @bulk: the structure to init
@@ -240,6 +242,7 @@ int ttm_resource_alloc(struct ttm_buffer_object *bo,
       spin_unlock(&bo->bdev->lru_lock);
       return 0;
   }
+EXPORT_SYMBOL_FOR_TESTS_ONLY(ttm_resource_alloc);
   void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res)
   {
#5 lore raw

Re: [PATCH v3 1/4] drm/ttm/tests: Add tests for ttm_resource and ttm_sys_man

In reply to Christian König
Quoted 54 lines · Christian König

Am 18.09.23 um 13:48 schrieb Karolina Stolarek:

Quoted 45 lines · Christian König
Quoted 44 lines

Am 12.09.23 um 13:49 schrieb Karolina Stolarek:

Quoted 32 lines

Test initialization of ttm_resource using different memory domains. Add tests for a system memory manager and functions that can be tested without a fully-featured resource manager. Update ttm_bo_kunit_init() to initialize BO's kref and reservation object. Export ttm_resource_alloc symbol for test purposes only.

---
  drivers/gpu/drm/ttm/tests/Makefile            |   1 +

  drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c |  23 ++
  drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h |   4 +
  drivers/gpu/drm/ttm/tests/ttm_resource_test.c | 335 
++++++++++++++++++
  drivers/gpu/drm/ttm/ttm_resource.c            |   3 +
  5 files changed, 366 insertions(+)

  create mode 100644 drivers/gpu/drm/ttm/tests/ttm_resource_test.c

drivers/gpu/drm/ttm/tests/Makefile +1 -0
index ec87c4fc1ad5..c92fe2052ef6 100644
@@ -3,4 +3,5 @@
   obj-$(CONFIG_DRM_TTM_KUNIT_TEST) += \
           ttm_device_test.o \
           ttm_pool_test.o \
+        ttm_resource_test.o \

          ttm_kunit_helpers.o

drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c +0 -0
index 81661d8827aa..eccc59b981f8 100644

drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c struct ttm_buffer_object · line 45

Quoted 8 lines
drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c +5 -0
@@ -38,10 +38,33 @@ struct ttm_buffer_object 
 *ttm_bo_kunit_init(struct kunit *test,
       bo->base = gem_obj;
       bo->bdev = devs->ttm_dev;
+    kref_init(&bo->kref);
+
+    dma_resv_init(&bo->base._resv);
+    bo->base.resv = &bo->base._resv;
+

I'm really wondering if we shouldn't now initialize the GEM object properly?

Quoted 3 lines
Quoted 1 line

That would also initialize the reservation object if I remember correctly.

Do you mean by using drm_gem_object_init()?

I think drm_gem_private_object_init is the right function for this, but not 100% sure.

Quoted 2 lines
Quoted 2 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

The solution with EXPORT_SYMBOL_FOR_TESTS_ONLY looks really nice I think and apart from that I can't see anything obviously wrong either, but I only skimmed over the code.

I'm also glad with EXPORT_SYMBOL_FOR_TESTS_ONLY solution, it's much better now. Right, you can take a closer look at the next version. I'll try to get an actual GEM object here, if you think that's feasible.

I can't promise it, but going to take a look when I have time.

I understand. I'm still looking at this, because I found lockdep warnings in ttm_bo_reserve_deadlock and ttm_bo_reserve_double_resv. Be cautious when reviewing my code, I left some traps there...

The good news is that "real" GEM objects work fine in the tests and I found a way to keep the misaligned test case for ttm_tt_init.

Many thanks,
Karolina

Quoted 427 lines

Regards,
Christian.

Quoted 422 lines

All the best,
Karolina

Quoted 418 lines

Regards,
Christian.

Quoted 413 lines
drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c +18 -0
       return bo;
   }
   EXPORT_SYMBOL_GPL(ttm_bo_kunit_init);
+struct ttm_place *ttm_place_kunit_init(struct kunit *test,
+                       uint32_t mem_type, uint32_t flags,
+                       size_t size)
+{
+    struct ttm_place *place;
+
+    place = kunit_kzalloc(test, sizeof(*place), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, place);
+
+    place->mem_type = mem_type;
+    place->flags = flags;
+    place->fpfn = size >> PAGE_SHIFT;
+    place->lpfn = place->fpfn + (size >> PAGE_SHIFT);
+
+    return place;
+}
+EXPORT_SYMBOL_GPL(ttm_place_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 e261e3660d0b..f38140f93c05 100644
@@ -8,6 +8,7 @@
   #include <drm/drm_drv.h>
   #include <drm/ttm/ttm_device.h>
   #include <drm/ttm/ttm_bo.h>
+#include <drm/ttm/ttm_placement.h>
   #include <drm/drm_kunit_helpers.h>
   #include <kunit/test.h>
@@ -28,6 +29,9 @@ int ttm_device_kunit_init(struct ttm_test_devices 
 *priv,
   struct ttm_buffer_object *ttm_bo_kunit_init(struct kunit *test,
                           struct ttm_test_devices *devs,
                           size_t size);
+struct ttm_place *ttm_place_kunit_init(struct kunit *test,
+                       uint32_t mem_type, uint32_t flags,
+                       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_resource_test.c new file +24 -0
new file mode 100644
index 000000000000..851cdc43dc37
@@ -0,0 +1,335 @@
+// SPDX-License-Identifier: GPL-2.0 AND MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+#include <drm/ttm/ttm_resource.h>
+
+#include "ttm_kunit_helpers.h"
+
+#define RES_SIZE        SZ_4K
+#define TTM_PRIV_DUMMY_REG    (TTM_NUM_MEM_TYPES - 1)
+
+struct ttm_resource_test_case {
+    const char *description;
+    uint32_t mem_type;
+    uint32_t flags;
+};
+
+struct ttm_resource_test_priv {
+    struct ttm_test_devices *devs;
+    struct ttm_buffer_object *bo;
+    struct ttm_place *place;
+};
+
+static const struct ttm_resource_manager_func 

ttm_resource_manager_mock_funcs = { };

drivers/gpu/drm/ttm/tests/ttm_resource_test.c new file +70 -0
+
+static int ttm_resource_test_init(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv;
+
+    priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, priv);
+
+    priv->devs = ttm_test_devices_all(test);
+    KUNIT_ASSERT_NOT_NULL(test, priv->devs);
+
+    test->priv = priv;
+
+    return 0;
+}
+
+static void ttm_resource_test_fini(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+
+    ttm_test_devices_put(test, priv->devs);
+}
+
+static void ttm_init_test_mocks(struct kunit *test,
+                struct ttm_resource_test_priv *priv,
+                uint32_t mem_type, uint32_t flags)
+{
+    size_t size = RES_SIZE;
+
+    /* Make sure we have what we need for a good BO mock */
+    KUNIT_ASSERT_NOT_NULL(test, priv->devs->ttm_dev);
+
+    priv->bo = ttm_bo_kunit_init(test, priv->devs, size);
+    priv->place = ttm_place_kunit_init(test, mem_type, flags, size);
+}
+
+static void ttm_init_test_manager(struct kunit *test,
+                  struct ttm_resource_test_priv *priv,
+                  uint32_t mem_type)
+{
+    struct ttm_device *ttm_dev = priv->devs->ttm_dev;
+    struct ttm_resource_manager *man;
+    size_t size = SZ_16K;
+
+    man = kunit_kzalloc(test, sizeof(*man), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, man);
+
+    man->use_tt = false;
+    man->func = &ttm_resource_manager_mock_funcs;
+
+    ttm_resource_manager_init(man, ttm_dev, size);
+    ttm_set_driver_manager(ttm_dev, mem_type, man);
+    ttm_resource_manager_set_used(man, true);
+}
+
+static const struct ttm_resource_test_case ttm_resource_cases[] = {
+    {
+        .description = "Init resource in TTM_PL_SYSTEM",
+        .mem_type = TTM_PL_SYSTEM,
+    },
+    {
+        .description = "Init resource in TTM_PL_VRAM",
+        .mem_type = TTM_PL_VRAM,
+    },
+    {
+        .description = "Init resource in a private placement",
+        .mem_type = TTM_PRIV_DUMMY_REG,
+    },
+    {
+        .description = "Init resource in TTM_PL_SYSTEM, set 

placement flags",
+        .mem_type = TTM_PL_SYSTEM,
+        .flags = TTM_PL_FLAG_TOPDOWN,
+    },

drivers/gpu/drm/ttm/tests/ttm_resource_test.c new file +3 -0
+};
+
+static void ttm_resource_case_desc(const struct 

ttm_resource_test_case *t, char *desc)

drivers/gpu/drm/ttm/tests/ttm_resource_test.c new file +5 -0
+{
+    strscpy(desc, t->description, KUNIT_PARAM_DESC_SIZE);
+}
+
+KUNIT_ARRAY_PARAM(ttm_resource, ttm_resource_cases, 

ttm_resource_case_desc);

drivers/gpu/drm/ttm/tests/ttm_resource_test.c new file +41 -0
+
+static void ttm_resource_init_basic(struct kunit *test)
+{
+    const struct ttm_resource_test_case *params = test->param_value;
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource *res;
+    struct ttm_buffer_object *bo;
+    struct ttm_place *place;
+    struct ttm_resource_manager *man;
+    uint64_t expected_usage;
+
+    ttm_init_test_mocks(test, priv, params->mem_type, params->flags);
+    bo = priv->bo;
+    place = priv->place;
+
+    if (params->mem_type > TTM_PL_SYSTEM)
+        ttm_init_test_manager(test, priv, params->mem_type);
+
+    res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, res);
+
+    man = ttm_manager_type(priv->devs->ttm_dev, place->mem_type);
+    expected_usage = man->usage + RES_SIZE;
+
+    KUNIT_ASSERT_TRUE(test, list_empty(&man->lru[bo->priority]));
+
+    ttm_resource_init(bo, place, res);
+
+    KUNIT_ASSERT_EQ(test, res->start, 0);
+    KUNIT_ASSERT_EQ(test, res->size, RES_SIZE);
+    KUNIT_ASSERT_EQ(test, res->mem_type, place->mem_type);
+    KUNIT_ASSERT_EQ(test, res->placement, place->flags);
+    KUNIT_ASSERT_PTR_EQ(test, res->bo, bo);
+
+    KUNIT_ASSERT_NULL(test, res->bus.addr);
+    KUNIT_ASSERT_EQ(test, res->bus.offset, 0);
+    KUNIT_ASSERT_FALSE(test, res->bus.is_iomem);
+    KUNIT_ASSERT_EQ(test, res->bus.caching, ttm_cached);
+    KUNIT_ASSERT_EQ(test, man->usage, expected_usage);
+
+    KUNIT_ASSERT_TRUE(test, 

list_is_singular(&man->lru[bo->priority]));

drivers/gpu/drm/ttm/tests/ttm_resource_test.c new file +89 -0
+
+    ttm_resource_fini(man, res);
+}
+
+static void ttm_resource_init_pinned(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource *res;
+    struct ttm_buffer_object *bo;
+    struct ttm_place *place;
+    struct ttm_resource_manager *man;
+
+    ttm_init_test_mocks(test, priv, TTM_PL_SYSTEM, 0);
+    bo = priv->bo;
+    place = priv->place;
+
+    man = ttm_manager_type(priv->devs->ttm_dev, place->mem_type);
+
+    res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, res);
+    KUNIT_ASSERT_TRUE(test, list_empty(&bo->bdev->pinned));
+
+    dma_resv_lock(bo->base.resv, NULL);
+    ttm_bo_pin(bo);
+    ttm_resource_init(bo, place, res);
+    KUNIT_ASSERT_TRUE(test, list_is_singular(&bo->bdev->pinned));
+
+    ttm_bo_unpin(bo);
+    ttm_resource_fini(man, res);
+    dma_resv_unlock(bo->base.resv);
+
+    KUNIT_ASSERT_TRUE(test, list_empty(&bo->bdev->pinned));
+}
+
+static void ttm_resource_fini_basic(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource *res;
+    struct ttm_buffer_object *bo;
+    struct ttm_place *place;
+    struct ttm_resource_manager *man;
+
+    ttm_init_test_mocks(test, priv, TTM_PL_SYSTEM, 0);
+    bo = priv->bo;
+    place = priv->place;
+
+    man = ttm_manager_type(priv->devs->ttm_dev, place->mem_type);
+
+    res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, res);
+
+    ttm_resource_init(bo, place, res);
+    ttm_resource_fini(man, res);
+
+    KUNIT_ASSERT_TRUE(test, list_empty(&res->lru));
+    KUNIT_ASSERT_EQ(test, man->usage, 0);
+}
+
+static void ttm_resource_manager_init_basic(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource_manager *man;
+    size_t size = SZ_16K;
+
+    man = kunit_kzalloc(test, sizeof(*man), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, man);
+
+    ttm_resource_manager_init(man, priv->devs->ttm_dev, size);
+
+    KUNIT_ASSERT_PTR_EQ(test, man->bdev, priv->devs->ttm_dev);
+    KUNIT_ASSERT_EQ(test, man->size, size);
+    KUNIT_ASSERT_EQ(test, man->usage, 0);
+    KUNIT_ASSERT_NULL(test, man->move);
+    KUNIT_ASSERT_NOT_NULL(test, &man->move_lock);
+
+    for (int i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
+        KUNIT_ASSERT_TRUE(test, list_empty(&man->lru[i]));
+}
+
+static void ttm_resource_manager_usage_basic(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource *res;
+    struct ttm_buffer_object *bo;
+    struct ttm_place *place;
+    struct ttm_resource_manager *man;
+    uint64_t actual_usage;
+
+    ttm_init_test_mocks(test, priv, TTM_PL_SYSTEM, 

TTM_PL_FLAG_TOPDOWN);
+    bo = priv->bo;
+    place = priv->place;

drivers/gpu/drm/ttm/tests/ttm_resource_test.c new file +78 -0
+
+    res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, res);
+
+    man = ttm_manager_type(priv->devs->ttm_dev, place->mem_type);
+
+    ttm_resource_init(bo, place, res);
+    actual_usage = ttm_resource_manager_usage(man);
+
+    KUNIT_ASSERT_EQ(test, actual_usage, RES_SIZE);
+
+    ttm_resource_fini(man, res);
+}
+
+static void ttm_resource_manager_set_used_basic(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource_manager *man;
+
+    man = ttm_manager_type(priv->devs->ttm_dev, TTM_PL_SYSTEM);
+    KUNIT_ASSERT_TRUE(test, man->use_type);
+
+    ttm_resource_manager_set_used(man, false);
+    KUNIT_ASSERT_FALSE(test, man->use_type);
+}
+
+static void ttm_sys_man_alloc_basic(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource_manager *man;
+    struct ttm_buffer_object *bo;
+    struct ttm_place *place;
+    struct ttm_resource *res;
+    uint32_t mem_type = TTM_PL_SYSTEM;
+    int ret;
+
+    ttm_init_test_mocks(test, priv, mem_type, 0);
+    bo = priv->bo;
+    place = priv->place;
+
+    man = ttm_manager_type(priv->devs->ttm_dev, mem_type);
+    ret = man->func->alloc(man, bo, place, &res);
+
+    KUNIT_ASSERT_EQ(test, ret, 0);
+    KUNIT_ASSERT_EQ(test, res->size, RES_SIZE);
+    KUNIT_ASSERT_EQ(test, res->mem_type, mem_type);
+    KUNIT_ASSERT_PTR_EQ(test, res->bo, bo);
+
+    ttm_resource_fini(man, res);
+}
+
+static void ttm_sys_man_free_basic(struct kunit *test)
+{
+    struct ttm_resource_test_priv *priv = test->priv;
+    struct ttm_resource_manager *man;
+    struct ttm_buffer_object *bo;
+    struct ttm_place *place;
+    struct ttm_resource *res;
+    uint32_t mem_type = TTM_PL_SYSTEM;
+
+    ttm_init_test_mocks(test, priv, mem_type, 0);
+    bo = priv->bo;
+    place = priv->place;
+
+    res = kunit_kzalloc(test, sizeof(*res), GFP_KERNEL);
+    KUNIT_ASSERT_NOT_NULL(test, res);
+
+    ttm_resource_alloc(bo, place, &res);
+
+    man = ttm_manager_type(priv->devs->ttm_dev, mem_type);
+    man->func->free(man, res);
+
+    KUNIT_ASSERT_TRUE(test, list_empty(&man->lru[bo->priority]));
+    KUNIT_ASSERT_EQ(test, man->usage, 0);
+}
+
+static struct kunit_case ttm_resource_test_cases[] = {
+    KUNIT_CASE_PARAM(ttm_resource_init_basic, 

ttm_resource_gen_params),
+    KUNIT_CASE(ttm_resource_init_pinned),
+    KUNIT_CASE(ttm_resource_fini_basic),
+    KUNIT_CASE(ttm_resource_manager_init_basic), +    KUNIT_CASE(ttm_resource_manager_usage_basic), +    KUNIT_CASE(ttm_resource_manager_set_used_basic), +    KUNIT_CASE(ttm_sys_man_alloc_basic),
+    KUNIT_CASE(ttm_sys_man_free_basic),
+    {}

drivers/gpu/drm/ttm/tests/ttm_resource_test.c new file +12 -0
+};
+
+static struct kunit_suite ttm_resource_test_suite = {
+    .name = "ttm_resource",
+    .init = ttm_resource_test_init,
+    .exit = ttm_resource_test_fini,
+    .test_cases = ttm_resource_test_cases,
+};
+
+kunit_test_suites(&ttm_resource_test_suite);
+
+MODULE_LICENSE("GPL");
drivers/gpu/drm/ttm/ttm_resource.c +3 -0
index 46ff9c75bb12..02b96d23fdb9 100644
@@ -30,6 +30,8 @@
   #include <drm/ttm/ttm_placement.h>
   #include <drm/ttm/ttm_resource.h>
+#include <drm/drm_util.h>
+
   /**
    * ttm_lru_bulk_move_init - initialize a bulk move structure
    * @bulk: the structure to init
@@ -240,6 +242,7 @@ int ttm_resource_alloc(struct ttm_buffer_object 
 *bo,
       spin_unlock(&bo->bdev->lru_lock);
       return 0;
   }
+EXPORT_SYMBOL_FOR_TESTS_ONLY(ttm_resource_alloc);
   void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res)
   {