mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Tao Liu <ltao@redhat.com>
To: harry.wentland@amd.com, sunpeng.li@amd.com, siqueira@igalia.com,
	alexander.deucher@amd.com, christian.koenig@amd.com,
	airlied@gmail.com, simona@ffwll.ch, alex.hung@amd.com,
	bhawanpreet.lakha@amd.com, ivan.lipski@amd.com,
	george.zhang@amd.com, adrian.betschart@cinemaone.ch,
	rodrigo.vivi@intel.com
Cc: amd-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org,
	linux-kernel@vger.kernel.org, Tao Liu <ltao@redhat.com>
Subject: [PATCH] drm/amd/display: Fix pdev lifetime in dm_test_conn_init_ctx_alloc()
Date: Thu, 19 Nov 2026 18:49:18 +1300	[thread overview]
Message-ID: <20261119054917.184758-2-ltao@redhat.com> (raw)

The pdev backing memory is currently allocated with kunit_kzalloc(),
which ties its lifetime to the KUnit test context.

However, pdev embeds a struct device whose lifetime is managed
independently through the kobject reference count. This creates two
independent lifetime management for the same object: KUnit manages
the backing memory, while kobject manages the device's reference count.

Although dm_test_conn_init_release_dev() is currently empty, the
embedded kobject still relies on the backing memory remaining valid
for reference count operations. If KUnit free pdev while its embedded
kobject is still referenced, subsequent reference count operations can
then access the freed kobject, resulting in a use-after-free:

    refcount_t: underflow; use-after-free.
    Call Trace:
     device_del+0x29c/0x410
     device_unregister+0x17/0x70
     i2c_del_adapter+0x128/0x1b0
     release_nodes+0x4a/0xe0
     devres_release_all+0x9e/0x120
     device_unbind_cleanup+0x26/0xa0
     device_release_driver_internal+0x1b6/0x240
     bus_remove_device+0xfe/0x200
     device_del+0x179/0x410
     device_unregister+0x17/0x70
     kunit_remove_resource+0x93/0xc0 [kunit]
     kunit_cleanup+0x49/0xa0 [kunit]
     kunit_generic_run_threadfn_adapter+0x21/0x40 [kunit]

Fix this by allocating pdev with kzalloc() and freeing it through
the device release callback. This ensures pdev's lifetime is managed
solely by the kobject.

Fixes: 7db85e4b053c ("drm/amd/display: Test connector init")
Signed-off-by: Tao Liu <ltao@redhat.com>
---
 .../amdgpu_dm/tests/amdgpu_dm_connector_test.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/tests/amdgpu_dm_connector_test.c b/drivers/gpu/drm/amd/display/amdgpu_dm/tests/amdgpu_dm_connector_test.c
index f09e2a72c534..b43c4eff2bef 100644
--- a/drivers/gpu/drm/amd/display/amdgpu_dm/tests/amdgpu_dm_connector_test.c
+++ b/drivers/gpu/drm/amd/display/amdgpu_dm/tests/amdgpu_dm_connector_test.c
@@ -9659,7 +9659,9 @@ static void dm_test_fs_caps_disables_replay(struct kunit *test)
  */
 static void dm_test_conn_init_release_dev(struct device *dev)
 {
-	/* Backing storage is KUnit managed, so there is nothing to free. */
+	struct pci_dev *pdev = to_pci_dev(dev);
+
+	kfree(pdev);
 }
 
 static void dm_test_conn_init_unregister_dev(void *data)
@@ -9667,6 +9669,11 @@ static void dm_test_conn_init_unregister_dev(void *data)
 	device_unregister(data);
 }
 
+static void dm_test_conn_init_put_dev(void *data)
+{
+	put_device(data);
+}
+
 struct dm_test_conn_init_ctx {
 	struct amdgpu_device *adev;
 	struct drm_device *drm;
@@ -9702,13 +9709,20 @@ dm_test_conn_init_ctx_alloc(struct kunit *test, enum signal_type signal)
 	ctx->adev->mode_info.num_crtc = 1;
 	dm_test_create_mode_props(test, ctx->adev);
 
-	pdev = kunit_kzalloc(test, sizeof(*pdev), GFP_KERNEL);
+	pdev = kzalloc_obj(*pdev);
 	KUNIT_ASSERT_NOT_NULL(test, pdev);
 	device_initialize(&pdev->dev);
 	pdev->dev.parent = dev;
 	pdev->dev.release = dm_test_conn_init_release_dev;
+
+	KUNIT_ASSERT_EQ(test,
+			kunit_add_action_or_reset(test, dm_test_conn_init_put_dev,
+						  &pdev->dev), 0);
 	KUNIT_ASSERT_EQ(test, dev_set_name(&pdev->dev, "dm-test-i2c-parent"), 0);
 	KUNIT_ASSERT_EQ(test, device_add(&pdev->dev), 0);
+
+	kunit_remove_action(test, dm_test_conn_init_put_dev, &pdev->dev);
+
 	KUNIT_ASSERT_EQ(test,
 			kunit_add_action_or_reset(test, dm_test_conn_init_unregister_dev,
 						  &pdev->dev), 0);
-- 
2.56.0


                 reply	other threads:[~2026-11-19  5:54 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261119054917.184758-2-ltao@redhat.com \
    --to=ltao@redhat.com \
    --cc=adrian.betschart@cinemaone.ch \
    --cc=airlied@gmail.com \
    --cc=alex.hung@amd.com \
    --cc=alexander.deucher@amd.com \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=bhawanpreet.lakha@amd.com \
    --cc=christian.koenig@amd.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=george.zhang@amd.com \
    --cc=harry.wentland@amd.com \
    --cc=ivan.lipski@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rodrigo.vivi@intel.com \
    --cc=simona@ffwll.ch \
    --cc=siqueira@igalia.com \
    --cc=sunpeng.li@amd.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®