Skip to contentSkip to conversation

drm/mediatek/dp: fix spurious kfree()

PATCH Superseded Michael Walle 3 participants 7 messages May 17, 2024

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

PATCH 1/1 drm/mediatek/dp: fix spurious kfree()

drm_edid_to_sad() might return an error or just zero. If that is the case, we must not free the SADs because there was no allocation in the first place.

Fixes dab12fa8d2bd ("drm/mediatek/dp: fix memory leak on ->get_edid callback audio detection")
---
 drivers/gpu/drm/mediatek/mtk_dp.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/gpu/drm/mediatek/mtk_dp.c +8 -2
index 536366956447..ada12927bbac 100644
@@ -2073,9 +2073,15 @@ static const struct drm_edid *mtk_dp_edid_read(struct drm_bridge *bridge,
 		 */
 		const struct edid *edid = drm_edid_raw(drm_edid);
 		struct cea_sad *sads;
+		int ret;
 
-		audio_caps->sad_count = drm_edid_to_sad(edid, &sads);
-		kfree(sads);
+		ret = drm_edid_to_sad(edid, &sads);
+		/* Ignore any errors */
+		if (ret < 0)
+			ret = 0;
+		if (ret)
+			kfree(sads);
+		audio_caps->sad_count = ret;
 
 		/*
 		 * FIXME: This should use connector->display_info.has_audio from
Signature · 1 line
2.39.2
#1 lore raw

Il 17/05/24 11:30, Michael Walle ha scritto:

Quoted 17 lines

drm_edid_to_sad() might return an error or just zero. If that is the case, we must not free the SADs because there was no allocation in the first place.

Fixes dab12fa8d2bd ("drm/mediatek/dp: fix memory leak on ->get_edid callback audio detection")
---
  drivers/gpu/drm/mediatek/mtk_dp.c | 10 ++++++++--
  1 file changed, 8 insertions(+), 2 deletions(-)
drivers/gpu/drm/mediatek/mtk_dp.c +0 -0
index 536366956447..ada12927bbac 100644
@@ -2073,9 +2073,15 @@ static const struct drm_edid *mtk_dp_edid_read(struct drm_bridge *bridge,
  		 */
  		const struct edid *edid = drm_edid_raw(drm_edid);

drivers/gpu/drm/mediatek/mtk_dp.c static const struct drm_edid *mtk_dp_edid_read(struct drm_bridge *bridge, · line 2082

Quoted 10 lines
drivers/gpu/drm/mediatek/mtk_dp.c +6 -2
  		struct cea_sad *sads;
+		int ret;
 
-		audio_caps->sad_count = drm_edid_to_sad(edid, &sads);
-		kfree(sads);
+		ret = drm_edid_to_sad(edid, &sads);
+		/* Ignore any errors */
+		if (ret < 0)
+			ret = 0;
+		if (ret)

Eh, this will never work, because you're clearing the error before checking if there's any error here?!?! :-P

Anyway in reality, it returns -ENOMEM if the allocation was not successful... in the event that any future update adds any other error we'd be back with the same issue, but I'm not sure how much should we worry about that.

To be extremely safe, we could do...

if (ret != -ENOMEM)
kfree(sads)

audio_caps->sad_count = ret < 0 ? 0 : ret;

Cheers!
Angelo

Quoted 5 lines
drivers/gpu/drm/mediatek/mtk_dp.c +2 -0
+			kfree(sads);
+		audio_caps->sad_count = ret;
 
  		/*
  		 * FIXME: This should use connector->display_info.has_audio from
#2 lore raw
Quoted 21 lines · AngeloGioacchino Del Regno

Il 17/05/24 11:30, Michael Walle ha scritto:

Quoted 20 lines

drm_edid_to_sad() might return an error or just zero. If that is the case, we must not free the SADs because there was no allocation in the first place.

Fixes dab12fa8d2bd ("drm/mediatek/dp: fix memory leak on ->get_edid callback audio detection")
---
  drivers/gpu/drm/mediatek/mtk_dp.c | 10 ++++++++--
  1 file changed, 8 insertions(+), 2 deletions(-)
drivers/gpu/drm/mediatek/mtk_dp.c +1 -0
index 536366956447..ada12927bbac 100644
@@ -2073,9 +2073,15 @@ static const struct drm_edid *mtk_dp_edid_read(struct drm_bridge *bridge,
  		 */
  		const struct edid *edid = drm_edid_raw(drm_edid);
  		struct cea_sad *sads;
+		int ret;
 
Quoted 10 lines

drivers/gpu/drm/mediatek/mtk_dp.c static const struct drm_edid *mtk_dp_edid_read(struct drm_bridge *bridge, · line 2082

Quoted 7 lines
drivers/gpu/drm/mediatek/mtk_dp.c +5 -2
-		audio_caps->sad_count = drm_edid_to_sad(edid, &sads);
-		kfree(sads);
+		ret = drm_edid_to_sad(edid, &sads);
+		/* Ignore any errors */
+		if (ret < 0)
+			ret = 0;
+		if (ret)

Eh, this will never work, because you're clearing the error before checking if there's any error here?!?! :-P

Don't get what you mean? Yes, I'm ignoring the error. Thus, in case of an error ret will be zero and there will be no free. If ret was zero, there won't be a free either. So you're left with the "normal" case, where you have to free the sads. Just like before.

Quoted 10 lines

Anyway in reality, it returns -ENOMEM if the allocation was not successful... in the event that any future update adds any other error we'd be back with the same issue, but I'm not sure how much should we worry about that.

To be extremely safe, we could do...

if (ret != -ENOMEM)
kfree(sads)

audio_caps->sad_count = ret < 0 ? 0 : ret;

Which is the same as above, but you only check for ENOMEM?

-michael

Quoted 9 lines

Cheers!
Angelo

Quoted 5 lines
drivers/gpu/drm/mediatek/mtk_dp.c +2 -0
+			kfree(sads);
+		audio_caps->sad_count = ret;
 
  		/*
  		 * FIXME: This should use connector->display_info.has_audio from
#3 lore raw

Il 17/05/24 13:07, Michael Walle ha scritto:

Quoted 42 lines · AngeloGioacchino Del Regno
Quoted 21 lines

Il 17/05/24 11:30, Michael Walle ha scritto:

Quoted 20 lines

drm_edid_to_sad() might return an error or just zero. If that is the case, we must not free the SADs because there was no allocation in the first place.

Fixes dab12fa8d2bd ("drm/mediatek/dp: fix memory leak on ->get_edid callback audio detection")
---
   drivers/gpu/drm/mediatek/mtk_dp.c | 10 ++++++++--
   1 file changed, 8 insertions(+), 2 deletions(-)
drivers/gpu/drm/mediatek/mtk_dp.c +1 -0
index 536366956447..ada12927bbac 100644
@@ -2073,9 +2073,15 @@ static const struct drm_edid *mtk_dp_edid_read(struct drm_bridge *bridge,
   		 */
   		const struct edid *edid = drm_edid_raw(drm_edid);
   		struct cea_sad *sads;
+		int ret;
 
Quoted 10 lines

drivers/gpu/drm/mediatek/mtk_dp.c static const struct drm_edid *mtk_dp_edid_read(struct drm_bridge *bridge, · line 2082

Quoted 7 lines
drivers/gpu/drm/mediatek/mtk_dp.c +5 -2
-		audio_caps->sad_count = drm_edid_to_sad(edid, &sads);
-		kfree(sads);
+		ret = drm_edid_to_sad(edid, &sads);
+		/* Ignore any errors */
+		if (ret < 0)
+			ret = 0;
+		if (ret)

Eh, this will never work, because you're clearing the error before checking if there's any error here?!?! :-P

Don't get what you mean? Yes, I'm ignoring the error. Thus, in case of an error ret will be zero and there will be no free. If ret was zero, there won't be a free either. So you're left with the "normal" case, where you have to free the sads. Just like before.

Quoted 4 lines

Anyway in reality, it returns -ENOMEM if the allocation was not successful... in the event that any future update adds any other error we'd be back with the same issue, but I'm not sure how much should we worry about that.

Quoted 9 lines
Quoted 6 lines

To be extremely safe, we could do...

if (ret != -ENOMEM)
kfree(sads)

audio_caps->sad_count = ret < 0 ? 0 : ret;

Which is the same as above, but you only check for ENOMEM?

Yes, the point is to avoid kfree(sads) for -ENOMEM only, as other errors that may be introduced later might still allocate it and leave it allocated.

Quoted 12 lines

-michael

Quoted 9 lines

Cheers!
Angelo

Quoted 5 lines
drivers/gpu/drm/mediatek/mtk_dp.c +2 -0
+			kfree(sads);
+		audio_caps->sad_count = ret;
 
   		/*
   		 * FIXME: This should use connector->display_info.has_audio from
#4 lore raw
Quoted 48 lines · AngeloGioacchino Del Regno

Il 17/05/24 13:07, Michael Walle ha scritto:

Quoted 47 lines · AngeloGioacchino Del Regno
Quoted 21 lines

Il 17/05/24 11:30, Michael Walle ha scritto:

Quoted 20 lines

drm_edid_to_sad() might return an error or just zero. If that is the case, we must not free the SADs because there was no allocation in the first place.

Fixes dab12fa8d2bd ("drm/mediatek/dp: fix memory leak on ->get_edid callback audio detection")
---
   drivers/gpu/drm/mediatek/mtk_dp.c | 10 ++++++++--
   1 file changed, 8 insertions(+), 2 deletions(-)
drivers/gpu/drm/mediatek/mtk_dp.c +1 -0
index 536366956447..ada12927bbac 100644
@@ -2073,9 +2073,15 @@ static const struct drm_edid *mtk_dp_edid_read(struct drm_bridge *bridge,
   		 */
   		const struct edid *edid = drm_edid_raw(drm_edid);
   		struct cea_sad *sads;
+		int ret;
 
Quoted 10 lines

drivers/gpu/drm/mediatek/mtk_dp.c static const struct drm_edid *mtk_dp_edid_read(struct drm_bridge *bridge, · line 2082

Quoted 7 lines
drivers/gpu/drm/mediatek/mtk_dp.c +5 -2
-		audio_caps->sad_count = drm_edid_to_sad(edid, &sads);
-		kfree(sads);
+		ret = drm_edid_to_sad(edid, &sads);
+		/* Ignore any errors */
+		if (ret < 0)
+			ret = 0;
+		if (ret)

Eh, this will never work, because you're clearing the error before checking if there's any error here?!?! :-P

Don't get what you mean? Yes, I'm ignoring the error. Thus, in case of an error ret will be zero and there will be no free. If ret was zero, there won't be a free either. So you're left with the "normal" case, where you have to free the sads. Just like before.

Quoted 9 lines

Anyway in reality, it returns -ENOMEM if the allocation was not successful... in the event that any future update adds any other error we'd be back with the same issue, but I'm not sure how much should we worry about that.

To be extremely safe, we could do...

if (ret != -ENOMEM)
kfree(sads)

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

audio_caps->sad_count = ret < 0 ? 0 : ret;

Which is the same as above, but you only check for ENOMEM?

Yes, the point is to avoid kfree(sads) for -ENOMEM only, as other errors that may be introduced later might still allocate it and leave it allocated.

Honestly, I doubt that any sane function will allocate memory, then return an error and expect the caller to free it.

-michael

maintainer Reviewed-by
#5 lore raw

Il 17/05/24 13:21, Michael Walle ha scritto:

Quoted 51 lines · AngeloGioacchino Del Regno
Quoted 50 lines

Il 17/05/24 13:07, Michael Walle ha scritto:

Quoted 49 lines · AngeloGioacchino Del Regno
Quoted 21 lines

Il 17/05/24 11:30, Michael Walle ha scritto:

Quoted 20 lines

drm_edid_to_sad() might return an error or just zero. If that is the case, we must not free the SADs because there was no allocation in the first place.

Fixes dab12fa8d2bd ("drm/mediatek/dp: fix memory leak on ->get_edid callback audio detection")
---
    drivers/gpu/drm/mediatek/mtk_dp.c | 10 ++++++++--
    1 file changed, 8 insertions(+), 2 deletions(-)
drivers/gpu/drm/mediatek/mtk_dp.c +1 -0
index 536366956447..ada12927bbac 100644
@@ -2073,9 +2073,15 @@ static const struct drm_edid *mtk_dp_edid_read(struct drm_bridge *bridge,
    		 */
    		const struct edid *edid = drm_edid_raw(drm_edid);
    		struct cea_sad *sads;
+		int ret;
 
Quoted 10 lines

drivers/gpu/drm/mediatek/mtk_dp.c static const struct drm_edid *mtk_dp_edid_read(struct drm_bridge *bridge, · line 2082

Quoted 7 lines
drivers/gpu/drm/mediatek/mtk_dp.c +5 -2
-		audio_caps->sad_count = drm_edid_to_sad(edid, &sads);
-		kfree(sads);
+		ret = drm_edid_to_sad(edid, &sads);
+		/* Ignore any errors */
+		if (ret < 0)
+			ret = 0;
+		if (ret)

Eh, this will never work, because you're clearing the error before checking if there's any error here?!?! :-P

Don't get what you mean? Yes, I'm ignoring the error. Thus, in case of an error ret will be zero and there will be no free. If ret was zero, there won't be a free either. So you're left with the "normal" case, where you have to free the sads. Just like before.

Quoted 10 lines

Anyway in reality, it returns -ENOMEM if the allocation was not successful... in the event that any future update adds any other error we'd be back with the same issue, but I'm not sure how much should we worry about that.

To be extremely safe, we could do...

if (ret != -ENOMEM)
kfree(sads)

audio_caps->sad_count = ret < 0 ? 0 : ret;

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

Which is the same as above, but you only check for ENOMEM?

Yes, the point is to avoid kfree(sads) for -ENOMEM only, as other errors that may be introduced later might still allocate it and leave it allocated.

Honestly, I doubt that any sane function will allocate memory, then return an error and expect the caller to free it.

My point was "you never know". But that wasn't a strong opinion anyway.

It's ok for me regardless of what you choose, either follow what I said or don't, the end result is the same, you're still fixing the issue and both ways are acceptable, so...

Reviewed-by on v1 1/1
#6 lore raw
In reply to Michael Walle
Quoted 15 lines · Michael Walle

drm_edid_to_sad() might return an error or just zero. If that is the case, we must not free the SADs because there was no allocation in the first place.

Fixes dab12fa8d2bd ("drm/mediatek/dp: fix memory leak on ->get_edid callback audio detection")
---
 drivers/gpu/drm/mediatek/mtk_dp.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/gpu/drm/mediatek/mtk_dp.c +0 -0
index 536366956447..ada12927bbac 100644

drivers/gpu/drm/mediatek/mtk_dp.c static const struct drm_edid *mtk_dp_edid_read(struct drm_bridge *bridge, · line 2075

Quoted 3 lines
drivers/gpu/drm/mediatek/mtk_dp.c +0 -0
@@ -2073,9 +2073,15 @@ static const struct drm_edid *mtk_dp_edid_read(struct drm_bridge *bridge,
 		 */
 		const struct edid *edid = drm_edid_raw(drm_edid);
 		struct cea_sad *sads;

I suppose I would've just initialized sads = NULL; and be done with it.

But *shrug*.

Quoted 14 lines
drivers/gpu/drm/mediatek/mtk_dp.c +8 -2
+		int ret;
 
-		audio_caps->sad_count = drm_edid_to_sad(edid, &sads);
-		kfree(sads);
+		ret = drm_edid_to_sad(edid, &sads);
+		/* Ignore any errors */
+		if (ret < 0)
+			ret = 0;
+		if (ret)
+			kfree(sads);
+		audio_caps->sad_count = ret;
 
 		/*
 		 * FIXME: This should use connector->display_info.has_audio from
Signature · 1 line
Jani Nikula, Intel