Skip to contentSkip to conversation

Improve test coverage of TTM

PATCH Superseded Karolina Stolarek 3 participants 17 messages Nov 29, 2023 – Dec 14, 2023

Subsystems (3)

v8 of 14 Superseded Latest is v14 →

lore
Share & tools
lore
#0 lore raw

v8 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

v8:
- Add Tested-by tags to commits that introduce tests
- Improve the comment for ttm_bo_reserve_deadlock() subtest (Andi)
- Actually clean up the resource when "error_free_blocks" is hit in ttm_mock_manager_alloc(). Without that change, we hit DEBUG_LOCKS_WARN_ON(lock->magic != lock) warning when cleaning up the resource manager because we try clean up an incomplete, orphaned resource. That's not good, and this could bite us back in the future.

v7:
- Drop size argument from ttm_place_kunit_init(), it's no longer needed
- Delete a TODO comment from ttm_bo_validate_tests.c
- First evict BOs before calling ttm_resource_manager_set_used() in ttm_mock_manager_fini()
- Stop calling ttm_resource_manager_cleanup() as a part of the mock manager fini sequence. It frees a move fence that is allocated via KUnit allocator, which gets freed again as a part of the test cleanup
- Set use_tt to true in mock manager and stop passing in the flag for it
- Make ttm_dev_empty_funcs static (drivers/gpu/drm/ttm/tests/ttm_tt_test.c:232:25: sparse: sparse:
symbol 'ttm_dev_empty_funcs' was not declared. Should it be static?)
- Cast bo->base.resv->fences to a generic pointer before it's checked by KUnit (drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c:98:9:
sparse: sparse: incompatible types in comparison expression (different base types))
- Clean up mock managers in ttm_bo_validate_move_fence_not_signaled subtest

v6:
- Include tests for ttm_bo_init_reserved() and ttm_bo_validate(), with a mock resource manager (patches 6-8; no eviction testing)
- Add ttm_test_devices_all_init() helper to also init ttm_device instance
- Remove fpfn and lpfn from ttm_place_kunit_init() helper -- these fields are neither used nor tested

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 (8):
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() drm/ttm/tests: Use an init function from the helpers lib drm/ttm/tests: Test simple BO creation and validation drm/ttm/tests: Add tests with mock resource managers drm/ttm/tests: Add test cases dependent on fence signaling

 drivers/gpu/drm/Kconfig                       |   1 +
 drivers/gpu/drm/ttm/tests/.kunitconfig        |   1 +
 drivers/gpu/drm/ttm/tests/Makefile            |   5 +
 drivers/gpu/drm/ttm/tests/ttm_bo_test.c       | 622 ++++++++++++++
 .../gpu/drm/ttm/tests/ttm_bo_validate_test.c  | 795 ++++++++++++++++++
 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c | 109 ++-
 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h |   7 +
 drivers/gpu/drm/ttm/tests/ttm_mock_manager.c  | 207 +++++
 drivers/gpu/drm/ttm/tests/ttm_mock_manager.h  |  31 +
 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       | 282 +++++++
 drivers/gpu/drm/ttm/ttm_resource.c            |   3 +
 drivers/gpu/drm/ttm/ttm_tt.c                  |   3 +
 14 files changed, 2401 insertions(+), 3 deletions(-)
 create mode 100644 drivers/gpu/drm/ttm/tests/ttm_bo_test.c
 create mode 100644 drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c
 create mode 100644 drivers/gpu/drm/ttm/tests/ttm_mock_manager.c
 create mode 100644 drivers/gpu/drm/ttm/tests/ttm_mock_manager.h
 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.34.1
#1 lore raw

PATCH v8 1/8 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 |  22 +-
 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h |   3 +
 drivers/gpu/drm/ttm/tests/ttm_resource_test.c | 335 ++++++++++++++++++
 drivers/gpu/drm/ttm/ttm_resource.c            |   3 +
 5 files changed, 363 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 +21 -1
index 81661d8827aa..779fbc038f17 100644
@@ -29,19 +29,39 @@ 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)
+{
+	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;
+
+	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 +3 -0
index e261e3660d0b..2f51c833a536 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,8 @@ 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);
 
 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.34.1
#2 lore raw

PATCH v8 2/8 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 779fbc038f17..ba4e5c689164 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.34.1
#3 lore raw

PATCH v8 3/8 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       | 622 ++++++++++++++++++
 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c |   6 +
 3 files changed, 629 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 +622 -0

622 lines not on this page · raw message

drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c +6 -0
index ba4e5c689164..7b7c1fa805fc 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.34.1
#4 lore raw

PATCH v8 4/8 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.34.1
#5 lore raw

PATCH v8 5/8 drm/ttm/tests: Use an init function from the helpers lib

In reply to Karolina Stolarek

Add a new helper function that also initializes the device. Use it in ttm_tt test suite and delete the local definition.

---
 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c | 14 ++++++++++++++
 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h |  1 +
 drivers/gpu/drm/ttm/tests/ttm_tt_test.c       | 15 +--------------
 3 files changed, 16 insertions(+), 14 deletions(-)
drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c +14 -0
index 7b7c1fa805fc..899a54dbe443 100644
@@ -150,6 +150,20 @@ int ttm_test_devices_init(struct kunit *test)
 }
 EXPORT_SYMBOL_GPL(ttm_test_devices_init);
 
+int ttm_test_devices_all_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;
+}
+EXPORT_SYMBOL_GPL(ttm_test_devices_all_init);
+
 void ttm_test_devices_fini(struct kunit *test)
 {
 	ttm_test_devices_put(test, test->priv);
drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h +1 -0
index 2f51c833a536..53bb5834939f 100644
@@ -39,6 +39,7 @@ void ttm_test_devices_put(struct kunit *test, struct ttm_test_devices *devs);
 
 /* Generic init/fini for tests that only need DRM/TTM devices */
 int ttm_test_devices_init(struct kunit *test);
+int ttm_test_devices_all_init(struct kunit *test);
 void ttm_test_devices_fini(struct kunit *test);
 
 #endif // TTM_KUNIT_HELPERS_H
drivers/gpu/drm/ttm/tests/ttm_tt_test.c +1 -14
index fd4502c18de6..a33a426a814d 100644
@@ -15,19 +15,6 @@ struct ttm_tt_test_case {
 	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",
@@ -285,7 +272,7 @@ static struct kunit_case ttm_tt_test_cases[] = {
 
 static struct kunit_suite ttm_tt_test_suite = {
 	.name = "ttm_tt",
-	.init = ttm_tt_test_init,
+	.init = ttm_test_devices_all_init,
 	.exit = ttm_test_devices_fini,
 	.test_cases = ttm_tt_test_cases,
 };
Signature · 1 line
2.34.1
#6 lore raw

PATCH v8 6/8 drm/ttm/tests: Test simple BO creation and validation

In reply to Karolina Stolarek

Add tests for ttm_bo_init_reserved() and ttm_bo_validate() that use sys manager. Define a simple move function in ttm_device_funcs. Expose destroy callback of the buffer object to make testing of ttm_bo_init_reserved() behaviour easier.

---
 drivers/gpu/drm/ttm/tests/Makefile            |   1 +
 .../gpu/drm/ttm/tests/ttm_bo_validate_test.c  | 211 ++++++++++++++++++
 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c |  14 +-
 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h |   1 +
 4 files changed, 226 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c
drivers/gpu/drm/ttm/tests/Makefile +1 -0
index 468535f7eed2..2e5ed63fb414 100644
@@ -6,4 +6,5 @@ obj-$(CONFIG_DRM_TTM_KUNIT_TEST) += \
         ttm_resource_test.o \
         ttm_tt_test.o \
         ttm_bo_test.o \
+        ttm_bo_validate_test.o \
         ttm_kunit_helpers.o
drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c new file +211 -0

211 lines not on this page · raw message

drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c +13 -1
index 899a54dbe443..b07ea760d819 100644
@@ -22,13 +22,19 @@ 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)
+static int mock_move(struct ttm_buffer_object *bo, bool evict,
+		     struct ttm_operation_ctx *ctx,
+		     struct ttm_resource *new_mem,
+		     struct ttm_place *hop)
 {
+	bo->resource = new_mem;
+	return 0;
 }
 
 struct ttm_device_funcs ttm_dev_funcs = {
 	.ttm_tt_create = ttm_tt_simple_create,
 	.ttm_tt_destroy = ttm_tt_simple_destroy,
+	.move = mock_move,
 };
 EXPORT_SYMBOL_GPL(ttm_dev_funcs);
 
@@ -88,6 +94,12 @@ struct ttm_place *ttm_place_kunit_init(struct kunit *test,
 }
 EXPORT_SYMBOL_GPL(ttm_place_kunit_init);
 
+void dummy_ttm_bo_destroy(struct ttm_buffer_object *bo)
+{
+	drm_gem_object_release(&bo->base);
+}
+EXPORT_SYMBOL_GPL(dummy_ttm_bo_destroy);
+
 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 +1 -0
index 53bb5834939f..3e5fe272c54c 100644
@@ -31,6 +31,7 @@ struct ttm_buffer_object *ttm_bo_kunit_init(struct kunit *test,
 					    size_t size);
 struct ttm_place *ttm_place_kunit_init(struct kunit *test,
 				       uint32_t mem_type, uint32_t flags);
+void dummy_ttm_bo_destroy(struct ttm_buffer_object *bo);
 
 struct ttm_test_devices *ttm_test_devices_basic(struct kunit *test);
 struct ttm_test_devices *ttm_test_devices_all(struct kunit *test);
Signature · 1 line
2.34.1
#7 lore raw

PATCH v8 7/8 drm/ttm/tests: Add tests with mock resource managers

In reply to Karolina Stolarek

Add mock resource manager to test ttm_bo_validate() with non-system placements. Update KConfig entry to enable DRM Buddy allocator, used by the mock manager. Update move function to do more than just assign a resource.

---
 drivers/gpu/drm/Kconfig                       |   1 +
 drivers/gpu/drm/ttm/tests/.kunitconfig        |   1 +
 drivers/gpu/drm/ttm/tests/Makefile            |   1 +
 .../gpu/drm/ttm/tests/ttm_bo_validate_test.c  | 276 ++++++++++++++++++
 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c |  39 ++-
 drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h |   2 +
 drivers/gpu/drm/ttm/tests/ttm_mock_manager.c  | 207 +++++++++++++
 drivers/gpu/drm/ttm/tests/ttm_mock_manager.h  |  31 ++
 8 files changed, 556 insertions(+), 2 deletions(-)
 create mode 100644 drivers/gpu/drm/ttm/tests/ttm_mock_manager.c
 create mode 100644 drivers/gpu/drm/ttm/tests/ttm_mock_manager.h
drivers/gpu/drm/Kconfig +1 -0
index 740c1c0bd068..3be7afa6df3e 100644
@@ -200,6 +200,7 @@ config DRM_TTM_KUNIT_TEST
         default n
         depends on DRM && KUNIT && MMU
         select DRM_TTM
+        select DRM_BUDDY
         select DRM_EXPORT_FOR_TESTS if m
         select DRM_KUNIT_TEST_HELPERS
         default KUNIT_ALL_TESTS
drivers/gpu/drm/ttm/tests/.kunitconfig +1 -0
index 75fdce0cd98e..9228ce9b913c 100644
@@ -2,3 +2,4 @@ CONFIG_KUNIT=y
 CONFIG_DRM=y
 CONFIG_DRM_KUNIT_TEST_HELPERS=y
 CONFIG_DRM_TTM_KUNIT_TEST=y
+CONFIG_DRM_BUDDY=y
drivers/gpu/drm/ttm/tests/Makefile +1 -0
index 2e5ed63fb414..f3149de77541 100644
@@ -7,4 +7,5 @@ obj-$(CONFIG_DRM_TTM_KUNIT_TEST) += \
         ttm_tt_test.o \
         ttm_bo_test.o \
         ttm_bo_validate_test.o \
+        ttm_mock_manager.o \
         ttm_kunit_helpers.o
drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c +276 -0

317 lines not on this page · raw message

drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c +37 -2
index b07ea760d819..719c1d56388e 100644
@@ -27,8 +27,43 @@ static int mock_move(struct ttm_buffer_object *bo, bool evict,
 		     struct ttm_resource *new_mem,
 		     struct ttm_place *hop)
 {
-	bo->resource = new_mem;
-	return 0;
+	struct ttm_resource *old_mem = bo->resource;
+	int ret;
+
+	if (!old_mem || (old_mem->mem_type == TTM_PL_SYSTEM && !bo->ttm)) {
+		ttm_bo_move_null(bo, new_mem);
+		return 0;
+	}
+
+	/* Dummy multihop */
+	if (bo->resource->mem_type == TTM_PL_VRAM &&
+	    new_mem->mem_type == TTM_PL_MULTIHOP) {
+		hop->mem_type = TTM_PL_TT;
+		hop->flags = TTM_PL_FLAG_TEMPORARY;
+		hop->fpfn = 0;
+		hop->lpfn = 0;
+		return -EMULTIHOP;
+	}
+
+	if (old_mem->mem_type == TTM_PL_SYSTEM &&
+	    new_mem->mem_type == TTM_PL_TT) {
+		ttm_bo_move_null(bo, new_mem);
+		return 0;
+	}
+
+	if (old_mem->mem_type == TTM_PL_TT &&
+	    new_mem->mem_type == TTM_PL_SYSTEM) {
+		ret = ttm_bo_wait_ctx(bo, ctx);
+
+		if (ret)
+			return ret;
+
+		ttm_resource_free(bo, &bo->resource);
+		ttm_bo_assign_mem(bo, new_mem);
+		return 0;
+	}
+
+	return ttm_bo_move_memcpy(bo, ctx, new_mem);
 }
 
 struct ttm_device_funcs ttm_dev_funcs = {
drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h +2 -0
index 3e5fe272c54c..a8977eb606e5 100644
@@ -13,6 +13,8 @@
 #include <drm/drm_kunit_helpers.h>
 #include <kunit/test.h>
 
+#define TTM_PL_MULTIHOP (TTM_PL_PRIV + 1)
+
 extern struct ttm_device_funcs ttm_dev_funcs;
 
 struct ttm_test_devices {
drivers/gpu/drm/ttm/tests/ttm_mock_manager.c new file +207 -0

207 lines not on this page · raw message

drivers/gpu/drm/ttm/tests/ttm_mock_manager.h new file +31 -0
new file mode 100644
index 000000000000..d2db9de9d876
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0 AND MIT */
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+#ifndef TTM_MOCK_MANAGER_H
+#define TTM_MOCK_MANAGER_H
+
+#include <drm/drm_buddy.h>
+
+struct ttm_mock_manager {
+	struct ttm_resource_manager man;
+	struct drm_buddy mm;
+	uint64_t default_page_size;
+	/* protects allocations of mock buffer objects */
+	struct mutex lock;
+};
+
+struct ttm_mock_resource {
+	struct ttm_resource base;
+	struct list_head blocks;
+	unsigned long flags;
+};
+
+int ttm_mock_manager_init(struct ttm_device *bdev, uint32_t mem_type,
+			  uint32_t size);
+int ttm_bad_manager_init(struct ttm_device *bdev, uint32_t mem_type,
+			 uint32_t size);
+void ttm_mock_manager_fini(struct ttm_device *bdev, uint32_t mem_type);
+void ttm_bad_manager_fini(struct ttm_device *bdev, uint32_t mem_type);
+
+#endif // TTM_MOCK_MANAGER_H
Signature · 1 line
2.34.1
#8 lore raw

PATCH v8 8/8 drm/ttm/tests: Add test cases dependent on fence signaling

In reply to Karolina Stolarek

Add test cases that check how the state of dma fences in BO's reservation object influence the ttm_bo_validation() flow. Do similar tests for resource manager's move fence.

---
 .../gpu/drm/ttm/tests/ttm_bo_validate_test.c  | 308 ++++++++++++++++++
 1 file changed, 308 insertions(+)
drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c +308 -0
index 5f6c24979f83..a659fc837186 100644
@@ -2,6 +2,8 @@
 /*
  * Copyright © 2023 Intel Corporation
  */
+#include <linux/delay.h>
+#include <linux/kthread.h>
 
 #include <drm/ttm/ttm_resource.h>
 #include <drm/ttm/ttm_placement.h>
@@ -13,11 +15,14 @@
 #define BO_SIZE		SZ_4K
 #define MANAGER_SIZE	SZ_1M
 
+static struct spinlock fence_lock;
+
 struct ttm_bo_validate_test_case {
 	const char *description;
 	enum ttm_bo_type bo_type;
 	uint32_t mem_type;
 	bool with_ttm;
+	bool no_gpu_wait;
 };
 
 static struct ttm_placement *ttm_placement_kunit_init(struct kunit *test,
@@ -39,6 +44,43 @@ static struct ttm_placement *ttm_placement_kunit_init(struct kunit *test,
 	return placement;
 }
 
+static const char *fence_name(struct dma_fence *f)
+{
+	return "ttm-bo-validate-fence";
+}
+
+static const struct dma_fence_ops fence_ops = {
+	.get_driver_name = fence_name,
+	.get_timeline_name = fence_name,
+};
+
+static struct dma_fence *alloc_mock_fence(struct kunit *test)
+{
+	struct dma_fence *fence;
+
+	fence = kunit_kzalloc(test, sizeof(*fence), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, fence);
+
+	dma_fence_init(fence, &fence_ops, &fence_lock, 0, 0);
+
+	return fence;
+}
+
+static void dma_resv_kunit_active_fence_init(struct kunit *test,
+					     struct dma_resv *resv,
+					     enum dma_resv_usage usage)
+{
+	struct dma_fence *fence;
+
+	fence = alloc_mock_fence(test);
+	dma_fence_enable_sw_signaling(fence);
+
+	dma_resv_lock(resv, NULL);
+	dma_resv_reserve_fences(resv, 1);
+	dma_resv_add_fence(resv, fence, usage);
+	dma_resv_unlock(resv);
+}
+
 static void ttm_bo_validate_case_desc(const struct ttm_bo_validate_test_case *t,
 				      char *desc)
 {
@@ -460,6 +502,265 @@ static void ttm_bo_validate_multihop(struct kunit *test)
 	ttm_mock_manager_fini(priv->ttm_dev, final_mem);
 }
 
+static const struct ttm_bo_validate_test_case ttm_bo_no_placement_cases[] = {
+	{
+		.description = "Buffer object in system domain, no page vector",
+	},
+	{
+		.description = "Buffer object in system domain with an existing page vector",
+		.with_ttm = true,
+	},
+};
+
+KUNIT_ARRAY_PARAM(ttm_bo_no_placement, ttm_bo_no_placement_cases,
+		  ttm_bo_validate_case_desc);
+
+static void ttm_bo_validate_no_placement_signaled(struct kunit *test)
+{
+	const struct ttm_bo_validate_test_case *params = test->param_value;
+	struct ttm_test_devices *priv = test->priv;
+	struct ttm_buffer_object *bo;
+	struct ttm_place *place;
+	struct ttm_tt *old_tt;
+	struct ttm_placement *placement;
+	struct ttm_resource_manager *man;
+	uint32_t mem_type = TTM_PL_SYSTEM;
+	enum ttm_bo_type bo_type = ttm_bo_type_device;
+	struct ttm_operation_ctx ctx = { };
+	uint32_t size = ALIGN(BO_SIZE, PAGE_SIZE);
+	uint32_t flags;
+	int err;
+
+	place = ttm_place_kunit_init(test, mem_type, 0);
+	man = ttm_manager_type(priv->ttm_dev, mem_type);
+
+	bo = ttm_bo_kunit_init(test, test->priv, size);
+	bo->type = bo_type;
+
+	if (params->with_ttm) {
+		old_tt = priv->ttm_dev->funcs->ttm_tt_create(bo, 0);
+		ttm_pool_alloc(&priv->ttm_dev->pool, old_tt, &ctx);
+		bo->ttm = old_tt;
+	}
+
+	err = ttm_resource_alloc(bo, place, &bo->resource);
+	KUNIT_EXPECT_EQ(test, err, 0);
+	KUNIT_ASSERT_EQ(test, man->usage, size);
+
+	placement = kunit_kzalloc(test, sizeof(*placement), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, placement);
+
+	ttm_bo_reserve(bo, false, false, NULL);
+	err = ttm_bo_validate(bo, placement, &ctx);
+	dma_resv_unlock(bo->base.resv);
+
+	KUNIT_EXPECT_EQ(test, err, 0);
+	KUNIT_ASSERT_EQ(test, man->usage, 0);
+	KUNIT_ASSERT_NOT_NULL(test, bo->ttm);
+	KUNIT_EXPECT_EQ(test, ctx.bytes_moved, 0);
+
+	if (params->with_ttm) {
+		flags = bo->ttm->page_flags;
+
+		KUNIT_ASSERT_PTR_EQ(test, bo->ttm, old_tt);
+		KUNIT_ASSERT_FALSE(test, flags & TTM_TT_FLAG_PRIV_POPULATED);
+		KUNIT_ASSERT_TRUE(test, flags & TTM_TT_FLAG_ZERO_ALLOC);
+	}
+
+	ttm_bo_put(bo);
+}
+
+static int threaded_dma_resv_signal(void *arg)
+{
+	struct ttm_buffer_object *bo = arg;
+	struct dma_resv *resv = bo->base.resv;
+	struct dma_resv_iter cursor;
+	struct dma_fence *fence;
+
+	dma_resv_iter_begin(&cursor, resv, DMA_RESV_USAGE_BOOKKEEP);
+	dma_resv_for_each_fence_unlocked(&cursor, fence) {
+		dma_fence_signal(fence);
+	}
+	dma_resv_iter_end(&cursor);
+
+	return 0;
+}
+
+static void ttm_bo_validate_no_placement_not_signaled(struct kunit *test)
+{
+	const struct ttm_bo_validate_test_case *params = test->param_value;
+	struct ttm_buffer_object *bo;
+	struct ttm_place *place;
+	struct ttm_placement *placement;
+	struct task_struct *task;
+	enum dma_resv_usage usage = DMA_RESV_USAGE_BOOKKEEP;
+	uint32_t mem_type = TTM_PL_SYSTEM;
+	struct ttm_operation_ctx ctx = { };
+	uint32_t size = ALIGN(BO_SIZE, PAGE_SIZE);
+	int err;
+
+	place = ttm_place_kunit_init(test, mem_type, 0);
+
+	bo = ttm_bo_kunit_init(test, test->priv, size);
+	bo->type = params->bo_type;
+
+	err = ttm_resource_alloc(bo, place, &bo->resource);
+	KUNIT_EXPECT_EQ(test, err, 0);
+
+	placement = kunit_kzalloc(test, sizeof(*placement), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, placement);
+
+	/* Create an active fence to simulate a non-idle resv object */
+	spin_lock_init(&fence_lock);
+	dma_resv_kunit_active_fence_init(test, bo->base.resv, usage);
+
+	task = kthread_create(threaded_dma_resv_signal, bo, "dma-resv-signal");
+	if (IS_ERR(task))
+		KUNIT_FAIL(test, "Couldn't create dma resv signal task\n");
+
+	wake_up_process(task);
+	ttm_bo_reserve(bo, false, false, NULL);
+	err = ttm_bo_validate(bo, placement, &ctx);
+	dma_resv_unlock(bo->base.resv);
+
+	KUNIT_EXPECT_EQ(test, err, 0);
+	KUNIT_ASSERT_NOT_NULL(test, bo->ttm);
+	KUNIT_ASSERT_NULL(test, bo->resource);
+	KUNIT_ASSERT_NULL(test, bo->bulk_move);
+	KUNIT_EXPECT_EQ(test, ctx.bytes_moved, 0);
+
+	if (bo->type != ttm_bo_type_sg)
+		KUNIT_ASSERT_PTR_EQ(test, bo->base.resv, &bo->base._resv);
+
+	/* Make sure we have an idle object at this point */
+	dma_resv_wait_timeout(bo->base.resv, usage, false, MAX_SCHEDULE_TIMEOUT);
+
+	ttm_bo_put(bo);
+}
+
+static void ttm_bo_validate_move_fence_signaled(struct kunit *test)
+{
+	struct ttm_test_devices *priv = test->priv;
+	struct ttm_buffer_object *bo;
+	struct ttm_place *place;
+	struct ttm_placement *placement;
+	struct ttm_resource_manager *man;
+	enum ttm_bo_type bo_type = ttm_bo_type_device;
+	uint32_t mem_type = TTM_PL_SYSTEM;
+	struct ttm_operation_ctx ctx = { };
+	uint32_t size = ALIGN(BO_SIZE, PAGE_SIZE);
+	int err;
+
+	man = ttm_manager_type(priv->ttm_dev, mem_type);
+	spin_lock_init(&fence_lock);
+	man->move = dma_fence_get_stub();
+
+	bo = ttm_bo_kunit_init(test, test->priv, size);
+	bo->type = bo_type;
+
+	place = ttm_place_kunit_init(test, mem_type, 0);
+	placement = ttm_placement_kunit_init(test, place, 1, NULL, 0);
+
+	ttm_bo_reserve(bo, false, false, NULL);
+	err = ttm_bo_validate(bo, placement, &ctx);
+	dma_resv_unlock(bo->base.resv);
+
+	KUNIT_EXPECT_EQ(test, err, 0);
+	KUNIT_EXPECT_EQ(test, bo->resource->mem_type, mem_type);
+	KUNIT_EXPECT_EQ(test, ctx.bytes_moved, size);
+
+	ttm_bo_put(bo);
+	dma_fence_put(man->move);
+}
+
+static const struct ttm_bo_validate_test_case ttm_bo_validate_wait_cases[] = {
+	{
+		.description = "Waits for GPU",
+		.no_gpu_wait = false,
+	},
+	{
+		.description = "Tries to lock straight away",
+		.no_gpu_wait = true,
+	},
+};
+
+KUNIT_ARRAY_PARAM(ttm_bo_validate_wait, ttm_bo_validate_wait_cases,
+		  ttm_bo_validate_case_desc);
+
+static int threaded_fence_signal(void *arg)
+{
+	struct dma_fence *fence = arg;
+	int err;
+
+	msleep(20);
+	err = dma_fence_signal(fence);
+
+	return err;
+}
+
+static void ttm_bo_validate_move_fence_not_signaled(struct kunit *test)
+{
+	const struct ttm_bo_validate_test_case *params = test->param_value;
+	struct ttm_test_devices *priv = test->priv;
+	struct ttm_buffer_object *bo;
+	struct ttm_place *init_place, *fst_place, *snd_place;
+	struct ttm_placement *placement_init, *placement_val;
+	struct task_struct *task;
+	struct ttm_resource_manager *man;
+	enum ttm_bo_type bo_type = ttm_bo_type_device;
+	uint32_t fst_mem = TTM_PL_VRAM, snd_mem = TTM_PL_VRAM + 1;
+	uint32_t size = ALIGN(BO_SIZE, PAGE_SIZE);
+	struct ttm_operation_ctx ctx_init = { },
+				 ctx_val  = { .no_wait_gpu = params->no_gpu_wait };
+	int err;
+
+	init_place = ttm_place_kunit_init(test, TTM_PL_SYSTEM, 0);
+	placement_init = ttm_placement_kunit_init(test, init_place, 1, NULL, 0);
+
+	bo = kunit_kzalloc(test, sizeof(*bo), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, bo);
+
+	drm_gem_private_object_init(priv->drm, &bo->base, size);
+
+	err = ttm_bo_init_reserved(priv->ttm_dev, bo, bo_type, placement_init,
+				   PAGE_SIZE, &ctx_init, NULL, NULL,
+				   &dummy_ttm_bo_destroy);
+	KUNIT_EXPECT_EQ(test, err, 0);
+
+	ttm_mock_manager_init(priv->ttm_dev, fst_mem, MANAGER_SIZE);
+	ttm_mock_manager_init(priv->ttm_dev, snd_mem, MANAGER_SIZE);
+
+	fst_place = ttm_place_kunit_init(test, fst_mem, 0);
+	snd_place = ttm_place_kunit_init(test, snd_mem, 0);
+	placement_val = ttm_placement_kunit_init(test, fst_place, 1, snd_place, 1);
+
+	spin_lock_init(&fence_lock);
+	man = ttm_manager_type(priv->ttm_dev, fst_place->mem_type);
+	man->move = alloc_mock_fence(test);
+
+	task = kthread_create(threaded_fence_signal, man->move, "move-fence-signal");
+	if (IS_ERR(task))
+		KUNIT_FAIL(test, "Couldn't create move fence signal task\n");
+
+	wake_up_process(task);
+	err = ttm_bo_validate(bo, placement_val, &ctx_val);
+	dma_resv_unlock(bo->base.resv);
+
+	dma_fence_wait_timeout(man->move, false, MAX_SCHEDULE_TIMEOUT);
+
+	KUNIT_EXPECT_EQ(test, err, 0);
+	KUNIT_EXPECT_EQ(test, ctx_val.bytes_moved, size);
+
+	if (params->no_gpu_wait)
+		KUNIT_EXPECT_EQ(test, bo->resource->mem_type, snd_mem);
+	else
+		KUNIT_EXPECT_EQ(test, bo->resource->mem_type, fst_mem);
+
+	ttm_bo_put(bo);
+	ttm_mock_manager_fini(priv->ttm_dev, fst_mem);
+	ttm_mock_manager_fini(priv->ttm_dev, snd_mem);
+}
+
 static struct kunit_case ttm_bo_validate_test_cases[] = {
 	KUNIT_CASE_PARAM(ttm_bo_init_reserved_sys_man, ttm_bo_types_gen_params),
 	KUNIT_CASE_PARAM(ttm_bo_init_reserved_mock_man, ttm_bo_types_gen_params),
@@ -472,6 +773,13 @@ static struct kunit_case ttm_bo_validate_test_cases[] = {
 	KUNIT_CASE(ttm_bo_validate_pinned),
 	KUNIT_CASE(ttm_bo_validate_busy_placement),
 	KUNIT_CASE_PARAM(ttm_bo_validate_multihop, ttm_bo_types_gen_params),
+	KUNIT_CASE_PARAM(ttm_bo_validate_no_placement_signaled,
+			 ttm_bo_no_placement_gen_params),
+	KUNIT_CASE_PARAM(ttm_bo_validate_no_placement_not_signaled,
+			 ttm_bo_types_gen_params),
+	KUNIT_CASE(ttm_bo_validate_move_fence_signaled),
+	KUNIT_CASE_PARAM(ttm_bo_validate_move_fence_not_signaled,
+			 ttm_bo_validate_wait_gen_params),
 	{}
 };
 
Signature · 1 line
2.34.1
#9 lore raw

Re: [PATCH v8 8/8] drm/ttm/tests: Add test cases dependent on fence signaling

Hi Karolina,

...

Quoted 8 lines
drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c +8 -0
+static void ttm_bo_validate_no_placement_signaled(struct kunit *test)
+{
+	const struct ttm_bo_validate_test_case *params = test->param_value;
+	struct ttm_test_devices *priv = test->priv;
+	struct ttm_buffer_object *bo;
+	struct ttm_place *place;
+	struct ttm_tt *old_tt;
+	struct ttm_placement *placement;

drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c static void ttm_bo_validate_multihop(struct kunit *test) · line 535

Quoted 10 lines
drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c +10 -0
+	struct ttm_resource_manager *man;
+	uint32_t mem_type = TTM_PL_SYSTEM;
+	enum ttm_bo_type bo_type = ttm_bo_type_device;
+	struct ttm_operation_ctx ctx = { };
+	uint32_t size = ALIGN(BO_SIZE, PAGE_SIZE);
+	uint32_t flags;
+	int err;
+
+	place = ttm_place_kunit_init(test, mem_type, 0);
+	man = ttm_manager_type(priv->ttm_dev, mem_type);

For next time, I find it difficult to follow all these variables, it's easier to read

	man = ttm_manager_type(priv->ttm_dev, TTM_PL_SYSTEM);

than

	mem_type = TTM_PL_SYSTEM;
	...
	...
	...
	man = ttm_manager_type(priv->ttm_dev, mem_type);

Review

Quoted 2 lines
(quoted diff) +2 -0
+	bo = ttm_bo_kunit_init(test, test->priv, size);
+	bo->type = bo_type;

same here... the bo_type variable is not giving any value.

	bo->type = ttm_bo_type_device;

is way more readable. You keep doing this all the way and I need to check everytime what's the value in the declaration :-)

I'm not going to comment on this anymore.

Quoted 6 lines
drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c +6 -0
+	if (params->with_ttm) {
+		old_tt = priv->ttm_dev->funcs->ttm_tt_create(bo, 0);
+		ttm_pool_alloc(&priv->ttm_dev->pool, old_tt, &ctx);
+		bo->ttm = old_tt;
+	}
+

drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c static void ttm_bo_validate_multihop(struct kunit *test) · line 555

Quoted 10 lines
drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c +10 -0
+	err = ttm_resource_alloc(bo, place, &bo->resource);
+	KUNIT_EXPECT_EQ(test, err, 0);
+	KUNIT_ASSERT_EQ(test, man->usage, size);
+
+	placement = kunit_kzalloc(test, sizeof(*placement), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, placement);
+
+	ttm_bo_reserve(bo, false, false, NULL);
+	err = ttm_bo_validate(bo, placement, &ctx);
+	dma_resv_unlock(bo->base.resv);

why don't you use here ttm_bo_unreserve()?

Quoted 15 lines
drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c +15 -0
+	KUNIT_EXPECT_EQ(test, err, 0);
+	KUNIT_ASSERT_EQ(test, man->usage, 0);
+	KUNIT_ASSERT_NOT_NULL(test, bo->ttm);
+	KUNIT_EXPECT_EQ(test, ctx.bytes_moved, 0);
+
+	if (params->with_ttm) {
+		flags = bo->ttm->page_flags;
+
+		KUNIT_ASSERT_PTR_EQ(test, bo->ttm, old_tt);
+		KUNIT_ASSERT_FALSE(test, flags & TTM_TT_FLAG_PRIV_POPULATED);
+		KUNIT_ASSERT_TRUE(test, flags & TTM_TT_FLAG_ZERO_ALLOC);
+	}
+
+	ttm_bo_put(bo);
+}

...

Quoted 5 lines
drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c +5 -0
+static void ttm_bo_validate_move_fence_signaled(struct kunit *test)
+{
+	struct ttm_test_devices *priv = test->priv;
+	struct ttm_buffer_object *bo;
+	struct ttm_place *place;

drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c static void ttm_bo_validate_multihop(struct kunit *test) · line 655

Quoted 10 lines
drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c +10 -0
+	struct ttm_placement *placement;
+	struct ttm_resource_manager *man;
+	enum ttm_bo_type bo_type = ttm_bo_type_device;
+	uint32_t mem_type = TTM_PL_SYSTEM;
+	struct ttm_operation_ctx ctx = { };
+	uint32_t size = ALIGN(BO_SIZE, PAGE_SIZE);
+	int err;
+
+	man = ttm_manager_type(priv->ttm_dev, mem_type);
+	spin_lock_init(&fence_lock);

where are we using the fence_lock here?

Quoted 4 lines
drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c +4 -0
+	man->move = dma_fence_get_stub();
+
+	bo = ttm_bo_kunit_init(test, test->priv, size);
+	bo->type = bo_type;

drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c static void ttm_bo_validate_multihop(struct kunit *test) · line 670

Quoted 10 lines
drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c +10 -0
+	place = ttm_place_kunit_init(test, mem_type, 0);
+	placement = ttm_placement_kunit_init(test, place, 1, NULL, 0);
+
+	ttm_bo_reserve(bo, false, false, NULL);
+	err = ttm_bo_validate(bo, placement, &ctx);
+	dma_resv_unlock(bo->base.resv);
+
+	KUNIT_EXPECT_EQ(test, err, 0);
+	KUNIT_EXPECT_EQ(test, bo->resource->mem_type, mem_type);
+	KUNIT_EXPECT_EQ(test, ctx.bytes_moved, size);

Do we want to check also bo->ttm here?

Quoted 17 lines
drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c +17 -0
+	ttm_bo_put(bo);
+	dma_fence_put(man->move);
+}
+
+static const struct ttm_bo_validate_test_case ttm_bo_validate_wait_cases[] = {
+	{
+		.description = "Waits for GPU",
+		.no_gpu_wait = false,
+	},
+	{
+		.description = "Tries to lock straight away",
+		.no_gpu_wait = true,
+	},
+};
+
+KUNIT_ARRAY_PARAM(ttm_bo_validate_wait, ttm_bo_validate_wait_cases,
+		  ttm_bo_validate_case_desc);

drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c static void ttm_bo_validate_multihop(struct kunit *test) · line 698

Quoted 10 lines
drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c +10 -0
+
+static int threaded_fence_signal(void *arg)
+{
+	struct dma_fence *fence = arg;
+	int err;
+
+	msleep(20);
+	err = dma_fence_signal(fence);
+
+	return err;

if you do here "return dma_fence_signal(fence);" you don't need for err.

Not a binding review, though, your choice.

Andi

...

#10 lore raw

Re: [PATCH v8 8/8] drm/ttm/tests: Add test cases dependent on fence signaling

Hi Andi,

Quoted 19 lines

For next time, I find it difficult to follow all these variables, it's easier to read

	man = ttm_manager_type(priv->ttm_dev, TTM_PL_SYSTEM);

than

	mem_type = TTM_PL_SYSTEM;
	...
	...
	...
	man = ttm_manager_type(priv->ttm_dev, mem_type);

Review

Quoted 2 lines
(quoted diff) +2 -0
+	bo = ttm_bo_kunit_init(test, test->priv, size);
+	bo->type = bo_type;

same here... the bo_type variable is not giving any value.

Quoted 4 lines
	bo->type = ttm_bo_type_device;

is way more readable. You keep doing this all the way and I need to check everytime what's the value in the declaration :-)

The idea was that I'd provide these as parameters, but I limited the scope of my tests and stick to set values. Also, in some cases, I defined them because I keep checking for them in assertions, and didn't want to repeat myself.

Quoted 13 lines

I'm not going to comment on this anymore.

Quoted 10 lines
drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c +10 -0
+	if (params->with_ttm) {
+		old_tt = priv->ttm_dev->funcs->ttm_tt_create(bo, 0);
+		ttm_pool_alloc(&priv->ttm_dev->pool, old_tt, &ctx);
+		bo->ttm = old_tt;
+	}
+
+	err = ttm_resource_alloc(bo, place, &bo->resource);
+	KUNIT_EXPECT_EQ(test, err, 0);
+	KUNIT_ASSERT_EQ(test, man->usage, size);
+
Quoted 8 lines

drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c static void ttm_bo_validate_multihop(struct kunit *test) · line 555

Quoted 6 lines
drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c +6 -0
+	placement = kunit_kzalloc(test, sizeof(*placement), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, placement);
+
+	ttm_bo_reserve(bo, false, false, NULL);
+	err = ttm_bo_validate(bo, placement, &ctx);
+	dma_resv_unlock(bo->base.resv);

why don't you use here ttm_bo_unreserve()?

That's a good question! I think that unreserve would work here as well.

(...)

Quoted 9 lines
Quoted 9 lines
drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c +9 -0
+static void ttm_bo_validate_move_fence_signaled(struct kunit *test)
+{
+	struct ttm_test_devices *priv = test->priv;
+	struct ttm_buffer_object *bo;
+	struct ttm_place *place;
+	struct ttm_placement *placement;
+	struct ttm_resource_manager *man;
+	enum ttm_bo_type bo_type = ttm_bo_type_device;
+	uint32_t mem_type = TTM_PL_SYSTEM;
Quoted 8 lines

drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c static void ttm_bo_validate_multihop(struct kunit *test) · line 655

Quoted 6 lines
drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c +6 -0
+	struct ttm_operation_ctx ctx = { };
+	uint32_t size = ALIGN(BO_SIZE, PAGE_SIZE);
+	int err;
+
+	man = ttm_manager_type(priv->ttm_dev, mem_type);
+	spin_lock_init(&fence_lock);

where are we using the fence_lock here?

Argh, it's a copy-paste mistake, sorry. We don't need it, as we use a stub fence in man->move. I will delete that.

Quoted 17 lines
Quoted 5 lines
drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c +4 -0
+	man->move = dma_fence_get_stub();
+
+	bo = ttm_bo_kunit_init(test, test->priv, size);
+	bo->type = bo_type;

drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c static void ttm_bo_validate_multihop(struct kunit *test) · line 670

Quoted 10 lines
drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c +10 -0
+	place = ttm_place_kunit_init(test, mem_type, 0);
+	placement = ttm_placement_kunit_init(test, place, 1, NULL, 0);
+
+	ttm_bo_reserve(bo, false, false, NULL);
+	err = ttm_bo_validate(bo, placement, &ctx);
+	dma_resv_unlock(bo->base.resv);
+
+	KUNIT_EXPECT_EQ(test, err, 0);
+	KUNIT_EXPECT_EQ(test, bo->resource->mem_type, mem_type);
+	KUNIT_EXPECT_EQ(test, ctx.bytes_moved, size);

Do we want to check also bo->ttm here?

Hmm, I think that I covered that case with "normal" testing of ttm_bo_validate, and kept this one minimal. Here, I'm just making sure that a signaled move fence doesn't get into a way of BO validation.

Quoted 23 lines
Quoted 23 lines
drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c +22 -0
+	ttm_bo_put(bo);
+	dma_fence_put(man->move);
+}
+
+static const struct ttm_bo_validate_test_case ttm_bo_validate_wait_cases[] = {
+	{
+		.description = "Waits for GPU",
+		.no_gpu_wait = false,
+	},
+	{
+		.description = "Tries to lock straight away",
+		.no_gpu_wait = true,
+	},
+};
+
+KUNIT_ARRAY_PARAM(ttm_bo_validate_wait, ttm_bo_validate_wait_cases,
+		  ttm_bo_validate_case_desc);
+
+static int threaded_fence_signal(void *arg)
+{
+	struct dma_fence *fence = arg;
+	int err;
Quoted 8 lines

drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c static void ttm_bo_validate_multihop(struct kunit *test) · line 698

Quoted 5 lines
drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c +5 -0
+
+	msleep(20);
+	err = dma_fence_signal(fence);
+
+	return err;

if you do here "return dma_fence_signal(fence);" you don't need for err.

That is true!

Quoted 1 line

Not a binding review, though, your choice.

You spotted a couple of things I can improve, so I plan to include them in the next version, after getting more review comments.

All the best,
Karolina

#11 lore raw
In reply to Karolina Stolarek

Hi Karolina and Christian,

Quoted 9 lines

Karolina Stolarek (8):
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() drm/ttm/tests: Use an init function from the helpers lib drm/ttm/tests: Test simple BO creation and validation drm/ttm/tests: Add tests with mock resource managers drm/ttm/tests: Add test cases dependent on fence signaling

I see that all these files (and previous patches, as well) don't have a consistent prefix system, like kunit_ttm_*. But they all start with ttm_*, thread_*, drm_*, etc, which makes it a bit difficult to follow and grep through.

Can we please maintain a consistent prefix system?

I know it looks bad to come out with it after this series (and previous work too) has been out for several months already. I need to say my "mea culpa" as well as I've been in the review loop, as well.

Thanks,
Andi

#12 lore raw

Hi Andi,

Quoted 11 lines · Andi Shyti

Hi Karolina and Christian,

Quoted 4 lines

Karolina Stolarek (8): 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() drm/ttm/tests: Use an init function from the helpers lib drm/ttm/tests: Test simple BO creation and validation drm/ttm/tests: Add tests with mock resource managers drm/ttm/tests:
Add test cases dependent on fence signaling

I see that all these files (and previous patches, as well) don't have a consistent prefix system, like kunit_ttm_*. But they all start with ttm_*, thread_*, drm_*, etc, which makes it a bit difficult to follow and grep through.

I see what you mean. As for ttm_* part, I decided to keep it as it is based on what was in DRM KUnit tests and helpers lib. Almost all functions and subtests start with drm_*, and as I'm working on TTM tests, ttm_* prefix seemed like a natural choice. With thread_*, I could add ttm_kunit_* in front of it, but not sure what would be the benefit of doing this.

To be honest, I haven't considered using kunit_* prefix here. In the code context, it's obvious these are kunit tests, and repeating that information would make already long function/struct names even longer.

I had a quick glance at other KUnit tests in the kernel and this seems to be the case, no kunit_* prefixes are used.

Quoted 4 lines

Can we please maintain a consistent prefix system?

I know it looks bad to come out with it after this series (and previous work too) has been out for several months already. I need to say my "mea culpa" as well as I've been in the review loop, as well.

After this series, I plan to send a small one to wrap up my work in this field. How about adding a TODO to clean these up? I mean, that's just how I see it, I'd like to hear Christian's thoughts on this.

All the best,
Karolina

#13 lore raw

Hi Karolina,

Quoted 1 line · Karolina Stolarek

On 5.12.2023 16:39, Andi Shyti wrote:

...

Quoted 1 line
Quoted 1 line

Hi Karolina and Christian,

...

Quoted 13 lines
Quoted 8 lines
Quoted 7 lines

Karolina Stolarek (8): 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() drm/ttm/tests: Use an init function from the helpers lib drm/ttm/tests: Test simple BO creation and validation drm/ttm/tests: Add tests with mock resource managers drm/ttm/tests:
Add test cases dependent on fence signaling

Quoted 4 lines

I see that all these files (and previous patches, as well) don't have a consistent prefix system, like kunit_ttm_*. But they all start with ttm_*, thread_*, drm_*, etc, which makes it a bit difficult to follow and grep through.

Quoted 6 lines

I see what you mean. As for ttm_* part, I decided to keep it as it is based on what was in DRM KUnit tests and helpers lib. Almost all functions and subtests start with drm_*, and as I'm working on TTM tests, ttm_* prefix seemed like a natural choice. With thread_*, I could add ttm_kunit_* in front of it, but not sure what would be the benefit of doing this.

I do not agree here, ttm_* are related to ttm and not "testing ttm" and we need to be consistent.

I'd be more inclined to a kunit_ttm_* or ttm_kunit_*.

Anyway, let's hear also what Christian thinks.

Quoted 9 lines

To be honest, I haven't considered using kunit_* prefix here. In the code context, it's obvious these are kunit tests, and repeating that information would make already long function/struct names even longer.

I had a quick glance at other KUnit tests in the kernel and this seems to be the case, no kunit_* prefixes are used.

Quoted 2 lines

Can we please maintain a consistent prefix system?

Quoted 7 lines
Quoted 3 lines

I know it looks bad to come out with it after this series (and previous work too) has been out for several months already. I need to say my "mea culpa" as well as I've been in the review loop, as well.

After this series, I plan to send a small one to wrap up my work in this field. How about adding a TODO to clean these up? I mean, that's just how I see it, I'd like to hear Christian's thoughts on this.

nah... a TODO note would be too much... your promise is enough for me.

Andi

#14 lore raw
In reply to Karolina Stolarek

Hi Christian,

Quoted 5 lines · Karolina Stolarek

Karolina Stolarek (8):
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()

Would it be possible to get these patches merged? They already have r-b's. There are a couple more, but these don't depend on any other patch that is pending review, and should be easy to get in.

I'm asking for this because I have three final patches ready for submission (eviction, ttm_tt_populate, TODO file) and wondered if I should submit them as v9, or if I could create a new series. I'd prefer creating a new one, this patchset is already big and intimidating :) Having said that, thank you for taking your time to review the tests, I really appreciate it.

All the best,
Karolina

Quoted 27 lines

drm/ttm/tests: Use an init function from the helpers lib drm/ttm/tests: Test simple BO creation and validation drm/ttm/tests: Add tests with mock resource managers drm/ttm/tests: Add test cases dependent on fence signaling

  drivers/gpu/drm/Kconfig                       |   1 +
  drivers/gpu/drm/ttm/tests/.kunitconfig        |   1 +
  drivers/gpu/drm/ttm/tests/Makefile            |   5 +
  drivers/gpu/drm/ttm/tests/ttm_bo_test.c       | 622 ++++++++++++++
  .../gpu/drm/ttm/tests/ttm_bo_validate_test.c  | 795 ++++++++++++++++++
  drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c | 109 ++-
  drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h |   7 +
  drivers/gpu/drm/ttm/tests/ttm_mock_manager.c  | 207 +++++
  drivers/gpu/drm/ttm/tests/ttm_mock_manager.h  |  31 +
  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       | 282 +++++++
  drivers/gpu/drm/ttm/ttm_resource.c            |   3 +
  drivers/gpu/drm/ttm/ttm_tt.c                  |   3 +
  14 files changed, 2401 insertions(+), 3 deletions(-)

create mode 100644 drivers/gpu/drm/ttm/tests/ttm_bo_test.c create mode 100644 drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c create mode 100644 drivers/gpu/drm/ttm/tests/ttm_mock_manager.c create mode 100644 drivers/gpu/drm/ttm/tests/ttm_mock_manager.h 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

Am 14.12.23 um 09:20 schrieb Karolina Stolarek:

Quoted 10 lines

Hi Christian,

Quoted 5 lines · Karolina Stolarek

Karolina Stolarek (8):
   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()

Quoted 3 lines

Would it be possible to get these patches merged? They already have r-b's. There are a couple more, but these don't depend on any other patch that is pending review, and should be easy to get in.

Sure, just pushed those three to drm-misc-next.

Quoted 7 lines

I'm asking for this because I have three final patches ready for submission (eviction, ttm_tt_populate, TODO file) and wondered if I should submit them as v9, or if I could create a new series. I'd prefer creating a new one, this patchset is already big and intimidating :) Having said that, thank you for taking your time to review the tests, I really appreciate it.

No problem, you are actually helping getting stuff from my way to long TODO list.

Regards,
Christian.

Quoted 31 lines

All the best,
Karolina

Quoted 27 lines

   drm/ttm/tests: Use an init function from the helpers lib drm/ttm/tests: Test simple BO creation and validation drm/ttm/tests: Add tests with mock resource managers drm/ttm/tests: Add test cases dependent on fence signaling

  drivers/gpu/drm/Kconfig                       |   1 + drivers/gpu/drm/ttm/tests/.kunitconfig        |   1 + drivers/gpu/drm/ttm/tests/Makefile            |   5 + drivers/gpu/drm/ttm/tests/ttm_bo_test.c       | 622 ++++++++++++++ .../gpu/drm/ttm/tests/ttm_bo_validate_test.c  | 795 ++++++++++++++++++ drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.c | 109 ++- drivers/gpu/drm/ttm/tests/ttm_kunit_helpers.h |   7 + drivers/gpu/drm/ttm/tests/ttm_mock_manager.c  | 207 +++++ drivers/gpu/drm/ttm/tests/ttm_mock_manager.h  |  31 + 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       | 282 +++++++ drivers/gpu/drm/ttm/ttm_resource.c            |   3 + drivers/gpu/drm/ttm/ttm_tt.c                  |   3 +

  14 files changed, 2401 insertions(+), 3 deletions(-)

  create mode 100644 drivers/gpu/drm/ttm/tests/ttm_bo_test.c create mode 100644 drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c create mode 100644 drivers/gpu/drm/ttm/tests/ttm_mock_manager.c create mode 100644 drivers/gpu/drm/ttm/tests/ttm_mock_manager.h create mode 100644 drivers/gpu/drm/ttm/tests/ttm_resource_test.c create mode 100644 drivers/gpu/drm/ttm/tests/ttm_tt_test.c

#16 lore raw
Quoted 13 lines · Christian König

Am 14.12.23 um 09:20 schrieb Karolina Stolarek:

Quoted 10 lines

Hi Christian,

Quoted 5 lines · Karolina Stolarek

Karolina Stolarek (8):
   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()

Quoted 5 lines
Quoted 3 lines

Would it be possible to get these patches merged? They already have r-b's. There are a couple more, but these don't depend on any other patch that is pending review, and should be easy to get in.

Sure, just pushed those three to drm-misc-next.

Awesome, thanks a lot. I see they went into drm-tip already, so I rebased my series on the top of that.

Quoted 8 lines
Quoted 6 lines

I'm asking for this because I have three final patches ready for submission (eviction, ttm_tt_populate, TODO file) and wondered if I should submit them as v9, or if I could create a new series. I'd prefer creating a new one, this patchset is already big and intimidating :) Having said that, thank you for taking your time to review the tests, I really appreciate it.

No problem, you are actually helping getting stuff from my way to long TODO list.

By producing 4k+ lines of code for you to look at? ;-)

All the best,
Karolina

Quoted 3 lines

Regards,
Christian.