Cover allocation pressure when the only evictable buffer object's reservation lock is held by another thread. Fill the mock VRAM manager with one unpinned BO and initialize the requesting BO in system memory.
Coordinate the lock holder with completions. With no ww acquisition ticket and the default allocation flags, check that ttm_bo_validate() returns -ENOMEM while both resources, their memory types, manager usage, and bytes_moved remain unchanged. Release and join the holder, then repeat the same validation and check that eviction and allocation succeed. Join the worker before destroying either BO on every exit path. This covers reservation-lock contention without changing the mock move implementation or production behavior. AI assistance: OpenAI Codex generated this test and description, traced the existing error and cleanup paths, and orchestrated the build and KUnit validation. The task was to test a sole busy eviction candidate, check the public errno and unchanged resources, and verify success after clearing the busy condition. Human review and DCO certification remain the submitter's responsibility. Signed-off-by: 宋沛泽 <[email protected]> --- Base: drm-misc-next 5ce32a0c94a827ff44ee60898c28060714dcff75 Validation was performed by Codex through the official kunit.py entry point with QEMU TCG. The target-tree arm64 configuration used the TTM .kunitconfig plus CONFIG_COMPILE_TEST=y and no real GPU driver: - Unmodified target, all ttm*: 102/102 passed. - Patched target: new test 1/1, validate suite 36/36, all ttm* 103/103. - Fresh-boot A/B repeats: old async-fence test 10 times per variant, and all ttm* 5 times per variant. All passed, with no BUG/Oops. - Full runs retain two warnings from existing negative tests; these were present in both variants. The isolated tests had none. - checkpatch --strict --no-signoff: 0 errors, warnings, or checks. Earlier work used mainline 4f069ab1ec243e6b60dc438772905855c60774e8. On its x86_64 QEMU configuration, four pool tests failed before and after the patch. Twenty full-suite reruns of each variant retained exactly those failures. On that same unmodified mainline commit, the arm64 full suite passed 102/102. This is consistent with the x86-specific global-pool initialization path in the existing fixture, not a fix to that path. No failing test was removed or disabled. One earlier patched full run had a general-protection fault in the existing dma-resv-signal helper before the new test executed. It has not reproduced in the paired reruns: 30 isolated async-fence boots per variant and 20 full boots per variant, plus 50 isolated boots per variant under KASAN and two virtual CPUs. The new test also passed 10 KASAN/SMP boots. Its exact cause remains unresolved; these results do not establish that the original fault has been fixed. The old helper has an unjoined-worker lifetime gap, but this is a source-review finding, not a confirmed diagnosis of that one fault. No existing test or production implementation was changed here. Reproduction on the target source, before and after applying this patch: python3 tools/testing/kunit/kunit.py run --arch=arm64 \ --cross_compile=aarch64-linux-gnu- \ --kunitconfig=drivers/gpu/drm/ttm/tests/.kunitconfig \ --kconfig_add CONFIG_COMPILE_TEST=y 'ttm*' Use 'ttm_bo_validate.ttm_bo_validate_busy_resv_evict' to select the new case and 'ttm_bo_validate' for its suite. This requires the cross compiler and qemu-system-aarch64 on PATH. Exact project-local commands, configs, image hashes, and raw logs are included in the review bundle. drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c | 158 +++++++++++++++++++ 1 file changed, 158 insertions(+) diff --git a/drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c b/drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c --- a/drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c +++ b/drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c @@ -2,8 +2,11 @@ /* * Copyright © 2023 Intel Corporation */ +#include <linux/completion.h> #include <linux/delay.h> +#include <linux/jiffies.h> #include <linux/kthread.h> +#include <linux/sched/task.h> #include <drm/ttm/ttm_resource.h> #include <drm/ttm/ttm_placement.h> @@ -967,6 +970,160 @@ ttm_mock_manager_fini(priv->ttm_dev, mem_type); ttm_mock_manager_fini(priv->ttm_dev, mem_multihop); +} + +struct ttm_bo_resv_lock { + struct ttm_buffer_object *bo; + struct completion locked; + struct completion release; + int err; +}; + +static int ttm_bo_hold_resv(void *arg) +{ + struct ttm_bo_resv_lock *lock = arg; + + lock->err = ttm_bo_reserve(lock->bo, false, false, NULL); + complete(&lock->locked); + if (lock->err) + return lock->err; + + wait_for_completion(&lock->release); + ttm_bo_unreserve(lock->bo); + + return 0; +} + +static void ttm_bo_validate_busy_resv_evict(struct kunit *test) +{ + struct ttm_operation_ctx ctx_init = { }, ctx_val = { }; + struct ttm_test_devices *priv = test->priv; + struct ttm_place place = { .mem_type = TTM_PL_VRAM }; + struct ttm_place sys_place = { .mem_type = TTM_PL_SYSTEM }; + struct ttm_placement placement = { + .num_placement = 1, + .placement = &place, + }; + struct ttm_placement sys_placement = { + .num_placement = 1, + .placement = &sys_place, + }; + struct ttm_buffer_object *bo, *bo_evictable; + struct ttm_resource *old_res, *old_evict_res; + struct ttm_resource_manager *man; + struct ttm_bo_resv_lock lock; + struct task_struct *task; + unsigned long timeout; + bool retry = false; + int err; + + bo = kunit_kzalloc(test, sizeof(*bo), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, bo); + bo_evictable = kunit_kzalloc(test, sizeof(*bo_evictable), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, bo_evictable); + + err = ttm_mock_manager_init(priv->ttm_dev, TTM_PL_VRAM, MANAGER_SIZE); + KUNIT_ASSERT_EQ(test, err, 0); + err = ttm_mock_manager_init(priv->ttm_dev, TTM_PL_TT, MANAGER_SIZE); + KUNIT_EXPECT_EQ(test, err, 0); + if (err) + goto fini_vram; + man = ttm_manager_type(priv->ttm_dev, TTM_PL_VRAM); + + /* One unpinned BO fills VRAM, so it is the only eviction candidate. */ + drm_gem_private_object_init(priv->drm, &bo_evictable->base, MANAGER_SIZE); + err = ttm_bo_init_reserved(priv->ttm_dev, bo_evictable, ttm_bo_type_device, + &placement, PAGE_SIZE, &ctx_init, NULL, NULL, + &dummy_ttm_bo_destroy); + KUNIT_EXPECT_EQ(test, err, 0); + if (err) + goto fini_tt; + ttm_bo_unreserve(bo_evictable); + + drm_gem_private_object_init(priv->drm, &bo->base, BO_SIZE); + err = ttm_bo_init_reserved(priv->ttm_dev, bo, ttm_bo_type_device, + &sys_placement, PAGE_SIZE, &ctx_init, NULL, + NULL, &dummy_ttm_bo_destroy); + KUNIT_EXPECT_EQ(test, err, 0); + if (err) + goto fini_evictable; + ttm_bo_unreserve(bo); + old_res = bo->resource; + old_evict_res = bo_evictable->resource; + + lock.bo = bo_evictable; + init_completion(&lock.locked); + init_completion(&lock.release); + task = kthread_create(ttm_bo_hold_resv, &lock, "ttm-resv-lock"); + KUNIT_EXPECT_FALSE(test, IS_ERR(task)); + if (IS_ERR(task)) + goto fini_bo; + get_task_struct(task); + wake_up_process(task); + + timeout = wait_for_completion_timeout(&lock.locked, 5 * HZ); + KUNIT_EXPECT_NE(test, timeout, 0UL); + if (!timeout) + goto stop_thread; + KUNIT_EXPECT_EQ(test, lock.err, 0); + if (lock.err) + goto stop_thread; + + /* Without a ticket, eviction skips the candidate's busy reservation. */ + err = ttm_bo_reserve(bo, false, false, NULL); + KUNIT_EXPECT_EQ(test, err, 0); + if (err) + goto stop_thread; + err = ttm_bo_validate(bo, &placement, &ctx_val); + ttm_bo_unreserve(bo); + + KUNIT_EXPECT_EQ(test, err, -ENOMEM); + KUNIT_EXPECT_PTR_EQ(test, bo->resource, old_res); + KUNIT_EXPECT_PTR_EQ(test, bo_evictable->resource, old_evict_res); + KUNIT_EXPECT_NOT_NULL(test, bo->resource); + if (bo->resource) + KUNIT_EXPECT_EQ(test, bo->resource->mem_type, TTM_PL_SYSTEM); + KUNIT_EXPECT_NOT_NULL(test, bo_evictable->resource); + if (bo_evictable->resource) + KUNIT_EXPECT_EQ(test, bo_evictable->resource->mem_type, TTM_PL_VRAM); + KUNIT_EXPECT_EQ(test, bo_evictable->pin_count, 0); + KUNIT_EXPECT_EQ(test, ctx_val.bytes_moved, 0); + KUNIT_EXPECT_EQ(test, ttm_resource_manager_usage(man), MANAGER_SIZE); + retry = true; + +stop_thread: + complete(&lock.release); + err = kthread_stop_put(task); + KUNIT_EXPECT_EQ(test, err, 0); + if (!retry || err) + goto fini_bo; + + /* Retry the same placement after releasing only the candidate's lock. */ + err = ttm_bo_reserve(bo, false, false, NULL); + KUNIT_EXPECT_EQ(test, err, 0); + if (err) + goto fini_bo; + err = ttm_bo_validate(bo, &placement, &ctx_val); + ttm_bo_unreserve(bo); + + KUNIT_EXPECT_EQ(test, err, 0); + KUNIT_EXPECT_NOT_NULL(test, bo->resource); + if (bo->resource) + KUNIT_EXPECT_EQ(test, bo->resource->mem_type, TTM_PL_VRAM); + KUNIT_EXPECT_NOT_NULL(test, bo_evictable->resource); + if (bo_evictable->resource) + KUNIT_EXPECT_EQ(test, bo_evictable->resource->mem_type, TTM_PL_SYSTEM); + KUNIT_EXPECT_EQ(test, ctx_val.bytes_moved, 2 * MANAGER_SIZE + BO_SIZE); + KUNIT_EXPECT_EQ(test, ttm_resource_manager_usage(man), BO_SIZE); + +fini_bo: + ttm_bo_fini(bo); +fini_evictable: + ttm_bo_fini(bo_evictable); +fini_tt: + ttm_mock_manager_fini(priv->ttm_dev, TTM_PL_TT); +fini_vram: + ttm_mock_manager_fini(priv->ttm_dev, TTM_PL_VRAM); } static void ttm_bo_validate_deleted_evict(struct kunit *test) @@ -1197,6 +1354,7 @@ KUNIT_CASE(ttm_bo_validate_happy_evict), KUNIT_CASE(ttm_bo_validate_all_pinned_evict), KUNIT_CASE(ttm_bo_validate_allowed_only_evict), + KUNIT_CASE(ttm_bo_validate_busy_resv_evict), KUNIT_CASE(ttm_bo_validate_deleted_evict), KUNIT_CASE(ttm_bo_validate_busy_domain_evict), KUNIT_CASE(ttm_bo_validate_evict_gutting),
