Skip to contentSkip to conversation

drm/amd/display: Fix pdev lifetime in dm_test_conn_init_ctx_alloc()

PATCH New Tao Liu 1 participant 1 message Nov 19

Subsystems (3)
lore
Share & tools
lore

Tao Liu

author
#0 lore raw

PATCH 1/1 drm/amd/display: Fix pdev lifetime in dm_test_conn_init_ctx_alloc()

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")
---
 .../amdgpu_dm/tests/amdgpu_dm_connector_test.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/display/amdgpu_dm/tests/amdgpu_dm_connector_test.c +16 -2
index f09e2a72c534..b43c4eff2bef 100644
@@ -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);
Signature · 1 line
2.56.0