drm/amd/display: fix NULL ptr deref in ISM delayed work
Subsystems (3)
PATCH 1/1 drm/amd/display: fix NULL ptr deref in ISM delayed work
dc_destroy() sets dm->dc to NULL before amdgpu_dm_ism_fini() is called, leaving a window where in-flight ISM delayed work dereferences the stale pointer. Call amdgpu_dm_ism_fini() in amdgpu_dm_fini() before dc_destroy().
--- drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c | 9 +++++++++ drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c | 7 ++++++- 2 files changed, 15 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c +9 -0
@@ -2236,6 +2236,8 @@ static int amdgpu_dm_early_fini(struct amdgpu_ip_block *ip_block)static void amdgpu_dm_fini (struct amdgpu_device *adev) {int i; +struct drm_crtc *crtc; +struct amdgpu_crtc *acrtc;if (adev->dm.vblank_control_workqueue) {destroy_workqueue (adev->dm.vblank_control_workqueue);
@@ -2252,6 +2254,13 @@ static void amdgpu_dm_fini(struct amdgpu_device *adev) adev->dm.idle_workqueue =NULL ; } +/* Finalize ISM for each CRTC before dc_destroy() sets dm->dc to NULL */ +drm_for_each_crtc (crtc,adev_to_drm (adev)) { + acrtc =to_amdgpu_crtc (crtc); +amdgpu_dm_ism_fini (&acrtc->ism); + + } +amdgpu_dm_destroy_drm_device (&adev->dm);#if defined(CONFIG_DRM_AMD_SECURE_DISPLAY )
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c +6 -1
@@ -459,7 +459,12 @@ static void amdgpu_dm_crtc_destroy(struct drm_crtc *crtc) {struct amdgpu_crtc *acrtc =to_amdgpu_crtc (crtc); -amdgpu_dm_ism_fini (&acrtc->ism); +/* +* amdgpu_dm_ism_fini() is intentionally called in amdgpu_dm_fini(). +* It must be called before dc_destroy() in amdgpu_dm_fini() +* to avoid ISM accessing an invalid dc handle once dc is released. +*/ +drm_crtc_cleanup (crtc);kfree (crtc); }
Signature · 1 line
2.43.0
Quoted 6 lines · Ray Wu
dc_destroy()setsdm->dcto NULL beforeamdgpu_dm_ism_fini()is called, leaving a window where in-flight ISM delayed work dereferences the stale pointer. Callamdgpu_dm_ism_fini()inamdgpu_dm_fini()beforedc_destroy().Fixes f5d0d3f3439e ("drm/amd/display: Add Idle state manager(ISM)")Signed-off-by Ray Wu <[email protected]>
Thanks!
Quoted 50 lines
--- drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c | 9 +++++++++ drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c | 7 ++++++- 2 files changed, 15 insertions(+), 1 deletion(-)drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c +9
-0@@ -2236,6 +2236,8 @@ static int amdgpu_dm_early_fini(struct amdgpu_ip_block *ip_block)static void amdgpu_dm_fini (struct amdgpu_device *adev) {int i; +struct drm_crtc *crtc; +struct amdgpu_crtc *acrtc;if (adev->dm.vblank_control_workqueue) {destroy_workqueue (adev->dm.vblank_control_workqueue);@@ -2252,6 +2254,13 @@ static void amdgpu_dm_fini(struct amdgpu_device *adev) adev->dm.idle_workqueue =NULL ; } +/* Finalize ISM for each CRTC before dc_destroy() sets dm->dc to NULL */ +drm_for_each_crtc (crtc,adev_to_drm (adev)) { + acrtc =to_amdgpu_crtc (crtc); +amdgpu_dm_ism_fini (&acrtc->ism); + + } +amdgpu_dm_destroy_drm_device (&adev->dm);#if defined(CONFIG_DRM_AMD_SECURE_DISPLAY )drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c +6
-1@@ -459,7 +459,12 @@ static void amdgpu_dm_crtc_destroy(struct drm_crtc *crtc) {struct amdgpu_crtc *acrtc =to_amdgpu_crtc (crtc); -amdgpu_dm_ism_fini (&acrtc->ism); +/* +* amdgpu_dm_ism_fini() is intentionally called in amdgpu_dm_fini(). +* It must be called before dc_destroy() in amdgpu_dm_fini() +* to avoid ISM accessing an invalid dc handle once dc is released. +*/ +drm_crtc_cleanup (crtc);kfree (crtc); }
Quoted 43 lines · Ray Wu
dc_destroy()setsdm->dcto NULL beforeamdgpu_dm_ism_fini()is called, leaving a window where in-flight ISM delayed work dereferences the stale pointer. Callamdgpu_dm_ism_fini()inamdgpu_dm_fini()beforedc_destroy().Fixes f5d0d3f3439e ("drm/amd/display: Add Idle state manager(ISM)")Signed-off-by Ray Wu <[email protected]>--- drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c | 9 +++++++++ drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c | 7 ++++++- 2 files changed, 15 insertions(+), 1 deletion(-)drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c +9
-0@@ -2236,6 +2236,8 @@ static int amdgpu_dm_early_fini(struct amdgpu_ip_block *ip_block)static void amdgpu_dm_fini (struct amdgpu_device *adev) {int i; +struct drm_crtc *crtc; +struct amdgpu_crtc *acrtc;if (adev->dm.vblank_control_workqueue) {destroy_workqueue (adev->dm.vblank_control_workqueue);@@ -2252,6 +2254,13 @@ static void amdgpu_dm_fini(struct amdgpu_device *adev) adev->dm.idle_workqueue =NULL ; } +/* Finalize ISM for each CRTC before dc_destroy() sets dm->dc to NULL */ +drm_for_each_crtc (crtc,adev_to_drm (adev)) { + acrtc =to_amdgpu_crtc (crtc); +amdgpu_dm_ism_fini (&acrtc->ism); + + } +amdgpu_dm_destroy_drm_device (&adev->dm);#if defined(CONFIG_DRM_AMD_SECURE_DISPLAY )drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c +0
-0
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c static void amdgpu_dm_crtc_destroy(struct drm_crtc *crtc) · line 466
Quoted 9 lines
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c +5
-1@@ -459,7 +459,12 @@ static void amdgpu_dm_crtc_destroy(struct drm_crtc *crtc) {struct amdgpu_crtc *acrtc =to_amdgpu_crtc (crtc); -amdgpu_dm_ism_fini (&acrtc->ism); +/* +* amdgpu_dm_ism_fini() is intentionally called in amdgpu_dm_fini(). +* It must be called before dc_destroy() in amdgpu_dm_fini() +* to avoid ISM accessing an invalid dc handle once dc is released. +*/
I'm seeing a new build warning with this hunk:
drivers/gpu/drm/amd/amdgpu/../display/amdgpu_dm/amdgpu_dm_crtc.c: In function ‘amdgpu_dm_crtc_destroy’:drivers/gpu/drm/amd/amdgpu/../display/amdgpu_dm/amdgpu_dm_crtc.c:460:29: warning: unused variable ‘acrtc’ [-Wunused-variable]
460 | struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc);
| ^~~~~
This diff resolved it:
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c +0 -2
@@ -457,8 +457,6 @@ static struct drm_crtc_state *amdgpu_dm_crtc_duplicate_state(struct drm_crtc *crstatic void amdgpu_dm_crtc_destroy (struct drm_crtc *crtc) { -struct amdgpu_crtc *acrtc =to_amdgpu_crtc (crtc); -/* * amdgpu_dm_ism_fini() is intentionally called in amdgpu_dm_fini(). * It must be called before dc_destroy() in amdgpu_dm_fini()
Thanks,
Matt
Quoted 4 lines
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_crtc.c +1
-0+drm_crtc_cleanup (crtc);kfree (crtc); }