From: Alex Hung <[email protected]> [WHAT] Add KUnit tests to verify both functions return early when job->fb is NULL without touching any buffer object.
Assisted-by: Copilot:Claude-Opus-4.6 Reviewed-by: Bhawanpreet Lakha <[email protected]> Signed-off-by: Alex Hung <[email protected]> Signed-off-by: George Zhang <[email protected]> --- .../drm/amd/display/amdgpu_dm/amdgpu_dm_wb.c | 6 ++- .../drm/amd/display/amdgpu_dm/amdgpu_dm_wb.h | 4 ++ .../amdgpu_dm/tests/amdgpu_dm_wb_test.c | 44 +++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_wb.c b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_wb.c index 0bf82e46f773..1fbb568a5c80 100644 --- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_wb.c +++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_wb.c @@ -85,7 +85,7 @@ STATIC_IFN_KUNIT int amdgpu_dm_wb_connector_get_modes(struct drm_connector *conn } EXPORT_IF_KUNIT(amdgpu_dm_wb_connector_get_modes); -static int amdgpu_dm_wb_prepare_job(struct drm_writeback_connector *wb_connector, +STATIC_IFN_KUNIT int amdgpu_dm_wb_prepare_job(struct drm_writeback_connector *wb_connector, struct drm_writeback_job *job) { struct amdgpu_framebuffer *afb; @@ -146,8 +146,9 @@ static int amdgpu_dm_wb_prepare_job(struct drm_writeback_connector *wb_connector amdgpu_bo_unreserve(rbo); return r; } +EXPORT_IF_KUNIT(amdgpu_dm_wb_prepare_job); -static void amdgpu_dm_wb_cleanup_job(struct drm_writeback_connector *connector, +STATIC_IFN_KUNIT void amdgpu_dm_wb_cleanup_job(struct drm_writeback_connector *connector, struct drm_writeback_job *job) { struct amdgpu_bo *rbo; @@ -167,6 +168,7 @@ static void amdgpu_dm_wb_cleanup_job(struct drm_writeback_connector *connector, amdgpu_bo_unreserve(rbo); amdgpu_bo_unref(&rbo); } +EXPORT_IF_KUNIT(amdgpu_dm_wb_cleanup_job); static const struct drm_encoder_helper_funcs amdgpu_dm_wb_encoder_helper_funcs = { .atomic_check = amdgpu_dm_wb_encoder_atomic_check, diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_wb.h b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_wb.h index 7e9fd7a036fa..5fd616bc43b5 100644 --- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_wb.h +++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_wb.h @@ -44,6 +44,10 @@ int amdgpu_dm_wb_encoder_atomic_check(struct drm_encoder *encoder, struct drm_crtc_state *crtc_state, struct drm_connector_state *conn_state); int amdgpu_dm_wb_connector_get_modes(struct drm_connector *connector); +int amdgpu_dm_wb_prepare_job(struct drm_writeback_connector *wb_connector, + struct drm_writeback_job *job); +void amdgpu_dm_wb_cleanup_job(struct drm_writeback_connector *connector, + struct drm_writeback_job *job); #endif #endif diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/tests/amdgpu_dm_wb_test.c b/drivers/gpu/drm/amd/display/amdgpu_dm/tests/amdgpu_dm_wb_test.c index c71f61a2438d..b43bc244487e 100644 --- a/drivers/gpu/drm/amd/display/amdgpu_dm/tests/amdgpu_dm_wb_test.c +++ b/drivers/gpu/drm/amd/display/amdgpu_dm/tests/amdgpu_dm_wb_test.c @@ -364,6 +364,47 @@ static void dm_test_wb_connector_init_success(struct kunit *test) KUNIT_EXPECT_EQ(test, wbcon->base.encoder.possible_crtcs, 0x1); } +/* Tests for amdgpu_dm_wb_prepare_job / amdgpu_dm_wb_cleanup_job */ + +/** + * dm_test_wb_prepare_job_no_fb - Verify prepare_job early return without a framebuffer + * @test: KUnit test context + * + * When job->fb is NULL there is nothing to pin, so amdgpu_dm_wb_prepare_job() + * must return 0 without touching any buffer object. + */ +static void dm_test_wb_prepare_job_no_fb(struct kunit *test) +{ + struct drm_writeback_job *job; + int ret; + + job = kunit_kzalloc(test, sizeof(*job), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, job); + + job->fb = NULL; + ret = amdgpu_dm_wb_prepare_job(NULL, job); + KUNIT_EXPECT_EQ(test, ret, 0); +} + +/** + * dm_test_wb_cleanup_job_no_fb - Verify cleanup_job early return without a framebuffer + * @test: KUnit test context + * + * When job->fb is NULL there is nothing to unpin, so amdgpu_dm_wb_cleanup_job() + * must return immediately. + */ +static void dm_test_wb_cleanup_job_no_fb(struct kunit *test) +{ + struct drm_writeback_job *job; + + job = kunit_kzalloc(test, sizeof(*job), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, job); + + job->fb = NULL; + /* Should return without dereferencing any buffer object. */ + amdgpu_dm_wb_cleanup_job(NULL, job); +} + static struct kunit_case dm_wb_test_cases[] = { /* amdgpu_dm_wb_encoder_atomic_check */ KUNIT_CASE(dm_test_wb_atomic_check_no_job), @@ -378,6 +419,9 @@ static struct kunit_case dm_wb_test_cases[] = { KUNIT_CASE(dm_test_wb_get_modes_bounded_by_max), /* amdgpu_dm_wb_connector_init */ KUNIT_CASE(dm_test_wb_connector_init_success), + /* amdgpu_dm_wb_prepare_job / amdgpu_dm_wb_cleanup_job */ + KUNIT_CASE(dm_test_wb_prepare_job_no_fb), + KUNIT_CASE(dm_test_wb_cleanup_job_no_fb), {} }; -- 2.55.0
