Skip to contentSkip to conversation

drm/mediatek: Check CRTC state before freeing

PATCH Applied Ruoyu Wang 3 participants 3 messages Jul 7 – Jul 22

Subsystems (3)
lore
Share & tools
lore

Ruoyu Wang

author
#0 lore raw

PATCH 1/1 drm/mediatek: Check CRTC state before freeing

mtk_crtc_reset() destroys the current CRTC state only when crtc->state is non-NULL, but it always converts crtc->state to struct mtk_crtc_state and passes the result to kfree().

When reset is called without an existing state, container_of(NULL, ...) does not produce NULL. Keep the mtk state free in the same crtc->state guard as the helper state destruction.

This issue was found by a static analysis checker and confirmed by manual source review.

Fixes 2d267b81898e ("drm/mtk: Use __drm_atomic_helper_crtc_reset")
---
 drivers/gpu/drm/mediatek/mtk_crtc.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/mediatek/mtk_crtc.c +3 -3
index 8e552cdc3b53b..97e3ff412e6ee 100644
@@ -154,10 +154,10 @@ static void mtk_crtc_reset(struct drm_crtc *crtc)
 {
 	struct mtk_crtc_state *state;
 
-	if (crtc->state)
+	if (crtc->state) {
 		__drm_atomic_helper_crtc_destroy_state(crtc->state);
-
-	kfree(to_mtk_crtc_state(crtc->state));
+		kfree(to_mtk_crtc_state(crtc->state));
+	}
 	crtc->state = NULL;
 
 	state = kzalloc_obj(*state);
Signature · 1 line
2.51.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Ruoyu Wang

External email : Please do not click links or open attachments until you have verified the sender or the content.

mtk_crtc_reset() destroys the current CRTC state only when crtc->state is non-NULL, but it always converts crtc->state to struct mtk_crtc_state and passes the result to kfree().

Quoted 6 lines

When reset is called without an existing state, container_of(NULL, ...) does not produce NULL. Keep the mtk state free in the same crtc->state guard as the helper state destruction.

This issue was found by a static analysis checker and confirmed by manual source review.

Quoted 29 lines
Fixes 2d267b81898e ("drm/mtk: Use __drm_atomic_helper_crtc_reset")
---
 drivers/gpu/drm/mediatek/mtk_crtc.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/mediatek/mtk_crtc.c +3 -4
index 8e552cdc3b53b..97e3ff412e6ee 100644
@@ -154,10 +154,10 @@ static void mtk_crtc_reset(struct drm_crtc *crtc)
 {
        struct mtk_crtc_state *state;
 
-       if (crtc->state)
+       if (crtc->state) {
                __drm_atomic_helper_crtc_destroy_state(crtc->state);
-
-       kfree(to_mtk_crtc_state(crtc->state));
+               kfree(to_mtk_crtc_state(crtc->state));
+       }
        crtc->state = NULL;
 
        state = kzalloc_obj(*state);
--

2.51.0

maintainer Applied
#2 lore raw
In reply to Ruoyu Wang

Ruoyu Wang <[email protected]> 於 2026年7月7日週二 下午3:05寫道:

Quoted 11 lines

mtk_crtc_reset() destroys the current CRTC state only when crtc->state is non-NULL, but it always converts crtc->state to struct mtk_crtc_state and passes the result to kfree().

When reset is called without an existing state, container_of(NULL, ...) does not produce NULL. Keep the mtk state free in the same crtc->state guard as the helper state destruction.

This issue was found by a static analysis checker and confirmed by manual source review.

Applied to mediatek-drm-fixes [1], thanks.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/chunkuang.hu/linux.git/log/?h=mediatek-drm-fixes

Regards,
Chun-Kuang.

Quoted 28 lines
Fixes 2d267b81898e ("drm/mtk: Use __drm_atomic_helper_crtc_reset")
---
 drivers/gpu/drm/mediatek/mtk_crtc.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/mediatek/mtk_crtc.c +3 -4
index 8e552cdc3b53b..97e3ff412e6ee 100644
@@ -154,10 +154,10 @@ static void mtk_crtc_reset(struct drm_crtc *crtc)
 {
        struct mtk_crtc_state *state;
 
-       if (crtc->state)
+       if (crtc->state) {
                __drm_atomic_helper_crtc_destroy_state(crtc->state);
-
-       kfree(to_mtk_crtc_state(crtc->state));
+               kfree(to_mtk_crtc_state(crtc->state));
+       }
        crtc->state = NULL;
 
        state = kzalloc_obj(*state);
--

2.51.0