* [PATCH] drm/amd/display: Fix pdev lifetime in dm_test_conn_init_ctx_alloc()
@ 2026-11-19 5:49 Tao Liu
0 siblings, 0 replies; only message in thread
From: Tao Liu @ 2026-11-19 5:49 UTC (permalink / raw)
To: harry.wentland, sunpeng.li, siqueira, alexander.deucher,
christian.koenig, airlied, simona, alex.hung, bhawanpreet.lakha,
ivan.lipski, george.zhang, adrian.betschart, rodrigo.vivi
Cc: amd-gfx, dri-devel, linux-kernel, Tao Liu
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
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-19 5:54 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19 5:49 [PATCH] drm/amd/display: Fix pdev lifetime in dm_test_conn_init_ctx_alloc() Tao Liu
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®