Skip to contentSkip to conversation

firmware: dmi: Don't take garbage into consideration in dmi_smbios3_present()

PATCH In review Andy Shevchenko 2 participants 5 messages Jul 26, 2022 – Jul 30, 2022 Dmi/smbios support

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 firmware: dmi: Don't take garbage into consideration in dmi_smbios3_present()

The byte at offset 6 represent length. Don't take it and drop it immediately by using proper accessor, i.e. get_unaligned_be24().

---
 drivers/firmware/dmi_scan.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/dmi_scan.c +1 -1
index b2ea318a10a4..24537ce29bc4 100644
@@ -630,7 +630,7 @@ static int __init dmi_smbios3_present(const u8 *buf)
 {
 	if (memcmp(buf, "_SM3_", 5) == 0 &&
 	    buf[6] < 32 && dmi_checksum(buf, buf[6])) {
-		dmi_ver = get_unaligned_be32(buf + 6) & 0xFFFFFF;
+		dmi_ver = get_unaligned_be24(buf + 7);
 		dmi_num = 0;			/* No longer specified */
 		dmi_len = get_unaligned_le32(buf + 12);
 		dmi_base = get_unaligned_le64(buf + 16);
Signature · 1 line
2.35.1
#1 lore raw

Hi Andy,

Quoted 2 lines · Andy Shevchenko

The byte at offset 6 represent length. Don't take it and drop it immediately by using proper accessor, i.e. get_unaligned_be24().

The subject sounds like you are fixing a bug, while this is only, at best, a minor optimization.

Quoted 10 lines
---
 drivers/firmware/dmi_scan.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/dmi_scan.c +0 -0
index b2ea318a10a4..24537ce29bc4 100644

drivers/firmware/dmi_scan.c static int __init dmi_smbios3_present(const u8 *buf) · line 636

Quoted 8 lines
drivers/firmware/dmi_scan.c +1 -1
@@ -630,7 +630,7 @@ static int __init dmi_smbios3_present(const u8 *buf)
 {
 	if (memcmp(buf, "_SM3_", 5) == 0 &&
 	    buf[6] < 32 && dmi_checksum(buf, buf[6])) {
-		dmi_ver = get_unaligned_be32(buf + 6) & 0xFFFFFF;
+		dmi_ver = get_unaligned_be24(buf + 7);
 		dmi_num = 0;			/* No longer specified */
 		dmi_len = get_unaligned_le32(buf + 12);
 		dmi_base = get_unaligned_le64(buf + 16);

I admit I did not know about get_unaligned_be24(). While I agree that it makes the source code look better, one downside is that it actually increases the binary size on x86_64. The reason is that get_unaligned_be32() is optimized by assembly instruction bswapl, while get_unaligned_be24() is not. Situation appears to be the same on ia64 and arm. Only arm64 would apparently benefit from your proposed change.

I'm not too sure what is preferred in such situations.

Signature · 2 lines
Jean Delvare
SUSE L3 Support
#2 lore raw
Quoted 6 lines · Jean Delvare
Quoted 2 lines · Andy Shevchenko

The byte at offset 6 represent length. Don't take it and drop it immediately by using proper accessor, i.e. get_unaligned_be24().

The subject sounds like you are fixing a bug, while this is only, at best, a minor optimization.

I don't know how to improve it, but it kinda a bug in a logic for non-prepared reader, esp. as specification suggests different thing about version offset.

...

Quoted 10 lines

drivers/firmware/dmi_scan.c static int __init dmi_smbios3_present(const u8 *buf) · line 633

Quoted 2 lines
drivers/firmware/dmi_scan.c +1 -1
-		dmi_ver = get_unaligned_be32(buf + 6) & 0xFFFFFF;
+		dmi_ver = get_unaligned_be24(buf + 7);

I admit I did not know about get_unaligned_be24(). While I agree that it makes the source code look better, one downside is that it actually increases the binary size on x86_64. The reason is that get_unaligned_be32() is optimized by assembly instruction bswapl, while get_unaligned_be24() is not. Situation appears to be the same on ia64 and arm. Only arm64 would apparently benefit from your proposed change.

Good to know!
But here it's not a hot path, right?

Quoted 1 line

I'm not too sure what is preferred in such situations.

For cold paths I think the correctness prevail the performance.

Alternatively we might add a comment in the code explaining the trick, although I won't like to do it.

Another way is to have a subset of 24-/48-bit unaligned accessors that use the trick specifically for hot paths with a caveat that they may access data out of the 24-/48-bit boundaries.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

Hi Andy,

Quoted 10 lines · Andy Shevchenko
Quoted 6 lines · Jean Delvare
Quoted 2 lines · Andy Shevchenko

The byte at offset 6 represent length. Don't take it and drop it immediately by using proper accessor, i.e. get_unaligned_be24().

The subject sounds like you are fixing a bug, while this is only, at best, a minor optimization.

I don't know how to improve it, but it kinda a bug in a logic for non-prepared reader, esp. as specification suggests different thing about version offset.

I didn't consider that. Having code match the documentation is indeed valuable.

Quoted 11 lines
Quoted 10 lines

drivers/firmware/dmi_scan.c static int __init dmi_smbios3_present(const u8 *buf) · line 633

Quoted 2 lines
drivers/firmware/dmi_scan.c +1 -1
-		dmi_ver = get_unaligned_be32(buf + 6) & 0xFFFFFF;
+		dmi_ver = get_unaligned_be24(buf + 7);  

I admit I did not know about get_unaligned_be24(). While I agree that it makes the source code look better, one downside is that it actually increases the binary size on x86_64. The reason is that get_unaligned_be32() is optimized by assembly instruction bswapl, while get_unaligned_be24() is not. Situation appears to be the same on ia64 and arm. Only arm64 would apparently benefit from your proposed change.

Quoted 2 lines

Good to know!
But here it's not a hot path, right?

True.

Quoted 3 lines
Quoted 1 line

I'm not too sure what is preferred in such situations.

For cold paths I think the correctness prevail the performance.

You have a point.

Quoted 6 lines

Alternatively we might add a comment in the code explaining the trick, although I won't like to do it.

Another way is to have a subset of 24-/48-bit unaligned accessors that use the trick specifically for hot paths with a caveat that they may access data out of the 24-/48-bit boundaries.

I considered that, but that's too hackish for me and could easily cause confusion leading to improper use. Not worth it. Let's keep things simple and understandable.

So I'll apply your patch, thanks for contributing it.

Signature · 2 lines
Jean Delvare
SUSE L3 Support
#4 lore raw
Quoted 1 line · Jean Delvare

On Fri, 29 Jul 2022 21:23:08 +0300, Andy Shevchenko wrote:

...

Quoted 1 line

So I'll apply your patch, thanks for contributing it.

Thanks for a nice discussion, I have learnt something new!

Signature · 2 lines
With Best Regards,
Andy Shevchenko