Skip to contentSkip to conversation

drm/amdgpu: properly handle vbios fake edid sizing

PATCH Superseded Alex Deucher 2 participants 5 messages Jul 23, 2024 – Jul 24, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Alex Deucher

author maintainer
#0 lore raw

PATCH 1/2 drm/amdgpu: properly handle vbios fake edid sizing

The comment in the vbios structure says:
// = 128 means EDID length is 128 bytes, otherwise the EDID length = ucFakeEDIDLength*128

This fake edid struct has not been used in a long time, so I'm not sure if there were actually any boards out there with a non-128 byte EDID, but align the code with the comment.

---
 .../gpu/drm/amd/amdgpu/atombios_encoders.c    | 24 +++++++++++--------
 1 file changed, 14 insertions(+), 10 deletions(-)
drivers/gpu/drm/amd/amdgpu/atombios_encoders.c +14 -10
index 25feab188dfe..a8751a5901c6 100644
@@ -2065,12 +2065,16 @@ amdgpu_atombios_encoder_get_lcd_info(struct amdgpu_encoder *encoder)
 					fake_edid_record = (ATOM_FAKE_EDID_PATCH_RECORD *)record;
 					if (fake_edid_record->ucFakeEDIDLength) {
 						struct edid *edid;
-						int edid_size =
-							max((int)EDID_LENGTH, (int)fake_edid_record->ucFakeEDIDLength);
-						edid = kmalloc(edid_size, GFP_KERNEL);
+						int edid_size;
+
+						if (fake_edid_record->ucFakeEDIDLength == 128)
+							edid_size = fake_edid_record->ucFakeEDIDLength;
+						else
+							edid_size = fake_edid_record->ucFakeEDIDLength * 128;
+						edid = kmalloc(max(EDID_LENGTH, edid_size), GFP_KERNEL);
 						if (edid) {
 							memcpy((u8 *)edid, (u8 *)&fake_edid_record->ucFakeEDIDString[0],
-							       fake_edid_record->ucFakeEDIDLength);
+							       edid_size);
 
 							if (drm_edid_is_valid(edid)) {
 								adev->mode_info.bios_hardcoded_edid = edid;
@@ -2078,13 +2082,13 @@ amdgpu_atombios_encoder_get_lcd_info(struct amdgpu_encoder *encoder)
 							} else
 								kfree(edid);
 						}
+						record += struct_size(fake_edid_record,
+								      ucFakeEDIDString,
+								      edid_size);
+					} else {
+						/* empty fake edid record must be 3 bytes long */
+						record += sizeof(ATOM_FAKE_EDID_PATCH_RECORD) + 1;
 					}
-					record += fake_edid_record->ucFakeEDIDLength ?
-						  struct_size(fake_edid_record,
-							      ucFakeEDIDString,
-							      fake_edid_record->ucFakeEDIDLength) :
-						  /* empty fake edid record must be 3 bytes long */
-						  sizeof(ATOM_FAKE_EDID_PATCH_RECORD) + 1;
 					break;
 				case LCD_PANEL_RESOLUTION_RECORD_TYPE:
 					panel_res_record = (ATOM_PANEL_RESOLUTION_PATCH_RECORD *)record;
Signature · 1 line
2.45.2
author maintainer
#1 lore raw

PATCH 2/2 drm/radeon: properly handle vbios fake edid sizing

The comment in the vbios structure says:
// = 128 means EDID length is 128 bytes, otherwise the EDID length = ucFakeEDIDLength*128

This fake edid struct has not been used in a long time, so I'm not sure if there were actually any boards out there with a non-128 byte EDID, but align the code with the comment.

---
 drivers/gpu/drm/radeon/radeon_atombios.c | 27 +++++++++++++-----------
 1 file changed, 15 insertions(+), 12 deletions(-)
drivers/gpu/drm/radeon/radeon_atombios.c +15 -12
index 97c4e10d0550..da8f8686bb9d 100644
@@ -1714,15 +1714,18 @@ struct radeon_encoder_atom_dig *radeon_atombios_get_lvds_info(struct
 					record += sizeof(ATOM_LCD_MODE_CONTROL_CAP);
 					break;
 				case LCD_FAKE_EDID_PATCH_RECORD_TYPE:
-					fake_edid_record = (ATOM_FAKE_EDID_PATCH_RECORD *)record;
-					if (fake_edid_record->ucFakeEDIDLength) {
+										if (fake_edid_record->ucFakeEDIDLength) {
 						struct edid *edid;
-						int edid_size =
-							max((int)EDID_LENGTH, (int)fake_edid_record->ucFakeEDIDLength);
-						edid = kmalloc(edid_size, GFP_KERNEL);
+						int edid_size;
+
+						if (fake_edid_record->ucFakeEDIDLength == 128)
+							edid_size = fake_edid_record->ucFakeEDIDLength;
+						else
+							edid_size = fake_edid_record->ucFakeEDIDLength * 128;
+						edid = kmalloc(max(EDID_LENGTH, edid_size), GFP_KERNEL);
 						if (edid) {
 							memcpy((u8 *)edid, (u8 *)&fake_edid_record->ucFakeEDIDString[0],
-							       fake_edid_record->ucFakeEDIDLength);
+							       edid_size);
 
 							if (drm_edid_is_valid(edid)) {
 								rdev->mode_info.bios_hardcoded_edid = edid;
@@ -1730,13 +1733,13 @@ struct radeon_encoder_atom_dig *radeon_atombios_get_lvds_info(struct
 							} else
 								kfree(edid);
 						}
+						record += struct_size(fake_edid_record,
+								      ucFakeEDIDString,
+								      edid_size);
+					} else {
+						/* empty fake edid record must be 3 bytes long */
+						record += sizeof(ATOM_FAKE_EDID_PATCH_RECORD) + 1;
 					}
-					record += fake_edid_record->ucFakeEDIDLength ?
-						  struct_size(fake_edid_record,
-							      ucFakeEDIDString,
-							      fake_edid_record->ucFakeEDIDLength) :
-						  /* empty fake edid record must be 3 bytes long */
-						  sizeof(ATOM_FAKE_EDID_PATCH_RECORD) + 1;
 					break;
 				case LCD_PANEL_RESOLUTION_RECORD_TYPE:
 					panel_res_record = (ATOM_PANEL_RESOLUTION_PATCH_RECORD *)record;
Signature · 1 line
2.45.2
#2 lore raw
In reply to Alex Deucher
Quoted 8 lines · Alex Deucher

The comment in the vbios structure says:
// = 128 means EDID length is 128 bytes, otherwise the EDID length = ucFakeEDIDLength*128

This fake edid struct has not been used in a long time, so I'm not sure if there were actually any boards out there with a non-128 byte EDID, but align the code with the comment.

Afaik Reported-by: should also have a Link:.

And IMO a Fixes: would also be fitting.

Quoted 13 lines
---
 .../gpu/drm/amd/amdgpu/atombios_encoders.c    | 24 +++++++++++--------
 1 file changed, 14 insertions(+), 10 deletions(-)
drivers/gpu/drm/amd/amdgpu/atombios_encoders.c +0 -0
index 25feab188dfe..a8751a5901c6 100644
@@ -2065,12 +2065,16 @@ amdgpu_atombios_encoder_get_lcd_info(struct amdgpu_encoder *encoder)
 					fake_edid_record = (ATOM_FAKE_EDID_PATCH_RECORD *)record;
 					if (fake_edid_record->ucFakeEDIDLength) {
 						struct edid *edid;

drivers/gpu/drm/amd/amdgpu/atombios_encoders.c amdgpu_atombios_encoder_get_lcd_info(struct amdgpu_encoder *encoder) · line 2074

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/atombios_encoders.c +7 -3
-						int edid_size =
-							max((int)EDID_LENGTH, (int)fake_edid_record->ucFakeEDIDLength);
-						edid = kmalloc(edid_size, GFP_KERNEL);
+						int edid_size;
+
+						if (fake_edid_record->ucFakeEDIDLength == 128)
+							edid_size = fake_edid_record->ucFakeEDIDLength;
+						else
+							edid_size = fake_edid_record->ucFakeEDIDLength * 128;
+						edid = kmalloc(max(EDID_LENGTH, edid_size), GFP_KERNEL);

This looks wrong, but it was similar before.
If the EDID contains extensions and the code tries to access those, it will be an out of bounds memory access, because the extensions were not actually copied.
(And this seems to already happen in drm_edid_is_valid())

This code will go away soon with my patch, but at least we can now figure out what to do on EDIDs with extensions instead of changing the behaviour just as a side-effect.

Is there any issue with just dropping the max() and keeping the full EDID?

Also if this is touched anyways, it could become kmemdup().

Quoted 30 lines
drivers/gpu/drm/amd/amdgpu/atombios_encoders.c +7 -7
 						if (edid) {
 							memcpy((u8 *)edid, (u8 *)&fake_edid_record->ucFakeEDIDString[0],
-							       fake_edid_record->ucFakeEDIDLength);
+							       edid_size);
 
 							if (drm_edid_is_valid(edid)) {
 								adev->mode_info.bios_hardcoded_edid = edid;
@@ -2078,13 +2082,13 @@ amdgpu_atombios_encoder_get_lcd_info(struct amdgpu_encoder *encoder)
 							} else
 								kfree(edid);
 						}
+						record += struct_size(fake_edid_record,
+								      ucFakeEDIDString,
+								      edid_size);
+					} else {
+						/* empty fake edid record must be 3 bytes long */
+						record += sizeof(ATOM_FAKE_EDID_PATCH_RECORD) + 1;
 					}
-					record += fake_edid_record->ucFakeEDIDLength ?
-						  struct_size(fake_edid_record,
-							      ucFakeEDIDString,
-							      fake_edid_record->ucFakeEDIDLength) :
-						  /* empty fake edid record must be 3 bytes long */
-						  sizeof(ATOM_FAKE_EDID_PATCH_RECORD) + 1;
 					break;
 				case LCD_PANEL_RESOLUTION_RECORD_TYPE:
 					panel_res_record = (ATOM_PANEL_RESOLUTION_PATCH_RECORD *)record;
Signature · 1 line
2.45.2
author maintainer
#3 lore raw

[Public]

Quoted 16 lines

-----Original Message-----
From: Thomas Weißschuh <[email protected]>
Sent: Tuesday, July 23, 2024 1:58 PM
To: Deucher, Alexander <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH 1/2] drm/amdgpu: properly handle vbios fake edid sizing

Quoted 8 lines · Alex Deucher

The comment in the vbios structure says:
// = 128 means EDID length is 128 bytes, otherwise the EDID length = ucFakeEDIDLength*128

This fake edid struct has not been used in a long time, so I'm not sure if there were actually any boards out there with a non-128 byte EDID, but align the code with the comment.

Quoted 5 lines
Quoted 1 line

Afaik Reported-by: should also have a Link:.

And IMO a Fixes: would also be fitting.

I can add those.

Quoted 42 lines
Quoted 4 lines
---
 .../gpu/drm/amd/amdgpu/atombios_encoders.c    | 24 +++++++++++------

--

drivers/gpu/drm/amd/amdgpu/atombios_encoders.c

Quoted 8 lines
 1 file changed, 14 insertions(+), 10 deletions(-)
drivers/gpu/drm/amd/amdgpu/atombios_encoders.c +0 -0
index 25feab188dfe..a8751a5901c6 100644
@@ -2065,12 +2065,16 @@

amdgpu_atombios_encoder_get_lcd_info(struct amdgpu_encoder *encoder)

drivers/gpu/drm/amd/amdgpu/atombios_encoders.c line 2065

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/atombios_encoders.c +0 -0
                                    fake_edid_record =

(ATOM_FAKE_EDID_PATCH_RECORD *)record;

drivers/gpu/drm/amd/amdgpu/atombios_encoders.c line 2070

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/atombios_encoders.c +0 -2
                                    if (fake_edid_record-
 ucFakeEDIDLength) {
                                            struct edid *edid;
-                                           int edid_size =
-
      max((int)EDID_LENGTH, (int)fake_edid_record->ucFakeEDIDLength);

drivers/gpu/drm/amd/amdgpu/atombios_encoders.c line 2071

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/atombios_encoders.c +0 -1
-                                           edid = kmalloc(edid_size,

GFP_KERNEL);

drivers/gpu/drm/amd/amdgpu/atombios_encoders.c line 2073

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/atombios_encoders.c +4 -0
+                                           int edid_size;
+
+                                           if (fake_edid_record-
 ucFakeEDIDLength == 128)
+                                                   edid_size =

fake_edid_record->ucFakeEDIDLength;

drivers/gpu/drm/amd/amdgpu/atombios_encoders.c line 2075

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/atombios_encoders.c +2 -0
+                                           else
+                                                   edid_size =

fake_edid_record->ucFakeEDIDLength * 128;

drivers/gpu/drm/amd/amdgpu/atombios_encoders.c line 2076

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/atombios_encoders.c +1 -0
+                                           edid =

kmalloc(max(EDID_LENGTH, edid_size), GFP_KERNEL);

This looks wrong, but it was similar before.
If the EDID contains extensions and the code tries to access those, it will be an out of bounds memory access, because the extensions were not actually copied.
(And this seems to already happen in drm_edid_is_valid())

Quoted 5 lines

This code will go away soon with my patch, but at least we can now figure out what to do on EDIDs with extensions instead of changing the behaviour just as a side-effect.

Is there any issue with just dropping the max() and keeping the full EDID?

Yes, we can drop the max(). Although I'm not entirely sure what the problem is that you are getting at. We always copy the entire EDID from the vbios. The EDID from the bios is either 128 bytes or a multiple of 128 bytes. The max was only there to make sure we allocated a minimum of EDID_LENGTH bytes.

Quoted 2 lines

Also if this is touched anyways, it could become kmemdup().

Will fix that up.

Thanks,

Alex

Quoted 46 lines

drivers/gpu/drm/amd/amdgpu/atombios_encoders.c line 2078

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/atombios_encoders.c +0 -0
                                            if (edid) {
                                                    memcpy((u8 *)edid,

(u8 *)&fake_edid_record->ucFakeEDIDString[0],

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/atombios_encoders.c +1 -1
-                                                          fake_edid_record-
 ucFakeEDIDLength);
+                                                          edid_size);

if

(drm_edid_is_valid(edid)) {

Quoted 3 lines

adev-
mode_info.bios_hardcoded_edid = edid; @@ -2078,13 +2082,13 @@ amdgpu_atombios_encoder_get_lcd_info(struct

amdgpu_encoder *encoder)

Quoted 4 lines
                                                    } else
                                                            kfree(edid);
                                            }

+ record +=

struct_size(fake_edid_record,

Quoted 1 line

+

ucFakeEDIDString,

drivers/gpu/drm/amd/amdgpu/atombios_encoders.c amdgpu_atombios_encoder_get_lcd_info(struct amdgpu_encoder *encoder)

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/atombios_encoders.c +3 -0
+                                                                 edid_size);
+                                   } else {
+                                           /* empty fake edid record

must be 3 bytes long */

Quoted 1 line

+ record +=

sizeof(ATOM_FAKE_EDID_PATCH_RECORD) + 1;

Quoted 8 lines
                                    }

- record += fake_edid_record-
ucFakeEDIDLength ?

drivers/gpu/drm/amd/amdgpu/atombios_encoders.c +0 -3
-                                             struct_size(fake_edid_record,
-                                                         ucFakeEDIDString,
-                                                         fake_edid_record-

ucFakeEDIDLength) :
- /* empty fake edid record

must be 3 bytes long */

Quoted 1 line

-

sizeof(ATOM_FAKE_EDID_PATCH_RECORD) + 1;

Quoted 2 lines
                                    break;
                            case

LCD_PANEL_RESOLUTION_RECORD_TYPE:

Quoted 1 line

panel_res_record =

(ATOM_PANEL_RESOLUTION_PATCH_RECORD *)record;

Quoted 3 lines

--
2.45.2

#4 lore raw
Quoted 70 lines · Alexander
Quoted 16 lines

-----Original Message-----
From: Thomas Weißschuh <[email protected]>
Sent: Tuesday, July 23, 2024 1:58 PM
To: Deucher, Alexander <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH 1/2] drm/amdgpu: properly handle vbios fake edid sizing

Quoted 8 lines · Alex Deucher

The comment in the vbios structure says:
// = 128 means EDID length is 128 bytes, otherwise the EDID length = ucFakeEDIDLength*128

This fake edid struct has not been used in a long time, so I'm not sure if there were actually any boards out there with a non-128 byte EDID, but align the code with the comment.

Quoted 5 lines
Quoted 1 line

Afaik Reported-by: should also have a Link:.

And IMO a Fixes: would also be fitting.

I can add those.

Quoted 46 lines
Quoted 4 lines
---
 .../gpu/drm/amd/amdgpu/atombios_encoders.c    | 24 +++++++++++------

--

drivers/gpu/drm/amd/amdgpu/atombios_encoders.c

Quoted 8 lines
 1 file changed, 14 insertions(+), 10 deletions(-)
drivers/gpu/drm/amd/amdgpu/atombios_encoders.c +0 -0
index 25feab188dfe..a8751a5901c6 100644
@@ -2065,12 +2065,16 @@

amdgpu_atombios_encoder_get_lcd_info(struct amdgpu_encoder *encoder)

drivers/gpu/drm/amd/amdgpu/atombios_encoders.c line 2065

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/atombios_encoders.c +0 -0
                                    fake_edid_record =

(ATOM_FAKE_EDID_PATCH_RECORD *)record;

drivers/gpu/drm/amd/amdgpu/atombios_encoders.c line 2070

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/atombios_encoders.c +0 -2
                                    if (fake_edid_record-
 ucFakeEDIDLength) {
                                            struct edid *edid;
-                                           int edid_size =
-
      max((int)EDID_LENGTH, (int)fake_edid_record->ucFakeEDIDLength);

drivers/gpu/drm/amd/amdgpu/atombios_encoders.c line 2071

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/atombios_encoders.c +0 -1
-                                           edid = kmalloc(edid_size,

GFP_KERNEL);

drivers/gpu/drm/amd/amdgpu/atombios_encoders.c line 2073

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/atombios_encoders.c +4 -0
+                                           int edid_size;
+
+                                           if (fake_edid_record-
 ucFakeEDIDLength == 128)
+                                                   edid_size =

fake_edid_record->ucFakeEDIDLength;

drivers/gpu/drm/amd/amdgpu/atombios_encoders.c line 2075

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/atombios_encoders.c +2 -0
+                                           else
+                                                   edid_size =

fake_edid_record->ucFakeEDIDLength * 128;

drivers/gpu/drm/amd/amdgpu/atombios_encoders.c line 2076

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/atombios_encoders.c +1 -0
+                                           edid =

kmalloc(max(EDID_LENGTH, edid_size), GFP_KERNEL);

This looks wrong, but it was similar before.
If the EDID contains extensions and the code tries to access those, it will be an out of bounds memory access, because the extensions were not actually copied.
(And this seems to already happen in drm_edid_is_valid())

This code will go away soon with my patch, but at least we can now figure out what to do on EDIDs with extensions instead of changing the behaviour just as a side-effect.

Quoted 7 lines
Quoted 1 line

Is there any issue with just dropping the max() and keeping the full EDID?

Yes, we can drop the max(). Although I'm not entirely sure what the problem is that you are getting at. We always copy the entire EDID from the vbios. The EDID from the bios is either 128 bytes or a multiple of 128 bytes. The max was only there to make sure we allocated a minimum of EDID_LENGTH bytes.

Somehow I got confused by max() and min(), so please ignore.