Skip to contentSkip to conversation

Improve test coverage of TTM

PATCH Superseded Karolina Stolarek 3 participants 16 messages Oct 16, 2023 – Nov 7, 2023

Subsystems (3)

v5 of 14 Superseded Latest is v14 →

lore
Share & tools
lore
#0 lore raw

v5 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

v5:
- Actually use the page_flags parameter in ttm_tt_simple_create()

v4:
- First unreserve the object before calling ww_acquire_fini() in ttm_bo_reserve_double_resv subtest
- Silence lockdep in ttm_bo_reserve_deadlock subtest (open to suggestions how to fix it in a different way)
- Use a genuine GEM object in ttm_buffer_object instead of an empty one

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       | 619 ++++++++++++++++++
 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c |  51 +-
 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       | 295 +++++++++
 drivers/gpu/drm/ttm/ttm_resource.c            |   3 +
 drivers/gpu/drm/ttm/ttm_tt.c                  |   3 +
 9 files changed, 1313 insertions(+), 3 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 v5 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 a genuine GEM drm object. Export ttm_resource_alloc for test purposes only.

---
 drivers/gpu/drm/ttm/tests/Makefile            |   1 +
 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c |  25 +-
 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, 367 insertions(+), 1 deletion(-)
 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 +24 -1
index 81661d8827aa..c605f010ea08 100644
@@ -29,19 +29,42 @@ 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 drm_gem_object gem_obj = { };
 	struct ttm_buffer_object *bo;
+	int err;
 
 	bo = kunit_kzalloc(test, sizeof(*bo), GFP_KERNEL);
 	KUNIT_ASSERT_NOT_NULL(test, bo);
 
 	bo->base = gem_obj;
+	err = drm_gem_object_init(devs->drm, &bo->base, size);
+	KUNIT_ASSERT_EQ(test, err, 0);
+
 	bo->bdev = devs->ttm_dev;
+	kref_init(&bo->kref);
 
 	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
#6 lore raw

PATCH v5 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       | 619 ++++++++++++++++++
 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c |   6 +
 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 +619 -0

619 lines not on this page · raw message

drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c +6 -0
index 5aa9d1d2fca3..ef4822c70626 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,
@@ -61,6 +65,8 @@ struct ttm_buffer_object *ttm_bo_kunit_init(struct kunit *test,
 	KUNIT_ASSERT_EQ(test, err, 0);
 
 	bo->bdev = devs->ttm_dev;
+	bo->destroy = dummy_ttm_bo_destroy;
+
 	kref_init(&bo->kref);
 
 	return bo;
Signature · 1 line
2.25.1
#4 lore raw

PATCH v5 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 testing 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       | 295 ++++++++++++++++++
 drivers/gpu/drm/ttm/ttm_tt.c                  |   3 +
 4 files changed, 319 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 c605f010ea08..5aa9d1d2fca3 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, page_flags, 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 +295 -0

295 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
#7 lore raw

PATCH v5 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
#8 lore raw
In reply to Karolina Stolarek
Quoted 14 lines · Karolina Stolarek

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.

Quoted 6 lines

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.

Tested [PATCH v4 0/4] Improve test coverage of TTM patches on AMD platform, looks good.

Regards,
S.Amarnath

Quoted 49 lines

Many thanks,
Karolina

v5:
- Actually use the page_flags parameter in ttm_tt_simple_create()

v4:
- First unreserve the object before calling ww_acquire_fini() in ttm_bo_reserve_double_resv subtest
- Silence lockdep in ttm_bo_reserve_deadlock subtest (open to suggestions how to fix it in a different way)
- Use a genuine GEM object in ttm_buffer_object instead of an empty one

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       | 619 ++++++++++++++++++
  drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c |  51 +-
  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       | 295 +++++++++
  drivers/gpu/drm/ttm/ttm_resource.c            |   3 +
  drivers/gpu/drm/ttm/ttm_tt.c                  |   3 +
  9 files changed, 1313 insertions(+), 3 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

#9 lore raw

Hi Amaranath,

Quoted 16 lines · Amaranath
Quoted 14 lines · Karolina Stolarek

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.

Quoted 8 lines
Quoted 7 lines

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.

Tested [PATCH v4 0/4] Improve test coverage of TTM patches on AMD platform, looks good.

Many thanks for taking a look.

Do I understand correctly that your Tested-by would apply to all the patches here?

All the best,
Karolina

Quoted 52 lines

Regards,
S.Amarnath

Quoted 49 lines

Many thanks,
Karolina

v5:
   - Actually use the page_flags parameter in ttm_tt_simple_create()

v4:
   - First unreserve the object before calling ww_acquire_fini() in ttm_bo_reserve_double_resv subtest
   - Silence lockdep in ttm_bo_reserve_deadlock subtest (open to suggestions how to fix it in a different way)
   - Use a genuine GEM object in ttm_buffer_object instead of an empty one

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       | 619 ++++++++++++++++++ drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c |  51 +- 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       | 295 +++++++++ drivers/gpu/drm/ttm/ttm_resource.c            |   3 + drivers/gpu/drm/ttm/ttm_tt.c                  |   3 +

  9 files changed, 1313 insertions(+), 3 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

#10 lore raw
Quoted 24 lines · Karolina Stolarek

Hi Amaranath,

Quoted 21 lines · Amaranath
Quoted 19 lines · Karolina Stolarek

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
Quoted 8 lines
Quoted 3 lines
Quoted 2 lines

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

Tested [PATCH v4 0/4] Improve test coverage of TTM patches on AMD platform, looks good.

Many thanks for taking a look.

Do I understand correctly that your Tested-by would apply to all the patches here?

Yes, I have tested all the 4 patches together.

Regards,
S.Amarnath

Quoted 56 lines

All the best,
Karolina

Quoted 53 lines

Regards,
S.Amarnath

Quoted 50 lines

Many thanks,
Karolina

v5:
   - Actually use the page_flags parameter in ttm_tt_simple_create()

v4:
   - First unreserve the object before calling ww_acquire_fini() in ttm_bo_reserve_double_resv subtest
   - Silence lockdep in ttm_bo_reserve_deadlock subtest (open to suggestions how to fix it in a different way)
   - Use a genuine GEM object in ttm_buffer_object instead of an empty one

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       | 619
++++++++++++++++++
  drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c |  51 +- 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       | 295 +++++++++ drivers/gpu/drm/ttm/ttm_resource.c            |   3 + drivers/gpu/drm/ttm/ttm_tt.c                  |   3 +

  9 files changed, 1313 insertions(+), 3 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

#11 lore raw

Am 17.10.23 um 13:22 schrieb Somalapuram, Amaranath:

Quoted 28 lines · Karolina Stolarek
Quoted 26 lines

Hi Amaranath,

Quoted 23 lines · Amaranath
Quoted 21 lines · Karolina Stolarek

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.

Quoted 7 lines
Quoted 6 lines
Quoted 1 line

Tested [PATCH v4 0/4] Improve test coverage of TTM patches on AMD platform, looks good.

Many thanks for taking a look.

Do I understand correctly that your Tested-by would apply to all the patches here?

Yes, I have tested all the 4 patches together.

No time for an in deep review, but I'm going to pick this up and push it to drm-misc-next.

Thanks,
Christian.

Quoted 59 lines

Regards,
S.Amarnath

Quoted 56 lines

All the best,
Karolina

Quoted 53 lines

Regards,
S.Amarnath

Quoted 50 lines

Many thanks,
Karolina

v5:
   - Actually use the page_flags parameter in ttm_tt_simple_create()

v4:
   - First unreserve the object before calling ww_acquire_fini() in ttm_bo_reserve_double_resv subtest
   - Silence lockdep in ttm_bo_reserve_deadlock subtest (open to suggestions how to fix it in a different way)
   - Use a genuine GEM object in ttm_buffer_object instead of an empty one

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       | 619
++++++++++++++++++
  drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c |  51 +- 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       | 295 +++++++++ drivers/gpu/drm/ttm/ttm_resource.c            |   3 + drivers/gpu/drm/ttm/ttm_tt.c                  |   3 +

  9 files changed, 1313 insertions(+), 3 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

#12 lore raw
Quoted 33 lines · Christian König

Am 17.10.23 um 13:22 schrieb Somalapuram, Amaranath:

Quoted 30 lines · Karolina Stolarek
Quoted 28 lines

Hi Amaranath,

Quoted 24 lines · Amaranath
Quoted 14 lines · Karolina Stolarek

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.

Quoted 7 lines

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.

Tested [PATCH v4 0/4] Improve test coverage of TTM patches on AMD platform, looks good.

Quoted 7 lines
Quoted 5 lines
Quoted 4 lines

Many thanks for taking a look.

Do I understand correctly that your Tested-by would apply to all the patches here?

Yes, I have tested all the 4 patches together.

No time for an in deep review, but I'm going to pick this up and push it to drm-misc-next.

Oh! Could you at least take a look at ttm_bo_reserve_deadlock and/or interrupted subtests? I'm not 100% sure if my solution is right.

Many thanks,
Karolina

Quoted 64 lines

Thanks,
Christian.

Quoted 59 lines

Regards,
S.Amarnath

Quoted 56 lines

All the best,
Karolina

Quoted 53 lines

Regards,
S.Amarnath

Quoted 50 lines

Many thanks,
Karolina

v5:
   - Actually use the page_flags parameter in ttm_tt_simple_create()

v4:
   - First unreserve the object before calling ww_acquire_fini() in ttm_bo_reserve_double_resv subtest
   - Silence lockdep in ttm_bo_reserve_deadlock subtest (open to suggestions how to fix it in a different way)
   - Use a genuine GEM object in ttm_buffer_object instead of an empty one

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       | 619
++++++++++++++++++
  drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c |  51 +- 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       | 295 +++++++++ drivers/gpu/drm/ttm/ttm_resource.c            |   3 + drivers/gpu/drm/ttm/ttm_tt.c                  |   3 +

  9 files changed, 1313 insertions(+), 3 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

#13 lore raw

Am 17.10.23 um 14:06 schrieb Karolina Stolarek:

Quoted 36 lines · Christian König
Quoted 35 lines

Am 17.10.23 um 13:22 schrieb Somalapuram, Amaranath:

Quoted 32 lines · Karolina Stolarek
Quoted 30 lines

Hi Amaranath,

Quoted 16 lines · Amaranath
Quoted 14 lines · Karolina Stolarek

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.

Quoted 8 lines
Quoted 7 lines

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.

Tested [PATCH v4 0/4] Improve test coverage of TTM patches on AMD platform, looks good.

Many thanks for taking a look.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 2 lines

Do I understand correctly that your Tested-by would apply to all the patches here?

Yes, I have tested all the 4 patches together.

No time for an in deep review, but I'm going to pick this up and push it to drm-misc-next.

Oh! Could you at least take a look at ttm_bo_reserve_deadlock and/or interrupted subtests? I'm not 100% sure if my solution is right.

Than this will have to wait till next week when I have some more time.

Christian.

Quoted 68 lines

Many thanks,
Karolina

Quoted 64 lines

Thanks,
Christian.

Quoted 59 lines

Regards,
S.Amarnath

Quoted 56 lines

All the best,
Karolina

Quoted 53 lines

Regards,
S.Amarnath

Quoted 50 lines

Many thanks,
Karolina

v5:
   - Actually use the page_flags parameter in ttm_tt_simple_create()

v4:
   - First unreserve the object before calling ww_acquire_fini() in ttm_bo_reserve_double_resv subtest
   - Silence lockdep in ttm_bo_reserve_deadlock subtest (open to suggestions how to fix it in a different way)
   - Use a genuine GEM object in ttm_buffer_object instead of an empty one

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       | 619
++++++++++++++++++
  drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c |  51 +- 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       | 295 +++++++++ drivers/gpu/drm/ttm/ttm_resource.c            |   3 + drivers/gpu/drm/ttm/ttm_tt.c                  |   3 +

  9 files changed, 1313 insertions(+), 3 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

#14 lore raw
Quoted 39 lines · Christian König

Am 17.10.23 um 14:06 schrieb Karolina Stolarek:

Quoted 38 lines · Christian König
Quoted 37 lines

Am 17.10.23 um 13:22 schrieb Somalapuram, Amaranath:

Quoted 34 lines · Karolina Stolarek
Quoted 32 lines

Hi Amaranath,

Quoted 16 lines · Amaranath
Quoted 14 lines · Karolina Stolarek

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.

Quoted 8 lines
Quoted 7 lines

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.

Tested [PATCH v4 0/4] Improve test coverage of TTM patches on AMD platform, looks good.

Many thanks for taking a look.

Do I understand correctly that your Tested-by would apply to all the patches here?

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Yes, I have tested all the 4 patches together.

No time for an in deep review, but I'm going to pick this up and push it to drm-misc-next.

Oh! Could you at least take a look at ttm_bo_reserve_deadlock and/or interrupted subtests? I'm not 100% sure if my solution is right.

Than this will have to wait till next week when I have some more time.

That's fine, I think. I still have to tie up loose ends in my ttm_bo_validate testing (with no eviction cases), so this will keep me busy for a couple more days.

All the best,
Karolina

Quoted 72 lines

Christian.

Quoted 68 lines

Many thanks,
Karolina

Quoted 64 lines

Thanks,
Christian.

Quoted 59 lines

Regards,
S.Amarnath

Quoted 56 lines

All the best,
Karolina

Quoted 53 lines

Regards,
S.Amarnath

Quoted 50 lines

Many thanks,
Karolina

v5:
   - Actually use the page_flags parameter in ttm_tt_simple_create()

v4:
   - First unreserve the object before calling ww_acquire_fini() in ttm_bo_reserve_double_resv subtest
   - Silence lockdep in ttm_bo_reserve_deadlock subtest (open to suggestions how to fix it in a different way)
   - Use a genuine GEM object in ttm_buffer_object instead of an empty one

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       | 619
++++++++++++++++++
  drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c |  51 +- 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       | 295 +++++++++ drivers/gpu/drm/ttm/ttm_resource.c            |   3 + drivers/gpu/drm/ttm/ttm_tt.c                  |   3 +

  9 files changed, 1313 insertions(+), 3 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

#15 lore raw
In reply to Christian König

Hi Christian,

Quoted 4 lines · Christian König

Am 17.10.23 um 14:06 schrieb Karolina Stolarek: >> Oh! Could you at least take a look at ttm_bo_reserve_deadlock and/or

Quoted 1 line

interrupted subtests? I'm not 100% sure if my solution is right.

Than this will have to wait till next week when I have some more time.

Would you have time this week/early next week to take a look at this?

I'm still working on the eviction test cases, but I was thinking about
splitting up my work and sending simpler test cases soon. I could send the new patches together with these in one series, but I think it would be easier to first merge this one and then follow with new patches.

All the best,
Karolina

#2 lore raw

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

In reply to Karolina Stolarek

Am 16.10.23 um 10:52 schrieb Karolina Stolarek:

Quoted 56 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 a genuine GEM drm object. Export ttm_resource_alloc for test purposes only.

---
  drivers/gpu/drm/ttm/tests/Makefile            |   1 +
  drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c |  25 +-
  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, 367 insertions(+), 1 deletion(-)

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 +9 -1
index 81661d8827aa..c605f010ea08 100644
@@ -29,19 +29,42 @@ 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 drm_gem_object gem_obj = { };
  	struct ttm_buffer_object *bo;
+	int err;
 
  	bo = kunit_kzalloc(test, sizeof(*bo), GFP_KERNEL);
  	KUNIT_ASSERT_NOT_NULL(test, bo);
 
  	bo->base = gem_obj;
+	err = drm_gem_object_init(devs->drm, &bo->base, size);
+	KUNIT_ASSERT_EQ(test, err, 0);
+
  	bo->bdev = devs->ttm_dev;
+	kref_init(&bo->kref);
 
  	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)

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

Quoted 10 lines
drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c +10 -0
+{
+	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);

What should that be good for here? Just to test fpfn and lpfn functionality?

If yes then I think that would be better in the test case and not the helper.

Apart from that looks good of hand.

Regards,
Christian.

Quoted 392 lines
drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c +5 -0
+
+	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 v5 1/4] drm/ttm/tests: Add tests for ttm_resource and ttm_sys_man

Quoted 41 lines · Christian König

Am 16.10.23 um 10:52 schrieb Karolina Stolarek:

Quoted 27 lines
drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c +9 -1
index 81661d8827aa..c605f010ea08 100644
@@ -29,19 +29,42 @@ 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 drm_gem_object gem_obj = { };
       struct ttm_buffer_object *bo;
+    int err;
       bo = kunit_kzalloc(test, sizeof(*bo), GFP_KERNEL);
       KUNIT_ASSERT_NOT_NULL(test, bo);
       bo->base = gem_obj;
+    err = drm_gem_object_init(devs->drm, &bo->base, size);
+    KUNIT_ASSERT_EQ(test, err, 0);
+
       bo->bdev = devs->ttm_dev;
+    kref_init(&bo->kref);
       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)

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

Quoted 10 lines
drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c +10 -0
+{
+    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);

What should that be good for here? Just to test fpfn and lpfn functionality?

Quoted 1 line

If yes then I think that would be better in the test case and not the helper.

OK, I'll move it to the test. And yes, that was my intention.

Quoted 1 line

Apart from that looks good of hand.

Thanks for taking a look!

All the best,
Karolina

#5 lore raw

Re: [PATCH v5 2/4] drm/ttm/tests: Add tests for ttm_tt

In reply to Karolina Stolarek

Am 16.10.23 um 10:52 schrieb Karolina Stolarek:

Quoted 4 lines

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

At least of hand I can't see anything obviously wrong, but I also haven't spend to much time on reading though this.

Feel free to add Reviewed-by: Christian König <[email protected]>

Regards,
Christian.

Quoted 381 lines
---
  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       | 295 ++++++++++++++++++
  drivers/gpu/drm/ttm/ttm_tt.c                  |   3 +
  4 files changed, 319 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 c605f010ea08..5aa9d1d2fca3 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, page_flags, 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 +295 -0
new file mode 100644
index 000000000000..4f5fc4d460b4
@@ -0,0 +1,295 @@
+// SPDX-License-Identifier: GPL-2.0 AND MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+#include <linux/shmem_fs.h>
+#include <drm/ttm/ttm_tt.h>
+
+#include "ttm_kunit_helpers.h"
+
+#define BO_SIZE		SZ_4K
+
+struct ttm_tt_test_case {
+	const char *description;
+	uint32_t size;
+	uint32_t extra_pages_num;
+};
+
+static int ttm_tt_test_init(struct kunit *test)
+{
+	struct ttm_test_devices *priv;
+
+	priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, priv);
+
+	priv = ttm_test_devices_all(test);
+	test->priv = priv;
+
+	return 0;
+}
+
+static const struct ttm_tt_test_case ttm_tt_init_basic_cases[] = {
+	{
+		.description = "Page-aligned size",
+		.size = SZ_4K,
+	},
+	{
+		.description = "Extra pages requested",
+		.size = SZ_4K,
+		.extra_pages_num = 1,
+	},
+};
+
+static void ttm_tt_init_case_desc(const struct ttm_tt_test_case *t,
+				  char *desc)
+{
+	strscpy(desc, t->description, KUNIT_PARAM_DESC_SIZE);
+}
+
+KUNIT_ARRAY_PARAM(ttm_tt_init_basic, ttm_tt_init_basic_cases,
+		  ttm_tt_init_case_desc);
+
+static void ttm_tt_init_basic(struct kunit *test)
+{
+	const struct ttm_tt_test_case *params = test->param_value;
+	struct ttm_buffer_object *bo;
+	struct ttm_tt *tt;
+	uint32_t page_flags = TTM_TT_FLAG_ZERO_ALLOC;
+	enum ttm_caching caching = ttm_cached;
+	uint32_t extra_pages = params->extra_pages_num;
+	int num_pages = params->size >> PAGE_SHIFT;
+	int err;
+
+	tt = kunit_kzalloc(test, sizeof(*tt), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, tt);
+
+	bo = ttm_bo_kunit_init(test, test->priv, params->size);
+
+	err = ttm_tt_init(tt, bo, page_flags, caching, extra_pages);
+	KUNIT_ASSERT_EQ(test, err, 0);
+
+	KUNIT_ASSERT_EQ(test, tt->num_pages, num_pages + extra_pages);
+
+	KUNIT_ASSERT_EQ(test, tt->page_flags, page_flags);
+	KUNIT_ASSERT_EQ(test, tt->caching, caching);
+
+	KUNIT_ASSERT_NULL(test, tt->dma_address);
+	KUNIT_ASSERT_NULL(test, tt->swap_storage);
+}
+
+static void ttm_tt_init_misaligned(struct kunit *test)
+{
+	struct ttm_buffer_object *bo;
+	struct ttm_tt *tt;
+	enum ttm_caching caching = ttm_cached;
+	uint32_t size = SZ_8K;
+	int num_pages = (size + SZ_4K) >> PAGE_SHIFT;
+	int err;
+
+	tt = kunit_kzalloc(test, sizeof(*tt), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, tt);
+
+	bo = ttm_bo_kunit_init(test, test->priv, size);
+
+	/* Make the object size misaligned */
+	bo->base.size += 1;
+
+	err = ttm_tt_init(tt, bo, 0, caching, 0);
+	KUNIT_ASSERT_EQ(test, err, 0);
+
+	KUNIT_ASSERT_EQ(test, tt->num_pages, num_pages);
+}
+
+static void ttm_tt_fini_basic(struct kunit *test)
+{
+	struct ttm_buffer_object *bo;
+	struct ttm_tt *tt;
+	enum ttm_caching caching = ttm_cached;
+	int err;
+
+	tt = kunit_kzalloc(test, sizeof(*tt), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, tt);
+
+	bo = ttm_bo_kunit_init(test, test->priv, BO_SIZE);
+
+	err = ttm_tt_init(tt, bo, 0, caching, 0);
+	KUNIT_ASSERT_EQ(test, err, 0);
+	KUNIT_ASSERT_NOT_NULL(test, tt->pages);
+
+	ttm_tt_fini(tt);
+	KUNIT_ASSERT_NULL(test, tt->pages);
+}
+
+static void ttm_tt_fini_sg(struct kunit *test)
+{
+	struct ttm_buffer_object *bo;
+	struct ttm_tt *tt;
+	enum ttm_caching caching = ttm_cached;
+	int err;
+
+	tt = kunit_kzalloc(test, sizeof(*tt), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, tt);
+
+	bo = ttm_bo_kunit_init(test, test->priv, BO_SIZE);
+
+	err = ttm_sg_tt_init(tt, bo, 0, caching);
+	KUNIT_ASSERT_EQ(test, err, 0);
+	KUNIT_ASSERT_NOT_NULL(test, tt->dma_address);
+
+	ttm_tt_fini(tt);
+	KUNIT_ASSERT_NULL(test, tt->dma_address);
+}
+
+static void ttm_tt_fini_shmem(struct kunit *test)
+{
+	struct ttm_buffer_object *bo;
+	struct ttm_tt *tt;
+	struct file *shmem;
+	enum ttm_caching caching = ttm_cached;
+	int err;
+
+	tt = kunit_kzalloc(test, sizeof(*tt), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, tt);
+
+	bo = ttm_bo_kunit_init(test, test->priv, BO_SIZE);
+
+	err = ttm_tt_init(tt, bo, 0, caching, 0);
+	KUNIT_ASSERT_EQ(test, err, 0);
+
+	shmem = shmem_file_setup("ttm swap", BO_SIZE, 0);
+	tt->swap_storage = shmem;
+
+	ttm_tt_fini(tt);
+	KUNIT_ASSERT_NULL(test, tt->swap_storage);
+}
+
+static void ttm_tt_create_basic(struct kunit *test)
+{
+	struct ttm_buffer_object *bo;
+	int err;
+
+	bo = ttm_bo_kunit_init(test, test->priv, BO_SIZE);
+	bo->type = ttm_bo_type_device;
+
+	dma_resv_lock(bo->base.resv, NULL);
+	err = ttm_tt_create(bo, false);
+	dma_resv_unlock(bo->base.resv);
+
+	KUNIT_EXPECT_EQ(test, err, 0);
+	KUNIT_EXPECT_NOT_NULL(test, bo->ttm);
+
+	/* Free manually, as it was allocated outside of KUnit */
+	kfree(bo->ttm);
+}
+
+static void ttm_tt_create_invalid_bo_type(struct kunit *test)
+{
+	struct ttm_buffer_object *bo;
+	int err;
+
+	bo = ttm_bo_kunit_init(test, test->priv, BO_SIZE);
+	bo->type = ttm_bo_type_sg + 1;
+
+	dma_resv_lock(bo->base.resv, NULL);
+	err = ttm_tt_create(bo, false);
+	dma_resv_unlock(bo->base.resv);
+
+	KUNIT_EXPECT_EQ(test, err, -EINVAL);
+	KUNIT_EXPECT_NULL(test, bo->ttm);
+}
+
+static void ttm_tt_create_ttm_exists(struct kunit *test)
+{
+	struct ttm_buffer_object *bo;
+	struct ttm_tt *tt;
+	enum ttm_caching caching = ttm_cached;
+	int err;
+
+	tt = kunit_kzalloc(test, sizeof(*tt), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, tt);
+
+	bo = ttm_bo_kunit_init(test, test->priv, BO_SIZE);
+
+	err = ttm_tt_init(tt, bo, 0, caching, 0);
+	KUNIT_ASSERT_EQ(test, err, 0);
+	bo->ttm = tt;
+
+	dma_resv_lock(bo->base.resv, NULL);
+	err = ttm_tt_create(bo, false);
+	dma_resv_unlock(bo->base.resv);
+
+	/* Expect to keep the previous TTM */
+	KUNIT_ASSERT_EQ(test, err, 0);
+	KUNIT_ASSERT_PTR_EQ(test, tt, bo->ttm);
+}
+
+static struct ttm_tt *ttm_tt_null_create(struct ttm_buffer_object *bo,
+					 uint32_t page_flags)
+{
+	return NULL;
+}
+
+struct ttm_device_funcs ttm_dev_empty_funcs = {
+	.ttm_tt_create = ttm_tt_null_create,
+};
+
+static void ttm_tt_create_failed(struct kunit *test)
+{
+	const struct ttm_test_devices *devs = test->priv;
+	struct ttm_buffer_object *bo;
+	int err;
+
+	bo = ttm_bo_kunit_init(test, test->priv, BO_SIZE);
+
+	/* Update ttm_device_funcs so we don't alloc ttm_tt */
+	devs->ttm_dev->funcs = &ttm_dev_empty_funcs;
+
+	dma_resv_lock(bo->base.resv, NULL);
+	err = ttm_tt_create(bo, false);
+	dma_resv_unlock(bo->base.resv);
+
+	KUNIT_ASSERT_EQ(test, err, -ENOMEM);
+}
+
+static void ttm_tt_destroy_basic(struct kunit *test)
+{
+	const struct ttm_test_devices *devs = test->priv;
+	struct ttm_buffer_object *bo;
+	int err;
+
+	bo = ttm_bo_kunit_init(test, test->priv, BO_SIZE);
+
+	dma_resv_lock(bo->base.resv, NULL);
+	err = ttm_tt_create(bo, false);
+	dma_resv_unlock(bo->base.resv);
+
+	KUNIT_ASSERT_EQ(test, err, 0);
+	KUNIT_ASSERT_NOT_NULL(test, bo->ttm);
+
+	ttm_tt_destroy(devs->ttm_dev, bo->ttm);
+}
+
+static struct kunit_case ttm_tt_test_cases[] = {
+	KUNIT_CASE_PARAM(ttm_tt_init_basic, ttm_tt_init_basic_gen_params),
+	KUNIT_CASE(ttm_tt_init_misaligned),
+	KUNIT_CASE(ttm_tt_fini_basic),
+	KUNIT_CASE(ttm_tt_fini_sg),
+	KUNIT_CASE(ttm_tt_fini_shmem),
+	KUNIT_CASE(ttm_tt_create_basic),
+	KUNIT_CASE(ttm_tt_create_invalid_bo_type),
+	KUNIT_CASE(ttm_tt_create_ttm_exists),
+	KUNIT_CASE(ttm_tt_create_failed),
+	KUNIT_CASE(ttm_tt_destroy_basic),
+	{}
+};
+
+static struct kunit_suite ttm_tt_test_suite = {
+	.name = "ttm_tt",
+	.init = ttm_tt_test_init,
+	.exit = ttm_test_devices_fini,
+	.test_cases = ttm_tt_test_cases,
+};
+
+kunit_test_suites(&ttm_tt_test_suite);
+
+MODULE_LICENSE("GPL");
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,