Skip to contentSkip to conversation

dpu1: dpu_encoder: fix a missing check on list iterator

PATCH In review Xiaomeng Tong 2 participants 3 messages Mar 27, 2022 – Apr 11, 2022

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dpu1: dpu_encoder: fix a missing check on list iterator

The bug is here:

	 cstate = to_dpu_crtc_state(drm_crtc->state);

For the drm_for_each_crtc(), just like list_for_each_entry(), the list iterator 'drm_crtc' will point to a bogus position containing HEAD if the list is empty or no element is found. This case must be checked before any use of the iterator, otherwise it will lead to a invalid memory access.

To fix this bug, use a new variable 'iter' as the list iterator, while use the origin variable 'drm_crtc' as a dedicated pointer to point to the found element.

Fixes b107603b4ad0f ("drm/msm/dpu: map mixer/ctl hw blocks in encoder modeset")
---
 drivers/gpu/drm/msm/disp/dpu1/dpu_encoder.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/gpu/drm/msm/disp/dpu1/dpu_encoder.c +8 -3
index 1e648db439f9..d3fdb18e96f9 100644
@@ -965,7 +965,7 @@ static void dpu_encoder_virt_mode_set(struct drm_encoder *drm_enc,
 	struct dpu_kms *dpu_kms;
 	struct list_head *connector_list;
 	struct drm_connector *conn = NULL, *conn_iter;
-	struct drm_crtc *drm_crtc;
+	struct drm_crtc *drm_crtc = NULL, *iter;
 	struct dpu_crtc_state *cstate;
 	struct dpu_global_state *global_state;
 	struct dpu_hw_blk *hw_pp[MAX_CHANNELS_PER_ENC];
@@ -1007,9 +1007,14 @@ static void dpu_encoder_virt_mode_set(struct drm_encoder *drm_enc,
 		return;
 	}
 
-	drm_for_each_crtc(drm_crtc, drm_enc->dev)
-		if (drm_crtc->state->encoder_mask & drm_encoder_mask(drm_enc))
+	drm_for_each_crtc(iter, drm_enc->dev)
+		if (iter->state->encoder_mask & drm_encoder_mask(drm_enc)) {
+			drm_crtc = iter;
 			break;
+		}
+
+	if (!drm_crtc)
+		return;
 
 	/* Query resource that have been reserved in atomic check step. */
 	num_pp = dpu_rm_get_assigned_resources(&dpu_kms->rm, global_state,
Signature · 1 line
2.17.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Xiaomeng Tong

The bug is here:

	 cstate = to_dpu_crtc_state(drm_crtc->state);

For the drm_for_each_crtc(), just like list_for_each_entry(), the list iterator 'drm_crtc' will point to a bogus position containing HEAD if the list is empty or no element is found. This case must be checked before any use of the iterator, otherwise it will lead to a invalid memory access.

Quoted 7 lines

To fix this bug, use a new variable 'iter' as the list iterator, while use the origin variable 'drm_crtc' as a dedicated pointer to point to the found element.

Fixes b107603b4ad0f ("drm/msm/dpu: map mixer/ctl hw blocks in encoder modeset")
Quoted 34 lines
---
  drivers/gpu/drm/msm/disp/dpu1/dpu_encoder.c | 11 ++++++++---
  1 file changed, 8 insertions(+), 3 deletions(-)
drivers/gpu/drm/msm/disp/dpu1/dpu_encoder.c +8 -3
index 1e648db439f9..d3fdb18e96f9 100644
@@ -965,7 +965,7 @@ static void dpu_encoder_virt_mode_set(struct drm_encoder *drm_enc,
  	struct dpu_kms *dpu_kms;
  	struct list_head *connector_list;
  	struct drm_connector *conn = NULL, *conn_iter;
-	struct drm_crtc *drm_crtc;
+	struct drm_crtc *drm_crtc = NULL, *iter;
  	struct dpu_crtc_state *cstate;
  	struct dpu_global_state *global_state;
  	struct dpu_hw_blk *hw_pp[MAX_CHANNELS_PER_ENC];
@@ -1007,9 +1007,14 @@ static void dpu_encoder_virt_mode_set(struct drm_encoder *drm_enc,
  		return;
  	}
 
-	drm_for_each_crtc(drm_crtc, drm_enc->dev)
-		if (drm_crtc->state->encoder_mask & drm_encoder_mask(drm_enc))
+	drm_for_each_crtc(iter, drm_enc->dev)
+		if (iter->state->encoder_mask & drm_encoder_mask(drm_enc)) {
+			drm_crtc = iter;
  			break;
+		}
+
+	if (!drm_crtc)
+		return;
 
  	/* Query resource that have been reserved in atomic check step. */
  	num_pp = dpu_rm_get_assigned_resources(&dpu_kms->rm, global_state,
Signature · 2 lines
With best wishes
Dmitry
#2 lore raw
Quoted 14 lines · Dmitry Baryshkov
Quoted 13 lines · Xiaomeng Tong

The bug is here:
     cstate = to_dpu_crtc_state(drm_crtc->state);

For the drm_for_each_crtc(), just like list_for_each_entry(), the list iterator 'drm_crtc' will point to a bogus position containing HEAD if the list is empty or no element is found. This case must be checked before any use of the iterator, otherwise it will lead to a invalid memory access.

To fix this bug, use a new variable 'iter' as the list iterator, while use the origin variable 'drm_crtc' as a dedicated pointer to point to the found element.

Quoted 5 lines
Quoted 3 lines
Fixes b107603b4ad0f ("drm/msm/dpu: map mixer/ctl hw blocks in encoder modeset")

On the other hand, this code has been removed in 5.18-rc1 in the commit 764332bf96244cbc8baf08aa35844b29106da312.

Quoted 37 lines
Quoted 35 lines

---
  drivers/gpu/drm/msm/disp/dpu1/dpu_encoder.c | 11 ++++++++---

  1 file changed, 8 insertions(+), 3 deletions(-)
drivers/gpu/drm/msm/disp/dpu1/dpu_encoder.c +1 -1
index 1e648db439f9..d3fdb18e96f9 100644
@@ -965,7 +965,7 @@ static void dpu_encoder_virt_mode_set(struct 
 drm_encoder *drm_enc,
       struct dpu_kms *dpu_kms;
       struct list_head *connector_list;
       struct drm_connector *conn = NULL, *conn_iter;
-    struct drm_crtc *drm_crtc;
+    struct drm_crtc *drm_crtc = NULL, *iter;
       struct dpu_crtc_state *cstate;
       struct dpu_global_state *global_state;

      struct dpu_hw_blk *hw_pp[MAX_CHANNELS_PER_ENC];

drivers/gpu/drm/msm/disp/dpu1/dpu_encoder.c +7 -2
@@ -1007,9 +1007,14 @@ static void dpu_encoder_virt_mode_set(struct 
 drm_encoder *drm_enc,
           return;
       }
-    drm_for_each_crtc(drm_crtc, drm_enc->dev)
-        if (drm_crtc->state->encoder_mask & drm_encoder_mask(drm_enc))
+    drm_for_each_crtc(iter, drm_enc->dev)
+        if (iter->state->encoder_mask & drm_encoder_mask(drm_enc)) {
+            drm_crtc = iter;
               break;
+        }
+
+    if (!drm_crtc)
+        return;
       /* Query resource that have been reserved in atomic check step. */
       num_pp = dpu_rm_get_assigned_resources(&dpu_kms->rm, global_state,
Signature · 2 lines
With best wishes
Dmitry