Skip to contentSkip to conversation

drm/msm/mdp5: check the return of kzalloc()

PATCH In review [email protected] 3 participants 3 messages Mar 24, 2022 – Apr 6, 2022

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

PATCH 1/1 drm/msm/mdp5: check the return of kzalloc()

From: Xiaoke Wang <[email protected]>

kzalloc() is a memory allocation function which can return NULL when some internal memory errors happen. So it is better to check it to prevent potential wrong memory access.

---
 drivers/gpu/drm/msm/disp/mdp5/mdp5_plane.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
drivers/gpu/drm/msm/disp/mdp5/mdp5_plane.c +10 -7
index c6b69af..5f914cc 100644
@@ -90,15 +90,18 @@ static void mdp5_plane_reset(struct drm_plane *plane)
 		__drm_atomic_helper_plane_destroy_state(plane->state);
 
 	kfree(to_mdp5_plane_state(plane->state));
-	mdp5_state = kzalloc(sizeof(*mdp5_state), GFP_KERNEL);
+	plane->state = NULL;
 
-	if (plane->type == DRM_PLANE_TYPE_PRIMARY)
-		mdp5_state->base.zpos = STAGE_BASE;
-	else
-		mdp5_state->base.zpos = STAGE0 + drm_plane_index(plane);
-	mdp5_state->base.normalized_zpos = mdp5_state->base.zpos;
+	mdp5_state = kzalloc(sizeof(*mdp5_state), GFP_KERNEL);
+	if (mdp5_state) {
+		if (plane->type == DRM_PLANE_TYPE_PRIMARY)
+			mdp5_state->base.zpos = STAGE_BASE;
+		else
+			mdp5_state->base.zpos = STAGE0 + drm_plane_index(plane);
+		mdp5_state->base.normalized_zpos = mdp5_state->base.zpos;
 
-	__drm_atomic_helper_plane_reset(plane, &mdp5_state->base);
+		__drm_atomic_helper_plane_reset(plane, &mdp5_state->base);
+	}
 }
 
 static struct drm_plane_state *
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · On 3/24/2022 1:36 AM, [email protected] wrote:

From: Xiaoke Wang <[email protected]>

kzalloc() is a memory allocation function which can return NULL when some internal memory errors happen. So it is better to check it to prevent potential wrong memory access.

Quoted 34 lines
---
  drivers/gpu/drm/msm/disp/mdp5/mdp5_plane.c | 17 ++++++++++-------
  1 file changed, 10 insertions(+), 7 deletions(-)
drivers/gpu/drm/msm/disp/mdp5/mdp5_plane.c +10 -7
index c6b69af..5f914cc 100644
@@ -90,15 +90,18 @@ static void mdp5_plane_reset(struct drm_plane *plane)
  		__drm_atomic_helper_plane_destroy_state(plane->state);
 
  	kfree(to_mdp5_plane_state(plane->state));
-	mdp5_state = kzalloc(sizeof(*mdp5_state), GFP_KERNEL);
+	plane->state = NULL;
 
-	if (plane->type == DRM_PLANE_TYPE_PRIMARY)
-		mdp5_state->base.zpos = STAGE_BASE;
-	else
-		mdp5_state->base.zpos = STAGE0 + drm_plane_index(plane);
-	mdp5_state->base.normalized_zpos = mdp5_state->base.zpos;
+	mdp5_state = kzalloc(sizeof(*mdp5_state), GFP_KERNEL);
+	if (mdp5_state) {
+		if (plane->type == DRM_PLANE_TYPE_PRIMARY)
+			mdp5_state->base.zpos = STAGE_BASE;
+		else
+			mdp5_state->base.zpos = STAGE0 + drm_plane_index(plane);
+		mdp5_state->base.normalized_zpos = mdp5_state->base.zpos;
 
-	__drm_atomic_helper_plane_reset(plane, &mdp5_state->base);
+		__drm_atomic_helper_plane_reset(plane, &mdp5_state->base);
+	}
  }
 
  static struct drm_plane_state *
#2 lore raw
In reply to [email protected]
Quoted 17 lines · On Thu, Mar 24, 2022 at 1:37 AM <[email protected]> wrote:

From: Xiaoke Wang <[email protected]>

kzalloc() is a memory allocation function which can return NULL when some internal memory errors happen. So it is better to check it to prevent potential wrong memory access.

---
 drivers/gpu/drm/msm/disp/mdp5/mdp5_plane.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
drivers/gpu/drm/msm/disp/mdp5/mdp5_plane.c +0 -0
index c6b69af..5f914cc 100644

drivers/gpu/drm/msm/disp/mdp5/mdp5_plane.c static void mdp5_plane_reset(struct drm_plane *plane) · line 93

Quoted 5 lines
drivers/gpu/drm/msm/disp/mdp5/mdp5_plane.c +1 -1
@@ -90,15 +90,18 @@ static void mdp5_plane_reset(struct drm_plane *plane)
                __drm_atomic_helper_plane_destroy_state(plane->state);
 
        kfree(to_mdp5_plane_state(plane->state));
-       mdp5_state = kzalloc(sizeof(*mdp5_state), GFP_KERNEL);
+       plane->state = NULL;

At this point, you could just:

    if (!mdp5_state)
       return;

BR,
-R

Quoted 21 lines
drivers/gpu/drm/msm/disp/mdp5/mdp5_plane.c +9 -7
-       if (plane->type == DRM_PLANE_TYPE_PRIMARY)
-               mdp5_state->base.zpos = STAGE_BASE;
-       else
-               mdp5_state->base.zpos = STAGE0 + drm_plane_index(plane);
-       mdp5_state->base.normalized_zpos = mdp5_state->base.zpos;
+       mdp5_state = kzalloc(sizeof(*mdp5_state), GFP_KERNEL);
+       if (mdp5_state) {
+               if (plane->type == DRM_PLANE_TYPE_PRIMARY)
+                       mdp5_state->base.zpos = STAGE_BASE;
+               else
+                       mdp5_state->base.zpos = STAGE0 + drm_plane_index(plane);
+               mdp5_state->base.normalized_zpos = mdp5_state->base.zpos;
 
-       __drm_atomic_helper_plane_reset(plane, &mdp5_state->base);
+               __drm_atomic_helper_plane_reset(plane, &mdp5_state->base);
+       }
 }
 
 static struct drm_plane_state *
--