Skip to contentSkip to conversation

drm/radeon: fix dangling pointer kfree

PATCH New Henning Schild 2 participants 6 messages Oct 11, 2014 – Oct 13, 2014

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

PATCH 1/1 drm/radeon: fix dangling pointer kfree

If drm_edid_to_speaker_allocation returns a count of 0 sadb is not initialized and should not get kfreed. Bail out like the other two callers.

---
 drivers/gpu/drm/radeon/dce3_1_afmt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/radeon/dce3_1_afmt.c +1 -1
index 51800e3..cb76074 100644
@@ -48,7 +48,7 @@ static void dce3_2_afmt_write_speaker_allocation(struct drm_encoder *encoder)
 	}
 
 	sad_count = drm_edid_to_speaker_allocation(radeon_connector->edid, &sadb);
-	if (sad_count < 0) {
+	if (sad_count <= 0) {
 		DRM_ERROR("Couldn't read Speaker Allocation Data Block: %d\n", sad_count);
 		return;
 	}
Signature · 1 line
2.0.4
#1 lore raw

PATCH 1/1 drm/radeon: remove code that can never get executed

Removing a code-path that can never be executed ... and its copies. If drm_edid_to_speaker_allocation returns 0 the callers return. There is no need to check that condition again.

---
 drivers/gpu/drm/radeon/dce3_1_afmt.c    | 5 +----
 drivers/gpu/drm/radeon/dce6_afmt.c      | 5 +----
 drivers/gpu/drm/radeon/evergreen_hdmi.c | 5 +----
 3 files changed, 3 insertions(+), 12 deletions(-)
drivers/gpu/drm/radeon/dce3_1_afmt.c +1 -4
index cb76074..6d31ed8 100644
@@ -58,10 +58,7 @@ static void dce3_2_afmt_write_speaker_allocation(struct drm_encoder *encoder)
 	tmp &= ~(DP_CONNECTION | SPEAKER_ALLOCATION_MASK);
 	/* set HDMI mode */
 	tmp |= HDMI_CONNECTION;
-	if (sad_count)
-		tmp |= SPEAKER_ALLOCATION(sadb[0]);
-	else
-		tmp |= SPEAKER_ALLOCATION(5); /* stereo */
+	tmp |= SPEAKER_ALLOCATION(sadb[0]);
 	WREG32(AZ_F0_CODEC_PIN0_CONTROL_CHANNEL_SPEAKER, tmp);
 
 	kfree(sadb);
drivers/gpu/drm/radeon/dce6_afmt.c +1 -4
index ab29f95..e6b2750 100644
@@ -186,10 +186,7 @@ void dce6_afmt_write_speaker_allocation(struct drm_encoder *encoder)
 	tmp &= ~(DP_CONNECTION | SPEAKER_ALLOCATION_MASK);
 	/* set HDMI mode */
 	tmp |= HDMI_CONNECTION;
-	if (sad_count)
-		tmp |= SPEAKER_ALLOCATION(sadb[0]);
-	else
-		tmp |= SPEAKER_ALLOCATION(5); /* stereo */
+	tmp |= SPEAKER_ALLOCATION(sadb[0]);
 	WREG32_ENDPOINT(offset, AZ_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER, tmp);
 
 	kfree(sadb);
drivers/gpu/drm/radeon/evergreen_hdmi.c +1 -4
index 278c7a1..11a6b65 100644
@@ -128,10 +128,7 @@ static void dce4_afmt_write_speaker_allocation(struct drm_encoder *encoder)
 	tmp &= ~(DP_CONNECTION | SPEAKER_ALLOCATION_MASK);
 	/* set HDMI mode */
 	tmp |= HDMI_CONNECTION;
-	if (sad_count)
-		tmp |= SPEAKER_ALLOCATION(sadb[0]);
-	else
-		tmp |= SPEAKER_ALLOCATION(5); /* stereo */
+	tmp |= SPEAKER_ALLOCATION(sadb[0]);
 	WREG32(AZ_F0_CODEC_PIN0_CONTROL_CHANNEL_SPEAKER, tmp);
 
 	kfree(sadb);
Signature · 1 line
2.0.4
#2 lore raw

RE: [PATCH] drm/radeon: remove code that can never get executed

Quoted 10 lines

-----Original Message-----
From: Henning Schild [mailto:[email protected]]
Sent: Saturday, October 11, 2014 8:51 AM
To: [email protected]
Cc: Henning Schild; Deucher, Alexander; Rafał Miłecki
Subject: [PATCH] drm/radeon: remove code that can never get executed

Removing a code-path that can never be executed ... and its copies. If drm_edid_to_speaker_allocation returns 0 the callers return. There is no need to check that condition again.

I think we actually want to set the speaker allocation setup to stereo if the speaker allocation block is not present so I think the attached patch is probably the proper fix.

Alex

Quoted 62 lines
---
 drivers/gpu/drm/radeon/dce3_1_afmt.c    | 5 +----
 drivers/gpu/drm/radeon/dce6_afmt.c      | 5 +----
 drivers/gpu/drm/radeon/evergreen_hdmi.c | 5 +----
 3 files changed, 3 insertions(+), 12 deletions(-)
drivers/gpu/drm/radeon/dce3_1_afmt.c +1 -4
index cb76074..6d31ed8 100644
@@ -58,10 +58,7 @@ static void
 dce3_2_afmt_write_speaker_allocation(struct drm_encoder *encoder)
 	tmp &= ~(DP_CONNECTION | SPEAKER_ALLOCATION_MASK);
 	/* set HDMI mode */
 	tmp |= HDMI_CONNECTION;
-	if (sad_count)
-		tmp |= SPEAKER_ALLOCATION(sadb[0]);
-	else
-		tmp |= SPEAKER_ALLOCATION(5); /* stereo */
+	tmp |= SPEAKER_ALLOCATION(sadb[0]);
 	WREG32(AZ_F0_CODEC_PIN0_CONTROL_CHANNEL_SPEAKER, tmp);
 
drivers/gpu/drm/radeon/dce3_1_afmt.c +0 -0
 	kfree(sadb);
drivers/gpu/drm/radeon/dce6_afmt.c +1 -4
index ab29f95..e6b2750 100644
@@ -186,10 +186,7 @@ void dce6_afmt_write_speaker_allocation(struct
 drm_encoder *encoder)
 	tmp &= ~(DP_CONNECTION | SPEAKER_ALLOCATION_MASK);
 	/* set HDMI mode */
 	tmp |= HDMI_CONNECTION;
-	if (sad_count)
-		tmp |= SPEAKER_ALLOCATION(sadb[0]);
-	else
-		tmp |= SPEAKER_ALLOCATION(5); /* stereo */
+	tmp |= SPEAKER_ALLOCATION(sadb[0]);
 	WREG32_ENDPOINT(offset,
 AZ_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER, tmp);
drivers/gpu/drm/radeon/dce6_afmt.c +0 -0
 	kfree(sadb);
drivers/gpu/drm/radeon/evergreen_hdmi.c +1 -4
index 278c7a1..11a6b65 100644
@@ -128,10 +128,7 @@ static void
 dce4_afmt_write_speaker_allocation(struct drm_encoder *encoder)
 	tmp &= ~(DP_CONNECTION | SPEAKER_ALLOCATION_MASK);
 	/* set HDMI mode */
 	tmp |= HDMI_CONNECTION;
-	if (sad_count)
-		tmp |= SPEAKER_ALLOCATION(sadb[0]);
-	else
-		tmp |= SPEAKER_ALLOCATION(5); /* stereo */
+	tmp |= SPEAKER_ALLOCATION(sadb[0]);
 	WREG32(AZ_F0_CODEC_PIN0_CONTROL_CHANNEL_SPEAKER, tmp);
 
Quoted 3 lines

kfree(sadb);
--
2.0.4

From ab8d0add442bbd7d9b96a04909c8a233628cef2f Mon Sep 17 00:00:00 2001
From: Alex Deucher <[email protected]>
Date: Mon, 13 Oct 2014 11:51:50 -0400
Subject: [PATCH] drm/radeon: fix speaker allocation setup

If the sad_count is 0, set the hw to stereo and change the error message to a warn. A lot of monitors don't set the speaker allocation block.

---
 drivers/gpu/drm/radeon/dce3_1_afmt.c    | 4 ++--
 drivers/gpu/drm/radeon/dce6_afmt.c      | 6 +++---
 drivers/gpu/drm/radeon/evergreen_hdmi.c | 6 +++---
 3 files changed, 8 insertions(+), 8 deletions(-)
drivers/gpu/drm/radeon/dce3_1_afmt.c +2 -2
index 950af15..adf65c0 100644
@@ -49,8 +49,8 @@ static void dce3_2_afmt_write_speaker_allocation(struct drm_encoder *encoder)
 
 	sad_count = drm_edid_to_speaker_allocation(radeon_connector->edid, &sadb);
 	if (sad_count < 0) {
-		DRM_ERROR("Couldn't read Speaker Allocation Data Block: %d\n", sad_count);
-		return;
+		DRM_DEBUG("Couldn't read Speaker Allocation Data Block: %d\n", sad_count);
+		sad_count = 0;
 	}
 
 	/* program the speaker allocation */
drivers/gpu/drm/radeon/dce6_afmt.c +3 -3
index c0bbf68..bdc3333 100644
@@ -176,9 +176,9 @@ void dce6_afmt_write_speaker_allocation(struct drm_encoder *encoder)
 	}
 
 	sad_count = drm_edid_to_speaker_allocation(radeon_connector_edid(connector), &sadb);
-	if (sad_count <= 0) {
-		DRM_ERROR("Couldn't read Speaker Allocation Data Block: %d\n", sad_count);
-		return;
+	if (sad_count < 0) {
+		DRM_DEBUG("Couldn't read Speaker Allocation Data Block: %d\n", sad_count);
+		sad_count = 0;
 	}
 
 	/* program the speaker allocation */
drivers/gpu/drm/radeon/evergreen_hdmi.c +3 -3
index 2514d65..ba942b0 100644
@@ -149,9 +149,9 @@ static void dce4_afmt_write_speaker_allocation(struct drm_encoder *encoder)
 	}
 
 	sad_count = drm_edid_to_speaker_allocation(radeon_connector_edid(connector), &sadb);
-	if (sad_count <= 0) {
-		DRM_ERROR("Couldn't read Speaker Allocation Data Block: %d\n", sad_count);
-		return;
+	if (sad_count < 0) {
+		DRM_DEBUG("Couldn't read Speaker Allocation Data Block: %d\n", sad_count);
+		sad_count = 0;
 	}
 
 	/* program the speaker allocation */
Signature · 1 line
1.8.3.1
#3 lore raw

RE: [PATCH] drm/radeon: remove code that can never get executed

Well i am not sure what the code actually is supposed to do. That is up to you because you know the hardware. I just wanted to point out that right now some code can never be reached.

I ran into the whole thing because the pointer (sadp) that gets kfreed was never initialized (in one of the three cases). My first fix was to initialize it to NULL, that way the kfree did not fail even if the allocation did not do anything. Then i found the other two copies and decided to follow their scheme.

In your patch you have to make kfree depend on (sad_count > 0) or sadp needs to be initialized with NULL. Otherwise you will get the dangling pointer kfree that lead me to read this code.

Henning

Quoted 87 lines
Quoted 10 lines · Alexander

-----Original Message-----
From: Henning Schild [mailto:[email protected]]
Sent: Saturday, October 11, 2014 8:51 AM
To: [email protected]
Cc: Henning Schild; Deucher, Alexander; Rafał Miłecki
Subject: [PATCH] drm/radeon: remove code that can never get executed

Removing a code-path that can never be executed ... and its copies. If drm_edid_to_speaker_allocation returns 0 the callers return. There is no need to check that condition again.

I think we actually want to set the speaker allocation setup to stereo if the speaker allocation block is not present so I think the attached patch is probably the proper fix.

Alex

Quoted 65 lines

---
drivers/gpu/drm/radeon/dce3_1_afmt.c | 5 +----
drivers/gpu/drm/radeon/dce6_afmt.c | 5 +----
drivers/gpu/drm/radeon/evergreen_hdmi.c | 5 +----

3 files changed, 3 insertions(+), 12 deletions(-)
drivers/gpu/drm/radeon/dce3_1_afmt.c +1 -4
index cb76074..6d31ed8 100644
@@ -58,10 +58,7 @@ static void
 dce3_2_afmt_write_speaker_allocation(struct drm_encoder *encoder)
 tmp &= ~(DP_CONNECTION | SPEAKER_ALLOCATION_MASK);
 /* set HDMI mode */
 tmp |= HDMI_CONNECTION;
- if (sad_count)
- tmp |= SPEAKER_ALLOCATION(sadb[0]);
- else
- tmp |= SPEAKER_ALLOCATION(5); /* stereo */
+ tmp |= SPEAKER_ALLOCATION(sadb[0]);
 WREG32(AZ_F0_CODEC_PIN0_CONTROL_CHANNEL_SPEAKER, tmp);
 

kfree(sadb);

drivers/gpu/drm/radeon/dce6_afmt.c +1 -4
index ab29f95..e6b2750 100644
@@ -186,10 +186,7 @@ void dce6_afmt_write_speaker_allocation(struct
 drm_encoder *encoder)
 tmp &= ~(DP_CONNECTION | SPEAKER_ALLOCATION_MASK);
 /* set HDMI mode */
 tmp |= HDMI_CONNECTION;
- if (sad_count)
- tmp |= SPEAKER_ALLOCATION(sadb[0]);
- else
- tmp |= SPEAKER_ALLOCATION(5); /* stereo */
+ tmp |= SPEAKER_ALLOCATION(sadb[0]);
 WREG32_ENDPOINT(offset,
 AZ_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER, tmp);

kfree(sadb);

drivers/gpu/drm/radeon/evergreen_hdmi.c +1 -4
index 278c7a1..11a6b65 100644
@@ -128,10 +128,7 @@ static void
 dce4_afmt_write_speaker_allocation(struct drm_encoder *encoder)
 tmp &= ~(DP_CONNECTION | SPEAKER_ALLOCATION_MASK);
 /* set HDMI mode */
 tmp |= HDMI_CONNECTION;
- if (sad_count)
- tmp |= SPEAKER_ALLOCATION(sadb[0]);
- else
- tmp |= SPEAKER_ALLOCATION(5); /* stereo */
+ tmp |= SPEAKER_ALLOCATION(sadb[0]);
 WREG32(AZ_F0_CODEC_PIN0_CONTROL_CHANNEL_SPEAKER, tmp);
 

kfree(sadb);
--
2.0.4

#4 lore raw

RE: [PATCH] drm/radeon: remove code that can never get executed

Quoted 19 lines

-----Original Message-----
From: Henning Schild [mailto:[email protected]]
Sent: Monday, October 13, 2014 12:44 PM
To: Deucher, Alexander; [email protected]
Cc: Rafał Miłecki
Subject: RE: [PATCH] drm/radeon: remove code that can never get executed

Well i am not sure what the code actually is supposed to do. That is up to you because you know the hardware. I just wanted to point out that right now some
code can never be reached.

I ran into the whole thing because the pointer (sadp) that gets kfreed was never
initialized (in one of the three cases). My first fix was to initialize it to NULL, that way the kfree did not fail even if the allocation did not do anything. Then i found the other two copies and decided to follow their scheme.

Quoted 6 lines

In your patch you have to make kfree depend on (sad_count > 0) or sadp needs to
be initialized with NULL. Otherwise you will get the dangling pointer kfree that
lead me to read this code.

Ah, ok. The attached patches should do the trick then.

Alex

Quoted 89 lines

Henning

Quoted 87 lines
Quoted 10 lines · Alexander

-----Original Message-----
From: Henning Schild [mailto:[email protected]]
Sent: Saturday, October 11, 2014 8:51 AM
To: [email protected]
Cc: Henning Schild; Deucher, Alexander; Rafał Miłecki
Subject: [PATCH] drm/radeon: remove code that can never get executed

Removing a code-path that can never be executed ... and its copies. If drm_edid_to_speaker_allocation returns 0 the callers return. There is no need to check that condition again.

I think we actually want to set the speaker allocation setup to stereo if the speaker allocation block is not present so I think the attached patch is probably the proper fix.

Alex

Quoted 65 lines

---
drivers/gpu/drm/radeon/dce3_1_afmt.c | 5 +----
drivers/gpu/drm/radeon/dce6_afmt.c | 5 +----
drivers/gpu/drm/radeon/evergreen_hdmi.c | 5 +----

3 files changed, 3 insertions(+), 12 deletions(-)
drivers/gpu/drm/radeon/dce3_1_afmt.c +1 -4
index cb76074..6d31ed8 100644
@@ -58,10 +58,7 @@ static void
 dce3_2_afmt_write_speaker_allocation(struct drm_encoder *encoder)
 tmp &= ~(DP_CONNECTION | SPEAKER_ALLOCATION_MASK);
 /* set HDMI mode */
 tmp |= HDMI_CONNECTION;
- if (sad_count)
- tmp |= SPEAKER_ALLOCATION(sadb[0]);
- else
- tmp |= SPEAKER_ALLOCATION(5); /* stereo */
+ tmp |= SPEAKER_ALLOCATION(sadb[0]);
 WREG32(AZ_F0_CODEC_PIN0_CONTROL_CHANNEL_SPEAKER, tmp);
 

kfree(sadb);

drivers/gpu/drm/radeon/dce6_afmt.c +1 -4
index ab29f95..e6b2750 100644
@@ -186,10 +186,7 @@ void dce6_afmt_write_speaker_allocation(struct
 drm_encoder *encoder)
 tmp &= ~(DP_CONNECTION | SPEAKER_ALLOCATION_MASK);
 /* set HDMI mode */
 tmp |= HDMI_CONNECTION;
- if (sad_count)
- tmp |= SPEAKER_ALLOCATION(sadb[0]);
- else
- tmp |= SPEAKER_ALLOCATION(5); /* stereo */
+ tmp |= SPEAKER_ALLOCATION(sadb[0]);
 WREG32_ENDPOINT(offset,
 AZ_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER, tmp);

kfree(sadb);

drivers/gpu/drm/radeon/evergreen_hdmi.c +1 -4
index 278c7a1..11a6b65 100644
@@ -128,10 +128,7 @@ static void
 dce4_afmt_write_speaker_allocation(struct drm_encoder *encoder)
 tmp &= ~(DP_CONNECTION | SPEAKER_ALLOCATION_MASK);
 /* set HDMI mode */
 tmp |= HDMI_CONNECTION;
- if (sad_count)
- tmp |= SPEAKER_ALLOCATION(sadb[0]);
- else
- tmp |= SPEAKER_ALLOCATION(5); /* stereo */
+ tmp |= SPEAKER_ALLOCATION(sadb[0]);
 WREG32(AZ_F0_CODEC_PIN0_CONTROL_CHANNEL_SPEAKER, tmp);
 

kfree(sadb);
--
2.0.4

From 432aefea7cf48d31ea665cac13de71af8036bb55 Mon Sep 17 00:00:00 2001
From: Alex Deucher <[email protected]>
Date: Mon, 13 Oct 2014 13:23:48 -0400
Subject: [PATCH 1/2] drm/radeon: initialize sadb to NULL in the audio code

Fixes kfree of the sadb buffer when it's NULL.

---
 drivers/gpu/drm/radeon/dce3_1_afmt.c    | 2 +-
 drivers/gpu/drm/radeon/dce6_afmt.c      | 2 +-
 drivers/gpu/drm/radeon/evergreen_hdmi.c | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/radeon/dce3_1_afmt.c +1 -1
index 950af15..6b1dbec 100644
@@ -32,7 +32,7 @@ static void dce3_2_afmt_write_speaker_allocation(struct drm_encoder *encoder)
 	struct drm_connector *connector;
 	struct radeon_connector *radeon_connector = NULL;
 	u32 tmp;
-	u8 *sadb;
+	u8 *sadb = NULL;
 	int sad_count;
 
 	list_for_each_entry(connector, &encoder->dev->mode_config.connector_list, head) {
drivers/gpu/drm/radeon/dce6_afmt.c +1 -1
index c0bbf68..960a5f0 100644
@@ -155,7 +155,7 @@ void dce6_afmt_write_speaker_allocation(struct drm_encoder *encoder)
 	struct drm_connector *connector;
 	struct radeon_connector *radeon_connector = NULL;
 	u32 offset, tmp;
-	u8 *sadb;
+	u8 *sadb = NULL;
 	int sad_count;
 
 	if (!dig || !dig->afmt || !dig->afmt->pin)
drivers/gpu/drm/radeon/evergreen_hdmi.c +1 -1
index 2514d65..f6a5c30 100644
@@ -133,7 +133,7 @@ static void dce4_afmt_write_speaker_allocation(struct drm_encoder *encoder)
 	struct drm_connector *connector;
 	struct radeon_connector *radeon_connector = NULL;
 	u32 tmp;
-	u8 *sadb;
+	u8 *sadb = NULL;
 	int sad_count;
 
 	list_for_each_entry(connector, &encoder->dev->mode_config.connector_list, head) {
Signature · 1 line
1.8.3.1

From 3412efaaf01eab20d5570f5b4818db928644f0db Mon Sep 17 00:00:00 2001
From: Alex Deucher <[email protected]>
Date: Mon, 13 Oct 2014 11:51:50 -0400
Subject: [PATCH 2/2] drm/radeon: fix speaker allocation setup

If the sad_count is 0, set the hw to stereo and change the error message to a warn. A lot of monitors don't set the speaker allocation block.

---
 drivers/gpu/drm/radeon/dce3_1_afmt.c    | 4 ++--
 drivers/gpu/drm/radeon/dce6_afmt.c      | 6 +++---
 drivers/gpu/drm/radeon/evergreen_hdmi.c | 6 +++---
 3 files changed, 8 insertions(+), 8 deletions(-)
drivers/gpu/drm/radeon/dce3_1_afmt.c +2 -2
index 6b1dbec..2fe8cfc 100644
@@ -49,8 +49,8 @@ static void dce3_2_afmt_write_speaker_allocation(struct drm_encoder *encoder)
 
 	sad_count = drm_edid_to_speaker_allocation(radeon_connector->edid, &sadb);
 	if (sad_count < 0) {
-		DRM_ERROR("Couldn't read Speaker Allocation Data Block: %d\n", sad_count);
-		return;
+		DRM_DEBUG("Couldn't read Speaker Allocation Data Block: %d\n", sad_count);
+		sad_count = 0;
 	}
 
 	/* program the speaker allocation */
drivers/gpu/drm/radeon/dce6_afmt.c +3 -3
index 960a5f0..f312edf 100644
@@ -176,9 +176,9 @@ void dce6_afmt_write_speaker_allocation(struct drm_encoder *encoder)
 	}
 
 	sad_count = drm_edid_to_speaker_allocation(radeon_connector_edid(connector), &sadb);
-	if (sad_count <= 0) {
-		DRM_ERROR("Couldn't read Speaker Allocation Data Block: %d\n", sad_count);
-		return;
+	if (sad_count < 0) {
+		DRM_DEBUG("Couldn't read Speaker Allocation Data Block: %d\n", sad_count);
+		sad_count = 0;
 	}
 
 	/* program the speaker allocation */
drivers/gpu/drm/radeon/evergreen_hdmi.c +3 -3
index f6a5c30..53abd9b 100644
@@ -149,9 +149,9 @@ static void dce4_afmt_write_speaker_allocation(struct drm_encoder *encoder)
 	}
 
 	sad_count = drm_edid_to_speaker_allocation(radeon_connector_edid(connector), &sadb);
-	if (sad_count <= 0) {
-		DRM_ERROR("Couldn't read Speaker Allocation Data Block: %d\n", sad_count);
-		return;
+	if (sad_count < 0) {
+		DRM_DEBUG("Couldn't read Speaker Allocation Data Block: %d\n", sad_count);
+		sad_count = 0;
 	}
 
 	/* program the speaker allocation */
Signature · 1 line
1.8.3.1
#5 lore raw

Re: [PATCH] drm/radeon: remove code that can never get executed

Quoted 1 line · Alexander

Ah, ok. The attached patches should do the trick then.

Yes that would do the trick. But i suggest doing the NULL initialization in drm_edid_to_speaker_allocation.
The kernel doc suggests that whatever drm_edid_to_speaker_allocation returns should be freed. Depending on which version you prefer, the doc might have to be changed as well.

Henning